ELF dynsyms
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
219d1afa 2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
07d6d2b8 80 return h->root.u.def.section;
2f0c68f2
CM
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
57963c05 221 asection *s;
6cd255ca 222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 223 if ((ibfd->flags
57963c05
AM
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
55255dae
L
595}
596
45d6a902
AM
597/* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600bfd_boolean
fe21a8fc
L
601bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
268b6b39 603 const char *name,
fe21a8fc
L
604 bfd_boolean provide,
605 bfd_boolean hidden)
45d6a902 606{
00cbee0a 607 struct elf_link_hash_entry *h, *hv;
4ea42fb7 608 struct elf_link_hash_table *htab;
00cbee0a 609 const struct elf_backend_data *bed;
45d6a902 610
0eddce27 611 if (!is_elf_hash_table (info->hash))
45d6a902
AM
612 return TRUE;
613
4ea42fb7
AM
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 616 if (h == NULL)
4ea42fb7 617 return provide;
45d6a902 618
8e2a4f11
AM
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
0f550b3d
L
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
73ec947d
AM
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
00cbee0a 643 switch (h->root.type)
77cfaee6 644 {
00cbee0a
L
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
4ea42fb7 654 h->root.type = bfd_link_hash_new;
77cfaee6
AM
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
657 break;
658 case bfd_link_hash_new:
00cbee0a
L
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 662 the versioned symbol point to this one. */
00cbee0a
L
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
8e2a4f11
AM
675 default:
676 BFD_FAIL ();
c2596ca5 677 return FALSE;
55255dae 678 }
45d6a902
AM
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
45d6a902
AM
687 h->root.type = bfd_link_hash_undefined;
688
689 /* If this symbol is not being provided by the linker script, and it is
690 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
691 then clear out any version information because the symbol will not be
692 associated with the dynamic object any more. */
45d6a902 693 if (!provide
f5385ebf
AM
694 && h->def_dynamic
695 && !h->def_regular)
b531344c
MR
696 h->verinfo.verdef = NULL;
697
698 /* Make sure this symbol is not garbage collected. */
699 h->mark = 1;
45d6a902 700
f5385ebf 701 h->def_regular = 1;
45d6a902 702
eb8476a6 703 if (hidden)
fe21a8fc 704 {
91d6fa6a 705 bed = get_elf_backend_data (output_bfd);
b8297068
AM
706 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
707 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
708 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
709 }
710
6fa3860b
PB
711 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
712 and executables. */
0e1862bb 713 if (!bfd_link_relocatable (info)
6fa3860b
PB
714 && h->dynindx != -1
715 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
716 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
717 h->forced_local = 1;
718
f5385ebf
AM
719 if ((h->def_dynamic
720 || h->ref_dynamic
6b3b0ab8
L
721 || bfd_link_dll (info)
722 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 723 && !h->forced_local
45d6a902
AM
724 && h->dynindx == -1)
725 {
c152c796 726 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
727 return FALSE;
728
729 /* If this is a weak defined symbol, and we know a corresponding
730 real symbol from the same dynamic object, make sure the real
731 symbol is also made into a dynamic symbol. */
60d67dc8 732 if (h->is_weakalias)
45d6a902 733 {
60d67dc8
AM
734 struct elf_link_hash_entry *def = weakdef (h);
735
736 if (def->dynindx == -1
737 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
738 return FALSE;
739 }
740 }
741
742 return TRUE;
743}
42751cf3 744
8c58d23b
AM
745/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
746 success, and 2 on a failure caused by attempting to record a symbol
747 in a discarded section, eg. a discarded link-once section symbol. */
748
749int
c152c796
AM
750bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
751 bfd *input_bfd,
752 long input_indx)
8c58d23b
AM
753{
754 bfd_size_type amt;
755 struct elf_link_local_dynamic_entry *entry;
756 struct elf_link_hash_table *eht;
757 struct elf_strtab_hash *dynstr;
ef53be89 758 size_t dynstr_index;
8c58d23b
AM
759 char *name;
760 Elf_External_Sym_Shndx eshndx;
761 char esym[sizeof (Elf64_External_Sym)];
762
0eddce27 763 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
764 return 0;
765
766 /* See if the entry exists already. */
767 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
768 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
769 return 1;
770
771 amt = sizeof (*entry);
a50b1753 772 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
773 if (entry == NULL)
774 return 0;
775
776 /* Go find the symbol, so that we can find it's name. */
777 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 778 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
779 {
780 bfd_release (input_bfd, entry);
781 return 0;
782 }
783
784 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 785 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
786 {
787 asection *s;
788
789 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
790 if (s == NULL || bfd_is_abs_section (s->output_section))
791 {
792 /* We can still bfd_release here as nothing has done another
793 bfd_alloc. We can't do this later in this function. */
794 bfd_release (input_bfd, entry);
795 return 2;
796 }
797 }
798
799 name = (bfd_elf_string_from_elf_section
800 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
801 entry->isym.st_name));
802
803 dynstr = elf_hash_table (info)->dynstr;
804 if (dynstr == NULL)
805 {
806 /* Create a strtab to hold the dynamic symbol names. */
807 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
808 if (dynstr == NULL)
809 return 0;
810 }
811
b34976b6 812 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 813 if (dynstr_index == (size_t) -1)
8c58d23b
AM
814 return 0;
815 entry->isym.st_name = dynstr_index;
816
817 eht = elf_hash_table (info);
818
819 entry->next = eht->dynlocal;
820 eht->dynlocal = entry;
821 entry->input_bfd = input_bfd;
822 entry->input_indx = input_indx;
823 eht->dynsymcount++;
824
825 /* Whatever binding the symbol had before, it's now local. */
826 entry->isym.st_info
827 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
828
829 /* The dynindx will be set at the end of size_dynamic_sections. */
830
831 return 1;
832}
833
30b30c21 834/* Return the dynindex of a local dynamic symbol. */
42751cf3 835
30b30c21 836long
268b6b39
AM
837_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
838 bfd *input_bfd,
839 long input_indx)
30b30c21
RH
840{
841 struct elf_link_local_dynamic_entry *e;
842
843 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
844 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
845 return e->dynindx;
846 return -1;
847}
848
849/* This function is used to renumber the dynamic symbols, if some of
850 them are removed because they are marked as local. This is called
851 via elf_link_hash_traverse. */
852
b34976b6 853static bfd_boolean
268b6b39
AM
854elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
855 void *data)
42751cf3 856{
a50b1753 857 size_t *count = (size_t *) data;
30b30c21 858
6fa3860b
PB
859 if (h->forced_local)
860 return TRUE;
861
862 if (h->dynindx != -1)
863 h->dynindx = ++(*count);
864
865 return TRUE;
866}
867
868
869/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
870 STB_LOCAL binding. */
871
872static bfd_boolean
873elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
874 void *data)
875{
a50b1753 876 size_t *count = (size_t *) data;
6fa3860b 877
6fa3860b
PB
878 if (!h->forced_local)
879 return TRUE;
880
42751cf3 881 if (h->dynindx != -1)
30b30c21
RH
882 h->dynindx = ++(*count);
883
b34976b6 884 return TRUE;
42751cf3 885}
30b30c21 886
aee6f5b4
AO
887/* Return true if the dynamic symbol for a given section should be
888 omitted when creating a shared library. */
889bfd_boolean
d00dd7dc
AM
890_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
891 struct bfd_link_info *info,
892 asection *p)
aee6f5b4 893{
74541ad4 894 struct elf_link_hash_table *htab;
ca55926c 895 asection *ip;
74541ad4 896
aee6f5b4
AO
897 switch (elf_section_data (p)->this_hdr.sh_type)
898 {
899 case SHT_PROGBITS:
900 case SHT_NOBITS:
901 /* If sh_type is yet undecided, assume it could be
902 SHT_PROGBITS/SHT_NOBITS. */
903 case SHT_NULL:
74541ad4
AM
904 htab = elf_hash_table (info);
905 if (p == htab->tls_sec)
906 return FALSE;
907
908 if (htab->text_index_section != NULL)
909 return p != htab->text_index_section && p != htab->data_index_section;
910
ca55926c 911 return (htab->dynobj != NULL
3d4d4302 912 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 913 && ip->output_section == p);
aee6f5b4
AO
914
915 /* There shouldn't be section relative relocations
916 against any other section. */
917 default:
918 return TRUE;
919 }
920}
921
d00dd7dc
AM
922bfd_boolean
923_bfd_elf_omit_section_dynsym_all
924 (bfd *output_bfd ATTRIBUTE_UNUSED,
925 struct bfd_link_info *info ATTRIBUTE_UNUSED,
926 asection *p ATTRIBUTE_UNUSED)
927{
928 return TRUE;
929}
930
062e2358 931/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
932 symbol for each output section, which come first. Next come symbols
933 which have been forced to local binding. Then all of the back-end
934 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
935 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
936 (This prevents the early call before elf_backend_init_index_section
937 and strip_excluded_output_sections setting dynindx for sections
938 that are stripped.) */
30b30c21 939
554220db
AM
940static unsigned long
941_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
942 struct bfd_link_info *info,
943 unsigned long *section_sym_count)
30b30c21
RH
944{
945 unsigned long dynsymcount = 0;
63f452a8 946 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 947
0e1862bb
L
948 if (bfd_link_pic (info)
949 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 950 {
aee6f5b4 951 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
952 asection *p;
953 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 954 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 955 && (p->flags & SEC_ALLOC) != 0
7f923b7f 956 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 957 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
958 {
959 ++dynsymcount;
960 if (do_sec)
961 elf_section_data (p)->dynindx = dynsymcount;
962 }
963 else if (do_sec)
74541ad4 964 elf_section_data (p)->dynindx = 0;
30b30c21 965 }
63f452a8
AM
966 if (do_sec)
967 *section_sym_count = dynsymcount;
30b30c21 968
6fa3860b
PB
969 elf_link_hash_traverse (elf_hash_table (info),
970 elf_link_renumber_local_hash_table_dynsyms,
971 &dynsymcount);
972
30b30c21
RH
973 if (elf_hash_table (info)->dynlocal)
974 {
975 struct elf_link_local_dynamic_entry *p;
976 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
977 p->dynindx = ++dynsymcount;
978 }
90ac2420 979 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
980
981 elf_link_hash_traverse (elf_hash_table (info),
982 elf_link_renumber_hash_table_dynsyms,
983 &dynsymcount);
984
d5486c43
L
985 /* There is an unused NULL entry at the head of the table which we
986 must account for in our count even if the table is empty since it
987 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
988 .dynamic section. */
989 dynsymcount++;
30b30c21 990
ccabcbe5
AM
991 elf_hash_table (info)->dynsymcount = dynsymcount;
992 return dynsymcount;
30b30c21 993}
252b5132 994
54ac0771
L
995/* Merge st_other field. */
996
997static void
998elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 999 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 1000 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1001{
1002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1003
1004 /* If st_other has a processor-specific meaning, specific
cd3416da 1005 code might be needed here. */
54ac0771
L
1006 if (bed->elf_backend_merge_symbol_attribute)
1007 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1008 dynamic);
1009
cd3416da 1010 if (!dynamic)
54ac0771 1011 {
cd3416da
AM
1012 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1013 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1014
cd3416da
AM
1015 /* Keep the most constraining visibility. Leave the remainder
1016 of the st_other field to elf_backend_merge_symbol_attribute. */
1017 if (symvis - 1 < hvis - 1)
1018 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1019 }
b8417128
AM
1020 else if (definition
1021 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1022 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1023 h->protected_def = 1;
54ac0771
L
1024}
1025
4f3fedcf
AM
1026/* This function is called when we want to merge a new symbol with an
1027 existing symbol. It handles the various cases which arise when we
1028 find a definition in a dynamic object, or when there is already a
1029 definition in a dynamic object. The new symbol is described by
1030 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1031 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1032 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1033 of an old common symbol. We set OVERRIDE if the old symbol is
1034 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1035 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1036 to change. By OK to change, we mean that we shouldn't warn if the
1037 type or size does change. */
45d6a902 1038
8a56bd02 1039static bfd_boolean
268b6b39
AM
1040_bfd_elf_merge_symbol (bfd *abfd,
1041 struct bfd_link_info *info,
1042 const char *name,
1043 Elf_Internal_Sym *sym,
1044 asection **psec,
1045 bfd_vma *pvalue,
4f3fedcf
AM
1046 struct elf_link_hash_entry **sym_hash,
1047 bfd **poldbfd,
37a9e49a 1048 bfd_boolean *pold_weak,
af44c138 1049 unsigned int *pold_alignment,
268b6b39
AM
1050 bfd_boolean *skip,
1051 bfd_boolean *override,
1052 bfd_boolean *type_change_ok,
6e33951e
L
1053 bfd_boolean *size_change_ok,
1054 bfd_boolean *matched)
252b5132 1055{
7479dfd4 1056 asection *sec, *oldsec;
45d6a902 1057 struct elf_link_hash_entry *h;
90c984fc 1058 struct elf_link_hash_entry *hi;
45d6a902
AM
1059 struct elf_link_hash_entry *flip;
1060 int bind;
1061 bfd *oldbfd;
1062 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1063 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1064 const struct elf_backend_data *bed;
6e33951e 1065 char *new_version;
93f4de39 1066 bfd_boolean default_sym = *matched;
45d6a902
AM
1067
1068 *skip = FALSE;
1069 *override = FALSE;
1070
1071 sec = *psec;
1072 bind = ELF_ST_BIND (sym->st_info);
1073
1074 if (! bfd_is_und_section (sec))
1075 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1076 else
1077 h = ((struct elf_link_hash_entry *)
1078 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1079 if (h == NULL)
1080 return FALSE;
1081 *sym_hash = h;
252b5132 1082
88ba32a0
L
1083 bed = get_elf_backend_data (abfd);
1084
6e33951e 1085 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1086 if (h->versioned != unversioned)
6e33951e 1087 {
422f1182
L
1088 /* Symbol version is unknown or versioned. */
1089 new_version = strrchr (name, ELF_VER_CHR);
1090 if (new_version)
1091 {
1092 if (h->versioned == unknown)
1093 {
1094 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1095 h->versioned = versioned_hidden;
1096 else
1097 h->versioned = versioned;
1098 }
1099 new_version += 1;
1100 if (new_version[0] == '\0')
1101 new_version = NULL;
1102 }
1103 else
1104 h->versioned = unversioned;
6e33951e 1105 }
422f1182
L
1106 else
1107 new_version = NULL;
6e33951e 1108
90c984fc
L
1109 /* For merging, we only care about real symbols. But we need to make
1110 sure that indirect symbol dynamic flags are updated. */
1111 hi = h;
45d6a902
AM
1112 while (h->root.type == bfd_link_hash_indirect
1113 || h->root.type == bfd_link_hash_warning)
1114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1115
6e33951e
L
1116 if (!*matched)
1117 {
1118 if (hi == h || h->root.type == bfd_link_hash_new)
1119 *matched = TRUE;
1120 else
1121 {
ae7683d2 1122 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1123 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1124 true if the new symbol is only visible to the symbol with
6e33951e 1125 the same symbol version. */
422f1182
L
1126 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1127 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1128 if (!old_hidden && !new_hidden)
1129 /* The new symbol matches the existing symbol if both
1130 aren't hidden. */
1131 *matched = TRUE;
1132 else
1133 {
1134 /* OLD_VERSION is the symbol version of the existing
1135 symbol. */
422f1182
L
1136 char *old_version;
1137
1138 if (h->versioned >= versioned)
1139 old_version = strrchr (h->root.root.string,
1140 ELF_VER_CHR) + 1;
1141 else
1142 old_version = NULL;
6e33951e
L
1143
1144 /* The new symbol matches the existing symbol if they
1145 have the same symbol version. */
1146 *matched = (old_version == new_version
1147 || (old_version != NULL
1148 && new_version != NULL
1149 && strcmp (old_version, new_version) == 0));
1150 }
1151 }
1152 }
1153
934bce08
AM
1154 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1155 existing symbol. */
1156
1157 oldbfd = NULL;
1158 oldsec = NULL;
1159 switch (h->root.type)
1160 {
1161 default:
1162 break;
1163
1164 case bfd_link_hash_undefined:
1165 case bfd_link_hash_undefweak:
1166 oldbfd = h->root.u.undef.abfd;
1167 break;
1168
1169 case bfd_link_hash_defined:
1170 case bfd_link_hash_defweak:
1171 oldbfd = h->root.u.def.section->owner;
1172 oldsec = h->root.u.def.section;
1173 break;
1174
1175 case bfd_link_hash_common:
1176 oldbfd = h->root.u.c.p->section->owner;
1177 oldsec = h->root.u.c.p->section;
1178 if (pold_alignment)
1179 *pold_alignment = h->root.u.c.p->alignment_power;
1180 break;
1181 }
1182 if (poldbfd && *poldbfd == NULL)
1183 *poldbfd = oldbfd;
1184
1185 /* Differentiate strong and weak symbols. */
1186 newweak = bind == STB_WEAK;
1187 oldweak = (h->root.type == bfd_link_hash_defweak
1188 || h->root.type == bfd_link_hash_undefweak);
1189 if (pold_weak)
1190 *pold_weak = oldweak;
1191
40b36307 1192 /* We have to check it for every instance since the first few may be
ee659f1f 1193 references and not all compilers emit symbol type for undefined
40b36307
L
1194 symbols. */
1195 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1196
ee659f1f
AM
1197 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1198 respectively, is from a dynamic object. */
1199
1200 newdyn = (abfd->flags & DYNAMIC) != 0;
1201
1202 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1203 syms and defined syms in dynamic libraries respectively.
1204 ref_dynamic on the other hand can be set for a symbol defined in
1205 a dynamic library, and def_dynamic may not be set; When the
1206 definition in a dynamic lib is overridden by a definition in the
1207 executable use of the symbol in the dynamic lib becomes a
1208 reference to the executable symbol. */
1209 if (newdyn)
1210 {
1211 if (bfd_is_und_section (sec))
1212 {
1213 if (bind != STB_WEAK)
1214 {
1215 h->ref_dynamic_nonweak = 1;
1216 hi->ref_dynamic_nonweak = 1;
1217 }
1218 }
1219 else
1220 {
6e33951e
L
1221 /* Update the existing symbol only if they match. */
1222 if (*matched)
1223 h->dynamic_def = 1;
ee659f1f
AM
1224 hi->dynamic_def = 1;
1225 }
1226 }
1227
45d6a902
AM
1228 /* If we just created the symbol, mark it as being an ELF symbol.
1229 Other than that, there is nothing to do--there is no merge issue
1230 with a newly defined symbol--so we just return. */
1231
1232 if (h->root.type == bfd_link_hash_new)
252b5132 1233 {
f5385ebf 1234 h->non_elf = 0;
45d6a902
AM
1235 return TRUE;
1236 }
252b5132 1237
45d6a902
AM
1238 /* In cases involving weak versioned symbols, we may wind up trying
1239 to merge a symbol with itself. Catch that here, to avoid the
1240 confusion that results if we try to override a symbol with
1241 itself. The additional tests catch cases like
1242 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1243 dynamic object, which we do want to handle here. */
1244 if (abfd == oldbfd
895fa45f 1245 && (newweak || oldweak)
45d6a902 1246 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1247 || !h->def_regular))
45d6a902
AM
1248 return TRUE;
1249
707bba77 1250 olddyn = FALSE;
45d6a902
AM
1251 if (oldbfd != NULL)
1252 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1253 else if (oldsec != NULL)
45d6a902 1254 {
707bba77 1255 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1256 indices used by MIPS ELF. */
707bba77 1257 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1258 }
252b5132 1259
1a3b5c34
AM
1260 /* Handle a case where plugin_notice won't be called and thus won't
1261 set the non_ir_ref flags on the first pass over symbols. */
1262 if (oldbfd != NULL
1263 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1264 && newdyn != olddyn)
1265 {
1266 h->root.non_ir_ref_dynamic = TRUE;
1267 hi->root.non_ir_ref_dynamic = TRUE;
1268 }
1269
45d6a902
AM
1270 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1271 respectively, appear to be a definition rather than reference. */
1272
707bba77 1273 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1274
707bba77
AM
1275 olddef = (h->root.type != bfd_link_hash_undefined
1276 && h->root.type != bfd_link_hash_undefweak
202ac193 1277 && h->root.type != bfd_link_hash_common);
45d6a902 1278
0a36a439
L
1279 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1280 respectively, appear to be a function. */
1281
1282 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1283 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1284
1285 oldfunc = (h->type != STT_NOTYPE
1286 && bed->is_function_type (h->type));
1287
c5d37467 1288 if (!(newfunc && oldfunc)
5b677558
AM
1289 && ELF_ST_TYPE (sym->st_info) != h->type
1290 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1291 && h->type != STT_NOTYPE
c5d37467
AM
1292 && (newdef || bfd_is_com_section (sec))
1293 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1294 {
c5d37467
AM
1295 /* If creating a default indirect symbol ("foo" or "foo@") from
1296 a dynamic versioned definition ("foo@@") skip doing so if
1297 there is an existing regular definition with a different
1298 type. We don't want, for example, a "time" variable in the
1299 executable overriding a "time" function in a shared library. */
1300 if (newdyn
1301 && !olddyn)
1302 {
1303 *skip = TRUE;
1304 return TRUE;
1305 }
1306
1307 /* When adding a symbol from a regular object file after we have
1308 created indirect symbols, undo the indirection and any
1309 dynamic state. */
1310 if (hi != h
1311 && !newdyn
1312 && olddyn)
1313 {
1314 h = hi;
1315 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1316 h->forced_local = 0;
1317 h->ref_dynamic = 0;
1318 h->def_dynamic = 0;
1319 h->dynamic_def = 0;
1320 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1321 {
1322 h->root.type = bfd_link_hash_undefined;
1323 h->root.u.undef.abfd = abfd;
1324 }
1325 else
1326 {
1327 h->root.type = bfd_link_hash_new;
1328 h->root.u.undef.abfd = NULL;
1329 }
1330 return TRUE;
1331 }
580a2b6e
L
1332 }
1333
4c34aff8
AM
1334 /* Check TLS symbols. We don't check undefined symbols introduced
1335 by "ld -u" which have no type (and oldbfd NULL), and we don't
1336 check symbols from plugins because they also have no type. */
1337 if (oldbfd != NULL
1338 && (oldbfd->flags & BFD_PLUGIN) == 0
1339 && (abfd->flags & BFD_PLUGIN) == 0
1340 && ELF_ST_TYPE (sym->st_info) != h->type
1341 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1342 {
1343 bfd *ntbfd, *tbfd;
1344 bfd_boolean ntdef, tdef;
1345 asection *ntsec, *tsec;
1346
1347 if (h->type == STT_TLS)
1348 {
3b36f7e6 1349 ntbfd = abfd;
7479dfd4
L
1350 ntsec = sec;
1351 ntdef = newdef;
1352 tbfd = oldbfd;
1353 tsec = oldsec;
1354 tdef = olddef;
1355 }
1356 else
1357 {
1358 ntbfd = oldbfd;
1359 ntsec = oldsec;
1360 ntdef = olddef;
1361 tbfd = abfd;
1362 tsec = sec;
1363 tdef = newdef;
1364 }
1365
1366 if (tdef && ntdef)
4eca0228 1367 _bfd_error_handler
695344c0 1368 /* xgettext:c-format */
871b3ab2
AM
1369 (_("%s: TLS definition in %pB section %pA "
1370 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1371 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1372 else if (!tdef && !ntdef)
4eca0228 1373 _bfd_error_handler
695344c0 1374 /* xgettext:c-format */
871b3ab2
AM
1375 (_("%s: TLS reference in %pB "
1376 "mismatches non-TLS reference in %pB"),
c08bb8dd 1377 h->root.root.string, tbfd, ntbfd);
7479dfd4 1378 else if (tdef)
4eca0228 1379 _bfd_error_handler
695344c0 1380 /* xgettext:c-format */
871b3ab2
AM
1381 (_("%s: TLS definition in %pB section %pA "
1382 "mismatches non-TLS reference in %pB"),
c08bb8dd 1383 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1384 else
4eca0228 1385 _bfd_error_handler
695344c0 1386 /* xgettext:c-format */
871b3ab2
AM
1387 (_("%s: TLS reference in %pB "
1388 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1389 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1390
1391 bfd_set_error (bfd_error_bad_value);
1392 return FALSE;
1393 }
1394
45d6a902
AM
1395 /* If the old symbol has non-default visibility, we ignore the new
1396 definition from a dynamic object. */
1397 if (newdyn
9c7a29a3 1398 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1399 && !bfd_is_und_section (sec))
1400 {
1401 *skip = TRUE;
1402 /* Make sure this symbol is dynamic. */
f5385ebf 1403 h->ref_dynamic = 1;
90c984fc 1404 hi->ref_dynamic = 1;
45d6a902
AM
1405 /* A protected symbol has external availability. Make sure it is
1406 recorded as dynamic.
1407
1408 FIXME: Should we check type and size for protected symbol? */
1409 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1410 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1411 else
1412 return TRUE;
1413 }
1414 else if (!newdyn
9c7a29a3 1415 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1416 && h->def_dynamic)
45d6a902
AM
1417 {
1418 /* If the new symbol with non-default visibility comes from a
1419 relocatable file and the old definition comes from a dynamic
1420 object, we remove the old definition. */
6c9b78e6 1421 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1422 {
1423 /* Handle the case where the old dynamic definition is
1424 default versioned. We need to copy the symbol info from
1425 the symbol with default version to the normal one if it
1426 was referenced before. */
1427 if (h->ref_regular)
1428 {
6c9b78e6 1429 hi->root.type = h->root.type;
d2dee3b2 1430 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1431 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1432
6c9b78e6 1433 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1434 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1435 {
aed81c4e
MR
1436 /* If the new symbol is hidden or internal, completely undo
1437 any dynamic link state. */
1438 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1439 h->forced_local = 0;
1440 h->ref_dynamic = 0;
d2dee3b2
L
1441 }
1442 else
aed81c4e
MR
1443 h->ref_dynamic = 1;
1444
1445 h->def_dynamic = 0;
aed81c4e
MR
1446 /* FIXME: Should we check type and size for protected symbol? */
1447 h->size = 0;
1448 h->type = 0;
1449
6c9b78e6 1450 h = hi;
d2dee3b2
L
1451 }
1452 else
6c9b78e6 1453 h = hi;
d2dee3b2 1454 }
1de1a317 1455
f5eda473
AM
1456 /* If the old symbol was undefined before, then it will still be
1457 on the undefs list. If the new symbol is undefined or
1458 common, we can't make it bfd_link_hash_new here, because new
1459 undefined or common symbols will be added to the undefs list
1460 by _bfd_generic_link_add_one_symbol. Symbols may not be
1461 added twice to the undefs list. Also, if the new symbol is
1462 undefweak then we don't want to lose the strong undef. */
1463 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1464 {
1de1a317 1465 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1466 h->root.u.undef.abfd = abfd;
1467 }
1468 else
1469 {
1470 h->root.type = bfd_link_hash_new;
1471 h->root.u.undef.abfd = NULL;
1472 }
1473
f5eda473 1474 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1475 {
f5eda473
AM
1476 /* If the new symbol is hidden or internal, completely undo
1477 any dynamic link state. */
1478 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1479 h->forced_local = 0;
1480 h->ref_dynamic = 0;
45d6a902 1481 }
f5eda473
AM
1482 else
1483 h->ref_dynamic = 1;
1484 h->def_dynamic = 0;
45d6a902
AM
1485 /* FIXME: Should we check type and size for protected symbol? */
1486 h->size = 0;
1487 h->type = 0;
1488 return TRUE;
1489 }
14a793b2 1490
15b43f48
AM
1491 /* If a new weak symbol definition comes from a regular file and the
1492 old symbol comes from a dynamic library, we treat the new one as
1493 strong. Similarly, an old weak symbol definition from a regular
1494 file is treated as strong when the new symbol comes from a dynamic
1495 library. Further, an old weak symbol from a dynamic library is
1496 treated as strong if the new symbol is from a dynamic library.
1497 This reflects the way glibc's ld.so works.
1498
165f707a
AM
1499 Also allow a weak symbol to override a linker script symbol
1500 defined by an early pass over the script. This is done so the
1501 linker knows the symbol is defined in an object file, for the
1502 DEFINED script function.
1503
15b43f48
AM
1504 Do this before setting *type_change_ok or *size_change_ok so that
1505 we warn properly when dynamic library symbols are overridden. */
1506
165f707a 1507 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1508 newweak = FALSE;
15b43f48 1509 if (olddef && newdyn)
0f8a2703
AM
1510 oldweak = FALSE;
1511
d334575b 1512 /* Allow changes between different types of function symbol. */
0a36a439 1513 if (newfunc && oldfunc)
fcb93ecf
PB
1514 *type_change_ok = TRUE;
1515
79349b09
AM
1516 /* It's OK to change the type if either the existing symbol or the
1517 new symbol is weak. A type change is also OK if the old symbol
1518 is undefined and the new symbol is defined. */
252b5132 1519
79349b09
AM
1520 if (oldweak
1521 || newweak
1522 || (newdef
1523 && h->root.type == bfd_link_hash_undefined))
1524 *type_change_ok = TRUE;
1525
1526 /* It's OK to change the size if either the existing symbol or the
1527 new symbol is weak, or if the old symbol is undefined. */
1528
1529 if (*type_change_ok
1530 || h->root.type == bfd_link_hash_undefined)
1531 *size_change_ok = TRUE;
45d6a902 1532
45d6a902
AM
1533 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1534 symbol, respectively, appears to be a common symbol in a dynamic
1535 object. If a symbol appears in an uninitialized section, and is
1536 not weak, and is not a function, then it may be a common symbol
1537 which was resolved when the dynamic object was created. We want
1538 to treat such symbols specially, because they raise special
1539 considerations when setting the symbol size: if the symbol
1540 appears as a common symbol in a regular object, and the size in
1541 the regular object is larger, we must make sure that we use the
1542 larger size. This problematic case can always be avoided in C,
1543 but it must be handled correctly when using Fortran shared
1544 libraries.
1545
1546 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1547 likewise for OLDDYNCOMMON and OLDDEF.
1548
1549 Note that this test is just a heuristic, and that it is quite
1550 possible to have an uninitialized symbol in a shared object which
1551 is really a definition, rather than a common symbol. This could
1552 lead to some minor confusion when the symbol really is a common
1553 symbol in some regular object. However, I think it will be
1554 harmless. */
1555
1556 if (newdyn
1557 && newdef
79349b09 1558 && !newweak
45d6a902
AM
1559 && (sec->flags & SEC_ALLOC) != 0
1560 && (sec->flags & SEC_LOAD) == 0
1561 && sym->st_size > 0
0a36a439 1562 && !newfunc)
45d6a902
AM
1563 newdyncommon = TRUE;
1564 else
1565 newdyncommon = FALSE;
1566
1567 if (olddyn
1568 && olddef
1569 && h->root.type == bfd_link_hash_defined
f5385ebf 1570 && h->def_dynamic
45d6a902
AM
1571 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1572 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1573 && h->size > 0
0a36a439 1574 && !oldfunc)
45d6a902
AM
1575 olddyncommon = TRUE;
1576 else
1577 olddyncommon = FALSE;
1578
a4d8e49b
L
1579 /* We now know everything about the old and new symbols. We ask the
1580 backend to check if we can merge them. */
5d13b3b3
AM
1581 if (bed->merge_symbol != NULL)
1582 {
1583 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1584 return FALSE;
1585 sec = *psec;
1586 }
a4d8e49b 1587
a83ef4d1
L
1588 /* There are multiple definitions of a normal symbol. Skip the
1589 default symbol as well as definition from an IR object. */
93f4de39 1590 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1591 && !default_sym && h->def_regular
1592 && !(oldbfd != NULL
1593 && (oldbfd->flags & BFD_PLUGIN) != 0
1594 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1595 {
1596 /* Handle a multiple definition. */
1597 (*info->callbacks->multiple_definition) (info, &h->root,
1598 abfd, sec, *pvalue);
1599 *skip = TRUE;
1600 return TRUE;
1601 }
1602
45d6a902
AM
1603 /* If both the old and the new symbols look like common symbols in a
1604 dynamic object, set the size of the symbol to the larger of the
1605 two. */
1606
1607 if (olddyncommon
1608 && newdyncommon
1609 && sym->st_size != h->size)
1610 {
1611 /* Since we think we have two common symbols, issue a multiple
1612 common warning if desired. Note that we only warn if the
1613 size is different. If the size is the same, we simply let
1614 the old symbol override the new one as normally happens with
1615 symbols defined in dynamic objects. */
1616
1a72702b
AM
1617 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1618 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1619 if (sym->st_size > h->size)
1620 h->size = sym->st_size;
252b5132 1621
45d6a902 1622 *size_change_ok = TRUE;
252b5132
RH
1623 }
1624
45d6a902
AM
1625 /* If we are looking at a dynamic object, and we have found a
1626 definition, we need to see if the symbol was already defined by
1627 some other object. If so, we want to use the existing
1628 definition, and we do not want to report a multiple symbol
1629 definition error; we do this by clobbering *PSEC to be
1630 bfd_und_section_ptr.
1631
1632 We treat a common symbol as a definition if the symbol in the
1633 shared library is a function, since common symbols always
1634 represent variables; this can cause confusion in principle, but
1635 any such confusion would seem to indicate an erroneous program or
1636 shared library. We also permit a common symbol in a regular
8170f769 1637 object to override a weak symbol in a shared object. */
45d6a902
AM
1638
1639 if (newdyn
1640 && newdef
77cfaee6 1641 && (olddef
45d6a902 1642 || (h->root.type == bfd_link_hash_common
8170f769 1643 && (newweak || newfunc))))
45d6a902
AM
1644 {
1645 *override = TRUE;
1646 newdef = FALSE;
1647 newdyncommon = FALSE;
252b5132 1648
45d6a902
AM
1649 *psec = sec = bfd_und_section_ptr;
1650 *size_change_ok = TRUE;
252b5132 1651
45d6a902
AM
1652 /* If we get here when the old symbol is a common symbol, then
1653 we are explicitly letting it override a weak symbol or
1654 function in a dynamic object, and we don't want to warn about
1655 a type change. If the old symbol is a defined symbol, a type
1656 change warning may still be appropriate. */
252b5132 1657
45d6a902
AM
1658 if (h->root.type == bfd_link_hash_common)
1659 *type_change_ok = TRUE;
1660 }
1661
1662 /* Handle the special case of an old common symbol merging with a
1663 new symbol which looks like a common symbol in a shared object.
1664 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1665 common symbol, and let _bfd_generic_link_add_one_symbol do the
1666 right thing. */
45d6a902
AM
1667
1668 if (newdyncommon
1669 && h->root.type == bfd_link_hash_common)
1670 {
1671 *override = TRUE;
1672 newdef = FALSE;
1673 newdyncommon = FALSE;
1674 *pvalue = sym->st_size;
a4d8e49b 1675 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1676 *size_change_ok = TRUE;
1677 }
1678
c5e2cead 1679 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1680 if (newdef && olddef && newweak)
54ac0771 1681 {
35ed3f94 1682 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1683 if (!(oldbfd != NULL
1684 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1685 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1686 {
1687 newdef = FALSE;
1688 *skip = TRUE;
1689 }
54ac0771
L
1690
1691 /* Merge st_other. If the symbol already has a dynamic index,
1692 but visibility says it should not be visible, turn it into a
1693 local symbol. */
b8417128 1694 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1695 if (h->dynindx != -1)
1696 switch (ELF_ST_VISIBILITY (h->other))
1697 {
1698 case STV_INTERNAL:
1699 case STV_HIDDEN:
1700 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1701 break;
1702 }
1703 }
c5e2cead 1704
45d6a902
AM
1705 /* If the old symbol is from a dynamic object, and the new symbol is
1706 a definition which is not from a dynamic object, then the new
1707 symbol overrides the old symbol. Symbols from regular files
1708 always take precedence over symbols from dynamic objects, even if
1709 they are defined after the dynamic object in the link.
1710
1711 As above, we again permit a common symbol in a regular object to
1712 override a definition in a shared object if the shared object
0f8a2703 1713 symbol is a function or is weak. */
45d6a902
AM
1714
1715 flip = NULL;
77cfaee6 1716 if (!newdyn
45d6a902
AM
1717 && (newdef
1718 || (bfd_is_com_section (sec)
0a36a439 1719 && (oldweak || oldfunc)))
45d6a902
AM
1720 && olddyn
1721 && olddef
f5385ebf 1722 && h->def_dynamic)
45d6a902
AM
1723 {
1724 /* Change the hash table entry to undefined, and let
1725 _bfd_generic_link_add_one_symbol do the right thing with the
1726 new definition. */
1727
1728 h->root.type = bfd_link_hash_undefined;
1729 h->root.u.undef.abfd = h->root.u.def.section->owner;
1730 *size_change_ok = TRUE;
1731
1732 olddef = FALSE;
1733 olddyncommon = FALSE;
1734
1735 /* We again permit a type change when a common symbol may be
1736 overriding a function. */
1737
1738 if (bfd_is_com_section (sec))
0a36a439
L
1739 {
1740 if (oldfunc)
1741 {
1742 /* If a common symbol overrides a function, make sure
1743 that it isn't defined dynamically nor has type
1744 function. */
1745 h->def_dynamic = 0;
1746 h->type = STT_NOTYPE;
1747 }
1748 *type_change_ok = TRUE;
1749 }
45d6a902 1750
6c9b78e6
AM
1751 if (hi->root.type == bfd_link_hash_indirect)
1752 flip = hi;
45d6a902
AM
1753 else
1754 /* This union may have been set to be non-NULL when this symbol
1755 was seen in a dynamic object. We must force the union to be
1756 NULL, so that it is correct for a regular symbol. */
1757 h->verinfo.vertree = NULL;
1758 }
1759
1760 /* Handle the special case of a new common symbol merging with an
1761 old symbol that looks like it might be a common symbol defined in
1762 a shared object. Note that we have already handled the case in
1763 which a new common symbol should simply override the definition
1764 in the shared library. */
1765
1766 if (! newdyn
1767 && bfd_is_com_section (sec)
1768 && olddyncommon)
1769 {
1770 /* It would be best if we could set the hash table entry to a
1771 common symbol, but we don't know what to use for the section
1772 or the alignment. */
1a72702b
AM
1773 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1774 bfd_link_hash_common, sym->st_size);
45d6a902 1775
4cc11e76 1776 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1777 larger, pretend that the new symbol has its size. */
1778
1779 if (h->size > *pvalue)
1780 *pvalue = h->size;
1781
af44c138
L
1782 /* We need to remember the alignment required by the symbol
1783 in the dynamic object. */
1784 BFD_ASSERT (pold_alignment);
1785 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1786
1787 olddef = FALSE;
1788 olddyncommon = FALSE;
1789
1790 h->root.type = bfd_link_hash_undefined;
1791 h->root.u.undef.abfd = h->root.u.def.section->owner;
1792
1793 *size_change_ok = TRUE;
1794 *type_change_ok = TRUE;
1795
6c9b78e6
AM
1796 if (hi->root.type == bfd_link_hash_indirect)
1797 flip = hi;
45d6a902
AM
1798 else
1799 h->verinfo.vertree = NULL;
1800 }
1801
1802 if (flip != NULL)
1803 {
1804 /* Handle the case where we had a versioned symbol in a dynamic
1805 library and now find a definition in a normal object. In this
1806 case, we make the versioned symbol point to the normal one. */
45d6a902 1807 flip->root.type = h->root.type;
00cbee0a 1808 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1809 h->root.type = bfd_link_hash_indirect;
1810 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1811 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1812 if (h->def_dynamic)
45d6a902 1813 {
f5385ebf
AM
1814 h->def_dynamic = 0;
1815 flip->ref_dynamic = 1;
45d6a902
AM
1816 }
1817 }
1818
45d6a902
AM
1819 return TRUE;
1820}
1821
1822/* This function is called to create an indirect symbol from the
1823 default for the symbol with the default version if needed. The
4f3fedcf 1824 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1825 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1826
28caa186 1827static bfd_boolean
268b6b39
AM
1828_bfd_elf_add_default_symbol (bfd *abfd,
1829 struct bfd_link_info *info,
1830 struct elf_link_hash_entry *h,
1831 const char *name,
1832 Elf_Internal_Sym *sym,
4f3fedcf
AM
1833 asection *sec,
1834 bfd_vma value,
1835 bfd **poldbfd,
e3c9d234 1836 bfd_boolean *dynsym)
45d6a902
AM
1837{
1838 bfd_boolean type_change_ok;
1839 bfd_boolean size_change_ok;
1840 bfd_boolean skip;
1841 char *shortname;
1842 struct elf_link_hash_entry *hi;
1843 struct bfd_link_hash_entry *bh;
9c5bfbb7 1844 const struct elf_backend_data *bed;
45d6a902
AM
1845 bfd_boolean collect;
1846 bfd_boolean dynamic;
e3c9d234 1847 bfd_boolean override;
45d6a902
AM
1848 char *p;
1849 size_t len, shortlen;
ffd65175 1850 asection *tmp_sec;
6e33951e 1851 bfd_boolean matched;
45d6a902 1852
422f1182
L
1853 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1854 return TRUE;
1855
45d6a902
AM
1856 /* If this symbol has a version, and it is the default version, we
1857 create an indirect symbol from the default name to the fully
1858 decorated name. This will cause external references which do not
1859 specify a version to be bound to this version of the symbol. */
1860 p = strchr (name, ELF_VER_CHR);
422f1182
L
1861 if (h->versioned == unknown)
1862 {
1863 if (p == NULL)
1864 {
1865 h->versioned = unversioned;
1866 return TRUE;
1867 }
1868 else
1869 {
1870 if (p[1] != ELF_VER_CHR)
1871 {
1872 h->versioned = versioned_hidden;
1873 return TRUE;
1874 }
1875 else
1876 h->versioned = versioned;
1877 }
1878 }
4373f8af
L
1879 else
1880 {
1881 /* PR ld/19073: We may see an unversioned definition after the
1882 default version. */
1883 if (p == NULL)
1884 return TRUE;
1885 }
45d6a902 1886
45d6a902
AM
1887 bed = get_elf_backend_data (abfd);
1888 collect = bed->collect;
1889 dynamic = (abfd->flags & DYNAMIC) != 0;
1890
1891 shortlen = p - name;
a50b1753 1892 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1893 if (shortname == NULL)
1894 return FALSE;
1895 memcpy (shortname, name, shortlen);
1896 shortname[shortlen] = '\0';
1897
1898 /* We are going to create a new symbol. Merge it with any existing
1899 symbol with this name. For the purposes of the merge, act as
1900 though we were defining the symbol we just defined, although we
1901 actually going to define an indirect symbol. */
1902 type_change_ok = FALSE;
1903 size_change_ok = FALSE;
6e33951e 1904 matched = TRUE;
ffd65175
AM
1905 tmp_sec = sec;
1906 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1907 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1908 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1909 return FALSE;
1910
1911 if (skip)
1912 goto nondefault;
1913
5b677558
AM
1914 if (hi->def_regular)
1915 {
1916 /* If the undecorated symbol will have a version added by a
1917 script different to H, then don't indirect to/from the
1918 undecorated symbol. This isn't ideal because we may not yet
1919 have seen symbol versions, if given by a script on the
1920 command line rather than via --version-script. */
1921 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1922 {
1923 bfd_boolean hide;
1924
1925 hi->verinfo.vertree
1926 = bfd_find_version_for_sym (info->version_info,
1927 hi->root.root.string, &hide);
1928 if (hi->verinfo.vertree != NULL && hide)
1929 {
1930 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1931 goto nondefault;
1932 }
1933 }
1934 if (hi->verinfo.vertree != NULL
1935 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1936 goto nondefault;
1937 }
1938
45d6a902
AM
1939 if (! override)
1940 {
c6e8a9a8 1941 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1942 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1943 {
1944 bh = &hi->root;
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, shortname, BSF_INDIRECT,
1947 bfd_ind_section_ptr,
1948 0, name, FALSE, collect, &bh)))
1949 return FALSE;
1950 hi = (struct elf_link_hash_entry *) bh;
1951 }
45d6a902
AM
1952 }
1953 else
1954 {
1955 /* In this case the symbol named SHORTNAME is overriding the
1956 indirect symbol we want to add. We were planning on making
1957 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1958 is the name without a version. NAME is the fully versioned
1959 name, and it is the default version.
1960
1961 Overriding means that we already saw a definition for the
1962 symbol SHORTNAME in a regular object, and it is overriding
1963 the symbol defined in the dynamic object.
1964
1965 When this happens, we actually want to change NAME, the
1966 symbol we just added, to refer to SHORTNAME. This will cause
1967 references to NAME in the shared object to become references
1968 to SHORTNAME in the regular object. This is what we expect
1969 when we override a function in a shared object: that the
1970 references in the shared object will be mapped to the
1971 definition in the regular object. */
1972
1973 while (hi->root.type == bfd_link_hash_indirect
1974 || hi->root.type == bfd_link_hash_warning)
1975 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1976
1977 h->root.type = bfd_link_hash_indirect;
1978 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1979 if (h->def_dynamic)
45d6a902 1980 {
f5385ebf
AM
1981 h->def_dynamic = 0;
1982 hi->ref_dynamic = 1;
1983 if (hi->ref_regular
1984 || hi->def_regular)
45d6a902 1985 {
c152c796 1986 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1987 return FALSE;
1988 }
1989 }
1990
1991 /* Now set HI to H, so that the following code will set the
1992 other fields correctly. */
1993 hi = h;
1994 }
1995
fab4a87f
L
1996 /* Check if HI is a warning symbol. */
1997 if (hi->root.type == bfd_link_hash_warning)
1998 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1999
45d6a902
AM
2000 /* If there is a duplicate definition somewhere, then HI may not
2001 point to an indirect symbol. We will have reported an error to
2002 the user in that case. */
2003
2004 if (hi->root.type == bfd_link_hash_indirect)
2005 {
2006 struct elf_link_hash_entry *ht;
2007
45d6a902 2008 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2009 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2010
68c88cd4
AM
2011 /* A reference to the SHORTNAME symbol from a dynamic library
2012 will be satisfied by the versioned symbol at runtime. In
2013 effect, we have a reference to the versioned symbol. */
2014 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2015 hi->dynamic_def |= ht->dynamic_def;
2016
45d6a902
AM
2017 /* See if the new flags lead us to realize that the symbol must
2018 be dynamic. */
2019 if (! *dynsym)
2020 {
2021 if (! dynamic)
2022 {
0e1862bb 2023 if (! bfd_link_executable (info)
90c984fc 2024 || hi->def_dynamic
f5385ebf 2025 || hi->ref_dynamic)
45d6a902
AM
2026 *dynsym = TRUE;
2027 }
2028 else
2029 {
f5385ebf 2030 if (hi->ref_regular)
45d6a902
AM
2031 *dynsym = TRUE;
2032 }
2033 }
2034 }
2035
2036 /* We also need to define an indirection from the nondefault version
2037 of the symbol. */
2038
2039nondefault:
2040 len = strlen (name);
a50b1753 2041 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2042 if (shortname == NULL)
2043 return FALSE;
2044 memcpy (shortname, name, shortlen);
2045 memcpy (shortname + shortlen, p + 1, len - shortlen);
2046
2047 /* Once again, merge with any existing symbol. */
2048 type_change_ok = FALSE;
2049 size_change_ok = FALSE;
ffd65175
AM
2050 tmp_sec = sec;
2051 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2052 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2053 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2054 return FALSE;
2055
2056 if (skip)
2057 return TRUE;
2058
2059 if (override)
2060 {
2061 /* Here SHORTNAME is a versioned name, so we don't expect to see
2062 the type of override we do in the case above unless it is
4cc11e76 2063 overridden by a versioned definition. */
45d6a902
AM
2064 if (hi->root.type != bfd_link_hash_defined
2065 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2066 _bfd_error_handler
695344c0 2067 /* xgettext:c-format */
871b3ab2 2068 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2069 abfd, shortname);
45d6a902
AM
2070 }
2071 else
2072 {
2073 bh = &hi->root;
2074 if (! (_bfd_generic_link_add_one_symbol
2075 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2076 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2077 return FALSE;
2078 hi = (struct elf_link_hash_entry *) bh;
2079
2080 /* If there is a duplicate definition somewhere, then HI may not
2081 point to an indirect symbol. We will have reported an error
2082 to the user in that case. */
2083
2084 if (hi->root.type == bfd_link_hash_indirect)
2085 {
fcfa13d2 2086 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2087 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2088 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2089
2090 /* See if the new flags lead us to realize that the symbol
2091 must be dynamic. */
2092 if (! *dynsym)
2093 {
2094 if (! dynamic)
2095 {
0e1862bb 2096 if (! bfd_link_executable (info)
f5385ebf 2097 || hi->ref_dynamic)
45d6a902
AM
2098 *dynsym = TRUE;
2099 }
2100 else
2101 {
f5385ebf 2102 if (hi->ref_regular)
45d6a902
AM
2103 *dynsym = TRUE;
2104 }
2105 }
2106 }
2107 }
2108
2109 return TRUE;
2110}
2111\f
2112/* This routine is used to export all defined symbols into the dynamic
2113 symbol table. It is called via elf_link_hash_traverse. */
2114
28caa186 2115static bfd_boolean
268b6b39 2116_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2117{
a50b1753 2118 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2119
2120 /* Ignore indirect symbols. These are added by the versioning code. */
2121 if (h->root.type == bfd_link_hash_indirect)
2122 return TRUE;
2123
7686d77d
AM
2124 /* Ignore this if we won't export it. */
2125 if (!eif->info->export_dynamic && !h->dynamic)
2126 return TRUE;
45d6a902
AM
2127
2128 if (h->dynindx == -1
fd91d419
L
2129 && (h->def_regular || h->ref_regular)
2130 && ! bfd_hide_sym_by_version (eif->info->version_info,
2131 h->root.root.string))
45d6a902 2132 {
fd91d419 2133 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2134 {
fd91d419
L
2135 eif->failed = TRUE;
2136 return FALSE;
45d6a902
AM
2137 }
2138 }
2139
2140 return TRUE;
2141}
2142\f
2143/* Look through the symbols which are defined in other shared
2144 libraries and referenced here. Update the list of version
2145 dependencies. This will be put into the .gnu.version_r section.
2146 This function is called via elf_link_hash_traverse. */
2147
28caa186 2148static bfd_boolean
268b6b39
AM
2149_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2150 void *data)
45d6a902 2151{
a50b1753 2152 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2153 Elf_Internal_Verneed *t;
2154 Elf_Internal_Vernaux *a;
2155 bfd_size_type amt;
2156
45d6a902
AM
2157 /* We only care about symbols defined in shared objects with version
2158 information. */
f5385ebf
AM
2159 if (!h->def_dynamic
2160 || h->def_regular
45d6a902 2161 || h->dynindx == -1
7b20f099
AM
2162 || h->verinfo.verdef == NULL
2163 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2164 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2165 return TRUE;
2166
2167 /* See if we already know about this version. */
28caa186
AM
2168 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2169 t != NULL;
2170 t = t->vn_nextref)
45d6a902
AM
2171 {
2172 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2173 continue;
2174
2175 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2176 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2177 return TRUE;
2178
2179 break;
2180 }
2181
2182 /* This is a new version. Add it to tree we are building. */
2183
2184 if (t == NULL)
2185 {
2186 amt = sizeof *t;
a50b1753 2187 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2188 if (t == NULL)
2189 {
2190 rinfo->failed = TRUE;
2191 return FALSE;
2192 }
2193
2194 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2195 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2196 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2197 }
2198
2199 amt = sizeof *a;
a50b1753 2200 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2201 if (a == NULL)
2202 {
2203 rinfo->failed = TRUE;
2204 return FALSE;
2205 }
45d6a902
AM
2206
2207 /* Note that we are copying a string pointer here, and testing it
2208 above. If bfd_elf_string_from_elf_section is ever changed to
2209 discard the string data when low in memory, this will have to be
2210 fixed. */
2211 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2212
2213 a->vna_flags = h->verinfo.verdef->vd_flags;
2214 a->vna_nextptr = t->vn_auxptr;
2215
2216 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2217 ++rinfo->vers;
2218
2219 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2220
2221 t->vn_auxptr = a;
2222
2223 return TRUE;
2224}
2225
099bb8fb
L
2226/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2227 hidden. Set *T_P to NULL if there is no match. */
2228
2229static bfd_boolean
2230_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2231 struct elf_link_hash_entry *h,
2232 const char *version_p,
2233 struct bfd_elf_version_tree **t_p,
2234 bfd_boolean *hide)
2235{
2236 struct bfd_elf_version_tree *t;
2237
2238 /* Look for the version. If we find it, it is no longer weak. */
2239 for (t = info->version_info; t != NULL; t = t->next)
2240 {
2241 if (strcmp (t->name, version_p) == 0)
2242 {
2243 size_t len;
2244 char *alc;
2245 struct bfd_elf_version_expr *d;
2246
2247 len = version_p - h->root.root.string;
2248 alc = (char *) bfd_malloc (len);
2249 if (alc == NULL)
2250 return FALSE;
2251 memcpy (alc, h->root.root.string, len - 1);
2252 alc[len - 1] = '\0';
2253 if (alc[len - 2] == ELF_VER_CHR)
2254 alc[len - 2] = '\0';
2255
2256 h->verinfo.vertree = t;
2257 t->used = TRUE;
2258 d = NULL;
2259
2260 if (t->globals.list != NULL)
2261 d = (*t->match) (&t->globals, NULL, alc);
2262
2263 /* See if there is anything to force this symbol to
2264 local scope. */
2265 if (d == NULL && t->locals.list != NULL)
2266 {
2267 d = (*t->match) (&t->locals, NULL, alc);
2268 if (d != NULL
2269 && h->dynindx != -1
2270 && ! info->export_dynamic)
2271 *hide = TRUE;
2272 }
2273
2274 free (alc);
2275 break;
2276 }
2277 }
2278
2279 *t_p = t;
2280
2281 return TRUE;
2282}
2283
2284/* Return TRUE if the symbol H is hidden by version script. */
2285
2286bfd_boolean
2287_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2288 struct elf_link_hash_entry *h)
2289{
2290 const char *p;
2291 bfd_boolean hide = FALSE;
2292 const struct elf_backend_data *bed
2293 = get_elf_backend_data (info->output_bfd);
2294
2295 /* Version script only hides symbols defined in regular objects. */
2296 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2297 return TRUE;
2298
2299 p = strchr (h->root.root.string, ELF_VER_CHR);
2300 if (p != NULL && h->verinfo.vertree == NULL)
2301 {
2302 struct bfd_elf_version_tree *t;
2303
2304 ++p;
2305 if (*p == ELF_VER_CHR)
2306 ++p;
2307
2308 if (*p != '\0'
2309 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2310 && hide)
2311 {
2312 if (hide)
2313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2314 return TRUE;
2315 }
2316 }
2317
2318 /* If we don't have a version for this symbol, see if we can find
2319 something. */
2320 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2321 {
2322 h->verinfo.vertree
2323 = bfd_find_version_for_sym (info->version_info,
2324 h->root.root.string, &hide);
2325 if (h->verinfo.vertree != NULL && hide)
2326 {
2327 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2328 return TRUE;
2329 }
2330 }
2331
2332 return FALSE;
2333}
2334
45d6a902
AM
2335/* Figure out appropriate versions for all the symbols. We may not
2336 have the version number script until we have read all of the input
2337 files, so until that point we don't know which symbols should be
2338 local. This function is called via elf_link_hash_traverse. */
2339
28caa186 2340static bfd_boolean
268b6b39 2341_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2342{
28caa186 2343 struct elf_info_failed *sinfo;
45d6a902 2344 struct bfd_link_info *info;
9c5bfbb7 2345 const struct elf_backend_data *bed;
45d6a902
AM
2346 struct elf_info_failed eif;
2347 char *p;
099bb8fb 2348 bfd_boolean hide;
45d6a902 2349
a50b1753 2350 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2351 info = sinfo->info;
2352
45d6a902
AM
2353 /* Fix the symbol flags. */
2354 eif.failed = FALSE;
2355 eif.info = info;
2356 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2357 {
2358 if (eif.failed)
2359 sinfo->failed = TRUE;
2360 return FALSE;
2361 }
2362
2363 /* We only need version numbers for symbols defined in regular
2364 objects. */
f5385ebf 2365 if (!h->def_regular)
45d6a902
AM
2366 return TRUE;
2367
099bb8fb 2368 hide = FALSE;
28caa186 2369 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2370 p = strchr (h->root.root.string, ELF_VER_CHR);
2371 if (p != NULL && h->verinfo.vertree == NULL)
2372 {
2373 struct bfd_elf_version_tree *t;
45d6a902 2374
45d6a902
AM
2375 ++p;
2376 if (*p == ELF_VER_CHR)
6e33951e 2377 ++p;
45d6a902
AM
2378
2379 /* If there is no version string, we can just return out. */
2380 if (*p == '\0')
6e33951e 2381 return TRUE;
45d6a902 2382
099bb8fb 2383 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2384 {
099bb8fb
L
2385 sinfo->failed = TRUE;
2386 return FALSE;
45d6a902
AM
2387 }
2388
099bb8fb
L
2389 if (hide)
2390 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2391
45d6a902
AM
2392 /* If we are building an application, we need to create a
2393 version node for this version. */
0e1862bb 2394 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2395 {
2396 struct bfd_elf_version_tree **pp;
2397 int version_index;
2398
2399 /* If we aren't going to export this symbol, we don't need
2400 to worry about it. */
2401 if (h->dynindx == -1)
2402 return TRUE;
2403
ef53be89
AM
2404 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2405 sizeof *t);
45d6a902
AM
2406 if (t == NULL)
2407 {
2408 sinfo->failed = TRUE;
2409 return FALSE;
2410 }
2411
45d6a902 2412 t->name = p;
45d6a902
AM
2413 t->name_indx = (unsigned int) -1;
2414 t->used = TRUE;
2415
2416 version_index = 1;
2417 /* Don't count anonymous version tag. */
fd91d419
L
2418 if (sinfo->info->version_info != NULL
2419 && sinfo->info->version_info->vernum == 0)
45d6a902 2420 version_index = 0;
fd91d419
L
2421 for (pp = &sinfo->info->version_info;
2422 *pp != NULL;
2423 pp = &(*pp)->next)
45d6a902
AM
2424 ++version_index;
2425 t->vernum = version_index;
2426
2427 *pp = t;
2428
2429 h->verinfo.vertree = t;
2430 }
2431 else if (t == NULL)
2432 {
2433 /* We could not find the version for a symbol when
2434 generating a shared archive. Return an error. */
4eca0228 2435 _bfd_error_handler
695344c0 2436 /* xgettext:c-format */
871b3ab2 2437 (_("%pB: version node not found for symbol %s"),
28caa186 2438 info->output_bfd, h->root.root.string);
45d6a902
AM
2439 bfd_set_error (bfd_error_bad_value);
2440 sinfo->failed = TRUE;
2441 return FALSE;
2442 }
45d6a902
AM
2443 }
2444
2445 /* If we don't have a version for this symbol, see if we can find
2446 something. */
099bb8fb
L
2447 if (!hide
2448 && h->verinfo.vertree == NULL
2449 && sinfo->info->version_info != NULL)
45d6a902 2450 {
fd91d419
L
2451 h->verinfo.vertree
2452 = bfd_find_version_for_sym (sinfo->info->version_info,
2453 h->root.root.string, &hide);
1e8fa21e
AM
2454 if (h->verinfo.vertree != NULL && hide)
2455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2456 }
2457
2458 return TRUE;
2459}
2460\f
45d6a902
AM
2461/* Read and swap the relocs from the section indicated by SHDR. This
2462 may be either a REL or a RELA section. The relocations are
2463 translated into RELA relocations and stored in INTERNAL_RELOCS,
2464 which should have already been allocated to contain enough space.
2465 The EXTERNAL_RELOCS are a buffer where the external form of the
2466 relocations should be stored.
2467
2468 Returns FALSE if something goes wrong. */
2469
2470static bfd_boolean
268b6b39 2471elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2472 asection *sec,
268b6b39
AM
2473 Elf_Internal_Shdr *shdr,
2474 void *external_relocs,
2475 Elf_Internal_Rela *internal_relocs)
45d6a902 2476{
9c5bfbb7 2477 const struct elf_backend_data *bed;
268b6b39 2478 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2479 const bfd_byte *erela;
2480 const bfd_byte *erelaend;
2481 Elf_Internal_Rela *irela;
243ef1e0
L
2482 Elf_Internal_Shdr *symtab_hdr;
2483 size_t nsyms;
45d6a902 2484
45d6a902
AM
2485 /* Position ourselves at the start of the section. */
2486 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2487 return FALSE;
2488
2489 /* Read the relocations. */
2490 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2491 return FALSE;
2492
243ef1e0 2493 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2494 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2495
45d6a902
AM
2496 bed = get_elf_backend_data (abfd);
2497
2498 /* Convert the external relocations to the internal format. */
2499 if (shdr->sh_entsize == bed->s->sizeof_rel)
2500 swap_in = bed->s->swap_reloc_in;
2501 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2502 swap_in = bed->s->swap_reloca_in;
2503 else
2504 {
2505 bfd_set_error (bfd_error_wrong_format);
2506 return FALSE;
2507 }
2508
a50b1753 2509 erela = (const bfd_byte *) external_relocs;
51992aec 2510 erelaend = erela + shdr->sh_size;
45d6a902
AM
2511 irela = internal_relocs;
2512 while (erela < erelaend)
2513 {
243ef1e0
L
2514 bfd_vma r_symndx;
2515
45d6a902 2516 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2517 r_symndx = ELF32_R_SYM (irela->r_info);
2518 if (bed->s->arch_size == 64)
2519 r_symndx >>= 24;
ce98a316
NC
2520 if (nsyms > 0)
2521 {
2522 if ((size_t) r_symndx >= nsyms)
2523 {
4eca0228 2524 _bfd_error_handler
695344c0 2525 /* xgettext:c-format */
2dcf00ce
AM
2526 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2527 " for offset %#" PRIx64 " in section `%pA'"),
2528 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2529 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2530 bfd_set_error (bfd_error_bad_value);
2531 return FALSE;
2532 }
2533 }
cf35638d 2534 else if (r_symndx != STN_UNDEF)
243ef1e0 2535 {
4eca0228 2536 _bfd_error_handler
695344c0 2537 /* xgettext:c-format */
2dcf00ce
AM
2538 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2539 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2540 " when the object file has no symbol table"),
2dcf00ce
AM
2541 abfd, (uint64_t) r_symndx,
2542 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
45d6a902
AM
2546 irela += bed->s->int_rels_per_ext_rel;
2547 erela += shdr->sh_entsize;
2548 }
2549
2550 return TRUE;
2551}
2552
2553/* Read and swap the relocs for a section O. They may have been
2554 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2555 not NULL, they are used as buffers to read into. They are known to
2556 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2557 the return value is allocated using either malloc or bfd_alloc,
2558 according to the KEEP_MEMORY argument. If O has two relocation
2559 sections (both REL and RELA relocations), then the REL_HDR
2560 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2561 RELA_HDR relocations. */
45d6a902
AM
2562
2563Elf_Internal_Rela *
268b6b39
AM
2564_bfd_elf_link_read_relocs (bfd *abfd,
2565 asection *o,
2566 void *external_relocs,
2567 Elf_Internal_Rela *internal_relocs,
2568 bfd_boolean keep_memory)
45d6a902 2569{
268b6b39 2570 void *alloc1 = NULL;
45d6a902 2571 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2572 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2573 struct bfd_elf_section_data *esdo = elf_section_data (o);
2574 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2575
d4730f92
BS
2576 if (esdo->relocs != NULL)
2577 return esdo->relocs;
45d6a902
AM
2578
2579 if (o->reloc_count == 0)
2580 return NULL;
2581
45d6a902
AM
2582 if (internal_relocs == NULL)
2583 {
2584 bfd_size_type size;
2585
056bafd4 2586 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2587 if (keep_memory)
a50b1753 2588 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2589 else
a50b1753 2590 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2591 if (internal_relocs == NULL)
2592 goto error_return;
2593 }
2594
2595 if (external_relocs == NULL)
2596 {
d4730f92
BS
2597 bfd_size_type size = 0;
2598
2599 if (esdo->rel.hdr)
2600 size += esdo->rel.hdr->sh_size;
2601 if (esdo->rela.hdr)
2602 size += esdo->rela.hdr->sh_size;
45d6a902 2603
268b6b39 2604 alloc1 = bfd_malloc (size);
45d6a902
AM
2605 if (alloc1 == NULL)
2606 goto error_return;
2607 external_relocs = alloc1;
2608 }
2609
d4730f92
BS
2610 internal_rela_relocs = internal_relocs;
2611 if (esdo->rel.hdr)
2612 {
2613 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2614 external_relocs,
2615 internal_relocs))
2616 goto error_return;
2617 external_relocs = (((bfd_byte *) external_relocs)
2618 + esdo->rel.hdr->sh_size);
2619 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2620 * bed->s->int_rels_per_ext_rel);
2621 }
2622
2623 if (esdo->rela.hdr
2624 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2625 external_relocs,
2626 internal_rela_relocs)))
45d6a902
AM
2627 goto error_return;
2628
2629 /* Cache the results for next time, if we can. */
2630 if (keep_memory)
d4730f92 2631 esdo->relocs = internal_relocs;
45d6a902
AM
2632
2633 if (alloc1 != NULL)
2634 free (alloc1);
2635
2636 /* Don't free alloc2, since if it was allocated we are passing it
2637 back (under the name of internal_relocs). */
2638
2639 return internal_relocs;
2640
2641 error_return:
2642 if (alloc1 != NULL)
2643 free (alloc1);
2644 if (alloc2 != NULL)
4dd07732
AM
2645 {
2646 if (keep_memory)
2647 bfd_release (abfd, alloc2);
2648 else
2649 free (alloc2);
2650 }
45d6a902
AM
2651 return NULL;
2652}
2653
2654/* Compute the size of, and allocate space for, REL_HDR which is the
2655 section header for a section containing relocations for O. */
2656
28caa186 2657static bfd_boolean
9eaff861
AO
2658_bfd_elf_link_size_reloc_section (bfd *abfd,
2659 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2660{
9eaff861 2661 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2662
2663 /* That allows us to calculate the size of the section. */
9eaff861 2664 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2665
2666 /* The contents field must last into write_object_contents, so we
2667 allocate it with bfd_alloc rather than malloc. Also since we
2668 cannot be sure that the contents will actually be filled in,
2669 we zero the allocated space. */
a50b1753 2670 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2671 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2672 return FALSE;
2673
d4730f92 2674 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2675 {
2676 struct elf_link_hash_entry **p;
2677
ca4be51c
AM
2678 p = ((struct elf_link_hash_entry **)
2679 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2680 if (p == NULL)
2681 return FALSE;
2682
d4730f92 2683 reldata->hashes = p;
45d6a902
AM
2684 }
2685
2686 return TRUE;
2687}
2688
2689/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2690 originated from the section given by INPUT_REL_HDR) to the
2691 OUTPUT_BFD. */
2692
2693bfd_boolean
268b6b39
AM
2694_bfd_elf_link_output_relocs (bfd *output_bfd,
2695 asection *input_section,
2696 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2697 Elf_Internal_Rela *internal_relocs,
2698 struct elf_link_hash_entry **rel_hash
2699 ATTRIBUTE_UNUSED)
45d6a902
AM
2700{
2701 Elf_Internal_Rela *irela;
2702 Elf_Internal_Rela *irelaend;
2703 bfd_byte *erel;
d4730f92 2704 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2705 asection *output_section;
9c5bfbb7 2706 const struct elf_backend_data *bed;
268b6b39 2707 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2708 struct bfd_elf_section_data *esdo;
45d6a902
AM
2709
2710 output_section = input_section->output_section;
45d6a902 2711
d4730f92
BS
2712 bed = get_elf_backend_data (output_bfd);
2713 esdo = elf_section_data (output_section);
2714 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2715 {
d4730f92
BS
2716 output_reldata = &esdo->rel;
2717 swap_out = bed->s->swap_reloc_out;
45d6a902 2718 }
d4730f92
BS
2719 else if (esdo->rela.hdr
2720 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2721 {
d4730f92
BS
2722 output_reldata = &esdo->rela;
2723 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2724 }
2725 else
2726 {
4eca0228 2727 _bfd_error_handler
695344c0 2728 /* xgettext:c-format */
871b3ab2 2729 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2730 output_bfd, input_section->owner, input_section);
297d8443 2731 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2732 return FALSE;
2733 }
2734
d4730f92
BS
2735 erel = output_reldata->hdr->contents;
2736 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2737 irela = internal_relocs;
2738 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2739 * bed->s->int_rels_per_ext_rel);
2740 while (irela < irelaend)
2741 {
2742 (*swap_out) (output_bfd, irela, erel);
2743 irela += bed->s->int_rels_per_ext_rel;
2744 erel += input_rel_hdr->sh_entsize;
2745 }
2746
2747 /* Bump the counter, so that we know where to add the next set of
2748 relocations. */
d4730f92 2749 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2750
2751 return TRUE;
2752}
2753\f
508c3946
L
2754/* Make weak undefined symbols in PIE dynamic. */
2755
2756bfd_boolean
2757_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2758 struct elf_link_hash_entry *h)
2759{
0e1862bb 2760 if (bfd_link_pie (info)
508c3946
L
2761 && h->dynindx == -1
2762 && h->root.type == bfd_link_hash_undefweak)
2763 return bfd_elf_link_record_dynamic_symbol (info, h);
2764
2765 return TRUE;
2766}
2767
45d6a902
AM
2768/* Fix up the flags for a symbol. This handles various cases which
2769 can only be fixed after all the input files are seen. This is
2770 currently called by both adjust_dynamic_symbol and
2771 assign_sym_version, which is unnecessary but perhaps more robust in
2772 the face of future changes. */
2773
28caa186 2774static bfd_boolean
268b6b39
AM
2775_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2776 struct elf_info_failed *eif)
45d6a902 2777{
33774f08 2778 const struct elf_backend_data *bed;
508c3946 2779
45d6a902
AM
2780 /* If this symbol was mentioned in a non-ELF file, try to set
2781 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2782 permit a non-ELF file to correctly refer to a symbol defined in
2783 an ELF dynamic object. */
f5385ebf 2784 if (h->non_elf)
45d6a902
AM
2785 {
2786 while (h->root.type == bfd_link_hash_indirect)
2787 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2788
2789 if (h->root.type != bfd_link_hash_defined
2790 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2791 {
2792 h->ref_regular = 1;
2793 h->ref_regular_nonweak = 1;
2794 }
45d6a902
AM
2795 else
2796 {
2797 if (h->root.u.def.section->owner != NULL
2798 && (bfd_get_flavour (h->root.u.def.section->owner)
2799 == bfd_target_elf_flavour))
f5385ebf
AM
2800 {
2801 h->ref_regular = 1;
2802 h->ref_regular_nonweak = 1;
2803 }
45d6a902 2804 else
f5385ebf 2805 h->def_regular = 1;
45d6a902
AM
2806 }
2807
2808 if (h->dynindx == -1
f5385ebf
AM
2809 && (h->def_dynamic
2810 || h->ref_dynamic))
45d6a902 2811 {
c152c796 2812 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2813 {
2814 eif->failed = TRUE;
2815 return FALSE;
2816 }
2817 }
2818 }
2819 else
2820 {
f5385ebf 2821 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2822 was first seen in a non-ELF file. Fortunately, if the symbol
2823 was first seen in an ELF file, we're probably OK unless the
2824 symbol was defined in a non-ELF file. Catch that case here.
2825 FIXME: We're still in trouble if the symbol was first seen in
2826 a dynamic object, and then later in a non-ELF regular object. */
2827 if ((h->root.type == bfd_link_hash_defined
2828 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2829 && !h->def_regular
45d6a902
AM
2830 && (h->root.u.def.section->owner != NULL
2831 ? (bfd_get_flavour (h->root.u.def.section->owner)
2832 != bfd_target_elf_flavour)
2833 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2834 && !h->def_dynamic)))
2835 h->def_regular = 1;
45d6a902
AM
2836 }
2837
508c3946 2838 /* Backend specific symbol fixup. */
33774f08
AM
2839 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2840 if (bed->elf_backend_fixup_symbol
2841 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2842 return FALSE;
508c3946 2843
45d6a902
AM
2844 /* If this is a final link, and the symbol was defined as a common
2845 symbol in a regular object file, and there was no definition in
2846 any dynamic object, then the linker will have allocated space for
f5385ebf 2847 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2848 flag will not have been set. */
2849 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2850 && !h->def_regular
2851 && h->ref_regular
2852 && !h->def_dynamic
96f29d96 2853 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2854 h->def_regular = 1;
45d6a902 2855
4deb8f71
L
2856 /* If a weak undefined symbol has non-default visibility, we also
2857 hide it from the dynamic linker. */
2858 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2859 && h->root.type == bfd_link_hash_undefweak)
2860 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2861
2862 /* A hidden versioned symbol in executable should be forced local if
2863 it is is locally defined, not referenced by shared library and not
2864 exported. */
2865 else if (bfd_link_executable (eif->info)
2866 && h->versioned == versioned_hidden
2867 && !eif->info->export_dynamic
2868 && !h->dynamic
2869 && !h->ref_dynamic
2870 && h->def_regular)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
45d6a902
AM
2873 /* If -Bsymbolic was used (which means to bind references to global
2874 symbols to the definition within the shared object), and this
2875 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2876 need a PLT entry. Likewise, if the symbol has non-default
2877 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2878 will force it local. */
4deb8f71
L
2879 else if (h->needs_plt
2880 && bfd_link_pic (eif->info)
2881 && is_elf_hash_table (eif->info->hash)
2882 && (SYMBOLIC_BIND (eif->info, h)
2883 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2884 && h->def_regular)
45d6a902 2885 {
45d6a902
AM
2886 bfd_boolean force_local;
2887
45d6a902
AM
2888 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2889 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2890 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2891 }
2892
45d6a902
AM
2893 /* If this is a weak defined symbol in a dynamic object, and we know
2894 the real definition in the dynamic object, copy interesting flags
2895 over to the real definition. */
60d67dc8 2896 if (h->is_weakalias)
45d6a902 2897 {
60d67dc8
AM
2898 struct elf_link_hash_entry *def = weakdef (h);
2899
45d6a902
AM
2900 /* If the real definition is defined by a regular object file,
2901 don't do anything special. See the longer description in
2902 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2903 if (def->def_regular)
2904 {
2905 h = def;
2906 while ((h = h->u.alias) != def)
2907 h->is_weakalias = 0;
2908 }
45d6a902 2909 else
a26587ba 2910 {
4e6b54a6
AM
2911 while (h->root.type == bfd_link_hash_indirect)
2912 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2913 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2914 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2915 BFD_ASSERT (def->def_dynamic);
2916 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2917 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2918 }
45d6a902
AM
2919 }
2920
2921 return TRUE;
2922}
2923
2924/* Make the backend pick a good value for a dynamic symbol. This is
2925 called via elf_link_hash_traverse, and also calls itself
2926 recursively. */
2927
28caa186 2928static bfd_boolean
268b6b39 2929_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2930{
a50b1753 2931 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2932 struct elf_link_hash_table *htab;
9c5bfbb7 2933 const struct elf_backend_data *bed;
45d6a902 2934
0eddce27 2935 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2936 return FALSE;
2937
45d6a902
AM
2938 /* Ignore indirect symbols. These are added by the versioning code. */
2939 if (h->root.type == bfd_link_hash_indirect)
2940 return TRUE;
2941
2942 /* Fix the symbol flags. */
2943 if (! _bfd_elf_fix_symbol_flags (h, eif))
2944 return FALSE;
2945
559192d8
AM
2946 htab = elf_hash_table (eif->info);
2947 bed = get_elf_backend_data (htab->dynobj);
2948
954b63d4
AM
2949 if (h->root.type == bfd_link_hash_undefweak)
2950 {
2951 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2952 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2953 else if (eif->info->dynamic_undefined_weak > 0
2954 && h->ref_regular
2955 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2956 && !bfd_hide_sym_by_version (eif->info->version_info,
2957 h->root.root.string))
2958 {
2959 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2960 {
2961 eif->failed = TRUE;
2962 return FALSE;
2963 }
2964 }
2965 }
2966
45d6a902
AM
2967 /* If this symbol does not require a PLT entry, and it is not
2968 defined by a dynamic object, or is not referenced by a regular
2969 object, ignore it. We do have to handle a weak defined symbol,
2970 even if no regular object refers to it, if we decided to add it
2971 to the dynamic symbol table. FIXME: Do we normally need to worry
2972 about symbols which are defined by one dynamic object and
2973 referenced by another one? */
f5385ebf 2974 if (!h->needs_plt
91e21fb7 2975 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2976 && (h->def_regular
2977 || !h->def_dynamic
2978 || (!h->ref_regular
60d67dc8 2979 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 2980 {
a6aa5195 2981 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2982 return TRUE;
2983 }
2984
2985 /* If we've already adjusted this symbol, don't do it again. This
2986 can happen via a recursive call. */
f5385ebf 2987 if (h->dynamic_adjusted)
45d6a902
AM
2988 return TRUE;
2989
2990 /* Don't look at this symbol again. Note that we must set this
2991 after checking the above conditions, because we may look at a
2992 symbol once, decide not to do anything, and then get called
2993 recursively later after REF_REGULAR is set below. */
f5385ebf 2994 h->dynamic_adjusted = 1;
45d6a902
AM
2995
2996 /* If this is a weak definition, and we know a real definition, and
2997 the real symbol is not itself defined by a regular object file,
2998 then get a good value for the real definition. We handle the
2999 real symbol first, for the convenience of the backend routine.
3000
3001 Note that there is a confusing case here. If the real definition
3002 is defined by a regular object file, we don't get the real symbol
3003 from the dynamic object, but we do get the weak symbol. If the
3004 processor backend uses a COPY reloc, then if some routine in the
3005 dynamic object changes the real symbol, we will not see that
3006 change in the corresponding weak symbol. This is the way other
3007 ELF linkers work as well, and seems to be a result of the shared
3008 library model.
3009
3010 I will clarify this issue. Most SVR4 shared libraries define the
3011 variable _timezone and define timezone as a weak synonym. The
3012 tzset call changes _timezone. If you write
3013 extern int timezone;
3014 int _timezone = 5;
3015 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3016 you might expect that, since timezone is a synonym for _timezone,
3017 the same number will print both times. However, if the processor
3018 backend uses a COPY reloc, then actually timezone will be copied
3019 into your process image, and, since you define _timezone
3020 yourself, _timezone will not. Thus timezone and _timezone will
3021 wind up at different memory locations. The tzset call will set
3022 _timezone, leaving timezone unchanged. */
3023
60d67dc8 3024 if (h->is_weakalias)
45d6a902 3025 {
60d67dc8
AM
3026 struct elf_link_hash_entry *def = weakdef (h);
3027
ec24dc88 3028 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3029 the alias by a regular object file via the weak symbol H. */
3030 def->ref_regular = 1;
45d6a902 3031
ec24dc88 3032 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3033 the strong alias before H by recursively calling ourselves. */
3034 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3035 return FALSE;
3036 }
3037
3038 /* If a symbol has no type and no size and does not require a PLT
3039 entry, then we are probably about to do the wrong thing here: we
3040 are probably going to create a COPY reloc for an empty object.
3041 This case can arise when a shared object is built with assembly
3042 code, and the assembly code fails to set the symbol type. */
3043 if (h->size == 0
3044 && h->type == STT_NOTYPE
f5385ebf 3045 && !h->needs_plt)
4eca0228 3046 _bfd_error_handler
45d6a902
AM
3047 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3048 h->root.root.string);
3049
45d6a902
AM
3050 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3051 {
3052 eif->failed = TRUE;
3053 return FALSE;
3054 }
3055
3056 return TRUE;
3057}
3058
027297b7
L
3059/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3060 DYNBSS. */
3061
3062bfd_boolean
6cabe1ea
AM
3063_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3064 struct elf_link_hash_entry *h,
027297b7
L
3065 asection *dynbss)
3066{
91ac5911 3067 unsigned int power_of_two;
027297b7
L
3068 bfd_vma mask;
3069 asection *sec = h->root.u.def.section;
3070
de194d85 3071 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3072 requirement of symbols defined in the section. Since we don't
3073 know the symbol alignment requirement, we start with the
3074 maximum alignment and check low bits of the symbol address
3075 for the minimum alignment. */
3076 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3077 mask = ((bfd_vma) 1 << power_of_two) - 1;
3078 while ((h->root.u.def.value & mask) != 0)
3079 {
3080 mask >>= 1;
3081 --power_of_two;
3082 }
027297b7 3083
91ac5911
L
3084 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3085 dynbss))
027297b7
L
3086 {
3087 /* Adjust the section alignment if needed. */
3088 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3089 power_of_two))
027297b7
L
3090 return FALSE;
3091 }
3092
91ac5911 3093 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3094 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3095
3096 /* Define the symbol as being at this point in DYNBSS. */
3097 h->root.u.def.section = dynbss;
3098 h->root.u.def.value = dynbss->size;
3099
3100 /* Increment the size of DYNBSS to make room for the symbol. */
3101 dynbss->size += h->size;
3102
f7483970
L
3103 /* No error if extern_protected_data is true. */
3104 if (h->protected_def
889c2a67
L
3105 && (!info->extern_protected_data
3106 || (info->extern_protected_data < 0
3107 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3108 info->callbacks->einfo
c1c8c1ef 3109 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3110 h->root.root.string);
6cabe1ea 3111
027297b7
L
3112 return TRUE;
3113}
3114
45d6a902
AM
3115/* Adjust all external symbols pointing into SEC_MERGE sections
3116 to reflect the object merging within the sections. */
3117
28caa186 3118static bfd_boolean
268b6b39 3119_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3120{
3121 asection *sec;
3122
45d6a902
AM
3123 if ((h->root.type == bfd_link_hash_defined
3124 || h->root.type == bfd_link_hash_defweak)
3125 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3126 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3127 {
a50b1753 3128 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3129
3130 h->root.u.def.value =
3131 _bfd_merged_section_offset (output_bfd,
3132 &h->root.u.def.section,
3133 elf_section_data (sec)->sec_info,
753731ee 3134 h->root.u.def.value);
45d6a902
AM
3135 }
3136
3137 return TRUE;
3138}
986a241f
RH
3139
3140/* Returns false if the symbol referred to by H should be considered
3141 to resolve local to the current module, and true if it should be
3142 considered to bind dynamically. */
3143
3144bfd_boolean
268b6b39
AM
3145_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3146 struct bfd_link_info *info,
89a2ee5a 3147 bfd_boolean not_local_protected)
986a241f
RH
3148{
3149 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3150 const struct elf_backend_data *bed;
3151 struct elf_link_hash_table *hash_table;
986a241f
RH
3152
3153 if (h == NULL)
3154 return FALSE;
3155
3156 while (h->root.type == bfd_link_hash_indirect
3157 || h->root.type == bfd_link_hash_warning)
3158 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3159
3160 /* If it was forced local, then clearly it's not dynamic. */
3161 if (h->dynindx == -1)
3162 return FALSE;
f5385ebf 3163 if (h->forced_local)
986a241f
RH
3164 return FALSE;
3165
3166 /* Identify the cases where name binding rules say that a
3167 visible symbol resolves locally. */
0e1862bb
L
3168 binding_stays_local_p = (bfd_link_executable (info)
3169 || SYMBOLIC_BIND (info, h));
986a241f
RH
3170
3171 switch (ELF_ST_VISIBILITY (h->other))
3172 {
3173 case STV_INTERNAL:
3174 case STV_HIDDEN:
3175 return FALSE;
3176
3177 case STV_PROTECTED:
fcb93ecf
PB
3178 hash_table = elf_hash_table (info);
3179 if (!is_elf_hash_table (hash_table))
3180 return FALSE;
3181
3182 bed = get_elf_backend_data (hash_table->dynobj);
3183
986a241f
RH
3184 /* Proper resolution for function pointer equality may require
3185 that these symbols perhaps be resolved dynamically, even though
3186 we should be resolving them to the current module. */
89a2ee5a 3187 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3188 binding_stays_local_p = TRUE;
3189 break;
3190
3191 default:
986a241f
RH
3192 break;
3193 }
3194
aa37626c 3195 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3196 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3197 return TRUE;
3198
986a241f
RH
3199 /* Otherwise, the symbol is dynamic if binding rules don't tell
3200 us that it remains local. */
3201 return !binding_stays_local_p;
3202}
f6c52c13
AM
3203
3204/* Return true if the symbol referred to by H should be considered
3205 to resolve local to the current module, and false otherwise. Differs
3206 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3207 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3208 for the place where dynindx == -1 is tested. If that test is true,
3209 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3210 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3211 defined symbols.
89a2ee5a
AM
3212 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3213 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3214 treatment of undefined weak symbols. For those that do not make
3215 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3216
3217bfd_boolean
268b6b39
AM
3218_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3219 struct bfd_link_info *info,
3220 bfd_boolean local_protected)
f6c52c13 3221{
fcb93ecf
PB
3222 const struct elf_backend_data *bed;
3223 struct elf_link_hash_table *hash_table;
3224
f6c52c13
AM
3225 /* If it's a local sym, of course we resolve locally. */
3226 if (h == NULL)
3227 return TRUE;
3228
d95edcac
L
3229 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3230 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3231 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3232 return TRUE;
3233
0fad2956
MR
3234 /* Forced local symbols resolve locally. */
3235 if (h->forced_local)
3236 return TRUE;
3237
7e2294f9
AO
3238 /* Common symbols that become definitions don't get the DEF_REGULAR
3239 flag set, so test it first, and don't bail out. */
3240 if (ELF_COMMON_DEF_P (h))
3241 /* Do nothing. */;
f6c52c13 3242 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3243 resolve locally. The sym is either undefined or dynamic. */
3244 else if (!h->def_regular)
f6c52c13
AM
3245 return FALSE;
3246
0fad2956 3247 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3248 if (h->dynindx == -1)
3249 return TRUE;
3250
3251 /* At this point, we know the symbol is defined and dynamic. In an
3252 executable it must resolve locally, likewise when building symbolic
3253 shared libraries. */
0e1862bb 3254 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3255 return TRUE;
3256
3257 /* Now deal with defined dynamic symbols in shared libraries. Ones
3258 with default visibility might not resolve locally. */
3259 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3260 return FALSE;
3261
fcb93ecf
PB
3262 hash_table = elf_hash_table (info);
3263 if (!is_elf_hash_table (hash_table))
3264 return TRUE;
3265
3266 bed = get_elf_backend_data (hash_table->dynobj);
3267
f7483970
L
3268 /* If extern_protected_data is false, STV_PROTECTED non-function
3269 symbols are local. */
889c2a67
L
3270 if ((!info->extern_protected_data
3271 || (info->extern_protected_data < 0
3272 && !bed->extern_protected_data))
3273 && !bed->is_function_type (h->type))
1c16dfa5
L
3274 return TRUE;
3275
f6c52c13 3276 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3277 symbols be treated as dynamic symbols. If the address of a
3278 function not defined in an executable is set to that function's
3279 plt entry in the executable, then the address of the function in
3280 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3281 return local_protected;
3282}
e1918d23
AM
3283
3284/* Caches some TLS segment info, and ensures that the TLS segment vma is
3285 aligned. Returns the first TLS output section. */
3286
3287struct bfd_section *
3288_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3289{
3290 struct bfd_section *sec, *tls;
3291 unsigned int align = 0;
3292
3293 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3294 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3295 break;
3296 tls = sec;
3297
3298 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3299 if (sec->alignment_power > align)
3300 align = sec->alignment_power;
3301
3302 elf_hash_table (info)->tls_sec = tls;
3303
3304 /* Ensure the alignment of the first section is the largest alignment,
3305 so that the tls segment starts aligned. */
3306 if (tls != NULL)
3307 tls->alignment_power = align;
3308
3309 return tls;
3310}
0ad989f9
L
3311
3312/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3313static bfd_boolean
3314is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3315 Elf_Internal_Sym *sym)
3316{
a4d8e49b
L
3317 const struct elf_backend_data *bed;
3318
0ad989f9
L
3319 /* Local symbols do not count, but target specific ones might. */
3320 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3321 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3322 return FALSE;
3323
fcb93ecf 3324 bed = get_elf_backend_data (abfd);
0ad989f9 3325 /* Function symbols do not count. */
fcb93ecf 3326 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3327 return FALSE;
3328
3329 /* If the section is undefined, then so is the symbol. */
3330 if (sym->st_shndx == SHN_UNDEF)
3331 return FALSE;
3332
3333 /* If the symbol is defined in the common section, then
3334 it is a common definition and so does not count. */
a4d8e49b 3335 if (bed->common_definition (sym))
0ad989f9
L
3336 return FALSE;
3337
3338 /* If the symbol is in a target specific section then we
3339 must rely upon the backend to tell us what it is. */
3340 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3341 /* FIXME - this function is not coded yet:
3342
3343 return _bfd_is_global_symbol_definition (abfd, sym);
3344
3345 Instead for now assume that the definition is not global,
3346 Even if this is wrong, at least the linker will behave
3347 in the same way that it used to do. */
3348 return FALSE;
3349
3350 return TRUE;
3351}
3352
3353/* Search the symbol table of the archive element of the archive ABFD
3354 whose archive map contains a mention of SYMDEF, and determine if
3355 the symbol is defined in this element. */
3356static bfd_boolean
3357elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3358{
3359 Elf_Internal_Shdr * hdr;
ef53be89
AM
3360 size_t symcount;
3361 size_t extsymcount;
3362 size_t extsymoff;
0ad989f9
L
3363 Elf_Internal_Sym *isymbuf;
3364 Elf_Internal_Sym *isym;
3365 Elf_Internal_Sym *isymend;
3366 bfd_boolean result;
3367
3368 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3369 if (abfd == NULL)
3370 return FALSE;
3371
3372 if (! bfd_check_format (abfd, bfd_object))
3373 return FALSE;
3374
7dc3990e
L
3375 /* Select the appropriate symbol table. If we don't know if the
3376 object file is an IR object, give linker LTO plugin a chance to
3377 get the correct symbol table. */
3378 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3379#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3380 || (abfd->plugin_format == bfd_plugin_unknown
3381 && bfd_link_plugin_object_p (abfd))
3382#endif
3383 )
3384 {
3385 /* Use the IR symbol table if the object has been claimed by
3386 plugin. */
3387 abfd = abfd->plugin_dummy_bfd;
3388 hdr = &elf_tdata (abfd)->symtab_hdr;
3389 }
3390 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3391 hdr = &elf_tdata (abfd)->symtab_hdr;
3392 else
3393 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3394
3395 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3396
3397 /* The sh_info field of the symtab header tells us where the
3398 external symbols start. We don't care about the local symbols. */
3399 if (elf_bad_symtab (abfd))
3400 {
3401 extsymcount = symcount;
3402 extsymoff = 0;
3403 }
3404 else
3405 {
3406 extsymcount = symcount - hdr->sh_info;
3407 extsymoff = hdr->sh_info;
3408 }
3409
3410 if (extsymcount == 0)
3411 return FALSE;
3412
3413 /* Read in the symbol table. */
3414 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3415 NULL, NULL, NULL);
3416 if (isymbuf == NULL)
3417 return FALSE;
3418
3419 /* Scan the symbol table looking for SYMDEF. */
3420 result = FALSE;
3421 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3422 {
3423 const char *name;
3424
3425 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3426 isym->st_name);
3427 if (name == NULL)
3428 break;
3429
3430 if (strcmp (name, symdef->name) == 0)
3431 {
3432 result = is_global_data_symbol_definition (abfd, isym);
3433 break;
3434 }
3435 }
3436
3437 free (isymbuf);
3438
3439 return result;
3440}
3441\f
5a580b3a
AM
3442/* Add an entry to the .dynamic table. */
3443
3444bfd_boolean
3445_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3446 bfd_vma tag,
3447 bfd_vma val)
3448{
3449 struct elf_link_hash_table *hash_table;
3450 const struct elf_backend_data *bed;
3451 asection *s;
3452 bfd_size_type newsize;
3453 bfd_byte *newcontents;
3454 Elf_Internal_Dyn dyn;
3455
3456 hash_table = elf_hash_table (info);
3457 if (! is_elf_hash_table (hash_table))
3458 return FALSE;
3459
7f923b7f
AM
3460 if (tag == DT_RELA || tag == DT_REL)
3461 hash_table->dynamic_relocs = TRUE;
3462
5a580b3a 3463 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3464 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3465 BFD_ASSERT (s != NULL);
3466
eea6121a 3467 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3468 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3469 if (newcontents == NULL)
3470 return FALSE;
3471
3472 dyn.d_tag = tag;
3473 dyn.d_un.d_val = val;
eea6121a 3474 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3475
eea6121a 3476 s->size = newsize;
5a580b3a
AM
3477 s->contents = newcontents;
3478
3479 return TRUE;
3480}
3481
3482/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3483 otherwise just check whether one already exists. Returns -1 on error,
3484 1 if a DT_NEEDED tag already exists, and 0 on success. */
3485
4ad4eba5 3486static int
7e9f0867
AM
3487elf_add_dt_needed_tag (bfd *abfd,
3488 struct bfd_link_info *info,
4ad4eba5
AM
3489 const char *soname,
3490 bfd_boolean do_it)
5a580b3a
AM
3491{
3492 struct elf_link_hash_table *hash_table;
ef53be89 3493 size_t strindex;
5a580b3a 3494
7e9f0867
AM
3495 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3496 return -1;
3497
5a580b3a 3498 hash_table = elf_hash_table (info);
5a580b3a 3499 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3500 if (strindex == (size_t) -1)
5a580b3a
AM
3501 return -1;
3502
02be4619 3503 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3504 {
3505 asection *sdyn;
3506 const struct elf_backend_data *bed;
3507 bfd_byte *extdyn;
3508
3509 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3510 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3511 if (sdyn != NULL)
3512 for (extdyn = sdyn->contents;
3513 extdyn < sdyn->contents + sdyn->size;
3514 extdyn += bed->s->sizeof_dyn)
3515 {
3516 Elf_Internal_Dyn dyn;
5a580b3a 3517
7e9f0867
AM
3518 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3519 if (dyn.d_tag == DT_NEEDED
3520 && dyn.d_un.d_val == strindex)
3521 {
3522 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3523 return 1;
3524 }
3525 }
5a580b3a
AM
3526 }
3527
3528 if (do_it)
3529 {
7e9f0867
AM
3530 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3531 return -1;
3532
5a580b3a
AM
3533 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3534 return -1;
3535 }
3536 else
3537 /* We were just checking for existence of the tag. */
3538 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3539
3540 return 0;
3541}
3542
7b15fa7a
AM
3543/* Return true if SONAME is on the needed list between NEEDED and STOP
3544 (or the end of list if STOP is NULL), and needed by a library that
3545 will be loaded. */
3546
010e5ae2 3547static bfd_boolean
7b15fa7a
AM
3548on_needed_list (const char *soname,
3549 struct bfd_link_needed_list *needed,
3550 struct bfd_link_needed_list *stop)
010e5ae2 3551{
7b15fa7a
AM
3552 struct bfd_link_needed_list *look;
3553 for (look = needed; look != stop; look = look->next)
3554 if (strcmp (soname, look->name) == 0
3555 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3556 /* If needed by a library that itself is not directly
3557 needed, recursively check whether that library is
3558 indirectly needed. Since we add DT_NEEDED entries to
3559 the end of the list, library dependencies appear after
3560 the library. Therefore search prior to the current
3561 LOOK, preventing possible infinite recursion. */
3562 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3563 return TRUE;
3564
3565 return FALSE;
3566}
3567
14160578 3568/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3569static int
3570elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3571{
3572 const struct elf_link_hash_entry *h1;
3573 const struct elf_link_hash_entry *h2;
10b7e05b 3574 bfd_signed_vma vdiff;
5a580b3a
AM
3575
3576 h1 = *(const struct elf_link_hash_entry **) arg1;
3577 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3578 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3579 if (vdiff != 0)
3580 return vdiff > 0 ? 1 : -1;
3581 else
3582 {
d3435ae8 3583 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3584 if (sdiff != 0)
3585 return sdiff > 0 ? 1 : -1;
3586 }
14160578
AM
3587 vdiff = h1->size - h2->size;
3588 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3589}
4ad4eba5 3590
5a580b3a
AM
3591/* This function is used to adjust offsets into .dynstr for
3592 dynamic symbols. This is called via elf_link_hash_traverse. */
3593
3594static bfd_boolean
3595elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3596{
a50b1753 3597 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3598
5a580b3a
AM
3599 if (h->dynindx != -1)
3600 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3601 return TRUE;
3602}
3603
3604/* Assign string offsets in .dynstr, update all structures referencing
3605 them. */
3606
4ad4eba5
AM
3607static bfd_boolean
3608elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3609{
3610 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3611 struct elf_link_local_dynamic_entry *entry;
3612 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3613 bfd *dynobj = hash_table->dynobj;
3614 asection *sdyn;
3615 bfd_size_type size;
3616 const struct elf_backend_data *bed;
3617 bfd_byte *extdyn;
3618
3619 _bfd_elf_strtab_finalize (dynstr);
3620 size = _bfd_elf_strtab_size (dynstr);
3621
3622 bed = get_elf_backend_data (dynobj);
3d4d4302 3623 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3624 BFD_ASSERT (sdyn != NULL);
3625
3626 /* Update all .dynamic entries referencing .dynstr strings. */
3627 for (extdyn = sdyn->contents;
eea6121a 3628 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3629 extdyn += bed->s->sizeof_dyn)
3630 {
3631 Elf_Internal_Dyn dyn;
3632
3633 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3634 switch (dyn.d_tag)
3635 {
3636 case DT_STRSZ:
3637 dyn.d_un.d_val = size;
3638 break;
3639 case DT_NEEDED:
3640 case DT_SONAME:
3641 case DT_RPATH:
3642 case DT_RUNPATH:
3643 case DT_FILTER:
3644 case DT_AUXILIARY:
7ee314fa
AM
3645 case DT_AUDIT:
3646 case DT_DEPAUDIT:
5a580b3a
AM
3647 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3648 break;
3649 default:
3650 continue;
3651 }
3652 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3653 }
3654
3655 /* Now update local dynamic symbols. */
3656 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3657 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3658 entry->isym.st_name);
3659
3660 /* And the rest of dynamic symbols. */
3661 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3662
3663 /* Adjust version definitions. */
3664 if (elf_tdata (output_bfd)->cverdefs)
3665 {
3666 asection *s;
3667 bfd_byte *p;
ef53be89 3668 size_t i;
5a580b3a
AM
3669 Elf_Internal_Verdef def;
3670 Elf_Internal_Verdaux defaux;
3671
3d4d4302 3672 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3673 p = s->contents;
3674 do
3675 {
3676 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3677 &def);
3678 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3679 if (def.vd_aux != sizeof (Elf_External_Verdef))
3680 continue;
5a580b3a
AM
3681 for (i = 0; i < def.vd_cnt; ++i)
3682 {
3683 _bfd_elf_swap_verdaux_in (output_bfd,
3684 (Elf_External_Verdaux *) p, &defaux);
3685 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3686 defaux.vda_name);
3687 _bfd_elf_swap_verdaux_out (output_bfd,
3688 &defaux, (Elf_External_Verdaux *) p);
3689 p += sizeof (Elf_External_Verdaux);
3690 }
3691 }
3692 while (def.vd_next);
3693 }
3694
3695 /* Adjust version references. */
3696 if (elf_tdata (output_bfd)->verref)
3697 {
3698 asection *s;
3699 bfd_byte *p;
ef53be89 3700 size_t i;
5a580b3a
AM
3701 Elf_Internal_Verneed need;
3702 Elf_Internal_Vernaux needaux;
3703
3d4d4302 3704 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3705 p = s->contents;
3706 do
3707 {
3708 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3709 &need);
3710 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3711 _bfd_elf_swap_verneed_out (output_bfd, &need,
3712 (Elf_External_Verneed *) p);
3713 p += sizeof (Elf_External_Verneed);
3714 for (i = 0; i < need.vn_cnt; ++i)
3715 {
3716 _bfd_elf_swap_vernaux_in (output_bfd,
3717 (Elf_External_Vernaux *) p, &needaux);
3718 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3719 needaux.vna_name);
3720 _bfd_elf_swap_vernaux_out (output_bfd,
3721 &needaux,
3722 (Elf_External_Vernaux *) p);
3723 p += sizeof (Elf_External_Vernaux);
3724 }
3725 }
3726 while (need.vn_next);
3727 }
3728
3729 return TRUE;
3730}
3731\f
13285a1b
AM
3732/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3733 The default is to only match when the INPUT and OUTPUT are exactly
3734 the same target. */
3735
3736bfd_boolean
3737_bfd_elf_default_relocs_compatible (const bfd_target *input,
3738 const bfd_target *output)
3739{
3740 return input == output;
3741}
3742
3743/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3744 This version is used when different targets for the same architecture
3745 are virtually identical. */
3746
3747bfd_boolean
3748_bfd_elf_relocs_compatible (const bfd_target *input,
3749 const bfd_target *output)
3750{
3751 const struct elf_backend_data *obed, *ibed;
3752
3753 if (input == output)
3754 return TRUE;
3755
3756 ibed = xvec_get_elf_backend_data (input);
3757 obed = xvec_get_elf_backend_data (output);
3758
3759 if (ibed->arch != obed->arch)
3760 return FALSE;
3761
3762 /* If both backends are using this function, deem them compatible. */
3763 return ibed->relocs_compatible == obed->relocs_compatible;
3764}
3765
e5034e59
AM
3766/* Make a special call to the linker "notice" function to tell it that
3767 we are about to handle an as-needed lib, or have finished
1b786873 3768 processing the lib. */
e5034e59
AM
3769
3770bfd_boolean
3771_bfd_elf_notice_as_needed (bfd *ibfd,
3772 struct bfd_link_info *info,
3773 enum notice_asneeded_action act)
3774{
46135103 3775 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3776}
3777
d9689752
L
3778/* Check relocations an ELF object file. */
3779
3780bfd_boolean
3781_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3782{
3783 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3784 struct elf_link_hash_table *htab = elf_hash_table (info);
3785
3786 /* If this object is the same format as the output object, and it is
3787 not a shared library, then let the backend look through the
3788 relocs.
3789
3790 This is required to build global offset table entries and to
3791 arrange for dynamic relocs. It is not required for the
3792 particular common case of linking non PIC code, even when linking
3793 against shared libraries, but unfortunately there is no way of
3794 knowing whether an object file has been compiled PIC or not.
3795 Looking through the relocs is not particularly time consuming.
3796 The problem is that we must either (1) keep the relocs in memory,
3797 which causes the linker to require additional runtime memory or
3798 (2) read the relocs twice from the input file, which wastes time.
3799 This would be a good case for using mmap.
3800
3801 I have no idea how to handle linking PIC code into a file of a
3802 different format. It probably can't be done. */
3803 if ((abfd->flags & DYNAMIC) == 0
3804 && is_elf_hash_table (htab)
3805 && bed->check_relocs != NULL
3806 && elf_object_id (abfd) == elf_hash_table_id (htab)
3807 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3808 {
3809 asection *o;
3810
3811 for (o = abfd->sections; o != NULL; o = o->next)
3812 {
3813 Elf_Internal_Rela *internal_relocs;
3814 bfd_boolean ok;
3815
5ce03cea 3816 /* Don't check relocations in excluded sections. */
d9689752 3817 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3818 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3819 || o->reloc_count == 0
3820 || ((info->strip == strip_all || info->strip == strip_debugger)
3821 && (o->flags & SEC_DEBUGGING) != 0)
3822 || bfd_is_abs_section (o->output_section))
3823 continue;
3824
3825 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3826 info->keep_memory);
3827 if (internal_relocs == NULL)
3828 return FALSE;
3829
3830 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3831
3832 if (elf_section_data (o)->relocs != internal_relocs)
3833 free (internal_relocs);
3834
3835 if (! ok)
3836 return FALSE;
3837 }
3838 }
3839
3840 return TRUE;
3841}
3842
4ad4eba5
AM
3843/* Add symbols from an ELF object file to the linker hash table. */
3844
3845static bfd_boolean
3846elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3847{
a0c402a5 3848 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3849 Elf_Internal_Shdr *hdr;
ef53be89
AM
3850 size_t symcount;
3851 size_t extsymcount;
3852 size_t extsymoff;
4ad4eba5
AM
3853 struct elf_link_hash_entry **sym_hash;
3854 bfd_boolean dynamic;
3855 Elf_External_Versym *extversym = NULL;
3856 Elf_External_Versym *ever;
3857 struct elf_link_hash_entry *weaks;
3858 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3859 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3860 Elf_Internal_Sym *isymbuf = NULL;
3861 Elf_Internal_Sym *isym;
3862 Elf_Internal_Sym *isymend;
3863 const struct elf_backend_data *bed;
3864 bfd_boolean add_needed;
66eb6687 3865 struct elf_link_hash_table *htab;
4ad4eba5 3866 bfd_size_type amt;
66eb6687 3867 void *alloc_mark = NULL;
4f87808c
AM
3868 struct bfd_hash_entry **old_table = NULL;
3869 unsigned int old_size = 0;
3870 unsigned int old_count = 0;
66eb6687 3871 void *old_tab = NULL;
66eb6687
AM
3872 void *old_ent;
3873 struct bfd_link_hash_entry *old_undefs = NULL;
3874 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3875 void *old_strtab = NULL;
66eb6687 3876 size_t tabsize = 0;
db6a5d5f 3877 asection *s;
29a9f53e 3878 bfd_boolean just_syms;
4ad4eba5 3879
66eb6687 3880 htab = elf_hash_table (info);
4ad4eba5 3881 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3882
3883 if ((abfd->flags & DYNAMIC) == 0)
3884 dynamic = FALSE;
3885 else
3886 {
3887 dynamic = TRUE;
3888
3889 /* You can't use -r against a dynamic object. Also, there's no
3890 hope of using a dynamic object which does not exactly match
3891 the format of the output file. */
0e1862bb 3892 if (bfd_link_relocatable (info)
66eb6687 3893 || !is_elf_hash_table (htab)
f13a99db 3894 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3895 {
0e1862bb 3896 if (bfd_link_relocatable (info))
9a0789ec
NC
3897 bfd_set_error (bfd_error_invalid_operation);
3898 else
3899 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3900 goto error_return;
3901 }
3902 }
3903
a0c402a5
L
3904 ehdr = elf_elfheader (abfd);
3905 if (info->warn_alternate_em
3906 && bed->elf_machine_code != ehdr->e_machine
3907 && ((bed->elf_machine_alt1 != 0
3908 && ehdr->e_machine == bed->elf_machine_alt1)
3909 || (bed->elf_machine_alt2 != 0
3910 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3911 _bfd_error_handler
695344c0 3912 /* xgettext:c-format */
9793eb77 3913 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3914 ehdr->e_machine, abfd, bed->elf_machine_code);
3915
4ad4eba5
AM
3916 /* As a GNU extension, any input sections which are named
3917 .gnu.warning.SYMBOL are treated as warning symbols for the given
3918 symbol. This differs from .gnu.warning sections, which generate
3919 warnings when they are included in an output file. */
dd98f8d2 3920 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3921 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3922 {
db6a5d5f 3923 const char *name;
4ad4eba5 3924
db6a5d5f
AM
3925 name = bfd_get_section_name (abfd, s);
3926 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3927 {
db6a5d5f
AM
3928 char *msg;
3929 bfd_size_type sz;
3930
3931 name += sizeof ".gnu.warning." - 1;
3932
3933 /* If this is a shared object, then look up the symbol
3934 in the hash table. If it is there, and it is already
3935 been defined, then we will not be using the entry
3936 from this shared object, so we don't need to warn.
3937 FIXME: If we see the definition in a regular object
3938 later on, we will warn, but we shouldn't. The only
3939 fix is to keep track of what warnings we are supposed
3940 to emit, and then handle them all at the end of the
3941 link. */
3942 if (dynamic)
4ad4eba5 3943 {
db6a5d5f
AM
3944 struct elf_link_hash_entry *h;
3945
3946 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3947
3948 /* FIXME: What about bfd_link_hash_common? */
3949 if (h != NULL
3950 && (h->root.type == bfd_link_hash_defined
3951 || h->root.type == bfd_link_hash_defweak))
3952 continue;
3953 }
4ad4eba5 3954
db6a5d5f
AM
3955 sz = s->size;
3956 msg = (char *) bfd_alloc (abfd, sz + 1);
3957 if (msg == NULL)
3958 goto error_return;
4ad4eba5 3959
db6a5d5f
AM
3960 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3961 goto error_return;
4ad4eba5 3962
db6a5d5f 3963 msg[sz] = '\0';
4ad4eba5 3964
db6a5d5f
AM
3965 if (! (_bfd_generic_link_add_one_symbol
3966 (info, abfd, name, BSF_WARNING, s, 0, msg,
3967 FALSE, bed->collect, NULL)))
3968 goto error_return;
4ad4eba5 3969
0e1862bb 3970 if (bfd_link_executable (info))
db6a5d5f
AM
3971 {
3972 /* Clobber the section size so that the warning does
3973 not get copied into the output file. */
3974 s->size = 0;
11d2f718 3975
db6a5d5f
AM
3976 /* Also set SEC_EXCLUDE, so that symbols defined in
3977 the warning section don't get copied to the output. */
3978 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3979 }
3980 }
3981 }
3982
29a9f53e
L
3983 just_syms = ((s = abfd->sections) != NULL
3984 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3985
4ad4eba5
AM
3986 add_needed = TRUE;
3987 if (! dynamic)
3988 {
3989 /* If we are creating a shared library, create all the dynamic
3990 sections immediately. We need to attach them to something,
3991 so we attach them to this BFD, provided it is the right
bf89386a
L
3992 format and is not from ld --just-symbols. Always create the
3993 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3994 are no input BFD's of the same format as the output, we can't
3995 make a shared library. */
3996 if (!just_syms
bf89386a 3997 && (bfd_link_pic (info)
9c1d7a08 3998 || (!bfd_link_relocatable (info)
3c5fce9b 3999 && info->nointerp
9c1d7a08 4000 && (info->export_dynamic || info->dynamic)))
66eb6687 4001 && is_elf_hash_table (htab)
f13a99db 4002 && info->output_bfd->xvec == abfd->xvec
66eb6687 4003 && !htab->dynamic_sections_created)
4ad4eba5
AM
4004 {
4005 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4006 goto error_return;
4007 }
4008 }
66eb6687 4009 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4010 goto error_return;
4011 else
4012 {
4ad4eba5 4013 const char *soname = NULL;
7ee314fa 4014 char *audit = NULL;
4ad4eba5 4015 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4016 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4017 int ret;
4018
4019 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4020 ld shouldn't allow it. */
29a9f53e 4021 if (just_syms)
92fd189d 4022 abort ();
4ad4eba5
AM
4023
4024 /* If this dynamic lib was specified on the command line with
4025 --as-needed in effect, then we don't want to add a DT_NEEDED
4026 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4027 in by another lib's DT_NEEDED. When --no-add-needed is used
4028 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4029 any dynamic library in DT_NEEDED tags in the dynamic lib at
4030 all. */
4031 add_needed = (elf_dyn_lib_class (abfd)
4032 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4033 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4034
4035 s = bfd_get_section_by_name (abfd, ".dynamic");
4036 if (s != NULL)
4037 {
4038 bfd_byte *dynbuf;
4039 bfd_byte *extdyn;
cb33740c 4040 unsigned int elfsec;
4ad4eba5
AM
4041 unsigned long shlink;
4042
eea6121a 4043 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4044 {
4045error_free_dyn:
4046 free (dynbuf);
4047 goto error_return;
4048 }
4ad4eba5
AM
4049
4050 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4051 if (elfsec == SHN_BAD)
4ad4eba5
AM
4052 goto error_free_dyn;
4053 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4054
4055 for (extdyn = dynbuf;
eea6121a 4056 extdyn < dynbuf + s->size;
4ad4eba5
AM
4057 extdyn += bed->s->sizeof_dyn)
4058 {
4059 Elf_Internal_Dyn dyn;
4060
4061 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4062 if (dyn.d_tag == DT_SONAME)
4063 {
4064 unsigned int tagv = dyn.d_un.d_val;
4065 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4066 if (soname == NULL)
4067 goto error_free_dyn;
4068 }
4069 if (dyn.d_tag == DT_NEEDED)
4070 {
4071 struct bfd_link_needed_list *n, **pn;
4072 char *fnm, *anm;
4073 unsigned int tagv = dyn.d_un.d_val;
4074
4075 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4076 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4077 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4078 if (n == NULL || fnm == NULL)
4079 goto error_free_dyn;
4080 amt = strlen (fnm) + 1;
a50b1753 4081 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4082 if (anm == NULL)
4083 goto error_free_dyn;
4084 memcpy (anm, fnm, amt);
4085 n->name = anm;
4086 n->by = abfd;
4087 n->next = NULL;
66eb6687 4088 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4089 ;
4090 *pn = n;
4091 }
4092 if (dyn.d_tag == DT_RUNPATH)
4093 {
4094 struct bfd_link_needed_list *n, **pn;
4095 char *fnm, *anm;
4096 unsigned int tagv = dyn.d_un.d_val;
4097
4098 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4099 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4100 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4101 if (n == NULL || fnm == NULL)
4102 goto error_free_dyn;
4103 amt = strlen (fnm) + 1;
a50b1753 4104 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4105 if (anm == NULL)
4106 goto error_free_dyn;
4107 memcpy (anm, fnm, amt);
4108 n->name = anm;
4109 n->by = abfd;
4110 n->next = NULL;
4111 for (pn = & runpath;
4112 *pn != NULL;
4113 pn = &(*pn)->next)
4114 ;
4115 *pn = n;
4116 }
4117 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4118 if (!runpath && dyn.d_tag == DT_RPATH)
4119 {
4120 struct bfd_link_needed_list *n, **pn;
4121 char *fnm, *anm;
4122 unsigned int tagv = dyn.d_un.d_val;
4123
4124 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4125 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4126 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4127 if (n == NULL || fnm == NULL)
4128 goto error_free_dyn;
4129 amt = strlen (fnm) + 1;
a50b1753 4130 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4131 if (anm == NULL)
f8703194 4132 goto error_free_dyn;
4ad4eba5
AM
4133 memcpy (anm, fnm, amt);
4134 n->name = anm;
4135 n->by = abfd;
4136 n->next = NULL;
4137 for (pn = & rpath;
4138 *pn != NULL;
4139 pn = &(*pn)->next)
4140 ;
4141 *pn = n;
4142 }
7ee314fa
AM
4143 if (dyn.d_tag == DT_AUDIT)
4144 {
4145 unsigned int tagv = dyn.d_un.d_val;
4146 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4147 }
4ad4eba5
AM
4148 }
4149
4150 free (dynbuf);
4151 }
4152
4153 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4154 frees all more recently bfd_alloc'd blocks as well. */
4155 if (runpath)
4156 rpath = runpath;
4157
4158 if (rpath)
4159 {
4160 struct bfd_link_needed_list **pn;
66eb6687 4161 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4162 ;
4163 *pn = rpath;
4164 }
4165
9acc85a6
AM
4166 /* If we have a PT_GNU_RELRO program header, mark as read-only
4167 all sections contained fully therein. This makes relro
4168 shared library sections appear as they will at run-time. */
4169 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4170 while (--phdr >= elf_tdata (abfd)->phdr)
4171 if (phdr->p_type == PT_GNU_RELRO)
4172 {
4173 for (s = abfd->sections; s != NULL; s = s->next)
4174 if ((s->flags & SEC_ALLOC) != 0
4175 && s->vma >= phdr->p_vaddr
4176 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4177 s->flags |= SEC_READONLY;
4178 break;
4179 }
4180
4ad4eba5
AM
4181 /* We do not want to include any of the sections in a dynamic
4182 object in the output file. We hack by simply clobbering the
4183 list of sections in the BFD. This could be handled more
4184 cleanly by, say, a new section flag; the existing
4185 SEC_NEVER_LOAD flag is not the one we want, because that one
4186 still implies that the section takes up space in the output
4187 file. */
4188 bfd_section_list_clear (abfd);
4189
4ad4eba5
AM
4190 /* Find the name to use in a DT_NEEDED entry that refers to this
4191 object. If the object has a DT_SONAME entry, we use it.
4192 Otherwise, if the generic linker stuck something in
4193 elf_dt_name, we use that. Otherwise, we just use the file
4194 name. */
4195 if (soname == NULL || *soname == '\0')
4196 {
4197 soname = elf_dt_name (abfd);
4198 if (soname == NULL || *soname == '\0')
4199 soname = bfd_get_filename (abfd);
4200 }
4201
4202 /* Save the SONAME because sometimes the linker emulation code
4203 will need to know it. */
4204 elf_dt_name (abfd) = soname;
4205
7e9f0867 4206 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4207 if (ret < 0)
4208 goto error_return;
4209
4210 /* If we have already included this dynamic object in the
4211 link, just ignore it. There is no reason to include a
4212 particular dynamic object more than once. */
4213 if (ret > 0)
4214 return TRUE;
7ee314fa
AM
4215
4216 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4217 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4218 }
4219
4220 /* If this is a dynamic object, we always link against the .dynsym
4221 symbol table, not the .symtab symbol table. The dynamic linker
4222 will only see the .dynsym symbol table, so there is no reason to
4223 look at .symtab for a dynamic object. */
4224
4225 if (! dynamic || elf_dynsymtab (abfd) == 0)
4226 hdr = &elf_tdata (abfd)->symtab_hdr;
4227 else
4228 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4229
4230 symcount = hdr->sh_size / bed->s->sizeof_sym;
4231
4232 /* The sh_info field of the symtab header tells us where the
4233 external symbols start. We don't care about the local symbols at
4234 this point. */
4235 if (elf_bad_symtab (abfd))
4236 {
4237 extsymcount = symcount;
4238 extsymoff = 0;
4239 }
4240 else
4241 {
4242 extsymcount = symcount - hdr->sh_info;
4243 extsymoff = hdr->sh_info;
4244 }
4245
f45794cb 4246 sym_hash = elf_sym_hashes (abfd);
012b2306 4247 if (extsymcount != 0)
4ad4eba5
AM
4248 {
4249 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4250 NULL, NULL, NULL);
4251 if (isymbuf == NULL)
4252 goto error_return;
4253
4ad4eba5 4254 if (sym_hash == NULL)
012b2306
AM
4255 {
4256 /* We store a pointer to the hash table entry for each
4257 external symbol. */
ef53be89
AM
4258 amt = extsymcount;
4259 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4260 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4261 if (sym_hash == NULL)
4262 goto error_free_sym;
4263 elf_sym_hashes (abfd) = sym_hash;
4264 }
4ad4eba5
AM
4265 }
4266
4267 if (dynamic)
4268 {
4269 /* Read in any version definitions. */
fc0e6df6
PB
4270 if (!_bfd_elf_slurp_version_tables (abfd,
4271 info->default_imported_symver))
4ad4eba5
AM
4272 goto error_free_sym;
4273
4274 /* Read in the symbol versions, but don't bother to convert them
4275 to internal format. */
4276 if (elf_dynversym (abfd) != 0)
4277 {
4278 Elf_Internal_Shdr *versymhdr;
4279
4280 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4281 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4282 if (extversym == NULL)
4283 goto error_free_sym;
4284 amt = versymhdr->sh_size;
4285 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4286 || bfd_bread (extversym, amt, abfd) != amt)
4287 goto error_free_vers;
4288 }
4289 }
4290
66eb6687
AM
4291 /* If we are loading an as-needed shared lib, save the symbol table
4292 state before we start adding symbols. If the lib turns out
4293 to be unneeded, restore the state. */
4294 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4295 {
4296 unsigned int i;
4297 size_t entsize;
4298
4299 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4300 {
4301 struct bfd_hash_entry *p;
2de92251 4302 struct elf_link_hash_entry *h;
66eb6687
AM
4303
4304 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4305 {
4306 h = (struct elf_link_hash_entry *) p;
4307 entsize += htab->root.table.entsize;
4308 if (h->root.type == bfd_link_hash_warning)
4309 entsize += htab->root.table.entsize;
4310 }
66eb6687
AM
4311 }
4312
4313 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4314 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4315 if (old_tab == NULL)
4316 goto error_free_vers;
4317
4318 /* Remember the current objalloc pointer, so that all mem for
4319 symbols added can later be reclaimed. */
4320 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4321 if (alloc_mark == NULL)
4322 goto error_free_vers;
4323
5061a885
AM
4324 /* Make a special call to the linker "notice" function to
4325 tell it that we are about to handle an as-needed lib. */
e5034e59 4326 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4327 goto error_free_vers;
5061a885 4328
f45794cb
AM
4329 /* Clone the symbol table. Remember some pointers into the
4330 symbol table, and dynamic symbol count. */
4331 old_ent = (char *) old_tab + tabsize;
66eb6687 4332 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4333 old_undefs = htab->root.undefs;
4334 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4335 old_table = htab->root.table.table;
4336 old_size = htab->root.table.size;
4337 old_count = htab->root.table.count;
5b677558
AM
4338 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4339 if (old_strtab == NULL)
4340 goto error_free_vers;
66eb6687
AM
4341
4342 for (i = 0; i < htab->root.table.size; i++)
4343 {
4344 struct bfd_hash_entry *p;
2de92251 4345 struct elf_link_hash_entry *h;
66eb6687
AM
4346
4347 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4348 {
4349 memcpy (old_ent, p, htab->root.table.entsize);
4350 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4351 h = (struct elf_link_hash_entry *) p;
4352 if (h->root.type == bfd_link_hash_warning)
4353 {
4354 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4355 old_ent = (char *) old_ent + htab->root.table.entsize;
4356 }
66eb6687
AM
4357 }
4358 }
4359 }
4ad4eba5 4360
66eb6687 4361 weaks = NULL;
4ad4eba5
AM
4362 ever = extversym != NULL ? extversym + extsymoff : NULL;
4363 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4364 isym < isymend;
4365 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4366 {
4367 int bind;
4368 bfd_vma value;
af44c138 4369 asection *sec, *new_sec;
4ad4eba5
AM
4370 flagword flags;
4371 const char *name;
4372 struct elf_link_hash_entry *h;
90c984fc 4373 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4374 bfd_boolean definition;
4375 bfd_boolean size_change_ok;
4376 bfd_boolean type_change_ok;
37a9e49a
L
4377 bfd_boolean new_weak;
4378 bfd_boolean old_weak;
4ad4eba5 4379 bfd_boolean override;
a4d8e49b 4380 bfd_boolean common;
97196564 4381 bfd_boolean discarded;
4ad4eba5
AM
4382 unsigned int old_alignment;
4383 bfd *old_bfd;
6e33951e 4384 bfd_boolean matched;
4ad4eba5
AM
4385
4386 override = FALSE;
4387
4388 flags = BSF_NO_FLAGS;
4389 sec = NULL;
4390 value = isym->st_value;
a4d8e49b 4391 common = bed->common_definition (isym);
2980ccad
L
4392 if (common && info->inhibit_common_definition)
4393 {
4394 /* Treat common symbol as undefined for --no-define-common. */
4395 isym->st_shndx = SHN_UNDEF;
4396 common = FALSE;
4397 }
97196564 4398 discarded = FALSE;
4ad4eba5
AM
4399
4400 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4401 switch (bind)
4ad4eba5 4402 {
3e7a7d11 4403 case STB_LOCAL:
4ad4eba5
AM
4404 /* This should be impossible, since ELF requires that all
4405 global symbols follow all local symbols, and that sh_info
4406 point to the first global symbol. Unfortunately, Irix 5
4407 screws this up. */
4408 continue;
3e7a7d11
NC
4409
4410 case STB_GLOBAL:
a4d8e49b 4411 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4412 flags = BSF_GLOBAL;
3e7a7d11
NC
4413 break;
4414
4415 case STB_WEAK:
4416 flags = BSF_WEAK;
4417 break;
4418
4419 case STB_GNU_UNIQUE:
4420 flags = BSF_GNU_UNIQUE;
4421 break;
4422
4423 default:
4ad4eba5 4424 /* Leave it up to the processor backend. */
3e7a7d11 4425 break;
4ad4eba5
AM
4426 }
4427
4428 if (isym->st_shndx == SHN_UNDEF)
4429 sec = bfd_und_section_ptr;
cb33740c
AM
4430 else if (isym->st_shndx == SHN_ABS)
4431 sec = bfd_abs_section_ptr;
4432 else if (isym->st_shndx == SHN_COMMON)
4433 {
4434 sec = bfd_com_section_ptr;
4435 /* What ELF calls the size we call the value. What ELF
4436 calls the value we call the alignment. */
4437 value = isym->st_size;
4438 }
4439 else
4ad4eba5
AM
4440 {
4441 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4442 if (sec == NULL)
4443 sec = bfd_abs_section_ptr;
dbaa2011 4444 else if (discarded_section (sec))
529fcb95 4445 {
e5d08002
L
4446 /* Symbols from discarded section are undefined. We keep
4447 its visibility. */
529fcb95 4448 sec = bfd_und_section_ptr;
97196564 4449 discarded = TRUE;
529fcb95
PB
4450 isym->st_shndx = SHN_UNDEF;
4451 }
4ad4eba5
AM
4452 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4453 value -= sec->vma;
4454 }
4ad4eba5
AM
4455
4456 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4457 isym->st_name);
4458 if (name == NULL)
4459 goto error_free_vers;
4460
4461 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4462 && (abfd->flags & BFD_PLUGIN) != 0)
4463 {
4464 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4465
4466 if (xc == NULL)
4467 {
4468 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4469 | SEC_EXCLUDE);
4470 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4471 if (xc == NULL)
4472 goto error_free_vers;
4473 }
4474 sec = xc;
4475 }
4476 else if (isym->st_shndx == SHN_COMMON
4477 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4478 && !bfd_link_relocatable (info))
4ad4eba5
AM
4479 {
4480 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4481
4482 if (tcomm == NULL)
4483 {
02d00247
AM
4484 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4485 | SEC_LINKER_CREATED);
4486 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4487 if (tcomm == NULL)
4ad4eba5
AM
4488 goto error_free_vers;
4489 }
4490 sec = tcomm;
4491 }
66eb6687 4492 else if (bed->elf_add_symbol_hook)
4ad4eba5 4493 {
66eb6687
AM
4494 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4495 &sec, &value))
4ad4eba5
AM
4496 goto error_free_vers;
4497
4498 /* The hook function sets the name to NULL if this symbol
4499 should be skipped for some reason. */
4500 if (name == NULL)
4501 continue;
4502 }
4503
4504 /* Sanity check that all possibilities were handled. */
4505 if (sec == NULL)
4506 {
4507 bfd_set_error (bfd_error_bad_value);
4508 goto error_free_vers;
4509 }
4510
191c0c42
AM
4511 /* Silently discard TLS symbols from --just-syms. There's
4512 no way to combine a static TLS block with a new TLS block
4513 for this executable. */
4514 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4515 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4516 continue;
4517
4ad4eba5
AM
4518 if (bfd_is_und_section (sec)
4519 || bfd_is_com_section (sec))
4520 definition = FALSE;
4521 else
4522 definition = TRUE;
4523
4524 size_change_ok = FALSE;
66eb6687 4525 type_change_ok = bed->type_change_ok;
37a9e49a 4526 old_weak = FALSE;
6e33951e 4527 matched = FALSE;
4ad4eba5
AM
4528 old_alignment = 0;
4529 old_bfd = NULL;
af44c138 4530 new_sec = sec;
4ad4eba5 4531
66eb6687 4532 if (is_elf_hash_table (htab))
4ad4eba5
AM
4533 {
4534 Elf_Internal_Versym iver;
4535 unsigned int vernum = 0;
4536 bfd_boolean skip;
4537
fc0e6df6 4538 if (ever == NULL)
4ad4eba5 4539 {
fc0e6df6
PB
4540 if (info->default_imported_symver)
4541 /* Use the default symbol version created earlier. */
4542 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4543 else
4544 iver.vs_vers = 0;
4545 }
4546 else
4547 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4548
4549 vernum = iver.vs_vers & VERSYM_VERSION;
4550
4551 /* If this is a hidden symbol, or if it is not version
4552 1, we append the version name to the symbol name.
cc86ff91
EB
4553 However, we do not modify a non-hidden absolute symbol
4554 if it is not a function, because it might be the version
4555 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4556 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4557 || (vernum > 1
4558 && (!bfd_is_abs_section (sec)
4559 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4560 {
4561 const char *verstr;
4562 size_t namelen, verlen, newlen;
4563 char *newname, *p;
4564
4565 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4566 {
fc0e6df6
PB
4567 if (vernum > elf_tdata (abfd)->cverdefs)
4568 verstr = NULL;
4569 else if (vernum > 1)
4570 verstr =
4571 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4572 else
4573 verstr = "";
4ad4eba5 4574
fc0e6df6 4575 if (verstr == NULL)
4ad4eba5 4576 {
4eca0228 4577 _bfd_error_handler
695344c0 4578 /* xgettext:c-format */
871b3ab2 4579 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4580 abfd, name, vernum,
4581 elf_tdata (abfd)->cverdefs);
4582 bfd_set_error (bfd_error_bad_value);
4583 goto error_free_vers;
4ad4eba5 4584 }
fc0e6df6
PB
4585 }
4586 else
4587 {
4588 /* We cannot simply test for the number of
4589 entries in the VERNEED section since the
4590 numbers for the needed versions do not start
4591 at 0. */
4592 Elf_Internal_Verneed *t;
4593
4594 verstr = NULL;
4595 for (t = elf_tdata (abfd)->verref;
4596 t != NULL;
4597 t = t->vn_nextref)
4ad4eba5 4598 {
fc0e6df6 4599 Elf_Internal_Vernaux *a;
4ad4eba5 4600
fc0e6df6
PB
4601 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4602 {
4603 if (a->vna_other == vernum)
4ad4eba5 4604 {
fc0e6df6
PB
4605 verstr = a->vna_nodename;
4606 break;
4ad4eba5 4607 }
4ad4eba5 4608 }
fc0e6df6
PB
4609 if (a != NULL)
4610 break;
4611 }
4612 if (verstr == NULL)
4613 {
4eca0228 4614 _bfd_error_handler
695344c0 4615 /* xgettext:c-format */
871b3ab2 4616 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4617 abfd, name, vernum);
4618 bfd_set_error (bfd_error_bad_value);
4619 goto error_free_vers;
4ad4eba5 4620 }
4ad4eba5 4621 }
fc0e6df6
PB
4622
4623 namelen = strlen (name);
4624 verlen = strlen (verstr);
4625 newlen = namelen + verlen + 2;
4626 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4627 && isym->st_shndx != SHN_UNDEF)
4628 ++newlen;
4629
a50b1753 4630 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4631 if (newname == NULL)
4632 goto error_free_vers;
4633 memcpy (newname, name, namelen);
4634 p = newname + namelen;
4635 *p++ = ELF_VER_CHR;
4636 /* If this is a defined non-hidden version symbol,
4637 we add another @ to the name. This indicates the
4638 default version of the symbol. */
4639 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4640 && isym->st_shndx != SHN_UNDEF)
4641 *p++ = ELF_VER_CHR;
4642 memcpy (p, verstr, verlen + 1);
4643
4644 name = newname;
4ad4eba5
AM
4645 }
4646
cd3416da
AM
4647 /* If this symbol has default visibility and the user has
4648 requested we not re-export it, then mark it as hidden. */
a0d49154 4649 if (!bfd_is_und_section (sec)
cd3416da 4650 && !dynamic
ce875075 4651 && abfd->no_export
cd3416da
AM
4652 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4653 isym->st_other = (STV_HIDDEN
4654 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4655
4f3fedcf
AM
4656 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4657 sym_hash, &old_bfd, &old_weak,
4658 &old_alignment, &skip, &override,
6e33951e
L
4659 &type_change_ok, &size_change_ok,
4660 &matched))
4ad4eba5
AM
4661 goto error_free_vers;
4662
4663 if (skip)
4664 continue;
4665
6e33951e
L
4666 /* Override a definition only if the new symbol matches the
4667 existing one. */
4668 if (override && matched)
4ad4eba5
AM
4669 definition = FALSE;
4670
4671 h = *sym_hash;
4672 while (h->root.type == bfd_link_hash_indirect
4673 || h->root.type == bfd_link_hash_warning)
4674 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4675
4ad4eba5 4676 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4677 && vernum > 1
4678 && definition)
4679 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4680 }
4681
4682 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4683 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4684 (struct bfd_link_hash_entry **) sym_hash)))
4685 goto error_free_vers;
4686
ac98f9e2
L
4687 if ((abfd->flags & DYNAMIC) == 0
4688 && (bfd_get_flavour (info->output_bfd)
4689 == bfd_target_elf_flavour))
4690 {
4691 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4692 elf_tdata (info->output_bfd)->has_gnu_symbols
4693 |= elf_gnu_symbol_ifunc;
4694 if ((flags & BSF_GNU_UNIQUE))
4695 elf_tdata (info->output_bfd)->has_gnu_symbols
4696 |= elf_gnu_symbol_unique;
4697 }
a43942db 4698
4ad4eba5 4699 h = *sym_hash;
90c984fc
L
4700 /* We need to make sure that indirect symbol dynamic flags are
4701 updated. */
4702 hi = h;
4ad4eba5
AM
4703 while (h->root.type == bfd_link_hash_indirect
4704 || h->root.type == bfd_link_hash_warning)
4705 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4706
97196564
L
4707 /* Setting the index to -3 tells elf_link_output_extsym that
4708 this symbol is defined in a discarded section. */
4709 if (discarded)
4710 h->indx = -3;
4711
4ad4eba5
AM
4712 *sym_hash = h;
4713
37a9e49a 4714 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4715 if (dynamic
4716 && definition
37a9e49a 4717 && new_weak
fcb93ecf 4718 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4719 && is_elf_hash_table (htab)
60d67dc8 4720 && h->u.alias == NULL)
4ad4eba5
AM
4721 {
4722 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4723 a dynamic object, using the alias field. Later in this
4724 function we will set the alias field to the correct
4ad4eba5
AM
4725 value. We only put non-function symbols from dynamic
4726 objects on this list, because that happens to be the only
4727 time we need to know the normal symbol corresponding to a
4728 weak symbol, and the information is time consuming to
60d67dc8 4729 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4730 then this symbol was already defined by some previous
4731 dynamic object, and we will be using that previous
4732 definition anyhow. */
4733
60d67dc8 4734 h->u.alias = weaks;
4ad4eba5 4735 weaks = h;
4ad4eba5
AM
4736 }
4737
4738 /* Set the alignment of a common symbol. */
a4d8e49b 4739 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4740 && h->root.type == bfd_link_hash_common)
4741 {
4742 unsigned int align;
4743
a4d8e49b 4744 if (common)
af44c138
L
4745 align = bfd_log2 (isym->st_value);
4746 else
4747 {
4748 /* The new symbol is a common symbol in a shared object.
4749 We need to get the alignment from the section. */
4750 align = new_sec->alignment_power;
4751 }
595213d4 4752 if (align > old_alignment)
4ad4eba5
AM
4753 h->root.u.c.p->alignment_power = align;
4754 else
4755 h->root.u.c.p->alignment_power = old_alignment;
4756 }
4757
66eb6687 4758 if (is_elf_hash_table (htab))
4ad4eba5 4759 {
4f3fedcf
AM
4760 /* Set a flag in the hash table entry indicating the type of
4761 reference or definition we just found. A dynamic symbol
4762 is one which is referenced or defined by both a regular
4763 object and a shared object. */
4764 bfd_boolean dynsym = FALSE;
4765
4766 /* Plugin symbols aren't normal. Don't set def_regular or
4767 ref_regular for them, or make them dynamic. */
4768 if ((abfd->flags & BFD_PLUGIN) != 0)
4769 ;
4770 else if (! dynamic)
4771 {
4772 if (! definition)
4773 {
4774 h->ref_regular = 1;
4775 if (bind != STB_WEAK)
4776 h->ref_regular_nonweak = 1;
4777 }
4778 else
4779 {
4780 h->def_regular = 1;
4781 if (h->def_dynamic)
4782 {
4783 h->def_dynamic = 0;
4784 h->ref_dynamic = 1;
4785 }
4786 }
4787
4788 /* If the indirect symbol has been forced local, don't
4789 make the real symbol dynamic. */
4790 if ((h == hi || !hi->forced_local)
0e1862bb 4791 && (bfd_link_dll (info)
4f3fedcf
AM
4792 || h->def_dynamic
4793 || h->ref_dynamic))
4794 dynsym = TRUE;
4795 }
4796 else
4797 {
4798 if (! definition)
4799 {
4800 h->ref_dynamic = 1;
4801 hi->ref_dynamic = 1;
4802 }
4803 else
4804 {
4805 h->def_dynamic = 1;
4806 hi->def_dynamic = 1;
4807 }
4808
4809 /* If the indirect symbol has been forced local, don't
4810 make the real symbol dynamic. */
4811 if ((h == hi || !hi->forced_local)
4812 && (h->def_regular
4813 || h->ref_regular
60d67dc8
AM
4814 || (h->is_weakalias
4815 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4816 dynsym = TRUE;
4817 }
4818
4819 /* Check to see if we need to add an indirect symbol for
4820 the default name. */
4821 if (definition
4822 || (!override && h->root.type == bfd_link_hash_common))
4823 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4824 sec, value, &old_bfd, &dynsym))
4825 goto error_free_vers;
4ad4eba5
AM
4826
4827 /* Check the alignment when a common symbol is involved. This
4828 can change when a common symbol is overridden by a normal
4829 definition or a common symbol is ignored due to the old
4830 normal definition. We need to make sure the maximum
4831 alignment is maintained. */
a4d8e49b 4832 if ((old_alignment || common)
4ad4eba5
AM
4833 && h->root.type != bfd_link_hash_common)
4834 {
4835 unsigned int common_align;
4836 unsigned int normal_align;
4837 unsigned int symbol_align;
4838 bfd *normal_bfd;
4839 bfd *common_bfd;
4840
3a81e825
AM
4841 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4842 || h->root.type == bfd_link_hash_defweak);
4843
4ad4eba5
AM
4844 symbol_align = ffs (h->root.u.def.value) - 1;
4845 if (h->root.u.def.section->owner != NULL
0616a280
AM
4846 && (h->root.u.def.section->owner->flags
4847 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4848 {
4849 normal_align = h->root.u.def.section->alignment_power;
4850 if (normal_align > symbol_align)
4851 normal_align = symbol_align;
4852 }
4853 else
4854 normal_align = symbol_align;
4855
4856 if (old_alignment)
4857 {
4858 common_align = old_alignment;
4859 common_bfd = old_bfd;
4860 normal_bfd = abfd;
4861 }
4862 else
4863 {
4864 common_align = bfd_log2 (isym->st_value);
4865 common_bfd = abfd;
4866 normal_bfd = old_bfd;
4867 }
4868
4869 if (normal_align < common_align)
d07676f8
NC
4870 {
4871 /* PR binutils/2735 */
4872 if (normal_bfd == NULL)
4eca0228 4873 _bfd_error_handler
695344c0 4874 /* xgettext:c-format */
9793eb77 4875 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4876 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4877 1 << common_align, name, common_bfd,
4878 1 << normal_align, h->root.u.def.section);
d07676f8 4879 else
4eca0228 4880 _bfd_error_handler
695344c0 4881 /* xgettext:c-format */
9793eb77 4882 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4883 " is smaller than %u in %pB"),
c08bb8dd
AM
4884 1 << normal_align, name, normal_bfd,
4885 1 << common_align, common_bfd);
d07676f8 4886 }
4ad4eba5
AM
4887 }
4888
83ad0046 4889 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4890 if (isym->st_size != 0
4891 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4892 && (definition || h->size == 0))
4893 {
83ad0046
L
4894 if (h->size != 0
4895 && h->size != isym->st_size
4896 && ! size_change_ok)
4eca0228 4897 _bfd_error_handler
695344c0 4898 /* xgettext:c-format */
9793eb77 4899 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4900 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4901 name, (uint64_t) h->size, old_bfd,
4902 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4903
4904 h->size = isym->st_size;
4905 }
4906
4907 /* If this is a common symbol, then we always want H->SIZE
4908 to be the size of the common symbol. The code just above
4909 won't fix the size if a common symbol becomes larger. We
4910 don't warn about a size change here, because that is
4f3fedcf 4911 covered by --warn-common. Allow changes between different
fcb93ecf 4912 function types. */
4ad4eba5
AM
4913 if (h->root.type == bfd_link_hash_common)
4914 h->size = h->root.u.c.size;
4915
4916 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4917 && ((definition && !new_weak)
4918 || (old_weak && h->root.type == bfd_link_hash_common)
4919 || h->type == STT_NOTYPE))
4ad4eba5 4920 {
2955ec4c
L
4921 unsigned int type = ELF_ST_TYPE (isym->st_info);
4922
4923 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4924 symbol. */
4925 if (type == STT_GNU_IFUNC
4926 && (abfd->flags & DYNAMIC) != 0)
4927 type = STT_FUNC;
4ad4eba5 4928
2955ec4c
L
4929 if (h->type != type)
4930 {
4931 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4932 /* xgettext:c-format */
4eca0228 4933 _bfd_error_handler
9793eb77 4934 (_("warning: type of symbol `%s' changed"
871b3ab2 4935 " from %d to %d in %pB"),
c08bb8dd 4936 name, h->type, type, abfd);
2955ec4c
L
4937
4938 h->type = type;
4939 }
4ad4eba5
AM
4940 }
4941
54ac0771 4942 /* Merge st_other field. */
b8417128 4943 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4944
c3df8c14 4945 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4946 if (definition
4947 && (sec->flags & SEC_DEBUGGING)
4948 && !bfd_link_relocatable (info))
c3df8c14
AM
4949 dynsym = FALSE;
4950
4f3fedcf
AM
4951 /* Nor should we make plugin symbols dynamic. */
4952 if ((abfd->flags & BFD_PLUGIN) != 0)
4953 dynsym = FALSE;
4954
35fc36a8 4955 if (definition)
35399224
L
4956 {
4957 h->target_internal = isym->st_target_internal;
4958 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4959 }
35fc36a8 4960
4ad4eba5
AM
4961 if (definition && !dynamic)
4962 {
4963 char *p = strchr (name, ELF_VER_CHR);
4964 if (p != NULL && p[1] != ELF_VER_CHR)
4965 {
4966 /* Queue non-default versions so that .symver x, x@FOO
4967 aliases can be checked. */
66eb6687 4968 if (!nondeflt_vers)
4ad4eba5 4969 {
66eb6687
AM
4970 amt = ((isymend - isym + 1)
4971 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4972 nondeflt_vers
4973 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4974 if (!nondeflt_vers)
4975 goto error_free_vers;
4ad4eba5 4976 }
66eb6687 4977 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4978 }
4979 }
4980
4981 if (dynsym && h->dynindx == -1)
4982 {
c152c796 4983 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4984 goto error_free_vers;
60d67dc8
AM
4985 if (h->is_weakalias
4986 && weakdef (h)->dynindx == -1)
4ad4eba5 4987 {
60d67dc8 4988 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
4989 goto error_free_vers;
4990 }
4991 }
1f599d0e 4992 else if (h->dynindx != -1)
4ad4eba5
AM
4993 /* If the symbol already has a dynamic index, but
4994 visibility says it should not be visible, turn it into
4995 a local symbol. */
4996 switch (ELF_ST_VISIBILITY (h->other))
4997 {
4998 case STV_INTERNAL:
4999 case STV_HIDDEN:
5000 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5001 dynsym = FALSE;
5002 break;
5003 }
5004
aef28989
L
5005 /* Don't add DT_NEEDED for references from the dummy bfd nor
5006 for unmatched symbol. */
4ad4eba5 5007 if (!add_needed
aef28989 5008 && matched
4ad4eba5 5009 && definition
010e5ae2 5010 && ((dynsym
ffa9430d 5011 && h->ref_regular_nonweak
4f3fedcf
AM
5012 && (old_bfd == NULL
5013 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5014 || (h->ref_dynamic_nonweak
010e5ae2 5015 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5016 && !on_needed_list (elf_dt_name (abfd),
5017 htab->needed, NULL))))
4ad4eba5
AM
5018 {
5019 int ret;
5020 const char *soname = elf_dt_name (abfd);
5021
16e4ecc0
AM
5022 info->callbacks->minfo ("%!", soname, old_bfd,
5023 h->root.root.string);
5024
4ad4eba5
AM
5025 /* A symbol from a library loaded via DT_NEEDED of some
5026 other library is referenced by a regular object.
e56f61be 5027 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5028 --no-add-needed is used and the reference was not
5029 a weak one. */
4f3fedcf 5030 if (old_bfd != NULL
b918acf9 5031 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5032 {
4eca0228 5033 _bfd_error_handler
695344c0 5034 /* xgettext:c-format */
871b3ab2 5035 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5036 old_bfd, name);
ff5ac77b 5037 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5038 goto error_free_vers;
5039 }
5040
a50b1753 5041 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5042 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5043
4ad4eba5 5044 add_needed = TRUE;
7e9f0867 5045 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5046 if (ret < 0)
5047 goto error_free_vers;
5048
5049 BFD_ASSERT (ret == 0);
5050 }
5051 }
5052 }
5053
a83ef4d1
L
5054 if (info->lto_plugin_active
5055 && !bfd_link_relocatable (info)
5056 && (abfd->flags & BFD_PLUGIN) == 0
5057 && !just_syms
5058 && extsymcount)
5059 {
5060 int r_sym_shift;
5061
5062 if (bed->s->arch_size == 32)
5063 r_sym_shift = 8;
5064 else
5065 r_sym_shift = 32;
5066
5067 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5068 referenced in regular objects so that linker plugin will get
5069 the correct symbol resolution. */
5070
5071 sym_hash = elf_sym_hashes (abfd);
5072 for (s = abfd->sections; s != NULL; s = s->next)
5073 {
5074 Elf_Internal_Rela *internal_relocs;
5075 Elf_Internal_Rela *rel, *relend;
5076
5077 /* Don't check relocations in excluded sections. */
5078 if ((s->flags & SEC_RELOC) == 0
5079 || s->reloc_count == 0
5080 || (s->flags & SEC_EXCLUDE) != 0
5081 || ((info->strip == strip_all
5082 || info->strip == strip_debugger)
5083 && (s->flags & SEC_DEBUGGING) != 0))
5084 continue;
5085
5086 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5087 NULL,
5088 info->keep_memory);
5089 if (internal_relocs == NULL)
5090 goto error_free_vers;
5091
5092 rel = internal_relocs;
5093 relend = rel + s->reloc_count;
5094 for ( ; rel < relend; rel++)
5095 {
5096 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5097 struct elf_link_hash_entry *h;
5098
5099 /* Skip local symbols. */
5100 if (r_symndx < extsymoff)
5101 continue;
5102
5103 h = sym_hash[r_symndx - extsymoff];
5104 if (h != NULL)
5105 h->root.non_ir_ref_regular = 1;
5106 }
5107
5108 if (elf_section_data (s)->relocs != internal_relocs)
5109 free (internal_relocs);
5110 }
5111 }
5112
66eb6687
AM
5113 if (extversym != NULL)
5114 {
5115 free (extversym);
5116 extversym = NULL;
5117 }
5118
5119 if (isymbuf != NULL)
5120 {
5121 free (isymbuf);
5122 isymbuf = NULL;
5123 }
5124
5125 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5126 {
5127 unsigned int i;
5128
5129 /* Restore the symbol table. */
f45794cb
AM
5130 old_ent = (char *) old_tab + tabsize;
5131 memset (elf_sym_hashes (abfd), 0,
5132 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5133 htab->root.table.table = old_table;
5134 htab->root.table.size = old_size;
5135 htab->root.table.count = old_count;
66eb6687 5136 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5137 htab->root.undefs = old_undefs;
5138 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5139 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5140 free (old_strtab);
5141 old_strtab = NULL;
66eb6687
AM
5142 for (i = 0; i < htab->root.table.size; i++)
5143 {
5144 struct bfd_hash_entry *p;
5145 struct elf_link_hash_entry *h;
3e0882af
L
5146 bfd_size_type size;
5147 unsigned int alignment_power;
4070765b 5148 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5149
5150 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5151 {
5152 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5153 if (h->root.type == bfd_link_hash_warning)
5154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5155
3e0882af
L
5156 /* Preserve the maximum alignment and size for common
5157 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5158 since it can still be loaded at run time by another
3e0882af
L
5159 dynamic lib. */
5160 if (h->root.type == bfd_link_hash_common)
5161 {
5162 size = h->root.u.c.size;
5163 alignment_power = h->root.u.c.p->alignment_power;
5164 }
5165 else
5166 {
5167 size = 0;
5168 alignment_power = 0;
5169 }
4070765b 5170 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5171 will be exported when the dynamic lib becomes needed
5172 in the second pass. */
4070765b 5173 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5174 memcpy (p, old_ent, htab->root.table.entsize);
5175 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5176 h = (struct elf_link_hash_entry *) p;
5177 if (h->root.type == bfd_link_hash_warning)
5178 {
5179 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5180 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5182 }
a4542f1b 5183 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5184 {
5185 if (size > h->root.u.c.size)
5186 h->root.u.c.size = size;
5187 if (alignment_power > h->root.u.c.p->alignment_power)
5188 h->root.u.c.p->alignment_power = alignment_power;
5189 }
4070765b 5190 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5191 }
5192 }
5193
5061a885
AM
5194 /* Make a special call to the linker "notice" function to
5195 tell it that symbols added for crefs may need to be removed. */
e5034e59 5196 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5197 goto error_free_vers;
5061a885 5198
66eb6687
AM
5199 free (old_tab);
5200 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5201 alloc_mark);
5202 if (nondeflt_vers != NULL)
5203 free (nondeflt_vers);
5204 return TRUE;
5205 }
2de92251 5206
66eb6687
AM
5207 if (old_tab != NULL)
5208 {
e5034e59 5209 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5210 goto error_free_vers;
66eb6687
AM
5211 free (old_tab);
5212 old_tab = NULL;
5213 }
5214
c6e8a9a8
L
5215 /* Now that all the symbols from this input file are created, if
5216 not performing a relocatable link, handle .symver foo, foo@BAR
5217 such that any relocs against foo become foo@BAR. */
0e1862bb 5218 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5219 {
ef53be89 5220 size_t cnt, symidx;
4ad4eba5
AM
5221
5222 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5223 {
5224 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5225 char *shortname, *p;
5226
5227 p = strchr (h->root.root.string, ELF_VER_CHR);
5228 if (p == NULL
5229 || (h->root.type != bfd_link_hash_defined
5230 && h->root.type != bfd_link_hash_defweak))
5231 continue;
5232
5233 amt = p - h->root.root.string;
a50b1753 5234 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5235 if (!shortname)
5236 goto error_free_vers;
4ad4eba5
AM
5237 memcpy (shortname, h->root.root.string, amt);
5238 shortname[amt] = '\0';
5239
5240 hi = (struct elf_link_hash_entry *)
66eb6687 5241 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5242 FALSE, FALSE, FALSE);
5243 if (hi != NULL
5244 && hi->root.type == h->root.type
5245 && hi->root.u.def.value == h->root.u.def.value
5246 && hi->root.u.def.section == h->root.u.def.section)
5247 {
5248 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5249 hi->root.type = bfd_link_hash_indirect;
5250 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5251 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5252 sym_hash = elf_sym_hashes (abfd);
5253 if (sym_hash)
5254 for (symidx = 0; symidx < extsymcount; ++symidx)
5255 if (sym_hash[symidx] == hi)
5256 {
5257 sym_hash[symidx] = h;
5258 break;
5259 }
5260 }
5261 free (shortname);
5262 }
5263 free (nondeflt_vers);
5264 nondeflt_vers = NULL;
5265 }
5266
60d67dc8 5267 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5268 symbols we found. The only way to do this is to search all the
5269 symbols. Since we only need the information for non functions in
5270 dynamic objects, that's the only time we actually put anything on
5271 the list WEAKS. We need this information so that if a regular
5272 object refers to a symbol defined weakly in a dynamic object, the
5273 real symbol in the dynamic object is also put in the dynamic
5274 symbols; we also must arrange for both symbols to point to the
5275 same memory location. We could handle the general case of symbol
5276 aliasing, but a general symbol alias can only be generated in
5277 assembler code, handling it correctly would be very time
5278 consuming, and other ELF linkers don't handle general aliasing
5279 either. */
5280 if (weaks != NULL)
5281 {
5282 struct elf_link_hash_entry **hpp;
5283 struct elf_link_hash_entry **hppend;
5284 struct elf_link_hash_entry **sorted_sym_hash;
5285 struct elf_link_hash_entry *h;
5286 size_t sym_count;
5287
5288 /* Since we have to search the whole symbol list for each weak
5289 defined symbol, search time for N weak defined symbols will be
5290 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5291 amt = extsymcount;
5292 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5293 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5294 if (sorted_sym_hash == NULL)
5295 goto error_return;
5296 sym_hash = sorted_sym_hash;
5297 hpp = elf_sym_hashes (abfd);
5298 hppend = hpp + extsymcount;
5299 sym_count = 0;
5300 for (; hpp < hppend; hpp++)
5301 {
5302 h = *hpp;
5303 if (h != NULL
5304 && h->root.type == bfd_link_hash_defined
fcb93ecf 5305 && !bed->is_function_type (h->type))
4ad4eba5
AM
5306 {
5307 *sym_hash = h;
5308 sym_hash++;
5309 sym_count++;
5310 }
5311 }
5312
5313 qsort (sorted_sym_hash, sym_count,
5314 sizeof (struct elf_link_hash_entry *),
5315 elf_sort_symbol);
5316
5317 while (weaks != NULL)
5318 {
5319 struct elf_link_hash_entry *hlook;
5320 asection *slook;
5321 bfd_vma vlook;
ed54588d 5322 size_t i, j, idx = 0;
4ad4eba5
AM
5323
5324 hlook = weaks;
60d67dc8
AM
5325 weaks = hlook->u.alias;
5326 hlook->u.alias = NULL;
4ad4eba5 5327
e3e53eed
AM
5328 if (hlook->root.type != bfd_link_hash_defined
5329 && hlook->root.type != bfd_link_hash_defweak)
5330 continue;
5331
4ad4eba5
AM
5332 slook = hlook->root.u.def.section;
5333 vlook = hlook->root.u.def.value;
5334
4ad4eba5
AM
5335 i = 0;
5336 j = sym_count;
14160578 5337 while (i != j)
4ad4eba5
AM
5338 {
5339 bfd_signed_vma vdiff;
5340 idx = (i + j) / 2;
14160578 5341 h = sorted_sym_hash[idx];
4ad4eba5
AM
5342 vdiff = vlook - h->root.u.def.value;
5343 if (vdiff < 0)
5344 j = idx;
5345 else if (vdiff > 0)
5346 i = idx + 1;
5347 else
5348 {
d3435ae8 5349 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5350 if (sdiff < 0)
5351 j = idx;
5352 else if (sdiff > 0)
5353 i = idx + 1;
5354 else
14160578 5355 break;
4ad4eba5
AM
5356 }
5357 }
5358
5359 /* We didn't find a value/section match. */
14160578 5360 if (i == j)
4ad4eba5
AM
5361 continue;
5362
14160578
AM
5363 /* With multiple aliases, or when the weak symbol is already
5364 strongly defined, we have multiple matching symbols and
5365 the binary search above may land on any of them. Step
5366 one past the matching symbol(s). */
5367 while (++idx != j)
5368 {
5369 h = sorted_sym_hash[idx];
5370 if (h->root.u.def.section != slook
5371 || h->root.u.def.value != vlook)
5372 break;
5373 }
5374
5375 /* Now look back over the aliases. Since we sorted by size
5376 as well as value and section, we'll choose the one with
5377 the largest size. */
5378 while (idx-- != i)
4ad4eba5 5379 {
14160578 5380 h = sorted_sym_hash[idx];
4ad4eba5
AM
5381
5382 /* Stop if value or section doesn't match. */
14160578
AM
5383 if (h->root.u.def.section != slook
5384 || h->root.u.def.value != vlook)
4ad4eba5
AM
5385 break;
5386 else if (h != hlook)
5387 {
60d67dc8
AM
5388 struct elf_link_hash_entry *t;
5389
5390 hlook->u.alias = h;
5391 hlook->is_weakalias = 1;
5392 t = h;
5393 if (t->u.alias != NULL)
5394 while (t->u.alias != h)
5395 t = t->u.alias;
5396 t->u.alias = hlook;
4ad4eba5
AM
5397
5398 /* If the weak definition is in the list of dynamic
5399 symbols, make sure the real definition is put
5400 there as well. */
5401 if (hlook->dynindx != -1 && h->dynindx == -1)
5402 {
c152c796 5403 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5404 {
5405 err_free_sym_hash:
5406 free (sorted_sym_hash);
5407 goto error_return;
5408 }
4ad4eba5
AM
5409 }
5410
5411 /* If the real definition is in the list of dynamic
5412 symbols, make sure the weak definition is put
5413 there as well. If we don't do this, then the
5414 dynamic loader might not merge the entries for the
5415 real definition and the weak definition. */
5416 if (h->dynindx != -1 && hlook->dynindx == -1)
5417 {
c152c796 5418 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5419 goto err_free_sym_hash;
4ad4eba5
AM
5420 }
5421 break;
5422 }
5423 }
5424 }
5425
5426 free (sorted_sym_hash);
5427 }
5428
33177bb1
AM
5429 if (bed->check_directives
5430 && !(*bed->check_directives) (abfd, info))
5431 return FALSE;
85fbca6a 5432
4ad4eba5
AM
5433 /* If this is a non-traditional link, try to optimize the handling
5434 of the .stab/.stabstr sections. */
5435 if (! dynamic
5436 && ! info->traditional_format
66eb6687 5437 && is_elf_hash_table (htab)
4ad4eba5
AM
5438 && (info->strip != strip_all && info->strip != strip_debugger))
5439 {
5440 asection *stabstr;
5441
5442 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5443 if (stabstr != NULL)
5444 {
5445 bfd_size_type string_offset = 0;
5446 asection *stab;
5447
5448 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5449 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5450 && (!stab->name[5] ||
5451 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5452 && (stab->flags & SEC_MERGE) == 0
5453 && !bfd_is_abs_section (stab->output_section))
5454 {
5455 struct bfd_elf_section_data *secdata;
5456
5457 secdata = elf_section_data (stab);
66eb6687
AM
5458 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5459 stabstr, &secdata->sec_info,
4ad4eba5
AM
5460 &string_offset))
5461 goto error_return;
5462 if (secdata->sec_info)
dbaa2011 5463 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5464 }
5465 }
5466 }
5467
66eb6687 5468 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5469 {
5470 /* Add this bfd to the loaded list. */
5471 struct elf_link_loaded_list *n;
5472
ca4be51c 5473 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5474 if (n == NULL)
5475 goto error_return;
5476 n->abfd = abfd;
66eb6687
AM
5477 n->next = htab->loaded;
5478 htab->loaded = n;
4ad4eba5
AM
5479 }
5480
5481 return TRUE;
5482
5483 error_free_vers:
66eb6687
AM
5484 if (old_tab != NULL)
5485 free (old_tab);
5b677558
AM
5486 if (old_strtab != NULL)
5487 free (old_strtab);
4ad4eba5
AM
5488 if (nondeflt_vers != NULL)
5489 free (nondeflt_vers);
5490 if (extversym != NULL)
5491 free (extversym);
5492 error_free_sym:
5493 if (isymbuf != NULL)
5494 free (isymbuf);
5495 error_return:
5496 return FALSE;
5497}
5498
8387904d
AM
5499/* Return the linker hash table entry of a symbol that might be
5500 satisfied by an archive symbol. Return -1 on error. */
5501
5502struct elf_link_hash_entry *
5503_bfd_elf_archive_symbol_lookup (bfd *abfd,
5504 struct bfd_link_info *info,
5505 const char *name)
5506{
5507 struct elf_link_hash_entry *h;
5508 char *p, *copy;
5509 size_t len, first;
5510
2a41f396 5511 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5512 if (h != NULL)
5513 return h;
5514
5515 /* If this is a default version (the name contains @@), look up the
5516 symbol again with only one `@' as well as without the version.
5517 The effect is that references to the symbol with and without the
5518 version will be matched by the default symbol in the archive. */
5519
5520 p = strchr (name, ELF_VER_CHR);
5521 if (p == NULL || p[1] != ELF_VER_CHR)
5522 return h;
5523
5524 /* First check with only one `@'. */
5525 len = strlen (name);
a50b1753 5526 copy = (char *) bfd_alloc (abfd, len);
8387904d 5527 if (copy == NULL)
e99955cd 5528 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5529
5530 first = p - name + 1;
5531 memcpy (copy, name, first);
5532 memcpy (copy + first, name + first + 1, len - first);
5533
2a41f396 5534 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5535 if (h == NULL)
5536 {
5537 /* We also need to check references to the symbol without the
5538 version. */
5539 copy[first - 1] = '\0';
5540 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5541 FALSE, FALSE, TRUE);
8387904d
AM
5542 }
5543
5544 bfd_release (abfd, copy);
5545 return h;
5546}
5547
0ad989f9 5548/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5549 don't use _bfd_generic_link_add_archive_symbols because we need to
5550 handle versioned symbols.
0ad989f9
L
5551
5552 Fortunately, ELF archive handling is simpler than that done by
5553 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5554 oddities. In ELF, if we find a symbol in the archive map, and the
5555 symbol is currently undefined, we know that we must pull in that
5556 object file.
5557
5558 Unfortunately, we do have to make multiple passes over the symbol
5559 table until nothing further is resolved. */
5560
4ad4eba5
AM
5561static bfd_boolean
5562elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5563{
5564 symindex c;
13e570f8 5565 unsigned char *included = NULL;
0ad989f9
L
5566 carsym *symdefs;
5567 bfd_boolean loop;
5568 bfd_size_type amt;
8387904d
AM
5569 const struct elf_backend_data *bed;
5570 struct elf_link_hash_entry * (*archive_symbol_lookup)
5571 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5572
5573 if (! bfd_has_map (abfd))
5574 {
5575 /* An empty archive is a special case. */
5576 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5577 return TRUE;
5578 bfd_set_error (bfd_error_no_armap);
5579 return FALSE;
5580 }
5581
5582 /* Keep track of all symbols we know to be already defined, and all
5583 files we know to be already included. This is to speed up the
5584 second and subsequent passes. */
5585 c = bfd_ardata (abfd)->symdef_count;
5586 if (c == 0)
5587 return TRUE;
5588 amt = c;
13e570f8
AM
5589 amt *= sizeof (*included);
5590 included = (unsigned char *) bfd_zmalloc (amt);
5591 if (included == NULL)
5592 return FALSE;
0ad989f9
L
5593
5594 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5595 bed = get_elf_backend_data (abfd);
5596 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5597
5598 do
5599 {
5600 file_ptr last;
5601 symindex i;
5602 carsym *symdef;
5603 carsym *symdefend;
5604
5605 loop = FALSE;
5606 last = -1;
5607
5608 symdef = symdefs;
5609 symdefend = symdef + c;
5610 for (i = 0; symdef < symdefend; symdef++, i++)
5611 {
5612 struct elf_link_hash_entry *h;
5613 bfd *element;
5614 struct bfd_link_hash_entry *undefs_tail;
5615 symindex mark;
5616
13e570f8 5617 if (included[i])
0ad989f9
L
5618 continue;
5619 if (symdef->file_offset == last)
5620 {
5621 included[i] = TRUE;
5622 continue;
5623 }
5624
8387904d 5625 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5626 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5627 goto error_return;
0ad989f9
L
5628
5629 if (h == NULL)
5630 continue;
5631
5632 if (h->root.type == bfd_link_hash_common)
5633 {
5634 /* We currently have a common symbol. The archive map contains
5635 a reference to this symbol, so we may want to include it. We
5636 only want to include it however, if this archive element
5637 contains a definition of the symbol, not just another common
5638 declaration of it.
5639
5640 Unfortunately some archivers (including GNU ar) will put
5641 declarations of common symbols into their archive maps, as
5642 well as real definitions, so we cannot just go by the archive
5643 map alone. Instead we must read in the element's symbol
5644 table and check that to see what kind of symbol definition
5645 this is. */
5646 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5647 continue;
5648 }
5649 else if (h->root.type != bfd_link_hash_undefined)
5650 {
5651 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5652 /* Symbol must be defined. Don't check it again. */
5653 included[i] = TRUE;
0ad989f9
L
5654 continue;
5655 }
5656
5657 /* We need to include this archive member. */
5658 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5659 if (element == NULL)
5660 goto error_return;
5661
5662 if (! bfd_check_format (element, bfd_object))
5663 goto error_return;
5664
0ad989f9
L
5665 undefs_tail = info->hash->undefs_tail;
5666
0e144ba7
AM
5667 if (!(*info->callbacks
5668 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5669 continue;
0e144ba7 5670 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5671 goto error_return;
5672
5673 /* If there are any new undefined symbols, we need to make
5674 another pass through the archive in order to see whether
5675 they can be defined. FIXME: This isn't perfect, because
5676 common symbols wind up on undefs_tail and because an
5677 undefined symbol which is defined later on in this pass
5678 does not require another pass. This isn't a bug, but it
5679 does make the code less efficient than it could be. */
5680 if (undefs_tail != info->hash->undefs_tail)
5681 loop = TRUE;
5682
5683 /* Look backward to mark all symbols from this object file
5684 which we have already seen in this pass. */
5685 mark = i;
5686 do
5687 {
5688 included[mark] = TRUE;
5689 if (mark == 0)
5690 break;
5691 --mark;
5692 }
5693 while (symdefs[mark].file_offset == symdef->file_offset);
5694
5695 /* We mark subsequent symbols from this object file as we go
5696 on through the loop. */
5697 last = symdef->file_offset;
5698 }
5699 }
5700 while (loop);
5701
0ad989f9
L
5702 free (included);
5703
5704 return TRUE;
5705
5706 error_return:
0ad989f9
L
5707 if (included != NULL)
5708 free (included);
5709 return FALSE;
5710}
4ad4eba5
AM
5711
5712/* Given an ELF BFD, add symbols to the global hash table as
5713 appropriate. */
5714
5715bfd_boolean
5716bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5717{
5718 switch (bfd_get_format (abfd))
5719 {
5720 case bfd_object:
5721 return elf_link_add_object_symbols (abfd, info);
5722 case bfd_archive:
5723 return elf_link_add_archive_symbols (abfd, info);
5724 default:
5725 bfd_set_error (bfd_error_wrong_format);
5726 return FALSE;
5727 }
5728}
5a580b3a 5729\f
14b1c01e
AM
5730struct hash_codes_info
5731{
5732 unsigned long *hashcodes;
5733 bfd_boolean error;
5734};
a0c8462f 5735
5a580b3a
AM
5736/* This function will be called though elf_link_hash_traverse to store
5737 all hash value of the exported symbols in an array. */
5738
5739static bfd_boolean
5740elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5741{
a50b1753 5742 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5743 const char *name;
5a580b3a
AM
5744 unsigned long ha;
5745 char *alc = NULL;
5746
5a580b3a
AM
5747 /* Ignore indirect symbols. These are added by the versioning code. */
5748 if (h->dynindx == -1)
5749 return TRUE;
5750
5751 name = h->root.root.string;
422f1182 5752 if (h->versioned >= versioned)
5a580b3a 5753 {
422f1182
L
5754 char *p = strchr (name, ELF_VER_CHR);
5755 if (p != NULL)
14b1c01e 5756 {
422f1182
L
5757 alc = (char *) bfd_malloc (p - name + 1);
5758 if (alc == NULL)
5759 {
5760 inf->error = TRUE;
5761 return FALSE;
5762 }
5763 memcpy (alc, name, p - name);
5764 alc[p - name] = '\0';
5765 name = alc;
14b1c01e 5766 }
5a580b3a
AM
5767 }
5768
5769 /* Compute the hash value. */
5770 ha = bfd_elf_hash (name);
5771
5772 /* Store the found hash value in the array given as the argument. */
14b1c01e 5773 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5774
5775 /* And store it in the struct so that we can put it in the hash table
5776 later. */
f6e332e6 5777 h->u.elf_hash_value = ha;
5a580b3a
AM
5778
5779 if (alc != NULL)
5780 free (alc);
5781
5782 return TRUE;
5783}
5784
fdc90cb4
JJ
5785struct collect_gnu_hash_codes
5786{
5787 bfd *output_bfd;
5788 const struct elf_backend_data *bed;
5789 unsigned long int nsyms;
5790 unsigned long int maskbits;
5791 unsigned long int *hashcodes;
5792 unsigned long int *hashval;
5793 unsigned long int *indx;
5794 unsigned long int *counts;
5795 bfd_vma *bitmask;
5796 bfd_byte *contents;
5797 long int min_dynindx;
5798 unsigned long int bucketcount;
5799 unsigned long int symindx;
5800 long int local_indx;
5801 long int shift1, shift2;
5802 unsigned long int mask;
14b1c01e 5803 bfd_boolean error;
fdc90cb4
JJ
5804};
5805
5806/* This function will be called though elf_link_hash_traverse to store
5807 all hash value of the exported symbols in an array. */
5808
5809static bfd_boolean
5810elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5811{
a50b1753 5812 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5813 const char *name;
fdc90cb4
JJ
5814 unsigned long ha;
5815 char *alc = NULL;
5816
fdc90cb4
JJ
5817 /* Ignore indirect symbols. These are added by the versioning code. */
5818 if (h->dynindx == -1)
5819 return TRUE;
5820
5821 /* Ignore also local symbols and undefined symbols. */
5822 if (! (*s->bed->elf_hash_symbol) (h))
5823 return TRUE;
5824
5825 name = h->root.root.string;
422f1182 5826 if (h->versioned >= versioned)
fdc90cb4 5827 {
422f1182
L
5828 char *p = strchr (name, ELF_VER_CHR);
5829 if (p != NULL)
14b1c01e 5830 {
422f1182
L
5831 alc = (char *) bfd_malloc (p - name + 1);
5832 if (alc == NULL)
5833 {
5834 s->error = TRUE;
5835 return FALSE;
5836 }
5837 memcpy (alc, name, p - name);
5838 alc[p - name] = '\0';
5839 name = alc;
14b1c01e 5840 }
fdc90cb4
JJ
5841 }
5842
5843 /* Compute the hash value. */
5844 ha = bfd_elf_gnu_hash (name);
5845
5846 /* Store the found hash value in the array for compute_bucket_count,
5847 and also for .dynsym reordering purposes. */
5848 s->hashcodes[s->nsyms] = ha;
5849 s->hashval[h->dynindx] = ha;
5850 ++s->nsyms;
5851 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5852 s->min_dynindx = h->dynindx;
5853
5854 if (alc != NULL)
5855 free (alc);
5856
5857 return TRUE;
5858}
5859
5860/* This function will be called though elf_link_hash_traverse to do
5861 final dynaminc symbol renumbering. */
5862
5863static bfd_boolean
5864elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5865{
a50b1753 5866 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5867 unsigned long int bucket;
5868 unsigned long int val;
5869
fdc90cb4
JJ
5870 /* Ignore indirect symbols. */
5871 if (h->dynindx == -1)
5872 return TRUE;
5873
5874 /* Ignore also local symbols and undefined symbols. */
5875 if (! (*s->bed->elf_hash_symbol) (h))
5876 {
5877 if (h->dynindx >= s->min_dynindx)
5878 h->dynindx = s->local_indx++;
5879 return TRUE;
5880 }
5881
5882 bucket = s->hashval[h->dynindx] % s->bucketcount;
5883 val = (s->hashval[h->dynindx] >> s->shift1)
5884 & ((s->maskbits >> s->shift1) - 1);
5885 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5886 s->bitmask[val]
5887 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5888 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5889 if (s->counts[bucket] == 1)
5890 /* Last element terminates the chain. */
5891 val |= 1;
5892 bfd_put_32 (s->output_bfd, val,
5893 s->contents + (s->indx[bucket] - s->symindx) * 4);
5894 --s->counts[bucket];
5895 h->dynindx = s->indx[bucket]++;
5896 return TRUE;
5897}
5898
5899/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5900
5901bfd_boolean
5902_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5903{
5904 return !(h->forced_local
5905 || h->root.type == bfd_link_hash_undefined
5906 || h->root.type == bfd_link_hash_undefweak
5907 || ((h->root.type == bfd_link_hash_defined
5908 || h->root.type == bfd_link_hash_defweak)
5909 && h->root.u.def.section->output_section == NULL));
5910}
5911
5a580b3a
AM
5912/* Array used to determine the number of hash table buckets to use
5913 based on the number of symbols there are. If there are fewer than
5914 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5915 fewer than 37 we use 17 buckets, and so forth. We never use more
5916 than 32771 buckets. */
5917
5918static const size_t elf_buckets[] =
5919{
5920 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5921 16411, 32771, 0
5922};
5923
5924/* Compute bucket count for hashing table. We do not use a static set
5925 of possible tables sizes anymore. Instead we determine for all
5926 possible reasonable sizes of the table the outcome (i.e., the
5927 number of collisions etc) and choose the best solution. The
5928 weighting functions are not too simple to allow the table to grow
5929 without bounds. Instead one of the weighting factors is the size.
5930 Therefore the result is always a good payoff between few collisions
5931 (= short chain lengths) and table size. */
5932static size_t
b20dd2ce 5933compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5934 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5935 unsigned long int nsyms,
5936 int gnu_hash)
5a580b3a 5937{
5a580b3a 5938 size_t best_size = 0;
5a580b3a 5939 unsigned long int i;
5a580b3a 5940
5a580b3a
AM
5941 /* We have a problem here. The following code to optimize the table
5942 size requires an integer type with more the 32 bits. If
5943 BFD_HOST_U_64_BIT is set we know about such a type. */
5944#ifdef BFD_HOST_U_64_BIT
5945 if (info->optimize)
5946 {
5a580b3a
AM
5947 size_t minsize;
5948 size_t maxsize;
5949 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5950 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5951 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5952 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5953 unsigned long int *counts;
d40f3da9 5954 bfd_size_type amt;
0883b6e0 5955 unsigned int no_improvement_count = 0;
5a580b3a
AM
5956
5957 /* Possible optimization parameters: if we have NSYMS symbols we say
5958 that the hashing table must at least have NSYMS/4 and at most
5959 2*NSYMS buckets. */
5960 minsize = nsyms / 4;
5961 if (minsize == 0)
5962 minsize = 1;
5963 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5964 if (gnu_hash)
5965 {
5966 if (minsize < 2)
5967 minsize = 2;
5968 if ((best_size & 31) == 0)
5969 ++best_size;
5970 }
5a580b3a
AM
5971
5972 /* Create array where we count the collisions in. We must use bfd_malloc
5973 since the size could be large. */
5974 amt = maxsize;
5975 amt *= sizeof (unsigned long int);
a50b1753 5976 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5977 if (counts == NULL)
fdc90cb4 5978 return 0;
5a580b3a
AM
5979
5980 /* Compute the "optimal" size for the hash table. The criteria is a
5981 minimal chain length. The minor criteria is (of course) the size
5982 of the table. */
5983 for (i = minsize; i < maxsize; ++i)
5984 {
5985 /* Walk through the array of hashcodes and count the collisions. */
5986 BFD_HOST_U_64_BIT max;
5987 unsigned long int j;
5988 unsigned long int fact;
5989
fdc90cb4
JJ
5990 if (gnu_hash && (i & 31) == 0)
5991 continue;
5992
5a580b3a
AM
5993 memset (counts, '\0', i * sizeof (unsigned long int));
5994
5995 /* Determine how often each hash bucket is used. */
5996 for (j = 0; j < nsyms; ++j)
5997 ++counts[hashcodes[j] % i];
5998
5999 /* For the weight function we need some information about the
6000 pagesize on the target. This is information need not be 100%
6001 accurate. Since this information is not available (so far) we
6002 define it here to a reasonable default value. If it is crucial
6003 to have a better value some day simply define this value. */
6004# ifndef BFD_TARGET_PAGESIZE
6005# define BFD_TARGET_PAGESIZE (4096)
6006# endif
6007
fdc90cb4
JJ
6008 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6009 and the chains. */
6010 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6011
6012# if 1
6013 /* Variant 1: optimize for short chains. We add the squares
6014 of all the chain lengths (which favors many small chain
6015 over a few long chains). */
6016 for (j = 0; j < i; ++j)
6017 max += counts[j] * counts[j];
6018
6019 /* This adds penalties for the overall size of the table. */
fdc90cb4 6020 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6021 max *= fact * fact;
6022# else
6023 /* Variant 2: Optimize a lot more for small table. Here we
6024 also add squares of the size but we also add penalties for
6025 empty slots (the +1 term). */
6026 for (j = 0; j < i; ++j)
6027 max += (1 + counts[j]) * (1 + counts[j]);
6028
6029 /* The overall size of the table is considered, but not as
6030 strong as in variant 1, where it is squared. */
fdc90cb4 6031 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6032 max *= fact;
6033# endif
6034
6035 /* Compare with current best results. */
6036 if (max < best_chlen)
6037 {
6038 best_chlen = max;
6039 best_size = i;
ca4be51c 6040 no_improvement_count = 0;
5a580b3a 6041 }
0883b6e0
NC
6042 /* PR 11843: Avoid futile long searches for the best bucket size
6043 when there are a large number of symbols. */
6044 else if (++no_improvement_count == 100)
6045 break;
5a580b3a
AM
6046 }
6047
6048 free (counts);
6049 }
6050 else
6051#endif /* defined (BFD_HOST_U_64_BIT) */
6052 {
6053 /* This is the fallback solution if no 64bit type is available or if we
6054 are not supposed to spend much time on optimizations. We select the
6055 bucket count using a fixed set of numbers. */
6056 for (i = 0; elf_buckets[i] != 0; i++)
6057 {
6058 best_size = elf_buckets[i];
fdc90cb4 6059 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6060 break;
6061 }
fdc90cb4
JJ
6062 if (gnu_hash && best_size < 2)
6063 best_size = 2;
5a580b3a
AM
6064 }
6065
5a580b3a
AM
6066 return best_size;
6067}
6068
d0bf826b
AM
6069/* Size any SHT_GROUP section for ld -r. */
6070
6071bfd_boolean
6072_bfd_elf_size_group_sections (struct bfd_link_info *info)
6073{
6074 bfd *ibfd;
57963c05 6075 asection *s;
d0bf826b 6076
c72f2fb2 6077 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6078 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6079 && (s = ibfd->sections) != NULL
6080 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6081 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6082 return FALSE;
6083 return TRUE;
6084}
6085
04c3a755
NS
6086/* Set a default stack segment size. The value in INFO wins. If it
6087 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6088 undefined it is initialized. */
6089
6090bfd_boolean
6091bfd_elf_stack_segment_size (bfd *output_bfd,
6092 struct bfd_link_info *info,
6093 const char *legacy_symbol,
6094 bfd_vma default_size)
6095{
6096 struct elf_link_hash_entry *h = NULL;
6097
6098 /* Look for legacy symbol. */
6099 if (legacy_symbol)
6100 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6101 FALSE, FALSE, FALSE);
6102 if (h && (h->root.type == bfd_link_hash_defined
6103 || h->root.type == bfd_link_hash_defweak)
6104 && h->def_regular
6105 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6106 {
6107 /* The symbol has no type if specified on the command line. */
6108 h->type = STT_OBJECT;
6109 if (info->stacksize)
695344c0 6110 /* xgettext:c-format */
871b3ab2 6111 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6112 output_bfd, legacy_symbol);
04c3a755 6113 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6114 /* xgettext:c-format */
871b3ab2 6115 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6116 output_bfd, legacy_symbol);
04c3a755
NS
6117 else
6118 info->stacksize = h->root.u.def.value;
6119 }
6120
6121 if (!info->stacksize)
6122 /* If the user didn't set a size, or explicitly inhibit the
6123 size, set it now. */
6124 info->stacksize = default_size;
6125
6126 /* Provide the legacy symbol, if it is referenced. */
6127 if (h && (h->root.type == bfd_link_hash_undefined
6128 || h->root.type == bfd_link_hash_undefweak))
6129 {
6130 struct bfd_link_hash_entry *bh = NULL;
6131
6132 if (!(_bfd_generic_link_add_one_symbol
6133 (info, output_bfd, legacy_symbol,
6134 BSF_GLOBAL, bfd_abs_section_ptr,
6135 info->stacksize >= 0 ? info->stacksize : 0,
6136 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6137 return FALSE;
6138
6139 h = (struct elf_link_hash_entry *) bh;
6140 h->def_regular = 1;
6141 h->type = STT_OBJECT;
6142 }
6143
6144 return TRUE;
6145}
6146
b531344c
MR
6147/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6148
6149struct elf_gc_sweep_symbol_info
6150{
6151 struct bfd_link_info *info;
6152 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6153 bfd_boolean);
6154};
6155
6156static bfd_boolean
6157elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6158{
6159 if (!h->mark
6160 && (((h->root.type == bfd_link_hash_defined
6161 || h->root.type == bfd_link_hash_defweak)
6162 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6163 && h->root.u.def.section->gc_mark))
6164 || h->root.type == bfd_link_hash_undefined
6165 || h->root.type == bfd_link_hash_undefweak))
6166 {
6167 struct elf_gc_sweep_symbol_info *inf;
6168
6169 inf = (struct elf_gc_sweep_symbol_info *) data;
6170 (*inf->hide_symbol) (inf->info, h, TRUE);
6171 h->def_regular = 0;
6172 h->ref_regular = 0;
6173 h->ref_regular_nonweak = 0;
6174 }
6175
6176 return TRUE;
6177}
6178
5a580b3a
AM
6179/* Set up the sizes and contents of the ELF dynamic sections. This is
6180 called by the ELF linker emulation before_allocation routine. We
6181 must set the sizes of the sections before the linker sets the
6182 addresses of the various sections. */
6183
6184bfd_boolean
6185bfd_elf_size_dynamic_sections (bfd *output_bfd,
6186 const char *soname,
6187 const char *rpath,
6188 const char *filter_shlib,
7ee314fa
AM
6189 const char *audit,
6190 const char *depaudit,
5a580b3a
AM
6191 const char * const *auxiliary_filters,
6192 struct bfd_link_info *info,
fd91d419 6193 asection **sinterpptr)
5a580b3a 6194{
5a580b3a
AM
6195 bfd *dynobj;
6196 const struct elf_backend_data *bed;
5a580b3a
AM
6197
6198 *sinterpptr = NULL;
6199
5a580b3a
AM
6200 if (!is_elf_hash_table (info->hash))
6201 return TRUE;
6202
5a580b3a
AM
6203 dynobj = elf_hash_table (info)->dynobj;
6204
9a2a56cc 6205 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6206 {
902e9fc7
MR
6207 struct bfd_elf_version_tree *verdefs;
6208 struct elf_info_failed asvinfo;
5a580b3a
AM
6209 struct bfd_elf_version_tree *t;
6210 struct bfd_elf_version_expr *d;
902e9fc7 6211 asection *s;
e6699019 6212 size_t soname_indx;
7ee314fa 6213
5a580b3a
AM
6214 /* If we are supposed to export all symbols into the dynamic symbol
6215 table (this is not the normal case), then do so. */
55255dae 6216 if (info->export_dynamic
0e1862bb 6217 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6218 {
3d13f3e9
AM
6219 struct elf_info_failed eif;
6220
6221 eif.info = info;
6222 eif.failed = FALSE;
5a580b3a
AM
6223 elf_link_hash_traverse (elf_hash_table (info),
6224 _bfd_elf_export_symbol,
6225 &eif);
6226 if (eif.failed)
6227 return FALSE;
6228 }
6229
e6699019
L
6230 if (soname != NULL)
6231 {
6232 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6233 soname, TRUE);
6234 if (soname_indx == (size_t) -1
6235 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6236 return FALSE;
6237 }
6238 else
6239 soname_indx = (size_t) -1;
6240
5a580b3a 6241 /* Make all global versions with definition. */
fd91d419 6242 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6243 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6244 if (!d->symver && d->literal)
5a580b3a
AM
6245 {
6246 const char *verstr, *name;
6247 size_t namelen, verlen, newlen;
93252b1c 6248 char *newname, *p, leading_char;
5a580b3a
AM
6249 struct elf_link_hash_entry *newh;
6250
93252b1c 6251 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6252 name = d->pattern;
93252b1c 6253 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6254 verstr = t->name;
6255 verlen = strlen (verstr);
6256 newlen = namelen + verlen + 3;
6257
a50b1753 6258 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6259 if (newname == NULL)
6260 return FALSE;
93252b1c
MF
6261 newname[0] = leading_char;
6262 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6263
6264 /* Check the hidden versioned definition. */
6265 p = newname + namelen;
6266 *p++ = ELF_VER_CHR;
6267 memcpy (p, verstr, verlen + 1);
6268 newh = elf_link_hash_lookup (elf_hash_table (info),
6269 newname, FALSE, FALSE,
6270 FALSE);
6271 if (newh == NULL
6272 || (newh->root.type != bfd_link_hash_defined
6273 && newh->root.type != bfd_link_hash_defweak))
6274 {
6275 /* Check the default versioned definition. */
6276 *p++ = ELF_VER_CHR;
6277 memcpy (p, verstr, verlen + 1);
6278 newh = elf_link_hash_lookup (elf_hash_table (info),
6279 newname, FALSE, FALSE,
6280 FALSE);
6281 }
6282 free (newname);
6283
6284 /* Mark this version if there is a definition and it is
6285 not defined in a shared object. */
6286 if (newh != NULL
f5385ebf 6287 && !newh->def_dynamic
5a580b3a
AM
6288 && (newh->root.type == bfd_link_hash_defined
6289 || newh->root.type == bfd_link_hash_defweak))
6290 d->symver = 1;
6291 }
6292
6293 /* Attach all the symbols to their version information. */
5a580b3a 6294 asvinfo.info = info;
5a580b3a
AM
6295 asvinfo.failed = FALSE;
6296
6297 elf_link_hash_traverse (elf_hash_table (info),
6298 _bfd_elf_link_assign_sym_version,
6299 &asvinfo);
6300 if (asvinfo.failed)
6301 return FALSE;
6302
6303 if (!info->allow_undefined_version)
6304 {
6305 /* Check if all global versions have a definition. */
3d13f3e9 6306 bfd_boolean all_defined = TRUE;
fd91d419 6307 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6308 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6309 if (d->literal && !d->symver && !d->script)
5a580b3a 6310 {
4eca0228 6311 _bfd_error_handler
5a580b3a
AM
6312 (_("%s: undefined version: %s"),
6313 d->pattern, t->name);
6314 all_defined = FALSE;
6315 }
6316
6317 if (!all_defined)
6318 {
6319 bfd_set_error (bfd_error_bad_value);
6320 return FALSE;
6321 }
6322 }
6323
902e9fc7
MR
6324 /* Set up the version definition section. */
6325 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6326 BFD_ASSERT (s != NULL);
5a580b3a 6327
902e9fc7
MR
6328 /* We may have created additional version definitions if we are
6329 just linking a regular application. */
6330 verdefs = info->version_info;
5a580b3a 6331
902e9fc7
MR
6332 /* Skip anonymous version tag. */
6333 if (verdefs != NULL && verdefs->vernum == 0)
6334 verdefs = verdefs->next;
5a580b3a 6335
902e9fc7
MR
6336 if (verdefs == NULL && !info->create_default_symver)
6337 s->flags |= SEC_EXCLUDE;
6338 else
5a580b3a 6339 {
902e9fc7
MR
6340 unsigned int cdefs;
6341 bfd_size_type size;
6342 bfd_byte *p;
6343 Elf_Internal_Verdef def;
6344 Elf_Internal_Verdaux defaux;
6345 struct bfd_link_hash_entry *bh;
6346 struct elf_link_hash_entry *h;
6347 const char *name;
5a580b3a 6348
902e9fc7
MR
6349 cdefs = 0;
6350 size = 0;
5a580b3a 6351
902e9fc7
MR
6352 /* Make space for the base version. */
6353 size += sizeof (Elf_External_Verdef);
6354 size += sizeof (Elf_External_Verdaux);
6355 ++cdefs;
6356
6357 /* Make space for the default version. */
6358 if (info->create_default_symver)
6359 {
6360 size += sizeof (Elf_External_Verdef);
6361 ++cdefs;
3e3b46e5
PB
6362 }
6363
5a580b3a
AM
6364 for (t = verdefs; t != NULL; t = t->next)
6365 {
6366 struct bfd_elf_version_deps *n;
6367
a6cc6b3b
RO
6368 /* Don't emit base version twice. */
6369 if (t->vernum == 0)
6370 continue;
6371
5a580b3a
AM
6372 size += sizeof (Elf_External_Verdef);
6373 size += sizeof (Elf_External_Verdaux);
6374 ++cdefs;
6375
6376 for (n = t->deps; n != NULL; n = n->next)
6377 size += sizeof (Elf_External_Verdaux);
6378 }
6379
eea6121a 6380 s->size = size;
a50b1753 6381 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6382 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6383 return FALSE;
6384
6385 /* Fill in the version definition section. */
6386
6387 p = s->contents;
6388
6389 def.vd_version = VER_DEF_CURRENT;
6390 def.vd_flags = VER_FLG_BASE;
6391 def.vd_ndx = 1;
6392 def.vd_cnt = 1;
3e3b46e5
PB
6393 if (info->create_default_symver)
6394 {
6395 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6396 def.vd_next = sizeof (Elf_External_Verdef);
6397 }
6398 else
6399 {
6400 def.vd_aux = sizeof (Elf_External_Verdef);
6401 def.vd_next = (sizeof (Elf_External_Verdef)
6402 + sizeof (Elf_External_Verdaux));
6403 }
5a580b3a 6404
ef53be89 6405 if (soname_indx != (size_t) -1)
5a580b3a
AM
6406 {
6407 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6408 soname_indx);
6409 def.vd_hash = bfd_elf_hash (soname);
6410 defaux.vda_name = soname_indx;
3e3b46e5 6411 name = soname;
5a580b3a
AM
6412 }
6413 else
6414 {
ef53be89 6415 size_t indx;
5a580b3a 6416
06084812 6417 name = lbasename (output_bfd->filename);
5a580b3a
AM
6418 def.vd_hash = bfd_elf_hash (name);
6419 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6420 name, FALSE);
ef53be89 6421 if (indx == (size_t) -1)
5a580b3a
AM
6422 return FALSE;
6423 defaux.vda_name = indx;
6424 }
6425 defaux.vda_next = 0;
6426
6427 _bfd_elf_swap_verdef_out (output_bfd, &def,
6428 (Elf_External_Verdef *) p);
6429 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6430 if (info->create_default_symver)
6431 {
6432 /* Add a symbol representing this version. */
6433 bh = NULL;
6434 if (! (_bfd_generic_link_add_one_symbol
6435 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6436 0, NULL, FALSE,
6437 get_elf_backend_data (dynobj)->collect, &bh)))
6438 return FALSE;
6439 h = (struct elf_link_hash_entry *) bh;
6440 h->non_elf = 0;
6441 h->def_regular = 1;
6442 h->type = STT_OBJECT;
6443 h->verinfo.vertree = NULL;
6444
6445 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6446 return FALSE;
6447
6448 /* Create a duplicate of the base version with the same
6449 aux block, but different flags. */
6450 def.vd_flags = 0;
6451 def.vd_ndx = 2;
6452 def.vd_aux = sizeof (Elf_External_Verdef);
6453 if (verdefs)
6454 def.vd_next = (sizeof (Elf_External_Verdef)
6455 + sizeof (Elf_External_Verdaux));
6456 else
6457 def.vd_next = 0;
6458 _bfd_elf_swap_verdef_out (output_bfd, &def,
6459 (Elf_External_Verdef *) p);
6460 p += sizeof (Elf_External_Verdef);
6461 }
5a580b3a
AM
6462 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6463 (Elf_External_Verdaux *) p);
6464 p += sizeof (Elf_External_Verdaux);
6465
6466 for (t = verdefs; t != NULL; t = t->next)
6467 {
6468 unsigned int cdeps;
6469 struct bfd_elf_version_deps *n;
5a580b3a 6470
a6cc6b3b
RO
6471 /* Don't emit the base version twice. */
6472 if (t->vernum == 0)
6473 continue;
6474
5a580b3a
AM
6475 cdeps = 0;
6476 for (n = t->deps; n != NULL; n = n->next)
6477 ++cdeps;
6478
6479 /* Add a symbol representing this version. */
6480 bh = NULL;
6481 if (! (_bfd_generic_link_add_one_symbol
6482 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6483 0, NULL, FALSE,
6484 get_elf_backend_data (dynobj)->collect, &bh)))
6485 return FALSE;
6486 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6487 h->non_elf = 0;
6488 h->def_regular = 1;
5a580b3a
AM
6489 h->type = STT_OBJECT;
6490 h->verinfo.vertree = t;
6491
c152c796 6492 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6493 return FALSE;
6494
6495 def.vd_version = VER_DEF_CURRENT;
6496 def.vd_flags = 0;
6497 if (t->globals.list == NULL
6498 && t->locals.list == NULL
6499 && ! t->used)
6500 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6501 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6502 def.vd_cnt = cdeps + 1;
6503 def.vd_hash = bfd_elf_hash (t->name);
6504 def.vd_aux = sizeof (Elf_External_Verdef);
6505 def.vd_next = 0;
a6cc6b3b
RO
6506
6507 /* If a basever node is next, it *must* be the last node in
6508 the chain, otherwise Verdef construction breaks. */
6509 if (t->next != NULL && t->next->vernum == 0)
6510 BFD_ASSERT (t->next->next == NULL);
6511
6512 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6513 def.vd_next = (sizeof (Elf_External_Verdef)
6514 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6515
6516 _bfd_elf_swap_verdef_out (output_bfd, &def,
6517 (Elf_External_Verdef *) p);
6518 p += sizeof (Elf_External_Verdef);
6519
6520 defaux.vda_name = h->dynstr_index;
6521 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6522 h->dynstr_index);
6523 defaux.vda_next = 0;
6524 if (t->deps != NULL)
6525 defaux.vda_next = sizeof (Elf_External_Verdaux);
6526 t->name_indx = defaux.vda_name;
6527
6528 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6529 (Elf_External_Verdaux *) p);
6530 p += sizeof (Elf_External_Verdaux);
6531
6532 for (n = t->deps; n != NULL; n = n->next)
6533 {
6534 if (n->version_needed == NULL)
6535 {
6536 /* This can happen if there was an error in the
6537 version script. */
6538 defaux.vda_name = 0;
6539 }
6540 else
6541 {
6542 defaux.vda_name = n->version_needed->name_indx;
6543 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6544 defaux.vda_name);
6545 }
6546 if (n->next == NULL)
6547 defaux.vda_next = 0;
6548 else
6549 defaux.vda_next = sizeof (Elf_External_Verdaux);
6550
6551 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6552 (Elf_External_Verdaux *) p);
6553 p += sizeof (Elf_External_Verdaux);
6554 }
6555 }
6556
5a580b3a
AM
6557 elf_tdata (output_bfd)->cverdefs = cdefs;
6558 }
902e9fc7
MR
6559 }
6560
6561 bed = get_elf_backend_data (output_bfd);
6562
6563 if (info->gc_sections && bed->can_gc_sections)
6564 {
6565 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6566
6567 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6568 dynamic symbol table. */
902e9fc7
MR
6569 sweep_info.info = info;
6570 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6571 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6572 &sweep_info);
3d13f3e9
AM
6573 }
6574
6575 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6576 {
6577 asection *s;
6578 struct elf_find_verdep_info sinfo;
6579
6580 /* Work out the size of the version reference section. */
6581
6582 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6583 BFD_ASSERT (s != NULL);
902e9fc7 6584
3d13f3e9
AM
6585 sinfo.info = info;
6586 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6587 if (sinfo.vers == 0)
6588 sinfo.vers = 1;
6589 sinfo.failed = FALSE;
6590
6591 elf_link_hash_traverse (elf_hash_table (info),
6592 _bfd_elf_link_find_version_dependencies,
6593 &sinfo);
6594 if (sinfo.failed)
6595 return FALSE;
6596
6597 if (elf_tdata (output_bfd)->verref == NULL)
6598 s->flags |= SEC_EXCLUDE;
6599 else
6600 {
6601 Elf_Internal_Verneed *vn;
6602 unsigned int size;
6603 unsigned int crefs;
6604 bfd_byte *p;
6605
6606 /* Build the version dependency section. */
6607 size = 0;
6608 crefs = 0;
6609 for (vn = elf_tdata (output_bfd)->verref;
6610 vn != NULL;
6611 vn = vn->vn_nextref)
6612 {
6613 Elf_Internal_Vernaux *a;
6614
6615 size += sizeof (Elf_External_Verneed);
6616 ++crefs;
6617 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6618 size += sizeof (Elf_External_Vernaux);
6619 }
6620
6621 s->size = size;
6622 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6623 if (s->contents == NULL)
6624 return FALSE;
6625
6626 p = s->contents;
6627 for (vn = elf_tdata (output_bfd)->verref;
6628 vn != NULL;
6629 vn = vn->vn_nextref)
6630 {
6631 unsigned int caux;
6632 Elf_Internal_Vernaux *a;
6633 size_t indx;
6634
6635 caux = 0;
6636 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6637 ++caux;
6638
6639 vn->vn_version = VER_NEED_CURRENT;
6640 vn->vn_cnt = caux;
6641 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6642 elf_dt_name (vn->vn_bfd) != NULL
6643 ? elf_dt_name (vn->vn_bfd)
6644 : lbasename (vn->vn_bfd->filename),
6645 FALSE);
6646 if (indx == (size_t) -1)
6647 return FALSE;
6648 vn->vn_file = indx;
6649 vn->vn_aux = sizeof (Elf_External_Verneed);
6650 if (vn->vn_nextref == NULL)
6651 vn->vn_next = 0;
6652 else
6653 vn->vn_next = (sizeof (Elf_External_Verneed)
6654 + caux * sizeof (Elf_External_Vernaux));
6655
6656 _bfd_elf_swap_verneed_out (output_bfd, vn,
6657 (Elf_External_Verneed *) p);
6658 p += sizeof (Elf_External_Verneed);
6659
6660 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6661 {
6662 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6663 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6664 a->vna_nodename, FALSE);
6665 if (indx == (size_t) -1)
6666 return FALSE;
6667 a->vna_name = indx;
6668 if (a->vna_nextptr == NULL)
6669 a->vna_next = 0;
6670 else
6671 a->vna_next = sizeof (Elf_External_Vernaux);
6672
6673 _bfd_elf_swap_vernaux_out (output_bfd, a,
6674 (Elf_External_Vernaux *) p);
6675 p += sizeof (Elf_External_Vernaux);
6676 }
6677 }
6678
6679 elf_tdata (output_bfd)->cverrefs = crefs;
6680 }
902e9fc7
MR
6681 }
6682
6683 /* Any syms created from now on start with -1 in
6684 got.refcount/offset and plt.refcount/offset. */
6685 elf_hash_table (info)->init_got_refcount
6686 = elf_hash_table (info)->init_got_offset;
6687 elf_hash_table (info)->init_plt_refcount
6688 = elf_hash_table (info)->init_plt_offset;
6689
6690 if (bfd_link_relocatable (info)
6691 && !_bfd_elf_size_group_sections (info))
6692 return FALSE;
6693
6694 /* The backend may have to create some sections regardless of whether
6695 we're dynamic or not. */
6696 if (bed->elf_backend_always_size_sections
6697 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6698 return FALSE;
6699
6700 /* Determine any GNU_STACK segment requirements, after the backend
6701 has had a chance to set a default segment size. */
6702 if (info->execstack)
6703 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6704 else if (info->noexecstack)
6705 elf_stack_flags (output_bfd) = PF_R | PF_W;
6706 else
6707 {
6708 bfd *inputobj;
6709 asection *notesec = NULL;
6710 int exec = 0;
6711
6712 for (inputobj = info->input_bfds;
6713 inputobj;
6714 inputobj = inputobj->link.next)
6715 {
6716 asection *s;
6717
6718 if (inputobj->flags
6719 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6720 continue;
57963c05
AM
6721 s = inputobj->sections;
6722 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6723 continue;
6724
902e9fc7
MR
6725 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6726 if (s)
6727 {
6728 if (s->flags & SEC_CODE)
6729 exec = PF_X;
6730 notesec = s;
6731 }
6732 else if (bed->default_execstack)
6733 exec = PF_X;
6734 }
6735 if (notesec || info->stacksize > 0)
6736 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6737 if (notesec && exec && bfd_link_relocatable (info)
6738 && notesec->output_section != bfd_abs_section_ptr)
6739 notesec->output_section->flags |= SEC_CODE;
6740 }
6741
6742 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6743 {
6744 struct elf_info_failed eif;
6745 struct elf_link_hash_entry *h;
6746 asection *dynstr;
6747 asection *s;
6748
6749 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6750 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6751
902e9fc7
MR
6752 if (info->symbolic)
6753 {
6754 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6755 return FALSE;
6756 info->flags |= DF_SYMBOLIC;
6757 }
6758
6759 if (rpath != NULL)
6760 {
6761 size_t indx;
6762 bfd_vma tag;
6763
6764 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6765 TRUE);
6766 if (indx == (size_t) -1)
6767 return FALSE;
6768
6769 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6770 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6771 return FALSE;
6772 }
6773
6774 if (filter_shlib != NULL)
6775 {
6776 size_t indx;
6777
6778 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6779 filter_shlib, TRUE);
6780 if (indx == (size_t) -1
6781 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6782 return FALSE;
6783 }
6784
6785 if (auxiliary_filters != NULL)
6786 {
6787 const char * const *p;
6788
6789 for (p = auxiliary_filters; *p != NULL; p++)
6790 {
6791 size_t indx;
6792
6793 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6794 *p, TRUE);
6795 if (indx == (size_t) -1
6796 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6797 return FALSE;
6798 }
6799 }
6800
6801 if (audit != NULL)
6802 {
6803 size_t indx;
6804
6805 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6806 TRUE);
6807 if (indx == (size_t) -1
6808 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6809 return FALSE;
6810 }
6811
6812 if (depaudit != NULL)
6813 {
6814 size_t indx;
6815
6816 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6817 TRUE);
6818 if (indx == (size_t) -1
6819 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6820 return FALSE;
6821 }
6822
6823 eif.info = info;
6824 eif.failed = FALSE;
6825
6826 /* Find all symbols which were defined in a dynamic object and make
6827 the backend pick a reasonable value for them. */
6828 elf_link_hash_traverse (elf_hash_table (info),
6829 _bfd_elf_adjust_dynamic_symbol,
6830 &eif);
6831 if (eif.failed)
6832 return FALSE;
6833
6834 /* Add some entries to the .dynamic section. We fill in some of the
6835 values later, in bfd_elf_final_link, but we must add the entries
6836 now so that we know the final size of the .dynamic section. */
6837
6838 /* If there are initialization and/or finalization functions to
6839 call then add the corresponding DT_INIT/DT_FINI entries. */
6840 h = (info->init_function
6841 ? elf_link_hash_lookup (elf_hash_table (info),
6842 info->init_function, FALSE,
6843 FALSE, FALSE)
6844 : NULL);
6845 if (h != NULL
6846 && (h->ref_regular
6847 || h->def_regular))
6848 {
6849 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6850 return FALSE;
6851 }
6852 h = (info->fini_function
6853 ? elf_link_hash_lookup (elf_hash_table (info),
6854 info->fini_function, FALSE,
6855 FALSE, FALSE)
6856 : NULL);
6857 if (h != NULL
6858 && (h->ref_regular
6859 || h->def_regular))
6860 {
6861 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6862 return FALSE;
6863 }
6864
6865 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6866 if (s != NULL && s->linker_has_input)
6867 {
6868 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6869 if (! bfd_link_executable (info))
6870 {
6871 bfd *sub;
6872 asection *o;
6873
57963c05
AM
6874 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6875 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6876 && (o = sub->sections) != NULL
6877 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6878 for (o = sub->sections; o != NULL; o = o->next)
6879 if (elf_section_data (o)->this_hdr.sh_type
6880 == SHT_PREINIT_ARRAY)
6881 {
6882 _bfd_error_handler
871b3ab2 6883 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6884 sub);
6885 break;
6886 }
6887
6888 bfd_set_error (bfd_error_nonrepresentable_section);
6889 return FALSE;
6890 }
6891
6892 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6893 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6894 return FALSE;
6895 }
6896 s = bfd_get_section_by_name (output_bfd, ".init_array");
6897 if (s != NULL && s->linker_has_input)
6898 {
6899 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6900 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6901 return FALSE;
6902 }
6903 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6904 if (s != NULL && s->linker_has_input)
6905 {
6906 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6907 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6908 return FALSE;
6909 }
6910
6911 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6912 /* If .dynstr is excluded from the link, we don't want any of
6913 these tags. Strictly, we should be checking each section
6914 individually; This quick check covers for the case where
6915 someone does a /DISCARD/ : { *(*) }. */
6916 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6917 {
6918 bfd_size_type strsize;
6919
6920 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6921 if ((info->emit_hash
6922 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6923 || (info->emit_gnu_hash
6924 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6925 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6926 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6927 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6928 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6929 bed->s->sizeof_sym))
6930 return FALSE;
6931 }
6932 }
6933
6934 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6935 return FALSE;
6936
6937 /* The backend must work out the sizes of all the other dynamic
6938 sections. */
6939 if (dynobj != NULL
6940 && bed->elf_backend_size_dynamic_sections != NULL
6941 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6942 return FALSE;
6943
6944 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6945 {
902e9fc7
MR
6946 if (elf_tdata (output_bfd)->cverdefs)
6947 {
6948 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6949
6950 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6951 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6952 return FALSE;
6953 }
6954
6955 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6956 {
6957 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6958 return FALSE;
6959 }
6960 else if (info->flags & DF_BIND_NOW)
6961 {
6962 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6963 return FALSE;
6964 }
6965
6966 if (info->flags_1)
6967 {
6968 if (bfd_link_executable (info))
6969 info->flags_1 &= ~ (DF_1_INITFIRST
6970 | DF_1_NODELETE
6971 | DF_1_NOOPEN);
6972 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6973 return FALSE;
6974 }
6975
6976 if (elf_tdata (output_bfd)->cverrefs)
6977 {
6978 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6979
6980 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6981 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6982 return FALSE;
6983 }
5a580b3a 6984
8423293d
AM
6985 if ((elf_tdata (output_bfd)->cverrefs == 0
6986 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 6987 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 6988 {
902e9fc7
MR
6989 asection *s;
6990
3d4d4302 6991 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6992 s->flags |= SEC_EXCLUDE;
6993 }
6994 }
6995 return TRUE;
6996}
6997
74541ad4
AM
6998/* Find the first non-excluded output section. We'll use its
6999 section symbol for some emitted relocs. */
7000void
7001_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7002{
7003 asection *s;
7004
7005 for (s = output_bfd->sections; s != NULL; s = s->next)
7006 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7007 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
7008 {
7009 elf_hash_table (info)->text_index_section = s;
7010 break;
7011 }
7012}
7013
7014/* Find two non-excluded output sections, one for code, one for data.
7015 We'll use their section symbols for some emitted relocs. */
7016void
7017_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7018{
7019 asection *s;
7020
266b05cf 7021 /* Data first, since setting text_index_section changes
7f923b7f 7022 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7023 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 7024 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 7025 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7026 {
266b05cf 7027 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
7028 break;
7029 }
7030
7031 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
7032 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7033 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 7034 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7035 {
266b05cf 7036 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
7037 break;
7038 }
7039
7040 if (elf_hash_table (info)->text_index_section == NULL)
7041 elf_hash_table (info)->text_index_section
7042 = elf_hash_table (info)->data_index_section;
7043}
7044
8423293d
AM
7045bfd_boolean
7046bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7047{
74541ad4 7048 const struct elf_backend_data *bed;
23ec1e32 7049 unsigned long section_sym_count;
96d01d93 7050 bfd_size_type dynsymcount = 0;
74541ad4 7051
8423293d
AM
7052 if (!is_elf_hash_table (info->hash))
7053 return TRUE;
7054
74541ad4
AM
7055 bed = get_elf_backend_data (output_bfd);
7056 (*bed->elf_backend_init_index_section) (output_bfd, info);
7057
23ec1e32
MR
7058 /* Assign dynsym indices. In a shared library we generate a section
7059 symbol for each output section, which come first. Next come all
7060 of the back-end allocated local dynamic syms, followed by the rest
7061 of the global symbols.
7062
7063 This is usually not needed for static binaries, however backends
7064 can request to always do it, e.g. the MIPS backend uses dynamic
7065 symbol counts to lay out GOT, which will be produced in the
7066 presence of GOT relocations even in static binaries (holding fixed
7067 data in that case, to satisfy those relocations). */
7068
7069 if (elf_hash_table (info)->dynamic_sections_created
7070 || bed->always_renumber_dynsyms)
7071 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7072 &section_sym_count);
7073
8423293d
AM
7074 if (elf_hash_table (info)->dynamic_sections_created)
7075 {
7076 bfd *dynobj;
8423293d 7077 asection *s;
8423293d
AM
7078 unsigned int dtagcount;
7079
7080 dynobj = elf_hash_table (info)->dynobj;
7081
5a580b3a 7082 /* Work out the size of the symbol version section. */
3d4d4302 7083 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7084 BFD_ASSERT (s != NULL);
d5486c43 7085 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7086 {
eea6121a 7087 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7088 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7089 if (s->contents == NULL)
7090 return FALSE;
7091
7092 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7093 return FALSE;
7094 }
7095
7096 /* Set the size of the .dynsym and .hash sections. We counted
7097 the number of dynamic symbols in elf_link_add_object_symbols.
7098 We will build the contents of .dynsym and .hash when we build
7099 the final symbol table, because until then we do not know the
7100 correct value to give the symbols. We built the .dynstr
7101 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7102 s = elf_hash_table (info)->dynsym;
5a580b3a 7103 BFD_ASSERT (s != NULL);
eea6121a 7104 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7105
d5486c43
L
7106 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7107 if (s->contents == NULL)
7108 return FALSE;
5a580b3a 7109
d5486c43
L
7110 /* The first entry in .dynsym is a dummy symbol. Clear all the
7111 section syms, in case we don't output them all. */
7112 ++section_sym_count;
7113 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7114
fdc90cb4
JJ
7115 elf_hash_table (info)->bucketcount = 0;
7116
5a580b3a
AM
7117 /* Compute the size of the hashing table. As a side effect this
7118 computes the hash values for all the names we export. */
fdc90cb4
JJ
7119 if (info->emit_hash)
7120 {
7121 unsigned long int *hashcodes;
14b1c01e 7122 struct hash_codes_info hashinf;
fdc90cb4
JJ
7123 bfd_size_type amt;
7124 unsigned long int nsyms;
7125 size_t bucketcount;
7126 size_t hash_entry_size;
7127
7128 /* Compute the hash values for all exported symbols. At the same
7129 time store the values in an array so that we could use them for
7130 optimizations. */
7131 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7132 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7133 if (hashcodes == NULL)
7134 return FALSE;
14b1c01e
AM
7135 hashinf.hashcodes = hashcodes;
7136 hashinf.error = FALSE;
5a580b3a 7137
fdc90cb4
JJ
7138 /* Put all hash values in HASHCODES. */
7139 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7140 elf_collect_hash_codes, &hashinf);
7141 if (hashinf.error)
4dd07732
AM
7142 {
7143 free (hashcodes);
7144 return FALSE;
7145 }
5a580b3a 7146
14b1c01e 7147 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7148 bucketcount
7149 = compute_bucket_count (info, hashcodes, nsyms, 0);
7150 free (hashcodes);
7151
4b48e2f6 7152 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7153 return FALSE;
5a580b3a 7154
fdc90cb4
JJ
7155 elf_hash_table (info)->bucketcount = bucketcount;
7156
3d4d4302 7157 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7158 BFD_ASSERT (s != NULL);
7159 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7160 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7161 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7162 if (s->contents == NULL)
7163 return FALSE;
7164
7165 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7166 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7167 s->contents + hash_entry_size);
7168 }
7169
7170 if (info->emit_gnu_hash)
7171 {
7172 size_t i, cnt;
7173 unsigned char *contents;
7174 struct collect_gnu_hash_codes cinfo;
7175 bfd_size_type amt;
7176 size_t bucketcount;
7177
7178 memset (&cinfo, 0, sizeof (cinfo));
7179
7180 /* Compute the hash values for all exported symbols. At the same
7181 time store the values in an array so that we could use them for
7182 optimizations. */
7183 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7184 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7185 if (cinfo.hashcodes == NULL)
7186 return FALSE;
7187
7188 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7189 cinfo.min_dynindx = -1;
7190 cinfo.output_bfd = output_bfd;
7191 cinfo.bed = bed;
7192
7193 /* Put all hash values in HASHCODES. */
7194 elf_link_hash_traverse (elf_hash_table (info),
7195 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7196 if (cinfo.error)
4dd07732
AM
7197 {
7198 free (cinfo.hashcodes);
7199 return FALSE;
7200 }
fdc90cb4
JJ
7201
7202 bucketcount
7203 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7204
7205 if (bucketcount == 0)
7206 {
7207 free (cinfo.hashcodes);
7208 return FALSE;
7209 }
7210
3d4d4302 7211 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7212 BFD_ASSERT (s != NULL);
7213
7214 if (cinfo.nsyms == 0)
7215 {
7216 /* Empty .gnu.hash section is special. */
7217 BFD_ASSERT (cinfo.min_dynindx == -1);
7218 free (cinfo.hashcodes);
7219 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7220 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7221 if (contents == NULL)
7222 return FALSE;
7223 s->contents = contents;
7224 /* 1 empty bucket. */
7225 bfd_put_32 (output_bfd, 1, contents);
7226 /* SYMIDX above the special symbol 0. */
7227 bfd_put_32 (output_bfd, 1, contents + 4);
7228 /* Just one word for bitmask. */
7229 bfd_put_32 (output_bfd, 1, contents + 8);
7230 /* Only hash fn bloom filter. */
7231 bfd_put_32 (output_bfd, 0, contents + 12);
7232 /* No hashes are valid - empty bitmask. */
7233 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7234 /* No hashes in the only bucket. */
7235 bfd_put_32 (output_bfd, 0,
7236 contents + 16 + bed->s->arch_size / 8);
7237 }
7238 else
7239 {
9e6619e2 7240 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7241 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7242
9e6619e2
AM
7243 x = cinfo.nsyms;
7244 maskbitslog2 = 1;
7245 while ((x >>= 1) != 0)
7246 ++maskbitslog2;
fdc90cb4
JJ
7247 if (maskbitslog2 < 3)
7248 maskbitslog2 = 5;
7249 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7250 maskbitslog2 = maskbitslog2 + 3;
7251 else
7252 maskbitslog2 = maskbitslog2 + 2;
7253 if (bed->s->arch_size == 64)
7254 {
7255 if (maskbitslog2 == 5)
7256 maskbitslog2 = 6;
7257 cinfo.shift1 = 6;
7258 }
7259 else
7260 cinfo.shift1 = 5;
7261 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7262 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7263 cinfo.maskbits = 1 << maskbitslog2;
7264 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7265 amt = bucketcount * sizeof (unsigned long int) * 2;
7266 amt += maskwords * sizeof (bfd_vma);
a50b1753 7267 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7268 if (cinfo.bitmask == NULL)
7269 {
7270 free (cinfo.hashcodes);
7271 return FALSE;
7272 }
7273
a50b1753 7274 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7275 cinfo.indx = cinfo.counts + bucketcount;
7276 cinfo.symindx = dynsymcount - cinfo.nsyms;
7277 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7278
7279 /* Determine how often each hash bucket is used. */
7280 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7281 for (i = 0; i < cinfo.nsyms; ++i)
7282 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7283
7284 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7285 if (cinfo.counts[i] != 0)
7286 {
7287 cinfo.indx[i] = cnt;
7288 cnt += cinfo.counts[i];
7289 }
7290 BFD_ASSERT (cnt == dynsymcount);
7291 cinfo.bucketcount = bucketcount;
7292 cinfo.local_indx = cinfo.min_dynindx;
7293
7294 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7295 s->size += cinfo.maskbits / 8;
a50b1753 7296 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7297 if (contents == NULL)
7298 {
7299 free (cinfo.bitmask);
7300 free (cinfo.hashcodes);
7301 return FALSE;
7302 }
7303
7304 s->contents = contents;
7305 bfd_put_32 (output_bfd, bucketcount, contents);
7306 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7307 bfd_put_32 (output_bfd, maskwords, contents + 8);
7308 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7309 contents += 16 + cinfo.maskbits / 8;
7310
7311 for (i = 0; i < bucketcount; ++i)
7312 {
7313 if (cinfo.counts[i] == 0)
7314 bfd_put_32 (output_bfd, 0, contents);
7315 else
7316 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7317 contents += 4;
7318 }
7319
7320 cinfo.contents = contents;
7321
7322 /* Renumber dynamic symbols, populate .gnu.hash section. */
7323 elf_link_hash_traverse (elf_hash_table (info),
7324 elf_renumber_gnu_hash_syms, &cinfo);
7325
7326 contents = s->contents + 16;
7327 for (i = 0; i < maskwords; ++i)
7328 {
7329 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7330 contents);
7331 contents += bed->s->arch_size / 8;
7332 }
7333
7334 free (cinfo.bitmask);
7335 free (cinfo.hashcodes);
7336 }
7337 }
5a580b3a 7338
3d4d4302 7339 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7340 BFD_ASSERT (s != NULL);
7341
4ad4eba5 7342 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7343
eea6121a 7344 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7345
7346 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7347 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7348 return FALSE;
7349 }
7350
7351 return TRUE;
7352}
4d269e42 7353\f
4d269e42
AM
7354/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7355
7356static void
7357merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7358 asection *sec)
7359{
dbaa2011
AM
7360 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7361 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7362}
7363
7364/* Finish SHF_MERGE section merging. */
7365
7366bfd_boolean
630993ec 7367_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7368{
7369 bfd *ibfd;
7370 asection *sec;
7371
7372 if (!is_elf_hash_table (info->hash))
7373 return FALSE;
7374
c72f2fb2 7375 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7376 if ((ibfd->flags & DYNAMIC) == 0
7377 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7378 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7379 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7380 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7381 if ((sec->flags & SEC_MERGE) != 0
7382 && !bfd_is_abs_section (sec->output_section))
7383 {
7384 struct bfd_elf_section_data *secdata;
7385
7386 secdata = elf_section_data (sec);
630993ec 7387 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7388 &elf_hash_table (info)->merge_info,
7389 sec, &secdata->sec_info))
7390 return FALSE;
7391 else if (secdata->sec_info)
dbaa2011 7392 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7393 }
7394
7395 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7396 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7397 merge_sections_remove_hook);
7398 return TRUE;
7399}
7400
7401/* Create an entry in an ELF linker hash table. */
7402
7403struct bfd_hash_entry *
7404_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7405 struct bfd_hash_table *table,
7406 const char *string)
7407{
7408 /* Allocate the structure if it has not already been allocated by a
7409 subclass. */
7410 if (entry == NULL)
7411 {
a50b1753 7412 entry = (struct bfd_hash_entry *)
ca4be51c 7413 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7414 if (entry == NULL)
7415 return entry;
7416 }
7417
7418 /* Call the allocation method of the superclass. */
7419 entry = _bfd_link_hash_newfunc (entry, table, string);
7420 if (entry != NULL)
7421 {
7422 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7423 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7424
7425 /* Set local fields. */
7426 ret->indx = -1;
7427 ret->dynindx = -1;
7428 ret->got = htab->init_got_refcount;
7429 ret->plt = htab->init_plt_refcount;
7430 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7431 - offsetof (struct elf_link_hash_entry, size)));
7432 /* Assume that we have been called by a non-ELF symbol reader.
7433 This flag is then reset by the code which reads an ELF input
7434 file. This ensures that a symbol created by a non-ELF symbol
7435 reader will have the flag set correctly. */
7436 ret->non_elf = 1;
7437 }
7438
7439 return entry;
7440}
7441
7442/* Copy data from an indirect symbol to its direct symbol, hiding the
7443 old indirect symbol. Also used for copying flags to a weakdef. */
7444
7445void
7446_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7447 struct elf_link_hash_entry *dir,
7448 struct elf_link_hash_entry *ind)
7449{
7450 struct elf_link_hash_table *htab;
7451
7452 /* Copy down any references that we may have already seen to the
e81830c5 7453 symbol which just became indirect. */
4d269e42 7454
422f1182 7455 if (dir->versioned != versioned_hidden)
e81830c5
AM
7456 dir->ref_dynamic |= ind->ref_dynamic;
7457 dir->ref_regular |= ind->ref_regular;
7458 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7459 dir->non_got_ref |= ind->non_got_ref;
7460 dir->needs_plt |= ind->needs_plt;
7461 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7462
7463 if (ind->root.type != bfd_link_hash_indirect)
7464 return;
7465
7466 /* Copy over the global and procedure linkage table refcount entries.
7467 These may have been already set up by a check_relocs routine. */
7468 htab = elf_hash_table (info);
7469 if (ind->got.refcount > htab->init_got_refcount.refcount)
7470 {
7471 if (dir->got.refcount < 0)
7472 dir->got.refcount = 0;
7473 dir->got.refcount += ind->got.refcount;
7474 ind->got.refcount = htab->init_got_refcount.refcount;
7475 }
7476
7477 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7478 {
7479 if (dir->plt.refcount < 0)
7480 dir->plt.refcount = 0;
7481 dir->plt.refcount += ind->plt.refcount;
7482 ind->plt.refcount = htab->init_plt_refcount.refcount;
7483 }
7484
7485 if (ind->dynindx != -1)
7486 {
7487 if (dir->dynindx != -1)
7488 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7489 dir->dynindx = ind->dynindx;
7490 dir->dynstr_index = ind->dynstr_index;
7491 ind->dynindx = -1;
7492 ind->dynstr_index = 0;
7493 }
7494}
7495
7496void
7497_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7498 struct elf_link_hash_entry *h,
7499 bfd_boolean force_local)
7500{
3aa14d16
L
7501 /* STT_GNU_IFUNC symbol must go through PLT. */
7502 if (h->type != STT_GNU_IFUNC)
7503 {
7504 h->plt = elf_hash_table (info)->init_plt_offset;
7505 h->needs_plt = 0;
7506 }
4d269e42
AM
7507 if (force_local)
7508 {
7509 h->forced_local = 1;
7510 if (h->dynindx != -1)
7511 {
4d269e42
AM
7512 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7513 h->dynstr_index);
641338d8
AM
7514 h->dynindx = -1;
7515 h->dynstr_index = 0;
4d269e42
AM
7516 }
7517 }
7518}
7519
34a87bb0
L
7520/* Hide a symbol. */
7521
7522void
7523_bfd_elf_link_hide_symbol (bfd *output_bfd,
7524 struct bfd_link_info *info,
7525 struct bfd_link_hash_entry *h)
7526{
7527 if (is_elf_hash_table (info->hash))
7528 {
7529 const struct elf_backend_data *bed
7530 = get_elf_backend_data (output_bfd);
7531 struct elf_link_hash_entry *eh
7532 = (struct elf_link_hash_entry *) h;
7533 bed->elf_backend_hide_symbol (info, eh, TRUE);
7534 eh->def_dynamic = 0;
7535 eh->ref_dynamic = 0;
7536 eh->dynamic_def = 0;
7537 }
7538}
7539
7bf52ea2
AM
7540/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7541 caller. */
4d269e42
AM
7542
7543bfd_boolean
7544_bfd_elf_link_hash_table_init
7545 (struct elf_link_hash_table *table,
7546 bfd *abfd,
7547 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7548 struct bfd_hash_table *,
7549 const char *),
4dfe6ac6
NC
7550 unsigned int entsize,
7551 enum elf_target_id target_id)
4d269e42
AM
7552{
7553 bfd_boolean ret;
7554 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7555
4d269e42
AM
7556 table->init_got_refcount.refcount = can_refcount - 1;
7557 table->init_plt_refcount.refcount = can_refcount - 1;
7558 table->init_got_offset.offset = -(bfd_vma) 1;
7559 table->init_plt_offset.offset = -(bfd_vma) 1;
7560 /* The first dynamic symbol is a dummy. */
7561 table->dynsymcount = 1;
7562
7563 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7564
4d269e42 7565 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7566 table->hash_table_id = target_id;
4d269e42
AM
7567
7568 return ret;
7569}
7570
7571/* Create an ELF linker hash table. */
7572
7573struct bfd_link_hash_table *
7574_bfd_elf_link_hash_table_create (bfd *abfd)
7575{
7576 struct elf_link_hash_table *ret;
7577 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7578
7bf52ea2 7579 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7580 if (ret == NULL)
7581 return NULL;
7582
7583 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7584 sizeof (struct elf_link_hash_entry),
7585 GENERIC_ELF_DATA))
4d269e42
AM
7586 {
7587 free (ret);
7588 return NULL;
7589 }
d495ab0d 7590 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7591
7592 return &ret->root;
7593}
7594
9f7c3e5e
AM
7595/* Destroy an ELF linker hash table. */
7596
7597void
d495ab0d 7598_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7599{
d495ab0d
AM
7600 struct elf_link_hash_table *htab;
7601
7602 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7603 if (htab->dynstr != NULL)
7604 _bfd_elf_strtab_free (htab->dynstr);
7605 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7606 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7607}
7608
4d269e42
AM
7609/* This is a hook for the ELF emulation code in the generic linker to
7610 tell the backend linker what file name to use for the DT_NEEDED
7611 entry for a dynamic object. */
7612
7613void
7614bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7615{
7616 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7617 && bfd_get_format (abfd) == bfd_object)
7618 elf_dt_name (abfd) = name;
7619}
7620
7621int
7622bfd_elf_get_dyn_lib_class (bfd *abfd)
7623{
7624 int lib_class;
7625 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7626 && bfd_get_format (abfd) == bfd_object)
7627 lib_class = elf_dyn_lib_class (abfd);
7628 else
7629 lib_class = 0;
7630 return lib_class;
7631}
7632
7633void
7634bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7635{
7636 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7637 && bfd_get_format (abfd) == bfd_object)
7638 elf_dyn_lib_class (abfd) = lib_class;
7639}
7640
7641/* Get the list of DT_NEEDED entries for a link. This is a hook for
7642 the linker ELF emulation code. */
7643
7644struct bfd_link_needed_list *
7645bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7646 struct bfd_link_info *info)
7647{
7648 if (! is_elf_hash_table (info->hash))
7649 return NULL;
7650 return elf_hash_table (info)->needed;
7651}
7652
7653/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7654 hook for the linker ELF emulation code. */
7655
7656struct bfd_link_needed_list *
7657bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7658 struct bfd_link_info *info)
7659{
7660 if (! is_elf_hash_table (info->hash))
7661 return NULL;
7662 return elf_hash_table (info)->runpath;
7663}
7664
7665/* Get the name actually used for a dynamic object for a link. This
7666 is the SONAME entry if there is one. Otherwise, it is the string
7667 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7668
7669const char *
7670bfd_elf_get_dt_soname (bfd *abfd)
7671{
7672 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7673 && bfd_get_format (abfd) == bfd_object)
7674 return elf_dt_name (abfd);
7675 return NULL;
7676}
7677
7678/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7679 the ELF linker emulation code. */
7680
7681bfd_boolean
7682bfd_elf_get_bfd_needed_list (bfd *abfd,
7683 struct bfd_link_needed_list **pneeded)
7684{
7685 asection *s;
7686 bfd_byte *dynbuf = NULL;
cb33740c 7687 unsigned int elfsec;
4d269e42
AM
7688 unsigned long shlink;
7689 bfd_byte *extdyn, *extdynend;
7690 size_t extdynsize;
7691 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7692
7693 *pneeded = NULL;
7694
7695 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7696 || bfd_get_format (abfd) != bfd_object)
7697 return TRUE;
7698
7699 s = bfd_get_section_by_name (abfd, ".dynamic");
7700 if (s == NULL || s->size == 0)
7701 return TRUE;
7702
7703 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7704 goto error_return;
7705
7706 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7707 if (elfsec == SHN_BAD)
4d269e42
AM
7708 goto error_return;
7709
7710 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7711
4d269e42
AM
7712 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7713 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7714
7715 extdyn = dynbuf;
7716 extdynend = extdyn + s->size;
7717 for (; extdyn < extdynend; extdyn += extdynsize)
7718 {
7719 Elf_Internal_Dyn dyn;
7720
7721 (*swap_dyn_in) (abfd, extdyn, &dyn);
7722
7723 if (dyn.d_tag == DT_NULL)
7724 break;
7725
7726 if (dyn.d_tag == DT_NEEDED)
7727 {
7728 const char *string;
7729 struct bfd_link_needed_list *l;
7730 unsigned int tagv = dyn.d_un.d_val;
7731 bfd_size_type amt;
7732
7733 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7734 if (string == NULL)
7735 goto error_return;
7736
7737 amt = sizeof *l;
a50b1753 7738 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7739 if (l == NULL)
7740 goto error_return;
7741
7742 l->by = abfd;
7743 l->name = string;
7744 l->next = *pneeded;
7745 *pneeded = l;
7746 }
7747 }
7748
7749 free (dynbuf);
7750
7751 return TRUE;
7752
7753 error_return:
7754 if (dynbuf != NULL)
7755 free (dynbuf);
7756 return FALSE;
7757}
7758
7759struct elf_symbuf_symbol
7760{
7761 unsigned long st_name; /* Symbol name, index in string tbl */
7762 unsigned char st_info; /* Type and binding attributes */
7763 unsigned char st_other; /* Visibilty, and target specific */
7764};
7765
7766struct elf_symbuf_head
7767{
7768 struct elf_symbuf_symbol *ssym;
ef53be89 7769 size_t count;
4d269e42
AM
7770 unsigned int st_shndx;
7771};
7772
7773struct elf_symbol
7774{
7775 union
7776 {
7777 Elf_Internal_Sym *isym;
7778 struct elf_symbuf_symbol *ssym;
7779 } u;
7780 const char *name;
7781};
7782
7783/* Sort references to symbols by ascending section number. */
7784
7785static int
7786elf_sort_elf_symbol (const void *arg1, const void *arg2)
7787{
7788 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7789 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7790
7791 return s1->st_shndx - s2->st_shndx;
7792}
7793
7794static int
7795elf_sym_name_compare (const void *arg1, const void *arg2)
7796{
7797 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7798 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7799 return strcmp (s1->name, s2->name);
7800}
7801
7802static struct elf_symbuf_head *
ef53be89 7803elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7804{
14b1c01e 7805 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7806 struct elf_symbuf_symbol *ssym;
7807 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7808 size_t i, shndx_count, total_size;
4d269e42 7809
a50b1753 7810 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7811 if (indbuf == NULL)
7812 return NULL;
7813
7814 for (ind = indbuf, i = 0; i < symcount; i++)
7815 if (isymbuf[i].st_shndx != SHN_UNDEF)
7816 *ind++ = &isymbuf[i];
7817 indbufend = ind;
7818
7819 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7820 elf_sort_elf_symbol);
7821
7822 shndx_count = 0;
7823 if (indbufend > indbuf)
7824 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7825 if (ind[0]->st_shndx != ind[1]->st_shndx)
7826 shndx_count++;
7827
3ae181ee
L
7828 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7829 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7830 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7831 if (ssymbuf == NULL)
7832 {
7833 free (indbuf);
7834 return NULL;
7835 }
7836
3ae181ee 7837 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7838 ssymbuf->ssym = NULL;
7839 ssymbuf->count = shndx_count;
7840 ssymbuf->st_shndx = 0;
7841 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7842 {
7843 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7844 {
7845 ssymhead++;
7846 ssymhead->ssym = ssym;
7847 ssymhead->count = 0;
7848 ssymhead->st_shndx = (*ind)->st_shndx;
7849 }
7850 ssym->st_name = (*ind)->st_name;
7851 ssym->st_info = (*ind)->st_info;
7852 ssym->st_other = (*ind)->st_other;
7853 ssymhead->count++;
7854 }
ef53be89 7855 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7856 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7857 == total_size));
4d269e42
AM
7858
7859 free (indbuf);
7860 return ssymbuf;
7861}
7862
7863/* Check if 2 sections define the same set of local and global
7864 symbols. */
7865
8f317e31 7866static bfd_boolean
4d269e42
AM
7867bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7868 struct bfd_link_info *info)
7869{
7870 bfd *bfd1, *bfd2;
7871 const struct elf_backend_data *bed1, *bed2;
7872 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7873 size_t symcount1, symcount2;
4d269e42
AM
7874 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7875 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7876 Elf_Internal_Sym *isym, *isymend;
7877 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7878 size_t count1, count2, i;
cb33740c 7879 unsigned int shndx1, shndx2;
4d269e42
AM
7880 bfd_boolean result;
7881
7882 bfd1 = sec1->owner;
7883 bfd2 = sec2->owner;
7884
4d269e42
AM
7885 /* Both sections have to be in ELF. */
7886 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7887 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7888 return FALSE;
7889
7890 if (elf_section_type (sec1) != elf_section_type (sec2))
7891 return FALSE;
7892
4d269e42
AM
7893 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7894 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7895 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7896 return FALSE;
7897
7898 bed1 = get_elf_backend_data (bfd1);
7899 bed2 = get_elf_backend_data (bfd2);
7900 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7901 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7902 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7903 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7904
7905 if (symcount1 == 0 || symcount2 == 0)
7906 return FALSE;
7907
7908 result = FALSE;
7909 isymbuf1 = NULL;
7910 isymbuf2 = NULL;
a50b1753
NC
7911 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7912 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7913
7914 if (ssymbuf1 == NULL)
7915 {
7916 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7917 NULL, NULL, NULL);
7918 if (isymbuf1 == NULL)
7919 goto done;
7920
7921 if (!info->reduce_memory_overheads)
7922 elf_tdata (bfd1)->symbuf = ssymbuf1
7923 = elf_create_symbuf (symcount1, isymbuf1);
7924 }
7925
7926 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7927 {
7928 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7929 NULL, NULL, NULL);
7930 if (isymbuf2 == NULL)
7931 goto done;
7932
7933 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7934 elf_tdata (bfd2)->symbuf = ssymbuf2
7935 = elf_create_symbuf (symcount2, isymbuf2);
7936 }
7937
7938 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7939 {
7940 /* Optimized faster version. */
ef53be89 7941 size_t lo, hi, mid;
4d269e42
AM
7942 struct elf_symbol *symp;
7943 struct elf_symbuf_symbol *ssym, *ssymend;
7944
7945 lo = 0;
7946 hi = ssymbuf1->count;
7947 ssymbuf1++;
7948 count1 = 0;
7949 while (lo < hi)
7950 {
7951 mid = (lo + hi) / 2;
cb33740c 7952 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7953 hi = mid;
cb33740c 7954 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7955 lo = mid + 1;
7956 else
7957 {
7958 count1 = ssymbuf1[mid].count;
7959 ssymbuf1 += mid;
7960 break;
7961 }
7962 }
7963
7964 lo = 0;
7965 hi = ssymbuf2->count;
7966 ssymbuf2++;
7967 count2 = 0;
7968 while (lo < hi)
7969 {
7970 mid = (lo + hi) / 2;
cb33740c 7971 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7972 hi = mid;
cb33740c 7973 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7974 lo = mid + 1;
7975 else
7976 {
7977 count2 = ssymbuf2[mid].count;
7978 ssymbuf2 += mid;
7979 break;
7980 }
7981 }
7982
7983 if (count1 == 0 || count2 == 0 || count1 != count2)
7984 goto done;
7985
ca4be51c
AM
7986 symtable1
7987 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7988 symtable2
7989 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7990 if (symtable1 == NULL || symtable2 == NULL)
7991 goto done;
7992
7993 symp = symtable1;
7994 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7995 ssym < ssymend; ssym++, symp++)
7996 {
7997 symp->u.ssym = ssym;
7998 symp->name = bfd_elf_string_from_elf_section (bfd1,
7999 hdr1->sh_link,
8000 ssym->st_name);
8001 }
8002
8003 symp = symtable2;
8004 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8005 ssym < ssymend; ssym++, symp++)
8006 {
8007 symp->u.ssym = ssym;
8008 symp->name = bfd_elf_string_from_elf_section (bfd2,
8009 hdr2->sh_link,
8010 ssym->st_name);
8011 }
8012
8013 /* Sort symbol by name. */
8014 qsort (symtable1, count1, sizeof (struct elf_symbol),
8015 elf_sym_name_compare);
8016 qsort (symtable2, count1, sizeof (struct elf_symbol),
8017 elf_sym_name_compare);
8018
8019 for (i = 0; i < count1; i++)
8020 /* Two symbols must have the same binding, type and name. */
8021 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8022 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8023 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8024 goto done;
8025
8026 result = TRUE;
8027 goto done;
8028 }
8029
a50b1753
NC
8030 symtable1 = (struct elf_symbol *)
8031 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8032 symtable2 = (struct elf_symbol *)
8033 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8034 if (symtable1 == NULL || symtable2 == NULL)
8035 goto done;
8036
8037 /* Count definitions in the section. */
8038 count1 = 0;
8039 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8040 if (isym->st_shndx == shndx1)
4d269e42
AM
8041 symtable1[count1++].u.isym = isym;
8042
8043 count2 = 0;
8044 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8045 if (isym->st_shndx == shndx2)
4d269e42
AM
8046 symtable2[count2++].u.isym = isym;
8047
8048 if (count1 == 0 || count2 == 0 || count1 != count2)
8049 goto done;
8050
8051 for (i = 0; i < count1; i++)
8052 symtable1[i].name
8053 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8054 symtable1[i].u.isym->st_name);
8055
8056 for (i = 0; i < count2; i++)
8057 symtable2[i].name
8058 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8059 symtable2[i].u.isym->st_name);
8060
8061 /* Sort symbol by name. */
8062 qsort (symtable1, count1, sizeof (struct elf_symbol),
8063 elf_sym_name_compare);
8064 qsort (symtable2, count1, sizeof (struct elf_symbol),
8065 elf_sym_name_compare);
8066
8067 for (i = 0; i < count1; i++)
8068 /* Two symbols must have the same binding, type and name. */
8069 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8070 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8071 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8072 goto done;
8073
8074 result = TRUE;
8075
8076done:
8077 if (symtable1)
8078 free (symtable1);
8079 if (symtable2)
8080 free (symtable2);
8081 if (isymbuf1)
8082 free (isymbuf1);
8083 if (isymbuf2)
8084 free (isymbuf2);
8085
8086 return result;
8087}
8088
8089/* Return TRUE if 2 section types are compatible. */
8090
8091bfd_boolean
8092_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8093 bfd *bbfd, const asection *bsec)
8094{
8095 if (asec == NULL
8096 || bsec == NULL
8097 || abfd->xvec->flavour != bfd_target_elf_flavour
8098 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8099 return TRUE;
8100
8101 return elf_section_type (asec) == elf_section_type (bsec);
8102}
8103\f
c152c796
AM
8104/* Final phase of ELF linker. */
8105
8106/* A structure we use to avoid passing large numbers of arguments. */
8107
8108struct elf_final_link_info
8109{
8110 /* General link information. */
8111 struct bfd_link_info *info;
8112 /* Output BFD. */
8113 bfd *output_bfd;
8114 /* Symbol string table. */
ef10c3ac 8115 struct elf_strtab_hash *symstrtab;
c152c796
AM
8116 /* .hash section. */
8117 asection *hash_sec;
8118 /* symbol version section (.gnu.version). */
8119 asection *symver_sec;
8120 /* Buffer large enough to hold contents of any section. */
8121 bfd_byte *contents;
8122 /* Buffer large enough to hold external relocs of any section. */
8123 void *external_relocs;
8124 /* Buffer large enough to hold internal relocs of any section. */
8125 Elf_Internal_Rela *internal_relocs;
8126 /* Buffer large enough to hold external local symbols of any input
8127 BFD. */
8128 bfd_byte *external_syms;
8129 /* And a buffer for symbol section indices. */
8130 Elf_External_Sym_Shndx *locsym_shndx;
8131 /* Buffer large enough to hold internal local symbols of any input
8132 BFD. */
8133 Elf_Internal_Sym *internal_syms;
8134 /* Array large enough to hold a symbol index for each local symbol
8135 of any input BFD. */
8136 long *indices;
8137 /* Array large enough to hold a section pointer for each local
8138 symbol of any input BFD. */
8139 asection **sections;
ef10c3ac 8140 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8141 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8142 /* Number of STT_FILE syms seen. */
8143 size_t filesym_count;
c152c796
AM
8144};
8145
8146/* This struct is used to pass information to elf_link_output_extsym. */
8147
8148struct elf_outext_info
8149{
8150 bfd_boolean failed;
8151 bfd_boolean localsyms;
34a79995 8152 bfd_boolean file_sym_done;
8b127cbc 8153 struct elf_final_link_info *flinfo;
c152c796
AM
8154};
8155
d9352518
DB
8156
8157/* Support for evaluating a complex relocation.
8158
8159 Complex relocations are generalized, self-describing relocations. The
8160 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8161 relocations themselves.
d9352518
DB
8162
8163 The relocations are use a reserved elf-wide relocation type code (R_RELC
8164 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8165 information (start bit, end bit, word width, etc) into the addend. This
8166 information is extracted from CGEN-generated operand tables within gas.
8167
8168 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8169 internal) representing prefix-notation expressions, including but not
8170 limited to those sorts of expressions normally encoded as addends in the
8171 addend field. The symbol mangling format is:
8172
8173 <node> := <literal>
07d6d2b8
AM
8174 | <unary-operator> ':' <node>
8175 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8176 ;
8177
8178 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8179 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8180 | '#' <hexdigits>
8181 ;
8182
8183 <binary-operator> := as in C
8184 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8185
8186static void
a0c8462f
AM
8187set_symbol_value (bfd *bfd_with_globals,
8188 Elf_Internal_Sym *isymbuf,
8189 size_t locsymcount,
8190 size_t symidx,
8191 bfd_vma val)
d9352518 8192{
8977835c
AM
8193 struct elf_link_hash_entry **sym_hashes;
8194 struct elf_link_hash_entry *h;
8195 size_t extsymoff = locsymcount;
d9352518 8196
8977835c 8197 if (symidx < locsymcount)
d9352518 8198 {
8977835c
AM
8199 Elf_Internal_Sym *sym;
8200
8201 sym = isymbuf + symidx;
8202 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8203 {
8204 /* It is a local symbol: move it to the
8205 "absolute" section and give it a value. */
8206 sym->st_shndx = SHN_ABS;
8207 sym->st_value = val;
8208 return;
8209 }
8210 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8211 extsymoff = 0;
d9352518 8212 }
8977835c
AM
8213
8214 /* It is a global symbol: set its link type
8215 to "defined" and give it a value. */
8216
8217 sym_hashes = elf_sym_hashes (bfd_with_globals);
8218 h = sym_hashes [symidx - extsymoff];
8219 while (h->root.type == bfd_link_hash_indirect
8220 || h->root.type == bfd_link_hash_warning)
8221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8222 h->root.type = bfd_link_hash_defined;
8223 h->root.u.def.value = val;
8224 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8225}
8226
a0c8462f
AM
8227static bfd_boolean
8228resolve_symbol (const char *name,
8229 bfd *input_bfd,
8b127cbc 8230 struct elf_final_link_info *flinfo,
a0c8462f
AM
8231 bfd_vma *result,
8232 Elf_Internal_Sym *isymbuf,
8233 size_t locsymcount)
d9352518 8234{
a0c8462f
AM
8235 Elf_Internal_Sym *sym;
8236 struct bfd_link_hash_entry *global_entry;
8237 const char *candidate = NULL;
8238 Elf_Internal_Shdr *symtab_hdr;
8239 size_t i;
8240
d9352518
DB
8241 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8242
8243 for (i = 0; i < locsymcount; ++ i)
8244 {
8977835c 8245 sym = isymbuf + i;
d9352518
DB
8246
8247 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8248 continue;
8249
8250 candidate = bfd_elf_string_from_elf_section (input_bfd,
8251 symtab_hdr->sh_link,
8252 sym->st_name);
8253#ifdef DEBUG
0f02bbd9
AM
8254 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8255 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8256#endif
8257 if (candidate && strcmp (candidate, name) == 0)
8258 {
8b127cbc 8259 asection *sec = flinfo->sections [i];
d9352518 8260
0f02bbd9
AM
8261 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8262 *result += sec->output_offset + sec->output_section->vma;
d9352518 8263#ifdef DEBUG
0f02bbd9
AM
8264 printf ("Found symbol with value %8.8lx\n",
8265 (unsigned long) *result);
d9352518
DB
8266#endif
8267 return TRUE;
8268 }
8269 }
8270
8271 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8272 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8273 FALSE, FALSE, TRUE);
d9352518
DB
8274 if (!global_entry)
8275 return FALSE;
a0c8462f 8276
d9352518
DB
8277 if (global_entry->type == bfd_link_hash_defined
8278 || global_entry->type == bfd_link_hash_defweak)
8279 {
a0c8462f
AM
8280 *result = (global_entry->u.def.value
8281 + global_entry->u.def.section->output_section->vma
8282 + global_entry->u.def.section->output_offset);
d9352518 8283#ifdef DEBUG
0f02bbd9
AM
8284 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8285 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8286#endif
8287 return TRUE;
a0c8462f 8288 }
d9352518 8289
d9352518
DB
8290 return FALSE;
8291}
8292
37b01f6a
DG
8293/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8294 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8295 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8296
d9352518 8297static bfd_boolean
a0c8462f
AM
8298resolve_section (const char *name,
8299 asection *sections,
37b01f6a
DG
8300 bfd_vma *result,
8301 bfd * abfd)
d9352518 8302{
a0c8462f
AM
8303 asection *curr;
8304 unsigned int len;
d9352518 8305
a0c8462f 8306 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8307 if (strcmp (curr->name, name) == 0)
8308 {
8309 *result = curr->vma;
8310 return TRUE;
8311 }
8312
8313 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8314 /* FIXME: This could be coded more efficiently... */
a0c8462f 8315 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8316 {
8317 len = strlen (curr->name);
a0c8462f 8318 if (len > strlen (name))
d9352518
DB
8319 continue;
8320
8321 if (strncmp (curr->name, name, len) == 0)
8322 {
8323 if (strncmp (".end", name + len, 4) == 0)
8324 {
37b01f6a 8325 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8326 return TRUE;
8327 }
8328
8329 /* Insert more pseudo-section names here, if you like. */
8330 }
8331 }
a0c8462f 8332
d9352518
DB
8333 return FALSE;
8334}
8335
8336static void
a0c8462f 8337undefined_reference (const char *reftype, const char *name)
d9352518 8338{
695344c0 8339 /* xgettext:c-format */
a0c8462f
AM
8340 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8341 reftype, name);
d9352518
DB
8342}
8343
8344static bfd_boolean
a0c8462f
AM
8345eval_symbol (bfd_vma *result,
8346 const char **symp,
8347 bfd *input_bfd,
8b127cbc 8348 struct elf_final_link_info *flinfo,
a0c8462f
AM
8349 bfd_vma dot,
8350 Elf_Internal_Sym *isymbuf,
8351 size_t locsymcount,
8352 int signed_p)
d9352518 8353{
4b93929b
NC
8354 size_t len;
8355 size_t symlen;
a0c8462f
AM
8356 bfd_vma a;
8357 bfd_vma b;
4b93929b 8358 char symbuf[4096];
0f02bbd9 8359 const char *sym = *symp;
a0c8462f
AM
8360 const char *symend;
8361 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8362
8363 len = strlen (sym);
8364 symend = sym + len;
8365
4b93929b 8366 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8367 {
8368 bfd_set_error (bfd_error_invalid_operation);
8369 return FALSE;
8370 }
a0c8462f 8371
d9352518
DB
8372 switch (* sym)
8373 {
8374 case '.':
0f02bbd9
AM
8375 *result = dot;
8376 *symp = sym + 1;
d9352518
DB
8377 return TRUE;
8378
8379 case '#':
0f02bbd9
AM
8380 ++sym;
8381 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8382 return TRUE;
8383
8384 case 'S':
8385 symbol_is_section = TRUE;
1a0670f3 8386 /* Fall through. */
a0c8462f 8387 case 's':
0f02bbd9
AM
8388 ++sym;
8389 symlen = strtol (sym, (char **) symp, 10);
8390 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8391
4b93929b 8392 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8393 {
8394 bfd_set_error (bfd_error_invalid_operation);
8395 return FALSE;
8396 }
8397
8398 memcpy (symbuf, sym, symlen);
a0c8462f 8399 symbuf[symlen] = '\0';
0f02bbd9 8400 *symp = sym + symlen;
a0c8462f
AM
8401
8402 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8403 the symbol as a section, or vice-versa. so we're pretty liberal in our
8404 interpretation here; section means "try section first", not "must be a
8405 section", and likewise with symbol. */
8406
a0c8462f 8407 if (symbol_is_section)
d9352518 8408 {
37b01f6a 8409 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8410 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8411 isymbuf, locsymcount))
d9352518
DB
8412 {
8413 undefined_reference ("section", symbuf);
8414 return FALSE;
8415 }
a0c8462f
AM
8416 }
8417 else
d9352518 8418 {
8b127cbc 8419 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8420 isymbuf, locsymcount)
8b127cbc 8421 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8422 result, input_bfd))
d9352518
DB
8423 {
8424 undefined_reference ("symbol", symbuf);
8425 return FALSE;
8426 }
8427 }
8428
8429 return TRUE;
a0c8462f 8430
d9352518
DB
8431 /* All that remains are operators. */
8432
8433#define UNARY_OP(op) \
8434 if (strncmp (sym, #op, strlen (#op)) == 0) \
8435 { \
8436 sym += strlen (#op); \
a0c8462f
AM
8437 if (*sym == ':') \
8438 ++sym; \
0f02bbd9 8439 *symp = sym; \
8b127cbc 8440 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8441 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8442 return FALSE; \
8443 if (signed_p) \
0f02bbd9 8444 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8445 else \
8446 *result = op a; \
d9352518
DB
8447 return TRUE; \
8448 }
8449
8450#define BINARY_OP(op) \
8451 if (strncmp (sym, #op, strlen (#op)) == 0) \
8452 { \
8453 sym += strlen (#op); \
a0c8462f
AM
8454 if (*sym == ':') \
8455 ++sym; \
0f02bbd9 8456 *symp = sym; \
8b127cbc 8457 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8458 isymbuf, locsymcount, signed_p)) \
a0c8462f 8459 return FALSE; \
0f02bbd9 8460 ++*symp; \
8b127cbc 8461 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8462 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8463 return FALSE; \
8464 if (signed_p) \
0f02bbd9 8465 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8466 else \
8467 *result = a op b; \
d9352518
DB
8468 return TRUE; \
8469 }
8470
8471 default:
8472 UNARY_OP (0-);
8473 BINARY_OP (<<);
8474 BINARY_OP (>>);
8475 BINARY_OP (==);
8476 BINARY_OP (!=);
8477 BINARY_OP (<=);
8478 BINARY_OP (>=);
8479 BINARY_OP (&&);
8480 BINARY_OP (||);
8481 UNARY_OP (~);
8482 UNARY_OP (!);
8483 BINARY_OP (*);
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 BINARY_OP (>);
8493#undef UNARY_OP
8494#undef BINARY_OP
8495 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8496 bfd_set_error (bfd_error_invalid_operation);
8497 return FALSE;
8498 }
8499}
8500
d9352518 8501static void
a0c8462f
AM
8502put_value (bfd_vma size,
8503 unsigned long chunksz,
8504 bfd *input_bfd,
8505 bfd_vma x,
8506 bfd_byte *location)
d9352518
DB
8507{
8508 location += (size - chunksz);
8509
41cd1ad1 8510 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8511 {
8512 switch (chunksz)
8513 {
d9352518
DB
8514 case 1:
8515 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8516 x >>= 8;
d9352518
DB
8517 break;
8518 case 2:
8519 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8520 x >>= 16;
d9352518
DB
8521 break;
8522 case 4:
8523 bfd_put_32 (input_bfd, x, location);
65164438
NC
8524 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8525 x >>= 16;
8526 x >>= 16;
d9352518 8527 break;
d9352518 8528#ifdef BFD64
41cd1ad1 8529 case 8:
d9352518 8530 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8531 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8532 x >>= 32;
8533 x >>= 32;
8534 break;
d9352518 8535#endif
41cd1ad1
NC
8536 default:
8537 abort ();
d9352518
DB
8538 break;
8539 }
8540 }
8541}
8542
a0c8462f
AM
8543static bfd_vma
8544get_value (bfd_vma size,
8545 unsigned long chunksz,
8546 bfd *input_bfd,
8547 bfd_byte *location)
d9352518 8548{
9b239e0e 8549 int shift;
d9352518
DB
8550 bfd_vma x = 0;
8551
9b239e0e
NC
8552 /* Sanity checks. */
8553 BFD_ASSERT (chunksz <= sizeof (x)
8554 && size >= chunksz
8555 && chunksz != 0
8556 && (size % chunksz) == 0
8557 && input_bfd != NULL
8558 && location != NULL);
8559
8560 if (chunksz == sizeof (x))
8561 {
8562 BFD_ASSERT (size == chunksz);
8563
8564 /* Make sure that we do not perform an undefined shift operation.
8565 We know that size == chunksz so there will only be one iteration
8566 of the loop below. */
8567 shift = 0;
8568 }
8569 else
8570 shift = 8 * chunksz;
8571
a0c8462f 8572 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8573 {
8574 switch (chunksz)
8575 {
d9352518 8576 case 1:
9b239e0e 8577 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8578 break;
8579 case 2:
9b239e0e 8580 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8581 break;
8582 case 4:
9b239e0e 8583 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8584 break;
d9352518 8585#ifdef BFD64
9b239e0e
NC
8586 case 8:
8587 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8588 break;
9b239e0e
NC
8589#endif
8590 default:
8591 abort ();
d9352518
DB
8592 }
8593 }
8594 return x;
8595}
8596
a0c8462f
AM
8597static void
8598decode_complex_addend (unsigned long *start, /* in bits */
8599 unsigned long *oplen, /* in bits */
8600 unsigned long *len, /* in bits */
8601 unsigned long *wordsz, /* in bytes */
8602 unsigned long *chunksz, /* in bytes */
8603 unsigned long *lsb0_p,
8604 unsigned long *signed_p,
8605 unsigned long *trunc_p,
8606 unsigned long encoded)
d9352518 8607{
07d6d2b8
AM
8608 * start = encoded & 0x3F;
8609 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8610 * oplen = (encoded >> 12) & 0x3F;
8611 * wordsz = (encoded >> 18) & 0xF;
8612 * chunksz = (encoded >> 22) & 0xF;
8613 * lsb0_p = (encoded >> 27) & 1;
8614 * signed_p = (encoded >> 28) & 1;
8615 * trunc_p = (encoded >> 29) & 1;
8616}
8617
cdfeee4f 8618bfd_reloc_status_type
0f02bbd9 8619bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8620 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8621 bfd_byte *contents,
8622 Elf_Internal_Rela *rel,
8623 bfd_vma relocation)
d9352518 8624{
0f02bbd9
AM
8625 bfd_vma shift, x, mask;
8626 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8627 bfd_reloc_status_type r;
d9352518
DB
8628
8629 /* Perform this reloc, since it is complex.
8630 (this is not to say that it necessarily refers to a complex
8631 symbol; merely that it is a self-describing CGEN based reloc.
8632 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8633 word size, etc) encoded within it.). */
d9352518 8634
a0c8462f
AM
8635 decode_complex_addend (&start, &oplen, &len, &wordsz,
8636 &chunksz, &lsb0_p, &signed_p,
8637 &trunc_p, rel->r_addend);
d9352518
DB
8638
8639 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8640
8641 if (lsb0_p)
8642 shift = (start + 1) - len;
8643 else
8644 shift = (8 * wordsz) - (start + len);
8645
37b01f6a
DG
8646 x = get_value (wordsz, chunksz, input_bfd,
8647 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8648
8649#ifdef DEBUG
8650 printf ("Doing complex reloc: "
8651 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8652 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8653 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8654 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8655 oplen, (unsigned long) x, (unsigned long) mask,
8656 (unsigned long) relocation);
d9352518
DB
8657#endif
8658
cdfeee4f 8659 r = bfd_reloc_ok;
d9352518 8660 if (! trunc_p)
cdfeee4f
AM
8661 /* Now do an overflow check. */
8662 r = bfd_check_overflow ((signed_p
8663 ? complain_overflow_signed
8664 : complain_overflow_unsigned),
8665 len, 0, (8 * wordsz),
8666 relocation);
a0c8462f 8667
d9352518
DB
8668 /* Do the deed. */
8669 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8670
8671#ifdef DEBUG
8672 printf (" relocation: %8.8lx\n"
8673 " shifted mask: %8.8lx\n"
8674 " shifted/masked reloc: %8.8lx\n"
8675 " result: %8.8lx\n",
9ccb8af9
AM
8676 (unsigned long) relocation, (unsigned long) (mask << shift),
8677 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8678#endif
37b01f6a
DG
8679 put_value (wordsz, chunksz, input_bfd, x,
8680 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8681 return r;
d9352518
DB
8682}
8683
0e287786
AM
8684/* Functions to read r_offset from external (target order) reloc
8685 entry. Faster than bfd_getl32 et al, because we let the compiler
8686 know the value is aligned. */
53df40a4 8687
0e287786
AM
8688static bfd_vma
8689ext32l_r_offset (const void *p)
53df40a4
AM
8690{
8691 union aligned32
8692 {
8693 uint32_t v;
8694 unsigned char c[4];
8695 };
8696 const union aligned32 *a
0e287786 8697 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8698
8699 uint32_t aval = ( (uint32_t) a->c[0]
8700 | (uint32_t) a->c[1] << 8
8701 | (uint32_t) a->c[2] << 16
8702 | (uint32_t) a->c[3] << 24);
0e287786 8703 return aval;
53df40a4
AM
8704}
8705
0e287786
AM
8706static bfd_vma
8707ext32b_r_offset (const void *p)
53df40a4
AM
8708{
8709 union aligned32
8710 {
8711 uint32_t v;
8712 unsigned char c[4];
8713 };
8714 const union aligned32 *a
0e287786 8715 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8716
8717 uint32_t aval = ( (uint32_t) a->c[0] << 24
8718 | (uint32_t) a->c[1] << 16
8719 | (uint32_t) a->c[2] << 8
8720 | (uint32_t) a->c[3]);
0e287786 8721 return aval;
53df40a4
AM
8722}
8723
8724#ifdef BFD_HOST_64_BIT
0e287786
AM
8725static bfd_vma
8726ext64l_r_offset (const void *p)
53df40a4
AM
8727{
8728 union aligned64
8729 {
8730 uint64_t v;
8731 unsigned char c[8];
8732 };
8733 const union aligned64 *a
0e287786 8734 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8735
8736 uint64_t aval = ( (uint64_t) a->c[0]
8737 | (uint64_t) a->c[1] << 8
8738 | (uint64_t) a->c[2] << 16
8739 | (uint64_t) a->c[3] << 24
8740 | (uint64_t) a->c[4] << 32
8741 | (uint64_t) a->c[5] << 40
8742 | (uint64_t) a->c[6] << 48
8743 | (uint64_t) a->c[7] << 56);
0e287786 8744 return aval;
53df40a4
AM
8745}
8746
0e287786
AM
8747static bfd_vma
8748ext64b_r_offset (const void *p)
53df40a4
AM
8749{
8750 union aligned64
8751 {
8752 uint64_t v;
8753 unsigned char c[8];
8754 };
8755 const union aligned64 *a
0e287786 8756 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8757
8758 uint64_t aval = ( (uint64_t) a->c[0] << 56
8759 | (uint64_t) a->c[1] << 48
8760 | (uint64_t) a->c[2] << 40
8761 | (uint64_t) a->c[3] << 32
8762 | (uint64_t) a->c[4] << 24
8763 | (uint64_t) a->c[5] << 16
8764 | (uint64_t) a->c[6] << 8
8765 | (uint64_t) a->c[7]);
0e287786 8766 return aval;
53df40a4
AM
8767}
8768#endif
8769
c152c796
AM
8770/* When performing a relocatable link, the input relocations are
8771 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8772 referenced must be updated. Update all the relocations found in
8773 RELDATA. */
c152c796 8774
bca6d0e3 8775static bfd_boolean
c152c796 8776elf_link_adjust_relocs (bfd *abfd,
9eaff861 8777 asection *sec,
28dbcedc 8778 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8779 bfd_boolean sort,
8780 struct bfd_link_info *info)
c152c796
AM
8781{
8782 unsigned int i;
8783 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8784 bfd_byte *erela;
8785 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8786 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8787 bfd_vma r_type_mask;
8788 int r_sym_shift;
d4730f92
BS
8789 unsigned int count = reldata->count;
8790 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8791
d4730f92 8792 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8793 {
8794 swap_in = bed->s->swap_reloc_in;
8795 swap_out = bed->s->swap_reloc_out;
8796 }
d4730f92 8797 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8798 {
8799 swap_in = bed->s->swap_reloca_in;
8800 swap_out = bed->s->swap_reloca_out;
8801 }
8802 else
8803 abort ();
8804
8805 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8806 abort ();
8807
8808 if (bed->s->arch_size == 32)
8809 {
8810 r_type_mask = 0xff;
8811 r_sym_shift = 8;
8812 }
8813 else
8814 {
8815 r_type_mask = 0xffffffff;
8816 r_sym_shift = 32;
8817 }
8818
d4730f92
BS
8819 erela = reldata->hdr->contents;
8820 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8821 {
8822 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8823 unsigned int j;
8824
8825 if (*rel_hash == NULL)
8826 continue;
8827
10bbbc1d
NC
8828 if ((*rel_hash)->indx == -2
8829 && info->gc_sections
8830 && ! info->gc_keep_exported)
8831 {
8832 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8833 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8834 abfd, sec,
8835 (*rel_hash)->root.root.string);
9793eb77 8836 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8837 abfd, sec);
10bbbc1d
NC
8838 bfd_set_error (bfd_error_invalid_operation);
8839 return FALSE;
8840 }
c152c796
AM
8841 BFD_ASSERT ((*rel_hash)->indx >= 0);
8842
8843 (*swap_in) (abfd, erela, irela);
8844 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8845 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8846 | (irela[j].r_info & r_type_mask));
8847 (*swap_out) (abfd, irela, erela);
8848 }
53df40a4 8849
9eaff861
AO
8850 if (bed->elf_backend_update_relocs)
8851 (*bed->elf_backend_update_relocs) (sec, reldata);
8852
0e287786 8853 if (sort && count != 0)
53df40a4 8854 {
0e287786
AM
8855 bfd_vma (*ext_r_off) (const void *);
8856 bfd_vma r_off;
8857 size_t elt_size;
8858 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8859 bfd_byte *buf = NULL;
28dbcedc
AM
8860
8861 if (bed->s->arch_size == 32)
8862 {
8863 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8864 ext_r_off = ext32l_r_offset;
28dbcedc 8865 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8866 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8867 else
8868 abort ();
8869 }
53df40a4 8870 else
28dbcedc 8871 {
53df40a4 8872#ifdef BFD_HOST_64_BIT
28dbcedc 8873 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8874 ext_r_off = ext64l_r_offset;
28dbcedc 8875 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8876 ext_r_off = ext64b_r_offset;
28dbcedc 8877 else
53df40a4 8878#endif
28dbcedc
AM
8879 abort ();
8880 }
0e287786 8881
bca6d0e3
AM
8882 /* Must use a stable sort here. A modified insertion sort,
8883 since the relocs are mostly sorted already. */
0e287786
AM
8884 elt_size = reldata->hdr->sh_entsize;
8885 base = reldata->hdr->contents;
8886 end = base + count * elt_size;
bca6d0e3 8887 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8888 abort ();
8889
8890 /* Ensure the first element is lowest. This acts as a sentinel,
8891 speeding the main loop below. */
8892 r_off = (*ext_r_off) (base);
8893 for (p = loc = base; (p += elt_size) < end; )
8894 {
8895 bfd_vma r_off2 = (*ext_r_off) (p);
8896 if (r_off > r_off2)
8897 {
8898 r_off = r_off2;
8899 loc = p;
8900 }
8901 }
8902 if (loc != base)
8903 {
8904 /* Don't just swap *base and *loc as that changes the order
8905 of the original base[0] and base[1] if they happen to
8906 have the same r_offset. */
bca6d0e3
AM
8907 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8908 memcpy (onebuf, loc, elt_size);
0e287786 8909 memmove (base + elt_size, base, loc - base);
bca6d0e3 8910 memcpy (base, onebuf, elt_size);
0e287786
AM
8911 }
8912
b29b8669 8913 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8914 {
8915 /* base to p is sorted, *p is next to insert. */
8916 r_off = (*ext_r_off) (p);
8917 /* Search the sorted region for location to insert. */
8918 loc = p - elt_size;
8919 while (r_off < (*ext_r_off) (loc))
8920 loc -= elt_size;
8921 loc += elt_size;
8922 if (loc != p)
8923 {
bca6d0e3
AM
8924 /* Chances are there is a run of relocs to insert here,
8925 from one of more input files. Files are not always
8926 linked in order due to the way elf_link_input_bfd is
8927 called. See pr17666. */
8928 size_t sortlen = p - loc;
8929 bfd_vma r_off2 = (*ext_r_off) (loc);
8930 size_t runlen = elt_size;
8931 size_t buf_size = 96 * 1024;
8932 while (p + runlen < end
8933 && (sortlen <= buf_size
8934 || runlen + elt_size <= buf_size)
8935 && r_off2 > (*ext_r_off) (p + runlen))
8936 runlen += elt_size;
8937 if (buf == NULL)
8938 {
8939 buf = bfd_malloc (buf_size);
8940 if (buf == NULL)
8941 return FALSE;
8942 }
8943 if (runlen < sortlen)
8944 {
8945 memcpy (buf, p, runlen);
8946 memmove (loc + runlen, loc, sortlen);
8947 memcpy (loc, buf, runlen);
8948 }
8949 else
8950 {
8951 memcpy (buf, loc, sortlen);
8952 memmove (loc, p, runlen);
8953 memcpy (loc + runlen, buf, sortlen);
8954 }
b29b8669 8955 p += runlen - elt_size;
0e287786
AM
8956 }
8957 }
8958 /* Hashes are no longer valid. */
28dbcedc
AM
8959 free (reldata->hashes);
8960 reldata->hashes = NULL;
bca6d0e3 8961 free (buf);
53df40a4 8962 }
bca6d0e3 8963 return TRUE;
c152c796
AM
8964}
8965
8966struct elf_link_sort_rela
8967{
8968 union {
8969 bfd_vma offset;
8970 bfd_vma sym_mask;
8971 } u;
8972 enum elf_reloc_type_class type;
8973 /* We use this as an array of size int_rels_per_ext_rel. */
8974 Elf_Internal_Rela rela[1];
8975};
8976
8977static int
8978elf_link_sort_cmp1 (const void *A, const void *B)
8979{
a50b1753
NC
8980 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8981 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8982 int relativea, relativeb;
8983
8984 relativea = a->type == reloc_class_relative;
8985 relativeb = b->type == reloc_class_relative;
8986
8987 if (relativea < relativeb)
8988 return 1;
8989 if (relativea > relativeb)
8990 return -1;
8991 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8992 return -1;
8993 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8994 return 1;
8995 if (a->rela->r_offset < b->rela->r_offset)
8996 return -1;
8997 if (a->rela->r_offset > b->rela->r_offset)
8998 return 1;
8999 return 0;
9000}
9001
9002static int
9003elf_link_sort_cmp2 (const void *A, const void *B)
9004{
a50b1753
NC
9005 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9006 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9007
7e612e98 9008 if (a->type < b->type)
c152c796 9009 return -1;
7e612e98 9010 if (a->type > b->type)
c152c796 9011 return 1;
7e612e98 9012 if (a->u.offset < b->u.offset)
c152c796 9013 return -1;
7e612e98 9014 if (a->u.offset > b->u.offset)
c152c796
AM
9015 return 1;
9016 if (a->rela->r_offset < b->rela->r_offset)
9017 return -1;
9018 if (a->rela->r_offset > b->rela->r_offset)
9019 return 1;
9020 return 0;
9021}
9022
9023static size_t
9024elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9025{
3410fea8 9026 asection *dynamic_relocs;
fc66a176
L
9027 asection *rela_dyn;
9028 asection *rel_dyn;
c152c796
AM
9029 bfd_size_type count, size;
9030 size_t i, ret, sort_elt, ext_size;
9031 bfd_byte *sort, *s_non_relative, *p;
9032 struct elf_link_sort_rela *sq;
9033 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9034 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9035 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9036 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9037 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9038 struct bfd_link_order *lo;
9039 bfd_vma r_sym_mask;
3410fea8 9040 bfd_boolean use_rela;
c152c796 9041
3410fea8
NC
9042 /* Find a dynamic reloc section. */
9043 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9044 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9045 if (rela_dyn != NULL && rela_dyn->size > 0
9046 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9047 {
3410fea8
NC
9048 bfd_boolean use_rela_initialised = FALSE;
9049
9050 /* This is just here to stop gcc from complaining.
c8e44c6d 9051 Its initialization checking code is not perfect. */
3410fea8
NC
9052 use_rela = TRUE;
9053
9054 /* Both sections are present. Examine the sizes
9055 of the indirect sections to help us choose. */
9056 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9057 if (lo->type == bfd_indirect_link_order)
9058 {
9059 asection *o = lo->u.indirect.section;
9060
9061 if ((o->size % bed->s->sizeof_rela) == 0)
9062 {
9063 if ((o->size % bed->s->sizeof_rel) == 0)
9064 /* Section size is divisible by both rel and rela sizes.
9065 It is of no help to us. */
9066 ;
9067 else
9068 {
9069 /* Section size is only divisible by rela. */
535b785f 9070 if (use_rela_initialised && !use_rela)
3410fea8 9071 {
9793eb77 9072 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9073 "they are in more than one size"),
9074 abfd);
3410fea8
NC
9075 bfd_set_error (bfd_error_invalid_operation);
9076 return 0;
9077 }
9078 else
9079 {
9080 use_rela = TRUE;
9081 use_rela_initialised = TRUE;
9082 }
9083 }
9084 }
9085 else if ((o->size % bed->s->sizeof_rel) == 0)
9086 {
9087 /* Section size is only divisible by rel. */
535b785f 9088 if (use_rela_initialised && use_rela)
3410fea8 9089 {
9793eb77 9090 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9091 "they are in more than one size"),
9092 abfd);
3410fea8
NC
9093 bfd_set_error (bfd_error_invalid_operation);
9094 return 0;
9095 }
9096 else
9097 {
9098 use_rela = FALSE;
9099 use_rela_initialised = TRUE;
9100 }
9101 }
9102 else
9103 {
c8e44c6d
AM
9104 /* The section size is not divisible by either -
9105 something is wrong. */
9793eb77 9106 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9107 "they are of an unknown size"), abfd);
3410fea8
NC
9108 bfd_set_error (bfd_error_invalid_operation);
9109 return 0;
9110 }
9111 }
9112
9113 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9114 if (lo->type == bfd_indirect_link_order)
9115 {
9116 asection *o = lo->u.indirect.section;
9117
9118 if ((o->size % bed->s->sizeof_rela) == 0)
9119 {
9120 if ((o->size % bed->s->sizeof_rel) == 0)
9121 /* Section size is divisible by both rel and rela sizes.
9122 It is of no help to us. */
9123 ;
9124 else
9125 {
9126 /* Section size is only divisible by rela. */
535b785f 9127 if (use_rela_initialised && !use_rela)
3410fea8 9128 {
9793eb77 9129 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9130 "they are in more than one size"),
9131 abfd);
3410fea8
NC
9132 bfd_set_error (bfd_error_invalid_operation);
9133 return 0;
9134 }
9135 else
9136 {
9137 use_rela = TRUE;
9138 use_rela_initialised = TRUE;
9139 }
9140 }
9141 }
9142 else if ((o->size % bed->s->sizeof_rel) == 0)
9143 {
9144 /* Section size is only divisible by rel. */
535b785f 9145 if (use_rela_initialised && use_rela)
3410fea8 9146 {
9793eb77 9147 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9148 "they are in more than one size"),
9149 abfd);
3410fea8
NC
9150 bfd_set_error (bfd_error_invalid_operation);
9151 return 0;
9152 }
9153 else
9154 {
9155 use_rela = FALSE;
9156 use_rela_initialised = TRUE;
9157 }
9158 }
9159 else
9160 {
c8e44c6d
AM
9161 /* The section size is not divisible by either -
9162 something is wrong. */
9793eb77 9163 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9164 "they are of an unknown size"), abfd);
3410fea8
NC
9165 bfd_set_error (bfd_error_invalid_operation);
9166 return 0;
9167 }
9168 }
9169
9170 if (! use_rela_initialised)
9171 /* Make a guess. */
9172 use_rela = TRUE;
c152c796 9173 }
fc66a176
L
9174 else if (rela_dyn != NULL && rela_dyn->size > 0)
9175 use_rela = TRUE;
9176 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9177 use_rela = FALSE;
c152c796 9178 else
fc66a176 9179 return 0;
3410fea8
NC
9180
9181 if (use_rela)
c152c796 9182 {
3410fea8 9183 dynamic_relocs = rela_dyn;
c152c796
AM
9184 ext_size = bed->s->sizeof_rela;
9185 swap_in = bed->s->swap_reloca_in;
9186 swap_out = bed->s->swap_reloca_out;
9187 }
3410fea8
NC
9188 else
9189 {
9190 dynamic_relocs = rel_dyn;
9191 ext_size = bed->s->sizeof_rel;
9192 swap_in = bed->s->swap_reloc_in;
9193 swap_out = bed->s->swap_reloc_out;
9194 }
c152c796
AM
9195
9196 size = 0;
3410fea8 9197 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9198 if (lo->type == bfd_indirect_link_order)
3410fea8 9199 size += lo->u.indirect.section->size;
c152c796 9200
3410fea8 9201 if (size != dynamic_relocs->size)
c152c796
AM
9202 return 0;
9203
9204 sort_elt = (sizeof (struct elf_link_sort_rela)
9205 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9206
9207 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9208 if (count == 0)
9209 return 0;
a50b1753 9210 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9211
c152c796
AM
9212 if (sort == NULL)
9213 {
9214 (*info->callbacks->warning)
9793eb77 9215 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9216 return 0;
9217 }
9218
9219 if (bed->s->arch_size == 32)
9220 r_sym_mask = ~(bfd_vma) 0xff;
9221 else
9222 r_sym_mask = ~(bfd_vma) 0xffffffff;
9223
3410fea8 9224 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9225 if (lo->type == bfd_indirect_link_order)
9226 {
9227 bfd_byte *erel, *erelend;
9228 asection *o = lo->u.indirect.section;
9229
1da212d6
AM
9230 if (o->contents == NULL && o->size != 0)
9231 {
9232 /* This is a reloc section that is being handled as a normal
9233 section. See bfd_section_from_shdr. We can't combine
9234 relocs in this case. */
9235 free (sort);
9236 return 0;
9237 }
c152c796 9238 erel = o->contents;
eea6121a 9239 erelend = o->contents + o->size;
c8e44c6d 9240 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9241
c152c796
AM
9242 while (erel < erelend)
9243 {
9244 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9245
c152c796 9246 (*swap_in) (abfd, erel, s->rela);
7e612e98 9247 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9248 s->u.sym_mask = r_sym_mask;
9249 p += sort_elt;
9250 erel += ext_size;
9251 }
9252 }
9253
9254 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9255
9256 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9257 {
9258 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9259 if (s->type != reloc_class_relative)
9260 break;
9261 }
9262 ret = i;
9263 s_non_relative = p;
9264
9265 sq = (struct elf_link_sort_rela *) s_non_relative;
9266 for (; i < count; i++, p += sort_elt)
9267 {
9268 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9269 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9270 sq = sp;
9271 sp->u.offset = sq->rela->r_offset;
9272 }
9273
9274 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9275
c8e44c6d
AM
9276 struct elf_link_hash_table *htab = elf_hash_table (info);
9277 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9278 {
9279 /* We have plt relocs in .rela.dyn. */
9280 sq = (struct elf_link_sort_rela *) sort;
9281 for (i = 0; i < count; i++)
9282 if (sq[count - i - 1].type != reloc_class_plt)
9283 break;
9284 if (i != 0 && htab->srelplt->size == i * ext_size)
9285 {
9286 struct bfd_link_order **plo;
9287 /* Put srelplt link_order last. This is so the output_offset
9288 set in the next loop is correct for DT_JMPREL. */
9289 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9290 if ((*plo)->type == bfd_indirect_link_order
9291 && (*plo)->u.indirect.section == htab->srelplt)
9292 {
9293 lo = *plo;
9294 *plo = lo->next;
9295 }
9296 else
9297 plo = &(*plo)->next;
9298 *plo = lo;
9299 lo->next = NULL;
9300 dynamic_relocs->map_tail.link_order = lo;
9301 }
9302 }
9303
9304 p = sort;
3410fea8 9305 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9306 if (lo->type == bfd_indirect_link_order)
9307 {
9308 bfd_byte *erel, *erelend;
9309 asection *o = lo->u.indirect.section;
9310
9311 erel = o->contents;
eea6121a 9312 erelend = o->contents + o->size;
c8e44c6d 9313 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9314 while (erel < erelend)
9315 {
9316 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9317 (*swap_out) (abfd, s->rela, erel);
9318 p += sort_elt;
9319 erel += ext_size;
9320 }
9321 }
9322
9323 free (sort);
3410fea8 9324 *psec = dynamic_relocs;
c152c796
AM
9325 return ret;
9326}
9327
ef10c3ac 9328/* Add a symbol to the output symbol string table. */
c152c796 9329
6e0b88f1 9330static int
ef10c3ac
L
9331elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9332 const char *name,
9333 Elf_Internal_Sym *elfsym,
9334 asection *input_sec,
9335 struct elf_link_hash_entry *h)
c152c796 9336{
6e0b88f1 9337 int (*output_symbol_hook)
c152c796
AM
9338 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9339 struct elf_link_hash_entry *);
ef10c3ac 9340 struct elf_link_hash_table *hash_table;
c152c796 9341 const struct elf_backend_data *bed;
ef10c3ac 9342 bfd_size_type strtabsize;
c152c796 9343
8539e4e8
AM
9344 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9345
8b127cbc 9346 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9347 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9348 if (output_symbol_hook != NULL)
9349 {
8b127cbc 9350 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9351 if (ret != 1)
9352 return ret;
c152c796
AM
9353 }
9354
ef10c3ac
L
9355 if (name == NULL
9356 || *name == '\0'
9357 || (input_sec->flags & SEC_EXCLUDE))
9358 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9359 else
9360 {
ef10c3ac
L
9361 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9362 to get the final offset for st_name. */
9363 elfsym->st_name
9364 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9365 name, FALSE);
c152c796 9366 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9367 return 0;
c152c796
AM
9368 }
9369
ef10c3ac
L
9370 hash_table = elf_hash_table (flinfo->info);
9371 strtabsize = hash_table->strtabsize;
9372 if (strtabsize <= hash_table->strtabcount)
c152c796 9373 {
ef10c3ac
L
9374 strtabsize += strtabsize;
9375 hash_table->strtabsize = strtabsize;
9376 strtabsize *= sizeof (*hash_table->strtab);
9377 hash_table->strtab
9378 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9379 strtabsize);
9380 if (hash_table->strtab == NULL)
6e0b88f1 9381 return 0;
c152c796 9382 }
ef10c3ac
L
9383 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9384 hash_table->strtab[hash_table->strtabcount].dest_index
9385 = hash_table->strtabcount;
9386 hash_table->strtab[hash_table->strtabcount].destshndx_index
9387 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9388
9389 bfd_get_symcount (flinfo->output_bfd) += 1;
9390 hash_table->strtabcount += 1;
9391
9392 return 1;
9393}
9394
9395/* Swap symbols out to the symbol table and flush the output symbols to
9396 the file. */
9397
9398static bfd_boolean
9399elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9400{
9401 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9402 bfd_size_type amt;
9403 size_t i;
ef10c3ac
L
9404 const struct elf_backend_data *bed;
9405 bfd_byte *symbuf;
9406 Elf_Internal_Shdr *hdr;
9407 file_ptr pos;
9408 bfd_boolean ret;
9409
9410 if (!hash_table->strtabcount)
9411 return TRUE;
9412
9413 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9414
9415 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9416
ef10c3ac
L
9417 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9418 symbuf = (bfd_byte *) bfd_malloc (amt);
9419 if (symbuf == NULL)
9420 return FALSE;
1b786873 9421
ef10c3ac 9422 if (flinfo->symshndxbuf)
c152c796 9423 {
ef53be89
AM
9424 amt = sizeof (Elf_External_Sym_Shndx);
9425 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9426 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9427 if (flinfo->symshndxbuf == NULL)
c152c796 9428 {
ef10c3ac
L
9429 free (symbuf);
9430 return FALSE;
c152c796 9431 }
c152c796
AM
9432 }
9433
ef10c3ac
L
9434 for (i = 0; i < hash_table->strtabcount; i++)
9435 {
9436 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9437 if (elfsym->sym.st_name == (unsigned long) -1)
9438 elfsym->sym.st_name = 0;
9439 else
9440 elfsym->sym.st_name
9441 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9442 elfsym->sym.st_name);
9443 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9444 ((bfd_byte *) symbuf
9445 + (elfsym->dest_index
9446 * bed->s->sizeof_sym)),
9447 (flinfo->symshndxbuf
9448 + elfsym->destshndx_index));
9449 }
9450
9451 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9452 pos = hdr->sh_offset + hdr->sh_size;
9453 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9454 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9455 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9456 {
9457 hdr->sh_size += amt;
9458 ret = TRUE;
9459 }
9460 else
9461 ret = FALSE;
c152c796 9462
ef10c3ac
L
9463 free (symbuf);
9464
9465 free (hash_table->strtab);
9466 hash_table->strtab = NULL;
9467
9468 return ret;
c152c796
AM
9469}
9470
c0d5a53d
L
9471/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9472
9473static bfd_boolean
9474check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9475{
4fbb74a6
AM
9476 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9477 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9478 {
9479 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9480 beyond 64k. */
4eca0228 9481 _bfd_error_handler
695344c0 9482 /* xgettext:c-format */
9793eb77 9483 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9484 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9485 bfd_set_error (bfd_error_nonrepresentable_section);
9486 return FALSE;
9487 }
9488 return TRUE;
9489}
9490
c152c796
AM
9491/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9492 allowing an unsatisfied unversioned symbol in the DSO to match a
9493 versioned symbol that would normally require an explicit version.
9494 We also handle the case that a DSO references a hidden symbol
9495 which may be satisfied by a versioned symbol in another DSO. */
9496
9497static bfd_boolean
9498elf_link_check_versioned_symbol (struct bfd_link_info *info,
9499 const struct elf_backend_data *bed,
9500 struct elf_link_hash_entry *h)
9501{
9502 bfd *abfd;
9503 struct elf_link_loaded_list *loaded;
9504
9505 if (!is_elf_hash_table (info->hash))
9506 return FALSE;
9507
90c984fc
L
9508 /* Check indirect symbol. */
9509 while (h->root.type == bfd_link_hash_indirect)
9510 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9511
c152c796
AM
9512 switch (h->root.type)
9513 {
9514 default:
9515 abfd = NULL;
9516 break;
9517
9518 case bfd_link_hash_undefined:
9519 case bfd_link_hash_undefweak:
9520 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9521 if (abfd == NULL
9522 || (abfd->flags & DYNAMIC) == 0
e56f61be 9523 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9524 return FALSE;
9525 break;
9526
9527 case bfd_link_hash_defined:
9528 case bfd_link_hash_defweak:
9529 abfd = h->root.u.def.section->owner;
9530 break;
9531
9532 case bfd_link_hash_common:
9533 abfd = h->root.u.c.p->section->owner;
9534 break;
9535 }
9536 BFD_ASSERT (abfd != NULL);
9537
9538 for (loaded = elf_hash_table (info)->loaded;
9539 loaded != NULL;
9540 loaded = loaded->next)
9541 {
9542 bfd *input;
9543 Elf_Internal_Shdr *hdr;
ef53be89
AM
9544 size_t symcount;
9545 size_t extsymcount;
9546 size_t extsymoff;
c152c796
AM
9547 Elf_Internal_Shdr *versymhdr;
9548 Elf_Internal_Sym *isym;
9549 Elf_Internal_Sym *isymend;
9550 Elf_Internal_Sym *isymbuf;
9551 Elf_External_Versym *ever;
9552 Elf_External_Versym *extversym;
9553
9554 input = loaded->abfd;
9555
9556 /* We check each DSO for a possible hidden versioned definition. */
9557 if (input == abfd
9558 || (input->flags & DYNAMIC) == 0
9559 || elf_dynversym (input) == 0)
9560 continue;
9561
9562 hdr = &elf_tdata (input)->dynsymtab_hdr;
9563
9564 symcount = hdr->sh_size / bed->s->sizeof_sym;
9565 if (elf_bad_symtab (input))
9566 {
9567 extsymcount = symcount;
9568 extsymoff = 0;
9569 }
9570 else
9571 {
9572 extsymcount = symcount - hdr->sh_info;
9573 extsymoff = hdr->sh_info;
9574 }
9575
9576 if (extsymcount == 0)
9577 continue;
9578
9579 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9580 NULL, NULL, NULL);
9581 if (isymbuf == NULL)
9582 return FALSE;
9583
9584 /* Read in any version definitions. */
9585 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9586 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9587 if (extversym == NULL)
9588 goto error_ret;
9589
9590 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9591 || (bfd_bread (extversym, versymhdr->sh_size, input)
9592 != versymhdr->sh_size))
9593 {
9594 free (extversym);
9595 error_ret:
9596 free (isymbuf);
9597 return FALSE;
9598 }
9599
9600 ever = extversym + extsymoff;
9601 isymend = isymbuf + extsymcount;
9602 for (isym = isymbuf; isym < isymend; isym++, ever++)
9603 {
9604 const char *name;
9605 Elf_Internal_Versym iver;
9606 unsigned short version_index;
9607
9608 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9609 || isym->st_shndx == SHN_UNDEF)
9610 continue;
9611
9612 name = bfd_elf_string_from_elf_section (input,
9613 hdr->sh_link,
9614 isym->st_name);
9615 if (strcmp (name, h->root.root.string) != 0)
9616 continue;
9617
9618 _bfd_elf_swap_versym_in (input, ever, &iver);
9619
d023c380
L
9620 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9621 && !(h->def_regular
9622 && h->forced_local))
c152c796
AM
9623 {
9624 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9625 have provided a definition for the undefined sym unless
9626 it is defined in a non-shared object and forced local.
9627 */
c152c796
AM
9628 abort ();
9629 }
9630
9631 version_index = iver.vs_vers & VERSYM_VERSION;
9632 if (version_index == 1 || version_index == 2)
9633 {
9634 /* This is the base or first version. We can use it. */
9635 free (extversym);
9636 free (isymbuf);
9637 return TRUE;
9638 }
9639 }
9640
9641 free (extversym);
9642 free (isymbuf);
9643 }
9644
9645 return FALSE;
9646}
9647
b8871f35
L
9648/* Convert ELF common symbol TYPE. */
9649
9650static int
9651elf_link_convert_common_type (struct bfd_link_info *info, int type)
9652{
9653 /* Commom symbol can only appear in relocatable link. */
9654 if (!bfd_link_relocatable (info))
9655 abort ();
9656 switch (info->elf_stt_common)
9657 {
9658 case unchanged:
9659 break;
9660 case elf_stt_common:
9661 type = STT_COMMON;
9662 break;
9663 case no_elf_stt_common:
9664 type = STT_OBJECT;
9665 break;
9666 }
9667 return type;
9668}
9669
c152c796
AM
9670/* Add an external symbol to the symbol table. This is called from
9671 the hash table traversal routine. When generating a shared object,
9672 we go through the symbol table twice. The first time we output
9673 anything that might have been forced to local scope in a version
9674 script. The second time we output the symbols that are still
9675 global symbols. */
9676
9677static bfd_boolean
7686d77d 9678elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9679{
7686d77d 9680 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9681 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9682 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9683 bfd_boolean strip;
9684 Elf_Internal_Sym sym;
9685 asection *input_sec;
9686 const struct elf_backend_data *bed;
6e0b88f1
AM
9687 long indx;
9688 int ret;
b8871f35 9689 unsigned int type;
c152c796
AM
9690
9691 if (h->root.type == bfd_link_hash_warning)
9692 {
9693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9694 if (h->root.type == bfd_link_hash_new)
9695 return TRUE;
9696 }
9697
9698 /* Decide whether to output this symbol in this pass. */
9699 if (eoinfo->localsyms)
9700 {
4deb8f71 9701 if (!h->forced_local)
c152c796
AM
9702 return TRUE;
9703 }
9704 else
9705 {
4deb8f71 9706 if (h->forced_local)
c152c796
AM
9707 return TRUE;
9708 }
9709
8b127cbc 9710 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9711
12ac1cf5 9712 if (h->root.type == bfd_link_hash_undefined)
c152c796 9713 {
12ac1cf5
NC
9714 /* If we have an undefined symbol reference here then it must have
9715 come from a shared library that is being linked in. (Undefined
98da7939
L
9716 references in regular files have already been handled unless
9717 they are in unreferenced sections which are removed by garbage
9718 collection). */
12ac1cf5
NC
9719 bfd_boolean ignore_undef = FALSE;
9720
9721 /* Some symbols may be special in that the fact that they're
9722 undefined can be safely ignored - let backend determine that. */
9723 if (bed->elf_backend_ignore_undef_symbol)
9724 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9725
9726 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9727 if (!ignore_undef
12ac1cf5 9728 && h->ref_dynamic
8b127cbc
AM
9729 && (!h->ref_regular || flinfo->info->gc_sections)
9730 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9731 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9732 (*flinfo->info->callbacks->undefined_symbol)
9733 (flinfo->info, h->root.root.string,
9734 h->ref_regular ? NULL : h->root.u.undef.abfd,
9735 NULL, 0,
9736 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9737
9738 /* Strip a global symbol defined in a discarded section. */
9739 if (h->indx == -3)
9740 return TRUE;
c152c796
AM
9741 }
9742
9743 /* We should also warn if a forced local symbol is referenced from
9744 shared libraries. */
0e1862bb 9745 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9746 && h->forced_local
9747 && h->ref_dynamic
371a5866 9748 && h->def_regular
f5385ebf 9749 && !h->dynamic_def
ee659f1f 9750 && h->ref_dynamic_nonweak
8b127cbc 9751 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9752 {
17d078c5
AM
9753 bfd *def_bfd;
9754 const char *msg;
90c984fc
L
9755 struct elf_link_hash_entry *hi = h;
9756
9757 /* Check indirect symbol. */
9758 while (hi->root.type == bfd_link_hash_indirect)
9759 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9760
9761 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9762 /* xgettext:c-format */
871b3ab2 9763 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9764 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9765 /* xgettext:c-format */
871b3ab2 9766 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9767 else
695344c0 9768 /* xgettext:c-format */
871b3ab2 9769 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9770 def_bfd = flinfo->output_bfd;
90c984fc
L
9771 if (hi->root.u.def.section != bfd_abs_section_ptr)
9772 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9773 _bfd_error_handler (msg, flinfo->output_bfd,
9774 h->root.root.string, def_bfd);
17d078c5 9775 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9776 eoinfo->failed = TRUE;
9777 return FALSE;
9778 }
9779
9780 /* We don't want to output symbols that have never been mentioned by
9781 a regular file, or that we have been told to strip. However, if
9782 h->indx is set to -2, the symbol is used by a reloc and we must
9783 output it. */
d983c8c5 9784 strip = FALSE;
c152c796 9785 if (h->indx == -2)
d983c8c5 9786 ;
f5385ebf 9787 else if ((h->def_dynamic
77cfaee6
AM
9788 || h->ref_dynamic
9789 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9790 && !h->def_regular
9791 && !h->ref_regular)
c152c796 9792 strip = TRUE;
8b127cbc 9793 else if (flinfo->info->strip == strip_all)
c152c796 9794 strip = TRUE;
8b127cbc
AM
9795 else if (flinfo->info->strip == strip_some
9796 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9797 h->root.root.string, FALSE, FALSE) == NULL)
9798 strip = TRUE;
d56d55e7
AM
9799 else if ((h->root.type == bfd_link_hash_defined
9800 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9801 && ((flinfo->info->strip_discarded
dbaa2011 9802 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9803 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9804 && h->root.u.def.section->owner != NULL
d56d55e7 9805 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9806 strip = TRUE;
9e2278f5
AM
9807 else if ((h->root.type == bfd_link_hash_undefined
9808 || h->root.type == bfd_link_hash_undefweak)
9809 && h->root.u.undef.abfd != NULL
9810 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9811 strip = TRUE;
c152c796 9812
b8871f35
L
9813 type = h->type;
9814
c152c796 9815 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9816 nothing else to do. However, if it is a forced local symbol or
9817 an ifunc symbol we need to give the backend finish_dynamic_symbol
9818 function a chance to make it dynamic. */
c152c796
AM
9819 if (strip
9820 && h->dynindx == -1
b8871f35 9821 && type != STT_GNU_IFUNC
f5385ebf 9822 && !h->forced_local)
c152c796
AM
9823 return TRUE;
9824
9825 sym.st_value = 0;
9826 sym.st_size = h->size;
9827 sym.st_other = h->other;
c152c796
AM
9828 switch (h->root.type)
9829 {
9830 default:
9831 case bfd_link_hash_new:
9832 case bfd_link_hash_warning:
9833 abort ();
9834 return FALSE;
9835
9836 case bfd_link_hash_undefined:
9837 case bfd_link_hash_undefweak:
9838 input_sec = bfd_und_section_ptr;
9839 sym.st_shndx = SHN_UNDEF;
9840 break;
9841
9842 case bfd_link_hash_defined:
9843 case bfd_link_hash_defweak:
9844 {
9845 input_sec = h->root.u.def.section;
9846 if (input_sec->output_section != NULL)
9847 {
9848 sym.st_shndx =
8b127cbc 9849 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9850 input_sec->output_section);
9851 if (sym.st_shndx == SHN_BAD)
9852 {
4eca0228 9853 _bfd_error_handler
695344c0 9854 /* xgettext:c-format */
871b3ab2 9855 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9856 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9857 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9858 eoinfo->failed = TRUE;
9859 return FALSE;
9860 }
9861
9862 /* ELF symbols in relocatable files are section relative,
9863 but in nonrelocatable files they are virtual
9864 addresses. */
9865 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9866 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9867 {
9868 sym.st_value += input_sec->output_section->vma;
9869 if (h->type == STT_TLS)
9870 {
8b127cbc 9871 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9872 if (tls_sec != NULL)
9873 sym.st_value -= tls_sec->vma;
c152c796
AM
9874 }
9875 }
9876 }
9877 else
9878 {
9879 BFD_ASSERT (input_sec->owner == NULL
9880 || (input_sec->owner->flags & DYNAMIC) != 0);
9881 sym.st_shndx = SHN_UNDEF;
9882 input_sec = bfd_und_section_ptr;
9883 }
9884 }
9885 break;
9886
9887 case bfd_link_hash_common:
9888 input_sec = h->root.u.c.p->section;
a4d8e49b 9889 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9890 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9891 break;
9892
9893 case bfd_link_hash_indirect:
9894 /* These symbols are created by symbol versioning. They point
9895 to the decorated version of the name. For example, if the
9896 symbol foo@@GNU_1.2 is the default, which should be used when
9897 foo is used with no version, then we add an indirect symbol
9898 foo which points to foo@@GNU_1.2. We ignore these symbols,
9899 since the indirected symbol is already in the hash table. */
9900 return TRUE;
9901 }
9902
b8871f35
L
9903 if (type == STT_COMMON || type == STT_OBJECT)
9904 switch (h->root.type)
9905 {
9906 case bfd_link_hash_common:
9907 type = elf_link_convert_common_type (flinfo->info, type);
9908 break;
9909 case bfd_link_hash_defined:
9910 case bfd_link_hash_defweak:
9911 if (bed->common_definition (&sym))
9912 type = elf_link_convert_common_type (flinfo->info, type);
9913 else
9914 type = STT_OBJECT;
9915 break;
9916 case bfd_link_hash_undefined:
9917 case bfd_link_hash_undefweak:
9918 break;
9919 default:
9920 abort ();
9921 }
9922
4deb8f71 9923 if (h->forced_local)
b8871f35
L
9924 {
9925 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9926 /* Turn off visibility on local symbol. */
9927 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9928 }
9929 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9930 else if (h->unique_global && h->def_regular)
9931 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9932 else if (h->root.type == bfd_link_hash_undefweak
9933 || h->root.type == bfd_link_hash_defweak)
9934 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9935 else
9936 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9937 sym.st_target_internal = h->target_internal;
9938
c152c796
AM
9939 /* Give the processor backend a chance to tweak the symbol value,
9940 and also to finish up anything that needs to be done for this
9941 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9942 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9943 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9944 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9945 && h->def_regular
0e1862bb 9946 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9947 || ((h->dynindx != -1
9948 || h->forced_local)
0e1862bb 9949 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9950 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9951 || h->root.type != bfd_link_hash_undefweak))
9952 || !h->forced_local)
8b127cbc 9953 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9954 {
9955 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9956 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9957 {
9958 eoinfo->failed = TRUE;
9959 return FALSE;
9960 }
9961 }
9962
9963 /* If we are marking the symbol as undefined, and there are no
9964 non-weak references to this symbol from a regular object, then
9965 mark the symbol as weak undefined; if there are non-weak
9966 references, mark the symbol as strong. We can't do this earlier,
9967 because it might not be marked as undefined until the
9968 finish_dynamic_symbol routine gets through with it. */
9969 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9970 && h->ref_regular
c152c796
AM
9971 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9972 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9973 {
9974 int bindtype;
b8871f35 9975 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9976
9977 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9978 if (type == STT_GNU_IFUNC)
9979 type = STT_FUNC;
c152c796 9980
f5385ebf 9981 if (h->ref_regular_nonweak)
c152c796
AM
9982 bindtype = STB_GLOBAL;
9983 else
9984 bindtype = STB_WEAK;
2955ec4c 9985 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9986 }
9987
bda987c2
CD
9988 /* If this is a symbol defined in a dynamic library, don't use the
9989 symbol size from the dynamic library. Relinking an executable
9990 against a new library may introduce gratuitous changes in the
9991 executable's symbols if we keep the size. */
9992 if (sym.st_shndx == SHN_UNDEF
9993 && !h->def_regular
9994 && h->def_dynamic)
9995 sym.st_size = 0;
9996
c152c796
AM
9997 /* If a non-weak symbol with non-default visibility is not defined
9998 locally, it is a fatal error. */
0e1862bb 9999 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10000 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10001 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10002 && h->root.type == bfd_link_hash_undefined
f5385ebf 10003 && !h->def_regular)
c152c796 10004 {
17d078c5
AM
10005 const char *msg;
10006
10007 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10008 /* xgettext:c-format */
871b3ab2 10009 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10010 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10011 /* xgettext:c-format */
871b3ab2 10012 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10013 else
695344c0 10014 /* xgettext:c-format */
871b3ab2 10015 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10016 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10017 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10018 eoinfo->failed = TRUE;
10019 return FALSE;
10020 }
10021
10022 /* If this symbol should be put in the .dynsym section, then put it
10023 there now. We already know the symbol index. We also fill in
10024 the entry in the .hash section. */
cae1fbbb 10025 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 10026 && h->dynindx != -1
8b127cbc 10027 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 10028 {
c152c796
AM
10029 bfd_byte *esym;
10030
90c984fc
L
10031 /* Since there is no version information in the dynamic string,
10032 if there is no version info in symbol version section, we will
1659f720 10033 have a run-time problem if not linking executable, referenced
4deb8f71 10034 by shared library, or not bound locally. */
1659f720 10035 if (h->verinfo.verdef == NULL
0e1862bb 10036 && (!bfd_link_executable (flinfo->info)
1659f720
L
10037 || h->ref_dynamic
10038 || !h->def_regular))
90c984fc
L
10039 {
10040 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10041
10042 if (p && p [1] != '\0')
10043 {
4eca0228 10044 _bfd_error_handler
695344c0 10045 /* xgettext:c-format */
9793eb77 10046 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10047 flinfo->output_bfd, h->root.root.string);
10048 eoinfo->failed = TRUE;
10049 return FALSE;
10050 }
10051 }
10052
c152c796 10053 sym.st_name = h->dynstr_index;
cae1fbbb
L
10054 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10055 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10056 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10057 {
10058 eoinfo->failed = TRUE;
10059 return FALSE;
10060 }
8b127cbc 10061 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10062
8b127cbc 10063 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10064 {
10065 size_t hash_entry_size;
10066 bfd_byte *bucketpos;
10067 bfd_vma chain;
41198d0c
L
10068 size_t bucketcount;
10069 size_t bucket;
10070
8b127cbc 10071 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10072 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10073
10074 hash_entry_size
8b127cbc
AM
10075 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10076 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10077 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10078 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10079 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10080 bucketpos);
10081 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10082 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10083 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10084 }
c152c796 10085
8b127cbc 10086 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10087 {
10088 Elf_Internal_Versym iversym;
10089 Elf_External_Versym *eversym;
10090
f5385ebf 10091 if (!h->def_regular)
c152c796 10092 {
7b20f099
AM
10093 if (h->verinfo.verdef == NULL
10094 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10095 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10096 iversym.vs_vers = 0;
10097 else
10098 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10099 }
10100 else
10101 {
10102 if (h->verinfo.vertree == NULL)
10103 iversym.vs_vers = 1;
10104 else
10105 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10106 if (flinfo->info->create_default_symver)
3e3b46e5 10107 iversym.vs_vers++;
c152c796
AM
10108 }
10109
422f1182 10110 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10111 defined locally. */
422f1182 10112 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10113 iversym.vs_vers |= VERSYM_HIDDEN;
10114
8b127cbc 10115 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10116 eversym += h->dynindx;
8b127cbc 10117 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10118 }
10119 }
10120
d983c8c5
AM
10121 /* If the symbol is undefined, and we didn't output it to .dynsym,
10122 strip it from .symtab too. Obviously we can't do this for
10123 relocatable output or when needed for --emit-relocs. */
10124 else if (input_sec == bfd_und_section_ptr
10125 && h->indx != -2
66cae560
NC
10126 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10127 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10128 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10129 return TRUE;
66cae560 10130
d983c8c5
AM
10131 /* Also strip others that we couldn't earlier due to dynamic symbol
10132 processing. */
10133 if (strip)
10134 return TRUE;
10135 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10136 return TRUE;
10137
2ec55de3
AM
10138 /* Output a FILE symbol so that following locals are not associated
10139 with the wrong input file. We need one for forced local symbols
10140 if we've seen more than one FILE symbol or when we have exactly
10141 one FILE symbol but global symbols are present in a file other
10142 than the one with the FILE symbol. We also need one if linker
10143 defined symbols are present. In practice these conditions are
10144 always met, so just emit the FILE symbol unconditionally. */
10145 if (eoinfo->localsyms
10146 && !eoinfo->file_sym_done
10147 && eoinfo->flinfo->filesym_count != 0)
10148 {
10149 Elf_Internal_Sym fsym;
10150
10151 memset (&fsym, 0, sizeof (fsym));
10152 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10153 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10154 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10155 bfd_und_section_ptr, NULL))
2ec55de3
AM
10156 return FALSE;
10157
10158 eoinfo->file_sym_done = TRUE;
10159 }
10160
8b127cbc 10161 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10162 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10163 input_sec, h);
6e0b88f1 10164 if (ret == 0)
c152c796
AM
10165 {
10166 eoinfo->failed = TRUE;
10167 return FALSE;
10168 }
6e0b88f1
AM
10169 else if (ret == 1)
10170 h->indx = indx;
10171 else if (h->indx == -2)
10172 abort();
c152c796
AM
10173
10174 return TRUE;
10175}
10176
cdd3575c
AM
10177/* Return TRUE if special handling is done for relocs in SEC against
10178 symbols defined in discarded sections. */
10179
c152c796
AM
10180static bfd_boolean
10181elf_section_ignore_discarded_relocs (asection *sec)
10182{
10183 const struct elf_backend_data *bed;
10184
cdd3575c
AM
10185 switch (sec->sec_info_type)
10186 {
dbaa2011
AM
10187 case SEC_INFO_TYPE_STABS:
10188 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10189 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10190 return TRUE;
10191 default:
10192 break;
10193 }
c152c796
AM
10194
10195 bed = get_elf_backend_data (sec->owner);
10196 if (bed->elf_backend_ignore_discarded_relocs != NULL
10197 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10198 return TRUE;
10199
10200 return FALSE;
10201}
10202
9e66c942
AM
10203/* Return a mask saying how ld should treat relocations in SEC against
10204 symbols defined in discarded sections. If this function returns
10205 COMPLAIN set, ld will issue a warning message. If this function
10206 returns PRETEND set, and the discarded section was link-once and the
10207 same size as the kept link-once section, ld will pretend that the
10208 symbol was actually defined in the kept section. Otherwise ld will
10209 zero the reloc (at least that is the intent, but some cooperation by
10210 the target dependent code is needed, particularly for REL targets). */
10211
8a696751
AM
10212unsigned int
10213_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10214{
9e66c942 10215 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10216 return PRETEND;
cdd3575c
AM
10217
10218 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10219 return 0;
cdd3575c
AM
10220
10221 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10222 return 0;
cdd3575c 10223
9e66c942 10224 return COMPLAIN | PRETEND;
cdd3575c
AM
10225}
10226
3d7f7666
L
10227/* Find a match between a section and a member of a section group. */
10228
10229static asection *
c0f00686
L
10230match_group_member (asection *sec, asection *group,
10231 struct bfd_link_info *info)
3d7f7666
L
10232{
10233 asection *first = elf_next_in_group (group);
10234 asection *s = first;
10235
10236 while (s != NULL)
10237 {
c0f00686 10238 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10239 return s;
10240
83180ade 10241 s = elf_next_in_group (s);
3d7f7666
L
10242 if (s == first)
10243 break;
10244 }
10245
10246 return NULL;
10247}
10248
01b3c8ab 10249/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10250 to replace it. Return the replacement if it is OK. Otherwise return
10251 NULL. */
01b3c8ab
L
10252
10253asection *
c0f00686 10254_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10255{
10256 asection *kept;
10257
10258 kept = sec->kept_section;
10259 if (kept != NULL)
10260 {
c2370991 10261 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10262 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10263 if (kept != NULL
10264 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10265 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10266 kept = NULL;
c2370991 10267 sec->kept_section = kept;
01b3c8ab
L
10268 }
10269 return kept;
10270}
10271
c152c796
AM
10272/* Link an input file into the linker output file. This function
10273 handles all the sections and relocations of the input file at once.
10274 This is so that we only have to read the local symbols once, and
10275 don't have to keep them in memory. */
10276
10277static bfd_boolean
8b127cbc 10278elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10279{
ece5ef60 10280 int (*relocate_section)
c152c796
AM
10281 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10282 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10283 bfd *output_bfd;
10284 Elf_Internal_Shdr *symtab_hdr;
10285 size_t locsymcount;
10286 size_t extsymoff;
10287 Elf_Internal_Sym *isymbuf;
10288 Elf_Internal_Sym *isym;
10289 Elf_Internal_Sym *isymend;
10290 long *pindex;
10291 asection **ppsection;
10292 asection *o;
10293 const struct elf_backend_data *bed;
c152c796 10294 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10295 bfd_size_type address_size;
10296 bfd_vma r_type_mask;
10297 int r_sym_shift;
ffbc01cc 10298 bfd_boolean have_file_sym = FALSE;
c152c796 10299
8b127cbc 10300 output_bfd = flinfo->output_bfd;
c152c796
AM
10301 bed = get_elf_backend_data (output_bfd);
10302 relocate_section = bed->elf_backend_relocate_section;
10303
10304 /* If this is a dynamic object, we don't want to do anything here:
10305 we don't want the local symbols, and we don't want the section
10306 contents. */
10307 if ((input_bfd->flags & DYNAMIC) != 0)
10308 return TRUE;
10309
c152c796
AM
10310 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10311 if (elf_bad_symtab (input_bfd))
10312 {
10313 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10314 extsymoff = 0;
10315 }
10316 else
10317 {
10318 locsymcount = symtab_hdr->sh_info;
10319 extsymoff = symtab_hdr->sh_info;
10320 }
10321
10322 /* Read the local symbols. */
10323 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10324 if (isymbuf == NULL && locsymcount != 0)
10325 {
10326 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10327 flinfo->internal_syms,
10328 flinfo->external_syms,
10329 flinfo->locsym_shndx);
c152c796
AM
10330 if (isymbuf == NULL)
10331 return FALSE;
10332 }
10333
10334 /* Find local symbol sections and adjust values of symbols in
10335 SEC_MERGE sections. Write out those local symbols we know are
10336 going into the output file. */
10337 isymend = isymbuf + locsymcount;
8b127cbc 10338 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10339 isym < isymend;
10340 isym++, pindex++, ppsection++)
10341 {
10342 asection *isec;
10343 const char *name;
10344 Elf_Internal_Sym osym;
6e0b88f1
AM
10345 long indx;
10346 int ret;
c152c796
AM
10347
10348 *pindex = -1;
10349
10350 if (elf_bad_symtab (input_bfd))
10351 {
10352 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10353 {
10354 *ppsection = NULL;
10355 continue;
10356 }
10357 }
10358
10359 if (isym->st_shndx == SHN_UNDEF)
10360 isec = bfd_und_section_ptr;
c152c796
AM
10361 else if (isym->st_shndx == SHN_ABS)
10362 isec = bfd_abs_section_ptr;
10363 else if (isym->st_shndx == SHN_COMMON)
10364 isec = bfd_com_section_ptr;
10365 else
10366 {
cb33740c
AM
10367 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10368 if (isec == NULL)
10369 {
10370 /* Don't attempt to output symbols with st_shnx in the
10371 reserved range other than SHN_ABS and SHN_COMMON. */
10372 *ppsection = NULL;
10373 continue;
10374 }
dbaa2011 10375 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10376 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10377 isym->st_value =
10378 _bfd_merged_section_offset (output_bfd, &isec,
10379 elf_section_data (isec)->sec_info,
10380 isym->st_value);
c152c796
AM
10381 }
10382
10383 *ppsection = isec;
10384
d983c8c5
AM
10385 /* Don't output the first, undefined, symbol. In fact, don't
10386 output any undefined local symbol. */
10387 if (isec == bfd_und_section_ptr)
c152c796
AM
10388 continue;
10389
10390 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10391 {
10392 /* We never output section symbols. Instead, we use the
10393 section symbol of the corresponding section in the output
10394 file. */
10395 continue;
10396 }
10397
10398 /* If we are stripping all symbols, we don't want to output this
10399 one. */
8b127cbc 10400 if (flinfo->info->strip == strip_all)
c152c796
AM
10401 continue;
10402
10403 /* If we are discarding all local symbols, we don't want to
10404 output this one. If we are generating a relocatable output
10405 file, then some of the local symbols may be required by
10406 relocs; we output them below as we discover that they are
10407 needed. */
8b127cbc 10408 if (flinfo->info->discard == discard_all)
c152c796
AM
10409 continue;
10410
10411 /* If this symbol is defined in a section which we are
f02571c5
AM
10412 discarding, we don't need to keep it. */
10413 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10414 && isym->st_shndx < SHN_LORESERVE
10415 && bfd_section_removed_from_list (output_bfd,
10416 isec->output_section))
e75a280b
L
10417 continue;
10418
c152c796
AM
10419 /* Get the name of the symbol. */
10420 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10421 isym->st_name);
10422 if (name == NULL)
10423 return FALSE;
10424
10425 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10426 if ((flinfo->info->strip == strip_some
10427 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10428 == NULL))
8b127cbc 10429 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10430 && (isec->flags & SEC_MERGE)
10431 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10432 || flinfo->info->discard == discard_l)
c152c796
AM
10433 && bfd_is_local_label_name (input_bfd, name)))
10434 continue;
10435
ffbc01cc
AM
10436 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10437 {
ce875075
AM
10438 if (input_bfd->lto_output)
10439 /* -flto puts a temp file name here. This means builds
10440 are not reproducible. Discard the symbol. */
10441 continue;
ffbc01cc
AM
10442 have_file_sym = TRUE;
10443 flinfo->filesym_count += 1;
10444 }
10445 if (!have_file_sym)
10446 {
10447 /* In the absence of debug info, bfd_find_nearest_line uses
10448 FILE symbols to determine the source file for local
10449 function symbols. Provide a FILE symbol here if input
10450 files lack such, so that their symbols won't be
10451 associated with a previous input file. It's not the
10452 source file, but the best we can do. */
10453 have_file_sym = TRUE;
10454 flinfo->filesym_count += 1;
10455 memset (&osym, 0, sizeof (osym));
10456 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10457 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10458 if (!elf_link_output_symstrtab (flinfo,
10459 (input_bfd->lto_output ? NULL
10460 : input_bfd->filename),
10461 &osym, bfd_abs_section_ptr,
10462 NULL))
ffbc01cc
AM
10463 return FALSE;
10464 }
10465
c152c796
AM
10466 osym = *isym;
10467
10468 /* Adjust the section index for the output file. */
10469 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10470 isec->output_section);
10471 if (osym.st_shndx == SHN_BAD)
10472 return FALSE;
10473
c152c796
AM
10474 /* ELF symbols in relocatable files are section relative, but
10475 in executable files they are virtual addresses. Note that
10476 this code assumes that all ELF sections have an associated
10477 BFD section with a reasonable value for output_offset; below
10478 we assume that they also have a reasonable value for
10479 output_section. Any special sections must be set up to meet
10480 these requirements. */
10481 osym.st_value += isec->output_offset;
0e1862bb 10482 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10483 {
10484 osym.st_value += isec->output_section->vma;
10485 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10486 {
10487 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10488 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10489 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10490 }
10491 }
10492
6e0b88f1 10493 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10494 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10495 if (ret == 0)
c152c796 10496 return FALSE;
6e0b88f1
AM
10497 else if (ret == 1)
10498 *pindex = indx;
c152c796
AM
10499 }
10500
310fd250
L
10501 if (bed->s->arch_size == 32)
10502 {
10503 r_type_mask = 0xff;
10504 r_sym_shift = 8;
10505 address_size = 4;
10506 }
10507 else
10508 {
10509 r_type_mask = 0xffffffff;
10510 r_sym_shift = 32;
10511 address_size = 8;
10512 }
10513
c152c796
AM
10514 /* Relocate the contents of each section. */
10515 sym_hashes = elf_sym_hashes (input_bfd);
10516 for (o = input_bfd->sections; o != NULL; o = o->next)
10517 {
10518 bfd_byte *contents;
10519
10520 if (! o->linker_mark)
10521 {
10522 /* This section was omitted from the link. */
10523 continue;
10524 }
10525
7bdf4127 10526 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10527 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10528 {
10529 /* Deal with the group signature symbol. */
10530 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10531 unsigned long symndx = sec_data->this_hdr.sh_info;
10532 asection *osec = o->output_section;
10533
7bdf4127 10534 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10535 if (symndx >= locsymcount
10536 || (elf_bad_symtab (input_bfd)
8b127cbc 10537 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10538 {
10539 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10540 while (h->root.type == bfd_link_hash_indirect
10541 || h->root.type == bfd_link_hash_warning)
10542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10543 /* Arrange for symbol to be output. */
10544 h->indx = -2;
10545 elf_section_data (osec)->this_hdr.sh_info = -2;
10546 }
10547 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10548 {
10549 /* We'll use the output section target_index. */
8b127cbc 10550 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10551 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10552 }
10553 else
10554 {
8b127cbc 10555 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10556 {
10557 /* Otherwise output the local symbol now. */
10558 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10559 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10560 const char *name;
6e0b88f1
AM
10561 long indx;
10562 int ret;
bcacc0f5
AM
10563
10564 name = bfd_elf_string_from_elf_section (input_bfd,
10565 symtab_hdr->sh_link,
10566 sym.st_name);
10567 if (name == NULL)
10568 return FALSE;
10569
10570 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10571 sec);
10572 if (sym.st_shndx == SHN_BAD)
10573 return FALSE;
10574
10575 sym.st_value += o->output_offset;
10576
6e0b88f1 10577 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10578 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10579 NULL);
6e0b88f1 10580 if (ret == 0)
bcacc0f5 10581 return FALSE;
6e0b88f1 10582 else if (ret == 1)
8b127cbc 10583 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10584 else
10585 abort ();
bcacc0f5
AM
10586 }
10587 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10588 = flinfo->indices[symndx];
bcacc0f5
AM
10589 }
10590 }
10591
c152c796 10592 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10593 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10594 continue;
10595
10596 if ((o->flags & SEC_LINKER_CREATED) != 0)
10597 {
10598 /* Section was created by _bfd_elf_link_create_dynamic_sections
10599 or somesuch. */
10600 continue;
10601 }
10602
10603 /* Get the contents of the section. They have been cached by a
10604 relaxation routine. Note that o is a section in an input
10605 file, so the contents field will not have been set by any of
10606 the routines which work on output files. */
10607 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10608 {
10609 contents = elf_section_data (o)->this_hdr.contents;
10610 if (bed->caches_rawsize
10611 && o->rawsize != 0
10612 && o->rawsize < o->size)
10613 {
10614 memcpy (flinfo->contents, contents, o->rawsize);
10615 contents = flinfo->contents;
10616 }
10617 }
c152c796
AM
10618 else
10619 {
8b127cbc 10620 contents = flinfo->contents;
4a114e3e 10621 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10622 return FALSE;
10623 }
10624
10625 if ((o->flags & SEC_RELOC) != 0)
10626 {
10627 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10628 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10629 int action_discarded;
ece5ef60 10630 int ret;
c152c796
AM
10631
10632 /* Get the swapped relocs. */
10633 internal_relocs
8b127cbc
AM
10634 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10635 flinfo->internal_relocs, FALSE);
c152c796
AM
10636 if (internal_relocs == NULL
10637 && o->reloc_count > 0)
10638 return FALSE;
10639
310fd250
L
10640 /* We need to reverse-copy input .ctors/.dtors sections if
10641 they are placed in .init_array/.finit_array for output. */
10642 if (o->size > address_size
10643 && ((strncmp (o->name, ".ctors", 6) == 0
10644 && strcmp (o->output_section->name,
10645 ".init_array") == 0)
10646 || (strncmp (o->name, ".dtors", 6) == 0
10647 && strcmp (o->output_section->name,
10648 ".fini_array") == 0))
10649 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10650 {
056bafd4
MR
10651 if (o->size * bed->s->int_rels_per_ext_rel
10652 != o->reloc_count * address_size)
310fd250 10653 {
4eca0228 10654 _bfd_error_handler
695344c0 10655 /* xgettext:c-format */
871b3ab2 10656 (_("error: %pB: size of section %pA is not "
310fd250
L
10657 "multiple of address size"),
10658 input_bfd, o);
8c6716e5 10659 bfd_set_error (bfd_error_bad_value);
310fd250
L
10660 return FALSE;
10661 }
10662 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10663 }
10664
0f02bbd9 10665 action_discarded = -1;
c152c796 10666 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10667 action_discarded = (*bed->action_discarded) (o);
10668
10669 /* Run through the relocs evaluating complex reloc symbols and
10670 looking for relocs against symbols from discarded sections
10671 or section symbols from removed link-once sections.
10672 Complain about relocs against discarded sections. Zero
10673 relocs against removed link-once sections. */
10674
10675 rel = internal_relocs;
056bafd4 10676 relend = rel + o->reloc_count;
0f02bbd9 10677 for ( ; rel < relend; rel++)
c152c796 10678 {
0f02bbd9
AM
10679 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10680 unsigned int s_type;
10681 asection **ps, *sec;
10682 struct elf_link_hash_entry *h = NULL;
10683 const char *sym_name;
c152c796 10684
0f02bbd9
AM
10685 if (r_symndx == STN_UNDEF)
10686 continue;
c152c796 10687
0f02bbd9
AM
10688 if (r_symndx >= locsymcount
10689 || (elf_bad_symtab (input_bfd)
8b127cbc 10690 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10691 {
10692 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10693
0f02bbd9
AM
10694 /* Badly formatted input files can contain relocs that
10695 reference non-existant symbols. Check here so that
10696 we do not seg fault. */
10697 if (h == NULL)
c152c796 10698 {
4eca0228 10699 _bfd_error_handler
695344c0 10700 /* xgettext:c-format */
2dcf00ce 10701 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10702 "that references a non-existent global symbol"),
2dcf00ce 10703 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10704 bfd_set_error (bfd_error_bad_value);
10705 return FALSE;
10706 }
3b36f7e6 10707
0f02bbd9
AM
10708 while (h->root.type == bfd_link_hash_indirect
10709 || h->root.type == bfd_link_hash_warning)
10710 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10711
0f02bbd9 10712 s_type = h->type;
cdd3575c 10713
9e2dec47 10714 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10715 mark the symbol as undefined. Note that the
10716 linker may attach linker created dynamic sections
10717 to the plugin bfd. Symbols defined in linker
10718 created sections are not plugin symbols. */
bc4e12de 10719 if ((h->root.non_ir_ref_regular
4070765b 10720 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10721 && (h->root.type == bfd_link_hash_defined
10722 || h->root.type == bfd_link_hash_defweak)
10723 && (h->root.u.def.section->flags
10724 & SEC_LINKER_CREATED) == 0
10725 && h->root.u.def.section->owner != NULL
10726 && (h->root.u.def.section->owner->flags
10727 & BFD_PLUGIN) != 0)
10728 {
10729 h->root.type = bfd_link_hash_undefined;
10730 h->root.u.undef.abfd = h->root.u.def.section->owner;
10731 }
10732
0f02bbd9
AM
10733 ps = NULL;
10734 if (h->root.type == bfd_link_hash_defined
10735 || h->root.type == bfd_link_hash_defweak)
10736 ps = &h->root.u.def.section;
10737
10738 sym_name = h->root.root.string;
10739 }
10740 else
10741 {
10742 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10743
10744 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10745 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10746 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10747 sym, *ps);
10748 }
c152c796 10749
c301e700 10750 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10751 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10752 {
10753 bfd_vma val;
10754 bfd_vma dot = (rel->r_offset
10755 + o->output_offset + o->output_section->vma);
10756#ifdef DEBUG
10757 printf ("Encountered a complex symbol!");
10758 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10759 input_bfd->filename, o->name,
10760 (long) (rel - internal_relocs));
0f02bbd9
AM
10761 printf (" symbol: idx %8.8lx, name %s\n",
10762 r_symndx, sym_name);
10763 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10764 (unsigned long) rel->r_info,
10765 (unsigned long) rel->r_offset);
10766#endif
8b127cbc 10767 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10768 isymbuf, locsymcount, s_type == STT_SRELC))
10769 return FALSE;
10770
10771 /* Symbol evaluated OK. Update to absolute value. */
10772 set_symbol_value (input_bfd, isymbuf, locsymcount,
10773 r_symndx, val);
10774 continue;
10775 }
10776
10777 if (action_discarded != -1 && ps != NULL)
10778 {
cdd3575c
AM
10779 /* Complain if the definition comes from a
10780 discarded section. */
dbaa2011 10781 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10782 {
cf35638d 10783 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10784 if (action_discarded & COMPLAIN)
8b127cbc 10785 (*flinfo->info->callbacks->einfo)
695344c0 10786 /* xgettext:c-format */
871b3ab2
AM
10787 (_("%X`%s' referenced in section `%pA' of %pB: "
10788 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10789 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10790
87e5235d 10791 /* Try to do the best we can to support buggy old
e0ae6d6f 10792 versions of gcc. Pretend that the symbol is
87e5235d
AM
10793 really defined in the kept linkonce section.
10794 FIXME: This is quite broken. Modifying the
10795 symbol here means we will be changing all later
e0ae6d6f 10796 uses of the symbol, not just in this section. */
0f02bbd9 10797 if (action_discarded & PRETEND)
87e5235d 10798 {
01b3c8ab
L
10799 asection *kept;
10800
c0f00686 10801 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10802 flinfo->info);
01b3c8ab 10803 if (kept != NULL)
87e5235d
AM
10804 {
10805 *ps = kept;
10806 continue;
10807 }
10808 }
c152c796
AM
10809 }
10810 }
10811 }
10812
10813 /* Relocate the section by invoking a back end routine.
10814
10815 The back end routine is responsible for adjusting the
10816 section contents as necessary, and (if using Rela relocs
10817 and generating a relocatable output file) adjusting the
10818 reloc addend as necessary.
10819
10820 The back end routine does not have to worry about setting
10821 the reloc address or the reloc symbol index.
10822
10823 The back end routine is given a pointer to the swapped in
10824 internal symbols, and can access the hash table entries
10825 for the external symbols via elf_sym_hashes (input_bfd).
10826
10827 When generating relocatable output, the back end routine
10828 must handle STB_LOCAL/STT_SECTION symbols specially. The
10829 output symbol is going to be a section symbol
10830 corresponding to the output section, which will require
10831 the addend to be adjusted. */
10832
8b127cbc 10833 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10834 input_bfd, o, contents,
10835 internal_relocs,
10836 isymbuf,
8b127cbc 10837 flinfo->sections);
ece5ef60 10838 if (!ret)
c152c796
AM
10839 return FALSE;
10840
ece5ef60 10841 if (ret == 2
0e1862bb 10842 || bfd_link_relocatable (flinfo->info)
8b127cbc 10843 || flinfo->info->emitrelocations)
c152c796
AM
10844 {
10845 Elf_Internal_Rela *irela;
d4730f92 10846 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10847 bfd_vma last_offset;
10848 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10849 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10850 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10851 unsigned int next_erel;
c152c796 10852 bfd_boolean rela_normal;
d4730f92 10853 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10854
d4730f92
BS
10855 esdi = elf_section_data (o);
10856 esdo = elf_section_data (o->output_section);
10857 rela_normal = FALSE;
c152c796
AM
10858
10859 /* Adjust the reloc addresses and symbol indices. */
10860
10861 irela = internal_relocs;
056bafd4 10862 irelaend = irela + o->reloc_count;
d4730f92
BS
10863 rel_hash = esdo->rel.hashes + esdo->rel.count;
10864 /* We start processing the REL relocs, if any. When we reach
10865 IRELAMID in the loop, we switch to the RELA relocs. */
10866 irelamid = irela;
10867 if (esdi->rel.hdr != NULL)
10868 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10869 * bed->s->int_rels_per_ext_rel);
eac338cf 10870 rel_hash_list = rel_hash;
d4730f92 10871 rela_hash_list = NULL;
c152c796 10872 last_offset = o->output_offset;
0e1862bb 10873 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10874 last_offset += o->output_section->vma;
10875 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10876 {
10877 unsigned long r_symndx;
10878 asection *sec;
10879 Elf_Internal_Sym sym;
10880
10881 if (next_erel == bed->s->int_rels_per_ext_rel)
10882 {
10883 rel_hash++;
10884 next_erel = 0;
10885 }
10886
d4730f92
BS
10887 if (irela == irelamid)
10888 {
10889 rel_hash = esdo->rela.hashes + esdo->rela.count;
10890 rela_hash_list = rel_hash;
10891 rela_normal = bed->rela_normal;
10892 }
10893
c152c796 10894 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10895 flinfo->info, o,
c152c796
AM
10896 irela->r_offset);
10897 if (irela->r_offset >= (bfd_vma) -2)
10898 {
10899 /* This is a reloc for a deleted entry or somesuch.
10900 Turn it into an R_*_NONE reloc, at the same
10901 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10902 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10903 being ordered. */
10904 irela->r_offset = last_offset;
10905 irela->r_info = 0;
10906 irela->r_addend = 0;
10907 continue;
10908 }
10909
10910 irela->r_offset += o->output_offset;
10911
10912 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10913 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10914 irela->r_offset += o->output_section->vma;
10915
10916 last_offset = irela->r_offset;
10917
10918 r_symndx = irela->r_info >> r_sym_shift;
10919 if (r_symndx == STN_UNDEF)
10920 continue;
10921
10922 if (r_symndx >= locsymcount
10923 || (elf_bad_symtab (input_bfd)
8b127cbc 10924 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10925 {
10926 struct elf_link_hash_entry *rh;
10927 unsigned long indx;
10928
10929 /* This is a reloc against a global symbol. We
10930 have not yet output all the local symbols, so
10931 we do not know the symbol index of any global
10932 symbol. We set the rel_hash entry for this
10933 reloc to point to the global hash table entry
10934 for this symbol. The symbol index is then
ee75fd95 10935 set at the end of bfd_elf_final_link. */
c152c796
AM
10936 indx = r_symndx - extsymoff;
10937 rh = elf_sym_hashes (input_bfd)[indx];
10938 while (rh->root.type == bfd_link_hash_indirect
10939 || rh->root.type == bfd_link_hash_warning)
10940 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10941
10942 /* Setting the index to -2 tells
10943 elf_link_output_extsym that this symbol is
10944 used by a reloc. */
10945 BFD_ASSERT (rh->indx < 0);
10946 rh->indx = -2;
c152c796
AM
10947 *rel_hash = rh;
10948
10949 continue;
10950 }
10951
10952 /* This is a reloc against a local symbol. */
10953
10954 *rel_hash = NULL;
10955 sym = isymbuf[r_symndx];
8b127cbc 10956 sec = flinfo->sections[r_symndx];
c152c796
AM
10957 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10958 {
10959 /* I suppose the backend ought to fill in the
10960 section of any STT_SECTION symbol against a
6a8d1586 10961 processor specific section. */
cf35638d 10962 r_symndx = STN_UNDEF;
6a8d1586
AM
10963 if (bfd_is_abs_section (sec))
10964 ;
c152c796
AM
10965 else if (sec == NULL || sec->owner == NULL)
10966 {
10967 bfd_set_error (bfd_error_bad_value);
10968 return FALSE;
10969 }
10970 else
10971 {
6a8d1586
AM
10972 asection *osec = sec->output_section;
10973
10974 /* If we have discarded a section, the output
10975 section will be the absolute section. In
ab96bf03
AM
10976 case of discarded SEC_MERGE sections, use
10977 the kept section. relocate_section should
10978 have already handled discarded linkonce
10979 sections. */
6a8d1586
AM
10980 if (bfd_is_abs_section (osec)
10981 && sec->kept_section != NULL
10982 && sec->kept_section->output_section != NULL)
10983 {
10984 osec = sec->kept_section->output_section;
10985 irela->r_addend -= osec->vma;
10986 }
10987
10988 if (!bfd_is_abs_section (osec))
10989 {
10990 r_symndx = osec->target_index;
cf35638d 10991 if (r_symndx == STN_UNDEF)
74541ad4 10992 {
051d833a
AM
10993 irela->r_addend += osec->vma;
10994 osec = _bfd_nearby_section (output_bfd, osec,
10995 osec->vma);
10996 irela->r_addend -= osec->vma;
10997 r_symndx = osec->target_index;
74541ad4 10998 }
6a8d1586 10999 }
c152c796
AM
11000 }
11001
11002 /* Adjust the addend according to where the
11003 section winds up in the output section. */
11004 if (rela_normal)
11005 irela->r_addend += sec->output_offset;
11006 }
11007 else
11008 {
8b127cbc 11009 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11010 {
11011 unsigned long shlink;
11012 const char *name;
11013 asection *osec;
6e0b88f1 11014 long indx;
c152c796 11015
8b127cbc 11016 if (flinfo->info->strip == strip_all)
c152c796
AM
11017 {
11018 /* You can't do ld -r -s. */
11019 bfd_set_error (bfd_error_invalid_operation);
11020 return FALSE;
11021 }
11022
11023 /* This symbol was skipped earlier, but
11024 since it is needed by a reloc, we
11025 must output it now. */
11026 shlink = symtab_hdr->sh_link;
11027 name = (bfd_elf_string_from_elf_section
11028 (input_bfd, shlink, sym.st_name));
11029 if (name == NULL)
11030 return FALSE;
11031
11032 osec = sec->output_section;
11033 sym.st_shndx =
11034 _bfd_elf_section_from_bfd_section (output_bfd,
11035 osec);
11036 if (sym.st_shndx == SHN_BAD)
11037 return FALSE;
11038
11039 sym.st_value += sec->output_offset;
0e1862bb 11040 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11041 {
11042 sym.st_value += osec->vma;
11043 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11044 {
11045 /* STT_TLS symbols are relative to PT_TLS
11046 segment base. */
8b127cbc 11047 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 11048 ->tls_sec != NULL);
8b127cbc 11049 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
11050 ->tls_sec->vma);
11051 }
11052 }
11053
6e0b88f1 11054 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11055 ret = elf_link_output_symstrtab (flinfo, name,
11056 &sym, sec,
11057 NULL);
6e0b88f1 11058 if (ret == 0)
c152c796 11059 return FALSE;
6e0b88f1 11060 else if (ret == 1)
8b127cbc 11061 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11062 else
11063 abort ();
c152c796
AM
11064 }
11065
8b127cbc 11066 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11067 }
11068
11069 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11070 | (irela->r_info & r_type_mask));
11071 }
11072
11073 /* Swap out the relocs. */
d4730f92
BS
11074 input_rel_hdr = esdi->rel.hdr;
11075 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11076 {
d4730f92
BS
11077 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11078 input_rel_hdr,
11079 internal_relocs,
11080 rel_hash_list))
11081 return FALSE;
c152c796
AM
11082 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11083 * bed->s->int_rels_per_ext_rel);
eac338cf 11084 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11085 }
11086
11087 input_rela_hdr = esdi->rela.hdr;
11088 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11089 {
eac338cf 11090 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11091 input_rela_hdr,
eac338cf 11092 internal_relocs,
d4730f92 11093 rela_hash_list))
c152c796
AM
11094 return FALSE;
11095 }
11096 }
11097 }
11098
11099 /* Write out the modified section contents. */
11100 if (bed->elf_backend_write_section
8b127cbc 11101 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11102 contents))
c152c796
AM
11103 {
11104 /* Section written out. */
11105 }
11106 else switch (o->sec_info_type)
11107 {
dbaa2011 11108 case SEC_INFO_TYPE_STABS:
c152c796
AM
11109 if (! (_bfd_write_section_stabs
11110 (output_bfd,
8b127cbc 11111 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11112 o, &elf_section_data (o)->sec_info, contents)))
11113 return FALSE;
11114 break;
dbaa2011 11115 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11116 if (! _bfd_write_merged_section (output_bfd, o,
11117 elf_section_data (o)->sec_info))
11118 return FALSE;
11119 break;
dbaa2011 11120 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11121 {
8b127cbc 11122 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11123 o, contents))
11124 return FALSE;
11125 }
11126 break;
2f0c68f2
CM
11127 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11128 {
11129 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11130 flinfo->info,
11131 o, contents))
11132 return FALSE;
11133 }
11134 break;
c152c796
AM
11135 default:
11136 {
310fd250
L
11137 if (! (o->flags & SEC_EXCLUDE))
11138 {
11139 file_ptr offset = (file_ptr) o->output_offset;
11140 bfd_size_type todo = o->size;
37b01f6a
DG
11141
11142 offset *= bfd_octets_per_byte (output_bfd);
11143
310fd250
L
11144 if ((o->flags & SEC_ELF_REVERSE_COPY))
11145 {
11146 /* Reverse-copy input section to output. */
11147 do
11148 {
11149 todo -= address_size;
11150 if (! bfd_set_section_contents (output_bfd,
11151 o->output_section,
11152 contents + todo,
11153 offset,
11154 address_size))
11155 return FALSE;
11156 if (todo == 0)
11157 break;
11158 offset += address_size;
11159 }
11160 while (1);
11161 }
11162 else if (! bfd_set_section_contents (output_bfd,
11163 o->output_section,
11164 contents,
11165 offset, todo))
11166 return FALSE;
11167 }
c152c796
AM
11168 }
11169 break;
11170 }
11171 }
11172
11173 return TRUE;
11174}
11175
11176/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11177 requested by the linker, and does not come from any input file. This
c152c796
AM
11178 is used to build constructor and destructor tables when linking
11179 with -Ur. */
11180
11181static bfd_boolean
11182elf_reloc_link_order (bfd *output_bfd,
11183 struct bfd_link_info *info,
11184 asection *output_section,
11185 struct bfd_link_order *link_order)
11186{
11187 reloc_howto_type *howto;
11188 long indx;
11189 bfd_vma offset;
11190 bfd_vma addend;
d4730f92 11191 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11192 struct elf_link_hash_entry **rel_hash_ptr;
11193 Elf_Internal_Shdr *rel_hdr;
11194 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11195 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11196 bfd_byte *erel;
11197 unsigned int i;
d4730f92 11198 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11199
11200 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11201 if (howto == NULL)
11202 {
11203 bfd_set_error (bfd_error_bad_value);
11204 return FALSE;
11205 }
11206
11207 addend = link_order->u.reloc.p->addend;
11208
d4730f92
BS
11209 if (esdo->rel.hdr)
11210 reldata = &esdo->rel;
11211 else if (esdo->rela.hdr)
11212 reldata = &esdo->rela;
11213 else
11214 {
11215 reldata = NULL;
11216 BFD_ASSERT (0);
11217 }
11218
c152c796 11219 /* Figure out the symbol index. */
d4730f92 11220 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11221 if (link_order->type == bfd_section_reloc_link_order)
11222 {
11223 indx = link_order->u.reloc.p->u.section->target_index;
11224 BFD_ASSERT (indx != 0);
11225 *rel_hash_ptr = NULL;
11226 }
11227 else
11228 {
11229 struct elf_link_hash_entry *h;
11230
11231 /* Treat a reloc against a defined symbol as though it were
11232 actually against the section. */
11233 h = ((struct elf_link_hash_entry *)
11234 bfd_wrapped_link_hash_lookup (output_bfd, info,
11235 link_order->u.reloc.p->u.name,
11236 FALSE, FALSE, TRUE));
11237 if (h != NULL
11238 && (h->root.type == bfd_link_hash_defined
11239 || h->root.type == bfd_link_hash_defweak))
11240 {
11241 asection *section;
11242
11243 section = h->root.u.def.section;
11244 indx = section->output_section->target_index;
11245 *rel_hash_ptr = NULL;
11246 /* It seems that we ought to add the symbol value to the
11247 addend here, but in practice it has already been added
11248 because it was passed to constructor_callback. */
11249 addend += section->output_section->vma + section->output_offset;
11250 }
11251 else if (h != NULL)
11252 {
11253 /* Setting the index to -2 tells elf_link_output_extsym that
11254 this symbol is used by a reloc. */
11255 h->indx = -2;
11256 *rel_hash_ptr = h;
11257 indx = 0;
11258 }
11259 else
11260 {
1a72702b
AM
11261 (*info->callbacks->unattached_reloc)
11262 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11263 indx = 0;
11264 }
11265 }
11266
11267 /* If this is an inplace reloc, we must write the addend into the
11268 object file. */
11269 if (howto->partial_inplace && addend != 0)
11270 {
11271 bfd_size_type size;
11272 bfd_reloc_status_type rstat;
11273 bfd_byte *buf;
11274 bfd_boolean ok;
11275 const char *sym_name;
11276
a50b1753
NC
11277 size = (bfd_size_type) bfd_get_reloc_size (howto);
11278 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11279 if (buf == NULL && size != 0)
c152c796
AM
11280 return FALSE;
11281 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11282 switch (rstat)
11283 {
11284 case bfd_reloc_ok:
11285 break;
11286
11287 default:
11288 case bfd_reloc_outofrange:
11289 abort ();
11290
11291 case bfd_reloc_overflow:
11292 if (link_order->type == bfd_section_reloc_link_order)
11293 sym_name = bfd_section_name (output_bfd,
11294 link_order->u.reloc.p->u.section);
11295 else
11296 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11297 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11298 howto->name, addend, NULL, NULL,
11299 (bfd_vma) 0);
c152c796
AM
11300 break;
11301 }
37b01f6a 11302
c152c796 11303 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11304 link_order->offset
11305 * bfd_octets_per_byte (output_bfd),
11306 size);
c152c796
AM
11307 free (buf);
11308 if (! ok)
11309 return FALSE;
11310 }
11311
11312 /* The address of a reloc is relative to the section in a
11313 relocatable file, and is a virtual address in an executable
11314 file. */
11315 offset = link_order->offset;
0e1862bb 11316 if (! bfd_link_relocatable (info))
c152c796
AM
11317 offset += output_section->vma;
11318
11319 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11320 {
11321 irel[i].r_offset = offset;
11322 irel[i].r_info = 0;
11323 irel[i].r_addend = 0;
11324 }
11325 if (bed->s->arch_size == 32)
11326 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11327 else
11328 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11329
d4730f92 11330 rel_hdr = reldata->hdr;
c152c796
AM
11331 erel = rel_hdr->contents;
11332 if (rel_hdr->sh_type == SHT_REL)
11333 {
d4730f92 11334 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11335 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11336 }
11337 else
11338 {
11339 irel[0].r_addend = addend;
d4730f92 11340 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11341 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11342 }
11343
d4730f92 11344 ++reldata->count;
c152c796
AM
11345
11346 return TRUE;
11347}
11348
0b52efa6
PB
11349
11350/* Get the output vma of the section pointed to by the sh_link field. */
11351
11352static bfd_vma
11353elf_get_linked_section_vma (struct bfd_link_order *p)
11354{
11355 Elf_Internal_Shdr **elf_shdrp;
11356 asection *s;
11357 int elfsec;
11358
11359 s = p->u.indirect.section;
11360 elf_shdrp = elf_elfsections (s->owner);
11361 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11362 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11363 /* PR 290:
11364 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11365 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11366 sh_info fields. Hence we could get the situation
11367 where elfsec is 0. */
11368 if (elfsec == 0)
11369 {
11370 const struct elf_backend_data *bed
11371 = get_elf_backend_data (s->owner);
11372 if (bed->link_order_error_handler)
d003868e 11373 bed->link_order_error_handler
695344c0 11374 /* xgettext:c-format */
871b3ab2 11375 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11376 return 0;
11377 }
11378 else
11379 {
11380 s = elf_shdrp[elfsec]->bfd_section;
11381 return s->output_section->vma + s->output_offset;
11382 }
0b52efa6
PB
11383}
11384
11385
11386/* Compare two sections based on the locations of the sections they are
11387 linked to. Used by elf_fixup_link_order. */
11388
11389static int
11390compare_link_order (const void * a, const void * b)
11391{
11392 bfd_vma apos;
11393 bfd_vma bpos;
11394
11395 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11396 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11397 if (apos < bpos)
11398 return -1;
11399 return apos > bpos;
11400}
11401
11402
11403/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11404 order as their linked sections. Returns false if this could not be done
11405 because an output section includes both ordered and unordered
11406 sections. Ideally we'd do this in the linker proper. */
11407
11408static bfd_boolean
11409elf_fixup_link_order (bfd *abfd, asection *o)
11410{
11411 int seen_linkorder;
11412 int seen_other;
11413 int n;
11414 struct bfd_link_order *p;
11415 bfd *sub;
11416 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11417 unsigned elfsec;
0b52efa6 11418 struct bfd_link_order **sections;
d33cdfe3 11419 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11420 bfd_vma offset;
3b36f7e6 11421
d33cdfe3
L
11422 other_sec = NULL;
11423 linkorder_sec = NULL;
0b52efa6
PB
11424 seen_other = 0;
11425 seen_linkorder = 0;
8423293d 11426 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11427 {
d33cdfe3 11428 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11429 {
11430 s = p->u.indirect.section;
d33cdfe3
L
11431 sub = s->owner;
11432 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11433 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11434 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11435 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11436 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11437 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11438 {
11439 seen_linkorder++;
11440 linkorder_sec = s;
11441 }
0b52efa6 11442 else
d33cdfe3
L
11443 {
11444 seen_other++;
11445 other_sec = s;
11446 }
0b52efa6
PB
11447 }
11448 else
11449 seen_other++;
d33cdfe3
L
11450
11451 if (seen_other && seen_linkorder)
11452 {
11453 if (other_sec && linkorder_sec)
4eca0228 11454 _bfd_error_handler
695344c0 11455 /* xgettext:c-format */
871b3ab2
AM
11456 (_("%pA has both ordered [`%pA' in %pB] "
11457 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11458 o, linkorder_sec, linkorder_sec->owner,
11459 other_sec, other_sec->owner);
d33cdfe3 11460 else
4eca0228 11461 _bfd_error_handler
871b3ab2 11462 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11463 bfd_set_error (bfd_error_bad_value);
11464 return FALSE;
11465 }
0b52efa6
PB
11466 }
11467
11468 if (!seen_linkorder)
11469 return TRUE;
11470
0b52efa6 11471 sections = (struct bfd_link_order **)
14b1c01e
AM
11472 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11473 if (sections == NULL)
11474 return FALSE;
0b52efa6 11475 seen_linkorder = 0;
3b36f7e6 11476
8423293d 11477 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11478 {
11479 sections[seen_linkorder++] = p;
11480 }
11481 /* Sort the input sections in the order of their linked section. */
11482 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11483 compare_link_order);
11484
11485 /* Change the offsets of the sections. */
11486 offset = 0;
11487 for (n = 0; n < seen_linkorder; n++)
11488 {
11489 s = sections[n]->u.indirect.section;
461686a3 11490 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11491 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11492 sections[n]->offset = offset;
11493 offset += sections[n]->size;
11494 }
11495
4dd07732 11496 free (sections);
0b52efa6
PB
11497 return TRUE;
11498}
11499
76359541
TP
11500/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11501 Returns TRUE upon success, FALSE otherwise. */
11502
11503static bfd_boolean
11504elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11505{
11506 bfd_boolean ret = FALSE;
11507 bfd *implib_bfd;
11508 const struct elf_backend_data *bed;
11509 flagword flags;
11510 enum bfd_architecture arch;
11511 unsigned int mach;
11512 asymbol **sympp = NULL;
11513 long symsize;
11514 long symcount;
11515 long src_count;
11516 elf_symbol_type *osymbuf;
11517
11518 implib_bfd = info->out_implib_bfd;
11519 bed = get_elf_backend_data (abfd);
11520
11521 if (!bfd_set_format (implib_bfd, bfd_object))
11522 return FALSE;
11523
046734ff 11524 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11525 flags = bfd_get_file_flags (abfd);
11526 flags &= ~HAS_RELOC;
11527 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11528 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11529 return FALSE;
11530
11531 /* Copy architecture of output file to import library file. */
11532 arch = bfd_get_arch (abfd);
11533 mach = bfd_get_mach (abfd);
11534 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11535 && (abfd->target_defaulted
11536 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11537 return FALSE;
11538
11539 /* Get symbol table size. */
11540 symsize = bfd_get_symtab_upper_bound (abfd);
11541 if (symsize < 0)
11542 return FALSE;
11543
11544 /* Read in the symbol table. */
11545 sympp = (asymbol **) xmalloc (symsize);
11546 symcount = bfd_canonicalize_symtab (abfd, sympp);
11547 if (symcount < 0)
11548 goto free_sym_buf;
11549
11550 /* Allow the BFD backend to copy any private header data it
11551 understands from the output BFD to the import library BFD. */
11552 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11553 goto free_sym_buf;
11554
11555 /* Filter symbols to appear in the import library. */
11556 if (bed->elf_backend_filter_implib_symbols)
11557 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11558 symcount);
11559 else
11560 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11561 if (symcount == 0)
11562 {
5df1bc57 11563 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11564 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11565 implib_bfd);
76359541
TP
11566 goto free_sym_buf;
11567 }
11568
11569
11570 /* Make symbols absolute. */
11571 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11572 sizeof (*osymbuf));
11573 for (src_count = 0; src_count < symcount; src_count++)
11574 {
11575 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11576 sizeof (*osymbuf));
11577 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11578 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11579 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11580 osymbuf[src_count].internal_elf_sym.st_value =
11581 osymbuf[src_count].symbol.value;
11582 sympp[src_count] = &osymbuf[src_count].symbol;
11583 }
11584
11585 bfd_set_symtab (implib_bfd, sympp, symcount);
11586
11587 /* Allow the BFD backend to copy any private data it understands
11588 from the output BFD to the import library BFD. This is done last
11589 to permit the routine to look at the filtered symbol table. */
11590 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11591 goto free_sym_buf;
11592
11593 if (!bfd_close (implib_bfd))
11594 goto free_sym_buf;
11595
11596 ret = TRUE;
11597
11598free_sym_buf:
11599 free (sympp);
11600 return ret;
11601}
11602
9f7c3e5e
AM
11603static void
11604elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11605{
11606 asection *o;
11607
11608 if (flinfo->symstrtab != NULL)
ef10c3ac 11609 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11610 if (flinfo->contents != NULL)
11611 free (flinfo->contents);
11612 if (flinfo->external_relocs != NULL)
11613 free (flinfo->external_relocs);
11614 if (flinfo->internal_relocs != NULL)
11615 free (flinfo->internal_relocs);
11616 if (flinfo->external_syms != NULL)
11617 free (flinfo->external_syms);
11618 if (flinfo->locsym_shndx != NULL)
11619 free (flinfo->locsym_shndx);
11620 if (flinfo->internal_syms != NULL)
11621 free (flinfo->internal_syms);
11622 if (flinfo->indices != NULL)
11623 free (flinfo->indices);
11624 if (flinfo->sections != NULL)
11625 free (flinfo->sections);
9f7c3e5e
AM
11626 if (flinfo->symshndxbuf != NULL)
11627 free (flinfo->symshndxbuf);
11628 for (o = obfd->sections; o != NULL; o = o->next)
11629 {
11630 struct bfd_elf_section_data *esdo = elf_section_data (o);
11631 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11632 free (esdo->rel.hashes);
11633 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11634 free (esdo->rela.hashes);
11635 }
11636}
0b52efa6 11637
c152c796
AM
11638/* Do the final step of an ELF link. */
11639
11640bfd_boolean
11641bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11642{
11643 bfd_boolean dynamic;
11644 bfd_boolean emit_relocs;
11645 bfd *dynobj;
8b127cbc 11646 struct elf_final_link_info flinfo;
91d6fa6a
NC
11647 asection *o;
11648 struct bfd_link_order *p;
11649 bfd *sub;
c152c796
AM
11650 bfd_size_type max_contents_size;
11651 bfd_size_type max_external_reloc_size;
11652 bfd_size_type max_internal_reloc_count;
11653 bfd_size_type max_sym_count;
11654 bfd_size_type max_sym_shndx_count;
c152c796
AM
11655 Elf_Internal_Sym elfsym;
11656 unsigned int i;
11657 Elf_Internal_Shdr *symtab_hdr;
11658 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11659 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11660 struct elf_outext_info eoinfo;
11661 bfd_boolean merged;
11662 size_t relativecount = 0;
11663 asection *reldyn = 0;
11664 bfd_size_type amt;
104d59d1
JM
11665 asection *attr_section = NULL;
11666 bfd_vma attr_size = 0;
11667 const char *std_attrs_section;
64f52338 11668 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11669
64f52338 11670 if (!is_elf_hash_table (htab))
c152c796
AM
11671 return FALSE;
11672
0e1862bb 11673 if (bfd_link_pic (info))
c152c796
AM
11674 abfd->flags |= DYNAMIC;
11675
64f52338
AM
11676 dynamic = htab->dynamic_sections_created;
11677 dynobj = htab->dynobj;
c152c796 11678
0e1862bb 11679 emit_relocs = (bfd_link_relocatable (info)
a4676736 11680 || info->emitrelocations);
c152c796 11681
8b127cbc
AM
11682 flinfo.info = info;
11683 flinfo.output_bfd = abfd;
ef10c3ac 11684 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11685 if (flinfo.symstrtab == NULL)
c152c796
AM
11686 return FALSE;
11687
11688 if (! dynamic)
11689 {
8b127cbc
AM
11690 flinfo.hash_sec = NULL;
11691 flinfo.symver_sec = NULL;
c152c796
AM
11692 }
11693 else
11694 {
3d4d4302 11695 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11696 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11697 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11698 /* Note that it is OK if symver_sec is NULL. */
11699 }
11700
8b127cbc
AM
11701 flinfo.contents = NULL;
11702 flinfo.external_relocs = NULL;
11703 flinfo.internal_relocs = NULL;
11704 flinfo.external_syms = NULL;
11705 flinfo.locsym_shndx = NULL;
11706 flinfo.internal_syms = NULL;
11707 flinfo.indices = NULL;
11708 flinfo.sections = NULL;
8b127cbc 11709 flinfo.symshndxbuf = NULL;
ffbc01cc 11710 flinfo.filesym_count = 0;
c152c796 11711
104d59d1
JM
11712 /* The object attributes have been merged. Remove the input
11713 sections from the link, and set the contents of the output
11714 secton. */
11715 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11716 for (o = abfd->sections; o != NULL; o = o->next)
11717 {
11718 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11719 || strcmp (o->name, ".gnu.attributes") == 0)
11720 {
11721 for (p = o->map_head.link_order; p != NULL; p = p->next)
11722 {
11723 asection *input_section;
11724
11725 if (p->type != bfd_indirect_link_order)
11726 continue;
11727 input_section = p->u.indirect.section;
11728 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11729 elf_link_input_bfd ignores this section. */
11730 input_section->flags &= ~SEC_HAS_CONTENTS;
11731 }
a0c8462f 11732
104d59d1
JM
11733 attr_size = bfd_elf_obj_attr_size (abfd);
11734 if (attr_size)
11735 {
11736 bfd_set_section_size (abfd, o, attr_size);
11737 attr_section = o;
11738 /* Skip this section later on. */
11739 o->map_head.link_order = NULL;
11740 }
11741 else
11742 o->flags |= SEC_EXCLUDE;
11743 }
6e5e9d58
AM
11744 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11745 {
11746 /* Remove empty group section from linker output. */
11747 o->flags |= SEC_EXCLUDE;
11748 bfd_section_list_remove (abfd, o);
11749 abfd->section_count--;
11750 }
104d59d1
JM
11751 }
11752
c152c796
AM
11753 /* Count up the number of relocations we will output for each output
11754 section, so that we know the sizes of the reloc sections. We
11755 also figure out some maximum sizes. */
11756 max_contents_size = 0;
11757 max_external_reloc_size = 0;
11758 max_internal_reloc_count = 0;
11759 max_sym_count = 0;
11760 max_sym_shndx_count = 0;
11761 merged = FALSE;
11762 for (o = abfd->sections; o != NULL; o = o->next)
11763 {
11764 struct bfd_elf_section_data *esdo = elf_section_data (o);
11765 o->reloc_count = 0;
11766
8423293d 11767 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11768 {
11769 unsigned int reloc_count = 0;
9eaff861 11770 unsigned int additional_reloc_count = 0;
c152c796 11771 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11772
11773 if (p->type == bfd_section_reloc_link_order
11774 || p->type == bfd_symbol_reloc_link_order)
11775 reloc_count = 1;
11776 else if (p->type == bfd_indirect_link_order)
11777 {
11778 asection *sec;
11779
11780 sec = p->u.indirect.section;
c152c796
AM
11781
11782 /* Mark all sections which are to be included in the
11783 link. This will normally be every section. We need
11784 to do this so that we can identify any sections which
11785 the linker has decided to not include. */
11786 sec->linker_mark = TRUE;
11787
11788 if (sec->flags & SEC_MERGE)
11789 merged = TRUE;
11790
eea6121a
AM
11791 if (sec->rawsize > max_contents_size)
11792 max_contents_size = sec->rawsize;
11793 if (sec->size > max_contents_size)
11794 max_contents_size = sec->size;
c152c796 11795
c152c796
AM
11796 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11797 && (sec->owner->flags & DYNAMIC) == 0)
11798 {
11799 size_t sym_count;
11800
a961cdd5
AM
11801 /* We are interested in just local symbols, not all
11802 symbols. */
c152c796
AM
11803 if (elf_bad_symtab (sec->owner))
11804 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11805 / bed->s->sizeof_sym);
11806 else
11807 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11808
11809 if (sym_count > max_sym_count)
11810 max_sym_count = sym_count;
11811
11812 if (sym_count > max_sym_shndx_count
6a40cf0c 11813 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11814 max_sym_shndx_count = sym_count;
11815
a961cdd5
AM
11816 if (esdo->this_hdr.sh_type == SHT_REL
11817 || esdo->this_hdr.sh_type == SHT_RELA)
11818 /* Some backends use reloc_count in relocation sections
11819 to count particular types of relocs. Of course,
11820 reloc sections themselves can't have relocations. */
11821 ;
11822 else if (emit_relocs)
11823 {
11824 reloc_count = sec->reloc_count;
11825 if (bed->elf_backend_count_additional_relocs)
11826 {
11827 int c;
11828 c = (*bed->elf_backend_count_additional_relocs) (sec);
11829 additional_reloc_count += c;
11830 }
11831 }
11832 else if (bed->elf_backend_count_relocs)
11833 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11834
11835 esdi = elf_section_data (sec);
11836
c152c796
AM
11837 if ((sec->flags & SEC_RELOC) != 0)
11838 {
d4730f92 11839 size_t ext_size = 0;
c152c796 11840
d4730f92
BS
11841 if (esdi->rel.hdr != NULL)
11842 ext_size = esdi->rel.hdr->sh_size;
11843 if (esdi->rela.hdr != NULL)
11844 ext_size += esdi->rela.hdr->sh_size;
7326c758 11845
c152c796
AM
11846 if (ext_size > max_external_reloc_size)
11847 max_external_reloc_size = ext_size;
11848 if (sec->reloc_count > max_internal_reloc_count)
11849 max_internal_reloc_count = sec->reloc_count;
11850 }
11851 }
11852 }
11853
11854 if (reloc_count == 0)
11855 continue;
11856
9eaff861 11857 reloc_count += additional_reloc_count;
c152c796
AM
11858 o->reloc_count += reloc_count;
11859
0e1862bb 11860 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11861 {
d4730f92 11862 if (esdi->rel.hdr)
9eaff861 11863 {
491d01d3 11864 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11865 esdo->rel.count += additional_reloc_count;
11866 }
d4730f92 11867 if (esdi->rela.hdr)
9eaff861 11868 {
491d01d3 11869 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11870 esdo->rela.count += additional_reloc_count;
11871 }
d4730f92
BS
11872 }
11873 else
11874 {
11875 if (o->use_rela_p)
11876 esdo->rela.count += reloc_count;
2c2b4ed4 11877 else
d4730f92 11878 esdo->rel.count += reloc_count;
c152c796 11879 }
c152c796
AM
11880 }
11881
9eaff861 11882 if (o->reloc_count > 0)
c152c796
AM
11883 o->flags |= SEC_RELOC;
11884 else
11885 {
11886 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11887 set it (this is probably a bug) and if it is set
11888 assign_section_numbers will create a reloc section. */
11889 o->flags &=~ SEC_RELOC;
11890 }
11891
11892 /* If the SEC_ALLOC flag is not set, force the section VMA to
11893 zero. This is done in elf_fake_sections as well, but forcing
11894 the VMA to 0 here will ensure that relocs against these
11895 sections are handled correctly. */
11896 if ((o->flags & SEC_ALLOC) == 0
11897 && ! o->user_set_vma)
11898 o->vma = 0;
11899 }
11900
0e1862bb 11901 if (! bfd_link_relocatable (info) && merged)
64f52338 11902 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11903
11904 /* Figure out the file positions for everything but the symbol table
11905 and the relocs. We set symcount to force assign_section_numbers
11906 to create a symbol table. */
8539e4e8 11907 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11908 BFD_ASSERT (! abfd->output_has_begun);
11909 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11910 goto error_return;
11911
ee75fd95 11912 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11913 for (o = abfd->sections; o != NULL; o = o->next)
11914 {
d4730f92 11915 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11916 if ((o->flags & SEC_RELOC) != 0)
11917 {
d4730f92 11918 if (esdo->rel.hdr
9eaff861 11919 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11920 goto error_return;
11921
d4730f92 11922 if (esdo->rela.hdr
9eaff861 11923 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11924 goto error_return;
11925 }
11926
11927 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11928 to count upwards while actually outputting the relocations. */
d4730f92
BS
11929 esdo->rel.count = 0;
11930 esdo->rela.count = 0;
0ce398f1
L
11931
11932 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11933 {
11934 /* Cache the section contents so that they can be compressed
11935 later. Use bfd_malloc since it will be freed by
11936 bfd_compress_section_contents. */
11937 unsigned char *contents = esdo->this_hdr.contents;
11938 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11939 abort ();
11940 contents
11941 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11942 if (contents == NULL)
11943 goto error_return;
11944 esdo->this_hdr.contents = contents;
11945 }
c152c796
AM
11946 }
11947
c152c796 11948 /* We have now assigned file positions for all the sections except
a485e98e
AM
11949 .symtab, .strtab, and non-loaded reloc sections. We start the
11950 .symtab section at the current file position, and write directly
11951 to it. We build the .strtab section in memory. */
c152c796
AM
11952 bfd_get_symcount (abfd) = 0;
11953 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11954 /* sh_name is set in prep_headers. */
11955 symtab_hdr->sh_type = SHT_SYMTAB;
11956 /* sh_flags, sh_addr and sh_size all start off zero. */
11957 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11958 /* sh_link is set in assign_section_numbers. */
11959 /* sh_info is set below. */
11960 /* sh_offset is set just below. */
72de5009 11961 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11962
ef10c3ac
L
11963 if (max_sym_count < 20)
11964 max_sym_count = 20;
64f52338 11965 htab->strtabsize = max_sym_count;
ef10c3ac 11966 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11967 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11968 if (htab->strtab == NULL)
c152c796 11969 goto error_return;
ef10c3ac
L
11970 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11971 flinfo.symshndxbuf
11972 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11973 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11974
8539e4e8 11975 if (info->strip != strip_all || emit_relocs)
c152c796 11976 {
8539e4e8
AM
11977 file_ptr off = elf_next_file_pos (abfd);
11978
11979 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11980
11981 /* Note that at this point elf_next_file_pos (abfd) is
11982 incorrect. We do not yet know the size of the .symtab section.
11983 We correct next_file_pos below, after we do know the size. */
11984
11985 /* Start writing out the symbol table. The first symbol is always a
11986 dummy symbol. */
c152c796
AM
11987 elfsym.st_value = 0;
11988 elfsym.st_size = 0;
11989 elfsym.st_info = 0;
11990 elfsym.st_other = 0;
11991 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11992 elfsym.st_target_internal = 0;
ef10c3ac
L
11993 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11994 bfd_und_section_ptr, NULL) != 1)
c152c796 11995 goto error_return;
c152c796 11996
8539e4e8
AM
11997 /* Output a symbol for each section. We output these even if we are
11998 discarding local symbols, since they are used for relocs. These
11999 symbols have no names. We store the index of each one in the
12000 index field of the section, so that we can find it again when
12001 outputting relocs. */
12002
c152c796
AM
12003 elfsym.st_size = 0;
12004 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12005 elfsym.st_other = 0;
f0b5bb34 12006 elfsym.st_value = 0;
35fc36a8 12007 elfsym.st_target_internal = 0;
c152c796
AM
12008 for (i = 1; i < elf_numsections (abfd); i++)
12009 {
12010 o = bfd_section_from_elf_index (abfd, i);
12011 if (o != NULL)
f0b5bb34
AM
12012 {
12013 o->target_index = bfd_get_symcount (abfd);
12014 elfsym.st_shndx = i;
0e1862bb 12015 if (!bfd_link_relocatable (info))
f0b5bb34 12016 elfsym.st_value = o->vma;
ef10c3ac
L
12017 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12018 NULL) != 1)
f0b5bb34
AM
12019 goto error_return;
12020 }
c152c796
AM
12021 }
12022 }
12023
12024 /* Allocate some memory to hold information read in from the input
12025 files. */
12026 if (max_contents_size != 0)
12027 {
8b127cbc
AM
12028 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12029 if (flinfo.contents == NULL)
c152c796
AM
12030 goto error_return;
12031 }
12032
12033 if (max_external_reloc_size != 0)
12034 {
8b127cbc
AM
12035 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12036 if (flinfo.external_relocs == NULL)
c152c796
AM
12037 goto error_return;
12038 }
12039
12040 if (max_internal_reloc_count != 0)
12041 {
056bafd4 12042 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12043 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12044 if (flinfo.internal_relocs == NULL)
c152c796
AM
12045 goto error_return;
12046 }
12047
12048 if (max_sym_count != 0)
12049 {
12050 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12051 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12052 if (flinfo.external_syms == NULL)
c152c796
AM
12053 goto error_return;
12054
12055 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12056 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12057 if (flinfo.internal_syms == NULL)
c152c796
AM
12058 goto error_return;
12059
12060 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12061 flinfo.indices = (long int *) bfd_malloc (amt);
12062 if (flinfo.indices == NULL)
c152c796
AM
12063 goto error_return;
12064
12065 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12066 flinfo.sections = (asection **) bfd_malloc (amt);
12067 if (flinfo.sections == NULL)
c152c796
AM
12068 goto error_return;
12069 }
12070
12071 if (max_sym_shndx_count != 0)
12072 {
12073 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12074 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12075 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12076 goto error_return;
12077 }
12078
64f52338 12079 if (htab->tls_sec)
c152c796
AM
12080 {
12081 bfd_vma base, end = 0;
12082 asection *sec;
12083
64f52338 12084 for (sec = htab->tls_sec;
c152c796
AM
12085 sec && (sec->flags & SEC_THREAD_LOCAL);
12086 sec = sec->next)
12087 {
3a800eb9 12088 bfd_size_type size = sec->size;
c152c796 12089
3a800eb9
AM
12090 if (size == 0
12091 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12092 {
91d6fa6a
NC
12093 struct bfd_link_order *ord = sec->map_tail.link_order;
12094
12095 if (ord != NULL)
12096 size = ord->offset + ord->size;
c152c796
AM
12097 }
12098 end = sec->vma + size;
12099 }
64f52338 12100 base = htab->tls_sec->vma;
7dc98aea
RO
12101 /* Only align end of TLS section if static TLS doesn't have special
12102 alignment requirements. */
12103 if (bed->static_tls_alignment == 1)
64f52338
AM
12104 end = align_power (end, htab->tls_sec->alignment_power);
12105 htab->tls_size = end - base;
c152c796
AM
12106 }
12107
0b52efa6
PB
12108 /* Reorder SHF_LINK_ORDER sections. */
12109 for (o = abfd->sections; o != NULL; o = o->next)
12110 {
12111 if (!elf_fixup_link_order (abfd, o))
12112 return FALSE;
12113 }
12114
2f0c68f2
CM
12115 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12116 return FALSE;
12117
c152c796
AM
12118 /* Since ELF permits relocations to be against local symbols, we
12119 must have the local symbols available when we do the relocations.
12120 Since we would rather only read the local symbols once, and we
12121 would rather not keep them in memory, we handle all the
12122 relocations for a single input file at the same time.
12123
12124 Unfortunately, there is no way to know the total number of local
12125 symbols until we have seen all of them, and the local symbol
12126 indices precede the global symbol indices. This means that when
12127 we are generating relocatable output, and we see a reloc against
12128 a global symbol, we can not know the symbol index until we have
12129 finished examining all the local symbols to see which ones we are
12130 going to output. To deal with this, we keep the relocations in
12131 memory, and don't output them until the end of the link. This is
12132 an unfortunate waste of memory, but I don't see a good way around
12133 it. Fortunately, it only happens when performing a relocatable
12134 link, which is not the common case. FIXME: If keep_memory is set
12135 we could write the relocs out and then read them again; I don't
12136 know how bad the memory loss will be. */
12137
c72f2fb2 12138 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12139 sub->output_has_begun = FALSE;
12140 for (o = abfd->sections; o != NULL; o = o->next)
12141 {
8423293d 12142 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12143 {
12144 if (p->type == bfd_indirect_link_order
12145 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12146 == bfd_target_elf_flavour)
12147 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12148 {
12149 if (! sub->output_has_begun)
12150 {
8b127cbc 12151 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12152 goto error_return;
12153 sub->output_has_begun = TRUE;
12154 }
12155 }
12156 else if (p->type == bfd_section_reloc_link_order
12157 || p->type == bfd_symbol_reloc_link_order)
12158 {
12159 if (! elf_reloc_link_order (abfd, info, o, p))
12160 goto error_return;
12161 }
12162 else
12163 {
12164 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12165 {
12166 if (p->type == bfd_indirect_link_order
12167 && (bfd_get_flavour (sub)
12168 == bfd_target_elf_flavour)
12169 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12170 != bed->s->elfclass))
12171 {
12172 const char *iclass, *oclass;
12173
aebf9be7 12174 switch (bed->s->elfclass)
351f65ca 12175 {
aebf9be7
NC
12176 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12177 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12178 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12179 default: abort ();
351f65ca 12180 }
aebf9be7
NC
12181
12182 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12183 {
aebf9be7
NC
12184 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12185 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12186 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12187 default: abort ();
351f65ca
L
12188 }
12189
12190 bfd_set_error (bfd_error_wrong_format);
4eca0228 12191 _bfd_error_handler
695344c0 12192 /* xgettext:c-format */
871b3ab2 12193 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12194 sub, iclass, oclass);
12195 }
12196
12197 goto error_return;
12198 }
c152c796
AM
12199 }
12200 }
12201 }
12202
c0f00686
L
12203 /* Free symbol buffer if needed. */
12204 if (!info->reduce_memory_overheads)
12205 {
c72f2fb2 12206 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12207 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12208 && elf_tdata (sub)->symbuf)
c0f00686
L
12209 {
12210 free (elf_tdata (sub)->symbuf);
12211 elf_tdata (sub)->symbuf = NULL;
12212 }
12213 }
12214
c152c796
AM
12215 /* Output any global symbols that got converted to local in a
12216 version script or due to symbol visibility. We do this in a
12217 separate step since ELF requires all local symbols to appear
12218 prior to any global symbols. FIXME: We should only do this if
12219 some global symbols were, in fact, converted to become local.
12220 FIXME: Will this work correctly with the Irix 5 linker? */
12221 eoinfo.failed = FALSE;
8b127cbc 12222 eoinfo.flinfo = &flinfo;
c152c796 12223 eoinfo.localsyms = TRUE;
34a79995 12224 eoinfo.file_sym_done = FALSE;
7686d77d 12225 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12226 if (eoinfo.failed)
12227 return FALSE;
12228
4e617b1e
PB
12229 /* If backend needs to output some local symbols not present in the hash
12230 table, do it now. */
8539e4e8
AM
12231 if (bed->elf_backend_output_arch_local_syms
12232 && (info->strip != strip_all || emit_relocs))
4e617b1e 12233 {
6e0b88f1 12234 typedef int (*out_sym_func)
4e617b1e
PB
12235 (void *, const char *, Elf_Internal_Sym *, asection *,
12236 struct elf_link_hash_entry *);
12237
12238 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12239 (abfd, info, &flinfo,
12240 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12241 return FALSE;
12242 }
12243
c152c796
AM
12244 /* That wrote out all the local symbols. Finish up the symbol table
12245 with the global symbols. Even if we want to strip everything we
12246 can, we still need to deal with those global symbols that got
12247 converted to local in a version script. */
12248
12249 /* The sh_info field records the index of the first non local symbol. */
12250 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12251
12252 if (dynamic
64f52338
AM
12253 && htab->dynsym != NULL
12254 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12255 {
12256 Elf_Internal_Sym sym;
64f52338 12257 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12258
64f52338
AM
12259 o = htab->dynsym->output_section;
12260 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12261
12262 /* Write out the section symbols for the output sections. */
0e1862bb 12263 if (bfd_link_pic (info)
64f52338 12264 || htab->is_relocatable_executable)
c152c796
AM
12265 {
12266 asection *s;
12267
12268 sym.st_size = 0;
12269 sym.st_name = 0;
12270 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12271 sym.st_other = 0;
35fc36a8 12272 sym.st_target_internal = 0;
c152c796
AM
12273
12274 for (s = abfd->sections; s != NULL; s = s->next)
12275 {
12276 int indx;
12277 bfd_byte *dest;
12278 long dynindx;
12279
c152c796 12280 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12281 if (dynindx <= 0)
12282 continue;
12283 indx = elf_section_data (s)->this_idx;
c152c796
AM
12284 BFD_ASSERT (indx > 0);
12285 sym.st_shndx = indx;
c0d5a53d
L
12286 if (! check_dynsym (abfd, &sym))
12287 return FALSE;
c152c796
AM
12288 sym.st_value = s->vma;
12289 dest = dynsym + dynindx * bed->s->sizeof_sym;
12290 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12291 }
c152c796
AM
12292 }
12293
12294 /* Write out the local dynsyms. */
64f52338 12295 if (htab->dynlocal)
c152c796
AM
12296 {
12297 struct elf_link_local_dynamic_entry *e;
64f52338 12298 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12299 {
12300 asection *s;
12301 bfd_byte *dest;
12302
935bd1e0 12303 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12304 Note that we saved a word of storage and overwrote
12305 the original st_name with the dynstr_index. */
12306 sym = e->isym;
935bd1e0 12307 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12308
cb33740c
AM
12309 s = bfd_section_from_elf_index (e->input_bfd,
12310 e->isym.st_shndx);
12311 if (s != NULL)
c152c796 12312 {
c152c796
AM
12313 sym.st_shndx =
12314 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12315 if (! check_dynsym (abfd, &sym))
12316 return FALSE;
c152c796
AM
12317 sym.st_value = (s->output_section->vma
12318 + s->output_offset
12319 + e->isym.st_value);
12320 }
12321
c152c796
AM
12322 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12323 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12324 }
12325 }
c152c796
AM
12326 }
12327
12328 /* We get the global symbols from the hash table. */
12329 eoinfo.failed = FALSE;
12330 eoinfo.localsyms = FALSE;
8b127cbc 12331 eoinfo.flinfo = &flinfo;
7686d77d 12332 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12333 if (eoinfo.failed)
12334 return FALSE;
12335
12336 /* If backend needs to output some symbols not present in the hash
12337 table, do it now. */
8539e4e8
AM
12338 if (bed->elf_backend_output_arch_syms
12339 && (info->strip != strip_all || emit_relocs))
c152c796 12340 {
6e0b88f1 12341 typedef int (*out_sym_func)
c152c796
AM
12342 (void *, const char *, Elf_Internal_Sym *, asection *,
12343 struct elf_link_hash_entry *);
12344
12345 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12346 (abfd, info, &flinfo,
12347 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12348 return FALSE;
12349 }
12350
ef10c3ac
L
12351 /* Finalize the .strtab section. */
12352 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12353
12354 /* Swap out the .strtab section. */
12355 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12356 return FALSE;
12357
12358 /* Now we know the size of the symtab section. */
c152c796
AM
12359 if (bfd_get_symcount (abfd) > 0)
12360 {
ee3b52e9
L
12361 /* Finish up and write out the symbol string table (.strtab)
12362 section. */
ad32986f 12363 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12364 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12365
ad32986f 12366 if (elf_symtab_shndx_list (abfd))
8539e4e8 12367 {
ad32986f 12368 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12369
ad32986f
NC
12370 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12371 {
12372 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12373 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12374 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12375 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12376 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12377
ad32986f
NC
12378 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12379 off, TRUE);
12380
12381 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12382 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12383 return FALSE;
12384 }
8539e4e8 12385 }
ee3b52e9
L
12386
12387 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12388 /* sh_name was set in prep_headers. */
12389 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12390 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12391 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12392 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12393 symstrtab_hdr->sh_entsize = 0;
12394 symstrtab_hdr->sh_link = 0;
12395 symstrtab_hdr->sh_info = 0;
12396 /* sh_offset is set just below. */
12397 symstrtab_hdr->sh_addralign = 1;
12398
12399 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12400 off, TRUE);
12401 elf_next_file_pos (abfd) = off;
12402
c152c796 12403 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12404 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12405 return FALSE;
12406 }
12407
76359541
TP
12408 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12409 {
871b3ab2 12410 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12411 info->out_implib_bfd);
76359541
TP
12412 return FALSE;
12413 }
12414
c152c796
AM
12415 /* Adjust the relocs to have the correct symbol indices. */
12416 for (o = abfd->sections; o != NULL; o = o->next)
12417 {
d4730f92 12418 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12419 bfd_boolean sort;
10bbbc1d 12420
c152c796
AM
12421 if ((o->flags & SEC_RELOC) == 0)
12422 continue;
12423
28dbcedc 12424 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12425 if (esdo->rel.hdr != NULL
10bbbc1d 12426 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12427 return FALSE;
12428 if (esdo->rela.hdr != NULL
10bbbc1d 12429 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12430 return FALSE;
c152c796
AM
12431
12432 /* Set the reloc_count field to 0 to prevent write_relocs from
12433 trying to swap the relocs out itself. */
12434 o->reloc_count = 0;
12435 }
12436
12437 if (dynamic && info->combreloc && dynobj != NULL)
12438 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12439
12440 /* If we are linking against a dynamic object, or generating a
12441 shared library, finish up the dynamic linking information. */
12442 if (dynamic)
12443 {
12444 bfd_byte *dyncon, *dynconend;
12445
12446 /* Fix up .dynamic entries. */
3d4d4302 12447 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12448 BFD_ASSERT (o != NULL);
12449
12450 dyncon = o->contents;
eea6121a 12451 dynconend = o->contents + o->size;
c152c796
AM
12452 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12453 {
12454 Elf_Internal_Dyn dyn;
12455 const char *name;
12456 unsigned int type;
64487780
AM
12457 bfd_size_type sh_size;
12458 bfd_vma sh_addr;
c152c796
AM
12459
12460 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12461
12462 switch (dyn.d_tag)
12463 {
12464 default:
12465 continue;
12466 case DT_NULL:
12467 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12468 {
12469 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12470 {
12471 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12472 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12473 default: continue;
12474 }
12475 dyn.d_un.d_val = relativecount;
12476 relativecount = 0;
12477 break;
12478 }
12479 continue;
12480
12481 case DT_INIT:
12482 name = info->init_function;
12483 goto get_sym;
12484 case DT_FINI:
12485 name = info->fini_function;
12486 get_sym:
12487 {
12488 struct elf_link_hash_entry *h;
12489
64f52338 12490 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12491 if (h != NULL
12492 && (h->root.type == bfd_link_hash_defined
12493 || h->root.type == bfd_link_hash_defweak))
12494 {
bef26483 12495 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12496 o = h->root.u.def.section;
12497 if (o->output_section != NULL)
bef26483 12498 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12499 + o->output_offset);
12500 else
12501 {
12502 /* The symbol is imported from another shared
12503 library and does not apply to this one. */
bef26483 12504 dyn.d_un.d_ptr = 0;
c152c796
AM
12505 }
12506 break;
12507 }
12508 }
12509 continue;
12510
12511 case DT_PREINIT_ARRAYSZ:
12512 name = ".preinit_array";
4ade44b7 12513 goto get_out_size;
c152c796
AM
12514 case DT_INIT_ARRAYSZ:
12515 name = ".init_array";
4ade44b7 12516 goto get_out_size;
c152c796
AM
12517 case DT_FINI_ARRAYSZ:
12518 name = ".fini_array";
4ade44b7 12519 get_out_size:
c152c796
AM
12520 o = bfd_get_section_by_name (abfd, name);
12521 if (o == NULL)
12522 {
4eca0228 12523 _bfd_error_handler
4ade44b7 12524 (_("could not find section %s"), name);
c152c796
AM
12525 goto error_return;
12526 }
eea6121a 12527 if (o->size == 0)
4eca0228 12528 _bfd_error_handler
c152c796 12529 (_("warning: %s section has zero size"), name);
eea6121a 12530 dyn.d_un.d_val = o->size;
c152c796
AM
12531 break;
12532
12533 case DT_PREINIT_ARRAY:
12534 name = ".preinit_array";
4ade44b7 12535 goto get_out_vma;
c152c796
AM
12536 case DT_INIT_ARRAY:
12537 name = ".init_array";
4ade44b7 12538 goto get_out_vma;
c152c796
AM
12539 case DT_FINI_ARRAY:
12540 name = ".fini_array";
4ade44b7
AM
12541 get_out_vma:
12542 o = bfd_get_section_by_name (abfd, name);
12543 goto do_vma;
c152c796
AM
12544
12545 case DT_HASH:
12546 name = ".hash";
12547 goto get_vma;
fdc90cb4
JJ
12548 case DT_GNU_HASH:
12549 name = ".gnu.hash";
12550 goto get_vma;
c152c796
AM
12551 case DT_STRTAB:
12552 name = ".dynstr";
12553 goto get_vma;
12554 case DT_SYMTAB:
12555 name = ".dynsym";
12556 goto get_vma;
12557 case DT_VERDEF:
12558 name = ".gnu.version_d";
12559 goto get_vma;
12560 case DT_VERNEED:
12561 name = ".gnu.version_r";
12562 goto get_vma;
12563 case DT_VERSYM:
12564 name = ".gnu.version";
12565 get_vma:
4ade44b7
AM
12566 o = bfd_get_linker_section (dynobj, name);
12567 do_vma:
b3293efa 12568 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12569 {
4eca0228 12570 _bfd_error_handler
4ade44b7 12571 (_("could not find section %s"), name);
c152c796
AM
12572 goto error_return;
12573 }
894891db
NC
12574 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12575 {
4eca0228 12576 _bfd_error_handler
894891db
NC
12577 (_("warning: section '%s' is being made into a note"), name);
12578 bfd_set_error (bfd_error_nonrepresentable_section);
12579 goto error_return;
12580 }
4ade44b7 12581 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12582 break;
12583
12584 case DT_REL:
12585 case DT_RELA:
12586 case DT_RELSZ:
12587 case DT_RELASZ:
12588 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12589 type = SHT_REL;
12590 else
12591 type = SHT_RELA;
64487780
AM
12592 sh_size = 0;
12593 sh_addr = 0;
c152c796
AM
12594 for (i = 1; i < elf_numsections (abfd); i++)
12595 {
12596 Elf_Internal_Shdr *hdr;
12597
12598 hdr = elf_elfsections (abfd)[i];
12599 if (hdr->sh_type == type
12600 && (hdr->sh_flags & SHF_ALLOC) != 0)
12601 {
64487780
AM
12602 sh_size += hdr->sh_size;
12603 if (sh_addr == 0
12604 || sh_addr > hdr->sh_addr)
12605 sh_addr = hdr->sh_addr;
c152c796
AM
12606 }
12607 }
64487780 12608
64f52338
AM
12609 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12610 {
12611 /* Don't count procedure linkage table relocs in the
12612 overall reloc count. */
64487780
AM
12613 sh_size -= htab->srelplt->size;
12614 if (sh_size == 0)
12615 /* If the size is zero, make the address zero too.
12616 This is to avoid a glibc bug. If the backend
12617 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12618 zero, then we'll put DT_RELA at the end of
12619 DT_JMPREL. glibc will interpret the end of
12620 DT_RELA matching the end of DT_JMPREL as the
12621 case where DT_RELA includes DT_JMPREL, and for
12622 LD_BIND_NOW will decide that processing DT_RELA
12623 will process the PLT relocs too. Net result:
12624 No PLT relocs applied. */
12625 sh_addr = 0;
12626
64f52338
AM
12627 /* If .rela.plt is the first .rela section, exclude
12628 it from DT_RELA. */
64487780
AM
12629 else if (sh_addr == (htab->srelplt->output_section->vma
12630 + htab->srelplt->output_offset))
12631 sh_addr += htab->srelplt->size;
64f52338 12632 }
64487780
AM
12633
12634 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12635 dyn.d_un.d_val = sh_size;
12636 else
12637 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12638 break;
12639 }
12640 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12641 }
12642 }
12643
12644 /* If we have created any dynamic sections, then output them. */
12645 if (dynobj != NULL)
12646 {
12647 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12648 goto error_return;
12649
943284cc 12650 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12651 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12652 || info->error_textrel)
3d4d4302 12653 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12654 {
12655 bfd_byte *dyncon, *dynconend;
12656
943284cc
DJ
12657 dyncon = o->contents;
12658 dynconend = o->contents + o->size;
12659 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12660 {
12661 Elf_Internal_Dyn dyn;
12662
12663 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12664
12665 if (dyn.d_tag == DT_TEXTREL)
12666 {
c192a133
AM
12667 if (info->error_textrel)
12668 info->callbacks->einfo
9793eb77 12669 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12670 else
12671 info->callbacks->einfo
9793eb77 12672 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12673 break;
12674 }
12675 }
12676 }
12677
c152c796
AM
12678 for (o = dynobj->sections; o != NULL; o = o->next)
12679 {
12680 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12681 || o->size == 0
c152c796
AM
12682 || o->output_section == bfd_abs_section_ptr)
12683 continue;
12684 if ((o->flags & SEC_LINKER_CREATED) == 0)
12685 {
12686 /* At this point, we are only interested in sections
12687 created by _bfd_elf_link_create_dynamic_sections. */
12688 continue;
12689 }
64f52338 12690 if (htab->stab_info.stabstr == o)
3722b82f 12691 continue;
64f52338 12692 if (htab->eh_info.hdr_sec == o)
eea6121a 12693 continue;
3d4d4302 12694 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12695 {
12696 if (! bfd_set_section_contents (abfd, o->output_section,
12697 o->contents,
37b01f6a
DG
12698 (file_ptr) o->output_offset
12699 * bfd_octets_per_byte (abfd),
eea6121a 12700 o->size))
c152c796
AM
12701 goto error_return;
12702 }
12703 else
12704 {
12705 /* The contents of the .dynstr section are actually in a
12706 stringtab. */
8539e4e8
AM
12707 file_ptr off;
12708
c152c796
AM
12709 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12710 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12711 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12712 goto error_return;
12713 }
12714 }
12715 }
12716
7bdf4127 12717 if (!info->resolve_section_groups)
c152c796
AM
12718 {
12719 bfd_boolean failed = FALSE;
12720
7bdf4127 12721 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12722 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12723 if (failed)
12724 goto error_return;
12725 }
12726
12727 /* If we have optimized stabs strings, output them. */
64f52338 12728 if (htab->stab_info.stabstr != NULL)
c152c796 12729 {
64f52338 12730 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12731 goto error_return;
12732 }
12733
9f7c3e5e
AM
12734 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12735 goto error_return;
c152c796 12736
9f7c3e5e 12737 elf_final_link_free (abfd, &flinfo);
c152c796 12738
12bd6957 12739 elf_linker (abfd) = TRUE;
c152c796 12740
104d59d1
JM
12741 if (attr_section)
12742 {
a50b1753 12743 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12744 if (contents == NULL)
d0f16d5e 12745 return FALSE; /* Bail out and fail. */
104d59d1
JM
12746 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12747 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12748 free (contents);
12749 }
12750
c152c796
AM
12751 return TRUE;
12752
12753 error_return:
9f7c3e5e 12754 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12755 return FALSE;
12756}
12757\f
5241d853
RS
12758/* Initialize COOKIE for input bfd ABFD. */
12759
12760static bfd_boolean
12761init_reloc_cookie (struct elf_reloc_cookie *cookie,
12762 struct bfd_link_info *info, bfd *abfd)
12763{
12764 Elf_Internal_Shdr *symtab_hdr;
12765 const struct elf_backend_data *bed;
12766
12767 bed = get_elf_backend_data (abfd);
12768 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12769
12770 cookie->abfd = abfd;
12771 cookie->sym_hashes = elf_sym_hashes (abfd);
12772 cookie->bad_symtab = elf_bad_symtab (abfd);
12773 if (cookie->bad_symtab)
12774 {
12775 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12776 cookie->extsymoff = 0;
12777 }
12778 else
12779 {
12780 cookie->locsymcount = symtab_hdr->sh_info;
12781 cookie->extsymoff = symtab_hdr->sh_info;
12782 }
12783
12784 if (bed->s->arch_size == 32)
12785 cookie->r_sym_shift = 8;
12786 else
12787 cookie->r_sym_shift = 32;
12788
12789 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12790 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12791 {
12792 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12793 cookie->locsymcount, 0,
12794 NULL, NULL, NULL);
12795 if (cookie->locsyms == NULL)
12796 {
12797 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12798 return FALSE;
12799 }
12800 if (info->keep_memory)
12801 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12802 }
12803 return TRUE;
12804}
12805
12806/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12807
12808static void
12809fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12810{
12811 Elf_Internal_Shdr *symtab_hdr;
12812
12813 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12814 if (cookie->locsyms != NULL
12815 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12816 free (cookie->locsyms);
12817}
12818
12819/* Initialize the relocation information in COOKIE for input section SEC
12820 of input bfd ABFD. */
12821
12822static bfd_boolean
12823init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12824 struct bfd_link_info *info, bfd *abfd,
12825 asection *sec)
12826{
5241d853
RS
12827 if (sec->reloc_count == 0)
12828 {
12829 cookie->rels = NULL;
12830 cookie->relend = NULL;
12831 }
12832 else
12833 {
5241d853
RS
12834 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12835 info->keep_memory);
12836 if (cookie->rels == NULL)
12837 return FALSE;
12838 cookie->rel = cookie->rels;
056bafd4 12839 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12840 }
12841 cookie->rel = cookie->rels;
12842 return TRUE;
12843}
12844
12845/* Free the memory allocated by init_reloc_cookie_rels,
12846 if appropriate. */
12847
12848static void
12849fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12850 asection *sec)
12851{
12852 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12853 free (cookie->rels);
12854}
12855
12856/* Initialize the whole of COOKIE for input section SEC. */
12857
12858static bfd_boolean
12859init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12860 struct bfd_link_info *info,
12861 asection *sec)
12862{
12863 if (!init_reloc_cookie (cookie, info, sec->owner))
12864 goto error1;
12865 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12866 goto error2;
12867 return TRUE;
12868
12869 error2:
12870 fini_reloc_cookie (cookie, sec->owner);
12871 error1:
12872 return FALSE;
12873}
12874
12875/* Free the memory allocated by init_reloc_cookie_for_section,
12876 if appropriate. */
12877
12878static void
12879fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12880 asection *sec)
12881{
12882 fini_reloc_cookie_rels (cookie, sec);
12883 fini_reloc_cookie (cookie, sec->owner);
12884}
12885\f
c152c796
AM
12886/* Garbage collect unused sections. */
12887
07adf181
AM
12888/* Default gc_mark_hook. */
12889
12890asection *
12891_bfd_elf_gc_mark_hook (asection *sec,
12892 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12893 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12894 struct elf_link_hash_entry *h,
12895 Elf_Internal_Sym *sym)
12896{
12897 if (h != NULL)
12898 {
12899 switch (h->root.type)
12900 {
12901 case bfd_link_hash_defined:
12902 case bfd_link_hash_defweak:
12903 return h->root.u.def.section;
12904
12905 case bfd_link_hash_common:
12906 return h->root.u.c.p->section;
12907
12908 default:
12909 break;
12910 }
12911 }
12912 else
12913 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12914
12915 return NULL;
12916}
12917
9e223787 12918/* Return the debug definition section. */
b7c871ed
L
12919
12920static asection *
12921elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12922 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12923 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12924 struct elf_link_hash_entry *h,
9e223787 12925 Elf_Internal_Sym *sym)
b7c871ed 12926{
9e223787
L
12927 if (h != NULL)
12928 {
12929 /* Return the global debug definition section. */
12930 if ((h->root.type == bfd_link_hash_defined
12931 || h->root.type == bfd_link_hash_defweak)
12932 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12933 return h->root.u.def.section;
12934 }
12935 else
12936 {
12937 /* Return the local debug definition section. */
12938 asection *isec = bfd_section_from_elf_index (sec->owner,
12939 sym->st_shndx);
12940 if ((isec->flags & SEC_DEBUGGING) != 0)
12941 return isec;
12942 }
b7c871ed
L
12943
12944 return NULL;
12945}
12946
5241d853
RS
12947/* COOKIE->rel describes a relocation against section SEC, which is
12948 a section we've decided to keep. Return the section that contains
12949 the relocation symbol, or NULL if no section contains it. */
12950
12951asection *
12952_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12953 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12954 struct elf_reloc_cookie *cookie,
12955 bfd_boolean *start_stop)
5241d853
RS
12956{
12957 unsigned long r_symndx;
12958 struct elf_link_hash_entry *h;
12959
12960 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12961 if (r_symndx == STN_UNDEF)
5241d853
RS
12962 return NULL;
12963
12964 if (r_symndx >= cookie->locsymcount
12965 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12966 {
12967 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12968 if (h == NULL)
12969 {
871b3ab2 12970 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
12971 sec->owner);
12972 return NULL;
12973 }
5241d853
RS
12974 while (h->root.type == bfd_link_hash_indirect
12975 || h->root.type == bfd_link_hash_warning)
12976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12977 h->mark = 1;
4e6b54a6
AM
12978 /* If this symbol is weak and there is a non-weak definition, we
12979 keep the non-weak definition because many backends put
12980 dynamic reloc info on the non-weak definition for code
12981 handling copy relocs. */
60d67dc8
AM
12982 if (h->is_weakalias)
12983 weakdef (h)->mark = 1;
1cce69b9 12984
a6a4679f 12985 if (start_stop != NULL)
1cce69b9 12986 {
7dba9362
AM
12987 /* To work around a glibc bug, mark XXX input sections
12988 when there is a reference to __start_XXX or __stop_XXX
12989 symbols. */
cbd0eecf 12990 if (h->start_stop)
1cce69b9 12991 {
cbd0eecf 12992 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12993 *start_stop = !s->gc_mark;
12994 return s;
1cce69b9
AM
12995 }
12996 }
12997
5241d853
RS
12998 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12999 }
13000
13001 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13002 &cookie->locsyms[r_symndx]);
13003}
13004
13005/* COOKIE->rel describes a relocation against section SEC, which is
13006 a section we've decided to keep. Mark the section that contains
9d0a14d3 13007 the relocation symbol. */
5241d853
RS
13008
13009bfd_boolean
13010_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13011 asection *sec,
13012 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13013 struct elf_reloc_cookie *cookie)
5241d853
RS
13014{
13015 asection *rsec;
1cce69b9 13016 bfd_boolean start_stop = FALSE;
5241d853 13017
1cce69b9
AM
13018 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13019 while (rsec != NULL)
5241d853 13020 {
1cce69b9
AM
13021 if (!rsec->gc_mark)
13022 {
13023 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13024 || (rsec->owner->flags & DYNAMIC) != 0)
13025 rsec->gc_mark = 1;
13026 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13027 return FALSE;
13028 }
13029 if (!start_stop)
13030 break;
199af150 13031 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13032 }
13033 return TRUE;
13034}
13035
07adf181
AM
13036/* The mark phase of garbage collection. For a given section, mark
13037 it and any sections in this section's group, and all the sections
13038 which define symbols to which it refers. */
13039
ccfa59ea
AM
13040bfd_boolean
13041_bfd_elf_gc_mark (struct bfd_link_info *info,
13042 asection *sec,
6a5bb875 13043 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13044{
13045 bfd_boolean ret;
9d0a14d3 13046 asection *group_sec, *eh_frame;
c152c796
AM
13047
13048 sec->gc_mark = 1;
13049
13050 /* Mark all the sections in the group. */
13051 group_sec = elf_section_data (sec)->next_in_group;
13052 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13053 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13054 return FALSE;
13055
13056 /* Look through the section relocs. */
13057 ret = TRUE;
9d0a14d3
RS
13058 eh_frame = elf_eh_frame_section (sec->owner);
13059 if ((sec->flags & SEC_RELOC) != 0
13060 && sec->reloc_count > 0
13061 && sec != eh_frame)
c152c796 13062 {
5241d853 13063 struct elf_reloc_cookie cookie;
c152c796 13064
5241d853
RS
13065 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13066 ret = FALSE;
c152c796 13067 else
c152c796 13068 {
5241d853 13069 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13070 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13071 {
13072 ret = FALSE;
13073 break;
13074 }
13075 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13076 }
13077 }
9d0a14d3
RS
13078
13079 if (ret && eh_frame && elf_fde_list (sec))
13080 {
13081 struct elf_reloc_cookie cookie;
13082
13083 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13084 ret = FALSE;
13085 else
13086 {
13087 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13088 gc_mark_hook, &cookie))
13089 ret = FALSE;
13090 fini_reloc_cookie_for_section (&cookie, eh_frame);
13091 }
13092 }
13093
2f0c68f2
CM
13094 eh_frame = elf_section_eh_frame_entry (sec);
13095 if (ret && eh_frame && !eh_frame->gc_mark)
13096 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13097 ret = FALSE;
13098
c152c796
AM
13099 return ret;
13100}
13101
3c758495
TG
13102/* Scan and mark sections in a special or debug section group. */
13103
13104static void
13105_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13106{
13107 /* Point to first section of section group. */
13108 asection *ssec;
13109 /* Used to iterate the section group. */
13110 asection *msec;
13111
13112 bfd_boolean is_special_grp = TRUE;
13113 bfd_boolean is_debug_grp = TRUE;
13114
13115 /* First scan to see if group contains any section other than debug
13116 and special section. */
13117 ssec = msec = elf_next_in_group (grp);
13118 do
13119 {
13120 if ((msec->flags & SEC_DEBUGGING) == 0)
13121 is_debug_grp = FALSE;
13122
13123 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13124 is_special_grp = FALSE;
13125
13126 msec = elf_next_in_group (msec);
13127 }
13128 while (msec != ssec);
13129
13130 /* If this is a pure debug section group or pure special section group,
13131 keep all sections in this group. */
13132 if (is_debug_grp || is_special_grp)
13133 {
13134 do
13135 {
13136 msec->gc_mark = 1;
13137 msec = elf_next_in_group (msec);
13138 }
13139 while (msec != ssec);
13140 }
13141}
13142
7f6ab9f8
AM
13143/* Keep debug and special sections. */
13144
13145bfd_boolean
13146_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13147 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13148{
13149 bfd *ibfd;
13150
c72f2fb2 13151 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13152 {
13153 asection *isec;
13154 bfd_boolean some_kept;
b40bf0a2 13155 bfd_boolean debug_frag_seen;
b7c871ed 13156 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13157
13158 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13159 continue;
57963c05
AM
13160 isec = ibfd->sections;
13161 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13162 continue;
7f6ab9f8 13163
b40bf0a2
NC
13164 /* Ensure all linker created sections are kept,
13165 see if any other section is already marked,
13166 and note if we have any fragmented debug sections. */
b7c871ed 13167 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13168 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13169 {
13170 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13171 isec->gc_mark = 1;
eb026f09
AM
13172 else if (isec->gc_mark
13173 && (isec->flags & SEC_ALLOC) != 0
13174 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13175 some_kept = TRUE;
b40bf0a2 13176
535b785f 13177 if (!debug_frag_seen
b40bf0a2
NC
13178 && (isec->flags & SEC_DEBUGGING)
13179 && CONST_STRNEQ (isec->name, ".debug_line."))
13180 debug_frag_seen = TRUE;
7f6ab9f8
AM
13181 }
13182
eb026f09
AM
13183 /* If no non-note alloc section in this file will be kept, then
13184 we can toss out the debug and special sections. */
7f6ab9f8
AM
13185 if (!some_kept)
13186 continue;
13187
13188 /* Keep debug and special sections like .comment when they are
3c758495
TG
13189 not part of a group. Also keep section groups that contain
13190 just debug sections or special sections. */
7f6ab9f8 13191 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13192 {
13193 if ((isec->flags & SEC_GROUP) != 0)
13194 _bfd_elf_gc_mark_debug_special_section_group (isec);
13195 else if (((isec->flags & SEC_DEBUGGING) != 0
13196 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13197 && elf_next_in_group (isec) == NULL)
13198 isec->gc_mark = 1;
b7c871ed
L
13199 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13200 has_kept_debug_info = TRUE;
3c758495 13201 }
b40bf0a2 13202
b40bf0a2
NC
13203 /* Look for CODE sections which are going to be discarded,
13204 and find and discard any fragmented debug sections which
13205 are associated with that code section. */
b7c871ed
L
13206 if (debug_frag_seen)
13207 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13208 if ((isec->flags & SEC_CODE) != 0
13209 && isec->gc_mark == 0)
13210 {
13211 unsigned int ilen;
13212 asection *dsec;
b40bf0a2 13213
b7c871ed 13214 ilen = strlen (isec->name);
b40bf0a2 13215
b7c871ed 13216 /* Association is determined by the name of the debug
07d6d2b8 13217 section containing the name of the code section as
b7c871ed
L
13218 a suffix. For example .debug_line.text.foo is a
13219 debug section associated with .text.foo. */
13220 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13221 {
13222 unsigned int dlen;
b40bf0a2 13223
b7c871ed
L
13224 if (dsec->gc_mark == 0
13225 || (dsec->flags & SEC_DEBUGGING) == 0)
13226 continue;
b40bf0a2 13227
b7c871ed 13228 dlen = strlen (dsec->name);
b40bf0a2 13229
b7c871ed
L
13230 if (dlen > ilen
13231 && strncmp (dsec->name + (dlen - ilen),
13232 isec->name, ilen) == 0)
b40bf0a2 13233 dsec->gc_mark = 0;
b7c871ed 13234 }
b40bf0a2 13235 }
b7c871ed
L
13236
13237 /* Mark debug sections referenced by kept debug sections. */
13238 if (has_kept_debug_info)
13239 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13240 if (isec->gc_mark
13241 && (isec->flags & SEC_DEBUGGING) != 0)
13242 if (!_bfd_elf_gc_mark (info, isec,
13243 elf_gc_mark_debug_section))
13244 return FALSE;
7f6ab9f8
AM
13245 }
13246 return TRUE;
13247}
13248
c152c796 13249static bfd_boolean
ccabcbe5 13250elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13251{
13252 bfd *sub;
ccabcbe5 13253 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13254
c72f2fb2 13255 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13256 {
13257 asection *o;
13258
b19a8f85 13259 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13260 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13261 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13262 continue;
57963c05
AM
13263 o = sub->sections;
13264 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13265 continue;
c152c796
AM
13266
13267 for (o = sub->sections; o != NULL; o = o->next)
13268 {
a33dafc3
L
13269 /* When any section in a section group is kept, we keep all
13270 sections in the section group. If the first member of
13271 the section group is excluded, we will also exclude the
13272 group section. */
13273 if (o->flags & SEC_GROUP)
13274 {
13275 asection *first = elf_next_in_group (o);
13276 o->gc_mark = first->gc_mark;
13277 }
c152c796 13278
1e7eae0d 13279 if (o->gc_mark)
c152c796
AM
13280 continue;
13281
13282 /* Skip sweeping sections already excluded. */
13283 if (o->flags & SEC_EXCLUDE)
13284 continue;
13285
13286 /* Since this is early in the link process, it is simple
13287 to remove a section from the output. */
13288 o->flags |= SEC_EXCLUDE;
13289
c55fe096 13290 if (info->print_gc_sections && o->size != 0)
695344c0 13291 /* xgettext:c-format */
9793eb77 13292 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13293 o, sub);
c152c796
AM
13294 }
13295 }
13296
c152c796
AM
13297 return TRUE;
13298}
13299
13300/* Propagate collected vtable information. This is called through
13301 elf_link_hash_traverse. */
13302
13303static bfd_boolean
13304elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13305{
c152c796 13306 /* Those that are not vtables. */
cbd0eecf
L
13307 if (h->start_stop
13308 || h->u2.vtable == NULL
13309 || h->u2.vtable->parent == NULL)
c152c796
AM
13310 return TRUE;
13311
13312 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13313 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13314 return TRUE;
13315
13316 /* If we've already been done, exit. */
cbd0eecf 13317 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13318 return TRUE;
13319
13320 /* Make sure the parent's table is up to date. */
cbd0eecf 13321 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13322
cbd0eecf 13323 if (h->u2.vtable->used == NULL)
c152c796
AM
13324 {
13325 /* None of this table's entries were referenced. Re-use the
13326 parent's table. */
cbd0eecf
L
13327 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13328 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13329 }
13330 else
13331 {
13332 size_t n;
13333 bfd_boolean *cu, *pu;
13334
13335 /* Or the parent's entries into ours. */
cbd0eecf 13336 cu = h->u2.vtable->used;
c152c796 13337 cu[-1] = TRUE;
cbd0eecf 13338 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13339 if (pu != NULL)
13340 {
13341 const struct elf_backend_data *bed;
13342 unsigned int log_file_align;
13343
13344 bed = get_elf_backend_data (h->root.u.def.section->owner);
13345 log_file_align = bed->s->log_file_align;
cbd0eecf 13346 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13347 while (n--)
13348 {
13349 if (*pu)
13350 *cu = TRUE;
13351 pu++;
13352 cu++;
13353 }
13354 }
13355 }
13356
13357 return TRUE;
13358}
13359
13360static bfd_boolean
13361elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13362{
13363 asection *sec;
13364 bfd_vma hstart, hend;
13365 Elf_Internal_Rela *relstart, *relend, *rel;
13366 const struct elf_backend_data *bed;
13367 unsigned int log_file_align;
13368
c152c796
AM
13369 /* Take care of both those symbols that do not describe vtables as
13370 well as those that are not loaded. */
cbd0eecf
L
13371 if (h->start_stop
13372 || h->u2.vtable == NULL
13373 || h->u2.vtable->parent == NULL)
c152c796
AM
13374 return TRUE;
13375
13376 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13377 || h->root.type == bfd_link_hash_defweak);
13378
13379 sec = h->root.u.def.section;
13380 hstart = h->root.u.def.value;
13381 hend = hstart + h->size;
13382
13383 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13384 if (!relstart)
13385 return *(bfd_boolean *) okp = FALSE;
13386 bed = get_elf_backend_data (sec->owner);
13387 log_file_align = bed->s->log_file_align;
13388
056bafd4 13389 relend = relstart + sec->reloc_count;
c152c796
AM
13390
13391 for (rel = relstart; rel < relend; ++rel)
13392 if (rel->r_offset >= hstart && rel->r_offset < hend)
13393 {
13394 /* If the entry is in use, do nothing. */
cbd0eecf
L
13395 if (h->u2.vtable->used
13396 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13397 {
13398 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13399 if (h->u2.vtable->used[entry])
c152c796
AM
13400 continue;
13401 }
13402 /* Otherwise, kill it. */
13403 rel->r_offset = rel->r_info = rel->r_addend = 0;
13404 }
13405
13406 return TRUE;
13407}
13408
87538722
AM
13409/* Mark sections containing dynamically referenced symbols. When
13410 building shared libraries, we must assume that any visible symbol is
13411 referenced. */
715df9b8 13412
64d03ab5
AM
13413bfd_boolean
13414bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13415{
87538722 13416 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13417 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13418
715df9b8
EB
13419 if ((h->root.type == bfd_link_hash_defined
13420 || h->root.type == bfd_link_hash_defweak)
d664fd41 13421 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13422 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13423 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13424 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13425 && (!bfd_link_executable (info)
22185505 13426 || info->gc_keep_exported
b407645f
AM
13427 || info->export_dynamic
13428 || (h->dynamic
13429 && d != NULL
13430 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13431 && (h->versioned >= versioned
54e8959c
L
13432 || !bfd_hide_sym_by_version (info->version_info,
13433 h->root.root.string)))))
715df9b8
EB
13434 h->root.u.def.section->flags |= SEC_KEEP;
13435
13436 return TRUE;
13437}
3b36f7e6 13438
74f0fb50
AM
13439/* Keep all sections containing symbols undefined on the command-line,
13440 and the section containing the entry symbol. */
13441
13442void
13443_bfd_elf_gc_keep (struct bfd_link_info *info)
13444{
13445 struct bfd_sym_chain *sym;
13446
13447 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13448 {
13449 struct elf_link_hash_entry *h;
13450
13451 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13452 FALSE, FALSE, FALSE);
13453
13454 if (h != NULL
13455 && (h->root.type == bfd_link_hash_defined
13456 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13457 && !bfd_is_abs_section (h->root.u.def.section)
13458 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13459 h->root.u.def.section->flags |= SEC_KEEP;
13460 }
13461}
13462
2f0c68f2
CM
13463bfd_boolean
13464bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13465 struct bfd_link_info *info)
13466{
13467 bfd *ibfd = info->input_bfds;
13468
13469 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13470 {
13471 asection *sec;
13472 struct elf_reloc_cookie cookie;
13473
13474 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13475 continue;
57963c05
AM
13476 sec = ibfd->sections;
13477 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13478 continue;
2f0c68f2
CM
13479
13480 if (!init_reloc_cookie (&cookie, info, ibfd))
13481 return FALSE;
13482
13483 for (sec = ibfd->sections; sec; sec = sec->next)
13484 {
13485 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13486 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13487 {
13488 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13489 fini_reloc_cookie_rels (&cookie, sec);
13490 }
13491 }
13492 }
13493 return TRUE;
13494}
13495
c152c796
AM
13496/* Do mark and sweep of unused sections. */
13497
13498bfd_boolean
13499bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13500{
13501 bfd_boolean ok = TRUE;
13502 bfd *sub;
6a5bb875 13503 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13504 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13505 struct elf_link_hash_table *htab;
c152c796 13506
64d03ab5 13507 if (!bed->can_gc_sections
715df9b8 13508 || !is_elf_hash_table (info->hash))
c152c796 13509 {
9793eb77 13510 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13511 return TRUE;
13512 }
13513
74f0fb50 13514 bed->gc_keep (info);
da44f4e5 13515 htab = elf_hash_table (info);
74f0fb50 13516
9d0a14d3
RS
13517 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13518 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13519 for (sub = info->input_bfds;
13520 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13521 sub = sub->link.next)
9d0a14d3
RS
13522 {
13523 asection *sec;
13524 struct elf_reloc_cookie cookie;
13525
57963c05
AM
13526 sec = sub->sections;
13527 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13528 continue;
9d0a14d3 13529 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13530 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13531 {
13532 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13533 if (elf_section_data (sec)->sec_info
13534 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13535 elf_eh_frame_section (sub) = sec;
13536 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13537 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13538 }
13539 }
9d0a14d3 13540
c152c796 13541 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13542 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13543 if (!ok)
13544 return FALSE;
13545
13546 /* Kill the vtable relocations that were not used. */
da44f4e5 13547 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13548 if (!ok)
13549 return FALSE;
13550
715df9b8 13551 /* Mark dynamically referenced symbols. */
22185505 13552 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13553 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13554
715df9b8 13555 /* Grovel through relocs to find out who stays ... */
64d03ab5 13556 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13557 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13558 {
13559 asection *o;
13560
b19a8f85 13561 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13562 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13563 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13564 continue;
13565
57963c05
AM
13566 o = sub->sections;
13567 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13568 continue;
13569
7f6ab9f8
AM
13570 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13571 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13572 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13573 well as FINI_ARRAY sections for ld -r. */
c152c796 13574 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13575 if (!o->gc_mark
13576 && (o->flags & SEC_EXCLUDE) == 0
24007750 13577 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13578 || (bfd_link_relocatable (info)
13579 && ((elf_section_data (o)->this_hdr.sh_type
13580 == SHT_PREINIT_ARRAY)
13581 || (elf_section_data (o)->this_hdr.sh_type
13582 == SHT_INIT_ARRAY)
13583 || (elf_section_data (o)->this_hdr.sh_type
13584 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13585 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13586 && elf_next_in_group (o) == NULL )))
13587 {
13588 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13589 return FALSE;
13590 }
c152c796
AM
13591 }
13592
6a5bb875 13593 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13594 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13595
c152c796 13596 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13597 return elf_gc_sweep (abfd, info);
c152c796
AM
13598}
13599\f
13600/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13601
13602bfd_boolean
13603bfd_elf_gc_record_vtinherit (bfd *abfd,
13604 asection *sec,
13605 struct elf_link_hash_entry *h,
13606 bfd_vma offset)
13607{
13608 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13609 struct elf_link_hash_entry **search, *child;
ef53be89 13610 size_t extsymcount;
c152c796
AM
13611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13612
13613 /* The sh_info field of the symtab header tells us where the
13614 external symbols start. We don't care about the local symbols at
13615 this point. */
13616 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13617 if (!elf_bad_symtab (abfd))
13618 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13619
13620 sym_hashes = elf_sym_hashes (abfd);
13621 sym_hashes_end = sym_hashes + extsymcount;
13622
13623 /* Hunt down the child symbol, which is in this section at the same
13624 offset as the relocation. */
13625 for (search = sym_hashes; search != sym_hashes_end; ++search)
13626 {
13627 if ((child = *search) != NULL
13628 && (child->root.type == bfd_link_hash_defined
13629 || child->root.type == bfd_link_hash_defweak)
13630 && child->root.u.def.section == sec
13631 && child->root.u.def.value == offset)
13632 goto win;
13633 }
13634
695344c0 13635 /* xgettext:c-format */
9793eb77 13636 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13637 abfd, sec, (uint64_t) offset);
c152c796
AM
13638 bfd_set_error (bfd_error_invalid_operation);
13639 return FALSE;
13640
13641 win:
cbd0eecf 13642 if (!child->u2.vtable)
f6e332e6 13643 {
cbd0eecf
L
13644 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13645 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13646 if (!child->u2.vtable)
f6e332e6
AM
13647 return FALSE;
13648 }
c152c796
AM
13649 if (!h)
13650 {
13651 /* This *should* only be the absolute section. It could potentially
13652 be that someone has defined a non-global vtable though, which
13653 would be bad. It isn't worth paging in the local symbols to be
13654 sure though; that case should simply be handled by the assembler. */
13655
cbd0eecf 13656 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13657 }
13658 else
cbd0eecf 13659 child->u2.vtable->parent = h;
c152c796
AM
13660
13661 return TRUE;
13662}
13663
13664/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13665
13666bfd_boolean
13667bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13668 asection *sec ATTRIBUTE_UNUSED,
13669 struct elf_link_hash_entry *h,
13670 bfd_vma addend)
13671{
13672 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13673 unsigned int log_file_align = bed->s->log_file_align;
13674
cbd0eecf 13675 if (!h->u2.vtable)
f6e332e6 13676 {
cbd0eecf
L
13677 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13678 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13679 if (!h->u2.vtable)
f6e332e6
AM
13680 return FALSE;
13681 }
13682
cbd0eecf 13683 if (addend >= h->u2.vtable->size)
c152c796
AM
13684 {
13685 size_t size, bytes, file_align;
cbd0eecf 13686 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13687
13688 /* While the symbol is undefined, we have to be prepared to handle
13689 a zero size. */
13690 file_align = 1 << log_file_align;
13691 if (h->root.type == bfd_link_hash_undefined)
13692 size = addend + file_align;
13693 else
13694 {
13695 size = h->size;
13696 if (addend >= size)
13697 {
13698 /* Oops! We've got a reference past the defined end of
13699 the table. This is probably a bug -- shall we warn? */
13700 size = addend + file_align;
13701 }
13702 }
13703 size = (size + file_align - 1) & -file_align;
13704
13705 /* Allocate one extra entry for use as a "done" flag for the
13706 consolidation pass. */
13707 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13708
13709 if (ptr)
13710 {
a50b1753 13711 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13712
13713 if (ptr != NULL)
13714 {
13715 size_t oldbytes;
13716
cbd0eecf 13717 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13718 * sizeof (bfd_boolean));
13719 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13720 }
13721 }
13722 else
a50b1753 13723 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13724
13725 if (ptr == NULL)
13726 return FALSE;
13727
13728 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13729 h->u2.vtable->used = ptr + 1;
13730 h->u2.vtable->size = size;
c152c796
AM
13731 }
13732
cbd0eecf 13733 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13734
13735 return TRUE;
13736}
13737
ae17ab41
CM
13738/* Map an ELF section header flag to its corresponding string. */
13739typedef struct
13740{
13741 char *flag_name;
13742 flagword flag_value;
13743} elf_flags_to_name_table;
13744
13745static elf_flags_to_name_table elf_flags_to_names [] =
13746{
13747 { "SHF_WRITE", SHF_WRITE },
13748 { "SHF_ALLOC", SHF_ALLOC },
13749 { "SHF_EXECINSTR", SHF_EXECINSTR },
13750 { "SHF_MERGE", SHF_MERGE },
13751 { "SHF_STRINGS", SHF_STRINGS },
13752 { "SHF_INFO_LINK", SHF_INFO_LINK},
13753 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13754 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13755 { "SHF_GROUP", SHF_GROUP },
13756 { "SHF_TLS", SHF_TLS },
13757 { "SHF_MASKOS", SHF_MASKOS },
13758 { "SHF_EXCLUDE", SHF_EXCLUDE },
13759};
13760
b9c361e0
JL
13761/* Returns TRUE if the section is to be included, otherwise FALSE. */
13762bfd_boolean
ae17ab41 13763bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13764 struct flag_info *flaginfo,
b9c361e0 13765 asection *section)
ae17ab41 13766{
8b127cbc 13767 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13768
8b127cbc 13769 if (!flaginfo->flags_initialized)
ae17ab41 13770 {
8b127cbc
AM
13771 bfd *obfd = info->output_bfd;
13772 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13773 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13774 int with_hex = 0;
13775 int without_hex = 0;
13776
8b127cbc 13777 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13778 {
b9c361e0 13779 unsigned i;
8b127cbc 13780 flagword (*lookup) (char *);
ae17ab41 13781
8b127cbc
AM
13782 lookup = bed->elf_backend_lookup_section_flags_hook;
13783 if (lookup != NULL)
ae17ab41 13784 {
8b127cbc 13785 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13786
13787 if (hexval != 0)
13788 {
13789 if (tf->with == with_flags)
13790 with_hex |= hexval;
13791 else if (tf->with == without_flags)
13792 without_hex |= hexval;
13793 tf->valid = TRUE;
13794 continue;
13795 }
ae17ab41 13796 }
8b127cbc 13797 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13798 {
8b127cbc 13799 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13800 {
13801 if (tf->with == with_flags)
13802 with_hex |= elf_flags_to_names[i].flag_value;
13803 else if (tf->with == without_flags)
13804 without_hex |= elf_flags_to_names[i].flag_value;
13805 tf->valid = TRUE;
13806 break;
13807 }
13808 }
8b127cbc 13809 if (!tf->valid)
b9c361e0 13810 {
68ffbac6 13811 info->callbacks->einfo
9793eb77 13812 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13813 return FALSE;
ae17ab41
CM
13814 }
13815 }
8b127cbc
AM
13816 flaginfo->flags_initialized = TRUE;
13817 flaginfo->only_with_flags |= with_hex;
13818 flaginfo->not_with_flags |= without_hex;
ae17ab41 13819 }
ae17ab41 13820
8b127cbc 13821 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13822 return FALSE;
13823
8b127cbc 13824 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13825 return FALSE;
13826
13827 return TRUE;
ae17ab41
CM
13828}
13829
c152c796
AM
13830struct alloc_got_off_arg {
13831 bfd_vma gotoff;
10455f89 13832 struct bfd_link_info *info;
c152c796
AM
13833};
13834
13835/* We need a special top-level link routine to convert got reference counts
13836 to real got offsets. */
13837
13838static bfd_boolean
13839elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13840{
a50b1753 13841 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13842 bfd *obfd = gofarg->info->output_bfd;
13843 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13844
c152c796
AM
13845 if (h->got.refcount > 0)
13846 {
13847 h->got.offset = gofarg->gotoff;
10455f89 13848 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13849 }
13850 else
13851 h->got.offset = (bfd_vma) -1;
13852
13853 return TRUE;
13854}
13855
13856/* And an accompanying bit to work out final got entry offsets once
13857 we're done. Should be called from final_link. */
13858
13859bfd_boolean
13860bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13861 struct bfd_link_info *info)
13862{
13863 bfd *i;
13864 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13865 bfd_vma gotoff;
c152c796
AM
13866 struct alloc_got_off_arg gofarg;
13867
10455f89
HPN
13868 BFD_ASSERT (abfd == info->output_bfd);
13869
c152c796
AM
13870 if (! is_elf_hash_table (info->hash))
13871 return FALSE;
13872
13873 /* The GOT offset is relative to the .got section, but the GOT header is
13874 put into the .got.plt section, if the backend uses it. */
13875 if (bed->want_got_plt)
13876 gotoff = 0;
13877 else
13878 gotoff = bed->got_header_size;
13879
13880 /* Do the local .got entries first. */
c72f2fb2 13881 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13882 {
13883 bfd_signed_vma *local_got;
ef53be89 13884 size_t j, locsymcount;
c152c796
AM
13885 Elf_Internal_Shdr *symtab_hdr;
13886
13887 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13888 continue;
13889
13890 local_got = elf_local_got_refcounts (i);
13891 if (!local_got)
13892 continue;
13893
13894 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13895 if (elf_bad_symtab (i))
13896 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13897 else
13898 locsymcount = symtab_hdr->sh_info;
13899
13900 for (j = 0; j < locsymcount; ++j)
13901 {
13902 if (local_got[j] > 0)
13903 {
13904 local_got[j] = gotoff;
10455f89 13905 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13906 }
13907 else
13908 local_got[j] = (bfd_vma) -1;
13909 }
13910 }
13911
13912 /* Then the global .got entries. .plt refcounts are handled by
13913 adjust_dynamic_symbol */
13914 gofarg.gotoff = gotoff;
10455f89 13915 gofarg.info = info;
c152c796
AM
13916 elf_link_hash_traverse (elf_hash_table (info),
13917 elf_gc_allocate_got_offsets,
13918 &gofarg);
13919 return TRUE;
13920}
13921
13922/* Many folk need no more in the way of final link than this, once
13923 got entry reference counting is enabled. */
13924
13925bfd_boolean
13926bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13927{
13928 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13929 return FALSE;
13930
13931 /* Invoke the regular ELF backend linker to do all the work. */
13932 return bfd_elf_final_link (abfd, info);
13933}
13934
13935bfd_boolean
13936bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13937{
a50b1753 13938 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13939
13940 if (rcookie->bad_symtab)
13941 rcookie->rel = rcookie->rels;
13942
13943 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13944 {
13945 unsigned long r_symndx;
13946
13947 if (! rcookie->bad_symtab)
13948 if (rcookie->rel->r_offset > offset)
13949 return FALSE;
13950 if (rcookie->rel->r_offset != offset)
13951 continue;
13952
13953 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13954 if (r_symndx == STN_UNDEF)
c152c796
AM
13955 return TRUE;
13956
13957 if (r_symndx >= rcookie->locsymcount
13958 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13959 {
13960 struct elf_link_hash_entry *h;
13961
13962 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13963
13964 while (h->root.type == bfd_link_hash_indirect
13965 || h->root.type == bfd_link_hash_warning)
13966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13967
13968 if ((h->root.type == bfd_link_hash_defined
13969 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13970 && (h->root.u.def.section->owner != rcookie->abfd
13971 || h->root.u.def.section->kept_section != NULL
13972 || discarded_section (h->root.u.def.section)))
c152c796 13973 return TRUE;
c152c796
AM
13974 }
13975 else
13976 {
13977 /* It's not a relocation against a global symbol,
13978 but it could be a relocation against a local
13979 symbol for a discarded section. */
13980 asection *isec;
13981 Elf_Internal_Sym *isym;
13982
13983 /* Need to: get the symbol; get the section. */
13984 isym = &rcookie->locsyms[r_symndx];
cb33740c 13985 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13986 if (isec != NULL
13987 && (isec->kept_section != NULL
13988 || discarded_section (isec)))
cb33740c 13989 return TRUE;
c152c796
AM
13990 }
13991 return FALSE;
13992 }
13993 return FALSE;
13994}
13995
13996/* Discard unneeded references to discarded sections.
75938853
AM
13997 Returns -1 on error, 1 if any section's size was changed, 0 if
13998 nothing changed. This function assumes that the relocations are in
13999 sorted order, which is true for all known assemblers. */
c152c796 14000
75938853 14001int
c152c796
AM
14002bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14003{
14004 struct elf_reloc_cookie cookie;
18cd5bce 14005 asection *o;
c152c796 14006 bfd *abfd;
75938853 14007 int changed = 0;
c152c796
AM
14008
14009 if (info->traditional_format
14010 || !is_elf_hash_table (info->hash))
75938853 14011 return 0;
c152c796 14012
18cd5bce
AM
14013 o = bfd_get_section_by_name (output_bfd, ".stab");
14014 if (o != NULL)
c152c796 14015 {
18cd5bce 14016 asection *i;
c152c796 14017
18cd5bce 14018 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14019 {
18cd5bce
AM
14020 if (i->size == 0
14021 || i->reloc_count == 0
14022 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14023 continue;
c152c796 14024
18cd5bce
AM
14025 abfd = i->owner;
14026 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14027 continue;
c152c796 14028
18cd5bce 14029 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14030 return -1;
c152c796 14031
18cd5bce
AM
14032 if (_bfd_discard_section_stabs (abfd, i,
14033 elf_section_data (i)->sec_info,
5241d853
RS
14034 bfd_elf_reloc_symbol_deleted_p,
14035 &cookie))
75938853 14036 changed = 1;
18cd5bce
AM
14037
14038 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14039 }
18cd5bce
AM
14040 }
14041
2f0c68f2
CM
14042 o = NULL;
14043 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14044 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14045 if (o != NULL)
14046 {
14047 asection *i;
d7153c4a 14048 int eh_changed = 0;
79a94a2a 14049 unsigned int eh_alignment;
c152c796 14050
18cd5bce 14051 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14052 {
18cd5bce
AM
14053 if (i->size == 0)
14054 continue;
14055
14056 abfd = i->owner;
14057 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14058 continue;
14059
14060 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14061 return -1;
18cd5bce
AM
14062
14063 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14064 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14065 bfd_elf_reloc_symbol_deleted_p,
14066 &cookie))
d7153c4a
AM
14067 {
14068 eh_changed = 1;
14069 if (i->size != i->rawsize)
14070 changed = 1;
14071 }
18cd5bce
AM
14072
14073 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14074 }
9866ffe2 14075
79a94a2a 14076 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14077 /* Skip over zero terminator, and prevent empty sections from
14078 adding alignment padding at the end. */
14079 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14080 if (i->size == 0)
14081 i->flags |= SEC_EXCLUDE;
14082 else if (i->size > 4)
14083 break;
14084 /* The last non-empty eh_frame section doesn't need padding. */
14085 if (i != NULL)
14086 i = i->map_tail.s;
14087 /* Any prior sections must pad the last FDE out to the output
14088 section alignment. Otherwise we might have zero padding
14089 between sections, which would be seen as a terminator. */
14090 for (; i != NULL; i = i->map_tail.s)
14091 if (i->size == 4)
14092 /* All but the last zero terminator should have been removed. */
14093 BFD_FAIL ();
14094 else
14095 {
14096 bfd_size_type size
14097 = (i->size + eh_alignment - 1) & -eh_alignment;
14098 if (i->size != size)
af471f82 14099 {
9866ffe2
AM
14100 i->size = size;
14101 changed = 1;
14102 eh_changed = 1;
af471f82 14103 }
9866ffe2 14104 }
d7153c4a
AM
14105 if (eh_changed)
14106 elf_link_hash_traverse (elf_hash_table (info),
14107 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14108 }
c152c796 14109
18cd5bce
AM
14110 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14111 {
14112 const struct elf_backend_data *bed;
57963c05 14113 asection *s;
c152c796 14114
18cd5bce
AM
14115 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14116 continue;
57963c05
AM
14117 s = abfd->sections;
14118 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14119 continue;
18cd5bce
AM
14120
14121 bed = get_elf_backend_data (abfd);
14122
14123 if (bed->elf_backend_discard_info != NULL)
14124 {
14125 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14126 return -1;
18cd5bce
AM
14127
14128 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14129 changed = 1;
18cd5bce
AM
14130
14131 fini_reloc_cookie (&cookie, abfd);
14132 }
c152c796
AM
14133 }
14134
2f0c68f2
CM
14135 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14136 _bfd_elf_end_eh_frame_parsing (info);
14137
14138 if (info->eh_frame_hdr_type
0e1862bb 14139 && !bfd_link_relocatable (info)
c152c796 14140 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14141 changed = 1;
c152c796 14142
75938853 14143 return changed;
c152c796 14144}
082b7297 14145
43e1669b 14146bfd_boolean
0c511000 14147_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14148 asection *sec,
c0f00686 14149 struct bfd_link_info *info)
082b7297
L
14150{
14151 flagword flags;
c77ec726 14152 const char *name, *key;
082b7297
L
14153 struct bfd_section_already_linked *l;
14154 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14155
c77ec726
AM
14156 if (sec->output_section == bfd_abs_section_ptr)
14157 return FALSE;
0c511000 14158
c77ec726 14159 flags = sec->flags;
0c511000 14160
c77ec726
AM
14161 /* Return if it isn't a linkonce section. A comdat group section
14162 also has SEC_LINK_ONCE set. */
14163 if ((flags & SEC_LINK_ONCE) == 0)
14164 return FALSE;
0c511000 14165
c77ec726
AM
14166 /* Don't put group member sections on our list of already linked
14167 sections. They are handled as a group via their group section. */
14168 if (elf_sec_group (sec) != NULL)
14169 return FALSE;
0c511000 14170
c77ec726
AM
14171 /* For a SHT_GROUP section, use the group signature as the key. */
14172 name = sec->name;
14173 if ((flags & SEC_GROUP) != 0
14174 && elf_next_in_group (sec) != NULL
14175 && elf_group_name (elf_next_in_group (sec)) != NULL)
14176 key = elf_group_name (elf_next_in_group (sec));
14177 else
14178 {
14179 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14180 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14181 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14182 key++;
0c511000 14183 else
c77ec726
AM
14184 /* Must be a user linkonce section that doesn't follow gcc's
14185 naming convention. In this case we won't be matching
14186 single member groups. */
14187 key = name;
0c511000 14188 }
6d2cd210 14189
c77ec726 14190 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14191
14192 for (l = already_linked_list->entry; l != NULL; l = l->next)
14193 {
c2370991 14194 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14195 sections with a signature of <key> (<key> is some string),
14196 and linkonce sections named .gnu.linkonce.<type>.<key>.
14197 Match like sections. LTO plugin sections are an exception.
14198 They are always named .gnu.linkonce.t.<key> and match either
14199 type of section. */
14200 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14201 && ((flags & SEC_GROUP) != 0
14202 || strcmp (name, l->sec->name) == 0))
14203 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14204 {
14205 /* The section has already been linked. See if we should
6d2cd210 14206 issue a warning. */
c77ec726
AM
14207 if (!_bfd_handle_already_linked (sec, l, info))
14208 return FALSE;
082b7297 14209
c77ec726 14210 if (flags & SEC_GROUP)
3d7f7666 14211 {
c77ec726
AM
14212 asection *first = elf_next_in_group (sec);
14213 asection *s = first;
3d7f7666 14214
c77ec726 14215 while (s != NULL)
3d7f7666 14216 {
c77ec726
AM
14217 s->output_section = bfd_abs_section_ptr;
14218 /* Record which group discards it. */
14219 s->kept_section = l->sec;
14220 s = elf_next_in_group (s);
14221 /* These lists are circular. */
14222 if (s == first)
14223 break;
3d7f7666
L
14224 }
14225 }
082b7297 14226
43e1669b 14227 return TRUE;
082b7297
L
14228 }
14229 }
14230
c77ec726
AM
14231 /* A single member comdat group section may be discarded by a
14232 linkonce section and vice versa. */
14233 if ((flags & SEC_GROUP) != 0)
3d7f7666 14234 {
c77ec726 14235 asection *first = elf_next_in_group (sec);
c2370991 14236
c77ec726
AM
14237 if (first != NULL && elf_next_in_group (first) == first)
14238 /* Check this single member group against linkonce sections. */
14239 for (l = already_linked_list->entry; l != NULL; l = l->next)
14240 if ((l->sec->flags & SEC_GROUP) == 0
14241 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14242 {
14243 first->output_section = bfd_abs_section_ptr;
14244 first->kept_section = l->sec;
14245 sec->output_section = bfd_abs_section_ptr;
14246 break;
14247 }
14248 }
14249 else
14250 /* Check this linkonce section against single member groups. */
14251 for (l = already_linked_list->entry; l != NULL; l = l->next)
14252 if (l->sec->flags & SEC_GROUP)
6d2cd210 14253 {
c77ec726 14254 asection *first = elf_next_in_group (l->sec);
6d2cd210 14255
c77ec726
AM
14256 if (first != NULL
14257 && elf_next_in_group (first) == first
14258 && bfd_elf_match_symbols_in_sections (first, sec, info))
14259 {
14260 sec->output_section = bfd_abs_section_ptr;
14261 sec->kept_section = first;
14262 break;
14263 }
6d2cd210 14264 }
0c511000 14265
c77ec726
AM
14266 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14267 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14268 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14269 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14270 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14271 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14272 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14273 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14274 The reverse order cannot happen as there is never a bfd with only the
14275 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14276 matter as here were are looking only for cross-bfd sections. */
14277
14278 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14279 for (l = already_linked_list->entry; l != NULL; l = l->next)
14280 if ((l->sec->flags & SEC_GROUP) == 0
14281 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14282 {
14283 if (abfd != l->sec->owner)
14284 sec->output_section = bfd_abs_section_ptr;
14285 break;
14286 }
80c29487 14287
082b7297 14288 /* This is the first section with this name. Record it. */
c77ec726 14289 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14290 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14291 return sec->output_section == bfd_abs_section_ptr;
082b7297 14292}
81e1b023 14293
a4d8e49b
L
14294bfd_boolean
14295_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14296{
14297 return sym->st_shndx == SHN_COMMON;
14298}
14299
14300unsigned int
14301_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14302{
14303 return SHN_COMMON;
14304}
14305
14306asection *
14307_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14308{
14309 return bfd_com_section_ptr;
14310}
10455f89
HPN
14311
14312bfd_vma
14313_bfd_elf_default_got_elt_size (bfd *abfd,
14314 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14315 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14316 bfd *ibfd ATTRIBUTE_UNUSED,
14317 unsigned long symndx ATTRIBUTE_UNUSED)
14318{
14319 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14320 return bed->s->arch_size / 8;
14321}
83bac4b0
NC
14322
14323/* Routines to support the creation of dynamic relocs. */
14324
83bac4b0
NC
14325/* Returns the name of the dynamic reloc section associated with SEC. */
14326
14327static const char *
14328get_dynamic_reloc_section_name (bfd * abfd,
14329 asection * sec,
14330 bfd_boolean is_rela)
14331{
ddcf1fcf
BS
14332 char *name;
14333 const char *old_name = bfd_get_section_name (NULL, sec);
14334 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14335
ddcf1fcf 14336 if (old_name == NULL)
83bac4b0
NC
14337 return NULL;
14338
ddcf1fcf 14339 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14340 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14341
14342 return name;
14343}
14344
14345/* Returns the dynamic reloc section associated with SEC.
14346 If necessary compute the name of the dynamic reloc section based
14347 on SEC's name (looked up in ABFD's string table) and the setting
14348 of IS_RELA. */
14349
14350asection *
14351_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14352 asection * sec,
14353 bfd_boolean is_rela)
14354{
14355 asection * reloc_sec = elf_section_data (sec)->sreloc;
14356
14357 if (reloc_sec == NULL)
14358 {
14359 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14360
14361 if (name != NULL)
14362 {
3d4d4302 14363 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14364
14365 if (reloc_sec != NULL)
14366 elf_section_data (sec)->sreloc = reloc_sec;
14367 }
14368 }
14369
14370 return reloc_sec;
14371}
14372
14373/* Returns the dynamic reloc section associated with SEC. If the
14374 section does not exist it is created and attached to the DYNOBJ
14375 bfd and stored in the SRELOC field of SEC's elf_section_data
14376 structure.
f8076f98 14377
83bac4b0
NC
14378 ALIGNMENT is the alignment for the newly created section and
14379 IS_RELA defines whether the name should be .rela.<SEC's name>
14380 or .rel.<SEC's name>. The section name is looked up in the
14381 string table associated with ABFD. */
14382
14383asection *
ca4be51c
AM
14384_bfd_elf_make_dynamic_reloc_section (asection *sec,
14385 bfd *dynobj,
14386 unsigned int alignment,
14387 bfd *abfd,
14388 bfd_boolean is_rela)
83bac4b0
NC
14389{
14390 asection * reloc_sec = elf_section_data (sec)->sreloc;
14391
14392 if (reloc_sec == NULL)
14393 {
14394 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14395
14396 if (name == NULL)
14397 return NULL;
14398
3d4d4302 14399 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14400
14401 if (reloc_sec == NULL)
14402 {
3d4d4302
AM
14403 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14404 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14405 if ((sec->flags & SEC_ALLOC) != 0)
14406 flags |= SEC_ALLOC | SEC_LOAD;
14407
3d4d4302 14408 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14409 if (reloc_sec != NULL)
14410 {
8877b5e5
AM
14411 /* _bfd_elf_get_sec_type_attr chooses a section type by
14412 name. Override as it may be wrong, eg. for a user
14413 section named "auto" we'll get ".relauto" which is
14414 seen to be a .rela section. */
14415 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14416 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14417 reloc_sec = NULL;
14418 }
14419 }
14420
14421 elf_section_data (sec)->sreloc = reloc_sec;
14422 }
14423
14424 return reloc_sec;
14425}
1338dd10 14426
bffebb6b
AM
14427/* Copy the ELF symbol type and other attributes for a linker script
14428 assignment from HSRC to HDEST. Generally this should be treated as
14429 if we found a strong non-dynamic definition for HDEST (except that
14430 ld ignores multiple definition errors). */
1338dd10 14431void
bffebb6b
AM
14432_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14433 struct bfd_link_hash_entry *hdest,
14434 struct bfd_link_hash_entry *hsrc)
1338dd10 14435{
bffebb6b
AM
14436 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14437 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14438 Elf_Internal_Sym isym;
1338dd10
PB
14439
14440 ehdest->type = ehsrc->type;
35fc36a8 14441 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14442
14443 isym.st_other = ehsrc->other;
b8417128 14444 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14445}
351f65ca
L
14446
14447/* Append a RELA relocation REL to section S in BFD. */
14448
14449void
14450elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14451{
14452 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14453 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14454 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14455 bed->s->swap_reloca_out (abfd, rel, loc);
14456}
14457
14458/* Append a REL relocation REL to section S in BFD. */
14459
14460void
14461elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14462{
14463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14464 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14465 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14466 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14467}
7dba9362
AM
14468
14469/* Define __start, __stop, .startof. or .sizeof. symbol. */
14470
14471struct bfd_link_hash_entry *
14472bfd_elf_define_start_stop (struct bfd_link_info *info,
14473 const char *symbol, asection *sec)
14474{
487b6440 14475 struct elf_link_hash_entry *h;
7dba9362 14476
487b6440
AM
14477 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14478 FALSE, FALSE, TRUE);
14479 if (h != NULL
14480 && (h->root.type == bfd_link_hash_undefined
14481 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14482 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14483 {
bf3077a6 14484 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14485 h->root.type = bfd_link_hash_defined;
14486 h->root.u.def.section = sec;
14487 h->root.u.def.value = 0;
14488 h->def_regular = 1;
14489 h->def_dynamic = 0;
14490 h->start_stop = 1;
14491 h->u2.start_stop_section = sec;
14492 if (symbol[0] == '.')
14493 {
14494 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14495 const struct elf_backend_data *bed;
14496 bed = get_elf_backend_data (info->output_bfd);
14497 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14498 }
36b8fda5
AM
14499 else
14500 {
14501 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14502 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14503 if (was_dynamic)
36b8fda5
AM
14504 bfd_elf_link_record_dynamic_symbol (info, h);
14505 }
487b6440 14506 return &h->root;
7dba9362 14507 }
487b6440 14508 return NULL;
7dba9362 14509}
This page took 3.259053 seconds and 4 git commands to generate.