PR25351 .ARM.attributes not found for symbol
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
b3adc24a 2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
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
5fa370e4 1907 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
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. */
5fa370e4 2370 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
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
3a3f4bf7 3590/* Sort symbol by value, section, size, and type. */
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;
3a3f4bf7
AM
3597 int sdiff;
3598 const char *n1;
3599 const char *n2;
5a580b3a
AM
3600
3601 h1 = *(const struct elf_link_hash_entry **) arg1;
3602 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3603 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3604 if (vdiff != 0)
3605 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3606
3607 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3608 if (sdiff != 0)
3609 return sdiff;
3610
3611 /* Sort so that sized symbols are selected over zero size symbols. */
3612 vdiff = h1->size - h2->size;
3613 if (vdiff != 0)
3614 return vdiff > 0 ? 1 : -1;
3615
3616 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3617 if (h1->type != h2->type)
3618 return h1->type - h2->type;
3619
3620 /* If symbols are properly sized and typed, and multiple strong
3621 aliases are not defined in a shared library by the user we
3622 shouldn't get here. Unfortunately linker script symbols like
3623 __bss_start sometimes match a user symbol defined at the start of
3624 .bss without proper size and type. We'd like to preference the
3625 user symbol over reserved system symbols. Sort on leading
3626 underscores. */
3627 n1 = h1->root.root.string;
3628 n2 = h2->root.root.string;
3629 while (*n1 == *n2)
10b7e05b 3630 {
3a3f4bf7
AM
3631 if (*n1 == 0)
3632 break;
3633 ++n1;
3634 ++n2;
10b7e05b 3635 }
3a3f4bf7
AM
3636 if (*n1 == '_')
3637 return -1;
3638 if (*n2 == '_')
3639 return 1;
3640
3641 /* Final sort on name selects user symbols like '_u' over reserved
3642 system symbols like '_Z' and also will avoid qsort instability. */
3643 return *n1 - *n2;
5a580b3a 3644}
4ad4eba5 3645
5a580b3a
AM
3646/* This function is used to adjust offsets into .dynstr for
3647 dynamic symbols. This is called via elf_link_hash_traverse. */
3648
3649static bfd_boolean
3650elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3651{
a50b1753 3652 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3653
5a580b3a
AM
3654 if (h->dynindx != -1)
3655 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3656 return TRUE;
3657}
3658
3659/* Assign string offsets in .dynstr, update all structures referencing
3660 them. */
3661
4ad4eba5
AM
3662static bfd_boolean
3663elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3664{
3665 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3666 struct elf_link_local_dynamic_entry *entry;
3667 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3668 bfd *dynobj = hash_table->dynobj;
3669 asection *sdyn;
3670 bfd_size_type size;
3671 const struct elf_backend_data *bed;
3672 bfd_byte *extdyn;
3673
3674 _bfd_elf_strtab_finalize (dynstr);
3675 size = _bfd_elf_strtab_size (dynstr);
3676
3677 bed = get_elf_backend_data (dynobj);
3d4d4302 3678 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3679 BFD_ASSERT (sdyn != NULL);
3680
3681 /* Update all .dynamic entries referencing .dynstr strings. */
3682 for (extdyn = sdyn->contents;
eea6121a 3683 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3684 extdyn += bed->s->sizeof_dyn)
3685 {
3686 Elf_Internal_Dyn dyn;
3687
3688 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3689 switch (dyn.d_tag)
3690 {
3691 case DT_STRSZ:
3692 dyn.d_un.d_val = size;
3693 break;
3694 case DT_NEEDED:
3695 case DT_SONAME:
3696 case DT_RPATH:
3697 case DT_RUNPATH:
3698 case DT_FILTER:
3699 case DT_AUXILIARY:
7ee314fa
AM
3700 case DT_AUDIT:
3701 case DT_DEPAUDIT:
5a580b3a
AM
3702 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3703 break;
3704 default:
3705 continue;
3706 }
3707 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3708 }
3709
3710 /* Now update local dynamic symbols. */
3711 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3712 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3713 entry->isym.st_name);
3714
3715 /* And the rest of dynamic symbols. */
3716 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3717
3718 /* Adjust version definitions. */
3719 if (elf_tdata (output_bfd)->cverdefs)
3720 {
3721 asection *s;
3722 bfd_byte *p;
ef53be89 3723 size_t i;
5a580b3a
AM
3724 Elf_Internal_Verdef def;
3725 Elf_Internal_Verdaux defaux;
3726
3d4d4302 3727 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3728 p = s->contents;
3729 do
3730 {
3731 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3732 &def);
3733 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3734 if (def.vd_aux != sizeof (Elf_External_Verdef))
3735 continue;
5a580b3a
AM
3736 for (i = 0; i < def.vd_cnt; ++i)
3737 {
3738 _bfd_elf_swap_verdaux_in (output_bfd,
3739 (Elf_External_Verdaux *) p, &defaux);
3740 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3741 defaux.vda_name);
3742 _bfd_elf_swap_verdaux_out (output_bfd,
3743 &defaux, (Elf_External_Verdaux *) p);
3744 p += sizeof (Elf_External_Verdaux);
3745 }
3746 }
3747 while (def.vd_next);
3748 }
3749
3750 /* Adjust version references. */
3751 if (elf_tdata (output_bfd)->verref)
3752 {
3753 asection *s;
3754 bfd_byte *p;
ef53be89 3755 size_t i;
5a580b3a
AM
3756 Elf_Internal_Verneed need;
3757 Elf_Internal_Vernaux needaux;
3758
3d4d4302 3759 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3760 p = s->contents;
3761 do
3762 {
3763 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3764 &need);
3765 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3766 _bfd_elf_swap_verneed_out (output_bfd, &need,
3767 (Elf_External_Verneed *) p);
3768 p += sizeof (Elf_External_Verneed);
3769 for (i = 0; i < need.vn_cnt; ++i)
3770 {
3771 _bfd_elf_swap_vernaux_in (output_bfd,
3772 (Elf_External_Vernaux *) p, &needaux);
3773 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3774 needaux.vna_name);
3775 _bfd_elf_swap_vernaux_out (output_bfd,
3776 &needaux,
3777 (Elf_External_Vernaux *) p);
3778 p += sizeof (Elf_External_Vernaux);
3779 }
3780 }
3781 while (need.vn_next);
3782 }
3783
3784 return TRUE;
3785}
3786\f
13285a1b
AM
3787/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3788 The default is to only match when the INPUT and OUTPUT are exactly
3789 the same target. */
3790
3791bfd_boolean
3792_bfd_elf_default_relocs_compatible (const bfd_target *input,
3793 const bfd_target *output)
3794{
3795 return input == output;
3796}
3797
3798/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3799 This version is used when different targets for the same architecture
3800 are virtually identical. */
3801
3802bfd_boolean
3803_bfd_elf_relocs_compatible (const bfd_target *input,
3804 const bfd_target *output)
3805{
3806 const struct elf_backend_data *obed, *ibed;
3807
3808 if (input == output)
3809 return TRUE;
3810
3811 ibed = xvec_get_elf_backend_data (input);
3812 obed = xvec_get_elf_backend_data (output);
3813
3814 if (ibed->arch != obed->arch)
3815 return FALSE;
3816
3817 /* If both backends are using this function, deem them compatible. */
3818 return ibed->relocs_compatible == obed->relocs_compatible;
3819}
3820
e5034e59
AM
3821/* Make a special call to the linker "notice" function to tell it that
3822 we are about to handle an as-needed lib, or have finished
1b786873 3823 processing the lib. */
e5034e59
AM
3824
3825bfd_boolean
3826_bfd_elf_notice_as_needed (bfd *ibfd,
3827 struct bfd_link_info *info,
3828 enum notice_asneeded_action act)
3829{
46135103 3830 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3831}
3832
d9689752
L
3833/* Check relocations an ELF object file. */
3834
3835bfd_boolean
3836_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3837{
3838 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3839 struct elf_link_hash_table *htab = elf_hash_table (info);
3840
3841 /* If this object is the same format as the output object, and it is
3842 not a shared library, then let the backend look through the
3843 relocs.
3844
3845 This is required to build global offset table entries and to
3846 arrange for dynamic relocs. It is not required for the
3847 particular common case of linking non PIC code, even when linking
3848 against shared libraries, but unfortunately there is no way of
3849 knowing whether an object file has been compiled PIC or not.
3850 Looking through the relocs is not particularly time consuming.
3851 The problem is that we must either (1) keep the relocs in memory,
3852 which causes the linker to require additional runtime memory or
3853 (2) read the relocs twice from the input file, which wastes time.
3854 This would be a good case for using mmap.
3855
3856 I have no idea how to handle linking PIC code into a file of a
3857 different format. It probably can't be done. */
3858 if ((abfd->flags & DYNAMIC) == 0
3859 && is_elf_hash_table (htab)
3860 && bed->check_relocs != NULL
3861 && elf_object_id (abfd) == elf_hash_table_id (htab)
3862 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3863 {
3864 asection *o;
3865
3866 for (o = abfd->sections; o != NULL; o = o->next)
3867 {
3868 Elf_Internal_Rela *internal_relocs;
3869 bfd_boolean ok;
3870
5ce03cea 3871 /* Don't check relocations in excluded sections. */
d9689752 3872 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3873 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3874 || o->reloc_count == 0
3875 || ((info->strip == strip_all || info->strip == strip_debugger)
3876 && (o->flags & SEC_DEBUGGING) != 0)
3877 || bfd_is_abs_section (o->output_section))
3878 continue;
3879
3880 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3881 info->keep_memory);
3882 if (internal_relocs == NULL)
3883 return FALSE;
3884
3885 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3886
3887 if (elf_section_data (o)->relocs != internal_relocs)
3888 free (internal_relocs);
3889
3890 if (! ok)
3891 return FALSE;
3892 }
3893 }
3894
3895 return TRUE;
3896}
3897
4ad4eba5
AM
3898/* Add symbols from an ELF object file to the linker hash table. */
3899
3900static bfd_boolean
3901elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3902{
a0c402a5 3903 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3904 Elf_Internal_Shdr *hdr;
ef53be89
AM
3905 size_t symcount;
3906 size_t extsymcount;
3907 size_t extsymoff;
4ad4eba5
AM
3908 struct elf_link_hash_entry **sym_hash;
3909 bfd_boolean dynamic;
3910 Elf_External_Versym *extversym = NULL;
be22c732 3911 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
3912 Elf_External_Versym *ever;
3913 struct elf_link_hash_entry *weaks;
3914 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3915 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3916 Elf_Internal_Sym *isymbuf = NULL;
3917 Elf_Internal_Sym *isym;
3918 Elf_Internal_Sym *isymend;
3919 const struct elf_backend_data *bed;
3920 bfd_boolean add_needed;
66eb6687 3921 struct elf_link_hash_table *htab;
4ad4eba5 3922 bfd_size_type amt;
66eb6687 3923 void *alloc_mark = NULL;
4f87808c
AM
3924 struct bfd_hash_entry **old_table = NULL;
3925 unsigned int old_size = 0;
3926 unsigned int old_count = 0;
66eb6687 3927 void *old_tab = NULL;
66eb6687
AM
3928 void *old_ent;
3929 struct bfd_link_hash_entry *old_undefs = NULL;
3930 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3931 void *old_strtab = NULL;
66eb6687 3932 size_t tabsize = 0;
db6a5d5f 3933 asection *s;
29a9f53e 3934 bfd_boolean just_syms;
4ad4eba5 3935
66eb6687 3936 htab = elf_hash_table (info);
4ad4eba5 3937 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3938
3939 if ((abfd->flags & DYNAMIC) == 0)
3940 dynamic = FALSE;
3941 else
3942 {
3943 dynamic = TRUE;
3944
3945 /* You can't use -r against a dynamic object. Also, there's no
3946 hope of using a dynamic object which does not exactly match
3947 the format of the output file. */
0e1862bb 3948 if (bfd_link_relocatable (info)
66eb6687 3949 || !is_elf_hash_table (htab)
f13a99db 3950 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3951 {
0e1862bb 3952 if (bfd_link_relocatable (info))
9a0789ec
NC
3953 bfd_set_error (bfd_error_invalid_operation);
3954 else
3955 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3956 goto error_return;
3957 }
3958 }
3959
a0c402a5
L
3960 ehdr = elf_elfheader (abfd);
3961 if (info->warn_alternate_em
3962 && bed->elf_machine_code != ehdr->e_machine
3963 && ((bed->elf_machine_alt1 != 0
3964 && ehdr->e_machine == bed->elf_machine_alt1)
3965 || (bed->elf_machine_alt2 != 0
3966 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3967 _bfd_error_handler
695344c0 3968 /* xgettext:c-format */
9793eb77 3969 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3970 ehdr->e_machine, abfd, bed->elf_machine_code);
3971
4ad4eba5
AM
3972 /* As a GNU extension, any input sections which are named
3973 .gnu.warning.SYMBOL are treated as warning symbols for the given
3974 symbol. This differs from .gnu.warning sections, which generate
3975 warnings when they are included in an output file. */
dd98f8d2 3976 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3977 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3978 {
db6a5d5f 3979 const char *name;
4ad4eba5 3980
fd361982 3981 name = bfd_section_name (s);
db6a5d5f 3982 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3983 {
db6a5d5f
AM
3984 char *msg;
3985 bfd_size_type sz;
3986
3987 name += sizeof ".gnu.warning." - 1;
3988
3989 /* If this is a shared object, then look up the symbol
3990 in the hash table. If it is there, and it is already
3991 been defined, then we will not be using the entry
3992 from this shared object, so we don't need to warn.
3993 FIXME: If we see the definition in a regular object
3994 later on, we will warn, but we shouldn't. The only
3995 fix is to keep track of what warnings we are supposed
3996 to emit, and then handle them all at the end of the
3997 link. */
3998 if (dynamic)
4ad4eba5 3999 {
db6a5d5f
AM
4000 struct elf_link_hash_entry *h;
4001
4002 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4003
4004 /* FIXME: What about bfd_link_hash_common? */
4005 if (h != NULL
4006 && (h->root.type == bfd_link_hash_defined
4007 || h->root.type == bfd_link_hash_defweak))
4008 continue;
4009 }
4ad4eba5 4010
db6a5d5f
AM
4011 sz = s->size;
4012 msg = (char *) bfd_alloc (abfd, sz + 1);
4013 if (msg == NULL)
4014 goto error_return;
4ad4eba5 4015
db6a5d5f
AM
4016 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4017 goto error_return;
4ad4eba5 4018
db6a5d5f 4019 msg[sz] = '\0';
4ad4eba5 4020
db6a5d5f
AM
4021 if (! (_bfd_generic_link_add_one_symbol
4022 (info, abfd, name, BSF_WARNING, s, 0, msg,
4023 FALSE, bed->collect, NULL)))
4024 goto error_return;
4ad4eba5 4025
0e1862bb 4026 if (bfd_link_executable (info))
db6a5d5f
AM
4027 {
4028 /* Clobber the section size so that the warning does
4029 not get copied into the output file. */
4030 s->size = 0;
11d2f718 4031
db6a5d5f
AM
4032 /* Also set SEC_EXCLUDE, so that symbols defined in
4033 the warning section don't get copied to the output. */
4034 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4035 }
4036 }
4037 }
4038
29a9f53e
L
4039 just_syms = ((s = abfd->sections) != NULL
4040 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4041
4ad4eba5
AM
4042 add_needed = TRUE;
4043 if (! dynamic)
4044 {
4045 /* If we are creating a shared library, create all the dynamic
4046 sections immediately. We need to attach them to something,
4047 so we attach them to this BFD, provided it is the right
bf89386a
L
4048 format and is not from ld --just-symbols. Always create the
4049 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4050 are no input BFD's of the same format as the output, we can't
4051 make a shared library. */
4052 if (!just_syms
bf89386a 4053 && (bfd_link_pic (info)
9c1d7a08 4054 || (!bfd_link_relocatable (info)
3c5fce9b 4055 && info->nointerp
9c1d7a08 4056 && (info->export_dynamic || info->dynamic)))
66eb6687 4057 && is_elf_hash_table (htab)
f13a99db 4058 && info->output_bfd->xvec == abfd->xvec
66eb6687 4059 && !htab->dynamic_sections_created)
4ad4eba5
AM
4060 {
4061 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4062 goto error_return;
4063 }
4064 }
66eb6687 4065 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4066 goto error_return;
4067 else
4068 {
4ad4eba5 4069 const char *soname = NULL;
7ee314fa 4070 char *audit = NULL;
4ad4eba5 4071 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4072 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4073 int ret;
4074
4075 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4076 ld shouldn't allow it. */
29a9f53e 4077 if (just_syms)
92fd189d 4078 abort ();
4ad4eba5
AM
4079
4080 /* If this dynamic lib was specified on the command line with
4081 --as-needed in effect, then we don't want to add a DT_NEEDED
4082 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4083 in by another lib's DT_NEEDED. When --no-add-needed is used
4084 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4085 any dynamic library in DT_NEEDED tags in the dynamic lib at
4086 all. */
4087 add_needed = (elf_dyn_lib_class (abfd)
4088 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4089 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4090
4091 s = bfd_get_section_by_name (abfd, ".dynamic");
4092 if (s != NULL)
4093 {
4094 bfd_byte *dynbuf;
4095 bfd_byte *extdyn;
cb33740c 4096 unsigned int elfsec;
4ad4eba5
AM
4097 unsigned long shlink;
4098
eea6121a 4099 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4100 {
4101error_free_dyn:
4102 free (dynbuf);
4103 goto error_return;
4104 }
4ad4eba5
AM
4105
4106 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4107 if (elfsec == SHN_BAD)
4ad4eba5
AM
4108 goto error_free_dyn;
4109 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4110
4111 for (extdyn = dynbuf;
9bff840e 4112 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4113 extdyn += bed->s->sizeof_dyn)
4114 {
4115 Elf_Internal_Dyn dyn;
4116
4117 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4118 if (dyn.d_tag == DT_SONAME)
4119 {
4120 unsigned int tagv = dyn.d_un.d_val;
4121 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4122 if (soname == NULL)
4123 goto error_free_dyn;
4124 }
4125 if (dyn.d_tag == DT_NEEDED)
4126 {
4127 struct bfd_link_needed_list *n, **pn;
4128 char *fnm, *anm;
4129 unsigned int tagv = dyn.d_un.d_val;
4130
4131 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4132 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4133 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4134 if (n == NULL || fnm == NULL)
4135 goto error_free_dyn;
4136 amt = strlen (fnm) + 1;
a50b1753 4137 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4138 if (anm == NULL)
4139 goto error_free_dyn;
4140 memcpy (anm, fnm, amt);
4141 n->name = anm;
4142 n->by = abfd;
4143 n->next = NULL;
66eb6687 4144 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4145 ;
4146 *pn = n;
4147 }
4148 if (dyn.d_tag == DT_RUNPATH)
4149 {
4150 struct bfd_link_needed_list *n, **pn;
4151 char *fnm, *anm;
4152 unsigned int tagv = dyn.d_un.d_val;
4153
4154 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4155 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4156 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4157 if (n == NULL || fnm == NULL)
4158 goto error_free_dyn;
4159 amt = strlen (fnm) + 1;
a50b1753 4160 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4161 if (anm == NULL)
4162 goto error_free_dyn;
4163 memcpy (anm, fnm, amt);
4164 n->name = anm;
4165 n->by = abfd;
4166 n->next = NULL;
4167 for (pn = & runpath;
4168 *pn != NULL;
4169 pn = &(*pn)->next)
4170 ;
4171 *pn = n;
4172 }
4173 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4174 if (!runpath && dyn.d_tag == DT_RPATH)
4175 {
4176 struct bfd_link_needed_list *n, **pn;
4177 char *fnm, *anm;
4178 unsigned int tagv = dyn.d_un.d_val;
4179
4180 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4181 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4182 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4183 if (n == NULL || fnm == NULL)
4184 goto error_free_dyn;
4185 amt = strlen (fnm) + 1;
a50b1753 4186 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4187 if (anm == NULL)
f8703194 4188 goto error_free_dyn;
4ad4eba5
AM
4189 memcpy (anm, fnm, amt);
4190 n->name = anm;
4191 n->by = abfd;
4192 n->next = NULL;
4193 for (pn = & rpath;
4194 *pn != NULL;
4195 pn = &(*pn)->next)
4196 ;
4197 *pn = n;
4198 }
7ee314fa
AM
4199 if (dyn.d_tag == DT_AUDIT)
4200 {
4201 unsigned int tagv = dyn.d_un.d_val;
4202 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4203 }
4ad4eba5
AM
4204 }
4205
4206 free (dynbuf);
4207 }
4208
4209 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4210 frees all more recently bfd_alloc'd blocks as well. */
4211 if (runpath)
4212 rpath = runpath;
4213
4214 if (rpath)
4215 {
4216 struct bfd_link_needed_list **pn;
66eb6687 4217 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4218 ;
4219 *pn = rpath;
4220 }
4221
9acc85a6
AM
4222 /* If we have a PT_GNU_RELRO program header, mark as read-only
4223 all sections contained fully therein. This makes relro
4224 shared library sections appear as they will at run-time. */
4225 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4226 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4227 if (phdr->p_type == PT_GNU_RELRO)
4228 {
4229 for (s = abfd->sections; s != NULL; s = s->next)
4230 if ((s->flags & SEC_ALLOC) != 0
4231 && s->vma >= phdr->p_vaddr
4232 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4233 s->flags |= SEC_READONLY;
4234 break;
4235 }
4236
4ad4eba5
AM
4237 /* We do not want to include any of the sections in a dynamic
4238 object in the output file. We hack by simply clobbering the
4239 list of sections in the BFD. This could be handled more
4240 cleanly by, say, a new section flag; the existing
4241 SEC_NEVER_LOAD flag is not the one we want, because that one
4242 still implies that the section takes up space in the output
4243 file. */
4244 bfd_section_list_clear (abfd);
4245
4ad4eba5
AM
4246 /* Find the name to use in a DT_NEEDED entry that refers to this
4247 object. If the object has a DT_SONAME entry, we use it.
4248 Otherwise, if the generic linker stuck something in
4249 elf_dt_name, we use that. Otherwise, we just use the file
4250 name. */
4251 if (soname == NULL || *soname == '\0')
4252 {
4253 soname = elf_dt_name (abfd);
4254 if (soname == NULL || *soname == '\0')
4255 soname = bfd_get_filename (abfd);
4256 }
4257
4258 /* Save the SONAME because sometimes the linker emulation code
4259 will need to know it. */
4260 elf_dt_name (abfd) = soname;
4261
7e9f0867 4262 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4263 if (ret < 0)
4264 goto error_return;
4265
4266 /* If we have already included this dynamic object in the
4267 link, just ignore it. There is no reason to include a
4268 particular dynamic object more than once. */
4269 if (ret > 0)
4270 return TRUE;
7ee314fa
AM
4271
4272 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4273 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4274 }
4275
4276 /* If this is a dynamic object, we always link against the .dynsym
4277 symbol table, not the .symtab symbol table. The dynamic linker
4278 will only see the .dynsym symbol table, so there is no reason to
4279 look at .symtab for a dynamic object. */
4280
4281 if (! dynamic || elf_dynsymtab (abfd) == 0)
4282 hdr = &elf_tdata (abfd)->symtab_hdr;
4283 else
4284 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4285
4286 symcount = hdr->sh_size / bed->s->sizeof_sym;
4287
4288 /* The sh_info field of the symtab header tells us where the
4289 external symbols start. We don't care about the local symbols at
4290 this point. */
4291 if (elf_bad_symtab (abfd))
4292 {
4293 extsymcount = symcount;
4294 extsymoff = 0;
4295 }
4296 else
4297 {
4298 extsymcount = symcount - hdr->sh_info;
4299 extsymoff = hdr->sh_info;
4300 }
4301
f45794cb 4302 sym_hash = elf_sym_hashes (abfd);
012b2306 4303 if (extsymcount != 0)
4ad4eba5
AM
4304 {
4305 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4306 NULL, NULL, NULL);
4307 if (isymbuf == NULL)
4308 goto error_return;
4309
4ad4eba5 4310 if (sym_hash == NULL)
012b2306
AM
4311 {
4312 /* We store a pointer to the hash table entry for each
4313 external symbol. */
ef53be89
AM
4314 amt = extsymcount;
4315 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4316 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4317 if (sym_hash == NULL)
4318 goto error_free_sym;
4319 elf_sym_hashes (abfd) = sym_hash;
4320 }
4ad4eba5
AM
4321 }
4322
4323 if (dynamic)
4324 {
4325 /* Read in any version definitions. */
fc0e6df6
PB
4326 if (!_bfd_elf_slurp_version_tables (abfd,
4327 info->default_imported_symver))
4ad4eba5
AM
4328 goto error_free_sym;
4329
4330 /* Read in the symbol versions, but don't bother to convert them
4331 to internal format. */
4332 if (elf_dynversym (abfd) != 0)
4333 {
4334 Elf_Internal_Shdr *versymhdr;
4335
4336 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
be22c732
NC
4337 amt = versymhdr->sh_size;
4338 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4ad4eba5
AM
4339 if (extversym == NULL)
4340 goto error_free_sym;
4ad4eba5
AM
4341 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4342 || bfd_bread (extversym, amt, abfd) != amt)
4343 goto error_free_vers;
be22c732 4344 extversym_end = extversym + (amt / sizeof (* extversym));
4ad4eba5
AM
4345 }
4346 }
4347
66eb6687
AM
4348 /* If we are loading an as-needed shared lib, save the symbol table
4349 state before we start adding symbols. If the lib turns out
4350 to be unneeded, restore the state. */
4351 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4352 {
4353 unsigned int i;
4354 size_t entsize;
4355
4356 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4357 {
4358 struct bfd_hash_entry *p;
2de92251 4359 struct elf_link_hash_entry *h;
66eb6687
AM
4360
4361 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4362 {
4363 h = (struct elf_link_hash_entry *) p;
4364 entsize += htab->root.table.entsize;
4365 if (h->root.type == bfd_link_hash_warning)
4366 entsize += htab->root.table.entsize;
4367 }
66eb6687
AM
4368 }
4369
4370 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4371 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4372 if (old_tab == NULL)
4373 goto error_free_vers;
4374
4375 /* Remember the current objalloc pointer, so that all mem for
4376 symbols added can later be reclaimed. */
4377 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4378 if (alloc_mark == NULL)
4379 goto error_free_vers;
4380
5061a885
AM
4381 /* Make a special call to the linker "notice" function to
4382 tell it that we are about to handle an as-needed lib. */
e5034e59 4383 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4384 goto error_free_vers;
5061a885 4385
f45794cb
AM
4386 /* Clone the symbol table. Remember some pointers into the
4387 symbol table, and dynamic symbol count. */
4388 old_ent = (char *) old_tab + tabsize;
66eb6687 4389 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4390 old_undefs = htab->root.undefs;
4391 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4392 old_table = htab->root.table.table;
4393 old_size = htab->root.table.size;
4394 old_count = htab->root.table.count;
5b677558
AM
4395 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4396 if (old_strtab == NULL)
4397 goto error_free_vers;
66eb6687
AM
4398
4399 for (i = 0; i < htab->root.table.size; i++)
4400 {
4401 struct bfd_hash_entry *p;
2de92251 4402 struct elf_link_hash_entry *h;
66eb6687
AM
4403
4404 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4405 {
4406 memcpy (old_ent, p, htab->root.table.entsize);
4407 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4408 h = (struct elf_link_hash_entry *) p;
4409 if (h->root.type == bfd_link_hash_warning)
4410 {
4411 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4412 old_ent = (char *) old_ent + htab->root.table.entsize;
4413 }
66eb6687
AM
4414 }
4415 }
4416 }
4ad4eba5 4417
66eb6687 4418 weaks = NULL;
be22c732
NC
4419 if (extversym == NULL)
4420 ever = NULL;
4421 else if (extversym + extsymoff < extversym_end)
4422 ever = extversym + extsymoff;
4423 else
4424 {
4425 /* xgettext:c-format */
4426 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4427 abfd, (long) extsymoff,
4428 (long) (extversym_end - extversym) / sizeof (* extversym));
4429 bfd_set_error (bfd_error_bad_value);
4430 goto error_free_vers;
4431 }
4432
b4c555cf
ML
4433 if (!bfd_link_relocatable (info)
4434 && abfd->lto_slim_object)
cc5277b1
ML
4435 {
4436 _bfd_error_handler
4437 (_("%pB: plugin needed to handle lto object"), abfd);
4438 }
4439
4ad4eba5
AM
4440 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4441 isym < isymend;
4442 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4443 {
4444 int bind;
4445 bfd_vma value;
af44c138 4446 asection *sec, *new_sec;
4ad4eba5
AM
4447 flagword flags;
4448 const char *name;
4449 struct elf_link_hash_entry *h;
90c984fc 4450 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4451 bfd_boolean definition;
4452 bfd_boolean size_change_ok;
4453 bfd_boolean type_change_ok;
37a9e49a
L
4454 bfd_boolean new_weak;
4455 bfd_boolean old_weak;
4ad4eba5 4456 bfd_boolean override;
a4d8e49b 4457 bfd_boolean common;
97196564 4458 bfd_boolean discarded;
4ad4eba5 4459 unsigned int old_alignment;
4538d1c7 4460 unsigned int shindex;
4ad4eba5 4461 bfd *old_bfd;
6e33951e 4462 bfd_boolean matched;
4ad4eba5
AM
4463
4464 override = FALSE;
4465
4466 flags = BSF_NO_FLAGS;
4467 sec = NULL;
4468 value = isym->st_value;
a4d8e49b 4469 common = bed->common_definition (isym);
2980ccad
L
4470 if (common && info->inhibit_common_definition)
4471 {
4472 /* Treat common symbol as undefined for --no-define-common. */
4473 isym->st_shndx = SHN_UNDEF;
4474 common = FALSE;
4475 }
97196564 4476 discarded = FALSE;
4ad4eba5
AM
4477
4478 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4479 switch (bind)
4ad4eba5 4480 {
3e7a7d11 4481 case STB_LOCAL:
4ad4eba5
AM
4482 /* This should be impossible, since ELF requires that all
4483 global symbols follow all local symbols, and that sh_info
4484 point to the first global symbol. Unfortunately, Irix 5
4485 screws this up. */
fe3fef62
AM
4486 if (elf_bad_symtab (abfd))
4487 continue;
4488
4489 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4490 then we'll likely segfault on a NULL symbol hash if the
4491 symbol is ever referenced in relocations. */
4492 shindex = elf_elfheader (abfd)->e_shstrndx;
4493 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4494 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4495 " (>= sh_info of %lu)"),
4496 abfd, name, (long) (isym - isymbuf + extsymoff),
4497 (long) extsymoff);
4498
4499 /* Dynamic object relocations are not processed by ld, so
4500 ld won't run into the problem mentioned above. */
4501 if (dynamic)
4502 continue;
fe3fef62
AM
4503 bfd_set_error (bfd_error_bad_value);
4504 goto error_free_vers;
3e7a7d11
NC
4505
4506 case STB_GLOBAL:
a4d8e49b 4507 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4508 flags = BSF_GLOBAL;
3e7a7d11
NC
4509 break;
4510
4511 case STB_WEAK:
4512 flags = BSF_WEAK;
4513 break;
4514
4515 case STB_GNU_UNIQUE:
4516 flags = BSF_GNU_UNIQUE;
4517 break;
4518
4519 default:
4ad4eba5 4520 /* Leave it up to the processor backend. */
3e7a7d11 4521 break;
4ad4eba5
AM
4522 }
4523
4524 if (isym->st_shndx == SHN_UNDEF)
4525 sec = bfd_und_section_ptr;
cb33740c
AM
4526 else if (isym->st_shndx == SHN_ABS)
4527 sec = bfd_abs_section_ptr;
4528 else if (isym->st_shndx == SHN_COMMON)
4529 {
4530 sec = bfd_com_section_ptr;
4531 /* What ELF calls the size we call the value. What ELF
4532 calls the value we call the alignment. */
4533 value = isym->st_size;
4534 }
4535 else
4ad4eba5
AM
4536 {
4537 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4538 if (sec == NULL)
4539 sec = bfd_abs_section_ptr;
dbaa2011 4540 else if (discarded_section (sec))
529fcb95 4541 {
e5d08002
L
4542 /* Symbols from discarded section are undefined. We keep
4543 its visibility. */
529fcb95 4544 sec = bfd_und_section_ptr;
97196564 4545 discarded = TRUE;
529fcb95
PB
4546 isym->st_shndx = SHN_UNDEF;
4547 }
4ad4eba5
AM
4548 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4549 value -= sec->vma;
4550 }
4ad4eba5
AM
4551
4552 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4553 isym->st_name);
4554 if (name == NULL)
4555 goto error_free_vers;
4556
4557 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4558 && (abfd->flags & BFD_PLUGIN) != 0)
4559 {
4560 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4561
4562 if (xc == NULL)
4563 {
4564 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4565 | SEC_EXCLUDE);
4566 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4567 if (xc == NULL)
4568 goto error_free_vers;
4569 }
4570 sec = xc;
4571 }
4572 else if (isym->st_shndx == SHN_COMMON
4573 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4574 && !bfd_link_relocatable (info))
4ad4eba5
AM
4575 {
4576 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4577
4578 if (tcomm == NULL)
4579 {
02d00247
AM
4580 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4581 | SEC_LINKER_CREATED);
4582 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4583 if (tcomm == NULL)
4ad4eba5
AM
4584 goto error_free_vers;
4585 }
4586 sec = tcomm;
4587 }
66eb6687 4588 else if (bed->elf_add_symbol_hook)
4ad4eba5 4589 {
66eb6687
AM
4590 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4591 &sec, &value))
4ad4eba5
AM
4592 goto error_free_vers;
4593
4594 /* The hook function sets the name to NULL if this symbol
4595 should be skipped for some reason. */
4596 if (name == NULL)
4597 continue;
4598 }
4599
4600 /* Sanity check that all possibilities were handled. */
4601 if (sec == NULL)
4538d1c7 4602 abort ();
4ad4eba5 4603
191c0c42
AM
4604 /* Silently discard TLS symbols from --just-syms. There's
4605 no way to combine a static TLS block with a new TLS block
4606 for this executable. */
4607 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4608 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4609 continue;
4610
4ad4eba5
AM
4611 if (bfd_is_und_section (sec)
4612 || bfd_is_com_section (sec))
4613 definition = FALSE;
4614 else
4615 definition = TRUE;
4616
4617 size_change_ok = FALSE;
66eb6687 4618 type_change_ok = bed->type_change_ok;
37a9e49a 4619 old_weak = FALSE;
6e33951e 4620 matched = FALSE;
4ad4eba5
AM
4621 old_alignment = 0;
4622 old_bfd = NULL;
af44c138 4623 new_sec = sec;
4ad4eba5 4624
66eb6687 4625 if (is_elf_hash_table (htab))
4ad4eba5
AM
4626 {
4627 Elf_Internal_Versym iver;
4628 unsigned int vernum = 0;
4629 bfd_boolean skip;
4630
fc0e6df6 4631 if (ever == NULL)
4ad4eba5 4632 {
fc0e6df6
PB
4633 if (info->default_imported_symver)
4634 /* Use the default symbol version created earlier. */
4635 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4636 else
4637 iver.vs_vers = 0;
4638 }
be22c732
NC
4639 else if (ever >= extversym_end)
4640 {
4641 /* xgettext:c-format */
4642 _bfd_error_handler (_("%pB: not enough version information"),
4643 abfd);
4644 bfd_set_error (bfd_error_bad_value);
4645 goto error_free_vers;
4646 }
fc0e6df6
PB
4647 else
4648 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4649
4650 vernum = iver.vs_vers & VERSYM_VERSION;
4651
4652 /* If this is a hidden symbol, or if it is not version
4653 1, we append the version name to the symbol name.
cc86ff91
EB
4654 However, we do not modify a non-hidden absolute symbol
4655 if it is not a function, because it might be the version
4656 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4657 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4658 || (vernum > 1
4659 && (!bfd_is_abs_section (sec)
4660 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4661 {
4662 const char *verstr;
4663 size_t namelen, verlen, newlen;
4664 char *newname, *p;
4665
4666 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4667 {
fc0e6df6
PB
4668 if (vernum > elf_tdata (abfd)->cverdefs)
4669 verstr = NULL;
4670 else if (vernum > 1)
4671 verstr =
4672 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4673 else
4674 verstr = "";
4ad4eba5 4675
fc0e6df6 4676 if (verstr == NULL)
4ad4eba5 4677 {
4eca0228 4678 _bfd_error_handler
695344c0 4679 /* xgettext:c-format */
871b3ab2 4680 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4681 abfd, name, vernum,
4682 elf_tdata (abfd)->cverdefs);
4683 bfd_set_error (bfd_error_bad_value);
4684 goto error_free_vers;
4ad4eba5 4685 }
fc0e6df6
PB
4686 }
4687 else
4688 {
4689 /* We cannot simply test for the number of
4690 entries in the VERNEED section since the
4691 numbers for the needed versions do not start
4692 at 0. */
4693 Elf_Internal_Verneed *t;
4694
4695 verstr = NULL;
4696 for (t = elf_tdata (abfd)->verref;
4697 t != NULL;
4698 t = t->vn_nextref)
4ad4eba5 4699 {
fc0e6df6 4700 Elf_Internal_Vernaux *a;
4ad4eba5 4701
fc0e6df6
PB
4702 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4703 {
4704 if (a->vna_other == vernum)
4ad4eba5 4705 {
fc0e6df6
PB
4706 verstr = a->vna_nodename;
4707 break;
4ad4eba5 4708 }
4ad4eba5 4709 }
fc0e6df6
PB
4710 if (a != NULL)
4711 break;
4712 }
4713 if (verstr == NULL)
4714 {
4eca0228 4715 _bfd_error_handler
695344c0 4716 /* xgettext:c-format */
871b3ab2 4717 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4718 abfd, name, vernum);
4719 bfd_set_error (bfd_error_bad_value);
4720 goto error_free_vers;
4ad4eba5 4721 }
4ad4eba5 4722 }
fc0e6df6
PB
4723
4724 namelen = strlen (name);
4725 verlen = strlen (verstr);
4726 newlen = namelen + verlen + 2;
4727 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4728 && isym->st_shndx != SHN_UNDEF)
4729 ++newlen;
4730
a50b1753 4731 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4732 if (newname == NULL)
4733 goto error_free_vers;
4734 memcpy (newname, name, namelen);
4735 p = newname + namelen;
4736 *p++ = ELF_VER_CHR;
4737 /* If this is a defined non-hidden version symbol,
4738 we add another @ to the name. This indicates the
4739 default version of the symbol. */
4740 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4741 && isym->st_shndx != SHN_UNDEF)
4742 *p++ = ELF_VER_CHR;
4743 memcpy (p, verstr, verlen + 1);
4744
4745 name = newname;
4ad4eba5
AM
4746 }
4747
cd3416da
AM
4748 /* If this symbol has default visibility and the user has
4749 requested we not re-export it, then mark it as hidden. */
a0d49154 4750 if (!bfd_is_und_section (sec)
cd3416da 4751 && !dynamic
ce875075 4752 && abfd->no_export
cd3416da
AM
4753 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4754 isym->st_other = (STV_HIDDEN
4755 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4756
4f3fedcf
AM
4757 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4758 sym_hash, &old_bfd, &old_weak,
4759 &old_alignment, &skip, &override,
6e33951e
L
4760 &type_change_ok, &size_change_ok,
4761 &matched))
4ad4eba5
AM
4762 goto error_free_vers;
4763
4764 if (skip)
4765 continue;
4766
6e33951e
L
4767 /* Override a definition only if the new symbol matches the
4768 existing one. */
4769 if (override && matched)
4ad4eba5
AM
4770 definition = FALSE;
4771
4772 h = *sym_hash;
4773 while (h->root.type == bfd_link_hash_indirect
4774 || h->root.type == bfd_link_hash_warning)
4775 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4776
4ad4eba5 4777 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4778 && vernum > 1
4779 && definition)
4780 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4781 }
4782
4783 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4784 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4785 (struct bfd_link_hash_entry **) sym_hash)))
4786 goto error_free_vers;
4787
4788 h = *sym_hash;
90c984fc
L
4789 /* We need to make sure that indirect symbol dynamic flags are
4790 updated. */
4791 hi = h;
4ad4eba5
AM
4792 while (h->root.type == bfd_link_hash_indirect
4793 || h->root.type == bfd_link_hash_warning)
4794 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4795
97196564
L
4796 /* Setting the index to -3 tells elf_link_output_extsym that
4797 this symbol is defined in a discarded section. */
4798 if (discarded)
4799 h->indx = -3;
4800
4ad4eba5
AM
4801 *sym_hash = h;
4802
37a9e49a 4803 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4804 if (dynamic
4805 && definition
37a9e49a 4806 && new_weak
fcb93ecf 4807 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4808 && is_elf_hash_table (htab)
60d67dc8 4809 && h->u.alias == NULL)
4ad4eba5
AM
4810 {
4811 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4812 a dynamic object, using the alias field. Later in this
4813 function we will set the alias field to the correct
4ad4eba5
AM
4814 value. We only put non-function symbols from dynamic
4815 objects on this list, because that happens to be the only
4816 time we need to know the normal symbol corresponding to a
4817 weak symbol, and the information is time consuming to
60d67dc8 4818 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4819 then this symbol was already defined by some previous
4820 dynamic object, and we will be using that previous
4821 definition anyhow. */
4822
60d67dc8 4823 h->u.alias = weaks;
4ad4eba5 4824 weaks = h;
4ad4eba5
AM
4825 }
4826
4827 /* Set the alignment of a common symbol. */
a4d8e49b 4828 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4829 && h->root.type == bfd_link_hash_common)
4830 {
4831 unsigned int align;
4832
a4d8e49b 4833 if (common)
af44c138
L
4834 align = bfd_log2 (isym->st_value);
4835 else
4836 {
4837 /* The new symbol is a common symbol in a shared object.
4838 We need to get the alignment from the section. */
4839 align = new_sec->alignment_power;
4840 }
595213d4 4841 if (align > old_alignment)
4ad4eba5
AM
4842 h->root.u.c.p->alignment_power = align;
4843 else
4844 h->root.u.c.p->alignment_power = old_alignment;
4845 }
4846
66eb6687 4847 if (is_elf_hash_table (htab))
4ad4eba5 4848 {
4f3fedcf
AM
4849 /* Set a flag in the hash table entry indicating the type of
4850 reference or definition we just found. A dynamic symbol
4851 is one which is referenced or defined by both a regular
4852 object and a shared object. */
4853 bfd_boolean dynsym = FALSE;
4854
4855 /* Plugin symbols aren't normal. Don't set def_regular or
4856 ref_regular for them, or make them dynamic. */
4857 if ((abfd->flags & BFD_PLUGIN) != 0)
4858 ;
4859 else if (! dynamic)
4860 {
4861 if (! definition)
4862 {
4863 h->ref_regular = 1;
4864 if (bind != STB_WEAK)
4865 h->ref_regular_nonweak = 1;
4866 }
4867 else
4868 {
4869 h->def_regular = 1;
4870 if (h->def_dynamic)
4871 {
4872 h->def_dynamic = 0;
4873 h->ref_dynamic = 1;
4874 }
4875 }
4876
4877 /* If the indirect symbol has been forced local, don't
4878 make the real symbol dynamic. */
4879 if ((h == hi || !hi->forced_local)
0e1862bb 4880 && (bfd_link_dll (info)
4f3fedcf
AM
4881 || h->def_dynamic
4882 || h->ref_dynamic))
4883 dynsym = TRUE;
4884 }
4885 else
4886 {
4887 if (! definition)
4888 {
4889 h->ref_dynamic = 1;
4890 hi->ref_dynamic = 1;
4891 }
4892 else
4893 {
4894 h->def_dynamic = 1;
4895 hi->def_dynamic = 1;
4896 }
4897
4898 /* If the indirect symbol has been forced local, don't
4899 make the real symbol dynamic. */
4900 if ((h == hi || !hi->forced_local)
4901 && (h->def_regular
4902 || h->ref_regular
60d67dc8
AM
4903 || (h->is_weakalias
4904 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4905 dynsym = TRUE;
4906 }
4907
4908 /* Check to see if we need to add an indirect symbol for
4909 the default name. */
4910 if (definition
4911 || (!override && h->root.type == bfd_link_hash_common))
4912 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4913 sec, value, &old_bfd, &dynsym))
4914 goto error_free_vers;
4ad4eba5
AM
4915
4916 /* Check the alignment when a common symbol is involved. This
4917 can change when a common symbol is overridden by a normal
4918 definition or a common symbol is ignored due to the old
4919 normal definition. We need to make sure the maximum
4920 alignment is maintained. */
a4d8e49b 4921 if ((old_alignment || common)
4ad4eba5
AM
4922 && h->root.type != bfd_link_hash_common)
4923 {
4924 unsigned int common_align;
4925 unsigned int normal_align;
4926 unsigned int symbol_align;
4927 bfd *normal_bfd;
4928 bfd *common_bfd;
4929
3a81e825
AM
4930 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4931 || h->root.type == bfd_link_hash_defweak);
4932
4ad4eba5
AM
4933 symbol_align = ffs (h->root.u.def.value) - 1;
4934 if (h->root.u.def.section->owner != NULL
0616a280
AM
4935 && (h->root.u.def.section->owner->flags
4936 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4937 {
4938 normal_align = h->root.u.def.section->alignment_power;
4939 if (normal_align > symbol_align)
4940 normal_align = symbol_align;
4941 }
4942 else
4943 normal_align = symbol_align;
4944
4945 if (old_alignment)
4946 {
4947 common_align = old_alignment;
4948 common_bfd = old_bfd;
4949 normal_bfd = abfd;
4950 }
4951 else
4952 {
4953 common_align = bfd_log2 (isym->st_value);
4954 common_bfd = abfd;
4955 normal_bfd = old_bfd;
4956 }
4957
4958 if (normal_align < common_align)
d07676f8
NC
4959 {
4960 /* PR binutils/2735 */
4961 if (normal_bfd == NULL)
4eca0228 4962 _bfd_error_handler
695344c0 4963 /* xgettext:c-format */
9793eb77 4964 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4965 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4966 1 << common_align, name, common_bfd,
4967 1 << normal_align, h->root.u.def.section);
d07676f8 4968 else
4eca0228 4969 _bfd_error_handler
695344c0 4970 /* xgettext:c-format */
9793eb77 4971 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4972 " is smaller than %u in %pB"),
c08bb8dd
AM
4973 1 << normal_align, name, normal_bfd,
4974 1 << common_align, common_bfd);
d07676f8 4975 }
4ad4eba5
AM
4976 }
4977
83ad0046 4978 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4979 if (isym->st_size != 0
4980 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4981 && (definition || h->size == 0))
4982 {
83ad0046
L
4983 if (h->size != 0
4984 && h->size != isym->st_size
4985 && ! size_change_ok)
4eca0228 4986 _bfd_error_handler
695344c0 4987 /* xgettext:c-format */
9793eb77 4988 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4989 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4990 name, (uint64_t) h->size, old_bfd,
4991 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4992
4993 h->size = isym->st_size;
4994 }
4995
4996 /* If this is a common symbol, then we always want H->SIZE
4997 to be the size of the common symbol. The code just above
4998 won't fix the size if a common symbol becomes larger. We
4999 don't warn about a size change here, because that is
4f3fedcf 5000 covered by --warn-common. Allow changes between different
fcb93ecf 5001 function types. */
4ad4eba5
AM
5002 if (h->root.type == bfd_link_hash_common)
5003 h->size = h->root.u.c.size;
5004
5005 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5006 && ((definition && !new_weak)
5007 || (old_weak && h->root.type == bfd_link_hash_common)
5008 || h->type == STT_NOTYPE))
4ad4eba5 5009 {
2955ec4c
L
5010 unsigned int type = ELF_ST_TYPE (isym->st_info);
5011
5012 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5013 symbol. */
5014 if (type == STT_GNU_IFUNC
5015 && (abfd->flags & DYNAMIC) != 0)
5016 type = STT_FUNC;
4ad4eba5 5017
2955ec4c
L
5018 if (h->type != type)
5019 {
5020 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5021 /* xgettext:c-format */
4eca0228 5022 _bfd_error_handler
9793eb77 5023 (_("warning: type of symbol `%s' changed"
871b3ab2 5024 " from %d to %d in %pB"),
c08bb8dd 5025 name, h->type, type, abfd);
2955ec4c
L
5026
5027 h->type = type;
5028 }
4ad4eba5
AM
5029 }
5030
54ac0771 5031 /* Merge st_other field. */
b8417128 5032 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 5033
c3df8c14 5034 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5035 if (definition
5036 && (sec->flags & SEC_DEBUGGING)
5037 && !bfd_link_relocatable (info))
c3df8c14
AM
5038 dynsym = FALSE;
5039
4f3fedcf
AM
5040 /* Nor should we make plugin symbols dynamic. */
5041 if ((abfd->flags & BFD_PLUGIN) != 0)
5042 dynsym = FALSE;
5043
35fc36a8 5044 if (definition)
35399224
L
5045 {
5046 h->target_internal = isym->st_target_internal;
5047 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5048 }
35fc36a8 5049
4ad4eba5
AM
5050 if (definition && !dynamic)
5051 {
5052 char *p = strchr (name, ELF_VER_CHR);
5053 if (p != NULL && p[1] != ELF_VER_CHR)
5054 {
5055 /* Queue non-default versions so that .symver x, x@FOO
5056 aliases can be checked. */
66eb6687 5057 if (!nondeflt_vers)
4ad4eba5 5058 {
66eb6687
AM
5059 amt = ((isymend - isym + 1)
5060 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5061 nondeflt_vers
5062 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5063 if (!nondeflt_vers)
5064 goto error_free_vers;
4ad4eba5 5065 }
66eb6687 5066 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5067 }
5068 }
5069
5070 if (dynsym && h->dynindx == -1)
5071 {
c152c796 5072 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5073 goto error_free_vers;
60d67dc8
AM
5074 if (h->is_weakalias
5075 && weakdef (h)->dynindx == -1)
4ad4eba5 5076 {
60d67dc8 5077 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5078 goto error_free_vers;
5079 }
5080 }
1f599d0e 5081 else if (h->dynindx != -1)
4ad4eba5
AM
5082 /* If the symbol already has a dynamic index, but
5083 visibility says it should not be visible, turn it into
5084 a local symbol. */
5085 switch (ELF_ST_VISIBILITY (h->other))
5086 {
5087 case STV_INTERNAL:
5088 case STV_HIDDEN:
5089 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5090 dynsym = FALSE;
5091 break;
5092 }
5093
aef28989
L
5094 /* Don't add DT_NEEDED for references from the dummy bfd nor
5095 for unmatched symbol. */
4ad4eba5 5096 if (!add_needed
aef28989 5097 && matched
4ad4eba5 5098 && definition
010e5ae2 5099 && ((dynsym
ffa9430d 5100 && h->ref_regular_nonweak
4f3fedcf
AM
5101 && (old_bfd == NULL
5102 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5103 || (h->ref_dynamic_nonweak
010e5ae2 5104 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5105 && !on_needed_list (elf_dt_name (abfd),
5106 htab->needed, NULL))))
4ad4eba5
AM
5107 {
5108 int ret;
5109 const char *soname = elf_dt_name (abfd);
5110
16e4ecc0
AM
5111 info->callbacks->minfo ("%!", soname, old_bfd,
5112 h->root.root.string);
5113
4ad4eba5
AM
5114 /* A symbol from a library loaded via DT_NEEDED of some
5115 other library is referenced by a regular object.
e56f61be 5116 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5117 --no-add-needed is used and the reference was not
5118 a weak one. */
4f3fedcf 5119 if (old_bfd != NULL
b918acf9 5120 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5121 {
4eca0228 5122 _bfd_error_handler
695344c0 5123 /* xgettext:c-format */
871b3ab2 5124 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5125 old_bfd, name);
ff5ac77b 5126 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5127 goto error_free_vers;
5128 }
5129
a50b1753 5130 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5131 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5132
4ad4eba5 5133 add_needed = TRUE;
7e9f0867 5134 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5135 if (ret < 0)
5136 goto error_free_vers;
5137
5138 BFD_ASSERT (ret == 0);
5139 }
5140 }
5141 }
5142
a83ef4d1
L
5143 if (info->lto_plugin_active
5144 && !bfd_link_relocatable (info)
5145 && (abfd->flags & BFD_PLUGIN) == 0
5146 && !just_syms
5147 && extsymcount)
5148 {
5149 int r_sym_shift;
5150
5151 if (bed->s->arch_size == 32)
5152 r_sym_shift = 8;
5153 else
5154 r_sym_shift = 32;
5155
5156 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5157 referenced in regular objects so that linker plugin will get
5158 the correct symbol resolution. */
5159
5160 sym_hash = elf_sym_hashes (abfd);
5161 for (s = abfd->sections; s != NULL; s = s->next)
5162 {
5163 Elf_Internal_Rela *internal_relocs;
5164 Elf_Internal_Rela *rel, *relend;
5165
5166 /* Don't check relocations in excluded sections. */
5167 if ((s->flags & SEC_RELOC) == 0
5168 || s->reloc_count == 0
5169 || (s->flags & SEC_EXCLUDE) != 0
5170 || ((info->strip == strip_all
5171 || info->strip == strip_debugger)
5172 && (s->flags & SEC_DEBUGGING) != 0))
5173 continue;
5174
5175 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5176 NULL,
5177 info->keep_memory);
5178 if (internal_relocs == NULL)
5179 goto error_free_vers;
5180
5181 rel = internal_relocs;
5182 relend = rel + s->reloc_count;
5183 for ( ; rel < relend; rel++)
5184 {
5185 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5186 struct elf_link_hash_entry *h;
5187
5188 /* Skip local symbols. */
5189 if (r_symndx < extsymoff)
5190 continue;
5191
5192 h = sym_hash[r_symndx - extsymoff];
5193 if (h != NULL)
5194 h->root.non_ir_ref_regular = 1;
5195 }
5196
5197 if (elf_section_data (s)->relocs != internal_relocs)
5198 free (internal_relocs);
5199 }
5200 }
5201
66eb6687
AM
5202 if (extversym != NULL)
5203 {
5204 free (extversym);
5205 extversym = NULL;
5206 }
5207
5208 if (isymbuf != NULL)
5209 {
5210 free (isymbuf);
5211 isymbuf = NULL;
5212 }
5213
5214 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5215 {
5216 unsigned int i;
5217
5218 /* Restore the symbol table. */
f45794cb
AM
5219 old_ent = (char *) old_tab + tabsize;
5220 memset (elf_sym_hashes (abfd), 0,
5221 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5222 htab->root.table.table = old_table;
5223 htab->root.table.size = old_size;
5224 htab->root.table.count = old_count;
66eb6687 5225 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5226 htab->root.undefs = old_undefs;
5227 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5228 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5229 free (old_strtab);
5230 old_strtab = NULL;
66eb6687
AM
5231 for (i = 0; i < htab->root.table.size; i++)
5232 {
5233 struct bfd_hash_entry *p;
5234 struct elf_link_hash_entry *h;
3e0882af
L
5235 bfd_size_type size;
5236 unsigned int alignment_power;
4070765b 5237 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5238
5239 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5240 {
5241 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5242 if (h->root.type == bfd_link_hash_warning)
5243 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5244
3e0882af
L
5245 /* Preserve the maximum alignment and size for common
5246 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5247 since it can still be loaded at run time by another
3e0882af
L
5248 dynamic lib. */
5249 if (h->root.type == bfd_link_hash_common)
5250 {
5251 size = h->root.u.c.size;
5252 alignment_power = h->root.u.c.p->alignment_power;
5253 }
5254 else
5255 {
5256 size = 0;
5257 alignment_power = 0;
5258 }
4070765b 5259 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5260 will be exported when the dynamic lib becomes needed
5261 in the second pass. */
4070765b 5262 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5263 memcpy (p, old_ent, htab->root.table.entsize);
5264 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5265 h = (struct elf_link_hash_entry *) p;
5266 if (h->root.type == bfd_link_hash_warning)
5267 {
5268 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5269 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5270 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5271 }
a4542f1b 5272 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5273 {
5274 if (size > h->root.u.c.size)
5275 h->root.u.c.size = size;
5276 if (alignment_power > h->root.u.c.p->alignment_power)
5277 h->root.u.c.p->alignment_power = alignment_power;
5278 }
4070765b 5279 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5280 }
5281 }
5282
5061a885
AM
5283 /* Make a special call to the linker "notice" function to
5284 tell it that symbols added for crefs may need to be removed. */
e5034e59 5285 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5286 goto error_free_vers;
5061a885 5287
66eb6687
AM
5288 free (old_tab);
5289 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5290 alloc_mark);
5291 if (nondeflt_vers != NULL)
5292 free (nondeflt_vers);
5293 return TRUE;
5294 }
2de92251 5295
66eb6687
AM
5296 if (old_tab != NULL)
5297 {
e5034e59 5298 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5299 goto error_free_vers;
66eb6687
AM
5300 free (old_tab);
5301 old_tab = NULL;
5302 }
5303
c6e8a9a8
L
5304 /* Now that all the symbols from this input file are created, if
5305 not performing a relocatable link, handle .symver foo, foo@BAR
5306 such that any relocs against foo become foo@BAR. */
0e1862bb 5307 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5308 {
ef53be89 5309 size_t cnt, symidx;
4ad4eba5
AM
5310
5311 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5312 {
5313 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5314 char *shortname, *p;
5315
5316 p = strchr (h->root.root.string, ELF_VER_CHR);
5317 if (p == NULL
5318 || (h->root.type != bfd_link_hash_defined
5319 && h->root.type != bfd_link_hash_defweak))
5320 continue;
5321
5322 amt = p - h->root.root.string;
a50b1753 5323 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5324 if (!shortname)
5325 goto error_free_vers;
4ad4eba5
AM
5326 memcpy (shortname, h->root.root.string, amt);
5327 shortname[amt] = '\0';
5328
5329 hi = (struct elf_link_hash_entry *)
66eb6687 5330 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5331 FALSE, FALSE, FALSE);
5332 if (hi != NULL
5333 && hi->root.type == h->root.type
5334 && hi->root.u.def.value == h->root.u.def.value
5335 && hi->root.u.def.section == h->root.u.def.section)
5336 {
5337 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5338 hi->root.type = bfd_link_hash_indirect;
5339 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5340 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5341 sym_hash = elf_sym_hashes (abfd);
5342 if (sym_hash)
5343 for (symidx = 0; symidx < extsymcount; ++symidx)
5344 if (sym_hash[symidx] == hi)
5345 {
5346 sym_hash[symidx] = h;
5347 break;
5348 }
5349 }
5350 free (shortname);
5351 }
5352 free (nondeflt_vers);
5353 nondeflt_vers = NULL;
5354 }
5355
60d67dc8 5356 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5357 symbols we found. The only way to do this is to search all the
5358 symbols. Since we only need the information for non functions in
5359 dynamic objects, that's the only time we actually put anything on
5360 the list WEAKS. We need this information so that if a regular
5361 object refers to a symbol defined weakly in a dynamic object, the
5362 real symbol in the dynamic object is also put in the dynamic
5363 symbols; we also must arrange for both symbols to point to the
5364 same memory location. We could handle the general case of symbol
5365 aliasing, but a general symbol alias can only be generated in
5366 assembler code, handling it correctly would be very time
5367 consuming, and other ELF linkers don't handle general aliasing
5368 either. */
5369 if (weaks != NULL)
5370 {
5371 struct elf_link_hash_entry **hpp;
5372 struct elf_link_hash_entry **hppend;
5373 struct elf_link_hash_entry **sorted_sym_hash;
5374 struct elf_link_hash_entry *h;
5375 size_t sym_count;
5376
5377 /* Since we have to search the whole symbol list for each weak
5378 defined symbol, search time for N weak defined symbols will be
5379 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89 5380 amt = extsymcount;
3a3f4bf7
AM
5381 amt *= sizeof (*sorted_sym_hash);
5382 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5383 if (sorted_sym_hash == NULL)
5384 goto error_return;
5385 sym_hash = sorted_sym_hash;
5386 hpp = elf_sym_hashes (abfd);
5387 hppend = hpp + extsymcount;
5388 sym_count = 0;
5389 for (; hpp < hppend; hpp++)
5390 {
5391 h = *hpp;
5392 if (h != NULL
5393 && h->root.type == bfd_link_hash_defined
fcb93ecf 5394 && !bed->is_function_type (h->type))
4ad4eba5
AM
5395 {
5396 *sym_hash = h;
5397 sym_hash++;
5398 sym_count++;
5399 }
5400 }
5401
3a3f4bf7 5402 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5403 elf_sort_symbol);
5404
5405 while (weaks != NULL)
5406 {
5407 struct elf_link_hash_entry *hlook;
5408 asection *slook;
5409 bfd_vma vlook;
ed54588d 5410 size_t i, j, idx = 0;
4ad4eba5
AM
5411
5412 hlook = weaks;
60d67dc8
AM
5413 weaks = hlook->u.alias;
5414 hlook->u.alias = NULL;
4ad4eba5 5415
e3e53eed
AM
5416 if (hlook->root.type != bfd_link_hash_defined
5417 && hlook->root.type != bfd_link_hash_defweak)
5418 continue;
5419
4ad4eba5
AM
5420 slook = hlook->root.u.def.section;
5421 vlook = hlook->root.u.def.value;
5422
4ad4eba5
AM
5423 i = 0;
5424 j = sym_count;
14160578 5425 while (i != j)
4ad4eba5
AM
5426 {
5427 bfd_signed_vma vdiff;
5428 idx = (i + j) / 2;
14160578 5429 h = sorted_sym_hash[idx];
4ad4eba5
AM
5430 vdiff = vlook - h->root.u.def.value;
5431 if (vdiff < 0)
5432 j = idx;
5433 else if (vdiff > 0)
5434 i = idx + 1;
5435 else
5436 {
d3435ae8 5437 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5438 if (sdiff < 0)
5439 j = idx;
5440 else if (sdiff > 0)
5441 i = idx + 1;
5442 else
14160578 5443 break;
4ad4eba5
AM
5444 }
5445 }
5446
5447 /* We didn't find a value/section match. */
14160578 5448 if (i == j)
4ad4eba5
AM
5449 continue;
5450
14160578
AM
5451 /* With multiple aliases, or when the weak symbol is already
5452 strongly defined, we have multiple matching symbols and
5453 the binary search above may land on any of them. Step
5454 one past the matching symbol(s). */
5455 while (++idx != j)
5456 {
5457 h = sorted_sym_hash[idx];
5458 if (h->root.u.def.section != slook
5459 || h->root.u.def.value != vlook)
5460 break;
5461 }
5462
5463 /* Now look back over the aliases. Since we sorted by size
5464 as well as value and section, we'll choose the one with
5465 the largest size. */
5466 while (idx-- != i)
4ad4eba5 5467 {
14160578 5468 h = sorted_sym_hash[idx];
4ad4eba5
AM
5469
5470 /* Stop if value or section doesn't match. */
14160578
AM
5471 if (h->root.u.def.section != slook
5472 || h->root.u.def.value != vlook)
4ad4eba5
AM
5473 break;
5474 else if (h != hlook)
5475 {
60d67dc8
AM
5476 struct elf_link_hash_entry *t;
5477
5478 hlook->u.alias = h;
5479 hlook->is_weakalias = 1;
5480 t = h;
5481 if (t->u.alias != NULL)
5482 while (t->u.alias != h)
5483 t = t->u.alias;
5484 t->u.alias = hlook;
4ad4eba5
AM
5485
5486 /* If the weak definition is in the list of dynamic
5487 symbols, make sure the real definition is put
5488 there as well. */
5489 if (hlook->dynindx != -1 && h->dynindx == -1)
5490 {
c152c796 5491 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5492 {
5493 err_free_sym_hash:
5494 free (sorted_sym_hash);
5495 goto error_return;
5496 }
4ad4eba5
AM
5497 }
5498
5499 /* If the real definition is in the list of dynamic
5500 symbols, make sure the weak definition is put
5501 there as well. If we don't do this, then the
5502 dynamic loader might not merge the entries for the
5503 real definition and the weak definition. */
5504 if (h->dynindx != -1 && hlook->dynindx == -1)
5505 {
c152c796 5506 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5507 goto err_free_sym_hash;
4ad4eba5
AM
5508 }
5509 break;
5510 }
5511 }
5512 }
5513
5514 free (sorted_sym_hash);
5515 }
5516
33177bb1
AM
5517 if (bed->check_directives
5518 && !(*bed->check_directives) (abfd, info))
5519 return FALSE;
85fbca6a 5520
4ad4eba5
AM
5521 /* If this is a non-traditional link, try to optimize the handling
5522 of the .stab/.stabstr sections. */
5523 if (! dynamic
5524 && ! info->traditional_format
66eb6687 5525 && is_elf_hash_table (htab)
4ad4eba5
AM
5526 && (info->strip != strip_all && info->strip != strip_debugger))
5527 {
5528 asection *stabstr;
5529
5530 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5531 if (stabstr != NULL)
5532 {
5533 bfd_size_type string_offset = 0;
5534 asection *stab;
5535
5536 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5537 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5538 && (!stab->name[5] ||
5539 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5540 && (stab->flags & SEC_MERGE) == 0
5541 && !bfd_is_abs_section (stab->output_section))
5542 {
5543 struct bfd_elf_section_data *secdata;
5544
5545 secdata = elf_section_data (stab);
66eb6687
AM
5546 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5547 stabstr, &secdata->sec_info,
4ad4eba5
AM
5548 &string_offset))
5549 goto error_return;
5550 if (secdata->sec_info)
dbaa2011 5551 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5552 }
5553 }
5554 }
5555
66eb6687 5556 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5557 {
5558 /* Add this bfd to the loaded list. */
5559 struct elf_link_loaded_list *n;
5560
ca4be51c 5561 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5562 if (n == NULL)
5563 goto error_return;
5564 n->abfd = abfd;
66eb6687
AM
5565 n->next = htab->loaded;
5566 htab->loaded = n;
4ad4eba5
AM
5567 }
5568
5569 return TRUE;
5570
5571 error_free_vers:
66eb6687
AM
5572 if (old_tab != NULL)
5573 free (old_tab);
5b677558
AM
5574 if (old_strtab != NULL)
5575 free (old_strtab);
4ad4eba5
AM
5576 if (nondeflt_vers != NULL)
5577 free (nondeflt_vers);
5578 if (extversym != NULL)
5579 free (extversym);
5580 error_free_sym:
5581 if (isymbuf != NULL)
5582 free (isymbuf);
5583 error_return:
5584 return FALSE;
5585}
5586
8387904d
AM
5587/* Return the linker hash table entry of a symbol that might be
5588 satisfied by an archive symbol. Return -1 on error. */
5589
5590struct elf_link_hash_entry *
5591_bfd_elf_archive_symbol_lookup (bfd *abfd,
5592 struct bfd_link_info *info,
5593 const char *name)
5594{
5595 struct elf_link_hash_entry *h;
5596 char *p, *copy;
5597 size_t len, first;
5598
2a41f396 5599 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5600 if (h != NULL)
5601 return h;
5602
5603 /* If this is a default version (the name contains @@), look up the
5604 symbol again with only one `@' as well as without the version.
5605 The effect is that references to the symbol with and without the
5606 version will be matched by the default symbol in the archive. */
5607
5608 p = strchr (name, ELF_VER_CHR);
5609 if (p == NULL || p[1] != ELF_VER_CHR)
5610 return h;
5611
5612 /* First check with only one `@'. */
5613 len = strlen (name);
a50b1753 5614 copy = (char *) bfd_alloc (abfd, len);
8387904d 5615 if (copy == NULL)
e99955cd 5616 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5617
5618 first = p - name + 1;
5619 memcpy (copy, name, first);
5620 memcpy (copy + first, name + first + 1, len - first);
5621
2a41f396 5622 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5623 if (h == NULL)
5624 {
5625 /* We also need to check references to the symbol without the
5626 version. */
5627 copy[first - 1] = '\0';
5628 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5629 FALSE, FALSE, TRUE);
8387904d
AM
5630 }
5631
5632 bfd_release (abfd, copy);
5633 return h;
5634}
5635
0ad989f9 5636/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5637 don't use _bfd_generic_link_add_archive_symbols because we need to
5638 handle versioned symbols.
0ad989f9
L
5639
5640 Fortunately, ELF archive handling is simpler than that done by
5641 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5642 oddities. In ELF, if we find a symbol in the archive map, and the
5643 symbol is currently undefined, we know that we must pull in that
5644 object file.
5645
5646 Unfortunately, we do have to make multiple passes over the symbol
5647 table until nothing further is resolved. */
5648
4ad4eba5
AM
5649static bfd_boolean
5650elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5651{
5652 symindex c;
13e570f8 5653 unsigned char *included = NULL;
0ad989f9
L
5654 carsym *symdefs;
5655 bfd_boolean loop;
5656 bfd_size_type amt;
8387904d
AM
5657 const struct elf_backend_data *bed;
5658 struct elf_link_hash_entry * (*archive_symbol_lookup)
5659 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5660
5661 if (! bfd_has_map (abfd))
5662 {
5663 /* An empty archive is a special case. */
5664 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5665 return TRUE;
5666 bfd_set_error (bfd_error_no_armap);
5667 return FALSE;
5668 }
5669
5670 /* Keep track of all symbols we know to be already defined, and all
5671 files we know to be already included. This is to speed up the
5672 second and subsequent passes. */
5673 c = bfd_ardata (abfd)->symdef_count;
5674 if (c == 0)
5675 return TRUE;
5676 amt = c;
13e570f8
AM
5677 amt *= sizeof (*included);
5678 included = (unsigned char *) bfd_zmalloc (amt);
5679 if (included == NULL)
5680 return FALSE;
0ad989f9
L
5681
5682 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5683 bed = get_elf_backend_data (abfd);
5684 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5685
5686 do
5687 {
5688 file_ptr last;
5689 symindex i;
5690 carsym *symdef;
5691 carsym *symdefend;
5692
5693 loop = FALSE;
5694 last = -1;
5695
5696 symdef = symdefs;
5697 symdefend = symdef + c;
5698 for (i = 0; symdef < symdefend; symdef++, i++)
5699 {
5700 struct elf_link_hash_entry *h;
5701 bfd *element;
5702 struct bfd_link_hash_entry *undefs_tail;
5703 symindex mark;
5704
13e570f8 5705 if (included[i])
0ad989f9
L
5706 continue;
5707 if (symdef->file_offset == last)
5708 {
5709 included[i] = TRUE;
5710 continue;
5711 }
5712
8387904d 5713 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5714 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5715 goto error_return;
0ad989f9
L
5716
5717 if (h == NULL)
5718 continue;
5719
5720 if (h->root.type == bfd_link_hash_common)
5721 {
5722 /* We currently have a common symbol. The archive map contains
5723 a reference to this symbol, so we may want to include it. We
5724 only want to include it however, if this archive element
5725 contains a definition of the symbol, not just another common
5726 declaration of it.
5727
5728 Unfortunately some archivers (including GNU ar) will put
5729 declarations of common symbols into their archive maps, as
5730 well as real definitions, so we cannot just go by the archive
5731 map alone. Instead we must read in the element's symbol
5732 table and check that to see what kind of symbol definition
5733 this is. */
5734 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5735 continue;
5736 }
5737 else if (h->root.type != bfd_link_hash_undefined)
5738 {
5739 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5740 /* Symbol must be defined. Don't check it again. */
5741 included[i] = TRUE;
0ad989f9
L
5742 continue;
5743 }
5744
5745 /* We need to include this archive member. */
5746 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5747 if (element == NULL)
5748 goto error_return;
5749
5750 if (! bfd_check_format (element, bfd_object))
5751 goto error_return;
5752
0ad989f9
L
5753 undefs_tail = info->hash->undefs_tail;
5754
0e144ba7
AM
5755 if (!(*info->callbacks
5756 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5757 continue;
0e144ba7 5758 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5759 goto error_return;
5760
5761 /* If there are any new undefined symbols, we need to make
5762 another pass through the archive in order to see whether
5763 they can be defined. FIXME: This isn't perfect, because
5764 common symbols wind up on undefs_tail and because an
5765 undefined symbol which is defined later on in this pass
5766 does not require another pass. This isn't a bug, but it
5767 does make the code less efficient than it could be. */
5768 if (undefs_tail != info->hash->undefs_tail)
5769 loop = TRUE;
5770
5771 /* Look backward to mark all symbols from this object file
5772 which we have already seen in this pass. */
5773 mark = i;
5774 do
5775 {
5776 included[mark] = TRUE;
5777 if (mark == 0)
5778 break;
5779 --mark;
5780 }
5781 while (symdefs[mark].file_offset == symdef->file_offset);
5782
5783 /* We mark subsequent symbols from this object file as we go
5784 on through the loop. */
5785 last = symdef->file_offset;
5786 }
5787 }
5788 while (loop);
5789
0ad989f9
L
5790 free (included);
5791
5792 return TRUE;
5793
5794 error_return:
0ad989f9
L
5795 if (included != NULL)
5796 free (included);
5797 return FALSE;
5798}
4ad4eba5
AM
5799
5800/* Given an ELF BFD, add symbols to the global hash table as
5801 appropriate. */
5802
5803bfd_boolean
5804bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5805{
5806 switch (bfd_get_format (abfd))
5807 {
5808 case bfd_object:
5809 return elf_link_add_object_symbols (abfd, info);
5810 case bfd_archive:
5811 return elf_link_add_archive_symbols (abfd, info);
5812 default:
5813 bfd_set_error (bfd_error_wrong_format);
5814 return FALSE;
5815 }
5816}
5a580b3a 5817\f
14b1c01e
AM
5818struct hash_codes_info
5819{
5820 unsigned long *hashcodes;
5821 bfd_boolean error;
5822};
a0c8462f 5823
5a580b3a
AM
5824/* This function will be called though elf_link_hash_traverse to store
5825 all hash value of the exported symbols in an array. */
5826
5827static bfd_boolean
5828elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5829{
a50b1753 5830 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5831 const char *name;
5a580b3a
AM
5832 unsigned long ha;
5833 char *alc = NULL;
5834
5a580b3a
AM
5835 /* Ignore indirect symbols. These are added by the versioning code. */
5836 if (h->dynindx == -1)
5837 return TRUE;
5838
5839 name = h->root.root.string;
422f1182 5840 if (h->versioned >= versioned)
5a580b3a 5841 {
422f1182
L
5842 char *p = strchr (name, ELF_VER_CHR);
5843 if (p != NULL)
14b1c01e 5844 {
422f1182
L
5845 alc = (char *) bfd_malloc (p - name + 1);
5846 if (alc == NULL)
5847 {
5848 inf->error = TRUE;
5849 return FALSE;
5850 }
5851 memcpy (alc, name, p - name);
5852 alc[p - name] = '\0';
5853 name = alc;
14b1c01e 5854 }
5a580b3a
AM
5855 }
5856
5857 /* Compute the hash value. */
5858 ha = bfd_elf_hash (name);
5859
5860 /* Store the found hash value in the array given as the argument. */
14b1c01e 5861 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5862
5863 /* And store it in the struct so that we can put it in the hash table
5864 later. */
f6e332e6 5865 h->u.elf_hash_value = ha;
5a580b3a
AM
5866
5867 if (alc != NULL)
5868 free (alc);
5869
5870 return TRUE;
5871}
5872
fdc90cb4
JJ
5873struct collect_gnu_hash_codes
5874{
5875 bfd *output_bfd;
5876 const struct elf_backend_data *bed;
5877 unsigned long int nsyms;
5878 unsigned long int maskbits;
5879 unsigned long int *hashcodes;
5880 unsigned long int *hashval;
5881 unsigned long int *indx;
5882 unsigned long int *counts;
5883 bfd_vma *bitmask;
5884 bfd_byte *contents;
f16a9783 5885 bfd_size_type xlat;
fdc90cb4
JJ
5886 long int min_dynindx;
5887 unsigned long int bucketcount;
5888 unsigned long int symindx;
5889 long int local_indx;
5890 long int shift1, shift2;
5891 unsigned long int mask;
14b1c01e 5892 bfd_boolean error;
fdc90cb4
JJ
5893};
5894
5895/* This function will be called though elf_link_hash_traverse to store
5896 all hash value of the exported symbols in an array. */
5897
5898static bfd_boolean
5899elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5900{
a50b1753 5901 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5902 const char *name;
fdc90cb4
JJ
5903 unsigned long ha;
5904 char *alc = NULL;
5905
fdc90cb4
JJ
5906 /* Ignore indirect symbols. These are added by the versioning code. */
5907 if (h->dynindx == -1)
5908 return TRUE;
5909
5910 /* Ignore also local symbols and undefined symbols. */
5911 if (! (*s->bed->elf_hash_symbol) (h))
5912 return TRUE;
5913
5914 name = h->root.root.string;
422f1182 5915 if (h->versioned >= versioned)
fdc90cb4 5916 {
422f1182
L
5917 char *p = strchr (name, ELF_VER_CHR);
5918 if (p != NULL)
14b1c01e 5919 {
422f1182
L
5920 alc = (char *) bfd_malloc (p - name + 1);
5921 if (alc == NULL)
5922 {
5923 s->error = TRUE;
5924 return FALSE;
5925 }
5926 memcpy (alc, name, p - name);
5927 alc[p - name] = '\0';
5928 name = alc;
14b1c01e 5929 }
fdc90cb4
JJ
5930 }
5931
5932 /* Compute the hash value. */
5933 ha = bfd_elf_gnu_hash (name);
5934
5935 /* Store the found hash value in the array for compute_bucket_count,
5936 and also for .dynsym reordering purposes. */
5937 s->hashcodes[s->nsyms] = ha;
5938 s->hashval[h->dynindx] = ha;
5939 ++s->nsyms;
5940 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5941 s->min_dynindx = h->dynindx;
5942
5943 if (alc != NULL)
5944 free (alc);
5945
5946 return TRUE;
5947}
5948
5949/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
5950 final dynamic symbol renumbering in case of .gnu.hash.
5951 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5952 to the translation table. */
fdc90cb4
JJ
5953
5954static bfd_boolean
f16a9783 5955elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 5956{
a50b1753 5957 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5958 unsigned long int bucket;
5959 unsigned long int val;
5960
fdc90cb4
JJ
5961 /* Ignore indirect symbols. */
5962 if (h->dynindx == -1)
5963 return TRUE;
5964
5965 /* Ignore also local symbols and undefined symbols. */
5966 if (! (*s->bed->elf_hash_symbol) (h))
5967 {
5968 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
5969 {
5970 if (s->bed->record_xhash_symbol != NULL)
5971 {
5972 (*s->bed->record_xhash_symbol) (h, 0);
5973 s->local_indx++;
5974 }
5975 else
5976 h->dynindx = s->local_indx++;
5977 }
fdc90cb4
JJ
5978 return TRUE;
5979 }
5980
5981 bucket = s->hashval[h->dynindx] % s->bucketcount;
5982 val = (s->hashval[h->dynindx] >> s->shift1)
5983 & ((s->maskbits >> s->shift1) - 1);
5984 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5985 s->bitmask[val]
5986 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5987 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5988 if (s->counts[bucket] == 1)
5989 /* Last element terminates the chain. */
5990 val |= 1;
5991 bfd_put_32 (s->output_bfd, val,
5992 s->contents + (s->indx[bucket] - s->symindx) * 4);
5993 --s->counts[bucket];
f16a9783
MS
5994 if (s->bed->record_xhash_symbol != NULL)
5995 {
5996 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5997
5998 (*s->bed->record_xhash_symbol) (h, xlat_loc);
5999 }
6000 else
6001 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6002 return TRUE;
6003}
6004
6005/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6006
6007bfd_boolean
6008_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6009{
6010 return !(h->forced_local
6011 || h->root.type == bfd_link_hash_undefined
6012 || h->root.type == bfd_link_hash_undefweak
6013 || ((h->root.type == bfd_link_hash_defined
6014 || h->root.type == bfd_link_hash_defweak)
6015 && h->root.u.def.section->output_section == NULL));
6016}
6017
5a580b3a
AM
6018/* Array used to determine the number of hash table buckets to use
6019 based on the number of symbols there are. If there are fewer than
6020 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6021 fewer than 37 we use 17 buckets, and so forth. We never use more
6022 than 32771 buckets. */
6023
6024static const size_t elf_buckets[] =
6025{
6026 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6027 16411, 32771, 0
6028};
6029
6030/* Compute bucket count for hashing table. We do not use a static set
6031 of possible tables sizes anymore. Instead we determine for all
6032 possible reasonable sizes of the table the outcome (i.e., the
6033 number of collisions etc) and choose the best solution. The
6034 weighting functions are not too simple to allow the table to grow
6035 without bounds. Instead one of the weighting factors is the size.
6036 Therefore the result is always a good payoff between few collisions
6037 (= short chain lengths) and table size. */
6038static size_t
b20dd2ce 6039compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6040 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6041 unsigned long int nsyms,
6042 int gnu_hash)
5a580b3a 6043{
5a580b3a 6044 size_t best_size = 0;
5a580b3a 6045 unsigned long int i;
5a580b3a 6046
5a580b3a
AM
6047 /* We have a problem here. The following code to optimize the table
6048 size requires an integer type with more the 32 bits. If
6049 BFD_HOST_U_64_BIT is set we know about such a type. */
6050#ifdef BFD_HOST_U_64_BIT
6051 if (info->optimize)
6052 {
5a580b3a
AM
6053 size_t minsize;
6054 size_t maxsize;
6055 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6056 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6057 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6058 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6059 unsigned long int *counts;
d40f3da9 6060 bfd_size_type amt;
0883b6e0 6061 unsigned int no_improvement_count = 0;
5a580b3a
AM
6062
6063 /* Possible optimization parameters: if we have NSYMS symbols we say
6064 that the hashing table must at least have NSYMS/4 and at most
6065 2*NSYMS buckets. */
6066 minsize = nsyms / 4;
6067 if (minsize == 0)
6068 minsize = 1;
6069 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6070 if (gnu_hash)
6071 {
6072 if (minsize < 2)
6073 minsize = 2;
6074 if ((best_size & 31) == 0)
6075 ++best_size;
6076 }
5a580b3a
AM
6077
6078 /* Create array where we count the collisions in. We must use bfd_malloc
6079 since the size could be large. */
6080 amt = maxsize;
6081 amt *= sizeof (unsigned long int);
a50b1753 6082 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6083 if (counts == NULL)
fdc90cb4 6084 return 0;
5a580b3a
AM
6085
6086 /* Compute the "optimal" size for the hash table. The criteria is a
6087 minimal chain length. The minor criteria is (of course) the size
6088 of the table. */
6089 for (i = minsize; i < maxsize; ++i)
6090 {
6091 /* Walk through the array of hashcodes and count the collisions. */
6092 BFD_HOST_U_64_BIT max;
6093 unsigned long int j;
6094 unsigned long int fact;
6095
fdc90cb4
JJ
6096 if (gnu_hash && (i & 31) == 0)
6097 continue;
6098
5a580b3a
AM
6099 memset (counts, '\0', i * sizeof (unsigned long int));
6100
6101 /* Determine how often each hash bucket is used. */
6102 for (j = 0; j < nsyms; ++j)
6103 ++counts[hashcodes[j] % i];
6104
6105 /* For the weight function we need some information about the
6106 pagesize on the target. This is information need not be 100%
6107 accurate. Since this information is not available (so far) we
6108 define it here to a reasonable default value. If it is crucial
6109 to have a better value some day simply define this value. */
6110# ifndef BFD_TARGET_PAGESIZE
6111# define BFD_TARGET_PAGESIZE (4096)
6112# endif
6113
fdc90cb4
JJ
6114 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6115 and the chains. */
6116 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6117
6118# if 1
6119 /* Variant 1: optimize for short chains. We add the squares
6120 of all the chain lengths (which favors many small chain
6121 over a few long chains). */
6122 for (j = 0; j < i; ++j)
6123 max += counts[j] * counts[j];
6124
6125 /* This adds penalties for the overall size of the table. */
fdc90cb4 6126 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6127 max *= fact * fact;
6128# else
6129 /* Variant 2: Optimize a lot more for small table. Here we
6130 also add squares of the size but we also add penalties for
6131 empty slots (the +1 term). */
6132 for (j = 0; j < i; ++j)
6133 max += (1 + counts[j]) * (1 + counts[j]);
6134
6135 /* The overall size of the table is considered, but not as
6136 strong as in variant 1, where it is squared. */
fdc90cb4 6137 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6138 max *= fact;
6139# endif
6140
6141 /* Compare with current best results. */
6142 if (max < best_chlen)
6143 {
6144 best_chlen = max;
6145 best_size = i;
ca4be51c 6146 no_improvement_count = 0;
5a580b3a 6147 }
0883b6e0
NC
6148 /* PR 11843: Avoid futile long searches for the best bucket size
6149 when there are a large number of symbols. */
6150 else if (++no_improvement_count == 100)
6151 break;
5a580b3a
AM
6152 }
6153
6154 free (counts);
6155 }
6156 else
6157#endif /* defined (BFD_HOST_U_64_BIT) */
6158 {
6159 /* This is the fallback solution if no 64bit type is available or if we
6160 are not supposed to spend much time on optimizations. We select the
6161 bucket count using a fixed set of numbers. */
6162 for (i = 0; elf_buckets[i] != 0; i++)
6163 {
6164 best_size = elf_buckets[i];
fdc90cb4 6165 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6166 break;
6167 }
fdc90cb4
JJ
6168 if (gnu_hash && best_size < 2)
6169 best_size = 2;
5a580b3a
AM
6170 }
6171
5a580b3a
AM
6172 return best_size;
6173}
6174
d0bf826b
AM
6175/* Size any SHT_GROUP section for ld -r. */
6176
6177bfd_boolean
6178_bfd_elf_size_group_sections (struct bfd_link_info *info)
6179{
6180 bfd *ibfd;
57963c05 6181 asection *s;
d0bf826b 6182
c72f2fb2 6183 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6184 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6185 && (s = ibfd->sections) != NULL
6186 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6187 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6188 return FALSE;
6189 return TRUE;
6190}
6191
04c3a755
NS
6192/* Set a default stack segment size. The value in INFO wins. If it
6193 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6194 undefined it is initialized. */
6195
6196bfd_boolean
6197bfd_elf_stack_segment_size (bfd *output_bfd,
6198 struct bfd_link_info *info,
6199 const char *legacy_symbol,
6200 bfd_vma default_size)
6201{
6202 struct elf_link_hash_entry *h = NULL;
6203
6204 /* Look for legacy symbol. */
6205 if (legacy_symbol)
6206 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6207 FALSE, FALSE, FALSE);
6208 if (h && (h->root.type == bfd_link_hash_defined
6209 || h->root.type == bfd_link_hash_defweak)
6210 && h->def_regular
6211 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6212 {
6213 /* The symbol has no type if specified on the command line. */
6214 h->type = STT_OBJECT;
6215 if (info->stacksize)
695344c0 6216 /* xgettext:c-format */
871b3ab2 6217 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6218 output_bfd, legacy_symbol);
04c3a755 6219 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6220 /* xgettext:c-format */
871b3ab2 6221 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6222 output_bfd, legacy_symbol);
04c3a755
NS
6223 else
6224 info->stacksize = h->root.u.def.value;
6225 }
6226
6227 if (!info->stacksize)
6228 /* If the user didn't set a size, or explicitly inhibit the
6229 size, set it now. */
6230 info->stacksize = default_size;
6231
6232 /* Provide the legacy symbol, if it is referenced. */
6233 if (h && (h->root.type == bfd_link_hash_undefined
6234 || h->root.type == bfd_link_hash_undefweak))
6235 {
6236 struct bfd_link_hash_entry *bh = NULL;
6237
6238 if (!(_bfd_generic_link_add_one_symbol
6239 (info, output_bfd, legacy_symbol,
6240 BSF_GLOBAL, bfd_abs_section_ptr,
6241 info->stacksize >= 0 ? info->stacksize : 0,
6242 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6243 return FALSE;
6244
6245 h = (struct elf_link_hash_entry *) bh;
6246 h->def_regular = 1;
6247 h->type = STT_OBJECT;
6248 }
6249
6250 return TRUE;
6251}
6252
b531344c
MR
6253/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6254
6255struct elf_gc_sweep_symbol_info
6256{
6257 struct bfd_link_info *info;
6258 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6259 bfd_boolean);
6260};
6261
6262static bfd_boolean
6263elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6264{
6265 if (!h->mark
6266 && (((h->root.type == bfd_link_hash_defined
6267 || h->root.type == bfd_link_hash_defweak)
6268 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6269 && h->root.u.def.section->gc_mark))
6270 || h->root.type == bfd_link_hash_undefined
6271 || h->root.type == bfd_link_hash_undefweak))
6272 {
6273 struct elf_gc_sweep_symbol_info *inf;
6274
6275 inf = (struct elf_gc_sweep_symbol_info *) data;
6276 (*inf->hide_symbol) (inf->info, h, TRUE);
6277 h->def_regular = 0;
6278 h->ref_regular = 0;
6279 h->ref_regular_nonweak = 0;
6280 }
6281
6282 return TRUE;
6283}
6284
5a580b3a
AM
6285/* Set up the sizes and contents of the ELF dynamic sections. This is
6286 called by the ELF linker emulation before_allocation routine. We
6287 must set the sizes of the sections before the linker sets the
6288 addresses of the various sections. */
6289
6290bfd_boolean
6291bfd_elf_size_dynamic_sections (bfd *output_bfd,
6292 const char *soname,
6293 const char *rpath,
6294 const char *filter_shlib,
7ee314fa
AM
6295 const char *audit,
6296 const char *depaudit,
5a580b3a
AM
6297 const char * const *auxiliary_filters,
6298 struct bfd_link_info *info,
fd91d419 6299 asection **sinterpptr)
5a580b3a 6300{
5a580b3a
AM
6301 bfd *dynobj;
6302 const struct elf_backend_data *bed;
5a580b3a
AM
6303
6304 *sinterpptr = NULL;
6305
5a580b3a
AM
6306 if (!is_elf_hash_table (info->hash))
6307 return TRUE;
6308
5a580b3a
AM
6309 dynobj = elf_hash_table (info)->dynobj;
6310
9a2a56cc 6311 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6312 {
902e9fc7
MR
6313 struct bfd_elf_version_tree *verdefs;
6314 struct elf_info_failed asvinfo;
5a580b3a
AM
6315 struct bfd_elf_version_tree *t;
6316 struct bfd_elf_version_expr *d;
902e9fc7 6317 asection *s;
e6699019 6318 size_t soname_indx;
7ee314fa 6319
5a580b3a
AM
6320 /* If we are supposed to export all symbols into the dynamic symbol
6321 table (this is not the normal case), then do so. */
55255dae 6322 if (info->export_dynamic
0e1862bb 6323 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6324 {
3d13f3e9
AM
6325 struct elf_info_failed eif;
6326
6327 eif.info = info;
6328 eif.failed = FALSE;
5a580b3a
AM
6329 elf_link_hash_traverse (elf_hash_table (info),
6330 _bfd_elf_export_symbol,
6331 &eif);
6332 if (eif.failed)
6333 return FALSE;
6334 }
6335
e6699019
L
6336 if (soname != NULL)
6337 {
6338 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6339 soname, TRUE);
6340 if (soname_indx == (size_t) -1
6341 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6342 return FALSE;
6343 }
6344 else
6345 soname_indx = (size_t) -1;
6346
5a580b3a 6347 /* Make all global versions with definition. */
fd91d419 6348 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6349 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6350 if (!d->symver && d->literal)
5a580b3a
AM
6351 {
6352 const char *verstr, *name;
6353 size_t namelen, verlen, newlen;
93252b1c 6354 char *newname, *p, leading_char;
5a580b3a
AM
6355 struct elf_link_hash_entry *newh;
6356
93252b1c 6357 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6358 name = d->pattern;
93252b1c 6359 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6360 verstr = t->name;
6361 verlen = strlen (verstr);
6362 newlen = namelen + verlen + 3;
6363
a50b1753 6364 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6365 if (newname == NULL)
6366 return FALSE;
93252b1c
MF
6367 newname[0] = leading_char;
6368 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6369
6370 /* Check the hidden versioned definition. */
6371 p = newname + namelen;
6372 *p++ = ELF_VER_CHR;
6373 memcpy (p, verstr, verlen + 1);
6374 newh = elf_link_hash_lookup (elf_hash_table (info),
6375 newname, FALSE, FALSE,
6376 FALSE);
6377 if (newh == NULL
6378 || (newh->root.type != bfd_link_hash_defined
6379 && newh->root.type != bfd_link_hash_defweak))
6380 {
6381 /* Check the default versioned definition. */
6382 *p++ = ELF_VER_CHR;
6383 memcpy (p, verstr, verlen + 1);
6384 newh = elf_link_hash_lookup (elf_hash_table (info),
6385 newname, FALSE, FALSE,
6386 FALSE);
6387 }
6388 free (newname);
6389
6390 /* Mark this version if there is a definition and it is
6391 not defined in a shared object. */
6392 if (newh != NULL
f5385ebf 6393 && !newh->def_dynamic
5a580b3a
AM
6394 && (newh->root.type == bfd_link_hash_defined
6395 || newh->root.type == bfd_link_hash_defweak))
6396 d->symver = 1;
6397 }
6398
6399 /* Attach all the symbols to their version information. */
5a580b3a 6400 asvinfo.info = info;
5a580b3a
AM
6401 asvinfo.failed = FALSE;
6402
6403 elf_link_hash_traverse (elf_hash_table (info),
6404 _bfd_elf_link_assign_sym_version,
6405 &asvinfo);
6406 if (asvinfo.failed)
6407 return FALSE;
6408
6409 if (!info->allow_undefined_version)
6410 {
6411 /* Check if all global versions have a definition. */
3d13f3e9 6412 bfd_boolean all_defined = TRUE;
fd91d419 6413 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6414 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6415 if (d->literal && !d->symver && !d->script)
5a580b3a 6416 {
4eca0228 6417 _bfd_error_handler
5a580b3a
AM
6418 (_("%s: undefined version: %s"),
6419 d->pattern, t->name);
6420 all_defined = FALSE;
6421 }
6422
6423 if (!all_defined)
6424 {
6425 bfd_set_error (bfd_error_bad_value);
6426 return FALSE;
6427 }
6428 }
6429
902e9fc7
MR
6430 /* Set up the version definition section. */
6431 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6432 BFD_ASSERT (s != NULL);
5a580b3a 6433
902e9fc7
MR
6434 /* We may have created additional version definitions if we are
6435 just linking a regular application. */
6436 verdefs = info->version_info;
5a580b3a 6437
902e9fc7
MR
6438 /* Skip anonymous version tag. */
6439 if (verdefs != NULL && verdefs->vernum == 0)
6440 verdefs = verdefs->next;
5a580b3a 6441
902e9fc7
MR
6442 if (verdefs == NULL && !info->create_default_symver)
6443 s->flags |= SEC_EXCLUDE;
6444 else
5a580b3a 6445 {
902e9fc7
MR
6446 unsigned int cdefs;
6447 bfd_size_type size;
6448 bfd_byte *p;
6449 Elf_Internal_Verdef def;
6450 Elf_Internal_Verdaux defaux;
6451 struct bfd_link_hash_entry *bh;
6452 struct elf_link_hash_entry *h;
6453 const char *name;
5a580b3a 6454
902e9fc7
MR
6455 cdefs = 0;
6456 size = 0;
5a580b3a 6457
902e9fc7
MR
6458 /* Make space for the base version. */
6459 size += sizeof (Elf_External_Verdef);
6460 size += sizeof (Elf_External_Verdaux);
6461 ++cdefs;
6462
6463 /* Make space for the default version. */
6464 if (info->create_default_symver)
6465 {
6466 size += sizeof (Elf_External_Verdef);
6467 ++cdefs;
3e3b46e5
PB
6468 }
6469
5a580b3a
AM
6470 for (t = verdefs; t != NULL; t = t->next)
6471 {
6472 struct bfd_elf_version_deps *n;
6473
a6cc6b3b
RO
6474 /* Don't emit base version twice. */
6475 if (t->vernum == 0)
6476 continue;
6477
5a580b3a
AM
6478 size += sizeof (Elf_External_Verdef);
6479 size += sizeof (Elf_External_Verdaux);
6480 ++cdefs;
6481
6482 for (n = t->deps; n != NULL; n = n->next)
6483 size += sizeof (Elf_External_Verdaux);
6484 }
6485
eea6121a 6486 s->size = size;
a50b1753 6487 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6488 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6489 return FALSE;
6490
6491 /* Fill in the version definition section. */
6492
6493 p = s->contents;
6494
6495 def.vd_version = VER_DEF_CURRENT;
6496 def.vd_flags = VER_FLG_BASE;
6497 def.vd_ndx = 1;
6498 def.vd_cnt = 1;
3e3b46e5
PB
6499 if (info->create_default_symver)
6500 {
6501 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6502 def.vd_next = sizeof (Elf_External_Verdef);
6503 }
6504 else
6505 {
6506 def.vd_aux = sizeof (Elf_External_Verdef);
6507 def.vd_next = (sizeof (Elf_External_Verdef)
6508 + sizeof (Elf_External_Verdaux));
6509 }
5a580b3a 6510
ef53be89 6511 if (soname_indx != (size_t) -1)
5a580b3a
AM
6512 {
6513 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6514 soname_indx);
6515 def.vd_hash = bfd_elf_hash (soname);
6516 defaux.vda_name = soname_indx;
3e3b46e5 6517 name = soname;
5a580b3a
AM
6518 }
6519 else
6520 {
ef53be89 6521 size_t indx;
5a580b3a 6522
06084812 6523 name = lbasename (output_bfd->filename);
5a580b3a
AM
6524 def.vd_hash = bfd_elf_hash (name);
6525 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6526 name, FALSE);
ef53be89 6527 if (indx == (size_t) -1)
5a580b3a
AM
6528 return FALSE;
6529 defaux.vda_name = indx;
6530 }
6531 defaux.vda_next = 0;
6532
6533 _bfd_elf_swap_verdef_out (output_bfd, &def,
6534 (Elf_External_Verdef *) p);
6535 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6536 if (info->create_default_symver)
6537 {
6538 /* Add a symbol representing this version. */
6539 bh = NULL;
6540 if (! (_bfd_generic_link_add_one_symbol
6541 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6542 0, NULL, FALSE,
6543 get_elf_backend_data (dynobj)->collect, &bh)))
6544 return FALSE;
6545 h = (struct elf_link_hash_entry *) bh;
6546 h->non_elf = 0;
6547 h->def_regular = 1;
6548 h->type = STT_OBJECT;
6549 h->verinfo.vertree = NULL;
6550
6551 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6552 return FALSE;
6553
6554 /* Create a duplicate of the base version with the same
6555 aux block, but different flags. */
6556 def.vd_flags = 0;
6557 def.vd_ndx = 2;
6558 def.vd_aux = sizeof (Elf_External_Verdef);
6559 if (verdefs)
6560 def.vd_next = (sizeof (Elf_External_Verdef)
6561 + sizeof (Elf_External_Verdaux));
6562 else
6563 def.vd_next = 0;
6564 _bfd_elf_swap_verdef_out (output_bfd, &def,
6565 (Elf_External_Verdef *) p);
6566 p += sizeof (Elf_External_Verdef);
6567 }
5a580b3a
AM
6568 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6569 (Elf_External_Verdaux *) p);
6570 p += sizeof (Elf_External_Verdaux);
6571
6572 for (t = verdefs; t != NULL; t = t->next)
6573 {
6574 unsigned int cdeps;
6575 struct bfd_elf_version_deps *n;
5a580b3a 6576
a6cc6b3b
RO
6577 /* Don't emit the base version twice. */
6578 if (t->vernum == 0)
6579 continue;
6580
5a580b3a
AM
6581 cdeps = 0;
6582 for (n = t->deps; n != NULL; n = n->next)
6583 ++cdeps;
6584
6585 /* Add a symbol representing this version. */
6586 bh = NULL;
6587 if (! (_bfd_generic_link_add_one_symbol
6588 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6589 0, NULL, FALSE,
6590 get_elf_backend_data (dynobj)->collect, &bh)))
6591 return FALSE;
6592 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6593 h->non_elf = 0;
6594 h->def_regular = 1;
5a580b3a
AM
6595 h->type = STT_OBJECT;
6596 h->verinfo.vertree = t;
6597
c152c796 6598 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6599 return FALSE;
6600
6601 def.vd_version = VER_DEF_CURRENT;
6602 def.vd_flags = 0;
6603 if (t->globals.list == NULL
6604 && t->locals.list == NULL
6605 && ! t->used)
6606 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6607 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6608 def.vd_cnt = cdeps + 1;
6609 def.vd_hash = bfd_elf_hash (t->name);
6610 def.vd_aux = sizeof (Elf_External_Verdef);
6611 def.vd_next = 0;
a6cc6b3b
RO
6612
6613 /* If a basever node is next, it *must* be the last node in
6614 the chain, otherwise Verdef construction breaks. */
6615 if (t->next != NULL && t->next->vernum == 0)
6616 BFD_ASSERT (t->next->next == NULL);
6617
6618 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6619 def.vd_next = (sizeof (Elf_External_Verdef)
6620 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6621
6622 _bfd_elf_swap_verdef_out (output_bfd, &def,
6623 (Elf_External_Verdef *) p);
6624 p += sizeof (Elf_External_Verdef);
6625
6626 defaux.vda_name = h->dynstr_index;
6627 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6628 h->dynstr_index);
6629 defaux.vda_next = 0;
6630 if (t->deps != NULL)
6631 defaux.vda_next = sizeof (Elf_External_Verdaux);
6632 t->name_indx = defaux.vda_name;
6633
6634 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6635 (Elf_External_Verdaux *) p);
6636 p += sizeof (Elf_External_Verdaux);
6637
6638 for (n = t->deps; n != NULL; n = n->next)
6639 {
6640 if (n->version_needed == NULL)
6641 {
6642 /* This can happen if there was an error in the
6643 version script. */
6644 defaux.vda_name = 0;
6645 }
6646 else
6647 {
6648 defaux.vda_name = n->version_needed->name_indx;
6649 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6650 defaux.vda_name);
6651 }
6652 if (n->next == NULL)
6653 defaux.vda_next = 0;
6654 else
6655 defaux.vda_next = sizeof (Elf_External_Verdaux);
6656
6657 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6658 (Elf_External_Verdaux *) p);
6659 p += sizeof (Elf_External_Verdaux);
6660 }
6661 }
6662
5a580b3a
AM
6663 elf_tdata (output_bfd)->cverdefs = cdefs;
6664 }
902e9fc7
MR
6665 }
6666
6667 bed = get_elf_backend_data (output_bfd);
6668
6669 if (info->gc_sections && bed->can_gc_sections)
6670 {
6671 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6672
6673 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6674 dynamic symbol table. */
902e9fc7
MR
6675 sweep_info.info = info;
6676 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6677 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6678 &sweep_info);
3d13f3e9
AM
6679 }
6680
6681 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6682 {
6683 asection *s;
6684 struct elf_find_verdep_info sinfo;
6685
6686 /* Work out the size of the version reference section. */
6687
6688 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6689 BFD_ASSERT (s != NULL);
902e9fc7 6690
3d13f3e9
AM
6691 sinfo.info = info;
6692 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6693 if (sinfo.vers == 0)
6694 sinfo.vers = 1;
6695 sinfo.failed = FALSE;
6696
6697 elf_link_hash_traverse (elf_hash_table (info),
6698 _bfd_elf_link_find_version_dependencies,
6699 &sinfo);
6700 if (sinfo.failed)
6701 return FALSE;
6702
6703 if (elf_tdata (output_bfd)->verref == NULL)
6704 s->flags |= SEC_EXCLUDE;
6705 else
6706 {
6707 Elf_Internal_Verneed *vn;
6708 unsigned int size;
6709 unsigned int crefs;
6710 bfd_byte *p;
6711
6712 /* Build the version dependency section. */
6713 size = 0;
6714 crefs = 0;
6715 for (vn = elf_tdata (output_bfd)->verref;
6716 vn != NULL;
6717 vn = vn->vn_nextref)
6718 {
6719 Elf_Internal_Vernaux *a;
6720
6721 size += sizeof (Elf_External_Verneed);
6722 ++crefs;
6723 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6724 size += sizeof (Elf_External_Vernaux);
6725 }
6726
6727 s->size = size;
6728 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6729 if (s->contents == NULL)
6730 return FALSE;
6731
6732 p = s->contents;
6733 for (vn = elf_tdata (output_bfd)->verref;
6734 vn != NULL;
6735 vn = vn->vn_nextref)
6736 {
6737 unsigned int caux;
6738 Elf_Internal_Vernaux *a;
6739 size_t indx;
6740
6741 caux = 0;
6742 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6743 ++caux;
6744
6745 vn->vn_version = VER_NEED_CURRENT;
6746 vn->vn_cnt = caux;
6747 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6748 elf_dt_name (vn->vn_bfd) != NULL
6749 ? elf_dt_name (vn->vn_bfd)
6750 : lbasename (vn->vn_bfd->filename),
6751 FALSE);
6752 if (indx == (size_t) -1)
6753 return FALSE;
6754 vn->vn_file = indx;
6755 vn->vn_aux = sizeof (Elf_External_Verneed);
6756 if (vn->vn_nextref == NULL)
6757 vn->vn_next = 0;
6758 else
6759 vn->vn_next = (sizeof (Elf_External_Verneed)
6760 + caux * sizeof (Elf_External_Vernaux));
6761
6762 _bfd_elf_swap_verneed_out (output_bfd, vn,
6763 (Elf_External_Verneed *) p);
6764 p += sizeof (Elf_External_Verneed);
6765
6766 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6767 {
6768 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6769 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6770 a->vna_nodename, FALSE);
6771 if (indx == (size_t) -1)
6772 return FALSE;
6773 a->vna_name = indx;
6774 if (a->vna_nextptr == NULL)
6775 a->vna_next = 0;
6776 else
6777 a->vna_next = sizeof (Elf_External_Vernaux);
6778
6779 _bfd_elf_swap_vernaux_out (output_bfd, a,
6780 (Elf_External_Vernaux *) p);
6781 p += sizeof (Elf_External_Vernaux);
6782 }
6783 }
6784
6785 elf_tdata (output_bfd)->cverrefs = crefs;
6786 }
902e9fc7
MR
6787 }
6788
6789 /* Any syms created from now on start with -1 in
6790 got.refcount/offset and plt.refcount/offset. */
6791 elf_hash_table (info)->init_got_refcount
6792 = elf_hash_table (info)->init_got_offset;
6793 elf_hash_table (info)->init_plt_refcount
6794 = elf_hash_table (info)->init_plt_offset;
6795
6796 if (bfd_link_relocatable (info)
6797 && !_bfd_elf_size_group_sections (info))
6798 return FALSE;
6799
6800 /* The backend may have to create some sections regardless of whether
6801 we're dynamic or not. */
6802 if (bed->elf_backend_always_size_sections
6803 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6804 return FALSE;
6805
6806 /* Determine any GNU_STACK segment requirements, after the backend
6807 has had a chance to set a default segment size. */
6808 if (info->execstack)
6809 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6810 else if (info->noexecstack)
6811 elf_stack_flags (output_bfd) = PF_R | PF_W;
6812 else
6813 {
6814 bfd *inputobj;
6815 asection *notesec = NULL;
6816 int exec = 0;
6817
6818 for (inputobj = info->input_bfds;
6819 inputobj;
6820 inputobj = inputobj->link.next)
6821 {
6822 asection *s;
6823
6824 if (inputobj->flags
6825 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6826 continue;
57963c05
AM
6827 s = inputobj->sections;
6828 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6829 continue;
6830
902e9fc7
MR
6831 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6832 if (s)
6833 {
6834 if (s->flags & SEC_CODE)
6835 exec = PF_X;
6836 notesec = s;
6837 }
6838 else if (bed->default_execstack)
6839 exec = PF_X;
6840 }
6841 if (notesec || info->stacksize > 0)
6842 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6843 if (notesec && exec && bfd_link_relocatable (info)
6844 && notesec->output_section != bfd_abs_section_ptr)
6845 notesec->output_section->flags |= SEC_CODE;
6846 }
6847
6848 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6849 {
6850 struct elf_info_failed eif;
6851 struct elf_link_hash_entry *h;
6852 asection *dynstr;
6853 asection *s;
6854
6855 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6856 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6857
902e9fc7
MR
6858 if (info->symbolic)
6859 {
6860 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6861 return FALSE;
6862 info->flags |= DF_SYMBOLIC;
6863 }
6864
6865 if (rpath != NULL)
6866 {
6867 size_t indx;
6868 bfd_vma tag;
6869
6870 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6871 TRUE);
6872 if (indx == (size_t) -1)
6873 return FALSE;
6874
6875 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6876 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6877 return FALSE;
6878 }
6879
6880 if (filter_shlib != NULL)
6881 {
6882 size_t indx;
6883
6884 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6885 filter_shlib, TRUE);
6886 if (indx == (size_t) -1
6887 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6888 return FALSE;
6889 }
6890
6891 if (auxiliary_filters != NULL)
6892 {
6893 const char * const *p;
6894
6895 for (p = auxiliary_filters; *p != NULL; p++)
6896 {
6897 size_t indx;
6898
6899 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6900 *p, TRUE);
6901 if (indx == (size_t) -1
6902 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6903 return FALSE;
6904 }
6905 }
6906
6907 if (audit != NULL)
6908 {
6909 size_t indx;
6910
6911 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6912 TRUE);
6913 if (indx == (size_t) -1
6914 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6915 return FALSE;
6916 }
6917
6918 if (depaudit != NULL)
6919 {
6920 size_t indx;
6921
6922 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6923 TRUE);
6924 if (indx == (size_t) -1
6925 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6926 return FALSE;
6927 }
6928
6929 eif.info = info;
6930 eif.failed = FALSE;
6931
6932 /* Find all symbols which were defined in a dynamic object and make
6933 the backend pick a reasonable value for them. */
6934 elf_link_hash_traverse (elf_hash_table (info),
6935 _bfd_elf_adjust_dynamic_symbol,
6936 &eif);
6937 if (eif.failed)
6938 return FALSE;
6939
6940 /* Add some entries to the .dynamic section. We fill in some of the
6941 values later, in bfd_elf_final_link, but we must add the entries
6942 now so that we know the final size of the .dynamic section. */
6943
6944 /* If there are initialization and/or finalization functions to
6945 call then add the corresponding DT_INIT/DT_FINI entries. */
6946 h = (info->init_function
6947 ? elf_link_hash_lookup (elf_hash_table (info),
6948 info->init_function, FALSE,
6949 FALSE, FALSE)
6950 : NULL);
6951 if (h != NULL
6952 && (h->ref_regular
6953 || h->def_regular))
6954 {
6955 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6956 return FALSE;
6957 }
6958 h = (info->fini_function
6959 ? elf_link_hash_lookup (elf_hash_table (info),
6960 info->fini_function, FALSE,
6961 FALSE, FALSE)
6962 : NULL);
6963 if (h != NULL
6964 && (h->ref_regular
6965 || h->def_regular))
6966 {
6967 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6968 return FALSE;
6969 }
6970
6971 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6972 if (s != NULL && s->linker_has_input)
6973 {
6974 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6975 if (! bfd_link_executable (info))
6976 {
6977 bfd *sub;
6978 asection *o;
6979
57963c05
AM
6980 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6981 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6982 && (o = sub->sections) != NULL
6983 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6984 for (o = sub->sections; o != NULL; o = o->next)
6985 if (elf_section_data (o)->this_hdr.sh_type
6986 == SHT_PREINIT_ARRAY)
6987 {
6988 _bfd_error_handler
871b3ab2 6989 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6990 sub);
6991 break;
6992 }
6993
6994 bfd_set_error (bfd_error_nonrepresentable_section);
6995 return FALSE;
6996 }
6997
6998 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6999 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7000 return FALSE;
7001 }
7002 s = bfd_get_section_by_name (output_bfd, ".init_array");
7003 if (s != NULL && s->linker_has_input)
7004 {
7005 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7006 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7007 return FALSE;
7008 }
7009 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7010 if (s != NULL && s->linker_has_input)
7011 {
7012 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7013 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7014 return FALSE;
7015 }
7016
7017 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7018 /* If .dynstr is excluded from the link, we don't want any of
7019 these tags. Strictly, we should be checking each section
7020 individually; This quick check covers for the case where
7021 someone does a /DISCARD/ : { *(*) }. */
7022 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7023 {
7024 bfd_size_type strsize;
7025
7026 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7027 if ((info->emit_hash
7028 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7029 || (info->emit_gnu_hash
f16a9783
MS
7030 && (bed->record_xhash_symbol == NULL
7031 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7032 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7033 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7034 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7035 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7036 bed->s->sizeof_sym))
7037 return FALSE;
7038 }
7039 }
7040
7041 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7042 return FALSE;
7043
7044 /* The backend must work out the sizes of all the other dynamic
7045 sections. */
7046 if (dynobj != NULL
7047 && bed->elf_backend_size_dynamic_sections != NULL
7048 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7049 return FALSE;
7050
7051 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7052 {
902e9fc7
MR
7053 if (elf_tdata (output_bfd)->cverdefs)
7054 {
7055 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7056
7057 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7058 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7059 return FALSE;
7060 }
7061
7062 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7063 {
7064 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7065 return FALSE;
7066 }
7067 else if (info->flags & DF_BIND_NOW)
7068 {
7069 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7070 return FALSE;
7071 }
7072
7073 if (info->flags_1)
7074 {
7075 if (bfd_link_executable (info))
7076 info->flags_1 &= ~ (DF_1_INITFIRST
7077 | DF_1_NODELETE
7078 | DF_1_NOOPEN);
7079 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7080 return FALSE;
7081 }
7082
7083 if (elf_tdata (output_bfd)->cverrefs)
7084 {
7085 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7086
7087 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7088 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7089 return FALSE;
7090 }
5a580b3a 7091
8423293d
AM
7092 if ((elf_tdata (output_bfd)->cverrefs == 0
7093 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7094 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7095 {
902e9fc7
MR
7096 asection *s;
7097
3d4d4302 7098 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7099 s->flags |= SEC_EXCLUDE;
7100 }
7101 }
7102 return TRUE;
7103}
7104
74541ad4
AM
7105/* Find the first non-excluded output section. We'll use its
7106 section symbol for some emitted relocs. */
7107void
7108_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7109{
7110 asection *s;
f26a3287 7111 asection *found = NULL;
74541ad4
AM
7112
7113 for (s = output_bfd->sections; s != NULL; s = s->next)
7114 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7115 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7116 {
f26a3287
AM
7117 found = s;
7118 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7119 break;
74541ad4 7120 }
f26a3287 7121 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7122}
7123
7124/* Find two non-excluded output sections, one for code, one for data.
7125 We'll use their section symbols for some emitted relocs. */
7126void
7127_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7128{
7129 asection *s;
f26a3287 7130 asection *found = NULL;
74541ad4 7131
266b05cf 7132 /* Data first, since setting text_index_section changes
7f923b7f 7133 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7134 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7135 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7136 && !(s->flags & SEC_READONLY)
d00dd7dc 7137 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7138 {
f26a3287
AM
7139 found = s;
7140 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7141 break;
74541ad4 7142 }
f26a3287 7143 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7144
7145 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7146 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7147 && (s->flags & SEC_READONLY)
d00dd7dc 7148 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7149 {
f26a3287 7150 found = s;
74541ad4
AM
7151 break;
7152 }
f26a3287 7153 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7154}
7155
f16a9783
MS
7156#define GNU_HASH_SECTION_NAME(bed) \
7157 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7158
8423293d
AM
7159bfd_boolean
7160bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7161{
74541ad4 7162 const struct elf_backend_data *bed;
23ec1e32 7163 unsigned long section_sym_count;
96d01d93 7164 bfd_size_type dynsymcount = 0;
74541ad4 7165
8423293d
AM
7166 if (!is_elf_hash_table (info->hash))
7167 return TRUE;
7168
74541ad4
AM
7169 bed = get_elf_backend_data (output_bfd);
7170 (*bed->elf_backend_init_index_section) (output_bfd, info);
7171
23ec1e32
MR
7172 /* Assign dynsym indices. In a shared library we generate a section
7173 symbol for each output section, which come first. Next come all
7174 of the back-end allocated local dynamic syms, followed by the rest
7175 of the global symbols.
7176
7177 This is usually not needed for static binaries, however backends
7178 can request to always do it, e.g. the MIPS backend uses dynamic
7179 symbol counts to lay out GOT, which will be produced in the
7180 presence of GOT relocations even in static binaries (holding fixed
7181 data in that case, to satisfy those relocations). */
7182
7183 if (elf_hash_table (info)->dynamic_sections_created
7184 || bed->always_renumber_dynsyms)
7185 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7186 &section_sym_count);
7187
8423293d
AM
7188 if (elf_hash_table (info)->dynamic_sections_created)
7189 {
7190 bfd *dynobj;
8423293d 7191 asection *s;
8423293d
AM
7192 unsigned int dtagcount;
7193
7194 dynobj = elf_hash_table (info)->dynobj;
7195
5a580b3a 7196 /* Work out the size of the symbol version section. */
3d4d4302 7197 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7198 BFD_ASSERT (s != NULL);
d5486c43 7199 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7200 {
eea6121a 7201 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7202 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7203 if (s->contents == NULL)
7204 return FALSE;
7205
7206 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7207 return FALSE;
7208 }
7209
7210 /* Set the size of the .dynsym and .hash sections. We counted
7211 the number of dynamic symbols in elf_link_add_object_symbols.
7212 We will build the contents of .dynsym and .hash when we build
7213 the final symbol table, because until then we do not know the
7214 correct value to give the symbols. We built the .dynstr
7215 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7216 s = elf_hash_table (info)->dynsym;
5a580b3a 7217 BFD_ASSERT (s != NULL);
eea6121a 7218 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7219
d5486c43
L
7220 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7221 if (s->contents == NULL)
7222 return FALSE;
5a580b3a 7223
d5486c43
L
7224 /* The first entry in .dynsym is a dummy symbol. Clear all the
7225 section syms, in case we don't output them all. */
7226 ++section_sym_count;
7227 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7228
fdc90cb4
JJ
7229 elf_hash_table (info)->bucketcount = 0;
7230
5a580b3a
AM
7231 /* Compute the size of the hashing table. As a side effect this
7232 computes the hash values for all the names we export. */
fdc90cb4
JJ
7233 if (info->emit_hash)
7234 {
7235 unsigned long int *hashcodes;
14b1c01e 7236 struct hash_codes_info hashinf;
fdc90cb4
JJ
7237 bfd_size_type amt;
7238 unsigned long int nsyms;
7239 size_t bucketcount;
7240 size_t hash_entry_size;
7241
7242 /* Compute the hash values for all exported symbols. At the same
7243 time store the values in an array so that we could use them for
7244 optimizations. */
7245 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7246 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7247 if (hashcodes == NULL)
7248 return FALSE;
14b1c01e
AM
7249 hashinf.hashcodes = hashcodes;
7250 hashinf.error = FALSE;
5a580b3a 7251
fdc90cb4
JJ
7252 /* Put all hash values in HASHCODES. */
7253 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7254 elf_collect_hash_codes, &hashinf);
7255 if (hashinf.error)
4dd07732
AM
7256 {
7257 free (hashcodes);
7258 return FALSE;
7259 }
5a580b3a 7260
14b1c01e 7261 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7262 bucketcount
7263 = compute_bucket_count (info, hashcodes, nsyms, 0);
7264 free (hashcodes);
7265
4b48e2f6 7266 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7267 return FALSE;
5a580b3a 7268
fdc90cb4
JJ
7269 elf_hash_table (info)->bucketcount = bucketcount;
7270
3d4d4302 7271 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7272 BFD_ASSERT (s != NULL);
7273 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7274 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7275 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7276 if (s->contents == NULL)
7277 return FALSE;
7278
7279 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7280 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7281 s->contents + hash_entry_size);
7282 }
7283
7284 if (info->emit_gnu_hash)
7285 {
7286 size_t i, cnt;
7287 unsigned char *contents;
7288 struct collect_gnu_hash_codes cinfo;
7289 bfd_size_type amt;
7290 size_t bucketcount;
7291
7292 memset (&cinfo, 0, sizeof (cinfo));
7293
7294 /* Compute the hash values for all exported symbols. At the same
7295 time store the values in an array so that we could use them for
7296 optimizations. */
7297 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7298 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7299 if (cinfo.hashcodes == NULL)
7300 return FALSE;
7301
7302 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7303 cinfo.min_dynindx = -1;
7304 cinfo.output_bfd = output_bfd;
7305 cinfo.bed = bed;
7306
7307 /* Put all hash values in HASHCODES. */
7308 elf_link_hash_traverse (elf_hash_table (info),
7309 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7310 if (cinfo.error)
4dd07732
AM
7311 {
7312 free (cinfo.hashcodes);
7313 return FALSE;
7314 }
fdc90cb4
JJ
7315
7316 bucketcount
7317 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7318
7319 if (bucketcount == 0)
7320 {
7321 free (cinfo.hashcodes);
7322 return FALSE;
7323 }
7324
f16a9783 7325 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7326 BFD_ASSERT (s != NULL);
7327
7328 if (cinfo.nsyms == 0)
7329 {
f16a9783 7330 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7331 BFD_ASSERT (cinfo.min_dynindx == -1);
7332 free (cinfo.hashcodes);
7333 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7334 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7335 if (contents == NULL)
7336 return FALSE;
7337 s->contents = contents;
7338 /* 1 empty bucket. */
7339 bfd_put_32 (output_bfd, 1, contents);
7340 /* SYMIDX above the special symbol 0. */
7341 bfd_put_32 (output_bfd, 1, contents + 4);
7342 /* Just one word for bitmask. */
7343 bfd_put_32 (output_bfd, 1, contents + 8);
7344 /* Only hash fn bloom filter. */
7345 bfd_put_32 (output_bfd, 0, contents + 12);
7346 /* No hashes are valid - empty bitmask. */
7347 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7348 /* No hashes in the only bucket. */
7349 bfd_put_32 (output_bfd, 0,
7350 contents + 16 + bed->s->arch_size / 8);
7351 }
7352 else
7353 {
9e6619e2 7354 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7355 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7356
9e6619e2
AM
7357 x = cinfo.nsyms;
7358 maskbitslog2 = 1;
7359 while ((x >>= 1) != 0)
7360 ++maskbitslog2;
fdc90cb4
JJ
7361 if (maskbitslog2 < 3)
7362 maskbitslog2 = 5;
7363 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7364 maskbitslog2 = maskbitslog2 + 3;
7365 else
7366 maskbitslog2 = maskbitslog2 + 2;
7367 if (bed->s->arch_size == 64)
7368 {
7369 if (maskbitslog2 == 5)
7370 maskbitslog2 = 6;
7371 cinfo.shift1 = 6;
7372 }
7373 else
7374 cinfo.shift1 = 5;
7375 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7376 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7377 cinfo.maskbits = 1 << maskbitslog2;
7378 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7379 amt = bucketcount * sizeof (unsigned long int) * 2;
7380 amt += maskwords * sizeof (bfd_vma);
a50b1753 7381 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7382 if (cinfo.bitmask == NULL)
7383 {
7384 free (cinfo.hashcodes);
7385 return FALSE;
7386 }
7387
a50b1753 7388 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7389 cinfo.indx = cinfo.counts + bucketcount;
7390 cinfo.symindx = dynsymcount - cinfo.nsyms;
7391 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7392
7393 /* Determine how often each hash bucket is used. */
7394 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7395 for (i = 0; i < cinfo.nsyms; ++i)
7396 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7397
7398 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7399 if (cinfo.counts[i] != 0)
7400 {
7401 cinfo.indx[i] = cnt;
7402 cnt += cinfo.counts[i];
7403 }
7404 BFD_ASSERT (cnt == dynsymcount);
7405 cinfo.bucketcount = bucketcount;
7406 cinfo.local_indx = cinfo.min_dynindx;
7407
7408 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7409 s->size += cinfo.maskbits / 8;
f16a9783
MS
7410 if (bed->record_xhash_symbol != NULL)
7411 s->size += cinfo.nsyms * 4;
a50b1753 7412 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7413 if (contents == NULL)
7414 {
7415 free (cinfo.bitmask);
7416 free (cinfo.hashcodes);
7417 return FALSE;
7418 }
7419
7420 s->contents = contents;
7421 bfd_put_32 (output_bfd, bucketcount, contents);
7422 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7423 bfd_put_32 (output_bfd, maskwords, contents + 8);
7424 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7425 contents += 16 + cinfo.maskbits / 8;
7426
7427 for (i = 0; i < bucketcount; ++i)
7428 {
7429 if (cinfo.counts[i] == 0)
7430 bfd_put_32 (output_bfd, 0, contents);
7431 else
7432 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7433 contents += 4;
7434 }
7435
7436 cinfo.contents = contents;
7437
f16a9783
MS
7438 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7439 /* Renumber dynamic symbols, if populating .gnu.hash section.
7440 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7441 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7442 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7443
7444 contents = s->contents + 16;
7445 for (i = 0; i < maskwords; ++i)
7446 {
7447 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7448 contents);
7449 contents += bed->s->arch_size / 8;
7450 }
7451
7452 free (cinfo.bitmask);
7453 free (cinfo.hashcodes);
7454 }
7455 }
5a580b3a 7456
3d4d4302 7457 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7458 BFD_ASSERT (s != NULL);
7459
4ad4eba5 7460 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7461
eea6121a 7462 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7463
7464 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7465 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7466 return FALSE;
7467 }
7468
7469 return TRUE;
7470}
4d269e42 7471\f
4d269e42
AM
7472/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7473
7474static void
7475merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7476 asection *sec)
7477{
dbaa2011
AM
7478 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7479 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7480}
7481
7482/* Finish SHF_MERGE section merging. */
7483
7484bfd_boolean
630993ec 7485_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7486{
7487 bfd *ibfd;
7488 asection *sec;
7489
7490 if (!is_elf_hash_table (info->hash))
7491 return FALSE;
7492
c72f2fb2 7493 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7494 if ((ibfd->flags & DYNAMIC) == 0
7495 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7496 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7497 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7498 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7499 if ((sec->flags & SEC_MERGE) != 0
7500 && !bfd_is_abs_section (sec->output_section))
7501 {
7502 struct bfd_elf_section_data *secdata;
7503
7504 secdata = elf_section_data (sec);
630993ec 7505 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7506 &elf_hash_table (info)->merge_info,
7507 sec, &secdata->sec_info))
7508 return FALSE;
7509 else if (secdata->sec_info)
dbaa2011 7510 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7511 }
7512
7513 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7514 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7515 merge_sections_remove_hook);
7516 return TRUE;
7517}
7518
7519/* Create an entry in an ELF linker hash table. */
7520
7521struct bfd_hash_entry *
7522_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7523 struct bfd_hash_table *table,
7524 const char *string)
7525{
7526 /* Allocate the structure if it has not already been allocated by a
7527 subclass. */
7528 if (entry == NULL)
7529 {
a50b1753 7530 entry = (struct bfd_hash_entry *)
ca4be51c 7531 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7532 if (entry == NULL)
7533 return entry;
7534 }
7535
7536 /* Call the allocation method of the superclass. */
7537 entry = _bfd_link_hash_newfunc (entry, table, string);
7538 if (entry != NULL)
7539 {
7540 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7541 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7542
7543 /* Set local fields. */
7544 ret->indx = -1;
7545 ret->dynindx = -1;
7546 ret->got = htab->init_got_refcount;
7547 ret->plt = htab->init_plt_refcount;
7548 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7549 - offsetof (struct elf_link_hash_entry, size)));
7550 /* Assume that we have been called by a non-ELF symbol reader.
7551 This flag is then reset by the code which reads an ELF input
7552 file. This ensures that a symbol created by a non-ELF symbol
7553 reader will have the flag set correctly. */
7554 ret->non_elf = 1;
7555 }
7556
7557 return entry;
7558}
7559
7560/* Copy data from an indirect symbol to its direct symbol, hiding the
7561 old indirect symbol. Also used for copying flags to a weakdef. */
7562
7563void
7564_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7565 struct elf_link_hash_entry *dir,
7566 struct elf_link_hash_entry *ind)
7567{
7568 struct elf_link_hash_table *htab;
7569
7570 /* Copy down any references that we may have already seen to the
e81830c5 7571 symbol which just became indirect. */
4d269e42 7572
422f1182 7573 if (dir->versioned != versioned_hidden)
e81830c5
AM
7574 dir->ref_dynamic |= ind->ref_dynamic;
7575 dir->ref_regular |= ind->ref_regular;
7576 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7577 dir->non_got_ref |= ind->non_got_ref;
7578 dir->needs_plt |= ind->needs_plt;
7579 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7580
7581 if (ind->root.type != bfd_link_hash_indirect)
7582 return;
7583
7584 /* Copy over the global and procedure linkage table refcount entries.
7585 These may have been already set up by a check_relocs routine. */
7586 htab = elf_hash_table (info);
7587 if (ind->got.refcount > htab->init_got_refcount.refcount)
7588 {
7589 if (dir->got.refcount < 0)
7590 dir->got.refcount = 0;
7591 dir->got.refcount += ind->got.refcount;
7592 ind->got.refcount = htab->init_got_refcount.refcount;
7593 }
7594
7595 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7596 {
7597 if (dir->plt.refcount < 0)
7598 dir->plt.refcount = 0;
7599 dir->plt.refcount += ind->plt.refcount;
7600 ind->plt.refcount = htab->init_plt_refcount.refcount;
7601 }
7602
7603 if (ind->dynindx != -1)
7604 {
7605 if (dir->dynindx != -1)
7606 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7607 dir->dynindx = ind->dynindx;
7608 dir->dynstr_index = ind->dynstr_index;
7609 ind->dynindx = -1;
7610 ind->dynstr_index = 0;
7611 }
7612}
7613
7614void
7615_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7616 struct elf_link_hash_entry *h,
7617 bfd_boolean force_local)
7618{
3aa14d16
L
7619 /* STT_GNU_IFUNC symbol must go through PLT. */
7620 if (h->type != STT_GNU_IFUNC)
7621 {
7622 h->plt = elf_hash_table (info)->init_plt_offset;
7623 h->needs_plt = 0;
7624 }
4d269e42
AM
7625 if (force_local)
7626 {
7627 h->forced_local = 1;
7628 if (h->dynindx != -1)
7629 {
4d269e42
AM
7630 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7631 h->dynstr_index);
641338d8
AM
7632 h->dynindx = -1;
7633 h->dynstr_index = 0;
4d269e42
AM
7634 }
7635 }
7636}
7637
34a87bb0
L
7638/* Hide a symbol. */
7639
7640void
7641_bfd_elf_link_hide_symbol (bfd *output_bfd,
7642 struct bfd_link_info *info,
7643 struct bfd_link_hash_entry *h)
7644{
7645 if (is_elf_hash_table (info->hash))
7646 {
7647 const struct elf_backend_data *bed
7648 = get_elf_backend_data (output_bfd);
7649 struct elf_link_hash_entry *eh
7650 = (struct elf_link_hash_entry *) h;
7651 bed->elf_backend_hide_symbol (info, eh, TRUE);
7652 eh->def_dynamic = 0;
7653 eh->ref_dynamic = 0;
7654 eh->dynamic_def = 0;
7655 }
7656}
7657
7bf52ea2
AM
7658/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7659 caller. */
4d269e42
AM
7660
7661bfd_boolean
7662_bfd_elf_link_hash_table_init
7663 (struct elf_link_hash_table *table,
7664 bfd *abfd,
7665 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7666 struct bfd_hash_table *,
7667 const char *),
4dfe6ac6
NC
7668 unsigned int entsize,
7669 enum elf_target_id target_id)
4d269e42
AM
7670{
7671 bfd_boolean ret;
7672 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7673
4d269e42
AM
7674 table->init_got_refcount.refcount = can_refcount - 1;
7675 table->init_plt_refcount.refcount = can_refcount - 1;
7676 table->init_got_offset.offset = -(bfd_vma) 1;
7677 table->init_plt_offset.offset = -(bfd_vma) 1;
7678 /* The first dynamic symbol is a dummy. */
7679 table->dynsymcount = 1;
7680
7681 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7682
4d269e42 7683 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7684 table->hash_table_id = target_id;
4d269e42
AM
7685
7686 return ret;
7687}
7688
7689/* Create an ELF linker hash table. */
7690
7691struct bfd_link_hash_table *
7692_bfd_elf_link_hash_table_create (bfd *abfd)
7693{
7694 struct elf_link_hash_table *ret;
7695 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7696
7bf52ea2 7697 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7698 if (ret == NULL)
7699 return NULL;
7700
7701 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7702 sizeof (struct elf_link_hash_entry),
7703 GENERIC_ELF_DATA))
4d269e42
AM
7704 {
7705 free (ret);
7706 return NULL;
7707 }
d495ab0d 7708 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7709
7710 return &ret->root;
7711}
7712
9f7c3e5e
AM
7713/* Destroy an ELF linker hash table. */
7714
7715void
d495ab0d 7716_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7717{
d495ab0d
AM
7718 struct elf_link_hash_table *htab;
7719
7720 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7721 if (htab->dynstr != NULL)
7722 _bfd_elf_strtab_free (htab->dynstr);
7723 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7724 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7725}
7726
4d269e42
AM
7727/* This is a hook for the ELF emulation code in the generic linker to
7728 tell the backend linker what file name to use for the DT_NEEDED
7729 entry for a dynamic object. */
7730
7731void
7732bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7733{
7734 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7735 && bfd_get_format (abfd) == bfd_object)
7736 elf_dt_name (abfd) = name;
7737}
7738
7739int
7740bfd_elf_get_dyn_lib_class (bfd *abfd)
7741{
7742 int lib_class;
7743 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7744 && bfd_get_format (abfd) == bfd_object)
7745 lib_class = elf_dyn_lib_class (abfd);
7746 else
7747 lib_class = 0;
7748 return lib_class;
7749}
7750
7751void
7752bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7753{
7754 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7755 && bfd_get_format (abfd) == bfd_object)
7756 elf_dyn_lib_class (abfd) = lib_class;
7757}
7758
7759/* Get the list of DT_NEEDED entries for a link. This is a hook for
7760 the linker ELF emulation code. */
7761
7762struct bfd_link_needed_list *
7763bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7764 struct bfd_link_info *info)
7765{
7766 if (! is_elf_hash_table (info->hash))
7767 return NULL;
7768 return elf_hash_table (info)->needed;
7769}
7770
7771/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7772 hook for the linker ELF emulation code. */
7773
7774struct bfd_link_needed_list *
7775bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7776 struct bfd_link_info *info)
7777{
7778 if (! is_elf_hash_table (info->hash))
7779 return NULL;
7780 return elf_hash_table (info)->runpath;
7781}
7782
7783/* Get the name actually used for a dynamic object for a link. This
7784 is the SONAME entry if there is one. Otherwise, it is the string
7785 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7786
7787const char *
7788bfd_elf_get_dt_soname (bfd *abfd)
7789{
7790 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7791 && bfd_get_format (abfd) == bfd_object)
7792 return elf_dt_name (abfd);
7793 return NULL;
7794}
7795
7796/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7797 the ELF linker emulation code. */
7798
7799bfd_boolean
7800bfd_elf_get_bfd_needed_list (bfd *abfd,
7801 struct bfd_link_needed_list **pneeded)
7802{
7803 asection *s;
7804 bfd_byte *dynbuf = NULL;
cb33740c 7805 unsigned int elfsec;
4d269e42
AM
7806 unsigned long shlink;
7807 bfd_byte *extdyn, *extdynend;
7808 size_t extdynsize;
7809 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7810
7811 *pneeded = NULL;
7812
7813 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7814 || bfd_get_format (abfd) != bfd_object)
7815 return TRUE;
7816
7817 s = bfd_get_section_by_name (abfd, ".dynamic");
7818 if (s == NULL || s->size == 0)
7819 return TRUE;
7820
7821 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7822 goto error_return;
7823
7824 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7825 if (elfsec == SHN_BAD)
4d269e42
AM
7826 goto error_return;
7827
7828 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7829
4d269e42
AM
7830 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7831 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7832
7833 extdyn = dynbuf;
7834 extdynend = extdyn + s->size;
7835 for (; extdyn < extdynend; extdyn += extdynsize)
7836 {
7837 Elf_Internal_Dyn dyn;
7838
7839 (*swap_dyn_in) (abfd, extdyn, &dyn);
7840
7841 if (dyn.d_tag == DT_NULL)
7842 break;
7843
7844 if (dyn.d_tag == DT_NEEDED)
7845 {
7846 const char *string;
7847 struct bfd_link_needed_list *l;
7848 unsigned int tagv = dyn.d_un.d_val;
7849 bfd_size_type amt;
7850
7851 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7852 if (string == NULL)
7853 goto error_return;
7854
7855 amt = sizeof *l;
a50b1753 7856 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7857 if (l == NULL)
7858 goto error_return;
7859
7860 l->by = abfd;
7861 l->name = string;
7862 l->next = *pneeded;
7863 *pneeded = l;
7864 }
7865 }
7866
7867 free (dynbuf);
7868
7869 return TRUE;
7870
7871 error_return:
7872 if (dynbuf != NULL)
7873 free (dynbuf);
7874 return FALSE;
7875}
7876
7877struct elf_symbuf_symbol
7878{
7879 unsigned long st_name; /* Symbol name, index in string tbl */
7880 unsigned char st_info; /* Type and binding attributes */
7881 unsigned char st_other; /* Visibilty, and target specific */
7882};
7883
7884struct elf_symbuf_head
7885{
7886 struct elf_symbuf_symbol *ssym;
ef53be89 7887 size_t count;
4d269e42
AM
7888 unsigned int st_shndx;
7889};
7890
7891struct elf_symbol
7892{
7893 union
7894 {
7895 Elf_Internal_Sym *isym;
7896 struct elf_symbuf_symbol *ssym;
dcea6a95 7897 void *p;
4d269e42
AM
7898 } u;
7899 const char *name;
7900};
7901
7902/* Sort references to symbols by ascending section number. */
7903
7904static int
7905elf_sort_elf_symbol (const void *arg1, const void *arg2)
7906{
7907 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7908 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7909
dcea6a95
AM
7910 if (s1->st_shndx != s2->st_shndx)
7911 return s1->st_shndx > s2->st_shndx ? 1 : -1;
7912 /* Final sort by the address of the sym in the symbuf ensures
7913 a stable sort. */
7914 if (s1 != s2)
7915 return s1 > s2 ? 1 : -1;
7916 return 0;
4d269e42
AM
7917}
7918
7919static int
7920elf_sym_name_compare (const void *arg1, const void *arg2)
7921{
7922 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7923 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
7924 int ret = strcmp (s1->name, s2->name);
7925 if (ret != 0)
7926 return ret;
7927 if (s1->u.p != s2->u.p)
7928 return s1->u.p > s2->u.p ? 1 : -1;
7929 return 0;
4d269e42
AM
7930}
7931
7932static struct elf_symbuf_head *
ef53be89 7933elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7934{
14b1c01e 7935 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7936 struct elf_symbuf_symbol *ssym;
7937 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7938 size_t i, shndx_count, total_size;
4d269e42 7939
a50b1753 7940 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7941 if (indbuf == NULL)
7942 return NULL;
7943
7944 for (ind = indbuf, i = 0; i < symcount; i++)
7945 if (isymbuf[i].st_shndx != SHN_UNDEF)
7946 *ind++ = &isymbuf[i];
7947 indbufend = ind;
7948
7949 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7950 elf_sort_elf_symbol);
7951
7952 shndx_count = 0;
7953 if (indbufend > indbuf)
7954 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7955 if (ind[0]->st_shndx != ind[1]->st_shndx)
7956 shndx_count++;
7957
3ae181ee
L
7958 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7959 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7960 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7961 if (ssymbuf == NULL)
7962 {
7963 free (indbuf);
7964 return NULL;
7965 }
7966
3ae181ee 7967 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7968 ssymbuf->ssym = NULL;
7969 ssymbuf->count = shndx_count;
7970 ssymbuf->st_shndx = 0;
7971 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7972 {
7973 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7974 {
7975 ssymhead++;
7976 ssymhead->ssym = ssym;
7977 ssymhead->count = 0;
7978 ssymhead->st_shndx = (*ind)->st_shndx;
7979 }
7980 ssym->st_name = (*ind)->st_name;
7981 ssym->st_info = (*ind)->st_info;
7982 ssym->st_other = (*ind)->st_other;
7983 ssymhead->count++;
7984 }
ef53be89 7985 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7986 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7987 == total_size));
4d269e42
AM
7988
7989 free (indbuf);
7990 return ssymbuf;
7991}
7992
7993/* Check if 2 sections define the same set of local and global
7994 symbols. */
7995
8f317e31 7996static bfd_boolean
4d269e42
AM
7997bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7998 struct bfd_link_info *info)
7999{
8000 bfd *bfd1, *bfd2;
8001 const struct elf_backend_data *bed1, *bed2;
8002 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8003 size_t symcount1, symcount2;
4d269e42
AM
8004 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8005 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8006 Elf_Internal_Sym *isym, *isymend;
8007 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8008 size_t count1, count2, i;
cb33740c 8009 unsigned int shndx1, shndx2;
4d269e42
AM
8010 bfd_boolean result;
8011
8012 bfd1 = sec1->owner;
8013 bfd2 = sec2->owner;
8014
4d269e42
AM
8015 /* Both sections have to be in ELF. */
8016 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8017 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8018 return FALSE;
8019
8020 if (elf_section_type (sec1) != elf_section_type (sec2))
8021 return FALSE;
8022
4d269e42
AM
8023 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8024 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8025 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8026 return FALSE;
8027
8028 bed1 = get_elf_backend_data (bfd1);
8029 bed2 = get_elf_backend_data (bfd2);
8030 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8031 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8032 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8033 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8034
8035 if (symcount1 == 0 || symcount2 == 0)
8036 return FALSE;
8037
8038 result = FALSE;
8039 isymbuf1 = NULL;
8040 isymbuf2 = NULL;
a50b1753
NC
8041 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8042 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8043
8044 if (ssymbuf1 == NULL)
8045 {
8046 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8047 NULL, NULL, NULL);
8048 if (isymbuf1 == NULL)
8049 goto done;
8050
8051 if (!info->reduce_memory_overheads)
dcea6a95
AM
8052 {
8053 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8054 elf_tdata (bfd1)->symbuf = ssymbuf1;
8055 }
4d269e42
AM
8056 }
8057
8058 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8059 {
8060 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8061 NULL, NULL, NULL);
8062 if (isymbuf2 == NULL)
8063 goto done;
8064
8065 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8066 {
8067 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8068 elf_tdata (bfd2)->symbuf = ssymbuf2;
8069 }
4d269e42
AM
8070 }
8071
8072 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8073 {
8074 /* Optimized faster version. */
ef53be89 8075 size_t lo, hi, mid;
4d269e42
AM
8076 struct elf_symbol *symp;
8077 struct elf_symbuf_symbol *ssym, *ssymend;
8078
8079 lo = 0;
8080 hi = ssymbuf1->count;
8081 ssymbuf1++;
8082 count1 = 0;
8083 while (lo < hi)
8084 {
8085 mid = (lo + hi) / 2;
cb33740c 8086 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8087 hi = mid;
cb33740c 8088 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8089 lo = mid + 1;
8090 else
8091 {
8092 count1 = ssymbuf1[mid].count;
8093 ssymbuf1 += mid;
8094 break;
8095 }
8096 }
8097
8098 lo = 0;
8099 hi = ssymbuf2->count;
8100 ssymbuf2++;
8101 count2 = 0;
8102 while (lo < hi)
8103 {
8104 mid = (lo + hi) / 2;
cb33740c 8105 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8106 hi = mid;
cb33740c 8107 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8108 lo = mid + 1;
8109 else
8110 {
8111 count2 = ssymbuf2[mid].count;
8112 ssymbuf2 += mid;
8113 break;
8114 }
8115 }
8116
8117 if (count1 == 0 || count2 == 0 || count1 != count2)
8118 goto done;
8119
ca4be51c
AM
8120 symtable1
8121 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8122 symtable2
8123 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8124 if (symtable1 == NULL || symtable2 == NULL)
8125 goto done;
8126
8127 symp = symtable1;
8128 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8129 ssym < ssymend; ssym++, symp++)
8130 {
8131 symp->u.ssym = ssym;
8132 symp->name = bfd_elf_string_from_elf_section (bfd1,
8133 hdr1->sh_link,
8134 ssym->st_name);
8135 }
8136
8137 symp = symtable2;
8138 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8139 ssym < ssymend; ssym++, symp++)
8140 {
8141 symp->u.ssym = ssym;
8142 symp->name = bfd_elf_string_from_elf_section (bfd2,
8143 hdr2->sh_link,
8144 ssym->st_name);
8145 }
8146
8147 /* Sort symbol by name. */
8148 qsort (symtable1, count1, sizeof (struct elf_symbol),
8149 elf_sym_name_compare);
8150 qsort (symtable2, count1, sizeof (struct elf_symbol),
8151 elf_sym_name_compare);
8152
8153 for (i = 0; i < count1; i++)
8154 /* Two symbols must have the same binding, type and name. */
8155 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8156 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8157 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8158 goto done;
8159
8160 result = TRUE;
8161 goto done;
8162 }
8163
a50b1753
NC
8164 symtable1 = (struct elf_symbol *)
8165 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8166 symtable2 = (struct elf_symbol *)
8167 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8168 if (symtable1 == NULL || symtable2 == NULL)
8169 goto done;
8170
8171 /* Count definitions in the section. */
8172 count1 = 0;
8173 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8174 if (isym->st_shndx == shndx1)
4d269e42
AM
8175 symtable1[count1++].u.isym = isym;
8176
8177 count2 = 0;
8178 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8179 if (isym->st_shndx == shndx2)
4d269e42
AM
8180 symtable2[count2++].u.isym = isym;
8181
8182 if (count1 == 0 || count2 == 0 || count1 != count2)
8183 goto done;
8184
8185 for (i = 0; i < count1; i++)
8186 symtable1[i].name
8187 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8188 symtable1[i].u.isym->st_name);
8189
8190 for (i = 0; i < count2; i++)
8191 symtable2[i].name
8192 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8193 symtable2[i].u.isym->st_name);
8194
8195 /* Sort symbol by name. */
8196 qsort (symtable1, count1, sizeof (struct elf_symbol),
8197 elf_sym_name_compare);
8198 qsort (symtable2, count1, sizeof (struct elf_symbol),
8199 elf_sym_name_compare);
8200
8201 for (i = 0; i < count1; i++)
8202 /* Two symbols must have the same binding, type and name. */
8203 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8204 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8205 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8206 goto done;
8207
8208 result = TRUE;
8209
8210done:
8211 if (symtable1)
8212 free (symtable1);
8213 if (symtable2)
8214 free (symtable2);
8215 if (isymbuf1)
8216 free (isymbuf1);
8217 if (isymbuf2)
8218 free (isymbuf2);
8219
8220 return result;
8221}
8222
8223/* Return TRUE if 2 section types are compatible. */
8224
8225bfd_boolean
8226_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8227 bfd *bbfd, const asection *bsec)
8228{
8229 if (asec == NULL
8230 || bsec == NULL
8231 || abfd->xvec->flavour != bfd_target_elf_flavour
8232 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8233 return TRUE;
8234
8235 return elf_section_type (asec) == elf_section_type (bsec);
8236}
8237\f
c152c796
AM
8238/* Final phase of ELF linker. */
8239
8240/* A structure we use to avoid passing large numbers of arguments. */
8241
8242struct elf_final_link_info
8243{
8244 /* General link information. */
8245 struct bfd_link_info *info;
8246 /* Output BFD. */
8247 bfd *output_bfd;
8248 /* Symbol string table. */
ef10c3ac 8249 struct elf_strtab_hash *symstrtab;
c152c796
AM
8250 /* .hash section. */
8251 asection *hash_sec;
8252 /* symbol version section (.gnu.version). */
8253 asection *symver_sec;
8254 /* Buffer large enough to hold contents of any section. */
8255 bfd_byte *contents;
8256 /* Buffer large enough to hold external relocs of any section. */
8257 void *external_relocs;
8258 /* Buffer large enough to hold internal relocs of any section. */
8259 Elf_Internal_Rela *internal_relocs;
8260 /* Buffer large enough to hold external local symbols of any input
8261 BFD. */
8262 bfd_byte *external_syms;
8263 /* And a buffer for symbol section indices. */
8264 Elf_External_Sym_Shndx *locsym_shndx;
8265 /* Buffer large enough to hold internal local symbols of any input
8266 BFD. */
8267 Elf_Internal_Sym *internal_syms;
8268 /* Array large enough to hold a symbol index for each local symbol
8269 of any input BFD. */
8270 long *indices;
8271 /* Array large enough to hold a section pointer for each local
8272 symbol of any input BFD. */
8273 asection **sections;
ef10c3ac 8274 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8275 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8276 /* Number of STT_FILE syms seen. */
8277 size_t filesym_count;
c152c796
AM
8278};
8279
8280/* This struct is used to pass information to elf_link_output_extsym. */
8281
8282struct elf_outext_info
8283{
8284 bfd_boolean failed;
8285 bfd_boolean localsyms;
34a79995 8286 bfd_boolean file_sym_done;
8b127cbc 8287 struct elf_final_link_info *flinfo;
c152c796
AM
8288};
8289
d9352518
DB
8290
8291/* Support for evaluating a complex relocation.
8292
8293 Complex relocations are generalized, self-describing relocations. The
8294 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8295 relocations themselves.
d9352518
DB
8296
8297 The relocations are use a reserved elf-wide relocation type code (R_RELC
8298 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8299 information (start bit, end bit, word width, etc) into the addend. This
8300 information is extracted from CGEN-generated operand tables within gas.
8301
8302 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8303 internal) representing prefix-notation expressions, including but not
8304 limited to those sorts of expressions normally encoded as addends in the
8305 addend field. The symbol mangling format is:
8306
8307 <node> := <literal>
07d6d2b8
AM
8308 | <unary-operator> ':' <node>
8309 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8310 ;
8311
8312 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8313 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8314 | '#' <hexdigits>
8315 ;
8316
8317 <binary-operator> := as in C
8318 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8319
8320static void
a0c8462f
AM
8321set_symbol_value (bfd *bfd_with_globals,
8322 Elf_Internal_Sym *isymbuf,
8323 size_t locsymcount,
8324 size_t symidx,
8325 bfd_vma val)
d9352518 8326{
8977835c
AM
8327 struct elf_link_hash_entry **sym_hashes;
8328 struct elf_link_hash_entry *h;
8329 size_t extsymoff = locsymcount;
d9352518 8330
8977835c 8331 if (symidx < locsymcount)
d9352518 8332 {
8977835c
AM
8333 Elf_Internal_Sym *sym;
8334
8335 sym = isymbuf + symidx;
8336 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8337 {
8338 /* It is a local symbol: move it to the
8339 "absolute" section and give it a value. */
8340 sym->st_shndx = SHN_ABS;
8341 sym->st_value = val;
8342 return;
8343 }
8344 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8345 extsymoff = 0;
d9352518 8346 }
8977835c
AM
8347
8348 /* It is a global symbol: set its link type
8349 to "defined" and give it a value. */
8350
8351 sym_hashes = elf_sym_hashes (bfd_with_globals);
8352 h = sym_hashes [symidx - extsymoff];
8353 while (h->root.type == bfd_link_hash_indirect
8354 || h->root.type == bfd_link_hash_warning)
8355 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8356 h->root.type = bfd_link_hash_defined;
8357 h->root.u.def.value = val;
8358 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8359}
8360
a0c8462f
AM
8361static bfd_boolean
8362resolve_symbol (const char *name,
8363 bfd *input_bfd,
8b127cbc 8364 struct elf_final_link_info *flinfo,
a0c8462f
AM
8365 bfd_vma *result,
8366 Elf_Internal_Sym *isymbuf,
8367 size_t locsymcount)
d9352518 8368{
a0c8462f
AM
8369 Elf_Internal_Sym *sym;
8370 struct bfd_link_hash_entry *global_entry;
8371 const char *candidate = NULL;
8372 Elf_Internal_Shdr *symtab_hdr;
8373 size_t i;
8374
d9352518
DB
8375 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8376
8377 for (i = 0; i < locsymcount; ++ i)
8378 {
8977835c 8379 sym = isymbuf + i;
d9352518
DB
8380
8381 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8382 continue;
8383
8384 candidate = bfd_elf_string_from_elf_section (input_bfd,
8385 symtab_hdr->sh_link,
8386 sym->st_name);
8387#ifdef DEBUG
0f02bbd9
AM
8388 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8389 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8390#endif
8391 if (candidate && strcmp (candidate, name) == 0)
8392 {
8b127cbc 8393 asection *sec = flinfo->sections [i];
d9352518 8394
0f02bbd9
AM
8395 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8396 *result += sec->output_offset + sec->output_section->vma;
d9352518 8397#ifdef DEBUG
0f02bbd9
AM
8398 printf ("Found symbol with value %8.8lx\n",
8399 (unsigned long) *result);
d9352518
DB
8400#endif
8401 return TRUE;
8402 }
8403 }
8404
8405 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8406 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8407 FALSE, FALSE, TRUE);
d9352518
DB
8408 if (!global_entry)
8409 return FALSE;
a0c8462f 8410
d9352518
DB
8411 if (global_entry->type == bfd_link_hash_defined
8412 || global_entry->type == bfd_link_hash_defweak)
8413 {
a0c8462f
AM
8414 *result = (global_entry->u.def.value
8415 + global_entry->u.def.section->output_section->vma
8416 + global_entry->u.def.section->output_offset);
d9352518 8417#ifdef DEBUG
0f02bbd9
AM
8418 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8419 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8420#endif
8421 return TRUE;
a0c8462f 8422 }
d9352518 8423
d9352518
DB
8424 return FALSE;
8425}
8426
37b01f6a
DG
8427/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8428 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8429 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8430
d9352518 8431static bfd_boolean
a0c8462f
AM
8432resolve_section (const char *name,
8433 asection *sections,
37b01f6a
DG
8434 bfd_vma *result,
8435 bfd * abfd)
d9352518 8436{
a0c8462f
AM
8437 asection *curr;
8438 unsigned int len;
d9352518 8439
a0c8462f 8440 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8441 if (strcmp (curr->name, name) == 0)
8442 {
8443 *result = curr->vma;
8444 return TRUE;
8445 }
8446
8447 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8448 /* FIXME: This could be coded more efficiently... */
a0c8462f 8449 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8450 {
8451 len = strlen (curr->name);
a0c8462f 8452 if (len > strlen (name))
d9352518
DB
8453 continue;
8454
8455 if (strncmp (curr->name, name, len) == 0)
8456 {
8457 if (strncmp (".end", name + len, 4) == 0)
8458 {
61826503 8459 *result = (curr->vma
bb294208 8460 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8461 return TRUE;
8462 }
8463
8464 /* Insert more pseudo-section names here, if you like. */
8465 }
8466 }
a0c8462f 8467
d9352518
DB
8468 return FALSE;
8469}
8470
8471static void
a0c8462f 8472undefined_reference (const char *reftype, const char *name)
d9352518 8473{
695344c0 8474 /* xgettext:c-format */
a0c8462f
AM
8475 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8476 reftype, name);
d9352518
DB
8477}
8478
8479static bfd_boolean
a0c8462f
AM
8480eval_symbol (bfd_vma *result,
8481 const char **symp,
8482 bfd *input_bfd,
8b127cbc 8483 struct elf_final_link_info *flinfo,
a0c8462f
AM
8484 bfd_vma dot,
8485 Elf_Internal_Sym *isymbuf,
8486 size_t locsymcount,
8487 int signed_p)
d9352518 8488{
4b93929b
NC
8489 size_t len;
8490 size_t symlen;
a0c8462f
AM
8491 bfd_vma a;
8492 bfd_vma b;
4b93929b 8493 char symbuf[4096];
0f02bbd9 8494 const char *sym = *symp;
a0c8462f
AM
8495 const char *symend;
8496 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8497
8498 len = strlen (sym);
8499 symend = sym + len;
8500
4b93929b 8501 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8502 {
8503 bfd_set_error (bfd_error_invalid_operation);
8504 return FALSE;
8505 }
a0c8462f 8506
d9352518
DB
8507 switch (* sym)
8508 {
8509 case '.':
0f02bbd9
AM
8510 *result = dot;
8511 *symp = sym + 1;
d9352518
DB
8512 return TRUE;
8513
8514 case '#':
0f02bbd9
AM
8515 ++sym;
8516 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8517 return TRUE;
8518
8519 case 'S':
8520 symbol_is_section = TRUE;
1a0670f3 8521 /* Fall through. */
a0c8462f 8522 case 's':
0f02bbd9
AM
8523 ++sym;
8524 symlen = strtol (sym, (char **) symp, 10);
8525 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8526
4b93929b 8527 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8528 {
8529 bfd_set_error (bfd_error_invalid_operation);
8530 return FALSE;
8531 }
8532
8533 memcpy (symbuf, sym, symlen);
a0c8462f 8534 symbuf[symlen] = '\0';
0f02bbd9 8535 *symp = sym + symlen;
a0c8462f
AM
8536
8537 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8538 the symbol as a section, or vice-versa. so we're pretty liberal in our
8539 interpretation here; section means "try section first", not "must be a
8540 section", and likewise with symbol. */
8541
a0c8462f 8542 if (symbol_is_section)
d9352518 8543 {
37b01f6a 8544 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8545 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8546 isymbuf, locsymcount))
d9352518
DB
8547 {
8548 undefined_reference ("section", symbuf);
8549 return FALSE;
8550 }
a0c8462f
AM
8551 }
8552 else
d9352518 8553 {
8b127cbc 8554 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8555 isymbuf, locsymcount)
8b127cbc 8556 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8557 result, input_bfd))
d9352518
DB
8558 {
8559 undefined_reference ("symbol", symbuf);
8560 return FALSE;
8561 }
8562 }
8563
8564 return TRUE;
a0c8462f 8565
d9352518
DB
8566 /* All that remains are operators. */
8567
8568#define UNARY_OP(op) \
8569 if (strncmp (sym, #op, strlen (#op)) == 0) \
8570 { \
8571 sym += strlen (#op); \
a0c8462f
AM
8572 if (*sym == ':') \
8573 ++sym; \
0f02bbd9 8574 *symp = sym; \
8b127cbc 8575 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8576 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8577 return FALSE; \
8578 if (signed_p) \
0f02bbd9 8579 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8580 else \
8581 *result = op a; \
d9352518
DB
8582 return TRUE; \
8583 }
8584
8585#define BINARY_OP(op) \
8586 if (strncmp (sym, #op, strlen (#op)) == 0) \
8587 { \
8588 sym += strlen (#op); \
a0c8462f
AM
8589 if (*sym == ':') \
8590 ++sym; \
0f02bbd9 8591 *symp = sym; \
8b127cbc 8592 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8593 isymbuf, locsymcount, signed_p)) \
a0c8462f 8594 return FALSE; \
0f02bbd9 8595 ++*symp; \
8b127cbc 8596 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8597 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8598 return FALSE; \
8599 if (signed_p) \
0f02bbd9 8600 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8601 else \
8602 *result = a op b; \
d9352518
DB
8603 return TRUE; \
8604 }
8605
8606 default:
8607 UNARY_OP (0-);
8608 BINARY_OP (<<);
8609 BINARY_OP (>>);
8610 BINARY_OP (==);
8611 BINARY_OP (!=);
8612 BINARY_OP (<=);
8613 BINARY_OP (>=);
8614 BINARY_OP (&&);
8615 BINARY_OP (||);
8616 UNARY_OP (~);
8617 UNARY_OP (!);
8618 BINARY_OP (*);
8619 BINARY_OP (/);
8620 BINARY_OP (%);
8621 BINARY_OP (^);
8622 BINARY_OP (|);
8623 BINARY_OP (&);
8624 BINARY_OP (+);
8625 BINARY_OP (-);
8626 BINARY_OP (<);
8627 BINARY_OP (>);
8628#undef UNARY_OP
8629#undef BINARY_OP
8630 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8631 bfd_set_error (bfd_error_invalid_operation);
8632 return FALSE;
8633 }
8634}
8635
d9352518 8636static void
a0c8462f
AM
8637put_value (bfd_vma size,
8638 unsigned long chunksz,
8639 bfd *input_bfd,
8640 bfd_vma x,
8641 bfd_byte *location)
d9352518
DB
8642{
8643 location += (size - chunksz);
8644
41cd1ad1 8645 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8646 {
8647 switch (chunksz)
8648 {
d9352518
DB
8649 case 1:
8650 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8651 x >>= 8;
d9352518
DB
8652 break;
8653 case 2:
8654 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8655 x >>= 16;
d9352518
DB
8656 break;
8657 case 4:
8658 bfd_put_32 (input_bfd, x, location);
65164438
NC
8659 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8660 x >>= 16;
8661 x >>= 16;
d9352518 8662 break;
d9352518 8663#ifdef BFD64
41cd1ad1 8664 case 8:
d9352518 8665 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8666 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8667 x >>= 32;
8668 x >>= 32;
8669 break;
d9352518 8670#endif
41cd1ad1
NC
8671 default:
8672 abort ();
d9352518
DB
8673 break;
8674 }
8675 }
8676}
8677
a0c8462f
AM
8678static bfd_vma
8679get_value (bfd_vma size,
8680 unsigned long chunksz,
8681 bfd *input_bfd,
8682 bfd_byte *location)
d9352518 8683{
9b239e0e 8684 int shift;
d9352518
DB
8685 bfd_vma x = 0;
8686
9b239e0e
NC
8687 /* Sanity checks. */
8688 BFD_ASSERT (chunksz <= sizeof (x)
8689 && size >= chunksz
8690 && chunksz != 0
8691 && (size % chunksz) == 0
8692 && input_bfd != NULL
8693 && location != NULL);
8694
8695 if (chunksz == sizeof (x))
8696 {
8697 BFD_ASSERT (size == chunksz);
8698
8699 /* Make sure that we do not perform an undefined shift operation.
8700 We know that size == chunksz so there will only be one iteration
8701 of the loop below. */
8702 shift = 0;
8703 }
8704 else
8705 shift = 8 * chunksz;
8706
a0c8462f 8707 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8708 {
8709 switch (chunksz)
8710 {
d9352518 8711 case 1:
9b239e0e 8712 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8713 break;
8714 case 2:
9b239e0e 8715 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8716 break;
8717 case 4:
9b239e0e 8718 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8719 break;
d9352518 8720#ifdef BFD64
9b239e0e
NC
8721 case 8:
8722 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8723 break;
9b239e0e
NC
8724#endif
8725 default:
8726 abort ();
d9352518
DB
8727 }
8728 }
8729 return x;
8730}
8731
a0c8462f
AM
8732static void
8733decode_complex_addend (unsigned long *start, /* in bits */
8734 unsigned long *oplen, /* in bits */
8735 unsigned long *len, /* in bits */
8736 unsigned long *wordsz, /* in bytes */
8737 unsigned long *chunksz, /* in bytes */
8738 unsigned long *lsb0_p,
8739 unsigned long *signed_p,
8740 unsigned long *trunc_p,
8741 unsigned long encoded)
d9352518 8742{
07d6d2b8
AM
8743 * start = encoded & 0x3F;
8744 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8745 * oplen = (encoded >> 12) & 0x3F;
8746 * wordsz = (encoded >> 18) & 0xF;
8747 * chunksz = (encoded >> 22) & 0xF;
8748 * lsb0_p = (encoded >> 27) & 1;
8749 * signed_p = (encoded >> 28) & 1;
8750 * trunc_p = (encoded >> 29) & 1;
8751}
8752
cdfeee4f 8753bfd_reloc_status_type
0f02bbd9 8754bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 8755 asection *input_section,
0f02bbd9
AM
8756 bfd_byte *contents,
8757 Elf_Internal_Rela *rel,
8758 bfd_vma relocation)
d9352518 8759{
0f02bbd9
AM
8760 bfd_vma shift, x, mask;
8761 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8762 bfd_reloc_status_type r;
bb294208 8763 bfd_size_type octets;
d9352518
DB
8764
8765 /* Perform this reloc, since it is complex.
8766 (this is not to say that it necessarily refers to a complex
8767 symbol; merely that it is a self-describing CGEN based reloc.
8768 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8769 word size, etc) encoded within it.). */
d9352518 8770
a0c8462f
AM
8771 decode_complex_addend (&start, &oplen, &len, &wordsz,
8772 &chunksz, &lsb0_p, &signed_p,
8773 &trunc_p, rel->r_addend);
d9352518
DB
8774
8775 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8776
8777 if (lsb0_p)
8778 shift = (start + 1) - len;
8779 else
8780 shift = (8 * wordsz) - (start + len);
8781
bb294208
AM
8782 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8783 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
8784
8785#ifdef DEBUG
8786 printf ("Doing complex reloc: "
8787 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8788 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8789 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8790 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8791 oplen, (unsigned long) x, (unsigned long) mask,
8792 (unsigned long) relocation);
d9352518
DB
8793#endif
8794
cdfeee4f 8795 r = bfd_reloc_ok;
d9352518 8796 if (! trunc_p)
cdfeee4f
AM
8797 /* Now do an overflow check. */
8798 r = bfd_check_overflow ((signed_p
8799 ? complain_overflow_signed
8800 : complain_overflow_unsigned),
8801 len, 0, (8 * wordsz),
8802 relocation);
a0c8462f 8803
d9352518
DB
8804 /* Do the deed. */
8805 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8806
8807#ifdef DEBUG
8808 printf (" relocation: %8.8lx\n"
8809 " shifted mask: %8.8lx\n"
8810 " shifted/masked reloc: %8.8lx\n"
8811 " result: %8.8lx\n",
9ccb8af9
AM
8812 (unsigned long) relocation, (unsigned long) (mask << shift),
8813 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8814#endif
bb294208 8815 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 8816 return r;
d9352518
DB
8817}
8818
0e287786
AM
8819/* Functions to read r_offset from external (target order) reloc
8820 entry. Faster than bfd_getl32 et al, because we let the compiler
8821 know the value is aligned. */
53df40a4 8822
0e287786
AM
8823static bfd_vma
8824ext32l_r_offset (const void *p)
53df40a4
AM
8825{
8826 union aligned32
8827 {
8828 uint32_t v;
8829 unsigned char c[4];
8830 };
8831 const union aligned32 *a
0e287786 8832 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8833
8834 uint32_t aval = ( (uint32_t) a->c[0]
8835 | (uint32_t) a->c[1] << 8
8836 | (uint32_t) a->c[2] << 16
8837 | (uint32_t) a->c[3] << 24);
0e287786 8838 return aval;
53df40a4
AM
8839}
8840
0e287786
AM
8841static bfd_vma
8842ext32b_r_offset (const void *p)
53df40a4
AM
8843{
8844 union aligned32
8845 {
8846 uint32_t v;
8847 unsigned char c[4];
8848 };
8849 const union aligned32 *a
0e287786 8850 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8851
8852 uint32_t aval = ( (uint32_t) a->c[0] << 24
8853 | (uint32_t) a->c[1] << 16
8854 | (uint32_t) a->c[2] << 8
8855 | (uint32_t) a->c[3]);
0e287786 8856 return aval;
53df40a4
AM
8857}
8858
8859#ifdef BFD_HOST_64_BIT
0e287786
AM
8860static bfd_vma
8861ext64l_r_offset (const void *p)
53df40a4
AM
8862{
8863 union aligned64
8864 {
8865 uint64_t v;
8866 unsigned char c[8];
8867 };
8868 const union aligned64 *a
0e287786 8869 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8870
8871 uint64_t aval = ( (uint64_t) a->c[0]
8872 | (uint64_t) a->c[1] << 8
8873 | (uint64_t) a->c[2] << 16
8874 | (uint64_t) a->c[3] << 24
8875 | (uint64_t) a->c[4] << 32
8876 | (uint64_t) a->c[5] << 40
8877 | (uint64_t) a->c[6] << 48
8878 | (uint64_t) a->c[7] << 56);
0e287786 8879 return aval;
53df40a4
AM
8880}
8881
0e287786
AM
8882static bfd_vma
8883ext64b_r_offset (const void *p)
53df40a4
AM
8884{
8885 union aligned64
8886 {
8887 uint64_t v;
8888 unsigned char c[8];
8889 };
8890 const union aligned64 *a
0e287786 8891 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8892
8893 uint64_t aval = ( (uint64_t) a->c[0] << 56
8894 | (uint64_t) a->c[1] << 48
8895 | (uint64_t) a->c[2] << 40
8896 | (uint64_t) a->c[3] << 32
8897 | (uint64_t) a->c[4] << 24
8898 | (uint64_t) a->c[5] << 16
8899 | (uint64_t) a->c[6] << 8
8900 | (uint64_t) a->c[7]);
0e287786 8901 return aval;
53df40a4
AM
8902}
8903#endif
8904
c152c796
AM
8905/* When performing a relocatable link, the input relocations are
8906 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8907 referenced must be updated. Update all the relocations found in
8908 RELDATA. */
c152c796 8909
bca6d0e3 8910static bfd_boolean
c152c796 8911elf_link_adjust_relocs (bfd *abfd,
9eaff861 8912 asection *sec,
28dbcedc 8913 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8914 bfd_boolean sort,
8915 struct bfd_link_info *info)
c152c796
AM
8916{
8917 unsigned int i;
8918 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8919 bfd_byte *erela;
8920 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8921 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8922 bfd_vma r_type_mask;
8923 int r_sym_shift;
d4730f92
BS
8924 unsigned int count = reldata->count;
8925 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8926
d4730f92 8927 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8928 {
8929 swap_in = bed->s->swap_reloc_in;
8930 swap_out = bed->s->swap_reloc_out;
8931 }
d4730f92 8932 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8933 {
8934 swap_in = bed->s->swap_reloca_in;
8935 swap_out = bed->s->swap_reloca_out;
8936 }
8937 else
8938 abort ();
8939
8940 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8941 abort ();
8942
8943 if (bed->s->arch_size == 32)
8944 {
8945 r_type_mask = 0xff;
8946 r_sym_shift = 8;
8947 }
8948 else
8949 {
8950 r_type_mask = 0xffffffff;
8951 r_sym_shift = 32;
8952 }
8953
d4730f92
BS
8954 erela = reldata->hdr->contents;
8955 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8956 {
8957 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8958 unsigned int j;
8959
8960 if (*rel_hash == NULL)
8961 continue;
8962
10bbbc1d
NC
8963 if ((*rel_hash)->indx == -2
8964 && info->gc_sections
8965 && ! info->gc_keep_exported)
8966 {
8967 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8968 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8969 abfd, sec,
8970 (*rel_hash)->root.root.string);
9793eb77 8971 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8972 abfd, sec);
10bbbc1d
NC
8973 bfd_set_error (bfd_error_invalid_operation);
8974 return FALSE;
8975 }
c152c796
AM
8976 BFD_ASSERT ((*rel_hash)->indx >= 0);
8977
8978 (*swap_in) (abfd, erela, irela);
8979 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8980 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8981 | (irela[j].r_info & r_type_mask));
8982 (*swap_out) (abfd, irela, erela);
8983 }
53df40a4 8984
9eaff861
AO
8985 if (bed->elf_backend_update_relocs)
8986 (*bed->elf_backend_update_relocs) (sec, reldata);
8987
0e287786 8988 if (sort && count != 0)
53df40a4 8989 {
0e287786
AM
8990 bfd_vma (*ext_r_off) (const void *);
8991 bfd_vma r_off;
8992 size_t elt_size;
8993 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8994 bfd_byte *buf = NULL;
28dbcedc
AM
8995
8996 if (bed->s->arch_size == 32)
8997 {
8998 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8999 ext_r_off = ext32l_r_offset;
28dbcedc 9000 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9001 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9002 else
9003 abort ();
9004 }
53df40a4 9005 else
28dbcedc 9006 {
53df40a4 9007#ifdef BFD_HOST_64_BIT
28dbcedc 9008 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9009 ext_r_off = ext64l_r_offset;
28dbcedc 9010 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9011 ext_r_off = ext64b_r_offset;
28dbcedc 9012 else
53df40a4 9013#endif
28dbcedc
AM
9014 abort ();
9015 }
0e287786 9016
bca6d0e3
AM
9017 /* Must use a stable sort here. A modified insertion sort,
9018 since the relocs are mostly sorted already. */
0e287786
AM
9019 elt_size = reldata->hdr->sh_entsize;
9020 base = reldata->hdr->contents;
9021 end = base + count * elt_size;
bca6d0e3 9022 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9023 abort ();
9024
9025 /* Ensure the first element is lowest. This acts as a sentinel,
9026 speeding the main loop below. */
9027 r_off = (*ext_r_off) (base);
9028 for (p = loc = base; (p += elt_size) < end; )
9029 {
9030 bfd_vma r_off2 = (*ext_r_off) (p);
9031 if (r_off > r_off2)
9032 {
9033 r_off = r_off2;
9034 loc = p;
9035 }
9036 }
9037 if (loc != base)
9038 {
9039 /* Don't just swap *base and *loc as that changes the order
9040 of the original base[0] and base[1] if they happen to
9041 have the same r_offset. */
bca6d0e3
AM
9042 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9043 memcpy (onebuf, loc, elt_size);
0e287786 9044 memmove (base + elt_size, base, loc - base);
bca6d0e3 9045 memcpy (base, onebuf, elt_size);
0e287786
AM
9046 }
9047
b29b8669 9048 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9049 {
9050 /* base to p is sorted, *p is next to insert. */
9051 r_off = (*ext_r_off) (p);
9052 /* Search the sorted region for location to insert. */
9053 loc = p - elt_size;
9054 while (r_off < (*ext_r_off) (loc))
9055 loc -= elt_size;
9056 loc += elt_size;
9057 if (loc != p)
9058 {
bca6d0e3
AM
9059 /* Chances are there is a run of relocs to insert here,
9060 from one of more input files. Files are not always
9061 linked in order due to the way elf_link_input_bfd is
9062 called. See pr17666. */
9063 size_t sortlen = p - loc;
9064 bfd_vma r_off2 = (*ext_r_off) (loc);
9065 size_t runlen = elt_size;
9066 size_t buf_size = 96 * 1024;
9067 while (p + runlen < end
9068 && (sortlen <= buf_size
9069 || runlen + elt_size <= buf_size)
9070 && r_off2 > (*ext_r_off) (p + runlen))
9071 runlen += elt_size;
9072 if (buf == NULL)
9073 {
9074 buf = bfd_malloc (buf_size);
9075 if (buf == NULL)
9076 return FALSE;
9077 }
9078 if (runlen < sortlen)
9079 {
9080 memcpy (buf, p, runlen);
9081 memmove (loc + runlen, loc, sortlen);
9082 memcpy (loc, buf, runlen);
9083 }
9084 else
9085 {
9086 memcpy (buf, loc, sortlen);
9087 memmove (loc, p, runlen);
9088 memcpy (loc + runlen, buf, sortlen);
9089 }
b29b8669 9090 p += runlen - elt_size;
0e287786
AM
9091 }
9092 }
9093 /* Hashes are no longer valid. */
28dbcedc
AM
9094 free (reldata->hashes);
9095 reldata->hashes = NULL;
bca6d0e3 9096 free (buf);
53df40a4 9097 }
bca6d0e3 9098 return TRUE;
c152c796
AM
9099}
9100
9101struct elf_link_sort_rela
9102{
9103 union {
9104 bfd_vma offset;
9105 bfd_vma sym_mask;
9106 } u;
9107 enum elf_reloc_type_class type;
9108 /* We use this as an array of size int_rels_per_ext_rel. */
9109 Elf_Internal_Rela rela[1];
9110};
9111
dcea6a95
AM
9112/* qsort stability here and for cmp2 is only an issue if multiple
9113 dynamic relocations are emitted at the same address. But targets
9114 that apply a series of dynamic relocations each operating on the
9115 result of the prior relocation can't use -z combreloc as
9116 implemented anyway. Such schemes tend to be broken by sorting on
9117 symbol index. That leaves dynamic NONE relocs as the only other
9118 case where ld might emit multiple relocs at the same address, and
9119 those are only emitted due to target bugs. */
9120
c152c796
AM
9121static int
9122elf_link_sort_cmp1 (const void *A, const void *B)
9123{
a50b1753
NC
9124 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9125 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9126 int relativea, relativeb;
9127
9128 relativea = a->type == reloc_class_relative;
9129 relativeb = b->type == reloc_class_relative;
9130
9131 if (relativea < relativeb)
9132 return 1;
9133 if (relativea > relativeb)
9134 return -1;
9135 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9136 return -1;
9137 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9138 return 1;
9139 if (a->rela->r_offset < b->rela->r_offset)
9140 return -1;
9141 if (a->rela->r_offset > b->rela->r_offset)
9142 return 1;
9143 return 0;
9144}
9145
9146static int
9147elf_link_sort_cmp2 (const void *A, const void *B)
9148{
a50b1753
NC
9149 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9150 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9151
7e612e98 9152 if (a->type < b->type)
c152c796 9153 return -1;
7e612e98 9154 if (a->type > b->type)
c152c796 9155 return 1;
7e612e98 9156 if (a->u.offset < b->u.offset)
c152c796 9157 return -1;
7e612e98 9158 if (a->u.offset > b->u.offset)
c152c796
AM
9159 return 1;
9160 if (a->rela->r_offset < b->rela->r_offset)
9161 return -1;
9162 if (a->rela->r_offset > b->rela->r_offset)
9163 return 1;
9164 return 0;
9165}
9166
9167static size_t
9168elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9169{
3410fea8 9170 asection *dynamic_relocs;
fc66a176
L
9171 asection *rela_dyn;
9172 asection *rel_dyn;
c152c796
AM
9173 bfd_size_type count, size;
9174 size_t i, ret, sort_elt, ext_size;
9175 bfd_byte *sort, *s_non_relative, *p;
9176 struct elf_link_sort_rela *sq;
9177 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9178 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9179 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9180 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9181 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9182 struct bfd_link_order *lo;
9183 bfd_vma r_sym_mask;
3410fea8 9184 bfd_boolean use_rela;
c152c796 9185
3410fea8
NC
9186 /* Find a dynamic reloc section. */
9187 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9188 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9189 if (rela_dyn != NULL && rela_dyn->size > 0
9190 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9191 {
3410fea8
NC
9192 bfd_boolean use_rela_initialised = FALSE;
9193
9194 /* This is just here to stop gcc from complaining.
c8e44c6d 9195 Its initialization checking code is not perfect. */
3410fea8
NC
9196 use_rela = TRUE;
9197
9198 /* Both sections are present. Examine the sizes
9199 of the indirect sections to help us choose. */
9200 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9201 if (lo->type == bfd_indirect_link_order)
9202 {
9203 asection *o = lo->u.indirect.section;
9204
9205 if ((o->size % bed->s->sizeof_rela) == 0)
9206 {
9207 if ((o->size % bed->s->sizeof_rel) == 0)
9208 /* Section size is divisible by both rel and rela sizes.
9209 It is of no help to us. */
9210 ;
9211 else
9212 {
9213 /* Section size is only divisible by rela. */
535b785f 9214 if (use_rela_initialised && !use_rela)
3410fea8 9215 {
9793eb77 9216 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9217 "they are in more than one size"),
9218 abfd);
3410fea8
NC
9219 bfd_set_error (bfd_error_invalid_operation);
9220 return 0;
9221 }
9222 else
9223 {
9224 use_rela = TRUE;
9225 use_rela_initialised = TRUE;
9226 }
9227 }
9228 }
9229 else if ((o->size % bed->s->sizeof_rel) == 0)
9230 {
9231 /* Section size is only divisible by rel. */
535b785f 9232 if (use_rela_initialised && use_rela)
3410fea8 9233 {
9793eb77 9234 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9235 "they are in more than one size"),
9236 abfd);
3410fea8
NC
9237 bfd_set_error (bfd_error_invalid_operation);
9238 return 0;
9239 }
9240 else
9241 {
9242 use_rela = FALSE;
9243 use_rela_initialised = TRUE;
9244 }
9245 }
9246 else
9247 {
c8e44c6d
AM
9248 /* The section size is not divisible by either -
9249 something is wrong. */
9793eb77 9250 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9251 "they are of an unknown size"), abfd);
3410fea8
NC
9252 bfd_set_error (bfd_error_invalid_operation);
9253 return 0;
9254 }
9255 }
9256
9257 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9258 if (lo->type == bfd_indirect_link_order)
9259 {
9260 asection *o = lo->u.indirect.section;
9261
9262 if ((o->size % bed->s->sizeof_rela) == 0)
9263 {
9264 if ((o->size % bed->s->sizeof_rel) == 0)
9265 /* Section size is divisible by both rel and rela sizes.
9266 It is of no help to us. */
9267 ;
9268 else
9269 {
9270 /* Section size is only divisible by rela. */
535b785f 9271 if (use_rela_initialised && !use_rela)
3410fea8 9272 {
9793eb77 9273 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9274 "they are in more than one size"),
9275 abfd);
3410fea8
NC
9276 bfd_set_error (bfd_error_invalid_operation);
9277 return 0;
9278 }
9279 else
9280 {
9281 use_rela = TRUE;
9282 use_rela_initialised = TRUE;
9283 }
9284 }
9285 }
9286 else if ((o->size % bed->s->sizeof_rel) == 0)
9287 {
9288 /* Section size is only divisible by rel. */
535b785f 9289 if (use_rela_initialised && use_rela)
3410fea8 9290 {
9793eb77 9291 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9292 "they are in more than one size"),
9293 abfd);
3410fea8
NC
9294 bfd_set_error (bfd_error_invalid_operation);
9295 return 0;
9296 }
9297 else
9298 {
9299 use_rela = FALSE;
9300 use_rela_initialised = TRUE;
9301 }
9302 }
9303 else
9304 {
c8e44c6d
AM
9305 /* The section size is not divisible by either -
9306 something is wrong. */
9793eb77 9307 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9308 "they are of an unknown size"), abfd);
3410fea8
NC
9309 bfd_set_error (bfd_error_invalid_operation);
9310 return 0;
9311 }
9312 }
9313
9314 if (! use_rela_initialised)
9315 /* Make a guess. */
9316 use_rela = TRUE;
c152c796 9317 }
fc66a176
L
9318 else if (rela_dyn != NULL && rela_dyn->size > 0)
9319 use_rela = TRUE;
9320 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9321 use_rela = FALSE;
c152c796 9322 else
fc66a176 9323 return 0;
3410fea8
NC
9324
9325 if (use_rela)
c152c796 9326 {
3410fea8 9327 dynamic_relocs = rela_dyn;
c152c796
AM
9328 ext_size = bed->s->sizeof_rela;
9329 swap_in = bed->s->swap_reloca_in;
9330 swap_out = bed->s->swap_reloca_out;
9331 }
3410fea8
NC
9332 else
9333 {
9334 dynamic_relocs = rel_dyn;
9335 ext_size = bed->s->sizeof_rel;
9336 swap_in = bed->s->swap_reloc_in;
9337 swap_out = bed->s->swap_reloc_out;
9338 }
c152c796
AM
9339
9340 size = 0;
3410fea8 9341 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9342 if (lo->type == bfd_indirect_link_order)
3410fea8 9343 size += lo->u.indirect.section->size;
c152c796 9344
3410fea8 9345 if (size != dynamic_relocs->size)
c152c796
AM
9346 return 0;
9347
9348 sort_elt = (sizeof (struct elf_link_sort_rela)
9349 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9350
9351 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9352 if (count == 0)
9353 return 0;
a50b1753 9354 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9355
c152c796
AM
9356 if (sort == NULL)
9357 {
9358 (*info->callbacks->warning)
9793eb77 9359 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9360 return 0;
9361 }
9362
9363 if (bed->s->arch_size == 32)
9364 r_sym_mask = ~(bfd_vma) 0xff;
9365 else
9366 r_sym_mask = ~(bfd_vma) 0xffffffff;
9367
3410fea8 9368 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9369 if (lo->type == bfd_indirect_link_order)
9370 {
9371 bfd_byte *erel, *erelend;
9372 asection *o = lo->u.indirect.section;
9373
1da212d6
AM
9374 if (o->contents == NULL && o->size != 0)
9375 {
9376 /* This is a reloc section that is being handled as a normal
9377 section. See bfd_section_from_shdr. We can't combine
9378 relocs in this case. */
9379 free (sort);
9380 return 0;
9381 }
c152c796 9382 erel = o->contents;
eea6121a 9383 erelend = o->contents + o->size;
c8e44c6d 9384 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9385
c152c796
AM
9386 while (erel < erelend)
9387 {
9388 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9389
c152c796 9390 (*swap_in) (abfd, erel, s->rela);
7e612e98 9391 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9392 s->u.sym_mask = r_sym_mask;
9393 p += sort_elt;
9394 erel += ext_size;
9395 }
9396 }
9397
9398 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9399
9400 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9401 {
9402 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9403 if (s->type != reloc_class_relative)
9404 break;
9405 }
9406 ret = i;
9407 s_non_relative = p;
9408
9409 sq = (struct elf_link_sort_rela *) s_non_relative;
9410 for (; i < count; i++, p += sort_elt)
9411 {
9412 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9413 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9414 sq = sp;
9415 sp->u.offset = sq->rela->r_offset;
9416 }
9417
9418 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9419
c8e44c6d
AM
9420 struct elf_link_hash_table *htab = elf_hash_table (info);
9421 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9422 {
9423 /* We have plt relocs in .rela.dyn. */
9424 sq = (struct elf_link_sort_rela *) sort;
9425 for (i = 0; i < count; i++)
9426 if (sq[count - i - 1].type != reloc_class_plt)
9427 break;
9428 if (i != 0 && htab->srelplt->size == i * ext_size)
9429 {
9430 struct bfd_link_order **plo;
9431 /* Put srelplt link_order last. This is so the output_offset
9432 set in the next loop is correct for DT_JMPREL. */
9433 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9434 if ((*plo)->type == bfd_indirect_link_order
9435 && (*plo)->u.indirect.section == htab->srelplt)
9436 {
9437 lo = *plo;
9438 *plo = lo->next;
9439 }
9440 else
9441 plo = &(*plo)->next;
9442 *plo = lo;
9443 lo->next = NULL;
9444 dynamic_relocs->map_tail.link_order = lo;
9445 }
9446 }
9447
9448 p = sort;
3410fea8 9449 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9450 if (lo->type == bfd_indirect_link_order)
9451 {
9452 bfd_byte *erel, *erelend;
9453 asection *o = lo->u.indirect.section;
9454
9455 erel = o->contents;
eea6121a 9456 erelend = o->contents + o->size;
c8e44c6d 9457 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9458 while (erel < erelend)
9459 {
9460 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9461 (*swap_out) (abfd, s->rela, erel);
9462 p += sort_elt;
9463 erel += ext_size;
9464 }
9465 }
9466
9467 free (sort);
3410fea8 9468 *psec = dynamic_relocs;
c152c796
AM
9469 return ret;
9470}
9471
ef10c3ac 9472/* Add a symbol to the output symbol string table. */
c152c796 9473
6e0b88f1 9474static int
ef10c3ac
L
9475elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9476 const char *name,
9477 Elf_Internal_Sym *elfsym,
9478 asection *input_sec,
9479 struct elf_link_hash_entry *h)
c152c796 9480{
6e0b88f1 9481 int (*output_symbol_hook)
c152c796
AM
9482 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9483 struct elf_link_hash_entry *);
ef10c3ac 9484 struct elf_link_hash_table *hash_table;
c152c796 9485 const struct elf_backend_data *bed;
ef10c3ac 9486 bfd_size_type strtabsize;
c152c796 9487
8539e4e8
AM
9488 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9489
8b127cbc 9490 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9491 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9492 if (output_symbol_hook != NULL)
9493 {
8b127cbc 9494 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9495 if (ret != 1)
9496 return ret;
c152c796
AM
9497 }
9498
06f44071
AM
9499 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9500 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9501 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9502 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9503
ef10c3ac
L
9504 if (name == NULL
9505 || *name == '\0'
9506 || (input_sec->flags & SEC_EXCLUDE))
9507 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9508 else
9509 {
ef10c3ac
L
9510 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9511 to get the final offset for st_name. */
9512 elfsym->st_name
9513 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9514 name, FALSE);
c152c796 9515 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9516 return 0;
c152c796
AM
9517 }
9518
ef10c3ac
L
9519 hash_table = elf_hash_table (flinfo->info);
9520 strtabsize = hash_table->strtabsize;
9521 if (strtabsize <= hash_table->strtabcount)
c152c796 9522 {
ef10c3ac
L
9523 strtabsize += strtabsize;
9524 hash_table->strtabsize = strtabsize;
9525 strtabsize *= sizeof (*hash_table->strtab);
9526 hash_table->strtab
9527 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9528 strtabsize);
9529 if (hash_table->strtab == NULL)
6e0b88f1 9530 return 0;
c152c796 9531 }
ef10c3ac
L
9532 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9533 hash_table->strtab[hash_table->strtabcount].dest_index
9534 = hash_table->strtabcount;
9535 hash_table->strtab[hash_table->strtabcount].destshndx_index
9536 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9537
ed48ec2e 9538 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9539 hash_table->strtabcount += 1;
9540
9541 return 1;
9542}
9543
9544/* Swap symbols out to the symbol table and flush the output symbols to
9545 the file. */
9546
9547static bfd_boolean
9548elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9549{
9550 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9551 bfd_size_type amt;
9552 size_t i;
ef10c3ac
L
9553 const struct elf_backend_data *bed;
9554 bfd_byte *symbuf;
9555 Elf_Internal_Shdr *hdr;
9556 file_ptr pos;
9557 bfd_boolean ret;
9558
9559 if (!hash_table->strtabcount)
9560 return TRUE;
9561
9562 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9563
9564 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9565
ef10c3ac
L
9566 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9567 symbuf = (bfd_byte *) bfd_malloc (amt);
9568 if (symbuf == NULL)
9569 return FALSE;
1b786873 9570
ef10c3ac 9571 if (flinfo->symshndxbuf)
c152c796 9572 {
ef53be89
AM
9573 amt = sizeof (Elf_External_Sym_Shndx);
9574 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9575 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9576 if (flinfo->symshndxbuf == NULL)
c152c796 9577 {
ef10c3ac
L
9578 free (symbuf);
9579 return FALSE;
c152c796 9580 }
c152c796
AM
9581 }
9582
ef10c3ac
L
9583 for (i = 0; i < hash_table->strtabcount; i++)
9584 {
9585 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9586 if (elfsym->sym.st_name == (unsigned long) -1)
9587 elfsym->sym.st_name = 0;
9588 else
9589 elfsym->sym.st_name
9590 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9591 elfsym->sym.st_name);
9592 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9593 ((bfd_byte *) symbuf
9594 + (elfsym->dest_index
9595 * bed->s->sizeof_sym)),
9596 (flinfo->symshndxbuf
9597 + elfsym->destshndx_index));
9598 }
9599
1ff6de03
NA
9600 /* Allow the linker to examine the strtab and symtab now they are
9601 populated. */
9602
9603 if (flinfo->info->callbacks->examine_strtab)
9604 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9605 hash_table->strtabcount,
9606 flinfo->symstrtab);
9607
ef10c3ac
L
9608 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9609 pos = hdr->sh_offset + hdr->sh_size;
9610 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9611 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9612 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9613 {
9614 hdr->sh_size += amt;
9615 ret = TRUE;
9616 }
9617 else
9618 ret = FALSE;
c152c796 9619
ef10c3ac
L
9620 free (symbuf);
9621
9622 free (hash_table->strtab);
9623 hash_table->strtab = NULL;
9624
9625 return ret;
c152c796
AM
9626}
9627
c0d5a53d
L
9628/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9629
9630static bfd_boolean
9631check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9632{
4fbb74a6
AM
9633 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9634 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9635 {
9636 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9637 beyond 64k. */
4eca0228 9638 _bfd_error_handler
695344c0 9639 /* xgettext:c-format */
9793eb77 9640 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9641 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9642 bfd_set_error (bfd_error_nonrepresentable_section);
9643 return FALSE;
9644 }
9645 return TRUE;
9646}
9647
c152c796
AM
9648/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9649 allowing an unsatisfied unversioned symbol in the DSO to match a
9650 versioned symbol that would normally require an explicit version.
9651 We also handle the case that a DSO references a hidden symbol
9652 which may be satisfied by a versioned symbol in another DSO. */
9653
9654static bfd_boolean
9655elf_link_check_versioned_symbol (struct bfd_link_info *info,
9656 const struct elf_backend_data *bed,
9657 struct elf_link_hash_entry *h)
9658{
9659 bfd *abfd;
9660 struct elf_link_loaded_list *loaded;
9661
9662 if (!is_elf_hash_table (info->hash))
9663 return FALSE;
9664
90c984fc
L
9665 /* Check indirect symbol. */
9666 while (h->root.type == bfd_link_hash_indirect)
9667 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9668
c152c796
AM
9669 switch (h->root.type)
9670 {
9671 default:
9672 abfd = NULL;
9673 break;
9674
9675 case bfd_link_hash_undefined:
9676 case bfd_link_hash_undefweak:
9677 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9678 if (abfd == NULL
9679 || (abfd->flags & DYNAMIC) == 0
e56f61be 9680 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9681 return FALSE;
9682 break;
9683
9684 case bfd_link_hash_defined:
9685 case bfd_link_hash_defweak:
9686 abfd = h->root.u.def.section->owner;
9687 break;
9688
9689 case bfd_link_hash_common:
9690 abfd = h->root.u.c.p->section->owner;
9691 break;
9692 }
9693 BFD_ASSERT (abfd != NULL);
9694
9695 for (loaded = elf_hash_table (info)->loaded;
9696 loaded != NULL;
9697 loaded = loaded->next)
9698 {
9699 bfd *input;
9700 Elf_Internal_Shdr *hdr;
ef53be89
AM
9701 size_t symcount;
9702 size_t extsymcount;
9703 size_t extsymoff;
c152c796
AM
9704 Elf_Internal_Shdr *versymhdr;
9705 Elf_Internal_Sym *isym;
9706 Elf_Internal_Sym *isymend;
9707 Elf_Internal_Sym *isymbuf;
9708 Elf_External_Versym *ever;
9709 Elf_External_Versym *extversym;
9710
9711 input = loaded->abfd;
9712
9713 /* We check each DSO for a possible hidden versioned definition. */
9714 if (input == abfd
9715 || (input->flags & DYNAMIC) == 0
9716 || elf_dynversym (input) == 0)
9717 continue;
9718
9719 hdr = &elf_tdata (input)->dynsymtab_hdr;
9720
9721 symcount = hdr->sh_size / bed->s->sizeof_sym;
9722 if (elf_bad_symtab (input))
9723 {
9724 extsymcount = symcount;
9725 extsymoff = 0;
9726 }
9727 else
9728 {
9729 extsymcount = symcount - hdr->sh_info;
9730 extsymoff = hdr->sh_info;
9731 }
9732
9733 if (extsymcount == 0)
9734 continue;
9735
9736 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9737 NULL, NULL, NULL);
9738 if (isymbuf == NULL)
9739 return FALSE;
9740
9741 /* Read in any version definitions. */
9742 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9743 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9744 if (extversym == NULL)
9745 goto error_ret;
9746
9747 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9748 || (bfd_bread (extversym, versymhdr->sh_size, input)
9749 != versymhdr->sh_size))
9750 {
9751 free (extversym);
9752 error_ret:
9753 free (isymbuf);
9754 return FALSE;
9755 }
9756
9757 ever = extversym + extsymoff;
9758 isymend = isymbuf + extsymcount;
9759 for (isym = isymbuf; isym < isymend; isym++, ever++)
9760 {
9761 const char *name;
9762 Elf_Internal_Versym iver;
9763 unsigned short version_index;
9764
9765 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9766 || isym->st_shndx == SHN_UNDEF)
9767 continue;
9768
9769 name = bfd_elf_string_from_elf_section (input,
9770 hdr->sh_link,
9771 isym->st_name);
9772 if (strcmp (name, h->root.root.string) != 0)
9773 continue;
9774
9775 _bfd_elf_swap_versym_in (input, ever, &iver);
9776
d023c380
L
9777 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9778 && !(h->def_regular
9779 && h->forced_local))
c152c796
AM
9780 {
9781 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9782 have provided a definition for the undefined sym unless
9783 it is defined in a non-shared object and forced local.
9784 */
c152c796
AM
9785 abort ();
9786 }
9787
9788 version_index = iver.vs_vers & VERSYM_VERSION;
9789 if (version_index == 1 || version_index == 2)
9790 {
9791 /* This is the base or first version. We can use it. */
9792 free (extversym);
9793 free (isymbuf);
9794 return TRUE;
9795 }
9796 }
9797
9798 free (extversym);
9799 free (isymbuf);
9800 }
9801
9802 return FALSE;
9803}
9804
b8871f35
L
9805/* Convert ELF common symbol TYPE. */
9806
9807static int
9808elf_link_convert_common_type (struct bfd_link_info *info, int type)
9809{
9810 /* Commom symbol can only appear in relocatable link. */
9811 if (!bfd_link_relocatable (info))
9812 abort ();
9813 switch (info->elf_stt_common)
9814 {
9815 case unchanged:
9816 break;
9817 case elf_stt_common:
9818 type = STT_COMMON;
9819 break;
9820 case no_elf_stt_common:
9821 type = STT_OBJECT;
9822 break;
9823 }
9824 return type;
9825}
9826
c152c796
AM
9827/* Add an external symbol to the symbol table. This is called from
9828 the hash table traversal routine. When generating a shared object,
9829 we go through the symbol table twice. The first time we output
9830 anything that might have been forced to local scope in a version
9831 script. The second time we output the symbols that are still
9832 global symbols. */
9833
9834static bfd_boolean
7686d77d 9835elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9836{
7686d77d 9837 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9838 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9839 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9840 bfd_boolean strip;
9841 Elf_Internal_Sym sym;
9842 asection *input_sec;
9843 const struct elf_backend_data *bed;
6e0b88f1
AM
9844 long indx;
9845 int ret;
b8871f35 9846 unsigned int type;
c152c796
AM
9847
9848 if (h->root.type == bfd_link_hash_warning)
9849 {
9850 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9851 if (h->root.type == bfd_link_hash_new)
9852 return TRUE;
9853 }
9854
9855 /* Decide whether to output this symbol in this pass. */
9856 if (eoinfo->localsyms)
9857 {
4deb8f71 9858 if (!h->forced_local)
c152c796
AM
9859 return TRUE;
9860 }
9861 else
9862 {
4deb8f71 9863 if (h->forced_local)
c152c796
AM
9864 return TRUE;
9865 }
9866
8b127cbc 9867 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9868
12ac1cf5 9869 if (h->root.type == bfd_link_hash_undefined)
c152c796 9870 {
12ac1cf5
NC
9871 /* If we have an undefined symbol reference here then it must have
9872 come from a shared library that is being linked in. (Undefined
98da7939
L
9873 references in regular files have already been handled unless
9874 they are in unreferenced sections which are removed by garbage
9875 collection). */
12ac1cf5
NC
9876 bfd_boolean ignore_undef = FALSE;
9877
9878 /* Some symbols may be special in that the fact that they're
9879 undefined can be safely ignored - let backend determine that. */
9880 if (bed->elf_backend_ignore_undef_symbol)
9881 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9882
9883 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9884 if (!ignore_undef
c54f1524 9885 && h->ref_dynamic_nonweak
8b127cbc
AM
9886 && (!h->ref_regular || flinfo->info->gc_sections)
9887 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9888 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9889 (*flinfo->info->callbacks->undefined_symbol)
9890 (flinfo->info, h->root.root.string,
9891 h->ref_regular ? NULL : h->root.u.undef.abfd,
9892 NULL, 0,
9893 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9894
9895 /* Strip a global symbol defined in a discarded section. */
9896 if (h->indx == -3)
9897 return TRUE;
c152c796
AM
9898 }
9899
9900 /* We should also warn if a forced local symbol is referenced from
9901 shared libraries. */
0e1862bb 9902 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9903 && h->forced_local
9904 && h->ref_dynamic
371a5866 9905 && h->def_regular
f5385ebf 9906 && !h->dynamic_def
ee659f1f 9907 && h->ref_dynamic_nonweak
8b127cbc 9908 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9909 {
17d078c5
AM
9910 bfd *def_bfd;
9911 const char *msg;
90c984fc
L
9912 struct elf_link_hash_entry *hi = h;
9913
9914 /* Check indirect symbol. */
9915 while (hi->root.type == bfd_link_hash_indirect)
9916 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9917
9918 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9919 /* xgettext:c-format */
871b3ab2 9920 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9921 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9922 /* xgettext:c-format */
871b3ab2 9923 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9924 else
695344c0 9925 /* xgettext:c-format */
871b3ab2 9926 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9927 def_bfd = flinfo->output_bfd;
90c984fc
L
9928 if (hi->root.u.def.section != bfd_abs_section_ptr)
9929 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9930 _bfd_error_handler (msg, flinfo->output_bfd,
9931 h->root.root.string, def_bfd);
17d078c5 9932 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9933 eoinfo->failed = TRUE;
9934 return FALSE;
9935 }
9936
9937 /* We don't want to output symbols that have never been mentioned by
9938 a regular file, or that we have been told to strip. However, if
9939 h->indx is set to -2, the symbol is used by a reloc and we must
9940 output it. */
d983c8c5 9941 strip = FALSE;
c152c796 9942 if (h->indx == -2)
d983c8c5 9943 ;
f5385ebf 9944 else if ((h->def_dynamic
77cfaee6
AM
9945 || h->ref_dynamic
9946 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9947 && !h->def_regular
9948 && !h->ref_regular)
c152c796 9949 strip = TRUE;
8b127cbc 9950 else if (flinfo->info->strip == strip_all)
c152c796 9951 strip = TRUE;
8b127cbc
AM
9952 else if (flinfo->info->strip == strip_some
9953 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9954 h->root.root.string, FALSE, FALSE) == NULL)
9955 strip = TRUE;
d56d55e7
AM
9956 else if ((h->root.type == bfd_link_hash_defined
9957 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9958 && ((flinfo->info->strip_discarded
dbaa2011 9959 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9960 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9961 && h->root.u.def.section->owner != NULL
d56d55e7 9962 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9963 strip = TRUE;
9e2278f5
AM
9964 else if ((h->root.type == bfd_link_hash_undefined
9965 || h->root.type == bfd_link_hash_undefweak)
9966 && h->root.u.undef.abfd != NULL
9967 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9968 strip = TRUE;
c152c796 9969
b8871f35
L
9970 type = h->type;
9971
c152c796 9972 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9973 nothing else to do. However, if it is a forced local symbol or
9974 an ifunc symbol we need to give the backend finish_dynamic_symbol
9975 function a chance to make it dynamic. */
c152c796
AM
9976 if (strip
9977 && h->dynindx == -1
b8871f35 9978 && type != STT_GNU_IFUNC
f5385ebf 9979 && !h->forced_local)
c152c796
AM
9980 return TRUE;
9981
9982 sym.st_value = 0;
9983 sym.st_size = h->size;
9984 sym.st_other = h->other;
c152c796
AM
9985 switch (h->root.type)
9986 {
9987 default:
9988 case bfd_link_hash_new:
9989 case bfd_link_hash_warning:
9990 abort ();
9991 return FALSE;
9992
9993 case bfd_link_hash_undefined:
9994 case bfd_link_hash_undefweak:
9995 input_sec = bfd_und_section_ptr;
9996 sym.st_shndx = SHN_UNDEF;
9997 break;
9998
9999 case bfd_link_hash_defined:
10000 case bfd_link_hash_defweak:
10001 {
10002 input_sec = h->root.u.def.section;
10003 if (input_sec->output_section != NULL)
10004 {
10005 sym.st_shndx =
8b127cbc 10006 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10007 input_sec->output_section);
10008 if (sym.st_shndx == SHN_BAD)
10009 {
4eca0228 10010 _bfd_error_handler
695344c0 10011 /* xgettext:c-format */
871b3ab2 10012 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10013 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10014 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10015 eoinfo->failed = TRUE;
10016 return FALSE;
10017 }
10018
10019 /* ELF symbols in relocatable files are section relative,
10020 but in nonrelocatable files they are virtual
10021 addresses. */
10022 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10023 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10024 {
10025 sym.st_value += input_sec->output_section->vma;
10026 if (h->type == STT_TLS)
10027 {
8b127cbc 10028 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10029 if (tls_sec != NULL)
10030 sym.st_value -= tls_sec->vma;
c152c796
AM
10031 }
10032 }
10033 }
10034 else
10035 {
10036 BFD_ASSERT (input_sec->owner == NULL
10037 || (input_sec->owner->flags & DYNAMIC) != 0);
10038 sym.st_shndx = SHN_UNDEF;
10039 input_sec = bfd_und_section_ptr;
10040 }
10041 }
10042 break;
10043
10044 case bfd_link_hash_common:
10045 input_sec = h->root.u.c.p->section;
a4d8e49b 10046 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10047 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10048 break;
10049
10050 case bfd_link_hash_indirect:
10051 /* These symbols are created by symbol versioning. They point
10052 to the decorated version of the name. For example, if the
10053 symbol foo@@GNU_1.2 is the default, which should be used when
10054 foo is used with no version, then we add an indirect symbol
10055 foo which points to foo@@GNU_1.2. We ignore these symbols,
10056 since the indirected symbol is already in the hash table. */
10057 return TRUE;
10058 }
10059
b8871f35
L
10060 if (type == STT_COMMON || type == STT_OBJECT)
10061 switch (h->root.type)
10062 {
10063 case bfd_link_hash_common:
10064 type = elf_link_convert_common_type (flinfo->info, type);
10065 break;
10066 case bfd_link_hash_defined:
10067 case bfd_link_hash_defweak:
10068 if (bed->common_definition (&sym))
10069 type = elf_link_convert_common_type (flinfo->info, type);
10070 else
10071 type = STT_OBJECT;
10072 break;
10073 case bfd_link_hash_undefined:
10074 case bfd_link_hash_undefweak:
10075 break;
10076 default:
10077 abort ();
10078 }
10079
4deb8f71 10080 if (h->forced_local)
b8871f35
L
10081 {
10082 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10083 /* Turn off visibility on local symbol. */
10084 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10085 }
10086 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10087 else if (h->unique_global && h->def_regular)
10088 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10089 else if (h->root.type == bfd_link_hash_undefweak
10090 || h->root.type == bfd_link_hash_defweak)
10091 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10092 else
10093 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10094 sym.st_target_internal = h->target_internal;
10095
c152c796
AM
10096 /* Give the processor backend a chance to tweak the symbol value,
10097 and also to finish up anything that needs to be done for this
10098 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10099 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10100 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10101 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10102 && h->def_regular
0e1862bb 10103 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10104 || ((h->dynindx != -1
10105 || h->forced_local)
0e1862bb 10106 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10107 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10108 || h->root.type != bfd_link_hash_undefweak))
10109 || !h->forced_local)
8b127cbc 10110 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10111 {
10112 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10113 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10114 {
10115 eoinfo->failed = TRUE;
10116 return FALSE;
10117 }
10118 }
10119
10120 /* If we are marking the symbol as undefined, and there are no
10121 non-weak references to this symbol from a regular object, then
10122 mark the symbol as weak undefined; if there are non-weak
10123 references, mark the symbol as strong. We can't do this earlier,
10124 because it might not be marked as undefined until the
10125 finish_dynamic_symbol routine gets through with it. */
10126 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10127 && h->ref_regular
c152c796
AM
10128 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10129 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10130 {
10131 int bindtype;
b8871f35 10132 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10133
10134 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10135 if (type == STT_GNU_IFUNC)
10136 type = STT_FUNC;
c152c796 10137
f5385ebf 10138 if (h->ref_regular_nonweak)
c152c796
AM
10139 bindtype = STB_GLOBAL;
10140 else
10141 bindtype = STB_WEAK;
2955ec4c 10142 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10143 }
10144
bda987c2
CD
10145 /* If this is a symbol defined in a dynamic library, don't use the
10146 symbol size from the dynamic library. Relinking an executable
10147 against a new library may introduce gratuitous changes in the
10148 executable's symbols if we keep the size. */
10149 if (sym.st_shndx == SHN_UNDEF
10150 && !h->def_regular
10151 && h->def_dynamic)
10152 sym.st_size = 0;
10153
c152c796
AM
10154 /* If a non-weak symbol with non-default visibility is not defined
10155 locally, it is a fatal error. */
0e1862bb 10156 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10157 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10158 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10159 && h->root.type == bfd_link_hash_undefined
f5385ebf 10160 && !h->def_regular)
c152c796 10161 {
17d078c5
AM
10162 const char *msg;
10163
10164 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10165 /* xgettext:c-format */
871b3ab2 10166 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10167 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10168 /* xgettext:c-format */
871b3ab2 10169 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10170 else
695344c0 10171 /* xgettext:c-format */
871b3ab2 10172 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10173 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10174 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10175 eoinfo->failed = TRUE;
10176 return FALSE;
10177 }
10178
10179 /* If this symbol should be put in the .dynsym section, then put it
10180 there now. We already know the symbol index. We also fill in
10181 the entry in the .hash section. */
1c2649ed
EB
10182 if (h->dynindx != -1
10183 && elf_hash_table (flinfo->info)->dynamic_sections_created
10184 && elf_hash_table (flinfo->info)->dynsym != NULL
10185 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10186 {
c152c796
AM
10187 bfd_byte *esym;
10188
90c984fc
L
10189 /* Since there is no version information in the dynamic string,
10190 if there is no version info in symbol version section, we will
1659f720 10191 have a run-time problem if not linking executable, referenced
4deb8f71 10192 by shared library, or not bound locally. */
1659f720 10193 if (h->verinfo.verdef == NULL
0e1862bb 10194 && (!bfd_link_executable (flinfo->info)
1659f720
L
10195 || h->ref_dynamic
10196 || !h->def_regular))
90c984fc
L
10197 {
10198 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10199
10200 if (p && p [1] != '\0')
10201 {
4eca0228 10202 _bfd_error_handler
695344c0 10203 /* xgettext:c-format */
9793eb77 10204 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10205 flinfo->output_bfd, h->root.root.string);
10206 eoinfo->failed = TRUE;
10207 return FALSE;
10208 }
10209 }
10210
c152c796 10211 sym.st_name = h->dynstr_index;
cae1fbbb
L
10212 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10213 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10214 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10215 {
10216 eoinfo->failed = TRUE;
10217 return FALSE;
10218 }
8b127cbc 10219 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10220
8b127cbc 10221 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10222 {
10223 size_t hash_entry_size;
10224 bfd_byte *bucketpos;
10225 bfd_vma chain;
41198d0c
L
10226 size_t bucketcount;
10227 size_t bucket;
10228
8b127cbc 10229 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10230 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10231
10232 hash_entry_size
8b127cbc
AM
10233 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10234 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10235 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10236 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10237 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10238 bucketpos);
10239 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10240 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10241 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10242 }
c152c796 10243
8b127cbc 10244 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10245 {
10246 Elf_Internal_Versym iversym;
10247 Elf_External_Versym *eversym;
10248
5fa370e4 10249 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10250 {
7b20f099
AM
10251 if (h->verinfo.verdef == NULL
10252 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10253 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10254 iversym.vs_vers = 0;
10255 else
10256 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10257 }
10258 else
10259 {
10260 if (h->verinfo.vertree == NULL)
10261 iversym.vs_vers = 1;
10262 else
10263 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10264 if (flinfo->info->create_default_symver)
3e3b46e5 10265 iversym.vs_vers++;
c152c796
AM
10266 }
10267
422f1182 10268 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10269 defined locally. */
422f1182 10270 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10271 iversym.vs_vers |= VERSYM_HIDDEN;
10272
8b127cbc 10273 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10274 eversym += h->dynindx;
8b127cbc 10275 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10276 }
10277 }
10278
d983c8c5
AM
10279 /* If the symbol is undefined, and we didn't output it to .dynsym,
10280 strip it from .symtab too. Obviously we can't do this for
10281 relocatable output or when needed for --emit-relocs. */
10282 else if (input_sec == bfd_und_section_ptr
10283 && h->indx != -2
66cae560
NC
10284 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10285 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10286 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10287 return TRUE;
66cae560 10288
d983c8c5
AM
10289 /* Also strip others that we couldn't earlier due to dynamic symbol
10290 processing. */
10291 if (strip)
10292 return TRUE;
10293 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10294 return TRUE;
10295
2ec55de3
AM
10296 /* Output a FILE symbol so that following locals are not associated
10297 with the wrong input file. We need one for forced local symbols
10298 if we've seen more than one FILE symbol or when we have exactly
10299 one FILE symbol but global symbols are present in a file other
10300 than the one with the FILE symbol. We also need one if linker
10301 defined symbols are present. In practice these conditions are
10302 always met, so just emit the FILE symbol unconditionally. */
10303 if (eoinfo->localsyms
10304 && !eoinfo->file_sym_done
10305 && eoinfo->flinfo->filesym_count != 0)
10306 {
10307 Elf_Internal_Sym fsym;
10308
10309 memset (&fsym, 0, sizeof (fsym));
10310 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10311 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10312 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10313 bfd_und_section_ptr, NULL))
2ec55de3
AM
10314 return FALSE;
10315
10316 eoinfo->file_sym_done = TRUE;
10317 }
10318
8b127cbc 10319 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10320 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10321 input_sec, h);
6e0b88f1 10322 if (ret == 0)
c152c796
AM
10323 {
10324 eoinfo->failed = TRUE;
10325 return FALSE;
10326 }
6e0b88f1
AM
10327 else if (ret == 1)
10328 h->indx = indx;
10329 else if (h->indx == -2)
10330 abort();
c152c796
AM
10331
10332 return TRUE;
10333}
10334
cdd3575c
AM
10335/* Return TRUE if special handling is done for relocs in SEC against
10336 symbols defined in discarded sections. */
10337
c152c796
AM
10338static bfd_boolean
10339elf_section_ignore_discarded_relocs (asection *sec)
10340{
10341 const struct elf_backend_data *bed;
10342
cdd3575c
AM
10343 switch (sec->sec_info_type)
10344 {
dbaa2011
AM
10345 case SEC_INFO_TYPE_STABS:
10346 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10347 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10348 return TRUE;
10349 default:
10350 break;
10351 }
c152c796
AM
10352
10353 bed = get_elf_backend_data (sec->owner);
10354 if (bed->elf_backend_ignore_discarded_relocs != NULL
10355 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10356 return TRUE;
10357
10358 return FALSE;
10359}
10360
9e66c942
AM
10361/* Return a mask saying how ld should treat relocations in SEC against
10362 symbols defined in discarded sections. If this function returns
10363 COMPLAIN set, ld will issue a warning message. If this function
10364 returns PRETEND set, and the discarded section was link-once and the
10365 same size as the kept link-once section, ld will pretend that the
10366 symbol was actually defined in the kept section. Otherwise ld will
10367 zero the reloc (at least that is the intent, but some cooperation by
10368 the target dependent code is needed, particularly for REL targets). */
10369
8a696751
AM
10370unsigned int
10371_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10372{
9e66c942 10373 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10374 return PRETEND;
cdd3575c
AM
10375
10376 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10377 return 0;
cdd3575c
AM
10378
10379 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10380 return 0;
cdd3575c 10381
9e66c942 10382 return COMPLAIN | PRETEND;
cdd3575c
AM
10383}
10384
3d7f7666
L
10385/* Find a match between a section and a member of a section group. */
10386
10387static asection *
c0f00686
L
10388match_group_member (asection *sec, asection *group,
10389 struct bfd_link_info *info)
3d7f7666
L
10390{
10391 asection *first = elf_next_in_group (group);
10392 asection *s = first;
10393
10394 while (s != NULL)
10395 {
c0f00686 10396 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10397 return s;
10398
83180ade 10399 s = elf_next_in_group (s);
3d7f7666
L
10400 if (s == first)
10401 break;
10402 }
10403
10404 return NULL;
10405}
10406
01b3c8ab 10407/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10408 to replace it. Return the replacement if it is OK. Otherwise return
10409 NULL. */
01b3c8ab
L
10410
10411asection *
c0f00686 10412_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10413{
10414 asection *kept;
10415
10416 kept = sec->kept_section;
10417 if (kept != NULL)
10418 {
c2370991 10419 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10420 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10421 if (kept != NULL
10422 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10423 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10424 kept = NULL;
c2370991 10425 sec->kept_section = kept;
01b3c8ab
L
10426 }
10427 return kept;
10428}
10429
c152c796
AM
10430/* Link an input file into the linker output file. This function
10431 handles all the sections and relocations of the input file at once.
10432 This is so that we only have to read the local symbols once, and
10433 don't have to keep them in memory. */
10434
10435static bfd_boolean
8b127cbc 10436elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10437{
ece5ef60 10438 int (*relocate_section)
c152c796
AM
10439 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10440 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10441 bfd *output_bfd;
10442 Elf_Internal_Shdr *symtab_hdr;
10443 size_t locsymcount;
10444 size_t extsymoff;
10445 Elf_Internal_Sym *isymbuf;
10446 Elf_Internal_Sym *isym;
10447 Elf_Internal_Sym *isymend;
10448 long *pindex;
10449 asection **ppsection;
10450 asection *o;
10451 const struct elf_backend_data *bed;
c152c796 10452 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10453 bfd_size_type address_size;
10454 bfd_vma r_type_mask;
10455 int r_sym_shift;
ffbc01cc 10456 bfd_boolean have_file_sym = FALSE;
c152c796 10457
8b127cbc 10458 output_bfd = flinfo->output_bfd;
c152c796
AM
10459 bed = get_elf_backend_data (output_bfd);
10460 relocate_section = bed->elf_backend_relocate_section;
10461
10462 /* If this is a dynamic object, we don't want to do anything here:
10463 we don't want the local symbols, and we don't want the section
10464 contents. */
10465 if ((input_bfd->flags & DYNAMIC) != 0)
10466 return TRUE;
10467
c152c796
AM
10468 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10469 if (elf_bad_symtab (input_bfd))
10470 {
10471 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10472 extsymoff = 0;
10473 }
10474 else
10475 {
10476 locsymcount = symtab_hdr->sh_info;
10477 extsymoff = symtab_hdr->sh_info;
10478 }
10479
10480 /* Read the local symbols. */
10481 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10482 if (isymbuf == NULL && locsymcount != 0)
10483 {
10484 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10485 flinfo->internal_syms,
10486 flinfo->external_syms,
10487 flinfo->locsym_shndx);
c152c796
AM
10488 if (isymbuf == NULL)
10489 return FALSE;
10490 }
10491
10492 /* Find local symbol sections and adjust values of symbols in
10493 SEC_MERGE sections. Write out those local symbols we know are
10494 going into the output file. */
10495 isymend = isymbuf + locsymcount;
8b127cbc 10496 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10497 isym < isymend;
10498 isym++, pindex++, ppsection++)
10499 {
10500 asection *isec;
10501 const char *name;
10502 Elf_Internal_Sym osym;
6e0b88f1
AM
10503 long indx;
10504 int ret;
c152c796
AM
10505
10506 *pindex = -1;
10507
10508 if (elf_bad_symtab (input_bfd))
10509 {
10510 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10511 {
10512 *ppsection = NULL;
10513 continue;
10514 }
10515 }
10516
10517 if (isym->st_shndx == SHN_UNDEF)
10518 isec = bfd_und_section_ptr;
c152c796
AM
10519 else if (isym->st_shndx == SHN_ABS)
10520 isec = bfd_abs_section_ptr;
10521 else if (isym->st_shndx == SHN_COMMON)
10522 isec = bfd_com_section_ptr;
10523 else
10524 {
cb33740c
AM
10525 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10526 if (isec == NULL)
10527 {
10528 /* Don't attempt to output symbols with st_shnx in the
10529 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10530 isec = bfd_und_section_ptr;
cb33740c 10531 }
dbaa2011 10532 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10533 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10534 isym->st_value =
10535 _bfd_merged_section_offset (output_bfd, &isec,
10536 elf_section_data (isec)->sec_info,
10537 isym->st_value);
c152c796
AM
10538 }
10539
10540 *ppsection = isec;
10541
d983c8c5
AM
10542 /* Don't output the first, undefined, symbol. In fact, don't
10543 output any undefined local symbol. */
10544 if (isec == bfd_und_section_ptr)
c152c796
AM
10545 continue;
10546
10547 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10548 {
10549 /* We never output section symbols. Instead, we use the
10550 section symbol of the corresponding section in the output
10551 file. */
10552 continue;
10553 }
10554
10555 /* If we are stripping all symbols, we don't want to output this
10556 one. */
8b127cbc 10557 if (flinfo->info->strip == strip_all)
c152c796
AM
10558 continue;
10559
10560 /* If we are discarding all local symbols, we don't want to
10561 output this one. If we are generating a relocatable output
10562 file, then some of the local symbols may be required by
10563 relocs; we output them below as we discover that they are
10564 needed. */
8b127cbc 10565 if (flinfo->info->discard == discard_all)
c152c796
AM
10566 continue;
10567
10568 /* If this symbol is defined in a section which we are
f02571c5
AM
10569 discarding, we don't need to keep it. */
10570 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10571 && isym->st_shndx < SHN_LORESERVE
10572 && bfd_section_removed_from_list (output_bfd,
10573 isec->output_section))
e75a280b
L
10574 continue;
10575
c152c796
AM
10576 /* Get the name of the symbol. */
10577 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10578 isym->st_name);
10579 if (name == NULL)
10580 return FALSE;
10581
10582 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10583 if ((flinfo->info->strip == strip_some
10584 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10585 == NULL))
8b127cbc 10586 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10587 && (isec->flags & SEC_MERGE)
10588 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10589 || flinfo->info->discard == discard_l)
c152c796
AM
10590 && bfd_is_local_label_name (input_bfd, name)))
10591 continue;
10592
ffbc01cc
AM
10593 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10594 {
ce875075
AM
10595 if (input_bfd->lto_output)
10596 /* -flto puts a temp file name here. This means builds
10597 are not reproducible. Discard the symbol. */
10598 continue;
ffbc01cc
AM
10599 have_file_sym = TRUE;
10600 flinfo->filesym_count += 1;
10601 }
10602 if (!have_file_sym)
10603 {
10604 /* In the absence of debug info, bfd_find_nearest_line uses
10605 FILE symbols to determine the source file for local
10606 function symbols. Provide a FILE symbol here if input
10607 files lack such, so that their symbols won't be
10608 associated with a previous input file. It's not the
10609 source file, but the best we can do. */
10610 have_file_sym = TRUE;
10611 flinfo->filesym_count += 1;
10612 memset (&osym, 0, sizeof (osym));
10613 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10614 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10615 if (!elf_link_output_symstrtab (flinfo,
10616 (input_bfd->lto_output ? NULL
10617 : input_bfd->filename),
10618 &osym, bfd_abs_section_ptr,
10619 NULL))
ffbc01cc
AM
10620 return FALSE;
10621 }
10622
c152c796
AM
10623 osym = *isym;
10624
10625 /* Adjust the section index for the output file. */
10626 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10627 isec->output_section);
10628 if (osym.st_shndx == SHN_BAD)
10629 return FALSE;
10630
c152c796
AM
10631 /* ELF symbols in relocatable files are section relative, but
10632 in executable files they are virtual addresses. Note that
10633 this code assumes that all ELF sections have an associated
10634 BFD section with a reasonable value for output_offset; below
10635 we assume that they also have a reasonable value for
10636 output_section. Any special sections must be set up to meet
10637 these requirements. */
10638 osym.st_value += isec->output_offset;
0e1862bb 10639 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10640 {
10641 osym.st_value += isec->output_section->vma;
10642 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10643 {
10644 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10645 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10646 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10647 else
10648 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10649 STT_NOTYPE);
c152c796
AM
10650 }
10651 }
10652
6e0b88f1 10653 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10654 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10655 if (ret == 0)
c152c796 10656 return FALSE;
6e0b88f1
AM
10657 else if (ret == 1)
10658 *pindex = indx;
c152c796
AM
10659 }
10660
310fd250
L
10661 if (bed->s->arch_size == 32)
10662 {
10663 r_type_mask = 0xff;
10664 r_sym_shift = 8;
10665 address_size = 4;
10666 }
10667 else
10668 {
10669 r_type_mask = 0xffffffff;
10670 r_sym_shift = 32;
10671 address_size = 8;
10672 }
10673
c152c796
AM
10674 /* Relocate the contents of each section. */
10675 sym_hashes = elf_sym_hashes (input_bfd);
10676 for (o = input_bfd->sections; o != NULL; o = o->next)
10677 {
10678 bfd_byte *contents;
10679
10680 if (! o->linker_mark)
10681 {
10682 /* This section was omitted from the link. */
10683 continue;
10684 }
10685
7bdf4127 10686 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10687 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10688 {
10689 /* Deal with the group signature symbol. */
10690 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10691 unsigned long symndx = sec_data->this_hdr.sh_info;
10692 asection *osec = o->output_section;
10693
7bdf4127 10694 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10695 if (symndx >= locsymcount
10696 || (elf_bad_symtab (input_bfd)
8b127cbc 10697 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10698 {
10699 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10700 while (h->root.type == bfd_link_hash_indirect
10701 || h->root.type == bfd_link_hash_warning)
10702 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10703 /* Arrange for symbol to be output. */
10704 h->indx = -2;
10705 elf_section_data (osec)->this_hdr.sh_info = -2;
10706 }
10707 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10708 {
10709 /* We'll use the output section target_index. */
8b127cbc 10710 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10711 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10712 }
10713 else
10714 {
8b127cbc 10715 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10716 {
10717 /* Otherwise output the local symbol now. */
10718 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10719 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10720 const char *name;
6e0b88f1
AM
10721 long indx;
10722 int ret;
bcacc0f5
AM
10723
10724 name = bfd_elf_string_from_elf_section (input_bfd,
10725 symtab_hdr->sh_link,
10726 sym.st_name);
10727 if (name == NULL)
10728 return FALSE;
10729
10730 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10731 sec);
10732 if (sym.st_shndx == SHN_BAD)
10733 return FALSE;
10734
10735 sym.st_value += o->output_offset;
10736
6e0b88f1 10737 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10738 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10739 NULL);
6e0b88f1 10740 if (ret == 0)
bcacc0f5 10741 return FALSE;
6e0b88f1 10742 else if (ret == 1)
8b127cbc 10743 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10744 else
10745 abort ();
bcacc0f5
AM
10746 }
10747 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10748 = flinfo->indices[symndx];
bcacc0f5
AM
10749 }
10750 }
10751
c152c796 10752 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10753 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10754 continue;
10755
10756 if ((o->flags & SEC_LINKER_CREATED) != 0)
10757 {
10758 /* Section was created by _bfd_elf_link_create_dynamic_sections
10759 or somesuch. */
10760 continue;
10761 }
10762
10763 /* Get the contents of the section. They have been cached by a
10764 relaxation routine. Note that o is a section in an input
10765 file, so the contents field will not have been set by any of
10766 the routines which work on output files. */
10767 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10768 {
10769 contents = elf_section_data (o)->this_hdr.contents;
10770 if (bed->caches_rawsize
10771 && o->rawsize != 0
10772 && o->rawsize < o->size)
10773 {
10774 memcpy (flinfo->contents, contents, o->rawsize);
10775 contents = flinfo->contents;
10776 }
10777 }
c152c796
AM
10778 else
10779 {
8b127cbc 10780 contents = flinfo->contents;
4a114e3e 10781 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10782 return FALSE;
10783 }
10784
10785 if ((o->flags & SEC_RELOC) != 0)
10786 {
10787 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10788 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10789 int action_discarded;
ece5ef60 10790 int ret;
c152c796
AM
10791
10792 /* Get the swapped relocs. */
10793 internal_relocs
8b127cbc
AM
10794 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10795 flinfo->internal_relocs, FALSE);
c152c796
AM
10796 if (internal_relocs == NULL
10797 && o->reloc_count > 0)
10798 return FALSE;
10799
310fd250
L
10800 /* We need to reverse-copy input .ctors/.dtors sections if
10801 they are placed in .init_array/.finit_array for output. */
10802 if (o->size > address_size
10803 && ((strncmp (o->name, ".ctors", 6) == 0
10804 && strcmp (o->output_section->name,
10805 ".init_array") == 0)
10806 || (strncmp (o->name, ".dtors", 6) == 0
10807 && strcmp (o->output_section->name,
10808 ".fini_array") == 0))
10809 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10810 {
056bafd4
MR
10811 if (o->size * bed->s->int_rels_per_ext_rel
10812 != o->reloc_count * address_size)
310fd250 10813 {
4eca0228 10814 _bfd_error_handler
695344c0 10815 /* xgettext:c-format */
871b3ab2 10816 (_("error: %pB: size of section %pA is not "
310fd250
L
10817 "multiple of address size"),
10818 input_bfd, o);
8c6716e5 10819 bfd_set_error (bfd_error_bad_value);
310fd250
L
10820 return FALSE;
10821 }
10822 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10823 }
10824
0f02bbd9 10825 action_discarded = -1;
c152c796 10826 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10827 action_discarded = (*bed->action_discarded) (o);
10828
10829 /* Run through the relocs evaluating complex reloc symbols and
10830 looking for relocs against symbols from discarded sections
10831 or section symbols from removed link-once sections.
10832 Complain about relocs against discarded sections. Zero
10833 relocs against removed link-once sections. */
10834
10835 rel = internal_relocs;
056bafd4 10836 relend = rel + o->reloc_count;
0f02bbd9 10837 for ( ; rel < relend; rel++)
c152c796 10838 {
0f02bbd9
AM
10839 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10840 unsigned int s_type;
10841 asection **ps, *sec;
10842 struct elf_link_hash_entry *h = NULL;
10843 const char *sym_name;
c152c796 10844
0f02bbd9
AM
10845 if (r_symndx == STN_UNDEF)
10846 continue;
c152c796 10847
0f02bbd9
AM
10848 if (r_symndx >= locsymcount
10849 || (elf_bad_symtab (input_bfd)
8b127cbc 10850 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10851 {
10852 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10853
0f02bbd9
AM
10854 /* Badly formatted input files can contain relocs that
10855 reference non-existant symbols. Check here so that
10856 we do not seg fault. */
10857 if (h == NULL)
c152c796 10858 {
4eca0228 10859 _bfd_error_handler
695344c0 10860 /* xgettext:c-format */
2dcf00ce 10861 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10862 "that references a non-existent global symbol"),
2dcf00ce 10863 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10864 bfd_set_error (bfd_error_bad_value);
10865 return FALSE;
10866 }
3b36f7e6 10867
0f02bbd9
AM
10868 while (h->root.type == bfd_link_hash_indirect
10869 || h->root.type == bfd_link_hash_warning)
10870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10871
0f02bbd9 10872 s_type = h->type;
cdd3575c 10873
9e2dec47 10874 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10875 mark the symbol as undefined. Note that the
10876 linker may attach linker created dynamic sections
10877 to the plugin bfd. Symbols defined in linker
10878 created sections are not plugin symbols. */
bc4e12de 10879 if ((h->root.non_ir_ref_regular
4070765b 10880 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10881 && (h->root.type == bfd_link_hash_defined
10882 || h->root.type == bfd_link_hash_defweak)
10883 && (h->root.u.def.section->flags
10884 & SEC_LINKER_CREATED) == 0
10885 && h->root.u.def.section->owner != NULL
10886 && (h->root.u.def.section->owner->flags
10887 & BFD_PLUGIN) != 0)
10888 {
10889 h->root.type = bfd_link_hash_undefined;
10890 h->root.u.undef.abfd = h->root.u.def.section->owner;
10891 }
10892
0f02bbd9
AM
10893 ps = NULL;
10894 if (h->root.type == bfd_link_hash_defined
10895 || h->root.type == bfd_link_hash_defweak)
10896 ps = &h->root.u.def.section;
10897
10898 sym_name = h->root.root.string;
10899 }
10900 else
10901 {
10902 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10903
10904 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10905 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10906 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10907 sym, *ps);
10908 }
c152c796 10909
c301e700 10910 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10911 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10912 {
10913 bfd_vma val;
10914 bfd_vma dot = (rel->r_offset
10915 + o->output_offset + o->output_section->vma);
10916#ifdef DEBUG
10917 printf ("Encountered a complex symbol!");
10918 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10919 input_bfd->filename, o->name,
10920 (long) (rel - internal_relocs));
0f02bbd9
AM
10921 printf (" symbol: idx %8.8lx, name %s\n",
10922 r_symndx, sym_name);
10923 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10924 (unsigned long) rel->r_info,
10925 (unsigned long) rel->r_offset);
10926#endif
8b127cbc 10927 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10928 isymbuf, locsymcount, s_type == STT_SRELC))
10929 return FALSE;
10930
10931 /* Symbol evaluated OK. Update to absolute value. */
10932 set_symbol_value (input_bfd, isymbuf, locsymcount,
10933 r_symndx, val);
10934 continue;
10935 }
10936
10937 if (action_discarded != -1 && ps != NULL)
10938 {
cdd3575c
AM
10939 /* Complain if the definition comes from a
10940 discarded section. */
dbaa2011 10941 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10942 {
cf35638d 10943 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10944 if (action_discarded & COMPLAIN)
8b127cbc 10945 (*flinfo->info->callbacks->einfo)
695344c0 10946 /* xgettext:c-format */
871b3ab2
AM
10947 (_("%X`%s' referenced in section `%pA' of %pB: "
10948 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10949 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10950
87e5235d 10951 /* Try to do the best we can to support buggy old
e0ae6d6f 10952 versions of gcc. Pretend that the symbol is
87e5235d
AM
10953 really defined in the kept linkonce section.
10954 FIXME: This is quite broken. Modifying the
10955 symbol here means we will be changing all later
e0ae6d6f 10956 uses of the symbol, not just in this section. */
0f02bbd9 10957 if (action_discarded & PRETEND)
87e5235d 10958 {
01b3c8ab
L
10959 asection *kept;
10960
c0f00686 10961 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10962 flinfo->info);
01b3c8ab 10963 if (kept != NULL)
87e5235d
AM
10964 {
10965 *ps = kept;
10966 continue;
10967 }
10968 }
c152c796
AM
10969 }
10970 }
10971 }
10972
10973 /* Relocate the section by invoking a back end routine.
10974
10975 The back end routine is responsible for adjusting the
10976 section contents as necessary, and (if using Rela relocs
10977 and generating a relocatable output file) adjusting the
10978 reloc addend as necessary.
10979
10980 The back end routine does not have to worry about setting
10981 the reloc address or the reloc symbol index.
10982
10983 The back end routine is given a pointer to the swapped in
10984 internal symbols, and can access the hash table entries
10985 for the external symbols via elf_sym_hashes (input_bfd).
10986
10987 When generating relocatable output, the back end routine
10988 must handle STB_LOCAL/STT_SECTION symbols specially. The
10989 output symbol is going to be a section symbol
10990 corresponding to the output section, which will require
10991 the addend to be adjusted. */
10992
8b127cbc 10993 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10994 input_bfd, o, contents,
10995 internal_relocs,
10996 isymbuf,
8b127cbc 10997 flinfo->sections);
ece5ef60 10998 if (!ret)
c152c796
AM
10999 return FALSE;
11000
ece5ef60 11001 if (ret == 2
0e1862bb 11002 || bfd_link_relocatable (flinfo->info)
8b127cbc 11003 || flinfo->info->emitrelocations)
c152c796
AM
11004 {
11005 Elf_Internal_Rela *irela;
d4730f92 11006 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11007 bfd_vma last_offset;
11008 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11009 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11010 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11011 unsigned int next_erel;
c152c796 11012 bfd_boolean rela_normal;
d4730f92 11013 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11014
d4730f92
BS
11015 esdi = elf_section_data (o);
11016 esdo = elf_section_data (o->output_section);
11017 rela_normal = FALSE;
c152c796
AM
11018
11019 /* Adjust the reloc addresses and symbol indices. */
11020
11021 irela = internal_relocs;
056bafd4 11022 irelaend = irela + o->reloc_count;
d4730f92
BS
11023 rel_hash = esdo->rel.hashes + esdo->rel.count;
11024 /* We start processing the REL relocs, if any. When we reach
11025 IRELAMID in the loop, we switch to the RELA relocs. */
11026 irelamid = irela;
11027 if (esdi->rel.hdr != NULL)
11028 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11029 * bed->s->int_rels_per_ext_rel);
eac338cf 11030 rel_hash_list = rel_hash;
d4730f92 11031 rela_hash_list = NULL;
c152c796 11032 last_offset = o->output_offset;
0e1862bb 11033 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11034 last_offset += o->output_section->vma;
11035 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11036 {
11037 unsigned long r_symndx;
11038 asection *sec;
11039 Elf_Internal_Sym sym;
11040
11041 if (next_erel == bed->s->int_rels_per_ext_rel)
11042 {
11043 rel_hash++;
11044 next_erel = 0;
11045 }
11046
d4730f92
BS
11047 if (irela == irelamid)
11048 {
11049 rel_hash = esdo->rela.hashes + esdo->rela.count;
11050 rela_hash_list = rel_hash;
11051 rela_normal = bed->rela_normal;
11052 }
11053
c152c796 11054 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11055 flinfo->info, o,
c152c796
AM
11056 irela->r_offset);
11057 if (irela->r_offset >= (bfd_vma) -2)
11058 {
11059 /* This is a reloc for a deleted entry or somesuch.
11060 Turn it into an R_*_NONE reloc, at the same
11061 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11062 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11063 being ordered. */
11064 irela->r_offset = last_offset;
11065 irela->r_info = 0;
11066 irela->r_addend = 0;
11067 continue;
11068 }
11069
11070 irela->r_offset += o->output_offset;
11071
11072 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11073 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11074 irela->r_offset += o->output_section->vma;
11075
11076 last_offset = irela->r_offset;
11077
11078 r_symndx = irela->r_info >> r_sym_shift;
11079 if (r_symndx == STN_UNDEF)
11080 continue;
11081
11082 if (r_symndx >= locsymcount
11083 || (elf_bad_symtab (input_bfd)
8b127cbc 11084 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11085 {
11086 struct elf_link_hash_entry *rh;
11087 unsigned long indx;
11088
11089 /* This is a reloc against a global symbol. We
11090 have not yet output all the local symbols, so
11091 we do not know the symbol index of any global
11092 symbol. We set the rel_hash entry for this
11093 reloc to point to the global hash table entry
11094 for this symbol. The symbol index is then
ee75fd95 11095 set at the end of bfd_elf_final_link. */
c152c796
AM
11096 indx = r_symndx - extsymoff;
11097 rh = elf_sym_hashes (input_bfd)[indx];
11098 while (rh->root.type == bfd_link_hash_indirect
11099 || rh->root.type == bfd_link_hash_warning)
11100 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11101
11102 /* Setting the index to -2 tells
11103 elf_link_output_extsym that this symbol is
11104 used by a reloc. */
11105 BFD_ASSERT (rh->indx < 0);
11106 rh->indx = -2;
c152c796
AM
11107 *rel_hash = rh;
11108
11109 continue;
11110 }
11111
11112 /* This is a reloc against a local symbol. */
11113
11114 *rel_hash = NULL;
11115 sym = isymbuf[r_symndx];
8b127cbc 11116 sec = flinfo->sections[r_symndx];
c152c796
AM
11117 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11118 {
11119 /* I suppose the backend ought to fill in the
11120 section of any STT_SECTION symbol against a
6a8d1586 11121 processor specific section. */
cf35638d 11122 r_symndx = STN_UNDEF;
6a8d1586
AM
11123 if (bfd_is_abs_section (sec))
11124 ;
c152c796
AM
11125 else if (sec == NULL || sec->owner == NULL)
11126 {
11127 bfd_set_error (bfd_error_bad_value);
11128 return FALSE;
11129 }
11130 else
11131 {
6a8d1586
AM
11132 asection *osec = sec->output_section;
11133
11134 /* If we have discarded a section, the output
11135 section will be the absolute section. In
ab96bf03
AM
11136 case of discarded SEC_MERGE sections, use
11137 the kept section. relocate_section should
11138 have already handled discarded linkonce
11139 sections. */
6a8d1586
AM
11140 if (bfd_is_abs_section (osec)
11141 && sec->kept_section != NULL
11142 && sec->kept_section->output_section != NULL)
11143 {
11144 osec = sec->kept_section->output_section;
11145 irela->r_addend -= osec->vma;
11146 }
11147
11148 if (!bfd_is_abs_section (osec))
11149 {
11150 r_symndx = osec->target_index;
cf35638d 11151 if (r_symndx == STN_UNDEF)
74541ad4 11152 {
051d833a
AM
11153 irela->r_addend += osec->vma;
11154 osec = _bfd_nearby_section (output_bfd, osec,
11155 osec->vma);
11156 irela->r_addend -= osec->vma;
11157 r_symndx = osec->target_index;
74541ad4 11158 }
6a8d1586 11159 }
c152c796
AM
11160 }
11161
11162 /* Adjust the addend according to where the
11163 section winds up in the output section. */
11164 if (rela_normal)
11165 irela->r_addend += sec->output_offset;
11166 }
11167 else
11168 {
8b127cbc 11169 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11170 {
11171 unsigned long shlink;
11172 const char *name;
11173 asection *osec;
6e0b88f1 11174 long indx;
c152c796 11175
8b127cbc 11176 if (flinfo->info->strip == strip_all)
c152c796
AM
11177 {
11178 /* You can't do ld -r -s. */
11179 bfd_set_error (bfd_error_invalid_operation);
11180 return FALSE;
11181 }
11182
11183 /* This symbol was skipped earlier, but
11184 since it is needed by a reloc, we
11185 must output it now. */
11186 shlink = symtab_hdr->sh_link;
11187 name = (bfd_elf_string_from_elf_section
11188 (input_bfd, shlink, sym.st_name));
11189 if (name == NULL)
11190 return FALSE;
11191
11192 osec = sec->output_section;
11193 sym.st_shndx =
11194 _bfd_elf_section_from_bfd_section (output_bfd,
11195 osec);
11196 if (sym.st_shndx == SHN_BAD)
11197 return FALSE;
11198
11199 sym.st_value += sec->output_offset;
0e1862bb 11200 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11201 {
11202 sym.st_value += osec->vma;
11203 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11204 {
102def4d
AM
11205 struct elf_link_hash_table *htab
11206 = elf_hash_table (flinfo->info);
11207
c152c796
AM
11208 /* STT_TLS symbols are relative to PT_TLS
11209 segment base. */
102def4d
AM
11210 if (htab->tls_sec != NULL)
11211 sym.st_value -= htab->tls_sec->vma;
11212 else
11213 sym.st_info
11214 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11215 STT_NOTYPE);
c152c796
AM
11216 }
11217 }
11218
6e0b88f1 11219 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11220 ret = elf_link_output_symstrtab (flinfo, name,
11221 &sym, sec,
11222 NULL);
6e0b88f1 11223 if (ret == 0)
c152c796 11224 return FALSE;
6e0b88f1 11225 else if (ret == 1)
8b127cbc 11226 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11227 else
11228 abort ();
c152c796
AM
11229 }
11230
8b127cbc 11231 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11232 }
11233
11234 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11235 | (irela->r_info & r_type_mask));
11236 }
11237
11238 /* Swap out the relocs. */
d4730f92
BS
11239 input_rel_hdr = esdi->rel.hdr;
11240 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11241 {
d4730f92
BS
11242 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11243 input_rel_hdr,
11244 internal_relocs,
11245 rel_hash_list))
11246 return FALSE;
c152c796
AM
11247 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11248 * bed->s->int_rels_per_ext_rel);
eac338cf 11249 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11250 }
11251
11252 input_rela_hdr = esdi->rela.hdr;
11253 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11254 {
eac338cf 11255 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11256 input_rela_hdr,
eac338cf 11257 internal_relocs,
d4730f92 11258 rela_hash_list))
c152c796
AM
11259 return FALSE;
11260 }
11261 }
11262 }
11263
11264 /* Write out the modified section contents. */
11265 if (bed->elf_backend_write_section
8b127cbc 11266 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11267 contents))
c152c796
AM
11268 {
11269 /* Section written out. */
11270 }
11271 else switch (o->sec_info_type)
11272 {
dbaa2011 11273 case SEC_INFO_TYPE_STABS:
c152c796
AM
11274 if (! (_bfd_write_section_stabs
11275 (output_bfd,
8b127cbc 11276 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11277 o, &elf_section_data (o)->sec_info, contents)))
11278 return FALSE;
11279 break;
dbaa2011 11280 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11281 if (! _bfd_write_merged_section (output_bfd, o,
11282 elf_section_data (o)->sec_info))
11283 return FALSE;
11284 break;
dbaa2011 11285 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11286 {
8b127cbc 11287 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11288 o, contents))
11289 return FALSE;
11290 }
11291 break;
2f0c68f2
CM
11292 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11293 {
11294 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11295 flinfo->info,
11296 o, contents))
11297 return FALSE;
11298 }
11299 break;
c152c796
AM
11300 default:
11301 {
310fd250
L
11302 if (! (o->flags & SEC_EXCLUDE))
11303 {
11304 file_ptr offset = (file_ptr) o->output_offset;
11305 bfd_size_type todo = o->size;
37b01f6a 11306
bb294208 11307 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11308
310fd250
L
11309 if ((o->flags & SEC_ELF_REVERSE_COPY))
11310 {
11311 /* Reverse-copy input section to output. */
11312 do
11313 {
11314 todo -= address_size;
11315 if (! bfd_set_section_contents (output_bfd,
11316 o->output_section,
11317 contents + todo,
11318 offset,
11319 address_size))
11320 return FALSE;
11321 if (todo == 0)
11322 break;
11323 offset += address_size;
11324 }
11325 while (1);
11326 }
11327 else if (! bfd_set_section_contents (output_bfd,
11328 o->output_section,
11329 contents,
11330 offset, todo))
11331 return FALSE;
11332 }
c152c796
AM
11333 }
11334 break;
11335 }
11336 }
11337
11338 return TRUE;
11339}
11340
11341/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11342 requested by the linker, and does not come from any input file. This
c152c796
AM
11343 is used to build constructor and destructor tables when linking
11344 with -Ur. */
11345
11346static bfd_boolean
11347elf_reloc_link_order (bfd *output_bfd,
11348 struct bfd_link_info *info,
11349 asection *output_section,
11350 struct bfd_link_order *link_order)
11351{
11352 reloc_howto_type *howto;
11353 long indx;
11354 bfd_vma offset;
11355 bfd_vma addend;
d4730f92 11356 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11357 struct elf_link_hash_entry **rel_hash_ptr;
11358 Elf_Internal_Shdr *rel_hdr;
11359 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11360 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11361 bfd_byte *erel;
11362 unsigned int i;
d4730f92 11363 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11364
11365 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11366 if (howto == NULL)
11367 {
11368 bfd_set_error (bfd_error_bad_value);
11369 return FALSE;
11370 }
11371
11372 addend = link_order->u.reloc.p->addend;
11373
d4730f92
BS
11374 if (esdo->rel.hdr)
11375 reldata = &esdo->rel;
11376 else if (esdo->rela.hdr)
11377 reldata = &esdo->rela;
11378 else
11379 {
11380 reldata = NULL;
11381 BFD_ASSERT (0);
11382 }
11383
c152c796 11384 /* Figure out the symbol index. */
d4730f92 11385 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11386 if (link_order->type == bfd_section_reloc_link_order)
11387 {
11388 indx = link_order->u.reloc.p->u.section->target_index;
11389 BFD_ASSERT (indx != 0);
11390 *rel_hash_ptr = NULL;
11391 }
11392 else
11393 {
11394 struct elf_link_hash_entry *h;
11395
11396 /* Treat a reloc against a defined symbol as though it were
11397 actually against the section. */
11398 h = ((struct elf_link_hash_entry *)
11399 bfd_wrapped_link_hash_lookup (output_bfd, info,
11400 link_order->u.reloc.p->u.name,
11401 FALSE, FALSE, TRUE));
11402 if (h != NULL
11403 && (h->root.type == bfd_link_hash_defined
11404 || h->root.type == bfd_link_hash_defweak))
11405 {
11406 asection *section;
11407
11408 section = h->root.u.def.section;
11409 indx = section->output_section->target_index;
11410 *rel_hash_ptr = NULL;
11411 /* It seems that we ought to add the symbol value to the
11412 addend here, but in practice it has already been added
11413 because it was passed to constructor_callback. */
11414 addend += section->output_section->vma + section->output_offset;
11415 }
11416 else if (h != NULL)
11417 {
11418 /* Setting the index to -2 tells elf_link_output_extsym that
11419 this symbol is used by a reloc. */
11420 h->indx = -2;
11421 *rel_hash_ptr = h;
11422 indx = 0;
11423 }
11424 else
11425 {
1a72702b
AM
11426 (*info->callbacks->unattached_reloc)
11427 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11428 indx = 0;
11429 }
11430 }
11431
11432 /* If this is an inplace reloc, we must write the addend into the
11433 object file. */
11434 if (howto->partial_inplace && addend != 0)
11435 {
11436 bfd_size_type size;
11437 bfd_reloc_status_type rstat;
11438 bfd_byte *buf;
11439 bfd_boolean ok;
11440 const char *sym_name;
bb294208 11441 bfd_size_type octets;
c152c796 11442
a50b1753
NC
11443 size = (bfd_size_type) bfd_get_reloc_size (howto);
11444 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11445 if (buf == NULL && size != 0)
c152c796
AM
11446 return FALSE;
11447 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11448 switch (rstat)
11449 {
11450 case bfd_reloc_ok:
11451 break;
11452
11453 default:
11454 case bfd_reloc_outofrange:
11455 abort ();
11456
11457 case bfd_reloc_overflow:
11458 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11459 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11460 else
11461 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11462 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11463 howto->name, addend, NULL, NULL,
11464 (bfd_vma) 0);
c152c796
AM
11465 break;
11466 }
37b01f6a 11467
bb294208
AM
11468 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11469 output_section);
c152c796 11470 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11471 octets, size);
c152c796
AM
11472 free (buf);
11473 if (! ok)
11474 return FALSE;
11475 }
11476
11477 /* The address of a reloc is relative to the section in a
11478 relocatable file, and is a virtual address in an executable
11479 file. */
11480 offset = link_order->offset;
0e1862bb 11481 if (! bfd_link_relocatable (info))
c152c796
AM
11482 offset += output_section->vma;
11483
11484 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11485 {
11486 irel[i].r_offset = offset;
11487 irel[i].r_info = 0;
11488 irel[i].r_addend = 0;
11489 }
11490 if (bed->s->arch_size == 32)
11491 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11492 else
11493 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11494
d4730f92 11495 rel_hdr = reldata->hdr;
c152c796
AM
11496 erel = rel_hdr->contents;
11497 if (rel_hdr->sh_type == SHT_REL)
11498 {
d4730f92 11499 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11500 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11501 }
11502 else
11503 {
11504 irel[0].r_addend = addend;
d4730f92 11505 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11506 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11507 }
11508
d4730f92 11509 ++reldata->count;
c152c796
AM
11510
11511 return TRUE;
11512}
11513
0b52efa6 11514
0b52efa6
PB
11515/* Compare two sections based on the locations of the sections they are
11516 linked to. Used by elf_fixup_link_order. */
11517
11518static int
8c1c5e5d 11519compare_link_order (const void *a, const void *b)
0b52efa6 11520{
8c1c5e5d
AM
11521 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11522 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11523 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11524 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11525 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11526 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11527
0b52efa6
PB
11528 if (apos < bpos)
11529 return -1;
8c1c5e5d
AM
11530 if (apos > bpos)
11531 return 1;
11532
11533 /* The only way we should get matching LMAs is when the first of two
11534 sections has zero size. */
11535 if (asec->size < bsec->size)
11536 return -1;
11537 if (asec->size > bsec->size)
11538 return 1;
11539
11540 /* If they are both zero size then they almost certainly have the same
11541 VMA and thus are not ordered with respect to each other. Test VMA
11542 anyway, and fall back to id to make the result reproducible across
11543 qsort implementations. */
11544 apos = asec->output_section->vma + asec->output_offset;
11545 bpos = bsec->output_section->vma + bsec->output_offset;
11546 if (apos < bpos)
11547 return -1;
11548 if (apos > bpos)
11549 return 1;
11550
11551 return asec->id - bsec->id;
0b52efa6
PB
11552}
11553
11554
11555/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11556 order as their linked sections. Returns false if this could not be done
11557 because an output section includes both ordered and unordered
11558 sections. Ideally we'd do this in the linker proper. */
11559
11560static bfd_boolean
11561elf_fixup_link_order (bfd *abfd, asection *o)
11562{
8c1c5e5d
AM
11563 size_t seen_linkorder;
11564 size_t seen_other;
11565 size_t n;
0b52efa6
PB
11566 struct bfd_link_order *p;
11567 bfd *sub;
0b52efa6 11568 struct bfd_link_order **sections;
d33cdfe3 11569 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11570 bfd_vma offset;
3b36f7e6 11571
d33cdfe3
L
11572 other_sec = NULL;
11573 linkorder_sec = NULL;
0b52efa6
PB
11574 seen_other = 0;
11575 seen_linkorder = 0;
8423293d 11576 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11577 {
d33cdfe3 11578 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11579 {
11580 s = p->u.indirect.section;
d33cdfe3 11581 sub = s->owner;
847d5183
AM
11582 if ((s->flags & SEC_LINKER_CREATED) == 0
11583 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11584 && elf_section_data (s) != NULL
11585 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11586 {
11587 seen_linkorder++;
11588 linkorder_sec = s;
11589 }
0b52efa6 11590 else
d33cdfe3
L
11591 {
11592 seen_other++;
11593 other_sec = s;
11594 }
0b52efa6
PB
11595 }
11596 else
11597 seen_other++;
d33cdfe3
L
11598
11599 if (seen_other && seen_linkorder)
11600 {
11601 if (other_sec && linkorder_sec)
4eca0228 11602 _bfd_error_handler
695344c0 11603 /* xgettext:c-format */
871b3ab2
AM
11604 (_("%pA has both ordered [`%pA' in %pB] "
11605 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11606 o, linkorder_sec, linkorder_sec->owner,
11607 other_sec, other_sec->owner);
d33cdfe3 11608 else
4eca0228 11609 _bfd_error_handler
871b3ab2 11610 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11611 bfd_set_error (bfd_error_bad_value);
11612 return FALSE;
11613 }
0b52efa6
PB
11614 }
11615
11616 if (!seen_linkorder)
11617 return TRUE;
11618
8c1c5e5d 11619 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11620 if (sections == NULL)
11621 return FALSE;
3b36f7e6 11622
8c1c5e5d 11623 seen_linkorder = 0;
8423293d 11624 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11625 sections[seen_linkorder++] = p;
11626
0b52efa6 11627 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11628 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11629
11630 /* Change the offsets of the sections. */
11631 offset = 0;
11632 for (n = 0; n < seen_linkorder; n++)
11633 {
8c1c5e5d 11634 bfd_vma mask;
0b52efa6 11635 s = sections[n]->u.indirect.section;
8c1c5e5d
AM
11636 mask = ~(bfd_vma) 0 << s->alignment_power;
11637 offset = (offset + ~mask) & mask;
bb294208 11638 s->output_offset = offset / bfd_octets_per_byte (abfd, s);
0b52efa6
PB
11639 sections[n]->offset = offset;
11640 offset += sections[n]->size;
11641 }
11642
4dd07732 11643 free (sections);
0b52efa6
PB
11644 return TRUE;
11645}
11646
76359541
TP
11647/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11648 Returns TRUE upon success, FALSE otherwise. */
11649
11650static bfd_boolean
11651elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11652{
11653 bfd_boolean ret = FALSE;
11654 bfd *implib_bfd;
11655 const struct elf_backend_data *bed;
11656 flagword flags;
11657 enum bfd_architecture arch;
11658 unsigned int mach;
11659 asymbol **sympp = NULL;
11660 long symsize;
11661 long symcount;
11662 long src_count;
11663 elf_symbol_type *osymbuf;
11664
11665 implib_bfd = info->out_implib_bfd;
11666 bed = get_elf_backend_data (abfd);
11667
11668 if (!bfd_set_format (implib_bfd, bfd_object))
11669 return FALSE;
11670
046734ff 11671 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11672 flags = bfd_get_file_flags (abfd);
11673 flags &= ~HAS_RELOC;
11674 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11675 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11676 return FALSE;
11677
11678 /* Copy architecture of output file to import library file. */
11679 arch = bfd_get_arch (abfd);
11680 mach = bfd_get_mach (abfd);
11681 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11682 && (abfd->target_defaulted
11683 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11684 return FALSE;
11685
11686 /* Get symbol table size. */
11687 symsize = bfd_get_symtab_upper_bound (abfd);
11688 if (symsize < 0)
11689 return FALSE;
11690
11691 /* Read in the symbol table. */
ec9bd0a2
AM
11692 sympp = (asymbol **) bfd_malloc (symsize);
11693 if (sympp == NULL)
11694 return FALSE;
11695
76359541
TP
11696 symcount = bfd_canonicalize_symtab (abfd, sympp);
11697 if (symcount < 0)
11698 goto free_sym_buf;
11699
11700 /* Allow the BFD backend to copy any private header data it
11701 understands from the output BFD to the import library BFD. */
11702 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11703 goto free_sym_buf;
11704
11705 /* Filter symbols to appear in the import library. */
11706 if (bed->elf_backend_filter_implib_symbols)
11707 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11708 symcount);
11709 else
11710 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11711 if (symcount == 0)
11712 {
5df1bc57 11713 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11714 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11715 implib_bfd);
76359541
TP
11716 goto free_sym_buf;
11717 }
11718
11719
11720 /* Make symbols absolute. */
11721 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11722 sizeof (*osymbuf));
ec9bd0a2
AM
11723 if (osymbuf == NULL)
11724 goto free_sym_buf;
11725
76359541
TP
11726 for (src_count = 0; src_count < symcount; src_count++)
11727 {
11728 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11729 sizeof (*osymbuf));
11730 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11731 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11732 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11733 osymbuf[src_count].internal_elf_sym.st_value =
11734 osymbuf[src_count].symbol.value;
11735 sympp[src_count] = &osymbuf[src_count].symbol;
11736 }
11737
11738 bfd_set_symtab (implib_bfd, sympp, symcount);
11739
11740 /* Allow the BFD backend to copy any private data it understands
11741 from the output BFD to the import library BFD. This is done last
11742 to permit the routine to look at the filtered symbol table. */
11743 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11744 goto free_sym_buf;
11745
11746 if (!bfd_close (implib_bfd))
11747 goto free_sym_buf;
11748
11749 ret = TRUE;
11750
11751free_sym_buf:
11752 free (sympp);
11753 return ret;
11754}
11755
9f7c3e5e
AM
11756static void
11757elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11758{
11759 asection *o;
11760
11761 if (flinfo->symstrtab != NULL)
ef10c3ac 11762 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11763 if (flinfo->contents != NULL)
11764 free (flinfo->contents);
11765 if (flinfo->external_relocs != NULL)
11766 free (flinfo->external_relocs);
11767 if (flinfo->internal_relocs != NULL)
11768 free (flinfo->internal_relocs);
11769 if (flinfo->external_syms != NULL)
11770 free (flinfo->external_syms);
11771 if (flinfo->locsym_shndx != NULL)
11772 free (flinfo->locsym_shndx);
11773 if (flinfo->internal_syms != NULL)
11774 free (flinfo->internal_syms);
11775 if (flinfo->indices != NULL)
11776 free (flinfo->indices);
11777 if (flinfo->sections != NULL)
11778 free (flinfo->sections);
a0f6fd21
AM
11779 if (flinfo->symshndxbuf != NULL
11780 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
11781 free (flinfo->symshndxbuf);
11782 for (o = obfd->sections; o != NULL; o = o->next)
11783 {
11784 struct bfd_elf_section_data *esdo = elf_section_data (o);
11785 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11786 free (esdo->rel.hashes);
11787 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11788 free (esdo->rela.hashes);
11789 }
11790}
0b52efa6 11791
c152c796
AM
11792/* Do the final step of an ELF link. */
11793
11794bfd_boolean
11795bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11796{
11797 bfd_boolean dynamic;
11798 bfd_boolean emit_relocs;
11799 bfd *dynobj;
8b127cbc 11800 struct elf_final_link_info flinfo;
91d6fa6a
NC
11801 asection *o;
11802 struct bfd_link_order *p;
11803 bfd *sub;
c152c796
AM
11804 bfd_size_type max_contents_size;
11805 bfd_size_type max_external_reloc_size;
11806 bfd_size_type max_internal_reloc_count;
11807 bfd_size_type max_sym_count;
11808 bfd_size_type max_sym_shndx_count;
c152c796
AM
11809 Elf_Internal_Sym elfsym;
11810 unsigned int i;
11811 Elf_Internal_Shdr *symtab_hdr;
11812 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11813 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11814 struct elf_outext_info eoinfo;
11815 bfd_boolean merged;
11816 size_t relativecount = 0;
11817 asection *reldyn = 0;
11818 bfd_size_type amt;
104d59d1
JM
11819 asection *attr_section = NULL;
11820 bfd_vma attr_size = 0;
11821 const char *std_attrs_section;
64f52338 11822 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 11823 bfd_boolean sections_removed;
c152c796 11824
64f52338 11825 if (!is_elf_hash_table (htab))
c152c796
AM
11826 return FALSE;
11827
0e1862bb 11828 if (bfd_link_pic (info))
c152c796
AM
11829 abfd->flags |= DYNAMIC;
11830
64f52338
AM
11831 dynamic = htab->dynamic_sections_created;
11832 dynobj = htab->dynobj;
c152c796 11833
0e1862bb 11834 emit_relocs = (bfd_link_relocatable (info)
a4676736 11835 || info->emitrelocations);
c152c796 11836
8b127cbc
AM
11837 flinfo.info = info;
11838 flinfo.output_bfd = abfd;
ef10c3ac 11839 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11840 if (flinfo.symstrtab == NULL)
c152c796
AM
11841 return FALSE;
11842
11843 if (! dynamic)
11844 {
8b127cbc
AM
11845 flinfo.hash_sec = NULL;
11846 flinfo.symver_sec = NULL;
c152c796
AM
11847 }
11848 else
11849 {
3d4d4302 11850 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11851 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11852 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11853 /* Note that it is OK if symver_sec is NULL. */
11854 }
11855
8b127cbc
AM
11856 flinfo.contents = NULL;
11857 flinfo.external_relocs = NULL;
11858 flinfo.internal_relocs = NULL;
11859 flinfo.external_syms = NULL;
11860 flinfo.locsym_shndx = NULL;
11861 flinfo.internal_syms = NULL;
11862 flinfo.indices = NULL;
11863 flinfo.sections = NULL;
8b127cbc 11864 flinfo.symshndxbuf = NULL;
ffbc01cc 11865 flinfo.filesym_count = 0;
c152c796 11866
104d59d1
JM
11867 /* The object attributes have been merged. Remove the input
11868 sections from the link, and set the contents of the output
1ff6de03 11869 section. */
4c6ee646 11870 sections_removed = FALSE;
104d59d1
JM
11871 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11872 for (o = abfd->sections; o != NULL; o = o->next)
11873 {
5270eddc 11874 bfd_boolean remove_section = FALSE;
b8a6ced7 11875
104d59d1
JM
11876 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11877 || strcmp (o->name, ".gnu.attributes") == 0)
11878 {
11879 for (p = o->map_head.link_order; p != NULL; p = p->next)
11880 {
11881 asection *input_section;
11882
11883 if (p->type != bfd_indirect_link_order)
11884 continue;
11885 input_section = p->u.indirect.section;
11886 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11887 elf_link_input_bfd ignores this section. */
11888 input_section->flags &= ~SEC_HAS_CONTENTS;
11889 }
a0c8462f 11890
104d59d1 11891 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 11892 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
11893 /* Skip this section later on. */
11894 o->map_head.link_order = NULL;
104d59d1 11895 if (attr_size)
b8a6ced7 11896 attr_section = o;
104d59d1 11897 else
5270eddc 11898 remove_section = TRUE;
104d59d1 11899 }
6e5e9d58
AM
11900 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11901 {
11902 /* Remove empty group section from linker output. */
5270eddc 11903 remove_section = TRUE;
b8a6ced7 11904 }
5270eddc 11905 if (remove_section)
b8a6ced7 11906 {
6e5e9d58
AM
11907 o->flags |= SEC_EXCLUDE;
11908 bfd_section_list_remove (abfd, o);
11909 abfd->section_count--;
4c6ee646 11910 sections_removed = TRUE;
6e5e9d58 11911 }
104d59d1 11912 }
4c6ee646
AM
11913 if (sections_removed)
11914 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 11915
c152c796
AM
11916 /* Count up the number of relocations we will output for each output
11917 section, so that we know the sizes of the reloc sections. We
11918 also figure out some maximum sizes. */
11919 max_contents_size = 0;
11920 max_external_reloc_size = 0;
11921 max_internal_reloc_count = 0;
11922 max_sym_count = 0;
11923 max_sym_shndx_count = 0;
11924 merged = FALSE;
11925 for (o = abfd->sections; o != NULL; o = o->next)
11926 {
11927 struct bfd_elf_section_data *esdo = elf_section_data (o);
11928 o->reloc_count = 0;
11929
8423293d 11930 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11931 {
11932 unsigned int reloc_count = 0;
9eaff861 11933 unsigned int additional_reloc_count = 0;
c152c796 11934 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11935
11936 if (p->type == bfd_section_reloc_link_order
11937 || p->type == bfd_symbol_reloc_link_order)
11938 reloc_count = 1;
11939 else if (p->type == bfd_indirect_link_order)
11940 {
11941 asection *sec;
11942
11943 sec = p->u.indirect.section;
c152c796
AM
11944
11945 /* Mark all sections which are to be included in the
11946 link. This will normally be every section. We need
11947 to do this so that we can identify any sections which
11948 the linker has decided to not include. */
11949 sec->linker_mark = TRUE;
11950
11951 if (sec->flags & SEC_MERGE)
11952 merged = TRUE;
11953
eea6121a
AM
11954 if (sec->rawsize > max_contents_size)
11955 max_contents_size = sec->rawsize;
11956 if (sec->size > max_contents_size)
11957 max_contents_size = sec->size;
c152c796 11958
c152c796
AM
11959 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11960 && (sec->owner->flags & DYNAMIC) == 0)
11961 {
11962 size_t sym_count;
11963
a961cdd5
AM
11964 /* We are interested in just local symbols, not all
11965 symbols. */
c152c796
AM
11966 if (elf_bad_symtab (sec->owner))
11967 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11968 / bed->s->sizeof_sym);
11969 else
11970 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11971
11972 if (sym_count > max_sym_count)
11973 max_sym_count = sym_count;
11974
11975 if (sym_count > max_sym_shndx_count
6a40cf0c 11976 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11977 max_sym_shndx_count = sym_count;
11978
a961cdd5
AM
11979 if (esdo->this_hdr.sh_type == SHT_REL
11980 || esdo->this_hdr.sh_type == SHT_RELA)
11981 /* Some backends use reloc_count in relocation sections
11982 to count particular types of relocs. Of course,
11983 reloc sections themselves can't have relocations. */
11984 ;
11985 else if (emit_relocs)
11986 {
11987 reloc_count = sec->reloc_count;
11988 if (bed->elf_backend_count_additional_relocs)
11989 {
11990 int c;
11991 c = (*bed->elf_backend_count_additional_relocs) (sec);
11992 additional_reloc_count += c;
11993 }
11994 }
11995 else if (bed->elf_backend_count_relocs)
11996 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11997
11998 esdi = elf_section_data (sec);
11999
c152c796
AM
12000 if ((sec->flags & SEC_RELOC) != 0)
12001 {
d4730f92 12002 size_t ext_size = 0;
c152c796 12003
d4730f92
BS
12004 if (esdi->rel.hdr != NULL)
12005 ext_size = esdi->rel.hdr->sh_size;
12006 if (esdi->rela.hdr != NULL)
12007 ext_size += esdi->rela.hdr->sh_size;
7326c758 12008
c152c796
AM
12009 if (ext_size > max_external_reloc_size)
12010 max_external_reloc_size = ext_size;
12011 if (sec->reloc_count > max_internal_reloc_count)
12012 max_internal_reloc_count = sec->reloc_count;
12013 }
12014 }
12015 }
12016
12017 if (reloc_count == 0)
12018 continue;
12019
9eaff861 12020 reloc_count += additional_reloc_count;
c152c796
AM
12021 o->reloc_count += reloc_count;
12022
0e1862bb 12023 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12024 {
d4730f92 12025 if (esdi->rel.hdr)
9eaff861 12026 {
491d01d3 12027 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12028 esdo->rel.count += additional_reloc_count;
12029 }
d4730f92 12030 if (esdi->rela.hdr)
9eaff861 12031 {
491d01d3 12032 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12033 esdo->rela.count += additional_reloc_count;
12034 }
d4730f92
BS
12035 }
12036 else
12037 {
12038 if (o->use_rela_p)
12039 esdo->rela.count += reloc_count;
2c2b4ed4 12040 else
d4730f92 12041 esdo->rel.count += reloc_count;
c152c796 12042 }
c152c796
AM
12043 }
12044
9eaff861 12045 if (o->reloc_count > 0)
c152c796
AM
12046 o->flags |= SEC_RELOC;
12047 else
12048 {
12049 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12050 set it (this is probably a bug) and if it is set
12051 assign_section_numbers will create a reloc section. */
12052 o->flags &=~ SEC_RELOC;
12053 }
12054
12055 /* If the SEC_ALLOC flag is not set, force the section VMA to
12056 zero. This is done in elf_fake_sections as well, but forcing
12057 the VMA to 0 here will ensure that relocs against these
12058 sections are handled correctly. */
12059 if ((o->flags & SEC_ALLOC) == 0
12060 && ! o->user_set_vma)
12061 o->vma = 0;
12062 }
12063
0e1862bb 12064 if (! bfd_link_relocatable (info) && merged)
64f52338 12065 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12066
12067 /* Figure out the file positions for everything but the symbol table
12068 and the relocs. We set symcount to force assign_section_numbers
12069 to create a symbol table. */
ed48ec2e 12070 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12071 BFD_ASSERT (! abfd->output_has_begun);
12072 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12073 goto error_return;
12074
ee75fd95 12075 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12076 for (o = abfd->sections; o != NULL; o = o->next)
12077 {
d4730f92 12078 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12079 if ((o->flags & SEC_RELOC) != 0)
12080 {
d4730f92 12081 if (esdo->rel.hdr
9eaff861 12082 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12083 goto error_return;
12084
d4730f92 12085 if (esdo->rela.hdr
9eaff861 12086 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12087 goto error_return;
12088 }
12089
48db3297
AM
12090 /* _bfd_elf_compute_section_file_positions makes temporary use
12091 of target_index. Reset it. */
12092 o->target_index = 0;
12093
c152c796
AM
12094 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12095 to count upwards while actually outputting the relocations. */
d4730f92
BS
12096 esdo->rel.count = 0;
12097 esdo->rela.count = 0;
0ce398f1 12098
1ff6de03
NA
12099 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12100 && !bfd_section_is_ctf (o))
0ce398f1
L
12101 {
12102 /* Cache the section contents so that they can be compressed
12103 later. Use bfd_malloc since it will be freed by
12104 bfd_compress_section_contents. */
12105 unsigned char *contents = esdo->this_hdr.contents;
12106 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12107 abort ();
12108 contents
12109 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12110 if (contents == NULL)
12111 goto error_return;
12112 esdo->this_hdr.contents = contents;
12113 }
c152c796
AM
12114 }
12115
1ff6de03
NA
12116 /* We have now assigned file positions for all the sections except .symtab,
12117 .strtab, and non-loaded reloc and compressed debugging sections. We start
12118 the .symtab section at the current file position, and write directly to it.
12119 We build the .strtab section in memory. */
ed48ec2e 12120 abfd->symcount = 0;
c152c796
AM
12121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12122 /* sh_name is set in prep_headers. */
12123 symtab_hdr->sh_type = SHT_SYMTAB;
12124 /* sh_flags, sh_addr and sh_size all start off zero. */
12125 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12126 /* sh_link is set in assign_section_numbers. */
12127 /* sh_info is set below. */
12128 /* sh_offset is set just below. */
72de5009 12129 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12130
ef10c3ac
L
12131 if (max_sym_count < 20)
12132 max_sym_count = 20;
64f52338 12133 htab->strtabsize = max_sym_count;
ef10c3ac 12134 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12135 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12136 if (htab->strtab == NULL)
c152c796 12137 goto error_return;
ef10c3ac
L
12138 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12139 flinfo.symshndxbuf
12140 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12141 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12142
8539e4e8 12143 if (info->strip != strip_all || emit_relocs)
c152c796 12144 {
8539e4e8
AM
12145 file_ptr off = elf_next_file_pos (abfd);
12146
12147 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12148
12149 /* Note that at this point elf_next_file_pos (abfd) is
12150 incorrect. We do not yet know the size of the .symtab section.
12151 We correct next_file_pos below, after we do know the size. */
12152
12153 /* Start writing out the symbol table. The first symbol is always a
12154 dummy symbol. */
c152c796
AM
12155 elfsym.st_value = 0;
12156 elfsym.st_size = 0;
12157 elfsym.st_info = 0;
12158 elfsym.st_other = 0;
12159 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12160 elfsym.st_target_internal = 0;
ef10c3ac
L
12161 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12162 bfd_und_section_ptr, NULL) != 1)
c152c796 12163 goto error_return;
c152c796 12164
8539e4e8
AM
12165 /* Output a symbol for each section. We output these even if we are
12166 discarding local symbols, since they are used for relocs. These
12167 symbols have no names. We store the index of each one in the
12168 index field of the section, so that we can find it again when
12169 outputting relocs. */
12170
c152c796
AM
12171 elfsym.st_size = 0;
12172 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12173 elfsym.st_other = 0;
f0b5bb34 12174 elfsym.st_value = 0;
35fc36a8 12175 elfsym.st_target_internal = 0;
c152c796
AM
12176 for (i = 1; i < elf_numsections (abfd); i++)
12177 {
12178 o = bfd_section_from_elf_index (abfd, i);
12179 if (o != NULL)
f0b5bb34
AM
12180 {
12181 o->target_index = bfd_get_symcount (abfd);
12182 elfsym.st_shndx = i;
0e1862bb 12183 if (!bfd_link_relocatable (info))
f0b5bb34 12184 elfsym.st_value = o->vma;
ef10c3ac
L
12185 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12186 NULL) != 1)
f0b5bb34
AM
12187 goto error_return;
12188 }
c152c796
AM
12189 }
12190 }
12191
12192 /* Allocate some memory to hold information read in from the input
12193 files. */
12194 if (max_contents_size != 0)
12195 {
8b127cbc
AM
12196 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12197 if (flinfo.contents == NULL)
c152c796
AM
12198 goto error_return;
12199 }
12200
12201 if (max_external_reloc_size != 0)
12202 {
8b127cbc
AM
12203 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12204 if (flinfo.external_relocs == NULL)
c152c796
AM
12205 goto error_return;
12206 }
12207
12208 if (max_internal_reloc_count != 0)
12209 {
056bafd4 12210 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12211 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12212 if (flinfo.internal_relocs == NULL)
c152c796
AM
12213 goto error_return;
12214 }
12215
12216 if (max_sym_count != 0)
12217 {
12218 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12219 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12220 if (flinfo.external_syms == NULL)
c152c796
AM
12221 goto error_return;
12222
12223 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12224 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12225 if (flinfo.internal_syms == NULL)
c152c796
AM
12226 goto error_return;
12227
12228 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12229 flinfo.indices = (long int *) bfd_malloc (amt);
12230 if (flinfo.indices == NULL)
c152c796
AM
12231 goto error_return;
12232
12233 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12234 flinfo.sections = (asection **) bfd_malloc (amt);
12235 if (flinfo.sections == NULL)
c152c796
AM
12236 goto error_return;
12237 }
12238
12239 if (max_sym_shndx_count != 0)
12240 {
12241 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12242 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12243 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12244 goto error_return;
12245 }
12246
64f52338 12247 if (htab->tls_sec)
c152c796
AM
12248 {
12249 bfd_vma base, end = 0;
12250 asection *sec;
12251
64f52338 12252 for (sec = htab->tls_sec;
c152c796
AM
12253 sec && (sec->flags & SEC_THREAD_LOCAL);
12254 sec = sec->next)
12255 {
3a800eb9 12256 bfd_size_type size = sec->size;
c152c796 12257
3a800eb9
AM
12258 if (size == 0
12259 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12260 {
91d6fa6a
NC
12261 struct bfd_link_order *ord = sec->map_tail.link_order;
12262
12263 if (ord != NULL)
12264 size = ord->offset + ord->size;
c152c796
AM
12265 }
12266 end = sec->vma + size;
12267 }
64f52338 12268 base = htab->tls_sec->vma;
7dc98aea
RO
12269 /* Only align end of TLS section if static TLS doesn't have special
12270 alignment requirements. */
12271 if (bed->static_tls_alignment == 1)
64f52338
AM
12272 end = align_power (end, htab->tls_sec->alignment_power);
12273 htab->tls_size = end - base;
c152c796
AM
12274 }
12275
0b52efa6
PB
12276 /* Reorder SHF_LINK_ORDER sections. */
12277 for (o = abfd->sections; o != NULL; o = o->next)
12278 {
12279 if (!elf_fixup_link_order (abfd, o))
12280 return FALSE;
12281 }
12282
2f0c68f2
CM
12283 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12284 return FALSE;
12285
c152c796
AM
12286 /* Since ELF permits relocations to be against local symbols, we
12287 must have the local symbols available when we do the relocations.
12288 Since we would rather only read the local symbols once, and we
12289 would rather not keep them in memory, we handle all the
12290 relocations for a single input file at the same time.
12291
12292 Unfortunately, there is no way to know the total number of local
12293 symbols until we have seen all of them, and the local symbol
12294 indices precede the global symbol indices. This means that when
12295 we are generating relocatable output, and we see a reloc against
12296 a global symbol, we can not know the symbol index until we have
12297 finished examining all the local symbols to see which ones we are
12298 going to output. To deal with this, we keep the relocations in
12299 memory, and don't output them until the end of the link. This is
12300 an unfortunate waste of memory, but I don't see a good way around
12301 it. Fortunately, it only happens when performing a relocatable
12302 link, which is not the common case. FIXME: If keep_memory is set
12303 we could write the relocs out and then read them again; I don't
12304 know how bad the memory loss will be. */
12305
c72f2fb2 12306 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12307 sub->output_has_begun = FALSE;
12308 for (o = abfd->sections; o != NULL; o = o->next)
12309 {
8423293d 12310 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12311 {
12312 if (p->type == bfd_indirect_link_order
12313 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12314 == bfd_target_elf_flavour)
12315 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12316 {
12317 if (! sub->output_has_begun)
12318 {
8b127cbc 12319 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12320 goto error_return;
12321 sub->output_has_begun = TRUE;
12322 }
12323 }
12324 else if (p->type == bfd_section_reloc_link_order
12325 || p->type == bfd_symbol_reloc_link_order)
12326 {
12327 if (! elf_reloc_link_order (abfd, info, o, p))
12328 goto error_return;
12329 }
12330 else
12331 {
12332 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12333 {
12334 if (p->type == bfd_indirect_link_order
12335 && (bfd_get_flavour (sub)
12336 == bfd_target_elf_flavour)
12337 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12338 != bed->s->elfclass))
12339 {
12340 const char *iclass, *oclass;
12341
aebf9be7 12342 switch (bed->s->elfclass)
351f65ca 12343 {
aebf9be7
NC
12344 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12345 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12346 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12347 default: abort ();
351f65ca 12348 }
aebf9be7
NC
12349
12350 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12351 {
aebf9be7
NC
12352 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12353 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12354 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12355 default: abort ();
351f65ca
L
12356 }
12357
12358 bfd_set_error (bfd_error_wrong_format);
4eca0228 12359 _bfd_error_handler
695344c0 12360 /* xgettext:c-format */
871b3ab2 12361 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12362 sub, iclass, oclass);
12363 }
12364
12365 goto error_return;
12366 }
c152c796
AM
12367 }
12368 }
12369 }
12370
c0f00686
L
12371 /* Free symbol buffer if needed. */
12372 if (!info->reduce_memory_overheads)
12373 {
c72f2fb2 12374 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12375 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12376 && elf_tdata (sub)->symbuf)
c0f00686
L
12377 {
12378 free (elf_tdata (sub)->symbuf);
12379 elf_tdata (sub)->symbuf = NULL;
12380 }
12381 }
12382
c152c796
AM
12383 /* Output any global symbols that got converted to local in a
12384 version script or due to symbol visibility. We do this in a
12385 separate step since ELF requires all local symbols to appear
12386 prior to any global symbols. FIXME: We should only do this if
12387 some global symbols were, in fact, converted to become local.
12388 FIXME: Will this work correctly with the Irix 5 linker? */
12389 eoinfo.failed = FALSE;
8b127cbc 12390 eoinfo.flinfo = &flinfo;
c152c796 12391 eoinfo.localsyms = TRUE;
34a79995 12392 eoinfo.file_sym_done = FALSE;
7686d77d 12393 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12394 if (eoinfo.failed)
12395 return FALSE;
12396
4e617b1e
PB
12397 /* If backend needs to output some local symbols not present in the hash
12398 table, do it now. */
8539e4e8
AM
12399 if (bed->elf_backend_output_arch_local_syms
12400 && (info->strip != strip_all || emit_relocs))
4e617b1e 12401 {
6e0b88f1 12402 typedef int (*out_sym_func)
4e617b1e
PB
12403 (void *, const char *, Elf_Internal_Sym *, asection *,
12404 struct elf_link_hash_entry *);
12405
12406 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12407 (abfd, info, &flinfo,
12408 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12409 return FALSE;
12410 }
12411
c152c796
AM
12412 /* That wrote out all the local symbols. Finish up the symbol table
12413 with the global symbols. Even if we want to strip everything we
12414 can, we still need to deal with those global symbols that got
12415 converted to local in a version script. */
12416
12417 /* The sh_info field records the index of the first non local symbol. */
12418 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12419
12420 if (dynamic
64f52338
AM
12421 && htab->dynsym != NULL
12422 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12423 {
12424 Elf_Internal_Sym sym;
64f52338 12425 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12426
64f52338
AM
12427 o = htab->dynsym->output_section;
12428 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12429
12430 /* Write out the section symbols for the output sections. */
0e1862bb 12431 if (bfd_link_pic (info)
64f52338 12432 || htab->is_relocatable_executable)
c152c796
AM
12433 {
12434 asection *s;
12435
12436 sym.st_size = 0;
12437 sym.st_name = 0;
12438 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12439 sym.st_other = 0;
35fc36a8 12440 sym.st_target_internal = 0;
c152c796
AM
12441
12442 for (s = abfd->sections; s != NULL; s = s->next)
12443 {
12444 int indx;
12445 bfd_byte *dest;
12446 long dynindx;
12447
c152c796 12448 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12449 if (dynindx <= 0)
12450 continue;
12451 indx = elf_section_data (s)->this_idx;
c152c796
AM
12452 BFD_ASSERT (indx > 0);
12453 sym.st_shndx = indx;
c0d5a53d
L
12454 if (! check_dynsym (abfd, &sym))
12455 return FALSE;
c152c796
AM
12456 sym.st_value = s->vma;
12457 dest = dynsym + dynindx * bed->s->sizeof_sym;
12458 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12459 }
c152c796
AM
12460 }
12461
12462 /* Write out the local dynsyms. */
64f52338 12463 if (htab->dynlocal)
c152c796
AM
12464 {
12465 struct elf_link_local_dynamic_entry *e;
64f52338 12466 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12467 {
12468 asection *s;
12469 bfd_byte *dest;
12470
935bd1e0 12471 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12472 Note that we saved a word of storage and overwrote
12473 the original st_name with the dynstr_index. */
12474 sym = e->isym;
935bd1e0 12475 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12476
cb33740c
AM
12477 s = bfd_section_from_elf_index (e->input_bfd,
12478 e->isym.st_shndx);
12479 if (s != NULL)
c152c796 12480 {
c152c796
AM
12481 sym.st_shndx =
12482 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12483 if (! check_dynsym (abfd, &sym))
12484 return FALSE;
c152c796
AM
12485 sym.st_value = (s->output_section->vma
12486 + s->output_offset
12487 + e->isym.st_value);
12488 }
12489
c152c796
AM
12490 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12491 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12492 }
12493 }
c152c796
AM
12494 }
12495
12496 /* We get the global symbols from the hash table. */
12497 eoinfo.failed = FALSE;
12498 eoinfo.localsyms = FALSE;
8b127cbc 12499 eoinfo.flinfo = &flinfo;
7686d77d 12500 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12501 if (eoinfo.failed)
12502 return FALSE;
12503
12504 /* If backend needs to output some symbols not present in the hash
12505 table, do it now. */
8539e4e8
AM
12506 if (bed->elf_backend_output_arch_syms
12507 && (info->strip != strip_all || emit_relocs))
c152c796 12508 {
6e0b88f1 12509 typedef int (*out_sym_func)
c152c796
AM
12510 (void *, const char *, Elf_Internal_Sym *, asection *,
12511 struct elf_link_hash_entry *);
12512
12513 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12514 (abfd, info, &flinfo,
12515 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12516 return FALSE;
12517 }
12518
ef10c3ac
L
12519 /* Finalize the .strtab section. */
12520 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12521
12522 /* Swap out the .strtab section. */
12523 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12524 return FALSE;
12525
12526 /* Now we know the size of the symtab section. */
c152c796
AM
12527 if (bfd_get_symcount (abfd) > 0)
12528 {
ee3b52e9
L
12529 /* Finish up and write out the symbol string table (.strtab)
12530 section. */
ad32986f 12531 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12532 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12533
ad32986f 12534 if (elf_symtab_shndx_list (abfd))
8539e4e8 12535 {
ad32986f 12536 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12537
ad32986f
NC
12538 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12539 {
12540 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12541 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12542 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12543 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12544 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12545
ad32986f
NC
12546 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12547 off, TRUE);
12548
12549 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12550 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12551 return FALSE;
12552 }
8539e4e8 12553 }
ee3b52e9
L
12554
12555 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12556 /* sh_name was set in prep_headers. */
12557 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12558 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12559 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12560 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12561 symstrtab_hdr->sh_entsize = 0;
12562 symstrtab_hdr->sh_link = 0;
12563 symstrtab_hdr->sh_info = 0;
12564 /* sh_offset is set just below. */
12565 symstrtab_hdr->sh_addralign = 1;
12566
12567 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12568 off, TRUE);
12569 elf_next_file_pos (abfd) = off;
12570
c152c796 12571 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12572 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12573 return FALSE;
12574 }
12575
76359541
TP
12576 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12577 {
871b3ab2 12578 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12579 info->out_implib_bfd);
76359541
TP
12580 return FALSE;
12581 }
12582
c152c796
AM
12583 /* Adjust the relocs to have the correct symbol indices. */
12584 for (o = abfd->sections; o != NULL; o = o->next)
12585 {
d4730f92 12586 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12587 bfd_boolean sort;
10bbbc1d 12588
c152c796
AM
12589 if ((o->flags & SEC_RELOC) == 0)
12590 continue;
12591
28dbcedc 12592 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12593 if (esdo->rel.hdr != NULL
10bbbc1d 12594 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12595 return FALSE;
12596 if (esdo->rela.hdr != NULL
10bbbc1d 12597 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12598 return FALSE;
c152c796
AM
12599
12600 /* Set the reloc_count field to 0 to prevent write_relocs from
12601 trying to swap the relocs out itself. */
12602 o->reloc_count = 0;
12603 }
12604
12605 if (dynamic && info->combreloc && dynobj != NULL)
12606 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12607
12608 /* If we are linking against a dynamic object, or generating a
12609 shared library, finish up the dynamic linking information. */
12610 if (dynamic)
12611 {
12612 bfd_byte *dyncon, *dynconend;
12613
12614 /* Fix up .dynamic entries. */
3d4d4302 12615 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12616 BFD_ASSERT (o != NULL);
12617
12618 dyncon = o->contents;
eea6121a 12619 dynconend = o->contents + o->size;
c152c796
AM
12620 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12621 {
12622 Elf_Internal_Dyn dyn;
12623 const char *name;
12624 unsigned int type;
64487780
AM
12625 bfd_size_type sh_size;
12626 bfd_vma sh_addr;
c152c796
AM
12627
12628 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12629
12630 switch (dyn.d_tag)
12631 {
12632 default:
12633 continue;
12634 case DT_NULL:
12635 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12636 {
12637 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12638 {
12639 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12640 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12641 default: continue;
12642 }
12643 dyn.d_un.d_val = relativecount;
12644 relativecount = 0;
12645 break;
12646 }
12647 continue;
12648
12649 case DT_INIT:
12650 name = info->init_function;
12651 goto get_sym;
12652 case DT_FINI:
12653 name = info->fini_function;
12654 get_sym:
12655 {
12656 struct elf_link_hash_entry *h;
12657
64f52338 12658 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12659 if (h != NULL
12660 && (h->root.type == bfd_link_hash_defined
12661 || h->root.type == bfd_link_hash_defweak))
12662 {
bef26483 12663 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12664 o = h->root.u.def.section;
12665 if (o->output_section != NULL)
bef26483 12666 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12667 + o->output_offset);
12668 else
12669 {
12670 /* The symbol is imported from another shared
12671 library and does not apply to this one. */
bef26483 12672 dyn.d_un.d_ptr = 0;
c152c796
AM
12673 }
12674 break;
12675 }
12676 }
12677 continue;
12678
12679 case DT_PREINIT_ARRAYSZ:
12680 name = ".preinit_array";
4ade44b7 12681 goto get_out_size;
c152c796
AM
12682 case DT_INIT_ARRAYSZ:
12683 name = ".init_array";
4ade44b7 12684 goto get_out_size;
c152c796
AM
12685 case DT_FINI_ARRAYSZ:
12686 name = ".fini_array";
4ade44b7 12687 get_out_size:
c152c796
AM
12688 o = bfd_get_section_by_name (abfd, name);
12689 if (o == NULL)
12690 {
4eca0228 12691 _bfd_error_handler
4ade44b7 12692 (_("could not find section %s"), name);
c152c796
AM
12693 goto error_return;
12694 }
eea6121a 12695 if (o->size == 0)
4eca0228 12696 _bfd_error_handler
c152c796 12697 (_("warning: %s section has zero size"), name);
eea6121a 12698 dyn.d_un.d_val = o->size;
c152c796
AM
12699 break;
12700
12701 case DT_PREINIT_ARRAY:
12702 name = ".preinit_array";
4ade44b7 12703 goto get_out_vma;
c152c796
AM
12704 case DT_INIT_ARRAY:
12705 name = ".init_array";
4ade44b7 12706 goto get_out_vma;
c152c796
AM
12707 case DT_FINI_ARRAY:
12708 name = ".fini_array";
4ade44b7
AM
12709 get_out_vma:
12710 o = bfd_get_section_by_name (abfd, name);
12711 goto do_vma;
c152c796
AM
12712
12713 case DT_HASH:
12714 name = ".hash";
12715 goto get_vma;
fdc90cb4
JJ
12716 case DT_GNU_HASH:
12717 name = ".gnu.hash";
12718 goto get_vma;
c152c796
AM
12719 case DT_STRTAB:
12720 name = ".dynstr";
12721 goto get_vma;
12722 case DT_SYMTAB:
12723 name = ".dynsym";
12724 goto get_vma;
12725 case DT_VERDEF:
12726 name = ".gnu.version_d";
12727 goto get_vma;
12728 case DT_VERNEED:
12729 name = ".gnu.version_r";
12730 goto get_vma;
12731 case DT_VERSYM:
12732 name = ".gnu.version";
12733 get_vma:
4ade44b7
AM
12734 o = bfd_get_linker_section (dynobj, name);
12735 do_vma:
b3293efa 12736 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12737 {
4eca0228 12738 _bfd_error_handler
4ade44b7 12739 (_("could not find section %s"), name);
c152c796
AM
12740 goto error_return;
12741 }
894891db
NC
12742 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12743 {
4eca0228 12744 _bfd_error_handler
894891db
NC
12745 (_("warning: section '%s' is being made into a note"), name);
12746 bfd_set_error (bfd_error_nonrepresentable_section);
12747 goto error_return;
12748 }
4ade44b7 12749 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12750 break;
12751
12752 case DT_REL:
12753 case DT_RELA:
12754 case DT_RELSZ:
12755 case DT_RELASZ:
12756 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12757 type = SHT_REL;
12758 else
12759 type = SHT_RELA;
64487780
AM
12760 sh_size = 0;
12761 sh_addr = 0;
c152c796
AM
12762 for (i = 1; i < elf_numsections (abfd); i++)
12763 {
12764 Elf_Internal_Shdr *hdr;
12765
12766 hdr = elf_elfsections (abfd)[i];
12767 if (hdr->sh_type == type
12768 && (hdr->sh_flags & SHF_ALLOC) != 0)
12769 {
64487780
AM
12770 sh_size += hdr->sh_size;
12771 if (sh_addr == 0
12772 || sh_addr > hdr->sh_addr)
12773 sh_addr = hdr->sh_addr;
c152c796
AM
12774 }
12775 }
64487780 12776
64f52338
AM
12777 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12778 {
12779 /* Don't count procedure linkage table relocs in the
12780 overall reloc count. */
64487780
AM
12781 sh_size -= htab->srelplt->size;
12782 if (sh_size == 0)
12783 /* If the size is zero, make the address zero too.
12784 This is to avoid a glibc bug. If the backend
12785 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12786 zero, then we'll put DT_RELA at the end of
12787 DT_JMPREL. glibc will interpret the end of
12788 DT_RELA matching the end of DT_JMPREL as the
12789 case where DT_RELA includes DT_JMPREL, and for
12790 LD_BIND_NOW will decide that processing DT_RELA
12791 will process the PLT relocs too. Net result:
12792 No PLT relocs applied. */
12793 sh_addr = 0;
12794
64f52338
AM
12795 /* If .rela.plt is the first .rela section, exclude
12796 it from DT_RELA. */
64487780
AM
12797 else if (sh_addr == (htab->srelplt->output_section->vma
12798 + htab->srelplt->output_offset))
12799 sh_addr += htab->srelplt->size;
64f52338 12800 }
64487780
AM
12801
12802 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12803 dyn.d_un.d_val = sh_size;
12804 else
12805 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12806 break;
12807 }
12808 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12809 }
12810 }
12811
12812 /* If we have created any dynamic sections, then output them. */
12813 if (dynobj != NULL)
12814 {
12815 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12816 goto error_return;
12817
943284cc 12818 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12819 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12820 || info->error_textrel)
3d4d4302 12821 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12822 {
12823 bfd_byte *dyncon, *dynconend;
12824
943284cc
DJ
12825 dyncon = o->contents;
12826 dynconend = o->contents + o->size;
12827 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12828 {
12829 Elf_Internal_Dyn dyn;
12830
12831 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12832
12833 if (dyn.d_tag == DT_TEXTREL)
12834 {
c192a133
AM
12835 if (info->error_textrel)
12836 info->callbacks->einfo
9793eb77 12837 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12838 else
12839 info->callbacks->einfo
9793eb77 12840 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12841 break;
12842 }
12843 }
12844 }
12845
c152c796
AM
12846 for (o = dynobj->sections; o != NULL; o = o->next)
12847 {
12848 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12849 || o->size == 0
c152c796
AM
12850 || o->output_section == bfd_abs_section_ptr)
12851 continue;
12852 if ((o->flags & SEC_LINKER_CREATED) == 0)
12853 {
12854 /* At this point, we are only interested in sections
12855 created by _bfd_elf_link_create_dynamic_sections. */
12856 continue;
12857 }
64f52338 12858 if (htab->stab_info.stabstr == o)
3722b82f 12859 continue;
64f52338 12860 if (htab->eh_info.hdr_sec == o)
eea6121a 12861 continue;
3d4d4302 12862 if (strcmp (o->name, ".dynstr") != 0)
c152c796 12863 {
bb294208
AM
12864 bfd_size_type octets = ((file_ptr) o->output_offset
12865 * bfd_octets_per_byte (abfd, o));
12866 if (!bfd_set_section_contents (abfd, o->output_section,
12867 o->contents, octets, o->size))
c152c796
AM
12868 goto error_return;
12869 }
12870 else
12871 {
12872 /* The contents of the .dynstr section are actually in a
12873 stringtab. */
8539e4e8
AM
12874 file_ptr off;
12875
c152c796
AM
12876 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12877 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12878 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12879 goto error_return;
12880 }
12881 }
12882 }
12883
7bdf4127 12884 if (!info->resolve_section_groups)
c152c796
AM
12885 {
12886 bfd_boolean failed = FALSE;
12887
7bdf4127 12888 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12889 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12890 if (failed)
12891 goto error_return;
12892 }
12893
12894 /* If we have optimized stabs strings, output them. */
64f52338 12895 if (htab->stab_info.stabstr != NULL)
c152c796 12896 {
64f52338 12897 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12898 goto error_return;
12899 }
12900
9f7c3e5e
AM
12901 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12902 goto error_return;
c152c796 12903
1ff6de03
NA
12904 if (info->callbacks->emit_ctf)
12905 info->callbacks->emit_ctf ();
12906
9f7c3e5e 12907 elf_final_link_free (abfd, &flinfo);
c152c796 12908
104d59d1
JM
12909 if (attr_section)
12910 {
a50b1753 12911 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12912 if (contents == NULL)
d0f16d5e 12913 return FALSE; /* Bail out and fail. */
104d59d1
JM
12914 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12915 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12916 free (contents);
12917 }
12918
c152c796
AM
12919 return TRUE;
12920
12921 error_return:
9f7c3e5e 12922 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12923 return FALSE;
12924}
12925\f
5241d853
RS
12926/* Initialize COOKIE for input bfd ABFD. */
12927
12928static bfd_boolean
12929init_reloc_cookie (struct elf_reloc_cookie *cookie,
12930 struct bfd_link_info *info, bfd *abfd)
12931{
12932 Elf_Internal_Shdr *symtab_hdr;
12933 const struct elf_backend_data *bed;
12934
12935 bed = get_elf_backend_data (abfd);
12936 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12937
12938 cookie->abfd = abfd;
12939 cookie->sym_hashes = elf_sym_hashes (abfd);
12940 cookie->bad_symtab = elf_bad_symtab (abfd);
12941 if (cookie->bad_symtab)
12942 {
12943 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12944 cookie->extsymoff = 0;
12945 }
12946 else
12947 {
12948 cookie->locsymcount = symtab_hdr->sh_info;
12949 cookie->extsymoff = symtab_hdr->sh_info;
12950 }
12951
12952 if (bed->s->arch_size == 32)
12953 cookie->r_sym_shift = 8;
12954 else
12955 cookie->r_sym_shift = 32;
12956
12957 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12958 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12959 {
12960 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12961 cookie->locsymcount, 0,
12962 NULL, NULL, NULL);
12963 if (cookie->locsyms == NULL)
12964 {
12965 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12966 return FALSE;
12967 }
12968 if (info->keep_memory)
12969 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12970 }
12971 return TRUE;
12972}
12973
12974/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12975
12976static void
12977fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12978{
12979 Elf_Internal_Shdr *symtab_hdr;
12980
12981 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12982 if (cookie->locsyms != NULL
12983 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12984 free (cookie->locsyms);
12985}
12986
12987/* Initialize the relocation information in COOKIE for input section SEC
12988 of input bfd ABFD. */
12989
12990static bfd_boolean
12991init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12992 struct bfd_link_info *info, bfd *abfd,
12993 asection *sec)
12994{
5241d853
RS
12995 if (sec->reloc_count == 0)
12996 {
12997 cookie->rels = NULL;
12998 cookie->relend = NULL;
12999 }
13000 else
13001 {
5241d853
RS
13002 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13003 info->keep_memory);
13004 if (cookie->rels == NULL)
13005 return FALSE;
13006 cookie->rel = cookie->rels;
056bafd4 13007 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13008 }
13009 cookie->rel = cookie->rels;
13010 return TRUE;
13011}
13012
13013/* Free the memory allocated by init_reloc_cookie_rels,
13014 if appropriate. */
13015
13016static void
13017fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13018 asection *sec)
13019{
13020 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13021 free (cookie->rels);
13022}
13023
13024/* Initialize the whole of COOKIE for input section SEC. */
13025
13026static bfd_boolean
13027init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13028 struct bfd_link_info *info,
13029 asection *sec)
13030{
13031 if (!init_reloc_cookie (cookie, info, sec->owner))
13032 goto error1;
13033 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13034 goto error2;
13035 return TRUE;
13036
13037 error2:
13038 fini_reloc_cookie (cookie, sec->owner);
13039 error1:
13040 return FALSE;
13041}
13042
13043/* Free the memory allocated by init_reloc_cookie_for_section,
13044 if appropriate. */
13045
13046static void
13047fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13048 asection *sec)
13049{
13050 fini_reloc_cookie_rels (cookie, sec);
13051 fini_reloc_cookie (cookie, sec->owner);
13052}
13053\f
c152c796
AM
13054/* Garbage collect unused sections. */
13055
07adf181
AM
13056/* Default gc_mark_hook. */
13057
13058asection *
13059_bfd_elf_gc_mark_hook (asection *sec,
13060 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13061 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13062 struct elf_link_hash_entry *h,
13063 Elf_Internal_Sym *sym)
13064{
13065 if (h != NULL)
13066 {
13067 switch (h->root.type)
13068 {
13069 case bfd_link_hash_defined:
13070 case bfd_link_hash_defweak:
13071 return h->root.u.def.section;
13072
13073 case bfd_link_hash_common:
13074 return h->root.u.c.p->section;
13075
13076 default:
13077 break;
13078 }
13079 }
13080 else
13081 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13082
13083 return NULL;
13084}
13085
9e223787 13086/* Return the debug definition section. */
b7c871ed
L
13087
13088static asection *
13089elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13090 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13091 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13092 struct elf_link_hash_entry *h,
9e223787 13093 Elf_Internal_Sym *sym)
b7c871ed 13094{
9e223787
L
13095 if (h != NULL)
13096 {
13097 /* Return the global debug definition section. */
13098 if ((h->root.type == bfd_link_hash_defined
13099 || h->root.type == bfd_link_hash_defweak)
13100 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13101 return h->root.u.def.section;
13102 }
13103 else
13104 {
13105 /* Return the local debug definition section. */
13106 asection *isec = bfd_section_from_elf_index (sec->owner,
13107 sym->st_shndx);
13108 if ((isec->flags & SEC_DEBUGGING) != 0)
13109 return isec;
13110 }
b7c871ed
L
13111
13112 return NULL;
13113}
13114
5241d853
RS
13115/* COOKIE->rel describes a relocation against section SEC, which is
13116 a section we've decided to keep. Return the section that contains
13117 the relocation symbol, or NULL if no section contains it. */
13118
13119asection *
13120_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13121 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13122 struct elf_reloc_cookie *cookie,
13123 bfd_boolean *start_stop)
5241d853
RS
13124{
13125 unsigned long r_symndx;
13126 struct elf_link_hash_entry *h;
13127
13128 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13129 if (r_symndx == STN_UNDEF)
5241d853
RS
13130 return NULL;
13131
13132 if (r_symndx >= cookie->locsymcount
13133 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13134 {
13135 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13136 if (h == NULL)
13137 {
871b3ab2 13138 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13139 sec->owner);
13140 return NULL;
13141 }
5241d853
RS
13142 while (h->root.type == bfd_link_hash_indirect
13143 || h->root.type == bfd_link_hash_warning)
13144 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13145 h->mark = 1;
4e6b54a6
AM
13146 /* If this symbol is weak and there is a non-weak definition, we
13147 keep the non-weak definition because many backends put
13148 dynamic reloc info on the non-weak definition for code
13149 handling copy relocs. */
60d67dc8
AM
13150 if (h->is_weakalias)
13151 weakdef (h)->mark = 1;
1cce69b9 13152
a6a4679f 13153 if (start_stop != NULL)
1cce69b9 13154 {
7dba9362
AM
13155 /* To work around a glibc bug, mark XXX input sections
13156 when there is a reference to __start_XXX or __stop_XXX
13157 symbols. */
cbd0eecf 13158 if (h->start_stop)
1cce69b9 13159 {
cbd0eecf 13160 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13161 *start_stop = !s->gc_mark;
13162 return s;
1cce69b9
AM
13163 }
13164 }
13165
5241d853
RS
13166 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13167 }
13168
13169 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13170 &cookie->locsyms[r_symndx]);
13171}
13172
13173/* COOKIE->rel describes a relocation against section SEC, which is
13174 a section we've decided to keep. Mark the section that contains
9d0a14d3 13175 the relocation symbol. */
5241d853
RS
13176
13177bfd_boolean
13178_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13179 asection *sec,
13180 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13181 struct elf_reloc_cookie *cookie)
5241d853
RS
13182{
13183 asection *rsec;
1cce69b9 13184 bfd_boolean start_stop = FALSE;
5241d853 13185
1cce69b9
AM
13186 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13187 while (rsec != NULL)
5241d853 13188 {
1cce69b9
AM
13189 if (!rsec->gc_mark)
13190 {
13191 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13192 || (rsec->owner->flags & DYNAMIC) != 0)
13193 rsec->gc_mark = 1;
13194 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13195 return FALSE;
13196 }
13197 if (!start_stop)
13198 break;
199af150 13199 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13200 }
13201 return TRUE;
13202}
13203
07adf181
AM
13204/* The mark phase of garbage collection. For a given section, mark
13205 it and any sections in this section's group, and all the sections
13206 which define symbols to which it refers. */
13207
ccfa59ea
AM
13208bfd_boolean
13209_bfd_elf_gc_mark (struct bfd_link_info *info,
13210 asection *sec,
6a5bb875 13211 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13212{
13213 bfd_boolean ret;
9d0a14d3 13214 asection *group_sec, *eh_frame;
c152c796
AM
13215
13216 sec->gc_mark = 1;
13217
13218 /* Mark all the sections in the group. */
13219 group_sec = elf_section_data (sec)->next_in_group;
13220 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13221 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13222 return FALSE;
13223
13224 /* Look through the section relocs. */
13225 ret = TRUE;
9d0a14d3
RS
13226 eh_frame = elf_eh_frame_section (sec->owner);
13227 if ((sec->flags & SEC_RELOC) != 0
13228 && sec->reloc_count > 0
13229 && sec != eh_frame)
c152c796 13230 {
5241d853 13231 struct elf_reloc_cookie cookie;
c152c796 13232
5241d853
RS
13233 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13234 ret = FALSE;
c152c796 13235 else
c152c796 13236 {
5241d853 13237 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13238 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13239 {
13240 ret = FALSE;
13241 break;
13242 }
13243 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13244 }
13245 }
9d0a14d3
RS
13246
13247 if (ret && eh_frame && elf_fde_list (sec))
13248 {
13249 struct elf_reloc_cookie cookie;
13250
13251 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13252 ret = FALSE;
13253 else
13254 {
13255 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13256 gc_mark_hook, &cookie))
13257 ret = FALSE;
13258 fini_reloc_cookie_for_section (&cookie, eh_frame);
13259 }
13260 }
13261
2f0c68f2
CM
13262 eh_frame = elf_section_eh_frame_entry (sec);
13263 if (ret && eh_frame && !eh_frame->gc_mark)
13264 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13265 ret = FALSE;
13266
c152c796
AM
13267 return ret;
13268}
13269
3c758495
TG
13270/* Scan and mark sections in a special or debug section group. */
13271
13272static void
13273_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13274{
13275 /* Point to first section of section group. */
13276 asection *ssec;
13277 /* Used to iterate the section group. */
13278 asection *msec;
13279
13280 bfd_boolean is_special_grp = TRUE;
13281 bfd_boolean is_debug_grp = TRUE;
13282
13283 /* First scan to see if group contains any section other than debug
13284 and special section. */
13285 ssec = msec = elf_next_in_group (grp);
13286 do
13287 {
13288 if ((msec->flags & SEC_DEBUGGING) == 0)
13289 is_debug_grp = FALSE;
13290
13291 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13292 is_special_grp = FALSE;
13293
13294 msec = elf_next_in_group (msec);
13295 }
13296 while (msec != ssec);
13297
13298 /* If this is a pure debug section group or pure special section group,
13299 keep all sections in this group. */
13300 if (is_debug_grp || is_special_grp)
13301 {
13302 do
13303 {
13304 msec->gc_mark = 1;
13305 msec = elf_next_in_group (msec);
13306 }
13307 while (msec != ssec);
13308 }
13309}
13310
7f6ab9f8
AM
13311/* Keep debug and special sections. */
13312
13313bfd_boolean
13314_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13315 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13316{
13317 bfd *ibfd;
13318
c72f2fb2 13319 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13320 {
13321 asection *isec;
13322 bfd_boolean some_kept;
b40bf0a2 13323 bfd_boolean debug_frag_seen;
b7c871ed 13324 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13325
13326 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13327 continue;
57963c05
AM
13328 isec = ibfd->sections;
13329 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13330 continue;
7f6ab9f8 13331
b40bf0a2
NC
13332 /* Ensure all linker created sections are kept,
13333 see if any other section is already marked,
13334 and note if we have any fragmented debug sections. */
b7c871ed 13335 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13336 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13337 {
13338 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13339 isec->gc_mark = 1;
eb026f09
AM
13340 else if (isec->gc_mark
13341 && (isec->flags & SEC_ALLOC) != 0
13342 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13343 some_kept = TRUE;
b40bf0a2 13344
535b785f 13345 if (!debug_frag_seen
b40bf0a2
NC
13346 && (isec->flags & SEC_DEBUGGING)
13347 && CONST_STRNEQ (isec->name, ".debug_line."))
13348 debug_frag_seen = TRUE;
7f6ab9f8
AM
13349 }
13350
eb026f09
AM
13351 /* If no non-note alloc section in this file will be kept, then
13352 we can toss out the debug and special sections. */
7f6ab9f8
AM
13353 if (!some_kept)
13354 continue;
13355
13356 /* Keep debug and special sections like .comment when they are
3c758495
TG
13357 not part of a group. Also keep section groups that contain
13358 just debug sections or special sections. */
7f6ab9f8 13359 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13360 {
13361 if ((isec->flags & SEC_GROUP) != 0)
13362 _bfd_elf_gc_mark_debug_special_section_group (isec);
13363 else if (((isec->flags & SEC_DEBUGGING) != 0
13364 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13365 && elf_next_in_group (isec) == NULL)
13366 isec->gc_mark = 1;
b7c871ed
L
13367 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13368 has_kept_debug_info = TRUE;
3c758495 13369 }
b40bf0a2 13370
b40bf0a2
NC
13371 /* Look for CODE sections which are going to be discarded,
13372 and find and discard any fragmented debug sections which
13373 are associated with that code section. */
b7c871ed
L
13374 if (debug_frag_seen)
13375 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13376 if ((isec->flags & SEC_CODE) != 0
13377 && isec->gc_mark == 0)
13378 {
13379 unsigned int ilen;
13380 asection *dsec;
b40bf0a2 13381
b7c871ed 13382 ilen = strlen (isec->name);
b40bf0a2 13383
b7c871ed 13384 /* Association is determined by the name of the debug
07d6d2b8 13385 section containing the name of the code section as
b7c871ed
L
13386 a suffix. For example .debug_line.text.foo is a
13387 debug section associated with .text.foo. */
13388 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13389 {
13390 unsigned int dlen;
b40bf0a2 13391
b7c871ed
L
13392 if (dsec->gc_mark == 0
13393 || (dsec->flags & SEC_DEBUGGING) == 0)
13394 continue;
b40bf0a2 13395
b7c871ed 13396 dlen = strlen (dsec->name);
b40bf0a2 13397
b7c871ed
L
13398 if (dlen > ilen
13399 && strncmp (dsec->name + (dlen - ilen),
13400 isec->name, ilen) == 0)
b40bf0a2 13401 dsec->gc_mark = 0;
b7c871ed 13402 }
b40bf0a2 13403 }
b7c871ed
L
13404
13405 /* Mark debug sections referenced by kept debug sections. */
13406 if (has_kept_debug_info)
13407 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13408 if (isec->gc_mark
13409 && (isec->flags & SEC_DEBUGGING) != 0)
13410 if (!_bfd_elf_gc_mark (info, isec,
13411 elf_gc_mark_debug_section))
13412 return FALSE;
7f6ab9f8
AM
13413 }
13414 return TRUE;
13415}
13416
c152c796 13417static bfd_boolean
ccabcbe5 13418elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13419{
13420 bfd *sub;
ccabcbe5 13421 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13422
c72f2fb2 13423 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13424 {
13425 asection *o;
13426
b19a8f85 13427 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13428 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13429 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13430 continue;
57963c05
AM
13431 o = sub->sections;
13432 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13433 continue;
c152c796
AM
13434
13435 for (o = sub->sections; o != NULL; o = o->next)
13436 {
a33dafc3
L
13437 /* When any section in a section group is kept, we keep all
13438 sections in the section group. If the first member of
13439 the section group is excluded, we will also exclude the
13440 group section. */
13441 if (o->flags & SEC_GROUP)
13442 {
13443 asection *first = elf_next_in_group (o);
13444 o->gc_mark = first->gc_mark;
13445 }
c152c796 13446
1e7eae0d 13447 if (o->gc_mark)
c152c796
AM
13448 continue;
13449
13450 /* Skip sweeping sections already excluded. */
13451 if (o->flags & SEC_EXCLUDE)
13452 continue;
13453
13454 /* Since this is early in the link process, it is simple
13455 to remove a section from the output. */
13456 o->flags |= SEC_EXCLUDE;
13457
c55fe096 13458 if (info->print_gc_sections && o->size != 0)
695344c0 13459 /* xgettext:c-format */
9793eb77 13460 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13461 o, sub);
c152c796
AM
13462 }
13463 }
13464
c152c796
AM
13465 return TRUE;
13466}
13467
13468/* Propagate collected vtable information. This is called through
13469 elf_link_hash_traverse. */
13470
13471static bfd_boolean
13472elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13473{
c152c796 13474 /* Those that are not vtables. */
cbd0eecf
L
13475 if (h->start_stop
13476 || h->u2.vtable == NULL
13477 || h->u2.vtable->parent == NULL)
c152c796
AM
13478 return TRUE;
13479
13480 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13481 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13482 return TRUE;
13483
13484 /* If we've already been done, exit. */
cbd0eecf 13485 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13486 return TRUE;
13487
13488 /* Make sure the parent's table is up to date. */
cbd0eecf 13489 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13490
cbd0eecf 13491 if (h->u2.vtable->used == NULL)
c152c796
AM
13492 {
13493 /* None of this table's entries were referenced. Re-use the
13494 parent's table. */
cbd0eecf
L
13495 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13496 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13497 }
13498 else
13499 {
13500 size_t n;
13501 bfd_boolean *cu, *pu;
13502
13503 /* Or the parent's entries into ours. */
cbd0eecf 13504 cu = h->u2.vtable->used;
c152c796 13505 cu[-1] = TRUE;
cbd0eecf 13506 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13507 if (pu != NULL)
13508 {
13509 const struct elf_backend_data *bed;
13510 unsigned int log_file_align;
13511
13512 bed = get_elf_backend_data (h->root.u.def.section->owner);
13513 log_file_align = bed->s->log_file_align;
cbd0eecf 13514 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13515 while (n--)
13516 {
13517 if (*pu)
13518 *cu = TRUE;
13519 pu++;
13520 cu++;
13521 }
13522 }
13523 }
13524
13525 return TRUE;
13526}
13527
13528static bfd_boolean
13529elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13530{
13531 asection *sec;
13532 bfd_vma hstart, hend;
13533 Elf_Internal_Rela *relstart, *relend, *rel;
13534 const struct elf_backend_data *bed;
13535 unsigned int log_file_align;
13536
c152c796
AM
13537 /* Take care of both those symbols that do not describe vtables as
13538 well as those that are not loaded. */
cbd0eecf
L
13539 if (h->start_stop
13540 || h->u2.vtable == NULL
13541 || h->u2.vtable->parent == NULL)
c152c796
AM
13542 return TRUE;
13543
13544 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13545 || h->root.type == bfd_link_hash_defweak);
13546
13547 sec = h->root.u.def.section;
13548 hstart = h->root.u.def.value;
13549 hend = hstart + h->size;
13550
13551 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13552 if (!relstart)
13553 return *(bfd_boolean *) okp = FALSE;
13554 bed = get_elf_backend_data (sec->owner);
13555 log_file_align = bed->s->log_file_align;
13556
056bafd4 13557 relend = relstart + sec->reloc_count;
c152c796
AM
13558
13559 for (rel = relstart; rel < relend; ++rel)
13560 if (rel->r_offset >= hstart && rel->r_offset < hend)
13561 {
13562 /* If the entry is in use, do nothing. */
cbd0eecf
L
13563 if (h->u2.vtable->used
13564 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13565 {
13566 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13567 if (h->u2.vtable->used[entry])
c152c796
AM
13568 continue;
13569 }
13570 /* Otherwise, kill it. */
13571 rel->r_offset = rel->r_info = rel->r_addend = 0;
13572 }
13573
13574 return TRUE;
13575}
13576
87538722
AM
13577/* Mark sections containing dynamically referenced symbols. When
13578 building shared libraries, we must assume that any visible symbol is
13579 referenced. */
715df9b8 13580
64d03ab5
AM
13581bfd_boolean
13582bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13583{
87538722 13584 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13585 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13586
715df9b8
EB
13587 if ((h->root.type == bfd_link_hash_defined
13588 || h->root.type == bfd_link_hash_defweak)
d664fd41 13589 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13590 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13591 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13592 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13593 && (!bfd_link_executable (info)
22185505 13594 || info->gc_keep_exported
b407645f
AM
13595 || info->export_dynamic
13596 || (h->dynamic
13597 && d != NULL
13598 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13599 && (h->versioned >= versioned
54e8959c
L
13600 || !bfd_hide_sym_by_version (info->version_info,
13601 h->root.root.string)))))
715df9b8
EB
13602 h->root.u.def.section->flags |= SEC_KEEP;
13603
13604 return TRUE;
13605}
3b36f7e6 13606
74f0fb50
AM
13607/* Keep all sections containing symbols undefined on the command-line,
13608 and the section containing the entry symbol. */
13609
13610void
13611_bfd_elf_gc_keep (struct bfd_link_info *info)
13612{
13613 struct bfd_sym_chain *sym;
13614
13615 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13616 {
13617 struct elf_link_hash_entry *h;
13618
13619 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13620 FALSE, FALSE, FALSE);
13621
13622 if (h != NULL
13623 && (h->root.type == bfd_link_hash_defined
13624 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13625 && !bfd_is_abs_section (h->root.u.def.section)
13626 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13627 h->root.u.def.section->flags |= SEC_KEEP;
13628 }
13629}
13630
2f0c68f2
CM
13631bfd_boolean
13632bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13633 struct bfd_link_info *info)
13634{
13635 bfd *ibfd = info->input_bfds;
13636
13637 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13638 {
13639 asection *sec;
13640 struct elf_reloc_cookie cookie;
13641
13642 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13643 continue;
57963c05
AM
13644 sec = ibfd->sections;
13645 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13646 continue;
2f0c68f2
CM
13647
13648 if (!init_reloc_cookie (&cookie, info, ibfd))
13649 return FALSE;
13650
13651 for (sec = ibfd->sections; sec; sec = sec->next)
13652 {
fd361982 13653 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13654 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13655 {
13656 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13657 fini_reloc_cookie_rels (&cookie, sec);
13658 }
13659 }
13660 }
13661 return TRUE;
13662}
13663
c152c796
AM
13664/* Do mark and sweep of unused sections. */
13665
13666bfd_boolean
13667bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13668{
13669 bfd_boolean ok = TRUE;
13670 bfd *sub;
6a5bb875 13671 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13672 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13673 struct elf_link_hash_table *htab;
c152c796 13674
64d03ab5 13675 if (!bed->can_gc_sections
715df9b8 13676 || !is_elf_hash_table (info->hash))
c152c796 13677 {
9793eb77 13678 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13679 return TRUE;
13680 }
13681
74f0fb50 13682 bed->gc_keep (info);
da44f4e5 13683 htab = elf_hash_table (info);
74f0fb50 13684
9d0a14d3
RS
13685 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13686 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13687 for (sub = info->input_bfds;
13688 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13689 sub = sub->link.next)
9d0a14d3
RS
13690 {
13691 asection *sec;
13692 struct elf_reloc_cookie cookie;
13693
57963c05
AM
13694 sec = sub->sections;
13695 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13696 continue;
9d0a14d3 13697 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13698 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13699 {
13700 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13701 if (elf_section_data (sec)->sec_info
13702 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13703 elf_eh_frame_section (sub) = sec;
13704 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13705 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13706 }
13707 }
9d0a14d3 13708
c152c796 13709 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13710 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13711 if (!ok)
13712 return FALSE;
13713
13714 /* Kill the vtable relocations that were not used. */
da44f4e5 13715 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13716 if (!ok)
13717 return FALSE;
13718
715df9b8 13719 /* Mark dynamically referenced symbols. */
22185505 13720 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13721 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13722
715df9b8 13723 /* Grovel through relocs to find out who stays ... */
64d03ab5 13724 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13725 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13726 {
13727 asection *o;
13728
b19a8f85 13729 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13730 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13731 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13732 continue;
13733
57963c05
AM
13734 o = sub->sections;
13735 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13736 continue;
13737
7f6ab9f8
AM
13738 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13739 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13740 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13741 well as FINI_ARRAY sections for ld -r. */
c152c796 13742 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13743 if (!o->gc_mark
13744 && (o->flags & SEC_EXCLUDE) == 0
24007750 13745 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13746 || (bfd_link_relocatable (info)
13747 && ((elf_section_data (o)->this_hdr.sh_type
13748 == SHT_PREINIT_ARRAY)
13749 || (elf_section_data (o)->this_hdr.sh_type
13750 == SHT_INIT_ARRAY)
13751 || (elf_section_data (o)->this_hdr.sh_type
13752 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13753 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13754 && elf_next_in_group (o) == NULL )))
13755 {
13756 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13757 return FALSE;
13758 }
c152c796
AM
13759 }
13760
6a5bb875 13761 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13762 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13763
c152c796 13764 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13765 return elf_gc_sweep (abfd, info);
c152c796
AM
13766}
13767\f
13768/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13769
13770bfd_boolean
13771bfd_elf_gc_record_vtinherit (bfd *abfd,
13772 asection *sec,
13773 struct elf_link_hash_entry *h,
13774 bfd_vma offset)
13775{
13776 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13777 struct elf_link_hash_entry **search, *child;
ef53be89 13778 size_t extsymcount;
c152c796
AM
13779 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13780
13781 /* The sh_info field of the symtab header tells us where the
13782 external symbols start. We don't care about the local symbols at
13783 this point. */
13784 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13785 if (!elf_bad_symtab (abfd))
13786 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13787
13788 sym_hashes = elf_sym_hashes (abfd);
13789 sym_hashes_end = sym_hashes + extsymcount;
13790
13791 /* Hunt down the child symbol, which is in this section at the same
13792 offset as the relocation. */
13793 for (search = sym_hashes; search != sym_hashes_end; ++search)
13794 {
13795 if ((child = *search) != NULL
13796 && (child->root.type == bfd_link_hash_defined
13797 || child->root.type == bfd_link_hash_defweak)
13798 && child->root.u.def.section == sec
13799 && child->root.u.def.value == offset)
13800 goto win;
13801 }
13802
695344c0 13803 /* xgettext:c-format */
9793eb77 13804 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13805 abfd, sec, (uint64_t) offset);
c152c796
AM
13806 bfd_set_error (bfd_error_invalid_operation);
13807 return FALSE;
13808
13809 win:
cbd0eecf 13810 if (!child->u2.vtable)
f6e332e6 13811 {
cbd0eecf
L
13812 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13813 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13814 if (!child->u2.vtable)
f6e332e6
AM
13815 return FALSE;
13816 }
c152c796
AM
13817 if (!h)
13818 {
13819 /* This *should* only be the absolute section. It could potentially
13820 be that someone has defined a non-global vtable though, which
13821 would be bad. It isn't worth paging in the local symbols to be
13822 sure though; that case should simply be handled by the assembler. */
13823
cbd0eecf 13824 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13825 }
13826 else
cbd0eecf 13827 child->u2.vtable->parent = h;
c152c796
AM
13828
13829 return TRUE;
13830}
13831
13832/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13833
13834bfd_boolean
a0ea3a14 13835bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
13836 struct elf_link_hash_entry *h,
13837 bfd_vma addend)
13838{
13839 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13840 unsigned int log_file_align = bed->s->log_file_align;
13841
a0ea3a14
L
13842 if (!h)
13843 {
13844 /* xgettext:c-format */
13845 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13846 abfd, sec);
13847 bfd_set_error (bfd_error_bad_value);
13848 return FALSE;
13849 }
13850
cbd0eecf 13851 if (!h->u2.vtable)
f6e332e6 13852 {
cbd0eecf
L
13853 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13854 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13855 if (!h->u2.vtable)
f6e332e6
AM
13856 return FALSE;
13857 }
13858
cbd0eecf 13859 if (addend >= h->u2.vtable->size)
c152c796
AM
13860 {
13861 size_t size, bytes, file_align;
cbd0eecf 13862 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13863
13864 /* While the symbol is undefined, we have to be prepared to handle
13865 a zero size. */
13866 file_align = 1 << log_file_align;
13867 if (h->root.type == bfd_link_hash_undefined)
13868 size = addend + file_align;
13869 else
13870 {
13871 size = h->size;
13872 if (addend >= size)
13873 {
13874 /* Oops! We've got a reference past the defined end of
13875 the table. This is probably a bug -- shall we warn? */
13876 size = addend + file_align;
13877 }
13878 }
13879 size = (size + file_align - 1) & -file_align;
13880
13881 /* Allocate one extra entry for use as a "done" flag for the
13882 consolidation pass. */
13883 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13884
13885 if (ptr)
13886 {
a50b1753 13887 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13888
13889 if (ptr != NULL)
13890 {
13891 size_t oldbytes;
13892
cbd0eecf 13893 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13894 * sizeof (bfd_boolean));
13895 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13896 }
13897 }
13898 else
a50b1753 13899 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13900
13901 if (ptr == NULL)
13902 return FALSE;
13903
13904 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13905 h->u2.vtable->used = ptr + 1;
13906 h->u2.vtable->size = size;
c152c796
AM
13907 }
13908
cbd0eecf 13909 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13910
13911 return TRUE;
13912}
13913
ae17ab41
CM
13914/* Map an ELF section header flag to its corresponding string. */
13915typedef struct
13916{
13917 char *flag_name;
13918 flagword flag_value;
13919} elf_flags_to_name_table;
13920
13921static elf_flags_to_name_table elf_flags_to_names [] =
13922{
13923 { "SHF_WRITE", SHF_WRITE },
13924 { "SHF_ALLOC", SHF_ALLOC },
13925 { "SHF_EXECINSTR", SHF_EXECINSTR },
13926 { "SHF_MERGE", SHF_MERGE },
13927 { "SHF_STRINGS", SHF_STRINGS },
13928 { "SHF_INFO_LINK", SHF_INFO_LINK},
13929 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13930 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13931 { "SHF_GROUP", SHF_GROUP },
13932 { "SHF_TLS", SHF_TLS },
13933 { "SHF_MASKOS", SHF_MASKOS },
13934 { "SHF_EXCLUDE", SHF_EXCLUDE },
13935};
13936
b9c361e0
JL
13937/* Returns TRUE if the section is to be included, otherwise FALSE. */
13938bfd_boolean
ae17ab41 13939bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13940 struct flag_info *flaginfo,
b9c361e0 13941 asection *section)
ae17ab41 13942{
8b127cbc 13943 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13944
8b127cbc 13945 if (!flaginfo->flags_initialized)
ae17ab41 13946 {
8b127cbc
AM
13947 bfd *obfd = info->output_bfd;
13948 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13949 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13950 int with_hex = 0;
13951 int without_hex = 0;
13952
8b127cbc 13953 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13954 {
b9c361e0 13955 unsigned i;
8b127cbc 13956 flagword (*lookup) (char *);
ae17ab41 13957
8b127cbc
AM
13958 lookup = bed->elf_backend_lookup_section_flags_hook;
13959 if (lookup != NULL)
ae17ab41 13960 {
8b127cbc 13961 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13962
13963 if (hexval != 0)
13964 {
13965 if (tf->with == with_flags)
13966 with_hex |= hexval;
13967 else if (tf->with == without_flags)
13968 without_hex |= hexval;
13969 tf->valid = TRUE;
13970 continue;
13971 }
ae17ab41 13972 }
8b127cbc 13973 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13974 {
8b127cbc 13975 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13976 {
13977 if (tf->with == with_flags)
13978 with_hex |= elf_flags_to_names[i].flag_value;
13979 else if (tf->with == without_flags)
13980 without_hex |= elf_flags_to_names[i].flag_value;
13981 tf->valid = TRUE;
13982 break;
13983 }
13984 }
8b127cbc 13985 if (!tf->valid)
b9c361e0 13986 {
68ffbac6 13987 info->callbacks->einfo
9793eb77 13988 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13989 return FALSE;
ae17ab41
CM
13990 }
13991 }
8b127cbc
AM
13992 flaginfo->flags_initialized = TRUE;
13993 flaginfo->only_with_flags |= with_hex;
13994 flaginfo->not_with_flags |= without_hex;
ae17ab41 13995 }
ae17ab41 13996
8b127cbc 13997 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13998 return FALSE;
13999
8b127cbc 14000 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14001 return FALSE;
14002
14003 return TRUE;
ae17ab41
CM
14004}
14005
c152c796
AM
14006struct alloc_got_off_arg {
14007 bfd_vma gotoff;
10455f89 14008 struct bfd_link_info *info;
c152c796
AM
14009};
14010
14011/* We need a special top-level link routine to convert got reference counts
14012 to real got offsets. */
14013
14014static bfd_boolean
14015elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14016{
a50b1753 14017 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14018 bfd *obfd = gofarg->info->output_bfd;
14019 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14020
c152c796
AM
14021 if (h->got.refcount > 0)
14022 {
14023 h->got.offset = gofarg->gotoff;
10455f89 14024 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14025 }
14026 else
14027 h->got.offset = (bfd_vma) -1;
14028
14029 return TRUE;
14030}
14031
14032/* And an accompanying bit to work out final got entry offsets once
14033 we're done. Should be called from final_link. */
14034
14035bfd_boolean
14036bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14037 struct bfd_link_info *info)
14038{
14039 bfd *i;
14040 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14041 bfd_vma gotoff;
c152c796
AM
14042 struct alloc_got_off_arg gofarg;
14043
10455f89
HPN
14044 BFD_ASSERT (abfd == info->output_bfd);
14045
c152c796
AM
14046 if (! is_elf_hash_table (info->hash))
14047 return FALSE;
14048
14049 /* The GOT offset is relative to the .got section, but the GOT header is
14050 put into the .got.plt section, if the backend uses it. */
14051 if (bed->want_got_plt)
14052 gotoff = 0;
14053 else
14054 gotoff = bed->got_header_size;
14055
14056 /* Do the local .got entries first. */
c72f2fb2 14057 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14058 {
14059 bfd_signed_vma *local_got;
ef53be89 14060 size_t j, locsymcount;
c152c796
AM
14061 Elf_Internal_Shdr *symtab_hdr;
14062
14063 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14064 continue;
14065
14066 local_got = elf_local_got_refcounts (i);
14067 if (!local_got)
14068 continue;
14069
14070 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14071 if (elf_bad_symtab (i))
14072 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14073 else
14074 locsymcount = symtab_hdr->sh_info;
14075
14076 for (j = 0; j < locsymcount; ++j)
14077 {
14078 if (local_got[j] > 0)
14079 {
14080 local_got[j] = gotoff;
10455f89 14081 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14082 }
14083 else
14084 local_got[j] = (bfd_vma) -1;
14085 }
14086 }
14087
14088 /* Then the global .got entries. .plt refcounts are handled by
14089 adjust_dynamic_symbol */
14090 gofarg.gotoff = gotoff;
10455f89 14091 gofarg.info = info;
c152c796
AM
14092 elf_link_hash_traverse (elf_hash_table (info),
14093 elf_gc_allocate_got_offsets,
14094 &gofarg);
14095 return TRUE;
14096}
14097
14098/* Many folk need no more in the way of final link than this, once
14099 got entry reference counting is enabled. */
14100
14101bfd_boolean
14102bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14103{
14104 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14105 return FALSE;
14106
14107 /* Invoke the regular ELF backend linker to do all the work. */
14108 return bfd_elf_final_link (abfd, info);
14109}
14110
14111bfd_boolean
14112bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14113{
a50b1753 14114 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14115
14116 if (rcookie->bad_symtab)
14117 rcookie->rel = rcookie->rels;
14118
14119 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14120 {
14121 unsigned long r_symndx;
14122
14123 if (! rcookie->bad_symtab)
14124 if (rcookie->rel->r_offset > offset)
14125 return FALSE;
14126 if (rcookie->rel->r_offset != offset)
14127 continue;
14128
14129 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14130 if (r_symndx == STN_UNDEF)
c152c796
AM
14131 return TRUE;
14132
14133 if (r_symndx >= rcookie->locsymcount
14134 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14135 {
14136 struct elf_link_hash_entry *h;
14137
14138 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14139
14140 while (h->root.type == bfd_link_hash_indirect
14141 || h->root.type == bfd_link_hash_warning)
14142 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14143
14144 if ((h->root.type == bfd_link_hash_defined
14145 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14146 && (h->root.u.def.section->owner != rcookie->abfd
14147 || h->root.u.def.section->kept_section != NULL
14148 || discarded_section (h->root.u.def.section)))
c152c796 14149 return TRUE;
c152c796
AM
14150 }
14151 else
14152 {
14153 /* It's not a relocation against a global symbol,
14154 but it could be a relocation against a local
14155 symbol for a discarded section. */
14156 asection *isec;
14157 Elf_Internal_Sym *isym;
14158
14159 /* Need to: get the symbol; get the section. */
14160 isym = &rcookie->locsyms[r_symndx];
cb33740c 14161 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14162 if (isec != NULL
14163 && (isec->kept_section != NULL
14164 || discarded_section (isec)))
cb33740c 14165 return TRUE;
c152c796
AM
14166 }
14167 return FALSE;
14168 }
14169 return FALSE;
14170}
14171
14172/* Discard unneeded references to discarded sections.
75938853
AM
14173 Returns -1 on error, 1 if any section's size was changed, 0 if
14174 nothing changed. This function assumes that the relocations are in
14175 sorted order, which is true for all known assemblers. */
c152c796 14176
75938853 14177int
c152c796
AM
14178bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14179{
14180 struct elf_reloc_cookie cookie;
18cd5bce 14181 asection *o;
c152c796 14182 bfd *abfd;
75938853 14183 int changed = 0;
c152c796
AM
14184
14185 if (info->traditional_format
14186 || !is_elf_hash_table (info->hash))
75938853 14187 return 0;
c152c796 14188
18cd5bce
AM
14189 o = bfd_get_section_by_name (output_bfd, ".stab");
14190 if (o != NULL)
c152c796 14191 {
18cd5bce 14192 asection *i;
c152c796 14193
18cd5bce 14194 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14195 {
18cd5bce
AM
14196 if (i->size == 0
14197 || i->reloc_count == 0
14198 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14199 continue;
c152c796 14200
18cd5bce
AM
14201 abfd = i->owner;
14202 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14203 continue;
c152c796 14204
18cd5bce 14205 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14206 return -1;
c152c796 14207
18cd5bce
AM
14208 if (_bfd_discard_section_stabs (abfd, i,
14209 elf_section_data (i)->sec_info,
5241d853
RS
14210 bfd_elf_reloc_symbol_deleted_p,
14211 &cookie))
75938853 14212 changed = 1;
18cd5bce
AM
14213
14214 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14215 }
18cd5bce
AM
14216 }
14217
2f0c68f2
CM
14218 o = NULL;
14219 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14220 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14221 if (o != NULL)
14222 {
14223 asection *i;
d7153c4a 14224 int eh_changed = 0;
79a94a2a 14225 unsigned int eh_alignment;
c152c796 14226
18cd5bce 14227 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14228 {
18cd5bce
AM
14229 if (i->size == 0)
14230 continue;
14231
14232 abfd = i->owner;
14233 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14234 continue;
14235
14236 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14237 return -1;
18cd5bce
AM
14238
14239 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14240 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14241 bfd_elf_reloc_symbol_deleted_p,
14242 &cookie))
d7153c4a
AM
14243 {
14244 eh_changed = 1;
14245 if (i->size != i->rawsize)
14246 changed = 1;
14247 }
18cd5bce
AM
14248
14249 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14250 }
9866ffe2 14251
79a94a2a 14252 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14253 /* Skip over zero terminator, and prevent empty sections from
14254 adding alignment padding at the end. */
14255 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14256 if (i->size == 0)
14257 i->flags |= SEC_EXCLUDE;
14258 else if (i->size > 4)
14259 break;
14260 /* The last non-empty eh_frame section doesn't need padding. */
14261 if (i != NULL)
14262 i = i->map_tail.s;
14263 /* Any prior sections must pad the last FDE out to the output
14264 section alignment. Otherwise we might have zero padding
14265 between sections, which would be seen as a terminator. */
14266 for (; i != NULL; i = i->map_tail.s)
14267 if (i->size == 4)
14268 /* All but the last zero terminator should have been removed. */
14269 BFD_FAIL ();
14270 else
14271 {
14272 bfd_size_type size
14273 = (i->size + eh_alignment - 1) & -eh_alignment;
14274 if (i->size != size)
af471f82 14275 {
9866ffe2
AM
14276 i->size = size;
14277 changed = 1;
14278 eh_changed = 1;
af471f82 14279 }
9866ffe2 14280 }
d7153c4a
AM
14281 if (eh_changed)
14282 elf_link_hash_traverse (elf_hash_table (info),
14283 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14284 }
c152c796 14285
18cd5bce
AM
14286 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14287 {
14288 const struct elf_backend_data *bed;
57963c05 14289 asection *s;
c152c796 14290
18cd5bce
AM
14291 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14292 continue;
57963c05
AM
14293 s = abfd->sections;
14294 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14295 continue;
18cd5bce
AM
14296
14297 bed = get_elf_backend_data (abfd);
14298
14299 if (bed->elf_backend_discard_info != NULL)
14300 {
14301 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14302 return -1;
18cd5bce
AM
14303
14304 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14305 changed = 1;
18cd5bce
AM
14306
14307 fini_reloc_cookie (&cookie, abfd);
14308 }
c152c796
AM
14309 }
14310
2f0c68f2
CM
14311 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14312 _bfd_elf_end_eh_frame_parsing (info);
14313
14314 if (info->eh_frame_hdr_type
0e1862bb 14315 && !bfd_link_relocatable (info)
c152c796 14316 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14317 changed = 1;
c152c796 14318
75938853 14319 return changed;
c152c796 14320}
082b7297 14321
43e1669b 14322bfd_boolean
0c511000 14323_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14324 asection *sec,
c0f00686 14325 struct bfd_link_info *info)
082b7297
L
14326{
14327 flagword flags;
c77ec726 14328 const char *name, *key;
082b7297
L
14329 struct bfd_section_already_linked *l;
14330 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14331
c77ec726
AM
14332 if (sec->output_section == bfd_abs_section_ptr)
14333 return FALSE;
0c511000 14334
c77ec726 14335 flags = sec->flags;
0c511000 14336
c77ec726
AM
14337 /* Return if it isn't a linkonce section. A comdat group section
14338 also has SEC_LINK_ONCE set. */
14339 if ((flags & SEC_LINK_ONCE) == 0)
14340 return FALSE;
0c511000 14341
c77ec726
AM
14342 /* Don't put group member sections on our list of already linked
14343 sections. They are handled as a group via their group section. */
14344 if (elf_sec_group (sec) != NULL)
14345 return FALSE;
0c511000 14346
c77ec726
AM
14347 /* For a SHT_GROUP section, use the group signature as the key. */
14348 name = sec->name;
14349 if ((flags & SEC_GROUP) != 0
14350 && elf_next_in_group (sec) != NULL
14351 && elf_group_name (elf_next_in_group (sec)) != NULL)
14352 key = elf_group_name (elf_next_in_group (sec));
14353 else
14354 {
14355 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14356 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14357 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14358 key++;
0c511000 14359 else
c77ec726
AM
14360 /* Must be a user linkonce section that doesn't follow gcc's
14361 naming convention. In this case we won't be matching
14362 single member groups. */
14363 key = name;
0c511000 14364 }
6d2cd210 14365
c77ec726 14366 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14367
14368 for (l = already_linked_list->entry; l != NULL; l = l->next)
14369 {
c2370991 14370 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14371 sections with a signature of <key> (<key> is some string),
14372 and linkonce sections named .gnu.linkonce.<type>.<key>.
14373 Match like sections. LTO plugin sections are an exception.
14374 They are always named .gnu.linkonce.t.<key> and match either
14375 type of section. */
14376 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14377 && ((flags & SEC_GROUP) != 0
14378 || strcmp (name, l->sec->name) == 0))
14379 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14380 {
14381 /* The section has already been linked. See if we should
6d2cd210 14382 issue a warning. */
c77ec726
AM
14383 if (!_bfd_handle_already_linked (sec, l, info))
14384 return FALSE;
082b7297 14385
c77ec726 14386 if (flags & SEC_GROUP)
3d7f7666 14387 {
c77ec726
AM
14388 asection *first = elf_next_in_group (sec);
14389 asection *s = first;
3d7f7666 14390
c77ec726 14391 while (s != NULL)
3d7f7666 14392 {
c77ec726
AM
14393 s->output_section = bfd_abs_section_ptr;
14394 /* Record which group discards it. */
14395 s->kept_section = l->sec;
14396 s = elf_next_in_group (s);
14397 /* These lists are circular. */
14398 if (s == first)
14399 break;
3d7f7666
L
14400 }
14401 }
082b7297 14402
43e1669b 14403 return TRUE;
082b7297
L
14404 }
14405 }
14406
c77ec726
AM
14407 /* A single member comdat group section may be discarded by a
14408 linkonce section and vice versa. */
14409 if ((flags & SEC_GROUP) != 0)
3d7f7666 14410 {
c77ec726 14411 asection *first = elf_next_in_group (sec);
c2370991 14412
c77ec726
AM
14413 if (first != NULL && elf_next_in_group (first) == first)
14414 /* Check this single member group against linkonce sections. */
14415 for (l = already_linked_list->entry; l != NULL; l = l->next)
14416 if ((l->sec->flags & SEC_GROUP) == 0
14417 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14418 {
14419 first->output_section = bfd_abs_section_ptr;
14420 first->kept_section = l->sec;
14421 sec->output_section = bfd_abs_section_ptr;
14422 break;
14423 }
14424 }
14425 else
14426 /* Check this linkonce section against single member groups. */
14427 for (l = already_linked_list->entry; l != NULL; l = l->next)
14428 if (l->sec->flags & SEC_GROUP)
6d2cd210 14429 {
c77ec726 14430 asection *first = elf_next_in_group (l->sec);
6d2cd210 14431
c77ec726
AM
14432 if (first != NULL
14433 && elf_next_in_group (first) == first
14434 && bfd_elf_match_symbols_in_sections (first, sec, info))
14435 {
14436 sec->output_section = bfd_abs_section_ptr;
14437 sec->kept_section = first;
14438 break;
14439 }
6d2cd210 14440 }
0c511000 14441
c77ec726
AM
14442 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14443 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14444 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14445 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14446 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14447 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14448 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14449 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14450 The reverse order cannot happen as there is never a bfd with only the
14451 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14452 matter as here were are looking only for cross-bfd sections. */
14453
14454 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14455 for (l = already_linked_list->entry; l != NULL; l = l->next)
14456 if ((l->sec->flags & SEC_GROUP) == 0
14457 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14458 {
14459 if (abfd != l->sec->owner)
14460 sec->output_section = bfd_abs_section_ptr;
14461 break;
14462 }
80c29487 14463
082b7297 14464 /* This is the first section with this name. Record it. */
c77ec726 14465 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14466 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14467 return sec->output_section == bfd_abs_section_ptr;
082b7297 14468}
81e1b023 14469
a4d8e49b
L
14470bfd_boolean
14471_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14472{
14473 return sym->st_shndx == SHN_COMMON;
14474}
14475
14476unsigned int
14477_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14478{
14479 return SHN_COMMON;
14480}
14481
14482asection *
14483_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14484{
14485 return bfd_com_section_ptr;
14486}
10455f89
HPN
14487
14488bfd_vma
14489_bfd_elf_default_got_elt_size (bfd *abfd,
14490 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14491 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14492 bfd *ibfd ATTRIBUTE_UNUSED,
14493 unsigned long symndx ATTRIBUTE_UNUSED)
14494{
14495 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14496 return bed->s->arch_size / 8;
14497}
83bac4b0
NC
14498
14499/* Routines to support the creation of dynamic relocs. */
14500
83bac4b0
NC
14501/* Returns the name of the dynamic reloc section associated with SEC. */
14502
14503static const char *
14504get_dynamic_reloc_section_name (bfd * abfd,
14505 asection * sec,
14506 bfd_boolean is_rela)
14507{
ddcf1fcf 14508 char *name;
fd361982 14509 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14510 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14511
ddcf1fcf 14512 if (old_name == NULL)
83bac4b0
NC
14513 return NULL;
14514
ddcf1fcf 14515 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14516 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14517
14518 return name;
14519}
14520
14521/* Returns the dynamic reloc section associated with SEC.
14522 If necessary compute the name of the dynamic reloc section based
14523 on SEC's name (looked up in ABFD's string table) and the setting
14524 of IS_RELA. */
14525
14526asection *
14527_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14528 asection * sec,
14529 bfd_boolean is_rela)
14530{
14531 asection * reloc_sec = elf_section_data (sec)->sreloc;
14532
14533 if (reloc_sec == NULL)
14534 {
14535 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14536
14537 if (name != NULL)
14538 {
3d4d4302 14539 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14540
14541 if (reloc_sec != NULL)
14542 elf_section_data (sec)->sreloc = reloc_sec;
14543 }
14544 }
14545
14546 return reloc_sec;
14547}
14548
14549/* Returns the dynamic reloc section associated with SEC. If the
14550 section does not exist it is created and attached to the DYNOBJ
14551 bfd and stored in the SRELOC field of SEC's elf_section_data
14552 structure.
f8076f98 14553
83bac4b0
NC
14554 ALIGNMENT is the alignment for the newly created section and
14555 IS_RELA defines whether the name should be .rela.<SEC's name>
14556 or .rel.<SEC's name>. The section name is looked up in the
14557 string table associated with ABFD. */
14558
14559asection *
ca4be51c
AM
14560_bfd_elf_make_dynamic_reloc_section (asection *sec,
14561 bfd *dynobj,
14562 unsigned int alignment,
14563 bfd *abfd,
14564 bfd_boolean is_rela)
83bac4b0
NC
14565{
14566 asection * reloc_sec = elf_section_data (sec)->sreloc;
14567
14568 if (reloc_sec == NULL)
14569 {
14570 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14571
14572 if (name == NULL)
14573 return NULL;
14574
3d4d4302 14575 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14576
14577 if (reloc_sec == NULL)
14578 {
3d4d4302
AM
14579 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14580 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14581 if ((sec->flags & SEC_ALLOC) != 0)
14582 flags |= SEC_ALLOC | SEC_LOAD;
14583
3d4d4302 14584 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14585 if (reloc_sec != NULL)
14586 {
8877b5e5
AM
14587 /* _bfd_elf_get_sec_type_attr chooses a section type by
14588 name. Override as it may be wrong, eg. for a user
14589 section named "auto" we'll get ".relauto" which is
14590 seen to be a .rela section. */
14591 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14592 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14593 reloc_sec = NULL;
14594 }
14595 }
14596
14597 elf_section_data (sec)->sreloc = reloc_sec;
14598 }
14599
14600 return reloc_sec;
14601}
1338dd10 14602
bffebb6b
AM
14603/* Copy the ELF symbol type and other attributes for a linker script
14604 assignment from HSRC to HDEST. Generally this should be treated as
14605 if we found a strong non-dynamic definition for HDEST (except that
14606 ld ignores multiple definition errors). */
1338dd10 14607void
bffebb6b
AM
14608_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14609 struct bfd_link_hash_entry *hdest,
14610 struct bfd_link_hash_entry *hsrc)
1338dd10 14611{
bffebb6b
AM
14612 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14613 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14614 Elf_Internal_Sym isym;
1338dd10
PB
14615
14616 ehdest->type = ehsrc->type;
35fc36a8 14617 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14618
14619 isym.st_other = ehsrc->other;
b8417128 14620 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14621}
351f65ca
L
14622
14623/* Append a RELA relocation REL to section S in BFD. */
14624
14625void
14626elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14627{
14628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14629 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14630 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14631 bed->s->swap_reloca_out (abfd, rel, loc);
14632}
14633
14634/* Append a REL relocation REL to section S in BFD. */
14635
14636void
14637elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14638{
14639 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14640 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14641 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14642 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14643}
7dba9362
AM
14644
14645/* Define __start, __stop, .startof. or .sizeof. symbol. */
14646
14647struct bfd_link_hash_entry *
14648bfd_elf_define_start_stop (struct bfd_link_info *info,
14649 const char *symbol, asection *sec)
14650{
487b6440 14651 struct elf_link_hash_entry *h;
7dba9362 14652
487b6440
AM
14653 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14654 FALSE, FALSE, TRUE);
14655 if (h != NULL
14656 && (h->root.type == bfd_link_hash_undefined
14657 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14658 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14659 {
bf3077a6 14660 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14661 h->root.type = bfd_link_hash_defined;
14662 h->root.u.def.section = sec;
14663 h->root.u.def.value = 0;
14664 h->def_regular = 1;
14665 h->def_dynamic = 0;
14666 h->start_stop = 1;
14667 h->u2.start_stop_section = sec;
14668 if (symbol[0] == '.')
14669 {
14670 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14671 const struct elf_backend_data *bed;
14672 bed = get_elf_backend_data (info->output_bfd);
14673 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14674 }
36b8fda5
AM
14675 else
14676 {
14677 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14678 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14679 if (was_dynamic)
36b8fda5
AM
14680 bfd_elf_link_record_dynamic_symbol (info, h);
14681 }
487b6440 14682 return &h->root;
7dba9362 14683 }
487b6440 14684 return NULL;
7dba9362 14685}
This page took 2.594116 seconds and 4 git commands to generate.