elf: Don't merge .note.gnu.property section in IR
[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
4de5434b 226 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
227 && !((s = ibfd->sections) != NULL
228 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
229 {
230 abfd = ibfd;
231 break;
232 }
233 }
234 hash_table->dynobj = abfd;
235 }
7e9f0867
AM
236
237 if (hash_table->dynstr == NULL)
238 {
239 hash_table->dynstr = _bfd_elf_strtab_init ();
240 if (hash_table->dynstr == NULL)
241 return FALSE;
242 }
243 return TRUE;
244}
245
45d6a902
AM
246/* Create some sections which will be filled in with dynamic linking
247 information. ABFD is an input file which requires dynamic sections
248 to be created. The dynamic sections take up virtual memory space
249 when the final executable is run, so we need to create them before
250 addresses are assigned to the output sections. We work out the
251 actual contents and size of these sections later. */
252b5132 252
b34976b6 253bfd_boolean
268b6b39 254_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 255{
45d6a902 256 flagword flags;
91d6fa6a 257 asection *s;
9c5bfbb7 258 const struct elf_backend_data *bed;
9637f6ef 259 struct elf_link_hash_entry *h;
252b5132 260
0eddce27 261 if (! is_elf_hash_table (info->hash))
45d6a902
AM
262 return FALSE;
263
264 if (elf_hash_table (info)->dynamic_sections_created)
265 return TRUE;
266
7e9f0867
AM
267 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
268 return FALSE;
45d6a902 269
7e9f0867 270 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
271 bed = get_elf_backend_data (abfd);
272
273 flags = bed->dynamic_sec_flags;
45d6a902
AM
274
275 /* A dynamically linked executable has a .interp section, but a
276 shared library does not. */
9b8b325a 277 if (bfd_link_executable (info) && !info->nointerp)
252b5132 278 {
14b2f831
AM
279 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
280 flags | SEC_READONLY);
3496cb2a 281 if (s == NULL)
45d6a902
AM
282 return FALSE;
283 }
bb0deeff 284
45d6a902
AM
285 /* Create sections to hold version informations. These are removed
286 if they are not needed. */
14b2f831
AM
287 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
288 flags | SEC_READONLY);
45d6a902 289 if (s == NULL
45d6a902
AM
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
291 return FALSE;
292
14b2f831
AM
293 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
294 flags | SEC_READONLY);
45d6a902 295 if (s == NULL
45d6a902
AM
296 || ! bfd_set_section_alignment (abfd, s, 1))
297 return FALSE;
298
14b2f831
AM
299 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
300 flags | SEC_READONLY);
45d6a902 301 if (s == NULL
45d6a902
AM
302 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
303 return FALSE;
304
14b2f831
AM
305 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
306 flags | SEC_READONLY);
45d6a902 307 if (s == NULL
45d6a902
AM
308 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 return FALSE;
cae1fbbb 310 elf_hash_table (info)->dynsym = s;
45d6a902 311
14b2f831
AM
312 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
313 flags | SEC_READONLY);
3496cb2a 314 if (s == NULL)
45d6a902
AM
315 return FALSE;
316
14b2f831 317 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 318 if (s == NULL
45d6a902
AM
319 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
320 return FALSE;
321
322 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
323 .dynamic section. We could set _DYNAMIC in a linker script, but we
324 only want to define it if we are, in fact, creating a .dynamic
325 section. We don't want to define it if there is no .dynamic
326 section, since on some ELF platforms the start up code examines it
327 to decide how to initialize the process. */
9637f6ef
L
328 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
329 elf_hash_table (info)->hdynamic = h;
330 if (h == NULL)
45d6a902
AM
331 return FALSE;
332
fdc90cb4
JJ
333 if (info->emit_hash)
334 {
14b2f831
AM
335 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
336 flags | SEC_READONLY);
fdc90cb4
JJ
337 if (s == NULL
338 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 return FALSE;
340 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
341 }
342
343 if (info->emit_gnu_hash)
344 {
14b2f831
AM
345 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
346 flags | SEC_READONLY);
fdc90cb4
JJ
347 if (s == NULL
348 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 return FALSE;
350 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
351 4 32-bit words followed by variable count of 64-bit words, then
352 variable count of 32-bit words. */
353 if (bed->s->arch_size == 64)
354 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 else
356 elf_section_data (s)->this_hdr.sh_entsize = 4;
357 }
45d6a902
AM
358
359 /* Let the backend create the rest of the sections. This lets the
360 backend set the right flags. The backend will normally create
361 the .got and .plt sections. */
894891db
NC
362 if (bed->elf_backend_create_dynamic_sections == NULL
363 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
364 return FALSE;
365
366 elf_hash_table (info)->dynamic_sections_created = TRUE;
367
368 return TRUE;
369}
370
371/* Create dynamic sections when linking against a dynamic object. */
372
373bfd_boolean
268b6b39 374_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
375{
376 flagword flags, pltflags;
7325306f 377 struct elf_link_hash_entry *h;
45d6a902 378 asection *s;
9c5bfbb7 379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 380 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 381
252b5132
RH
382 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
383 .rel[a].bss sections. */
e5a52504 384 flags = bed->dynamic_sec_flags;
252b5132
RH
385
386 pltflags = flags;
252b5132 387 if (bed->plt_not_loaded)
6df4d94c
MM
388 /* We do not clear SEC_ALLOC here because we still want the OS to
389 allocate space for the section; it's just that there's nothing
390 to read in from the object file. */
5d1634d7 391 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
392 else
393 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
394 if (bed->plt_readonly)
395 pltflags |= SEC_READONLY;
396
14b2f831 397 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 398 if (s == NULL
252b5132 399 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 400 return FALSE;
6de2ae4a 401 htab->splt = s;
252b5132 402
d98685ac
AM
403 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 .plt section. */
7325306f
RS
405 if (bed->want_plt_sym)
406 {
407 h = _bfd_elf_define_linkage_sym (abfd, info, s,
408 "_PROCEDURE_LINKAGE_TABLE_");
409 elf_hash_table (info)->hplt = h;
410 if (h == NULL)
411 return FALSE;
412 }
252b5132 413
14b2f831
AM
414 s = bfd_make_section_anyway_with_flags (abfd,
415 (bed->rela_plts_and_copies_p
416 ? ".rela.plt" : ".rel.plt"),
417 flags | SEC_READONLY);
252b5132 418 if (s == NULL
45d6a902 419 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 420 return FALSE;
6de2ae4a 421 htab->srelplt = s;
252b5132
RH
422
423 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 424 return FALSE;
252b5132 425
3018b441
RH
426 if (bed->want_dynbss)
427 {
428 /* The .dynbss section is a place to put symbols which are defined
429 by dynamic objects, are referenced by regular objects, and are
430 not functions. We must allocate space for them in the process
431 image and use a R_*_COPY reloc to tell the dynamic linker to
432 initialize them at run time. The linker script puts the .dynbss
433 section into the .bss section of the final image. */
14b2f831 434 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 435 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 436 if (s == NULL)
b34976b6 437 return FALSE;
9d19e4fd 438 htab->sdynbss = s;
252b5132 439
5474d94f
AM
440 if (bed->want_dynrelro)
441 {
442 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
443 sections. This section doesn't really need to have contents,
444 but make it like other .data.rel.ro sections. */
5474d94f 445 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 446 flags);
5474d94f
AM
447 if (s == NULL)
448 return FALSE;
449 htab->sdynrelro = s;
450 }
451
3018b441 452 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
453 normally needed. We need to create it here, though, so that the
454 linker will map it to an output section. We can't just create it
455 only if we need it, because we will not know whether we need it
456 until we have seen all the input files, and the first time the
457 main linker code calls BFD after examining all the input files
458 (size_dynamic_sections) the input sections have already been
459 mapped to the output sections. If the section turns out not to
460 be needed, we can discard it later. We will never need this
461 section when generating a shared object, since they do not use
462 copy relocs. */
9d19e4fd 463 if (bfd_link_executable (info))
3018b441 464 {
14b2f831
AM
465 s = bfd_make_section_anyway_with_flags (abfd,
466 (bed->rela_plts_and_copies_p
467 ? ".rela.bss" : ".rel.bss"),
468 flags | SEC_READONLY);
3018b441 469 if (s == NULL
45d6a902 470 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 471 return FALSE;
9d19e4fd 472 htab->srelbss = s;
5474d94f
AM
473
474 if (bed->want_dynrelro)
475 {
476 s = (bfd_make_section_anyway_with_flags
477 (abfd, (bed->rela_plts_and_copies_p
478 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
479 flags | SEC_READONLY));
480 if (s == NULL
481 || ! bfd_set_section_alignment (abfd, s,
482 bed->s->log_file_align))
483 return FALSE;
484 htab->sreldynrelro = s;
485 }
3018b441 486 }
252b5132
RH
487 }
488
b34976b6 489 return TRUE;
252b5132
RH
490}
491\f
252b5132
RH
492/* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
498 one. */
499
b34976b6 500bfd_boolean
c152c796
AM
501bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
502 struct elf_link_hash_entry *h)
252b5132
RH
503{
504 if (h->dynindx == -1)
505 {
2b0f7ef9 506 struct elf_strtab_hash *dynstr;
68b6ddd0 507 char *p;
252b5132 508 const char *name;
ef53be89 509 size_t indx;
252b5132 510
7a13edea
NC
511 /* XXX: The ABI draft says the linker must turn hidden and
512 internal symbols into STB_LOCAL symbols when producing the
513 DSO. However, if ld.so honors st_other in the dynamic table,
514 this would not be necessary. */
515 switch (ELF_ST_VISIBILITY (h->other))
516 {
517 case STV_INTERNAL:
518 case STV_HIDDEN:
9d6eee78
L
519 if (h->root.type != bfd_link_hash_undefined
520 && h->root.type != bfd_link_hash_undefweak)
38048eb9 521 {
f5385ebf 522 h->forced_local = 1;
67687978
PB
523 if (!elf_hash_table (info)->is_relocatable_executable)
524 return TRUE;
7a13edea 525 }
0444bdd4 526
7a13edea
NC
527 default:
528 break;
529 }
530
252b5132
RH
531 h->dynindx = elf_hash_table (info)->dynsymcount;
532 ++elf_hash_table (info)->dynsymcount;
533
534 dynstr = elf_hash_table (info)->dynstr;
535 if (dynstr == NULL)
536 {
537 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 538 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 539 if (dynstr == NULL)
b34976b6 540 return FALSE;
252b5132
RH
541 }
542
543 /* We don't put any version information in the dynamic string
aad5d350 544 table. */
252b5132
RH
545 name = h->root.root.string;
546 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
547 if (p != NULL)
548 /* We know that the p points into writable memory. In fact,
549 there are only a few symbols that have read-only names, being
550 those like _GLOBAL_OFFSET_TABLE_ that are created specially
551 by the backends. Most symbols will have names pointing into
552 an ELF string table read from a file, or to objalloc memory. */
553 *p = 0;
554
555 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
556
557 if (p != NULL)
558 *p = ELF_VER_CHR;
252b5132 559
ef53be89 560 if (indx == (size_t) -1)
b34976b6 561 return FALSE;
252b5132
RH
562 h->dynstr_index = indx;
563 }
564
b34976b6 565 return TRUE;
252b5132 566}
45d6a902 567\f
55255dae
L
568/* Mark a symbol dynamic. */
569
28caa186 570static void
55255dae 571bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
572 struct elf_link_hash_entry *h,
573 Elf_Internal_Sym *sym)
55255dae 574{
40b36307 575 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 576
40b36307 577 /* It may be called more than once on the same H. */
0e1862bb 578 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
579 return;
580
40b36307
L
581 if ((info->dynamic_data
582 && (h->type == STT_OBJECT
b8871f35 583 || h->type == STT_COMMON
40b36307 584 || (sym != NULL
b8871f35
L
585 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
586 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 587 || (d != NULL
73ec947d 588 && h->non_elf
40b36307 589 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
590 {
591 h->dynamic = 1;
592 /* NB: If a symbol is made dynamic by --dynamic-list, it has
593 non-IR reference. */
594 h->root.non_ir_ref_dynamic = 1;
595 }
55255dae
L
596}
597
45d6a902
AM
598/* Record an assignment to a symbol made by a linker script. We need
599 this in case some dynamic object refers to this symbol. */
600
601bfd_boolean
fe21a8fc
L
602bfd_elf_record_link_assignment (bfd *output_bfd,
603 struct bfd_link_info *info,
268b6b39 604 const char *name,
fe21a8fc
L
605 bfd_boolean provide,
606 bfd_boolean hidden)
45d6a902 607{
00cbee0a 608 struct elf_link_hash_entry *h, *hv;
4ea42fb7 609 struct elf_link_hash_table *htab;
00cbee0a 610 const struct elf_backend_data *bed;
45d6a902 611
0eddce27 612 if (!is_elf_hash_table (info->hash))
45d6a902
AM
613 return TRUE;
614
4ea42fb7
AM
615 htab = elf_hash_table (info);
616 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 617 if (h == NULL)
4ea42fb7 618 return provide;
45d6a902 619
8e2a4f11
AM
620 if (h->root.type == bfd_link_hash_warning)
621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
622
0f550b3d
L
623 if (h->versioned == unknown)
624 {
625 /* Set versioned if symbol version is unknown. */
626 char *version = strrchr (name, ELF_VER_CHR);
627 if (version)
628 {
629 if (version > name && version[-1] != ELF_VER_CHR)
630 h->versioned = versioned_hidden;
631 else
632 h->versioned = versioned;
633 }
634 }
635
73ec947d
AM
636 /* Symbols defined in a linker script but not referenced anywhere
637 else will have non_elf set. */
638 if (h->non_elf)
639 {
640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
641 h->non_elf = 0;
642 }
643
00cbee0a 644 switch (h->root.type)
77cfaee6 645 {
00cbee0a
L
646 case bfd_link_hash_defined:
647 case bfd_link_hash_defweak:
648 case bfd_link_hash_common:
649 break;
650 case bfd_link_hash_undefweak:
651 case bfd_link_hash_undefined:
652 /* Since we're defining the symbol, don't let it seem to have not
653 been defined. record_dynamic_symbol and size_dynamic_sections
654 may depend on this. */
4ea42fb7 655 h->root.type = bfd_link_hash_new;
77cfaee6
AM
656 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
657 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
658 break;
659 case bfd_link_hash_new:
00cbee0a
L
660 break;
661 case bfd_link_hash_indirect:
662 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 663 the versioned symbol point to this one. */
00cbee0a
L
664 bed = get_elf_backend_data (output_bfd);
665 hv = h;
666 while (hv->root.type == bfd_link_hash_indirect
667 || hv->root.type == bfd_link_hash_warning)
668 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
669 /* We don't need to update h->root.u since linker will set them
670 later. */
671 h->root.type = bfd_link_hash_undefined;
672 hv->root.type = bfd_link_hash_indirect;
673 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
674 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
675 break;
8e2a4f11
AM
676 default:
677 BFD_FAIL ();
c2596ca5 678 return FALSE;
55255dae 679 }
45d6a902
AM
680
681 /* If this symbol is being provided by the linker script, and it is
682 currently defined by a dynamic object, but not by a regular
683 object, then mark it as undefined so that the generic linker will
684 force the correct value. */
685 if (provide
f5385ebf
AM
686 && h->def_dynamic
687 && !h->def_regular)
45d6a902
AM
688 h->root.type = bfd_link_hash_undefined;
689
48e30f52
L
690 /* If this symbol is currently defined by a dynamic object, but not
691 by a regular object, then clear out any version information because
692 the symbol will not be associated with the dynamic object any
693 more. */
694 if (h->def_dynamic && !h->def_regular)
b531344c
MR
695 h->verinfo.verdef = NULL;
696
697 /* Make sure this symbol is not garbage collected. */
698 h->mark = 1;
45d6a902 699
f5385ebf 700 h->def_regular = 1;
45d6a902 701
eb8476a6 702 if (hidden)
fe21a8fc 703 {
91d6fa6a 704 bed = get_elf_backend_data (output_bfd);
b8297068
AM
705 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
706 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
707 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
708 }
709
6fa3860b
PB
710 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
711 and executables. */
0e1862bb 712 if (!bfd_link_relocatable (info)
6fa3860b
PB
713 && h->dynindx != -1
714 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
715 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
716 h->forced_local = 1;
717
f5385ebf
AM
718 if ((h->def_dynamic
719 || h->ref_dynamic
6b3b0ab8
L
720 || bfd_link_dll (info)
721 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 722 && !h->forced_local
45d6a902
AM
723 && h->dynindx == -1)
724 {
c152c796 725 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
726 return FALSE;
727
728 /* If this is a weak defined symbol, and we know a corresponding
729 real symbol from the same dynamic object, make sure the real
730 symbol is also made into a dynamic symbol. */
60d67dc8 731 if (h->is_weakalias)
45d6a902 732 {
60d67dc8
AM
733 struct elf_link_hash_entry *def = weakdef (h);
734
735 if (def->dynindx == -1
736 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
737 return FALSE;
738 }
739 }
740
741 return TRUE;
742}
42751cf3 743
8c58d23b
AM
744/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
745 success, and 2 on a failure caused by attempting to record a symbol
746 in a discarded section, eg. a discarded link-once section symbol. */
747
748int
c152c796
AM
749bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
750 bfd *input_bfd,
751 long input_indx)
8c58d23b
AM
752{
753 bfd_size_type amt;
754 struct elf_link_local_dynamic_entry *entry;
755 struct elf_link_hash_table *eht;
756 struct elf_strtab_hash *dynstr;
ef53be89 757 size_t dynstr_index;
8c58d23b
AM
758 char *name;
759 Elf_External_Sym_Shndx eshndx;
760 char esym[sizeof (Elf64_External_Sym)];
761
0eddce27 762 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
763 return 0;
764
765 /* See if the entry exists already. */
766 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
767 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
768 return 1;
769
770 amt = sizeof (*entry);
a50b1753 771 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
772 if (entry == NULL)
773 return 0;
774
775 /* Go find the symbol, so that we can find it's name. */
776 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 777 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
778 {
779 bfd_release (input_bfd, entry);
780 return 0;
781 }
782
783 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 784 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
785 {
786 asection *s;
787
788 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
789 if (s == NULL || bfd_is_abs_section (s->output_section))
790 {
791 /* We can still bfd_release here as nothing has done another
792 bfd_alloc. We can't do this later in this function. */
793 bfd_release (input_bfd, entry);
794 return 2;
795 }
796 }
797
798 name = (bfd_elf_string_from_elf_section
799 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
800 entry->isym.st_name));
801
802 dynstr = elf_hash_table (info)->dynstr;
803 if (dynstr == NULL)
804 {
805 /* Create a strtab to hold the dynamic symbol names. */
806 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
807 if (dynstr == NULL)
808 return 0;
809 }
810
b34976b6 811 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 812 if (dynstr_index == (size_t) -1)
8c58d23b
AM
813 return 0;
814 entry->isym.st_name = dynstr_index;
815
816 eht = elf_hash_table (info);
817
818 entry->next = eht->dynlocal;
819 eht->dynlocal = entry;
820 entry->input_bfd = input_bfd;
821 entry->input_indx = input_indx;
822 eht->dynsymcount++;
823
824 /* Whatever binding the symbol had before, it's now local. */
825 entry->isym.st_info
826 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
827
828 /* The dynindx will be set at the end of size_dynamic_sections. */
829
830 return 1;
831}
832
30b30c21 833/* Return the dynindex of a local dynamic symbol. */
42751cf3 834
30b30c21 835long
268b6b39
AM
836_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
837 bfd *input_bfd,
838 long input_indx)
30b30c21
RH
839{
840 struct elf_link_local_dynamic_entry *e;
841
842 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
843 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
844 return e->dynindx;
845 return -1;
846}
847
848/* This function is used to renumber the dynamic symbols, if some of
849 them are removed because they are marked as local. This is called
850 via elf_link_hash_traverse. */
851
b34976b6 852static bfd_boolean
268b6b39
AM
853elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
854 void *data)
42751cf3 855{
a50b1753 856 size_t *count = (size_t *) data;
30b30c21 857
6fa3860b
PB
858 if (h->forced_local)
859 return TRUE;
860
861 if (h->dynindx != -1)
862 h->dynindx = ++(*count);
863
864 return TRUE;
865}
866
867
868/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
869 STB_LOCAL binding. */
870
871static bfd_boolean
872elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
874{
a50b1753 875 size_t *count = (size_t *) data;
6fa3860b 876
6fa3860b
PB
877 if (!h->forced_local)
878 return TRUE;
879
42751cf3 880 if (h->dynindx != -1)
30b30c21
RH
881 h->dynindx = ++(*count);
882
b34976b6 883 return TRUE;
42751cf3 884}
30b30c21 885
aee6f5b4
AO
886/* Return true if the dynamic symbol for a given section should be
887 omitted when creating a shared library. */
888bfd_boolean
d00dd7dc
AM
889_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
890 struct bfd_link_info *info,
891 asection *p)
aee6f5b4 892{
74541ad4 893 struct elf_link_hash_table *htab;
ca55926c 894 asection *ip;
74541ad4 895
aee6f5b4
AO
896 switch (elf_section_data (p)->this_hdr.sh_type)
897 {
898 case SHT_PROGBITS:
899 case SHT_NOBITS:
900 /* If sh_type is yet undecided, assume it could be
901 SHT_PROGBITS/SHT_NOBITS. */
902 case SHT_NULL:
74541ad4
AM
903 htab = elf_hash_table (info);
904 if (p == htab->tls_sec)
905 return FALSE;
906
907 if (htab->text_index_section != NULL)
908 return p != htab->text_index_section && p != htab->data_index_section;
909
ca55926c 910 return (htab->dynobj != NULL
3d4d4302 911 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 912 && ip->output_section == p);
aee6f5b4
AO
913
914 /* There shouldn't be section relative relocations
915 against any other section. */
916 default:
917 return TRUE;
918 }
919}
920
d00dd7dc
AM
921bfd_boolean
922_bfd_elf_omit_section_dynsym_all
923 (bfd *output_bfd ATTRIBUTE_UNUSED,
924 struct bfd_link_info *info ATTRIBUTE_UNUSED,
925 asection *p ATTRIBUTE_UNUSED)
926{
927 return TRUE;
928}
929
062e2358 930/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
931 symbol for each output section, which come first. Next come symbols
932 which have been forced to local binding. Then all of the back-end
933 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
934 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
935 (This prevents the early call before elf_backend_init_index_section
936 and strip_excluded_output_sections setting dynindx for sections
937 that are stripped.) */
30b30c21 938
554220db
AM
939static unsigned long
940_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
941 struct bfd_link_info *info,
942 unsigned long *section_sym_count)
30b30c21
RH
943{
944 unsigned long dynsymcount = 0;
63f452a8 945 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 946
0e1862bb
L
947 if (bfd_link_pic (info)
948 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 949 {
aee6f5b4 950 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
951 asection *p;
952 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 953 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 954 && (p->flags & SEC_ALLOC) != 0
7f923b7f 955 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 956 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
957 {
958 ++dynsymcount;
959 if (do_sec)
960 elf_section_data (p)->dynindx = dynsymcount;
961 }
962 else if (do_sec)
74541ad4 963 elf_section_data (p)->dynindx = 0;
30b30c21 964 }
63f452a8
AM
965 if (do_sec)
966 *section_sym_count = dynsymcount;
30b30c21 967
6fa3860b
PB
968 elf_link_hash_traverse (elf_hash_table (info),
969 elf_link_renumber_local_hash_table_dynsyms,
970 &dynsymcount);
971
30b30c21
RH
972 if (elf_hash_table (info)->dynlocal)
973 {
974 struct elf_link_local_dynamic_entry *p;
975 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
976 p->dynindx = ++dynsymcount;
977 }
90ac2420 978 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
979
980 elf_link_hash_traverse (elf_hash_table (info),
981 elf_link_renumber_hash_table_dynsyms,
982 &dynsymcount);
983
d5486c43
L
984 /* There is an unused NULL entry at the head of the table which we
985 must account for in our count even if the table is empty since it
986 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
987 .dynamic section. */
988 dynsymcount++;
30b30c21 989
ccabcbe5
AM
990 elf_hash_table (info)->dynsymcount = dynsymcount;
991 return dynsymcount;
30b30c21 992}
252b5132 993
54ac0771
L
994/* Merge st_other field. */
995
996static void
997elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 998 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 999 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1000{
1001 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1002
1003 /* If st_other has a processor-specific meaning, specific
cd3416da 1004 code might be needed here. */
54ac0771
L
1005 if (bed->elf_backend_merge_symbol_attribute)
1006 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1007 dynamic);
1008
cd3416da 1009 if (!dynamic)
54ac0771 1010 {
cd3416da
AM
1011 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1012 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1013
cd3416da
AM
1014 /* Keep the most constraining visibility. Leave the remainder
1015 of the st_other field to elf_backend_merge_symbol_attribute. */
1016 if (symvis - 1 < hvis - 1)
1017 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1018 }
b8417128
AM
1019 else if (definition
1020 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1021 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1022 h->protected_def = 1;
54ac0771
L
1023}
1024
4f3fedcf
AM
1025/* This function is called when we want to merge a new symbol with an
1026 existing symbol. It handles the various cases which arise when we
1027 find a definition in a dynamic object, or when there is already a
1028 definition in a dynamic object. The new symbol is described by
1029 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1030 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1031 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1032 of an old common symbol. We set OVERRIDE if the old symbol is
1033 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1034 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1035 to change. By OK to change, we mean that we shouldn't warn if the
1036 type or size does change. */
45d6a902 1037
8a56bd02 1038static bfd_boolean
268b6b39
AM
1039_bfd_elf_merge_symbol (bfd *abfd,
1040 struct bfd_link_info *info,
1041 const char *name,
1042 Elf_Internal_Sym *sym,
1043 asection **psec,
1044 bfd_vma *pvalue,
4f3fedcf
AM
1045 struct elf_link_hash_entry **sym_hash,
1046 bfd **poldbfd,
37a9e49a 1047 bfd_boolean *pold_weak,
af44c138 1048 unsigned int *pold_alignment,
268b6b39
AM
1049 bfd_boolean *skip,
1050 bfd_boolean *override,
1051 bfd_boolean *type_change_ok,
6e33951e
L
1052 bfd_boolean *size_change_ok,
1053 bfd_boolean *matched)
252b5132 1054{
7479dfd4 1055 asection *sec, *oldsec;
45d6a902 1056 struct elf_link_hash_entry *h;
90c984fc 1057 struct elf_link_hash_entry *hi;
45d6a902
AM
1058 struct elf_link_hash_entry *flip;
1059 int bind;
1060 bfd *oldbfd;
1061 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1062 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1063 const struct elf_backend_data *bed;
6e33951e 1064 char *new_version;
93f4de39 1065 bfd_boolean default_sym = *matched;
45d6a902
AM
1066
1067 *skip = FALSE;
1068 *override = FALSE;
1069
1070 sec = *psec;
1071 bind = ELF_ST_BIND (sym->st_info);
1072
1073 if (! bfd_is_und_section (sec))
1074 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1075 else
1076 h = ((struct elf_link_hash_entry *)
1077 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1078 if (h == NULL)
1079 return FALSE;
1080 *sym_hash = h;
252b5132 1081
88ba32a0
L
1082 bed = get_elf_backend_data (abfd);
1083
6e33951e 1084 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1085 if (h->versioned != unversioned)
6e33951e 1086 {
422f1182
L
1087 /* Symbol version is unknown or versioned. */
1088 new_version = strrchr (name, ELF_VER_CHR);
1089 if (new_version)
1090 {
1091 if (h->versioned == unknown)
1092 {
1093 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1094 h->versioned = versioned_hidden;
1095 else
1096 h->versioned = versioned;
1097 }
1098 new_version += 1;
1099 if (new_version[0] == '\0')
1100 new_version = NULL;
1101 }
1102 else
1103 h->versioned = unversioned;
6e33951e 1104 }
422f1182
L
1105 else
1106 new_version = NULL;
6e33951e 1107
90c984fc
L
1108 /* For merging, we only care about real symbols. But we need to make
1109 sure that indirect symbol dynamic flags are updated. */
1110 hi = h;
45d6a902
AM
1111 while (h->root.type == bfd_link_hash_indirect
1112 || h->root.type == bfd_link_hash_warning)
1113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1114
6e33951e
L
1115 if (!*matched)
1116 {
1117 if (hi == h || h->root.type == bfd_link_hash_new)
1118 *matched = TRUE;
1119 else
1120 {
ae7683d2 1121 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1122 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1123 true if the new symbol is only visible to the symbol with
6e33951e 1124 the same symbol version. */
422f1182
L
1125 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1126 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1127 if (!old_hidden && !new_hidden)
1128 /* The new symbol matches the existing symbol if both
1129 aren't hidden. */
1130 *matched = TRUE;
1131 else
1132 {
1133 /* OLD_VERSION is the symbol version of the existing
1134 symbol. */
422f1182
L
1135 char *old_version;
1136
1137 if (h->versioned >= versioned)
1138 old_version = strrchr (h->root.root.string,
1139 ELF_VER_CHR) + 1;
1140 else
1141 old_version = NULL;
6e33951e
L
1142
1143 /* The new symbol matches the existing symbol if they
1144 have the same symbol version. */
1145 *matched = (old_version == new_version
1146 || (old_version != NULL
1147 && new_version != NULL
1148 && strcmp (old_version, new_version) == 0));
1149 }
1150 }
1151 }
1152
934bce08
AM
1153 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1154 existing symbol. */
1155
1156 oldbfd = NULL;
1157 oldsec = NULL;
1158 switch (h->root.type)
1159 {
1160 default:
1161 break;
1162
1163 case bfd_link_hash_undefined:
1164 case bfd_link_hash_undefweak:
1165 oldbfd = h->root.u.undef.abfd;
1166 break;
1167
1168 case bfd_link_hash_defined:
1169 case bfd_link_hash_defweak:
1170 oldbfd = h->root.u.def.section->owner;
1171 oldsec = h->root.u.def.section;
1172 break;
1173
1174 case bfd_link_hash_common:
1175 oldbfd = h->root.u.c.p->section->owner;
1176 oldsec = h->root.u.c.p->section;
1177 if (pold_alignment)
1178 *pold_alignment = h->root.u.c.p->alignment_power;
1179 break;
1180 }
1181 if (poldbfd && *poldbfd == NULL)
1182 *poldbfd = oldbfd;
1183
1184 /* Differentiate strong and weak symbols. */
1185 newweak = bind == STB_WEAK;
1186 oldweak = (h->root.type == bfd_link_hash_defweak
1187 || h->root.type == bfd_link_hash_undefweak);
1188 if (pold_weak)
1189 *pold_weak = oldweak;
1190
40b36307 1191 /* We have to check it for every instance since the first few may be
ee659f1f 1192 references and not all compilers emit symbol type for undefined
40b36307
L
1193 symbols. */
1194 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1195
ee659f1f
AM
1196 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1197 respectively, is from a dynamic object. */
1198
1199 newdyn = (abfd->flags & DYNAMIC) != 0;
1200
1201 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1202 syms and defined syms in dynamic libraries respectively.
1203 ref_dynamic on the other hand can be set for a symbol defined in
1204 a dynamic library, and def_dynamic may not be set; When the
1205 definition in a dynamic lib is overridden by a definition in the
1206 executable use of the symbol in the dynamic lib becomes a
1207 reference to the executable symbol. */
1208 if (newdyn)
1209 {
1210 if (bfd_is_und_section (sec))
1211 {
1212 if (bind != STB_WEAK)
1213 {
1214 h->ref_dynamic_nonweak = 1;
1215 hi->ref_dynamic_nonweak = 1;
1216 }
1217 }
1218 else
1219 {
6e33951e
L
1220 /* Update the existing symbol only if they match. */
1221 if (*matched)
1222 h->dynamic_def = 1;
ee659f1f
AM
1223 hi->dynamic_def = 1;
1224 }
1225 }
1226
45d6a902
AM
1227 /* If we just created the symbol, mark it as being an ELF symbol.
1228 Other than that, there is nothing to do--there is no merge issue
1229 with a newly defined symbol--so we just return. */
1230
1231 if (h->root.type == bfd_link_hash_new)
252b5132 1232 {
f5385ebf 1233 h->non_elf = 0;
45d6a902
AM
1234 return TRUE;
1235 }
252b5132 1236
45d6a902
AM
1237 /* In cases involving weak versioned symbols, we may wind up trying
1238 to merge a symbol with itself. Catch that here, to avoid the
1239 confusion that results if we try to override a symbol with
1240 itself. The additional tests catch cases like
1241 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1242 dynamic object, which we do want to handle here. */
1243 if (abfd == oldbfd
895fa45f 1244 && (newweak || oldweak)
45d6a902 1245 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1246 || !h->def_regular))
45d6a902
AM
1247 return TRUE;
1248
707bba77 1249 olddyn = FALSE;
45d6a902
AM
1250 if (oldbfd != NULL)
1251 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1252 else if (oldsec != NULL)
45d6a902 1253 {
707bba77 1254 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1255 indices used by MIPS ELF. */
707bba77 1256 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1257 }
252b5132 1258
1a3b5c34
AM
1259 /* Handle a case where plugin_notice won't be called and thus won't
1260 set the non_ir_ref flags on the first pass over symbols. */
1261 if (oldbfd != NULL
1262 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1263 && newdyn != olddyn)
1264 {
1265 h->root.non_ir_ref_dynamic = TRUE;
1266 hi->root.non_ir_ref_dynamic = TRUE;
1267 }
1268
45d6a902
AM
1269 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1270 respectively, appear to be a definition rather than reference. */
1271
707bba77 1272 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1273
707bba77
AM
1274 olddef = (h->root.type != bfd_link_hash_undefined
1275 && h->root.type != bfd_link_hash_undefweak
202ac193 1276 && h->root.type != bfd_link_hash_common);
45d6a902 1277
0a36a439
L
1278 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1279 respectively, appear to be a function. */
1280
1281 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1282 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1283
1284 oldfunc = (h->type != STT_NOTYPE
1285 && bed->is_function_type (h->type));
1286
c5d37467 1287 if (!(newfunc && oldfunc)
5b677558
AM
1288 && ELF_ST_TYPE (sym->st_info) != h->type
1289 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1290 && h->type != STT_NOTYPE
c5d37467
AM
1291 && (newdef || bfd_is_com_section (sec))
1292 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1293 {
c5d37467
AM
1294 /* If creating a default indirect symbol ("foo" or "foo@") from
1295 a dynamic versioned definition ("foo@@") skip doing so if
1296 there is an existing regular definition with a different
1297 type. We don't want, for example, a "time" variable in the
1298 executable overriding a "time" function in a shared library. */
1299 if (newdyn
1300 && !olddyn)
1301 {
1302 *skip = TRUE;
1303 return TRUE;
1304 }
1305
1306 /* When adding a symbol from a regular object file after we have
1307 created indirect symbols, undo the indirection and any
1308 dynamic state. */
1309 if (hi != h
1310 && !newdyn
1311 && olddyn)
1312 {
1313 h = hi;
1314 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1315 h->forced_local = 0;
1316 h->ref_dynamic = 0;
1317 h->def_dynamic = 0;
1318 h->dynamic_def = 0;
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1320 {
1321 h->root.type = bfd_link_hash_undefined;
1322 h->root.u.undef.abfd = abfd;
1323 }
1324 else
1325 {
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1328 }
1329 return TRUE;
1330 }
580a2b6e
L
1331 }
1332
4c34aff8
AM
1333 /* Check TLS symbols. We don't check undefined symbols introduced
1334 by "ld -u" which have no type (and oldbfd NULL), and we don't
1335 check symbols from plugins because they also have no type. */
1336 if (oldbfd != NULL
1337 && (oldbfd->flags & BFD_PLUGIN) == 0
1338 && (abfd->flags & BFD_PLUGIN) == 0
1339 && ELF_ST_TYPE (sym->st_info) != h->type
1340 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1341 {
1342 bfd *ntbfd, *tbfd;
1343 bfd_boolean ntdef, tdef;
1344 asection *ntsec, *tsec;
1345
1346 if (h->type == STT_TLS)
1347 {
3b36f7e6 1348 ntbfd = abfd;
7479dfd4
L
1349 ntsec = sec;
1350 ntdef = newdef;
1351 tbfd = oldbfd;
1352 tsec = oldsec;
1353 tdef = olddef;
1354 }
1355 else
1356 {
1357 ntbfd = oldbfd;
1358 ntsec = oldsec;
1359 ntdef = olddef;
1360 tbfd = abfd;
1361 tsec = sec;
1362 tdef = newdef;
1363 }
1364
1365 if (tdef && ntdef)
4eca0228 1366 _bfd_error_handler
695344c0 1367 /* xgettext:c-format */
871b3ab2
AM
1368 (_("%s: TLS definition in %pB section %pA "
1369 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1370 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1371 else if (!tdef && !ntdef)
4eca0228 1372 _bfd_error_handler
695344c0 1373 /* xgettext:c-format */
871b3ab2
AM
1374 (_("%s: TLS reference in %pB "
1375 "mismatches non-TLS reference in %pB"),
c08bb8dd 1376 h->root.root.string, tbfd, ntbfd);
7479dfd4 1377 else if (tdef)
4eca0228 1378 _bfd_error_handler
695344c0 1379 /* xgettext:c-format */
871b3ab2
AM
1380 (_("%s: TLS definition in %pB section %pA "
1381 "mismatches non-TLS reference in %pB"),
c08bb8dd 1382 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1383 else
4eca0228 1384 _bfd_error_handler
695344c0 1385 /* xgettext:c-format */
871b3ab2
AM
1386 (_("%s: TLS reference in %pB "
1387 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1388 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1389
1390 bfd_set_error (bfd_error_bad_value);
1391 return FALSE;
1392 }
1393
45d6a902
AM
1394 /* If the old symbol has non-default visibility, we ignore the new
1395 definition from a dynamic object. */
1396 if (newdyn
9c7a29a3 1397 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1398 && !bfd_is_und_section (sec))
1399 {
1400 *skip = TRUE;
1401 /* Make sure this symbol is dynamic. */
f5385ebf 1402 h->ref_dynamic = 1;
90c984fc 1403 hi->ref_dynamic = 1;
45d6a902
AM
1404 /* A protected symbol has external availability. Make sure it is
1405 recorded as dynamic.
1406
1407 FIXME: Should we check type and size for protected symbol? */
1408 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1409 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1410 else
1411 return TRUE;
1412 }
1413 else if (!newdyn
9c7a29a3 1414 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1415 && h->def_dynamic)
45d6a902
AM
1416 {
1417 /* If the new symbol with non-default visibility comes from a
1418 relocatable file and the old definition comes from a dynamic
1419 object, we remove the old definition. */
6c9b78e6 1420 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1421 {
1422 /* Handle the case where the old dynamic definition is
1423 default versioned. We need to copy the symbol info from
1424 the symbol with default version to the normal one if it
1425 was referenced before. */
1426 if (h->ref_regular)
1427 {
6c9b78e6 1428 hi->root.type = h->root.type;
d2dee3b2 1429 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1430 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1431
6c9b78e6 1432 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1433 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1434 {
aed81c4e
MR
1435 /* If the new symbol is hidden or internal, completely undo
1436 any dynamic link state. */
1437 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1438 h->forced_local = 0;
1439 h->ref_dynamic = 0;
d2dee3b2
L
1440 }
1441 else
aed81c4e
MR
1442 h->ref_dynamic = 1;
1443
1444 h->def_dynamic = 0;
aed81c4e
MR
1445 /* FIXME: Should we check type and size for protected symbol? */
1446 h->size = 0;
1447 h->type = 0;
1448
6c9b78e6 1449 h = hi;
d2dee3b2
L
1450 }
1451 else
6c9b78e6 1452 h = hi;
d2dee3b2 1453 }
1de1a317 1454
f5eda473
AM
1455 /* If the old symbol was undefined before, then it will still be
1456 on the undefs list. If the new symbol is undefined or
1457 common, we can't make it bfd_link_hash_new here, because new
1458 undefined or common symbols will be added to the undefs list
1459 by _bfd_generic_link_add_one_symbol. Symbols may not be
1460 added twice to the undefs list. Also, if the new symbol is
1461 undefweak then we don't want to lose the strong undef. */
1462 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1463 {
1de1a317 1464 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1465 h->root.u.undef.abfd = abfd;
1466 }
1467 else
1468 {
1469 h->root.type = bfd_link_hash_new;
1470 h->root.u.undef.abfd = NULL;
1471 }
1472
f5eda473 1473 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1474 {
f5eda473
AM
1475 /* If the new symbol is hidden or internal, completely undo
1476 any dynamic link state. */
1477 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1478 h->forced_local = 0;
1479 h->ref_dynamic = 0;
45d6a902 1480 }
f5eda473
AM
1481 else
1482 h->ref_dynamic = 1;
1483 h->def_dynamic = 0;
45d6a902
AM
1484 /* FIXME: Should we check type and size for protected symbol? */
1485 h->size = 0;
1486 h->type = 0;
1487 return TRUE;
1488 }
14a793b2 1489
15b43f48
AM
1490 /* If a new weak symbol definition comes from a regular file and the
1491 old symbol comes from a dynamic library, we treat the new one as
1492 strong. Similarly, an old weak symbol definition from a regular
1493 file is treated as strong when the new symbol comes from a dynamic
1494 library. Further, an old weak symbol from a dynamic library is
1495 treated as strong if the new symbol is from a dynamic library.
1496 This reflects the way glibc's ld.so works.
1497
165f707a
AM
1498 Also allow a weak symbol to override a linker script symbol
1499 defined by an early pass over the script. This is done so the
1500 linker knows the symbol is defined in an object file, for the
1501 DEFINED script function.
1502
15b43f48
AM
1503 Do this before setting *type_change_ok or *size_change_ok so that
1504 we warn properly when dynamic library symbols are overridden. */
1505
165f707a 1506 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1507 newweak = FALSE;
15b43f48 1508 if (olddef && newdyn)
0f8a2703
AM
1509 oldweak = FALSE;
1510
d334575b 1511 /* Allow changes between different types of function symbol. */
0a36a439 1512 if (newfunc && oldfunc)
fcb93ecf
PB
1513 *type_change_ok = TRUE;
1514
79349b09
AM
1515 /* It's OK to change the type if either the existing symbol or the
1516 new symbol is weak. A type change is also OK if the old symbol
1517 is undefined and the new symbol is defined. */
252b5132 1518
79349b09
AM
1519 if (oldweak
1520 || newweak
1521 || (newdef
1522 && h->root.type == bfd_link_hash_undefined))
1523 *type_change_ok = TRUE;
1524
1525 /* It's OK to change the size if either the existing symbol or the
1526 new symbol is weak, or if the old symbol is undefined. */
1527
1528 if (*type_change_ok
1529 || h->root.type == bfd_link_hash_undefined)
1530 *size_change_ok = TRUE;
45d6a902 1531
45d6a902
AM
1532 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1533 symbol, respectively, appears to be a common symbol in a dynamic
1534 object. If a symbol appears in an uninitialized section, and is
1535 not weak, and is not a function, then it may be a common symbol
1536 which was resolved when the dynamic object was created. We want
1537 to treat such symbols specially, because they raise special
1538 considerations when setting the symbol size: if the symbol
1539 appears as a common symbol in a regular object, and the size in
1540 the regular object is larger, we must make sure that we use the
1541 larger size. This problematic case can always be avoided in C,
1542 but it must be handled correctly when using Fortran shared
1543 libraries.
1544
1545 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1546 likewise for OLDDYNCOMMON and OLDDEF.
1547
1548 Note that this test is just a heuristic, and that it is quite
1549 possible to have an uninitialized symbol in a shared object which
1550 is really a definition, rather than a common symbol. This could
1551 lead to some minor confusion when the symbol really is a common
1552 symbol in some regular object. However, I think it will be
1553 harmless. */
1554
1555 if (newdyn
1556 && newdef
79349b09 1557 && !newweak
45d6a902
AM
1558 && (sec->flags & SEC_ALLOC) != 0
1559 && (sec->flags & SEC_LOAD) == 0
1560 && sym->st_size > 0
0a36a439 1561 && !newfunc)
45d6a902
AM
1562 newdyncommon = TRUE;
1563 else
1564 newdyncommon = FALSE;
1565
1566 if (olddyn
1567 && olddef
1568 && h->root.type == bfd_link_hash_defined
f5385ebf 1569 && h->def_dynamic
45d6a902
AM
1570 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1571 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1572 && h->size > 0
0a36a439 1573 && !oldfunc)
45d6a902
AM
1574 olddyncommon = TRUE;
1575 else
1576 olddyncommon = FALSE;
1577
a4d8e49b
L
1578 /* We now know everything about the old and new symbols. We ask the
1579 backend to check if we can merge them. */
5d13b3b3
AM
1580 if (bed->merge_symbol != NULL)
1581 {
1582 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1583 return FALSE;
1584 sec = *psec;
1585 }
a4d8e49b 1586
a83ef4d1
L
1587 /* There are multiple definitions of a normal symbol. Skip the
1588 default symbol as well as definition from an IR object. */
93f4de39 1589 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1590 && !default_sym && h->def_regular
1591 && !(oldbfd != NULL
1592 && (oldbfd->flags & BFD_PLUGIN) != 0
1593 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1594 {
1595 /* Handle a multiple definition. */
1596 (*info->callbacks->multiple_definition) (info, &h->root,
1597 abfd, sec, *pvalue);
1598 *skip = TRUE;
1599 return TRUE;
1600 }
1601
45d6a902
AM
1602 /* If both the old and the new symbols look like common symbols in a
1603 dynamic object, set the size of the symbol to the larger of the
1604 two. */
1605
1606 if (olddyncommon
1607 && newdyncommon
1608 && sym->st_size != h->size)
1609 {
1610 /* Since we think we have two common symbols, issue a multiple
1611 common warning if desired. Note that we only warn if the
1612 size is different. If the size is the same, we simply let
1613 the old symbol override the new one as normally happens with
1614 symbols defined in dynamic objects. */
1615
1a72702b
AM
1616 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1617 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1618 if (sym->st_size > h->size)
1619 h->size = sym->st_size;
252b5132 1620
45d6a902 1621 *size_change_ok = TRUE;
252b5132
RH
1622 }
1623
45d6a902
AM
1624 /* If we are looking at a dynamic object, and we have found a
1625 definition, we need to see if the symbol was already defined by
1626 some other object. If so, we want to use the existing
1627 definition, and we do not want to report a multiple symbol
1628 definition error; we do this by clobbering *PSEC to be
1629 bfd_und_section_ptr.
1630
1631 We treat a common symbol as a definition if the symbol in the
1632 shared library is a function, since common symbols always
1633 represent variables; this can cause confusion in principle, but
1634 any such confusion would seem to indicate an erroneous program or
1635 shared library. We also permit a common symbol in a regular
8170f769 1636 object to override a weak symbol in a shared object. */
45d6a902
AM
1637
1638 if (newdyn
1639 && newdef
77cfaee6 1640 && (olddef
45d6a902 1641 || (h->root.type == bfd_link_hash_common
8170f769 1642 && (newweak || newfunc))))
45d6a902
AM
1643 {
1644 *override = TRUE;
1645 newdef = FALSE;
1646 newdyncommon = FALSE;
252b5132 1647
45d6a902
AM
1648 *psec = sec = bfd_und_section_ptr;
1649 *size_change_ok = TRUE;
252b5132 1650
45d6a902
AM
1651 /* If we get here when the old symbol is a common symbol, then
1652 we are explicitly letting it override a weak symbol or
1653 function in a dynamic object, and we don't want to warn about
1654 a type change. If the old symbol is a defined symbol, a type
1655 change warning may still be appropriate. */
252b5132 1656
45d6a902
AM
1657 if (h->root.type == bfd_link_hash_common)
1658 *type_change_ok = TRUE;
1659 }
1660
1661 /* Handle the special case of an old common symbol merging with a
1662 new symbol which looks like a common symbol in a shared object.
1663 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1664 common symbol, and let _bfd_generic_link_add_one_symbol do the
1665 right thing. */
45d6a902
AM
1666
1667 if (newdyncommon
1668 && h->root.type == bfd_link_hash_common)
1669 {
1670 *override = TRUE;
1671 newdef = FALSE;
1672 newdyncommon = FALSE;
1673 *pvalue = sym->st_size;
a4d8e49b 1674 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1675 *size_change_ok = TRUE;
1676 }
1677
c5e2cead 1678 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1679 if (newdef && olddef && newweak)
54ac0771 1680 {
35ed3f94 1681 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1682 if (!(oldbfd != NULL
1683 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1684 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1685 {
1686 newdef = FALSE;
1687 *skip = TRUE;
1688 }
54ac0771
L
1689
1690 /* Merge st_other. If the symbol already has a dynamic index,
1691 but visibility says it should not be visible, turn it into a
1692 local symbol. */
b8417128 1693 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1694 if (h->dynindx != -1)
1695 switch (ELF_ST_VISIBILITY (h->other))
1696 {
1697 case STV_INTERNAL:
1698 case STV_HIDDEN:
1699 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1700 break;
1701 }
1702 }
c5e2cead 1703
45d6a902
AM
1704 /* If the old symbol is from a dynamic object, and the new symbol is
1705 a definition which is not from a dynamic object, then the new
1706 symbol overrides the old symbol. Symbols from regular files
1707 always take precedence over symbols from dynamic objects, even if
1708 they are defined after the dynamic object in the link.
1709
1710 As above, we again permit a common symbol in a regular object to
1711 override a definition in a shared object if the shared object
0f8a2703 1712 symbol is a function or is weak. */
45d6a902
AM
1713
1714 flip = NULL;
77cfaee6 1715 if (!newdyn
45d6a902
AM
1716 && (newdef
1717 || (bfd_is_com_section (sec)
0a36a439 1718 && (oldweak || oldfunc)))
45d6a902
AM
1719 && olddyn
1720 && olddef
f5385ebf 1721 && h->def_dynamic)
45d6a902
AM
1722 {
1723 /* Change the hash table entry to undefined, and let
1724 _bfd_generic_link_add_one_symbol do the right thing with the
1725 new definition. */
1726
1727 h->root.type = bfd_link_hash_undefined;
1728 h->root.u.undef.abfd = h->root.u.def.section->owner;
1729 *size_change_ok = TRUE;
1730
1731 olddef = FALSE;
1732 olddyncommon = FALSE;
1733
1734 /* We again permit a type change when a common symbol may be
1735 overriding a function. */
1736
1737 if (bfd_is_com_section (sec))
0a36a439
L
1738 {
1739 if (oldfunc)
1740 {
1741 /* If a common symbol overrides a function, make sure
1742 that it isn't defined dynamically nor has type
1743 function. */
1744 h->def_dynamic = 0;
1745 h->type = STT_NOTYPE;
1746 }
1747 *type_change_ok = TRUE;
1748 }
45d6a902 1749
6c9b78e6
AM
1750 if (hi->root.type == bfd_link_hash_indirect)
1751 flip = hi;
45d6a902
AM
1752 else
1753 /* This union may have been set to be non-NULL when this symbol
1754 was seen in a dynamic object. We must force the union to be
1755 NULL, so that it is correct for a regular symbol. */
1756 h->verinfo.vertree = NULL;
1757 }
1758
1759 /* Handle the special case of a new common symbol merging with an
1760 old symbol that looks like it might be a common symbol defined in
1761 a shared object. Note that we have already handled the case in
1762 which a new common symbol should simply override the definition
1763 in the shared library. */
1764
1765 if (! newdyn
1766 && bfd_is_com_section (sec)
1767 && olddyncommon)
1768 {
1769 /* It would be best if we could set the hash table entry to a
1770 common symbol, but we don't know what to use for the section
1771 or the alignment. */
1a72702b
AM
1772 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1773 bfd_link_hash_common, sym->st_size);
45d6a902 1774
4cc11e76 1775 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1776 larger, pretend that the new symbol has its size. */
1777
1778 if (h->size > *pvalue)
1779 *pvalue = h->size;
1780
af44c138
L
1781 /* We need to remember the alignment required by the symbol
1782 in the dynamic object. */
1783 BFD_ASSERT (pold_alignment);
1784 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1785
1786 olddef = FALSE;
1787 olddyncommon = FALSE;
1788
1789 h->root.type = bfd_link_hash_undefined;
1790 h->root.u.undef.abfd = h->root.u.def.section->owner;
1791
1792 *size_change_ok = TRUE;
1793 *type_change_ok = TRUE;
1794
6c9b78e6
AM
1795 if (hi->root.type == bfd_link_hash_indirect)
1796 flip = hi;
45d6a902
AM
1797 else
1798 h->verinfo.vertree = NULL;
1799 }
1800
1801 if (flip != NULL)
1802 {
1803 /* Handle the case where we had a versioned symbol in a dynamic
1804 library and now find a definition in a normal object. In this
1805 case, we make the versioned symbol point to the normal one. */
45d6a902 1806 flip->root.type = h->root.type;
00cbee0a 1807 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1810 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1811 if (h->def_dynamic)
45d6a902 1812 {
f5385ebf
AM
1813 h->def_dynamic = 0;
1814 flip->ref_dynamic = 1;
45d6a902
AM
1815 }
1816 }
1817
45d6a902
AM
1818 return TRUE;
1819}
1820
1821/* This function is called to create an indirect symbol from the
1822 default for the symbol with the default version if needed. The
4f3fedcf 1823 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1824 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1825
28caa186 1826static bfd_boolean
268b6b39
AM
1827_bfd_elf_add_default_symbol (bfd *abfd,
1828 struct bfd_link_info *info,
1829 struct elf_link_hash_entry *h,
1830 const char *name,
1831 Elf_Internal_Sym *sym,
4f3fedcf
AM
1832 asection *sec,
1833 bfd_vma value,
1834 bfd **poldbfd,
e3c9d234 1835 bfd_boolean *dynsym)
45d6a902
AM
1836{
1837 bfd_boolean type_change_ok;
1838 bfd_boolean size_change_ok;
1839 bfd_boolean skip;
1840 char *shortname;
1841 struct elf_link_hash_entry *hi;
1842 struct bfd_link_hash_entry *bh;
9c5bfbb7 1843 const struct elf_backend_data *bed;
45d6a902
AM
1844 bfd_boolean collect;
1845 bfd_boolean dynamic;
e3c9d234 1846 bfd_boolean override;
45d6a902
AM
1847 char *p;
1848 size_t len, shortlen;
ffd65175 1849 asection *tmp_sec;
6e33951e 1850 bfd_boolean matched;
45d6a902 1851
422f1182
L
1852 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1853 return TRUE;
1854
45d6a902
AM
1855 /* If this symbol has a version, and it is the default version, we
1856 create an indirect symbol from the default name to the fully
1857 decorated name. This will cause external references which do not
1858 specify a version to be bound to this version of the symbol. */
1859 p = strchr (name, ELF_VER_CHR);
422f1182
L
1860 if (h->versioned == unknown)
1861 {
1862 if (p == NULL)
1863 {
1864 h->versioned = unversioned;
1865 return TRUE;
1866 }
1867 else
1868 {
1869 if (p[1] != ELF_VER_CHR)
1870 {
1871 h->versioned = versioned_hidden;
1872 return TRUE;
1873 }
1874 else
1875 h->versioned = versioned;
1876 }
1877 }
4373f8af
L
1878 else
1879 {
1880 /* PR ld/19073: We may see an unversioned definition after the
1881 default version. */
1882 if (p == NULL)
1883 return TRUE;
1884 }
45d6a902 1885
45d6a902
AM
1886 bed = get_elf_backend_data (abfd);
1887 collect = bed->collect;
1888 dynamic = (abfd->flags & DYNAMIC) != 0;
1889
1890 shortlen = p - name;
a50b1753 1891 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1892 if (shortname == NULL)
1893 return FALSE;
1894 memcpy (shortname, name, shortlen);
1895 shortname[shortlen] = '\0';
1896
1897 /* We are going to create a new symbol. Merge it with any existing
1898 symbol with this name. For the purposes of the merge, act as
1899 though we were defining the symbol we just defined, although we
1900 actually going to define an indirect symbol. */
1901 type_change_ok = FALSE;
1902 size_change_ok = FALSE;
6e33951e 1903 matched = TRUE;
ffd65175
AM
1904 tmp_sec = sec;
1905 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1906 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1907 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1908 return FALSE;
1909
1910 if (skip)
1911 goto nondefault;
1912
5b677558
AM
1913 if (hi->def_regular)
1914 {
1915 /* If the undecorated symbol will have a version added by a
1916 script different to H, then don't indirect to/from the
1917 undecorated symbol. This isn't ideal because we may not yet
1918 have seen symbol versions, if given by a script on the
1919 command line rather than via --version-script. */
1920 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1921 {
1922 bfd_boolean hide;
1923
1924 hi->verinfo.vertree
1925 = bfd_find_version_for_sym (info->version_info,
1926 hi->root.root.string, &hide);
1927 if (hi->verinfo.vertree != NULL && hide)
1928 {
1929 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1930 goto nondefault;
1931 }
1932 }
1933 if (hi->verinfo.vertree != NULL
1934 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1935 goto nondefault;
1936 }
1937
45d6a902
AM
1938 if (! override)
1939 {
c6e8a9a8 1940 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1941 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1942 {
1943 bh = &hi->root;
1944 if (! (_bfd_generic_link_add_one_symbol
1945 (info, abfd, shortname, BSF_INDIRECT,
1946 bfd_ind_section_ptr,
1947 0, name, FALSE, collect, &bh)))
1948 return FALSE;
1949 hi = (struct elf_link_hash_entry *) bh;
1950 }
45d6a902
AM
1951 }
1952 else
1953 {
1954 /* In this case the symbol named SHORTNAME is overriding the
1955 indirect symbol we want to add. We were planning on making
1956 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1957 is the name without a version. NAME is the fully versioned
1958 name, and it is the default version.
1959
1960 Overriding means that we already saw a definition for the
1961 symbol SHORTNAME in a regular object, and it is overriding
1962 the symbol defined in the dynamic object.
1963
1964 When this happens, we actually want to change NAME, the
1965 symbol we just added, to refer to SHORTNAME. This will cause
1966 references to NAME in the shared object to become references
1967 to SHORTNAME in the regular object. This is what we expect
1968 when we override a function in a shared object: that the
1969 references in the shared object will be mapped to the
1970 definition in the regular object. */
1971
1972 while (hi->root.type == bfd_link_hash_indirect
1973 || hi->root.type == bfd_link_hash_warning)
1974 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1975
1976 h->root.type = bfd_link_hash_indirect;
1977 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1978 if (h->def_dynamic)
45d6a902 1979 {
f5385ebf
AM
1980 h->def_dynamic = 0;
1981 hi->ref_dynamic = 1;
1982 if (hi->ref_regular
1983 || hi->def_regular)
45d6a902 1984 {
c152c796 1985 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1986 return FALSE;
1987 }
1988 }
1989
1990 /* Now set HI to H, so that the following code will set the
1991 other fields correctly. */
1992 hi = h;
1993 }
1994
fab4a87f
L
1995 /* Check if HI is a warning symbol. */
1996 if (hi->root.type == bfd_link_hash_warning)
1997 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1998
45d6a902
AM
1999 /* If there is a duplicate definition somewhere, then HI may not
2000 point to an indirect symbol. We will have reported an error to
2001 the user in that case. */
2002
2003 if (hi->root.type == bfd_link_hash_indirect)
2004 {
2005 struct elf_link_hash_entry *ht;
2006
45d6a902 2007 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2008 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2009
68c88cd4
AM
2010 /* A reference to the SHORTNAME symbol from a dynamic library
2011 will be satisfied by the versioned symbol at runtime. In
2012 effect, we have a reference to the versioned symbol. */
2013 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2014 hi->dynamic_def |= ht->dynamic_def;
2015
45d6a902
AM
2016 /* See if the new flags lead us to realize that the symbol must
2017 be dynamic. */
2018 if (! *dynsym)
2019 {
2020 if (! dynamic)
2021 {
0e1862bb 2022 if (! bfd_link_executable (info)
90c984fc 2023 || hi->def_dynamic
f5385ebf 2024 || hi->ref_dynamic)
45d6a902
AM
2025 *dynsym = TRUE;
2026 }
2027 else
2028 {
f5385ebf 2029 if (hi->ref_regular)
45d6a902
AM
2030 *dynsym = TRUE;
2031 }
2032 }
2033 }
2034
2035 /* We also need to define an indirection from the nondefault version
2036 of the symbol. */
2037
2038nondefault:
2039 len = strlen (name);
a50b1753 2040 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2041 if (shortname == NULL)
2042 return FALSE;
2043 memcpy (shortname, name, shortlen);
2044 memcpy (shortname + shortlen, p + 1, len - shortlen);
2045
2046 /* Once again, merge with any existing symbol. */
2047 type_change_ok = FALSE;
2048 size_change_ok = FALSE;
ffd65175
AM
2049 tmp_sec = sec;
2050 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2051 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2052 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2053 return FALSE;
2054
2055 if (skip)
2056 return TRUE;
2057
2058 if (override)
2059 {
2060 /* Here SHORTNAME is a versioned name, so we don't expect to see
2061 the type of override we do in the case above unless it is
4cc11e76 2062 overridden by a versioned definition. */
45d6a902
AM
2063 if (hi->root.type != bfd_link_hash_defined
2064 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2065 _bfd_error_handler
695344c0 2066 /* xgettext:c-format */
871b3ab2 2067 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2068 abfd, shortname);
45d6a902
AM
2069 }
2070 else
2071 {
2072 bh = &hi->root;
2073 if (! (_bfd_generic_link_add_one_symbol
2074 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2075 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2076 return FALSE;
2077 hi = (struct elf_link_hash_entry *) bh;
2078
2079 /* If there is a duplicate definition somewhere, then HI may not
2080 point to an indirect symbol. We will have reported an error
2081 to the user in that case. */
2082
2083 if (hi->root.type == bfd_link_hash_indirect)
2084 {
fcfa13d2 2085 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2086 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2087 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2088
2089 /* See if the new flags lead us to realize that the symbol
2090 must be dynamic. */
2091 if (! *dynsym)
2092 {
2093 if (! dynamic)
2094 {
0e1862bb 2095 if (! bfd_link_executable (info)
f5385ebf 2096 || hi->ref_dynamic)
45d6a902
AM
2097 *dynsym = TRUE;
2098 }
2099 else
2100 {
f5385ebf 2101 if (hi->ref_regular)
45d6a902
AM
2102 *dynsym = TRUE;
2103 }
2104 }
2105 }
2106 }
2107
2108 return TRUE;
2109}
2110\f
2111/* This routine is used to export all defined symbols into the dynamic
2112 symbol table. It is called via elf_link_hash_traverse. */
2113
28caa186 2114static bfd_boolean
268b6b39 2115_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2116{
a50b1753 2117 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2118
2119 /* Ignore indirect symbols. These are added by the versioning code. */
2120 if (h->root.type == bfd_link_hash_indirect)
2121 return TRUE;
2122
7686d77d
AM
2123 /* Ignore this if we won't export it. */
2124 if (!eif->info->export_dynamic && !h->dynamic)
2125 return TRUE;
45d6a902
AM
2126
2127 if (h->dynindx == -1
fd91d419
L
2128 && (h->def_regular || h->ref_regular)
2129 && ! bfd_hide_sym_by_version (eif->info->version_info,
2130 h->root.root.string))
45d6a902 2131 {
fd91d419 2132 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2133 {
fd91d419
L
2134 eif->failed = TRUE;
2135 return FALSE;
45d6a902
AM
2136 }
2137 }
2138
2139 return TRUE;
2140}
2141\f
2142/* Look through the symbols which are defined in other shared
2143 libraries and referenced here. Update the list of version
2144 dependencies. This will be put into the .gnu.version_r section.
2145 This function is called via elf_link_hash_traverse. */
2146
28caa186 2147static bfd_boolean
268b6b39
AM
2148_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2149 void *data)
45d6a902 2150{
a50b1753 2151 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2152 Elf_Internal_Verneed *t;
2153 Elf_Internal_Vernaux *a;
2154 bfd_size_type amt;
2155
45d6a902
AM
2156 /* We only care about symbols defined in shared objects with version
2157 information. */
f5385ebf
AM
2158 if (!h->def_dynamic
2159 || h->def_regular
45d6a902 2160 || h->dynindx == -1
7b20f099
AM
2161 || h->verinfo.verdef == NULL
2162 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2163 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2164 return TRUE;
2165
2166 /* See if we already know about this version. */
28caa186
AM
2167 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2168 t != NULL;
2169 t = t->vn_nextref)
45d6a902
AM
2170 {
2171 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2172 continue;
2173
2174 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2175 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2176 return TRUE;
2177
2178 break;
2179 }
2180
2181 /* This is a new version. Add it to tree we are building. */
2182
2183 if (t == NULL)
2184 {
2185 amt = sizeof *t;
a50b1753 2186 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2187 if (t == NULL)
2188 {
2189 rinfo->failed = TRUE;
2190 return FALSE;
2191 }
2192
2193 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2194 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2195 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2196 }
2197
2198 amt = sizeof *a;
a50b1753 2199 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2200 if (a == NULL)
2201 {
2202 rinfo->failed = TRUE;
2203 return FALSE;
2204 }
45d6a902
AM
2205
2206 /* Note that we are copying a string pointer here, and testing it
2207 above. If bfd_elf_string_from_elf_section is ever changed to
2208 discard the string data when low in memory, this will have to be
2209 fixed. */
2210 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2211
2212 a->vna_flags = h->verinfo.verdef->vd_flags;
2213 a->vna_nextptr = t->vn_auxptr;
2214
2215 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2216 ++rinfo->vers;
2217
2218 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2219
2220 t->vn_auxptr = a;
2221
2222 return TRUE;
2223}
2224
099bb8fb
L
2225/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2226 hidden. Set *T_P to NULL if there is no match. */
2227
2228static bfd_boolean
2229_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2230 struct elf_link_hash_entry *h,
2231 const char *version_p,
2232 struct bfd_elf_version_tree **t_p,
2233 bfd_boolean *hide)
2234{
2235 struct bfd_elf_version_tree *t;
2236
2237 /* Look for the version. If we find it, it is no longer weak. */
2238 for (t = info->version_info; t != NULL; t = t->next)
2239 {
2240 if (strcmp (t->name, version_p) == 0)
2241 {
2242 size_t len;
2243 char *alc;
2244 struct bfd_elf_version_expr *d;
2245
2246 len = version_p - h->root.root.string;
2247 alc = (char *) bfd_malloc (len);
2248 if (alc == NULL)
2249 return FALSE;
2250 memcpy (alc, h->root.root.string, len - 1);
2251 alc[len - 1] = '\0';
2252 if (alc[len - 2] == ELF_VER_CHR)
2253 alc[len - 2] = '\0';
2254
2255 h->verinfo.vertree = t;
2256 t->used = TRUE;
2257 d = NULL;
2258
2259 if (t->globals.list != NULL)
2260 d = (*t->match) (&t->globals, NULL, alc);
2261
2262 /* See if there is anything to force this symbol to
2263 local scope. */
2264 if (d == NULL && t->locals.list != NULL)
2265 {
2266 d = (*t->match) (&t->locals, NULL, alc);
2267 if (d != NULL
2268 && h->dynindx != -1
2269 && ! info->export_dynamic)
2270 *hide = TRUE;
2271 }
2272
2273 free (alc);
2274 break;
2275 }
2276 }
2277
2278 *t_p = t;
2279
2280 return TRUE;
2281}
2282
2283/* Return TRUE if the symbol H is hidden by version script. */
2284
2285bfd_boolean
2286_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2287 struct elf_link_hash_entry *h)
2288{
2289 const char *p;
2290 bfd_boolean hide = FALSE;
2291 const struct elf_backend_data *bed
2292 = get_elf_backend_data (info->output_bfd);
2293
2294 /* Version script only hides symbols defined in regular objects. */
2295 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2296 return TRUE;
2297
2298 p = strchr (h->root.root.string, ELF_VER_CHR);
2299 if (p != NULL && h->verinfo.vertree == NULL)
2300 {
2301 struct bfd_elf_version_tree *t;
2302
2303 ++p;
2304 if (*p == ELF_VER_CHR)
2305 ++p;
2306
2307 if (*p != '\0'
2308 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2309 && hide)
2310 {
2311 if (hide)
2312 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2313 return TRUE;
2314 }
2315 }
2316
2317 /* If we don't have a version for this symbol, see if we can find
2318 something. */
2319 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2320 {
2321 h->verinfo.vertree
2322 = bfd_find_version_for_sym (info->version_info,
2323 h->root.root.string, &hide);
2324 if (h->verinfo.vertree != NULL && hide)
2325 {
2326 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2327 return TRUE;
2328 }
2329 }
2330
2331 return FALSE;
2332}
2333
45d6a902
AM
2334/* Figure out appropriate versions for all the symbols. We may not
2335 have the version number script until we have read all of the input
2336 files, so until that point we don't know which symbols should be
2337 local. This function is called via elf_link_hash_traverse. */
2338
28caa186 2339static bfd_boolean
268b6b39 2340_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2341{
28caa186 2342 struct elf_info_failed *sinfo;
45d6a902 2343 struct bfd_link_info *info;
9c5bfbb7 2344 const struct elf_backend_data *bed;
45d6a902
AM
2345 struct elf_info_failed eif;
2346 char *p;
099bb8fb 2347 bfd_boolean hide;
45d6a902 2348
a50b1753 2349 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2350 info = sinfo->info;
2351
45d6a902
AM
2352 /* Fix the symbol flags. */
2353 eif.failed = FALSE;
2354 eif.info = info;
2355 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2356 {
2357 if (eif.failed)
2358 sinfo->failed = TRUE;
2359 return FALSE;
2360 }
2361
0a640d71
L
2362 bed = get_elf_backend_data (info->output_bfd);
2363
45d6a902
AM
2364 /* We only need version numbers for symbols defined in regular
2365 objects. */
f5385ebf 2366 if (!h->def_regular)
0a640d71
L
2367 {
2368 /* Hide symbols defined in discarded input sections. */
2369 if ((h->root.type == bfd_link_hash_defined
2370 || h->root.type == bfd_link_hash_defweak)
2371 && discarded_section (h->root.u.def.section))
2372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2373 return TRUE;
2374 }
45d6a902 2375
099bb8fb 2376 hide = FALSE;
45d6a902
AM
2377 p = strchr (h->root.root.string, ELF_VER_CHR);
2378 if (p != NULL && h->verinfo.vertree == NULL)
2379 {
2380 struct bfd_elf_version_tree *t;
45d6a902 2381
45d6a902
AM
2382 ++p;
2383 if (*p == ELF_VER_CHR)
6e33951e 2384 ++p;
45d6a902
AM
2385
2386 /* If there is no version string, we can just return out. */
2387 if (*p == '\0')
6e33951e 2388 return TRUE;
45d6a902 2389
099bb8fb 2390 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2391 {
099bb8fb
L
2392 sinfo->failed = TRUE;
2393 return FALSE;
45d6a902
AM
2394 }
2395
099bb8fb
L
2396 if (hide)
2397 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2398
45d6a902
AM
2399 /* If we are building an application, we need to create a
2400 version node for this version. */
0e1862bb 2401 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2402 {
2403 struct bfd_elf_version_tree **pp;
2404 int version_index;
2405
2406 /* If we aren't going to export this symbol, we don't need
2407 to worry about it. */
2408 if (h->dynindx == -1)
2409 return TRUE;
2410
ef53be89
AM
2411 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2412 sizeof *t);
45d6a902
AM
2413 if (t == NULL)
2414 {
2415 sinfo->failed = TRUE;
2416 return FALSE;
2417 }
2418
45d6a902 2419 t->name = p;
45d6a902
AM
2420 t->name_indx = (unsigned int) -1;
2421 t->used = TRUE;
2422
2423 version_index = 1;
2424 /* Don't count anonymous version tag. */
fd91d419
L
2425 if (sinfo->info->version_info != NULL
2426 && sinfo->info->version_info->vernum == 0)
45d6a902 2427 version_index = 0;
fd91d419
L
2428 for (pp = &sinfo->info->version_info;
2429 *pp != NULL;
2430 pp = &(*pp)->next)
45d6a902
AM
2431 ++version_index;
2432 t->vernum = version_index;
2433
2434 *pp = t;
2435
2436 h->verinfo.vertree = t;
2437 }
2438 else if (t == NULL)
2439 {
2440 /* We could not find the version for a symbol when
2441 generating a shared archive. Return an error. */
4eca0228 2442 _bfd_error_handler
695344c0 2443 /* xgettext:c-format */
871b3ab2 2444 (_("%pB: version node not found for symbol %s"),
28caa186 2445 info->output_bfd, h->root.root.string);
45d6a902
AM
2446 bfd_set_error (bfd_error_bad_value);
2447 sinfo->failed = TRUE;
2448 return FALSE;
2449 }
45d6a902
AM
2450 }
2451
2452 /* If we don't have a version for this symbol, see if we can find
2453 something. */
099bb8fb
L
2454 if (!hide
2455 && h->verinfo.vertree == NULL
2456 && sinfo->info->version_info != NULL)
45d6a902 2457 {
fd91d419
L
2458 h->verinfo.vertree
2459 = bfd_find_version_for_sym (sinfo->info->version_info,
2460 h->root.root.string, &hide);
1e8fa21e
AM
2461 if (h->verinfo.vertree != NULL && hide)
2462 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2463 }
2464
2465 return TRUE;
2466}
2467\f
45d6a902
AM
2468/* Read and swap the relocs from the section indicated by SHDR. This
2469 may be either a REL or a RELA section. The relocations are
2470 translated into RELA relocations and stored in INTERNAL_RELOCS,
2471 which should have already been allocated to contain enough space.
2472 The EXTERNAL_RELOCS are a buffer where the external form of the
2473 relocations should be stored.
2474
2475 Returns FALSE if something goes wrong. */
2476
2477static bfd_boolean
268b6b39 2478elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2479 asection *sec,
268b6b39
AM
2480 Elf_Internal_Shdr *shdr,
2481 void *external_relocs,
2482 Elf_Internal_Rela *internal_relocs)
45d6a902 2483{
9c5bfbb7 2484 const struct elf_backend_data *bed;
268b6b39 2485 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2486 const bfd_byte *erela;
2487 const bfd_byte *erelaend;
2488 Elf_Internal_Rela *irela;
243ef1e0
L
2489 Elf_Internal_Shdr *symtab_hdr;
2490 size_t nsyms;
45d6a902 2491
45d6a902
AM
2492 /* Position ourselves at the start of the section. */
2493 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2494 return FALSE;
2495
2496 /* Read the relocations. */
2497 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2498 return FALSE;
2499
243ef1e0 2500 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2501 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2502
45d6a902
AM
2503 bed = get_elf_backend_data (abfd);
2504
2505 /* Convert the external relocations to the internal format. */
2506 if (shdr->sh_entsize == bed->s->sizeof_rel)
2507 swap_in = bed->s->swap_reloc_in;
2508 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2509 swap_in = bed->s->swap_reloca_in;
2510 else
2511 {
2512 bfd_set_error (bfd_error_wrong_format);
2513 return FALSE;
2514 }
2515
a50b1753 2516 erela = (const bfd_byte *) external_relocs;
51992aec 2517 erelaend = erela + shdr->sh_size;
45d6a902
AM
2518 irela = internal_relocs;
2519 while (erela < erelaend)
2520 {
243ef1e0
L
2521 bfd_vma r_symndx;
2522
45d6a902 2523 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2524 r_symndx = ELF32_R_SYM (irela->r_info);
2525 if (bed->s->arch_size == 64)
2526 r_symndx >>= 24;
ce98a316
NC
2527 if (nsyms > 0)
2528 {
2529 if ((size_t) r_symndx >= nsyms)
2530 {
4eca0228 2531 _bfd_error_handler
695344c0 2532 /* xgettext:c-format */
2dcf00ce
AM
2533 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2534 " for offset %#" PRIx64 " in section `%pA'"),
2535 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2536 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2537 bfd_set_error (bfd_error_bad_value);
2538 return FALSE;
2539 }
2540 }
cf35638d 2541 else if (r_symndx != STN_UNDEF)
243ef1e0 2542 {
4eca0228 2543 _bfd_error_handler
695344c0 2544 /* xgettext:c-format */
2dcf00ce
AM
2545 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2546 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2547 " when the object file has no symbol table"),
2dcf00ce
AM
2548 abfd, (uint64_t) r_symndx,
2549 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2550 bfd_set_error (bfd_error_bad_value);
2551 return FALSE;
2552 }
45d6a902
AM
2553 irela += bed->s->int_rels_per_ext_rel;
2554 erela += shdr->sh_entsize;
2555 }
2556
2557 return TRUE;
2558}
2559
2560/* Read and swap the relocs for a section O. They may have been
2561 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2562 not NULL, they are used as buffers to read into. They are known to
2563 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2564 the return value is allocated using either malloc or bfd_alloc,
2565 according to the KEEP_MEMORY argument. If O has two relocation
2566 sections (both REL and RELA relocations), then the REL_HDR
2567 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2568 RELA_HDR relocations. */
45d6a902
AM
2569
2570Elf_Internal_Rela *
268b6b39
AM
2571_bfd_elf_link_read_relocs (bfd *abfd,
2572 asection *o,
2573 void *external_relocs,
2574 Elf_Internal_Rela *internal_relocs,
2575 bfd_boolean keep_memory)
45d6a902 2576{
268b6b39 2577 void *alloc1 = NULL;
45d6a902 2578 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2579 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2580 struct bfd_elf_section_data *esdo = elf_section_data (o);
2581 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2582
d4730f92
BS
2583 if (esdo->relocs != NULL)
2584 return esdo->relocs;
45d6a902
AM
2585
2586 if (o->reloc_count == 0)
2587 return NULL;
2588
45d6a902
AM
2589 if (internal_relocs == NULL)
2590 {
2591 bfd_size_type size;
2592
056bafd4 2593 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2594 if (keep_memory)
a50b1753 2595 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2596 else
a50b1753 2597 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2598 if (internal_relocs == NULL)
2599 goto error_return;
2600 }
2601
2602 if (external_relocs == NULL)
2603 {
d4730f92
BS
2604 bfd_size_type size = 0;
2605
2606 if (esdo->rel.hdr)
2607 size += esdo->rel.hdr->sh_size;
2608 if (esdo->rela.hdr)
2609 size += esdo->rela.hdr->sh_size;
45d6a902 2610
268b6b39 2611 alloc1 = bfd_malloc (size);
45d6a902
AM
2612 if (alloc1 == NULL)
2613 goto error_return;
2614 external_relocs = alloc1;
2615 }
2616
d4730f92
BS
2617 internal_rela_relocs = internal_relocs;
2618 if (esdo->rel.hdr)
2619 {
2620 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2621 external_relocs,
2622 internal_relocs))
2623 goto error_return;
2624 external_relocs = (((bfd_byte *) external_relocs)
2625 + esdo->rel.hdr->sh_size);
2626 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2627 * bed->s->int_rels_per_ext_rel);
2628 }
2629
2630 if (esdo->rela.hdr
2631 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2632 external_relocs,
2633 internal_rela_relocs)))
45d6a902
AM
2634 goto error_return;
2635
2636 /* Cache the results for next time, if we can. */
2637 if (keep_memory)
d4730f92 2638 esdo->relocs = internal_relocs;
45d6a902
AM
2639
2640 if (alloc1 != NULL)
2641 free (alloc1);
2642
2643 /* Don't free alloc2, since if it was allocated we are passing it
2644 back (under the name of internal_relocs). */
2645
2646 return internal_relocs;
2647
2648 error_return:
2649 if (alloc1 != NULL)
2650 free (alloc1);
2651 if (alloc2 != NULL)
4dd07732
AM
2652 {
2653 if (keep_memory)
2654 bfd_release (abfd, alloc2);
2655 else
2656 free (alloc2);
2657 }
45d6a902
AM
2658 return NULL;
2659}
2660
2661/* Compute the size of, and allocate space for, REL_HDR which is the
2662 section header for a section containing relocations for O. */
2663
28caa186 2664static bfd_boolean
9eaff861
AO
2665_bfd_elf_link_size_reloc_section (bfd *abfd,
2666 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2667{
9eaff861 2668 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2669
2670 /* That allows us to calculate the size of the section. */
9eaff861 2671 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2672
2673 /* The contents field must last into write_object_contents, so we
2674 allocate it with bfd_alloc rather than malloc. Also since we
2675 cannot be sure that the contents will actually be filled in,
2676 we zero the allocated space. */
a50b1753 2677 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2678 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2679 return FALSE;
2680
d4730f92 2681 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2682 {
2683 struct elf_link_hash_entry **p;
2684
ca4be51c
AM
2685 p = ((struct elf_link_hash_entry **)
2686 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2687 if (p == NULL)
2688 return FALSE;
2689
d4730f92 2690 reldata->hashes = p;
45d6a902
AM
2691 }
2692
2693 return TRUE;
2694}
2695
2696/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2697 originated from the section given by INPUT_REL_HDR) to the
2698 OUTPUT_BFD. */
2699
2700bfd_boolean
268b6b39
AM
2701_bfd_elf_link_output_relocs (bfd *output_bfd,
2702 asection *input_section,
2703 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2704 Elf_Internal_Rela *internal_relocs,
2705 struct elf_link_hash_entry **rel_hash
2706 ATTRIBUTE_UNUSED)
45d6a902
AM
2707{
2708 Elf_Internal_Rela *irela;
2709 Elf_Internal_Rela *irelaend;
2710 bfd_byte *erel;
d4730f92 2711 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2712 asection *output_section;
9c5bfbb7 2713 const struct elf_backend_data *bed;
268b6b39 2714 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2715 struct bfd_elf_section_data *esdo;
45d6a902
AM
2716
2717 output_section = input_section->output_section;
45d6a902 2718
d4730f92
BS
2719 bed = get_elf_backend_data (output_bfd);
2720 esdo = elf_section_data (output_section);
2721 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2722 {
d4730f92
BS
2723 output_reldata = &esdo->rel;
2724 swap_out = bed->s->swap_reloc_out;
45d6a902 2725 }
d4730f92
BS
2726 else if (esdo->rela.hdr
2727 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2728 {
d4730f92
BS
2729 output_reldata = &esdo->rela;
2730 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2731 }
2732 else
2733 {
4eca0228 2734 _bfd_error_handler
695344c0 2735 /* xgettext:c-format */
871b3ab2 2736 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2737 output_bfd, input_section->owner, input_section);
297d8443 2738 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2739 return FALSE;
2740 }
2741
d4730f92
BS
2742 erel = output_reldata->hdr->contents;
2743 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2744 irela = internal_relocs;
2745 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2746 * bed->s->int_rels_per_ext_rel);
2747 while (irela < irelaend)
2748 {
2749 (*swap_out) (output_bfd, irela, erel);
2750 irela += bed->s->int_rels_per_ext_rel;
2751 erel += input_rel_hdr->sh_entsize;
2752 }
2753
2754 /* Bump the counter, so that we know where to add the next set of
2755 relocations. */
d4730f92 2756 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2757
2758 return TRUE;
2759}
2760\f
508c3946
L
2761/* Make weak undefined symbols in PIE dynamic. */
2762
2763bfd_boolean
2764_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2765 struct elf_link_hash_entry *h)
2766{
0e1862bb 2767 if (bfd_link_pie (info)
508c3946
L
2768 && h->dynindx == -1
2769 && h->root.type == bfd_link_hash_undefweak)
2770 return bfd_elf_link_record_dynamic_symbol (info, h);
2771
2772 return TRUE;
2773}
2774
45d6a902
AM
2775/* Fix up the flags for a symbol. This handles various cases which
2776 can only be fixed after all the input files are seen. This is
2777 currently called by both adjust_dynamic_symbol and
2778 assign_sym_version, which is unnecessary but perhaps more robust in
2779 the face of future changes. */
2780
28caa186 2781static bfd_boolean
268b6b39
AM
2782_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2783 struct elf_info_failed *eif)
45d6a902 2784{
33774f08 2785 const struct elf_backend_data *bed;
508c3946 2786
45d6a902
AM
2787 /* If this symbol was mentioned in a non-ELF file, try to set
2788 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2789 permit a non-ELF file to correctly refer to a symbol defined in
2790 an ELF dynamic object. */
f5385ebf 2791 if (h->non_elf)
45d6a902
AM
2792 {
2793 while (h->root.type == bfd_link_hash_indirect)
2794 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2795
2796 if (h->root.type != bfd_link_hash_defined
2797 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2798 {
2799 h->ref_regular = 1;
2800 h->ref_regular_nonweak = 1;
2801 }
45d6a902
AM
2802 else
2803 {
2804 if (h->root.u.def.section->owner != NULL
2805 && (bfd_get_flavour (h->root.u.def.section->owner)
2806 == bfd_target_elf_flavour))
f5385ebf
AM
2807 {
2808 h->ref_regular = 1;
2809 h->ref_regular_nonweak = 1;
2810 }
45d6a902 2811 else
f5385ebf 2812 h->def_regular = 1;
45d6a902
AM
2813 }
2814
2815 if (h->dynindx == -1
f5385ebf
AM
2816 && (h->def_dynamic
2817 || h->ref_dynamic))
45d6a902 2818 {
c152c796 2819 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2820 {
2821 eif->failed = TRUE;
2822 return FALSE;
2823 }
2824 }
2825 }
2826 else
2827 {
f5385ebf 2828 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2829 was first seen in a non-ELF file. Fortunately, if the symbol
2830 was first seen in an ELF file, we're probably OK unless the
2831 symbol was defined in a non-ELF file. Catch that case here.
2832 FIXME: We're still in trouble if the symbol was first seen in
2833 a dynamic object, and then later in a non-ELF regular object. */
2834 if ((h->root.type == bfd_link_hash_defined
2835 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2836 && !h->def_regular
45d6a902
AM
2837 && (h->root.u.def.section->owner != NULL
2838 ? (bfd_get_flavour (h->root.u.def.section->owner)
2839 != bfd_target_elf_flavour)
2840 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2841 && !h->def_dynamic)))
2842 h->def_regular = 1;
45d6a902
AM
2843 }
2844
508c3946 2845 /* Backend specific symbol fixup. */
33774f08
AM
2846 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2847 if (bed->elf_backend_fixup_symbol
2848 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2849 return FALSE;
508c3946 2850
45d6a902
AM
2851 /* If this is a final link, and the symbol was defined as a common
2852 symbol in a regular object file, and there was no definition in
2853 any dynamic object, then the linker will have allocated space for
f5385ebf 2854 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2855 flag will not have been set. */
2856 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2857 && !h->def_regular
2858 && h->ref_regular
2859 && !h->def_dynamic
96f29d96 2860 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2861 h->def_regular = 1;
45d6a902 2862
af0bfb9c
AM
2863 /* Symbols defined in discarded sections shouldn't be dynamic. */
2864 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2865 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2866
4deb8f71
L
2867 /* If a weak undefined symbol has non-default visibility, we also
2868 hide it from the dynamic linker. */
af0bfb9c
AM
2869 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2870 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
2873 /* A hidden versioned symbol in executable should be forced local if
2874 it is is locally defined, not referenced by shared library and not
2875 exported. */
2876 else if (bfd_link_executable (eif->info)
2877 && h->versioned == versioned_hidden
2878 && !eif->info->export_dynamic
2879 && !h->dynamic
2880 && !h->ref_dynamic
2881 && h->def_regular)
2882 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2883
45d6a902
AM
2884 /* If -Bsymbolic was used (which means to bind references to global
2885 symbols to the definition within the shared object), and this
2886 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2887 need a PLT entry. Likewise, if the symbol has non-default
2888 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2889 will force it local. */
4deb8f71
L
2890 else if (h->needs_plt
2891 && bfd_link_pic (eif->info)
2892 && is_elf_hash_table (eif->info->hash)
2893 && (SYMBOLIC_BIND (eif->info, h)
2894 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2895 && h->def_regular)
45d6a902 2896 {
45d6a902
AM
2897 bfd_boolean force_local;
2898
45d6a902
AM
2899 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2900 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2901 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2902 }
2903
45d6a902
AM
2904 /* If this is a weak defined symbol in a dynamic object, and we know
2905 the real definition in the dynamic object, copy interesting flags
2906 over to the real definition. */
60d67dc8 2907 if (h->is_weakalias)
45d6a902 2908 {
60d67dc8
AM
2909 struct elf_link_hash_entry *def = weakdef (h);
2910
45d6a902
AM
2911 /* If the real definition is defined by a regular object file,
2912 don't do anything special. See the longer description in
2913 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2914 if (def->def_regular)
2915 {
2916 h = def;
2917 while ((h = h->u.alias) != def)
2918 h->is_weakalias = 0;
2919 }
45d6a902 2920 else
a26587ba 2921 {
4e6b54a6
AM
2922 while (h->root.type == bfd_link_hash_indirect)
2923 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2924 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2925 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2926 BFD_ASSERT (def->def_dynamic);
2927 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2928 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2929 }
45d6a902
AM
2930 }
2931
2932 return TRUE;
2933}
2934
2935/* Make the backend pick a good value for a dynamic symbol. This is
2936 called via elf_link_hash_traverse, and also calls itself
2937 recursively. */
2938
28caa186 2939static bfd_boolean
268b6b39 2940_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2941{
a50b1753 2942 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2943 struct elf_link_hash_table *htab;
9c5bfbb7 2944 const struct elf_backend_data *bed;
45d6a902 2945
0eddce27 2946 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2947 return FALSE;
2948
45d6a902
AM
2949 /* Ignore indirect symbols. These are added by the versioning code. */
2950 if (h->root.type == bfd_link_hash_indirect)
2951 return TRUE;
2952
2953 /* Fix the symbol flags. */
2954 if (! _bfd_elf_fix_symbol_flags (h, eif))
2955 return FALSE;
2956
559192d8
AM
2957 htab = elf_hash_table (eif->info);
2958 bed = get_elf_backend_data (htab->dynobj);
2959
954b63d4
AM
2960 if (h->root.type == bfd_link_hash_undefweak)
2961 {
2962 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2963 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2964 else if (eif->info->dynamic_undefined_weak > 0
2965 && h->ref_regular
2966 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2967 && !bfd_hide_sym_by_version (eif->info->version_info,
2968 h->root.root.string))
2969 {
2970 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2971 {
2972 eif->failed = TRUE;
2973 return FALSE;
2974 }
2975 }
2976 }
2977
45d6a902
AM
2978 /* If this symbol does not require a PLT entry, and it is not
2979 defined by a dynamic object, or is not referenced by a regular
2980 object, ignore it. We do have to handle a weak defined symbol,
2981 even if no regular object refers to it, if we decided to add it
2982 to the dynamic symbol table. FIXME: Do we normally need to worry
2983 about symbols which are defined by one dynamic object and
2984 referenced by another one? */
f5385ebf 2985 if (!h->needs_plt
91e21fb7 2986 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2987 && (h->def_regular
2988 || !h->def_dynamic
2989 || (!h->ref_regular
60d67dc8 2990 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 2991 {
a6aa5195 2992 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2993 return TRUE;
2994 }
2995
2996 /* If we've already adjusted this symbol, don't do it again. This
2997 can happen via a recursive call. */
f5385ebf 2998 if (h->dynamic_adjusted)
45d6a902
AM
2999 return TRUE;
3000
3001 /* Don't look at this symbol again. Note that we must set this
3002 after checking the above conditions, because we may look at a
3003 symbol once, decide not to do anything, and then get called
3004 recursively later after REF_REGULAR is set below. */
f5385ebf 3005 h->dynamic_adjusted = 1;
45d6a902
AM
3006
3007 /* If this is a weak definition, and we know a real definition, and
3008 the real symbol is not itself defined by a regular object file,
3009 then get a good value for the real definition. We handle the
3010 real symbol first, for the convenience of the backend routine.
3011
3012 Note that there is a confusing case here. If the real definition
3013 is defined by a regular object file, we don't get the real symbol
3014 from the dynamic object, but we do get the weak symbol. If the
3015 processor backend uses a COPY reloc, then if some routine in the
3016 dynamic object changes the real symbol, we will not see that
3017 change in the corresponding weak symbol. This is the way other
3018 ELF linkers work as well, and seems to be a result of the shared
3019 library model.
3020
3021 I will clarify this issue. Most SVR4 shared libraries define the
3022 variable _timezone and define timezone as a weak synonym. The
3023 tzset call changes _timezone. If you write
3024 extern int timezone;
3025 int _timezone = 5;
3026 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3027 you might expect that, since timezone is a synonym for _timezone,
3028 the same number will print both times. However, if the processor
3029 backend uses a COPY reloc, then actually timezone will be copied
3030 into your process image, and, since you define _timezone
3031 yourself, _timezone will not. Thus timezone and _timezone will
3032 wind up at different memory locations. The tzset call will set
3033 _timezone, leaving timezone unchanged. */
3034
60d67dc8 3035 if (h->is_weakalias)
45d6a902 3036 {
60d67dc8
AM
3037 struct elf_link_hash_entry *def = weakdef (h);
3038
ec24dc88 3039 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3040 the alias by a regular object file via the weak symbol H. */
3041 def->ref_regular = 1;
45d6a902 3042
ec24dc88 3043 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3044 the strong alias before H by recursively calling ourselves. */
3045 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3046 return FALSE;
3047 }
3048
3049 /* If a symbol has no type and no size and does not require a PLT
3050 entry, then we are probably about to do the wrong thing here: we
3051 are probably going to create a COPY reloc for an empty object.
3052 This case can arise when a shared object is built with assembly
3053 code, and the assembly code fails to set the symbol type. */
3054 if (h->size == 0
3055 && h->type == STT_NOTYPE
f5385ebf 3056 && !h->needs_plt)
4eca0228 3057 _bfd_error_handler
45d6a902
AM
3058 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3059 h->root.root.string);
3060
45d6a902
AM
3061 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3062 {
3063 eif->failed = TRUE;
3064 return FALSE;
3065 }
3066
3067 return TRUE;
3068}
3069
027297b7
L
3070/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3071 DYNBSS. */
3072
3073bfd_boolean
6cabe1ea
AM
3074_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3075 struct elf_link_hash_entry *h,
027297b7
L
3076 asection *dynbss)
3077{
91ac5911 3078 unsigned int power_of_two;
027297b7
L
3079 bfd_vma mask;
3080 asection *sec = h->root.u.def.section;
3081
de194d85 3082 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3083 requirement of symbols defined in the section. Since we don't
3084 know the symbol alignment requirement, we start with the
3085 maximum alignment and check low bits of the symbol address
3086 for the minimum alignment. */
3087 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3088 mask = ((bfd_vma) 1 << power_of_two) - 1;
3089 while ((h->root.u.def.value & mask) != 0)
3090 {
3091 mask >>= 1;
3092 --power_of_two;
3093 }
027297b7 3094
91ac5911
L
3095 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3096 dynbss))
027297b7
L
3097 {
3098 /* Adjust the section alignment if needed. */
3099 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3100 power_of_two))
027297b7
L
3101 return FALSE;
3102 }
3103
91ac5911 3104 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3105 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3106
3107 /* Define the symbol as being at this point in DYNBSS. */
3108 h->root.u.def.section = dynbss;
3109 h->root.u.def.value = dynbss->size;
3110
3111 /* Increment the size of DYNBSS to make room for the symbol. */
3112 dynbss->size += h->size;
3113
f7483970
L
3114 /* No error if extern_protected_data is true. */
3115 if (h->protected_def
889c2a67
L
3116 && (!info->extern_protected_data
3117 || (info->extern_protected_data < 0
3118 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3119 info->callbacks->einfo
c1c8c1ef 3120 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3121 h->root.root.string);
6cabe1ea 3122
027297b7
L
3123 return TRUE;
3124}
3125
45d6a902
AM
3126/* Adjust all external symbols pointing into SEC_MERGE sections
3127 to reflect the object merging within the sections. */
3128
28caa186 3129static bfd_boolean
268b6b39 3130_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3131{
3132 asection *sec;
3133
45d6a902
AM
3134 if ((h->root.type == bfd_link_hash_defined
3135 || h->root.type == bfd_link_hash_defweak)
3136 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3137 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3138 {
a50b1753 3139 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3140
3141 h->root.u.def.value =
3142 _bfd_merged_section_offset (output_bfd,
3143 &h->root.u.def.section,
3144 elf_section_data (sec)->sec_info,
753731ee 3145 h->root.u.def.value);
45d6a902
AM
3146 }
3147
3148 return TRUE;
3149}
986a241f
RH
3150
3151/* Returns false if the symbol referred to by H should be considered
3152 to resolve local to the current module, and true if it should be
3153 considered to bind dynamically. */
3154
3155bfd_boolean
268b6b39
AM
3156_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3157 struct bfd_link_info *info,
89a2ee5a 3158 bfd_boolean not_local_protected)
986a241f
RH
3159{
3160 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3161 const struct elf_backend_data *bed;
3162 struct elf_link_hash_table *hash_table;
986a241f
RH
3163
3164 if (h == NULL)
3165 return FALSE;
3166
3167 while (h->root.type == bfd_link_hash_indirect
3168 || h->root.type == bfd_link_hash_warning)
3169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3170
3171 /* If it was forced local, then clearly it's not dynamic. */
3172 if (h->dynindx == -1)
3173 return FALSE;
f5385ebf 3174 if (h->forced_local)
986a241f
RH
3175 return FALSE;
3176
3177 /* Identify the cases where name binding rules say that a
3178 visible symbol resolves locally. */
0e1862bb
L
3179 binding_stays_local_p = (bfd_link_executable (info)
3180 || SYMBOLIC_BIND (info, h));
986a241f
RH
3181
3182 switch (ELF_ST_VISIBILITY (h->other))
3183 {
3184 case STV_INTERNAL:
3185 case STV_HIDDEN:
3186 return FALSE;
3187
3188 case STV_PROTECTED:
fcb93ecf
PB
3189 hash_table = elf_hash_table (info);
3190 if (!is_elf_hash_table (hash_table))
3191 return FALSE;
3192
3193 bed = get_elf_backend_data (hash_table->dynobj);
3194
986a241f
RH
3195 /* Proper resolution for function pointer equality may require
3196 that these symbols perhaps be resolved dynamically, even though
3197 we should be resolving them to the current module. */
89a2ee5a 3198 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3199 binding_stays_local_p = TRUE;
3200 break;
3201
3202 default:
986a241f
RH
3203 break;
3204 }
3205
aa37626c 3206 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3207 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3208 return TRUE;
3209
986a241f
RH
3210 /* Otherwise, the symbol is dynamic if binding rules don't tell
3211 us that it remains local. */
3212 return !binding_stays_local_p;
3213}
f6c52c13
AM
3214
3215/* Return true if the symbol referred to by H should be considered
3216 to resolve local to the current module, and false otherwise. Differs
3217 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3218 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3219 for the place where dynindx == -1 is tested. If that test is true,
3220 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3221 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3222 defined symbols.
89a2ee5a
AM
3223 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3224 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3225 treatment of undefined weak symbols. For those that do not make
3226 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3227
3228bfd_boolean
268b6b39
AM
3229_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3230 struct bfd_link_info *info,
3231 bfd_boolean local_protected)
f6c52c13 3232{
fcb93ecf
PB
3233 const struct elf_backend_data *bed;
3234 struct elf_link_hash_table *hash_table;
3235
f6c52c13
AM
3236 /* If it's a local sym, of course we resolve locally. */
3237 if (h == NULL)
3238 return TRUE;
3239
d95edcac
L
3240 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3241 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3242 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3243 return TRUE;
3244
0fad2956
MR
3245 /* Forced local symbols resolve locally. */
3246 if (h->forced_local)
3247 return TRUE;
3248
7e2294f9
AO
3249 /* Common symbols that become definitions don't get the DEF_REGULAR
3250 flag set, so test it first, and don't bail out. */
3251 if (ELF_COMMON_DEF_P (h))
3252 /* Do nothing. */;
f6c52c13 3253 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3254 resolve locally. The sym is either undefined or dynamic. */
3255 else if (!h->def_regular)
f6c52c13
AM
3256 return FALSE;
3257
0fad2956 3258 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3259 if (h->dynindx == -1)
3260 return TRUE;
3261
3262 /* At this point, we know the symbol is defined and dynamic. In an
3263 executable it must resolve locally, likewise when building symbolic
3264 shared libraries. */
0e1862bb 3265 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3266 return TRUE;
3267
3268 /* Now deal with defined dynamic symbols in shared libraries. Ones
3269 with default visibility might not resolve locally. */
3270 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3271 return FALSE;
3272
fcb93ecf
PB
3273 hash_table = elf_hash_table (info);
3274 if (!is_elf_hash_table (hash_table))
3275 return TRUE;
3276
3277 bed = get_elf_backend_data (hash_table->dynobj);
3278
f7483970
L
3279 /* If extern_protected_data is false, STV_PROTECTED non-function
3280 symbols are local. */
889c2a67
L
3281 if ((!info->extern_protected_data
3282 || (info->extern_protected_data < 0
3283 && !bed->extern_protected_data))
3284 && !bed->is_function_type (h->type))
1c16dfa5
L
3285 return TRUE;
3286
f6c52c13 3287 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3288 symbols be treated as dynamic symbols. If the address of a
3289 function not defined in an executable is set to that function's
3290 plt entry in the executable, then the address of the function in
3291 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3292 return local_protected;
3293}
e1918d23
AM
3294
3295/* Caches some TLS segment info, and ensures that the TLS segment vma is
3296 aligned. Returns the first TLS output section. */
3297
3298struct bfd_section *
3299_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3300{
3301 struct bfd_section *sec, *tls;
3302 unsigned int align = 0;
3303
3304 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3305 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3306 break;
3307 tls = sec;
3308
3309 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3310 if (sec->alignment_power > align)
3311 align = sec->alignment_power;
3312
3313 elf_hash_table (info)->tls_sec = tls;
3314
3315 /* Ensure the alignment of the first section is the largest alignment,
3316 so that the tls segment starts aligned. */
3317 if (tls != NULL)
3318 tls->alignment_power = align;
3319
3320 return tls;
3321}
0ad989f9
L
3322
3323/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3324static bfd_boolean
3325is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3326 Elf_Internal_Sym *sym)
3327{
a4d8e49b
L
3328 const struct elf_backend_data *bed;
3329
0ad989f9
L
3330 /* Local symbols do not count, but target specific ones might. */
3331 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3332 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3333 return FALSE;
3334
fcb93ecf 3335 bed = get_elf_backend_data (abfd);
0ad989f9 3336 /* Function symbols do not count. */
fcb93ecf 3337 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3338 return FALSE;
3339
3340 /* If the section is undefined, then so is the symbol. */
3341 if (sym->st_shndx == SHN_UNDEF)
3342 return FALSE;
3343
3344 /* If the symbol is defined in the common section, then
3345 it is a common definition and so does not count. */
a4d8e49b 3346 if (bed->common_definition (sym))
0ad989f9
L
3347 return FALSE;
3348
3349 /* If the symbol is in a target specific section then we
3350 must rely upon the backend to tell us what it is. */
3351 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3352 /* FIXME - this function is not coded yet:
3353
3354 return _bfd_is_global_symbol_definition (abfd, sym);
3355
3356 Instead for now assume that the definition is not global,
3357 Even if this is wrong, at least the linker will behave
3358 in the same way that it used to do. */
3359 return FALSE;
3360
3361 return TRUE;
3362}
3363
3364/* Search the symbol table of the archive element of the archive ABFD
3365 whose archive map contains a mention of SYMDEF, and determine if
3366 the symbol is defined in this element. */
3367static bfd_boolean
3368elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3369{
3370 Elf_Internal_Shdr * hdr;
ef53be89
AM
3371 size_t symcount;
3372 size_t extsymcount;
3373 size_t extsymoff;
0ad989f9
L
3374 Elf_Internal_Sym *isymbuf;
3375 Elf_Internal_Sym *isym;
3376 Elf_Internal_Sym *isymend;
3377 bfd_boolean result;
3378
3379 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3380 if (abfd == NULL)
3381 return FALSE;
3382
3383 if (! bfd_check_format (abfd, bfd_object))
3384 return FALSE;
3385
7dc3990e
L
3386 /* Select the appropriate symbol table. If we don't know if the
3387 object file is an IR object, give linker LTO plugin a chance to
3388 get the correct symbol table. */
3389 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3390#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3391 || (abfd->plugin_format == bfd_plugin_unknown
3392 && bfd_link_plugin_object_p (abfd))
3393#endif
3394 )
3395 {
3396 /* Use the IR symbol table if the object has been claimed by
3397 plugin. */
3398 abfd = abfd->plugin_dummy_bfd;
3399 hdr = &elf_tdata (abfd)->symtab_hdr;
3400 }
3401 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3402 hdr = &elf_tdata (abfd)->symtab_hdr;
3403 else
3404 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3405
3406 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3407
3408 /* The sh_info field of the symtab header tells us where the
3409 external symbols start. We don't care about the local symbols. */
3410 if (elf_bad_symtab (abfd))
3411 {
3412 extsymcount = symcount;
3413 extsymoff = 0;
3414 }
3415 else
3416 {
3417 extsymcount = symcount - hdr->sh_info;
3418 extsymoff = hdr->sh_info;
3419 }
3420
3421 if (extsymcount == 0)
3422 return FALSE;
3423
3424 /* Read in the symbol table. */
3425 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3426 NULL, NULL, NULL);
3427 if (isymbuf == NULL)
3428 return FALSE;
3429
3430 /* Scan the symbol table looking for SYMDEF. */
3431 result = FALSE;
3432 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3433 {
3434 const char *name;
3435
3436 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3437 isym->st_name);
3438 if (name == NULL)
3439 break;
3440
3441 if (strcmp (name, symdef->name) == 0)
3442 {
3443 result = is_global_data_symbol_definition (abfd, isym);
3444 break;
3445 }
3446 }
3447
3448 free (isymbuf);
3449
3450 return result;
3451}
3452\f
5a580b3a
AM
3453/* Add an entry to the .dynamic table. */
3454
3455bfd_boolean
3456_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3457 bfd_vma tag,
3458 bfd_vma val)
3459{
3460 struct elf_link_hash_table *hash_table;
3461 const struct elf_backend_data *bed;
3462 asection *s;
3463 bfd_size_type newsize;
3464 bfd_byte *newcontents;
3465 Elf_Internal_Dyn dyn;
3466
3467 hash_table = elf_hash_table (info);
3468 if (! is_elf_hash_table (hash_table))
3469 return FALSE;
3470
7f923b7f
AM
3471 if (tag == DT_RELA || tag == DT_REL)
3472 hash_table->dynamic_relocs = TRUE;
3473
5a580b3a 3474 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3475 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3476 BFD_ASSERT (s != NULL);
3477
eea6121a 3478 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3479 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3480 if (newcontents == NULL)
3481 return FALSE;
3482
3483 dyn.d_tag = tag;
3484 dyn.d_un.d_val = val;
eea6121a 3485 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3486
eea6121a 3487 s->size = newsize;
5a580b3a
AM
3488 s->contents = newcontents;
3489
3490 return TRUE;
3491}
3492
3493/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3494 otherwise just check whether one already exists. Returns -1 on error,
3495 1 if a DT_NEEDED tag already exists, and 0 on success. */
3496
4ad4eba5 3497static int
7e9f0867
AM
3498elf_add_dt_needed_tag (bfd *abfd,
3499 struct bfd_link_info *info,
4ad4eba5
AM
3500 const char *soname,
3501 bfd_boolean do_it)
5a580b3a
AM
3502{
3503 struct elf_link_hash_table *hash_table;
ef53be89 3504 size_t strindex;
5a580b3a 3505
7e9f0867
AM
3506 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3507 return -1;
3508
5a580b3a 3509 hash_table = elf_hash_table (info);
5a580b3a 3510 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3511 if (strindex == (size_t) -1)
5a580b3a
AM
3512 return -1;
3513
02be4619 3514 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3515 {
3516 asection *sdyn;
3517 const struct elf_backend_data *bed;
3518 bfd_byte *extdyn;
3519
3520 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3521 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3522 if (sdyn != NULL)
3523 for (extdyn = sdyn->contents;
3524 extdyn < sdyn->contents + sdyn->size;
3525 extdyn += bed->s->sizeof_dyn)
3526 {
3527 Elf_Internal_Dyn dyn;
5a580b3a 3528
7e9f0867
AM
3529 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3530 if (dyn.d_tag == DT_NEEDED
3531 && dyn.d_un.d_val == strindex)
3532 {
3533 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3534 return 1;
3535 }
3536 }
5a580b3a
AM
3537 }
3538
3539 if (do_it)
3540 {
7e9f0867
AM
3541 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3542 return -1;
3543
5a580b3a
AM
3544 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3545 return -1;
3546 }
3547 else
3548 /* We were just checking for existence of the tag. */
3549 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3550
3551 return 0;
3552}
3553
7b15fa7a
AM
3554/* Return true if SONAME is on the needed list between NEEDED and STOP
3555 (or the end of list if STOP is NULL), and needed by a library that
3556 will be loaded. */
3557
010e5ae2 3558static bfd_boolean
7b15fa7a
AM
3559on_needed_list (const char *soname,
3560 struct bfd_link_needed_list *needed,
3561 struct bfd_link_needed_list *stop)
010e5ae2 3562{
7b15fa7a
AM
3563 struct bfd_link_needed_list *look;
3564 for (look = needed; look != stop; look = look->next)
3565 if (strcmp (soname, look->name) == 0
3566 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3567 /* If needed by a library that itself is not directly
3568 needed, recursively check whether that library is
3569 indirectly needed. Since we add DT_NEEDED entries to
3570 the end of the list, library dependencies appear after
3571 the library. Therefore search prior to the current
3572 LOOK, preventing possible infinite recursion. */
3573 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3574 return TRUE;
3575
3576 return FALSE;
3577}
3578
14160578 3579/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3580static int
3581elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3582{
3583 const struct elf_link_hash_entry *h1;
3584 const struct elf_link_hash_entry *h2;
10b7e05b 3585 bfd_signed_vma vdiff;
5a580b3a
AM
3586
3587 h1 = *(const struct elf_link_hash_entry **) arg1;
3588 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3589 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3590 if (vdiff != 0)
3591 return vdiff > 0 ? 1 : -1;
3592 else
3593 {
d3435ae8 3594 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3595 if (sdiff != 0)
3596 return sdiff > 0 ? 1 : -1;
3597 }
14160578
AM
3598 vdiff = h1->size - h2->size;
3599 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3600}
4ad4eba5 3601
5a580b3a
AM
3602/* This function is used to adjust offsets into .dynstr for
3603 dynamic symbols. This is called via elf_link_hash_traverse. */
3604
3605static bfd_boolean
3606elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3607{
a50b1753 3608 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3609
5a580b3a
AM
3610 if (h->dynindx != -1)
3611 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3612 return TRUE;
3613}
3614
3615/* Assign string offsets in .dynstr, update all structures referencing
3616 them. */
3617
4ad4eba5
AM
3618static bfd_boolean
3619elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3620{
3621 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3622 struct elf_link_local_dynamic_entry *entry;
3623 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3624 bfd *dynobj = hash_table->dynobj;
3625 asection *sdyn;
3626 bfd_size_type size;
3627 const struct elf_backend_data *bed;
3628 bfd_byte *extdyn;
3629
3630 _bfd_elf_strtab_finalize (dynstr);
3631 size = _bfd_elf_strtab_size (dynstr);
3632
3633 bed = get_elf_backend_data (dynobj);
3d4d4302 3634 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3635 BFD_ASSERT (sdyn != NULL);
3636
3637 /* Update all .dynamic entries referencing .dynstr strings. */
3638 for (extdyn = sdyn->contents;
eea6121a 3639 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3640 extdyn += bed->s->sizeof_dyn)
3641 {
3642 Elf_Internal_Dyn dyn;
3643
3644 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3645 switch (dyn.d_tag)
3646 {
3647 case DT_STRSZ:
3648 dyn.d_un.d_val = size;
3649 break;
3650 case DT_NEEDED:
3651 case DT_SONAME:
3652 case DT_RPATH:
3653 case DT_RUNPATH:
3654 case DT_FILTER:
3655 case DT_AUXILIARY:
7ee314fa
AM
3656 case DT_AUDIT:
3657 case DT_DEPAUDIT:
5a580b3a
AM
3658 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3659 break;
3660 default:
3661 continue;
3662 }
3663 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3664 }
3665
3666 /* Now update local dynamic symbols. */
3667 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3668 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3669 entry->isym.st_name);
3670
3671 /* And the rest of dynamic symbols. */
3672 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3673
3674 /* Adjust version definitions. */
3675 if (elf_tdata (output_bfd)->cverdefs)
3676 {
3677 asection *s;
3678 bfd_byte *p;
ef53be89 3679 size_t i;
5a580b3a
AM
3680 Elf_Internal_Verdef def;
3681 Elf_Internal_Verdaux defaux;
3682
3d4d4302 3683 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3684 p = s->contents;
3685 do
3686 {
3687 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3688 &def);
3689 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3690 if (def.vd_aux != sizeof (Elf_External_Verdef))
3691 continue;
5a580b3a
AM
3692 for (i = 0; i < def.vd_cnt; ++i)
3693 {
3694 _bfd_elf_swap_verdaux_in (output_bfd,
3695 (Elf_External_Verdaux *) p, &defaux);
3696 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3697 defaux.vda_name);
3698 _bfd_elf_swap_verdaux_out (output_bfd,
3699 &defaux, (Elf_External_Verdaux *) p);
3700 p += sizeof (Elf_External_Verdaux);
3701 }
3702 }
3703 while (def.vd_next);
3704 }
3705
3706 /* Adjust version references. */
3707 if (elf_tdata (output_bfd)->verref)
3708 {
3709 asection *s;
3710 bfd_byte *p;
ef53be89 3711 size_t i;
5a580b3a
AM
3712 Elf_Internal_Verneed need;
3713 Elf_Internal_Vernaux needaux;
3714
3d4d4302 3715 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3716 p = s->contents;
3717 do
3718 {
3719 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3720 &need);
3721 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3722 _bfd_elf_swap_verneed_out (output_bfd, &need,
3723 (Elf_External_Verneed *) p);
3724 p += sizeof (Elf_External_Verneed);
3725 for (i = 0; i < need.vn_cnt; ++i)
3726 {
3727 _bfd_elf_swap_vernaux_in (output_bfd,
3728 (Elf_External_Vernaux *) p, &needaux);
3729 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3730 needaux.vna_name);
3731 _bfd_elf_swap_vernaux_out (output_bfd,
3732 &needaux,
3733 (Elf_External_Vernaux *) p);
3734 p += sizeof (Elf_External_Vernaux);
3735 }
3736 }
3737 while (need.vn_next);
3738 }
3739
3740 return TRUE;
3741}
3742\f
13285a1b
AM
3743/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3744 The default is to only match when the INPUT and OUTPUT are exactly
3745 the same target. */
3746
3747bfd_boolean
3748_bfd_elf_default_relocs_compatible (const bfd_target *input,
3749 const bfd_target *output)
3750{
3751 return input == output;
3752}
3753
3754/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3755 This version is used when different targets for the same architecture
3756 are virtually identical. */
3757
3758bfd_boolean
3759_bfd_elf_relocs_compatible (const bfd_target *input,
3760 const bfd_target *output)
3761{
3762 const struct elf_backend_data *obed, *ibed;
3763
3764 if (input == output)
3765 return TRUE;
3766
3767 ibed = xvec_get_elf_backend_data (input);
3768 obed = xvec_get_elf_backend_data (output);
3769
3770 if (ibed->arch != obed->arch)
3771 return FALSE;
3772
3773 /* If both backends are using this function, deem them compatible. */
3774 return ibed->relocs_compatible == obed->relocs_compatible;
3775}
3776
e5034e59
AM
3777/* Make a special call to the linker "notice" function to tell it that
3778 we are about to handle an as-needed lib, or have finished
1b786873 3779 processing the lib. */
e5034e59
AM
3780
3781bfd_boolean
3782_bfd_elf_notice_as_needed (bfd *ibfd,
3783 struct bfd_link_info *info,
3784 enum notice_asneeded_action act)
3785{
46135103 3786 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3787}
3788
d9689752
L
3789/* Check relocations an ELF object file. */
3790
3791bfd_boolean
3792_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3793{
3794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3795 struct elf_link_hash_table *htab = elf_hash_table (info);
3796
3797 /* If this object is the same format as the output object, and it is
3798 not a shared library, then let the backend look through the
3799 relocs.
3800
3801 This is required to build global offset table entries and to
3802 arrange for dynamic relocs. It is not required for the
3803 particular common case of linking non PIC code, even when linking
3804 against shared libraries, but unfortunately there is no way of
3805 knowing whether an object file has been compiled PIC or not.
3806 Looking through the relocs is not particularly time consuming.
3807 The problem is that we must either (1) keep the relocs in memory,
3808 which causes the linker to require additional runtime memory or
3809 (2) read the relocs twice from the input file, which wastes time.
3810 This would be a good case for using mmap.
3811
3812 I have no idea how to handle linking PIC code into a file of a
3813 different format. It probably can't be done. */
3814 if ((abfd->flags & DYNAMIC) == 0
3815 && is_elf_hash_table (htab)
3816 && bed->check_relocs != NULL
3817 && elf_object_id (abfd) == elf_hash_table_id (htab)
3818 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3819 {
3820 asection *o;
3821
3822 for (o = abfd->sections; o != NULL; o = o->next)
3823 {
3824 Elf_Internal_Rela *internal_relocs;
3825 bfd_boolean ok;
3826
5ce03cea 3827 /* Don't check relocations in excluded sections. */
d9689752 3828 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3829 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3830 || o->reloc_count == 0
3831 || ((info->strip == strip_all || info->strip == strip_debugger)
3832 && (o->flags & SEC_DEBUGGING) != 0)
3833 || bfd_is_abs_section (o->output_section))
3834 continue;
3835
3836 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3837 info->keep_memory);
3838 if (internal_relocs == NULL)
3839 return FALSE;
3840
3841 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3842
3843 if (elf_section_data (o)->relocs != internal_relocs)
3844 free (internal_relocs);
3845
3846 if (! ok)
3847 return FALSE;
3848 }
3849 }
3850
3851 return TRUE;
3852}
3853
4ad4eba5
AM
3854/* Add symbols from an ELF object file to the linker hash table. */
3855
3856static bfd_boolean
3857elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3858{
a0c402a5 3859 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3860 Elf_Internal_Shdr *hdr;
ef53be89
AM
3861 size_t symcount;
3862 size_t extsymcount;
3863 size_t extsymoff;
4ad4eba5
AM
3864 struct elf_link_hash_entry **sym_hash;
3865 bfd_boolean dynamic;
3866 Elf_External_Versym *extversym = NULL;
3867 Elf_External_Versym *ever;
3868 struct elf_link_hash_entry *weaks;
3869 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3870 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3871 Elf_Internal_Sym *isymbuf = NULL;
3872 Elf_Internal_Sym *isym;
3873 Elf_Internal_Sym *isymend;
3874 const struct elf_backend_data *bed;
3875 bfd_boolean add_needed;
66eb6687 3876 struct elf_link_hash_table *htab;
4ad4eba5 3877 bfd_size_type amt;
66eb6687 3878 void *alloc_mark = NULL;
4f87808c
AM
3879 struct bfd_hash_entry **old_table = NULL;
3880 unsigned int old_size = 0;
3881 unsigned int old_count = 0;
66eb6687 3882 void *old_tab = NULL;
66eb6687
AM
3883 void *old_ent;
3884 struct bfd_link_hash_entry *old_undefs = NULL;
3885 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3886 void *old_strtab = NULL;
66eb6687 3887 size_t tabsize = 0;
db6a5d5f 3888 asection *s;
29a9f53e 3889 bfd_boolean just_syms;
4ad4eba5 3890
66eb6687 3891 htab = elf_hash_table (info);
4ad4eba5 3892 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3893
3894 if ((abfd->flags & DYNAMIC) == 0)
3895 dynamic = FALSE;
3896 else
3897 {
3898 dynamic = TRUE;
3899
3900 /* You can't use -r against a dynamic object. Also, there's no
3901 hope of using a dynamic object which does not exactly match
3902 the format of the output file. */
0e1862bb 3903 if (bfd_link_relocatable (info)
66eb6687 3904 || !is_elf_hash_table (htab)
f13a99db 3905 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3906 {
0e1862bb 3907 if (bfd_link_relocatable (info))
9a0789ec
NC
3908 bfd_set_error (bfd_error_invalid_operation);
3909 else
3910 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3911 goto error_return;
3912 }
3913 }
3914
a0c402a5
L
3915 ehdr = elf_elfheader (abfd);
3916 if (info->warn_alternate_em
3917 && bed->elf_machine_code != ehdr->e_machine
3918 && ((bed->elf_machine_alt1 != 0
3919 && ehdr->e_machine == bed->elf_machine_alt1)
3920 || (bed->elf_machine_alt2 != 0
3921 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3922 _bfd_error_handler
695344c0 3923 /* xgettext:c-format */
9793eb77 3924 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3925 ehdr->e_machine, abfd, bed->elf_machine_code);
3926
4ad4eba5
AM
3927 /* As a GNU extension, any input sections which are named
3928 .gnu.warning.SYMBOL are treated as warning symbols for the given
3929 symbol. This differs from .gnu.warning sections, which generate
3930 warnings when they are included in an output file. */
dd98f8d2 3931 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3932 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3933 {
db6a5d5f 3934 const char *name;
4ad4eba5 3935
db6a5d5f
AM
3936 name = bfd_get_section_name (abfd, s);
3937 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3938 {
db6a5d5f
AM
3939 char *msg;
3940 bfd_size_type sz;
3941
3942 name += sizeof ".gnu.warning." - 1;
3943
3944 /* If this is a shared object, then look up the symbol
3945 in the hash table. If it is there, and it is already
3946 been defined, then we will not be using the entry
3947 from this shared object, so we don't need to warn.
3948 FIXME: If we see the definition in a regular object
3949 later on, we will warn, but we shouldn't. The only
3950 fix is to keep track of what warnings we are supposed
3951 to emit, and then handle them all at the end of the
3952 link. */
3953 if (dynamic)
4ad4eba5 3954 {
db6a5d5f
AM
3955 struct elf_link_hash_entry *h;
3956
3957 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3958
3959 /* FIXME: What about bfd_link_hash_common? */
3960 if (h != NULL
3961 && (h->root.type == bfd_link_hash_defined
3962 || h->root.type == bfd_link_hash_defweak))
3963 continue;
3964 }
4ad4eba5 3965
db6a5d5f
AM
3966 sz = s->size;
3967 msg = (char *) bfd_alloc (abfd, sz + 1);
3968 if (msg == NULL)
3969 goto error_return;
4ad4eba5 3970
db6a5d5f
AM
3971 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3972 goto error_return;
4ad4eba5 3973
db6a5d5f 3974 msg[sz] = '\0';
4ad4eba5 3975
db6a5d5f
AM
3976 if (! (_bfd_generic_link_add_one_symbol
3977 (info, abfd, name, BSF_WARNING, s, 0, msg,
3978 FALSE, bed->collect, NULL)))
3979 goto error_return;
4ad4eba5 3980
0e1862bb 3981 if (bfd_link_executable (info))
db6a5d5f
AM
3982 {
3983 /* Clobber the section size so that the warning does
3984 not get copied into the output file. */
3985 s->size = 0;
11d2f718 3986
db6a5d5f
AM
3987 /* Also set SEC_EXCLUDE, so that symbols defined in
3988 the warning section don't get copied to the output. */
3989 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3990 }
3991 }
3992 }
3993
29a9f53e
L
3994 just_syms = ((s = abfd->sections) != NULL
3995 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3996
4ad4eba5
AM
3997 add_needed = TRUE;
3998 if (! dynamic)
3999 {
4000 /* If we are creating a shared library, create all the dynamic
4001 sections immediately. We need to attach them to something,
4002 so we attach them to this BFD, provided it is the right
bf89386a
L
4003 format and is not from ld --just-symbols. Always create the
4004 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4005 are no input BFD's of the same format as the output, we can't
4006 make a shared library. */
4007 if (!just_syms
bf89386a 4008 && (bfd_link_pic (info)
9c1d7a08 4009 || (!bfd_link_relocatable (info)
3c5fce9b 4010 && info->nointerp
9c1d7a08 4011 && (info->export_dynamic || info->dynamic)))
66eb6687 4012 && is_elf_hash_table (htab)
f13a99db 4013 && info->output_bfd->xvec == abfd->xvec
66eb6687 4014 && !htab->dynamic_sections_created)
4ad4eba5
AM
4015 {
4016 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4017 goto error_return;
4018 }
4019 }
66eb6687 4020 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4021 goto error_return;
4022 else
4023 {
4ad4eba5 4024 const char *soname = NULL;
7ee314fa 4025 char *audit = NULL;
4ad4eba5 4026 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4027 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4028 int ret;
4029
4030 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4031 ld shouldn't allow it. */
29a9f53e 4032 if (just_syms)
92fd189d 4033 abort ();
4ad4eba5
AM
4034
4035 /* If this dynamic lib was specified on the command line with
4036 --as-needed in effect, then we don't want to add a DT_NEEDED
4037 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4038 in by another lib's DT_NEEDED. When --no-add-needed is used
4039 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4040 any dynamic library in DT_NEEDED tags in the dynamic lib at
4041 all. */
4042 add_needed = (elf_dyn_lib_class (abfd)
4043 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4044 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4045
4046 s = bfd_get_section_by_name (abfd, ".dynamic");
4047 if (s != NULL)
4048 {
4049 bfd_byte *dynbuf;
4050 bfd_byte *extdyn;
cb33740c 4051 unsigned int elfsec;
4ad4eba5
AM
4052 unsigned long shlink;
4053
eea6121a 4054 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4055 {
4056error_free_dyn:
4057 free (dynbuf);
4058 goto error_return;
4059 }
4ad4eba5
AM
4060
4061 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4062 if (elfsec == SHN_BAD)
4ad4eba5
AM
4063 goto error_free_dyn;
4064 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4065
4066 for (extdyn = dynbuf;
eea6121a 4067 extdyn < dynbuf + s->size;
4ad4eba5
AM
4068 extdyn += bed->s->sizeof_dyn)
4069 {
4070 Elf_Internal_Dyn dyn;
4071
4072 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4073 if (dyn.d_tag == DT_SONAME)
4074 {
4075 unsigned int tagv = dyn.d_un.d_val;
4076 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4077 if (soname == NULL)
4078 goto error_free_dyn;
4079 }
4080 if (dyn.d_tag == DT_NEEDED)
4081 {
4082 struct bfd_link_needed_list *n, **pn;
4083 char *fnm, *anm;
4084 unsigned int tagv = dyn.d_un.d_val;
4085
4086 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4087 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4088 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4089 if (n == NULL || fnm == NULL)
4090 goto error_free_dyn;
4091 amt = strlen (fnm) + 1;
a50b1753 4092 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4093 if (anm == NULL)
4094 goto error_free_dyn;
4095 memcpy (anm, fnm, amt);
4096 n->name = anm;
4097 n->by = abfd;
4098 n->next = NULL;
66eb6687 4099 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4100 ;
4101 *pn = n;
4102 }
4103 if (dyn.d_tag == DT_RUNPATH)
4104 {
4105 struct bfd_link_needed_list *n, **pn;
4106 char *fnm, *anm;
4107 unsigned int tagv = dyn.d_un.d_val;
4108
4109 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4110 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4111 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4112 if (n == NULL || fnm == NULL)
4113 goto error_free_dyn;
4114 amt = strlen (fnm) + 1;
a50b1753 4115 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4116 if (anm == NULL)
4117 goto error_free_dyn;
4118 memcpy (anm, fnm, amt);
4119 n->name = anm;
4120 n->by = abfd;
4121 n->next = NULL;
4122 for (pn = & runpath;
4123 *pn != NULL;
4124 pn = &(*pn)->next)
4125 ;
4126 *pn = n;
4127 }
4128 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4129 if (!runpath && dyn.d_tag == DT_RPATH)
4130 {
4131 struct bfd_link_needed_list *n, **pn;
4132 char *fnm, *anm;
4133 unsigned int tagv = dyn.d_un.d_val;
4134
4135 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4136 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4137 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4138 if (n == NULL || fnm == NULL)
4139 goto error_free_dyn;
4140 amt = strlen (fnm) + 1;
a50b1753 4141 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4142 if (anm == NULL)
f8703194 4143 goto error_free_dyn;
4ad4eba5
AM
4144 memcpy (anm, fnm, amt);
4145 n->name = anm;
4146 n->by = abfd;
4147 n->next = NULL;
4148 for (pn = & rpath;
4149 *pn != NULL;
4150 pn = &(*pn)->next)
4151 ;
4152 *pn = n;
4153 }
7ee314fa
AM
4154 if (dyn.d_tag == DT_AUDIT)
4155 {
4156 unsigned int tagv = dyn.d_un.d_val;
4157 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4158 }
4ad4eba5
AM
4159 }
4160
4161 free (dynbuf);
4162 }
4163
4164 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4165 frees all more recently bfd_alloc'd blocks as well. */
4166 if (runpath)
4167 rpath = runpath;
4168
4169 if (rpath)
4170 {
4171 struct bfd_link_needed_list **pn;
66eb6687 4172 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4173 ;
4174 *pn = rpath;
4175 }
4176
9acc85a6
AM
4177 /* If we have a PT_GNU_RELRO program header, mark as read-only
4178 all sections contained fully therein. This makes relro
4179 shared library sections appear as they will at run-time. */
4180 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4181 while (--phdr >= elf_tdata (abfd)->phdr)
4182 if (phdr->p_type == PT_GNU_RELRO)
4183 {
4184 for (s = abfd->sections; s != NULL; s = s->next)
4185 if ((s->flags & SEC_ALLOC) != 0
4186 && s->vma >= phdr->p_vaddr
4187 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4188 s->flags |= SEC_READONLY;
4189 break;
4190 }
4191
4ad4eba5
AM
4192 /* We do not want to include any of the sections in a dynamic
4193 object in the output file. We hack by simply clobbering the
4194 list of sections in the BFD. This could be handled more
4195 cleanly by, say, a new section flag; the existing
4196 SEC_NEVER_LOAD flag is not the one we want, because that one
4197 still implies that the section takes up space in the output
4198 file. */
4199 bfd_section_list_clear (abfd);
4200
4ad4eba5
AM
4201 /* Find the name to use in a DT_NEEDED entry that refers to this
4202 object. If the object has a DT_SONAME entry, we use it.
4203 Otherwise, if the generic linker stuck something in
4204 elf_dt_name, we use that. Otherwise, we just use the file
4205 name. */
4206 if (soname == NULL || *soname == '\0')
4207 {
4208 soname = elf_dt_name (abfd);
4209 if (soname == NULL || *soname == '\0')
4210 soname = bfd_get_filename (abfd);
4211 }
4212
4213 /* Save the SONAME because sometimes the linker emulation code
4214 will need to know it. */
4215 elf_dt_name (abfd) = soname;
4216
7e9f0867 4217 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4218 if (ret < 0)
4219 goto error_return;
4220
4221 /* If we have already included this dynamic object in the
4222 link, just ignore it. There is no reason to include a
4223 particular dynamic object more than once. */
4224 if (ret > 0)
4225 return TRUE;
7ee314fa
AM
4226
4227 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4228 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4229 }
4230
4231 /* If this is a dynamic object, we always link against the .dynsym
4232 symbol table, not the .symtab symbol table. The dynamic linker
4233 will only see the .dynsym symbol table, so there is no reason to
4234 look at .symtab for a dynamic object. */
4235
4236 if (! dynamic || elf_dynsymtab (abfd) == 0)
4237 hdr = &elf_tdata (abfd)->symtab_hdr;
4238 else
4239 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4240
4241 symcount = hdr->sh_size / bed->s->sizeof_sym;
4242
4243 /* The sh_info field of the symtab header tells us where the
4244 external symbols start. We don't care about the local symbols at
4245 this point. */
4246 if (elf_bad_symtab (abfd))
4247 {
4248 extsymcount = symcount;
4249 extsymoff = 0;
4250 }
4251 else
4252 {
4253 extsymcount = symcount - hdr->sh_info;
4254 extsymoff = hdr->sh_info;
4255 }
4256
f45794cb 4257 sym_hash = elf_sym_hashes (abfd);
012b2306 4258 if (extsymcount != 0)
4ad4eba5
AM
4259 {
4260 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4261 NULL, NULL, NULL);
4262 if (isymbuf == NULL)
4263 goto error_return;
4264
4ad4eba5 4265 if (sym_hash == NULL)
012b2306
AM
4266 {
4267 /* We store a pointer to the hash table entry for each
4268 external symbol. */
ef53be89
AM
4269 amt = extsymcount;
4270 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4271 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4272 if (sym_hash == NULL)
4273 goto error_free_sym;
4274 elf_sym_hashes (abfd) = sym_hash;
4275 }
4ad4eba5
AM
4276 }
4277
4278 if (dynamic)
4279 {
4280 /* Read in any version definitions. */
fc0e6df6
PB
4281 if (!_bfd_elf_slurp_version_tables (abfd,
4282 info->default_imported_symver))
4ad4eba5
AM
4283 goto error_free_sym;
4284
4285 /* Read in the symbol versions, but don't bother to convert them
4286 to internal format. */
4287 if (elf_dynversym (abfd) != 0)
4288 {
4289 Elf_Internal_Shdr *versymhdr;
4290
4291 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4292 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4293 if (extversym == NULL)
4294 goto error_free_sym;
4295 amt = versymhdr->sh_size;
4296 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4297 || bfd_bread (extversym, amt, abfd) != amt)
4298 goto error_free_vers;
4299 }
4300 }
4301
66eb6687
AM
4302 /* If we are loading an as-needed shared lib, save the symbol table
4303 state before we start adding symbols. If the lib turns out
4304 to be unneeded, restore the state. */
4305 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4306 {
4307 unsigned int i;
4308 size_t entsize;
4309
4310 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4311 {
4312 struct bfd_hash_entry *p;
2de92251 4313 struct elf_link_hash_entry *h;
66eb6687
AM
4314
4315 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4316 {
4317 h = (struct elf_link_hash_entry *) p;
4318 entsize += htab->root.table.entsize;
4319 if (h->root.type == bfd_link_hash_warning)
4320 entsize += htab->root.table.entsize;
4321 }
66eb6687
AM
4322 }
4323
4324 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4325 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4326 if (old_tab == NULL)
4327 goto error_free_vers;
4328
4329 /* Remember the current objalloc pointer, so that all mem for
4330 symbols added can later be reclaimed. */
4331 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4332 if (alloc_mark == NULL)
4333 goto error_free_vers;
4334
5061a885
AM
4335 /* Make a special call to the linker "notice" function to
4336 tell it that we are about to handle an as-needed lib. */
e5034e59 4337 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4338 goto error_free_vers;
5061a885 4339
f45794cb
AM
4340 /* Clone the symbol table. Remember some pointers into the
4341 symbol table, and dynamic symbol count. */
4342 old_ent = (char *) old_tab + tabsize;
66eb6687 4343 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4344 old_undefs = htab->root.undefs;
4345 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4346 old_table = htab->root.table.table;
4347 old_size = htab->root.table.size;
4348 old_count = htab->root.table.count;
5b677558
AM
4349 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4350 if (old_strtab == NULL)
4351 goto error_free_vers;
66eb6687
AM
4352
4353 for (i = 0; i < htab->root.table.size; i++)
4354 {
4355 struct bfd_hash_entry *p;
2de92251 4356 struct elf_link_hash_entry *h;
66eb6687
AM
4357
4358 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4359 {
4360 memcpy (old_ent, p, htab->root.table.entsize);
4361 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4362 h = (struct elf_link_hash_entry *) p;
4363 if (h->root.type == bfd_link_hash_warning)
4364 {
4365 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4366 old_ent = (char *) old_ent + htab->root.table.entsize;
4367 }
66eb6687
AM
4368 }
4369 }
4370 }
4ad4eba5 4371
66eb6687 4372 weaks = NULL;
4ad4eba5
AM
4373 ever = extversym != NULL ? extversym + extsymoff : NULL;
4374 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4375 isym < isymend;
4376 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4377 {
4378 int bind;
4379 bfd_vma value;
af44c138 4380 asection *sec, *new_sec;
4ad4eba5
AM
4381 flagword flags;
4382 const char *name;
4383 struct elf_link_hash_entry *h;
90c984fc 4384 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4385 bfd_boolean definition;
4386 bfd_boolean size_change_ok;
4387 bfd_boolean type_change_ok;
37a9e49a
L
4388 bfd_boolean new_weak;
4389 bfd_boolean old_weak;
4ad4eba5 4390 bfd_boolean override;
a4d8e49b 4391 bfd_boolean common;
97196564 4392 bfd_boolean discarded;
4ad4eba5
AM
4393 unsigned int old_alignment;
4394 bfd *old_bfd;
6e33951e 4395 bfd_boolean matched;
4ad4eba5
AM
4396
4397 override = FALSE;
4398
4399 flags = BSF_NO_FLAGS;
4400 sec = NULL;
4401 value = isym->st_value;
a4d8e49b 4402 common = bed->common_definition (isym);
2980ccad
L
4403 if (common && info->inhibit_common_definition)
4404 {
4405 /* Treat common symbol as undefined for --no-define-common. */
4406 isym->st_shndx = SHN_UNDEF;
4407 common = FALSE;
4408 }
97196564 4409 discarded = FALSE;
4ad4eba5
AM
4410
4411 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4412 switch (bind)
4ad4eba5 4413 {
3e7a7d11 4414 case STB_LOCAL:
4ad4eba5
AM
4415 /* This should be impossible, since ELF requires that all
4416 global symbols follow all local symbols, and that sh_info
4417 point to the first global symbol. Unfortunately, Irix 5
4418 screws this up. */
4419 continue;
3e7a7d11
NC
4420
4421 case STB_GLOBAL:
a4d8e49b 4422 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4423 flags = BSF_GLOBAL;
3e7a7d11
NC
4424 break;
4425
4426 case STB_WEAK:
4427 flags = BSF_WEAK;
4428 break;
4429
4430 case STB_GNU_UNIQUE:
4431 flags = BSF_GNU_UNIQUE;
4432 break;
4433
4434 default:
4ad4eba5 4435 /* Leave it up to the processor backend. */
3e7a7d11 4436 break;
4ad4eba5
AM
4437 }
4438
4439 if (isym->st_shndx == SHN_UNDEF)
4440 sec = bfd_und_section_ptr;
cb33740c
AM
4441 else if (isym->st_shndx == SHN_ABS)
4442 sec = bfd_abs_section_ptr;
4443 else if (isym->st_shndx == SHN_COMMON)
4444 {
4445 sec = bfd_com_section_ptr;
4446 /* What ELF calls the size we call the value. What ELF
4447 calls the value we call the alignment. */
4448 value = isym->st_size;
4449 }
4450 else
4ad4eba5
AM
4451 {
4452 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4453 if (sec == NULL)
4454 sec = bfd_abs_section_ptr;
dbaa2011 4455 else if (discarded_section (sec))
529fcb95 4456 {
e5d08002
L
4457 /* Symbols from discarded section are undefined. We keep
4458 its visibility. */
529fcb95 4459 sec = bfd_und_section_ptr;
97196564 4460 discarded = TRUE;
529fcb95
PB
4461 isym->st_shndx = SHN_UNDEF;
4462 }
4ad4eba5
AM
4463 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4464 value -= sec->vma;
4465 }
4ad4eba5
AM
4466
4467 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4468 isym->st_name);
4469 if (name == NULL)
4470 goto error_free_vers;
4471
4472 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4473 && (abfd->flags & BFD_PLUGIN) != 0)
4474 {
4475 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4476
4477 if (xc == NULL)
4478 {
4479 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4480 | SEC_EXCLUDE);
4481 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4482 if (xc == NULL)
4483 goto error_free_vers;
4484 }
4485 sec = xc;
4486 }
4487 else if (isym->st_shndx == SHN_COMMON
4488 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4489 && !bfd_link_relocatable (info))
4ad4eba5
AM
4490 {
4491 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4492
4493 if (tcomm == NULL)
4494 {
02d00247
AM
4495 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4496 | SEC_LINKER_CREATED);
4497 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4498 if (tcomm == NULL)
4ad4eba5
AM
4499 goto error_free_vers;
4500 }
4501 sec = tcomm;
4502 }
66eb6687 4503 else if (bed->elf_add_symbol_hook)
4ad4eba5 4504 {
66eb6687
AM
4505 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4506 &sec, &value))
4ad4eba5
AM
4507 goto error_free_vers;
4508
4509 /* The hook function sets the name to NULL if this symbol
4510 should be skipped for some reason. */
4511 if (name == NULL)
4512 continue;
4513 }
4514
4515 /* Sanity check that all possibilities were handled. */
4516 if (sec == NULL)
4517 {
4518 bfd_set_error (bfd_error_bad_value);
4519 goto error_free_vers;
4520 }
4521
191c0c42
AM
4522 /* Silently discard TLS symbols from --just-syms. There's
4523 no way to combine a static TLS block with a new TLS block
4524 for this executable. */
4525 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4526 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4527 continue;
4528
4ad4eba5
AM
4529 if (bfd_is_und_section (sec)
4530 || bfd_is_com_section (sec))
4531 definition = FALSE;
4532 else
4533 definition = TRUE;
4534
4535 size_change_ok = FALSE;
66eb6687 4536 type_change_ok = bed->type_change_ok;
37a9e49a 4537 old_weak = FALSE;
6e33951e 4538 matched = FALSE;
4ad4eba5
AM
4539 old_alignment = 0;
4540 old_bfd = NULL;
af44c138 4541 new_sec = sec;
4ad4eba5 4542
66eb6687 4543 if (is_elf_hash_table (htab))
4ad4eba5
AM
4544 {
4545 Elf_Internal_Versym iver;
4546 unsigned int vernum = 0;
4547 bfd_boolean skip;
4548
fc0e6df6 4549 if (ever == NULL)
4ad4eba5 4550 {
fc0e6df6
PB
4551 if (info->default_imported_symver)
4552 /* Use the default symbol version created earlier. */
4553 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4554 else
4555 iver.vs_vers = 0;
4556 }
4557 else
4558 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4559
4560 vernum = iver.vs_vers & VERSYM_VERSION;
4561
4562 /* If this is a hidden symbol, or if it is not version
4563 1, we append the version name to the symbol name.
cc86ff91
EB
4564 However, we do not modify a non-hidden absolute symbol
4565 if it is not a function, because it might be the version
4566 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4567 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4568 || (vernum > 1
4569 && (!bfd_is_abs_section (sec)
4570 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4571 {
4572 const char *verstr;
4573 size_t namelen, verlen, newlen;
4574 char *newname, *p;
4575
4576 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4577 {
fc0e6df6
PB
4578 if (vernum > elf_tdata (abfd)->cverdefs)
4579 verstr = NULL;
4580 else if (vernum > 1)
4581 verstr =
4582 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4583 else
4584 verstr = "";
4ad4eba5 4585
fc0e6df6 4586 if (verstr == NULL)
4ad4eba5 4587 {
4eca0228 4588 _bfd_error_handler
695344c0 4589 /* xgettext:c-format */
871b3ab2 4590 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4591 abfd, name, vernum,
4592 elf_tdata (abfd)->cverdefs);
4593 bfd_set_error (bfd_error_bad_value);
4594 goto error_free_vers;
4ad4eba5 4595 }
fc0e6df6
PB
4596 }
4597 else
4598 {
4599 /* We cannot simply test for the number of
4600 entries in the VERNEED section since the
4601 numbers for the needed versions do not start
4602 at 0. */
4603 Elf_Internal_Verneed *t;
4604
4605 verstr = NULL;
4606 for (t = elf_tdata (abfd)->verref;
4607 t != NULL;
4608 t = t->vn_nextref)
4ad4eba5 4609 {
fc0e6df6 4610 Elf_Internal_Vernaux *a;
4ad4eba5 4611
fc0e6df6
PB
4612 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4613 {
4614 if (a->vna_other == vernum)
4ad4eba5 4615 {
fc0e6df6
PB
4616 verstr = a->vna_nodename;
4617 break;
4ad4eba5 4618 }
4ad4eba5 4619 }
fc0e6df6
PB
4620 if (a != NULL)
4621 break;
4622 }
4623 if (verstr == NULL)
4624 {
4eca0228 4625 _bfd_error_handler
695344c0 4626 /* xgettext:c-format */
871b3ab2 4627 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4628 abfd, name, vernum);
4629 bfd_set_error (bfd_error_bad_value);
4630 goto error_free_vers;
4ad4eba5 4631 }
4ad4eba5 4632 }
fc0e6df6
PB
4633
4634 namelen = strlen (name);
4635 verlen = strlen (verstr);
4636 newlen = namelen + verlen + 2;
4637 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4638 && isym->st_shndx != SHN_UNDEF)
4639 ++newlen;
4640
a50b1753 4641 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4642 if (newname == NULL)
4643 goto error_free_vers;
4644 memcpy (newname, name, namelen);
4645 p = newname + namelen;
4646 *p++ = ELF_VER_CHR;
4647 /* If this is a defined non-hidden version symbol,
4648 we add another @ to the name. This indicates the
4649 default version of the symbol. */
4650 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4651 && isym->st_shndx != SHN_UNDEF)
4652 *p++ = ELF_VER_CHR;
4653 memcpy (p, verstr, verlen + 1);
4654
4655 name = newname;
4ad4eba5
AM
4656 }
4657
cd3416da
AM
4658 /* If this symbol has default visibility and the user has
4659 requested we not re-export it, then mark it as hidden. */
a0d49154 4660 if (!bfd_is_und_section (sec)
cd3416da 4661 && !dynamic
ce875075 4662 && abfd->no_export
cd3416da
AM
4663 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4664 isym->st_other = (STV_HIDDEN
4665 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4666
4f3fedcf
AM
4667 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4668 sym_hash, &old_bfd, &old_weak,
4669 &old_alignment, &skip, &override,
6e33951e
L
4670 &type_change_ok, &size_change_ok,
4671 &matched))
4ad4eba5
AM
4672 goto error_free_vers;
4673
4674 if (skip)
4675 continue;
4676
6e33951e
L
4677 /* Override a definition only if the new symbol matches the
4678 existing one. */
4679 if (override && matched)
4ad4eba5
AM
4680 definition = FALSE;
4681
4682 h = *sym_hash;
4683 while (h->root.type == bfd_link_hash_indirect
4684 || h->root.type == bfd_link_hash_warning)
4685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4686
4ad4eba5 4687 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4688 && vernum > 1
4689 && definition)
4690 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4691 }
4692
4693 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4694 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4695 (struct bfd_link_hash_entry **) sym_hash)))
4696 goto error_free_vers;
4697
ac98f9e2
L
4698 if ((abfd->flags & DYNAMIC) == 0
4699 && (bfd_get_flavour (info->output_bfd)
4700 == bfd_target_elf_flavour))
4701 {
4702 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4703 elf_tdata (info->output_bfd)->has_gnu_symbols
4704 |= elf_gnu_symbol_ifunc;
4705 if ((flags & BSF_GNU_UNIQUE))
4706 elf_tdata (info->output_bfd)->has_gnu_symbols
4707 |= elf_gnu_symbol_unique;
4708 }
a43942db 4709
4ad4eba5 4710 h = *sym_hash;
90c984fc
L
4711 /* We need to make sure that indirect symbol dynamic flags are
4712 updated. */
4713 hi = h;
4ad4eba5
AM
4714 while (h->root.type == bfd_link_hash_indirect
4715 || h->root.type == bfd_link_hash_warning)
4716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4717
97196564
L
4718 /* Setting the index to -3 tells elf_link_output_extsym that
4719 this symbol is defined in a discarded section. */
4720 if (discarded)
4721 h->indx = -3;
4722
4ad4eba5
AM
4723 *sym_hash = h;
4724
37a9e49a 4725 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4726 if (dynamic
4727 && definition
37a9e49a 4728 && new_weak
fcb93ecf 4729 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4730 && is_elf_hash_table (htab)
60d67dc8 4731 && h->u.alias == NULL)
4ad4eba5
AM
4732 {
4733 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4734 a dynamic object, using the alias field. Later in this
4735 function we will set the alias field to the correct
4ad4eba5
AM
4736 value. We only put non-function symbols from dynamic
4737 objects on this list, because that happens to be the only
4738 time we need to know the normal symbol corresponding to a
4739 weak symbol, and the information is time consuming to
60d67dc8 4740 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4741 then this symbol was already defined by some previous
4742 dynamic object, and we will be using that previous
4743 definition anyhow. */
4744
60d67dc8 4745 h->u.alias = weaks;
4ad4eba5 4746 weaks = h;
4ad4eba5
AM
4747 }
4748
4749 /* Set the alignment of a common symbol. */
a4d8e49b 4750 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4751 && h->root.type == bfd_link_hash_common)
4752 {
4753 unsigned int align;
4754
a4d8e49b 4755 if (common)
af44c138
L
4756 align = bfd_log2 (isym->st_value);
4757 else
4758 {
4759 /* The new symbol is a common symbol in a shared object.
4760 We need to get the alignment from the section. */
4761 align = new_sec->alignment_power;
4762 }
595213d4 4763 if (align > old_alignment)
4ad4eba5
AM
4764 h->root.u.c.p->alignment_power = align;
4765 else
4766 h->root.u.c.p->alignment_power = old_alignment;
4767 }
4768
66eb6687 4769 if (is_elf_hash_table (htab))
4ad4eba5 4770 {
4f3fedcf
AM
4771 /* Set a flag in the hash table entry indicating the type of
4772 reference or definition we just found. A dynamic symbol
4773 is one which is referenced or defined by both a regular
4774 object and a shared object. */
4775 bfd_boolean dynsym = FALSE;
4776
4777 /* Plugin symbols aren't normal. Don't set def_regular or
4778 ref_regular for them, or make them dynamic. */
4779 if ((abfd->flags & BFD_PLUGIN) != 0)
4780 ;
4781 else if (! dynamic)
4782 {
4783 if (! definition)
4784 {
4785 h->ref_regular = 1;
4786 if (bind != STB_WEAK)
4787 h->ref_regular_nonweak = 1;
4788 }
4789 else
4790 {
4791 h->def_regular = 1;
4792 if (h->def_dynamic)
4793 {
4794 h->def_dynamic = 0;
4795 h->ref_dynamic = 1;
4796 }
4797 }
4798
4799 /* If the indirect symbol has been forced local, don't
4800 make the real symbol dynamic. */
4801 if ((h == hi || !hi->forced_local)
0e1862bb 4802 && (bfd_link_dll (info)
4f3fedcf
AM
4803 || h->def_dynamic
4804 || h->ref_dynamic))
4805 dynsym = TRUE;
4806 }
4807 else
4808 {
4809 if (! definition)
4810 {
4811 h->ref_dynamic = 1;
4812 hi->ref_dynamic = 1;
4813 }
4814 else
4815 {
4816 h->def_dynamic = 1;
4817 hi->def_dynamic = 1;
4818 }
4819
4820 /* If the indirect symbol has been forced local, don't
4821 make the real symbol dynamic. */
4822 if ((h == hi || !hi->forced_local)
4823 && (h->def_regular
4824 || h->ref_regular
60d67dc8
AM
4825 || (h->is_weakalias
4826 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4827 dynsym = TRUE;
4828 }
4829
4830 /* Check to see if we need to add an indirect symbol for
4831 the default name. */
4832 if (definition
4833 || (!override && h->root.type == bfd_link_hash_common))
4834 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4835 sec, value, &old_bfd, &dynsym))
4836 goto error_free_vers;
4ad4eba5
AM
4837
4838 /* Check the alignment when a common symbol is involved. This
4839 can change when a common symbol is overridden by a normal
4840 definition or a common symbol is ignored due to the old
4841 normal definition. We need to make sure the maximum
4842 alignment is maintained. */
a4d8e49b 4843 if ((old_alignment || common)
4ad4eba5
AM
4844 && h->root.type != bfd_link_hash_common)
4845 {
4846 unsigned int common_align;
4847 unsigned int normal_align;
4848 unsigned int symbol_align;
4849 bfd *normal_bfd;
4850 bfd *common_bfd;
4851
3a81e825
AM
4852 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4853 || h->root.type == bfd_link_hash_defweak);
4854
4ad4eba5
AM
4855 symbol_align = ffs (h->root.u.def.value) - 1;
4856 if (h->root.u.def.section->owner != NULL
0616a280
AM
4857 && (h->root.u.def.section->owner->flags
4858 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4859 {
4860 normal_align = h->root.u.def.section->alignment_power;
4861 if (normal_align > symbol_align)
4862 normal_align = symbol_align;
4863 }
4864 else
4865 normal_align = symbol_align;
4866
4867 if (old_alignment)
4868 {
4869 common_align = old_alignment;
4870 common_bfd = old_bfd;
4871 normal_bfd = abfd;
4872 }
4873 else
4874 {
4875 common_align = bfd_log2 (isym->st_value);
4876 common_bfd = abfd;
4877 normal_bfd = old_bfd;
4878 }
4879
4880 if (normal_align < common_align)
d07676f8
NC
4881 {
4882 /* PR binutils/2735 */
4883 if (normal_bfd == NULL)
4eca0228 4884 _bfd_error_handler
695344c0 4885 /* xgettext:c-format */
9793eb77 4886 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4887 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4888 1 << common_align, name, common_bfd,
4889 1 << normal_align, h->root.u.def.section);
d07676f8 4890 else
4eca0228 4891 _bfd_error_handler
695344c0 4892 /* xgettext:c-format */
9793eb77 4893 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4894 " is smaller than %u in %pB"),
c08bb8dd
AM
4895 1 << normal_align, name, normal_bfd,
4896 1 << common_align, common_bfd);
d07676f8 4897 }
4ad4eba5
AM
4898 }
4899
83ad0046 4900 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4901 if (isym->st_size != 0
4902 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4903 && (definition || h->size == 0))
4904 {
83ad0046
L
4905 if (h->size != 0
4906 && h->size != isym->st_size
4907 && ! size_change_ok)
4eca0228 4908 _bfd_error_handler
695344c0 4909 /* xgettext:c-format */
9793eb77 4910 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4911 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4912 name, (uint64_t) h->size, old_bfd,
4913 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4914
4915 h->size = isym->st_size;
4916 }
4917
4918 /* If this is a common symbol, then we always want H->SIZE
4919 to be the size of the common symbol. The code just above
4920 won't fix the size if a common symbol becomes larger. We
4921 don't warn about a size change here, because that is
4f3fedcf 4922 covered by --warn-common. Allow changes between different
fcb93ecf 4923 function types. */
4ad4eba5
AM
4924 if (h->root.type == bfd_link_hash_common)
4925 h->size = h->root.u.c.size;
4926
4927 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4928 && ((definition && !new_weak)
4929 || (old_weak && h->root.type == bfd_link_hash_common)
4930 || h->type == STT_NOTYPE))
4ad4eba5 4931 {
2955ec4c
L
4932 unsigned int type = ELF_ST_TYPE (isym->st_info);
4933
4934 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4935 symbol. */
4936 if (type == STT_GNU_IFUNC
4937 && (abfd->flags & DYNAMIC) != 0)
4938 type = STT_FUNC;
4ad4eba5 4939
2955ec4c
L
4940 if (h->type != type)
4941 {
4942 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4943 /* xgettext:c-format */
4eca0228 4944 _bfd_error_handler
9793eb77 4945 (_("warning: type of symbol `%s' changed"
871b3ab2 4946 " from %d to %d in %pB"),
c08bb8dd 4947 name, h->type, type, abfd);
2955ec4c
L
4948
4949 h->type = type;
4950 }
4ad4eba5
AM
4951 }
4952
54ac0771 4953 /* Merge st_other field. */
b8417128 4954 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4955
c3df8c14 4956 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4957 if (definition
4958 && (sec->flags & SEC_DEBUGGING)
4959 && !bfd_link_relocatable (info))
c3df8c14
AM
4960 dynsym = FALSE;
4961
4f3fedcf
AM
4962 /* Nor should we make plugin symbols dynamic. */
4963 if ((abfd->flags & BFD_PLUGIN) != 0)
4964 dynsym = FALSE;
4965
35fc36a8 4966 if (definition)
35399224
L
4967 {
4968 h->target_internal = isym->st_target_internal;
4969 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4970 }
35fc36a8 4971
4ad4eba5
AM
4972 if (definition && !dynamic)
4973 {
4974 char *p = strchr (name, ELF_VER_CHR);
4975 if (p != NULL && p[1] != ELF_VER_CHR)
4976 {
4977 /* Queue non-default versions so that .symver x, x@FOO
4978 aliases can be checked. */
66eb6687 4979 if (!nondeflt_vers)
4ad4eba5 4980 {
66eb6687
AM
4981 amt = ((isymend - isym + 1)
4982 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4983 nondeflt_vers
4984 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4985 if (!nondeflt_vers)
4986 goto error_free_vers;
4ad4eba5 4987 }
66eb6687 4988 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4989 }
4990 }
4991
4992 if (dynsym && h->dynindx == -1)
4993 {
c152c796 4994 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4995 goto error_free_vers;
60d67dc8
AM
4996 if (h->is_weakalias
4997 && weakdef (h)->dynindx == -1)
4ad4eba5 4998 {
60d67dc8 4999 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5000 goto error_free_vers;
5001 }
5002 }
1f599d0e 5003 else if (h->dynindx != -1)
4ad4eba5
AM
5004 /* If the symbol already has a dynamic index, but
5005 visibility says it should not be visible, turn it into
5006 a local symbol. */
5007 switch (ELF_ST_VISIBILITY (h->other))
5008 {
5009 case STV_INTERNAL:
5010 case STV_HIDDEN:
5011 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5012 dynsym = FALSE;
5013 break;
5014 }
5015
aef28989
L
5016 /* Don't add DT_NEEDED for references from the dummy bfd nor
5017 for unmatched symbol. */
4ad4eba5 5018 if (!add_needed
aef28989 5019 && matched
4ad4eba5 5020 && definition
010e5ae2 5021 && ((dynsym
ffa9430d 5022 && h->ref_regular_nonweak
4f3fedcf
AM
5023 && (old_bfd == NULL
5024 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5025 || (h->ref_dynamic_nonweak
010e5ae2 5026 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5027 && !on_needed_list (elf_dt_name (abfd),
5028 htab->needed, NULL))))
4ad4eba5
AM
5029 {
5030 int ret;
5031 const char *soname = elf_dt_name (abfd);
5032
16e4ecc0
AM
5033 info->callbacks->minfo ("%!", soname, old_bfd,
5034 h->root.root.string);
5035
4ad4eba5
AM
5036 /* A symbol from a library loaded via DT_NEEDED of some
5037 other library is referenced by a regular object.
e56f61be 5038 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5039 --no-add-needed is used and the reference was not
5040 a weak one. */
4f3fedcf 5041 if (old_bfd != NULL
b918acf9 5042 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5043 {
4eca0228 5044 _bfd_error_handler
695344c0 5045 /* xgettext:c-format */
871b3ab2 5046 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5047 old_bfd, name);
ff5ac77b 5048 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5049 goto error_free_vers;
5050 }
5051
a50b1753 5052 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5053 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5054
4ad4eba5 5055 add_needed = TRUE;
7e9f0867 5056 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5057 if (ret < 0)
5058 goto error_free_vers;
5059
5060 BFD_ASSERT (ret == 0);
5061 }
5062 }
5063 }
5064
a83ef4d1
L
5065 if (info->lto_plugin_active
5066 && !bfd_link_relocatable (info)
5067 && (abfd->flags & BFD_PLUGIN) == 0
5068 && !just_syms
5069 && extsymcount)
5070 {
5071 int r_sym_shift;
5072
5073 if (bed->s->arch_size == 32)
5074 r_sym_shift = 8;
5075 else
5076 r_sym_shift = 32;
5077
5078 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5079 referenced in regular objects so that linker plugin will get
5080 the correct symbol resolution. */
5081
5082 sym_hash = elf_sym_hashes (abfd);
5083 for (s = abfd->sections; s != NULL; s = s->next)
5084 {
5085 Elf_Internal_Rela *internal_relocs;
5086 Elf_Internal_Rela *rel, *relend;
5087
5088 /* Don't check relocations in excluded sections. */
5089 if ((s->flags & SEC_RELOC) == 0
5090 || s->reloc_count == 0
5091 || (s->flags & SEC_EXCLUDE) != 0
5092 || ((info->strip == strip_all
5093 || info->strip == strip_debugger)
5094 && (s->flags & SEC_DEBUGGING) != 0))
5095 continue;
5096
5097 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5098 NULL,
5099 info->keep_memory);
5100 if (internal_relocs == NULL)
5101 goto error_free_vers;
5102
5103 rel = internal_relocs;
5104 relend = rel + s->reloc_count;
5105 for ( ; rel < relend; rel++)
5106 {
5107 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5108 struct elf_link_hash_entry *h;
5109
5110 /* Skip local symbols. */
5111 if (r_symndx < extsymoff)
5112 continue;
5113
5114 h = sym_hash[r_symndx - extsymoff];
5115 if (h != NULL)
5116 h->root.non_ir_ref_regular = 1;
5117 }
5118
5119 if (elf_section_data (s)->relocs != internal_relocs)
5120 free (internal_relocs);
5121 }
5122 }
5123
66eb6687
AM
5124 if (extversym != NULL)
5125 {
5126 free (extversym);
5127 extversym = NULL;
5128 }
5129
5130 if (isymbuf != NULL)
5131 {
5132 free (isymbuf);
5133 isymbuf = NULL;
5134 }
5135
5136 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5137 {
5138 unsigned int i;
5139
5140 /* Restore the symbol table. */
f45794cb
AM
5141 old_ent = (char *) old_tab + tabsize;
5142 memset (elf_sym_hashes (abfd), 0,
5143 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5144 htab->root.table.table = old_table;
5145 htab->root.table.size = old_size;
5146 htab->root.table.count = old_count;
66eb6687 5147 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5148 htab->root.undefs = old_undefs;
5149 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5150 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5151 free (old_strtab);
5152 old_strtab = NULL;
66eb6687
AM
5153 for (i = 0; i < htab->root.table.size; i++)
5154 {
5155 struct bfd_hash_entry *p;
5156 struct elf_link_hash_entry *h;
3e0882af
L
5157 bfd_size_type size;
5158 unsigned int alignment_power;
4070765b 5159 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5160
5161 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5162 {
5163 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5164 if (h->root.type == bfd_link_hash_warning)
5165 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5166
3e0882af
L
5167 /* Preserve the maximum alignment and size for common
5168 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5169 since it can still be loaded at run time by another
3e0882af
L
5170 dynamic lib. */
5171 if (h->root.type == bfd_link_hash_common)
5172 {
5173 size = h->root.u.c.size;
5174 alignment_power = h->root.u.c.p->alignment_power;
5175 }
5176 else
5177 {
5178 size = 0;
5179 alignment_power = 0;
5180 }
4070765b 5181 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5182 will be exported when the dynamic lib becomes needed
5183 in the second pass. */
4070765b 5184 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5185 memcpy (p, old_ent, htab->root.table.entsize);
5186 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5187 h = (struct elf_link_hash_entry *) p;
5188 if (h->root.type == bfd_link_hash_warning)
5189 {
5190 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5191 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5192 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5193 }
a4542f1b 5194 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5195 {
5196 if (size > h->root.u.c.size)
5197 h->root.u.c.size = size;
5198 if (alignment_power > h->root.u.c.p->alignment_power)
5199 h->root.u.c.p->alignment_power = alignment_power;
5200 }
4070765b 5201 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5202 }
5203 }
5204
5061a885
AM
5205 /* Make a special call to the linker "notice" function to
5206 tell it that symbols added for crefs may need to be removed. */
e5034e59 5207 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5208 goto error_free_vers;
5061a885 5209
66eb6687
AM
5210 free (old_tab);
5211 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5212 alloc_mark);
5213 if (nondeflt_vers != NULL)
5214 free (nondeflt_vers);
5215 return TRUE;
5216 }
2de92251 5217
66eb6687
AM
5218 if (old_tab != NULL)
5219 {
e5034e59 5220 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5221 goto error_free_vers;
66eb6687
AM
5222 free (old_tab);
5223 old_tab = NULL;
5224 }
5225
c6e8a9a8
L
5226 /* Now that all the symbols from this input file are created, if
5227 not performing a relocatable link, handle .symver foo, foo@BAR
5228 such that any relocs against foo become foo@BAR. */
0e1862bb 5229 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5230 {
ef53be89 5231 size_t cnt, symidx;
4ad4eba5
AM
5232
5233 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5234 {
5235 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5236 char *shortname, *p;
5237
5238 p = strchr (h->root.root.string, ELF_VER_CHR);
5239 if (p == NULL
5240 || (h->root.type != bfd_link_hash_defined
5241 && h->root.type != bfd_link_hash_defweak))
5242 continue;
5243
5244 amt = p - h->root.root.string;
a50b1753 5245 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5246 if (!shortname)
5247 goto error_free_vers;
4ad4eba5
AM
5248 memcpy (shortname, h->root.root.string, amt);
5249 shortname[amt] = '\0';
5250
5251 hi = (struct elf_link_hash_entry *)
66eb6687 5252 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5253 FALSE, FALSE, FALSE);
5254 if (hi != NULL
5255 && hi->root.type == h->root.type
5256 && hi->root.u.def.value == h->root.u.def.value
5257 && hi->root.u.def.section == h->root.u.def.section)
5258 {
5259 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5260 hi->root.type = bfd_link_hash_indirect;
5261 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5262 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5263 sym_hash = elf_sym_hashes (abfd);
5264 if (sym_hash)
5265 for (symidx = 0; symidx < extsymcount; ++symidx)
5266 if (sym_hash[symidx] == hi)
5267 {
5268 sym_hash[symidx] = h;
5269 break;
5270 }
5271 }
5272 free (shortname);
5273 }
5274 free (nondeflt_vers);
5275 nondeflt_vers = NULL;
5276 }
5277
60d67dc8 5278 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5279 symbols we found. The only way to do this is to search all the
5280 symbols. Since we only need the information for non functions in
5281 dynamic objects, that's the only time we actually put anything on
5282 the list WEAKS. We need this information so that if a regular
5283 object refers to a symbol defined weakly in a dynamic object, the
5284 real symbol in the dynamic object is also put in the dynamic
5285 symbols; we also must arrange for both symbols to point to the
5286 same memory location. We could handle the general case of symbol
5287 aliasing, but a general symbol alias can only be generated in
5288 assembler code, handling it correctly would be very time
5289 consuming, and other ELF linkers don't handle general aliasing
5290 either. */
5291 if (weaks != NULL)
5292 {
5293 struct elf_link_hash_entry **hpp;
5294 struct elf_link_hash_entry **hppend;
5295 struct elf_link_hash_entry **sorted_sym_hash;
5296 struct elf_link_hash_entry *h;
5297 size_t sym_count;
5298
5299 /* Since we have to search the whole symbol list for each weak
5300 defined symbol, search time for N weak defined symbols will be
5301 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5302 amt = extsymcount;
5303 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5304 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5305 if (sorted_sym_hash == NULL)
5306 goto error_return;
5307 sym_hash = sorted_sym_hash;
5308 hpp = elf_sym_hashes (abfd);
5309 hppend = hpp + extsymcount;
5310 sym_count = 0;
5311 for (; hpp < hppend; hpp++)
5312 {
5313 h = *hpp;
5314 if (h != NULL
5315 && h->root.type == bfd_link_hash_defined
fcb93ecf 5316 && !bed->is_function_type (h->type))
4ad4eba5
AM
5317 {
5318 *sym_hash = h;
5319 sym_hash++;
5320 sym_count++;
5321 }
5322 }
5323
5324 qsort (sorted_sym_hash, sym_count,
5325 sizeof (struct elf_link_hash_entry *),
5326 elf_sort_symbol);
5327
5328 while (weaks != NULL)
5329 {
5330 struct elf_link_hash_entry *hlook;
5331 asection *slook;
5332 bfd_vma vlook;
ed54588d 5333 size_t i, j, idx = 0;
4ad4eba5
AM
5334
5335 hlook = weaks;
60d67dc8
AM
5336 weaks = hlook->u.alias;
5337 hlook->u.alias = NULL;
4ad4eba5 5338
e3e53eed
AM
5339 if (hlook->root.type != bfd_link_hash_defined
5340 && hlook->root.type != bfd_link_hash_defweak)
5341 continue;
5342
4ad4eba5
AM
5343 slook = hlook->root.u.def.section;
5344 vlook = hlook->root.u.def.value;
5345
4ad4eba5
AM
5346 i = 0;
5347 j = sym_count;
14160578 5348 while (i != j)
4ad4eba5
AM
5349 {
5350 bfd_signed_vma vdiff;
5351 idx = (i + j) / 2;
14160578 5352 h = sorted_sym_hash[idx];
4ad4eba5
AM
5353 vdiff = vlook - h->root.u.def.value;
5354 if (vdiff < 0)
5355 j = idx;
5356 else if (vdiff > 0)
5357 i = idx + 1;
5358 else
5359 {
d3435ae8 5360 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5361 if (sdiff < 0)
5362 j = idx;
5363 else if (sdiff > 0)
5364 i = idx + 1;
5365 else
14160578 5366 break;
4ad4eba5
AM
5367 }
5368 }
5369
5370 /* We didn't find a value/section match. */
14160578 5371 if (i == j)
4ad4eba5
AM
5372 continue;
5373
14160578
AM
5374 /* With multiple aliases, or when the weak symbol is already
5375 strongly defined, we have multiple matching symbols and
5376 the binary search above may land on any of them. Step
5377 one past the matching symbol(s). */
5378 while (++idx != j)
5379 {
5380 h = sorted_sym_hash[idx];
5381 if (h->root.u.def.section != slook
5382 || h->root.u.def.value != vlook)
5383 break;
5384 }
5385
5386 /* Now look back over the aliases. Since we sorted by size
5387 as well as value and section, we'll choose the one with
5388 the largest size. */
5389 while (idx-- != i)
4ad4eba5 5390 {
14160578 5391 h = sorted_sym_hash[idx];
4ad4eba5
AM
5392
5393 /* Stop if value or section doesn't match. */
14160578
AM
5394 if (h->root.u.def.section != slook
5395 || h->root.u.def.value != vlook)
4ad4eba5
AM
5396 break;
5397 else if (h != hlook)
5398 {
60d67dc8
AM
5399 struct elf_link_hash_entry *t;
5400
5401 hlook->u.alias = h;
5402 hlook->is_weakalias = 1;
5403 t = h;
5404 if (t->u.alias != NULL)
5405 while (t->u.alias != h)
5406 t = t->u.alias;
5407 t->u.alias = hlook;
4ad4eba5
AM
5408
5409 /* If the weak definition is in the list of dynamic
5410 symbols, make sure the real definition is put
5411 there as well. */
5412 if (hlook->dynindx != -1 && h->dynindx == -1)
5413 {
c152c796 5414 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5415 {
5416 err_free_sym_hash:
5417 free (sorted_sym_hash);
5418 goto error_return;
5419 }
4ad4eba5
AM
5420 }
5421
5422 /* If the real definition is in the list of dynamic
5423 symbols, make sure the weak definition is put
5424 there as well. If we don't do this, then the
5425 dynamic loader might not merge the entries for the
5426 real definition and the weak definition. */
5427 if (h->dynindx != -1 && hlook->dynindx == -1)
5428 {
c152c796 5429 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5430 goto err_free_sym_hash;
4ad4eba5
AM
5431 }
5432 break;
5433 }
5434 }
5435 }
5436
5437 free (sorted_sym_hash);
5438 }
5439
33177bb1
AM
5440 if (bed->check_directives
5441 && !(*bed->check_directives) (abfd, info))
5442 return FALSE;
85fbca6a 5443
4ad4eba5
AM
5444 /* If this is a non-traditional link, try to optimize the handling
5445 of the .stab/.stabstr sections. */
5446 if (! dynamic
5447 && ! info->traditional_format
66eb6687 5448 && is_elf_hash_table (htab)
4ad4eba5
AM
5449 && (info->strip != strip_all && info->strip != strip_debugger))
5450 {
5451 asection *stabstr;
5452
5453 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5454 if (stabstr != NULL)
5455 {
5456 bfd_size_type string_offset = 0;
5457 asection *stab;
5458
5459 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5460 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5461 && (!stab->name[5] ||
5462 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5463 && (stab->flags & SEC_MERGE) == 0
5464 && !bfd_is_abs_section (stab->output_section))
5465 {
5466 struct bfd_elf_section_data *secdata;
5467
5468 secdata = elf_section_data (stab);
66eb6687
AM
5469 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5470 stabstr, &secdata->sec_info,
4ad4eba5
AM
5471 &string_offset))
5472 goto error_return;
5473 if (secdata->sec_info)
dbaa2011 5474 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5475 }
5476 }
5477 }
5478
66eb6687 5479 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5480 {
5481 /* Add this bfd to the loaded list. */
5482 struct elf_link_loaded_list *n;
5483
ca4be51c 5484 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5485 if (n == NULL)
5486 goto error_return;
5487 n->abfd = abfd;
66eb6687
AM
5488 n->next = htab->loaded;
5489 htab->loaded = n;
4ad4eba5
AM
5490 }
5491
5492 return TRUE;
5493
5494 error_free_vers:
66eb6687
AM
5495 if (old_tab != NULL)
5496 free (old_tab);
5b677558
AM
5497 if (old_strtab != NULL)
5498 free (old_strtab);
4ad4eba5
AM
5499 if (nondeflt_vers != NULL)
5500 free (nondeflt_vers);
5501 if (extversym != NULL)
5502 free (extversym);
5503 error_free_sym:
5504 if (isymbuf != NULL)
5505 free (isymbuf);
5506 error_return:
5507 return FALSE;
5508}
5509
8387904d
AM
5510/* Return the linker hash table entry of a symbol that might be
5511 satisfied by an archive symbol. Return -1 on error. */
5512
5513struct elf_link_hash_entry *
5514_bfd_elf_archive_symbol_lookup (bfd *abfd,
5515 struct bfd_link_info *info,
5516 const char *name)
5517{
5518 struct elf_link_hash_entry *h;
5519 char *p, *copy;
5520 size_t len, first;
5521
2a41f396 5522 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5523 if (h != NULL)
5524 return h;
5525
5526 /* If this is a default version (the name contains @@), look up the
5527 symbol again with only one `@' as well as without the version.
5528 The effect is that references to the symbol with and without the
5529 version will be matched by the default symbol in the archive. */
5530
5531 p = strchr (name, ELF_VER_CHR);
5532 if (p == NULL || p[1] != ELF_VER_CHR)
5533 return h;
5534
5535 /* First check with only one `@'. */
5536 len = strlen (name);
a50b1753 5537 copy = (char *) bfd_alloc (abfd, len);
8387904d 5538 if (copy == NULL)
e99955cd 5539 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5540
5541 first = p - name + 1;
5542 memcpy (copy, name, first);
5543 memcpy (copy + first, name + first + 1, len - first);
5544
2a41f396 5545 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5546 if (h == NULL)
5547 {
5548 /* We also need to check references to the symbol without the
5549 version. */
5550 copy[first - 1] = '\0';
5551 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5552 FALSE, FALSE, TRUE);
8387904d
AM
5553 }
5554
5555 bfd_release (abfd, copy);
5556 return h;
5557}
5558
0ad989f9 5559/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5560 don't use _bfd_generic_link_add_archive_symbols because we need to
5561 handle versioned symbols.
0ad989f9
L
5562
5563 Fortunately, ELF archive handling is simpler than that done by
5564 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5565 oddities. In ELF, if we find a symbol in the archive map, and the
5566 symbol is currently undefined, we know that we must pull in that
5567 object file.
5568
5569 Unfortunately, we do have to make multiple passes over the symbol
5570 table until nothing further is resolved. */
5571
4ad4eba5
AM
5572static bfd_boolean
5573elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5574{
5575 symindex c;
13e570f8 5576 unsigned char *included = NULL;
0ad989f9
L
5577 carsym *symdefs;
5578 bfd_boolean loop;
5579 bfd_size_type amt;
8387904d
AM
5580 const struct elf_backend_data *bed;
5581 struct elf_link_hash_entry * (*archive_symbol_lookup)
5582 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5583
5584 if (! bfd_has_map (abfd))
5585 {
5586 /* An empty archive is a special case. */
5587 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5588 return TRUE;
5589 bfd_set_error (bfd_error_no_armap);
5590 return FALSE;
5591 }
5592
5593 /* Keep track of all symbols we know to be already defined, and all
5594 files we know to be already included. This is to speed up the
5595 second and subsequent passes. */
5596 c = bfd_ardata (abfd)->symdef_count;
5597 if (c == 0)
5598 return TRUE;
5599 amt = c;
13e570f8
AM
5600 amt *= sizeof (*included);
5601 included = (unsigned char *) bfd_zmalloc (amt);
5602 if (included == NULL)
5603 return FALSE;
0ad989f9
L
5604
5605 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5606 bed = get_elf_backend_data (abfd);
5607 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5608
5609 do
5610 {
5611 file_ptr last;
5612 symindex i;
5613 carsym *symdef;
5614 carsym *symdefend;
5615
5616 loop = FALSE;
5617 last = -1;
5618
5619 symdef = symdefs;
5620 symdefend = symdef + c;
5621 for (i = 0; symdef < symdefend; symdef++, i++)
5622 {
5623 struct elf_link_hash_entry *h;
5624 bfd *element;
5625 struct bfd_link_hash_entry *undefs_tail;
5626 symindex mark;
5627
13e570f8 5628 if (included[i])
0ad989f9
L
5629 continue;
5630 if (symdef->file_offset == last)
5631 {
5632 included[i] = TRUE;
5633 continue;
5634 }
5635
8387904d 5636 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5637 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5638 goto error_return;
0ad989f9
L
5639
5640 if (h == NULL)
5641 continue;
5642
5643 if (h->root.type == bfd_link_hash_common)
5644 {
5645 /* We currently have a common symbol. The archive map contains
5646 a reference to this symbol, so we may want to include it. We
5647 only want to include it however, if this archive element
5648 contains a definition of the symbol, not just another common
5649 declaration of it.
5650
5651 Unfortunately some archivers (including GNU ar) will put
5652 declarations of common symbols into their archive maps, as
5653 well as real definitions, so we cannot just go by the archive
5654 map alone. Instead we must read in the element's symbol
5655 table and check that to see what kind of symbol definition
5656 this is. */
5657 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5658 continue;
5659 }
5660 else if (h->root.type != bfd_link_hash_undefined)
5661 {
5662 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5663 /* Symbol must be defined. Don't check it again. */
5664 included[i] = TRUE;
0ad989f9
L
5665 continue;
5666 }
5667
5668 /* We need to include this archive member. */
5669 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5670 if (element == NULL)
5671 goto error_return;
5672
5673 if (! bfd_check_format (element, bfd_object))
5674 goto error_return;
5675
0ad989f9
L
5676 undefs_tail = info->hash->undefs_tail;
5677
0e144ba7
AM
5678 if (!(*info->callbacks
5679 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5680 continue;
0e144ba7 5681 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5682 goto error_return;
5683
5684 /* If there are any new undefined symbols, we need to make
5685 another pass through the archive in order to see whether
5686 they can be defined. FIXME: This isn't perfect, because
5687 common symbols wind up on undefs_tail and because an
5688 undefined symbol which is defined later on in this pass
5689 does not require another pass. This isn't a bug, but it
5690 does make the code less efficient than it could be. */
5691 if (undefs_tail != info->hash->undefs_tail)
5692 loop = TRUE;
5693
5694 /* Look backward to mark all symbols from this object file
5695 which we have already seen in this pass. */
5696 mark = i;
5697 do
5698 {
5699 included[mark] = TRUE;
5700 if (mark == 0)
5701 break;
5702 --mark;
5703 }
5704 while (symdefs[mark].file_offset == symdef->file_offset);
5705
5706 /* We mark subsequent symbols from this object file as we go
5707 on through the loop. */
5708 last = symdef->file_offset;
5709 }
5710 }
5711 while (loop);
5712
0ad989f9
L
5713 free (included);
5714
5715 return TRUE;
5716
5717 error_return:
0ad989f9
L
5718 if (included != NULL)
5719 free (included);
5720 return FALSE;
5721}
4ad4eba5
AM
5722
5723/* Given an ELF BFD, add symbols to the global hash table as
5724 appropriate. */
5725
5726bfd_boolean
5727bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5728{
5729 switch (bfd_get_format (abfd))
5730 {
5731 case bfd_object:
5732 return elf_link_add_object_symbols (abfd, info);
5733 case bfd_archive:
5734 return elf_link_add_archive_symbols (abfd, info);
5735 default:
5736 bfd_set_error (bfd_error_wrong_format);
5737 return FALSE;
5738 }
5739}
5a580b3a 5740\f
14b1c01e
AM
5741struct hash_codes_info
5742{
5743 unsigned long *hashcodes;
5744 bfd_boolean error;
5745};
a0c8462f 5746
5a580b3a
AM
5747/* This function will be called though elf_link_hash_traverse to store
5748 all hash value of the exported symbols in an array. */
5749
5750static bfd_boolean
5751elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5752{
a50b1753 5753 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5754 const char *name;
5a580b3a
AM
5755 unsigned long ha;
5756 char *alc = NULL;
5757
5a580b3a
AM
5758 /* Ignore indirect symbols. These are added by the versioning code. */
5759 if (h->dynindx == -1)
5760 return TRUE;
5761
5762 name = h->root.root.string;
422f1182 5763 if (h->versioned >= versioned)
5a580b3a 5764 {
422f1182
L
5765 char *p = strchr (name, ELF_VER_CHR);
5766 if (p != NULL)
14b1c01e 5767 {
422f1182
L
5768 alc = (char *) bfd_malloc (p - name + 1);
5769 if (alc == NULL)
5770 {
5771 inf->error = TRUE;
5772 return FALSE;
5773 }
5774 memcpy (alc, name, p - name);
5775 alc[p - name] = '\0';
5776 name = alc;
14b1c01e 5777 }
5a580b3a
AM
5778 }
5779
5780 /* Compute the hash value. */
5781 ha = bfd_elf_hash (name);
5782
5783 /* Store the found hash value in the array given as the argument. */
14b1c01e 5784 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5785
5786 /* And store it in the struct so that we can put it in the hash table
5787 later. */
f6e332e6 5788 h->u.elf_hash_value = ha;
5a580b3a
AM
5789
5790 if (alc != NULL)
5791 free (alc);
5792
5793 return TRUE;
5794}
5795
fdc90cb4
JJ
5796struct collect_gnu_hash_codes
5797{
5798 bfd *output_bfd;
5799 const struct elf_backend_data *bed;
5800 unsigned long int nsyms;
5801 unsigned long int maskbits;
5802 unsigned long int *hashcodes;
5803 unsigned long int *hashval;
5804 unsigned long int *indx;
5805 unsigned long int *counts;
5806 bfd_vma *bitmask;
5807 bfd_byte *contents;
5808 long int min_dynindx;
5809 unsigned long int bucketcount;
5810 unsigned long int symindx;
5811 long int local_indx;
5812 long int shift1, shift2;
5813 unsigned long int mask;
14b1c01e 5814 bfd_boolean error;
fdc90cb4
JJ
5815};
5816
5817/* This function will be called though elf_link_hash_traverse to store
5818 all hash value of the exported symbols in an array. */
5819
5820static bfd_boolean
5821elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5822{
a50b1753 5823 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5824 const char *name;
fdc90cb4
JJ
5825 unsigned long ha;
5826 char *alc = NULL;
5827
fdc90cb4
JJ
5828 /* Ignore indirect symbols. These are added by the versioning code. */
5829 if (h->dynindx == -1)
5830 return TRUE;
5831
5832 /* Ignore also local symbols and undefined symbols. */
5833 if (! (*s->bed->elf_hash_symbol) (h))
5834 return TRUE;
5835
5836 name = h->root.root.string;
422f1182 5837 if (h->versioned >= versioned)
fdc90cb4 5838 {
422f1182
L
5839 char *p = strchr (name, ELF_VER_CHR);
5840 if (p != NULL)
14b1c01e 5841 {
422f1182
L
5842 alc = (char *) bfd_malloc (p - name + 1);
5843 if (alc == NULL)
5844 {
5845 s->error = TRUE;
5846 return FALSE;
5847 }
5848 memcpy (alc, name, p - name);
5849 alc[p - name] = '\0';
5850 name = alc;
14b1c01e 5851 }
fdc90cb4
JJ
5852 }
5853
5854 /* Compute the hash value. */
5855 ha = bfd_elf_gnu_hash (name);
5856
5857 /* Store the found hash value in the array for compute_bucket_count,
5858 and also for .dynsym reordering purposes. */
5859 s->hashcodes[s->nsyms] = ha;
5860 s->hashval[h->dynindx] = ha;
5861 ++s->nsyms;
5862 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5863 s->min_dynindx = h->dynindx;
5864
5865 if (alc != NULL)
5866 free (alc);
5867
5868 return TRUE;
5869}
5870
5871/* This function will be called though elf_link_hash_traverse to do
5872 final dynaminc symbol renumbering. */
5873
5874static bfd_boolean
5875elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5876{
a50b1753 5877 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5878 unsigned long int bucket;
5879 unsigned long int val;
5880
fdc90cb4
JJ
5881 /* Ignore indirect symbols. */
5882 if (h->dynindx == -1)
5883 return TRUE;
5884
5885 /* Ignore also local symbols and undefined symbols. */
5886 if (! (*s->bed->elf_hash_symbol) (h))
5887 {
5888 if (h->dynindx >= s->min_dynindx)
5889 h->dynindx = s->local_indx++;
5890 return TRUE;
5891 }
5892
5893 bucket = s->hashval[h->dynindx] % s->bucketcount;
5894 val = (s->hashval[h->dynindx] >> s->shift1)
5895 & ((s->maskbits >> s->shift1) - 1);
5896 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5897 s->bitmask[val]
5898 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5899 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5900 if (s->counts[bucket] == 1)
5901 /* Last element terminates the chain. */
5902 val |= 1;
5903 bfd_put_32 (s->output_bfd, val,
5904 s->contents + (s->indx[bucket] - s->symindx) * 4);
5905 --s->counts[bucket];
5906 h->dynindx = s->indx[bucket]++;
5907 return TRUE;
5908}
5909
5910/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5911
5912bfd_boolean
5913_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5914{
5915 return !(h->forced_local
5916 || h->root.type == bfd_link_hash_undefined
5917 || h->root.type == bfd_link_hash_undefweak
5918 || ((h->root.type == bfd_link_hash_defined
5919 || h->root.type == bfd_link_hash_defweak)
5920 && h->root.u.def.section->output_section == NULL));
5921}
5922
5a580b3a
AM
5923/* Array used to determine the number of hash table buckets to use
5924 based on the number of symbols there are. If there are fewer than
5925 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5926 fewer than 37 we use 17 buckets, and so forth. We never use more
5927 than 32771 buckets. */
5928
5929static const size_t elf_buckets[] =
5930{
5931 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5932 16411, 32771, 0
5933};
5934
5935/* Compute bucket count for hashing table. We do not use a static set
5936 of possible tables sizes anymore. Instead we determine for all
5937 possible reasonable sizes of the table the outcome (i.e., the
5938 number of collisions etc) and choose the best solution. The
5939 weighting functions are not too simple to allow the table to grow
5940 without bounds. Instead one of the weighting factors is the size.
5941 Therefore the result is always a good payoff between few collisions
5942 (= short chain lengths) and table size. */
5943static size_t
b20dd2ce 5944compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5945 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5946 unsigned long int nsyms,
5947 int gnu_hash)
5a580b3a 5948{
5a580b3a 5949 size_t best_size = 0;
5a580b3a 5950 unsigned long int i;
5a580b3a 5951
5a580b3a
AM
5952 /* We have a problem here. The following code to optimize the table
5953 size requires an integer type with more the 32 bits. If
5954 BFD_HOST_U_64_BIT is set we know about such a type. */
5955#ifdef BFD_HOST_U_64_BIT
5956 if (info->optimize)
5957 {
5a580b3a
AM
5958 size_t minsize;
5959 size_t maxsize;
5960 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5961 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5962 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5963 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5964 unsigned long int *counts;
d40f3da9 5965 bfd_size_type amt;
0883b6e0 5966 unsigned int no_improvement_count = 0;
5a580b3a
AM
5967
5968 /* Possible optimization parameters: if we have NSYMS symbols we say
5969 that the hashing table must at least have NSYMS/4 and at most
5970 2*NSYMS buckets. */
5971 minsize = nsyms / 4;
5972 if (minsize == 0)
5973 minsize = 1;
5974 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5975 if (gnu_hash)
5976 {
5977 if (minsize < 2)
5978 minsize = 2;
5979 if ((best_size & 31) == 0)
5980 ++best_size;
5981 }
5a580b3a
AM
5982
5983 /* Create array where we count the collisions in. We must use bfd_malloc
5984 since the size could be large. */
5985 amt = maxsize;
5986 amt *= sizeof (unsigned long int);
a50b1753 5987 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5988 if (counts == NULL)
fdc90cb4 5989 return 0;
5a580b3a
AM
5990
5991 /* Compute the "optimal" size for the hash table. The criteria is a
5992 minimal chain length. The minor criteria is (of course) the size
5993 of the table. */
5994 for (i = minsize; i < maxsize; ++i)
5995 {
5996 /* Walk through the array of hashcodes and count the collisions. */
5997 BFD_HOST_U_64_BIT max;
5998 unsigned long int j;
5999 unsigned long int fact;
6000
fdc90cb4
JJ
6001 if (gnu_hash && (i & 31) == 0)
6002 continue;
6003
5a580b3a
AM
6004 memset (counts, '\0', i * sizeof (unsigned long int));
6005
6006 /* Determine how often each hash bucket is used. */
6007 for (j = 0; j < nsyms; ++j)
6008 ++counts[hashcodes[j] % i];
6009
6010 /* For the weight function we need some information about the
6011 pagesize on the target. This is information need not be 100%
6012 accurate. Since this information is not available (so far) we
6013 define it here to a reasonable default value. If it is crucial
6014 to have a better value some day simply define this value. */
6015# ifndef BFD_TARGET_PAGESIZE
6016# define BFD_TARGET_PAGESIZE (4096)
6017# endif
6018
fdc90cb4
JJ
6019 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6020 and the chains. */
6021 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6022
6023# if 1
6024 /* Variant 1: optimize for short chains. We add the squares
6025 of all the chain lengths (which favors many small chain
6026 over a few long chains). */
6027 for (j = 0; j < i; ++j)
6028 max += counts[j] * counts[j];
6029
6030 /* This adds penalties for the overall size of the table. */
fdc90cb4 6031 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6032 max *= fact * fact;
6033# else
6034 /* Variant 2: Optimize a lot more for small table. Here we
6035 also add squares of the size but we also add penalties for
6036 empty slots (the +1 term). */
6037 for (j = 0; j < i; ++j)
6038 max += (1 + counts[j]) * (1 + counts[j]);
6039
6040 /* The overall size of the table is considered, but not as
6041 strong as in variant 1, where it is squared. */
fdc90cb4 6042 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6043 max *= fact;
6044# endif
6045
6046 /* Compare with current best results. */
6047 if (max < best_chlen)
6048 {
6049 best_chlen = max;
6050 best_size = i;
ca4be51c 6051 no_improvement_count = 0;
5a580b3a 6052 }
0883b6e0
NC
6053 /* PR 11843: Avoid futile long searches for the best bucket size
6054 when there are a large number of symbols. */
6055 else if (++no_improvement_count == 100)
6056 break;
5a580b3a
AM
6057 }
6058
6059 free (counts);
6060 }
6061 else
6062#endif /* defined (BFD_HOST_U_64_BIT) */
6063 {
6064 /* This is the fallback solution if no 64bit type is available or if we
6065 are not supposed to spend much time on optimizations. We select the
6066 bucket count using a fixed set of numbers. */
6067 for (i = 0; elf_buckets[i] != 0; i++)
6068 {
6069 best_size = elf_buckets[i];
fdc90cb4 6070 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6071 break;
6072 }
fdc90cb4
JJ
6073 if (gnu_hash && best_size < 2)
6074 best_size = 2;
5a580b3a
AM
6075 }
6076
5a580b3a
AM
6077 return best_size;
6078}
6079
d0bf826b
AM
6080/* Size any SHT_GROUP section for ld -r. */
6081
6082bfd_boolean
6083_bfd_elf_size_group_sections (struct bfd_link_info *info)
6084{
6085 bfd *ibfd;
57963c05 6086 asection *s;
d0bf826b 6087
c72f2fb2 6088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6089 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6090 && (s = ibfd->sections) != NULL
6091 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6092 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6093 return FALSE;
6094 return TRUE;
6095}
6096
04c3a755
NS
6097/* Set a default stack segment size. The value in INFO wins. If it
6098 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6099 undefined it is initialized. */
6100
6101bfd_boolean
6102bfd_elf_stack_segment_size (bfd *output_bfd,
6103 struct bfd_link_info *info,
6104 const char *legacy_symbol,
6105 bfd_vma default_size)
6106{
6107 struct elf_link_hash_entry *h = NULL;
6108
6109 /* Look for legacy symbol. */
6110 if (legacy_symbol)
6111 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6112 FALSE, FALSE, FALSE);
6113 if (h && (h->root.type == bfd_link_hash_defined
6114 || h->root.type == bfd_link_hash_defweak)
6115 && h->def_regular
6116 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6117 {
6118 /* The symbol has no type if specified on the command line. */
6119 h->type = STT_OBJECT;
6120 if (info->stacksize)
695344c0 6121 /* xgettext:c-format */
871b3ab2 6122 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6123 output_bfd, legacy_symbol);
04c3a755 6124 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6125 /* xgettext:c-format */
871b3ab2 6126 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6127 output_bfd, legacy_symbol);
04c3a755
NS
6128 else
6129 info->stacksize = h->root.u.def.value;
6130 }
6131
6132 if (!info->stacksize)
6133 /* If the user didn't set a size, or explicitly inhibit the
6134 size, set it now. */
6135 info->stacksize = default_size;
6136
6137 /* Provide the legacy symbol, if it is referenced. */
6138 if (h && (h->root.type == bfd_link_hash_undefined
6139 || h->root.type == bfd_link_hash_undefweak))
6140 {
6141 struct bfd_link_hash_entry *bh = NULL;
6142
6143 if (!(_bfd_generic_link_add_one_symbol
6144 (info, output_bfd, legacy_symbol,
6145 BSF_GLOBAL, bfd_abs_section_ptr,
6146 info->stacksize >= 0 ? info->stacksize : 0,
6147 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6148 return FALSE;
6149
6150 h = (struct elf_link_hash_entry *) bh;
6151 h->def_regular = 1;
6152 h->type = STT_OBJECT;
6153 }
6154
6155 return TRUE;
6156}
6157
b531344c
MR
6158/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6159
6160struct elf_gc_sweep_symbol_info
6161{
6162 struct bfd_link_info *info;
6163 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6164 bfd_boolean);
6165};
6166
6167static bfd_boolean
6168elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6169{
6170 if (!h->mark
6171 && (((h->root.type == bfd_link_hash_defined
6172 || h->root.type == bfd_link_hash_defweak)
6173 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6174 && h->root.u.def.section->gc_mark))
6175 || h->root.type == bfd_link_hash_undefined
6176 || h->root.type == bfd_link_hash_undefweak))
6177 {
6178 struct elf_gc_sweep_symbol_info *inf;
6179
6180 inf = (struct elf_gc_sweep_symbol_info *) data;
6181 (*inf->hide_symbol) (inf->info, h, TRUE);
6182 h->def_regular = 0;
6183 h->ref_regular = 0;
6184 h->ref_regular_nonweak = 0;
6185 }
6186
6187 return TRUE;
6188}
6189
5a580b3a
AM
6190/* Set up the sizes and contents of the ELF dynamic sections. This is
6191 called by the ELF linker emulation before_allocation routine. We
6192 must set the sizes of the sections before the linker sets the
6193 addresses of the various sections. */
6194
6195bfd_boolean
6196bfd_elf_size_dynamic_sections (bfd *output_bfd,
6197 const char *soname,
6198 const char *rpath,
6199 const char *filter_shlib,
7ee314fa
AM
6200 const char *audit,
6201 const char *depaudit,
5a580b3a
AM
6202 const char * const *auxiliary_filters,
6203 struct bfd_link_info *info,
fd91d419 6204 asection **sinterpptr)
5a580b3a 6205{
5a580b3a
AM
6206 bfd *dynobj;
6207 const struct elf_backend_data *bed;
5a580b3a
AM
6208
6209 *sinterpptr = NULL;
6210
5a580b3a
AM
6211 if (!is_elf_hash_table (info->hash))
6212 return TRUE;
6213
5a580b3a
AM
6214 dynobj = elf_hash_table (info)->dynobj;
6215
9a2a56cc 6216 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6217 {
902e9fc7
MR
6218 struct bfd_elf_version_tree *verdefs;
6219 struct elf_info_failed asvinfo;
5a580b3a
AM
6220 struct bfd_elf_version_tree *t;
6221 struct bfd_elf_version_expr *d;
902e9fc7 6222 asection *s;
e6699019 6223 size_t soname_indx;
7ee314fa 6224
5a580b3a
AM
6225 /* If we are supposed to export all symbols into the dynamic symbol
6226 table (this is not the normal case), then do so. */
55255dae 6227 if (info->export_dynamic
0e1862bb 6228 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6229 {
3d13f3e9
AM
6230 struct elf_info_failed eif;
6231
6232 eif.info = info;
6233 eif.failed = FALSE;
5a580b3a
AM
6234 elf_link_hash_traverse (elf_hash_table (info),
6235 _bfd_elf_export_symbol,
6236 &eif);
6237 if (eif.failed)
6238 return FALSE;
6239 }
6240
e6699019
L
6241 if (soname != NULL)
6242 {
6243 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6244 soname, TRUE);
6245 if (soname_indx == (size_t) -1
6246 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6247 return FALSE;
6248 }
6249 else
6250 soname_indx = (size_t) -1;
6251
5a580b3a 6252 /* Make all global versions with definition. */
fd91d419 6253 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6254 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6255 if (!d->symver && d->literal)
5a580b3a
AM
6256 {
6257 const char *verstr, *name;
6258 size_t namelen, verlen, newlen;
93252b1c 6259 char *newname, *p, leading_char;
5a580b3a
AM
6260 struct elf_link_hash_entry *newh;
6261
93252b1c 6262 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6263 name = d->pattern;
93252b1c 6264 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6265 verstr = t->name;
6266 verlen = strlen (verstr);
6267 newlen = namelen + verlen + 3;
6268
a50b1753 6269 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6270 if (newname == NULL)
6271 return FALSE;
93252b1c
MF
6272 newname[0] = leading_char;
6273 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6274
6275 /* Check the hidden versioned definition. */
6276 p = newname + namelen;
6277 *p++ = ELF_VER_CHR;
6278 memcpy (p, verstr, verlen + 1);
6279 newh = elf_link_hash_lookup (elf_hash_table (info),
6280 newname, FALSE, FALSE,
6281 FALSE);
6282 if (newh == NULL
6283 || (newh->root.type != bfd_link_hash_defined
6284 && newh->root.type != bfd_link_hash_defweak))
6285 {
6286 /* Check the default versioned definition. */
6287 *p++ = ELF_VER_CHR;
6288 memcpy (p, verstr, verlen + 1);
6289 newh = elf_link_hash_lookup (elf_hash_table (info),
6290 newname, FALSE, FALSE,
6291 FALSE);
6292 }
6293 free (newname);
6294
6295 /* Mark this version if there is a definition and it is
6296 not defined in a shared object. */
6297 if (newh != NULL
f5385ebf 6298 && !newh->def_dynamic
5a580b3a
AM
6299 && (newh->root.type == bfd_link_hash_defined
6300 || newh->root.type == bfd_link_hash_defweak))
6301 d->symver = 1;
6302 }
6303
6304 /* Attach all the symbols to their version information. */
5a580b3a 6305 asvinfo.info = info;
5a580b3a
AM
6306 asvinfo.failed = FALSE;
6307
6308 elf_link_hash_traverse (elf_hash_table (info),
6309 _bfd_elf_link_assign_sym_version,
6310 &asvinfo);
6311 if (asvinfo.failed)
6312 return FALSE;
6313
6314 if (!info->allow_undefined_version)
6315 {
6316 /* Check if all global versions have a definition. */
3d13f3e9 6317 bfd_boolean all_defined = TRUE;
fd91d419 6318 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6319 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6320 if (d->literal && !d->symver && !d->script)
5a580b3a 6321 {
4eca0228 6322 _bfd_error_handler
5a580b3a
AM
6323 (_("%s: undefined version: %s"),
6324 d->pattern, t->name);
6325 all_defined = FALSE;
6326 }
6327
6328 if (!all_defined)
6329 {
6330 bfd_set_error (bfd_error_bad_value);
6331 return FALSE;
6332 }
6333 }
6334
902e9fc7
MR
6335 /* Set up the version definition section. */
6336 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6337 BFD_ASSERT (s != NULL);
5a580b3a 6338
902e9fc7
MR
6339 /* We may have created additional version definitions if we are
6340 just linking a regular application. */
6341 verdefs = info->version_info;
5a580b3a 6342
902e9fc7
MR
6343 /* Skip anonymous version tag. */
6344 if (verdefs != NULL && verdefs->vernum == 0)
6345 verdefs = verdefs->next;
5a580b3a 6346
902e9fc7
MR
6347 if (verdefs == NULL && !info->create_default_symver)
6348 s->flags |= SEC_EXCLUDE;
6349 else
5a580b3a 6350 {
902e9fc7
MR
6351 unsigned int cdefs;
6352 bfd_size_type size;
6353 bfd_byte *p;
6354 Elf_Internal_Verdef def;
6355 Elf_Internal_Verdaux defaux;
6356 struct bfd_link_hash_entry *bh;
6357 struct elf_link_hash_entry *h;
6358 const char *name;
5a580b3a 6359
902e9fc7
MR
6360 cdefs = 0;
6361 size = 0;
5a580b3a 6362
902e9fc7
MR
6363 /* Make space for the base version. */
6364 size += sizeof (Elf_External_Verdef);
6365 size += sizeof (Elf_External_Verdaux);
6366 ++cdefs;
6367
6368 /* Make space for the default version. */
6369 if (info->create_default_symver)
6370 {
6371 size += sizeof (Elf_External_Verdef);
6372 ++cdefs;
3e3b46e5
PB
6373 }
6374
5a580b3a
AM
6375 for (t = verdefs; t != NULL; t = t->next)
6376 {
6377 struct bfd_elf_version_deps *n;
6378
a6cc6b3b
RO
6379 /* Don't emit base version twice. */
6380 if (t->vernum == 0)
6381 continue;
6382
5a580b3a
AM
6383 size += sizeof (Elf_External_Verdef);
6384 size += sizeof (Elf_External_Verdaux);
6385 ++cdefs;
6386
6387 for (n = t->deps; n != NULL; n = n->next)
6388 size += sizeof (Elf_External_Verdaux);
6389 }
6390
eea6121a 6391 s->size = size;
a50b1753 6392 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6393 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6394 return FALSE;
6395
6396 /* Fill in the version definition section. */
6397
6398 p = s->contents;
6399
6400 def.vd_version = VER_DEF_CURRENT;
6401 def.vd_flags = VER_FLG_BASE;
6402 def.vd_ndx = 1;
6403 def.vd_cnt = 1;
3e3b46e5
PB
6404 if (info->create_default_symver)
6405 {
6406 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6407 def.vd_next = sizeof (Elf_External_Verdef);
6408 }
6409 else
6410 {
6411 def.vd_aux = sizeof (Elf_External_Verdef);
6412 def.vd_next = (sizeof (Elf_External_Verdef)
6413 + sizeof (Elf_External_Verdaux));
6414 }
5a580b3a 6415
ef53be89 6416 if (soname_indx != (size_t) -1)
5a580b3a
AM
6417 {
6418 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6419 soname_indx);
6420 def.vd_hash = bfd_elf_hash (soname);
6421 defaux.vda_name = soname_indx;
3e3b46e5 6422 name = soname;
5a580b3a
AM
6423 }
6424 else
6425 {
ef53be89 6426 size_t indx;
5a580b3a 6427
06084812 6428 name = lbasename (output_bfd->filename);
5a580b3a
AM
6429 def.vd_hash = bfd_elf_hash (name);
6430 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6431 name, FALSE);
ef53be89 6432 if (indx == (size_t) -1)
5a580b3a
AM
6433 return FALSE;
6434 defaux.vda_name = indx;
6435 }
6436 defaux.vda_next = 0;
6437
6438 _bfd_elf_swap_verdef_out (output_bfd, &def,
6439 (Elf_External_Verdef *) p);
6440 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6441 if (info->create_default_symver)
6442 {
6443 /* Add a symbol representing this version. */
6444 bh = NULL;
6445 if (! (_bfd_generic_link_add_one_symbol
6446 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6447 0, NULL, FALSE,
6448 get_elf_backend_data (dynobj)->collect, &bh)))
6449 return FALSE;
6450 h = (struct elf_link_hash_entry *) bh;
6451 h->non_elf = 0;
6452 h->def_regular = 1;
6453 h->type = STT_OBJECT;
6454 h->verinfo.vertree = NULL;
6455
6456 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6457 return FALSE;
6458
6459 /* Create a duplicate of the base version with the same
6460 aux block, but different flags. */
6461 def.vd_flags = 0;
6462 def.vd_ndx = 2;
6463 def.vd_aux = sizeof (Elf_External_Verdef);
6464 if (verdefs)
6465 def.vd_next = (sizeof (Elf_External_Verdef)
6466 + sizeof (Elf_External_Verdaux));
6467 else
6468 def.vd_next = 0;
6469 _bfd_elf_swap_verdef_out (output_bfd, &def,
6470 (Elf_External_Verdef *) p);
6471 p += sizeof (Elf_External_Verdef);
6472 }
5a580b3a
AM
6473 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6474 (Elf_External_Verdaux *) p);
6475 p += sizeof (Elf_External_Verdaux);
6476
6477 for (t = verdefs; t != NULL; t = t->next)
6478 {
6479 unsigned int cdeps;
6480 struct bfd_elf_version_deps *n;
5a580b3a 6481
a6cc6b3b
RO
6482 /* Don't emit the base version twice. */
6483 if (t->vernum == 0)
6484 continue;
6485
5a580b3a
AM
6486 cdeps = 0;
6487 for (n = t->deps; n != NULL; n = n->next)
6488 ++cdeps;
6489
6490 /* Add a symbol representing this version. */
6491 bh = NULL;
6492 if (! (_bfd_generic_link_add_one_symbol
6493 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6494 0, NULL, FALSE,
6495 get_elf_backend_data (dynobj)->collect, &bh)))
6496 return FALSE;
6497 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6498 h->non_elf = 0;
6499 h->def_regular = 1;
5a580b3a
AM
6500 h->type = STT_OBJECT;
6501 h->verinfo.vertree = t;
6502
c152c796 6503 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6504 return FALSE;
6505
6506 def.vd_version = VER_DEF_CURRENT;
6507 def.vd_flags = 0;
6508 if (t->globals.list == NULL
6509 && t->locals.list == NULL
6510 && ! t->used)
6511 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6512 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6513 def.vd_cnt = cdeps + 1;
6514 def.vd_hash = bfd_elf_hash (t->name);
6515 def.vd_aux = sizeof (Elf_External_Verdef);
6516 def.vd_next = 0;
a6cc6b3b
RO
6517
6518 /* If a basever node is next, it *must* be the last node in
6519 the chain, otherwise Verdef construction breaks. */
6520 if (t->next != NULL && t->next->vernum == 0)
6521 BFD_ASSERT (t->next->next == NULL);
6522
6523 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6524 def.vd_next = (sizeof (Elf_External_Verdef)
6525 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6526
6527 _bfd_elf_swap_verdef_out (output_bfd, &def,
6528 (Elf_External_Verdef *) p);
6529 p += sizeof (Elf_External_Verdef);
6530
6531 defaux.vda_name = h->dynstr_index;
6532 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6533 h->dynstr_index);
6534 defaux.vda_next = 0;
6535 if (t->deps != NULL)
6536 defaux.vda_next = sizeof (Elf_External_Verdaux);
6537 t->name_indx = defaux.vda_name;
6538
6539 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6540 (Elf_External_Verdaux *) p);
6541 p += sizeof (Elf_External_Verdaux);
6542
6543 for (n = t->deps; n != NULL; n = n->next)
6544 {
6545 if (n->version_needed == NULL)
6546 {
6547 /* This can happen if there was an error in the
6548 version script. */
6549 defaux.vda_name = 0;
6550 }
6551 else
6552 {
6553 defaux.vda_name = n->version_needed->name_indx;
6554 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6555 defaux.vda_name);
6556 }
6557 if (n->next == NULL)
6558 defaux.vda_next = 0;
6559 else
6560 defaux.vda_next = sizeof (Elf_External_Verdaux);
6561
6562 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6563 (Elf_External_Verdaux *) p);
6564 p += sizeof (Elf_External_Verdaux);
6565 }
6566 }
6567
5a580b3a
AM
6568 elf_tdata (output_bfd)->cverdefs = cdefs;
6569 }
902e9fc7
MR
6570 }
6571
6572 bed = get_elf_backend_data (output_bfd);
6573
6574 if (info->gc_sections && bed->can_gc_sections)
6575 {
6576 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6577
6578 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6579 dynamic symbol table. */
902e9fc7
MR
6580 sweep_info.info = info;
6581 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6582 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6583 &sweep_info);
3d13f3e9
AM
6584 }
6585
6586 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6587 {
6588 asection *s;
6589 struct elf_find_verdep_info sinfo;
6590
6591 /* Work out the size of the version reference section. */
6592
6593 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6594 BFD_ASSERT (s != NULL);
902e9fc7 6595
3d13f3e9
AM
6596 sinfo.info = info;
6597 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6598 if (sinfo.vers == 0)
6599 sinfo.vers = 1;
6600 sinfo.failed = FALSE;
6601
6602 elf_link_hash_traverse (elf_hash_table (info),
6603 _bfd_elf_link_find_version_dependencies,
6604 &sinfo);
6605 if (sinfo.failed)
6606 return FALSE;
6607
6608 if (elf_tdata (output_bfd)->verref == NULL)
6609 s->flags |= SEC_EXCLUDE;
6610 else
6611 {
6612 Elf_Internal_Verneed *vn;
6613 unsigned int size;
6614 unsigned int crefs;
6615 bfd_byte *p;
6616
6617 /* Build the version dependency section. */
6618 size = 0;
6619 crefs = 0;
6620 for (vn = elf_tdata (output_bfd)->verref;
6621 vn != NULL;
6622 vn = vn->vn_nextref)
6623 {
6624 Elf_Internal_Vernaux *a;
6625
6626 size += sizeof (Elf_External_Verneed);
6627 ++crefs;
6628 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6629 size += sizeof (Elf_External_Vernaux);
6630 }
6631
6632 s->size = size;
6633 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6634 if (s->contents == NULL)
6635 return FALSE;
6636
6637 p = s->contents;
6638 for (vn = elf_tdata (output_bfd)->verref;
6639 vn != NULL;
6640 vn = vn->vn_nextref)
6641 {
6642 unsigned int caux;
6643 Elf_Internal_Vernaux *a;
6644 size_t indx;
6645
6646 caux = 0;
6647 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6648 ++caux;
6649
6650 vn->vn_version = VER_NEED_CURRENT;
6651 vn->vn_cnt = caux;
6652 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6653 elf_dt_name (vn->vn_bfd) != NULL
6654 ? elf_dt_name (vn->vn_bfd)
6655 : lbasename (vn->vn_bfd->filename),
6656 FALSE);
6657 if (indx == (size_t) -1)
6658 return FALSE;
6659 vn->vn_file = indx;
6660 vn->vn_aux = sizeof (Elf_External_Verneed);
6661 if (vn->vn_nextref == NULL)
6662 vn->vn_next = 0;
6663 else
6664 vn->vn_next = (sizeof (Elf_External_Verneed)
6665 + caux * sizeof (Elf_External_Vernaux));
6666
6667 _bfd_elf_swap_verneed_out (output_bfd, vn,
6668 (Elf_External_Verneed *) p);
6669 p += sizeof (Elf_External_Verneed);
6670
6671 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6672 {
6673 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6674 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6675 a->vna_nodename, FALSE);
6676 if (indx == (size_t) -1)
6677 return FALSE;
6678 a->vna_name = indx;
6679 if (a->vna_nextptr == NULL)
6680 a->vna_next = 0;
6681 else
6682 a->vna_next = sizeof (Elf_External_Vernaux);
6683
6684 _bfd_elf_swap_vernaux_out (output_bfd, a,
6685 (Elf_External_Vernaux *) p);
6686 p += sizeof (Elf_External_Vernaux);
6687 }
6688 }
6689
6690 elf_tdata (output_bfd)->cverrefs = crefs;
6691 }
902e9fc7
MR
6692 }
6693
6694 /* Any syms created from now on start with -1 in
6695 got.refcount/offset and plt.refcount/offset. */
6696 elf_hash_table (info)->init_got_refcount
6697 = elf_hash_table (info)->init_got_offset;
6698 elf_hash_table (info)->init_plt_refcount
6699 = elf_hash_table (info)->init_plt_offset;
6700
6701 if (bfd_link_relocatable (info)
6702 && !_bfd_elf_size_group_sections (info))
6703 return FALSE;
6704
6705 /* The backend may have to create some sections regardless of whether
6706 we're dynamic or not. */
6707 if (bed->elf_backend_always_size_sections
6708 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6709 return FALSE;
6710
6711 /* Determine any GNU_STACK segment requirements, after the backend
6712 has had a chance to set a default segment size. */
6713 if (info->execstack)
6714 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6715 else if (info->noexecstack)
6716 elf_stack_flags (output_bfd) = PF_R | PF_W;
6717 else
6718 {
6719 bfd *inputobj;
6720 asection *notesec = NULL;
6721 int exec = 0;
6722
6723 for (inputobj = info->input_bfds;
6724 inputobj;
6725 inputobj = inputobj->link.next)
6726 {
6727 asection *s;
6728
6729 if (inputobj->flags
6730 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6731 continue;
57963c05
AM
6732 s = inputobj->sections;
6733 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6734 continue;
6735
902e9fc7
MR
6736 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6737 if (s)
6738 {
6739 if (s->flags & SEC_CODE)
6740 exec = PF_X;
6741 notesec = s;
6742 }
6743 else if (bed->default_execstack)
6744 exec = PF_X;
6745 }
6746 if (notesec || info->stacksize > 0)
6747 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6748 if (notesec && exec && bfd_link_relocatable (info)
6749 && notesec->output_section != bfd_abs_section_ptr)
6750 notesec->output_section->flags |= SEC_CODE;
6751 }
6752
6753 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6754 {
6755 struct elf_info_failed eif;
6756 struct elf_link_hash_entry *h;
6757 asection *dynstr;
6758 asection *s;
6759
6760 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6761 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6762
902e9fc7
MR
6763 if (info->symbolic)
6764 {
6765 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6766 return FALSE;
6767 info->flags |= DF_SYMBOLIC;
6768 }
6769
6770 if (rpath != NULL)
6771 {
6772 size_t indx;
6773 bfd_vma tag;
6774
6775 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6776 TRUE);
6777 if (indx == (size_t) -1)
6778 return FALSE;
6779
6780 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6781 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6782 return FALSE;
6783 }
6784
6785 if (filter_shlib != NULL)
6786 {
6787 size_t indx;
6788
6789 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6790 filter_shlib, TRUE);
6791 if (indx == (size_t) -1
6792 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6793 return FALSE;
6794 }
6795
6796 if (auxiliary_filters != NULL)
6797 {
6798 const char * const *p;
6799
6800 for (p = auxiliary_filters; *p != NULL; p++)
6801 {
6802 size_t indx;
6803
6804 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6805 *p, TRUE);
6806 if (indx == (size_t) -1
6807 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6808 return FALSE;
6809 }
6810 }
6811
6812 if (audit != NULL)
6813 {
6814 size_t indx;
6815
6816 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6817 TRUE);
6818 if (indx == (size_t) -1
6819 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6820 return FALSE;
6821 }
6822
6823 if (depaudit != NULL)
6824 {
6825 size_t indx;
6826
6827 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6828 TRUE);
6829 if (indx == (size_t) -1
6830 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6831 return FALSE;
6832 }
6833
6834 eif.info = info;
6835 eif.failed = FALSE;
6836
6837 /* Find all symbols which were defined in a dynamic object and make
6838 the backend pick a reasonable value for them. */
6839 elf_link_hash_traverse (elf_hash_table (info),
6840 _bfd_elf_adjust_dynamic_symbol,
6841 &eif);
6842 if (eif.failed)
6843 return FALSE;
6844
6845 /* Add some entries to the .dynamic section. We fill in some of the
6846 values later, in bfd_elf_final_link, but we must add the entries
6847 now so that we know the final size of the .dynamic section. */
6848
6849 /* If there are initialization and/or finalization functions to
6850 call then add the corresponding DT_INIT/DT_FINI entries. */
6851 h = (info->init_function
6852 ? elf_link_hash_lookup (elf_hash_table (info),
6853 info->init_function, FALSE,
6854 FALSE, FALSE)
6855 : NULL);
6856 if (h != NULL
6857 && (h->ref_regular
6858 || h->def_regular))
6859 {
6860 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6861 return FALSE;
6862 }
6863 h = (info->fini_function
6864 ? elf_link_hash_lookup (elf_hash_table (info),
6865 info->fini_function, FALSE,
6866 FALSE, FALSE)
6867 : NULL);
6868 if (h != NULL
6869 && (h->ref_regular
6870 || h->def_regular))
6871 {
6872 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6873 return FALSE;
6874 }
6875
6876 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6877 if (s != NULL && s->linker_has_input)
6878 {
6879 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6880 if (! bfd_link_executable (info))
6881 {
6882 bfd *sub;
6883 asection *o;
6884
57963c05
AM
6885 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6886 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6887 && (o = sub->sections) != NULL
6888 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6889 for (o = sub->sections; o != NULL; o = o->next)
6890 if (elf_section_data (o)->this_hdr.sh_type
6891 == SHT_PREINIT_ARRAY)
6892 {
6893 _bfd_error_handler
871b3ab2 6894 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6895 sub);
6896 break;
6897 }
6898
6899 bfd_set_error (bfd_error_nonrepresentable_section);
6900 return FALSE;
6901 }
6902
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6904 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6905 return FALSE;
6906 }
6907 s = bfd_get_section_by_name (output_bfd, ".init_array");
6908 if (s != NULL && s->linker_has_input)
6909 {
6910 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6911 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6912 return FALSE;
6913 }
6914 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6915 if (s != NULL && s->linker_has_input)
6916 {
6917 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6918 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6919 return FALSE;
6920 }
6921
6922 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6923 /* If .dynstr is excluded from the link, we don't want any of
6924 these tags. Strictly, we should be checking each section
6925 individually; This quick check covers for the case where
6926 someone does a /DISCARD/ : { *(*) }. */
6927 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6928 {
6929 bfd_size_type strsize;
6930
6931 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6932 if ((info->emit_hash
6933 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6934 || (info->emit_gnu_hash
6935 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6936 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6937 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6938 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6939 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6940 bed->s->sizeof_sym))
6941 return FALSE;
6942 }
6943 }
6944
6945 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6946 return FALSE;
6947
6948 /* The backend must work out the sizes of all the other dynamic
6949 sections. */
6950 if (dynobj != NULL
6951 && bed->elf_backend_size_dynamic_sections != NULL
6952 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6953 return FALSE;
6954
6955 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6956 {
902e9fc7
MR
6957 if (elf_tdata (output_bfd)->cverdefs)
6958 {
6959 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6960
6961 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6962 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6963 return FALSE;
6964 }
6965
6966 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6967 {
6968 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6969 return FALSE;
6970 }
6971 else if (info->flags & DF_BIND_NOW)
6972 {
6973 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6974 return FALSE;
6975 }
6976
6977 if (info->flags_1)
6978 {
6979 if (bfd_link_executable (info))
6980 info->flags_1 &= ~ (DF_1_INITFIRST
6981 | DF_1_NODELETE
6982 | DF_1_NOOPEN);
6983 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6984 return FALSE;
6985 }
6986
6987 if (elf_tdata (output_bfd)->cverrefs)
6988 {
6989 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6990
6991 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6992 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6993 return FALSE;
6994 }
5a580b3a 6995
8423293d
AM
6996 if ((elf_tdata (output_bfd)->cverrefs == 0
6997 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 6998 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 6999 {
902e9fc7
MR
7000 asection *s;
7001
3d4d4302 7002 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7003 s->flags |= SEC_EXCLUDE;
7004 }
7005 }
7006 return TRUE;
7007}
7008
74541ad4
AM
7009/* Find the first non-excluded output section. We'll use its
7010 section symbol for some emitted relocs. */
7011void
7012_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7013{
7014 asection *s;
7015
7016 for (s = output_bfd->sections; s != NULL; s = s->next)
7017 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7018 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
7019 {
7020 elf_hash_table (info)->text_index_section = s;
7021 break;
7022 }
7023}
7024
7025/* Find two non-excluded output sections, one for code, one for data.
7026 We'll use their section symbols for some emitted relocs. */
7027void
7028_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7029{
7030 asection *s;
7031
266b05cf 7032 /* Data first, since setting text_index_section changes
7f923b7f 7033 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7034 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 7035 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 7036 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7037 {
266b05cf 7038 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
7039 break;
7040 }
7041
7042 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
7043 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7044 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 7045 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7046 {
266b05cf 7047 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
7048 break;
7049 }
7050
7051 if (elf_hash_table (info)->text_index_section == NULL)
7052 elf_hash_table (info)->text_index_section
7053 = elf_hash_table (info)->data_index_section;
7054}
7055
8423293d
AM
7056bfd_boolean
7057bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7058{
74541ad4 7059 const struct elf_backend_data *bed;
23ec1e32 7060 unsigned long section_sym_count;
96d01d93 7061 bfd_size_type dynsymcount = 0;
74541ad4 7062
8423293d
AM
7063 if (!is_elf_hash_table (info->hash))
7064 return TRUE;
7065
74541ad4
AM
7066 bed = get_elf_backend_data (output_bfd);
7067 (*bed->elf_backend_init_index_section) (output_bfd, info);
7068
23ec1e32
MR
7069 /* Assign dynsym indices. In a shared library we generate a section
7070 symbol for each output section, which come first. Next come all
7071 of the back-end allocated local dynamic syms, followed by the rest
7072 of the global symbols.
7073
7074 This is usually not needed for static binaries, however backends
7075 can request to always do it, e.g. the MIPS backend uses dynamic
7076 symbol counts to lay out GOT, which will be produced in the
7077 presence of GOT relocations even in static binaries (holding fixed
7078 data in that case, to satisfy those relocations). */
7079
7080 if (elf_hash_table (info)->dynamic_sections_created
7081 || bed->always_renumber_dynsyms)
7082 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7083 &section_sym_count);
7084
8423293d
AM
7085 if (elf_hash_table (info)->dynamic_sections_created)
7086 {
7087 bfd *dynobj;
8423293d 7088 asection *s;
8423293d
AM
7089 unsigned int dtagcount;
7090
7091 dynobj = elf_hash_table (info)->dynobj;
7092
5a580b3a 7093 /* Work out the size of the symbol version section. */
3d4d4302 7094 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7095 BFD_ASSERT (s != NULL);
d5486c43 7096 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7097 {
eea6121a 7098 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7099 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7100 if (s->contents == NULL)
7101 return FALSE;
7102
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7104 return FALSE;
7105 }
7106
7107 /* Set the size of the .dynsym and .hash sections. We counted
7108 the number of dynamic symbols in elf_link_add_object_symbols.
7109 We will build the contents of .dynsym and .hash when we build
7110 the final symbol table, because until then we do not know the
7111 correct value to give the symbols. We built the .dynstr
7112 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7113 s = elf_hash_table (info)->dynsym;
5a580b3a 7114 BFD_ASSERT (s != NULL);
eea6121a 7115 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7116
d5486c43
L
7117 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7118 if (s->contents == NULL)
7119 return FALSE;
5a580b3a 7120
d5486c43
L
7121 /* The first entry in .dynsym is a dummy symbol. Clear all the
7122 section syms, in case we don't output them all. */
7123 ++section_sym_count;
7124 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7125
fdc90cb4
JJ
7126 elf_hash_table (info)->bucketcount = 0;
7127
5a580b3a
AM
7128 /* Compute the size of the hashing table. As a side effect this
7129 computes the hash values for all the names we export. */
fdc90cb4
JJ
7130 if (info->emit_hash)
7131 {
7132 unsigned long int *hashcodes;
14b1c01e 7133 struct hash_codes_info hashinf;
fdc90cb4
JJ
7134 bfd_size_type amt;
7135 unsigned long int nsyms;
7136 size_t bucketcount;
7137 size_t hash_entry_size;
7138
7139 /* Compute the hash values for all exported symbols. At the same
7140 time store the values in an array so that we could use them for
7141 optimizations. */
7142 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7143 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7144 if (hashcodes == NULL)
7145 return FALSE;
14b1c01e
AM
7146 hashinf.hashcodes = hashcodes;
7147 hashinf.error = FALSE;
5a580b3a 7148
fdc90cb4
JJ
7149 /* Put all hash values in HASHCODES. */
7150 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7151 elf_collect_hash_codes, &hashinf);
7152 if (hashinf.error)
4dd07732
AM
7153 {
7154 free (hashcodes);
7155 return FALSE;
7156 }
5a580b3a 7157
14b1c01e 7158 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7159 bucketcount
7160 = compute_bucket_count (info, hashcodes, nsyms, 0);
7161 free (hashcodes);
7162
4b48e2f6 7163 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7164 return FALSE;
5a580b3a 7165
fdc90cb4
JJ
7166 elf_hash_table (info)->bucketcount = bucketcount;
7167
3d4d4302 7168 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7169 BFD_ASSERT (s != NULL);
7170 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7171 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7172 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7173 if (s->contents == NULL)
7174 return FALSE;
7175
7176 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7177 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7178 s->contents + hash_entry_size);
7179 }
7180
7181 if (info->emit_gnu_hash)
7182 {
7183 size_t i, cnt;
7184 unsigned char *contents;
7185 struct collect_gnu_hash_codes cinfo;
7186 bfd_size_type amt;
7187 size_t bucketcount;
7188
7189 memset (&cinfo, 0, sizeof (cinfo));
7190
7191 /* Compute the hash values for all exported symbols. At the same
7192 time store the values in an array so that we could use them for
7193 optimizations. */
7194 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7195 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7196 if (cinfo.hashcodes == NULL)
7197 return FALSE;
7198
7199 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7200 cinfo.min_dynindx = -1;
7201 cinfo.output_bfd = output_bfd;
7202 cinfo.bed = bed;
7203
7204 /* Put all hash values in HASHCODES. */
7205 elf_link_hash_traverse (elf_hash_table (info),
7206 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7207 if (cinfo.error)
4dd07732
AM
7208 {
7209 free (cinfo.hashcodes);
7210 return FALSE;
7211 }
fdc90cb4
JJ
7212
7213 bucketcount
7214 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7215
7216 if (bucketcount == 0)
7217 {
7218 free (cinfo.hashcodes);
7219 return FALSE;
7220 }
7221
3d4d4302 7222 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7223 BFD_ASSERT (s != NULL);
7224
7225 if (cinfo.nsyms == 0)
7226 {
7227 /* Empty .gnu.hash section is special. */
7228 BFD_ASSERT (cinfo.min_dynindx == -1);
7229 free (cinfo.hashcodes);
7230 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7231 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7232 if (contents == NULL)
7233 return FALSE;
7234 s->contents = contents;
7235 /* 1 empty bucket. */
7236 bfd_put_32 (output_bfd, 1, contents);
7237 /* SYMIDX above the special symbol 0. */
7238 bfd_put_32 (output_bfd, 1, contents + 4);
7239 /* Just one word for bitmask. */
7240 bfd_put_32 (output_bfd, 1, contents + 8);
7241 /* Only hash fn bloom filter. */
7242 bfd_put_32 (output_bfd, 0, contents + 12);
7243 /* No hashes are valid - empty bitmask. */
7244 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7245 /* No hashes in the only bucket. */
7246 bfd_put_32 (output_bfd, 0,
7247 contents + 16 + bed->s->arch_size / 8);
7248 }
7249 else
7250 {
9e6619e2 7251 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7252 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7253
9e6619e2
AM
7254 x = cinfo.nsyms;
7255 maskbitslog2 = 1;
7256 while ((x >>= 1) != 0)
7257 ++maskbitslog2;
fdc90cb4
JJ
7258 if (maskbitslog2 < 3)
7259 maskbitslog2 = 5;
7260 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7261 maskbitslog2 = maskbitslog2 + 3;
7262 else
7263 maskbitslog2 = maskbitslog2 + 2;
7264 if (bed->s->arch_size == 64)
7265 {
7266 if (maskbitslog2 == 5)
7267 maskbitslog2 = 6;
7268 cinfo.shift1 = 6;
7269 }
7270 else
7271 cinfo.shift1 = 5;
7272 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7273 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7274 cinfo.maskbits = 1 << maskbitslog2;
7275 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7276 amt = bucketcount * sizeof (unsigned long int) * 2;
7277 amt += maskwords * sizeof (bfd_vma);
a50b1753 7278 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7279 if (cinfo.bitmask == NULL)
7280 {
7281 free (cinfo.hashcodes);
7282 return FALSE;
7283 }
7284
a50b1753 7285 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7286 cinfo.indx = cinfo.counts + bucketcount;
7287 cinfo.symindx = dynsymcount - cinfo.nsyms;
7288 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7289
7290 /* Determine how often each hash bucket is used. */
7291 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7292 for (i = 0; i < cinfo.nsyms; ++i)
7293 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7294
7295 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7296 if (cinfo.counts[i] != 0)
7297 {
7298 cinfo.indx[i] = cnt;
7299 cnt += cinfo.counts[i];
7300 }
7301 BFD_ASSERT (cnt == dynsymcount);
7302 cinfo.bucketcount = bucketcount;
7303 cinfo.local_indx = cinfo.min_dynindx;
7304
7305 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7306 s->size += cinfo.maskbits / 8;
a50b1753 7307 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7308 if (contents == NULL)
7309 {
7310 free (cinfo.bitmask);
7311 free (cinfo.hashcodes);
7312 return FALSE;
7313 }
7314
7315 s->contents = contents;
7316 bfd_put_32 (output_bfd, bucketcount, contents);
7317 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7318 bfd_put_32 (output_bfd, maskwords, contents + 8);
7319 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7320 contents += 16 + cinfo.maskbits / 8;
7321
7322 for (i = 0; i < bucketcount; ++i)
7323 {
7324 if (cinfo.counts[i] == 0)
7325 bfd_put_32 (output_bfd, 0, contents);
7326 else
7327 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7328 contents += 4;
7329 }
7330
7331 cinfo.contents = contents;
7332
7333 /* Renumber dynamic symbols, populate .gnu.hash section. */
7334 elf_link_hash_traverse (elf_hash_table (info),
7335 elf_renumber_gnu_hash_syms, &cinfo);
7336
7337 contents = s->contents + 16;
7338 for (i = 0; i < maskwords; ++i)
7339 {
7340 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7341 contents);
7342 contents += bed->s->arch_size / 8;
7343 }
7344
7345 free (cinfo.bitmask);
7346 free (cinfo.hashcodes);
7347 }
7348 }
5a580b3a 7349
3d4d4302 7350 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7351 BFD_ASSERT (s != NULL);
7352
4ad4eba5 7353 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7354
eea6121a 7355 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7356
7357 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7358 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7359 return FALSE;
7360 }
7361
7362 return TRUE;
7363}
4d269e42 7364\f
4d269e42
AM
7365/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7366
7367static void
7368merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7369 asection *sec)
7370{
dbaa2011
AM
7371 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7372 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7373}
7374
7375/* Finish SHF_MERGE section merging. */
7376
7377bfd_boolean
630993ec 7378_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7379{
7380 bfd *ibfd;
7381 asection *sec;
7382
7383 if (!is_elf_hash_table (info->hash))
7384 return FALSE;
7385
c72f2fb2 7386 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7387 if ((ibfd->flags & DYNAMIC) == 0
7388 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7389 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7390 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7391 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7392 if ((sec->flags & SEC_MERGE) != 0
7393 && !bfd_is_abs_section (sec->output_section))
7394 {
7395 struct bfd_elf_section_data *secdata;
7396
7397 secdata = elf_section_data (sec);
630993ec 7398 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7399 &elf_hash_table (info)->merge_info,
7400 sec, &secdata->sec_info))
7401 return FALSE;
7402 else if (secdata->sec_info)
dbaa2011 7403 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7404 }
7405
7406 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7407 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7408 merge_sections_remove_hook);
7409 return TRUE;
7410}
7411
7412/* Create an entry in an ELF linker hash table. */
7413
7414struct bfd_hash_entry *
7415_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7416 struct bfd_hash_table *table,
7417 const char *string)
7418{
7419 /* Allocate the structure if it has not already been allocated by a
7420 subclass. */
7421 if (entry == NULL)
7422 {
a50b1753 7423 entry = (struct bfd_hash_entry *)
ca4be51c 7424 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7425 if (entry == NULL)
7426 return entry;
7427 }
7428
7429 /* Call the allocation method of the superclass. */
7430 entry = _bfd_link_hash_newfunc (entry, table, string);
7431 if (entry != NULL)
7432 {
7433 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7434 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7435
7436 /* Set local fields. */
7437 ret->indx = -1;
7438 ret->dynindx = -1;
7439 ret->got = htab->init_got_refcount;
7440 ret->plt = htab->init_plt_refcount;
7441 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7442 - offsetof (struct elf_link_hash_entry, size)));
7443 /* Assume that we have been called by a non-ELF symbol reader.
7444 This flag is then reset by the code which reads an ELF input
7445 file. This ensures that a symbol created by a non-ELF symbol
7446 reader will have the flag set correctly. */
7447 ret->non_elf = 1;
7448 }
7449
7450 return entry;
7451}
7452
7453/* Copy data from an indirect symbol to its direct symbol, hiding the
7454 old indirect symbol. Also used for copying flags to a weakdef. */
7455
7456void
7457_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7458 struct elf_link_hash_entry *dir,
7459 struct elf_link_hash_entry *ind)
7460{
7461 struct elf_link_hash_table *htab;
7462
7463 /* Copy down any references that we may have already seen to the
e81830c5 7464 symbol which just became indirect. */
4d269e42 7465
422f1182 7466 if (dir->versioned != versioned_hidden)
e81830c5
AM
7467 dir->ref_dynamic |= ind->ref_dynamic;
7468 dir->ref_regular |= ind->ref_regular;
7469 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7470 dir->non_got_ref |= ind->non_got_ref;
7471 dir->needs_plt |= ind->needs_plt;
7472 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7473
7474 if (ind->root.type != bfd_link_hash_indirect)
7475 return;
7476
7477 /* Copy over the global and procedure linkage table refcount entries.
7478 These may have been already set up by a check_relocs routine. */
7479 htab = elf_hash_table (info);
7480 if (ind->got.refcount > htab->init_got_refcount.refcount)
7481 {
7482 if (dir->got.refcount < 0)
7483 dir->got.refcount = 0;
7484 dir->got.refcount += ind->got.refcount;
7485 ind->got.refcount = htab->init_got_refcount.refcount;
7486 }
7487
7488 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7489 {
7490 if (dir->plt.refcount < 0)
7491 dir->plt.refcount = 0;
7492 dir->plt.refcount += ind->plt.refcount;
7493 ind->plt.refcount = htab->init_plt_refcount.refcount;
7494 }
7495
7496 if (ind->dynindx != -1)
7497 {
7498 if (dir->dynindx != -1)
7499 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7500 dir->dynindx = ind->dynindx;
7501 dir->dynstr_index = ind->dynstr_index;
7502 ind->dynindx = -1;
7503 ind->dynstr_index = 0;
7504 }
7505}
7506
7507void
7508_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7509 struct elf_link_hash_entry *h,
7510 bfd_boolean force_local)
7511{
3aa14d16
L
7512 /* STT_GNU_IFUNC symbol must go through PLT. */
7513 if (h->type != STT_GNU_IFUNC)
7514 {
7515 h->plt = elf_hash_table (info)->init_plt_offset;
7516 h->needs_plt = 0;
7517 }
4d269e42
AM
7518 if (force_local)
7519 {
7520 h->forced_local = 1;
7521 if (h->dynindx != -1)
7522 {
4d269e42
AM
7523 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7524 h->dynstr_index);
641338d8
AM
7525 h->dynindx = -1;
7526 h->dynstr_index = 0;
4d269e42
AM
7527 }
7528 }
7529}
7530
34a87bb0
L
7531/* Hide a symbol. */
7532
7533void
7534_bfd_elf_link_hide_symbol (bfd *output_bfd,
7535 struct bfd_link_info *info,
7536 struct bfd_link_hash_entry *h)
7537{
7538 if (is_elf_hash_table (info->hash))
7539 {
7540 const struct elf_backend_data *bed
7541 = get_elf_backend_data (output_bfd);
7542 struct elf_link_hash_entry *eh
7543 = (struct elf_link_hash_entry *) h;
7544 bed->elf_backend_hide_symbol (info, eh, TRUE);
7545 eh->def_dynamic = 0;
7546 eh->ref_dynamic = 0;
7547 eh->dynamic_def = 0;
7548 }
7549}
7550
7bf52ea2
AM
7551/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7552 caller. */
4d269e42
AM
7553
7554bfd_boolean
7555_bfd_elf_link_hash_table_init
7556 (struct elf_link_hash_table *table,
7557 bfd *abfd,
7558 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7559 struct bfd_hash_table *,
7560 const char *),
4dfe6ac6
NC
7561 unsigned int entsize,
7562 enum elf_target_id target_id)
4d269e42
AM
7563{
7564 bfd_boolean ret;
7565 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7566
4d269e42
AM
7567 table->init_got_refcount.refcount = can_refcount - 1;
7568 table->init_plt_refcount.refcount = can_refcount - 1;
7569 table->init_got_offset.offset = -(bfd_vma) 1;
7570 table->init_plt_offset.offset = -(bfd_vma) 1;
7571 /* The first dynamic symbol is a dummy. */
7572 table->dynsymcount = 1;
7573
7574 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7575
4d269e42 7576 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7577 table->hash_table_id = target_id;
4d269e42
AM
7578
7579 return ret;
7580}
7581
7582/* Create an ELF linker hash table. */
7583
7584struct bfd_link_hash_table *
7585_bfd_elf_link_hash_table_create (bfd *abfd)
7586{
7587 struct elf_link_hash_table *ret;
7588 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7589
7bf52ea2 7590 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7591 if (ret == NULL)
7592 return NULL;
7593
7594 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7595 sizeof (struct elf_link_hash_entry),
7596 GENERIC_ELF_DATA))
4d269e42
AM
7597 {
7598 free (ret);
7599 return NULL;
7600 }
d495ab0d 7601 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7602
7603 return &ret->root;
7604}
7605
9f7c3e5e
AM
7606/* Destroy an ELF linker hash table. */
7607
7608void
d495ab0d 7609_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7610{
d495ab0d
AM
7611 struct elf_link_hash_table *htab;
7612
7613 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7614 if (htab->dynstr != NULL)
7615 _bfd_elf_strtab_free (htab->dynstr);
7616 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7617 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7618}
7619
4d269e42
AM
7620/* This is a hook for the ELF emulation code in the generic linker to
7621 tell the backend linker what file name to use for the DT_NEEDED
7622 entry for a dynamic object. */
7623
7624void
7625bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7626{
7627 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7628 && bfd_get_format (abfd) == bfd_object)
7629 elf_dt_name (abfd) = name;
7630}
7631
7632int
7633bfd_elf_get_dyn_lib_class (bfd *abfd)
7634{
7635 int lib_class;
7636 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7637 && bfd_get_format (abfd) == bfd_object)
7638 lib_class = elf_dyn_lib_class (abfd);
7639 else
7640 lib_class = 0;
7641 return lib_class;
7642}
7643
7644void
7645bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7646{
7647 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7648 && bfd_get_format (abfd) == bfd_object)
7649 elf_dyn_lib_class (abfd) = lib_class;
7650}
7651
7652/* Get the list of DT_NEEDED entries for a link. This is a hook for
7653 the linker ELF emulation code. */
7654
7655struct bfd_link_needed_list *
7656bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7657 struct bfd_link_info *info)
7658{
7659 if (! is_elf_hash_table (info->hash))
7660 return NULL;
7661 return elf_hash_table (info)->needed;
7662}
7663
7664/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7665 hook for the linker ELF emulation code. */
7666
7667struct bfd_link_needed_list *
7668bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7669 struct bfd_link_info *info)
7670{
7671 if (! is_elf_hash_table (info->hash))
7672 return NULL;
7673 return elf_hash_table (info)->runpath;
7674}
7675
7676/* Get the name actually used for a dynamic object for a link. This
7677 is the SONAME entry if there is one. Otherwise, it is the string
7678 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7679
7680const char *
7681bfd_elf_get_dt_soname (bfd *abfd)
7682{
7683 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7684 && bfd_get_format (abfd) == bfd_object)
7685 return elf_dt_name (abfd);
7686 return NULL;
7687}
7688
7689/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7690 the ELF linker emulation code. */
7691
7692bfd_boolean
7693bfd_elf_get_bfd_needed_list (bfd *abfd,
7694 struct bfd_link_needed_list **pneeded)
7695{
7696 asection *s;
7697 bfd_byte *dynbuf = NULL;
cb33740c 7698 unsigned int elfsec;
4d269e42
AM
7699 unsigned long shlink;
7700 bfd_byte *extdyn, *extdynend;
7701 size_t extdynsize;
7702 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7703
7704 *pneeded = NULL;
7705
7706 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7707 || bfd_get_format (abfd) != bfd_object)
7708 return TRUE;
7709
7710 s = bfd_get_section_by_name (abfd, ".dynamic");
7711 if (s == NULL || s->size == 0)
7712 return TRUE;
7713
7714 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7715 goto error_return;
7716
7717 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7718 if (elfsec == SHN_BAD)
4d269e42
AM
7719 goto error_return;
7720
7721 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7722
4d269e42
AM
7723 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7724 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7725
7726 extdyn = dynbuf;
7727 extdynend = extdyn + s->size;
7728 for (; extdyn < extdynend; extdyn += extdynsize)
7729 {
7730 Elf_Internal_Dyn dyn;
7731
7732 (*swap_dyn_in) (abfd, extdyn, &dyn);
7733
7734 if (dyn.d_tag == DT_NULL)
7735 break;
7736
7737 if (dyn.d_tag == DT_NEEDED)
7738 {
7739 const char *string;
7740 struct bfd_link_needed_list *l;
7741 unsigned int tagv = dyn.d_un.d_val;
7742 bfd_size_type amt;
7743
7744 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7745 if (string == NULL)
7746 goto error_return;
7747
7748 amt = sizeof *l;
a50b1753 7749 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7750 if (l == NULL)
7751 goto error_return;
7752
7753 l->by = abfd;
7754 l->name = string;
7755 l->next = *pneeded;
7756 *pneeded = l;
7757 }
7758 }
7759
7760 free (dynbuf);
7761
7762 return TRUE;
7763
7764 error_return:
7765 if (dynbuf != NULL)
7766 free (dynbuf);
7767 return FALSE;
7768}
7769
7770struct elf_symbuf_symbol
7771{
7772 unsigned long st_name; /* Symbol name, index in string tbl */
7773 unsigned char st_info; /* Type and binding attributes */
7774 unsigned char st_other; /* Visibilty, and target specific */
7775};
7776
7777struct elf_symbuf_head
7778{
7779 struct elf_symbuf_symbol *ssym;
ef53be89 7780 size_t count;
4d269e42
AM
7781 unsigned int st_shndx;
7782};
7783
7784struct elf_symbol
7785{
7786 union
7787 {
7788 Elf_Internal_Sym *isym;
7789 struct elf_symbuf_symbol *ssym;
7790 } u;
7791 const char *name;
7792};
7793
7794/* Sort references to symbols by ascending section number. */
7795
7796static int
7797elf_sort_elf_symbol (const void *arg1, const void *arg2)
7798{
7799 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7800 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7801
7802 return s1->st_shndx - s2->st_shndx;
7803}
7804
7805static int
7806elf_sym_name_compare (const void *arg1, const void *arg2)
7807{
7808 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7809 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7810 return strcmp (s1->name, s2->name);
7811}
7812
7813static struct elf_symbuf_head *
ef53be89 7814elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7815{
14b1c01e 7816 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7817 struct elf_symbuf_symbol *ssym;
7818 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7819 size_t i, shndx_count, total_size;
4d269e42 7820
a50b1753 7821 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7822 if (indbuf == NULL)
7823 return NULL;
7824
7825 for (ind = indbuf, i = 0; i < symcount; i++)
7826 if (isymbuf[i].st_shndx != SHN_UNDEF)
7827 *ind++ = &isymbuf[i];
7828 indbufend = ind;
7829
7830 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7831 elf_sort_elf_symbol);
7832
7833 shndx_count = 0;
7834 if (indbufend > indbuf)
7835 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7836 if (ind[0]->st_shndx != ind[1]->st_shndx)
7837 shndx_count++;
7838
3ae181ee
L
7839 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7840 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7841 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7842 if (ssymbuf == NULL)
7843 {
7844 free (indbuf);
7845 return NULL;
7846 }
7847
3ae181ee 7848 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7849 ssymbuf->ssym = NULL;
7850 ssymbuf->count = shndx_count;
7851 ssymbuf->st_shndx = 0;
7852 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7853 {
7854 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7855 {
7856 ssymhead++;
7857 ssymhead->ssym = ssym;
7858 ssymhead->count = 0;
7859 ssymhead->st_shndx = (*ind)->st_shndx;
7860 }
7861 ssym->st_name = (*ind)->st_name;
7862 ssym->st_info = (*ind)->st_info;
7863 ssym->st_other = (*ind)->st_other;
7864 ssymhead->count++;
7865 }
ef53be89 7866 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7867 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7868 == total_size));
4d269e42
AM
7869
7870 free (indbuf);
7871 return ssymbuf;
7872}
7873
7874/* Check if 2 sections define the same set of local and global
7875 symbols. */
7876
8f317e31 7877static bfd_boolean
4d269e42
AM
7878bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7879 struct bfd_link_info *info)
7880{
7881 bfd *bfd1, *bfd2;
7882 const struct elf_backend_data *bed1, *bed2;
7883 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7884 size_t symcount1, symcount2;
4d269e42
AM
7885 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7886 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7887 Elf_Internal_Sym *isym, *isymend;
7888 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7889 size_t count1, count2, i;
cb33740c 7890 unsigned int shndx1, shndx2;
4d269e42
AM
7891 bfd_boolean result;
7892
7893 bfd1 = sec1->owner;
7894 bfd2 = sec2->owner;
7895
4d269e42
AM
7896 /* Both sections have to be in ELF. */
7897 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7898 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7899 return FALSE;
7900
7901 if (elf_section_type (sec1) != elf_section_type (sec2))
7902 return FALSE;
7903
4d269e42
AM
7904 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7905 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7906 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7907 return FALSE;
7908
7909 bed1 = get_elf_backend_data (bfd1);
7910 bed2 = get_elf_backend_data (bfd2);
7911 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7912 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7913 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7914 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7915
7916 if (symcount1 == 0 || symcount2 == 0)
7917 return FALSE;
7918
7919 result = FALSE;
7920 isymbuf1 = NULL;
7921 isymbuf2 = NULL;
a50b1753
NC
7922 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7923 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7924
7925 if (ssymbuf1 == NULL)
7926 {
7927 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7928 NULL, NULL, NULL);
7929 if (isymbuf1 == NULL)
7930 goto done;
7931
7932 if (!info->reduce_memory_overheads)
7933 elf_tdata (bfd1)->symbuf = ssymbuf1
7934 = elf_create_symbuf (symcount1, isymbuf1);
7935 }
7936
7937 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7938 {
7939 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7940 NULL, NULL, NULL);
7941 if (isymbuf2 == NULL)
7942 goto done;
7943
7944 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7945 elf_tdata (bfd2)->symbuf = ssymbuf2
7946 = elf_create_symbuf (symcount2, isymbuf2);
7947 }
7948
7949 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7950 {
7951 /* Optimized faster version. */
ef53be89 7952 size_t lo, hi, mid;
4d269e42
AM
7953 struct elf_symbol *symp;
7954 struct elf_symbuf_symbol *ssym, *ssymend;
7955
7956 lo = 0;
7957 hi = ssymbuf1->count;
7958 ssymbuf1++;
7959 count1 = 0;
7960 while (lo < hi)
7961 {
7962 mid = (lo + hi) / 2;
cb33740c 7963 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7964 hi = mid;
cb33740c 7965 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7966 lo = mid + 1;
7967 else
7968 {
7969 count1 = ssymbuf1[mid].count;
7970 ssymbuf1 += mid;
7971 break;
7972 }
7973 }
7974
7975 lo = 0;
7976 hi = ssymbuf2->count;
7977 ssymbuf2++;
7978 count2 = 0;
7979 while (lo < hi)
7980 {
7981 mid = (lo + hi) / 2;
cb33740c 7982 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7983 hi = mid;
cb33740c 7984 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7985 lo = mid + 1;
7986 else
7987 {
7988 count2 = ssymbuf2[mid].count;
7989 ssymbuf2 += mid;
7990 break;
7991 }
7992 }
7993
7994 if (count1 == 0 || count2 == 0 || count1 != count2)
7995 goto done;
7996
ca4be51c
AM
7997 symtable1
7998 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7999 symtable2
8000 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8001 if (symtable1 == NULL || symtable2 == NULL)
8002 goto done;
8003
8004 symp = symtable1;
8005 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8006 ssym < ssymend; ssym++, symp++)
8007 {
8008 symp->u.ssym = ssym;
8009 symp->name = bfd_elf_string_from_elf_section (bfd1,
8010 hdr1->sh_link,
8011 ssym->st_name);
8012 }
8013
8014 symp = symtable2;
8015 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8016 ssym < ssymend; ssym++, symp++)
8017 {
8018 symp->u.ssym = ssym;
8019 symp->name = bfd_elf_string_from_elf_section (bfd2,
8020 hdr2->sh_link,
8021 ssym->st_name);
8022 }
8023
8024 /* Sort symbol by name. */
8025 qsort (symtable1, count1, sizeof (struct elf_symbol),
8026 elf_sym_name_compare);
8027 qsort (symtable2, count1, sizeof (struct elf_symbol),
8028 elf_sym_name_compare);
8029
8030 for (i = 0; i < count1; i++)
8031 /* Two symbols must have the same binding, type and name. */
8032 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8033 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8034 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8035 goto done;
8036
8037 result = TRUE;
8038 goto done;
8039 }
8040
a50b1753
NC
8041 symtable1 = (struct elf_symbol *)
8042 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8043 symtable2 = (struct elf_symbol *)
8044 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8045 if (symtable1 == NULL || symtable2 == NULL)
8046 goto done;
8047
8048 /* Count definitions in the section. */
8049 count1 = 0;
8050 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8051 if (isym->st_shndx == shndx1)
4d269e42
AM
8052 symtable1[count1++].u.isym = isym;
8053
8054 count2 = 0;
8055 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8056 if (isym->st_shndx == shndx2)
4d269e42
AM
8057 symtable2[count2++].u.isym = isym;
8058
8059 if (count1 == 0 || count2 == 0 || count1 != count2)
8060 goto done;
8061
8062 for (i = 0; i < count1; i++)
8063 symtable1[i].name
8064 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8065 symtable1[i].u.isym->st_name);
8066
8067 for (i = 0; i < count2; i++)
8068 symtable2[i].name
8069 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8070 symtable2[i].u.isym->st_name);
8071
8072 /* Sort symbol by name. */
8073 qsort (symtable1, count1, sizeof (struct elf_symbol),
8074 elf_sym_name_compare);
8075 qsort (symtable2, count1, sizeof (struct elf_symbol),
8076 elf_sym_name_compare);
8077
8078 for (i = 0; i < count1; i++)
8079 /* Two symbols must have the same binding, type and name. */
8080 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8081 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8082 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8083 goto done;
8084
8085 result = TRUE;
8086
8087done:
8088 if (symtable1)
8089 free (symtable1);
8090 if (symtable2)
8091 free (symtable2);
8092 if (isymbuf1)
8093 free (isymbuf1);
8094 if (isymbuf2)
8095 free (isymbuf2);
8096
8097 return result;
8098}
8099
8100/* Return TRUE if 2 section types are compatible. */
8101
8102bfd_boolean
8103_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8104 bfd *bbfd, const asection *bsec)
8105{
8106 if (asec == NULL
8107 || bsec == NULL
8108 || abfd->xvec->flavour != bfd_target_elf_flavour
8109 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8110 return TRUE;
8111
8112 return elf_section_type (asec) == elf_section_type (bsec);
8113}
8114\f
c152c796
AM
8115/* Final phase of ELF linker. */
8116
8117/* A structure we use to avoid passing large numbers of arguments. */
8118
8119struct elf_final_link_info
8120{
8121 /* General link information. */
8122 struct bfd_link_info *info;
8123 /* Output BFD. */
8124 bfd *output_bfd;
8125 /* Symbol string table. */
ef10c3ac 8126 struct elf_strtab_hash *symstrtab;
c152c796
AM
8127 /* .hash section. */
8128 asection *hash_sec;
8129 /* symbol version section (.gnu.version). */
8130 asection *symver_sec;
8131 /* Buffer large enough to hold contents of any section. */
8132 bfd_byte *contents;
8133 /* Buffer large enough to hold external relocs of any section. */
8134 void *external_relocs;
8135 /* Buffer large enough to hold internal relocs of any section. */
8136 Elf_Internal_Rela *internal_relocs;
8137 /* Buffer large enough to hold external local symbols of any input
8138 BFD. */
8139 bfd_byte *external_syms;
8140 /* And a buffer for symbol section indices. */
8141 Elf_External_Sym_Shndx *locsym_shndx;
8142 /* Buffer large enough to hold internal local symbols of any input
8143 BFD. */
8144 Elf_Internal_Sym *internal_syms;
8145 /* Array large enough to hold a symbol index for each local symbol
8146 of any input BFD. */
8147 long *indices;
8148 /* Array large enough to hold a section pointer for each local
8149 symbol of any input BFD. */
8150 asection **sections;
ef10c3ac 8151 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8152 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8153 /* Number of STT_FILE syms seen. */
8154 size_t filesym_count;
c152c796
AM
8155};
8156
8157/* This struct is used to pass information to elf_link_output_extsym. */
8158
8159struct elf_outext_info
8160{
8161 bfd_boolean failed;
8162 bfd_boolean localsyms;
34a79995 8163 bfd_boolean file_sym_done;
8b127cbc 8164 struct elf_final_link_info *flinfo;
c152c796
AM
8165};
8166
d9352518
DB
8167
8168/* Support for evaluating a complex relocation.
8169
8170 Complex relocations are generalized, self-describing relocations. The
8171 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8172 relocations themselves.
d9352518
DB
8173
8174 The relocations are use a reserved elf-wide relocation type code (R_RELC
8175 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8176 information (start bit, end bit, word width, etc) into the addend. This
8177 information is extracted from CGEN-generated operand tables within gas.
8178
8179 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8180 internal) representing prefix-notation expressions, including but not
8181 limited to those sorts of expressions normally encoded as addends in the
8182 addend field. The symbol mangling format is:
8183
8184 <node> := <literal>
07d6d2b8
AM
8185 | <unary-operator> ':' <node>
8186 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8187 ;
8188
8189 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8190 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8191 | '#' <hexdigits>
8192 ;
8193
8194 <binary-operator> := as in C
8195 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8196
8197static void
a0c8462f
AM
8198set_symbol_value (bfd *bfd_with_globals,
8199 Elf_Internal_Sym *isymbuf,
8200 size_t locsymcount,
8201 size_t symidx,
8202 bfd_vma val)
d9352518 8203{
8977835c
AM
8204 struct elf_link_hash_entry **sym_hashes;
8205 struct elf_link_hash_entry *h;
8206 size_t extsymoff = locsymcount;
d9352518 8207
8977835c 8208 if (symidx < locsymcount)
d9352518 8209 {
8977835c
AM
8210 Elf_Internal_Sym *sym;
8211
8212 sym = isymbuf + symidx;
8213 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8214 {
8215 /* It is a local symbol: move it to the
8216 "absolute" section and give it a value. */
8217 sym->st_shndx = SHN_ABS;
8218 sym->st_value = val;
8219 return;
8220 }
8221 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8222 extsymoff = 0;
d9352518 8223 }
8977835c
AM
8224
8225 /* It is a global symbol: set its link type
8226 to "defined" and give it a value. */
8227
8228 sym_hashes = elf_sym_hashes (bfd_with_globals);
8229 h = sym_hashes [symidx - extsymoff];
8230 while (h->root.type == bfd_link_hash_indirect
8231 || h->root.type == bfd_link_hash_warning)
8232 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8233 h->root.type = bfd_link_hash_defined;
8234 h->root.u.def.value = val;
8235 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8236}
8237
a0c8462f
AM
8238static bfd_boolean
8239resolve_symbol (const char *name,
8240 bfd *input_bfd,
8b127cbc 8241 struct elf_final_link_info *flinfo,
a0c8462f
AM
8242 bfd_vma *result,
8243 Elf_Internal_Sym *isymbuf,
8244 size_t locsymcount)
d9352518 8245{
a0c8462f
AM
8246 Elf_Internal_Sym *sym;
8247 struct bfd_link_hash_entry *global_entry;
8248 const char *candidate = NULL;
8249 Elf_Internal_Shdr *symtab_hdr;
8250 size_t i;
8251
d9352518
DB
8252 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8253
8254 for (i = 0; i < locsymcount; ++ i)
8255 {
8977835c 8256 sym = isymbuf + i;
d9352518
DB
8257
8258 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8259 continue;
8260
8261 candidate = bfd_elf_string_from_elf_section (input_bfd,
8262 symtab_hdr->sh_link,
8263 sym->st_name);
8264#ifdef DEBUG
0f02bbd9
AM
8265 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8266 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8267#endif
8268 if (candidate && strcmp (candidate, name) == 0)
8269 {
8b127cbc 8270 asection *sec = flinfo->sections [i];
d9352518 8271
0f02bbd9
AM
8272 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8273 *result += sec->output_offset + sec->output_section->vma;
d9352518 8274#ifdef DEBUG
0f02bbd9
AM
8275 printf ("Found symbol with value %8.8lx\n",
8276 (unsigned long) *result);
d9352518
DB
8277#endif
8278 return TRUE;
8279 }
8280 }
8281
8282 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8283 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8284 FALSE, FALSE, TRUE);
d9352518
DB
8285 if (!global_entry)
8286 return FALSE;
a0c8462f 8287
d9352518
DB
8288 if (global_entry->type == bfd_link_hash_defined
8289 || global_entry->type == bfd_link_hash_defweak)
8290 {
a0c8462f
AM
8291 *result = (global_entry->u.def.value
8292 + global_entry->u.def.section->output_section->vma
8293 + global_entry->u.def.section->output_offset);
d9352518 8294#ifdef DEBUG
0f02bbd9
AM
8295 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8296 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8297#endif
8298 return TRUE;
a0c8462f 8299 }
d9352518 8300
d9352518
DB
8301 return FALSE;
8302}
8303
37b01f6a
DG
8304/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8305 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8306 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8307
d9352518 8308static bfd_boolean
a0c8462f
AM
8309resolve_section (const char *name,
8310 asection *sections,
37b01f6a
DG
8311 bfd_vma *result,
8312 bfd * abfd)
d9352518 8313{
a0c8462f
AM
8314 asection *curr;
8315 unsigned int len;
d9352518 8316
a0c8462f 8317 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8318 if (strcmp (curr->name, name) == 0)
8319 {
8320 *result = curr->vma;
8321 return TRUE;
8322 }
8323
8324 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8325 /* FIXME: This could be coded more efficiently... */
a0c8462f 8326 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8327 {
8328 len = strlen (curr->name);
a0c8462f 8329 if (len > strlen (name))
d9352518
DB
8330 continue;
8331
8332 if (strncmp (curr->name, name, len) == 0)
8333 {
8334 if (strncmp (".end", name + len, 4) == 0)
8335 {
37b01f6a 8336 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8337 return TRUE;
8338 }
8339
8340 /* Insert more pseudo-section names here, if you like. */
8341 }
8342 }
a0c8462f 8343
d9352518
DB
8344 return FALSE;
8345}
8346
8347static void
a0c8462f 8348undefined_reference (const char *reftype, const char *name)
d9352518 8349{
695344c0 8350 /* xgettext:c-format */
a0c8462f
AM
8351 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8352 reftype, name);
d9352518
DB
8353}
8354
8355static bfd_boolean
a0c8462f
AM
8356eval_symbol (bfd_vma *result,
8357 const char **symp,
8358 bfd *input_bfd,
8b127cbc 8359 struct elf_final_link_info *flinfo,
a0c8462f
AM
8360 bfd_vma dot,
8361 Elf_Internal_Sym *isymbuf,
8362 size_t locsymcount,
8363 int signed_p)
d9352518 8364{
4b93929b
NC
8365 size_t len;
8366 size_t symlen;
a0c8462f
AM
8367 bfd_vma a;
8368 bfd_vma b;
4b93929b 8369 char symbuf[4096];
0f02bbd9 8370 const char *sym = *symp;
a0c8462f
AM
8371 const char *symend;
8372 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8373
8374 len = strlen (sym);
8375 symend = sym + len;
8376
4b93929b 8377 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8378 {
8379 bfd_set_error (bfd_error_invalid_operation);
8380 return FALSE;
8381 }
a0c8462f 8382
d9352518
DB
8383 switch (* sym)
8384 {
8385 case '.':
0f02bbd9
AM
8386 *result = dot;
8387 *symp = sym + 1;
d9352518
DB
8388 return TRUE;
8389
8390 case '#':
0f02bbd9
AM
8391 ++sym;
8392 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8393 return TRUE;
8394
8395 case 'S':
8396 symbol_is_section = TRUE;
1a0670f3 8397 /* Fall through. */
a0c8462f 8398 case 's':
0f02bbd9
AM
8399 ++sym;
8400 symlen = strtol (sym, (char **) symp, 10);
8401 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8402
4b93929b 8403 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8404 {
8405 bfd_set_error (bfd_error_invalid_operation);
8406 return FALSE;
8407 }
8408
8409 memcpy (symbuf, sym, symlen);
a0c8462f 8410 symbuf[symlen] = '\0';
0f02bbd9 8411 *symp = sym + symlen;
a0c8462f
AM
8412
8413 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8414 the symbol as a section, or vice-versa. so we're pretty liberal in our
8415 interpretation here; section means "try section first", not "must be a
8416 section", and likewise with symbol. */
8417
a0c8462f 8418 if (symbol_is_section)
d9352518 8419 {
37b01f6a 8420 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8421 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8422 isymbuf, locsymcount))
d9352518
DB
8423 {
8424 undefined_reference ("section", symbuf);
8425 return FALSE;
8426 }
a0c8462f
AM
8427 }
8428 else
d9352518 8429 {
8b127cbc 8430 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8431 isymbuf, locsymcount)
8b127cbc 8432 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8433 result, input_bfd))
d9352518
DB
8434 {
8435 undefined_reference ("symbol", symbuf);
8436 return FALSE;
8437 }
8438 }
8439
8440 return TRUE;
a0c8462f 8441
d9352518
DB
8442 /* All that remains are operators. */
8443
8444#define UNARY_OP(op) \
8445 if (strncmp (sym, #op, strlen (#op)) == 0) \
8446 { \
8447 sym += strlen (#op); \
a0c8462f
AM
8448 if (*sym == ':') \
8449 ++sym; \
0f02bbd9 8450 *symp = sym; \
8b127cbc 8451 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8452 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8453 return FALSE; \
8454 if (signed_p) \
0f02bbd9 8455 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8456 else \
8457 *result = op a; \
d9352518
DB
8458 return TRUE; \
8459 }
8460
8461#define BINARY_OP(op) \
8462 if (strncmp (sym, #op, strlen (#op)) == 0) \
8463 { \
8464 sym += strlen (#op); \
a0c8462f
AM
8465 if (*sym == ':') \
8466 ++sym; \
0f02bbd9 8467 *symp = sym; \
8b127cbc 8468 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8469 isymbuf, locsymcount, signed_p)) \
a0c8462f 8470 return FALSE; \
0f02bbd9 8471 ++*symp; \
8b127cbc 8472 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8473 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8474 return FALSE; \
8475 if (signed_p) \
0f02bbd9 8476 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8477 else \
8478 *result = a op b; \
d9352518
DB
8479 return TRUE; \
8480 }
8481
8482 default:
8483 UNARY_OP (0-);
8484 BINARY_OP (<<);
8485 BINARY_OP (>>);
8486 BINARY_OP (==);
8487 BINARY_OP (!=);
8488 BINARY_OP (<=);
8489 BINARY_OP (>=);
8490 BINARY_OP (&&);
8491 BINARY_OP (||);
8492 UNARY_OP (~);
8493 UNARY_OP (!);
8494 BINARY_OP (*);
8495 BINARY_OP (/);
8496 BINARY_OP (%);
8497 BINARY_OP (^);
8498 BINARY_OP (|);
8499 BINARY_OP (&);
8500 BINARY_OP (+);
8501 BINARY_OP (-);
8502 BINARY_OP (<);
8503 BINARY_OP (>);
8504#undef UNARY_OP
8505#undef BINARY_OP
8506 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8507 bfd_set_error (bfd_error_invalid_operation);
8508 return FALSE;
8509 }
8510}
8511
d9352518 8512static void
a0c8462f
AM
8513put_value (bfd_vma size,
8514 unsigned long chunksz,
8515 bfd *input_bfd,
8516 bfd_vma x,
8517 bfd_byte *location)
d9352518
DB
8518{
8519 location += (size - chunksz);
8520
41cd1ad1 8521 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8522 {
8523 switch (chunksz)
8524 {
d9352518
DB
8525 case 1:
8526 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8527 x >>= 8;
d9352518
DB
8528 break;
8529 case 2:
8530 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8531 x >>= 16;
d9352518
DB
8532 break;
8533 case 4:
8534 bfd_put_32 (input_bfd, x, location);
65164438
NC
8535 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8536 x >>= 16;
8537 x >>= 16;
d9352518 8538 break;
d9352518 8539#ifdef BFD64
41cd1ad1 8540 case 8:
d9352518 8541 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8542 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8543 x >>= 32;
8544 x >>= 32;
8545 break;
d9352518 8546#endif
41cd1ad1
NC
8547 default:
8548 abort ();
d9352518
DB
8549 break;
8550 }
8551 }
8552}
8553
a0c8462f
AM
8554static bfd_vma
8555get_value (bfd_vma size,
8556 unsigned long chunksz,
8557 bfd *input_bfd,
8558 bfd_byte *location)
d9352518 8559{
9b239e0e 8560 int shift;
d9352518
DB
8561 bfd_vma x = 0;
8562
9b239e0e
NC
8563 /* Sanity checks. */
8564 BFD_ASSERT (chunksz <= sizeof (x)
8565 && size >= chunksz
8566 && chunksz != 0
8567 && (size % chunksz) == 0
8568 && input_bfd != NULL
8569 && location != NULL);
8570
8571 if (chunksz == sizeof (x))
8572 {
8573 BFD_ASSERT (size == chunksz);
8574
8575 /* Make sure that we do not perform an undefined shift operation.
8576 We know that size == chunksz so there will only be one iteration
8577 of the loop below. */
8578 shift = 0;
8579 }
8580 else
8581 shift = 8 * chunksz;
8582
a0c8462f 8583 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8584 {
8585 switch (chunksz)
8586 {
d9352518 8587 case 1:
9b239e0e 8588 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8589 break;
8590 case 2:
9b239e0e 8591 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8592 break;
8593 case 4:
9b239e0e 8594 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8595 break;
d9352518 8596#ifdef BFD64
9b239e0e
NC
8597 case 8:
8598 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8599 break;
9b239e0e
NC
8600#endif
8601 default:
8602 abort ();
d9352518
DB
8603 }
8604 }
8605 return x;
8606}
8607
a0c8462f
AM
8608static void
8609decode_complex_addend (unsigned long *start, /* in bits */
8610 unsigned long *oplen, /* in bits */
8611 unsigned long *len, /* in bits */
8612 unsigned long *wordsz, /* in bytes */
8613 unsigned long *chunksz, /* in bytes */
8614 unsigned long *lsb0_p,
8615 unsigned long *signed_p,
8616 unsigned long *trunc_p,
8617 unsigned long encoded)
d9352518 8618{
07d6d2b8
AM
8619 * start = encoded & 0x3F;
8620 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8621 * oplen = (encoded >> 12) & 0x3F;
8622 * wordsz = (encoded >> 18) & 0xF;
8623 * chunksz = (encoded >> 22) & 0xF;
8624 * lsb0_p = (encoded >> 27) & 1;
8625 * signed_p = (encoded >> 28) & 1;
8626 * trunc_p = (encoded >> 29) & 1;
8627}
8628
cdfeee4f 8629bfd_reloc_status_type
0f02bbd9 8630bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8631 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8632 bfd_byte *contents,
8633 Elf_Internal_Rela *rel,
8634 bfd_vma relocation)
d9352518 8635{
0f02bbd9
AM
8636 bfd_vma shift, x, mask;
8637 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8638 bfd_reloc_status_type r;
d9352518
DB
8639
8640 /* Perform this reloc, since it is complex.
8641 (this is not to say that it necessarily refers to a complex
8642 symbol; merely that it is a self-describing CGEN based reloc.
8643 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8644 word size, etc) encoded within it.). */
d9352518 8645
a0c8462f
AM
8646 decode_complex_addend (&start, &oplen, &len, &wordsz,
8647 &chunksz, &lsb0_p, &signed_p,
8648 &trunc_p, rel->r_addend);
d9352518
DB
8649
8650 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8651
8652 if (lsb0_p)
8653 shift = (start + 1) - len;
8654 else
8655 shift = (8 * wordsz) - (start + len);
8656
37b01f6a
DG
8657 x = get_value (wordsz, chunksz, input_bfd,
8658 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8659
8660#ifdef DEBUG
8661 printf ("Doing complex reloc: "
8662 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8663 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8664 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8665 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8666 oplen, (unsigned long) x, (unsigned long) mask,
8667 (unsigned long) relocation);
d9352518
DB
8668#endif
8669
cdfeee4f 8670 r = bfd_reloc_ok;
d9352518 8671 if (! trunc_p)
cdfeee4f
AM
8672 /* Now do an overflow check. */
8673 r = bfd_check_overflow ((signed_p
8674 ? complain_overflow_signed
8675 : complain_overflow_unsigned),
8676 len, 0, (8 * wordsz),
8677 relocation);
a0c8462f 8678
d9352518
DB
8679 /* Do the deed. */
8680 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8681
8682#ifdef DEBUG
8683 printf (" relocation: %8.8lx\n"
8684 " shifted mask: %8.8lx\n"
8685 " shifted/masked reloc: %8.8lx\n"
8686 " result: %8.8lx\n",
9ccb8af9
AM
8687 (unsigned long) relocation, (unsigned long) (mask << shift),
8688 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8689#endif
37b01f6a
DG
8690 put_value (wordsz, chunksz, input_bfd, x,
8691 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8692 return r;
d9352518
DB
8693}
8694
0e287786
AM
8695/* Functions to read r_offset from external (target order) reloc
8696 entry. Faster than bfd_getl32 et al, because we let the compiler
8697 know the value is aligned. */
53df40a4 8698
0e287786
AM
8699static bfd_vma
8700ext32l_r_offset (const void *p)
53df40a4
AM
8701{
8702 union aligned32
8703 {
8704 uint32_t v;
8705 unsigned char c[4];
8706 };
8707 const union aligned32 *a
0e287786 8708 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8709
8710 uint32_t aval = ( (uint32_t) a->c[0]
8711 | (uint32_t) a->c[1] << 8
8712 | (uint32_t) a->c[2] << 16
8713 | (uint32_t) a->c[3] << 24);
0e287786 8714 return aval;
53df40a4
AM
8715}
8716
0e287786
AM
8717static bfd_vma
8718ext32b_r_offset (const void *p)
53df40a4
AM
8719{
8720 union aligned32
8721 {
8722 uint32_t v;
8723 unsigned char c[4];
8724 };
8725 const union aligned32 *a
0e287786 8726 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8727
8728 uint32_t aval = ( (uint32_t) a->c[0] << 24
8729 | (uint32_t) a->c[1] << 16
8730 | (uint32_t) a->c[2] << 8
8731 | (uint32_t) a->c[3]);
0e287786 8732 return aval;
53df40a4
AM
8733}
8734
8735#ifdef BFD_HOST_64_BIT
0e287786
AM
8736static bfd_vma
8737ext64l_r_offset (const void *p)
53df40a4
AM
8738{
8739 union aligned64
8740 {
8741 uint64_t v;
8742 unsigned char c[8];
8743 };
8744 const union aligned64 *a
0e287786 8745 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8746
8747 uint64_t aval = ( (uint64_t) a->c[0]
8748 | (uint64_t) a->c[1] << 8
8749 | (uint64_t) a->c[2] << 16
8750 | (uint64_t) a->c[3] << 24
8751 | (uint64_t) a->c[4] << 32
8752 | (uint64_t) a->c[5] << 40
8753 | (uint64_t) a->c[6] << 48
8754 | (uint64_t) a->c[7] << 56);
0e287786 8755 return aval;
53df40a4
AM
8756}
8757
0e287786
AM
8758static bfd_vma
8759ext64b_r_offset (const void *p)
53df40a4
AM
8760{
8761 union aligned64
8762 {
8763 uint64_t v;
8764 unsigned char c[8];
8765 };
8766 const union aligned64 *a
0e287786 8767 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8768
8769 uint64_t aval = ( (uint64_t) a->c[0] << 56
8770 | (uint64_t) a->c[1] << 48
8771 | (uint64_t) a->c[2] << 40
8772 | (uint64_t) a->c[3] << 32
8773 | (uint64_t) a->c[4] << 24
8774 | (uint64_t) a->c[5] << 16
8775 | (uint64_t) a->c[6] << 8
8776 | (uint64_t) a->c[7]);
0e287786 8777 return aval;
53df40a4
AM
8778}
8779#endif
8780
c152c796
AM
8781/* When performing a relocatable link, the input relocations are
8782 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8783 referenced must be updated. Update all the relocations found in
8784 RELDATA. */
c152c796 8785
bca6d0e3 8786static bfd_boolean
c152c796 8787elf_link_adjust_relocs (bfd *abfd,
9eaff861 8788 asection *sec,
28dbcedc 8789 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8790 bfd_boolean sort,
8791 struct bfd_link_info *info)
c152c796
AM
8792{
8793 unsigned int i;
8794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8795 bfd_byte *erela;
8796 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8797 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8798 bfd_vma r_type_mask;
8799 int r_sym_shift;
d4730f92
BS
8800 unsigned int count = reldata->count;
8801 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8802
d4730f92 8803 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8804 {
8805 swap_in = bed->s->swap_reloc_in;
8806 swap_out = bed->s->swap_reloc_out;
8807 }
d4730f92 8808 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8809 {
8810 swap_in = bed->s->swap_reloca_in;
8811 swap_out = bed->s->swap_reloca_out;
8812 }
8813 else
8814 abort ();
8815
8816 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8817 abort ();
8818
8819 if (bed->s->arch_size == 32)
8820 {
8821 r_type_mask = 0xff;
8822 r_sym_shift = 8;
8823 }
8824 else
8825 {
8826 r_type_mask = 0xffffffff;
8827 r_sym_shift = 32;
8828 }
8829
d4730f92
BS
8830 erela = reldata->hdr->contents;
8831 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8832 {
8833 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8834 unsigned int j;
8835
8836 if (*rel_hash == NULL)
8837 continue;
8838
10bbbc1d
NC
8839 if ((*rel_hash)->indx == -2
8840 && info->gc_sections
8841 && ! info->gc_keep_exported)
8842 {
8843 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8844 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8845 abfd, sec,
8846 (*rel_hash)->root.root.string);
9793eb77 8847 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8848 abfd, sec);
10bbbc1d
NC
8849 bfd_set_error (bfd_error_invalid_operation);
8850 return FALSE;
8851 }
c152c796
AM
8852 BFD_ASSERT ((*rel_hash)->indx >= 0);
8853
8854 (*swap_in) (abfd, erela, irela);
8855 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8856 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8857 | (irela[j].r_info & r_type_mask));
8858 (*swap_out) (abfd, irela, erela);
8859 }
53df40a4 8860
9eaff861
AO
8861 if (bed->elf_backend_update_relocs)
8862 (*bed->elf_backend_update_relocs) (sec, reldata);
8863
0e287786 8864 if (sort && count != 0)
53df40a4 8865 {
0e287786
AM
8866 bfd_vma (*ext_r_off) (const void *);
8867 bfd_vma r_off;
8868 size_t elt_size;
8869 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8870 bfd_byte *buf = NULL;
28dbcedc
AM
8871
8872 if (bed->s->arch_size == 32)
8873 {
8874 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8875 ext_r_off = ext32l_r_offset;
28dbcedc 8876 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8877 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8878 else
8879 abort ();
8880 }
53df40a4 8881 else
28dbcedc 8882 {
53df40a4 8883#ifdef BFD_HOST_64_BIT
28dbcedc 8884 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8885 ext_r_off = ext64l_r_offset;
28dbcedc 8886 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8887 ext_r_off = ext64b_r_offset;
28dbcedc 8888 else
53df40a4 8889#endif
28dbcedc
AM
8890 abort ();
8891 }
0e287786 8892
bca6d0e3
AM
8893 /* Must use a stable sort here. A modified insertion sort,
8894 since the relocs are mostly sorted already. */
0e287786
AM
8895 elt_size = reldata->hdr->sh_entsize;
8896 base = reldata->hdr->contents;
8897 end = base + count * elt_size;
bca6d0e3 8898 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8899 abort ();
8900
8901 /* Ensure the first element is lowest. This acts as a sentinel,
8902 speeding the main loop below. */
8903 r_off = (*ext_r_off) (base);
8904 for (p = loc = base; (p += elt_size) < end; )
8905 {
8906 bfd_vma r_off2 = (*ext_r_off) (p);
8907 if (r_off > r_off2)
8908 {
8909 r_off = r_off2;
8910 loc = p;
8911 }
8912 }
8913 if (loc != base)
8914 {
8915 /* Don't just swap *base and *loc as that changes the order
8916 of the original base[0] and base[1] if they happen to
8917 have the same r_offset. */
bca6d0e3
AM
8918 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8919 memcpy (onebuf, loc, elt_size);
0e287786 8920 memmove (base + elt_size, base, loc - base);
bca6d0e3 8921 memcpy (base, onebuf, elt_size);
0e287786
AM
8922 }
8923
b29b8669 8924 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8925 {
8926 /* base to p is sorted, *p is next to insert. */
8927 r_off = (*ext_r_off) (p);
8928 /* Search the sorted region for location to insert. */
8929 loc = p - elt_size;
8930 while (r_off < (*ext_r_off) (loc))
8931 loc -= elt_size;
8932 loc += elt_size;
8933 if (loc != p)
8934 {
bca6d0e3
AM
8935 /* Chances are there is a run of relocs to insert here,
8936 from one of more input files. Files are not always
8937 linked in order due to the way elf_link_input_bfd is
8938 called. See pr17666. */
8939 size_t sortlen = p - loc;
8940 bfd_vma r_off2 = (*ext_r_off) (loc);
8941 size_t runlen = elt_size;
8942 size_t buf_size = 96 * 1024;
8943 while (p + runlen < end
8944 && (sortlen <= buf_size
8945 || runlen + elt_size <= buf_size)
8946 && r_off2 > (*ext_r_off) (p + runlen))
8947 runlen += elt_size;
8948 if (buf == NULL)
8949 {
8950 buf = bfd_malloc (buf_size);
8951 if (buf == NULL)
8952 return FALSE;
8953 }
8954 if (runlen < sortlen)
8955 {
8956 memcpy (buf, p, runlen);
8957 memmove (loc + runlen, loc, sortlen);
8958 memcpy (loc, buf, runlen);
8959 }
8960 else
8961 {
8962 memcpy (buf, loc, sortlen);
8963 memmove (loc, p, runlen);
8964 memcpy (loc + runlen, buf, sortlen);
8965 }
b29b8669 8966 p += runlen - elt_size;
0e287786
AM
8967 }
8968 }
8969 /* Hashes are no longer valid. */
28dbcedc
AM
8970 free (reldata->hashes);
8971 reldata->hashes = NULL;
bca6d0e3 8972 free (buf);
53df40a4 8973 }
bca6d0e3 8974 return TRUE;
c152c796
AM
8975}
8976
8977struct elf_link_sort_rela
8978{
8979 union {
8980 bfd_vma offset;
8981 bfd_vma sym_mask;
8982 } u;
8983 enum elf_reloc_type_class type;
8984 /* We use this as an array of size int_rels_per_ext_rel. */
8985 Elf_Internal_Rela rela[1];
8986};
8987
8988static int
8989elf_link_sort_cmp1 (const void *A, const void *B)
8990{
a50b1753
NC
8991 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8992 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8993 int relativea, relativeb;
8994
8995 relativea = a->type == reloc_class_relative;
8996 relativeb = b->type == reloc_class_relative;
8997
8998 if (relativea < relativeb)
8999 return 1;
9000 if (relativea > relativeb)
9001 return -1;
9002 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9003 return -1;
9004 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9005 return 1;
9006 if (a->rela->r_offset < b->rela->r_offset)
9007 return -1;
9008 if (a->rela->r_offset > b->rela->r_offset)
9009 return 1;
9010 return 0;
9011}
9012
9013static int
9014elf_link_sort_cmp2 (const void *A, const void *B)
9015{
a50b1753
NC
9016 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9017 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9018
7e612e98 9019 if (a->type < b->type)
c152c796 9020 return -1;
7e612e98 9021 if (a->type > b->type)
c152c796 9022 return 1;
7e612e98 9023 if (a->u.offset < b->u.offset)
c152c796 9024 return -1;
7e612e98 9025 if (a->u.offset > b->u.offset)
c152c796
AM
9026 return 1;
9027 if (a->rela->r_offset < b->rela->r_offset)
9028 return -1;
9029 if (a->rela->r_offset > b->rela->r_offset)
9030 return 1;
9031 return 0;
9032}
9033
9034static size_t
9035elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9036{
3410fea8 9037 asection *dynamic_relocs;
fc66a176
L
9038 asection *rela_dyn;
9039 asection *rel_dyn;
c152c796
AM
9040 bfd_size_type count, size;
9041 size_t i, ret, sort_elt, ext_size;
9042 bfd_byte *sort, *s_non_relative, *p;
9043 struct elf_link_sort_rela *sq;
9044 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9045 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9046 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9047 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9048 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9049 struct bfd_link_order *lo;
9050 bfd_vma r_sym_mask;
3410fea8 9051 bfd_boolean use_rela;
c152c796 9052
3410fea8
NC
9053 /* Find a dynamic reloc section. */
9054 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9055 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9056 if (rela_dyn != NULL && rela_dyn->size > 0
9057 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9058 {
3410fea8
NC
9059 bfd_boolean use_rela_initialised = FALSE;
9060
9061 /* This is just here to stop gcc from complaining.
c8e44c6d 9062 Its initialization checking code is not perfect. */
3410fea8
NC
9063 use_rela = TRUE;
9064
9065 /* Both sections are present. Examine the sizes
9066 of the indirect sections to help us choose. */
9067 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9068 if (lo->type == bfd_indirect_link_order)
9069 {
9070 asection *o = lo->u.indirect.section;
9071
9072 if ((o->size % bed->s->sizeof_rela) == 0)
9073 {
9074 if ((o->size % bed->s->sizeof_rel) == 0)
9075 /* Section size is divisible by both rel and rela sizes.
9076 It is of no help to us. */
9077 ;
9078 else
9079 {
9080 /* Section size is only divisible by rela. */
535b785f 9081 if (use_rela_initialised && !use_rela)
3410fea8 9082 {
9793eb77 9083 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9084 "they are in more than one size"),
9085 abfd);
3410fea8
NC
9086 bfd_set_error (bfd_error_invalid_operation);
9087 return 0;
9088 }
9089 else
9090 {
9091 use_rela = TRUE;
9092 use_rela_initialised = TRUE;
9093 }
9094 }
9095 }
9096 else if ((o->size % bed->s->sizeof_rel) == 0)
9097 {
9098 /* Section size is only divisible by rel. */
535b785f 9099 if (use_rela_initialised && use_rela)
3410fea8 9100 {
9793eb77 9101 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9102 "they are in more than one size"),
9103 abfd);
3410fea8
NC
9104 bfd_set_error (bfd_error_invalid_operation);
9105 return 0;
9106 }
9107 else
9108 {
9109 use_rela = FALSE;
9110 use_rela_initialised = TRUE;
9111 }
9112 }
9113 else
9114 {
c8e44c6d
AM
9115 /* The section size is not divisible by either -
9116 something is wrong. */
9793eb77 9117 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9118 "they are of an unknown size"), abfd);
3410fea8
NC
9119 bfd_set_error (bfd_error_invalid_operation);
9120 return 0;
9121 }
9122 }
9123
9124 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9125 if (lo->type == bfd_indirect_link_order)
9126 {
9127 asection *o = lo->u.indirect.section;
9128
9129 if ((o->size % bed->s->sizeof_rela) == 0)
9130 {
9131 if ((o->size % bed->s->sizeof_rel) == 0)
9132 /* Section size is divisible by both rel and rela sizes.
9133 It is of no help to us. */
9134 ;
9135 else
9136 {
9137 /* Section size is only divisible by rela. */
535b785f 9138 if (use_rela_initialised && !use_rela)
3410fea8 9139 {
9793eb77 9140 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9141 "they are in more than one size"),
9142 abfd);
3410fea8
NC
9143 bfd_set_error (bfd_error_invalid_operation);
9144 return 0;
9145 }
9146 else
9147 {
9148 use_rela = TRUE;
9149 use_rela_initialised = TRUE;
9150 }
9151 }
9152 }
9153 else if ((o->size % bed->s->sizeof_rel) == 0)
9154 {
9155 /* Section size is only divisible by rel. */
535b785f 9156 if (use_rela_initialised && use_rela)
3410fea8 9157 {
9793eb77 9158 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9159 "they are in more than one size"),
9160 abfd);
3410fea8
NC
9161 bfd_set_error (bfd_error_invalid_operation);
9162 return 0;
9163 }
9164 else
9165 {
9166 use_rela = FALSE;
9167 use_rela_initialised = TRUE;
9168 }
9169 }
9170 else
9171 {
c8e44c6d
AM
9172 /* The section size is not divisible by either -
9173 something is wrong. */
9793eb77 9174 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9175 "they are of an unknown size"), abfd);
3410fea8
NC
9176 bfd_set_error (bfd_error_invalid_operation);
9177 return 0;
9178 }
9179 }
9180
9181 if (! use_rela_initialised)
9182 /* Make a guess. */
9183 use_rela = TRUE;
c152c796 9184 }
fc66a176
L
9185 else if (rela_dyn != NULL && rela_dyn->size > 0)
9186 use_rela = TRUE;
9187 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9188 use_rela = FALSE;
c152c796 9189 else
fc66a176 9190 return 0;
3410fea8
NC
9191
9192 if (use_rela)
c152c796 9193 {
3410fea8 9194 dynamic_relocs = rela_dyn;
c152c796
AM
9195 ext_size = bed->s->sizeof_rela;
9196 swap_in = bed->s->swap_reloca_in;
9197 swap_out = bed->s->swap_reloca_out;
9198 }
3410fea8
NC
9199 else
9200 {
9201 dynamic_relocs = rel_dyn;
9202 ext_size = bed->s->sizeof_rel;
9203 swap_in = bed->s->swap_reloc_in;
9204 swap_out = bed->s->swap_reloc_out;
9205 }
c152c796
AM
9206
9207 size = 0;
3410fea8 9208 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9209 if (lo->type == bfd_indirect_link_order)
3410fea8 9210 size += lo->u.indirect.section->size;
c152c796 9211
3410fea8 9212 if (size != dynamic_relocs->size)
c152c796
AM
9213 return 0;
9214
9215 sort_elt = (sizeof (struct elf_link_sort_rela)
9216 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9217
9218 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9219 if (count == 0)
9220 return 0;
a50b1753 9221 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9222
c152c796
AM
9223 if (sort == NULL)
9224 {
9225 (*info->callbacks->warning)
9793eb77 9226 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9227 return 0;
9228 }
9229
9230 if (bed->s->arch_size == 32)
9231 r_sym_mask = ~(bfd_vma) 0xff;
9232 else
9233 r_sym_mask = ~(bfd_vma) 0xffffffff;
9234
3410fea8 9235 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9236 if (lo->type == bfd_indirect_link_order)
9237 {
9238 bfd_byte *erel, *erelend;
9239 asection *o = lo->u.indirect.section;
9240
1da212d6
AM
9241 if (o->contents == NULL && o->size != 0)
9242 {
9243 /* This is a reloc section that is being handled as a normal
9244 section. See bfd_section_from_shdr. We can't combine
9245 relocs in this case. */
9246 free (sort);
9247 return 0;
9248 }
c152c796 9249 erel = o->contents;
eea6121a 9250 erelend = o->contents + o->size;
c8e44c6d 9251 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9252
c152c796
AM
9253 while (erel < erelend)
9254 {
9255 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9256
c152c796 9257 (*swap_in) (abfd, erel, s->rela);
7e612e98 9258 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9259 s->u.sym_mask = r_sym_mask;
9260 p += sort_elt;
9261 erel += ext_size;
9262 }
9263 }
9264
9265 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9266
9267 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9268 {
9269 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9270 if (s->type != reloc_class_relative)
9271 break;
9272 }
9273 ret = i;
9274 s_non_relative = p;
9275
9276 sq = (struct elf_link_sort_rela *) s_non_relative;
9277 for (; i < count; i++, p += sort_elt)
9278 {
9279 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9280 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9281 sq = sp;
9282 sp->u.offset = sq->rela->r_offset;
9283 }
9284
9285 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9286
c8e44c6d
AM
9287 struct elf_link_hash_table *htab = elf_hash_table (info);
9288 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9289 {
9290 /* We have plt relocs in .rela.dyn. */
9291 sq = (struct elf_link_sort_rela *) sort;
9292 for (i = 0; i < count; i++)
9293 if (sq[count - i - 1].type != reloc_class_plt)
9294 break;
9295 if (i != 0 && htab->srelplt->size == i * ext_size)
9296 {
9297 struct bfd_link_order **plo;
9298 /* Put srelplt link_order last. This is so the output_offset
9299 set in the next loop is correct for DT_JMPREL. */
9300 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9301 if ((*plo)->type == bfd_indirect_link_order
9302 && (*plo)->u.indirect.section == htab->srelplt)
9303 {
9304 lo = *plo;
9305 *plo = lo->next;
9306 }
9307 else
9308 plo = &(*plo)->next;
9309 *plo = lo;
9310 lo->next = NULL;
9311 dynamic_relocs->map_tail.link_order = lo;
9312 }
9313 }
9314
9315 p = sort;
3410fea8 9316 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9317 if (lo->type == bfd_indirect_link_order)
9318 {
9319 bfd_byte *erel, *erelend;
9320 asection *o = lo->u.indirect.section;
9321
9322 erel = o->contents;
eea6121a 9323 erelend = o->contents + o->size;
c8e44c6d 9324 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9325 while (erel < erelend)
9326 {
9327 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9328 (*swap_out) (abfd, s->rela, erel);
9329 p += sort_elt;
9330 erel += ext_size;
9331 }
9332 }
9333
9334 free (sort);
3410fea8 9335 *psec = dynamic_relocs;
c152c796
AM
9336 return ret;
9337}
9338
ef10c3ac 9339/* Add a symbol to the output symbol string table. */
c152c796 9340
6e0b88f1 9341static int
ef10c3ac
L
9342elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9343 const char *name,
9344 Elf_Internal_Sym *elfsym,
9345 asection *input_sec,
9346 struct elf_link_hash_entry *h)
c152c796 9347{
6e0b88f1 9348 int (*output_symbol_hook)
c152c796
AM
9349 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9350 struct elf_link_hash_entry *);
ef10c3ac 9351 struct elf_link_hash_table *hash_table;
c152c796 9352 const struct elf_backend_data *bed;
ef10c3ac 9353 bfd_size_type strtabsize;
c152c796 9354
8539e4e8
AM
9355 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9356
8b127cbc 9357 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9358 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9359 if (output_symbol_hook != NULL)
9360 {
8b127cbc 9361 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9362 if (ret != 1)
9363 return ret;
c152c796
AM
9364 }
9365
ef10c3ac
L
9366 if (name == NULL
9367 || *name == '\0'
9368 || (input_sec->flags & SEC_EXCLUDE))
9369 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9370 else
9371 {
ef10c3ac
L
9372 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9373 to get the final offset for st_name. */
9374 elfsym->st_name
9375 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9376 name, FALSE);
c152c796 9377 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9378 return 0;
c152c796
AM
9379 }
9380
ef10c3ac
L
9381 hash_table = elf_hash_table (flinfo->info);
9382 strtabsize = hash_table->strtabsize;
9383 if (strtabsize <= hash_table->strtabcount)
c152c796 9384 {
ef10c3ac
L
9385 strtabsize += strtabsize;
9386 hash_table->strtabsize = strtabsize;
9387 strtabsize *= sizeof (*hash_table->strtab);
9388 hash_table->strtab
9389 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9390 strtabsize);
9391 if (hash_table->strtab == NULL)
6e0b88f1 9392 return 0;
c152c796 9393 }
ef10c3ac
L
9394 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9395 hash_table->strtab[hash_table->strtabcount].dest_index
9396 = hash_table->strtabcount;
9397 hash_table->strtab[hash_table->strtabcount].destshndx_index
9398 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9399
9400 bfd_get_symcount (flinfo->output_bfd) += 1;
9401 hash_table->strtabcount += 1;
9402
9403 return 1;
9404}
9405
9406/* Swap symbols out to the symbol table and flush the output symbols to
9407 the file. */
9408
9409static bfd_boolean
9410elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9411{
9412 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9413 bfd_size_type amt;
9414 size_t i;
ef10c3ac
L
9415 const struct elf_backend_data *bed;
9416 bfd_byte *symbuf;
9417 Elf_Internal_Shdr *hdr;
9418 file_ptr pos;
9419 bfd_boolean ret;
9420
9421 if (!hash_table->strtabcount)
9422 return TRUE;
9423
9424 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9425
9426 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9427
ef10c3ac
L
9428 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9429 symbuf = (bfd_byte *) bfd_malloc (amt);
9430 if (symbuf == NULL)
9431 return FALSE;
1b786873 9432
ef10c3ac 9433 if (flinfo->symshndxbuf)
c152c796 9434 {
ef53be89
AM
9435 amt = sizeof (Elf_External_Sym_Shndx);
9436 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9437 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9438 if (flinfo->symshndxbuf == NULL)
c152c796 9439 {
ef10c3ac
L
9440 free (symbuf);
9441 return FALSE;
c152c796 9442 }
c152c796
AM
9443 }
9444
ef10c3ac
L
9445 for (i = 0; i < hash_table->strtabcount; i++)
9446 {
9447 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9448 if (elfsym->sym.st_name == (unsigned long) -1)
9449 elfsym->sym.st_name = 0;
9450 else
9451 elfsym->sym.st_name
9452 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9453 elfsym->sym.st_name);
9454 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9455 ((bfd_byte *) symbuf
9456 + (elfsym->dest_index
9457 * bed->s->sizeof_sym)),
9458 (flinfo->symshndxbuf
9459 + elfsym->destshndx_index));
9460 }
9461
9462 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9463 pos = hdr->sh_offset + hdr->sh_size;
9464 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9465 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9466 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9467 {
9468 hdr->sh_size += amt;
9469 ret = TRUE;
9470 }
9471 else
9472 ret = FALSE;
c152c796 9473
ef10c3ac
L
9474 free (symbuf);
9475
9476 free (hash_table->strtab);
9477 hash_table->strtab = NULL;
9478
9479 return ret;
c152c796
AM
9480}
9481
c0d5a53d
L
9482/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9483
9484static bfd_boolean
9485check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9486{
4fbb74a6
AM
9487 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9488 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9489 {
9490 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9491 beyond 64k. */
4eca0228 9492 _bfd_error_handler
695344c0 9493 /* xgettext:c-format */
9793eb77 9494 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9495 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9496 bfd_set_error (bfd_error_nonrepresentable_section);
9497 return FALSE;
9498 }
9499 return TRUE;
9500}
9501
c152c796
AM
9502/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9503 allowing an unsatisfied unversioned symbol in the DSO to match a
9504 versioned symbol that would normally require an explicit version.
9505 We also handle the case that a DSO references a hidden symbol
9506 which may be satisfied by a versioned symbol in another DSO. */
9507
9508static bfd_boolean
9509elf_link_check_versioned_symbol (struct bfd_link_info *info,
9510 const struct elf_backend_data *bed,
9511 struct elf_link_hash_entry *h)
9512{
9513 bfd *abfd;
9514 struct elf_link_loaded_list *loaded;
9515
9516 if (!is_elf_hash_table (info->hash))
9517 return FALSE;
9518
90c984fc
L
9519 /* Check indirect symbol. */
9520 while (h->root.type == bfd_link_hash_indirect)
9521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9522
c152c796
AM
9523 switch (h->root.type)
9524 {
9525 default:
9526 abfd = NULL;
9527 break;
9528
9529 case bfd_link_hash_undefined:
9530 case bfd_link_hash_undefweak:
9531 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9532 if (abfd == NULL
9533 || (abfd->flags & DYNAMIC) == 0
e56f61be 9534 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9535 return FALSE;
9536 break;
9537
9538 case bfd_link_hash_defined:
9539 case bfd_link_hash_defweak:
9540 abfd = h->root.u.def.section->owner;
9541 break;
9542
9543 case bfd_link_hash_common:
9544 abfd = h->root.u.c.p->section->owner;
9545 break;
9546 }
9547 BFD_ASSERT (abfd != NULL);
9548
9549 for (loaded = elf_hash_table (info)->loaded;
9550 loaded != NULL;
9551 loaded = loaded->next)
9552 {
9553 bfd *input;
9554 Elf_Internal_Shdr *hdr;
ef53be89
AM
9555 size_t symcount;
9556 size_t extsymcount;
9557 size_t extsymoff;
c152c796
AM
9558 Elf_Internal_Shdr *versymhdr;
9559 Elf_Internal_Sym *isym;
9560 Elf_Internal_Sym *isymend;
9561 Elf_Internal_Sym *isymbuf;
9562 Elf_External_Versym *ever;
9563 Elf_External_Versym *extversym;
9564
9565 input = loaded->abfd;
9566
9567 /* We check each DSO for a possible hidden versioned definition. */
9568 if (input == abfd
9569 || (input->flags & DYNAMIC) == 0
9570 || elf_dynversym (input) == 0)
9571 continue;
9572
9573 hdr = &elf_tdata (input)->dynsymtab_hdr;
9574
9575 symcount = hdr->sh_size / bed->s->sizeof_sym;
9576 if (elf_bad_symtab (input))
9577 {
9578 extsymcount = symcount;
9579 extsymoff = 0;
9580 }
9581 else
9582 {
9583 extsymcount = symcount - hdr->sh_info;
9584 extsymoff = hdr->sh_info;
9585 }
9586
9587 if (extsymcount == 0)
9588 continue;
9589
9590 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9591 NULL, NULL, NULL);
9592 if (isymbuf == NULL)
9593 return FALSE;
9594
9595 /* Read in any version definitions. */
9596 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9597 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9598 if (extversym == NULL)
9599 goto error_ret;
9600
9601 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9602 || (bfd_bread (extversym, versymhdr->sh_size, input)
9603 != versymhdr->sh_size))
9604 {
9605 free (extversym);
9606 error_ret:
9607 free (isymbuf);
9608 return FALSE;
9609 }
9610
9611 ever = extversym + extsymoff;
9612 isymend = isymbuf + extsymcount;
9613 for (isym = isymbuf; isym < isymend; isym++, ever++)
9614 {
9615 const char *name;
9616 Elf_Internal_Versym iver;
9617 unsigned short version_index;
9618
9619 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9620 || isym->st_shndx == SHN_UNDEF)
9621 continue;
9622
9623 name = bfd_elf_string_from_elf_section (input,
9624 hdr->sh_link,
9625 isym->st_name);
9626 if (strcmp (name, h->root.root.string) != 0)
9627 continue;
9628
9629 _bfd_elf_swap_versym_in (input, ever, &iver);
9630
d023c380
L
9631 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9632 && !(h->def_regular
9633 && h->forced_local))
c152c796
AM
9634 {
9635 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9636 have provided a definition for the undefined sym unless
9637 it is defined in a non-shared object and forced local.
9638 */
c152c796
AM
9639 abort ();
9640 }
9641
9642 version_index = iver.vs_vers & VERSYM_VERSION;
9643 if (version_index == 1 || version_index == 2)
9644 {
9645 /* This is the base or first version. We can use it. */
9646 free (extversym);
9647 free (isymbuf);
9648 return TRUE;
9649 }
9650 }
9651
9652 free (extversym);
9653 free (isymbuf);
9654 }
9655
9656 return FALSE;
9657}
9658
b8871f35
L
9659/* Convert ELF common symbol TYPE. */
9660
9661static int
9662elf_link_convert_common_type (struct bfd_link_info *info, int type)
9663{
9664 /* Commom symbol can only appear in relocatable link. */
9665 if (!bfd_link_relocatable (info))
9666 abort ();
9667 switch (info->elf_stt_common)
9668 {
9669 case unchanged:
9670 break;
9671 case elf_stt_common:
9672 type = STT_COMMON;
9673 break;
9674 case no_elf_stt_common:
9675 type = STT_OBJECT;
9676 break;
9677 }
9678 return type;
9679}
9680
c152c796
AM
9681/* Add an external symbol to the symbol table. This is called from
9682 the hash table traversal routine. When generating a shared object,
9683 we go through the symbol table twice. The first time we output
9684 anything that might have been forced to local scope in a version
9685 script. The second time we output the symbols that are still
9686 global symbols. */
9687
9688static bfd_boolean
7686d77d 9689elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9690{
7686d77d 9691 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9692 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9693 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9694 bfd_boolean strip;
9695 Elf_Internal_Sym sym;
9696 asection *input_sec;
9697 const struct elf_backend_data *bed;
6e0b88f1
AM
9698 long indx;
9699 int ret;
b8871f35 9700 unsigned int type;
c152c796
AM
9701
9702 if (h->root.type == bfd_link_hash_warning)
9703 {
9704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9705 if (h->root.type == bfd_link_hash_new)
9706 return TRUE;
9707 }
9708
9709 /* Decide whether to output this symbol in this pass. */
9710 if (eoinfo->localsyms)
9711 {
4deb8f71 9712 if (!h->forced_local)
c152c796
AM
9713 return TRUE;
9714 }
9715 else
9716 {
4deb8f71 9717 if (h->forced_local)
c152c796
AM
9718 return TRUE;
9719 }
9720
8b127cbc 9721 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9722
12ac1cf5 9723 if (h->root.type == bfd_link_hash_undefined)
c152c796 9724 {
12ac1cf5
NC
9725 /* If we have an undefined symbol reference here then it must have
9726 come from a shared library that is being linked in. (Undefined
98da7939
L
9727 references in regular files have already been handled unless
9728 they are in unreferenced sections which are removed by garbage
9729 collection). */
12ac1cf5
NC
9730 bfd_boolean ignore_undef = FALSE;
9731
9732 /* Some symbols may be special in that the fact that they're
9733 undefined can be safely ignored - let backend determine that. */
9734 if (bed->elf_backend_ignore_undef_symbol)
9735 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9736
9737 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9738 if (!ignore_undef
12ac1cf5 9739 && h->ref_dynamic
8b127cbc
AM
9740 && (!h->ref_regular || flinfo->info->gc_sections)
9741 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9742 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9743 (*flinfo->info->callbacks->undefined_symbol)
9744 (flinfo->info, h->root.root.string,
9745 h->ref_regular ? NULL : h->root.u.undef.abfd,
9746 NULL, 0,
9747 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9748
9749 /* Strip a global symbol defined in a discarded section. */
9750 if (h->indx == -3)
9751 return TRUE;
c152c796
AM
9752 }
9753
9754 /* We should also warn if a forced local symbol is referenced from
9755 shared libraries. */
0e1862bb 9756 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9757 && h->forced_local
9758 && h->ref_dynamic
371a5866 9759 && h->def_regular
f5385ebf 9760 && !h->dynamic_def
ee659f1f 9761 && h->ref_dynamic_nonweak
8b127cbc 9762 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9763 {
17d078c5
AM
9764 bfd *def_bfd;
9765 const char *msg;
90c984fc
L
9766 struct elf_link_hash_entry *hi = h;
9767
9768 /* Check indirect symbol. */
9769 while (hi->root.type == bfd_link_hash_indirect)
9770 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9771
9772 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9773 /* xgettext:c-format */
871b3ab2 9774 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9775 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9776 /* xgettext:c-format */
871b3ab2 9777 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9778 else
695344c0 9779 /* xgettext:c-format */
871b3ab2 9780 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9781 def_bfd = flinfo->output_bfd;
90c984fc
L
9782 if (hi->root.u.def.section != bfd_abs_section_ptr)
9783 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9784 _bfd_error_handler (msg, flinfo->output_bfd,
9785 h->root.root.string, def_bfd);
17d078c5 9786 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9787 eoinfo->failed = TRUE;
9788 return FALSE;
9789 }
9790
9791 /* We don't want to output symbols that have never been mentioned by
9792 a regular file, or that we have been told to strip. However, if
9793 h->indx is set to -2, the symbol is used by a reloc and we must
9794 output it. */
d983c8c5 9795 strip = FALSE;
c152c796 9796 if (h->indx == -2)
d983c8c5 9797 ;
f5385ebf 9798 else if ((h->def_dynamic
77cfaee6
AM
9799 || h->ref_dynamic
9800 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9801 && !h->def_regular
9802 && !h->ref_regular)
c152c796 9803 strip = TRUE;
8b127cbc 9804 else if (flinfo->info->strip == strip_all)
c152c796 9805 strip = TRUE;
8b127cbc
AM
9806 else if (flinfo->info->strip == strip_some
9807 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9808 h->root.root.string, FALSE, FALSE) == NULL)
9809 strip = TRUE;
d56d55e7
AM
9810 else if ((h->root.type == bfd_link_hash_defined
9811 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9812 && ((flinfo->info->strip_discarded
dbaa2011 9813 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9814 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9815 && h->root.u.def.section->owner != NULL
d56d55e7 9816 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9817 strip = TRUE;
9e2278f5
AM
9818 else if ((h->root.type == bfd_link_hash_undefined
9819 || h->root.type == bfd_link_hash_undefweak)
9820 && h->root.u.undef.abfd != NULL
9821 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9822 strip = TRUE;
c152c796 9823
b8871f35
L
9824 type = h->type;
9825
c152c796 9826 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9827 nothing else to do. However, if it is a forced local symbol or
9828 an ifunc symbol we need to give the backend finish_dynamic_symbol
9829 function a chance to make it dynamic. */
c152c796
AM
9830 if (strip
9831 && h->dynindx == -1
b8871f35 9832 && type != STT_GNU_IFUNC
f5385ebf 9833 && !h->forced_local)
c152c796
AM
9834 return TRUE;
9835
9836 sym.st_value = 0;
9837 sym.st_size = h->size;
9838 sym.st_other = h->other;
c152c796
AM
9839 switch (h->root.type)
9840 {
9841 default:
9842 case bfd_link_hash_new:
9843 case bfd_link_hash_warning:
9844 abort ();
9845 return FALSE;
9846
9847 case bfd_link_hash_undefined:
9848 case bfd_link_hash_undefweak:
9849 input_sec = bfd_und_section_ptr;
9850 sym.st_shndx = SHN_UNDEF;
9851 break;
9852
9853 case bfd_link_hash_defined:
9854 case bfd_link_hash_defweak:
9855 {
9856 input_sec = h->root.u.def.section;
9857 if (input_sec->output_section != NULL)
9858 {
9859 sym.st_shndx =
8b127cbc 9860 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9861 input_sec->output_section);
9862 if (sym.st_shndx == SHN_BAD)
9863 {
4eca0228 9864 _bfd_error_handler
695344c0 9865 /* xgettext:c-format */
871b3ab2 9866 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9867 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9868 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9869 eoinfo->failed = TRUE;
9870 return FALSE;
9871 }
9872
9873 /* ELF symbols in relocatable files are section relative,
9874 but in nonrelocatable files they are virtual
9875 addresses. */
9876 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9877 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9878 {
9879 sym.st_value += input_sec->output_section->vma;
9880 if (h->type == STT_TLS)
9881 {
8b127cbc 9882 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9883 if (tls_sec != NULL)
9884 sym.st_value -= tls_sec->vma;
c152c796
AM
9885 }
9886 }
9887 }
9888 else
9889 {
9890 BFD_ASSERT (input_sec->owner == NULL
9891 || (input_sec->owner->flags & DYNAMIC) != 0);
9892 sym.st_shndx = SHN_UNDEF;
9893 input_sec = bfd_und_section_ptr;
9894 }
9895 }
9896 break;
9897
9898 case bfd_link_hash_common:
9899 input_sec = h->root.u.c.p->section;
a4d8e49b 9900 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9901 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9902 break;
9903
9904 case bfd_link_hash_indirect:
9905 /* These symbols are created by symbol versioning. They point
9906 to the decorated version of the name. For example, if the
9907 symbol foo@@GNU_1.2 is the default, which should be used when
9908 foo is used with no version, then we add an indirect symbol
9909 foo which points to foo@@GNU_1.2. We ignore these symbols,
9910 since the indirected symbol is already in the hash table. */
9911 return TRUE;
9912 }
9913
b8871f35
L
9914 if (type == STT_COMMON || type == STT_OBJECT)
9915 switch (h->root.type)
9916 {
9917 case bfd_link_hash_common:
9918 type = elf_link_convert_common_type (flinfo->info, type);
9919 break;
9920 case bfd_link_hash_defined:
9921 case bfd_link_hash_defweak:
9922 if (bed->common_definition (&sym))
9923 type = elf_link_convert_common_type (flinfo->info, type);
9924 else
9925 type = STT_OBJECT;
9926 break;
9927 case bfd_link_hash_undefined:
9928 case bfd_link_hash_undefweak:
9929 break;
9930 default:
9931 abort ();
9932 }
9933
4deb8f71 9934 if (h->forced_local)
b8871f35
L
9935 {
9936 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9937 /* Turn off visibility on local symbol. */
9938 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9939 }
9940 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9941 else if (h->unique_global && h->def_regular)
9942 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9943 else if (h->root.type == bfd_link_hash_undefweak
9944 || h->root.type == bfd_link_hash_defweak)
9945 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9946 else
9947 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9948 sym.st_target_internal = h->target_internal;
9949
c152c796
AM
9950 /* Give the processor backend a chance to tweak the symbol value,
9951 and also to finish up anything that needs to be done for this
9952 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9953 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9954 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9955 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9956 && h->def_regular
0e1862bb 9957 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9958 || ((h->dynindx != -1
9959 || h->forced_local)
0e1862bb 9960 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9961 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9962 || h->root.type != bfd_link_hash_undefweak))
9963 || !h->forced_local)
8b127cbc 9964 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9965 {
9966 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9967 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9968 {
9969 eoinfo->failed = TRUE;
9970 return FALSE;
9971 }
9972 }
9973
9974 /* If we are marking the symbol as undefined, and there are no
9975 non-weak references to this symbol from a regular object, then
9976 mark the symbol as weak undefined; if there are non-weak
9977 references, mark the symbol as strong. We can't do this earlier,
9978 because it might not be marked as undefined until the
9979 finish_dynamic_symbol routine gets through with it. */
9980 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9981 && h->ref_regular
c152c796
AM
9982 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9983 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9984 {
9985 int bindtype;
b8871f35 9986 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9987
9988 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9989 if (type == STT_GNU_IFUNC)
9990 type = STT_FUNC;
c152c796 9991
f5385ebf 9992 if (h->ref_regular_nonweak)
c152c796
AM
9993 bindtype = STB_GLOBAL;
9994 else
9995 bindtype = STB_WEAK;
2955ec4c 9996 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9997 }
9998
bda987c2
CD
9999 /* If this is a symbol defined in a dynamic library, don't use the
10000 symbol size from the dynamic library. Relinking an executable
10001 against a new library may introduce gratuitous changes in the
10002 executable's symbols if we keep the size. */
10003 if (sym.st_shndx == SHN_UNDEF
10004 && !h->def_regular
10005 && h->def_dynamic)
10006 sym.st_size = 0;
10007
c152c796
AM
10008 /* If a non-weak symbol with non-default visibility is not defined
10009 locally, it is a fatal error. */
0e1862bb 10010 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10011 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10012 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10013 && h->root.type == bfd_link_hash_undefined
f5385ebf 10014 && !h->def_regular)
c152c796 10015 {
17d078c5
AM
10016 const char *msg;
10017
10018 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10019 /* xgettext:c-format */
871b3ab2 10020 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10021 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10022 /* xgettext:c-format */
871b3ab2 10023 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10024 else
695344c0 10025 /* xgettext:c-format */
871b3ab2 10026 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10027 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10028 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10029 eoinfo->failed = TRUE;
10030 return FALSE;
10031 }
10032
10033 /* If this symbol should be put in the .dynsym section, then put it
10034 there now. We already know the symbol index. We also fill in
10035 the entry in the .hash section. */
1c2649ed
EB
10036 if (h->dynindx != -1
10037 && elf_hash_table (flinfo->info)->dynamic_sections_created
10038 && elf_hash_table (flinfo->info)->dynsym != NULL
10039 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10040 {
c152c796
AM
10041 bfd_byte *esym;
10042
90c984fc
L
10043 /* Since there is no version information in the dynamic string,
10044 if there is no version info in symbol version section, we will
1659f720 10045 have a run-time problem if not linking executable, referenced
4deb8f71 10046 by shared library, or not bound locally. */
1659f720 10047 if (h->verinfo.verdef == NULL
0e1862bb 10048 && (!bfd_link_executable (flinfo->info)
1659f720
L
10049 || h->ref_dynamic
10050 || !h->def_regular))
90c984fc
L
10051 {
10052 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10053
10054 if (p && p [1] != '\0')
10055 {
4eca0228 10056 _bfd_error_handler
695344c0 10057 /* xgettext:c-format */
9793eb77 10058 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10059 flinfo->output_bfd, h->root.root.string);
10060 eoinfo->failed = TRUE;
10061 return FALSE;
10062 }
10063 }
10064
c152c796 10065 sym.st_name = h->dynstr_index;
cae1fbbb
L
10066 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10067 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10068 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10069 {
10070 eoinfo->failed = TRUE;
10071 return FALSE;
10072 }
8b127cbc 10073 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10074
8b127cbc 10075 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10076 {
10077 size_t hash_entry_size;
10078 bfd_byte *bucketpos;
10079 bfd_vma chain;
41198d0c
L
10080 size_t bucketcount;
10081 size_t bucket;
10082
8b127cbc 10083 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10084 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10085
10086 hash_entry_size
8b127cbc
AM
10087 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10088 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10089 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10090 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10091 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10092 bucketpos);
10093 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10094 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10095 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10096 }
c152c796 10097
8b127cbc 10098 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10099 {
10100 Elf_Internal_Versym iversym;
10101 Elf_External_Versym *eversym;
10102
f5385ebf 10103 if (!h->def_regular)
c152c796 10104 {
7b20f099
AM
10105 if (h->verinfo.verdef == NULL
10106 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10107 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10108 iversym.vs_vers = 0;
10109 else
10110 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10111 }
10112 else
10113 {
10114 if (h->verinfo.vertree == NULL)
10115 iversym.vs_vers = 1;
10116 else
10117 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10118 if (flinfo->info->create_default_symver)
3e3b46e5 10119 iversym.vs_vers++;
c152c796
AM
10120 }
10121
422f1182 10122 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10123 defined locally. */
422f1182 10124 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10125 iversym.vs_vers |= VERSYM_HIDDEN;
10126
8b127cbc 10127 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10128 eversym += h->dynindx;
8b127cbc 10129 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10130 }
10131 }
10132
d983c8c5
AM
10133 /* If the symbol is undefined, and we didn't output it to .dynsym,
10134 strip it from .symtab too. Obviously we can't do this for
10135 relocatable output or when needed for --emit-relocs. */
10136 else if (input_sec == bfd_und_section_ptr
10137 && h->indx != -2
66cae560
NC
10138 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10139 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10140 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10141 return TRUE;
66cae560 10142
d983c8c5
AM
10143 /* Also strip others that we couldn't earlier due to dynamic symbol
10144 processing. */
10145 if (strip)
10146 return TRUE;
10147 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10148 return TRUE;
10149
2ec55de3
AM
10150 /* Output a FILE symbol so that following locals are not associated
10151 with the wrong input file. We need one for forced local symbols
10152 if we've seen more than one FILE symbol or when we have exactly
10153 one FILE symbol but global symbols are present in a file other
10154 than the one with the FILE symbol. We also need one if linker
10155 defined symbols are present. In practice these conditions are
10156 always met, so just emit the FILE symbol unconditionally. */
10157 if (eoinfo->localsyms
10158 && !eoinfo->file_sym_done
10159 && eoinfo->flinfo->filesym_count != 0)
10160 {
10161 Elf_Internal_Sym fsym;
10162
10163 memset (&fsym, 0, sizeof (fsym));
10164 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10165 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10166 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10167 bfd_und_section_ptr, NULL))
2ec55de3
AM
10168 return FALSE;
10169
10170 eoinfo->file_sym_done = TRUE;
10171 }
10172
8b127cbc 10173 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10174 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10175 input_sec, h);
6e0b88f1 10176 if (ret == 0)
c152c796
AM
10177 {
10178 eoinfo->failed = TRUE;
10179 return FALSE;
10180 }
6e0b88f1
AM
10181 else if (ret == 1)
10182 h->indx = indx;
10183 else if (h->indx == -2)
10184 abort();
c152c796
AM
10185
10186 return TRUE;
10187}
10188
cdd3575c
AM
10189/* Return TRUE if special handling is done for relocs in SEC against
10190 symbols defined in discarded sections. */
10191
c152c796
AM
10192static bfd_boolean
10193elf_section_ignore_discarded_relocs (asection *sec)
10194{
10195 const struct elf_backend_data *bed;
10196
cdd3575c
AM
10197 switch (sec->sec_info_type)
10198 {
dbaa2011
AM
10199 case SEC_INFO_TYPE_STABS:
10200 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10201 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10202 return TRUE;
10203 default:
10204 break;
10205 }
c152c796
AM
10206
10207 bed = get_elf_backend_data (sec->owner);
10208 if (bed->elf_backend_ignore_discarded_relocs != NULL
10209 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10210 return TRUE;
10211
10212 return FALSE;
10213}
10214
9e66c942
AM
10215/* Return a mask saying how ld should treat relocations in SEC against
10216 symbols defined in discarded sections. If this function returns
10217 COMPLAIN set, ld will issue a warning message. If this function
10218 returns PRETEND set, and the discarded section was link-once and the
10219 same size as the kept link-once section, ld will pretend that the
10220 symbol was actually defined in the kept section. Otherwise ld will
10221 zero the reloc (at least that is the intent, but some cooperation by
10222 the target dependent code is needed, particularly for REL targets). */
10223
8a696751
AM
10224unsigned int
10225_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10226{
9e66c942 10227 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10228 return PRETEND;
cdd3575c
AM
10229
10230 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10231 return 0;
cdd3575c
AM
10232
10233 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10234 return 0;
cdd3575c 10235
9e66c942 10236 return COMPLAIN | PRETEND;
cdd3575c
AM
10237}
10238
3d7f7666
L
10239/* Find a match between a section and a member of a section group. */
10240
10241static asection *
c0f00686
L
10242match_group_member (asection *sec, asection *group,
10243 struct bfd_link_info *info)
3d7f7666
L
10244{
10245 asection *first = elf_next_in_group (group);
10246 asection *s = first;
10247
10248 while (s != NULL)
10249 {
c0f00686 10250 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10251 return s;
10252
83180ade 10253 s = elf_next_in_group (s);
3d7f7666
L
10254 if (s == first)
10255 break;
10256 }
10257
10258 return NULL;
10259}
10260
01b3c8ab 10261/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10262 to replace it. Return the replacement if it is OK. Otherwise return
10263 NULL. */
01b3c8ab
L
10264
10265asection *
c0f00686 10266_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10267{
10268 asection *kept;
10269
10270 kept = sec->kept_section;
10271 if (kept != NULL)
10272 {
c2370991 10273 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10274 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10275 if (kept != NULL
10276 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10277 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10278 kept = NULL;
c2370991 10279 sec->kept_section = kept;
01b3c8ab
L
10280 }
10281 return kept;
10282}
10283
c152c796
AM
10284/* Link an input file into the linker output file. This function
10285 handles all the sections and relocations of the input file at once.
10286 This is so that we only have to read the local symbols once, and
10287 don't have to keep them in memory. */
10288
10289static bfd_boolean
8b127cbc 10290elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10291{
ece5ef60 10292 int (*relocate_section)
c152c796
AM
10293 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10294 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10295 bfd *output_bfd;
10296 Elf_Internal_Shdr *symtab_hdr;
10297 size_t locsymcount;
10298 size_t extsymoff;
10299 Elf_Internal_Sym *isymbuf;
10300 Elf_Internal_Sym *isym;
10301 Elf_Internal_Sym *isymend;
10302 long *pindex;
10303 asection **ppsection;
10304 asection *o;
10305 const struct elf_backend_data *bed;
c152c796 10306 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10307 bfd_size_type address_size;
10308 bfd_vma r_type_mask;
10309 int r_sym_shift;
ffbc01cc 10310 bfd_boolean have_file_sym = FALSE;
c152c796 10311
8b127cbc 10312 output_bfd = flinfo->output_bfd;
c152c796
AM
10313 bed = get_elf_backend_data (output_bfd);
10314 relocate_section = bed->elf_backend_relocate_section;
10315
10316 /* If this is a dynamic object, we don't want to do anything here:
10317 we don't want the local symbols, and we don't want the section
10318 contents. */
10319 if ((input_bfd->flags & DYNAMIC) != 0)
10320 return TRUE;
10321
c152c796
AM
10322 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10323 if (elf_bad_symtab (input_bfd))
10324 {
10325 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10326 extsymoff = 0;
10327 }
10328 else
10329 {
10330 locsymcount = symtab_hdr->sh_info;
10331 extsymoff = symtab_hdr->sh_info;
10332 }
10333
10334 /* Read the local symbols. */
10335 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10336 if (isymbuf == NULL && locsymcount != 0)
10337 {
10338 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10339 flinfo->internal_syms,
10340 flinfo->external_syms,
10341 flinfo->locsym_shndx);
c152c796
AM
10342 if (isymbuf == NULL)
10343 return FALSE;
10344 }
10345
10346 /* Find local symbol sections and adjust values of symbols in
10347 SEC_MERGE sections. Write out those local symbols we know are
10348 going into the output file. */
10349 isymend = isymbuf + locsymcount;
8b127cbc 10350 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10351 isym < isymend;
10352 isym++, pindex++, ppsection++)
10353 {
10354 asection *isec;
10355 const char *name;
10356 Elf_Internal_Sym osym;
6e0b88f1
AM
10357 long indx;
10358 int ret;
c152c796
AM
10359
10360 *pindex = -1;
10361
10362 if (elf_bad_symtab (input_bfd))
10363 {
10364 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10365 {
10366 *ppsection = NULL;
10367 continue;
10368 }
10369 }
10370
10371 if (isym->st_shndx == SHN_UNDEF)
10372 isec = bfd_und_section_ptr;
c152c796
AM
10373 else if (isym->st_shndx == SHN_ABS)
10374 isec = bfd_abs_section_ptr;
10375 else if (isym->st_shndx == SHN_COMMON)
10376 isec = bfd_com_section_ptr;
10377 else
10378 {
cb33740c
AM
10379 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10380 if (isec == NULL)
10381 {
10382 /* Don't attempt to output symbols with st_shnx in the
10383 reserved range other than SHN_ABS and SHN_COMMON. */
10384 *ppsection = NULL;
10385 continue;
10386 }
dbaa2011 10387 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10388 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10389 isym->st_value =
10390 _bfd_merged_section_offset (output_bfd, &isec,
10391 elf_section_data (isec)->sec_info,
10392 isym->st_value);
c152c796
AM
10393 }
10394
10395 *ppsection = isec;
10396
d983c8c5
AM
10397 /* Don't output the first, undefined, symbol. In fact, don't
10398 output any undefined local symbol. */
10399 if (isec == bfd_und_section_ptr)
c152c796
AM
10400 continue;
10401
10402 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10403 {
10404 /* We never output section symbols. Instead, we use the
10405 section symbol of the corresponding section in the output
10406 file. */
10407 continue;
10408 }
10409
10410 /* If we are stripping all symbols, we don't want to output this
10411 one. */
8b127cbc 10412 if (flinfo->info->strip == strip_all)
c152c796
AM
10413 continue;
10414
10415 /* If we are discarding all local symbols, we don't want to
10416 output this one. If we are generating a relocatable output
10417 file, then some of the local symbols may be required by
10418 relocs; we output them below as we discover that they are
10419 needed. */
8b127cbc 10420 if (flinfo->info->discard == discard_all)
c152c796
AM
10421 continue;
10422
10423 /* If this symbol is defined in a section which we are
f02571c5
AM
10424 discarding, we don't need to keep it. */
10425 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10426 && isym->st_shndx < SHN_LORESERVE
10427 && bfd_section_removed_from_list (output_bfd,
10428 isec->output_section))
e75a280b
L
10429 continue;
10430
c152c796
AM
10431 /* Get the name of the symbol. */
10432 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10433 isym->st_name);
10434 if (name == NULL)
10435 return FALSE;
10436
10437 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10438 if ((flinfo->info->strip == strip_some
10439 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10440 == NULL))
8b127cbc 10441 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10442 && (isec->flags & SEC_MERGE)
10443 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10444 || flinfo->info->discard == discard_l)
c152c796
AM
10445 && bfd_is_local_label_name (input_bfd, name)))
10446 continue;
10447
ffbc01cc
AM
10448 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10449 {
ce875075
AM
10450 if (input_bfd->lto_output)
10451 /* -flto puts a temp file name here. This means builds
10452 are not reproducible. Discard the symbol. */
10453 continue;
ffbc01cc
AM
10454 have_file_sym = TRUE;
10455 flinfo->filesym_count += 1;
10456 }
10457 if (!have_file_sym)
10458 {
10459 /* In the absence of debug info, bfd_find_nearest_line uses
10460 FILE symbols to determine the source file for local
10461 function symbols. Provide a FILE symbol here if input
10462 files lack such, so that their symbols won't be
10463 associated with a previous input file. It's not the
10464 source file, but the best we can do. */
10465 have_file_sym = TRUE;
10466 flinfo->filesym_count += 1;
10467 memset (&osym, 0, sizeof (osym));
10468 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10469 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10470 if (!elf_link_output_symstrtab (flinfo,
10471 (input_bfd->lto_output ? NULL
10472 : input_bfd->filename),
10473 &osym, bfd_abs_section_ptr,
10474 NULL))
ffbc01cc
AM
10475 return FALSE;
10476 }
10477
c152c796
AM
10478 osym = *isym;
10479
10480 /* Adjust the section index for the output file. */
10481 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10482 isec->output_section);
10483 if (osym.st_shndx == SHN_BAD)
10484 return FALSE;
10485
c152c796
AM
10486 /* ELF symbols in relocatable files are section relative, but
10487 in executable files they are virtual addresses. Note that
10488 this code assumes that all ELF sections have an associated
10489 BFD section with a reasonable value for output_offset; below
10490 we assume that they also have a reasonable value for
10491 output_section. Any special sections must be set up to meet
10492 these requirements. */
10493 osym.st_value += isec->output_offset;
0e1862bb 10494 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10495 {
10496 osym.st_value += isec->output_section->vma;
10497 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10498 {
10499 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10500 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10501 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10502 else
10503 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10504 STT_NOTYPE);
c152c796
AM
10505 }
10506 }
10507
6e0b88f1 10508 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10509 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10510 if (ret == 0)
c152c796 10511 return FALSE;
6e0b88f1
AM
10512 else if (ret == 1)
10513 *pindex = indx;
c152c796
AM
10514 }
10515
310fd250
L
10516 if (bed->s->arch_size == 32)
10517 {
10518 r_type_mask = 0xff;
10519 r_sym_shift = 8;
10520 address_size = 4;
10521 }
10522 else
10523 {
10524 r_type_mask = 0xffffffff;
10525 r_sym_shift = 32;
10526 address_size = 8;
10527 }
10528
c152c796
AM
10529 /* Relocate the contents of each section. */
10530 sym_hashes = elf_sym_hashes (input_bfd);
10531 for (o = input_bfd->sections; o != NULL; o = o->next)
10532 {
10533 bfd_byte *contents;
10534
10535 if (! o->linker_mark)
10536 {
10537 /* This section was omitted from the link. */
10538 continue;
10539 }
10540
7bdf4127 10541 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10542 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10543 {
10544 /* Deal with the group signature symbol. */
10545 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10546 unsigned long symndx = sec_data->this_hdr.sh_info;
10547 asection *osec = o->output_section;
10548
7bdf4127 10549 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10550 if (symndx >= locsymcount
10551 || (elf_bad_symtab (input_bfd)
8b127cbc 10552 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10553 {
10554 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10555 while (h->root.type == bfd_link_hash_indirect
10556 || h->root.type == bfd_link_hash_warning)
10557 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10558 /* Arrange for symbol to be output. */
10559 h->indx = -2;
10560 elf_section_data (osec)->this_hdr.sh_info = -2;
10561 }
10562 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10563 {
10564 /* We'll use the output section target_index. */
8b127cbc 10565 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10566 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10567 }
10568 else
10569 {
8b127cbc 10570 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10571 {
10572 /* Otherwise output the local symbol now. */
10573 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10574 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10575 const char *name;
6e0b88f1
AM
10576 long indx;
10577 int ret;
bcacc0f5
AM
10578
10579 name = bfd_elf_string_from_elf_section (input_bfd,
10580 symtab_hdr->sh_link,
10581 sym.st_name);
10582 if (name == NULL)
10583 return FALSE;
10584
10585 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10586 sec);
10587 if (sym.st_shndx == SHN_BAD)
10588 return FALSE;
10589
10590 sym.st_value += o->output_offset;
10591
6e0b88f1 10592 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10593 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10594 NULL);
6e0b88f1 10595 if (ret == 0)
bcacc0f5 10596 return FALSE;
6e0b88f1 10597 else if (ret == 1)
8b127cbc 10598 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10599 else
10600 abort ();
bcacc0f5
AM
10601 }
10602 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10603 = flinfo->indices[symndx];
bcacc0f5
AM
10604 }
10605 }
10606
c152c796 10607 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10608 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10609 continue;
10610
10611 if ((o->flags & SEC_LINKER_CREATED) != 0)
10612 {
10613 /* Section was created by _bfd_elf_link_create_dynamic_sections
10614 or somesuch. */
10615 continue;
10616 }
10617
10618 /* Get the contents of the section. They have been cached by a
10619 relaxation routine. Note that o is a section in an input
10620 file, so the contents field will not have been set by any of
10621 the routines which work on output files. */
10622 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10623 {
10624 contents = elf_section_data (o)->this_hdr.contents;
10625 if (bed->caches_rawsize
10626 && o->rawsize != 0
10627 && o->rawsize < o->size)
10628 {
10629 memcpy (flinfo->contents, contents, o->rawsize);
10630 contents = flinfo->contents;
10631 }
10632 }
c152c796
AM
10633 else
10634 {
8b127cbc 10635 contents = flinfo->contents;
4a114e3e 10636 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10637 return FALSE;
10638 }
10639
10640 if ((o->flags & SEC_RELOC) != 0)
10641 {
10642 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10643 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10644 int action_discarded;
ece5ef60 10645 int ret;
c152c796
AM
10646
10647 /* Get the swapped relocs. */
10648 internal_relocs
8b127cbc
AM
10649 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10650 flinfo->internal_relocs, FALSE);
c152c796
AM
10651 if (internal_relocs == NULL
10652 && o->reloc_count > 0)
10653 return FALSE;
10654
310fd250
L
10655 /* We need to reverse-copy input .ctors/.dtors sections if
10656 they are placed in .init_array/.finit_array for output. */
10657 if (o->size > address_size
10658 && ((strncmp (o->name, ".ctors", 6) == 0
10659 && strcmp (o->output_section->name,
10660 ".init_array") == 0)
10661 || (strncmp (o->name, ".dtors", 6) == 0
10662 && strcmp (o->output_section->name,
10663 ".fini_array") == 0))
10664 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10665 {
056bafd4
MR
10666 if (o->size * bed->s->int_rels_per_ext_rel
10667 != o->reloc_count * address_size)
310fd250 10668 {
4eca0228 10669 _bfd_error_handler
695344c0 10670 /* xgettext:c-format */
871b3ab2 10671 (_("error: %pB: size of section %pA is not "
310fd250
L
10672 "multiple of address size"),
10673 input_bfd, o);
8c6716e5 10674 bfd_set_error (bfd_error_bad_value);
310fd250
L
10675 return FALSE;
10676 }
10677 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10678 }
10679
0f02bbd9 10680 action_discarded = -1;
c152c796 10681 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10682 action_discarded = (*bed->action_discarded) (o);
10683
10684 /* Run through the relocs evaluating complex reloc symbols and
10685 looking for relocs against symbols from discarded sections
10686 or section symbols from removed link-once sections.
10687 Complain about relocs against discarded sections. Zero
10688 relocs against removed link-once sections. */
10689
10690 rel = internal_relocs;
056bafd4 10691 relend = rel + o->reloc_count;
0f02bbd9 10692 for ( ; rel < relend; rel++)
c152c796 10693 {
0f02bbd9
AM
10694 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10695 unsigned int s_type;
10696 asection **ps, *sec;
10697 struct elf_link_hash_entry *h = NULL;
10698 const char *sym_name;
c152c796 10699
0f02bbd9
AM
10700 if (r_symndx == STN_UNDEF)
10701 continue;
c152c796 10702
0f02bbd9
AM
10703 if (r_symndx >= locsymcount
10704 || (elf_bad_symtab (input_bfd)
8b127cbc 10705 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10706 {
10707 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10708
0f02bbd9
AM
10709 /* Badly formatted input files can contain relocs that
10710 reference non-existant symbols. Check here so that
10711 we do not seg fault. */
10712 if (h == NULL)
c152c796 10713 {
4eca0228 10714 _bfd_error_handler
695344c0 10715 /* xgettext:c-format */
2dcf00ce 10716 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10717 "that references a non-existent global symbol"),
2dcf00ce 10718 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10719 bfd_set_error (bfd_error_bad_value);
10720 return FALSE;
10721 }
3b36f7e6 10722
0f02bbd9
AM
10723 while (h->root.type == bfd_link_hash_indirect
10724 || h->root.type == bfd_link_hash_warning)
10725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10726
0f02bbd9 10727 s_type = h->type;
cdd3575c 10728
9e2dec47 10729 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10730 mark the symbol as undefined. Note that the
10731 linker may attach linker created dynamic sections
10732 to the plugin bfd. Symbols defined in linker
10733 created sections are not plugin symbols. */
bc4e12de 10734 if ((h->root.non_ir_ref_regular
4070765b 10735 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10736 && (h->root.type == bfd_link_hash_defined
10737 || h->root.type == bfd_link_hash_defweak)
10738 && (h->root.u.def.section->flags
10739 & SEC_LINKER_CREATED) == 0
10740 && h->root.u.def.section->owner != NULL
10741 && (h->root.u.def.section->owner->flags
10742 & BFD_PLUGIN) != 0)
10743 {
10744 h->root.type = bfd_link_hash_undefined;
10745 h->root.u.undef.abfd = h->root.u.def.section->owner;
10746 }
10747
0f02bbd9
AM
10748 ps = NULL;
10749 if (h->root.type == bfd_link_hash_defined
10750 || h->root.type == bfd_link_hash_defweak)
10751 ps = &h->root.u.def.section;
10752
10753 sym_name = h->root.root.string;
10754 }
10755 else
10756 {
10757 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10758
10759 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10760 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10761 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10762 sym, *ps);
10763 }
c152c796 10764
c301e700 10765 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10766 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10767 {
10768 bfd_vma val;
10769 bfd_vma dot = (rel->r_offset
10770 + o->output_offset + o->output_section->vma);
10771#ifdef DEBUG
10772 printf ("Encountered a complex symbol!");
10773 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10774 input_bfd->filename, o->name,
10775 (long) (rel - internal_relocs));
0f02bbd9
AM
10776 printf (" symbol: idx %8.8lx, name %s\n",
10777 r_symndx, sym_name);
10778 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10779 (unsigned long) rel->r_info,
10780 (unsigned long) rel->r_offset);
10781#endif
8b127cbc 10782 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10783 isymbuf, locsymcount, s_type == STT_SRELC))
10784 return FALSE;
10785
10786 /* Symbol evaluated OK. Update to absolute value. */
10787 set_symbol_value (input_bfd, isymbuf, locsymcount,
10788 r_symndx, val);
10789 continue;
10790 }
10791
10792 if (action_discarded != -1 && ps != NULL)
10793 {
cdd3575c
AM
10794 /* Complain if the definition comes from a
10795 discarded section. */
dbaa2011 10796 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10797 {
cf35638d 10798 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10799 if (action_discarded & COMPLAIN)
8b127cbc 10800 (*flinfo->info->callbacks->einfo)
695344c0 10801 /* xgettext:c-format */
871b3ab2
AM
10802 (_("%X`%s' referenced in section `%pA' of %pB: "
10803 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10804 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10805
87e5235d 10806 /* Try to do the best we can to support buggy old
e0ae6d6f 10807 versions of gcc. Pretend that the symbol is
87e5235d
AM
10808 really defined in the kept linkonce section.
10809 FIXME: This is quite broken. Modifying the
10810 symbol here means we will be changing all later
e0ae6d6f 10811 uses of the symbol, not just in this section. */
0f02bbd9 10812 if (action_discarded & PRETEND)
87e5235d 10813 {
01b3c8ab
L
10814 asection *kept;
10815
c0f00686 10816 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10817 flinfo->info);
01b3c8ab 10818 if (kept != NULL)
87e5235d
AM
10819 {
10820 *ps = kept;
10821 continue;
10822 }
10823 }
c152c796
AM
10824 }
10825 }
10826 }
10827
10828 /* Relocate the section by invoking a back end routine.
10829
10830 The back end routine is responsible for adjusting the
10831 section contents as necessary, and (if using Rela relocs
10832 and generating a relocatable output file) adjusting the
10833 reloc addend as necessary.
10834
10835 The back end routine does not have to worry about setting
10836 the reloc address or the reloc symbol index.
10837
10838 The back end routine is given a pointer to the swapped in
10839 internal symbols, and can access the hash table entries
10840 for the external symbols via elf_sym_hashes (input_bfd).
10841
10842 When generating relocatable output, the back end routine
10843 must handle STB_LOCAL/STT_SECTION symbols specially. The
10844 output symbol is going to be a section symbol
10845 corresponding to the output section, which will require
10846 the addend to be adjusted. */
10847
8b127cbc 10848 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10849 input_bfd, o, contents,
10850 internal_relocs,
10851 isymbuf,
8b127cbc 10852 flinfo->sections);
ece5ef60 10853 if (!ret)
c152c796
AM
10854 return FALSE;
10855
ece5ef60 10856 if (ret == 2
0e1862bb 10857 || bfd_link_relocatable (flinfo->info)
8b127cbc 10858 || flinfo->info->emitrelocations)
c152c796
AM
10859 {
10860 Elf_Internal_Rela *irela;
d4730f92 10861 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10862 bfd_vma last_offset;
10863 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10864 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10865 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10866 unsigned int next_erel;
c152c796 10867 bfd_boolean rela_normal;
d4730f92 10868 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10869
d4730f92
BS
10870 esdi = elf_section_data (o);
10871 esdo = elf_section_data (o->output_section);
10872 rela_normal = FALSE;
c152c796
AM
10873
10874 /* Adjust the reloc addresses and symbol indices. */
10875
10876 irela = internal_relocs;
056bafd4 10877 irelaend = irela + o->reloc_count;
d4730f92
BS
10878 rel_hash = esdo->rel.hashes + esdo->rel.count;
10879 /* We start processing the REL relocs, if any. When we reach
10880 IRELAMID in the loop, we switch to the RELA relocs. */
10881 irelamid = irela;
10882 if (esdi->rel.hdr != NULL)
10883 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10884 * bed->s->int_rels_per_ext_rel);
eac338cf 10885 rel_hash_list = rel_hash;
d4730f92 10886 rela_hash_list = NULL;
c152c796 10887 last_offset = o->output_offset;
0e1862bb 10888 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10889 last_offset += o->output_section->vma;
10890 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10891 {
10892 unsigned long r_symndx;
10893 asection *sec;
10894 Elf_Internal_Sym sym;
10895
10896 if (next_erel == bed->s->int_rels_per_ext_rel)
10897 {
10898 rel_hash++;
10899 next_erel = 0;
10900 }
10901
d4730f92
BS
10902 if (irela == irelamid)
10903 {
10904 rel_hash = esdo->rela.hashes + esdo->rela.count;
10905 rela_hash_list = rel_hash;
10906 rela_normal = bed->rela_normal;
10907 }
10908
c152c796 10909 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10910 flinfo->info, o,
c152c796
AM
10911 irela->r_offset);
10912 if (irela->r_offset >= (bfd_vma) -2)
10913 {
10914 /* This is a reloc for a deleted entry or somesuch.
10915 Turn it into an R_*_NONE reloc, at the same
10916 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10917 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10918 being ordered. */
10919 irela->r_offset = last_offset;
10920 irela->r_info = 0;
10921 irela->r_addend = 0;
10922 continue;
10923 }
10924
10925 irela->r_offset += o->output_offset;
10926
10927 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10928 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10929 irela->r_offset += o->output_section->vma;
10930
10931 last_offset = irela->r_offset;
10932
10933 r_symndx = irela->r_info >> r_sym_shift;
10934 if (r_symndx == STN_UNDEF)
10935 continue;
10936
10937 if (r_symndx >= locsymcount
10938 || (elf_bad_symtab (input_bfd)
8b127cbc 10939 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10940 {
10941 struct elf_link_hash_entry *rh;
10942 unsigned long indx;
10943
10944 /* This is a reloc against a global symbol. We
10945 have not yet output all the local symbols, so
10946 we do not know the symbol index of any global
10947 symbol. We set the rel_hash entry for this
10948 reloc to point to the global hash table entry
10949 for this symbol. The symbol index is then
ee75fd95 10950 set at the end of bfd_elf_final_link. */
c152c796
AM
10951 indx = r_symndx - extsymoff;
10952 rh = elf_sym_hashes (input_bfd)[indx];
10953 while (rh->root.type == bfd_link_hash_indirect
10954 || rh->root.type == bfd_link_hash_warning)
10955 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10956
10957 /* Setting the index to -2 tells
10958 elf_link_output_extsym that this symbol is
10959 used by a reloc. */
10960 BFD_ASSERT (rh->indx < 0);
10961 rh->indx = -2;
c152c796
AM
10962 *rel_hash = rh;
10963
10964 continue;
10965 }
10966
10967 /* This is a reloc against a local symbol. */
10968
10969 *rel_hash = NULL;
10970 sym = isymbuf[r_symndx];
8b127cbc 10971 sec = flinfo->sections[r_symndx];
c152c796
AM
10972 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10973 {
10974 /* I suppose the backend ought to fill in the
10975 section of any STT_SECTION symbol against a
6a8d1586 10976 processor specific section. */
cf35638d 10977 r_symndx = STN_UNDEF;
6a8d1586
AM
10978 if (bfd_is_abs_section (sec))
10979 ;
c152c796
AM
10980 else if (sec == NULL || sec->owner == NULL)
10981 {
10982 bfd_set_error (bfd_error_bad_value);
10983 return FALSE;
10984 }
10985 else
10986 {
6a8d1586
AM
10987 asection *osec = sec->output_section;
10988
10989 /* If we have discarded a section, the output
10990 section will be the absolute section. In
ab96bf03
AM
10991 case of discarded SEC_MERGE sections, use
10992 the kept section. relocate_section should
10993 have already handled discarded linkonce
10994 sections. */
6a8d1586
AM
10995 if (bfd_is_abs_section (osec)
10996 && sec->kept_section != NULL
10997 && sec->kept_section->output_section != NULL)
10998 {
10999 osec = sec->kept_section->output_section;
11000 irela->r_addend -= osec->vma;
11001 }
11002
11003 if (!bfd_is_abs_section (osec))
11004 {
11005 r_symndx = osec->target_index;
cf35638d 11006 if (r_symndx == STN_UNDEF)
74541ad4 11007 {
051d833a
AM
11008 irela->r_addend += osec->vma;
11009 osec = _bfd_nearby_section (output_bfd, osec,
11010 osec->vma);
11011 irela->r_addend -= osec->vma;
11012 r_symndx = osec->target_index;
74541ad4 11013 }
6a8d1586 11014 }
c152c796
AM
11015 }
11016
11017 /* Adjust the addend according to where the
11018 section winds up in the output section. */
11019 if (rela_normal)
11020 irela->r_addend += sec->output_offset;
11021 }
11022 else
11023 {
8b127cbc 11024 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11025 {
11026 unsigned long shlink;
11027 const char *name;
11028 asection *osec;
6e0b88f1 11029 long indx;
c152c796 11030
8b127cbc 11031 if (flinfo->info->strip == strip_all)
c152c796
AM
11032 {
11033 /* You can't do ld -r -s. */
11034 bfd_set_error (bfd_error_invalid_operation);
11035 return FALSE;
11036 }
11037
11038 /* This symbol was skipped earlier, but
11039 since it is needed by a reloc, we
11040 must output it now. */
11041 shlink = symtab_hdr->sh_link;
11042 name = (bfd_elf_string_from_elf_section
11043 (input_bfd, shlink, sym.st_name));
11044 if (name == NULL)
11045 return FALSE;
11046
11047 osec = sec->output_section;
11048 sym.st_shndx =
11049 _bfd_elf_section_from_bfd_section (output_bfd,
11050 osec);
11051 if (sym.st_shndx == SHN_BAD)
11052 return FALSE;
11053
11054 sym.st_value += sec->output_offset;
0e1862bb 11055 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11056 {
11057 sym.st_value += osec->vma;
11058 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11059 {
102def4d
AM
11060 struct elf_link_hash_table *htab
11061 = elf_hash_table (flinfo->info);
11062
c152c796
AM
11063 /* STT_TLS symbols are relative to PT_TLS
11064 segment base. */
102def4d
AM
11065 if (htab->tls_sec != NULL)
11066 sym.st_value -= htab->tls_sec->vma;
11067 else
11068 sym.st_info
11069 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11070 STT_NOTYPE);
c152c796
AM
11071 }
11072 }
11073
6e0b88f1 11074 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11075 ret = elf_link_output_symstrtab (flinfo, name,
11076 &sym, sec,
11077 NULL);
6e0b88f1 11078 if (ret == 0)
c152c796 11079 return FALSE;
6e0b88f1 11080 else if (ret == 1)
8b127cbc 11081 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11082 else
11083 abort ();
c152c796
AM
11084 }
11085
8b127cbc 11086 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11087 }
11088
11089 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11090 | (irela->r_info & r_type_mask));
11091 }
11092
11093 /* Swap out the relocs. */
d4730f92
BS
11094 input_rel_hdr = esdi->rel.hdr;
11095 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11096 {
d4730f92
BS
11097 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11098 input_rel_hdr,
11099 internal_relocs,
11100 rel_hash_list))
11101 return FALSE;
c152c796
AM
11102 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11103 * bed->s->int_rels_per_ext_rel);
eac338cf 11104 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11105 }
11106
11107 input_rela_hdr = esdi->rela.hdr;
11108 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11109 {
eac338cf 11110 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11111 input_rela_hdr,
eac338cf 11112 internal_relocs,
d4730f92 11113 rela_hash_list))
c152c796
AM
11114 return FALSE;
11115 }
11116 }
11117 }
11118
11119 /* Write out the modified section contents. */
11120 if (bed->elf_backend_write_section
8b127cbc 11121 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11122 contents))
c152c796
AM
11123 {
11124 /* Section written out. */
11125 }
11126 else switch (o->sec_info_type)
11127 {
dbaa2011 11128 case SEC_INFO_TYPE_STABS:
c152c796
AM
11129 if (! (_bfd_write_section_stabs
11130 (output_bfd,
8b127cbc 11131 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11132 o, &elf_section_data (o)->sec_info, contents)))
11133 return FALSE;
11134 break;
dbaa2011 11135 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11136 if (! _bfd_write_merged_section (output_bfd, o,
11137 elf_section_data (o)->sec_info))
11138 return FALSE;
11139 break;
dbaa2011 11140 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11141 {
8b127cbc 11142 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11143 o, contents))
11144 return FALSE;
11145 }
11146 break;
2f0c68f2
CM
11147 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11148 {
11149 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11150 flinfo->info,
11151 o, contents))
11152 return FALSE;
11153 }
11154 break;
c152c796
AM
11155 default:
11156 {
310fd250
L
11157 if (! (o->flags & SEC_EXCLUDE))
11158 {
11159 file_ptr offset = (file_ptr) o->output_offset;
11160 bfd_size_type todo = o->size;
37b01f6a
DG
11161
11162 offset *= bfd_octets_per_byte (output_bfd);
11163
310fd250
L
11164 if ((o->flags & SEC_ELF_REVERSE_COPY))
11165 {
11166 /* Reverse-copy input section to output. */
11167 do
11168 {
11169 todo -= address_size;
11170 if (! bfd_set_section_contents (output_bfd,
11171 o->output_section,
11172 contents + todo,
11173 offset,
11174 address_size))
11175 return FALSE;
11176 if (todo == 0)
11177 break;
11178 offset += address_size;
11179 }
11180 while (1);
11181 }
11182 else if (! bfd_set_section_contents (output_bfd,
11183 o->output_section,
11184 contents,
11185 offset, todo))
11186 return FALSE;
11187 }
c152c796
AM
11188 }
11189 break;
11190 }
11191 }
11192
11193 return TRUE;
11194}
11195
11196/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11197 requested by the linker, and does not come from any input file. This
c152c796
AM
11198 is used to build constructor and destructor tables when linking
11199 with -Ur. */
11200
11201static bfd_boolean
11202elf_reloc_link_order (bfd *output_bfd,
11203 struct bfd_link_info *info,
11204 asection *output_section,
11205 struct bfd_link_order *link_order)
11206{
11207 reloc_howto_type *howto;
11208 long indx;
11209 bfd_vma offset;
11210 bfd_vma addend;
d4730f92 11211 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11212 struct elf_link_hash_entry **rel_hash_ptr;
11213 Elf_Internal_Shdr *rel_hdr;
11214 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11215 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11216 bfd_byte *erel;
11217 unsigned int i;
d4730f92 11218 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11219
11220 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11221 if (howto == NULL)
11222 {
11223 bfd_set_error (bfd_error_bad_value);
11224 return FALSE;
11225 }
11226
11227 addend = link_order->u.reloc.p->addend;
11228
d4730f92
BS
11229 if (esdo->rel.hdr)
11230 reldata = &esdo->rel;
11231 else if (esdo->rela.hdr)
11232 reldata = &esdo->rela;
11233 else
11234 {
11235 reldata = NULL;
11236 BFD_ASSERT (0);
11237 }
11238
c152c796 11239 /* Figure out the symbol index. */
d4730f92 11240 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11241 if (link_order->type == bfd_section_reloc_link_order)
11242 {
11243 indx = link_order->u.reloc.p->u.section->target_index;
11244 BFD_ASSERT (indx != 0);
11245 *rel_hash_ptr = NULL;
11246 }
11247 else
11248 {
11249 struct elf_link_hash_entry *h;
11250
11251 /* Treat a reloc against a defined symbol as though it were
11252 actually against the section. */
11253 h = ((struct elf_link_hash_entry *)
11254 bfd_wrapped_link_hash_lookup (output_bfd, info,
11255 link_order->u.reloc.p->u.name,
11256 FALSE, FALSE, TRUE));
11257 if (h != NULL
11258 && (h->root.type == bfd_link_hash_defined
11259 || h->root.type == bfd_link_hash_defweak))
11260 {
11261 asection *section;
11262
11263 section = h->root.u.def.section;
11264 indx = section->output_section->target_index;
11265 *rel_hash_ptr = NULL;
11266 /* It seems that we ought to add the symbol value to the
11267 addend here, but in practice it has already been added
11268 because it was passed to constructor_callback. */
11269 addend += section->output_section->vma + section->output_offset;
11270 }
11271 else if (h != NULL)
11272 {
11273 /* Setting the index to -2 tells elf_link_output_extsym that
11274 this symbol is used by a reloc. */
11275 h->indx = -2;
11276 *rel_hash_ptr = h;
11277 indx = 0;
11278 }
11279 else
11280 {
1a72702b
AM
11281 (*info->callbacks->unattached_reloc)
11282 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11283 indx = 0;
11284 }
11285 }
11286
11287 /* If this is an inplace reloc, we must write the addend into the
11288 object file. */
11289 if (howto->partial_inplace && addend != 0)
11290 {
11291 bfd_size_type size;
11292 bfd_reloc_status_type rstat;
11293 bfd_byte *buf;
11294 bfd_boolean ok;
11295 const char *sym_name;
11296
a50b1753
NC
11297 size = (bfd_size_type) bfd_get_reloc_size (howto);
11298 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11299 if (buf == NULL && size != 0)
c152c796
AM
11300 return FALSE;
11301 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11302 switch (rstat)
11303 {
11304 case bfd_reloc_ok:
11305 break;
11306
11307 default:
11308 case bfd_reloc_outofrange:
11309 abort ();
11310
11311 case bfd_reloc_overflow:
11312 if (link_order->type == bfd_section_reloc_link_order)
11313 sym_name = bfd_section_name (output_bfd,
11314 link_order->u.reloc.p->u.section);
11315 else
11316 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11317 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11318 howto->name, addend, NULL, NULL,
11319 (bfd_vma) 0);
c152c796
AM
11320 break;
11321 }
37b01f6a 11322
c152c796 11323 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11324 link_order->offset
11325 * bfd_octets_per_byte (output_bfd),
11326 size);
c152c796
AM
11327 free (buf);
11328 if (! ok)
11329 return FALSE;
11330 }
11331
11332 /* The address of a reloc is relative to the section in a
11333 relocatable file, and is a virtual address in an executable
11334 file. */
11335 offset = link_order->offset;
0e1862bb 11336 if (! bfd_link_relocatable (info))
c152c796
AM
11337 offset += output_section->vma;
11338
11339 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11340 {
11341 irel[i].r_offset = offset;
11342 irel[i].r_info = 0;
11343 irel[i].r_addend = 0;
11344 }
11345 if (bed->s->arch_size == 32)
11346 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11347 else
11348 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11349
d4730f92 11350 rel_hdr = reldata->hdr;
c152c796
AM
11351 erel = rel_hdr->contents;
11352 if (rel_hdr->sh_type == SHT_REL)
11353 {
d4730f92 11354 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11355 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11356 }
11357 else
11358 {
11359 irel[0].r_addend = addend;
d4730f92 11360 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11361 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11362 }
11363
d4730f92 11364 ++reldata->count;
c152c796
AM
11365
11366 return TRUE;
11367}
11368
0b52efa6
PB
11369
11370/* Get the output vma of the section pointed to by the sh_link field. */
11371
11372static bfd_vma
11373elf_get_linked_section_vma (struct bfd_link_order *p)
11374{
11375 Elf_Internal_Shdr **elf_shdrp;
11376 asection *s;
11377 int elfsec;
11378
11379 s = p->u.indirect.section;
11380 elf_shdrp = elf_elfsections (s->owner);
11381 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11382 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11383 /* PR 290:
11384 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11385 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11386 sh_info fields. Hence we could get the situation
11387 where elfsec is 0. */
11388 if (elfsec == 0)
11389 {
11390 const struct elf_backend_data *bed
11391 = get_elf_backend_data (s->owner);
11392 if (bed->link_order_error_handler)
d003868e 11393 bed->link_order_error_handler
695344c0 11394 /* xgettext:c-format */
871b3ab2 11395 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11396 return 0;
11397 }
11398 else
11399 {
11400 s = elf_shdrp[elfsec]->bfd_section;
11401 return s->output_section->vma + s->output_offset;
11402 }
0b52efa6
PB
11403}
11404
11405
11406/* Compare two sections based on the locations of the sections they are
11407 linked to. Used by elf_fixup_link_order. */
11408
11409static int
11410compare_link_order (const void * a, const void * b)
11411{
11412 bfd_vma apos;
11413 bfd_vma bpos;
11414
11415 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11416 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11417 if (apos < bpos)
11418 return -1;
11419 return apos > bpos;
11420}
11421
11422
11423/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11424 order as their linked sections. Returns false if this could not be done
11425 because an output section includes both ordered and unordered
11426 sections. Ideally we'd do this in the linker proper. */
11427
11428static bfd_boolean
11429elf_fixup_link_order (bfd *abfd, asection *o)
11430{
11431 int seen_linkorder;
11432 int seen_other;
11433 int n;
11434 struct bfd_link_order *p;
11435 bfd *sub;
11436 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11437 unsigned elfsec;
0b52efa6 11438 struct bfd_link_order **sections;
d33cdfe3 11439 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11440 bfd_vma offset;
3b36f7e6 11441
d33cdfe3
L
11442 other_sec = NULL;
11443 linkorder_sec = NULL;
0b52efa6
PB
11444 seen_other = 0;
11445 seen_linkorder = 0;
8423293d 11446 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11447 {
d33cdfe3 11448 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11449 {
11450 s = p->u.indirect.section;
d33cdfe3
L
11451 sub = s->owner;
11452 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11453 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11454 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11455 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11456 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11457 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11458 {
11459 seen_linkorder++;
11460 linkorder_sec = s;
11461 }
0b52efa6 11462 else
d33cdfe3
L
11463 {
11464 seen_other++;
11465 other_sec = s;
11466 }
0b52efa6
PB
11467 }
11468 else
11469 seen_other++;
d33cdfe3
L
11470
11471 if (seen_other && seen_linkorder)
11472 {
11473 if (other_sec && linkorder_sec)
4eca0228 11474 _bfd_error_handler
695344c0 11475 /* xgettext:c-format */
871b3ab2
AM
11476 (_("%pA has both ordered [`%pA' in %pB] "
11477 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11478 o, linkorder_sec, linkorder_sec->owner,
11479 other_sec, other_sec->owner);
d33cdfe3 11480 else
4eca0228 11481 _bfd_error_handler
871b3ab2 11482 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11483 bfd_set_error (bfd_error_bad_value);
11484 return FALSE;
11485 }
0b52efa6
PB
11486 }
11487
11488 if (!seen_linkorder)
11489 return TRUE;
11490
0b52efa6 11491 sections = (struct bfd_link_order **)
14b1c01e
AM
11492 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11493 if (sections == NULL)
11494 return FALSE;
0b52efa6 11495 seen_linkorder = 0;
3b36f7e6 11496
8423293d 11497 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11498 {
11499 sections[seen_linkorder++] = p;
11500 }
11501 /* Sort the input sections in the order of their linked section. */
11502 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11503 compare_link_order);
11504
11505 /* Change the offsets of the sections. */
11506 offset = 0;
11507 for (n = 0; n < seen_linkorder; n++)
11508 {
11509 s = sections[n]->u.indirect.section;
461686a3 11510 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11511 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11512 sections[n]->offset = offset;
11513 offset += sections[n]->size;
11514 }
11515
4dd07732 11516 free (sections);
0b52efa6
PB
11517 return TRUE;
11518}
11519
76359541
TP
11520/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11521 Returns TRUE upon success, FALSE otherwise. */
11522
11523static bfd_boolean
11524elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11525{
11526 bfd_boolean ret = FALSE;
11527 bfd *implib_bfd;
11528 const struct elf_backend_data *bed;
11529 flagword flags;
11530 enum bfd_architecture arch;
11531 unsigned int mach;
11532 asymbol **sympp = NULL;
11533 long symsize;
11534 long symcount;
11535 long src_count;
11536 elf_symbol_type *osymbuf;
11537
11538 implib_bfd = info->out_implib_bfd;
11539 bed = get_elf_backend_data (abfd);
11540
11541 if (!bfd_set_format (implib_bfd, bfd_object))
11542 return FALSE;
11543
046734ff 11544 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11545 flags = bfd_get_file_flags (abfd);
11546 flags &= ~HAS_RELOC;
11547 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11548 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11549 return FALSE;
11550
11551 /* Copy architecture of output file to import library file. */
11552 arch = bfd_get_arch (abfd);
11553 mach = bfd_get_mach (abfd);
11554 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11555 && (abfd->target_defaulted
11556 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11557 return FALSE;
11558
11559 /* Get symbol table size. */
11560 symsize = bfd_get_symtab_upper_bound (abfd);
11561 if (symsize < 0)
11562 return FALSE;
11563
11564 /* Read in the symbol table. */
11565 sympp = (asymbol **) xmalloc (symsize);
11566 symcount = bfd_canonicalize_symtab (abfd, sympp);
11567 if (symcount < 0)
11568 goto free_sym_buf;
11569
11570 /* Allow the BFD backend to copy any private header data it
11571 understands from the output BFD to the import library BFD. */
11572 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11573 goto free_sym_buf;
11574
11575 /* Filter symbols to appear in the import library. */
11576 if (bed->elf_backend_filter_implib_symbols)
11577 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11578 symcount);
11579 else
11580 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11581 if (symcount == 0)
11582 {
5df1bc57 11583 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11584 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11585 implib_bfd);
76359541
TP
11586 goto free_sym_buf;
11587 }
11588
11589
11590 /* Make symbols absolute. */
11591 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11592 sizeof (*osymbuf));
11593 for (src_count = 0; src_count < symcount; src_count++)
11594 {
11595 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11596 sizeof (*osymbuf));
11597 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11598 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11599 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11600 osymbuf[src_count].internal_elf_sym.st_value =
11601 osymbuf[src_count].symbol.value;
11602 sympp[src_count] = &osymbuf[src_count].symbol;
11603 }
11604
11605 bfd_set_symtab (implib_bfd, sympp, symcount);
11606
11607 /* Allow the BFD backend to copy any private data it understands
11608 from the output BFD to the import library BFD. This is done last
11609 to permit the routine to look at the filtered symbol table. */
11610 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11611 goto free_sym_buf;
11612
11613 if (!bfd_close (implib_bfd))
11614 goto free_sym_buf;
11615
11616 ret = TRUE;
11617
11618free_sym_buf:
11619 free (sympp);
11620 return ret;
11621}
11622
9f7c3e5e
AM
11623static void
11624elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11625{
11626 asection *o;
11627
11628 if (flinfo->symstrtab != NULL)
ef10c3ac 11629 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11630 if (flinfo->contents != NULL)
11631 free (flinfo->contents);
11632 if (flinfo->external_relocs != NULL)
11633 free (flinfo->external_relocs);
11634 if (flinfo->internal_relocs != NULL)
11635 free (flinfo->internal_relocs);
11636 if (flinfo->external_syms != NULL)
11637 free (flinfo->external_syms);
11638 if (flinfo->locsym_shndx != NULL)
11639 free (flinfo->locsym_shndx);
11640 if (flinfo->internal_syms != NULL)
11641 free (flinfo->internal_syms);
11642 if (flinfo->indices != NULL)
11643 free (flinfo->indices);
11644 if (flinfo->sections != NULL)
11645 free (flinfo->sections);
9f7c3e5e
AM
11646 if (flinfo->symshndxbuf != NULL)
11647 free (flinfo->symshndxbuf);
11648 for (o = obfd->sections; o != NULL; o = o->next)
11649 {
11650 struct bfd_elf_section_data *esdo = elf_section_data (o);
11651 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11652 free (esdo->rel.hashes);
11653 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11654 free (esdo->rela.hashes);
11655 }
11656}
0b52efa6 11657
c152c796
AM
11658/* Do the final step of an ELF link. */
11659
11660bfd_boolean
11661bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11662{
11663 bfd_boolean dynamic;
11664 bfd_boolean emit_relocs;
11665 bfd *dynobj;
8b127cbc 11666 struct elf_final_link_info flinfo;
91d6fa6a
NC
11667 asection *o;
11668 struct bfd_link_order *p;
11669 bfd *sub;
c152c796
AM
11670 bfd_size_type max_contents_size;
11671 bfd_size_type max_external_reloc_size;
11672 bfd_size_type max_internal_reloc_count;
11673 bfd_size_type max_sym_count;
11674 bfd_size_type max_sym_shndx_count;
c152c796
AM
11675 Elf_Internal_Sym elfsym;
11676 unsigned int i;
11677 Elf_Internal_Shdr *symtab_hdr;
11678 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11679 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11680 struct elf_outext_info eoinfo;
11681 bfd_boolean merged;
11682 size_t relativecount = 0;
11683 asection *reldyn = 0;
11684 bfd_size_type amt;
104d59d1
JM
11685 asection *attr_section = NULL;
11686 bfd_vma attr_size = 0;
11687 const char *std_attrs_section;
64f52338 11688 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11689
64f52338 11690 if (!is_elf_hash_table (htab))
c152c796
AM
11691 return FALSE;
11692
0e1862bb 11693 if (bfd_link_pic (info))
c152c796
AM
11694 abfd->flags |= DYNAMIC;
11695
64f52338
AM
11696 dynamic = htab->dynamic_sections_created;
11697 dynobj = htab->dynobj;
c152c796 11698
0e1862bb 11699 emit_relocs = (bfd_link_relocatable (info)
a4676736 11700 || info->emitrelocations);
c152c796 11701
8b127cbc
AM
11702 flinfo.info = info;
11703 flinfo.output_bfd = abfd;
ef10c3ac 11704 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11705 if (flinfo.symstrtab == NULL)
c152c796
AM
11706 return FALSE;
11707
11708 if (! dynamic)
11709 {
8b127cbc
AM
11710 flinfo.hash_sec = NULL;
11711 flinfo.symver_sec = NULL;
c152c796
AM
11712 }
11713 else
11714 {
3d4d4302 11715 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11716 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11717 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11718 /* Note that it is OK if symver_sec is NULL. */
11719 }
11720
8b127cbc
AM
11721 flinfo.contents = NULL;
11722 flinfo.external_relocs = NULL;
11723 flinfo.internal_relocs = NULL;
11724 flinfo.external_syms = NULL;
11725 flinfo.locsym_shndx = NULL;
11726 flinfo.internal_syms = NULL;
11727 flinfo.indices = NULL;
11728 flinfo.sections = NULL;
8b127cbc 11729 flinfo.symshndxbuf = NULL;
ffbc01cc 11730 flinfo.filesym_count = 0;
c152c796 11731
104d59d1
JM
11732 /* The object attributes have been merged. Remove the input
11733 sections from the link, and set the contents of the output
11734 secton. */
11735 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11736 for (o = abfd->sections; o != NULL; o = o->next)
11737 {
5270eddc 11738 bfd_boolean remove_section = FALSE;
b8a6ced7 11739
104d59d1
JM
11740 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11741 || strcmp (o->name, ".gnu.attributes") == 0)
11742 {
11743 for (p = o->map_head.link_order; p != NULL; p = p->next)
11744 {
11745 asection *input_section;
11746
11747 if (p->type != bfd_indirect_link_order)
11748 continue;
11749 input_section = p->u.indirect.section;
11750 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11751 elf_link_input_bfd ignores this section. */
11752 input_section->flags &= ~SEC_HAS_CONTENTS;
11753 }
a0c8462f 11754
104d59d1 11755 attr_size = bfd_elf_obj_attr_size (abfd);
b8a6ced7
AM
11756 bfd_set_section_size (abfd, o, attr_size);
11757 /* Skip this section later on. */
11758 o->map_head.link_order = NULL;
104d59d1 11759 if (attr_size)
b8a6ced7 11760 attr_section = o;
104d59d1 11761 else
5270eddc 11762 remove_section = TRUE;
104d59d1 11763 }
6e5e9d58
AM
11764 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11765 {
11766 /* Remove empty group section from linker output. */
5270eddc 11767 remove_section = TRUE;
b8a6ced7 11768 }
5270eddc 11769 if (remove_section)
b8a6ced7 11770 {
6e5e9d58
AM
11771 o->flags |= SEC_EXCLUDE;
11772 bfd_section_list_remove (abfd, o);
11773 abfd->section_count--;
11774 }
104d59d1
JM
11775 }
11776
c152c796
AM
11777 /* Count up the number of relocations we will output for each output
11778 section, so that we know the sizes of the reloc sections. We
11779 also figure out some maximum sizes. */
11780 max_contents_size = 0;
11781 max_external_reloc_size = 0;
11782 max_internal_reloc_count = 0;
11783 max_sym_count = 0;
11784 max_sym_shndx_count = 0;
11785 merged = FALSE;
11786 for (o = abfd->sections; o != NULL; o = o->next)
11787 {
11788 struct bfd_elf_section_data *esdo = elf_section_data (o);
11789 o->reloc_count = 0;
11790
8423293d 11791 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11792 {
11793 unsigned int reloc_count = 0;
9eaff861 11794 unsigned int additional_reloc_count = 0;
c152c796 11795 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11796
11797 if (p->type == bfd_section_reloc_link_order
11798 || p->type == bfd_symbol_reloc_link_order)
11799 reloc_count = 1;
11800 else if (p->type == bfd_indirect_link_order)
11801 {
11802 asection *sec;
11803
11804 sec = p->u.indirect.section;
c152c796
AM
11805
11806 /* Mark all sections which are to be included in the
11807 link. This will normally be every section. We need
11808 to do this so that we can identify any sections which
11809 the linker has decided to not include. */
11810 sec->linker_mark = TRUE;
11811
11812 if (sec->flags & SEC_MERGE)
11813 merged = TRUE;
11814
eea6121a
AM
11815 if (sec->rawsize > max_contents_size)
11816 max_contents_size = sec->rawsize;
11817 if (sec->size > max_contents_size)
11818 max_contents_size = sec->size;
c152c796 11819
c152c796
AM
11820 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11821 && (sec->owner->flags & DYNAMIC) == 0)
11822 {
11823 size_t sym_count;
11824
a961cdd5
AM
11825 /* We are interested in just local symbols, not all
11826 symbols. */
c152c796
AM
11827 if (elf_bad_symtab (sec->owner))
11828 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11829 / bed->s->sizeof_sym);
11830 else
11831 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11832
11833 if (sym_count > max_sym_count)
11834 max_sym_count = sym_count;
11835
11836 if (sym_count > max_sym_shndx_count
6a40cf0c 11837 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11838 max_sym_shndx_count = sym_count;
11839
a961cdd5
AM
11840 if (esdo->this_hdr.sh_type == SHT_REL
11841 || esdo->this_hdr.sh_type == SHT_RELA)
11842 /* Some backends use reloc_count in relocation sections
11843 to count particular types of relocs. Of course,
11844 reloc sections themselves can't have relocations. */
11845 ;
11846 else if (emit_relocs)
11847 {
11848 reloc_count = sec->reloc_count;
11849 if (bed->elf_backend_count_additional_relocs)
11850 {
11851 int c;
11852 c = (*bed->elf_backend_count_additional_relocs) (sec);
11853 additional_reloc_count += c;
11854 }
11855 }
11856 else if (bed->elf_backend_count_relocs)
11857 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11858
11859 esdi = elf_section_data (sec);
11860
c152c796
AM
11861 if ((sec->flags & SEC_RELOC) != 0)
11862 {
d4730f92 11863 size_t ext_size = 0;
c152c796 11864
d4730f92
BS
11865 if (esdi->rel.hdr != NULL)
11866 ext_size = esdi->rel.hdr->sh_size;
11867 if (esdi->rela.hdr != NULL)
11868 ext_size += esdi->rela.hdr->sh_size;
7326c758 11869
c152c796
AM
11870 if (ext_size > max_external_reloc_size)
11871 max_external_reloc_size = ext_size;
11872 if (sec->reloc_count > max_internal_reloc_count)
11873 max_internal_reloc_count = sec->reloc_count;
11874 }
11875 }
11876 }
11877
11878 if (reloc_count == 0)
11879 continue;
11880
9eaff861 11881 reloc_count += additional_reloc_count;
c152c796
AM
11882 o->reloc_count += reloc_count;
11883
0e1862bb 11884 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11885 {
d4730f92 11886 if (esdi->rel.hdr)
9eaff861 11887 {
491d01d3 11888 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11889 esdo->rel.count += additional_reloc_count;
11890 }
d4730f92 11891 if (esdi->rela.hdr)
9eaff861 11892 {
491d01d3 11893 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11894 esdo->rela.count += additional_reloc_count;
11895 }
d4730f92
BS
11896 }
11897 else
11898 {
11899 if (o->use_rela_p)
11900 esdo->rela.count += reloc_count;
2c2b4ed4 11901 else
d4730f92 11902 esdo->rel.count += reloc_count;
c152c796 11903 }
c152c796
AM
11904 }
11905
9eaff861 11906 if (o->reloc_count > 0)
c152c796
AM
11907 o->flags |= SEC_RELOC;
11908 else
11909 {
11910 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11911 set it (this is probably a bug) and if it is set
11912 assign_section_numbers will create a reloc section. */
11913 o->flags &=~ SEC_RELOC;
11914 }
11915
11916 /* If the SEC_ALLOC flag is not set, force the section VMA to
11917 zero. This is done in elf_fake_sections as well, but forcing
11918 the VMA to 0 here will ensure that relocs against these
11919 sections are handled correctly. */
11920 if ((o->flags & SEC_ALLOC) == 0
11921 && ! o->user_set_vma)
11922 o->vma = 0;
11923 }
11924
0e1862bb 11925 if (! bfd_link_relocatable (info) && merged)
64f52338 11926 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11927
11928 /* Figure out the file positions for everything but the symbol table
11929 and the relocs. We set symcount to force assign_section_numbers
11930 to create a symbol table. */
8539e4e8 11931 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11932 BFD_ASSERT (! abfd->output_has_begun);
11933 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11934 goto error_return;
11935
ee75fd95 11936 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11937 for (o = abfd->sections; o != NULL; o = o->next)
11938 {
d4730f92 11939 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11940 if ((o->flags & SEC_RELOC) != 0)
11941 {
d4730f92 11942 if (esdo->rel.hdr
9eaff861 11943 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11944 goto error_return;
11945
d4730f92 11946 if (esdo->rela.hdr
9eaff861 11947 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11948 goto error_return;
11949 }
11950
11951 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11952 to count upwards while actually outputting the relocations. */
d4730f92
BS
11953 esdo->rel.count = 0;
11954 esdo->rela.count = 0;
0ce398f1
L
11955
11956 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11957 {
11958 /* Cache the section contents so that they can be compressed
11959 later. Use bfd_malloc since it will be freed by
11960 bfd_compress_section_contents. */
11961 unsigned char *contents = esdo->this_hdr.contents;
11962 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11963 abort ();
11964 contents
11965 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11966 if (contents == NULL)
11967 goto error_return;
11968 esdo->this_hdr.contents = contents;
11969 }
c152c796
AM
11970 }
11971
c152c796 11972 /* We have now assigned file positions for all the sections except
a485e98e
AM
11973 .symtab, .strtab, and non-loaded reloc sections. We start the
11974 .symtab section at the current file position, and write directly
11975 to it. We build the .strtab section in memory. */
c152c796
AM
11976 bfd_get_symcount (abfd) = 0;
11977 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11978 /* sh_name is set in prep_headers. */
11979 symtab_hdr->sh_type = SHT_SYMTAB;
11980 /* sh_flags, sh_addr and sh_size all start off zero. */
11981 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11982 /* sh_link is set in assign_section_numbers. */
11983 /* sh_info is set below. */
11984 /* sh_offset is set just below. */
72de5009 11985 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11986
ef10c3ac
L
11987 if (max_sym_count < 20)
11988 max_sym_count = 20;
64f52338 11989 htab->strtabsize = max_sym_count;
ef10c3ac 11990 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11991 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11992 if (htab->strtab == NULL)
c152c796 11993 goto error_return;
ef10c3ac
L
11994 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11995 flinfo.symshndxbuf
11996 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11997 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11998
8539e4e8 11999 if (info->strip != strip_all || emit_relocs)
c152c796 12000 {
8539e4e8
AM
12001 file_ptr off = elf_next_file_pos (abfd);
12002
12003 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12004
12005 /* Note that at this point elf_next_file_pos (abfd) is
12006 incorrect. We do not yet know the size of the .symtab section.
12007 We correct next_file_pos below, after we do know the size. */
12008
12009 /* Start writing out the symbol table. The first symbol is always a
12010 dummy symbol. */
c152c796
AM
12011 elfsym.st_value = 0;
12012 elfsym.st_size = 0;
12013 elfsym.st_info = 0;
12014 elfsym.st_other = 0;
12015 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12016 elfsym.st_target_internal = 0;
ef10c3ac
L
12017 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12018 bfd_und_section_ptr, NULL) != 1)
c152c796 12019 goto error_return;
c152c796 12020
8539e4e8
AM
12021 /* Output a symbol for each section. We output these even if we are
12022 discarding local symbols, since they are used for relocs. These
12023 symbols have no names. We store the index of each one in the
12024 index field of the section, so that we can find it again when
12025 outputting relocs. */
12026
c152c796
AM
12027 elfsym.st_size = 0;
12028 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12029 elfsym.st_other = 0;
f0b5bb34 12030 elfsym.st_value = 0;
35fc36a8 12031 elfsym.st_target_internal = 0;
c152c796
AM
12032 for (i = 1; i < elf_numsections (abfd); i++)
12033 {
12034 o = bfd_section_from_elf_index (abfd, i);
12035 if (o != NULL)
f0b5bb34
AM
12036 {
12037 o->target_index = bfd_get_symcount (abfd);
12038 elfsym.st_shndx = i;
0e1862bb 12039 if (!bfd_link_relocatable (info))
f0b5bb34 12040 elfsym.st_value = o->vma;
ef10c3ac
L
12041 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12042 NULL) != 1)
f0b5bb34
AM
12043 goto error_return;
12044 }
c152c796
AM
12045 }
12046 }
12047
12048 /* Allocate some memory to hold information read in from the input
12049 files. */
12050 if (max_contents_size != 0)
12051 {
8b127cbc
AM
12052 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12053 if (flinfo.contents == NULL)
c152c796
AM
12054 goto error_return;
12055 }
12056
12057 if (max_external_reloc_size != 0)
12058 {
8b127cbc
AM
12059 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12060 if (flinfo.external_relocs == NULL)
c152c796
AM
12061 goto error_return;
12062 }
12063
12064 if (max_internal_reloc_count != 0)
12065 {
056bafd4 12066 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12067 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12068 if (flinfo.internal_relocs == NULL)
c152c796
AM
12069 goto error_return;
12070 }
12071
12072 if (max_sym_count != 0)
12073 {
12074 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12075 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12076 if (flinfo.external_syms == NULL)
c152c796
AM
12077 goto error_return;
12078
12079 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12080 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12081 if (flinfo.internal_syms == NULL)
c152c796
AM
12082 goto error_return;
12083
12084 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12085 flinfo.indices = (long int *) bfd_malloc (amt);
12086 if (flinfo.indices == NULL)
c152c796
AM
12087 goto error_return;
12088
12089 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12090 flinfo.sections = (asection **) bfd_malloc (amt);
12091 if (flinfo.sections == NULL)
c152c796
AM
12092 goto error_return;
12093 }
12094
12095 if (max_sym_shndx_count != 0)
12096 {
12097 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12098 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12099 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12100 goto error_return;
12101 }
12102
64f52338 12103 if (htab->tls_sec)
c152c796
AM
12104 {
12105 bfd_vma base, end = 0;
12106 asection *sec;
12107
64f52338 12108 for (sec = htab->tls_sec;
c152c796
AM
12109 sec && (sec->flags & SEC_THREAD_LOCAL);
12110 sec = sec->next)
12111 {
3a800eb9 12112 bfd_size_type size = sec->size;
c152c796 12113
3a800eb9
AM
12114 if (size == 0
12115 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12116 {
91d6fa6a
NC
12117 struct bfd_link_order *ord = sec->map_tail.link_order;
12118
12119 if (ord != NULL)
12120 size = ord->offset + ord->size;
c152c796
AM
12121 }
12122 end = sec->vma + size;
12123 }
64f52338 12124 base = htab->tls_sec->vma;
7dc98aea
RO
12125 /* Only align end of TLS section if static TLS doesn't have special
12126 alignment requirements. */
12127 if (bed->static_tls_alignment == 1)
64f52338
AM
12128 end = align_power (end, htab->tls_sec->alignment_power);
12129 htab->tls_size = end - base;
c152c796
AM
12130 }
12131
0b52efa6
PB
12132 /* Reorder SHF_LINK_ORDER sections. */
12133 for (o = abfd->sections; o != NULL; o = o->next)
12134 {
12135 if (!elf_fixup_link_order (abfd, o))
12136 return FALSE;
12137 }
12138
2f0c68f2
CM
12139 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12140 return FALSE;
12141
c152c796
AM
12142 /* Since ELF permits relocations to be against local symbols, we
12143 must have the local symbols available when we do the relocations.
12144 Since we would rather only read the local symbols once, and we
12145 would rather not keep them in memory, we handle all the
12146 relocations for a single input file at the same time.
12147
12148 Unfortunately, there is no way to know the total number of local
12149 symbols until we have seen all of them, and the local symbol
12150 indices precede the global symbol indices. This means that when
12151 we are generating relocatable output, and we see a reloc against
12152 a global symbol, we can not know the symbol index until we have
12153 finished examining all the local symbols to see which ones we are
12154 going to output. To deal with this, we keep the relocations in
12155 memory, and don't output them until the end of the link. This is
12156 an unfortunate waste of memory, but I don't see a good way around
12157 it. Fortunately, it only happens when performing a relocatable
12158 link, which is not the common case. FIXME: If keep_memory is set
12159 we could write the relocs out and then read them again; I don't
12160 know how bad the memory loss will be. */
12161
c72f2fb2 12162 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12163 sub->output_has_begun = FALSE;
12164 for (o = abfd->sections; o != NULL; o = o->next)
12165 {
8423293d 12166 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12167 {
12168 if (p->type == bfd_indirect_link_order
12169 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12170 == bfd_target_elf_flavour)
12171 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12172 {
12173 if (! sub->output_has_begun)
12174 {
8b127cbc 12175 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12176 goto error_return;
12177 sub->output_has_begun = TRUE;
12178 }
12179 }
12180 else if (p->type == bfd_section_reloc_link_order
12181 || p->type == bfd_symbol_reloc_link_order)
12182 {
12183 if (! elf_reloc_link_order (abfd, info, o, p))
12184 goto error_return;
12185 }
12186 else
12187 {
12188 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12189 {
12190 if (p->type == bfd_indirect_link_order
12191 && (bfd_get_flavour (sub)
12192 == bfd_target_elf_flavour)
12193 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12194 != bed->s->elfclass))
12195 {
12196 const char *iclass, *oclass;
12197
aebf9be7 12198 switch (bed->s->elfclass)
351f65ca 12199 {
aebf9be7
NC
12200 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12201 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12202 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12203 default: abort ();
351f65ca 12204 }
aebf9be7
NC
12205
12206 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12207 {
aebf9be7
NC
12208 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12209 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12210 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12211 default: abort ();
351f65ca
L
12212 }
12213
12214 bfd_set_error (bfd_error_wrong_format);
4eca0228 12215 _bfd_error_handler
695344c0 12216 /* xgettext:c-format */
871b3ab2 12217 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12218 sub, iclass, oclass);
12219 }
12220
12221 goto error_return;
12222 }
c152c796
AM
12223 }
12224 }
12225 }
12226
c0f00686
L
12227 /* Free symbol buffer if needed. */
12228 if (!info->reduce_memory_overheads)
12229 {
c72f2fb2 12230 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12231 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12232 && elf_tdata (sub)->symbuf)
c0f00686
L
12233 {
12234 free (elf_tdata (sub)->symbuf);
12235 elf_tdata (sub)->symbuf = NULL;
12236 }
12237 }
12238
c152c796
AM
12239 /* Output any global symbols that got converted to local in a
12240 version script or due to symbol visibility. We do this in a
12241 separate step since ELF requires all local symbols to appear
12242 prior to any global symbols. FIXME: We should only do this if
12243 some global symbols were, in fact, converted to become local.
12244 FIXME: Will this work correctly with the Irix 5 linker? */
12245 eoinfo.failed = FALSE;
8b127cbc 12246 eoinfo.flinfo = &flinfo;
c152c796 12247 eoinfo.localsyms = TRUE;
34a79995 12248 eoinfo.file_sym_done = FALSE;
7686d77d 12249 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12250 if (eoinfo.failed)
12251 return FALSE;
12252
4e617b1e
PB
12253 /* If backend needs to output some local symbols not present in the hash
12254 table, do it now. */
8539e4e8
AM
12255 if (bed->elf_backend_output_arch_local_syms
12256 && (info->strip != strip_all || emit_relocs))
4e617b1e 12257 {
6e0b88f1 12258 typedef int (*out_sym_func)
4e617b1e
PB
12259 (void *, const char *, Elf_Internal_Sym *, asection *,
12260 struct elf_link_hash_entry *);
12261
12262 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12263 (abfd, info, &flinfo,
12264 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12265 return FALSE;
12266 }
12267
c152c796
AM
12268 /* That wrote out all the local symbols. Finish up the symbol table
12269 with the global symbols. Even if we want to strip everything we
12270 can, we still need to deal with those global symbols that got
12271 converted to local in a version script. */
12272
12273 /* The sh_info field records the index of the first non local symbol. */
12274 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12275
12276 if (dynamic
64f52338
AM
12277 && htab->dynsym != NULL
12278 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12279 {
12280 Elf_Internal_Sym sym;
64f52338 12281 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12282
64f52338
AM
12283 o = htab->dynsym->output_section;
12284 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12285
12286 /* Write out the section symbols for the output sections. */
0e1862bb 12287 if (bfd_link_pic (info)
64f52338 12288 || htab->is_relocatable_executable)
c152c796
AM
12289 {
12290 asection *s;
12291
12292 sym.st_size = 0;
12293 sym.st_name = 0;
12294 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12295 sym.st_other = 0;
35fc36a8 12296 sym.st_target_internal = 0;
c152c796
AM
12297
12298 for (s = abfd->sections; s != NULL; s = s->next)
12299 {
12300 int indx;
12301 bfd_byte *dest;
12302 long dynindx;
12303
c152c796 12304 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12305 if (dynindx <= 0)
12306 continue;
12307 indx = elf_section_data (s)->this_idx;
c152c796
AM
12308 BFD_ASSERT (indx > 0);
12309 sym.st_shndx = indx;
c0d5a53d
L
12310 if (! check_dynsym (abfd, &sym))
12311 return FALSE;
c152c796
AM
12312 sym.st_value = s->vma;
12313 dest = dynsym + dynindx * bed->s->sizeof_sym;
12314 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12315 }
c152c796
AM
12316 }
12317
12318 /* Write out the local dynsyms. */
64f52338 12319 if (htab->dynlocal)
c152c796
AM
12320 {
12321 struct elf_link_local_dynamic_entry *e;
64f52338 12322 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12323 {
12324 asection *s;
12325 bfd_byte *dest;
12326
935bd1e0 12327 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12328 Note that we saved a word of storage and overwrote
12329 the original st_name with the dynstr_index. */
12330 sym = e->isym;
935bd1e0 12331 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12332
cb33740c
AM
12333 s = bfd_section_from_elf_index (e->input_bfd,
12334 e->isym.st_shndx);
12335 if (s != NULL)
c152c796 12336 {
c152c796
AM
12337 sym.st_shndx =
12338 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12339 if (! check_dynsym (abfd, &sym))
12340 return FALSE;
c152c796
AM
12341 sym.st_value = (s->output_section->vma
12342 + s->output_offset
12343 + e->isym.st_value);
12344 }
12345
c152c796
AM
12346 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12347 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12348 }
12349 }
c152c796
AM
12350 }
12351
12352 /* We get the global symbols from the hash table. */
12353 eoinfo.failed = FALSE;
12354 eoinfo.localsyms = FALSE;
8b127cbc 12355 eoinfo.flinfo = &flinfo;
7686d77d 12356 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12357 if (eoinfo.failed)
12358 return FALSE;
12359
12360 /* If backend needs to output some symbols not present in the hash
12361 table, do it now. */
8539e4e8
AM
12362 if (bed->elf_backend_output_arch_syms
12363 && (info->strip != strip_all || emit_relocs))
c152c796 12364 {
6e0b88f1 12365 typedef int (*out_sym_func)
c152c796
AM
12366 (void *, const char *, Elf_Internal_Sym *, asection *,
12367 struct elf_link_hash_entry *);
12368
12369 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12370 (abfd, info, &flinfo,
12371 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12372 return FALSE;
12373 }
12374
ef10c3ac
L
12375 /* Finalize the .strtab section. */
12376 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12377
12378 /* Swap out the .strtab section. */
12379 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12380 return FALSE;
12381
12382 /* Now we know the size of the symtab section. */
c152c796
AM
12383 if (bfd_get_symcount (abfd) > 0)
12384 {
ee3b52e9
L
12385 /* Finish up and write out the symbol string table (.strtab)
12386 section. */
ad32986f 12387 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12388 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12389
ad32986f 12390 if (elf_symtab_shndx_list (abfd))
8539e4e8 12391 {
ad32986f 12392 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12393
ad32986f
NC
12394 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12395 {
12396 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12397 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12398 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12399 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12400 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12401
ad32986f
NC
12402 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12403 off, TRUE);
12404
12405 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12406 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12407 return FALSE;
12408 }
8539e4e8 12409 }
ee3b52e9
L
12410
12411 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12412 /* sh_name was set in prep_headers. */
12413 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12414 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12415 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12416 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12417 symstrtab_hdr->sh_entsize = 0;
12418 symstrtab_hdr->sh_link = 0;
12419 symstrtab_hdr->sh_info = 0;
12420 /* sh_offset is set just below. */
12421 symstrtab_hdr->sh_addralign = 1;
12422
12423 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12424 off, TRUE);
12425 elf_next_file_pos (abfd) = off;
12426
c152c796 12427 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12428 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12429 return FALSE;
12430 }
12431
76359541
TP
12432 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12433 {
871b3ab2 12434 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12435 info->out_implib_bfd);
76359541
TP
12436 return FALSE;
12437 }
12438
c152c796
AM
12439 /* Adjust the relocs to have the correct symbol indices. */
12440 for (o = abfd->sections; o != NULL; o = o->next)
12441 {
d4730f92 12442 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12443 bfd_boolean sort;
10bbbc1d 12444
c152c796
AM
12445 if ((o->flags & SEC_RELOC) == 0)
12446 continue;
12447
28dbcedc 12448 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12449 if (esdo->rel.hdr != NULL
10bbbc1d 12450 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12451 return FALSE;
12452 if (esdo->rela.hdr != NULL
10bbbc1d 12453 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12454 return FALSE;
c152c796
AM
12455
12456 /* Set the reloc_count field to 0 to prevent write_relocs from
12457 trying to swap the relocs out itself. */
12458 o->reloc_count = 0;
12459 }
12460
12461 if (dynamic && info->combreloc && dynobj != NULL)
12462 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12463
12464 /* If we are linking against a dynamic object, or generating a
12465 shared library, finish up the dynamic linking information. */
12466 if (dynamic)
12467 {
12468 bfd_byte *dyncon, *dynconend;
12469
12470 /* Fix up .dynamic entries. */
3d4d4302 12471 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12472 BFD_ASSERT (o != NULL);
12473
12474 dyncon = o->contents;
eea6121a 12475 dynconend = o->contents + o->size;
c152c796
AM
12476 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12477 {
12478 Elf_Internal_Dyn dyn;
12479 const char *name;
12480 unsigned int type;
64487780
AM
12481 bfd_size_type sh_size;
12482 bfd_vma sh_addr;
c152c796
AM
12483
12484 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12485
12486 switch (dyn.d_tag)
12487 {
12488 default:
12489 continue;
12490 case DT_NULL:
12491 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12492 {
12493 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12494 {
12495 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12496 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12497 default: continue;
12498 }
12499 dyn.d_un.d_val = relativecount;
12500 relativecount = 0;
12501 break;
12502 }
12503 continue;
12504
12505 case DT_INIT:
12506 name = info->init_function;
12507 goto get_sym;
12508 case DT_FINI:
12509 name = info->fini_function;
12510 get_sym:
12511 {
12512 struct elf_link_hash_entry *h;
12513
64f52338 12514 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12515 if (h != NULL
12516 && (h->root.type == bfd_link_hash_defined
12517 || h->root.type == bfd_link_hash_defweak))
12518 {
bef26483 12519 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12520 o = h->root.u.def.section;
12521 if (o->output_section != NULL)
bef26483 12522 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12523 + o->output_offset);
12524 else
12525 {
12526 /* The symbol is imported from another shared
12527 library and does not apply to this one. */
bef26483 12528 dyn.d_un.d_ptr = 0;
c152c796
AM
12529 }
12530 break;
12531 }
12532 }
12533 continue;
12534
12535 case DT_PREINIT_ARRAYSZ:
12536 name = ".preinit_array";
4ade44b7 12537 goto get_out_size;
c152c796
AM
12538 case DT_INIT_ARRAYSZ:
12539 name = ".init_array";
4ade44b7 12540 goto get_out_size;
c152c796
AM
12541 case DT_FINI_ARRAYSZ:
12542 name = ".fini_array";
4ade44b7 12543 get_out_size:
c152c796
AM
12544 o = bfd_get_section_by_name (abfd, name);
12545 if (o == NULL)
12546 {
4eca0228 12547 _bfd_error_handler
4ade44b7 12548 (_("could not find section %s"), name);
c152c796
AM
12549 goto error_return;
12550 }
eea6121a 12551 if (o->size == 0)
4eca0228 12552 _bfd_error_handler
c152c796 12553 (_("warning: %s section has zero size"), name);
eea6121a 12554 dyn.d_un.d_val = o->size;
c152c796
AM
12555 break;
12556
12557 case DT_PREINIT_ARRAY:
12558 name = ".preinit_array";
4ade44b7 12559 goto get_out_vma;
c152c796
AM
12560 case DT_INIT_ARRAY:
12561 name = ".init_array";
4ade44b7 12562 goto get_out_vma;
c152c796
AM
12563 case DT_FINI_ARRAY:
12564 name = ".fini_array";
4ade44b7
AM
12565 get_out_vma:
12566 o = bfd_get_section_by_name (abfd, name);
12567 goto do_vma;
c152c796
AM
12568
12569 case DT_HASH:
12570 name = ".hash";
12571 goto get_vma;
fdc90cb4
JJ
12572 case DT_GNU_HASH:
12573 name = ".gnu.hash";
12574 goto get_vma;
c152c796
AM
12575 case DT_STRTAB:
12576 name = ".dynstr";
12577 goto get_vma;
12578 case DT_SYMTAB:
12579 name = ".dynsym";
12580 goto get_vma;
12581 case DT_VERDEF:
12582 name = ".gnu.version_d";
12583 goto get_vma;
12584 case DT_VERNEED:
12585 name = ".gnu.version_r";
12586 goto get_vma;
12587 case DT_VERSYM:
12588 name = ".gnu.version";
12589 get_vma:
4ade44b7
AM
12590 o = bfd_get_linker_section (dynobj, name);
12591 do_vma:
b3293efa 12592 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12593 {
4eca0228 12594 _bfd_error_handler
4ade44b7 12595 (_("could not find section %s"), name);
c152c796
AM
12596 goto error_return;
12597 }
894891db
NC
12598 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12599 {
4eca0228 12600 _bfd_error_handler
894891db
NC
12601 (_("warning: section '%s' is being made into a note"), name);
12602 bfd_set_error (bfd_error_nonrepresentable_section);
12603 goto error_return;
12604 }
4ade44b7 12605 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12606 break;
12607
12608 case DT_REL:
12609 case DT_RELA:
12610 case DT_RELSZ:
12611 case DT_RELASZ:
12612 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12613 type = SHT_REL;
12614 else
12615 type = SHT_RELA;
64487780
AM
12616 sh_size = 0;
12617 sh_addr = 0;
c152c796
AM
12618 for (i = 1; i < elf_numsections (abfd); i++)
12619 {
12620 Elf_Internal_Shdr *hdr;
12621
12622 hdr = elf_elfsections (abfd)[i];
12623 if (hdr->sh_type == type
12624 && (hdr->sh_flags & SHF_ALLOC) != 0)
12625 {
64487780
AM
12626 sh_size += hdr->sh_size;
12627 if (sh_addr == 0
12628 || sh_addr > hdr->sh_addr)
12629 sh_addr = hdr->sh_addr;
c152c796
AM
12630 }
12631 }
64487780 12632
64f52338
AM
12633 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12634 {
12635 /* Don't count procedure linkage table relocs in the
12636 overall reloc count. */
64487780
AM
12637 sh_size -= htab->srelplt->size;
12638 if (sh_size == 0)
12639 /* If the size is zero, make the address zero too.
12640 This is to avoid a glibc bug. If the backend
12641 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12642 zero, then we'll put DT_RELA at the end of
12643 DT_JMPREL. glibc will interpret the end of
12644 DT_RELA matching the end of DT_JMPREL as the
12645 case where DT_RELA includes DT_JMPREL, and for
12646 LD_BIND_NOW will decide that processing DT_RELA
12647 will process the PLT relocs too. Net result:
12648 No PLT relocs applied. */
12649 sh_addr = 0;
12650
64f52338
AM
12651 /* If .rela.plt is the first .rela section, exclude
12652 it from DT_RELA. */
64487780
AM
12653 else if (sh_addr == (htab->srelplt->output_section->vma
12654 + htab->srelplt->output_offset))
12655 sh_addr += htab->srelplt->size;
64f52338 12656 }
64487780
AM
12657
12658 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12659 dyn.d_un.d_val = sh_size;
12660 else
12661 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12662 break;
12663 }
12664 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12665 }
12666 }
12667
12668 /* If we have created any dynamic sections, then output them. */
12669 if (dynobj != NULL)
12670 {
12671 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12672 goto error_return;
12673
943284cc 12674 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12675 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12676 || info->error_textrel)
3d4d4302 12677 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12678 {
12679 bfd_byte *dyncon, *dynconend;
12680
943284cc
DJ
12681 dyncon = o->contents;
12682 dynconend = o->contents + o->size;
12683 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12684 {
12685 Elf_Internal_Dyn dyn;
12686
12687 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12688
12689 if (dyn.d_tag == DT_TEXTREL)
12690 {
c192a133
AM
12691 if (info->error_textrel)
12692 info->callbacks->einfo
9793eb77 12693 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12694 else
12695 info->callbacks->einfo
9793eb77 12696 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12697 break;
12698 }
12699 }
12700 }
12701
c152c796
AM
12702 for (o = dynobj->sections; o != NULL; o = o->next)
12703 {
12704 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12705 || o->size == 0
c152c796
AM
12706 || o->output_section == bfd_abs_section_ptr)
12707 continue;
12708 if ((o->flags & SEC_LINKER_CREATED) == 0)
12709 {
12710 /* At this point, we are only interested in sections
12711 created by _bfd_elf_link_create_dynamic_sections. */
12712 continue;
12713 }
64f52338 12714 if (htab->stab_info.stabstr == o)
3722b82f 12715 continue;
64f52338 12716 if (htab->eh_info.hdr_sec == o)
eea6121a 12717 continue;
3d4d4302 12718 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12719 {
12720 if (! bfd_set_section_contents (abfd, o->output_section,
12721 o->contents,
37b01f6a
DG
12722 (file_ptr) o->output_offset
12723 * bfd_octets_per_byte (abfd),
eea6121a 12724 o->size))
c152c796
AM
12725 goto error_return;
12726 }
12727 else
12728 {
12729 /* The contents of the .dynstr section are actually in a
12730 stringtab. */
8539e4e8
AM
12731 file_ptr off;
12732
c152c796
AM
12733 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12734 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12735 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12736 goto error_return;
12737 }
12738 }
12739 }
12740
7bdf4127 12741 if (!info->resolve_section_groups)
c152c796
AM
12742 {
12743 bfd_boolean failed = FALSE;
12744
7bdf4127 12745 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12746 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12747 if (failed)
12748 goto error_return;
12749 }
12750
12751 /* If we have optimized stabs strings, output them. */
64f52338 12752 if (htab->stab_info.stabstr != NULL)
c152c796 12753 {
64f52338 12754 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12755 goto error_return;
12756 }
12757
9f7c3e5e
AM
12758 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12759 goto error_return;
c152c796 12760
9f7c3e5e 12761 elf_final_link_free (abfd, &flinfo);
c152c796 12762
12bd6957 12763 elf_linker (abfd) = TRUE;
c152c796 12764
104d59d1
JM
12765 if (attr_section)
12766 {
a50b1753 12767 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12768 if (contents == NULL)
d0f16d5e 12769 return FALSE; /* Bail out and fail. */
104d59d1
JM
12770 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12771 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12772 free (contents);
12773 }
12774
c152c796
AM
12775 return TRUE;
12776
12777 error_return:
9f7c3e5e 12778 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12779 return FALSE;
12780}
12781\f
5241d853
RS
12782/* Initialize COOKIE for input bfd ABFD. */
12783
12784static bfd_boolean
12785init_reloc_cookie (struct elf_reloc_cookie *cookie,
12786 struct bfd_link_info *info, bfd *abfd)
12787{
12788 Elf_Internal_Shdr *symtab_hdr;
12789 const struct elf_backend_data *bed;
12790
12791 bed = get_elf_backend_data (abfd);
12792 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12793
12794 cookie->abfd = abfd;
12795 cookie->sym_hashes = elf_sym_hashes (abfd);
12796 cookie->bad_symtab = elf_bad_symtab (abfd);
12797 if (cookie->bad_symtab)
12798 {
12799 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12800 cookie->extsymoff = 0;
12801 }
12802 else
12803 {
12804 cookie->locsymcount = symtab_hdr->sh_info;
12805 cookie->extsymoff = symtab_hdr->sh_info;
12806 }
12807
12808 if (bed->s->arch_size == 32)
12809 cookie->r_sym_shift = 8;
12810 else
12811 cookie->r_sym_shift = 32;
12812
12813 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12814 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12815 {
12816 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12817 cookie->locsymcount, 0,
12818 NULL, NULL, NULL);
12819 if (cookie->locsyms == NULL)
12820 {
12821 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12822 return FALSE;
12823 }
12824 if (info->keep_memory)
12825 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12826 }
12827 return TRUE;
12828}
12829
12830/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12831
12832static void
12833fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12834{
12835 Elf_Internal_Shdr *symtab_hdr;
12836
12837 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12838 if (cookie->locsyms != NULL
12839 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12840 free (cookie->locsyms);
12841}
12842
12843/* Initialize the relocation information in COOKIE for input section SEC
12844 of input bfd ABFD. */
12845
12846static bfd_boolean
12847init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12848 struct bfd_link_info *info, bfd *abfd,
12849 asection *sec)
12850{
5241d853
RS
12851 if (sec->reloc_count == 0)
12852 {
12853 cookie->rels = NULL;
12854 cookie->relend = NULL;
12855 }
12856 else
12857 {
5241d853
RS
12858 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12859 info->keep_memory);
12860 if (cookie->rels == NULL)
12861 return FALSE;
12862 cookie->rel = cookie->rels;
056bafd4 12863 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12864 }
12865 cookie->rel = cookie->rels;
12866 return TRUE;
12867}
12868
12869/* Free the memory allocated by init_reloc_cookie_rels,
12870 if appropriate. */
12871
12872static void
12873fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12874 asection *sec)
12875{
12876 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12877 free (cookie->rels);
12878}
12879
12880/* Initialize the whole of COOKIE for input section SEC. */
12881
12882static bfd_boolean
12883init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12884 struct bfd_link_info *info,
12885 asection *sec)
12886{
12887 if (!init_reloc_cookie (cookie, info, sec->owner))
12888 goto error1;
12889 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12890 goto error2;
12891 return TRUE;
12892
12893 error2:
12894 fini_reloc_cookie (cookie, sec->owner);
12895 error1:
12896 return FALSE;
12897}
12898
12899/* Free the memory allocated by init_reloc_cookie_for_section,
12900 if appropriate. */
12901
12902static void
12903fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12904 asection *sec)
12905{
12906 fini_reloc_cookie_rels (cookie, sec);
12907 fini_reloc_cookie (cookie, sec->owner);
12908}
12909\f
c152c796
AM
12910/* Garbage collect unused sections. */
12911
07adf181
AM
12912/* Default gc_mark_hook. */
12913
12914asection *
12915_bfd_elf_gc_mark_hook (asection *sec,
12916 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12917 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12918 struct elf_link_hash_entry *h,
12919 Elf_Internal_Sym *sym)
12920{
12921 if (h != NULL)
12922 {
12923 switch (h->root.type)
12924 {
12925 case bfd_link_hash_defined:
12926 case bfd_link_hash_defweak:
12927 return h->root.u.def.section;
12928
12929 case bfd_link_hash_common:
12930 return h->root.u.c.p->section;
12931
12932 default:
12933 break;
12934 }
12935 }
12936 else
12937 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12938
12939 return NULL;
12940}
12941
9e223787 12942/* Return the debug definition section. */
b7c871ed
L
12943
12944static asection *
12945elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12946 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12947 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12948 struct elf_link_hash_entry *h,
9e223787 12949 Elf_Internal_Sym *sym)
b7c871ed 12950{
9e223787
L
12951 if (h != NULL)
12952 {
12953 /* Return the global debug definition section. */
12954 if ((h->root.type == bfd_link_hash_defined
12955 || h->root.type == bfd_link_hash_defweak)
12956 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12957 return h->root.u.def.section;
12958 }
12959 else
12960 {
12961 /* Return the local debug definition section. */
12962 asection *isec = bfd_section_from_elf_index (sec->owner,
12963 sym->st_shndx);
12964 if ((isec->flags & SEC_DEBUGGING) != 0)
12965 return isec;
12966 }
b7c871ed
L
12967
12968 return NULL;
12969}
12970
5241d853
RS
12971/* COOKIE->rel describes a relocation against section SEC, which is
12972 a section we've decided to keep. Return the section that contains
12973 the relocation symbol, or NULL if no section contains it. */
12974
12975asection *
12976_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12977 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12978 struct elf_reloc_cookie *cookie,
12979 bfd_boolean *start_stop)
5241d853
RS
12980{
12981 unsigned long r_symndx;
12982 struct elf_link_hash_entry *h;
12983
12984 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12985 if (r_symndx == STN_UNDEF)
5241d853
RS
12986 return NULL;
12987
12988 if (r_symndx >= cookie->locsymcount
12989 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12990 {
12991 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12992 if (h == NULL)
12993 {
871b3ab2 12994 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
12995 sec->owner);
12996 return NULL;
12997 }
5241d853
RS
12998 while (h->root.type == bfd_link_hash_indirect
12999 || h->root.type == bfd_link_hash_warning)
13000 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13001 h->mark = 1;
4e6b54a6
AM
13002 /* If this symbol is weak and there is a non-weak definition, we
13003 keep the non-weak definition because many backends put
13004 dynamic reloc info on the non-weak definition for code
13005 handling copy relocs. */
60d67dc8
AM
13006 if (h->is_weakalias)
13007 weakdef (h)->mark = 1;
1cce69b9 13008
a6a4679f 13009 if (start_stop != NULL)
1cce69b9 13010 {
7dba9362
AM
13011 /* To work around a glibc bug, mark XXX input sections
13012 when there is a reference to __start_XXX or __stop_XXX
13013 symbols. */
cbd0eecf 13014 if (h->start_stop)
1cce69b9 13015 {
cbd0eecf 13016 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13017 *start_stop = !s->gc_mark;
13018 return s;
1cce69b9
AM
13019 }
13020 }
13021
5241d853
RS
13022 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13023 }
13024
13025 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13026 &cookie->locsyms[r_symndx]);
13027}
13028
13029/* COOKIE->rel describes a relocation against section SEC, which is
13030 a section we've decided to keep. Mark the section that contains
9d0a14d3 13031 the relocation symbol. */
5241d853
RS
13032
13033bfd_boolean
13034_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13035 asection *sec,
13036 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13037 struct elf_reloc_cookie *cookie)
5241d853
RS
13038{
13039 asection *rsec;
1cce69b9 13040 bfd_boolean start_stop = FALSE;
5241d853 13041
1cce69b9
AM
13042 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13043 while (rsec != NULL)
5241d853 13044 {
1cce69b9
AM
13045 if (!rsec->gc_mark)
13046 {
13047 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13048 || (rsec->owner->flags & DYNAMIC) != 0)
13049 rsec->gc_mark = 1;
13050 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13051 return FALSE;
13052 }
13053 if (!start_stop)
13054 break;
199af150 13055 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13056 }
13057 return TRUE;
13058}
13059
07adf181
AM
13060/* The mark phase of garbage collection. For a given section, mark
13061 it and any sections in this section's group, and all the sections
13062 which define symbols to which it refers. */
13063
ccfa59ea
AM
13064bfd_boolean
13065_bfd_elf_gc_mark (struct bfd_link_info *info,
13066 asection *sec,
6a5bb875 13067 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13068{
13069 bfd_boolean ret;
9d0a14d3 13070 asection *group_sec, *eh_frame;
c152c796
AM
13071
13072 sec->gc_mark = 1;
13073
13074 /* Mark all the sections in the group. */
13075 group_sec = elf_section_data (sec)->next_in_group;
13076 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13077 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13078 return FALSE;
13079
13080 /* Look through the section relocs. */
13081 ret = TRUE;
9d0a14d3
RS
13082 eh_frame = elf_eh_frame_section (sec->owner);
13083 if ((sec->flags & SEC_RELOC) != 0
13084 && sec->reloc_count > 0
13085 && sec != eh_frame)
c152c796 13086 {
5241d853 13087 struct elf_reloc_cookie cookie;
c152c796 13088
5241d853
RS
13089 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13090 ret = FALSE;
c152c796 13091 else
c152c796 13092 {
5241d853 13093 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13094 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13095 {
13096 ret = FALSE;
13097 break;
13098 }
13099 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13100 }
13101 }
9d0a14d3
RS
13102
13103 if (ret && eh_frame && elf_fde_list (sec))
13104 {
13105 struct elf_reloc_cookie cookie;
13106
13107 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13108 ret = FALSE;
13109 else
13110 {
13111 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13112 gc_mark_hook, &cookie))
13113 ret = FALSE;
13114 fini_reloc_cookie_for_section (&cookie, eh_frame);
13115 }
13116 }
13117
2f0c68f2
CM
13118 eh_frame = elf_section_eh_frame_entry (sec);
13119 if (ret && eh_frame && !eh_frame->gc_mark)
13120 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13121 ret = FALSE;
13122
c152c796
AM
13123 return ret;
13124}
13125
3c758495
TG
13126/* Scan and mark sections in a special or debug section group. */
13127
13128static void
13129_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13130{
13131 /* Point to first section of section group. */
13132 asection *ssec;
13133 /* Used to iterate the section group. */
13134 asection *msec;
13135
13136 bfd_boolean is_special_grp = TRUE;
13137 bfd_boolean is_debug_grp = TRUE;
13138
13139 /* First scan to see if group contains any section other than debug
13140 and special section. */
13141 ssec = msec = elf_next_in_group (grp);
13142 do
13143 {
13144 if ((msec->flags & SEC_DEBUGGING) == 0)
13145 is_debug_grp = FALSE;
13146
13147 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13148 is_special_grp = FALSE;
13149
13150 msec = elf_next_in_group (msec);
13151 }
13152 while (msec != ssec);
13153
13154 /* If this is a pure debug section group or pure special section group,
13155 keep all sections in this group. */
13156 if (is_debug_grp || is_special_grp)
13157 {
13158 do
13159 {
13160 msec->gc_mark = 1;
13161 msec = elf_next_in_group (msec);
13162 }
13163 while (msec != ssec);
13164 }
13165}
13166
7f6ab9f8
AM
13167/* Keep debug and special sections. */
13168
13169bfd_boolean
13170_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13171 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13172{
13173 bfd *ibfd;
13174
c72f2fb2 13175 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13176 {
13177 asection *isec;
13178 bfd_boolean some_kept;
b40bf0a2 13179 bfd_boolean debug_frag_seen;
b7c871ed 13180 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13181
13182 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13183 continue;
57963c05
AM
13184 isec = ibfd->sections;
13185 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13186 continue;
7f6ab9f8 13187
b40bf0a2
NC
13188 /* Ensure all linker created sections are kept,
13189 see if any other section is already marked,
13190 and note if we have any fragmented debug sections. */
b7c871ed 13191 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13192 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13193 {
13194 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13195 isec->gc_mark = 1;
eb026f09
AM
13196 else if (isec->gc_mark
13197 && (isec->flags & SEC_ALLOC) != 0
13198 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13199 some_kept = TRUE;
b40bf0a2 13200
535b785f 13201 if (!debug_frag_seen
b40bf0a2
NC
13202 && (isec->flags & SEC_DEBUGGING)
13203 && CONST_STRNEQ (isec->name, ".debug_line."))
13204 debug_frag_seen = TRUE;
7f6ab9f8
AM
13205 }
13206
eb026f09
AM
13207 /* If no non-note alloc section in this file will be kept, then
13208 we can toss out the debug and special sections. */
7f6ab9f8
AM
13209 if (!some_kept)
13210 continue;
13211
13212 /* Keep debug and special sections like .comment when they are
3c758495
TG
13213 not part of a group. Also keep section groups that contain
13214 just debug sections or special sections. */
7f6ab9f8 13215 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13216 {
13217 if ((isec->flags & SEC_GROUP) != 0)
13218 _bfd_elf_gc_mark_debug_special_section_group (isec);
13219 else if (((isec->flags & SEC_DEBUGGING) != 0
13220 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13221 && elf_next_in_group (isec) == NULL)
13222 isec->gc_mark = 1;
b7c871ed
L
13223 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13224 has_kept_debug_info = TRUE;
3c758495 13225 }
b40bf0a2 13226
b40bf0a2
NC
13227 /* Look for CODE sections which are going to be discarded,
13228 and find and discard any fragmented debug sections which
13229 are associated with that code section. */
b7c871ed
L
13230 if (debug_frag_seen)
13231 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13232 if ((isec->flags & SEC_CODE) != 0
13233 && isec->gc_mark == 0)
13234 {
13235 unsigned int ilen;
13236 asection *dsec;
b40bf0a2 13237
b7c871ed 13238 ilen = strlen (isec->name);
b40bf0a2 13239
b7c871ed 13240 /* Association is determined by the name of the debug
07d6d2b8 13241 section containing the name of the code section as
b7c871ed
L
13242 a suffix. For example .debug_line.text.foo is a
13243 debug section associated with .text.foo. */
13244 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13245 {
13246 unsigned int dlen;
b40bf0a2 13247
b7c871ed
L
13248 if (dsec->gc_mark == 0
13249 || (dsec->flags & SEC_DEBUGGING) == 0)
13250 continue;
b40bf0a2 13251
b7c871ed 13252 dlen = strlen (dsec->name);
b40bf0a2 13253
b7c871ed
L
13254 if (dlen > ilen
13255 && strncmp (dsec->name + (dlen - ilen),
13256 isec->name, ilen) == 0)
b40bf0a2 13257 dsec->gc_mark = 0;
b7c871ed 13258 }
b40bf0a2 13259 }
b7c871ed
L
13260
13261 /* Mark debug sections referenced by kept debug sections. */
13262 if (has_kept_debug_info)
13263 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13264 if (isec->gc_mark
13265 && (isec->flags & SEC_DEBUGGING) != 0)
13266 if (!_bfd_elf_gc_mark (info, isec,
13267 elf_gc_mark_debug_section))
13268 return FALSE;
7f6ab9f8
AM
13269 }
13270 return TRUE;
13271}
13272
c152c796 13273static bfd_boolean
ccabcbe5 13274elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13275{
13276 bfd *sub;
ccabcbe5 13277 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13278
c72f2fb2 13279 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13280 {
13281 asection *o;
13282
b19a8f85 13283 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13284 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13285 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13286 continue;
57963c05
AM
13287 o = sub->sections;
13288 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13289 continue;
c152c796
AM
13290
13291 for (o = sub->sections; o != NULL; o = o->next)
13292 {
a33dafc3
L
13293 /* When any section in a section group is kept, we keep all
13294 sections in the section group. If the first member of
13295 the section group is excluded, we will also exclude the
13296 group section. */
13297 if (o->flags & SEC_GROUP)
13298 {
13299 asection *first = elf_next_in_group (o);
13300 o->gc_mark = first->gc_mark;
13301 }
c152c796 13302
1e7eae0d 13303 if (o->gc_mark)
c152c796
AM
13304 continue;
13305
13306 /* Skip sweeping sections already excluded. */
13307 if (o->flags & SEC_EXCLUDE)
13308 continue;
13309
13310 /* Since this is early in the link process, it is simple
13311 to remove a section from the output. */
13312 o->flags |= SEC_EXCLUDE;
13313
c55fe096 13314 if (info->print_gc_sections && o->size != 0)
695344c0 13315 /* xgettext:c-format */
9793eb77 13316 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13317 o, sub);
c152c796
AM
13318 }
13319 }
13320
c152c796
AM
13321 return TRUE;
13322}
13323
13324/* Propagate collected vtable information. This is called through
13325 elf_link_hash_traverse. */
13326
13327static bfd_boolean
13328elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13329{
c152c796 13330 /* Those that are not vtables. */
cbd0eecf
L
13331 if (h->start_stop
13332 || h->u2.vtable == NULL
13333 || h->u2.vtable->parent == NULL)
c152c796
AM
13334 return TRUE;
13335
13336 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13337 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13338 return TRUE;
13339
13340 /* If we've already been done, exit. */
cbd0eecf 13341 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13342 return TRUE;
13343
13344 /* Make sure the parent's table is up to date. */
cbd0eecf 13345 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13346
cbd0eecf 13347 if (h->u2.vtable->used == NULL)
c152c796
AM
13348 {
13349 /* None of this table's entries were referenced. Re-use the
13350 parent's table. */
cbd0eecf
L
13351 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13352 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13353 }
13354 else
13355 {
13356 size_t n;
13357 bfd_boolean *cu, *pu;
13358
13359 /* Or the parent's entries into ours. */
cbd0eecf 13360 cu = h->u2.vtable->used;
c152c796 13361 cu[-1] = TRUE;
cbd0eecf 13362 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13363 if (pu != NULL)
13364 {
13365 const struct elf_backend_data *bed;
13366 unsigned int log_file_align;
13367
13368 bed = get_elf_backend_data (h->root.u.def.section->owner);
13369 log_file_align = bed->s->log_file_align;
cbd0eecf 13370 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13371 while (n--)
13372 {
13373 if (*pu)
13374 *cu = TRUE;
13375 pu++;
13376 cu++;
13377 }
13378 }
13379 }
13380
13381 return TRUE;
13382}
13383
13384static bfd_boolean
13385elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13386{
13387 asection *sec;
13388 bfd_vma hstart, hend;
13389 Elf_Internal_Rela *relstart, *relend, *rel;
13390 const struct elf_backend_data *bed;
13391 unsigned int log_file_align;
13392
c152c796
AM
13393 /* Take care of both those symbols that do not describe vtables as
13394 well as those that are not loaded. */
cbd0eecf
L
13395 if (h->start_stop
13396 || h->u2.vtable == NULL
13397 || h->u2.vtable->parent == NULL)
c152c796
AM
13398 return TRUE;
13399
13400 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13401 || h->root.type == bfd_link_hash_defweak);
13402
13403 sec = h->root.u.def.section;
13404 hstart = h->root.u.def.value;
13405 hend = hstart + h->size;
13406
13407 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13408 if (!relstart)
13409 return *(bfd_boolean *) okp = FALSE;
13410 bed = get_elf_backend_data (sec->owner);
13411 log_file_align = bed->s->log_file_align;
13412
056bafd4 13413 relend = relstart + sec->reloc_count;
c152c796
AM
13414
13415 for (rel = relstart; rel < relend; ++rel)
13416 if (rel->r_offset >= hstart && rel->r_offset < hend)
13417 {
13418 /* If the entry is in use, do nothing. */
cbd0eecf
L
13419 if (h->u2.vtable->used
13420 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13421 {
13422 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13423 if (h->u2.vtable->used[entry])
c152c796
AM
13424 continue;
13425 }
13426 /* Otherwise, kill it. */
13427 rel->r_offset = rel->r_info = rel->r_addend = 0;
13428 }
13429
13430 return TRUE;
13431}
13432
87538722
AM
13433/* Mark sections containing dynamically referenced symbols. When
13434 building shared libraries, we must assume that any visible symbol is
13435 referenced. */
715df9b8 13436
64d03ab5
AM
13437bfd_boolean
13438bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13439{
87538722 13440 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13441 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13442
715df9b8
EB
13443 if ((h->root.type == bfd_link_hash_defined
13444 || h->root.type == bfd_link_hash_defweak)
d664fd41 13445 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13446 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13447 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13448 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13449 && (!bfd_link_executable (info)
22185505 13450 || info->gc_keep_exported
b407645f
AM
13451 || info->export_dynamic
13452 || (h->dynamic
13453 && d != NULL
13454 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13455 && (h->versioned >= versioned
54e8959c
L
13456 || !bfd_hide_sym_by_version (info->version_info,
13457 h->root.root.string)))))
715df9b8
EB
13458 h->root.u.def.section->flags |= SEC_KEEP;
13459
13460 return TRUE;
13461}
3b36f7e6 13462
74f0fb50
AM
13463/* Keep all sections containing symbols undefined on the command-line,
13464 and the section containing the entry symbol. */
13465
13466void
13467_bfd_elf_gc_keep (struct bfd_link_info *info)
13468{
13469 struct bfd_sym_chain *sym;
13470
13471 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13472 {
13473 struct elf_link_hash_entry *h;
13474
13475 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13476 FALSE, FALSE, FALSE);
13477
13478 if (h != NULL
13479 && (h->root.type == bfd_link_hash_defined
13480 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13481 && !bfd_is_abs_section (h->root.u.def.section)
13482 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13483 h->root.u.def.section->flags |= SEC_KEEP;
13484 }
13485}
13486
2f0c68f2
CM
13487bfd_boolean
13488bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13489 struct bfd_link_info *info)
13490{
13491 bfd *ibfd = info->input_bfds;
13492
13493 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13494 {
13495 asection *sec;
13496 struct elf_reloc_cookie cookie;
13497
13498 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13499 continue;
57963c05
AM
13500 sec = ibfd->sections;
13501 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13502 continue;
2f0c68f2
CM
13503
13504 if (!init_reloc_cookie (&cookie, info, ibfd))
13505 return FALSE;
13506
13507 for (sec = ibfd->sections; sec; sec = sec->next)
13508 {
13509 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13510 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13511 {
13512 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13513 fini_reloc_cookie_rels (&cookie, sec);
13514 }
13515 }
13516 }
13517 return TRUE;
13518}
13519
c152c796
AM
13520/* Do mark and sweep of unused sections. */
13521
13522bfd_boolean
13523bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13524{
13525 bfd_boolean ok = TRUE;
13526 bfd *sub;
6a5bb875 13527 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13528 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13529 struct elf_link_hash_table *htab;
c152c796 13530
64d03ab5 13531 if (!bed->can_gc_sections
715df9b8 13532 || !is_elf_hash_table (info->hash))
c152c796 13533 {
9793eb77 13534 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13535 return TRUE;
13536 }
13537
74f0fb50 13538 bed->gc_keep (info);
da44f4e5 13539 htab = elf_hash_table (info);
74f0fb50 13540
9d0a14d3
RS
13541 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13542 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13543 for (sub = info->input_bfds;
13544 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13545 sub = sub->link.next)
9d0a14d3
RS
13546 {
13547 asection *sec;
13548 struct elf_reloc_cookie cookie;
13549
57963c05
AM
13550 sec = sub->sections;
13551 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13552 continue;
9d0a14d3 13553 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13554 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13555 {
13556 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13557 if (elf_section_data (sec)->sec_info
13558 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13559 elf_eh_frame_section (sub) = sec;
13560 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13561 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13562 }
13563 }
9d0a14d3 13564
c152c796 13565 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13566 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13567 if (!ok)
13568 return FALSE;
13569
13570 /* Kill the vtable relocations that were not used. */
da44f4e5 13571 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13572 if (!ok)
13573 return FALSE;
13574
715df9b8 13575 /* Mark dynamically referenced symbols. */
22185505 13576 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13577 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13578
715df9b8 13579 /* Grovel through relocs to find out who stays ... */
64d03ab5 13580 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13581 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13582 {
13583 asection *o;
13584
b19a8f85 13585 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13586 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13587 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13588 continue;
13589
57963c05
AM
13590 o = sub->sections;
13591 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13592 continue;
13593
7f6ab9f8
AM
13594 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13595 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13596 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13597 well as FINI_ARRAY sections for ld -r. */
c152c796 13598 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13599 if (!o->gc_mark
13600 && (o->flags & SEC_EXCLUDE) == 0
24007750 13601 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13602 || (bfd_link_relocatable (info)
13603 && ((elf_section_data (o)->this_hdr.sh_type
13604 == SHT_PREINIT_ARRAY)
13605 || (elf_section_data (o)->this_hdr.sh_type
13606 == SHT_INIT_ARRAY)
13607 || (elf_section_data (o)->this_hdr.sh_type
13608 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13609 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13610 && elf_next_in_group (o) == NULL )))
13611 {
13612 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13613 return FALSE;
13614 }
c152c796
AM
13615 }
13616
6a5bb875 13617 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13618 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13619
c152c796 13620 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13621 return elf_gc_sweep (abfd, info);
c152c796
AM
13622}
13623\f
13624/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13625
13626bfd_boolean
13627bfd_elf_gc_record_vtinherit (bfd *abfd,
13628 asection *sec,
13629 struct elf_link_hash_entry *h,
13630 bfd_vma offset)
13631{
13632 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13633 struct elf_link_hash_entry **search, *child;
ef53be89 13634 size_t extsymcount;
c152c796
AM
13635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13636
13637 /* The sh_info field of the symtab header tells us where the
13638 external symbols start. We don't care about the local symbols at
13639 this point. */
13640 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13641 if (!elf_bad_symtab (abfd))
13642 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13643
13644 sym_hashes = elf_sym_hashes (abfd);
13645 sym_hashes_end = sym_hashes + extsymcount;
13646
13647 /* Hunt down the child symbol, which is in this section at the same
13648 offset as the relocation. */
13649 for (search = sym_hashes; search != sym_hashes_end; ++search)
13650 {
13651 if ((child = *search) != NULL
13652 && (child->root.type == bfd_link_hash_defined
13653 || child->root.type == bfd_link_hash_defweak)
13654 && child->root.u.def.section == sec
13655 && child->root.u.def.value == offset)
13656 goto win;
13657 }
13658
695344c0 13659 /* xgettext:c-format */
9793eb77 13660 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13661 abfd, sec, (uint64_t) offset);
c152c796
AM
13662 bfd_set_error (bfd_error_invalid_operation);
13663 return FALSE;
13664
13665 win:
cbd0eecf 13666 if (!child->u2.vtable)
f6e332e6 13667 {
cbd0eecf
L
13668 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13669 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13670 if (!child->u2.vtable)
f6e332e6
AM
13671 return FALSE;
13672 }
c152c796
AM
13673 if (!h)
13674 {
13675 /* This *should* only be the absolute section. It could potentially
13676 be that someone has defined a non-global vtable though, which
13677 would be bad. It isn't worth paging in the local symbols to be
13678 sure though; that case should simply be handled by the assembler. */
13679
cbd0eecf 13680 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13681 }
13682 else
cbd0eecf 13683 child->u2.vtable->parent = h;
c152c796
AM
13684
13685 return TRUE;
13686}
13687
13688/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13689
13690bfd_boolean
13691bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13692 asection *sec ATTRIBUTE_UNUSED,
13693 struct elf_link_hash_entry *h,
13694 bfd_vma addend)
13695{
13696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13697 unsigned int log_file_align = bed->s->log_file_align;
13698
cbd0eecf 13699 if (!h->u2.vtable)
f6e332e6 13700 {
cbd0eecf
L
13701 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13702 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13703 if (!h->u2.vtable)
f6e332e6
AM
13704 return FALSE;
13705 }
13706
cbd0eecf 13707 if (addend >= h->u2.vtable->size)
c152c796
AM
13708 {
13709 size_t size, bytes, file_align;
cbd0eecf 13710 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13711
13712 /* While the symbol is undefined, we have to be prepared to handle
13713 a zero size. */
13714 file_align = 1 << log_file_align;
13715 if (h->root.type == bfd_link_hash_undefined)
13716 size = addend + file_align;
13717 else
13718 {
13719 size = h->size;
13720 if (addend >= size)
13721 {
13722 /* Oops! We've got a reference past the defined end of
13723 the table. This is probably a bug -- shall we warn? */
13724 size = addend + file_align;
13725 }
13726 }
13727 size = (size + file_align - 1) & -file_align;
13728
13729 /* Allocate one extra entry for use as a "done" flag for the
13730 consolidation pass. */
13731 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13732
13733 if (ptr)
13734 {
a50b1753 13735 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13736
13737 if (ptr != NULL)
13738 {
13739 size_t oldbytes;
13740
cbd0eecf 13741 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13742 * sizeof (bfd_boolean));
13743 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13744 }
13745 }
13746 else
a50b1753 13747 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13748
13749 if (ptr == NULL)
13750 return FALSE;
13751
13752 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13753 h->u2.vtable->used = ptr + 1;
13754 h->u2.vtable->size = size;
c152c796
AM
13755 }
13756
cbd0eecf 13757 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13758
13759 return TRUE;
13760}
13761
ae17ab41
CM
13762/* Map an ELF section header flag to its corresponding string. */
13763typedef struct
13764{
13765 char *flag_name;
13766 flagword flag_value;
13767} elf_flags_to_name_table;
13768
13769static elf_flags_to_name_table elf_flags_to_names [] =
13770{
13771 { "SHF_WRITE", SHF_WRITE },
13772 { "SHF_ALLOC", SHF_ALLOC },
13773 { "SHF_EXECINSTR", SHF_EXECINSTR },
13774 { "SHF_MERGE", SHF_MERGE },
13775 { "SHF_STRINGS", SHF_STRINGS },
13776 { "SHF_INFO_LINK", SHF_INFO_LINK},
13777 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13778 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13779 { "SHF_GROUP", SHF_GROUP },
13780 { "SHF_TLS", SHF_TLS },
13781 { "SHF_MASKOS", SHF_MASKOS },
13782 { "SHF_EXCLUDE", SHF_EXCLUDE },
13783};
13784
b9c361e0
JL
13785/* Returns TRUE if the section is to be included, otherwise FALSE. */
13786bfd_boolean
ae17ab41 13787bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13788 struct flag_info *flaginfo,
b9c361e0 13789 asection *section)
ae17ab41 13790{
8b127cbc 13791 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13792
8b127cbc 13793 if (!flaginfo->flags_initialized)
ae17ab41 13794 {
8b127cbc
AM
13795 bfd *obfd = info->output_bfd;
13796 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13797 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13798 int with_hex = 0;
13799 int without_hex = 0;
13800
8b127cbc 13801 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13802 {
b9c361e0 13803 unsigned i;
8b127cbc 13804 flagword (*lookup) (char *);
ae17ab41 13805
8b127cbc
AM
13806 lookup = bed->elf_backend_lookup_section_flags_hook;
13807 if (lookup != NULL)
ae17ab41 13808 {
8b127cbc 13809 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13810
13811 if (hexval != 0)
13812 {
13813 if (tf->with == with_flags)
13814 with_hex |= hexval;
13815 else if (tf->with == without_flags)
13816 without_hex |= hexval;
13817 tf->valid = TRUE;
13818 continue;
13819 }
ae17ab41 13820 }
8b127cbc 13821 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13822 {
8b127cbc 13823 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13824 {
13825 if (tf->with == with_flags)
13826 with_hex |= elf_flags_to_names[i].flag_value;
13827 else if (tf->with == without_flags)
13828 without_hex |= elf_flags_to_names[i].flag_value;
13829 tf->valid = TRUE;
13830 break;
13831 }
13832 }
8b127cbc 13833 if (!tf->valid)
b9c361e0 13834 {
68ffbac6 13835 info->callbacks->einfo
9793eb77 13836 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13837 return FALSE;
ae17ab41
CM
13838 }
13839 }
8b127cbc
AM
13840 flaginfo->flags_initialized = TRUE;
13841 flaginfo->only_with_flags |= with_hex;
13842 flaginfo->not_with_flags |= without_hex;
ae17ab41 13843 }
ae17ab41 13844
8b127cbc 13845 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13846 return FALSE;
13847
8b127cbc 13848 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13849 return FALSE;
13850
13851 return TRUE;
ae17ab41
CM
13852}
13853
c152c796
AM
13854struct alloc_got_off_arg {
13855 bfd_vma gotoff;
10455f89 13856 struct bfd_link_info *info;
c152c796
AM
13857};
13858
13859/* We need a special top-level link routine to convert got reference counts
13860 to real got offsets. */
13861
13862static bfd_boolean
13863elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13864{
a50b1753 13865 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13866 bfd *obfd = gofarg->info->output_bfd;
13867 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13868
c152c796
AM
13869 if (h->got.refcount > 0)
13870 {
13871 h->got.offset = gofarg->gotoff;
10455f89 13872 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13873 }
13874 else
13875 h->got.offset = (bfd_vma) -1;
13876
13877 return TRUE;
13878}
13879
13880/* And an accompanying bit to work out final got entry offsets once
13881 we're done. Should be called from final_link. */
13882
13883bfd_boolean
13884bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13885 struct bfd_link_info *info)
13886{
13887 bfd *i;
13888 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13889 bfd_vma gotoff;
c152c796
AM
13890 struct alloc_got_off_arg gofarg;
13891
10455f89
HPN
13892 BFD_ASSERT (abfd == info->output_bfd);
13893
c152c796
AM
13894 if (! is_elf_hash_table (info->hash))
13895 return FALSE;
13896
13897 /* The GOT offset is relative to the .got section, but the GOT header is
13898 put into the .got.plt section, if the backend uses it. */
13899 if (bed->want_got_plt)
13900 gotoff = 0;
13901 else
13902 gotoff = bed->got_header_size;
13903
13904 /* Do the local .got entries first. */
c72f2fb2 13905 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13906 {
13907 bfd_signed_vma *local_got;
ef53be89 13908 size_t j, locsymcount;
c152c796
AM
13909 Elf_Internal_Shdr *symtab_hdr;
13910
13911 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13912 continue;
13913
13914 local_got = elf_local_got_refcounts (i);
13915 if (!local_got)
13916 continue;
13917
13918 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13919 if (elf_bad_symtab (i))
13920 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13921 else
13922 locsymcount = symtab_hdr->sh_info;
13923
13924 for (j = 0; j < locsymcount; ++j)
13925 {
13926 if (local_got[j] > 0)
13927 {
13928 local_got[j] = gotoff;
10455f89 13929 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13930 }
13931 else
13932 local_got[j] = (bfd_vma) -1;
13933 }
13934 }
13935
13936 /* Then the global .got entries. .plt refcounts are handled by
13937 adjust_dynamic_symbol */
13938 gofarg.gotoff = gotoff;
10455f89 13939 gofarg.info = info;
c152c796
AM
13940 elf_link_hash_traverse (elf_hash_table (info),
13941 elf_gc_allocate_got_offsets,
13942 &gofarg);
13943 return TRUE;
13944}
13945
13946/* Many folk need no more in the way of final link than this, once
13947 got entry reference counting is enabled. */
13948
13949bfd_boolean
13950bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13951{
13952 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13953 return FALSE;
13954
13955 /* Invoke the regular ELF backend linker to do all the work. */
13956 return bfd_elf_final_link (abfd, info);
13957}
13958
13959bfd_boolean
13960bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13961{
a50b1753 13962 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13963
13964 if (rcookie->bad_symtab)
13965 rcookie->rel = rcookie->rels;
13966
13967 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13968 {
13969 unsigned long r_symndx;
13970
13971 if (! rcookie->bad_symtab)
13972 if (rcookie->rel->r_offset > offset)
13973 return FALSE;
13974 if (rcookie->rel->r_offset != offset)
13975 continue;
13976
13977 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13978 if (r_symndx == STN_UNDEF)
c152c796
AM
13979 return TRUE;
13980
13981 if (r_symndx >= rcookie->locsymcount
13982 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13983 {
13984 struct elf_link_hash_entry *h;
13985
13986 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13987
13988 while (h->root.type == bfd_link_hash_indirect
13989 || h->root.type == bfd_link_hash_warning)
13990 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13991
13992 if ((h->root.type == bfd_link_hash_defined
13993 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13994 && (h->root.u.def.section->owner != rcookie->abfd
13995 || h->root.u.def.section->kept_section != NULL
13996 || discarded_section (h->root.u.def.section)))
c152c796 13997 return TRUE;
c152c796
AM
13998 }
13999 else
14000 {
14001 /* It's not a relocation against a global symbol,
14002 but it could be a relocation against a local
14003 symbol for a discarded section. */
14004 asection *isec;
14005 Elf_Internal_Sym *isym;
14006
14007 /* Need to: get the symbol; get the section. */
14008 isym = &rcookie->locsyms[r_symndx];
cb33740c 14009 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14010 if (isec != NULL
14011 && (isec->kept_section != NULL
14012 || discarded_section (isec)))
cb33740c 14013 return TRUE;
c152c796
AM
14014 }
14015 return FALSE;
14016 }
14017 return FALSE;
14018}
14019
14020/* Discard unneeded references to discarded sections.
75938853
AM
14021 Returns -1 on error, 1 if any section's size was changed, 0 if
14022 nothing changed. This function assumes that the relocations are in
14023 sorted order, which is true for all known assemblers. */
c152c796 14024
75938853 14025int
c152c796
AM
14026bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14027{
14028 struct elf_reloc_cookie cookie;
18cd5bce 14029 asection *o;
c152c796 14030 bfd *abfd;
75938853 14031 int changed = 0;
c152c796
AM
14032
14033 if (info->traditional_format
14034 || !is_elf_hash_table (info->hash))
75938853 14035 return 0;
c152c796 14036
18cd5bce
AM
14037 o = bfd_get_section_by_name (output_bfd, ".stab");
14038 if (o != NULL)
c152c796 14039 {
18cd5bce 14040 asection *i;
c152c796 14041
18cd5bce 14042 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14043 {
18cd5bce
AM
14044 if (i->size == 0
14045 || i->reloc_count == 0
14046 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14047 continue;
c152c796 14048
18cd5bce
AM
14049 abfd = i->owner;
14050 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14051 continue;
c152c796 14052
18cd5bce 14053 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14054 return -1;
c152c796 14055
18cd5bce
AM
14056 if (_bfd_discard_section_stabs (abfd, i,
14057 elf_section_data (i)->sec_info,
5241d853
RS
14058 bfd_elf_reloc_symbol_deleted_p,
14059 &cookie))
75938853 14060 changed = 1;
18cd5bce
AM
14061
14062 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14063 }
18cd5bce
AM
14064 }
14065
2f0c68f2
CM
14066 o = NULL;
14067 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14068 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14069 if (o != NULL)
14070 {
14071 asection *i;
d7153c4a 14072 int eh_changed = 0;
79a94a2a 14073 unsigned int eh_alignment;
c152c796 14074
18cd5bce 14075 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14076 {
18cd5bce
AM
14077 if (i->size == 0)
14078 continue;
14079
14080 abfd = i->owner;
14081 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14082 continue;
14083
14084 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14085 return -1;
18cd5bce
AM
14086
14087 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14088 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14089 bfd_elf_reloc_symbol_deleted_p,
14090 &cookie))
d7153c4a
AM
14091 {
14092 eh_changed = 1;
14093 if (i->size != i->rawsize)
14094 changed = 1;
14095 }
18cd5bce
AM
14096
14097 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14098 }
9866ffe2 14099
79a94a2a 14100 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14101 /* Skip over zero terminator, and prevent empty sections from
14102 adding alignment padding at the end. */
14103 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14104 if (i->size == 0)
14105 i->flags |= SEC_EXCLUDE;
14106 else if (i->size > 4)
14107 break;
14108 /* The last non-empty eh_frame section doesn't need padding. */
14109 if (i != NULL)
14110 i = i->map_tail.s;
14111 /* Any prior sections must pad the last FDE out to the output
14112 section alignment. Otherwise we might have zero padding
14113 between sections, which would be seen as a terminator. */
14114 for (; i != NULL; i = i->map_tail.s)
14115 if (i->size == 4)
14116 /* All but the last zero terminator should have been removed. */
14117 BFD_FAIL ();
14118 else
14119 {
14120 bfd_size_type size
14121 = (i->size + eh_alignment - 1) & -eh_alignment;
14122 if (i->size != size)
af471f82 14123 {
9866ffe2
AM
14124 i->size = size;
14125 changed = 1;
14126 eh_changed = 1;
af471f82 14127 }
9866ffe2 14128 }
d7153c4a
AM
14129 if (eh_changed)
14130 elf_link_hash_traverse (elf_hash_table (info),
14131 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14132 }
c152c796 14133
18cd5bce
AM
14134 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14135 {
14136 const struct elf_backend_data *bed;
57963c05 14137 asection *s;
c152c796 14138
18cd5bce
AM
14139 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14140 continue;
57963c05
AM
14141 s = abfd->sections;
14142 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14143 continue;
18cd5bce
AM
14144
14145 bed = get_elf_backend_data (abfd);
14146
14147 if (bed->elf_backend_discard_info != NULL)
14148 {
14149 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14150 return -1;
18cd5bce
AM
14151
14152 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14153 changed = 1;
18cd5bce
AM
14154
14155 fini_reloc_cookie (&cookie, abfd);
14156 }
c152c796
AM
14157 }
14158
2f0c68f2
CM
14159 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14160 _bfd_elf_end_eh_frame_parsing (info);
14161
14162 if (info->eh_frame_hdr_type
0e1862bb 14163 && !bfd_link_relocatable (info)
c152c796 14164 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14165 changed = 1;
c152c796 14166
75938853 14167 return changed;
c152c796 14168}
082b7297 14169
43e1669b 14170bfd_boolean
0c511000 14171_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14172 asection *sec,
c0f00686 14173 struct bfd_link_info *info)
082b7297
L
14174{
14175 flagword flags;
c77ec726 14176 const char *name, *key;
082b7297
L
14177 struct bfd_section_already_linked *l;
14178 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14179
c77ec726
AM
14180 if (sec->output_section == bfd_abs_section_ptr)
14181 return FALSE;
0c511000 14182
c77ec726 14183 flags = sec->flags;
0c511000 14184
c77ec726
AM
14185 /* Return if it isn't a linkonce section. A comdat group section
14186 also has SEC_LINK_ONCE set. */
14187 if ((flags & SEC_LINK_ONCE) == 0)
14188 return FALSE;
0c511000 14189
c77ec726
AM
14190 /* Don't put group member sections on our list of already linked
14191 sections. They are handled as a group via their group section. */
14192 if (elf_sec_group (sec) != NULL)
14193 return FALSE;
0c511000 14194
c77ec726
AM
14195 /* For a SHT_GROUP section, use the group signature as the key. */
14196 name = sec->name;
14197 if ((flags & SEC_GROUP) != 0
14198 && elf_next_in_group (sec) != NULL
14199 && elf_group_name (elf_next_in_group (sec)) != NULL)
14200 key = elf_group_name (elf_next_in_group (sec));
14201 else
14202 {
14203 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14204 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14205 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14206 key++;
0c511000 14207 else
c77ec726
AM
14208 /* Must be a user linkonce section that doesn't follow gcc's
14209 naming convention. In this case we won't be matching
14210 single member groups. */
14211 key = name;
0c511000 14212 }
6d2cd210 14213
c77ec726 14214 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14215
14216 for (l = already_linked_list->entry; l != NULL; l = l->next)
14217 {
c2370991 14218 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14219 sections with a signature of <key> (<key> is some string),
14220 and linkonce sections named .gnu.linkonce.<type>.<key>.
14221 Match like sections. LTO plugin sections are an exception.
14222 They are always named .gnu.linkonce.t.<key> and match either
14223 type of section. */
14224 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14225 && ((flags & SEC_GROUP) != 0
14226 || strcmp (name, l->sec->name) == 0))
14227 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14228 {
14229 /* The section has already been linked. See if we should
6d2cd210 14230 issue a warning. */
c77ec726
AM
14231 if (!_bfd_handle_already_linked (sec, l, info))
14232 return FALSE;
082b7297 14233
c77ec726 14234 if (flags & SEC_GROUP)
3d7f7666 14235 {
c77ec726
AM
14236 asection *first = elf_next_in_group (sec);
14237 asection *s = first;
3d7f7666 14238
c77ec726 14239 while (s != NULL)
3d7f7666 14240 {
c77ec726
AM
14241 s->output_section = bfd_abs_section_ptr;
14242 /* Record which group discards it. */
14243 s->kept_section = l->sec;
14244 s = elf_next_in_group (s);
14245 /* These lists are circular. */
14246 if (s == first)
14247 break;
3d7f7666
L
14248 }
14249 }
082b7297 14250
43e1669b 14251 return TRUE;
082b7297
L
14252 }
14253 }
14254
c77ec726
AM
14255 /* A single member comdat group section may be discarded by a
14256 linkonce section and vice versa. */
14257 if ((flags & SEC_GROUP) != 0)
3d7f7666 14258 {
c77ec726 14259 asection *first = elf_next_in_group (sec);
c2370991 14260
c77ec726
AM
14261 if (first != NULL && elf_next_in_group (first) == first)
14262 /* Check this single member group against linkonce sections. */
14263 for (l = already_linked_list->entry; l != NULL; l = l->next)
14264 if ((l->sec->flags & SEC_GROUP) == 0
14265 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14266 {
14267 first->output_section = bfd_abs_section_ptr;
14268 first->kept_section = l->sec;
14269 sec->output_section = bfd_abs_section_ptr;
14270 break;
14271 }
14272 }
14273 else
14274 /* Check this linkonce section against single member groups. */
14275 for (l = already_linked_list->entry; l != NULL; l = l->next)
14276 if (l->sec->flags & SEC_GROUP)
6d2cd210 14277 {
c77ec726 14278 asection *first = elf_next_in_group (l->sec);
6d2cd210 14279
c77ec726
AM
14280 if (first != NULL
14281 && elf_next_in_group (first) == first
14282 && bfd_elf_match_symbols_in_sections (first, sec, info))
14283 {
14284 sec->output_section = bfd_abs_section_ptr;
14285 sec->kept_section = first;
14286 break;
14287 }
6d2cd210 14288 }
0c511000 14289
c77ec726
AM
14290 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14291 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14292 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14293 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14294 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14295 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14296 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14297 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14298 The reverse order cannot happen as there is never a bfd with only the
14299 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14300 matter as here were are looking only for cross-bfd sections. */
14301
14302 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14303 for (l = already_linked_list->entry; l != NULL; l = l->next)
14304 if ((l->sec->flags & SEC_GROUP) == 0
14305 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14306 {
14307 if (abfd != l->sec->owner)
14308 sec->output_section = bfd_abs_section_ptr;
14309 break;
14310 }
80c29487 14311
082b7297 14312 /* This is the first section with this name. Record it. */
c77ec726 14313 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14314 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14315 return sec->output_section == bfd_abs_section_ptr;
082b7297 14316}
81e1b023 14317
a4d8e49b
L
14318bfd_boolean
14319_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14320{
14321 return sym->st_shndx == SHN_COMMON;
14322}
14323
14324unsigned int
14325_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14326{
14327 return SHN_COMMON;
14328}
14329
14330asection *
14331_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14332{
14333 return bfd_com_section_ptr;
14334}
10455f89
HPN
14335
14336bfd_vma
14337_bfd_elf_default_got_elt_size (bfd *abfd,
14338 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14339 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14340 bfd *ibfd ATTRIBUTE_UNUSED,
14341 unsigned long symndx ATTRIBUTE_UNUSED)
14342{
14343 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14344 return bed->s->arch_size / 8;
14345}
83bac4b0
NC
14346
14347/* Routines to support the creation of dynamic relocs. */
14348
83bac4b0
NC
14349/* Returns the name of the dynamic reloc section associated with SEC. */
14350
14351static const char *
14352get_dynamic_reloc_section_name (bfd * abfd,
14353 asection * sec,
14354 bfd_boolean is_rela)
14355{
ddcf1fcf
BS
14356 char *name;
14357 const char *old_name = bfd_get_section_name (NULL, sec);
14358 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14359
ddcf1fcf 14360 if (old_name == NULL)
83bac4b0
NC
14361 return NULL;
14362
ddcf1fcf 14363 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14364 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14365
14366 return name;
14367}
14368
14369/* Returns the dynamic reloc section associated with SEC.
14370 If necessary compute the name of the dynamic reloc section based
14371 on SEC's name (looked up in ABFD's string table) and the setting
14372 of IS_RELA. */
14373
14374asection *
14375_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14376 asection * sec,
14377 bfd_boolean is_rela)
14378{
14379 asection * reloc_sec = elf_section_data (sec)->sreloc;
14380
14381 if (reloc_sec == NULL)
14382 {
14383 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14384
14385 if (name != NULL)
14386 {
3d4d4302 14387 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14388
14389 if (reloc_sec != NULL)
14390 elf_section_data (sec)->sreloc = reloc_sec;
14391 }
14392 }
14393
14394 return reloc_sec;
14395}
14396
14397/* Returns the dynamic reloc section associated with SEC. If the
14398 section does not exist it is created and attached to the DYNOBJ
14399 bfd and stored in the SRELOC field of SEC's elf_section_data
14400 structure.
f8076f98 14401
83bac4b0
NC
14402 ALIGNMENT is the alignment for the newly created section and
14403 IS_RELA defines whether the name should be .rela.<SEC's name>
14404 or .rel.<SEC's name>. The section name is looked up in the
14405 string table associated with ABFD. */
14406
14407asection *
ca4be51c
AM
14408_bfd_elf_make_dynamic_reloc_section (asection *sec,
14409 bfd *dynobj,
14410 unsigned int alignment,
14411 bfd *abfd,
14412 bfd_boolean is_rela)
83bac4b0
NC
14413{
14414 asection * reloc_sec = elf_section_data (sec)->sreloc;
14415
14416 if (reloc_sec == NULL)
14417 {
14418 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14419
14420 if (name == NULL)
14421 return NULL;
14422
3d4d4302 14423 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14424
14425 if (reloc_sec == NULL)
14426 {
3d4d4302
AM
14427 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14428 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14429 if ((sec->flags & SEC_ALLOC) != 0)
14430 flags |= SEC_ALLOC | SEC_LOAD;
14431
3d4d4302 14432 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14433 if (reloc_sec != NULL)
14434 {
8877b5e5
AM
14435 /* _bfd_elf_get_sec_type_attr chooses a section type by
14436 name. Override as it may be wrong, eg. for a user
14437 section named "auto" we'll get ".relauto" which is
14438 seen to be a .rela section. */
14439 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14440 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14441 reloc_sec = NULL;
14442 }
14443 }
14444
14445 elf_section_data (sec)->sreloc = reloc_sec;
14446 }
14447
14448 return reloc_sec;
14449}
1338dd10 14450
bffebb6b
AM
14451/* Copy the ELF symbol type and other attributes for a linker script
14452 assignment from HSRC to HDEST. Generally this should be treated as
14453 if we found a strong non-dynamic definition for HDEST (except that
14454 ld ignores multiple definition errors). */
1338dd10 14455void
bffebb6b
AM
14456_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14457 struct bfd_link_hash_entry *hdest,
14458 struct bfd_link_hash_entry *hsrc)
1338dd10 14459{
bffebb6b
AM
14460 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14461 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14462 Elf_Internal_Sym isym;
1338dd10
PB
14463
14464 ehdest->type = ehsrc->type;
35fc36a8 14465 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14466
14467 isym.st_other = ehsrc->other;
b8417128 14468 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14469}
351f65ca
L
14470
14471/* Append a RELA relocation REL to section S in BFD. */
14472
14473void
14474elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14475{
14476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14477 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14478 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14479 bed->s->swap_reloca_out (abfd, rel, loc);
14480}
14481
14482/* Append a REL relocation REL to section S in BFD. */
14483
14484void
14485elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14486{
14487 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14488 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14489 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14490 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14491}
7dba9362
AM
14492
14493/* Define __start, __stop, .startof. or .sizeof. symbol. */
14494
14495struct bfd_link_hash_entry *
14496bfd_elf_define_start_stop (struct bfd_link_info *info,
14497 const char *symbol, asection *sec)
14498{
487b6440 14499 struct elf_link_hash_entry *h;
7dba9362 14500
487b6440
AM
14501 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14502 FALSE, FALSE, TRUE);
14503 if (h != NULL
14504 && (h->root.type == bfd_link_hash_undefined
14505 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14506 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14507 {
bf3077a6 14508 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14509 h->root.type = bfd_link_hash_defined;
14510 h->root.u.def.section = sec;
14511 h->root.u.def.value = 0;
14512 h->def_regular = 1;
14513 h->def_dynamic = 0;
14514 h->start_stop = 1;
14515 h->u2.start_stop_section = sec;
14516 if (symbol[0] == '.')
14517 {
14518 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14519 const struct elf_backend_data *bed;
14520 bed = get_elf_backend_data (info->output_bfd);
14521 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14522 }
36b8fda5
AM
14523 else
14524 {
14525 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14526 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14527 if (was_dynamic)
36b8fda5
AM
14528 bfd_elf_link_record_dynamic_symbol (info, h);
14529 }
487b6440 14530 return &h->root;
7dba9362 14531 }
487b6440 14532 return NULL;
7dba9362 14533}
This page took 3.016876 seconds and 4 git commands to generate.