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