]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
qsort: elf_sort_sections use of target_index
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
82704155 2 Copyright (C) 1995-2019 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
28caa186
AM
35/* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38struct elf_info_failed
39{
40 struct bfd_link_info *info;
28caa186
AM
41 bfd_boolean failed;
42};
43
44/* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47struct elf_find_verdep_info
48{
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55};
56
57static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
2f0c68f2
CM
60asection *
61_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64{
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
07d6d2b8 79 return h->root.u.def.section;
2f0c68f2
CM
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99}
100
d98685ac
AM
101/* Define a symbol in a dynamic linkage section. */
102
103struct elf_link_hash_entry *
104_bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108{
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
ccabcbe5 111 const struct elf_backend_data *bed;
d98685ac
AM
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
ad32986f 121 bh = &h->root;
d98685ac 122 }
ad32986f
NC
123 else
124 bh = NULL;
d98685ac 125
cf18fda4 126 bed = get_elf_backend_data (abfd);
d98685ac 127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 128 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
ad32986f 132 BFD_ASSERT (h != NULL);
d98685ac 133 h->def_regular = 1;
e28df02b 134 h->non_elf = 0;
12b2843a 135 h->root.linker_def = 1;
d98685ac 136 h->type = STT_OBJECT;
00b7642b
AM
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 139
ccabcbe5 140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
141 return h;
142}
143
b34976b6 144bfd_boolean
268b6b39 145_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
146{
147 flagword flags;
aad5d350 148 asection *s;
252b5132 149 struct elf_link_hash_entry *h;
9c5bfbb7 150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 151 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
152
153 /* This function may be called more than once. */
ce558b89 154 if (htab->sgot != NULL)
b34976b6 155 return TRUE;
252b5132 156
e5a52504 157 flags = bed->dynamic_sec_flags;
252b5132 158
14b2f831
AM
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
6de2ae4a 164 if (s == NULL
fd361982 165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
6de2ae4a
L
166 return FALSE;
167 htab->srelgot = s;
252b5132 168
14b2f831 169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d 170 if (s == NULL
fd361982 171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
64e77c6d
L
172 return FALSE;
173 htab->sgot = s;
174
252b5132
RH
175 if (bed->want_got_plt)
176 {
14b2f831 177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 178 if (s == NULL
fd361982 179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 180 return FALSE;
6de2ae4a 181 htab->sgotplt = s;
252b5132
RH
182 }
183
64e77c6d
L
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
2517a57f
AM
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
6de2ae4a
L
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
2517a57f 195 elf_hash_table (info)->hgot = h;
d98685ac
AM
196 if (h == NULL)
197 return FALSE;
2517a57f 198 }
252b5132 199
b34976b6 200 return TRUE;
252b5132
RH
201}
202\f
7e9f0867
AM
203/* Create a strtab to hold the dynamic symbol names. */
204static bfd_boolean
205_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206{
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
6cd255ca
L
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
57963c05 219 asection *s;
6cd255ca 220 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 221 if ((ibfd->flags
57963c05
AM
222 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 224 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
225 && !((s = ibfd->sections) != NULL
226 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
227 {
228 abfd = ibfd;
229 break;
230 }
231 }
232 hash_table->dynobj = abfd;
233 }
7e9f0867
AM
234
235 if (hash_table->dynstr == NULL)
236 {
237 hash_table->dynstr = _bfd_elf_strtab_init ();
238 if (hash_table->dynstr == NULL)
239 return FALSE;
240 }
241 return TRUE;
242}
243
45d6a902
AM
244/* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
252b5132 250
b34976b6 251bfd_boolean
268b6b39 252_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 253{
45d6a902 254 flagword flags;
91d6fa6a 255 asection *s;
9c5bfbb7 256 const struct elf_backend_data *bed;
9637f6ef 257 struct elf_link_hash_entry *h;
252b5132 258
0eddce27 259 if (! is_elf_hash_table (info->hash))
45d6a902
AM
260 return FALSE;
261
262 if (elf_hash_table (info)->dynamic_sections_created)
263 return TRUE;
264
7e9f0867
AM
265 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
266 return FALSE;
45d6a902 267
7e9f0867 268 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
269 bed = get_elf_backend_data (abfd);
270
271 flags = bed->dynamic_sec_flags;
45d6a902
AM
272
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
9b8b325a 275 if (bfd_link_executable (info) && !info->nointerp)
252b5132 276 {
14b2f831
AM
277 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 flags | SEC_READONLY);
3496cb2a 279 if (s == NULL)
45d6a902
AM
280 return FALSE;
281 }
bb0deeff 282
45d6a902
AM
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
14b2f831
AM
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 flags | SEC_READONLY);
45d6a902 287 if (s == NULL
fd361982 288 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
289 return FALSE;
290
14b2f831
AM
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 flags | SEC_READONLY);
45d6a902 293 if (s == NULL
fd361982 294 || !bfd_set_section_alignment (s, 1))
45d6a902
AM
295 return FALSE;
296
14b2f831
AM
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 flags | SEC_READONLY);
45d6a902 299 if (s == NULL
fd361982 300 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
301 return FALSE;
302
14b2f831
AM
303 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 flags | SEC_READONLY);
45d6a902 305 if (s == NULL
fd361982 306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902 307 return FALSE;
cae1fbbb 308 elf_hash_table (info)->dynsym = s;
45d6a902 309
14b2f831
AM
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 flags | SEC_READONLY);
3496cb2a 312 if (s == NULL)
45d6a902
AM
313 return FALSE;
314
14b2f831 315 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 316 if (s == NULL
fd361982 317 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
318 return FALSE;
319
320 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
9637f6ef
L
326 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327 elf_hash_table (info)->hdynamic = h;
328 if (h == NULL)
45d6a902
AM
329 return FALSE;
330
fdc90cb4
JJ
331 if (info->emit_hash)
332 {
14b2f831
AM
333 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 flags | SEC_READONLY);
fdc90cb4 335 if (s == NULL
fd361982 336 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
337 return FALSE;
338 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
339 }
340
f16a9783 341 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
fdc90cb4 342 {
14b2f831
AM
343 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 flags | SEC_READONLY);
fdc90cb4 345 if (s == NULL
fd361982 346 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
347 return FALSE;
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed->s->arch_size == 64)
352 elf_section_data (s)->this_hdr.sh_entsize = 0;
353 else
354 elf_section_data (s)->this_hdr.sh_entsize = 4;
355 }
45d6a902
AM
356
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
894891db
NC
360 if (bed->elf_backend_create_dynamic_sections == NULL
361 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
362 return FALSE;
363
364 elf_hash_table (info)->dynamic_sections_created = TRUE;
365
366 return TRUE;
367}
368
369/* Create dynamic sections when linking against a dynamic object. */
370
371bfd_boolean
268b6b39 372_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
373{
374 flagword flags, pltflags;
7325306f 375 struct elf_link_hash_entry *h;
45d6a902 376 asection *s;
9c5bfbb7 377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 378 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 379
252b5132
RH
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
e5a52504 382 flags = bed->dynamic_sec_flags;
252b5132
RH
383
384 pltflags = flags;
252b5132 385 if (bed->plt_not_loaded)
6df4d94c
MM
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
5d1634d7 389 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
390 else
391 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
392 if (bed->plt_readonly)
393 pltflags |= SEC_READONLY;
394
14b2f831 395 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 396 if (s == NULL
fd361982 397 || !bfd_set_section_alignment (s, bed->plt_alignment))
b34976b6 398 return FALSE;
6de2ae4a 399 htab->splt = s;
252b5132 400
d98685ac
AM
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
402 .plt section. */
7325306f
RS
403 if (bed->want_plt_sym)
404 {
405 h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info)->hplt = h;
408 if (h == NULL)
409 return FALSE;
410 }
252b5132 411
14b2f831
AM
412 s = bfd_make_section_anyway_with_flags (abfd,
413 (bed->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags | SEC_READONLY);
252b5132 416 if (s == NULL
fd361982 417 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 418 return FALSE;
6de2ae4a 419 htab->srelplt = s;
252b5132
RH
420
421 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 422 return FALSE;
252b5132 423
3018b441
RH
424 if (bed->want_dynbss)
425 {
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
14b2f831 432 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 433 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 434 if (s == NULL)
b34976b6 435 return FALSE;
9d19e4fd 436 htab->sdynbss = s;
252b5132 437
5474d94f
AM
438 if (bed->want_dynrelro)
439 {
440 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
5474d94f 443 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 444 flags);
5474d94f
AM
445 if (s == NULL)
446 return FALSE;
447 htab->sdynrelro = s;
448 }
449
3018b441 450 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
460 copy relocs. */
9d19e4fd 461 if (bfd_link_executable (info))
3018b441 462 {
14b2f831
AM
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags | SEC_READONLY);
3018b441 467 if (s == NULL
fd361982 468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 469 return FALSE;
9d19e4fd 470 htab->srelbss = s;
5474d94f
AM
471
472 if (bed->want_dynrelro)
473 {
474 s = (bfd_make_section_anyway_with_flags
475 (abfd, (bed->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags | SEC_READONLY));
478 if (s == NULL
fd361982 479 || !bfd_set_section_alignment (s, bed->s->log_file_align))
5474d94f
AM
480 return FALSE;
481 htab->sreldynrelro = s;
482 }
3018b441 483 }
252b5132
RH
484 }
485
b34976b6 486 return TRUE;
252b5132
RH
487}
488\f
252b5132
RH
489/* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
495 one. */
496
b34976b6 497bfd_boolean
c152c796
AM
498bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 struct elf_link_hash_entry *h)
252b5132
RH
500{
501 if (h->dynindx == -1)
502 {
2b0f7ef9 503 struct elf_strtab_hash *dynstr;
68b6ddd0 504 char *p;
252b5132 505 const char *name;
ef53be89 506 size_t indx;
252b5132 507
7a13edea
NC
508 /* XXX: The ABI draft says the linker must turn hidden and
509 internal symbols into STB_LOCAL symbols when producing the
510 DSO. However, if ld.so honors st_other in the dynamic table,
511 this would not be necessary. */
512 switch (ELF_ST_VISIBILITY (h->other))
513 {
514 case STV_INTERNAL:
515 case STV_HIDDEN:
9d6eee78
L
516 if (h->root.type != bfd_link_hash_undefined
517 && h->root.type != bfd_link_hash_undefweak)
38048eb9 518 {
f5385ebf 519 h->forced_local = 1;
67687978
PB
520 if (!elf_hash_table (info)->is_relocatable_executable)
521 return TRUE;
7a13edea 522 }
0444bdd4 523
7a13edea
NC
524 default:
525 break;
526 }
527
252b5132
RH
528 h->dynindx = elf_hash_table (info)->dynsymcount;
529 ++elf_hash_table (info)->dynsymcount;
530
531 dynstr = elf_hash_table (info)->dynstr;
532 if (dynstr == NULL)
533 {
534 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 535 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 536 if (dynstr == NULL)
b34976b6 537 return FALSE;
252b5132
RH
538 }
539
540 /* We don't put any version information in the dynamic string
aad5d350 541 table. */
252b5132
RH
542 name = h->root.root.string;
543 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
544 if (p != NULL)
545 /* We know that the p points into writable memory. In fact,
546 there are only a few symbols that have read-only names, being
547 those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 by the backends. Most symbols will have names pointing into
549 an ELF string table read from a file, or to objalloc memory. */
550 *p = 0;
551
552 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
553
554 if (p != NULL)
555 *p = ELF_VER_CHR;
252b5132 556
ef53be89 557 if (indx == (size_t) -1)
b34976b6 558 return FALSE;
252b5132
RH
559 h->dynstr_index = indx;
560 }
561
b34976b6 562 return TRUE;
252b5132 563}
45d6a902 564\f
55255dae
L
565/* Mark a symbol dynamic. */
566
28caa186 567static void
55255dae 568bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
569 struct elf_link_hash_entry *h,
570 Elf_Internal_Sym *sym)
55255dae 571{
40b36307 572 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 573
40b36307 574 /* It may be called more than once on the same H. */
0e1862bb 575 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
576 return;
577
40b36307
L
578 if ((info->dynamic_data
579 && (h->type == STT_OBJECT
b8871f35 580 || h->type == STT_COMMON
40b36307 581 || (sym != NULL
b8871f35
L
582 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
583 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 584 || (d != NULL
73ec947d 585 && h->non_elf
40b36307 586 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
587 {
588 h->dynamic = 1;
589 /* NB: If a symbol is made dynamic by --dynamic-list, it has
590 non-IR reference. */
591 h->root.non_ir_ref_dynamic = 1;
592 }
55255dae
L
593}
594
45d6a902
AM
595/* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
597
598bfd_boolean
fe21a8fc
L
599bfd_elf_record_link_assignment (bfd *output_bfd,
600 struct bfd_link_info *info,
268b6b39 601 const char *name,
fe21a8fc
L
602 bfd_boolean provide,
603 bfd_boolean hidden)
45d6a902 604{
00cbee0a 605 struct elf_link_hash_entry *h, *hv;
4ea42fb7 606 struct elf_link_hash_table *htab;
00cbee0a 607 const struct elf_backend_data *bed;
45d6a902 608
0eddce27 609 if (!is_elf_hash_table (info->hash))
45d6a902
AM
610 return TRUE;
611
4ea42fb7
AM
612 htab = elf_hash_table (info);
613 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 614 if (h == NULL)
4ea42fb7 615 return provide;
45d6a902 616
8e2a4f11
AM
617 if (h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
619
0f550b3d
L
620 if (h->versioned == unknown)
621 {
622 /* Set versioned if symbol version is unknown. */
623 char *version = strrchr (name, ELF_VER_CHR);
624 if (version)
625 {
626 if (version > name && version[-1] != ELF_VER_CHR)
627 h->versioned = versioned_hidden;
628 else
629 h->versioned = versioned;
630 }
631 }
632
73ec947d
AM
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
635 if (h->non_elf)
636 {
637 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 h->non_elf = 0;
639 }
640
00cbee0a 641 switch (h->root.type)
77cfaee6 642 {
00cbee0a
L
643 case bfd_link_hash_defined:
644 case bfd_link_hash_defweak:
645 case bfd_link_hash_common:
646 break;
647 case bfd_link_hash_undefweak:
648 case bfd_link_hash_undefined:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
4ea42fb7 652 h->root.type = bfd_link_hash_new;
77cfaee6
AM
653 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
655 break;
656 case bfd_link_hash_new:
00cbee0a
L
657 break;
658 case bfd_link_hash_indirect:
659 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 660 the versioned symbol point to this one. */
00cbee0a
L
661 bed = get_elf_backend_data (output_bfd);
662 hv = h;
663 while (hv->root.type == bfd_link_hash_indirect
664 || hv->root.type == bfd_link_hash_warning)
665 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 /* We don't need to update h->root.u since linker will set them
667 later. */
668 h->root.type = bfd_link_hash_undefined;
669 hv->root.type = bfd_link_hash_indirect;
670 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672 break;
8e2a4f11
AM
673 default:
674 BFD_FAIL ();
c2596ca5 675 return FALSE;
55255dae 676 }
45d6a902
AM
677
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
682 if (provide
f5385ebf
AM
683 && h->def_dynamic
684 && !h->def_regular)
45d6a902
AM
685 h->root.type = bfd_link_hash_undefined;
686
48e30f52
L
687 /* If this symbol is currently defined by a dynamic object, but not
688 by a regular object, then clear out any version information because
689 the symbol will not be associated with the dynamic object any
690 more. */
691 if (h->def_dynamic && !h->def_regular)
b531344c
MR
692 h->verinfo.verdef = NULL;
693
694 /* Make sure this symbol is not garbage collected. */
695 h->mark = 1;
45d6a902 696
f5385ebf 697 h->def_regular = 1;
45d6a902 698
eb8476a6 699 if (hidden)
fe21a8fc 700 {
91d6fa6a 701 bed = get_elf_backend_data (output_bfd);
b8297068
AM
702 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
703 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
704 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
705 }
706
6fa3860b
PB
707 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 and executables. */
0e1862bb 709 if (!bfd_link_relocatable (info)
6fa3860b
PB
710 && h->dynindx != -1
711 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
712 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
713 h->forced_local = 1;
714
f5385ebf
AM
715 if ((h->def_dynamic
716 || h->ref_dynamic
6b3b0ab8
L
717 || bfd_link_dll (info)
718 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 719 && !h->forced_local
45d6a902
AM
720 && h->dynindx == -1)
721 {
c152c796 722 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
723 return FALSE;
724
725 /* If this is a weak defined symbol, and we know a corresponding
726 real symbol from the same dynamic object, make sure the real
727 symbol is also made into a dynamic symbol. */
60d67dc8 728 if (h->is_weakalias)
45d6a902 729 {
60d67dc8
AM
730 struct elf_link_hash_entry *def = weakdef (h);
731
732 if (def->dynindx == -1
733 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
734 return FALSE;
735 }
736 }
737
738 return TRUE;
739}
42751cf3 740
8c58d23b
AM
741/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
742 success, and 2 on a failure caused by attempting to record a symbol
743 in a discarded section, eg. a discarded link-once section symbol. */
744
745int
c152c796
AM
746bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
747 bfd *input_bfd,
748 long input_indx)
8c58d23b
AM
749{
750 bfd_size_type amt;
751 struct elf_link_local_dynamic_entry *entry;
752 struct elf_link_hash_table *eht;
753 struct elf_strtab_hash *dynstr;
ef53be89 754 size_t dynstr_index;
8c58d23b
AM
755 char *name;
756 Elf_External_Sym_Shndx eshndx;
757 char esym[sizeof (Elf64_External_Sym)];
758
0eddce27 759 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
760 return 0;
761
762 /* See if the entry exists already. */
763 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
764 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 return 1;
766
767 amt = sizeof (*entry);
a50b1753 768 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
769 if (entry == NULL)
770 return 0;
771
772 /* Go find the symbol, so that we can find it's name. */
773 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 774 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
775 {
776 bfd_release (input_bfd, entry);
777 return 0;
778 }
779
780 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 781 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
782 {
783 asection *s;
784
785 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
786 if (s == NULL || bfd_is_abs_section (s->output_section))
787 {
788 /* We can still bfd_release here as nothing has done another
789 bfd_alloc. We can't do this later in this function. */
790 bfd_release (input_bfd, entry);
791 return 2;
792 }
793 }
794
795 name = (bfd_elf_string_from_elf_section
796 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
797 entry->isym.st_name));
798
799 dynstr = elf_hash_table (info)->dynstr;
800 if (dynstr == NULL)
801 {
802 /* Create a strtab to hold the dynamic symbol names. */
803 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
804 if (dynstr == NULL)
805 return 0;
806 }
807
b34976b6 808 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 809 if (dynstr_index == (size_t) -1)
8c58d23b
AM
810 return 0;
811 entry->isym.st_name = dynstr_index;
812
813 eht = elf_hash_table (info);
814
815 entry->next = eht->dynlocal;
816 eht->dynlocal = entry;
817 entry->input_bfd = input_bfd;
818 entry->input_indx = input_indx;
819 eht->dynsymcount++;
820
821 /* Whatever binding the symbol had before, it's now local. */
822 entry->isym.st_info
823 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
824
825 /* The dynindx will be set at the end of size_dynamic_sections. */
826
827 return 1;
828}
829
30b30c21 830/* Return the dynindex of a local dynamic symbol. */
42751cf3 831
30b30c21 832long
268b6b39
AM
833_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
834 bfd *input_bfd,
835 long input_indx)
30b30c21
RH
836{
837 struct elf_link_local_dynamic_entry *e;
838
839 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
840 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
841 return e->dynindx;
842 return -1;
843}
844
845/* This function is used to renumber the dynamic symbols, if some of
846 them are removed because they are marked as local. This is called
847 via elf_link_hash_traverse. */
848
b34976b6 849static bfd_boolean
268b6b39
AM
850elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 void *data)
42751cf3 852{
a50b1753 853 size_t *count = (size_t *) data;
30b30c21 854
6fa3860b
PB
855 if (h->forced_local)
856 return TRUE;
857
858 if (h->dynindx != -1)
859 h->dynindx = ++(*count);
860
861 return TRUE;
862}
863
864
865/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866 STB_LOCAL binding. */
867
868static bfd_boolean
869elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 void *data)
871{
a50b1753 872 size_t *count = (size_t *) data;
6fa3860b 873
6fa3860b
PB
874 if (!h->forced_local)
875 return TRUE;
876
42751cf3 877 if (h->dynindx != -1)
30b30c21
RH
878 h->dynindx = ++(*count);
879
b34976b6 880 return TRUE;
42751cf3 881}
30b30c21 882
aee6f5b4
AO
883/* Return true if the dynamic symbol for a given section should be
884 omitted when creating a shared library. */
885bfd_boolean
d00dd7dc
AM
886_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
887 struct bfd_link_info *info,
888 asection *p)
aee6f5b4 889{
74541ad4 890 struct elf_link_hash_table *htab;
ca55926c 891 asection *ip;
74541ad4 892
aee6f5b4
AO
893 switch (elf_section_data (p)->this_hdr.sh_type)
894 {
895 case SHT_PROGBITS:
896 case SHT_NOBITS:
897 /* If sh_type is yet undecided, assume it could be
898 SHT_PROGBITS/SHT_NOBITS. */
899 case SHT_NULL:
74541ad4 900 htab = elf_hash_table (info);
74541ad4
AM
901 if (htab->text_index_section != NULL)
902 return p != htab->text_index_section && p != htab->data_index_section;
903
ca55926c 904 return (htab->dynobj != NULL
3d4d4302 905 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 906 && ip->output_section == p);
aee6f5b4
AO
907
908 /* There shouldn't be section relative relocations
909 against any other section. */
910 default:
911 return TRUE;
912 }
913}
914
d00dd7dc
AM
915bfd_boolean
916_bfd_elf_omit_section_dynsym_all
917 (bfd *output_bfd ATTRIBUTE_UNUSED,
918 struct bfd_link_info *info ATTRIBUTE_UNUSED,
919 asection *p ATTRIBUTE_UNUSED)
920{
921 return TRUE;
922}
923
062e2358 924/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
925 symbol for each output section, which come first. Next come symbols
926 which have been forced to local binding. Then all of the back-end
927 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
928 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929 (This prevents the early call before elf_backend_init_index_section
930 and strip_excluded_output_sections setting dynindx for sections
931 that are stripped.) */
30b30c21 932
554220db
AM
933static unsigned long
934_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
935 struct bfd_link_info *info,
936 unsigned long *section_sym_count)
30b30c21
RH
937{
938 unsigned long dynsymcount = 0;
63f452a8 939 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 940
0e1862bb
L
941 if (bfd_link_pic (info)
942 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 943 {
aee6f5b4 944 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
945 asection *p;
946 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 947 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 948 && (p->flags & SEC_ALLOC) != 0
7f923b7f 949 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
951 {
952 ++dynsymcount;
953 if (do_sec)
954 elf_section_data (p)->dynindx = dynsymcount;
955 }
956 else if (do_sec)
74541ad4 957 elf_section_data (p)->dynindx = 0;
30b30c21 958 }
63f452a8
AM
959 if (do_sec)
960 *section_sym_count = dynsymcount;
30b30c21 961
6fa3860b
PB
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
964 &dynsymcount);
965
30b30c21
RH
966 if (elf_hash_table (info)->dynlocal)
967 {
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
971 }
90ac2420 972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
973
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
976 &dynsymcount);
977
d5486c43
L
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981 .dynamic section. */
982 dynsymcount++;
30b30c21 983
ccabcbe5
AM
984 elf_hash_table (info)->dynsymcount = dynsymcount;
985 return dynsymcount;
30b30c21 986}
252b5132 987
54ac0771
L
988/* Merge st_other field. */
989
990static void
991elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 992 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 993 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
994{
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996
997 /* If st_other has a processor-specific meaning, specific
cd3416da 998 code might be needed here. */
54ac0771
L
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 dynamic);
1002
cd3416da 1003 if (!dynamic)
54ac0771 1004 {
cd3416da
AM
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1007
cd3416da
AM
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1012 }
b8417128
AM
1013 else if (definition
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1016 h->protected_def = 1;
54ac0771
L
1017}
1018
4f3fedcf
AM
1019/* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
45d6a902 1031
8a56bd02 1032static bfd_boolean
268b6b39
AM
1033_bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1035 const char *name,
1036 Elf_Internal_Sym *sym,
1037 asection **psec,
1038 bfd_vma *pvalue,
4f3fedcf
AM
1039 struct elf_link_hash_entry **sym_hash,
1040 bfd **poldbfd,
37a9e49a 1041 bfd_boolean *pold_weak,
af44c138 1042 unsigned int *pold_alignment,
268b6b39
AM
1043 bfd_boolean *skip,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
6e33951e
L
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
252b5132 1048{
7479dfd4 1049 asection *sec, *oldsec;
45d6a902 1050 struct elf_link_hash_entry *h;
90c984fc 1051 struct elf_link_hash_entry *hi;
45d6a902
AM
1052 struct elf_link_hash_entry *flip;
1053 int bind;
1054 bfd *oldbfd;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1057 const struct elf_backend_data *bed;
6e33951e 1058 char *new_version;
93f4de39 1059 bfd_boolean default_sym = *matched;
45d6a902
AM
1060
1061 *skip = FALSE;
1062 *override = FALSE;
1063
1064 sec = *psec;
1065 bind = ELF_ST_BIND (sym->st_info);
1066
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069 else
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072 if (h == NULL)
1073 return FALSE;
1074 *sym_hash = h;
252b5132 1075
88ba32a0
L
1076 bed = get_elf_backend_data (abfd);
1077
6e33951e 1078 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1079 if (h->versioned != unversioned)
6e33951e 1080 {
422f1182
L
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1083 if (new_version)
1084 {
1085 if (h->versioned == unknown)
1086 {
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1089 else
1090 h->versioned = versioned;
1091 }
1092 new_version += 1;
1093 if (new_version[0] == '\0')
1094 new_version = NULL;
1095 }
1096 else
1097 h->versioned = unversioned;
6e33951e 1098 }
422f1182
L
1099 else
1100 new_version = NULL;
6e33951e 1101
90c984fc
L
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1104 hi = h;
45d6a902
AM
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108
6e33951e
L
1109 if (!*matched)
1110 {
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1112 *matched = TRUE;
1113 else
1114 {
ae7683d2 1115 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1116 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1117 true if the new symbol is only visible to the symbol with
6e33951e 1118 the same symbol version. */
422f1182
L
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1123 aren't hidden. */
1124 *matched = TRUE;
1125 else
1126 {
1127 /* OLD_VERSION is the symbol version of the existing
1128 symbol. */
422f1182
L
1129 char *old_version;
1130
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1133 ELF_VER_CHR) + 1;
1134 else
1135 old_version = NULL;
6e33951e
L
1136
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1143 }
1144 }
1145 }
1146
934bce08
AM
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148 existing symbol. */
1149
1150 oldbfd = NULL;
1151 oldsec = NULL;
1152 switch (h->root.type)
1153 {
1154 default:
1155 break;
1156
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1160 break;
1161
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1166 break;
1167
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1171 if (pold_alignment)
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1173 break;
1174 }
1175 if (poldbfd && *poldbfd == NULL)
1176 *poldbfd = oldbfd;
1177
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1182 if (pold_weak)
1183 *pold_weak = oldweak;
1184
40b36307 1185 /* We have to check it for every instance since the first few may be
ee659f1f 1186 references and not all compilers emit symbol type for undefined
40b36307
L
1187 symbols. */
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189
ee659f1f
AM
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1192
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1194
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1202 if (newdyn)
1203 {
1204 if (bfd_is_und_section (sec))
1205 {
1206 if (bind != STB_WEAK)
1207 {
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1210 }
1211 }
1212 else
1213 {
6e33951e
L
1214 /* Update the existing symbol only if they match. */
1215 if (*matched)
1216 h->dynamic_def = 1;
ee659f1f
AM
1217 hi->dynamic_def = 1;
1218 }
1219 }
1220
45d6a902
AM
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1224
1225 if (h->root.type == bfd_link_hash_new)
252b5132 1226 {
f5385ebf 1227 h->non_elf = 0;
45d6a902
AM
1228 return TRUE;
1229 }
252b5132 1230
45d6a902
AM
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1237 if (abfd == oldbfd
895fa45f 1238 && (newweak || oldweak)
45d6a902 1239 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1240 || !h->def_regular))
45d6a902
AM
1241 return TRUE;
1242
707bba77 1243 olddyn = FALSE;
45d6a902
AM
1244 if (oldbfd != NULL)
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1246 else if (oldsec != NULL)
45d6a902 1247 {
707bba77 1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1249 indices used by MIPS ELF. */
707bba77 1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1251 }
252b5132 1252
1a3b5c34
AM
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1255 if (oldbfd != NULL
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1258 {
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1261 }
1262
45d6a902
AM
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1265
707bba77 1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1267
707bba77
AM
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
202ac193 1270 && h->root.type != bfd_link_hash_common);
45d6a902 1271
0a36a439
L
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1274
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1280
c5d37467 1281 if (!(newfunc && oldfunc)
5b677558
AM
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
c5d37467
AM
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1287 {
c5d37467
AM
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1293 if (newdyn
1294 && !olddyn)
1295 {
1296 *skip = TRUE;
1297 return TRUE;
1298 }
1299
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1302 dynamic state. */
1303 if (hi != h
1304 && !newdyn
1305 && olddyn)
1306 {
1307 h = hi;
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
1311 h->def_dynamic = 0;
1312 h->dynamic_def = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 {
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1317 }
1318 else
1319 {
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1322 }
1323 return TRUE;
1324 }
580a2b6e
L
1325 }
1326
4c34aff8
AM
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1330 if (oldbfd != NULL
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1335 {
1336 bfd *ntbfd, *tbfd;
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1339
1340 if (h->type == STT_TLS)
1341 {
3b36f7e6 1342 ntbfd = abfd;
7479dfd4
L
1343 ntsec = sec;
1344 ntdef = newdef;
1345 tbfd = oldbfd;
1346 tsec = oldsec;
1347 tdef = olddef;
1348 }
1349 else
1350 {
1351 ntbfd = oldbfd;
1352 ntsec = oldsec;
1353 ntdef = olddef;
1354 tbfd = abfd;
1355 tsec = sec;
1356 tdef = newdef;
1357 }
1358
1359 if (tdef && ntdef)
4eca0228 1360 _bfd_error_handler
695344c0 1361 /* xgettext:c-format */
871b3ab2
AM
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1365 else if (!tdef && !ntdef)
4eca0228 1366 _bfd_error_handler
695344c0 1367 /* xgettext:c-format */
871b3ab2
AM
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
c08bb8dd 1370 h->root.root.string, tbfd, ntbfd);
7479dfd4 1371 else if (tdef)
4eca0228 1372 _bfd_error_handler
695344c0 1373 /* xgettext:c-format */
871b3ab2
AM
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
c08bb8dd 1376 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1377 else
4eca0228 1378 _bfd_error_handler
695344c0 1379 /* xgettext:c-format */
871b3ab2
AM
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1382 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1383
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1386 }
1387
45d6a902
AM
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1390 if (newdyn
9c7a29a3 1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1392 && !bfd_is_und_section (sec))
1393 {
1394 *skip = TRUE;
1395 /* Make sure this symbol is dynamic. */
f5385ebf 1396 h->ref_dynamic = 1;
90c984fc 1397 hi->ref_dynamic = 1;
45d6a902
AM
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1400
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1403 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1404 else
1405 return TRUE;
1406 }
1407 else if (!newdyn
9c7a29a3 1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1409 && h->def_dynamic)
45d6a902
AM
1410 {
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
6c9b78e6 1414 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1415 {
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1420 if (h->ref_regular)
1421 {
6c9b78e6 1422 hi->root.type = h->root.type;
d2dee3b2 1423 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1425
6c9b78e6 1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1428 {
aed81c4e
MR
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1433 h->ref_dynamic = 0;
d2dee3b2
L
1434 }
1435 else
aed81c4e
MR
1436 h->ref_dynamic = 1;
1437
1438 h->def_dynamic = 0;
aed81c4e
MR
1439 /* FIXME: Should we check type and size for protected symbol? */
1440 h->size = 0;
1441 h->type = 0;
1442
6c9b78e6 1443 h = hi;
d2dee3b2
L
1444 }
1445 else
6c9b78e6 1446 h = hi;
d2dee3b2 1447 }
1de1a317 1448
f5eda473
AM
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1457 {
1de1a317 1458 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1459 h->root.u.undef.abfd = abfd;
1460 }
1461 else
1462 {
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1465 }
1466
f5eda473 1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1468 {
f5eda473
AM
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1473 h->ref_dynamic = 0;
45d6a902 1474 }
f5eda473
AM
1475 else
1476 h->ref_dynamic = 1;
1477 h->def_dynamic = 0;
45d6a902
AM
1478 /* FIXME: Should we check type and size for protected symbol? */
1479 h->size = 0;
1480 h->type = 0;
1481 return TRUE;
1482 }
14a793b2 1483
15b43f48
AM
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1491
165f707a
AM
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1496
15b43f48
AM
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1499
165f707a 1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1501 newweak = FALSE;
15b43f48 1502 if (olddef && newdyn)
0f8a2703
AM
1503 oldweak = FALSE;
1504
d334575b 1505 /* Allow changes between different types of function symbol. */
0a36a439 1506 if (newfunc && oldfunc)
fcb93ecf
PB
1507 *type_change_ok = TRUE;
1508
79349b09
AM
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
252b5132 1512
79349b09
AM
1513 if (oldweak
1514 || newweak
1515 || (newdef
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1521
1522 if (*type_change_ok
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
45d6a902 1525
45d6a902
AM
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1537 libraries.
1538
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1541
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1547 harmless. */
1548
1549 if (newdyn
1550 && newdef
79349b09 1551 && !newweak
45d6a902
AM
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1554 && sym->st_size > 0
0a36a439 1555 && !newfunc)
45d6a902
AM
1556 newdyncommon = TRUE;
1557 else
1558 newdyncommon = FALSE;
1559
1560 if (olddyn
1561 && olddef
1562 && h->root.type == bfd_link_hash_defined
f5385ebf 1563 && h->def_dynamic
45d6a902
AM
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566 && h->size > 0
0a36a439 1567 && !oldfunc)
45d6a902
AM
1568 olddyncommon = TRUE;
1569 else
1570 olddyncommon = FALSE;
1571
a4d8e49b
L
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
5d13b3b3
AM
1574 if (bed->merge_symbol != NULL)
1575 {
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 return FALSE;
1578 sec = *psec;
1579 }
a4d8e49b 1580
a83ef4d1
L
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
93f4de39 1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1584 && !default_sym && h->def_regular
1585 && !(oldbfd != NULL
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1588 {
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1592 *skip = TRUE;
1593 return TRUE;
1594 }
1595
45d6a902
AM
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1598 two. */
1599
1600 if (olddyncommon
1601 && newdyncommon
1602 && sym->st_size != h->size)
1603 {
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1609
1a72702b
AM
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
252b5132 1614
45d6a902 1615 *size_change_ok = TRUE;
252b5132
RH
1616 }
1617
45d6a902
AM
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1624
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
8170f769 1630 object to override a weak symbol in a shared object. */
45d6a902
AM
1631
1632 if (newdyn
1633 && newdef
77cfaee6 1634 && (olddef
45d6a902 1635 || (h->root.type == bfd_link_hash_common
8170f769 1636 && (newweak || newfunc))))
45d6a902
AM
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
252b5132 1641
45d6a902
AM
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
252b5132 1644
45d6a902
AM
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
252b5132 1650
45d6a902
AM
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1653 }
1654
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1659 right thing. */
45d6a902
AM
1660
1661 if (newdyncommon
1662 && h->root.type == bfd_link_hash_common)
1663 {
1664 *override = TRUE;
1665 newdef = FALSE;
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
a4d8e49b 1668 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1669 *size_change_ok = TRUE;
1670 }
1671
c5e2cead 1672 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1673 if (newdef && olddef && newweak)
54ac0771 1674 {
35ed3f94 1675 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1678 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1679 {
1680 newdef = FALSE;
1681 *skip = TRUE;
1682 }
54ac0771
L
1683
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1686 local symbol. */
b8417128 1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_INTERNAL:
1692 case STV_HIDDEN:
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 break;
1695 }
1696 }
c5e2cead 1697
45d6a902
AM
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1703
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
0f8a2703 1706 symbol is a function or is weak. */
45d6a902
AM
1707
1708 flip = NULL;
77cfaee6 1709 if (!newdyn
45d6a902
AM
1710 && (newdef
1711 || (bfd_is_com_section (sec)
0a36a439 1712 && (oldweak || oldfunc)))
45d6a902
AM
1713 && olddyn
1714 && olddef
f5385ebf 1715 && h->def_dynamic)
45d6a902
AM
1716 {
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1719 new definition. */
1720
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1724
1725 olddef = FALSE;
1726 olddyncommon = FALSE;
1727
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1730
1731 if (bfd_is_com_section (sec))
0a36a439
L
1732 {
1733 if (oldfunc)
1734 {
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1737 function. */
1738 h->def_dynamic = 0;
1739 h->type = STT_NOTYPE;
1740 }
1741 *type_change_ok = TRUE;
1742 }
45d6a902 1743
6c9b78e6
AM
1744 if (hi->root.type == bfd_link_hash_indirect)
1745 flip = hi;
45d6a902
AM
1746 else
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1751 }
1752
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1758
1759 if (! newdyn
1760 && bfd_is_com_section (sec)
1761 && olddyncommon)
1762 {
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1a72702b
AM
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
45d6a902 1768
4cc11e76 1769 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1770 larger, pretend that the new symbol has its size. */
1771
1772 if (h->size > *pvalue)
1773 *pvalue = h->size;
1774
af44c138
L
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1779
1780 olddef = FALSE;
1781 olddyncommon = FALSE;
1782
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1785
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1788
6c9b78e6
AM
1789 if (hi->root.type == bfd_link_hash_indirect)
1790 flip = hi;
45d6a902
AM
1791 else
1792 h->verinfo.vertree = NULL;
1793 }
1794
1795 if (flip != NULL)
1796 {
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
45d6a902 1800 flip->root.type = h->root.type;
00cbee0a 1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1805 if (h->def_dynamic)
45d6a902 1806 {
f5385ebf
AM
1807 h->def_dynamic = 0;
1808 flip->ref_dynamic = 1;
45d6a902
AM
1809 }
1810 }
1811
45d6a902
AM
1812 return TRUE;
1813}
1814
1815/* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
4f3fedcf 1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1818 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1819
28caa186 1820static bfd_boolean
268b6b39
AM
1821_bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1824 const char *name,
1825 Elf_Internal_Sym *sym,
4f3fedcf
AM
1826 asection *sec,
1827 bfd_vma value,
1828 bfd **poldbfd,
e3c9d234 1829 bfd_boolean *dynsym)
45d6a902
AM
1830{
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1833 bfd_boolean skip;
1834 char *shortname;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
9c5bfbb7 1837 const struct elf_backend_data *bed;
45d6a902
AM
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
e3c9d234 1840 bfd_boolean override;
45d6a902
AM
1841 char *p;
1842 size_t len, shortlen;
ffd65175 1843 asection *tmp_sec;
6e33951e 1844 bfd_boolean matched;
45d6a902 1845
422f1182
L
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847 return TRUE;
1848
45d6a902
AM
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
422f1182
L
1854 if (h->versioned == unknown)
1855 {
1856 if (p == NULL)
1857 {
1858 h->versioned = unversioned;
1859 return TRUE;
1860 }
1861 else
1862 {
1863 if (p[1] != ELF_VER_CHR)
1864 {
1865 h->versioned = versioned_hidden;
1866 return TRUE;
1867 }
1868 else
1869 h->versioned = versioned;
1870 }
1871 }
4373f8af
L
1872 else
1873 {
1874 /* PR ld/19073: We may see an unversioned definition after the
1875 default version. */
1876 if (p == NULL)
1877 return TRUE;
1878 }
45d6a902 1879
45d6a902
AM
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1883
1884 shortlen = p - name;
a50b1753 1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1886 if (shortname == NULL)
1887 return FALSE;
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1890
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
6e33951e 1897 matched = TRUE;
ffd65175
AM
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1900 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1901 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1902 return FALSE;
1903
1904 if (skip)
1905 goto nondefault;
1906
5b677558
AM
1907 if (hi->def_regular)
1908 {
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 {
1916 bfd_boolean hide;
1917
1918 hi->verinfo.vertree
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1922 {
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 goto nondefault;
1925 }
1926 }
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 goto nondefault;
1930 }
1931
45d6a902
AM
1932 if (! override)
1933 {
c6e8a9a8 1934 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1935 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1936 {
1937 bh = &hi->root;
fbcc8baf 1938 if (bh->type == bfd_link_hash_defined
6cc71b82 1939 && bh->u.def.section->owner != NULL
fbcc8baf
L
1940 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1941 {
1942 /* Mark the previous definition from IR object as
1943 undefined so that the generic linker will override
1944 it. */
1945 bh->type = bfd_link_hash_undefined;
1946 bh->u.undef.abfd = bh->u.def.section->owner;
1947 }
c6e8a9a8
L
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr,
1951 0, name, FALSE, collect, &bh)))
1952 return FALSE;
1953 hi = (struct elf_link_hash_entry *) bh;
1954 }
45d6a902
AM
1955 }
1956 else
1957 {
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1963
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1967
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1975
1976 while (hi->root.type == bfd_link_hash_indirect
1977 || hi->root.type == bfd_link_hash_warning)
1978 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1979
1980 h->root.type = bfd_link_hash_indirect;
1981 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1982 if (h->def_dynamic)
45d6a902 1983 {
f5385ebf
AM
1984 h->def_dynamic = 0;
1985 hi->ref_dynamic = 1;
1986 if (hi->ref_regular
1987 || hi->def_regular)
45d6a902 1988 {
c152c796 1989 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1990 return FALSE;
1991 }
1992 }
1993
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1996 hi = h;
1997 }
1998
fab4a87f
L
1999 /* Check if HI is a warning symbol. */
2000 if (hi->root.type == bfd_link_hash_warning)
2001 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002
45d6a902
AM
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2006
2007 if (hi->root.type == bfd_link_hash_indirect)
2008 {
2009 struct elf_link_hash_entry *ht;
2010
45d6a902 2011 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2012 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2013
68c88cd4
AM
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018 hi->dynamic_def |= ht->dynamic_def;
2019
45d6a902
AM
2020 /* See if the new flags lead us to realize that the symbol must
2021 be dynamic. */
2022 if (! *dynsym)
2023 {
2024 if (! dynamic)
2025 {
0e1862bb 2026 if (! bfd_link_executable (info)
90c984fc 2027 || hi->def_dynamic
f5385ebf 2028 || hi->ref_dynamic)
45d6a902
AM
2029 *dynsym = TRUE;
2030 }
2031 else
2032 {
f5385ebf 2033 if (hi->ref_regular)
45d6a902
AM
2034 *dynsym = TRUE;
2035 }
2036 }
2037 }
2038
2039 /* We also need to define an indirection from the nondefault version
2040 of the symbol. */
2041
2042nondefault:
2043 len = strlen (name);
a50b1753 2044 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2045 if (shortname == NULL)
2046 return FALSE;
2047 memcpy (shortname, name, shortlen);
2048 memcpy (shortname + shortlen, p + 1, len - shortlen);
2049
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok = FALSE;
2052 size_change_ok = FALSE;
ffd65175
AM
2053 tmp_sec = sec;
2054 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2055 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2056 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2057 return FALSE;
2058
2059 if (skip)
2060 return TRUE;
2061
2062 if (override)
2063 {
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
4cc11e76 2066 overridden by a versioned definition. */
45d6a902
AM
2067 if (hi->root.type != bfd_link_hash_defined
2068 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2069 _bfd_error_handler
695344c0 2070 /* xgettext:c-format */
871b3ab2 2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2072 abfd, shortname);
45d6a902
AM
2073 }
2074 else
2075 {
2076 bh = &hi->root;
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2079 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2080 return FALSE;
2081 hi = (struct elf_link_hash_entry *) bh;
2082
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2086
2087 if (hi->root.type == bfd_link_hash_indirect)
2088 {
fcfa13d2 2089 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2090 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2092
2093 /* See if the new flags lead us to realize that the symbol
2094 must be dynamic. */
2095 if (! *dynsym)
2096 {
2097 if (! dynamic)
2098 {
0e1862bb 2099 if (! bfd_link_executable (info)
f5385ebf 2100 || hi->ref_dynamic)
45d6a902
AM
2101 *dynsym = TRUE;
2102 }
2103 else
2104 {
f5385ebf 2105 if (hi->ref_regular)
45d6a902
AM
2106 *dynsym = TRUE;
2107 }
2108 }
2109 }
2110 }
2111
2112 return TRUE;
2113}
2114\f
2115/* This routine is used to export all defined symbols into the dynamic
2116 symbol table. It is called via elf_link_hash_traverse. */
2117
28caa186 2118static bfd_boolean
268b6b39 2119_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2120{
a50b1753 2121 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2122
2123 /* Ignore indirect symbols. These are added by the versioning code. */
2124 if (h->root.type == bfd_link_hash_indirect)
2125 return TRUE;
2126
7686d77d
AM
2127 /* Ignore this if we won't export it. */
2128 if (!eif->info->export_dynamic && !h->dynamic)
2129 return TRUE;
45d6a902
AM
2130
2131 if (h->dynindx == -1
fd91d419
L
2132 && (h->def_regular || h->ref_regular)
2133 && ! bfd_hide_sym_by_version (eif->info->version_info,
2134 h->root.root.string))
45d6a902 2135 {
fd91d419 2136 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2137 {
fd91d419
L
2138 eif->failed = TRUE;
2139 return FALSE;
45d6a902
AM
2140 }
2141 }
2142
2143 return TRUE;
2144}
2145\f
2146/* Look through the symbols which are defined in other shared
2147 libraries and referenced here. Update the list of version
2148 dependencies. This will be put into the .gnu.version_r section.
2149 This function is called via elf_link_hash_traverse. */
2150
28caa186 2151static bfd_boolean
268b6b39
AM
2152_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2153 void *data)
45d6a902 2154{
a50b1753 2155 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2156 Elf_Internal_Verneed *t;
2157 Elf_Internal_Vernaux *a;
2158 bfd_size_type amt;
2159
45d6a902
AM
2160 /* We only care about symbols defined in shared objects with version
2161 information. */
f5385ebf
AM
2162 if (!h->def_dynamic
2163 || h->def_regular
45d6a902 2164 || h->dynindx == -1
7b20f099
AM
2165 || h->verinfo.verdef == NULL
2166 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2167 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2168 return TRUE;
2169
2170 /* See if we already know about this version. */
28caa186
AM
2171 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2172 t != NULL;
2173 t = t->vn_nextref)
45d6a902
AM
2174 {
2175 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2176 continue;
2177
2178 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2179 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2180 return TRUE;
2181
2182 break;
2183 }
2184
2185 /* This is a new version. Add it to tree we are building. */
2186
2187 if (t == NULL)
2188 {
2189 amt = sizeof *t;
a50b1753 2190 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2191 if (t == NULL)
2192 {
2193 rinfo->failed = TRUE;
2194 return FALSE;
2195 }
2196
2197 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2198 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2199 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2200 }
2201
2202 amt = sizeof *a;
a50b1753 2203 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2204 if (a == NULL)
2205 {
2206 rinfo->failed = TRUE;
2207 return FALSE;
2208 }
45d6a902
AM
2209
2210 /* Note that we are copying a string pointer here, and testing it
2211 above. If bfd_elf_string_from_elf_section is ever changed to
2212 discard the string data when low in memory, this will have to be
2213 fixed. */
2214 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2215
2216 a->vna_flags = h->verinfo.verdef->vd_flags;
2217 a->vna_nextptr = t->vn_auxptr;
2218
2219 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2220 ++rinfo->vers;
2221
2222 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2223
2224 t->vn_auxptr = a;
2225
2226 return TRUE;
2227}
2228
099bb8fb
L
2229/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230 hidden. Set *T_P to NULL if there is no match. */
2231
2232static bfd_boolean
2233_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2234 struct elf_link_hash_entry *h,
2235 const char *version_p,
2236 struct bfd_elf_version_tree **t_p,
2237 bfd_boolean *hide)
2238{
2239 struct bfd_elf_version_tree *t;
2240
2241 /* Look for the version. If we find it, it is no longer weak. */
2242 for (t = info->version_info; t != NULL; t = t->next)
2243 {
2244 if (strcmp (t->name, version_p) == 0)
2245 {
2246 size_t len;
2247 char *alc;
2248 struct bfd_elf_version_expr *d;
2249
2250 len = version_p - h->root.root.string;
2251 alc = (char *) bfd_malloc (len);
2252 if (alc == NULL)
2253 return FALSE;
2254 memcpy (alc, h->root.root.string, len - 1);
2255 alc[len - 1] = '\0';
2256 if (alc[len - 2] == ELF_VER_CHR)
2257 alc[len - 2] = '\0';
2258
2259 h->verinfo.vertree = t;
2260 t->used = TRUE;
2261 d = NULL;
2262
2263 if (t->globals.list != NULL)
2264 d = (*t->match) (&t->globals, NULL, alc);
2265
2266 /* See if there is anything to force this symbol to
2267 local scope. */
2268 if (d == NULL && t->locals.list != NULL)
2269 {
2270 d = (*t->match) (&t->locals, NULL, alc);
2271 if (d != NULL
2272 && h->dynindx != -1
2273 && ! info->export_dynamic)
2274 *hide = TRUE;
2275 }
2276
2277 free (alc);
2278 break;
2279 }
2280 }
2281
2282 *t_p = t;
2283
2284 return TRUE;
2285}
2286
2287/* Return TRUE if the symbol H is hidden by version script. */
2288
2289bfd_boolean
2290_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2291 struct elf_link_hash_entry *h)
2292{
2293 const char *p;
2294 bfd_boolean hide = FALSE;
2295 const struct elf_backend_data *bed
2296 = get_elf_backend_data (info->output_bfd);
2297
2298 /* Version script only hides symbols defined in regular objects. */
2299 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2300 return TRUE;
2301
2302 p = strchr (h->root.root.string, ELF_VER_CHR);
2303 if (p != NULL && h->verinfo.vertree == NULL)
2304 {
2305 struct bfd_elf_version_tree *t;
2306
2307 ++p;
2308 if (*p == ELF_VER_CHR)
2309 ++p;
2310
2311 if (*p != '\0'
2312 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2313 && hide)
2314 {
2315 if (hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2317 return TRUE;
2318 }
2319 }
2320
2321 /* If we don't have a version for this symbol, see if we can find
2322 something. */
2323 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2324 {
2325 h->verinfo.vertree
2326 = bfd_find_version_for_sym (info->version_info,
2327 h->root.root.string, &hide);
2328 if (h->verinfo.vertree != NULL && hide)
2329 {
2330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2331 return TRUE;
2332 }
2333 }
2334
2335 return FALSE;
2336}
2337
45d6a902
AM
2338/* Figure out appropriate versions for all the symbols. We may not
2339 have the version number script until we have read all of the input
2340 files, so until that point we don't know which symbols should be
2341 local. This function is called via elf_link_hash_traverse. */
2342
28caa186 2343static bfd_boolean
268b6b39 2344_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2345{
28caa186 2346 struct elf_info_failed *sinfo;
45d6a902 2347 struct bfd_link_info *info;
9c5bfbb7 2348 const struct elf_backend_data *bed;
45d6a902
AM
2349 struct elf_info_failed eif;
2350 char *p;
099bb8fb 2351 bfd_boolean hide;
45d6a902 2352
a50b1753 2353 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2354 info = sinfo->info;
2355
45d6a902
AM
2356 /* Fix the symbol flags. */
2357 eif.failed = FALSE;
2358 eif.info = info;
2359 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2360 {
2361 if (eif.failed)
2362 sinfo->failed = TRUE;
2363 return FALSE;
2364 }
2365
0a640d71
L
2366 bed = get_elf_backend_data (info->output_bfd);
2367
45d6a902
AM
2368 /* We only need version numbers for symbols defined in regular
2369 objects. */
f5385ebf 2370 if (!h->def_regular)
0a640d71
L
2371 {
2372 /* Hide symbols defined in discarded input sections. */
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && discarded_section (h->root.u.def.section))
2376 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2377 return TRUE;
2378 }
45d6a902 2379
099bb8fb 2380 hide = FALSE;
45d6a902
AM
2381 p = strchr (h->root.root.string, ELF_VER_CHR);
2382 if (p != NULL && h->verinfo.vertree == NULL)
2383 {
2384 struct bfd_elf_version_tree *t;
45d6a902 2385
45d6a902
AM
2386 ++p;
2387 if (*p == ELF_VER_CHR)
6e33951e 2388 ++p;
45d6a902
AM
2389
2390 /* If there is no version string, we can just return out. */
2391 if (*p == '\0')
6e33951e 2392 return TRUE;
45d6a902 2393
099bb8fb 2394 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2395 {
099bb8fb
L
2396 sinfo->failed = TRUE;
2397 return FALSE;
45d6a902
AM
2398 }
2399
099bb8fb
L
2400 if (hide)
2401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2402
45d6a902
AM
2403 /* If we are building an application, we need to create a
2404 version node for this version. */
0e1862bb 2405 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2406 {
2407 struct bfd_elf_version_tree **pp;
2408 int version_index;
2409
2410 /* If we aren't going to export this symbol, we don't need
2411 to worry about it. */
2412 if (h->dynindx == -1)
2413 return TRUE;
2414
ef53be89
AM
2415 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2416 sizeof *t);
45d6a902
AM
2417 if (t == NULL)
2418 {
2419 sinfo->failed = TRUE;
2420 return FALSE;
2421 }
2422
45d6a902 2423 t->name = p;
45d6a902
AM
2424 t->name_indx = (unsigned int) -1;
2425 t->used = TRUE;
2426
2427 version_index = 1;
2428 /* Don't count anonymous version tag. */
fd91d419
L
2429 if (sinfo->info->version_info != NULL
2430 && sinfo->info->version_info->vernum == 0)
45d6a902 2431 version_index = 0;
fd91d419
L
2432 for (pp = &sinfo->info->version_info;
2433 *pp != NULL;
2434 pp = &(*pp)->next)
45d6a902
AM
2435 ++version_index;
2436 t->vernum = version_index;
2437
2438 *pp = t;
2439
2440 h->verinfo.vertree = t;
2441 }
2442 else if (t == NULL)
2443 {
2444 /* We could not find the version for a symbol when
2445 generating a shared archive. Return an error. */
4eca0228 2446 _bfd_error_handler
695344c0 2447 /* xgettext:c-format */
871b3ab2 2448 (_("%pB: version node not found for symbol %s"),
28caa186 2449 info->output_bfd, h->root.root.string);
45d6a902
AM
2450 bfd_set_error (bfd_error_bad_value);
2451 sinfo->failed = TRUE;
2452 return FALSE;
2453 }
45d6a902
AM
2454 }
2455
2456 /* If we don't have a version for this symbol, see if we can find
2457 something. */
099bb8fb
L
2458 if (!hide
2459 && h->verinfo.vertree == NULL
2460 && sinfo->info->version_info != NULL)
45d6a902 2461 {
fd91d419
L
2462 h->verinfo.vertree
2463 = bfd_find_version_for_sym (sinfo->info->version_info,
2464 h->root.root.string, &hide);
1e8fa21e
AM
2465 if (h->verinfo.vertree != NULL && hide)
2466 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2467 }
2468
2469 return TRUE;
2470}
2471\f
45d6a902
AM
2472/* Read and swap the relocs from the section indicated by SHDR. This
2473 may be either a REL or a RELA section. The relocations are
2474 translated into RELA relocations and stored in INTERNAL_RELOCS,
2475 which should have already been allocated to contain enough space.
2476 The EXTERNAL_RELOCS are a buffer where the external form of the
2477 relocations should be stored.
2478
2479 Returns FALSE if something goes wrong. */
2480
2481static bfd_boolean
268b6b39 2482elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2483 asection *sec,
268b6b39
AM
2484 Elf_Internal_Shdr *shdr,
2485 void *external_relocs,
2486 Elf_Internal_Rela *internal_relocs)
45d6a902 2487{
9c5bfbb7 2488 const struct elf_backend_data *bed;
268b6b39 2489 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2490 const bfd_byte *erela;
2491 const bfd_byte *erelaend;
2492 Elf_Internal_Rela *irela;
243ef1e0
L
2493 Elf_Internal_Shdr *symtab_hdr;
2494 size_t nsyms;
45d6a902 2495
45d6a902
AM
2496 /* Position ourselves at the start of the section. */
2497 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2498 return FALSE;
2499
2500 /* Read the relocations. */
2501 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2502 return FALSE;
2503
243ef1e0 2504 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2505 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2506
45d6a902
AM
2507 bed = get_elf_backend_data (abfd);
2508
2509 /* Convert the external relocations to the internal format. */
2510 if (shdr->sh_entsize == bed->s->sizeof_rel)
2511 swap_in = bed->s->swap_reloc_in;
2512 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2513 swap_in = bed->s->swap_reloca_in;
2514 else
2515 {
2516 bfd_set_error (bfd_error_wrong_format);
2517 return FALSE;
2518 }
2519
a50b1753 2520 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2521 /* Setting erelaend like this and comparing with <= handles case of
2522 a fuzzed object with sh_size not a multiple of sh_entsize. */
2523 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2524 irela = internal_relocs;
f55b1e32 2525 while (erela <= erelaend)
45d6a902 2526 {
243ef1e0
L
2527 bfd_vma r_symndx;
2528
45d6a902 2529 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2530 r_symndx = ELF32_R_SYM (irela->r_info);
2531 if (bed->s->arch_size == 64)
2532 r_symndx >>= 24;
ce98a316
NC
2533 if (nsyms > 0)
2534 {
2535 if ((size_t) r_symndx >= nsyms)
2536 {
4eca0228 2537 _bfd_error_handler
695344c0 2538 /* xgettext:c-format */
2dcf00ce
AM
2539 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2540 " for offset %#" PRIx64 " in section `%pA'"),
2541 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2542 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
2546 }
cf35638d 2547 else if (r_symndx != STN_UNDEF)
243ef1e0 2548 {
4eca0228 2549 _bfd_error_handler
695344c0 2550 /* xgettext:c-format */
2dcf00ce
AM
2551 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2552 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2553 " when the object file has no symbol table"),
2dcf00ce
AM
2554 abfd, (uint64_t) r_symndx,
2555 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2556 bfd_set_error (bfd_error_bad_value);
2557 return FALSE;
2558 }
45d6a902
AM
2559 irela += bed->s->int_rels_per_ext_rel;
2560 erela += shdr->sh_entsize;
2561 }
2562
2563 return TRUE;
2564}
2565
2566/* Read and swap the relocs for a section O. They may have been
2567 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568 not NULL, they are used as buffers to read into. They are known to
2569 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2570 the return value is allocated using either malloc or bfd_alloc,
2571 according to the KEEP_MEMORY argument. If O has two relocation
2572 sections (both REL and RELA relocations), then the REL_HDR
2573 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2574 RELA_HDR relocations. */
45d6a902
AM
2575
2576Elf_Internal_Rela *
268b6b39
AM
2577_bfd_elf_link_read_relocs (bfd *abfd,
2578 asection *o,
2579 void *external_relocs,
2580 Elf_Internal_Rela *internal_relocs,
2581 bfd_boolean keep_memory)
45d6a902 2582{
268b6b39 2583 void *alloc1 = NULL;
45d6a902 2584 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2586 struct bfd_elf_section_data *esdo = elf_section_data (o);
2587 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2588
d4730f92
BS
2589 if (esdo->relocs != NULL)
2590 return esdo->relocs;
45d6a902
AM
2591
2592 if (o->reloc_count == 0)
2593 return NULL;
2594
45d6a902
AM
2595 if (internal_relocs == NULL)
2596 {
2597 bfd_size_type size;
2598
056bafd4 2599 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2600 if (keep_memory)
a50b1753 2601 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2602 else
a50b1753 2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2604 if (internal_relocs == NULL)
2605 goto error_return;
2606 }
2607
2608 if (external_relocs == NULL)
2609 {
d4730f92
BS
2610 bfd_size_type size = 0;
2611
2612 if (esdo->rel.hdr)
2613 size += esdo->rel.hdr->sh_size;
2614 if (esdo->rela.hdr)
2615 size += esdo->rela.hdr->sh_size;
45d6a902 2616
268b6b39 2617 alloc1 = bfd_malloc (size);
45d6a902
AM
2618 if (alloc1 == NULL)
2619 goto error_return;
2620 external_relocs = alloc1;
2621 }
2622
d4730f92
BS
2623 internal_rela_relocs = internal_relocs;
2624 if (esdo->rel.hdr)
2625 {
2626 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2627 external_relocs,
2628 internal_relocs))
2629 goto error_return;
2630 external_relocs = (((bfd_byte *) external_relocs)
2631 + esdo->rel.hdr->sh_size);
2632 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2633 * bed->s->int_rels_per_ext_rel);
2634 }
2635
2636 if (esdo->rela.hdr
2637 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2638 external_relocs,
2639 internal_rela_relocs)))
45d6a902
AM
2640 goto error_return;
2641
2642 /* Cache the results for next time, if we can. */
2643 if (keep_memory)
d4730f92 2644 esdo->relocs = internal_relocs;
45d6a902
AM
2645
2646 if (alloc1 != NULL)
2647 free (alloc1);
2648
2649 /* Don't free alloc2, since if it was allocated we are passing it
2650 back (under the name of internal_relocs). */
2651
2652 return internal_relocs;
2653
2654 error_return:
2655 if (alloc1 != NULL)
2656 free (alloc1);
2657 if (alloc2 != NULL)
4dd07732
AM
2658 {
2659 if (keep_memory)
2660 bfd_release (abfd, alloc2);
2661 else
2662 free (alloc2);
2663 }
45d6a902
AM
2664 return NULL;
2665}
2666
2667/* Compute the size of, and allocate space for, REL_HDR which is the
2668 section header for a section containing relocations for O. */
2669
28caa186 2670static bfd_boolean
9eaff861
AO
2671_bfd_elf_link_size_reloc_section (bfd *abfd,
2672 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2673{
9eaff861 2674 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2675
2676 /* That allows us to calculate the size of the section. */
9eaff861 2677 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2678
2679 /* The contents field must last into write_object_contents, so we
2680 allocate it with bfd_alloc rather than malloc. Also since we
2681 cannot be sure that the contents will actually be filled in,
2682 we zero the allocated space. */
a50b1753 2683 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2684 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2685 return FALSE;
2686
d4730f92 2687 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2688 {
2689 struct elf_link_hash_entry **p;
2690
ca4be51c
AM
2691 p = ((struct elf_link_hash_entry **)
2692 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2693 if (p == NULL)
2694 return FALSE;
2695
d4730f92 2696 reldata->hashes = p;
45d6a902
AM
2697 }
2698
2699 return TRUE;
2700}
2701
2702/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703 originated from the section given by INPUT_REL_HDR) to the
2704 OUTPUT_BFD. */
2705
2706bfd_boolean
268b6b39
AM
2707_bfd_elf_link_output_relocs (bfd *output_bfd,
2708 asection *input_section,
2709 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2710 Elf_Internal_Rela *internal_relocs,
2711 struct elf_link_hash_entry **rel_hash
2712 ATTRIBUTE_UNUSED)
45d6a902
AM
2713{
2714 Elf_Internal_Rela *irela;
2715 Elf_Internal_Rela *irelaend;
2716 bfd_byte *erel;
d4730f92 2717 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2718 asection *output_section;
9c5bfbb7 2719 const struct elf_backend_data *bed;
268b6b39 2720 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2721 struct bfd_elf_section_data *esdo;
45d6a902
AM
2722
2723 output_section = input_section->output_section;
45d6a902 2724
d4730f92
BS
2725 bed = get_elf_backend_data (output_bfd);
2726 esdo = elf_section_data (output_section);
2727 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2728 {
d4730f92
BS
2729 output_reldata = &esdo->rel;
2730 swap_out = bed->s->swap_reloc_out;
45d6a902 2731 }
d4730f92
BS
2732 else if (esdo->rela.hdr
2733 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2734 {
d4730f92
BS
2735 output_reldata = &esdo->rela;
2736 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2737 }
2738 else
2739 {
4eca0228 2740 _bfd_error_handler
695344c0 2741 /* xgettext:c-format */
871b3ab2 2742 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2743 output_bfd, input_section->owner, input_section);
297d8443 2744 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2745 return FALSE;
2746 }
2747
d4730f92
BS
2748 erel = output_reldata->hdr->contents;
2749 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2750 irela = internal_relocs;
2751 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2752 * bed->s->int_rels_per_ext_rel);
2753 while (irela < irelaend)
2754 {
2755 (*swap_out) (output_bfd, irela, erel);
2756 irela += bed->s->int_rels_per_ext_rel;
2757 erel += input_rel_hdr->sh_entsize;
2758 }
2759
2760 /* Bump the counter, so that we know where to add the next set of
2761 relocations. */
d4730f92 2762 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2763
2764 return TRUE;
2765}
2766\f
508c3946
L
2767/* Make weak undefined symbols in PIE dynamic. */
2768
2769bfd_boolean
2770_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2771 struct elf_link_hash_entry *h)
2772{
0e1862bb 2773 if (bfd_link_pie (info)
508c3946
L
2774 && h->dynindx == -1
2775 && h->root.type == bfd_link_hash_undefweak)
2776 return bfd_elf_link_record_dynamic_symbol (info, h);
2777
2778 return TRUE;
2779}
2780
45d6a902
AM
2781/* Fix up the flags for a symbol. This handles various cases which
2782 can only be fixed after all the input files are seen. This is
2783 currently called by both adjust_dynamic_symbol and
2784 assign_sym_version, which is unnecessary but perhaps more robust in
2785 the face of future changes. */
2786
28caa186 2787static bfd_boolean
268b6b39
AM
2788_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2789 struct elf_info_failed *eif)
45d6a902 2790{
33774f08 2791 const struct elf_backend_data *bed;
508c3946 2792
45d6a902
AM
2793 /* If this symbol was mentioned in a non-ELF file, try to set
2794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2795 permit a non-ELF file to correctly refer to a symbol defined in
2796 an ELF dynamic object. */
f5385ebf 2797 if (h->non_elf)
45d6a902
AM
2798 {
2799 while (h->root.type == bfd_link_hash_indirect)
2800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2801
2802 if (h->root.type != bfd_link_hash_defined
2803 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2804 {
2805 h->ref_regular = 1;
2806 h->ref_regular_nonweak = 1;
2807 }
45d6a902
AM
2808 else
2809 {
2810 if (h->root.u.def.section->owner != NULL
2811 && (bfd_get_flavour (h->root.u.def.section->owner)
2812 == bfd_target_elf_flavour))
f5385ebf
AM
2813 {
2814 h->ref_regular = 1;
2815 h->ref_regular_nonweak = 1;
2816 }
45d6a902 2817 else
f5385ebf 2818 h->def_regular = 1;
45d6a902
AM
2819 }
2820
2821 if (h->dynindx == -1
f5385ebf
AM
2822 && (h->def_dynamic
2823 || h->ref_dynamic))
45d6a902 2824 {
c152c796 2825 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2826 {
2827 eif->failed = TRUE;
2828 return FALSE;
2829 }
2830 }
2831 }
2832 else
2833 {
f5385ebf 2834 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2835 was first seen in a non-ELF file. Fortunately, if the symbol
2836 was first seen in an ELF file, we're probably OK unless the
2837 symbol was defined in a non-ELF file. Catch that case here.
2838 FIXME: We're still in trouble if the symbol was first seen in
2839 a dynamic object, and then later in a non-ELF regular object. */
2840 if ((h->root.type == bfd_link_hash_defined
2841 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2842 && !h->def_regular
45d6a902
AM
2843 && (h->root.u.def.section->owner != NULL
2844 ? (bfd_get_flavour (h->root.u.def.section->owner)
2845 != bfd_target_elf_flavour)
2846 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2847 && !h->def_dynamic)))
2848 h->def_regular = 1;
45d6a902
AM
2849 }
2850
508c3946 2851 /* Backend specific symbol fixup. */
33774f08
AM
2852 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2853 if (bed->elf_backend_fixup_symbol
2854 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2855 return FALSE;
508c3946 2856
45d6a902
AM
2857 /* If this is a final link, and the symbol was defined as a common
2858 symbol in a regular object file, and there was no definition in
2859 any dynamic object, then the linker will have allocated space for
f5385ebf 2860 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2861 flag will not have been set. */
2862 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2863 && !h->def_regular
2864 && h->ref_regular
2865 && !h->def_dynamic
96f29d96 2866 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2867 h->def_regular = 1;
45d6a902 2868
af0bfb9c
AM
2869 /* Symbols defined in discarded sections shouldn't be dynamic. */
2870 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
4deb8f71
L
2873 /* If a weak undefined symbol has non-default visibility, we also
2874 hide it from the dynamic linker. */
af0bfb9c
AM
2875 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2876 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2877 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2878
2879 /* A hidden versioned symbol in executable should be forced local if
2880 it is is locally defined, not referenced by shared library and not
2881 exported. */
2882 else if (bfd_link_executable (eif->info)
2883 && h->versioned == versioned_hidden
2884 && !eif->info->export_dynamic
2885 && !h->dynamic
2886 && !h->ref_dynamic
2887 && h->def_regular)
2888 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2889
45d6a902
AM
2890 /* If -Bsymbolic was used (which means to bind references to global
2891 symbols to the definition within the shared object), and this
2892 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2893 need a PLT entry. Likewise, if the symbol has non-default
2894 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2895 will force it local. */
4deb8f71
L
2896 else if (h->needs_plt
2897 && bfd_link_pic (eif->info)
2898 && is_elf_hash_table (eif->info->hash)
2899 && (SYMBOLIC_BIND (eif->info, h)
2900 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2901 && h->def_regular)
45d6a902 2902 {
45d6a902
AM
2903 bfd_boolean force_local;
2904
45d6a902
AM
2905 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2906 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2907 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2908 }
2909
45d6a902
AM
2910 /* If this is a weak defined symbol in a dynamic object, and we know
2911 the real definition in the dynamic object, copy interesting flags
2912 over to the real definition. */
60d67dc8 2913 if (h->is_weakalias)
45d6a902 2914 {
60d67dc8
AM
2915 struct elf_link_hash_entry *def = weakdef (h);
2916
45d6a902
AM
2917 /* If the real definition is defined by a regular object file,
2918 don't do anything special. See the longer description in
5b9d7a9a
AM
2919 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2920 bfd_link_hash_defined as it was when put on the alias list
2921 then it must have originally been a versioned symbol (for
2922 which a non-versioned indirect symbol is created) and later
2923 a definition for the non-versioned symbol is found. In that
2924 case the indirection is flipped with the versioned symbol
2925 becoming an indirect pointing at the non-versioned symbol.
2926 Thus, not an alias any more. */
2927 if (def->def_regular
2928 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2929 {
2930 h = def;
2931 while ((h = h->u.alias) != def)
2932 h->is_weakalias = 0;
2933 }
45d6a902 2934 else
a26587ba 2935 {
4e6b54a6
AM
2936 while (h->root.type == bfd_link_hash_indirect)
2937 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2938 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2940 BFD_ASSERT (def->def_dynamic);
60d67dc8 2941 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2942 }
45d6a902
AM
2943 }
2944
2945 return TRUE;
2946}
2947
2948/* Make the backend pick a good value for a dynamic symbol. This is
2949 called via elf_link_hash_traverse, and also calls itself
2950 recursively. */
2951
28caa186 2952static bfd_boolean
268b6b39 2953_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2954{
a50b1753 2955 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2956 struct elf_link_hash_table *htab;
9c5bfbb7 2957 const struct elf_backend_data *bed;
45d6a902 2958
0eddce27 2959 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2960 return FALSE;
2961
45d6a902
AM
2962 /* Ignore indirect symbols. These are added by the versioning code. */
2963 if (h->root.type == bfd_link_hash_indirect)
2964 return TRUE;
2965
2966 /* Fix the symbol flags. */
2967 if (! _bfd_elf_fix_symbol_flags (h, eif))
2968 return FALSE;
2969
559192d8
AM
2970 htab = elf_hash_table (eif->info);
2971 bed = get_elf_backend_data (htab->dynobj);
2972
954b63d4
AM
2973 if (h->root.type == bfd_link_hash_undefweak)
2974 {
2975 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2976 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2977 else if (eif->info->dynamic_undefined_weak > 0
2978 && h->ref_regular
2979 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2980 && !bfd_hide_sym_by_version (eif->info->version_info,
2981 h->root.root.string))
2982 {
2983 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2984 {
2985 eif->failed = TRUE;
2986 return FALSE;
2987 }
2988 }
2989 }
2990
45d6a902
AM
2991 /* If this symbol does not require a PLT entry, and it is not
2992 defined by a dynamic object, or is not referenced by a regular
2993 object, ignore it. We do have to handle a weak defined symbol,
2994 even if no regular object refers to it, if we decided to add it
2995 to the dynamic symbol table. FIXME: Do we normally need to worry
2996 about symbols which are defined by one dynamic object and
2997 referenced by another one? */
f5385ebf 2998 if (!h->needs_plt
91e21fb7 2999 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3000 && (h->def_regular
3001 || !h->def_dynamic
3002 || (!h->ref_regular
60d67dc8 3003 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3004 {
a6aa5195 3005 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3006 return TRUE;
3007 }
3008
3009 /* If we've already adjusted this symbol, don't do it again. This
3010 can happen via a recursive call. */
f5385ebf 3011 if (h->dynamic_adjusted)
45d6a902
AM
3012 return TRUE;
3013
3014 /* Don't look at this symbol again. Note that we must set this
3015 after checking the above conditions, because we may look at a
3016 symbol once, decide not to do anything, and then get called
3017 recursively later after REF_REGULAR is set below. */
f5385ebf 3018 h->dynamic_adjusted = 1;
45d6a902
AM
3019
3020 /* If this is a weak definition, and we know a real definition, and
3021 the real symbol is not itself defined by a regular object file,
3022 then get a good value for the real definition. We handle the
3023 real symbol first, for the convenience of the backend routine.
3024
3025 Note that there is a confusing case here. If the real definition
3026 is defined by a regular object file, we don't get the real symbol
3027 from the dynamic object, but we do get the weak symbol. If the
3028 processor backend uses a COPY reloc, then if some routine in the
3029 dynamic object changes the real symbol, we will not see that
3030 change in the corresponding weak symbol. This is the way other
3031 ELF linkers work as well, and seems to be a result of the shared
3032 library model.
3033
3034 I will clarify this issue. Most SVR4 shared libraries define the
3035 variable _timezone and define timezone as a weak synonym. The
3036 tzset call changes _timezone. If you write
3037 extern int timezone;
3038 int _timezone = 5;
3039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3040 you might expect that, since timezone is a synonym for _timezone,
3041 the same number will print both times. However, if the processor
3042 backend uses a COPY reloc, then actually timezone will be copied
3043 into your process image, and, since you define _timezone
3044 yourself, _timezone will not. Thus timezone and _timezone will
3045 wind up at different memory locations. The tzset call will set
3046 _timezone, leaving timezone unchanged. */
3047
60d67dc8 3048 if (h->is_weakalias)
45d6a902 3049 {
60d67dc8
AM
3050 struct elf_link_hash_entry *def = weakdef (h);
3051
ec24dc88 3052 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3053 the alias by a regular object file via the weak symbol H. */
3054 def->ref_regular = 1;
45d6a902 3055
ec24dc88 3056 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3057 the strong alias before H by recursively calling ourselves. */
3058 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3059 return FALSE;
3060 }
3061
3062 /* If a symbol has no type and no size and does not require a PLT
3063 entry, then we are probably about to do the wrong thing here: we
3064 are probably going to create a COPY reloc for an empty object.
3065 This case can arise when a shared object is built with assembly
3066 code, and the assembly code fails to set the symbol type. */
3067 if (h->size == 0
3068 && h->type == STT_NOTYPE
f5385ebf 3069 && !h->needs_plt)
4eca0228 3070 _bfd_error_handler
45d6a902
AM
3071 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3072 h->root.root.string);
3073
45d6a902
AM
3074 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3075 {
3076 eif->failed = TRUE;
3077 return FALSE;
3078 }
3079
3080 return TRUE;
3081}
3082
027297b7
L
3083/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3084 DYNBSS. */
3085
3086bfd_boolean
6cabe1ea
AM
3087_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3088 struct elf_link_hash_entry *h,
027297b7
L
3089 asection *dynbss)
3090{
91ac5911 3091 unsigned int power_of_two;
027297b7
L
3092 bfd_vma mask;
3093 asection *sec = h->root.u.def.section;
3094
de194d85 3095 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3096 requirement of symbols defined in the section. Since we don't
3097 know the symbol alignment requirement, we start with the
3098 maximum alignment and check low bits of the symbol address
3099 for the minimum alignment. */
fd361982 3100 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3101 mask = ((bfd_vma) 1 << power_of_two) - 1;
3102 while ((h->root.u.def.value & mask) != 0)
3103 {
3104 mask >>= 1;
3105 --power_of_two;
3106 }
027297b7 3107
fd361982 3108 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3109 {
3110 /* Adjust the section alignment if needed. */
fd361982 3111 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3112 return FALSE;
3113 }
3114
91ac5911 3115 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3117
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3121
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3124
f7483970
L
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
889c2a67
L
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3130 info->callbacks->einfo
c1c8c1ef 3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3132 h->root.root.string);
6cabe1ea 3133
027297b7
L
3134 return TRUE;
3135}
3136
45d6a902
AM
3137/* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3139
28caa186 3140static bfd_boolean
268b6b39 3141_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3142{
3143 asection *sec;
3144
45d6a902
AM
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3149 {
a50b1753 3150 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3151
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
753731ee 3156 h->root.u.def.value);
45d6a902
AM
3157 }
3158
3159 return TRUE;
3160}
986a241f
RH
3161
3162/* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3165
3166bfd_boolean
268b6b39
AM
3167_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
89a2ee5a 3169 bfd_boolean not_local_protected)
986a241f
RH
3170{
3171 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
986a241f
RH
3174
3175 if (h == NULL)
3176 return FALSE;
3177
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3181
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3184 return FALSE;
f5385ebf 3185 if (h->forced_local)
986a241f
RH
3186 return FALSE;
3187
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
0e1862bb
L
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
986a241f
RH
3192
3193 switch (ELF_ST_VISIBILITY (h->other))
3194 {
3195 case STV_INTERNAL:
3196 case STV_HIDDEN:
3197 return FALSE;
3198
3199 case STV_PROTECTED:
fcb93ecf
PB
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3202 return FALSE;
3203
3204 bed = get_elf_backend_data (hash_table->dynobj);
3205
986a241f
RH
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
89a2ee5a 3209 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3210 binding_stays_local_p = TRUE;
3211 break;
3212
3213 default:
986a241f
RH
3214 break;
3215 }
3216
aa37626c 3217 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3219 return TRUE;
3220
986a241f
RH
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3224}
f6c52c13
AM
3225
3226/* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3229 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3233 defined symbols.
89a2ee5a
AM
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3238
3239bfd_boolean
268b6b39
AM
3240_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
f6c52c13 3243{
fcb93ecf
PB
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3246
f6c52c13
AM
3247 /* If it's a local sym, of course we resolve locally. */
3248 if (h == NULL)
3249 return TRUE;
3250
d95edcac
L
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3254 return TRUE;
3255
0fad2956
MR
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3258 return TRUE;
3259
7e2294f9
AO
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3263 /* Do nothing. */;
f6c52c13 3264 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
f6c52c13
AM
3267 return FALSE;
3268
0fad2956 3269 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3270 if (h->dynindx == -1)
3271 return TRUE;
3272
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
0e1862bb 3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3277 return TRUE;
3278
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3282 return FALSE;
3283
fcb93ecf
PB
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3286 return TRUE;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3289
f7483970
L
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
889c2a67
L
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
1c16dfa5
L
3296 return TRUE;
3297
f6c52c13 3298 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3303 return local_protected;
3304}
e1918d23
AM
3305
3306/* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3308
3309struct bfd_section *
3310_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3311{
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3314
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3317 break;
3318 tls = sec;
3319
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3323
3324 elf_hash_table (info)->tls_sec = tls;
3325
3326 /* Ensure the alignment of the first section is the largest alignment,
3327 so that the tls segment starts aligned. */
3328 if (tls != NULL)
3329 tls->alignment_power = align;
3330
3331 return tls;
3332}
0ad989f9
L
3333
3334/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3335static bfd_boolean
3336is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3338{
a4d8e49b
L
3339 const struct elf_backend_data *bed;
3340
0ad989f9
L
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3344 return FALSE;
3345
fcb93ecf 3346 bed = get_elf_backend_data (abfd);
0ad989f9 3347 /* Function symbols do not count. */
fcb93ecf 3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3349 return FALSE;
3350
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3353 return FALSE;
3354
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
a4d8e49b 3357 if (bed->common_definition (sym))
0ad989f9
L
3358 return FALSE;
3359
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3364
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3366
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3370 return FALSE;
3371
3372 return TRUE;
3373}
3374
3375/* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3378static bfd_boolean
3379elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3380{
3381 Elf_Internal_Shdr * hdr;
ef53be89
AM
3382 size_t symcount;
3383 size_t extsymcount;
3384 size_t extsymoff;
0ad989f9
L
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3388 bfd_boolean result;
3389
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3391 if (abfd == NULL)
3392 return FALSE;
3393
3394 if (! bfd_check_format (abfd, bfd_object))
3395 return FALSE;
3396
7dc3990e
L
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3401#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3404#endif
3405 )
3406 {
3407 /* Use the IR symbol table if the object has been claimed by
3408 plugin. */
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3411 }
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3414 else
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3416
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3418
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3422 {
3423 extsymcount = symcount;
3424 extsymoff = 0;
3425 }
3426 else
3427 {
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3430 }
3431
3432 if (extsymcount == 0)
3433 return FALSE;
3434
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3437 NULL, NULL, NULL);
3438 if (isymbuf == NULL)
3439 return FALSE;
3440
3441 /* Scan the symbol table looking for SYMDEF. */
3442 result = FALSE;
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3444 {
3445 const char *name;
3446
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3448 isym->st_name);
3449 if (name == NULL)
3450 break;
3451
3452 if (strcmp (name, symdef->name) == 0)
3453 {
3454 result = is_global_data_symbol_definition (abfd, isym);
3455 break;
3456 }
3457 }
3458
3459 free (isymbuf);
3460
3461 return result;
3462}
3463\f
5a580b3a
AM
3464/* Add an entry to the .dynamic table. */
3465
3466bfd_boolean
3467_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3468 bfd_vma tag,
3469 bfd_vma val)
3470{
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3473 asection *s;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3477
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3480 return FALSE;
3481
7f923b7f
AM
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3484
5a580b3a 3485 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3487 BFD_ASSERT (s != NULL);
3488
eea6121a 3489 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3491 if (newcontents == NULL)
3492 return FALSE;
3493
3494 dyn.d_tag = tag;
3495 dyn.d_un.d_val = val;
eea6121a 3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3497
eea6121a 3498 s->size = newsize;
5a580b3a
AM
3499 s->contents = newcontents;
3500
3501 return TRUE;
3502}
3503
3504/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3505 otherwise just check whether one already exists. Returns -1 on error,
3506 1 if a DT_NEEDED tag already exists, and 0 on success. */
3507
4ad4eba5 3508static int
7e9f0867
AM
3509elf_add_dt_needed_tag (bfd *abfd,
3510 struct bfd_link_info *info,
4ad4eba5
AM
3511 const char *soname,
3512 bfd_boolean do_it)
5a580b3a
AM
3513{
3514 struct elf_link_hash_table *hash_table;
ef53be89 3515 size_t strindex;
5a580b3a 3516
7e9f0867
AM
3517 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3518 return -1;
3519
5a580b3a 3520 hash_table = elf_hash_table (info);
5a580b3a 3521 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3522 if (strindex == (size_t) -1)
5a580b3a
AM
3523 return -1;
3524
02be4619 3525 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3526 {
3527 asection *sdyn;
3528 const struct elf_backend_data *bed;
3529 bfd_byte *extdyn;
3530
3531 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3532 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3533 if (sdyn != NULL)
3534 for (extdyn = sdyn->contents;
3535 extdyn < sdyn->contents + sdyn->size;
3536 extdyn += bed->s->sizeof_dyn)
3537 {
3538 Elf_Internal_Dyn dyn;
5a580b3a 3539
7e9f0867
AM
3540 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3541 if (dyn.d_tag == DT_NEEDED
3542 && dyn.d_un.d_val == strindex)
3543 {
3544 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3545 return 1;
3546 }
3547 }
5a580b3a
AM
3548 }
3549
3550 if (do_it)
3551 {
7e9f0867
AM
3552 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3553 return -1;
3554
5a580b3a
AM
3555 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3556 return -1;
3557 }
3558 else
3559 /* We were just checking for existence of the tag. */
3560 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3561
3562 return 0;
3563}
3564
7b15fa7a
AM
3565/* Return true if SONAME is on the needed list between NEEDED and STOP
3566 (or the end of list if STOP is NULL), and needed by a library that
3567 will be loaded. */
3568
010e5ae2 3569static bfd_boolean
7b15fa7a
AM
3570on_needed_list (const char *soname,
3571 struct bfd_link_needed_list *needed,
3572 struct bfd_link_needed_list *stop)
010e5ae2 3573{
7b15fa7a
AM
3574 struct bfd_link_needed_list *look;
3575 for (look = needed; look != stop; look = look->next)
3576 if (strcmp (soname, look->name) == 0
3577 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3578 /* If needed by a library that itself is not directly
3579 needed, recursively check whether that library is
3580 indirectly needed. Since we add DT_NEEDED entries to
3581 the end of the list, library dependencies appear after
3582 the library. Therefore search prior to the current
3583 LOOK, preventing possible infinite recursion. */
3584 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3585 return TRUE;
3586
3587 return FALSE;
3588}
3589
14160578 3590/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3591static int
3592elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3593{
3594 const struct elf_link_hash_entry *h1;
3595 const struct elf_link_hash_entry *h2;
10b7e05b 3596 bfd_signed_vma vdiff;
5a580b3a
AM
3597
3598 h1 = *(const struct elf_link_hash_entry **) arg1;
3599 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3600 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3601 if (vdiff != 0)
3602 return vdiff > 0 ? 1 : -1;
3603 else
3604 {
d3435ae8 3605 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3606 if (sdiff != 0)
3607 return sdiff > 0 ? 1 : -1;
3608 }
14160578
AM
3609 vdiff = h1->size - h2->size;
3610 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3611}
4ad4eba5 3612
5a580b3a
AM
3613/* This function is used to adjust offsets into .dynstr for
3614 dynamic symbols. This is called via elf_link_hash_traverse. */
3615
3616static bfd_boolean
3617elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3618{
a50b1753 3619 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3620
5a580b3a
AM
3621 if (h->dynindx != -1)
3622 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3623 return TRUE;
3624}
3625
3626/* Assign string offsets in .dynstr, update all structures referencing
3627 them. */
3628
4ad4eba5
AM
3629static bfd_boolean
3630elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3631{
3632 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3633 struct elf_link_local_dynamic_entry *entry;
3634 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3635 bfd *dynobj = hash_table->dynobj;
3636 asection *sdyn;
3637 bfd_size_type size;
3638 const struct elf_backend_data *bed;
3639 bfd_byte *extdyn;
3640
3641 _bfd_elf_strtab_finalize (dynstr);
3642 size = _bfd_elf_strtab_size (dynstr);
3643
3644 bed = get_elf_backend_data (dynobj);
3d4d4302 3645 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3646 BFD_ASSERT (sdyn != NULL);
3647
3648 /* Update all .dynamic entries referencing .dynstr strings. */
3649 for (extdyn = sdyn->contents;
eea6121a 3650 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3651 extdyn += bed->s->sizeof_dyn)
3652 {
3653 Elf_Internal_Dyn dyn;
3654
3655 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3656 switch (dyn.d_tag)
3657 {
3658 case DT_STRSZ:
3659 dyn.d_un.d_val = size;
3660 break;
3661 case DT_NEEDED:
3662 case DT_SONAME:
3663 case DT_RPATH:
3664 case DT_RUNPATH:
3665 case DT_FILTER:
3666 case DT_AUXILIARY:
7ee314fa
AM
3667 case DT_AUDIT:
3668 case DT_DEPAUDIT:
5a580b3a
AM
3669 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3670 break;
3671 default:
3672 continue;
3673 }
3674 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3675 }
3676
3677 /* Now update local dynamic symbols. */
3678 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3679 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3680 entry->isym.st_name);
3681
3682 /* And the rest of dynamic symbols. */
3683 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3684
3685 /* Adjust version definitions. */
3686 if (elf_tdata (output_bfd)->cverdefs)
3687 {
3688 asection *s;
3689 bfd_byte *p;
ef53be89 3690 size_t i;
5a580b3a
AM
3691 Elf_Internal_Verdef def;
3692 Elf_Internal_Verdaux defaux;
3693
3d4d4302 3694 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3695 p = s->contents;
3696 do
3697 {
3698 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3699 &def);
3700 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3701 if (def.vd_aux != sizeof (Elf_External_Verdef))
3702 continue;
5a580b3a
AM
3703 for (i = 0; i < def.vd_cnt; ++i)
3704 {
3705 _bfd_elf_swap_verdaux_in (output_bfd,
3706 (Elf_External_Verdaux *) p, &defaux);
3707 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3708 defaux.vda_name);
3709 _bfd_elf_swap_verdaux_out (output_bfd,
3710 &defaux, (Elf_External_Verdaux *) p);
3711 p += sizeof (Elf_External_Verdaux);
3712 }
3713 }
3714 while (def.vd_next);
3715 }
3716
3717 /* Adjust version references. */
3718 if (elf_tdata (output_bfd)->verref)
3719 {
3720 asection *s;
3721 bfd_byte *p;
ef53be89 3722 size_t i;
5a580b3a
AM
3723 Elf_Internal_Verneed need;
3724 Elf_Internal_Vernaux needaux;
3725
3d4d4302 3726 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3727 p = s->contents;
3728 do
3729 {
3730 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3731 &need);
3732 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3733 _bfd_elf_swap_verneed_out (output_bfd, &need,
3734 (Elf_External_Verneed *) p);
3735 p += sizeof (Elf_External_Verneed);
3736 for (i = 0; i < need.vn_cnt; ++i)
3737 {
3738 _bfd_elf_swap_vernaux_in (output_bfd,
3739 (Elf_External_Vernaux *) p, &needaux);
3740 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3741 needaux.vna_name);
3742 _bfd_elf_swap_vernaux_out (output_bfd,
3743 &needaux,
3744 (Elf_External_Vernaux *) p);
3745 p += sizeof (Elf_External_Vernaux);
3746 }
3747 }
3748 while (need.vn_next);
3749 }
3750
3751 return TRUE;
3752}
3753\f
13285a1b
AM
3754/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3755 The default is to only match when the INPUT and OUTPUT are exactly
3756 the same target. */
3757
3758bfd_boolean
3759_bfd_elf_default_relocs_compatible (const bfd_target *input,
3760 const bfd_target *output)
3761{
3762 return input == output;
3763}
3764
3765/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3766 This version is used when different targets for the same architecture
3767 are virtually identical. */
3768
3769bfd_boolean
3770_bfd_elf_relocs_compatible (const bfd_target *input,
3771 const bfd_target *output)
3772{
3773 const struct elf_backend_data *obed, *ibed;
3774
3775 if (input == output)
3776 return TRUE;
3777
3778 ibed = xvec_get_elf_backend_data (input);
3779 obed = xvec_get_elf_backend_data (output);
3780
3781 if (ibed->arch != obed->arch)
3782 return FALSE;
3783
3784 /* If both backends are using this function, deem them compatible. */
3785 return ibed->relocs_compatible == obed->relocs_compatible;
3786}
3787
e5034e59
AM
3788/* Make a special call to the linker "notice" function to tell it that
3789 we are about to handle an as-needed lib, or have finished
1b786873 3790 processing the lib. */
e5034e59
AM
3791
3792bfd_boolean
3793_bfd_elf_notice_as_needed (bfd *ibfd,
3794 struct bfd_link_info *info,
3795 enum notice_asneeded_action act)
3796{
46135103 3797 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3798}
3799
d9689752
L
3800/* Check relocations an ELF object file. */
3801
3802bfd_boolean
3803_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3804{
3805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3806 struct elf_link_hash_table *htab = elf_hash_table (info);
3807
3808 /* If this object is the same format as the output object, and it is
3809 not a shared library, then let the backend look through the
3810 relocs.
3811
3812 This is required to build global offset table entries and to
3813 arrange for dynamic relocs. It is not required for the
3814 particular common case of linking non PIC code, even when linking
3815 against shared libraries, but unfortunately there is no way of
3816 knowing whether an object file has been compiled PIC or not.
3817 Looking through the relocs is not particularly time consuming.
3818 The problem is that we must either (1) keep the relocs in memory,
3819 which causes the linker to require additional runtime memory or
3820 (2) read the relocs twice from the input file, which wastes time.
3821 This would be a good case for using mmap.
3822
3823 I have no idea how to handle linking PIC code into a file of a
3824 different format. It probably can't be done. */
3825 if ((abfd->flags & DYNAMIC) == 0
3826 && is_elf_hash_table (htab)
3827 && bed->check_relocs != NULL
3828 && elf_object_id (abfd) == elf_hash_table_id (htab)
3829 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3830 {
3831 asection *o;
3832
3833 for (o = abfd->sections; o != NULL; o = o->next)
3834 {
3835 Elf_Internal_Rela *internal_relocs;
3836 bfd_boolean ok;
3837
5ce03cea 3838 /* Don't check relocations in excluded sections. */
d9689752 3839 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3840 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3841 || o->reloc_count == 0
3842 || ((info->strip == strip_all || info->strip == strip_debugger)
3843 && (o->flags & SEC_DEBUGGING) != 0)
3844 || bfd_is_abs_section (o->output_section))
3845 continue;
3846
3847 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3848 info->keep_memory);
3849 if (internal_relocs == NULL)
3850 return FALSE;
3851
3852 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3853
3854 if (elf_section_data (o)->relocs != internal_relocs)
3855 free (internal_relocs);
3856
3857 if (! ok)
3858 return FALSE;
3859 }
3860 }
3861
3862 return TRUE;
3863}
3864
4ad4eba5
AM
3865/* Add symbols from an ELF object file to the linker hash table. */
3866
3867static bfd_boolean
3868elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3869{
a0c402a5 3870 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3871 Elf_Internal_Shdr *hdr;
ef53be89
AM
3872 size_t symcount;
3873 size_t extsymcount;
3874 size_t extsymoff;
4ad4eba5
AM
3875 struct elf_link_hash_entry **sym_hash;
3876 bfd_boolean dynamic;
3877 Elf_External_Versym *extversym = NULL;
be22c732 3878 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
3879 Elf_External_Versym *ever;
3880 struct elf_link_hash_entry *weaks;
3881 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3882 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3883 Elf_Internal_Sym *isymbuf = NULL;
3884 Elf_Internal_Sym *isym;
3885 Elf_Internal_Sym *isymend;
3886 const struct elf_backend_data *bed;
3887 bfd_boolean add_needed;
66eb6687 3888 struct elf_link_hash_table *htab;
4ad4eba5 3889 bfd_size_type amt;
66eb6687 3890 void *alloc_mark = NULL;
4f87808c
AM
3891 struct bfd_hash_entry **old_table = NULL;
3892 unsigned int old_size = 0;
3893 unsigned int old_count = 0;
66eb6687 3894 void *old_tab = NULL;
66eb6687
AM
3895 void *old_ent;
3896 struct bfd_link_hash_entry *old_undefs = NULL;
3897 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3898 void *old_strtab = NULL;
66eb6687 3899 size_t tabsize = 0;
db6a5d5f 3900 asection *s;
29a9f53e 3901 bfd_boolean just_syms;
4ad4eba5 3902
66eb6687 3903 htab = elf_hash_table (info);
4ad4eba5 3904 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3905
3906 if ((abfd->flags & DYNAMIC) == 0)
3907 dynamic = FALSE;
3908 else
3909 {
3910 dynamic = TRUE;
3911
3912 /* You can't use -r against a dynamic object. Also, there's no
3913 hope of using a dynamic object which does not exactly match
3914 the format of the output file. */
0e1862bb 3915 if (bfd_link_relocatable (info)
66eb6687 3916 || !is_elf_hash_table (htab)
f13a99db 3917 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3918 {
0e1862bb 3919 if (bfd_link_relocatable (info))
9a0789ec
NC
3920 bfd_set_error (bfd_error_invalid_operation);
3921 else
3922 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3923 goto error_return;
3924 }
3925 }
3926
a0c402a5
L
3927 ehdr = elf_elfheader (abfd);
3928 if (info->warn_alternate_em
3929 && bed->elf_machine_code != ehdr->e_machine
3930 && ((bed->elf_machine_alt1 != 0
3931 && ehdr->e_machine == bed->elf_machine_alt1)
3932 || (bed->elf_machine_alt2 != 0
3933 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3934 _bfd_error_handler
695344c0 3935 /* xgettext:c-format */
9793eb77 3936 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3937 ehdr->e_machine, abfd, bed->elf_machine_code);
3938
4ad4eba5
AM
3939 /* As a GNU extension, any input sections which are named
3940 .gnu.warning.SYMBOL are treated as warning symbols for the given
3941 symbol. This differs from .gnu.warning sections, which generate
3942 warnings when they are included in an output file. */
dd98f8d2 3943 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3944 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3945 {
db6a5d5f 3946 const char *name;
4ad4eba5 3947
fd361982 3948 name = bfd_section_name (s);
db6a5d5f 3949 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3950 {
db6a5d5f
AM
3951 char *msg;
3952 bfd_size_type sz;
3953
3954 name += sizeof ".gnu.warning." - 1;
3955
3956 /* If this is a shared object, then look up the symbol
3957 in the hash table. If it is there, and it is already
3958 been defined, then we will not be using the entry
3959 from this shared object, so we don't need to warn.
3960 FIXME: If we see the definition in a regular object
3961 later on, we will warn, but we shouldn't. The only
3962 fix is to keep track of what warnings we are supposed
3963 to emit, and then handle them all at the end of the
3964 link. */
3965 if (dynamic)
4ad4eba5 3966 {
db6a5d5f
AM
3967 struct elf_link_hash_entry *h;
3968
3969 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3970
3971 /* FIXME: What about bfd_link_hash_common? */
3972 if (h != NULL
3973 && (h->root.type == bfd_link_hash_defined
3974 || h->root.type == bfd_link_hash_defweak))
3975 continue;
3976 }
4ad4eba5 3977
db6a5d5f
AM
3978 sz = s->size;
3979 msg = (char *) bfd_alloc (abfd, sz + 1);
3980 if (msg == NULL)
3981 goto error_return;
4ad4eba5 3982
db6a5d5f
AM
3983 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3984 goto error_return;
4ad4eba5 3985
db6a5d5f 3986 msg[sz] = '\0';
4ad4eba5 3987
db6a5d5f
AM
3988 if (! (_bfd_generic_link_add_one_symbol
3989 (info, abfd, name, BSF_WARNING, s, 0, msg,
3990 FALSE, bed->collect, NULL)))
3991 goto error_return;
4ad4eba5 3992
0e1862bb 3993 if (bfd_link_executable (info))
db6a5d5f
AM
3994 {
3995 /* Clobber the section size so that the warning does
3996 not get copied into the output file. */
3997 s->size = 0;
11d2f718 3998
db6a5d5f
AM
3999 /* Also set SEC_EXCLUDE, so that symbols defined in
4000 the warning section don't get copied to the output. */
4001 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4002 }
4003 }
4004 }
4005
29a9f53e
L
4006 just_syms = ((s = abfd->sections) != NULL
4007 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4008
4ad4eba5
AM
4009 add_needed = TRUE;
4010 if (! dynamic)
4011 {
4012 /* If we are creating a shared library, create all the dynamic
4013 sections immediately. We need to attach them to something,
4014 so we attach them to this BFD, provided it is the right
bf89386a
L
4015 format and is not from ld --just-symbols. Always create the
4016 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4017 are no input BFD's of the same format as the output, we can't
4018 make a shared library. */
4019 if (!just_syms
bf89386a 4020 && (bfd_link_pic (info)
9c1d7a08 4021 || (!bfd_link_relocatable (info)
3c5fce9b 4022 && info->nointerp
9c1d7a08 4023 && (info->export_dynamic || info->dynamic)))
66eb6687 4024 && is_elf_hash_table (htab)
f13a99db 4025 && info->output_bfd->xvec == abfd->xvec
66eb6687 4026 && !htab->dynamic_sections_created)
4ad4eba5
AM
4027 {
4028 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4029 goto error_return;
4030 }
4031 }
66eb6687 4032 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4033 goto error_return;
4034 else
4035 {
4ad4eba5 4036 const char *soname = NULL;
7ee314fa 4037 char *audit = NULL;
4ad4eba5 4038 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4039 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4040 int ret;
4041
4042 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4043 ld shouldn't allow it. */
29a9f53e 4044 if (just_syms)
92fd189d 4045 abort ();
4ad4eba5
AM
4046
4047 /* If this dynamic lib was specified on the command line with
4048 --as-needed in effect, then we don't want to add a DT_NEEDED
4049 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4050 in by another lib's DT_NEEDED. When --no-add-needed is used
4051 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4052 any dynamic library in DT_NEEDED tags in the dynamic lib at
4053 all. */
4054 add_needed = (elf_dyn_lib_class (abfd)
4055 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4056 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4057
4058 s = bfd_get_section_by_name (abfd, ".dynamic");
4059 if (s != NULL)
4060 {
4061 bfd_byte *dynbuf;
4062 bfd_byte *extdyn;
cb33740c 4063 unsigned int elfsec;
4ad4eba5
AM
4064 unsigned long shlink;
4065
eea6121a 4066 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4067 {
4068error_free_dyn:
4069 free (dynbuf);
4070 goto error_return;
4071 }
4ad4eba5
AM
4072
4073 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4074 if (elfsec == SHN_BAD)
4ad4eba5
AM
4075 goto error_free_dyn;
4076 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4077
4078 for (extdyn = dynbuf;
9bff840e 4079 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4080 extdyn += bed->s->sizeof_dyn)
4081 {
4082 Elf_Internal_Dyn dyn;
4083
4084 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4085 if (dyn.d_tag == DT_SONAME)
4086 {
4087 unsigned int tagv = dyn.d_un.d_val;
4088 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4089 if (soname == NULL)
4090 goto error_free_dyn;
4091 }
4092 if (dyn.d_tag == DT_NEEDED)
4093 {
4094 struct bfd_link_needed_list *n, **pn;
4095 char *fnm, *anm;
4096 unsigned int tagv = dyn.d_un.d_val;
4097
4098 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4099 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4100 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4101 if (n == NULL || fnm == NULL)
4102 goto error_free_dyn;
4103 amt = strlen (fnm) + 1;
a50b1753 4104 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4105 if (anm == NULL)
4106 goto error_free_dyn;
4107 memcpy (anm, fnm, amt);
4108 n->name = anm;
4109 n->by = abfd;
4110 n->next = NULL;
66eb6687 4111 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4112 ;
4113 *pn = n;
4114 }
4115 if (dyn.d_tag == DT_RUNPATH)
4116 {
4117 struct bfd_link_needed_list *n, **pn;
4118 char *fnm, *anm;
4119 unsigned int tagv = dyn.d_un.d_val;
4120
4121 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4122 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4123 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4124 if (n == NULL || fnm == NULL)
4125 goto error_free_dyn;
4126 amt = strlen (fnm) + 1;
a50b1753 4127 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4128 if (anm == NULL)
4129 goto error_free_dyn;
4130 memcpy (anm, fnm, amt);
4131 n->name = anm;
4132 n->by = abfd;
4133 n->next = NULL;
4134 for (pn = & runpath;
4135 *pn != NULL;
4136 pn = &(*pn)->next)
4137 ;
4138 *pn = n;
4139 }
4140 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4141 if (!runpath && dyn.d_tag == DT_RPATH)
4142 {
4143 struct bfd_link_needed_list *n, **pn;
4144 char *fnm, *anm;
4145 unsigned int tagv = dyn.d_un.d_val;
4146
4147 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4148 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4149 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4150 if (n == NULL || fnm == NULL)
4151 goto error_free_dyn;
4152 amt = strlen (fnm) + 1;
a50b1753 4153 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4154 if (anm == NULL)
f8703194 4155 goto error_free_dyn;
4ad4eba5
AM
4156 memcpy (anm, fnm, amt);
4157 n->name = anm;
4158 n->by = abfd;
4159 n->next = NULL;
4160 for (pn = & rpath;
4161 *pn != NULL;
4162 pn = &(*pn)->next)
4163 ;
4164 *pn = n;
4165 }
7ee314fa
AM
4166 if (dyn.d_tag == DT_AUDIT)
4167 {
4168 unsigned int tagv = dyn.d_un.d_val;
4169 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4170 }
4ad4eba5
AM
4171 }
4172
4173 free (dynbuf);
4174 }
4175
4176 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4177 frees all more recently bfd_alloc'd blocks as well. */
4178 if (runpath)
4179 rpath = runpath;
4180
4181 if (rpath)
4182 {
4183 struct bfd_link_needed_list **pn;
66eb6687 4184 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4185 ;
4186 *pn = rpath;
4187 }
4188
9acc85a6
AM
4189 /* If we have a PT_GNU_RELRO program header, mark as read-only
4190 all sections contained fully therein. This makes relro
4191 shared library sections appear as they will at run-time. */
4192 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4193 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4194 if (phdr->p_type == PT_GNU_RELRO)
4195 {
4196 for (s = abfd->sections; s != NULL; s = s->next)
4197 if ((s->flags & SEC_ALLOC) != 0
4198 && s->vma >= phdr->p_vaddr
4199 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4200 s->flags |= SEC_READONLY;
4201 break;
4202 }
4203
4ad4eba5
AM
4204 /* We do not want to include any of the sections in a dynamic
4205 object in the output file. We hack by simply clobbering the
4206 list of sections in the BFD. This could be handled more
4207 cleanly by, say, a new section flag; the existing
4208 SEC_NEVER_LOAD flag is not the one we want, because that one
4209 still implies that the section takes up space in the output
4210 file. */
4211 bfd_section_list_clear (abfd);
4212
4ad4eba5
AM
4213 /* Find the name to use in a DT_NEEDED entry that refers to this
4214 object. If the object has a DT_SONAME entry, we use it.
4215 Otherwise, if the generic linker stuck something in
4216 elf_dt_name, we use that. Otherwise, we just use the file
4217 name. */
4218 if (soname == NULL || *soname == '\0')
4219 {
4220 soname = elf_dt_name (abfd);
4221 if (soname == NULL || *soname == '\0')
4222 soname = bfd_get_filename (abfd);
4223 }
4224
4225 /* Save the SONAME because sometimes the linker emulation code
4226 will need to know it. */
4227 elf_dt_name (abfd) = soname;
4228
7e9f0867 4229 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4230 if (ret < 0)
4231 goto error_return;
4232
4233 /* If we have already included this dynamic object in the
4234 link, just ignore it. There is no reason to include a
4235 particular dynamic object more than once. */
4236 if (ret > 0)
4237 return TRUE;
7ee314fa
AM
4238
4239 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4240 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4241 }
4242
4243 /* If this is a dynamic object, we always link against the .dynsym
4244 symbol table, not the .symtab symbol table. The dynamic linker
4245 will only see the .dynsym symbol table, so there is no reason to
4246 look at .symtab for a dynamic object. */
4247
4248 if (! dynamic || elf_dynsymtab (abfd) == 0)
4249 hdr = &elf_tdata (abfd)->symtab_hdr;
4250 else
4251 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4252
4253 symcount = hdr->sh_size / bed->s->sizeof_sym;
4254
4255 /* The sh_info field of the symtab header tells us where the
4256 external symbols start. We don't care about the local symbols at
4257 this point. */
4258 if (elf_bad_symtab (abfd))
4259 {
4260 extsymcount = symcount;
4261 extsymoff = 0;
4262 }
4263 else
4264 {
4265 extsymcount = symcount - hdr->sh_info;
4266 extsymoff = hdr->sh_info;
4267 }
4268
f45794cb 4269 sym_hash = elf_sym_hashes (abfd);
012b2306 4270 if (extsymcount != 0)
4ad4eba5
AM
4271 {
4272 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4273 NULL, NULL, NULL);
4274 if (isymbuf == NULL)
4275 goto error_return;
4276
4ad4eba5 4277 if (sym_hash == NULL)
012b2306
AM
4278 {
4279 /* We store a pointer to the hash table entry for each
4280 external symbol. */
ef53be89
AM
4281 amt = extsymcount;
4282 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4283 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4284 if (sym_hash == NULL)
4285 goto error_free_sym;
4286 elf_sym_hashes (abfd) = sym_hash;
4287 }
4ad4eba5
AM
4288 }
4289
4290 if (dynamic)
4291 {
4292 /* Read in any version definitions. */
fc0e6df6
PB
4293 if (!_bfd_elf_slurp_version_tables (abfd,
4294 info->default_imported_symver))
4ad4eba5
AM
4295 goto error_free_sym;
4296
4297 /* Read in the symbol versions, but don't bother to convert them
4298 to internal format. */
4299 if (elf_dynversym (abfd) != 0)
4300 {
4301 Elf_Internal_Shdr *versymhdr;
4302
4303 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
be22c732
NC
4304 amt = versymhdr->sh_size;
4305 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4ad4eba5
AM
4306 if (extversym == NULL)
4307 goto error_free_sym;
4ad4eba5
AM
4308 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4309 || bfd_bread (extversym, amt, abfd) != amt)
4310 goto error_free_vers;
be22c732 4311 extversym_end = extversym + (amt / sizeof (* extversym));
4ad4eba5
AM
4312 }
4313 }
4314
66eb6687
AM
4315 /* If we are loading an as-needed shared lib, save the symbol table
4316 state before we start adding symbols. If the lib turns out
4317 to be unneeded, restore the state. */
4318 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4319 {
4320 unsigned int i;
4321 size_t entsize;
4322
4323 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4324 {
4325 struct bfd_hash_entry *p;
2de92251 4326 struct elf_link_hash_entry *h;
66eb6687
AM
4327
4328 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4329 {
4330 h = (struct elf_link_hash_entry *) p;
4331 entsize += htab->root.table.entsize;
4332 if (h->root.type == bfd_link_hash_warning)
4333 entsize += htab->root.table.entsize;
4334 }
66eb6687
AM
4335 }
4336
4337 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4338 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4339 if (old_tab == NULL)
4340 goto error_free_vers;
4341
4342 /* Remember the current objalloc pointer, so that all mem for
4343 symbols added can later be reclaimed. */
4344 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4345 if (alloc_mark == NULL)
4346 goto error_free_vers;
4347
5061a885
AM
4348 /* Make a special call to the linker "notice" function to
4349 tell it that we are about to handle an as-needed lib. */
e5034e59 4350 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4351 goto error_free_vers;
5061a885 4352
f45794cb
AM
4353 /* Clone the symbol table. Remember some pointers into the
4354 symbol table, and dynamic symbol count. */
4355 old_ent = (char *) old_tab + tabsize;
66eb6687 4356 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4357 old_undefs = htab->root.undefs;
4358 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4359 old_table = htab->root.table.table;
4360 old_size = htab->root.table.size;
4361 old_count = htab->root.table.count;
5b677558
AM
4362 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4363 if (old_strtab == NULL)
4364 goto error_free_vers;
66eb6687
AM
4365
4366 for (i = 0; i < htab->root.table.size; i++)
4367 {
4368 struct bfd_hash_entry *p;
2de92251 4369 struct elf_link_hash_entry *h;
66eb6687
AM
4370
4371 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4372 {
4373 memcpy (old_ent, p, htab->root.table.entsize);
4374 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4375 h = (struct elf_link_hash_entry *) p;
4376 if (h->root.type == bfd_link_hash_warning)
4377 {
4378 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4379 old_ent = (char *) old_ent + htab->root.table.entsize;
4380 }
66eb6687
AM
4381 }
4382 }
4383 }
4ad4eba5 4384
66eb6687 4385 weaks = NULL;
be22c732
NC
4386 if (extversym == NULL)
4387 ever = NULL;
4388 else if (extversym + extsymoff < extversym_end)
4389 ever = extversym + extsymoff;
4390 else
4391 {
4392 /* xgettext:c-format */
4393 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4394 abfd, (long) extsymoff,
4395 (long) (extversym_end - extversym) / sizeof (* extversym));
4396 bfd_set_error (bfd_error_bad_value);
4397 goto error_free_vers;
4398 }
4399
b4c555cf
ML
4400 if (!bfd_link_relocatable (info)
4401 && abfd->lto_slim_object)
cc5277b1
ML
4402 {
4403 _bfd_error_handler
4404 (_("%pB: plugin needed to handle lto object"), abfd);
4405 }
4406
4ad4eba5
AM
4407 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4408 isym < isymend;
4409 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4410 {
4411 int bind;
4412 bfd_vma value;
af44c138 4413 asection *sec, *new_sec;
4ad4eba5
AM
4414 flagword flags;
4415 const char *name;
4416 struct elf_link_hash_entry *h;
90c984fc 4417 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4418 bfd_boolean definition;
4419 bfd_boolean size_change_ok;
4420 bfd_boolean type_change_ok;
37a9e49a
L
4421 bfd_boolean new_weak;
4422 bfd_boolean old_weak;
4ad4eba5 4423 bfd_boolean override;
a4d8e49b 4424 bfd_boolean common;
97196564 4425 bfd_boolean discarded;
4ad4eba5 4426 unsigned int old_alignment;
4538d1c7 4427 unsigned int shindex;
4ad4eba5 4428 bfd *old_bfd;
6e33951e 4429 bfd_boolean matched;
4ad4eba5
AM
4430
4431 override = FALSE;
4432
4433 flags = BSF_NO_FLAGS;
4434 sec = NULL;
4435 value = isym->st_value;
a4d8e49b 4436 common = bed->common_definition (isym);
2980ccad
L
4437 if (common && info->inhibit_common_definition)
4438 {
4439 /* Treat common symbol as undefined for --no-define-common. */
4440 isym->st_shndx = SHN_UNDEF;
4441 common = FALSE;
4442 }
97196564 4443 discarded = FALSE;
4ad4eba5
AM
4444
4445 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4446 switch (bind)
4ad4eba5 4447 {
3e7a7d11 4448 case STB_LOCAL:
4ad4eba5
AM
4449 /* This should be impossible, since ELF requires that all
4450 global symbols follow all local symbols, and that sh_info
4451 point to the first global symbol. Unfortunately, Irix 5
4452 screws this up. */
fe3fef62
AM
4453 if (elf_bad_symtab (abfd))
4454 continue;
4455
4456 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4457 then we'll likely segfault on a NULL symbol hash if the
4458 symbol is ever referenced in relocations. */
4459 shindex = elf_elfheader (abfd)->e_shstrndx;
4460 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4461 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4462 " (>= sh_info of %lu)"),
4463 abfd, name, (long) (isym - isymbuf + extsymoff),
4464 (long) extsymoff);
4465
4466 /* Dynamic object relocations are not processed by ld, so
4467 ld won't run into the problem mentioned above. */
4468 if (dynamic)
4469 continue;
fe3fef62
AM
4470 bfd_set_error (bfd_error_bad_value);
4471 goto error_free_vers;
3e7a7d11
NC
4472
4473 case STB_GLOBAL:
a4d8e49b 4474 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4475 flags = BSF_GLOBAL;
3e7a7d11
NC
4476 break;
4477
4478 case STB_WEAK:
4479 flags = BSF_WEAK;
4480 break;
4481
4482 case STB_GNU_UNIQUE:
4483 flags = BSF_GNU_UNIQUE;
4484 break;
4485
4486 default:
4ad4eba5 4487 /* Leave it up to the processor backend. */
3e7a7d11 4488 break;
4ad4eba5
AM
4489 }
4490
4491 if (isym->st_shndx == SHN_UNDEF)
4492 sec = bfd_und_section_ptr;
cb33740c
AM
4493 else if (isym->st_shndx == SHN_ABS)
4494 sec = bfd_abs_section_ptr;
4495 else if (isym->st_shndx == SHN_COMMON)
4496 {
4497 sec = bfd_com_section_ptr;
4498 /* What ELF calls the size we call the value. What ELF
4499 calls the value we call the alignment. */
4500 value = isym->st_size;
4501 }
4502 else
4ad4eba5
AM
4503 {
4504 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4505 if (sec == NULL)
4506 sec = bfd_abs_section_ptr;
dbaa2011 4507 else if (discarded_section (sec))
529fcb95 4508 {
e5d08002
L
4509 /* Symbols from discarded section are undefined. We keep
4510 its visibility. */
529fcb95 4511 sec = bfd_und_section_ptr;
97196564 4512 discarded = TRUE;
529fcb95
PB
4513 isym->st_shndx = SHN_UNDEF;
4514 }
4ad4eba5
AM
4515 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4516 value -= sec->vma;
4517 }
4ad4eba5
AM
4518
4519 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4520 isym->st_name);
4521 if (name == NULL)
4522 goto error_free_vers;
4523
4524 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4525 && (abfd->flags & BFD_PLUGIN) != 0)
4526 {
4527 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4528
4529 if (xc == NULL)
4530 {
4531 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4532 | SEC_EXCLUDE);
4533 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4534 if (xc == NULL)
4535 goto error_free_vers;
4536 }
4537 sec = xc;
4538 }
4539 else if (isym->st_shndx == SHN_COMMON
4540 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4541 && !bfd_link_relocatable (info))
4ad4eba5
AM
4542 {
4543 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4544
4545 if (tcomm == NULL)
4546 {
02d00247
AM
4547 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4548 | SEC_LINKER_CREATED);
4549 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4550 if (tcomm == NULL)
4ad4eba5
AM
4551 goto error_free_vers;
4552 }
4553 sec = tcomm;
4554 }
66eb6687 4555 else if (bed->elf_add_symbol_hook)
4ad4eba5 4556 {
66eb6687
AM
4557 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4558 &sec, &value))
4ad4eba5
AM
4559 goto error_free_vers;
4560
4561 /* The hook function sets the name to NULL if this symbol
4562 should be skipped for some reason. */
4563 if (name == NULL)
4564 continue;
4565 }
4566
4567 /* Sanity check that all possibilities were handled. */
4568 if (sec == NULL)
4538d1c7 4569 abort ();
4ad4eba5 4570
191c0c42
AM
4571 /* Silently discard TLS symbols from --just-syms. There's
4572 no way to combine a static TLS block with a new TLS block
4573 for this executable. */
4574 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4575 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4576 continue;
4577
4ad4eba5
AM
4578 if (bfd_is_und_section (sec)
4579 || bfd_is_com_section (sec))
4580 definition = FALSE;
4581 else
4582 definition = TRUE;
4583
4584 size_change_ok = FALSE;
66eb6687 4585 type_change_ok = bed->type_change_ok;
37a9e49a 4586 old_weak = FALSE;
6e33951e 4587 matched = FALSE;
4ad4eba5
AM
4588 old_alignment = 0;
4589 old_bfd = NULL;
af44c138 4590 new_sec = sec;
4ad4eba5 4591
66eb6687 4592 if (is_elf_hash_table (htab))
4ad4eba5
AM
4593 {
4594 Elf_Internal_Versym iver;
4595 unsigned int vernum = 0;
4596 bfd_boolean skip;
4597
fc0e6df6 4598 if (ever == NULL)
4ad4eba5 4599 {
fc0e6df6
PB
4600 if (info->default_imported_symver)
4601 /* Use the default symbol version created earlier. */
4602 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4603 else
4604 iver.vs_vers = 0;
4605 }
be22c732
NC
4606 else if (ever >= extversym_end)
4607 {
4608 /* xgettext:c-format */
4609 _bfd_error_handler (_("%pB: not enough version information"),
4610 abfd);
4611 bfd_set_error (bfd_error_bad_value);
4612 goto error_free_vers;
4613 }
fc0e6df6
PB
4614 else
4615 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4616
4617 vernum = iver.vs_vers & VERSYM_VERSION;
4618
4619 /* If this is a hidden symbol, or if it is not version
4620 1, we append the version name to the symbol name.
cc86ff91
EB
4621 However, we do not modify a non-hidden absolute symbol
4622 if it is not a function, because it might be the version
4623 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4624 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4625 || (vernum > 1
4626 && (!bfd_is_abs_section (sec)
4627 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4628 {
4629 const char *verstr;
4630 size_t namelen, verlen, newlen;
4631 char *newname, *p;
4632
4633 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4634 {
fc0e6df6
PB
4635 if (vernum > elf_tdata (abfd)->cverdefs)
4636 verstr = NULL;
4637 else if (vernum > 1)
4638 verstr =
4639 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4640 else
4641 verstr = "";
4ad4eba5 4642
fc0e6df6 4643 if (verstr == NULL)
4ad4eba5 4644 {
4eca0228 4645 _bfd_error_handler
695344c0 4646 /* xgettext:c-format */
871b3ab2 4647 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4648 abfd, name, vernum,
4649 elf_tdata (abfd)->cverdefs);
4650 bfd_set_error (bfd_error_bad_value);
4651 goto error_free_vers;
4ad4eba5 4652 }
fc0e6df6
PB
4653 }
4654 else
4655 {
4656 /* We cannot simply test for the number of
4657 entries in the VERNEED section since the
4658 numbers for the needed versions do not start
4659 at 0. */
4660 Elf_Internal_Verneed *t;
4661
4662 verstr = NULL;
4663 for (t = elf_tdata (abfd)->verref;
4664 t != NULL;
4665 t = t->vn_nextref)
4ad4eba5 4666 {
fc0e6df6 4667 Elf_Internal_Vernaux *a;
4ad4eba5 4668
fc0e6df6
PB
4669 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4670 {
4671 if (a->vna_other == vernum)
4ad4eba5 4672 {
fc0e6df6
PB
4673 verstr = a->vna_nodename;
4674 break;
4ad4eba5 4675 }
4ad4eba5 4676 }
fc0e6df6
PB
4677 if (a != NULL)
4678 break;
4679 }
4680 if (verstr == NULL)
4681 {
4eca0228 4682 _bfd_error_handler
695344c0 4683 /* xgettext:c-format */
871b3ab2 4684 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4685 abfd, name, vernum);
4686 bfd_set_error (bfd_error_bad_value);
4687 goto error_free_vers;
4ad4eba5 4688 }
4ad4eba5 4689 }
fc0e6df6
PB
4690
4691 namelen = strlen (name);
4692 verlen = strlen (verstr);
4693 newlen = namelen + verlen + 2;
4694 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4695 && isym->st_shndx != SHN_UNDEF)
4696 ++newlen;
4697
a50b1753 4698 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4699 if (newname == NULL)
4700 goto error_free_vers;
4701 memcpy (newname, name, namelen);
4702 p = newname + namelen;
4703 *p++ = ELF_VER_CHR;
4704 /* If this is a defined non-hidden version symbol,
4705 we add another @ to the name. This indicates the
4706 default version of the symbol. */
4707 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4708 && isym->st_shndx != SHN_UNDEF)
4709 *p++ = ELF_VER_CHR;
4710 memcpy (p, verstr, verlen + 1);
4711
4712 name = newname;
4ad4eba5
AM
4713 }
4714
cd3416da
AM
4715 /* If this symbol has default visibility and the user has
4716 requested we not re-export it, then mark it as hidden. */
a0d49154 4717 if (!bfd_is_und_section (sec)
cd3416da 4718 && !dynamic
ce875075 4719 && abfd->no_export
cd3416da
AM
4720 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4721 isym->st_other = (STV_HIDDEN
4722 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4723
4f3fedcf
AM
4724 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4725 sym_hash, &old_bfd, &old_weak,
4726 &old_alignment, &skip, &override,
6e33951e
L
4727 &type_change_ok, &size_change_ok,
4728 &matched))
4ad4eba5
AM
4729 goto error_free_vers;
4730
4731 if (skip)
4732 continue;
4733
6e33951e
L
4734 /* Override a definition only if the new symbol matches the
4735 existing one. */
4736 if (override && matched)
4ad4eba5
AM
4737 definition = FALSE;
4738
4739 h = *sym_hash;
4740 while (h->root.type == bfd_link_hash_indirect
4741 || h->root.type == bfd_link_hash_warning)
4742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4743
4ad4eba5 4744 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4745 && vernum > 1
4746 && definition)
4747 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4748 }
4749
4750 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4751 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4752 (struct bfd_link_hash_entry **) sym_hash)))
4753 goto error_free_vers;
4754
4755 h = *sym_hash;
90c984fc
L
4756 /* We need to make sure that indirect symbol dynamic flags are
4757 updated. */
4758 hi = h;
4ad4eba5
AM
4759 while (h->root.type == bfd_link_hash_indirect
4760 || h->root.type == bfd_link_hash_warning)
4761 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4762
97196564
L
4763 /* Setting the index to -3 tells elf_link_output_extsym that
4764 this symbol is defined in a discarded section. */
4765 if (discarded)
4766 h->indx = -3;
4767
4ad4eba5
AM
4768 *sym_hash = h;
4769
37a9e49a 4770 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4771 if (dynamic
4772 && definition
37a9e49a 4773 && new_weak
fcb93ecf 4774 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4775 && is_elf_hash_table (htab)
60d67dc8 4776 && h->u.alias == NULL)
4ad4eba5
AM
4777 {
4778 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4779 a dynamic object, using the alias field. Later in this
4780 function we will set the alias field to the correct
4ad4eba5
AM
4781 value. We only put non-function symbols from dynamic
4782 objects on this list, because that happens to be the only
4783 time we need to know the normal symbol corresponding to a
4784 weak symbol, and the information is time consuming to
60d67dc8 4785 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4786 then this symbol was already defined by some previous
4787 dynamic object, and we will be using that previous
4788 definition anyhow. */
4789
60d67dc8 4790 h->u.alias = weaks;
4ad4eba5 4791 weaks = h;
4ad4eba5
AM
4792 }
4793
4794 /* Set the alignment of a common symbol. */
a4d8e49b 4795 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4796 && h->root.type == bfd_link_hash_common)
4797 {
4798 unsigned int align;
4799
a4d8e49b 4800 if (common)
af44c138
L
4801 align = bfd_log2 (isym->st_value);
4802 else
4803 {
4804 /* The new symbol is a common symbol in a shared object.
4805 We need to get the alignment from the section. */
4806 align = new_sec->alignment_power;
4807 }
595213d4 4808 if (align > old_alignment)
4ad4eba5
AM
4809 h->root.u.c.p->alignment_power = align;
4810 else
4811 h->root.u.c.p->alignment_power = old_alignment;
4812 }
4813
66eb6687 4814 if (is_elf_hash_table (htab))
4ad4eba5 4815 {
4f3fedcf
AM
4816 /* Set a flag in the hash table entry indicating the type of
4817 reference or definition we just found. A dynamic symbol
4818 is one which is referenced or defined by both a regular
4819 object and a shared object. */
4820 bfd_boolean dynsym = FALSE;
4821
4822 /* Plugin symbols aren't normal. Don't set def_regular or
4823 ref_regular for them, or make them dynamic. */
4824 if ((abfd->flags & BFD_PLUGIN) != 0)
4825 ;
4826 else if (! dynamic)
4827 {
4828 if (! definition)
4829 {
4830 h->ref_regular = 1;
4831 if (bind != STB_WEAK)
4832 h->ref_regular_nonweak = 1;
4833 }
4834 else
4835 {
4836 h->def_regular = 1;
4837 if (h->def_dynamic)
4838 {
4839 h->def_dynamic = 0;
4840 h->ref_dynamic = 1;
4841 }
4842 }
4843
4844 /* If the indirect symbol has been forced local, don't
4845 make the real symbol dynamic. */
4846 if ((h == hi || !hi->forced_local)
0e1862bb 4847 && (bfd_link_dll (info)
4f3fedcf
AM
4848 || h->def_dynamic
4849 || h->ref_dynamic))
4850 dynsym = TRUE;
4851 }
4852 else
4853 {
4854 if (! definition)
4855 {
4856 h->ref_dynamic = 1;
4857 hi->ref_dynamic = 1;
4858 }
4859 else
4860 {
4861 h->def_dynamic = 1;
4862 hi->def_dynamic = 1;
4863 }
4864
4865 /* If the indirect symbol has been forced local, don't
4866 make the real symbol dynamic. */
4867 if ((h == hi || !hi->forced_local)
4868 && (h->def_regular
4869 || h->ref_regular
60d67dc8
AM
4870 || (h->is_weakalias
4871 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4872 dynsym = TRUE;
4873 }
4874
4875 /* Check to see if we need to add an indirect symbol for
4876 the default name. */
4877 if (definition
4878 || (!override && h->root.type == bfd_link_hash_common))
4879 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4880 sec, value, &old_bfd, &dynsym))
4881 goto error_free_vers;
4ad4eba5
AM
4882
4883 /* Check the alignment when a common symbol is involved. This
4884 can change when a common symbol is overridden by a normal
4885 definition or a common symbol is ignored due to the old
4886 normal definition. We need to make sure the maximum
4887 alignment is maintained. */
a4d8e49b 4888 if ((old_alignment || common)
4ad4eba5
AM
4889 && h->root.type != bfd_link_hash_common)
4890 {
4891 unsigned int common_align;
4892 unsigned int normal_align;
4893 unsigned int symbol_align;
4894 bfd *normal_bfd;
4895 bfd *common_bfd;
4896
3a81e825
AM
4897 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4898 || h->root.type == bfd_link_hash_defweak);
4899
4ad4eba5
AM
4900 symbol_align = ffs (h->root.u.def.value) - 1;
4901 if (h->root.u.def.section->owner != NULL
0616a280
AM
4902 && (h->root.u.def.section->owner->flags
4903 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4904 {
4905 normal_align = h->root.u.def.section->alignment_power;
4906 if (normal_align > symbol_align)
4907 normal_align = symbol_align;
4908 }
4909 else
4910 normal_align = symbol_align;
4911
4912 if (old_alignment)
4913 {
4914 common_align = old_alignment;
4915 common_bfd = old_bfd;
4916 normal_bfd = abfd;
4917 }
4918 else
4919 {
4920 common_align = bfd_log2 (isym->st_value);
4921 common_bfd = abfd;
4922 normal_bfd = old_bfd;
4923 }
4924
4925 if (normal_align < common_align)
d07676f8
NC
4926 {
4927 /* PR binutils/2735 */
4928 if (normal_bfd == NULL)
4eca0228 4929 _bfd_error_handler
695344c0 4930 /* xgettext:c-format */
9793eb77 4931 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4932 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4933 1 << common_align, name, common_bfd,
4934 1 << normal_align, h->root.u.def.section);
d07676f8 4935 else
4eca0228 4936 _bfd_error_handler
695344c0 4937 /* xgettext:c-format */
9793eb77 4938 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4939 " is smaller than %u in %pB"),
c08bb8dd
AM
4940 1 << normal_align, name, normal_bfd,
4941 1 << common_align, common_bfd);
d07676f8 4942 }
4ad4eba5
AM
4943 }
4944
83ad0046 4945 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4946 if (isym->st_size != 0
4947 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4948 && (definition || h->size == 0))
4949 {
83ad0046
L
4950 if (h->size != 0
4951 && h->size != isym->st_size
4952 && ! size_change_ok)
4eca0228 4953 _bfd_error_handler
695344c0 4954 /* xgettext:c-format */
9793eb77 4955 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4956 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4957 name, (uint64_t) h->size, old_bfd,
4958 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4959
4960 h->size = isym->st_size;
4961 }
4962
4963 /* If this is a common symbol, then we always want H->SIZE
4964 to be the size of the common symbol. The code just above
4965 won't fix the size if a common symbol becomes larger. We
4966 don't warn about a size change here, because that is
4f3fedcf 4967 covered by --warn-common. Allow changes between different
fcb93ecf 4968 function types. */
4ad4eba5
AM
4969 if (h->root.type == bfd_link_hash_common)
4970 h->size = h->root.u.c.size;
4971
4972 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4973 && ((definition && !new_weak)
4974 || (old_weak && h->root.type == bfd_link_hash_common)
4975 || h->type == STT_NOTYPE))
4ad4eba5 4976 {
2955ec4c
L
4977 unsigned int type = ELF_ST_TYPE (isym->st_info);
4978
4979 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4980 symbol. */
4981 if (type == STT_GNU_IFUNC
4982 && (abfd->flags & DYNAMIC) != 0)
4983 type = STT_FUNC;
4ad4eba5 4984
2955ec4c
L
4985 if (h->type != type)
4986 {
4987 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4988 /* xgettext:c-format */
4eca0228 4989 _bfd_error_handler
9793eb77 4990 (_("warning: type of symbol `%s' changed"
871b3ab2 4991 " from %d to %d in %pB"),
c08bb8dd 4992 name, h->type, type, abfd);
2955ec4c
L
4993
4994 h->type = type;
4995 }
4ad4eba5
AM
4996 }
4997
54ac0771 4998 /* Merge st_other field. */
b8417128 4999 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 5000
c3df8c14 5001 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5002 if (definition
5003 && (sec->flags & SEC_DEBUGGING)
5004 && !bfd_link_relocatable (info))
c3df8c14
AM
5005 dynsym = FALSE;
5006
4f3fedcf
AM
5007 /* Nor should we make plugin symbols dynamic. */
5008 if ((abfd->flags & BFD_PLUGIN) != 0)
5009 dynsym = FALSE;
5010
35fc36a8 5011 if (definition)
35399224
L
5012 {
5013 h->target_internal = isym->st_target_internal;
5014 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5015 }
35fc36a8 5016
4ad4eba5
AM
5017 if (definition && !dynamic)
5018 {
5019 char *p = strchr (name, ELF_VER_CHR);
5020 if (p != NULL && p[1] != ELF_VER_CHR)
5021 {
5022 /* Queue non-default versions so that .symver x, x@FOO
5023 aliases can be checked. */
66eb6687 5024 if (!nondeflt_vers)
4ad4eba5 5025 {
66eb6687
AM
5026 amt = ((isymend - isym + 1)
5027 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5028 nondeflt_vers
5029 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5030 if (!nondeflt_vers)
5031 goto error_free_vers;
4ad4eba5 5032 }
66eb6687 5033 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5034 }
5035 }
5036
5037 if (dynsym && h->dynindx == -1)
5038 {
c152c796 5039 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5040 goto error_free_vers;
60d67dc8
AM
5041 if (h->is_weakalias
5042 && weakdef (h)->dynindx == -1)
4ad4eba5 5043 {
60d67dc8 5044 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5045 goto error_free_vers;
5046 }
5047 }
1f599d0e 5048 else if (h->dynindx != -1)
4ad4eba5
AM
5049 /* If the symbol already has a dynamic index, but
5050 visibility says it should not be visible, turn it into
5051 a local symbol. */
5052 switch (ELF_ST_VISIBILITY (h->other))
5053 {
5054 case STV_INTERNAL:
5055 case STV_HIDDEN:
5056 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5057 dynsym = FALSE;
5058 break;
5059 }
5060
aef28989
L
5061 /* Don't add DT_NEEDED for references from the dummy bfd nor
5062 for unmatched symbol. */
4ad4eba5 5063 if (!add_needed
aef28989 5064 && matched
4ad4eba5 5065 && definition
010e5ae2 5066 && ((dynsym
ffa9430d 5067 && h->ref_regular_nonweak
4f3fedcf
AM
5068 && (old_bfd == NULL
5069 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5070 || (h->ref_dynamic_nonweak
010e5ae2 5071 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5072 && !on_needed_list (elf_dt_name (abfd),
5073 htab->needed, NULL))))
4ad4eba5
AM
5074 {
5075 int ret;
5076 const char *soname = elf_dt_name (abfd);
5077
16e4ecc0
AM
5078 info->callbacks->minfo ("%!", soname, old_bfd,
5079 h->root.root.string);
5080
4ad4eba5
AM
5081 /* A symbol from a library loaded via DT_NEEDED of some
5082 other library is referenced by a regular object.
e56f61be 5083 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5084 --no-add-needed is used and the reference was not
5085 a weak one. */
4f3fedcf 5086 if (old_bfd != NULL
b918acf9 5087 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5088 {
4eca0228 5089 _bfd_error_handler
695344c0 5090 /* xgettext:c-format */
871b3ab2 5091 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5092 old_bfd, name);
ff5ac77b 5093 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5094 goto error_free_vers;
5095 }
5096
a50b1753 5097 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5098 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5099
4ad4eba5 5100 add_needed = TRUE;
7e9f0867 5101 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5102 if (ret < 0)
5103 goto error_free_vers;
5104
5105 BFD_ASSERT (ret == 0);
5106 }
5107 }
5108 }
5109
a83ef4d1
L
5110 if (info->lto_plugin_active
5111 && !bfd_link_relocatable (info)
5112 && (abfd->flags & BFD_PLUGIN) == 0
5113 && !just_syms
5114 && extsymcount)
5115 {
5116 int r_sym_shift;
5117
5118 if (bed->s->arch_size == 32)
5119 r_sym_shift = 8;
5120 else
5121 r_sym_shift = 32;
5122
5123 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5124 referenced in regular objects so that linker plugin will get
5125 the correct symbol resolution. */
5126
5127 sym_hash = elf_sym_hashes (abfd);
5128 for (s = abfd->sections; s != NULL; s = s->next)
5129 {
5130 Elf_Internal_Rela *internal_relocs;
5131 Elf_Internal_Rela *rel, *relend;
5132
5133 /* Don't check relocations in excluded sections. */
5134 if ((s->flags & SEC_RELOC) == 0
5135 || s->reloc_count == 0
5136 || (s->flags & SEC_EXCLUDE) != 0
5137 || ((info->strip == strip_all
5138 || info->strip == strip_debugger)
5139 && (s->flags & SEC_DEBUGGING) != 0))
5140 continue;
5141
5142 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5143 NULL,
5144 info->keep_memory);
5145 if (internal_relocs == NULL)
5146 goto error_free_vers;
5147
5148 rel = internal_relocs;
5149 relend = rel + s->reloc_count;
5150 for ( ; rel < relend; rel++)
5151 {
5152 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5153 struct elf_link_hash_entry *h;
5154
5155 /* Skip local symbols. */
5156 if (r_symndx < extsymoff)
5157 continue;
5158
5159 h = sym_hash[r_symndx - extsymoff];
5160 if (h != NULL)
5161 h->root.non_ir_ref_regular = 1;
5162 }
5163
5164 if (elf_section_data (s)->relocs != internal_relocs)
5165 free (internal_relocs);
5166 }
5167 }
5168
66eb6687
AM
5169 if (extversym != NULL)
5170 {
5171 free (extversym);
5172 extversym = NULL;
5173 }
5174
5175 if (isymbuf != NULL)
5176 {
5177 free (isymbuf);
5178 isymbuf = NULL;
5179 }
5180
5181 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5182 {
5183 unsigned int i;
5184
5185 /* Restore the symbol table. */
f45794cb
AM
5186 old_ent = (char *) old_tab + tabsize;
5187 memset (elf_sym_hashes (abfd), 0,
5188 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5189 htab->root.table.table = old_table;
5190 htab->root.table.size = old_size;
5191 htab->root.table.count = old_count;
66eb6687 5192 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5193 htab->root.undefs = old_undefs;
5194 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5195 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5196 free (old_strtab);
5197 old_strtab = NULL;
66eb6687
AM
5198 for (i = 0; i < htab->root.table.size; i++)
5199 {
5200 struct bfd_hash_entry *p;
5201 struct elf_link_hash_entry *h;
3e0882af
L
5202 bfd_size_type size;
5203 unsigned int alignment_power;
4070765b 5204 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5205
5206 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5207 {
5208 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5209 if (h->root.type == bfd_link_hash_warning)
5210 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5211
3e0882af
L
5212 /* Preserve the maximum alignment and size for common
5213 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5214 since it can still be loaded at run time by another
3e0882af
L
5215 dynamic lib. */
5216 if (h->root.type == bfd_link_hash_common)
5217 {
5218 size = h->root.u.c.size;
5219 alignment_power = h->root.u.c.p->alignment_power;
5220 }
5221 else
5222 {
5223 size = 0;
5224 alignment_power = 0;
5225 }
4070765b 5226 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5227 will be exported when the dynamic lib becomes needed
5228 in the second pass. */
4070765b 5229 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5230 memcpy (p, old_ent, htab->root.table.entsize);
5231 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5232 h = (struct elf_link_hash_entry *) p;
5233 if (h->root.type == bfd_link_hash_warning)
5234 {
5235 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5236 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5238 }
a4542f1b 5239 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5240 {
5241 if (size > h->root.u.c.size)
5242 h->root.u.c.size = size;
5243 if (alignment_power > h->root.u.c.p->alignment_power)
5244 h->root.u.c.p->alignment_power = alignment_power;
5245 }
4070765b 5246 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5247 }
5248 }
5249
5061a885
AM
5250 /* Make a special call to the linker "notice" function to
5251 tell it that symbols added for crefs may need to be removed. */
e5034e59 5252 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5253 goto error_free_vers;
5061a885 5254
66eb6687
AM
5255 free (old_tab);
5256 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5257 alloc_mark);
5258 if (nondeflt_vers != NULL)
5259 free (nondeflt_vers);
5260 return TRUE;
5261 }
2de92251 5262
66eb6687
AM
5263 if (old_tab != NULL)
5264 {
e5034e59 5265 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5266 goto error_free_vers;
66eb6687
AM
5267 free (old_tab);
5268 old_tab = NULL;
5269 }
5270
c6e8a9a8
L
5271 /* Now that all the symbols from this input file are created, if
5272 not performing a relocatable link, handle .symver foo, foo@BAR
5273 such that any relocs against foo become foo@BAR. */
0e1862bb 5274 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5275 {
ef53be89 5276 size_t cnt, symidx;
4ad4eba5
AM
5277
5278 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5279 {
5280 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5281 char *shortname, *p;
5282
5283 p = strchr (h->root.root.string, ELF_VER_CHR);
5284 if (p == NULL
5285 || (h->root.type != bfd_link_hash_defined
5286 && h->root.type != bfd_link_hash_defweak))
5287 continue;
5288
5289 amt = p - h->root.root.string;
a50b1753 5290 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5291 if (!shortname)
5292 goto error_free_vers;
4ad4eba5
AM
5293 memcpy (shortname, h->root.root.string, amt);
5294 shortname[amt] = '\0';
5295
5296 hi = (struct elf_link_hash_entry *)
66eb6687 5297 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5298 FALSE, FALSE, FALSE);
5299 if (hi != NULL
5300 && hi->root.type == h->root.type
5301 && hi->root.u.def.value == h->root.u.def.value
5302 && hi->root.u.def.section == h->root.u.def.section)
5303 {
5304 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5305 hi->root.type = bfd_link_hash_indirect;
5306 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5307 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5308 sym_hash = elf_sym_hashes (abfd);
5309 if (sym_hash)
5310 for (symidx = 0; symidx < extsymcount; ++symidx)
5311 if (sym_hash[symidx] == hi)
5312 {
5313 sym_hash[symidx] = h;
5314 break;
5315 }
5316 }
5317 free (shortname);
5318 }
5319 free (nondeflt_vers);
5320 nondeflt_vers = NULL;
5321 }
5322
60d67dc8 5323 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5324 symbols we found. The only way to do this is to search all the
5325 symbols. Since we only need the information for non functions in
5326 dynamic objects, that's the only time we actually put anything on
5327 the list WEAKS. We need this information so that if a regular
5328 object refers to a symbol defined weakly in a dynamic object, the
5329 real symbol in the dynamic object is also put in the dynamic
5330 symbols; we also must arrange for both symbols to point to the
5331 same memory location. We could handle the general case of symbol
5332 aliasing, but a general symbol alias can only be generated in
5333 assembler code, handling it correctly would be very time
5334 consuming, and other ELF linkers don't handle general aliasing
5335 either. */
5336 if (weaks != NULL)
5337 {
5338 struct elf_link_hash_entry **hpp;
5339 struct elf_link_hash_entry **hppend;
5340 struct elf_link_hash_entry **sorted_sym_hash;
5341 struct elf_link_hash_entry *h;
5342 size_t sym_count;
5343
5344 /* Since we have to search the whole symbol list for each weak
5345 defined symbol, search time for N weak defined symbols will be
5346 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5347 amt = extsymcount;
5348 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5349 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5350 if (sorted_sym_hash == NULL)
5351 goto error_return;
5352 sym_hash = sorted_sym_hash;
5353 hpp = elf_sym_hashes (abfd);
5354 hppend = hpp + extsymcount;
5355 sym_count = 0;
5356 for (; hpp < hppend; hpp++)
5357 {
5358 h = *hpp;
5359 if (h != NULL
5360 && h->root.type == bfd_link_hash_defined
fcb93ecf 5361 && !bed->is_function_type (h->type))
4ad4eba5
AM
5362 {
5363 *sym_hash = h;
5364 sym_hash++;
5365 sym_count++;
5366 }
5367 }
5368
5369 qsort (sorted_sym_hash, sym_count,
5370 sizeof (struct elf_link_hash_entry *),
5371 elf_sort_symbol);
5372
5373 while (weaks != NULL)
5374 {
5375 struct elf_link_hash_entry *hlook;
5376 asection *slook;
5377 bfd_vma vlook;
ed54588d 5378 size_t i, j, idx = 0;
4ad4eba5
AM
5379
5380 hlook = weaks;
60d67dc8
AM
5381 weaks = hlook->u.alias;
5382 hlook->u.alias = NULL;
4ad4eba5 5383
e3e53eed
AM
5384 if (hlook->root.type != bfd_link_hash_defined
5385 && hlook->root.type != bfd_link_hash_defweak)
5386 continue;
5387
4ad4eba5
AM
5388 slook = hlook->root.u.def.section;
5389 vlook = hlook->root.u.def.value;
5390
4ad4eba5
AM
5391 i = 0;
5392 j = sym_count;
14160578 5393 while (i != j)
4ad4eba5
AM
5394 {
5395 bfd_signed_vma vdiff;
5396 idx = (i + j) / 2;
14160578 5397 h = sorted_sym_hash[idx];
4ad4eba5
AM
5398 vdiff = vlook - h->root.u.def.value;
5399 if (vdiff < 0)
5400 j = idx;
5401 else if (vdiff > 0)
5402 i = idx + 1;
5403 else
5404 {
d3435ae8 5405 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5406 if (sdiff < 0)
5407 j = idx;
5408 else if (sdiff > 0)
5409 i = idx + 1;
5410 else
14160578 5411 break;
4ad4eba5
AM
5412 }
5413 }
5414
5415 /* We didn't find a value/section match. */
14160578 5416 if (i == j)
4ad4eba5
AM
5417 continue;
5418
14160578
AM
5419 /* With multiple aliases, or when the weak symbol is already
5420 strongly defined, we have multiple matching symbols and
5421 the binary search above may land on any of them. Step
5422 one past the matching symbol(s). */
5423 while (++idx != j)
5424 {
5425 h = sorted_sym_hash[idx];
5426 if (h->root.u.def.section != slook
5427 || h->root.u.def.value != vlook)
5428 break;
5429 }
5430
5431 /* Now look back over the aliases. Since we sorted by size
5432 as well as value and section, we'll choose the one with
5433 the largest size. */
5434 while (idx-- != i)
4ad4eba5 5435 {
14160578 5436 h = sorted_sym_hash[idx];
4ad4eba5
AM
5437
5438 /* Stop if value or section doesn't match. */
14160578
AM
5439 if (h->root.u.def.section != slook
5440 || h->root.u.def.value != vlook)
4ad4eba5
AM
5441 break;
5442 else if (h != hlook)
5443 {
60d67dc8
AM
5444 struct elf_link_hash_entry *t;
5445
5446 hlook->u.alias = h;
5447 hlook->is_weakalias = 1;
5448 t = h;
5449 if (t->u.alias != NULL)
5450 while (t->u.alias != h)
5451 t = t->u.alias;
5452 t->u.alias = hlook;
4ad4eba5
AM
5453
5454 /* If the weak definition is in the list of dynamic
5455 symbols, make sure the real definition is put
5456 there as well. */
5457 if (hlook->dynindx != -1 && h->dynindx == -1)
5458 {
c152c796 5459 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5460 {
5461 err_free_sym_hash:
5462 free (sorted_sym_hash);
5463 goto error_return;
5464 }
4ad4eba5
AM
5465 }
5466
5467 /* If the real definition is in the list of dynamic
5468 symbols, make sure the weak definition is put
5469 there as well. If we don't do this, then the
5470 dynamic loader might not merge the entries for the
5471 real definition and the weak definition. */
5472 if (h->dynindx != -1 && hlook->dynindx == -1)
5473 {
c152c796 5474 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5475 goto err_free_sym_hash;
4ad4eba5
AM
5476 }
5477 break;
5478 }
5479 }
5480 }
5481
5482 free (sorted_sym_hash);
5483 }
5484
33177bb1
AM
5485 if (bed->check_directives
5486 && !(*bed->check_directives) (abfd, info))
5487 return FALSE;
85fbca6a 5488
4ad4eba5
AM
5489 /* If this is a non-traditional link, try to optimize the handling
5490 of the .stab/.stabstr sections. */
5491 if (! dynamic
5492 && ! info->traditional_format
66eb6687 5493 && is_elf_hash_table (htab)
4ad4eba5
AM
5494 && (info->strip != strip_all && info->strip != strip_debugger))
5495 {
5496 asection *stabstr;
5497
5498 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5499 if (stabstr != NULL)
5500 {
5501 bfd_size_type string_offset = 0;
5502 asection *stab;
5503
5504 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5505 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5506 && (!stab->name[5] ||
5507 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5508 && (stab->flags & SEC_MERGE) == 0
5509 && !bfd_is_abs_section (stab->output_section))
5510 {
5511 struct bfd_elf_section_data *secdata;
5512
5513 secdata = elf_section_data (stab);
66eb6687
AM
5514 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5515 stabstr, &secdata->sec_info,
4ad4eba5
AM
5516 &string_offset))
5517 goto error_return;
5518 if (secdata->sec_info)
dbaa2011 5519 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5520 }
5521 }
5522 }
5523
66eb6687 5524 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5525 {
5526 /* Add this bfd to the loaded list. */
5527 struct elf_link_loaded_list *n;
5528
ca4be51c 5529 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5530 if (n == NULL)
5531 goto error_return;
5532 n->abfd = abfd;
66eb6687
AM
5533 n->next = htab->loaded;
5534 htab->loaded = n;
4ad4eba5
AM
5535 }
5536
5537 return TRUE;
5538
5539 error_free_vers:
66eb6687
AM
5540 if (old_tab != NULL)
5541 free (old_tab);
5b677558
AM
5542 if (old_strtab != NULL)
5543 free (old_strtab);
4ad4eba5
AM
5544 if (nondeflt_vers != NULL)
5545 free (nondeflt_vers);
5546 if (extversym != NULL)
5547 free (extversym);
5548 error_free_sym:
5549 if (isymbuf != NULL)
5550 free (isymbuf);
5551 error_return:
5552 return FALSE;
5553}
5554
8387904d
AM
5555/* Return the linker hash table entry of a symbol that might be
5556 satisfied by an archive symbol. Return -1 on error. */
5557
5558struct elf_link_hash_entry *
5559_bfd_elf_archive_symbol_lookup (bfd *abfd,
5560 struct bfd_link_info *info,
5561 const char *name)
5562{
5563 struct elf_link_hash_entry *h;
5564 char *p, *copy;
5565 size_t len, first;
5566
2a41f396 5567 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5568 if (h != NULL)
5569 return h;
5570
5571 /* If this is a default version (the name contains @@), look up the
5572 symbol again with only one `@' as well as without the version.
5573 The effect is that references to the symbol with and without the
5574 version will be matched by the default symbol in the archive. */
5575
5576 p = strchr (name, ELF_VER_CHR);
5577 if (p == NULL || p[1] != ELF_VER_CHR)
5578 return h;
5579
5580 /* First check with only one `@'. */
5581 len = strlen (name);
a50b1753 5582 copy = (char *) bfd_alloc (abfd, len);
8387904d 5583 if (copy == NULL)
e99955cd 5584 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5585
5586 first = p - name + 1;
5587 memcpy (copy, name, first);
5588 memcpy (copy + first, name + first + 1, len - first);
5589
2a41f396 5590 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5591 if (h == NULL)
5592 {
5593 /* We also need to check references to the symbol without the
5594 version. */
5595 copy[first - 1] = '\0';
5596 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5597 FALSE, FALSE, TRUE);
8387904d
AM
5598 }
5599
5600 bfd_release (abfd, copy);
5601 return h;
5602}
5603
0ad989f9 5604/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5605 don't use _bfd_generic_link_add_archive_symbols because we need to
5606 handle versioned symbols.
0ad989f9
L
5607
5608 Fortunately, ELF archive handling is simpler than that done by
5609 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5610 oddities. In ELF, if we find a symbol in the archive map, and the
5611 symbol is currently undefined, we know that we must pull in that
5612 object file.
5613
5614 Unfortunately, we do have to make multiple passes over the symbol
5615 table until nothing further is resolved. */
5616
4ad4eba5
AM
5617static bfd_boolean
5618elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5619{
5620 symindex c;
13e570f8 5621 unsigned char *included = NULL;
0ad989f9
L
5622 carsym *symdefs;
5623 bfd_boolean loop;
5624 bfd_size_type amt;
8387904d
AM
5625 const struct elf_backend_data *bed;
5626 struct elf_link_hash_entry * (*archive_symbol_lookup)
5627 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5628
5629 if (! bfd_has_map (abfd))
5630 {
5631 /* An empty archive is a special case. */
5632 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5633 return TRUE;
5634 bfd_set_error (bfd_error_no_armap);
5635 return FALSE;
5636 }
5637
5638 /* Keep track of all symbols we know to be already defined, and all
5639 files we know to be already included. This is to speed up the
5640 second and subsequent passes. */
5641 c = bfd_ardata (abfd)->symdef_count;
5642 if (c == 0)
5643 return TRUE;
5644 amt = c;
13e570f8
AM
5645 amt *= sizeof (*included);
5646 included = (unsigned char *) bfd_zmalloc (amt);
5647 if (included == NULL)
5648 return FALSE;
0ad989f9
L
5649
5650 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5651 bed = get_elf_backend_data (abfd);
5652 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5653
5654 do
5655 {
5656 file_ptr last;
5657 symindex i;
5658 carsym *symdef;
5659 carsym *symdefend;
5660
5661 loop = FALSE;
5662 last = -1;
5663
5664 symdef = symdefs;
5665 symdefend = symdef + c;
5666 for (i = 0; symdef < symdefend; symdef++, i++)
5667 {
5668 struct elf_link_hash_entry *h;
5669 bfd *element;
5670 struct bfd_link_hash_entry *undefs_tail;
5671 symindex mark;
5672
13e570f8 5673 if (included[i])
0ad989f9
L
5674 continue;
5675 if (symdef->file_offset == last)
5676 {
5677 included[i] = TRUE;
5678 continue;
5679 }
5680
8387904d 5681 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5682 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5683 goto error_return;
0ad989f9
L
5684
5685 if (h == NULL)
5686 continue;
5687
5688 if (h->root.type == bfd_link_hash_common)
5689 {
5690 /* We currently have a common symbol. The archive map contains
5691 a reference to this symbol, so we may want to include it. We
5692 only want to include it however, if this archive element
5693 contains a definition of the symbol, not just another common
5694 declaration of it.
5695
5696 Unfortunately some archivers (including GNU ar) will put
5697 declarations of common symbols into their archive maps, as
5698 well as real definitions, so we cannot just go by the archive
5699 map alone. Instead we must read in the element's symbol
5700 table and check that to see what kind of symbol definition
5701 this is. */
5702 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5703 continue;
5704 }
5705 else if (h->root.type != bfd_link_hash_undefined)
5706 {
5707 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5708 /* Symbol must be defined. Don't check it again. */
5709 included[i] = TRUE;
0ad989f9
L
5710 continue;
5711 }
5712
5713 /* We need to include this archive member. */
5714 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5715 if (element == NULL)
5716 goto error_return;
5717
5718 if (! bfd_check_format (element, bfd_object))
5719 goto error_return;
5720
0ad989f9
L
5721 undefs_tail = info->hash->undefs_tail;
5722
0e144ba7
AM
5723 if (!(*info->callbacks
5724 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5725 continue;
0e144ba7 5726 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5727 goto error_return;
5728
5729 /* If there are any new undefined symbols, we need to make
5730 another pass through the archive in order to see whether
5731 they can be defined. FIXME: This isn't perfect, because
5732 common symbols wind up on undefs_tail and because an
5733 undefined symbol which is defined later on in this pass
5734 does not require another pass. This isn't a bug, but it
5735 does make the code less efficient than it could be. */
5736 if (undefs_tail != info->hash->undefs_tail)
5737 loop = TRUE;
5738
5739 /* Look backward to mark all symbols from this object file
5740 which we have already seen in this pass. */
5741 mark = i;
5742 do
5743 {
5744 included[mark] = TRUE;
5745 if (mark == 0)
5746 break;
5747 --mark;
5748 }
5749 while (symdefs[mark].file_offset == symdef->file_offset);
5750
5751 /* We mark subsequent symbols from this object file as we go
5752 on through the loop. */
5753 last = symdef->file_offset;
5754 }
5755 }
5756 while (loop);
5757
0ad989f9
L
5758 free (included);
5759
5760 return TRUE;
5761
5762 error_return:
0ad989f9
L
5763 if (included != NULL)
5764 free (included);
5765 return FALSE;
5766}
4ad4eba5
AM
5767
5768/* Given an ELF BFD, add symbols to the global hash table as
5769 appropriate. */
5770
5771bfd_boolean
5772bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5773{
5774 switch (bfd_get_format (abfd))
5775 {
5776 case bfd_object:
5777 return elf_link_add_object_symbols (abfd, info);
5778 case bfd_archive:
5779 return elf_link_add_archive_symbols (abfd, info);
5780 default:
5781 bfd_set_error (bfd_error_wrong_format);
5782 return FALSE;
5783 }
5784}
5a580b3a 5785\f
14b1c01e
AM
5786struct hash_codes_info
5787{
5788 unsigned long *hashcodes;
5789 bfd_boolean error;
5790};
a0c8462f 5791
5a580b3a
AM
5792/* This function will be called though elf_link_hash_traverse to store
5793 all hash value of the exported symbols in an array. */
5794
5795static bfd_boolean
5796elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5797{
a50b1753 5798 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5799 const char *name;
5a580b3a
AM
5800 unsigned long ha;
5801 char *alc = NULL;
5802
5a580b3a
AM
5803 /* Ignore indirect symbols. These are added by the versioning code. */
5804 if (h->dynindx == -1)
5805 return TRUE;
5806
5807 name = h->root.root.string;
422f1182 5808 if (h->versioned >= versioned)
5a580b3a 5809 {
422f1182
L
5810 char *p = strchr (name, ELF_VER_CHR);
5811 if (p != NULL)
14b1c01e 5812 {
422f1182
L
5813 alc = (char *) bfd_malloc (p - name + 1);
5814 if (alc == NULL)
5815 {
5816 inf->error = TRUE;
5817 return FALSE;
5818 }
5819 memcpy (alc, name, p - name);
5820 alc[p - name] = '\0';
5821 name = alc;
14b1c01e 5822 }
5a580b3a
AM
5823 }
5824
5825 /* Compute the hash value. */
5826 ha = bfd_elf_hash (name);
5827
5828 /* Store the found hash value in the array given as the argument. */
14b1c01e 5829 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5830
5831 /* And store it in the struct so that we can put it in the hash table
5832 later. */
f6e332e6 5833 h->u.elf_hash_value = ha;
5a580b3a
AM
5834
5835 if (alc != NULL)
5836 free (alc);
5837
5838 return TRUE;
5839}
5840
fdc90cb4
JJ
5841struct collect_gnu_hash_codes
5842{
5843 bfd *output_bfd;
5844 const struct elf_backend_data *bed;
5845 unsigned long int nsyms;
5846 unsigned long int maskbits;
5847 unsigned long int *hashcodes;
5848 unsigned long int *hashval;
5849 unsigned long int *indx;
5850 unsigned long int *counts;
5851 bfd_vma *bitmask;
5852 bfd_byte *contents;
f16a9783 5853 bfd_size_type xlat;
fdc90cb4
JJ
5854 long int min_dynindx;
5855 unsigned long int bucketcount;
5856 unsigned long int symindx;
5857 long int local_indx;
5858 long int shift1, shift2;
5859 unsigned long int mask;
14b1c01e 5860 bfd_boolean error;
fdc90cb4
JJ
5861};
5862
5863/* This function will be called though elf_link_hash_traverse to store
5864 all hash value of the exported symbols in an array. */
5865
5866static bfd_boolean
5867elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5868{
a50b1753 5869 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5870 const char *name;
fdc90cb4
JJ
5871 unsigned long ha;
5872 char *alc = NULL;
5873
fdc90cb4
JJ
5874 /* Ignore indirect symbols. These are added by the versioning code. */
5875 if (h->dynindx == -1)
5876 return TRUE;
5877
5878 /* Ignore also local symbols and undefined symbols. */
5879 if (! (*s->bed->elf_hash_symbol) (h))
5880 return TRUE;
5881
5882 name = h->root.root.string;
422f1182 5883 if (h->versioned >= versioned)
fdc90cb4 5884 {
422f1182
L
5885 char *p = strchr (name, ELF_VER_CHR);
5886 if (p != NULL)
14b1c01e 5887 {
422f1182
L
5888 alc = (char *) bfd_malloc (p - name + 1);
5889 if (alc == NULL)
5890 {
5891 s->error = TRUE;
5892 return FALSE;
5893 }
5894 memcpy (alc, name, p - name);
5895 alc[p - name] = '\0';
5896 name = alc;
14b1c01e 5897 }
fdc90cb4
JJ
5898 }
5899
5900 /* Compute the hash value. */
5901 ha = bfd_elf_gnu_hash (name);
5902
5903 /* Store the found hash value in the array for compute_bucket_count,
5904 and also for .dynsym reordering purposes. */
5905 s->hashcodes[s->nsyms] = ha;
5906 s->hashval[h->dynindx] = ha;
5907 ++s->nsyms;
5908 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5909 s->min_dynindx = h->dynindx;
5910
5911 if (alc != NULL)
5912 free (alc);
5913
5914 return TRUE;
5915}
5916
5917/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
5918 final dynamic symbol renumbering in case of .gnu.hash.
5919 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5920 to the translation table. */
fdc90cb4
JJ
5921
5922static bfd_boolean
f16a9783 5923elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 5924{
a50b1753 5925 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5926 unsigned long int bucket;
5927 unsigned long int val;
5928
fdc90cb4
JJ
5929 /* Ignore indirect symbols. */
5930 if (h->dynindx == -1)
5931 return TRUE;
5932
5933 /* Ignore also local symbols and undefined symbols. */
5934 if (! (*s->bed->elf_hash_symbol) (h))
5935 {
5936 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
5937 {
5938 if (s->bed->record_xhash_symbol != NULL)
5939 {
5940 (*s->bed->record_xhash_symbol) (h, 0);
5941 s->local_indx++;
5942 }
5943 else
5944 h->dynindx = s->local_indx++;
5945 }
fdc90cb4
JJ
5946 return TRUE;
5947 }
5948
5949 bucket = s->hashval[h->dynindx] % s->bucketcount;
5950 val = (s->hashval[h->dynindx] >> s->shift1)
5951 & ((s->maskbits >> s->shift1) - 1);
5952 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5953 s->bitmask[val]
5954 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5955 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5956 if (s->counts[bucket] == 1)
5957 /* Last element terminates the chain. */
5958 val |= 1;
5959 bfd_put_32 (s->output_bfd, val,
5960 s->contents + (s->indx[bucket] - s->symindx) * 4);
5961 --s->counts[bucket];
f16a9783
MS
5962 if (s->bed->record_xhash_symbol != NULL)
5963 {
5964 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5965
5966 (*s->bed->record_xhash_symbol) (h, xlat_loc);
5967 }
5968 else
5969 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
5970 return TRUE;
5971}
5972
5973/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5974
5975bfd_boolean
5976_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5977{
5978 return !(h->forced_local
5979 || h->root.type == bfd_link_hash_undefined
5980 || h->root.type == bfd_link_hash_undefweak
5981 || ((h->root.type == bfd_link_hash_defined
5982 || h->root.type == bfd_link_hash_defweak)
5983 && h->root.u.def.section->output_section == NULL));
5984}
5985
5a580b3a
AM
5986/* Array used to determine the number of hash table buckets to use
5987 based on the number of symbols there are. If there are fewer than
5988 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5989 fewer than 37 we use 17 buckets, and so forth. We never use more
5990 than 32771 buckets. */
5991
5992static const size_t elf_buckets[] =
5993{
5994 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5995 16411, 32771, 0
5996};
5997
5998/* Compute bucket count for hashing table. We do not use a static set
5999 of possible tables sizes anymore. Instead we determine for all
6000 possible reasonable sizes of the table the outcome (i.e., the
6001 number of collisions etc) and choose the best solution. The
6002 weighting functions are not too simple to allow the table to grow
6003 without bounds. Instead one of the weighting factors is the size.
6004 Therefore the result is always a good payoff between few collisions
6005 (= short chain lengths) and table size. */
6006static size_t
b20dd2ce 6007compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6008 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6009 unsigned long int nsyms,
6010 int gnu_hash)
5a580b3a 6011{
5a580b3a 6012 size_t best_size = 0;
5a580b3a 6013 unsigned long int i;
5a580b3a 6014
5a580b3a
AM
6015 /* We have a problem here. The following code to optimize the table
6016 size requires an integer type with more the 32 bits. If
6017 BFD_HOST_U_64_BIT is set we know about such a type. */
6018#ifdef BFD_HOST_U_64_BIT
6019 if (info->optimize)
6020 {
5a580b3a
AM
6021 size_t minsize;
6022 size_t maxsize;
6023 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6024 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6025 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6026 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6027 unsigned long int *counts;
d40f3da9 6028 bfd_size_type amt;
0883b6e0 6029 unsigned int no_improvement_count = 0;
5a580b3a
AM
6030
6031 /* Possible optimization parameters: if we have NSYMS symbols we say
6032 that the hashing table must at least have NSYMS/4 and at most
6033 2*NSYMS buckets. */
6034 minsize = nsyms / 4;
6035 if (minsize == 0)
6036 minsize = 1;
6037 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6038 if (gnu_hash)
6039 {
6040 if (minsize < 2)
6041 minsize = 2;
6042 if ((best_size & 31) == 0)
6043 ++best_size;
6044 }
5a580b3a
AM
6045
6046 /* Create array where we count the collisions in. We must use bfd_malloc
6047 since the size could be large. */
6048 amt = maxsize;
6049 amt *= sizeof (unsigned long int);
a50b1753 6050 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6051 if (counts == NULL)
fdc90cb4 6052 return 0;
5a580b3a
AM
6053
6054 /* Compute the "optimal" size for the hash table. The criteria is a
6055 minimal chain length. The minor criteria is (of course) the size
6056 of the table. */
6057 for (i = minsize; i < maxsize; ++i)
6058 {
6059 /* Walk through the array of hashcodes and count the collisions. */
6060 BFD_HOST_U_64_BIT max;
6061 unsigned long int j;
6062 unsigned long int fact;
6063
fdc90cb4
JJ
6064 if (gnu_hash && (i & 31) == 0)
6065 continue;
6066
5a580b3a
AM
6067 memset (counts, '\0', i * sizeof (unsigned long int));
6068
6069 /* Determine how often each hash bucket is used. */
6070 for (j = 0; j < nsyms; ++j)
6071 ++counts[hashcodes[j] % i];
6072
6073 /* For the weight function we need some information about the
6074 pagesize on the target. This is information need not be 100%
6075 accurate. Since this information is not available (so far) we
6076 define it here to a reasonable default value. If it is crucial
6077 to have a better value some day simply define this value. */
6078# ifndef BFD_TARGET_PAGESIZE
6079# define BFD_TARGET_PAGESIZE (4096)
6080# endif
6081
fdc90cb4
JJ
6082 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6083 and the chains. */
6084 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6085
6086# if 1
6087 /* Variant 1: optimize for short chains. We add the squares
6088 of all the chain lengths (which favors many small chain
6089 over a few long chains). */
6090 for (j = 0; j < i; ++j)
6091 max += counts[j] * counts[j];
6092
6093 /* This adds penalties for the overall size of the table. */
fdc90cb4 6094 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6095 max *= fact * fact;
6096# else
6097 /* Variant 2: Optimize a lot more for small table. Here we
6098 also add squares of the size but we also add penalties for
6099 empty slots (the +1 term). */
6100 for (j = 0; j < i; ++j)
6101 max += (1 + counts[j]) * (1 + counts[j]);
6102
6103 /* The overall size of the table is considered, but not as
6104 strong as in variant 1, where it is squared. */
fdc90cb4 6105 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6106 max *= fact;
6107# endif
6108
6109 /* Compare with current best results. */
6110 if (max < best_chlen)
6111 {
6112 best_chlen = max;
6113 best_size = i;
ca4be51c 6114 no_improvement_count = 0;
5a580b3a 6115 }
0883b6e0
NC
6116 /* PR 11843: Avoid futile long searches for the best bucket size
6117 when there are a large number of symbols. */
6118 else if (++no_improvement_count == 100)
6119 break;
5a580b3a
AM
6120 }
6121
6122 free (counts);
6123 }
6124 else
6125#endif /* defined (BFD_HOST_U_64_BIT) */
6126 {
6127 /* This is the fallback solution if no 64bit type is available or if we
6128 are not supposed to spend much time on optimizations. We select the
6129 bucket count using a fixed set of numbers. */
6130 for (i = 0; elf_buckets[i] != 0; i++)
6131 {
6132 best_size = elf_buckets[i];
fdc90cb4 6133 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6134 break;
6135 }
fdc90cb4
JJ
6136 if (gnu_hash && best_size < 2)
6137 best_size = 2;
5a580b3a
AM
6138 }
6139
5a580b3a
AM
6140 return best_size;
6141}
6142
d0bf826b
AM
6143/* Size any SHT_GROUP section for ld -r. */
6144
6145bfd_boolean
6146_bfd_elf_size_group_sections (struct bfd_link_info *info)
6147{
6148 bfd *ibfd;
57963c05 6149 asection *s;
d0bf826b 6150
c72f2fb2 6151 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6152 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6153 && (s = ibfd->sections) != NULL
6154 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6155 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6156 return FALSE;
6157 return TRUE;
6158}
6159
04c3a755
NS
6160/* Set a default stack segment size. The value in INFO wins. If it
6161 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6162 undefined it is initialized. */
6163
6164bfd_boolean
6165bfd_elf_stack_segment_size (bfd *output_bfd,
6166 struct bfd_link_info *info,
6167 const char *legacy_symbol,
6168 bfd_vma default_size)
6169{
6170 struct elf_link_hash_entry *h = NULL;
6171
6172 /* Look for legacy symbol. */
6173 if (legacy_symbol)
6174 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6175 FALSE, FALSE, FALSE);
6176 if (h && (h->root.type == bfd_link_hash_defined
6177 || h->root.type == bfd_link_hash_defweak)
6178 && h->def_regular
6179 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6180 {
6181 /* The symbol has no type if specified on the command line. */
6182 h->type = STT_OBJECT;
6183 if (info->stacksize)
695344c0 6184 /* xgettext:c-format */
871b3ab2 6185 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6186 output_bfd, legacy_symbol);
04c3a755 6187 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6188 /* xgettext:c-format */
871b3ab2 6189 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6190 output_bfd, legacy_symbol);
04c3a755
NS
6191 else
6192 info->stacksize = h->root.u.def.value;
6193 }
6194
6195 if (!info->stacksize)
6196 /* If the user didn't set a size, or explicitly inhibit the
6197 size, set it now. */
6198 info->stacksize = default_size;
6199
6200 /* Provide the legacy symbol, if it is referenced. */
6201 if (h && (h->root.type == bfd_link_hash_undefined
6202 || h->root.type == bfd_link_hash_undefweak))
6203 {
6204 struct bfd_link_hash_entry *bh = NULL;
6205
6206 if (!(_bfd_generic_link_add_one_symbol
6207 (info, output_bfd, legacy_symbol,
6208 BSF_GLOBAL, bfd_abs_section_ptr,
6209 info->stacksize >= 0 ? info->stacksize : 0,
6210 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6211 return FALSE;
6212
6213 h = (struct elf_link_hash_entry *) bh;
6214 h->def_regular = 1;
6215 h->type = STT_OBJECT;
6216 }
6217
6218 return TRUE;
6219}
6220
b531344c
MR
6221/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6222
6223struct elf_gc_sweep_symbol_info
6224{
6225 struct bfd_link_info *info;
6226 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6227 bfd_boolean);
6228};
6229
6230static bfd_boolean
6231elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6232{
6233 if (!h->mark
6234 && (((h->root.type == bfd_link_hash_defined
6235 || h->root.type == bfd_link_hash_defweak)
6236 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6237 && h->root.u.def.section->gc_mark))
6238 || h->root.type == bfd_link_hash_undefined
6239 || h->root.type == bfd_link_hash_undefweak))
6240 {
6241 struct elf_gc_sweep_symbol_info *inf;
6242
6243 inf = (struct elf_gc_sweep_symbol_info *) data;
6244 (*inf->hide_symbol) (inf->info, h, TRUE);
6245 h->def_regular = 0;
6246 h->ref_regular = 0;
6247 h->ref_regular_nonweak = 0;
6248 }
6249
6250 return TRUE;
6251}
6252
5a580b3a
AM
6253/* Set up the sizes and contents of the ELF dynamic sections. This is
6254 called by the ELF linker emulation before_allocation routine. We
6255 must set the sizes of the sections before the linker sets the
6256 addresses of the various sections. */
6257
6258bfd_boolean
6259bfd_elf_size_dynamic_sections (bfd *output_bfd,
6260 const char *soname,
6261 const char *rpath,
6262 const char *filter_shlib,
7ee314fa
AM
6263 const char *audit,
6264 const char *depaudit,
5a580b3a
AM
6265 const char * const *auxiliary_filters,
6266 struct bfd_link_info *info,
fd91d419 6267 asection **sinterpptr)
5a580b3a 6268{
5a580b3a
AM
6269 bfd *dynobj;
6270 const struct elf_backend_data *bed;
5a580b3a
AM
6271
6272 *sinterpptr = NULL;
6273
5a580b3a
AM
6274 if (!is_elf_hash_table (info->hash))
6275 return TRUE;
6276
5a580b3a
AM
6277 dynobj = elf_hash_table (info)->dynobj;
6278
9a2a56cc 6279 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6280 {
902e9fc7
MR
6281 struct bfd_elf_version_tree *verdefs;
6282 struct elf_info_failed asvinfo;
5a580b3a
AM
6283 struct bfd_elf_version_tree *t;
6284 struct bfd_elf_version_expr *d;
902e9fc7 6285 asection *s;
e6699019 6286 size_t soname_indx;
7ee314fa 6287
5a580b3a
AM
6288 /* If we are supposed to export all symbols into the dynamic symbol
6289 table (this is not the normal case), then do so. */
55255dae 6290 if (info->export_dynamic
0e1862bb 6291 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6292 {
3d13f3e9
AM
6293 struct elf_info_failed eif;
6294
6295 eif.info = info;
6296 eif.failed = FALSE;
5a580b3a
AM
6297 elf_link_hash_traverse (elf_hash_table (info),
6298 _bfd_elf_export_symbol,
6299 &eif);
6300 if (eif.failed)
6301 return FALSE;
6302 }
6303
e6699019
L
6304 if (soname != NULL)
6305 {
6306 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6307 soname, TRUE);
6308 if (soname_indx == (size_t) -1
6309 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6310 return FALSE;
6311 }
6312 else
6313 soname_indx = (size_t) -1;
6314
5a580b3a 6315 /* Make all global versions with definition. */
fd91d419 6316 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6317 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6318 if (!d->symver && d->literal)
5a580b3a
AM
6319 {
6320 const char *verstr, *name;
6321 size_t namelen, verlen, newlen;
93252b1c 6322 char *newname, *p, leading_char;
5a580b3a
AM
6323 struct elf_link_hash_entry *newh;
6324
93252b1c 6325 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6326 name = d->pattern;
93252b1c 6327 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6328 verstr = t->name;
6329 verlen = strlen (verstr);
6330 newlen = namelen + verlen + 3;
6331
a50b1753 6332 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6333 if (newname == NULL)
6334 return FALSE;
93252b1c
MF
6335 newname[0] = leading_char;
6336 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6337
6338 /* Check the hidden versioned definition. */
6339 p = newname + namelen;
6340 *p++ = ELF_VER_CHR;
6341 memcpy (p, verstr, verlen + 1);
6342 newh = elf_link_hash_lookup (elf_hash_table (info),
6343 newname, FALSE, FALSE,
6344 FALSE);
6345 if (newh == NULL
6346 || (newh->root.type != bfd_link_hash_defined
6347 && newh->root.type != bfd_link_hash_defweak))
6348 {
6349 /* Check the default versioned definition. */
6350 *p++ = ELF_VER_CHR;
6351 memcpy (p, verstr, verlen + 1);
6352 newh = elf_link_hash_lookup (elf_hash_table (info),
6353 newname, FALSE, FALSE,
6354 FALSE);
6355 }
6356 free (newname);
6357
6358 /* Mark this version if there is a definition and it is
6359 not defined in a shared object. */
6360 if (newh != NULL
f5385ebf 6361 && !newh->def_dynamic
5a580b3a
AM
6362 && (newh->root.type == bfd_link_hash_defined
6363 || newh->root.type == bfd_link_hash_defweak))
6364 d->symver = 1;
6365 }
6366
6367 /* Attach all the symbols to their version information. */
5a580b3a 6368 asvinfo.info = info;
5a580b3a
AM
6369 asvinfo.failed = FALSE;
6370
6371 elf_link_hash_traverse (elf_hash_table (info),
6372 _bfd_elf_link_assign_sym_version,
6373 &asvinfo);
6374 if (asvinfo.failed)
6375 return FALSE;
6376
6377 if (!info->allow_undefined_version)
6378 {
6379 /* Check if all global versions have a definition. */
3d13f3e9 6380 bfd_boolean all_defined = TRUE;
fd91d419 6381 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6382 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6383 if (d->literal && !d->symver && !d->script)
5a580b3a 6384 {
4eca0228 6385 _bfd_error_handler
5a580b3a
AM
6386 (_("%s: undefined version: %s"),
6387 d->pattern, t->name);
6388 all_defined = FALSE;
6389 }
6390
6391 if (!all_defined)
6392 {
6393 bfd_set_error (bfd_error_bad_value);
6394 return FALSE;
6395 }
6396 }
6397
902e9fc7
MR
6398 /* Set up the version definition section. */
6399 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6400 BFD_ASSERT (s != NULL);
5a580b3a 6401
902e9fc7
MR
6402 /* We may have created additional version definitions if we are
6403 just linking a regular application. */
6404 verdefs = info->version_info;
5a580b3a 6405
902e9fc7
MR
6406 /* Skip anonymous version tag. */
6407 if (verdefs != NULL && verdefs->vernum == 0)
6408 verdefs = verdefs->next;
5a580b3a 6409
902e9fc7
MR
6410 if (verdefs == NULL && !info->create_default_symver)
6411 s->flags |= SEC_EXCLUDE;
6412 else
5a580b3a 6413 {
902e9fc7
MR
6414 unsigned int cdefs;
6415 bfd_size_type size;
6416 bfd_byte *p;
6417 Elf_Internal_Verdef def;
6418 Elf_Internal_Verdaux defaux;
6419 struct bfd_link_hash_entry *bh;
6420 struct elf_link_hash_entry *h;
6421 const char *name;
5a580b3a 6422
902e9fc7
MR
6423 cdefs = 0;
6424 size = 0;
5a580b3a 6425
902e9fc7
MR
6426 /* Make space for the base version. */
6427 size += sizeof (Elf_External_Verdef);
6428 size += sizeof (Elf_External_Verdaux);
6429 ++cdefs;
6430
6431 /* Make space for the default version. */
6432 if (info->create_default_symver)
6433 {
6434 size += sizeof (Elf_External_Verdef);
6435 ++cdefs;
3e3b46e5
PB
6436 }
6437
5a580b3a
AM
6438 for (t = verdefs; t != NULL; t = t->next)
6439 {
6440 struct bfd_elf_version_deps *n;
6441
a6cc6b3b
RO
6442 /* Don't emit base version twice. */
6443 if (t->vernum == 0)
6444 continue;
6445
5a580b3a
AM
6446 size += sizeof (Elf_External_Verdef);
6447 size += sizeof (Elf_External_Verdaux);
6448 ++cdefs;
6449
6450 for (n = t->deps; n != NULL; n = n->next)
6451 size += sizeof (Elf_External_Verdaux);
6452 }
6453
eea6121a 6454 s->size = size;
a50b1753 6455 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6456 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6457 return FALSE;
6458
6459 /* Fill in the version definition section. */
6460
6461 p = s->contents;
6462
6463 def.vd_version = VER_DEF_CURRENT;
6464 def.vd_flags = VER_FLG_BASE;
6465 def.vd_ndx = 1;
6466 def.vd_cnt = 1;
3e3b46e5
PB
6467 if (info->create_default_symver)
6468 {
6469 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6470 def.vd_next = sizeof (Elf_External_Verdef);
6471 }
6472 else
6473 {
6474 def.vd_aux = sizeof (Elf_External_Verdef);
6475 def.vd_next = (sizeof (Elf_External_Verdef)
6476 + sizeof (Elf_External_Verdaux));
6477 }
5a580b3a 6478
ef53be89 6479 if (soname_indx != (size_t) -1)
5a580b3a
AM
6480 {
6481 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6482 soname_indx);
6483 def.vd_hash = bfd_elf_hash (soname);
6484 defaux.vda_name = soname_indx;
3e3b46e5 6485 name = soname;
5a580b3a
AM
6486 }
6487 else
6488 {
ef53be89 6489 size_t indx;
5a580b3a 6490
06084812 6491 name = lbasename (output_bfd->filename);
5a580b3a
AM
6492 def.vd_hash = bfd_elf_hash (name);
6493 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6494 name, FALSE);
ef53be89 6495 if (indx == (size_t) -1)
5a580b3a
AM
6496 return FALSE;
6497 defaux.vda_name = indx;
6498 }
6499 defaux.vda_next = 0;
6500
6501 _bfd_elf_swap_verdef_out (output_bfd, &def,
6502 (Elf_External_Verdef *) p);
6503 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6504 if (info->create_default_symver)
6505 {
6506 /* Add a symbol representing this version. */
6507 bh = NULL;
6508 if (! (_bfd_generic_link_add_one_symbol
6509 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6510 0, NULL, FALSE,
6511 get_elf_backend_data (dynobj)->collect, &bh)))
6512 return FALSE;
6513 h = (struct elf_link_hash_entry *) bh;
6514 h->non_elf = 0;
6515 h->def_regular = 1;
6516 h->type = STT_OBJECT;
6517 h->verinfo.vertree = NULL;
6518
6519 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6520 return FALSE;
6521
6522 /* Create a duplicate of the base version with the same
6523 aux block, but different flags. */
6524 def.vd_flags = 0;
6525 def.vd_ndx = 2;
6526 def.vd_aux = sizeof (Elf_External_Verdef);
6527 if (verdefs)
6528 def.vd_next = (sizeof (Elf_External_Verdef)
6529 + sizeof (Elf_External_Verdaux));
6530 else
6531 def.vd_next = 0;
6532 _bfd_elf_swap_verdef_out (output_bfd, &def,
6533 (Elf_External_Verdef *) p);
6534 p += sizeof (Elf_External_Verdef);
6535 }
5a580b3a
AM
6536 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6537 (Elf_External_Verdaux *) p);
6538 p += sizeof (Elf_External_Verdaux);
6539
6540 for (t = verdefs; t != NULL; t = t->next)
6541 {
6542 unsigned int cdeps;
6543 struct bfd_elf_version_deps *n;
5a580b3a 6544
a6cc6b3b
RO
6545 /* Don't emit the base version twice. */
6546 if (t->vernum == 0)
6547 continue;
6548
5a580b3a
AM
6549 cdeps = 0;
6550 for (n = t->deps; n != NULL; n = n->next)
6551 ++cdeps;
6552
6553 /* Add a symbol representing this version. */
6554 bh = NULL;
6555 if (! (_bfd_generic_link_add_one_symbol
6556 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6557 0, NULL, FALSE,
6558 get_elf_backend_data (dynobj)->collect, &bh)))
6559 return FALSE;
6560 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6561 h->non_elf = 0;
6562 h->def_regular = 1;
5a580b3a
AM
6563 h->type = STT_OBJECT;
6564 h->verinfo.vertree = t;
6565
c152c796 6566 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6567 return FALSE;
6568
6569 def.vd_version = VER_DEF_CURRENT;
6570 def.vd_flags = 0;
6571 if (t->globals.list == NULL
6572 && t->locals.list == NULL
6573 && ! t->used)
6574 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6575 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6576 def.vd_cnt = cdeps + 1;
6577 def.vd_hash = bfd_elf_hash (t->name);
6578 def.vd_aux = sizeof (Elf_External_Verdef);
6579 def.vd_next = 0;
a6cc6b3b
RO
6580
6581 /* If a basever node is next, it *must* be the last node in
6582 the chain, otherwise Verdef construction breaks. */
6583 if (t->next != NULL && t->next->vernum == 0)
6584 BFD_ASSERT (t->next->next == NULL);
6585
6586 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6587 def.vd_next = (sizeof (Elf_External_Verdef)
6588 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6589
6590 _bfd_elf_swap_verdef_out (output_bfd, &def,
6591 (Elf_External_Verdef *) p);
6592 p += sizeof (Elf_External_Verdef);
6593
6594 defaux.vda_name = h->dynstr_index;
6595 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6596 h->dynstr_index);
6597 defaux.vda_next = 0;
6598 if (t->deps != NULL)
6599 defaux.vda_next = sizeof (Elf_External_Verdaux);
6600 t->name_indx = defaux.vda_name;
6601
6602 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6603 (Elf_External_Verdaux *) p);
6604 p += sizeof (Elf_External_Verdaux);
6605
6606 for (n = t->deps; n != NULL; n = n->next)
6607 {
6608 if (n->version_needed == NULL)
6609 {
6610 /* This can happen if there was an error in the
6611 version script. */
6612 defaux.vda_name = 0;
6613 }
6614 else
6615 {
6616 defaux.vda_name = n->version_needed->name_indx;
6617 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6618 defaux.vda_name);
6619 }
6620 if (n->next == NULL)
6621 defaux.vda_next = 0;
6622 else
6623 defaux.vda_next = sizeof (Elf_External_Verdaux);
6624
6625 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6626 (Elf_External_Verdaux *) p);
6627 p += sizeof (Elf_External_Verdaux);
6628 }
6629 }
6630
5a580b3a
AM
6631 elf_tdata (output_bfd)->cverdefs = cdefs;
6632 }
902e9fc7
MR
6633 }
6634
6635 bed = get_elf_backend_data (output_bfd);
6636
6637 if (info->gc_sections && bed->can_gc_sections)
6638 {
6639 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6640
6641 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6642 dynamic symbol table. */
902e9fc7
MR
6643 sweep_info.info = info;
6644 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6645 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6646 &sweep_info);
3d13f3e9
AM
6647 }
6648
6649 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6650 {
6651 asection *s;
6652 struct elf_find_verdep_info sinfo;
6653
6654 /* Work out the size of the version reference section. */
6655
6656 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6657 BFD_ASSERT (s != NULL);
902e9fc7 6658
3d13f3e9
AM
6659 sinfo.info = info;
6660 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6661 if (sinfo.vers == 0)
6662 sinfo.vers = 1;
6663 sinfo.failed = FALSE;
6664
6665 elf_link_hash_traverse (elf_hash_table (info),
6666 _bfd_elf_link_find_version_dependencies,
6667 &sinfo);
6668 if (sinfo.failed)
6669 return FALSE;
6670
6671 if (elf_tdata (output_bfd)->verref == NULL)
6672 s->flags |= SEC_EXCLUDE;
6673 else
6674 {
6675 Elf_Internal_Verneed *vn;
6676 unsigned int size;
6677 unsigned int crefs;
6678 bfd_byte *p;
6679
6680 /* Build the version dependency section. */
6681 size = 0;
6682 crefs = 0;
6683 for (vn = elf_tdata (output_bfd)->verref;
6684 vn != NULL;
6685 vn = vn->vn_nextref)
6686 {
6687 Elf_Internal_Vernaux *a;
6688
6689 size += sizeof (Elf_External_Verneed);
6690 ++crefs;
6691 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6692 size += sizeof (Elf_External_Vernaux);
6693 }
6694
6695 s->size = size;
6696 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6697 if (s->contents == NULL)
6698 return FALSE;
6699
6700 p = s->contents;
6701 for (vn = elf_tdata (output_bfd)->verref;
6702 vn != NULL;
6703 vn = vn->vn_nextref)
6704 {
6705 unsigned int caux;
6706 Elf_Internal_Vernaux *a;
6707 size_t indx;
6708
6709 caux = 0;
6710 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6711 ++caux;
6712
6713 vn->vn_version = VER_NEED_CURRENT;
6714 vn->vn_cnt = caux;
6715 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6716 elf_dt_name (vn->vn_bfd) != NULL
6717 ? elf_dt_name (vn->vn_bfd)
6718 : lbasename (vn->vn_bfd->filename),
6719 FALSE);
6720 if (indx == (size_t) -1)
6721 return FALSE;
6722 vn->vn_file = indx;
6723 vn->vn_aux = sizeof (Elf_External_Verneed);
6724 if (vn->vn_nextref == NULL)
6725 vn->vn_next = 0;
6726 else
6727 vn->vn_next = (sizeof (Elf_External_Verneed)
6728 + caux * sizeof (Elf_External_Vernaux));
6729
6730 _bfd_elf_swap_verneed_out (output_bfd, vn,
6731 (Elf_External_Verneed *) p);
6732 p += sizeof (Elf_External_Verneed);
6733
6734 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6735 {
6736 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6737 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6738 a->vna_nodename, FALSE);
6739 if (indx == (size_t) -1)
6740 return FALSE;
6741 a->vna_name = indx;
6742 if (a->vna_nextptr == NULL)
6743 a->vna_next = 0;
6744 else
6745 a->vna_next = sizeof (Elf_External_Vernaux);
6746
6747 _bfd_elf_swap_vernaux_out (output_bfd, a,
6748 (Elf_External_Vernaux *) p);
6749 p += sizeof (Elf_External_Vernaux);
6750 }
6751 }
6752
6753 elf_tdata (output_bfd)->cverrefs = crefs;
6754 }
902e9fc7
MR
6755 }
6756
6757 /* Any syms created from now on start with -1 in
6758 got.refcount/offset and plt.refcount/offset. */
6759 elf_hash_table (info)->init_got_refcount
6760 = elf_hash_table (info)->init_got_offset;
6761 elf_hash_table (info)->init_plt_refcount
6762 = elf_hash_table (info)->init_plt_offset;
6763
6764 if (bfd_link_relocatable (info)
6765 && !_bfd_elf_size_group_sections (info))
6766 return FALSE;
6767
6768 /* The backend may have to create some sections regardless of whether
6769 we're dynamic or not. */
6770 if (bed->elf_backend_always_size_sections
6771 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6772 return FALSE;
6773
6774 /* Determine any GNU_STACK segment requirements, after the backend
6775 has had a chance to set a default segment size. */
6776 if (info->execstack)
6777 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6778 else if (info->noexecstack)
6779 elf_stack_flags (output_bfd) = PF_R | PF_W;
6780 else
6781 {
6782 bfd *inputobj;
6783 asection *notesec = NULL;
6784 int exec = 0;
6785
6786 for (inputobj = info->input_bfds;
6787 inputobj;
6788 inputobj = inputobj->link.next)
6789 {
6790 asection *s;
6791
6792 if (inputobj->flags
6793 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6794 continue;
57963c05
AM
6795 s = inputobj->sections;
6796 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6797 continue;
6798
902e9fc7
MR
6799 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6800 if (s)
6801 {
6802 if (s->flags & SEC_CODE)
6803 exec = PF_X;
6804 notesec = s;
6805 }
6806 else if (bed->default_execstack)
6807 exec = PF_X;
6808 }
6809 if (notesec || info->stacksize > 0)
6810 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6811 if (notesec && exec && bfd_link_relocatable (info)
6812 && notesec->output_section != bfd_abs_section_ptr)
6813 notesec->output_section->flags |= SEC_CODE;
6814 }
6815
6816 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6817 {
6818 struct elf_info_failed eif;
6819 struct elf_link_hash_entry *h;
6820 asection *dynstr;
6821 asection *s;
6822
6823 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6824 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6825
902e9fc7
MR
6826 if (info->symbolic)
6827 {
6828 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6829 return FALSE;
6830 info->flags |= DF_SYMBOLIC;
6831 }
6832
6833 if (rpath != NULL)
6834 {
6835 size_t indx;
6836 bfd_vma tag;
6837
6838 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6839 TRUE);
6840 if (indx == (size_t) -1)
6841 return FALSE;
6842
6843 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6844 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6845 return FALSE;
6846 }
6847
6848 if (filter_shlib != NULL)
6849 {
6850 size_t indx;
6851
6852 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6853 filter_shlib, TRUE);
6854 if (indx == (size_t) -1
6855 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6856 return FALSE;
6857 }
6858
6859 if (auxiliary_filters != NULL)
6860 {
6861 const char * const *p;
6862
6863 for (p = auxiliary_filters; *p != NULL; p++)
6864 {
6865 size_t indx;
6866
6867 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6868 *p, TRUE);
6869 if (indx == (size_t) -1
6870 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6871 return FALSE;
6872 }
6873 }
6874
6875 if (audit != NULL)
6876 {
6877 size_t indx;
6878
6879 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6880 TRUE);
6881 if (indx == (size_t) -1
6882 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6883 return FALSE;
6884 }
6885
6886 if (depaudit != NULL)
6887 {
6888 size_t indx;
6889
6890 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6891 TRUE);
6892 if (indx == (size_t) -1
6893 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6894 return FALSE;
6895 }
6896
6897 eif.info = info;
6898 eif.failed = FALSE;
6899
6900 /* Find all symbols which were defined in a dynamic object and make
6901 the backend pick a reasonable value for them. */
6902 elf_link_hash_traverse (elf_hash_table (info),
6903 _bfd_elf_adjust_dynamic_symbol,
6904 &eif);
6905 if (eif.failed)
6906 return FALSE;
6907
6908 /* Add some entries to the .dynamic section. We fill in some of the
6909 values later, in bfd_elf_final_link, but we must add the entries
6910 now so that we know the final size of the .dynamic section. */
6911
6912 /* If there are initialization and/or finalization functions to
6913 call then add the corresponding DT_INIT/DT_FINI entries. */
6914 h = (info->init_function
6915 ? elf_link_hash_lookup (elf_hash_table (info),
6916 info->init_function, FALSE,
6917 FALSE, FALSE)
6918 : NULL);
6919 if (h != NULL
6920 && (h->ref_regular
6921 || h->def_regular))
6922 {
6923 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6924 return FALSE;
6925 }
6926 h = (info->fini_function
6927 ? elf_link_hash_lookup (elf_hash_table (info),
6928 info->fini_function, FALSE,
6929 FALSE, FALSE)
6930 : NULL);
6931 if (h != NULL
6932 && (h->ref_regular
6933 || h->def_regular))
6934 {
6935 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6936 return FALSE;
6937 }
6938
6939 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6940 if (s != NULL && s->linker_has_input)
6941 {
6942 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6943 if (! bfd_link_executable (info))
6944 {
6945 bfd *sub;
6946 asection *o;
6947
57963c05
AM
6948 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6949 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6950 && (o = sub->sections) != NULL
6951 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6952 for (o = sub->sections; o != NULL; o = o->next)
6953 if (elf_section_data (o)->this_hdr.sh_type
6954 == SHT_PREINIT_ARRAY)
6955 {
6956 _bfd_error_handler
871b3ab2 6957 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6958 sub);
6959 break;
6960 }
6961
6962 bfd_set_error (bfd_error_nonrepresentable_section);
6963 return FALSE;
6964 }
6965
6966 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6967 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6968 return FALSE;
6969 }
6970 s = bfd_get_section_by_name (output_bfd, ".init_array");
6971 if (s != NULL && s->linker_has_input)
6972 {
6973 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6974 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6975 return FALSE;
6976 }
6977 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6978 if (s != NULL && s->linker_has_input)
6979 {
6980 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6981 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6982 return FALSE;
6983 }
6984
6985 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6986 /* If .dynstr is excluded from the link, we don't want any of
6987 these tags. Strictly, we should be checking each section
6988 individually; This quick check covers for the case where
6989 someone does a /DISCARD/ : { *(*) }. */
6990 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6991 {
6992 bfd_size_type strsize;
6993
6994 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6995 if ((info->emit_hash
6996 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6997 || (info->emit_gnu_hash
f16a9783
MS
6998 && (bed->record_xhash_symbol == NULL
6999 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7000 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7001 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7002 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7003 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7004 bed->s->sizeof_sym))
7005 return FALSE;
7006 }
7007 }
7008
7009 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7010 return FALSE;
7011
7012 /* The backend must work out the sizes of all the other dynamic
7013 sections. */
7014 if (dynobj != NULL
7015 && bed->elf_backend_size_dynamic_sections != NULL
7016 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7017 return FALSE;
7018
7019 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7020 {
902e9fc7
MR
7021 if (elf_tdata (output_bfd)->cverdefs)
7022 {
7023 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7024
7025 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7026 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7027 return FALSE;
7028 }
7029
7030 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7031 {
7032 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7033 return FALSE;
7034 }
7035 else if (info->flags & DF_BIND_NOW)
7036 {
7037 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7038 return FALSE;
7039 }
7040
7041 if (info->flags_1)
7042 {
7043 if (bfd_link_executable (info))
7044 info->flags_1 &= ~ (DF_1_INITFIRST
7045 | DF_1_NODELETE
7046 | DF_1_NOOPEN);
7047 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7048 return FALSE;
7049 }
7050
7051 if (elf_tdata (output_bfd)->cverrefs)
7052 {
7053 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7054
7055 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7056 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7057 return FALSE;
7058 }
5a580b3a 7059
8423293d
AM
7060 if ((elf_tdata (output_bfd)->cverrefs == 0
7061 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7062 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7063 {
902e9fc7
MR
7064 asection *s;
7065
3d4d4302 7066 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7067 s->flags |= SEC_EXCLUDE;
7068 }
7069 }
7070 return TRUE;
7071}
7072
74541ad4
AM
7073/* Find the first non-excluded output section. We'll use its
7074 section symbol for some emitted relocs. */
7075void
7076_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7077{
7078 asection *s;
f26a3287 7079 asection *found = NULL;
74541ad4
AM
7080
7081 for (s = output_bfd->sections; s != NULL; s = s->next)
7082 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7083 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7084 {
f26a3287
AM
7085 found = s;
7086 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7087 break;
74541ad4 7088 }
f26a3287 7089 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7090}
7091
7092/* Find two non-excluded output sections, one for code, one for data.
7093 We'll use their section symbols for some emitted relocs. */
7094void
7095_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7096{
7097 asection *s;
f26a3287 7098 asection *found = NULL;
74541ad4 7099
266b05cf 7100 /* Data first, since setting text_index_section changes
7f923b7f 7101 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7102 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7103 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7104 && !(s->flags & SEC_READONLY)
d00dd7dc 7105 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7106 {
f26a3287
AM
7107 found = s;
7108 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7109 break;
74541ad4 7110 }
f26a3287 7111 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7112
7113 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7114 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7115 && (s->flags & SEC_READONLY)
d00dd7dc 7116 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7117 {
f26a3287 7118 found = s;
74541ad4
AM
7119 break;
7120 }
f26a3287 7121 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7122}
7123
f16a9783
MS
7124#define GNU_HASH_SECTION_NAME(bed) \
7125 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7126
8423293d
AM
7127bfd_boolean
7128bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7129{
74541ad4 7130 const struct elf_backend_data *bed;
23ec1e32 7131 unsigned long section_sym_count;
96d01d93 7132 bfd_size_type dynsymcount = 0;
74541ad4 7133
8423293d
AM
7134 if (!is_elf_hash_table (info->hash))
7135 return TRUE;
7136
74541ad4
AM
7137 bed = get_elf_backend_data (output_bfd);
7138 (*bed->elf_backend_init_index_section) (output_bfd, info);
7139
23ec1e32
MR
7140 /* Assign dynsym indices. In a shared library we generate a section
7141 symbol for each output section, which come first. Next come all
7142 of the back-end allocated local dynamic syms, followed by the rest
7143 of the global symbols.
7144
7145 This is usually not needed for static binaries, however backends
7146 can request to always do it, e.g. the MIPS backend uses dynamic
7147 symbol counts to lay out GOT, which will be produced in the
7148 presence of GOT relocations even in static binaries (holding fixed
7149 data in that case, to satisfy those relocations). */
7150
7151 if (elf_hash_table (info)->dynamic_sections_created
7152 || bed->always_renumber_dynsyms)
7153 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7154 &section_sym_count);
7155
8423293d
AM
7156 if (elf_hash_table (info)->dynamic_sections_created)
7157 {
7158 bfd *dynobj;
8423293d 7159 asection *s;
8423293d
AM
7160 unsigned int dtagcount;
7161
7162 dynobj = elf_hash_table (info)->dynobj;
7163
5a580b3a 7164 /* Work out the size of the symbol version section. */
3d4d4302 7165 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7166 BFD_ASSERT (s != NULL);
d5486c43 7167 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7168 {
eea6121a 7169 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7170 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7171 if (s->contents == NULL)
7172 return FALSE;
7173
7174 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7175 return FALSE;
7176 }
7177
7178 /* Set the size of the .dynsym and .hash sections. We counted
7179 the number of dynamic symbols in elf_link_add_object_symbols.
7180 We will build the contents of .dynsym and .hash when we build
7181 the final symbol table, because until then we do not know the
7182 correct value to give the symbols. We built the .dynstr
7183 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7184 s = elf_hash_table (info)->dynsym;
5a580b3a 7185 BFD_ASSERT (s != NULL);
eea6121a 7186 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7187
d5486c43
L
7188 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7189 if (s->contents == NULL)
7190 return FALSE;
5a580b3a 7191
d5486c43
L
7192 /* The first entry in .dynsym is a dummy symbol. Clear all the
7193 section syms, in case we don't output them all. */
7194 ++section_sym_count;
7195 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7196
fdc90cb4
JJ
7197 elf_hash_table (info)->bucketcount = 0;
7198
5a580b3a
AM
7199 /* Compute the size of the hashing table. As a side effect this
7200 computes the hash values for all the names we export. */
fdc90cb4
JJ
7201 if (info->emit_hash)
7202 {
7203 unsigned long int *hashcodes;
14b1c01e 7204 struct hash_codes_info hashinf;
fdc90cb4
JJ
7205 bfd_size_type amt;
7206 unsigned long int nsyms;
7207 size_t bucketcount;
7208 size_t hash_entry_size;
7209
7210 /* Compute the hash values for all exported symbols. At the same
7211 time store the values in an array so that we could use them for
7212 optimizations. */
7213 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7214 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7215 if (hashcodes == NULL)
7216 return FALSE;
14b1c01e
AM
7217 hashinf.hashcodes = hashcodes;
7218 hashinf.error = FALSE;
5a580b3a 7219
fdc90cb4
JJ
7220 /* Put all hash values in HASHCODES. */
7221 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7222 elf_collect_hash_codes, &hashinf);
7223 if (hashinf.error)
4dd07732
AM
7224 {
7225 free (hashcodes);
7226 return FALSE;
7227 }
5a580b3a 7228
14b1c01e 7229 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7230 bucketcount
7231 = compute_bucket_count (info, hashcodes, nsyms, 0);
7232 free (hashcodes);
7233
4b48e2f6 7234 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7235 return FALSE;
5a580b3a 7236
fdc90cb4
JJ
7237 elf_hash_table (info)->bucketcount = bucketcount;
7238
3d4d4302 7239 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7240 BFD_ASSERT (s != NULL);
7241 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7242 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7243 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7244 if (s->contents == NULL)
7245 return FALSE;
7246
7247 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7248 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7249 s->contents + hash_entry_size);
7250 }
7251
7252 if (info->emit_gnu_hash)
7253 {
7254 size_t i, cnt;
7255 unsigned char *contents;
7256 struct collect_gnu_hash_codes cinfo;
7257 bfd_size_type amt;
7258 size_t bucketcount;
7259
7260 memset (&cinfo, 0, sizeof (cinfo));
7261
7262 /* Compute the hash values for all exported symbols. At the same
7263 time store the values in an array so that we could use them for
7264 optimizations. */
7265 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7266 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7267 if (cinfo.hashcodes == NULL)
7268 return FALSE;
7269
7270 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7271 cinfo.min_dynindx = -1;
7272 cinfo.output_bfd = output_bfd;
7273 cinfo.bed = bed;
7274
7275 /* Put all hash values in HASHCODES. */
7276 elf_link_hash_traverse (elf_hash_table (info),
7277 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7278 if (cinfo.error)
4dd07732
AM
7279 {
7280 free (cinfo.hashcodes);
7281 return FALSE;
7282 }
fdc90cb4
JJ
7283
7284 bucketcount
7285 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7286
7287 if (bucketcount == 0)
7288 {
7289 free (cinfo.hashcodes);
7290 return FALSE;
7291 }
7292
f16a9783 7293 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7294 BFD_ASSERT (s != NULL);
7295
7296 if (cinfo.nsyms == 0)
7297 {
f16a9783 7298 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7299 BFD_ASSERT (cinfo.min_dynindx == -1);
7300 free (cinfo.hashcodes);
7301 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7302 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7303 if (contents == NULL)
7304 return FALSE;
7305 s->contents = contents;
7306 /* 1 empty bucket. */
7307 bfd_put_32 (output_bfd, 1, contents);
7308 /* SYMIDX above the special symbol 0. */
7309 bfd_put_32 (output_bfd, 1, contents + 4);
7310 /* Just one word for bitmask. */
7311 bfd_put_32 (output_bfd, 1, contents + 8);
7312 /* Only hash fn bloom filter. */
7313 bfd_put_32 (output_bfd, 0, contents + 12);
7314 /* No hashes are valid - empty bitmask. */
7315 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7316 /* No hashes in the only bucket. */
7317 bfd_put_32 (output_bfd, 0,
7318 contents + 16 + bed->s->arch_size / 8);
7319 }
7320 else
7321 {
9e6619e2 7322 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7323 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7324
9e6619e2
AM
7325 x = cinfo.nsyms;
7326 maskbitslog2 = 1;
7327 while ((x >>= 1) != 0)
7328 ++maskbitslog2;
fdc90cb4
JJ
7329 if (maskbitslog2 < 3)
7330 maskbitslog2 = 5;
7331 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7332 maskbitslog2 = maskbitslog2 + 3;
7333 else
7334 maskbitslog2 = maskbitslog2 + 2;
7335 if (bed->s->arch_size == 64)
7336 {
7337 if (maskbitslog2 == 5)
7338 maskbitslog2 = 6;
7339 cinfo.shift1 = 6;
7340 }
7341 else
7342 cinfo.shift1 = 5;
7343 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7344 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7345 cinfo.maskbits = 1 << maskbitslog2;
7346 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7347 amt = bucketcount * sizeof (unsigned long int) * 2;
7348 amt += maskwords * sizeof (bfd_vma);
a50b1753 7349 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7350 if (cinfo.bitmask == NULL)
7351 {
7352 free (cinfo.hashcodes);
7353 return FALSE;
7354 }
7355
a50b1753 7356 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7357 cinfo.indx = cinfo.counts + bucketcount;
7358 cinfo.symindx = dynsymcount - cinfo.nsyms;
7359 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7360
7361 /* Determine how often each hash bucket is used. */
7362 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7363 for (i = 0; i < cinfo.nsyms; ++i)
7364 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7365
7366 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7367 if (cinfo.counts[i] != 0)
7368 {
7369 cinfo.indx[i] = cnt;
7370 cnt += cinfo.counts[i];
7371 }
7372 BFD_ASSERT (cnt == dynsymcount);
7373 cinfo.bucketcount = bucketcount;
7374 cinfo.local_indx = cinfo.min_dynindx;
7375
7376 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7377 s->size += cinfo.maskbits / 8;
f16a9783
MS
7378 if (bed->record_xhash_symbol != NULL)
7379 s->size += cinfo.nsyms * 4;
a50b1753 7380 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7381 if (contents == NULL)
7382 {
7383 free (cinfo.bitmask);
7384 free (cinfo.hashcodes);
7385 return FALSE;
7386 }
7387
7388 s->contents = contents;
7389 bfd_put_32 (output_bfd, bucketcount, contents);
7390 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7391 bfd_put_32 (output_bfd, maskwords, contents + 8);
7392 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7393 contents += 16 + cinfo.maskbits / 8;
7394
7395 for (i = 0; i < bucketcount; ++i)
7396 {
7397 if (cinfo.counts[i] == 0)
7398 bfd_put_32 (output_bfd, 0, contents);
7399 else
7400 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7401 contents += 4;
7402 }
7403
7404 cinfo.contents = contents;
7405
f16a9783
MS
7406 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7407 /* Renumber dynamic symbols, if populating .gnu.hash section.
7408 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7409 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7410 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7411
7412 contents = s->contents + 16;
7413 for (i = 0; i < maskwords; ++i)
7414 {
7415 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7416 contents);
7417 contents += bed->s->arch_size / 8;
7418 }
7419
7420 free (cinfo.bitmask);
7421 free (cinfo.hashcodes);
7422 }
7423 }
5a580b3a 7424
3d4d4302 7425 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7426 BFD_ASSERT (s != NULL);
7427
4ad4eba5 7428 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7429
eea6121a 7430 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7431
7432 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7433 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7434 return FALSE;
7435 }
7436
7437 return TRUE;
7438}
4d269e42 7439\f
4d269e42
AM
7440/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7441
7442static void
7443merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7444 asection *sec)
7445{
dbaa2011
AM
7446 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7447 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7448}
7449
7450/* Finish SHF_MERGE section merging. */
7451
7452bfd_boolean
630993ec 7453_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7454{
7455 bfd *ibfd;
7456 asection *sec;
7457
7458 if (!is_elf_hash_table (info->hash))
7459 return FALSE;
7460
c72f2fb2 7461 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7462 if ((ibfd->flags & DYNAMIC) == 0
7463 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7464 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7465 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7466 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7467 if ((sec->flags & SEC_MERGE) != 0
7468 && !bfd_is_abs_section (sec->output_section))
7469 {
7470 struct bfd_elf_section_data *secdata;
7471
7472 secdata = elf_section_data (sec);
630993ec 7473 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7474 &elf_hash_table (info)->merge_info,
7475 sec, &secdata->sec_info))
7476 return FALSE;
7477 else if (secdata->sec_info)
dbaa2011 7478 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7479 }
7480
7481 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7482 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7483 merge_sections_remove_hook);
7484 return TRUE;
7485}
7486
7487/* Create an entry in an ELF linker hash table. */
7488
7489struct bfd_hash_entry *
7490_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7491 struct bfd_hash_table *table,
7492 const char *string)
7493{
7494 /* Allocate the structure if it has not already been allocated by a
7495 subclass. */
7496 if (entry == NULL)
7497 {
a50b1753 7498 entry = (struct bfd_hash_entry *)
ca4be51c 7499 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7500 if (entry == NULL)
7501 return entry;
7502 }
7503
7504 /* Call the allocation method of the superclass. */
7505 entry = _bfd_link_hash_newfunc (entry, table, string);
7506 if (entry != NULL)
7507 {
7508 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7509 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7510
7511 /* Set local fields. */
7512 ret->indx = -1;
7513 ret->dynindx = -1;
7514 ret->got = htab->init_got_refcount;
7515 ret->plt = htab->init_plt_refcount;
7516 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7517 - offsetof (struct elf_link_hash_entry, size)));
7518 /* Assume that we have been called by a non-ELF symbol reader.
7519 This flag is then reset by the code which reads an ELF input
7520 file. This ensures that a symbol created by a non-ELF symbol
7521 reader will have the flag set correctly. */
7522 ret->non_elf = 1;
7523 }
7524
7525 return entry;
7526}
7527
7528/* Copy data from an indirect symbol to its direct symbol, hiding the
7529 old indirect symbol. Also used for copying flags to a weakdef. */
7530
7531void
7532_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7533 struct elf_link_hash_entry *dir,
7534 struct elf_link_hash_entry *ind)
7535{
7536 struct elf_link_hash_table *htab;
7537
7538 /* Copy down any references that we may have already seen to the
e81830c5 7539 symbol which just became indirect. */
4d269e42 7540
422f1182 7541 if (dir->versioned != versioned_hidden)
e81830c5
AM
7542 dir->ref_dynamic |= ind->ref_dynamic;
7543 dir->ref_regular |= ind->ref_regular;
7544 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7545 dir->non_got_ref |= ind->non_got_ref;
7546 dir->needs_plt |= ind->needs_plt;
7547 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7548
7549 if (ind->root.type != bfd_link_hash_indirect)
7550 return;
7551
7552 /* Copy over the global and procedure linkage table refcount entries.
7553 These may have been already set up by a check_relocs routine. */
7554 htab = elf_hash_table (info);
7555 if (ind->got.refcount > htab->init_got_refcount.refcount)
7556 {
7557 if (dir->got.refcount < 0)
7558 dir->got.refcount = 0;
7559 dir->got.refcount += ind->got.refcount;
7560 ind->got.refcount = htab->init_got_refcount.refcount;
7561 }
7562
7563 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7564 {
7565 if (dir->plt.refcount < 0)
7566 dir->plt.refcount = 0;
7567 dir->plt.refcount += ind->plt.refcount;
7568 ind->plt.refcount = htab->init_plt_refcount.refcount;
7569 }
7570
7571 if (ind->dynindx != -1)
7572 {
7573 if (dir->dynindx != -1)
7574 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7575 dir->dynindx = ind->dynindx;
7576 dir->dynstr_index = ind->dynstr_index;
7577 ind->dynindx = -1;
7578 ind->dynstr_index = 0;
7579 }
7580}
7581
7582void
7583_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7584 struct elf_link_hash_entry *h,
7585 bfd_boolean force_local)
7586{
3aa14d16
L
7587 /* STT_GNU_IFUNC symbol must go through PLT. */
7588 if (h->type != STT_GNU_IFUNC)
7589 {
7590 h->plt = elf_hash_table (info)->init_plt_offset;
7591 h->needs_plt = 0;
7592 }
4d269e42
AM
7593 if (force_local)
7594 {
7595 h->forced_local = 1;
7596 if (h->dynindx != -1)
7597 {
4d269e42
AM
7598 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7599 h->dynstr_index);
641338d8
AM
7600 h->dynindx = -1;
7601 h->dynstr_index = 0;
4d269e42
AM
7602 }
7603 }
7604}
7605
34a87bb0
L
7606/* Hide a symbol. */
7607
7608void
7609_bfd_elf_link_hide_symbol (bfd *output_bfd,
7610 struct bfd_link_info *info,
7611 struct bfd_link_hash_entry *h)
7612{
7613 if (is_elf_hash_table (info->hash))
7614 {
7615 const struct elf_backend_data *bed
7616 = get_elf_backend_data (output_bfd);
7617 struct elf_link_hash_entry *eh
7618 = (struct elf_link_hash_entry *) h;
7619 bed->elf_backend_hide_symbol (info, eh, TRUE);
7620 eh->def_dynamic = 0;
7621 eh->ref_dynamic = 0;
7622 eh->dynamic_def = 0;
7623 }
7624}
7625
7bf52ea2
AM
7626/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7627 caller. */
4d269e42
AM
7628
7629bfd_boolean
7630_bfd_elf_link_hash_table_init
7631 (struct elf_link_hash_table *table,
7632 bfd *abfd,
7633 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7634 struct bfd_hash_table *,
7635 const char *),
4dfe6ac6
NC
7636 unsigned int entsize,
7637 enum elf_target_id target_id)
4d269e42
AM
7638{
7639 bfd_boolean ret;
7640 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7641
4d269e42
AM
7642 table->init_got_refcount.refcount = can_refcount - 1;
7643 table->init_plt_refcount.refcount = can_refcount - 1;
7644 table->init_got_offset.offset = -(bfd_vma) 1;
7645 table->init_plt_offset.offset = -(bfd_vma) 1;
7646 /* The first dynamic symbol is a dummy. */
7647 table->dynsymcount = 1;
7648
7649 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7650
4d269e42 7651 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7652 table->hash_table_id = target_id;
4d269e42
AM
7653
7654 return ret;
7655}
7656
7657/* Create an ELF linker hash table. */
7658
7659struct bfd_link_hash_table *
7660_bfd_elf_link_hash_table_create (bfd *abfd)
7661{
7662 struct elf_link_hash_table *ret;
7663 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7664
7bf52ea2 7665 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7666 if (ret == NULL)
7667 return NULL;
7668
7669 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7670 sizeof (struct elf_link_hash_entry),
7671 GENERIC_ELF_DATA))
4d269e42
AM
7672 {
7673 free (ret);
7674 return NULL;
7675 }
d495ab0d 7676 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7677
7678 return &ret->root;
7679}
7680
9f7c3e5e
AM
7681/* Destroy an ELF linker hash table. */
7682
7683void
d495ab0d 7684_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7685{
d495ab0d
AM
7686 struct elf_link_hash_table *htab;
7687
7688 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7689 if (htab->dynstr != NULL)
7690 _bfd_elf_strtab_free (htab->dynstr);
7691 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7692 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7693}
7694
4d269e42
AM
7695/* This is a hook for the ELF emulation code in the generic linker to
7696 tell the backend linker what file name to use for the DT_NEEDED
7697 entry for a dynamic object. */
7698
7699void
7700bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7701{
7702 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7703 && bfd_get_format (abfd) == bfd_object)
7704 elf_dt_name (abfd) = name;
7705}
7706
7707int
7708bfd_elf_get_dyn_lib_class (bfd *abfd)
7709{
7710 int lib_class;
7711 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7712 && bfd_get_format (abfd) == bfd_object)
7713 lib_class = elf_dyn_lib_class (abfd);
7714 else
7715 lib_class = 0;
7716 return lib_class;
7717}
7718
7719void
7720bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7721{
7722 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7723 && bfd_get_format (abfd) == bfd_object)
7724 elf_dyn_lib_class (abfd) = lib_class;
7725}
7726
7727/* Get the list of DT_NEEDED entries for a link. This is a hook for
7728 the linker ELF emulation code. */
7729
7730struct bfd_link_needed_list *
7731bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7732 struct bfd_link_info *info)
7733{
7734 if (! is_elf_hash_table (info->hash))
7735 return NULL;
7736 return elf_hash_table (info)->needed;
7737}
7738
7739/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7740 hook for the linker ELF emulation code. */
7741
7742struct bfd_link_needed_list *
7743bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7744 struct bfd_link_info *info)
7745{
7746 if (! is_elf_hash_table (info->hash))
7747 return NULL;
7748 return elf_hash_table (info)->runpath;
7749}
7750
7751/* Get the name actually used for a dynamic object for a link. This
7752 is the SONAME entry if there is one. Otherwise, it is the string
7753 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7754
7755const char *
7756bfd_elf_get_dt_soname (bfd *abfd)
7757{
7758 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7759 && bfd_get_format (abfd) == bfd_object)
7760 return elf_dt_name (abfd);
7761 return NULL;
7762}
7763
7764/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7765 the ELF linker emulation code. */
7766
7767bfd_boolean
7768bfd_elf_get_bfd_needed_list (bfd *abfd,
7769 struct bfd_link_needed_list **pneeded)
7770{
7771 asection *s;
7772 bfd_byte *dynbuf = NULL;
cb33740c 7773 unsigned int elfsec;
4d269e42
AM
7774 unsigned long shlink;
7775 bfd_byte *extdyn, *extdynend;
7776 size_t extdynsize;
7777 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7778
7779 *pneeded = NULL;
7780
7781 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7782 || bfd_get_format (abfd) != bfd_object)
7783 return TRUE;
7784
7785 s = bfd_get_section_by_name (abfd, ".dynamic");
7786 if (s == NULL || s->size == 0)
7787 return TRUE;
7788
7789 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7790 goto error_return;
7791
7792 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7793 if (elfsec == SHN_BAD)
4d269e42
AM
7794 goto error_return;
7795
7796 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7797
4d269e42
AM
7798 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7799 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7800
7801 extdyn = dynbuf;
7802 extdynend = extdyn + s->size;
7803 for (; extdyn < extdynend; extdyn += extdynsize)
7804 {
7805 Elf_Internal_Dyn dyn;
7806
7807 (*swap_dyn_in) (abfd, extdyn, &dyn);
7808
7809 if (dyn.d_tag == DT_NULL)
7810 break;
7811
7812 if (dyn.d_tag == DT_NEEDED)
7813 {
7814 const char *string;
7815 struct bfd_link_needed_list *l;
7816 unsigned int tagv = dyn.d_un.d_val;
7817 bfd_size_type amt;
7818
7819 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7820 if (string == NULL)
7821 goto error_return;
7822
7823 amt = sizeof *l;
a50b1753 7824 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7825 if (l == NULL)
7826 goto error_return;
7827
7828 l->by = abfd;
7829 l->name = string;
7830 l->next = *pneeded;
7831 *pneeded = l;
7832 }
7833 }
7834
7835 free (dynbuf);
7836
7837 return TRUE;
7838
7839 error_return:
7840 if (dynbuf != NULL)
7841 free (dynbuf);
7842 return FALSE;
7843}
7844
7845struct elf_symbuf_symbol
7846{
7847 unsigned long st_name; /* Symbol name, index in string tbl */
7848 unsigned char st_info; /* Type and binding attributes */
7849 unsigned char st_other; /* Visibilty, and target specific */
7850};
7851
7852struct elf_symbuf_head
7853{
7854 struct elf_symbuf_symbol *ssym;
ef53be89 7855 size_t count;
4d269e42
AM
7856 unsigned int st_shndx;
7857};
7858
7859struct elf_symbol
7860{
7861 union
7862 {
7863 Elf_Internal_Sym *isym;
7864 struct elf_symbuf_symbol *ssym;
dcea6a95 7865 void *p;
4d269e42
AM
7866 } u;
7867 const char *name;
7868};
7869
7870/* Sort references to symbols by ascending section number. */
7871
7872static int
7873elf_sort_elf_symbol (const void *arg1, const void *arg2)
7874{
7875 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7876 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7877
dcea6a95
AM
7878 if (s1->st_shndx != s2->st_shndx)
7879 return s1->st_shndx > s2->st_shndx ? 1 : -1;
7880 /* Final sort by the address of the sym in the symbuf ensures
7881 a stable sort. */
7882 if (s1 != s2)
7883 return s1 > s2 ? 1 : -1;
7884 return 0;
4d269e42
AM
7885}
7886
7887static int
7888elf_sym_name_compare (const void *arg1, const void *arg2)
7889{
7890 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7891 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
7892 int ret = strcmp (s1->name, s2->name);
7893 if (ret != 0)
7894 return ret;
7895 if (s1->u.p != s2->u.p)
7896 return s1->u.p > s2->u.p ? 1 : -1;
7897 return 0;
4d269e42
AM
7898}
7899
7900static struct elf_symbuf_head *
ef53be89 7901elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7902{
14b1c01e 7903 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7904 struct elf_symbuf_symbol *ssym;
7905 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7906 size_t i, shndx_count, total_size;
4d269e42 7907
a50b1753 7908 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7909 if (indbuf == NULL)
7910 return NULL;
7911
7912 for (ind = indbuf, i = 0; i < symcount; i++)
7913 if (isymbuf[i].st_shndx != SHN_UNDEF)
7914 *ind++ = &isymbuf[i];
7915 indbufend = ind;
7916
7917 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7918 elf_sort_elf_symbol);
7919
7920 shndx_count = 0;
7921 if (indbufend > indbuf)
7922 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7923 if (ind[0]->st_shndx != ind[1]->st_shndx)
7924 shndx_count++;
7925
3ae181ee
L
7926 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7927 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7928 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7929 if (ssymbuf == NULL)
7930 {
7931 free (indbuf);
7932 return NULL;
7933 }
7934
3ae181ee 7935 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7936 ssymbuf->ssym = NULL;
7937 ssymbuf->count = shndx_count;
7938 ssymbuf->st_shndx = 0;
7939 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7940 {
7941 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7942 {
7943 ssymhead++;
7944 ssymhead->ssym = ssym;
7945 ssymhead->count = 0;
7946 ssymhead->st_shndx = (*ind)->st_shndx;
7947 }
7948 ssym->st_name = (*ind)->st_name;
7949 ssym->st_info = (*ind)->st_info;
7950 ssym->st_other = (*ind)->st_other;
7951 ssymhead->count++;
7952 }
ef53be89 7953 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7954 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7955 == total_size));
4d269e42
AM
7956
7957 free (indbuf);
7958 return ssymbuf;
7959}
7960
7961/* Check if 2 sections define the same set of local and global
7962 symbols. */
7963
8f317e31 7964static bfd_boolean
4d269e42
AM
7965bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7966 struct bfd_link_info *info)
7967{
7968 bfd *bfd1, *bfd2;
7969 const struct elf_backend_data *bed1, *bed2;
7970 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7971 size_t symcount1, symcount2;
4d269e42
AM
7972 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7973 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7974 Elf_Internal_Sym *isym, *isymend;
7975 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7976 size_t count1, count2, i;
cb33740c 7977 unsigned int shndx1, shndx2;
4d269e42
AM
7978 bfd_boolean result;
7979
7980 bfd1 = sec1->owner;
7981 bfd2 = sec2->owner;
7982
4d269e42
AM
7983 /* Both sections have to be in ELF. */
7984 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7985 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7986 return FALSE;
7987
7988 if (elf_section_type (sec1) != elf_section_type (sec2))
7989 return FALSE;
7990
4d269e42
AM
7991 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7992 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7993 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7994 return FALSE;
7995
7996 bed1 = get_elf_backend_data (bfd1);
7997 bed2 = get_elf_backend_data (bfd2);
7998 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7999 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8000 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8001 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8002
8003 if (symcount1 == 0 || symcount2 == 0)
8004 return FALSE;
8005
8006 result = FALSE;
8007 isymbuf1 = NULL;
8008 isymbuf2 = NULL;
a50b1753
NC
8009 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8010 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8011
8012 if (ssymbuf1 == NULL)
8013 {
8014 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8015 NULL, NULL, NULL);
8016 if (isymbuf1 == NULL)
8017 goto done;
8018
8019 if (!info->reduce_memory_overheads)
dcea6a95
AM
8020 {
8021 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8022 elf_tdata (bfd1)->symbuf = ssymbuf1;
8023 }
4d269e42
AM
8024 }
8025
8026 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8027 {
8028 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8029 NULL, NULL, NULL);
8030 if (isymbuf2 == NULL)
8031 goto done;
8032
8033 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8034 {
8035 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8036 elf_tdata (bfd2)->symbuf = ssymbuf2;
8037 }
4d269e42
AM
8038 }
8039
8040 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8041 {
8042 /* Optimized faster version. */
ef53be89 8043 size_t lo, hi, mid;
4d269e42
AM
8044 struct elf_symbol *symp;
8045 struct elf_symbuf_symbol *ssym, *ssymend;
8046
8047 lo = 0;
8048 hi = ssymbuf1->count;
8049 ssymbuf1++;
8050 count1 = 0;
8051 while (lo < hi)
8052 {
8053 mid = (lo + hi) / 2;
cb33740c 8054 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8055 hi = mid;
cb33740c 8056 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8057 lo = mid + 1;
8058 else
8059 {
8060 count1 = ssymbuf1[mid].count;
8061 ssymbuf1 += mid;
8062 break;
8063 }
8064 }
8065
8066 lo = 0;
8067 hi = ssymbuf2->count;
8068 ssymbuf2++;
8069 count2 = 0;
8070 while (lo < hi)
8071 {
8072 mid = (lo + hi) / 2;
cb33740c 8073 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8074 hi = mid;
cb33740c 8075 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8076 lo = mid + 1;
8077 else
8078 {
8079 count2 = ssymbuf2[mid].count;
8080 ssymbuf2 += mid;
8081 break;
8082 }
8083 }
8084
8085 if (count1 == 0 || count2 == 0 || count1 != count2)
8086 goto done;
8087
ca4be51c
AM
8088 symtable1
8089 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8090 symtable2
8091 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8092 if (symtable1 == NULL || symtable2 == NULL)
8093 goto done;
8094
8095 symp = symtable1;
8096 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8097 ssym < ssymend; ssym++, symp++)
8098 {
8099 symp->u.ssym = ssym;
8100 symp->name = bfd_elf_string_from_elf_section (bfd1,
8101 hdr1->sh_link,
8102 ssym->st_name);
8103 }
8104
8105 symp = symtable2;
8106 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8107 ssym < ssymend; ssym++, symp++)
8108 {
8109 symp->u.ssym = ssym;
8110 symp->name = bfd_elf_string_from_elf_section (bfd2,
8111 hdr2->sh_link,
8112 ssym->st_name);
8113 }
8114
8115 /* Sort symbol by name. */
8116 qsort (symtable1, count1, sizeof (struct elf_symbol),
8117 elf_sym_name_compare);
8118 qsort (symtable2, count1, sizeof (struct elf_symbol),
8119 elf_sym_name_compare);
8120
8121 for (i = 0; i < count1; i++)
8122 /* Two symbols must have the same binding, type and name. */
8123 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8124 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8125 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8126 goto done;
8127
8128 result = TRUE;
8129 goto done;
8130 }
8131
a50b1753
NC
8132 symtable1 = (struct elf_symbol *)
8133 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8134 symtable2 = (struct elf_symbol *)
8135 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8136 if (symtable1 == NULL || symtable2 == NULL)
8137 goto done;
8138
8139 /* Count definitions in the section. */
8140 count1 = 0;
8141 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8142 if (isym->st_shndx == shndx1)
4d269e42
AM
8143 symtable1[count1++].u.isym = isym;
8144
8145 count2 = 0;
8146 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8147 if (isym->st_shndx == shndx2)
4d269e42
AM
8148 symtable2[count2++].u.isym = isym;
8149
8150 if (count1 == 0 || count2 == 0 || count1 != count2)
8151 goto done;
8152
8153 for (i = 0; i < count1; i++)
8154 symtable1[i].name
8155 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8156 symtable1[i].u.isym->st_name);
8157
8158 for (i = 0; i < count2; i++)
8159 symtable2[i].name
8160 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8161 symtable2[i].u.isym->st_name);
8162
8163 /* Sort symbol by name. */
8164 qsort (symtable1, count1, sizeof (struct elf_symbol),
8165 elf_sym_name_compare);
8166 qsort (symtable2, count1, sizeof (struct elf_symbol),
8167 elf_sym_name_compare);
8168
8169 for (i = 0; i < count1; i++)
8170 /* Two symbols must have the same binding, type and name. */
8171 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8172 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8173 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8174 goto done;
8175
8176 result = TRUE;
8177
8178done:
8179 if (symtable1)
8180 free (symtable1);
8181 if (symtable2)
8182 free (symtable2);
8183 if (isymbuf1)
8184 free (isymbuf1);
8185 if (isymbuf2)
8186 free (isymbuf2);
8187
8188 return result;
8189}
8190
8191/* Return TRUE if 2 section types are compatible. */
8192
8193bfd_boolean
8194_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8195 bfd *bbfd, const asection *bsec)
8196{
8197 if (asec == NULL
8198 || bsec == NULL
8199 || abfd->xvec->flavour != bfd_target_elf_flavour
8200 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8201 return TRUE;
8202
8203 return elf_section_type (asec) == elf_section_type (bsec);
8204}
8205\f
c152c796
AM
8206/* Final phase of ELF linker. */
8207
8208/* A structure we use to avoid passing large numbers of arguments. */
8209
8210struct elf_final_link_info
8211{
8212 /* General link information. */
8213 struct bfd_link_info *info;
8214 /* Output BFD. */
8215 bfd *output_bfd;
8216 /* Symbol string table. */
ef10c3ac 8217 struct elf_strtab_hash *symstrtab;
c152c796
AM
8218 /* .hash section. */
8219 asection *hash_sec;
8220 /* symbol version section (.gnu.version). */
8221 asection *symver_sec;
8222 /* Buffer large enough to hold contents of any section. */
8223 bfd_byte *contents;
8224 /* Buffer large enough to hold external relocs of any section. */
8225 void *external_relocs;
8226 /* Buffer large enough to hold internal relocs of any section. */
8227 Elf_Internal_Rela *internal_relocs;
8228 /* Buffer large enough to hold external local symbols of any input
8229 BFD. */
8230 bfd_byte *external_syms;
8231 /* And a buffer for symbol section indices. */
8232 Elf_External_Sym_Shndx *locsym_shndx;
8233 /* Buffer large enough to hold internal local symbols of any input
8234 BFD. */
8235 Elf_Internal_Sym *internal_syms;
8236 /* Array large enough to hold a symbol index for each local symbol
8237 of any input BFD. */
8238 long *indices;
8239 /* Array large enough to hold a section pointer for each local
8240 symbol of any input BFD. */
8241 asection **sections;
ef10c3ac 8242 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8243 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8244 /* Number of STT_FILE syms seen. */
8245 size_t filesym_count;
c152c796
AM
8246};
8247
8248/* This struct is used to pass information to elf_link_output_extsym. */
8249
8250struct elf_outext_info
8251{
8252 bfd_boolean failed;
8253 bfd_boolean localsyms;
34a79995 8254 bfd_boolean file_sym_done;
8b127cbc 8255 struct elf_final_link_info *flinfo;
c152c796
AM
8256};
8257
d9352518
DB
8258
8259/* Support for evaluating a complex relocation.
8260
8261 Complex relocations are generalized, self-describing relocations. The
8262 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8263 relocations themselves.
d9352518
DB
8264
8265 The relocations are use a reserved elf-wide relocation type code (R_RELC
8266 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8267 information (start bit, end bit, word width, etc) into the addend. This
8268 information is extracted from CGEN-generated operand tables within gas.
8269
8270 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8271 internal) representing prefix-notation expressions, including but not
8272 limited to those sorts of expressions normally encoded as addends in the
8273 addend field. The symbol mangling format is:
8274
8275 <node> := <literal>
07d6d2b8
AM
8276 | <unary-operator> ':' <node>
8277 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8278 ;
8279
8280 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8281 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8282 | '#' <hexdigits>
8283 ;
8284
8285 <binary-operator> := as in C
8286 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8287
8288static void
a0c8462f
AM
8289set_symbol_value (bfd *bfd_with_globals,
8290 Elf_Internal_Sym *isymbuf,
8291 size_t locsymcount,
8292 size_t symidx,
8293 bfd_vma val)
d9352518 8294{
8977835c
AM
8295 struct elf_link_hash_entry **sym_hashes;
8296 struct elf_link_hash_entry *h;
8297 size_t extsymoff = locsymcount;
d9352518 8298
8977835c 8299 if (symidx < locsymcount)
d9352518 8300 {
8977835c
AM
8301 Elf_Internal_Sym *sym;
8302
8303 sym = isymbuf + symidx;
8304 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8305 {
8306 /* It is a local symbol: move it to the
8307 "absolute" section and give it a value. */
8308 sym->st_shndx = SHN_ABS;
8309 sym->st_value = val;
8310 return;
8311 }
8312 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8313 extsymoff = 0;
d9352518 8314 }
8977835c
AM
8315
8316 /* It is a global symbol: set its link type
8317 to "defined" and give it a value. */
8318
8319 sym_hashes = elf_sym_hashes (bfd_with_globals);
8320 h = sym_hashes [symidx - extsymoff];
8321 while (h->root.type == bfd_link_hash_indirect
8322 || h->root.type == bfd_link_hash_warning)
8323 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8324 h->root.type = bfd_link_hash_defined;
8325 h->root.u.def.value = val;
8326 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8327}
8328
a0c8462f
AM
8329static bfd_boolean
8330resolve_symbol (const char *name,
8331 bfd *input_bfd,
8b127cbc 8332 struct elf_final_link_info *flinfo,
a0c8462f
AM
8333 bfd_vma *result,
8334 Elf_Internal_Sym *isymbuf,
8335 size_t locsymcount)
d9352518 8336{
a0c8462f
AM
8337 Elf_Internal_Sym *sym;
8338 struct bfd_link_hash_entry *global_entry;
8339 const char *candidate = NULL;
8340 Elf_Internal_Shdr *symtab_hdr;
8341 size_t i;
8342
d9352518
DB
8343 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8344
8345 for (i = 0; i < locsymcount; ++ i)
8346 {
8977835c 8347 sym = isymbuf + i;
d9352518
DB
8348
8349 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8350 continue;
8351
8352 candidate = bfd_elf_string_from_elf_section (input_bfd,
8353 symtab_hdr->sh_link,
8354 sym->st_name);
8355#ifdef DEBUG
0f02bbd9
AM
8356 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8357 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8358#endif
8359 if (candidate && strcmp (candidate, name) == 0)
8360 {
8b127cbc 8361 asection *sec = flinfo->sections [i];
d9352518 8362
0f02bbd9
AM
8363 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8364 *result += sec->output_offset + sec->output_section->vma;
d9352518 8365#ifdef DEBUG
0f02bbd9
AM
8366 printf ("Found symbol with value %8.8lx\n",
8367 (unsigned long) *result);
d9352518
DB
8368#endif
8369 return TRUE;
8370 }
8371 }
8372
8373 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8374 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8375 FALSE, FALSE, TRUE);
d9352518
DB
8376 if (!global_entry)
8377 return FALSE;
a0c8462f 8378
d9352518
DB
8379 if (global_entry->type == bfd_link_hash_defined
8380 || global_entry->type == bfd_link_hash_defweak)
8381 {
a0c8462f
AM
8382 *result = (global_entry->u.def.value
8383 + global_entry->u.def.section->output_section->vma
8384 + global_entry->u.def.section->output_offset);
d9352518 8385#ifdef DEBUG
0f02bbd9
AM
8386 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8387 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8388#endif
8389 return TRUE;
a0c8462f 8390 }
d9352518 8391
d9352518
DB
8392 return FALSE;
8393}
8394
37b01f6a
DG
8395/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8396 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8397 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8398
d9352518 8399static bfd_boolean
a0c8462f
AM
8400resolve_section (const char *name,
8401 asection *sections,
37b01f6a
DG
8402 bfd_vma *result,
8403 bfd * abfd)
d9352518 8404{
a0c8462f
AM
8405 asection *curr;
8406 unsigned int len;
d9352518 8407
a0c8462f 8408 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8409 if (strcmp (curr->name, name) == 0)
8410 {
8411 *result = curr->vma;
8412 return TRUE;
8413 }
8414
8415 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8416 /* FIXME: This could be coded more efficiently... */
a0c8462f 8417 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8418 {
8419 len = strlen (curr->name);
a0c8462f 8420 if (len > strlen (name))
d9352518
DB
8421 continue;
8422
8423 if (strncmp (curr->name, name, len) == 0)
8424 {
8425 if (strncmp (".end", name + len, 4) == 0)
8426 {
37b01f6a 8427 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8428 return TRUE;
8429 }
8430
8431 /* Insert more pseudo-section names here, if you like. */
8432 }
8433 }
a0c8462f 8434
d9352518
DB
8435 return FALSE;
8436}
8437
8438static void
a0c8462f 8439undefined_reference (const char *reftype, const char *name)
d9352518 8440{
695344c0 8441 /* xgettext:c-format */
a0c8462f
AM
8442 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8443 reftype, name);
d9352518
DB
8444}
8445
8446static bfd_boolean
a0c8462f
AM
8447eval_symbol (bfd_vma *result,
8448 const char **symp,
8449 bfd *input_bfd,
8b127cbc 8450 struct elf_final_link_info *flinfo,
a0c8462f
AM
8451 bfd_vma dot,
8452 Elf_Internal_Sym *isymbuf,
8453 size_t locsymcount,
8454 int signed_p)
d9352518 8455{
4b93929b
NC
8456 size_t len;
8457 size_t symlen;
a0c8462f
AM
8458 bfd_vma a;
8459 bfd_vma b;
4b93929b 8460 char symbuf[4096];
0f02bbd9 8461 const char *sym = *symp;
a0c8462f
AM
8462 const char *symend;
8463 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8464
8465 len = strlen (sym);
8466 symend = sym + len;
8467
4b93929b 8468 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8469 {
8470 bfd_set_error (bfd_error_invalid_operation);
8471 return FALSE;
8472 }
a0c8462f 8473
d9352518
DB
8474 switch (* sym)
8475 {
8476 case '.':
0f02bbd9
AM
8477 *result = dot;
8478 *symp = sym + 1;
d9352518
DB
8479 return TRUE;
8480
8481 case '#':
0f02bbd9
AM
8482 ++sym;
8483 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8484 return TRUE;
8485
8486 case 'S':
8487 symbol_is_section = TRUE;
1a0670f3 8488 /* Fall through. */
a0c8462f 8489 case 's':
0f02bbd9
AM
8490 ++sym;
8491 symlen = strtol (sym, (char **) symp, 10);
8492 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8493
4b93929b 8494 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8495 {
8496 bfd_set_error (bfd_error_invalid_operation);
8497 return FALSE;
8498 }
8499
8500 memcpy (symbuf, sym, symlen);
a0c8462f 8501 symbuf[symlen] = '\0';
0f02bbd9 8502 *symp = sym + symlen;
a0c8462f
AM
8503
8504 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8505 the symbol as a section, or vice-versa. so we're pretty liberal in our
8506 interpretation here; section means "try section first", not "must be a
8507 section", and likewise with symbol. */
8508
a0c8462f 8509 if (symbol_is_section)
d9352518 8510 {
37b01f6a 8511 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8512 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8513 isymbuf, locsymcount))
d9352518
DB
8514 {
8515 undefined_reference ("section", symbuf);
8516 return FALSE;
8517 }
a0c8462f
AM
8518 }
8519 else
d9352518 8520 {
8b127cbc 8521 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8522 isymbuf, locsymcount)
8b127cbc 8523 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8524 result, input_bfd))
d9352518
DB
8525 {
8526 undefined_reference ("symbol", symbuf);
8527 return FALSE;
8528 }
8529 }
8530
8531 return TRUE;
a0c8462f 8532
d9352518
DB
8533 /* All that remains are operators. */
8534
8535#define UNARY_OP(op) \
8536 if (strncmp (sym, #op, strlen (#op)) == 0) \
8537 { \
8538 sym += strlen (#op); \
a0c8462f
AM
8539 if (*sym == ':') \
8540 ++sym; \
0f02bbd9 8541 *symp = sym; \
8b127cbc 8542 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8543 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8544 return FALSE; \
8545 if (signed_p) \
0f02bbd9 8546 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8547 else \
8548 *result = op a; \
d9352518
DB
8549 return TRUE; \
8550 }
8551
8552#define BINARY_OP(op) \
8553 if (strncmp (sym, #op, strlen (#op)) == 0) \
8554 { \
8555 sym += strlen (#op); \
a0c8462f
AM
8556 if (*sym == ':') \
8557 ++sym; \
0f02bbd9 8558 *symp = sym; \
8b127cbc 8559 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8560 isymbuf, locsymcount, signed_p)) \
a0c8462f 8561 return FALSE; \
0f02bbd9 8562 ++*symp; \
8b127cbc 8563 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8564 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8565 return FALSE; \
8566 if (signed_p) \
0f02bbd9 8567 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8568 else \
8569 *result = a op b; \
d9352518
DB
8570 return TRUE; \
8571 }
8572
8573 default:
8574 UNARY_OP (0-);
8575 BINARY_OP (<<);
8576 BINARY_OP (>>);
8577 BINARY_OP (==);
8578 BINARY_OP (!=);
8579 BINARY_OP (<=);
8580 BINARY_OP (>=);
8581 BINARY_OP (&&);
8582 BINARY_OP (||);
8583 UNARY_OP (~);
8584 UNARY_OP (!);
8585 BINARY_OP (*);
8586 BINARY_OP (/);
8587 BINARY_OP (%);
8588 BINARY_OP (^);
8589 BINARY_OP (|);
8590 BINARY_OP (&);
8591 BINARY_OP (+);
8592 BINARY_OP (-);
8593 BINARY_OP (<);
8594 BINARY_OP (>);
8595#undef UNARY_OP
8596#undef BINARY_OP
8597 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8598 bfd_set_error (bfd_error_invalid_operation);
8599 return FALSE;
8600 }
8601}
8602
d9352518 8603static void
a0c8462f
AM
8604put_value (bfd_vma size,
8605 unsigned long chunksz,
8606 bfd *input_bfd,
8607 bfd_vma x,
8608 bfd_byte *location)
d9352518
DB
8609{
8610 location += (size - chunksz);
8611
41cd1ad1 8612 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8613 {
8614 switch (chunksz)
8615 {
d9352518
DB
8616 case 1:
8617 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8618 x >>= 8;
d9352518
DB
8619 break;
8620 case 2:
8621 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8622 x >>= 16;
d9352518
DB
8623 break;
8624 case 4:
8625 bfd_put_32 (input_bfd, x, location);
65164438
NC
8626 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8627 x >>= 16;
8628 x >>= 16;
d9352518 8629 break;
d9352518 8630#ifdef BFD64
41cd1ad1 8631 case 8:
d9352518 8632 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8633 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8634 x >>= 32;
8635 x >>= 32;
8636 break;
d9352518 8637#endif
41cd1ad1
NC
8638 default:
8639 abort ();
d9352518
DB
8640 break;
8641 }
8642 }
8643}
8644
a0c8462f
AM
8645static bfd_vma
8646get_value (bfd_vma size,
8647 unsigned long chunksz,
8648 bfd *input_bfd,
8649 bfd_byte *location)
d9352518 8650{
9b239e0e 8651 int shift;
d9352518
DB
8652 bfd_vma x = 0;
8653
9b239e0e
NC
8654 /* Sanity checks. */
8655 BFD_ASSERT (chunksz <= sizeof (x)
8656 && size >= chunksz
8657 && chunksz != 0
8658 && (size % chunksz) == 0
8659 && input_bfd != NULL
8660 && location != NULL);
8661
8662 if (chunksz == sizeof (x))
8663 {
8664 BFD_ASSERT (size == chunksz);
8665
8666 /* Make sure that we do not perform an undefined shift operation.
8667 We know that size == chunksz so there will only be one iteration
8668 of the loop below. */
8669 shift = 0;
8670 }
8671 else
8672 shift = 8 * chunksz;
8673
a0c8462f 8674 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8675 {
8676 switch (chunksz)
8677 {
d9352518 8678 case 1:
9b239e0e 8679 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8680 break;
8681 case 2:
9b239e0e 8682 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8683 break;
8684 case 4:
9b239e0e 8685 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8686 break;
d9352518 8687#ifdef BFD64
9b239e0e
NC
8688 case 8:
8689 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8690 break;
9b239e0e
NC
8691#endif
8692 default:
8693 abort ();
d9352518
DB
8694 }
8695 }
8696 return x;
8697}
8698
a0c8462f
AM
8699static void
8700decode_complex_addend (unsigned long *start, /* in bits */
8701 unsigned long *oplen, /* in bits */
8702 unsigned long *len, /* in bits */
8703 unsigned long *wordsz, /* in bytes */
8704 unsigned long *chunksz, /* in bytes */
8705 unsigned long *lsb0_p,
8706 unsigned long *signed_p,
8707 unsigned long *trunc_p,
8708 unsigned long encoded)
d9352518 8709{
07d6d2b8
AM
8710 * start = encoded & 0x3F;
8711 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8712 * oplen = (encoded >> 12) & 0x3F;
8713 * wordsz = (encoded >> 18) & 0xF;
8714 * chunksz = (encoded >> 22) & 0xF;
8715 * lsb0_p = (encoded >> 27) & 1;
8716 * signed_p = (encoded >> 28) & 1;
8717 * trunc_p = (encoded >> 29) & 1;
8718}
8719
cdfeee4f 8720bfd_reloc_status_type
0f02bbd9 8721bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8722 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8723 bfd_byte *contents,
8724 Elf_Internal_Rela *rel,
8725 bfd_vma relocation)
d9352518 8726{
0f02bbd9
AM
8727 bfd_vma shift, x, mask;
8728 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8729 bfd_reloc_status_type r;
d9352518
DB
8730
8731 /* Perform this reloc, since it is complex.
8732 (this is not to say that it necessarily refers to a complex
8733 symbol; merely that it is a self-describing CGEN based reloc.
8734 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8735 word size, etc) encoded within it.). */
d9352518 8736
a0c8462f
AM
8737 decode_complex_addend (&start, &oplen, &len, &wordsz,
8738 &chunksz, &lsb0_p, &signed_p,
8739 &trunc_p, rel->r_addend);
d9352518
DB
8740
8741 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8742
8743 if (lsb0_p)
8744 shift = (start + 1) - len;
8745 else
8746 shift = (8 * wordsz) - (start + len);
8747
37b01f6a
DG
8748 x = get_value (wordsz, chunksz, input_bfd,
8749 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8750
8751#ifdef DEBUG
8752 printf ("Doing complex reloc: "
8753 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8754 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8755 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8756 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8757 oplen, (unsigned long) x, (unsigned long) mask,
8758 (unsigned long) relocation);
d9352518
DB
8759#endif
8760
cdfeee4f 8761 r = bfd_reloc_ok;
d9352518 8762 if (! trunc_p)
cdfeee4f
AM
8763 /* Now do an overflow check. */
8764 r = bfd_check_overflow ((signed_p
8765 ? complain_overflow_signed
8766 : complain_overflow_unsigned),
8767 len, 0, (8 * wordsz),
8768 relocation);
a0c8462f 8769
d9352518
DB
8770 /* Do the deed. */
8771 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8772
8773#ifdef DEBUG
8774 printf (" relocation: %8.8lx\n"
8775 " shifted mask: %8.8lx\n"
8776 " shifted/masked reloc: %8.8lx\n"
8777 " result: %8.8lx\n",
9ccb8af9
AM
8778 (unsigned long) relocation, (unsigned long) (mask << shift),
8779 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8780#endif
37b01f6a
DG
8781 put_value (wordsz, chunksz, input_bfd, x,
8782 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8783 return r;
d9352518
DB
8784}
8785
0e287786
AM
8786/* Functions to read r_offset from external (target order) reloc
8787 entry. Faster than bfd_getl32 et al, because we let the compiler
8788 know the value is aligned. */
53df40a4 8789
0e287786
AM
8790static bfd_vma
8791ext32l_r_offset (const void *p)
53df40a4
AM
8792{
8793 union aligned32
8794 {
8795 uint32_t v;
8796 unsigned char c[4];
8797 };
8798 const union aligned32 *a
0e287786 8799 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8800
8801 uint32_t aval = ( (uint32_t) a->c[0]
8802 | (uint32_t) a->c[1] << 8
8803 | (uint32_t) a->c[2] << 16
8804 | (uint32_t) a->c[3] << 24);
0e287786 8805 return aval;
53df40a4
AM
8806}
8807
0e287786
AM
8808static bfd_vma
8809ext32b_r_offset (const void *p)
53df40a4
AM
8810{
8811 union aligned32
8812 {
8813 uint32_t v;
8814 unsigned char c[4];
8815 };
8816 const union aligned32 *a
0e287786 8817 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8818
8819 uint32_t aval = ( (uint32_t) a->c[0] << 24
8820 | (uint32_t) a->c[1] << 16
8821 | (uint32_t) a->c[2] << 8
8822 | (uint32_t) a->c[3]);
0e287786 8823 return aval;
53df40a4
AM
8824}
8825
8826#ifdef BFD_HOST_64_BIT
0e287786
AM
8827static bfd_vma
8828ext64l_r_offset (const void *p)
53df40a4
AM
8829{
8830 union aligned64
8831 {
8832 uint64_t v;
8833 unsigned char c[8];
8834 };
8835 const union aligned64 *a
0e287786 8836 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8837
8838 uint64_t aval = ( (uint64_t) a->c[0]
8839 | (uint64_t) a->c[1] << 8
8840 | (uint64_t) a->c[2] << 16
8841 | (uint64_t) a->c[3] << 24
8842 | (uint64_t) a->c[4] << 32
8843 | (uint64_t) a->c[5] << 40
8844 | (uint64_t) a->c[6] << 48
8845 | (uint64_t) a->c[7] << 56);
0e287786 8846 return aval;
53df40a4
AM
8847}
8848
0e287786
AM
8849static bfd_vma
8850ext64b_r_offset (const void *p)
53df40a4
AM
8851{
8852 union aligned64
8853 {
8854 uint64_t v;
8855 unsigned char c[8];
8856 };
8857 const union aligned64 *a
0e287786 8858 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8859
8860 uint64_t aval = ( (uint64_t) a->c[0] << 56
8861 | (uint64_t) a->c[1] << 48
8862 | (uint64_t) a->c[2] << 40
8863 | (uint64_t) a->c[3] << 32
8864 | (uint64_t) a->c[4] << 24
8865 | (uint64_t) a->c[5] << 16
8866 | (uint64_t) a->c[6] << 8
8867 | (uint64_t) a->c[7]);
0e287786 8868 return aval;
53df40a4
AM
8869}
8870#endif
8871
c152c796
AM
8872/* When performing a relocatable link, the input relocations are
8873 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8874 referenced must be updated. Update all the relocations found in
8875 RELDATA. */
c152c796 8876
bca6d0e3 8877static bfd_boolean
c152c796 8878elf_link_adjust_relocs (bfd *abfd,
9eaff861 8879 asection *sec,
28dbcedc 8880 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8881 bfd_boolean sort,
8882 struct bfd_link_info *info)
c152c796
AM
8883{
8884 unsigned int i;
8885 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8886 bfd_byte *erela;
8887 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8888 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8889 bfd_vma r_type_mask;
8890 int r_sym_shift;
d4730f92
BS
8891 unsigned int count = reldata->count;
8892 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8893
d4730f92 8894 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8895 {
8896 swap_in = bed->s->swap_reloc_in;
8897 swap_out = bed->s->swap_reloc_out;
8898 }
d4730f92 8899 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8900 {
8901 swap_in = bed->s->swap_reloca_in;
8902 swap_out = bed->s->swap_reloca_out;
8903 }
8904 else
8905 abort ();
8906
8907 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8908 abort ();
8909
8910 if (bed->s->arch_size == 32)
8911 {
8912 r_type_mask = 0xff;
8913 r_sym_shift = 8;
8914 }
8915 else
8916 {
8917 r_type_mask = 0xffffffff;
8918 r_sym_shift = 32;
8919 }
8920
d4730f92
BS
8921 erela = reldata->hdr->contents;
8922 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8923 {
8924 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8925 unsigned int j;
8926
8927 if (*rel_hash == NULL)
8928 continue;
8929
10bbbc1d
NC
8930 if ((*rel_hash)->indx == -2
8931 && info->gc_sections
8932 && ! info->gc_keep_exported)
8933 {
8934 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8935 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8936 abfd, sec,
8937 (*rel_hash)->root.root.string);
9793eb77 8938 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8939 abfd, sec);
10bbbc1d
NC
8940 bfd_set_error (bfd_error_invalid_operation);
8941 return FALSE;
8942 }
c152c796
AM
8943 BFD_ASSERT ((*rel_hash)->indx >= 0);
8944
8945 (*swap_in) (abfd, erela, irela);
8946 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8947 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8948 | (irela[j].r_info & r_type_mask));
8949 (*swap_out) (abfd, irela, erela);
8950 }
53df40a4 8951
9eaff861
AO
8952 if (bed->elf_backend_update_relocs)
8953 (*bed->elf_backend_update_relocs) (sec, reldata);
8954
0e287786 8955 if (sort && count != 0)
53df40a4 8956 {
0e287786
AM
8957 bfd_vma (*ext_r_off) (const void *);
8958 bfd_vma r_off;
8959 size_t elt_size;
8960 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8961 bfd_byte *buf = NULL;
28dbcedc
AM
8962
8963 if (bed->s->arch_size == 32)
8964 {
8965 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8966 ext_r_off = ext32l_r_offset;
28dbcedc 8967 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8968 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8969 else
8970 abort ();
8971 }
53df40a4 8972 else
28dbcedc 8973 {
53df40a4 8974#ifdef BFD_HOST_64_BIT
28dbcedc 8975 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8976 ext_r_off = ext64l_r_offset;
28dbcedc 8977 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8978 ext_r_off = ext64b_r_offset;
28dbcedc 8979 else
53df40a4 8980#endif
28dbcedc
AM
8981 abort ();
8982 }
0e287786 8983
bca6d0e3
AM
8984 /* Must use a stable sort here. A modified insertion sort,
8985 since the relocs are mostly sorted already. */
0e287786
AM
8986 elt_size = reldata->hdr->sh_entsize;
8987 base = reldata->hdr->contents;
8988 end = base + count * elt_size;
bca6d0e3 8989 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8990 abort ();
8991
8992 /* Ensure the first element is lowest. This acts as a sentinel,
8993 speeding the main loop below. */
8994 r_off = (*ext_r_off) (base);
8995 for (p = loc = base; (p += elt_size) < end; )
8996 {
8997 bfd_vma r_off2 = (*ext_r_off) (p);
8998 if (r_off > r_off2)
8999 {
9000 r_off = r_off2;
9001 loc = p;
9002 }
9003 }
9004 if (loc != base)
9005 {
9006 /* Don't just swap *base and *loc as that changes the order
9007 of the original base[0] and base[1] if they happen to
9008 have the same r_offset. */
bca6d0e3
AM
9009 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9010 memcpy (onebuf, loc, elt_size);
0e287786 9011 memmove (base + elt_size, base, loc - base);
bca6d0e3 9012 memcpy (base, onebuf, elt_size);
0e287786
AM
9013 }
9014
b29b8669 9015 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9016 {
9017 /* base to p is sorted, *p is next to insert. */
9018 r_off = (*ext_r_off) (p);
9019 /* Search the sorted region for location to insert. */
9020 loc = p - elt_size;
9021 while (r_off < (*ext_r_off) (loc))
9022 loc -= elt_size;
9023 loc += elt_size;
9024 if (loc != p)
9025 {
bca6d0e3
AM
9026 /* Chances are there is a run of relocs to insert here,
9027 from one of more input files. Files are not always
9028 linked in order due to the way elf_link_input_bfd is
9029 called. See pr17666. */
9030 size_t sortlen = p - loc;
9031 bfd_vma r_off2 = (*ext_r_off) (loc);
9032 size_t runlen = elt_size;
9033 size_t buf_size = 96 * 1024;
9034 while (p + runlen < end
9035 && (sortlen <= buf_size
9036 || runlen + elt_size <= buf_size)
9037 && r_off2 > (*ext_r_off) (p + runlen))
9038 runlen += elt_size;
9039 if (buf == NULL)
9040 {
9041 buf = bfd_malloc (buf_size);
9042 if (buf == NULL)
9043 return FALSE;
9044 }
9045 if (runlen < sortlen)
9046 {
9047 memcpy (buf, p, runlen);
9048 memmove (loc + runlen, loc, sortlen);
9049 memcpy (loc, buf, runlen);
9050 }
9051 else
9052 {
9053 memcpy (buf, loc, sortlen);
9054 memmove (loc, p, runlen);
9055 memcpy (loc + runlen, buf, sortlen);
9056 }
b29b8669 9057 p += runlen - elt_size;
0e287786
AM
9058 }
9059 }
9060 /* Hashes are no longer valid. */
28dbcedc
AM
9061 free (reldata->hashes);
9062 reldata->hashes = NULL;
bca6d0e3 9063 free (buf);
53df40a4 9064 }
bca6d0e3 9065 return TRUE;
c152c796
AM
9066}
9067
9068struct elf_link_sort_rela
9069{
9070 union {
9071 bfd_vma offset;
9072 bfd_vma sym_mask;
9073 } u;
9074 enum elf_reloc_type_class type;
9075 /* We use this as an array of size int_rels_per_ext_rel. */
9076 Elf_Internal_Rela rela[1];
9077};
9078
dcea6a95
AM
9079/* qsort stability here and for cmp2 is only an issue if multiple
9080 dynamic relocations are emitted at the same address. But targets
9081 that apply a series of dynamic relocations each operating on the
9082 result of the prior relocation can't use -z combreloc as
9083 implemented anyway. Such schemes tend to be broken by sorting on
9084 symbol index. That leaves dynamic NONE relocs as the only other
9085 case where ld might emit multiple relocs at the same address, and
9086 those are only emitted due to target bugs. */
9087
c152c796
AM
9088static int
9089elf_link_sort_cmp1 (const void *A, const void *B)
9090{
a50b1753
NC
9091 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9092 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9093 int relativea, relativeb;
9094
9095 relativea = a->type == reloc_class_relative;
9096 relativeb = b->type == reloc_class_relative;
9097
9098 if (relativea < relativeb)
9099 return 1;
9100 if (relativea > relativeb)
9101 return -1;
9102 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9103 return -1;
9104 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9105 return 1;
9106 if (a->rela->r_offset < b->rela->r_offset)
9107 return -1;
9108 if (a->rela->r_offset > b->rela->r_offset)
9109 return 1;
9110 return 0;
9111}
9112
9113static int
9114elf_link_sort_cmp2 (const void *A, const void *B)
9115{
a50b1753
NC
9116 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9117 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9118
7e612e98 9119 if (a->type < b->type)
c152c796 9120 return -1;
7e612e98 9121 if (a->type > b->type)
c152c796 9122 return 1;
7e612e98 9123 if (a->u.offset < b->u.offset)
c152c796 9124 return -1;
7e612e98 9125 if (a->u.offset > b->u.offset)
c152c796
AM
9126 return 1;
9127 if (a->rela->r_offset < b->rela->r_offset)
9128 return -1;
9129 if (a->rela->r_offset > b->rela->r_offset)
9130 return 1;
9131 return 0;
9132}
9133
9134static size_t
9135elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9136{
3410fea8 9137 asection *dynamic_relocs;
fc66a176
L
9138 asection *rela_dyn;
9139 asection *rel_dyn;
c152c796
AM
9140 bfd_size_type count, size;
9141 size_t i, ret, sort_elt, ext_size;
9142 bfd_byte *sort, *s_non_relative, *p;
9143 struct elf_link_sort_rela *sq;
9144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9145 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9146 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9147 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9148 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9149 struct bfd_link_order *lo;
9150 bfd_vma r_sym_mask;
3410fea8 9151 bfd_boolean use_rela;
c152c796 9152
3410fea8
NC
9153 /* Find a dynamic reloc section. */
9154 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9155 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9156 if (rela_dyn != NULL && rela_dyn->size > 0
9157 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9158 {
3410fea8
NC
9159 bfd_boolean use_rela_initialised = FALSE;
9160
9161 /* This is just here to stop gcc from complaining.
c8e44c6d 9162 Its initialization checking code is not perfect. */
3410fea8
NC
9163 use_rela = TRUE;
9164
9165 /* Both sections are present. Examine the sizes
9166 of the indirect sections to help us choose. */
9167 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9168 if (lo->type == bfd_indirect_link_order)
9169 {
9170 asection *o = lo->u.indirect.section;
9171
9172 if ((o->size % bed->s->sizeof_rela) == 0)
9173 {
9174 if ((o->size % bed->s->sizeof_rel) == 0)
9175 /* Section size is divisible by both rel and rela sizes.
9176 It is of no help to us. */
9177 ;
9178 else
9179 {
9180 /* Section size is only divisible by rela. */
535b785f 9181 if (use_rela_initialised && !use_rela)
3410fea8 9182 {
9793eb77 9183 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9184 "they are in more than one size"),
9185 abfd);
3410fea8
NC
9186 bfd_set_error (bfd_error_invalid_operation);
9187 return 0;
9188 }
9189 else
9190 {
9191 use_rela = TRUE;
9192 use_rela_initialised = TRUE;
9193 }
9194 }
9195 }
9196 else if ((o->size % bed->s->sizeof_rel) == 0)
9197 {
9198 /* Section size is only divisible by rel. */
535b785f 9199 if (use_rela_initialised && use_rela)
3410fea8 9200 {
9793eb77 9201 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9202 "they are in more than one size"),
9203 abfd);
3410fea8
NC
9204 bfd_set_error (bfd_error_invalid_operation);
9205 return 0;
9206 }
9207 else
9208 {
9209 use_rela = FALSE;
9210 use_rela_initialised = TRUE;
9211 }
9212 }
9213 else
9214 {
c8e44c6d
AM
9215 /* The section size is not divisible by either -
9216 something is wrong. */
9793eb77 9217 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9218 "they are of an unknown size"), abfd);
3410fea8
NC
9219 bfd_set_error (bfd_error_invalid_operation);
9220 return 0;
9221 }
9222 }
9223
9224 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9225 if (lo->type == bfd_indirect_link_order)
9226 {
9227 asection *o = lo->u.indirect.section;
9228
9229 if ((o->size % bed->s->sizeof_rela) == 0)
9230 {
9231 if ((o->size % bed->s->sizeof_rel) == 0)
9232 /* Section size is divisible by both rel and rela sizes.
9233 It is of no help to us. */
9234 ;
9235 else
9236 {
9237 /* Section size is only divisible by rela. */
535b785f 9238 if (use_rela_initialised && !use_rela)
3410fea8 9239 {
9793eb77 9240 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9241 "they are in more than one size"),
9242 abfd);
3410fea8
NC
9243 bfd_set_error (bfd_error_invalid_operation);
9244 return 0;
9245 }
9246 else
9247 {
9248 use_rela = TRUE;
9249 use_rela_initialised = TRUE;
9250 }
9251 }
9252 }
9253 else if ((o->size % bed->s->sizeof_rel) == 0)
9254 {
9255 /* Section size is only divisible by rel. */
535b785f 9256 if (use_rela_initialised && use_rela)
3410fea8 9257 {
9793eb77 9258 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9259 "they are in more than one size"),
9260 abfd);
3410fea8
NC
9261 bfd_set_error (bfd_error_invalid_operation);
9262 return 0;
9263 }
9264 else
9265 {
9266 use_rela = FALSE;
9267 use_rela_initialised = TRUE;
9268 }
9269 }
9270 else
9271 {
c8e44c6d
AM
9272 /* The section size is not divisible by either -
9273 something is wrong. */
9793eb77 9274 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9275 "they are of an unknown size"), abfd);
3410fea8
NC
9276 bfd_set_error (bfd_error_invalid_operation);
9277 return 0;
9278 }
9279 }
9280
9281 if (! use_rela_initialised)
9282 /* Make a guess. */
9283 use_rela = TRUE;
c152c796 9284 }
fc66a176
L
9285 else if (rela_dyn != NULL && rela_dyn->size > 0)
9286 use_rela = TRUE;
9287 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9288 use_rela = FALSE;
c152c796 9289 else
fc66a176 9290 return 0;
3410fea8
NC
9291
9292 if (use_rela)
c152c796 9293 {
3410fea8 9294 dynamic_relocs = rela_dyn;
c152c796
AM
9295 ext_size = bed->s->sizeof_rela;
9296 swap_in = bed->s->swap_reloca_in;
9297 swap_out = bed->s->swap_reloca_out;
9298 }
3410fea8
NC
9299 else
9300 {
9301 dynamic_relocs = rel_dyn;
9302 ext_size = bed->s->sizeof_rel;
9303 swap_in = bed->s->swap_reloc_in;
9304 swap_out = bed->s->swap_reloc_out;
9305 }
c152c796
AM
9306
9307 size = 0;
3410fea8 9308 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9309 if (lo->type == bfd_indirect_link_order)
3410fea8 9310 size += lo->u.indirect.section->size;
c152c796 9311
3410fea8 9312 if (size != dynamic_relocs->size)
c152c796
AM
9313 return 0;
9314
9315 sort_elt = (sizeof (struct elf_link_sort_rela)
9316 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9317
9318 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9319 if (count == 0)
9320 return 0;
a50b1753 9321 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9322
c152c796
AM
9323 if (sort == NULL)
9324 {
9325 (*info->callbacks->warning)
9793eb77 9326 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9327 return 0;
9328 }
9329
9330 if (bed->s->arch_size == 32)
9331 r_sym_mask = ~(bfd_vma) 0xff;
9332 else
9333 r_sym_mask = ~(bfd_vma) 0xffffffff;
9334
3410fea8 9335 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9336 if (lo->type == bfd_indirect_link_order)
9337 {
9338 bfd_byte *erel, *erelend;
9339 asection *o = lo->u.indirect.section;
9340
1da212d6
AM
9341 if (o->contents == NULL && o->size != 0)
9342 {
9343 /* This is a reloc section that is being handled as a normal
9344 section. See bfd_section_from_shdr. We can't combine
9345 relocs in this case. */
9346 free (sort);
9347 return 0;
9348 }
c152c796 9349 erel = o->contents;
eea6121a 9350 erelend = o->contents + o->size;
c8e44c6d 9351 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9352
c152c796
AM
9353 while (erel < erelend)
9354 {
9355 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9356
c152c796 9357 (*swap_in) (abfd, erel, s->rela);
7e612e98 9358 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9359 s->u.sym_mask = r_sym_mask;
9360 p += sort_elt;
9361 erel += ext_size;
9362 }
9363 }
9364
9365 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9366
9367 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9368 {
9369 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9370 if (s->type != reloc_class_relative)
9371 break;
9372 }
9373 ret = i;
9374 s_non_relative = p;
9375
9376 sq = (struct elf_link_sort_rela *) s_non_relative;
9377 for (; i < count; i++, p += sort_elt)
9378 {
9379 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9380 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9381 sq = sp;
9382 sp->u.offset = sq->rela->r_offset;
9383 }
9384
9385 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9386
c8e44c6d
AM
9387 struct elf_link_hash_table *htab = elf_hash_table (info);
9388 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9389 {
9390 /* We have plt relocs in .rela.dyn. */
9391 sq = (struct elf_link_sort_rela *) sort;
9392 for (i = 0; i < count; i++)
9393 if (sq[count - i - 1].type != reloc_class_plt)
9394 break;
9395 if (i != 0 && htab->srelplt->size == i * ext_size)
9396 {
9397 struct bfd_link_order **plo;
9398 /* Put srelplt link_order last. This is so the output_offset
9399 set in the next loop is correct for DT_JMPREL. */
9400 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9401 if ((*plo)->type == bfd_indirect_link_order
9402 && (*plo)->u.indirect.section == htab->srelplt)
9403 {
9404 lo = *plo;
9405 *plo = lo->next;
9406 }
9407 else
9408 plo = &(*plo)->next;
9409 *plo = lo;
9410 lo->next = NULL;
9411 dynamic_relocs->map_tail.link_order = lo;
9412 }
9413 }
9414
9415 p = sort;
3410fea8 9416 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9417 if (lo->type == bfd_indirect_link_order)
9418 {
9419 bfd_byte *erel, *erelend;
9420 asection *o = lo->u.indirect.section;
9421
9422 erel = o->contents;
eea6121a 9423 erelend = o->contents + o->size;
c8e44c6d 9424 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9425 while (erel < erelend)
9426 {
9427 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9428 (*swap_out) (abfd, s->rela, erel);
9429 p += sort_elt;
9430 erel += ext_size;
9431 }
9432 }
9433
9434 free (sort);
3410fea8 9435 *psec = dynamic_relocs;
c152c796
AM
9436 return ret;
9437}
9438
ef10c3ac 9439/* Add a symbol to the output symbol string table. */
c152c796 9440
6e0b88f1 9441static int
ef10c3ac
L
9442elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9443 const char *name,
9444 Elf_Internal_Sym *elfsym,
9445 asection *input_sec,
9446 struct elf_link_hash_entry *h)
c152c796 9447{
6e0b88f1 9448 int (*output_symbol_hook)
c152c796
AM
9449 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9450 struct elf_link_hash_entry *);
ef10c3ac 9451 struct elf_link_hash_table *hash_table;
c152c796 9452 const struct elf_backend_data *bed;
ef10c3ac 9453 bfd_size_type strtabsize;
c152c796 9454
8539e4e8
AM
9455 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9456
8b127cbc 9457 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9458 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9459 if (output_symbol_hook != NULL)
9460 {
8b127cbc 9461 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9462 if (ret != 1)
9463 return ret;
c152c796
AM
9464 }
9465
06f44071
AM
9466 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9467 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9468 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9469 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9470
ef10c3ac
L
9471 if (name == NULL
9472 || *name == '\0'
9473 || (input_sec->flags & SEC_EXCLUDE))
9474 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9475 else
9476 {
ef10c3ac
L
9477 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9478 to get the final offset for st_name. */
9479 elfsym->st_name
9480 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9481 name, FALSE);
c152c796 9482 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9483 return 0;
c152c796
AM
9484 }
9485
ef10c3ac
L
9486 hash_table = elf_hash_table (flinfo->info);
9487 strtabsize = hash_table->strtabsize;
9488 if (strtabsize <= hash_table->strtabcount)
c152c796 9489 {
ef10c3ac
L
9490 strtabsize += strtabsize;
9491 hash_table->strtabsize = strtabsize;
9492 strtabsize *= sizeof (*hash_table->strtab);
9493 hash_table->strtab
9494 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9495 strtabsize);
9496 if (hash_table->strtab == NULL)
6e0b88f1 9497 return 0;
c152c796 9498 }
ef10c3ac
L
9499 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9500 hash_table->strtab[hash_table->strtabcount].dest_index
9501 = hash_table->strtabcount;
9502 hash_table->strtab[hash_table->strtabcount].destshndx_index
9503 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9504
ed48ec2e 9505 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9506 hash_table->strtabcount += 1;
9507
9508 return 1;
9509}
9510
9511/* Swap symbols out to the symbol table and flush the output symbols to
9512 the file. */
9513
9514static bfd_boolean
9515elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9516{
9517 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9518 bfd_size_type amt;
9519 size_t i;
ef10c3ac
L
9520 const struct elf_backend_data *bed;
9521 bfd_byte *symbuf;
9522 Elf_Internal_Shdr *hdr;
9523 file_ptr pos;
9524 bfd_boolean ret;
9525
9526 if (!hash_table->strtabcount)
9527 return TRUE;
9528
9529 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9530
9531 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9532
ef10c3ac
L
9533 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9534 symbuf = (bfd_byte *) bfd_malloc (amt);
9535 if (symbuf == NULL)
9536 return FALSE;
1b786873 9537
ef10c3ac 9538 if (flinfo->symshndxbuf)
c152c796 9539 {
ef53be89
AM
9540 amt = sizeof (Elf_External_Sym_Shndx);
9541 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9542 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9543 if (flinfo->symshndxbuf == NULL)
c152c796 9544 {
ef10c3ac
L
9545 free (symbuf);
9546 return FALSE;
c152c796 9547 }
c152c796
AM
9548 }
9549
ef10c3ac
L
9550 for (i = 0; i < hash_table->strtabcount; i++)
9551 {
9552 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9553 if (elfsym->sym.st_name == (unsigned long) -1)
9554 elfsym->sym.st_name = 0;
9555 else
9556 elfsym->sym.st_name
9557 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9558 elfsym->sym.st_name);
9559 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9560 ((bfd_byte *) symbuf
9561 + (elfsym->dest_index
9562 * bed->s->sizeof_sym)),
9563 (flinfo->symshndxbuf
9564 + elfsym->destshndx_index));
9565 }
9566
1ff6de03
NA
9567 /* Allow the linker to examine the strtab and symtab now they are
9568 populated. */
9569
9570 if (flinfo->info->callbacks->examine_strtab)
9571 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9572 hash_table->strtabcount,
9573 flinfo->symstrtab);
9574
ef10c3ac
L
9575 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9576 pos = hdr->sh_offset + hdr->sh_size;
9577 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9578 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9579 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9580 {
9581 hdr->sh_size += amt;
9582 ret = TRUE;
9583 }
9584 else
9585 ret = FALSE;
c152c796 9586
ef10c3ac
L
9587 free (symbuf);
9588
9589 free (hash_table->strtab);
9590 hash_table->strtab = NULL;
9591
9592 return ret;
c152c796
AM
9593}
9594
c0d5a53d
L
9595/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9596
9597static bfd_boolean
9598check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9599{
4fbb74a6
AM
9600 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9601 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9602 {
9603 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9604 beyond 64k. */
4eca0228 9605 _bfd_error_handler
695344c0 9606 /* xgettext:c-format */
9793eb77 9607 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9608 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9609 bfd_set_error (bfd_error_nonrepresentable_section);
9610 return FALSE;
9611 }
9612 return TRUE;
9613}
9614
c152c796
AM
9615/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9616 allowing an unsatisfied unversioned symbol in the DSO to match a
9617 versioned symbol that would normally require an explicit version.
9618 We also handle the case that a DSO references a hidden symbol
9619 which may be satisfied by a versioned symbol in another DSO. */
9620
9621static bfd_boolean
9622elf_link_check_versioned_symbol (struct bfd_link_info *info,
9623 const struct elf_backend_data *bed,
9624 struct elf_link_hash_entry *h)
9625{
9626 bfd *abfd;
9627 struct elf_link_loaded_list *loaded;
9628
9629 if (!is_elf_hash_table (info->hash))
9630 return FALSE;
9631
90c984fc
L
9632 /* Check indirect symbol. */
9633 while (h->root.type == bfd_link_hash_indirect)
9634 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9635
c152c796
AM
9636 switch (h->root.type)
9637 {
9638 default:
9639 abfd = NULL;
9640 break;
9641
9642 case bfd_link_hash_undefined:
9643 case bfd_link_hash_undefweak:
9644 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9645 if (abfd == NULL
9646 || (abfd->flags & DYNAMIC) == 0
e56f61be 9647 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9648 return FALSE;
9649 break;
9650
9651 case bfd_link_hash_defined:
9652 case bfd_link_hash_defweak:
9653 abfd = h->root.u.def.section->owner;
9654 break;
9655
9656 case bfd_link_hash_common:
9657 abfd = h->root.u.c.p->section->owner;
9658 break;
9659 }
9660 BFD_ASSERT (abfd != NULL);
9661
9662 for (loaded = elf_hash_table (info)->loaded;
9663 loaded != NULL;
9664 loaded = loaded->next)
9665 {
9666 bfd *input;
9667 Elf_Internal_Shdr *hdr;
ef53be89
AM
9668 size_t symcount;
9669 size_t extsymcount;
9670 size_t extsymoff;
c152c796
AM
9671 Elf_Internal_Shdr *versymhdr;
9672 Elf_Internal_Sym *isym;
9673 Elf_Internal_Sym *isymend;
9674 Elf_Internal_Sym *isymbuf;
9675 Elf_External_Versym *ever;
9676 Elf_External_Versym *extversym;
9677
9678 input = loaded->abfd;
9679
9680 /* We check each DSO for a possible hidden versioned definition. */
9681 if (input == abfd
9682 || (input->flags & DYNAMIC) == 0
9683 || elf_dynversym (input) == 0)
9684 continue;
9685
9686 hdr = &elf_tdata (input)->dynsymtab_hdr;
9687
9688 symcount = hdr->sh_size / bed->s->sizeof_sym;
9689 if (elf_bad_symtab (input))
9690 {
9691 extsymcount = symcount;
9692 extsymoff = 0;
9693 }
9694 else
9695 {
9696 extsymcount = symcount - hdr->sh_info;
9697 extsymoff = hdr->sh_info;
9698 }
9699
9700 if (extsymcount == 0)
9701 continue;
9702
9703 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9704 NULL, NULL, NULL);
9705 if (isymbuf == NULL)
9706 return FALSE;
9707
9708 /* Read in any version definitions. */
9709 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9710 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9711 if (extversym == NULL)
9712 goto error_ret;
9713
9714 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9715 || (bfd_bread (extversym, versymhdr->sh_size, input)
9716 != versymhdr->sh_size))
9717 {
9718 free (extversym);
9719 error_ret:
9720 free (isymbuf);
9721 return FALSE;
9722 }
9723
9724 ever = extversym + extsymoff;
9725 isymend = isymbuf + extsymcount;
9726 for (isym = isymbuf; isym < isymend; isym++, ever++)
9727 {
9728 const char *name;
9729 Elf_Internal_Versym iver;
9730 unsigned short version_index;
9731
9732 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9733 || isym->st_shndx == SHN_UNDEF)
9734 continue;
9735
9736 name = bfd_elf_string_from_elf_section (input,
9737 hdr->sh_link,
9738 isym->st_name);
9739 if (strcmp (name, h->root.root.string) != 0)
9740 continue;
9741
9742 _bfd_elf_swap_versym_in (input, ever, &iver);
9743
d023c380
L
9744 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9745 && !(h->def_regular
9746 && h->forced_local))
c152c796
AM
9747 {
9748 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9749 have provided a definition for the undefined sym unless
9750 it is defined in a non-shared object and forced local.
9751 */
c152c796
AM
9752 abort ();
9753 }
9754
9755 version_index = iver.vs_vers & VERSYM_VERSION;
9756 if (version_index == 1 || version_index == 2)
9757 {
9758 /* This is the base or first version. We can use it. */
9759 free (extversym);
9760 free (isymbuf);
9761 return TRUE;
9762 }
9763 }
9764
9765 free (extversym);
9766 free (isymbuf);
9767 }
9768
9769 return FALSE;
9770}
9771
b8871f35
L
9772/* Convert ELF common symbol TYPE. */
9773
9774static int
9775elf_link_convert_common_type (struct bfd_link_info *info, int type)
9776{
9777 /* Commom symbol can only appear in relocatable link. */
9778 if (!bfd_link_relocatable (info))
9779 abort ();
9780 switch (info->elf_stt_common)
9781 {
9782 case unchanged:
9783 break;
9784 case elf_stt_common:
9785 type = STT_COMMON;
9786 break;
9787 case no_elf_stt_common:
9788 type = STT_OBJECT;
9789 break;
9790 }
9791 return type;
9792}
9793
c152c796
AM
9794/* Add an external symbol to the symbol table. This is called from
9795 the hash table traversal routine. When generating a shared object,
9796 we go through the symbol table twice. The first time we output
9797 anything that might have been forced to local scope in a version
9798 script. The second time we output the symbols that are still
9799 global symbols. */
9800
9801static bfd_boolean
7686d77d 9802elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9803{
7686d77d 9804 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9805 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9806 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9807 bfd_boolean strip;
9808 Elf_Internal_Sym sym;
9809 asection *input_sec;
9810 const struct elf_backend_data *bed;
6e0b88f1
AM
9811 long indx;
9812 int ret;
b8871f35 9813 unsigned int type;
c152c796
AM
9814
9815 if (h->root.type == bfd_link_hash_warning)
9816 {
9817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9818 if (h->root.type == bfd_link_hash_new)
9819 return TRUE;
9820 }
9821
9822 /* Decide whether to output this symbol in this pass. */
9823 if (eoinfo->localsyms)
9824 {
4deb8f71 9825 if (!h->forced_local)
c152c796
AM
9826 return TRUE;
9827 }
9828 else
9829 {
4deb8f71 9830 if (h->forced_local)
c152c796
AM
9831 return TRUE;
9832 }
9833
8b127cbc 9834 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9835
12ac1cf5 9836 if (h->root.type == bfd_link_hash_undefined)
c152c796 9837 {
12ac1cf5
NC
9838 /* If we have an undefined symbol reference here then it must have
9839 come from a shared library that is being linked in. (Undefined
98da7939
L
9840 references in regular files have already been handled unless
9841 they are in unreferenced sections which are removed by garbage
9842 collection). */
12ac1cf5
NC
9843 bfd_boolean ignore_undef = FALSE;
9844
9845 /* Some symbols may be special in that the fact that they're
9846 undefined can be safely ignored - let backend determine that. */
9847 if (bed->elf_backend_ignore_undef_symbol)
9848 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9849
9850 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9851 if (!ignore_undef
c54f1524 9852 && h->ref_dynamic_nonweak
8b127cbc
AM
9853 && (!h->ref_regular || flinfo->info->gc_sections)
9854 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9855 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9856 (*flinfo->info->callbacks->undefined_symbol)
9857 (flinfo->info, h->root.root.string,
9858 h->ref_regular ? NULL : h->root.u.undef.abfd,
9859 NULL, 0,
9860 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9861
9862 /* Strip a global symbol defined in a discarded section. */
9863 if (h->indx == -3)
9864 return TRUE;
c152c796
AM
9865 }
9866
9867 /* We should also warn if a forced local symbol is referenced from
9868 shared libraries. */
0e1862bb 9869 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9870 && h->forced_local
9871 && h->ref_dynamic
371a5866 9872 && h->def_regular
f5385ebf 9873 && !h->dynamic_def
ee659f1f 9874 && h->ref_dynamic_nonweak
8b127cbc 9875 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9876 {
17d078c5
AM
9877 bfd *def_bfd;
9878 const char *msg;
90c984fc
L
9879 struct elf_link_hash_entry *hi = h;
9880
9881 /* Check indirect symbol. */
9882 while (hi->root.type == bfd_link_hash_indirect)
9883 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9884
9885 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9886 /* xgettext:c-format */
871b3ab2 9887 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9888 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9889 /* xgettext:c-format */
871b3ab2 9890 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9891 else
695344c0 9892 /* xgettext:c-format */
871b3ab2 9893 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9894 def_bfd = flinfo->output_bfd;
90c984fc
L
9895 if (hi->root.u.def.section != bfd_abs_section_ptr)
9896 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9897 _bfd_error_handler (msg, flinfo->output_bfd,
9898 h->root.root.string, def_bfd);
17d078c5 9899 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9900 eoinfo->failed = TRUE;
9901 return FALSE;
9902 }
9903
9904 /* We don't want to output symbols that have never been mentioned by
9905 a regular file, or that we have been told to strip. However, if
9906 h->indx is set to -2, the symbol is used by a reloc and we must
9907 output it. */
d983c8c5 9908 strip = FALSE;
c152c796 9909 if (h->indx == -2)
d983c8c5 9910 ;
f5385ebf 9911 else if ((h->def_dynamic
77cfaee6
AM
9912 || h->ref_dynamic
9913 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9914 && !h->def_regular
9915 && !h->ref_regular)
c152c796 9916 strip = TRUE;
8b127cbc 9917 else if (flinfo->info->strip == strip_all)
c152c796 9918 strip = TRUE;
8b127cbc
AM
9919 else if (flinfo->info->strip == strip_some
9920 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9921 h->root.root.string, FALSE, FALSE) == NULL)
9922 strip = TRUE;
d56d55e7
AM
9923 else if ((h->root.type == bfd_link_hash_defined
9924 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9925 && ((flinfo->info->strip_discarded
dbaa2011 9926 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9927 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9928 && h->root.u.def.section->owner != NULL
d56d55e7 9929 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9930 strip = TRUE;
9e2278f5
AM
9931 else if ((h->root.type == bfd_link_hash_undefined
9932 || h->root.type == bfd_link_hash_undefweak)
9933 && h->root.u.undef.abfd != NULL
9934 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9935 strip = TRUE;
c152c796 9936
b8871f35
L
9937 type = h->type;
9938
c152c796 9939 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9940 nothing else to do. However, if it is a forced local symbol or
9941 an ifunc symbol we need to give the backend finish_dynamic_symbol
9942 function a chance to make it dynamic. */
c152c796
AM
9943 if (strip
9944 && h->dynindx == -1
b8871f35 9945 && type != STT_GNU_IFUNC
f5385ebf 9946 && !h->forced_local)
c152c796
AM
9947 return TRUE;
9948
9949 sym.st_value = 0;
9950 sym.st_size = h->size;
9951 sym.st_other = h->other;
c152c796
AM
9952 switch (h->root.type)
9953 {
9954 default:
9955 case bfd_link_hash_new:
9956 case bfd_link_hash_warning:
9957 abort ();
9958 return FALSE;
9959
9960 case bfd_link_hash_undefined:
9961 case bfd_link_hash_undefweak:
9962 input_sec = bfd_und_section_ptr;
9963 sym.st_shndx = SHN_UNDEF;
9964 break;
9965
9966 case bfd_link_hash_defined:
9967 case bfd_link_hash_defweak:
9968 {
9969 input_sec = h->root.u.def.section;
9970 if (input_sec->output_section != NULL)
9971 {
9972 sym.st_shndx =
8b127cbc 9973 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9974 input_sec->output_section);
9975 if (sym.st_shndx == SHN_BAD)
9976 {
4eca0228 9977 _bfd_error_handler
695344c0 9978 /* xgettext:c-format */
871b3ab2 9979 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9980 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9981 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9982 eoinfo->failed = TRUE;
9983 return FALSE;
9984 }
9985
9986 /* ELF symbols in relocatable files are section relative,
9987 but in nonrelocatable files they are virtual
9988 addresses. */
9989 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9990 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9991 {
9992 sym.st_value += input_sec->output_section->vma;
9993 if (h->type == STT_TLS)
9994 {
8b127cbc 9995 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9996 if (tls_sec != NULL)
9997 sym.st_value -= tls_sec->vma;
c152c796
AM
9998 }
9999 }
10000 }
10001 else
10002 {
10003 BFD_ASSERT (input_sec->owner == NULL
10004 || (input_sec->owner->flags & DYNAMIC) != 0);
10005 sym.st_shndx = SHN_UNDEF;
10006 input_sec = bfd_und_section_ptr;
10007 }
10008 }
10009 break;
10010
10011 case bfd_link_hash_common:
10012 input_sec = h->root.u.c.p->section;
a4d8e49b 10013 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10014 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10015 break;
10016
10017 case bfd_link_hash_indirect:
10018 /* These symbols are created by symbol versioning. They point
10019 to the decorated version of the name. For example, if the
10020 symbol foo@@GNU_1.2 is the default, which should be used when
10021 foo is used with no version, then we add an indirect symbol
10022 foo which points to foo@@GNU_1.2. We ignore these symbols,
10023 since the indirected symbol is already in the hash table. */
10024 return TRUE;
10025 }
10026
b8871f35
L
10027 if (type == STT_COMMON || type == STT_OBJECT)
10028 switch (h->root.type)
10029 {
10030 case bfd_link_hash_common:
10031 type = elf_link_convert_common_type (flinfo->info, type);
10032 break;
10033 case bfd_link_hash_defined:
10034 case bfd_link_hash_defweak:
10035 if (bed->common_definition (&sym))
10036 type = elf_link_convert_common_type (flinfo->info, type);
10037 else
10038 type = STT_OBJECT;
10039 break;
10040 case bfd_link_hash_undefined:
10041 case bfd_link_hash_undefweak:
10042 break;
10043 default:
10044 abort ();
10045 }
10046
4deb8f71 10047 if (h->forced_local)
b8871f35
L
10048 {
10049 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10050 /* Turn off visibility on local symbol. */
10051 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10052 }
10053 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10054 else if (h->unique_global && h->def_regular)
10055 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10056 else if (h->root.type == bfd_link_hash_undefweak
10057 || h->root.type == bfd_link_hash_defweak)
10058 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10059 else
10060 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10061 sym.st_target_internal = h->target_internal;
10062
c152c796
AM
10063 /* Give the processor backend a chance to tweak the symbol value,
10064 and also to finish up anything that needs to be done for this
10065 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10066 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10067 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10068 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10069 && h->def_regular
0e1862bb 10070 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10071 || ((h->dynindx != -1
10072 || h->forced_local)
0e1862bb 10073 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10074 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10075 || h->root.type != bfd_link_hash_undefweak))
10076 || !h->forced_local)
8b127cbc 10077 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10078 {
10079 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10080 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10081 {
10082 eoinfo->failed = TRUE;
10083 return FALSE;
10084 }
10085 }
10086
10087 /* If we are marking the symbol as undefined, and there are no
10088 non-weak references to this symbol from a regular object, then
10089 mark the symbol as weak undefined; if there are non-weak
10090 references, mark the symbol as strong. We can't do this earlier,
10091 because it might not be marked as undefined until the
10092 finish_dynamic_symbol routine gets through with it. */
10093 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10094 && h->ref_regular
c152c796
AM
10095 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10096 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10097 {
10098 int bindtype;
b8871f35 10099 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10100
10101 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10102 if (type == STT_GNU_IFUNC)
10103 type = STT_FUNC;
c152c796 10104
f5385ebf 10105 if (h->ref_regular_nonweak)
c152c796
AM
10106 bindtype = STB_GLOBAL;
10107 else
10108 bindtype = STB_WEAK;
2955ec4c 10109 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10110 }
10111
bda987c2
CD
10112 /* If this is a symbol defined in a dynamic library, don't use the
10113 symbol size from the dynamic library. Relinking an executable
10114 against a new library may introduce gratuitous changes in the
10115 executable's symbols if we keep the size. */
10116 if (sym.st_shndx == SHN_UNDEF
10117 && !h->def_regular
10118 && h->def_dynamic)
10119 sym.st_size = 0;
10120
c152c796
AM
10121 /* If a non-weak symbol with non-default visibility is not defined
10122 locally, it is a fatal error. */
0e1862bb 10123 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10124 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10125 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10126 && h->root.type == bfd_link_hash_undefined
f5385ebf 10127 && !h->def_regular)
c152c796 10128 {
17d078c5
AM
10129 const char *msg;
10130
10131 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10132 /* xgettext:c-format */
871b3ab2 10133 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10134 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10135 /* xgettext:c-format */
871b3ab2 10136 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10137 else
695344c0 10138 /* xgettext:c-format */
871b3ab2 10139 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10140 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10141 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10142 eoinfo->failed = TRUE;
10143 return FALSE;
10144 }
10145
10146 /* If this symbol should be put in the .dynsym section, then put it
10147 there now. We already know the symbol index. We also fill in
10148 the entry in the .hash section. */
1c2649ed
EB
10149 if (h->dynindx != -1
10150 && elf_hash_table (flinfo->info)->dynamic_sections_created
10151 && elf_hash_table (flinfo->info)->dynsym != NULL
10152 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10153 {
c152c796
AM
10154 bfd_byte *esym;
10155
90c984fc
L
10156 /* Since there is no version information in the dynamic string,
10157 if there is no version info in symbol version section, we will
1659f720 10158 have a run-time problem if not linking executable, referenced
4deb8f71 10159 by shared library, or not bound locally. */
1659f720 10160 if (h->verinfo.verdef == NULL
0e1862bb 10161 && (!bfd_link_executable (flinfo->info)
1659f720
L
10162 || h->ref_dynamic
10163 || !h->def_regular))
90c984fc
L
10164 {
10165 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10166
10167 if (p && p [1] != '\0')
10168 {
4eca0228 10169 _bfd_error_handler
695344c0 10170 /* xgettext:c-format */
9793eb77 10171 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10172 flinfo->output_bfd, h->root.root.string);
10173 eoinfo->failed = TRUE;
10174 return FALSE;
10175 }
10176 }
10177
c152c796 10178 sym.st_name = h->dynstr_index;
cae1fbbb
L
10179 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10180 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10181 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10182 {
10183 eoinfo->failed = TRUE;
10184 return FALSE;
10185 }
8b127cbc 10186 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10187
8b127cbc 10188 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10189 {
10190 size_t hash_entry_size;
10191 bfd_byte *bucketpos;
10192 bfd_vma chain;
41198d0c
L
10193 size_t bucketcount;
10194 size_t bucket;
10195
8b127cbc 10196 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10197 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10198
10199 hash_entry_size
8b127cbc
AM
10200 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10201 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10202 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10203 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10204 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10205 bucketpos);
10206 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10207 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10208 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10209 }
c152c796 10210
8b127cbc 10211 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10212 {
10213 Elf_Internal_Versym iversym;
10214 Elf_External_Versym *eversym;
10215
f5385ebf 10216 if (!h->def_regular)
c152c796 10217 {
7b20f099
AM
10218 if (h->verinfo.verdef == NULL
10219 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10220 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10221 iversym.vs_vers = 0;
10222 else
10223 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10224 }
10225 else
10226 {
10227 if (h->verinfo.vertree == NULL)
10228 iversym.vs_vers = 1;
10229 else
10230 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10231 if (flinfo->info->create_default_symver)
3e3b46e5 10232 iversym.vs_vers++;
c152c796
AM
10233 }
10234
422f1182 10235 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10236 defined locally. */
422f1182 10237 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10238 iversym.vs_vers |= VERSYM_HIDDEN;
10239
8b127cbc 10240 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10241 eversym += h->dynindx;
8b127cbc 10242 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10243 }
10244 }
10245
d983c8c5
AM
10246 /* If the symbol is undefined, and we didn't output it to .dynsym,
10247 strip it from .symtab too. Obviously we can't do this for
10248 relocatable output or when needed for --emit-relocs. */
10249 else if (input_sec == bfd_und_section_ptr
10250 && h->indx != -2
66cae560
NC
10251 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10252 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10253 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10254 return TRUE;
66cae560 10255
d983c8c5
AM
10256 /* Also strip others that we couldn't earlier due to dynamic symbol
10257 processing. */
10258 if (strip)
10259 return TRUE;
10260 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10261 return TRUE;
10262
2ec55de3
AM
10263 /* Output a FILE symbol so that following locals are not associated
10264 with the wrong input file. We need one for forced local symbols
10265 if we've seen more than one FILE symbol or when we have exactly
10266 one FILE symbol but global symbols are present in a file other
10267 than the one with the FILE symbol. We also need one if linker
10268 defined symbols are present. In practice these conditions are
10269 always met, so just emit the FILE symbol unconditionally. */
10270 if (eoinfo->localsyms
10271 && !eoinfo->file_sym_done
10272 && eoinfo->flinfo->filesym_count != 0)
10273 {
10274 Elf_Internal_Sym fsym;
10275
10276 memset (&fsym, 0, sizeof (fsym));
10277 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10278 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10279 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10280 bfd_und_section_ptr, NULL))
2ec55de3
AM
10281 return FALSE;
10282
10283 eoinfo->file_sym_done = TRUE;
10284 }
10285
8b127cbc 10286 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10287 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10288 input_sec, h);
6e0b88f1 10289 if (ret == 0)
c152c796
AM
10290 {
10291 eoinfo->failed = TRUE;
10292 return FALSE;
10293 }
6e0b88f1
AM
10294 else if (ret == 1)
10295 h->indx = indx;
10296 else if (h->indx == -2)
10297 abort();
c152c796
AM
10298
10299 return TRUE;
10300}
10301
cdd3575c
AM
10302/* Return TRUE if special handling is done for relocs in SEC against
10303 symbols defined in discarded sections. */
10304
c152c796
AM
10305static bfd_boolean
10306elf_section_ignore_discarded_relocs (asection *sec)
10307{
10308 const struct elf_backend_data *bed;
10309
cdd3575c
AM
10310 switch (sec->sec_info_type)
10311 {
dbaa2011
AM
10312 case SEC_INFO_TYPE_STABS:
10313 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10314 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10315 return TRUE;
10316 default:
10317 break;
10318 }
c152c796
AM
10319
10320 bed = get_elf_backend_data (sec->owner);
10321 if (bed->elf_backend_ignore_discarded_relocs != NULL
10322 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10323 return TRUE;
10324
10325 return FALSE;
10326}
10327
9e66c942
AM
10328/* Return a mask saying how ld should treat relocations in SEC against
10329 symbols defined in discarded sections. If this function returns
10330 COMPLAIN set, ld will issue a warning message. If this function
10331 returns PRETEND set, and the discarded section was link-once and the
10332 same size as the kept link-once section, ld will pretend that the
10333 symbol was actually defined in the kept section. Otherwise ld will
10334 zero the reloc (at least that is the intent, but some cooperation by
10335 the target dependent code is needed, particularly for REL targets). */
10336
8a696751
AM
10337unsigned int
10338_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10339{
9e66c942 10340 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10341 return PRETEND;
cdd3575c
AM
10342
10343 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10344 return 0;
cdd3575c
AM
10345
10346 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10347 return 0;
cdd3575c 10348
9e66c942 10349 return COMPLAIN | PRETEND;
cdd3575c
AM
10350}
10351
3d7f7666
L
10352/* Find a match between a section and a member of a section group. */
10353
10354static asection *
c0f00686
L
10355match_group_member (asection *sec, asection *group,
10356 struct bfd_link_info *info)
3d7f7666
L
10357{
10358 asection *first = elf_next_in_group (group);
10359 asection *s = first;
10360
10361 while (s != NULL)
10362 {
c0f00686 10363 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10364 return s;
10365
83180ade 10366 s = elf_next_in_group (s);
3d7f7666
L
10367 if (s == first)
10368 break;
10369 }
10370
10371 return NULL;
10372}
10373
01b3c8ab 10374/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10375 to replace it. Return the replacement if it is OK. Otherwise return
10376 NULL. */
01b3c8ab
L
10377
10378asection *
c0f00686 10379_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10380{
10381 asection *kept;
10382
10383 kept = sec->kept_section;
10384 if (kept != NULL)
10385 {
c2370991 10386 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10387 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10388 if (kept != NULL
10389 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10390 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10391 kept = NULL;
c2370991 10392 sec->kept_section = kept;
01b3c8ab
L
10393 }
10394 return kept;
10395}
10396
c152c796
AM
10397/* Link an input file into the linker output file. This function
10398 handles all the sections and relocations of the input file at once.
10399 This is so that we only have to read the local symbols once, and
10400 don't have to keep them in memory. */
10401
10402static bfd_boolean
8b127cbc 10403elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10404{
ece5ef60 10405 int (*relocate_section)
c152c796
AM
10406 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10407 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10408 bfd *output_bfd;
10409 Elf_Internal_Shdr *symtab_hdr;
10410 size_t locsymcount;
10411 size_t extsymoff;
10412 Elf_Internal_Sym *isymbuf;
10413 Elf_Internal_Sym *isym;
10414 Elf_Internal_Sym *isymend;
10415 long *pindex;
10416 asection **ppsection;
10417 asection *o;
10418 const struct elf_backend_data *bed;
c152c796 10419 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10420 bfd_size_type address_size;
10421 bfd_vma r_type_mask;
10422 int r_sym_shift;
ffbc01cc 10423 bfd_boolean have_file_sym = FALSE;
c152c796 10424
8b127cbc 10425 output_bfd = flinfo->output_bfd;
c152c796
AM
10426 bed = get_elf_backend_data (output_bfd);
10427 relocate_section = bed->elf_backend_relocate_section;
10428
10429 /* If this is a dynamic object, we don't want to do anything here:
10430 we don't want the local symbols, and we don't want the section
10431 contents. */
10432 if ((input_bfd->flags & DYNAMIC) != 0)
10433 return TRUE;
10434
c152c796
AM
10435 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10436 if (elf_bad_symtab (input_bfd))
10437 {
10438 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10439 extsymoff = 0;
10440 }
10441 else
10442 {
10443 locsymcount = symtab_hdr->sh_info;
10444 extsymoff = symtab_hdr->sh_info;
10445 }
10446
10447 /* Read the local symbols. */
10448 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10449 if (isymbuf == NULL && locsymcount != 0)
10450 {
10451 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10452 flinfo->internal_syms,
10453 flinfo->external_syms,
10454 flinfo->locsym_shndx);
c152c796
AM
10455 if (isymbuf == NULL)
10456 return FALSE;
10457 }
10458
10459 /* Find local symbol sections and adjust values of symbols in
10460 SEC_MERGE sections. Write out those local symbols we know are
10461 going into the output file. */
10462 isymend = isymbuf + locsymcount;
8b127cbc 10463 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10464 isym < isymend;
10465 isym++, pindex++, ppsection++)
10466 {
10467 asection *isec;
10468 const char *name;
10469 Elf_Internal_Sym osym;
6e0b88f1
AM
10470 long indx;
10471 int ret;
c152c796
AM
10472
10473 *pindex = -1;
10474
10475 if (elf_bad_symtab (input_bfd))
10476 {
10477 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10478 {
10479 *ppsection = NULL;
10480 continue;
10481 }
10482 }
10483
10484 if (isym->st_shndx == SHN_UNDEF)
10485 isec = bfd_und_section_ptr;
c152c796
AM
10486 else if (isym->st_shndx == SHN_ABS)
10487 isec = bfd_abs_section_ptr;
10488 else if (isym->st_shndx == SHN_COMMON)
10489 isec = bfd_com_section_ptr;
10490 else
10491 {
cb33740c
AM
10492 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10493 if (isec == NULL)
10494 {
10495 /* Don't attempt to output symbols with st_shnx in the
10496 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10497 isec = bfd_und_section_ptr;
cb33740c 10498 }
dbaa2011 10499 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10500 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10501 isym->st_value =
10502 _bfd_merged_section_offset (output_bfd, &isec,
10503 elf_section_data (isec)->sec_info,
10504 isym->st_value);
c152c796
AM
10505 }
10506
10507 *ppsection = isec;
10508
d983c8c5
AM
10509 /* Don't output the first, undefined, symbol. In fact, don't
10510 output any undefined local symbol. */
10511 if (isec == bfd_und_section_ptr)
c152c796
AM
10512 continue;
10513
10514 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10515 {
10516 /* We never output section symbols. Instead, we use the
10517 section symbol of the corresponding section in the output
10518 file. */
10519 continue;
10520 }
10521
10522 /* If we are stripping all symbols, we don't want to output this
10523 one. */
8b127cbc 10524 if (flinfo->info->strip == strip_all)
c152c796
AM
10525 continue;
10526
10527 /* If we are discarding all local symbols, we don't want to
10528 output this one. If we are generating a relocatable output
10529 file, then some of the local symbols may be required by
10530 relocs; we output them below as we discover that they are
10531 needed. */
8b127cbc 10532 if (flinfo->info->discard == discard_all)
c152c796
AM
10533 continue;
10534
10535 /* If this symbol is defined in a section which we are
f02571c5
AM
10536 discarding, we don't need to keep it. */
10537 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10538 && isym->st_shndx < SHN_LORESERVE
10539 && bfd_section_removed_from_list (output_bfd,
10540 isec->output_section))
e75a280b
L
10541 continue;
10542
c152c796
AM
10543 /* Get the name of the symbol. */
10544 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10545 isym->st_name);
10546 if (name == NULL)
10547 return FALSE;
10548
10549 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10550 if ((flinfo->info->strip == strip_some
10551 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10552 == NULL))
8b127cbc 10553 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10554 && (isec->flags & SEC_MERGE)
10555 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10556 || flinfo->info->discard == discard_l)
c152c796
AM
10557 && bfd_is_local_label_name (input_bfd, name)))
10558 continue;
10559
ffbc01cc
AM
10560 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10561 {
ce875075
AM
10562 if (input_bfd->lto_output)
10563 /* -flto puts a temp file name here. This means builds
10564 are not reproducible. Discard the symbol. */
10565 continue;
ffbc01cc
AM
10566 have_file_sym = TRUE;
10567 flinfo->filesym_count += 1;
10568 }
10569 if (!have_file_sym)
10570 {
10571 /* In the absence of debug info, bfd_find_nearest_line uses
10572 FILE symbols to determine the source file for local
10573 function symbols. Provide a FILE symbol here if input
10574 files lack such, so that their symbols won't be
10575 associated with a previous input file. It's not the
10576 source file, but the best we can do. */
10577 have_file_sym = TRUE;
10578 flinfo->filesym_count += 1;
10579 memset (&osym, 0, sizeof (osym));
10580 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10581 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10582 if (!elf_link_output_symstrtab (flinfo,
10583 (input_bfd->lto_output ? NULL
10584 : input_bfd->filename),
10585 &osym, bfd_abs_section_ptr,
10586 NULL))
ffbc01cc
AM
10587 return FALSE;
10588 }
10589
c152c796
AM
10590 osym = *isym;
10591
10592 /* Adjust the section index for the output file. */
10593 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10594 isec->output_section);
10595 if (osym.st_shndx == SHN_BAD)
10596 return FALSE;
10597
c152c796
AM
10598 /* ELF symbols in relocatable files are section relative, but
10599 in executable files they are virtual addresses. Note that
10600 this code assumes that all ELF sections have an associated
10601 BFD section with a reasonable value for output_offset; below
10602 we assume that they also have a reasonable value for
10603 output_section. Any special sections must be set up to meet
10604 these requirements. */
10605 osym.st_value += isec->output_offset;
0e1862bb 10606 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10607 {
10608 osym.st_value += isec->output_section->vma;
10609 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10610 {
10611 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10612 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10613 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10614 else
10615 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10616 STT_NOTYPE);
c152c796
AM
10617 }
10618 }
10619
6e0b88f1 10620 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10621 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10622 if (ret == 0)
c152c796 10623 return FALSE;
6e0b88f1
AM
10624 else if (ret == 1)
10625 *pindex = indx;
c152c796
AM
10626 }
10627
310fd250
L
10628 if (bed->s->arch_size == 32)
10629 {
10630 r_type_mask = 0xff;
10631 r_sym_shift = 8;
10632 address_size = 4;
10633 }
10634 else
10635 {
10636 r_type_mask = 0xffffffff;
10637 r_sym_shift = 32;
10638 address_size = 8;
10639 }
10640
c152c796
AM
10641 /* Relocate the contents of each section. */
10642 sym_hashes = elf_sym_hashes (input_bfd);
10643 for (o = input_bfd->sections; o != NULL; o = o->next)
10644 {
10645 bfd_byte *contents;
10646
10647 if (! o->linker_mark)
10648 {
10649 /* This section was omitted from the link. */
10650 continue;
10651 }
10652
7bdf4127 10653 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10654 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10655 {
10656 /* Deal with the group signature symbol. */
10657 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10658 unsigned long symndx = sec_data->this_hdr.sh_info;
10659 asection *osec = o->output_section;
10660
7bdf4127 10661 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10662 if (symndx >= locsymcount
10663 || (elf_bad_symtab (input_bfd)
8b127cbc 10664 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10665 {
10666 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10667 while (h->root.type == bfd_link_hash_indirect
10668 || h->root.type == bfd_link_hash_warning)
10669 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10670 /* Arrange for symbol to be output. */
10671 h->indx = -2;
10672 elf_section_data (osec)->this_hdr.sh_info = -2;
10673 }
10674 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10675 {
10676 /* We'll use the output section target_index. */
8b127cbc 10677 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10678 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10679 }
10680 else
10681 {
8b127cbc 10682 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10683 {
10684 /* Otherwise output the local symbol now. */
10685 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10686 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10687 const char *name;
6e0b88f1
AM
10688 long indx;
10689 int ret;
bcacc0f5
AM
10690
10691 name = bfd_elf_string_from_elf_section (input_bfd,
10692 symtab_hdr->sh_link,
10693 sym.st_name);
10694 if (name == NULL)
10695 return FALSE;
10696
10697 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10698 sec);
10699 if (sym.st_shndx == SHN_BAD)
10700 return FALSE;
10701
10702 sym.st_value += o->output_offset;
10703
6e0b88f1 10704 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10705 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10706 NULL);
6e0b88f1 10707 if (ret == 0)
bcacc0f5 10708 return FALSE;
6e0b88f1 10709 else if (ret == 1)
8b127cbc 10710 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10711 else
10712 abort ();
bcacc0f5
AM
10713 }
10714 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10715 = flinfo->indices[symndx];
bcacc0f5
AM
10716 }
10717 }
10718
c152c796 10719 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10720 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10721 continue;
10722
10723 if ((o->flags & SEC_LINKER_CREATED) != 0)
10724 {
10725 /* Section was created by _bfd_elf_link_create_dynamic_sections
10726 or somesuch. */
10727 continue;
10728 }
10729
10730 /* Get the contents of the section. They have been cached by a
10731 relaxation routine. Note that o is a section in an input
10732 file, so the contents field will not have been set by any of
10733 the routines which work on output files. */
10734 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10735 {
10736 contents = elf_section_data (o)->this_hdr.contents;
10737 if (bed->caches_rawsize
10738 && o->rawsize != 0
10739 && o->rawsize < o->size)
10740 {
10741 memcpy (flinfo->contents, contents, o->rawsize);
10742 contents = flinfo->contents;
10743 }
10744 }
c152c796
AM
10745 else
10746 {
8b127cbc 10747 contents = flinfo->contents;
4a114e3e 10748 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10749 return FALSE;
10750 }
10751
10752 if ((o->flags & SEC_RELOC) != 0)
10753 {
10754 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10755 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10756 int action_discarded;
ece5ef60 10757 int ret;
c152c796
AM
10758
10759 /* Get the swapped relocs. */
10760 internal_relocs
8b127cbc
AM
10761 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10762 flinfo->internal_relocs, FALSE);
c152c796
AM
10763 if (internal_relocs == NULL
10764 && o->reloc_count > 0)
10765 return FALSE;
10766
310fd250
L
10767 /* We need to reverse-copy input .ctors/.dtors sections if
10768 they are placed in .init_array/.finit_array for output. */
10769 if (o->size > address_size
10770 && ((strncmp (o->name, ".ctors", 6) == 0
10771 && strcmp (o->output_section->name,
10772 ".init_array") == 0)
10773 || (strncmp (o->name, ".dtors", 6) == 0
10774 && strcmp (o->output_section->name,
10775 ".fini_array") == 0))
10776 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10777 {
056bafd4
MR
10778 if (o->size * bed->s->int_rels_per_ext_rel
10779 != o->reloc_count * address_size)
310fd250 10780 {
4eca0228 10781 _bfd_error_handler
695344c0 10782 /* xgettext:c-format */
871b3ab2 10783 (_("error: %pB: size of section %pA is not "
310fd250
L
10784 "multiple of address size"),
10785 input_bfd, o);
8c6716e5 10786 bfd_set_error (bfd_error_bad_value);
310fd250
L
10787 return FALSE;
10788 }
10789 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10790 }
10791
0f02bbd9 10792 action_discarded = -1;
c152c796 10793 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10794 action_discarded = (*bed->action_discarded) (o);
10795
10796 /* Run through the relocs evaluating complex reloc symbols and
10797 looking for relocs against symbols from discarded sections
10798 or section symbols from removed link-once sections.
10799 Complain about relocs against discarded sections. Zero
10800 relocs against removed link-once sections. */
10801
10802 rel = internal_relocs;
056bafd4 10803 relend = rel + o->reloc_count;
0f02bbd9 10804 for ( ; rel < relend; rel++)
c152c796 10805 {
0f02bbd9
AM
10806 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10807 unsigned int s_type;
10808 asection **ps, *sec;
10809 struct elf_link_hash_entry *h = NULL;
10810 const char *sym_name;
c152c796 10811
0f02bbd9
AM
10812 if (r_symndx == STN_UNDEF)
10813 continue;
c152c796 10814
0f02bbd9
AM
10815 if (r_symndx >= locsymcount
10816 || (elf_bad_symtab (input_bfd)
8b127cbc 10817 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10818 {
10819 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10820
0f02bbd9
AM
10821 /* Badly formatted input files can contain relocs that
10822 reference non-existant symbols. Check here so that
10823 we do not seg fault. */
10824 if (h == NULL)
c152c796 10825 {
4eca0228 10826 _bfd_error_handler
695344c0 10827 /* xgettext:c-format */
2dcf00ce 10828 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10829 "that references a non-existent global symbol"),
2dcf00ce 10830 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10831 bfd_set_error (bfd_error_bad_value);
10832 return FALSE;
10833 }
3b36f7e6 10834
0f02bbd9
AM
10835 while (h->root.type == bfd_link_hash_indirect
10836 || h->root.type == bfd_link_hash_warning)
10837 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10838
0f02bbd9 10839 s_type = h->type;
cdd3575c 10840
9e2dec47 10841 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10842 mark the symbol as undefined. Note that the
10843 linker may attach linker created dynamic sections
10844 to the plugin bfd. Symbols defined in linker
10845 created sections are not plugin symbols. */
bc4e12de 10846 if ((h->root.non_ir_ref_regular
4070765b 10847 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10848 && (h->root.type == bfd_link_hash_defined
10849 || h->root.type == bfd_link_hash_defweak)
10850 && (h->root.u.def.section->flags
10851 & SEC_LINKER_CREATED) == 0
10852 && h->root.u.def.section->owner != NULL
10853 && (h->root.u.def.section->owner->flags
10854 & BFD_PLUGIN) != 0)
10855 {
10856 h->root.type = bfd_link_hash_undefined;
10857 h->root.u.undef.abfd = h->root.u.def.section->owner;
10858 }
10859
0f02bbd9
AM
10860 ps = NULL;
10861 if (h->root.type == bfd_link_hash_defined
10862 || h->root.type == bfd_link_hash_defweak)
10863 ps = &h->root.u.def.section;
10864
10865 sym_name = h->root.root.string;
10866 }
10867 else
10868 {
10869 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10870
10871 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10872 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10873 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10874 sym, *ps);
10875 }
c152c796 10876
c301e700 10877 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10878 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10879 {
10880 bfd_vma val;
10881 bfd_vma dot = (rel->r_offset
10882 + o->output_offset + o->output_section->vma);
10883#ifdef DEBUG
10884 printf ("Encountered a complex symbol!");
10885 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10886 input_bfd->filename, o->name,
10887 (long) (rel - internal_relocs));
0f02bbd9
AM
10888 printf (" symbol: idx %8.8lx, name %s\n",
10889 r_symndx, sym_name);
10890 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10891 (unsigned long) rel->r_info,
10892 (unsigned long) rel->r_offset);
10893#endif
8b127cbc 10894 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10895 isymbuf, locsymcount, s_type == STT_SRELC))
10896 return FALSE;
10897
10898 /* Symbol evaluated OK. Update to absolute value. */
10899 set_symbol_value (input_bfd, isymbuf, locsymcount,
10900 r_symndx, val);
10901 continue;
10902 }
10903
10904 if (action_discarded != -1 && ps != NULL)
10905 {
cdd3575c
AM
10906 /* Complain if the definition comes from a
10907 discarded section. */
dbaa2011 10908 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10909 {
cf35638d 10910 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10911 if (action_discarded & COMPLAIN)
8b127cbc 10912 (*flinfo->info->callbacks->einfo)
695344c0 10913 /* xgettext:c-format */
871b3ab2
AM
10914 (_("%X`%s' referenced in section `%pA' of %pB: "
10915 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10916 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10917
87e5235d 10918 /* Try to do the best we can to support buggy old
e0ae6d6f 10919 versions of gcc. Pretend that the symbol is
87e5235d
AM
10920 really defined in the kept linkonce section.
10921 FIXME: This is quite broken. Modifying the
10922 symbol here means we will be changing all later
e0ae6d6f 10923 uses of the symbol, not just in this section. */
0f02bbd9 10924 if (action_discarded & PRETEND)
87e5235d 10925 {
01b3c8ab
L
10926 asection *kept;
10927
c0f00686 10928 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10929 flinfo->info);
01b3c8ab 10930 if (kept != NULL)
87e5235d
AM
10931 {
10932 *ps = kept;
10933 continue;
10934 }
10935 }
c152c796
AM
10936 }
10937 }
10938 }
10939
10940 /* Relocate the section by invoking a back end routine.
10941
10942 The back end routine is responsible for adjusting the
10943 section contents as necessary, and (if using Rela relocs
10944 and generating a relocatable output file) adjusting the
10945 reloc addend as necessary.
10946
10947 The back end routine does not have to worry about setting
10948 the reloc address or the reloc symbol index.
10949
10950 The back end routine is given a pointer to the swapped in
10951 internal symbols, and can access the hash table entries
10952 for the external symbols via elf_sym_hashes (input_bfd).
10953
10954 When generating relocatable output, the back end routine
10955 must handle STB_LOCAL/STT_SECTION symbols specially. The
10956 output symbol is going to be a section symbol
10957 corresponding to the output section, which will require
10958 the addend to be adjusted. */
10959
8b127cbc 10960 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10961 input_bfd, o, contents,
10962 internal_relocs,
10963 isymbuf,
8b127cbc 10964 flinfo->sections);
ece5ef60 10965 if (!ret)
c152c796
AM
10966 return FALSE;
10967
ece5ef60 10968 if (ret == 2
0e1862bb 10969 || bfd_link_relocatable (flinfo->info)
8b127cbc 10970 || flinfo->info->emitrelocations)
c152c796
AM
10971 {
10972 Elf_Internal_Rela *irela;
d4730f92 10973 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10974 bfd_vma last_offset;
10975 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10976 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10977 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10978 unsigned int next_erel;
c152c796 10979 bfd_boolean rela_normal;
d4730f92 10980 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10981
d4730f92
BS
10982 esdi = elf_section_data (o);
10983 esdo = elf_section_data (o->output_section);
10984 rela_normal = FALSE;
c152c796
AM
10985
10986 /* Adjust the reloc addresses and symbol indices. */
10987
10988 irela = internal_relocs;
056bafd4 10989 irelaend = irela + o->reloc_count;
d4730f92
BS
10990 rel_hash = esdo->rel.hashes + esdo->rel.count;
10991 /* We start processing the REL relocs, if any. When we reach
10992 IRELAMID in the loop, we switch to the RELA relocs. */
10993 irelamid = irela;
10994 if (esdi->rel.hdr != NULL)
10995 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10996 * bed->s->int_rels_per_ext_rel);
eac338cf 10997 rel_hash_list = rel_hash;
d4730f92 10998 rela_hash_list = NULL;
c152c796 10999 last_offset = o->output_offset;
0e1862bb 11000 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11001 last_offset += o->output_section->vma;
11002 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11003 {
11004 unsigned long r_symndx;
11005 asection *sec;
11006 Elf_Internal_Sym sym;
11007
11008 if (next_erel == bed->s->int_rels_per_ext_rel)
11009 {
11010 rel_hash++;
11011 next_erel = 0;
11012 }
11013
d4730f92
BS
11014 if (irela == irelamid)
11015 {
11016 rel_hash = esdo->rela.hashes + esdo->rela.count;
11017 rela_hash_list = rel_hash;
11018 rela_normal = bed->rela_normal;
11019 }
11020
c152c796 11021 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11022 flinfo->info, o,
c152c796
AM
11023 irela->r_offset);
11024 if (irela->r_offset >= (bfd_vma) -2)
11025 {
11026 /* This is a reloc for a deleted entry or somesuch.
11027 Turn it into an R_*_NONE reloc, at the same
11028 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11029 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11030 being ordered. */
11031 irela->r_offset = last_offset;
11032 irela->r_info = 0;
11033 irela->r_addend = 0;
11034 continue;
11035 }
11036
11037 irela->r_offset += o->output_offset;
11038
11039 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11040 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11041 irela->r_offset += o->output_section->vma;
11042
11043 last_offset = irela->r_offset;
11044
11045 r_symndx = irela->r_info >> r_sym_shift;
11046 if (r_symndx == STN_UNDEF)
11047 continue;
11048
11049 if (r_symndx >= locsymcount
11050 || (elf_bad_symtab (input_bfd)
8b127cbc 11051 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11052 {
11053 struct elf_link_hash_entry *rh;
11054 unsigned long indx;
11055
11056 /* This is a reloc against a global symbol. We
11057 have not yet output all the local symbols, so
11058 we do not know the symbol index of any global
11059 symbol. We set the rel_hash entry for this
11060 reloc to point to the global hash table entry
11061 for this symbol. The symbol index is then
ee75fd95 11062 set at the end of bfd_elf_final_link. */
c152c796
AM
11063 indx = r_symndx - extsymoff;
11064 rh = elf_sym_hashes (input_bfd)[indx];
11065 while (rh->root.type == bfd_link_hash_indirect
11066 || rh->root.type == bfd_link_hash_warning)
11067 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11068
11069 /* Setting the index to -2 tells
11070 elf_link_output_extsym that this symbol is
11071 used by a reloc. */
11072 BFD_ASSERT (rh->indx < 0);
11073 rh->indx = -2;
c152c796
AM
11074 *rel_hash = rh;
11075
11076 continue;
11077 }
11078
11079 /* This is a reloc against a local symbol. */
11080
11081 *rel_hash = NULL;
11082 sym = isymbuf[r_symndx];
8b127cbc 11083 sec = flinfo->sections[r_symndx];
c152c796
AM
11084 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11085 {
11086 /* I suppose the backend ought to fill in the
11087 section of any STT_SECTION symbol against a
6a8d1586 11088 processor specific section. */
cf35638d 11089 r_symndx = STN_UNDEF;
6a8d1586
AM
11090 if (bfd_is_abs_section (sec))
11091 ;
c152c796
AM
11092 else if (sec == NULL || sec->owner == NULL)
11093 {
11094 bfd_set_error (bfd_error_bad_value);
11095 return FALSE;
11096 }
11097 else
11098 {
6a8d1586
AM
11099 asection *osec = sec->output_section;
11100
11101 /* If we have discarded a section, the output
11102 section will be the absolute section. In
ab96bf03
AM
11103 case of discarded SEC_MERGE sections, use
11104 the kept section. relocate_section should
11105 have already handled discarded linkonce
11106 sections. */
6a8d1586
AM
11107 if (bfd_is_abs_section (osec)
11108 && sec->kept_section != NULL
11109 && sec->kept_section->output_section != NULL)
11110 {
11111 osec = sec->kept_section->output_section;
11112 irela->r_addend -= osec->vma;
11113 }
11114
11115 if (!bfd_is_abs_section (osec))
11116 {
11117 r_symndx = osec->target_index;
cf35638d 11118 if (r_symndx == STN_UNDEF)
74541ad4 11119 {
051d833a
AM
11120 irela->r_addend += osec->vma;
11121 osec = _bfd_nearby_section (output_bfd, osec,
11122 osec->vma);
11123 irela->r_addend -= osec->vma;
11124 r_symndx = osec->target_index;
74541ad4 11125 }
6a8d1586 11126 }
c152c796
AM
11127 }
11128
11129 /* Adjust the addend according to where the
11130 section winds up in the output section. */
11131 if (rela_normal)
11132 irela->r_addend += sec->output_offset;
11133 }
11134 else
11135 {
8b127cbc 11136 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11137 {
11138 unsigned long shlink;
11139 const char *name;
11140 asection *osec;
6e0b88f1 11141 long indx;
c152c796 11142
8b127cbc 11143 if (flinfo->info->strip == strip_all)
c152c796
AM
11144 {
11145 /* You can't do ld -r -s. */
11146 bfd_set_error (bfd_error_invalid_operation);
11147 return FALSE;
11148 }
11149
11150 /* This symbol was skipped earlier, but
11151 since it is needed by a reloc, we
11152 must output it now. */
11153 shlink = symtab_hdr->sh_link;
11154 name = (bfd_elf_string_from_elf_section
11155 (input_bfd, shlink, sym.st_name));
11156 if (name == NULL)
11157 return FALSE;
11158
11159 osec = sec->output_section;
11160 sym.st_shndx =
11161 _bfd_elf_section_from_bfd_section (output_bfd,
11162 osec);
11163 if (sym.st_shndx == SHN_BAD)
11164 return FALSE;
11165
11166 sym.st_value += sec->output_offset;
0e1862bb 11167 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11168 {
11169 sym.st_value += osec->vma;
11170 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11171 {
102def4d
AM
11172 struct elf_link_hash_table *htab
11173 = elf_hash_table (flinfo->info);
11174
c152c796
AM
11175 /* STT_TLS symbols are relative to PT_TLS
11176 segment base. */
102def4d
AM
11177 if (htab->tls_sec != NULL)
11178 sym.st_value -= htab->tls_sec->vma;
11179 else
11180 sym.st_info
11181 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11182 STT_NOTYPE);
c152c796
AM
11183 }
11184 }
11185
6e0b88f1 11186 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11187 ret = elf_link_output_symstrtab (flinfo, name,
11188 &sym, sec,
11189 NULL);
6e0b88f1 11190 if (ret == 0)
c152c796 11191 return FALSE;
6e0b88f1 11192 else if (ret == 1)
8b127cbc 11193 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11194 else
11195 abort ();
c152c796
AM
11196 }
11197
8b127cbc 11198 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11199 }
11200
11201 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11202 | (irela->r_info & r_type_mask));
11203 }
11204
11205 /* Swap out the relocs. */
d4730f92
BS
11206 input_rel_hdr = esdi->rel.hdr;
11207 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11208 {
d4730f92
BS
11209 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11210 input_rel_hdr,
11211 internal_relocs,
11212 rel_hash_list))
11213 return FALSE;
c152c796
AM
11214 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11215 * bed->s->int_rels_per_ext_rel);
eac338cf 11216 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11217 }
11218
11219 input_rela_hdr = esdi->rela.hdr;
11220 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11221 {
eac338cf 11222 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11223 input_rela_hdr,
eac338cf 11224 internal_relocs,
d4730f92 11225 rela_hash_list))
c152c796
AM
11226 return FALSE;
11227 }
11228 }
11229 }
11230
11231 /* Write out the modified section contents. */
11232 if (bed->elf_backend_write_section
8b127cbc 11233 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11234 contents))
c152c796
AM
11235 {
11236 /* Section written out. */
11237 }
11238 else switch (o->sec_info_type)
11239 {
dbaa2011 11240 case SEC_INFO_TYPE_STABS:
c152c796
AM
11241 if (! (_bfd_write_section_stabs
11242 (output_bfd,
8b127cbc 11243 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11244 o, &elf_section_data (o)->sec_info, contents)))
11245 return FALSE;
11246 break;
dbaa2011 11247 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11248 if (! _bfd_write_merged_section (output_bfd, o,
11249 elf_section_data (o)->sec_info))
11250 return FALSE;
11251 break;
dbaa2011 11252 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11253 {
8b127cbc 11254 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11255 o, contents))
11256 return FALSE;
11257 }
11258 break;
2f0c68f2
CM
11259 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11260 {
11261 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11262 flinfo->info,
11263 o, contents))
11264 return FALSE;
11265 }
11266 break;
c152c796
AM
11267 default:
11268 {
310fd250
L
11269 if (! (o->flags & SEC_EXCLUDE))
11270 {
11271 file_ptr offset = (file_ptr) o->output_offset;
11272 bfd_size_type todo = o->size;
37b01f6a
DG
11273
11274 offset *= bfd_octets_per_byte (output_bfd);
11275
310fd250
L
11276 if ((o->flags & SEC_ELF_REVERSE_COPY))
11277 {
11278 /* Reverse-copy input section to output. */
11279 do
11280 {
11281 todo -= address_size;
11282 if (! bfd_set_section_contents (output_bfd,
11283 o->output_section,
11284 contents + todo,
11285 offset,
11286 address_size))
11287 return FALSE;
11288 if (todo == 0)
11289 break;
11290 offset += address_size;
11291 }
11292 while (1);
11293 }
11294 else if (! bfd_set_section_contents (output_bfd,
11295 o->output_section,
11296 contents,
11297 offset, todo))
11298 return FALSE;
11299 }
c152c796
AM
11300 }
11301 break;
11302 }
11303 }
11304
11305 return TRUE;
11306}
11307
11308/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11309 requested by the linker, and does not come from any input file. This
c152c796
AM
11310 is used to build constructor and destructor tables when linking
11311 with -Ur. */
11312
11313static bfd_boolean
11314elf_reloc_link_order (bfd *output_bfd,
11315 struct bfd_link_info *info,
11316 asection *output_section,
11317 struct bfd_link_order *link_order)
11318{
11319 reloc_howto_type *howto;
11320 long indx;
11321 bfd_vma offset;
11322 bfd_vma addend;
d4730f92 11323 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11324 struct elf_link_hash_entry **rel_hash_ptr;
11325 Elf_Internal_Shdr *rel_hdr;
11326 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11327 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11328 bfd_byte *erel;
11329 unsigned int i;
d4730f92 11330 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11331
11332 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11333 if (howto == NULL)
11334 {
11335 bfd_set_error (bfd_error_bad_value);
11336 return FALSE;
11337 }
11338
11339 addend = link_order->u.reloc.p->addend;
11340
d4730f92
BS
11341 if (esdo->rel.hdr)
11342 reldata = &esdo->rel;
11343 else if (esdo->rela.hdr)
11344 reldata = &esdo->rela;
11345 else
11346 {
11347 reldata = NULL;
11348 BFD_ASSERT (0);
11349 }
11350
c152c796 11351 /* Figure out the symbol index. */
d4730f92 11352 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11353 if (link_order->type == bfd_section_reloc_link_order)
11354 {
11355 indx = link_order->u.reloc.p->u.section->target_index;
11356 BFD_ASSERT (indx != 0);
11357 *rel_hash_ptr = NULL;
11358 }
11359 else
11360 {
11361 struct elf_link_hash_entry *h;
11362
11363 /* Treat a reloc against a defined symbol as though it were
11364 actually against the section. */
11365 h = ((struct elf_link_hash_entry *)
11366 bfd_wrapped_link_hash_lookup (output_bfd, info,
11367 link_order->u.reloc.p->u.name,
11368 FALSE, FALSE, TRUE));
11369 if (h != NULL
11370 && (h->root.type == bfd_link_hash_defined
11371 || h->root.type == bfd_link_hash_defweak))
11372 {
11373 asection *section;
11374
11375 section = h->root.u.def.section;
11376 indx = section->output_section->target_index;
11377 *rel_hash_ptr = NULL;
11378 /* It seems that we ought to add the symbol value to the
11379 addend here, but in practice it has already been added
11380 because it was passed to constructor_callback. */
11381 addend += section->output_section->vma + section->output_offset;
11382 }
11383 else if (h != NULL)
11384 {
11385 /* Setting the index to -2 tells elf_link_output_extsym that
11386 this symbol is used by a reloc. */
11387 h->indx = -2;
11388 *rel_hash_ptr = h;
11389 indx = 0;
11390 }
11391 else
11392 {
1a72702b
AM
11393 (*info->callbacks->unattached_reloc)
11394 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11395 indx = 0;
11396 }
11397 }
11398
11399 /* If this is an inplace reloc, we must write the addend into the
11400 object file. */
11401 if (howto->partial_inplace && addend != 0)
11402 {
11403 bfd_size_type size;
11404 bfd_reloc_status_type rstat;
11405 bfd_byte *buf;
11406 bfd_boolean ok;
11407 const char *sym_name;
11408
a50b1753
NC
11409 size = (bfd_size_type) bfd_get_reloc_size (howto);
11410 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11411 if (buf == NULL && size != 0)
c152c796
AM
11412 return FALSE;
11413 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11414 switch (rstat)
11415 {
11416 case bfd_reloc_ok:
11417 break;
11418
11419 default:
11420 case bfd_reloc_outofrange:
11421 abort ();
11422
11423 case bfd_reloc_overflow:
11424 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11425 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11426 else
11427 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11428 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11429 howto->name, addend, NULL, NULL,
11430 (bfd_vma) 0);
c152c796
AM
11431 break;
11432 }
37b01f6a 11433
c152c796 11434 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11435 link_order->offset
11436 * bfd_octets_per_byte (output_bfd),
11437 size);
c152c796
AM
11438 free (buf);
11439 if (! ok)
11440 return FALSE;
11441 }
11442
11443 /* The address of a reloc is relative to the section in a
11444 relocatable file, and is a virtual address in an executable
11445 file. */
11446 offset = link_order->offset;
0e1862bb 11447 if (! bfd_link_relocatable (info))
c152c796
AM
11448 offset += output_section->vma;
11449
11450 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11451 {
11452 irel[i].r_offset = offset;
11453 irel[i].r_info = 0;
11454 irel[i].r_addend = 0;
11455 }
11456 if (bed->s->arch_size == 32)
11457 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11458 else
11459 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11460
d4730f92 11461 rel_hdr = reldata->hdr;
c152c796
AM
11462 erel = rel_hdr->contents;
11463 if (rel_hdr->sh_type == SHT_REL)
11464 {
d4730f92 11465 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11466 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11467 }
11468 else
11469 {
11470 irel[0].r_addend = addend;
d4730f92 11471 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11472 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11473 }
11474
d4730f92 11475 ++reldata->count;
c152c796
AM
11476
11477 return TRUE;
11478}
11479
0b52efa6 11480
0b52efa6
PB
11481/* Compare two sections based on the locations of the sections they are
11482 linked to. Used by elf_fixup_link_order. */
11483
11484static int
8c1c5e5d 11485compare_link_order (const void *a, const void *b)
0b52efa6 11486{
8c1c5e5d
AM
11487 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11488 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11489 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11490 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11491 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11492 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11493
0b52efa6
PB
11494 if (apos < bpos)
11495 return -1;
8c1c5e5d
AM
11496 if (apos > bpos)
11497 return 1;
11498
11499 /* The only way we should get matching LMAs is when the first of two
11500 sections has zero size. */
11501 if (asec->size < bsec->size)
11502 return -1;
11503 if (asec->size > bsec->size)
11504 return 1;
11505
11506 /* If they are both zero size then they almost certainly have the same
11507 VMA and thus are not ordered with respect to each other. Test VMA
11508 anyway, and fall back to id to make the result reproducible across
11509 qsort implementations. */
11510 apos = asec->output_section->vma + asec->output_offset;
11511 bpos = bsec->output_section->vma + bsec->output_offset;
11512 if (apos < bpos)
11513 return -1;
11514 if (apos > bpos)
11515 return 1;
11516
11517 return asec->id - bsec->id;
0b52efa6
PB
11518}
11519
11520
11521/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11522 order as their linked sections. Returns false if this could not be done
11523 because an output section includes both ordered and unordered
11524 sections. Ideally we'd do this in the linker proper. */
11525
11526static bfd_boolean
11527elf_fixup_link_order (bfd *abfd, asection *o)
11528{
8c1c5e5d
AM
11529 size_t seen_linkorder;
11530 size_t seen_other;
11531 size_t n;
0b52efa6
PB
11532 struct bfd_link_order *p;
11533 bfd *sub;
0b52efa6 11534 struct bfd_link_order **sections;
d33cdfe3 11535 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11536 bfd_vma offset;
3b36f7e6 11537
d33cdfe3
L
11538 other_sec = NULL;
11539 linkorder_sec = NULL;
0b52efa6
PB
11540 seen_other = 0;
11541 seen_linkorder = 0;
8423293d 11542 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11543 {
d33cdfe3 11544 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11545 {
11546 s = p->u.indirect.section;
d33cdfe3 11547 sub = s->owner;
847d5183
AM
11548 if ((s->flags & SEC_LINKER_CREATED) == 0
11549 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11550 && elf_section_data (s) != NULL
11551 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11552 {
11553 seen_linkorder++;
11554 linkorder_sec = s;
11555 }
0b52efa6 11556 else
d33cdfe3
L
11557 {
11558 seen_other++;
11559 other_sec = s;
11560 }
0b52efa6
PB
11561 }
11562 else
11563 seen_other++;
d33cdfe3
L
11564
11565 if (seen_other && seen_linkorder)
11566 {
11567 if (other_sec && linkorder_sec)
4eca0228 11568 _bfd_error_handler
695344c0 11569 /* xgettext:c-format */
871b3ab2
AM
11570 (_("%pA has both ordered [`%pA' in %pB] "
11571 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11572 o, linkorder_sec, linkorder_sec->owner,
11573 other_sec, other_sec->owner);
d33cdfe3 11574 else
4eca0228 11575 _bfd_error_handler
871b3ab2 11576 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11577 bfd_set_error (bfd_error_bad_value);
11578 return FALSE;
11579 }
0b52efa6
PB
11580 }
11581
11582 if (!seen_linkorder)
11583 return TRUE;
11584
8c1c5e5d 11585 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11586 if (sections == NULL)
11587 return FALSE;
3b36f7e6 11588
8c1c5e5d 11589 seen_linkorder = 0;
8423293d 11590 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11591 sections[seen_linkorder++] = p;
11592
0b52efa6 11593 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11594 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11595
11596 /* Change the offsets of the sections. */
11597 offset = 0;
11598 for (n = 0; n < seen_linkorder; n++)
11599 {
8c1c5e5d 11600 bfd_vma mask;
0b52efa6 11601 s = sections[n]->u.indirect.section;
8c1c5e5d
AM
11602 mask = ~(bfd_vma) 0 << s->alignment_power;
11603 offset = (offset + ~mask) & mask;
37b01f6a 11604 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11605 sections[n]->offset = offset;
11606 offset += sections[n]->size;
11607 }
11608
4dd07732 11609 free (sections);
0b52efa6
PB
11610 return TRUE;
11611}
11612
76359541
TP
11613/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11614 Returns TRUE upon success, FALSE otherwise. */
11615
11616static bfd_boolean
11617elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11618{
11619 bfd_boolean ret = FALSE;
11620 bfd *implib_bfd;
11621 const struct elf_backend_data *bed;
11622 flagword flags;
11623 enum bfd_architecture arch;
11624 unsigned int mach;
11625 asymbol **sympp = NULL;
11626 long symsize;
11627 long symcount;
11628 long src_count;
11629 elf_symbol_type *osymbuf;
11630
11631 implib_bfd = info->out_implib_bfd;
11632 bed = get_elf_backend_data (abfd);
11633
11634 if (!bfd_set_format (implib_bfd, bfd_object))
11635 return FALSE;
11636
046734ff 11637 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11638 flags = bfd_get_file_flags (abfd);
11639 flags &= ~HAS_RELOC;
11640 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11641 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11642 return FALSE;
11643
11644 /* Copy architecture of output file to import library file. */
11645 arch = bfd_get_arch (abfd);
11646 mach = bfd_get_mach (abfd);
11647 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11648 && (abfd->target_defaulted
11649 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11650 return FALSE;
11651
11652 /* Get symbol table size. */
11653 symsize = bfd_get_symtab_upper_bound (abfd);
11654 if (symsize < 0)
11655 return FALSE;
11656
11657 /* Read in the symbol table. */
ec9bd0a2
AM
11658 sympp = (asymbol **) bfd_malloc (symsize);
11659 if (sympp == NULL)
11660 return FALSE;
11661
76359541
TP
11662 symcount = bfd_canonicalize_symtab (abfd, sympp);
11663 if (symcount < 0)
11664 goto free_sym_buf;
11665
11666 /* Allow the BFD backend to copy any private header data it
11667 understands from the output BFD to the import library BFD. */
11668 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11669 goto free_sym_buf;
11670
11671 /* Filter symbols to appear in the import library. */
11672 if (bed->elf_backend_filter_implib_symbols)
11673 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11674 symcount);
11675 else
11676 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11677 if (symcount == 0)
11678 {
5df1bc57 11679 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11680 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11681 implib_bfd);
76359541
TP
11682 goto free_sym_buf;
11683 }
11684
11685
11686 /* Make symbols absolute. */
11687 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11688 sizeof (*osymbuf));
ec9bd0a2
AM
11689 if (osymbuf == NULL)
11690 goto free_sym_buf;
11691
76359541
TP
11692 for (src_count = 0; src_count < symcount; src_count++)
11693 {
11694 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11695 sizeof (*osymbuf));
11696 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11697 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11698 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11699 osymbuf[src_count].internal_elf_sym.st_value =
11700 osymbuf[src_count].symbol.value;
11701 sympp[src_count] = &osymbuf[src_count].symbol;
11702 }
11703
11704 bfd_set_symtab (implib_bfd, sympp, symcount);
11705
11706 /* Allow the BFD backend to copy any private data it understands
11707 from the output BFD to the import library BFD. This is done last
11708 to permit the routine to look at the filtered symbol table. */
11709 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11710 goto free_sym_buf;
11711
11712 if (!bfd_close (implib_bfd))
11713 goto free_sym_buf;
11714
11715 ret = TRUE;
11716
11717free_sym_buf:
11718 free (sympp);
11719 return ret;
11720}
11721
9f7c3e5e
AM
11722static void
11723elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11724{
11725 asection *o;
11726
11727 if (flinfo->symstrtab != NULL)
ef10c3ac 11728 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11729 if (flinfo->contents != NULL)
11730 free (flinfo->contents);
11731 if (flinfo->external_relocs != NULL)
11732 free (flinfo->external_relocs);
11733 if (flinfo->internal_relocs != NULL)
11734 free (flinfo->internal_relocs);
11735 if (flinfo->external_syms != NULL)
11736 free (flinfo->external_syms);
11737 if (flinfo->locsym_shndx != NULL)
11738 free (flinfo->locsym_shndx);
11739 if (flinfo->internal_syms != NULL)
11740 free (flinfo->internal_syms);
11741 if (flinfo->indices != NULL)
11742 free (flinfo->indices);
11743 if (flinfo->sections != NULL)
11744 free (flinfo->sections);
a0f6fd21
AM
11745 if (flinfo->symshndxbuf != NULL
11746 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
11747 free (flinfo->symshndxbuf);
11748 for (o = obfd->sections; o != NULL; o = o->next)
11749 {
11750 struct bfd_elf_section_data *esdo = elf_section_data (o);
11751 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11752 free (esdo->rel.hashes);
11753 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11754 free (esdo->rela.hashes);
11755 }
11756}
0b52efa6 11757
c152c796
AM
11758/* Do the final step of an ELF link. */
11759
11760bfd_boolean
11761bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11762{
11763 bfd_boolean dynamic;
11764 bfd_boolean emit_relocs;
11765 bfd *dynobj;
8b127cbc 11766 struct elf_final_link_info flinfo;
91d6fa6a
NC
11767 asection *o;
11768 struct bfd_link_order *p;
11769 bfd *sub;
c152c796
AM
11770 bfd_size_type max_contents_size;
11771 bfd_size_type max_external_reloc_size;
11772 bfd_size_type max_internal_reloc_count;
11773 bfd_size_type max_sym_count;
11774 bfd_size_type max_sym_shndx_count;
c152c796
AM
11775 Elf_Internal_Sym elfsym;
11776 unsigned int i;
11777 Elf_Internal_Shdr *symtab_hdr;
11778 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11779 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11780 struct elf_outext_info eoinfo;
11781 bfd_boolean merged;
11782 size_t relativecount = 0;
11783 asection *reldyn = 0;
11784 bfd_size_type amt;
104d59d1
JM
11785 asection *attr_section = NULL;
11786 bfd_vma attr_size = 0;
11787 const char *std_attrs_section;
64f52338 11788 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11789
64f52338 11790 if (!is_elf_hash_table (htab))
c152c796
AM
11791 return FALSE;
11792
0e1862bb 11793 if (bfd_link_pic (info))
c152c796
AM
11794 abfd->flags |= DYNAMIC;
11795
64f52338
AM
11796 dynamic = htab->dynamic_sections_created;
11797 dynobj = htab->dynobj;
c152c796 11798
0e1862bb 11799 emit_relocs = (bfd_link_relocatable (info)
a4676736 11800 || info->emitrelocations);
c152c796 11801
8b127cbc
AM
11802 flinfo.info = info;
11803 flinfo.output_bfd = abfd;
ef10c3ac 11804 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11805 if (flinfo.symstrtab == NULL)
c152c796
AM
11806 return FALSE;
11807
11808 if (! dynamic)
11809 {
8b127cbc
AM
11810 flinfo.hash_sec = NULL;
11811 flinfo.symver_sec = NULL;
c152c796
AM
11812 }
11813 else
11814 {
3d4d4302 11815 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11816 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11817 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11818 /* Note that it is OK if symver_sec is NULL. */
11819 }
11820
8b127cbc
AM
11821 flinfo.contents = NULL;
11822 flinfo.external_relocs = NULL;
11823 flinfo.internal_relocs = NULL;
11824 flinfo.external_syms = NULL;
11825 flinfo.locsym_shndx = NULL;
11826 flinfo.internal_syms = NULL;
11827 flinfo.indices = NULL;
11828 flinfo.sections = NULL;
8b127cbc 11829 flinfo.symshndxbuf = NULL;
ffbc01cc 11830 flinfo.filesym_count = 0;
c152c796 11831
104d59d1
JM
11832 /* The object attributes have been merged. Remove the input
11833 sections from the link, and set the contents of the output
1ff6de03 11834 section. */
104d59d1
JM
11835 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11836 for (o = abfd->sections; o != NULL; o = o->next)
11837 {
5270eddc 11838 bfd_boolean remove_section = FALSE;
b8a6ced7 11839
104d59d1
JM
11840 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11841 || strcmp (o->name, ".gnu.attributes") == 0)
11842 {
11843 for (p = o->map_head.link_order; p != NULL; p = p->next)
11844 {
11845 asection *input_section;
11846
11847 if (p->type != bfd_indirect_link_order)
11848 continue;
11849 input_section = p->u.indirect.section;
11850 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11851 elf_link_input_bfd ignores this section. */
11852 input_section->flags &= ~SEC_HAS_CONTENTS;
11853 }
a0c8462f 11854
104d59d1 11855 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 11856 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
11857 /* Skip this section later on. */
11858 o->map_head.link_order = NULL;
104d59d1 11859 if (attr_size)
b8a6ced7 11860 attr_section = o;
104d59d1 11861 else
5270eddc 11862 remove_section = TRUE;
104d59d1 11863 }
6e5e9d58
AM
11864 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11865 {
11866 /* Remove empty group section from linker output. */
5270eddc 11867 remove_section = TRUE;
b8a6ced7 11868 }
5270eddc 11869 if (remove_section)
b8a6ced7 11870 {
6e5e9d58
AM
11871 o->flags |= SEC_EXCLUDE;
11872 bfd_section_list_remove (abfd, o);
11873 abfd->section_count--;
11874 }
104d59d1
JM
11875 }
11876
c152c796
AM
11877 /* Count up the number of relocations we will output for each output
11878 section, so that we know the sizes of the reloc sections. We
11879 also figure out some maximum sizes. */
11880 max_contents_size = 0;
11881 max_external_reloc_size = 0;
11882 max_internal_reloc_count = 0;
11883 max_sym_count = 0;
11884 max_sym_shndx_count = 0;
11885 merged = FALSE;
11886 for (o = abfd->sections; o != NULL; o = o->next)
11887 {
11888 struct bfd_elf_section_data *esdo = elf_section_data (o);
11889 o->reloc_count = 0;
11890
8423293d 11891 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11892 {
11893 unsigned int reloc_count = 0;
9eaff861 11894 unsigned int additional_reloc_count = 0;
c152c796 11895 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11896
11897 if (p->type == bfd_section_reloc_link_order
11898 || p->type == bfd_symbol_reloc_link_order)
11899 reloc_count = 1;
11900 else if (p->type == bfd_indirect_link_order)
11901 {
11902 asection *sec;
11903
11904 sec = p->u.indirect.section;
c152c796
AM
11905
11906 /* Mark all sections which are to be included in the
11907 link. This will normally be every section. We need
11908 to do this so that we can identify any sections which
11909 the linker has decided to not include. */
11910 sec->linker_mark = TRUE;
11911
11912 if (sec->flags & SEC_MERGE)
11913 merged = TRUE;
11914
eea6121a
AM
11915 if (sec->rawsize > max_contents_size)
11916 max_contents_size = sec->rawsize;
11917 if (sec->size > max_contents_size)
11918 max_contents_size = sec->size;
c152c796 11919
c152c796
AM
11920 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11921 && (sec->owner->flags & DYNAMIC) == 0)
11922 {
11923 size_t sym_count;
11924
a961cdd5
AM
11925 /* We are interested in just local symbols, not all
11926 symbols. */
c152c796
AM
11927 if (elf_bad_symtab (sec->owner))
11928 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11929 / bed->s->sizeof_sym);
11930 else
11931 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11932
11933 if (sym_count > max_sym_count)
11934 max_sym_count = sym_count;
11935
11936 if (sym_count > max_sym_shndx_count
6a40cf0c 11937 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11938 max_sym_shndx_count = sym_count;
11939
a961cdd5
AM
11940 if (esdo->this_hdr.sh_type == SHT_REL
11941 || esdo->this_hdr.sh_type == SHT_RELA)
11942 /* Some backends use reloc_count in relocation sections
11943 to count particular types of relocs. Of course,
11944 reloc sections themselves can't have relocations. */
11945 ;
11946 else if (emit_relocs)
11947 {
11948 reloc_count = sec->reloc_count;
11949 if (bed->elf_backend_count_additional_relocs)
11950 {
11951 int c;
11952 c = (*bed->elf_backend_count_additional_relocs) (sec);
11953 additional_reloc_count += c;
11954 }
11955 }
11956 else if (bed->elf_backend_count_relocs)
11957 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11958
11959 esdi = elf_section_data (sec);
11960
c152c796
AM
11961 if ((sec->flags & SEC_RELOC) != 0)
11962 {
d4730f92 11963 size_t ext_size = 0;
c152c796 11964
d4730f92
BS
11965 if (esdi->rel.hdr != NULL)
11966 ext_size = esdi->rel.hdr->sh_size;
11967 if (esdi->rela.hdr != NULL)
11968 ext_size += esdi->rela.hdr->sh_size;
7326c758 11969
c152c796
AM
11970 if (ext_size > max_external_reloc_size)
11971 max_external_reloc_size = ext_size;
11972 if (sec->reloc_count > max_internal_reloc_count)
11973 max_internal_reloc_count = sec->reloc_count;
11974 }
11975 }
11976 }
11977
11978 if (reloc_count == 0)
11979 continue;
11980
9eaff861 11981 reloc_count += additional_reloc_count;
c152c796
AM
11982 o->reloc_count += reloc_count;
11983
0e1862bb 11984 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11985 {
d4730f92 11986 if (esdi->rel.hdr)
9eaff861 11987 {
491d01d3 11988 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11989 esdo->rel.count += additional_reloc_count;
11990 }
d4730f92 11991 if (esdi->rela.hdr)
9eaff861 11992 {
491d01d3 11993 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11994 esdo->rela.count += additional_reloc_count;
11995 }
d4730f92
BS
11996 }
11997 else
11998 {
11999 if (o->use_rela_p)
12000 esdo->rela.count += reloc_count;
2c2b4ed4 12001 else
d4730f92 12002 esdo->rel.count += reloc_count;
c152c796 12003 }
c152c796
AM
12004 }
12005
9eaff861 12006 if (o->reloc_count > 0)
c152c796
AM
12007 o->flags |= SEC_RELOC;
12008 else
12009 {
12010 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12011 set it (this is probably a bug) and if it is set
12012 assign_section_numbers will create a reloc section. */
12013 o->flags &=~ SEC_RELOC;
12014 }
12015
12016 /* If the SEC_ALLOC flag is not set, force the section VMA to
12017 zero. This is done in elf_fake_sections as well, but forcing
12018 the VMA to 0 here will ensure that relocs against these
12019 sections are handled correctly. */
12020 if ((o->flags & SEC_ALLOC) == 0
12021 && ! o->user_set_vma)
12022 o->vma = 0;
12023 }
12024
0e1862bb 12025 if (! bfd_link_relocatable (info) && merged)
64f52338 12026 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12027
12028 /* Figure out the file positions for everything but the symbol table
12029 and the relocs. We set symcount to force assign_section_numbers
12030 to create a symbol table. */
ed48ec2e 12031 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12032 BFD_ASSERT (! abfd->output_has_begun);
12033 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12034 goto error_return;
12035
ee75fd95 12036 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12037 for (o = abfd->sections; o != NULL; o = o->next)
12038 {
d4730f92 12039 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12040 if ((o->flags & SEC_RELOC) != 0)
12041 {
d4730f92 12042 if (esdo->rel.hdr
9eaff861 12043 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12044 goto error_return;
12045
d4730f92 12046 if (esdo->rela.hdr
9eaff861 12047 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12048 goto error_return;
12049 }
12050
48db3297
AM
12051 /* _bfd_elf_compute_section_file_positions makes temporary use
12052 of target_index. Reset it. */
12053 o->target_index = 0;
12054
c152c796
AM
12055 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12056 to count upwards while actually outputting the relocations. */
d4730f92
BS
12057 esdo->rel.count = 0;
12058 esdo->rela.count = 0;
0ce398f1 12059
1ff6de03
NA
12060 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12061 && !bfd_section_is_ctf (o))
0ce398f1
L
12062 {
12063 /* Cache the section contents so that they can be compressed
12064 later. Use bfd_malloc since it will be freed by
12065 bfd_compress_section_contents. */
12066 unsigned char *contents = esdo->this_hdr.contents;
12067 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12068 abort ();
12069 contents
12070 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12071 if (contents == NULL)
12072 goto error_return;
12073 esdo->this_hdr.contents = contents;
12074 }
c152c796
AM
12075 }
12076
1ff6de03
NA
12077 /* We have now assigned file positions for all the sections except .symtab,
12078 .strtab, and non-loaded reloc and compressed debugging sections. We start
12079 the .symtab section at the current file position, and write directly to it.
12080 We build the .strtab section in memory. */
ed48ec2e 12081 abfd->symcount = 0;
c152c796
AM
12082 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12083 /* sh_name is set in prep_headers. */
12084 symtab_hdr->sh_type = SHT_SYMTAB;
12085 /* sh_flags, sh_addr and sh_size all start off zero. */
12086 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12087 /* sh_link is set in assign_section_numbers. */
12088 /* sh_info is set below. */
12089 /* sh_offset is set just below. */
72de5009 12090 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12091
ef10c3ac
L
12092 if (max_sym_count < 20)
12093 max_sym_count = 20;
64f52338 12094 htab->strtabsize = max_sym_count;
ef10c3ac 12095 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12096 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12097 if (htab->strtab == NULL)
c152c796 12098 goto error_return;
ef10c3ac
L
12099 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12100 flinfo.symshndxbuf
12101 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12102 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12103
8539e4e8 12104 if (info->strip != strip_all || emit_relocs)
c152c796 12105 {
8539e4e8
AM
12106 file_ptr off = elf_next_file_pos (abfd);
12107
12108 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12109
12110 /* Note that at this point elf_next_file_pos (abfd) is
12111 incorrect. We do not yet know the size of the .symtab section.
12112 We correct next_file_pos below, after we do know the size. */
12113
12114 /* Start writing out the symbol table. The first symbol is always a
12115 dummy symbol. */
c152c796
AM
12116 elfsym.st_value = 0;
12117 elfsym.st_size = 0;
12118 elfsym.st_info = 0;
12119 elfsym.st_other = 0;
12120 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12121 elfsym.st_target_internal = 0;
ef10c3ac
L
12122 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12123 bfd_und_section_ptr, NULL) != 1)
c152c796 12124 goto error_return;
c152c796 12125
8539e4e8
AM
12126 /* Output a symbol for each section. We output these even if we are
12127 discarding local symbols, since they are used for relocs. These
12128 symbols have no names. We store the index of each one in the
12129 index field of the section, so that we can find it again when
12130 outputting relocs. */
12131
c152c796
AM
12132 elfsym.st_size = 0;
12133 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12134 elfsym.st_other = 0;
f0b5bb34 12135 elfsym.st_value = 0;
35fc36a8 12136 elfsym.st_target_internal = 0;
c152c796
AM
12137 for (i = 1; i < elf_numsections (abfd); i++)
12138 {
12139 o = bfd_section_from_elf_index (abfd, i);
12140 if (o != NULL)
f0b5bb34
AM
12141 {
12142 o->target_index = bfd_get_symcount (abfd);
12143 elfsym.st_shndx = i;
0e1862bb 12144 if (!bfd_link_relocatable (info))
f0b5bb34 12145 elfsym.st_value = o->vma;
ef10c3ac
L
12146 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12147 NULL) != 1)
f0b5bb34
AM
12148 goto error_return;
12149 }
c152c796
AM
12150 }
12151 }
12152
12153 /* Allocate some memory to hold information read in from the input
12154 files. */
12155 if (max_contents_size != 0)
12156 {
8b127cbc
AM
12157 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12158 if (flinfo.contents == NULL)
c152c796
AM
12159 goto error_return;
12160 }
12161
12162 if (max_external_reloc_size != 0)
12163 {
8b127cbc
AM
12164 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12165 if (flinfo.external_relocs == NULL)
c152c796
AM
12166 goto error_return;
12167 }
12168
12169 if (max_internal_reloc_count != 0)
12170 {
056bafd4 12171 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12172 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12173 if (flinfo.internal_relocs == NULL)
c152c796
AM
12174 goto error_return;
12175 }
12176
12177 if (max_sym_count != 0)
12178 {
12179 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12180 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12181 if (flinfo.external_syms == NULL)
c152c796
AM
12182 goto error_return;
12183
12184 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12185 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12186 if (flinfo.internal_syms == NULL)
c152c796
AM
12187 goto error_return;
12188
12189 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12190 flinfo.indices = (long int *) bfd_malloc (amt);
12191 if (flinfo.indices == NULL)
c152c796
AM
12192 goto error_return;
12193
12194 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12195 flinfo.sections = (asection **) bfd_malloc (amt);
12196 if (flinfo.sections == NULL)
c152c796
AM
12197 goto error_return;
12198 }
12199
12200 if (max_sym_shndx_count != 0)
12201 {
12202 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12203 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12204 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12205 goto error_return;
12206 }
12207
64f52338 12208 if (htab->tls_sec)
c152c796
AM
12209 {
12210 bfd_vma base, end = 0;
12211 asection *sec;
12212
64f52338 12213 for (sec = htab->tls_sec;
c152c796
AM
12214 sec && (sec->flags & SEC_THREAD_LOCAL);
12215 sec = sec->next)
12216 {
3a800eb9 12217 bfd_size_type size = sec->size;
c152c796 12218
3a800eb9
AM
12219 if (size == 0
12220 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12221 {
91d6fa6a
NC
12222 struct bfd_link_order *ord = sec->map_tail.link_order;
12223
12224 if (ord != NULL)
12225 size = ord->offset + ord->size;
c152c796
AM
12226 }
12227 end = sec->vma + size;
12228 }
64f52338 12229 base = htab->tls_sec->vma;
7dc98aea
RO
12230 /* Only align end of TLS section if static TLS doesn't have special
12231 alignment requirements. */
12232 if (bed->static_tls_alignment == 1)
64f52338
AM
12233 end = align_power (end, htab->tls_sec->alignment_power);
12234 htab->tls_size = end - base;
c152c796
AM
12235 }
12236
0b52efa6
PB
12237 /* Reorder SHF_LINK_ORDER sections. */
12238 for (o = abfd->sections; o != NULL; o = o->next)
12239 {
12240 if (!elf_fixup_link_order (abfd, o))
12241 return FALSE;
12242 }
12243
2f0c68f2
CM
12244 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12245 return FALSE;
12246
c152c796
AM
12247 /* Since ELF permits relocations to be against local symbols, we
12248 must have the local symbols available when we do the relocations.
12249 Since we would rather only read the local symbols once, and we
12250 would rather not keep them in memory, we handle all the
12251 relocations for a single input file at the same time.
12252
12253 Unfortunately, there is no way to know the total number of local
12254 symbols until we have seen all of them, and the local symbol
12255 indices precede the global symbol indices. This means that when
12256 we are generating relocatable output, and we see a reloc against
12257 a global symbol, we can not know the symbol index until we have
12258 finished examining all the local symbols to see which ones we are
12259 going to output. To deal with this, we keep the relocations in
12260 memory, and don't output them until the end of the link. This is
12261 an unfortunate waste of memory, but I don't see a good way around
12262 it. Fortunately, it only happens when performing a relocatable
12263 link, which is not the common case. FIXME: If keep_memory is set
12264 we could write the relocs out and then read them again; I don't
12265 know how bad the memory loss will be. */
12266
c72f2fb2 12267 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12268 sub->output_has_begun = FALSE;
12269 for (o = abfd->sections; o != NULL; o = o->next)
12270 {
8423293d 12271 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12272 {
12273 if (p->type == bfd_indirect_link_order
12274 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12275 == bfd_target_elf_flavour)
12276 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12277 {
12278 if (! sub->output_has_begun)
12279 {
8b127cbc 12280 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12281 goto error_return;
12282 sub->output_has_begun = TRUE;
12283 }
12284 }
12285 else if (p->type == bfd_section_reloc_link_order
12286 || p->type == bfd_symbol_reloc_link_order)
12287 {
12288 if (! elf_reloc_link_order (abfd, info, o, p))
12289 goto error_return;
12290 }
12291 else
12292 {
12293 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12294 {
12295 if (p->type == bfd_indirect_link_order
12296 && (bfd_get_flavour (sub)
12297 == bfd_target_elf_flavour)
12298 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12299 != bed->s->elfclass))
12300 {
12301 const char *iclass, *oclass;
12302
aebf9be7 12303 switch (bed->s->elfclass)
351f65ca 12304 {
aebf9be7
NC
12305 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12306 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12307 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12308 default: abort ();
351f65ca 12309 }
aebf9be7
NC
12310
12311 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12312 {
aebf9be7
NC
12313 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12314 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12315 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12316 default: abort ();
351f65ca
L
12317 }
12318
12319 bfd_set_error (bfd_error_wrong_format);
4eca0228 12320 _bfd_error_handler
695344c0 12321 /* xgettext:c-format */
871b3ab2 12322 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12323 sub, iclass, oclass);
12324 }
12325
12326 goto error_return;
12327 }
c152c796
AM
12328 }
12329 }
12330 }
12331
c0f00686
L
12332 /* Free symbol buffer if needed. */
12333 if (!info->reduce_memory_overheads)
12334 {
c72f2fb2 12335 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12336 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12337 && elf_tdata (sub)->symbuf)
c0f00686
L
12338 {
12339 free (elf_tdata (sub)->symbuf);
12340 elf_tdata (sub)->symbuf = NULL;
12341 }
12342 }
12343
c152c796
AM
12344 /* Output any global symbols that got converted to local in a
12345 version script or due to symbol visibility. We do this in a
12346 separate step since ELF requires all local symbols to appear
12347 prior to any global symbols. FIXME: We should only do this if
12348 some global symbols were, in fact, converted to become local.
12349 FIXME: Will this work correctly with the Irix 5 linker? */
12350 eoinfo.failed = FALSE;
8b127cbc 12351 eoinfo.flinfo = &flinfo;
c152c796 12352 eoinfo.localsyms = TRUE;
34a79995 12353 eoinfo.file_sym_done = FALSE;
7686d77d 12354 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12355 if (eoinfo.failed)
12356 return FALSE;
12357
4e617b1e
PB
12358 /* If backend needs to output some local symbols not present in the hash
12359 table, do it now. */
8539e4e8
AM
12360 if (bed->elf_backend_output_arch_local_syms
12361 && (info->strip != strip_all || emit_relocs))
4e617b1e 12362 {
6e0b88f1 12363 typedef int (*out_sym_func)
4e617b1e
PB
12364 (void *, const char *, Elf_Internal_Sym *, asection *,
12365 struct elf_link_hash_entry *);
12366
12367 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12368 (abfd, info, &flinfo,
12369 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12370 return FALSE;
12371 }
12372
c152c796
AM
12373 /* That wrote out all the local symbols. Finish up the symbol table
12374 with the global symbols. Even if we want to strip everything we
12375 can, we still need to deal with those global symbols that got
12376 converted to local in a version script. */
12377
12378 /* The sh_info field records the index of the first non local symbol. */
12379 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12380
12381 if (dynamic
64f52338
AM
12382 && htab->dynsym != NULL
12383 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12384 {
12385 Elf_Internal_Sym sym;
64f52338 12386 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12387
64f52338
AM
12388 o = htab->dynsym->output_section;
12389 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12390
12391 /* Write out the section symbols for the output sections. */
0e1862bb 12392 if (bfd_link_pic (info)
64f52338 12393 || htab->is_relocatable_executable)
c152c796
AM
12394 {
12395 asection *s;
12396
12397 sym.st_size = 0;
12398 sym.st_name = 0;
12399 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12400 sym.st_other = 0;
35fc36a8 12401 sym.st_target_internal = 0;
c152c796
AM
12402
12403 for (s = abfd->sections; s != NULL; s = s->next)
12404 {
12405 int indx;
12406 bfd_byte *dest;
12407 long dynindx;
12408
c152c796 12409 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12410 if (dynindx <= 0)
12411 continue;
12412 indx = elf_section_data (s)->this_idx;
c152c796
AM
12413 BFD_ASSERT (indx > 0);
12414 sym.st_shndx = indx;
c0d5a53d
L
12415 if (! check_dynsym (abfd, &sym))
12416 return FALSE;
c152c796
AM
12417 sym.st_value = s->vma;
12418 dest = dynsym + dynindx * bed->s->sizeof_sym;
12419 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12420 }
c152c796
AM
12421 }
12422
12423 /* Write out the local dynsyms. */
64f52338 12424 if (htab->dynlocal)
c152c796
AM
12425 {
12426 struct elf_link_local_dynamic_entry *e;
64f52338 12427 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12428 {
12429 asection *s;
12430 bfd_byte *dest;
12431
935bd1e0 12432 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12433 Note that we saved a word of storage and overwrote
12434 the original st_name with the dynstr_index. */
12435 sym = e->isym;
935bd1e0 12436 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12437
cb33740c
AM
12438 s = bfd_section_from_elf_index (e->input_bfd,
12439 e->isym.st_shndx);
12440 if (s != NULL)
c152c796 12441 {
c152c796
AM
12442 sym.st_shndx =
12443 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12444 if (! check_dynsym (abfd, &sym))
12445 return FALSE;
c152c796
AM
12446 sym.st_value = (s->output_section->vma
12447 + s->output_offset
12448 + e->isym.st_value);
12449 }
12450
c152c796
AM
12451 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12452 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12453 }
12454 }
c152c796
AM
12455 }
12456
12457 /* We get the global symbols from the hash table. */
12458 eoinfo.failed = FALSE;
12459 eoinfo.localsyms = FALSE;
8b127cbc 12460 eoinfo.flinfo = &flinfo;
7686d77d 12461 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12462 if (eoinfo.failed)
12463 return FALSE;
12464
12465 /* If backend needs to output some symbols not present in the hash
12466 table, do it now. */
8539e4e8
AM
12467 if (bed->elf_backend_output_arch_syms
12468 && (info->strip != strip_all || emit_relocs))
c152c796 12469 {
6e0b88f1 12470 typedef int (*out_sym_func)
c152c796
AM
12471 (void *, const char *, Elf_Internal_Sym *, asection *,
12472 struct elf_link_hash_entry *);
12473
12474 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12475 (abfd, info, &flinfo,
12476 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12477 return FALSE;
12478 }
12479
ef10c3ac
L
12480 /* Finalize the .strtab section. */
12481 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12482
12483 /* Swap out the .strtab section. */
12484 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12485 return FALSE;
12486
12487 /* Now we know the size of the symtab section. */
c152c796
AM
12488 if (bfd_get_symcount (abfd) > 0)
12489 {
ee3b52e9
L
12490 /* Finish up and write out the symbol string table (.strtab)
12491 section. */
ad32986f 12492 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12493 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12494
ad32986f 12495 if (elf_symtab_shndx_list (abfd))
8539e4e8 12496 {
ad32986f 12497 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12498
ad32986f
NC
12499 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12500 {
12501 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12502 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12503 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12504 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12505 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12506
ad32986f
NC
12507 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12508 off, TRUE);
12509
12510 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12511 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12512 return FALSE;
12513 }
8539e4e8 12514 }
ee3b52e9
L
12515
12516 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12517 /* sh_name was set in prep_headers. */
12518 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12519 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12520 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12521 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12522 symstrtab_hdr->sh_entsize = 0;
12523 symstrtab_hdr->sh_link = 0;
12524 symstrtab_hdr->sh_info = 0;
12525 /* sh_offset is set just below. */
12526 symstrtab_hdr->sh_addralign = 1;
12527
12528 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12529 off, TRUE);
12530 elf_next_file_pos (abfd) = off;
12531
c152c796 12532 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12533 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12534 return FALSE;
12535 }
12536
76359541
TP
12537 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12538 {
871b3ab2 12539 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12540 info->out_implib_bfd);
76359541
TP
12541 return FALSE;
12542 }
12543
c152c796
AM
12544 /* Adjust the relocs to have the correct symbol indices. */
12545 for (o = abfd->sections; o != NULL; o = o->next)
12546 {
d4730f92 12547 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12548 bfd_boolean sort;
10bbbc1d 12549
c152c796
AM
12550 if ((o->flags & SEC_RELOC) == 0)
12551 continue;
12552
28dbcedc 12553 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12554 if (esdo->rel.hdr != NULL
10bbbc1d 12555 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12556 return FALSE;
12557 if (esdo->rela.hdr != NULL
10bbbc1d 12558 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12559 return FALSE;
c152c796
AM
12560
12561 /* Set the reloc_count field to 0 to prevent write_relocs from
12562 trying to swap the relocs out itself. */
12563 o->reloc_count = 0;
12564 }
12565
12566 if (dynamic && info->combreloc && dynobj != NULL)
12567 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12568
12569 /* If we are linking against a dynamic object, or generating a
12570 shared library, finish up the dynamic linking information. */
12571 if (dynamic)
12572 {
12573 bfd_byte *dyncon, *dynconend;
12574
12575 /* Fix up .dynamic entries. */
3d4d4302 12576 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12577 BFD_ASSERT (o != NULL);
12578
12579 dyncon = o->contents;
eea6121a 12580 dynconend = o->contents + o->size;
c152c796
AM
12581 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12582 {
12583 Elf_Internal_Dyn dyn;
12584 const char *name;
12585 unsigned int type;
64487780
AM
12586 bfd_size_type sh_size;
12587 bfd_vma sh_addr;
c152c796
AM
12588
12589 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12590
12591 switch (dyn.d_tag)
12592 {
12593 default:
12594 continue;
12595 case DT_NULL:
12596 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12597 {
12598 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12599 {
12600 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12601 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12602 default: continue;
12603 }
12604 dyn.d_un.d_val = relativecount;
12605 relativecount = 0;
12606 break;
12607 }
12608 continue;
12609
12610 case DT_INIT:
12611 name = info->init_function;
12612 goto get_sym;
12613 case DT_FINI:
12614 name = info->fini_function;
12615 get_sym:
12616 {
12617 struct elf_link_hash_entry *h;
12618
64f52338 12619 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12620 if (h != NULL
12621 && (h->root.type == bfd_link_hash_defined
12622 || h->root.type == bfd_link_hash_defweak))
12623 {
bef26483 12624 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12625 o = h->root.u.def.section;
12626 if (o->output_section != NULL)
bef26483 12627 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12628 + o->output_offset);
12629 else
12630 {
12631 /* The symbol is imported from another shared
12632 library and does not apply to this one. */
bef26483 12633 dyn.d_un.d_ptr = 0;
c152c796
AM
12634 }
12635 break;
12636 }
12637 }
12638 continue;
12639
12640 case DT_PREINIT_ARRAYSZ:
12641 name = ".preinit_array";
4ade44b7 12642 goto get_out_size;
c152c796
AM
12643 case DT_INIT_ARRAYSZ:
12644 name = ".init_array";
4ade44b7 12645 goto get_out_size;
c152c796
AM
12646 case DT_FINI_ARRAYSZ:
12647 name = ".fini_array";
4ade44b7 12648 get_out_size:
c152c796
AM
12649 o = bfd_get_section_by_name (abfd, name);
12650 if (o == NULL)
12651 {
4eca0228 12652 _bfd_error_handler
4ade44b7 12653 (_("could not find section %s"), name);
c152c796
AM
12654 goto error_return;
12655 }
eea6121a 12656 if (o->size == 0)
4eca0228 12657 _bfd_error_handler
c152c796 12658 (_("warning: %s section has zero size"), name);
eea6121a 12659 dyn.d_un.d_val = o->size;
c152c796
AM
12660 break;
12661
12662 case DT_PREINIT_ARRAY:
12663 name = ".preinit_array";
4ade44b7 12664 goto get_out_vma;
c152c796
AM
12665 case DT_INIT_ARRAY:
12666 name = ".init_array";
4ade44b7 12667 goto get_out_vma;
c152c796
AM
12668 case DT_FINI_ARRAY:
12669 name = ".fini_array";
4ade44b7
AM
12670 get_out_vma:
12671 o = bfd_get_section_by_name (abfd, name);
12672 goto do_vma;
c152c796
AM
12673
12674 case DT_HASH:
12675 name = ".hash";
12676 goto get_vma;
fdc90cb4
JJ
12677 case DT_GNU_HASH:
12678 name = ".gnu.hash";
12679 goto get_vma;
c152c796
AM
12680 case DT_STRTAB:
12681 name = ".dynstr";
12682 goto get_vma;
12683 case DT_SYMTAB:
12684 name = ".dynsym";
12685 goto get_vma;
12686 case DT_VERDEF:
12687 name = ".gnu.version_d";
12688 goto get_vma;
12689 case DT_VERNEED:
12690 name = ".gnu.version_r";
12691 goto get_vma;
12692 case DT_VERSYM:
12693 name = ".gnu.version";
12694 get_vma:
4ade44b7
AM
12695 o = bfd_get_linker_section (dynobj, name);
12696 do_vma:
b3293efa 12697 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12698 {
4eca0228 12699 _bfd_error_handler
4ade44b7 12700 (_("could not find section %s"), name);
c152c796
AM
12701 goto error_return;
12702 }
894891db
NC
12703 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12704 {
4eca0228 12705 _bfd_error_handler
894891db
NC
12706 (_("warning: section '%s' is being made into a note"), name);
12707 bfd_set_error (bfd_error_nonrepresentable_section);
12708 goto error_return;
12709 }
4ade44b7 12710 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12711 break;
12712
12713 case DT_REL:
12714 case DT_RELA:
12715 case DT_RELSZ:
12716 case DT_RELASZ:
12717 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12718 type = SHT_REL;
12719 else
12720 type = SHT_RELA;
64487780
AM
12721 sh_size = 0;
12722 sh_addr = 0;
c152c796
AM
12723 for (i = 1; i < elf_numsections (abfd); i++)
12724 {
12725 Elf_Internal_Shdr *hdr;
12726
12727 hdr = elf_elfsections (abfd)[i];
12728 if (hdr->sh_type == type
12729 && (hdr->sh_flags & SHF_ALLOC) != 0)
12730 {
64487780
AM
12731 sh_size += hdr->sh_size;
12732 if (sh_addr == 0
12733 || sh_addr > hdr->sh_addr)
12734 sh_addr = hdr->sh_addr;
c152c796
AM
12735 }
12736 }
64487780 12737
64f52338
AM
12738 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12739 {
12740 /* Don't count procedure linkage table relocs in the
12741 overall reloc count. */
64487780
AM
12742 sh_size -= htab->srelplt->size;
12743 if (sh_size == 0)
12744 /* If the size is zero, make the address zero too.
12745 This is to avoid a glibc bug. If the backend
12746 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12747 zero, then we'll put DT_RELA at the end of
12748 DT_JMPREL. glibc will interpret the end of
12749 DT_RELA matching the end of DT_JMPREL as the
12750 case where DT_RELA includes DT_JMPREL, and for
12751 LD_BIND_NOW will decide that processing DT_RELA
12752 will process the PLT relocs too. Net result:
12753 No PLT relocs applied. */
12754 sh_addr = 0;
12755
64f52338
AM
12756 /* If .rela.plt is the first .rela section, exclude
12757 it from DT_RELA. */
64487780
AM
12758 else if (sh_addr == (htab->srelplt->output_section->vma
12759 + htab->srelplt->output_offset))
12760 sh_addr += htab->srelplt->size;
64f52338 12761 }
64487780
AM
12762
12763 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12764 dyn.d_un.d_val = sh_size;
12765 else
12766 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12767 break;
12768 }
12769 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12770 }
12771 }
12772
12773 /* If we have created any dynamic sections, then output them. */
12774 if (dynobj != NULL)
12775 {
12776 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12777 goto error_return;
12778
943284cc 12779 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12780 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12781 || info->error_textrel)
3d4d4302 12782 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12783 {
12784 bfd_byte *dyncon, *dynconend;
12785
943284cc
DJ
12786 dyncon = o->contents;
12787 dynconend = o->contents + o->size;
12788 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12789 {
12790 Elf_Internal_Dyn dyn;
12791
12792 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12793
12794 if (dyn.d_tag == DT_TEXTREL)
12795 {
c192a133
AM
12796 if (info->error_textrel)
12797 info->callbacks->einfo
9793eb77 12798 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12799 else
12800 info->callbacks->einfo
9793eb77 12801 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12802 break;
12803 }
12804 }
12805 }
12806
c152c796
AM
12807 for (o = dynobj->sections; o != NULL; o = o->next)
12808 {
12809 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12810 || o->size == 0
c152c796
AM
12811 || o->output_section == bfd_abs_section_ptr)
12812 continue;
12813 if ((o->flags & SEC_LINKER_CREATED) == 0)
12814 {
12815 /* At this point, we are only interested in sections
12816 created by _bfd_elf_link_create_dynamic_sections. */
12817 continue;
12818 }
64f52338 12819 if (htab->stab_info.stabstr == o)
3722b82f 12820 continue;
64f52338 12821 if (htab->eh_info.hdr_sec == o)
eea6121a 12822 continue;
3d4d4302 12823 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12824 {
12825 if (! bfd_set_section_contents (abfd, o->output_section,
12826 o->contents,
37b01f6a
DG
12827 (file_ptr) o->output_offset
12828 * bfd_octets_per_byte (abfd),
eea6121a 12829 o->size))
c152c796
AM
12830 goto error_return;
12831 }
12832 else
12833 {
12834 /* The contents of the .dynstr section are actually in a
12835 stringtab. */
8539e4e8
AM
12836 file_ptr off;
12837
c152c796
AM
12838 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12839 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12840 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12841 goto error_return;
12842 }
12843 }
12844 }
12845
7bdf4127 12846 if (!info->resolve_section_groups)
c152c796
AM
12847 {
12848 bfd_boolean failed = FALSE;
12849
7bdf4127 12850 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12851 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12852 if (failed)
12853 goto error_return;
12854 }
12855
12856 /* If we have optimized stabs strings, output them. */
64f52338 12857 if (htab->stab_info.stabstr != NULL)
c152c796 12858 {
64f52338 12859 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12860 goto error_return;
12861 }
12862
9f7c3e5e
AM
12863 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12864 goto error_return;
c152c796 12865
1ff6de03
NA
12866 if (info->callbacks->emit_ctf)
12867 info->callbacks->emit_ctf ();
12868
9f7c3e5e 12869 elf_final_link_free (abfd, &flinfo);
c152c796 12870
104d59d1
JM
12871 if (attr_section)
12872 {
a50b1753 12873 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12874 if (contents == NULL)
d0f16d5e 12875 return FALSE; /* Bail out and fail. */
104d59d1
JM
12876 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12877 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12878 free (contents);
12879 }
12880
c152c796
AM
12881 return TRUE;
12882
12883 error_return:
9f7c3e5e 12884 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12885 return FALSE;
12886}
12887\f
5241d853
RS
12888/* Initialize COOKIE for input bfd ABFD. */
12889
12890static bfd_boolean
12891init_reloc_cookie (struct elf_reloc_cookie *cookie,
12892 struct bfd_link_info *info, bfd *abfd)
12893{
12894 Elf_Internal_Shdr *symtab_hdr;
12895 const struct elf_backend_data *bed;
12896
12897 bed = get_elf_backend_data (abfd);
12898 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12899
12900 cookie->abfd = abfd;
12901 cookie->sym_hashes = elf_sym_hashes (abfd);
12902 cookie->bad_symtab = elf_bad_symtab (abfd);
12903 if (cookie->bad_symtab)
12904 {
12905 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12906 cookie->extsymoff = 0;
12907 }
12908 else
12909 {
12910 cookie->locsymcount = symtab_hdr->sh_info;
12911 cookie->extsymoff = symtab_hdr->sh_info;
12912 }
12913
12914 if (bed->s->arch_size == 32)
12915 cookie->r_sym_shift = 8;
12916 else
12917 cookie->r_sym_shift = 32;
12918
12919 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12920 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12921 {
12922 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12923 cookie->locsymcount, 0,
12924 NULL, NULL, NULL);
12925 if (cookie->locsyms == NULL)
12926 {
12927 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12928 return FALSE;
12929 }
12930 if (info->keep_memory)
12931 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12932 }
12933 return TRUE;
12934}
12935
12936/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12937
12938static void
12939fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12940{
12941 Elf_Internal_Shdr *symtab_hdr;
12942
12943 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12944 if (cookie->locsyms != NULL
12945 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12946 free (cookie->locsyms);
12947}
12948
12949/* Initialize the relocation information in COOKIE for input section SEC
12950 of input bfd ABFD. */
12951
12952static bfd_boolean
12953init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12954 struct bfd_link_info *info, bfd *abfd,
12955 asection *sec)
12956{
5241d853
RS
12957 if (sec->reloc_count == 0)
12958 {
12959 cookie->rels = NULL;
12960 cookie->relend = NULL;
12961 }
12962 else
12963 {
5241d853
RS
12964 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12965 info->keep_memory);
12966 if (cookie->rels == NULL)
12967 return FALSE;
12968 cookie->rel = cookie->rels;
056bafd4 12969 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12970 }
12971 cookie->rel = cookie->rels;
12972 return TRUE;
12973}
12974
12975/* Free the memory allocated by init_reloc_cookie_rels,
12976 if appropriate. */
12977
12978static void
12979fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12980 asection *sec)
12981{
12982 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12983 free (cookie->rels);
12984}
12985
12986/* Initialize the whole of COOKIE for input section SEC. */
12987
12988static bfd_boolean
12989init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12990 struct bfd_link_info *info,
12991 asection *sec)
12992{
12993 if (!init_reloc_cookie (cookie, info, sec->owner))
12994 goto error1;
12995 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12996 goto error2;
12997 return TRUE;
12998
12999 error2:
13000 fini_reloc_cookie (cookie, sec->owner);
13001 error1:
13002 return FALSE;
13003}
13004
13005/* Free the memory allocated by init_reloc_cookie_for_section,
13006 if appropriate. */
13007
13008static void
13009fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13010 asection *sec)
13011{
13012 fini_reloc_cookie_rels (cookie, sec);
13013 fini_reloc_cookie (cookie, sec->owner);
13014}
13015\f
c152c796
AM
13016/* Garbage collect unused sections. */
13017
07adf181
AM
13018/* Default gc_mark_hook. */
13019
13020asection *
13021_bfd_elf_gc_mark_hook (asection *sec,
13022 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13023 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13024 struct elf_link_hash_entry *h,
13025 Elf_Internal_Sym *sym)
13026{
13027 if (h != NULL)
13028 {
13029 switch (h->root.type)
13030 {
13031 case bfd_link_hash_defined:
13032 case bfd_link_hash_defweak:
13033 return h->root.u.def.section;
13034
13035 case bfd_link_hash_common:
13036 return h->root.u.c.p->section;
13037
13038 default:
13039 break;
13040 }
13041 }
13042 else
13043 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13044
13045 return NULL;
13046}
13047
9e223787 13048/* Return the debug definition section. */
b7c871ed
L
13049
13050static asection *
13051elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13052 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13053 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13054 struct elf_link_hash_entry *h,
9e223787 13055 Elf_Internal_Sym *sym)
b7c871ed 13056{
9e223787
L
13057 if (h != NULL)
13058 {
13059 /* Return the global debug definition section. */
13060 if ((h->root.type == bfd_link_hash_defined
13061 || h->root.type == bfd_link_hash_defweak)
13062 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13063 return h->root.u.def.section;
13064 }
13065 else
13066 {
13067 /* Return the local debug definition section. */
13068 asection *isec = bfd_section_from_elf_index (sec->owner,
13069 sym->st_shndx);
13070 if ((isec->flags & SEC_DEBUGGING) != 0)
13071 return isec;
13072 }
b7c871ed
L
13073
13074 return NULL;
13075}
13076
5241d853
RS
13077/* COOKIE->rel describes a relocation against section SEC, which is
13078 a section we've decided to keep. Return the section that contains
13079 the relocation symbol, or NULL if no section contains it. */
13080
13081asection *
13082_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13083 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13084 struct elf_reloc_cookie *cookie,
13085 bfd_boolean *start_stop)
5241d853
RS
13086{
13087 unsigned long r_symndx;
13088 struct elf_link_hash_entry *h;
13089
13090 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13091 if (r_symndx == STN_UNDEF)
5241d853
RS
13092 return NULL;
13093
13094 if (r_symndx >= cookie->locsymcount
13095 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13096 {
13097 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13098 if (h == NULL)
13099 {
871b3ab2 13100 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13101 sec->owner);
13102 return NULL;
13103 }
5241d853
RS
13104 while (h->root.type == bfd_link_hash_indirect
13105 || h->root.type == bfd_link_hash_warning)
13106 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13107 h->mark = 1;
4e6b54a6
AM
13108 /* If this symbol is weak and there is a non-weak definition, we
13109 keep the non-weak definition because many backends put
13110 dynamic reloc info on the non-weak definition for code
13111 handling copy relocs. */
60d67dc8
AM
13112 if (h->is_weakalias)
13113 weakdef (h)->mark = 1;
1cce69b9 13114
a6a4679f 13115 if (start_stop != NULL)
1cce69b9 13116 {
7dba9362
AM
13117 /* To work around a glibc bug, mark XXX input sections
13118 when there is a reference to __start_XXX or __stop_XXX
13119 symbols. */
cbd0eecf 13120 if (h->start_stop)
1cce69b9 13121 {
cbd0eecf 13122 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13123 *start_stop = !s->gc_mark;
13124 return s;
1cce69b9
AM
13125 }
13126 }
13127
5241d853
RS
13128 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13129 }
13130
13131 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13132 &cookie->locsyms[r_symndx]);
13133}
13134
13135/* COOKIE->rel describes a relocation against section SEC, which is
13136 a section we've decided to keep. Mark the section that contains
9d0a14d3 13137 the relocation symbol. */
5241d853
RS
13138
13139bfd_boolean
13140_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13141 asection *sec,
13142 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13143 struct elf_reloc_cookie *cookie)
5241d853
RS
13144{
13145 asection *rsec;
1cce69b9 13146 bfd_boolean start_stop = FALSE;
5241d853 13147
1cce69b9
AM
13148 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13149 while (rsec != NULL)
5241d853 13150 {
1cce69b9
AM
13151 if (!rsec->gc_mark)
13152 {
13153 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13154 || (rsec->owner->flags & DYNAMIC) != 0)
13155 rsec->gc_mark = 1;
13156 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13157 return FALSE;
13158 }
13159 if (!start_stop)
13160 break;
199af150 13161 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13162 }
13163 return TRUE;
13164}
13165
07adf181
AM
13166/* The mark phase of garbage collection. For a given section, mark
13167 it and any sections in this section's group, and all the sections
13168 which define symbols to which it refers. */
13169
ccfa59ea
AM
13170bfd_boolean
13171_bfd_elf_gc_mark (struct bfd_link_info *info,
13172 asection *sec,
6a5bb875 13173 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13174{
13175 bfd_boolean ret;
9d0a14d3 13176 asection *group_sec, *eh_frame;
c152c796
AM
13177
13178 sec->gc_mark = 1;
13179
13180 /* Mark all the sections in the group. */
13181 group_sec = elf_section_data (sec)->next_in_group;
13182 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13183 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13184 return FALSE;
13185
13186 /* Look through the section relocs. */
13187 ret = TRUE;
9d0a14d3
RS
13188 eh_frame = elf_eh_frame_section (sec->owner);
13189 if ((sec->flags & SEC_RELOC) != 0
13190 && sec->reloc_count > 0
13191 && sec != eh_frame)
c152c796 13192 {
5241d853 13193 struct elf_reloc_cookie cookie;
c152c796 13194
5241d853
RS
13195 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13196 ret = FALSE;
c152c796 13197 else
c152c796 13198 {
5241d853 13199 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13200 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13201 {
13202 ret = FALSE;
13203 break;
13204 }
13205 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13206 }
13207 }
9d0a14d3
RS
13208
13209 if (ret && eh_frame && elf_fde_list (sec))
13210 {
13211 struct elf_reloc_cookie cookie;
13212
13213 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13214 ret = FALSE;
13215 else
13216 {
13217 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13218 gc_mark_hook, &cookie))
13219 ret = FALSE;
13220 fini_reloc_cookie_for_section (&cookie, eh_frame);
13221 }
13222 }
13223
2f0c68f2
CM
13224 eh_frame = elf_section_eh_frame_entry (sec);
13225 if (ret && eh_frame && !eh_frame->gc_mark)
13226 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13227 ret = FALSE;
13228
c152c796
AM
13229 return ret;
13230}
13231
3c758495
TG
13232/* Scan and mark sections in a special or debug section group. */
13233
13234static void
13235_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13236{
13237 /* Point to first section of section group. */
13238 asection *ssec;
13239 /* Used to iterate the section group. */
13240 asection *msec;
13241
13242 bfd_boolean is_special_grp = TRUE;
13243 bfd_boolean is_debug_grp = TRUE;
13244
13245 /* First scan to see if group contains any section other than debug
13246 and special section. */
13247 ssec = msec = elf_next_in_group (grp);
13248 do
13249 {
13250 if ((msec->flags & SEC_DEBUGGING) == 0)
13251 is_debug_grp = FALSE;
13252
13253 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13254 is_special_grp = FALSE;
13255
13256 msec = elf_next_in_group (msec);
13257 }
13258 while (msec != ssec);
13259
13260 /* If this is a pure debug section group or pure special section group,
13261 keep all sections in this group. */
13262 if (is_debug_grp || is_special_grp)
13263 {
13264 do
13265 {
13266 msec->gc_mark = 1;
13267 msec = elf_next_in_group (msec);
13268 }
13269 while (msec != ssec);
13270 }
13271}
13272
7f6ab9f8
AM
13273/* Keep debug and special sections. */
13274
13275bfd_boolean
13276_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13277 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13278{
13279 bfd *ibfd;
13280
c72f2fb2 13281 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13282 {
13283 asection *isec;
13284 bfd_boolean some_kept;
b40bf0a2 13285 bfd_boolean debug_frag_seen;
b7c871ed 13286 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13287
13288 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13289 continue;
57963c05
AM
13290 isec = ibfd->sections;
13291 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13292 continue;
7f6ab9f8 13293
b40bf0a2
NC
13294 /* Ensure all linker created sections are kept,
13295 see if any other section is already marked,
13296 and note if we have any fragmented debug sections. */
b7c871ed 13297 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13298 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13299 {
13300 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13301 isec->gc_mark = 1;
eb026f09
AM
13302 else if (isec->gc_mark
13303 && (isec->flags & SEC_ALLOC) != 0
13304 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13305 some_kept = TRUE;
b40bf0a2 13306
535b785f 13307 if (!debug_frag_seen
b40bf0a2
NC
13308 && (isec->flags & SEC_DEBUGGING)
13309 && CONST_STRNEQ (isec->name, ".debug_line."))
13310 debug_frag_seen = TRUE;
7f6ab9f8
AM
13311 }
13312
eb026f09
AM
13313 /* If no non-note alloc section in this file will be kept, then
13314 we can toss out the debug and special sections. */
7f6ab9f8
AM
13315 if (!some_kept)
13316 continue;
13317
13318 /* Keep debug and special sections like .comment when they are
3c758495
TG
13319 not part of a group. Also keep section groups that contain
13320 just debug sections or special sections. */
7f6ab9f8 13321 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13322 {
13323 if ((isec->flags & SEC_GROUP) != 0)
13324 _bfd_elf_gc_mark_debug_special_section_group (isec);
13325 else if (((isec->flags & SEC_DEBUGGING) != 0
13326 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13327 && elf_next_in_group (isec) == NULL)
13328 isec->gc_mark = 1;
b7c871ed
L
13329 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13330 has_kept_debug_info = TRUE;
3c758495 13331 }
b40bf0a2 13332
b40bf0a2
NC
13333 /* Look for CODE sections which are going to be discarded,
13334 and find and discard any fragmented debug sections which
13335 are associated with that code section. */
b7c871ed
L
13336 if (debug_frag_seen)
13337 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13338 if ((isec->flags & SEC_CODE) != 0
13339 && isec->gc_mark == 0)
13340 {
13341 unsigned int ilen;
13342 asection *dsec;
b40bf0a2 13343
b7c871ed 13344 ilen = strlen (isec->name);
b40bf0a2 13345
b7c871ed 13346 /* Association is determined by the name of the debug
07d6d2b8 13347 section containing the name of the code section as
b7c871ed
L
13348 a suffix. For example .debug_line.text.foo is a
13349 debug section associated with .text.foo. */
13350 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13351 {
13352 unsigned int dlen;
b40bf0a2 13353
b7c871ed
L
13354 if (dsec->gc_mark == 0
13355 || (dsec->flags & SEC_DEBUGGING) == 0)
13356 continue;
b40bf0a2 13357
b7c871ed 13358 dlen = strlen (dsec->name);
b40bf0a2 13359
b7c871ed
L
13360 if (dlen > ilen
13361 && strncmp (dsec->name + (dlen - ilen),
13362 isec->name, ilen) == 0)
b40bf0a2 13363 dsec->gc_mark = 0;
b7c871ed 13364 }
b40bf0a2 13365 }
b7c871ed
L
13366
13367 /* Mark debug sections referenced by kept debug sections. */
13368 if (has_kept_debug_info)
13369 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13370 if (isec->gc_mark
13371 && (isec->flags & SEC_DEBUGGING) != 0)
13372 if (!_bfd_elf_gc_mark (info, isec,
13373 elf_gc_mark_debug_section))
13374 return FALSE;
7f6ab9f8
AM
13375 }
13376 return TRUE;
13377}
13378
c152c796 13379static bfd_boolean
ccabcbe5 13380elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13381{
13382 bfd *sub;
ccabcbe5 13383 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13384
c72f2fb2 13385 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13386 {
13387 asection *o;
13388
b19a8f85 13389 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13390 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13391 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13392 continue;
57963c05
AM
13393 o = sub->sections;
13394 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13395 continue;
c152c796
AM
13396
13397 for (o = sub->sections; o != NULL; o = o->next)
13398 {
a33dafc3
L
13399 /* When any section in a section group is kept, we keep all
13400 sections in the section group. If the first member of
13401 the section group is excluded, we will also exclude the
13402 group section. */
13403 if (o->flags & SEC_GROUP)
13404 {
13405 asection *first = elf_next_in_group (o);
13406 o->gc_mark = first->gc_mark;
13407 }
c152c796 13408
1e7eae0d 13409 if (o->gc_mark)
c152c796
AM
13410 continue;
13411
13412 /* Skip sweeping sections already excluded. */
13413 if (o->flags & SEC_EXCLUDE)
13414 continue;
13415
13416 /* Since this is early in the link process, it is simple
13417 to remove a section from the output. */
13418 o->flags |= SEC_EXCLUDE;
13419
c55fe096 13420 if (info->print_gc_sections && o->size != 0)
695344c0 13421 /* xgettext:c-format */
9793eb77 13422 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13423 o, sub);
c152c796
AM
13424 }
13425 }
13426
c152c796
AM
13427 return TRUE;
13428}
13429
13430/* Propagate collected vtable information. This is called through
13431 elf_link_hash_traverse. */
13432
13433static bfd_boolean
13434elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13435{
c152c796 13436 /* Those that are not vtables. */
cbd0eecf
L
13437 if (h->start_stop
13438 || h->u2.vtable == NULL
13439 || h->u2.vtable->parent == NULL)
c152c796
AM
13440 return TRUE;
13441
13442 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13443 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13444 return TRUE;
13445
13446 /* If we've already been done, exit. */
cbd0eecf 13447 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13448 return TRUE;
13449
13450 /* Make sure the parent's table is up to date. */
cbd0eecf 13451 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13452
cbd0eecf 13453 if (h->u2.vtable->used == NULL)
c152c796
AM
13454 {
13455 /* None of this table's entries were referenced. Re-use the
13456 parent's table. */
cbd0eecf
L
13457 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13458 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13459 }
13460 else
13461 {
13462 size_t n;
13463 bfd_boolean *cu, *pu;
13464
13465 /* Or the parent's entries into ours. */
cbd0eecf 13466 cu = h->u2.vtable->used;
c152c796 13467 cu[-1] = TRUE;
cbd0eecf 13468 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13469 if (pu != NULL)
13470 {
13471 const struct elf_backend_data *bed;
13472 unsigned int log_file_align;
13473
13474 bed = get_elf_backend_data (h->root.u.def.section->owner);
13475 log_file_align = bed->s->log_file_align;
cbd0eecf 13476 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13477 while (n--)
13478 {
13479 if (*pu)
13480 *cu = TRUE;
13481 pu++;
13482 cu++;
13483 }
13484 }
13485 }
13486
13487 return TRUE;
13488}
13489
13490static bfd_boolean
13491elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13492{
13493 asection *sec;
13494 bfd_vma hstart, hend;
13495 Elf_Internal_Rela *relstart, *relend, *rel;
13496 const struct elf_backend_data *bed;
13497 unsigned int log_file_align;
13498
c152c796
AM
13499 /* Take care of both those symbols that do not describe vtables as
13500 well as those that are not loaded. */
cbd0eecf
L
13501 if (h->start_stop
13502 || h->u2.vtable == NULL
13503 || h->u2.vtable->parent == NULL)
c152c796
AM
13504 return TRUE;
13505
13506 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13507 || h->root.type == bfd_link_hash_defweak);
13508
13509 sec = h->root.u.def.section;
13510 hstart = h->root.u.def.value;
13511 hend = hstart + h->size;
13512
13513 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13514 if (!relstart)
13515 return *(bfd_boolean *) okp = FALSE;
13516 bed = get_elf_backend_data (sec->owner);
13517 log_file_align = bed->s->log_file_align;
13518
056bafd4 13519 relend = relstart + sec->reloc_count;
c152c796
AM
13520
13521 for (rel = relstart; rel < relend; ++rel)
13522 if (rel->r_offset >= hstart && rel->r_offset < hend)
13523 {
13524 /* If the entry is in use, do nothing. */
cbd0eecf
L
13525 if (h->u2.vtable->used
13526 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13527 {
13528 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13529 if (h->u2.vtable->used[entry])
c152c796
AM
13530 continue;
13531 }
13532 /* Otherwise, kill it. */
13533 rel->r_offset = rel->r_info = rel->r_addend = 0;
13534 }
13535
13536 return TRUE;
13537}
13538
87538722
AM
13539/* Mark sections containing dynamically referenced symbols. When
13540 building shared libraries, we must assume that any visible symbol is
13541 referenced. */
715df9b8 13542
64d03ab5
AM
13543bfd_boolean
13544bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13545{
87538722 13546 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13547 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13548
715df9b8
EB
13549 if ((h->root.type == bfd_link_hash_defined
13550 || h->root.type == bfd_link_hash_defweak)
d664fd41 13551 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13552 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13553 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13554 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13555 && (!bfd_link_executable (info)
22185505 13556 || info->gc_keep_exported
b407645f
AM
13557 || info->export_dynamic
13558 || (h->dynamic
13559 && d != NULL
13560 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13561 && (h->versioned >= versioned
54e8959c
L
13562 || !bfd_hide_sym_by_version (info->version_info,
13563 h->root.root.string)))))
715df9b8
EB
13564 h->root.u.def.section->flags |= SEC_KEEP;
13565
13566 return TRUE;
13567}
3b36f7e6 13568
74f0fb50
AM
13569/* Keep all sections containing symbols undefined on the command-line,
13570 and the section containing the entry symbol. */
13571
13572void
13573_bfd_elf_gc_keep (struct bfd_link_info *info)
13574{
13575 struct bfd_sym_chain *sym;
13576
13577 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13578 {
13579 struct elf_link_hash_entry *h;
13580
13581 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13582 FALSE, FALSE, FALSE);
13583
13584 if (h != NULL
13585 && (h->root.type == bfd_link_hash_defined
13586 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13587 && !bfd_is_abs_section (h->root.u.def.section)
13588 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13589 h->root.u.def.section->flags |= SEC_KEEP;
13590 }
13591}
13592
2f0c68f2
CM
13593bfd_boolean
13594bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13595 struct bfd_link_info *info)
13596{
13597 bfd *ibfd = info->input_bfds;
13598
13599 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13600 {
13601 asection *sec;
13602 struct elf_reloc_cookie cookie;
13603
13604 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13605 continue;
57963c05
AM
13606 sec = ibfd->sections;
13607 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13608 continue;
2f0c68f2
CM
13609
13610 if (!init_reloc_cookie (&cookie, info, ibfd))
13611 return FALSE;
13612
13613 for (sec = ibfd->sections; sec; sec = sec->next)
13614 {
fd361982 13615 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13616 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13617 {
13618 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13619 fini_reloc_cookie_rels (&cookie, sec);
13620 }
13621 }
13622 }
13623 return TRUE;
13624}
13625
c152c796
AM
13626/* Do mark and sweep of unused sections. */
13627
13628bfd_boolean
13629bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13630{
13631 bfd_boolean ok = TRUE;
13632 bfd *sub;
6a5bb875 13633 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13634 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13635 struct elf_link_hash_table *htab;
c152c796 13636
64d03ab5 13637 if (!bed->can_gc_sections
715df9b8 13638 || !is_elf_hash_table (info->hash))
c152c796 13639 {
9793eb77 13640 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13641 return TRUE;
13642 }
13643
74f0fb50 13644 bed->gc_keep (info);
da44f4e5 13645 htab = elf_hash_table (info);
74f0fb50 13646
9d0a14d3
RS
13647 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13648 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13649 for (sub = info->input_bfds;
13650 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13651 sub = sub->link.next)
9d0a14d3
RS
13652 {
13653 asection *sec;
13654 struct elf_reloc_cookie cookie;
13655
57963c05
AM
13656 sec = sub->sections;
13657 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13658 continue;
9d0a14d3 13659 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13660 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13661 {
13662 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13663 if (elf_section_data (sec)->sec_info
13664 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13665 elf_eh_frame_section (sub) = sec;
13666 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13667 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13668 }
13669 }
9d0a14d3 13670
c152c796 13671 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13672 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13673 if (!ok)
13674 return FALSE;
13675
13676 /* Kill the vtable relocations that were not used. */
da44f4e5 13677 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13678 if (!ok)
13679 return FALSE;
13680
715df9b8 13681 /* Mark dynamically referenced symbols. */
22185505 13682 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13683 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13684
715df9b8 13685 /* Grovel through relocs to find out who stays ... */
64d03ab5 13686 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13687 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13688 {
13689 asection *o;
13690
b19a8f85 13691 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13692 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13693 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13694 continue;
13695
57963c05
AM
13696 o = sub->sections;
13697 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13698 continue;
13699
7f6ab9f8
AM
13700 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13701 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13702 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13703 well as FINI_ARRAY sections for ld -r. */
c152c796 13704 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13705 if (!o->gc_mark
13706 && (o->flags & SEC_EXCLUDE) == 0
24007750 13707 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13708 || (bfd_link_relocatable (info)
13709 && ((elf_section_data (o)->this_hdr.sh_type
13710 == SHT_PREINIT_ARRAY)
13711 || (elf_section_data (o)->this_hdr.sh_type
13712 == SHT_INIT_ARRAY)
13713 || (elf_section_data (o)->this_hdr.sh_type
13714 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13715 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13716 && elf_next_in_group (o) == NULL )))
13717 {
13718 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13719 return FALSE;
13720 }
c152c796
AM
13721 }
13722
6a5bb875 13723 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13724 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13725
c152c796 13726 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13727 return elf_gc_sweep (abfd, info);
c152c796
AM
13728}
13729\f
13730/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13731
13732bfd_boolean
13733bfd_elf_gc_record_vtinherit (bfd *abfd,
13734 asection *sec,
13735 struct elf_link_hash_entry *h,
13736 bfd_vma offset)
13737{
13738 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13739 struct elf_link_hash_entry **search, *child;
ef53be89 13740 size_t extsymcount;
c152c796
AM
13741 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13742
13743 /* The sh_info field of the symtab header tells us where the
13744 external symbols start. We don't care about the local symbols at
13745 this point. */
13746 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13747 if (!elf_bad_symtab (abfd))
13748 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13749
13750 sym_hashes = elf_sym_hashes (abfd);
13751 sym_hashes_end = sym_hashes + extsymcount;
13752
13753 /* Hunt down the child symbol, which is in this section at the same
13754 offset as the relocation. */
13755 for (search = sym_hashes; search != sym_hashes_end; ++search)
13756 {
13757 if ((child = *search) != NULL
13758 && (child->root.type == bfd_link_hash_defined
13759 || child->root.type == bfd_link_hash_defweak)
13760 && child->root.u.def.section == sec
13761 && child->root.u.def.value == offset)
13762 goto win;
13763 }
13764
695344c0 13765 /* xgettext:c-format */
9793eb77 13766 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13767 abfd, sec, (uint64_t) offset);
c152c796
AM
13768 bfd_set_error (bfd_error_invalid_operation);
13769 return FALSE;
13770
13771 win:
cbd0eecf 13772 if (!child->u2.vtable)
f6e332e6 13773 {
cbd0eecf
L
13774 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13775 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13776 if (!child->u2.vtable)
f6e332e6
AM
13777 return FALSE;
13778 }
c152c796
AM
13779 if (!h)
13780 {
13781 /* This *should* only be the absolute section. It could potentially
13782 be that someone has defined a non-global vtable though, which
13783 would be bad. It isn't worth paging in the local symbols to be
13784 sure though; that case should simply be handled by the assembler. */
13785
cbd0eecf 13786 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13787 }
13788 else
cbd0eecf 13789 child->u2.vtable->parent = h;
c152c796
AM
13790
13791 return TRUE;
13792}
13793
13794/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13795
13796bfd_boolean
a0ea3a14 13797bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
13798 struct elf_link_hash_entry *h,
13799 bfd_vma addend)
13800{
13801 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13802 unsigned int log_file_align = bed->s->log_file_align;
13803
a0ea3a14
L
13804 if (!h)
13805 {
13806 /* xgettext:c-format */
13807 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13808 abfd, sec);
13809 bfd_set_error (bfd_error_bad_value);
13810 return FALSE;
13811 }
13812
cbd0eecf 13813 if (!h->u2.vtable)
f6e332e6 13814 {
cbd0eecf
L
13815 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13816 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13817 if (!h->u2.vtable)
f6e332e6
AM
13818 return FALSE;
13819 }
13820
cbd0eecf 13821 if (addend >= h->u2.vtable->size)
c152c796
AM
13822 {
13823 size_t size, bytes, file_align;
cbd0eecf 13824 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13825
13826 /* While the symbol is undefined, we have to be prepared to handle
13827 a zero size. */
13828 file_align = 1 << log_file_align;
13829 if (h->root.type == bfd_link_hash_undefined)
13830 size = addend + file_align;
13831 else
13832 {
13833 size = h->size;
13834 if (addend >= size)
13835 {
13836 /* Oops! We've got a reference past the defined end of
13837 the table. This is probably a bug -- shall we warn? */
13838 size = addend + file_align;
13839 }
13840 }
13841 size = (size + file_align - 1) & -file_align;
13842
13843 /* Allocate one extra entry for use as a "done" flag for the
13844 consolidation pass. */
13845 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13846
13847 if (ptr)
13848 {
a50b1753 13849 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13850
13851 if (ptr != NULL)
13852 {
13853 size_t oldbytes;
13854
cbd0eecf 13855 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13856 * sizeof (bfd_boolean));
13857 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13858 }
13859 }
13860 else
a50b1753 13861 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13862
13863 if (ptr == NULL)
13864 return FALSE;
13865
13866 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13867 h->u2.vtable->used = ptr + 1;
13868 h->u2.vtable->size = size;
c152c796
AM
13869 }
13870
cbd0eecf 13871 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13872
13873 return TRUE;
13874}
13875
ae17ab41
CM
13876/* Map an ELF section header flag to its corresponding string. */
13877typedef struct
13878{
13879 char *flag_name;
13880 flagword flag_value;
13881} elf_flags_to_name_table;
13882
13883static elf_flags_to_name_table elf_flags_to_names [] =
13884{
13885 { "SHF_WRITE", SHF_WRITE },
13886 { "SHF_ALLOC", SHF_ALLOC },
13887 { "SHF_EXECINSTR", SHF_EXECINSTR },
13888 { "SHF_MERGE", SHF_MERGE },
13889 { "SHF_STRINGS", SHF_STRINGS },
13890 { "SHF_INFO_LINK", SHF_INFO_LINK},
13891 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13892 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13893 { "SHF_GROUP", SHF_GROUP },
13894 { "SHF_TLS", SHF_TLS },
13895 { "SHF_MASKOS", SHF_MASKOS },
13896 { "SHF_EXCLUDE", SHF_EXCLUDE },
13897};
13898
b9c361e0
JL
13899/* Returns TRUE if the section is to be included, otherwise FALSE. */
13900bfd_boolean
ae17ab41 13901bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13902 struct flag_info *flaginfo,
b9c361e0 13903 asection *section)
ae17ab41 13904{
8b127cbc 13905 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13906
8b127cbc 13907 if (!flaginfo->flags_initialized)
ae17ab41 13908 {
8b127cbc
AM
13909 bfd *obfd = info->output_bfd;
13910 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13911 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13912 int with_hex = 0;
13913 int without_hex = 0;
13914
8b127cbc 13915 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13916 {
b9c361e0 13917 unsigned i;
8b127cbc 13918 flagword (*lookup) (char *);
ae17ab41 13919
8b127cbc
AM
13920 lookup = bed->elf_backend_lookup_section_flags_hook;
13921 if (lookup != NULL)
ae17ab41 13922 {
8b127cbc 13923 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13924
13925 if (hexval != 0)
13926 {
13927 if (tf->with == with_flags)
13928 with_hex |= hexval;
13929 else if (tf->with == without_flags)
13930 without_hex |= hexval;
13931 tf->valid = TRUE;
13932 continue;
13933 }
ae17ab41 13934 }
8b127cbc 13935 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13936 {
8b127cbc 13937 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13938 {
13939 if (tf->with == with_flags)
13940 with_hex |= elf_flags_to_names[i].flag_value;
13941 else if (tf->with == without_flags)
13942 without_hex |= elf_flags_to_names[i].flag_value;
13943 tf->valid = TRUE;
13944 break;
13945 }
13946 }
8b127cbc 13947 if (!tf->valid)
b9c361e0 13948 {
68ffbac6 13949 info->callbacks->einfo
9793eb77 13950 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13951 return FALSE;
ae17ab41
CM
13952 }
13953 }
8b127cbc
AM
13954 flaginfo->flags_initialized = TRUE;
13955 flaginfo->only_with_flags |= with_hex;
13956 flaginfo->not_with_flags |= without_hex;
ae17ab41 13957 }
ae17ab41 13958
8b127cbc 13959 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13960 return FALSE;
13961
8b127cbc 13962 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13963 return FALSE;
13964
13965 return TRUE;
ae17ab41
CM
13966}
13967
c152c796
AM
13968struct alloc_got_off_arg {
13969 bfd_vma gotoff;
10455f89 13970 struct bfd_link_info *info;
c152c796
AM
13971};
13972
13973/* We need a special top-level link routine to convert got reference counts
13974 to real got offsets. */
13975
13976static bfd_boolean
13977elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13978{
a50b1753 13979 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13980 bfd *obfd = gofarg->info->output_bfd;
13981 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13982
c152c796
AM
13983 if (h->got.refcount > 0)
13984 {
13985 h->got.offset = gofarg->gotoff;
10455f89 13986 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13987 }
13988 else
13989 h->got.offset = (bfd_vma) -1;
13990
13991 return TRUE;
13992}
13993
13994/* And an accompanying bit to work out final got entry offsets once
13995 we're done. Should be called from final_link. */
13996
13997bfd_boolean
13998bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13999 struct bfd_link_info *info)
14000{
14001 bfd *i;
14002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14003 bfd_vma gotoff;
c152c796
AM
14004 struct alloc_got_off_arg gofarg;
14005
10455f89
HPN
14006 BFD_ASSERT (abfd == info->output_bfd);
14007
c152c796
AM
14008 if (! is_elf_hash_table (info->hash))
14009 return FALSE;
14010
14011 /* The GOT offset is relative to the .got section, but the GOT header is
14012 put into the .got.plt section, if the backend uses it. */
14013 if (bed->want_got_plt)
14014 gotoff = 0;
14015 else
14016 gotoff = bed->got_header_size;
14017
14018 /* Do the local .got entries first. */
c72f2fb2 14019 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14020 {
14021 bfd_signed_vma *local_got;
ef53be89 14022 size_t j, locsymcount;
c152c796
AM
14023 Elf_Internal_Shdr *symtab_hdr;
14024
14025 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14026 continue;
14027
14028 local_got = elf_local_got_refcounts (i);
14029 if (!local_got)
14030 continue;
14031
14032 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14033 if (elf_bad_symtab (i))
14034 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14035 else
14036 locsymcount = symtab_hdr->sh_info;
14037
14038 for (j = 0; j < locsymcount; ++j)
14039 {
14040 if (local_got[j] > 0)
14041 {
14042 local_got[j] = gotoff;
10455f89 14043 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14044 }
14045 else
14046 local_got[j] = (bfd_vma) -1;
14047 }
14048 }
14049
14050 /* Then the global .got entries. .plt refcounts are handled by
14051 adjust_dynamic_symbol */
14052 gofarg.gotoff = gotoff;
10455f89 14053 gofarg.info = info;
c152c796
AM
14054 elf_link_hash_traverse (elf_hash_table (info),
14055 elf_gc_allocate_got_offsets,
14056 &gofarg);
14057 return TRUE;
14058}
14059
14060/* Many folk need no more in the way of final link than this, once
14061 got entry reference counting is enabled. */
14062
14063bfd_boolean
14064bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14065{
14066 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14067 return FALSE;
14068
14069 /* Invoke the regular ELF backend linker to do all the work. */
14070 return bfd_elf_final_link (abfd, info);
14071}
14072
14073bfd_boolean
14074bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14075{
a50b1753 14076 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14077
14078 if (rcookie->bad_symtab)
14079 rcookie->rel = rcookie->rels;
14080
14081 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14082 {
14083 unsigned long r_symndx;
14084
14085 if (! rcookie->bad_symtab)
14086 if (rcookie->rel->r_offset > offset)
14087 return FALSE;
14088 if (rcookie->rel->r_offset != offset)
14089 continue;
14090
14091 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14092 if (r_symndx == STN_UNDEF)
c152c796
AM
14093 return TRUE;
14094
14095 if (r_symndx >= rcookie->locsymcount
14096 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14097 {
14098 struct elf_link_hash_entry *h;
14099
14100 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14101
14102 while (h->root.type == bfd_link_hash_indirect
14103 || h->root.type == bfd_link_hash_warning)
14104 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14105
14106 if ((h->root.type == bfd_link_hash_defined
14107 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14108 && (h->root.u.def.section->owner != rcookie->abfd
14109 || h->root.u.def.section->kept_section != NULL
14110 || discarded_section (h->root.u.def.section)))
c152c796 14111 return TRUE;
c152c796
AM
14112 }
14113 else
14114 {
14115 /* It's not a relocation against a global symbol,
14116 but it could be a relocation against a local
14117 symbol for a discarded section. */
14118 asection *isec;
14119 Elf_Internal_Sym *isym;
14120
14121 /* Need to: get the symbol; get the section. */
14122 isym = &rcookie->locsyms[r_symndx];
cb33740c 14123 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14124 if (isec != NULL
14125 && (isec->kept_section != NULL
14126 || discarded_section (isec)))
cb33740c 14127 return TRUE;
c152c796
AM
14128 }
14129 return FALSE;
14130 }
14131 return FALSE;
14132}
14133
14134/* Discard unneeded references to discarded sections.
75938853
AM
14135 Returns -1 on error, 1 if any section's size was changed, 0 if
14136 nothing changed. This function assumes that the relocations are in
14137 sorted order, which is true for all known assemblers. */
c152c796 14138
75938853 14139int
c152c796
AM
14140bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14141{
14142 struct elf_reloc_cookie cookie;
18cd5bce 14143 asection *o;
c152c796 14144 bfd *abfd;
75938853 14145 int changed = 0;
c152c796
AM
14146
14147 if (info->traditional_format
14148 || !is_elf_hash_table (info->hash))
75938853 14149 return 0;
c152c796 14150
18cd5bce
AM
14151 o = bfd_get_section_by_name (output_bfd, ".stab");
14152 if (o != NULL)
c152c796 14153 {
18cd5bce 14154 asection *i;
c152c796 14155
18cd5bce 14156 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14157 {
18cd5bce
AM
14158 if (i->size == 0
14159 || i->reloc_count == 0
14160 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14161 continue;
c152c796 14162
18cd5bce
AM
14163 abfd = i->owner;
14164 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14165 continue;
c152c796 14166
18cd5bce 14167 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14168 return -1;
c152c796 14169
18cd5bce
AM
14170 if (_bfd_discard_section_stabs (abfd, i,
14171 elf_section_data (i)->sec_info,
5241d853
RS
14172 bfd_elf_reloc_symbol_deleted_p,
14173 &cookie))
75938853 14174 changed = 1;
18cd5bce
AM
14175
14176 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14177 }
18cd5bce
AM
14178 }
14179
2f0c68f2
CM
14180 o = NULL;
14181 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14182 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14183 if (o != NULL)
14184 {
14185 asection *i;
d7153c4a 14186 int eh_changed = 0;
79a94a2a 14187 unsigned int eh_alignment;
c152c796 14188
18cd5bce 14189 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14190 {
18cd5bce
AM
14191 if (i->size == 0)
14192 continue;
14193
14194 abfd = i->owner;
14195 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14196 continue;
14197
14198 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14199 return -1;
18cd5bce
AM
14200
14201 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14202 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14203 bfd_elf_reloc_symbol_deleted_p,
14204 &cookie))
d7153c4a
AM
14205 {
14206 eh_changed = 1;
14207 if (i->size != i->rawsize)
14208 changed = 1;
14209 }
18cd5bce
AM
14210
14211 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14212 }
9866ffe2 14213
79a94a2a 14214 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14215 /* Skip over zero terminator, and prevent empty sections from
14216 adding alignment padding at the end. */
14217 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14218 if (i->size == 0)
14219 i->flags |= SEC_EXCLUDE;
14220 else if (i->size > 4)
14221 break;
14222 /* The last non-empty eh_frame section doesn't need padding. */
14223 if (i != NULL)
14224 i = i->map_tail.s;
14225 /* Any prior sections must pad the last FDE out to the output
14226 section alignment. Otherwise we might have zero padding
14227 between sections, which would be seen as a terminator. */
14228 for (; i != NULL; i = i->map_tail.s)
14229 if (i->size == 4)
14230 /* All but the last zero terminator should have been removed. */
14231 BFD_FAIL ();
14232 else
14233 {
14234 bfd_size_type size
14235 = (i->size + eh_alignment - 1) & -eh_alignment;
14236 if (i->size != size)
af471f82 14237 {
9866ffe2
AM
14238 i->size = size;
14239 changed = 1;
14240 eh_changed = 1;
af471f82 14241 }
9866ffe2 14242 }
d7153c4a
AM
14243 if (eh_changed)
14244 elf_link_hash_traverse (elf_hash_table (info),
14245 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14246 }
c152c796 14247
18cd5bce
AM
14248 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14249 {
14250 const struct elf_backend_data *bed;
57963c05 14251 asection *s;
c152c796 14252
18cd5bce
AM
14253 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14254 continue;
57963c05
AM
14255 s = abfd->sections;
14256 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14257 continue;
18cd5bce
AM
14258
14259 bed = get_elf_backend_data (abfd);
14260
14261 if (bed->elf_backend_discard_info != NULL)
14262 {
14263 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14264 return -1;
18cd5bce
AM
14265
14266 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14267 changed = 1;
18cd5bce
AM
14268
14269 fini_reloc_cookie (&cookie, abfd);
14270 }
c152c796
AM
14271 }
14272
2f0c68f2
CM
14273 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14274 _bfd_elf_end_eh_frame_parsing (info);
14275
14276 if (info->eh_frame_hdr_type
0e1862bb 14277 && !bfd_link_relocatable (info)
c152c796 14278 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14279 changed = 1;
c152c796 14280
75938853 14281 return changed;
c152c796 14282}
082b7297 14283
43e1669b 14284bfd_boolean
0c511000 14285_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14286 asection *sec,
c0f00686 14287 struct bfd_link_info *info)
082b7297
L
14288{
14289 flagword flags;
c77ec726 14290 const char *name, *key;
082b7297
L
14291 struct bfd_section_already_linked *l;
14292 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14293
c77ec726
AM
14294 if (sec->output_section == bfd_abs_section_ptr)
14295 return FALSE;
0c511000 14296
c77ec726 14297 flags = sec->flags;
0c511000 14298
c77ec726
AM
14299 /* Return if it isn't a linkonce section. A comdat group section
14300 also has SEC_LINK_ONCE set. */
14301 if ((flags & SEC_LINK_ONCE) == 0)
14302 return FALSE;
0c511000 14303
c77ec726
AM
14304 /* Don't put group member sections on our list of already linked
14305 sections. They are handled as a group via their group section. */
14306 if (elf_sec_group (sec) != NULL)
14307 return FALSE;
0c511000 14308
c77ec726
AM
14309 /* For a SHT_GROUP section, use the group signature as the key. */
14310 name = sec->name;
14311 if ((flags & SEC_GROUP) != 0
14312 && elf_next_in_group (sec) != NULL
14313 && elf_group_name (elf_next_in_group (sec)) != NULL)
14314 key = elf_group_name (elf_next_in_group (sec));
14315 else
14316 {
14317 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14318 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14319 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14320 key++;
0c511000 14321 else
c77ec726
AM
14322 /* Must be a user linkonce section that doesn't follow gcc's
14323 naming convention. In this case we won't be matching
14324 single member groups. */
14325 key = name;
0c511000 14326 }
6d2cd210 14327
c77ec726 14328 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14329
14330 for (l = already_linked_list->entry; l != NULL; l = l->next)
14331 {
c2370991 14332 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14333 sections with a signature of <key> (<key> is some string),
14334 and linkonce sections named .gnu.linkonce.<type>.<key>.
14335 Match like sections. LTO plugin sections are an exception.
14336 They are always named .gnu.linkonce.t.<key> and match either
14337 type of section. */
14338 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14339 && ((flags & SEC_GROUP) != 0
14340 || strcmp (name, l->sec->name) == 0))
14341 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14342 {
14343 /* The section has already been linked. See if we should
6d2cd210 14344 issue a warning. */
c77ec726
AM
14345 if (!_bfd_handle_already_linked (sec, l, info))
14346 return FALSE;
082b7297 14347
c77ec726 14348 if (flags & SEC_GROUP)
3d7f7666 14349 {
c77ec726
AM
14350 asection *first = elf_next_in_group (sec);
14351 asection *s = first;
3d7f7666 14352
c77ec726 14353 while (s != NULL)
3d7f7666 14354 {
c77ec726
AM
14355 s->output_section = bfd_abs_section_ptr;
14356 /* Record which group discards it. */
14357 s->kept_section = l->sec;
14358 s = elf_next_in_group (s);
14359 /* These lists are circular. */
14360 if (s == first)
14361 break;
3d7f7666
L
14362 }
14363 }
082b7297 14364
43e1669b 14365 return TRUE;
082b7297
L
14366 }
14367 }
14368
c77ec726
AM
14369 /* A single member comdat group section may be discarded by a
14370 linkonce section and vice versa. */
14371 if ((flags & SEC_GROUP) != 0)
3d7f7666 14372 {
c77ec726 14373 asection *first = elf_next_in_group (sec);
c2370991 14374
c77ec726
AM
14375 if (first != NULL && elf_next_in_group (first) == first)
14376 /* Check this single member group against linkonce sections. */
14377 for (l = already_linked_list->entry; l != NULL; l = l->next)
14378 if ((l->sec->flags & SEC_GROUP) == 0
14379 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14380 {
14381 first->output_section = bfd_abs_section_ptr;
14382 first->kept_section = l->sec;
14383 sec->output_section = bfd_abs_section_ptr;
14384 break;
14385 }
14386 }
14387 else
14388 /* Check this linkonce section against single member groups. */
14389 for (l = already_linked_list->entry; l != NULL; l = l->next)
14390 if (l->sec->flags & SEC_GROUP)
6d2cd210 14391 {
c77ec726 14392 asection *first = elf_next_in_group (l->sec);
6d2cd210 14393
c77ec726
AM
14394 if (first != NULL
14395 && elf_next_in_group (first) == first
14396 && bfd_elf_match_symbols_in_sections (first, sec, info))
14397 {
14398 sec->output_section = bfd_abs_section_ptr;
14399 sec->kept_section = first;
14400 break;
14401 }
6d2cd210 14402 }
0c511000 14403
c77ec726
AM
14404 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14405 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14406 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14407 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14408 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14409 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14410 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14411 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14412 The reverse order cannot happen as there is never a bfd with only the
14413 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14414 matter as here were are looking only for cross-bfd sections. */
14415
14416 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14417 for (l = already_linked_list->entry; l != NULL; l = l->next)
14418 if ((l->sec->flags & SEC_GROUP) == 0
14419 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14420 {
14421 if (abfd != l->sec->owner)
14422 sec->output_section = bfd_abs_section_ptr;
14423 break;
14424 }
80c29487 14425
082b7297 14426 /* This is the first section with this name. Record it. */
c77ec726 14427 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14428 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14429 return sec->output_section == bfd_abs_section_ptr;
082b7297 14430}
81e1b023 14431
a4d8e49b
L
14432bfd_boolean
14433_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14434{
14435 return sym->st_shndx == SHN_COMMON;
14436}
14437
14438unsigned int
14439_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14440{
14441 return SHN_COMMON;
14442}
14443
14444asection *
14445_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14446{
14447 return bfd_com_section_ptr;
14448}
10455f89
HPN
14449
14450bfd_vma
14451_bfd_elf_default_got_elt_size (bfd *abfd,
14452 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14453 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14454 bfd *ibfd ATTRIBUTE_UNUSED,
14455 unsigned long symndx ATTRIBUTE_UNUSED)
14456{
14457 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14458 return bed->s->arch_size / 8;
14459}
83bac4b0
NC
14460
14461/* Routines to support the creation of dynamic relocs. */
14462
83bac4b0
NC
14463/* Returns the name of the dynamic reloc section associated with SEC. */
14464
14465static const char *
14466get_dynamic_reloc_section_name (bfd * abfd,
14467 asection * sec,
14468 bfd_boolean is_rela)
14469{
ddcf1fcf 14470 char *name;
fd361982 14471 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14472 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14473
ddcf1fcf 14474 if (old_name == NULL)
83bac4b0
NC
14475 return NULL;
14476
ddcf1fcf 14477 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14478 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14479
14480 return name;
14481}
14482
14483/* Returns the dynamic reloc section associated with SEC.
14484 If necessary compute the name of the dynamic reloc section based
14485 on SEC's name (looked up in ABFD's string table) and the setting
14486 of IS_RELA. */
14487
14488asection *
14489_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14490 asection * sec,
14491 bfd_boolean is_rela)
14492{
14493 asection * reloc_sec = elf_section_data (sec)->sreloc;
14494
14495 if (reloc_sec == NULL)
14496 {
14497 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14498
14499 if (name != NULL)
14500 {
3d4d4302 14501 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14502
14503 if (reloc_sec != NULL)
14504 elf_section_data (sec)->sreloc = reloc_sec;
14505 }
14506 }
14507
14508 return reloc_sec;
14509}
14510
14511/* Returns the dynamic reloc section associated with SEC. If the
14512 section does not exist it is created and attached to the DYNOBJ
14513 bfd and stored in the SRELOC field of SEC's elf_section_data
14514 structure.
f8076f98 14515
83bac4b0
NC
14516 ALIGNMENT is the alignment for the newly created section and
14517 IS_RELA defines whether the name should be .rela.<SEC's name>
14518 or .rel.<SEC's name>. The section name is looked up in the
14519 string table associated with ABFD. */
14520
14521asection *
ca4be51c
AM
14522_bfd_elf_make_dynamic_reloc_section (asection *sec,
14523 bfd *dynobj,
14524 unsigned int alignment,
14525 bfd *abfd,
14526 bfd_boolean is_rela)
83bac4b0
NC
14527{
14528 asection * reloc_sec = elf_section_data (sec)->sreloc;
14529
14530 if (reloc_sec == NULL)
14531 {
14532 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14533
14534 if (name == NULL)
14535 return NULL;
14536
3d4d4302 14537 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14538
14539 if (reloc_sec == NULL)
14540 {
3d4d4302
AM
14541 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14542 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14543 if ((sec->flags & SEC_ALLOC) != 0)
14544 flags |= SEC_ALLOC | SEC_LOAD;
14545
3d4d4302 14546 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14547 if (reloc_sec != NULL)
14548 {
8877b5e5
AM
14549 /* _bfd_elf_get_sec_type_attr chooses a section type by
14550 name. Override as it may be wrong, eg. for a user
14551 section named "auto" we'll get ".relauto" which is
14552 seen to be a .rela section. */
14553 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14554 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14555 reloc_sec = NULL;
14556 }
14557 }
14558
14559 elf_section_data (sec)->sreloc = reloc_sec;
14560 }
14561
14562 return reloc_sec;
14563}
1338dd10 14564
bffebb6b
AM
14565/* Copy the ELF symbol type and other attributes for a linker script
14566 assignment from HSRC to HDEST. Generally this should be treated as
14567 if we found a strong non-dynamic definition for HDEST (except that
14568 ld ignores multiple definition errors). */
1338dd10 14569void
bffebb6b
AM
14570_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14571 struct bfd_link_hash_entry *hdest,
14572 struct bfd_link_hash_entry *hsrc)
1338dd10 14573{
bffebb6b
AM
14574 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14575 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14576 Elf_Internal_Sym isym;
1338dd10
PB
14577
14578 ehdest->type = ehsrc->type;
35fc36a8 14579 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14580
14581 isym.st_other = ehsrc->other;
b8417128 14582 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14583}
351f65ca
L
14584
14585/* Append a RELA relocation REL to section S in BFD. */
14586
14587void
14588elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14589{
14590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14591 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14592 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14593 bed->s->swap_reloca_out (abfd, rel, loc);
14594}
14595
14596/* Append a REL relocation REL to section S in BFD. */
14597
14598void
14599elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14600{
14601 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14602 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14603 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14604 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14605}
7dba9362
AM
14606
14607/* Define __start, __stop, .startof. or .sizeof. symbol. */
14608
14609struct bfd_link_hash_entry *
14610bfd_elf_define_start_stop (struct bfd_link_info *info,
14611 const char *symbol, asection *sec)
14612{
487b6440 14613 struct elf_link_hash_entry *h;
7dba9362 14614
487b6440
AM
14615 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14616 FALSE, FALSE, TRUE);
14617 if (h != NULL
14618 && (h->root.type == bfd_link_hash_undefined
14619 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14620 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14621 {
bf3077a6 14622 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14623 h->root.type = bfd_link_hash_defined;
14624 h->root.u.def.section = sec;
14625 h->root.u.def.value = 0;
14626 h->def_regular = 1;
14627 h->def_dynamic = 0;
14628 h->start_stop = 1;
14629 h->u2.start_stop_section = sec;
14630 if (symbol[0] == '.')
14631 {
14632 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14633 const struct elf_backend_data *bed;
14634 bed = get_elf_backend_data (info->output_bfd);
14635 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14636 }
36b8fda5
AM
14637 else
14638 {
14639 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14640 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14641 if (was_dynamic)
36b8fda5
AM
14642 bfd_elf_link_record_dynamic_symbol (info, h);
14643 }
487b6440 14644 return &h->root;
7dba9362 14645 }
487b6440 14646 return NULL;
7dba9362 14647}