BFD/ELF: Correct a `remove' global shadowing error for pre-4.8 GCC
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
219d1afa 2 Copyright (C) 1995-2018 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"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
07d6d2b8 80 return h->root.u.def.section;
2f0c68f2
CM
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
57963c05 221 asection *s;
6cd255ca 222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 223 if ((ibfd->flags
57963c05
AM
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
55255dae
L
595}
596
45d6a902
AM
597/* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600bfd_boolean
fe21a8fc
L
601bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
268b6b39 603 const char *name,
fe21a8fc
L
604 bfd_boolean provide,
605 bfd_boolean hidden)
45d6a902 606{
00cbee0a 607 struct elf_link_hash_entry *h, *hv;
4ea42fb7 608 struct elf_link_hash_table *htab;
00cbee0a 609 const struct elf_backend_data *bed;
45d6a902 610
0eddce27 611 if (!is_elf_hash_table (info->hash))
45d6a902
AM
612 return TRUE;
613
4ea42fb7
AM
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 616 if (h == NULL)
4ea42fb7 617 return provide;
45d6a902 618
8e2a4f11
AM
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
0f550b3d
L
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
73ec947d
AM
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
00cbee0a 643 switch (h->root.type)
77cfaee6 644 {
00cbee0a
L
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
4ea42fb7 654 h->root.type = bfd_link_hash_new;
77cfaee6
AM
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
657 break;
658 case bfd_link_hash_new:
00cbee0a
L
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 662 the versioned symbol point to this one. */
00cbee0a
L
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
8e2a4f11
AM
675 default:
676 BFD_FAIL ();
c2596ca5 677 return FALSE;
55255dae 678 }
45d6a902
AM
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
45d6a902
AM
687 h->root.type = bfd_link_hash_undefined;
688
689 /* If this symbol is not being provided by the linker script, and it is
690 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
691 then clear out any version information because the symbol will not be
692 associated with the dynamic object any more. */
45d6a902 693 if (!provide
f5385ebf
AM
694 && h->def_dynamic
695 && !h->def_regular)
b531344c
MR
696 h->verinfo.verdef = NULL;
697
698 /* Make sure this symbol is not garbage collected. */
699 h->mark = 1;
45d6a902 700
f5385ebf 701 h->def_regular = 1;
45d6a902 702
eb8476a6 703 if (hidden)
fe21a8fc 704 {
91d6fa6a 705 bed = get_elf_backend_data (output_bfd);
b8297068
AM
706 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
707 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
708 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
709 }
710
6fa3860b
PB
711 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
712 and executables. */
0e1862bb 713 if (!bfd_link_relocatable (info)
6fa3860b
PB
714 && h->dynindx != -1
715 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
716 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
717 h->forced_local = 1;
718
f5385ebf
AM
719 if ((h->def_dynamic
720 || h->ref_dynamic
6b3b0ab8
L
721 || bfd_link_dll (info)
722 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 723 && !h->forced_local
45d6a902
AM
724 && h->dynindx == -1)
725 {
c152c796 726 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
727 return FALSE;
728
729 /* If this is a weak defined symbol, and we know a corresponding
730 real symbol from the same dynamic object, make sure the real
731 symbol is also made into a dynamic symbol. */
60d67dc8 732 if (h->is_weakalias)
45d6a902 733 {
60d67dc8
AM
734 struct elf_link_hash_entry *def = weakdef (h);
735
736 if (def->dynindx == -1
737 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
738 return FALSE;
739 }
740 }
741
742 return TRUE;
743}
42751cf3 744
8c58d23b
AM
745/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
746 success, and 2 on a failure caused by attempting to record a symbol
747 in a discarded section, eg. a discarded link-once section symbol. */
748
749int
c152c796
AM
750bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
751 bfd *input_bfd,
752 long input_indx)
8c58d23b
AM
753{
754 bfd_size_type amt;
755 struct elf_link_local_dynamic_entry *entry;
756 struct elf_link_hash_table *eht;
757 struct elf_strtab_hash *dynstr;
ef53be89 758 size_t dynstr_index;
8c58d23b
AM
759 char *name;
760 Elf_External_Sym_Shndx eshndx;
761 char esym[sizeof (Elf64_External_Sym)];
762
0eddce27 763 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
764 return 0;
765
766 /* See if the entry exists already. */
767 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
768 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
769 return 1;
770
771 amt = sizeof (*entry);
a50b1753 772 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
773 if (entry == NULL)
774 return 0;
775
776 /* Go find the symbol, so that we can find it's name. */
777 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 778 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
779 {
780 bfd_release (input_bfd, entry);
781 return 0;
782 }
783
784 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 785 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
786 {
787 asection *s;
788
789 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
790 if (s == NULL || bfd_is_abs_section (s->output_section))
791 {
792 /* We can still bfd_release here as nothing has done another
793 bfd_alloc. We can't do this later in this function. */
794 bfd_release (input_bfd, entry);
795 return 2;
796 }
797 }
798
799 name = (bfd_elf_string_from_elf_section
800 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
801 entry->isym.st_name));
802
803 dynstr = elf_hash_table (info)->dynstr;
804 if (dynstr == NULL)
805 {
806 /* Create a strtab to hold the dynamic symbol names. */
807 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
808 if (dynstr == NULL)
809 return 0;
810 }
811
b34976b6 812 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 813 if (dynstr_index == (size_t) -1)
8c58d23b
AM
814 return 0;
815 entry->isym.st_name = dynstr_index;
816
817 eht = elf_hash_table (info);
818
819 entry->next = eht->dynlocal;
820 eht->dynlocal = entry;
821 entry->input_bfd = input_bfd;
822 entry->input_indx = input_indx;
823 eht->dynsymcount++;
824
825 /* Whatever binding the symbol had before, it's now local. */
826 entry->isym.st_info
827 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
828
829 /* The dynindx will be set at the end of size_dynamic_sections. */
830
831 return 1;
832}
833
30b30c21 834/* Return the dynindex of a local dynamic symbol. */
42751cf3 835
30b30c21 836long
268b6b39
AM
837_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
838 bfd *input_bfd,
839 long input_indx)
30b30c21
RH
840{
841 struct elf_link_local_dynamic_entry *e;
842
843 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
844 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
845 return e->dynindx;
846 return -1;
847}
848
849/* This function is used to renumber the dynamic symbols, if some of
850 them are removed because they are marked as local. This is called
851 via elf_link_hash_traverse. */
852
b34976b6 853static bfd_boolean
268b6b39
AM
854elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
855 void *data)
42751cf3 856{
a50b1753 857 size_t *count = (size_t *) data;
30b30c21 858
6fa3860b
PB
859 if (h->forced_local)
860 return TRUE;
861
862 if (h->dynindx != -1)
863 h->dynindx = ++(*count);
864
865 return TRUE;
866}
867
868
869/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
870 STB_LOCAL binding. */
871
872static bfd_boolean
873elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
874 void *data)
875{
a50b1753 876 size_t *count = (size_t *) data;
6fa3860b 877
6fa3860b
PB
878 if (!h->forced_local)
879 return TRUE;
880
42751cf3 881 if (h->dynindx != -1)
30b30c21
RH
882 h->dynindx = ++(*count);
883
b34976b6 884 return TRUE;
42751cf3 885}
30b30c21 886
aee6f5b4
AO
887/* Return true if the dynamic symbol for a given section should be
888 omitted when creating a shared library. */
889bfd_boolean
d00dd7dc
AM
890_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
891 struct bfd_link_info *info,
892 asection *p)
aee6f5b4 893{
74541ad4 894 struct elf_link_hash_table *htab;
ca55926c 895 asection *ip;
74541ad4 896
aee6f5b4
AO
897 switch (elf_section_data (p)->this_hdr.sh_type)
898 {
899 case SHT_PROGBITS:
900 case SHT_NOBITS:
901 /* If sh_type is yet undecided, assume it could be
902 SHT_PROGBITS/SHT_NOBITS. */
903 case SHT_NULL:
74541ad4
AM
904 htab = elf_hash_table (info);
905 if (p == htab->tls_sec)
906 return FALSE;
907
908 if (htab->text_index_section != NULL)
909 return p != htab->text_index_section && p != htab->data_index_section;
910
ca55926c 911 return (htab->dynobj != NULL
3d4d4302 912 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 913 && ip->output_section == p);
aee6f5b4
AO
914
915 /* There shouldn't be section relative relocations
916 against any other section. */
917 default:
918 return TRUE;
919 }
920}
921
d00dd7dc
AM
922bfd_boolean
923_bfd_elf_omit_section_dynsym_all
924 (bfd *output_bfd ATTRIBUTE_UNUSED,
925 struct bfd_link_info *info ATTRIBUTE_UNUSED,
926 asection *p ATTRIBUTE_UNUSED)
927{
928 return TRUE;
929}
930
062e2358 931/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
932 symbol for each output section, which come first. Next come symbols
933 which have been forced to local binding. Then all of the back-end
934 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
935 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
936 (This prevents the early call before elf_backend_init_index_section
937 and strip_excluded_output_sections setting dynindx for sections
938 that are stripped.) */
30b30c21 939
554220db
AM
940static unsigned long
941_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
942 struct bfd_link_info *info,
943 unsigned long *section_sym_count)
30b30c21
RH
944{
945 unsigned long dynsymcount = 0;
63f452a8 946 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 947
0e1862bb
L
948 if (bfd_link_pic (info)
949 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 950 {
aee6f5b4 951 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
952 asection *p;
953 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 954 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 955 && (p->flags & SEC_ALLOC) != 0
7f923b7f 956 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 957 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
958 {
959 ++dynsymcount;
960 if (do_sec)
961 elf_section_data (p)->dynindx = dynsymcount;
962 }
963 else if (do_sec)
74541ad4 964 elf_section_data (p)->dynindx = 0;
30b30c21 965 }
63f452a8
AM
966 if (do_sec)
967 *section_sym_count = dynsymcount;
30b30c21 968
6fa3860b
PB
969 elf_link_hash_traverse (elf_hash_table (info),
970 elf_link_renumber_local_hash_table_dynsyms,
971 &dynsymcount);
972
30b30c21
RH
973 if (elf_hash_table (info)->dynlocal)
974 {
975 struct elf_link_local_dynamic_entry *p;
976 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
977 p->dynindx = ++dynsymcount;
978 }
90ac2420 979 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
980
981 elf_link_hash_traverse (elf_hash_table (info),
982 elf_link_renumber_hash_table_dynsyms,
983 &dynsymcount);
984
d5486c43
L
985 /* There is an unused NULL entry at the head of the table which we
986 must account for in our count even if the table is empty since it
987 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
988 .dynamic section. */
989 dynsymcount++;
30b30c21 990
ccabcbe5
AM
991 elf_hash_table (info)->dynsymcount = dynsymcount;
992 return dynsymcount;
30b30c21 993}
252b5132 994
54ac0771
L
995/* Merge st_other field. */
996
997static void
998elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 999 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 1000 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1001{
1002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1003
1004 /* If st_other has a processor-specific meaning, specific
cd3416da 1005 code might be needed here. */
54ac0771
L
1006 if (bed->elf_backend_merge_symbol_attribute)
1007 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1008 dynamic);
1009
cd3416da 1010 if (!dynamic)
54ac0771 1011 {
cd3416da
AM
1012 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1013 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1014
cd3416da
AM
1015 /* Keep the most constraining visibility. Leave the remainder
1016 of the st_other field to elf_backend_merge_symbol_attribute. */
1017 if (symvis - 1 < hvis - 1)
1018 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1019 }
b8417128
AM
1020 else if (definition
1021 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1022 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1023 h->protected_def = 1;
54ac0771
L
1024}
1025
4f3fedcf
AM
1026/* This function is called when we want to merge a new symbol with an
1027 existing symbol. It handles the various cases which arise when we
1028 find a definition in a dynamic object, or when there is already a
1029 definition in a dynamic object. The new symbol is described by
1030 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1031 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1032 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1033 of an old common symbol. We set OVERRIDE if the old symbol is
1034 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1035 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1036 to change. By OK to change, we mean that we shouldn't warn if the
1037 type or size does change. */
45d6a902 1038
8a56bd02 1039static bfd_boolean
268b6b39
AM
1040_bfd_elf_merge_symbol (bfd *abfd,
1041 struct bfd_link_info *info,
1042 const char *name,
1043 Elf_Internal_Sym *sym,
1044 asection **psec,
1045 bfd_vma *pvalue,
4f3fedcf
AM
1046 struct elf_link_hash_entry **sym_hash,
1047 bfd **poldbfd,
37a9e49a 1048 bfd_boolean *pold_weak,
af44c138 1049 unsigned int *pold_alignment,
268b6b39
AM
1050 bfd_boolean *skip,
1051 bfd_boolean *override,
1052 bfd_boolean *type_change_ok,
6e33951e
L
1053 bfd_boolean *size_change_ok,
1054 bfd_boolean *matched)
252b5132 1055{
7479dfd4 1056 asection *sec, *oldsec;
45d6a902 1057 struct elf_link_hash_entry *h;
90c984fc 1058 struct elf_link_hash_entry *hi;
45d6a902
AM
1059 struct elf_link_hash_entry *flip;
1060 int bind;
1061 bfd *oldbfd;
1062 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1063 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1064 const struct elf_backend_data *bed;
6e33951e 1065 char *new_version;
93f4de39 1066 bfd_boolean default_sym = *matched;
45d6a902
AM
1067
1068 *skip = FALSE;
1069 *override = FALSE;
1070
1071 sec = *psec;
1072 bind = ELF_ST_BIND (sym->st_info);
1073
1074 if (! bfd_is_und_section (sec))
1075 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1076 else
1077 h = ((struct elf_link_hash_entry *)
1078 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1079 if (h == NULL)
1080 return FALSE;
1081 *sym_hash = h;
252b5132 1082
88ba32a0
L
1083 bed = get_elf_backend_data (abfd);
1084
6e33951e 1085 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1086 if (h->versioned != unversioned)
6e33951e 1087 {
422f1182
L
1088 /* Symbol version is unknown or versioned. */
1089 new_version = strrchr (name, ELF_VER_CHR);
1090 if (new_version)
1091 {
1092 if (h->versioned == unknown)
1093 {
1094 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1095 h->versioned = versioned_hidden;
1096 else
1097 h->versioned = versioned;
1098 }
1099 new_version += 1;
1100 if (new_version[0] == '\0')
1101 new_version = NULL;
1102 }
1103 else
1104 h->versioned = unversioned;
6e33951e 1105 }
422f1182
L
1106 else
1107 new_version = NULL;
6e33951e 1108
90c984fc
L
1109 /* For merging, we only care about real symbols. But we need to make
1110 sure that indirect symbol dynamic flags are updated. */
1111 hi = h;
45d6a902
AM
1112 while (h->root.type == bfd_link_hash_indirect
1113 || h->root.type == bfd_link_hash_warning)
1114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1115
6e33951e
L
1116 if (!*matched)
1117 {
1118 if (hi == h || h->root.type == bfd_link_hash_new)
1119 *matched = TRUE;
1120 else
1121 {
ae7683d2 1122 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1123 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1124 true if the new symbol is only visible to the symbol with
6e33951e 1125 the same symbol version. */
422f1182
L
1126 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1127 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1128 if (!old_hidden && !new_hidden)
1129 /* The new symbol matches the existing symbol if both
1130 aren't hidden. */
1131 *matched = TRUE;
1132 else
1133 {
1134 /* OLD_VERSION is the symbol version of the existing
1135 symbol. */
422f1182
L
1136 char *old_version;
1137
1138 if (h->versioned >= versioned)
1139 old_version = strrchr (h->root.root.string,
1140 ELF_VER_CHR) + 1;
1141 else
1142 old_version = NULL;
6e33951e
L
1143
1144 /* The new symbol matches the existing symbol if they
1145 have the same symbol version. */
1146 *matched = (old_version == new_version
1147 || (old_version != NULL
1148 && new_version != NULL
1149 && strcmp (old_version, new_version) == 0));
1150 }
1151 }
1152 }
1153
934bce08
AM
1154 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1155 existing symbol. */
1156
1157 oldbfd = NULL;
1158 oldsec = NULL;
1159 switch (h->root.type)
1160 {
1161 default:
1162 break;
1163
1164 case bfd_link_hash_undefined:
1165 case bfd_link_hash_undefweak:
1166 oldbfd = h->root.u.undef.abfd;
1167 break;
1168
1169 case bfd_link_hash_defined:
1170 case bfd_link_hash_defweak:
1171 oldbfd = h->root.u.def.section->owner;
1172 oldsec = h->root.u.def.section;
1173 break;
1174
1175 case bfd_link_hash_common:
1176 oldbfd = h->root.u.c.p->section->owner;
1177 oldsec = h->root.u.c.p->section;
1178 if (pold_alignment)
1179 *pold_alignment = h->root.u.c.p->alignment_power;
1180 break;
1181 }
1182 if (poldbfd && *poldbfd == NULL)
1183 *poldbfd = oldbfd;
1184
1185 /* Differentiate strong and weak symbols. */
1186 newweak = bind == STB_WEAK;
1187 oldweak = (h->root.type == bfd_link_hash_defweak
1188 || h->root.type == bfd_link_hash_undefweak);
1189 if (pold_weak)
1190 *pold_weak = oldweak;
1191
40b36307 1192 /* We have to check it for every instance since the first few may be
ee659f1f 1193 references and not all compilers emit symbol type for undefined
40b36307
L
1194 symbols. */
1195 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1196
ee659f1f
AM
1197 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1198 respectively, is from a dynamic object. */
1199
1200 newdyn = (abfd->flags & DYNAMIC) != 0;
1201
1202 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1203 syms and defined syms in dynamic libraries respectively.
1204 ref_dynamic on the other hand can be set for a symbol defined in
1205 a dynamic library, and def_dynamic may not be set; When the
1206 definition in a dynamic lib is overridden by a definition in the
1207 executable use of the symbol in the dynamic lib becomes a
1208 reference to the executable symbol. */
1209 if (newdyn)
1210 {
1211 if (bfd_is_und_section (sec))
1212 {
1213 if (bind != STB_WEAK)
1214 {
1215 h->ref_dynamic_nonweak = 1;
1216 hi->ref_dynamic_nonweak = 1;
1217 }
1218 }
1219 else
1220 {
6e33951e
L
1221 /* Update the existing symbol only if they match. */
1222 if (*matched)
1223 h->dynamic_def = 1;
ee659f1f
AM
1224 hi->dynamic_def = 1;
1225 }
1226 }
1227
45d6a902
AM
1228 /* If we just created the symbol, mark it as being an ELF symbol.
1229 Other than that, there is nothing to do--there is no merge issue
1230 with a newly defined symbol--so we just return. */
1231
1232 if (h->root.type == bfd_link_hash_new)
252b5132 1233 {
f5385ebf 1234 h->non_elf = 0;
45d6a902
AM
1235 return TRUE;
1236 }
252b5132 1237
45d6a902
AM
1238 /* In cases involving weak versioned symbols, we may wind up trying
1239 to merge a symbol with itself. Catch that here, to avoid the
1240 confusion that results if we try to override a symbol with
1241 itself. The additional tests catch cases like
1242 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1243 dynamic object, which we do want to handle here. */
1244 if (abfd == oldbfd
895fa45f 1245 && (newweak || oldweak)
45d6a902 1246 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1247 || !h->def_regular))
45d6a902
AM
1248 return TRUE;
1249
707bba77 1250 olddyn = FALSE;
45d6a902
AM
1251 if (oldbfd != NULL)
1252 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1253 else if (oldsec != NULL)
45d6a902 1254 {
707bba77 1255 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1256 indices used by MIPS ELF. */
707bba77 1257 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1258 }
252b5132 1259
1a3b5c34
AM
1260 /* Handle a case where plugin_notice won't be called and thus won't
1261 set the non_ir_ref flags on the first pass over symbols. */
1262 if (oldbfd != NULL
1263 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1264 && newdyn != olddyn)
1265 {
1266 h->root.non_ir_ref_dynamic = TRUE;
1267 hi->root.non_ir_ref_dynamic = TRUE;
1268 }
1269
45d6a902
AM
1270 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1271 respectively, appear to be a definition rather than reference. */
1272
707bba77 1273 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1274
707bba77
AM
1275 olddef = (h->root.type != bfd_link_hash_undefined
1276 && h->root.type != bfd_link_hash_undefweak
202ac193 1277 && h->root.type != bfd_link_hash_common);
45d6a902 1278
0a36a439
L
1279 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1280 respectively, appear to be a function. */
1281
1282 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1283 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1284
1285 oldfunc = (h->type != STT_NOTYPE
1286 && bed->is_function_type (h->type));
1287
c5d37467 1288 if (!(newfunc && oldfunc)
5b677558
AM
1289 && ELF_ST_TYPE (sym->st_info) != h->type
1290 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1291 && h->type != STT_NOTYPE
c5d37467
AM
1292 && (newdef || bfd_is_com_section (sec))
1293 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1294 {
c5d37467
AM
1295 /* If creating a default indirect symbol ("foo" or "foo@") from
1296 a dynamic versioned definition ("foo@@") skip doing so if
1297 there is an existing regular definition with a different
1298 type. We don't want, for example, a "time" variable in the
1299 executable overriding a "time" function in a shared library. */
1300 if (newdyn
1301 && !olddyn)
1302 {
1303 *skip = TRUE;
1304 return TRUE;
1305 }
1306
1307 /* When adding a symbol from a regular object file after we have
1308 created indirect symbols, undo the indirection and any
1309 dynamic state. */
1310 if (hi != h
1311 && !newdyn
1312 && olddyn)
1313 {
1314 h = hi;
1315 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1316 h->forced_local = 0;
1317 h->ref_dynamic = 0;
1318 h->def_dynamic = 0;
1319 h->dynamic_def = 0;
1320 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1321 {
1322 h->root.type = bfd_link_hash_undefined;
1323 h->root.u.undef.abfd = abfd;
1324 }
1325 else
1326 {
1327 h->root.type = bfd_link_hash_new;
1328 h->root.u.undef.abfd = NULL;
1329 }
1330 return TRUE;
1331 }
580a2b6e
L
1332 }
1333
4c34aff8
AM
1334 /* Check TLS symbols. We don't check undefined symbols introduced
1335 by "ld -u" which have no type (and oldbfd NULL), and we don't
1336 check symbols from plugins because they also have no type. */
1337 if (oldbfd != NULL
1338 && (oldbfd->flags & BFD_PLUGIN) == 0
1339 && (abfd->flags & BFD_PLUGIN) == 0
1340 && ELF_ST_TYPE (sym->st_info) != h->type
1341 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1342 {
1343 bfd *ntbfd, *tbfd;
1344 bfd_boolean ntdef, tdef;
1345 asection *ntsec, *tsec;
1346
1347 if (h->type == STT_TLS)
1348 {
3b36f7e6 1349 ntbfd = abfd;
7479dfd4
L
1350 ntsec = sec;
1351 ntdef = newdef;
1352 tbfd = oldbfd;
1353 tsec = oldsec;
1354 tdef = olddef;
1355 }
1356 else
1357 {
1358 ntbfd = oldbfd;
1359 ntsec = oldsec;
1360 ntdef = olddef;
1361 tbfd = abfd;
1362 tsec = sec;
1363 tdef = newdef;
1364 }
1365
1366 if (tdef && ntdef)
4eca0228 1367 _bfd_error_handler
695344c0 1368 /* xgettext:c-format */
871b3ab2
AM
1369 (_("%s: TLS definition in %pB section %pA "
1370 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1371 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1372 else if (!tdef && !ntdef)
4eca0228 1373 _bfd_error_handler
695344c0 1374 /* xgettext:c-format */
871b3ab2
AM
1375 (_("%s: TLS reference in %pB "
1376 "mismatches non-TLS reference in %pB"),
c08bb8dd 1377 h->root.root.string, tbfd, ntbfd);
7479dfd4 1378 else if (tdef)
4eca0228 1379 _bfd_error_handler
695344c0 1380 /* xgettext:c-format */
871b3ab2
AM
1381 (_("%s: TLS definition in %pB section %pA "
1382 "mismatches non-TLS reference in %pB"),
c08bb8dd 1383 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1384 else
4eca0228 1385 _bfd_error_handler
695344c0 1386 /* xgettext:c-format */
871b3ab2
AM
1387 (_("%s: TLS reference in %pB "
1388 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1389 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1390
1391 bfd_set_error (bfd_error_bad_value);
1392 return FALSE;
1393 }
1394
45d6a902
AM
1395 /* If the old symbol has non-default visibility, we ignore the new
1396 definition from a dynamic object. */
1397 if (newdyn
9c7a29a3 1398 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1399 && !bfd_is_und_section (sec))
1400 {
1401 *skip = TRUE;
1402 /* Make sure this symbol is dynamic. */
f5385ebf 1403 h->ref_dynamic = 1;
90c984fc 1404 hi->ref_dynamic = 1;
45d6a902
AM
1405 /* A protected symbol has external availability. Make sure it is
1406 recorded as dynamic.
1407
1408 FIXME: Should we check type and size for protected symbol? */
1409 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1410 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1411 else
1412 return TRUE;
1413 }
1414 else if (!newdyn
9c7a29a3 1415 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1416 && h->def_dynamic)
45d6a902
AM
1417 {
1418 /* If the new symbol with non-default visibility comes from a
1419 relocatable file and the old definition comes from a dynamic
1420 object, we remove the old definition. */
6c9b78e6 1421 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1422 {
1423 /* Handle the case where the old dynamic definition is
1424 default versioned. We need to copy the symbol info from
1425 the symbol with default version to the normal one if it
1426 was referenced before. */
1427 if (h->ref_regular)
1428 {
6c9b78e6 1429 hi->root.type = h->root.type;
d2dee3b2 1430 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1431 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1432
6c9b78e6 1433 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1434 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1435 {
aed81c4e
MR
1436 /* If the new symbol is hidden or internal, completely undo
1437 any dynamic link state. */
1438 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1439 h->forced_local = 0;
1440 h->ref_dynamic = 0;
d2dee3b2
L
1441 }
1442 else
aed81c4e
MR
1443 h->ref_dynamic = 1;
1444
1445 h->def_dynamic = 0;
aed81c4e
MR
1446 /* FIXME: Should we check type and size for protected symbol? */
1447 h->size = 0;
1448 h->type = 0;
1449
6c9b78e6 1450 h = hi;
d2dee3b2
L
1451 }
1452 else
6c9b78e6 1453 h = hi;
d2dee3b2 1454 }
1de1a317 1455
f5eda473
AM
1456 /* If the old symbol was undefined before, then it will still be
1457 on the undefs list. If the new symbol is undefined or
1458 common, we can't make it bfd_link_hash_new here, because new
1459 undefined or common symbols will be added to the undefs list
1460 by _bfd_generic_link_add_one_symbol. Symbols may not be
1461 added twice to the undefs list. Also, if the new symbol is
1462 undefweak then we don't want to lose the strong undef. */
1463 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1464 {
1de1a317 1465 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1466 h->root.u.undef.abfd = abfd;
1467 }
1468 else
1469 {
1470 h->root.type = bfd_link_hash_new;
1471 h->root.u.undef.abfd = NULL;
1472 }
1473
f5eda473 1474 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1475 {
f5eda473
AM
1476 /* If the new symbol is hidden or internal, completely undo
1477 any dynamic link state. */
1478 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1479 h->forced_local = 0;
1480 h->ref_dynamic = 0;
45d6a902 1481 }
f5eda473
AM
1482 else
1483 h->ref_dynamic = 1;
1484 h->def_dynamic = 0;
45d6a902
AM
1485 /* FIXME: Should we check type and size for protected symbol? */
1486 h->size = 0;
1487 h->type = 0;
1488 return TRUE;
1489 }
14a793b2 1490
15b43f48
AM
1491 /* If a new weak symbol definition comes from a regular file and the
1492 old symbol comes from a dynamic library, we treat the new one as
1493 strong. Similarly, an old weak symbol definition from a regular
1494 file is treated as strong when the new symbol comes from a dynamic
1495 library. Further, an old weak symbol from a dynamic library is
1496 treated as strong if the new symbol is from a dynamic library.
1497 This reflects the way glibc's ld.so works.
1498
165f707a
AM
1499 Also allow a weak symbol to override a linker script symbol
1500 defined by an early pass over the script. This is done so the
1501 linker knows the symbol is defined in an object file, for the
1502 DEFINED script function.
1503
15b43f48
AM
1504 Do this before setting *type_change_ok or *size_change_ok so that
1505 we warn properly when dynamic library symbols are overridden. */
1506
165f707a 1507 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1508 newweak = FALSE;
15b43f48 1509 if (olddef && newdyn)
0f8a2703
AM
1510 oldweak = FALSE;
1511
d334575b 1512 /* Allow changes between different types of function symbol. */
0a36a439 1513 if (newfunc && oldfunc)
fcb93ecf
PB
1514 *type_change_ok = TRUE;
1515
79349b09
AM
1516 /* It's OK to change the type if either the existing symbol or the
1517 new symbol is weak. A type change is also OK if the old symbol
1518 is undefined and the new symbol is defined. */
252b5132 1519
79349b09
AM
1520 if (oldweak
1521 || newweak
1522 || (newdef
1523 && h->root.type == bfd_link_hash_undefined))
1524 *type_change_ok = TRUE;
1525
1526 /* It's OK to change the size if either the existing symbol or the
1527 new symbol is weak, or if the old symbol is undefined. */
1528
1529 if (*type_change_ok
1530 || h->root.type == bfd_link_hash_undefined)
1531 *size_change_ok = TRUE;
45d6a902 1532
45d6a902
AM
1533 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1534 symbol, respectively, appears to be a common symbol in a dynamic
1535 object. If a symbol appears in an uninitialized section, and is
1536 not weak, and is not a function, then it may be a common symbol
1537 which was resolved when the dynamic object was created. We want
1538 to treat such symbols specially, because they raise special
1539 considerations when setting the symbol size: if the symbol
1540 appears as a common symbol in a regular object, and the size in
1541 the regular object is larger, we must make sure that we use the
1542 larger size. This problematic case can always be avoided in C,
1543 but it must be handled correctly when using Fortran shared
1544 libraries.
1545
1546 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1547 likewise for OLDDYNCOMMON and OLDDEF.
1548
1549 Note that this test is just a heuristic, and that it is quite
1550 possible to have an uninitialized symbol in a shared object which
1551 is really a definition, rather than a common symbol. This could
1552 lead to some minor confusion when the symbol really is a common
1553 symbol in some regular object. However, I think it will be
1554 harmless. */
1555
1556 if (newdyn
1557 && newdef
79349b09 1558 && !newweak
45d6a902
AM
1559 && (sec->flags & SEC_ALLOC) != 0
1560 && (sec->flags & SEC_LOAD) == 0
1561 && sym->st_size > 0
0a36a439 1562 && !newfunc)
45d6a902
AM
1563 newdyncommon = TRUE;
1564 else
1565 newdyncommon = FALSE;
1566
1567 if (olddyn
1568 && olddef
1569 && h->root.type == bfd_link_hash_defined
f5385ebf 1570 && h->def_dynamic
45d6a902
AM
1571 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1572 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1573 && h->size > 0
0a36a439 1574 && !oldfunc)
45d6a902
AM
1575 olddyncommon = TRUE;
1576 else
1577 olddyncommon = FALSE;
1578
a4d8e49b
L
1579 /* We now know everything about the old and new symbols. We ask the
1580 backend to check if we can merge them. */
5d13b3b3
AM
1581 if (bed->merge_symbol != NULL)
1582 {
1583 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1584 return FALSE;
1585 sec = *psec;
1586 }
a4d8e49b 1587
a83ef4d1
L
1588 /* There are multiple definitions of a normal symbol. Skip the
1589 default symbol as well as definition from an IR object. */
93f4de39 1590 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1591 && !default_sym && h->def_regular
1592 && !(oldbfd != NULL
1593 && (oldbfd->flags & BFD_PLUGIN) != 0
1594 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1595 {
1596 /* Handle a multiple definition. */
1597 (*info->callbacks->multiple_definition) (info, &h->root,
1598 abfd, sec, *pvalue);
1599 *skip = TRUE;
1600 return TRUE;
1601 }
1602
45d6a902
AM
1603 /* If both the old and the new symbols look like common symbols in a
1604 dynamic object, set the size of the symbol to the larger of the
1605 two. */
1606
1607 if (olddyncommon
1608 && newdyncommon
1609 && sym->st_size != h->size)
1610 {
1611 /* Since we think we have two common symbols, issue a multiple
1612 common warning if desired. Note that we only warn if the
1613 size is different. If the size is the same, we simply let
1614 the old symbol override the new one as normally happens with
1615 symbols defined in dynamic objects. */
1616
1a72702b
AM
1617 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1618 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1619 if (sym->st_size > h->size)
1620 h->size = sym->st_size;
252b5132 1621
45d6a902 1622 *size_change_ok = TRUE;
252b5132
RH
1623 }
1624
45d6a902
AM
1625 /* If we are looking at a dynamic object, and we have found a
1626 definition, we need to see if the symbol was already defined by
1627 some other object. If so, we want to use the existing
1628 definition, and we do not want to report a multiple symbol
1629 definition error; we do this by clobbering *PSEC to be
1630 bfd_und_section_ptr.
1631
1632 We treat a common symbol as a definition if the symbol in the
1633 shared library is a function, since common symbols always
1634 represent variables; this can cause confusion in principle, but
1635 any such confusion would seem to indicate an erroneous program or
1636 shared library. We also permit a common symbol in a regular
8170f769 1637 object to override a weak symbol in a shared object. */
45d6a902
AM
1638
1639 if (newdyn
1640 && newdef
77cfaee6 1641 && (olddef
45d6a902 1642 || (h->root.type == bfd_link_hash_common
8170f769 1643 && (newweak || newfunc))))
45d6a902
AM
1644 {
1645 *override = TRUE;
1646 newdef = FALSE;
1647 newdyncommon = FALSE;
252b5132 1648
45d6a902
AM
1649 *psec = sec = bfd_und_section_ptr;
1650 *size_change_ok = TRUE;
252b5132 1651
45d6a902
AM
1652 /* If we get here when the old symbol is a common symbol, then
1653 we are explicitly letting it override a weak symbol or
1654 function in a dynamic object, and we don't want to warn about
1655 a type change. If the old symbol is a defined symbol, a type
1656 change warning may still be appropriate. */
252b5132 1657
45d6a902
AM
1658 if (h->root.type == bfd_link_hash_common)
1659 *type_change_ok = TRUE;
1660 }
1661
1662 /* Handle the special case of an old common symbol merging with a
1663 new symbol which looks like a common symbol in a shared object.
1664 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1665 common symbol, and let _bfd_generic_link_add_one_symbol do the
1666 right thing. */
45d6a902
AM
1667
1668 if (newdyncommon
1669 && h->root.type == bfd_link_hash_common)
1670 {
1671 *override = TRUE;
1672 newdef = FALSE;
1673 newdyncommon = FALSE;
1674 *pvalue = sym->st_size;
a4d8e49b 1675 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1676 *size_change_ok = TRUE;
1677 }
1678
c5e2cead 1679 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1680 if (newdef && olddef && newweak)
54ac0771 1681 {
35ed3f94 1682 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1683 if (!(oldbfd != NULL
1684 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1685 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1686 {
1687 newdef = FALSE;
1688 *skip = TRUE;
1689 }
54ac0771
L
1690
1691 /* Merge st_other. If the symbol already has a dynamic index,
1692 but visibility says it should not be visible, turn it into a
1693 local symbol. */
b8417128 1694 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1695 if (h->dynindx != -1)
1696 switch (ELF_ST_VISIBILITY (h->other))
1697 {
1698 case STV_INTERNAL:
1699 case STV_HIDDEN:
1700 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1701 break;
1702 }
1703 }
c5e2cead 1704
45d6a902
AM
1705 /* If the old symbol is from a dynamic object, and the new symbol is
1706 a definition which is not from a dynamic object, then the new
1707 symbol overrides the old symbol. Symbols from regular files
1708 always take precedence over symbols from dynamic objects, even if
1709 they are defined after the dynamic object in the link.
1710
1711 As above, we again permit a common symbol in a regular object to
1712 override a definition in a shared object if the shared object
0f8a2703 1713 symbol is a function or is weak. */
45d6a902
AM
1714
1715 flip = NULL;
77cfaee6 1716 if (!newdyn
45d6a902
AM
1717 && (newdef
1718 || (bfd_is_com_section (sec)
0a36a439 1719 && (oldweak || oldfunc)))
45d6a902
AM
1720 && olddyn
1721 && olddef
f5385ebf 1722 && h->def_dynamic)
45d6a902
AM
1723 {
1724 /* Change the hash table entry to undefined, and let
1725 _bfd_generic_link_add_one_symbol do the right thing with the
1726 new definition. */
1727
1728 h->root.type = bfd_link_hash_undefined;
1729 h->root.u.undef.abfd = h->root.u.def.section->owner;
1730 *size_change_ok = TRUE;
1731
1732 olddef = FALSE;
1733 olddyncommon = FALSE;
1734
1735 /* We again permit a type change when a common symbol may be
1736 overriding a function. */
1737
1738 if (bfd_is_com_section (sec))
0a36a439
L
1739 {
1740 if (oldfunc)
1741 {
1742 /* If a common symbol overrides a function, make sure
1743 that it isn't defined dynamically nor has type
1744 function. */
1745 h->def_dynamic = 0;
1746 h->type = STT_NOTYPE;
1747 }
1748 *type_change_ok = TRUE;
1749 }
45d6a902 1750
6c9b78e6
AM
1751 if (hi->root.type == bfd_link_hash_indirect)
1752 flip = hi;
45d6a902
AM
1753 else
1754 /* This union may have been set to be non-NULL when this symbol
1755 was seen in a dynamic object. We must force the union to be
1756 NULL, so that it is correct for a regular symbol. */
1757 h->verinfo.vertree = NULL;
1758 }
1759
1760 /* Handle the special case of a new common symbol merging with an
1761 old symbol that looks like it might be a common symbol defined in
1762 a shared object. Note that we have already handled the case in
1763 which a new common symbol should simply override the definition
1764 in the shared library. */
1765
1766 if (! newdyn
1767 && bfd_is_com_section (sec)
1768 && olddyncommon)
1769 {
1770 /* It would be best if we could set the hash table entry to a
1771 common symbol, but we don't know what to use for the section
1772 or the alignment. */
1a72702b
AM
1773 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1774 bfd_link_hash_common, sym->st_size);
45d6a902 1775
4cc11e76 1776 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1777 larger, pretend that the new symbol has its size. */
1778
1779 if (h->size > *pvalue)
1780 *pvalue = h->size;
1781
af44c138
L
1782 /* We need to remember the alignment required by the symbol
1783 in the dynamic object. */
1784 BFD_ASSERT (pold_alignment);
1785 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1786
1787 olddef = FALSE;
1788 olddyncommon = FALSE;
1789
1790 h->root.type = bfd_link_hash_undefined;
1791 h->root.u.undef.abfd = h->root.u.def.section->owner;
1792
1793 *size_change_ok = TRUE;
1794 *type_change_ok = TRUE;
1795
6c9b78e6
AM
1796 if (hi->root.type == bfd_link_hash_indirect)
1797 flip = hi;
45d6a902
AM
1798 else
1799 h->verinfo.vertree = NULL;
1800 }
1801
1802 if (flip != NULL)
1803 {
1804 /* Handle the case where we had a versioned symbol in a dynamic
1805 library and now find a definition in a normal object. In this
1806 case, we make the versioned symbol point to the normal one. */
45d6a902 1807 flip->root.type = h->root.type;
00cbee0a 1808 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1809 h->root.type = bfd_link_hash_indirect;
1810 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1811 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1812 if (h->def_dynamic)
45d6a902 1813 {
f5385ebf
AM
1814 h->def_dynamic = 0;
1815 flip->ref_dynamic = 1;
45d6a902
AM
1816 }
1817 }
1818
45d6a902
AM
1819 return TRUE;
1820}
1821
1822/* This function is called to create an indirect symbol from the
1823 default for the symbol with the default version if needed. The
4f3fedcf 1824 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1825 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1826
28caa186 1827static bfd_boolean
268b6b39
AM
1828_bfd_elf_add_default_symbol (bfd *abfd,
1829 struct bfd_link_info *info,
1830 struct elf_link_hash_entry *h,
1831 const char *name,
1832 Elf_Internal_Sym *sym,
4f3fedcf
AM
1833 asection *sec,
1834 bfd_vma value,
1835 bfd **poldbfd,
e3c9d234 1836 bfd_boolean *dynsym)
45d6a902
AM
1837{
1838 bfd_boolean type_change_ok;
1839 bfd_boolean size_change_ok;
1840 bfd_boolean skip;
1841 char *shortname;
1842 struct elf_link_hash_entry *hi;
1843 struct bfd_link_hash_entry *bh;
9c5bfbb7 1844 const struct elf_backend_data *bed;
45d6a902
AM
1845 bfd_boolean collect;
1846 bfd_boolean dynamic;
e3c9d234 1847 bfd_boolean override;
45d6a902
AM
1848 char *p;
1849 size_t len, shortlen;
ffd65175 1850 asection *tmp_sec;
6e33951e 1851 bfd_boolean matched;
45d6a902 1852
422f1182
L
1853 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1854 return TRUE;
1855
45d6a902
AM
1856 /* If this symbol has a version, and it is the default version, we
1857 create an indirect symbol from the default name to the fully
1858 decorated name. This will cause external references which do not
1859 specify a version to be bound to this version of the symbol. */
1860 p = strchr (name, ELF_VER_CHR);
422f1182
L
1861 if (h->versioned == unknown)
1862 {
1863 if (p == NULL)
1864 {
1865 h->versioned = unversioned;
1866 return TRUE;
1867 }
1868 else
1869 {
1870 if (p[1] != ELF_VER_CHR)
1871 {
1872 h->versioned = versioned_hidden;
1873 return TRUE;
1874 }
1875 else
1876 h->versioned = versioned;
1877 }
1878 }
4373f8af
L
1879 else
1880 {
1881 /* PR ld/19073: We may see an unversioned definition after the
1882 default version. */
1883 if (p == NULL)
1884 return TRUE;
1885 }
45d6a902 1886
45d6a902
AM
1887 bed = get_elf_backend_data (abfd);
1888 collect = bed->collect;
1889 dynamic = (abfd->flags & DYNAMIC) != 0;
1890
1891 shortlen = p - name;
a50b1753 1892 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1893 if (shortname == NULL)
1894 return FALSE;
1895 memcpy (shortname, name, shortlen);
1896 shortname[shortlen] = '\0';
1897
1898 /* We are going to create a new symbol. Merge it with any existing
1899 symbol with this name. For the purposes of the merge, act as
1900 though we were defining the symbol we just defined, although we
1901 actually going to define an indirect symbol. */
1902 type_change_ok = FALSE;
1903 size_change_ok = FALSE;
6e33951e 1904 matched = TRUE;
ffd65175
AM
1905 tmp_sec = sec;
1906 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1907 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1908 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1909 return FALSE;
1910
1911 if (skip)
1912 goto nondefault;
1913
5b677558
AM
1914 if (hi->def_regular)
1915 {
1916 /* If the undecorated symbol will have a version added by a
1917 script different to H, then don't indirect to/from the
1918 undecorated symbol. This isn't ideal because we may not yet
1919 have seen symbol versions, if given by a script on the
1920 command line rather than via --version-script. */
1921 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1922 {
1923 bfd_boolean hide;
1924
1925 hi->verinfo.vertree
1926 = bfd_find_version_for_sym (info->version_info,
1927 hi->root.root.string, &hide);
1928 if (hi->verinfo.vertree != NULL && hide)
1929 {
1930 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1931 goto nondefault;
1932 }
1933 }
1934 if (hi->verinfo.vertree != NULL
1935 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1936 goto nondefault;
1937 }
1938
45d6a902
AM
1939 if (! override)
1940 {
c6e8a9a8 1941 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1942 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1943 {
1944 bh = &hi->root;
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, shortname, BSF_INDIRECT,
1947 bfd_ind_section_ptr,
1948 0, name, FALSE, collect, &bh)))
1949 return FALSE;
1950 hi = (struct elf_link_hash_entry *) bh;
1951 }
45d6a902
AM
1952 }
1953 else
1954 {
1955 /* In this case the symbol named SHORTNAME is overriding the
1956 indirect symbol we want to add. We were planning on making
1957 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1958 is the name without a version. NAME is the fully versioned
1959 name, and it is the default version.
1960
1961 Overriding means that we already saw a definition for the
1962 symbol SHORTNAME in a regular object, and it is overriding
1963 the symbol defined in the dynamic object.
1964
1965 When this happens, we actually want to change NAME, the
1966 symbol we just added, to refer to SHORTNAME. This will cause
1967 references to NAME in the shared object to become references
1968 to SHORTNAME in the regular object. This is what we expect
1969 when we override a function in a shared object: that the
1970 references in the shared object will be mapped to the
1971 definition in the regular object. */
1972
1973 while (hi->root.type == bfd_link_hash_indirect
1974 || hi->root.type == bfd_link_hash_warning)
1975 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1976
1977 h->root.type = bfd_link_hash_indirect;
1978 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1979 if (h->def_dynamic)
45d6a902 1980 {
f5385ebf
AM
1981 h->def_dynamic = 0;
1982 hi->ref_dynamic = 1;
1983 if (hi->ref_regular
1984 || hi->def_regular)
45d6a902 1985 {
c152c796 1986 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1987 return FALSE;
1988 }
1989 }
1990
1991 /* Now set HI to H, so that the following code will set the
1992 other fields correctly. */
1993 hi = h;
1994 }
1995
fab4a87f
L
1996 /* Check if HI is a warning symbol. */
1997 if (hi->root.type == bfd_link_hash_warning)
1998 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1999
45d6a902
AM
2000 /* If there is a duplicate definition somewhere, then HI may not
2001 point to an indirect symbol. We will have reported an error to
2002 the user in that case. */
2003
2004 if (hi->root.type == bfd_link_hash_indirect)
2005 {
2006 struct elf_link_hash_entry *ht;
2007
45d6a902 2008 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2009 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2010
68c88cd4
AM
2011 /* A reference to the SHORTNAME symbol from a dynamic library
2012 will be satisfied by the versioned symbol at runtime. In
2013 effect, we have a reference to the versioned symbol. */
2014 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2015 hi->dynamic_def |= ht->dynamic_def;
2016
45d6a902
AM
2017 /* See if the new flags lead us to realize that the symbol must
2018 be dynamic. */
2019 if (! *dynsym)
2020 {
2021 if (! dynamic)
2022 {
0e1862bb 2023 if (! bfd_link_executable (info)
90c984fc 2024 || hi->def_dynamic
f5385ebf 2025 || hi->ref_dynamic)
45d6a902
AM
2026 *dynsym = TRUE;
2027 }
2028 else
2029 {
f5385ebf 2030 if (hi->ref_regular)
45d6a902
AM
2031 *dynsym = TRUE;
2032 }
2033 }
2034 }
2035
2036 /* We also need to define an indirection from the nondefault version
2037 of the symbol. */
2038
2039nondefault:
2040 len = strlen (name);
a50b1753 2041 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2042 if (shortname == NULL)
2043 return FALSE;
2044 memcpy (shortname, name, shortlen);
2045 memcpy (shortname + shortlen, p + 1, len - shortlen);
2046
2047 /* Once again, merge with any existing symbol. */
2048 type_change_ok = FALSE;
2049 size_change_ok = FALSE;
ffd65175
AM
2050 tmp_sec = sec;
2051 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2052 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2053 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2054 return FALSE;
2055
2056 if (skip)
2057 return TRUE;
2058
2059 if (override)
2060 {
2061 /* Here SHORTNAME is a versioned name, so we don't expect to see
2062 the type of override we do in the case above unless it is
4cc11e76 2063 overridden by a versioned definition. */
45d6a902
AM
2064 if (hi->root.type != bfd_link_hash_defined
2065 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2066 _bfd_error_handler
695344c0 2067 /* xgettext:c-format */
871b3ab2 2068 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2069 abfd, shortname);
45d6a902
AM
2070 }
2071 else
2072 {
2073 bh = &hi->root;
2074 if (! (_bfd_generic_link_add_one_symbol
2075 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2076 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2077 return FALSE;
2078 hi = (struct elf_link_hash_entry *) bh;
2079
2080 /* If there is a duplicate definition somewhere, then HI may not
2081 point to an indirect symbol. We will have reported an error
2082 to the user in that case. */
2083
2084 if (hi->root.type == bfd_link_hash_indirect)
2085 {
fcfa13d2 2086 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2087 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2088 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2089
2090 /* See if the new flags lead us to realize that the symbol
2091 must be dynamic. */
2092 if (! *dynsym)
2093 {
2094 if (! dynamic)
2095 {
0e1862bb 2096 if (! bfd_link_executable (info)
f5385ebf 2097 || hi->ref_dynamic)
45d6a902
AM
2098 *dynsym = TRUE;
2099 }
2100 else
2101 {
f5385ebf 2102 if (hi->ref_regular)
45d6a902
AM
2103 *dynsym = TRUE;
2104 }
2105 }
2106 }
2107 }
2108
2109 return TRUE;
2110}
2111\f
2112/* This routine is used to export all defined symbols into the dynamic
2113 symbol table. It is called via elf_link_hash_traverse. */
2114
28caa186 2115static bfd_boolean
268b6b39 2116_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2117{
a50b1753 2118 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2119
2120 /* Ignore indirect symbols. These are added by the versioning code. */
2121 if (h->root.type == bfd_link_hash_indirect)
2122 return TRUE;
2123
7686d77d
AM
2124 /* Ignore this if we won't export it. */
2125 if (!eif->info->export_dynamic && !h->dynamic)
2126 return TRUE;
45d6a902
AM
2127
2128 if (h->dynindx == -1
fd91d419
L
2129 && (h->def_regular || h->ref_regular)
2130 && ! bfd_hide_sym_by_version (eif->info->version_info,
2131 h->root.root.string))
45d6a902 2132 {
fd91d419 2133 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2134 {
fd91d419
L
2135 eif->failed = TRUE;
2136 return FALSE;
45d6a902
AM
2137 }
2138 }
2139
2140 return TRUE;
2141}
2142\f
2143/* Look through the symbols which are defined in other shared
2144 libraries and referenced here. Update the list of version
2145 dependencies. This will be put into the .gnu.version_r section.
2146 This function is called via elf_link_hash_traverse. */
2147
28caa186 2148static bfd_boolean
268b6b39
AM
2149_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2150 void *data)
45d6a902 2151{
a50b1753 2152 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2153 Elf_Internal_Verneed *t;
2154 Elf_Internal_Vernaux *a;
2155 bfd_size_type amt;
2156
45d6a902
AM
2157 /* We only care about symbols defined in shared objects with version
2158 information. */
f5385ebf
AM
2159 if (!h->def_dynamic
2160 || h->def_regular
45d6a902 2161 || h->dynindx == -1
7b20f099
AM
2162 || h->verinfo.verdef == NULL
2163 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2164 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2165 return TRUE;
2166
2167 /* See if we already know about this version. */
28caa186
AM
2168 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2169 t != NULL;
2170 t = t->vn_nextref)
45d6a902
AM
2171 {
2172 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2173 continue;
2174
2175 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2176 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2177 return TRUE;
2178
2179 break;
2180 }
2181
2182 /* This is a new version. Add it to tree we are building. */
2183
2184 if (t == NULL)
2185 {
2186 amt = sizeof *t;
a50b1753 2187 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2188 if (t == NULL)
2189 {
2190 rinfo->failed = TRUE;
2191 return FALSE;
2192 }
2193
2194 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2195 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2196 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2197 }
2198
2199 amt = sizeof *a;
a50b1753 2200 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2201 if (a == NULL)
2202 {
2203 rinfo->failed = TRUE;
2204 return FALSE;
2205 }
45d6a902
AM
2206
2207 /* Note that we are copying a string pointer here, and testing it
2208 above. If bfd_elf_string_from_elf_section is ever changed to
2209 discard the string data when low in memory, this will have to be
2210 fixed. */
2211 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2212
2213 a->vna_flags = h->verinfo.verdef->vd_flags;
2214 a->vna_nextptr = t->vn_auxptr;
2215
2216 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2217 ++rinfo->vers;
2218
2219 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2220
2221 t->vn_auxptr = a;
2222
2223 return TRUE;
2224}
2225
099bb8fb
L
2226/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2227 hidden. Set *T_P to NULL if there is no match. */
2228
2229static bfd_boolean
2230_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2231 struct elf_link_hash_entry *h,
2232 const char *version_p,
2233 struct bfd_elf_version_tree **t_p,
2234 bfd_boolean *hide)
2235{
2236 struct bfd_elf_version_tree *t;
2237
2238 /* Look for the version. If we find it, it is no longer weak. */
2239 for (t = info->version_info; t != NULL; t = t->next)
2240 {
2241 if (strcmp (t->name, version_p) == 0)
2242 {
2243 size_t len;
2244 char *alc;
2245 struct bfd_elf_version_expr *d;
2246
2247 len = version_p - h->root.root.string;
2248 alc = (char *) bfd_malloc (len);
2249 if (alc == NULL)
2250 return FALSE;
2251 memcpy (alc, h->root.root.string, len - 1);
2252 alc[len - 1] = '\0';
2253 if (alc[len - 2] == ELF_VER_CHR)
2254 alc[len - 2] = '\0';
2255
2256 h->verinfo.vertree = t;
2257 t->used = TRUE;
2258 d = NULL;
2259
2260 if (t->globals.list != NULL)
2261 d = (*t->match) (&t->globals, NULL, alc);
2262
2263 /* See if there is anything to force this symbol to
2264 local scope. */
2265 if (d == NULL && t->locals.list != NULL)
2266 {
2267 d = (*t->match) (&t->locals, NULL, alc);
2268 if (d != NULL
2269 && h->dynindx != -1
2270 && ! info->export_dynamic)
2271 *hide = TRUE;
2272 }
2273
2274 free (alc);
2275 break;
2276 }
2277 }
2278
2279 *t_p = t;
2280
2281 return TRUE;
2282}
2283
2284/* Return TRUE if the symbol H is hidden by version script. */
2285
2286bfd_boolean
2287_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2288 struct elf_link_hash_entry *h)
2289{
2290 const char *p;
2291 bfd_boolean hide = FALSE;
2292 const struct elf_backend_data *bed
2293 = get_elf_backend_data (info->output_bfd);
2294
2295 /* Version script only hides symbols defined in regular objects. */
2296 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2297 return TRUE;
2298
2299 p = strchr (h->root.root.string, ELF_VER_CHR);
2300 if (p != NULL && h->verinfo.vertree == NULL)
2301 {
2302 struct bfd_elf_version_tree *t;
2303
2304 ++p;
2305 if (*p == ELF_VER_CHR)
2306 ++p;
2307
2308 if (*p != '\0'
2309 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2310 && hide)
2311 {
2312 if (hide)
2313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2314 return TRUE;
2315 }
2316 }
2317
2318 /* If we don't have a version for this symbol, see if we can find
2319 something. */
2320 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2321 {
2322 h->verinfo.vertree
2323 = bfd_find_version_for_sym (info->version_info,
2324 h->root.root.string, &hide);
2325 if (h->verinfo.vertree != NULL && hide)
2326 {
2327 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2328 return TRUE;
2329 }
2330 }
2331
2332 return FALSE;
2333}
2334
45d6a902
AM
2335/* Figure out appropriate versions for all the symbols. We may not
2336 have the version number script until we have read all of the input
2337 files, so until that point we don't know which symbols should be
2338 local. This function is called via elf_link_hash_traverse. */
2339
28caa186 2340static bfd_boolean
268b6b39 2341_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2342{
28caa186 2343 struct elf_info_failed *sinfo;
45d6a902 2344 struct bfd_link_info *info;
9c5bfbb7 2345 const struct elf_backend_data *bed;
45d6a902
AM
2346 struct elf_info_failed eif;
2347 char *p;
099bb8fb 2348 bfd_boolean hide;
45d6a902 2349
a50b1753 2350 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2351 info = sinfo->info;
2352
45d6a902
AM
2353 /* Fix the symbol flags. */
2354 eif.failed = FALSE;
2355 eif.info = info;
2356 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2357 {
2358 if (eif.failed)
2359 sinfo->failed = TRUE;
2360 return FALSE;
2361 }
2362
2363 /* We only need version numbers for symbols defined in regular
2364 objects. */
f5385ebf 2365 if (!h->def_regular)
45d6a902
AM
2366 return TRUE;
2367
099bb8fb 2368 hide = FALSE;
28caa186 2369 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2370 p = strchr (h->root.root.string, ELF_VER_CHR);
2371 if (p != NULL && h->verinfo.vertree == NULL)
2372 {
2373 struct bfd_elf_version_tree *t;
45d6a902 2374
45d6a902
AM
2375 ++p;
2376 if (*p == ELF_VER_CHR)
6e33951e 2377 ++p;
45d6a902
AM
2378
2379 /* If there is no version string, we can just return out. */
2380 if (*p == '\0')
6e33951e 2381 return TRUE;
45d6a902 2382
099bb8fb 2383 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2384 {
099bb8fb
L
2385 sinfo->failed = TRUE;
2386 return FALSE;
45d6a902
AM
2387 }
2388
099bb8fb
L
2389 if (hide)
2390 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2391
45d6a902
AM
2392 /* If we are building an application, we need to create a
2393 version node for this version. */
0e1862bb 2394 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2395 {
2396 struct bfd_elf_version_tree **pp;
2397 int version_index;
2398
2399 /* If we aren't going to export this symbol, we don't need
2400 to worry about it. */
2401 if (h->dynindx == -1)
2402 return TRUE;
2403
ef53be89
AM
2404 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2405 sizeof *t);
45d6a902
AM
2406 if (t == NULL)
2407 {
2408 sinfo->failed = TRUE;
2409 return FALSE;
2410 }
2411
45d6a902 2412 t->name = p;
45d6a902
AM
2413 t->name_indx = (unsigned int) -1;
2414 t->used = TRUE;
2415
2416 version_index = 1;
2417 /* Don't count anonymous version tag. */
fd91d419
L
2418 if (sinfo->info->version_info != NULL
2419 && sinfo->info->version_info->vernum == 0)
45d6a902 2420 version_index = 0;
fd91d419
L
2421 for (pp = &sinfo->info->version_info;
2422 *pp != NULL;
2423 pp = &(*pp)->next)
45d6a902
AM
2424 ++version_index;
2425 t->vernum = version_index;
2426
2427 *pp = t;
2428
2429 h->verinfo.vertree = t;
2430 }
2431 else if (t == NULL)
2432 {
2433 /* We could not find the version for a symbol when
2434 generating a shared archive. Return an error. */
4eca0228 2435 _bfd_error_handler
695344c0 2436 /* xgettext:c-format */
871b3ab2 2437 (_("%pB: version node not found for symbol %s"),
28caa186 2438 info->output_bfd, h->root.root.string);
45d6a902
AM
2439 bfd_set_error (bfd_error_bad_value);
2440 sinfo->failed = TRUE;
2441 return FALSE;
2442 }
45d6a902
AM
2443 }
2444
2445 /* If we don't have a version for this symbol, see if we can find
2446 something. */
099bb8fb
L
2447 if (!hide
2448 && h->verinfo.vertree == NULL
2449 && sinfo->info->version_info != NULL)
45d6a902 2450 {
fd91d419
L
2451 h->verinfo.vertree
2452 = bfd_find_version_for_sym (sinfo->info->version_info,
2453 h->root.root.string, &hide);
1e8fa21e
AM
2454 if (h->verinfo.vertree != NULL && hide)
2455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2456 }
2457
2458 return TRUE;
2459}
2460\f
45d6a902
AM
2461/* Read and swap the relocs from the section indicated by SHDR. This
2462 may be either a REL or a RELA section. The relocations are
2463 translated into RELA relocations and stored in INTERNAL_RELOCS,
2464 which should have already been allocated to contain enough space.
2465 The EXTERNAL_RELOCS are a buffer where the external form of the
2466 relocations should be stored.
2467
2468 Returns FALSE if something goes wrong. */
2469
2470static bfd_boolean
268b6b39 2471elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2472 asection *sec,
268b6b39
AM
2473 Elf_Internal_Shdr *shdr,
2474 void *external_relocs,
2475 Elf_Internal_Rela *internal_relocs)
45d6a902 2476{
9c5bfbb7 2477 const struct elf_backend_data *bed;
268b6b39 2478 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2479 const bfd_byte *erela;
2480 const bfd_byte *erelaend;
2481 Elf_Internal_Rela *irela;
243ef1e0
L
2482 Elf_Internal_Shdr *symtab_hdr;
2483 size_t nsyms;
45d6a902 2484
45d6a902
AM
2485 /* Position ourselves at the start of the section. */
2486 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2487 return FALSE;
2488
2489 /* Read the relocations. */
2490 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2491 return FALSE;
2492
243ef1e0 2493 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2494 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2495
45d6a902
AM
2496 bed = get_elf_backend_data (abfd);
2497
2498 /* Convert the external relocations to the internal format. */
2499 if (shdr->sh_entsize == bed->s->sizeof_rel)
2500 swap_in = bed->s->swap_reloc_in;
2501 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2502 swap_in = bed->s->swap_reloca_in;
2503 else
2504 {
2505 bfd_set_error (bfd_error_wrong_format);
2506 return FALSE;
2507 }
2508
a50b1753 2509 erela = (const bfd_byte *) external_relocs;
51992aec 2510 erelaend = erela + shdr->sh_size;
45d6a902
AM
2511 irela = internal_relocs;
2512 while (erela < erelaend)
2513 {
243ef1e0
L
2514 bfd_vma r_symndx;
2515
45d6a902 2516 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2517 r_symndx = ELF32_R_SYM (irela->r_info);
2518 if (bed->s->arch_size == 64)
2519 r_symndx >>= 24;
ce98a316
NC
2520 if (nsyms > 0)
2521 {
2522 if ((size_t) r_symndx >= nsyms)
2523 {
4eca0228 2524 _bfd_error_handler
695344c0 2525 /* xgettext:c-format */
2dcf00ce
AM
2526 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2527 " for offset %#" PRIx64 " in section `%pA'"),
2528 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2529 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2530 bfd_set_error (bfd_error_bad_value);
2531 return FALSE;
2532 }
2533 }
cf35638d 2534 else if (r_symndx != STN_UNDEF)
243ef1e0 2535 {
4eca0228 2536 _bfd_error_handler
695344c0 2537 /* xgettext:c-format */
2dcf00ce
AM
2538 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2539 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2540 " when the object file has no symbol table"),
2dcf00ce
AM
2541 abfd, (uint64_t) r_symndx,
2542 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
45d6a902
AM
2546 irela += bed->s->int_rels_per_ext_rel;
2547 erela += shdr->sh_entsize;
2548 }
2549
2550 return TRUE;
2551}
2552
2553/* Read and swap the relocs for a section O. They may have been
2554 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2555 not NULL, they are used as buffers to read into. They are known to
2556 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2557 the return value is allocated using either malloc or bfd_alloc,
2558 according to the KEEP_MEMORY argument. If O has two relocation
2559 sections (both REL and RELA relocations), then the REL_HDR
2560 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2561 RELA_HDR relocations. */
45d6a902
AM
2562
2563Elf_Internal_Rela *
268b6b39
AM
2564_bfd_elf_link_read_relocs (bfd *abfd,
2565 asection *o,
2566 void *external_relocs,
2567 Elf_Internal_Rela *internal_relocs,
2568 bfd_boolean keep_memory)
45d6a902 2569{
268b6b39 2570 void *alloc1 = NULL;
45d6a902 2571 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2572 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2573 struct bfd_elf_section_data *esdo = elf_section_data (o);
2574 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2575
d4730f92
BS
2576 if (esdo->relocs != NULL)
2577 return esdo->relocs;
45d6a902
AM
2578
2579 if (o->reloc_count == 0)
2580 return NULL;
2581
45d6a902
AM
2582 if (internal_relocs == NULL)
2583 {
2584 bfd_size_type size;
2585
056bafd4 2586 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2587 if (keep_memory)
a50b1753 2588 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2589 else
a50b1753 2590 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2591 if (internal_relocs == NULL)
2592 goto error_return;
2593 }
2594
2595 if (external_relocs == NULL)
2596 {
d4730f92
BS
2597 bfd_size_type size = 0;
2598
2599 if (esdo->rel.hdr)
2600 size += esdo->rel.hdr->sh_size;
2601 if (esdo->rela.hdr)
2602 size += esdo->rela.hdr->sh_size;
45d6a902 2603
268b6b39 2604 alloc1 = bfd_malloc (size);
45d6a902
AM
2605 if (alloc1 == NULL)
2606 goto error_return;
2607 external_relocs = alloc1;
2608 }
2609
d4730f92
BS
2610 internal_rela_relocs = internal_relocs;
2611 if (esdo->rel.hdr)
2612 {
2613 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2614 external_relocs,
2615 internal_relocs))
2616 goto error_return;
2617 external_relocs = (((bfd_byte *) external_relocs)
2618 + esdo->rel.hdr->sh_size);
2619 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2620 * bed->s->int_rels_per_ext_rel);
2621 }
2622
2623 if (esdo->rela.hdr
2624 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2625 external_relocs,
2626 internal_rela_relocs)))
45d6a902
AM
2627 goto error_return;
2628
2629 /* Cache the results for next time, if we can. */
2630 if (keep_memory)
d4730f92 2631 esdo->relocs = internal_relocs;
45d6a902
AM
2632
2633 if (alloc1 != NULL)
2634 free (alloc1);
2635
2636 /* Don't free alloc2, since if it was allocated we are passing it
2637 back (under the name of internal_relocs). */
2638
2639 return internal_relocs;
2640
2641 error_return:
2642 if (alloc1 != NULL)
2643 free (alloc1);
2644 if (alloc2 != NULL)
4dd07732
AM
2645 {
2646 if (keep_memory)
2647 bfd_release (abfd, alloc2);
2648 else
2649 free (alloc2);
2650 }
45d6a902
AM
2651 return NULL;
2652}
2653
2654/* Compute the size of, and allocate space for, REL_HDR which is the
2655 section header for a section containing relocations for O. */
2656
28caa186 2657static bfd_boolean
9eaff861
AO
2658_bfd_elf_link_size_reloc_section (bfd *abfd,
2659 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2660{
9eaff861 2661 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2662
2663 /* That allows us to calculate the size of the section. */
9eaff861 2664 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2665
2666 /* The contents field must last into write_object_contents, so we
2667 allocate it with bfd_alloc rather than malloc. Also since we
2668 cannot be sure that the contents will actually be filled in,
2669 we zero the allocated space. */
a50b1753 2670 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2671 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2672 return FALSE;
2673
d4730f92 2674 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2675 {
2676 struct elf_link_hash_entry **p;
2677
ca4be51c
AM
2678 p = ((struct elf_link_hash_entry **)
2679 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2680 if (p == NULL)
2681 return FALSE;
2682
d4730f92 2683 reldata->hashes = p;
45d6a902
AM
2684 }
2685
2686 return TRUE;
2687}
2688
2689/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2690 originated from the section given by INPUT_REL_HDR) to the
2691 OUTPUT_BFD. */
2692
2693bfd_boolean
268b6b39
AM
2694_bfd_elf_link_output_relocs (bfd *output_bfd,
2695 asection *input_section,
2696 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2697 Elf_Internal_Rela *internal_relocs,
2698 struct elf_link_hash_entry **rel_hash
2699 ATTRIBUTE_UNUSED)
45d6a902
AM
2700{
2701 Elf_Internal_Rela *irela;
2702 Elf_Internal_Rela *irelaend;
2703 bfd_byte *erel;
d4730f92 2704 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2705 asection *output_section;
9c5bfbb7 2706 const struct elf_backend_data *bed;
268b6b39 2707 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2708 struct bfd_elf_section_data *esdo;
45d6a902
AM
2709
2710 output_section = input_section->output_section;
45d6a902 2711
d4730f92
BS
2712 bed = get_elf_backend_data (output_bfd);
2713 esdo = elf_section_data (output_section);
2714 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2715 {
d4730f92
BS
2716 output_reldata = &esdo->rel;
2717 swap_out = bed->s->swap_reloc_out;
45d6a902 2718 }
d4730f92
BS
2719 else if (esdo->rela.hdr
2720 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2721 {
d4730f92
BS
2722 output_reldata = &esdo->rela;
2723 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2724 }
2725 else
2726 {
4eca0228 2727 _bfd_error_handler
695344c0 2728 /* xgettext:c-format */
871b3ab2 2729 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2730 output_bfd, input_section->owner, input_section);
297d8443 2731 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2732 return FALSE;
2733 }
2734
d4730f92
BS
2735 erel = output_reldata->hdr->contents;
2736 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2737 irela = internal_relocs;
2738 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2739 * bed->s->int_rels_per_ext_rel);
2740 while (irela < irelaend)
2741 {
2742 (*swap_out) (output_bfd, irela, erel);
2743 irela += bed->s->int_rels_per_ext_rel;
2744 erel += input_rel_hdr->sh_entsize;
2745 }
2746
2747 /* Bump the counter, so that we know where to add the next set of
2748 relocations. */
d4730f92 2749 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2750
2751 return TRUE;
2752}
2753\f
508c3946
L
2754/* Make weak undefined symbols in PIE dynamic. */
2755
2756bfd_boolean
2757_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2758 struct elf_link_hash_entry *h)
2759{
0e1862bb 2760 if (bfd_link_pie (info)
508c3946
L
2761 && h->dynindx == -1
2762 && h->root.type == bfd_link_hash_undefweak)
2763 return bfd_elf_link_record_dynamic_symbol (info, h);
2764
2765 return TRUE;
2766}
2767
45d6a902
AM
2768/* Fix up the flags for a symbol. This handles various cases which
2769 can only be fixed after all the input files are seen. This is
2770 currently called by both adjust_dynamic_symbol and
2771 assign_sym_version, which is unnecessary but perhaps more robust in
2772 the face of future changes. */
2773
28caa186 2774static bfd_boolean
268b6b39
AM
2775_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2776 struct elf_info_failed *eif)
45d6a902 2777{
33774f08 2778 const struct elf_backend_data *bed;
508c3946 2779
45d6a902
AM
2780 /* If this symbol was mentioned in a non-ELF file, try to set
2781 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2782 permit a non-ELF file to correctly refer to a symbol defined in
2783 an ELF dynamic object. */
f5385ebf 2784 if (h->non_elf)
45d6a902
AM
2785 {
2786 while (h->root.type == bfd_link_hash_indirect)
2787 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2788
2789 if (h->root.type != bfd_link_hash_defined
2790 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2791 {
2792 h->ref_regular = 1;
2793 h->ref_regular_nonweak = 1;
2794 }
45d6a902
AM
2795 else
2796 {
2797 if (h->root.u.def.section->owner != NULL
2798 && (bfd_get_flavour (h->root.u.def.section->owner)
2799 == bfd_target_elf_flavour))
f5385ebf
AM
2800 {
2801 h->ref_regular = 1;
2802 h->ref_regular_nonweak = 1;
2803 }
45d6a902 2804 else
f5385ebf 2805 h->def_regular = 1;
45d6a902
AM
2806 }
2807
2808 if (h->dynindx == -1
f5385ebf
AM
2809 && (h->def_dynamic
2810 || h->ref_dynamic))
45d6a902 2811 {
c152c796 2812 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2813 {
2814 eif->failed = TRUE;
2815 return FALSE;
2816 }
2817 }
2818 }
2819 else
2820 {
f5385ebf 2821 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2822 was first seen in a non-ELF file. Fortunately, if the symbol
2823 was first seen in an ELF file, we're probably OK unless the
2824 symbol was defined in a non-ELF file. Catch that case here.
2825 FIXME: We're still in trouble if the symbol was first seen in
2826 a dynamic object, and then later in a non-ELF regular object. */
2827 if ((h->root.type == bfd_link_hash_defined
2828 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2829 && !h->def_regular
45d6a902
AM
2830 && (h->root.u.def.section->owner != NULL
2831 ? (bfd_get_flavour (h->root.u.def.section->owner)
2832 != bfd_target_elf_flavour)
2833 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2834 && !h->def_dynamic)))
2835 h->def_regular = 1;
45d6a902
AM
2836 }
2837
508c3946 2838 /* Backend specific symbol fixup. */
33774f08
AM
2839 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2840 if (bed->elf_backend_fixup_symbol
2841 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2842 return FALSE;
508c3946 2843
45d6a902
AM
2844 /* If this is a final link, and the symbol was defined as a common
2845 symbol in a regular object file, and there was no definition in
2846 any dynamic object, then the linker will have allocated space for
f5385ebf 2847 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2848 flag will not have been set. */
2849 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2850 && !h->def_regular
2851 && h->ref_regular
2852 && !h->def_dynamic
96f29d96 2853 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2854 h->def_regular = 1;
45d6a902 2855
af0bfb9c
AM
2856 /* Symbols defined in discarded sections shouldn't be dynamic. */
2857 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2858 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2859
4deb8f71
L
2860 /* If a weak undefined symbol has non-default visibility, we also
2861 hide it from the dynamic linker. */
af0bfb9c
AM
2862 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2863 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2864 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2865
2866 /* A hidden versioned symbol in executable should be forced local if
2867 it is is locally defined, not referenced by shared library and not
2868 exported. */
2869 else if (bfd_link_executable (eif->info)
2870 && h->versioned == versioned_hidden
2871 && !eif->info->export_dynamic
2872 && !h->dynamic
2873 && !h->ref_dynamic
2874 && h->def_regular)
2875 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2876
45d6a902
AM
2877 /* If -Bsymbolic was used (which means to bind references to global
2878 symbols to the definition within the shared object), and this
2879 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2880 need a PLT entry. Likewise, if the symbol has non-default
2881 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2882 will force it local. */
4deb8f71
L
2883 else if (h->needs_plt
2884 && bfd_link_pic (eif->info)
2885 && is_elf_hash_table (eif->info->hash)
2886 && (SYMBOLIC_BIND (eif->info, h)
2887 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2888 && h->def_regular)
45d6a902 2889 {
45d6a902
AM
2890 bfd_boolean force_local;
2891
45d6a902
AM
2892 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2893 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2894 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2895 }
2896
45d6a902
AM
2897 /* If this is a weak defined symbol in a dynamic object, and we know
2898 the real definition in the dynamic object, copy interesting flags
2899 over to the real definition. */
60d67dc8 2900 if (h->is_weakalias)
45d6a902 2901 {
60d67dc8
AM
2902 struct elf_link_hash_entry *def = weakdef (h);
2903
45d6a902
AM
2904 /* If the real definition is defined by a regular object file,
2905 don't do anything special. See the longer description in
2906 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2907 if (def->def_regular)
2908 {
2909 h = def;
2910 while ((h = h->u.alias) != def)
2911 h->is_weakalias = 0;
2912 }
45d6a902 2913 else
a26587ba 2914 {
4e6b54a6
AM
2915 while (h->root.type == bfd_link_hash_indirect)
2916 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2917 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2918 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2919 BFD_ASSERT (def->def_dynamic);
2920 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2921 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2922 }
45d6a902
AM
2923 }
2924
2925 return TRUE;
2926}
2927
2928/* Make the backend pick a good value for a dynamic symbol. This is
2929 called via elf_link_hash_traverse, and also calls itself
2930 recursively. */
2931
28caa186 2932static bfd_boolean
268b6b39 2933_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2934{
a50b1753 2935 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2936 struct elf_link_hash_table *htab;
9c5bfbb7 2937 const struct elf_backend_data *bed;
45d6a902 2938
0eddce27 2939 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2940 return FALSE;
2941
45d6a902
AM
2942 /* Ignore indirect symbols. These are added by the versioning code. */
2943 if (h->root.type == bfd_link_hash_indirect)
2944 return TRUE;
2945
2946 /* Fix the symbol flags. */
2947 if (! _bfd_elf_fix_symbol_flags (h, eif))
2948 return FALSE;
2949
559192d8
AM
2950 htab = elf_hash_table (eif->info);
2951 bed = get_elf_backend_data (htab->dynobj);
2952
954b63d4
AM
2953 if (h->root.type == bfd_link_hash_undefweak)
2954 {
2955 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2956 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2957 else if (eif->info->dynamic_undefined_weak > 0
2958 && h->ref_regular
2959 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2960 && !bfd_hide_sym_by_version (eif->info->version_info,
2961 h->root.root.string))
2962 {
2963 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2964 {
2965 eif->failed = TRUE;
2966 return FALSE;
2967 }
2968 }
2969 }
2970
45d6a902
AM
2971 /* If this symbol does not require a PLT entry, and it is not
2972 defined by a dynamic object, or is not referenced by a regular
2973 object, ignore it. We do have to handle a weak defined symbol,
2974 even if no regular object refers to it, if we decided to add it
2975 to the dynamic symbol table. FIXME: Do we normally need to worry
2976 about symbols which are defined by one dynamic object and
2977 referenced by another one? */
f5385ebf 2978 if (!h->needs_plt
91e21fb7 2979 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2980 && (h->def_regular
2981 || !h->def_dynamic
2982 || (!h->ref_regular
60d67dc8 2983 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 2984 {
a6aa5195 2985 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2986 return TRUE;
2987 }
2988
2989 /* If we've already adjusted this symbol, don't do it again. This
2990 can happen via a recursive call. */
f5385ebf 2991 if (h->dynamic_adjusted)
45d6a902
AM
2992 return TRUE;
2993
2994 /* Don't look at this symbol again. Note that we must set this
2995 after checking the above conditions, because we may look at a
2996 symbol once, decide not to do anything, and then get called
2997 recursively later after REF_REGULAR is set below. */
f5385ebf 2998 h->dynamic_adjusted = 1;
45d6a902
AM
2999
3000 /* If this is a weak definition, and we know a real definition, and
3001 the real symbol is not itself defined by a regular object file,
3002 then get a good value for the real definition. We handle the
3003 real symbol first, for the convenience of the backend routine.
3004
3005 Note that there is a confusing case here. If the real definition
3006 is defined by a regular object file, we don't get the real symbol
3007 from the dynamic object, but we do get the weak symbol. If the
3008 processor backend uses a COPY reloc, then if some routine in the
3009 dynamic object changes the real symbol, we will not see that
3010 change in the corresponding weak symbol. This is the way other
3011 ELF linkers work as well, and seems to be a result of the shared
3012 library model.
3013
3014 I will clarify this issue. Most SVR4 shared libraries define the
3015 variable _timezone and define timezone as a weak synonym. The
3016 tzset call changes _timezone. If you write
3017 extern int timezone;
3018 int _timezone = 5;
3019 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3020 you might expect that, since timezone is a synonym for _timezone,
3021 the same number will print both times. However, if the processor
3022 backend uses a COPY reloc, then actually timezone will be copied
3023 into your process image, and, since you define _timezone
3024 yourself, _timezone will not. Thus timezone and _timezone will
3025 wind up at different memory locations. The tzset call will set
3026 _timezone, leaving timezone unchanged. */
3027
60d67dc8 3028 if (h->is_weakalias)
45d6a902 3029 {
60d67dc8
AM
3030 struct elf_link_hash_entry *def = weakdef (h);
3031
ec24dc88 3032 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3033 the alias by a regular object file via the weak symbol H. */
3034 def->ref_regular = 1;
45d6a902 3035
ec24dc88 3036 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3037 the strong alias before H by recursively calling ourselves. */
3038 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3039 return FALSE;
3040 }
3041
3042 /* If a symbol has no type and no size and does not require a PLT
3043 entry, then we are probably about to do the wrong thing here: we
3044 are probably going to create a COPY reloc for an empty object.
3045 This case can arise when a shared object is built with assembly
3046 code, and the assembly code fails to set the symbol type. */
3047 if (h->size == 0
3048 && h->type == STT_NOTYPE
f5385ebf 3049 && !h->needs_plt)
4eca0228 3050 _bfd_error_handler
45d6a902
AM
3051 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3052 h->root.root.string);
3053
45d6a902
AM
3054 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3055 {
3056 eif->failed = TRUE;
3057 return FALSE;
3058 }
3059
3060 return TRUE;
3061}
3062
027297b7
L
3063/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3064 DYNBSS. */
3065
3066bfd_boolean
6cabe1ea
AM
3067_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3068 struct elf_link_hash_entry *h,
027297b7
L
3069 asection *dynbss)
3070{
91ac5911 3071 unsigned int power_of_two;
027297b7
L
3072 bfd_vma mask;
3073 asection *sec = h->root.u.def.section;
3074
de194d85 3075 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3076 requirement of symbols defined in the section. Since we don't
3077 know the symbol alignment requirement, we start with the
3078 maximum alignment and check low bits of the symbol address
3079 for the minimum alignment. */
3080 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3081 mask = ((bfd_vma) 1 << power_of_two) - 1;
3082 while ((h->root.u.def.value & mask) != 0)
3083 {
3084 mask >>= 1;
3085 --power_of_two;
3086 }
027297b7 3087
91ac5911
L
3088 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3089 dynbss))
027297b7
L
3090 {
3091 /* Adjust the section alignment if needed. */
3092 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3093 power_of_two))
027297b7
L
3094 return FALSE;
3095 }
3096
91ac5911 3097 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3098 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3099
3100 /* Define the symbol as being at this point in DYNBSS. */
3101 h->root.u.def.section = dynbss;
3102 h->root.u.def.value = dynbss->size;
3103
3104 /* Increment the size of DYNBSS to make room for the symbol. */
3105 dynbss->size += h->size;
3106
f7483970
L
3107 /* No error if extern_protected_data is true. */
3108 if (h->protected_def
889c2a67
L
3109 && (!info->extern_protected_data
3110 || (info->extern_protected_data < 0
3111 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3112 info->callbacks->einfo
c1c8c1ef 3113 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3114 h->root.root.string);
6cabe1ea 3115
027297b7
L
3116 return TRUE;
3117}
3118
45d6a902
AM
3119/* Adjust all external symbols pointing into SEC_MERGE sections
3120 to reflect the object merging within the sections. */
3121
28caa186 3122static bfd_boolean
268b6b39 3123_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3124{
3125 asection *sec;
3126
45d6a902
AM
3127 if ((h->root.type == bfd_link_hash_defined
3128 || h->root.type == bfd_link_hash_defweak)
3129 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3130 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3131 {
a50b1753 3132 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3133
3134 h->root.u.def.value =
3135 _bfd_merged_section_offset (output_bfd,
3136 &h->root.u.def.section,
3137 elf_section_data (sec)->sec_info,
753731ee 3138 h->root.u.def.value);
45d6a902
AM
3139 }
3140
3141 return TRUE;
3142}
986a241f
RH
3143
3144/* Returns false if the symbol referred to by H should be considered
3145 to resolve local to the current module, and true if it should be
3146 considered to bind dynamically. */
3147
3148bfd_boolean
268b6b39
AM
3149_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3150 struct bfd_link_info *info,
89a2ee5a 3151 bfd_boolean not_local_protected)
986a241f
RH
3152{
3153 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3154 const struct elf_backend_data *bed;
3155 struct elf_link_hash_table *hash_table;
986a241f
RH
3156
3157 if (h == NULL)
3158 return FALSE;
3159
3160 while (h->root.type == bfd_link_hash_indirect
3161 || h->root.type == bfd_link_hash_warning)
3162 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3163
3164 /* If it was forced local, then clearly it's not dynamic. */
3165 if (h->dynindx == -1)
3166 return FALSE;
f5385ebf 3167 if (h->forced_local)
986a241f
RH
3168 return FALSE;
3169
3170 /* Identify the cases where name binding rules say that a
3171 visible symbol resolves locally. */
0e1862bb
L
3172 binding_stays_local_p = (bfd_link_executable (info)
3173 || SYMBOLIC_BIND (info, h));
986a241f
RH
3174
3175 switch (ELF_ST_VISIBILITY (h->other))
3176 {
3177 case STV_INTERNAL:
3178 case STV_HIDDEN:
3179 return FALSE;
3180
3181 case STV_PROTECTED:
fcb93ecf
PB
3182 hash_table = elf_hash_table (info);
3183 if (!is_elf_hash_table (hash_table))
3184 return FALSE;
3185
3186 bed = get_elf_backend_data (hash_table->dynobj);
3187
986a241f
RH
3188 /* Proper resolution for function pointer equality may require
3189 that these symbols perhaps be resolved dynamically, even though
3190 we should be resolving them to the current module. */
89a2ee5a 3191 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3192 binding_stays_local_p = TRUE;
3193 break;
3194
3195 default:
986a241f
RH
3196 break;
3197 }
3198
aa37626c 3199 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3200 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3201 return TRUE;
3202
986a241f
RH
3203 /* Otherwise, the symbol is dynamic if binding rules don't tell
3204 us that it remains local. */
3205 return !binding_stays_local_p;
3206}
f6c52c13
AM
3207
3208/* Return true if the symbol referred to by H should be considered
3209 to resolve local to the current module, and false otherwise. Differs
3210 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3211 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3212 for the place where dynindx == -1 is tested. If that test is true,
3213 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3214 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3215 defined symbols.
89a2ee5a
AM
3216 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3217 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3218 treatment of undefined weak symbols. For those that do not make
3219 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3220
3221bfd_boolean
268b6b39
AM
3222_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3223 struct bfd_link_info *info,
3224 bfd_boolean local_protected)
f6c52c13 3225{
fcb93ecf
PB
3226 const struct elf_backend_data *bed;
3227 struct elf_link_hash_table *hash_table;
3228
f6c52c13
AM
3229 /* If it's a local sym, of course we resolve locally. */
3230 if (h == NULL)
3231 return TRUE;
3232
d95edcac
L
3233 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3234 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3235 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3236 return TRUE;
3237
0fad2956
MR
3238 /* Forced local symbols resolve locally. */
3239 if (h->forced_local)
3240 return TRUE;
3241
7e2294f9
AO
3242 /* Common symbols that become definitions don't get the DEF_REGULAR
3243 flag set, so test it first, and don't bail out. */
3244 if (ELF_COMMON_DEF_P (h))
3245 /* Do nothing. */;
f6c52c13 3246 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3247 resolve locally. The sym is either undefined or dynamic. */
3248 else if (!h->def_regular)
f6c52c13
AM
3249 return FALSE;
3250
0fad2956 3251 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3252 if (h->dynindx == -1)
3253 return TRUE;
3254
3255 /* At this point, we know the symbol is defined and dynamic. In an
3256 executable it must resolve locally, likewise when building symbolic
3257 shared libraries. */
0e1862bb 3258 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3259 return TRUE;
3260
3261 /* Now deal with defined dynamic symbols in shared libraries. Ones
3262 with default visibility might not resolve locally. */
3263 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3264 return FALSE;
3265
fcb93ecf
PB
3266 hash_table = elf_hash_table (info);
3267 if (!is_elf_hash_table (hash_table))
3268 return TRUE;
3269
3270 bed = get_elf_backend_data (hash_table->dynobj);
3271
f7483970
L
3272 /* If extern_protected_data is false, STV_PROTECTED non-function
3273 symbols are local. */
889c2a67
L
3274 if ((!info->extern_protected_data
3275 || (info->extern_protected_data < 0
3276 && !bed->extern_protected_data))
3277 && !bed->is_function_type (h->type))
1c16dfa5
L
3278 return TRUE;
3279
f6c52c13 3280 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3281 symbols be treated as dynamic symbols. If the address of a
3282 function not defined in an executable is set to that function's
3283 plt entry in the executable, then the address of the function in
3284 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3285 return local_protected;
3286}
e1918d23
AM
3287
3288/* Caches some TLS segment info, and ensures that the TLS segment vma is
3289 aligned. Returns the first TLS output section. */
3290
3291struct bfd_section *
3292_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3293{
3294 struct bfd_section *sec, *tls;
3295 unsigned int align = 0;
3296
3297 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3298 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3299 break;
3300 tls = sec;
3301
3302 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3303 if (sec->alignment_power > align)
3304 align = sec->alignment_power;
3305
3306 elf_hash_table (info)->tls_sec = tls;
3307
3308 /* Ensure the alignment of the first section is the largest alignment,
3309 so that the tls segment starts aligned. */
3310 if (tls != NULL)
3311 tls->alignment_power = align;
3312
3313 return tls;
3314}
0ad989f9
L
3315
3316/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3317static bfd_boolean
3318is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3319 Elf_Internal_Sym *sym)
3320{
a4d8e49b
L
3321 const struct elf_backend_data *bed;
3322
0ad989f9
L
3323 /* Local symbols do not count, but target specific ones might. */
3324 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3325 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3326 return FALSE;
3327
fcb93ecf 3328 bed = get_elf_backend_data (abfd);
0ad989f9 3329 /* Function symbols do not count. */
fcb93ecf 3330 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3331 return FALSE;
3332
3333 /* If the section is undefined, then so is the symbol. */
3334 if (sym->st_shndx == SHN_UNDEF)
3335 return FALSE;
3336
3337 /* If the symbol is defined in the common section, then
3338 it is a common definition and so does not count. */
a4d8e49b 3339 if (bed->common_definition (sym))
0ad989f9
L
3340 return FALSE;
3341
3342 /* If the symbol is in a target specific section then we
3343 must rely upon the backend to tell us what it is. */
3344 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3345 /* FIXME - this function is not coded yet:
3346
3347 return _bfd_is_global_symbol_definition (abfd, sym);
3348
3349 Instead for now assume that the definition is not global,
3350 Even if this is wrong, at least the linker will behave
3351 in the same way that it used to do. */
3352 return FALSE;
3353
3354 return TRUE;
3355}
3356
3357/* Search the symbol table of the archive element of the archive ABFD
3358 whose archive map contains a mention of SYMDEF, and determine if
3359 the symbol is defined in this element. */
3360static bfd_boolean
3361elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3362{
3363 Elf_Internal_Shdr * hdr;
ef53be89
AM
3364 size_t symcount;
3365 size_t extsymcount;
3366 size_t extsymoff;
0ad989f9
L
3367 Elf_Internal_Sym *isymbuf;
3368 Elf_Internal_Sym *isym;
3369 Elf_Internal_Sym *isymend;
3370 bfd_boolean result;
3371
3372 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3373 if (abfd == NULL)
3374 return FALSE;
3375
3376 if (! bfd_check_format (abfd, bfd_object))
3377 return FALSE;
3378
7dc3990e
L
3379 /* Select the appropriate symbol table. If we don't know if the
3380 object file is an IR object, give linker LTO plugin a chance to
3381 get the correct symbol table. */
3382 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3383#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3384 || (abfd->plugin_format == bfd_plugin_unknown
3385 && bfd_link_plugin_object_p (abfd))
3386#endif
3387 )
3388 {
3389 /* Use the IR symbol table if the object has been claimed by
3390 plugin. */
3391 abfd = abfd->plugin_dummy_bfd;
3392 hdr = &elf_tdata (abfd)->symtab_hdr;
3393 }
3394 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3395 hdr = &elf_tdata (abfd)->symtab_hdr;
3396 else
3397 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3398
3399 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3400
3401 /* The sh_info field of the symtab header tells us where the
3402 external symbols start. We don't care about the local symbols. */
3403 if (elf_bad_symtab (abfd))
3404 {
3405 extsymcount = symcount;
3406 extsymoff = 0;
3407 }
3408 else
3409 {
3410 extsymcount = symcount - hdr->sh_info;
3411 extsymoff = hdr->sh_info;
3412 }
3413
3414 if (extsymcount == 0)
3415 return FALSE;
3416
3417 /* Read in the symbol table. */
3418 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3419 NULL, NULL, NULL);
3420 if (isymbuf == NULL)
3421 return FALSE;
3422
3423 /* Scan the symbol table looking for SYMDEF. */
3424 result = FALSE;
3425 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3426 {
3427 const char *name;
3428
3429 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3430 isym->st_name);
3431 if (name == NULL)
3432 break;
3433
3434 if (strcmp (name, symdef->name) == 0)
3435 {
3436 result = is_global_data_symbol_definition (abfd, isym);
3437 break;
3438 }
3439 }
3440
3441 free (isymbuf);
3442
3443 return result;
3444}
3445\f
5a580b3a
AM
3446/* Add an entry to the .dynamic table. */
3447
3448bfd_boolean
3449_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3450 bfd_vma tag,
3451 bfd_vma val)
3452{
3453 struct elf_link_hash_table *hash_table;
3454 const struct elf_backend_data *bed;
3455 asection *s;
3456 bfd_size_type newsize;
3457 bfd_byte *newcontents;
3458 Elf_Internal_Dyn dyn;
3459
3460 hash_table = elf_hash_table (info);
3461 if (! is_elf_hash_table (hash_table))
3462 return FALSE;
3463
7f923b7f
AM
3464 if (tag == DT_RELA || tag == DT_REL)
3465 hash_table->dynamic_relocs = TRUE;
3466
5a580b3a 3467 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3468 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3469 BFD_ASSERT (s != NULL);
3470
eea6121a 3471 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3472 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3473 if (newcontents == NULL)
3474 return FALSE;
3475
3476 dyn.d_tag = tag;
3477 dyn.d_un.d_val = val;
eea6121a 3478 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3479
eea6121a 3480 s->size = newsize;
5a580b3a
AM
3481 s->contents = newcontents;
3482
3483 return TRUE;
3484}
3485
3486/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3487 otherwise just check whether one already exists. Returns -1 on error,
3488 1 if a DT_NEEDED tag already exists, and 0 on success. */
3489
4ad4eba5 3490static int
7e9f0867
AM
3491elf_add_dt_needed_tag (bfd *abfd,
3492 struct bfd_link_info *info,
4ad4eba5
AM
3493 const char *soname,
3494 bfd_boolean do_it)
5a580b3a
AM
3495{
3496 struct elf_link_hash_table *hash_table;
ef53be89 3497 size_t strindex;
5a580b3a 3498
7e9f0867
AM
3499 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3500 return -1;
3501
5a580b3a 3502 hash_table = elf_hash_table (info);
5a580b3a 3503 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3504 if (strindex == (size_t) -1)
5a580b3a
AM
3505 return -1;
3506
02be4619 3507 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3508 {
3509 asection *sdyn;
3510 const struct elf_backend_data *bed;
3511 bfd_byte *extdyn;
3512
3513 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3514 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3515 if (sdyn != NULL)
3516 for (extdyn = sdyn->contents;
3517 extdyn < sdyn->contents + sdyn->size;
3518 extdyn += bed->s->sizeof_dyn)
3519 {
3520 Elf_Internal_Dyn dyn;
5a580b3a 3521
7e9f0867
AM
3522 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3523 if (dyn.d_tag == DT_NEEDED
3524 && dyn.d_un.d_val == strindex)
3525 {
3526 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3527 return 1;
3528 }
3529 }
5a580b3a
AM
3530 }
3531
3532 if (do_it)
3533 {
7e9f0867
AM
3534 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3535 return -1;
3536
5a580b3a
AM
3537 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3538 return -1;
3539 }
3540 else
3541 /* We were just checking for existence of the tag. */
3542 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3543
3544 return 0;
3545}
3546
7b15fa7a
AM
3547/* Return true if SONAME is on the needed list between NEEDED and STOP
3548 (or the end of list if STOP is NULL), and needed by a library that
3549 will be loaded. */
3550
010e5ae2 3551static bfd_boolean
7b15fa7a
AM
3552on_needed_list (const char *soname,
3553 struct bfd_link_needed_list *needed,
3554 struct bfd_link_needed_list *stop)
010e5ae2 3555{
7b15fa7a
AM
3556 struct bfd_link_needed_list *look;
3557 for (look = needed; look != stop; look = look->next)
3558 if (strcmp (soname, look->name) == 0
3559 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3560 /* If needed by a library that itself is not directly
3561 needed, recursively check whether that library is
3562 indirectly needed. Since we add DT_NEEDED entries to
3563 the end of the list, library dependencies appear after
3564 the library. Therefore search prior to the current
3565 LOOK, preventing possible infinite recursion. */
3566 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3567 return TRUE;
3568
3569 return FALSE;
3570}
3571
14160578 3572/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3573static int
3574elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3575{
3576 const struct elf_link_hash_entry *h1;
3577 const struct elf_link_hash_entry *h2;
10b7e05b 3578 bfd_signed_vma vdiff;
5a580b3a
AM
3579
3580 h1 = *(const struct elf_link_hash_entry **) arg1;
3581 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3582 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3583 if (vdiff != 0)
3584 return vdiff > 0 ? 1 : -1;
3585 else
3586 {
d3435ae8 3587 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3588 if (sdiff != 0)
3589 return sdiff > 0 ? 1 : -1;
3590 }
14160578
AM
3591 vdiff = h1->size - h2->size;
3592 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3593}
4ad4eba5 3594
5a580b3a
AM
3595/* This function is used to adjust offsets into .dynstr for
3596 dynamic symbols. This is called via elf_link_hash_traverse. */
3597
3598static bfd_boolean
3599elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3600{
a50b1753 3601 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3602
5a580b3a
AM
3603 if (h->dynindx != -1)
3604 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3605 return TRUE;
3606}
3607
3608/* Assign string offsets in .dynstr, update all structures referencing
3609 them. */
3610
4ad4eba5
AM
3611static bfd_boolean
3612elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3613{
3614 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3615 struct elf_link_local_dynamic_entry *entry;
3616 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3617 bfd *dynobj = hash_table->dynobj;
3618 asection *sdyn;
3619 bfd_size_type size;
3620 const struct elf_backend_data *bed;
3621 bfd_byte *extdyn;
3622
3623 _bfd_elf_strtab_finalize (dynstr);
3624 size = _bfd_elf_strtab_size (dynstr);
3625
3626 bed = get_elf_backend_data (dynobj);
3d4d4302 3627 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3628 BFD_ASSERT (sdyn != NULL);
3629
3630 /* Update all .dynamic entries referencing .dynstr strings. */
3631 for (extdyn = sdyn->contents;
eea6121a 3632 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3633 extdyn += bed->s->sizeof_dyn)
3634 {
3635 Elf_Internal_Dyn dyn;
3636
3637 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3638 switch (dyn.d_tag)
3639 {
3640 case DT_STRSZ:
3641 dyn.d_un.d_val = size;
3642 break;
3643 case DT_NEEDED:
3644 case DT_SONAME:
3645 case DT_RPATH:
3646 case DT_RUNPATH:
3647 case DT_FILTER:
3648 case DT_AUXILIARY:
7ee314fa
AM
3649 case DT_AUDIT:
3650 case DT_DEPAUDIT:
5a580b3a
AM
3651 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3652 break;
3653 default:
3654 continue;
3655 }
3656 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3657 }
3658
3659 /* Now update local dynamic symbols. */
3660 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3661 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3662 entry->isym.st_name);
3663
3664 /* And the rest of dynamic symbols. */
3665 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3666
3667 /* Adjust version definitions. */
3668 if (elf_tdata (output_bfd)->cverdefs)
3669 {
3670 asection *s;
3671 bfd_byte *p;
ef53be89 3672 size_t i;
5a580b3a
AM
3673 Elf_Internal_Verdef def;
3674 Elf_Internal_Verdaux defaux;
3675
3d4d4302 3676 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3677 p = s->contents;
3678 do
3679 {
3680 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3681 &def);
3682 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3683 if (def.vd_aux != sizeof (Elf_External_Verdef))
3684 continue;
5a580b3a
AM
3685 for (i = 0; i < def.vd_cnt; ++i)
3686 {
3687 _bfd_elf_swap_verdaux_in (output_bfd,
3688 (Elf_External_Verdaux *) p, &defaux);
3689 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3690 defaux.vda_name);
3691 _bfd_elf_swap_verdaux_out (output_bfd,
3692 &defaux, (Elf_External_Verdaux *) p);
3693 p += sizeof (Elf_External_Verdaux);
3694 }
3695 }
3696 while (def.vd_next);
3697 }
3698
3699 /* Adjust version references. */
3700 if (elf_tdata (output_bfd)->verref)
3701 {
3702 asection *s;
3703 bfd_byte *p;
ef53be89 3704 size_t i;
5a580b3a
AM
3705 Elf_Internal_Verneed need;
3706 Elf_Internal_Vernaux needaux;
3707
3d4d4302 3708 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3709 p = s->contents;
3710 do
3711 {
3712 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3713 &need);
3714 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3715 _bfd_elf_swap_verneed_out (output_bfd, &need,
3716 (Elf_External_Verneed *) p);
3717 p += sizeof (Elf_External_Verneed);
3718 for (i = 0; i < need.vn_cnt; ++i)
3719 {
3720 _bfd_elf_swap_vernaux_in (output_bfd,
3721 (Elf_External_Vernaux *) p, &needaux);
3722 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3723 needaux.vna_name);
3724 _bfd_elf_swap_vernaux_out (output_bfd,
3725 &needaux,
3726 (Elf_External_Vernaux *) p);
3727 p += sizeof (Elf_External_Vernaux);
3728 }
3729 }
3730 while (need.vn_next);
3731 }
3732
3733 return TRUE;
3734}
3735\f
13285a1b
AM
3736/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3737 The default is to only match when the INPUT and OUTPUT are exactly
3738 the same target. */
3739
3740bfd_boolean
3741_bfd_elf_default_relocs_compatible (const bfd_target *input,
3742 const bfd_target *output)
3743{
3744 return input == output;
3745}
3746
3747/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3748 This version is used when different targets for the same architecture
3749 are virtually identical. */
3750
3751bfd_boolean
3752_bfd_elf_relocs_compatible (const bfd_target *input,
3753 const bfd_target *output)
3754{
3755 const struct elf_backend_data *obed, *ibed;
3756
3757 if (input == output)
3758 return TRUE;
3759
3760 ibed = xvec_get_elf_backend_data (input);
3761 obed = xvec_get_elf_backend_data (output);
3762
3763 if (ibed->arch != obed->arch)
3764 return FALSE;
3765
3766 /* If both backends are using this function, deem them compatible. */
3767 return ibed->relocs_compatible == obed->relocs_compatible;
3768}
3769
e5034e59
AM
3770/* Make a special call to the linker "notice" function to tell it that
3771 we are about to handle an as-needed lib, or have finished
1b786873 3772 processing the lib. */
e5034e59
AM
3773
3774bfd_boolean
3775_bfd_elf_notice_as_needed (bfd *ibfd,
3776 struct bfd_link_info *info,
3777 enum notice_asneeded_action act)
3778{
46135103 3779 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3780}
3781
d9689752
L
3782/* Check relocations an ELF object file. */
3783
3784bfd_boolean
3785_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3786{
3787 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3788 struct elf_link_hash_table *htab = elf_hash_table (info);
3789
3790 /* If this object is the same format as the output object, and it is
3791 not a shared library, then let the backend look through the
3792 relocs.
3793
3794 This is required to build global offset table entries and to
3795 arrange for dynamic relocs. It is not required for the
3796 particular common case of linking non PIC code, even when linking
3797 against shared libraries, but unfortunately there is no way of
3798 knowing whether an object file has been compiled PIC or not.
3799 Looking through the relocs is not particularly time consuming.
3800 The problem is that we must either (1) keep the relocs in memory,
3801 which causes the linker to require additional runtime memory or
3802 (2) read the relocs twice from the input file, which wastes time.
3803 This would be a good case for using mmap.
3804
3805 I have no idea how to handle linking PIC code into a file of a
3806 different format. It probably can't be done. */
3807 if ((abfd->flags & DYNAMIC) == 0
3808 && is_elf_hash_table (htab)
3809 && bed->check_relocs != NULL
3810 && elf_object_id (abfd) == elf_hash_table_id (htab)
3811 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3812 {
3813 asection *o;
3814
3815 for (o = abfd->sections; o != NULL; o = o->next)
3816 {
3817 Elf_Internal_Rela *internal_relocs;
3818 bfd_boolean ok;
3819
5ce03cea 3820 /* Don't check relocations in excluded sections. */
d9689752 3821 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3822 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3823 || o->reloc_count == 0
3824 || ((info->strip == strip_all || info->strip == strip_debugger)
3825 && (o->flags & SEC_DEBUGGING) != 0)
3826 || bfd_is_abs_section (o->output_section))
3827 continue;
3828
3829 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3830 info->keep_memory);
3831 if (internal_relocs == NULL)
3832 return FALSE;
3833
3834 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3835
3836 if (elf_section_data (o)->relocs != internal_relocs)
3837 free (internal_relocs);
3838
3839 if (! ok)
3840 return FALSE;
3841 }
3842 }
3843
3844 return TRUE;
3845}
3846
4ad4eba5
AM
3847/* Add symbols from an ELF object file to the linker hash table. */
3848
3849static bfd_boolean
3850elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3851{
a0c402a5 3852 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3853 Elf_Internal_Shdr *hdr;
ef53be89
AM
3854 size_t symcount;
3855 size_t extsymcount;
3856 size_t extsymoff;
4ad4eba5
AM
3857 struct elf_link_hash_entry **sym_hash;
3858 bfd_boolean dynamic;
3859 Elf_External_Versym *extversym = NULL;
3860 Elf_External_Versym *ever;
3861 struct elf_link_hash_entry *weaks;
3862 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3863 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3864 Elf_Internal_Sym *isymbuf = NULL;
3865 Elf_Internal_Sym *isym;
3866 Elf_Internal_Sym *isymend;
3867 const struct elf_backend_data *bed;
3868 bfd_boolean add_needed;
66eb6687 3869 struct elf_link_hash_table *htab;
4ad4eba5 3870 bfd_size_type amt;
66eb6687 3871 void *alloc_mark = NULL;
4f87808c
AM
3872 struct bfd_hash_entry **old_table = NULL;
3873 unsigned int old_size = 0;
3874 unsigned int old_count = 0;
66eb6687 3875 void *old_tab = NULL;
66eb6687
AM
3876 void *old_ent;
3877 struct bfd_link_hash_entry *old_undefs = NULL;
3878 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3879 void *old_strtab = NULL;
66eb6687 3880 size_t tabsize = 0;
db6a5d5f 3881 asection *s;
29a9f53e 3882 bfd_boolean just_syms;
4ad4eba5 3883
66eb6687 3884 htab = elf_hash_table (info);
4ad4eba5 3885 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3886
3887 if ((abfd->flags & DYNAMIC) == 0)
3888 dynamic = FALSE;
3889 else
3890 {
3891 dynamic = TRUE;
3892
3893 /* You can't use -r against a dynamic object. Also, there's no
3894 hope of using a dynamic object which does not exactly match
3895 the format of the output file. */
0e1862bb 3896 if (bfd_link_relocatable (info)
66eb6687 3897 || !is_elf_hash_table (htab)
f13a99db 3898 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3899 {
0e1862bb 3900 if (bfd_link_relocatable (info))
9a0789ec
NC
3901 bfd_set_error (bfd_error_invalid_operation);
3902 else
3903 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3904 goto error_return;
3905 }
3906 }
3907
a0c402a5
L
3908 ehdr = elf_elfheader (abfd);
3909 if (info->warn_alternate_em
3910 && bed->elf_machine_code != ehdr->e_machine
3911 && ((bed->elf_machine_alt1 != 0
3912 && ehdr->e_machine == bed->elf_machine_alt1)
3913 || (bed->elf_machine_alt2 != 0
3914 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3915 _bfd_error_handler
695344c0 3916 /* xgettext:c-format */
9793eb77 3917 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3918 ehdr->e_machine, abfd, bed->elf_machine_code);
3919
4ad4eba5
AM
3920 /* As a GNU extension, any input sections which are named
3921 .gnu.warning.SYMBOL are treated as warning symbols for the given
3922 symbol. This differs from .gnu.warning sections, which generate
3923 warnings when they are included in an output file. */
dd98f8d2 3924 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3925 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3926 {
db6a5d5f 3927 const char *name;
4ad4eba5 3928
db6a5d5f
AM
3929 name = bfd_get_section_name (abfd, s);
3930 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3931 {
db6a5d5f
AM
3932 char *msg;
3933 bfd_size_type sz;
3934
3935 name += sizeof ".gnu.warning." - 1;
3936
3937 /* If this is a shared object, then look up the symbol
3938 in the hash table. If it is there, and it is already
3939 been defined, then we will not be using the entry
3940 from this shared object, so we don't need to warn.
3941 FIXME: If we see the definition in a regular object
3942 later on, we will warn, but we shouldn't. The only
3943 fix is to keep track of what warnings we are supposed
3944 to emit, and then handle them all at the end of the
3945 link. */
3946 if (dynamic)
4ad4eba5 3947 {
db6a5d5f
AM
3948 struct elf_link_hash_entry *h;
3949
3950 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3951
3952 /* FIXME: What about bfd_link_hash_common? */
3953 if (h != NULL
3954 && (h->root.type == bfd_link_hash_defined
3955 || h->root.type == bfd_link_hash_defweak))
3956 continue;
3957 }
4ad4eba5 3958
db6a5d5f
AM
3959 sz = s->size;
3960 msg = (char *) bfd_alloc (abfd, sz + 1);
3961 if (msg == NULL)
3962 goto error_return;
4ad4eba5 3963
db6a5d5f
AM
3964 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3965 goto error_return;
4ad4eba5 3966
db6a5d5f 3967 msg[sz] = '\0';
4ad4eba5 3968
db6a5d5f
AM
3969 if (! (_bfd_generic_link_add_one_symbol
3970 (info, abfd, name, BSF_WARNING, s, 0, msg,
3971 FALSE, bed->collect, NULL)))
3972 goto error_return;
4ad4eba5 3973
0e1862bb 3974 if (bfd_link_executable (info))
db6a5d5f
AM
3975 {
3976 /* Clobber the section size so that the warning does
3977 not get copied into the output file. */
3978 s->size = 0;
11d2f718 3979
db6a5d5f
AM
3980 /* Also set SEC_EXCLUDE, so that symbols defined in
3981 the warning section don't get copied to the output. */
3982 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3983 }
3984 }
3985 }
3986
29a9f53e
L
3987 just_syms = ((s = abfd->sections) != NULL
3988 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3989
4ad4eba5
AM
3990 add_needed = TRUE;
3991 if (! dynamic)
3992 {
3993 /* If we are creating a shared library, create all the dynamic
3994 sections immediately. We need to attach them to something,
3995 so we attach them to this BFD, provided it is the right
bf89386a
L
3996 format and is not from ld --just-symbols. Always create the
3997 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3998 are no input BFD's of the same format as the output, we can't
3999 make a shared library. */
4000 if (!just_syms
bf89386a 4001 && (bfd_link_pic (info)
9c1d7a08 4002 || (!bfd_link_relocatable (info)
3c5fce9b 4003 && info->nointerp
9c1d7a08 4004 && (info->export_dynamic || info->dynamic)))
66eb6687 4005 && is_elf_hash_table (htab)
f13a99db 4006 && info->output_bfd->xvec == abfd->xvec
66eb6687 4007 && !htab->dynamic_sections_created)
4ad4eba5
AM
4008 {
4009 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4010 goto error_return;
4011 }
4012 }
66eb6687 4013 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4014 goto error_return;
4015 else
4016 {
4ad4eba5 4017 const char *soname = NULL;
7ee314fa 4018 char *audit = NULL;
4ad4eba5 4019 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4020 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4021 int ret;
4022
4023 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4024 ld shouldn't allow it. */
29a9f53e 4025 if (just_syms)
92fd189d 4026 abort ();
4ad4eba5
AM
4027
4028 /* If this dynamic lib was specified on the command line with
4029 --as-needed in effect, then we don't want to add a DT_NEEDED
4030 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4031 in by another lib's DT_NEEDED. When --no-add-needed is used
4032 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4033 any dynamic library in DT_NEEDED tags in the dynamic lib at
4034 all. */
4035 add_needed = (elf_dyn_lib_class (abfd)
4036 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4037 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4038
4039 s = bfd_get_section_by_name (abfd, ".dynamic");
4040 if (s != NULL)
4041 {
4042 bfd_byte *dynbuf;
4043 bfd_byte *extdyn;
cb33740c 4044 unsigned int elfsec;
4ad4eba5
AM
4045 unsigned long shlink;
4046
eea6121a 4047 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4048 {
4049error_free_dyn:
4050 free (dynbuf);
4051 goto error_return;
4052 }
4ad4eba5
AM
4053
4054 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4055 if (elfsec == SHN_BAD)
4ad4eba5
AM
4056 goto error_free_dyn;
4057 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4058
4059 for (extdyn = dynbuf;
eea6121a 4060 extdyn < dynbuf + s->size;
4ad4eba5
AM
4061 extdyn += bed->s->sizeof_dyn)
4062 {
4063 Elf_Internal_Dyn dyn;
4064
4065 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4066 if (dyn.d_tag == DT_SONAME)
4067 {
4068 unsigned int tagv = dyn.d_un.d_val;
4069 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4070 if (soname == NULL)
4071 goto error_free_dyn;
4072 }
4073 if (dyn.d_tag == DT_NEEDED)
4074 {
4075 struct bfd_link_needed_list *n, **pn;
4076 char *fnm, *anm;
4077 unsigned int tagv = dyn.d_un.d_val;
4078
4079 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4080 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4081 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4082 if (n == NULL || fnm == NULL)
4083 goto error_free_dyn;
4084 amt = strlen (fnm) + 1;
a50b1753 4085 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4086 if (anm == NULL)
4087 goto error_free_dyn;
4088 memcpy (anm, fnm, amt);
4089 n->name = anm;
4090 n->by = abfd;
4091 n->next = NULL;
66eb6687 4092 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4093 ;
4094 *pn = n;
4095 }
4096 if (dyn.d_tag == DT_RUNPATH)
4097 {
4098 struct bfd_link_needed_list *n, **pn;
4099 char *fnm, *anm;
4100 unsigned int tagv = dyn.d_un.d_val;
4101
4102 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4103 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4104 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4105 if (n == NULL || fnm == NULL)
4106 goto error_free_dyn;
4107 amt = strlen (fnm) + 1;
a50b1753 4108 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4109 if (anm == NULL)
4110 goto error_free_dyn;
4111 memcpy (anm, fnm, amt);
4112 n->name = anm;
4113 n->by = abfd;
4114 n->next = NULL;
4115 for (pn = & runpath;
4116 *pn != NULL;
4117 pn = &(*pn)->next)
4118 ;
4119 *pn = n;
4120 }
4121 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4122 if (!runpath && dyn.d_tag == DT_RPATH)
4123 {
4124 struct bfd_link_needed_list *n, **pn;
4125 char *fnm, *anm;
4126 unsigned int tagv = dyn.d_un.d_val;
4127
4128 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4129 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4130 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4131 if (n == NULL || fnm == NULL)
4132 goto error_free_dyn;
4133 amt = strlen (fnm) + 1;
a50b1753 4134 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4135 if (anm == NULL)
f8703194 4136 goto error_free_dyn;
4ad4eba5
AM
4137 memcpy (anm, fnm, amt);
4138 n->name = anm;
4139 n->by = abfd;
4140 n->next = NULL;
4141 for (pn = & rpath;
4142 *pn != NULL;
4143 pn = &(*pn)->next)
4144 ;
4145 *pn = n;
4146 }
7ee314fa
AM
4147 if (dyn.d_tag == DT_AUDIT)
4148 {
4149 unsigned int tagv = dyn.d_un.d_val;
4150 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4151 }
4ad4eba5
AM
4152 }
4153
4154 free (dynbuf);
4155 }
4156
4157 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4158 frees all more recently bfd_alloc'd blocks as well. */
4159 if (runpath)
4160 rpath = runpath;
4161
4162 if (rpath)
4163 {
4164 struct bfd_link_needed_list **pn;
66eb6687 4165 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4166 ;
4167 *pn = rpath;
4168 }
4169
9acc85a6
AM
4170 /* If we have a PT_GNU_RELRO program header, mark as read-only
4171 all sections contained fully therein. This makes relro
4172 shared library sections appear as they will at run-time. */
4173 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4174 while (--phdr >= elf_tdata (abfd)->phdr)
4175 if (phdr->p_type == PT_GNU_RELRO)
4176 {
4177 for (s = abfd->sections; s != NULL; s = s->next)
4178 if ((s->flags & SEC_ALLOC) != 0
4179 && s->vma >= phdr->p_vaddr
4180 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4181 s->flags |= SEC_READONLY;
4182 break;
4183 }
4184
4ad4eba5
AM
4185 /* We do not want to include any of the sections in a dynamic
4186 object in the output file. We hack by simply clobbering the
4187 list of sections in the BFD. This could be handled more
4188 cleanly by, say, a new section flag; the existing
4189 SEC_NEVER_LOAD flag is not the one we want, because that one
4190 still implies that the section takes up space in the output
4191 file. */
4192 bfd_section_list_clear (abfd);
4193
4ad4eba5
AM
4194 /* Find the name to use in a DT_NEEDED entry that refers to this
4195 object. If the object has a DT_SONAME entry, we use it.
4196 Otherwise, if the generic linker stuck something in
4197 elf_dt_name, we use that. Otherwise, we just use the file
4198 name. */
4199 if (soname == NULL || *soname == '\0')
4200 {
4201 soname = elf_dt_name (abfd);
4202 if (soname == NULL || *soname == '\0')
4203 soname = bfd_get_filename (abfd);
4204 }
4205
4206 /* Save the SONAME because sometimes the linker emulation code
4207 will need to know it. */
4208 elf_dt_name (abfd) = soname;
4209
7e9f0867 4210 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4211 if (ret < 0)
4212 goto error_return;
4213
4214 /* If we have already included this dynamic object in the
4215 link, just ignore it. There is no reason to include a
4216 particular dynamic object more than once. */
4217 if (ret > 0)
4218 return TRUE;
7ee314fa
AM
4219
4220 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4221 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4222 }
4223
4224 /* If this is a dynamic object, we always link against the .dynsym
4225 symbol table, not the .symtab symbol table. The dynamic linker
4226 will only see the .dynsym symbol table, so there is no reason to
4227 look at .symtab for a dynamic object. */
4228
4229 if (! dynamic || elf_dynsymtab (abfd) == 0)
4230 hdr = &elf_tdata (abfd)->symtab_hdr;
4231 else
4232 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4233
4234 symcount = hdr->sh_size / bed->s->sizeof_sym;
4235
4236 /* The sh_info field of the symtab header tells us where the
4237 external symbols start. We don't care about the local symbols at
4238 this point. */
4239 if (elf_bad_symtab (abfd))
4240 {
4241 extsymcount = symcount;
4242 extsymoff = 0;
4243 }
4244 else
4245 {
4246 extsymcount = symcount - hdr->sh_info;
4247 extsymoff = hdr->sh_info;
4248 }
4249
f45794cb 4250 sym_hash = elf_sym_hashes (abfd);
012b2306 4251 if (extsymcount != 0)
4ad4eba5
AM
4252 {
4253 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4254 NULL, NULL, NULL);
4255 if (isymbuf == NULL)
4256 goto error_return;
4257
4ad4eba5 4258 if (sym_hash == NULL)
012b2306
AM
4259 {
4260 /* We store a pointer to the hash table entry for each
4261 external symbol. */
ef53be89
AM
4262 amt = extsymcount;
4263 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4264 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4265 if (sym_hash == NULL)
4266 goto error_free_sym;
4267 elf_sym_hashes (abfd) = sym_hash;
4268 }
4ad4eba5
AM
4269 }
4270
4271 if (dynamic)
4272 {
4273 /* Read in any version definitions. */
fc0e6df6
PB
4274 if (!_bfd_elf_slurp_version_tables (abfd,
4275 info->default_imported_symver))
4ad4eba5
AM
4276 goto error_free_sym;
4277
4278 /* Read in the symbol versions, but don't bother to convert them
4279 to internal format. */
4280 if (elf_dynversym (abfd) != 0)
4281 {
4282 Elf_Internal_Shdr *versymhdr;
4283
4284 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4285 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4286 if (extversym == NULL)
4287 goto error_free_sym;
4288 amt = versymhdr->sh_size;
4289 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4290 || bfd_bread (extversym, amt, abfd) != amt)
4291 goto error_free_vers;
4292 }
4293 }
4294
66eb6687
AM
4295 /* If we are loading an as-needed shared lib, save the symbol table
4296 state before we start adding symbols. If the lib turns out
4297 to be unneeded, restore the state. */
4298 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4299 {
4300 unsigned int i;
4301 size_t entsize;
4302
4303 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4304 {
4305 struct bfd_hash_entry *p;
2de92251 4306 struct elf_link_hash_entry *h;
66eb6687
AM
4307
4308 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4309 {
4310 h = (struct elf_link_hash_entry *) p;
4311 entsize += htab->root.table.entsize;
4312 if (h->root.type == bfd_link_hash_warning)
4313 entsize += htab->root.table.entsize;
4314 }
66eb6687
AM
4315 }
4316
4317 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4318 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4319 if (old_tab == NULL)
4320 goto error_free_vers;
4321
4322 /* Remember the current objalloc pointer, so that all mem for
4323 symbols added can later be reclaimed. */
4324 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4325 if (alloc_mark == NULL)
4326 goto error_free_vers;
4327
5061a885
AM
4328 /* Make a special call to the linker "notice" function to
4329 tell it that we are about to handle an as-needed lib. */
e5034e59 4330 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4331 goto error_free_vers;
5061a885 4332
f45794cb
AM
4333 /* Clone the symbol table. Remember some pointers into the
4334 symbol table, and dynamic symbol count. */
4335 old_ent = (char *) old_tab + tabsize;
66eb6687 4336 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4337 old_undefs = htab->root.undefs;
4338 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4339 old_table = htab->root.table.table;
4340 old_size = htab->root.table.size;
4341 old_count = htab->root.table.count;
5b677558
AM
4342 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4343 if (old_strtab == NULL)
4344 goto error_free_vers;
66eb6687
AM
4345
4346 for (i = 0; i < htab->root.table.size; i++)
4347 {
4348 struct bfd_hash_entry *p;
2de92251 4349 struct elf_link_hash_entry *h;
66eb6687
AM
4350
4351 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4352 {
4353 memcpy (old_ent, p, htab->root.table.entsize);
4354 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4355 h = (struct elf_link_hash_entry *) p;
4356 if (h->root.type == bfd_link_hash_warning)
4357 {
4358 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4359 old_ent = (char *) old_ent + htab->root.table.entsize;
4360 }
66eb6687
AM
4361 }
4362 }
4363 }
4ad4eba5 4364
66eb6687 4365 weaks = NULL;
4ad4eba5
AM
4366 ever = extversym != NULL ? extversym + extsymoff : NULL;
4367 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4368 isym < isymend;
4369 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4370 {
4371 int bind;
4372 bfd_vma value;
af44c138 4373 asection *sec, *new_sec;
4ad4eba5
AM
4374 flagword flags;
4375 const char *name;
4376 struct elf_link_hash_entry *h;
90c984fc 4377 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4378 bfd_boolean definition;
4379 bfd_boolean size_change_ok;
4380 bfd_boolean type_change_ok;
37a9e49a
L
4381 bfd_boolean new_weak;
4382 bfd_boolean old_weak;
4ad4eba5 4383 bfd_boolean override;
a4d8e49b 4384 bfd_boolean common;
97196564 4385 bfd_boolean discarded;
4ad4eba5
AM
4386 unsigned int old_alignment;
4387 bfd *old_bfd;
6e33951e 4388 bfd_boolean matched;
4ad4eba5
AM
4389
4390 override = FALSE;
4391
4392 flags = BSF_NO_FLAGS;
4393 sec = NULL;
4394 value = isym->st_value;
a4d8e49b 4395 common = bed->common_definition (isym);
2980ccad
L
4396 if (common && info->inhibit_common_definition)
4397 {
4398 /* Treat common symbol as undefined for --no-define-common. */
4399 isym->st_shndx = SHN_UNDEF;
4400 common = FALSE;
4401 }
97196564 4402 discarded = FALSE;
4ad4eba5
AM
4403
4404 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4405 switch (bind)
4ad4eba5 4406 {
3e7a7d11 4407 case STB_LOCAL:
4ad4eba5
AM
4408 /* This should be impossible, since ELF requires that all
4409 global symbols follow all local symbols, and that sh_info
4410 point to the first global symbol. Unfortunately, Irix 5
4411 screws this up. */
4412 continue;
3e7a7d11
NC
4413
4414 case STB_GLOBAL:
a4d8e49b 4415 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4416 flags = BSF_GLOBAL;
3e7a7d11
NC
4417 break;
4418
4419 case STB_WEAK:
4420 flags = BSF_WEAK;
4421 break;
4422
4423 case STB_GNU_UNIQUE:
4424 flags = BSF_GNU_UNIQUE;
4425 break;
4426
4427 default:
4ad4eba5 4428 /* Leave it up to the processor backend. */
3e7a7d11 4429 break;
4ad4eba5
AM
4430 }
4431
4432 if (isym->st_shndx == SHN_UNDEF)
4433 sec = bfd_und_section_ptr;
cb33740c
AM
4434 else if (isym->st_shndx == SHN_ABS)
4435 sec = bfd_abs_section_ptr;
4436 else if (isym->st_shndx == SHN_COMMON)
4437 {
4438 sec = bfd_com_section_ptr;
4439 /* What ELF calls the size we call the value. What ELF
4440 calls the value we call the alignment. */
4441 value = isym->st_size;
4442 }
4443 else
4ad4eba5
AM
4444 {
4445 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4446 if (sec == NULL)
4447 sec = bfd_abs_section_ptr;
dbaa2011 4448 else if (discarded_section (sec))
529fcb95 4449 {
e5d08002
L
4450 /* Symbols from discarded section are undefined. We keep
4451 its visibility. */
529fcb95 4452 sec = bfd_und_section_ptr;
97196564 4453 discarded = TRUE;
529fcb95
PB
4454 isym->st_shndx = SHN_UNDEF;
4455 }
4ad4eba5
AM
4456 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4457 value -= sec->vma;
4458 }
4ad4eba5
AM
4459
4460 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4461 isym->st_name);
4462 if (name == NULL)
4463 goto error_free_vers;
4464
4465 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4466 && (abfd->flags & BFD_PLUGIN) != 0)
4467 {
4468 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4469
4470 if (xc == NULL)
4471 {
4472 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4473 | SEC_EXCLUDE);
4474 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4475 if (xc == NULL)
4476 goto error_free_vers;
4477 }
4478 sec = xc;
4479 }
4480 else if (isym->st_shndx == SHN_COMMON
4481 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4482 && !bfd_link_relocatable (info))
4ad4eba5
AM
4483 {
4484 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4485
4486 if (tcomm == NULL)
4487 {
02d00247
AM
4488 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4489 | SEC_LINKER_CREATED);
4490 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4491 if (tcomm == NULL)
4ad4eba5
AM
4492 goto error_free_vers;
4493 }
4494 sec = tcomm;
4495 }
66eb6687 4496 else if (bed->elf_add_symbol_hook)
4ad4eba5 4497 {
66eb6687
AM
4498 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4499 &sec, &value))
4ad4eba5
AM
4500 goto error_free_vers;
4501
4502 /* The hook function sets the name to NULL if this symbol
4503 should be skipped for some reason. */
4504 if (name == NULL)
4505 continue;
4506 }
4507
4508 /* Sanity check that all possibilities were handled. */
4509 if (sec == NULL)
4510 {
4511 bfd_set_error (bfd_error_bad_value);
4512 goto error_free_vers;
4513 }
4514
191c0c42
AM
4515 /* Silently discard TLS symbols from --just-syms. There's
4516 no way to combine a static TLS block with a new TLS block
4517 for this executable. */
4518 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4519 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4520 continue;
4521
4ad4eba5
AM
4522 if (bfd_is_und_section (sec)
4523 || bfd_is_com_section (sec))
4524 definition = FALSE;
4525 else
4526 definition = TRUE;
4527
4528 size_change_ok = FALSE;
66eb6687 4529 type_change_ok = bed->type_change_ok;
37a9e49a 4530 old_weak = FALSE;
6e33951e 4531 matched = FALSE;
4ad4eba5
AM
4532 old_alignment = 0;
4533 old_bfd = NULL;
af44c138 4534 new_sec = sec;
4ad4eba5 4535
66eb6687 4536 if (is_elf_hash_table (htab))
4ad4eba5
AM
4537 {
4538 Elf_Internal_Versym iver;
4539 unsigned int vernum = 0;
4540 bfd_boolean skip;
4541
fc0e6df6 4542 if (ever == NULL)
4ad4eba5 4543 {
fc0e6df6
PB
4544 if (info->default_imported_symver)
4545 /* Use the default symbol version created earlier. */
4546 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4547 else
4548 iver.vs_vers = 0;
4549 }
4550 else
4551 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4552
4553 vernum = iver.vs_vers & VERSYM_VERSION;
4554
4555 /* If this is a hidden symbol, or if it is not version
4556 1, we append the version name to the symbol name.
cc86ff91
EB
4557 However, we do not modify a non-hidden absolute symbol
4558 if it is not a function, because it might be the version
4559 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4560 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4561 || (vernum > 1
4562 && (!bfd_is_abs_section (sec)
4563 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4564 {
4565 const char *verstr;
4566 size_t namelen, verlen, newlen;
4567 char *newname, *p;
4568
4569 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4570 {
fc0e6df6
PB
4571 if (vernum > elf_tdata (abfd)->cverdefs)
4572 verstr = NULL;
4573 else if (vernum > 1)
4574 verstr =
4575 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4576 else
4577 verstr = "";
4ad4eba5 4578
fc0e6df6 4579 if (verstr == NULL)
4ad4eba5 4580 {
4eca0228 4581 _bfd_error_handler
695344c0 4582 /* xgettext:c-format */
871b3ab2 4583 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4584 abfd, name, vernum,
4585 elf_tdata (abfd)->cverdefs);
4586 bfd_set_error (bfd_error_bad_value);
4587 goto error_free_vers;
4ad4eba5 4588 }
fc0e6df6
PB
4589 }
4590 else
4591 {
4592 /* We cannot simply test for the number of
4593 entries in the VERNEED section since the
4594 numbers for the needed versions do not start
4595 at 0. */
4596 Elf_Internal_Verneed *t;
4597
4598 verstr = NULL;
4599 for (t = elf_tdata (abfd)->verref;
4600 t != NULL;
4601 t = t->vn_nextref)
4ad4eba5 4602 {
fc0e6df6 4603 Elf_Internal_Vernaux *a;
4ad4eba5 4604
fc0e6df6
PB
4605 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4606 {
4607 if (a->vna_other == vernum)
4ad4eba5 4608 {
fc0e6df6
PB
4609 verstr = a->vna_nodename;
4610 break;
4ad4eba5 4611 }
4ad4eba5 4612 }
fc0e6df6
PB
4613 if (a != NULL)
4614 break;
4615 }
4616 if (verstr == NULL)
4617 {
4eca0228 4618 _bfd_error_handler
695344c0 4619 /* xgettext:c-format */
871b3ab2 4620 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4621 abfd, name, vernum);
4622 bfd_set_error (bfd_error_bad_value);
4623 goto error_free_vers;
4ad4eba5 4624 }
4ad4eba5 4625 }
fc0e6df6
PB
4626
4627 namelen = strlen (name);
4628 verlen = strlen (verstr);
4629 newlen = namelen + verlen + 2;
4630 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4631 && isym->st_shndx != SHN_UNDEF)
4632 ++newlen;
4633
a50b1753 4634 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4635 if (newname == NULL)
4636 goto error_free_vers;
4637 memcpy (newname, name, namelen);
4638 p = newname + namelen;
4639 *p++ = ELF_VER_CHR;
4640 /* If this is a defined non-hidden version symbol,
4641 we add another @ to the name. This indicates the
4642 default version of the symbol. */
4643 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4644 && isym->st_shndx != SHN_UNDEF)
4645 *p++ = ELF_VER_CHR;
4646 memcpy (p, verstr, verlen + 1);
4647
4648 name = newname;
4ad4eba5
AM
4649 }
4650
cd3416da
AM
4651 /* If this symbol has default visibility and the user has
4652 requested we not re-export it, then mark it as hidden. */
a0d49154 4653 if (!bfd_is_und_section (sec)
cd3416da 4654 && !dynamic
ce875075 4655 && abfd->no_export
cd3416da
AM
4656 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4657 isym->st_other = (STV_HIDDEN
4658 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4659
4f3fedcf
AM
4660 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4661 sym_hash, &old_bfd, &old_weak,
4662 &old_alignment, &skip, &override,
6e33951e
L
4663 &type_change_ok, &size_change_ok,
4664 &matched))
4ad4eba5
AM
4665 goto error_free_vers;
4666
4667 if (skip)
4668 continue;
4669
6e33951e
L
4670 /* Override a definition only if the new symbol matches the
4671 existing one. */
4672 if (override && matched)
4ad4eba5
AM
4673 definition = FALSE;
4674
4675 h = *sym_hash;
4676 while (h->root.type == bfd_link_hash_indirect
4677 || h->root.type == bfd_link_hash_warning)
4678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4679
4ad4eba5 4680 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4681 && vernum > 1
4682 && definition)
4683 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4684 }
4685
4686 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4687 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4688 (struct bfd_link_hash_entry **) sym_hash)))
4689 goto error_free_vers;
4690
ac98f9e2
L
4691 if ((abfd->flags & DYNAMIC) == 0
4692 && (bfd_get_flavour (info->output_bfd)
4693 == bfd_target_elf_flavour))
4694 {
4695 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4696 elf_tdata (info->output_bfd)->has_gnu_symbols
4697 |= elf_gnu_symbol_ifunc;
4698 if ((flags & BSF_GNU_UNIQUE))
4699 elf_tdata (info->output_bfd)->has_gnu_symbols
4700 |= elf_gnu_symbol_unique;
4701 }
a43942db 4702
4ad4eba5 4703 h = *sym_hash;
90c984fc
L
4704 /* We need to make sure that indirect symbol dynamic flags are
4705 updated. */
4706 hi = h;
4ad4eba5
AM
4707 while (h->root.type == bfd_link_hash_indirect
4708 || h->root.type == bfd_link_hash_warning)
4709 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4710
97196564
L
4711 /* Setting the index to -3 tells elf_link_output_extsym that
4712 this symbol is defined in a discarded section. */
4713 if (discarded)
4714 h->indx = -3;
4715
4ad4eba5
AM
4716 *sym_hash = h;
4717
37a9e49a 4718 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4719 if (dynamic
4720 && definition
37a9e49a 4721 && new_weak
fcb93ecf 4722 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4723 && is_elf_hash_table (htab)
60d67dc8 4724 && h->u.alias == NULL)
4ad4eba5
AM
4725 {
4726 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4727 a dynamic object, using the alias field. Later in this
4728 function we will set the alias field to the correct
4ad4eba5
AM
4729 value. We only put non-function symbols from dynamic
4730 objects on this list, because that happens to be the only
4731 time we need to know the normal symbol corresponding to a
4732 weak symbol, and the information is time consuming to
60d67dc8 4733 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4734 then this symbol was already defined by some previous
4735 dynamic object, and we will be using that previous
4736 definition anyhow. */
4737
60d67dc8 4738 h->u.alias = weaks;
4ad4eba5 4739 weaks = h;
4ad4eba5
AM
4740 }
4741
4742 /* Set the alignment of a common symbol. */
a4d8e49b 4743 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4744 && h->root.type == bfd_link_hash_common)
4745 {
4746 unsigned int align;
4747
a4d8e49b 4748 if (common)
af44c138
L
4749 align = bfd_log2 (isym->st_value);
4750 else
4751 {
4752 /* The new symbol is a common symbol in a shared object.
4753 We need to get the alignment from the section. */
4754 align = new_sec->alignment_power;
4755 }
595213d4 4756 if (align > old_alignment)
4ad4eba5
AM
4757 h->root.u.c.p->alignment_power = align;
4758 else
4759 h->root.u.c.p->alignment_power = old_alignment;
4760 }
4761
66eb6687 4762 if (is_elf_hash_table (htab))
4ad4eba5 4763 {
4f3fedcf
AM
4764 /* Set a flag in the hash table entry indicating the type of
4765 reference or definition we just found. A dynamic symbol
4766 is one which is referenced or defined by both a regular
4767 object and a shared object. */
4768 bfd_boolean dynsym = FALSE;
4769
4770 /* Plugin symbols aren't normal. Don't set def_regular or
4771 ref_regular for them, or make them dynamic. */
4772 if ((abfd->flags & BFD_PLUGIN) != 0)
4773 ;
4774 else if (! dynamic)
4775 {
4776 if (! definition)
4777 {
4778 h->ref_regular = 1;
4779 if (bind != STB_WEAK)
4780 h->ref_regular_nonweak = 1;
4781 }
4782 else
4783 {
4784 h->def_regular = 1;
4785 if (h->def_dynamic)
4786 {
4787 h->def_dynamic = 0;
4788 h->ref_dynamic = 1;
4789 }
4790 }
4791
4792 /* If the indirect symbol has been forced local, don't
4793 make the real symbol dynamic. */
4794 if ((h == hi || !hi->forced_local)
0e1862bb 4795 && (bfd_link_dll (info)
4f3fedcf
AM
4796 || h->def_dynamic
4797 || h->ref_dynamic))
4798 dynsym = TRUE;
4799 }
4800 else
4801 {
4802 if (! definition)
4803 {
4804 h->ref_dynamic = 1;
4805 hi->ref_dynamic = 1;
4806 }
4807 else
4808 {
4809 h->def_dynamic = 1;
4810 hi->def_dynamic = 1;
4811 }
4812
4813 /* If the indirect symbol has been forced local, don't
4814 make the real symbol dynamic. */
4815 if ((h == hi || !hi->forced_local)
4816 && (h->def_regular
4817 || h->ref_regular
60d67dc8
AM
4818 || (h->is_weakalias
4819 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4820 dynsym = TRUE;
4821 }
4822
4823 /* Check to see if we need to add an indirect symbol for
4824 the default name. */
4825 if (definition
4826 || (!override && h->root.type == bfd_link_hash_common))
4827 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4828 sec, value, &old_bfd, &dynsym))
4829 goto error_free_vers;
4ad4eba5
AM
4830
4831 /* Check the alignment when a common symbol is involved. This
4832 can change when a common symbol is overridden by a normal
4833 definition or a common symbol is ignored due to the old
4834 normal definition. We need to make sure the maximum
4835 alignment is maintained. */
a4d8e49b 4836 if ((old_alignment || common)
4ad4eba5
AM
4837 && h->root.type != bfd_link_hash_common)
4838 {
4839 unsigned int common_align;
4840 unsigned int normal_align;
4841 unsigned int symbol_align;
4842 bfd *normal_bfd;
4843 bfd *common_bfd;
4844
3a81e825
AM
4845 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4846 || h->root.type == bfd_link_hash_defweak);
4847
4ad4eba5
AM
4848 symbol_align = ffs (h->root.u.def.value) - 1;
4849 if (h->root.u.def.section->owner != NULL
0616a280
AM
4850 && (h->root.u.def.section->owner->flags
4851 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4852 {
4853 normal_align = h->root.u.def.section->alignment_power;
4854 if (normal_align > symbol_align)
4855 normal_align = symbol_align;
4856 }
4857 else
4858 normal_align = symbol_align;
4859
4860 if (old_alignment)
4861 {
4862 common_align = old_alignment;
4863 common_bfd = old_bfd;
4864 normal_bfd = abfd;
4865 }
4866 else
4867 {
4868 common_align = bfd_log2 (isym->st_value);
4869 common_bfd = abfd;
4870 normal_bfd = old_bfd;
4871 }
4872
4873 if (normal_align < common_align)
d07676f8
NC
4874 {
4875 /* PR binutils/2735 */
4876 if (normal_bfd == NULL)
4eca0228 4877 _bfd_error_handler
695344c0 4878 /* xgettext:c-format */
9793eb77 4879 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4880 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4881 1 << common_align, name, common_bfd,
4882 1 << normal_align, h->root.u.def.section);
d07676f8 4883 else
4eca0228 4884 _bfd_error_handler
695344c0 4885 /* xgettext:c-format */
9793eb77 4886 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4887 " is smaller than %u in %pB"),
c08bb8dd
AM
4888 1 << normal_align, name, normal_bfd,
4889 1 << common_align, common_bfd);
d07676f8 4890 }
4ad4eba5
AM
4891 }
4892
83ad0046 4893 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4894 if (isym->st_size != 0
4895 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4896 && (definition || h->size == 0))
4897 {
83ad0046
L
4898 if (h->size != 0
4899 && h->size != isym->st_size
4900 && ! size_change_ok)
4eca0228 4901 _bfd_error_handler
695344c0 4902 /* xgettext:c-format */
9793eb77 4903 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4904 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4905 name, (uint64_t) h->size, old_bfd,
4906 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4907
4908 h->size = isym->st_size;
4909 }
4910
4911 /* If this is a common symbol, then we always want H->SIZE
4912 to be the size of the common symbol. The code just above
4913 won't fix the size if a common symbol becomes larger. We
4914 don't warn about a size change here, because that is
4f3fedcf 4915 covered by --warn-common. Allow changes between different
fcb93ecf 4916 function types. */
4ad4eba5
AM
4917 if (h->root.type == bfd_link_hash_common)
4918 h->size = h->root.u.c.size;
4919
4920 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4921 && ((definition && !new_weak)
4922 || (old_weak && h->root.type == bfd_link_hash_common)
4923 || h->type == STT_NOTYPE))
4ad4eba5 4924 {
2955ec4c
L
4925 unsigned int type = ELF_ST_TYPE (isym->st_info);
4926
4927 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4928 symbol. */
4929 if (type == STT_GNU_IFUNC
4930 && (abfd->flags & DYNAMIC) != 0)
4931 type = STT_FUNC;
4ad4eba5 4932
2955ec4c
L
4933 if (h->type != type)
4934 {
4935 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4936 /* xgettext:c-format */
4eca0228 4937 _bfd_error_handler
9793eb77 4938 (_("warning: type of symbol `%s' changed"
871b3ab2 4939 " from %d to %d in %pB"),
c08bb8dd 4940 name, h->type, type, abfd);
2955ec4c
L
4941
4942 h->type = type;
4943 }
4ad4eba5
AM
4944 }
4945
54ac0771 4946 /* Merge st_other field. */
b8417128 4947 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4948
c3df8c14 4949 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4950 if (definition
4951 && (sec->flags & SEC_DEBUGGING)
4952 && !bfd_link_relocatable (info))
c3df8c14
AM
4953 dynsym = FALSE;
4954
4f3fedcf
AM
4955 /* Nor should we make plugin symbols dynamic. */
4956 if ((abfd->flags & BFD_PLUGIN) != 0)
4957 dynsym = FALSE;
4958
35fc36a8 4959 if (definition)
35399224
L
4960 {
4961 h->target_internal = isym->st_target_internal;
4962 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4963 }
35fc36a8 4964
4ad4eba5
AM
4965 if (definition && !dynamic)
4966 {
4967 char *p = strchr (name, ELF_VER_CHR);
4968 if (p != NULL && p[1] != ELF_VER_CHR)
4969 {
4970 /* Queue non-default versions so that .symver x, x@FOO
4971 aliases can be checked. */
66eb6687 4972 if (!nondeflt_vers)
4ad4eba5 4973 {
66eb6687
AM
4974 amt = ((isymend - isym + 1)
4975 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4976 nondeflt_vers
4977 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4978 if (!nondeflt_vers)
4979 goto error_free_vers;
4ad4eba5 4980 }
66eb6687 4981 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4982 }
4983 }
4984
4985 if (dynsym && h->dynindx == -1)
4986 {
c152c796 4987 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4988 goto error_free_vers;
60d67dc8
AM
4989 if (h->is_weakalias
4990 && weakdef (h)->dynindx == -1)
4ad4eba5 4991 {
60d67dc8 4992 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
4993 goto error_free_vers;
4994 }
4995 }
1f599d0e 4996 else if (h->dynindx != -1)
4ad4eba5
AM
4997 /* If the symbol already has a dynamic index, but
4998 visibility says it should not be visible, turn it into
4999 a local symbol. */
5000 switch (ELF_ST_VISIBILITY (h->other))
5001 {
5002 case STV_INTERNAL:
5003 case STV_HIDDEN:
5004 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5005 dynsym = FALSE;
5006 break;
5007 }
5008
aef28989
L
5009 /* Don't add DT_NEEDED for references from the dummy bfd nor
5010 for unmatched symbol. */
4ad4eba5 5011 if (!add_needed
aef28989 5012 && matched
4ad4eba5 5013 && definition
010e5ae2 5014 && ((dynsym
ffa9430d 5015 && h->ref_regular_nonweak
4f3fedcf
AM
5016 && (old_bfd == NULL
5017 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5018 || (h->ref_dynamic_nonweak
010e5ae2 5019 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5020 && !on_needed_list (elf_dt_name (abfd),
5021 htab->needed, NULL))))
4ad4eba5
AM
5022 {
5023 int ret;
5024 const char *soname = elf_dt_name (abfd);
5025
16e4ecc0
AM
5026 info->callbacks->minfo ("%!", soname, old_bfd,
5027 h->root.root.string);
5028
4ad4eba5
AM
5029 /* A symbol from a library loaded via DT_NEEDED of some
5030 other library is referenced by a regular object.
e56f61be 5031 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5032 --no-add-needed is used and the reference was not
5033 a weak one. */
4f3fedcf 5034 if (old_bfd != NULL
b918acf9 5035 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5036 {
4eca0228 5037 _bfd_error_handler
695344c0 5038 /* xgettext:c-format */
871b3ab2 5039 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5040 old_bfd, name);
ff5ac77b 5041 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5042 goto error_free_vers;
5043 }
5044
a50b1753 5045 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5046 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5047
4ad4eba5 5048 add_needed = TRUE;
7e9f0867 5049 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5050 if (ret < 0)
5051 goto error_free_vers;
5052
5053 BFD_ASSERT (ret == 0);
5054 }
5055 }
5056 }
5057
a83ef4d1
L
5058 if (info->lto_plugin_active
5059 && !bfd_link_relocatable (info)
5060 && (abfd->flags & BFD_PLUGIN) == 0
5061 && !just_syms
5062 && extsymcount)
5063 {
5064 int r_sym_shift;
5065
5066 if (bed->s->arch_size == 32)
5067 r_sym_shift = 8;
5068 else
5069 r_sym_shift = 32;
5070
5071 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5072 referenced in regular objects so that linker plugin will get
5073 the correct symbol resolution. */
5074
5075 sym_hash = elf_sym_hashes (abfd);
5076 for (s = abfd->sections; s != NULL; s = s->next)
5077 {
5078 Elf_Internal_Rela *internal_relocs;
5079 Elf_Internal_Rela *rel, *relend;
5080
5081 /* Don't check relocations in excluded sections. */
5082 if ((s->flags & SEC_RELOC) == 0
5083 || s->reloc_count == 0
5084 || (s->flags & SEC_EXCLUDE) != 0
5085 || ((info->strip == strip_all
5086 || info->strip == strip_debugger)
5087 && (s->flags & SEC_DEBUGGING) != 0))
5088 continue;
5089
5090 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5091 NULL,
5092 info->keep_memory);
5093 if (internal_relocs == NULL)
5094 goto error_free_vers;
5095
5096 rel = internal_relocs;
5097 relend = rel + s->reloc_count;
5098 for ( ; rel < relend; rel++)
5099 {
5100 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5101 struct elf_link_hash_entry *h;
5102
5103 /* Skip local symbols. */
5104 if (r_symndx < extsymoff)
5105 continue;
5106
5107 h = sym_hash[r_symndx - extsymoff];
5108 if (h != NULL)
5109 h->root.non_ir_ref_regular = 1;
5110 }
5111
5112 if (elf_section_data (s)->relocs != internal_relocs)
5113 free (internal_relocs);
5114 }
5115 }
5116
66eb6687
AM
5117 if (extversym != NULL)
5118 {
5119 free (extversym);
5120 extversym = NULL;
5121 }
5122
5123 if (isymbuf != NULL)
5124 {
5125 free (isymbuf);
5126 isymbuf = NULL;
5127 }
5128
5129 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5130 {
5131 unsigned int i;
5132
5133 /* Restore the symbol table. */
f45794cb
AM
5134 old_ent = (char *) old_tab + tabsize;
5135 memset (elf_sym_hashes (abfd), 0,
5136 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5137 htab->root.table.table = old_table;
5138 htab->root.table.size = old_size;
5139 htab->root.table.count = old_count;
66eb6687 5140 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5141 htab->root.undefs = old_undefs;
5142 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5143 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5144 free (old_strtab);
5145 old_strtab = NULL;
66eb6687
AM
5146 for (i = 0; i < htab->root.table.size; i++)
5147 {
5148 struct bfd_hash_entry *p;
5149 struct elf_link_hash_entry *h;
3e0882af
L
5150 bfd_size_type size;
5151 unsigned int alignment_power;
4070765b 5152 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5153
5154 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5155 {
5156 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5157 if (h->root.type == bfd_link_hash_warning)
5158 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5159
3e0882af
L
5160 /* Preserve the maximum alignment and size for common
5161 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5162 since it can still be loaded at run time by another
3e0882af
L
5163 dynamic lib. */
5164 if (h->root.type == bfd_link_hash_common)
5165 {
5166 size = h->root.u.c.size;
5167 alignment_power = h->root.u.c.p->alignment_power;
5168 }
5169 else
5170 {
5171 size = 0;
5172 alignment_power = 0;
5173 }
4070765b 5174 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5175 will be exported when the dynamic lib becomes needed
5176 in the second pass. */
4070765b 5177 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5178 memcpy (p, old_ent, htab->root.table.entsize);
5179 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5180 h = (struct elf_link_hash_entry *) p;
5181 if (h->root.type == bfd_link_hash_warning)
5182 {
5183 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5184 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5185 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5186 }
a4542f1b 5187 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5188 {
5189 if (size > h->root.u.c.size)
5190 h->root.u.c.size = size;
5191 if (alignment_power > h->root.u.c.p->alignment_power)
5192 h->root.u.c.p->alignment_power = alignment_power;
5193 }
4070765b 5194 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5195 }
5196 }
5197
5061a885
AM
5198 /* Make a special call to the linker "notice" function to
5199 tell it that symbols added for crefs may need to be removed. */
e5034e59 5200 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5201 goto error_free_vers;
5061a885 5202
66eb6687
AM
5203 free (old_tab);
5204 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5205 alloc_mark);
5206 if (nondeflt_vers != NULL)
5207 free (nondeflt_vers);
5208 return TRUE;
5209 }
2de92251 5210
66eb6687
AM
5211 if (old_tab != NULL)
5212 {
e5034e59 5213 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5214 goto error_free_vers;
66eb6687
AM
5215 free (old_tab);
5216 old_tab = NULL;
5217 }
5218
c6e8a9a8
L
5219 /* Now that all the symbols from this input file are created, if
5220 not performing a relocatable link, handle .symver foo, foo@BAR
5221 such that any relocs against foo become foo@BAR. */
0e1862bb 5222 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5223 {
ef53be89 5224 size_t cnt, symidx;
4ad4eba5
AM
5225
5226 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5227 {
5228 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5229 char *shortname, *p;
5230
5231 p = strchr (h->root.root.string, ELF_VER_CHR);
5232 if (p == NULL
5233 || (h->root.type != bfd_link_hash_defined
5234 && h->root.type != bfd_link_hash_defweak))
5235 continue;
5236
5237 amt = p - h->root.root.string;
a50b1753 5238 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5239 if (!shortname)
5240 goto error_free_vers;
4ad4eba5
AM
5241 memcpy (shortname, h->root.root.string, amt);
5242 shortname[amt] = '\0';
5243
5244 hi = (struct elf_link_hash_entry *)
66eb6687 5245 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5246 FALSE, FALSE, FALSE);
5247 if (hi != NULL
5248 && hi->root.type == h->root.type
5249 && hi->root.u.def.value == h->root.u.def.value
5250 && hi->root.u.def.section == h->root.u.def.section)
5251 {
5252 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5253 hi->root.type = bfd_link_hash_indirect;
5254 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5255 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5256 sym_hash = elf_sym_hashes (abfd);
5257 if (sym_hash)
5258 for (symidx = 0; symidx < extsymcount; ++symidx)
5259 if (sym_hash[symidx] == hi)
5260 {
5261 sym_hash[symidx] = h;
5262 break;
5263 }
5264 }
5265 free (shortname);
5266 }
5267 free (nondeflt_vers);
5268 nondeflt_vers = NULL;
5269 }
5270
60d67dc8 5271 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5272 symbols we found. The only way to do this is to search all the
5273 symbols. Since we only need the information for non functions in
5274 dynamic objects, that's the only time we actually put anything on
5275 the list WEAKS. We need this information so that if a regular
5276 object refers to a symbol defined weakly in a dynamic object, the
5277 real symbol in the dynamic object is also put in the dynamic
5278 symbols; we also must arrange for both symbols to point to the
5279 same memory location. We could handle the general case of symbol
5280 aliasing, but a general symbol alias can only be generated in
5281 assembler code, handling it correctly would be very time
5282 consuming, and other ELF linkers don't handle general aliasing
5283 either. */
5284 if (weaks != NULL)
5285 {
5286 struct elf_link_hash_entry **hpp;
5287 struct elf_link_hash_entry **hppend;
5288 struct elf_link_hash_entry **sorted_sym_hash;
5289 struct elf_link_hash_entry *h;
5290 size_t sym_count;
5291
5292 /* Since we have to search the whole symbol list for each weak
5293 defined symbol, search time for N weak defined symbols will be
5294 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5295 amt = extsymcount;
5296 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5297 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5298 if (sorted_sym_hash == NULL)
5299 goto error_return;
5300 sym_hash = sorted_sym_hash;
5301 hpp = elf_sym_hashes (abfd);
5302 hppend = hpp + extsymcount;
5303 sym_count = 0;
5304 for (; hpp < hppend; hpp++)
5305 {
5306 h = *hpp;
5307 if (h != NULL
5308 && h->root.type == bfd_link_hash_defined
fcb93ecf 5309 && !bed->is_function_type (h->type))
4ad4eba5
AM
5310 {
5311 *sym_hash = h;
5312 sym_hash++;
5313 sym_count++;
5314 }
5315 }
5316
5317 qsort (sorted_sym_hash, sym_count,
5318 sizeof (struct elf_link_hash_entry *),
5319 elf_sort_symbol);
5320
5321 while (weaks != NULL)
5322 {
5323 struct elf_link_hash_entry *hlook;
5324 asection *slook;
5325 bfd_vma vlook;
ed54588d 5326 size_t i, j, idx = 0;
4ad4eba5
AM
5327
5328 hlook = weaks;
60d67dc8
AM
5329 weaks = hlook->u.alias;
5330 hlook->u.alias = NULL;
4ad4eba5 5331
e3e53eed
AM
5332 if (hlook->root.type != bfd_link_hash_defined
5333 && hlook->root.type != bfd_link_hash_defweak)
5334 continue;
5335
4ad4eba5
AM
5336 slook = hlook->root.u.def.section;
5337 vlook = hlook->root.u.def.value;
5338
4ad4eba5
AM
5339 i = 0;
5340 j = sym_count;
14160578 5341 while (i != j)
4ad4eba5
AM
5342 {
5343 bfd_signed_vma vdiff;
5344 idx = (i + j) / 2;
14160578 5345 h = sorted_sym_hash[idx];
4ad4eba5
AM
5346 vdiff = vlook - h->root.u.def.value;
5347 if (vdiff < 0)
5348 j = idx;
5349 else if (vdiff > 0)
5350 i = idx + 1;
5351 else
5352 {
d3435ae8 5353 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5354 if (sdiff < 0)
5355 j = idx;
5356 else if (sdiff > 0)
5357 i = idx + 1;
5358 else
14160578 5359 break;
4ad4eba5
AM
5360 }
5361 }
5362
5363 /* We didn't find a value/section match. */
14160578 5364 if (i == j)
4ad4eba5
AM
5365 continue;
5366
14160578
AM
5367 /* With multiple aliases, or when the weak symbol is already
5368 strongly defined, we have multiple matching symbols and
5369 the binary search above may land on any of them. Step
5370 one past the matching symbol(s). */
5371 while (++idx != j)
5372 {
5373 h = sorted_sym_hash[idx];
5374 if (h->root.u.def.section != slook
5375 || h->root.u.def.value != vlook)
5376 break;
5377 }
5378
5379 /* Now look back over the aliases. Since we sorted by size
5380 as well as value and section, we'll choose the one with
5381 the largest size. */
5382 while (idx-- != i)
4ad4eba5 5383 {
14160578 5384 h = sorted_sym_hash[idx];
4ad4eba5
AM
5385
5386 /* Stop if value or section doesn't match. */
14160578
AM
5387 if (h->root.u.def.section != slook
5388 || h->root.u.def.value != vlook)
4ad4eba5
AM
5389 break;
5390 else if (h != hlook)
5391 {
60d67dc8
AM
5392 struct elf_link_hash_entry *t;
5393
5394 hlook->u.alias = h;
5395 hlook->is_weakalias = 1;
5396 t = h;
5397 if (t->u.alias != NULL)
5398 while (t->u.alias != h)
5399 t = t->u.alias;
5400 t->u.alias = hlook;
4ad4eba5
AM
5401
5402 /* If the weak definition is in the list of dynamic
5403 symbols, make sure the real definition is put
5404 there as well. */
5405 if (hlook->dynindx != -1 && h->dynindx == -1)
5406 {
c152c796 5407 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5408 {
5409 err_free_sym_hash:
5410 free (sorted_sym_hash);
5411 goto error_return;
5412 }
4ad4eba5
AM
5413 }
5414
5415 /* If the real definition is in the list of dynamic
5416 symbols, make sure the weak definition is put
5417 there as well. If we don't do this, then the
5418 dynamic loader might not merge the entries for the
5419 real definition and the weak definition. */
5420 if (h->dynindx != -1 && hlook->dynindx == -1)
5421 {
c152c796 5422 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5423 goto err_free_sym_hash;
4ad4eba5
AM
5424 }
5425 break;
5426 }
5427 }
5428 }
5429
5430 free (sorted_sym_hash);
5431 }
5432
33177bb1
AM
5433 if (bed->check_directives
5434 && !(*bed->check_directives) (abfd, info))
5435 return FALSE;
85fbca6a 5436
4ad4eba5
AM
5437 /* If this is a non-traditional link, try to optimize the handling
5438 of the .stab/.stabstr sections. */
5439 if (! dynamic
5440 && ! info->traditional_format
66eb6687 5441 && is_elf_hash_table (htab)
4ad4eba5
AM
5442 && (info->strip != strip_all && info->strip != strip_debugger))
5443 {
5444 asection *stabstr;
5445
5446 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5447 if (stabstr != NULL)
5448 {
5449 bfd_size_type string_offset = 0;
5450 asection *stab;
5451
5452 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5453 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5454 && (!stab->name[5] ||
5455 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5456 && (stab->flags & SEC_MERGE) == 0
5457 && !bfd_is_abs_section (stab->output_section))
5458 {
5459 struct bfd_elf_section_data *secdata;
5460
5461 secdata = elf_section_data (stab);
66eb6687
AM
5462 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5463 stabstr, &secdata->sec_info,
4ad4eba5
AM
5464 &string_offset))
5465 goto error_return;
5466 if (secdata->sec_info)
dbaa2011 5467 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5468 }
5469 }
5470 }
5471
66eb6687 5472 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5473 {
5474 /* Add this bfd to the loaded list. */
5475 struct elf_link_loaded_list *n;
5476
ca4be51c 5477 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5478 if (n == NULL)
5479 goto error_return;
5480 n->abfd = abfd;
66eb6687
AM
5481 n->next = htab->loaded;
5482 htab->loaded = n;
4ad4eba5
AM
5483 }
5484
5485 return TRUE;
5486
5487 error_free_vers:
66eb6687
AM
5488 if (old_tab != NULL)
5489 free (old_tab);
5b677558
AM
5490 if (old_strtab != NULL)
5491 free (old_strtab);
4ad4eba5
AM
5492 if (nondeflt_vers != NULL)
5493 free (nondeflt_vers);
5494 if (extversym != NULL)
5495 free (extversym);
5496 error_free_sym:
5497 if (isymbuf != NULL)
5498 free (isymbuf);
5499 error_return:
5500 return FALSE;
5501}
5502
8387904d
AM
5503/* Return the linker hash table entry of a symbol that might be
5504 satisfied by an archive symbol. Return -1 on error. */
5505
5506struct elf_link_hash_entry *
5507_bfd_elf_archive_symbol_lookup (bfd *abfd,
5508 struct bfd_link_info *info,
5509 const char *name)
5510{
5511 struct elf_link_hash_entry *h;
5512 char *p, *copy;
5513 size_t len, first;
5514
2a41f396 5515 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5516 if (h != NULL)
5517 return h;
5518
5519 /* If this is a default version (the name contains @@), look up the
5520 symbol again with only one `@' as well as without the version.
5521 The effect is that references to the symbol with and without the
5522 version will be matched by the default symbol in the archive. */
5523
5524 p = strchr (name, ELF_VER_CHR);
5525 if (p == NULL || p[1] != ELF_VER_CHR)
5526 return h;
5527
5528 /* First check with only one `@'. */
5529 len = strlen (name);
a50b1753 5530 copy = (char *) bfd_alloc (abfd, len);
8387904d 5531 if (copy == NULL)
e99955cd 5532 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5533
5534 first = p - name + 1;
5535 memcpy (copy, name, first);
5536 memcpy (copy + first, name + first + 1, len - first);
5537
2a41f396 5538 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5539 if (h == NULL)
5540 {
5541 /* We also need to check references to the symbol without the
5542 version. */
5543 copy[first - 1] = '\0';
5544 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5545 FALSE, FALSE, TRUE);
8387904d
AM
5546 }
5547
5548 bfd_release (abfd, copy);
5549 return h;
5550}
5551
0ad989f9 5552/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5553 don't use _bfd_generic_link_add_archive_symbols because we need to
5554 handle versioned symbols.
0ad989f9
L
5555
5556 Fortunately, ELF archive handling is simpler than that done by
5557 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5558 oddities. In ELF, if we find a symbol in the archive map, and the
5559 symbol is currently undefined, we know that we must pull in that
5560 object file.
5561
5562 Unfortunately, we do have to make multiple passes over the symbol
5563 table until nothing further is resolved. */
5564
4ad4eba5
AM
5565static bfd_boolean
5566elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5567{
5568 symindex c;
13e570f8 5569 unsigned char *included = NULL;
0ad989f9
L
5570 carsym *symdefs;
5571 bfd_boolean loop;
5572 bfd_size_type amt;
8387904d
AM
5573 const struct elf_backend_data *bed;
5574 struct elf_link_hash_entry * (*archive_symbol_lookup)
5575 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5576
5577 if (! bfd_has_map (abfd))
5578 {
5579 /* An empty archive is a special case. */
5580 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5581 return TRUE;
5582 bfd_set_error (bfd_error_no_armap);
5583 return FALSE;
5584 }
5585
5586 /* Keep track of all symbols we know to be already defined, and all
5587 files we know to be already included. This is to speed up the
5588 second and subsequent passes. */
5589 c = bfd_ardata (abfd)->symdef_count;
5590 if (c == 0)
5591 return TRUE;
5592 amt = c;
13e570f8
AM
5593 amt *= sizeof (*included);
5594 included = (unsigned char *) bfd_zmalloc (amt);
5595 if (included == NULL)
5596 return FALSE;
0ad989f9
L
5597
5598 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5599 bed = get_elf_backend_data (abfd);
5600 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5601
5602 do
5603 {
5604 file_ptr last;
5605 symindex i;
5606 carsym *symdef;
5607 carsym *symdefend;
5608
5609 loop = FALSE;
5610 last = -1;
5611
5612 symdef = symdefs;
5613 symdefend = symdef + c;
5614 for (i = 0; symdef < symdefend; symdef++, i++)
5615 {
5616 struct elf_link_hash_entry *h;
5617 bfd *element;
5618 struct bfd_link_hash_entry *undefs_tail;
5619 symindex mark;
5620
13e570f8 5621 if (included[i])
0ad989f9
L
5622 continue;
5623 if (symdef->file_offset == last)
5624 {
5625 included[i] = TRUE;
5626 continue;
5627 }
5628
8387904d 5629 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5630 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5631 goto error_return;
0ad989f9
L
5632
5633 if (h == NULL)
5634 continue;
5635
5636 if (h->root.type == bfd_link_hash_common)
5637 {
5638 /* We currently have a common symbol. The archive map contains
5639 a reference to this symbol, so we may want to include it. We
5640 only want to include it however, if this archive element
5641 contains a definition of the symbol, not just another common
5642 declaration of it.
5643
5644 Unfortunately some archivers (including GNU ar) will put
5645 declarations of common symbols into their archive maps, as
5646 well as real definitions, so we cannot just go by the archive
5647 map alone. Instead we must read in the element's symbol
5648 table and check that to see what kind of symbol definition
5649 this is. */
5650 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5651 continue;
5652 }
5653 else if (h->root.type != bfd_link_hash_undefined)
5654 {
5655 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5656 /* Symbol must be defined. Don't check it again. */
5657 included[i] = TRUE;
0ad989f9
L
5658 continue;
5659 }
5660
5661 /* We need to include this archive member. */
5662 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5663 if (element == NULL)
5664 goto error_return;
5665
5666 if (! bfd_check_format (element, bfd_object))
5667 goto error_return;
5668
0ad989f9
L
5669 undefs_tail = info->hash->undefs_tail;
5670
0e144ba7
AM
5671 if (!(*info->callbacks
5672 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5673 continue;
0e144ba7 5674 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5675 goto error_return;
5676
5677 /* If there are any new undefined symbols, we need to make
5678 another pass through the archive in order to see whether
5679 they can be defined. FIXME: This isn't perfect, because
5680 common symbols wind up on undefs_tail and because an
5681 undefined symbol which is defined later on in this pass
5682 does not require another pass. This isn't a bug, but it
5683 does make the code less efficient than it could be. */
5684 if (undefs_tail != info->hash->undefs_tail)
5685 loop = TRUE;
5686
5687 /* Look backward to mark all symbols from this object file
5688 which we have already seen in this pass. */
5689 mark = i;
5690 do
5691 {
5692 included[mark] = TRUE;
5693 if (mark == 0)
5694 break;
5695 --mark;
5696 }
5697 while (symdefs[mark].file_offset == symdef->file_offset);
5698
5699 /* We mark subsequent symbols from this object file as we go
5700 on through the loop. */
5701 last = symdef->file_offset;
5702 }
5703 }
5704 while (loop);
5705
0ad989f9
L
5706 free (included);
5707
5708 return TRUE;
5709
5710 error_return:
0ad989f9
L
5711 if (included != NULL)
5712 free (included);
5713 return FALSE;
5714}
4ad4eba5
AM
5715
5716/* Given an ELF BFD, add symbols to the global hash table as
5717 appropriate. */
5718
5719bfd_boolean
5720bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5721{
5722 switch (bfd_get_format (abfd))
5723 {
5724 case bfd_object:
5725 return elf_link_add_object_symbols (abfd, info);
5726 case bfd_archive:
5727 return elf_link_add_archive_symbols (abfd, info);
5728 default:
5729 bfd_set_error (bfd_error_wrong_format);
5730 return FALSE;
5731 }
5732}
5a580b3a 5733\f
14b1c01e
AM
5734struct hash_codes_info
5735{
5736 unsigned long *hashcodes;
5737 bfd_boolean error;
5738};
a0c8462f 5739
5a580b3a
AM
5740/* This function will be called though elf_link_hash_traverse to store
5741 all hash value of the exported symbols in an array. */
5742
5743static bfd_boolean
5744elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5745{
a50b1753 5746 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5747 const char *name;
5a580b3a
AM
5748 unsigned long ha;
5749 char *alc = NULL;
5750
5a580b3a
AM
5751 /* Ignore indirect symbols. These are added by the versioning code. */
5752 if (h->dynindx == -1)
5753 return TRUE;
5754
5755 name = h->root.root.string;
422f1182 5756 if (h->versioned >= versioned)
5a580b3a 5757 {
422f1182
L
5758 char *p = strchr (name, ELF_VER_CHR);
5759 if (p != NULL)
14b1c01e 5760 {
422f1182
L
5761 alc = (char *) bfd_malloc (p - name + 1);
5762 if (alc == NULL)
5763 {
5764 inf->error = TRUE;
5765 return FALSE;
5766 }
5767 memcpy (alc, name, p - name);
5768 alc[p - name] = '\0';
5769 name = alc;
14b1c01e 5770 }
5a580b3a
AM
5771 }
5772
5773 /* Compute the hash value. */
5774 ha = bfd_elf_hash (name);
5775
5776 /* Store the found hash value in the array given as the argument. */
14b1c01e 5777 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5778
5779 /* And store it in the struct so that we can put it in the hash table
5780 later. */
f6e332e6 5781 h->u.elf_hash_value = ha;
5a580b3a
AM
5782
5783 if (alc != NULL)
5784 free (alc);
5785
5786 return TRUE;
5787}
5788
fdc90cb4
JJ
5789struct collect_gnu_hash_codes
5790{
5791 bfd *output_bfd;
5792 const struct elf_backend_data *bed;
5793 unsigned long int nsyms;
5794 unsigned long int maskbits;
5795 unsigned long int *hashcodes;
5796 unsigned long int *hashval;
5797 unsigned long int *indx;
5798 unsigned long int *counts;
5799 bfd_vma *bitmask;
5800 bfd_byte *contents;
5801 long int min_dynindx;
5802 unsigned long int bucketcount;
5803 unsigned long int symindx;
5804 long int local_indx;
5805 long int shift1, shift2;
5806 unsigned long int mask;
14b1c01e 5807 bfd_boolean error;
fdc90cb4
JJ
5808};
5809
5810/* This function will be called though elf_link_hash_traverse to store
5811 all hash value of the exported symbols in an array. */
5812
5813static bfd_boolean
5814elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5815{
a50b1753 5816 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5817 const char *name;
fdc90cb4
JJ
5818 unsigned long ha;
5819 char *alc = NULL;
5820
fdc90cb4
JJ
5821 /* Ignore indirect symbols. These are added by the versioning code. */
5822 if (h->dynindx == -1)
5823 return TRUE;
5824
5825 /* Ignore also local symbols and undefined symbols. */
5826 if (! (*s->bed->elf_hash_symbol) (h))
5827 return TRUE;
5828
5829 name = h->root.root.string;
422f1182 5830 if (h->versioned >= versioned)
fdc90cb4 5831 {
422f1182
L
5832 char *p = strchr (name, ELF_VER_CHR);
5833 if (p != NULL)
14b1c01e 5834 {
422f1182
L
5835 alc = (char *) bfd_malloc (p - name + 1);
5836 if (alc == NULL)
5837 {
5838 s->error = TRUE;
5839 return FALSE;
5840 }
5841 memcpy (alc, name, p - name);
5842 alc[p - name] = '\0';
5843 name = alc;
14b1c01e 5844 }
fdc90cb4
JJ
5845 }
5846
5847 /* Compute the hash value. */
5848 ha = bfd_elf_gnu_hash (name);
5849
5850 /* Store the found hash value in the array for compute_bucket_count,
5851 and also for .dynsym reordering purposes. */
5852 s->hashcodes[s->nsyms] = ha;
5853 s->hashval[h->dynindx] = ha;
5854 ++s->nsyms;
5855 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5856 s->min_dynindx = h->dynindx;
5857
5858 if (alc != NULL)
5859 free (alc);
5860
5861 return TRUE;
5862}
5863
5864/* This function will be called though elf_link_hash_traverse to do
5865 final dynaminc symbol renumbering. */
5866
5867static bfd_boolean
5868elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5869{
a50b1753 5870 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5871 unsigned long int bucket;
5872 unsigned long int val;
5873
fdc90cb4
JJ
5874 /* Ignore indirect symbols. */
5875 if (h->dynindx == -1)
5876 return TRUE;
5877
5878 /* Ignore also local symbols and undefined symbols. */
5879 if (! (*s->bed->elf_hash_symbol) (h))
5880 {
5881 if (h->dynindx >= s->min_dynindx)
5882 h->dynindx = s->local_indx++;
5883 return TRUE;
5884 }
5885
5886 bucket = s->hashval[h->dynindx] % s->bucketcount;
5887 val = (s->hashval[h->dynindx] >> s->shift1)
5888 & ((s->maskbits >> s->shift1) - 1);
5889 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5890 s->bitmask[val]
5891 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5892 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5893 if (s->counts[bucket] == 1)
5894 /* Last element terminates the chain. */
5895 val |= 1;
5896 bfd_put_32 (s->output_bfd, val,
5897 s->contents + (s->indx[bucket] - s->symindx) * 4);
5898 --s->counts[bucket];
5899 h->dynindx = s->indx[bucket]++;
5900 return TRUE;
5901}
5902
5903/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5904
5905bfd_boolean
5906_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5907{
5908 return !(h->forced_local
5909 || h->root.type == bfd_link_hash_undefined
5910 || h->root.type == bfd_link_hash_undefweak
5911 || ((h->root.type == bfd_link_hash_defined
5912 || h->root.type == bfd_link_hash_defweak)
5913 && h->root.u.def.section->output_section == NULL));
5914}
5915
5a580b3a
AM
5916/* Array used to determine the number of hash table buckets to use
5917 based on the number of symbols there are. If there are fewer than
5918 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5919 fewer than 37 we use 17 buckets, and so forth. We never use more
5920 than 32771 buckets. */
5921
5922static const size_t elf_buckets[] =
5923{
5924 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5925 16411, 32771, 0
5926};
5927
5928/* Compute bucket count for hashing table. We do not use a static set
5929 of possible tables sizes anymore. Instead we determine for all
5930 possible reasonable sizes of the table the outcome (i.e., the
5931 number of collisions etc) and choose the best solution. The
5932 weighting functions are not too simple to allow the table to grow
5933 without bounds. Instead one of the weighting factors is the size.
5934 Therefore the result is always a good payoff between few collisions
5935 (= short chain lengths) and table size. */
5936static size_t
b20dd2ce 5937compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5938 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5939 unsigned long int nsyms,
5940 int gnu_hash)
5a580b3a 5941{
5a580b3a 5942 size_t best_size = 0;
5a580b3a 5943 unsigned long int i;
5a580b3a 5944
5a580b3a
AM
5945 /* We have a problem here. The following code to optimize the table
5946 size requires an integer type with more the 32 bits. If
5947 BFD_HOST_U_64_BIT is set we know about such a type. */
5948#ifdef BFD_HOST_U_64_BIT
5949 if (info->optimize)
5950 {
5a580b3a
AM
5951 size_t minsize;
5952 size_t maxsize;
5953 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5954 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5955 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5956 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5957 unsigned long int *counts;
d40f3da9 5958 bfd_size_type amt;
0883b6e0 5959 unsigned int no_improvement_count = 0;
5a580b3a
AM
5960
5961 /* Possible optimization parameters: if we have NSYMS symbols we say
5962 that the hashing table must at least have NSYMS/4 and at most
5963 2*NSYMS buckets. */
5964 minsize = nsyms / 4;
5965 if (minsize == 0)
5966 minsize = 1;
5967 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5968 if (gnu_hash)
5969 {
5970 if (minsize < 2)
5971 minsize = 2;
5972 if ((best_size & 31) == 0)
5973 ++best_size;
5974 }
5a580b3a
AM
5975
5976 /* Create array where we count the collisions in. We must use bfd_malloc
5977 since the size could be large. */
5978 amt = maxsize;
5979 amt *= sizeof (unsigned long int);
a50b1753 5980 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5981 if (counts == NULL)
fdc90cb4 5982 return 0;
5a580b3a
AM
5983
5984 /* Compute the "optimal" size for the hash table. The criteria is a
5985 minimal chain length. The minor criteria is (of course) the size
5986 of the table. */
5987 for (i = minsize; i < maxsize; ++i)
5988 {
5989 /* Walk through the array of hashcodes and count the collisions. */
5990 BFD_HOST_U_64_BIT max;
5991 unsigned long int j;
5992 unsigned long int fact;
5993
fdc90cb4
JJ
5994 if (gnu_hash && (i & 31) == 0)
5995 continue;
5996
5a580b3a
AM
5997 memset (counts, '\0', i * sizeof (unsigned long int));
5998
5999 /* Determine how often each hash bucket is used. */
6000 for (j = 0; j < nsyms; ++j)
6001 ++counts[hashcodes[j] % i];
6002
6003 /* For the weight function we need some information about the
6004 pagesize on the target. This is information need not be 100%
6005 accurate. Since this information is not available (so far) we
6006 define it here to a reasonable default value. If it is crucial
6007 to have a better value some day simply define this value. */
6008# ifndef BFD_TARGET_PAGESIZE
6009# define BFD_TARGET_PAGESIZE (4096)
6010# endif
6011
fdc90cb4
JJ
6012 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6013 and the chains. */
6014 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6015
6016# if 1
6017 /* Variant 1: optimize for short chains. We add the squares
6018 of all the chain lengths (which favors many small chain
6019 over a few long chains). */
6020 for (j = 0; j < i; ++j)
6021 max += counts[j] * counts[j];
6022
6023 /* This adds penalties for the overall size of the table. */
fdc90cb4 6024 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6025 max *= fact * fact;
6026# else
6027 /* Variant 2: Optimize a lot more for small table. Here we
6028 also add squares of the size but we also add penalties for
6029 empty slots (the +1 term). */
6030 for (j = 0; j < i; ++j)
6031 max += (1 + counts[j]) * (1 + counts[j]);
6032
6033 /* The overall size of the table is considered, but not as
6034 strong as in variant 1, where it is squared. */
fdc90cb4 6035 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6036 max *= fact;
6037# endif
6038
6039 /* Compare with current best results. */
6040 if (max < best_chlen)
6041 {
6042 best_chlen = max;
6043 best_size = i;
ca4be51c 6044 no_improvement_count = 0;
5a580b3a 6045 }
0883b6e0
NC
6046 /* PR 11843: Avoid futile long searches for the best bucket size
6047 when there are a large number of symbols. */
6048 else if (++no_improvement_count == 100)
6049 break;
5a580b3a
AM
6050 }
6051
6052 free (counts);
6053 }
6054 else
6055#endif /* defined (BFD_HOST_U_64_BIT) */
6056 {
6057 /* This is the fallback solution if no 64bit type is available or if we
6058 are not supposed to spend much time on optimizations. We select the
6059 bucket count using a fixed set of numbers. */
6060 for (i = 0; elf_buckets[i] != 0; i++)
6061 {
6062 best_size = elf_buckets[i];
fdc90cb4 6063 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6064 break;
6065 }
fdc90cb4
JJ
6066 if (gnu_hash && best_size < 2)
6067 best_size = 2;
5a580b3a
AM
6068 }
6069
5a580b3a
AM
6070 return best_size;
6071}
6072
d0bf826b
AM
6073/* Size any SHT_GROUP section for ld -r. */
6074
6075bfd_boolean
6076_bfd_elf_size_group_sections (struct bfd_link_info *info)
6077{
6078 bfd *ibfd;
57963c05 6079 asection *s;
d0bf826b 6080
c72f2fb2 6081 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6082 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6083 && (s = ibfd->sections) != NULL
6084 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6085 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6086 return FALSE;
6087 return TRUE;
6088}
6089
04c3a755
NS
6090/* Set a default stack segment size. The value in INFO wins. If it
6091 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6092 undefined it is initialized. */
6093
6094bfd_boolean
6095bfd_elf_stack_segment_size (bfd *output_bfd,
6096 struct bfd_link_info *info,
6097 const char *legacy_symbol,
6098 bfd_vma default_size)
6099{
6100 struct elf_link_hash_entry *h = NULL;
6101
6102 /* Look for legacy symbol. */
6103 if (legacy_symbol)
6104 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6105 FALSE, FALSE, FALSE);
6106 if (h && (h->root.type == bfd_link_hash_defined
6107 || h->root.type == bfd_link_hash_defweak)
6108 && h->def_regular
6109 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6110 {
6111 /* The symbol has no type if specified on the command line. */
6112 h->type = STT_OBJECT;
6113 if (info->stacksize)
695344c0 6114 /* xgettext:c-format */
871b3ab2 6115 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6116 output_bfd, legacy_symbol);
04c3a755 6117 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6118 /* xgettext:c-format */
871b3ab2 6119 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6120 output_bfd, legacy_symbol);
04c3a755
NS
6121 else
6122 info->stacksize = h->root.u.def.value;
6123 }
6124
6125 if (!info->stacksize)
6126 /* If the user didn't set a size, or explicitly inhibit the
6127 size, set it now. */
6128 info->stacksize = default_size;
6129
6130 /* Provide the legacy symbol, if it is referenced. */
6131 if (h && (h->root.type == bfd_link_hash_undefined
6132 || h->root.type == bfd_link_hash_undefweak))
6133 {
6134 struct bfd_link_hash_entry *bh = NULL;
6135
6136 if (!(_bfd_generic_link_add_one_symbol
6137 (info, output_bfd, legacy_symbol,
6138 BSF_GLOBAL, bfd_abs_section_ptr,
6139 info->stacksize >= 0 ? info->stacksize : 0,
6140 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6141 return FALSE;
6142
6143 h = (struct elf_link_hash_entry *) bh;
6144 h->def_regular = 1;
6145 h->type = STT_OBJECT;
6146 }
6147
6148 return TRUE;
6149}
6150
b531344c
MR
6151/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6152
6153struct elf_gc_sweep_symbol_info
6154{
6155 struct bfd_link_info *info;
6156 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6157 bfd_boolean);
6158};
6159
6160static bfd_boolean
6161elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6162{
6163 if (!h->mark
6164 && (((h->root.type == bfd_link_hash_defined
6165 || h->root.type == bfd_link_hash_defweak)
6166 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6167 && h->root.u.def.section->gc_mark))
6168 || h->root.type == bfd_link_hash_undefined
6169 || h->root.type == bfd_link_hash_undefweak))
6170 {
6171 struct elf_gc_sweep_symbol_info *inf;
6172
6173 inf = (struct elf_gc_sweep_symbol_info *) data;
6174 (*inf->hide_symbol) (inf->info, h, TRUE);
6175 h->def_regular = 0;
6176 h->ref_regular = 0;
6177 h->ref_regular_nonweak = 0;
6178 }
6179
6180 return TRUE;
6181}
6182
5a580b3a
AM
6183/* Set up the sizes and contents of the ELF dynamic sections. This is
6184 called by the ELF linker emulation before_allocation routine. We
6185 must set the sizes of the sections before the linker sets the
6186 addresses of the various sections. */
6187
6188bfd_boolean
6189bfd_elf_size_dynamic_sections (bfd *output_bfd,
6190 const char *soname,
6191 const char *rpath,
6192 const char *filter_shlib,
7ee314fa
AM
6193 const char *audit,
6194 const char *depaudit,
5a580b3a
AM
6195 const char * const *auxiliary_filters,
6196 struct bfd_link_info *info,
fd91d419 6197 asection **sinterpptr)
5a580b3a 6198{
5a580b3a
AM
6199 bfd *dynobj;
6200 const struct elf_backend_data *bed;
5a580b3a
AM
6201
6202 *sinterpptr = NULL;
6203
5a580b3a
AM
6204 if (!is_elf_hash_table (info->hash))
6205 return TRUE;
6206
5a580b3a
AM
6207 dynobj = elf_hash_table (info)->dynobj;
6208
9a2a56cc 6209 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6210 {
902e9fc7
MR
6211 struct bfd_elf_version_tree *verdefs;
6212 struct elf_info_failed asvinfo;
5a580b3a
AM
6213 struct bfd_elf_version_tree *t;
6214 struct bfd_elf_version_expr *d;
902e9fc7 6215 asection *s;
e6699019 6216 size_t soname_indx;
7ee314fa 6217
5a580b3a
AM
6218 /* If we are supposed to export all symbols into the dynamic symbol
6219 table (this is not the normal case), then do so. */
55255dae 6220 if (info->export_dynamic
0e1862bb 6221 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6222 {
3d13f3e9
AM
6223 struct elf_info_failed eif;
6224
6225 eif.info = info;
6226 eif.failed = FALSE;
5a580b3a
AM
6227 elf_link_hash_traverse (elf_hash_table (info),
6228 _bfd_elf_export_symbol,
6229 &eif);
6230 if (eif.failed)
6231 return FALSE;
6232 }
6233
e6699019
L
6234 if (soname != NULL)
6235 {
6236 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6237 soname, TRUE);
6238 if (soname_indx == (size_t) -1
6239 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6240 return FALSE;
6241 }
6242 else
6243 soname_indx = (size_t) -1;
6244
5a580b3a 6245 /* Make all global versions with definition. */
fd91d419 6246 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6247 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6248 if (!d->symver && d->literal)
5a580b3a
AM
6249 {
6250 const char *verstr, *name;
6251 size_t namelen, verlen, newlen;
93252b1c 6252 char *newname, *p, leading_char;
5a580b3a
AM
6253 struct elf_link_hash_entry *newh;
6254
93252b1c 6255 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6256 name = d->pattern;
93252b1c 6257 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6258 verstr = t->name;
6259 verlen = strlen (verstr);
6260 newlen = namelen + verlen + 3;
6261
a50b1753 6262 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6263 if (newname == NULL)
6264 return FALSE;
93252b1c
MF
6265 newname[0] = leading_char;
6266 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6267
6268 /* Check the hidden versioned definition. */
6269 p = newname + namelen;
6270 *p++ = ELF_VER_CHR;
6271 memcpy (p, verstr, verlen + 1);
6272 newh = elf_link_hash_lookup (elf_hash_table (info),
6273 newname, FALSE, FALSE,
6274 FALSE);
6275 if (newh == NULL
6276 || (newh->root.type != bfd_link_hash_defined
6277 && newh->root.type != bfd_link_hash_defweak))
6278 {
6279 /* Check the default versioned definition. */
6280 *p++ = ELF_VER_CHR;
6281 memcpy (p, verstr, verlen + 1);
6282 newh = elf_link_hash_lookup (elf_hash_table (info),
6283 newname, FALSE, FALSE,
6284 FALSE);
6285 }
6286 free (newname);
6287
6288 /* Mark this version if there is a definition and it is
6289 not defined in a shared object. */
6290 if (newh != NULL
f5385ebf 6291 && !newh->def_dynamic
5a580b3a
AM
6292 && (newh->root.type == bfd_link_hash_defined
6293 || newh->root.type == bfd_link_hash_defweak))
6294 d->symver = 1;
6295 }
6296
6297 /* Attach all the symbols to their version information. */
5a580b3a 6298 asvinfo.info = info;
5a580b3a
AM
6299 asvinfo.failed = FALSE;
6300
6301 elf_link_hash_traverse (elf_hash_table (info),
6302 _bfd_elf_link_assign_sym_version,
6303 &asvinfo);
6304 if (asvinfo.failed)
6305 return FALSE;
6306
6307 if (!info->allow_undefined_version)
6308 {
6309 /* Check if all global versions have a definition. */
3d13f3e9 6310 bfd_boolean all_defined = TRUE;
fd91d419 6311 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6312 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6313 if (d->literal && !d->symver && !d->script)
5a580b3a 6314 {
4eca0228 6315 _bfd_error_handler
5a580b3a
AM
6316 (_("%s: undefined version: %s"),
6317 d->pattern, t->name);
6318 all_defined = FALSE;
6319 }
6320
6321 if (!all_defined)
6322 {
6323 bfd_set_error (bfd_error_bad_value);
6324 return FALSE;
6325 }
6326 }
6327
902e9fc7
MR
6328 /* Set up the version definition section. */
6329 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6330 BFD_ASSERT (s != NULL);
5a580b3a 6331
902e9fc7
MR
6332 /* We may have created additional version definitions if we are
6333 just linking a regular application. */
6334 verdefs = info->version_info;
5a580b3a 6335
902e9fc7
MR
6336 /* Skip anonymous version tag. */
6337 if (verdefs != NULL && verdefs->vernum == 0)
6338 verdefs = verdefs->next;
5a580b3a 6339
902e9fc7
MR
6340 if (verdefs == NULL && !info->create_default_symver)
6341 s->flags |= SEC_EXCLUDE;
6342 else
5a580b3a 6343 {
902e9fc7
MR
6344 unsigned int cdefs;
6345 bfd_size_type size;
6346 bfd_byte *p;
6347 Elf_Internal_Verdef def;
6348 Elf_Internal_Verdaux defaux;
6349 struct bfd_link_hash_entry *bh;
6350 struct elf_link_hash_entry *h;
6351 const char *name;
5a580b3a 6352
902e9fc7
MR
6353 cdefs = 0;
6354 size = 0;
5a580b3a 6355
902e9fc7
MR
6356 /* Make space for the base version. */
6357 size += sizeof (Elf_External_Verdef);
6358 size += sizeof (Elf_External_Verdaux);
6359 ++cdefs;
6360
6361 /* Make space for the default version. */
6362 if (info->create_default_symver)
6363 {
6364 size += sizeof (Elf_External_Verdef);
6365 ++cdefs;
3e3b46e5
PB
6366 }
6367
5a580b3a
AM
6368 for (t = verdefs; t != NULL; t = t->next)
6369 {
6370 struct bfd_elf_version_deps *n;
6371
a6cc6b3b
RO
6372 /* Don't emit base version twice. */
6373 if (t->vernum == 0)
6374 continue;
6375
5a580b3a
AM
6376 size += sizeof (Elf_External_Verdef);
6377 size += sizeof (Elf_External_Verdaux);
6378 ++cdefs;
6379
6380 for (n = t->deps; n != NULL; n = n->next)
6381 size += sizeof (Elf_External_Verdaux);
6382 }
6383
eea6121a 6384 s->size = size;
a50b1753 6385 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6386 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6387 return FALSE;
6388
6389 /* Fill in the version definition section. */
6390
6391 p = s->contents;
6392
6393 def.vd_version = VER_DEF_CURRENT;
6394 def.vd_flags = VER_FLG_BASE;
6395 def.vd_ndx = 1;
6396 def.vd_cnt = 1;
3e3b46e5
PB
6397 if (info->create_default_symver)
6398 {
6399 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6400 def.vd_next = sizeof (Elf_External_Verdef);
6401 }
6402 else
6403 {
6404 def.vd_aux = sizeof (Elf_External_Verdef);
6405 def.vd_next = (sizeof (Elf_External_Verdef)
6406 + sizeof (Elf_External_Verdaux));
6407 }
5a580b3a 6408
ef53be89 6409 if (soname_indx != (size_t) -1)
5a580b3a
AM
6410 {
6411 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6412 soname_indx);
6413 def.vd_hash = bfd_elf_hash (soname);
6414 defaux.vda_name = soname_indx;
3e3b46e5 6415 name = soname;
5a580b3a
AM
6416 }
6417 else
6418 {
ef53be89 6419 size_t indx;
5a580b3a 6420
06084812 6421 name = lbasename (output_bfd->filename);
5a580b3a
AM
6422 def.vd_hash = bfd_elf_hash (name);
6423 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6424 name, FALSE);
ef53be89 6425 if (indx == (size_t) -1)
5a580b3a
AM
6426 return FALSE;
6427 defaux.vda_name = indx;
6428 }
6429 defaux.vda_next = 0;
6430
6431 _bfd_elf_swap_verdef_out (output_bfd, &def,
6432 (Elf_External_Verdef *) p);
6433 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6434 if (info->create_default_symver)
6435 {
6436 /* Add a symbol representing this version. */
6437 bh = NULL;
6438 if (! (_bfd_generic_link_add_one_symbol
6439 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6440 0, NULL, FALSE,
6441 get_elf_backend_data (dynobj)->collect, &bh)))
6442 return FALSE;
6443 h = (struct elf_link_hash_entry *) bh;
6444 h->non_elf = 0;
6445 h->def_regular = 1;
6446 h->type = STT_OBJECT;
6447 h->verinfo.vertree = NULL;
6448
6449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6450 return FALSE;
6451
6452 /* Create a duplicate of the base version with the same
6453 aux block, but different flags. */
6454 def.vd_flags = 0;
6455 def.vd_ndx = 2;
6456 def.vd_aux = sizeof (Elf_External_Verdef);
6457 if (verdefs)
6458 def.vd_next = (sizeof (Elf_External_Verdef)
6459 + sizeof (Elf_External_Verdaux));
6460 else
6461 def.vd_next = 0;
6462 _bfd_elf_swap_verdef_out (output_bfd, &def,
6463 (Elf_External_Verdef *) p);
6464 p += sizeof (Elf_External_Verdef);
6465 }
5a580b3a
AM
6466 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6467 (Elf_External_Verdaux *) p);
6468 p += sizeof (Elf_External_Verdaux);
6469
6470 for (t = verdefs; t != NULL; t = t->next)
6471 {
6472 unsigned int cdeps;
6473 struct bfd_elf_version_deps *n;
5a580b3a 6474
a6cc6b3b
RO
6475 /* Don't emit the base version twice. */
6476 if (t->vernum == 0)
6477 continue;
6478
5a580b3a
AM
6479 cdeps = 0;
6480 for (n = t->deps; n != NULL; n = n->next)
6481 ++cdeps;
6482
6483 /* Add a symbol representing this version. */
6484 bh = NULL;
6485 if (! (_bfd_generic_link_add_one_symbol
6486 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6487 0, NULL, FALSE,
6488 get_elf_backend_data (dynobj)->collect, &bh)))
6489 return FALSE;
6490 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6491 h->non_elf = 0;
6492 h->def_regular = 1;
5a580b3a
AM
6493 h->type = STT_OBJECT;
6494 h->verinfo.vertree = t;
6495
c152c796 6496 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6497 return FALSE;
6498
6499 def.vd_version = VER_DEF_CURRENT;
6500 def.vd_flags = 0;
6501 if (t->globals.list == NULL
6502 && t->locals.list == NULL
6503 && ! t->used)
6504 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6505 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6506 def.vd_cnt = cdeps + 1;
6507 def.vd_hash = bfd_elf_hash (t->name);
6508 def.vd_aux = sizeof (Elf_External_Verdef);
6509 def.vd_next = 0;
a6cc6b3b
RO
6510
6511 /* If a basever node is next, it *must* be the last node in
6512 the chain, otherwise Verdef construction breaks. */
6513 if (t->next != NULL && t->next->vernum == 0)
6514 BFD_ASSERT (t->next->next == NULL);
6515
6516 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6517 def.vd_next = (sizeof (Elf_External_Verdef)
6518 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6519
6520 _bfd_elf_swap_verdef_out (output_bfd, &def,
6521 (Elf_External_Verdef *) p);
6522 p += sizeof (Elf_External_Verdef);
6523
6524 defaux.vda_name = h->dynstr_index;
6525 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6526 h->dynstr_index);
6527 defaux.vda_next = 0;
6528 if (t->deps != NULL)
6529 defaux.vda_next = sizeof (Elf_External_Verdaux);
6530 t->name_indx = defaux.vda_name;
6531
6532 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6533 (Elf_External_Verdaux *) p);
6534 p += sizeof (Elf_External_Verdaux);
6535
6536 for (n = t->deps; n != NULL; n = n->next)
6537 {
6538 if (n->version_needed == NULL)
6539 {
6540 /* This can happen if there was an error in the
6541 version script. */
6542 defaux.vda_name = 0;
6543 }
6544 else
6545 {
6546 defaux.vda_name = n->version_needed->name_indx;
6547 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6548 defaux.vda_name);
6549 }
6550 if (n->next == NULL)
6551 defaux.vda_next = 0;
6552 else
6553 defaux.vda_next = sizeof (Elf_External_Verdaux);
6554
6555 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6556 (Elf_External_Verdaux *) p);
6557 p += sizeof (Elf_External_Verdaux);
6558 }
6559 }
6560
5a580b3a
AM
6561 elf_tdata (output_bfd)->cverdefs = cdefs;
6562 }
902e9fc7
MR
6563 }
6564
6565 bed = get_elf_backend_data (output_bfd);
6566
6567 if (info->gc_sections && bed->can_gc_sections)
6568 {
6569 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6570
6571 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6572 dynamic symbol table. */
902e9fc7
MR
6573 sweep_info.info = info;
6574 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6575 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6576 &sweep_info);
3d13f3e9
AM
6577 }
6578
6579 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6580 {
6581 asection *s;
6582 struct elf_find_verdep_info sinfo;
6583
6584 /* Work out the size of the version reference section. */
6585
6586 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6587 BFD_ASSERT (s != NULL);
902e9fc7 6588
3d13f3e9
AM
6589 sinfo.info = info;
6590 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6591 if (sinfo.vers == 0)
6592 sinfo.vers = 1;
6593 sinfo.failed = FALSE;
6594
6595 elf_link_hash_traverse (elf_hash_table (info),
6596 _bfd_elf_link_find_version_dependencies,
6597 &sinfo);
6598 if (sinfo.failed)
6599 return FALSE;
6600
6601 if (elf_tdata (output_bfd)->verref == NULL)
6602 s->flags |= SEC_EXCLUDE;
6603 else
6604 {
6605 Elf_Internal_Verneed *vn;
6606 unsigned int size;
6607 unsigned int crefs;
6608 bfd_byte *p;
6609
6610 /* Build the version dependency section. */
6611 size = 0;
6612 crefs = 0;
6613 for (vn = elf_tdata (output_bfd)->verref;
6614 vn != NULL;
6615 vn = vn->vn_nextref)
6616 {
6617 Elf_Internal_Vernaux *a;
6618
6619 size += sizeof (Elf_External_Verneed);
6620 ++crefs;
6621 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6622 size += sizeof (Elf_External_Vernaux);
6623 }
6624
6625 s->size = size;
6626 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6627 if (s->contents == NULL)
6628 return FALSE;
6629
6630 p = s->contents;
6631 for (vn = elf_tdata (output_bfd)->verref;
6632 vn != NULL;
6633 vn = vn->vn_nextref)
6634 {
6635 unsigned int caux;
6636 Elf_Internal_Vernaux *a;
6637 size_t indx;
6638
6639 caux = 0;
6640 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6641 ++caux;
6642
6643 vn->vn_version = VER_NEED_CURRENT;
6644 vn->vn_cnt = caux;
6645 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6646 elf_dt_name (vn->vn_bfd) != NULL
6647 ? elf_dt_name (vn->vn_bfd)
6648 : lbasename (vn->vn_bfd->filename),
6649 FALSE);
6650 if (indx == (size_t) -1)
6651 return FALSE;
6652 vn->vn_file = indx;
6653 vn->vn_aux = sizeof (Elf_External_Verneed);
6654 if (vn->vn_nextref == NULL)
6655 vn->vn_next = 0;
6656 else
6657 vn->vn_next = (sizeof (Elf_External_Verneed)
6658 + caux * sizeof (Elf_External_Vernaux));
6659
6660 _bfd_elf_swap_verneed_out (output_bfd, vn,
6661 (Elf_External_Verneed *) p);
6662 p += sizeof (Elf_External_Verneed);
6663
6664 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6665 {
6666 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6667 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6668 a->vna_nodename, FALSE);
6669 if (indx == (size_t) -1)
6670 return FALSE;
6671 a->vna_name = indx;
6672 if (a->vna_nextptr == NULL)
6673 a->vna_next = 0;
6674 else
6675 a->vna_next = sizeof (Elf_External_Vernaux);
6676
6677 _bfd_elf_swap_vernaux_out (output_bfd, a,
6678 (Elf_External_Vernaux *) p);
6679 p += sizeof (Elf_External_Vernaux);
6680 }
6681 }
6682
6683 elf_tdata (output_bfd)->cverrefs = crefs;
6684 }
902e9fc7
MR
6685 }
6686
6687 /* Any syms created from now on start with -1 in
6688 got.refcount/offset and plt.refcount/offset. */
6689 elf_hash_table (info)->init_got_refcount
6690 = elf_hash_table (info)->init_got_offset;
6691 elf_hash_table (info)->init_plt_refcount
6692 = elf_hash_table (info)->init_plt_offset;
6693
6694 if (bfd_link_relocatable (info)
6695 && !_bfd_elf_size_group_sections (info))
6696 return FALSE;
6697
6698 /* The backend may have to create some sections regardless of whether
6699 we're dynamic or not. */
6700 if (bed->elf_backend_always_size_sections
6701 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6702 return FALSE;
6703
6704 /* Determine any GNU_STACK segment requirements, after the backend
6705 has had a chance to set a default segment size. */
6706 if (info->execstack)
6707 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6708 else if (info->noexecstack)
6709 elf_stack_flags (output_bfd) = PF_R | PF_W;
6710 else
6711 {
6712 bfd *inputobj;
6713 asection *notesec = NULL;
6714 int exec = 0;
6715
6716 for (inputobj = info->input_bfds;
6717 inputobj;
6718 inputobj = inputobj->link.next)
6719 {
6720 asection *s;
6721
6722 if (inputobj->flags
6723 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6724 continue;
57963c05
AM
6725 s = inputobj->sections;
6726 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6727 continue;
6728
902e9fc7
MR
6729 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6730 if (s)
6731 {
6732 if (s->flags & SEC_CODE)
6733 exec = PF_X;
6734 notesec = s;
6735 }
6736 else if (bed->default_execstack)
6737 exec = PF_X;
6738 }
6739 if (notesec || info->stacksize > 0)
6740 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6741 if (notesec && exec && bfd_link_relocatable (info)
6742 && notesec->output_section != bfd_abs_section_ptr)
6743 notesec->output_section->flags |= SEC_CODE;
6744 }
6745
6746 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6747 {
6748 struct elf_info_failed eif;
6749 struct elf_link_hash_entry *h;
6750 asection *dynstr;
6751 asection *s;
6752
6753 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6754 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6755
902e9fc7
MR
6756 if (info->symbolic)
6757 {
6758 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6759 return FALSE;
6760 info->flags |= DF_SYMBOLIC;
6761 }
6762
6763 if (rpath != NULL)
6764 {
6765 size_t indx;
6766 bfd_vma tag;
6767
6768 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6769 TRUE);
6770 if (indx == (size_t) -1)
6771 return FALSE;
6772
6773 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6774 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6775 return FALSE;
6776 }
6777
6778 if (filter_shlib != NULL)
6779 {
6780 size_t indx;
6781
6782 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6783 filter_shlib, TRUE);
6784 if (indx == (size_t) -1
6785 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6786 return FALSE;
6787 }
6788
6789 if (auxiliary_filters != NULL)
6790 {
6791 const char * const *p;
6792
6793 for (p = auxiliary_filters; *p != NULL; p++)
6794 {
6795 size_t indx;
6796
6797 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6798 *p, TRUE);
6799 if (indx == (size_t) -1
6800 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6801 return FALSE;
6802 }
6803 }
6804
6805 if (audit != NULL)
6806 {
6807 size_t indx;
6808
6809 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6810 TRUE);
6811 if (indx == (size_t) -1
6812 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6813 return FALSE;
6814 }
6815
6816 if (depaudit != NULL)
6817 {
6818 size_t indx;
6819
6820 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6821 TRUE);
6822 if (indx == (size_t) -1
6823 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6824 return FALSE;
6825 }
6826
6827 eif.info = info;
6828 eif.failed = FALSE;
6829
6830 /* Find all symbols which were defined in a dynamic object and make
6831 the backend pick a reasonable value for them. */
6832 elf_link_hash_traverse (elf_hash_table (info),
6833 _bfd_elf_adjust_dynamic_symbol,
6834 &eif);
6835 if (eif.failed)
6836 return FALSE;
6837
6838 /* Add some entries to the .dynamic section. We fill in some of the
6839 values later, in bfd_elf_final_link, but we must add the entries
6840 now so that we know the final size of the .dynamic section. */
6841
6842 /* If there are initialization and/or finalization functions to
6843 call then add the corresponding DT_INIT/DT_FINI entries. */
6844 h = (info->init_function
6845 ? elf_link_hash_lookup (elf_hash_table (info),
6846 info->init_function, FALSE,
6847 FALSE, FALSE)
6848 : NULL);
6849 if (h != NULL
6850 && (h->ref_regular
6851 || h->def_regular))
6852 {
6853 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6854 return FALSE;
6855 }
6856 h = (info->fini_function
6857 ? elf_link_hash_lookup (elf_hash_table (info),
6858 info->fini_function, FALSE,
6859 FALSE, FALSE)
6860 : NULL);
6861 if (h != NULL
6862 && (h->ref_regular
6863 || h->def_regular))
6864 {
6865 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6866 return FALSE;
6867 }
6868
6869 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6870 if (s != NULL && s->linker_has_input)
6871 {
6872 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6873 if (! bfd_link_executable (info))
6874 {
6875 bfd *sub;
6876 asection *o;
6877
57963c05
AM
6878 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6879 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6880 && (o = sub->sections) != NULL
6881 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6882 for (o = sub->sections; o != NULL; o = o->next)
6883 if (elf_section_data (o)->this_hdr.sh_type
6884 == SHT_PREINIT_ARRAY)
6885 {
6886 _bfd_error_handler
871b3ab2 6887 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6888 sub);
6889 break;
6890 }
6891
6892 bfd_set_error (bfd_error_nonrepresentable_section);
6893 return FALSE;
6894 }
6895
6896 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6897 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6898 return FALSE;
6899 }
6900 s = bfd_get_section_by_name (output_bfd, ".init_array");
6901 if (s != NULL && s->linker_has_input)
6902 {
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6904 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6905 return FALSE;
6906 }
6907 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6908 if (s != NULL && s->linker_has_input)
6909 {
6910 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6911 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6912 return FALSE;
6913 }
6914
6915 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6916 /* If .dynstr is excluded from the link, we don't want any of
6917 these tags. Strictly, we should be checking each section
6918 individually; This quick check covers for the case where
6919 someone does a /DISCARD/ : { *(*) }. */
6920 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6921 {
6922 bfd_size_type strsize;
6923
6924 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6925 if ((info->emit_hash
6926 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6927 || (info->emit_gnu_hash
6928 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6929 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6930 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6931 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6932 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6933 bed->s->sizeof_sym))
6934 return FALSE;
6935 }
6936 }
6937
6938 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6939 return FALSE;
6940
6941 /* The backend must work out the sizes of all the other dynamic
6942 sections. */
6943 if (dynobj != NULL
6944 && bed->elf_backend_size_dynamic_sections != NULL
6945 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6946 return FALSE;
6947
6948 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6949 {
902e9fc7
MR
6950 if (elf_tdata (output_bfd)->cverdefs)
6951 {
6952 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6953
6954 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6955 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6956 return FALSE;
6957 }
6958
6959 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6960 {
6961 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6962 return FALSE;
6963 }
6964 else if (info->flags & DF_BIND_NOW)
6965 {
6966 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6967 return FALSE;
6968 }
6969
6970 if (info->flags_1)
6971 {
6972 if (bfd_link_executable (info))
6973 info->flags_1 &= ~ (DF_1_INITFIRST
6974 | DF_1_NODELETE
6975 | DF_1_NOOPEN);
6976 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6977 return FALSE;
6978 }
6979
6980 if (elf_tdata (output_bfd)->cverrefs)
6981 {
6982 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6983
6984 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6985 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6986 return FALSE;
6987 }
5a580b3a 6988
8423293d
AM
6989 if ((elf_tdata (output_bfd)->cverrefs == 0
6990 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 6991 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 6992 {
902e9fc7
MR
6993 asection *s;
6994
3d4d4302 6995 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6996 s->flags |= SEC_EXCLUDE;
6997 }
6998 }
6999 return TRUE;
7000}
7001
74541ad4
AM
7002/* Find the first non-excluded output section. We'll use its
7003 section symbol for some emitted relocs. */
7004void
7005_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7006{
7007 asection *s;
7008
7009 for (s = output_bfd->sections; s != NULL; s = s->next)
7010 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7011 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
7012 {
7013 elf_hash_table (info)->text_index_section = s;
7014 break;
7015 }
7016}
7017
7018/* Find two non-excluded output sections, one for code, one for data.
7019 We'll use their section symbols for some emitted relocs. */
7020void
7021_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7022{
7023 asection *s;
7024
266b05cf 7025 /* Data first, since setting text_index_section changes
7f923b7f 7026 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7027 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 7028 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 7029 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7030 {
266b05cf 7031 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
7032 break;
7033 }
7034
7035 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
7036 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7037 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 7038 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7039 {
266b05cf 7040 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
7041 break;
7042 }
7043
7044 if (elf_hash_table (info)->text_index_section == NULL)
7045 elf_hash_table (info)->text_index_section
7046 = elf_hash_table (info)->data_index_section;
7047}
7048
8423293d
AM
7049bfd_boolean
7050bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7051{
74541ad4 7052 const struct elf_backend_data *bed;
23ec1e32 7053 unsigned long section_sym_count;
96d01d93 7054 bfd_size_type dynsymcount = 0;
74541ad4 7055
8423293d
AM
7056 if (!is_elf_hash_table (info->hash))
7057 return TRUE;
7058
74541ad4
AM
7059 bed = get_elf_backend_data (output_bfd);
7060 (*bed->elf_backend_init_index_section) (output_bfd, info);
7061
23ec1e32
MR
7062 /* Assign dynsym indices. In a shared library we generate a section
7063 symbol for each output section, which come first. Next come all
7064 of the back-end allocated local dynamic syms, followed by the rest
7065 of the global symbols.
7066
7067 This is usually not needed for static binaries, however backends
7068 can request to always do it, e.g. the MIPS backend uses dynamic
7069 symbol counts to lay out GOT, which will be produced in the
7070 presence of GOT relocations even in static binaries (holding fixed
7071 data in that case, to satisfy those relocations). */
7072
7073 if (elf_hash_table (info)->dynamic_sections_created
7074 || bed->always_renumber_dynsyms)
7075 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7076 &section_sym_count);
7077
8423293d
AM
7078 if (elf_hash_table (info)->dynamic_sections_created)
7079 {
7080 bfd *dynobj;
8423293d 7081 asection *s;
8423293d
AM
7082 unsigned int dtagcount;
7083
7084 dynobj = elf_hash_table (info)->dynobj;
7085
5a580b3a 7086 /* Work out the size of the symbol version section. */
3d4d4302 7087 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7088 BFD_ASSERT (s != NULL);
d5486c43 7089 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7090 {
eea6121a 7091 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7092 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7093 if (s->contents == NULL)
7094 return FALSE;
7095
7096 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7097 return FALSE;
7098 }
7099
7100 /* Set the size of the .dynsym and .hash sections. We counted
7101 the number of dynamic symbols in elf_link_add_object_symbols.
7102 We will build the contents of .dynsym and .hash when we build
7103 the final symbol table, because until then we do not know the
7104 correct value to give the symbols. We built the .dynstr
7105 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7106 s = elf_hash_table (info)->dynsym;
5a580b3a 7107 BFD_ASSERT (s != NULL);
eea6121a 7108 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7109
d5486c43
L
7110 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7111 if (s->contents == NULL)
7112 return FALSE;
5a580b3a 7113
d5486c43
L
7114 /* The first entry in .dynsym is a dummy symbol. Clear all the
7115 section syms, in case we don't output them all. */
7116 ++section_sym_count;
7117 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7118
fdc90cb4
JJ
7119 elf_hash_table (info)->bucketcount = 0;
7120
5a580b3a
AM
7121 /* Compute the size of the hashing table. As a side effect this
7122 computes the hash values for all the names we export. */
fdc90cb4
JJ
7123 if (info->emit_hash)
7124 {
7125 unsigned long int *hashcodes;
14b1c01e 7126 struct hash_codes_info hashinf;
fdc90cb4
JJ
7127 bfd_size_type amt;
7128 unsigned long int nsyms;
7129 size_t bucketcount;
7130 size_t hash_entry_size;
7131
7132 /* Compute the hash values for all exported symbols. At the same
7133 time store the values in an array so that we could use them for
7134 optimizations. */
7135 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7136 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7137 if (hashcodes == NULL)
7138 return FALSE;
14b1c01e
AM
7139 hashinf.hashcodes = hashcodes;
7140 hashinf.error = FALSE;
5a580b3a 7141
fdc90cb4
JJ
7142 /* Put all hash values in HASHCODES. */
7143 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7144 elf_collect_hash_codes, &hashinf);
7145 if (hashinf.error)
4dd07732
AM
7146 {
7147 free (hashcodes);
7148 return FALSE;
7149 }
5a580b3a 7150
14b1c01e 7151 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7152 bucketcount
7153 = compute_bucket_count (info, hashcodes, nsyms, 0);
7154 free (hashcodes);
7155
4b48e2f6 7156 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7157 return FALSE;
5a580b3a 7158
fdc90cb4
JJ
7159 elf_hash_table (info)->bucketcount = bucketcount;
7160
3d4d4302 7161 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7162 BFD_ASSERT (s != NULL);
7163 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7164 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7165 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7166 if (s->contents == NULL)
7167 return FALSE;
7168
7169 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7170 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7171 s->contents + hash_entry_size);
7172 }
7173
7174 if (info->emit_gnu_hash)
7175 {
7176 size_t i, cnt;
7177 unsigned char *contents;
7178 struct collect_gnu_hash_codes cinfo;
7179 bfd_size_type amt;
7180 size_t bucketcount;
7181
7182 memset (&cinfo, 0, sizeof (cinfo));
7183
7184 /* Compute the hash values for all exported symbols. At the same
7185 time store the values in an array so that we could use them for
7186 optimizations. */
7187 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7188 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7189 if (cinfo.hashcodes == NULL)
7190 return FALSE;
7191
7192 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7193 cinfo.min_dynindx = -1;
7194 cinfo.output_bfd = output_bfd;
7195 cinfo.bed = bed;
7196
7197 /* Put all hash values in HASHCODES. */
7198 elf_link_hash_traverse (elf_hash_table (info),
7199 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7200 if (cinfo.error)
4dd07732
AM
7201 {
7202 free (cinfo.hashcodes);
7203 return FALSE;
7204 }
fdc90cb4
JJ
7205
7206 bucketcount
7207 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7208
7209 if (bucketcount == 0)
7210 {
7211 free (cinfo.hashcodes);
7212 return FALSE;
7213 }
7214
3d4d4302 7215 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7216 BFD_ASSERT (s != NULL);
7217
7218 if (cinfo.nsyms == 0)
7219 {
7220 /* Empty .gnu.hash section is special. */
7221 BFD_ASSERT (cinfo.min_dynindx == -1);
7222 free (cinfo.hashcodes);
7223 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7224 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7225 if (contents == NULL)
7226 return FALSE;
7227 s->contents = contents;
7228 /* 1 empty bucket. */
7229 bfd_put_32 (output_bfd, 1, contents);
7230 /* SYMIDX above the special symbol 0. */
7231 bfd_put_32 (output_bfd, 1, contents + 4);
7232 /* Just one word for bitmask. */
7233 bfd_put_32 (output_bfd, 1, contents + 8);
7234 /* Only hash fn bloom filter. */
7235 bfd_put_32 (output_bfd, 0, contents + 12);
7236 /* No hashes are valid - empty bitmask. */
7237 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7238 /* No hashes in the only bucket. */
7239 bfd_put_32 (output_bfd, 0,
7240 contents + 16 + bed->s->arch_size / 8);
7241 }
7242 else
7243 {
9e6619e2 7244 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7245 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7246
9e6619e2
AM
7247 x = cinfo.nsyms;
7248 maskbitslog2 = 1;
7249 while ((x >>= 1) != 0)
7250 ++maskbitslog2;
fdc90cb4
JJ
7251 if (maskbitslog2 < 3)
7252 maskbitslog2 = 5;
7253 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7254 maskbitslog2 = maskbitslog2 + 3;
7255 else
7256 maskbitslog2 = maskbitslog2 + 2;
7257 if (bed->s->arch_size == 64)
7258 {
7259 if (maskbitslog2 == 5)
7260 maskbitslog2 = 6;
7261 cinfo.shift1 = 6;
7262 }
7263 else
7264 cinfo.shift1 = 5;
7265 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7266 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7267 cinfo.maskbits = 1 << maskbitslog2;
7268 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7269 amt = bucketcount * sizeof (unsigned long int) * 2;
7270 amt += maskwords * sizeof (bfd_vma);
a50b1753 7271 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7272 if (cinfo.bitmask == NULL)
7273 {
7274 free (cinfo.hashcodes);
7275 return FALSE;
7276 }
7277
a50b1753 7278 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7279 cinfo.indx = cinfo.counts + bucketcount;
7280 cinfo.symindx = dynsymcount - cinfo.nsyms;
7281 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7282
7283 /* Determine how often each hash bucket is used. */
7284 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7285 for (i = 0; i < cinfo.nsyms; ++i)
7286 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7287
7288 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7289 if (cinfo.counts[i] != 0)
7290 {
7291 cinfo.indx[i] = cnt;
7292 cnt += cinfo.counts[i];
7293 }
7294 BFD_ASSERT (cnt == dynsymcount);
7295 cinfo.bucketcount = bucketcount;
7296 cinfo.local_indx = cinfo.min_dynindx;
7297
7298 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7299 s->size += cinfo.maskbits / 8;
a50b1753 7300 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7301 if (contents == NULL)
7302 {
7303 free (cinfo.bitmask);
7304 free (cinfo.hashcodes);
7305 return FALSE;
7306 }
7307
7308 s->contents = contents;
7309 bfd_put_32 (output_bfd, bucketcount, contents);
7310 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7311 bfd_put_32 (output_bfd, maskwords, contents + 8);
7312 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7313 contents += 16 + cinfo.maskbits / 8;
7314
7315 for (i = 0; i < bucketcount; ++i)
7316 {
7317 if (cinfo.counts[i] == 0)
7318 bfd_put_32 (output_bfd, 0, contents);
7319 else
7320 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7321 contents += 4;
7322 }
7323
7324 cinfo.contents = contents;
7325
7326 /* Renumber dynamic symbols, populate .gnu.hash section. */
7327 elf_link_hash_traverse (elf_hash_table (info),
7328 elf_renumber_gnu_hash_syms, &cinfo);
7329
7330 contents = s->contents + 16;
7331 for (i = 0; i < maskwords; ++i)
7332 {
7333 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7334 contents);
7335 contents += bed->s->arch_size / 8;
7336 }
7337
7338 free (cinfo.bitmask);
7339 free (cinfo.hashcodes);
7340 }
7341 }
5a580b3a 7342
3d4d4302 7343 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7344 BFD_ASSERT (s != NULL);
7345
4ad4eba5 7346 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7347
eea6121a 7348 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7349
7350 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7351 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7352 return FALSE;
7353 }
7354
7355 return TRUE;
7356}
4d269e42 7357\f
4d269e42
AM
7358/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7359
7360static void
7361merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7362 asection *sec)
7363{
dbaa2011
AM
7364 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7365 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7366}
7367
7368/* Finish SHF_MERGE section merging. */
7369
7370bfd_boolean
630993ec 7371_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7372{
7373 bfd *ibfd;
7374 asection *sec;
7375
7376 if (!is_elf_hash_table (info->hash))
7377 return FALSE;
7378
c72f2fb2 7379 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7380 if ((ibfd->flags & DYNAMIC) == 0
7381 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7382 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7383 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7384 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7385 if ((sec->flags & SEC_MERGE) != 0
7386 && !bfd_is_abs_section (sec->output_section))
7387 {
7388 struct bfd_elf_section_data *secdata;
7389
7390 secdata = elf_section_data (sec);
630993ec 7391 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7392 &elf_hash_table (info)->merge_info,
7393 sec, &secdata->sec_info))
7394 return FALSE;
7395 else if (secdata->sec_info)
dbaa2011 7396 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7397 }
7398
7399 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7400 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7401 merge_sections_remove_hook);
7402 return TRUE;
7403}
7404
7405/* Create an entry in an ELF linker hash table. */
7406
7407struct bfd_hash_entry *
7408_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7409 struct bfd_hash_table *table,
7410 const char *string)
7411{
7412 /* Allocate the structure if it has not already been allocated by a
7413 subclass. */
7414 if (entry == NULL)
7415 {
a50b1753 7416 entry = (struct bfd_hash_entry *)
ca4be51c 7417 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7418 if (entry == NULL)
7419 return entry;
7420 }
7421
7422 /* Call the allocation method of the superclass. */
7423 entry = _bfd_link_hash_newfunc (entry, table, string);
7424 if (entry != NULL)
7425 {
7426 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7427 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7428
7429 /* Set local fields. */
7430 ret->indx = -1;
7431 ret->dynindx = -1;
7432 ret->got = htab->init_got_refcount;
7433 ret->plt = htab->init_plt_refcount;
7434 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7435 - offsetof (struct elf_link_hash_entry, size)));
7436 /* Assume that we have been called by a non-ELF symbol reader.
7437 This flag is then reset by the code which reads an ELF input
7438 file. This ensures that a symbol created by a non-ELF symbol
7439 reader will have the flag set correctly. */
7440 ret->non_elf = 1;
7441 }
7442
7443 return entry;
7444}
7445
7446/* Copy data from an indirect symbol to its direct symbol, hiding the
7447 old indirect symbol. Also used for copying flags to a weakdef. */
7448
7449void
7450_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7451 struct elf_link_hash_entry *dir,
7452 struct elf_link_hash_entry *ind)
7453{
7454 struct elf_link_hash_table *htab;
7455
7456 /* Copy down any references that we may have already seen to the
e81830c5 7457 symbol which just became indirect. */
4d269e42 7458
422f1182 7459 if (dir->versioned != versioned_hidden)
e81830c5
AM
7460 dir->ref_dynamic |= ind->ref_dynamic;
7461 dir->ref_regular |= ind->ref_regular;
7462 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7463 dir->non_got_ref |= ind->non_got_ref;
7464 dir->needs_plt |= ind->needs_plt;
7465 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7466
7467 if (ind->root.type != bfd_link_hash_indirect)
7468 return;
7469
7470 /* Copy over the global and procedure linkage table refcount entries.
7471 These may have been already set up by a check_relocs routine. */
7472 htab = elf_hash_table (info);
7473 if (ind->got.refcount > htab->init_got_refcount.refcount)
7474 {
7475 if (dir->got.refcount < 0)
7476 dir->got.refcount = 0;
7477 dir->got.refcount += ind->got.refcount;
7478 ind->got.refcount = htab->init_got_refcount.refcount;
7479 }
7480
7481 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7482 {
7483 if (dir->plt.refcount < 0)
7484 dir->plt.refcount = 0;
7485 dir->plt.refcount += ind->plt.refcount;
7486 ind->plt.refcount = htab->init_plt_refcount.refcount;
7487 }
7488
7489 if (ind->dynindx != -1)
7490 {
7491 if (dir->dynindx != -1)
7492 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7493 dir->dynindx = ind->dynindx;
7494 dir->dynstr_index = ind->dynstr_index;
7495 ind->dynindx = -1;
7496 ind->dynstr_index = 0;
7497 }
7498}
7499
7500void
7501_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7502 struct elf_link_hash_entry *h,
7503 bfd_boolean force_local)
7504{
3aa14d16
L
7505 /* STT_GNU_IFUNC symbol must go through PLT. */
7506 if (h->type != STT_GNU_IFUNC)
7507 {
7508 h->plt = elf_hash_table (info)->init_plt_offset;
7509 h->needs_plt = 0;
7510 }
4d269e42
AM
7511 if (force_local)
7512 {
7513 h->forced_local = 1;
7514 if (h->dynindx != -1)
7515 {
4d269e42
AM
7516 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7517 h->dynstr_index);
641338d8
AM
7518 h->dynindx = -1;
7519 h->dynstr_index = 0;
4d269e42
AM
7520 }
7521 }
7522}
7523
34a87bb0
L
7524/* Hide a symbol. */
7525
7526void
7527_bfd_elf_link_hide_symbol (bfd *output_bfd,
7528 struct bfd_link_info *info,
7529 struct bfd_link_hash_entry *h)
7530{
7531 if (is_elf_hash_table (info->hash))
7532 {
7533 const struct elf_backend_data *bed
7534 = get_elf_backend_data (output_bfd);
7535 struct elf_link_hash_entry *eh
7536 = (struct elf_link_hash_entry *) h;
7537 bed->elf_backend_hide_symbol (info, eh, TRUE);
7538 eh->def_dynamic = 0;
7539 eh->ref_dynamic = 0;
7540 eh->dynamic_def = 0;
7541 }
7542}
7543
7bf52ea2
AM
7544/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7545 caller. */
4d269e42
AM
7546
7547bfd_boolean
7548_bfd_elf_link_hash_table_init
7549 (struct elf_link_hash_table *table,
7550 bfd *abfd,
7551 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7552 struct bfd_hash_table *,
7553 const char *),
4dfe6ac6
NC
7554 unsigned int entsize,
7555 enum elf_target_id target_id)
4d269e42
AM
7556{
7557 bfd_boolean ret;
7558 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7559
4d269e42
AM
7560 table->init_got_refcount.refcount = can_refcount - 1;
7561 table->init_plt_refcount.refcount = can_refcount - 1;
7562 table->init_got_offset.offset = -(bfd_vma) 1;
7563 table->init_plt_offset.offset = -(bfd_vma) 1;
7564 /* The first dynamic symbol is a dummy. */
7565 table->dynsymcount = 1;
7566
7567 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7568
4d269e42 7569 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7570 table->hash_table_id = target_id;
4d269e42
AM
7571
7572 return ret;
7573}
7574
7575/* Create an ELF linker hash table. */
7576
7577struct bfd_link_hash_table *
7578_bfd_elf_link_hash_table_create (bfd *abfd)
7579{
7580 struct elf_link_hash_table *ret;
7581 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7582
7bf52ea2 7583 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7584 if (ret == NULL)
7585 return NULL;
7586
7587 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7588 sizeof (struct elf_link_hash_entry),
7589 GENERIC_ELF_DATA))
4d269e42
AM
7590 {
7591 free (ret);
7592 return NULL;
7593 }
d495ab0d 7594 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7595
7596 return &ret->root;
7597}
7598
9f7c3e5e
AM
7599/* Destroy an ELF linker hash table. */
7600
7601void
d495ab0d 7602_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7603{
d495ab0d
AM
7604 struct elf_link_hash_table *htab;
7605
7606 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7607 if (htab->dynstr != NULL)
7608 _bfd_elf_strtab_free (htab->dynstr);
7609 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7610 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7611}
7612
4d269e42
AM
7613/* This is a hook for the ELF emulation code in the generic linker to
7614 tell the backend linker what file name to use for the DT_NEEDED
7615 entry for a dynamic object. */
7616
7617void
7618bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7619{
7620 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7621 && bfd_get_format (abfd) == bfd_object)
7622 elf_dt_name (abfd) = name;
7623}
7624
7625int
7626bfd_elf_get_dyn_lib_class (bfd *abfd)
7627{
7628 int lib_class;
7629 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7630 && bfd_get_format (abfd) == bfd_object)
7631 lib_class = elf_dyn_lib_class (abfd);
7632 else
7633 lib_class = 0;
7634 return lib_class;
7635}
7636
7637void
7638bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7639{
7640 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7641 && bfd_get_format (abfd) == bfd_object)
7642 elf_dyn_lib_class (abfd) = lib_class;
7643}
7644
7645/* Get the list of DT_NEEDED entries for a link. This is a hook for
7646 the linker ELF emulation code. */
7647
7648struct bfd_link_needed_list *
7649bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7650 struct bfd_link_info *info)
7651{
7652 if (! is_elf_hash_table (info->hash))
7653 return NULL;
7654 return elf_hash_table (info)->needed;
7655}
7656
7657/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7658 hook for the linker ELF emulation code. */
7659
7660struct bfd_link_needed_list *
7661bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7662 struct bfd_link_info *info)
7663{
7664 if (! is_elf_hash_table (info->hash))
7665 return NULL;
7666 return elf_hash_table (info)->runpath;
7667}
7668
7669/* Get the name actually used for a dynamic object for a link. This
7670 is the SONAME entry if there is one. Otherwise, it is the string
7671 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7672
7673const char *
7674bfd_elf_get_dt_soname (bfd *abfd)
7675{
7676 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7677 && bfd_get_format (abfd) == bfd_object)
7678 return elf_dt_name (abfd);
7679 return NULL;
7680}
7681
7682/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7683 the ELF linker emulation code. */
7684
7685bfd_boolean
7686bfd_elf_get_bfd_needed_list (bfd *abfd,
7687 struct bfd_link_needed_list **pneeded)
7688{
7689 asection *s;
7690 bfd_byte *dynbuf = NULL;
cb33740c 7691 unsigned int elfsec;
4d269e42
AM
7692 unsigned long shlink;
7693 bfd_byte *extdyn, *extdynend;
7694 size_t extdynsize;
7695 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7696
7697 *pneeded = NULL;
7698
7699 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7700 || bfd_get_format (abfd) != bfd_object)
7701 return TRUE;
7702
7703 s = bfd_get_section_by_name (abfd, ".dynamic");
7704 if (s == NULL || s->size == 0)
7705 return TRUE;
7706
7707 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7708 goto error_return;
7709
7710 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7711 if (elfsec == SHN_BAD)
4d269e42
AM
7712 goto error_return;
7713
7714 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7715
4d269e42
AM
7716 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7717 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7718
7719 extdyn = dynbuf;
7720 extdynend = extdyn + s->size;
7721 for (; extdyn < extdynend; extdyn += extdynsize)
7722 {
7723 Elf_Internal_Dyn dyn;
7724
7725 (*swap_dyn_in) (abfd, extdyn, &dyn);
7726
7727 if (dyn.d_tag == DT_NULL)
7728 break;
7729
7730 if (dyn.d_tag == DT_NEEDED)
7731 {
7732 const char *string;
7733 struct bfd_link_needed_list *l;
7734 unsigned int tagv = dyn.d_un.d_val;
7735 bfd_size_type amt;
7736
7737 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7738 if (string == NULL)
7739 goto error_return;
7740
7741 amt = sizeof *l;
a50b1753 7742 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7743 if (l == NULL)
7744 goto error_return;
7745
7746 l->by = abfd;
7747 l->name = string;
7748 l->next = *pneeded;
7749 *pneeded = l;
7750 }
7751 }
7752
7753 free (dynbuf);
7754
7755 return TRUE;
7756
7757 error_return:
7758 if (dynbuf != NULL)
7759 free (dynbuf);
7760 return FALSE;
7761}
7762
7763struct elf_symbuf_symbol
7764{
7765 unsigned long st_name; /* Symbol name, index in string tbl */
7766 unsigned char st_info; /* Type and binding attributes */
7767 unsigned char st_other; /* Visibilty, and target specific */
7768};
7769
7770struct elf_symbuf_head
7771{
7772 struct elf_symbuf_symbol *ssym;
ef53be89 7773 size_t count;
4d269e42
AM
7774 unsigned int st_shndx;
7775};
7776
7777struct elf_symbol
7778{
7779 union
7780 {
7781 Elf_Internal_Sym *isym;
7782 struct elf_symbuf_symbol *ssym;
7783 } u;
7784 const char *name;
7785};
7786
7787/* Sort references to symbols by ascending section number. */
7788
7789static int
7790elf_sort_elf_symbol (const void *arg1, const void *arg2)
7791{
7792 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7793 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7794
7795 return s1->st_shndx - s2->st_shndx;
7796}
7797
7798static int
7799elf_sym_name_compare (const void *arg1, const void *arg2)
7800{
7801 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7802 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7803 return strcmp (s1->name, s2->name);
7804}
7805
7806static struct elf_symbuf_head *
ef53be89 7807elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7808{
14b1c01e 7809 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7810 struct elf_symbuf_symbol *ssym;
7811 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7812 size_t i, shndx_count, total_size;
4d269e42 7813
a50b1753 7814 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7815 if (indbuf == NULL)
7816 return NULL;
7817
7818 for (ind = indbuf, i = 0; i < symcount; i++)
7819 if (isymbuf[i].st_shndx != SHN_UNDEF)
7820 *ind++ = &isymbuf[i];
7821 indbufend = ind;
7822
7823 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7824 elf_sort_elf_symbol);
7825
7826 shndx_count = 0;
7827 if (indbufend > indbuf)
7828 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7829 if (ind[0]->st_shndx != ind[1]->st_shndx)
7830 shndx_count++;
7831
3ae181ee
L
7832 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7833 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7834 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7835 if (ssymbuf == NULL)
7836 {
7837 free (indbuf);
7838 return NULL;
7839 }
7840
3ae181ee 7841 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7842 ssymbuf->ssym = NULL;
7843 ssymbuf->count = shndx_count;
7844 ssymbuf->st_shndx = 0;
7845 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7846 {
7847 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7848 {
7849 ssymhead++;
7850 ssymhead->ssym = ssym;
7851 ssymhead->count = 0;
7852 ssymhead->st_shndx = (*ind)->st_shndx;
7853 }
7854 ssym->st_name = (*ind)->st_name;
7855 ssym->st_info = (*ind)->st_info;
7856 ssym->st_other = (*ind)->st_other;
7857 ssymhead->count++;
7858 }
ef53be89 7859 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7860 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7861 == total_size));
4d269e42
AM
7862
7863 free (indbuf);
7864 return ssymbuf;
7865}
7866
7867/* Check if 2 sections define the same set of local and global
7868 symbols. */
7869
8f317e31 7870static bfd_boolean
4d269e42
AM
7871bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7872 struct bfd_link_info *info)
7873{
7874 bfd *bfd1, *bfd2;
7875 const struct elf_backend_data *bed1, *bed2;
7876 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7877 size_t symcount1, symcount2;
4d269e42
AM
7878 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7879 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7880 Elf_Internal_Sym *isym, *isymend;
7881 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7882 size_t count1, count2, i;
cb33740c 7883 unsigned int shndx1, shndx2;
4d269e42
AM
7884 bfd_boolean result;
7885
7886 bfd1 = sec1->owner;
7887 bfd2 = sec2->owner;
7888
4d269e42
AM
7889 /* Both sections have to be in ELF. */
7890 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7891 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7892 return FALSE;
7893
7894 if (elf_section_type (sec1) != elf_section_type (sec2))
7895 return FALSE;
7896
4d269e42
AM
7897 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7898 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7899 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7900 return FALSE;
7901
7902 bed1 = get_elf_backend_data (bfd1);
7903 bed2 = get_elf_backend_data (bfd2);
7904 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7905 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7906 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7907 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7908
7909 if (symcount1 == 0 || symcount2 == 0)
7910 return FALSE;
7911
7912 result = FALSE;
7913 isymbuf1 = NULL;
7914 isymbuf2 = NULL;
a50b1753
NC
7915 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7916 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7917
7918 if (ssymbuf1 == NULL)
7919 {
7920 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7921 NULL, NULL, NULL);
7922 if (isymbuf1 == NULL)
7923 goto done;
7924
7925 if (!info->reduce_memory_overheads)
7926 elf_tdata (bfd1)->symbuf = ssymbuf1
7927 = elf_create_symbuf (symcount1, isymbuf1);
7928 }
7929
7930 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7931 {
7932 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7933 NULL, NULL, NULL);
7934 if (isymbuf2 == NULL)
7935 goto done;
7936
7937 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7938 elf_tdata (bfd2)->symbuf = ssymbuf2
7939 = elf_create_symbuf (symcount2, isymbuf2);
7940 }
7941
7942 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7943 {
7944 /* Optimized faster version. */
ef53be89 7945 size_t lo, hi, mid;
4d269e42
AM
7946 struct elf_symbol *symp;
7947 struct elf_symbuf_symbol *ssym, *ssymend;
7948
7949 lo = 0;
7950 hi = ssymbuf1->count;
7951 ssymbuf1++;
7952 count1 = 0;
7953 while (lo < hi)
7954 {
7955 mid = (lo + hi) / 2;
cb33740c 7956 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7957 hi = mid;
cb33740c 7958 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7959 lo = mid + 1;
7960 else
7961 {
7962 count1 = ssymbuf1[mid].count;
7963 ssymbuf1 += mid;
7964 break;
7965 }
7966 }
7967
7968 lo = 0;
7969 hi = ssymbuf2->count;
7970 ssymbuf2++;
7971 count2 = 0;
7972 while (lo < hi)
7973 {
7974 mid = (lo + hi) / 2;
cb33740c 7975 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7976 hi = mid;
cb33740c 7977 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7978 lo = mid + 1;
7979 else
7980 {
7981 count2 = ssymbuf2[mid].count;
7982 ssymbuf2 += mid;
7983 break;
7984 }
7985 }
7986
7987 if (count1 == 0 || count2 == 0 || count1 != count2)
7988 goto done;
7989
ca4be51c
AM
7990 symtable1
7991 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7992 symtable2
7993 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7994 if (symtable1 == NULL || symtable2 == NULL)
7995 goto done;
7996
7997 symp = symtable1;
7998 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7999 ssym < ssymend; ssym++, symp++)
8000 {
8001 symp->u.ssym = ssym;
8002 symp->name = bfd_elf_string_from_elf_section (bfd1,
8003 hdr1->sh_link,
8004 ssym->st_name);
8005 }
8006
8007 symp = symtable2;
8008 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8009 ssym < ssymend; ssym++, symp++)
8010 {
8011 symp->u.ssym = ssym;
8012 symp->name = bfd_elf_string_from_elf_section (bfd2,
8013 hdr2->sh_link,
8014 ssym->st_name);
8015 }
8016
8017 /* Sort symbol by name. */
8018 qsort (symtable1, count1, sizeof (struct elf_symbol),
8019 elf_sym_name_compare);
8020 qsort (symtable2, count1, sizeof (struct elf_symbol),
8021 elf_sym_name_compare);
8022
8023 for (i = 0; i < count1; i++)
8024 /* Two symbols must have the same binding, type and name. */
8025 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8026 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8027 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8028 goto done;
8029
8030 result = TRUE;
8031 goto done;
8032 }
8033
a50b1753
NC
8034 symtable1 = (struct elf_symbol *)
8035 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8036 symtable2 = (struct elf_symbol *)
8037 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8038 if (symtable1 == NULL || symtable2 == NULL)
8039 goto done;
8040
8041 /* Count definitions in the section. */
8042 count1 = 0;
8043 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8044 if (isym->st_shndx == shndx1)
4d269e42
AM
8045 symtable1[count1++].u.isym = isym;
8046
8047 count2 = 0;
8048 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8049 if (isym->st_shndx == shndx2)
4d269e42
AM
8050 symtable2[count2++].u.isym = isym;
8051
8052 if (count1 == 0 || count2 == 0 || count1 != count2)
8053 goto done;
8054
8055 for (i = 0; i < count1; i++)
8056 symtable1[i].name
8057 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8058 symtable1[i].u.isym->st_name);
8059
8060 for (i = 0; i < count2; i++)
8061 symtable2[i].name
8062 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8063 symtable2[i].u.isym->st_name);
8064
8065 /* Sort symbol by name. */
8066 qsort (symtable1, count1, sizeof (struct elf_symbol),
8067 elf_sym_name_compare);
8068 qsort (symtable2, count1, sizeof (struct elf_symbol),
8069 elf_sym_name_compare);
8070
8071 for (i = 0; i < count1; i++)
8072 /* Two symbols must have the same binding, type and name. */
8073 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8074 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8075 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8076 goto done;
8077
8078 result = TRUE;
8079
8080done:
8081 if (symtable1)
8082 free (symtable1);
8083 if (symtable2)
8084 free (symtable2);
8085 if (isymbuf1)
8086 free (isymbuf1);
8087 if (isymbuf2)
8088 free (isymbuf2);
8089
8090 return result;
8091}
8092
8093/* Return TRUE if 2 section types are compatible. */
8094
8095bfd_boolean
8096_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8097 bfd *bbfd, const asection *bsec)
8098{
8099 if (asec == NULL
8100 || bsec == NULL
8101 || abfd->xvec->flavour != bfd_target_elf_flavour
8102 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8103 return TRUE;
8104
8105 return elf_section_type (asec) == elf_section_type (bsec);
8106}
8107\f
c152c796
AM
8108/* Final phase of ELF linker. */
8109
8110/* A structure we use to avoid passing large numbers of arguments. */
8111
8112struct elf_final_link_info
8113{
8114 /* General link information. */
8115 struct bfd_link_info *info;
8116 /* Output BFD. */
8117 bfd *output_bfd;
8118 /* Symbol string table. */
ef10c3ac 8119 struct elf_strtab_hash *symstrtab;
c152c796
AM
8120 /* .hash section. */
8121 asection *hash_sec;
8122 /* symbol version section (.gnu.version). */
8123 asection *symver_sec;
8124 /* Buffer large enough to hold contents of any section. */
8125 bfd_byte *contents;
8126 /* Buffer large enough to hold external relocs of any section. */
8127 void *external_relocs;
8128 /* Buffer large enough to hold internal relocs of any section. */
8129 Elf_Internal_Rela *internal_relocs;
8130 /* Buffer large enough to hold external local symbols of any input
8131 BFD. */
8132 bfd_byte *external_syms;
8133 /* And a buffer for symbol section indices. */
8134 Elf_External_Sym_Shndx *locsym_shndx;
8135 /* Buffer large enough to hold internal local symbols of any input
8136 BFD. */
8137 Elf_Internal_Sym *internal_syms;
8138 /* Array large enough to hold a symbol index for each local symbol
8139 of any input BFD. */
8140 long *indices;
8141 /* Array large enough to hold a section pointer for each local
8142 symbol of any input BFD. */
8143 asection **sections;
ef10c3ac 8144 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8145 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8146 /* Number of STT_FILE syms seen. */
8147 size_t filesym_count;
c152c796
AM
8148};
8149
8150/* This struct is used to pass information to elf_link_output_extsym. */
8151
8152struct elf_outext_info
8153{
8154 bfd_boolean failed;
8155 bfd_boolean localsyms;
34a79995 8156 bfd_boolean file_sym_done;
8b127cbc 8157 struct elf_final_link_info *flinfo;
c152c796
AM
8158};
8159
d9352518
DB
8160
8161/* Support for evaluating a complex relocation.
8162
8163 Complex relocations are generalized, self-describing relocations. The
8164 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8165 relocations themselves.
d9352518
DB
8166
8167 The relocations are use a reserved elf-wide relocation type code (R_RELC
8168 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8169 information (start bit, end bit, word width, etc) into the addend. This
8170 information is extracted from CGEN-generated operand tables within gas.
8171
8172 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8173 internal) representing prefix-notation expressions, including but not
8174 limited to those sorts of expressions normally encoded as addends in the
8175 addend field. The symbol mangling format is:
8176
8177 <node> := <literal>
07d6d2b8
AM
8178 | <unary-operator> ':' <node>
8179 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8180 ;
8181
8182 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8183 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8184 | '#' <hexdigits>
8185 ;
8186
8187 <binary-operator> := as in C
8188 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8189
8190static void
a0c8462f
AM
8191set_symbol_value (bfd *bfd_with_globals,
8192 Elf_Internal_Sym *isymbuf,
8193 size_t locsymcount,
8194 size_t symidx,
8195 bfd_vma val)
d9352518 8196{
8977835c
AM
8197 struct elf_link_hash_entry **sym_hashes;
8198 struct elf_link_hash_entry *h;
8199 size_t extsymoff = locsymcount;
d9352518 8200
8977835c 8201 if (symidx < locsymcount)
d9352518 8202 {
8977835c
AM
8203 Elf_Internal_Sym *sym;
8204
8205 sym = isymbuf + symidx;
8206 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8207 {
8208 /* It is a local symbol: move it to the
8209 "absolute" section and give it a value. */
8210 sym->st_shndx = SHN_ABS;
8211 sym->st_value = val;
8212 return;
8213 }
8214 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8215 extsymoff = 0;
d9352518 8216 }
8977835c
AM
8217
8218 /* It is a global symbol: set its link type
8219 to "defined" and give it a value. */
8220
8221 sym_hashes = elf_sym_hashes (bfd_with_globals);
8222 h = sym_hashes [symidx - extsymoff];
8223 while (h->root.type == bfd_link_hash_indirect
8224 || h->root.type == bfd_link_hash_warning)
8225 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8226 h->root.type = bfd_link_hash_defined;
8227 h->root.u.def.value = val;
8228 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8229}
8230
a0c8462f
AM
8231static bfd_boolean
8232resolve_symbol (const char *name,
8233 bfd *input_bfd,
8b127cbc 8234 struct elf_final_link_info *flinfo,
a0c8462f
AM
8235 bfd_vma *result,
8236 Elf_Internal_Sym *isymbuf,
8237 size_t locsymcount)
d9352518 8238{
a0c8462f
AM
8239 Elf_Internal_Sym *sym;
8240 struct bfd_link_hash_entry *global_entry;
8241 const char *candidate = NULL;
8242 Elf_Internal_Shdr *symtab_hdr;
8243 size_t i;
8244
d9352518
DB
8245 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8246
8247 for (i = 0; i < locsymcount; ++ i)
8248 {
8977835c 8249 sym = isymbuf + i;
d9352518
DB
8250
8251 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8252 continue;
8253
8254 candidate = bfd_elf_string_from_elf_section (input_bfd,
8255 symtab_hdr->sh_link,
8256 sym->st_name);
8257#ifdef DEBUG
0f02bbd9
AM
8258 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8259 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8260#endif
8261 if (candidate && strcmp (candidate, name) == 0)
8262 {
8b127cbc 8263 asection *sec = flinfo->sections [i];
d9352518 8264
0f02bbd9
AM
8265 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8266 *result += sec->output_offset + sec->output_section->vma;
d9352518 8267#ifdef DEBUG
0f02bbd9
AM
8268 printf ("Found symbol with value %8.8lx\n",
8269 (unsigned long) *result);
d9352518
DB
8270#endif
8271 return TRUE;
8272 }
8273 }
8274
8275 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8276 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8277 FALSE, FALSE, TRUE);
d9352518
DB
8278 if (!global_entry)
8279 return FALSE;
a0c8462f 8280
d9352518
DB
8281 if (global_entry->type == bfd_link_hash_defined
8282 || global_entry->type == bfd_link_hash_defweak)
8283 {
a0c8462f
AM
8284 *result = (global_entry->u.def.value
8285 + global_entry->u.def.section->output_section->vma
8286 + global_entry->u.def.section->output_offset);
d9352518 8287#ifdef DEBUG
0f02bbd9
AM
8288 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8289 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8290#endif
8291 return TRUE;
a0c8462f 8292 }
d9352518 8293
d9352518
DB
8294 return FALSE;
8295}
8296
37b01f6a
DG
8297/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8298 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8299 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8300
d9352518 8301static bfd_boolean
a0c8462f
AM
8302resolve_section (const char *name,
8303 asection *sections,
37b01f6a
DG
8304 bfd_vma *result,
8305 bfd * abfd)
d9352518 8306{
a0c8462f
AM
8307 asection *curr;
8308 unsigned int len;
d9352518 8309
a0c8462f 8310 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8311 if (strcmp (curr->name, name) == 0)
8312 {
8313 *result = curr->vma;
8314 return TRUE;
8315 }
8316
8317 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8318 /* FIXME: This could be coded more efficiently... */
a0c8462f 8319 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8320 {
8321 len = strlen (curr->name);
a0c8462f 8322 if (len > strlen (name))
d9352518
DB
8323 continue;
8324
8325 if (strncmp (curr->name, name, len) == 0)
8326 {
8327 if (strncmp (".end", name + len, 4) == 0)
8328 {
37b01f6a 8329 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8330 return TRUE;
8331 }
8332
8333 /* Insert more pseudo-section names here, if you like. */
8334 }
8335 }
a0c8462f 8336
d9352518
DB
8337 return FALSE;
8338}
8339
8340static void
a0c8462f 8341undefined_reference (const char *reftype, const char *name)
d9352518 8342{
695344c0 8343 /* xgettext:c-format */
a0c8462f
AM
8344 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8345 reftype, name);
d9352518
DB
8346}
8347
8348static bfd_boolean
a0c8462f
AM
8349eval_symbol (bfd_vma *result,
8350 const char **symp,
8351 bfd *input_bfd,
8b127cbc 8352 struct elf_final_link_info *flinfo,
a0c8462f
AM
8353 bfd_vma dot,
8354 Elf_Internal_Sym *isymbuf,
8355 size_t locsymcount,
8356 int signed_p)
d9352518 8357{
4b93929b
NC
8358 size_t len;
8359 size_t symlen;
a0c8462f
AM
8360 bfd_vma a;
8361 bfd_vma b;
4b93929b 8362 char symbuf[4096];
0f02bbd9 8363 const char *sym = *symp;
a0c8462f
AM
8364 const char *symend;
8365 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8366
8367 len = strlen (sym);
8368 symend = sym + len;
8369
4b93929b 8370 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8371 {
8372 bfd_set_error (bfd_error_invalid_operation);
8373 return FALSE;
8374 }
a0c8462f 8375
d9352518
DB
8376 switch (* sym)
8377 {
8378 case '.':
0f02bbd9
AM
8379 *result = dot;
8380 *symp = sym + 1;
d9352518
DB
8381 return TRUE;
8382
8383 case '#':
0f02bbd9
AM
8384 ++sym;
8385 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8386 return TRUE;
8387
8388 case 'S':
8389 symbol_is_section = TRUE;
1a0670f3 8390 /* Fall through. */
a0c8462f 8391 case 's':
0f02bbd9
AM
8392 ++sym;
8393 symlen = strtol (sym, (char **) symp, 10);
8394 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8395
4b93929b 8396 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8397 {
8398 bfd_set_error (bfd_error_invalid_operation);
8399 return FALSE;
8400 }
8401
8402 memcpy (symbuf, sym, symlen);
a0c8462f 8403 symbuf[symlen] = '\0';
0f02bbd9 8404 *symp = sym + symlen;
a0c8462f
AM
8405
8406 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8407 the symbol as a section, or vice-versa. so we're pretty liberal in our
8408 interpretation here; section means "try section first", not "must be a
8409 section", and likewise with symbol. */
8410
a0c8462f 8411 if (symbol_is_section)
d9352518 8412 {
37b01f6a 8413 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8414 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8415 isymbuf, locsymcount))
d9352518
DB
8416 {
8417 undefined_reference ("section", symbuf);
8418 return FALSE;
8419 }
a0c8462f
AM
8420 }
8421 else
d9352518 8422 {
8b127cbc 8423 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8424 isymbuf, locsymcount)
8b127cbc 8425 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8426 result, input_bfd))
d9352518
DB
8427 {
8428 undefined_reference ("symbol", symbuf);
8429 return FALSE;
8430 }
8431 }
8432
8433 return TRUE;
a0c8462f 8434
d9352518
DB
8435 /* All that remains are operators. */
8436
8437#define UNARY_OP(op) \
8438 if (strncmp (sym, #op, strlen (#op)) == 0) \
8439 { \
8440 sym += strlen (#op); \
a0c8462f
AM
8441 if (*sym == ':') \
8442 ++sym; \
0f02bbd9 8443 *symp = sym; \
8b127cbc 8444 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8445 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8446 return FALSE; \
8447 if (signed_p) \
0f02bbd9 8448 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8449 else \
8450 *result = op a; \
d9352518
DB
8451 return TRUE; \
8452 }
8453
8454#define BINARY_OP(op) \
8455 if (strncmp (sym, #op, strlen (#op)) == 0) \
8456 { \
8457 sym += strlen (#op); \
a0c8462f
AM
8458 if (*sym == ':') \
8459 ++sym; \
0f02bbd9 8460 *symp = sym; \
8b127cbc 8461 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8462 isymbuf, locsymcount, signed_p)) \
a0c8462f 8463 return FALSE; \
0f02bbd9 8464 ++*symp; \
8b127cbc 8465 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8466 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8467 return FALSE; \
8468 if (signed_p) \
0f02bbd9 8469 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8470 else \
8471 *result = a op b; \
d9352518
DB
8472 return TRUE; \
8473 }
8474
8475 default:
8476 UNARY_OP (0-);
8477 BINARY_OP (<<);
8478 BINARY_OP (>>);
8479 BINARY_OP (==);
8480 BINARY_OP (!=);
8481 BINARY_OP (<=);
8482 BINARY_OP (>=);
8483 BINARY_OP (&&);
8484 BINARY_OP (||);
8485 UNARY_OP (~);
8486 UNARY_OP (!);
8487 BINARY_OP (*);
8488 BINARY_OP (/);
8489 BINARY_OP (%);
8490 BINARY_OP (^);
8491 BINARY_OP (|);
8492 BINARY_OP (&);
8493 BINARY_OP (+);
8494 BINARY_OP (-);
8495 BINARY_OP (<);
8496 BINARY_OP (>);
8497#undef UNARY_OP
8498#undef BINARY_OP
8499 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8500 bfd_set_error (bfd_error_invalid_operation);
8501 return FALSE;
8502 }
8503}
8504
d9352518 8505static void
a0c8462f
AM
8506put_value (bfd_vma size,
8507 unsigned long chunksz,
8508 bfd *input_bfd,
8509 bfd_vma x,
8510 bfd_byte *location)
d9352518
DB
8511{
8512 location += (size - chunksz);
8513
41cd1ad1 8514 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8515 {
8516 switch (chunksz)
8517 {
d9352518
DB
8518 case 1:
8519 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8520 x >>= 8;
d9352518
DB
8521 break;
8522 case 2:
8523 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8524 x >>= 16;
d9352518
DB
8525 break;
8526 case 4:
8527 bfd_put_32 (input_bfd, x, location);
65164438
NC
8528 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8529 x >>= 16;
8530 x >>= 16;
d9352518 8531 break;
d9352518 8532#ifdef BFD64
41cd1ad1 8533 case 8:
d9352518 8534 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8535 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8536 x >>= 32;
8537 x >>= 32;
8538 break;
d9352518 8539#endif
41cd1ad1
NC
8540 default:
8541 abort ();
d9352518
DB
8542 break;
8543 }
8544 }
8545}
8546
a0c8462f
AM
8547static bfd_vma
8548get_value (bfd_vma size,
8549 unsigned long chunksz,
8550 bfd *input_bfd,
8551 bfd_byte *location)
d9352518 8552{
9b239e0e 8553 int shift;
d9352518
DB
8554 bfd_vma x = 0;
8555
9b239e0e
NC
8556 /* Sanity checks. */
8557 BFD_ASSERT (chunksz <= sizeof (x)
8558 && size >= chunksz
8559 && chunksz != 0
8560 && (size % chunksz) == 0
8561 && input_bfd != NULL
8562 && location != NULL);
8563
8564 if (chunksz == sizeof (x))
8565 {
8566 BFD_ASSERT (size == chunksz);
8567
8568 /* Make sure that we do not perform an undefined shift operation.
8569 We know that size == chunksz so there will only be one iteration
8570 of the loop below. */
8571 shift = 0;
8572 }
8573 else
8574 shift = 8 * chunksz;
8575
a0c8462f 8576 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8577 {
8578 switch (chunksz)
8579 {
d9352518 8580 case 1:
9b239e0e 8581 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8582 break;
8583 case 2:
9b239e0e 8584 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8585 break;
8586 case 4:
9b239e0e 8587 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8588 break;
d9352518 8589#ifdef BFD64
9b239e0e
NC
8590 case 8:
8591 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8592 break;
9b239e0e
NC
8593#endif
8594 default:
8595 abort ();
d9352518
DB
8596 }
8597 }
8598 return x;
8599}
8600
a0c8462f
AM
8601static void
8602decode_complex_addend (unsigned long *start, /* in bits */
8603 unsigned long *oplen, /* in bits */
8604 unsigned long *len, /* in bits */
8605 unsigned long *wordsz, /* in bytes */
8606 unsigned long *chunksz, /* in bytes */
8607 unsigned long *lsb0_p,
8608 unsigned long *signed_p,
8609 unsigned long *trunc_p,
8610 unsigned long encoded)
d9352518 8611{
07d6d2b8
AM
8612 * start = encoded & 0x3F;
8613 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8614 * oplen = (encoded >> 12) & 0x3F;
8615 * wordsz = (encoded >> 18) & 0xF;
8616 * chunksz = (encoded >> 22) & 0xF;
8617 * lsb0_p = (encoded >> 27) & 1;
8618 * signed_p = (encoded >> 28) & 1;
8619 * trunc_p = (encoded >> 29) & 1;
8620}
8621
cdfeee4f 8622bfd_reloc_status_type
0f02bbd9 8623bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8624 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8625 bfd_byte *contents,
8626 Elf_Internal_Rela *rel,
8627 bfd_vma relocation)
d9352518 8628{
0f02bbd9
AM
8629 bfd_vma shift, x, mask;
8630 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8631 bfd_reloc_status_type r;
d9352518
DB
8632
8633 /* Perform this reloc, since it is complex.
8634 (this is not to say that it necessarily refers to a complex
8635 symbol; merely that it is a self-describing CGEN based reloc.
8636 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8637 word size, etc) encoded within it.). */
d9352518 8638
a0c8462f
AM
8639 decode_complex_addend (&start, &oplen, &len, &wordsz,
8640 &chunksz, &lsb0_p, &signed_p,
8641 &trunc_p, rel->r_addend);
d9352518
DB
8642
8643 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8644
8645 if (lsb0_p)
8646 shift = (start + 1) - len;
8647 else
8648 shift = (8 * wordsz) - (start + len);
8649
37b01f6a
DG
8650 x = get_value (wordsz, chunksz, input_bfd,
8651 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8652
8653#ifdef DEBUG
8654 printf ("Doing complex reloc: "
8655 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8656 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8657 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8658 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8659 oplen, (unsigned long) x, (unsigned long) mask,
8660 (unsigned long) relocation);
d9352518
DB
8661#endif
8662
cdfeee4f 8663 r = bfd_reloc_ok;
d9352518 8664 if (! trunc_p)
cdfeee4f
AM
8665 /* Now do an overflow check. */
8666 r = bfd_check_overflow ((signed_p
8667 ? complain_overflow_signed
8668 : complain_overflow_unsigned),
8669 len, 0, (8 * wordsz),
8670 relocation);
a0c8462f 8671
d9352518
DB
8672 /* Do the deed. */
8673 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8674
8675#ifdef DEBUG
8676 printf (" relocation: %8.8lx\n"
8677 " shifted mask: %8.8lx\n"
8678 " shifted/masked reloc: %8.8lx\n"
8679 " result: %8.8lx\n",
9ccb8af9
AM
8680 (unsigned long) relocation, (unsigned long) (mask << shift),
8681 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8682#endif
37b01f6a
DG
8683 put_value (wordsz, chunksz, input_bfd, x,
8684 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8685 return r;
d9352518
DB
8686}
8687
0e287786
AM
8688/* Functions to read r_offset from external (target order) reloc
8689 entry. Faster than bfd_getl32 et al, because we let the compiler
8690 know the value is aligned. */
53df40a4 8691
0e287786
AM
8692static bfd_vma
8693ext32l_r_offset (const void *p)
53df40a4
AM
8694{
8695 union aligned32
8696 {
8697 uint32_t v;
8698 unsigned char c[4];
8699 };
8700 const union aligned32 *a
0e287786 8701 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8702
8703 uint32_t aval = ( (uint32_t) a->c[0]
8704 | (uint32_t) a->c[1] << 8
8705 | (uint32_t) a->c[2] << 16
8706 | (uint32_t) a->c[3] << 24);
0e287786 8707 return aval;
53df40a4
AM
8708}
8709
0e287786
AM
8710static bfd_vma
8711ext32b_r_offset (const void *p)
53df40a4
AM
8712{
8713 union aligned32
8714 {
8715 uint32_t v;
8716 unsigned char c[4];
8717 };
8718 const union aligned32 *a
0e287786 8719 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8720
8721 uint32_t aval = ( (uint32_t) a->c[0] << 24
8722 | (uint32_t) a->c[1] << 16
8723 | (uint32_t) a->c[2] << 8
8724 | (uint32_t) a->c[3]);
0e287786 8725 return aval;
53df40a4
AM
8726}
8727
8728#ifdef BFD_HOST_64_BIT
0e287786
AM
8729static bfd_vma
8730ext64l_r_offset (const void *p)
53df40a4
AM
8731{
8732 union aligned64
8733 {
8734 uint64_t v;
8735 unsigned char c[8];
8736 };
8737 const union aligned64 *a
0e287786 8738 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8739
8740 uint64_t aval = ( (uint64_t) a->c[0]
8741 | (uint64_t) a->c[1] << 8
8742 | (uint64_t) a->c[2] << 16
8743 | (uint64_t) a->c[3] << 24
8744 | (uint64_t) a->c[4] << 32
8745 | (uint64_t) a->c[5] << 40
8746 | (uint64_t) a->c[6] << 48
8747 | (uint64_t) a->c[7] << 56);
0e287786 8748 return aval;
53df40a4
AM
8749}
8750
0e287786
AM
8751static bfd_vma
8752ext64b_r_offset (const void *p)
53df40a4
AM
8753{
8754 union aligned64
8755 {
8756 uint64_t v;
8757 unsigned char c[8];
8758 };
8759 const union aligned64 *a
0e287786 8760 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8761
8762 uint64_t aval = ( (uint64_t) a->c[0] << 56
8763 | (uint64_t) a->c[1] << 48
8764 | (uint64_t) a->c[2] << 40
8765 | (uint64_t) a->c[3] << 32
8766 | (uint64_t) a->c[4] << 24
8767 | (uint64_t) a->c[5] << 16
8768 | (uint64_t) a->c[6] << 8
8769 | (uint64_t) a->c[7]);
0e287786 8770 return aval;
53df40a4
AM
8771}
8772#endif
8773
c152c796
AM
8774/* When performing a relocatable link, the input relocations are
8775 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8776 referenced must be updated. Update all the relocations found in
8777 RELDATA. */
c152c796 8778
bca6d0e3 8779static bfd_boolean
c152c796 8780elf_link_adjust_relocs (bfd *abfd,
9eaff861 8781 asection *sec,
28dbcedc 8782 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8783 bfd_boolean sort,
8784 struct bfd_link_info *info)
c152c796
AM
8785{
8786 unsigned int i;
8787 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8788 bfd_byte *erela;
8789 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8790 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8791 bfd_vma r_type_mask;
8792 int r_sym_shift;
d4730f92
BS
8793 unsigned int count = reldata->count;
8794 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8795
d4730f92 8796 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8797 {
8798 swap_in = bed->s->swap_reloc_in;
8799 swap_out = bed->s->swap_reloc_out;
8800 }
d4730f92 8801 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8802 {
8803 swap_in = bed->s->swap_reloca_in;
8804 swap_out = bed->s->swap_reloca_out;
8805 }
8806 else
8807 abort ();
8808
8809 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8810 abort ();
8811
8812 if (bed->s->arch_size == 32)
8813 {
8814 r_type_mask = 0xff;
8815 r_sym_shift = 8;
8816 }
8817 else
8818 {
8819 r_type_mask = 0xffffffff;
8820 r_sym_shift = 32;
8821 }
8822
d4730f92
BS
8823 erela = reldata->hdr->contents;
8824 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8825 {
8826 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8827 unsigned int j;
8828
8829 if (*rel_hash == NULL)
8830 continue;
8831
10bbbc1d
NC
8832 if ((*rel_hash)->indx == -2
8833 && info->gc_sections
8834 && ! info->gc_keep_exported)
8835 {
8836 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8837 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8838 abfd, sec,
8839 (*rel_hash)->root.root.string);
9793eb77 8840 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8841 abfd, sec);
10bbbc1d
NC
8842 bfd_set_error (bfd_error_invalid_operation);
8843 return FALSE;
8844 }
c152c796
AM
8845 BFD_ASSERT ((*rel_hash)->indx >= 0);
8846
8847 (*swap_in) (abfd, erela, irela);
8848 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8849 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8850 | (irela[j].r_info & r_type_mask));
8851 (*swap_out) (abfd, irela, erela);
8852 }
53df40a4 8853
9eaff861
AO
8854 if (bed->elf_backend_update_relocs)
8855 (*bed->elf_backend_update_relocs) (sec, reldata);
8856
0e287786 8857 if (sort && count != 0)
53df40a4 8858 {
0e287786
AM
8859 bfd_vma (*ext_r_off) (const void *);
8860 bfd_vma r_off;
8861 size_t elt_size;
8862 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8863 bfd_byte *buf = NULL;
28dbcedc
AM
8864
8865 if (bed->s->arch_size == 32)
8866 {
8867 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8868 ext_r_off = ext32l_r_offset;
28dbcedc 8869 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8870 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8871 else
8872 abort ();
8873 }
53df40a4 8874 else
28dbcedc 8875 {
53df40a4 8876#ifdef BFD_HOST_64_BIT
28dbcedc 8877 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8878 ext_r_off = ext64l_r_offset;
28dbcedc 8879 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8880 ext_r_off = ext64b_r_offset;
28dbcedc 8881 else
53df40a4 8882#endif
28dbcedc
AM
8883 abort ();
8884 }
0e287786 8885
bca6d0e3
AM
8886 /* Must use a stable sort here. A modified insertion sort,
8887 since the relocs are mostly sorted already. */
0e287786
AM
8888 elt_size = reldata->hdr->sh_entsize;
8889 base = reldata->hdr->contents;
8890 end = base + count * elt_size;
bca6d0e3 8891 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8892 abort ();
8893
8894 /* Ensure the first element is lowest. This acts as a sentinel,
8895 speeding the main loop below. */
8896 r_off = (*ext_r_off) (base);
8897 for (p = loc = base; (p += elt_size) < end; )
8898 {
8899 bfd_vma r_off2 = (*ext_r_off) (p);
8900 if (r_off > r_off2)
8901 {
8902 r_off = r_off2;
8903 loc = p;
8904 }
8905 }
8906 if (loc != base)
8907 {
8908 /* Don't just swap *base and *loc as that changes the order
8909 of the original base[0] and base[1] if they happen to
8910 have the same r_offset. */
bca6d0e3
AM
8911 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8912 memcpy (onebuf, loc, elt_size);
0e287786 8913 memmove (base + elt_size, base, loc - base);
bca6d0e3 8914 memcpy (base, onebuf, elt_size);
0e287786
AM
8915 }
8916
b29b8669 8917 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8918 {
8919 /* base to p is sorted, *p is next to insert. */
8920 r_off = (*ext_r_off) (p);
8921 /* Search the sorted region for location to insert. */
8922 loc = p - elt_size;
8923 while (r_off < (*ext_r_off) (loc))
8924 loc -= elt_size;
8925 loc += elt_size;
8926 if (loc != p)
8927 {
bca6d0e3
AM
8928 /* Chances are there is a run of relocs to insert here,
8929 from one of more input files. Files are not always
8930 linked in order due to the way elf_link_input_bfd is
8931 called. See pr17666. */
8932 size_t sortlen = p - loc;
8933 bfd_vma r_off2 = (*ext_r_off) (loc);
8934 size_t runlen = elt_size;
8935 size_t buf_size = 96 * 1024;
8936 while (p + runlen < end
8937 && (sortlen <= buf_size
8938 || runlen + elt_size <= buf_size)
8939 && r_off2 > (*ext_r_off) (p + runlen))
8940 runlen += elt_size;
8941 if (buf == NULL)
8942 {
8943 buf = bfd_malloc (buf_size);
8944 if (buf == NULL)
8945 return FALSE;
8946 }
8947 if (runlen < sortlen)
8948 {
8949 memcpy (buf, p, runlen);
8950 memmove (loc + runlen, loc, sortlen);
8951 memcpy (loc, buf, runlen);
8952 }
8953 else
8954 {
8955 memcpy (buf, loc, sortlen);
8956 memmove (loc, p, runlen);
8957 memcpy (loc + runlen, buf, sortlen);
8958 }
b29b8669 8959 p += runlen - elt_size;
0e287786
AM
8960 }
8961 }
8962 /* Hashes are no longer valid. */
28dbcedc
AM
8963 free (reldata->hashes);
8964 reldata->hashes = NULL;
bca6d0e3 8965 free (buf);
53df40a4 8966 }
bca6d0e3 8967 return TRUE;
c152c796
AM
8968}
8969
8970struct elf_link_sort_rela
8971{
8972 union {
8973 bfd_vma offset;
8974 bfd_vma sym_mask;
8975 } u;
8976 enum elf_reloc_type_class type;
8977 /* We use this as an array of size int_rels_per_ext_rel. */
8978 Elf_Internal_Rela rela[1];
8979};
8980
8981static int
8982elf_link_sort_cmp1 (const void *A, const void *B)
8983{
a50b1753
NC
8984 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8985 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8986 int relativea, relativeb;
8987
8988 relativea = a->type == reloc_class_relative;
8989 relativeb = b->type == reloc_class_relative;
8990
8991 if (relativea < relativeb)
8992 return 1;
8993 if (relativea > relativeb)
8994 return -1;
8995 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8996 return -1;
8997 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8998 return 1;
8999 if (a->rela->r_offset < b->rela->r_offset)
9000 return -1;
9001 if (a->rela->r_offset > b->rela->r_offset)
9002 return 1;
9003 return 0;
9004}
9005
9006static int
9007elf_link_sort_cmp2 (const void *A, const void *B)
9008{
a50b1753
NC
9009 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9010 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9011
7e612e98 9012 if (a->type < b->type)
c152c796 9013 return -1;
7e612e98 9014 if (a->type > b->type)
c152c796 9015 return 1;
7e612e98 9016 if (a->u.offset < b->u.offset)
c152c796 9017 return -1;
7e612e98 9018 if (a->u.offset > b->u.offset)
c152c796
AM
9019 return 1;
9020 if (a->rela->r_offset < b->rela->r_offset)
9021 return -1;
9022 if (a->rela->r_offset > b->rela->r_offset)
9023 return 1;
9024 return 0;
9025}
9026
9027static size_t
9028elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9029{
3410fea8 9030 asection *dynamic_relocs;
fc66a176
L
9031 asection *rela_dyn;
9032 asection *rel_dyn;
c152c796
AM
9033 bfd_size_type count, size;
9034 size_t i, ret, sort_elt, ext_size;
9035 bfd_byte *sort, *s_non_relative, *p;
9036 struct elf_link_sort_rela *sq;
9037 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9038 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9039 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9040 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9041 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9042 struct bfd_link_order *lo;
9043 bfd_vma r_sym_mask;
3410fea8 9044 bfd_boolean use_rela;
c152c796 9045
3410fea8
NC
9046 /* Find a dynamic reloc section. */
9047 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9048 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9049 if (rela_dyn != NULL && rela_dyn->size > 0
9050 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9051 {
3410fea8
NC
9052 bfd_boolean use_rela_initialised = FALSE;
9053
9054 /* This is just here to stop gcc from complaining.
c8e44c6d 9055 Its initialization checking code is not perfect. */
3410fea8
NC
9056 use_rela = TRUE;
9057
9058 /* Both sections are present. Examine the sizes
9059 of the indirect sections to help us choose. */
9060 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9061 if (lo->type == bfd_indirect_link_order)
9062 {
9063 asection *o = lo->u.indirect.section;
9064
9065 if ((o->size % bed->s->sizeof_rela) == 0)
9066 {
9067 if ((o->size % bed->s->sizeof_rel) == 0)
9068 /* Section size is divisible by both rel and rela sizes.
9069 It is of no help to us. */
9070 ;
9071 else
9072 {
9073 /* Section size is only divisible by rela. */
535b785f 9074 if (use_rela_initialised && !use_rela)
3410fea8 9075 {
9793eb77 9076 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9077 "they are in more than one size"),
9078 abfd);
3410fea8
NC
9079 bfd_set_error (bfd_error_invalid_operation);
9080 return 0;
9081 }
9082 else
9083 {
9084 use_rela = TRUE;
9085 use_rela_initialised = TRUE;
9086 }
9087 }
9088 }
9089 else if ((o->size % bed->s->sizeof_rel) == 0)
9090 {
9091 /* Section size is only divisible by rel. */
535b785f 9092 if (use_rela_initialised && use_rela)
3410fea8 9093 {
9793eb77 9094 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9095 "they are in more than one size"),
9096 abfd);
3410fea8
NC
9097 bfd_set_error (bfd_error_invalid_operation);
9098 return 0;
9099 }
9100 else
9101 {
9102 use_rela = FALSE;
9103 use_rela_initialised = TRUE;
9104 }
9105 }
9106 else
9107 {
c8e44c6d
AM
9108 /* The section size is not divisible by either -
9109 something is wrong. */
9793eb77 9110 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9111 "they are of an unknown size"), abfd);
3410fea8
NC
9112 bfd_set_error (bfd_error_invalid_operation);
9113 return 0;
9114 }
9115 }
9116
9117 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9118 if (lo->type == bfd_indirect_link_order)
9119 {
9120 asection *o = lo->u.indirect.section;
9121
9122 if ((o->size % bed->s->sizeof_rela) == 0)
9123 {
9124 if ((o->size % bed->s->sizeof_rel) == 0)
9125 /* Section size is divisible by both rel and rela sizes.
9126 It is of no help to us. */
9127 ;
9128 else
9129 {
9130 /* Section size is only divisible by rela. */
535b785f 9131 if (use_rela_initialised && !use_rela)
3410fea8 9132 {
9793eb77 9133 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9134 "they are in more than one size"),
9135 abfd);
3410fea8
NC
9136 bfd_set_error (bfd_error_invalid_operation);
9137 return 0;
9138 }
9139 else
9140 {
9141 use_rela = TRUE;
9142 use_rela_initialised = TRUE;
9143 }
9144 }
9145 }
9146 else if ((o->size % bed->s->sizeof_rel) == 0)
9147 {
9148 /* Section size is only divisible by rel. */
535b785f 9149 if (use_rela_initialised && use_rela)
3410fea8 9150 {
9793eb77 9151 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9152 "they are in more than one size"),
9153 abfd);
3410fea8
NC
9154 bfd_set_error (bfd_error_invalid_operation);
9155 return 0;
9156 }
9157 else
9158 {
9159 use_rela = FALSE;
9160 use_rela_initialised = TRUE;
9161 }
9162 }
9163 else
9164 {
c8e44c6d
AM
9165 /* The section size is not divisible by either -
9166 something is wrong. */
9793eb77 9167 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9168 "they are of an unknown size"), abfd);
3410fea8
NC
9169 bfd_set_error (bfd_error_invalid_operation);
9170 return 0;
9171 }
9172 }
9173
9174 if (! use_rela_initialised)
9175 /* Make a guess. */
9176 use_rela = TRUE;
c152c796 9177 }
fc66a176
L
9178 else if (rela_dyn != NULL && rela_dyn->size > 0)
9179 use_rela = TRUE;
9180 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9181 use_rela = FALSE;
c152c796 9182 else
fc66a176 9183 return 0;
3410fea8
NC
9184
9185 if (use_rela)
c152c796 9186 {
3410fea8 9187 dynamic_relocs = rela_dyn;
c152c796
AM
9188 ext_size = bed->s->sizeof_rela;
9189 swap_in = bed->s->swap_reloca_in;
9190 swap_out = bed->s->swap_reloca_out;
9191 }
3410fea8
NC
9192 else
9193 {
9194 dynamic_relocs = rel_dyn;
9195 ext_size = bed->s->sizeof_rel;
9196 swap_in = bed->s->swap_reloc_in;
9197 swap_out = bed->s->swap_reloc_out;
9198 }
c152c796
AM
9199
9200 size = 0;
3410fea8 9201 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9202 if (lo->type == bfd_indirect_link_order)
3410fea8 9203 size += lo->u.indirect.section->size;
c152c796 9204
3410fea8 9205 if (size != dynamic_relocs->size)
c152c796
AM
9206 return 0;
9207
9208 sort_elt = (sizeof (struct elf_link_sort_rela)
9209 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9210
9211 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9212 if (count == 0)
9213 return 0;
a50b1753 9214 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9215
c152c796
AM
9216 if (sort == NULL)
9217 {
9218 (*info->callbacks->warning)
9793eb77 9219 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9220 return 0;
9221 }
9222
9223 if (bed->s->arch_size == 32)
9224 r_sym_mask = ~(bfd_vma) 0xff;
9225 else
9226 r_sym_mask = ~(bfd_vma) 0xffffffff;
9227
3410fea8 9228 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9229 if (lo->type == bfd_indirect_link_order)
9230 {
9231 bfd_byte *erel, *erelend;
9232 asection *o = lo->u.indirect.section;
9233
1da212d6
AM
9234 if (o->contents == NULL && o->size != 0)
9235 {
9236 /* This is a reloc section that is being handled as a normal
9237 section. See bfd_section_from_shdr. We can't combine
9238 relocs in this case. */
9239 free (sort);
9240 return 0;
9241 }
c152c796 9242 erel = o->contents;
eea6121a 9243 erelend = o->contents + o->size;
c8e44c6d 9244 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9245
c152c796
AM
9246 while (erel < erelend)
9247 {
9248 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9249
c152c796 9250 (*swap_in) (abfd, erel, s->rela);
7e612e98 9251 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9252 s->u.sym_mask = r_sym_mask;
9253 p += sort_elt;
9254 erel += ext_size;
9255 }
9256 }
9257
9258 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9259
9260 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9261 {
9262 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9263 if (s->type != reloc_class_relative)
9264 break;
9265 }
9266 ret = i;
9267 s_non_relative = p;
9268
9269 sq = (struct elf_link_sort_rela *) s_non_relative;
9270 for (; i < count; i++, p += sort_elt)
9271 {
9272 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9273 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9274 sq = sp;
9275 sp->u.offset = sq->rela->r_offset;
9276 }
9277
9278 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9279
c8e44c6d
AM
9280 struct elf_link_hash_table *htab = elf_hash_table (info);
9281 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9282 {
9283 /* We have plt relocs in .rela.dyn. */
9284 sq = (struct elf_link_sort_rela *) sort;
9285 for (i = 0; i < count; i++)
9286 if (sq[count - i - 1].type != reloc_class_plt)
9287 break;
9288 if (i != 0 && htab->srelplt->size == i * ext_size)
9289 {
9290 struct bfd_link_order **plo;
9291 /* Put srelplt link_order last. This is so the output_offset
9292 set in the next loop is correct for DT_JMPREL. */
9293 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9294 if ((*plo)->type == bfd_indirect_link_order
9295 && (*plo)->u.indirect.section == htab->srelplt)
9296 {
9297 lo = *plo;
9298 *plo = lo->next;
9299 }
9300 else
9301 plo = &(*plo)->next;
9302 *plo = lo;
9303 lo->next = NULL;
9304 dynamic_relocs->map_tail.link_order = lo;
9305 }
9306 }
9307
9308 p = sort;
3410fea8 9309 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9310 if (lo->type == bfd_indirect_link_order)
9311 {
9312 bfd_byte *erel, *erelend;
9313 asection *o = lo->u.indirect.section;
9314
9315 erel = o->contents;
eea6121a 9316 erelend = o->contents + o->size;
c8e44c6d 9317 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9318 while (erel < erelend)
9319 {
9320 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9321 (*swap_out) (abfd, s->rela, erel);
9322 p += sort_elt;
9323 erel += ext_size;
9324 }
9325 }
9326
9327 free (sort);
3410fea8 9328 *psec = dynamic_relocs;
c152c796
AM
9329 return ret;
9330}
9331
ef10c3ac 9332/* Add a symbol to the output symbol string table. */
c152c796 9333
6e0b88f1 9334static int
ef10c3ac
L
9335elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9336 const char *name,
9337 Elf_Internal_Sym *elfsym,
9338 asection *input_sec,
9339 struct elf_link_hash_entry *h)
c152c796 9340{
6e0b88f1 9341 int (*output_symbol_hook)
c152c796
AM
9342 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9343 struct elf_link_hash_entry *);
ef10c3ac 9344 struct elf_link_hash_table *hash_table;
c152c796 9345 const struct elf_backend_data *bed;
ef10c3ac 9346 bfd_size_type strtabsize;
c152c796 9347
8539e4e8
AM
9348 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9349
8b127cbc 9350 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9351 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9352 if (output_symbol_hook != NULL)
9353 {
8b127cbc 9354 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9355 if (ret != 1)
9356 return ret;
c152c796
AM
9357 }
9358
ef10c3ac
L
9359 if (name == NULL
9360 || *name == '\0'
9361 || (input_sec->flags & SEC_EXCLUDE))
9362 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9363 else
9364 {
ef10c3ac
L
9365 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9366 to get the final offset for st_name. */
9367 elfsym->st_name
9368 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9369 name, FALSE);
c152c796 9370 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9371 return 0;
c152c796
AM
9372 }
9373
ef10c3ac
L
9374 hash_table = elf_hash_table (flinfo->info);
9375 strtabsize = hash_table->strtabsize;
9376 if (strtabsize <= hash_table->strtabcount)
c152c796 9377 {
ef10c3ac
L
9378 strtabsize += strtabsize;
9379 hash_table->strtabsize = strtabsize;
9380 strtabsize *= sizeof (*hash_table->strtab);
9381 hash_table->strtab
9382 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9383 strtabsize);
9384 if (hash_table->strtab == NULL)
6e0b88f1 9385 return 0;
c152c796 9386 }
ef10c3ac
L
9387 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9388 hash_table->strtab[hash_table->strtabcount].dest_index
9389 = hash_table->strtabcount;
9390 hash_table->strtab[hash_table->strtabcount].destshndx_index
9391 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9392
9393 bfd_get_symcount (flinfo->output_bfd) += 1;
9394 hash_table->strtabcount += 1;
9395
9396 return 1;
9397}
9398
9399/* Swap symbols out to the symbol table and flush the output symbols to
9400 the file. */
9401
9402static bfd_boolean
9403elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9404{
9405 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9406 bfd_size_type amt;
9407 size_t i;
ef10c3ac
L
9408 const struct elf_backend_data *bed;
9409 bfd_byte *symbuf;
9410 Elf_Internal_Shdr *hdr;
9411 file_ptr pos;
9412 bfd_boolean ret;
9413
9414 if (!hash_table->strtabcount)
9415 return TRUE;
9416
9417 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9418
9419 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9420
ef10c3ac
L
9421 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9422 symbuf = (bfd_byte *) bfd_malloc (amt);
9423 if (symbuf == NULL)
9424 return FALSE;
1b786873 9425
ef10c3ac 9426 if (flinfo->symshndxbuf)
c152c796 9427 {
ef53be89
AM
9428 amt = sizeof (Elf_External_Sym_Shndx);
9429 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9430 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9431 if (flinfo->symshndxbuf == NULL)
c152c796 9432 {
ef10c3ac
L
9433 free (symbuf);
9434 return FALSE;
c152c796 9435 }
c152c796
AM
9436 }
9437
ef10c3ac
L
9438 for (i = 0; i < hash_table->strtabcount; i++)
9439 {
9440 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9441 if (elfsym->sym.st_name == (unsigned long) -1)
9442 elfsym->sym.st_name = 0;
9443 else
9444 elfsym->sym.st_name
9445 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9446 elfsym->sym.st_name);
9447 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9448 ((bfd_byte *) symbuf
9449 + (elfsym->dest_index
9450 * bed->s->sizeof_sym)),
9451 (flinfo->symshndxbuf
9452 + elfsym->destshndx_index));
9453 }
9454
9455 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9456 pos = hdr->sh_offset + hdr->sh_size;
9457 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9458 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9459 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9460 {
9461 hdr->sh_size += amt;
9462 ret = TRUE;
9463 }
9464 else
9465 ret = FALSE;
c152c796 9466
ef10c3ac
L
9467 free (symbuf);
9468
9469 free (hash_table->strtab);
9470 hash_table->strtab = NULL;
9471
9472 return ret;
c152c796
AM
9473}
9474
c0d5a53d
L
9475/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9476
9477static bfd_boolean
9478check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9479{
4fbb74a6
AM
9480 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9481 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9482 {
9483 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9484 beyond 64k. */
4eca0228 9485 _bfd_error_handler
695344c0 9486 /* xgettext:c-format */
9793eb77 9487 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9488 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9489 bfd_set_error (bfd_error_nonrepresentable_section);
9490 return FALSE;
9491 }
9492 return TRUE;
9493}
9494
c152c796
AM
9495/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9496 allowing an unsatisfied unversioned symbol in the DSO to match a
9497 versioned symbol that would normally require an explicit version.
9498 We also handle the case that a DSO references a hidden symbol
9499 which may be satisfied by a versioned symbol in another DSO. */
9500
9501static bfd_boolean
9502elf_link_check_versioned_symbol (struct bfd_link_info *info,
9503 const struct elf_backend_data *bed,
9504 struct elf_link_hash_entry *h)
9505{
9506 bfd *abfd;
9507 struct elf_link_loaded_list *loaded;
9508
9509 if (!is_elf_hash_table (info->hash))
9510 return FALSE;
9511
90c984fc
L
9512 /* Check indirect symbol. */
9513 while (h->root.type == bfd_link_hash_indirect)
9514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9515
c152c796
AM
9516 switch (h->root.type)
9517 {
9518 default:
9519 abfd = NULL;
9520 break;
9521
9522 case bfd_link_hash_undefined:
9523 case bfd_link_hash_undefweak:
9524 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9525 if (abfd == NULL
9526 || (abfd->flags & DYNAMIC) == 0
e56f61be 9527 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9528 return FALSE;
9529 break;
9530
9531 case bfd_link_hash_defined:
9532 case bfd_link_hash_defweak:
9533 abfd = h->root.u.def.section->owner;
9534 break;
9535
9536 case bfd_link_hash_common:
9537 abfd = h->root.u.c.p->section->owner;
9538 break;
9539 }
9540 BFD_ASSERT (abfd != NULL);
9541
9542 for (loaded = elf_hash_table (info)->loaded;
9543 loaded != NULL;
9544 loaded = loaded->next)
9545 {
9546 bfd *input;
9547 Elf_Internal_Shdr *hdr;
ef53be89
AM
9548 size_t symcount;
9549 size_t extsymcount;
9550 size_t extsymoff;
c152c796
AM
9551 Elf_Internal_Shdr *versymhdr;
9552 Elf_Internal_Sym *isym;
9553 Elf_Internal_Sym *isymend;
9554 Elf_Internal_Sym *isymbuf;
9555 Elf_External_Versym *ever;
9556 Elf_External_Versym *extversym;
9557
9558 input = loaded->abfd;
9559
9560 /* We check each DSO for a possible hidden versioned definition. */
9561 if (input == abfd
9562 || (input->flags & DYNAMIC) == 0
9563 || elf_dynversym (input) == 0)
9564 continue;
9565
9566 hdr = &elf_tdata (input)->dynsymtab_hdr;
9567
9568 symcount = hdr->sh_size / bed->s->sizeof_sym;
9569 if (elf_bad_symtab (input))
9570 {
9571 extsymcount = symcount;
9572 extsymoff = 0;
9573 }
9574 else
9575 {
9576 extsymcount = symcount - hdr->sh_info;
9577 extsymoff = hdr->sh_info;
9578 }
9579
9580 if (extsymcount == 0)
9581 continue;
9582
9583 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9584 NULL, NULL, NULL);
9585 if (isymbuf == NULL)
9586 return FALSE;
9587
9588 /* Read in any version definitions. */
9589 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9590 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9591 if (extversym == NULL)
9592 goto error_ret;
9593
9594 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9595 || (bfd_bread (extversym, versymhdr->sh_size, input)
9596 != versymhdr->sh_size))
9597 {
9598 free (extversym);
9599 error_ret:
9600 free (isymbuf);
9601 return FALSE;
9602 }
9603
9604 ever = extversym + extsymoff;
9605 isymend = isymbuf + extsymcount;
9606 for (isym = isymbuf; isym < isymend; isym++, ever++)
9607 {
9608 const char *name;
9609 Elf_Internal_Versym iver;
9610 unsigned short version_index;
9611
9612 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9613 || isym->st_shndx == SHN_UNDEF)
9614 continue;
9615
9616 name = bfd_elf_string_from_elf_section (input,
9617 hdr->sh_link,
9618 isym->st_name);
9619 if (strcmp (name, h->root.root.string) != 0)
9620 continue;
9621
9622 _bfd_elf_swap_versym_in (input, ever, &iver);
9623
d023c380
L
9624 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9625 && !(h->def_regular
9626 && h->forced_local))
c152c796
AM
9627 {
9628 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9629 have provided a definition for the undefined sym unless
9630 it is defined in a non-shared object and forced local.
9631 */
c152c796
AM
9632 abort ();
9633 }
9634
9635 version_index = iver.vs_vers & VERSYM_VERSION;
9636 if (version_index == 1 || version_index == 2)
9637 {
9638 /* This is the base or first version. We can use it. */
9639 free (extversym);
9640 free (isymbuf);
9641 return TRUE;
9642 }
9643 }
9644
9645 free (extversym);
9646 free (isymbuf);
9647 }
9648
9649 return FALSE;
9650}
9651
b8871f35
L
9652/* Convert ELF common symbol TYPE. */
9653
9654static int
9655elf_link_convert_common_type (struct bfd_link_info *info, int type)
9656{
9657 /* Commom symbol can only appear in relocatable link. */
9658 if (!bfd_link_relocatable (info))
9659 abort ();
9660 switch (info->elf_stt_common)
9661 {
9662 case unchanged:
9663 break;
9664 case elf_stt_common:
9665 type = STT_COMMON;
9666 break;
9667 case no_elf_stt_common:
9668 type = STT_OBJECT;
9669 break;
9670 }
9671 return type;
9672}
9673
c152c796
AM
9674/* Add an external symbol to the symbol table. This is called from
9675 the hash table traversal routine. When generating a shared object,
9676 we go through the symbol table twice. The first time we output
9677 anything that might have been forced to local scope in a version
9678 script. The second time we output the symbols that are still
9679 global symbols. */
9680
9681static bfd_boolean
7686d77d 9682elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9683{
7686d77d 9684 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9685 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9686 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9687 bfd_boolean strip;
9688 Elf_Internal_Sym sym;
9689 asection *input_sec;
9690 const struct elf_backend_data *bed;
6e0b88f1
AM
9691 long indx;
9692 int ret;
b8871f35 9693 unsigned int type;
c152c796
AM
9694
9695 if (h->root.type == bfd_link_hash_warning)
9696 {
9697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9698 if (h->root.type == bfd_link_hash_new)
9699 return TRUE;
9700 }
9701
9702 /* Decide whether to output this symbol in this pass. */
9703 if (eoinfo->localsyms)
9704 {
4deb8f71 9705 if (!h->forced_local)
c152c796
AM
9706 return TRUE;
9707 }
9708 else
9709 {
4deb8f71 9710 if (h->forced_local)
c152c796
AM
9711 return TRUE;
9712 }
9713
8b127cbc 9714 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9715
12ac1cf5 9716 if (h->root.type == bfd_link_hash_undefined)
c152c796 9717 {
12ac1cf5
NC
9718 /* If we have an undefined symbol reference here then it must have
9719 come from a shared library that is being linked in. (Undefined
98da7939
L
9720 references in regular files have already been handled unless
9721 they are in unreferenced sections which are removed by garbage
9722 collection). */
12ac1cf5
NC
9723 bfd_boolean ignore_undef = FALSE;
9724
9725 /* Some symbols may be special in that the fact that they're
9726 undefined can be safely ignored - let backend determine that. */
9727 if (bed->elf_backend_ignore_undef_symbol)
9728 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9729
9730 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9731 if (!ignore_undef
12ac1cf5 9732 && h->ref_dynamic
8b127cbc
AM
9733 && (!h->ref_regular || flinfo->info->gc_sections)
9734 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9735 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9736 (*flinfo->info->callbacks->undefined_symbol)
9737 (flinfo->info, h->root.root.string,
9738 h->ref_regular ? NULL : h->root.u.undef.abfd,
9739 NULL, 0,
9740 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9741
9742 /* Strip a global symbol defined in a discarded section. */
9743 if (h->indx == -3)
9744 return TRUE;
c152c796
AM
9745 }
9746
9747 /* We should also warn if a forced local symbol is referenced from
9748 shared libraries. */
0e1862bb 9749 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9750 && h->forced_local
9751 && h->ref_dynamic
371a5866 9752 && h->def_regular
f5385ebf 9753 && !h->dynamic_def
ee659f1f 9754 && h->ref_dynamic_nonweak
8b127cbc 9755 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9756 {
17d078c5
AM
9757 bfd *def_bfd;
9758 const char *msg;
90c984fc
L
9759 struct elf_link_hash_entry *hi = h;
9760
9761 /* Check indirect symbol. */
9762 while (hi->root.type == bfd_link_hash_indirect)
9763 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9764
9765 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9766 /* xgettext:c-format */
871b3ab2 9767 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9768 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9769 /* xgettext:c-format */
871b3ab2 9770 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9771 else
695344c0 9772 /* xgettext:c-format */
871b3ab2 9773 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9774 def_bfd = flinfo->output_bfd;
90c984fc
L
9775 if (hi->root.u.def.section != bfd_abs_section_ptr)
9776 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9777 _bfd_error_handler (msg, flinfo->output_bfd,
9778 h->root.root.string, def_bfd);
17d078c5 9779 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9780 eoinfo->failed = TRUE;
9781 return FALSE;
9782 }
9783
9784 /* We don't want to output symbols that have never been mentioned by
9785 a regular file, or that we have been told to strip. However, if
9786 h->indx is set to -2, the symbol is used by a reloc and we must
9787 output it. */
d983c8c5 9788 strip = FALSE;
c152c796 9789 if (h->indx == -2)
d983c8c5 9790 ;
f5385ebf 9791 else if ((h->def_dynamic
77cfaee6
AM
9792 || h->ref_dynamic
9793 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9794 && !h->def_regular
9795 && !h->ref_regular)
c152c796 9796 strip = TRUE;
8b127cbc 9797 else if (flinfo->info->strip == strip_all)
c152c796 9798 strip = TRUE;
8b127cbc
AM
9799 else if (flinfo->info->strip == strip_some
9800 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9801 h->root.root.string, FALSE, FALSE) == NULL)
9802 strip = TRUE;
d56d55e7
AM
9803 else if ((h->root.type == bfd_link_hash_defined
9804 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9805 && ((flinfo->info->strip_discarded
dbaa2011 9806 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9807 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9808 && h->root.u.def.section->owner != NULL
d56d55e7 9809 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9810 strip = TRUE;
9e2278f5
AM
9811 else if ((h->root.type == bfd_link_hash_undefined
9812 || h->root.type == bfd_link_hash_undefweak)
9813 && h->root.u.undef.abfd != NULL
9814 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9815 strip = TRUE;
c152c796 9816
b8871f35
L
9817 type = h->type;
9818
c152c796 9819 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9820 nothing else to do. However, if it is a forced local symbol or
9821 an ifunc symbol we need to give the backend finish_dynamic_symbol
9822 function a chance to make it dynamic. */
c152c796
AM
9823 if (strip
9824 && h->dynindx == -1
b8871f35 9825 && type != STT_GNU_IFUNC
f5385ebf 9826 && !h->forced_local)
c152c796
AM
9827 return TRUE;
9828
9829 sym.st_value = 0;
9830 sym.st_size = h->size;
9831 sym.st_other = h->other;
c152c796
AM
9832 switch (h->root.type)
9833 {
9834 default:
9835 case bfd_link_hash_new:
9836 case bfd_link_hash_warning:
9837 abort ();
9838 return FALSE;
9839
9840 case bfd_link_hash_undefined:
9841 case bfd_link_hash_undefweak:
9842 input_sec = bfd_und_section_ptr;
9843 sym.st_shndx = SHN_UNDEF;
9844 break;
9845
9846 case bfd_link_hash_defined:
9847 case bfd_link_hash_defweak:
9848 {
9849 input_sec = h->root.u.def.section;
9850 if (input_sec->output_section != NULL)
9851 {
9852 sym.st_shndx =
8b127cbc 9853 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9854 input_sec->output_section);
9855 if (sym.st_shndx == SHN_BAD)
9856 {
4eca0228 9857 _bfd_error_handler
695344c0 9858 /* xgettext:c-format */
871b3ab2 9859 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9860 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9861 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9862 eoinfo->failed = TRUE;
9863 return FALSE;
9864 }
9865
9866 /* ELF symbols in relocatable files are section relative,
9867 but in nonrelocatable files they are virtual
9868 addresses. */
9869 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9870 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9871 {
9872 sym.st_value += input_sec->output_section->vma;
9873 if (h->type == STT_TLS)
9874 {
8b127cbc 9875 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9876 if (tls_sec != NULL)
9877 sym.st_value -= tls_sec->vma;
c152c796
AM
9878 }
9879 }
9880 }
9881 else
9882 {
9883 BFD_ASSERT (input_sec->owner == NULL
9884 || (input_sec->owner->flags & DYNAMIC) != 0);
9885 sym.st_shndx = SHN_UNDEF;
9886 input_sec = bfd_und_section_ptr;
9887 }
9888 }
9889 break;
9890
9891 case bfd_link_hash_common:
9892 input_sec = h->root.u.c.p->section;
a4d8e49b 9893 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9894 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9895 break;
9896
9897 case bfd_link_hash_indirect:
9898 /* These symbols are created by symbol versioning. They point
9899 to the decorated version of the name. For example, if the
9900 symbol foo@@GNU_1.2 is the default, which should be used when
9901 foo is used with no version, then we add an indirect symbol
9902 foo which points to foo@@GNU_1.2. We ignore these symbols,
9903 since the indirected symbol is already in the hash table. */
9904 return TRUE;
9905 }
9906
b8871f35
L
9907 if (type == STT_COMMON || type == STT_OBJECT)
9908 switch (h->root.type)
9909 {
9910 case bfd_link_hash_common:
9911 type = elf_link_convert_common_type (flinfo->info, type);
9912 break;
9913 case bfd_link_hash_defined:
9914 case bfd_link_hash_defweak:
9915 if (bed->common_definition (&sym))
9916 type = elf_link_convert_common_type (flinfo->info, type);
9917 else
9918 type = STT_OBJECT;
9919 break;
9920 case bfd_link_hash_undefined:
9921 case bfd_link_hash_undefweak:
9922 break;
9923 default:
9924 abort ();
9925 }
9926
4deb8f71 9927 if (h->forced_local)
b8871f35
L
9928 {
9929 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9930 /* Turn off visibility on local symbol. */
9931 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9932 }
9933 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9934 else if (h->unique_global && h->def_regular)
9935 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9936 else if (h->root.type == bfd_link_hash_undefweak
9937 || h->root.type == bfd_link_hash_defweak)
9938 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9939 else
9940 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9941 sym.st_target_internal = h->target_internal;
9942
c152c796
AM
9943 /* Give the processor backend a chance to tweak the symbol value,
9944 and also to finish up anything that needs to be done for this
9945 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9946 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9947 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9948 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9949 && h->def_regular
0e1862bb 9950 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9951 || ((h->dynindx != -1
9952 || h->forced_local)
0e1862bb 9953 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9954 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9955 || h->root.type != bfd_link_hash_undefweak))
9956 || !h->forced_local)
8b127cbc 9957 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9958 {
9959 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9960 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9961 {
9962 eoinfo->failed = TRUE;
9963 return FALSE;
9964 }
9965 }
9966
9967 /* If we are marking the symbol as undefined, and there are no
9968 non-weak references to this symbol from a regular object, then
9969 mark the symbol as weak undefined; if there are non-weak
9970 references, mark the symbol as strong. We can't do this earlier,
9971 because it might not be marked as undefined until the
9972 finish_dynamic_symbol routine gets through with it. */
9973 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9974 && h->ref_regular
c152c796
AM
9975 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9976 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9977 {
9978 int bindtype;
b8871f35 9979 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9980
9981 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9982 if (type == STT_GNU_IFUNC)
9983 type = STT_FUNC;
c152c796 9984
f5385ebf 9985 if (h->ref_regular_nonweak)
c152c796
AM
9986 bindtype = STB_GLOBAL;
9987 else
9988 bindtype = STB_WEAK;
2955ec4c 9989 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9990 }
9991
bda987c2
CD
9992 /* If this is a symbol defined in a dynamic library, don't use the
9993 symbol size from the dynamic library. Relinking an executable
9994 against a new library may introduce gratuitous changes in the
9995 executable's symbols if we keep the size. */
9996 if (sym.st_shndx == SHN_UNDEF
9997 && !h->def_regular
9998 && h->def_dynamic)
9999 sym.st_size = 0;
10000
c152c796
AM
10001 /* If a non-weak symbol with non-default visibility is not defined
10002 locally, it is a fatal error. */
0e1862bb 10003 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10004 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10005 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10006 && h->root.type == bfd_link_hash_undefined
f5385ebf 10007 && !h->def_regular)
c152c796 10008 {
17d078c5
AM
10009 const char *msg;
10010
10011 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10012 /* xgettext:c-format */
871b3ab2 10013 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10014 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10015 /* xgettext:c-format */
871b3ab2 10016 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10017 else
695344c0 10018 /* xgettext:c-format */
871b3ab2 10019 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10020 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10021 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10022 eoinfo->failed = TRUE;
10023 return FALSE;
10024 }
10025
10026 /* If this symbol should be put in the .dynsym section, then put it
10027 there now. We already know the symbol index. We also fill in
10028 the entry in the .hash section. */
cae1fbbb 10029 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 10030 && h->dynindx != -1
8b127cbc 10031 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 10032 {
c152c796
AM
10033 bfd_byte *esym;
10034
90c984fc
L
10035 /* Since there is no version information in the dynamic string,
10036 if there is no version info in symbol version section, we will
1659f720 10037 have a run-time problem if not linking executable, referenced
4deb8f71 10038 by shared library, or not bound locally. */
1659f720 10039 if (h->verinfo.verdef == NULL
0e1862bb 10040 && (!bfd_link_executable (flinfo->info)
1659f720
L
10041 || h->ref_dynamic
10042 || !h->def_regular))
90c984fc
L
10043 {
10044 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10045
10046 if (p && p [1] != '\0')
10047 {
4eca0228 10048 _bfd_error_handler
695344c0 10049 /* xgettext:c-format */
9793eb77 10050 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10051 flinfo->output_bfd, h->root.root.string);
10052 eoinfo->failed = TRUE;
10053 return FALSE;
10054 }
10055 }
10056
c152c796 10057 sym.st_name = h->dynstr_index;
cae1fbbb
L
10058 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10059 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10060 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10061 {
10062 eoinfo->failed = TRUE;
10063 return FALSE;
10064 }
8b127cbc 10065 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10066
8b127cbc 10067 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10068 {
10069 size_t hash_entry_size;
10070 bfd_byte *bucketpos;
10071 bfd_vma chain;
41198d0c
L
10072 size_t bucketcount;
10073 size_t bucket;
10074
8b127cbc 10075 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10076 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10077
10078 hash_entry_size
8b127cbc
AM
10079 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10080 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10081 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10082 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10083 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10084 bucketpos);
10085 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10086 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10087 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10088 }
c152c796 10089
8b127cbc 10090 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10091 {
10092 Elf_Internal_Versym iversym;
10093 Elf_External_Versym *eversym;
10094
f5385ebf 10095 if (!h->def_regular)
c152c796 10096 {
7b20f099
AM
10097 if (h->verinfo.verdef == NULL
10098 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10099 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10100 iversym.vs_vers = 0;
10101 else
10102 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10103 }
10104 else
10105 {
10106 if (h->verinfo.vertree == NULL)
10107 iversym.vs_vers = 1;
10108 else
10109 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10110 if (flinfo->info->create_default_symver)
3e3b46e5 10111 iversym.vs_vers++;
c152c796
AM
10112 }
10113
422f1182 10114 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10115 defined locally. */
422f1182 10116 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10117 iversym.vs_vers |= VERSYM_HIDDEN;
10118
8b127cbc 10119 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10120 eversym += h->dynindx;
8b127cbc 10121 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10122 }
10123 }
10124
d983c8c5
AM
10125 /* If the symbol is undefined, and we didn't output it to .dynsym,
10126 strip it from .symtab too. Obviously we can't do this for
10127 relocatable output or when needed for --emit-relocs. */
10128 else if (input_sec == bfd_und_section_ptr
10129 && h->indx != -2
66cae560
NC
10130 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10131 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10132 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10133 return TRUE;
66cae560 10134
d983c8c5
AM
10135 /* Also strip others that we couldn't earlier due to dynamic symbol
10136 processing. */
10137 if (strip)
10138 return TRUE;
10139 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10140 return TRUE;
10141
2ec55de3
AM
10142 /* Output a FILE symbol so that following locals are not associated
10143 with the wrong input file. We need one for forced local symbols
10144 if we've seen more than one FILE symbol or when we have exactly
10145 one FILE symbol but global symbols are present in a file other
10146 than the one with the FILE symbol. We also need one if linker
10147 defined symbols are present. In practice these conditions are
10148 always met, so just emit the FILE symbol unconditionally. */
10149 if (eoinfo->localsyms
10150 && !eoinfo->file_sym_done
10151 && eoinfo->flinfo->filesym_count != 0)
10152 {
10153 Elf_Internal_Sym fsym;
10154
10155 memset (&fsym, 0, sizeof (fsym));
10156 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10157 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10158 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10159 bfd_und_section_ptr, NULL))
2ec55de3
AM
10160 return FALSE;
10161
10162 eoinfo->file_sym_done = TRUE;
10163 }
10164
8b127cbc 10165 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10166 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10167 input_sec, h);
6e0b88f1 10168 if (ret == 0)
c152c796
AM
10169 {
10170 eoinfo->failed = TRUE;
10171 return FALSE;
10172 }
6e0b88f1
AM
10173 else if (ret == 1)
10174 h->indx = indx;
10175 else if (h->indx == -2)
10176 abort();
c152c796
AM
10177
10178 return TRUE;
10179}
10180
cdd3575c
AM
10181/* Return TRUE if special handling is done for relocs in SEC against
10182 symbols defined in discarded sections. */
10183
c152c796
AM
10184static bfd_boolean
10185elf_section_ignore_discarded_relocs (asection *sec)
10186{
10187 const struct elf_backend_data *bed;
10188
cdd3575c
AM
10189 switch (sec->sec_info_type)
10190 {
dbaa2011
AM
10191 case SEC_INFO_TYPE_STABS:
10192 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10193 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10194 return TRUE;
10195 default:
10196 break;
10197 }
c152c796
AM
10198
10199 bed = get_elf_backend_data (sec->owner);
10200 if (bed->elf_backend_ignore_discarded_relocs != NULL
10201 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10202 return TRUE;
10203
10204 return FALSE;
10205}
10206
9e66c942
AM
10207/* Return a mask saying how ld should treat relocations in SEC against
10208 symbols defined in discarded sections. If this function returns
10209 COMPLAIN set, ld will issue a warning message. If this function
10210 returns PRETEND set, and the discarded section was link-once and the
10211 same size as the kept link-once section, ld will pretend that the
10212 symbol was actually defined in the kept section. Otherwise ld will
10213 zero the reloc (at least that is the intent, but some cooperation by
10214 the target dependent code is needed, particularly for REL targets). */
10215
8a696751
AM
10216unsigned int
10217_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10218{
9e66c942 10219 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10220 return PRETEND;
cdd3575c
AM
10221
10222 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10223 return 0;
cdd3575c
AM
10224
10225 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10226 return 0;
cdd3575c 10227
9e66c942 10228 return COMPLAIN | PRETEND;
cdd3575c
AM
10229}
10230
3d7f7666
L
10231/* Find a match between a section and a member of a section group. */
10232
10233static asection *
c0f00686
L
10234match_group_member (asection *sec, asection *group,
10235 struct bfd_link_info *info)
3d7f7666
L
10236{
10237 asection *first = elf_next_in_group (group);
10238 asection *s = first;
10239
10240 while (s != NULL)
10241 {
c0f00686 10242 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10243 return s;
10244
83180ade 10245 s = elf_next_in_group (s);
3d7f7666
L
10246 if (s == first)
10247 break;
10248 }
10249
10250 return NULL;
10251}
10252
01b3c8ab 10253/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10254 to replace it. Return the replacement if it is OK. Otherwise return
10255 NULL. */
01b3c8ab
L
10256
10257asection *
c0f00686 10258_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10259{
10260 asection *kept;
10261
10262 kept = sec->kept_section;
10263 if (kept != NULL)
10264 {
c2370991 10265 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10266 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10267 if (kept != NULL
10268 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10269 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10270 kept = NULL;
c2370991 10271 sec->kept_section = kept;
01b3c8ab
L
10272 }
10273 return kept;
10274}
10275
c152c796
AM
10276/* Link an input file into the linker output file. This function
10277 handles all the sections and relocations of the input file at once.
10278 This is so that we only have to read the local symbols once, and
10279 don't have to keep them in memory. */
10280
10281static bfd_boolean
8b127cbc 10282elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10283{
ece5ef60 10284 int (*relocate_section)
c152c796
AM
10285 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10286 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10287 bfd *output_bfd;
10288 Elf_Internal_Shdr *symtab_hdr;
10289 size_t locsymcount;
10290 size_t extsymoff;
10291 Elf_Internal_Sym *isymbuf;
10292 Elf_Internal_Sym *isym;
10293 Elf_Internal_Sym *isymend;
10294 long *pindex;
10295 asection **ppsection;
10296 asection *o;
10297 const struct elf_backend_data *bed;
c152c796 10298 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10299 bfd_size_type address_size;
10300 bfd_vma r_type_mask;
10301 int r_sym_shift;
ffbc01cc 10302 bfd_boolean have_file_sym = FALSE;
c152c796 10303
8b127cbc 10304 output_bfd = flinfo->output_bfd;
c152c796
AM
10305 bed = get_elf_backend_data (output_bfd);
10306 relocate_section = bed->elf_backend_relocate_section;
10307
10308 /* If this is a dynamic object, we don't want to do anything here:
10309 we don't want the local symbols, and we don't want the section
10310 contents. */
10311 if ((input_bfd->flags & DYNAMIC) != 0)
10312 return TRUE;
10313
c152c796
AM
10314 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10315 if (elf_bad_symtab (input_bfd))
10316 {
10317 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10318 extsymoff = 0;
10319 }
10320 else
10321 {
10322 locsymcount = symtab_hdr->sh_info;
10323 extsymoff = symtab_hdr->sh_info;
10324 }
10325
10326 /* Read the local symbols. */
10327 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10328 if (isymbuf == NULL && locsymcount != 0)
10329 {
10330 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10331 flinfo->internal_syms,
10332 flinfo->external_syms,
10333 flinfo->locsym_shndx);
c152c796
AM
10334 if (isymbuf == NULL)
10335 return FALSE;
10336 }
10337
10338 /* Find local symbol sections and adjust values of symbols in
10339 SEC_MERGE sections. Write out those local symbols we know are
10340 going into the output file. */
10341 isymend = isymbuf + locsymcount;
8b127cbc 10342 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10343 isym < isymend;
10344 isym++, pindex++, ppsection++)
10345 {
10346 asection *isec;
10347 const char *name;
10348 Elf_Internal_Sym osym;
6e0b88f1
AM
10349 long indx;
10350 int ret;
c152c796
AM
10351
10352 *pindex = -1;
10353
10354 if (elf_bad_symtab (input_bfd))
10355 {
10356 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10357 {
10358 *ppsection = NULL;
10359 continue;
10360 }
10361 }
10362
10363 if (isym->st_shndx == SHN_UNDEF)
10364 isec = bfd_und_section_ptr;
c152c796
AM
10365 else if (isym->st_shndx == SHN_ABS)
10366 isec = bfd_abs_section_ptr;
10367 else if (isym->st_shndx == SHN_COMMON)
10368 isec = bfd_com_section_ptr;
10369 else
10370 {
cb33740c
AM
10371 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10372 if (isec == NULL)
10373 {
10374 /* Don't attempt to output symbols with st_shnx in the
10375 reserved range other than SHN_ABS and SHN_COMMON. */
10376 *ppsection = NULL;
10377 continue;
10378 }
dbaa2011 10379 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10380 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10381 isym->st_value =
10382 _bfd_merged_section_offset (output_bfd, &isec,
10383 elf_section_data (isec)->sec_info,
10384 isym->st_value);
c152c796
AM
10385 }
10386
10387 *ppsection = isec;
10388
d983c8c5
AM
10389 /* Don't output the first, undefined, symbol. In fact, don't
10390 output any undefined local symbol. */
10391 if (isec == bfd_und_section_ptr)
c152c796
AM
10392 continue;
10393
10394 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10395 {
10396 /* We never output section symbols. Instead, we use the
10397 section symbol of the corresponding section in the output
10398 file. */
10399 continue;
10400 }
10401
10402 /* If we are stripping all symbols, we don't want to output this
10403 one. */
8b127cbc 10404 if (flinfo->info->strip == strip_all)
c152c796
AM
10405 continue;
10406
10407 /* If we are discarding all local symbols, we don't want to
10408 output this one. If we are generating a relocatable output
10409 file, then some of the local symbols may be required by
10410 relocs; we output them below as we discover that they are
10411 needed. */
8b127cbc 10412 if (flinfo->info->discard == discard_all)
c152c796
AM
10413 continue;
10414
10415 /* If this symbol is defined in a section which we are
f02571c5
AM
10416 discarding, we don't need to keep it. */
10417 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10418 && isym->st_shndx < SHN_LORESERVE
10419 && bfd_section_removed_from_list (output_bfd,
10420 isec->output_section))
e75a280b
L
10421 continue;
10422
c152c796
AM
10423 /* Get the name of the symbol. */
10424 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10425 isym->st_name);
10426 if (name == NULL)
10427 return FALSE;
10428
10429 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10430 if ((flinfo->info->strip == strip_some
10431 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10432 == NULL))
8b127cbc 10433 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10434 && (isec->flags & SEC_MERGE)
10435 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10436 || flinfo->info->discard == discard_l)
c152c796
AM
10437 && bfd_is_local_label_name (input_bfd, name)))
10438 continue;
10439
ffbc01cc
AM
10440 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10441 {
ce875075
AM
10442 if (input_bfd->lto_output)
10443 /* -flto puts a temp file name here. This means builds
10444 are not reproducible. Discard the symbol. */
10445 continue;
ffbc01cc
AM
10446 have_file_sym = TRUE;
10447 flinfo->filesym_count += 1;
10448 }
10449 if (!have_file_sym)
10450 {
10451 /* In the absence of debug info, bfd_find_nearest_line uses
10452 FILE symbols to determine the source file for local
10453 function symbols. Provide a FILE symbol here if input
10454 files lack such, so that their symbols won't be
10455 associated with a previous input file. It's not the
10456 source file, but the best we can do. */
10457 have_file_sym = TRUE;
10458 flinfo->filesym_count += 1;
10459 memset (&osym, 0, sizeof (osym));
10460 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10461 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10462 if (!elf_link_output_symstrtab (flinfo,
10463 (input_bfd->lto_output ? NULL
10464 : input_bfd->filename),
10465 &osym, bfd_abs_section_ptr,
10466 NULL))
ffbc01cc
AM
10467 return FALSE;
10468 }
10469
c152c796
AM
10470 osym = *isym;
10471
10472 /* Adjust the section index for the output file. */
10473 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10474 isec->output_section);
10475 if (osym.st_shndx == SHN_BAD)
10476 return FALSE;
10477
c152c796
AM
10478 /* ELF symbols in relocatable files are section relative, but
10479 in executable files they are virtual addresses. Note that
10480 this code assumes that all ELF sections have an associated
10481 BFD section with a reasonable value for output_offset; below
10482 we assume that they also have a reasonable value for
10483 output_section. Any special sections must be set up to meet
10484 these requirements. */
10485 osym.st_value += isec->output_offset;
0e1862bb 10486 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10487 {
10488 osym.st_value += isec->output_section->vma;
10489 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10490 {
10491 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10492 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10493 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10494 }
10495 }
10496
6e0b88f1 10497 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10498 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10499 if (ret == 0)
c152c796 10500 return FALSE;
6e0b88f1
AM
10501 else if (ret == 1)
10502 *pindex = indx;
c152c796
AM
10503 }
10504
310fd250
L
10505 if (bed->s->arch_size == 32)
10506 {
10507 r_type_mask = 0xff;
10508 r_sym_shift = 8;
10509 address_size = 4;
10510 }
10511 else
10512 {
10513 r_type_mask = 0xffffffff;
10514 r_sym_shift = 32;
10515 address_size = 8;
10516 }
10517
c152c796
AM
10518 /* Relocate the contents of each section. */
10519 sym_hashes = elf_sym_hashes (input_bfd);
10520 for (o = input_bfd->sections; o != NULL; o = o->next)
10521 {
10522 bfd_byte *contents;
10523
10524 if (! o->linker_mark)
10525 {
10526 /* This section was omitted from the link. */
10527 continue;
10528 }
10529
7bdf4127 10530 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10531 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10532 {
10533 /* Deal with the group signature symbol. */
10534 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10535 unsigned long symndx = sec_data->this_hdr.sh_info;
10536 asection *osec = o->output_section;
10537
7bdf4127 10538 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10539 if (symndx >= locsymcount
10540 || (elf_bad_symtab (input_bfd)
8b127cbc 10541 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10542 {
10543 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10544 while (h->root.type == bfd_link_hash_indirect
10545 || h->root.type == bfd_link_hash_warning)
10546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10547 /* Arrange for symbol to be output. */
10548 h->indx = -2;
10549 elf_section_data (osec)->this_hdr.sh_info = -2;
10550 }
10551 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10552 {
10553 /* We'll use the output section target_index. */
8b127cbc 10554 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10555 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10556 }
10557 else
10558 {
8b127cbc 10559 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10560 {
10561 /* Otherwise output the local symbol now. */
10562 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10563 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10564 const char *name;
6e0b88f1
AM
10565 long indx;
10566 int ret;
bcacc0f5
AM
10567
10568 name = bfd_elf_string_from_elf_section (input_bfd,
10569 symtab_hdr->sh_link,
10570 sym.st_name);
10571 if (name == NULL)
10572 return FALSE;
10573
10574 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10575 sec);
10576 if (sym.st_shndx == SHN_BAD)
10577 return FALSE;
10578
10579 sym.st_value += o->output_offset;
10580
6e0b88f1 10581 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10582 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10583 NULL);
6e0b88f1 10584 if (ret == 0)
bcacc0f5 10585 return FALSE;
6e0b88f1 10586 else if (ret == 1)
8b127cbc 10587 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10588 else
10589 abort ();
bcacc0f5
AM
10590 }
10591 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10592 = flinfo->indices[symndx];
bcacc0f5
AM
10593 }
10594 }
10595
c152c796 10596 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10597 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10598 continue;
10599
10600 if ((o->flags & SEC_LINKER_CREATED) != 0)
10601 {
10602 /* Section was created by _bfd_elf_link_create_dynamic_sections
10603 or somesuch. */
10604 continue;
10605 }
10606
10607 /* Get the contents of the section. They have been cached by a
10608 relaxation routine. Note that o is a section in an input
10609 file, so the contents field will not have been set by any of
10610 the routines which work on output files. */
10611 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10612 {
10613 contents = elf_section_data (o)->this_hdr.contents;
10614 if (bed->caches_rawsize
10615 && o->rawsize != 0
10616 && o->rawsize < o->size)
10617 {
10618 memcpy (flinfo->contents, contents, o->rawsize);
10619 contents = flinfo->contents;
10620 }
10621 }
c152c796
AM
10622 else
10623 {
8b127cbc 10624 contents = flinfo->contents;
4a114e3e 10625 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10626 return FALSE;
10627 }
10628
10629 if ((o->flags & SEC_RELOC) != 0)
10630 {
10631 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10632 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10633 int action_discarded;
ece5ef60 10634 int ret;
c152c796
AM
10635
10636 /* Get the swapped relocs. */
10637 internal_relocs
8b127cbc
AM
10638 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10639 flinfo->internal_relocs, FALSE);
c152c796
AM
10640 if (internal_relocs == NULL
10641 && o->reloc_count > 0)
10642 return FALSE;
10643
310fd250
L
10644 /* We need to reverse-copy input .ctors/.dtors sections if
10645 they are placed in .init_array/.finit_array for output. */
10646 if (o->size > address_size
10647 && ((strncmp (o->name, ".ctors", 6) == 0
10648 && strcmp (o->output_section->name,
10649 ".init_array") == 0)
10650 || (strncmp (o->name, ".dtors", 6) == 0
10651 && strcmp (o->output_section->name,
10652 ".fini_array") == 0))
10653 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10654 {
056bafd4
MR
10655 if (o->size * bed->s->int_rels_per_ext_rel
10656 != o->reloc_count * address_size)
310fd250 10657 {
4eca0228 10658 _bfd_error_handler
695344c0 10659 /* xgettext:c-format */
871b3ab2 10660 (_("error: %pB: size of section %pA is not "
310fd250
L
10661 "multiple of address size"),
10662 input_bfd, o);
8c6716e5 10663 bfd_set_error (bfd_error_bad_value);
310fd250
L
10664 return FALSE;
10665 }
10666 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10667 }
10668
0f02bbd9 10669 action_discarded = -1;
c152c796 10670 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10671 action_discarded = (*bed->action_discarded) (o);
10672
10673 /* Run through the relocs evaluating complex reloc symbols and
10674 looking for relocs against symbols from discarded sections
10675 or section symbols from removed link-once sections.
10676 Complain about relocs against discarded sections. Zero
10677 relocs against removed link-once sections. */
10678
10679 rel = internal_relocs;
056bafd4 10680 relend = rel + o->reloc_count;
0f02bbd9 10681 for ( ; rel < relend; rel++)
c152c796 10682 {
0f02bbd9
AM
10683 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10684 unsigned int s_type;
10685 asection **ps, *sec;
10686 struct elf_link_hash_entry *h = NULL;
10687 const char *sym_name;
c152c796 10688
0f02bbd9
AM
10689 if (r_symndx == STN_UNDEF)
10690 continue;
c152c796 10691
0f02bbd9
AM
10692 if (r_symndx >= locsymcount
10693 || (elf_bad_symtab (input_bfd)
8b127cbc 10694 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10695 {
10696 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10697
0f02bbd9
AM
10698 /* Badly formatted input files can contain relocs that
10699 reference non-existant symbols. Check here so that
10700 we do not seg fault. */
10701 if (h == NULL)
c152c796 10702 {
4eca0228 10703 _bfd_error_handler
695344c0 10704 /* xgettext:c-format */
2dcf00ce 10705 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10706 "that references a non-existent global symbol"),
2dcf00ce 10707 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10708 bfd_set_error (bfd_error_bad_value);
10709 return FALSE;
10710 }
3b36f7e6 10711
0f02bbd9
AM
10712 while (h->root.type == bfd_link_hash_indirect
10713 || h->root.type == bfd_link_hash_warning)
10714 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10715
0f02bbd9 10716 s_type = h->type;
cdd3575c 10717
9e2dec47 10718 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10719 mark the symbol as undefined. Note that the
10720 linker may attach linker created dynamic sections
10721 to the plugin bfd. Symbols defined in linker
10722 created sections are not plugin symbols. */
bc4e12de 10723 if ((h->root.non_ir_ref_regular
4070765b 10724 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10725 && (h->root.type == bfd_link_hash_defined
10726 || h->root.type == bfd_link_hash_defweak)
10727 && (h->root.u.def.section->flags
10728 & SEC_LINKER_CREATED) == 0
10729 && h->root.u.def.section->owner != NULL
10730 && (h->root.u.def.section->owner->flags
10731 & BFD_PLUGIN) != 0)
10732 {
10733 h->root.type = bfd_link_hash_undefined;
10734 h->root.u.undef.abfd = h->root.u.def.section->owner;
10735 }
10736
0f02bbd9
AM
10737 ps = NULL;
10738 if (h->root.type == bfd_link_hash_defined
10739 || h->root.type == bfd_link_hash_defweak)
10740 ps = &h->root.u.def.section;
10741
10742 sym_name = h->root.root.string;
10743 }
10744 else
10745 {
10746 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10747
10748 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10749 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10750 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10751 sym, *ps);
10752 }
c152c796 10753
c301e700 10754 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10755 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10756 {
10757 bfd_vma val;
10758 bfd_vma dot = (rel->r_offset
10759 + o->output_offset + o->output_section->vma);
10760#ifdef DEBUG
10761 printf ("Encountered a complex symbol!");
10762 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10763 input_bfd->filename, o->name,
10764 (long) (rel - internal_relocs));
0f02bbd9
AM
10765 printf (" symbol: idx %8.8lx, name %s\n",
10766 r_symndx, sym_name);
10767 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10768 (unsigned long) rel->r_info,
10769 (unsigned long) rel->r_offset);
10770#endif
8b127cbc 10771 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10772 isymbuf, locsymcount, s_type == STT_SRELC))
10773 return FALSE;
10774
10775 /* Symbol evaluated OK. Update to absolute value. */
10776 set_symbol_value (input_bfd, isymbuf, locsymcount,
10777 r_symndx, val);
10778 continue;
10779 }
10780
10781 if (action_discarded != -1 && ps != NULL)
10782 {
cdd3575c
AM
10783 /* Complain if the definition comes from a
10784 discarded section. */
dbaa2011 10785 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10786 {
cf35638d 10787 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10788 if (action_discarded & COMPLAIN)
8b127cbc 10789 (*flinfo->info->callbacks->einfo)
695344c0 10790 /* xgettext:c-format */
871b3ab2
AM
10791 (_("%X`%s' referenced in section `%pA' of %pB: "
10792 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10793 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10794
87e5235d 10795 /* Try to do the best we can to support buggy old
e0ae6d6f 10796 versions of gcc. Pretend that the symbol is
87e5235d
AM
10797 really defined in the kept linkonce section.
10798 FIXME: This is quite broken. Modifying the
10799 symbol here means we will be changing all later
e0ae6d6f 10800 uses of the symbol, not just in this section. */
0f02bbd9 10801 if (action_discarded & PRETEND)
87e5235d 10802 {
01b3c8ab
L
10803 asection *kept;
10804
c0f00686 10805 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10806 flinfo->info);
01b3c8ab 10807 if (kept != NULL)
87e5235d
AM
10808 {
10809 *ps = kept;
10810 continue;
10811 }
10812 }
c152c796
AM
10813 }
10814 }
10815 }
10816
10817 /* Relocate the section by invoking a back end routine.
10818
10819 The back end routine is responsible for adjusting the
10820 section contents as necessary, and (if using Rela relocs
10821 and generating a relocatable output file) adjusting the
10822 reloc addend as necessary.
10823
10824 The back end routine does not have to worry about setting
10825 the reloc address or the reloc symbol index.
10826
10827 The back end routine is given a pointer to the swapped in
10828 internal symbols, and can access the hash table entries
10829 for the external symbols via elf_sym_hashes (input_bfd).
10830
10831 When generating relocatable output, the back end routine
10832 must handle STB_LOCAL/STT_SECTION symbols specially. The
10833 output symbol is going to be a section symbol
10834 corresponding to the output section, which will require
10835 the addend to be adjusted. */
10836
8b127cbc 10837 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10838 input_bfd, o, contents,
10839 internal_relocs,
10840 isymbuf,
8b127cbc 10841 flinfo->sections);
ece5ef60 10842 if (!ret)
c152c796
AM
10843 return FALSE;
10844
ece5ef60 10845 if (ret == 2
0e1862bb 10846 || bfd_link_relocatable (flinfo->info)
8b127cbc 10847 || flinfo->info->emitrelocations)
c152c796
AM
10848 {
10849 Elf_Internal_Rela *irela;
d4730f92 10850 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10851 bfd_vma last_offset;
10852 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10853 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10854 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10855 unsigned int next_erel;
c152c796 10856 bfd_boolean rela_normal;
d4730f92 10857 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10858
d4730f92
BS
10859 esdi = elf_section_data (o);
10860 esdo = elf_section_data (o->output_section);
10861 rela_normal = FALSE;
c152c796
AM
10862
10863 /* Adjust the reloc addresses and symbol indices. */
10864
10865 irela = internal_relocs;
056bafd4 10866 irelaend = irela + o->reloc_count;
d4730f92
BS
10867 rel_hash = esdo->rel.hashes + esdo->rel.count;
10868 /* We start processing the REL relocs, if any. When we reach
10869 IRELAMID in the loop, we switch to the RELA relocs. */
10870 irelamid = irela;
10871 if (esdi->rel.hdr != NULL)
10872 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10873 * bed->s->int_rels_per_ext_rel);
eac338cf 10874 rel_hash_list = rel_hash;
d4730f92 10875 rela_hash_list = NULL;
c152c796 10876 last_offset = o->output_offset;
0e1862bb 10877 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10878 last_offset += o->output_section->vma;
10879 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10880 {
10881 unsigned long r_symndx;
10882 asection *sec;
10883 Elf_Internal_Sym sym;
10884
10885 if (next_erel == bed->s->int_rels_per_ext_rel)
10886 {
10887 rel_hash++;
10888 next_erel = 0;
10889 }
10890
d4730f92
BS
10891 if (irela == irelamid)
10892 {
10893 rel_hash = esdo->rela.hashes + esdo->rela.count;
10894 rela_hash_list = rel_hash;
10895 rela_normal = bed->rela_normal;
10896 }
10897
c152c796 10898 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10899 flinfo->info, o,
c152c796
AM
10900 irela->r_offset);
10901 if (irela->r_offset >= (bfd_vma) -2)
10902 {
10903 /* This is a reloc for a deleted entry or somesuch.
10904 Turn it into an R_*_NONE reloc, at the same
10905 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10906 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10907 being ordered. */
10908 irela->r_offset = last_offset;
10909 irela->r_info = 0;
10910 irela->r_addend = 0;
10911 continue;
10912 }
10913
10914 irela->r_offset += o->output_offset;
10915
10916 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10917 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10918 irela->r_offset += o->output_section->vma;
10919
10920 last_offset = irela->r_offset;
10921
10922 r_symndx = irela->r_info >> r_sym_shift;
10923 if (r_symndx == STN_UNDEF)
10924 continue;
10925
10926 if (r_symndx >= locsymcount
10927 || (elf_bad_symtab (input_bfd)
8b127cbc 10928 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10929 {
10930 struct elf_link_hash_entry *rh;
10931 unsigned long indx;
10932
10933 /* This is a reloc against a global symbol. We
10934 have not yet output all the local symbols, so
10935 we do not know the symbol index of any global
10936 symbol. We set the rel_hash entry for this
10937 reloc to point to the global hash table entry
10938 for this symbol. The symbol index is then
ee75fd95 10939 set at the end of bfd_elf_final_link. */
c152c796
AM
10940 indx = r_symndx - extsymoff;
10941 rh = elf_sym_hashes (input_bfd)[indx];
10942 while (rh->root.type == bfd_link_hash_indirect
10943 || rh->root.type == bfd_link_hash_warning)
10944 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10945
10946 /* Setting the index to -2 tells
10947 elf_link_output_extsym that this symbol is
10948 used by a reloc. */
10949 BFD_ASSERT (rh->indx < 0);
10950 rh->indx = -2;
c152c796
AM
10951 *rel_hash = rh;
10952
10953 continue;
10954 }
10955
10956 /* This is a reloc against a local symbol. */
10957
10958 *rel_hash = NULL;
10959 sym = isymbuf[r_symndx];
8b127cbc 10960 sec = flinfo->sections[r_symndx];
c152c796
AM
10961 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10962 {
10963 /* I suppose the backend ought to fill in the
10964 section of any STT_SECTION symbol against a
6a8d1586 10965 processor specific section. */
cf35638d 10966 r_symndx = STN_UNDEF;
6a8d1586
AM
10967 if (bfd_is_abs_section (sec))
10968 ;
c152c796
AM
10969 else if (sec == NULL || sec->owner == NULL)
10970 {
10971 bfd_set_error (bfd_error_bad_value);
10972 return FALSE;
10973 }
10974 else
10975 {
6a8d1586
AM
10976 asection *osec = sec->output_section;
10977
10978 /* If we have discarded a section, the output
10979 section will be the absolute section. In
ab96bf03
AM
10980 case of discarded SEC_MERGE sections, use
10981 the kept section. relocate_section should
10982 have already handled discarded linkonce
10983 sections. */
6a8d1586
AM
10984 if (bfd_is_abs_section (osec)
10985 && sec->kept_section != NULL
10986 && sec->kept_section->output_section != NULL)
10987 {
10988 osec = sec->kept_section->output_section;
10989 irela->r_addend -= osec->vma;
10990 }
10991
10992 if (!bfd_is_abs_section (osec))
10993 {
10994 r_symndx = osec->target_index;
cf35638d 10995 if (r_symndx == STN_UNDEF)
74541ad4 10996 {
051d833a
AM
10997 irela->r_addend += osec->vma;
10998 osec = _bfd_nearby_section (output_bfd, osec,
10999 osec->vma);
11000 irela->r_addend -= osec->vma;
11001 r_symndx = osec->target_index;
74541ad4 11002 }
6a8d1586 11003 }
c152c796
AM
11004 }
11005
11006 /* Adjust the addend according to where the
11007 section winds up in the output section. */
11008 if (rela_normal)
11009 irela->r_addend += sec->output_offset;
11010 }
11011 else
11012 {
8b127cbc 11013 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11014 {
11015 unsigned long shlink;
11016 const char *name;
11017 asection *osec;
6e0b88f1 11018 long indx;
c152c796 11019
8b127cbc 11020 if (flinfo->info->strip == strip_all)
c152c796
AM
11021 {
11022 /* You can't do ld -r -s. */
11023 bfd_set_error (bfd_error_invalid_operation);
11024 return FALSE;
11025 }
11026
11027 /* This symbol was skipped earlier, but
11028 since it is needed by a reloc, we
11029 must output it now. */
11030 shlink = symtab_hdr->sh_link;
11031 name = (bfd_elf_string_from_elf_section
11032 (input_bfd, shlink, sym.st_name));
11033 if (name == NULL)
11034 return FALSE;
11035
11036 osec = sec->output_section;
11037 sym.st_shndx =
11038 _bfd_elf_section_from_bfd_section (output_bfd,
11039 osec);
11040 if (sym.st_shndx == SHN_BAD)
11041 return FALSE;
11042
11043 sym.st_value += sec->output_offset;
0e1862bb 11044 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11045 {
11046 sym.st_value += osec->vma;
11047 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11048 {
11049 /* STT_TLS symbols are relative to PT_TLS
11050 segment base. */
8b127cbc 11051 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 11052 ->tls_sec != NULL);
8b127cbc 11053 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
11054 ->tls_sec->vma);
11055 }
11056 }
11057
6e0b88f1 11058 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11059 ret = elf_link_output_symstrtab (flinfo, name,
11060 &sym, sec,
11061 NULL);
6e0b88f1 11062 if (ret == 0)
c152c796 11063 return FALSE;
6e0b88f1 11064 else if (ret == 1)
8b127cbc 11065 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11066 else
11067 abort ();
c152c796
AM
11068 }
11069
8b127cbc 11070 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11071 }
11072
11073 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11074 | (irela->r_info & r_type_mask));
11075 }
11076
11077 /* Swap out the relocs. */
d4730f92
BS
11078 input_rel_hdr = esdi->rel.hdr;
11079 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11080 {
d4730f92
BS
11081 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11082 input_rel_hdr,
11083 internal_relocs,
11084 rel_hash_list))
11085 return FALSE;
c152c796
AM
11086 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11087 * bed->s->int_rels_per_ext_rel);
eac338cf 11088 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11089 }
11090
11091 input_rela_hdr = esdi->rela.hdr;
11092 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11093 {
eac338cf 11094 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11095 input_rela_hdr,
eac338cf 11096 internal_relocs,
d4730f92 11097 rela_hash_list))
c152c796
AM
11098 return FALSE;
11099 }
11100 }
11101 }
11102
11103 /* Write out the modified section contents. */
11104 if (bed->elf_backend_write_section
8b127cbc 11105 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11106 contents))
c152c796
AM
11107 {
11108 /* Section written out. */
11109 }
11110 else switch (o->sec_info_type)
11111 {
dbaa2011 11112 case SEC_INFO_TYPE_STABS:
c152c796
AM
11113 if (! (_bfd_write_section_stabs
11114 (output_bfd,
8b127cbc 11115 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11116 o, &elf_section_data (o)->sec_info, contents)))
11117 return FALSE;
11118 break;
dbaa2011 11119 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11120 if (! _bfd_write_merged_section (output_bfd, o,
11121 elf_section_data (o)->sec_info))
11122 return FALSE;
11123 break;
dbaa2011 11124 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11125 {
8b127cbc 11126 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11127 o, contents))
11128 return FALSE;
11129 }
11130 break;
2f0c68f2
CM
11131 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11132 {
11133 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11134 flinfo->info,
11135 o, contents))
11136 return FALSE;
11137 }
11138 break;
c152c796
AM
11139 default:
11140 {
310fd250
L
11141 if (! (o->flags & SEC_EXCLUDE))
11142 {
11143 file_ptr offset = (file_ptr) o->output_offset;
11144 bfd_size_type todo = o->size;
37b01f6a
DG
11145
11146 offset *= bfd_octets_per_byte (output_bfd);
11147
310fd250
L
11148 if ((o->flags & SEC_ELF_REVERSE_COPY))
11149 {
11150 /* Reverse-copy input section to output. */
11151 do
11152 {
11153 todo -= address_size;
11154 if (! bfd_set_section_contents (output_bfd,
11155 o->output_section,
11156 contents + todo,
11157 offset,
11158 address_size))
11159 return FALSE;
11160 if (todo == 0)
11161 break;
11162 offset += address_size;
11163 }
11164 while (1);
11165 }
11166 else if (! bfd_set_section_contents (output_bfd,
11167 o->output_section,
11168 contents,
11169 offset, todo))
11170 return FALSE;
11171 }
c152c796
AM
11172 }
11173 break;
11174 }
11175 }
11176
11177 return TRUE;
11178}
11179
11180/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11181 requested by the linker, and does not come from any input file. This
c152c796
AM
11182 is used to build constructor and destructor tables when linking
11183 with -Ur. */
11184
11185static bfd_boolean
11186elf_reloc_link_order (bfd *output_bfd,
11187 struct bfd_link_info *info,
11188 asection *output_section,
11189 struct bfd_link_order *link_order)
11190{
11191 reloc_howto_type *howto;
11192 long indx;
11193 bfd_vma offset;
11194 bfd_vma addend;
d4730f92 11195 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11196 struct elf_link_hash_entry **rel_hash_ptr;
11197 Elf_Internal_Shdr *rel_hdr;
11198 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11199 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11200 bfd_byte *erel;
11201 unsigned int i;
d4730f92 11202 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11203
11204 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11205 if (howto == NULL)
11206 {
11207 bfd_set_error (bfd_error_bad_value);
11208 return FALSE;
11209 }
11210
11211 addend = link_order->u.reloc.p->addend;
11212
d4730f92
BS
11213 if (esdo->rel.hdr)
11214 reldata = &esdo->rel;
11215 else if (esdo->rela.hdr)
11216 reldata = &esdo->rela;
11217 else
11218 {
11219 reldata = NULL;
11220 BFD_ASSERT (0);
11221 }
11222
c152c796 11223 /* Figure out the symbol index. */
d4730f92 11224 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11225 if (link_order->type == bfd_section_reloc_link_order)
11226 {
11227 indx = link_order->u.reloc.p->u.section->target_index;
11228 BFD_ASSERT (indx != 0);
11229 *rel_hash_ptr = NULL;
11230 }
11231 else
11232 {
11233 struct elf_link_hash_entry *h;
11234
11235 /* Treat a reloc against a defined symbol as though it were
11236 actually against the section. */
11237 h = ((struct elf_link_hash_entry *)
11238 bfd_wrapped_link_hash_lookup (output_bfd, info,
11239 link_order->u.reloc.p->u.name,
11240 FALSE, FALSE, TRUE));
11241 if (h != NULL
11242 && (h->root.type == bfd_link_hash_defined
11243 || h->root.type == bfd_link_hash_defweak))
11244 {
11245 asection *section;
11246
11247 section = h->root.u.def.section;
11248 indx = section->output_section->target_index;
11249 *rel_hash_ptr = NULL;
11250 /* It seems that we ought to add the symbol value to the
11251 addend here, but in practice it has already been added
11252 because it was passed to constructor_callback. */
11253 addend += section->output_section->vma + section->output_offset;
11254 }
11255 else if (h != NULL)
11256 {
11257 /* Setting the index to -2 tells elf_link_output_extsym that
11258 this symbol is used by a reloc. */
11259 h->indx = -2;
11260 *rel_hash_ptr = h;
11261 indx = 0;
11262 }
11263 else
11264 {
1a72702b
AM
11265 (*info->callbacks->unattached_reloc)
11266 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11267 indx = 0;
11268 }
11269 }
11270
11271 /* If this is an inplace reloc, we must write the addend into the
11272 object file. */
11273 if (howto->partial_inplace && addend != 0)
11274 {
11275 bfd_size_type size;
11276 bfd_reloc_status_type rstat;
11277 bfd_byte *buf;
11278 bfd_boolean ok;
11279 const char *sym_name;
11280
a50b1753
NC
11281 size = (bfd_size_type) bfd_get_reloc_size (howto);
11282 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11283 if (buf == NULL && size != 0)
c152c796
AM
11284 return FALSE;
11285 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11286 switch (rstat)
11287 {
11288 case bfd_reloc_ok:
11289 break;
11290
11291 default:
11292 case bfd_reloc_outofrange:
11293 abort ();
11294
11295 case bfd_reloc_overflow:
11296 if (link_order->type == bfd_section_reloc_link_order)
11297 sym_name = bfd_section_name (output_bfd,
11298 link_order->u.reloc.p->u.section);
11299 else
11300 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11301 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11302 howto->name, addend, NULL, NULL,
11303 (bfd_vma) 0);
c152c796
AM
11304 break;
11305 }
37b01f6a 11306
c152c796 11307 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11308 link_order->offset
11309 * bfd_octets_per_byte (output_bfd),
11310 size);
c152c796
AM
11311 free (buf);
11312 if (! ok)
11313 return FALSE;
11314 }
11315
11316 /* The address of a reloc is relative to the section in a
11317 relocatable file, and is a virtual address in an executable
11318 file. */
11319 offset = link_order->offset;
0e1862bb 11320 if (! bfd_link_relocatable (info))
c152c796
AM
11321 offset += output_section->vma;
11322
11323 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11324 {
11325 irel[i].r_offset = offset;
11326 irel[i].r_info = 0;
11327 irel[i].r_addend = 0;
11328 }
11329 if (bed->s->arch_size == 32)
11330 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11331 else
11332 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11333
d4730f92 11334 rel_hdr = reldata->hdr;
c152c796
AM
11335 erel = rel_hdr->contents;
11336 if (rel_hdr->sh_type == SHT_REL)
11337 {
d4730f92 11338 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11339 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11340 }
11341 else
11342 {
11343 irel[0].r_addend = addend;
d4730f92 11344 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11345 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11346 }
11347
d4730f92 11348 ++reldata->count;
c152c796
AM
11349
11350 return TRUE;
11351}
11352
0b52efa6
PB
11353
11354/* Get the output vma of the section pointed to by the sh_link field. */
11355
11356static bfd_vma
11357elf_get_linked_section_vma (struct bfd_link_order *p)
11358{
11359 Elf_Internal_Shdr **elf_shdrp;
11360 asection *s;
11361 int elfsec;
11362
11363 s = p->u.indirect.section;
11364 elf_shdrp = elf_elfsections (s->owner);
11365 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11366 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11367 /* PR 290:
11368 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11369 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11370 sh_info fields. Hence we could get the situation
11371 where elfsec is 0. */
11372 if (elfsec == 0)
11373 {
11374 const struct elf_backend_data *bed
11375 = get_elf_backend_data (s->owner);
11376 if (bed->link_order_error_handler)
d003868e 11377 bed->link_order_error_handler
695344c0 11378 /* xgettext:c-format */
871b3ab2 11379 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11380 return 0;
11381 }
11382 else
11383 {
11384 s = elf_shdrp[elfsec]->bfd_section;
11385 return s->output_section->vma + s->output_offset;
11386 }
0b52efa6
PB
11387}
11388
11389
11390/* Compare two sections based on the locations of the sections they are
11391 linked to. Used by elf_fixup_link_order. */
11392
11393static int
11394compare_link_order (const void * a, const void * b)
11395{
11396 bfd_vma apos;
11397 bfd_vma bpos;
11398
11399 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11400 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11401 if (apos < bpos)
11402 return -1;
11403 return apos > bpos;
11404}
11405
11406
11407/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11408 order as their linked sections. Returns false if this could not be done
11409 because an output section includes both ordered and unordered
11410 sections. Ideally we'd do this in the linker proper. */
11411
11412static bfd_boolean
11413elf_fixup_link_order (bfd *abfd, asection *o)
11414{
11415 int seen_linkorder;
11416 int seen_other;
11417 int n;
11418 struct bfd_link_order *p;
11419 bfd *sub;
11420 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11421 unsigned elfsec;
0b52efa6 11422 struct bfd_link_order **sections;
d33cdfe3 11423 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11424 bfd_vma offset;
3b36f7e6 11425
d33cdfe3
L
11426 other_sec = NULL;
11427 linkorder_sec = NULL;
0b52efa6
PB
11428 seen_other = 0;
11429 seen_linkorder = 0;
8423293d 11430 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11431 {
d33cdfe3 11432 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11433 {
11434 s = p->u.indirect.section;
d33cdfe3
L
11435 sub = s->owner;
11436 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11437 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11438 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11439 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11440 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11441 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11442 {
11443 seen_linkorder++;
11444 linkorder_sec = s;
11445 }
0b52efa6 11446 else
d33cdfe3
L
11447 {
11448 seen_other++;
11449 other_sec = s;
11450 }
0b52efa6
PB
11451 }
11452 else
11453 seen_other++;
d33cdfe3
L
11454
11455 if (seen_other && seen_linkorder)
11456 {
11457 if (other_sec && linkorder_sec)
4eca0228 11458 _bfd_error_handler
695344c0 11459 /* xgettext:c-format */
871b3ab2
AM
11460 (_("%pA has both ordered [`%pA' in %pB] "
11461 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11462 o, linkorder_sec, linkorder_sec->owner,
11463 other_sec, other_sec->owner);
d33cdfe3 11464 else
4eca0228 11465 _bfd_error_handler
871b3ab2 11466 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11467 bfd_set_error (bfd_error_bad_value);
11468 return FALSE;
11469 }
0b52efa6
PB
11470 }
11471
11472 if (!seen_linkorder)
11473 return TRUE;
11474
0b52efa6 11475 sections = (struct bfd_link_order **)
14b1c01e
AM
11476 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11477 if (sections == NULL)
11478 return FALSE;
0b52efa6 11479 seen_linkorder = 0;
3b36f7e6 11480
8423293d 11481 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11482 {
11483 sections[seen_linkorder++] = p;
11484 }
11485 /* Sort the input sections in the order of their linked section. */
11486 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11487 compare_link_order);
11488
11489 /* Change the offsets of the sections. */
11490 offset = 0;
11491 for (n = 0; n < seen_linkorder; n++)
11492 {
11493 s = sections[n]->u.indirect.section;
461686a3 11494 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11495 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11496 sections[n]->offset = offset;
11497 offset += sections[n]->size;
11498 }
11499
4dd07732 11500 free (sections);
0b52efa6
PB
11501 return TRUE;
11502}
11503
76359541
TP
11504/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11505 Returns TRUE upon success, FALSE otherwise. */
11506
11507static bfd_boolean
11508elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11509{
11510 bfd_boolean ret = FALSE;
11511 bfd *implib_bfd;
11512 const struct elf_backend_data *bed;
11513 flagword flags;
11514 enum bfd_architecture arch;
11515 unsigned int mach;
11516 asymbol **sympp = NULL;
11517 long symsize;
11518 long symcount;
11519 long src_count;
11520 elf_symbol_type *osymbuf;
11521
11522 implib_bfd = info->out_implib_bfd;
11523 bed = get_elf_backend_data (abfd);
11524
11525 if (!bfd_set_format (implib_bfd, bfd_object))
11526 return FALSE;
11527
046734ff 11528 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11529 flags = bfd_get_file_flags (abfd);
11530 flags &= ~HAS_RELOC;
11531 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11532 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11533 return FALSE;
11534
11535 /* Copy architecture of output file to import library file. */
11536 arch = bfd_get_arch (abfd);
11537 mach = bfd_get_mach (abfd);
11538 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11539 && (abfd->target_defaulted
11540 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11541 return FALSE;
11542
11543 /* Get symbol table size. */
11544 symsize = bfd_get_symtab_upper_bound (abfd);
11545 if (symsize < 0)
11546 return FALSE;
11547
11548 /* Read in the symbol table. */
11549 sympp = (asymbol **) xmalloc (symsize);
11550 symcount = bfd_canonicalize_symtab (abfd, sympp);
11551 if (symcount < 0)
11552 goto free_sym_buf;
11553
11554 /* Allow the BFD backend to copy any private header data it
11555 understands from the output BFD to the import library BFD. */
11556 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11557 goto free_sym_buf;
11558
11559 /* Filter symbols to appear in the import library. */
11560 if (bed->elf_backend_filter_implib_symbols)
11561 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11562 symcount);
11563 else
11564 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11565 if (symcount == 0)
11566 {
5df1bc57 11567 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11568 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11569 implib_bfd);
76359541
TP
11570 goto free_sym_buf;
11571 }
11572
11573
11574 /* Make symbols absolute. */
11575 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11576 sizeof (*osymbuf));
11577 for (src_count = 0; src_count < symcount; src_count++)
11578 {
11579 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11580 sizeof (*osymbuf));
11581 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11582 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11583 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11584 osymbuf[src_count].internal_elf_sym.st_value =
11585 osymbuf[src_count].symbol.value;
11586 sympp[src_count] = &osymbuf[src_count].symbol;
11587 }
11588
11589 bfd_set_symtab (implib_bfd, sympp, symcount);
11590
11591 /* Allow the BFD backend to copy any private data it understands
11592 from the output BFD to the import library BFD. This is done last
11593 to permit the routine to look at the filtered symbol table. */
11594 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11595 goto free_sym_buf;
11596
11597 if (!bfd_close (implib_bfd))
11598 goto free_sym_buf;
11599
11600 ret = TRUE;
11601
11602free_sym_buf:
11603 free (sympp);
11604 return ret;
11605}
11606
9f7c3e5e
AM
11607static void
11608elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11609{
11610 asection *o;
11611
11612 if (flinfo->symstrtab != NULL)
ef10c3ac 11613 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11614 if (flinfo->contents != NULL)
11615 free (flinfo->contents);
11616 if (flinfo->external_relocs != NULL)
11617 free (flinfo->external_relocs);
11618 if (flinfo->internal_relocs != NULL)
11619 free (flinfo->internal_relocs);
11620 if (flinfo->external_syms != NULL)
11621 free (flinfo->external_syms);
11622 if (flinfo->locsym_shndx != NULL)
11623 free (flinfo->locsym_shndx);
11624 if (flinfo->internal_syms != NULL)
11625 free (flinfo->internal_syms);
11626 if (flinfo->indices != NULL)
11627 free (flinfo->indices);
11628 if (flinfo->sections != NULL)
11629 free (flinfo->sections);
9f7c3e5e
AM
11630 if (flinfo->symshndxbuf != NULL)
11631 free (flinfo->symshndxbuf);
11632 for (o = obfd->sections; o != NULL; o = o->next)
11633 {
11634 struct bfd_elf_section_data *esdo = elf_section_data (o);
11635 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11636 free (esdo->rel.hashes);
11637 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11638 free (esdo->rela.hashes);
11639 }
11640}
0b52efa6 11641
c152c796
AM
11642/* Do the final step of an ELF link. */
11643
11644bfd_boolean
11645bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11646{
11647 bfd_boolean dynamic;
11648 bfd_boolean emit_relocs;
11649 bfd *dynobj;
8b127cbc 11650 struct elf_final_link_info flinfo;
91d6fa6a
NC
11651 asection *o;
11652 struct bfd_link_order *p;
11653 bfd *sub;
c152c796
AM
11654 bfd_size_type max_contents_size;
11655 bfd_size_type max_external_reloc_size;
11656 bfd_size_type max_internal_reloc_count;
11657 bfd_size_type max_sym_count;
11658 bfd_size_type max_sym_shndx_count;
c152c796
AM
11659 Elf_Internal_Sym elfsym;
11660 unsigned int i;
11661 Elf_Internal_Shdr *symtab_hdr;
11662 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11664 struct elf_outext_info eoinfo;
11665 bfd_boolean merged;
11666 size_t relativecount = 0;
11667 asection *reldyn = 0;
11668 bfd_size_type amt;
104d59d1
JM
11669 asection *attr_section = NULL;
11670 bfd_vma attr_size = 0;
11671 const char *std_attrs_section;
64f52338 11672 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11673
64f52338 11674 if (!is_elf_hash_table (htab))
c152c796
AM
11675 return FALSE;
11676
0e1862bb 11677 if (bfd_link_pic (info))
c152c796
AM
11678 abfd->flags |= DYNAMIC;
11679
64f52338
AM
11680 dynamic = htab->dynamic_sections_created;
11681 dynobj = htab->dynobj;
c152c796 11682
0e1862bb 11683 emit_relocs = (bfd_link_relocatable (info)
a4676736 11684 || info->emitrelocations);
c152c796 11685
8b127cbc
AM
11686 flinfo.info = info;
11687 flinfo.output_bfd = abfd;
ef10c3ac 11688 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11689 if (flinfo.symstrtab == NULL)
c152c796
AM
11690 return FALSE;
11691
11692 if (! dynamic)
11693 {
8b127cbc
AM
11694 flinfo.hash_sec = NULL;
11695 flinfo.symver_sec = NULL;
c152c796
AM
11696 }
11697 else
11698 {
3d4d4302 11699 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11700 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11701 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11702 /* Note that it is OK if symver_sec is NULL. */
11703 }
11704
8b127cbc
AM
11705 flinfo.contents = NULL;
11706 flinfo.external_relocs = NULL;
11707 flinfo.internal_relocs = NULL;
11708 flinfo.external_syms = NULL;
11709 flinfo.locsym_shndx = NULL;
11710 flinfo.internal_syms = NULL;
11711 flinfo.indices = NULL;
11712 flinfo.sections = NULL;
8b127cbc 11713 flinfo.symshndxbuf = NULL;
ffbc01cc 11714 flinfo.filesym_count = 0;
c152c796 11715
104d59d1
JM
11716 /* The object attributes have been merged. Remove the input
11717 sections from the link, and set the contents of the output
11718 secton. */
11719 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11720 for (o = abfd->sections; o != NULL; o = o->next)
11721 {
5270eddc 11722 bfd_boolean remove_section = FALSE;
b8a6ced7 11723
104d59d1
JM
11724 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11725 || strcmp (o->name, ".gnu.attributes") == 0)
11726 {
11727 for (p = o->map_head.link_order; p != NULL; p = p->next)
11728 {
11729 asection *input_section;
11730
11731 if (p->type != bfd_indirect_link_order)
11732 continue;
11733 input_section = p->u.indirect.section;
11734 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11735 elf_link_input_bfd ignores this section. */
11736 input_section->flags &= ~SEC_HAS_CONTENTS;
11737 }
a0c8462f 11738
104d59d1 11739 attr_size = bfd_elf_obj_attr_size (abfd);
b8a6ced7
AM
11740 bfd_set_section_size (abfd, o, attr_size);
11741 /* Skip this section later on. */
11742 o->map_head.link_order = NULL;
104d59d1 11743 if (attr_size)
b8a6ced7 11744 attr_section = o;
104d59d1 11745 else
5270eddc 11746 remove_section = TRUE;
104d59d1 11747 }
6e5e9d58
AM
11748 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11749 {
11750 /* Remove empty group section from linker output. */
5270eddc 11751 remove_section = TRUE;
b8a6ced7 11752 }
5270eddc 11753 if (remove_section)
b8a6ced7 11754 {
6e5e9d58
AM
11755 o->flags |= SEC_EXCLUDE;
11756 bfd_section_list_remove (abfd, o);
11757 abfd->section_count--;
11758 }
104d59d1
JM
11759 }
11760
c152c796
AM
11761 /* Count up the number of relocations we will output for each output
11762 section, so that we know the sizes of the reloc sections. We
11763 also figure out some maximum sizes. */
11764 max_contents_size = 0;
11765 max_external_reloc_size = 0;
11766 max_internal_reloc_count = 0;
11767 max_sym_count = 0;
11768 max_sym_shndx_count = 0;
11769 merged = FALSE;
11770 for (o = abfd->sections; o != NULL; o = o->next)
11771 {
11772 struct bfd_elf_section_data *esdo = elf_section_data (o);
11773 o->reloc_count = 0;
11774
8423293d 11775 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11776 {
11777 unsigned int reloc_count = 0;
9eaff861 11778 unsigned int additional_reloc_count = 0;
c152c796 11779 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11780
11781 if (p->type == bfd_section_reloc_link_order
11782 || p->type == bfd_symbol_reloc_link_order)
11783 reloc_count = 1;
11784 else if (p->type == bfd_indirect_link_order)
11785 {
11786 asection *sec;
11787
11788 sec = p->u.indirect.section;
c152c796
AM
11789
11790 /* Mark all sections which are to be included in the
11791 link. This will normally be every section. We need
11792 to do this so that we can identify any sections which
11793 the linker has decided to not include. */
11794 sec->linker_mark = TRUE;
11795
11796 if (sec->flags & SEC_MERGE)
11797 merged = TRUE;
11798
eea6121a
AM
11799 if (sec->rawsize > max_contents_size)
11800 max_contents_size = sec->rawsize;
11801 if (sec->size > max_contents_size)
11802 max_contents_size = sec->size;
c152c796 11803
c152c796
AM
11804 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11805 && (sec->owner->flags & DYNAMIC) == 0)
11806 {
11807 size_t sym_count;
11808
a961cdd5
AM
11809 /* We are interested in just local symbols, not all
11810 symbols. */
c152c796
AM
11811 if (elf_bad_symtab (sec->owner))
11812 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11813 / bed->s->sizeof_sym);
11814 else
11815 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11816
11817 if (sym_count > max_sym_count)
11818 max_sym_count = sym_count;
11819
11820 if (sym_count > max_sym_shndx_count
6a40cf0c 11821 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11822 max_sym_shndx_count = sym_count;
11823
a961cdd5
AM
11824 if (esdo->this_hdr.sh_type == SHT_REL
11825 || esdo->this_hdr.sh_type == SHT_RELA)
11826 /* Some backends use reloc_count in relocation sections
11827 to count particular types of relocs. Of course,
11828 reloc sections themselves can't have relocations. */
11829 ;
11830 else if (emit_relocs)
11831 {
11832 reloc_count = sec->reloc_count;
11833 if (bed->elf_backend_count_additional_relocs)
11834 {
11835 int c;
11836 c = (*bed->elf_backend_count_additional_relocs) (sec);
11837 additional_reloc_count += c;
11838 }
11839 }
11840 else if (bed->elf_backend_count_relocs)
11841 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11842
11843 esdi = elf_section_data (sec);
11844
c152c796
AM
11845 if ((sec->flags & SEC_RELOC) != 0)
11846 {
d4730f92 11847 size_t ext_size = 0;
c152c796 11848
d4730f92
BS
11849 if (esdi->rel.hdr != NULL)
11850 ext_size = esdi->rel.hdr->sh_size;
11851 if (esdi->rela.hdr != NULL)
11852 ext_size += esdi->rela.hdr->sh_size;
7326c758 11853
c152c796
AM
11854 if (ext_size > max_external_reloc_size)
11855 max_external_reloc_size = ext_size;
11856 if (sec->reloc_count > max_internal_reloc_count)
11857 max_internal_reloc_count = sec->reloc_count;
11858 }
11859 }
11860 }
11861
11862 if (reloc_count == 0)
11863 continue;
11864
9eaff861 11865 reloc_count += additional_reloc_count;
c152c796
AM
11866 o->reloc_count += reloc_count;
11867
0e1862bb 11868 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11869 {
d4730f92 11870 if (esdi->rel.hdr)
9eaff861 11871 {
491d01d3 11872 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11873 esdo->rel.count += additional_reloc_count;
11874 }
d4730f92 11875 if (esdi->rela.hdr)
9eaff861 11876 {
491d01d3 11877 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11878 esdo->rela.count += additional_reloc_count;
11879 }
d4730f92
BS
11880 }
11881 else
11882 {
11883 if (o->use_rela_p)
11884 esdo->rela.count += reloc_count;
2c2b4ed4 11885 else
d4730f92 11886 esdo->rel.count += reloc_count;
c152c796 11887 }
c152c796
AM
11888 }
11889
9eaff861 11890 if (o->reloc_count > 0)
c152c796
AM
11891 o->flags |= SEC_RELOC;
11892 else
11893 {
11894 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11895 set it (this is probably a bug) and if it is set
11896 assign_section_numbers will create a reloc section. */
11897 o->flags &=~ SEC_RELOC;
11898 }
11899
11900 /* If the SEC_ALLOC flag is not set, force the section VMA to
11901 zero. This is done in elf_fake_sections as well, but forcing
11902 the VMA to 0 here will ensure that relocs against these
11903 sections are handled correctly. */
11904 if ((o->flags & SEC_ALLOC) == 0
11905 && ! o->user_set_vma)
11906 o->vma = 0;
11907 }
11908
0e1862bb 11909 if (! bfd_link_relocatable (info) && merged)
64f52338 11910 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11911
11912 /* Figure out the file positions for everything but the symbol table
11913 and the relocs. We set symcount to force assign_section_numbers
11914 to create a symbol table. */
8539e4e8 11915 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11916 BFD_ASSERT (! abfd->output_has_begun);
11917 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11918 goto error_return;
11919
ee75fd95 11920 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11921 for (o = abfd->sections; o != NULL; o = o->next)
11922 {
d4730f92 11923 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11924 if ((o->flags & SEC_RELOC) != 0)
11925 {
d4730f92 11926 if (esdo->rel.hdr
9eaff861 11927 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11928 goto error_return;
11929
d4730f92 11930 if (esdo->rela.hdr
9eaff861 11931 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11932 goto error_return;
11933 }
11934
11935 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11936 to count upwards while actually outputting the relocations. */
d4730f92
BS
11937 esdo->rel.count = 0;
11938 esdo->rela.count = 0;
0ce398f1
L
11939
11940 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11941 {
11942 /* Cache the section contents so that they can be compressed
11943 later. Use bfd_malloc since it will be freed by
11944 bfd_compress_section_contents. */
11945 unsigned char *contents = esdo->this_hdr.contents;
11946 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11947 abort ();
11948 contents
11949 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11950 if (contents == NULL)
11951 goto error_return;
11952 esdo->this_hdr.contents = contents;
11953 }
c152c796
AM
11954 }
11955
c152c796 11956 /* We have now assigned file positions for all the sections except
a485e98e
AM
11957 .symtab, .strtab, and non-loaded reloc sections. We start the
11958 .symtab section at the current file position, and write directly
11959 to it. We build the .strtab section in memory. */
c152c796
AM
11960 bfd_get_symcount (abfd) = 0;
11961 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11962 /* sh_name is set in prep_headers. */
11963 symtab_hdr->sh_type = SHT_SYMTAB;
11964 /* sh_flags, sh_addr and sh_size all start off zero. */
11965 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11966 /* sh_link is set in assign_section_numbers. */
11967 /* sh_info is set below. */
11968 /* sh_offset is set just below. */
72de5009 11969 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11970
ef10c3ac
L
11971 if (max_sym_count < 20)
11972 max_sym_count = 20;
64f52338 11973 htab->strtabsize = max_sym_count;
ef10c3ac 11974 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11975 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11976 if (htab->strtab == NULL)
c152c796 11977 goto error_return;
ef10c3ac
L
11978 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11979 flinfo.symshndxbuf
11980 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11981 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11982
8539e4e8 11983 if (info->strip != strip_all || emit_relocs)
c152c796 11984 {
8539e4e8
AM
11985 file_ptr off = elf_next_file_pos (abfd);
11986
11987 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11988
11989 /* Note that at this point elf_next_file_pos (abfd) is
11990 incorrect. We do not yet know the size of the .symtab section.
11991 We correct next_file_pos below, after we do know the size. */
11992
11993 /* Start writing out the symbol table. The first symbol is always a
11994 dummy symbol. */
c152c796
AM
11995 elfsym.st_value = 0;
11996 elfsym.st_size = 0;
11997 elfsym.st_info = 0;
11998 elfsym.st_other = 0;
11999 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12000 elfsym.st_target_internal = 0;
ef10c3ac
L
12001 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12002 bfd_und_section_ptr, NULL) != 1)
c152c796 12003 goto error_return;
c152c796 12004
8539e4e8
AM
12005 /* Output a symbol for each section. We output these even if we are
12006 discarding local symbols, since they are used for relocs. These
12007 symbols have no names. We store the index of each one in the
12008 index field of the section, so that we can find it again when
12009 outputting relocs. */
12010
c152c796
AM
12011 elfsym.st_size = 0;
12012 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12013 elfsym.st_other = 0;
f0b5bb34 12014 elfsym.st_value = 0;
35fc36a8 12015 elfsym.st_target_internal = 0;
c152c796
AM
12016 for (i = 1; i < elf_numsections (abfd); i++)
12017 {
12018 o = bfd_section_from_elf_index (abfd, i);
12019 if (o != NULL)
f0b5bb34
AM
12020 {
12021 o->target_index = bfd_get_symcount (abfd);
12022 elfsym.st_shndx = i;
0e1862bb 12023 if (!bfd_link_relocatable (info))
f0b5bb34 12024 elfsym.st_value = o->vma;
ef10c3ac
L
12025 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12026 NULL) != 1)
f0b5bb34
AM
12027 goto error_return;
12028 }
c152c796
AM
12029 }
12030 }
12031
12032 /* Allocate some memory to hold information read in from the input
12033 files. */
12034 if (max_contents_size != 0)
12035 {
8b127cbc
AM
12036 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12037 if (flinfo.contents == NULL)
c152c796
AM
12038 goto error_return;
12039 }
12040
12041 if (max_external_reloc_size != 0)
12042 {
8b127cbc
AM
12043 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12044 if (flinfo.external_relocs == NULL)
c152c796
AM
12045 goto error_return;
12046 }
12047
12048 if (max_internal_reloc_count != 0)
12049 {
056bafd4 12050 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12051 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12052 if (flinfo.internal_relocs == NULL)
c152c796
AM
12053 goto error_return;
12054 }
12055
12056 if (max_sym_count != 0)
12057 {
12058 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12059 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12060 if (flinfo.external_syms == NULL)
c152c796
AM
12061 goto error_return;
12062
12063 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12064 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12065 if (flinfo.internal_syms == NULL)
c152c796
AM
12066 goto error_return;
12067
12068 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12069 flinfo.indices = (long int *) bfd_malloc (amt);
12070 if (flinfo.indices == NULL)
c152c796
AM
12071 goto error_return;
12072
12073 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12074 flinfo.sections = (asection **) bfd_malloc (amt);
12075 if (flinfo.sections == NULL)
c152c796
AM
12076 goto error_return;
12077 }
12078
12079 if (max_sym_shndx_count != 0)
12080 {
12081 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12082 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12083 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12084 goto error_return;
12085 }
12086
64f52338 12087 if (htab->tls_sec)
c152c796
AM
12088 {
12089 bfd_vma base, end = 0;
12090 asection *sec;
12091
64f52338 12092 for (sec = htab->tls_sec;
c152c796
AM
12093 sec && (sec->flags & SEC_THREAD_LOCAL);
12094 sec = sec->next)
12095 {
3a800eb9 12096 bfd_size_type size = sec->size;
c152c796 12097
3a800eb9
AM
12098 if (size == 0
12099 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12100 {
91d6fa6a
NC
12101 struct bfd_link_order *ord = sec->map_tail.link_order;
12102
12103 if (ord != NULL)
12104 size = ord->offset + ord->size;
c152c796
AM
12105 }
12106 end = sec->vma + size;
12107 }
64f52338 12108 base = htab->tls_sec->vma;
7dc98aea
RO
12109 /* Only align end of TLS section if static TLS doesn't have special
12110 alignment requirements. */
12111 if (bed->static_tls_alignment == 1)
64f52338
AM
12112 end = align_power (end, htab->tls_sec->alignment_power);
12113 htab->tls_size = end - base;
c152c796
AM
12114 }
12115
0b52efa6
PB
12116 /* Reorder SHF_LINK_ORDER sections. */
12117 for (o = abfd->sections; o != NULL; o = o->next)
12118 {
12119 if (!elf_fixup_link_order (abfd, o))
12120 return FALSE;
12121 }
12122
2f0c68f2
CM
12123 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12124 return FALSE;
12125
c152c796
AM
12126 /* Since ELF permits relocations to be against local symbols, we
12127 must have the local symbols available when we do the relocations.
12128 Since we would rather only read the local symbols once, and we
12129 would rather not keep them in memory, we handle all the
12130 relocations for a single input file at the same time.
12131
12132 Unfortunately, there is no way to know the total number of local
12133 symbols until we have seen all of them, and the local symbol
12134 indices precede the global symbol indices. This means that when
12135 we are generating relocatable output, and we see a reloc against
12136 a global symbol, we can not know the symbol index until we have
12137 finished examining all the local symbols to see which ones we are
12138 going to output. To deal with this, we keep the relocations in
12139 memory, and don't output them until the end of the link. This is
12140 an unfortunate waste of memory, but I don't see a good way around
12141 it. Fortunately, it only happens when performing a relocatable
12142 link, which is not the common case. FIXME: If keep_memory is set
12143 we could write the relocs out and then read them again; I don't
12144 know how bad the memory loss will be. */
12145
c72f2fb2 12146 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12147 sub->output_has_begun = FALSE;
12148 for (o = abfd->sections; o != NULL; o = o->next)
12149 {
8423293d 12150 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12151 {
12152 if (p->type == bfd_indirect_link_order
12153 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12154 == bfd_target_elf_flavour)
12155 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12156 {
12157 if (! sub->output_has_begun)
12158 {
8b127cbc 12159 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12160 goto error_return;
12161 sub->output_has_begun = TRUE;
12162 }
12163 }
12164 else if (p->type == bfd_section_reloc_link_order
12165 || p->type == bfd_symbol_reloc_link_order)
12166 {
12167 if (! elf_reloc_link_order (abfd, info, o, p))
12168 goto error_return;
12169 }
12170 else
12171 {
12172 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12173 {
12174 if (p->type == bfd_indirect_link_order
12175 && (bfd_get_flavour (sub)
12176 == bfd_target_elf_flavour)
12177 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12178 != bed->s->elfclass))
12179 {
12180 const char *iclass, *oclass;
12181
aebf9be7 12182 switch (bed->s->elfclass)
351f65ca 12183 {
aebf9be7
NC
12184 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12185 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12186 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12187 default: abort ();
351f65ca 12188 }
aebf9be7
NC
12189
12190 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12191 {
aebf9be7
NC
12192 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12193 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12194 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12195 default: abort ();
351f65ca
L
12196 }
12197
12198 bfd_set_error (bfd_error_wrong_format);
4eca0228 12199 _bfd_error_handler
695344c0 12200 /* xgettext:c-format */
871b3ab2 12201 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12202 sub, iclass, oclass);
12203 }
12204
12205 goto error_return;
12206 }
c152c796
AM
12207 }
12208 }
12209 }
12210
c0f00686
L
12211 /* Free symbol buffer if needed. */
12212 if (!info->reduce_memory_overheads)
12213 {
c72f2fb2 12214 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12215 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12216 && elf_tdata (sub)->symbuf)
c0f00686
L
12217 {
12218 free (elf_tdata (sub)->symbuf);
12219 elf_tdata (sub)->symbuf = NULL;
12220 }
12221 }
12222
c152c796
AM
12223 /* Output any global symbols that got converted to local in a
12224 version script or due to symbol visibility. We do this in a
12225 separate step since ELF requires all local symbols to appear
12226 prior to any global symbols. FIXME: We should only do this if
12227 some global symbols were, in fact, converted to become local.
12228 FIXME: Will this work correctly with the Irix 5 linker? */
12229 eoinfo.failed = FALSE;
8b127cbc 12230 eoinfo.flinfo = &flinfo;
c152c796 12231 eoinfo.localsyms = TRUE;
34a79995 12232 eoinfo.file_sym_done = FALSE;
7686d77d 12233 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12234 if (eoinfo.failed)
12235 return FALSE;
12236
4e617b1e
PB
12237 /* If backend needs to output some local symbols not present in the hash
12238 table, do it now. */
8539e4e8
AM
12239 if (bed->elf_backend_output_arch_local_syms
12240 && (info->strip != strip_all || emit_relocs))
4e617b1e 12241 {
6e0b88f1 12242 typedef int (*out_sym_func)
4e617b1e
PB
12243 (void *, const char *, Elf_Internal_Sym *, asection *,
12244 struct elf_link_hash_entry *);
12245
12246 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12247 (abfd, info, &flinfo,
12248 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12249 return FALSE;
12250 }
12251
c152c796
AM
12252 /* That wrote out all the local symbols. Finish up the symbol table
12253 with the global symbols. Even if we want to strip everything we
12254 can, we still need to deal with those global symbols that got
12255 converted to local in a version script. */
12256
12257 /* The sh_info field records the index of the first non local symbol. */
12258 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12259
12260 if (dynamic
64f52338
AM
12261 && htab->dynsym != NULL
12262 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12263 {
12264 Elf_Internal_Sym sym;
64f52338 12265 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12266
64f52338
AM
12267 o = htab->dynsym->output_section;
12268 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12269
12270 /* Write out the section symbols for the output sections. */
0e1862bb 12271 if (bfd_link_pic (info)
64f52338 12272 || htab->is_relocatable_executable)
c152c796
AM
12273 {
12274 asection *s;
12275
12276 sym.st_size = 0;
12277 sym.st_name = 0;
12278 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12279 sym.st_other = 0;
35fc36a8 12280 sym.st_target_internal = 0;
c152c796
AM
12281
12282 for (s = abfd->sections; s != NULL; s = s->next)
12283 {
12284 int indx;
12285 bfd_byte *dest;
12286 long dynindx;
12287
c152c796 12288 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12289 if (dynindx <= 0)
12290 continue;
12291 indx = elf_section_data (s)->this_idx;
c152c796
AM
12292 BFD_ASSERT (indx > 0);
12293 sym.st_shndx = indx;
c0d5a53d
L
12294 if (! check_dynsym (abfd, &sym))
12295 return FALSE;
c152c796
AM
12296 sym.st_value = s->vma;
12297 dest = dynsym + dynindx * bed->s->sizeof_sym;
12298 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12299 }
c152c796
AM
12300 }
12301
12302 /* Write out the local dynsyms. */
64f52338 12303 if (htab->dynlocal)
c152c796
AM
12304 {
12305 struct elf_link_local_dynamic_entry *e;
64f52338 12306 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12307 {
12308 asection *s;
12309 bfd_byte *dest;
12310
935bd1e0 12311 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12312 Note that we saved a word of storage and overwrote
12313 the original st_name with the dynstr_index. */
12314 sym = e->isym;
935bd1e0 12315 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12316
cb33740c
AM
12317 s = bfd_section_from_elf_index (e->input_bfd,
12318 e->isym.st_shndx);
12319 if (s != NULL)
c152c796 12320 {
c152c796
AM
12321 sym.st_shndx =
12322 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12323 if (! check_dynsym (abfd, &sym))
12324 return FALSE;
c152c796
AM
12325 sym.st_value = (s->output_section->vma
12326 + s->output_offset
12327 + e->isym.st_value);
12328 }
12329
c152c796
AM
12330 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12331 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12332 }
12333 }
c152c796
AM
12334 }
12335
12336 /* We get the global symbols from the hash table. */
12337 eoinfo.failed = FALSE;
12338 eoinfo.localsyms = FALSE;
8b127cbc 12339 eoinfo.flinfo = &flinfo;
7686d77d 12340 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12341 if (eoinfo.failed)
12342 return FALSE;
12343
12344 /* If backend needs to output some symbols not present in the hash
12345 table, do it now. */
8539e4e8
AM
12346 if (bed->elf_backend_output_arch_syms
12347 && (info->strip != strip_all || emit_relocs))
c152c796 12348 {
6e0b88f1 12349 typedef int (*out_sym_func)
c152c796
AM
12350 (void *, const char *, Elf_Internal_Sym *, asection *,
12351 struct elf_link_hash_entry *);
12352
12353 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12354 (abfd, info, &flinfo,
12355 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12356 return FALSE;
12357 }
12358
ef10c3ac
L
12359 /* Finalize the .strtab section. */
12360 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12361
12362 /* Swap out the .strtab section. */
12363 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12364 return FALSE;
12365
12366 /* Now we know the size of the symtab section. */
c152c796
AM
12367 if (bfd_get_symcount (abfd) > 0)
12368 {
ee3b52e9
L
12369 /* Finish up and write out the symbol string table (.strtab)
12370 section. */
ad32986f 12371 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12372 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12373
ad32986f 12374 if (elf_symtab_shndx_list (abfd))
8539e4e8 12375 {
ad32986f 12376 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12377
ad32986f
NC
12378 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12379 {
12380 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12381 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12382 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12383 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12384 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12385
ad32986f
NC
12386 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12387 off, TRUE);
12388
12389 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12390 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12391 return FALSE;
12392 }
8539e4e8 12393 }
ee3b52e9
L
12394
12395 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12396 /* sh_name was set in prep_headers. */
12397 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12398 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12399 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12400 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12401 symstrtab_hdr->sh_entsize = 0;
12402 symstrtab_hdr->sh_link = 0;
12403 symstrtab_hdr->sh_info = 0;
12404 /* sh_offset is set just below. */
12405 symstrtab_hdr->sh_addralign = 1;
12406
12407 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12408 off, TRUE);
12409 elf_next_file_pos (abfd) = off;
12410
c152c796 12411 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12412 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12413 return FALSE;
12414 }
12415
76359541
TP
12416 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12417 {
871b3ab2 12418 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12419 info->out_implib_bfd);
76359541
TP
12420 return FALSE;
12421 }
12422
c152c796
AM
12423 /* Adjust the relocs to have the correct symbol indices. */
12424 for (o = abfd->sections; o != NULL; o = o->next)
12425 {
d4730f92 12426 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12427 bfd_boolean sort;
10bbbc1d 12428
c152c796
AM
12429 if ((o->flags & SEC_RELOC) == 0)
12430 continue;
12431
28dbcedc 12432 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12433 if (esdo->rel.hdr != NULL
10bbbc1d 12434 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12435 return FALSE;
12436 if (esdo->rela.hdr != NULL
10bbbc1d 12437 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12438 return FALSE;
c152c796
AM
12439
12440 /* Set the reloc_count field to 0 to prevent write_relocs from
12441 trying to swap the relocs out itself. */
12442 o->reloc_count = 0;
12443 }
12444
12445 if (dynamic && info->combreloc && dynobj != NULL)
12446 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12447
12448 /* If we are linking against a dynamic object, or generating a
12449 shared library, finish up the dynamic linking information. */
12450 if (dynamic)
12451 {
12452 bfd_byte *dyncon, *dynconend;
12453
12454 /* Fix up .dynamic entries. */
3d4d4302 12455 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12456 BFD_ASSERT (o != NULL);
12457
12458 dyncon = o->contents;
eea6121a 12459 dynconend = o->contents + o->size;
c152c796
AM
12460 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12461 {
12462 Elf_Internal_Dyn dyn;
12463 const char *name;
12464 unsigned int type;
64487780
AM
12465 bfd_size_type sh_size;
12466 bfd_vma sh_addr;
c152c796
AM
12467
12468 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12469
12470 switch (dyn.d_tag)
12471 {
12472 default:
12473 continue;
12474 case DT_NULL:
12475 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12476 {
12477 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12478 {
12479 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12480 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12481 default: continue;
12482 }
12483 dyn.d_un.d_val = relativecount;
12484 relativecount = 0;
12485 break;
12486 }
12487 continue;
12488
12489 case DT_INIT:
12490 name = info->init_function;
12491 goto get_sym;
12492 case DT_FINI:
12493 name = info->fini_function;
12494 get_sym:
12495 {
12496 struct elf_link_hash_entry *h;
12497
64f52338 12498 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12499 if (h != NULL
12500 && (h->root.type == bfd_link_hash_defined
12501 || h->root.type == bfd_link_hash_defweak))
12502 {
bef26483 12503 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12504 o = h->root.u.def.section;
12505 if (o->output_section != NULL)
bef26483 12506 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12507 + o->output_offset);
12508 else
12509 {
12510 /* The symbol is imported from another shared
12511 library and does not apply to this one. */
bef26483 12512 dyn.d_un.d_ptr = 0;
c152c796
AM
12513 }
12514 break;
12515 }
12516 }
12517 continue;
12518
12519 case DT_PREINIT_ARRAYSZ:
12520 name = ".preinit_array";
4ade44b7 12521 goto get_out_size;
c152c796
AM
12522 case DT_INIT_ARRAYSZ:
12523 name = ".init_array";
4ade44b7 12524 goto get_out_size;
c152c796
AM
12525 case DT_FINI_ARRAYSZ:
12526 name = ".fini_array";
4ade44b7 12527 get_out_size:
c152c796
AM
12528 o = bfd_get_section_by_name (abfd, name);
12529 if (o == NULL)
12530 {
4eca0228 12531 _bfd_error_handler
4ade44b7 12532 (_("could not find section %s"), name);
c152c796
AM
12533 goto error_return;
12534 }
eea6121a 12535 if (o->size == 0)
4eca0228 12536 _bfd_error_handler
c152c796 12537 (_("warning: %s section has zero size"), name);
eea6121a 12538 dyn.d_un.d_val = o->size;
c152c796
AM
12539 break;
12540
12541 case DT_PREINIT_ARRAY:
12542 name = ".preinit_array";
4ade44b7 12543 goto get_out_vma;
c152c796
AM
12544 case DT_INIT_ARRAY:
12545 name = ".init_array";
4ade44b7 12546 goto get_out_vma;
c152c796
AM
12547 case DT_FINI_ARRAY:
12548 name = ".fini_array";
4ade44b7
AM
12549 get_out_vma:
12550 o = bfd_get_section_by_name (abfd, name);
12551 goto do_vma;
c152c796
AM
12552
12553 case DT_HASH:
12554 name = ".hash";
12555 goto get_vma;
fdc90cb4
JJ
12556 case DT_GNU_HASH:
12557 name = ".gnu.hash";
12558 goto get_vma;
c152c796
AM
12559 case DT_STRTAB:
12560 name = ".dynstr";
12561 goto get_vma;
12562 case DT_SYMTAB:
12563 name = ".dynsym";
12564 goto get_vma;
12565 case DT_VERDEF:
12566 name = ".gnu.version_d";
12567 goto get_vma;
12568 case DT_VERNEED:
12569 name = ".gnu.version_r";
12570 goto get_vma;
12571 case DT_VERSYM:
12572 name = ".gnu.version";
12573 get_vma:
4ade44b7
AM
12574 o = bfd_get_linker_section (dynobj, name);
12575 do_vma:
b3293efa 12576 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12577 {
4eca0228 12578 _bfd_error_handler
4ade44b7 12579 (_("could not find section %s"), name);
c152c796
AM
12580 goto error_return;
12581 }
894891db
NC
12582 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12583 {
4eca0228 12584 _bfd_error_handler
894891db
NC
12585 (_("warning: section '%s' is being made into a note"), name);
12586 bfd_set_error (bfd_error_nonrepresentable_section);
12587 goto error_return;
12588 }
4ade44b7 12589 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12590 break;
12591
12592 case DT_REL:
12593 case DT_RELA:
12594 case DT_RELSZ:
12595 case DT_RELASZ:
12596 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12597 type = SHT_REL;
12598 else
12599 type = SHT_RELA;
64487780
AM
12600 sh_size = 0;
12601 sh_addr = 0;
c152c796
AM
12602 for (i = 1; i < elf_numsections (abfd); i++)
12603 {
12604 Elf_Internal_Shdr *hdr;
12605
12606 hdr = elf_elfsections (abfd)[i];
12607 if (hdr->sh_type == type
12608 && (hdr->sh_flags & SHF_ALLOC) != 0)
12609 {
64487780
AM
12610 sh_size += hdr->sh_size;
12611 if (sh_addr == 0
12612 || sh_addr > hdr->sh_addr)
12613 sh_addr = hdr->sh_addr;
c152c796
AM
12614 }
12615 }
64487780 12616
64f52338
AM
12617 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12618 {
12619 /* Don't count procedure linkage table relocs in the
12620 overall reloc count. */
64487780
AM
12621 sh_size -= htab->srelplt->size;
12622 if (sh_size == 0)
12623 /* If the size is zero, make the address zero too.
12624 This is to avoid a glibc bug. If the backend
12625 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12626 zero, then we'll put DT_RELA at the end of
12627 DT_JMPREL. glibc will interpret the end of
12628 DT_RELA matching the end of DT_JMPREL as the
12629 case where DT_RELA includes DT_JMPREL, and for
12630 LD_BIND_NOW will decide that processing DT_RELA
12631 will process the PLT relocs too. Net result:
12632 No PLT relocs applied. */
12633 sh_addr = 0;
12634
64f52338
AM
12635 /* If .rela.plt is the first .rela section, exclude
12636 it from DT_RELA. */
64487780
AM
12637 else if (sh_addr == (htab->srelplt->output_section->vma
12638 + htab->srelplt->output_offset))
12639 sh_addr += htab->srelplt->size;
64f52338 12640 }
64487780
AM
12641
12642 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12643 dyn.d_un.d_val = sh_size;
12644 else
12645 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12646 break;
12647 }
12648 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12649 }
12650 }
12651
12652 /* If we have created any dynamic sections, then output them. */
12653 if (dynobj != NULL)
12654 {
12655 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12656 goto error_return;
12657
943284cc 12658 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12659 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12660 || info->error_textrel)
3d4d4302 12661 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12662 {
12663 bfd_byte *dyncon, *dynconend;
12664
943284cc
DJ
12665 dyncon = o->contents;
12666 dynconend = o->contents + o->size;
12667 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12668 {
12669 Elf_Internal_Dyn dyn;
12670
12671 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12672
12673 if (dyn.d_tag == DT_TEXTREL)
12674 {
c192a133
AM
12675 if (info->error_textrel)
12676 info->callbacks->einfo
9793eb77 12677 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12678 else
12679 info->callbacks->einfo
9793eb77 12680 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12681 break;
12682 }
12683 }
12684 }
12685
c152c796
AM
12686 for (o = dynobj->sections; o != NULL; o = o->next)
12687 {
12688 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12689 || o->size == 0
c152c796
AM
12690 || o->output_section == bfd_abs_section_ptr)
12691 continue;
12692 if ((o->flags & SEC_LINKER_CREATED) == 0)
12693 {
12694 /* At this point, we are only interested in sections
12695 created by _bfd_elf_link_create_dynamic_sections. */
12696 continue;
12697 }
64f52338 12698 if (htab->stab_info.stabstr == o)
3722b82f 12699 continue;
64f52338 12700 if (htab->eh_info.hdr_sec == o)
eea6121a 12701 continue;
3d4d4302 12702 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12703 {
12704 if (! bfd_set_section_contents (abfd, o->output_section,
12705 o->contents,
37b01f6a
DG
12706 (file_ptr) o->output_offset
12707 * bfd_octets_per_byte (abfd),
eea6121a 12708 o->size))
c152c796
AM
12709 goto error_return;
12710 }
12711 else
12712 {
12713 /* The contents of the .dynstr section are actually in a
12714 stringtab. */
8539e4e8
AM
12715 file_ptr off;
12716
c152c796
AM
12717 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12718 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12719 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12720 goto error_return;
12721 }
12722 }
12723 }
12724
7bdf4127 12725 if (!info->resolve_section_groups)
c152c796
AM
12726 {
12727 bfd_boolean failed = FALSE;
12728
7bdf4127 12729 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12730 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12731 if (failed)
12732 goto error_return;
12733 }
12734
12735 /* If we have optimized stabs strings, output them. */
64f52338 12736 if (htab->stab_info.stabstr != NULL)
c152c796 12737 {
64f52338 12738 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12739 goto error_return;
12740 }
12741
9f7c3e5e
AM
12742 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12743 goto error_return;
c152c796 12744
9f7c3e5e 12745 elf_final_link_free (abfd, &flinfo);
c152c796 12746
12bd6957 12747 elf_linker (abfd) = TRUE;
c152c796 12748
104d59d1
JM
12749 if (attr_section)
12750 {
a50b1753 12751 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12752 if (contents == NULL)
d0f16d5e 12753 return FALSE; /* Bail out and fail. */
104d59d1
JM
12754 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12755 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12756 free (contents);
12757 }
12758
c152c796
AM
12759 return TRUE;
12760
12761 error_return:
9f7c3e5e 12762 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12763 return FALSE;
12764}
12765\f
5241d853
RS
12766/* Initialize COOKIE for input bfd ABFD. */
12767
12768static bfd_boolean
12769init_reloc_cookie (struct elf_reloc_cookie *cookie,
12770 struct bfd_link_info *info, bfd *abfd)
12771{
12772 Elf_Internal_Shdr *symtab_hdr;
12773 const struct elf_backend_data *bed;
12774
12775 bed = get_elf_backend_data (abfd);
12776 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12777
12778 cookie->abfd = abfd;
12779 cookie->sym_hashes = elf_sym_hashes (abfd);
12780 cookie->bad_symtab = elf_bad_symtab (abfd);
12781 if (cookie->bad_symtab)
12782 {
12783 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12784 cookie->extsymoff = 0;
12785 }
12786 else
12787 {
12788 cookie->locsymcount = symtab_hdr->sh_info;
12789 cookie->extsymoff = symtab_hdr->sh_info;
12790 }
12791
12792 if (bed->s->arch_size == 32)
12793 cookie->r_sym_shift = 8;
12794 else
12795 cookie->r_sym_shift = 32;
12796
12797 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12798 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12799 {
12800 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12801 cookie->locsymcount, 0,
12802 NULL, NULL, NULL);
12803 if (cookie->locsyms == NULL)
12804 {
12805 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12806 return FALSE;
12807 }
12808 if (info->keep_memory)
12809 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12810 }
12811 return TRUE;
12812}
12813
12814/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12815
12816static void
12817fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12818{
12819 Elf_Internal_Shdr *symtab_hdr;
12820
12821 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12822 if (cookie->locsyms != NULL
12823 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12824 free (cookie->locsyms);
12825}
12826
12827/* Initialize the relocation information in COOKIE for input section SEC
12828 of input bfd ABFD. */
12829
12830static bfd_boolean
12831init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12832 struct bfd_link_info *info, bfd *abfd,
12833 asection *sec)
12834{
5241d853
RS
12835 if (sec->reloc_count == 0)
12836 {
12837 cookie->rels = NULL;
12838 cookie->relend = NULL;
12839 }
12840 else
12841 {
5241d853
RS
12842 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12843 info->keep_memory);
12844 if (cookie->rels == NULL)
12845 return FALSE;
12846 cookie->rel = cookie->rels;
056bafd4 12847 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12848 }
12849 cookie->rel = cookie->rels;
12850 return TRUE;
12851}
12852
12853/* Free the memory allocated by init_reloc_cookie_rels,
12854 if appropriate. */
12855
12856static void
12857fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12858 asection *sec)
12859{
12860 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12861 free (cookie->rels);
12862}
12863
12864/* Initialize the whole of COOKIE for input section SEC. */
12865
12866static bfd_boolean
12867init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12868 struct bfd_link_info *info,
12869 asection *sec)
12870{
12871 if (!init_reloc_cookie (cookie, info, sec->owner))
12872 goto error1;
12873 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12874 goto error2;
12875 return TRUE;
12876
12877 error2:
12878 fini_reloc_cookie (cookie, sec->owner);
12879 error1:
12880 return FALSE;
12881}
12882
12883/* Free the memory allocated by init_reloc_cookie_for_section,
12884 if appropriate. */
12885
12886static void
12887fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12888 asection *sec)
12889{
12890 fini_reloc_cookie_rels (cookie, sec);
12891 fini_reloc_cookie (cookie, sec->owner);
12892}
12893\f
c152c796
AM
12894/* Garbage collect unused sections. */
12895
07adf181
AM
12896/* Default gc_mark_hook. */
12897
12898asection *
12899_bfd_elf_gc_mark_hook (asection *sec,
12900 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12901 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12902 struct elf_link_hash_entry *h,
12903 Elf_Internal_Sym *sym)
12904{
12905 if (h != NULL)
12906 {
12907 switch (h->root.type)
12908 {
12909 case bfd_link_hash_defined:
12910 case bfd_link_hash_defweak:
12911 return h->root.u.def.section;
12912
12913 case bfd_link_hash_common:
12914 return h->root.u.c.p->section;
12915
12916 default:
12917 break;
12918 }
12919 }
12920 else
12921 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12922
12923 return NULL;
12924}
12925
9e223787 12926/* Return the debug definition section. */
b7c871ed
L
12927
12928static asection *
12929elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12930 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12931 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12932 struct elf_link_hash_entry *h,
9e223787 12933 Elf_Internal_Sym *sym)
b7c871ed 12934{
9e223787
L
12935 if (h != NULL)
12936 {
12937 /* Return the global debug definition section. */
12938 if ((h->root.type == bfd_link_hash_defined
12939 || h->root.type == bfd_link_hash_defweak)
12940 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12941 return h->root.u.def.section;
12942 }
12943 else
12944 {
12945 /* Return the local debug definition section. */
12946 asection *isec = bfd_section_from_elf_index (sec->owner,
12947 sym->st_shndx);
12948 if ((isec->flags & SEC_DEBUGGING) != 0)
12949 return isec;
12950 }
b7c871ed
L
12951
12952 return NULL;
12953}
12954
5241d853
RS
12955/* COOKIE->rel describes a relocation against section SEC, which is
12956 a section we've decided to keep. Return the section that contains
12957 the relocation symbol, or NULL if no section contains it. */
12958
12959asection *
12960_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12961 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12962 struct elf_reloc_cookie *cookie,
12963 bfd_boolean *start_stop)
5241d853
RS
12964{
12965 unsigned long r_symndx;
12966 struct elf_link_hash_entry *h;
12967
12968 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12969 if (r_symndx == STN_UNDEF)
5241d853
RS
12970 return NULL;
12971
12972 if (r_symndx >= cookie->locsymcount
12973 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12974 {
12975 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12976 if (h == NULL)
12977 {
871b3ab2 12978 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
12979 sec->owner);
12980 return NULL;
12981 }
5241d853
RS
12982 while (h->root.type == bfd_link_hash_indirect
12983 || h->root.type == bfd_link_hash_warning)
12984 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12985 h->mark = 1;
4e6b54a6
AM
12986 /* If this symbol is weak and there is a non-weak definition, we
12987 keep the non-weak definition because many backends put
12988 dynamic reloc info on the non-weak definition for code
12989 handling copy relocs. */
60d67dc8
AM
12990 if (h->is_weakalias)
12991 weakdef (h)->mark = 1;
1cce69b9 12992
a6a4679f 12993 if (start_stop != NULL)
1cce69b9 12994 {
7dba9362
AM
12995 /* To work around a glibc bug, mark XXX input sections
12996 when there is a reference to __start_XXX or __stop_XXX
12997 symbols. */
cbd0eecf 12998 if (h->start_stop)
1cce69b9 12999 {
cbd0eecf 13000 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13001 *start_stop = !s->gc_mark;
13002 return s;
1cce69b9
AM
13003 }
13004 }
13005
5241d853
RS
13006 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13007 }
13008
13009 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13010 &cookie->locsyms[r_symndx]);
13011}
13012
13013/* COOKIE->rel describes a relocation against section SEC, which is
13014 a section we've decided to keep. Mark the section that contains
9d0a14d3 13015 the relocation symbol. */
5241d853
RS
13016
13017bfd_boolean
13018_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13019 asection *sec,
13020 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13021 struct elf_reloc_cookie *cookie)
5241d853
RS
13022{
13023 asection *rsec;
1cce69b9 13024 bfd_boolean start_stop = FALSE;
5241d853 13025
1cce69b9
AM
13026 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13027 while (rsec != NULL)
5241d853 13028 {
1cce69b9
AM
13029 if (!rsec->gc_mark)
13030 {
13031 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13032 || (rsec->owner->flags & DYNAMIC) != 0)
13033 rsec->gc_mark = 1;
13034 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13035 return FALSE;
13036 }
13037 if (!start_stop)
13038 break;
199af150 13039 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13040 }
13041 return TRUE;
13042}
13043
07adf181
AM
13044/* The mark phase of garbage collection. For a given section, mark
13045 it and any sections in this section's group, and all the sections
13046 which define symbols to which it refers. */
13047
ccfa59ea
AM
13048bfd_boolean
13049_bfd_elf_gc_mark (struct bfd_link_info *info,
13050 asection *sec,
6a5bb875 13051 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13052{
13053 bfd_boolean ret;
9d0a14d3 13054 asection *group_sec, *eh_frame;
c152c796
AM
13055
13056 sec->gc_mark = 1;
13057
13058 /* Mark all the sections in the group. */
13059 group_sec = elf_section_data (sec)->next_in_group;
13060 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13061 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13062 return FALSE;
13063
13064 /* Look through the section relocs. */
13065 ret = TRUE;
9d0a14d3
RS
13066 eh_frame = elf_eh_frame_section (sec->owner);
13067 if ((sec->flags & SEC_RELOC) != 0
13068 && sec->reloc_count > 0
13069 && sec != eh_frame)
c152c796 13070 {
5241d853 13071 struct elf_reloc_cookie cookie;
c152c796 13072
5241d853
RS
13073 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13074 ret = FALSE;
c152c796 13075 else
c152c796 13076 {
5241d853 13077 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13078 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13079 {
13080 ret = FALSE;
13081 break;
13082 }
13083 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13084 }
13085 }
9d0a14d3
RS
13086
13087 if (ret && eh_frame && elf_fde_list (sec))
13088 {
13089 struct elf_reloc_cookie cookie;
13090
13091 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13092 ret = FALSE;
13093 else
13094 {
13095 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13096 gc_mark_hook, &cookie))
13097 ret = FALSE;
13098 fini_reloc_cookie_for_section (&cookie, eh_frame);
13099 }
13100 }
13101
2f0c68f2
CM
13102 eh_frame = elf_section_eh_frame_entry (sec);
13103 if (ret && eh_frame && !eh_frame->gc_mark)
13104 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13105 ret = FALSE;
13106
c152c796
AM
13107 return ret;
13108}
13109
3c758495
TG
13110/* Scan and mark sections in a special or debug section group. */
13111
13112static void
13113_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13114{
13115 /* Point to first section of section group. */
13116 asection *ssec;
13117 /* Used to iterate the section group. */
13118 asection *msec;
13119
13120 bfd_boolean is_special_grp = TRUE;
13121 bfd_boolean is_debug_grp = TRUE;
13122
13123 /* First scan to see if group contains any section other than debug
13124 and special section. */
13125 ssec = msec = elf_next_in_group (grp);
13126 do
13127 {
13128 if ((msec->flags & SEC_DEBUGGING) == 0)
13129 is_debug_grp = FALSE;
13130
13131 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13132 is_special_grp = FALSE;
13133
13134 msec = elf_next_in_group (msec);
13135 }
13136 while (msec != ssec);
13137
13138 /* If this is a pure debug section group or pure special section group,
13139 keep all sections in this group. */
13140 if (is_debug_grp || is_special_grp)
13141 {
13142 do
13143 {
13144 msec->gc_mark = 1;
13145 msec = elf_next_in_group (msec);
13146 }
13147 while (msec != ssec);
13148 }
13149}
13150
7f6ab9f8
AM
13151/* Keep debug and special sections. */
13152
13153bfd_boolean
13154_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13155 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13156{
13157 bfd *ibfd;
13158
c72f2fb2 13159 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13160 {
13161 asection *isec;
13162 bfd_boolean some_kept;
b40bf0a2 13163 bfd_boolean debug_frag_seen;
b7c871ed 13164 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13165
13166 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13167 continue;
57963c05
AM
13168 isec = ibfd->sections;
13169 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13170 continue;
7f6ab9f8 13171
b40bf0a2
NC
13172 /* Ensure all linker created sections are kept,
13173 see if any other section is already marked,
13174 and note if we have any fragmented debug sections. */
b7c871ed 13175 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13176 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13177 {
13178 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13179 isec->gc_mark = 1;
eb026f09
AM
13180 else if (isec->gc_mark
13181 && (isec->flags & SEC_ALLOC) != 0
13182 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13183 some_kept = TRUE;
b40bf0a2 13184
535b785f 13185 if (!debug_frag_seen
b40bf0a2
NC
13186 && (isec->flags & SEC_DEBUGGING)
13187 && CONST_STRNEQ (isec->name, ".debug_line."))
13188 debug_frag_seen = TRUE;
7f6ab9f8
AM
13189 }
13190
eb026f09
AM
13191 /* If no non-note alloc section in this file will be kept, then
13192 we can toss out the debug and special sections. */
7f6ab9f8
AM
13193 if (!some_kept)
13194 continue;
13195
13196 /* Keep debug and special sections like .comment when they are
3c758495
TG
13197 not part of a group. Also keep section groups that contain
13198 just debug sections or special sections. */
7f6ab9f8 13199 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13200 {
13201 if ((isec->flags & SEC_GROUP) != 0)
13202 _bfd_elf_gc_mark_debug_special_section_group (isec);
13203 else if (((isec->flags & SEC_DEBUGGING) != 0
13204 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13205 && elf_next_in_group (isec) == NULL)
13206 isec->gc_mark = 1;
b7c871ed
L
13207 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13208 has_kept_debug_info = TRUE;
3c758495 13209 }
b40bf0a2 13210
b40bf0a2
NC
13211 /* Look for CODE sections which are going to be discarded,
13212 and find and discard any fragmented debug sections which
13213 are associated with that code section. */
b7c871ed
L
13214 if (debug_frag_seen)
13215 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13216 if ((isec->flags & SEC_CODE) != 0
13217 && isec->gc_mark == 0)
13218 {
13219 unsigned int ilen;
13220 asection *dsec;
b40bf0a2 13221
b7c871ed 13222 ilen = strlen (isec->name);
b40bf0a2 13223
b7c871ed 13224 /* Association is determined by the name of the debug
07d6d2b8 13225 section containing the name of the code section as
b7c871ed
L
13226 a suffix. For example .debug_line.text.foo is a
13227 debug section associated with .text.foo. */
13228 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13229 {
13230 unsigned int dlen;
b40bf0a2 13231
b7c871ed
L
13232 if (dsec->gc_mark == 0
13233 || (dsec->flags & SEC_DEBUGGING) == 0)
13234 continue;
b40bf0a2 13235
b7c871ed 13236 dlen = strlen (dsec->name);
b40bf0a2 13237
b7c871ed
L
13238 if (dlen > ilen
13239 && strncmp (dsec->name + (dlen - ilen),
13240 isec->name, ilen) == 0)
b40bf0a2 13241 dsec->gc_mark = 0;
b7c871ed 13242 }
b40bf0a2 13243 }
b7c871ed
L
13244
13245 /* Mark debug sections referenced by kept debug sections. */
13246 if (has_kept_debug_info)
13247 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13248 if (isec->gc_mark
13249 && (isec->flags & SEC_DEBUGGING) != 0)
13250 if (!_bfd_elf_gc_mark (info, isec,
13251 elf_gc_mark_debug_section))
13252 return FALSE;
7f6ab9f8
AM
13253 }
13254 return TRUE;
13255}
13256
c152c796 13257static bfd_boolean
ccabcbe5 13258elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13259{
13260 bfd *sub;
ccabcbe5 13261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13262
c72f2fb2 13263 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13264 {
13265 asection *o;
13266
b19a8f85 13267 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13268 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13269 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13270 continue;
57963c05
AM
13271 o = sub->sections;
13272 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13273 continue;
c152c796
AM
13274
13275 for (o = sub->sections; o != NULL; o = o->next)
13276 {
a33dafc3
L
13277 /* When any section in a section group is kept, we keep all
13278 sections in the section group. If the first member of
13279 the section group is excluded, we will also exclude the
13280 group section. */
13281 if (o->flags & SEC_GROUP)
13282 {
13283 asection *first = elf_next_in_group (o);
13284 o->gc_mark = first->gc_mark;
13285 }
c152c796 13286
1e7eae0d 13287 if (o->gc_mark)
c152c796
AM
13288 continue;
13289
13290 /* Skip sweeping sections already excluded. */
13291 if (o->flags & SEC_EXCLUDE)
13292 continue;
13293
13294 /* Since this is early in the link process, it is simple
13295 to remove a section from the output. */
13296 o->flags |= SEC_EXCLUDE;
13297
c55fe096 13298 if (info->print_gc_sections && o->size != 0)
695344c0 13299 /* xgettext:c-format */
9793eb77 13300 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13301 o, sub);
c152c796
AM
13302 }
13303 }
13304
c152c796
AM
13305 return TRUE;
13306}
13307
13308/* Propagate collected vtable information. This is called through
13309 elf_link_hash_traverse. */
13310
13311static bfd_boolean
13312elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13313{
c152c796 13314 /* Those that are not vtables. */
cbd0eecf
L
13315 if (h->start_stop
13316 || h->u2.vtable == NULL
13317 || h->u2.vtable->parent == NULL)
c152c796
AM
13318 return TRUE;
13319
13320 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13321 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13322 return TRUE;
13323
13324 /* If we've already been done, exit. */
cbd0eecf 13325 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13326 return TRUE;
13327
13328 /* Make sure the parent's table is up to date. */
cbd0eecf 13329 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13330
cbd0eecf 13331 if (h->u2.vtable->used == NULL)
c152c796
AM
13332 {
13333 /* None of this table's entries were referenced. Re-use the
13334 parent's table. */
cbd0eecf
L
13335 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13336 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13337 }
13338 else
13339 {
13340 size_t n;
13341 bfd_boolean *cu, *pu;
13342
13343 /* Or the parent's entries into ours. */
cbd0eecf 13344 cu = h->u2.vtable->used;
c152c796 13345 cu[-1] = TRUE;
cbd0eecf 13346 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13347 if (pu != NULL)
13348 {
13349 const struct elf_backend_data *bed;
13350 unsigned int log_file_align;
13351
13352 bed = get_elf_backend_data (h->root.u.def.section->owner);
13353 log_file_align = bed->s->log_file_align;
cbd0eecf 13354 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13355 while (n--)
13356 {
13357 if (*pu)
13358 *cu = TRUE;
13359 pu++;
13360 cu++;
13361 }
13362 }
13363 }
13364
13365 return TRUE;
13366}
13367
13368static bfd_boolean
13369elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13370{
13371 asection *sec;
13372 bfd_vma hstart, hend;
13373 Elf_Internal_Rela *relstart, *relend, *rel;
13374 const struct elf_backend_data *bed;
13375 unsigned int log_file_align;
13376
c152c796
AM
13377 /* Take care of both those symbols that do not describe vtables as
13378 well as those that are not loaded. */
cbd0eecf
L
13379 if (h->start_stop
13380 || h->u2.vtable == NULL
13381 || h->u2.vtable->parent == NULL)
c152c796
AM
13382 return TRUE;
13383
13384 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13385 || h->root.type == bfd_link_hash_defweak);
13386
13387 sec = h->root.u.def.section;
13388 hstart = h->root.u.def.value;
13389 hend = hstart + h->size;
13390
13391 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13392 if (!relstart)
13393 return *(bfd_boolean *) okp = FALSE;
13394 bed = get_elf_backend_data (sec->owner);
13395 log_file_align = bed->s->log_file_align;
13396
056bafd4 13397 relend = relstart + sec->reloc_count;
c152c796
AM
13398
13399 for (rel = relstart; rel < relend; ++rel)
13400 if (rel->r_offset >= hstart && rel->r_offset < hend)
13401 {
13402 /* If the entry is in use, do nothing. */
cbd0eecf
L
13403 if (h->u2.vtable->used
13404 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13405 {
13406 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13407 if (h->u2.vtable->used[entry])
c152c796
AM
13408 continue;
13409 }
13410 /* Otherwise, kill it. */
13411 rel->r_offset = rel->r_info = rel->r_addend = 0;
13412 }
13413
13414 return TRUE;
13415}
13416
87538722
AM
13417/* Mark sections containing dynamically referenced symbols. When
13418 building shared libraries, we must assume that any visible symbol is
13419 referenced. */
715df9b8 13420
64d03ab5
AM
13421bfd_boolean
13422bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13423{
87538722 13424 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13425 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13426
715df9b8
EB
13427 if ((h->root.type == bfd_link_hash_defined
13428 || h->root.type == bfd_link_hash_defweak)
d664fd41 13429 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13430 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13431 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13432 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13433 && (!bfd_link_executable (info)
22185505 13434 || info->gc_keep_exported
b407645f
AM
13435 || info->export_dynamic
13436 || (h->dynamic
13437 && d != NULL
13438 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13439 && (h->versioned >= versioned
54e8959c
L
13440 || !bfd_hide_sym_by_version (info->version_info,
13441 h->root.root.string)))))
715df9b8
EB
13442 h->root.u.def.section->flags |= SEC_KEEP;
13443
13444 return TRUE;
13445}
3b36f7e6 13446
74f0fb50
AM
13447/* Keep all sections containing symbols undefined on the command-line,
13448 and the section containing the entry symbol. */
13449
13450void
13451_bfd_elf_gc_keep (struct bfd_link_info *info)
13452{
13453 struct bfd_sym_chain *sym;
13454
13455 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13456 {
13457 struct elf_link_hash_entry *h;
13458
13459 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13460 FALSE, FALSE, FALSE);
13461
13462 if (h != NULL
13463 && (h->root.type == bfd_link_hash_defined
13464 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13465 && !bfd_is_abs_section (h->root.u.def.section)
13466 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13467 h->root.u.def.section->flags |= SEC_KEEP;
13468 }
13469}
13470
2f0c68f2
CM
13471bfd_boolean
13472bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13473 struct bfd_link_info *info)
13474{
13475 bfd *ibfd = info->input_bfds;
13476
13477 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13478 {
13479 asection *sec;
13480 struct elf_reloc_cookie cookie;
13481
13482 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13483 continue;
57963c05
AM
13484 sec = ibfd->sections;
13485 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13486 continue;
2f0c68f2
CM
13487
13488 if (!init_reloc_cookie (&cookie, info, ibfd))
13489 return FALSE;
13490
13491 for (sec = ibfd->sections; sec; sec = sec->next)
13492 {
13493 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13494 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13495 {
13496 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13497 fini_reloc_cookie_rels (&cookie, sec);
13498 }
13499 }
13500 }
13501 return TRUE;
13502}
13503
c152c796
AM
13504/* Do mark and sweep of unused sections. */
13505
13506bfd_boolean
13507bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13508{
13509 bfd_boolean ok = TRUE;
13510 bfd *sub;
6a5bb875 13511 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13512 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13513 struct elf_link_hash_table *htab;
c152c796 13514
64d03ab5 13515 if (!bed->can_gc_sections
715df9b8 13516 || !is_elf_hash_table (info->hash))
c152c796 13517 {
9793eb77 13518 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13519 return TRUE;
13520 }
13521
74f0fb50 13522 bed->gc_keep (info);
da44f4e5 13523 htab = elf_hash_table (info);
74f0fb50 13524
9d0a14d3
RS
13525 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13526 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13527 for (sub = info->input_bfds;
13528 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13529 sub = sub->link.next)
9d0a14d3
RS
13530 {
13531 asection *sec;
13532 struct elf_reloc_cookie cookie;
13533
57963c05
AM
13534 sec = sub->sections;
13535 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13536 continue;
9d0a14d3 13537 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13538 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13539 {
13540 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13541 if (elf_section_data (sec)->sec_info
13542 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13543 elf_eh_frame_section (sub) = sec;
13544 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13545 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13546 }
13547 }
9d0a14d3 13548
c152c796 13549 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13550 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13551 if (!ok)
13552 return FALSE;
13553
13554 /* Kill the vtable relocations that were not used. */
da44f4e5 13555 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13556 if (!ok)
13557 return FALSE;
13558
715df9b8 13559 /* Mark dynamically referenced symbols. */
22185505 13560 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13561 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13562
715df9b8 13563 /* Grovel through relocs to find out who stays ... */
64d03ab5 13564 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13565 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13566 {
13567 asection *o;
13568
b19a8f85 13569 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13570 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13571 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13572 continue;
13573
57963c05
AM
13574 o = sub->sections;
13575 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13576 continue;
13577
7f6ab9f8
AM
13578 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13579 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13580 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13581 well as FINI_ARRAY sections for ld -r. */
c152c796 13582 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13583 if (!o->gc_mark
13584 && (o->flags & SEC_EXCLUDE) == 0
24007750 13585 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13586 || (bfd_link_relocatable (info)
13587 && ((elf_section_data (o)->this_hdr.sh_type
13588 == SHT_PREINIT_ARRAY)
13589 || (elf_section_data (o)->this_hdr.sh_type
13590 == SHT_INIT_ARRAY)
13591 || (elf_section_data (o)->this_hdr.sh_type
13592 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13593 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13594 && elf_next_in_group (o) == NULL )))
13595 {
13596 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13597 return FALSE;
13598 }
c152c796
AM
13599 }
13600
6a5bb875 13601 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13602 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13603
c152c796 13604 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13605 return elf_gc_sweep (abfd, info);
c152c796
AM
13606}
13607\f
13608/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13609
13610bfd_boolean
13611bfd_elf_gc_record_vtinherit (bfd *abfd,
13612 asection *sec,
13613 struct elf_link_hash_entry *h,
13614 bfd_vma offset)
13615{
13616 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13617 struct elf_link_hash_entry **search, *child;
ef53be89 13618 size_t extsymcount;
c152c796
AM
13619 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13620
13621 /* The sh_info field of the symtab header tells us where the
13622 external symbols start. We don't care about the local symbols at
13623 this point. */
13624 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13625 if (!elf_bad_symtab (abfd))
13626 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13627
13628 sym_hashes = elf_sym_hashes (abfd);
13629 sym_hashes_end = sym_hashes + extsymcount;
13630
13631 /* Hunt down the child symbol, which is in this section at the same
13632 offset as the relocation. */
13633 for (search = sym_hashes; search != sym_hashes_end; ++search)
13634 {
13635 if ((child = *search) != NULL
13636 && (child->root.type == bfd_link_hash_defined
13637 || child->root.type == bfd_link_hash_defweak)
13638 && child->root.u.def.section == sec
13639 && child->root.u.def.value == offset)
13640 goto win;
13641 }
13642
695344c0 13643 /* xgettext:c-format */
9793eb77 13644 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13645 abfd, sec, (uint64_t) offset);
c152c796
AM
13646 bfd_set_error (bfd_error_invalid_operation);
13647 return FALSE;
13648
13649 win:
cbd0eecf 13650 if (!child->u2.vtable)
f6e332e6 13651 {
cbd0eecf
L
13652 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13653 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13654 if (!child->u2.vtable)
f6e332e6
AM
13655 return FALSE;
13656 }
c152c796
AM
13657 if (!h)
13658 {
13659 /* This *should* only be the absolute section. It could potentially
13660 be that someone has defined a non-global vtable though, which
13661 would be bad. It isn't worth paging in the local symbols to be
13662 sure though; that case should simply be handled by the assembler. */
13663
cbd0eecf 13664 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13665 }
13666 else
cbd0eecf 13667 child->u2.vtable->parent = h;
c152c796
AM
13668
13669 return TRUE;
13670}
13671
13672/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13673
13674bfd_boolean
13675bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13676 asection *sec ATTRIBUTE_UNUSED,
13677 struct elf_link_hash_entry *h,
13678 bfd_vma addend)
13679{
13680 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13681 unsigned int log_file_align = bed->s->log_file_align;
13682
cbd0eecf 13683 if (!h->u2.vtable)
f6e332e6 13684 {
cbd0eecf
L
13685 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13686 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13687 if (!h->u2.vtable)
f6e332e6
AM
13688 return FALSE;
13689 }
13690
cbd0eecf 13691 if (addend >= h->u2.vtable->size)
c152c796
AM
13692 {
13693 size_t size, bytes, file_align;
cbd0eecf 13694 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13695
13696 /* While the symbol is undefined, we have to be prepared to handle
13697 a zero size. */
13698 file_align = 1 << log_file_align;
13699 if (h->root.type == bfd_link_hash_undefined)
13700 size = addend + file_align;
13701 else
13702 {
13703 size = h->size;
13704 if (addend >= size)
13705 {
13706 /* Oops! We've got a reference past the defined end of
13707 the table. This is probably a bug -- shall we warn? */
13708 size = addend + file_align;
13709 }
13710 }
13711 size = (size + file_align - 1) & -file_align;
13712
13713 /* Allocate one extra entry for use as a "done" flag for the
13714 consolidation pass. */
13715 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13716
13717 if (ptr)
13718 {
a50b1753 13719 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13720
13721 if (ptr != NULL)
13722 {
13723 size_t oldbytes;
13724
cbd0eecf 13725 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13726 * sizeof (bfd_boolean));
13727 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13728 }
13729 }
13730 else
a50b1753 13731 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13732
13733 if (ptr == NULL)
13734 return FALSE;
13735
13736 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13737 h->u2.vtable->used = ptr + 1;
13738 h->u2.vtable->size = size;
c152c796
AM
13739 }
13740
cbd0eecf 13741 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13742
13743 return TRUE;
13744}
13745
ae17ab41
CM
13746/* Map an ELF section header flag to its corresponding string. */
13747typedef struct
13748{
13749 char *flag_name;
13750 flagword flag_value;
13751} elf_flags_to_name_table;
13752
13753static elf_flags_to_name_table elf_flags_to_names [] =
13754{
13755 { "SHF_WRITE", SHF_WRITE },
13756 { "SHF_ALLOC", SHF_ALLOC },
13757 { "SHF_EXECINSTR", SHF_EXECINSTR },
13758 { "SHF_MERGE", SHF_MERGE },
13759 { "SHF_STRINGS", SHF_STRINGS },
13760 { "SHF_INFO_LINK", SHF_INFO_LINK},
13761 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13762 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13763 { "SHF_GROUP", SHF_GROUP },
13764 { "SHF_TLS", SHF_TLS },
13765 { "SHF_MASKOS", SHF_MASKOS },
13766 { "SHF_EXCLUDE", SHF_EXCLUDE },
13767};
13768
b9c361e0
JL
13769/* Returns TRUE if the section is to be included, otherwise FALSE. */
13770bfd_boolean
ae17ab41 13771bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13772 struct flag_info *flaginfo,
b9c361e0 13773 asection *section)
ae17ab41 13774{
8b127cbc 13775 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13776
8b127cbc 13777 if (!flaginfo->flags_initialized)
ae17ab41 13778 {
8b127cbc
AM
13779 bfd *obfd = info->output_bfd;
13780 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13781 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13782 int with_hex = 0;
13783 int without_hex = 0;
13784
8b127cbc 13785 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13786 {
b9c361e0 13787 unsigned i;
8b127cbc 13788 flagword (*lookup) (char *);
ae17ab41 13789
8b127cbc
AM
13790 lookup = bed->elf_backend_lookup_section_flags_hook;
13791 if (lookup != NULL)
ae17ab41 13792 {
8b127cbc 13793 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13794
13795 if (hexval != 0)
13796 {
13797 if (tf->with == with_flags)
13798 with_hex |= hexval;
13799 else if (tf->with == without_flags)
13800 without_hex |= hexval;
13801 tf->valid = TRUE;
13802 continue;
13803 }
ae17ab41 13804 }
8b127cbc 13805 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13806 {
8b127cbc 13807 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13808 {
13809 if (tf->with == with_flags)
13810 with_hex |= elf_flags_to_names[i].flag_value;
13811 else if (tf->with == without_flags)
13812 without_hex |= elf_flags_to_names[i].flag_value;
13813 tf->valid = TRUE;
13814 break;
13815 }
13816 }
8b127cbc 13817 if (!tf->valid)
b9c361e0 13818 {
68ffbac6 13819 info->callbacks->einfo
9793eb77 13820 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13821 return FALSE;
ae17ab41
CM
13822 }
13823 }
8b127cbc
AM
13824 flaginfo->flags_initialized = TRUE;
13825 flaginfo->only_with_flags |= with_hex;
13826 flaginfo->not_with_flags |= without_hex;
ae17ab41 13827 }
ae17ab41 13828
8b127cbc 13829 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13830 return FALSE;
13831
8b127cbc 13832 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13833 return FALSE;
13834
13835 return TRUE;
ae17ab41
CM
13836}
13837
c152c796
AM
13838struct alloc_got_off_arg {
13839 bfd_vma gotoff;
10455f89 13840 struct bfd_link_info *info;
c152c796
AM
13841};
13842
13843/* We need a special top-level link routine to convert got reference counts
13844 to real got offsets. */
13845
13846static bfd_boolean
13847elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13848{
a50b1753 13849 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13850 bfd *obfd = gofarg->info->output_bfd;
13851 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13852
c152c796
AM
13853 if (h->got.refcount > 0)
13854 {
13855 h->got.offset = gofarg->gotoff;
10455f89 13856 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13857 }
13858 else
13859 h->got.offset = (bfd_vma) -1;
13860
13861 return TRUE;
13862}
13863
13864/* And an accompanying bit to work out final got entry offsets once
13865 we're done. Should be called from final_link. */
13866
13867bfd_boolean
13868bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13869 struct bfd_link_info *info)
13870{
13871 bfd *i;
13872 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13873 bfd_vma gotoff;
c152c796
AM
13874 struct alloc_got_off_arg gofarg;
13875
10455f89
HPN
13876 BFD_ASSERT (abfd == info->output_bfd);
13877
c152c796
AM
13878 if (! is_elf_hash_table (info->hash))
13879 return FALSE;
13880
13881 /* The GOT offset is relative to the .got section, but the GOT header is
13882 put into the .got.plt section, if the backend uses it. */
13883 if (bed->want_got_plt)
13884 gotoff = 0;
13885 else
13886 gotoff = bed->got_header_size;
13887
13888 /* Do the local .got entries first. */
c72f2fb2 13889 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13890 {
13891 bfd_signed_vma *local_got;
ef53be89 13892 size_t j, locsymcount;
c152c796
AM
13893 Elf_Internal_Shdr *symtab_hdr;
13894
13895 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13896 continue;
13897
13898 local_got = elf_local_got_refcounts (i);
13899 if (!local_got)
13900 continue;
13901
13902 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13903 if (elf_bad_symtab (i))
13904 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13905 else
13906 locsymcount = symtab_hdr->sh_info;
13907
13908 for (j = 0; j < locsymcount; ++j)
13909 {
13910 if (local_got[j] > 0)
13911 {
13912 local_got[j] = gotoff;
10455f89 13913 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13914 }
13915 else
13916 local_got[j] = (bfd_vma) -1;
13917 }
13918 }
13919
13920 /* Then the global .got entries. .plt refcounts are handled by
13921 adjust_dynamic_symbol */
13922 gofarg.gotoff = gotoff;
10455f89 13923 gofarg.info = info;
c152c796
AM
13924 elf_link_hash_traverse (elf_hash_table (info),
13925 elf_gc_allocate_got_offsets,
13926 &gofarg);
13927 return TRUE;
13928}
13929
13930/* Many folk need no more in the way of final link than this, once
13931 got entry reference counting is enabled. */
13932
13933bfd_boolean
13934bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13935{
13936 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13937 return FALSE;
13938
13939 /* Invoke the regular ELF backend linker to do all the work. */
13940 return bfd_elf_final_link (abfd, info);
13941}
13942
13943bfd_boolean
13944bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13945{
a50b1753 13946 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13947
13948 if (rcookie->bad_symtab)
13949 rcookie->rel = rcookie->rels;
13950
13951 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13952 {
13953 unsigned long r_symndx;
13954
13955 if (! rcookie->bad_symtab)
13956 if (rcookie->rel->r_offset > offset)
13957 return FALSE;
13958 if (rcookie->rel->r_offset != offset)
13959 continue;
13960
13961 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13962 if (r_symndx == STN_UNDEF)
c152c796
AM
13963 return TRUE;
13964
13965 if (r_symndx >= rcookie->locsymcount
13966 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13967 {
13968 struct elf_link_hash_entry *h;
13969
13970 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13971
13972 while (h->root.type == bfd_link_hash_indirect
13973 || h->root.type == bfd_link_hash_warning)
13974 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13975
13976 if ((h->root.type == bfd_link_hash_defined
13977 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13978 && (h->root.u.def.section->owner != rcookie->abfd
13979 || h->root.u.def.section->kept_section != NULL
13980 || discarded_section (h->root.u.def.section)))
c152c796 13981 return TRUE;
c152c796
AM
13982 }
13983 else
13984 {
13985 /* It's not a relocation against a global symbol,
13986 but it could be a relocation against a local
13987 symbol for a discarded section. */
13988 asection *isec;
13989 Elf_Internal_Sym *isym;
13990
13991 /* Need to: get the symbol; get the section. */
13992 isym = &rcookie->locsyms[r_symndx];
cb33740c 13993 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13994 if (isec != NULL
13995 && (isec->kept_section != NULL
13996 || discarded_section (isec)))
cb33740c 13997 return TRUE;
c152c796
AM
13998 }
13999 return FALSE;
14000 }
14001 return FALSE;
14002}
14003
14004/* Discard unneeded references to discarded sections.
75938853
AM
14005 Returns -1 on error, 1 if any section's size was changed, 0 if
14006 nothing changed. This function assumes that the relocations are in
14007 sorted order, which is true for all known assemblers. */
c152c796 14008
75938853 14009int
c152c796
AM
14010bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14011{
14012 struct elf_reloc_cookie cookie;
18cd5bce 14013 asection *o;
c152c796 14014 bfd *abfd;
75938853 14015 int changed = 0;
c152c796
AM
14016
14017 if (info->traditional_format
14018 || !is_elf_hash_table (info->hash))
75938853 14019 return 0;
c152c796 14020
18cd5bce
AM
14021 o = bfd_get_section_by_name (output_bfd, ".stab");
14022 if (o != NULL)
c152c796 14023 {
18cd5bce 14024 asection *i;
c152c796 14025
18cd5bce 14026 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14027 {
18cd5bce
AM
14028 if (i->size == 0
14029 || i->reloc_count == 0
14030 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14031 continue;
c152c796 14032
18cd5bce
AM
14033 abfd = i->owner;
14034 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14035 continue;
c152c796 14036
18cd5bce 14037 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14038 return -1;
c152c796 14039
18cd5bce
AM
14040 if (_bfd_discard_section_stabs (abfd, i,
14041 elf_section_data (i)->sec_info,
5241d853
RS
14042 bfd_elf_reloc_symbol_deleted_p,
14043 &cookie))
75938853 14044 changed = 1;
18cd5bce
AM
14045
14046 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14047 }
18cd5bce
AM
14048 }
14049
2f0c68f2
CM
14050 o = NULL;
14051 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14052 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14053 if (o != NULL)
14054 {
14055 asection *i;
d7153c4a 14056 int eh_changed = 0;
79a94a2a 14057 unsigned int eh_alignment;
c152c796 14058
18cd5bce 14059 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14060 {
18cd5bce
AM
14061 if (i->size == 0)
14062 continue;
14063
14064 abfd = i->owner;
14065 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14066 continue;
14067
14068 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14069 return -1;
18cd5bce
AM
14070
14071 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14072 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14073 bfd_elf_reloc_symbol_deleted_p,
14074 &cookie))
d7153c4a
AM
14075 {
14076 eh_changed = 1;
14077 if (i->size != i->rawsize)
14078 changed = 1;
14079 }
18cd5bce
AM
14080
14081 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14082 }
9866ffe2 14083
79a94a2a 14084 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14085 /* Skip over zero terminator, and prevent empty sections from
14086 adding alignment padding at the end. */
14087 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14088 if (i->size == 0)
14089 i->flags |= SEC_EXCLUDE;
14090 else if (i->size > 4)
14091 break;
14092 /* The last non-empty eh_frame section doesn't need padding. */
14093 if (i != NULL)
14094 i = i->map_tail.s;
14095 /* Any prior sections must pad the last FDE out to the output
14096 section alignment. Otherwise we might have zero padding
14097 between sections, which would be seen as a terminator. */
14098 for (; i != NULL; i = i->map_tail.s)
14099 if (i->size == 4)
14100 /* All but the last zero terminator should have been removed. */
14101 BFD_FAIL ();
14102 else
14103 {
14104 bfd_size_type size
14105 = (i->size + eh_alignment - 1) & -eh_alignment;
14106 if (i->size != size)
af471f82 14107 {
9866ffe2
AM
14108 i->size = size;
14109 changed = 1;
14110 eh_changed = 1;
af471f82 14111 }
9866ffe2 14112 }
d7153c4a
AM
14113 if (eh_changed)
14114 elf_link_hash_traverse (elf_hash_table (info),
14115 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14116 }
c152c796 14117
18cd5bce
AM
14118 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14119 {
14120 const struct elf_backend_data *bed;
57963c05 14121 asection *s;
c152c796 14122
18cd5bce
AM
14123 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14124 continue;
57963c05
AM
14125 s = abfd->sections;
14126 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14127 continue;
18cd5bce
AM
14128
14129 bed = get_elf_backend_data (abfd);
14130
14131 if (bed->elf_backend_discard_info != NULL)
14132 {
14133 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14134 return -1;
18cd5bce
AM
14135
14136 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14137 changed = 1;
18cd5bce
AM
14138
14139 fini_reloc_cookie (&cookie, abfd);
14140 }
c152c796
AM
14141 }
14142
2f0c68f2
CM
14143 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14144 _bfd_elf_end_eh_frame_parsing (info);
14145
14146 if (info->eh_frame_hdr_type
0e1862bb 14147 && !bfd_link_relocatable (info)
c152c796 14148 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14149 changed = 1;
c152c796 14150
75938853 14151 return changed;
c152c796 14152}
082b7297 14153
43e1669b 14154bfd_boolean
0c511000 14155_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14156 asection *sec,
c0f00686 14157 struct bfd_link_info *info)
082b7297
L
14158{
14159 flagword flags;
c77ec726 14160 const char *name, *key;
082b7297
L
14161 struct bfd_section_already_linked *l;
14162 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14163
c77ec726
AM
14164 if (sec->output_section == bfd_abs_section_ptr)
14165 return FALSE;
0c511000 14166
c77ec726 14167 flags = sec->flags;
0c511000 14168
c77ec726
AM
14169 /* Return if it isn't a linkonce section. A comdat group section
14170 also has SEC_LINK_ONCE set. */
14171 if ((flags & SEC_LINK_ONCE) == 0)
14172 return FALSE;
0c511000 14173
c77ec726
AM
14174 /* Don't put group member sections on our list of already linked
14175 sections. They are handled as a group via their group section. */
14176 if (elf_sec_group (sec) != NULL)
14177 return FALSE;
0c511000 14178
c77ec726
AM
14179 /* For a SHT_GROUP section, use the group signature as the key. */
14180 name = sec->name;
14181 if ((flags & SEC_GROUP) != 0
14182 && elf_next_in_group (sec) != NULL
14183 && elf_group_name (elf_next_in_group (sec)) != NULL)
14184 key = elf_group_name (elf_next_in_group (sec));
14185 else
14186 {
14187 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14188 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14189 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14190 key++;
0c511000 14191 else
c77ec726
AM
14192 /* Must be a user linkonce section that doesn't follow gcc's
14193 naming convention. In this case we won't be matching
14194 single member groups. */
14195 key = name;
0c511000 14196 }
6d2cd210 14197
c77ec726 14198 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14199
14200 for (l = already_linked_list->entry; l != NULL; l = l->next)
14201 {
c2370991 14202 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14203 sections with a signature of <key> (<key> is some string),
14204 and linkonce sections named .gnu.linkonce.<type>.<key>.
14205 Match like sections. LTO plugin sections are an exception.
14206 They are always named .gnu.linkonce.t.<key> and match either
14207 type of section. */
14208 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14209 && ((flags & SEC_GROUP) != 0
14210 || strcmp (name, l->sec->name) == 0))
14211 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14212 {
14213 /* The section has already been linked. See if we should
6d2cd210 14214 issue a warning. */
c77ec726
AM
14215 if (!_bfd_handle_already_linked (sec, l, info))
14216 return FALSE;
082b7297 14217
c77ec726 14218 if (flags & SEC_GROUP)
3d7f7666 14219 {
c77ec726
AM
14220 asection *first = elf_next_in_group (sec);
14221 asection *s = first;
3d7f7666 14222
c77ec726 14223 while (s != NULL)
3d7f7666 14224 {
c77ec726
AM
14225 s->output_section = bfd_abs_section_ptr;
14226 /* Record which group discards it. */
14227 s->kept_section = l->sec;
14228 s = elf_next_in_group (s);
14229 /* These lists are circular. */
14230 if (s == first)
14231 break;
3d7f7666
L
14232 }
14233 }
082b7297 14234
43e1669b 14235 return TRUE;
082b7297
L
14236 }
14237 }
14238
c77ec726
AM
14239 /* A single member comdat group section may be discarded by a
14240 linkonce section and vice versa. */
14241 if ((flags & SEC_GROUP) != 0)
3d7f7666 14242 {
c77ec726 14243 asection *first = elf_next_in_group (sec);
c2370991 14244
c77ec726
AM
14245 if (first != NULL && elf_next_in_group (first) == first)
14246 /* Check this single member group against linkonce sections. */
14247 for (l = already_linked_list->entry; l != NULL; l = l->next)
14248 if ((l->sec->flags & SEC_GROUP) == 0
14249 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14250 {
14251 first->output_section = bfd_abs_section_ptr;
14252 first->kept_section = l->sec;
14253 sec->output_section = bfd_abs_section_ptr;
14254 break;
14255 }
14256 }
14257 else
14258 /* Check this linkonce section against single member groups. */
14259 for (l = already_linked_list->entry; l != NULL; l = l->next)
14260 if (l->sec->flags & SEC_GROUP)
6d2cd210 14261 {
c77ec726 14262 asection *first = elf_next_in_group (l->sec);
6d2cd210 14263
c77ec726
AM
14264 if (first != NULL
14265 && elf_next_in_group (first) == first
14266 && bfd_elf_match_symbols_in_sections (first, sec, info))
14267 {
14268 sec->output_section = bfd_abs_section_ptr;
14269 sec->kept_section = first;
14270 break;
14271 }
6d2cd210 14272 }
0c511000 14273
c77ec726
AM
14274 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14275 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14276 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14277 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14278 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14279 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14280 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14281 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14282 The reverse order cannot happen as there is never a bfd with only the
14283 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14284 matter as here were are looking only for cross-bfd sections. */
14285
14286 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14287 for (l = already_linked_list->entry; l != NULL; l = l->next)
14288 if ((l->sec->flags & SEC_GROUP) == 0
14289 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14290 {
14291 if (abfd != l->sec->owner)
14292 sec->output_section = bfd_abs_section_ptr;
14293 break;
14294 }
80c29487 14295
082b7297 14296 /* This is the first section with this name. Record it. */
c77ec726 14297 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14298 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14299 return sec->output_section == bfd_abs_section_ptr;
082b7297 14300}
81e1b023 14301
a4d8e49b
L
14302bfd_boolean
14303_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14304{
14305 return sym->st_shndx == SHN_COMMON;
14306}
14307
14308unsigned int
14309_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14310{
14311 return SHN_COMMON;
14312}
14313
14314asection *
14315_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14316{
14317 return bfd_com_section_ptr;
14318}
10455f89
HPN
14319
14320bfd_vma
14321_bfd_elf_default_got_elt_size (bfd *abfd,
14322 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14323 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14324 bfd *ibfd ATTRIBUTE_UNUSED,
14325 unsigned long symndx ATTRIBUTE_UNUSED)
14326{
14327 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14328 return bed->s->arch_size / 8;
14329}
83bac4b0
NC
14330
14331/* Routines to support the creation of dynamic relocs. */
14332
83bac4b0
NC
14333/* Returns the name of the dynamic reloc section associated with SEC. */
14334
14335static const char *
14336get_dynamic_reloc_section_name (bfd * abfd,
14337 asection * sec,
14338 bfd_boolean is_rela)
14339{
ddcf1fcf
BS
14340 char *name;
14341 const char *old_name = bfd_get_section_name (NULL, sec);
14342 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14343
ddcf1fcf 14344 if (old_name == NULL)
83bac4b0
NC
14345 return NULL;
14346
ddcf1fcf 14347 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14348 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14349
14350 return name;
14351}
14352
14353/* Returns the dynamic reloc section associated with SEC.
14354 If necessary compute the name of the dynamic reloc section based
14355 on SEC's name (looked up in ABFD's string table) and the setting
14356 of IS_RELA. */
14357
14358asection *
14359_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14360 asection * sec,
14361 bfd_boolean is_rela)
14362{
14363 asection * reloc_sec = elf_section_data (sec)->sreloc;
14364
14365 if (reloc_sec == NULL)
14366 {
14367 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14368
14369 if (name != NULL)
14370 {
3d4d4302 14371 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14372
14373 if (reloc_sec != NULL)
14374 elf_section_data (sec)->sreloc = reloc_sec;
14375 }
14376 }
14377
14378 return reloc_sec;
14379}
14380
14381/* Returns the dynamic reloc section associated with SEC. If the
14382 section does not exist it is created and attached to the DYNOBJ
14383 bfd and stored in the SRELOC field of SEC's elf_section_data
14384 structure.
f8076f98 14385
83bac4b0
NC
14386 ALIGNMENT is the alignment for the newly created section and
14387 IS_RELA defines whether the name should be .rela.<SEC's name>
14388 or .rel.<SEC's name>. The section name is looked up in the
14389 string table associated with ABFD. */
14390
14391asection *
ca4be51c
AM
14392_bfd_elf_make_dynamic_reloc_section (asection *sec,
14393 bfd *dynobj,
14394 unsigned int alignment,
14395 bfd *abfd,
14396 bfd_boolean is_rela)
83bac4b0
NC
14397{
14398 asection * reloc_sec = elf_section_data (sec)->sreloc;
14399
14400 if (reloc_sec == NULL)
14401 {
14402 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14403
14404 if (name == NULL)
14405 return NULL;
14406
3d4d4302 14407 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14408
14409 if (reloc_sec == NULL)
14410 {
3d4d4302
AM
14411 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14412 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14413 if ((sec->flags & SEC_ALLOC) != 0)
14414 flags |= SEC_ALLOC | SEC_LOAD;
14415
3d4d4302 14416 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14417 if (reloc_sec != NULL)
14418 {
8877b5e5
AM
14419 /* _bfd_elf_get_sec_type_attr chooses a section type by
14420 name. Override as it may be wrong, eg. for a user
14421 section named "auto" we'll get ".relauto" which is
14422 seen to be a .rela section. */
14423 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14424 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14425 reloc_sec = NULL;
14426 }
14427 }
14428
14429 elf_section_data (sec)->sreloc = reloc_sec;
14430 }
14431
14432 return reloc_sec;
14433}
1338dd10 14434
bffebb6b
AM
14435/* Copy the ELF symbol type and other attributes for a linker script
14436 assignment from HSRC to HDEST. Generally this should be treated as
14437 if we found a strong non-dynamic definition for HDEST (except that
14438 ld ignores multiple definition errors). */
1338dd10 14439void
bffebb6b
AM
14440_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14441 struct bfd_link_hash_entry *hdest,
14442 struct bfd_link_hash_entry *hsrc)
1338dd10 14443{
bffebb6b
AM
14444 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14445 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14446 Elf_Internal_Sym isym;
1338dd10
PB
14447
14448 ehdest->type = ehsrc->type;
35fc36a8 14449 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14450
14451 isym.st_other = ehsrc->other;
b8417128 14452 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14453}
351f65ca
L
14454
14455/* Append a RELA relocation REL to section S in BFD. */
14456
14457void
14458elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14459{
14460 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14461 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14462 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14463 bed->s->swap_reloca_out (abfd, rel, loc);
14464}
14465
14466/* Append a REL relocation REL to section S in BFD. */
14467
14468void
14469elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14470{
14471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14472 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14473 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14474 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14475}
7dba9362
AM
14476
14477/* Define __start, __stop, .startof. or .sizeof. symbol. */
14478
14479struct bfd_link_hash_entry *
14480bfd_elf_define_start_stop (struct bfd_link_info *info,
14481 const char *symbol, asection *sec)
14482{
487b6440 14483 struct elf_link_hash_entry *h;
7dba9362 14484
487b6440
AM
14485 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14486 FALSE, FALSE, TRUE);
14487 if (h != NULL
14488 && (h->root.type == bfd_link_hash_undefined
14489 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14490 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14491 {
bf3077a6 14492 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14493 h->root.type = bfd_link_hash_defined;
14494 h->root.u.def.section = sec;
14495 h->root.u.def.value = 0;
14496 h->def_regular = 1;
14497 h->def_dynamic = 0;
14498 h->start_stop = 1;
14499 h->u2.start_stop_section = sec;
14500 if (symbol[0] == '.')
14501 {
14502 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14503 const struct elf_backend_data *bed;
14504 bed = get_elf_backend_data (info->output_bfd);
14505 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14506 }
36b8fda5
AM
14507 else
14508 {
14509 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14510 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14511 if (was_dynamic)
36b8fda5
AM
14512 bfd_elf_link_record_dynamic_symbol (info, h);
14513 }
487b6440 14514 return &h->root;
7dba9362 14515 }
487b6440 14516 return NULL;
7dba9362 14517}
This page took 2.467391 seconds and 4 git commands to generate.