]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
Add elf64.lo together with elfxx-x86.lo for 64-bit BFD
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 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))
80 return h->root.u.def.section;
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)))
55255dae
L
589 h->dynamic = 1;
590}
591
45d6a902
AM
592/* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
594
595bfd_boolean
fe21a8fc
L
596bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
268b6b39 598 const char *name,
fe21a8fc
L
599 bfd_boolean provide,
600 bfd_boolean hidden)
45d6a902 601{
00cbee0a 602 struct elf_link_hash_entry *h, *hv;
4ea42fb7 603 struct elf_link_hash_table *htab;
00cbee0a 604 const struct elf_backend_data *bed;
45d6a902 605
0eddce27 606 if (!is_elf_hash_table (info->hash))
45d6a902
AM
607 return TRUE;
608
4ea42fb7
AM
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 611 if (h == NULL)
4ea42fb7 612 return provide;
45d6a902 613
8e2a4f11
AM
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
616
0f550b3d
L
617 if (h->versioned == unknown)
618 {
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
621 if (version)
622 {
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
625 else
626 h->versioned = versioned;
627 }
628 }
629
73ec947d
AM
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
632 if (h->non_elf)
633 {
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
635 h->non_elf = 0;
636 }
637
00cbee0a 638 switch (h->root.type)
77cfaee6 639 {
00cbee0a
L
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
643 break;
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
4ea42fb7 649 h->root.type = bfd_link_hash_new;
77cfaee6
AM
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
652 break;
653 case bfd_link_hash_new:
00cbee0a
L
654 break;
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 657 the versioned symbol point to this one. */
00cbee0a
L
658 bed = get_elf_backend_data (output_bfd);
659 hv = h;
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
664 later. */
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
669 break;
8e2a4f11
AM
670 default:
671 BFD_FAIL ();
c2596ca5 672 return FALSE;
55255dae 673 }
45d6a902
AM
674
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
679 if (provide
f5385ebf
AM
680 && h->def_dynamic
681 && !h->def_regular)
45d6a902
AM
682 h->root.type = bfd_link_hash_undefined;
683
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
45d6a902 688 if (!provide
f5385ebf
AM
689 && h->def_dynamic
690 && !h->def_regular)
b531344c
MR
691 h->verinfo.verdef = NULL;
692
693 /* Make sure this symbol is not garbage collected. */
694 h->mark = 1;
45d6a902 695
f5385ebf 696 h->def_regular = 1;
45d6a902 697
eb8476a6 698 if (hidden)
fe21a8fc 699 {
91d6fa6a 700 bed = get_elf_backend_data (output_bfd);
b8297068
AM
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
704 }
705
6fa3860b
PB
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
707 and executables. */
0e1862bb 708 if (!bfd_link_relocatable (info)
6fa3860b
PB
709 && h->dynindx != -1
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
712 h->forced_local = 1;
713
f5385ebf
AM
714 if ((h->def_dynamic
715 || h->ref_dynamic
6b3b0ab8
L
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
718 && h->dynindx == -1)
719 {
c152c796 720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
721 return FALSE;
722
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
f6e332e6
AM
726 if (h->u.weakdef != NULL
727 && h->u.weakdef->dynindx == -1)
45d6a902 728 {
f6e332e6 729 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
730 return FALSE;
731 }
732 }
733
734 return TRUE;
735}
42751cf3 736
8c58d23b
AM
737/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
738 success, and 2 on a failure caused by attempting to record a symbol
739 in a discarded section, eg. a discarded link-once section symbol. */
740
741int
c152c796
AM
742bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
743 bfd *input_bfd,
744 long input_indx)
8c58d23b
AM
745{
746 bfd_size_type amt;
747 struct elf_link_local_dynamic_entry *entry;
748 struct elf_link_hash_table *eht;
749 struct elf_strtab_hash *dynstr;
ef53be89 750 size_t dynstr_index;
8c58d23b
AM
751 char *name;
752 Elf_External_Sym_Shndx eshndx;
753 char esym[sizeof (Elf64_External_Sym)];
754
0eddce27 755 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
756 return 0;
757
758 /* See if the entry exists already. */
759 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
760 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
761 return 1;
762
763 amt = sizeof (*entry);
a50b1753 764 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
765 if (entry == NULL)
766 return 0;
767
768 /* Go find the symbol, so that we can find it's name. */
769 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 770 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
771 {
772 bfd_release (input_bfd, entry);
773 return 0;
774 }
775
776 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 777 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
778 {
779 asection *s;
780
781 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
782 if (s == NULL || bfd_is_abs_section (s->output_section))
783 {
784 /* We can still bfd_release here as nothing has done another
785 bfd_alloc. We can't do this later in this function. */
786 bfd_release (input_bfd, entry);
787 return 2;
788 }
789 }
790
791 name = (bfd_elf_string_from_elf_section
792 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
793 entry->isym.st_name));
794
795 dynstr = elf_hash_table (info)->dynstr;
796 if (dynstr == NULL)
797 {
798 /* Create a strtab to hold the dynamic symbol names. */
799 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
800 if (dynstr == NULL)
801 return 0;
802 }
803
b34976b6 804 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 805 if (dynstr_index == (size_t) -1)
8c58d23b
AM
806 return 0;
807 entry->isym.st_name = dynstr_index;
808
809 eht = elf_hash_table (info);
810
811 entry->next = eht->dynlocal;
812 eht->dynlocal = entry;
813 entry->input_bfd = input_bfd;
814 entry->input_indx = input_indx;
815 eht->dynsymcount++;
816
817 /* Whatever binding the symbol had before, it's now local. */
818 entry->isym.st_info
819 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
820
821 /* The dynindx will be set at the end of size_dynamic_sections. */
822
823 return 1;
824}
825
30b30c21 826/* Return the dynindex of a local dynamic symbol. */
42751cf3 827
30b30c21 828long
268b6b39
AM
829_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
830 bfd *input_bfd,
831 long input_indx)
30b30c21
RH
832{
833 struct elf_link_local_dynamic_entry *e;
834
835 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
836 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
837 return e->dynindx;
838 return -1;
839}
840
841/* This function is used to renumber the dynamic symbols, if some of
842 them are removed because they are marked as local. This is called
843 via elf_link_hash_traverse. */
844
b34976b6 845static bfd_boolean
268b6b39
AM
846elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
847 void *data)
42751cf3 848{
a50b1753 849 size_t *count = (size_t *) data;
30b30c21 850
6fa3860b
PB
851 if (h->forced_local)
852 return TRUE;
853
854 if (h->dynindx != -1)
855 h->dynindx = ++(*count);
856
857 return TRUE;
858}
859
860
861/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
862 STB_LOCAL binding. */
863
864static bfd_boolean
865elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
866 void *data)
867{
a50b1753 868 size_t *count = (size_t *) data;
6fa3860b 869
6fa3860b
PB
870 if (!h->forced_local)
871 return TRUE;
872
42751cf3 873 if (h->dynindx != -1)
30b30c21
RH
874 h->dynindx = ++(*count);
875
b34976b6 876 return TRUE;
42751cf3 877}
30b30c21 878
aee6f5b4
AO
879/* Return true if the dynamic symbol for a given section should be
880 omitted when creating a shared library. */
881bfd_boolean
882_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
883 struct bfd_link_info *info,
884 asection *p)
885{
74541ad4 886 struct elf_link_hash_table *htab;
ca55926c 887 asection *ip;
74541ad4 888
aee6f5b4
AO
889 switch (elf_section_data (p)->this_hdr.sh_type)
890 {
891 case SHT_PROGBITS:
892 case SHT_NOBITS:
893 /* If sh_type is yet undecided, assume it could be
894 SHT_PROGBITS/SHT_NOBITS. */
895 case SHT_NULL:
74541ad4
AM
896 htab = elf_hash_table (info);
897 if (p == htab->tls_sec)
898 return FALSE;
899
900 if (htab->text_index_section != NULL)
901 return p != htab->text_index_section && p != htab->data_index_section;
902
ca55926c 903 return (htab->dynobj != NULL
3d4d4302 904 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 905 && ip->output_section == p);
aee6f5b4
AO
906
907 /* There shouldn't be section relative relocations
908 against any other section. */
909 default:
910 return TRUE;
911 }
912}
913
062e2358 914/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
915 symbol for each output section, which come first. Next come symbols
916 which have been forced to local binding. Then all of the back-end
917 allocated local dynamic syms, followed by the rest of the global
918 symbols. */
30b30c21 919
554220db
AM
920static unsigned long
921_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
922 struct bfd_link_info *info,
923 unsigned long *section_sym_count)
30b30c21
RH
924{
925 unsigned long dynsymcount = 0;
926
0e1862bb
L
927 if (bfd_link_pic (info)
928 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 929 {
aee6f5b4 930 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
931 asection *p;
932 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 933 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
934 && (p->flags & SEC_ALLOC) != 0
935 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
936 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
937 else
938 elf_section_data (p)->dynindx = 0;
30b30c21 939 }
554220db 940 *section_sym_count = dynsymcount;
30b30c21 941
6fa3860b
PB
942 elf_link_hash_traverse (elf_hash_table (info),
943 elf_link_renumber_local_hash_table_dynsyms,
944 &dynsymcount);
945
30b30c21
RH
946 if (elf_hash_table (info)->dynlocal)
947 {
948 struct elf_link_local_dynamic_entry *p;
949 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
950 p->dynindx = ++dynsymcount;
951 }
90ac2420 952 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
953
954 elf_link_hash_traverse (elf_hash_table (info),
955 elf_link_renumber_hash_table_dynsyms,
956 &dynsymcount);
957
d5486c43
L
958 /* There is an unused NULL entry at the head of the table which we
959 must account for in our count even if the table is empty since it
960 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
961 .dynamic section. */
962 dynsymcount++;
30b30c21 963
ccabcbe5
AM
964 elf_hash_table (info)->dynsymcount = dynsymcount;
965 return dynsymcount;
30b30c21 966}
252b5132 967
54ac0771
L
968/* Merge st_other field. */
969
970static void
971elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 972 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 973 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
974{
975 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
976
977 /* If st_other has a processor-specific meaning, specific
cd3416da 978 code might be needed here. */
54ac0771
L
979 if (bed->elf_backend_merge_symbol_attribute)
980 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
981 dynamic);
982
cd3416da 983 if (!dynamic)
54ac0771 984 {
cd3416da
AM
985 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
986 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 987
cd3416da
AM
988 /* Keep the most constraining visibility. Leave the remainder
989 of the st_other field to elf_backend_merge_symbol_attribute. */
990 if (symvis - 1 < hvis - 1)
991 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 992 }
b8417128
AM
993 else if (definition
994 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
995 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 996 h->protected_def = 1;
54ac0771
L
997}
998
4f3fedcf
AM
999/* This function is called when we want to merge a new symbol with an
1000 existing symbol. It handles the various cases which arise when we
1001 find a definition in a dynamic object, or when there is already a
1002 definition in a dynamic object. The new symbol is described by
1003 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1004 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1005 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1006 of an old common symbol. We set OVERRIDE if the old symbol is
1007 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1008 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1009 to change. By OK to change, we mean that we shouldn't warn if the
1010 type or size does change. */
45d6a902 1011
8a56bd02 1012static bfd_boolean
268b6b39
AM
1013_bfd_elf_merge_symbol (bfd *abfd,
1014 struct bfd_link_info *info,
1015 const char *name,
1016 Elf_Internal_Sym *sym,
1017 asection **psec,
1018 bfd_vma *pvalue,
4f3fedcf
AM
1019 struct elf_link_hash_entry **sym_hash,
1020 bfd **poldbfd,
37a9e49a 1021 bfd_boolean *pold_weak,
af44c138 1022 unsigned int *pold_alignment,
268b6b39
AM
1023 bfd_boolean *skip,
1024 bfd_boolean *override,
1025 bfd_boolean *type_change_ok,
6e33951e
L
1026 bfd_boolean *size_change_ok,
1027 bfd_boolean *matched)
252b5132 1028{
7479dfd4 1029 asection *sec, *oldsec;
45d6a902 1030 struct elf_link_hash_entry *h;
90c984fc 1031 struct elf_link_hash_entry *hi;
45d6a902
AM
1032 struct elf_link_hash_entry *flip;
1033 int bind;
1034 bfd *oldbfd;
1035 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1036 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1037 const struct elf_backend_data *bed;
6e33951e 1038 char *new_version;
45d6a902
AM
1039
1040 *skip = FALSE;
1041 *override = FALSE;
1042
1043 sec = *psec;
1044 bind = ELF_ST_BIND (sym->st_info);
1045
1046 if (! bfd_is_und_section (sec))
1047 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1048 else
1049 h = ((struct elf_link_hash_entry *)
1050 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1051 if (h == NULL)
1052 return FALSE;
1053 *sym_hash = h;
252b5132 1054
88ba32a0
L
1055 bed = get_elf_backend_data (abfd);
1056
6e33951e 1057 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1058 if (h->versioned != unversioned)
6e33951e 1059 {
422f1182
L
1060 /* Symbol version is unknown or versioned. */
1061 new_version = strrchr (name, ELF_VER_CHR);
1062 if (new_version)
1063 {
1064 if (h->versioned == unknown)
1065 {
1066 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1067 h->versioned = versioned_hidden;
1068 else
1069 h->versioned = versioned;
1070 }
1071 new_version += 1;
1072 if (new_version[0] == '\0')
1073 new_version = NULL;
1074 }
1075 else
1076 h->versioned = unversioned;
6e33951e 1077 }
422f1182
L
1078 else
1079 new_version = NULL;
6e33951e 1080
90c984fc
L
1081 /* For merging, we only care about real symbols. But we need to make
1082 sure that indirect symbol dynamic flags are updated. */
1083 hi = h;
45d6a902
AM
1084 while (h->root.type == bfd_link_hash_indirect
1085 || h->root.type == bfd_link_hash_warning)
1086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1087
6e33951e
L
1088 if (!*matched)
1089 {
1090 if (hi == h || h->root.type == bfd_link_hash_new)
1091 *matched = TRUE;
1092 else
1093 {
ae7683d2 1094 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1095 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1096 true if the new symbol is only visible to the symbol with
6e33951e 1097 the same symbol version. */
422f1182
L
1098 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1099 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1100 if (!old_hidden && !new_hidden)
1101 /* The new symbol matches the existing symbol if both
1102 aren't hidden. */
1103 *matched = TRUE;
1104 else
1105 {
1106 /* OLD_VERSION is the symbol version of the existing
1107 symbol. */
422f1182
L
1108 char *old_version;
1109
1110 if (h->versioned >= versioned)
1111 old_version = strrchr (h->root.root.string,
1112 ELF_VER_CHR) + 1;
1113 else
1114 old_version = NULL;
6e33951e
L
1115
1116 /* The new symbol matches the existing symbol if they
1117 have the same symbol version. */
1118 *matched = (old_version == new_version
1119 || (old_version != NULL
1120 && new_version != NULL
1121 && strcmp (old_version, new_version) == 0));
1122 }
1123 }
1124 }
1125
934bce08
AM
1126 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1127 existing symbol. */
1128
1129 oldbfd = NULL;
1130 oldsec = NULL;
1131 switch (h->root.type)
1132 {
1133 default:
1134 break;
1135
1136 case bfd_link_hash_undefined:
1137 case bfd_link_hash_undefweak:
1138 oldbfd = h->root.u.undef.abfd;
1139 break;
1140
1141 case bfd_link_hash_defined:
1142 case bfd_link_hash_defweak:
1143 oldbfd = h->root.u.def.section->owner;
1144 oldsec = h->root.u.def.section;
1145 break;
1146
1147 case bfd_link_hash_common:
1148 oldbfd = h->root.u.c.p->section->owner;
1149 oldsec = h->root.u.c.p->section;
1150 if (pold_alignment)
1151 *pold_alignment = h->root.u.c.p->alignment_power;
1152 break;
1153 }
1154 if (poldbfd && *poldbfd == NULL)
1155 *poldbfd = oldbfd;
1156
1157 /* Differentiate strong and weak symbols. */
1158 newweak = bind == STB_WEAK;
1159 oldweak = (h->root.type == bfd_link_hash_defweak
1160 || h->root.type == bfd_link_hash_undefweak);
1161 if (pold_weak)
1162 *pold_weak = oldweak;
1163
1164 /* This code is for coping with dynamic objects, and is only useful
1165 if we are doing an ELF link. */
1166 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1167 return TRUE;
1168
40b36307 1169 /* We have to check it for every instance since the first few may be
ee659f1f 1170 references and not all compilers emit symbol type for undefined
40b36307
L
1171 symbols. */
1172 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1173
ee659f1f
AM
1174 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1175 respectively, is from a dynamic object. */
1176
1177 newdyn = (abfd->flags & DYNAMIC) != 0;
1178
1179 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1180 syms and defined syms in dynamic libraries respectively.
1181 ref_dynamic on the other hand can be set for a symbol defined in
1182 a dynamic library, and def_dynamic may not be set; When the
1183 definition in a dynamic lib is overridden by a definition in the
1184 executable use of the symbol in the dynamic lib becomes a
1185 reference to the executable symbol. */
1186 if (newdyn)
1187 {
1188 if (bfd_is_und_section (sec))
1189 {
1190 if (bind != STB_WEAK)
1191 {
1192 h->ref_dynamic_nonweak = 1;
1193 hi->ref_dynamic_nonweak = 1;
1194 }
1195 }
1196 else
1197 {
6e33951e
L
1198 /* Update the existing symbol only if they match. */
1199 if (*matched)
1200 h->dynamic_def = 1;
ee659f1f
AM
1201 hi->dynamic_def = 1;
1202 }
1203 }
1204
45d6a902
AM
1205 /* If we just created the symbol, mark it as being an ELF symbol.
1206 Other than that, there is nothing to do--there is no merge issue
1207 with a newly defined symbol--so we just return. */
1208
1209 if (h->root.type == bfd_link_hash_new)
252b5132 1210 {
f5385ebf 1211 h->non_elf = 0;
45d6a902
AM
1212 return TRUE;
1213 }
252b5132 1214
45d6a902
AM
1215 /* In cases involving weak versioned symbols, we may wind up trying
1216 to merge a symbol with itself. Catch that here, to avoid the
1217 confusion that results if we try to override a symbol with
1218 itself. The additional tests catch cases like
1219 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1220 dynamic object, which we do want to handle here. */
1221 if (abfd == oldbfd
895fa45f 1222 && (newweak || oldweak)
45d6a902 1223 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1224 || !h->def_regular))
45d6a902
AM
1225 return TRUE;
1226
707bba77 1227 olddyn = FALSE;
45d6a902
AM
1228 if (oldbfd != NULL)
1229 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1230 else if (oldsec != NULL)
45d6a902 1231 {
707bba77 1232 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1233 indices used by MIPS ELF. */
707bba77 1234 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1235 }
252b5132 1236
45d6a902
AM
1237 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1238 respectively, appear to be a definition rather than reference. */
1239
707bba77 1240 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1241
707bba77
AM
1242 olddef = (h->root.type != bfd_link_hash_undefined
1243 && h->root.type != bfd_link_hash_undefweak
202ac193 1244 && h->root.type != bfd_link_hash_common);
45d6a902 1245
0a36a439
L
1246 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1247 respectively, appear to be a function. */
1248
1249 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1250 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1251
1252 oldfunc = (h->type != STT_NOTYPE
1253 && bed->is_function_type (h->type));
1254
c5d37467 1255 if (!(newfunc && oldfunc)
5b677558
AM
1256 && ELF_ST_TYPE (sym->st_info) != h->type
1257 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1258 && h->type != STT_NOTYPE
c5d37467
AM
1259 && (newdef || bfd_is_com_section (sec))
1260 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1261 {
c5d37467
AM
1262 /* If creating a default indirect symbol ("foo" or "foo@") from
1263 a dynamic versioned definition ("foo@@") skip doing so if
1264 there is an existing regular definition with a different
1265 type. We don't want, for example, a "time" variable in the
1266 executable overriding a "time" function in a shared library. */
1267 if (newdyn
1268 && !olddyn)
1269 {
1270 *skip = TRUE;
1271 return TRUE;
1272 }
1273
1274 /* When adding a symbol from a regular object file after we have
1275 created indirect symbols, undo the indirection and any
1276 dynamic state. */
1277 if (hi != h
1278 && !newdyn
1279 && olddyn)
1280 {
1281 h = hi;
1282 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1283 h->forced_local = 0;
1284 h->ref_dynamic = 0;
1285 h->def_dynamic = 0;
1286 h->dynamic_def = 0;
1287 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1288 {
1289 h->root.type = bfd_link_hash_undefined;
1290 h->root.u.undef.abfd = abfd;
1291 }
1292 else
1293 {
1294 h->root.type = bfd_link_hash_new;
1295 h->root.u.undef.abfd = NULL;
1296 }
1297 return TRUE;
1298 }
580a2b6e
L
1299 }
1300
4c34aff8
AM
1301 /* Check TLS symbols. We don't check undefined symbols introduced
1302 by "ld -u" which have no type (and oldbfd NULL), and we don't
1303 check symbols from plugins because they also have no type. */
1304 if (oldbfd != NULL
1305 && (oldbfd->flags & BFD_PLUGIN) == 0
1306 && (abfd->flags & BFD_PLUGIN) == 0
1307 && ELF_ST_TYPE (sym->st_info) != h->type
1308 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1309 {
1310 bfd *ntbfd, *tbfd;
1311 bfd_boolean ntdef, tdef;
1312 asection *ntsec, *tsec;
1313
1314 if (h->type == STT_TLS)
1315 {
3b36f7e6 1316 ntbfd = abfd;
7479dfd4
L
1317 ntsec = sec;
1318 ntdef = newdef;
1319 tbfd = oldbfd;
1320 tsec = oldsec;
1321 tdef = olddef;
1322 }
1323 else
1324 {
1325 ntbfd = oldbfd;
1326 ntsec = oldsec;
1327 ntdef = olddef;
1328 tbfd = abfd;
1329 tsec = sec;
1330 tdef = newdef;
1331 }
1332
1333 if (tdef && ntdef)
4eca0228 1334 _bfd_error_handler
695344c0 1335 /* xgettext:c-format */
191c0c42
AM
1336 (_("%s: TLS definition in %B section %A "
1337 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1338 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1339 else if (!tdef && !ntdef)
4eca0228 1340 _bfd_error_handler
695344c0 1341 /* xgettext:c-format */
191c0c42
AM
1342 (_("%s: TLS reference in %B "
1343 "mismatches non-TLS reference in %B"),
c08bb8dd 1344 h->root.root.string, tbfd, ntbfd);
7479dfd4 1345 else if (tdef)
4eca0228 1346 _bfd_error_handler
695344c0 1347 /* xgettext:c-format */
191c0c42
AM
1348 (_("%s: TLS definition in %B section %A "
1349 "mismatches non-TLS reference in %B"),
c08bb8dd 1350 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1351 else
4eca0228 1352 _bfd_error_handler
695344c0 1353 /* xgettext:c-format */
191c0c42
AM
1354 (_("%s: TLS reference in %B "
1355 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1356 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1357
1358 bfd_set_error (bfd_error_bad_value);
1359 return FALSE;
1360 }
1361
45d6a902
AM
1362 /* If the old symbol has non-default visibility, we ignore the new
1363 definition from a dynamic object. */
1364 if (newdyn
9c7a29a3 1365 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1366 && !bfd_is_und_section (sec))
1367 {
1368 *skip = TRUE;
1369 /* Make sure this symbol is dynamic. */
f5385ebf 1370 h->ref_dynamic = 1;
90c984fc 1371 hi->ref_dynamic = 1;
45d6a902
AM
1372 /* A protected symbol has external availability. Make sure it is
1373 recorded as dynamic.
1374
1375 FIXME: Should we check type and size for protected symbol? */
1376 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1377 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1378 else
1379 return TRUE;
1380 }
1381 else if (!newdyn
9c7a29a3 1382 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1383 && h->def_dynamic)
45d6a902
AM
1384 {
1385 /* If the new symbol with non-default visibility comes from a
1386 relocatable file and the old definition comes from a dynamic
1387 object, we remove the old definition. */
6c9b78e6 1388 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1389 {
1390 /* Handle the case where the old dynamic definition is
1391 default versioned. We need to copy the symbol info from
1392 the symbol with default version to the normal one if it
1393 was referenced before. */
1394 if (h->ref_regular)
1395 {
6c9b78e6 1396 hi->root.type = h->root.type;
d2dee3b2 1397 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1398 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1399
6c9b78e6 1400 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1401 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1402 {
aed81c4e
MR
1403 /* If the new symbol is hidden or internal, completely undo
1404 any dynamic link state. */
1405 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1406 h->forced_local = 0;
1407 h->ref_dynamic = 0;
d2dee3b2
L
1408 }
1409 else
aed81c4e
MR
1410 h->ref_dynamic = 1;
1411
1412 h->def_dynamic = 0;
aed81c4e
MR
1413 /* FIXME: Should we check type and size for protected symbol? */
1414 h->size = 0;
1415 h->type = 0;
1416
6c9b78e6 1417 h = hi;
d2dee3b2
L
1418 }
1419 else
6c9b78e6 1420 h = hi;
d2dee3b2 1421 }
1de1a317 1422
f5eda473
AM
1423 /* If the old symbol was undefined before, then it will still be
1424 on the undefs list. If the new symbol is undefined or
1425 common, we can't make it bfd_link_hash_new here, because new
1426 undefined or common symbols will be added to the undefs list
1427 by _bfd_generic_link_add_one_symbol. Symbols may not be
1428 added twice to the undefs list. Also, if the new symbol is
1429 undefweak then we don't want to lose the strong undef. */
1430 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1431 {
1de1a317 1432 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1433 h->root.u.undef.abfd = abfd;
1434 }
1435 else
1436 {
1437 h->root.type = bfd_link_hash_new;
1438 h->root.u.undef.abfd = NULL;
1439 }
1440
f5eda473 1441 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1442 {
f5eda473
AM
1443 /* If the new symbol is hidden or internal, completely undo
1444 any dynamic link state. */
1445 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1446 h->forced_local = 0;
1447 h->ref_dynamic = 0;
45d6a902 1448 }
f5eda473
AM
1449 else
1450 h->ref_dynamic = 1;
1451 h->def_dynamic = 0;
45d6a902
AM
1452 /* FIXME: Should we check type and size for protected symbol? */
1453 h->size = 0;
1454 h->type = 0;
1455 return TRUE;
1456 }
14a793b2 1457
15b43f48
AM
1458 /* If a new weak symbol definition comes from a regular file and the
1459 old symbol comes from a dynamic library, we treat the new one as
1460 strong. Similarly, an old weak symbol definition from a regular
1461 file is treated as strong when the new symbol comes from a dynamic
1462 library. Further, an old weak symbol from a dynamic library is
1463 treated as strong if the new symbol is from a dynamic library.
1464 This reflects the way glibc's ld.so works.
1465
1466 Do this before setting *type_change_ok or *size_change_ok so that
1467 we warn properly when dynamic library symbols are overridden. */
1468
1469 if (newdef && !newdyn && olddyn)
0f8a2703 1470 newweak = FALSE;
15b43f48 1471 if (olddef && newdyn)
0f8a2703
AM
1472 oldweak = FALSE;
1473
d334575b 1474 /* Allow changes between different types of function symbol. */
0a36a439 1475 if (newfunc && oldfunc)
fcb93ecf
PB
1476 *type_change_ok = TRUE;
1477
79349b09
AM
1478 /* It's OK to change the type if either the existing symbol or the
1479 new symbol is weak. A type change is also OK if the old symbol
1480 is undefined and the new symbol is defined. */
252b5132 1481
79349b09
AM
1482 if (oldweak
1483 || newweak
1484 || (newdef
1485 && h->root.type == bfd_link_hash_undefined))
1486 *type_change_ok = TRUE;
1487
1488 /* It's OK to change the size if either the existing symbol or the
1489 new symbol is weak, or if the old symbol is undefined. */
1490
1491 if (*type_change_ok
1492 || h->root.type == bfd_link_hash_undefined)
1493 *size_change_ok = TRUE;
45d6a902 1494
45d6a902
AM
1495 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1496 symbol, respectively, appears to be a common symbol in a dynamic
1497 object. If a symbol appears in an uninitialized section, and is
1498 not weak, and is not a function, then it may be a common symbol
1499 which was resolved when the dynamic object was created. We want
1500 to treat such symbols specially, because they raise special
1501 considerations when setting the symbol size: if the symbol
1502 appears as a common symbol in a regular object, and the size in
1503 the regular object is larger, we must make sure that we use the
1504 larger size. This problematic case can always be avoided in C,
1505 but it must be handled correctly when using Fortran shared
1506 libraries.
1507
1508 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1509 likewise for OLDDYNCOMMON and OLDDEF.
1510
1511 Note that this test is just a heuristic, and that it is quite
1512 possible to have an uninitialized symbol in a shared object which
1513 is really a definition, rather than a common symbol. This could
1514 lead to some minor confusion when the symbol really is a common
1515 symbol in some regular object. However, I think it will be
1516 harmless. */
1517
1518 if (newdyn
1519 && newdef
79349b09 1520 && !newweak
45d6a902
AM
1521 && (sec->flags & SEC_ALLOC) != 0
1522 && (sec->flags & SEC_LOAD) == 0
1523 && sym->st_size > 0
0a36a439 1524 && !newfunc)
45d6a902
AM
1525 newdyncommon = TRUE;
1526 else
1527 newdyncommon = FALSE;
1528
1529 if (olddyn
1530 && olddef
1531 && h->root.type == bfd_link_hash_defined
f5385ebf 1532 && h->def_dynamic
45d6a902
AM
1533 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1534 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1535 && h->size > 0
0a36a439 1536 && !oldfunc)
45d6a902
AM
1537 olddyncommon = TRUE;
1538 else
1539 olddyncommon = FALSE;
1540
a4d8e49b
L
1541 /* We now know everything about the old and new symbols. We ask the
1542 backend to check if we can merge them. */
5d13b3b3
AM
1543 if (bed->merge_symbol != NULL)
1544 {
1545 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1546 return FALSE;
1547 sec = *psec;
1548 }
a4d8e49b 1549
45d6a902
AM
1550 /* If both the old and the new symbols look like common symbols in a
1551 dynamic object, set the size of the symbol to the larger of the
1552 two. */
1553
1554 if (olddyncommon
1555 && newdyncommon
1556 && sym->st_size != h->size)
1557 {
1558 /* Since we think we have two common symbols, issue a multiple
1559 common warning if desired. Note that we only warn if the
1560 size is different. If the size is the same, we simply let
1561 the old symbol override the new one as normally happens with
1562 symbols defined in dynamic objects. */
1563
1a72702b
AM
1564 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1565 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1566 if (sym->st_size > h->size)
1567 h->size = sym->st_size;
252b5132 1568
45d6a902 1569 *size_change_ok = TRUE;
252b5132
RH
1570 }
1571
45d6a902
AM
1572 /* If we are looking at a dynamic object, and we have found a
1573 definition, we need to see if the symbol was already defined by
1574 some other object. If so, we want to use the existing
1575 definition, and we do not want to report a multiple symbol
1576 definition error; we do this by clobbering *PSEC to be
1577 bfd_und_section_ptr.
1578
1579 We treat a common symbol as a definition if the symbol in the
1580 shared library is a function, since common symbols always
1581 represent variables; this can cause confusion in principle, but
1582 any such confusion would seem to indicate an erroneous program or
1583 shared library. We also permit a common symbol in a regular
8170f769 1584 object to override a weak symbol in a shared object. */
45d6a902
AM
1585
1586 if (newdyn
1587 && newdef
77cfaee6 1588 && (olddef
45d6a902 1589 || (h->root.type == bfd_link_hash_common
8170f769 1590 && (newweak || newfunc))))
45d6a902
AM
1591 {
1592 *override = TRUE;
1593 newdef = FALSE;
1594 newdyncommon = FALSE;
252b5132 1595
45d6a902
AM
1596 *psec = sec = bfd_und_section_ptr;
1597 *size_change_ok = TRUE;
252b5132 1598
45d6a902
AM
1599 /* If we get here when the old symbol is a common symbol, then
1600 we are explicitly letting it override a weak symbol or
1601 function in a dynamic object, and we don't want to warn about
1602 a type change. If the old symbol is a defined symbol, a type
1603 change warning may still be appropriate. */
252b5132 1604
45d6a902
AM
1605 if (h->root.type == bfd_link_hash_common)
1606 *type_change_ok = TRUE;
1607 }
1608
1609 /* Handle the special case of an old common symbol merging with a
1610 new symbol which looks like a common symbol in a shared object.
1611 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1612 common symbol, and let _bfd_generic_link_add_one_symbol do the
1613 right thing. */
45d6a902
AM
1614
1615 if (newdyncommon
1616 && h->root.type == bfd_link_hash_common)
1617 {
1618 *override = TRUE;
1619 newdef = FALSE;
1620 newdyncommon = FALSE;
1621 *pvalue = sym->st_size;
a4d8e49b 1622 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1623 *size_change_ok = TRUE;
1624 }
1625
c5e2cead 1626 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1627 if (newdef && olddef && newweak)
54ac0771 1628 {
35ed3f94 1629 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1630 if (!(oldbfd != NULL
1631 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1632 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1633 {
1634 newdef = FALSE;
1635 *skip = TRUE;
1636 }
54ac0771
L
1637
1638 /* Merge st_other. If the symbol already has a dynamic index,
1639 but visibility says it should not be visible, turn it into a
1640 local symbol. */
b8417128 1641 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1642 if (h->dynindx != -1)
1643 switch (ELF_ST_VISIBILITY (h->other))
1644 {
1645 case STV_INTERNAL:
1646 case STV_HIDDEN:
1647 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1648 break;
1649 }
1650 }
c5e2cead 1651
45d6a902
AM
1652 /* If the old symbol is from a dynamic object, and the new symbol is
1653 a definition which is not from a dynamic object, then the new
1654 symbol overrides the old symbol. Symbols from regular files
1655 always take precedence over symbols from dynamic objects, even if
1656 they are defined after the dynamic object in the link.
1657
1658 As above, we again permit a common symbol in a regular object to
1659 override a definition in a shared object if the shared object
0f8a2703 1660 symbol is a function or is weak. */
45d6a902
AM
1661
1662 flip = NULL;
77cfaee6 1663 if (!newdyn
45d6a902
AM
1664 && (newdef
1665 || (bfd_is_com_section (sec)
0a36a439 1666 && (oldweak || oldfunc)))
45d6a902
AM
1667 && olddyn
1668 && olddef
f5385ebf 1669 && h->def_dynamic)
45d6a902
AM
1670 {
1671 /* Change the hash table entry to undefined, and let
1672 _bfd_generic_link_add_one_symbol do the right thing with the
1673 new definition. */
1674
1675 h->root.type = bfd_link_hash_undefined;
1676 h->root.u.undef.abfd = h->root.u.def.section->owner;
1677 *size_change_ok = TRUE;
1678
1679 olddef = FALSE;
1680 olddyncommon = FALSE;
1681
1682 /* We again permit a type change when a common symbol may be
1683 overriding a function. */
1684
1685 if (bfd_is_com_section (sec))
0a36a439
L
1686 {
1687 if (oldfunc)
1688 {
1689 /* If a common symbol overrides a function, make sure
1690 that it isn't defined dynamically nor has type
1691 function. */
1692 h->def_dynamic = 0;
1693 h->type = STT_NOTYPE;
1694 }
1695 *type_change_ok = TRUE;
1696 }
45d6a902 1697
6c9b78e6
AM
1698 if (hi->root.type == bfd_link_hash_indirect)
1699 flip = hi;
45d6a902
AM
1700 else
1701 /* This union may have been set to be non-NULL when this symbol
1702 was seen in a dynamic object. We must force the union to be
1703 NULL, so that it is correct for a regular symbol. */
1704 h->verinfo.vertree = NULL;
1705 }
1706
1707 /* Handle the special case of a new common symbol merging with an
1708 old symbol that looks like it might be a common symbol defined in
1709 a shared object. Note that we have already handled the case in
1710 which a new common symbol should simply override the definition
1711 in the shared library. */
1712
1713 if (! newdyn
1714 && bfd_is_com_section (sec)
1715 && olddyncommon)
1716 {
1717 /* It would be best if we could set the hash table entry to a
1718 common symbol, but we don't know what to use for the section
1719 or the alignment. */
1a72702b
AM
1720 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1721 bfd_link_hash_common, sym->st_size);
45d6a902 1722
4cc11e76 1723 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1724 larger, pretend that the new symbol has its size. */
1725
1726 if (h->size > *pvalue)
1727 *pvalue = h->size;
1728
af44c138
L
1729 /* We need to remember the alignment required by the symbol
1730 in the dynamic object. */
1731 BFD_ASSERT (pold_alignment);
1732 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1733
1734 olddef = FALSE;
1735 olddyncommon = FALSE;
1736
1737 h->root.type = bfd_link_hash_undefined;
1738 h->root.u.undef.abfd = h->root.u.def.section->owner;
1739
1740 *size_change_ok = TRUE;
1741 *type_change_ok = TRUE;
1742
6c9b78e6
AM
1743 if (hi->root.type == bfd_link_hash_indirect)
1744 flip = hi;
45d6a902
AM
1745 else
1746 h->verinfo.vertree = NULL;
1747 }
1748
1749 if (flip != NULL)
1750 {
1751 /* Handle the case where we had a versioned symbol in a dynamic
1752 library and now find a definition in a normal object. In this
1753 case, we make the versioned symbol point to the normal one. */
45d6a902 1754 flip->root.type = h->root.type;
00cbee0a 1755 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1756 h->root.type = bfd_link_hash_indirect;
1757 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1758 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1759 if (h->def_dynamic)
45d6a902 1760 {
f5385ebf
AM
1761 h->def_dynamic = 0;
1762 flip->ref_dynamic = 1;
45d6a902
AM
1763 }
1764 }
1765
45d6a902
AM
1766 return TRUE;
1767}
1768
1769/* This function is called to create an indirect symbol from the
1770 default for the symbol with the default version if needed. The
4f3fedcf 1771 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1772 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1773
28caa186 1774static bfd_boolean
268b6b39
AM
1775_bfd_elf_add_default_symbol (bfd *abfd,
1776 struct bfd_link_info *info,
1777 struct elf_link_hash_entry *h,
1778 const char *name,
1779 Elf_Internal_Sym *sym,
4f3fedcf
AM
1780 asection *sec,
1781 bfd_vma value,
1782 bfd **poldbfd,
e3c9d234 1783 bfd_boolean *dynsym)
45d6a902
AM
1784{
1785 bfd_boolean type_change_ok;
1786 bfd_boolean size_change_ok;
1787 bfd_boolean skip;
1788 char *shortname;
1789 struct elf_link_hash_entry *hi;
1790 struct bfd_link_hash_entry *bh;
9c5bfbb7 1791 const struct elf_backend_data *bed;
45d6a902
AM
1792 bfd_boolean collect;
1793 bfd_boolean dynamic;
e3c9d234 1794 bfd_boolean override;
45d6a902
AM
1795 char *p;
1796 size_t len, shortlen;
ffd65175 1797 asection *tmp_sec;
6e33951e 1798 bfd_boolean matched;
45d6a902 1799
422f1182
L
1800 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1801 return TRUE;
1802
45d6a902
AM
1803 /* If this symbol has a version, and it is the default version, we
1804 create an indirect symbol from the default name to the fully
1805 decorated name. This will cause external references which do not
1806 specify a version to be bound to this version of the symbol. */
1807 p = strchr (name, ELF_VER_CHR);
422f1182
L
1808 if (h->versioned == unknown)
1809 {
1810 if (p == NULL)
1811 {
1812 h->versioned = unversioned;
1813 return TRUE;
1814 }
1815 else
1816 {
1817 if (p[1] != ELF_VER_CHR)
1818 {
1819 h->versioned = versioned_hidden;
1820 return TRUE;
1821 }
1822 else
1823 h->versioned = versioned;
1824 }
1825 }
4373f8af
L
1826 else
1827 {
1828 /* PR ld/19073: We may see an unversioned definition after the
1829 default version. */
1830 if (p == NULL)
1831 return TRUE;
1832 }
45d6a902 1833
45d6a902
AM
1834 bed = get_elf_backend_data (abfd);
1835 collect = bed->collect;
1836 dynamic = (abfd->flags & DYNAMIC) != 0;
1837
1838 shortlen = p - name;
a50b1753 1839 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1840 if (shortname == NULL)
1841 return FALSE;
1842 memcpy (shortname, name, shortlen);
1843 shortname[shortlen] = '\0';
1844
1845 /* We are going to create a new symbol. Merge it with any existing
1846 symbol with this name. For the purposes of the merge, act as
1847 though we were defining the symbol we just defined, although we
1848 actually going to define an indirect symbol. */
1849 type_change_ok = FALSE;
1850 size_change_ok = FALSE;
6e33951e 1851 matched = TRUE;
ffd65175
AM
1852 tmp_sec = sec;
1853 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1854 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1855 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1856 return FALSE;
1857
1858 if (skip)
1859 goto nondefault;
1860
5b677558
AM
1861 if (hi->def_regular)
1862 {
1863 /* If the undecorated symbol will have a version added by a
1864 script different to H, then don't indirect to/from the
1865 undecorated symbol. This isn't ideal because we may not yet
1866 have seen symbol versions, if given by a script on the
1867 command line rather than via --version-script. */
1868 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1869 {
1870 bfd_boolean hide;
1871
1872 hi->verinfo.vertree
1873 = bfd_find_version_for_sym (info->version_info,
1874 hi->root.root.string, &hide);
1875 if (hi->verinfo.vertree != NULL && hide)
1876 {
1877 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1878 goto nondefault;
1879 }
1880 }
1881 if (hi->verinfo.vertree != NULL
1882 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1883 goto nondefault;
1884 }
1885
45d6a902
AM
1886 if (! override)
1887 {
c6e8a9a8 1888 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1889 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1890 {
1891 bh = &hi->root;
1892 if (! (_bfd_generic_link_add_one_symbol
1893 (info, abfd, shortname, BSF_INDIRECT,
1894 bfd_ind_section_ptr,
1895 0, name, FALSE, collect, &bh)))
1896 return FALSE;
1897 hi = (struct elf_link_hash_entry *) bh;
1898 }
45d6a902
AM
1899 }
1900 else
1901 {
1902 /* In this case the symbol named SHORTNAME is overriding the
1903 indirect symbol we want to add. We were planning on making
1904 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1905 is the name without a version. NAME is the fully versioned
1906 name, and it is the default version.
1907
1908 Overriding means that we already saw a definition for the
1909 symbol SHORTNAME in a regular object, and it is overriding
1910 the symbol defined in the dynamic object.
1911
1912 When this happens, we actually want to change NAME, the
1913 symbol we just added, to refer to SHORTNAME. This will cause
1914 references to NAME in the shared object to become references
1915 to SHORTNAME in the regular object. This is what we expect
1916 when we override a function in a shared object: that the
1917 references in the shared object will be mapped to the
1918 definition in the regular object. */
1919
1920 while (hi->root.type == bfd_link_hash_indirect
1921 || hi->root.type == bfd_link_hash_warning)
1922 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1923
1924 h->root.type = bfd_link_hash_indirect;
1925 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1926 if (h->def_dynamic)
45d6a902 1927 {
f5385ebf
AM
1928 h->def_dynamic = 0;
1929 hi->ref_dynamic = 1;
1930 if (hi->ref_regular
1931 || hi->def_regular)
45d6a902 1932 {
c152c796 1933 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1934 return FALSE;
1935 }
1936 }
1937
1938 /* Now set HI to H, so that the following code will set the
1939 other fields correctly. */
1940 hi = h;
1941 }
1942
fab4a87f
L
1943 /* Check if HI is a warning symbol. */
1944 if (hi->root.type == bfd_link_hash_warning)
1945 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1946
45d6a902
AM
1947 /* If there is a duplicate definition somewhere, then HI may not
1948 point to an indirect symbol. We will have reported an error to
1949 the user in that case. */
1950
1951 if (hi->root.type == bfd_link_hash_indirect)
1952 {
1953 struct elf_link_hash_entry *ht;
1954
45d6a902 1955 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1956 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1957
68c88cd4
AM
1958 /* A reference to the SHORTNAME symbol from a dynamic library
1959 will be satisfied by the versioned symbol at runtime. In
1960 effect, we have a reference to the versioned symbol. */
1961 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1962 hi->dynamic_def |= ht->dynamic_def;
1963
45d6a902
AM
1964 /* See if the new flags lead us to realize that the symbol must
1965 be dynamic. */
1966 if (! *dynsym)
1967 {
1968 if (! dynamic)
1969 {
0e1862bb 1970 if (! bfd_link_executable (info)
90c984fc 1971 || hi->def_dynamic
f5385ebf 1972 || hi->ref_dynamic)
45d6a902
AM
1973 *dynsym = TRUE;
1974 }
1975 else
1976 {
f5385ebf 1977 if (hi->ref_regular)
45d6a902
AM
1978 *dynsym = TRUE;
1979 }
1980 }
1981 }
1982
1983 /* We also need to define an indirection from the nondefault version
1984 of the symbol. */
1985
1986nondefault:
1987 len = strlen (name);
a50b1753 1988 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1989 if (shortname == NULL)
1990 return FALSE;
1991 memcpy (shortname, name, shortlen);
1992 memcpy (shortname + shortlen, p + 1, len - shortlen);
1993
1994 /* Once again, merge with any existing symbol. */
1995 type_change_ok = FALSE;
1996 size_change_ok = FALSE;
ffd65175
AM
1997 tmp_sec = sec;
1998 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1999 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2000 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2001 return FALSE;
2002
2003 if (skip)
2004 return TRUE;
2005
2006 if (override)
2007 {
2008 /* Here SHORTNAME is a versioned name, so we don't expect to see
2009 the type of override we do in the case above unless it is
4cc11e76 2010 overridden by a versioned definition. */
45d6a902
AM
2011 if (hi->root.type != bfd_link_hash_defined
2012 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2013 _bfd_error_handler
695344c0 2014 /* xgettext:c-format */
d003868e
AM
2015 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2016 abfd, shortname);
45d6a902
AM
2017 }
2018 else
2019 {
2020 bh = &hi->root;
2021 if (! (_bfd_generic_link_add_one_symbol
2022 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2023 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2024 return FALSE;
2025 hi = (struct elf_link_hash_entry *) bh;
2026
2027 /* If there is a duplicate definition somewhere, then HI may not
2028 point to an indirect symbol. We will have reported an error
2029 to the user in that case. */
2030
2031 if (hi->root.type == bfd_link_hash_indirect)
2032 {
fcfa13d2 2033 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2034 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2035 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2036
2037 /* See if the new flags lead us to realize that the symbol
2038 must be dynamic. */
2039 if (! *dynsym)
2040 {
2041 if (! dynamic)
2042 {
0e1862bb 2043 if (! bfd_link_executable (info)
f5385ebf 2044 || hi->ref_dynamic)
45d6a902
AM
2045 *dynsym = TRUE;
2046 }
2047 else
2048 {
f5385ebf 2049 if (hi->ref_regular)
45d6a902
AM
2050 *dynsym = TRUE;
2051 }
2052 }
2053 }
2054 }
2055
2056 return TRUE;
2057}
2058\f
2059/* This routine is used to export all defined symbols into the dynamic
2060 symbol table. It is called via elf_link_hash_traverse. */
2061
28caa186 2062static bfd_boolean
268b6b39 2063_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2064{
a50b1753 2065 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2066
2067 /* Ignore indirect symbols. These are added by the versioning code. */
2068 if (h->root.type == bfd_link_hash_indirect)
2069 return TRUE;
2070
7686d77d
AM
2071 /* Ignore this if we won't export it. */
2072 if (!eif->info->export_dynamic && !h->dynamic)
2073 return TRUE;
45d6a902
AM
2074
2075 if (h->dynindx == -1
fd91d419
L
2076 && (h->def_regular || h->ref_regular)
2077 && ! bfd_hide_sym_by_version (eif->info->version_info,
2078 h->root.root.string))
45d6a902 2079 {
fd91d419 2080 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2081 {
fd91d419
L
2082 eif->failed = TRUE;
2083 return FALSE;
45d6a902
AM
2084 }
2085 }
2086
2087 return TRUE;
2088}
2089\f
2090/* Look through the symbols which are defined in other shared
2091 libraries and referenced here. Update the list of version
2092 dependencies. This will be put into the .gnu.version_r section.
2093 This function is called via elf_link_hash_traverse. */
2094
28caa186 2095static bfd_boolean
268b6b39
AM
2096_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2097 void *data)
45d6a902 2098{
a50b1753 2099 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2100 Elf_Internal_Verneed *t;
2101 Elf_Internal_Vernaux *a;
2102 bfd_size_type amt;
2103
45d6a902
AM
2104 /* We only care about symbols defined in shared objects with version
2105 information. */
f5385ebf
AM
2106 if (!h->def_dynamic
2107 || h->def_regular
45d6a902 2108 || h->dynindx == -1
7b20f099
AM
2109 || h->verinfo.verdef == NULL
2110 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2111 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2112 return TRUE;
2113
2114 /* See if we already know about this version. */
28caa186
AM
2115 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2116 t != NULL;
2117 t = t->vn_nextref)
45d6a902
AM
2118 {
2119 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2120 continue;
2121
2122 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2123 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2124 return TRUE;
2125
2126 break;
2127 }
2128
2129 /* This is a new version. Add it to tree we are building. */
2130
2131 if (t == NULL)
2132 {
2133 amt = sizeof *t;
a50b1753 2134 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2135 if (t == NULL)
2136 {
2137 rinfo->failed = TRUE;
2138 return FALSE;
2139 }
2140
2141 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2142 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2143 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2144 }
2145
2146 amt = sizeof *a;
a50b1753 2147 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2148 if (a == NULL)
2149 {
2150 rinfo->failed = TRUE;
2151 return FALSE;
2152 }
45d6a902
AM
2153
2154 /* Note that we are copying a string pointer here, and testing it
2155 above. If bfd_elf_string_from_elf_section is ever changed to
2156 discard the string data when low in memory, this will have to be
2157 fixed. */
2158 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2159
2160 a->vna_flags = h->verinfo.verdef->vd_flags;
2161 a->vna_nextptr = t->vn_auxptr;
2162
2163 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2164 ++rinfo->vers;
2165
2166 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2167
2168 t->vn_auxptr = a;
2169
2170 return TRUE;
2171}
2172
2173/* Figure out appropriate versions for all the symbols. We may not
2174 have the version number script until we have read all of the input
2175 files, so until that point we don't know which symbols should be
2176 local. This function is called via elf_link_hash_traverse. */
2177
28caa186 2178static bfd_boolean
268b6b39 2179_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2180{
28caa186 2181 struct elf_info_failed *sinfo;
45d6a902 2182 struct bfd_link_info *info;
9c5bfbb7 2183 const struct elf_backend_data *bed;
45d6a902
AM
2184 struct elf_info_failed eif;
2185 char *p;
45d6a902 2186
a50b1753 2187 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2188 info = sinfo->info;
2189
45d6a902
AM
2190 /* Fix the symbol flags. */
2191 eif.failed = FALSE;
2192 eif.info = info;
2193 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2194 {
2195 if (eif.failed)
2196 sinfo->failed = TRUE;
2197 return FALSE;
2198 }
2199
2200 /* We only need version numbers for symbols defined in regular
2201 objects. */
f5385ebf 2202 if (!h->def_regular)
45d6a902
AM
2203 return TRUE;
2204
28caa186 2205 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2206 p = strchr (h->root.root.string, ELF_VER_CHR);
2207 if (p != NULL && h->verinfo.vertree == NULL)
2208 {
2209 struct bfd_elf_version_tree *t;
45d6a902 2210
45d6a902
AM
2211 ++p;
2212 if (*p == ELF_VER_CHR)
6e33951e 2213 ++p;
45d6a902
AM
2214
2215 /* If there is no version string, we can just return out. */
2216 if (*p == '\0')
6e33951e 2217 return TRUE;
45d6a902
AM
2218
2219 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2220 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2221 {
2222 if (strcmp (t->name, p) == 0)
2223 {
2224 size_t len;
2225 char *alc;
2226 struct bfd_elf_version_expr *d;
2227
2228 len = p - h->root.root.string;
a50b1753 2229 alc = (char *) bfd_malloc (len);
45d6a902 2230 if (alc == NULL)
14b1c01e
AM
2231 {
2232 sinfo->failed = TRUE;
2233 return FALSE;
2234 }
45d6a902
AM
2235 memcpy (alc, h->root.root.string, len - 1);
2236 alc[len - 1] = '\0';
2237 if (alc[len - 2] == ELF_VER_CHR)
2238 alc[len - 2] = '\0';
2239
2240 h->verinfo.vertree = t;
2241 t->used = TRUE;
2242 d = NULL;
2243
108ba305
JJ
2244 if (t->globals.list != NULL)
2245 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2246
2247 /* See if there is anything to force this symbol to
2248 local scope. */
108ba305 2249 if (d == NULL && t->locals.list != NULL)
45d6a902 2250 {
108ba305
JJ
2251 d = (*t->match) (&t->locals, NULL, alc);
2252 if (d != NULL
2253 && h->dynindx != -1
108ba305
JJ
2254 && ! info->export_dynamic)
2255 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2256 }
2257
2258 free (alc);
2259 break;
2260 }
2261 }
2262
2263 /* If we are building an application, we need to create a
2264 version node for this version. */
0e1862bb 2265 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2266 {
2267 struct bfd_elf_version_tree **pp;
2268 int version_index;
2269
2270 /* If we aren't going to export this symbol, we don't need
2271 to worry about it. */
2272 if (h->dynindx == -1)
2273 return TRUE;
2274
ef53be89
AM
2275 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2276 sizeof *t);
45d6a902
AM
2277 if (t == NULL)
2278 {
2279 sinfo->failed = TRUE;
2280 return FALSE;
2281 }
2282
45d6a902 2283 t->name = p;
45d6a902
AM
2284 t->name_indx = (unsigned int) -1;
2285 t->used = TRUE;
2286
2287 version_index = 1;
2288 /* Don't count anonymous version tag. */
fd91d419
L
2289 if (sinfo->info->version_info != NULL
2290 && sinfo->info->version_info->vernum == 0)
45d6a902 2291 version_index = 0;
fd91d419
L
2292 for (pp = &sinfo->info->version_info;
2293 *pp != NULL;
2294 pp = &(*pp)->next)
45d6a902
AM
2295 ++version_index;
2296 t->vernum = version_index;
2297
2298 *pp = t;
2299
2300 h->verinfo.vertree = t;
2301 }
2302 else if (t == NULL)
2303 {
2304 /* We could not find the version for a symbol when
2305 generating a shared archive. Return an error. */
4eca0228 2306 _bfd_error_handler
695344c0 2307 /* xgettext:c-format */
c55fe096 2308 (_("%B: version node not found for symbol %s"),
28caa186 2309 info->output_bfd, h->root.root.string);
45d6a902
AM
2310 bfd_set_error (bfd_error_bad_value);
2311 sinfo->failed = TRUE;
2312 return FALSE;
2313 }
45d6a902
AM
2314 }
2315
2316 /* If we don't have a version for this symbol, see if we can find
2317 something. */
fd91d419 2318 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2319 {
1e8fa21e 2320 bfd_boolean hide;
ae5a3597 2321
fd91d419
L
2322 h->verinfo.vertree
2323 = bfd_find_version_for_sym (sinfo->info->version_info,
2324 h->root.root.string, &hide);
1e8fa21e
AM
2325 if (h->verinfo.vertree != NULL && hide)
2326 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2327 }
2328
2329 return TRUE;
2330}
2331\f
45d6a902
AM
2332/* Read and swap the relocs from the section indicated by SHDR. This
2333 may be either a REL or a RELA section. The relocations are
2334 translated into RELA relocations and stored in INTERNAL_RELOCS,
2335 which should have already been allocated to contain enough space.
2336 The EXTERNAL_RELOCS are a buffer where the external form of the
2337 relocations should be stored.
2338
2339 Returns FALSE if something goes wrong. */
2340
2341static bfd_boolean
268b6b39 2342elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2343 asection *sec,
268b6b39
AM
2344 Elf_Internal_Shdr *shdr,
2345 void *external_relocs,
2346 Elf_Internal_Rela *internal_relocs)
45d6a902 2347{
9c5bfbb7 2348 const struct elf_backend_data *bed;
268b6b39 2349 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2350 const bfd_byte *erela;
2351 const bfd_byte *erelaend;
2352 Elf_Internal_Rela *irela;
243ef1e0
L
2353 Elf_Internal_Shdr *symtab_hdr;
2354 size_t nsyms;
45d6a902 2355
45d6a902
AM
2356 /* Position ourselves at the start of the section. */
2357 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2358 return FALSE;
2359
2360 /* Read the relocations. */
2361 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2362 return FALSE;
2363
243ef1e0 2364 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2365 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2366
45d6a902
AM
2367 bed = get_elf_backend_data (abfd);
2368
2369 /* Convert the external relocations to the internal format. */
2370 if (shdr->sh_entsize == bed->s->sizeof_rel)
2371 swap_in = bed->s->swap_reloc_in;
2372 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2373 swap_in = bed->s->swap_reloca_in;
2374 else
2375 {
2376 bfd_set_error (bfd_error_wrong_format);
2377 return FALSE;
2378 }
2379
a50b1753 2380 erela = (const bfd_byte *) external_relocs;
51992aec 2381 erelaend = erela + shdr->sh_size;
45d6a902
AM
2382 irela = internal_relocs;
2383 while (erela < erelaend)
2384 {
243ef1e0
L
2385 bfd_vma r_symndx;
2386
45d6a902 2387 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2388 r_symndx = ELF32_R_SYM (irela->r_info);
2389 if (bed->s->arch_size == 64)
2390 r_symndx >>= 24;
ce98a316
NC
2391 if (nsyms > 0)
2392 {
2393 if ((size_t) r_symndx >= nsyms)
2394 {
4eca0228 2395 _bfd_error_handler
695344c0 2396 /* xgettext:c-format */
d42c267e 2397 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
76cfced5 2398 " for offset %#Lx in section `%A'"),
d42c267e 2399 abfd, r_symndx, (unsigned long) nsyms,
c08bb8dd 2400 irela->r_offset, sec);
ce98a316
NC
2401 bfd_set_error (bfd_error_bad_value);
2402 return FALSE;
2403 }
2404 }
cf35638d 2405 else if (r_symndx != STN_UNDEF)
243ef1e0 2406 {
4eca0228 2407 _bfd_error_handler
695344c0 2408 /* xgettext:c-format */
d42c267e 2409 (_("%B: non-zero symbol index (%#Lx)"
76cfced5 2410 " for offset %#Lx in section `%A'"
ce98a316 2411 " when the object file has no symbol table"),
d42c267e 2412 abfd, r_symndx,
c08bb8dd 2413 irela->r_offset, sec);
243ef1e0
L
2414 bfd_set_error (bfd_error_bad_value);
2415 return FALSE;
2416 }
45d6a902
AM
2417 irela += bed->s->int_rels_per_ext_rel;
2418 erela += shdr->sh_entsize;
2419 }
2420
2421 return TRUE;
2422}
2423
2424/* Read and swap the relocs for a section O. They may have been
2425 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2426 not NULL, they are used as buffers to read into. They are known to
2427 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2428 the return value is allocated using either malloc or bfd_alloc,
2429 according to the KEEP_MEMORY argument. If O has two relocation
2430 sections (both REL and RELA relocations), then the REL_HDR
2431 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2432 RELA_HDR relocations. */
45d6a902
AM
2433
2434Elf_Internal_Rela *
268b6b39
AM
2435_bfd_elf_link_read_relocs (bfd *abfd,
2436 asection *o,
2437 void *external_relocs,
2438 Elf_Internal_Rela *internal_relocs,
2439 bfd_boolean keep_memory)
45d6a902 2440{
268b6b39 2441 void *alloc1 = NULL;
45d6a902 2442 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2443 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2444 struct bfd_elf_section_data *esdo = elf_section_data (o);
2445 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2446
d4730f92
BS
2447 if (esdo->relocs != NULL)
2448 return esdo->relocs;
45d6a902
AM
2449
2450 if (o->reloc_count == 0)
2451 return NULL;
2452
45d6a902
AM
2453 if (internal_relocs == NULL)
2454 {
2455 bfd_size_type size;
2456
056bafd4 2457 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2458 if (keep_memory)
a50b1753 2459 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2460 else
a50b1753 2461 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2462 if (internal_relocs == NULL)
2463 goto error_return;
2464 }
2465
2466 if (external_relocs == NULL)
2467 {
d4730f92
BS
2468 bfd_size_type size = 0;
2469
2470 if (esdo->rel.hdr)
2471 size += esdo->rel.hdr->sh_size;
2472 if (esdo->rela.hdr)
2473 size += esdo->rela.hdr->sh_size;
45d6a902 2474
268b6b39 2475 alloc1 = bfd_malloc (size);
45d6a902
AM
2476 if (alloc1 == NULL)
2477 goto error_return;
2478 external_relocs = alloc1;
2479 }
2480
d4730f92
BS
2481 internal_rela_relocs = internal_relocs;
2482 if (esdo->rel.hdr)
2483 {
2484 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2485 external_relocs,
2486 internal_relocs))
2487 goto error_return;
2488 external_relocs = (((bfd_byte *) external_relocs)
2489 + esdo->rel.hdr->sh_size);
2490 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2491 * bed->s->int_rels_per_ext_rel);
2492 }
2493
2494 if (esdo->rela.hdr
2495 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2496 external_relocs,
2497 internal_rela_relocs)))
45d6a902
AM
2498 goto error_return;
2499
2500 /* Cache the results for next time, if we can. */
2501 if (keep_memory)
d4730f92 2502 esdo->relocs = internal_relocs;
45d6a902
AM
2503
2504 if (alloc1 != NULL)
2505 free (alloc1);
2506
2507 /* Don't free alloc2, since if it was allocated we are passing it
2508 back (under the name of internal_relocs). */
2509
2510 return internal_relocs;
2511
2512 error_return:
2513 if (alloc1 != NULL)
2514 free (alloc1);
2515 if (alloc2 != NULL)
4dd07732
AM
2516 {
2517 if (keep_memory)
2518 bfd_release (abfd, alloc2);
2519 else
2520 free (alloc2);
2521 }
45d6a902
AM
2522 return NULL;
2523}
2524
2525/* Compute the size of, and allocate space for, REL_HDR which is the
2526 section header for a section containing relocations for O. */
2527
28caa186 2528static bfd_boolean
9eaff861
AO
2529_bfd_elf_link_size_reloc_section (bfd *abfd,
2530 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2531{
9eaff861 2532 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2533
2534 /* That allows us to calculate the size of the section. */
9eaff861 2535 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2536
2537 /* The contents field must last into write_object_contents, so we
2538 allocate it with bfd_alloc rather than malloc. Also since we
2539 cannot be sure that the contents will actually be filled in,
2540 we zero the allocated space. */
a50b1753 2541 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2542 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2543 return FALSE;
2544
d4730f92 2545 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2546 {
2547 struct elf_link_hash_entry **p;
2548
ca4be51c
AM
2549 p = ((struct elf_link_hash_entry **)
2550 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2551 if (p == NULL)
2552 return FALSE;
2553
d4730f92 2554 reldata->hashes = p;
45d6a902
AM
2555 }
2556
2557 return TRUE;
2558}
2559
2560/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2561 originated from the section given by INPUT_REL_HDR) to the
2562 OUTPUT_BFD. */
2563
2564bfd_boolean
268b6b39
AM
2565_bfd_elf_link_output_relocs (bfd *output_bfd,
2566 asection *input_section,
2567 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2568 Elf_Internal_Rela *internal_relocs,
2569 struct elf_link_hash_entry **rel_hash
2570 ATTRIBUTE_UNUSED)
45d6a902
AM
2571{
2572 Elf_Internal_Rela *irela;
2573 Elf_Internal_Rela *irelaend;
2574 bfd_byte *erel;
d4730f92 2575 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2576 asection *output_section;
9c5bfbb7 2577 const struct elf_backend_data *bed;
268b6b39 2578 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2579 struct bfd_elf_section_data *esdo;
45d6a902
AM
2580
2581 output_section = input_section->output_section;
45d6a902 2582
d4730f92
BS
2583 bed = get_elf_backend_data (output_bfd);
2584 esdo = elf_section_data (output_section);
2585 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2586 {
d4730f92
BS
2587 output_reldata = &esdo->rel;
2588 swap_out = bed->s->swap_reloc_out;
45d6a902 2589 }
d4730f92
BS
2590 else if (esdo->rela.hdr
2591 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2592 {
d4730f92
BS
2593 output_reldata = &esdo->rela;
2594 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2595 }
2596 else
2597 {
4eca0228 2598 _bfd_error_handler
695344c0 2599 /* xgettext:c-format */
d003868e
AM
2600 (_("%B: relocation size mismatch in %B section %A"),
2601 output_bfd, input_section->owner, input_section);
297d8443 2602 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2603 return FALSE;
2604 }
2605
d4730f92
BS
2606 erel = output_reldata->hdr->contents;
2607 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2608 irela = internal_relocs;
2609 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2610 * bed->s->int_rels_per_ext_rel);
2611 while (irela < irelaend)
2612 {
2613 (*swap_out) (output_bfd, irela, erel);
2614 irela += bed->s->int_rels_per_ext_rel;
2615 erel += input_rel_hdr->sh_entsize;
2616 }
2617
2618 /* Bump the counter, so that we know where to add the next set of
2619 relocations. */
d4730f92 2620 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2621
2622 return TRUE;
2623}
2624\f
508c3946
L
2625/* Make weak undefined symbols in PIE dynamic. */
2626
2627bfd_boolean
2628_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2629 struct elf_link_hash_entry *h)
2630{
0e1862bb 2631 if (bfd_link_pie (info)
508c3946
L
2632 && h->dynindx == -1
2633 && h->root.type == bfd_link_hash_undefweak)
2634 return bfd_elf_link_record_dynamic_symbol (info, h);
2635
2636 return TRUE;
2637}
2638
45d6a902
AM
2639/* Fix up the flags for a symbol. This handles various cases which
2640 can only be fixed after all the input files are seen. This is
2641 currently called by both adjust_dynamic_symbol and
2642 assign_sym_version, which is unnecessary but perhaps more robust in
2643 the face of future changes. */
2644
28caa186 2645static bfd_boolean
268b6b39
AM
2646_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2647 struct elf_info_failed *eif)
45d6a902 2648{
33774f08 2649 const struct elf_backend_data *bed;
508c3946 2650
45d6a902
AM
2651 /* If this symbol was mentioned in a non-ELF file, try to set
2652 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2653 permit a non-ELF file to correctly refer to a symbol defined in
2654 an ELF dynamic object. */
f5385ebf 2655 if (h->non_elf)
45d6a902
AM
2656 {
2657 while (h->root.type == bfd_link_hash_indirect)
2658 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2659
2660 if (h->root.type != bfd_link_hash_defined
2661 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2662 {
2663 h->ref_regular = 1;
2664 h->ref_regular_nonweak = 1;
2665 }
45d6a902
AM
2666 else
2667 {
2668 if (h->root.u.def.section->owner != NULL
2669 && (bfd_get_flavour (h->root.u.def.section->owner)
2670 == bfd_target_elf_flavour))
f5385ebf
AM
2671 {
2672 h->ref_regular = 1;
2673 h->ref_regular_nonweak = 1;
2674 }
45d6a902 2675 else
f5385ebf 2676 h->def_regular = 1;
45d6a902
AM
2677 }
2678
2679 if (h->dynindx == -1
f5385ebf
AM
2680 && (h->def_dynamic
2681 || h->ref_dynamic))
45d6a902 2682 {
c152c796 2683 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2684 {
2685 eif->failed = TRUE;
2686 return FALSE;
2687 }
2688 }
2689 }
2690 else
2691 {
f5385ebf 2692 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2693 was first seen in a non-ELF file. Fortunately, if the symbol
2694 was first seen in an ELF file, we're probably OK unless the
2695 symbol was defined in a non-ELF file. Catch that case here.
2696 FIXME: We're still in trouble if the symbol was first seen in
2697 a dynamic object, and then later in a non-ELF regular object. */
2698 if ((h->root.type == bfd_link_hash_defined
2699 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2700 && !h->def_regular
45d6a902
AM
2701 && (h->root.u.def.section->owner != NULL
2702 ? (bfd_get_flavour (h->root.u.def.section->owner)
2703 != bfd_target_elf_flavour)
2704 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2705 && !h->def_dynamic)))
2706 h->def_regular = 1;
45d6a902
AM
2707 }
2708
508c3946 2709 /* Backend specific symbol fixup. */
33774f08
AM
2710 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2711 if (bed->elf_backend_fixup_symbol
2712 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2713 return FALSE;
508c3946 2714
45d6a902
AM
2715 /* If this is a final link, and the symbol was defined as a common
2716 symbol in a regular object file, and there was no definition in
2717 any dynamic object, then the linker will have allocated space for
f5385ebf 2718 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2719 flag will not have been set. */
2720 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2721 && !h->def_regular
2722 && h->ref_regular
2723 && !h->def_dynamic
96f29d96 2724 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2725 h->def_regular = 1;
45d6a902 2726
4deb8f71
L
2727 /* If a weak undefined symbol has non-default visibility, we also
2728 hide it from the dynamic linker. */
2729 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2730 && h->root.type == bfd_link_hash_undefweak)
2731 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2732
2733 /* A hidden versioned symbol in executable should be forced local if
2734 it is is locally defined, not referenced by shared library and not
2735 exported. */
2736 else if (bfd_link_executable (eif->info)
2737 && h->versioned == versioned_hidden
2738 && !eif->info->export_dynamic
2739 && !h->dynamic
2740 && !h->ref_dynamic
2741 && h->def_regular)
2742 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2743
45d6a902
AM
2744 /* If -Bsymbolic was used (which means to bind references to global
2745 symbols to the definition within the shared object), and this
2746 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2747 need a PLT entry. Likewise, if the symbol has non-default
2748 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2749 will force it local. */
4deb8f71
L
2750 else if (h->needs_plt
2751 && bfd_link_pic (eif->info)
2752 && is_elf_hash_table (eif->info->hash)
2753 && (SYMBOLIC_BIND (eif->info, h)
2754 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2755 && h->def_regular)
45d6a902 2756 {
45d6a902
AM
2757 bfd_boolean force_local;
2758
45d6a902
AM
2759 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2760 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2761 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2762 }
2763
45d6a902
AM
2764 /* If this is a weak defined symbol in a dynamic object, and we know
2765 the real definition in the dynamic object, copy interesting flags
2766 over to the real definition. */
f6e332e6 2767 if (h->u.weakdef != NULL)
45d6a902 2768 {
45d6a902
AM
2769 /* If the real definition is defined by a regular object file,
2770 don't do anything special. See the longer description in
2771 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2772 if (h->u.weakdef->def_regular)
f6e332e6 2773 h->u.weakdef = NULL;
45d6a902 2774 else
a26587ba 2775 {
4e6b54a6
AM
2776 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2777
2778 while (h->root.type == bfd_link_hash_indirect)
2779 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2780
2781 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2782 || h->root.type == bfd_link_hash_defweak);
2783 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2784 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2785 || weakdef->root.type == bfd_link_hash_defweak);
2786 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2787 }
45d6a902
AM
2788 }
2789
2790 return TRUE;
2791}
2792
2793/* Make the backend pick a good value for a dynamic symbol. This is
2794 called via elf_link_hash_traverse, and also calls itself
2795 recursively. */
2796
28caa186 2797static bfd_boolean
268b6b39 2798_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2799{
a50b1753 2800 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2801 bfd *dynobj;
9c5bfbb7 2802 const struct elf_backend_data *bed;
45d6a902 2803
0eddce27 2804 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2805 return FALSE;
2806
45d6a902
AM
2807 /* Ignore indirect symbols. These are added by the versioning code. */
2808 if (h->root.type == bfd_link_hash_indirect)
2809 return TRUE;
2810
2811 /* Fix the symbol flags. */
2812 if (! _bfd_elf_fix_symbol_flags (h, eif))
2813 return FALSE;
2814
954b63d4
AM
2815 if (h->root.type == bfd_link_hash_undefweak)
2816 {
2817 if (eif->info->dynamic_undefined_weak == 0)
2818 _bfd_elf_link_hash_hide_symbol (eif->info, h, TRUE);
2819 else if (eif->info->dynamic_undefined_weak > 0
2820 && h->ref_regular
2821 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2822 && !bfd_hide_sym_by_version (eif->info->version_info,
2823 h->root.root.string))
2824 {
2825 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2826 {
2827 eif->failed = TRUE;
2828 return FALSE;
2829 }
2830 }
2831 }
2832
45d6a902
AM
2833 /* If this symbol does not require a PLT entry, and it is not
2834 defined by a dynamic object, or is not referenced by a regular
2835 object, ignore it. We do have to handle a weak defined symbol,
2836 even if no regular object refers to it, if we decided to add it
2837 to the dynamic symbol table. FIXME: Do we normally need to worry
2838 about symbols which are defined by one dynamic object and
2839 referenced by another one? */
f5385ebf 2840 if (!h->needs_plt
91e21fb7 2841 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2842 && (h->def_regular
2843 || !h->def_dynamic
2844 || (!h->ref_regular
f6e332e6 2845 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2846 {
a6aa5195 2847 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2848 return TRUE;
2849 }
2850
2851 /* If we've already adjusted this symbol, don't do it again. This
2852 can happen via a recursive call. */
f5385ebf 2853 if (h->dynamic_adjusted)
45d6a902
AM
2854 return TRUE;
2855
2856 /* Don't look at this symbol again. Note that we must set this
2857 after checking the above conditions, because we may look at a
2858 symbol once, decide not to do anything, and then get called
2859 recursively later after REF_REGULAR is set below. */
f5385ebf 2860 h->dynamic_adjusted = 1;
45d6a902
AM
2861
2862 /* If this is a weak definition, and we know a real definition, and
2863 the real symbol is not itself defined by a regular object file,
2864 then get a good value for the real definition. We handle the
2865 real symbol first, for the convenience of the backend routine.
2866
2867 Note that there is a confusing case here. If the real definition
2868 is defined by a regular object file, we don't get the real symbol
2869 from the dynamic object, but we do get the weak symbol. If the
2870 processor backend uses a COPY reloc, then if some routine in the
2871 dynamic object changes the real symbol, we will not see that
2872 change in the corresponding weak symbol. This is the way other
2873 ELF linkers work as well, and seems to be a result of the shared
2874 library model.
2875
2876 I will clarify this issue. Most SVR4 shared libraries define the
2877 variable _timezone and define timezone as a weak synonym. The
2878 tzset call changes _timezone. If you write
2879 extern int timezone;
2880 int _timezone = 5;
2881 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2882 you might expect that, since timezone is a synonym for _timezone,
2883 the same number will print both times. However, if the processor
2884 backend uses a COPY reloc, then actually timezone will be copied
2885 into your process image, and, since you define _timezone
2886 yourself, _timezone will not. Thus timezone and _timezone will
2887 wind up at different memory locations. The tzset call will set
2888 _timezone, leaving timezone unchanged. */
2889
f6e332e6 2890 if (h->u.weakdef != NULL)
45d6a902 2891 {
ec24dc88
AM
2892 /* If we get to this point, there is an implicit reference to
2893 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2894 h->u.weakdef->ref_regular = 1;
45d6a902 2895
ec24dc88
AM
2896 /* Ensure that the backend adjust_dynamic_symbol function sees
2897 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2898 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2899 return FALSE;
2900 }
2901
2902 /* If a symbol has no type and no size and does not require a PLT
2903 entry, then we are probably about to do the wrong thing here: we
2904 are probably going to create a COPY reloc for an empty object.
2905 This case can arise when a shared object is built with assembly
2906 code, and the assembly code fails to set the symbol type. */
2907 if (h->size == 0
2908 && h->type == STT_NOTYPE
f5385ebf 2909 && !h->needs_plt)
4eca0228 2910 _bfd_error_handler
45d6a902
AM
2911 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2912 h->root.root.string);
2913
2914 dynobj = elf_hash_table (eif->info)->dynobj;
2915 bed = get_elf_backend_data (dynobj);
e7c33416 2916
45d6a902
AM
2917 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2918 {
2919 eif->failed = TRUE;
2920 return FALSE;
2921 }
2922
2923 return TRUE;
2924}
2925
027297b7
L
2926/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2927 DYNBSS. */
2928
2929bfd_boolean
6cabe1ea
AM
2930_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2931 struct elf_link_hash_entry *h,
027297b7
L
2932 asection *dynbss)
2933{
91ac5911 2934 unsigned int power_of_two;
027297b7
L
2935 bfd_vma mask;
2936 asection *sec = h->root.u.def.section;
2937
de194d85 2938 /* The section alignment of the definition is the maximum alignment
91ac5911
L
2939 requirement of symbols defined in the section. Since we don't
2940 know the symbol alignment requirement, we start with the
2941 maximum alignment and check low bits of the symbol address
2942 for the minimum alignment. */
2943 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2944 mask = ((bfd_vma) 1 << power_of_two) - 1;
2945 while ((h->root.u.def.value & mask) != 0)
2946 {
2947 mask >>= 1;
2948 --power_of_two;
2949 }
027297b7 2950
91ac5911
L
2951 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2952 dynbss))
027297b7
L
2953 {
2954 /* Adjust the section alignment if needed. */
2955 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2956 power_of_two))
027297b7
L
2957 return FALSE;
2958 }
2959
91ac5911 2960 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2961 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2962
2963 /* Define the symbol as being at this point in DYNBSS. */
2964 h->root.u.def.section = dynbss;
2965 h->root.u.def.value = dynbss->size;
2966
2967 /* Increment the size of DYNBSS to make room for the symbol. */
2968 dynbss->size += h->size;
2969
f7483970
L
2970 /* No error if extern_protected_data is true. */
2971 if (h->protected_def
889c2a67
L
2972 && (!info->extern_protected_data
2973 || (info->extern_protected_data < 0
2974 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2975 info->callbacks->einfo
2976 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2977 h->root.root.string);
6cabe1ea 2978
027297b7
L
2979 return TRUE;
2980}
2981
45d6a902
AM
2982/* Adjust all external symbols pointing into SEC_MERGE sections
2983 to reflect the object merging within the sections. */
2984
28caa186 2985static bfd_boolean
268b6b39 2986_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2987{
2988 asection *sec;
2989
45d6a902
AM
2990 if ((h->root.type == bfd_link_hash_defined
2991 || h->root.type == bfd_link_hash_defweak)
2992 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2993 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2994 {
a50b1753 2995 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2996
2997 h->root.u.def.value =
2998 _bfd_merged_section_offset (output_bfd,
2999 &h->root.u.def.section,
3000 elf_section_data (sec)->sec_info,
753731ee 3001 h->root.u.def.value);
45d6a902
AM
3002 }
3003
3004 return TRUE;
3005}
986a241f
RH
3006
3007/* Returns false if the symbol referred to by H should be considered
3008 to resolve local to the current module, and true if it should be
3009 considered to bind dynamically. */
3010
3011bfd_boolean
268b6b39
AM
3012_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3013 struct bfd_link_info *info,
89a2ee5a 3014 bfd_boolean not_local_protected)
986a241f
RH
3015{
3016 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3017 const struct elf_backend_data *bed;
3018 struct elf_link_hash_table *hash_table;
986a241f
RH
3019
3020 if (h == NULL)
3021 return FALSE;
3022
3023 while (h->root.type == bfd_link_hash_indirect
3024 || h->root.type == bfd_link_hash_warning)
3025 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3026
3027 /* If it was forced local, then clearly it's not dynamic. */
3028 if (h->dynindx == -1)
3029 return FALSE;
f5385ebf 3030 if (h->forced_local)
986a241f
RH
3031 return FALSE;
3032
3033 /* Identify the cases where name binding rules say that a
3034 visible symbol resolves locally. */
0e1862bb
L
3035 binding_stays_local_p = (bfd_link_executable (info)
3036 || SYMBOLIC_BIND (info, h));
986a241f
RH
3037
3038 switch (ELF_ST_VISIBILITY (h->other))
3039 {
3040 case STV_INTERNAL:
3041 case STV_HIDDEN:
3042 return FALSE;
3043
3044 case STV_PROTECTED:
fcb93ecf
PB
3045 hash_table = elf_hash_table (info);
3046 if (!is_elf_hash_table (hash_table))
3047 return FALSE;
3048
3049 bed = get_elf_backend_data (hash_table->dynobj);
3050
986a241f
RH
3051 /* Proper resolution for function pointer equality may require
3052 that these symbols perhaps be resolved dynamically, even though
3053 we should be resolving them to the current module. */
89a2ee5a 3054 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3055 binding_stays_local_p = TRUE;
3056 break;
3057
3058 default:
986a241f
RH
3059 break;
3060 }
3061
aa37626c 3062 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3063 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3064 return TRUE;
3065
986a241f
RH
3066 /* Otherwise, the symbol is dynamic if binding rules don't tell
3067 us that it remains local. */
3068 return !binding_stays_local_p;
3069}
f6c52c13
AM
3070
3071/* Return true if the symbol referred to by H should be considered
3072 to resolve local to the current module, and false otherwise. Differs
3073 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3074 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3075 for the place where dynindx == -1 is tested. If that test is true,
3076 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3077 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3078 defined symbols.
89a2ee5a
AM
3079 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3080 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3081 treatment of undefined weak symbols. For those that do not make
3082 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3083
3084bfd_boolean
268b6b39
AM
3085_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3086 struct bfd_link_info *info,
3087 bfd_boolean local_protected)
f6c52c13 3088{
fcb93ecf
PB
3089 const struct elf_backend_data *bed;
3090 struct elf_link_hash_table *hash_table;
3091
f6c52c13
AM
3092 /* If it's a local sym, of course we resolve locally. */
3093 if (h == NULL)
3094 return TRUE;
3095
d95edcac
L
3096 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3097 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3098 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3099 return TRUE;
3100
0fad2956
MR
3101 /* Forced local symbols resolve locally. */
3102 if (h->forced_local)
3103 return TRUE;
3104
7e2294f9
AO
3105 /* Common symbols that become definitions don't get the DEF_REGULAR
3106 flag set, so test it first, and don't bail out. */
3107 if (ELF_COMMON_DEF_P (h))
3108 /* Do nothing. */;
f6c52c13 3109 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3110 resolve locally. The sym is either undefined or dynamic. */
3111 else if (!h->def_regular)
f6c52c13
AM
3112 return FALSE;
3113
0fad2956 3114 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3115 if (h->dynindx == -1)
3116 return TRUE;
3117
3118 /* At this point, we know the symbol is defined and dynamic. In an
3119 executable it must resolve locally, likewise when building symbolic
3120 shared libraries. */
0e1862bb 3121 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3122 return TRUE;
3123
3124 /* Now deal with defined dynamic symbols in shared libraries. Ones
3125 with default visibility might not resolve locally. */
3126 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3127 return FALSE;
3128
fcb93ecf
PB
3129 hash_table = elf_hash_table (info);
3130 if (!is_elf_hash_table (hash_table))
3131 return TRUE;
3132
3133 bed = get_elf_backend_data (hash_table->dynobj);
3134
f7483970
L
3135 /* If extern_protected_data is false, STV_PROTECTED non-function
3136 symbols are local. */
889c2a67
L
3137 if ((!info->extern_protected_data
3138 || (info->extern_protected_data < 0
3139 && !bed->extern_protected_data))
3140 && !bed->is_function_type (h->type))
1c16dfa5
L
3141 return TRUE;
3142
f6c52c13 3143 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3144 symbols be treated as dynamic symbols. If the address of a
3145 function not defined in an executable is set to that function's
3146 plt entry in the executable, then the address of the function in
3147 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3148 return local_protected;
3149}
e1918d23
AM
3150
3151/* Caches some TLS segment info, and ensures that the TLS segment vma is
3152 aligned. Returns the first TLS output section. */
3153
3154struct bfd_section *
3155_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3156{
3157 struct bfd_section *sec, *tls;
3158 unsigned int align = 0;
3159
3160 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3161 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3162 break;
3163 tls = sec;
3164
3165 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3166 if (sec->alignment_power > align)
3167 align = sec->alignment_power;
3168
3169 elf_hash_table (info)->tls_sec = tls;
3170
3171 /* Ensure the alignment of the first section is the largest alignment,
3172 so that the tls segment starts aligned. */
3173 if (tls != NULL)
3174 tls->alignment_power = align;
3175
3176 return tls;
3177}
0ad989f9
L
3178
3179/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3180static bfd_boolean
3181is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3182 Elf_Internal_Sym *sym)
3183{
a4d8e49b
L
3184 const struct elf_backend_data *bed;
3185
0ad989f9
L
3186 /* Local symbols do not count, but target specific ones might. */
3187 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3188 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3189 return FALSE;
3190
fcb93ecf 3191 bed = get_elf_backend_data (abfd);
0ad989f9 3192 /* Function symbols do not count. */
fcb93ecf 3193 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3194 return FALSE;
3195
3196 /* If the section is undefined, then so is the symbol. */
3197 if (sym->st_shndx == SHN_UNDEF)
3198 return FALSE;
3199
3200 /* If the symbol is defined in the common section, then
3201 it is a common definition and so does not count. */
a4d8e49b 3202 if (bed->common_definition (sym))
0ad989f9
L
3203 return FALSE;
3204
3205 /* If the symbol is in a target specific section then we
3206 must rely upon the backend to tell us what it is. */
3207 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3208 /* FIXME - this function is not coded yet:
3209
3210 return _bfd_is_global_symbol_definition (abfd, sym);
3211
3212 Instead for now assume that the definition is not global,
3213 Even if this is wrong, at least the linker will behave
3214 in the same way that it used to do. */
3215 return FALSE;
3216
3217 return TRUE;
3218}
3219
3220/* Search the symbol table of the archive element of the archive ABFD
3221 whose archive map contains a mention of SYMDEF, and determine if
3222 the symbol is defined in this element. */
3223static bfd_boolean
3224elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3225{
3226 Elf_Internal_Shdr * hdr;
ef53be89
AM
3227 size_t symcount;
3228 size_t extsymcount;
3229 size_t extsymoff;
0ad989f9
L
3230 Elf_Internal_Sym *isymbuf;
3231 Elf_Internal_Sym *isym;
3232 Elf_Internal_Sym *isymend;
3233 bfd_boolean result;
3234
3235 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3236 if (abfd == NULL)
3237 return FALSE;
3238
3239 if (! bfd_check_format (abfd, bfd_object))
3240 return FALSE;
3241
7dc3990e
L
3242 /* Select the appropriate symbol table. If we don't know if the
3243 object file is an IR object, give linker LTO plugin a chance to
3244 get the correct symbol table. */
3245 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3246#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3247 || (abfd->plugin_format == bfd_plugin_unknown
3248 && bfd_link_plugin_object_p (abfd))
3249#endif
3250 )
3251 {
3252 /* Use the IR symbol table if the object has been claimed by
3253 plugin. */
3254 abfd = abfd->plugin_dummy_bfd;
3255 hdr = &elf_tdata (abfd)->symtab_hdr;
3256 }
3257 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3258 hdr = &elf_tdata (abfd)->symtab_hdr;
3259 else
3260 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3261
3262 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3263
3264 /* The sh_info field of the symtab header tells us where the
3265 external symbols start. We don't care about the local symbols. */
3266 if (elf_bad_symtab (abfd))
3267 {
3268 extsymcount = symcount;
3269 extsymoff = 0;
3270 }
3271 else
3272 {
3273 extsymcount = symcount - hdr->sh_info;
3274 extsymoff = hdr->sh_info;
3275 }
3276
3277 if (extsymcount == 0)
3278 return FALSE;
3279
3280 /* Read in the symbol table. */
3281 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3282 NULL, NULL, NULL);
3283 if (isymbuf == NULL)
3284 return FALSE;
3285
3286 /* Scan the symbol table looking for SYMDEF. */
3287 result = FALSE;
3288 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3289 {
3290 const char *name;
3291
3292 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3293 isym->st_name);
3294 if (name == NULL)
3295 break;
3296
3297 if (strcmp (name, symdef->name) == 0)
3298 {
3299 result = is_global_data_symbol_definition (abfd, isym);
3300 break;
3301 }
3302 }
3303
3304 free (isymbuf);
3305
3306 return result;
3307}
3308\f
5a580b3a
AM
3309/* Add an entry to the .dynamic table. */
3310
3311bfd_boolean
3312_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3313 bfd_vma tag,
3314 bfd_vma val)
3315{
3316 struct elf_link_hash_table *hash_table;
3317 const struct elf_backend_data *bed;
3318 asection *s;
3319 bfd_size_type newsize;
3320 bfd_byte *newcontents;
3321 Elf_Internal_Dyn dyn;
3322
3323 hash_table = elf_hash_table (info);
3324 if (! is_elf_hash_table (hash_table))
3325 return FALSE;
3326
3327 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3328 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3329 BFD_ASSERT (s != NULL);
3330
eea6121a 3331 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3332 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3333 if (newcontents == NULL)
3334 return FALSE;
3335
3336 dyn.d_tag = tag;
3337 dyn.d_un.d_val = val;
eea6121a 3338 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3339
eea6121a 3340 s->size = newsize;
5a580b3a
AM
3341 s->contents = newcontents;
3342
3343 return TRUE;
3344}
3345
3346/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3347 otherwise just check whether one already exists. Returns -1 on error,
3348 1 if a DT_NEEDED tag already exists, and 0 on success. */
3349
4ad4eba5 3350static int
7e9f0867
AM
3351elf_add_dt_needed_tag (bfd *abfd,
3352 struct bfd_link_info *info,
4ad4eba5
AM
3353 const char *soname,
3354 bfd_boolean do_it)
5a580b3a
AM
3355{
3356 struct elf_link_hash_table *hash_table;
ef53be89 3357 size_t strindex;
5a580b3a 3358
7e9f0867
AM
3359 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3360 return -1;
3361
5a580b3a 3362 hash_table = elf_hash_table (info);
5a580b3a 3363 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3364 if (strindex == (size_t) -1)
5a580b3a
AM
3365 return -1;
3366
02be4619 3367 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3368 {
3369 asection *sdyn;
3370 const struct elf_backend_data *bed;
3371 bfd_byte *extdyn;
3372
3373 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3374 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3375 if (sdyn != NULL)
3376 for (extdyn = sdyn->contents;
3377 extdyn < sdyn->contents + sdyn->size;
3378 extdyn += bed->s->sizeof_dyn)
3379 {
3380 Elf_Internal_Dyn dyn;
5a580b3a 3381
7e9f0867
AM
3382 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3383 if (dyn.d_tag == DT_NEEDED
3384 && dyn.d_un.d_val == strindex)
3385 {
3386 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3387 return 1;
3388 }
3389 }
5a580b3a
AM
3390 }
3391
3392 if (do_it)
3393 {
7e9f0867
AM
3394 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3395 return -1;
3396
5a580b3a
AM
3397 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3398 return -1;
3399 }
3400 else
3401 /* We were just checking for existence of the tag. */
3402 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3403
3404 return 0;
3405}
3406
7b15fa7a
AM
3407/* Return true if SONAME is on the needed list between NEEDED and STOP
3408 (or the end of list if STOP is NULL), and needed by a library that
3409 will be loaded. */
3410
010e5ae2 3411static bfd_boolean
7b15fa7a
AM
3412on_needed_list (const char *soname,
3413 struct bfd_link_needed_list *needed,
3414 struct bfd_link_needed_list *stop)
010e5ae2 3415{
7b15fa7a
AM
3416 struct bfd_link_needed_list *look;
3417 for (look = needed; look != stop; look = look->next)
3418 if (strcmp (soname, look->name) == 0
3419 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3420 /* If needed by a library that itself is not directly
3421 needed, recursively check whether that library is
3422 indirectly needed. Since we add DT_NEEDED entries to
3423 the end of the list, library dependencies appear after
3424 the library. Therefore search prior to the current
3425 LOOK, preventing possible infinite recursion. */
3426 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3427 return TRUE;
3428
3429 return FALSE;
3430}
3431
14160578 3432/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3433static int
3434elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3435{
3436 const struct elf_link_hash_entry *h1;
3437 const struct elf_link_hash_entry *h2;
10b7e05b 3438 bfd_signed_vma vdiff;
5a580b3a
AM
3439
3440 h1 = *(const struct elf_link_hash_entry **) arg1;
3441 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3442 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3443 if (vdiff != 0)
3444 return vdiff > 0 ? 1 : -1;
3445 else
3446 {
d3435ae8 3447 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3448 if (sdiff != 0)
3449 return sdiff > 0 ? 1 : -1;
3450 }
14160578
AM
3451 vdiff = h1->size - h2->size;
3452 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3453}
4ad4eba5 3454
5a580b3a
AM
3455/* This function is used to adjust offsets into .dynstr for
3456 dynamic symbols. This is called via elf_link_hash_traverse. */
3457
3458static bfd_boolean
3459elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3460{
a50b1753 3461 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3462
5a580b3a
AM
3463 if (h->dynindx != -1)
3464 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3465 return TRUE;
3466}
3467
3468/* Assign string offsets in .dynstr, update all structures referencing
3469 them. */
3470
4ad4eba5
AM
3471static bfd_boolean
3472elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3473{
3474 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3475 struct elf_link_local_dynamic_entry *entry;
3476 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3477 bfd *dynobj = hash_table->dynobj;
3478 asection *sdyn;
3479 bfd_size_type size;
3480 const struct elf_backend_data *bed;
3481 bfd_byte *extdyn;
3482
3483 _bfd_elf_strtab_finalize (dynstr);
3484 size = _bfd_elf_strtab_size (dynstr);
3485
3486 bed = get_elf_backend_data (dynobj);
3d4d4302 3487 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3488 BFD_ASSERT (sdyn != NULL);
3489
3490 /* Update all .dynamic entries referencing .dynstr strings. */
3491 for (extdyn = sdyn->contents;
eea6121a 3492 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3493 extdyn += bed->s->sizeof_dyn)
3494 {
3495 Elf_Internal_Dyn dyn;
3496
3497 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3498 switch (dyn.d_tag)
3499 {
3500 case DT_STRSZ:
3501 dyn.d_un.d_val = size;
3502 break;
3503 case DT_NEEDED:
3504 case DT_SONAME:
3505 case DT_RPATH:
3506 case DT_RUNPATH:
3507 case DT_FILTER:
3508 case DT_AUXILIARY:
7ee314fa
AM
3509 case DT_AUDIT:
3510 case DT_DEPAUDIT:
5a580b3a
AM
3511 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3512 break;
3513 default:
3514 continue;
3515 }
3516 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3517 }
3518
3519 /* Now update local dynamic symbols. */
3520 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3521 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3522 entry->isym.st_name);
3523
3524 /* And the rest of dynamic symbols. */
3525 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3526
3527 /* Adjust version definitions. */
3528 if (elf_tdata (output_bfd)->cverdefs)
3529 {
3530 asection *s;
3531 bfd_byte *p;
ef53be89 3532 size_t i;
5a580b3a
AM
3533 Elf_Internal_Verdef def;
3534 Elf_Internal_Verdaux defaux;
3535
3d4d4302 3536 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3537 p = s->contents;
3538 do
3539 {
3540 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3541 &def);
3542 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3543 if (def.vd_aux != sizeof (Elf_External_Verdef))
3544 continue;
5a580b3a
AM
3545 for (i = 0; i < def.vd_cnt; ++i)
3546 {
3547 _bfd_elf_swap_verdaux_in (output_bfd,
3548 (Elf_External_Verdaux *) p, &defaux);
3549 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3550 defaux.vda_name);
3551 _bfd_elf_swap_verdaux_out (output_bfd,
3552 &defaux, (Elf_External_Verdaux *) p);
3553 p += sizeof (Elf_External_Verdaux);
3554 }
3555 }
3556 while (def.vd_next);
3557 }
3558
3559 /* Adjust version references. */
3560 if (elf_tdata (output_bfd)->verref)
3561 {
3562 asection *s;
3563 bfd_byte *p;
ef53be89 3564 size_t i;
5a580b3a
AM
3565 Elf_Internal_Verneed need;
3566 Elf_Internal_Vernaux needaux;
3567
3d4d4302 3568 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3569 p = s->contents;
3570 do
3571 {
3572 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3573 &need);
3574 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3575 _bfd_elf_swap_verneed_out (output_bfd, &need,
3576 (Elf_External_Verneed *) p);
3577 p += sizeof (Elf_External_Verneed);
3578 for (i = 0; i < need.vn_cnt; ++i)
3579 {
3580 _bfd_elf_swap_vernaux_in (output_bfd,
3581 (Elf_External_Vernaux *) p, &needaux);
3582 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3583 needaux.vna_name);
3584 _bfd_elf_swap_vernaux_out (output_bfd,
3585 &needaux,
3586 (Elf_External_Vernaux *) p);
3587 p += sizeof (Elf_External_Vernaux);
3588 }
3589 }
3590 while (need.vn_next);
3591 }
3592
3593 return TRUE;
3594}
3595\f
13285a1b
AM
3596/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3597 The default is to only match when the INPUT and OUTPUT are exactly
3598 the same target. */
3599
3600bfd_boolean
3601_bfd_elf_default_relocs_compatible (const bfd_target *input,
3602 const bfd_target *output)
3603{
3604 return input == output;
3605}
3606
3607/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3608 This version is used when different targets for the same architecture
3609 are virtually identical. */
3610
3611bfd_boolean
3612_bfd_elf_relocs_compatible (const bfd_target *input,
3613 const bfd_target *output)
3614{
3615 const struct elf_backend_data *obed, *ibed;
3616
3617 if (input == output)
3618 return TRUE;
3619
3620 ibed = xvec_get_elf_backend_data (input);
3621 obed = xvec_get_elf_backend_data (output);
3622
3623 if (ibed->arch != obed->arch)
3624 return FALSE;
3625
3626 /* If both backends are using this function, deem them compatible. */
3627 return ibed->relocs_compatible == obed->relocs_compatible;
3628}
3629
e5034e59
AM
3630/* Make a special call to the linker "notice" function to tell it that
3631 we are about to handle an as-needed lib, or have finished
1b786873 3632 processing the lib. */
e5034e59
AM
3633
3634bfd_boolean
3635_bfd_elf_notice_as_needed (bfd *ibfd,
3636 struct bfd_link_info *info,
3637 enum notice_asneeded_action act)
3638{
46135103 3639 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3640}
3641
d9689752
L
3642/* Check relocations an ELF object file. */
3643
3644bfd_boolean
3645_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3646{
3647 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3648 struct elf_link_hash_table *htab = elf_hash_table (info);
3649
3650 /* If this object is the same format as the output object, and it is
3651 not a shared library, then let the backend look through the
3652 relocs.
3653
3654 This is required to build global offset table entries and to
3655 arrange for dynamic relocs. It is not required for the
3656 particular common case of linking non PIC code, even when linking
3657 against shared libraries, but unfortunately there is no way of
3658 knowing whether an object file has been compiled PIC or not.
3659 Looking through the relocs is not particularly time consuming.
3660 The problem is that we must either (1) keep the relocs in memory,
3661 which causes the linker to require additional runtime memory or
3662 (2) read the relocs twice from the input file, which wastes time.
3663 This would be a good case for using mmap.
3664
3665 I have no idea how to handle linking PIC code into a file of a
3666 different format. It probably can't be done. */
3667 if ((abfd->flags & DYNAMIC) == 0
3668 && is_elf_hash_table (htab)
3669 && bed->check_relocs != NULL
3670 && elf_object_id (abfd) == elf_hash_table_id (htab)
3671 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3672 {
3673 asection *o;
3674
3675 for (o = abfd->sections; o != NULL; o = o->next)
3676 {
3677 Elf_Internal_Rela *internal_relocs;
3678 bfd_boolean ok;
3679
5ce03cea 3680 /* Don't check relocations in excluded sections. */
d9689752 3681 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3682 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3683 || o->reloc_count == 0
3684 || ((info->strip == strip_all || info->strip == strip_debugger)
3685 && (o->flags & SEC_DEBUGGING) != 0)
3686 || bfd_is_abs_section (o->output_section))
3687 continue;
3688
3689 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3690 info->keep_memory);
3691 if (internal_relocs == NULL)
3692 return FALSE;
3693
3694 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3695
3696 if (elf_section_data (o)->relocs != internal_relocs)
3697 free (internal_relocs);
3698
3699 if (! ok)
3700 return FALSE;
3701 }
3702 }
3703
3704 return TRUE;
3705}
3706
4ad4eba5
AM
3707/* Add symbols from an ELF object file to the linker hash table. */
3708
3709static bfd_boolean
3710elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3711{
a0c402a5 3712 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3713 Elf_Internal_Shdr *hdr;
ef53be89
AM
3714 size_t symcount;
3715 size_t extsymcount;
3716 size_t extsymoff;
4ad4eba5
AM
3717 struct elf_link_hash_entry **sym_hash;
3718 bfd_boolean dynamic;
3719 Elf_External_Versym *extversym = NULL;
3720 Elf_External_Versym *ever;
3721 struct elf_link_hash_entry *weaks;
3722 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3723 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3724 Elf_Internal_Sym *isymbuf = NULL;
3725 Elf_Internal_Sym *isym;
3726 Elf_Internal_Sym *isymend;
3727 const struct elf_backend_data *bed;
3728 bfd_boolean add_needed;
66eb6687 3729 struct elf_link_hash_table *htab;
4ad4eba5 3730 bfd_size_type amt;
66eb6687 3731 void *alloc_mark = NULL;
4f87808c
AM
3732 struct bfd_hash_entry **old_table = NULL;
3733 unsigned int old_size = 0;
3734 unsigned int old_count = 0;
66eb6687 3735 void *old_tab = NULL;
66eb6687
AM
3736 void *old_ent;
3737 struct bfd_link_hash_entry *old_undefs = NULL;
3738 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3739 void *old_strtab = NULL;
66eb6687 3740 size_t tabsize = 0;
db6a5d5f 3741 asection *s;
29a9f53e 3742 bfd_boolean just_syms;
4ad4eba5 3743
66eb6687 3744 htab = elf_hash_table (info);
4ad4eba5 3745 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3746
3747 if ((abfd->flags & DYNAMIC) == 0)
3748 dynamic = FALSE;
3749 else
3750 {
3751 dynamic = TRUE;
3752
3753 /* You can't use -r against a dynamic object. Also, there's no
3754 hope of using a dynamic object which does not exactly match
3755 the format of the output file. */
0e1862bb 3756 if (bfd_link_relocatable (info)
66eb6687 3757 || !is_elf_hash_table (htab)
f13a99db 3758 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3759 {
0e1862bb 3760 if (bfd_link_relocatable (info))
9a0789ec
NC
3761 bfd_set_error (bfd_error_invalid_operation);
3762 else
3763 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3764 goto error_return;
3765 }
3766 }
3767
a0c402a5
L
3768 ehdr = elf_elfheader (abfd);
3769 if (info->warn_alternate_em
3770 && bed->elf_machine_code != ehdr->e_machine
3771 && ((bed->elf_machine_alt1 != 0
3772 && ehdr->e_machine == bed->elf_machine_alt1)
3773 || (bed->elf_machine_alt2 != 0
3774 && ehdr->e_machine == bed->elf_machine_alt2)))
3775 info->callbacks->einfo
695344c0 3776 /* xgettext:c-format */
a0c402a5
L
3777 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3778 ehdr->e_machine, abfd, bed->elf_machine_code);
3779
4ad4eba5
AM
3780 /* As a GNU extension, any input sections which are named
3781 .gnu.warning.SYMBOL are treated as warning symbols for the given
3782 symbol. This differs from .gnu.warning sections, which generate
3783 warnings when they are included in an output file. */
dd98f8d2 3784 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3785 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3786 {
db6a5d5f 3787 const char *name;
4ad4eba5 3788
db6a5d5f
AM
3789 name = bfd_get_section_name (abfd, s);
3790 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3791 {
db6a5d5f
AM
3792 char *msg;
3793 bfd_size_type sz;
3794
3795 name += sizeof ".gnu.warning." - 1;
3796
3797 /* If this is a shared object, then look up the symbol
3798 in the hash table. If it is there, and it is already
3799 been defined, then we will not be using the entry
3800 from this shared object, so we don't need to warn.
3801 FIXME: If we see the definition in a regular object
3802 later on, we will warn, but we shouldn't. The only
3803 fix is to keep track of what warnings we are supposed
3804 to emit, and then handle them all at the end of the
3805 link. */
3806 if (dynamic)
4ad4eba5 3807 {
db6a5d5f
AM
3808 struct elf_link_hash_entry *h;
3809
3810 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3811
3812 /* FIXME: What about bfd_link_hash_common? */
3813 if (h != NULL
3814 && (h->root.type == bfd_link_hash_defined
3815 || h->root.type == bfd_link_hash_defweak))
3816 continue;
3817 }
4ad4eba5 3818
db6a5d5f
AM
3819 sz = s->size;
3820 msg = (char *) bfd_alloc (abfd, sz + 1);
3821 if (msg == NULL)
3822 goto error_return;
4ad4eba5 3823
db6a5d5f
AM
3824 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3825 goto error_return;
4ad4eba5 3826
db6a5d5f 3827 msg[sz] = '\0';
4ad4eba5 3828
db6a5d5f
AM
3829 if (! (_bfd_generic_link_add_one_symbol
3830 (info, abfd, name, BSF_WARNING, s, 0, msg,
3831 FALSE, bed->collect, NULL)))
3832 goto error_return;
4ad4eba5 3833
0e1862bb 3834 if (bfd_link_executable (info))
db6a5d5f
AM
3835 {
3836 /* Clobber the section size so that the warning does
3837 not get copied into the output file. */
3838 s->size = 0;
11d2f718 3839
db6a5d5f
AM
3840 /* Also set SEC_EXCLUDE, so that symbols defined in
3841 the warning section don't get copied to the output. */
3842 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3843 }
3844 }
3845 }
3846
29a9f53e
L
3847 just_syms = ((s = abfd->sections) != NULL
3848 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3849
4ad4eba5
AM
3850 add_needed = TRUE;
3851 if (! dynamic)
3852 {
3853 /* If we are creating a shared library, create all the dynamic
3854 sections immediately. We need to attach them to something,
3855 so we attach them to this BFD, provided it is the right
bf89386a
L
3856 format and is not from ld --just-symbols. Always create the
3857 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3858 are no input BFD's of the same format as the output, we can't
3859 make a shared library. */
3860 if (!just_syms
bf89386a 3861 && (bfd_link_pic (info)
9c1d7a08 3862 || (!bfd_link_relocatable (info)
3c5fce9b 3863 && info->nointerp
9c1d7a08 3864 && (info->export_dynamic || info->dynamic)))
66eb6687 3865 && is_elf_hash_table (htab)
f13a99db 3866 && info->output_bfd->xvec == abfd->xvec
66eb6687 3867 && !htab->dynamic_sections_created)
4ad4eba5
AM
3868 {
3869 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3870 goto error_return;
3871 }
3872 }
66eb6687 3873 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3874 goto error_return;
3875 else
3876 {
4ad4eba5 3877 const char *soname = NULL;
7ee314fa 3878 char *audit = NULL;
4ad4eba5 3879 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3880 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3881 int ret;
3882
3883 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3884 ld shouldn't allow it. */
29a9f53e 3885 if (just_syms)
92fd189d 3886 abort ();
4ad4eba5
AM
3887
3888 /* If this dynamic lib was specified on the command line with
3889 --as-needed in effect, then we don't want to add a DT_NEEDED
3890 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3891 in by another lib's DT_NEEDED. When --no-add-needed is used
3892 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3893 any dynamic library in DT_NEEDED tags in the dynamic lib at
3894 all. */
3895 add_needed = (elf_dyn_lib_class (abfd)
3896 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3897 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3898
3899 s = bfd_get_section_by_name (abfd, ".dynamic");
3900 if (s != NULL)
3901 {
3902 bfd_byte *dynbuf;
3903 bfd_byte *extdyn;
cb33740c 3904 unsigned int elfsec;
4ad4eba5
AM
3905 unsigned long shlink;
3906
eea6121a 3907 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3908 {
3909error_free_dyn:
3910 free (dynbuf);
3911 goto error_return;
3912 }
4ad4eba5
AM
3913
3914 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3915 if (elfsec == SHN_BAD)
4ad4eba5
AM
3916 goto error_free_dyn;
3917 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3918
3919 for (extdyn = dynbuf;
eea6121a 3920 extdyn < dynbuf + s->size;
4ad4eba5
AM
3921 extdyn += bed->s->sizeof_dyn)
3922 {
3923 Elf_Internal_Dyn dyn;
3924
3925 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3926 if (dyn.d_tag == DT_SONAME)
3927 {
3928 unsigned int tagv = dyn.d_un.d_val;
3929 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3930 if (soname == NULL)
3931 goto error_free_dyn;
3932 }
3933 if (dyn.d_tag == DT_NEEDED)
3934 {
3935 struct bfd_link_needed_list *n, **pn;
3936 char *fnm, *anm;
3937 unsigned int tagv = dyn.d_un.d_val;
3938
3939 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3940 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3941 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3942 if (n == NULL || fnm == NULL)
3943 goto error_free_dyn;
3944 amt = strlen (fnm) + 1;
a50b1753 3945 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3946 if (anm == NULL)
3947 goto error_free_dyn;
3948 memcpy (anm, fnm, amt);
3949 n->name = anm;
3950 n->by = abfd;
3951 n->next = NULL;
66eb6687 3952 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3953 ;
3954 *pn = n;
3955 }
3956 if (dyn.d_tag == DT_RUNPATH)
3957 {
3958 struct bfd_link_needed_list *n, **pn;
3959 char *fnm, *anm;
3960 unsigned int tagv = dyn.d_un.d_val;
3961
3962 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3963 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3964 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3965 if (n == NULL || fnm == NULL)
3966 goto error_free_dyn;
3967 amt = strlen (fnm) + 1;
a50b1753 3968 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3969 if (anm == NULL)
3970 goto error_free_dyn;
3971 memcpy (anm, fnm, amt);
3972 n->name = anm;
3973 n->by = abfd;
3974 n->next = NULL;
3975 for (pn = & runpath;
3976 *pn != NULL;
3977 pn = &(*pn)->next)
3978 ;
3979 *pn = n;
3980 }
3981 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3982 if (!runpath && dyn.d_tag == DT_RPATH)
3983 {
3984 struct bfd_link_needed_list *n, **pn;
3985 char *fnm, *anm;
3986 unsigned int tagv = dyn.d_un.d_val;
3987
3988 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3989 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3990 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3991 if (n == NULL || fnm == NULL)
3992 goto error_free_dyn;
3993 amt = strlen (fnm) + 1;
a50b1753 3994 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3995 if (anm == NULL)
f8703194 3996 goto error_free_dyn;
4ad4eba5
AM
3997 memcpy (anm, fnm, amt);
3998 n->name = anm;
3999 n->by = abfd;
4000 n->next = NULL;
4001 for (pn = & rpath;
4002 *pn != NULL;
4003 pn = &(*pn)->next)
4004 ;
4005 *pn = n;
4006 }
7ee314fa
AM
4007 if (dyn.d_tag == DT_AUDIT)
4008 {
4009 unsigned int tagv = dyn.d_un.d_val;
4010 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4011 }
4ad4eba5
AM
4012 }
4013
4014 free (dynbuf);
4015 }
4016
4017 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4018 frees all more recently bfd_alloc'd blocks as well. */
4019 if (runpath)
4020 rpath = runpath;
4021
4022 if (rpath)
4023 {
4024 struct bfd_link_needed_list **pn;
66eb6687 4025 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4026 ;
4027 *pn = rpath;
4028 }
4029
9acc85a6
AM
4030 /* If we have a PT_GNU_RELRO program header, mark as read-only
4031 all sections contained fully therein. This makes relro
4032 shared library sections appear as they will at run-time. */
4033 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4034 while (--phdr >= elf_tdata (abfd)->phdr)
4035 if (phdr->p_type == PT_GNU_RELRO)
4036 {
4037 for (s = abfd->sections; s != NULL; s = s->next)
4038 if ((s->flags & SEC_ALLOC) != 0
4039 && s->vma >= phdr->p_vaddr
4040 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4041 s->flags |= SEC_READONLY;
4042 break;
4043 }
4044
4ad4eba5
AM
4045 /* We do not want to include any of the sections in a dynamic
4046 object in the output file. We hack by simply clobbering the
4047 list of sections in the BFD. This could be handled more
4048 cleanly by, say, a new section flag; the existing
4049 SEC_NEVER_LOAD flag is not the one we want, because that one
4050 still implies that the section takes up space in the output
4051 file. */
4052 bfd_section_list_clear (abfd);
4053
4ad4eba5
AM
4054 /* Find the name to use in a DT_NEEDED entry that refers to this
4055 object. If the object has a DT_SONAME entry, we use it.
4056 Otherwise, if the generic linker stuck something in
4057 elf_dt_name, we use that. Otherwise, we just use the file
4058 name. */
4059 if (soname == NULL || *soname == '\0')
4060 {
4061 soname = elf_dt_name (abfd);
4062 if (soname == NULL || *soname == '\0')
4063 soname = bfd_get_filename (abfd);
4064 }
4065
4066 /* Save the SONAME because sometimes the linker emulation code
4067 will need to know it. */
4068 elf_dt_name (abfd) = soname;
4069
7e9f0867 4070 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4071 if (ret < 0)
4072 goto error_return;
4073
4074 /* If we have already included this dynamic object in the
4075 link, just ignore it. There is no reason to include a
4076 particular dynamic object more than once. */
4077 if (ret > 0)
4078 return TRUE;
7ee314fa
AM
4079
4080 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4081 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4082 }
4083
4084 /* If this is a dynamic object, we always link against the .dynsym
4085 symbol table, not the .symtab symbol table. The dynamic linker
4086 will only see the .dynsym symbol table, so there is no reason to
4087 look at .symtab for a dynamic object. */
4088
4089 if (! dynamic || elf_dynsymtab (abfd) == 0)
4090 hdr = &elf_tdata (abfd)->symtab_hdr;
4091 else
4092 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4093
4094 symcount = hdr->sh_size / bed->s->sizeof_sym;
4095
4096 /* The sh_info field of the symtab header tells us where the
4097 external symbols start. We don't care about the local symbols at
4098 this point. */
4099 if (elf_bad_symtab (abfd))
4100 {
4101 extsymcount = symcount;
4102 extsymoff = 0;
4103 }
4104 else
4105 {
4106 extsymcount = symcount - hdr->sh_info;
4107 extsymoff = hdr->sh_info;
4108 }
4109
f45794cb 4110 sym_hash = elf_sym_hashes (abfd);
012b2306 4111 if (extsymcount != 0)
4ad4eba5
AM
4112 {
4113 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4114 NULL, NULL, NULL);
4115 if (isymbuf == NULL)
4116 goto error_return;
4117
4ad4eba5 4118 if (sym_hash == NULL)
012b2306
AM
4119 {
4120 /* We store a pointer to the hash table entry for each
4121 external symbol. */
ef53be89
AM
4122 amt = extsymcount;
4123 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4124 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4125 if (sym_hash == NULL)
4126 goto error_free_sym;
4127 elf_sym_hashes (abfd) = sym_hash;
4128 }
4ad4eba5
AM
4129 }
4130
4131 if (dynamic)
4132 {
4133 /* Read in any version definitions. */
fc0e6df6
PB
4134 if (!_bfd_elf_slurp_version_tables (abfd,
4135 info->default_imported_symver))
4ad4eba5
AM
4136 goto error_free_sym;
4137
4138 /* Read in the symbol versions, but don't bother to convert them
4139 to internal format. */
4140 if (elf_dynversym (abfd) != 0)
4141 {
4142 Elf_Internal_Shdr *versymhdr;
4143
4144 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4145 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4146 if (extversym == NULL)
4147 goto error_free_sym;
4148 amt = versymhdr->sh_size;
4149 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4150 || bfd_bread (extversym, amt, abfd) != amt)
4151 goto error_free_vers;
4152 }
4153 }
4154
66eb6687
AM
4155 /* If we are loading an as-needed shared lib, save the symbol table
4156 state before we start adding symbols. If the lib turns out
4157 to be unneeded, restore the state. */
4158 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4159 {
4160 unsigned int i;
4161 size_t entsize;
4162
4163 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4164 {
4165 struct bfd_hash_entry *p;
2de92251 4166 struct elf_link_hash_entry *h;
66eb6687
AM
4167
4168 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4169 {
4170 h = (struct elf_link_hash_entry *) p;
4171 entsize += htab->root.table.entsize;
4172 if (h->root.type == bfd_link_hash_warning)
4173 entsize += htab->root.table.entsize;
4174 }
66eb6687
AM
4175 }
4176
4177 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4178 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4179 if (old_tab == NULL)
4180 goto error_free_vers;
4181
4182 /* Remember the current objalloc pointer, so that all mem for
4183 symbols added can later be reclaimed. */
4184 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4185 if (alloc_mark == NULL)
4186 goto error_free_vers;
4187
5061a885
AM
4188 /* Make a special call to the linker "notice" function to
4189 tell it that we are about to handle an as-needed lib. */
e5034e59 4190 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4191 goto error_free_vers;
5061a885 4192
f45794cb
AM
4193 /* Clone the symbol table. Remember some pointers into the
4194 symbol table, and dynamic symbol count. */
4195 old_ent = (char *) old_tab + tabsize;
66eb6687 4196 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4197 old_undefs = htab->root.undefs;
4198 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4199 old_table = htab->root.table.table;
4200 old_size = htab->root.table.size;
4201 old_count = htab->root.table.count;
5b677558
AM
4202 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4203 if (old_strtab == NULL)
4204 goto error_free_vers;
66eb6687
AM
4205
4206 for (i = 0; i < htab->root.table.size; i++)
4207 {
4208 struct bfd_hash_entry *p;
2de92251 4209 struct elf_link_hash_entry *h;
66eb6687
AM
4210
4211 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4212 {
4213 memcpy (old_ent, p, htab->root.table.entsize);
4214 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4215 h = (struct elf_link_hash_entry *) p;
4216 if (h->root.type == bfd_link_hash_warning)
4217 {
4218 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4219 old_ent = (char *) old_ent + htab->root.table.entsize;
4220 }
66eb6687
AM
4221 }
4222 }
4223 }
4ad4eba5 4224
66eb6687 4225 weaks = NULL;
4ad4eba5
AM
4226 ever = extversym != NULL ? extversym + extsymoff : NULL;
4227 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4228 isym < isymend;
4229 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4230 {
4231 int bind;
4232 bfd_vma value;
af44c138 4233 asection *sec, *new_sec;
4ad4eba5
AM
4234 flagword flags;
4235 const char *name;
4236 struct elf_link_hash_entry *h;
90c984fc 4237 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4238 bfd_boolean definition;
4239 bfd_boolean size_change_ok;
4240 bfd_boolean type_change_ok;
4241 bfd_boolean new_weakdef;
37a9e49a
L
4242 bfd_boolean new_weak;
4243 bfd_boolean old_weak;
4ad4eba5 4244 bfd_boolean override;
a4d8e49b 4245 bfd_boolean common;
97196564 4246 bfd_boolean discarded;
4ad4eba5
AM
4247 unsigned int old_alignment;
4248 bfd *old_bfd;
6e33951e 4249 bfd_boolean matched;
4ad4eba5
AM
4250
4251 override = FALSE;
4252
4253 flags = BSF_NO_FLAGS;
4254 sec = NULL;
4255 value = isym->st_value;
a4d8e49b 4256 common = bed->common_definition (isym);
2980ccad
L
4257 if (common && info->inhibit_common_definition)
4258 {
4259 /* Treat common symbol as undefined for --no-define-common. */
4260 isym->st_shndx = SHN_UNDEF;
4261 common = FALSE;
4262 }
97196564 4263 discarded = FALSE;
4ad4eba5
AM
4264
4265 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4266 switch (bind)
4ad4eba5 4267 {
3e7a7d11 4268 case STB_LOCAL:
4ad4eba5
AM
4269 /* This should be impossible, since ELF requires that all
4270 global symbols follow all local symbols, and that sh_info
4271 point to the first global symbol. Unfortunately, Irix 5
4272 screws this up. */
4273 continue;
3e7a7d11
NC
4274
4275 case STB_GLOBAL:
a4d8e49b 4276 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4277 flags = BSF_GLOBAL;
3e7a7d11
NC
4278 break;
4279
4280 case STB_WEAK:
4281 flags = BSF_WEAK;
4282 break;
4283
4284 case STB_GNU_UNIQUE:
4285 flags = BSF_GNU_UNIQUE;
4286 break;
4287
4288 default:
4ad4eba5 4289 /* Leave it up to the processor backend. */
3e7a7d11 4290 break;
4ad4eba5
AM
4291 }
4292
4293 if (isym->st_shndx == SHN_UNDEF)
4294 sec = bfd_und_section_ptr;
cb33740c
AM
4295 else if (isym->st_shndx == SHN_ABS)
4296 sec = bfd_abs_section_ptr;
4297 else if (isym->st_shndx == SHN_COMMON)
4298 {
4299 sec = bfd_com_section_ptr;
4300 /* What ELF calls the size we call the value. What ELF
4301 calls the value we call the alignment. */
4302 value = isym->st_size;
4303 }
4304 else
4ad4eba5
AM
4305 {
4306 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4307 if (sec == NULL)
4308 sec = bfd_abs_section_ptr;
dbaa2011 4309 else if (discarded_section (sec))
529fcb95 4310 {
e5d08002
L
4311 /* Symbols from discarded section are undefined. We keep
4312 its visibility. */
529fcb95 4313 sec = bfd_und_section_ptr;
97196564 4314 discarded = TRUE;
529fcb95
PB
4315 isym->st_shndx = SHN_UNDEF;
4316 }
4ad4eba5
AM
4317 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4318 value -= sec->vma;
4319 }
4ad4eba5
AM
4320
4321 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4322 isym->st_name);
4323 if (name == NULL)
4324 goto error_free_vers;
4325
4326 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4327 && (abfd->flags & BFD_PLUGIN) != 0)
4328 {
4329 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4330
4331 if (xc == NULL)
4332 {
4333 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4334 | SEC_EXCLUDE);
4335 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4336 if (xc == NULL)
4337 goto error_free_vers;
4338 }
4339 sec = xc;
4340 }
4341 else if (isym->st_shndx == SHN_COMMON
4342 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4343 && !bfd_link_relocatable (info))
4ad4eba5
AM
4344 {
4345 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4346
4347 if (tcomm == NULL)
4348 {
02d00247
AM
4349 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4350 | SEC_LINKER_CREATED);
4351 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4352 if (tcomm == NULL)
4ad4eba5
AM
4353 goto error_free_vers;
4354 }
4355 sec = tcomm;
4356 }
66eb6687 4357 else if (bed->elf_add_symbol_hook)
4ad4eba5 4358 {
66eb6687
AM
4359 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4360 &sec, &value))
4ad4eba5
AM
4361 goto error_free_vers;
4362
4363 /* The hook function sets the name to NULL if this symbol
4364 should be skipped for some reason. */
4365 if (name == NULL)
4366 continue;
4367 }
4368
4369 /* Sanity check that all possibilities were handled. */
4370 if (sec == NULL)
4371 {
4372 bfd_set_error (bfd_error_bad_value);
4373 goto error_free_vers;
4374 }
4375
191c0c42
AM
4376 /* Silently discard TLS symbols from --just-syms. There's
4377 no way to combine a static TLS block with a new TLS block
4378 for this executable. */
4379 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4380 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4381 continue;
4382
4ad4eba5
AM
4383 if (bfd_is_und_section (sec)
4384 || bfd_is_com_section (sec))
4385 definition = FALSE;
4386 else
4387 definition = TRUE;
4388
4389 size_change_ok = FALSE;
66eb6687 4390 type_change_ok = bed->type_change_ok;
37a9e49a 4391 old_weak = FALSE;
6e33951e 4392 matched = FALSE;
4ad4eba5
AM
4393 old_alignment = 0;
4394 old_bfd = NULL;
af44c138 4395 new_sec = sec;
4ad4eba5 4396
66eb6687 4397 if (is_elf_hash_table (htab))
4ad4eba5
AM
4398 {
4399 Elf_Internal_Versym iver;
4400 unsigned int vernum = 0;
4401 bfd_boolean skip;
4402
fc0e6df6 4403 if (ever == NULL)
4ad4eba5 4404 {
fc0e6df6
PB
4405 if (info->default_imported_symver)
4406 /* Use the default symbol version created earlier. */
4407 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4408 else
4409 iver.vs_vers = 0;
4410 }
4411 else
4412 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4413
4414 vernum = iver.vs_vers & VERSYM_VERSION;
4415
4416 /* If this is a hidden symbol, or if it is not version
4417 1, we append the version name to the symbol name.
cc86ff91
EB
4418 However, we do not modify a non-hidden absolute symbol
4419 if it is not a function, because it might be the version
4420 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4421 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4422 || (vernum > 1
4423 && (!bfd_is_abs_section (sec)
4424 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4425 {
4426 const char *verstr;
4427 size_t namelen, verlen, newlen;
4428 char *newname, *p;
4429
4430 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4431 {
fc0e6df6
PB
4432 if (vernum > elf_tdata (abfd)->cverdefs)
4433 verstr = NULL;
4434 else if (vernum > 1)
4435 verstr =
4436 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4437 else
4438 verstr = "";
4ad4eba5 4439
fc0e6df6 4440 if (verstr == NULL)
4ad4eba5 4441 {
4eca0228 4442 _bfd_error_handler
695344c0 4443 /* xgettext:c-format */
fc0e6df6
PB
4444 (_("%B: %s: invalid version %u (max %d)"),
4445 abfd, name, vernum,
4446 elf_tdata (abfd)->cverdefs);
4447 bfd_set_error (bfd_error_bad_value);
4448 goto error_free_vers;
4ad4eba5 4449 }
fc0e6df6
PB
4450 }
4451 else
4452 {
4453 /* We cannot simply test for the number of
4454 entries in the VERNEED section since the
4455 numbers for the needed versions do not start
4456 at 0. */
4457 Elf_Internal_Verneed *t;
4458
4459 verstr = NULL;
4460 for (t = elf_tdata (abfd)->verref;
4461 t != NULL;
4462 t = t->vn_nextref)
4ad4eba5 4463 {
fc0e6df6 4464 Elf_Internal_Vernaux *a;
4ad4eba5 4465
fc0e6df6
PB
4466 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4467 {
4468 if (a->vna_other == vernum)
4ad4eba5 4469 {
fc0e6df6
PB
4470 verstr = a->vna_nodename;
4471 break;
4ad4eba5 4472 }
4ad4eba5 4473 }
fc0e6df6
PB
4474 if (a != NULL)
4475 break;
4476 }
4477 if (verstr == NULL)
4478 {
4eca0228 4479 _bfd_error_handler
695344c0 4480 /* xgettext:c-format */
fc0e6df6
PB
4481 (_("%B: %s: invalid needed version %d"),
4482 abfd, name, vernum);
4483 bfd_set_error (bfd_error_bad_value);
4484 goto error_free_vers;
4ad4eba5 4485 }
4ad4eba5 4486 }
fc0e6df6
PB
4487
4488 namelen = strlen (name);
4489 verlen = strlen (verstr);
4490 newlen = namelen + verlen + 2;
4491 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4492 && isym->st_shndx != SHN_UNDEF)
4493 ++newlen;
4494
a50b1753 4495 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4496 if (newname == NULL)
4497 goto error_free_vers;
4498 memcpy (newname, name, namelen);
4499 p = newname + namelen;
4500 *p++ = ELF_VER_CHR;
4501 /* If this is a defined non-hidden version symbol,
4502 we add another @ to the name. This indicates the
4503 default version of the symbol. */
4504 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4505 && isym->st_shndx != SHN_UNDEF)
4506 *p++ = ELF_VER_CHR;
4507 memcpy (p, verstr, verlen + 1);
4508
4509 name = newname;
4ad4eba5
AM
4510 }
4511
cd3416da
AM
4512 /* If this symbol has default visibility and the user has
4513 requested we not re-export it, then mark it as hidden. */
a0d49154 4514 if (!bfd_is_und_section (sec)
cd3416da 4515 && !dynamic
ce875075 4516 && abfd->no_export
cd3416da
AM
4517 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4518 isym->st_other = (STV_HIDDEN
4519 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4520
4f3fedcf
AM
4521 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4522 sym_hash, &old_bfd, &old_weak,
4523 &old_alignment, &skip, &override,
6e33951e
L
4524 &type_change_ok, &size_change_ok,
4525 &matched))
4ad4eba5
AM
4526 goto error_free_vers;
4527
4528 if (skip)
4529 continue;
4530
6e33951e
L
4531 /* Override a definition only if the new symbol matches the
4532 existing one. */
4533 if (override && matched)
4ad4eba5
AM
4534 definition = FALSE;
4535
4536 h = *sym_hash;
4537 while (h->root.type == bfd_link_hash_indirect
4538 || h->root.type == bfd_link_hash_warning)
4539 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4540
4ad4eba5 4541 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4542 && vernum > 1
4543 && definition)
4544 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4545 }
4546
4547 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4548 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4549 (struct bfd_link_hash_entry **) sym_hash)))
4550 goto error_free_vers;
4551
a43942db
MR
4552 if ((flags & BSF_GNU_UNIQUE)
4553 && (abfd->flags & DYNAMIC) == 0
4554 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4555 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4556
4ad4eba5 4557 h = *sym_hash;
90c984fc
L
4558 /* We need to make sure that indirect symbol dynamic flags are
4559 updated. */
4560 hi = h;
4ad4eba5
AM
4561 while (h->root.type == bfd_link_hash_indirect
4562 || h->root.type == bfd_link_hash_warning)
4563 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4564
97196564
L
4565 /* Setting the index to -3 tells elf_link_output_extsym that
4566 this symbol is defined in a discarded section. */
4567 if (discarded)
4568 h->indx = -3;
4569
4ad4eba5
AM
4570 *sym_hash = h;
4571
37a9e49a 4572 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4573 new_weakdef = FALSE;
4574 if (dynamic
4575 && definition
37a9e49a 4576 && new_weak
fcb93ecf 4577 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4578 && is_elf_hash_table (htab)
f6e332e6 4579 && h->u.weakdef == NULL)
4ad4eba5
AM
4580 {
4581 /* Keep a list of all weak defined non function symbols from
4582 a dynamic object, using the weakdef field. Later in this
4583 function we will set the weakdef field to the correct
4584 value. We only put non-function symbols from dynamic
4585 objects on this list, because that happens to be the only
4586 time we need to know the normal symbol corresponding to a
4587 weak symbol, and the information is time consuming to
4588 figure out. If the weakdef field is not already NULL,
4589 then this symbol was already defined by some previous
4590 dynamic object, and we will be using that previous
4591 definition anyhow. */
4592
f6e332e6 4593 h->u.weakdef = weaks;
4ad4eba5
AM
4594 weaks = h;
4595 new_weakdef = TRUE;
4596 }
4597
4598 /* Set the alignment of a common symbol. */
a4d8e49b 4599 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4600 && h->root.type == bfd_link_hash_common)
4601 {
4602 unsigned int align;
4603
a4d8e49b 4604 if (common)
af44c138
L
4605 align = bfd_log2 (isym->st_value);
4606 else
4607 {
4608 /* The new symbol is a common symbol in a shared object.
4609 We need to get the alignment from the section. */
4610 align = new_sec->alignment_power;
4611 }
595213d4 4612 if (align > old_alignment)
4ad4eba5
AM
4613 h->root.u.c.p->alignment_power = align;
4614 else
4615 h->root.u.c.p->alignment_power = old_alignment;
4616 }
4617
66eb6687 4618 if (is_elf_hash_table (htab))
4ad4eba5 4619 {
4f3fedcf
AM
4620 /* Set a flag in the hash table entry indicating the type of
4621 reference or definition we just found. A dynamic symbol
4622 is one which is referenced or defined by both a regular
4623 object and a shared object. */
4624 bfd_boolean dynsym = FALSE;
4625
4626 /* Plugin symbols aren't normal. Don't set def_regular or
4627 ref_regular for them, or make them dynamic. */
4628 if ((abfd->flags & BFD_PLUGIN) != 0)
4629 ;
4630 else if (! dynamic)
4631 {
4632 if (! definition)
4633 {
4634 h->ref_regular = 1;
4635 if (bind != STB_WEAK)
4636 h->ref_regular_nonweak = 1;
4637 }
4638 else
4639 {
4640 h->def_regular = 1;
4641 if (h->def_dynamic)
4642 {
4643 h->def_dynamic = 0;
4644 h->ref_dynamic = 1;
4645 }
4646 }
4647
4648 /* If the indirect symbol has been forced local, don't
4649 make the real symbol dynamic. */
4650 if ((h == hi || !hi->forced_local)
0e1862bb 4651 && (bfd_link_dll (info)
4f3fedcf
AM
4652 || h->def_dynamic
4653 || h->ref_dynamic))
4654 dynsym = TRUE;
4655 }
4656 else
4657 {
4658 if (! definition)
4659 {
4660 h->ref_dynamic = 1;
4661 hi->ref_dynamic = 1;
4662 }
4663 else
4664 {
4665 h->def_dynamic = 1;
4666 hi->def_dynamic = 1;
4667 }
4668
4669 /* If the indirect symbol has been forced local, don't
4670 make the real symbol dynamic. */
4671 if ((h == hi || !hi->forced_local)
4672 && (h->def_regular
4673 || h->ref_regular
4674 || (h->u.weakdef != NULL
4675 && ! new_weakdef
4676 && h->u.weakdef->dynindx != -1)))
4677 dynsym = TRUE;
4678 }
4679
4680 /* Check to see if we need to add an indirect symbol for
4681 the default name. */
4682 if (definition
4683 || (!override && h->root.type == bfd_link_hash_common))
4684 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4685 sec, value, &old_bfd, &dynsym))
4686 goto error_free_vers;
4ad4eba5
AM
4687
4688 /* Check the alignment when a common symbol is involved. This
4689 can change when a common symbol is overridden by a normal
4690 definition or a common symbol is ignored due to the old
4691 normal definition. We need to make sure the maximum
4692 alignment is maintained. */
a4d8e49b 4693 if ((old_alignment || common)
4ad4eba5
AM
4694 && h->root.type != bfd_link_hash_common)
4695 {
4696 unsigned int common_align;
4697 unsigned int normal_align;
4698 unsigned int symbol_align;
4699 bfd *normal_bfd;
4700 bfd *common_bfd;
4701
3a81e825
AM
4702 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4703 || h->root.type == bfd_link_hash_defweak);
4704
4ad4eba5
AM
4705 symbol_align = ffs (h->root.u.def.value) - 1;
4706 if (h->root.u.def.section->owner != NULL
0616a280
AM
4707 && (h->root.u.def.section->owner->flags
4708 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4709 {
4710 normal_align = h->root.u.def.section->alignment_power;
4711 if (normal_align > symbol_align)
4712 normal_align = symbol_align;
4713 }
4714 else
4715 normal_align = symbol_align;
4716
4717 if (old_alignment)
4718 {
4719 common_align = old_alignment;
4720 common_bfd = old_bfd;
4721 normal_bfd = abfd;
4722 }
4723 else
4724 {
4725 common_align = bfd_log2 (isym->st_value);
4726 common_bfd = abfd;
4727 normal_bfd = old_bfd;
4728 }
4729
4730 if (normal_align < common_align)
d07676f8
NC
4731 {
4732 /* PR binutils/2735 */
4733 if (normal_bfd == NULL)
4eca0228 4734 _bfd_error_handler
695344c0 4735 /* xgettext:c-format */
4f3fedcf
AM
4736 (_("Warning: alignment %u of common symbol `%s' in %B is"
4737 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4738 1 << common_align, name, common_bfd,
4739 1 << normal_align, h->root.u.def.section);
d07676f8 4740 else
4eca0228 4741 _bfd_error_handler
695344c0 4742 /* xgettext:c-format */
d07676f8
NC
4743 (_("Warning: alignment %u of symbol `%s' in %B"
4744 " is smaller than %u in %B"),
c08bb8dd
AM
4745 1 << normal_align, name, normal_bfd,
4746 1 << common_align, common_bfd);
d07676f8 4747 }
4ad4eba5
AM
4748 }
4749
83ad0046 4750 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4751 if (isym->st_size != 0
4752 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4753 && (definition || h->size == 0))
4754 {
83ad0046
L
4755 if (h->size != 0
4756 && h->size != isym->st_size
4757 && ! size_change_ok)
4eca0228 4758 _bfd_error_handler
695344c0 4759 /* xgettext:c-format */
d003868e 4760 (_("Warning: size of symbol `%s' changed"
76cfced5
AM
4761 " from %Lu in %B to %Lu in %B"),
4762 name, h->size, old_bfd, isym->st_size, abfd);
4ad4eba5
AM
4763
4764 h->size = isym->st_size;
4765 }
4766
4767 /* If this is a common symbol, then we always want H->SIZE
4768 to be the size of the common symbol. The code just above
4769 won't fix the size if a common symbol becomes larger. We
4770 don't warn about a size change here, because that is
4f3fedcf 4771 covered by --warn-common. Allow changes between different
fcb93ecf 4772 function types. */
4ad4eba5
AM
4773 if (h->root.type == bfd_link_hash_common)
4774 h->size = h->root.u.c.size;
4775
4776 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4777 && ((definition && !new_weak)
4778 || (old_weak && h->root.type == bfd_link_hash_common)
4779 || h->type == STT_NOTYPE))
4ad4eba5 4780 {
2955ec4c
L
4781 unsigned int type = ELF_ST_TYPE (isym->st_info);
4782
4783 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4784 symbol. */
4785 if (type == STT_GNU_IFUNC
4786 && (abfd->flags & DYNAMIC) != 0)
4787 type = STT_FUNC;
4ad4eba5 4788
2955ec4c
L
4789 if (h->type != type)
4790 {
4791 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4792 /* xgettext:c-format */
4eca0228 4793 _bfd_error_handler
2955ec4c
L
4794 (_("Warning: type of symbol `%s' changed"
4795 " from %d to %d in %B"),
c08bb8dd 4796 name, h->type, type, abfd);
2955ec4c
L
4797
4798 h->type = type;
4799 }
4ad4eba5
AM
4800 }
4801
54ac0771 4802 /* Merge st_other field. */
b8417128 4803 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4804
c3df8c14 4805 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4806 if (definition
4807 && (sec->flags & SEC_DEBUGGING)
4808 && !bfd_link_relocatable (info))
c3df8c14
AM
4809 dynsym = FALSE;
4810
4f3fedcf
AM
4811 /* Nor should we make plugin symbols dynamic. */
4812 if ((abfd->flags & BFD_PLUGIN) != 0)
4813 dynsym = FALSE;
4814
35fc36a8 4815 if (definition)
35399224
L
4816 {
4817 h->target_internal = isym->st_target_internal;
4818 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4819 }
35fc36a8 4820
4ad4eba5
AM
4821 if (definition && !dynamic)
4822 {
4823 char *p = strchr (name, ELF_VER_CHR);
4824 if (p != NULL && p[1] != ELF_VER_CHR)
4825 {
4826 /* Queue non-default versions so that .symver x, x@FOO
4827 aliases can be checked. */
66eb6687 4828 if (!nondeflt_vers)
4ad4eba5 4829 {
66eb6687
AM
4830 amt = ((isymend - isym + 1)
4831 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4832 nondeflt_vers
4833 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4834 if (!nondeflt_vers)
4835 goto error_free_vers;
4ad4eba5 4836 }
66eb6687 4837 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4838 }
4839 }
4840
4841 if (dynsym && h->dynindx == -1)
4842 {
c152c796 4843 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4844 goto error_free_vers;
f6e332e6 4845 if (h->u.weakdef != NULL
4ad4eba5 4846 && ! new_weakdef
f6e332e6 4847 && h->u.weakdef->dynindx == -1)
4ad4eba5 4848 {
66eb6687 4849 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4850 goto error_free_vers;
4851 }
4852 }
1f599d0e 4853 else if (h->dynindx != -1)
4ad4eba5
AM
4854 /* If the symbol already has a dynamic index, but
4855 visibility says it should not be visible, turn it into
4856 a local symbol. */
4857 switch (ELF_ST_VISIBILITY (h->other))
4858 {
4859 case STV_INTERNAL:
4860 case STV_HIDDEN:
4861 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4862 dynsym = FALSE;
4863 break;
4864 }
4865
aef28989
L
4866 /* Don't add DT_NEEDED for references from the dummy bfd nor
4867 for unmatched symbol. */
4ad4eba5 4868 if (!add_needed
aef28989 4869 && matched
4ad4eba5 4870 && definition
010e5ae2 4871 && ((dynsym
ffa9430d 4872 && h->ref_regular_nonweak
4f3fedcf
AM
4873 && (old_bfd == NULL
4874 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4875 || (h->ref_dynamic_nonweak
010e5ae2 4876 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4877 && !on_needed_list (elf_dt_name (abfd),
4878 htab->needed, NULL))))
4ad4eba5
AM
4879 {
4880 int ret;
4881 const char *soname = elf_dt_name (abfd);
4882
16e4ecc0
AM
4883 info->callbacks->minfo ("%!", soname, old_bfd,
4884 h->root.root.string);
4885
4ad4eba5
AM
4886 /* A symbol from a library loaded via DT_NEEDED of some
4887 other library is referenced by a regular object.
e56f61be 4888 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4889 --no-add-needed is used and the reference was not
4890 a weak one. */
4f3fedcf 4891 if (old_bfd != NULL
b918acf9 4892 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4893 {
4eca0228 4894 _bfd_error_handler
695344c0 4895 /* xgettext:c-format */
3cbc5de0 4896 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4897 old_bfd, name);
ff5ac77b 4898 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4899 goto error_free_vers;
4900 }
4901
a50b1753 4902 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4903 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4904
4ad4eba5 4905 add_needed = TRUE;
7e9f0867 4906 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4907 if (ret < 0)
4908 goto error_free_vers;
4909
4910 BFD_ASSERT (ret == 0);
4911 }
4912 }
4913 }
4914
66eb6687
AM
4915 if (extversym != NULL)
4916 {
4917 free (extversym);
4918 extversym = NULL;
4919 }
4920
4921 if (isymbuf != NULL)
4922 {
4923 free (isymbuf);
4924 isymbuf = NULL;
4925 }
4926
4927 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4928 {
4929 unsigned int i;
4930
4931 /* Restore the symbol table. */
f45794cb
AM
4932 old_ent = (char *) old_tab + tabsize;
4933 memset (elf_sym_hashes (abfd), 0,
4934 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4935 htab->root.table.table = old_table;
4936 htab->root.table.size = old_size;
4937 htab->root.table.count = old_count;
66eb6687 4938 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4939 htab->root.undefs = old_undefs;
4940 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4941 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4942 free (old_strtab);
4943 old_strtab = NULL;
66eb6687
AM
4944 for (i = 0; i < htab->root.table.size; i++)
4945 {
4946 struct bfd_hash_entry *p;
4947 struct elf_link_hash_entry *h;
3e0882af
L
4948 bfd_size_type size;
4949 unsigned int alignment_power;
4070765b 4950 unsigned int non_ir_ref_dynamic;
66eb6687
AM
4951
4952 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4953 {
4954 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4955 if (h->root.type == bfd_link_hash_warning)
4956 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4957
3e0882af
L
4958 /* Preserve the maximum alignment and size for common
4959 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4960 since it can still be loaded at run time by another
3e0882af
L
4961 dynamic lib. */
4962 if (h->root.type == bfd_link_hash_common)
4963 {
4964 size = h->root.u.c.size;
4965 alignment_power = h->root.u.c.p->alignment_power;
4966 }
4967 else
4968 {
4969 size = 0;
4970 alignment_power = 0;
4971 }
4070765b 4972 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
4973 will be exported when the dynamic lib becomes needed
4974 in the second pass. */
4070765b 4975 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
4976 memcpy (p, old_ent, htab->root.table.entsize);
4977 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4978 h = (struct elf_link_hash_entry *) p;
4979 if (h->root.type == bfd_link_hash_warning)
4980 {
4981 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4982 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4983 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4984 }
a4542f1b 4985 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4986 {
4987 if (size > h->root.u.c.size)
4988 h->root.u.c.size = size;
4989 if (alignment_power > h->root.u.c.p->alignment_power)
4990 h->root.u.c.p->alignment_power = alignment_power;
4991 }
4070765b 4992 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
4993 }
4994 }
4995
5061a885
AM
4996 /* Make a special call to the linker "notice" function to
4997 tell it that symbols added for crefs may need to be removed. */
e5034e59 4998 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4999 goto error_free_vers;
5061a885 5000
66eb6687
AM
5001 free (old_tab);
5002 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5003 alloc_mark);
5004 if (nondeflt_vers != NULL)
5005 free (nondeflt_vers);
5006 return TRUE;
5007 }
2de92251 5008
66eb6687
AM
5009 if (old_tab != NULL)
5010 {
e5034e59 5011 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5012 goto error_free_vers;
66eb6687
AM
5013 free (old_tab);
5014 old_tab = NULL;
5015 }
5016
c6e8a9a8
L
5017 /* Now that all the symbols from this input file are created, if
5018 not performing a relocatable link, handle .symver foo, foo@BAR
5019 such that any relocs against foo become foo@BAR. */
0e1862bb 5020 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5021 {
ef53be89 5022 size_t cnt, symidx;
4ad4eba5
AM
5023
5024 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5025 {
5026 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5027 char *shortname, *p;
5028
5029 p = strchr (h->root.root.string, ELF_VER_CHR);
5030 if (p == NULL
5031 || (h->root.type != bfd_link_hash_defined
5032 && h->root.type != bfd_link_hash_defweak))
5033 continue;
5034
5035 amt = p - h->root.root.string;
a50b1753 5036 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5037 if (!shortname)
5038 goto error_free_vers;
4ad4eba5
AM
5039 memcpy (shortname, h->root.root.string, amt);
5040 shortname[amt] = '\0';
5041
5042 hi = (struct elf_link_hash_entry *)
66eb6687 5043 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5044 FALSE, FALSE, FALSE);
5045 if (hi != NULL
5046 && hi->root.type == h->root.type
5047 && hi->root.u.def.value == h->root.u.def.value
5048 && hi->root.u.def.section == h->root.u.def.section)
5049 {
5050 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5051 hi->root.type = bfd_link_hash_indirect;
5052 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5053 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5054 sym_hash = elf_sym_hashes (abfd);
5055 if (sym_hash)
5056 for (symidx = 0; symidx < extsymcount; ++symidx)
5057 if (sym_hash[symidx] == hi)
5058 {
5059 sym_hash[symidx] = h;
5060 break;
5061 }
5062 }
5063 free (shortname);
5064 }
5065 free (nondeflt_vers);
5066 nondeflt_vers = NULL;
5067 }
5068
4ad4eba5
AM
5069 /* Now set the weakdefs field correctly for all the weak defined
5070 symbols we found. The only way to do this is to search all the
5071 symbols. Since we only need the information for non functions in
5072 dynamic objects, that's the only time we actually put anything on
5073 the list WEAKS. We need this information so that if a regular
5074 object refers to a symbol defined weakly in a dynamic object, the
5075 real symbol in the dynamic object is also put in the dynamic
5076 symbols; we also must arrange for both symbols to point to the
5077 same memory location. We could handle the general case of symbol
5078 aliasing, but a general symbol alias can only be generated in
5079 assembler code, handling it correctly would be very time
5080 consuming, and other ELF linkers don't handle general aliasing
5081 either. */
5082 if (weaks != NULL)
5083 {
5084 struct elf_link_hash_entry **hpp;
5085 struct elf_link_hash_entry **hppend;
5086 struct elf_link_hash_entry **sorted_sym_hash;
5087 struct elf_link_hash_entry *h;
5088 size_t sym_count;
5089
5090 /* Since we have to search the whole symbol list for each weak
5091 defined symbol, search time for N weak defined symbols will be
5092 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5093 amt = extsymcount;
5094 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5095 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5096 if (sorted_sym_hash == NULL)
5097 goto error_return;
5098 sym_hash = sorted_sym_hash;
5099 hpp = elf_sym_hashes (abfd);
5100 hppend = hpp + extsymcount;
5101 sym_count = 0;
5102 for (; hpp < hppend; hpp++)
5103 {
5104 h = *hpp;
5105 if (h != NULL
5106 && h->root.type == bfd_link_hash_defined
fcb93ecf 5107 && !bed->is_function_type (h->type))
4ad4eba5
AM
5108 {
5109 *sym_hash = h;
5110 sym_hash++;
5111 sym_count++;
5112 }
5113 }
5114
5115 qsort (sorted_sym_hash, sym_count,
5116 sizeof (struct elf_link_hash_entry *),
5117 elf_sort_symbol);
5118
5119 while (weaks != NULL)
5120 {
5121 struct elf_link_hash_entry *hlook;
5122 asection *slook;
5123 bfd_vma vlook;
ed54588d 5124 size_t i, j, idx = 0;
4ad4eba5
AM
5125
5126 hlook = weaks;
f6e332e6
AM
5127 weaks = hlook->u.weakdef;
5128 hlook->u.weakdef = NULL;
4ad4eba5
AM
5129
5130 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5131 || hlook->root.type == bfd_link_hash_defweak
5132 || hlook->root.type == bfd_link_hash_common
5133 || hlook->root.type == bfd_link_hash_indirect);
5134 slook = hlook->root.u.def.section;
5135 vlook = hlook->root.u.def.value;
5136
4ad4eba5
AM
5137 i = 0;
5138 j = sym_count;
14160578 5139 while (i != j)
4ad4eba5
AM
5140 {
5141 bfd_signed_vma vdiff;
5142 idx = (i + j) / 2;
14160578 5143 h = sorted_sym_hash[idx];
4ad4eba5
AM
5144 vdiff = vlook - h->root.u.def.value;
5145 if (vdiff < 0)
5146 j = idx;
5147 else if (vdiff > 0)
5148 i = idx + 1;
5149 else
5150 {
d3435ae8 5151 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5152 if (sdiff < 0)
5153 j = idx;
5154 else if (sdiff > 0)
5155 i = idx + 1;
5156 else
14160578 5157 break;
4ad4eba5
AM
5158 }
5159 }
5160
5161 /* We didn't find a value/section match. */
14160578 5162 if (i == j)
4ad4eba5
AM
5163 continue;
5164
14160578
AM
5165 /* With multiple aliases, or when the weak symbol is already
5166 strongly defined, we have multiple matching symbols and
5167 the binary search above may land on any of them. Step
5168 one past the matching symbol(s). */
5169 while (++idx != j)
5170 {
5171 h = sorted_sym_hash[idx];
5172 if (h->root.u.def.section != slook
5173 || h->root.u.def.value != vlook)
5174 break;
5175 }
5176
5177 /* Now look back over the aliases. Since we sorted by size
5178 as well as value and section, we'll choose the one with
5179 the largest size. */
5180 while (idx-- != i)
4ad4eba5 5181 {
14160578 5182 h = sorted_sym_hash[idx];
4ad4eba5
AM
5183
5184 /* Stop if value or section doesn't match. */
14160578
AM
5185 if (h->root.u.def.section != slook
5186 || h->root.u.def.value != vlook)
4ad4eba5
AM
5187 break;
5188 else if (h != hlook)
5189 {
f6e332e6 5190 hlook->u.weakdef = h;
4ad4eba5
AM
5191
5192 /* If the weak definition is in the list of dynamic
5193 symbols, make sure the real definition is put
5194 there as well. */
5195 if (hlook->dynindx != -1 && h->dynindx == -1)
5196 {
c152c796 5197 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5198 {
5199 err_free_sym_hash:
5200 free (sorted_sym_hash);
5201 goto error_return;
5202 }
4ad4eba5
AM
5203 }
5204
5205 /* If the real definition is in the list of dynamic
5206 symbols, make sure the weak definition is put
5207 there as well. If we don't do this, then the
5208 dynamic loader might not merge the entries for the
5209 real definition and the weak definition. */
5210 if (h->dynindx != -1 && hlook->dynindx == -1)
5211 {
c152c796 5212 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5213 goto err_free_sym_hash;
4ad4eba5
AM
5214 }
5215 break;
5216 }
5217 }
5218 }
5219
5220 free (sorted_sym_hash);
5221 }
5222
33177bb1
AM
5223 if (bed->check_directives
5224 && !(*bed->check_directives) (abfd, info))
5225 return FALSE;
85fbca6a 5226
d9689752
L
5227 if (!info->check_relocs_after_open_input
5228 && !_bfd_elf_link_check_relocs (abfd, info))
5229 return FALSE;
4ad4eba5
AM
5230
5231 /* If this is a non-traditional link, try to optimize the handling
5232 of the .stab/.stabstr sections. */
5233 if (! dynamic
5234 && ! info->traditional_format
66eb6687 5235 && is_elf_hash_table (htab)
4ad4eba5
AM
5236 && (info->strip != strip_all && info->strip != strip_debugger))
5237 {
5238 asection *stabstr;
5239
5240 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5241 if (stabstr != NULL)
5242 {
5243 bfd_size_type string_offset = 0;
5244 asection *stab;
5245
5246 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5247 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5248 && (!stab->name[5] ||
5249 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5250 && (stab->flags & SEC_MERGE) == 0
5251 && !bfd_is_abs_section (stab->output_section))
5252 {
5253 struct bfd_elf_section_data *secdata;
5254
5255 secdata = elf_section_data (stab);
66eb6687
AM
5256 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5257 stabstr, &secdata->sec_info,
4ad4eba5
AM
5258 &string_offset))
5259 goto error_return;
5260 if (secdata->sec_info)
dbaa2011 5261 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5262 }
5263 }
5264 }
5265
66eb6687 5266 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5267 {
5268 /* Add this bfd to the loaded list. */
5269 struct elf_link_loaded_list *n;
5270
ca4be51c 5271 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5272 if (n == NULL)
5273 goto error_return;
5274 n->abfd = abfd;
66eb6687
AM
5275 n->next = htab->loaded;
5276 htab->loaded = n;
4ad4eba5
AM
5277 }
5278
5279 return TRUE;
5280
5281 error_free_vers:
66eb6687
AM
5282 if (old_tab != NULL)
5283 free (old_tab);
5b677558
AM
5284 if (old_strtab != NULL)
5285 free (old_strtab);
4ad4eba5
AM
5286 if (nondeflt_vers != NULL)
5287 free (nondeflt_vers);
5288 if (extversym != NULL)
5289 free (extversym);
5290 error_free_sym:
5291 if (isymbuf != NULL)
5292 free (isymbuf);
5293 error_return:
5294 return FALSE;
5295}
5296
8387904d
AM
5297/* Return the linker hash table entry of a symbol that might be
5298 satisfied by an archive symbol. Return -1 on error. */
5299
5300struct elf_link_hash_entry *
5301_bfd_elf_archive_symbol_lookup (bfd *abfd,
5302 struct bfd_link_info *info,
5303 const char *name)
5304{
5305 struct elf_link_hash_entry *h;
5306 char *p, *copy;
5307 size_t len, first;
5308
2a41f396 5309 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5310 if (h != NULL)
5311 return h;
5312
5313 /* If this is a default version (the name contains @@), look up the
5314 symbol again with only one `@' as well as without the version.
5315 The effect is that references to the symbol with and without the
5316 version will be matched by the default symbol in the archive. */
5317
5318 p = strchr (name, ELF_VER_CHR);
5319 if (p == NULL || p[1] != ELF_VER_CHR)
5320 return h;
5321
5322 /* First check with only one `@'. */
5323 len = strlen (name);
a50b1753 5324 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5325 if (copy == NULL)
5326 return (struct elf_link_hash_entry *) 0 - 1;
5327
5328 first = p - name + 1;
5329 memcpy (copy, name, first);
5330 memcpy (copy + first, name + first + 1, len - first);
5331
2a41f396 5332 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5333 if (h == NULL)
5334 {
5335 /* We also need to check references to the symbol without the
5336 version. */
5337 copy[first - 1] = '\0';
5338 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5339 FALSE, FALSE, TRUE);
8387904d
AM
5340 }
5341
5342 bfd_release (abfd, copy);
5343 return h;
5344}
5345
0ad989f9 5346/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5347 don't use _bfd_generic_link_add_archive_symbols because we need to
5348 handle versioned symbols.
0ad989f9
L
5349
5350 Fortunately, ELF archive handling is simpler than that done by
5351 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5352 oddities. In ELF, if we find a symbol in the archive map, and the
5353 symbol is currently undefined, we know that we must pull in that
5354 object file.
5355
5356 Unfortunately, we do have to make multiple passes over the symbol
5357 table until nothing further is resolved. */
5358
4ad4eba5
AM
5359static bfd_boolean
5360elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5361{
5362 symindex c;
13e570f8 5363 unsigned char *included = NULL;
0ad989f9
L
5364 carsym *symdefs;
5365 bfd_boolean loop;
5366 bfd_size_type amt;
8387904d
AM
5367 const struct elf_backend_data *bed;
5368 struct elf_link_hash_entry * (*archive_symbol_lookup)
5369 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5370
5371 if (! bfd_has_map (abfd))
5372 {
5373 /* An empty archive is a special case. */
5374 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5375 return TRUE;
5376 bfd_set_error (bfd_error_no_armap);
5377 return FALSE;
5378 }
5379
5380 /* Keep track of all symbols we know to be already defined, and all
5381 files we know to be already included. This is to speed up the
5382 second and subsequent passes. */
5383 c = bfd_ardata (abfd)->symdef_count;
5384 if (c == 0)
5385 return TRUE;
5386 amt = c;
13e570f8
AM
5387 amt *= sizeof (*included);
5388 included = (unsigned char *) bfd_zmalloc (amt);
5389 if (included == NULL)
5390 return FALSE;
0ad989f9
L
5391
5392 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5393 bed = get_elf_backend_data (abfd);
5394 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5395
5396 do
5397 {
5398 file_ptr last;
5399 symindex i;
5400 carsym *symdef;
5401 carsym *symdefend;
5402
5403 loop = FALSE;
5404 last = -1;
5405
5406 symdef = symdefs;
5407 symdefend = symdef + c;
5408 for (i = 0; symdef < symdefend; symdef++, i++)
5409 {
5410 struct elf_link_hash_entry *h;
5411 bfd *element;
5412 struct bfd_link_hash_entry *undefs_tail;
5413 symindex mark;
5414
13e570f8 5415 if (included[i])
0ad989f9
L
5416 continue;
5417 if (symdef->file_offset == last)
5418 {
5419 included[i] = TRUE;
5420 continue;
5421 }
5422
8387904d
AM
5423 h = archive_symbol_lookup (abfd, info, symdef->name);
5424 if (h == (struct elf_link_hash_entry *) 0 - 1)
5425 goto error_return;
0ad989f9
L
5426
5427 if (h == NULL)
5428 continue;
5429
5430 if (h->root.type == bfd_link_hash_common)
5431 {
5432 /* We currently have a common symbol. The archive map contains
5433 a reference to this symbol, so we may want to include it. We
5434 only want to include it however, if this archive element
5435 contains a definition of the symbol, not just another common
5436 declaration of it.
5437
5438 Unfortunately some archivers (including GNU ar) will put
5439 declarations of common symbols into their archive maps, as
5440 well as real definitions, so we cannot just go by the archive
5441 map alone. Instead we must read in the element's symbol
5442 table and check that to see what kind of symbol definition
5443 this is. */
5444 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5445 continue;
5446 }
5447 else if (h->root.type != bfd_link_hash_undefined)
5448 {
5449 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5450 /* Symbol must be defined. Don't check it again. */
5451 included[i] = TRUE;
0ad989f9
L
5452 continue;
5453 }
5454
5455 /* We need to include this archive member. */
5456 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5457 if (element == NULL)
5458 goto error_return;
5459
5460 if (! bfd_check_format (element, bfd_object))
5461 goto error_return;
5462
0ad989f9
L
5463 undefs_tail = info->hash->undefs_tail;
5464
0e144ba7
AM
5465 if (!(*info->callbacks
5466 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5467 continue;
0e144ba7 5468 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5469 goto error_return;
5470
5471 /* If there are any new undefined symbols, we need to make
5472 another pass through the archive in order to see whether
5473 they can be defined. FIXME: This isn't perfect, because
5474 common symbols wind up on undefs_tail and because an
5475 undefined symbol which is defined later on in this pass
5476 does not require another pass. This isn't a bug, but it
5477 does make the code less efficient than it could be. */
5478 if (undefs_tail != info->hash->undefs_tail)
5479 loop = TRUE;
5480
5481 /* Look backward to mark all symbols from this object file
5482 which we have already seen in this pass. */
5483 mark = i;
5484 do
5485 {
5486 included[mark] = TRUE;
5487 if (mark == 0)
5488 break;
5489 --mark;
5490 }
5491 while (symdefs[mark].file_offset == symdef->file_offset);
5492
5493 /* We mark subsequent symbols from this object file as we go
5494 on through the loop. */
5495 last = symdef->file_offset;
5496 }
5497 }
5498 while (loop);
5499
0ad989f9
L
5500 free (included);
5501
5502 return TRUE;
5503
5504 error_return:
0ad989f9
L
5505 if (included != NULL)
5506 free (included);
5507 return FALSE;
5508}
4ad4eba5
AM
5509
5510/* Given an ELF BFD, add symbols to the global hash table as
5511 appropriate. */
5512
5513bfd_boolean
5514bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5515{
5516 switch (bfd_get_format (abfd))
5517 {
5518 case bfd_object:
5519 return elf_link_add_object_symbols (abfd, info);
5520 case bfd_archive:
5521 return elf_link_add_archive_symbols (abfd, info);
5522 default:
5523 bfd_set_error (bfd_error_wrong_format);
5524 return FALSE;
5525 }
5526}
5a580b3a 5527\f
14b1c01e
AM
5528struct hash_codes_info
5529{
5530 unsigned long *hashcodes;
5531 bfd_boolean error;
5532};
a0c8462f 5533
5a580b3a
AM
5534/* This function will be called though elf_link_hash_traverse to store
5535 all hash value of the exported symbols in an array. */
5536
5537static bfd_boolean
5538elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5539{
a50b1753 5540 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5541 const char *name;
5a580b3a
AM
5542 unsigned long ha;
5543 char *alc = NULL;
5544
5a580b3a
AM
5545 /* Ignore indirect symbols. These are added by the versioning code. */
5546 if (h->dynindx == -1)
5547 return TRUE;
5548
5549 name = h->root.root.string;
422f1182 5550 if (h->versioned >= versioned)
5a580b3a 5551 {
422f1182
L
5552 char *p = strchr (name, ELF_VER_CHR);
5553 if (p != NULL)
14b1c01e 5554 {
422f1182
L
5555 alc = (char *) bfd_malloc (p - name + 1);
5556 if (alc == NULL)
5557 {
5558 inf->error = TRUE;
5559 return FALSE;
5560 }
5561 memcpy (alc, name, p - name);
5562 alc[p - name] = '\0';
5563 name = alc;
14b1c01e 5564 }
5a580b3a
AM
5565 }
5566
5567 /* Compute the hash value. */
5568 ha = bfd_elf_hash (name);
5569
5570 /* Store the found hash value in the array given as the argument. */
14b1c01e 5571 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5572
5573 /* And store it in the struct so that we can put it in the hash table
5574 later. */
f6e332e6 5575 h->u.elf_hash_value = ha;
5a580b3a
AM
5576
5577 if (alc != NULL)
5578 free (alc);
5579
5580 return TRUE;
5581}
5582
fdc90cb4
JJ
5583struct collect_gnu_hash_codes
5584{
5585 bfd *output_bfd;
5586 const struct elf_backend_data *bed;
5587 unsigned long int nsyms;
5588 unsigned long int maskbits;
5589 unsigned long int *hashcodes;
5590 unsigned long int *hashval;
5591 unsigned long int *indx;
5592 unsigned long int *counts;
5593 bfd_vma *bitmask;
5594 bfd_byte *contents;
5595 long int min_dynindx;
5596 unsigned long int bucketcount;
5597 unsigned long int symindx;
5598 long int local_indx;
5599 long int shift1, shift2;
5600 unsigned long int mask;
14b1c01e 5601 bfd_boolean error;
fdc90cb4
JJ
5602};
5603
5604/* This function will be called though elf_link_hash_traverse to store
5605 all hash value of the exported symbols in an array. */
5606
5607static bfd_boolean
5608elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5609{
a50b1753 5610 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5611 const char *name;
fdc90cb4
JJ
5612 unsigned long ha;
5613 char *alc = NULL;
5614
fdc90cb4
JJ
5615 /* Ignore indirect symbols. These are added by the versioning code. */
5616 if (h->dynindx == -1)
5617 return TRUE;
5618
5619 /* Ignore also local symbols and undefined symbols. */
5620 if (! (*s->bed->elf_hash_symbol) (h))
5621 return TRUE;
5622
5623 name = h->root.root.string;
422f1182 5624 if (h->versioned >= versioned)
fdc90cb4 5625 {
422f1182
L
5626 char *p = strchr (name, ELF_VER_CHR);
5627 if (p != NULL)
14b1c01e 5628 {
422f1182
L
5629 alc = (char *) bfd_malloc (p - name + 1);
5630 if (alc == NULL)
5631 {
5632 s->error = TRUE;
5633 return FALSE;
5634 }
5635 memcpy (alc, name, p - name);
5636 alc[p - name] = '\0';
5637 name = alc;
14b1c01e 5638 }
fdc90cb4
JJ
5639 }
5640
5641 /* Compute the hash value. */
5642 ha = bfd_elf_gnu_hash (name);
5643
5644 /* Store the found hash value in the array for compute_bucket_count,
5645 and also for .dynsym reordering purposes. */
5646 s->hashcodes[s->nsyms] = ha;
5647 s->hashval[h->dynindx] = ha;
5648 ++s->nsyms;
5649 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5650 s->min_dynindx = h->dynindx;
5651
5652 if (alc != NULL)
5653 free (alc);
5654
5655 return TRUE;
5656}
5657
5658/* This function will be called though elf_link_hash_traverse to do
5659 final dynaminc symbol renumbering. */
5660
5661static bfd_boolean
5662elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5663{
a50b1753 5664 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5665 unsigned long int bucket;
5666 unsigned long int val;
5667
fdc90cb4
JJ
5668 /* Ignore indirect symbols. */
5669 if (h->dynindx == -1)
5670 return TRUE;
5671
5672 /* Ignore also local symbols and undefined symbols. */
5673 if (! (*s->bed->elf_hash_symbol) (h))
5674 {
5675 if (h->dynindx >= s->min_dynindx)
5676 h->dynindx = s->local_indx++;
5677 return TRUE;
5678 }
5679
5680 bucket = s->hashval[h->dynindx] % s->bucketcount;
5681 val = (s->hashval[h->dynindx] >> s->shift1)
5682 & ((s->maskbits >> s->shift1) - 1);
5683 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5684 s->bitmask[val]
5685 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5686 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5687 if (s->counts[bucket] == 1)
5688 /* Last element terminates the chain. */
5689 val |= 1;
5690 bfd_put_32 (s->output_bfd, val,
5691 s->contents + (s->indx[bucket] - s->symindx) * 4);
5692 --s->counts[bucket];
5693 h->dynindx = s->indx[bucket]++;
5694 return TRUE;
5695}
5696
5697/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5698
5699bfd_boolean
5700_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5701{
5702 return !(h->forced_local
5703 || h->root.type == bfd_link_hash_undefined
5704 || h->root.type == bfd_link_hash_undefweak
5705 || ((h->root.type == bfd_link_hash_defined
5706 || h->root.type == bfd_link_hash_defweak)
5707 && h->root.u.def.section->output_section == NULL));
5708}
5709
5a580b3a
AM
5710/* Array used to determine the number of hash table buckets to use
5711 based on the number of symbols there are. If there are fewer than
5712 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5713 fewer than 37 we use 17 buckets, and so forth. We never use more
5714 than 32771 buckets. */
5715
5716static const size_t elf_buckets[] =
5717{
5718 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5719 16411, 32771, 0
5720};
5721
5722/* Compute bucket count for hashing table. We do not use a static set
5723 of possible tables sizes anymore. Instead we determine for all
5724 possible reasonable sizes of the table the outcome (i.e., the
5725 number of collisions etc) and choose the best solution. The
5726 weighting functions are not too simple to allow the table to grow
5727 without bounds. Instead one of the weighting factors is the size.
5728 Therefore the result is always a good payoff between few collisions
5729 (= short chain lengths) and table size. */
5730static size_t
b20dd2ce 5731compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5732 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5733 unsigned long int nsyms,
5734 int gnu_hash)
5a580b3a 5735{
5a580b3a 5736 size_t best_size = 0;
5a580b3a 5737 unsigned long int i;
5a580b3a 5738
5a580b3a
AM
5739 /* We have a problem here. The following code to optimize the table
5740 size requires an integer type with more the 32 bits. If
5741 BFD_HOST_U_64_BIT is set we know about such a type. */
5742#ifdef BFD_HOST_U_64_BIT
5743 if (info->optimize)
5744 {
5a580b3a
AM
5745 size_t minsize;
5746 size_t maxsize;
5747 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5748 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5749 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5750 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5751 unsigned long int *counts;
d40f3da9 5752 bfd_size_type amt;
0883b6e0 5753 unsigned int no_improvement_count = 0;
5a580b3a
AM
5754
5755 /* Possible optimization parameters: if we have NSYMS symbols we say
5756 that the hashing table must at least have NSYMS/4 and at most
5757 2*NSYMS buckets. */
5758 minsize = nsyms / 4;
5759 if (minsize == 0)
5760 minsize = 1;
5761 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5762 if (gnu_hash)
5763 {
5764 if (minsize < 2)
5765 minsize = 2;
5766 if ((best_size & 31) == 0)
5767 ++best_size;
5768 }
5a580b3a
AM
5769
5770 /* Create array where we count the collisions in. We must use bfd_malloc
5771 since the size could be large. */
5772 amt = maxsize;
5773 amt *= sizeof (unsigned long int);
a50b1753 5774 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5775 if (counts == NULL)
fdc90cb4 5776 return 0;
5a580b3a
AM
5777
5778 /* Compute the "optimal" size for the hash table. The criteria is a
5779 minimal chain length. The minor criteria is (of course) the size
5780 of the table. */
5781 for (i = minsize; i < maxsize; ++i)
5782 {
5783 /* Walk through the array of hashcodes and count the collisions. */
5784 BFD_HOST_U_64_BIT max;
5785 unsigned long int j;
5786 unsigned long int fact;
5787
fdc90cb4
JJ
5788 if (gnu_hash && (i & 31) == 0)
5789 continue;
5790
5a580b3a
AM
5791 memset (counts, '\0', i * sizeof (unsigned long int));
5792
5793 /* Determine how often each hash bucket is used. */
5794 for (j = 0; j < nsyms; ++j)
5795 ++counts[hashcodes[j] % i];
5796
5797 /* For the weight function we need some information about the
5798 pagesize on the target. This is information need not be 100%
5799 accurate. Since this information is not available (so far) we
5800 define it here to a reasonable default value. If it is crucial
5801 to have a better value some day simply define this value. */
5802# ifndef BFD_TARGET_PAGESIZE
5803# define BFD_TARGET_PAGESIZE (4096)
5804# endif
5805
fdc90cb4
JJ
5806 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5807 and the chains. */
5808 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5809
5810# if 1
5811 /* Variant 1: optimize for short chains. We add the squares
5812 of all the chain lengths (which favors many small chain
5813 over a few long chains). */
5814 for (j = 0; j < i; ++j)
5815 max += counts[j] * counts[j];
5816
5817 /* This adds penalties for the overall size of the table. */
fdc90cb4 5818 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5819 max *= fact * fact;
5820# else
5821 /* Variant 2: Optimize a lot more for small table. Here we
5822 also add squares of the size but we also add penalties for
5823 empty slots (the +1 term). */
5824 for (j = 0; j < i; ++j)
5825 max += (1 + counts[j]) * (1 + counts[j]);
5826
5827 /* The overall size of the table is considered, but not as
5828 strong as in variant 1, where it is squared. */
fdc90cb4 5829 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5830 max *= fact;
5831# endif
5832
5833 /* Compare with current best results. */
5834 if (max < best_chlen)
5835 {
5836 best_chlen = max;
5837 best_size = i;
ca4be51c 5838 no_improvement_count = 0;
5a580b3a 5839 }
0883b6e0
NC
5840 /* PR 11843: Avoid futile long searches for the best bucket size
5841 when there are a large number of symbols. */
5842 else if (++no_improvement_count == 100)
5843 break;
5a580b3a
AM
5844 }
5845
5846 free (counts);
5847 }
5848 else
5849#endif /* defined (BFD_HOST_U_64_BIT) */
5850 {
5851 /* This is the fallback solution if no 64bit type is available or if we
5852 are not supposed to spend much time on optimizations. We select the
5853 bucket count using a fixed set of numbers. */
5854 for (i = 0; elf_buckets[i] != 0; i++)
5855 {
5856 best_size = elf_buckets[i];
fdc90cb4 5857 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5858 break;
5859 }
fdc90cb4
JJ
5860 if (gnu_hash && best_size < 2)
5861 best_size = 2;
5a580b3a
AM
5862 }
5863
5a580b3a
AM
5864 return best_size;
5865}
5866
d0bf826b
AM
5867/* Size any SHT_GROUP section for ld -r. */
5868
5869bfd_boolean
5870_bfd_elf_size_group_sections (struct bfd_link_info *info)
5871{
5872 bfd *ibfd;
57963c05 5873 asection *s;
d0bf826b 5874
c72f2fb2 5875 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 5876 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
5877 && (s = ibfd->sections) != NULL
5878 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
5879 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5880 return FALSE;
5881 return TRUE;
5882}
5883
04c3a755
NS
5884/* Set a default stack segment size. The value in INFO wins. If it
5885 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5886 undefined it is initialized. */
5887
5888bfd_boolean
5889bfd_elf_stack_segment_size (bfd *output_bfd,
5890 struct bfd_link_info *info,
5891 const char *legacy_symbol,
5892 bfd_vma default_size)
5893{
5894 struct elf_link_hash_entry *h = NULL;
5895
5896 /* Look for legacy symbol. */
5897 if (legacy_symbol)
5898 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5899 FALSE, FALSE, FALSE);
5900 if (h && (h->root.type == bfd_link_hash_defined
5901 || h->root.type == bfd_link_hash_defweak)
5902 && h->def_regular
5903 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5904 {
5905 /* The symbol has no type if specified on the command line. */
5906 h->type = STT_OBJECT;
5907 if (info->stacksize)
695344c0 5908 /* xgettext:c-format */
4eca0228
AM
5909 _bfd_error_handler (_("%B: stack size specified and %s set"),
5910 output_bfd, legacy_symbol);
04c3a755 5911 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5912 /* xgettext:c-format */
4eca0228
AM
5913 _bfd_error_handler (_("%B: %s not absolute"),
5914 output_bfd, legacy_symbol);
04c3a755
NS
5915 else
5916 info->stacksize = h->root.u.def.value;
5917 }
5918
5919 if (!info->stacksize)
5920 /* If the user didn't set a size, or explicitly inhibit the
5921 size, set it now. */
5922 info->stacksize = default_size;
5923
5924 /* Provide the legacy symbol, if it is referenced. */
5925 if (h && (h->root.type == bfd_link_hash_undefined
5926 || h->root.type == bfd_link_hash_undefweak))
5927 {
5928 struct bfd_link_hash_entry *bh = NULL;
5929
5930 if (!(_bfd_generic_link_add_one_symbol
5931 (info, output_bfd, legacy_symbol,
5932 BSF_GLOBAL, bfd_abs_section_ptr,
5933 info->stacksize >= 0 ? info->stacksize : 0,
5934 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5935 return FALSE;
5936
5937 h = (struct elf_link_hash_entry *) bh;
5938 h->def_regular = 1;
5939 h->type = STT_OBJECT;
5940 }
5941
5942 return TRUE;
5943}
5944
b531344c
MR
5945/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5946
5947struct elf_gc_sweep_symbol_info
5948{
5949 struct bfd_link_info *info;
5950 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5951 bfd_boolean);
5952};
5953
5954static bfd_boolean
5955elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5956{
5957 if (!h->mark
5958 && (((h->root.type == bfd_link_hash_defined
5959 || h->root.type == bfd_link_hash_defweak)
5960 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5961 && h->root.u.def.section->gc_mark))
5962 || h->root.type == bfd_link_hash_undefined
5963 || h->root.type == bfd_link_hash_undefweak))
5964 {
5965 struct elf_gc_sweep_symbol_info *inf;
5966
5967 inf = (struct elf_gc_sweep_symbol_info *) data;
5968 (*inf->hide_symbol) (inf->info, h, TRUE);
5969 h->def_regular = 0;
5970 h->ref_regular = 0;
5971 h->ref_regular_nonweak = 0;
5972 }
5973
5974 return TRUE;
5975}
5976
5a580b3a
AM
5977/* Set up the sizes and contents of the ELF dynamic sections. This is
5978 called by the ELF linker emulation before_allocation routine. We
5979 must set the sizes of the sections before the linker sets the
5980 addresses of the various sections. */
5981
5982bfd_boolean
5983bfd_elf_size_dynamic_sections (bfd *output_bfd,
5984 const char *soname,
5985 const char *rpath,
5986 const char *filter_shlib,
7ee314fa
AM
5987 const char *audit,
5988 const char *depaudit,
5a580b3a
AM
5989 const char * const *auxiliary_filters,
5990 struct bfd_link_info *info,
fd91d419 5991 asection **sinterpptr)
5a580b3a 5992{
5a580b3a
AM
5993 bfd *dynobj;
5994 const struct elf_backend_data *bed;
5a580b3a
AM
5995
5996 *sinterpptr = NULL;
5997
5a580b3a
AM
5998 if (!is_elf_hash_table (info->hash))
5999 return TRUE;
6000
5a580b3a
AM
6001 dynobj = elf_hash_table (info)->dynobj;
6002
9a2a56cc 6003 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6004 {
902e9fc7
MR
6005 struct bfd_elf_version_tree *verdefs;
6006 struct elf_info_failed asvinfo;
5a580b3a
AM
6007 struct bfd_elf_version_tree *t;
6008 struct bfd_elf_version_expr *d;
902e9fc7 6009 struct elf_info_failed eif;
5a580b3a 6010 bfd_boolean all_defined;
902e9fc7 6011 asection *s;
e6699019 6012 size_t soname_indx;
7ee314fa 6013
5a580b3a 6014 eif.info = info;
5a580b3a
AM
6015 eif.failed = FALSE;
6016
6017 /* If we are supposed to export all symbols into the dynamic symbol
6018 table (this is not the normal case), then do so. */
55255dae 6019 if (info->export_dynamic
0e1862bb 6020 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6021 {
6022 elf_link_hash_traverse (elf_hash_table (info),
6023 _bfd_elf_export_symbol,
6024 &eif);
6025 if (eif.failed)
6026 return FALSE;
6027 }
6028
e6699019
L
6029 if (soname != NULL)
6030 {
6031 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6032 soname, TRUE);
6033 if (soname_indx == (size_t) -1
6034 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6035 return FALSE;
6036 }
6037 else
6038 soname_indx = (size_t) -1;
6039
5a580b3a 6040 /* Make all global versions with definition. */
fd91d419 6041 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6042 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6043 if (!d->symver && d->literal)
5a580b3a
AM
6044 {
6045 const char *verstr, *name;
6046 size_t namelen, verlen, newlen;
93252b1c 6047 char *newname, *p, leading_char;
5a580b3a
AM
6048 struct elf_link_hash_entry *newh;
6049
93252b1c 6050 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6051 name = d->pattern;
93252b1c 6052 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6053 verstr = t->name;
6054 verlen = strlen (verstr);
6055 newlen = namelen + verlen + 3;
6056
a50b1753 6057 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6058 if (newname == NULL)
6059 return FALSE;
93252b1c
MF
6060 newname[0] = leading_char;
6061 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6062
6063 /* Check the hidden versioned definition. */
6064 p = newname + namelen;
6065 *p++ = ELF_VER_CHR;
6066 memcpy (p, verstr, verlen + 1);
6067 newh = elf_link_hash_lookup (elf_hash_table (info),
6068 newname, FALSE, FALSE,
6069 FALSE);
6070 if (newh == NULL
6071 || (newh->root.type != bfd_link_hash_defined
6072 && newh->root.type != bfd_link_hash_defweak))
6073 {
6074 /* Check the default versioned definition. */
6075 *p++ = ELF_VER_CHR;
6076 memcpy (p, verstr, verlen + 1);
6077 newh = elf_link_hash_lookup (elf_hash_table (info),
6078 newname, FALSE, FALSE,
6079 FALSE);
6080 }
6081 free (newname);
6082
6083 /* Mark this version if there is a definition and it is
6084 not defined in a shared object. */
6085 if (newh != NULL
f5385ebf 6086 && !newh->def_dynamic
5a580b3a
AM
6087 && (newh->root.type == bfd_link_hash_defined
6088 || newh->root.type == bfd_link_hash_defweak))
6089 d->symver = 1;
6090 }
6091
6092 /* Attach all the symbols to their version information. */
5a580b3a 6093 asvinfo.info = info;
5a580b3a
AM
6094 asvinfo.failed = FALSE;
6095
6096 elf_link_hash_traverse (elf_hash_table (info),
6097 _bfd_elf_link_assign_sym_version,
6098 &asvinfo);
6099 if (asvinfo.failed)
6100 return FALSE;
6101
6102 if (!info->allow_undefined_version)
6103 {
6104 /* Check if all global versions have a definition. */
6105 all_defined = TRUE;
fd91d419 6106 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6107 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6108 if (d->literal && !d->symver && !d->script)
5a580b3a 6109 {
4eca0228 6110 _bfd_error_handler
5a580b3a
AM
6111 (_("%s: undefined version: %s"),
6112 d->pattern, t->name);
6113 all_defined = FALSE;
6114 }
6115
6116 if (!all_defined)
6117 {
6118 bfd_set_error (bfd_error_bad_value);
6119 return FALSE;
6120 }
6121 }
6122
902e9fc7
MR
6123 /* Set up the version definition section. */
6124 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6125 BFD_ASSERT (s != NULL);
5a580b3a 6126
902e9fc7
MR
6127 /* We may have created additional version definitions if we are
6128 just linking a regular application. */
6129 verdefs = info->version_info;
5a580b3a 6130
902e9fc7
MR
6131 /* Skip anonymous version tag. */
6132 if (verdefs != NULL && verdefs->vernum == 0)
6133 verdefs = verdefs->next;
5a580b3a 6134
902e9fc7
MR
6135 if (verdefs == NULL && !info->create_default_symver)
6136 s->flags |= SEC_EXCLUDE;
6137 else
5a580b3a 6138 {
902e9fc7
MR
6139 unsigned int cdefs;
6140 bfd_size_type size;
6141 bfd_byte *p;
6142 Elf_Internal_Verdef def;
6143 Elf_Internal_Verdaux defaux;
6144 struct bfd_link_hash_entry *bh;
6145 struct elf_link_hash_entry *h;
6146 const char *name;
5a580b3a 6147
902e9fc7
MR
6148 cdefs = 0;
6149 size = 0;
5a580b3a 6150
902e9fc7
MR
6151 /* Make space for the base version. */
6152 size += sizeof (Elf_External_Verdef);
6153 size += sizeof (Elf_External_Verdaux);
6154 ++cdefs;
6155
6156 /* Make space for the default version. */
6157 if (info->create_default_symver)
6158 {
6159 size += sizeof (Elf_External_Verdef);
6160 ++cdefs;
3e3b46e5
PB
6161 }
6162
5a580b3a
AM
6163 for (t = verdefs; t != NULL; t = t->next)
6164 {
6165 struct bfd_elf_version_deps *n;
6166
a6cc6b3b
RO
6167 /* Don't emit base version twice. */
6168 if (t->vernum == 0)
6169 continue;
6170
5a580b3a
AM
6171 size += sizeof (Elf_External_Verdef);
6172 size += sizeof (Elf_External_Verdaux);
6173 ++cdefs;
6174
6175 for (n = t->deps; n != NULL; n = n->next)
6176 size += sizeof (Elf_External_Verdaux);
6177 }
6178
eea6121a 6179 s->size = size;
a50b1753 6180 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6181 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6182 return FALSE;
6183
6184 /* Fill in the version definition section. */
6185
6186 p = s->contents;
6187
6188 def.vd_version = VER_DEF_CURRENT;
6189 def.vd_flags = VER_FLG_BASE;
6190 def.vd_ndx = 1;
6191 def.vd_cnt = 1;
3e3b46e5
PB
6192 if (info->create_default_symver)
6193 {
6194 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6195 def.vd_next = sizeof (Elf_External_Verdef);
6196 }
6197 else
6198 {
6199 def.vd_aux = sizeof (Elf_External_Verdef);
6200 def.vd_next = (sizeof (Elf_External_Verdef)
6201 + sizeof (Elf_External_Verdaux));
6202 }
5a580b3a 6203
ef53be89 6204 if (soname_indx != (size_t) -1)
5a580b3a
AM
6205 {
6206 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6207 soname_indx);
6208 def.vd_hash = bfd_elf_hash (soname);
6209 defaux.vda_name = soname_indx;
3e3b46e5 6210 name = soname;
5a580b3a
AM
6211 }
6212 else
6213 {
ef53be89 6214 size_t indx;
5a580b3a 6215
06084812 6216 name = lbasename (output_bfd->filename);
5a580b3a
AM
6217 def.vd_hash = bfd_elf_hash (name);
6218 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6219 name, FALSE);
ef53be89 6220 if (indx == (size_t) -1)
5a580b3a
AM
6221 return FALSE;
6222 defaux.vda_name = indx;
6223 }
6224 defaux.vda_next = 0;
6225
6226 _bfd_elf_swap_verdef_out (output_bfd, &def,
6227 (Elf_External_Verdef *) p);
6228 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6229 if (info->create_default_symver)
6230 {
6231 /* Add a symbol representing this version. */
6232 bh = NULL;
6233 if (! (_bfd_generic_link_add_one_symbol
6234 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6235 0, NULL, FALSE,
6236 get_elf_backend_data (dynobj)->collect, &bh)))
6237 return FALSE;
6238 h = (struct elf_link_hash_entry *) bh;
6239 h->non_elf = 0;
6240 h->def_regular = 1;
6241 h->type = STT_OBJECT;
6242 h->verinfo.vertree = NULL;
6243
6244 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6245 return FALSE;
6246
6247 /* Create a duplicate of the base version with the same
6248 aux block, but different flags. */
6249 def.vd_flags = 0;
6250 def.vd_ndx = 2;
6251 def.vd_aux = sizeof (Elf_External_Verdef);
6252 if (verdefs)
6253 def.vd_next = (sizeof (Elf_External_Verdef)
6254 + sizeof (Elf_External_Verdaux));
6255 else
6256 def.vd_next = 0;
6257 _bfd_elf_swap_verdef_out (output_bfd, &def,
6258 (Elf_External_Verdef *) p);
6259 p += sizeof (Elf_External_Verdef);
6260 }
5a580b3a
AM
6261 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6262 (Elf_External_Verdaux *) p);
6263 p += sizeof (Elf_External_Verdaux);
6264
6265 for (t = verdefs; t != NULL; t = t->next)
6266 {
6267 unsigned int cdeps;
6268 struct bfd_elf_version_deps *n;
5a580b3a 6269
a6cc6b3b
RO
6270 /* Don't emit the base version twice. */
6271 if (t->vernum == 0)
6272 continue;
6273
5a580b3a
AM
6274 cdeps = 0;
6275 for (n = t->deps; n != NULL; n = n->next)
6276 ++cdeps;
6277
6278 /* Add a symbol representing this version. */
6279 bh = NULL;
6280 if (! (_bfd_generic_link_add_one_symbol
6281 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6282 0, NULL, FALSE,
6283 get_elf_backend_data (dynobj)->collect, &bh)))
6284 return FALSE;
6285 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6286 h->non_elf = 0;
6287 h->def_regular = 1;
5a580b3a
AM
6288 h->type = STT_OBJECT;
6289 h->verinfo.vertree = t;
6290
c152c796 6291 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6292 return FALSE;
6293
6294 def.vd_version = VER_DEF_CURRENT;
6295 def.vd_flags = 0;
6296 if (t->globals.list == NULL
6297 && t->locals.list == NULL
6298 && ! t->used)
6299 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6300 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6301 def.vd_cnt = cdeps + 1;
6302 def.vd_hash = bfd_elf_hash (t->name);
6303 def.vd_aux = sizeof (Elf_External_Verdef);
6304 def.vd_next = 0;
a6cc6b3b
RO
6305
6306 /* If a basever node is next, it *must* be the last node in
6307 the chain, otherwise Verdef construction breaks. */
6308 if (t->next != NULL && t->next->vernum == 0)
6309 BFD_ASSERT (t->next->next == NULL);
6310
6311 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6312 def.vd_next = (sizeof (Elf_External_Verdef)
6313 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6314
6315 _bfd_elf_swap_verdef_out (output_bfd, &def,
6316 (Elf_External_Verdef *) p);
6317 p += sizeof (Elf_External_Verdef);
6318
6319 defaux.vda_name = h->dynstr_index;
6320 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6321 h->dynstr_index);
6322 defaux.vda_next = 0;
6323 if (t->deps != NULL)
6324 defaux.vda_next = sizeof (Elf_External_Verdaux);
6325 t->name_indx = defaux.vda_name;
6326
6327 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6328 (Elf_External_Verdaux *) p);
6329 p += sizeof (Elf_External_Verdaux);
6330
6331 for (n = t->deps; n != NULL; n = n->next)
6332 {
6333 if (n->version_needed == NULL)
6334 {
6335 /* This can happen if there was an error in the
6336 version script. */
6337 defaux.vda_name = 0;
6338 }
6339 else
6340 {
6341 defaux.vda_name = n->version_needed->name_indx;
6342 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6343 defaux.vda_name);
6344 }
6345 if (n->next == NULL)
6346 defaux.vda_next = 0;
6347 else
6348 defaux.vda_next = sizeof (Elf_External_Verdaux);
6349
6350 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6351 (Elf_External_Verdaux *) p);
6352 p += sizeof (Elf_External_Verdaux);
6353 }
6354 }
6355
5a580b3a
AM
6356 elf_tdata (output_bfd)->cverdefs = cdefs;
6357 }
6358
5a580b3a
AM
6359 /* Work out the size of the version reference section. */
6360
3d4d4302 6361 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6362 BFD_ASSERT (s != NULL);
6363 {
6364 struct elf_find_verdep_info sinfo;
6365
5a580b3a
AM
6366 sinfo.info = info;
6367 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6368 if (sinfo.vers == 0)
6369 sinfo.vers = 1;
6370 sinfo.failed = FALSE;
6371
6372 elf_link_hash_traverse (elf_hash_table (info),
6373 _bfd_elf_link_find_version_dependencies,
6374 &sinfo);
14b1c01e
AM
6375 if (sinfo.failed)
6376 return FALSE;
5a580b3a
AM
6377
6378 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6379 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6380 else
6381 {
902e9fc7 6382 Elf_Internal_Verneed *vn;
5a580b3a
AM
6383 unsigned int size;
6384 unsigned int crefs;
6385 bfd_byte *p;
6386
a6cc6b3b 6387 /* Build the version dependency section. */
5a580b3a
AM
6388 size = 0;
6389 crefs = 0;
902e9fc7
MR
6390 for (vn = elf_tdata (output_bfd)->verref;
6391 vn != NULL;
6392 vn = vn->vn_nextref)
5a580b3a
AM
6393 {
6394 Elf_Internal_Vernaux *a;
6395
6396 size += sizeof (Elf_External_Verneed);
6397 ++crefs;
902e9fc7 6398 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6399 size += sizeof (Elf_External_Vernaux);
6400 }
6401
eea6121a 6402 s->size = size;
a50b1753 6403 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6404 if (s->contents == NULL)
6405 return FALSE;
6406
6407 p = s->contents;
902e9fc7
MR
6408 for (vn = elf_tdata (output_bfd)->verref;
6409 vn != NULL;
6410 vn = vn->vn_nextref)
5a580b3a
AM
6411 {
6412 unsigned int caux;
6413 Elf_Internal_Vernaux *a;
ef53be89 6414 size_t indx;
5a580b3a
AM
6415
6416 caux = 0;
902e9fc7 6417 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6418 ++caux;
6419
902e9fc7
MR
6420 vn->vn_version = VER_NEED_CURRENT;
6421 vn->vn_cnt = caux;
5a580b3a 6422 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
902e9fc7
MR
6423 elf_dt_name (vn->vn_bfd) != NULL
6424 ? elf_dt_name (vn->vn_bfd)
6425 : lbasename (vn->vn_bfd->filename),
5a580b3a 6426 FALSE);
ef53be89 6427 if (indx == (size_t) -1)
5a580b3a 6428 return FALSE;
902e9fc7
MR
6429 vn->vn_file = indx;
6430 vn->vn_aux = sizeof (Elf_External_Verneed);
6431 if (vn->vn_nextref == NULL)
6432 vn->vn_next = 0;
5a580b3a 6433 else
902e9fc7 6434 vn->vn_next = (sizeof (Elf_External_Verneed)
5a580b3a
AM
6435 + caux * sizeof (Elf_External_Vernaux));
6436
902e9fc7 6437 _bfd_elf_swap_verneed_out (output_bfd, vn,
5a580b3a
AM
6438 (Elf_External_Verneed *) p);
6439 p += sizeof (Elf_External_Verneed);
6440
902e9fc7 6441 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6442 {
6443 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6444 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6445 a->vna_nodename, FALSE);
ef53be89 6446 if (indx == (size_t) -1)
5a580b3a
AM
6447 return FALSE;
6448 a->vna_name = indx;
6449 if (a->vna_nextptr == NULL)
6450 a->vna_next = 0;
6451 else
6452 a->vna_next = sizeof (Elf_External_Vernaux);
6453
6454 _bfd_elf_swap_vernaux_out (output_bfd, a,
6455 (Elf_External_Vernaux *) p);
6456 p += sizeof (Elf_External_Vernaux);
6457 }
6458 }
6459
5a580b3a
AM
6460 elf_tdata (output_bfd)->cverrefs = crefs;
6461 }
6462 }
902e9fc7
MR
6463 }
6464
6465 bed = get_elf_backend_data (output_bfd);
6466
6467 if (info->gc_sections && bed->can_gc_sections)
6468 {
6469 struct elf_gc_sweep_symbol_info sweep_info;
6470 unsigned long section_sym_count;
6471
6472 /* Remove the symbols that were in the swept sections from the
6473 dynamic symbol table. GCFIXME: Anyone know how to get them
6474 out of the static symbol table as well? */
6475 sweep_info.info = info;
6476 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6477 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6478 &sweep_info);
6479
23ec1e32
MR
6480 /* We need to reassign dynsym indices now that symbols may have
6481 been removed. See the call in `bfd_elf_size_dynsym_hash_dynstr'
6482 for the details of the conditions used here. */
6483 if (elf_hash_table (info)->dynamic_sections_created
6484 || bed->always_renumber_dynsyms)
c46cec3a 6485 _bfd_elf_link_renumber_dynsyms (output_bfd, info, &section_sym_count);
902e9fc7
MR
6486 }
6487
6488 /* Any syms created from now on start with -1 in
6489 got.refcount/offset and plt.refcount/offset. */
6490 elf_hash_table (info)->init_got_refcount
6491 = elf_hash_table (info)->init_got_offset;
6492 elf_hash_table (info)->init_plt_refcount
6493 = elf_hash_table (info)->init_plt_offset;
6494
6495 if (bfd_link_relocatable (info)
6496 && !_bfd_elf_size_group_sections (info))
6497 return FALSE;
6498
6499 /* The backend may have to create some sections regardless of whether
6500 we're dynamic or not. */
6501 if (bed->elf_backend_always_size_sections
6502 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6503 return FALSE;
6504
6505 /* Determine any GNU_STACK segment requirements, after the backend
6506 has had a chance to set a default segment size. */
6507 if (info->execstack)
6508 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6509 else if (info->noexecstack)
6510 elf_stack_flags (output_bfd) = PF_R | PF_W;
6511 else
6512 {
6513 bfd *inputobj;
6514 asection *notesec = NULL;
6515 int exec = 0;
6516
6517 for (inputobj = info->input_bfds;
6518 inputobj;
6519 inputobj = inputobj->link.next)
6520 {
6521 asection *s;
6522
6523 if (inputobj->flags
6524 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6525 continue;
57963c05
AM
6526 s = inputobj->sections;
6527 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6528 continue;
6529
902e9fc7
MR
6530 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6531 if (s)
6532 {
6533 if (s->flags & SEC_CODE)
6534 exec = PF_X;
6535 notesec = s;
6536 }
6537 else if (bed->default_execstack)
6538 exec = PF_X;
6539 }
6540 if (notesec || info->stacksize > 0)
6541 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6542 if (notesec && exec && bfd_link_relocatable (info)
6543 && notesec->output_section != bfd_abs_section_ptr)
6544 notesec->output_section->flags |= SEC_CODE;
6545 }
6546
6547 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6548 {
6549 struct elf_info_failed eif;
6550 struct elf_link_hash_entry *h;
6551 asection *dynstr;
6552 asection *s;
6553
6554 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6555 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6556
902e9fc7
MR
6557 if (info->symbolic)
6558 {
6559 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6560 return FALSE;
6561 info->flags |= DF_SYMBOLIC;
6562 }
6563
6564 if (rpath != NULL)
6565 {
6566 size_t indx;
6567 bfd_vma tag;
6568
6569 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6570 TRUE);
6571 if (indx == (size_t) -1)
6572 return FALSE;
6573
6574 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6575 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6576 return FALSE;
6577 }
6578
6579 if (filter_shlib != NULL)
6580 {
6581 size_t indx;
6582
6583 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6584 filter_shlib, TRUE);
6585 if (indx == (size_t) -1
6586 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6587 return FALSE;
6588 }
6589
6590 if (auxiliary_filters != NULL)
6591 {
6592 const char * const *p;
6593
6594 for (p = auxiliary_filters; *p != NULL; p++)
6595 {
6596 size_t indx;
6597
6598 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6599 *p, TRUE);
6600 if (indx == (size_t) -1
6601 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6602 return FALSE;
6603 }
6604 }
6605
6606 if (audit != NULL)
6607 {
6608 size_t indx;
6609
6610 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6611 TRUE);
6612 if (indx == (size_t) -1
6613 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6614 return FALSE;
6615 }
6616
6617 if (depaudit != NULL)
6618 {
6619 size_t indx;
6620
6621 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6622 TRUE);
6623 if (indx == (size_t) -1
6624 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6625 return FALSE;
6626 }
6627
6628 eif.info = info;
6629 eif.failed = FALSE;
6630
6631 /* Find all symbols which were defined in a dynamic object and make
6632 the backend pick a reasonable value for them. */
6633 elf_link_hash_traverse (elf_hash_table (info),
6634 _bfd_elf_adjust_dynamic_symbol,
6635 &eif);
6636 if (eif.failed)
6637 return FALSE;
6638
6639 /* Add some entries to the .dynamic section. We fill in some of the
6640 values later, in bfd_elf_final_link, but we must add the entries
6641 now so that we know the final size of the .dynamic section. */
6642
6643 /* If there are initialization and/or finalization functions to
6644 call then add the corresponding DT_INIT/DT_FINI entries. */
6645 h = (info->init_function
6646 ? elf_link_hash_lookup (elf_hash_table (info),
6647 info->init_function, FALSE,
6648 FALSE, FALSE)
6649 : NULL);
6650 if (h != NULL
6651 && (h->ref_regular
6652 || h->def_regular))
6653 {
6654 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6655 return FALSE;
6656 }
6657 h = (info->fini_function
6658 ? elf_link_hash_lookup (elf_hash_table (info),
6659 info->fini_function, FALSE,
6660 FALSE, FALSE)
6661 : NULL);
6662 if (h != NULL
6663 && (h->ref_regular
6664 || h->def_regular))
6665 {
6666 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6667 return FALSE;
6668 }
6669
6670 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6671 if (s != NULL && s->linker_has_input)
6672 {
6673 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6674 if (! bfd_link_executable (info))
6675 {
6676 bfd *sub;
6677 asection *o;
6678
57963c05
AM
6679 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6680 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6681 && (o = sub->sections) != NULL
6682 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6683 for (o = sub->sections; o != NULL; o = o->next)
6684 if (elf_section_data (o)->this_hdr.sh_type
6685 == SHT_PREINIT_ARRAY)
6686 {
6687 _bfd_error_handler
6688 (_("%B: .preinit_array section is not allowed in DSO"),
6689 sub);
6690 break;
6691 }
6692
6693 bfd_set_error (bfd_error_nonrepresentable_section);
6694 return FALSE;
6695 }
6696
6697 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6698 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6699 return FALSE;
6700 }
6701 s = bfd_get_section_by_name (output_bfd, ".init_array");
6702 if (s != NULL && s->linker_has_input)
6703 {
6704 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6705 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6706 return FALSE;
6707 }
6708 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6709 if (s != NULL && s->linker_has_input)
6710 {
6711 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6712 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6713 return FALSE;
6714 }
6715
6716 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6717 /* If .dynstr is excluded from the link, we don't want any of
6718 these tags. Strictly, we should be checking each section
6719 individually; This quick check covers for the case where
6720 someone does a /DISCARD/ : { *(*) }. */
6721 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6722 {
6723 bfd_size_type strsize;
6724
6725 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6726 if ((info->emit_hash
6727 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6728 || (info->emit_gnu_hash
6729 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6730 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6731 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6732 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6733 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6734 bed->s->sizeof_sym))
6735 return FALSE;
6736 }
6737 }
6738
6739 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6740 return FALSE;
6741
6742 /* The backend must work out the sizes of all the other dynamic
6743 sections. */
6744 if (dynobj != NULL
6745 && bed->elf_backend_size_dynamic_sections != NULL
6746 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6747 return FALSE;
6748
6749 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6750 {
6751 unsigned long section_sym_count;
6752
6753 if (elf_tdata (output_bfd)->cverdefs)
6754 {
6755 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6756
6757 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6758 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6759 return FALSE;
6760 }
6761
6762 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6763 {
6764 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6765 return FALSE;
6766 }
6767 else if (info->flags & DF_BIND_NOW)
6768 {
6769 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6770 return FALSE;
6771 }
6772
6773 if (info->flags_1)
6774 {
6775 if (bfd_link_executable (info))
6776 info->flags_1 &= ~ (DF_1_INITFIRST
6777 | DF_1_NODELETE
6778 | DF_1_NOOPEN);
6779 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6780 return FALSE;
6781 }
6782
6783 if (elf_tdata (output_bfd)->cverrefs)
6784 {
6785 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6786
6787 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6788 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6789 return FALSE;
6790 }
5a580b3a 6791
8423293d
AM
6792 if ((elf_tdata (output_bfd)->cverrefs == 0
6793 && elf_tdata (output_bfd)->cverdefs == 0)
6794 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6795 &section_sym_count) == 0)
6796 {
902e9fc7
MR
6797 asection *s;
6798
3d4d4302 6799 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6800 s->flags |= SEC_EXCLUDE;
6801 }
6802 }
6803 return TRUE;
6804}
6805
74541ad4
AM
6806/* Find the first non-excluded output section. We'll use its
6807 section symbol for some emitted relocs. */
6808void
6809_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6810{
6811 asection *s;
6812
6813 for (s = output_bfd->sections; s != NULL; s = s->next)
6814 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6815 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6816 {
6817 elf_hash_table (info)->text_index_section = s;
6818 break;
6819 }
6820}
6821
6822/* Find two non-excluded output sections, one for code, one for data.
6823 We'll use their section symbols for some emitted relocs. */
6824void
6825_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6826{
6827 asection *s;
6828
266b05cf
DJ
6829 /* Data first, since setting text_index_section changes
6830 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6831 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6832 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6833 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6834 {
266b05cf 6835 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6836 break;
6837 }
6838
6839 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6840 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6841 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6842 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6843 {
266b05cf 6844 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6845 break;
6846 }
6847
6848 if (elf_hash_table (info)->text_index_section == NULL)
6849 elf_hash_table (info)->text_index_section
6850 = elf_hash_table (info)->data_index_section;
6851}
6852
8423293d
AM
6853bfd_boolean
6854bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6855{
74541ad4 6856 const struct elf_backend_data *bed;
23ec1e32
MR
6857 unsigned long section_sym_count;
6858 bfd_size_type dynsymcount;
74541ad4 6859
8423293d
AM
6860 if (!is_elf_hash_table (info->hash))
6861 return TRUE;
6862
74541ad4
AM
6863 bed = get_elf_backend_data (output_bfd);
6864 (*bed->elf_backend_init_index_section) (output_bfd, info);
6865
23ec1e32
MR
6866 /* Assign dynsym indices. In a shared library we generate a section
6867 symbol for each output section, which come first. Next come all
6868 of the back-end allocated local dynamic syms, followed by the rest
6869 of the global symbols.
6870
6871 This is usually not needed for static binaries, however backends
6872 can request to always do it, e.g. the MIPS backend uses dynamic
6873 symbol counts to lay out GOT, which will be produced in the
6874 presence of GOT relocations even in static binaries (holding fixed
6875 data in that case, to satisfy those relocations). */
6876
6877 if (elf_hash_table (info)->dynamic_sections_created
6878 || bed->always_renumber_dynsyms)
6879 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6880 &section_sym_count);
6881
8423293d
AM
6882 if (elf_hash_table (info)->dynamic_sections_created)
6883 {
6884 bfd *dynobj;
8423293d 6885 asection *s;
8423293d
AM
6886 unsigned int dtagcount;
6887
6888 dynobj = elf_hash_table (info)->dynobj;
6889
5a580b3a 6890 /* Work out the size of the symbol version section. */
3d4d4302 6891 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6892 BFD_ASSERT (s != NULL);
d5486c43 6893 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6894 {
eea6121a 6895 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6896 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6897 if (s->contents == NULL)
6898 return FALSE;
6899
6900 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6901 return FALSE;
6902 }
6903
6904 /* Set the size of the .dynsym and .hash sections. We counted
6905 the number of dynamic symbols in elf_link_add_object_symbols.
6906 We will build the contents of .dynsym and .hash when we build
6907 the final symbol table, because until then we do not know the
6908 correct value to give the symbols. We built the .dynstr
6909 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6910 s = elf_hash_table (info)->dynsym;
5a580b3a 6911 BFD_ASSERT (s != NULL);
eea6121a 6912 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6913
d5486c43
L
6914 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6915 if (s->contents == NULL)
6916 return FALSE;
5a580b3a 6917
d5486c43
L
6918 /* The first entry in .dynsym is a dummy symbol. Clear all the
6919 section syms, in case we don't output them all. */
6920 ++section_sym_count;
6921 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6922
fdc90cb4
JJ
6923 elf_hash_table (info)->bucketcount = 0;
6924
5a580b3a
AM
6925 /* Compute the size of the hashing table. As a side effect this
6926 computes the hash values for all the names we export. */
fdc90cb4
JJ
6927 if (info->emit_hash)
6928 {
6929 unsigned long int *hashcodes;
14b1c01e 6930 struct hash_codes_info hashinf;
fdc90cb4
JJ
6931 bfd_size_type amt;
6932 unsigned long int nsyms;
6933 size_t bucketcount;
6934 size_t hash_entry_size;
6935
6936 /* Compute the hash values for all exported symbols. At the same
6937 time store the values in an array so that we could use them for
6938 optimizations. */
6939 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6940 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6941 if (hashcodes == NULL)
6942 return FALSE;
14b1c01e
AM
6943 hashinf.hashcodes = hashcodes;
6944 hashinf.error = FALSE;
5a580b3a 6945
fdc90cb4
JJ
6946 /* Put all hash values in HASHCODES. */
6947 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6948 elf_collect_hash_codes, &hashinf);
6949 if (hashinf.error)
4dd07732
AM
6950 {
6951 free (hashcodes);
6952 return FALSE;
6953 }
5a580b3a 6954
14b1c01e 6955 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6956 bucketcount
6957 = compute_bucket_count (info, hashcodes, nsyms, 0);
6958 free (hashcodes);
6959
4b48e2f6 6960 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 6961 return FALSE;
5a580b3a 6962
fdc90cb4
JJ
6963 elf_hash_table (info)->bucketcount = bucketcount;
6964
3d4d4302 6965 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6966 BFD_ASSERT (s != NULL);
6967 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6968 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6969 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6970 if (s->contents == NULL)
6971 return FALSE;
6972
6973 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6974 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6975 s->contents + hash_entry_size);
6976 }
6977
6978 if (info->emit_gnu_hash)
6979 {
6980 size_t i, cnt;
6981 unsigned char *contents;
6982 struct collect_gnu_hash_codes cinfo;
6983 bfd_size_type amt;
6984 size_t bucketcount;
6985
6986 memset (&cinfo, 0, sizeof (cinfo));
6987
6988 /* Compute the hash values for all exported symbols. At the same
6989 time store the values in an array so that we could use them for
6990 optimizations. */
6991 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6992 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6993 if (cinfo.hashcodes == NULL)
6994 return FALSE;
6995
6996 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6997 cinfo.min_dynindx = -1;
6998 cinfo.output_bfd = output_bfd;
6999 cinfo.bed = bed;
7000
7001 /* Put all hash values in HASHCODES. */
7002 elf_link_hash_traverse (elf_hash_table (info),
7003 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7004 if (cinfo.error)
4dd07732
AM
7005 {
7006 free (cinfo.hashcodes);
7007 return FALSE;
7008 }
fdc90cb4
JJ
7009
7010 bucketcount
7011 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7012
7013 if (bucketcount == 0)
7014 {
7015 free (cinfo.hashcodes);
7016 return FALSE;
7017 }
7018
3d4d4302 7019 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7020 BFD_ASSERT (s != NULL);
7021
7022 if (cinfo.nsyms == 0)
7023 {
7024 /* Empty .gnu.hash section is special. */
7025 BFD_ASSERT (cinfo.min_dynindx == -1);
7026 free (cinfo.hashcodes);
7027 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7028 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7029 if (contents == NULL)
7030 return FALSE;
7031 s->contents = contents;
7032 /* 1 empty bucket. */
7033 bfd_put_32 (output_bfd, 1, contents);
7034 /* SYMIDX above the special symbol 0. */
7035 bfd_put_32 (output_bfd, 1, contents + 4);
7036 /* Just one word for bitmask. */
7037 bfd_put_32 (output_bfd, 1, contents + 8);
7038 /* Only hash fn bloom filter. */
7039 bfd_put_32 (output_bfd, 0, contents + 12);
7040 /* No hashes are valid - empty bitmask. */
7041 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7042 /* No hashes in the only bucket. */
7043 bfd_put_32 (output_bfd, 0,
7044 contents + 16 + bed->s->arch_size / 8);
7045 }
7046 else
7047 {
9e6619e2 7048 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7049 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7050
9e6619e2
AM
7051 x = cinfo.nsyms;
7052 maskbitslog2 = 1;
7053 while ((x >>= 1) != 0)
7054 ++maskbitslog2;
fdc90cb4
JJ
7055 if (maskbitslog2 < 3)
7056 maskbitslog2 = 5;
7057 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7058 maskbitslog2 = maskbitslog2 + 3;
7059 else
7060 maskbitslog2 = maskbitslog2 + 2;
7061 if (bed->s->arch_size == 64)
7062 {
7063 if (maskbitslog2 == 5)
7064 maskbitslog2 = 6;
7065 cinfo.shift1 = 6;
7066 }
7067 else
7068 cinfo.shift1 = 5;
7069 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7070 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7071 cinfo.maskbits = 1 << maskbitslog2;
7072 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7073 amt = bucketcount * sizeof (unsigned long int) * 2;
7074 amt += maskwords * sizeof (bfd_vma);
a50b1753 7075 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7076 if (cinfo.bitmask == NULL)
7077 {
7078 free (cinfo.hashcodes);
7079 return FALSE;
7080 }
7081
a50b1753 7082 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7083 cinfo.indx = cinfo.counts + bucketcount;
7084 cinfo.symindx = dynsymcount - cinfo.nsyms;
7085 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7086
7087 /* Determine how often each hash bucket is used. */
7088 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7089 for (i = 0; i < cinfo.nsyms; ++i)
7090 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7091
7092 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7093 if (cinfo.counts[i] != 0)
7094 {
7095 cinfo.indx[i] = cnt;
7096 cnt += cinfo.counts[i];
7097 }
7098 BFD_ASSERT (cnt == dynsymcount);
7099 cinfo.bucketcount = bucketcount;
7100 cinfo.local_indx = cinfo.min_dynindx;
7101
7102 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7103 s->size += cinfo.maskbits / 8;
a50b1753 7104 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7105 if (contents == NULL)
7106 {
7107 free (cinfo.bitmask);
7108 free (cinfo.hashcodes);
7109 return FALSE;
7110 }
7111
7112 s->contents = contents;
7113 bfd_put_32 (output_bfd, bucketcount, contents);
7114 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7115 bfd_put_32 (output_bfd, maskwords, contents + 8);
7116 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7117 contents += 16 + cinfo.maskbits / 8;
7118
7119 for (i = 0; i < bucketcount; ++i)
7120 {
7121 if (cinfo.counts[i] == 0)
7122 bfd_put_32 (output_bfd, 0, contents);
7123 else
7124 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7125 contents += 4;
7126 }
7127
7128 cinfo.contents = contents;
7129
7130 /* Renumber dynamic symbols, populate .gnu.hash section. */
7131 elf_link_hash_traverse (elf_hash_table (info),
7132 elf_renumber_gnu_hash_syms, &cinfo);
7133
7134 contents = s->contents + 16;
7135 for (i = 0; i < maskwords; ++i)
7136 {
7137 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7138 contents);
7139 contents += bed->s->arch_size / 8;
7140 }
7141
7142 free (cinfo.bitmask);
7143 free (cinfo.hashcodes);
7144 }
7145 }
5a580b3a 7146
3d4d4302 7147 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7148 BFD_ASSERT (s != NULL);
7149
4ad4eba5 7150 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7151
eea6121a 7152 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7153
7154 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7156 return FALSE;
7157 }
7158
7159 return TRUE;
7160}
4d269e42 7161\f
4d269e42
AM
7162/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7163
7164static void
7165merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7166 asection *sec)
7167{
dbaa2011
AM
7168 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7169 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7170}
7171
7172/* Finish SHF_MERGE section merging. */
7173
7174bfd_boolean
630993ec 7175_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7176{
7177 bfd *ibfd;
7178 asection *sec;
7179
7180 if (!is_elf_hash_table (info->hash))
7181 return FALSE;
7182
c72f2fb2 7183 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7184 if ((ibfd->flags & DYNAMIC) == 0
7185 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7186 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7187 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7188 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7189 if ((sec->flags & SEC_MERGE) != 0
7190 && !bfd_is_abs_section (sec->output_section))
7191 {
7192 struct bfd_elf_section_data *secdata;
7193
7194 secdata = elf_section_data (sec);
630993ec 7195 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7196 &elf_hash_table (info)->merge_info,
7197 sec, &secdata->sec_info))
7198 return FALSE;
7199 else if (secdata->sec_info)
dbaa2011 7200 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7201 }
7202
7203 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7204 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7205 merge_sections_remove_hook);
7206 return TRUE;
7207}
7208
7209/* Create an entry in an ELF linker hash table. */
7210
7211struct bfd_hash_entry *
7212_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7213 struct bfd_hash_table *table,
7214 const char *string)
7215{
7216 /* Allocate the structure if it has not already been allocated by a
7217 subclass. */
7218 if (entry == NULL)
7219 {
a50b1753 7220 entry = (struct bfd_hash_entry *)
ca4be51c 7221 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7222 if (entry == NULL)
7223 return entry;
7224 }
7225
7226 /* Call the allocation method of the superclass. */
7227 entry = _bfd_link_hash_newfunc (entry, table, string);
7228 if (entry != NULL)
7229 {
7230 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7231 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7232
7233 /* Set local fields. */
7234 ret->indx = -1;
7235 ret->dynindx = -1;
7236 ret->got = htab->init_got_refcount;
7237 ret->plt = htab->init_plt_refcount;
7238 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7239 - offsetof (struct elf_link_hash_entry, size)));
7240 /* Assume that we have been called by a non-ELF symbol reader.
7241 This flag is then reset by the code which reads an ELF input
7242 file. This ensures that a symbol created by a non-ELF symbol
7243 reader will have the flag set correctly. */
7244 ret->non_elf = 1;
7245 }
7246
7247 return entry;
7248}
7249
7250/* Copy data from an indirect symbol to its direct symbol, hiding the
7251 old indirect symbol. Also used for copying flags to a weakdef. */
7252
7253void
7254_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7255 struct elf_link_hash_entry *dir,
7256 struct elf_link_hash_entry *ind)
7257{
7258 struct elf_link_hash_table *htab;
7259
7260 /* Copy down any references that we may have already seen to the
e81830c5 7261 symbol which just became indirect. */
4d269e42 7262
422f1182 7263 if (dir->versioned != versioned_hidden)
e81830c5
AM
7264 dir->ref_dynamic |= ind->ref_dynamic;
7265 dir->ref_regular |= ind->ref_regular;
7266 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7267 dir->non_got_ref |= ind->non_got_ref;
7268 dir->needs_plt |= ind->needs_plt;
7269 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7270
7271 if (ind->root.type != bfd_link_hash_indirect)
7272 return;
7273
7274 /* Copy over the global and procedure linkage table refcount entries.
7275 These may have been already set up by a check_relocs routine. */
7276 htab = elf_hash_table (info);
7277 if (ind->got.refcount > htab->init_got_refcount.refcount)
7278 {
7279 if (dir->got.refcount < 0)
7280 dir->got.refcount = 0;
7281 dir->got.refcount += ind->got.refcount;
7282 ind->got.refcount = htab->init_got_refcount.refcount;
7283 }
7284
7285 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7286 {
7287 if (dir->plt.refcount < 0)
7288 dir->plt.refcount = 0;
7289 dir->plt.refcount += ind->plt.refcount;
7290 ind->plt.refcount = htab->init_plt_refcount.refcount;
7291 }
7292
7293 if (ind->dynindx != -1)
7294 {
7295 if (dir->dynindx != -1)
7296 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7297 dir->dynindx = ind->dynindx;
7298 dir->dynstr_index = ind->dynstr_index;
7299 ind->dynindx = -1;
7300 ind->dynstr_index = 0;
7301 }
7302}
7303
7304void
7305_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7306 struct elf_link_hash_entry *h,
7307 bfd_boolean force_local)
7308{
3aa14d16
L
7309 /* STT_GNU_IFUNC symbol must go through PLT. */
7310 if (h->type != STT_GNU_IFUNC)
7311 {
7312 h->plt = elf_hash_table (info)->init_plt_offset;
7313 h->needs_plt = 0;
7314 }
4d269e42
AM
7315 if (force_local)
7316 {
7317 h->forced_local = 1;
7318 if (h->dynindx != -1)
7319 {
4d269e42
AM
7320 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7321 h->dynstr_index);
641338d8
AM
7322 h->dynindx = -1;
7323 h->dynstr_index = 0;
4d269e42
AM
7324 }
7325 }
7326}
7327
7bf52ea2
AM
7328/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7329 caller. */
4d269e42
AM
7330
7331bfd_boolean
7332_bfd_elf_link_hash_table_init
7333 (struct elf_link_hash_table *table,
7334 bfd *abfd,
7335 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7336 struct bfd_hash_table *,
7337 const char *),
4dfe6ac6
NC
7338 unsigned int entsize,
7339 enum elf_target_id target_id)
4d269e42
AM
7340{
7341 bfd_boolean ret;
7342 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7343
4d269e42
AM
7344 table->init_got_refcount.refcount = can_refcount - 1;
7345 table->init_plt_refcount.refcount = can_refcount - 1;
7346 table->init_got_offset.offset = -(bfd_vma) 1;
7347 table->init_plt_offset.offset = -(bfd_vma) 1;
7348 /* The first dynamic symbol is a dummy. */
7349 table->dynsymcount = 1;
7350
7351 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7352
4d269e42 7353 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7354 table->hash_table_id = target_id;
4d269e42
AM
7355
7356 return ret;
7357}
7358
7359/* Create an ELF linker hash table. */
7360
7361struct bfd_link_hash_table *
7362_bfd_elf_link_hash_table_create (bfd *abfd)
7363{
7364 struct elf_link_hash_table *ret;
7365 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7366
7bf52ea2 7367 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7368 if (ret == NULL)
7369 return NULL;
7370
7371 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7372 sizeof (struct elf_link_hash_entry),
7373 GENERIC_ELF_DATA))
4d269e42
AM
7374 {
7375 free (ret);
7376 return NULL;
7377 }
d495ab0d 7378 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7379
7380 return &ret->root;
7381}
7382
9f7c3e5e
AM
7383/* Destroy an ELF linker hash table. */
7384
7385void
d495ab0d 7386_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7387{
d495ab0d
AM
7388 struct elf_link_hash_table *htab;
7389
7390 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7391 if (htab->dynstr != NULL)
7392 _bfd_elf_strtab_free (htab->dynstr);
7393 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7394 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7395}
7396
4d269e42
AM
7397/* This is a hook for the ELF emulation code in the generic linker to
7398 tell the backend linker what file name to use for the DT_NEEDED
7399 entry for a dynamic object. */
7400
7401void
7402bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7403{
7404 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7405 && bfd_get_format (abfd) == bfd_object)
7406 elf_dt_name (abfd) = name;
7407}
7408
7409int
7410bfd_elf_get_dyn_lib_class (bfd *abfd)
7411{
7412 int lib_class;
7413 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7414 && bfd_get_format (abfd) == bfd_object)
7415 lib_class = elf_dyn_lib_class (abfd);
7416 else
7417 lib_class = 0;
7418 return lib_class;
7419}
7420
7421void
7422bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7423{
7424 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7425 && bfd_get_format (abfd) == bfd_object)
7426 elf_dyn_lib_class (abfd) = lib_class;
7427}
7428
7429/* Get the list of DT_NEEDED entries for a link. This is a hook for
7430 the linker ELF emulation code. */
7431
7432struct bfd_link_needed_list *
7433bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7434 struct bfd_link_info *info)
7435{
7436 if (! is_elf_hash_table (info->hash))
7437 return NULL;
7438 return elf_hash_table (info)->needed;
7439}
7440
7441/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7442 hook for the linker ELF emulation code. */
7443
7444struct bfd_link_needed_list *
7445bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7446 struct bfd_link_info *info)
7447{
7448 if (! is_elf_hash_table (info->hash))
7449 return NULL;
7450 return elf_hash_table (info)->runpath;
7451}
7452
7453/* Get the name actually used for a dynamic object for a link. This
7454 is the SONAME entry if there is one. Otherwise, it is the string
7455 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7456
7457const char *
7458bfd_elf_get_dt_soname (bfd *abfd)
7459{
7460 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7461 && bfd_get_format (abfd) == bfd_object)
7462 return elf_dt_name (abfd);
7463 return NULL;
7464}
7465
7466/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7467 the ELF linker emulation code. */
7468
7469bfd_boolean
7470bfd_elf_get_bfd_needed_list (bfd *abfd,
7471 struct bfd_link_needed_list **pneeded)
7472{
7473 asection *s;
7474 bfd_byte *dynbuf = NULL;
cb33740c 7475 unsigned int elfsec;
4d269e42
AM
7476 unsigned long shlink;
7477 bfd_byte *extdyn, *extdynend;
7478 size_t extdynsize;
7479 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7480
7481 *pneeded = NULL;
7482
7483 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7484 || bfd_get_format (abfd) != bfd_object)
7485 return TRUE;
7486
7487 s = bfd_get_section_by_name (abfd, ".dynamic");
7488 if (s == NULL || s->size == 0)
7489 return TRUE;
7490
7491 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7492 goto error_return;
7493
7494 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7495 if (elfsec == SHN_BAD)
4d269e42
AM
7496 goto error_return;
7497
7498 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7499
4d269e42
AM
7500 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7501 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7502
7503 extdyn = dynbuf;
7504 extdynend = extdyn + s->size;
7505 for (; extdyn < extdynend; extdyn += extdynsize)
7506 {
7507 Elf_Internal_Dyn dyn;
7508
7509 (*swap_dyn_in) (abfd, extdyn, &dyn);
7510
7511 if (dyn.d_tag == DT_NULL)
7512 break;
7513
7514 if (dyn.d_tag == DT_NEEDED)
7515 {
7516 const char *string;
7517 struct bfd_link_needed_list *l;
7518 unsigned int tagv = dyn.d_un.d_val;
7519 bfd_size_type amt;
7520
7521 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7522 if (string == NULL)
7523 goto error_return;
7524
7525 amt = sizeof *l;
a50b1753 7526 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7527 if (l == NULL)
7528 goto error_return;
7529
7530 l->by = abfd;
7531 l->name = string;
7532 l->next = *pneeded;
7533 *pneeded = l;
7534 }
7535 }
7536
7537 free (dynbuf);
7538
7539 return TRUE;
7540
7541 error_return:
7542 if (dynbuf != NULL)
7543 free (dynbuf);
7544 return FALSE;
7545}
7546
7547struct elf_symbuf_symbol
7548{
7549 unsigned long st_name; /* Symbol name, index in string tbl */
7550 unsigned char st_info; /* Type and binding attributes */
7551 unsigned char st_other; /* Visibilty, and target specific */
7552};
7553
7554struct elf_symbuf_head
7555{
7556 struct elf_symbuf_symbol *ssym;
ef53be89 7557 size_t count;
4d269e42
AM
7558 unsigned int st_shndx;
7559};
7560
7561struct elf_symbol
7562{
7563 union
7564 {
7565 Elf_Internal_Sym *isym;
7566 struct elf_symbuf_symbol *ssym;
7567 } u;
7568 const char *name;
7569};
7570
7571/* Sort references to symbols by ascending section number. */
7572
7573static int
7574elf_sort_elf_symbol (const void *arg1, const void *arg2)
7575{
7576 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7577 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7578
7579 return s1->st_shndx - s2->st_shndx;
7580}
7581
7582static int
7583elf_sym_name_compare (const void *arg1, const void *arg2)
7584{
7585 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7586 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7587 return strcmp (s1->name, s2->name);
7588}
7589
7590static struct elf_symbuf_head *
ef53be89 7591elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7592{
14b1c01e 7593 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7594 struct elf_symbuf_symbol *ssym;
7595 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7596 size_t i, shndx_count, total_size;
4d269e42 7597
a50b1753 7598 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7599 if (indbuf == NULL)
7600 return NULL;
7601
7602 for (ind = indbuf, i = 0; i < symcount; i++)
7603 if (isymbuf[i].st_shndx != SHN_UNDEF)
7604 *ind++ = &isymbuf[i];
7605 indbufend = ind;
7606
7607 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7608 elf_sort_elf_symbol);
7609
7610 shndx_count = 0;
7611 if (indbufend > indbuf)
7612 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7613 if (ind[0]->st_shndx != ind[1]->st_shndx)
7614 shndx_count++;
7615
3ae181ee
L
7616 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7617 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7618 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7619 if (ssymbuf == NULL)
7620 {
7621 free (indbuf);
7622 return NULL;
7623 }
7624
3ae181ee 7625 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7626 ssymbuf->ssym = NULL;
7627 ssymbuf->count = shndx_count;
7628 ssymbuf->st_shndx = 0;
7629 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7630 {
7631 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7632 {
7633 ssymhead++;
7634 ssymhead->ssym = ssym;
7635 ssymhead->count = 0;
7636 ssymhead->st_shndx = (*ind)->st_shndx;
7637 }
7638 ssym->st_name = (*ind)->st_name;
7639 ssym->st_info = (*ind)->st_info;
7640 ssym->st_other = (*ind)->st_other;
7641 ssymhead->count++;
7642 }
ef53be89 7643 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7644 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7645 == total_size));
4d269e42
AM
7646
7647 free (indbuf);
7648 return ssymbuf;
7649}
7650
7651/* Check if 2 sections define the same set of local and global
7652 symbols. */
7653
8f317e31 7654static bfd_boolean
4d269e42
AM
7655bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7656 struct bfd_link_info *info)
7657{
7658 bfd *bfd1, *bfd2;
7659 const struct elf_backend_data *bed1, *bed2;
7660 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7661 size_t symcount1, symcount2;
4d269e42
AM
7662 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7663 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7664 Elf_Internal_Sym *isym, *isymend;
7665 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7666 size_t count1, count2, i;
cb33740c 7667 unsigned int shndx1, shndx2;
4d269e42
AM
7668 bfd_boolean result;
7669
7670 bfd1 = sec1->owner;
7671 bfd2 = sec2->owner;
7672
4d269e42
AM
7673 /* Both sections have to be in ELF. */
7674 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7675 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7676 return FALSE;
7677
7678 if (elf_section_type (sec1) != elf_section_type (sec2))
7679 return FALSE;
7680
4d269e42
AM
7681 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7682 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7683 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7684 return FALSE;
7685
7686 bed1 = get_elf_backend_data (bfd1);
7687 bed2 = get_elf_backend_data (bfd2);
7688 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7689 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7690 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7691 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7692
7693 if (symcount1 == 0 || symcount2 == 0)
7694 return FALSE;
7695
7696 result = FALSE;
7697 isymbuf1 = NULL;
7698 isymbuf2 = NULL;
a50b1753
NC
7699 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7700 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7701
7702 if (ssymbuf1 == NULL)
7703 {
7704 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7705 NULL, NULL, NULL);
7706 if (isymbuf1 == NULL)
7707 goto done;
7708
7709 if (!info->reduce_memory_overheads)
7710 elf_tdata (bfd1)->symbuf = ssymbuf1
7711 = elf_create_symbuf (symcount1, isymbuf1);
7712 }
7713
7714 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7715 {
7716 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7717 NULL, NULL, NULL);
7718 if (isymbuf2 == NULL)
7719 goto done;
7720
7721 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7722 elf_tdata (bfd2)->symbuf = ssymbuf2
7723 = elf_create_symbuf (symcount2, isymbuf2);
7724 }
7725
7726 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7727 {
7728 /* Optimized faster version. */
ef53be89 7729 size_t lo, hi, mid;
4d269e42
AM
7730 struct elf_symbol *symp;
7731 struct elf_symbuf_symbol *ssym, *ssymend;
7732
7733 lo = 0;
7734 hi = ssymbuf1->count;
7735 ssymbuf1++;
7736 count1 = 0;
7737 while (lo < hi)
7738 {
7739 mid = (lo + hi) / 2;
cb33740c 7740 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7741 hi = mid;
cb33740c 7742 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7743 lo = mid + 1;
7744 else
7745 {
7746 count1 = ssymbuf1[mid].count;
7747 ssymbuf1 += mid;
7748 break;
7749 }
7750 }
7751
7752 lo = 0;
7753 hi = ssymbuf2->count;
7754 ssymbuf2++;
7755 count2 = 0;
7756 while (lo < hi)
7757 {
7758 mid = (lo + hi) / 2;
cb33740c 7759 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7760 hi = mid;
cb33740c 7761 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7762 lo = mid + 1;
7763 else
7764 {
7765 count2 = ssymbuf2[mid].count;
7766 ssymbuf2 += mid;
7767 break;
7768 }
7769 }
7770
7771 if (count1 == 0 || count2 == 0 || count1 != count2)
7772 goto done;
7773
ca4be51c
AM
7774 symtable1
7775 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7776 symtable2
7777 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7778 if (symtable1 == NULL || symtable2 == NULL)
7779 goto done;
7780
7781 symp = symtable1;
7782 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7783 ssym < ssymend; ssym++, symp++)
7784 {
7785 symp->u.ssym = ssym;
7786 symp->name = bfd_elf_string_from_elf_section (bfd1,
7787 hdr1->sh_link,
7788 ssym->st_name);
7789 }
7790
7791 symp = symtable2;
7792 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7793 ssym < ssymend; ssym++, symp++)
7794 {
7795 symp->u.ssym = ssym;
7796 symp->name = bfd_elf_string_from_elf_section (bfd2,
7797 hdr2->sh_link,
7798 ssym->st_name);
7799 }
7800
7801 /* Sort symbol by name. */
7802 qsort (symtable1, count1, sizeof (struct elf_symbol),
7803 elf_sym_name_compare);
7804 qsort (symtable2, count1, sizeof (struct elf_symbol),
7805 elf_sym_name_compare);
7806
7807 for (i = 0; i < count1; i++)
7808 /* Two symbols must have the same binding, type and name. */
7809 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7810 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7811 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7812 goto done;
7813
7814 result = TRUE;
7815 goto done;
7816 }
7817
a50b1753
NC
7818 symtable1 = (struct elf_symbol *)
7819 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7820 symtable2 = (struct elf_symbol *)
7821 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7822 if (symtable1 == NULL || symtable2 == NULL)
7823 goto done;
7824
7825 /* Count definitions in the section. */
7826 count1 = 0;
7827 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7828 if (isym->st_shndx == shndx1)
4d269e42
AM
7829 symtable1[count1++].u.isym = isym;
7830
7831 count2 = 0;
7832 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7833 if (isym->st_shndx == shndx2)
4d269e42
AM
7834 symtable2[count2++].u.isym = isym;
7835
7836 if (count1 == 0 || count2 == 0 || count1 != count2)
7837 goto done;
7838
7839 for (i = 0; i < count1; i++)
7840 symtable1[i].name
7841 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7842 symtable1[i].u.isym->st_name);
7843
7844 for (i = 0; i < count2; i++)
7845 symtable2[i].name
7846 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7847 symtable2[i].u.isym->st_name);
7848
7849 /* Sort symbol by name. */
7850 qsort (symtable1, count1, sizeof (struct elf_symbol),
7851 elf_sym_name_compare);
7852 qsort (symtable2, count1, sizeof (struct elf_symbol),
7853 elf_sym_name_compare);
7854
7855 for (i = 0; i < count1; i++)
7856 /* Two symbols must have the same binding, type and name. */
7857 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7858 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7859 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7860 goto done;
7861
7862 result = TRUE;
7863
7864done:
7865 if (symtable1)
7866 free (symtable1);
7867 if (symtable2)
7868 free (symtable2);
7869 if (isymbuf1)
7870 free (isymbuf1);
7871 if (isymbuf2)
7872 free (isymbuf2);
7873
7874 return result;
7875}
7876
7877/* Return TRUE if 2 section types are compatible. */
7878
7879bfd_boolean
7880_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7881 bfd *bbfd, const asection *bsec)
7882{
7883 if (asec == NULL
7884 || bsec == NULL
7885 || abfd->xvec->flavour != bfd_target_elf_flavour
7886 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7887 return TRUE;
7888
7889 return elf_section_type (asec) == elf_section_type (bsec);
7890}
7891\f
c152c796
AM
7892/* Final phase of ELF linker. */
7893
7894/* A structure we use to avoid passing large numbers of arguments. */
7895
7896struct elf_final_link_info
7897{
7898 /* General link information. */
7899 struct bfd_link_info *info;
7900 /* Output BFD. */
7901 bfd *output_bfd;
7902 /* Symbol string table. */
ef10c3ac 7903 struct elf_strtab_hash *symstrtab;
c152c796
AM
7904 /* .hash section. */
7905 asection *hash_sec;
7906 /* symbol version section (.gnu.version). */
7907 asection *symver_sec;
7908 /* Buffer large enough to hold contents of any section. */
7909 bfd_byte *contents;
7910 /* Buffer large enough to hold external relocs of any section. */
7911 void *external_relocs;
7912 /* Buffer large enough to hold internal relocs of any section. */
7913 Elf_Internal_Rela *internal_relocs;
7914 /* Buffer large enough to hold external local symbols of any input
7915 BFD. */
7916 bfd_byte *external_syms;
7917 /* And a buffer for symbol section indices. */
7918 Elf_External_Sym_Shndx *locsym_shndx;
7919 /* Buffer large enough to hold internal local symbols of any input
7920 BFD. */
7921 Elf_Internal_Sym *internal_syms;
7922 /* Array large enough to hold a symbol index for each local symbol
7923 of any input BFD. */
7924 long *indices;
7925 /* Array large enough to hold a section pointer for each local
7926 symbol of any input BFD. */
7927 asection **sections;
ef10c3ac 7928 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7929 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7930 /* Number of STT_FILE syms seen. */
7931 size_t filesym_count;
c152c796
AM
7932};
7933
7934/* This struct is used to pass information to elf_link_output_extsym. */
7935
7936struct elf_outext_info
7937{
7938 bfd_boolean failed;
7939 bfd_boolean localsyms;
34a79995 7940 bfd_boolean file_sym_done;
8b127cbc 7941 struct elf_final_link_info *flinfo;
c152c796
AM
7942};
7943
d9352518
DB
7944
7945/* Support for evaluating a complex relocation.
7946
7947 Complex relocations are generalized, self-describing relocations. The
7948 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7949 relocations themselves.
d9352518
DB
7950
7951 The relocations are use a reserved elf-wide relocation type code (R_RELC
7952 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7953 information (start bit, end bit, word width, etc) into the addend. This
7954 information is extracted from CGEN-generated operand tables within gas.
7955
7956 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7957 internal) representing prefix-notation expressions, including but not
7958 limited to those sorts of expressions normally encoded as addends in the
7959 addend field. The symbol mangling format is:
7960
7961 <node> := <literal>
7962 | <unary-operator> ':' <node>
7963 | <binary-operator> ':' <node> ':' <node>
7964 ;
7965
7966 <literal> := 's' <digits=N> ':' <N character symbol name>
7967 | 'S' <digits=N> ':' <N character section name>
7968 | '#' <hexdigits>
7969 ;
7970
7971 <binary-operator> := as in C
7972 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7973
7974static void
a0c8462f
AM
7975set_symbol_value (bfd *bfd_with_globals,
7976 Elf_Internal_Sym *isymbuf,
7977 size_t locsymcount,
7978 size_t symidx,
7979 bfd_vma val)
d9352518 7980{
8977835c
AM
7981 struct elf_link_hash_entry **sym_hashes;
7982 struct elf_link_hash_entry *h;
7983 size_t extsymoff = locsymcount;
d9352518 7984
8977835c 7985 if (symidx < locsymcount)
d9352518 7986 {
8977835c
AM
7987 Elf_Internal_Sym *sym;
7988
7989 sym = isymbuf + symidx;
7990 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7991 {
7992 /* It is a local symbol: move it to the
7993 "absolute" section and give it a value. */
7994 sym->st_shndx = SHN_ABS;
7995 sym->st_value = val;
7996 return;
7997 }
7998 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7999 extsymoff = 0;
d9352518 8000 }
8977835c
AM
8001
8002 /* It is a global symbol: set its link type
8003 to "defined" and give it a value. */
8004
8005 sym_hashes = elf_sym_hashes (bfd_with_globals);
8006 h = sym_hashes [symidx - extsymoff];
8007 while (h->root.type == bfd_link_hash_indirect
8008 || h->root.type == bfd_link_hash_warning)
8009 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8010 h->root.type = bfd_link_hash_defined;
8011 h->root.u.def.value = val;
8012 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8013}
8014
a0c8462f
AM
8015static bfd_boolean
8016resolve_symbol (const char *name,
8017 bfd *input_bfd,
8b127cbc 8018 struct elf_final_link_info *flinfo,
a0c8462f
AM
8019 bfd_vma *result,
8020 Elf_Internal_Sym *isymbuf,
8021 size_t locsymcount)
d9352518 8022{
a0c8462f
AM
8023 Elf_Internal_Sym *sym;
8024 struct bfd_link_hash_entry *global_entry;
8025 const char *candidate = NULL;
8026 Elf_Internal_Shdr *symtab_hdr;
8027 size_t i;
8028
d9352518
DB
8029 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8030
8031 for (i = 0; i < locsymcount; ++ i)
8032 {
8977835c 8033 sym = isymbuf + i;
d9352518
DB
8034
8035 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8036 continue;
8037
8038 candidate = bfd_elf_string_from_elf_section (input_bfd,
8039 symtab_hdr->sh_link,
8040 sym->st_name);
8041#ifdef DEBUG
0f02bbd9
AM
8042 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8043 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8044#endif
8045 if (candidate && strcmp (candidate, name) == 0)
8046 {
8b127cbc 8047 asection *sec = flinfo->sections [i];
d9352518 8048
0f02bbd9
AM
8049 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8050 *result += sec->output_offset + sec->output_section->vma;
d9352518 8051#ifdef DEBUG
0f02bbd9
AM
8052 printf ("Found symbol with value %8.8lx\n",
8053 (unsigned long) *result);
d9352518
DB
8054#endif
8055 return TRUE;
8056 }
8057 }
8058
8059 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8060 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8061 FALSE, FALSE, TRUE);
d9352518
DB
8062 if (!global_entry)
8063 return FALSE;
a0c8462f 8064
d9352518
DB
8065 if (global_entry->type == bfd_link_hash_defined
8066 || global_entry->type == bfd_link_hash_defweak)
8067 {
a0c8462f
AM
8068 *result = (global_entry->u.def.value
8069 + global_entry->u.def.section->output_section->vma
8070 + global_entry->u.def.section->output_offset);
d9352518 8071#ifdef DEBUG
0f02bbd9
AM
8072 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8073 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8074#endif
8075 return TRUE;
a0c8462f 8076 }
d9352518 8077
d9352518
DB
8078 return FALSE;
8079}
8080
37b01f6a
DG
8081/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8082 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8083 names like "foo.end" which is the end address of section "foo". */
8084
d9352518 8085static bfd_boolean
a0c8462f
AM
8086resolve_section (const char *name,
8087 asection *sections,
37b01f6a
DG
8088 bfd_vma *result,
8089 bfd * abfd)
d9352518 8090{
a0c8462f
AM
8091 asection *curr;
8092 unsigned int len;
d9352518 8093
a0c8462f 8094 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8095 if (strcmp (curr->name, name) == 0)
8096 {
8097 *result = curr->vma;
8098 return TRUE;
8099 }
8100
8101 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8102 /* FIXME: This could be coded more efficiently... */
a0c8462f 8103 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8104 {
8105 len = strlen (curr->name);
a0c8462f 8106 if (len > strlen (name))
d9352518
DB
8107 continue;
8108
8109 if (strncmp (curr->name, name, len) == 0)
8110 {
8111 if (strncmp (".end", name + len, 4) == 0)
8112 {
37b01f6a 8113 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8114 return TRUE;
8115 }
8116
8117 /* Insert more pseudo-section names here, if you like. */
8118 }
8119 }
a0c8462f 8120
d9352518
DB
8121 return FALSE;
8122}
8123
8124static void
a0c8462f 8125undefined_reference (const char *reftype, const char *name)
d9352518 8126{
695344c0 8127 /* xgettext:c-format */
a0c8462f
AM
8128 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8129 reftype, name);
d9352518
DB
8130}
8131
8132static bfd_boolean
a0c8462f
AM
8133eval_symbol (bfd_vma *result,
8134 const char **symp,
8135 bfd *input_bfd,
8b127cbc 8136 struct elf_final_link_info *flinfo,
a0c8462f
AM
8137 bfd_vma dot,
8138 Elf_Internal_Sym *isymbuf,
8139 size_t locsymcount,
8140 int signed_p)
d9352518 8141{
4b93929b
NC
8142 size_t len;
8143 size_t symlen;
a0c8462f
AM
8144 bfd_vma a;
8145 bfd_vma b;
4b93929b 8146 char symbuf[4096];
0f02bbd9 8147 const char *sym = *symp;
a0c8462f
AM
8148 const char *symend;
8149 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8150
8151 len = strlen (sym);
8152 symend = sym + len;
8153
4b93929b 8154 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8155 {
8156 bfd_set_error (bfd_error_invalid_operation);
8157 return FALSE;
8158 }
a0c8462f 8159
d9352518
DB
8160 switch (* sym)
8161 {
8162 case '.':
0f02bbd9
AM
8163 *result = dot;
8164 *symp = sym + 1;
d9352518
DB
8165 return TRUE;
8166
8167 case '#':
0f02bbd9
AM
8168 ++sym;
8169 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8170 return TRUE;
8171
8172 case 'S':
8173 symbol_is_section = TRUE;
1a0670f3 8174 /* Fall through. */
a0c8462f 8175 case 's':
0f02bbd9
AM
8176 ++sym;
8177 symlen = strtol (sym, (char **) symp, 10);
8178 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8179
4b93929b 8180 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8181 {
8182 bfd_set_error (bfd_error_invalid_operation);
8183 return FALSE;
8184 }
8185
8186 memcpy (symbuf, sym, symlen);
a0c8462f 8187 symbuf[symlen] = '\0';
0f02bbd9 8188 *symp = sym + symlen;
a0c8462f
AM
8189
8190 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8191 the symbol as a section, or vice-versa. so we're pretty liberal in our
8192 interpretation here; section means "try section first", not "must be a
8193 section", and likewise with symbol. */
8194
a0c8462f 8195 if (symbol_is_section)
d9352518 8196 {
37b01f6a 8197 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8198 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8199 isymbuf, locsymcount))
d9352518
DB
8200 {
8201 undefined_reference ("section", symbuf);
8202 return FALSE;
8203 }
a0c8462f
AM
8204 }
8205 else
d9352518 8206 {
8b127cbc 8207 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8208 isymbuf, locsymcount)
8b127cbc 8209 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8210 result, input_bfd))
d9352518
DB
8211 {
8212 undefined_reference ("symbol", symbuf);
8213 return FALSE;
8214 }
8215 }
8216
8217 return TRUE;
a0c8462f 8218
d9352518
DB
8219 /* All that remains are operators. */
8220
8221#define UNARY_OP(op) \
8222 if (strncmp (sym, #op, strlen (#op)) == 0) \
8223 { \
8224 sym += strlen (#op); \
a0c8462f
AM
8225 if (*sym == ':') \
8226 ++sym; \
0f02bbd9 8227 *symp = sym; \
8b127cbc 8228 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8229 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8230 return FALSE; \
8231 if (signed_p) \
0f02bbd9 8232 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8233 else \
8234 *result = op a; \
d9352518
DB
8235 return TRUE; \
8236 }
8237
8238#define BINARY_OP(op) \
8239 if (strncmp (sym, #op, strlen (#op)) == 0) \
8240 { \
8241 sym += strlen (#op); \
a0c8462f
AM
8242 if (*sym == ':') \
8243 ++sym; \
0f02bbd9 8244 *symp = sym; \
8b127cbc 8245 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8246 isymbuf, locsymcount, signed_p)) \
a0c8462f 8247 return FALSE; \
0f02bbd9 8248 ++*symp; \
8b127cbc 8249 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8250 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8251 return FALSE; \
8252 if (signed_p) \
0f02bbd9 8253 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8254 else \
8255 *result = a op b; \
d9352518
DB
8256 return TRUE; \
8257 }
8258
8259 default:
8260 UNARY_OP (0-);
8261 BINARY_OP (<<);
8262 BINARY_OP (>>);
8263 BINARY_OP (==);
8264 BINARY_OP (!=);
8265 BINARY_OP (<=);
8266 BINARY_OP (>=);
8267 BINARY_OP (&&);
8268 BINARY_OP (||);
8269 UNARY_OP (~);
8270 UNARY_OP (!);
8271 BINARY_OP (*);
8272 BINARY_OP (/);
8273 BINARY_OP (%);
8274 BINARY_OP (^);
8275 BINARY_OP (|);
8276 BINARY_OP (&);
8277 BINARY_OP (+);
8278 BINARY_OP (-);
8279 BINARY_OP (<);
8280 BINARY_OP (>);
8281#undef UNARY_OP
8282#undef BINARY_OP
8283 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8284 bfd_set_error (bfd_error_invalid_operation);
8285 return FALSE;
8286 }
8287}
8288
d9352518 8289static void
a0c8462f
AM
8290put_value (bfd_vma size,
8291 unsigned long chunksz,
8292 bfd *input_bfd,
8293 bfd_vma x,
8294 bfd_byte *location)
d9352518
DB
8295{
8296 location += (size - chunksz);
8297
41cd1ad1 8298 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8299 {
8300 switch (chunksz)
8301 {
d9352518
DB
8302 case 1:
8303 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8304 x >>= 8;
d9352518
DB
8305 break;
8306 case 2:
8307 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8308 x >>= 16;
d9352518
DB
8309 break;
8310 case 4:
8311 bfd_put_32 (input_bfd, x, location);
65164438
NC
8312 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8313 x >>= 16;
8314 x >>= 16;
d9352518 8315 break;
d9352518 8316#ifdef BFD64
41cd1ad1 8317 case 8:
d9352518 8318 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8319 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8320 x >>= 32;
8321 x >>= 32;
8322 break;
d9352518 8323#endif
41cd1ad1
NC
8324 default:
8325 abort ();
d9352518
DB
8326 break;
8327 }
8328 }
8329}
8330
a0c8462f
AM
8331static bfd_vma
8332get_value (bfd_vma size,
8333 unsigned long chunksz,
8334 bfd *input_bfd,
8335 bfd_byte *location)
d9352518 8336{
9b239e0e 8337 int shift;
d9352518
DB
8338 bfd_vma x = 0;
8339
9b239e0e
NC
8340 /* Sanity checks. */
8341 BFD_ASSERT (chunksz <= sizeof (x)
8342 && size >= chunksz
8343 && chunksz != 0
8344 && (size % chunksz) == 0
8345 && input_bfd != NULL
8346 && location != NULL);
8347
8348 if (chunksz == sizeof (x))
8349 {
8350 BFD_ASSERT (size == chunksz);
8351
8352 /* Make sure that we do not perform an undefined shift operation.
8353 We know that size == chunksz so there will only be one iteration
8354 of the loop below. */
8355 shift = 0;
8356 }
8357 else
8358 shift = 8 * chunksz;
8359
a0c8462f 8360 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8361 {
8362 switch (chunksz)
8363 {
d9352518 8364 case 1:
9b239e0e 8365 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8366 break;
8367 case 2:
9b239e0e 8368 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8369 break;
8370 case 4:
9b239e0e 8371 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8372 break;
d9352518 8373#ifdef BFD64
9b239e0e
NC
8374 case 8:
8375 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8376 break;
9b239e0e
NC
8377#endif
8378 default:
8379 abort ();
d9352518
DB
8380 }
8381 }
8382 return x;
8383}
8384
a0c8462f
AM
8385static void
8386decode_complex_addend (unsigned long *start, /* in bits */
8387 unsigned long *oplen, /* in bits */
8388 unsigned long *len, /* in bits */
8389 unsigned long *wordsz, /* in bytes */
8390 unsigned long *chunksz, /* in bytes */
8391 unsigned long *lsb0_p,
8392 unsigned long *signed_p,
8393 unsigned long *trunc_p,
8394 unsigned long encoded)
d9352518
DB
8395{
8396 * start = encoded & 0x3F;
8397 * len = (encoded >> 6) & 0x3F;
8398 * oplen = (encoded >> 12) & 0x3F;
8399 * wordsz = (encoded >> 18) & 0xF;
8400 * chunksz = (encoded >> 22) & 0xF;
8401 * lsb0_p = (encoded >> 27) & 1;
8402 * signed_p = (encoded >> 28) & 1;
8403 * trunc_p = (encoded >> 29) & 1;
8404}
8405
cdfeee4f 8406bfd_reloc_status_type
0f02bbd9 8407bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8408 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8409 bfd_byte *contents,
8410 Elf_Internal_Rela *rel,
8411 bfd_vma relocation)
d9352518 8412{
0f02bbd9
AM
8413 bfd_vma shift, x, mask;
8414 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8415 bfd_reloc_status_type r;
d9352518
DB
8416
8417 /* Perform this reloc, since it is complex.
8418 (this is not to say that it necessarily refers to a complex
8419 symbol; merely that it is a self-describing CGEN based reloc.
8420 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8421 word size, etc) encoded within it.). */
d9352518 8422
a0c8462f
AM
8423 decode_complex_addend (&start, &oplen, &len, &wordsz,
8424 &chunksz, &lsb0_p, &signed_p,
8425 &trunc_p, rel->r_addend);
d9352518
DB
8426
8427 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8428
8429 if (lsb0_p)
8430 shift = (start + 1) - len;
8431 else
8432 shift = (8 * wordsz) - (start + len);
8433
37b01f6a
DG
8434 x = get_value (wordsz, chunksz, input_bfd,
8435 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8436
8437#ifdef DEBUG
8438 printf ("Doing complex reloc: "
8439 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8440 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8441 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8442 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8443 oplen, (unsigned long) x, (unsigned long) mask,
8444 (unsigned long) relocation);
d9352518
DB
8445#endif
8446
cdfeee4f 8447 r = bfd_reloc_ok;
d9352518 8448 if (! trunc_p)
cdfeee4f
AM
8449 /* Now do an overflow check. */
8450 r = bfd_check_overflow ((signed_p
8451 ? complain_overflow_signed
8452 : complain_overflow_unsigned),
8453 len, 0, (8 * wordsz),
8454 relocation);
a0c8462f 8455
d9352518
DB
8456 /* Do the deed. */
8457 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8458
8459#ifdef DEBUG
8460 printf (" relocation: %8.8lx\n"
8461 " shifted mask: %8.8lx\n"
8462 " shifted/masked reloc: %8.8lx\n"
8463 " result: %8.8lx\n",
9ccb8af9
AM
8464 (unsigned long) relocation, (unsigned long) (mask << shift),
8465 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8466#endif
37b01f6a
DG
8467 put_value (wordsz, chunksz, input_bfd, x,
8468 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8469 return r;
d9352518
DB
8470}
8471
0e287786
AM
8472/* Functions to read r_offset from external (target order) reloc
8473 entry. Faster than bfd_getl32 et al, because we let the compiler
8474 know the value is aligned. */
53df40a4 8475
0e287786
AM
8476static bfd_vma
8477ext32l_r_offset (const void *p)
53df40a4
AM
8478{
8479 union aligned32
8480 {
8481 uint32_t v;
8482 unsigned char c[4];
8483 };
8484 const union aligned32 *a
0e287786 8485 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8486
8487 uint32_t aval = ( (uint32_t) a->c[0]
8488 | (uint32_t) a->c[1] << 8
8489 | (uint32_t) a->c[2] << 16
8490 | (uint32_t) a->c[3] << 24);
0e287786 8491 return aval;
53df40a4
AM
8492}
8493
0e287786
AM
8494static bfd_vma
8495ext32b_r_offset (const void *p)
53df40a4
AM
8496{
8497 union aligned32
8498 {
8499 uint32_t v;
8500 unsigned char c[4];
8501 };
8502 const union aligned32 *a
0e287786 8503 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8504
8505 uint32_t aval = ( (uint32_t) a->c[0] << 24
8506 | (uint32_t) a->c[1] << 16
8507 | (uint32_t) a->c[2] << 8
8508 | (uint32_t) a->c[3]);
0e287786 8509 return aval;
53df40a4
AM
8510}
8511
8512#ifdef BFD_HOST_64_BIT
0e287786
AM
8513static bfd_vma
8514ext64l_r_offset (const void *p)
53df40a4
AM
8515{
8516 union aligned64
8517 {
8518 uint64_t v;
8519 unsigned char c[8];
8520 };
8521 const union aligned64 *a
0e287786 8522 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8523
8524 uint64_t aval = ( (uint64_t) a->c[0]
8525 | (uint64_t) a->c[1] << 8
8526 | (uint64_t) a->c[2] << 16
8527 | (uint64_t) a->c[3] << 24
8528 | (uint64_t) a->c[4] << 32
8529 | (uint64_t) a->c[5] << 40
8530 | (uint64_t) a->c[6] << 48
8531 | (uint64_t) a->c[7] << 56);
0e287786 8532 return aval;
53df40a4
AM
8533}
8534
0e287786
AM
8535static bfd_vma
8536ext64b_r_offset (const void *p)
53df40a4
AM
8537{
8538 union aligned64
8539 {
8540 uint64_t v;
8541 unsigned char c[8];
8542 };
8543 const union aligned64 *a
0e287786 8544 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8545
8546 uint64_t aval = ( (uint64_t) a->c[0] << 56
8547 | (uint64_t) a->c[1] << 48
8548 | (uint64_t) a->c[2] << 40
8549 | (uint64_t) a->c[3] << 32
8550 | (uint64_t) a->c[4] << 24
8551 | (uint64_t) a->c[5] << 16
8552 | (uint64_t) a->c[6] << 8
8553 | (uint64_t) a->c[7]);
0e287786 8554 return aval;
53df40a4
AM
8555}
8556#endif
8557
c152c796
AM
8558/* When performing a relocatable link, the input relocations are
8559 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8560 referenced must be updated. Update all the relocations found in
8561 RELDATA. */
c152c796 8562
bca6d0e3 8563static bfd_boolean
c152c796 8564elf_link_adjust_relocs (bfd *abfd,
9eaff861 8565 asection *sec,
28dbcedc 8566 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8567 bfd_boolean sort,
8568 struct bfd_link_info *info)
c152c796
AM
8569{
8570 unsigned int i;
8571 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8572 bfd_byte *erela;
8573 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8574 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8575 bfd_vma r_type_mask;
8576 int r_sym_shift;
d4730f92
BS
8577 unsigned int count = reldata->count;
8578 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8579
d4730f92 8580 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8581 {
8582 swap_in = bed->s->swap_reloc_in;
8583 swap_out = bed->s->swap_reloc_out;
8584 }
d4730f92 8585 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8586 {
8587 swap_in = bed->s->swap_reloca_in;
8588 swap_out = bed->s->swap_reloca_out;
8589 }
8590 else
8591 abort ();
8592
8593 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8594 abort ();
8595
8596 if (bed->s->arch_size == 32)
8597 {
8598 r_type_mask = 0xff;
8599 r_sym_shift = 8;
8600 }
8601 else
8602 {
8603 r_type_mask = 0xffffffff;
8604 r_sym_shift = 32;
8605 }
8606
d4730f92
BS
8607 erela = reldata->hdr->contents;
8608 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8609 {
8610 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8611 unsigned int j;
8612
8613 if (*rel_hash == NULL)
8614 continue;
8615
10bbbc1d
NC
8616 if ((*rel_hash)->indx == -2
8617 && info->gc_sections
8618 && ! info->gc_keep_exported)
8619 {
8620 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8621 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8622 abfd, sec,
8623 (*rel_hash)->root.root.string);
8624 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
d42c267e 8625 abfd, sec);
10bbbc1d
NC
8626 bfd_set_error (bfd_error_invalid_operation);
8627 return FALSE;
8628 }
c152c796
AM
8629 BFD_ASSERT ((*rel_hash)->indx >= 0);
8630
8631 (*swap_in) (abfd, erela, irela);
8632 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8633 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8634 | (irela[j].r_info & r_type_mask));
8635 (*swap_out) (abfd, irela, erela);
8636 }
53df40a4 8637
9eaff861
AO
8638 if (bed->elf_backend_update_relocs)
8639 (*bed->elf_backend_update_relocs) (sec, reldata);
8640
0e287786 8641 if (sort && count != 0)
53df40a4 8642 {
0e287786
AM
8643 bfd_vma (*ext_r_off) (const void *);
8644 bfd_vma r_off;
8645 size_t elt_size;
8646 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8647 bfd_byte *buf = NULL;
28dbcedc
AM
8648
8649 if (bed->s->arch_size == 32)
8650 {
8651 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8652 ext_r_off = ext32l_r_offset;
28dbcedc 8653 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8654 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8655 else
8656 abort ();
8657 }
53df40a4 8658 else
28dbcedc 8659 {
53df40a4 8660#ifdef BFD_HOST_64_BIT
28dbcedc 8661 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8662 ext_r_off = ext64l_r_offset;
28dbcedc 8663 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8664 ext_r_off = ext64b_r_offset;
28dbcedc 8665 else
53df40a4 8666#endif
28dbcedc
AM
8667 abort ();
8668 }
0e287786 8669
bca6d0e3
AM
8670 /* Must use a stable sort here. A modified insertion sort,
8671 since the relocs are mostly sorted already. */
0e287786
AM
8672 elt_size = reldata->hdr->sh_entsize;
8673 base = reldata->hdr->contents;
8674 end = base + count * elt_size;
bca6d0e3 8675 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8676 abort ();
8677
8678 /* Ensure the first element is lowest. This acts as a sentinel,
8679 speeding the main loop below. */
8680 r_off = (*ext_r_off) (base);
8681 for (p = loc = base; (p += elt_size) < end; )
8682 {
8683 bfd_vma r_off2 = (*ext_r_off) (p);
8684 if (r_off > r_off2)
8685 {
8686 r_off = r_off2;
8687 loc = p;
8688 }
8689 }
8690 if (loc != base)
8691 {
8692 /* Don't just swap *base and *loc as that changes the order
8693 of the original base[0] and base[1] if they happen to
8694 have the same r_offset. */
bca6d0e3
AM
8695 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8696 memcpy (onebuf, loc, elt_size);
0e287786 8697 memmove (base + elt_size, base, loc - base);
bca6d0e3 8698 memcpy (base, onebuf, elt_size);
0e287786
AM
8699 }
8700
b29b8669 8701 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8702 {
8703 /* base to p is sorted, *p is next to insert. */
8704 r_off = (*ext_r_off) (p);
8705 /* Search the sorted region for location to insert. */
8706 loc = p - elt_size;
8707 while (r_off < (*ext_r_off) (loc))
8708 loc -= elt_size;
8709 loc += elt_size;
8710 if (loc != p)
8711 {
bca6d0e3
AM
8712 /* Chances are there is a run of relocs to insert here,
8713 from one of more input files. Files are not always
8714 linked in order due to the way elf_link_input_bfd is
8715 called. See pr17666. */
8716 size_t sortlen = p - loc;
8717 bfd_vma r_off2 = (*ext_r_off) (loc);
8718 size_t runlen = elt_size;
8719 size_t buf_size = 96 * 1024;
8720 while (p + runlen < end
8721 && (sortlen <= buf_size
8722 || runlen + elt_size <= buf_size)
8723 && r_off2 > (*ext_r_off) (p + runlen))
8724 runlen += elt_size;
8725 if (buf == NULL)
8726 {
8727 buf = bfd_malloc (buf_size);
8728 if (buf == NULL)
8729 return FALSE;
8730 }
8731 if (runlen < sortlen)
8732 {
8733 memcpy (buf, p, runlen);
8734 memmove (loc + runlen, loc, sortlen);
8735 memcpy (loc, buf, runlen);
8736 }
8737 else
8738 {
8739 memcpy (buf, loc, sortlen);
8740 memmove (loc, p, runlen);
8741 memcpy (loc + runlen, buf, sortlen);
8742 }
b29b8669 8743 p += runlen - elt_size;
0e287786
AM
8744 }
8745 }
8746 /* Hashes are no longer valid. */
28dbcedc
AM
8747 free (reldata->hashes);
8748 reldata->hashes = NULL;
bca6d0e3 8749 free (buf);
53df40a4 8750 }
bca6d0e3 8751 return TRUE;
c152c796
AM
8752}
8753
8754struct elf_link_sort_rela
8755{
8756 union {
8757 bfd_vma offset;
8758 bfd_vma sym_mask;
8759 } u;
8760 enum elf_reloc_type_class type;
8761 /* We use this as an array of size int_rels_per_ext_rel. */
8762 Elf_Internal_Rela rela[1];
8763};
8764
8765static int
8766elf_link_sort_cmp1 (const void *A, const void *B)
8767{
a50b1753
NC
8768 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8769 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8770 int relativea, relativeb;
8771
8772 relativea = a->type == reloc_class_relative;
8773 relativeb = b->type == reloc_class_relative;
8774
8775 if (relativea < relativeb)
8776 return 1;
8777 if (relativea > relativeb)
8778 return -1;
8779 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8780 return -1;
8781 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8782 return 1;
8783 if (a->rela->r_offset < b->rela->r_offset)
8784 return -1;
8785 if (a->rela->r_offset > b->rela->r_offset)
8786 return 1;
8787 return 0;
8788}
8789
8790static int
8791elf_link_sort_cmp2 (const void *A, const void *B)
8792{
a50b1753
NC
8793 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8794 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8795
7e612e98 8796 if (a->type < b->type)
c152c796 8797 return -1;
7e612e98 8798 if (a->type > b->type)
c152c796 8799 return 1;
7e612e98 8800 if (a->u.offset < b->u.offset)
c152c796 8801 return -1;
7e612e98 8802 if (a->u.offset > b->u.offset)
c152c796
AM
8803 return 1;
8804 if (a->rela->r_offset < b->rela->r_offset)
8805 return -1;
8806 if (a->rela->r_offset > b->rela->r_offset)
8807 return 1;
8808 return 0;
8809}
8810
8811static size_t
8812elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8813{
3410fea8 8814 asection *dynamic_relocs;
fc66a176
L
8815 asection *rela_dyn;
8816 asection *rel_dyn;
c152c796
AM
8817 bfd_size_type count, size;
8818 size_t i, ret, sort_elt, ext_size;
8819 bfd_byte *sort, *s_non_relative, *p;
8820 struct elf_link_sort_rela *sq;
8821 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8822 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8823 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8824 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8825 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8826 struct bfd_link_order *lo;
8827 bfd_vma r_sym_mask;
3410fea8 8828 bfd_boolean use_rela;
c152c796 8829
3410fea8
NC
8830 /* Find a dynamic reloc section. */
8831 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8832 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8833 if (rela_dyn != NULL && rela_dyn->size > 0
8834 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8835 {
3410fea8
NC
8836 bfd_boolean use_rela_initialised = FALSE;
8837
8838 /* This is just here to stop gcc from complaining.
c8e44c6d 8839 Its initialization checking code is not perfect. */
3410fea8
NC
8840 use_rela = TRUE;
8841
8842 /* Both sections are present. Examine the sizes
8843 of the indirect sections to help us choose. */
8844 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8845 if (lo->type == bfd_indirect_link_order)
8846 {
8847 asection *o = lo->u.indirect.section;
8848
8849 if ((o->size % bed->s->sizeof_rela) == 0)
8850 {
8851 if ((o->size % bed->s->sizeof_rel) == 0)
8852 /* Section size is divisible by both rel and rela sizes.
8853 It is of no help to us. */
8854 ;
8855 else
8856 {
8857 /* Section size is only divisible by rela. */
535b785f 8858 if (use_rela_initialised && !use_rela)
3410fea8 8859 {
c8e44c6d
AM
8860 _bfd_error_handler (_("%B: Unable to sort relocs - "
8861 "they are in more than one size"),
8862 abfd);
3410fea8
NC
8863 bfd_set_error (bfd_error_invalid_operation);
8864 return 0;
8865 }
8866 else
8867 {
8868 use_rela = TRUE;
8869 use_rela_initialised = TRUE;
8870 }
8871 }
8872 }
8873 else if ((o->size % bed->s->sizeof_rel) == 0)
8874 {
8875 /* Section size is only divisible by rel. */
535b785f 8876 if (use_rela_initialised && use_rela)
3410fea8 8877 {
c8e44c6d
AM
8878 _bfd_error_handler (_("%B: Unable to sort relocs - "
8879 "they are in more than one size"),
8880 abfd);
3410fea8
NC
8881 bfd_set_error (bfd_error_invalid_operation);
8882 return 0;
8883 }
8884 else
8885 {
8886 use_rela = FALSE;
8887 use_rela_initialised = TRUE;
8888 }
8889 }
8890 else
8891 {
c8e44c6d
AM
8892 /* The section size is not divisible by either -
8893 something is wrong. */
8894 _bfd_error_handler (_("%B: Unable to sort relocs - "
8895 "they are of an unknown size"), abfd);
3410fea8
NC
8896 bfd_set_error (bfd_error_invalid_operation);
8897 return 0;
8898 }
8899 }
8900
8901 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8902 if (lo->type == bfd_indirect_link_order)
8903 {
8904 asection *o = lo->u.indirect.section;
8905
8906 if ((o->size % bed->s->sizeof_rela) == 0)
8907 {
8908 if ((o->size % bed->s->sizeof_rel) == 0)
8909 /* Section size is divisible by both rel and rela sizes.
8910 It is of no help to us. */
8911 ;
8912 else
8913 {
8914 /* Section size is only divisible by rela. */
535b785f 8915 if (use_rela_initialised && !use_rela)
3410fea8 8916 {
c8e44c6d
AM
8917 _bfd_error_handler (_("%B: Unable to sort relocs - "
8918 "they are in more than one size"),
8919 abfd);
3410fea8
NC
8920 bfd_set_error (bfd_error_invalid_operation);
8921 return 0;
8922 }
8923 else
8924 {
8925 use_rela = TRUE;
8926 use_rela_initialised = TRUE;
8927 }
8928 }
8929 }
8930 else if ((o->size % bed->s->sizeof_rel) == 0)
8931 {
8932 /* Section size is only divisible by rel. */
535b785f 8933 if (use_rela_initialised && use_rela)
3410fea8 8934 {
c8e44c6d
AM
8935 _bfd_error_handler (_("%B: Unable to sort relocs - "
8936 "they are in more than one size"),
8937 abfd);
3410fea8
NC
8938 bfd_set_error (bfd_error_invalid_operation);
8939 return 0;
8940 }
8941 else
8942 {
8943 use_rela = FALSE;
8944 use_rela_initialised = TRUE;
8945 }
8946 }
8947 else
8948 {
c8e44c6d
AM
8949 /* The section size is not divisible by either -
8950 something is wrong. */
8951 _bfd_error_handler (_("%B: Unable to sort relocs - "
8952 "they are of an unknown size"), abfd);
3410fea8
NC
8953 bfd_set_error (bfd_error_invalid_operation);
8954 return 0;
8955 }
8956 }
8957
8958 if (! use_rela_initialised)
8959 /* Make a guess. */
8960 use_rela = TRUE;
c152c796 8961 }
fc66a176
L
8962 else if (rela_dyn != NULL && rela_dyn->size > 0)
8963 use_rela = TRUE;
8964 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8965 use_rela = FALSE;
c152c796 8966 else
fc66a176 8967 return 0;
3410fea8
NC
8968
8969 if (use_rela)
c152c796 8970 {
3410fea8 8971 dynamic_relocs = rela_dyn;
c152c796
AM
8972 ext_size = bed->s->sizeof_rela;
8973 swap_in = bed->s->swap_reloca_in;
8974 swap_out = bed->s->swap_reloca_out;
8975 }
3410fea8
NC
8976 else
8977 {
8978 dynamic_relocs = rel_dyn;
8979 ext_size = bed->s->sizeof_rel;
8980 swap_in = bed->s->swap_reloc_in;
8981 swap_out = bed->s->swap_reloc_out;
8982 }
c152c796
AM
8983
8984 size = 0;
3410fea8 8985 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8986 if (lo->type == bfd_indirect_link_order)
3410fea8 8987 size += lo->u.indirect.section->size;
c152c796 8988
3410fea8 8989 if (size != dynamic_relocs->size)
c152c796
AM
8990 return 0;
8991
8992 sort_elt = (sizeof (struct elf_link_sort_rela)
8993 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8994
8995 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8996 if (count == 0)
8997 return 0;
a50b1753 8998 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8999
c152c796
AM
9000 if (sort == NULL)
9001 {
9002 (*info->callbacks->warning)
9003 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9004 return 0;
9005 }
9006
9007 if (bed->s->arch_size == 32)
9008 r_sym_mask = ~(bfd_vma) 0xff;
9009 else
9010 r_sym_mask = ~(bfd_vma) 0xffffffff;
9011
3410fea8 9012 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9013 if (lo->type == bfd_indirect_link_order)
9014 {
9015 bfd_byte *erel, *erelend;
9016 asection *o = lo->u.indirect.section;
9017
1da212d6
AM
9018 if (o->contents == NULL && o->size != 0)
9019 {
9020 /* This is a reloc section that is being handled as a normal
9021 section. See bfd_section_from_shdr. We can't combine
9022 relocs in this case. */
9023 free (sort);
9024 return 0;
9025 }
c152c796 9026 erel = o->contents;
eea6121a 9027 erelend = o->contents + o->size;
c8e44c6d 9028 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9029
c152c796
AM
9030 while (erel < erelend)
9031 {
9032 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9033
c152c796 9034 (*swap_in) (abfd, erel, s->rela);
7e612e98 9035 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9036 s->u.sym_mask = r_sym_mask;
9037 p += sort_elt;
9038 erel += ext_size;
9039 }
9040 }
9041
9042 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9043
9044 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9045 {
9046 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9047 if (s->type != reloc_class_relative)
9048 break;
9049 }
9050 ret = i;
9051 s_non_relative = p;
9052
9053 sq = (struct elf_link_sort_rela *) s_non_relative;
9054 for (; i < count; i++, p += sort_elt)
9055 {
9056 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9057 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9058 sq = sp;
9059 sp->u.offset = sq->rela->r_offset;
9060 }
9061
9062 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9063
c8e44c6d
AM
9064 struct elf_link_hash_table *htab = elf_hash_table (info);
9065 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9066 {
9067 /* We have plt relocs in .rela.dyn. */
9068 sq = (struct elf_link_sort_rela *) sort;
9069 for (i = 0; i < count; i++)
9070 if (sq[count - i - 1].type != reloc_class_plt)
9071 break;
9072 if (i != 0 && htab->srelplt->size == i * ext_size)
9073 {
9074 struct bfd_link_order **plo;
9075 /* Put srelplt link_order last. This is so the output_offset
9076 set in the next loop is correct for DT_JMPREL. */
9077 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9078 if ((*plo)->type == bfd_indirect_link_order
9079 && (*plo)->u.indirect.section == htab->srelplt)
9080 {
9081 lo = *plo;
9082 *plo = lo->next;
9083 }
9084 else
9085 plo = &(*plo)->next;
9086 *plo = lo;
9087 lo->next = NULL;
9088 dynamic_relocs->map_tail.link_order = lo;
9089 }
9090 }
9091
9092 p = sort;
3410fea8 9093 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9094 if (lo->type == bfd_indirect_link_order)
9095 {
9096 bfd_byte *erel, *erelend;
9097 asection *o = lo->u.indirect.section;
9098
9099 erel = o->contents;
eea6121a 9100 erelend = o->contents + o->size;
c8e44c6d 9101 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9102 while (erel < erelend)
9103 {
9104 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9105 (*swap_out) (abfd, s->rela, erel);
9106 p += sort_elt;
9107 erel += ext_size;
9108 }
9109 }
9110
9111 free (sort);
3410fea8 9112 *psec = dynamic_relocs;
c152c796
AM
9113 return ret;
9114}
9115
ef10c3ac 9116/* Add a symbol to the output symbol string table. */
c152c796 9117
6e0b88f1 9118static int
ef10c3ac
L
9119elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9120 const char *name,
9121 Elf_Internal_Sym *elfsym,
9122 asection *input_sec,
9123 struct elf_link_hash_entry *h)
c152c796 9124{
6e0b88f1 9125 int (*output_symbol_hook)
c152c796
AM
9126 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9127 struct elf_link_hash_entry *);
ef10c3ac 9128 struct elf_link_hash_table *hash_table;
c152c796 9129 const struct elf_backend_data *bed;
ef10c3ac 9130 bfd_size_type strtabsize;
c152c796 9131
8539e4e8
AM
9132 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9133
8b127cbc 9134 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9135 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9136 if (output_symbol_hook != NULL)
9137 {
8b127cbc 9138 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9139 if (ret != 1)
9140 return ret;
c152c796
AM
9141 }
9142
ef10c3ac
L
9143 if (name == NULL
9144 || *name == '\0'
9145 || (input_sec->flags & SEC_EXCLUDE))
9146 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9147 else
9148 {
ef10c3ac
L
9149 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9150 to get the final offset for st_name. */
9151 elfsym->st_name
9152 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9153 name, FALSE);
c152c796 9154 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9155 return 0;
c152c796
AM
9156 }
9157
ef10c3ac
L
9158 hash_table = elf_hash_table (flinfo->info);
9159 strtabsize = hash_table->strtabsize;
9160 if (strtabsize <= hash_table->strtabcount)
c152c796 9161 {
ef10c3ac
L
9162 strtabsize += strtabsize;
9163 hash_table->strtabsize = strtabsize;
9164 strtabsize *= sizeof (*hash_table->strtab);
9165 hash_table->strtab
9166 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9167 strtabsize);
9168 if (hash_table->strtab == NULL)
6e0b88f1 9169 return 0;
c152c796 9170 }
ef10c3ac
L
9171 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9172 hash_table->strtab[hash_table->strtabcount].dest_index
9173 = hash_table->strtabcount;
9174 hash_table->strtab[hash_table->strtabcount].destshndx_index
9175 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9176
9177 bfd_get_symcount (flinfo->output_bfd) += 1;
9178 hash_table->strtabcount += 1;
9179
9180 return 1;
9181}
9182
9183/* Swap symbols out to the symbol table and flush the output symbols to
9184 the file. */
9185
9186static bfd_boolean
9187elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9188{
9189 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9190 bfd_size_type amt;
9191 size_t i;
ef10c3ac
L
9192 const struct elf_backend_data *bed;
9193 bfd_byte *symbuf;
9194 Elf_Internal_Shdr *hdr;
9195 file_ptr pos;
9196 bfd_boolean ret;
9197
9198 if (!hash_table->strtabcount)
9199 return TRUE;
9200
9201 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9202
9203 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9204
ef10c3ac
L
9205 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9206 symbuf = (bfd_byte *) bfd_malloc (amt);
9207 if (symbuf == NULL)
9208 return FALSE;
1b786873 9209
ef10c3ac 9210 if (flinfo->symshndxbuf)
c152c796 9211 {
ef53be89
AM
9212 amt = sizeof (Elf_External_Sym_Shndx);
9213 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9214 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9215 if (flinfo->symshndxbuf == NULL)
c152c796 9216 {
ef10c3ac
L
9217 free (symbuf);
9218 return FALSE;
c152c796 9219 }
c152c796
AM
9220 }
9221
ef10c3ac
L
9222 for (i = 0; i < hash_table->strtabcount; i++)
9223 {
9224 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9225 if (elfsym->sym.st_name == (unsigned long) -1)
9226 elfsym->sym.st_name = 0;
9227 else
9228 elfsym->sym.st_name
9229 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9230 elfsym->sym.st_name);
9231 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9232 ((bfd_byte *) symbuf
9233 + (elfsym->dest_index
9234 * bed->s->sizeof_sym)),
9235 (flinfo->symshndxbuf
9236 + elfsym->destshndx_index));
9237 }
9238
9239 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9240 pos = hdr->sh_offset + hdr->sh_size;
9241 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9242 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9243 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9244 {
9245 hdr->sh_size += amt;
9246 ret = TRUE;
9247 }
9248 else
9249 ret = FALSE;
c152c796 9250
ef10c3ac
L
9251 free (symbuf);
9252
9253 free (hash_table->strtab);
9254 hash_table->strtab = NULL;
9255
9256 return ret;
c152c796
AM
9257}
9258
c0d5a53d
L
9259/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9260
9261static bfd_boolean
9262check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9263{
4fbb74a6
AM
9264 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9265 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9266 {
9267 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9268 beyond 64k. */
4eca0228 9269 _bfd_error_handler
695344c0 9270 /* xgettext:c-format */
c0d5a53d 9271 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9272 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9273 bfd_set_error (bfd_error_nonrepresentable_section);
9274 return FALSE;
9275 }
9276 return TRUE;
9277}
9278
c152c796
AM
9279/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9280 allowing an unsatisfied unversioned symbol in the DSO to match a
9281 versioned symbol that would normally require an explicit version.
9282 We also handle the case that a DSO references a hidden symbol
9283 which may be satisfied by a versioned symbol in another DSO. */
9284
9285static bfd_boolean
9286elf_link_check_versioned_symbol (struct bfd_link_info *info,
9287 const struct elf_backend_data *bed,
9288 struct elf_link_hash_entry *h)
9289{
9290 bfd *abfd;
9291 struct elf_link_loaded_list *loaded;
9292
9293 if (!is_elf_hash_table (info->hash))
9294 return FALSE;
9295
90c984fc
L
9296 /* Check indirect symbol. */
9297 while (h->root.type == bfd_link_hash_indirect)
9298 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9299
c152c796
AM
9300 switch (h->root.type)
9301 {
9302 default:
9303 abfd = NULL;
9304 break;
9305
9306 case bfd_link_hash_undefined:
9307 case bfd_link_hash_undefweak:
9308 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9309 if (abfd == NULL
9310 || (abfd->flags & DYNAMIC) == 0
e56f61be 9311 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9312 return FALSE;
9313 break;
9314
9315 case bfd_link_hash_defined:
9316 case bfd_link_hash_defweak:
9317 abfd = h->root.u.def.section->owner;
9318 break;
9319
9320 case bfd_link_hash_common:
9321 abfd = h->root.u.c.p->section->owner;
9322 break;
9323 }
9324 BFD_ASSERT (abfd != NULL);
9325
9326 for (loaded = elf_hash_table (info)->loaded;
9327 loaded != NULL;
9328 loaded = loaded->next)
9329 {
9330 bfd *input;
9331 Elf_Internal_Shdr *hdr;
ef53be89
AM
9332 size_t symcount;
9333 size_t extsymcount;
9334 size_t extsymoff;
c152c796
AM
9335 Elf_Internal_Shdr *versymhdr;
9336 Elf_Internal_Sym *isym;
9337 Elf_Internal_Sym *isymend;
9338 Elf_Internal_Sym *isymbuf;
9339 Elf_External_Versym *ever;
9340 Elf_External_Versym *extversym;
9341
9342 input = loaded->abfd;
9343
9344 /* We check each DSO for a possible hidden versioned definition. */
9345 if (input == abfd
9346 || (input->flags & DYNAMIC) == 0
9347 || elf_dynversym (input) == 0)
9348 continue;
9349
9350 hdr = &elf_tdata (input)->dynsymtab_hdr;
9351
9352 symcount = hdr->sh_size / bed->s->sizeof_sym;
9353 if (elf_bad_symtab (input))
9354 {
9355 extsymcount = symcount;
9356 extsymoff = 0;
9357 }
9358 else
9359 {
9360 extsymcount = symcount - hdr->sh_info;
9361 extsymoff = hdr->sh_info;
9362 }
9363
9364 if (extsymcount == 0)
9365 continue;
9366
9367 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9368 NULL, NULL, NULL);
9369 if (isymbuf == NULL)
9370 return FALSE;
9371
9372 /* Read in any version definitions. */
9373 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9374 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9375 if (extversym == NULL)
9376 goto error_ret;
9377
9378 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9379 || (bfd_bread (extversym, versymhdr->sh_size, input)
9380 != versymhdr->sh_size))
9381 {
9382 free (extversym);
9383 error_ret:
9384 free (isymbuf);
9385 return FALSE;
9386 }
9387
9388 ever = extversym + extsymoff;
9389 isymend = isymbuf + extsymcount;
9390 for (isym = isymbuf; isym < isymend; isym++, ever++)
9391 {
9392 const char *name;
9393 Elf_Internal_Versym iver;
9394 unsigned short version_index;
9395
9396 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9397 || isym->st_shndx == SHN_UNDEF)
9398 continue;
9399
9400 name = bfd_elf_string_from_elf_section (input,
9401 hdr->sh_link,
9402 isym->st_name);
9403 if (strcmp (name, h->root.root.string) != 0)
9404 continue;
9405
9406 _bfd_elf_swap_versym_in (input, ever, &iver);
9407
d023c380
L
9408 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9409 && !(h->def_regular
9410 && h->forced_local))
c152c796
AM
9411 {
9412 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9413 have provided a definition for the undefined sym unless
9414 it is defined in a non-shared object and forced local.
9415 */
c152c796
AM
9416 abort ();
9417 }
9418
9419 version_index = iver.vs_vers & VERSYM_VERSION;
9420 if (version_index == 1 || version_index == 2)
9421 {
9422 /* This is the base or first version. We can use it. */
9423 free (extversym);
9424 free (isymbuf);
9425 return TRUE;
9426 }
9427 }
9428
9429 free (extversym);
9430 free (isymbuf);
9431 }
9432
9433 return FALSE;
9434}
9435
b8871f35
L
9436/* Convert ELF common symbol TYPE. */
9437
9438static int
9439elf_link_convert_common_type (struct bfd_link_info *info, int type)
9440{
9441 /* Commom symbol can only appear in relocatable link. */
9442 if (!bfd_link_relocatable (info))
9443 abort ();
9444 switch (info->elf_stt_common)
9445 {
9446 case unchanged:
9447 break;
9448 case elf_stt_common:
9449 type = STT_COMMON;
9450 break;
9451 case no_elf_stt_common:
9452 type = STT_OBJECT;
9453 break;
9454 }
9455 return type;
9456}
9457
c152c796
AM
9458/* Add an external symbol to the symbol table. This is called from
9459 the hash table traversal routine. When generating a shared object,
9460 we go through the symbol table twice. The first time we output
9461 anything that might have been forced to local scope in a version
9462 script. The second time we output the symbols that are still
9463 global symbols. */
9464
9465static bfd_boolean
7686d77d 9466elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9467{
7686d77d 9468 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9469 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9470 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9471 bfd_boolean strip;
9472 Elf_Internal_Sym sym;
9473 asection *input_sec;
9474 const struct elf_backend_data *bed;
6e0b88f1
AM
9475 long indx;
9476 int ret;
b8871f35 9477 unsigned int type;
c152c796
AM
9478
9479 if (h->root.type == bfd_link_hash_warning)
9480 {
9481 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9482 if (h->root.type == bfd_link_hash_new)
9483 return TRUE;
9484 }
9485
9486 /* Decide whether to output this symbol in this pass. */
9487 if (eoinfo->localsyms)
9488 {
4deb8f71 9489 if (!h->forced_local)
c152c796
AM
9490 return TRUE;
9491 }
9492 else
9493 {
4deb8f71 9494 if (h->forced_local)
c152c796
AM
9495 return TRUE;
9496 }
9497
8b127cbc 9498 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9499
12ac1cf5 9500 if (h->root.type == bfd_link_hash_undefined)
c152c796 9501 {
12ac1cf5
NC
9502 /* If we have an undefined symbol reference here then it must have
9503 come from a shared library that is being linked in. (Undefined
98da7939
L
9504 references in regular files have already been handled unless
9505 they are in unreferenced sections which are removed by garbage
9506 collection). */
12ac1cf5
NC
9507 bfd_boolean ignore_undef = FALSE;
9508
9509 /* Some symbols may be special in that the fact that they're
9510 undefined can be safely ignored - let backend determine that. */
9511 if (bed->elf_backend_ignore_undef_symbol)
9512 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9513
9514 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9515 if (!ignore_undef
12ac1cf5 9516 && h->ref_dynamic
8b127cbc
AM
9517 && (!h->ref_regular || flinfo->info->gc_sections)
9518 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9519 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9520 (*flinfo->info->callbacks->undefined_symbol)
9521 (flinfo->info, h->root.root.string,
9522 h->ref_regular ? NULL : h->root.u.undef.abfd,
9523 NULL, 0,
9524 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9525
9526 /* Strip a global symbol defined in a discarded section. */
9527 if (h->indx == -3)
9528 return TRUE;
c152c796
AM
9529 }
9530
9531 /* We should also warn if a forced local symbol is referenced from
9532 shared libraries. */
0e1862bb 9533 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9534 && h->forced_local
9535 && h->ref_dynamic
371a5866 9536 && h->def_regular
f5385ebf 9537 && !h->dynamic_def
ee659f1f 9538 && h->ref_dynamic_nonweak
8b127cbc 9539 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9540 {
17d078c5
AM
9541 bfd *def_bfd;
9542 const char *msg;
90c984fc
L
9543 struct elf_link_hash_entry *hi = h;
9544
9545 /* Check indirect symbol. */
9546 while (hi->root.type == bfd_link_hash_indirect)
9547 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9548
9549 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9550 /* xgettext:c-format */
17d078c5
AM
9551 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9552 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9553 /* xgettext:c-format */
17d078c5
AM
9554 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9555 else
695344c0 9556 /* xgettext:c-format */
17d078c5 9557 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9558 def_bfd = flinfo->output_bfd;
90c984fc
L
9559 if (hi->root.u.def.section != bfd_abs_section_ptr)
9560 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9561 _bfd_error_handler (msg, flinfo->output_bfd,
9562 h->root.root.string, def_bfd);
17d078c5 9563 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9564 eoinfo->failed = TRUE;
9565 return FALSE;
9566 }
9567
9568 /* We don't want to output symbols that have never been mentioned by
9569 a regular file, or that we have been told to strip. However, if
9570 h->indx is set to -2, the symbol is used by a reloc and we must
9571 output it. */
d983c8c5 9572 strip = FALSE;
c152c796 9573 if (h->indx == -2)
d983c8c5 9574 ;
f5385ebf 9575 else if ((h->def_dynamic
77cfaee6
AM
9576 || h->ref_dynamic
9577 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9578 && !h->def_regular
9579 && !h->ref_regular)
c152c796 9580 strip = TRUE;
8b127cbc 9581 else if (flinfo->info->strip == strip_all)
c152c796 9582 strip = TRUE;
8b127cbc
AM
9583 else if (flinfo->info->strip == strip_some
9584 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9585 h->root.root.string, FALSE, FALSE) == NULL)
9586 strip = TRUE;
d56d55e7
AM
9587 else if ((h->root.type == bfd_link_hash_defined
9588 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9589 && ((flinfo->info->strip_discarded
dbaa2011 9590 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9591 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9592 && h->root.u.def.section->owner != NULL
d56d55e7 9593 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9594 strip = TRUE;
9e2278f5
AM
9595 else if ((h->root.type == bfd_link_hash_undefined
9596 || h->root.type == bfd_link_hash_undefweak)
9597 && h->root.u.undef.abfd != NULL
9598 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9599 strip = TRUE;
c152c796 9600
b8871f35
L
9601 type = h->type;
9602
c152c796 9603 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9604 nothing else to do. However, if it is a forced local symbol or
9605 an ifunc symbol we need to give the backend finish_dynamic_symbol
9606 function a chance to make it dynamic. */
c152c796
AM
9607 if (strip
9608 && h->dynindx == -1
b8871f35 9609 && type != STT_GNU_IFUNC
f5385ebf 9610 && !h->forced_local)
c152c796
AM
9611 return TRUE;
9612
9613 sym.st_value = 0;
9614 sym.st_size = h->size;
9615 sym.st_other = h->other;
c152c796
AM
9616 switch (h->root.type)
9617 {
9618 default:
9619 case bfd_link_hash_new:
9620 case bfd_link_hash_warning:
9621 abort ();
9622 return FALSE;
9623
9624 case bfd_link_hash_undefined:
9625 case bfd_link_hash_undefweak:
9626 input_sec = bfd_und_section_ptr;
9627 sym.st_shndx = SHN_UNDEF;
9628 break;
9629
9630 case bfd_link_hash_defined:
9631 case bfd_link_hash_defweak:
9632 {
9633 input_sec = h->root.u.def.section;
9634 if (input_sec->output_section != NULL)
9635 {
9636 sym.st_shndx =
8b127cbc 9637 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9638 input_sec->output_section);
9639 if (sym.st_shndx == SHN_BAD)
9640 {
4eca0228 9641 _bfd_error_handler
695344c0 9642 /* xgettext:c-format */
d003868e 9643 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9644 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9645 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9646 eoinfo->failed = TRUE;
9647 return FALSE;
9648 }
9649
9650 /* ELF symbols in relocatable files are section relative,
9651 but in nonrelocatable files they are virtual
9652 addresses. */
9653 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9654 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9655 {
9656 sym.st_value += input_sec->output_section->vma;
9657 if (h->type == STT_TLS)
9658 {
8b127cbc 9659 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9660 if (tls_sec != NULL)
9661 sym.st_value -= tls_sec->vma;
c152c796
AM
9662 }
9663 }
9664 }
9665 else
9666 {
9667 BFD_ASSERT (input_sec->owner == NULL
9668 || (input_sec->owner->flags & DYNAMIC) != 0);
9669 sym.st_shndx = SHN_UNDEF;
9670 input_sec = bfd_und_section_ptr;
9671 }
9672 }
9673 break;
9674
9675 case bfd_link_hash_common:
9676 input_sec = h->root.u.c.p->section;
a4d8e49b 9677 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9678 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9679 break;
9680
9681 case bfd_link_hash_indirect:
9682 /* These symbols are created by symbol versioning. They point
9683 to the decorated version of the name. For example, if the
9684 symbol foo@@GNU_1.2 is the default, which should be used when
9685 foo is used with no version, then we add an indirect symbol
9686 foo which points to foo@@GNU_1.2. We ignore these symbols,
9687 since the indirected symbol is already in the hash table. */
9688 return TRUE;
9689 }
9690
b8871f35
L
9691 if (type == STT_COMMON || type == STT_OBJECT)
9692 switch (h->root.type)
9693 {
9694 case bfd_link_hash_common:
9695 type = elf_link_convert_common_type (flinfo->info, type);
9696 break;
9697 case bfd_link_hash_defined:
9698 case bfd_link_hash_defweak:
9699 if (bed->common_definition (&sym))
9700 type = elf_link_convert_common_type (flinfo->info, type);
9701 else
9702 type = STT_OBJECT;
9703 break;
9704 case bfd_link_hash_undefined:
9705 case bfd_link_hash_undefweak:
9706 break;
9707 default:
9708 abort ();
9709 }
9710
4deb8f71 9711 if (h->forced_local)
b8871f35
L
9712 {
9713 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9714 /* Turn off visibility on local symbol. */
9715 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9716 }
9717 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9718 else if (h->unique_global && h->def_regular)
9719 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9720 else if (h->root.type == bfd_link_hash_undefweak
9721 || h->root.type == bfd_link_hash_defweak)
9722 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9723 else
9724 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9725 sym.st_target_internal = h->target_internal;
9726
c152c796
AM
9727 /* Give the processor backend a chance to tweak the symbol value,
9728 and also to finish up anything that needs to be done for this
9729 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9730 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9731 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9732 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9733 && h->def_regular
0e1862bb 9734 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9735 || ((h->dynindx != -1
9736 || h->forced_local)
0e1862bb 9737 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9738 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9739 || h->root.type != bfd_link_hash_undefweak))
9740 || !h->forced_local)
8b127cbc 9741 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9742 {
9743 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9744 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9745 {
9746 eoinfo->failed = TRUE;
9747 return FALSE;
9748 }
9749 }
9750
9751 /* If we are marking the symbol as undefined, and there are no
9752 non-weak references to this symbol from a regular object, then
9753 mark the symbol as weak undefined; if there are non-weak
9754 references, mark the symbol as strong. We can't do this earlier,
9755 because it might not be marked as undefined until the
9756 finish_dynamic_symbol routine gets through with it. */
9757 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9758 && h->ref_regular
c152c796
AM
9759 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9760 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9761 {
9762 int bindtype;
b8871f35 9763 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9764
9765 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9766 if (type == STT_GNU_IFUNC)
9767 type = STT_FUNC;
c152c796 9768
f5385ebf 9769 if (h->ref_regular_nonweak)
c152c796
AM
9770 bindtype = STB_GLOBAL;
9771 else
9772 bindtype = STB_WEAK;
2955ec4c 9773 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9774 }
9775
bda987c2
CD
9776 /* If this is a symbol defined in a dynamic library, don't use the
9777 symbol size from the dynamic library. Relinking an executable
9778 against a new library may introduce gratuitous changes in the
9779 executable's symbols if we keep the size. */
9780 if (sym.st_shndx == SHN_UNDEF
9781 && !h->def_regular
9782 && h->def_dynamic)
9783 sym.st_size = 0;
9784
c152c796
AM
9785 /* If a non-weak symbol with non-default visibility is not defined
9786 locally, it is a fatal error. */
0e1862bb 9787 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9788 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9789 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9790 && h->root.type == bfd_link_hash_undefined
f5385ebf 9791 && !h->def_regular)
c152c796 9792 {
17d078c5
AM
9793 const char *msg;
9794
9795 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9796 /* xgettext:c-format */
17d078c5
AM
9797 msg = _("%B: protected symbol `%s' isn't defined");
9798 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9799 /* xgettext:c-format */
17d078c5
AM
9800 msg = _("%B: internal symbol `%s' isn't defined");
9801 else
695344c0 9802 /* xgettext:c-format */
17d078c5 9803 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9804 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9805 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9806 eoinfo->failed = TRUE;
9807 return FALSE;
9808 }
9809
9810 /* If this symbol should be put in the .dynsym section, then put it
9811 there now. We already know the symbol index. We also fill in
9812 the entry in the .hash section. */
cae1fbbb 9813 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9814 && h->dynindx != -1
8b127cbc 9815 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9816 {
c152c796
AM
9817 bfd_byte *esym;
9818
90c984fc
L
9819 /* Since there is no version information in the dynamic string,
9820 if there is no version info in symbol version section, we will
1659f720 9821 have a run-time problem if not linking executable, referenced
4deb8f71 9822 by shared library, or not bound locally. */
1659f720 9823 if (h->verinfo.verdef == NULL
0e1862bb 9824 && (!bfd_link_executable (flinfo->info)
1659f720
L
9825 || h->ref_dynamic
9826 || !h->def_regular))
90c984fc
L
9827 {
9828 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9829
9830 if (p && p [1] != '\0')
9831 {
4eca0228 9832 _bfd_error_handler
695344c0 9833 /* xgettext:c-format */
90c984fc
L
9834 (_("%B: No symbol version section for versioned symbol `%s'"),
9835 flinfo->output_bfd, h->root.root.string);
9836 eoinfo->failed = TRUE;
9837 return FALSE;
9838 }
9839 }
9840
c152c796 9841 sym.st_name = h->dynstr_index;
cae1fbbb
L
9842 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9843 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9844 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9845 {
9846 eoinfo->failed = TRUE;
9847 return FALSE;
9848 }
8b127cbc 9849 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9850
8b127cbc 9851 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9852 {
9853 size_t hash_entry_size;
9854 bfd_byte *bucketpos;
9855 bfd_vma chain;
41198d0c
L
9856 size_t bucketcount;
9857 size_t bucket;
9858
8b127cbc 9859 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9860 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9861
9862 hash_entry_size
8b127cbc
AM
9863 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9864 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9865 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9866 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9867 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9868 bucketpos);
9869 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9870 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9871 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9872 }
c152c796 9873
8b127cbc 9874 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9875 {
9876 Elf_Internal_Versym iversym;
9877 Elf_External_Versym *eversym;
9878
f5385ebf 9879 if (!h->def_regular)
c152c796 9880 {
7b20f099
AM
9881 if (h->verinfo.verdef == NULL
9882 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9883 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9884 iversym.vs_vers = 0;
9885 else
9886 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9887 }
9888 else
9889 {
9890 if (h->verinfo.vertree == NULL)
9891 iversym.vs_vers = 1;
9892 else
9893 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9894 if (flinfo->info->create_default_symver)
3e3b46e5 9895 iversym.vs_vers++;
c152c796
AM
9896 }
9897
422f1182 9898 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9899 defined locally. */
422f1182 9900 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9901 iversym.vs_vers |= VERSYM_HIDDEN;
9902
8b127cbc 9903 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9904 eversym += h->dynindx;
8b127cbc 9905 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9906 }
9907 }
9908
d983c8c5
AM
9909 /* If the symbol is undefined, and we didn't output it to .dynsym,
9910 strip it from .symtab too. Obviously we can't do this for
9911 relocatable output or when needed for --emit-relocs. */
9912 else if (input_sec == bfd_und_section_ptr
9913 && h->indx != -2
0e1862bb 9914 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9915 return TRUE;
9916 /* Also strip others that we couldn't earlier due to dynamic symbol
9917 processing. */
9918 if (strip)
9919 return TRUE;
9920 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9921 return TRUE;
9922
2ec55de3
AM
9923 /* Output a FILE symbol so that following locals are not associated
9924 with the wrong input file. We need one for forced local symbols
9925 if we've seen more than one FILE symbol or when we have exactly
9926 one FILE symbol but global symbols are present in a file other
9927 than the one with the FILE symbol. We also need one if linker
9928 defined symbols are present. In practice these conditions are
9929 always met, so just emit the FILE symbol unconditionally. */
9930 if (eoinfo->localsyms
9931 && !eoinfo->file_sym_done
9932 && eoinfo->flinfo->filesym_count != 0)
9933 {
9934 Elf_Internal_Sym fsym;
9935
9936 memset (&fsym, 0, sizeof (fsym));
9937 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9938 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9939 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9940 bfd_und_section_ptr, NULL))
2ec55de3
AM
9941 return FALSE;
9942
9943 eoinfo->file_sym_done = TRUE;
9944 }
9945
8b127cbc 9946 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9947 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9948 input_sec, h);
6e0b88f1 9949 if (ret == 0)
c152c796
AM
9950 {
9951 eoinfo->failed = TRUE;
9952 return FALSE;
9953 }
6e0b88f1
AM
9954 else if (ret == 1)
9955 h->indx = indx;
9956 else if (h->indx == -2)
9957 abort();
c152c796
AM
9958
9959 return TRUE;
9960}
9961
cdd3575c
AM
9962/* Return TRUE if special handling is done for relocs in SEC against
9963 symbols defined in discarded sections. */
9964
c152c796
AM
9965static bfd_boolean
9966elf_section_ignore_discarded_relocs (asection *sec)
9967{
9968 const struct elf_backend_data *bed;
9969
cdd3575c
AM
9970 switch (sec->sec_info_type)
9971 {
dbaa2011
AM
9972 case SEC_INFO_TYPE_STABS:
9973 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9974 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9975 return TRUE;
9976 default:
9977 break;
9978 }
c152c796
AM
9979
9980 bed = get_elf_backend_data (sec->owner);
9981 if (bed->elf_backend_ignore_discarded_relocs != NULL
9982 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9983 return TRUE;
9984
9985 return FALSE;
9986}
9987
9e66c942
AM
9988/* Return a mask saying how ld should treat relocations in SEC against
9989 symbols defined in discarded sections. If this function returns
9990 COMPLAIN set, ld will issue a warning message. If this function
9991 returns PRETEND set, and the discarded section was link-once and the
9992 same size as the kept link-once section, ld will pretend that the
9993 symbol was actually defined in the kept section. Otherwise ld will
9994 zero the reloc (at least that is the intent, but some cooperation by
9995 the target dependent code is needed, particularly for REL targets). */
9996
8a696751
AM
9997unsigned int
9998_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9999{
9e66c942 10000 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10001 return PRETEND;
cdd3575c
AM
10002
10003 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10004 return 0;
cdd3575c
AM
10005
10006 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10007 return 0;
cdd3575c 10008
9e66c942 10009 return COMPLAIN | PRETEND;
cdd3575c
AM
10010}
10011
3d7f7666
L
10012/* Find a match between a section and a member of a section group. */
10013
10014static asection *
c0f00686
L
10015match_group_member (asection *sec, asection *group,
10016 struct bfd_link_info *info)
3d7f7666
L
10017{
10018 asection *first = elf_next_in_group (group);
10019 asection *s = first;
10020
10021 while (s != NULL)
10022 {
c0f00686 10023 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10024 return s;
10025
83180ade 10026 s = elf_next_in_group (s);
3d7f7666
L
10027 if (s == first)
10028 break;
10029 }
10030
10031 return NULL;
10032}
10033
01b3c8ab 10034/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10035 to replace it. Return the replacement if it is OK. Otherwise return
10036 NULL. */
01b3c8ab
L
10037
10038asection *
c0f00686 10039_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10040{
10041 asection *kept;
10042
10043 kept = sec->kept_section;
10044 if (kept != NULL)
10045 {
c2370991 10046 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10047 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10048 if (kept != NULL
10049 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10050 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10051 kept = NULL;
c2370991 10052 sec->kept_section = kept;
01b3c8ab
L
10053 }
10054 return kept;
10055}
10056
c152c796
AM
10057/* Link an input file into the linker output file. This function
10058 handles all the sections and relocations of the input file at once.
10059 This is so that we only have to read the local symbols once, and
10060 don't have to keep them in memory. */
10061
10062static bfd_boolean
8b127cbc 10063elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10064{
ece5ef60 10065 int (*relocate_section)
c152c796
AM
10066 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10067 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10068 bfd *output_bfd;
10069 Elf_Internal_Shdr *symtab_hdr;
10070 size_t locsymcount;
10071 size_t extsymoff;
10072 Elf_Internal_Sym *isymbuf;
10073 Elf_Internal_Sym *isym;
10074 Elf_Internal_Sym *isymend;
10075 long *pindex;
10076 asection **ppsection;
10077 asection *o;
10078 const struct elf_backend_data *bed;
c152c796 10079 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10080 bfd_size_type address_size;
10081 bfd_vma r_type_mask;
10082 int r_sym_shift;
ffbc01cc 10083 bfd_boolean have_file_sym = FALSE;
c152c796 10084
8b127cbc 10085 output_bfd = flinfo->output_bfd;
c152c796
AM
10086 bed = get_elf_backend_data (output_bfd);
10087 relocate_section = bed->elf_backend_relocate_section;
10088
10089 /* If this is a dynamic object, we don't want to do anything here:
10090 we don't want the local symbols, and we don't want the section
10091 contents. */
10092 if ((input_bfd->flags & DYNAMIC) != 0)
10093 return TRUE;
10094
c152c796
AM
10095 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10096 if (elf_bad_symtab (input_bfd))
10097 {
10098 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10099 extsymoff = 0;
10100 }
10101 else
10102 {
10103 locsymcount = symtab_hdr->sh_info;
10104 extsymoff = symtab_hdr->sh_info;
10105 }
10106
10107 /* Read the local symbols. */
10108 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10109 if (isymbuf == NULL && locsymcount != 0)
10110 {
10111 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10112 flinfo->internal_syms,
10113 flinfo->external_syms,
10114 flinfo->locsym_shndx);
c152c796
AM
10115 if (isymbuf == NULL)
10116 return FALSE;
10117 }
10118
10119 /* Find local symbol sections and adjust values of symbols in
10120 SEC_MERGE sections. Write out those local symbols we know are
10121 going into the output file. */
10122 isymend = isymbuf + locsymcount;
8b127cbc 10123 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10124 isym < isymend;
10125 isym++, pindex++, ppsection++)
10126 {
10127 asection *isec;
10128 const char *name;
10129 Elf_Internal_Sym osym;
6e0b88f1
AM
10130 long indx;
10131 int ret;
c152c796
AM
10132
10133 *pindex = -1;
10134
10135 if (elf_bad_symtab (input_bfd))
10136 {
10137 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10138 {
10139 *ppsection = NULL;
10140 continue;
10141 }
10142 }
10143
10144 if (isym->st_shndx == SHN_UNDEF)
10145 isec = bfd_und_section_ptr;
c152c796
AM
10146 else if (isym->st_shndx == SHN_ABS)
10147 isec = bfd_abs_section_ptr;
10148 else if (isym->st_shndx == SHN_COMMON)
10149 isec = bfd_com_section_ptr;
10150 else
10151 {
cb33740c
AM
10152 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10153 if (isec == NULL)
10154 {
10155 /* Don't attempt to output symbols with st_shnx in the
10156 reserved range other than SHN_ABS and SHN_COMMON. */
10157 *ppsection = NULL;
10158 continue;
10159 }
dbaa2011 10160 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10161 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10162 isym->st_value =
10163 _bfd_merged_section_offset (output_bfd, &isec,
10164 elf_section_data (isec)->sec_info,
10165 isym->st_value);
c152c796
AM
10166 }
10167
10168 *ppsection = isec;
10169
d983c8c5
AM
10170 /* Don't output the first, undefined, symbol. In fact, don't
10171 output any undefined local symbol. */
10172 if (isec == bfd_und_section_ptr)
c152c796
AM
10173 continue;
10174
10175 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10176 {
10177 /* We never output section symbols. Instead, we use the
10178 section symbol of the corresponding section in the output
10179 file. */
10180 continue;
10181 }
10182
10183 /* If we are stripping all symbols, we don't want to output this
10184 one. */
8b127cbc 10185 if (flinfo->info->strip == strip_all)
c152c796
AM
10186 continue;
10187
10188 /* If we are discarding all local symbols, we don't want to
10189 output this one. If we are generating a relocatable output
10190 file, then some of the local symbols may be required by
10191 relocs; we output them below as we discover that they are
10192 needed. */
8b127cbc 10193 if (flinfo->info->discard == discard_all)
c152c796
AM
10194 continue;
10195
10196 /* If this symbol is defined in a section which we are
f02571c5
AM
10197 discarding, we don't need to keep it. */
10198 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10199 && isym->st_shndx < SHN_LORESERVE
10200 && bfd_section_removed_from_list (output_bfd,
10201 isec->output_section))
e75a280b
L
10202 continue;
10203
c152c796
AM
10204 /* Get the name of the symbol. */
10205 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10206 isym->st_name);
10207 if (name == NULL)
10208 return FALSE;
10209
10210 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10211 if ((flinfo->info->strip == strip_some
10212 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10213 == NULL))
8b127cbc 10214 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10215 && (isec->flags & SEC_MERGE)
10216 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10217 || flinfo->info->discard == discard_l)
c152c796
AM
10218 && bfd_is_local_label_name (input_bfd, name)))
10219 continue;
10220
ffbc01cc
AM
10221 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10222 {
ce875075
AM
10223 if (input_bfd->lto_output)
10224 /* -flto puts a temp file name here. This means builds
10225 are not reproducible. Discard the symbol. */
10226 continue;
ffbc01cc
AM
10227 have_file_sym = TRUE;
10228 flinfo->filesym_count += 1;
10229 }
10230 if (!have_file_sym)
10231 {
10232 /* In the absence of debug info, bfd_find_nearest_line uses
10233 FILE symbols to determine the source file for local
10234 function symbols. Provide a FILE symbol here if input
10235 files lack such, so that their symbols won't be
10236 associated with a previous input file. It's not the
10237 source file, but the best we can do. */
10238 have_file_sym = TRUE;
10239 flinfo->filesym_count += 1;
10240 memset (&osym, 0, sizeof (osym));
10241 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10242 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10243 if (!elf_link_output_symstrtab (flinfo,
10244 (input_bfd->lto_output ? NULL
10245 : input_bfd->filename),
10246 &osym, bfd_abs_section_ptr,
10247 NULL))
ffbc01cc
AM
10248 return FALSE;
10249 }
10250
c152c796
AM
10251 osym = *isym;
10252
10253 /* Adjust the section index for the output file. */
10254 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10255 isec->output_section);
10256 if (osym.st_shndx == SHN_BAD)
10257 return FALSE;
10258
c152c796
AM
10259 /* ELF symbols in relocatable files are section relative, but
10260 in executable files they are virtual addresses. Note that
10261 this code assumes that all ELF sections have an associated
10262 BFD section with a reasonable value for output_offset; below
10263 we assume that they also have a reasonable value for
10264 output_section. Any special sections must be set up to meet
10265 these requirements. */
10266 osym.st_value += isec->output_offset;
0e1862bb 10267 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10268 {
10269 osym.st_value += isec->output_section->vma;
10270 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10271 {
10272 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10273 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10274 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10275 }
10276 }
10277
6e0b88f1 10278 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10279 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10280 if (ret == 0)
c152c796 10281 return FALSE;
6e0b88f1
AM
10282 else if (ret == 1)
10283 *pindex = indx;
c152c796
AM
10284 }
10285
310fd250
L
10286 if (bed->s->arch_size == 32)
10287 {
10288 r_type_mask = 0xff;
10289 r_sym_shift = 8;
10290 address_size = 4;
10291 }
10292 else
10293 {
10294 r_type_mask = 0xffffffff;
10295 r_sym_shift = 32;
10296 address_size = 8;
10297 }
10298
c152c796
AM
10299 /* Relocate the contents of each section. */
10300 sym_hashes = elf_sym_hashes (input_bfd);
10301 for (o = input_bfd->sections; o != NULL; o = o->next)
10302 {
10303 bfd_byte *contents;
10304
10305 if (! o->linker_mark)
10306 {
10307 /* This section was omitted from the link. */
10308 continue;
10309 }
10310
7bdf4127 10311 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10312 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10313 {
10314 /* Deal with the group signature symbol. */
10315 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10316 unsigned long symndx = sec_data->this_hdr.sh_info;
10317 asection *osec = o->output_section;
10318
7bdf4127 10319 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10320 if (symndx >= locsymcount
10321 || (elf_bad_symtab (input_bfd)
8b127cbc 10322 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10323 {
10324 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10325 while (h->root.type == bfd_link_hash_indirect
10326 || h->root.type == bfd_link_hash_warning)
10327 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10328 /* Arrange for symbol to be output. */
10329 h->indx = -2;
10330 elf_section_data (osec)->this_hdr.sh_info = -2;
10331 }
10332 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10333 {
10334 /* We'll use the output section target_index. */
8b127cbc 10335 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10336 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10337 }
10338 else
10339 {
8b127cbc 10340 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10341 {
10342 /* Otherwise output the local symbol now. */
10343 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10344 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10345 const char *name;
6e0b88f1
AM
10346 long indx;
10347 int ret;
bcacc0f5
AM
10348
10349 name = bfd_elf_string_from_elf_section (input_bfd,
10350 symtab_hdr->sh_link,
10351 sym.st_name);
10352 if (name == NULL)
10353 return FALSE;
10354
10355 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10356 sec);
10357 if (sym.st_shndx == SHN_BAD)
10358 return FALSE;
10359
10360 sym.st_value += o->output_offset;
10361
6e0b88f1 10362 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10363 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10364 NULL);
6e0b88f1 10365 if (ret == 0)
bcacc0f5 10366 return FALSE;
6e0b88f1 10367 else if (ret == 1)
8b127cbc 10368 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10369 else
10370 abort ();
bcacc0f5
AM
10371 }
10372 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10373 = flinfo->indices[symndx];
bcacc0f5
AM
10374 }
10375 }
10376
c152c796 10377 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10378 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10379 continue;
10380
10381 if ((o->flags & SEC_LINKER_CREATED) != 0)
10382 {
10383 /* Section was created by _bfd_elf_link_create_dynamic_sections
10384 or somesuch. */
10385 continue;
10386 }
10387
10388 /* Get the contents of the section. They have been cached by a
10389 relaxation routine. Note that o is a section in an input
10390 file, so the contents field will not have been set by any of
10391 the routines which work on output files. */
10392 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10393 {
10394 contents = elf_section_data (o)->this_hdr.contents;
10395 if (bed->caches_rawsize
10396 && o->rawsize != 0
10397 && o->rawsize < o->size)
10398 {
10399 memcpy (flinfo->contents, contents, o->rawsize);
10400 contents = flinfo->contents;
10401 }
10402 }
c152c796
AM
10403 else
10404 {
8b127cbc 10405 contents = flinfo->contents;
4a114e3e 10406 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10407 return FALSE;
10408 }
10409
10410 if ((o->flags & SEC_RELOC) != 0)
10411 {
10412 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10413 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10414 int action_discarded;
ece5ef60 10415 int ret;
c152c796
AM
10416
10417 /* Get the swapped relocs. */
10418 internal_relocs
8b127cbc
AM
10419 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10420 flinfo->internal_relocs, FALSE);
c152c796
AM
10421 if (internal_relocs == NULL
10422 && o->reloc_count > 0)
10423 return FALSE;
10424
310fd250
L
10425 /* We need to reverse-copy input .ctors/.dtors sections if
10426 they are placed in .init_array/.finit_array for output. */
10427 if (o->size > address_size
10428 && ((strncmp (o->name, ".ctors", 6) == 0
10429 && strcmp (o->output_section->name,
10430 ".init_array") == 0)
10431 || (strncmp (o->name, ".dtors", 6) == 0
10432 && strcmp (o->output_section->name,
10433 ".fini_array") == 0))
10434 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10435 {
056bafd4
MR
10436 if (o->size * bed->s->int_rels_per_ext_rel
10437 != o->reloc_count * address_size)
310fd250 10438 {
4eca0228 10439 _bfd_error_handler
695344c0 10440 /* xgettext:c-format */
310fd250
L
10441 (_("error: %B: size of section %A is not "
10442 "multiple of address size"),
10443 input_bfd, o);
10444 bfd_set_error (bfd_error_on_input);
10445 return FALSE;
10446 }
10447 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10448 }
10449
0f02bbd9 10450 action_discarded = -1;
c152c796 10451 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10452 action_discarded = (*bed->action_discarded) (o);
10453
10454 /* Run through the relocs evaluating complex reloc symbols and
10455 looking for relocs against symbols from discarded sections
10456 or section symbols from removed link-once sections.
10457 Complain about relocs against discarded sections. Zero
10458 relocs against removed link-once sections. */
10459
10460 rel = internal_relocs;
056bafd4 10461 relend = rel + o->reloc_count;
0f02bbd9 10462 for ( ; rel < relend; rel++)
c152c796 10463 {
0f02bbd9
AM
10464 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10465 unsigned int s_type;
10466 asection **ps, *sec;
10467 struct elf_link_hash_entry *h = NULL;
10468 const char *sym_name;
c152c796 10469
0f02bbd9
AM
10470 if (r_symndx == STN_UNDEF)
10471 continue;
c152c796 10472
0f02bbd9
AM
10473 if (r_symndx >= locsymcount
10474 || (elf_bad_symtab (input_bfd)
8b127cbc 10475 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10476 {
10477 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10478
0f02bbd9
AM
10479 /* Badly formatted input files can contain relocs that
10480 reference non-existant symbols. Check here so that
10481 we do not seg fault. */
10482 if (h == NULL)
c152c796 10483 {
4eca0228 10484 _bfd_error_handler
695344c0 10485 /* xgettext:c-format */
76cfced5 10486 (_("error: %B contains a reloc (%#Lx) for section %A "
0f02bbd9 10487 "that references a non-existent global symbol"),
76cfced5 10488 input_bfd, rel->r_info, o);
0f02bbd9
AM
10489 bfd_set_error (bfd_error_bad_value);
10490 return FALSE;
10491 }
3b36f7e6 10492
0f02bbd9
AM
10493 while (h->root.type == bfd_link_hash_indirect
10494 || h->root.type == bfd_link_hash_warning)
10495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10496
0f02bbd9 10497 s_type = h->type;
cdd3575c 10498
9e2dec47 10499 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10500 mark the symbol as undefined. Note that the
10501 linker may attach linker created dynamic sections
10502 to the plugin bfd. Symbols defined in linker
10503 created sections are not plugin symbols. */
bc4e12de 10504 if ((h->root.non_ir_ref_regular
4070765b 10505 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10506 && (h->root.type == bfd_link_hash_defined
10507 || h->root.type == bfd_link_hash_defweak)
10508 && (h->root.u.def.section->flags
10509 & SEC_LINKER_CREATED) == 0
10510 && h->root.u.def.section->owner != NULL
10511 && (h->root.u.def.section->owner->flags
10512 & BFD_PLUGIN) != 0)
10513 {
10514 h->root.type = bfd_link_hash_undefined;
10515 h->root.u.undef.abfd = h->root.u.def.section->owner;
10516 }
10517
0f02bbd9
AM
10518 ps = NULL;
10519 if (h->root.type == bfd_link_hash_defined
10520 || h->root.type == bfd_link_hash_defweak)
10521 ps = &h->root.u.def.section;
10522
10523 sym_name = h->root.root.string;
10524 }
10525 else
10526 {
10527 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10528
10529 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10530 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10531 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10532 sym, *ps);
10533 }
c152c796 10534
c301e700 10535 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10536 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10537 {
10538 bfd_vma val;
10539 bfd_vma dot = (rel->r_offset
10540 + o->output_offset + o->output_section->vma);
10541#ifdef DEBUG
10542 printf ("Encountered a complex symbol!");
10543 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10544 input_bfd->filename, o->name,
10545 (long) (rel - internal_relocs));
0f02bbd9
AM
10546 printf (" symbol: idx %8.8lx, name %s\n",
10547 r_symndx, sym_name);
10548 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10549 (unsigned long) rel->r_info,
10550 (unsigned long) rel->r_offset);
10551#endif
8b127cbc 10552 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10553 isymbuf, locsymcount, s_type == STT_SRELC))
10554 return FALSE;
10555
10556 /* Symbol evaluated OK. Update to absolute value. */
10557 set_symbol_value (input_bfd, isymbuf, locsymcount,
10558 r_symndx, val);
10559 continue;
10560 }
10561
10562 if (action_discarded != -1 && ps != NULL)
10563 {
cdd3575c
AM
10564 /* Complain if the definition comes from a
10565 discarded section. */
dbaa2011 10566 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10567 {
cf35638d 10568 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10569 if (action_discarded & COMPLAIN)
8b127cbc 10570 (*flinfo->info->callbacks->einfo)
695344c0 10571 /* xgettext:c-format */
e1fffbe6 10572 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10573 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10574 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10575
87e5235d 10576 /* Try to do the best we can to support buggy old
e0ae6d6f 10577 versions of gcc. Pretend that the symbol is
87e5235d
AM
10578 really defined in the kept linkonce section.
10579 FIXME: This is quite broken. Modifying the
10580 symbol here means we will be changing all later
e0ae6d6f 10581 uses of the symbol, not just in this section. */
0f02bbd9 10582 if (action_discarded & PRETEND)
87e5235d 10583 {
01b3c8ab
L
10584 asection *kept;
10585
c0f00686 10586 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10587 flinfo->info);
01b3c8ab 10588 if (kept != NULL)
87e5235d
AM
10589 {
10590 *ps = kept;
10591 continue;
10592 }
10593 }
c152c796
AM
10594 }
10595 }
10596 }
10597
10598 /* Relocate the section by invoking a back end routine.
10599
10600 The back end routine is responsible for adjusting the
10601 section contents as necessary, and (if using Rela relocs
10602 and generating a relocatable output file) adjusting the
10603 reloc addend as necessary.
10604
10605 The back end routine does not have to worry about setting
10606 the reloc address or the reloc symbol index.
10607
10608 The back end routine is given a pointer to the swapped in
10609 internal symbols, and can access the hash table entries
10610 for the external symbols via elf_sym_hashes (input_bfd).
10611
10612 When generating relocatable output, the back end routine
10613 must handle STB_LOCAL/STT_SECTION symbols specially. The
10614 output symbol is going to be a section symbol
10615 corresponding to the output section, which will require
10616 the addend to be adjusted. */
10617
8b127cbc 10618 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10619 input_bfd, o, contents,
10620 internal_relocs,
10621 isymbuf,
8b127cbc 10622 flinfo->sections);
ece5ef60 10623 if (!ret)
c152c796
AM
10624 return FALSE;
10625
ece5ef60 10626 if (ret == 2
0e1862bb 10627 || bfd_link_relocatable (flinfo->info)
8b127cbc 10628 || flinfo->info->emitrelocations)
c152c796
AM
10629 {
10630 Elf_Internal_Rela *irela;
d4730f92 10631 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10632 bfd_vma last_offset;
10633 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10634 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10635 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10636 unsigned int next_erel;
c152c796 10637 bfd_boolean rela_normal;
d4730f92 10638 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10639
d4730f92
BS
10640 esdi = elf_section_data (o);
10641 esdo = elf_section_data (o->output_section);
10642 rela_normal = FALSE;
c152c796
AM
10643
10644 /* Adjust the reloc addresses and symbol indices. */
10645
10646 irela = internal_relocs;
056bafd4 10647 irelaend = irela + o->reloc_count;
d4730f92
BS
10648 rel_hash = esdo->rel.hashes + esdo->rel.count;
10649 /* We start processing the REL relocs, if any. When we reach
10650 IRELAMID in the loop, we switch to the RELA relocs. */
10651 irelamid = irela;
10652 if (esdi->rel.hdr != NULL)
10653 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10654 * bed->s->int_rels_per_ext_rel);
eac338cf 10655 rel_hash_list = rel_hash;
d4730f92 10656 rela_hash_list = NULL;
c152c796 10657 last_offset = o->output_offset;
0e1862bb 10658 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10659 last_offset += o->output_section->vma;
10660 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10661 {
10662 unsigned long r_symndx;
10663 asection *sec;
10664 Elf_Internal_Sym sym;
10665
10666 if (next_erel == bed->s->int_rels_per_ext_rel)
10667 {
10668 rel_hash++;
10669 next_erel = 0;
10670 }
10671
d4730f92
BS
10672 if (irela == irelamid)
10673 {
10674 rel_hash = esdo->rela.hashes + esdo->rela.count;
10675 rela_hash_list = rel_hash;
10676 rela_normal = bed->rela_normal;
10677 }
10678
c152c796 10679 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10680 flinfo->info, o,
c152c796
AM
10681 irela->r_offset);
10682 if (irela->r_offset >= (bfd_vma) -2)
10683 {
10684 /* This is a reloc for a deleted entry or somesuch.
10685 Turn it into an R_*_NONE reloc, at the same
10686 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10687 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10688 being ordered. */
10689 irela->r_offset = last_offset;
10690 irela->r_info = 0;
10691 irela->r_addend = 0;
10692 continue;
10693 }
10694
10695 irela->r_offset += o->output_offset;
10696
10697 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10698 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10699 irela->r_offset += o->output_section->vma;
10700
10701 last_offset = irela->r_offset;
10702
10703 r_symndx = irela->r_info >> r_sym_shift;
10704 if (r_symndx == STN_UNDEF)
10705 continue;
10706
10707 if (r_symndx >= locsymcount
10708 || (elf_bad_symtab (input_bfd)
8b127cbc 10709 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10710 {
10711 struct elf_link_hash_entry *rh;
10712 unsigned long indx;
10713
10714 /* This is a reloc against a global symbol. We
10715 have not yet output all the local symbols, so
10716 we do not know the symbol index of any global
10717 symbol. We set the rel_hash entry for this
10718 reloc to point to the global hash table entry
10719 for this symbol. The symbol index is then
ee75fd95 10720 set at the end of bfd_elf_final_link. */
c152c796
AM
10721 indx = r_symndx - extsymoff;
10722 rh = elf_sym_hashes (input_bfd)[indx];
10723 while (rh->root.type == bfd_link_hash_indirect
10724 || rh->root.type == bfd_link_hash_warning)
10725 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10726
10727 /* Setting the index to -2 tells
10728 elf_link_output_extsym that this symbol is
10729 used by a reloc. */
10730 BFD_ASSERT (rh->indx < 0);
10731 rh->indx = -2;
c152c796
AM
10732 *rel_hash = rh;
10733
10734 continue;
10735 }
10736
10737 /* This is a reloc against a local symbol. */
10738
10739 *rel_hash = NULL;
10740 sym = isymbuf[r_symndx];
8b127cbc 10741 sec = flinfo->sections[r_symndx];
c152c796
AM
10742 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10743 {
10744 /* I suppose the backend ought to fill in the
10745 section of any STT_SECTION symbol against a
6a8d1586 10746 processor specific section. */
cf35638d 10747 r_symndx = STN_UNDEF;
6a8d1586
AM
10748 if (bfd_is_abs_section (sec))
10749 ;
c152c796
AM
10750 else if (sec == NULL || sec->owner == NULL)
10751 {
10752 bfd_set_error (bfd_error_bad_value);
10753 return FALSE;
10754 }
10755 else
10756 {
6a8d1586
AM
10757 asection *osec = sec->output_section;
10758
10759 /* If we have discarded a section, the output
10760 section will be the absolute section. In
ab96bf03
AM
10761 case of discarded SEC_MERGE sections, use
10762 the kept section. relocate_section should
10763 have already handled discarded linkonce
10764 sections. */
6a8d1586
AM
10765 if (bfd_is_abs_section (osec)
10766 && sec->kept_section != NULL
10767 && sec->kept_section->output_section != NULL)
10768 {
10769 osec = sec->kept_section->output_section;
10770 irela->r_addend -= osec->vma;
10771 }
10772
10773 if (!bfd_is_abs_section (osec))
10774 {
10775 r_symndx = osec->target_index;
cf35638d 10776 if (r_symndx == STN_UNDEF)
74541ad4 10777 {
051d833a
AM
10778 irela->r_addend += osec->vma;
10779 osec = _bfd_nearby_section (output_bfd, osec,
10780 osec->vma);
10781 irela->r_addend -= osec->vma;
10782 r_symndx = osec->target_index;
74541ad4 10783 }
6a8d1586 10784 }
c152c796
AM
10785 }
10786
10787 /* Adjust the addend according to where the
10788 section winds up in the output section. */
10789 if (rela_normal)
10790 irela->r_addend += sec->output_offset;
10791 }
10792 else
10793 {
8b127cbc 10794 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10795 {
10796 unsigned long shlink;
10797 const char *name;
10798 asection *osec;
6e0b88f1 10799 long indx;
c152c796 10800
8b127cbc 10801 if (flinfo->info->strip == strip_all)
c152c796
AM
10802 {
10803 /* You can't do ld -r -s. */
10804 bfd_set_error (bfd_error_invalid_operation);
10805 return FALSE;
10806 }
10807
10808 /* This symbol was skipped earlier, but
10809 since it is needed by a reloc, we
10810 must output it now. */
10811 shlink = symtab_hdr->sh_link;
10812 name = (bfd_elf_string_from_elf_section
10813 (input_bfd, shlink, sym.st_name));
10814 if (name == NULL)
10815 return FALSE;
10816
10817 osec = sec->output_section;
10818 sym.st_shndx =
10819 _bfd_elf_section_from_bfd_section (output_bfd,
10820 osec);
10821 if (sym.st_shndx == SHN_BAD)
10822 return FALSE;
10823
10824 sym.st_value += sec->output_offset;
0e1862bb 10825 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10826 {
10827 sym.st_value += osec->vma;
10828 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10829 {
10830 /* STT_TLS symbols are relative to PT_TLS
10831 segment base. */
8b127cbc 10832 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10833 ->tls_sec != NULL);
8b127cbc 10834 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10835 ->tls_sec->vma);
10836 }
10837 }
10838
6e0b88f1 10839 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10840 ret = elf_link_output_symstrtab (flinfo, name,
10841 &sym, sec,
10842 NULL);
6e0b88f1 10843 if (ret == 0)
c152c796 10844 return FALSE;
6e0b88f1 10845 else if (ret == 1)
8b127cbc 10846 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10847 else
10848 abort ();
c152c796
AM
10849 }
10850
8b127cbc 10851 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10852 }
10853
10854 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10855 | (irela->r_info & r_type_mask));
10856 }
10857
10858 /* Swap out the relocs. */
d4730f92
BS
10859 input_rel_hdr = esdi->rel.hdr;
10860 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10861 {
d4730f92
BS
10862 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10863 input_rel_hdr,
10864 internal_relocs,
10865 rel_hash_list))
10866 return FALSE;
c152c796
AM
10867 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10868 * bed->s->int_rels_per_ext_rel);
eac338cf 10869 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10870 }
10871
10872 input_rela_hdr = esdi->rela.hdr;
10873 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10874 {
eac338cf 10875 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10876 input_rela_hdr,
eac338cf 10877 internal_relocs,
d4730f92 10878 rela_hash_list))
c152c796
AM
10879 return FALSE;
10880 }
10881 }
10882 }
10883
10884 /* Write out the modified section contents. */
10885 if (bed->elf_backend_write_section
8b127cbc 10886 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10887 contents))
c152c796
AM
10888 {
10889 /* Section written out. */
10890 }
10891 else switch (o->sec_info_type)
10892 {
dbaa2011 10893 case SEC_INFO_TYPE_STABS:
c152c796
AM
10894 if (! (_bfd_write_section_stabs
10895 (output_bfd,
8b127cbc 10896 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10897 o, &elf_section_data (o)->sec_info, contents)))
10898 return FALSE;
10899 break;
dbaa2011 10900 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10901 if (! _bfd_write_merged_section (output_bfd, o,
10902 elf_section_data (o)->sec_info))
10903 return FALSE;
10904 break;
dbaa2011 10905 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10906 {
8b127cbc 10907 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10908 o, contents))
10909 return FALSE;
10910 }
10911 break;
2f0c68f2
CM
10912 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10913 {
10914 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10915 flinfo->info,
10916 o, contents))
10917 return FALSE;
10918 }
10919 break;
c152c796
AM
10920 default:
10921 {
310fd250
L
10922 if (! (o->flags & SEC_EXCLUDE))
10923 {
10924 file_ptr offset = (file_ptr) o->output_offset;
10925 bfd_size_type todo = o->size;
37b01f6a
DG
10926
10927 offset *= bfd_octets_per_byte (output_bfd);
10928
310fd250
L
10929 if ((o->flags & SEC_ELF_REVERSE_COPY))
10930 {
10931 /* Reverse-copy input section to output. */
10932 do
10933 {
10934 todo -= address_size;
10935 if (! bfd_set_section_contents (output_bfd,
10936 o->output_section,
10937 contents + todo,
10938 offset,
10939 address_size))
10940 return FALSE;
10941 if (todo == 0)
10942 break;
10943 offset += address_size;
10944 }
10945 while (1);
10946 }
10947 else if (! bfd_set_section_contents (output_bfd,
10948 o->output_section,
10949 contents,
10950 offset, todo))
10951 return FALSE;
10952 }
c152c796
AM
10953 }
10954 break;
10955 }
10956 }
10957
10958 return TRUE;
10959}
10960
10961/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10962 requested by the linker, and does not come from any input file. This
c152c796
AM
10963 is used to build constructor and destructor tables when linking
10964 with -Ur. */
10965
10966static bfd_boolean
10967elf_reloc_link_order (bfd *output_bfd,
10968 struct bfd_link_info *info,
10969 asection *output_section,
10970 struct bfd_link_order *link_order)
10971{
10972 reloc_howto_type *howto;
10973 long indx;
10974 bfd_vma offset;
10975 bfd_vma addend;
d4730f92 10976 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10977 struct elf_link_hash_entry **rel_hash_ptr;
10978 Elf_Internal_Shdr *rel_hdr;
10979 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10980 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10981 bfd_byte *erel;
10982 unsigned int i;
d4730f92 10983 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10984
10985 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10986 if (howto == NULL)
10987 {
10988 bfd_set_error (bfd_error_bad_value);
10989 return FALSE;
10990 }
10991
10992 addend = link_order->u.reloc.p->addend;
10993
d4730f92
BS
10994 if (esdo->rel.hdr)
10995 reldata = &esdo->rel;
10996 else if (esdo->rela.hdr)
10997 reldata = &esdo->rela;
10998 else
10999 {
11000 reldata = NULL;
11001 BFD_ASSERT (0);
11002 }
11003
c152c796 11004 /* Figure out the symbol index. */
d4730f92 11005 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11006 if (link_order->type == bfd_section_reloc_link_order)
11007 {
11008 indx = link_order->u.reloc.p->u.section->target_index;
11009 BFD_ASSERT (indx != 0);
11010 *rel_hash_ptr = NULL;
11011 }
11012 else
11013 {
11014 struct elf_link_hash_entry *h;
11015
11016 /* Treat a reloc against a defined symbol as though it were
11017 actually against the section. */
11018 h = ((struct elf_link_hash_entry *)
11019 bfd_wrapped_link_hash_lookup (output_bfd, info,
11020 link_order->u.reloc.p->u.name,
11021 FALSE, FALSE, TRUE));
11022 if (h != NULL
11023 && (h->root.type == bfd_link_hash_defined
11024 || h->root.type == bfd_link_hash_defweak))
11025 {
11026 asection *section;
11027
11028 section = h->root.u.def.section;
11029 indx = section->output_section->target_index;
11030 *rel_hash_ptr = NULL;
11031 /* It seems that we ought to add the symbol value to the
11032 addend here, but in practice it has already been added
11033 because it was passed to constructor_callback. */
11034 addend += section->output_section->vma + section->output_offset;
11035 }
11036 else if (h != NULL)
11037 {
11038 /* Setting the index to -2 tells elf_link_output_extsym that
11039 this symbol is used by a reloc. */
11040 h->indx = -2;
11041 *rel_hash_ptr = h;
11042 indx = 0;
11043 }
11044 else
11045 {
1a72702b
AM
11046 (*info->callbacks->unattached_reloc)
11047 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11048 indx = 0;
11049 }
11050 }
11051
11052 /* If this is an inplace reloc, we must write the addend into the
11053 object file. */
11054 if (howto->partial_inplace && addend != 0)
11055 {
11056 bfd_size_type size;
11057 bfd_reloc_status_type rstat;
11058 bfd_byte *buf;
11059 bfd_boolean ok;
11060 const char *sym_name;
11061
a50b1753
NC
11062 size = (bfd_size_type) bfd_get_reloc_size (howto);
11063 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11064 if (buf == NULL && size != 0)
c152c796
AM
11065 return FALSE;
11066 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11067 switch (rstat)
11068 {
11069 case bfd_reloc_ok:
11070 break;
11071
11072 default:
11073 case bfd_reloc_outofrange:
11074 abort ();
11075
11076 case bfd_reloc_overflow:
11077 if (link_order->type == bfd_section_reloc_link_order)
11078 sym_name = bfd_section_name (output_bfd,
11079 link_order->u.reloc.p->u.section);
11080 else
11081 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11082 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11083 howto->name, addend, NULL, NULL,
11084 (bfd_vma) 0);
c152c796
AM
11085 break;
11086 }
37b01f6a 11087
c152c796 11088 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11089 link_order->offset
11090 * bfd_octets_per_byte (output_bfd),
11091 size);
c152c796
AM
11092 free (buf);
11093 if (! ok)
11094 return FALSE;
11095 }
11096
11097 /* The address of a reloc is relative to the section in a
11098 relocatable file, and is a virtual address in an executable
11099 file. */
11100 offset = link_order->offset;
0e1862bb 11101 if (! bfd_link_relocatable (info))
c152c796
AM
11102 offset += output_section->vma;
11103
11104 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11105 {
11106 irel[i].r_offset = offset;
11107 irel[i].r_info = 0;
11108 irel[i].r_addend = 0;
11109 }
11110 if (bed->s->arch_size == 32)
11111 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11112 else
11113 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11114
d4730f92 11115 rel_hdr = reldata->hdr;
c152c796
AM
11116 erel = rel_hdr->contents;
11117 if (rel_hdr->sh_type == SHT_REL)
11118 {
d4730f92 11119 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11120 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11121 }
11122 else
11123 {
11124 irel[0].r_addend = addend;
d4730f92 11125 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11126 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11127 }
11128
d4730f92 11129 ++reldata->count;
c152c796
AM
11130
11131 return TRUE;
11132}
11133
0b52efa6
PB
11134
11135/* Get the output vma of the section pointed to by the sh_link field. */
11136
11137static bfd_vma
11138elf_get_linked_section_vma (struct bfd_link_order *p)
11139{
11140 Elf_Internal_Shdr **elf_shdrp;
11141 asection *s;
11142 int elfsec;
11143
11144 s = p->u.indirect.section;
11145 elf_shdrp = elf_elfsections (s->owner);
11146 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11147 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11148 /* PR 290:
11149 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11150 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11151 sh_info fields. Hence we could get the situation
11152 where elfsec is 0. */
11153 if (elfsec == 0)
11154 {
11155 const struct elf_backend_data *bed
11156 = get_elf_backend_data (s->owner);
11157 if (bed->link_order_error_handler)
d003868e 11158 bed->link_order_error_handler
695344c0 11159 /* xgettext:c-format */
d003868e 11160 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11161 return 0;
11162 }
11163 else
11164 {
11165 s = elf_shdrp[elfsec]->bfd_section;
11166 return s->output_section->vma + s->output_offset;
11167 }
0b52efa6
PB
11168}
11169
11170
11171/* Compare two sections based on the locations of the sections they are
11172 linked to. Used by elf_fixup_link_order. */
11173
11174static int
11175compare_link_order (const void * a, const void * b)
11176{
11177 bfd_vma apos;
11178 bfd_vma bpos;
11179
11180 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11181 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11182 if (apos < bpos)
11183 return -1;
11184 return apos > bpos;
11185}
11186
11187
11188/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11189 order as their linked sections. Returns false if this could not be done
11190 because an output section includes both ordered and unordered
11191 sections. Ideally we'd do this in the linker proper. */
11192
11193static bfd_boolean
11194elf_fixup_link_order (bfd *abfd, asection *o)
11195{
11196 int seen_linkorder;
11197 int seen_other;
11198 int n;
11199 struct bfd_link_order *p;
11200 bfd *sub;
11201 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11202 unsigned elfsec;
0b52efa6 11203 struct bfd_link_order **sections;
d33cdfe3 11204 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11205 bfd_vma offset;
3b36f7e6 11206
d33cdfe3
L
11207 other_sec = NULL;
11208 linkorder_sec = NULL;
0b52efa6
PB
11209 seen_other = 0;
11210 seen_linkorder = 0;
8423293d 11211 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11212 {
d33cdfe3 11213 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11214 {
11215 s = p->u.indirect.section;
d33cdfe3
L
11216 sub = s->owner;
11217 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11218 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11219 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11220 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11221 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11222 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11223 {
11224 seen_linkorder++;
11225 linkorder_sec = s;
11226 }
0b52efa6 11227 else
d33cdfe3
L
11228 {
11229 seen_other++;
11230 other_sec = s;
11231 }
0b52efa6
PB
11232 }
11233 else
11234 seen_other++;
d33cdfe3
L
11235
11236 if (seen_other && seen_linkorder)
11237 {
11238 if (other_sec && linkorder_sec)
4eca0228 11239 _bfd_error_handler
695344c0 11240 /* xgettext:c-format */
4eca0228
AM
11241 (_("%A has both ordered [`%A' in %B] "
11242 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11243 o, linkorder_sec, linkorder_sec->owner,
11244 other_sec, other_sec->owner);
d33cdfe3 11245 else
4eca0228
AM
11246 _bfd_error_handler
11247 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11248 bfd_set_error (bfd_error_bad_value);
11249 return FALSE;
11250 }
0b52efa6
PB
11251 }
11252
11253 if (!seen_linkorder)
11254 return TRUE;
11255
0b52efa6 11256 sections = (struct bfd_link_order **)
14b1c01e
AM
11257 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11258 if (sections == NULL)
11259 return FALSE;
0b52efa6 11260 seen_linkorder = 0;
3b36f7e6 11261
8423293d 11262 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11263 {
11264 sections[seen_linkorder++] = p;
11265 }
11266 /* Sort the input sections in the order of their linked section. */
11267 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11268 compare_link_order);
11269
11270 /* Change the offsets of the sections. */
11271 offset = 0;
11272 for (n = 0; n < seen_linkorder; n++)
11273 {
11274 s = sections[n]->u.indirect.section;
461686a3 11275 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11276 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11277 sections[n]->offset = offset;
11278 offset += sections[n]->size;
11279 }
11280
4dd07732 11281 free (sections);
0b52efa6
PB
11282 return TRUE;
11283}
11284
76359541
TP
11285/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11286 Returns TRUE upon success, FALSE otherwise. */
11287
11288static bfd_boolean
11289elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11290{
11291 bfd_boolean ret = FALSE;
11292 bfd *implib_bfd;
11293 const struct elf_backend_data *bed;
11294 flagword flags;
11295 enum bfd_architecture arch;
11296 unsigned int mach;
11297 asymbol **sympp = NULL;
11298 long symsize;
11299 long symcount;
11300 long src_count;
11301 elf_symbol_type *osymbuf;
11302
11303 implib_bfd = info->out_implib_bfd;
11304 bed = get_elf_backend_data (abfd);
11305
11306 if (!bfd_set_format (implib_bfd, bfd_object))
11307 return FALSE;
11308
046734ff 11309 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11310 flags = bfd_get_file_flags (abfd);
11311 flags &= ~HAS_RELOC;
11312 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11313 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11314 return FALSE;
11315
11316 /* Copy architecture of output file to import library file. */
11317 arch = bfd_get_arch (abfd);
11318 mach = bfd_get_mach (abfd);
11319 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11320 && (abfd->target_defaulted
11321 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11322 return FALSE;
11323
11324 /* Get symbol table size. */
11325 symsize = bfd_get_symtab_upper_bound (abfd);
11326 if (symsize < 0)
11327 return FALSE;
11328
11329 /* Read in the symbol table. */
11330 sympp = (asymbol **) xmalloc (symsize);
11331 symcount = bfd_canonicalize_symtab (abfd, sympp);
11332 if (symcount < 0)
11333 goto free_sym_buf;
11334
11335 /* Allow the BFD backend to copy any private header data it
11336 understands from the output BFD to the import library BFD. */
11337 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11338 goto free_sym_buf;
11339
11340 /* Filter symbols to appear in the import library. */
11341 if (bed->elf_backend_filter_implib_symbols)
11342 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11343 symcount);
11344 else
11345 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11346 if (symcount == 0)
11347 {
5df1bc57 11348 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11349 _bfd_error_handler (_("%B: no symbol found for import library"),
11350 implib_bfd);
76359541
TP
11351 goto free_sym_buf;
11352 }
11353
11354
11355 /* Make symbols absolute. */
11356 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11357 sizeof (*osymbuf));
11358 for (src_count = 0; src_count < symcount; src_count++)
11359 {
11360 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11361 sizeof (*osymbuf));
11362 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11363 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11364 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11365 osymbuf[src_count].internal_elf_sym.st_value =
11366 osymbuf[src_count].symbol.value;
11367 sympp[src_count] = &osymbuf[src_count].symbol;
11368 }
11369
11370 bfd_set_symtab (implib_bfd, sympp, symcount);
11371
11372 /* Allow the BFD backend to copy any private data it understands
11373 from the output BFD to the import library BFD. This is done last
11374 to permit the routine to look at the filtered symbol table. */
11375 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11376 goto free_sym_buf;
11377
11378 if (!bfd_close (implib_bfd))
11379 goto free_sym_buf;
11380
11381 ret = TRUE;
11382
11383free_sym_buf:
11384 free (sympp);
11385 return ret;
11386}
11387
9f7c3e5e
AM
11388static void
11389elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11390{
11391 asection *o;
11392
11393 if (flinfo->symstrtab != NULL)
ef10c3ac 11394 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11395 if (flinfo->contents != NULL)
11396 free (flinfo->contents);
11397 if (flinfo->external_relocs != NULL)
11398 free (flinfo->external_relocs);
11399 if (flinfo->internal_relocs != NULL)
11400 free (flinfo->internal_relocs);
11401 if (flinfo->external_syms != NULL)
11402 free (flinfo->external_syms);
11403 if (flinfo->locsym_shndx != NULL)
11404 free (flinfo->locsym_shndx);
11405 if (flinfo->internal_syms != NULL)
11406 free (flinfo->internal_syms);
11407 if (flinfo->indices != NULL)
11408 free (flinfo->indices);
11409 if (flinfo->sections != NULL)
11410 free (flinfo->sections);
9f7c3e5e
AM
11411 if (flinfo->symshndxbuf != NULL)
11412 free (flinfo->symshndxbuf);
11413 for (o = obfd->sections; o != NULL; o = o->next)
11414 {
11415 struct bfd_elf_section_data *esdo = elf_section_data (o);
11416 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11417 free (esdo->rel.hashes);
11418 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11419 free (esdo->rela.hashes);
11420 }
11421}
0b52efa6 11422
c152c796
AM
11423/* Do the final step of an ELF link. */
11424
11425bfd_boolean
11426bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11427{
11428 bfd_boolean dynamic;
11429 bfd_boolean emit_relocs;
11430 bfd *dynobj;
8b127cbc 11431 struct elf_final_link_info flinfo;
91d6fa6a
NC
11432 asection *o;
11433 struct bfd_link_order *p;
11434 bfd *sub;
c152c796
AM
11435 bfd_size_type max_contents_size;
11436 bfd_size_type max_external_reloc_size;
11437 bfd_size_type max_internal_reloc_count;
11438 bfd_size_type max_sym_count;
11439 bfd_size_type max_sym_shndx_count;
c152c796
AM
11440 Elf_Internal_Sym elfsym;
11441 unsigned int i;
11442 Elf_Internal_Shdr *symtab_hdr;
11443 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11444 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11445 struct elf_outext_info eoinfo;
11446 bfd_boolean merged;
11447 size_t relativecount = 0;
11448 asection *reldyn = 0;
11449 bfd_size_type amt;
104d59d1
JM
11450 asection *attr_section = NULL;
11451 bfd_vma attr_size = 0;
11452 const char *std_attrs_section;
64f52338 11453 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11454
64f52338 11455 if (!is_elf_hash_table (htab))
c152c796
AM
11456 return FALSE;
11457
0e1862bb 11458 if (bfd_link_pic (info))
c152c796
AM
11459 abfd->flags |= DYNAMIC;
11460
64f52338
AM
11461 dynamic = htab->dynamic_sections_created;
11462 dynobj = htab->dynobj;
c152c796 11463
0e1862bb 11464 emit_relocs = (bfd_link_relocatable (info)
a4676736 11465 || info->emitrelocations);
c152c796 11466
8b127cbc
AM
11467 flinfo.info = info;
11468 flinfo.output_bfd = abfd;
ef10c3ac 11469 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11470 if (flinfo.symstrtab == NULL)
c152c796
AM
11471 return FALSE;
11472
11473 if (! dynamic)
11474 {
8b127cbc
AM
11475 flinfo.hash_sec = NULL;
11476 flinfo.symver_sec = NULL;
c152c796
AM
11477 }
11478 else
11479 {
3d4d4302 11480 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11481 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11482 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11483 /* Note that it is OK if symver_sec is NULL. */
11484 }
11485
8b127cbc
AM
11486 flinfo.contents = NULL;
11487 flinfo.external_relocs = NULL;
11488 flinfo.internal_relocs = NULL;
11489 flinfo.external_syms = NULL;
11490 flinfo.locsym_shndx = NULL;
11491 flinfo.internal_syms = NULL;
11492 flinfo.indices = NULL;
11493 flinfo.sections = NULL;
8b127cbc 11494 flinfo.symshndxbuf = NULL;
ffbc01cc 11495 flinfo.filesym_count = 0;
c152c796 11496
104d59d1
JM
11497 /* The object attributes have been merged. Remove the input
11498 sections from the link, and set the contents of the output
11499 secton. */
11500 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11501 for (o = abfd->sections; o != NULL; o = o->next)
11502 {
11503 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11504 || strcmp (o->name, ".gnu.attributes") == 0)
11505 {
11506 for (p = o->map_head.link_order; p != NULL; p = p->next)
11507 {
11508 asection *input_section;
11509
11510 if (p->type != bfd_indirect_link_order)
11511 continue;
11512 input_section = p->u.indirect.section;
11513 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11514 elf_link_input_bfd ignores this section. */
11515 input_section->flags &= ~SEC_HAS_CONTENTS;
11516 }
a0c8462f 11517
104d59d1
JM
11518 attr_size = bfd_elf_obj_attr_size (abfd);
11519 if (attr_size)
11520 {
11521 bfd_set_section_size (abfd, o, attr_size);
11522 attr_section = o;
11523 /* Skip this section later on. */
11524 o->map_head.link_order = NULL;
11525 }
11526 else
11527 o->flags |= SEC_EXCLUDE;
11528 }
11529 }
11530
c152c796
AM
11531 /* Count up the number of relocations we will output for each output
11532 section, so that we know the sizes of the reloc sections. We
11533 also figure out some maximum sizes. */
11534 max_contents_size = 0;
11535 max_external_reloc_size = 0;
11536 max_internal_reloc_count = 0;
11537 max_sym_count = 0;
11538 max_sym_shndx_count = 0;
11539 merged = FALSE;
11540 for (o = abfd->sections; o != NULL; o = o->next)
11541 {
11542 struct bfd_elf_section_data *esdo = elf_section_data (o);
11543 o->reloc_count = 0;
11544
8423293d 11545 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11546 {
11547 unsigned int reloc_count = 0;
9eaff861 11548 unsigned int additional_reloc_count = 0;
c152c796 11549 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11550
11551 if (p->type == bfd_section_reloc_link_order
11552 || p->type == bfd_symbol_reloc_link_order)
11553 reloc_count = 1;
11554 else if (p->type == bfd_indirect_link_order)
11555 {
11556 asection *sec;
11557
11558 sec = p->u.indirect.section;
c152c796
AM
11559
11560 /* Mark all sections which are to be included in the
11561 link. This will normally be every section. We need
11562 to do this so that we can identify any sections which
11563 the linker has decided to not include. */
11564 sec->linker_mark = TRUE;
11565
11566 if (sec->flags & SEC_MERGE)
11567 merged = TRUE;
11568
eea6121a
AM
11569 if (sec->rawsize > max_contents_size)
11570 max_contents_size = sec->rawsize;
11571 if (sec->size > max_contents_size)
11572 max_contents_size = sec->size;
c152c796 11573
c152c796
AM
11574 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11575 && (sec->owner->flags & DYNAMIC) == 0)
11576 {
11577 size_t sym_count;
11578
a961cdd5
AM
11579 /* We are interested in just local symbols, not all
11580 symbols. */
c152c796
AM
11581 if (elf_bad_symtab (sec->owner))
11582 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11583 / bed->s->sizeof_sym);
11584 else
11585 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11586
11587 if (sym_count > max_sym_count)
11588 max_sym_count = sym_count;
11589
11590 if (sym_count > max_sym_shndx_count
6a40cf0c 11591 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11592 max_sym_shndx_count = sym_count;
11593
a961cdd5
AM
11594 if (esdo->this_hdr.sh_type == SHT_REL
11595 || esdo->this_hdr.sh_type == SHT_RELA)
11596 /* Some backends use reloc_count in relocation sections
11597 to count particular types of relocs. Of course,
11598 reloc sections themselves can't have relocations. */
11599 ;
11600 else if (emit_relocs)
11601 {
11602 reloc_count = sec->reloc_count;
11603 if (bed->elf_backend_count_additional_relocs)
11604 {
11605 int c;
11606 c = (*bed->elf_backend_count_additional_relocs) (sec);
11607 additional_reloc_count += c;
11608 }
11609 }
11610 else if (bed->elf_backend_count_relocs)
11611 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11612
11613 esdi = elf_section_data (sec);
11614
c152c796
AM
11615 if ((sec->flags & SEC_RELOC) != 0)
11616 {
d4730f92 11617 size_t ext_size = 0;
c152c796 11618
d4730f92
BS
11619 if (esdi->rel.hdr != NULL)
11620 ext_size = esdi->rel.hdr->sh_size;
11621 if (esdi->rela.hdr != NULL)
11622 ext_size += esdi->rela.hdr->sh_size;
7326c758 11623
c152c796
AM
11624 if (ext_size > max_external_reloc_size)
11625 max_external_reloc_size = ext_size;
11626 if (sec->reloc_count > max_internal_reloc_count)
11627 max_internal_reloc_count = sec->reloc_count;
11628 }
11629 }
11630 }
11631
11632 if (reloc_count == 0)
11633 continue;
11634
9eaff861 11635 reloc_count += additional_reloc_count;
c152c796
AM
11636 o->reloc_count += reloc_count;
11637
0e1862bb 11638 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11639 {
d4730f92 11640 if (esdi->rel.hdr)
9eaff861 11641 {
491d01d3 11642 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11643 esdo->rel.count += additional_reloc_count;
11644 }
d4730f92 11645 if (esdi->rela.hdr)
9eaff861 11646 {
491d01d3 11647 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11648 esdo->rela.count += additional_reloc_count;
11649 }
d4730f92
BS
11650 }
11651 else
11652 {
11653 if (o->use_rela_p)
11654 esdo->rela.count += reloc_count;
2c2b4ed4 11655 else
d4730f92 11656 esdo->rel.count += reloc_count;
c152c796 11657 }
c152c796
AM
11658 }
11659
9eaff861 11660 if (o->reloc_count > 0)
c152c796
AM
11661 o->flags |= SEC_RELOC;
11662 else
11663 {
11664 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11665 set it (this is probably a bug) and if it is set
11666 assign_section_numbers will create a reloc section. */
11667 o->flags &=~ SEC_RELOC;
11668 }
11669
11670 /* If the SEC_ALLOC flag is not set, force the section VMA to
11671 zero. This is done in elf_fake_sections as well, but forcing
11672 the VMA to 0 here will ensure that relocs against these
11673 sections are handled correctly. */
11674 if ((o->flags & SEC_ALLOC) == 0
11675 && ! o->user_set_vma)
11676 o->vma = 0;
11677 }
11678
0e1862bb 11679 if (! bfd_link_relocatable (info) && merged)
64f52338 11680 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11681
11682 /* Figure out the file positions for everything but the symbol table
11683 and the relocs. We set symcount to force assign_section_numbers
11684 to create a symbol table. */
8539e4e8 11685 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11686 BFD_ASSERT (! abfd->output_has_begun);
11687 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11688 goto error_return;
11689
ee75fd95 11690 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11691 for (o = abfd->sections; o != NULL; o = o->next)
11692 {
d4730f92 11693 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11694 if ((o->flags & SEC_RELOC) != 0)
11695 {
d4730f92 11696 if (esdo->rel.hdr
9eaff861 11697 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11698 goto error_return;
11699
d4730f92 11700 if (esdo->rela.hdr
9eaff861 11701 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11702 goto error_return;
11703 }
11704
11705 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11706 to count upwards while actually outputting the relocations. */
d4730f92
BS
11707 esdo->rel.count = 0;
11708 esdo->rela.count = 0;
0ce398f1
L
11709
11710 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11711 {
11712 /* Cache the section contents so that they can be compressed
11713 later. Use bfd_malloc since it will be freed by
11714 bfd_compress_section_contents. */
11715 unsigned char *contents = esdo->this_hdr.contents;
11716 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11717 abort ();
11718 contents
11719 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11720 if (contents == NULL)
11721 goto error_return;
11722 esdo->this_hdr.contents = contents;
11723 }
c152c796
AM
11724 }
11725
c152c796 11726 /* We have now assigned file positions for all the sections except
a485e98e
AM
11727 .symtab, .strtab, and non-loaded reloc sections. We start the
11728 .symtab section at the current file position, and write directly
11729 to it. We build the .strtab section in memory. */
c152c796
AM
11730 bfd_get_symcount (abfd) = 0;
11731 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11732 /* sh_name is set in prep_headers. */
11733 symtab_hdr->sh_type = SHT_SYMTAB;
11734 /* sh_flags, sh_addr and sh_size all start off zero. */
11735 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11736 /* sh_link is set in assign_section_numbers. */
11737 /* sh_info is set below. */
11738 /* sh_offset is set just below. */
72de5009 11739 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11740
ef10c3ac
L
11741 if (max_sym_count < 20)
11742 max_sym_count = 20;
64f52338 11743 htab->strtabsize = max_sym_count;
ef10c3ac 11744 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11745 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11746 if (htab->strtab == NULL)
c152c796 11747 goto error_return;
ef10c3ac
L
11748 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11749 flinfo.symshndxbuf
11750 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11751 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11752
8539e4e8 11753 if (info->strip != strip_all || emit_relocs)
c152c796 11754 {
8539e4e8
AM
11755 file_ptr off = elf_next_file_pos (abfd);
11756
11757 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11758
11759 /* Note that at this point elf_next_file_pos (abfd) is
11760 incorrect. We do not yet know the size of the .symtab section.
11761 We correct next_file_pos below, after we do know the size. */
11762
11763 /* Start writing out the symbol table. The first symbol is always a
11764 dummy symbol. */
c152c796
AM
11765 elfsym.st_value = 0;
11766 elfsym.st_size = 0;
11767 elfsym.st_info = 0;
11768 elfsym.st_other = 0;
11769 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11770 elfsym.st_target_internal = 0;
ef10c3ac
L
11771 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11772 bfd_und_section_ptr, NULL) != 1)
c152c796 11773 goto error_return;
c152c796 11774
8539e4e8
AM
11775 /* Output a symbol for each section. We output these even if we are
11776 discarding local symbols, since they are used for relocs. These
11777 symbols have no names. We store the index of each one in the
11778 index field of the section, so that we can find it again when
11779 outputting relocs. */
11780
c152c796
AM
11781 elfsym.st_size = 0;
11782 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11783 elfsym.st_other = 0;
f0b5bb34 11784 elfsym.st_value = 0;
35fc36a8 11785 elfsym.st_target_internal = 0;
c152c796
AM
11786 for (i = 1; i < elf_numsections (abfd); i++)
11787 {
11788 o = bfd_section_from_elf_index (abfd, i);
11789 if (o != NULL)
f0b5bb34
AM
11790 {
11791 o->target_index = bfd_get_symcount (abfd);
11792 elfsym.st_shndx = i;
0e1862bb 11793 if (!bfd_link_relocatable (info))
f0b5bb34 11794 elfsym.st_value = o->vma;
ef10c3ac
L
11795 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11796 NULL) != 1)
f0b5bb34
AM
11797 goto error_return;
11798 }
c152c796
AM
11799 }
11800 }
11801
11802 /* Allocate some memory to hold information read in from the input
11803 files. */
11804 if (max_contents_size != 0)
11805 {
8b127cbc
AM
11806 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11807 if (flinfo.contents == NULL)
c152c796
AM
11808 goto error_return;
11809 }
11810
11811 if (max_external_reloc_size != 0)
11812 {
8b127cbc
AM
11813 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11814 if (flinfo.external_relocs == NULL)
c152c796
AM
11815 goto error_return;
11816 }
11817
11818 if (max_internal_reloc_count != 0)
11819 {
056bafd4 11820 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
11821 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11822 if (flinfo.internal_relocs == NULL)
c152c796
AM
11823 goto error_return;
11824 }
11825
11826 if (max_sym_count != 0)
11827 {
11828 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11829 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11830 if (flinfo.external_syms == NULL)
c152c796
AM
11831 goto error_return;
11832
11833 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11834 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11835 if (flinfo.internal_syms == NULL)
c152c796
AM
11836 goto error_return;
11837
11838 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11839 flinfo.indices = (long int *) bfd_malloc (amt);
11840 if (flinfo.indices == NULL)
c152c796
AM
11841 goto error_return;
11842
11843 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11844 flinfo.sections = (asection **) bfd_malloc (amt);
11845 if (flinfo.sections == NULL)
c152c796
AM
11846 goto error_return;
11847 }
11848
11849 if (max_sym_shndx_count != 0)
11850 {
11851 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11852 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11853 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11854 goto error_return;
11855 }
11856
64f52338 11857 if (htab->tls_sec)
c152c796
AM
11858 {
11859 bfd_vma base, end = 0;
11860 asection *sec;
11861
64f52338 11862 for (sec = htab->tls_sec;
c152c796
AM
11863 sec && (sec->flags & SEC_THREAD_LOCAL);
11864 sec = sec->next)
11865 {
3a800eb9 11866 bfd_size_type size = sec->size;
c152c796 11867
3a800eb9
AM
11868 if (size == 0
11869 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11870 {
91d6fa6a
NC
11871 struct bfd_link_order *ord = sec->map_tail.link_order;
11872
11873 if (ord != NULL)
11874 size = ord->offset + ord->size;
c152c796
AM
11875 }
11876 end = sec->vma + size;
11877 }
64f52338 11878 base = htab->tls_sec->vma;
7dc98aea
RO
11879 /* Only align end of TLS section if static TLS doesn't have special
11880 alignment requirements. */
11881 if (bed->static_tls_alignment == 1)
64f52338
AM
11882 end = align_power (end, htab->tls_sec->alignment_power);
11883 htab->tls_size = end - base;
c152c796
AM
11884 }
11885
0b52efa6
PB
11886 /* Reorder SHF_LINK_ORDER sections. */
11887 for (o = abfd->sections; o != NULL; o = o->next)
11888 {
11889 if (!elf_fixup_link_order (abfd, o))
11890 return FALSE;
11891 }
11892
2f0c68f2
CM
11893 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11894 return FALSE;
11895
c152c796
AM
11896 /* Since ELF permits relocations to be against local symbols, we
11897 must have the local symbols available when we do the relocations.
11898 Since we would rather only read the local symbols once, and we
11899 would rather not keep them in memory, we handle all the
11900 relocations for a single input file at the same time.
11901
11902 Unfortunately, there is no way to know the total number of local
11903 symbols until we have seen all of them, and the local symbol
11904 indices precede the global symbol indices. This means that when
11905 we are generating relocatable output, and we see a reloc against
11906 a global symbol, we can not know the symbol index until we have
11907 finished examining all the local symbols to see which ones we are
11908 going to output. To deal with this, we keep the relocations in
11909 memory, and don't output them until the end of the link. This is
11910 an unfortunate waste of memory, but I don't see a good way around
11911 it. Fortunately, it only happens when performing a relocatable
11912 link, which is not the common case. FIXME: If keep_memory is set
11913 we could write the relocs out and then read them again; I don't
11914 know how bad the memory loss will be. */
11915
c72f2fb2 11916 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11917 sub->output_has_begun = FALSE;
11918 for (o = abfd->sections; o != NULL; o = o->next)
11919 {
8423293d 11920 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11921 {
11922 if (p->type == bfd_indirect_link_order
11923 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11924 == bfd_target_elf_flavour)
11925 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11926 {
11927 if (! sub->output_has_begun)
11928 {
8b127cbc 11929 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11930 goto error_return;
11931 sub->output_has_begun = TRUE;
11932 }
11933 }
11934 else if (p->type == bfd_section_reloc_link_order
11935 || p->type == bfd_symbol_reloc_link_order)
11936 {
11937 if (! elf_reloc_link_order (abfd, info, o, p))
11938 goto error_return;
11939 }
11940 else
11941 {
11942 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11943 {
11944 if (p->type == bfd_indirect_link_order
11945 && (bfd_get_flavour (sub)
11946 == bfd_target_elf_flavour)
11947 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11948 != bed->s->elfclass))
11949 {
11950 const char *iclass, *oclass;
11951
aebf9be7 11952 switch (bed->s->elfclass)
351f65ca 11953 {
aebf9be7
NC
11954 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11955 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11956 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11957 default: abort ();
351f65ca 11958 }
aebf9be7
NC
11959
11960 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11961 {
aebf9be7
NC
11962 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11963 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11964 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11965 default: abort ();
351f65ca
L
11966 }
11967
11968 bfd_set_error (bfd_error_wrong_format);
4eca0228 11969 _bfd_error_handler
695344c0 11970 /* xgettext:c-format */
351f65ca
L
11971 (_("%B: file class %s incompatible with %s"),
11972 sub, iclass, oclass);
11973 }
11974
11975 goto error_return;
11976 }
c152c796
AM
11977 }
11978 }
11979 }
11980
c0f00686
L
11981 /* Free symbol buffer if needed. */
11982 if (!info->reduce_memory_overheads)
11983 {
c72f2fb2 11984 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11985 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11986 && elf_tdata (sub)->symbuf)
c0f00686
L
11987 {
11988 free (elf_tdata (sub)->symbuf);
11989 elf_tdata (sub)->symbuf = NULL;
11990 }
11991 }
11992
c152c796
AM
11993 /* Output any global symbols that got converted to local in a
11994 version script or due to symbol visibility. We do this in a
11995 separate step since ELF requires all local symbols to appear
11996 prior to any global symbols. FIXME: We should only do this if
11997 some global symbols were, in fact, converted to become local.
11998 FIXME: Will this work correctly with the Irix 5 linker? */
11999 eoinfo.failed = FALSE;
8b127cbc 12000 eoinfo.flinfo = &flinfo;
c152c796 12001 eoinfo.localsyms = TRUE;
34a79995 12002 eoinfo.file_sym_done = FALSE;
7686d77d 12003 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12004 if (eoinfo.failed)
12005 return FALSE;
12006
4e617b1e
PB
12007 /* If backend needs to output some local symbols not present in the hash
12008 table, do it now. */
8539e4e8
AM
12009 if (bed->elf_backend_output_arch_local_syms
12010 && (info->strip != strip_all || emit_relocs))
4e617b1e 12011 {
6e0b88f1 12012 typedef int (*out_sym_func)
4e617b1e
PB
12013 (void *, const char *, Elf_Internal_Sym *, asection *,
12014 struct elf_link_hash_entry *);
12015
12016 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12017 (abfd, info, &flinfo,
12018 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12019 return FALSE;
12020 }
12021
c152c796
AM
12022 /* That wrote out all the local symbols. Finish up the symbol table
12023 with the global symbols. Even if we want to strip everything we
12024 can, we still need to deal with those global symbols that got
12025 converted to local in a version script. */
12026
12027 /* The sh_info field records the index of the first non local symbol. */
12028 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12029
12030 if (dynamic
64f52338
AM
12031 && htab->dynsym != NULL
12032 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12033 {
12034 Elf_Internal_Sym sym;
64f52338 12035 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12036
64f52338
AM
12037 o = htab->dynsym->output_section;
12038 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12039
12040 /* Write out the section symbols for the output sections. */
0e1862bb 12041 if (bfd_link_pic (info)
64f52338 12042 || htab->is_relocatable_executable)
c152c796
AM
12043 {
12044 asection *s;
12045
12046 sym.st_size = 0;
12047 sym.st_name = 0;
12048 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12049 sym.st_other = 0;
35fc36a8 12050 sym.st_target_internal = 0;
c152c796
AM
12051
12052 for (s = abfd->sections; s != NULL; s = s->next)
12053 {
12054 int indx;
12055 bfd_byte *dest;
12056 long dynindx;
12057
c152c796 12058 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12059 if (dynindx <= 0)
12060 continue;
12061 indx = elf_section_data (s)->this_idx;
c152c796
AM
12062 BFD_ASSERT (indx > 0);
12063 sym.st_shndx = indx;
c0d5a53d
L
12064 if (! check_dynsym (abfd, &sym))
12065 return FALSE;
c152c796
AM
12066 sym.st_value = s->vma;
12067 dest = dynsym + dynindx * bed->s->sizeof_sym;
12068 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12069 }
c152c796
AM
12070 }
12071
12072 /* Write out the local dynsyms. */
64f52338 12073 if (htab->dynlocal)
c152c796
AM
12074 {
12075 struct elf_link_local_dynamic_entry *e;
64f52338 12076 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12077 {
12078 asection *s;
12079 bfd_byte *dest;
12080
935bd1e0 12081 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12082 Note that we saved a word of storage and overwrote
12083 the original st_name with the dynstr_index. */
12084 sym = e->isym;
935bd1e0 12085 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12086
cb33740c
AM
12087 s = bfd_section_from_elf_index (e->input_bfd,
12088 e->isym.st_shndx);
12089 if (s != NULL)
c152c796 12090 {
c152c796
AM
12091 sym.st_shndx =
12092 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12093 if (! check_dynsym (abfd, &sym))
12094 return FALSE;
c152c796
AM
12095 sym.st_value = (s->output_section->vma
12096 + s->output_offset
12097 + e->isym.st_value);
12098 }
12099
c152c796
AM
12100 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12101 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12102 }
12103 }
c152c796
AM
12104 }
12105
12106 /* We get the global symbols from the hash table. */
12107 eoinfo.failed = FALSE;
12108 eoinfo.localsyms = FALSE;
8b127cbc 12109 eoinfo.flinfo = &flinfo;
7686d77d 12110 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12111 if (eoinfo.failed)
12112 return FALSE;
12113
12114 /* If backend needs to output some symbols not present in the hash
12115 table, do it now. */
8539e4e8
AM
12116 if (bed->elf_backend_output_arch_syms
12117 && (info->strip != strip_all || emit_relocs))
c152c796 12118 {
6e0b88f1 12119 typedef int (*out_sym_func)
c152c796
AM
12120 (void *, const char *, Elf_Internal_Sym *, asection *,
12121 struct elf_link_hash_entry *);
12122
12123 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12124 (abfd, info, &flinfo,
12125 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12126 return FALSE;
12127 }
12128
ef10c3ac
L
12129 /* Finalize the .strtab section. */
12130 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12131
12132 /* Swap out the .strtab section. */
12133 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12134 return FALSE;
12135
12136 /* Now we know the size of the symtab section. */
c152c796
AM
12137 if (bfd_get_symcount (abfd) > 0)
12138 {
ee3b52e9
L
12139 /* Finish up and write out the symbol string table (.strtab)
12140 section. */
ad32986f 12141 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12142 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12143
ad32986f 12144 if (elf_symtab_shndx_list (abfd))
8539e4e8 12145 {
ad32986f 12146 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12147
ad32986f
NC
12148 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12149 {
12150 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12151 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12152 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12153 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12154 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12155
ad32986f
NC
12156 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12157 off, TRUE);
12158
12159 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12160 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12161 return FALSE;
12162 }
8539e4e8 12163 }
ee3b52e9
L
12164
12165 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12166 /* sh_name was set in prep_headers. */
12167 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12168 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12169 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12170 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12171 symstrtab_hdr->sh_entsize = 0;
12172 symstrtab_hdr->sh_link = 0;
12173 symstrtab_hdr->sh_info = 0;
12174 /* sh_offset is set just below. */
12175 symstrtab_hdr->sh_addralign = 1;
12176
12177 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12178 off, TRUE);
12179 elf_next_file_pos (abfd) = off;
12180
c152c796 12181 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12182 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12183 return FALSE;
12184 }
12185
76359541
TP
12186 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12187 {
4eca0228
AM
12188 _bfd_error_handler (_("%B: failed to generate import library"),
12189 info->out_implib_bfd);
76359541
TP
12190 return FALSE;
12191 }
12192
c152c796
AM
12193 /* Adjust the relocs to have the correct symbol indices. */
12194 for (o = abfd->sections; o != NULL; o = o->next)
12195 {
d4730f92 12196 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12197 bfd_boolean sort;
10bbbc1d 12198
c152c796
AM
12199 if ((o->flags & SEC_RELOC) == 0)
12200 continue;
12201
28dbcedc 12202 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12203 if (esdo->rel.hdr != NULL
10bbbc1d 12204 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12205 return FALSE;
12206 if (esdo->rela.hdr != NULL
10bbbc1d 12207 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12208 return FALSE;
c152c796
AM
12209
12210 /* Set the reloc_count field to 0 to prevent write_relocs from
12211 trying to swap the relocs out itself. */
12212 o->reloc_count = 0;
12213 }
12214
12215 if (dynamic && info->combreloc && dynobj != NULL)
12216 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12217
12218 /* If we are linking against a dynamic object, or generating a
12219 shared library, finish up the dynamic linking information. */
12220 if (dynamic)
12221 {
12222 bfd_byte *dyncon, *dynconend;
12223
12224 /* Fix up .dynamic entries. */
3d4d4302 12225 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12226 BFD_ASSERT (o != NULL);
12227
12228 dyncon = o->contents;
eea6121a 12229 dynconend = o->contents + o->size;
c152c796
AM
12230 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12231 {
12232 Elf_Internal_Dyn dyn;
12233 const char *name;
12234 unsigned int type;
64487780
AM
12235 bfd_size_type sh_size;
12236 bfd_vma sh_addr;
c152c796
AM
12237
12238 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12239
12240 switch (dyn.d_tag)
12241 {
12242 default:
12243 continue;
12244 case DT_NULL:
12245 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12246 {
12247 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12248 {
12249 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12250 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12251 default: continue;
12252 }
12253 dyn.d_un.d_val = relativecount;
12254 relativecount = 0;
12255 break;
12256 }
12257 continue;
12258
12259 case DT_INIT:
12260 name = info->init_function;
12261 goto get_sym;
12262 case DT_FINI:
12263 name = info->fini_function;
12264 get_sym:
12265 {
12266 struct elf_link_hash_entry *h;
12267
64f52338 12268 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12269 if (h != NULL
12270 && (h->root.type == bfd_link_hash_defined
12271 || h->root.type == bfd_link_hash_defweak))
12272 {
bef26483 12273 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12274 o = h->root.u.def.section;
12275 if (o->output_section != NULL)
bef26483 12276 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12277 + o->output_offset);
12278 else
12279 {
12280 /* The symbol is imported from another shared
12281 library and does not apply to this one. */
bef26483 12282 dyn.d_un.d_ptr = 0;
c152c796
AM
12283 }
12284 break;
12285 }
12286 }
12287 continue;
12288
12289 case DT_PREINIT_ARRAYSZ:
12290 name = ".preinit_array";
4ade44b7 12291 goto get_out_size;
c152c796
AM
12292 case DT_INIT_ARRAYSZ:
12293 name = ".init_array";
4ade44b7 12294 goto get_out_size;
c152c796
AM
12295 case DT_FINI_ARRAYSZ:
12296 name = ".fini_array";
4ade44b7 12297 get_out_size:
c152c796
AM
12298 o = bfd_get_section_by_name (abfd, name);
12299 if (o == NULL)
12300 {
4eca0228 12301 _bfd_error_handler
4ade44b7 12302 (_("could not find section %s"), name);
c152c796
AM
12303 goto error_return;
12304 }
eea6121a 12305 if (o->size == 0)
4eca0228 12306 _bfd_error_handler
c152c796 12307 (_("warning: %s section has zero size"), name);
eea6121a 12308 dyn.d_un.d_val = o->size;
c152c796
AM
12309 break;
12310
12311 case DT_PREINIT_ARRAY:
12312 name = ".preinit_array";
4ade44b7 12313 goto get_out_vma;
c152c796
AM
12314 case DT_INIT_ARRAY:
12315 name = ".init_array";
4ade44b7 12316 goto get_out_vma;
c152c796
AM
12317 case DT_FINI_ARRAY:
12318 name = ".fini_array";
4ade44b7
AM
12319 get_out_vma:
12320 o = bfd_get_section_by_name (abfd, name);
12321 goto do_vma;
c152c796
AM
12322
12323 case DT_HASH:
12324 name = ".hash";
12325 goto get_vma;
fdc90cb4
JJ
12326 case DT_GNU_HASH:
12327 name = ".gnu.hash";
12328 goto get_vma;
c152c796
AM
12329 case DT_STRTAB:
12330 name = ".dynstr";
12331 goto get_vma;
12332 case DT_SYMTAB:
12333 name = ".dynsym";
12334 goto get_vma;
12335 case DT_VERDEF:
12336 name = ".gnu.version_d";
12337 goto get_vma;
12338 case DT_VERNEED:
12339 name = ".gnu.version_r";
12340 goto get_vma;
12341 case DT_VERSYM:
12342 name = ".gnu.version";
12343 get_vma:
4ade44b7
AM
12344 o = bfd_get_linker_section (dynobj, name);
12345 do_vma:
b3293efa 12346 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12347 {
4eca0228 12348 _bfd_error_handler
4ade44b7 12349 (_("could not find section %s"), name);
c152c796
AM
12350 goto error_return;
12351 }
894891db
NC
12352 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12353 {
4eca0228 12354 _bfd_error_handler
894891db
NC
12355 (_("warning: section '%s' is being made into a note"), name);
12356 bfd_set_error (bfd_error_nonrepresentable_section);
12357 goto error_return;
12358 }
4ade44b7 12359 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12360 break;
12361
12362 case DT_REL:
12363 case DT_RELA:
12364 case DT_RELSZ:
12365 case DT_RELASZ:
12366 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12367 type = SHT_REL;
12368 else
12369 type = SHT_RELA;
64487780
AM
12370 sh_size = 0;
12371 sh_addr = 0;
c152c796
AM
12372 for (i = 1; i < elf_numsections (abfd); i++)
12373 {
12374 Elf_Internal_Shdr *hdr;
12375
12376 hdr = elf_elfsections (abfd)[i];
12377 if (hdr->sh_type == type
12378 && (hdr->sh_flags & SHF_ALLOC) != 0)
12379 {
64487780
AM
12380 sh_size += hdr->sh_size;
12381 if (sh_addr == 0
12382 || sh_addr > hdr->sh_addr)
12383 sh_addr = hdr->sh_addr;
c152c796
AM
12384 }
12385 }
64487780 12386
64f52338
AM
12387 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12388 {
12389 /* Don't count procedure linkage table relocs in the
12390 overall reloc count. */
64487780
AM
12391 sh_size -= htab->srelplt->size;
12392 if (sh_size == 0)
12393 /* If the size is zero, make the address zero too.
12394 This is to avoid a glibc bug. If the backend
12395 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12396 zero, then we'll put DT_RELA at the end of
12397 DT_JMPREL. glibc will interpret the end of
12398 DT_RELA matching the end of DT_JMPREL as the
12399 case where DT_RELA includes DT_JMPREL, and for
12400 LD_BIND_NOW will decide that processing DT_RELA
12401 will process the PLT relocs too. Net result:
12402 No PLT relocs applied. */
12403 sh_addr = 0;
12404
64f52338
AM
12405 /* If .rela.plt is the first .rela section, exclude
12406 it from DT_RELA. */
64487780
AM
12407 else if (sh_addr == (htab->srelplt->output_section->vma
12408 + htab->srelplt->output_offset))
12409 sh_addr += htab->srelplt->size;
64f52338 12410 }
64487780
AM
12411
12412 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12413 dyn.d_un.d_val = sh_size;
12414 else
12415 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12416 break;
12417 }
12418 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12419 }
12420 }
12421
12422 /* If we have created any dynamic sections, then output them. */
12423 if (dynobj != NULL)
12424 {
12425 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12426 goto error_return;
12427
943284cc 12428 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12429 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12430 || info->error_textrel)
3d4d4302 12431 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12432 {
12433 bfd_byte *dyncon, *dynconend;
12434
943284cc
DJ
12435 dyncon = o->contents;
12436 dynconend = o->contents + o->size;
12437 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12438 {
12439 Elf_Internal_Dyn dyn;
12440
12441 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12442
12443 if (dyn.d_tag == DT_TEXTREL)
12444 {
c192a133
AM
12445 if (info->error_textrel)
12446 info->callbacks->einfo
12447 (_("%P%X: read-only segment has dynamic relocations.\n"));
12448 else
12449 info->callbacks->einfo
12450 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12451 break;
12452 }
12453 }
12454 }
12455
c152c796
AM
12456 for (o = dynobj->sections; o != NULL; o = o->next)
12457 {
12458 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12459 || o->size == 0
c152c796
AM
12460 || o->output_section == bfd_abs_section_ptr)
12461 continue;
12462 if ((o->flags & SEC_LINKER_CREATED) == 0)
12463 {
12464 /* At this point, we are only interested in sections
12465 created by _bfd_elf_link_create_dynamic_sections. */
12466 continue;
12467 }
64f52338 12468 if (htab->stab_info.stabstr == o)
3722b82f 12469 continue;
64f52338 12470 if (htab->eh_info.hdr_sec == o)
eea6121a 12471 continue;
3d4d4302 12472 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12473 {
12474 if (! bfd_set_section_contents (abfd, o->output_section,
12475 o->contents,
37b01f6a
DG
12476 (file_ptr) o->output_offset
12477 * bfd_octets_per_byte (abfd),
eea6121a 12478 o->size))
c152c796
AM
12479 goto error_return;
12480 }
12481 else
12482 {
12483 /* The contents of the .dynstr section are actually in a
12484 stringtab. */
8539e4e8
AM
12485 file_ptr off;
12486
c152c796
AM
12487 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12488 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12489 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12490 goto error_return;
12491 }
12492 }
12493 }
12494
7bdf4127 12495 if (!info->resolve_section_groups)
c152c796
AM
12496 {
12497 bfd_boolean failed = FALSE;
12498
7bdf4127 12499 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12500 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12501 if (failed)
12502 goto error_return;
12503 }
12504
12505 /* If we have optimized stabs strings, output them. */
64f52338 12506 if (htab->stab_info.stabstr != NULL)
c152c796 12507 {
64f52338 12508 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12509 goto error_return;
12510 }
12511
9f7c3e5e
AM
12512 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12513 goto error_return;
c152c796 12514
9f7c3e5e 12515 elf_final_link_free (abfd, &flinfo);
c152c796 12516
12bd6957 12517 elf_linker (abfd) = TRUE;
c152c796 12518
104d59d1
JM
12519 if (attr_section)
12520 {
a50b1753 12521 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12522 if (contents == NULL)
d0f16d5e 12523 return FALSE; /* Bail out and fail. */
104d59d1
JM
12524 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12525 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12526 free (contents);
12527 }
12528
c152c796
AM
12529 return TRUE;
12530
12531 error_return:
9f7c3e5e 12532 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12533 return FALSE;
12534}
12535\f
5241d853
RS
12536/* Initialize COOKIE for input bfd ABFD. */
12537
12538static bfd_boolean
12539init_reloc_cookie (struct elf_reloc_cookie *cookie,
12540 struct bfd_link_info *info, bfd *abfd)
12541{
12542 Elf_Internal_Shdr *symtab_hdr;
12543 const struct elf_backend_data *bed;
12544
12545 bed = get_elf_backend_data (abfd);
12546 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12547
12548 cookie->abfd = abfd;
12549 cookie->sym_hashes = elf_sym_hashes (abfd);
12550 cookie->bad_symtab = elf_bad_symtab (abfd);
12551 if (cookie->bad_symtab)
12552 {
12553 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12554 cookie->extsymoff = 0;
12555 }
12556 else
12557 {
12558 cookie->locsymcount = symtab_hdr->sh_info;
12559 cookie->extsymoff = symtab_hdr->sh_info;
12560 }
12561
12562 if (bed->s->arch_size == 32)
12563 cookie->r_sym_shift = 8;
12564 else
12565 cookie->r_sym_shift = 32;
12566
12567 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12568 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12569 {
12570 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12571 cookie->locsymcount, 0,
12572 NULL, NULL, NULL);
12573 if (cookie->locsyms == NULL)
12574 {
12575 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12576 return FALSE;
12577 }
12578 if (info->keep_memory)
12579 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12580 }
12581 return TRUE;
12582}
12583
12584/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12585
12586static void
12587fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12588{
12589 Elf_Internal_Shdr *symtab_hdr;
12590
12591 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12592 if (cookie->locsyms != NULL
12593 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12594 free (cookie->locsyms);
12595}
12596
12597/* Initialize the relocation information in COOKIE for input section SEC
12598 of input bfd ABFD. */
12599
12600static bfd_boolean
12601init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12602 struct bfd_link_info *info, bfd *abfd,
12603 asection *sec)
12604{
5241d853
RS
12605 if (sec->reloc_count == 0)
12606 {
12607 cookie->rels = NULL;
12608 cookie->relend = NULL;
12609 }
12610 else
12611 {
5241d853
RS
12612 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12613 info->keep_memory);
12614 if (cookie->rels == NULL)
12615 return FALSE;
12616 cookie->rel = cookie->rels;
056bafd4 12617 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12618 }
12619 cookie->rel = cookie->rels;
12620 return TRUE;
12621}
12622
12623/* Free the memory allocated by init_reloc_cookie_rels,
12624 if appropriate. */
12625
12626static void
12627fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12628 asection *sec)
12629{
12630 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12631 free (cookie->rels);
12632}
12633
12634/* Initialize the whole of COOKIE for input section SEC. */
12635
12636static bfd_boolean
12637init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12638 struct bfd_link_info *info,
12639 asection *sec)
12640{
12641 if (!init_reloc_cookie (cookie, info, sec->owner))
12642 goto error1;
12643 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12644 goto error2;
12645 return TRUE;
12646
12647 error2:
12648 fini_reloc_cookie (cookie, sec->owner);
12649 error1:
12650 return FALSE;
12651}
12652
12653/* Free the memory allocated by init_reloc_cookie_for_section,
12654 if appropriate. */
12655
12656static void
12657fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12658 asection *sec)
12659{
12660 fini_reloc_cookie_rels (cookie, sec);
12661 fini_reloc_cookie (cookie, sec->owner);
12662}
12663\f
c152c796
AM
12664/* Garbage collect unused sections. */
12665
07adf181
AM
12666/* Default gc_mark_hook. */
12667
12668asection *
12669_bfd_elf_gc_mark_hook (asection *sec,
12670 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12671 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12672 struct elf_link_hash_entry *h,
12673 Elf_Internal_Sym *sym)
12674{
12675 if (h != NULL)
12676 {
12677 switch (h->root.type)
12678 {
12679 case bfd_link_hash_defined:
12680 case bfd_link_hash_defweak:
12681 return h->root.u.def.section;
12682
12683 case bfd_link_hash_common:
12684 return h->root.u.c.p->section;
12685
12686 default:
12687 break;
12688 }
12689 }
12690 else
12691 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12692
12693 return NULL;
12694}
12695
b7c871ed
L
12696/* Return the global debug definition section. */
12697
12698static asection *
12699elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12700 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12701 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12702 struct elf_link_hash_entry *h,
12703 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12704{
12705 if (h != NULL
12706 && (h->root.type == bfd_link_hash_defined
12707 || h->root.type == bfd_link_hash_defweak)
12708 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12709 return h->root.u.def.section;
12710
12711 return NULL;
12712}
12713
5241d853
RS
12714/* COOKIE->rel describes a relocation against section SEC, which is
12715 a section we've decided to keep. Return the section that contains
12716 the relocation symbol, or NULL if no section contains it. */
12717
12718asection *
12719_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12720 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12721 struct elf_reloc_cookie *cookie,
12722 bfd_boolean *start_stop)
5241d853
RS
12723{
12724 unsigned long r_symndx;
12725 struct elf_link_hash_entry *h;
12726
12727 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12728 if (r_symndx == STN_UNDEF)
5241d853
RS
12729 return NULL;
12730
12731 if (r_symndx >= cookie->locsymcount
12732 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12733 {
12734 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12735 if (h == NULL)
12736 {
12737 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12738 sec->owner);
12739 return NULL;
12740 }
5241d853
RS
12741 while (h->root.type == bfd_link_hash_indirect
12742 || h->root.type == bfd_link_hash_warning)
12743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12744 h->mark = 1;
4e6b54a6
AM
12745 /* If this symbol is weak and there is a non-weak definition, we
12746 keep the non-weak definition because many backends put
12747 dynamic reloc info on the non-weak definition for code
12748 handling copy relocs. */
12749 if (h->u.weakdef != NULL)
12750 h->u.weakdef->mark = 1;
1cce69b9 12751
a6a4679f 12752 if (start_stop != NULL)
1cce69b9 12753 {
7dba9362
AM
12754 /* To work around a glibc bug, mark XXX input sections
12755 when there is a reference to __start_XXX or __stop_XXX
12756 symbols. */
cbd0eecf 12757 if (h->start_stop)
1cce69b9 12758 {
cbd0eecf 12759 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12760 *start_stop = !s->gc_mark;
12761 return s;
1cce69b9
AM
12762 }
12763 }
12764
5241d853
RS
12765 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12766 }
12767
12768 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12769 &cookie->locsyms[r_symndx]);
12770}
12771
12772/* COOKIE->rel describes a relocation against section SEC, which is
12773 a section we've decided to keep. Mark the section that contains
9d0a14d3 12774 the relocation symbol. */
5241d853
RS
12775
12776bfd_boolean
12777_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12778 asection *sec,
12779 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12780 struct elf_reloc_cookie *cookie)
5241d853
RS
12781{
12782 asection *rsec;
1cce69b9 12783 bfd_boolean start_stop = FALSE;
5241d853 12784
1cce69b9
AM
12785 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12786 while (rsec != NULL)
5241d853 12787 {
1cce69b9
AM
12788 if (!rsec->gc_mark)
12789 {
12790 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12791 || (rsec->owner->flags & DYNAMIC) != 0)
12792 rsec->gc_mark = 1;
12793 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12794 return FALSE;
12795 }
12796 if (!start_stop)
12797 break;
199af150 12798 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12799 }
12800 return TRUE;
12801}
12802
07adf181
AM
12803/* The mark phase of garbage collection. For a given section, mark
12804 it and any sections in this section's group, and all the sections
12805 which define symbols to which it refers. */
12806
ccfa59ea
AM
12807bfd_boolean
12808_bfd_elf_gc_mark (struct bfd_link_info *info,
12809 asection *sec,
6a5bb875 12810 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12811{
12812 bfd_boolean ret;
9d0a14d3 12813 asection *group_sec, *eh_frame;
c152c796
AM
12814
12815 sec->gc_mark = 1;
12816
12817 /* Mark all the sections in the group. */
12818 group_sec = elf_section_data (sec)->next_in_group;
12819 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12820 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12821 return FALSE;
12822
12823 /* Look through the section relocs. */
12824 ret = TRUE;
9d0a14d3
RS
12825 eh_frame = elf_eh_frame_section (sec->owner);
12826 if ((sec->flags & SEC_RELOC) != 0
12827 && sec->reloc_count > 0
12828 && sec != eh_frame)
c152c796 12829 {
5241d853 12830 struct elf_reloc_cookie cookie;
c152c796 12831
5241d853
RS
12832 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12833 ret = FALSE;
c152c796 12834 else
c152c796 12835 {
5241d853 12836 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12837 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12838 {
12839 ret = FALSE;
12840 break;
12841 }
12842 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12843 }
12844 }
9d0a14d3
RS
12845
12846 if (ret && eh_frame && elf_fde_list (sec))
12847 {
12848 struct elf_reloc_cookie cookie;
12849
12850 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12851 ret = FALSE;
12852 else
12853 {
12854 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12855 gc_mark_hook, &cookie))
12856 ret = FALSE;
12857 fini_reloc_cookie_for_section (&cookie, eh_frame);
12858 }
12859 }
12860
2f0c68f2
CM
12861 eh_frame = elf_section_eh_frame_entry (sec);
12862 if (ret && eh_frame && !eh_frame->gc_mark)
12863 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12864 ret = FALSE;
12865
c152c796
AM
12866 return ret;
12867}
12868
3c758495
TG
12869/* Scan and mark sections in a special or debug section group. */
12870
12871static void
12872_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12873{
12874 /* Point to first section of section group. */
12875 asection *ssec;
12876 /* Used to iterate the section group. */
12877 asection *msec;
12878
12879 bfd_boolean is_special_grp = TRUE;
12880 bfd_boolean is_debug_grp = TRUE;
12881
12882 /* First scan to see if group contains any section other than debug
12883 and special section. */
12884 ssec = msec = elf_next_in_group (grp);
12885 do
12886 {
12887 if ((msec->flags & SEC_DEBUGGING) == 0)
12888 is_debug_grp = FALSE;
12889
12890 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12891 is_special_grp = FALSE;
12892
12893 msec = elf_next_in_group (msec);
12894 }
12895 while (msec != ssec);
12896
12897 /* If this is a pure debug section group or pure special section group,
12898 keep all sections in this group. */
12899 if (is_debug_grp || is_special_grp)
12900 {
12901 do
12902 {
12903 msec->gc_mark = 1;
12904 msec = elf_next_in_group (msec);
12905 }
12906 while (msec != ssec);
12907 }
12908}
12909
7f6ab9f8
AM
12910/* Keep debug and special sections. */
12911
12912bfd_boolean
12913_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12914 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12915{
12916 bfd *ibfd;
12917
c72f2fb2 12918 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12919 {
12920 asection *isec;
12921 bfd_boolean some_kept;
b40bf0a2 12922 bfd_boolean debug_frag_seen;
b7c871ed 12923 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
12924
12925 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12926 continue;
57963c05
AM
12927 isec = ibfd->sections;
12928 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
12929 continue;
7f6ab9f8 12930
b40bf0a2
NC
12931 /* Ensure all linker created sections are kept,
12932 see if any other section is already marked,
12933 and note if we have any fragmented debug sections. */
b7c871ed 12934 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
12935 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12936 {
12937 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12938 isec->gc_mark = 1;
eb026f09
AM
12939 else if (isec->gc_mark
12940 && (isec->flags & SEC_ALLOC) != 0
12941 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 12942 some_kept = TRUE;
b40bf0a2 12943
535b785f 12944 if (!debug_frag_seen
b40bf0a2
NC
12945 && (isec->flags & SEC_DEBUGGING)
12946 && CONST_STRNEQ (isec->name, ".debug_line."))
12947 debug_frag_seen = TRUE;
7f6ab9f8
AM
12948 }
12949
eb026f09
AM
12950 /* If no non-note alloc section in this file will be kept, then
12951 we can toss out the debug and special sections. */
7f6ab9f8
AM
12952 if (!some_kept)
12953 continue;
12954
12955 /* Keep debug and special sections like .comment when they are
3c758495
TG
12956 not part of a group. Also keep section groups that contain
12957 just debug sections or special sections. */
7f6ab9f8 12958 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12959 {
12960 if ((isec->flags & SEC_GROUP) != 0)
12961 _bfd_elf_gc_mark_debug_special_section_group (isec);
12962 else if (((isec->flags & SEC_DEBUGGING) != 0
12963 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12964 && elf_next_in_group (isec) == NULL)
12965 isec->gc_mark = 1;
b7c871ed
L
12966 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12967 has_kept_debug_info = TRUE;
3c758495 12968 }
b40bf0a2 12969
b40bf0a2
NC
12970 /* Look for CODE sections which are going to be discarded,
12971 and find and discard any fragmented debug sections which
12972 are associated with that code section. */
b7c871ed
L
12973 if (debug_frag_seen)
12974 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12975 if ((isec->flags & SEC_CODE) != 0
12976 && isec->gc_mark == 0)
12977 {
12978 unsigned int ilen;
12979 asection *dsec;
b40bf0a2 12980
b7c871ed 12981 ilen = strlen (isec->name);
b40bf0a2 12982
b7c871ed
L
12983 /* Association is determined by the name of the debug
12984 section containing the name of the code section as
12985 a suffix. For example .debug_line.text.foo is a
12986 debug section associated with .text.foo. */
12987 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12988 {
12989 unsigned int dlen;
b40bf0a2 12990
b7c871ed
L
12991 if (dsec->gc_mark == 0
12992 || (dsec->flags & SEC_DEBUGGING) == 0)
12993 continue;
b40bf0a2 12994
b7c871ed 12995 dlen = strlen (dsec->name);
b40bf0a2 12996
b7c871ed
L
12997 if (dlen > ilen
12998 && strncmp (dsec->name + (dlen - ilen),
12999 isec->name, ilen) == 0)
b40bf0a2 13000 dsec->gc_mark = 0;
b7c871ed 13001 }
b40bf0a2 13002 }
b7c871ed
L
13003
13004 /* Mark debug sections referenced by kept debug sections. */
13005 if (has_kept_debug_info)
13006 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13007 if (isec->gc_mark
13008 && (isec->flags & SEC_DEBUGGING) != 0)
13009 if (!_bfd_elf_gc_mark (info, isec,
13010 elf_gc_mark_debug_section))
13011 return FALSE;
7f6ab9f8
AM
13012 }
13013 return TRUE;
13014}
13015
c152c796
AM
13016/* The sweep phase of garbage collection. Remove all garbage sections. */
13017
13018typedef bfd_boolean (*gc_sweep_hook_fn)
13019 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
13020
13021static bfd_boolean
ccabcbe5 13022elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13023{
13024 bfd *sub;
ccabcbe5
AM
13025 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13026 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 13027
c72f2fb2 13028 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13029 {
13030 asection *o;
13031
b19a8f85
L
13032 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13033 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13034 continue;
57963c05
AM
13035 o = sub->sections;
13036 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13037 continue;
c152c796
AM
13038
13039 for (o = sub->sections; o != NULL; o = o->next)
13040 {
a33dafc3
L
13041 /* When any section in a section group is kept, we keep all
13042 sections in the section group. If the first member of
13043 the section group is excluded, we will also exclude the
13044 group section. */
13045 if (o->flags & SEC_GROUP)
13046 {
13047 asection *first = elf_next_in_group (o);
13048 o->gc_mark = first->gc_mark;
13049 }
c152c796 13050
1e7eae0d 13051 if (o->gc_mark)
c152c796
AM
13052 continue;
13053
13054 /* Skip sweeping sections already excluded. */
13055 if (o->flags & SEC_EXCLUDE)
13056 continue;
13057
13058 /* Since this is early in the link process, it is simple
13059 to remove a section from the output. */
13060 o->flags |= SEC_EXCLUDE;
13061
c55fe096 13062 if (info->print_gc_sections && o->size != 0)
695344c0 13063 /* xgettext:c-format */
c08bb8dd
AM
13064 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13065 o, sub);
c17d87de 13066
c152c796
AM
13067 /* But we also have to update some of the relocation
13068 info we collected before. */
13069 if (gc_sweep_hook
e8aaee2a 13070 && (o->flags & SEC_RELOC) != 0
9850436d
AM
13071 && o->reloc_count != 0
13072 && !((info->strip == strip_all || info->strip == strip_debugger)
13073 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 13074 && !bfd_is_abs_section (o->output_section))
c152c796
AM
13075 {
13076 Elf_Internal_Rela *internal_relocs;
13077 bfd_boolean r;
13078
13079 internal_relocs
13080 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13081 info->keep_memory);
13082 if (internal_relocs == NULL)
13083 return FALSE;
13084
13085 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13086
13087 if (elf_section_data (o)->relocs != internal_relocs)
13088 free (internal_relocs);
13089
13090 if (!r)
13091 return FALSE;
13092 }
13093 }
13094 }
13095
c152c796
AM
13096 return TRUE;
13097}
13098
13099/* Propagate collected vtable information. This is called through
13100 elf_link_hash_traverse. */
13101
13102static bfd_boolean
13103elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13104{
c152c796 13105 /* Those that are not vtables. */
cbd0eecf
L
13106 if (h->start_stop
13107 || h->u2.vtable == NULL
13108 || h->u2.vtable->parent == NULL)
c152c796
AM
13109 return TRUE;
13110
13111 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13112 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13113 return TRUE;
13114
13115 /* If we've already been done, exit. */
cbd0eecf 13116 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13117 return TRUE;
13118
13119 /* Make sure the parent's table is up to date. */
cbd0eecf 13120 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13121
cbd0eecf 13122 if (h->u2.vtable->used == NULL)
c152c796
AM
13123 {
13124 /* None of this table's entries were referenced. Re-use the
13125 parent's table. */
cbd0eecf
L
13126 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13127 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13128 }
13129 else
13130 {
13131 size_t n;
13132 bfd_boolean *cu, *pu;
13133
13134 /* Or the parent's entries into ours. */
cbd0eecf 13135 cu = h->u2.vtable->used;
c152c796 13136 cu[-1] = TRUE;
cbd0eecf 13137 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13138 if (pu != NULL)
13139 {
13140 const struct elf_backend_data *bed;
13141 unsigned int log_file_align;
13142
13143 bed = get_elf_backend_data (h->root.u.def.section->owner);
13144 log_file_align = bed->s->log_file_align;
cbd0eecf 13145 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13146 while (n--)
13147 {
13148 if (*pu)
13149 *cu = TRUE;
13150 pu++;
13151 cu++;
13152 }
13153 }
13154 }
13155
13156 return TRUE;
13157}
13158
13159static bfd_boolean
13160elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13161{
13162 asection *sec;
13163 bfd_vma hstart, hend;
13164 Elf_Internal_Rela *relstart, *relend, *rel;
13165 const struct elf_backend_data *bed;
13166 unsigned int log_file_align;
13167
c152c796
AM
13168 /* Take care of both those symbols that do not describe vtables as
13169 well as those that are not loaded. */
cbd0eecf
L
13170 if (h->start_stop
13171 || h->u2.vtable == NULL
13172 || h->u2.vtable->parent == NULL)
c152c796
AM
13173 return TRUE;
13174
13175 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13176 || h->root.type == bfd_link_hash_defweak);
13177
13178 sec = h->root.u.def.section;
13179 hstart = h->root.u.def.value;
13180 hend = hstart + h->size;
13181
13182 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13183 if (!relstart)
13184 return *(bfd_boolean *) okp = FALSE;
13185 bed = get_elf_backend_data (sec->owner);
13186 log_file_align = bed->s->log_file_align;
13187
056bafd4 13188 relend = relstart + sec->reloc_count;
c152c796
AM
13189
13190 for (rel = relstart; rel < relend; ++rel)
13191 if (rel->r_offset >= hstart && rel->r_offset < hend)
13192 {
13193 /* If the entry is in use, do nothing. */
cbd0eecf
L
13194 if (h->u2.vtable->used
13195 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13196 {
13197 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13198 if (h->u2.vtable->used[entry])
c152c796
AM
13199 continue;
13200 }
13201 /* Otherwise, kill it. */
13202 rel->r_offset = rel->r_info = rel->r_addend = 0;
13203 }
13204
13205 return TRUE;
13206}
13207
87538722
AM
13208/* Mark sections containing dynamically referenced symbols. When
13209 building shared libraries, we must assume that any visible symbol is
13210 referenced. */
715df9b8 13211
64d03ab5
AM
13212bfd_boolean
13213bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13214{
87538722 13215 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13216 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13217
715df9b8
EB
13218 if ((h->root.type == bfd_link_hash_defined
13219 || h->root.type == bfd_link_hash_defweak)
87538722 13220 && (h->ref_dynamic
c4621b33 13221 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13222 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13223 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13224 && (!bfd_link_executable (info)
22185505 13225 || info->gc_keep_exported
b407645f
AM
13226 || info->export_dynamic
13227 || (h->dynamic
13228 && d != NULL
13229 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13230 && (h->versioned >= versioned
54e8959c
L
13231 || !bfd_hide_sym_by_version (info->version_info,
13232 h->root.root.string)))))
715df9b8
EB
13233 h->root.u.def.section->flags |= SEC_KEEP;
13234
13235 return TRUE;
13236}
3b36f7e6 13237
74f0fb50
AM
13238/* Keep all sections containing symbols undefined on the command-line,
13239 and the section containing the entry symbol. */
13240
13241void
13242_bfd_elf_gc_keep (struct bfd_link_info *info)
13243{
13244 struct bfd_sym_chain *sym;
13245
13246 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13247 {
13248 struct elf_link_hash_entry *h;
13249
13250 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13251 FALSE, FALSE, FALSE);
13252
13253 if (h != NULL
13254 && (h->root.type == bfd_link_hash_defined
13255 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13256 && !bfd_is_abs_section (h->root.u.def.section)
13257 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13258 h->root.u.def.section->flags |= SEC_KEEP;
13259 }
13260}
13261
2f0c68f2
CM
13262bfd_boolean
13263bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13264 struct bfd_link_info *info)
13265{
13266 bfd *ibfd = info->input_bfds;
13267
13268 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13269 {
13270 asection *sec;
13271 struct elf_reloc_cookie cookie;
13272
13273 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13274 continue;
57963c05
AM
13275 sec = ibfd->sections;
13276 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13277 continue;
2f0c68f2
CM
13278
13279 if (!init_reloc_cookie (&cookie, info, ibfd))
13280 return FALSE;
13281
13282 for (sec = ibfd->sections; sec; sec = sec->next)
13283 {
13284 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13285 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13286 {
13287 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13288 fini_reloc_cookie_rels (&cookie, sec);
13289 }
13290 }
13291 }
13292 return TRUE;
13293}
13294
c152c796
AM
13295/* Do mark and sweep of unused sections. */
13296
13297bfd_boolean
13298bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13299{
13300 bfd_boolean ok = TRUE;
13301 bfd *sub;
6a5bb875 13302 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13303 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13304 struct elf_link_hash_table *htab;
c152c796 13305
64d03ab5 13306 if (!bed->can_gc_sections
715df9b8 13307 || !is_elf_hash_table (info->hash))
c152c796 13308 {
4eca0228 13309 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13310 return TRUE;
13311 }
13312
74f0fb50 13313 bed->gc_keep (info);
da44f4e5 13314 htab = elf_hash_table (info);
74f0fb50 13315
9d0a14d3
RS
13316 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13317 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13318 for (sub = info->input_bfds;
13319 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13320 sub = sub->link.next)
9d0a14d3
RS
13321 {
13322 asection *sec;
13323 struct elf_reloc_cookie cookie;
13324
57963c05
AM
13325 sec = sub->sections;
13326 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13327 continue;
9d0a14d3 13328 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13329 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13330 {
13331 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13332 if (elf_section_data (sec)->sec_info
13333 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13334 elf_eh_frame_section (sub) = sec;
13335 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13336 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13337 }
13338 }
9d0a14d3 13339
c152c796 13340 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13341 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13342 if (!ok)
13343 return FALSE;
13344
13345 /* Kill the vtable relocations that were not used. */
da44f4e5 13346 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13347 if (!ok)
13348 return FALSE;
13349
715df9b8 13350 /* Mark dynamically referenced symbols. */
22185505 13351 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13352 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13353
715df9b8 13354 /* Grovel through relocs to find out who stays ... */
64d03ab5 13355 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13356 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13357 {
13358 asection *o;
13359
b19a8f85
L
13360 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13361 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13362 continue;
13363
57963c05
AM
13364 o = sub->sections;
13365 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13366 continue;
13367
7f6ab9f8
AM
13368 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13369 Also treat note sections as a root, if the section is not part
13370 of a group. */
c152c796 13371 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13372 if (!o->gc_mark
13373 && (o->flags & SEC_EXCLUDE) == 0
24007750 13374 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13375 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13376 && elf_next_in_group (o) == NULL )))
13377 {
13378 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13379 return FALSE;
13380 }
c152c796
AM
13381 }
13382
6a5bb875 13383 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13384 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13385
c152c796 13386 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13387 return elf_gc_sweep (abfd, info);
c152c796
AM
13388}
13389\f
13390/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13391
13392bfd_boolean
13393bfd_elf_gc_record_vtinherit (bfd *abfd,
13394 asection *sec,
13395 struct elf_link_hash_entry *h,
13396 bfd_vma offset)
13397{
13398 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13399 struct elf_link_hash_entry **search, *child;
ef53be89 13400 size_t extsymcount;
c152c796
AM
13401 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13402
13403 /* The sh_info field of the symtab header tells us where the
13404 external symbols start. We don't care about the local symbols at
13405 this point. */
13406 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13407 if (!elf_bad_symtab (abfd))
13408 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13409
13410 sym_hashes = elf_sym_hashes (abfd);
13411 sym_hashes_end = sym_hashes + extsymcount;
13412
13413 /* Hunt down the child symbol, which is in this section at the same
13414 offset as the relocation. */
13415 for (search = sym_hashes; search != sym_hashes_end; ++search)
13416 {
13417 if ((child = *search) != NULL
13418 && (child->root.type == bfd_link_hash_defined
13419 || child->root.type == bfd_link_hash_defweak)
13420 && child->root.u.def.section == sec
13421 && child->root.u.def.value == offset)
13422 goto win;
13423 }
13424
695344c0 13425 /* xgettext:c-format */
76cfced5
AM
13426 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13427 abfd, sec, offset);
c152c796
AM
13428 bfd_set_error (bfd_error_invalid_operation);
13429 return FALSE;
13430
13431 win:
cbd0eecf 13432 if (!child->u2.vtable)
f6e332e6 13433 {
cbd0eecf
L
13434 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13435 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13436 if (!child->u2.vtable)
f6e332e6
AM
13437 return FALSE;
13438 }
c152c796
AM
13439 if (!h)
13440 {
13441 /* This *should* only be the absolute section. It could potentially
13442 be that someone has defined a non-global vtable though, which
13443 would be bad. It isn't worth paging in the local symbols to be
13444 sure though; that case should simply be handled by the assembler. */
13445
cbd0eecf 13446 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13447 }
13448 else
cbd0eecf 13449 child->u2.vtable->parent = h;
c152c796
AM
13450
13451 return TRUE;
13452}
13453
13454/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13455
13456bfd_boolean
13457bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13458 asection *sec ATTRIBUTE_UNUSED,
13459 struct elf_link_hash_entry *h,
13460 bfd_vma addend)
13461{
13462 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13463 unsigned int log_file_align = bed->s->log_file_align;
13464
cbd0eecf 13465 if (!h->u2.vtable)
f6e332e6 13466 {
cbd0eecf
L
13467 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13468 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13469 if (!h->u2.vtable)
f6e332e6
AM
13470 return FALSE;
13471 }
13472
cbd0eecf 13473 if (addend >= h->u2.vtable->size)
c152c796
AM
13474 {
13475 size_t size, bytes, file_align;
cbd0eecf 13476 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13477
13478 /* While the symbol is undefined, we have to be prepared to handle
13479 a zero size. */
13480 file_align = 1 << log_file_align;
13481 if (h->root.type == bfd_link_hash_undefined)
13482 size = addend + file_align;
13483 else
13484 {
13485 size = h->size;
13486 if (addend >= size)
13487 {
13488 /* Oops! We've got a reference past the defined end of
13489 the table. This is probably a bug -- shall we warn? */
13490 size = addend + file_align;
13491 }
13492 }
13493 size = (size + file_align - 1) & -file_align;
13494
13495 /* Allocate one extra entry for use as a "done" flag for the
13496 consolidation pass. */
13497 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13498
13499 if (ptr)
13500 {
a50b1753 13501 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13502
13503 if (ptr != NULL)
13504 {
13505 size_t oldbytes;
13506
cbd0eecf 13507 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13508 * sizeof (bfd_boolean));
13509 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13510 }
13511 }
13512 else
a50b1753 13513 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13514
13515 if (ptr == NULL)
13516 return FALSE;
13517
13518 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13519 h->u2.vtable->used = ptr + 1;
13520 h->u2.vtable->size = size;
c152c796
AM
13521 }
13522
cbd0eecf 13523 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13524
13525 return TRUE;
13526}
13527
ae17ab41
CM
13528/* Map an ELF section header flag to its corresponding string. */
13529typedef struct
13530{
13531 char *flag_name;
13532 flagword flag_value;
13533} elf_flags_to_name_table;
13534
13535static elf_flags_to_name_table elf_flags_to_names [] =
13536{
13537 { "SHF_WRITE", SHF_WRITE },
13538 { "SHF_ALLOC", SHF_ALLOC },
13539 { "SHF_EXECINSTR", SHF_EXECINSTR },
13540 { "SHF_MERGE", SHF_MERGE },
13541 { "SHF_STRINGS", SHF_STRINGS },
13542 { "SHF_INFO_LINK", SHF_INFO_LINK},
13543 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13544 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13545 { "SHF_GROUP", SHF_GROUP },
13546 { "SHF_TLS", SHF_TLS },
13547 { "SHF_MASKOS", SHF_MASKOS },
13548 { "SHF_EXCLUDE", SHF_EXCLUDE },
13549};
13550
b9c361e0
JL
13551/* Returns TRUE if the section is to be included, otherwise FALSE. */
13552bfd_boolean
ae17ab41 13553bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13554 struct flag_info *flaginfo,
b9c361e0 13555 asection *section)
ae17ab41 13556{
8b127cbc 13557 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13558
8b127cbc 13559 if (!flaginfo->flags_initialized)
ae17ab41 13560 {
8b127cbc
AM
13561 bfd *obfd = info->output_bfd;
13562 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13563 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13564 int with_hex = 0;
13565 int without_hex = 0;
13566
8b127cbc 13567 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13568 {
b9c361e0 13569 unsigned i;
8b127cbc 13570 flagword (*lookup) (char *);
ae17ab41 13571
8b127cbc
AM
13572 lookup = bed->elf_backend_lookup_section_flags_hook;
13573 if (lookup != NULL)
ae17ab41 13574 {
8b127cbc 13575 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13576
13577 if (hexval != 0)
13578 {
13579 if (tf->with == with_flags)
13580 with_hex |= hexval;
13581 else if (tf->with == without_flags)
13582 without_hex |= hexval;
13583 tf->valid = TRUE;
13584 continue;
13585 }
ae17ab41 13586 }
8b127cbc 13587 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13588 {
8b127cbc 13589 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13590 {
13591 if (tf->with == with_flags)
13592 with_hex |= elf_flags_to_names[i].flag_value;
13593 else if (tf->with == without_flags)
13594 without_hex |= elf_flags_to_names[i].flag_value;
13595 tf->valid = TRUE;
13596 break;
13597 }
13598 }
8b127cbc 13599 if (!tf->valid)
b9c361e0 13600 {
68ffbac6 13601 info->callbacks->einfo
8b127cbc 13602 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13603 return FALSE;
ae17ab41
CM
13604 }
13605 }
8b127cbc
AM
13606 flaginfo->flags_initialized = TRUE;
13607 flaginfo->only_with_flags |= with_hex;
13608 flaginfo->not_with_flags |= without_hex;
ae17ab41 13609 }
ae17ab41 13610
8b127cbc 13611 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13612 return FALSE;
13613
8b127cbc 13614 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13615 return FALSE;
13616
13617 return TRUE;
ae17ab41
CM
13618}
13619
c152c796
AM
13620struct alloc_got_off_arg {
13621 bfd_vma gotoff;
10455f89 13622 struct bfd_link_info *info;
c152c796
AM
13623};
13624
13625/* We need a special top-level link routine to convert got reference counts
13626 to real got offsets. */
13627
13628static bfd_boolean
13629elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13630{
a50b1753 13631 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13632 bfd *obfd = gofarg->info->output_bfd;
13633 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13634
c152c796
AM
13635 if (h->got.refcount > 0)
13636 {
13637 h->got.offset = gofarg->gotoff;
10455f89 13638 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13639 }
13640 else
13641 h->got.offset = (bfd_vma) -1;
13642
13643 return TRUE;
13644}
13645
13646/* And an accompanying bit to work out final got entry offsets once
13647 we're done. Should be called from final_link. */
13648
13649bfd_boolean
13650bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13651 struct bfd_link_info *info)
13652{
13653 bfd *i;
13654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13655 bfd_vma gotoff;
c152c796
AM
13656 struct alloc_got_off_arg gofarg;
13657
10455f89
HPN
13658 BFD_ASSERT (abfd == info->output_bfd);
13659
c152c796
AM
13660 if (! is_elf_hash_table (info->hash))
13661 return FALSE;
13662
13663 /* The GOT offset is relative to the .got section, but the GOT header is
13664 put into the .got.plt section, if the backend uses it. */
13665 if (bed->want_got_plt)
13666 gotoff = 0;
13667 else
13668 gotoff = bed->got_header_size;
13669
13670 /* Do the local .got entries first. */
c72f2fb2 13671 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13672 {
13673 bfd_signed_vma *local_got;
ef53be89 13674 size_t j, locsymcount;
c152c796
AM
13675 Elf_Internal_Shdr *symtab_hdr;
13676
13677 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13678 continue;
13679
13680 local_got = elf_local_got_refcounts (i);
13681 if (!local_got)
13682 continue;
13683
13684 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13685 if (elf_bad_symtab (i))
13686 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13687 else
13688 locsymcount = symtab_hdr->sh_info;
13689
13690 for (j = 0; j < locsymcount; ++j)
13691 {
13692 if (local_got[j] > 0)
13693 {
13694 local_got[j] = gotoff;
10455f89 13695 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13696 }
13697 else
13698 local_got[j] = (bfd_vma) -1;
13699 }
13700 }
13701
13702 /* Then the global .got entries. .plt refcounts are handled by
13703 adjust_dynamic_symbol */
13704 gofarg.gotoff = gotoff;
10455f89 13705 gofarg.info = info;
c152c796
AM
13706 elf_link_hash_traverse (elf_hash_table (info),
13707 elf_gc_allocate_got_offsets,
13708 &gofarg);
13709 return TRUE;
13710}
13711
13712/* Many folk need no more in the way of final link than this, once
13713 got entry reference counting is enabled. */
13714
13715bfd_boolean
13716bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13717{
13718 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13719 return FALSE;
13720
13721 /* Invoke the regular ELF backend linker to do all the work. */
13722 return bfd_elf_final_link (abfd, info);
13723}
13724
13725bfd_boolean
13726bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13727{
a50b1753 13728 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13729
13730 if (rcookie->bad_symtab)
13731 rcookie->rel = rcookie->rels;
13732
13733 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13734 {
13735 unsigned long r_symndx;
13736
13737 if (! rcookie->bad_symtab)
13738 if (rcookie->rel->r_offset > offset)
13739 return FALSE;
13740 if (rcookie->rel->r_offset != offset)
13741 continue;
13742
13743 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13744 if (r_symndx == STN_UNDEF)
c152c796
AM
13745 return TRUE;
13746
13747 if (r_symndx >= rcookie->locsymcount
13748 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13749 {
13750 struct elf_link_hash_entry *h;
13751
13752 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13753
13754 while (h->root.type == bfd_link_hash_indirect
13755 || h->root.type == bfd_link_hash_warning)
13756 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13757
13758 if ((h->root.type == bfd_link_hash_defined
13759 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13760 && (h->root.u.def.section->owner != rcookie->abfd
13761 || h->root.u.def.section->kept_section != NULL
13762 || discarded_section (h->root.u.def.section)))
c152c796 13763 return TRUE;
c152c796
AM
13764 }
13765 else
13766 {
13767 /* It's not a relocation against a global symbol,
13768 but it could be a relocation against a local
13769 symbol for a discarded section. */
13770 asection *isec;
13771 Elf_Internal_Sym *isym;
13772
13773 /* Need to: get the symbol; get the section. */
13774 isym = &rcookie->locsyms[r_symndx];
cb33740c 13775 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13776 if (isec != NULL
13777 && (isec->kept_section != NULL
13778 || discarded_section (isec)))
cb33740c 13779 return TRUE;
c152c796
AM
13780 }
13781 return FALSE;
13782 }
13783 return FALSE;
13784}
13785
13786/* Discard unneeded references to discarded sections.
75938853
AM
13787 Returns -1 on error, 1 if any section's size was changed, 0 if
13788 nothing changed. This function assumes that the relocations are in
13789 sorted order, which is true for all known assemblers. */
c152c796 13790
75938853 13791int
c152c796
AM
13792bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13793{
13794 struct elf_reloc_cookie cookie;
18cd5bce 13795 asection *o;
c152c796 13796 bfd *abfd;
75938853 13797 int changed = 0;
c152c796
AM
13798
13799 if (info->traditional_format
13800 || !is_elf_hash_table (info->hash))
75938853 13801 return 0;
c152c796 13802
18cd5bce
AM
13803 o = bfd_get_section_by_name (output_bfd, ".stab");
13804 if (o != NULL)
c152c796 13805 {
18cd5bce 13806 asection *i;
c152c796 13807
18cd5bce 13808 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13809 {
18cd5bce
AM
13810 if (i->size == 0
13811 || i->reloc_count == 0
13812 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13813 continue;
c152c796 13814
18cd5bce
AM
13815 abfd = i->owner;
13816 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13817 continue;
c152c796 13818
18cd5bce 13819 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13820 return -1;
c152c796 13821
18cd5bce
AM
13822 if (_bfd_discard_section_stabs (abfd, i,
13823 elf_section_data (i)->sec_info,
5241d853
RS
13824 bfd_elf_reloc_symbol_deleted_p,
13825 &cookie))
75938853 13826 changed = 1;
18cd5bce
AM
13827
13828 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13829 }
18cd5bce
AM
13830 }
13831
2f0c68f2
CM
13832 o = NULL;
13833 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13834 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13835 if (o != NULL)
13836 {
13837 asection *i;
d7153c4a 13838 int eh_changed = 0;
79a94a2a 13839 unsigned int eh_alignment;
c152c796 13840
18cd5bce 13841 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13842 {
18cd5bce
AM
13843 if (i->size == 0)
13844 continue;
13845
13846 abfd = i->owner;
13847 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13848 continue;
13849
13850 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13851 return -1;
18cd5bce
AM
13852
13853 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13854 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13855 bfd_elf_reloc_symbol_deleted_p,
13856 &cookie))
d7153c4a
AM
13857 {
13858 eh_changed = 1;
13859 if (i->size != i->rawsize)
13860 changed = 1;
13861 }
18cd5bce
AM
13862
13863 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13864 }
79a94a2a
AM
13865 eh_alignment = 1 << o->alignment_power;
13866 if (eh_alignment > 4)
13867 {
13868 /* Skip over zero terminator, and prevent empty sections
13869 from adding alignment padding at the end. */
13870 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13871 if (i->size == 0)
13872 i->flags |= SEC_EXCLUDE;
13873 else if (i->size > 4)
13874 break;
13875 /* The last non-empty eh_frame section doesn't need padding. */
13876 if (i != NULL)
13877 i = i->map_tail.s;
13878 /* Any prior sections must pad the last FDE out to the
13879 output section alignment. Otherwise we might have zero
13880 padding between sections, which would be seen as a
13881 terminator. */
13882 for (; i != NULL; i = i->map_tail.s)
13883 {
13884 bfd_size_type size = (i->size + eh_alignment - 1) & -eh_alignment;
13885 if (i->size != size)
13886 {
13887 i->size = size;
13888 changed = 1;
13889 eh_changed = 1;
13890 }
13891 }
13892 }
d7153c4a
AM
13893 if (eh_changed)
13894 elf_link_hash_traverse (elf_hash_table (info),
13895 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13896 }
c152c796 13897
18cd5bce
AM
13898 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13899 {
13900 const struct elf_backend_data *bed;
57963c05 13901 asection *s;
c152c796 13902
18cd5bce
AM
13903 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13904 continue;
57963c05
AM
13905 s = abfd->sections;
13906 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13907 continue;
18cd5bce
AM
13908
13909 bed = get_elf_backend_data (abfd);
13910
13911 if (bed->elf_backend_discard_info != NULL)
13912 {
13913 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13914 return -1;
18cd5bce
AM
13915
13916 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13917 changed = 1;
18cd5bce
AM
13918
13919 fini_reloc_cookie (&cookie, abfd);
13920 }
c152c796
AM
13921 }
13922
2f0c68f2
CM
13923 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13924 _bfd_elf_end_eh_frame_parsing (info);
13925
13926 if (info->eh_frame_hdr_type
0e1862bb 13927 && !bfd_link_relocatable (info)
c152c796 13928 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13929 changed = 1;
c152c796 13930
75938853 13931 return changed;
c152c796 13932}
082b7297 13933
43e1669b 13934bfd_boolean
0c511000 13935_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13936 asection *sec,
c0f00686 13937 struct bfd_link_info *info)
082b7297
L
13938{
13939 flagword flags;
c77ec726 13940 const char *name, *key;
082b7297
L
13941 struct bfd_section_already_linked *l;
13942 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13943
c77ec726
AM
13944 if (sec->output_section == bfd_abs_section_ptr)
13945 return FALSE;
0c511000 13946
c77ec726 13947 flags = sec->flags;
0c511000 13948
c77ec726
AM
13949 /* Return if it isn't a linkonce section. A comdat group section
13950 also has SEC_LINK_ONCE set. */
13951 if ((flags & SEC_LINK_ONCE) == 0)
13952 return FALSE;
0c511000 13953
c77ec726
AM
13954 /* Don't put group member sections on our list of already linked
13955 sections. They are handled as a group via their group section. */
13956 if (elf_sec_group (sec) != NULL)
13957 return FALSE;
0c511000 13958
c77ec726
AM
13959 /* For a SHT_GROUP section, use the group signature as the key. */
13960 name = sec->name;
13961 if ((flags & SEC_GROUP) != 0
13962 && elf_next_in_group (sec) != NULL
13963 && elf_group_name (elf_next_in_group (sec)) != NULL)
13964 key = elf_group_name (elf_next_in_group (sec));
13965 else
13966 {
13967 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13968 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13969 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13970 key++;
0c511000 13971 else
c77ec726
AM
13972 /* Must be a user linkonce section that doesn't follow gcc's
13973 naming convention. In this case we won't be matching
13974 single member groups. */
13975 key = name;
0c511000 13976 }
6d2cd210 13977
c77ec726 13978 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13979
13980 for (l = already_linked_list->entry; l != NULL; l = l->next)
13981 {
c2370991 13982 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13983 sections with a signature of <key> (<key> is some string),
13984 and linkonce sections named .gnu.linkonce.<type>.<key>.
13985 Match like sections. LTO plugin sections are an exception.
13986 They are always named .gnu.linkonce.t.<key> and match either
13987 type of section. */
13988 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13989 && ((flags & SEC_GROUP) != 0
13990 || strcmp (name, l->sec->name) == 0))
13991 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13992 {
13993 /* The section has already been linked. See if we should
6d2cd210 13994 issue a warning. */
c77ec726
AM
13995 if (!_bfd_handle_already_linked (sec, l, info))
13996 return FALSE;
082b7297 13997
c77ec726 13998 if (flags & SEC_GROUP)
3d7f7666 13999 {
c77ec726
AM
14000 asection *first = elf_next_in_group (sec);
14001 asection *s = first;
3d7f7666 14002
c77ec726 14003 while (s != NULL)
3d7f7666 14004 {
c77ec726
AM
14005 s->output_section = bfd_abs_section_ptr;
14006 /* Record which group discards it. */
14007 s->kept_section = l->sec;
14008 s = elf_next_in_group (s);
14009 /* These lists are circular. */
14010 if (s == first)
14011 break;
3d7f7666
L
14012 }
14013 }
082b7297 14014
43e1669b 14015 return TRUE;
082b7297
L
14016 }
14017 }
14018
c77ec726
AM
14019 /* A single member comdat group section may be discarded by a
14020 linkonce section and vice versa. */
14021 if ((flags & SEC_GROUP) != 0)
3d7f7666 14022 {
c77ec726 14023 asection *first = elf_next_in_group (sec);
c2370991 14024
c77ec726
AM
14025 if (first != NULL && elf_next_in_group (first) == first)
14026 /* Check this single member group against linkonce sections. */
14027 for (l = already_linked_list->entry; l != NULL; l = l->next)
14028 if ((l->sec->flags & SEC_GROUP) == 0
14029 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14030 {
14031 first->output_section = bfd_abs_section_ptr;
14032 first->kept_section = l->sec;
14033 sec->output_section = bfd_abs_section_ptr;
14034 break;
14035 }
14036 }
14037 else
14038 /* Check this linkonce section against single member groups. */
14039 for (l = already_linked_list->entry; l != NULL; l = l->next)
14040 if (l->sec->flags & SEC_GROUP)
6d2cd210 14041 {
c77ec726 14042 asection *first = elf_next_in_group (l->sec);
6d2cd210 14043
c77ec726
AM
14044 if (first != NULL
14045 && elf_next_in_group (first) == first
14046 && bfd_elf_match_symbols_in_sections (first, sec, info))
14047 {
14048 sec->output_section = bfd_abs_section_ptr;
14049 sec->kept_section = first;
14050 break;
14051 }
6d2cd210 14052 }
0c511000 14053
c77ec726
AM
14054 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14055 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14056 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14057 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14058 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14059 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14060 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14061 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14062 The reverse order cannot happen as there is never a bfd with only the
14063 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14064 matter as here were are looking only for cross-bfd sections. */
14065
14066 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14067 for (l = already_linked_list->entry; l != NULL; l = l->next)
14068 if ((l->sec->flags & SEC_GROUP) == 0
14069 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14070 {
14071 if (abfd != l->sec->owner)
14072 sec->output_section = bfd_abs_section_ptr;
14073 break;
14074 }
80c29487 14075
082b7297 14076 /* This is the first section with this name. Record it. */
c77ec726 14077 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14078 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14079 return sec->output_section == bfd_abs_section_ptr;
082b7297 14080}
81e1b023 14081
a4d8e49b
L
14082bfd_boolean
14083_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14084{
14085 return sym->st_shndx == SHN_COMMON;
14086}
14087
14088unsigned int
14089_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14090{
14091 return SHN_COMMON;
14092}
14093
14094asection *
14095_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14096{
14097 return bfd_com_section_ptr;
14098}
10455f89
HPN
14099
14100bfd_vma
14101_bfd_elf_default_got_elt_size (bfd *abfd,
14102 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14103 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14104 bfd *ibfd ATTRIBUTE_UNUSED,
14105 unsigned long symndx ATTRIBUTE_UNUSED)
14106{
14107 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14108 return bed->s->arch_size / 8;
14109}
83bac4b0
NC
14110
14111/* Routines to support the creation of dynamic relocs. */
14112
83bac4b0
NC
14113/* Returns the name of the dynamic reloc section associated with SEC. */
14114
14115static const char *
14116get_dynamic_reloc_section_name (bfd * abfd,
14117 asection * sec,
14118 bfd_boolean is_rela)
14119{
ddcf1fcf
BS
14120 char *name;
14121 const char *old_name = bfd_get_section_name (NULL, sec);
14122 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14123
ddcf1fcf 14124 if (old_name == NULL)
83bac4b0
NC
14125 return NULL;
14126
ddcf1fcf 14127 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14128 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14129
14130 return name;
14131}
14132
14133/* Returns the dynamic reloc section associated with SEC.
14134 If necessary compute the name of the dynamic reloc section based
14135 on SEC's name (looked up in ABFD's string table) and the setting
14136 of IS_RELA. */
14137
14138asection *
14139_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14140 asection * sec,
14141 bfd_boolean is_rela)
14142{
14143 asection * reloc_sec = elf_section_data (sec)->sreloc;
14144
14145 if (reloc_sec == NULL)
14146 {
14147 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14148
14149 if (name != NULL)
14150 {
3d4d4302 14151 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14152
14153 if (reloc_sec != NULL)
14154 elf_section_data (sec)->sreloc = reloc_sec;
14155 }
14156 }
14157
14158 return reloc_sec;
14159}
14160
14161/* Returns the dynamic reloc section associated with SEC. If the
14162 section does not exist it is created and attached to the DYNOBJ
14163 bfd and stored in the SRELOC field of SEC's elf_section_data
14164 structure.
f8076f98 14165
83bac4b0
NC
14166 ALIGNMENT is the alignment for the newly created section and
14167 IS_RELA defines whether the name should be .rela.<SEC's name>
14168 or .rel.<SEC's name>. The section name is looked up in the
14169 string table associated with ABFD. */
14170
14171asection *
ca4be51c
AM
14172_bfd_elf_make_dynamic_reloc_section (asection *sec,
14173 bfd *dynobj,
14174 unsigned int alignment,
14175 bfd *abfd,
14176 bfd_boolean is_rela)
83bac4b0
NC
14177{
14178 asection * reloc_sec = elf_section_data (sec)->sreloc;
14179
14180 if (reloc_sec == NULL)
14181 {
14182 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14183
14184 if (name == NULL)
14185 return NULL;
14186
3d4d4302 14187 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14188
14189 if (reloc_sec == NULL)
14190 {
3d4d4302
AM
14191 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14192 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14193 if ((sec->flags & SEC_ALLOC) != 0)
14194 flags |= SEC_ALLOC | SEC_LOAD;
14195
3d4d4302 14196 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14197 if (reloc_sec != NULL)
14198 {
8877b5e5
AM
14199 /* _bfd_elf_get_sec_type_attr chooses a section type by
14200 name. Override as it may be wrong, eg. for a user
14201 section named "auto" we'll get ".relauto" which is
14202 seen to be a .rela section. */
14203 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14204 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14205 reloc_sec = NULL;
14206 }
14207 }
14208
14209 elf_section_data (sec)->sreloc = reloc_sec;
14210 }
14211
14212 return reloc_sec;
14213}
1338dd10 14214
bffebb6b
AM
14215/* Copy the ELF symbol type and other attributes for a linker script
14216 assignment from HSRC to HDEST. Generally this should be treated as
14217 if we found a strong non-dynamic definition for HDEST (except that
14218 ld ignores multiple definition errors). */
1338dd10 14219void
bffebb6b
AM
14220_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14221 struct bfd_link_hash_entry *hdest,
14222 struct bfd_link_hash_entry *hsrc)
1338dd10 14223{
bffebb6b
AM
14224 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14225 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14226 Elf_Internal_Sym isym;
1338dd10
PB
14227
14228 ehdest->type = ehsrc->type;
35fc36a8 14229 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14230
14231 isym.st_other = ehsrc->other;
b8417128 14232 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14233}
351f65ca
L
14234
14235/* Append a RELA relocation REL to section S in BFD. */
14236
14237void
14238elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14239{
14240 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14241 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14242 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14243 bed->s->swap_reloca_out (abfd, rel, loc);
14244}
14245
14246/* Append a REL relocation REL to section S in BFD. */
14247
14248void
14249elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14250{
14251 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14252 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14253 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14254 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14255}
7dba9362
AM
14256
14257/* Define __start, __stop, .startof. or .sizeof. symbol. */
14258
14259struct bfd_link_hash_entry *
14260bfd_elf_define_start_stop (struct bfd_link_info *info,
14261 const char *symbol, asection *sec)
14262{
487b6440 14263 struct elf_link_hash_entry *h;
7dba9362 14264
487b6440
AM
14265 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14266 FALSE, FALSE, TRUE);
14267 if (h != NULL
14268 && (h->root.type == bfd_link_hash_undefined
14269 || h->root.type == bfd_link_hash_undefweak
14270 || (h->ref_regular && !h->def_regular)))
7dba9362 14271 {
487b6440
AM
14272 h->root.type = bfd_link_hash_defined;
14273 h->root.u.def.section = sec;
14274 h->root.u.def.value = 0;
14275 h->def_regular = 1;
14276 h->def_dynamic = 0;
14277 h->start_stop = 1;
14278 h->u2.start_stop_section = sec;
14279 if (symbol[0] == '.')
14280 {
14281 /* .startof. and .sizeof. symbols are local. */
14282 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
14283 }
14284 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14285 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14286 return &h->root;
7dba9362 14287 }
487b6440 14288 return NULL;
7dba9362 14289}