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