]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
[AArch64] Use SYMBOL_REFERENCES_LOCAL in one symbol check
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
222 if ((ibfd->flags
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
224 {
225 abfd = ibfd;
226 break;
227 }
228 }
229 hash_table->dynobj = abfd;
230 }
7e9f0867
AM
231
232 if (hash_table->dynstr == NULL)
233 {
234 hash_table->dynstr = _bfd_elf_strtab_init ();
235 if (hash_table->dynstr == NULL)
236 return FALSE;
237 }
238 return TRUE;
239}
240
45d6a902
AM
241/* Create some sections which will be filled in with dynamic linking
242 information. ABFD is an input file which requires dynamic sections
243 to be created. The dynamic sections take up virtual memory space
244 when the final executable is run, so we need to create them before
245 addresses are assigned to the output sections. We work out the
246 actual contents and size of these sections later. */
252b5132 247
b34976b6 248bfd_boolean
268b6b39 249_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 250{
45d6a902 251 flagword flags;
91d6fa6a 252 asection *s;
9c5bfbb7 253 const struct elf_backend_data *bed;
9637f6ef 254 struct elf_link_hash_entry *h;
252b5132 255
0eddce27 256 if (! is_elf_hash_table (info->hash))
45d6a902
AM
257 return FALSE;
258
259 if (elf_hash_table (info)->dynamic_sections_created)
260 return TRUE;
261
7e9f0867
AM
262 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
263 return FALSE;
45d6a902 264
7e9f0867 265 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
266 bed = get_elf_backend_data (abfd);
267
268 flags = bed->dynamic_sec_flags;
45d6a902
AM
269
270 /* A dynamically linked executable has a .interp section, but a
271 shared library does not. */
9b8b325a 272 if (bfd_link_executable (info) && !info->nointerp)
252b5132 273 {
14b2f831
AM
274 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
275 flags | SEC_READONLY);
3496cb2a 276 if (s == NULL)
45d6a902
AM
277 return FALSE;
278 }
bb0deeff 279
45d6a902
AM
280 /* Create sections to hold version informations. These are removed
281 if they are not needed. */
14b2f831
AM
282 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
283 flags | SEC_READONLY);
45d6a902 284 if (s == NULL
45d6a902
AM
285 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
286 return FALSE;
287
14b2f831
AM
288 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
289 flags | SEC_READONLY);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, 1))
292 return FALSE;
293
14b2f831
AM
294 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
295 flags | SEC_READONLY);
45d6a902 296 if (s == NULL
45d6a902
AM
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 return FALSE;
299
14b2f831
AM
300 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
301 flags | SEC_READONLY);
45d6a902 302 if (s == NULL
45d6a902
AM
303 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 return FALSE;
cae1fbbb 305 elf_hash_table (info)->dynsym = s;
45d6a902 306
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
308 flags | SEC_READONLY);
3496cb2a 309 if (s == NULL)
45d6a902
AM
310 return FALSE;
311
14b2f831 312 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 313 if (s == NULL
45d6a902
AM
314 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
315 return FALSE;
316
317 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
318 .dynamic section. We could set _DYNAMIC in a linker script, but we
319 only want to define it if we are, in fact, creating a .dynamic
320 section. We don't want to define it if there is no .dynamic
321 section, since on some ELF platforms the start up code examines it
322 to decide how to initialize the process. */
9637f6ef
L
323 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
324 elf_hash_table (info)->hdynamic = h;
325 if (h == NULL)
45d6a902
AM
326 return FALSE;
327
fdc90cb4
JJ
328 if (info->emit_hash)
329 {
14b2f831
AM
330 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
331 flags | SEC_READONLY);
fdc90cb4
JJ
332 if (s == NULL
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
335 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
336 }
337
338 if (info->emit_gnu_hash)
339 {
14b2f831
AM
340 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
341 flags | SEC_READONLY);
fdc90cb4
JJ
342 if (s == NULL
343 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
344 return FALSE;
345 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
346 4 32-bit words followed by variable count of 64-bit words, then
347 variable count of 32-bit words. */
348 if (bed->s->arch_size == 64)
349 elf_section_data (s)->this_hdr.sh_entsize = 0;
350 else
351 elf_section_data (s)->this_hdr.sh_entsize = 4;
352 }
45d6a902
AM
353
354 /* Let the backend create the rest of the sections. This lets the
355 backend set the right flags. The backend will normally create
356 the .got and .plt sections. */
894891db
NC
357 if (bed->elf_backend_create_dynamic_sections == NULL
358 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
359 return FALSE;
360
361 elf_hash_table (info)->dynamic_sections_created = TRUE;
362
363 return TRUE;
364}
365
366/* Create dynamic sections when linking against a dynamic object. */
367
368bfd_boolean
268b6b39 369_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
370{
371 flagword flags, pltflags;
7325306f 372 struct elf_link_hash_entry *h;
45d6a902 373 asection *s;
9c5bfbb7 374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 375 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 376
252b5132
RH
377 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
378 .rel[a].bss sections. */
e5a52504 379 flags = bed->dynamic_sec_flags;
252b5132
RH
380
381 pltflags = flags;
252b5132 382 if (bed->plt_not_loaded)
6df4d94c
MM
383 /* We do not clear SEC_ALLOC here because we still want the OS to
384 allocate space for the section; it's just that there's nothing
385 to read in from the object file. */
5d1634d7 386 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
387 else
388 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
389 if (bed->plt_readonly)
390 pltflags |= SEC_READONLY;
391
14b2f831 392 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 393 if (s == NULL
252b5132 394 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 395 return FALSE;
6de2ae4a 396 htab->splt = s;
252b5132 397
d98685ac
AM
398 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
399 .plt section. */
7325306f
RS
400 if (bed->want_plt_sym)
401 {
402 h = _bfd_elf_define_linkage_sym (abfd, info, s,
403 "_PROCEDURE_LINKAGE_TABLE_");
404 elf_hash_table (info)->hplt = h;
405 if (h == NULL)
406 return FALSE;
407 }
252b5132 408
14b2f831
AM
409 s = bfd_make_section_anyway_with_flags (abfd,
410 (bed->rela_plts_and_copies_p
411 ? ".rela.plt" : ".rel.plt"),
412 flags | SEC_READONLY);
252b5132 413 if (s == NULL
45d6a902 414 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 415 return FALSE;
6de2ae4a 416 htab->srelplt = s;
252b5132
RH
417
418 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 419 return FALSE;
252b5132 420
3018b441
RH
421 if (bed->want_dynbss)
422 {
423 /* The .dynbss section is a place to put symbols which are defined
424 by dynamic objects, are referenced by regular objects, and are
425 not functions. We must allocate space for them in the process
426 image and use a R_*_COPY reloc to tell the dynamic linker to
427 initialize them at run time. The linker script puts the .dynbss
428 section into the .bss section of the final image. */
14b2f831 429 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 430 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 431 if (s == NULL)
b34976b6 432 return FALSE;
9d19e4fd 433 htab->sdynbss = s;
252b5132 434
5474d94f
AM
435 if (bed->want_dynrelro)
436 {
437 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
438 sections. This section doesn't really need to have contents,
439 but make it like other .data.rel.ro sections. */
5474d94f 440 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 441 flags);
5474d94f
AM
442 if (s == NULL)
443 return FALSE;
444 htab->sdynrelro = s;
445 }
446
3018b441 447 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
448 normally needed. We need to create it here, though, so that the
449 linker will map it to an output section. We can't just create it
450 only if we need it, because we will not know whether we need it
451 until we have seen all the input files, and the first time the
452 main linker code calls BFD after examining all the input files
453 (size_dynamic_sections) the input sections have already been
454 mapped to the output sections. If the section turns out not to
455 be needed, we can discard it later. We will never need this
456 section when generating a shared object, since they do not use
457 copy relocs. */
9d19e4fd 458 if (bfd_link_executable (info))
3018b441 459 {
14b2f831
AM
460 s = bfd_make_section_anyway_with_flags (abfd,
461 (bed->rela_plts_and_copies_p
462 ? ".rela.bss" : ".rel.bss"),
463 flags | SEC_READONLY);
3018b441 464 if (s == NULL
45d6a902 465 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 466 return FALSE;
9d19e4fd 467 htab->srelbss = s;
5474d94f
AM
468
469 if (bed->want_dynrelro)
470 {
471 s = (bfd_make_section_anyway_with_flags
472 (abfd, (bed->rela_plts_and_copies_p
473 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
474 flags | SEC_READONLY));
475 if (s == NULL
476 || ! bfd_set_section_alignment (abfd, s,
477 bed->s->log_file_align))
478 return FALSE;
479 htab->sreldynrelro = s;
480 }
3018b441 481 }
252b5132
RH
482 }
483
b34976b6 484 return TRUE;
252b5132
RH
485}
486\f
252b5132
RH
487/* Record a new dynamic symbol. We record the dynamic symbols as we
488 read the input files, since we need to have a list of all of them
489 before we can determine the final sizes of the output sections.
490 Note that we may actually call this function even though we are not
491 going to output any dynamic symbols; in some cases we know that a
492 symbol should be in the dynamic symbol table, but only if there is
493 one. */
494
b34976b6 495bfd_boolean
c152c796
AM
496bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
497 struct elf_link_hash_entry *h)
252b5132
RH
498{
499 if (h->dynindx == -1)
500 {
2b0f7ef9 501 struct elf_strtab_hash *dynstr;
68b6ddd0 502 char *p;
252b5132 503 const char *name;
ef53be89 504 size_t indx;
252b5132 505
7a13edea
NC
506 /* XXX: The ABI draft says the linker must turn hidden and
507 internal symbols into STB_LOCAL symbols when producing the
508 DSO. However, if ld.so honors st_other in the dynamic table,
509 this would not be necessary. */
510 switch (ELF_ST_VISIBILITY (h->other))
511 {
512 case STV_INTERNAL:
513 case STV_HIDDEN:
9d6eee78
L
514 if (h->root.type != bfd_link_hash_undefined
515 && h->root.type != bfd_link_hash_undefweak)
38048eb9 516 {
f5385ebf 517 h->forced_local = 1;
67687978
PB
518 if (!elf_hash_table (info)->is_relocatable_executable)
519 return TRUE;
7a13edea 520 }
0444bdd4 521
7a13edea
NC
522 default:
523 break;
524 }
525
252b5132
RH
526 h->dynindx = elf_hash_table (info)->dynsymcount;
527 ++elf_hash_table (info)->dynsymcount;
528
529 dynstr = elf_hash_table (info)->dynstr;
530 if (dynstr == NULL)
531 {
532 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 533 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 534 if (dynstr == NULL)
b34976b6 535 return FALSE;
252b5132
RH
536 }
537
538 /* We don't put any version information in the dynamic string
aad5d350 539 table. */
252b5132
RH
540 name = h->root.root.string;
541 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
542 if (p != NULL)
543 /* We know that the p points into writable memory. In fact,
544 there are only a few symbols that have read-only names, being
545 those like _GLOBAL_OFFSET_TABLE_ that are created specially
546 by the backends. Most symbols will have names pointing into
547 an ELF string table read from a file, or to objalloc memory. */
548 *p = 0;
549
550 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
551
552 if (p != NULL)
553 *p = ELF_VER_CHR;
252b5132 554
ef53be89 555 if (indx == (size_t) -1)
b34976b6 556 return FALSE;
252b5132
RH
557 h->dynstr_index = indx;
558 }
559
b34976b6 560 return TRUE;
252b5132 561}
45d6a902 562\f
55255dae
L
563/* Mark a symbol dynamic. */
564
28caa186 565static void
55255dae 566bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
567 struct elf_link_hash_entry *h,
568 Elf_Internal_Sym *sym)
55255dae 569{
40b36307 570 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 571
40b36307 572 /* It may be called more than once on the same H. */
0e1862bb 573 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
574 return;
575
40b36307
L
576 if ((info->dynamic_data
577 && (h->type == STT_OBJECT
b8871f35 578 || h->type == STT_COMMON
40b36307 579 || (sym != NULL
b8871f35
L
580 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
581 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 582 || (d != NULL
73ec947d 583 && h->non_elf
40b36307 584 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
585 h->dynamic = 1;
586}
587
45d6a902
AM
588/* Record an assignment to a symbol made by a linker script. We need
589 this in case some dynamic object refers to this symbol. */
590
591bfd_boolean
fe21a8fc
L
592bfd_elf_record_link_assignment (bfd *output_bfd,
593 struct bfd_link_info *info,
268b6b39 594 const char *name,
fe21a8fc
L
595 bfd_boolean provide,
596 bfd_boolean hidden)
45d6a902 597{
00cbee0a 598 struct elf_link_hash_entry *h, *hv;
4ea42fb7 599 struct elf_link_hash_table *htab;
00cbee0a 600 const struct elf_backend_data *bed;
45d6a902 601
0eddce27 602 if (!is_elf_hash_table (info->hash))
45d6a902
AM
603 return TRUE;
604
4ea42fb7
AM
605 htab = elf_hash_table (info);
606 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 607 if (h == NULL)
4ea42fb7 608 return provide;
45d6a902 609
8e2a4f11
AM
610 if (h->root.type == bfd_link_hash_warning)
611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
612
0f550b3d
L
613 if (h->versioned == unknown)
614 {
615 /* Set versioned if symbol version is unknown. */
616 char *version = strrchr (name, ELF_VER_CHR);
617 if (version)
618 {
619 if (version > name && version[-1] != ELF_VER_CHR)
620 h->versioned = versioned_hidden;
621 else
622 h->versioned = versioned;
623 }
624 }
625
73ec947d
AM
626 /* Symbols defined in a linker script but not referenced anywhere
627 else will have non_elf set. */
628 if (h->non_elf)
629 {
630 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
631 h->non_elf = 0;
632 }
633
00cbee0a 634 switch (h->root.type)
77cfaee6 635 {
00cbee0a
L
636 case bfd_link_hash_defined:
637 case bfd_link_hash_defweak:
638 case bfd_link_hash_common:
639 break;
640 case bfd_link_hash_undefweak:
641 case bfd_link_hash_undefined:
642 /* Since we're defining the symbol, don't let it seem to have not
643 been defined. record_dynamic_symbol and size_dynamic_sections
644 may depend on this. */
4ea42fb7 645 h->root.type = bfd_link_hash_new;
77cfaee6
AM
646 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
647 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
648 break;
649 case bfd_link_hash_new:
00cbee0a
L
650 break;
651 case bfd_link_hash_indirect:
652 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 653 the versioned symbol point to this one. */
00cbee0a
L
654 bed = get_elf_backend_data (output_bfd);
655 hv = h;
656 while (hv->root.type == bfd_link_hash_indirect
657 || hv->root.type == bfd_link_hash_warning)
658 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
659 /* We don't need to update h->root.u since linker will set them
660 later. */
661 h->root.type = bfd_link_hash_undefined;
662 hv->root.type = bfd_link_hash_indirect;
663 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
664 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
665 break;
8e2a4f11
AM
666 default:
667 BFD_FAIL ();
c2596ca5 668 return FALSE;
55255dae 669 }
45d6a902
AM
670
671 /* If this symbol is being provided by the linker script, and it is
672 currently defined by a dynamic object, but not by a regular
673 object, then mark it as undefined so that the generic linker will
674 force the correct value. */
675 if (provide
f5385ebf
AM
676 && h->def_dynamic
677 && !h->def_regular)
45d6a902
AM
678 h->root.type = bfd_link_hash_undefined;
679
680 /* If this symbol is not being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
682 then clear out any version information because the symbol will not be
683 associated with the dynamic object any more. */
45d6a902 684 if (!provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
b531344c
MR
687 h->verinfo.verdef = NULL;
688
689 /* Make sure this symbol is not garbage collected. */
690 h->mark = 1;
45d6a902 691
f5385ebf 692 h->def_regular = 1;
45d6a902 693
eb8476a6 694 if (hidden)
fe21a8fc 695 {
91d6fa6a 696 bed = get_elf_backend_data (output_bfd);
b8297068
AM
697 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
698 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
699 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
700 }
701
6fa3860b
PB
702 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
703 and executables. */
0e1862bb 704 if (!bfd_link_relocatable (info)
6fa3860b
PB
705 && h->dynindx != -1
706 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
707 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
708 h->forced_local = 1;
709
f5385ebf
AM
710 if ((h->def_dynamic
711 || h->ref_dynamic
6b3b0ab8
L
712 || bfd_link_dll (info)
713 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
714 && h->dynindx == -1)
715 {
c152c796 716 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
717 return FALSE;
718
719 /* If this is a weak defined symbol, and we know a corresponding
720 real symbol from the same dynamic object, make sure the real
721 symbol is also made into a dynamic symbol. */
f6e332e6
AM
722 if (h->u.weakdef != NULL
723 && h->u.weakdef->dynindx == -1)
45d6a902 724 {
f6e332e6 725 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
726 return FALSE;
727 }
728 }
729
730 return TRUE;
731}
42751cf3 732
8c58d23b
AM
733/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
734 success, and 2 on a failure caused by attempting to record a symbol
735 in a discarded section, eg. a discarded link-once section symbol. */
736
737int
c152c796
AM
738bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
739 bfd *input_bfd,
740 long input_indx)
8c58d23b
AM
741{
742 bfd_size_type amt;
743 struct elf_link_local_dynamic_entry *entry;
744 struct elf_link_hash_table *eht;
745 struct elf_strtab_hash *dynstr;
ef53be89 746 size_t dynstr_index;
8c58d23b
AM
747 char *name;
748 Elf_External_Sym_Shndx eshndx;
749 char esym[sizeof (Elf64_External_Sym)];
750
0eddce27 751 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
752 return 0;
753
754 /* See if the entry exists already. */
755 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
756 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
757 return 1;
758
759 amt = sizeof (*entry);
a50b1753 760 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
761 if (entry == NULL)
762 return 0;
763
764 /* Go find the symbol, so that we can find it's name. */
765 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 766 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
767 {
768 bfd_release (input_bfd, entry);
769 return 0;
770 }
771
772 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 773 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
774 {
775 asection *s;
776
777 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
778 if (s == NULL || bfd_is_abs_section (s->output_section))
779 {
780 /* We can still bfd_release here as nothing has done another
781 bfd_alloc. We can't do this later in this function. */
782 bfd_release (input_bfd, entry);
783 return 2;
784 }
785 }
786
787 name = (bfd_elf_string_from_elf_section
788 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
789 entry->isym.st_name));
790
791 dynstr = elf_hash_table (info)->dynstr;
792 if (dynstr == NULL)
793 {
794 /* Create a strtab to hold the dynamic symbol names. */
795 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
796 if (dynstr == NULL)
797 return 0;
798 }
799
b34976b6 800 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 801 if (dynstr_index == (size_t) -1)
8c58d23b
AM
802 return 0;
803 entry->isym.st_name = dynstr_index;
804
805 eht = elf_hash_table (info);
806
807 entry->next = eht->dynlocal;
808 eht->dynlocal = entry;
809 entry->input_bfd = input_bfd;
810 entry->input_indx = input_indx;
811 eht->dynsymcount++;
812
813 /* Whatever binding the symbol had before, it's now local. */
814 entry->isym.st_info
815 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
816
817 /* The dynindx will be set at the end of size_dynamic_sections. */
818
819 return 1;
820}
821
30b30c21 822/* Return the dynindex of a local dynamic symbol. */
42751cf3 823
30b30c21 824long
268b6b39
AM
825_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
826 bfd *input_bfd,
827 long input_indx)
30b30c21
RH
828{
829 struct elf_link_local_dynamic_entry *e;
830
831 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
832 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
833 return e->dynindx;
834 return -1;
835}
836
837/* This function is used to renumber the dynamic symbols, if some of
838 them are removed because they are marked as local. This is called
839 via elf_link_hash_traverse. */
840
b34976b6 841static bfd_boolean
268b6b39
AM
842elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
843 void *data)
42751cf3 844{
a50b1753 845 size_t *count = (size_t *) data;
30b30c21 846
6fa3860b
PB
847 if (h->forced_local)
848 return TRUE;
849
850 if (h->dynindx != -1)
851 h->dynindx = ++(*count);
852
853 return TRUE;
854}
855
856
857/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
858 STB_LOCAL binding. */
859
860static bfd_boolean
861elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
862 void *data)
863{
a50b1753 864 size_t *count = (size_t *) data;
6fa3860b 865
6fa3860b
PB
866 if (!h->forced_local)
867 return TRUE;
868
42751cf3 869 if (h->dynindx != -1)
30b30c21
RH
870 h->dynindx = ++(*count);
871
b34976b6 872 return TRUE;
42751cf3 873}
30b30c21 874
aee6f5b4
AO
875/* Return true if the dynamic symbol for a given section should be
876 omitted when creating a shared library. */
877bfd_boolean
878_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
879 struct bfd_link_info *info,
880 asection *p)
881{
74541ad4 882 struct elf_link_hash_table *htab;
ca55926c 883 asection *ip;
74541ad4 884
aee6f5b4
AO
885 switch (elf_section_data (p)->this_hdr.sh_type)
886 {
887 case SHT_PROGBITS:
888 case SHT_NOBITS:
889 /* If sh_type is yet undecided, assume it could be
890 SHT_PROGBITS/SHT_NOBITS. */
891 case SHT_NULL:
74541ad4
AM
892 htab = elf_hash_table (info);
893 if (p == htab->tls_sec)
894 return FALSE;
895
896 if (htab->text_index_section != NULL)
897 return p != htab->text_index_section && p != htab->data_index_section;
898
ca55926c 899 return (htab->dynobj != NULL
3d4d4302 900 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 901 && ip->output_section == p);
aee6f5b4
AO
902
903 /* There shouldn't be section relative relocations
904 against any other section. */
905 default:
906 return TRUE;
907 }
908}
909
062e2358 910/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
911 symbol for each output section, which come first. Next come symbols
912 which have been forced to local binding. Then all of the back-end
913 allocated local dynamic syms, followed by the rest of the global
914 symbols. */
30b30c21 915
554220db
AM
916static unsigned long
917_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
918 struct bfd_link_info *info,
919 unsigned long *section_sym_count)
30b30c21
RH
920{
921 unsigned long dynsymcount = 0;
922
0e1862bb
L
923 if (bfd_link_pic (info)
924 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 925 {
aee6f5b4 926 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
927 asection *p;
928 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 929 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
930 && (p->flags & SEC_ALLOC) != 0
931 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
932 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
933 else
934 elf_section_data (p)->dynindx = 0;
30b30c21 935 }
554220db 936 *section_sym_count = dynsymcount;
30b30c21 937
6fa3860b
PB
938 elf_link_hash_traverse (elf_hash_table (info),
939 elf_link_renumber_local_hash_table_dynsyms,
940 &dynsymcount);
941
30b30c21
RH
942 if (elf_hash_table (info)->dynlocal)
943 {
944 struct elf_link_local_dynamic_entry *p;
945 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
946 p->dynindx = ++dynsymcount;
947 }
90ac2420 948 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
949
950 elf_link_hash_traverse (elf_hash_table (info),
951 elf_link_renumber_hash_table_dynsyms,
952 &dynsymcount);
953
d5486c43
L
954 /* There is an unused NULL entry at the head of the table which we
955 must account for in our count even if the table is empty since it
956 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
957 .dynamic section. */
958 dynsymcount++;
30b30c21 959
ccabcbe5
AM
960 elf_hash_table (info)->dynsymcount = dynsymcount;
961 return dynsymcount;
30b30c21 962}
252b5132 963
54ac0771
L
964/* Merge st_other field. */
965
966static void
967elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 968 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 969 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
970{
971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
972
973 /* If st_other has a processor-specific meaning, specific
cd3416da 974 code might be needed here. */
54ac0771
L
975 if (bed->elf_backend_merge_symbol_attribute)
976 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
977 dynamic);
978
cd3416da 979 if (!dynamic)
54ac0771 980 {
cd3416da
AM
981 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
982 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 983
cd3416da
AM
984 /* Keep the most constraining visibility. Leave the remainder
985 of the st_other field to elf_backend_merge_symbol_attribute. */
986 if (symvis - 1 < hvis - 1)
987 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 988 }
b8417128
AM
989 else if (definition
990 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
991 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 992 h->protected_def = 1;
54ac0771
L
993}
994
4f3fedcf
AM
995/* This function is called when we want to merge a new symbol with an
996 existing symbol. It handles the various cases which arise when we
997 find a definition in a dynamic object, or when there is already a
998 definition in a dynamic object. The new symbol is described by
999 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1000 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1001 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1002 of an old common symbol. We set OVERRIDE if the old symbol is
1003 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1004 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1005 to change. By OK to change, we mean that we shouldn't warn if the
1006 type or size does change. */
45d6a902 1007
8a56bd02 1008static bfd_boolean
268b6b39
AM
1009_bfd_elf_merge_symbol (bfd *abfd,
1010 struct bfd_link_info *info,
1011 const char *name,
1012 Elf_Internal_Sym *sym,
1013 asection **psec,
1014 bfd_vma *pvalue,
4f3fedcf
AM
1015 struct elf_link_hash_entry **sym_hash,
1016 bfd **poldbfd,
37a9e49a 1017 bfd_boolean *pold_weak,
af44c138 1018 unsigned int *pold_alignment,
268b6b39
AM
1019 bfd_boolean *skip,
1020 bfd_boolean *override,
1021 bfd_boolean *type_change_ok,
6e33951e
L
1022 bfd_boolean *size_change_ok,
1023 bfd_boolean *matched)
252b5132 1024{
7479dfd4 1025 asection *sec, *oldsec;
45d6a902 1026 struct elf_link_hash_entry *h;
90c984fc 1027 struct elf_link_hash_entry *hi;
45d6a902
AM
1028 struct elf_link_hash_entry *flip;
1029 int bind;
1030 bfd *oldbfd;
1031 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1032 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1033 const struct elf_backend_data *bed;
6e33951e 1034 char *new_version;
45d6a902
AM
1035
1036 *skip = FALSE;
1037 *override = FALSE;
1038
1039 sec = *psec;
1040 bind = ELF_ST_BIND (sym->st_info);
1041
1042 if (! bfd_is_und_section (sec))
1043 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1044 else
1045 h = ((struct elf_link_hash_entry *)
1046 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1047 if (h == NULL)
1048 return FALSE;
1049 *sym_hash = h;
252b5132 1050
88ba32a0
L
1051 bed = get_elf_backend_data (abfd);
1052
6e33951e 1053 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1054 if (h->versioned != unversioned)
6e33951e 1055 {
422f1182
L
1056 /* Symbol version is unknown or versioned. */
1057 new_version = strrchr (name, ELF_VER_CHR);
1058 if (new_version)
1059 {
1060 if (h->versioned == unknown)
1061 {
1062 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1063 h->versioned = versioned_hidden;
1064 else
1065 h->versioned = versioned;
1066 }
1067 new_version += 1;
1068 if (new_version[0] == '\0')
1069 new_version = NULL;
1070 }
1071 else
1072 h->versioned = unversioned;
6e33951e 1073 }
422f1182
L
1074 else
1075 new_version = NULL;
6e33951e 1076
90c984fc
L
1077 /* For merging, we only care about real symbols. But we need to make
1078 sure that indirect symbol dynamic flags are updated. */
1079 hi = h;
45d6a902
AM
1080 while (h->root.type == bfd_link_hash_indirect
1081 || h->root.type == bfd_link_hash_warning)
1082 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1083
6e33951e
L
1084 if (!*matched)
1085 {
1086 if (hi == h || h->root.type == bfd_link_hash_new)
1087 *matched = TRUE;
1088 else
1089 {
ae7683d2 1090 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1091 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1092 true if the new symbol is only visible to the symbol with
6e33951e 1093 the same symbol version. */
422f1182
L
1094 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1095 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1096 if (!old_hidden && !new_hidden)
1097 /* The new symbol matches the existing symbol if both
1098 aren't hidden. */
1099 *matched = TRUE;
1100 else
1101 {
1102 /* OLD_VERSION is the symbol version of the existing
1103 symbol. */
422f1182
L
1104 char *old_version;
1105
1106 if (h->versioned >= versioned)
1107 old_version = strrchr (h->root.root.string,
1108 ELF_VER_CHR) + 1;
1109 else
1110 old_version = NULL;
6e33951e
L
1111
1112 /* The new symbol matches the existing symbol if they
1113 have the same symbol version. */
1114 *matched = (old_version == new_version
1115 || (old_version != NULL
1116 && new_version != NULL
1117 && strcmp (old_version, new_version) == 0));
1118 }
1119 }
1120 }
1121
934bce08
AM
1122 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1123 existing symbol. */
1124
1125 oldbfd = NULL;
1126 oldsec = NULL;
1127 switch (h->root.type)
1128 {
1129 default:
1130 break;
1131
1132 case bfd_link_hash_undefined:
1133 case bfd_link_hash_undefweak:
1134 oldbfd = h->root.u.undef.abfd;
1135 break;
1136
1137 case bfd_link_hash_defined:
1138 case bfd_link_hash_defweak:
1139 oldbfd = h->root.u.def.section->owner;
1140 oldsec = h->root.u.def.section;
1141 break;
1142
1143 case bfd_link_hash_common:
1144 oldbfd = h->root.u.c.p->section->owner;
1145 oldsec = h->root.u.c.p->section;
1146 if (pold_alignment)
1147 *pold_alignment = h->root.u.c.p->alignment_power;
1148 break;
1149 }
1150 if (poldbfd && *poldbfd == NULL)
1151 *poldbfd = oldbfd;
1152
1153 /* Differentiate strong and weak symbols. */
1154 newweak = bind == STB_WEAK;
1155 oldweak = (h->root.type == bfd_link_hash_defweak
1156 || h->root.type == bfd_link_hash_undefweak);
1157 if (pold_weak)
1158 *pold_weak = oldweak;
1159
1160 /* This code is for coping with dynamic objects, and is only useful
1161 if we are doing an ELF link. */
1162 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1163 return TRUE;
1164
40b36307 1165 /* We have to check it for every instance since the first few may be
ee659f1f 1166 references and not all compilers emit symbol type for undefined
40b36307
L
1167 symbols. */
1168 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1169
ee659f1f
AM
1170 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1171 respectively, is from a dynamic object. */
1172
1173 newdyn = (abfd->flags & DYNAMIC) != 0;
1174
1175 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1176 syms and defined syms in dynamic libraries respectively.
1177 ref_dynamic on the other hand can be set for a symbol defined in
1178 a dynamic library, and def_dynamic may not be set; When the
1179 definition in a dynamic lib is overridden by a definition in the
1180 executable use of the symbol in the dynamic lib becomes a
1181 reference to the executable symbol. */
1182 if (newdyn)
1183 {
1184 if (bfd_is_und_section (sec))
1185 {
1186 if (bind != STB_WEAK)
1187 {
1188 h->ref_dynamic_nonweak = 1;
1189 hi->ref_dynamic_nonweak = 1;
1190 }
1191 }
1192 else
1193 {
6e33951e
L
1194 /* Update the existing symbol only if they match. */
1195 if (*matched)
1196 h->dynamic_def = 1;
ee659f1f
AM
1197 hi->dynamic_def = 1;
1198 }
1199 }
1200
45d6a902
AM
1201 /* If we just created the symbol, mark it as being an ELF symbol.
1202 Other than that, there is nothing to do--there is no merge issue
1203 with a newly defined symbol--so we just return. */
1204
1205 if (h->root.type == bfd_link_hash_new)
252b5132 1206 {
f5385ebf 1207 h->non_elf = 0;
45d6a902
AM
1208 return TRUE;
1209 }
252b5132 1210
45d6a902
AM
1211 /* In cases involving weak versioned symbols, we may wind up trying
1212 to merge a symbol with itself. Catch that here, to avoid the
1213 confusion that results if we try to override a symbol with
1214 itself. The additional tests catch cases like
1215 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1216 dynamic object, which we do want to handle here. */
1217 if (abfd == oldbfd
895fa45f 1218 && (newweak || oldweak)
45d6a902 1219 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1220 || !h->def_regular))
45d6a902
AM
1221 return TRUE;
1222
707bba77 1223 olddyn = FALSE;
45d6a902
AM
1224 if (oldbfd != NULL)
1225 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1226 else if (oldsec != NULL)
45d6a902 1227 {
707bba77 1228 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1229 indices used by MIPS ELF. */
707bba77 1230 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1231 }
252b5132 1232
45d6a902
AM
1233 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1234 respectively, appear to be a definition rather than reference. */
1235
707bba77 1236 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1237
707bba77
AM
1238 olddef = (h->root.type != bfd_link_hash_undefined
1239 && h->root.type != bfd_link_hash_undefweak
202ac193 1240 && h->root.type != bfd_link_hash_common);
45d6a902 1241
0a36a439
L
1242 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1243 respectively, appear to be a function. */
1244
1245 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1246 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1247
1248 oldfunc = (h->type != STT_NOTYPE
1249 && bed->is_function_type (h->type));
1250
c5d37467 1251 if (!(newfunc && oldfunc)
5b677558
AM
1252 && ELF_ST_TYPE (sym->st_info) != h->type
1253 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1254 && h->type != STT_NOTYPE
c5d37467
AM
1255 && (newdef || bfd_is_com_section (sec))
1256 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1257 {
c5d37467
AM
1258 /* If creating a default indirect symbol ("foo" or "foo@") from
1259 a dynamic versioned definition ("foo@@") skip doing so if
1260 there is an existing regular definition with a different
1261 type. We don't want, for example, a "time" variable in the
1262 executable overriding a "time" function in a shared library. */
1263 if (newdyn
1264 && !olddyn)
1265 {
1266 *skip = TRUE;
1267 return TRUE;
1268 }
1269
1270 /* When adding a symbol from a regular object file after we have
1271 created indirect symbols, undo the indirection and any
1272 dynamic state. */
1273 if (hi != h
1274 && !newdyn
1275 && olddyn)
1276 {
1277 h = hi;
1278 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1279 h->forced_local = 0;
1280 h->ref_dynamic = 0;
1281 h->def_dynamic = 0;
1282 h->dynamic_def = 0;
1283 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1284 {
1285 h->root.type = bfd_link_hash_undefined;
1286 h->root.u.undef.abfd = abfd;
1287 }
1288 else
1289 {
1290 h->root.type = bfd_link_hash_new;
1291 h->root.u.undef.abfd = NULL;
1292 }
1293 return TRUE;
1294 }
580a2b6e
L
1295 }
1296
4c34aff8
AM
1297 /* Check TLS symbols. We don't check undefined symbols introduced
1298 by "ld -u" which have no type (and oldbfd NULL), and we don't
1299 check symbols from plugins because they also have no type. */
1300 if (oldbfd != NULL
1301 && (oldbfd->flags & BFD_PLUGIN) == 0
1302 && (abfd->flags & BFD_PLUGIN) == 0
1303 && ELF_ST_TYPE (sym->st_info) != h->type
1304 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1305 {
1306 bfd *ntbfd, *tbfd;
1307 bfd_boolean ntdef, tdef;
1308 asection *ntsec, *tsec;
1309
1310 if (h->type == STT_TLS)
1311 {
3b36f7e6 1312 ntbfd = abfd;
7479dfd4
L
1313 ntsec = sec;
1314 ntdef = newdef;
1315 tbfd = oldbfd;
1316 tsec = oldsec;
1317 tdef = olddef;
1318 }
1319 else
1320 {
1321 ntbfd = oldbfd;
1322 ntsec = oldsec;
1323 ntdef = olddef;
1324 tbfd = abfd;
1325 tsec = sec;
1326 tdef = newdef;
1327 }
1328
1329 if (tdef && ntdef)
4eca0228 1330 _bfd_error_handler
695344c0 1331 /* xgettext:c-format */
191c0c42
AM
1332 (_("%s: TLS definition in %B section %A "
1333 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1334 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1335 else if (!tdef && !ntdef)
4eca0228 1336 _bfd_error_handler
695344c0 1337 /* xgettext:c-format */
191c0c42
AM
1338 (_("%s: TLS reference in %B "
1339 "mismatches non-TLS reference in %B"),
c08bb8dd 1340 h->root.root.string, tbfd, ntbfd);
7479dfd4 1341 else if (tdef)
4eca0228 1342 _bfd_error_handler
695344c0 1343 /* xgettext:c-format */
191c0c42
AM
1344 (_("%s: TLS definition in %B section %A "
1345 "mismatches non-TLS reference in %B"),
c08bb8dd 1346 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1347 else
4eca0228 1348 _bfd_error_handler
695344c0 1349 /* xgettext:c-format */
191c0c42
AM
1350 (_("%s: TLS reference in %B "
1351 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1352 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1353
1354 bfd_set_error (bfd_error_bad_value);
1355 return FALSE;
1356 }
1357
45d6a902
AM
1358 /* If the old symbol has non-default visibility, we ignore the new
1359 definition from a dynamic object. */
1360 if (newdyn
9c7a29a3 1361 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1362 && !bfd_is_und_section (sec))
1363 {
1364 *skip = TRUE;
1365 /* Make sure this symbol is dynamic. */
f5385ebf 1366 h->ref_dynamic = 1;
90c984fc 1367 hi->ref_dynamic = 1;
45d6a902
AM
1368 /* A protected symbol has external availability. Make sure it is
1369 recorded as dynamic.
1370
1371 FIXME: Should we check type and size for protected symbol? */
1372 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1373 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1374 else
1375 return TRUE;
1376 }
1377 else if (!newdyn
9c7a29a3 1378 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1379 && h->def_dynamic)
45d6a902
AM
1380 {
1381 /* If the new symbol with non-default visibility comes from a
1382 relocatable file and the old definition comes from a dynamic
1383 object, we remove the old definition. */
6c9b78e6 1384 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1385 {
1386 /* Handle the case where the old dynamic definition is
1387 default versioned. We need to copy the symbol info from
1388 the symbol with default version to the normal one if it
1389 was referenced before. */
1390 if (h->ref_regular)
1391 {
6c9b78e6 1392 hi->root.type = h->root.type;
d2dee3b2 1393 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1394 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1395
6c9b78e6 1396 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1397 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1398 {
aed81c4e
MR
1399 /* If the new symbol is hidden or internal, completely undo
1400 any dynamic link state. */
1401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1402 h->forced_local = 0;
1403 h->ref_dynamic = 0;
d2dee3b2
L
1404 }
1405 else
aed81c4e
MR
1406 h->ref_dynamic = 1;
1407
1408 h->def_dynamic = 0;
aed81c4e
MR
1409 /* FIXME: Should we check type and size for protected symbol? */
1410 h->size = 0;
1411 h->type = 0;
1412
6c9b78e6 1413 h = hi;
d2dee3b2
L
1414 }
1415 else
6c9b78e6 1416 h = hi;
d2dee3b2 1417 }
1de1a317 1418
f5eda473
AM
1419 /* If the old symbol was undefined before, then it will still be
1420 on the undefs list. If the new symbol is undefined or
1421 common, we can't make it bfd_link_hash_new here, because new
1422 undefined or common symbols will be added to the undefs list
1423 by _bfd_generic_link_add_one_symbol. Symbols may not be
1424 added twice to the undefs list. Also, if the new symbol is
1425 undefweak then we don't want to lose the strong undef. */
1426 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1427 {
1de1a317 1428 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1429 h->root.u.undef.abfd = abfd;
1430 }
1431 else
1432 {
1433 h->root.type = bfd_link_hash_new;
1434 h->root.u.undef.abfd = NULL;
1435 }
1436
f5eda473 1437 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1438 {
f5eda473
AM
1439 /* If the new symbol is hidden or internal, completely undo
1440 any dynamic link state. */
1441 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1442 h->forced_local = 0;
1443 h->ref_dynamic = 0;
45d6a902 1444 }
f5eda473
AM
1445 else
1446 h->ref_dynamic = 1;
1447 h->def_dynamic = 0;
45d6a902
AM
1448 /* FIXME: Should we check type and size for protected symbol? */
1449 h->size = 0;
1450 h->type = 0;
1451 return TRUE;
1452 }
14a793b2 1453
15b43f48
AM
1454 /* If a new weak symbol definition comes from a regular file and the
1455 old symbol comes from a dynamic library, we treat the new one as
1456 strong. Similarly, an old weak symbol definition from a regular
1457 file is treated as strong when the new symbol comes from a dynamic
1458 library. Further, an old weak symbol from a dynamic library is
1459 treated as strong if the new symbol is from a dynamic library.
1460 This reflects the way glibc's ld.so works.
1461
1462 Do this before setting *type_change_ok or *size_change_ok so that
1463 we warn properly when dynamic library symbols are overridden. */
1464
1465 if (newdef && !newdyn && olddyn)
0f8a2703 1466 newweak = FALSE;
15b43f48 1467 if (olddef && newdyn)
0f8a2703
AM
1468 oldweak = FALSE;
1469
d334575b 1470 /* Allow changes between different types of function symbol. */
0a36a439 1471 if (newfunc && oldfunc)
fcb93ecf
PB
1472 *type_change_ok = TRUE;
1473
79349b09
AM
1474 /* It's OK to change the type if either the existing symbol or the
1475 new symbol is weak. A type change is also OK if the old symbol
1476 is undefined and the new symbol is defined. */
252b5132 1477
79349b09
AM
1478 if (oldweak
1479 || newweak
1480 || (newdef
1481 && h->root.type == bfd_link_hash_undefined))
1482 *type_change_ok = TRUE;
1483
1484 /* It's OK to change the size if either the existing symbol or the
1485 new symbol is weak, or if the old symbol is undefined. */
1486
1487 if (*type_change_ok
1488 || h->root.type == bfd_link_hash_undefined)
1489 *size_change_ok = TRUE;
45d6a902 1490
45d6a902
AM
1491 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1492 symbol, respectively, appears to be a common symbol in a dynamic
1493 object. If a symbol appears in an uninitialized section, and is
1494 not weak, and is not a function, then it may be a common symbol
1495 which was resolved when the dynamic object was created. We want
1496 to treat such symbols specially, because they raise special
1497 considerations when setting the symbol size: if the symbol
1498 appears as a common symbol in a regular object, and the size in
1499 the regular object is larger, we must make sure that we use the
1500 larger size. This problematic case can always be avoided in C,
1501 but it must be handled correctly when using Fortran shared
1502 libraries.
1503
1504 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1505 likewise for OLDDYNCOMMON and OLDDEF.
1506
1507 Note that this test is just a heuristic, and that it is quite
1508 possible to have an uninitialized symbol in a shared object which
1509 is really a definition, rather than a common symbol. This could
1510 lead to some minor confusion when the symbol really is a common
1511 symbol in some regular object. However, I think it will be
1512 harmless. */
1513
1514 if (newdyn
1515 && newdef
79349b09 1516 && !newweak
45d6a902
AM
1517 && (sec->flags & SEC_ALLOC) != 0
1518 && (sec->flags & SEC_LOAD) == 0
1519 && sym->st_size > 0
0a36a439 1520 && !newfunc)
45d6a902
AM
1521 newdyncommon = TRUE;
1522 else
1523 newdyncommon = FALSE;
1524
1525 if (olddyn
1526 && olddef
1527 && h->root.type == bfd_link_hash_defined
f5385ebf 1528 && h->def_dynamic
45d6a902
AM
1529 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1530 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1531 && h->size > 0
0a36a439 1532 && !oldfunc)
45d6a902
AM
1533 olddyncommon = TRUE;
1534 else
1535 olddyncommon = FALSE;
1536
a4d8e49b
L
1537 /* We now know everything about the old and new symbols. We ask the
1538 backend to check if we can merge them. */
5d13b3b3
AM
1539 if (bed->merge_symbol != NULL)
1540 {
1541 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1542 return FALSE;
1543 sec = *psec;
1544 }
a4d8e49b 1545
45d6a902
AM
1546 /* If both the old and the new symbols look like common symbols in a
1547 dynamic object, set the size of the symbol to the larger of the
1548 two. */
1549
1550 if (olddyncommon
1551 && newdyncommon
1552 && sym->st_size != h->size)
1553 {
1554 /* Since we think we have two common symbols, issue a multiple
1555 common warning if desired. Note that we only warn if the
1556 size is different. If the size is the same, we simply let
1557 the old symbol override the new one as normally happens with
1558 symbols defined in dynamic objects. */
1559
1a72702b
AM
1560 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1561 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1562 if (sym->st_size > h->size)
1563 h->size = sym->st_size;
252b5132 1564
45d6a902 1565 *size_change_ok = TRUE;
252b5132
RH
1566 }
1567
45d6a902
AM
1568 /* If we are looking at a dynamic object, and we have found a
1569 definition, we need to see if the symbol was already defined by
1570 some other object. If so, we want to use the existing
1571 definition, and we do not want to report a multiple symbol
1572 definition error; we do this by clobbering *PSEC to be
1573 bfd_und_section_ptr.
1574
1575 We treat a common symbol as a definition if the symbol in the
1576 shared library is a function, since common symbols always
1577 represent variables; this can cause confusion in principle, but
1578 any such confusion would seem to indicate an erroneous program or
1579 shared library. We also permit a common symbol in a regular
8170f769 1580 object to override a weak symbol in a shared object. */
45d6a902
AM
1581
1582 if (newdyn
1583 && newdef
77cfaee6 1584 && (olddef
45d6a902 1585 || (h->root.type == bfd_link_hash_common
8170f769 1586 && (newweak || newfunc))))
45d6a902
AM
1587 {
1588 *override = TRUE;
1589 newdef = FALSE;
1590 newdyncommon = FALSE;
252b5132 1591
45d6a902
AM
1592 *psec = sec = bfd_und_section_ptr;
1593 *size_change_ok = TRUE;
252b5132 1594
45d6a902
AM
1595 /* If we get here when the old symbol is a common symbol, then
1596 we are explicitly letting it override a weak symbol or
1597 function in a dynamic object, and we don't want to warn about
1598 a type change. If the old symbol is a defined symbol, a type
1599 change warning may still be appropriate. */
252b5132 1600
45d6a902
AM
1601 if (h->root.type == bfd_link_hash_common)
1602 *type_change_ok = TRUE;
1603 }
1604
1605 /* Handle the special case of an old common symbol merging with a
1606 new symbol which looks like a common symbol in a shared object.
1607 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1608 common symbol, and let _bfd_generic_link_add_one_symbol do the
1609 right thing. */
45d6a902
AM
1610
1611 if (newdyncommon
1612 && h->root.type == bfd_link_hash_common)
1613 {
1614 *override = TRUE;
1615 newdef = FALSE;
1616 newdyncommon = FALSE;
1617 *pvalue = sym->st_size;
a4d8e49b 1618 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1619 *size_change_ok = TRUE;
1620 }
1621
c5e2cead 1622 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1623 if (newdef && olddef && newweak)
54ac0771 1624 {
35ed3f94 1625 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1626 if (!(oldbfd != NULL
1627 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1628 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1629 {
1630 newdef = FALSE;
1631 *skip = TRUE;
1632 }
54ac0771
L
1633
1634 /* Merge st_other. If the symbol already has a dynamic index,
1635 but visibility says it should not be visible, turn it into a
1636 local symbol. */
b8417128 1637 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1638 if (h->dynindx != -1)
1639 switch (ELF_ST_VISIBILITY (h->other))
1640 {
1641 case STV_INTERNAL:
1642 case STV_HIDDEN:
1643 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1644 break;
1645 }
1646 }
c5e2cead 1647
45d6a902
AM
1648 /* If the old symbol is from a dynamic object, and the new symbol is
1649 a definition which is not from a dynamic object, then the new
1650 symbol overrides the old symbol. Symbols from regular files
1651 always take precedence over symbols from dynamic objects, even if
1652 they are defined after the dynamic object in the link.
1653
1654 As above, we again permit a common symbol in a regular object to
1655 override a definition in a shared object if the shared object
0f8a2703 1656 symbol is a function or is weak. */
45d6a902
AM
1657
1658 flip = NULL;
77cfaee6 1659 if (!newdyn
45d6a902
AM
1660 && (newdef
1661 || (bfd_is_com_section (sec)
0a36a439 1662 && (oldweak || oldfunc)))
45d6a902
AM
1663 && olddyn
1664 && olddef
f5385ebf 1665 && h->def_dynamic)
45d6a902
AM
1666 {
1667 /* Change the hash table entry to undefined, and let
1668 _bfd_generic_link_add_one_symbol do the right thing with the
1669 new definition. */
1670
1671 h->root.type = bfd_link_hash_undefined;
1672 h->root.u.undef.abfd = h->root.u.def.section->owner;
1673 *size_change_ok = TRUE;
1674
1675 olddef = FALSE;
1676 olddyncommon = FALSE;
1677
1678 /* We again permit a type change when a common symbol may be
1679 overriding a function. */
1680
1681 if (bfd_is_com_section (sec))
0a36a439
L
1682 {
1683 if (oldfunc)
1684 {
1685 /* If a common symbol overrides a function, make sure
1686 that it isn't defined dynamically nor has type
1687 function. */
1688 h->def_dynamic = 0;
1689 h->type = STT_NOTYPE;
1690 }
1691 *type_change_ok = TRUE;
1692 }
45d6a902 1693
6c9b78e6
AM
1694 if (hi->root.type == bfd_link_hash_indirect)
1695 flip = hi;
45d6a902
AM
1696 else
1697 /* This union may have been set to be non-NULL when this symbol
1698 was seen in a dynamic object. We must force the union to be
1699 NULL, so that it is correct for a regular symbol. */
1700 h->verinfo.vertree = NULL;
1701 }
1702
1703 /* Handle the special case of a new common symbol merging with an
1704 old symbol that looks like it might be a common symbol defined in
1705 a shared object. Note that we have already handled the case in
1706 which a new common symbol should simply override the definition
1707 in the shared library. */
1708
1709 if (! newdyn
1710 && bfd_is_com_section (sec)
1711 && olddyncommon)
1712 {
1713 /* It would be best if we could set the hash table entry to a
1714 common symbol, but we don't know what to use for the section
1715 or the alignment. */
1a72702b
AM
1716 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1717 bfd_link_hash_common, sym->st_size);
45d6a902 1718
4cc11e76 1719 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1720 larger, pretend that the new symbol has its size. */
1721
1722 if (h->size > *pvalue)
1723 *pvalue = h->size;
1724
af44c138
L
1725 /* We need to remember the alignment required by the symbol
1726 in the dynamic object. */
1727 BFD_ASSERT (pold_alignment);
1728 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1729
1730 olddef = FALSE;
1731 olddyncommon = FALSE;
1732
1733 h->root.type = bfd_link_hash_undefined;
1734 h->root.u.undef.abfd = h->root.u.def.section->owner;
1735
1736 *size_change_ok = TRUE;
1737 *type_change_ok = TRUE;
1738
6c9b78e6
AM
1739 if (hi->root.type == bfd_link_hash_indirect)
1740 flip = hi;
45d6a902
AM
1741 else
1742 h->verinfo.vertree = NULL;
1743 }
1744
1745 if (flip != NULL)
1746 {
1747 /* Handle the case where we had a versioned symbol in a dynamic
1748 library and now find a definition in a normal object. In this
1749 case, we make the versioned symbol point to the normal one. */
45d6a902 1750 flip->root.type = h->root.type;
00cbee0a 1751 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1752 h->root.type = bfd_link_hash_indirect;
1753 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1754 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1755 if (h->def_dynamic)
45d6a902 1756 {
f5385ebf
AM
1757 h->def_dynamic = 0;
1758 flip->ref_dynamic = 1;
45d6a902
AM
1759 }
1760 }
1761
45d6a902
AM
1762 return TRUE;
1763}
1764
1765/* This function is called to create an indirect symbol from the
1766 default for the symbol with the default version if needed. The
4f3fedcf 1767 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1768 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1769
28caa186 1770static bfd_boolean
268b6b39
AM
1771_bfd_elf_add_default_symbol (bfd *abfd,
1772 struct bfd_link_info *info,
1773 struct elf_link_hash_entry *h,
1774 const char *name,
1775 Elf_Internal_Sym *sym,
4f3fedcf
AM
1776 asection *sec,
1777 bfd_vma value,
1778 bfd **poldbfd,
e3c9d234 1779 bfd_boolean *dynsym)
45d6a902
AM
1780{
1781 bfd_boolean type_change_ok;
1782 bfd_boolean size_change_ok;
1783 bfd_boolean skip;
1784 char *shortname;
1785 struct elf_link_hash_entry *hi;
1786 struct bfd_link_hash_entry *bh;
9c5bfbb7 1787 const struct elf_backend_data *bed;
45d6a902
AM
1788 bfd_boolean collect;
1789 bfd_boolean dynamic;
e3c9d234 1790 bfd_boolean override;
45d6a902
AM
1791 char *p;
1792 size_t len, shortlen;
ffd65175 1793 asection *tmp_sec;
6e33951e 1794 bfd_boolean matched;
45d6a902 1795
422f1182
L
1796 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1797 return TRUE;
1798
45d6a902
AM
1799 /* If this symbol has a version, and it is the default version, we
1800 create an indirect symbol from the default name to the fully
1801 decorated name. This will cause external references which do not
1802 specify a version to be bound to this version of the symbol. */
1803 p = strchr (name, ELF_VER_CHR);
422f1182
L
1804 if (h->versioned == unknown)
1805 {
1806 if (p == NULL)
1807 {
1808 h->versioned = unversioned;
1809 return TRUE;
1810 }
1811 else
1812 {
1813 if (p[1] != ELF_VER_CHR)
1814 {
1815 h->versioned = versioned_hidden;
1816 return TRUE;
1817 }
1818 else
1819 h->versioned = versioned;
1820 }
1821 }
4373f8af
L
1822 else
1823 {
1824 /* PR ld/19073: We may see an unversioned definition after the
1825 default version. */
1826 if (p == NULL)
1827 return TRUE;
1828 }
45d6a902 1829
45d6a902
AM
1830 bed = get_elf_backend_data (abfd);
1831 collect = bed->collect;
1832 dynamic = (abfd->flags & DYNAMIC) != 0;
1833
1834 shortlen = p - name;
a50b1753 1835 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1836 if (shortname == NULL)
1837 return FALSE;
1838 memcpy (shortname, name, shortlen);
1839 shortname[shortlen] = '\0';
1840
1841 /* We are going to create a new symbol. Merge it with any existing
1842 symbol with this name. For the purposes of the merge, act as
1843 though we were defining the symbol we just defined, although we
1844 actually going to define an indirect symbol. */
1845 type_change_ok = FALSE;
1846 size_change_ok = FALSE;
6e33951e 1847 matched = TRUE;
ffd65175
AM
1848 tmp_sec = sec;
1849 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1850 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1851 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1852 return FALSE;
1853
1854 if (skip)
1855 goto nondefault;
1856
5b677558
AM
1857 if (hi->def_regular)
1858 {
1859 /* If the undecorated symbol will have a version added by a
1860 script different to H, then don't indirect to/from the
1861 undecorated symbol. This isn't ideal because we may not yet
1862 have seen symbol versions, if given by a script on the
1863 command line rather than via --version-script. */
1864 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1865 {
1866 bfd_boolean hide;
1867
1868 hi->verinfo.vertree
1869 = bfd_find_version_for_sym (info->version_info,
1870 hi->root.root.string, &hide);
1871 if (hi->verinfo.vertree != NULL && hide)
1872 {
1873 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1874 goto nondefault;
1875 }
1876 }
1877 if (hi->verinfo.vertree != NULL
1878 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1879 goto nondefault;
1880 }
1881
45d6a902
AM
1882 if (! override)
1883 {
c6e8a9a8 1884 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1885 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1886 {
1887 bh = &hi->root;
1888 if (! (_bfd_generic_link_add_one_symbol
1889 (info, abfd, shortname, BSF_INDIRECT,
1890 bfd_ind_section_ptr,
1891 0, name, FALSE, collect, &bh)))
1892 return FALSE;
1893 hi = (struct elf_link_hash_entry *) bh;
1894 }
45d6a902
AM
1895 }
1896 else
1897 {
1898 /* In this case the symbol named SHORTNAME is overriding the
1899 indirect symbol we want to add. We were planning on making
1900 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1901 is the name without a version. NAME is the fully versioned
1902 name, and it is the default version.
1903
1904 Overriding means that we already saw a definition for the
1905 symbol SHORTNAME in a regular object, and it is overriding
1906 the symbol defined in the dynamic object.
1907
1908 When this happens, we actually want to change NAME, the
1909 symbol we just added, to refer to SHORTNAME. This will cause
1910 references to NAME in the shared object to become references
1911 to SHORTNAME in the regular object. This is what we expect
1912 when we override a function in a shared object: that the
1913 references in the shared object will be mapped to the
1914 definition in the regular object. */
1915
1916 while (hi->root.type == bfd_link_hash_indirect
1917 || hi->root.type == bfd_link_hash_warning)
1918 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1919
1920 h->root.type = bfd_link_hash_indirect;
1921 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1922 if (h->def_dynamic)
45d6a902 1923 {
f5385ebf
AM
1924 h->def_dynamic = 0;
1925 hi->ref_dynamic = 1;
1926 if (hi->ref_regular
1927 || hi->def_regular)
45d6a902 1928 {
c152c796 1929 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1930 return FALSE;
1931 }
1932 }
1933
1934 /* Now set HI to H, so that the following code will set the
1935 other fields correctly. */
1936 hi = h;
1937 }
1938
fab4a87f
L
1939 /* Check if HI is a warning symbol. */
1940 if (hi->root.type == bfd_link_hash_warning)
1941 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1942
45d6a902
AM
1943 /* If there is a duplicate definition somewhere, then HI may not
1944 point to an indirect symbol. We will have reported an error to
1945 the user in that case. */
1946
1947 if (hi->root.type == bfd_link_hash_indirect)
1948 {
1949 struct elf_link_hash_entry *ht;
1950
45d6a902 1951 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1952 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1953
68c88cd4
AM
1954 /* A reference to the SHORTNAME symbol from a dynamic library
1955 will be satisfied by the versioned symbol at runtime. In
1956 effect, we have a reference to the versioned symbol. */
1957 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1958 hi->dynamic_def |= ht->dynamic_def;
1959
45d6a902
AM
1960 /* See if the new flags lead us to realize that the symbol must
1961 be dynamic. */
1962 if (! *dynsym)
1963 {
1964 if (! dynamic)
1965 {
0e1862bb 1966 if (! bfd_link_executable (info)
90c984fc 1967 || hi->def_dynamic
f5385ebf 1968 || hi->ref_dynamic)
45d6a902
AM
1969 *dynsym = TRUE;
1970 }
1971 else
1972 {
f5385ebf 1973 if (hi->ref_regular)
45d6a902
AM
1974 *dynsym = TRUE;
1975 }
1976 }
1977 }
1978
1979 /* We also need to define an indirection from the nondefault version
1980 of the symbol. */
1981
1982nondefault:
1983 len = strlen (name);
a50b1753 1984 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1985 if (shortname == NULL)
1986 return FALSE;
1987 memcpy (shortname, name, shortlen);
1988 memcpy (shortname + shortlen, p + 1, len - shortlen);
1989
1990 /* Once again, merge with any existing symbol. */
1991 type_change_ok = FALSE;
1992 size_change_ok = FALSE;
ffd65175
AM
1993 tmp_sec = sec;
1994 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1995 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1996 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1997 return FALSE;
1998
1999 if (skip)
2000 return TRUE;
2001
2002 if (override)
2003 {
2004 /* Here SHORTNAME is a versioned name, so we don't expect to see
2005 the type of override we do in the case above unless it is
4cc11e76 2006 overridden by a versioned definition. */
45d6a902
AM
2007 if (hi->root.type != bfd_link_hash_defined
2008 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2009 _bfd_error_handler
695344c0 2010 /* xgettext:c-format */
d003868e
AM
2011 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2012 abfd, shortname);
45d6a902
AM
2013 }
2014 else
2015 {
2016 bh = &hi->root;
2017 if (! (_bfd_generic_link_add_one_symbol
2018 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2019 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2020 return FALSE;
2021 hi = (struct elf_link_hash_entry *) bh;
2022
2023 /* If there is a duplicate definition somewhere, then HI may not
2024 point to an indirect symbol. We will have reported an error
2025 to the user in that case. */
2026
2027 if (hi->root.type == bfd_link_hash_indirect)
2028 {
fcfa13d2 2029 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2030 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2031 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2032
2033 /* See if the new flags lead us to realize that the symbol
2034 must be dynamic. */
2035 if (! *dynsym)
2036 {
2037 if (! dynamic)
2038 {
0e1862bb 2039 if (! bfd_link_executable (info)
f5385ebf 2040 || hi->ref_dynamic)
45d6a902
AM
2041 *dynsym = TRUE;
2042 }
2043 else
2044 {
f5385ebf 2045 if (hi->ref_regular)
45d6a902
AM
2046 *dynsym = TRUE;
2047 }
2048 }
2049 }
2050 }
2051
2052 return TRUE;
2053}
2054\f
2055/* This routine is used to export all defined symbols into the dynamic
2056 symbol table. It is called via elf_link_hash_traverse. */
2057
28caa186 2058static bfd_boolean
268b6b39 2059_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2060{
a50b1753 2061 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2062
2063 /* Ignore indirect symbols. These are added by the versioning code. */
2064 if (h->root.type == bfd_link_hash_indirect)
2065 return TRUE;
2066
7686d77d
AM
2067 /* Ignore this if we won't export it. */
2068 if (!eif->info->export_dynamic && !h->dynamic)
2069 return TRUE;
45d6a902
AM
2070
2071 if (h->dynindx == -1
fd91d419
L
2072 && (h->def_regular || h->ref_regular)
2073 && ! bfd_hide_sym_by_version (eif->info->version_info,
2074 h->root.root.string))
45d6a902 2075 {
fd91d419 2076 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2077 {
fd91d419
L
2078 eif->failed = TRUE;
2079 return FALSE;
45d6a902
AM
2080 }
2081 }
2082
2083 return TRUE;
2084}
2085\f
2086/* Look through the symbols which are defined in other shared
2087 libraries and referenced here. Update the list of version
2088 dependencies. This will be put into the .gnu.version_r section.
2089 This function is called via elf_link_hash_traverse. */
2090
28caa186 2091static bfd_boolean
268b6b39
AM
2092_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2093 void *data)
45d6a902 2094{
a50b1753 2095 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2096 Elf_Internal_Verneed *t;
2097 Elf_Internal_Vernaux *a;
2098 bfd_size_type amt;
2099
45d6a902
AM
2100 /* We only care about symbols defined in shared objects with version
2101 information. */
f5385ebf
AM
2102 if (!h->def_dynamic
2103 || h->def_regular
45d6a902 2104 || h->dynindx == -1
7b20f099
AM
2105 || h->verinfo.verdef == NULL
2106 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2107 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2108 return TRUE;
2109
2110 /* See if we already know about this version. */
28caa186
AM
2111 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2112 t != NULL;
2113 t = t->vn_nextref)
45d6a902
AM
2114 {
2115 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2116 continue;
2117
2118 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2119 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2120 return TRUE;
2121
2122 break;
2123 }
2124
2125 /* This is a new version. Add it to tree we are building. */
2126
2127 if (t == NULL)
2128 {
2129 amt = sizeof *t;
a50b1753 2130 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2131 if (t == NULL)
2132 {
2133 rinfo->failed = TRUE;
2134 return FALSE;
2135 }
2136
2137 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2138 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2139 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2140 }
2141
2142 amt = sizeof *a;
a50b1753 2143 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2144 if (a == NULL)
2145 {
2146 rinfo->failed = TRUE;
2147 return FALSE;
2148 }
45d6a902
AM
2149
2150 /* Note that we are copying a string pointer here, and testing it
2151 above. If bfd_elf_string_from_elf_section is ever changed to
2152 discard the string data when low in memory, this will have to be
2153 fixed. */
2154 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2155
2156 a->vna_flags = h->verinfo.verdef->vd_flags;
2157 a->vna_nextptr = t->vn_auxptr;
2158
2159 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2160 ++rinfo->vers;
2161
2162 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2163
2164 t->vn_auxptr = a;
2165
2166 return TRUE;
2167}
2168
2169/* Figure out appropriate versions for all the symbols. We may not
2170 have the version number script until we have read all of the input
2171 files, so until that point we don't know which symbols should be
2172 local. This function is called via elf_link_hash_traverse. */
2173
28caa186 2174static bfd_boolean
268b6b39 2175_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2176{
28caa186 2177 struct elf_info_failed *sinfo;
45d6a902 2178 struct bfd_link_info *info;
9c5bfbb7 2179 const struct elf_backend_data *bed;
45d6a902
AM
2180 struct elf_info_failed eif;
2181 char *p;
45d6a902 2182
a50b1753 2183 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2184 info = sinfo->info;
2185
45d6a902
AM
2186 /* Fix the symbol flags. */
2187 eif.failed = FALSE;
2188 eif.info = info;
2189 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2190 {
2191 if (eif.failed)
2192 sinfo->failed = TRUE;
2193 return FALSE;
2194 }
2195
2196 /* We only need version numbers for symbols defined in regular
2197 objects. */
f5385ebf 2198 if (!h->def_regular)
45d6a902
AM
2199 return TRUE;
2200
28caa186 2201 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2202 p = strchr (h->root.root.string, ELF_VER_CHR);
2203 if (p != NULL && h->verinfo.vertree == NULL)
2204 {
2205 struct bfd_elf_version_tree *t;
45d6a902 2206
45d6a902
AM
2207 ++p;
2208 if (*p == ELF_VER_CHR)
6e33951e 2209 ++p;
45d6a902
AM
2210
2211 /* If there is no version string, we can just return out. */
2212 if (*p == '\0')
6e33951e 2213 return TRUE;
45d6a902
AM
2214
2215 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2216 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2217 {
2218 if (strcmp (t->name, p) == 0)
2219 {
2220 size_t len;
2221 char *alc;
2222 struct bfd_elf_version_expr *d;
2223
2224 len = p - h->root.root.string;
a50b1753 2225 alc = (char *) bfd_malloc (len);
45d6a902 2226 if (alc == NULL)
14b1c01e
AM
2227 {
2228 sinfo->failed = TRUE;
2229 return FALSE;
2230 }
45d6a902
AM
2231 memcpy (alc, h->root.root.string, len - 1);
2232 alc[len - 1] = '\0';
2233 if (alc[len - 2] == ELF_VER_CHR)
2234 alc[len - 2] = '\0';
2235
2236 h->verinfo.vertree = t;
2237 t->used = TRUE;
2238 d = NULL;
2239
108ba305
JJ
2240 if (t->globals.list != NULL)
2241 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2242
2243 /* See if there is anything to force this symbol to
2244 local scope. */
108ba305 2245 if (d == NULL && t->locals.list != NULL)
45d6a902 2246 {
108ba305
JJ
2247 d = (*t->match) (&t->locals, NULL, alc);
2248 if (d != NULL
2249 && h->dynindx != -1
108ba305
JJ
2250 && ! info->export_dynamic)
2251 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2252 }
2253
2254 free (alc);
2255 break;
2256 }
2257 }
2258
2259 /* If we are building an application, we need to create a
2260 version node for this version. */
0e1862bb 2261 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2262 {
2263 struct bfd_elf_version_tree **pp;
2264 int version_index;
2265
2266 /* If we aren't going to export this symbol, we don't need
2267 to worry about it. */
2268 if (h->dynindx == -1)
2269 return TRUE;
2270
ef53be89
AM
2271 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2272 sizeof *t);
45d6a902
AM
2273 if (t == NULL)
2274 {
2275 sinfo->failed = TRUE;
2276 return FALSE;
2277 }
2278
45d6a902 2279 t->name = p;
45d6a902
AM
2280 t->name_indx = (unsigned int) -1;
2281 t->used = TRUE;
2282
2283 version_index = 1;
2284 /* Don't count anonymous version tag. */
fd91d419
L
2285 if (sinfo->info->version_info != NULL
2286 && sinfo->info->version_info->vernum == 0)
45d6a902 2287 version_index = 0;
fd91d419
L
2288 for (pp = &sinfo->info->version_info;
2289 *pp != NULL;
2290 pp = &(*pp)->next)
45d6a902
AM
2291 ++version_index;
2292 t->vernum = version_index;
2293
2294 *pp = t;
2295
2296 h->verinfo.vertree = t;
2297 }
2298 else if (t == NULL)
2299 {
2300 /* We could not find the version for a symbol when
2301 generating a shared archive. Return an error. */
4eca0228 2302 _bfd_error_handler
695344c0 2303 /* xgettext:c-format */
c55fe096 2304 (_("%B: version node not found for symbol %s"),
28caa186 2305 info->output_bfd, h->root.root.string);
45d6a902
AM
2306 bfd_set_error (bfd_error_bad_value);
2307 sinfo->failed = TRUE;
2308 return FALSE;
2309 }
45d6a902
AM
2310 }
2311
2312 /* If we don't have a version for this symbol, see if we can find
2313 something. */
fd91d419 2314 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2315 {
1e8fa21e 2316 bfd_boolean hide;
ae5a3597 2317
fd91d419
L
2318 h->verinfo.vertree
2319 = bfd_find_version_for_sym (sinfo->info->version_info,
2320 h->root.root.string, &hide);
1e8fa21e
AM
2321 if (h->verinfo.vertree != NULL && hide)
2322 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2323 }
2324
2325 return TRUE;
2326}
2327\f
45d6a902
AM
2328/* Read and swap the relocs from the section indicated by SHDR. This
2329 may be either a REL or a RELA section. The relocations are
2330 translated into RELA relocations and stored in INTERNAL_RELOCS,
2331 which should have already been allocated to contain enough space.
2332 The EXTERNAL_RELOCS are a buffer where the external form of the
2333 relocations should be stored.
2334
2335 Returns FALSE if something goes wrong. */
2336
2337static bfd_boolean
268b6b39 2338elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2339 asection *sec,
268b6b39
AM
2340 Elf_Internal_Shdr *shdr,
2341 void *external_relocs,
2342 Elf_Internal_Rela *internal_relocs)
45d6a902 2343{
9c5bfbb7 2344 const struct elf_backend_data *bed;
268b6b39 2345 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2346 const bfd_byte *erela;
2347 const bfd_byte *erelaend;
2348 Elf_Internal_Rela *irela;
243ef1e0
L
2349 Elf_Internal_Shdr *symtab_hdr;
2350 size_t nsyms;
45d6a902 2351
45d6a902
AM
2352 /* Position ourselves at the start of the section. */
2353 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2354 return FALSE;
2355
2356 /* Read the relocations. */
2357 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2358 return FALSE;
2359
243ef1e0 2360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2361 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2362
45d6a902
AM
2363 bed = get_elf_backend_data (abfd);
2364
2365 /* Convert the external relocations to the internal format. */
2366 if (shdr->sh_entsize == bed->s->sizeof_rel)
2367 swap_in = bed->s->swap_reloc_in;
2368 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2369 swap_in = bed->s->swap_reloca_in;
2370 else
2371 {
2372 bfd_set_error (bfd_error_wrong_format);
2373 return FALSE;
2374 }
2375
a50b1753 2376 erela = (const bfd_byte *) external_relocs;
51992aec 2377 erelaend = erela + shdr->sh_size;
45d6a902
AM
2378 irela = internal_relocs;
2379 while (erela < erelaend)
2380 {
243ef1e0
L
2381 bfd_vma r_symndx;
2382
45d6a902 2383 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2384 r_symndx = ELF32_R_SYM (irela->r_info);
2385 if (bed->s->arch_size == 64)
2386 r_symndx >>= 24;
ce98a316
NC
2387 if (nsyms > 0)
2388 {
2389 if ((size_t) r_symndx >= nsyms)
2390 {
4eca0228 2391 _bfd_error_handler
695344c0 2392 /* xgettext:c-format */
ce98a316
NC
2393 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2394 " for offset 0x%lx in section `%A'"),
c08bb8dd
AM
2395 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2396 irela->r_offset, sec);
ce98a316
NC
2397 bfd_set_error (bfd_error_bad_value);
2398 return FALSE;
2399 }
2400 }
cf35638d 2401 else if (r_symndx != STN_UNDEF)
243ef1e0 2402 {
4eca0228 2403 _bfd_error_handler
695344c0 2404 /* xgettext:c-format */
c08bb8dd
AM
2405 (_("%B: non-zero symbol index (0x%lx)"
2406 " for offset 0x%lx in section `%A'"
ce98a316 2407 " when the object file has no symbol table"),
c08bb8dd
AM
2408 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2409 irela->r_offset, sec);
243ef1e0
L
2410 bfd_set_error (bfd_error_bad_value);
2411 return FALSE;
2412 }
45d6a902
AM
2413 irela += bed->s->int_rels_per_ext_rel;
2414 erela += shdr->sh_entsize;
2415 }
2416
2417 return TRUE;
2418}
2419
2420/* Read and swap the relocs for a section O. They may have been
2421 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2422 not NULL, they are used as buffers to read into. They are known to
2423 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2424 the return value is allocated using either malloc or bfd_alloc,
2425 according to the KEEP_MEMORY argument. If O has two relocation
2426 sections (both REL and RELA relocations), then the REL_HDR
2427 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2428 RELA_HDR relocations. */
45d6a902
AM
2429
2430Elf_Internal_Rela *
268b6b39
AM
2431_bfd_elf_link_read_relocs (bfd *abfd,
2432 asection *o,
2433 void *external_relocs,
2434 Elf_Internal_Rela *internal_relocs,
2435 bfd_boolean keep_memory)
45d6a902 2436{
268b6b39 2437 void *alloc1 = NULL;
45d6a902 2438 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2439 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2440 struct bfd_elf_section_data *esdo = elf_section_data (o);
2441 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2442
d4730f92
BS
2443 if (esdo->relocs != NULL)
2444 return esdo->relocs;
45d6a902
AM
2445
2446 if (o->reloc_count == 0)
2447 return NULL;
2448
45d6a902
AM
2449 if (internal_relocs == NULL)
2450 {
2451 bfd_size_type size;
2452
056bafd4 2453 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2454 if (keep_memory)
a50b1753 2455 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2456 else
a50b1753 2457 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2458 if (internal_relocs == NULL)
2459 goto error_return;
2460 }
2461
2462 if (external_relocs == NULL)
2463 {
d4730f92
BS
2464 bfd_size_type size = 0;
2465
2466 if (esdo->rel.hdr)
2467 size += esdo->rel.hdr->sh_size;
2468 if (esdo->rela.hdr)
2469 size += esdo->rela.hdr->sh_size;
45d6a902 2470
268b6b39 2471 alloc1 = bfd_malloc (size);
45d6a902
AM
2472 if (alloc1 == NULL)
2473 goto error_return;
2474 external_relocs = alloc1;
2475 }
2476
d4730f92
BS
2477 internal_rela_relocs = internal_relocs;
2478 if (esdo->rel.hdr)
2479 {
2480 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2481 external_relocs,
2482 internal_relocs))
2483 goto error_return;
2484 external_relocs = (((bfd_byte *) external_relocs)
2485 + esdo->rel.hdr->sh_size);
2486 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2487 * bed->s->int_rels_per_ext_rel);
2488 }
2489
2490 if (esdo->rela.hdr
2491 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2492 external_relocs,
2493 internal_rela_relocs)))
45d6a902
AM
2494 goto error_return;
2495
2496 /* Cache the results for next time, if we can. */
2497 if (keep_memory)
d4730f92 2498 esdo->relocs = internal_relocs;
45d6a902
AM
2499
2500 if (alloc1 != NULL)
2501 free (alloc1);
2502
2503 /* Don't free alloc2, since if it was allocated we are passing it
2504 back (under the name of internal_relocs). */
2505
2506 return internal_relocs;
2507
2508 error_return:
2509 if (alloc1 != NULL)
2510 free (alloc1);
2511 if (alloc2 != NULL)
4dd07732
AM
2512 {
2513 if (keep_memory)
2514 bfd_release (abfd, alloc2);
2515 else
2516 free (alloc2);
2517 }
45d6a902
AM
2518 return NULL;
2519}
2520
2521/* Compute the size of, and allocate space for, REL_HDR which is the
2522 section header for a section containing relocations for O. */
2523
28caa186 2524static bfd_boolean
9eaff861
AO
2525_bfd_elf_link_size_reloc_section (bfd *abfd,
2526 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2527{
9eaff861 2528 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2529
2530 /* That allows us to calculate the size of the section. */
9eaff861 2531 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2532
2533 /* The contents field must last into write_object_contents, so we
2534 allocate it with bfd_alloc rather than malloc. Also since we
2535 cannot be sure that the contents will actually be filled in,
2536 we zero the allocated space. */
a50b1753 2537 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2538 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2539 return FALSE;
2540
d4730f92 2541 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2542 {
2543 struct elf_link_hash_entry **p;
2544
ca4be51c
AM
2545 p = ((struct elf_link_hash_entry **)
2546 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2547 if (p == NULL)
2548 return FALSE;
2549
d4730f92 2550 reldata->hashes = p;
45d6a902
AM
2551 }
2552
2553 return TRUE;
2554}
2555
2556/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2557 originated from the section given by INPUT_REL_HDR) to the
2558 OUTPUT_BFD. */
2559
2560bfd_boolean
268b6b39
AM
2561_bfd_elf_link_output_relocs (bfd *output_bfd,
2562 asection *input_section,
2563 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2564 Elf_Internal_Rela *internal_relocs,
2565 struct elf_link_hash_entry **rel_hash
2566 ATTRIBUTE_UNUSED)
45d6a902
AM
2567{
2568 Elf_Internal_Rela *irela;
2569 Elf_Internal_Rela *irelaend;
2570 bfd_byte *erel;
d4730f92 2571 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2572 asection *output_section;
9c5bfbb7 2573 const struct elf_backend_data *bed;
268b6b39 2574 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2575 struct bfd_elf_section_data *esdo;
45d6a902
AM
2576
2577 output_section = input_section->output_section;
45d6a902 2578
d4730f92
BS
2579 bed = get_elf_backend_data (output_bfd);
2580 esdo = elf_section_data (output_section);
2581 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2582 {
d4730f92
BS
2583 output_reldata = &esdo->rel;
2584 swap_out = bed->s->swap_reloc_out;
45d6a902 2585 }
d4730f92
BS
2586 else if (esdo->rela.hdr
2587 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2588 {
d4730f92
BS
2589 output_reldata = &esdo->rela;
2590 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2591 }
2592 else
2593 {
4eca0228 2594 _bfd_error_handler
695344c0 2595 /* xgettext:c-format */
d003868e
AM
2596 (_("%B: relocation size mismatch in %B section %A"),
2597 output_bfd, input_section->owner, input_section);
297d8443 2598 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2599 return FALSE;
2600 }
2601
d4730f92
BS
2602 erel = output_reldata->hdr->contents;
2603 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2604 irela = internal_relocs;
2605 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2606 * bed->s->int_rels_per_ext_rel);
2607 while (irela < irelaend)
2608 {
2609 (*swap_out) (output_bfd, irela, erel);
2610 irela += bed->s->int_rels_per_ext_rel;
2611 erel += input_rel_hdr->sh_entsize;
2612 }
2613
2614 /* Bump the counter, so that we know where to add the next set of
2615 relocations. */
d4730f92 2616 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2617
2618 return TRUE;
2619}
2620\f
508c3946
L
2621/* Make weak undefined symbols in PIE dynamic. */
2622
2623bfd_boolean
2624_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2625 struct elf_link_hash_entry *h)
2626{
0e1862bb 2627 if (bfd_link_pie (info)
508c3946
L
2628 && h->dynindx == -1
2629 && h->root.type == bfd_link_hash_undefweak)
2630 return bfd_elf_link_record_dynamic_symbol (info, h);
2631
2632 return TRUE;
2633}
2634
45d6a902
AM
2635/* Fix up the flags for a symbol. This handles various cases which
2636 can only be fixed after all the input files are seen. This is
2637 currently called by both adjust_dynamic_symbol and
2638 assign_sym_version, which is unnecessary but perhaps more robust in
2639 the face of future changes. */
2640
28caa186 2641static bfd_boolean
268b6b39
AM
2642_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2643 struct elf_info_failed *eif)
45d6a902 2644{
33774f08 2645 const struct elf_backend_data *bed;
508c3946 2646
45d6a902
AM
2647 /* If this symbol was mentioned in a non-ELF file, try to set
2648 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2649 permit a non-ELF file to correctly refer to a symbol defined in
2650 an ELF dynamic object. */
f5385ebf 2651 if (h->non_elf)
45d6a902
AM
2652 {
2653 while (h->root.type == bfd_link_hash_indirect)
2654 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2655
2656 if (h->root.type != bfd_link_hash_defined
2657 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2658 {
2659 h->ref_regular = 1;
2660 h->ref_regular_nonweak = 1;
2661 }
45d6a902
AM
2662 else
2663 {
2664 if (h->root.u.def.section->owner != NULL
2665 && (bfd_get_flavour (h->root.u.def.section->owner)
2666 == bfd_target_elf_flavour))
f5385ebf
AM
2667 {
2668 h->ref_regular = 1;
2669 h->ref_regular_nonweak = 1;
2670 }
45d6a902 2671 else
f5385ebf 2672 h->def_regular = 1;
45d6a902
AM
2673 }
2674
2675 if (h->dynindx == -1
f5385ebf
AM
2676 && (h->def_dynamic
2677 || h->ref_dynamic))
45d6a902 2678 {
c152c796 2679 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2680 {
2681 eif->failed = TRUE;
2682 return FALSE;
2683 }
2684 }
2685 }
2686 else
2687 {
f5385ebf 2688 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2689 was first seen in a non-ELF file. Fortunately, if the symbol
2690 was first seen in an ELF file, we're probably OK unless the
2691 symbol was defined in a non-ELF file. Catch that case here.
2692 FIXME: We're still in trouble if the symbol was first seen in
2693 a dynamic object, and then later in a non-ELF regular object. */
2694 if ((h->root.type == bfd_link_hash_defined
2695 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2696 && !h->def_regular
45d6a902
AM
2697 && (h->root.u.def.section->owner != NULL
2698 ? (bfd_get_flavour (h->root.u.def.section->owner)
2699 != bfd_target_elf_flavour)
2700 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2701 && !h->def_dynamic)))
2702 h->def_regular = 1;
45d6a902
AM
2703 }
2704
508c3946 2705 /* Backend specific symbol fixup. */
33774f08
AM
2706 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2707 if (bed->elf_backend_fixup_symbol
2708 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2709 return FALSE;
508c3946 2710
45d6a902
AM
2711 /* If this is a final link, and the symbol was defined as a common
2712 symbol in a regular object file, and there was no definition in
2713 any dynamic object, then the linker will have allocated space for
f5385ebf 2714 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2715 flag will not have been set. */
2716 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2717 && !h->def_regular
2718 && h->ref_regular
2719 && !h->def_dynamic
96f29d96 2720 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2721 h->def_regular = 1;
45d6a902 2722
4deb8f71
L
2723 /* If a weak undefined symbol has non-default visibility, we also
2724 hide it from the dynamic linker. */
2725 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2726 && h->root.type == bfd_link_hash_undefweak)
2727 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2728
2729 /* A hidden versioned symbol in executable should be forced local if
2730 it is is locally defined, not referenced by shared library and not
2731 exported. */
2732 else if (bfd_link_executable (eif->info)
2733 && h->versioned == versioned_hidden
2734 && !eif->info->export_dynamic
2735 && !h->dynamic
2736 && !h->ref_dynamic
2737 && h->def_regular)
2738 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2739
45d6a902
AM
2740 /* If -Bsymbolic was used (which means to bind references to global
2741 symbols to the definition within the shared object), and this
2742 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2743 need a PLT entry. Likewise, if the symbol has non-default
2744 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2745 will force it local. */
4deb8f71
L
2746 else if (h->needs_plt
2747 && bfd_link_pic (eif->info)
2748 && is_elf_hash_table (eif->info->hash)
2749 && (SYMBOLIC_BIND (eif->info, h)
2750 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2751 && h->def_regular)
45d6a902 2752 {
45d6a902
AM
2753 bfd_boolean force_local;
2754
45d6a902
AM
2755 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2756 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2757 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2758 }
2759
45d6a902
AM
2760 /* If this is a weak defined symbol in a dynamic object, and we know
2761 the real definition in the dynamic object, copy interesting flags
2762 over to the real definition. */
f6e332e6 2763 if (h->u.weakdef != NULL)
45d6a902 2764 {
45d6a902
AM
2765 /* If the real definition is defined by a regular object file,
2766 don't do anything special. See the longer description in
2767 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2768 if (h->u.weakdef->def_regular)
f6e332e6 2769 h->u.weakdef = NULL;
45d6a902 2770 else
a26587ba 2771 {
4e6b54a6
AM
2772 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2773
2774 while (h->root.type == bfd_link_hash_indirect)
2775 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2776
2777 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2778 || h->root.type == bfd_link_hash_defweak);
2779 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2780 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2781 || weakdef->root.type == bfd_link_hash_defweak);
2782 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2783 }
45d6a902
AM
2784 }
2785
2786 return TRUE;
2787}
2788
2789/* Make the backend pick a good value for a dynamic symbol. This is
2790 called via elf_link_hash_traverse, and also calls itself
2791 recursively. */
2792
28caa186 2793static bfd_boolean
268b6b39 2794_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2795{
a50b1753 2796 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2797 bfd *dynobj;
9c5bfbb7 2798 const struct elf_backend_data *bed;
45d6a902 2799
0eddce27 2800 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2801 return FALSE;
2802
45d6a902
AM
2803 /* Ignore indirect symbols. These are added by the versioning code. */
2804 if (h->root.type == bfd_link_hash_indirect)
2805 return TRUE;
2806
2807 /* Fix the symbol flags. */
2808 if (! _bfd_elf_fix_symbol_flags (h, eif))
2809 return FALSE;
2810
954b63d4
AM
2811 if (h->root.type == bfd_link_hash_undefweak)
2812 {
2813 if (eif->info->dynamic_undefined_weak == 0)
2814 _bfd_elf_link_hash_hide_symbol (eif->info, h, TRUE);
2815 else if (eif->info->dynamic_undefined_weak > 0
2816 && h->ref_regular
2817 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2818 && !bfd_hide_sym_by_version (eif->info->version_info,
2819 h->root.root.string))
2820 {
2821 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2822 {
2823 eif->failed = TRUE;
2824 return FALSE;
2825 }
2826 }
2827 }
2828
45d6a902
AM
2829 /* If this symbol does not require a PLT entry, and it is not
2830 defined by a dynamic object, or is not referenced by a regular
2831 object, ignore it. We do have to handle a weak defined symbol,
2832 even if no regular object refers to it, if we decided to add it
2833 to the dynamic symbol table. FIXME: Do we normally need to worry
2834 about symbols which are defined by one dynamic object and
2835 referenced by another one? */
f5385ebf 2836 if (!h->needs_plt
91e21fb7 2837 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2838 && (h->def_regular
2839 || !h->def_dynamic
2840 || (!h->ref_regular
f6e332e6 2841 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2842 {
a6aa5195 2843 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2844 return TRUE;
2845 }
2846
2847 /* If we've already adjusted this symbol, don't do it again. This
2848 can happen via a recursive call. */
f5385ebf 2849 if (h->dynamic_adjusted)
45d6a902
AM
2850 return TRUE;
2851
2852 /* Don't look at this symbol again. Note that we must set this
2853 after checking the above conditions, because we may look at a
2854 symbol once, decide not to do anything, and then get called
2855 recursively later after REF_REGULAR is set below. */
f5385ebf 2856 h->dynamic_adjusted = 1;
45d6a902
AM
2857
2858 /* If this is a weak definition, and we know a real definition, and
2859 the real symbol is not itself defined by a regular object file,
2860 then get a good value for the real definition. We handle the
2861 real symbol first, for the convenience of the backend routine.
2862
2863 Note that there is a confusing case here. If the real definition
2864 is defined by a regular object file, we don't get the real symbol
2865 from the dynamic object, but we do get the weak symbol. If the
2866 processor backend uses a COPY reloc, then if some routine in the
2867 dynamic object changes the real symbol, we will not see that
2868 change in the corresponding weak symbol. This is the way other
2869 ELF linkers work as well, and seems to be a result of the shared
2870 library model.
2871
2872 I will clarify this issue. Most SVR4 shared libraries define the
2873 variable _timezone and define timezone as a weak synonym. The
2874 tzset call changes _timezone. If you write
2875 extern int timezone;
2876 int _timezone = 5;
2877 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2878 you might expect that, since timezone is a synonym for _timezone,
2879 the same number will print both times. However, if the processor
2880 backend uses a COPY reloc, then actually timezone will be copied
2881 into your process image, and, since you define _timezone
2882 yourself, _timezone will not. Thus timezone and _timezone will
2883 wind up at different memory locations. The tzset call will set
2884 _timezone, leaving timezone unchanged. */
2885
f6e332e6 2886 if (h->u.weakdef != NULL)
45d6a902 2887 {
ec24dc88
AM
2888 /* If we get to this point, there is an implicit reference to
2889 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2890 h->u.weakdef->ref_regular = 1;
45d6a902 2891
ec24dc88
AM
2892 /* Ensure that the backend adjust_dynamic_symbol function sees
2893 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2894 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2895 return FALSE;
2896 }
2897
2898 /* If a symbol has no type and no size and does not require a PLT
2899 entry, then we are probably about to do the wrong thing here: we
2900 are probably going to create a COPY reloc for an empty object.
2901 This case can arise when a shared object is built with assembly
2902 code, and the assembly code fails to set the symbol type. */
2903 if (h->size == 0
2904 && h->type == STT_NOTYPE
f5385ebf 2905 && !h->needs_plt)
4eca0228 2906 _bfd_error_handler
45d6a902
AM
2907 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2908 h->root.root.string);
2909
2910 dynobj = elf_hash_table (eif->info)->dynobj;
2911 bed = get_elf_backend_data (dynobj);
e7c33416 2912
45d6a902
AM
2913 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2914 {
2915 eif->failed = TRUE;
2916 return FALSE;
2917 }
2918
2919 return TRUE;
2920}
2921
027297b7
L
2922/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2923 DYNBSS. */
2924
2925bfd_boolean
6cabe1ea
AM
2926_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2927 struct elf_link_hash_entry *h,
027297b7
L
2928 asection *dynbss)
2929{
91ac5911 2930 unsigned int power_of_two;
027297b7
L
2931 bfd_vma mask;
2932 asection *sec = h->root.u.def.section;
2933
2934 /* The section aligment of definition is the maximum alignment
91ac5911
L
2935 requirement of symbols defined in the section. Since we don't
2936 know the symbol alignment requirement, we start with the
2937 maximum alignment and check low bits of the symbol address
2938 for the minimum alignment. */
2939 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2940 mask = ((bfd_vma) 1 << power_of_two) - 1;
2941 while ((h->root.u.def.value & mask) != 0)
2942 {
2943 mask >>= 1;
2944 --power_of_two;
2945 }
027297b7 2946
91ac5911
L
2947 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2948 dynbss))
027297b7
L
2949 {
2950 /* Adjust the section alignment if needed. */
2951 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2952 power_of_two))
027297b7
L
2953 return FALSE;
2954 }
2955
91ac5911 2956 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2957 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2958
2959 /* Define the symbol as being at this point in DYNBSS. */
2960 h->root.u.def.section = dynbss;
2961 h->root.u.def.value = dynbss->size;
2962
2963 /* Increment the size of DYNBSS to make room for the symbol. */
2964 dynbss->size += h->size;
2965
f7483970
L
2966 /* No error if extern_protected_data is true. */
2967 if (h->protected_def
889c2a67
L
2968 && (!info->extern_protected_data
2969 || (info->extern_protected_data < 0
2970 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2971 info->callbacks->einfo
2972 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2973 h->root.root.string);
6cabe1ea 2974
027297b7
L
2975 return TRUE;
2976}
2977
45d6a902
AM
2978/* Adjust all external symbols pointing into SEC_MERGE sections
2979 to reflect the object merging within the sections. */
2980
28caa186 2981static bfd_boolean
268b6b39 2982_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2983{
2984 asection *sec;
2985
45d6a902
AM
2986 if ((h->root.type == bfd_link_hash_defined
2987 || h->root.type == bfd_link_hash_defweak)
2988 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2989 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2990 {
a50b1753 2991 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2992
2993 h->root.u.def.value =
2994 _bfd_merged_section_offset (output_bfd,
2995 &h->root.u.def.section,
2996 elf_section_data (sec)->sec_info,
753731ee 2997 h->root.u.def.value);
45d6a902
AM
2998 }
2999
3000 return TRUE;
3001}
986a241f
RH
3002
3003/* Returns false if the symbol referred to by H should be considered
3004 to resolve local to the current module, and true if it should be
3005 considered to bind dynamically. */
3006
3007bfd_boolean
268b6b39
AM
3008_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3009 struct bfd_link_info *info,
89a2ee5a 3010 bfd_boolean not_local_protected)
986a241f
RH
3011{
3012 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3013 const struct elf_backend_data *bed;
3014 struct elf_link_hash_table *hash_table;
986a241f
RH
3015
3016 if (h == NULL)
3017 return FALSE;
3018
3019 while (h->root.type == bfd_link_hash_indirect
3020 || h->root.type == bfd_link_hash_warning)
3021 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3022
3023 /* If it was forced local, then clearly it's not dynamic. */
3024 if (h->dynindx == -1)
3025 return FALSE;
f5385ebf 3026 if (h->forced_local)
986a241f
RH
3027 return FALSE;
3028
3029 /* Identify the cases where name binding rules say that a
3030 visible symbol resolves locally. */
0e1862bb
L
3031 binding_stays_local_p = (bfd_link_executable (info)
3032 || SYMBOLIC_BIND (info, h));
986a241f
RH
3033
3034 switch (ELF_ST_VISIBILITY (h->other))
3035 {
3036 case STV_INTERNAL:
3037 case STV_HIDDEN:
3038 return FALSE;
3039
3040 case STV_PROTECTED:
fcb93ecf
PB
3041 hash_table = elf_hash_table (info);
3042 if (!is_elf_hash_table (hash_table))
3043 return FALSE;
3044
3045 bed = get_elf_backend_data (hash_table->dynobj);
3046
986a241f
RH
3047 /* Proper resolution for function pointer equality may require
3048 that these symbols perhaps be resolved dynamically, even though
3049 we should be resolving them to the current module. */
89a2ee5a 3050 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3051 binding_stays_local_p = TRUE;
3052 break;
3053
3054 default:
986a241f
RH
3055 break;
3056 }
3057
aa37626c 3058 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3059 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3060 return TRUE;
3061
986a241f
RH
3062 /* Otherwise, the symbol is dynamic if binding rules don't tell
3063 us that it remains local. */
3064 return !binding_stays_local_p;
3065}
f6c52c13
AM
3066
3067/* Return true if the symbol referred to by H should be considered
3068 to resolve local to the current module, and false otherwise. Differs
3069 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3070 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3071 for the place where dynindx == -1 is tested. If that test is true,
3072 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3073 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3074 defined symbols.
89a2ee5a
AM
3075 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3076 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3077 treatment of undefined weak symbols. For those that do not make
3078 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3079
3080bfd_boolean
268b6b39
AM
3081_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3082 struct bfd_link_info *info,
3083 bfd_boolean local_protected)
f6c52c13 3084{
fcb93ecf
PB
3085 const struct elf_backend_data *bed;
3086 struct elf_link_hash_table *hash_table;
3087
f6c52c13
AM
3088 /* If it's a local sym, of course we resolve locally. */
3089 if (h == NULL)
3090 return TRUE;
3091
d95edcac
L
3092 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3093 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3094 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3095 return TRUE;
3096
0fad2956
MR
3097 /* Forced local symbols resolve locally. */
3098 if (h->forced_local)
3099 return TRUE;
3100
7e2294f9
AO
3101 /* Common symbols that become definitions don't get the DEF_REGULAR
3102 flag set, so test it first, and don't bail out. */
3103 if (ELF_COMMON_DEF_P (h))
3104 /* Do nothing. */;
f6c52c13 3105 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3106 resolve locally. The sym is either undefined or dynamic. */
3107 else if (!h->def_regular)
f6c52c13
AM
3108 return FALSE;
3109
0fad2956 3110 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3111 if (h->dynindx == -1)
3112 return TRUE;
3113
3114 /* At this point, we know the symbol is defined and dynamic. In an
3115 executable it must resolve locally, likewise when building symbolic
3116 shared libraries. */
0e1862bb 3117 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3118 return TRUE;
3119
3120 /* Now deal with defined dynamic symbols in shared libraries. Ones
3121 with default visibility might not resolve locally. */
3122 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3123 return FALSE;
3124
fcb93ecf
PB
3125 hash_table = elf_hash_table (info);
3126 if (!is_elf_hash_table (hash_table))
3127 return TRUE;
3128
3129 bed = get_elf_backend_data (hash_table->dynobj);
3130
f7483970
L
3131 /* If extern_protected_data is false, STV_PROTECTED non-function
3132 symbols are local. */
889c2a67
L
3133 if ((!info->extern_protected_data
3134 || (info->extern_protected_data < 0
3135 && !bed->extern_protected_data))
3136 && !bed->is_function_type (h->type))
1c16dfa5
L
3137 return TRUE;
3138
f6c52c13 3139 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3140 symbols be treated as dynamic symbols. If the address of a
3141 function not defined in an executable is set to that function's
3142 plt entry in the executable, then the address of the function in
3143 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3144 return local_protected;
3145}
e1918d23
AM
3146
3147/* Caches some TLS segment info, and ensures that the TLS segment vma is
3148 aligned. Returns the first TLS output section. */
3149
3150struct bfd_section *
3151_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3152{
3153 struct bfd_section *sec, *tls;
3154 unsigned int align = 0;
3155
3156 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3157 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3158 break;
3159 tls = sec;
3160
3161 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3162 if (sec->alignment_power > align)
3163 align = sec->alignment_power;
3164
3165 elf_hash_table (info)->tls_sec = tls;
3166
3167 /* Ensure the alignment of the first section is the largest alignment,
3168 so that the tls segment starts aligned. */
3169 if (tls != NULL)
3170 tls->alignment_power = align;
3171
3172 return tls;
3173}
0ad989f9
L
3174
3175/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3176static bfd_boolean
3177is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3178 Elf_Internal_Sym *sym)
3179{
a4d8e49b
L
3180 const struct elf_backend_data *bed;
3181
0ad989f9
L
3182 /* Local symbols do not count, but target specific ones might. */
3183 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3184 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3185 return FALSE;
3186
fcb93ecf 3187 bed = get_elf_backend_data (abfd);
0ad989f9 3188 /* Function symbols do not count. */
fcb93ecf 3189 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3190 return FALSE;
3191
3192 /* If the section is undefined, then so is the symbol. */
3193 if (sym->st_shndx == SHN_UNDEF)
3194 return FALSE;
3195
3196 /* If the symbol is defined in the common section, then
3197 it is a common definition and so does not count. */
a4d8e49b 3198 if (bed->common_definition (sym))
0ad989f9
L
3199 return FALSE;
3200
3201 /* If the symbol is in a target specific section then we
3202 must rely upon the backend to tell us what it is. */
3203 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3204 /* FIXME - this function is not coded yet:
3205
3206 return _bfd_is_global_symbol_definition (abfd, sym);
3207
3208 Instead for now assume that the definition is not global,
3209 Even if this is wrong, at least the linker will behave
3210 in the same way that it used to do. */
3211 return FALSE;
3212
3213 return TRUE;
3214}
3215
3216/* Search the symbol table of the archive element of the archive ABFD
3217 whose archive map contains a mention of SYMDEF, and determine if
3218 the symbol is defined in this element. */
3219static bfd_boolean
3220elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3221{
3222 Elf_Internal_Shdr * hdr;
ef53be89
AM
3223 size_t symcount;
3224 size_t extsymcount;
3225 size_t extsymoff;
0ad989f9
L
3226 Elf_Internal_Sym *isymbuf;
3227 Elf_Internal_Sym *isym;
3228 Elf_Internal_Sym *isymend;
3229 bfd_boolean result;
3230
3231 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3232 if (abfd == NULL)
3233 return FALSE;
3234
3235 if (! bfd_check_format (abfd, bfd_object))
3236 return FALSE;
3237
7dc3990e
L
3238 /* Select the appropriate symbol table. If we don't know if the
3239 object file is an IR object, give linker LTO plugin a chance to
3240 get the correct symbol table. */
3241 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3242#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3243 || (abfd->plugin_format == bfd_plugin_unknown
3244 && bfd_link_plugin_object_p (abfd))
3245#endif
3246 )
3247 {
3248 /* Use the IR symbol table if the object has been claimed by
3249 plugin. */
3250 abfd = abfd->plugin_dummy_bfd;
3251 hdr = &elf_tdata (abfd)->symtab_hdr;
3252 }
3253 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3254 hdr = &elf_tdata (abfd)->symtab_hdr;
3255 else
3256 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3257
3258 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3259
3260 /* The sh_info field of the symtab header tells us where the
3261 external symbols start. We don't care about the local symbols. */
3262 if (elf_bad_symtab (abfd))
3263 {
3264 extsymcount = symcount;
3265 extsymoff = 0;
3266 }
3267 else
3268 {
3269 extsymcount = symcount - hdr->sh_info;
3270 extsymoff = hdr->sh_info;
3271 }
3272
3273 if (extsymcount == 0)
3274 return FALSE;
3275
3276 /* Read in the symbol table. */
3277 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3278 NULL, NULL, NULL);
3279 if (isymbuf == NULL)
3280 return FALSE;
3281
3282 /* Scan the symbol table looking for SYMDEF. */
3283 result = FALSE;
3284 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3285 {
3286 const char *name;
3287
3288 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3289 isym->st_name);
3290 if (name == NULL)
3291 break;
3292
3293 if (strcmp (name, symdef->name) == 0)
3294 {
3295 result = is_global_data_symbol_definition (abfd, isym);
3296 break;
3297 }
3298 }
3299
3300 free (isymbuf);
3301
3302 return result;
3303}
3304\f
5a580b3a
AM
3305/* Add an entry to the .dynamic table. */
3306
3307bfd_boolean
3308_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3309 bfd_vma tag,
3310 bfd_vma val)
3311{
3312 struct elf_link_hash_table *hash_table;
3313 const struct elf_backend_data *bed;
3314 asection *s;
3315 bfd_size_type newsize;
3316 bfd_byte *newcontents;
3317 Elf_Internal_Dyn dyn;
3318
3319 hash_table = elf_hash_table (info);
3320 if (! is_elf_hash_table (hash_table))
3321 return FALSE;
3322
3323 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3324 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3325 BFD_ASSERT (s != NULL);
3326
eea6121a 3327 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3328 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3329 if (newcontents == NULL)
3330 return FALSE;
3331
3332 dyn.d_tag = tag;
3333 dyn.d_un.d_val = val;
eea6121a 3334 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3335
eea6121a 3336 s->size = newsize;
5a580b3a
AM
3337 s->contents = newcontents;
3338
3339 return TRUE;
3340}
3341
3342/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3343 otherwise just check whether one already exists. Returns -1 on error,
3344 1 if a DT_NEEDED tag already exists, and 0 on success. */
3345
4ad4eba5 3346static int
7e9f0867
AM
3347elf_add_dt_needed_tag (bfd *abfd,
3348 struct bfd_link_info *info,
4ad4eba5
AM
3349 const char *soname,
3350 bfd_boolean do_it)
5a580b3a
AM
3351{
3352 struct elf_link_hash_table *hash_table;
ef53be89 3353 size_t strindex;
5a580b3a 3354
7e9f0867
AM
3355 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3356 return -1;
3357
5a580b3a 3358 hash_table = elf_hash_table (info);
5a580b3a 3359 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3360 if (strindex == (size_t) -1)
5a580b3a
AM
3361 return -1;
3362
02be4619 3363 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3364 {
3365 asection *sdyn;
3366 const struct elf_backend_data *bed;
3367 bfd_byte *extdyn;
3368
3369 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3370 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3371 if (sdyn != NULL)
3372 for (extdyn = sdyn->contents;
3373 extdyn < sdyn->contents + sdyn->size;
3374 extdyn += bed->s->sizeof_dyn)
3375 {
3376 Elf_Internal_Dyn dyn;
5a580b3a 3377
7e9f0867
AM
3378 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3379 if (dyn.d_tag == DT_NEEDED
3380 && dyn.d_un.d_val == strindex)
3381 {
3382 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3383 return 1;
3384 }
3385 }
5a580b3a
AM
3386 }
3387
3388 if (do_it)
3389 {
7e9f0867
AM
3390 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3391 return -1;
3392
5a580b3a
AM
3393 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3394 return -1;
3395 }
3396 else
3397 /* We were just checking for existence of the tag. */
3398 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3399
3400 return 0;
3401}
3402
7b15fa7a
AM
3403/* Return true if SONAME is on the needed list between NEEDED and STOP
3404 (or the end of list if STOP is NULL), and needed by a library that
3405 will be loaded. */
3406
010e5ae2 3407static bfd_boolean
7b15fa7a
AM
3408on_needed_list (const char *soname,
3409 struct bfd_link_needed_list *needed,
3410 struct bfd_link_needed_list *stop)
010e5ae2 3411{
7b15fa7a
AM
3412 struct bfd_link_needed_list *look;
3413 for (look = needed; look != stop; look = look->next)
3414 if (strcmp (soname, look->name) == 0
3415 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3416 /* If needed by a library that itself is not directly
3417 needed, recursively check whether that library is
3418 indirectly needed. Since we add DT_NEEDED entries to
3419 the end of the list, library dependencies appear after
3420 the library. Therefore search prior to the current
3421 LOOK, preventing possible infinite recursion. */
3422 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3423 return TRUE;
3424
3425 return FALSE;
3426}
3427
14160578 3428/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3429static int
3430elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3431{
3432 const struct elf_link_hash_entry *h1;
3433 const struct elf_link_hash_entry *h2;
10b7e05b 3434 bfd_signed_vma vdiff;
5a580b3a
AM
3435
3436 h1 = *(const struct elf_link_hash_entry **) arg1;
3437 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3438 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3439 if (vdiff != 0)
3440 return vdiff > 0 ? 1 : -1;
3441 else
3442 {
d3435ae8 3443 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3444 if (sdiff != 0)
3445 return sdiff > 0 ? 1 : -1;
3446 }
14160578
AM
3447 vdiff = h1->size - h2->size;
3448 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3449}
4ad4eba5 3450
5a580b3a
AM
3451/* This function is used to adjust offsets into .dynstr for
3452 dynamic symbols. This is called via elf_link_hash_traverse. */
3453
3454static bfd_boolean
3455elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3456{
a50b1753 3457 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3458
5a580b3a
AM
3459 if (h->dynindx != -1)
3460 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3461 return TRUE;
3462}
3463
3464/* Assign string offsets in .dynstr, update all structures referencing
3465 them. */
3466
4ad4eba5
AM
3467static bfd_boolean
3468elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3469{
3470 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3471 struct elf_link_local_dynamic_entry *entry;
3472 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3473 bfd *dynobj = hash_table->dynobj;
3474 asection *sdyn;
3475 bfd_size_type size;
3476 const struct elf_backend_data *bed;
3477 bfd_byte *extdyn;
3478
3479 _bfd_elf_strtab_finalize (dynstr);
3480 size = _bfd_elf_strtab_size (dynstr);
3481
3482 bed = get_elf_backend_data (dynobj);
3d4d4302 3483 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3484 BFD_ASSERT (sdyn != NULL);
3485
3486 /* Update all .dynamic entries referencing .dynstr strings. */
3487 for (extdyn = sdyn->contents;
eea6121a 3488 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3489 extdyn += bed->s->sizeof_dyn)
3490 {
3491 Elf_Internal_Dyn dyn;
3492
3493 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3494 switch (dyn.d_tag)
3495 {
3496 case DT_STRSZ:
3497 dyn.d_un.d_val = size;
3498 break;
3499 case DT_NEEDED:
3500 case DT_SONAME:
3501 case DT_RPATH:
3502 case DT_RUNPATH:
3503 case DT_FILTER:
3504 case DT_AUXILIARY:
7ee314fa
AM
3505 case DT_AUDIT:
3506 case DT_DEPAUDIT:
5a580b3a
AM
3507 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3508 break;
3509 default:
3510 continue;
3511 }
3512 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3513 }
3514
3515 /* Now update local dynamic symbols. */
3516 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3517 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3518 entry->isym.st_name);
3519
3520 /* And the rest of dynamic symbols. */
3521 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3522
3523 /* Adjust version definitions. */
3524 if (elf_tdata (output_bfd)->cverdefs)
3525 {
3526 asection *s;
3527 bfd_byte *p;
ef53be89 3528 size_t i;
5a580b3a
AM
3529 Elf_Internal_Verdef def;
3530 Elf_Internal_Verdaux defaux;
3531
3d4d4302 3532 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3533 p = s->contents;
3534 do
3535 {
3536 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3537 &def);
3538 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3539 if (def.vd_aux != sizeof (Elf_External_Verdef))
3540 continue;
5a580b3a
AM
3541 for (i = 0; i < def.vd_cnt; ++i)
3542 {
3543 _bfd_elf_swap_verdaux_in (output_bfd,
3544 (Elf_External_Verdaux *) p, &defaux);
3545 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3546 defaux.vda_name);
3547 _bfd_elf_swap_verdaux_out (output_bfd,
3548 &defaux, (Elf_External_Verdaux *) p);
3549 p += sizeof (Elf_External_Verdaux);
3550 }
3551 }
3552 while (def.vd_next);
3553 }
3554
3555 /* Adjust version references. */
3556 if (elf_tdata (output_bfd)->verref)
3557 {
3558 asection *s;
3559 bfd_byte *p;
ef53be89 3560 size_t i;
5a580b3a
AM
3561 Elf_Internal_Verneed need;
3562 Elf_Internal_Vernaux needaux;
3563
3d4d4302 3564 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3565 p = s->contents;
3566 do
3567 {
3568 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3569 &need);
3570 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3571 _bfd_elf_swap_verneed_out (output_bfd, &need,
3572 (Elf_External_Verneed *) p);
3573 p += sizeof (Elf_External_Verneed);
3574 for (i = 0; i < need.vn_cnt; ++i)
3575 {
3576 _bfd_elf_swap_vernaux_in (output_bfd,
3577 (Elf_External_Vernaux *) p, &needaux);
3578 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3579 needaux.vna_name);
3580 _bfd_elf_swap_vernaux_out (output_bfd,
3581 &needaux,
3582 (Elf_External_Vernaux *) p);
3583 p += sizeof (Elf_External_Vernaux);
3584 }
3585 }
3586 while (need.vn_next);
3587 }
3588
3589 return TRUE;
3590}
3591\f
13285a1b
AM
3592/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3593 The default is to only match when the INPUT and OUTPUT are exactly
3594 the same target. */
3595
3596bfd_boolean
3597_bfd_elf_default_relocs_compatible (const bfd_target *input,
3598 const bfd_target *output)
3599{
3600 return input == output;
3601}
3602
3603/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3604 This version is used when different targets for the same architecture
3605 are virtually identical. */
3606
3607bfd_boolean
3608_bfd_elf_relocs_compatible (const bfd_target *input,
3609 const bfd_target *output)
3610{
3611 const struct elf_backend_data *obed, *ibed;
3612
3613 if (input == output)
3614 return TRUE;
3615
3616 ibed = xvec_get_elf_backend_data (input);
3617 obed = xvec_get_elf_backend_data (output);
3618
3619 if (ibed->arch != obed->arch)
3620 return FALSE;
3621
3622 /* If both backends are using this function, deem them compatible. */
3623 return ibed->relocs_compatible == obed->relocs_compatible;
3624}
3625
e5034e59
AM
3626/* Make a special call to the linker "notice" function to tell it that
3627 we are about to handle an as-needed lib, or have finished
1b786873 3628 processing the lib. */
e5034e59
AM
3629
3630bfd_boolean
3631_bfd_elf_notice_as_needed (bfd *ibfd,
3632 struct bfd_link_info *info,
3633 enum notice_asneeded_action act)
3634{
46135103 3635 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3636}
3637
d9689752
L
3638/* Check relocations an ELF object file. */
3639
3640bfd_boolean
3641_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3642{
3643 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3644 struct elf_link_hash_table *htab = elf_hash_table (info);
3645
3646 /* If this object is the same format as the output object, and it is
3647 not a shared library, then let the backend look through the
3648 relocs.
3649
3650 This is required to build global offset table entries and to
3651 arrange for dynamic relocs. It is not required for the
3652 particular common case of linking non PIC code, even when linking
3653 against shared libraries, but unfortunately there is no way of
3654 knowing whether an object file has been compiled PIC or not.
3655 Looking through the relocs is not particularly time consuming.
3656 The problem is that we must either (1) keep the relocs in memory,
3657 which causes the linker to require additional runtime memory or
3658 (2) read the relocs twice from the input file, which wastes time.
3659 This would be a good case for using mmap.
3660
3661 I have no idea how to handle linking PIC code into a file of a
3662 different format. It probably can't be done. */
3663 if ((abfd->flags & DYNAMIC) == 0
3664 && is_elf_hash_table (htab)
3665 && bed->check_relocs != NULL
3666 && elf_object_id (abfd) == elf_hash_table_id (htab)
3667 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3668 {
3669 asection *o;
3670
3671 for (o = abfd->sections; o != NULL; o = o->next)
3672 {
3673 Elf_Internal_Rela *internal_relocs;
3674 bfd_boolean ok;
3675
5ce03cea 3676 /* Don't check relocations in excluded sections. */
d9689752 3677 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3678 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3679 || o->reloc_count == 0
3680 || ((info->strip == strip_all || info->strip == strip_debugger)
3681 && (o->flags & SEC_DEBUGGING) != 0)
3682 || bfd_is_abs_section (o->output_section))
3683 continue;
3684
3685 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3686 info->keep_memory);
3687 if (internal_relocs == NULL)
3688 return FALSE;
3689
3690 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3691
3692 if (elf_section_data (o)->relocs != internal_relocs)
3693 free (internal_relocs);
3694
3695 if (! ok)
3696 return FALSE;
3697 }
3698 }
3699
3700 return TRUE;
3701}
3702
4ad4eba5
AM
3703/* Add symbols from an ELF object file to the linker hash table. */
3704
3705static bfd_boolean
3706elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3707{
a0c402a5 3708 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3709 Elf_Internal_Shdr *hdr;
ef53be89
AM
3710 size_t symcount;
3711 size_t extsymcount;
3712 size_t extsymoff;
4ad4eba5
AM
3713 struct elf_link_hash_entry **sym_hash;
3714 bfd_boolean dynamic;
3715 Elf_External_Versym *extversym = NULL;
3716 Elf_External_Versym *ever;
3717 struct elf_link_hash_entry *weaks;
3718 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3719 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3720 Elf_Internal_Sym *isymbuf = NULL;
3721 Elf_Internal_Sym *isym;
3722 Elf_Internal_Sym *isymend;
3723 const struct elf_backend_data *bed;
3724 bfd_boolean add_needed;
66eb6687 3725 struct elf_link_hash_table *htab;
4ad4eba5 3726 bfd_size_type amt;
66eb6687 3727 void *alloc_mark = NULL;
4f87808c
AM
3728 struct bfd_hash_entry **old_table = NULL;
3729 unsigned int old_size = 0;
3730 unsigned int old_count = 0;
66eb6687 3731 void *old_tab = NULL;
66eb6687
AM
3732 void *old_ent;
3733 struct bfd_link_hash_entry *old_undefs = NULL;
3734 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3735 void *old_strtab = NULL;
66eb6687 3736 size_t tabsize = 0;
db6a5d5f 3737 asection *s;
29a9f53e 3738 bfd_boolean just_syms;
4ad4eba5 3739
66eb6687 3740 htab = elf_hash_table (info);
4ad4eba5 3741 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3742
3743 if ((abfd->flags & DYNAMIC) == 0)
3744 dynamic = FALSE;
3745 else
3746 {
3747 dynamic = TRUE;
3748
3749 /* You can't use -r against a dynamic object. Also, there's no
3750 hope of using a dynamic object which does not exactly match
3751 the format of the output file. */
0e1862bb 3752 if (bfd_link_relocatable (info)
66eb6687 3753 || !is_elf_hash_table (htab)
f13a99db 3754 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3755 {
0e1862bb 3756 if (bfd_link_relocatable (info))
9a0789ec
NC
3757 bfd_set_error (bfd_error_invalid_operation);
3758 else
3759 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3760 goto error_return;
3761 }
3762 }
3763
a0c402a5
L
3764 ehdr = elf_elfheader (abfd);
3765 if (info->warn_alternate_em
3766 && bed->elf_machine_code != ehdr->e_machine
3767 && ((bed->elf_machine_alt1 != 0
3768 && ehdr->e_machine == bed->elf_machine_alt1)
3769 || (bed->elf_machine_alt2 != 0
3770 && ehdr->e_machine == bed->elf_machine_alt2)))
3771 info->callbacks->einfo
695344c0 3772 /* xgettext:c-format */
a0c402a5
L
3773 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3774 ehdr->e_machine, abfd, bed->elf_machine_code);
3775
4ad4eba5
AM
3776 /* As a GNU extension, any input sections which are named
3777 .gnu.warning.SYMBOL are treated as warning symbols for the given
3778 symbol. This differs from .gnu.warning sections, which generate
3779 warnings when they are included in an output file. */
dd98f8d2 3780 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3781 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3782 {
db6a5d5f 3783 const char *name;
4ad4eba5 3784
db6a5d5f
AM
3785 name = bfd_get_section_name (abfd, s);
3786 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3787 {
db6a5d5f
AM
3788 char *msg;
3789 bfd_size_type sz;
3790
3791 name += sizeof ".gnu.warning." - 1;
3792
3793 /* If this is a shared object, then look up the symbol
3794 in the hash table. If it is there, and it is already
3795 been defined, then we will not be using the entry
3796 from this shared object, so we don't need to warn.
3797 FIXME: If we see the definition in a regular object
3798 later on, we will warn, but we shouldn't. The only
3799 fix is to keep track of what warnings we are supposed
3800 to emit, and then handle them all at the end of the
3801 link. */
3802 if (dynamic)
4ad4eba5 3803 {
db6a5d5f
AM
3804 struct elf_link_hash_entry *h;
3805
3806 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3807
3808 /* FIXME: What about bfd_link_hash_common? */
3809 if (h != NULL
3810 && (h->root.type == bfd_link_hash_defined
3811 || h->root.type == bfd_link_hash_defweak))
3812 continue;
3813 }
4ad4eba5 3814
db6a5d5f
AM
3815 sz = s->size;
3816 msg = (char *) bfd_alloc (abfd, sz + 1);
3817 if (msg == NULL)
3818 goto error_return;
4ad4eba5 3819
db6a5d5f
AM
3820 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3821 goto error_return;
4ad4eba5 3822
db6a5d5f 3823 msg[sz] = '\0';
4ad4eba5 3824
db6a5d5f
AM
3825 if (! (_bfd_generic_link_add_one_symbol
3826 (info, abfd, name, BSF_WARNING, s, 0, msg,
3827 FALSE, bed->collect, NULL)))
3828 goto error_return;
4ad4eba5 3829
0e1862bb 3830 if (bfd_link_executable (info))
db6a5d5f
AM
3831 {
3832 /* Clobber the section size so that the warning does
3833 not get copied into the output file. */
3834 s->size = 0;
11d2f718 3835
db6a5d5f
AM
3836 /* Also set SEC_EXCLUDE, so that symbols defined in
3837 the warning section don't get copied to the output. */
3838 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3839 }
3840 }
3841 }
3842
29a9f53e
L
3843 just_syms = ((s = abfd->sections) != NULL
3844 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3845
4ad4eba5
AM
3846 add_needed = TRUE;
3847 if (! dynamic)
3848 {
3849 /* If we are creating a shared library, create all the dynamic
3850 sections immediately. We need to attach them to something,
3851 so we attach them to this BFD, provided it is the right
bf89386a
L
3852 format and is not from ld --just-symbols. Always create the
3853 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3854 are no input BFD's of the same format as the output, we can't
3855 make a shared library. */
3856 if (!just_syms
bf89386a 3857 && (bfd_link_pic (info)
9c1d7a08 3858 || (!bfd_link_relocatable (info)
3c5fce9b 3859 && info->nointerp
9c1d7a08 3860 && (info->export_dynamic || info->dynamic)))
66eb6687 3861 && is_elf_hash_table (htab)
f13a99db 3862 && info->output_bfd->xvec == abfd->xvec
66eb6687 3863 && !htab->dynamic_sections_created)
4ad4eba5
AM
3864 {
3865 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3866 goto error_return;
3867 }
3868 }
66eb6687 3869 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3870 goto error_return;
3871 else
3872 {
4ad4eba5 3873 const char *soname = NULL;
7ee314fa 3874 char *audit = NULL;
4ad4eba5 3875 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3876 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3877 int ret;
3878
3879 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3880 ld shouldn't allow it. */
29a9f53e 3881 if (just_syms)
92fd189d 3882 abort ();
4ad4eba5
AM
3883
3884 /* If this dynamic lib was specified on the command line with
3885 --as-needed in effect, then we don't want to add a DT_NEEDED
3886 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3887 in by another lib's DT_NEEDED. When --no-add-needed is used
3888 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3889 any dynamic library in DT_NEEDED tags in the dynamic lib at
3890 all. */
3891 add_needed = (elf_dyn_lib_class (abfd)
3892 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3893 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3894
3895 s = bfd_get_section_by_name (abfd, ".dynamic");
3896 if (s != NULL)
3897 {
3898 bfd_byte *dynbuf;
3899 bfd_byte *extdyn;
cb33740c 3900 unsigned int elfsec;
4ad4eba5
AM
3901 unsigned long shlink;
3902
eea6121a 3903 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3904 {
3905error_free_dyn:
3906 free (dynbuf);
3907 goto error_return;
3908 }
4ad4eba5
AM
3909
3910 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3911 if (elfsec == SHN_BAD)
4ad4eba5
AM
3912 goto error_free_dyn;
3913 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3914
3915 for (extdyn = dynbuf;
eea6121a 3916 extdyn < dynbuf + s->size;
4ad4eba5
AM
3917 extdyn += bed->s->sizeof_dyn)
3918 {
3919 Elf_Internal_Dyn dyn;
3920
3921 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3922 if (dyn.d_tag == DT_SONAME)
3923 {
3924 unsigned int tagv = dyn.d_un.d_val;
3925 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3926 if (soname == NULL)
3927 goto error_free_dyn;
3928 }
3929 if (dyn.d_tag == DT_NEEDED)
3930 {
3931 struct bfd_link_needed_list *n, **pn;
3932 char *fnm, *anm;
3933 unsigned int tagv = dyn.d_un.d_val;
3934
3935 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3936 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3937 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3938 if (n == NULL || fnm == NULL)
3939 goto error_free_dyn;
3940 amt = strlen (fnm) + 1;
a50b1753 3941 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3942 if (anm == NULL)
3943 goto error_free_dyn;
3944 memcpy (anm, fnm, amt);
3945 n->name = anm;
3946 n->by = abfd;
3947 n->next = NULL;
66eb6687 3948 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3949 ;
3950 *pn = n;
3951 }
3952 if (dyn.d_tag == DT_RUNPATH)
3953 {
3954 struct bfd_link_needed_list *n, **pn;
3955 char *fnm, *anm;
3956 unsigned int tagv = dyn.d_un.d_val;
3957
3958 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3959 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3960 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3961 if (n == NULL || fnm == NULL)
3962 goto error_free_dyn;
3963 amt = strlen (fnm) + 1;
a50b1753 3964 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3965 if (anm == NULL)
3966 goto error_free_dyn;
3967 memcpy (anm, fnm, amt);
3968 n->name = anm;
3969 n->by = abfd;
3970 n->next = NULL;
3971 for (pn = & runpath;
3972 *pn != NULL;
3973 pn = &(*pn)->next)
3974 ;
3975 *pn = n;
3976 }
3977 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3978 if (!runpath && dyn.d_tag == DT_RPATH)
3979 {
3980 struct bfd_link_needed_list *n, **pn;
3981 char *fnm, *anm;
3982 unsigned int tagv = dyn.d_un.d_val;
3983
3984 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3985 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3986 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3987 if (n == NULL || fnm == NULL)
3988 goto error_free_dyn;
3989 amt = strlen (fnm) + 1;
a50b1753 3990 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3991 if (anm == NULL)
f8703194 3992 goto error_free_dyn;
4ad4eba5
AM
3993 memcpy (anm, fnm, amt);
3994 n->name = anm;
3995 n->by = abfd;
3996 n->next = NULL;
3997 for (pn = & rpath;
3998 *pn != NULL;
3999 pn = &(*pn)->next)
4000 ;
4001 *pn = n;
4002 }
7ee314fa
AM
4003 if (dyn.d_tag == DT_AUDIT)
4004 {
4005 unsigned int tagv = dyn.d_un.d_val;
4006 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4007 }
4ad4eba5
AM
4008 }
4009
4010 free (dynbuf);
4011 }
4012
4013 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4014 frees all more recently bfd_alloc'd blocks as well. */
4015 if (runpath)
4016 rpath = runpath;
4017
4018 if (rpath)
4019 {
4020 struct bfd_link_needed_list **pn;
66eb6687 4021 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4022 ;
4023 *pn = rpath;
4024 }
4025
9acc85a6
AM
4026 /* If we have a PT_GNU_RELRO program header, mark as read-only
4027 all sections contained fully therein. This makes relro
4028 shared library sections appear as they will at run-time. */
4029 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4030 while (--phdr >= elf_tdata (abfd)->phdr)
4031 if (phdr->p_type == PT_GNU_RELRO)
4032 {
4033 for (s = abfd->sections; s != NULL; s = s->next)
4034 if ((s->flags & SEC_ALLOC) != 0
4035 && s->vma >= phdr->p_vaddr
4036 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4037 s->flags |= SEC_READONLY;
4038 break;
4039 }
4040
4ad4eba5
AM
4041 /* We do not want to include any of the sections in a dynamic
4042 object in the output file. We hack by simply clobbering the
4043 list of sections in the BFD. This could be handled more
4044 cleanly by, say, a new section flag; the existing
4045 SEC_NEVER_LOAD flag is not the one we want, because that one
4046 still implies that the section takes up space in the output
4047 file. */
4048 bfd_section_list_clear (abfd);
4049
4ad4eba5
AM
4050 /* Find the name to use in a DT_NEEDED entry that refers to this
4051 object. If the object has a DT_SONAME entry, we use it.
4052 Otherwise, if the generic linker stuck something in
4053 elf_dt_name, we use that. Otherwise, we just use the file
4054 name. */
4055 if (soname == NULL || *soname == '\0')
4056 {
4057 soname = elf_dt_name (abfd);
4058 if (soname == NULL || *soname == '\0')
4059 soname = bfd_get_filename (abfd);
4060 }
4061
4062 /* Save the SONAME because sometimes the linker emulation code
4063 will need to know it. */
4064 elf_dt_name (abfd) = soname;
4065
7e9f0867 4066 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4067 if (ret < 0)
4068 goto error_return;
4069
4070 /* If we have already included this dynamic object in the
4071 link, just ignore it. There is no reason to include a
4072 particular dynamic object more than once. */
4073 if (ret > 0)
4074 return TRUE;
7ee314fa
AM
4075
4076 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4077 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4078 }
4079
4080 /* If this is a dynamic object, we always link against the .dynsym
4081 symbol table, not the .symtab symbol table. The dynamic linker
4082 will only see the .dynsym symbol table, so there is no reason to
4083 look at .symtab for a dynamic object. */
4084
4085 if (! dynamic || elf_dynsymtab (abfd) == 0)
4086 hdr = &elf_tdata (abfd)->symtab_hdr;
4087 else
4088 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4089
4090 symcount = hdr->sh_size / bed->s->sizeof_sym;
4091
4092 /* The sh_info field of the symtab header tells us where the
4093 external symbols start. We don't care about the local symbols at
4094 this point. */
4095 if (elf_bad_symtab (abfd))
4096 {
4097 extsymcount = symcount;
4098 extsymoff = 0;
4099 }
4100 else
4101 {
4102 extsymcount = symcount - hdr->sh_info;
4103 extsymoff = hdr->sh_info;
4104 }
4105
f45794cb 4106 sym_hash = elf_sym_hashes (abfd);
012b2306 4107 if (extsymcount != 0)
4ad4eba5
AM
4108 {
4109 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4110 NULL, NULL, NULL);
4111 if (isymbuf == NULL)
4112 goto error_return;
4113
4ad4eba5 4114 if (sym_hash == NULL)
012b2306
AM
4115 {
4116 /* We store a pointer to the hash table entry for each
4117 external symbol. */
ef53be89
AM
4118 amt = extsymcount;
4119 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4120 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4121 if (sym_hash == NULL)
4122 goto error_free_sym;
4123 elf_sym_hashes (abfd) = sym_hash;
4124 }
4ad4eba5
AM
4125 }
4126
4127 if (dynamic)
4128 {
4129 /* Read in any version definitions. */
fc0e6df6
PB
4130 if (!_bfd_elf_slurp_version_tables (abfd,
4131 info->default_imported_symver))
4ad4eba5
AM
4132 goto error_free_sym;
4133
4134 /* Read in the symbol versions, but don't bother to convert them
4135 to internal format. */
4136 if (elf_dynversym (abfd) != 0)
4137 {
4138 Elf_Internal_Shdr *versymhdr;
4139
4140 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4141 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4142 if (extversym == NULL)
4143 goto error_free_sym;
4144 amt = versymhdr->sh_size;
4145 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4146 || bfd_bread (extversym, amt, abfd) != amt)
4147 goto error_free_vers;
4148 }
4149 }
4150
66eb6687
AM
4151 /* If we are loading an as-needed shared lib, save the symbol table
4152 state before we start adding symbols. If the lib turns out
4153 to be unneeded, restore the state. */
4154 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4155 {
4156 unsigned int i;
4157 size_t entsize;
4158
4159 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4160 {
4161 struct bfd_hash_entry *p;
2de92251 4162 struct elf_link_hash_entry *h;
66eb6687
AM
4163
4164 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4165 {
4166 h = (struct elf_link_hash_entry *) p;
4167 entsize += htab->root.table.entsize;
4168 if (h->root.type == bfd_link_hash_warning)
4169 entsize += htab->root.table.entsize;
4170 }
66eb6687
AM
4171 }
4172
4173 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4174 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4175 if (old_tab == NULL)
4176 goto error_free_vers;
4177
4178 /* Remember the current objalloc pointer, so that all mem for
4179 symbols added can later be reclaimed. */
4180 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4181 if (alloc_mark == NULL)
4182 goto error_free_vers;
4183
5061a885
AM
4184 /* Make a special call to the linker "notice" function to
4185 tell it that we are about to handle an as-needed lib. */
e5034e59 4186 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4187 goto error_free_vers;
5061a885 4188
f45794cb
AM
4189 /* Clone the symbol table. Remember some pointers into the
4190 symbol table, and dynamic symbol count. */
4191 old_ent = (char *) old_tab + tabsize;
66eb6687 4192 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4193 old_undefs = htab->root.undefs;
4194 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4195 old_table = htab->root.table.table;
4196 old_size = htab->root.table.size;
4197 old_count = htab->root.table.count;
5b677558
AM
4198 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4199 if (old_strtab == NULL)
4200 goto error_free_vers;
66eb6687
AM
4201
4202 for (i = 0; i < htab->root.table.size; i++)
4203 {
4204 struct bfd_hash_entry *p;
2de92251 4205 struct elf_link_hash_entry *h;
66eb6687
AM
4206
4207 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4208 {
4209 memcpy (old_ent, p, htab->root.table.entsize);
4210 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4211 h = (struct elf_link_hash_entry *) p;
4212 if (h->root.type == bfd_link_hash_warning)
4213 {
4214 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4215 old_ent = (char *) old_ent + htab->root.table.entsize;
4216 }
66eb6687
AM
4217 }
4218 }
4219 }
4ad4eba5 4220
66eb6687 4221 weaks = NULL;
4ad4eba5
AM
4222 ever = extversym != NULL ? extversym + extsymoff : NULL;
4223 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4224 isym < isymend;
4225 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4226 {
4227 int bind;
4228 bfd_vma value;
af44c138 4229 asection *sec, *new_sec;
4ad4eba5
AM
4230 flagword flags;
4231 const char *name;
4232 struct elf_link_hash_entry *h;
90c984fc 4233 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4234 bfd_boolean definition;
4235 bfd_boolean size_change_ok;
4236 bfd_boolean type_change_ok;
4237 bfd_boolean new_weakdef;
37a9e49a
L
4238 bfd_boolean new_weak;
4239 bfd_boolean old_weak;
4ad4eba5 4240 bfd_boolean override;
a4d8e49b 4241 bfd_boolean common;
97196564 4242 bfd_boolean discarded;
4ad4eba5
AM
4243 unsigned int old_alignment;
4244 bfd *old_bfd;
6e33951e 4245 bfd_boolean matched;
4ad4eba5
AM
4246
4247 override = FALSE;
4248
4249 flags = BSF_NO_FLAGS;
4250 sec = NULL;
4251 value = isym->st_value;
a4d8e49b 4252 common = bed->common_definition (isym);
97196564 4253 discarded = FALSE;
4ad4eba5
AM
4254
4255 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4256 switch (bind)
4ad4eba5 4257 {
3e7a7d11 4258 case STB_LOCAL:
4ad4eba5
AM
4259 /* This should be impossible, since ELF requires that all
4260 global symbols follow all local symbols, and that sh_info
4261 point to the first global symbol. Unfortunately, Irix 5
4262 screws this up. */
4263 continue;
3e7a7d11
NC
4264
4265 case STB_GLOBAL:
a4d8e49b 4266 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4267 flags = BSF_GLOBAL;
3e7a7d11
NC
4268 break;
4269
4270 case STB_WEAK:
4271 flags = BSF_WEAK;
4272 break;
4273
4274 case STB_GNU_UNIQUE:
4275 flags = BSF_GNU_UNIQUE;
4276 break;
4277
4278 default:
4ad4eba5 4279 /* Leave it up to the processor backend. */
3e7a7d11 4280 break;
4ad4eba5
AM
4281 }
4282
4283 if (isym->st_shndx == SHN_UNDEF)
4284 sec = bfd_und_section_ptr;
cb33740c
AM
4285 else if (isym->st_shndx == SHN_ABS)
4286 sec = bfd_abs_section_ptr;
4287 else if (isym->st_shndx == SHN_COMMON)
4288 {
4289 sec = bfd_com_section_ptr;
4290 /* What ELF calls the size we call the value. What ELF
4291 calls the value we call the alignment. */
4292 value = isym->st_size;
4293 }
4294 else
4ad4eba5
AM
4295 {
4296 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4297 if (sec == NULL)
4298 sec = bfd_abs_section_ptr;
dbaa2011 4299 else if (discarded_section (sec))
529fcb95 4300 {
e5d08002
L
4301 /* Symbols from discarded section are undefined. We keep
4302 its visibility. */
529fcb95 4303 sec = bfd_und_section_ptr;
97196564 4304 discarded = TRUE;
529fcb95
PB
4305 isym->st_shndx = SHN_UNDEF;
4306 }
4ad4eba5
AM
4307 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4308 value -= sec->vma;
4309 }
4ad4eba5
AM
4310
4311 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4312 isym->st_name);
4313 if (name == NULL)
4314 goto error_free_vers;
4315
4316 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4317 && (abfd->flags & BFD_PLUGIN) != 0)
4318 {
4319 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4320
4321 if (xc == NULL)
4322 {
4323 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4324 | SEC_EXCLUDE);
4325 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4326 if (xc == NULL)
4327 goto error_free_vers;
4328 }
4329 sec = xc;
4330 }
4331 else if (isym->st_shndx == SHN_COMMON
4332 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4333 && !bfd_link_relocatable (info))
4ad4eba5
AM
4334 {
4335 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4336
4337 if (tcomm == NULL)
4338 {
02d00247
AM
4339 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4340 | SEC_LINKER_CREATED);
4341 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4342 if (tcomm == NULL)
4ad4eba5
AM
4343 goto error_free_vers;
4344 }
4345 sec = tcomm;
4346 }
66eb6687 4347 else if (bed->elf_add_symbol_hook)
4ad4eba5 4348 {
66eb6687
AM
4349 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4350 &sec, &value))
4ad4eba5
AM
4351 goto error_free_vers;
4352
4353 /* The hook function sets the name to NULL if this symbol
4354 should be skipped for some reason. */
4355 if (name == NULL)
4356 continue;
4357 }
4358
4359 /* Sanity check that all possibilities were handled. */
4360 if (sec == NULL)
4361 {
4362 bfd_set_error (bfd_error_bad_value);
4363 goto error_free_vers;
4364 }
4365
191c0c42
AM
4366 /* Silently discard TLS symbols from --just-syms. There's
4367 no way to combine a static TLS block with a new TLS block
4368 for this executable. */
4369 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4370 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4371 continue;
4372
4ad4eba5
AM
4373 if (bfd_is_und_section (sec)
4374 || bfd_is_com_section (sec))
4375 definition = FALSE;
4376 else
4377 definition = TRUE;
4378
4379 size_change_ok = FALSE;
66eb6687 4380 type_change_ok = bed->type_change_ok;
37a9e49a 4381 old_weak = FALSE;
6e33951e 4382 matched = FALSE;
4ad4eba5
AM
4383 old_alignment = 0;
4384 old_bfd = NULL;
af44c138 4385 new_sec = sec;
4ad4eba5 4386
66eb6687 4387 if (is_elf_hash_table (htab))
4ad4eba5
AM
4388 {
4389 Elf_Internal_Versym iver;
4390 unsigned int vernum = 0;
4391 bfd_boolean skip;
4392
fc0e6df6 4393 if (ever == NULL)
4ad4eba5 4394 {
fc0e6df6
PB
4395 if (info->default_imported_symver)
4396 /* Use the default symbol version created earlier. */
4397 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4398 else
4399 iver.vs_vers = 0;
4400 }
4401 else
4402 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4403
4404 vernum = iver.vs_vers & VERSYM_VERSION;
4405
4406 /* If this is a hidden symbol, or if it is not version
4407 1, we append the version name to the symbol name.
cc86ff91
EB
4408 However, we do not modify a non-hidden absolute symbol
4409 if it is not a function, because it might be the version
4410 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4411 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4412 || (vernum > 1
4413 && (!bfd_is_abs_section (sec)
4414 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4415 {
4416 const char *verstr;
4417 size_t namelen, verlen, newlen;
4418 char *newname, *p;
4419
4420 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4421 {
fc0e6df6
PB
4422 if (vernum > elf_tdata (abfd)->cverdefs)
4423 verstr = NULL;
4424 else if (vernum > 1)
4425 verstr =
4426 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4427 else
4428 verstr = "";
4ad4eba5 4429
fc0e6df6 4430 if (verstr == NULL)
4ad4eba5 4431 {
4eca0228 4432 _bfd_error_handler
695344c0 4433 /* xgettext:c-format */
fc0e6df6
PB
4434 (_("%B: %s: invalid version %u (max %d)"),
4435 abfd, name, vernum,
4436 elf_tdata (abfd)->cverdefs);
4437 bfd_set_error (bfd_error_bad_value);
4438 goto error_free_vers;
4ad4eba5 4439 }
fc0e6df6
PB
4440 }
4441 else
4442 {
4443 /* We cannot simply test for the number of
4444 entries in the VERNEED section since the
4445 numbers for the needed versions do not start
4446 at 0. */
4447 Elf_Internal_Verneed *t;
4448
4449 verstr = NULL;
4450 for (t = elf_tdata (abfd)->verref;
4451 t != NULL;
4452 t = t->vn_nextref)
4ad4eba5 4453 {
fc0e6df6 4454 Elf_Internal_Vernaux *a;
4ad4eba5 4455
fc0e6df6
PB
4456 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4457 {
4458 if (a->vna_other == vernum)
4ad4eba5 4459 {
fc0e6df6
PB
4460 verstr = a->vna_nodename;
4461 break;
4ad4eba5 4462 }
4ad4eba5 4463 }
fc0e6df6
PB
4464 if (a != NULL)
4465 break;
4466 }
4467 if (verstr == NULL)
4468 {
4eca0228 4469 _bfd_error_handler
695344c0 4470 /* xgettext:c-format */
fc0e6df6
PB
4471 (_("%B: %s: invalid needed version %d"),
4472 abfd, name, vernum);
4473 bfd_set_error (bfd_error_bad_value);
4474 goto error_free_vers;
4ad4eba5 4475 }
4ad4eba5 4476 }
fc0e6df6
PB
4477
4478 namelen = strlen (name);
4479 verlen = strlen (verstr);
4480 newlen = namelen + verlen + 2;
4481 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4482 && isym->st_shndx != SHN_UNDEF)
4483 ++newlen;
4484
a50b1753 4485 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4486 if (newname == NULL)
4487 goto error_free_vers;
4488 memcpy (newname, name, namelen);
4489 p = newname + namelen;
4490 *p++ = ELF_VER_CHR;
4491 /* If this is a defined non-hidden version symbol,
4492 we add another @ to the name. This indicates the
4493 default version of the symbol. */
4494 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4495 && isym->st_shndx != SHN_UNDEF)
4496 *p++ = ELF_VER_CHR;
4497 memcpy (p, verstr, verlen + 1);
4498
4499 name = newname;
4ad4eba5
AM
4500 }
4501
cd3416da
AM
4502 /* If this symbol has default visibility and the user has
4503 requested we not re-export it, then mark it as hidden. */
a0d49154 4504 if (!bfd_is_und_section (sec)
cd3416da 4505 && !dynamic
ce875075 4506 && abfd->no_export
cd3416da
AM
4507 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4508 isym->st_other = (STV_HIDDEN
4509 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4510
4f3fedcf
AM
4511 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4512 sym_hash, &old_bfd, &old_weak,
4513 &old_alignment, &skip, &override,
6e33951e
L
4514 &type_change_ok, &size_change_ok,
4515 &matched))
4ad4eba5
AM
4516 goto error_free_vers;
4517
4518 if (skip)
4519 continue;
4520
6e33951e
L
4521 /* Override a definition only if the new symbol matches the
4522 existing one. */
4523 if (override && matched)
4ad4eba5
AM
4524 definition = FALSE;
4525
4526 h = *sym_hash;
4527 while (h->root.type == bfd_link_hash_indirect
4528 || h->root.type == bfd_link_hash_warning)
4529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4530
4ad4eba5 4531 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4532 && vernum > 1
4533 && definition)
4534 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4535 }
4536
4537 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4538 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4539 (struct bfd_link_hash_entry **) sym_hash)))
4540 goto error_free_vers;
4541
a43942db
MR
4542 if ((flags & BSF_GNU_UNIQUE)
4543 && (abfd->flags & DYNAMIC) == 0
4544 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4545 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4546
4ad4eba5 4547 h = *sym_hash;
90c984fc
L
4548 /* We need to make sure that indirect symbol dynamic flags are
4549 updated. */
4550 hi = h;
4ad4eba5
AM
4551 while (h->root.type == bfd_link_hash_indirect
4552 || h->root.type == bfd_link_hash_warning)
4553 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4554
97196564
L
4555 /* Setting the index to -3 tells elf_link_output_extsym that
4556 this symbol is defined in a discarded section. */
4557 if (discarded)
4558 h->indx = -3;
4559
4ad4eba5
AM
4560 *sym_hash = h;
4561
37a9e49a 4562 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4563 new_weakdef = FALSE;
4564 if (dynamic
4565 && definition
37a9e49a 4566 && new_weak
fcb93ecf 4567 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4568 && is_elf_hash_table (htab)
f6e332e6 4569 && h->u.weakdef == NULL)
4ad4eba5
AM
4570 {
4571 /* Keep a list of all weak defined non function symbols from
4572 a dynamic object, using the weakdef field. Later in this
4573 function we will set the weakdef field to the correct
4574 value. We only put non-function symbols from dynamic
4575 objects on this list, because that happens to be the only
4576 time we need to know the normal symbol corresponding to a
4577 weak symbol, and the information is time consuming to
4578 figure out. If the weakdef field is not already NULL,
4579 then this symbol was already defined by some previous
4580 dynamic object, and we will be using that previous
4581 definition anyhow. */
4582
f6e332e6 4583 h->u.weakdef = weaks;
4ad4eba5
AM
4584 weaks = h;
4585 new_weakdef = TRUE;
4586 }
4587
4588 /* Set the alignment of a common symbol. */
a4d8e49b 4589 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4590 && h->root.type == bfd_link_hash_common)
4591 {
4592 unsigned int align;
4593
a4d8e49b 4594 if (common)
af44c138
L
4595 align = bfd_log2 (isym->st_value);
4596 else
4597 {
4598 /* The new symbol is a common symbol in a shared object.
4599 We need to get the alignment from the section. */
4600 align = new_sec->alignment_power;
4601 }
595213d4 4602 if (align > old_alignment)
4ad4eba5
AM
4603 h->root.u.c.p->alignment_power = align;
4604 else
4605 h->root.u.c.p->alignment_power = old_alignment;
4606 }
4607
66eb6687 4608 if (is_elf_hash_table (htab))
4ad4eba5 4609 {
4f3fedcf
AM
4610 /* Set a flag in the hash table entry indicating the type of
4611 reference or definition we just found. A dynamic symbol
4612 is one which is referenced or defined by both a regular
4613 object and a shared object. */
4614 bfd_boolean dynsym = FALSE;
4615
4616 /* Plugin symbols aren't normal. Don't set def_regular or
4617 ref_regular for them, or make them dynamic. */
4618 if ((abfd->flags & BFD_PLUGIN) != 0)
4619 ;
4620 else if (! dynamic)
4621 {
4622 if (! definition)
4623 {
4624 h->ref_regular = 1;
4625 if (bind != STB_WEAK)
4626 h->ref_regular_nonweak = 1;
4627 }
4628 else
4629 {
4630 h->def_regular = 1;
4631 if (h->def_dynamic)
4632 {
4633 h->def_dynamic = 0;
4634 h->ref_dynamic = 1;
4635 }
4636 }
4637
4638 /* If the indirect symbol has been forced local, don't
4639 make the real symbol dynamic. */
4640 if ((h == hi || !hi->forced_local)
0e1862bb 4641 && (bfd_link_dll (info)
4f3fedcf
AM
4642 || h->def_dynamic
4643 || h->ref_dynamic))
4644 dynsym = TRUE;
4645 }
4646 else
4647 {
4648 if (! definition)
4649 {
4650 h->ref_dynamic = 1;
4651 hi->ref_dynamic = 1;
4652 }
4653 else
4654 {
4655 h->def_dynamic = 1;
4656 hi->def_dynamic = 1;
4657 }
4658
4659 /* If the indirect symbol has been forced local, don't
4660 make the real symbol dynamic. */
4661 if ((h == hi || !hi->forced_local)
4662 && (h->def_regular
4663 || h->ref_regular
4664 || (h->u.weakdef != NULL
4665 && ! new_weakdef
4666 && h->u.weakdef->dynindx != -1)))
4667 dynsym = TRUE;
4668 }
4669
4670 /* Check to see if we need to add an indirect symbol for
4671 the default name. */
4672 if (definition
4673 || (!override && h->root.type == bfd_link_hash_common))
4674 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4675 sec, value, &old_bfd, &dynsym))
4676 goto error_free_vers;
4ad4eba5
AM
4677
4678 /* Check the alignment when a common symbol is involved. This
4679 can change when a common symbol is overridden by a normal
4680 definition or a common symbol is ignored due to the old
4681 normal definition. We need to make sure the maximum
4682 alignment is maintained. */
a4d8e49b 4683 if ((old_alignment || common)
4ad4eba5
AM
4684 && h->root.type != bfd_link_hash_common)
4685 {
4686 unsigned int common_align;
4687 unsigned int normal_align;
4688 unsigned int symbol_align;
4689 bfd *normal_bfd;
4690 bfd *common_bfd;
4691
3a81e825
AM
4692 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4693 || h->root.type == bfd_link_hash_defweak);
4694
4ad4eba5
AM
4695 symbol_align = ffs (h->root.u.def.value) - 1;
4696 if (h->root.u.def.section->owner != NULL
0616a280
AM
4697 && (h->root.u.def.section->owner->flags
4698 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4699 {
4700 normal_align = h->root.u.def.section->alignment_power;
4701 if (normal_align > symbol_align)
4702 normal_align = symbol_align;
4703 }
4704 else
4705 normal_align = symbol_align;
4706
4707 if (old_alignment)
4708 {
4709 common_align = old_alignment;
4710 common_bfd = old_bfd;
4711 normal_bfd = abfd;
4712 }
4713 else
4714 {
4715 common_align = bfd_log2 (isym->st_value);
4716 common_bfd = abfd;
4717 normal_bfd = old_bfd;
4718 }
4719
4720 if (normal_align < common_align)
d07676f8
NC
4721 {
4722 /* PR binutils/2735 */
4723 if (normal_bfd == NULL)
4eca0228 4724 _bfd_error_handler
695344c0 4725 /* xgettext:c-format */
4f3fedcf
AM
4726 (_("Warning: alignment %u of common symbol `%s' in %B is"
4727 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4728 1 << common_align, name, common_bfd,
4729 1 << normal_align, h->root.u.def.section);
d07676f8 4730 else
4eca0228 4731 _bfd_error_handler
695344c0 4732 /* xgettext:c-format */
d07676f8
NC
4733 (_("Warning: alignment %u of symbol `%s' in %B"
4734 " is smaller than %u in %B"),
c08bb8dd
AM
4735 1 << normal_align, name, normal_bfd,
4736 1 << common_align, common_bfd);
d07676f8 4737 }
4ad4eba5
AM
4738 }
4739
83ad0046 4740 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4741 if (isym->st_size != 0
4742 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4743 && (definition || h->size == 0))
4744 {
83ad0046
L
4745 if (h->size != 0
4746 && h->size != isym->st_size
4747 && ! size_change_ok)
4eca0228 4748 _bfd_error_handler
695344c0 4749 /* xgettext:c-format */
d003868e
AM
4750 (_("Warning: size of symbol `%s' changed"
4751 " from %lu in %B to %lu in %B"),
c08bb8dd
AM
4752 name, (unsigned long) h->size, old_bfd,
4753 (unsigned long) isym->st_size, abfd);
4ad4eba5
AM
4754
4755 h->size = isym->st_size;
4756 }
4757
4758 /* If this is a common symbol, then we always want H->SIZE
4759 to be the size of the common symbol. The code just above
4760 won't fix the size if a common symbol becomes larger. We
4761 don't warn about a size change here, because that is
4f3fedcf 4762 covered by --warn-common. Allow changes between different
fcb93ecf 4763 function types. */
4ad4eba5
AM
4764 if (h->root.type == bfd_link_hash_common)
4765 h->size = h->root.u.c.size;
4766
4767 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4768 && ((definition && !new_weak)
4769 || (old_weak && h->root.type == bfd_link_hash_common)
4770 || h->type == STT_NOTYPE))
4ad4eba5 4771 {
2955ec4c
L
4772 unsigned int type = ELF_ST_TYPE (isym->st_info);
4773
4774 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4775 symbol. */
4776 if (type == STT_GNU_IFUNC
4777 && (abfd->flags & DYNAMIC) != 0)
4778 type = STT_FUNC;
4ad4eba5 4779
2955ec4c
L
4780 if (h->type != type)
4781 {
4782 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4783 /* xgettext:c-format */
4eca0228 4784 _bfd_error_handler
2955ec4c
L
4785 (_("Warning: type of symbol `%s' changed"
4786 " from %d to %d in %B"),
c08bb8dd 4787 name, h->type, type, abfd);
2955ec4c
L
4788
4789 h->type = type;
4790 }
4ad4eba5
AM
4791 }
4792
54ac0771 4793 /* Merge st_other field. */
b8417128 4794 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4795
c3df8c14 4796 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4797 if (definition
4798 && (sec->flags & SEC_DEBUGGING)
4799 && !bfd_link_relocatable (info))
c3df8c14
AM
4800 dynsym = FALSE;
4801
4f3fedcf
AM
4802 /* Nor should we make plugin symbols dynamic. */
4803 if ((abfd->flags & BFD_PLUGIN) != 0)
4804 dynsym = FALSE;
4805
35fc36a8 4806 if (definition)
35399224
L
4807 {
4808 h->target_internal = isym->st_target_internal;
4809 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4810 }
35fc36a8 4811
4ad4eba5
AM
4812 if (definition && !dynamic)
4813 {
4814 char *p = strchr (name, ELF_VER_CHR);
4815 if (p != NULL && p[1] != ELF_VER_CHR)
4816 {
4817 /* Queue non-default versions so that .symver x, x@FOO
4818 aliases can be checked. */
66eb6687 4819 if (!nondeflt_vers)
4ad4eba5 4820 {
66eb6687
AM
4821 amt = ((isymend - isym + 1)
4822 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4823 nondeflt_vers
4824 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4825 if (!nondeflt_vers)
4826 goto error_free_vers;
4ad4eba5 4827 }
66eb6687 4828 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4829 }
4830 }
4831
4832 if (dynsym && h->dynindx == -1)
4833 {
c152c796 4834 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4835 goto error_free_vers;
f6e332e6 4836 if (h->u.weakdef != NULL
4ad4eba5 4837 && ! new_weakdef
f6e332e6 4838 && h->u.weakdef->dynindx == -1)
4ad4eba5 4839 {
66eb6687 4840 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4841 goto error_free_vers;
4842 }
4843 }
1f599d0e 4844 else if (h->dynindx != -1)
4ad4eba5
AM
4845 /* If the symbol already has a dynamic index, but
4846 visibility says it should not be visible, turn it into
4847 a local symbol. */
4848 switch (ELF_ST_VISIBILITY (h->other))
4849 {
4850 case STV_INTERNAL:
4851 case STV_HIDDEN:
4852 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4853 dynsym = FALSE;
4854 break;
4855 }
4856
aef28989
L
4857 /* Don't add DT_NEEDED for references from the dummy bfd nor
4858 for unmatched symbol. */
4ad4eba5 4859 if (!add_needed
aef28989 4860 && matched
4ad4eba5 4861 && definition
010e5ae2 4862 && ((dynsym
ffa9430d 4863 && h->ref_regular_nonweak
4f3fedcf
AM
4864 && (old_bfd == NULL
4865 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4866 || (h->ref_dynamic_nonweak
010e5ae2 4867 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4868 && !on_needed_list (elf_dt_name (abfd),
4869 htab->needed, NULL))))
4ad4eba5
AM
4870 {
4871 int ret;
4872 const char *soname = elf_dt_name (abfd);
4873
16e4ecc0
AM
4874 info->callbacks->minfo ("%!", soname, old_bfd,
4875 h->root.root.string);
4876
4ad4eba5
AM
4877 /* A symbol from a library loaded via DT_NEEDED of some
4878 other library is referenced by a regular object.
e56f61be 4879 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4880 --no-add-needed is used and the reference was not
4881 a weak one. */
4f3fedcf 4882 if (old_bfd != NULL
b918acf9 4883 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4884 {
4eca0228 4885 _bfd_error_handler
695344c0 4886 /* xgettext:c-format */
3cbc5de0 4887 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4888 old_bfd, name);
ff5ac77b 4889 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4890 goto error_free_vers;
4891 }
4892
a50b1753 4893 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4894 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4895
4ad4eba5 4896 add_needed = TRUE;
7e9f0867 4897 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4898 if (ret < 0)
4899 goto error_free_vers;
4900
4901 BFD_ASSERT (ret == 0);
4902 }
4903 }
4904 }
4905
66eb6687
AM
4906 if (extversym != NULL)
4907 {
4908 free (extversym);
4909 extversym = NULL;
4910 }
4911
4912 if (isymbuf != NULL)
4913 {
4914 free (isymbuf);
4915 isymbuf = NULL;
4916 }
4917
4918 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4919 {
4920 unsigned int i;
4921
4922 /* Restore the symbol table. */
f45794cb
AM
4923 old_ent = (char *) old_tab + tabsize;
4924 memset (elf_sym_hashes (abfd), 0,
4925 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4926 htab->root.table.table = old_table;
4927 htab->root.table.size = old_size;
4928 htab->root.table.count = old_count;
66eb6687 4929 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4930 htab->root.undefs = old_undefs;
4931 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4932 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4933 free (old_strtab);
4934 old_strtab = NULL;
66eb6687
AM
4935 for (i = 0; i < htab->root.table.size; i++)
4936 {
4937 struct bfd_hash_entry *p;
4938 struct elf_link_hash_entry *h;
3e0882af
L
4939 bfd_size_type size;
4940 unsigned int alignment_power;
4070765b 4941 unsigned int non_ir_ref_dynamic;
66eb6687
AM
4942
4943 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4944 {
4945 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4946 if (h->root.type == bfd_link_hash_warning)
4947 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4948
3e0882af
L
4949 /* Preserve the maximum alignment and size for common
4950 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4951 since it can still be loaded at run time by another
3e0882af
L
4952 dynamic lib. */
4953 if (h->root.type == bfd_link_hash_common)
4954 {
4955 size = h->root.u.c.size;
4956 alignment_power = h->root.u.c.p->alignment_power;
4957 }
4958 else
4959 {
4960 size = 0;
4961 alignment_power = 0;
4962 }
4070765b 4963 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
4964 will be exported when the dynamic lib becomes needed
4965 in the second pass. */
4070765b 4966 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
4967 memcpy (p, old_ent, htab->root.table.entsize);
4968 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4969 h = (struct elf_link_hash_entry *) p;
4970 if (h->root.type == bfd_link_hash_warning)
4971 {
4972 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4973 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4974 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4975 }
a4542f1b 4976 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4977 {
4978 if (size > h->root.u.c.size)
4979 h->root.u.c.size = size;
4980 if (alignment_power > h->root.u.c.p->alignment_power)
4981 h->root.u.c.p->alignment_power = alignment_power;
4982 }
4070765b 4983 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
4984 }
4985 }
4986
5061a885
AM
4987 /* Make a special call to the linker "notice" function to
4988 tell it that symbols added for crefs may need to be removed. */
e5034e59 4989 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4990 goto error_free_vers;
5061a885 4991
66eb6687
AM
4992 free (old_tab);
4993 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4994 alloc_mark);
4995 if (nondeflt_vers != NULL)
4996 free (nondeflt_vers);
4997 return TRUE;
4998 }
2de92251 4999
66eb6687
AM
5000 if (old_tab != NULL)
5001 {
e5034e59 5002 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5003 goto error_free_vers;
66eb6687
AM
5004 free (old_tab);
5005 old_tab = NULL;
5006 }
5007
c6e8a9a8
L
5008 /* Now that all the symbols from this input file are created, if
5009 not performing a relocatable link, handle .symver foo, foo@BAR
5010 such that any relocs against foo become foo@BAR. */
0e1862bb 5011 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5012 {
ef53be89 5013 size_t cnt, symidx;
4ad4eba5
AM
5014
5015 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5016 {
5017 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5018 char *shortname, *p;
5019
5020 p = strchr (h->root.root.string, ELF_VER_CHR);
5021 if (p == NULL
5022 || (h->root.type != bfd_link_hash_defined
5023 && h->root.type != bfd_link_hash_defweak))
5024 continue;
5025
5026 amt = p - h->root.root.string;
a50b1753 5027 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5028 if (!shortname)
5029 goto error_free_vers;
4ad4eba5
AM
5030 memcpy (shortname, h->root.root.string, amt);
5031 shortname[amt] = '\0';
5032
5033 hi = (struct elf_link_hash_entry *)
66eb6687 5034 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5035 FALSE, FALSE, FALSE);
5036 if (hi != NULL
5037 && hi->root.type == h->root.type
5038 && hi->root.u.def.value == h->root.u.def.value
5039 && hi->root.u.def.section == h->root.u.def.section)
5040 {
5041 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5042 hi->root.type = bfd_link_hash_indirect;
5043 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5044 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5045 sym_hash = elf_sym_hashes (abfd);
5046 if (sym_hash)
5047 for (symidx = 0; symidx < extsymcount; ++symidx)
5048 if (sym_hash[symidx] == hi)
5049 {
5050 sym_hash[symidx] = h;
5051 break;
5052 }
5053 }
5054 free (shortname);
5055 }
5056 free (nondeflt_vers);
5057 nondeflt_vers = NULL;
5058 }
5059
4ad4eba5
AM
5060 /* Now set the weakdefs field correctly for all the weak defined
5061 symbols we found. The only way to do this is to search all the
5062 symbols. Since we only need the information for non functions in
5063 dynamic objects, that's the only time we actually put anything on
5064 the list WEAKS. We need this information so that if a regular
5065 object refers to a symbol defined weakly in a dynamic object, the
5066 real symbol in the dynamic object is also put in the dynamic
5067 symbols; we also must arrange for both symbols to point to the
5068 same memory location. We could handle the general case of symbol
5069 aliasing, but a general symbol alias can only be generated in
5070 assembler code, handling it correctly would be very time
5071 consuming, and other ELF linkers don't handle general aliasing
5072 either. */
5073 if (weaks != NULL)
5074 {
5075 struct elf_link_hash_entry **hpp;
5076 struct elf_link_hash_entry **hppend;
5077 struct elf_link_hash_entry **sorted_sym_hash;
5078 struct elf_link_hash_entry *h;
5079 size_t sym_count;
5080
5081 /* Since we have to search the whole symbol list for each weak
5082 defined symbol, search time for N weak defined symbols will be
5083 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5084 amt = extsymcount;
5085 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5086 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5087 if (sorted_sym_hash == NULL)
5088 goto error_return;
5089 sym_hash = sorted_sym_hash;
5090 hpp = elf_sym_hashes (abfd);
5091 hppend = hpp + extsymcount;
5092 sym_count = 0;
5093 for (; hpp < hppend; hpp++)
5094 {
5095 h = *hpp;
5096 if (h != NULL
5097 && h->root.type == bfd_link_hash_defined
fcb93ecf 5098 && !bed->is_function_type (h->type))
4ad4eba5
AM
5099 {
5100 *sym_hash = h;
5101 sym_hash++;
5102 sym_count++;
5103 }
5104 }
5105
5106 qsort (sorted_sym_hash, sym_count,
5107 sizeof (struct elf_link_hash_entry *),
5108 elf_sort_symbol);
5109
5110 while (weaks != NULL)
5111 {
5112 struct elf_link_hash_entry *hlook;
5113 asection *slook;
5114 bfd_vma vlook;
ed54588d 5115 size_t i, j, idx = 0;
4ad4eba5
AM
5116
5117 hlook = weaks;
f6e332e6
AM
5118 weaks = hlook->u.weakdef;
5119 hlook->u.weakdef = NULL;
4ad4eba5
AM
5120
5121 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5122 || hlook->root.type == bfd_link_hash_defweak
5123 || hlook->root.type == bfd_link_hash_common
5124 || hlook->root.type == bfd_link_hash_indirect);
5125 slook = hlook->root.u.def.section;
5126 vlook = hlook->root.u.def.value;
5127
4ad4eba5
AM
5128 i = 0;
5129 j = sym_count;
14160578 5130 while (i != j)
4ad4eba5
AM
5131 {
5132 bfd_signed_vma vdiff;
5133 idx = (i + j) / 2;
14160578 5134 h = sorted_sym_hash[idx];
4ad4eba5
AM
5135 vdiff = vlook - h->root.u.def.value;
5136 if (vdiff < 0)
5137 j = idx;
5138 else if (vdiff > 0)
5139 i = idx + 1;
5140 else
5141 {
d3435ae8 5142 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5143 if (sdiff < 0)
5144 j = idx;
5145 else if (sdiff > 0)
5146 i = idx + 1;
5147 else
14160578 5148 break;
4ad4eba5
AM
5149 }
5150 }
5151
5152 /* We didn't find a value/section match. */
14160578 5153 if (i == j)
4ad4eba5
AM
5154 continue;
5155
14160578
AM
5156 /* With multiple aliases, or when the weak symbol is already
5157 strongly defined, we have multiple matching symbols and
5158 the binary search above may land on any of them. Step
5159 one past the matching symbol(s). */
5160 while (++idx != j)
5161 {
5162 h = sorted_sym_hash[idx];
5163 if (h->root.u.def.section != slook
5164 || h->root.u.def.value != vlook)
5165 break;
5166 }
5167
5168 /* Now look back over the aliases. Since we sorted by size
5169 as well as value and section, we'll choose the one with
5170 the largest size. */
5171 while (idx-- != i)
4ad4eba5 5172 {
14160578 5173 h = sorted_sym_hash[idx];
4ad4eba5
AM
5174
5175 /* Stop if value or section doesn't match. */
14160578
AM
5176 if (h->root.u.def.section != slook
5177 || h->root.u.def.value != vlook)
4ad4eba5
AM
5178 break;
5179 else if (h != hlook)
5180 {
f6e332e6 5181 hlook->u.weakdef = h;
4ad4eba5
AM
5182
5183 /* If the weak definition is in the list of dynamic
5184 symbols, make sure the real definition is put
5185 there as well. */
5186 if (hlook->dynindx != -1 && h->dynindx == -1)
5187 {
c152c796 5188 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5189 {
5190 err_free_sym_hash:
5191 free (sorted_sym_hash);
5192 goto error_return;
5193 }
4ad4eba5
AM
5194 }
5195
5196 /* If the real definition is in the list of dynamic
5197 symbols, make sure the weak definition is put
5198 there as well. If we don't do this, then the
5199 dynamic loader might not merge the entries for the
5200 real definition and the weak definition. */
5201 if (h->dynindx != -1 && hlook->dynindx == -1)
5202 {
c152c796 5203 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5204 goto err_free_sym_hash;
4ad4eba5
AM
5205 }
5206 break;
5207 }
5208 }
5209 }
5210
5211 free (sorted_sym_hash);
5212 }
5213
33177bb1
AM
5214 if (bed->check_directives
5215 && !(*bed->check_directives) (abfd, info))
5216 return FALSE;
85fbca6a 5217
d9689752
L
5218 if (!info->check_relocs_after_open_input
5219 && !_bfd_elf_link_check_relocs (abfd, info))
5220 return FALSE;
4ad4eba5
AM
5221
5222 /* If this is a non-traditional link, try to optimize the handling
5223 of the .stab/.stabstr sections. */
5224 if (! dynamic
5225 && ! info->traditional_format
66eb6687 5226 && is_elf_hash_table (htab)
4ad4eba5
AM
5227 && (info->strip != strip_all && info->strip != strip_debugger))
5228 {
5229 asection *stabstr;
5230
5231 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5232 if (stabstr != NULL)
5233 {
5234 bfd_size_type string_offset = 0;
5235 asection *stab;
5236
5237 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5238 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5239 && (!stab->name[5] ||
5240 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5241 && (stab->flags & SEC_MERGE) == 0
5242 && !bfd_is_abs_section (stab->output_section))
5243 {
5244 struct bfd_elf_section_data *secdata;
5245
5246 secdata = elf_section_data (stab);
66eb6687
AM
5247 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5248 stabstr, &secdata->sec_info,
4ad4eba5
AM
5249 &string_offset))
5250 goto error_return;
5251 if (secdata->sec_info)
dbaa2011 5252 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5253 }
5254 }
5255 }
5256
66eb6687 5257 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5258 {
5259 /* Add this bfd to the loaded list. */
5260 struct elf_link_loaded_list *n;
5261
ca4be51c 5262 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5263 if (n == NULL)
5264 goto error_return;
5265 n->abfd = abfd;
66eb6687
AM
5266 n->next = htab->loaded;
5267 htab->loaded = n;
4ad4eba5
AM
5268 }
5269
5270 return TRUE;
5271
5272 error_free_vers:
66eb6687
AM
5273 if (old_tab != NULL)
5274 free (old_tab);
5b677558
AM
5275 if (old_strtab != NULL)
5276 free (old_strtab);
4ad4eba5
AM
5277 if (nondeflt_vers != NULL)
5278 free (nondeflt_vers);
5279 if (extversym != NULL)
5280 free (extversym);
5281 error_free_sym:
5282 if (isymbuf != NULL)
5283 free (isymbuf);
5284 error_return:
5285 return FALSE;
5286}
5287
8387904d
AM
5288/* Return the linker hash table entry of a symbol that might be
5289 satisfied by an archive symbol. Return -1 on error. */
5290
5291struct elf_link_hash_entry *
5292_bfd_elf_archive_symbol_lookup (bfd *abfd,
5293 struct bfd_link_info *info,
5294 const char *name)
5295{
5296 struct elf_link_hash_entry *h;
5297 char *p, *copy;
5298 size_t len, first;
5299
2a41f396 5300 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5301 if (h != NULL)
5302 return h;
5303
5304 /* If this is a default version (the name contains @@), look up the
5305 symbol again with only one `@' as well as without the version.
5306 The effect is that references to the symbol with and without the
5307 version will be matched by the default symbol in the archive. */
5308
5309 p = strchr (name, ELF_VER_CHR);
5310 if (p == NULL || p[1] != ELF_VER_CHR)
5311 return h;
5312
5313 /* First check with only one `@'. */
5314 len = strlen (name);
a50b1753 5315 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5316 if (copy == NULL)
5317 return (struct elf_link_hash_entry *) 0 - 1;
5318
5319 first = p - name + 1;
5320 memcpy (copy, name, first);
5321 memcpy (copy + first, name + first + 1, len - first);
5322
2a41f396 5323 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5324 if (h == NULL)
5325 {
5326 /* We also need to check references to the symbol without the
5327 version. */
5328 copy[first - 1] = '\0';
5329 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5330 FALSE, FALSE, TRUE);
8387904d
AM
5331 }
5332
5333 bfd_release (abfd, copy);
5334 return h;
5335}
5336
0ad989f9 5337/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5338 don't use _bfd_generic_link_add_archive_symbols because we need to
5339 handle versioned symbols.
0ad989f9
L
5340
5341 Fortunately, ELF archive handling is simpler than that done by
5342 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5343 oddities. In ELF, if we find a symbol in the archive map, and the
5344 symbol is currently undefined, we know that we must pull in that
5345 object file.
5346
5347 Unfortunately, we do have to make multiple passes over the symbol
5348 table until nothing further is resolved. */
5349
4ad4eba5
AM
5350static bfd_boolean
5351elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5352{
5353 symindex c;
13e570f8 5354 unsigned char *included = NULL;
0ad989f9
L
5355 carsym *symdefs;
5356 bfd_boolean loop;
5357 bfd_size_type amt;
8387904d
AM
5358 const struct elf_backend_data *bed;
5359 struct elf_link_hash_entry * (*archive_symbol_lookup)
5360 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5361
5362 if (! bfd_has_map (abfd))
5363 {
5364 /* An empty archive is a special case. */
5365 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5366 return TRUE;
5367 bfd_set_error (bfd_error_no_armap);
5368 return FALSE;
5369 }
5370
5371 /* Keep track of all symbols we know to be already defined, and all
5372 files we know to be already included. This is to speed up the
5373 second and subsequent passes. */
5374 c = bfd_ardata (abfd)->symdef_count;
5375 if (c == 0)
5376 return TRUE;
5377 amt = c;
13e570f8
AM
5378 amt *= sizeof (*included);
5379 included = (unsigned char *) bfd_zmalloc (amt);
5380 if (included == NULL)
5381 return FALSE;
0ad989f9
L
5382
5383 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5384 bed = get_elf_backend_data (abfd);
5385 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5386
5387 do
5388 {
5389 file_ptr last;
5390 symindex i;
5391 carsym *symdef;
5392 carsym *symdefend;
5393
5394 loop = FALSE;
5395 last = -1;
5396
5397 symdef = symdefs;
5398 symdefend = symdef + c;
5399 for (i = 0; symdef < symdefend; symdef++, i++)
5400 {
5401 struct elf_link_hash_entry *h;
5402 bfd *element;
5403 struct bfd_link_hash_entry *undefs_tail;
5404 symindex mark;
5405
13e570f8 5406 if (included[i])
0ad989f9
L
5407 continue;
5408 if (symdef->file_offset == last)
5409 {
5410 included[i] = TRUE;
5411 continue;
5412 }
5413
8387904d
AM
5414 h = archive_symbol_lookup (abfd, info, symdef->name);
5415 if (h == (struct elf_link_hash_entry *) 0 - 1)
5416 goto error_return;
0ad989f9
L
5417
5418 if (h == NULL)
5419 continue;
5420
5421 if (h->root.type == bfd_link_hash_common)
5422 {
5423 /* We currently have a common symbol. The archive map contains
5424 a reference to this symbol, so we may want to include it. We
5425 only want to include it however, if this archive element
5426 contains a definition of the symbol, not just another common
5427 declaration of it.
5428
5429 Unfortunately some archivers (including GNU ar) will put
5430 declarations of common symbols into their archive maps, as
5431 well as real definitions, so we cannot just go by the archive
5432 map alone. Instead we must read in the element's symbol
5433 table and check that to see what kind of symbol definition
5434 this is. */
5435 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5436 continue;
5437 }
5438 else if (h->root.type != bfd_link_hash_undefined)
5439 {
5440 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5441 /* Symbol must be defined. Don't check it again. */
5442 included[i] = TRUE;
0ad989f9
L
5443 continue;
5444 }
5445
5446 /* We need to include this archive member. */
5447 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5448 if (element == NULL)
5449 goto error_return;
5450
5451 if (! bfd_check_format (element, bfd_object))
5452 goto error_return;
5453
0ad989f9
L
5454 undefs_tail = info->hash->undefs_tail;
5455
0e144ba7
AM
5456 if (!(*info->callbacks
5457 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5458 continue;
0e144ba7 5459 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5460 goto error_return;
5461
5462 /* If there are any new undefined symbols, we need to make
5463 another pass through the archive in order to see whether
5464 they can be defined. FIXME: This isn't perfect, because
5465 common symbols wind up on undefs_tail and because an
5466 undefined symbol which is defined later on in this pass
5467 does not require another pass. This isn't a bug, but it
5468 does make the code less efficient than it could be. */
5469 if (undefs_tail != info->hash->undefs_tail)
5470 loop = TRUE;
5471
5472 /* Look backward to mark all symbols from this object file
5473 which we have already seen in this pass. */
5474 mark = i;
5475 do
5476 {
5477 included[mark] = TRUE;
5478 if (mark == 0)
5479 break;
5480 --mark;
5481 }
5482 while (symdefs[mark].file_offset == symdef->file_offset);
5483
5484 /* We mark subsequent symbols from this object file as we go
5485 on through the loop. */
5486 last = symdef->file_offset;
5487 }
5488 }
5489 while (loop);
5490
0ad989f9
L
5491 free (included);
5492
5493 return TRUE;
5494
5495 error_return:
0ad989f9
L
5496 if (included != NULL)
5497 free (included);
5498 return FALSE;
5499}
4ad4eba5
AM
5500
5501/* Given an ELF BFD, add symbols to the global hash table as
5502 appropriate. */
5503
5504bfd_boolean
5505bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5506{
5507 switch (bfd_get_format (abfd))
5508 {
5509 case bfd_object:
5510 return elf_link_add_object_symbols (abfd, info);
5511 case bfd_archive:
5512 return elf_link_add_archive_symbols (abfd, info);
5513 default:
5514 bfd_set_error (bfd_error_wrong_format);
5515 return FALSE;
5516 }
5517}
5a580b3a 5518\f
14b1c01e
AM
5519struct hash_codes_info
5520{
5521 unsigned long *hashcodes;
5522 bfd_boolean error;
5523};
a0c8462f 5524
5a580b3a
AM
5525/* This function will be called though elf_link_hash_traverse to store
5526 all hash value of the exported symbols in an array. */
5527
5528static bfd_boolean
5529elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5530{
a50b1753 5531 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5532 const char *name;
5a580b3a
AM
5533 unsigned long ha;
5534 char *alc = NULL;
5535
5a580b3a
AM
5536 /* Ignore indirect symbols. These are added by the versioning code. */
5537 if (h->dynindx == -1)
5538 return TRUE;
5539
5540 name = h->root.root.string;
422f1182 5541 if (h->versioned >= versioned)
5a580b3a 5542 {
422f1182
L
5543 char *p = strchr (name, ELF_VER_CHR);
5544 if (p != NULL)
14b1c01e 5545 {
422f1182
L
5546 alc = (char *) bfd_malloc (p - name + 1);
5547 if (alc == NULL)
5548 {
5549 inf->error = TRUE;
5550 return FALSE;
5551 }
5552 memcpy (alc, name, p - name);
5553 alc[p - name] = '\0';
5554 name = alc;
14b1c01e 5555 }
5a580b3a
AM
5556 }
5557
5558 /* Compute the hash value. */
5559 ha = bfd_elf_hash (name);
5560
5561 /* Store the found hash value in the array given as the argument. */
14b1c01e 5562 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5563
5564 /* And store it in the struct so that we can put it in the hash table
5565 later. */
f6e332e6 5566 h->u.elf_hash_value = ha;
5a580b3a
AM
5567
5568 if (alc != NULL)
5569 free (alc);
5570
5571 return TRUE;
5572}
5573
fdc90cb4
JJ
5574struct collect_gnu_hash_codes
5575{
5576 bfd *output_bfd;
5577 const struct elf_backend_data *bed;
5578 unsigned long int nsyms;
5579 unsigned long int maskbits;
5580 unsigned long int *hashcodes;
5581 unsigned long int *hashval;
5582 unsigned long int *indx;
5583 unsigned long int *counts;
5584 bfd_vma *bitmask;
5585 bfd_byte *contents;
5586 long int min_dynindx;
5587 unsigned long int bucketcount;
5588 unsigned long int symindx;
5589 long int local_indx;
5590 long int shift1, shift2;
5591 unsigned long int mask;
14b1c01e 5592 bfd_boolean error;
fdc90cb4
JJ
5593};
5594
5595/* This function will be called though elf_link_hash_traverse to store
5596 all hash value of the exported symbols in an array. */
5597
5598static bfd_boolean
5599elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5600{
a50b1753 5601 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5602 const char *name;
fdc90cb4
JJ
5603 unsigned long ha;
5604 char *alc = NULL;
5605
fdc90cb4
JJ
5606 /* Ignore indirect symbols. These are added by the versioning code. */
5607 if (h->dynindx == -1)
5608 return TRUE;
5609
5610 /* Ignore also local symbols and undefined symbols. */
5611 if (! (*s->bed->elf_hash_symbol) (h))
5612 return TRUE;
5613
5614 name = h->root.root.string;
422f1182 5615 if (h->versioned >= versioned)
fdc90cb4 5616 {
422f1182
L
5617 char *p = strchr (name, ELF_VER_CHR);
5618 if (p != NULL)
14b1c01e 5619 {
422f1182
L
5620 alc = (char *) bfd_malloc (p - name + 1);
5621 if (alc == NULL)
5622 {
5623 s->error = TRUE;
5624 return FALSE;
5625 }
5626 memcpy (alc, name, p - name);
5627 alc[p - name] = '\0';
5628 name = alc;
14b1c01e 5629 }
fdc90cb4
JJ
5630 }
5631
5632 /* Compute the hash value. */
5633 ha = bfd_elf_gnu_hash (name);
5634
5635 /* Store the found hash value in the array for compute_bucket_count,
5636 and also for .dynsym reordering purposes. */
5637 s->hashcodes[s->nsyms] = ha;
5638 s->hashval[h->dynindx] = ha;
5639 ++s->nsyms;
5640 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5641 s->min_dynindx = h->dynindx;
5642
5643 if (alc != NULL)
5644 free (alc);
5645
5646 return TRUE;
5647}
5648
5649/* This function will be called though elf_link_hash_traverse to do
5650 final dynaminc symbol renumbering. */
5651
5652static bfd_boolean
5653elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5654{
a50b1753 5655 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5656 unsigned long int bucket;
5657 unsigned long int val;
5658
fdc90cb4
JJ
5659 /* Ignore indirect symbols. */
5660 if (h->dynindx == -1)
5661 return TRUE;
5662
5663 /* Ignore also local symbols and undefined symbols. */
5664 if (! (*s->bed->elf_hash_symbol) (h))
5665 {
5666 if (h->dynindx >= s->min_dynindx)
5667 h->dynindx = s->local_indx++;
5668 return TRUE;
5669 }
5670
5671 bucket = s->hashval[h->dynindx] % s->bucketcount;
5672 val = (s->hashval[h->dynindx] >> s->shift1)
5673 & ((s->maskbits >> s->shift1) - 1);
5674 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5675 s->bitmask[val]
5676 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5677 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5678 if (s->counts[bucket] == 1)
5679 /* Last element terminates the chain. */
5680 val |= 1;
5681 bfd_put_32 (s->output_bfd, val,
5682 s->contents + (s->indx[bucket] - s->symindx) * 4);
5683 --s->counts[bucket];
5684 h->dynindx = s->indx[bucket]++;
5685 return TRUE;
5686}
5687
5688/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5689
5690bfd_boolean
5691_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5692{
5693 return !(h->forced_local
5694 || h->root.type == bfd_link_hash_undefined
5695 || h->root.type == bfd_link_hash_undefweak
5696 || ((h->root.type == bfd_link_hash_defined
5697 || h->root.type == bfd_link_hash_defweak)
5698 && h->root.u.def.section->output_section == NULL));
5699}
5700
5a580b3a
AM
5701/* Array used to determine the number of hash table buckets to use
5702 based on the number of symbols there are. If there are fewer than
5703 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5704 fewer than 37 we use 17 buckets, and so forth. We never use more
5705 than 32771 buckets. */
5706
5707static const size_t elf_buckets[] =
5708{
5709 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5710 16411, 32771, 0
5711};
5712
5713/* Compute bucket count for hashing table. We do not use a static set
5714 of possible tables sizes anymore. Instead we determine for all
5715 possible reasonable sizes of the table the outcome (i.e., the
5716 number of collisions etc) and choose the best solution. The
5717 weighting functions are not too simple to allow the table to grow
5718 without bounds. Instead one of the weighting factors is the size.
5719 Therefore the result is always a good payoff between few collisions
5720 (= short chain lengths) and table size. */
5721static size_t
b20dd2ce 5722compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5723 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5724 unsigned long int nsyms,
5725 int gnu_hash)
5a580b3a 5726{
5a580b3a 5727 size_t best_size = 0;
5a580b3a 5728 unsigned long int i;
5a580b3a 5729
5a580b3a
AM
5730 /* We have a problem here. The following code to optimize the table
5731 size requires an integer type with more the 32 bits. If
5732 BFD_HOST_U_64_BIT is set we know about such a type. */
5733#ifdef BFD_HOST_U_64_BIT
5734 if (info->optimize)
5735 {
5a580b3a
AM
5736 size_t minsize;
5737 size_t maxsize;
5738 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5739 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5740 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5741 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5742 unsigned long int *counts;
d40f3da9 5743 bfd_size_type amt;
0883b6e0 5744 unsigned int no_improvement_count = 0;
5a580b3a
AM
5745
5746 /* Possible optimization parameters: if we have NSYMS symbols we say
5747 that the hashing table must at least have NSYMS/4 and at most
5748 2*NSYMS buckets. */
5749 minsize = nsyms / 4;
5750 if (minsize == 0)
5751 minsize = 1;
5752 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5753 if (gnu_hash)
5754 {
5755 if (minsize < 2)
5756 minsize = 2;
5757 if ((best_size & 31) == 0)
5758 ++best_size;
5759 }
5a580b3a
AM
5760
5761 /* Create array where we count the collisions in. We must use bfd_malloc
5762 since the size could be large. */
5763 amt = maxsize;
5764 amt *= sizeof (unsigned long int);
a50b1753 5765 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5766 if (counts == NULL)
fdc90cb4 5767 return 0;
5a580b3a
AM
5768
5769 /* Compute the "optimal" size for the hash table. The criteria is a
5770 minimal chain length. The minor criteria is (of course) the size
5771 of the table. */
5772 for (i = minsize; i < maxsize; ++i)
5773 {
5774 /* Walk through the array of hashcodes and count the collisions. */
5775 BFD_HOST_U_64_BIT max;
5776 unsigned long int j;
5777 unsigned long int fact;
5778
fdc90cb4
JJ
5779 if (gnu_hash && (i & 31) == 0)
5780 continue;
5781
5a580b3a
AM
5782 memset (counts, '\0', i * sizeof (unsigned long int));
5783
5784 /* Determine how often each hash bucket is used. */
5785 for (j = 0; j < nsyms; ++j)
5786 ++counts[hashcodes[j] % i];
5787
5788 /* For the weight function we need some information about the
5789 pagesize on the target. This is information need not be 100%
5790 accurate. Since this information is not available (so far) we
5791 define it here to a reasonable default value. If it is crucial
5792 to have a better value some day simply define this value. */
5793# ifndef BFD_TARGET_PAGESIZE
5794# define BFD_TARGET_PAGESIZE (4096)
5795# endif
5796
fdc90cb4
JJ
5797 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5798 and the chains. */
5799 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5800
5801# if 1
5802 /* Variant 1: optimize for short chains. We add the squares
5803 of all the chain lengths (which favors many small chain
5804 over a few long chains). */
5805 for (j = 0; j < i; ++j)
5806 max += counts[j] * counts[j];
5807
5808 /* This adds penalties for the overall size of the table. */
fdc90cb4 5809 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5810 max *= fact * fact;
5811# else
5812 /* Variant 2: Optimize a lot more for small table. Here we
5813 also add squares of the size but we also add penalties for
5814 empty slots (the +1 term). */
5815 for (j = 0; j < i; ++j)
5816 max += (1 + counts[j]) * (1 + counts[j]);
5817
5818 /* The overall size of the table is considered, but not as
5819 strong as in variant 1, where it is squared. */
fdc90cb4 5820 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5821 max *= fact;
5822# endif
5823
5824 /* Compare with current best results. */
5825 if (max < best_chlen)
5826 {
5827 best_chlen = max;
5828 best_size = i;
ca4be51c 5829 no_improvement_count = 0;
5a580b3a 5830 }
0883b6e0
NC
5831 /* PR 11843: Avoid futile long searches for the best bucket size
5832 when there are a large number of symbols. */
5833 else if (++no_improvement_count == 100)
5834 break;
5a580b3a
AM
5835 }
5836
5837 free (counts);
5838 }
5839 else
5840#endif /* defined (BFD_HOST_U_64_BIT) */
5841 {
5842 /* This is the fallback solution if no 64bit type is available or if we
5843 are not supposed to spend much time on optimizations. We select the
5844 bucket count using a fixed set of numbers. */
5845 for (i = 0; elf_buckets[i] != 0; i++)
5846 {
5847 best_size = elf_buckets[i];
fdc90cb4 5848 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5849 break;
5850 }
fdc90cb4
JJ
5851 if (gnu_hash && best_size < 2)
5852 best_size = 2;
5a580b3a
AM
5853 }
5854
5a580b3a
AM
5855 return best_size;
5856}
5857
d0bf826b
AM
5858/* Size any SHT_GROUP section for ld -r. */
5859
5860bfd_boolean
5861_bfd_elf_size_group_sections (struct bfd_link_info *info)
5862{
5863 bfd *ibfd;
5864
c72f2fb2 5865 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5866 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5867 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5868 return FALSE;
5869 return TRUE;
5870}
5871
04c3a755
NS
5872/* Set a default stack segment size. The value in INFO wins. If it
5873 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5874 undefined it is initialized. */
5875
5876bfd_boolean
5877bfd_elf_stack_segment_size (bfd *output_bfd,
5878 struct bfd_link_info *info,
5879 const char *legacy_symbol,
5880 bfd_vma default_size)
5881{
5882 struct elf_link_hash_entry *h = NULL;
5883
5884 /* Look for legacy symbol. */
5885 if (legacy_symbol)
5886 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5887 FALSE, FALSE, FALSE);
5888 if (h && (h->root.type == bfd_link_hash_defined
5889 || h->root.type == bfd_link_hash_defweak)
5890 && h->def_regular
5891 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5892 {
5893 /* The symbol has no type if specified on the command line. */
5894 h->type = STT_OBJECT;
5895 if (info->stacksize)
695344c0 5896 /* xgettext:c-format */
4eca0228
AM
5897 _bfd_error_handler (_("%B: stack size specified and %s set"),
5898 output_bfd, legacy_symbol);
04c3a755 5899 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5900 /* xgettext:c-format */
4eca0228
AM
5901 _bfd_error_handler (_("%B: %s not absolute"),
5902 output_bfd, legacy_symbol);
04c3a755
NS
5903 else
5904 info->stacksize = h->root.u.def.value;
5905 }
5906
5907 if (!info->stacksize)
5908 /* If the user didn't set a size, or explicitly inhibit the
5909 size, set it now. */
5910 info->stacksize = default_size;
5911
5912 /* Provide the legacy symbol, if it is referenced. */
5913 if (h && (h->root.type == bfd_link_hash_undefined
5914 || h->root.type == bfd_link_hash_undefweak))
5915 {
5916 struct bfd_link_hash_entry *bh = NULL;
5917
5918 if (!(_bfd_generic_link_add_one_symbol
5919 (info, output_bfd, legacy_symbol,
5920 BSF_GLOBAL, bfd_abs_section_ptr,
5921 info->stacksize >= 0 ? info->stacksize : 0,
5922 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5923 return FALSE;
5924
5925 h = (struct elf_link_hash_entry *) bh;
5926 h->def_regular = 1;
5927 h->type = STT_OBJECT;
5928 }
5929
5930 return TRUE;
5931}
5932
b531344c
MR
5933/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5934
5935struct elf_gc_sweep_symbol_info
5936{
5937 struct bfd_link_info *info;
5938 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5939 bfd_boolean);
5940};
5941
5942static bfd_boolean
5943elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5944{
5945 if (!h->mark
5946 && (((h->root.type == bfd_link_hash_defined
5947 || h->root.type == bfd_link_hash_defweak)
5948 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5949 && h->root.u.def.section->gc_mark))
5950 || h->root.type == bfd_link_hash_undefined
5951 || h->root.type == bfd_link_hash_undefweak))
5952 {
5953 struct elf_gc_sweep_symbol_info *inf;
5954
5955 inf = (struct elf_gc_sweep_symbol_info *) data;
5956 (*inf->hide_symbol) (inf->info, h, TRUE);
5957 h->def_regular = 0;
5958 h->ref_regular = 0;
5959 h->ref_regular_nonweak = 0;
5960 }
5961
5962 return TRUE;
5963}
5964
5a580b3a
AM
5965/* Set up the sizes and contents of the ELF dynamic sections. This is
5966 called by the ELF linker emulation before_allocation routine. We
5967 must set the sizes of the sections before the linker sets the
5968 addresses of the various sections. */
5969
5970bfd_boolean
5971bfd_elf_size_dynamic_sections (bfd *output_bfd,
5972 const char *soname,
5973 const char *rpath,
5974 const char *filter_shlib,
7ee314fa
AM
5975 const char *audit,
5976 const char *depaudit,
5a580b3a
AM
5977 const char * const *auxiliary_filters,
5978 struct bfd_link_info *info,
fd91d419 5979 asection **sinterpptr)
5a580b3a 5980{
5a580b3a
AM
5981 bfd *dynobj;
5982 const struct elf_backend_data *bed;
5a580b3a
AM
5983
5984 *sinterpptr = NULL;
5985
5a580b3a
AM
5986 if (!is_elf_hash_table (info->hash))
5987 return TRUE;
5988
5a580b3a
AM
5989 dynobj = elf_hash_table (info)->dynobj;
5990
9a2a56cc 5991 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 5992 {
902e9fc7
MR
5993 struct bfd_elf_version_tree *verdefs;
5994 struct elf_info_failed asvinfo;
5a580b3a
AM
5995 struct bfd_elf_version_tree *t;
5996 struct bfd_elf_version_expr *d;
902e9fc7 5997 struct elf_info_failed eif;
5a580b3a 5998 bfd_boolean all_defined;
902e9fc7 5999 asection *s;
e6699019 6000 size_t soname_indx;
7ee314fa 6001
5a580b3a 6002 eif.info = info;
5a580b3a
AM
6003 eif.failed = FALSE;
6004
6005 /* If we are supposed to export all symbols into the dynamic symbol
6006 table (this is not the normal case), then do so. */
55255dae 6007 if (info->export_dynamic
0e1862bb 6008 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6009 {
6010 elf_link_hash_traverse (elf_hash_table (info),
6011 _bfd_elf_export_symbol,
6012 &eif);
6013 if (eif.failed)
6014 return FALSE;
6015 }
6016
e6699019
L
6017 if (soname != NULL)
6018 {
6019 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6020 soname, TRUE);
6021 if (soname_indx == (size_t) -1
6022 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6023 return FALSE;
6024 }
6025 else
6026 soname_indx = (size_t) -1;
6027
5a580b3a 6028 /* Make all global versions with definition. */
fd91d419 6029 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6030 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6031 if (!d->symver && d->literal)
5a580b3a
AM
6032 {
6033 const char *verstr, *name;
6034 size_t namelen, verlen, newlen;
93252b1c 6035 char *newname, *p, leading_char;
5a580b3a
AM
6036 struct elf_link_hash_entry *newh;
6037
93252b1c 6038 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6039 name = d->pattern;
93252b1c 6040 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6041 verstr = t->name;
6042 verlen = strlen (verstr);
6043 newlen = namelen + verlen + 3;
6044
a50b1753 6045 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6046 if (newname == NULL)
6047 return FALSE;
93252b1c
MF
6048 newname[0] = leading_char;
6049 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6050
6051 /* Check the hidden versioned definition. */
6052 p = newname + namelen;
6053 *p++ = ELF_VER_CHR;
6054 memcpy (p, verstr, verlen + 1);
6055 newh = elf_link_hash_lookup (elf_hash_table (info),
6056 newname, FALSE, FALSE,
6057 FALSE);
6058 if (newh == NULL
6059 || (newh->root.type != bfd_link_hash_defined
6060 && newh->root.type != bfd_link_hash_defweak))
6061 {
6062 /* Check the default versioned definition. */
6063 *p++ = ELF_VER_CHR;
6064 memcpy (p, verstr, verlen + 1);
6065 newh = elf_link_hash_lookup (elf_hash_table (info),
6066 newname, FALSE, FALSE,
6067 FALSE);
6068 }
6069 free (newname);
6070
6071 /* Mark this version if there is a definition and it is
6072 not defined in a shared object. */
6073 if (newh != NULL
f5385ebf 6074 && !newh->def_dynamic
5a580b3a
AM
6075 && (newh->root.type == bfd_link_hash_defined
6076 || newh->root.type == bfd_link_hash_defweak))
6077 d->symver = 1;
6078 }
6079
6080 /* Attach all the symbols to their version information. */
5a580b3a 6081 asvinfo.info = info;
5a580b3a
AM
6082 asvinfo.failed = FALSE;
6083
6084 elf_link_hash_traverse (elf_hash_table (info),
6085 _bfd_elf_link_assign_sym_version,
6086 &asvinfo);
6087 if (asvinfo.failed)
6088 return FALSE;
6089
6090 if (!info->allow_undefined_version)
6091 {
6092 /* Check if all global versions have a definition. */
6093 all_defined = TRUE;
fd91d419 6094 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6095 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6096 if (d->literal && !d->symver && !d->script)
5a580b3a 6097 {
4eca0228 6098 _bfd_error_handler
5a580b3a
AM
6099 (_("%s: undefined version: %s"),
6100 d->pattern, t->name);
6101 all_defined = FALSE;
6102 }
6103
6104 if (!all_defined)
6105 {
6106 bfd_set_error (bfd_error_bad_value);
6107 return FALSE;
6108 }
6109 }
6110
902e9fc7
MR
6111 /* Set up the version definition section. */
6112 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6113 BFD_ASSERT (s != NULL);
5a580b3a 6114
902e9fc7
MR
6115 /* We may have created additional version definitions if we are
6116 just linking a regular application. */
6117 verdefs = info->version_info;
5a580b3a 6118
902e9fc7
MR
6119 /* Skip anonymous version tag. */
6120 if (verdefs != NULL && verdefs->vernum == 0)
6121 verdefs = verdefs->next;
5a580b3a 6122
902e9fc7
MR
6123 if (verdefs == NULL && !info->create_default_symver)
6124 s->flags |= SEC_EXCLUDE;
6125 else
5a580b3a 6126 {
902e9fc7
MR
6127 unsigned int cdefs;
6128 bfd_size_type size;
6129 bfd_byte *p;
6130 Elf_Internal_Verdef def;
6131 Elf_Internal_Verdaux defaux;
6132 struct bfd_link_hash_entry *bh;
6133 struct elf_link_hash_entry *h;
6134 const char *name;
5a580b3a 6135
902e9fc7
MR
6136 cdefs = 0;
6137 size = 0;
5a580b3a 6138
902e9fc7
MR
6139 /* Make space for the base version. */
6140 size += sizeof (Elf_External_Verdef);
6141 size += sizeof (Elf_External_Verdaux);
6142 ++cdefs;
6143
6144 /* Make space for the default version. */
6145 if (info->create_default_symver)
6146 {
6147 size += sizeof (Elf_External_Verdef);
6148 ++cdefs;
3e3b46e5
PB
6149 }
6150
5a580b3a
AM
6151 for (t = verdefs; t != NULL; t = t->next)
6152 {
6153 struct bfd_elf_version_deps *n;
6154
a6cc6b3b
RO
6155 /* Don't emit base version twice. */
6156 if (t->vernum == 0)
6157 continue;
6158
5a580b3a
AM
6159 size += sizeof (Elf_External_Verdef);
6160 size += sizeof (Elf_External_Verdaux);
6161 ++cdefs;
6162
6163 for (n = t->deps; n != NULL; n = n->next)
6164 size += sizeof (Elf_External_Verdaux);
6165 }
6166
eea6121a 6167 s->size = size;
a50b1753 6168 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6169 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6170 return FALSE;
6171
6172 /* Fill in the version definition section. */
6173
6174 p = s->contents;
6175
6176 def.vd_version = VER_DEF_CURRENT;
6177 def.vd_flags = VER_FLG_BASE;
6178 def.vd_ndx = 1;
6179 def.vd_cnt = 1;
3e3b46e5
PB
6180 if (info->create_default_symver)
6181 {
6182 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6183 def.vd_next = sizeof (Elf_External_Verdef);
6184 }
6185 else
6186 {
6187 def.vd_aux = sizeof (Elf_External_Verdef);
6188 def.vd_next = (sizeof (Elf_External_Verdef)
6189 + sizeof (Elf_External_Verdaux));
6190 }
5a580b3a 6191
ef53be89 6192 if (soname_indx != (size_t) -1)
5a580b3a
AM
6193 {
6194 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6195 soname_indx);
6196 def.vd_hash = bfd_elf_hash (soname);
6197 defaux.vda_name = soname_indx;
3e3b46e5 6198 name = soname;
5a580b3a
AM
6199 }
6200 else
6201 {
ef53be89 6202 size_t indx;
5a580b3a 6203
06084812 6204 name = lbasename (output_bfd->filename);
5a580b3a
AM
6205 def.vd_hash = bfd_elf_hash (name);
6206 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6207 name, FALSE);
ef53be89 6208 if (indx == (size_t) -1)
5a580b3a
AM
6209 return FALSE;
6210 defaux.vda_name = indx;
6211 }
6212 defaux.vda_next = 0;
6213
6214 _bfd_elf_swap_verdef_out (output_bfd, &def,
6215 (Elf_External_Verdef *) p);
6216 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6217 if (info->create_default_symver)
6218 {
6219 /* Add a symbol representing this version. */
6220 bh = NULL;
6221 if (! (_bfd_generic_link_add_one_symbol
6222 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6223 0, NULL, FALSE,
6224 get_elf_backend_data (dynobj)->collect, &bh)))
6225 return FALSE;
6226 h = (struct elf_link_hash_entry *) bh;
6227 h->non_elf = 0;
6228 h->def_regular = 1;
6229 h->type = STT_OBJECT;
6230 h->verinfo.vertree = NULL;
6231
6232 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6233 return FALSE;
6234
6235 /* Create a duplicate of the base version with the same
6236 aux block, but different flags. */
6237 def.vd_flags = 0;
6238 def.vd_ndx = 2;
6239 def.vd_aux = sizeof (Elf_External_Verdef);
6240 if (verdefs)
6241 def.vd_next = (sizeof (Elf_External_Verdef)
6242 + sizeof (Elf_External_Verdaux));
6243 else
6244 def.vd_next = 0;
6245 _bfd_elf_swap_verdef_out (output_bfd, &def,
6246 (Elf_External_Verdef *) p);
6247 p += sizeof (Elf_External_Verdef);
6248 }
5a580b3a
AM
6249 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6250 (Elf_External_Verdaux *) p);
6251 p += sizeof (Elf_External_Verdaux);
6252
6253 for (t = verdefs; t != NULL; t = t->next)
6254 {
6255 unsigned int cdeps;
6256 struct bfd_elf_version_deps *n;
5a580b3a 6257
a6cc6b3b
RO
6258 /* Don't emit the base version twice. */
6259 if (t->vernum == 0)
6260 continue;
6261
5a580b3a
AM
6262 cdeps = 0;
6263 for (n = t->deps; n != NULL; n = n->next)
6264 ++cdeps;
6265
6266 /* Add a symbol representing this version. */
6267 bh = NULL;
6268 if (! (_bfd_generic_link_add_one_symbol
6269 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6270 0, NULL, FALSE,
6271 get_elf_backend_data (dynobj)->collect, &bh)))
6272 return FALSE;
6273 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6274 h->non_elf = 0;
6275 h->def_regular = 1;
5a580b3a
AM
6276 h->type = STT_OBJECT;
6277 h->verinfo.vertree = t;
6278
c152c796 6279 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6280 return FALSE;
6281
6282 def.vd_version = VER_DEF_CURRENT;
6283 def.vd_flags = 0;
6284 if (t->globals.list == NULL
6285 && t->locals.list == NULL
6286 && ! t->used)
6287 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6288 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6289 def.vd_cnt = cdeps + 1;
6290 def.vd_hash = bfd_elf_hash (t->name);
6291 def.vd_aux = sizeof (Elf_External_Verdef);
6292 def.vd_next = 0;
a6cc6b3b
RO
6293
6294 /* If a basever node is next, it *must* be the last node in
6295 the chain, otherwise Verdef construction breaks. */
6296 if (t->next != NULL && t->next->vernum == 0)
6297 BFD_ASSERT (t->next->next == NULL);
6298
6299 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6300 def.vd_next = (sizeof (Elf_External_Verdef)
6301 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6302
6303 _bfd_elf_swap_verdef_out (output_bfd, &def,
6304 (Elf_External_Verdef *) p);
6305 p += sizeof (Elf_External_Verdef);
6306
6307 defaux.vda_name = h->dynstr_index;
6308 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6309 h->dynstr_index);
6310 defaux.vda_next = 0;
6311 if (t->deps != NULL)
6312 defaux.vda_next = sizeof (Elf_External_Verdaux);
6313 t->name_indx = defaux.vda_name;
6314
6315 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6316 (Elf_External_Verdaux *) p);
6317 p += sizeof (Elf_External_Verdaux);
6318
6319 for (n = t->deps; n != NULL; n = n->next)
6320 {
6321 if (n->version_needed == NULL)
6322 {
6323 /* This can happen if there was an error in the
6324 version script. */
6325 defaux.vda_name = 0;
6326 }
6327 else
6328 {
6329 defaux.vda_name = n->version_needed->name_indx;
6330 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6331 defaux.vda_name);
6332 }
6333 if (n->next == NULL)
6334 defaux.vda_next = 0;
6335 else
6336 defaux.vda_next = sizeof (Elf_External_Verdaux);
6337
6338 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6339 (Elf_External_Verdaux *) p);
6340 p += sizeof (Elf_External_Verdaux);
6341 }
6342 }
6343
5a580b3a
AM
6344 elf_tdata (output_bfd)->cverdefs = cdefs;
6345 }
6346
5a580b3a
AM
6347 /* Work out the size of the version reference section. */
6348
3d4d4302 6349 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6350 BFD_ASSERT (s != NULL);
6351 {
6352 struct elf_find_verdep_info sinfo;
6353
5a580b3a
AM
6354 sinfo.info = info;
6355 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6356 if (sinfo.vers == 0)
6357 sinfo.vers = 1;
6358 sinfo.failed = FALSE;
6359
6360 elf_link_hash_traverse (elf_hash_table (info),
6361 _bfd_elf_link_find_version_dependencies,
6362 &sinfo);
14b1c01e
AM
6363 if (sinfo.failed)
6364 return FALSE;
5a580b3a
AM
6365
6366 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6367 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6368 else
6369 {
902e9fc7 6370 Elf_Internal_Verneed *vn;
5a580b3a
AM
6371 unsigned int size;
6372 unsigned int crefs;
6373 bfd_byte *p;
6374
a6cc6b3b 6375 /* Build the version dependency section. */
5a580b3a
AM
6376 size = 0;
6377 crefs = 0;
902e9fc7
MR
6378 for (vn = elf_tdata (output_bfd)->verref;
6379 vn != NULL;
6380 vn = vn->vn_nextref)
5a580b3a
AM
6381 {
6382 Elf_Internal_Vernaux *a;
6383
6384 size += sizeof (Elf_External_Verneed);
6385 ++crefs;
902e9fc7 6386 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6387 size += sizeof (Elf_External_Vernaux);
6388 }
6389
eea6121a 6390 s->size = size;
a50b1753 6391 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6392 if (s->contents == NULL)
6393 return FALSE;
6394
6395 p = s->contents;
902e9fc7
MR
6396 for (vn = elf_tdata (output_bfd)->verref;
6397 vn != NULL;
6398 vn = vn->vn_nextref)
5a580b3a
AM
6399 {
6400 unsigned int caux;
6401 Elf_Internal_Vernaux *a;
ef53be89 6402 size_t indx;
5a580b3a
AM
6403
6404 caux = 0;
902e9fc7 6405 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6406 ++caux;
6407
902e9fc7
MR
6408 vn->vn_version = VER_NEED_CURRENT;
6409 vn->vn_cnt = caux;
5a580b3a 6410 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
902e9fc7
MR
6411 elf_dt_name (vn->vn_bfd) != NULL
6412 ? elf_dt_name (vn->vn_bfd)
6413 : lbasename (vn->vn_bfd->filename),
5a580b3a 6414 FALSE);
ef53be89 6415 if (indx == (size_t) -1)
5a580b3a 6416 return FALSE;
902e9fc7
MR
6417 vn->vn_file = indx;
6418 vn->vn_aux = sizeof (Elf_External_Verneed);
6419 if (vn->vn_nextref == NULL)
6420 vn->vn_next = 0;
5a580b3a 6421 else
902e9fc7 6422 vn->vn_next = (sizeof (Elf_External_Verneed)
5a580b3a
AM
6423 + caux * sizeof (Elf_External_Vernaux));
6424
902e9fc7 6425 _bfd_elf_swap_verneed_out (output_bfd, vn,
5a580b3a
AM
6426 (Elf_External_Verneed *) p);
6427 p += sizeof (Elf_External_Verneed);
6428
902e9fc7 6429 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6430 {
6431 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6432 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6433 a->vna_nodename, FALSE);
ef53be89 6434 if (indx == (size_t) -1)
5a580b3a
AM
6435 return FALSE;
6436 a->vna_name = indx;
6437 if (a->vna_nextptr == NULL)
6438 a->vna_next = 0;
6439 else
6440 a->vna_next = sizeof (Elf_External_Vernaux);
6441
6442 _bfd_elf_swap_vernaux_out (output_bfd, a,
6443 (Elf_External_Vernaux *) p);
6444 p += sizeof (Elf_External_Vernaux);
6445 }
6446 }
6447
5a580b3a
AM
6448 elf_tdata (output_bfd)->cverrefs = crefs;
6449 }
6450 }
902e9fc7
MR
6451 }
6452
6453 bed = get_elf_backend_data (output_bfd);
6454
6455 if (info->gc_sections && bed->can_gc_sections)
6456 {
6457 struct elf_gc_sweep_symbol_info sweep_info;
6458 unsigned long section_sym_count;
6459
6460 /* Remove the symbols that were in the swept sections from the
6461 dynamic symbol table. GCFIXME: Anyone know how to get them
6462 out of the static symbol table as well? */
6463 sweep_info.info = info;
6464 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6465 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6466 &sweep_info);
6467
23ec1e32
MR
6468 /* We need to reassign dynsym indices now that symbols may have
6469 been removed. See the call in `bfd_elf_size_dynsym_hash_dynstr'
6470 for the details of the conditions used here. */
6471 if (elf_hash_table (info)->dynamic_sections_created
6472 || bed->always_renumber_dynsyms)
c46cec3a 6473 _bfd_elf_link_renumber_dynsyms (output_bfd, info, &section_sym_count);
902e9fc7
MR
6474 }
6475
6476 /* Any syms created from now on start with -1 in
6477 got.refcount/offset and plt.refcount/offset. */
6478 elf_hash_table (info)->init_got_refcount
6479 = elf_hash_table (info)->init_got_offset;
6480 elf_hash_table (info)->init_plt_refcount
6481 = elf_hash_table (info)->init_plt_offset;
6482
6483 if (bfd_link_relocatable (info)
6484 && !_bfd_elf_size_group_sections (info))
6485 return FALSE;
6486
6487 /* The backend may have to create some sections regardless of whether
6488 we're dynamic or not. */
6489 if (bed->elf_backend_always_size_sections
6490 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6491 return FALSE;
6492
6493 /* Determine any GNU_STACK segment requirements, after the backend
6494 has had a chance to set a default segment size. */
6495 if (info->execstack)
6496 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6497 else if (info->noexecstack)
6498 elf_stack_flags (output_bfd) = PF_R | PF_W;
6499 else
6500 {
6501 bfd *inputobj;
6502 asection *notesec = NULL;
6503 int exec = 0;
6504
6505 for (inputobj = info->input_bfds;
6506 inputobj;
6507 inputobj = inputobj->link.next)
6508 {
6509 asection *s;
6510
6511 if (inputobj->flags
6512 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6513 continue;
6514 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6515 if (s)
6516 {
6517 if (s->flags & SEC_CODE)
6518 exec = PF_X;
6519 notesec = s;
6520 }
6521 else if (bed->default_execstack)
6522 exec = PF_X;
6523 }
6524 if (notesec || info->stacksize > 0)
6525 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6526 if (notesec && exec && bfd_link_relocatable (info)
6527 && notesec->output_section != bfd_abs_section_ptr)
6528 notesec->output_section->flags |= SEC_CODE;
6529 }
6530
6531 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6532 {
6533 struct elf_info_failed eif;
6534 struct elf_link_hash_entry *h;
6535 asection *dynstr;
6536 asection *s;
6537
6538 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6539 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6540
902e9fc7
MR
6541 if (info->symbolic)
6542 {
6543 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6544 return FALSE;
6545 info->flags |= DF_SYMBOLIC;
6546 }
6547
6548 if (rpath != NULL)
6549 {
6550 size_t indx;
6551 bfd_vma tag;
6552
6553 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6554 TRUE);
6555 if (indx == (size_t) -1)
6556 return FALSE;
6557
6558 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6559 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6560 return FALSE;
6561 }
6562
6563 if (filter_shlib != NULL)
6564 {
6565 size_t indx;
6566
6567 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6568 filter_shlib, TRUE);
6569 if (indx == (size_t) -1
6570 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6571 return FALSE;
6572 }
6573
6574 if (auxiliary_filters != NULL)
6575 {
6576 const char * const *p;
6577
6578 for (p = auxiliary_filters; *p != NULL; p++)
6579 {
6580 size_t indx;
6581
6582 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6583 *p, TRUE);
6584 if (indx == (size_t) -1
6585 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6586 return FALSE;
6587 }
6588 }
6589
6590 if (audit != NULL)
6591 {
6592 size_t indx;
6593
6594 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6595 TRUE);
6596 if (indx == (size_t) -1
6597 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6598 return FALSE;
6599 }
6600
6601 if (depaudit != NULL)
6602 {
6603 size_t indx;
6604
6605 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6606 TRUE);
6607 if (indx == (size_t) -1
6608 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6609 return FALSE;
6610 }
6611
6612 eif.info = info;
6613 eif.failed = FALSE;
6614
6615 /* Find all symbols which were defined in a dynamic object and make
6616 the backend pick a reasonable value for them. */
6617 elf_link_hash_traverse (elf_hash_table (info),
6618 _bfd_elf_adjust_dynamic_symbol,
6619 &eif);
6620 if (eif.failed)
6621 return FALSE;
6622
6623 /* Add some entries to the .dynamic section. We fill in some of the
6624 values later, in bfd_elf_final_link, but we must add the entries
6625 now so that we know the final size of the .dynamic section. */
6626
6627 /* If there are initialization and/or finalization functions to
6628 call then add the corresponding DT_INIT/DT_FINI entries. */
6629 h = (info->init_function
6630 ? elf_link_hash_lookup (elf_hash_table (info),
6631 info->init_function, FALSE,
6632 FALSE, FALSE)
6633 : NULL);
6634 if (h != NULL
6635 && (h->ref_regular
6636 || h->def_regular))
6637 {
6638 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6639 return FALSE;
6640 }
6641 h = (info->fini_function
6642 ? elf_link_hash_lookup (elf_hash_table (info),
6643 info->fini_function, FALSE,
6644 FALSE, FALSE)
6645 : NULL);
6646 if (h != NULL
6647 && (h->ref_regular
6648 || h->def_regular))
6649 {
6650 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6651 return FALSE;
6652 }
6653
6654 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6655 if (s != NULL && s->linker_has_input)
6656 {
6657 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6658 if (! bfd_link_executable (info))
6659 {
6660 bfd *sub;
6661 asection *o;
6662
6663 for (sub = info->input_bfds; sub != NULL;
6664 sub = sub->link.next)
6665 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6666 for (o = sub->sections; o != NULL; o = o->next)
6667 if (elf_section_data (o)->this_hdr.sh_type
6668 == SHT_PREINIT_ARRAY)
6669 {
6670 _bfd_error_handler
6671 (_("%B: .preinit_array section is not allowed in DSO"),
6672 sub);
6673 break;
6674 }
6675
6676 bfd_set_error (bfd_error_nonrepresentable_section);
6677 return FALSE;
6678 }
6679
6680 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6681 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6682 return FALSE;
6683 }
6684 s = bfd_get_section_by_name (output_bfd, ".init_array");
6685 if (s != NULL && s->linker_has_input)
6686 {
6687 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6688 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6689 return FALSE;
6690 }
6691 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6692 if (s != NULL && s->linker_has_input)
6693 {
6694 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6695 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6696 return FALSE;
6697 }
6698
6699 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6700 /* If .dynstr is excluded from the link, we don't want any of
6701 these tags. Strictly, we should be checking each section
6702 individually; This quick check covers for the case where
6703 someone does a /DISCARD/ : { *(*) }. */
6704 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6705 {
6706 bfd_size_type strsize;
6707
6708 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6709 if ((info->emit_hash
6710 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6711 || (info->emit_gnu_hash
6712 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6713 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6714 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6715 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6716 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6717 bed->s->sizeof_sym))
6718 return FALSE;
6719 }
6720 }
6721
6722 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6723 return FALSE;
6724
6725 /* The backend must work out the sizes of all the other dynamic
6726 sections. */
6727 if (dynobj != NULL
6728 && bed->elf_backend_size_dynamic_sections != NULL
6729 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6730 return FALSE;
6731
6732 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6733 {
6734 unsigned long section_sym_count;
6735
6736 if (elf_tdata (output_bfd)->cverdefs)
6737 {
6738 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6739
6740 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6741 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6742 return FALSE;
6743 }
6744
6745 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6746 {
6747 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6748 return FALSE;
6749 }
6750 else if (info->flags & DF_BIND_NOW)
6751 {
6752 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6753 return FALSE;
6754 }
6755
6756 if (info->flags_1)
6757 {
6758 if (bfd_link_executable (info))
6759 info->flags_1 &= ~ (DF_1_INITFIRST
6760 | DF_1_NODELETE
6761 | DF_1_NOOPEN);
6762 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6763 return FALSE;
6764 }
6765
6766 if (elf_tdata (output_bfd)->cverrefs)
6767 {
6768 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6769
6770 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6771 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6772 return FALSE;
6773 }
5a580b3a 6774
8423293d
AM
6775 if ((elf_tdata (output_bfd)->cverrefs == 0
6776 && elf_tdata (output_bfd)->cverdefs == 0)
6777 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6778 &section_sym_count) == 0)
6779 {
902e9fc7
MR
6780 asection *s;
6781
3d4d4302 6782 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6783 s->flags |= SEC_EXCLUDE;
6784 }
6785 }
6786 return TRUE;
6787}
6788
74541ad4
AM
6789/* Find the first non-excluded output section. We'll use its
6790 section symbol for some emitted relocs. */
6791void
6792_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6793{
6794 asection *s;
6795
6796 for (s = output_bfd->sections; s != NULL; s = s->next)
6797 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6798 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6799 {
6800 elf_hash_table (info)->text_index_section = s;
6801 break;
6802 }
6803}
6804
6805/* Find two non-excluded output sections, one for code, one for data.
6806 We'll use their section symbols for some emitted relocs. */
6807void
6808_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6809{
6810 asection *s;
6811
266b05cf
DJ
6812 /* Data first, since setting text_index_section changes
6813 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6814 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6815 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6816 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6817 {
266b05cf 6818 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6819 break;
6820 }
6821
6822 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6823 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6824 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6825 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6826 {
266b05cf 6827 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6828 break;
6829 }
6830
6831 if (elf_hash_table (info)->text_index_section == NULL)
6832 elf_hash_table (info)->text_index_section
6833 = elf_hash_table (info)->data_index_section;
6834}
6835
8423293d
AM
6836bfd_boolean
6837bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6838{
74541ad4 6839 const struct elf_backend_data *bed;
23ec1e32
MR
6840 unsigned long section_sym_count;
6841 bfd_size_type dynsymcount;
74541ad4 6842
8423293d
AM
6843 if (!is_elf_hash_table (info->hash))
6844 return TRUE;
6845
74541ad4
AM
6846 bed = get_elf_backend_data (output_bfd);
6847 (*bed->elf_backend_init_index_section) (output_bfd, info);
6848
23ec1e32
MR
6849 /* Assign dynsym indices. In a shared library we generate a section
6850 symbol for each output section, which come first. Next come all
6851 of the back-end allocated local dynamic syms, followed by the rest
6852 of the global symbols.
6853
6854 This is usually not needed for static binaries, however backends
6855 can request to always do it, e.g. the MIPS backend uses dynamic
6856 symbol counts to lay out GOT, which will be produced in the
6857 presence of GOT relocations even in static binaries (holding fixed
6858 data in that case, to satisfy those relocations). */
6859
6860 if (elf_hash_table (info)->dynamic_sections_created
6861 || bed->always_renumber_dynsyms)
6862 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6863 &section_sym_count);
6864
8423293d
AM
6865 if (elf_hash_table (info)->dynamic_sections_created)
6866 {
6867 bfd *dynobj;
8423293d 6868 asection *s;
8423293d
AM
6869 unsigned int dtagcount;
6870
6871 dynobj = elf_hash_table (info)->dynobj;
6872
5a580b3a 6873 /* Work out the size of the symbol version section. */
3d4d4302 6874 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6875 BFD_ASSERT (s != NULL);
d5486c43 6876 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6877 {
eea6121a 6878 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6879 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6880 if (s->contents == NULL)
6881 return FALSE;
6882
6883 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6884 return FALSE;
6885 }
6886
6887 /* Set the size of the .dynsym and .hash sections. We counted
6888 the number of dynamic symbols in elf_link_add_object_symbols.
6889 We will build the contents of .dynsym and .hash when we build
6890 the final symbol table, because until then we do not know the
6891 correct value to give the symbols. We built the .dynstr
6892 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6893 s = elf_hash_table (info)->dynsym;
5a580b3a 6894 BFD_ASSERT (s != NULL);
eea6121a 6895 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6896
d5486c43
L
6897 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6898 if (s->contents == NULL)
6899 return FALSE;
5a580b3a 6900
d5486c43
L
6901 /* The first entry in .dynsym is a dummy symbol. Clear all the
6902 section syms, in case we don't output them all. */
6903 ++section_sym_count;
6904 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6905
fdc90cb4
JJ
6906 elf_hash_table (info)->bucketcount = 0;
6907
5a580b3a
AM
6908 /* Compute the size of the hashing table. As a side effect this
6909 computes the hash values for all the names we export. */
fdc90cb4
JJ
6910 if (info->emit_hash)
6911 {
6912 unsigned long int *hashcodes;
14b1c01e 6913 struct hash_codes_info hashinf;
fdc90cb4
JJ
6914 bfd_size_type amt;
6915 unsigned long int nsyms;
6916 size_t bucketcount;
6917 size_t hash_entry_size;
6918
6919 /* Compute the hash values for all exported symbols. At the same
6920 time store the values in an array so that we could use them for
6921 optimizations. */
6922 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6923 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6924 if (hashcodes == NULL)
6925 return FALSE;
14b1c01e
AM
6926 hashinf.hashcodes = hashcodes;
6927 hashinf.error = FALSE;
5a580b3a 6928
fdc90cb4
JJ
6929 /* Put all hash values in HASHCODES. */
6930 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6931 elf_collect_hash_codes, &hashinf);
6932 if (hashinf.error)
4dd07732
AM
6933 {
6934 free (hashcodes);
6935 return FALSE;
6936 }
5a580b3a 6937
14b1c01e 6938 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6939 bucketcount
6940 = compute_bucket_count (info, hashcodes, nsyms, 0);
6941 free (hashcodes);
6942
6943 if (bucketcount == 0)
6944 return FALSE;
5a580b3a 6945
fdc90cb4
JJ
6946 elf_hash_table (info)->bucketcount = bucketcount;
6947
3d4d4302 6948 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6949 BFD_ASSERT (s != NULL);
6950 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6951 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6952 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6953 if (s->contents == NULL)
6954 return FALSE;
6955
6956 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6957 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6958 s->contents + hash_entry_size);
6959 }
6960
6961 if (info->emit_gnu_hash)
6962 {
6963 size_t i, cnt;
6964 unsigned char *contents;
6965 struct collect_gnu_hash_codes cinfo;
6966 bfd_size_type amt;
6967 size_t bucketcount;
6968
6969 memset (&cinfo, 0, sizeof (cinfo));
6970
6971 /* Compute the hash values for all exported symbols. At the same
6972 time store the values in an array so that we could use them for
6973 optimizations. */
6974 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6975 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6976 if (cinfo.hashcodes == NULL)
6977 return FALSE;
6978
6979 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6980 cinfo.min_dynindx = -1;
6981 cinfo.output_bfd = output_bfd;
6982 cinfo.bed = bed;
6983
6984 /* Put all hash values in HASHCODES. */
6985 elf_link_hash_traverse (elf_hash_table (info),
6986 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6987 if (cinfo.error)
4dd07732
AM
6988 {
6989 free (cinfo.hashcodes);
6990 return FALSE;
6991 }
fdc90cb4
JJ
6992
6993 bucketcount
6994 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6995
6996 if (bucketcount == 0)
6997 {
6998 free (cinfo.hashcodes);
6999 return FALSE;
7000 }
7001
3d4d4302 7002 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7003 BFD_ASSERT (s != NULL);
7004
7005 if (cinfo.nsyms == 0)
7006 {
7007 /* Empty .gnu.hash section is special. */
7008 BFD_ASSERT (cinfo.min_dynindx == -1);
7009 free (cinfo.hashcodes);
7010 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7011 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7012 if (contents == NULL)
7013 return FALSE;
7014 s->contents = contents;
7015 /* 1 empty bucket. */
7016 bfd_put_32 (output_bfd, 1, contents);
7017 /* SYMIDX above the special symbol 0. */
7018 bfd_put_32 (output_bfd, 1, contents + 4);
7019 /* Just one word for bitmask. */
7020 bfd_put_32 (output_bfd, 1, contents + 8);
7021 /* Only hash fn bloom filter. */
7022 bfd_put_32 (output_bfd, 0, contents + 12);
7023 /* No hashes are valid - empty bitmask. */
7024 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7025 /* No hashes in the only bucket. */
7026 bfd_put_32 (output_bfd, 0,
7027 contents + 16 + bed->s->arch_size / 8);
7028 }
7029 else
7030 {
9e6619e2 7031 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7032 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7033
9e6619e2
AM
7034 x = cinfo.nsyms;
7035 maskbitslog2 = 1;
7036 while ((x >>= 1) != 0)
7037 ++maskbitslog2;
fdc90cb4
JJ
7038 if (maskbitslog2 < 3)
7039 maskbitslog2 = 5;
7040 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7041 maskbitslog2 = maskbitslog2 + 3;
7042 else
7043 maskbitslog2 = maskbitslog2 + 2;
7044 if (bed->s->arch_size == 64)
7045 {
7046 if (maskbitslog2 == 5)
7047 maskbitslog2 = 6;
7048 cinfo.shift1 = 6;
7049 }
7050 else
7051 cinfo.shift1 = 5;
7052 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7053 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7054 cinfo.maskbits = 1 << maskbitslog2;
7055 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7056 amt = bucketcount * sizeof (unsigned long int) * 2;
7057 amt += maskwords * sizeof (bfd_vma);
a50b1753 7058 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7059 if (cinfo.bitmask == NULL)
7060 {
7061 free (cinfo.hashcodes);
7062 return FALSE;
7063 }
7064
a50b1753 7065 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7066 cinfo.indx = cinfo.counts + bucketcount;
7067 cinfo.symindx = dynsymcount - cinfo.nsyms;
7068 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7069
7070 /* Determine how often each hash bucket is used. */
7071 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7072 for (i = 0; i < cinfo.nsyms; ++i)
7073 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7074
7075 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7076 if (cinfo.counts[i] != 0)
7077 {
7078 cinfo.indx[i] = cnt;
7079 cnt += cinfo.counts[i];
7080 }
7081 BFD_ASSERT (cnt == dynsymcount);
7082 cinfo.bucketcount = bucketcount;
7083 cinfo.local_indx = cinfo.min_dynindx;
7084
7085 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7086 s->size += cinfo.maskbits / 8;
a50b1753 7087 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7088 if (contents == NULL)
7089 {
7090 free (cinfo.bitmask);
7091 free (cinfo.hashcodes);
7092 return FALSE;
7093 }
7094
7095 s->contents = contents;
7096 bfd_put_32 (output_bfd, bucketcount, contents);
7097 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7098 bfd_put_32 (output_bfd, maskwords, contents + 8);
7099 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7100 contents += 16 + cinfo.maskbits / 8;
7101
7102 for (i = 0; i < bucketcount; ++i)
7103 {
7104 if (cinfo.counts[i] == 0)
7105 bfd_put_32 (output_bfd, 0, contents);
7106 else
7107 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7108 contents += 4;
7109 }
7110
7111 cinfo.contents = contents;
7112
7113 /* Renumber dynamic symbols, populate .gnu.hash section. */
7114 elf_link_hash_traverse (elf_hash_table (info),
7115 elf_renumber_gnu_hash_syms, &cinfo);
7116
7117 contents = s->contents + 16;
7118 for (i = 0; i < maskwords; ++i)
7119 {
7120 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7121 contents);
7122 contents += bed->s->arch_size / 8;
7123 }
7124
7125 free (cinfo.bitmask);
7126 free (cinfo.hashcodes);
7127 }
7128 }
5a580b3a 7129
3d4d4302 7130 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7131 BFD_ASSERT (s != NULL);
7132
4ad4eba5 7133 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7134
eea6121a 7135 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7136
7137 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7138 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7139 return FALSE;
7140 }
7141
7142 return TRUE;
7143}
4d269e42 7144\f
4d269e42
AM
7145/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7146
7147static void
7148merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7149 asection *sec)
7150{
dbaa2011
AM
7151 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7152 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7153}
7154
7155/* Finish SHF_MERGE section merging. */
7156
7157bfd_boolean
630993ec 7158_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7159{
7160 bfd *ibfd;
7161 asection *sec;
7162
7163 if (!is_elf_hash_table (info->hash))
7164 return FALSE;
7165
c72f2fb2 7166 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7167 if ((ibfd->flags & DYNAMIC) == 0
7168 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7169 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7170 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7171 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7172 if ((sec->flags & SEC_MERGE) != 0
7173 && !bfd_is_abs_section (sec->output_section))
7174 {
7175 struct bfd_elf_section_data *secdata;
7176
7177 secdata = elf_section_data (sec);
630993ec 7178 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7179 &elf_hash_table (info)->merge_info,
7180 sec, &secdata->sec_info))
7181 return FALSE;
7182 else if (secdata->sec_info)
dbaa2011 7183 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7184 }
7185
7186 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7187 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7188 merge_sections_remove_hook);
7189 return TRUE;
7190}
7191
7192/* Create an entry in an ELF linker hash table. */
7193
7194struct bfd_hash_entry *
7195_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7196 struct bfd_hash_table *table,
7197 const char *string)
7198{
7199 /* Allocate the structure if it has not already been allocated by a
7200 subclass. */
7201 if (entry == NULL)
7202 {
a50b1753 7203 entry = (struct bfd_hash_entry *)
ca4be51c 7204 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7205 if (entry == NULL)
7206 return entry;
7207 }
7208
7209 /* Call the allocation method of the superclass. */
7210 entry = _bfd_link_hash_newfunc (entry, table, string);
7211 if (entry != NULL)
7212 {
7213 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7214 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7215
7216 /* Set local fields. */
7217 ret->indx = -1;
7218 ret->dynindx = -1;
7219 ret->got = htab->init_got_refcount;
7220 ret->plt = htab->init_plt_refcount;
7221 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7222 - offsetof (struct elf_link_hash_entry, size)));
7223 /* Assume that we have been called by a non-ELF symbol reader.
7224 This flag is then reset by the code which reads an ELF input
7225 file. This ensures that a symbol created by a non-ELF symbol
7226 reader will have the flag set correctly. */
7227 ret->non_elf = 1;
7228 }
7229
7230 return entry;
7231}
7232
7233/* Copy data from an indirect symbol to its direct symbol, hiding the
7234 old indirect symbol. Also used for copying flags to a weakdef. */
7235
7236void
7237_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7238 struct elf_link_hash_entry *dir,
7239 struct elf_link_hash_entry *ind)
7240{
7241 struct elf_link_hash_table *htab;
7242
7243 /* Copy down any references that we may have already seen to the
e81830c5 7244 symbol which just became indirect. */
4d269e42 7245
422f1182 7246 if (dir->versioned != versioned_hidden)
e81830c5
AM
7247 dir->ref_dynamic |= ind->ref_dynamic;
7248 dir->ref_regular |= ind->ref_regular;
7249 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7250 dir->non_got_ref |= ind->non_got_ref;
7251 dir->needs_plt |= ind->needs_plt;
7252 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7253
7254 if (ind->root.type != bfd_link_hash_indirect)
7255 return;
7256
7257 /* Copy over the global and procedure linkage table refcount entries.
7258 These may have been already set up by a check_relocs routine. */
7259 htab = elf_hash_table (info);
7260 if (ind->got.refcount > htab->init_got_refcount.refcount)
7261 {
7262 if (dir->got.refcount < 0)
7263 dir->got.refcount = 0;
7264 dir->got.refcount += ind->got.refcount;
7265 ind->got.refcount = htab->init_got_refcount.refcount;
7266 }
7267
7268 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7269 {
7270 if (dir->plt.refcount < 0)
7271 dir->plt.refcount = 0;
7272 dir->plt.refcount += ind->plt.refcount;
7273 ind->plt.refcount = htab->init_plt_refcount.refcount;
7274 }
7275
7276 if (ind->dynindx != -1)
7277 {
7278 if (dir->dynindx != -1)
7279 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7280 dir->dynindx = ind->dynindx;
7281 dir->dynstr_index = ind->dynstr_index;
7282 ind->dynindx = -1;
7283 ind->dynstr_index = 0;
7284 }
7285}
7286
7287void
7288_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7289 struct elf_link_hash_entry *h,
7290 bfd_boolean force_local)
7291{
3aa14d16
L
7292 /* STT_GNU_IFUNC symbol must go through PLT. */
7293 if (h->type != STT_GNU_IFUNC)
7294 {
7295 h->plt = elf_hash_table (info)->init_plt_offset;
7296 h->needs_plt = 0;
7297 }
4d269e42
AM
7298 if (force_local)
7299 {
7300 h->forced_local = 1;
7301 if (h->dynindx != -1)
7302 {
4d269e42
AM
7303 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7304 h->dynstr_index);
641338d8
AM
7305 h->dynindx = -1;
7306 h->dynstr_index = 0;
4d269e42
AM
7307 }
7308 }
7309}
7310
7bf52ea2
AM
7311/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7312 caller. */
4d269e42
AM
7313
7314bfd_boolean
7315_bfd_elf_link_hash_table_init
7316 (struct elf_link_hash_table *table,
7317 bfd *abfd,
7318 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7319 struct bfd_hash_table *,
7320 const char *),
4dfe6ac6
NC
7321 unsigned int entsize,
7322 enum elf_target_id target_id)
4d269e42
AM
7323{
7324 bfd_boolean ret;
7325 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7326
4d269e42
AM
7327 table->init_got_refcount.refcount = can_refcount - 1;
7328 table->init_plt_refcount.refcount = can_refcount - 1;
7329 table->init_got_offset.offset = -(bfd_vma) 1;
7330 table->init_plt_offset.offset = -(bfd_vma) 1;
7331 /* The first dynamic symbol is a dummy. */
7332 table->dynsymcount = 1;
7333
7334 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7335
4d269e42 7336 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7337 table->hash_table_id = target_id;
4d269e42
AM
7338
7339 return ret;
7340}
7341
7342/* Create an ELF linker hash table. */
7343
7344struct bfd_link_hash_table *
7345_bfd_elf_link_hash_table_create (bfd *abfd)
7346{
7347 struct elf_link_hash_table *ret;
7348 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7349
7bf52ea2 7350 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7351 if (ret == NULL)
7352 return NULL;
7353
7354 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7355 sizeof (struct elf_link_hash_entry),
7356 GENERIC_ELF_DATA))
4d269e42
AM
7357 {
7358 free (ret);
7359 return NULL;
7360 }
d495ab0d 7361 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7362
7363 return &ret->root;
7364}
7365
9f7c3e5e
AM
7366/* Destroy an ELF linker hash table. */
7367
7368void
d495ab0d 7369_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7370{
d495ab0d
AM
7371 struct elf_link_hash_table *htab;
7372
7373 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7374 if (htab->dynstr != NULL)
7375 _bfd_elf_strtab_free (htab->dynstr);
7376 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7377 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7378}
7379
4d269e42
AM
7380/* This is a hook for the ELF emulation code in the generic linker to
7381 tell the backend linker what file name to use for the DT_NEEDED
7382 entry for a dynamic object. */
7383
7384void
7385bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7386{
7387 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7388 && bfd_get_format (abfd) == bfd_object)
7389 elf_dt_name (abfd) = name;
7390}
7391
7392int
7393bfd_elf_get_dyn_lib_class (bfd *abfd)
7394{
7395 int lib_class;
7396 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7397 && bfd_get_format (abfd) == bfd_object)
7398 lib_class = elf_dyn_lib_class (abfd);
7399 else
7400 lib_class = 0;
7401 return lib_class;
7402}
7403
7404void
7405bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7406{
7407 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7408 && bfd_get_format (abfd) == bfd_object)
7409 elf_dyn_lib_class (abfd) = lib_class;
7410}
7411
7412/* Get the list of DT_NEEDED entries for a link. This is a hook for
7413 the linker ELF emulation code. */
7414
7415struct bfd_link_needed_list *
7416bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7417 struct bfd_link_info *info)
7418{
7419 if (! is_elf_hash_table (info->hash))
7420 return NULL;
7421 return elf_hash_table (info)->needed;
7422}
7423
7424/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7425 hook for the linker ELF emulation code. */
7426
7427struct bfd_link_needed_list *
7428bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7429 struct bfd_link_info *info)
7430{
7431 if (! is_elf_hash_table (info->hash))
7432 return NULL;
7433 return elf_hash_table (info)->runpath;
7434}
7435
7436/* Get the name actually used for a dynamic object for a link. This
7437 is the SONAME entry if there is one. Otherwise, it is the string
7438 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7439
7440const char *
7441bfd_elf_get_dt_soname (bfd *abfd)
7442{
7443 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7444 && bfd_get_format (abfd) == bfd_object)
7445 return elf_dt_name (abfd);
7446 return NULL;
7447}
7448
7449/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7450 the ELF linker emulation code. */
7451
7452bfd_boolean
7453bfd_elf_get_bfd_needed_list (bfd *abfd,
7454 struct bfd_link_needed_list **pneeded)
7455{
7456 asection *s;
7457 bfd_byte *dynbuf = NULL;
cb33740c 7458 unsigned int elfsec;
4d269e42
AM
7459 unsigned long shlink;
7460 bfd_byte *extdyn, *extdynend;
7461 size_t extdynsize;
7462 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7463
7464 *pneeded = NULL;
7465
7466 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7467 || bfd_get_format (abfd) != bfd_object)
7468 return TRUE;
7469
7470 s = bfd_get_section_by_name (abfd, ".dynamic");
7471 if (s == NULL || s->size == 0)
7472 return TRUE;
7473
7474 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7475 goto error_return;
7476
7477 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7478 if (elfsec == SHN_BAD)
4d269e42
AM
7479 goto error_return;
7480
7481 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7482
4d269e42
AM
7483 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7484 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7485
7486 extdyn = dynbuf;
7487 extdynend = extdyn + s->size;
7488 for (; extdyn < extdynend; extdyn += extdynsize)
7489 {
7490 Elf_Internal_Dyn dyn;
7491
7492 (*swap_dyn_in) (abfd, extdyn, &dyn);
7493
7494 if (dyn.d_tag == DT_NULL)
7495 break;
7496
7497 if (dyn.d_tag == DT_NEEDED)
7498 {
7499 const char *string;
7500 struct bfd_link_needed_list *l;
7501 unsigned int tagv = dyn.d_un.d_val;
7502 bfd_size_type amt;
7503
7504 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7505 if (string == NULL)
7506 goto error_return;
7507
7508 amt = sizeof *l;
a50b1753 7509 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7510 if (l == NULL)
7511 goto error_return;
7512
7513 l->by = abfd;
7514 l->name = string;
7515 l->next = *pneeded;
7516 *pneeded = l;
7517 }
7518 }
7519
7520 free (dynbuf);
7521
7522 return TRUE;
7523
7524 error_return:
7525 if (dynbuf != NULL)
7526 free (dynbuf);
7527 return FALSE;
7528}
7529
7530struct elf_symbuf_symbol
7531{
7532 unsigned long st_name; /* Symbol name, index in string tbl */
7533 unsigned char st_info; /* Type and binding attributes */
7534 unsigned char st_other; /* Visibilty, and target specific */
7535};
7536
7537struct elf_symbuf_head
7538{
7539 struct elf_symbuf_symbol *ssym;
ef53be89 7540 size_t count;
4d269e42
AM
7541 unsigned int st_shndx;
7542};
7543
7544struct elf_symbol
7545{
7546 union
7547 {
7548 Elf_Internal_Sym *isym;
7549 struct elf_symbuf_symbol *ssym;
7550 } u;
7551 const char *name;
7552};
7553
7554/* Sort references to symbols by ascending section number. */
7555
7556static int
7557elf_sort_elf_symbol (const void *arg1, const void *arg2)
7558{
7559 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7560 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7561
7562 return s1->st_shndx - s2->st_shndx;
7563}
7564
7565static int
7566elf_sym_name_compare (const void *arg1, const void *arg2)
7567{
7568 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7569 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7570 return strcmp (s1->name, s2->name);
7571}
7572
7573static struct elf_symbuf_head *
ef53be89 7574elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7575{
14b1c01e 7576 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7577 struct elf_symbuf_symbol *ssym;
7578 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7579 size_t i, shndx_count, total_size;
4d269e42 7580
a50b1753 7581 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7582 if (indbuf == NULL)
7583 return NULL;
7584
7585 for (ind = indbuf, i = 0; i < symcount; i++)
7586 if (isymbuf[i].st_shndx != SHN_UNDEF)
7587 *ind++ = &isymbuf[i];
7588 indbufend = ind;
7589
7590 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7591 elf_sort_elf_symbol);
7592
7593 shndx_count = 0;
7594 if (indbufend > indbuf)
7595 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7596 if (ind[0]->st_shndx != ind[1]->st_shndx)
7597 shndx_count++;
7598
3ae181ee
L
7599 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7600 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7601 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7602 if (ssymbuf == NULL)
7603 {
7604 free (indbuf);
7605 return NULL;
7606 }
7607
3ae181ee 7608 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7609 ssymbuf->ssym = NULL;
7610 ssymbuf->count = shndx_count;
7611 ssymbuf->st_shndx = 0;
7612 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7613 {
7614 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7615 {
7616 ssymhead++;
7617 ssymhead->ssym = ssym;
7618 ssymhead->count = 0;
7619 ssymhead->st_shndx = (*ind)->st_shndx;
7620 }
7621 ssym->st_name = (*ind)->st_name;
7622 ssym->st_info = (*ind)->st_info;
7623 ssym->st_other = (*ind)->st_other;
7624 ssymhead->count++;
7625 }
ef53be89 7626 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7627 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7628 == total_size));
4d269e42
AM
7629
7630 free (indbuf);
7631 return ssymbuf;
7632}
7633
7634/* Check if 2 sections define the same set of local and global
7635 symbols. */
7636
8f317e31 7637static bfd_boolean
4d269e42
AM
7638bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7639 struct bfd_link_info *info)
7640{
7641 bfd *bfd1, *bfd2;
7642 const struct elf_backend_data *bed1, *bed2;
7643 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7644 size_t symcount1, symcount2;
4d269e42
AM
7645 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7646 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7647 Elf_Internal_Sym *isym, *isymend;
7648 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7649 size_t count1, count2, i;
cb33740c 7650 unsigned int shndx1, shndx2;
4d269e42
AM
7651 bfd_boolean result;
7652
7653 bfd1 = sec1->owner;
7654 bfd2 = sec2->owner;
7655
4d269e42
AM
7656 /* Both sections have to be in ELF. */
7657 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7658 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7659 return FALSE;
7660
7661 if (elf_section_type (sec1) != elf_section_type (sec2))
7662 return FALSE;
7663
4d269e42
AM
7664 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7665 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7666 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7667 return FALSE;
7668
7669 bed1 = get_elf_backend_data (bfd1);
7670 bed2 = get_elf_backend_data (bfd2);
7671 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7672 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7673 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7674 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7675
7676 if (symcount1 == 0 || symcount2 == 0)
7677 return FALSE;
7678
7679 result = FALSE;
7680 isymbuf1 = NULL;
7681 isymbuf2 = NULL;
a50b1753
NC
7682 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7683 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7684
7685 if (ssymbuf1 == NULL)
7686 {
7687 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7688 NULL, NULL, NULL);
7689 if (isymbuf1 == NULL)
7690 goto done;
7691
7692 if (!info->reduce_memory_overheads)
7693 elf_tdata (bfd1)->symbuf = ssymbuf1
7694 = elf_create_symbuf (symcount1, isymbuf1);
7695 }
7696
7697 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7698 {
7699 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7700 NULL, NULL, NULL);
7701 if (isymbuf2 == NULL)
7702 goto done;
7703
7704 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7705 elf_tdata (bfd2)->symbuf = ssymbuf2
7706 = elf_create_symbuf (symcount2, isymbuf2);
7707 }
7708
7709 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7710 {
7711 /* Optimized faster version. */
ef53be89 7712 size_t lo, hi, mid;
4d269e42
AM
7713 struct elf_symbol *symp;
7714 struct elf_symbuf_symbol *ssym, *ssymend;
7715
7716 lo = 0;
7717 hi = ssymbuf1->count;
7718 ssymbuf1++;
7719 count1 = 0;
7720 while (lo < hi)
7721 {
7722 mid = (lo + hi) / 2;
cb33740c 7723 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7724 hi = mid;
cb33740c 7725 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7726 lo = mid + 1;
7727 else
7728 {
7729 count1 = ssymbuf1[mid].count;
7730 ssymbuf1 += mid;
7731 break;
7732 }
7733 }
7734
7735 lo = 0;
7736 hi = ssymbuf2->count;
7737 ssymbuf2++;
7738 count2 = 0;
7739 while (lo < hi)
7740 {
7741 mid = (lo + hi) / 2;
cb33740c 7742 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7743 hi = mid;
cb33740c 7744 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7745 lo = mid + 1;
7746 else
7747 {
7748 count2 = ssymbuf2[mid].count;
7749 ssymbuf2 += mid;
7750 break;
7751 }
7752 }
7753
7754 if (count1 == 0 || count2 == 0 || count1 != count2)
7755 goto done;
7756
ca4be51c
AM
7757 symtable1
7758 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7759 symtable2
7760 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7761 if (symtable1 == NULL || symtable2 == NULL)
7762 goto done;
7763
7764 symp = symtable1;
7765 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7766 ssym < ssymend; ssym++, symp++)
7767 {
7768 symp->u.ssym = ssym;
7769 symp->name = bfd_elf_string_from_elf_section (bfd1,
7770 hdr1->sh_link,
7771 ssym->st_name);
7772 }
7773
7774 symp = symtable2;
7775 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7776 ssym < ssymend; ssym++, symp++)
7777 {
7778 symp->u.ssym = ssym;
7779 symp->name = bfd_elf_string_from_elf_section (bfd2,
7780 hdr2->sh_link,
7781 ssym->st_name);
7782 }
7783
7784 /* Sort symbol by name. */
7785 qsort (symtable1, count1, sizeof (struct elf_symbol),
7786 elf_sym_name_compare);
7787 qsort (symtable2, count1, sizeof (struct elf_symbol),
7788 elf_sym_name_compare);
7789
7790 for (i = 0; i < count1; i++)
7791 /* Two symbols must have the same binding, type and name. */
7792 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7793 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7794 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7795 goto done;
7796
7797 result = TRUE;
7798 goto done;
7799 }
7800
a50b1753
NC
7801 symtable1 = (struct elf_symbol *)
7802 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7803 symtable2 = (struct elf_symbol *)
7804 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7805 if (symtable1 == NULL || symtable2 == NULL)
7806 goto done;
7807
7808 /* Count definitions in the section. */
7809 count1 = 0;
7810 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7811 if (isym->st_shndx == shndx1)
4d269e42
AM
7812 symtable1[count1++].u.isym = isym;
7813
7814 count2 = 0;
7815 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7816 if (isym->st_shndx == shndx2)
4d269e42
AM
7817 symtable2[count2++].u.isym = isym;
7818
7819 if (count1 == 0 || count2 == 0 || count1 != count2)
7820 goto done;
7821
7822 for (i = 0; i < count1; i++)
7823 symtable1[i].name
7824 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7825 symtable1[i].u.isym->st_name);
7826
7827 for (i = 0; i < count2; i++)
7828 symtable2[i].name
7829 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7830 symtable2[i].u.isym->st_name);
7831
7832 /* Sort symbol by name. */
7833 qsort (symtable1, count1, sizeof (struct elf_symbol),
7834 elf_sym_name_compare);
7835 qsort (symtable2, count1, sizeof (struct elf_symbol),
7836 elf_sym_name_compare);
7837
7838 for (i = 0; i < count1; i++)
7839 /* Two symbols must have the same binding, type and name. */
7840 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7841 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7842 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7843 goto done;
7844
7845 result = TRUE;
7846
7847done:
7848 if (symtable1)
7849 free (symtable1);
7850 if (symtable2)
7851 free (symtable2);
7852 if (isymbuf1)
7853 free (isymbuf1);
7854 if (isymbuf2)
7855 free (isymbuf2);
7856
7857 return result;
7858}
7859
7860/* Return TRUE if 2 section types are compatible. */
7861
7862bfd_boolean
7863_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7864 bfd *bbfd, const asection *bsec)
7865{
7866 if (asec == NULL
7867 || bsec == NULL
7868 || abfd->xvec->flavour != bfd_target_elf_flavour
7869 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7870 return TRUE;
7871
7872 return elf_section_type (asec) == elf_section_type (bsec);
7873}
7874\f
c152c796
AM
7875/* Final phase of ELF linker. */
7876
7877/* A structure we use to avoid passing large numbers of arguments. */
7878
7879struct elf_final_link_info
7880{
7881 /* General link information. */
7882 struct bfd_link_info *info;
7883 /* Output BFD. */
7884 bfd *output_bfd;
7885 /* Symbol string table. */
ef10c3ac 7886 struct elf_strtab_hash *symstrtab;
c152c796
AM
7887 /* .hash section. */
7888 asection *hash_sec;
7889 /* symbol version section (.gnu.version). */
7890 asection *symver_sec;
7891 /* Buffer large enough to hold contents of any section. */
7892 bfd_byte *contents;
7893 /* Buffer large enough to hold external relocs of any section. */
7894 void *external_relocs;
7895 /* Buffer large enough to hold internal relocs of any section. */
7896 Elf_Internal_Rela *internal_relocs;
7897 /* Buffer large enough to hold external local symbols of any input
7898 BFD. */
7899 bfd_byte *external_syms;
7900 /* And a buffer for symbol section indices. */
7901 Elf_External_Sym_Shndx *locsym_shndx;
7902 /* Buffer large enough to hold internal local symbols of any input
7903 BFD. */
7904 Elf_Internal_Sym *internal_syms;
7905 /* Array large enough to hold a symbol index for each local symbol
7906 of any input BFD. */
7907 long *indices;
7908 /* Array large enough to hold a section pointer for each local
7909 symbol of any input BFD. */
7910 asection **sections;
ef10c3ac 7911 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7912 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7913 /* Number of STT_FILE syms seen. */
7914 size_t filesym_count;
c152c796
AM
7915};
7916
7917/* This struct is used to pass information to elf_link_output_extsym. */
7918
7919struct elf_outext_info
7920{
7921 bfd_boolean failed;
7922 bfd_boolean localsyms;
34a79995 7923 bfd_boolean file_sym_done;
8b127cbc 7924 struct elf_final_link_info *flinfo;
c152c796
AM
7925};
7926
d9352518
DB
7927
7928/* Support for evaluating a complex relocation.
7929
7930 Complex relocations are generalized, self-describing relocations. The
7931 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7932 relocations themselves.
d9352518
DB
7933
7934 The relocations are use a reserved elf-wide relocation type code (R_RELC
7935 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7936 information (start bit, end bit, word width, etc) into the addend. This
7937 information is extracted from CGEN-generated operand tables within gas.
7938
7939 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7940 internal) representing prefix-notation expressions, including but not
7941 limited to those sorts of expressions normally encoded as addends in the
7942 addend field. The symbol mangling format is:
7943
7944 <node> := <literal>
7945 | <unary-operator> ':' <node>
7946 | <binary-operator> ':' <node> ':' <node>
7947 ;
7948
7949 <literal> := 's' <digits=N> ':' <N character symbol name>
7950 | 'S' <digits=N> ':' <N character section name>
7951 | '#' <hexdigits>
7952 ;
7953
7954 <binary-operator> := as in C
7955 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7956
7957static void
a0c8462f
AM
7958set_symbol_value (bfd *bfd_with_globals,
7959 Elf_Internal_Sym *isymbuf,
7960 size_t locsymcount,
7961 size_t symidx,
7962 bfd_vma val)
d9352518 7963{
8977835c
AM
7964 struct elf_link_hash_entry **sym_hashes;
7965 struct elf_link_hash_entry *h;
7966 size_t extsymoff = locsymcount;
d9352518 7967
8977835c 7968 if (symidx < locsymcount)
d9352518 7969 {
8977835c
AM
7970 Elf_Internal_Sym *sym;
7971
7972 sym = isymbuf + symidx;
7973 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7974 {
7975 /* It is a local symbol: move it to the
7976 "absolute" section and give it a value. */
7977 sym->st_shndx = SHN_ABS;
7978 sym->st_value = val;
7979 return;
7980 }
7981 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7982 extsymoff = 0;
d9352518 7983 }
8977835c
AM
7984
7985 /* It is a global symbol: set its link type
7986 to "defined" and give it a value. */
7987
7988 sym_hashes = elf_sym_hashes (bfd_with_globals);
7989 h = sym_hashes [symidx - extsymoff];
7990 while (h->root.type == bfd_link_hash_indirect
7991 || h->root.type == bfd_link_hash_warning)
7992 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7993 h->root.type = bfd_link_hash_defined;
7994 h->root.u.def.value = val;
7995 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7996}
7997
a0c8462f
AM
7998static bfd_boolean
7999resolve_symbol (const char *name,
8000 bfd *input_bfd,
8b127cbc 8001 struct elf_final_link_info *flinfo,
a0c8462f
AM
8002 bfd_vma *result,
8003 Elf_Internal_Sym *isymbuf,
8004 size_t locsymcount)
d9352518 8005{
a0c8462f
AM
8006 Elf_Internal_Sym *sym;
8007 struct bfd_link_hash_entry *global_entry;
8008 const char *candidate = NULL;
8009 Elf_Internal_Shdr *symtab_hdr;
8010 size_t i;
8011
d9352518
DB
8012 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8013
8014 for (i = 0; i < locsymcount; ++ i)
8015 {
8977835c 8016 sym = isymbuf + i;
d9352518
DB
8017
8018 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8019 continue;
8020
8021 candidate = bfd_elf_string_from_elf_section (input_bfd,
8022 symtab_hdr->sh_link,
8023 sym->st_name);
8024#ifdef DEBUG
0f02bbd9
AM
8025 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8026 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8027#endif
8028 if (candidate && strcmp (candidate, name) == 0)
8029 {
8b127cbc 8030 asection *sec = flinfo->sections [i];
d9352518 8031
0f02bbd9
AM
8032 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8033 *result += sec->output_offset + sec->output_section->vma;
d9352518 8034#ifdef DEBUG
0f02bbd9
AM
8035 printf ("Found symbol with value %8.8lx\n",
8036 (unsigned long) *result);
d9352518
DB
8037#endif
8038 return TRUE;
8039 }
8040 }
8041
8042 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8043 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8044 FALSE, FALSE, TRUE);
d9352518
DB
8045 if (!global_entry)
8046 return FALSE;
a0c8462f 8047
d9352518
DB
8048 if (global_entry->type == bfd_link_hash_defined
8049 || global_entry->type == bfd_link_hash_defweak)
8050 {
a0c8462f
AM
8051 *result = (global_entry->u.def.value
8052 + global_entry->u.def.section->output_section->vma
8053 + global_entry->u.def.section->output_offset);
d9352518 8054#ifdef DEBUG
0f02bbd9
AM
8055 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8056 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8057#endif
8058 return TRUE;
a0c8462f 8059 }
d9352518 8060
d9352518
DB
8061 return FALSE;
8062}
8063
37b01f6a
DG
8064/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8065 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8066 names like "foo.end" which is the end address of section "foo". */
8067
d9352518 8068static bfd_boolean
a0c8462f
AM
8069resolve_section (const char *name,
8070 asection *sections,
37b01f6a
DG
8071 bfd_vma *result,
8072 bfd * abfd)
d9352518 8073{
a0c8462f
AM
8074 asection *curr;
8075 unsigned int len;
d9352518 8076
a0c8462f 8077 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8078 if (strcmp (curr->name, name) == 0)
8079 {
8080 *result = curr->vma;
8081 return TRUE;
8082 }
8083
8084 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8085 /* FIXME: This could be coded more efficiently... */
a0c8462f 8086 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8087 {
8088 len = strlen (curr->name);
a0c8462f 8089 if (len > strlen (name))
d9352518
DB
8090 continue;
8091
8092 if (strncmp (curr->name, name, len) == 0)
8093 {
8094 if (strncmp (".end", name + len, 4) == 0)
8095 {
37b01f6a 8096 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8097 return TRUE;
8098 }
8099
8100 /* Insert more pseudo-section names here, if you like. */
8101 }
8102 }
a0c8462f 8103
d9352518
DB
8104 return FALSE;
8105}
8106
8107static void
a0c8462f 8108undefined_reference (const char *reftype, const char *name)
d9352518 8109{
695344c0 8110 /* xgettext:c-format */
a0c8462f
AM
8111 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8112 reftype, name);
d9352518
DB
8113}
8114
8115static bfd_boolean
a0c8462f
AM
8116eval_symbol (bfd_vma *result,
8117 const char **symp,
8118 bfd *input_bfd,
8b127cbc 8119 struct elf_final_link_info *flinfo,
a0c8462f
AM
8120 bfd_vma dot,
8121 Elf_Internal_Sym *isymbuf,
8122 size_t locsymcount,
8123 int signed_p)
d9352518 8124{
4b93929b
NC
8125 size_t len;
8126 size_t symlen;
a0c8462f
AM
8127 bfd_vma a;
8128 bfd_vma b;
4b93929b 8129 char symbuf[4096];
0f02bbd9 8130 const char *sym = *symp;
a0c8462f
AM
8131 const char *symend;
8132 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8133
8134 len = strlen (sym);
8135 symend = sym + len;
8136
4b93929b 8137 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8138 {
8139 bfd_set_error (bfd_error_invalid_operation);
8140 return FALSE;
8141 }
a0c8462f 8142
d9352518
DB
8143 switch (* sym)
8144 {
8145 case '.':
0f02bbd9
AM
8146 *result = dot;
8147 *symp = sym + 1;
d9352518
DB
8148 return TRUE;
8149
8150 case '#':
0f02bbd9
AM
8151 ++sym;
8152 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8153 return TRUE;
8154
8155 case 'S':
8156 symbol_is_section = TRUE;
1a0670f3 8157 /* Fall through. */
a0c8462f 8158 case 's':
0f02bbd9
AM
8159 ++sym;
8160 symlen = strtol (sym, (char **) symp, 10);
8161 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8162
4b93929b 8163 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8164 {
8165 bfd_set_error (bfd_error_invalid_operation);
8166 return FALSE;
8167 }
8168
8169 memcpy (symbuf, sym, symlen);
a0c8462f 8170 symbuf[symlen] = '\0';
0f02bbd9 8171 *symp = sym + symlen;
a0c8462f
AM
8172
8173 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8174 the symbol as a section, or vice-versa. so we're pretty liberal in our
8175 interpretation here; section means "try section first", not "must be a
8176 section", and likewise with symbol. */
8177
a0c8462f 8178 if (symbol_is_section)
d9352518 8179 {
37b01f6a 8180 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8181 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8182 isymbuf, locsymcount))
d9352518
DB
8183 {
8184 undefined_reference ("section", symbuf);
8185 return FALSE;
8186 }
a0c8462f
AM
8187 }
8188 else
d9352518 8189 {
8b127cbc 8190 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8191 isymbuf, locsymcount)
8b127cbc 8192 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8193 result, input_bfd))
d9352518
DB
8194 {
8195 undefined_reference ("symbol", symbuf);
8196 return FALSE;
8197 }
8198 }
8199
8200 return TRUE;
a0c8462f 8201
d9352518
DB
8202 /* All that remains are operators. */
8203
8204#define UNARY_OP(op) \
8205 if (strncmp (sym, #op, strlen (#op)) == 0) \
8206 { \
8207 sym += strlen (#op); \
a0c8462f
AM
8208 if (*sym == ':') \
8209 ++sym; \
0f02bbd9 8210 *symp = sym; \
8b127cbc 8211 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8212 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8213 return FALSE; \
8214 if (signed_p) \
0f02bbd9 8215 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8216 else \
8217 *result = op a; \
d9352518
DB
8218 return TRUE; \
8219 }
8220
8221#define BINARY_OP(op) \
8222 if (strncmp (sym, #op, strlen (#op)) == 0) \
8223 { \
8224 sym += strlen (#op); \
a0c8462f
AM
8225 if (*sym == ':') \
8226 ++sym; \
0f02bbd9 8227 *symp = sym; \
8b127cbc 8228 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8229 isymbuf, locsymcount, signed_p)) \
a0c8462f 8230 return FALSE; \
0f02bbd9 8231 ++*symp; \
8b127cbc 8232 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8233 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8234 return FALSE; \
8235 if (signed_p) \
0f02bbd9 8236 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8237 else \
8238 *result = a op b; \
d9352518
DB
8239 return TRUE; \
8240 }
8241
8242 default:
8243 UNARY_OP (0-);
8244 BINARY_OP (<<);
8245 BINARY_OP (>>);
8246 BINARY_OP (==);
8247 BINARY_OP (!=);
8248 BINARY_OP (<=);
8249 BINARY_OP (>=);
8250 BINARY_OP (&&);
8251 BINARY_OP (||);
8252 UNARY_OP (~);
8253 UNARY_OP (!);
8254 BINARY_OP (*);
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 BINARY_OP (>);
8264#undef UNARY_OP
8265#undef BINARY_OP
8266 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8267 bfd_set_error (bfd_error_invalid_operation);
8268 return FALSE;
8269 }
8270}
8271
d9352518 8272static void
a0c8462f
AM
8273put_value (bfd_vma size,
8274 unsigned long chunksz,
8275 bfd *input_bfd,
8276 bfd_vma x,
8277 bfd_byte *location)
d9352518
DB
8278{
8279 location += (size - chunksz);
8280
41cd1ad1 8281 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8282 {
8283 switch (chunksz)
8284 {
d9352518
DB
8285 case 1:
8286 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8287 x >>= 8;
d9352518
DB
8288 break;
8289 case 2:
8290 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8291 x >>= 16;
d9352518
DB
8292 break;
8293 case 4:
8294 bfd_put_32 (input_bfd, x, location);
65164438
NC
8295 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8296 x >>= 16;
8297 x >>= 16;
d9352518 8298 break;
d9352518 8299#ifdef BFD64
41cd1ad1 8300 case 8:
d9352518 8301 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8302 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8303 x >>= 32;
8304 x >>= 32;
8305 break;
d9352518 8306#endif
41cd1ad1
NC
8307 default:
8308 abort ();
d9352518
DB
8309 break;
8310 }
8311 }
8312}
8313
a0c8462f
AM
8314static bfd_vma
8315get_value (bfd_vma size,
8316 unsigned long chunksz,
8317 bfd *input_bfd,
8318 bfd_byte *location)
d9352518 8319{
9b239e0e 8320 int shift;
d9352518
DB
8321 bfd_vma x = 0;
8322
9b239e0e
NC
8323 /* Sanity checks. */
8324 BFD_ASSERT (chunksz <= sizeof (x)
8325 && size >= chunksz
8326 && chunksz != 0
8327 && (size % chunksz) == 0
8328 && input_bfd != NULL
8329 && location != NULL);
8330
8331 if (chunksz == sizeof (x))
8332 {
8333 BFD_ASSERT (size == chunksz);
8334
8335 /* Make sure that we do not perform an undefined shift operation.
8336 We know that size == chunksz so there will only be one iteration
8337 of the loop below. */
8338 shift = 0;
8339 }
8340 else
8341 shift = 8 * chunksz;
8342
a0c8462f 8343 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8344 {
8345 switch (chunksz)
8346 {
d9352518 8347 case 1:
9b239e0e 8348 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8349 break;
8350 case 2:
9b239e0e 8351 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8352 break;
8353 case 4:
9b239e0e 8354 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8355 break;
d9352518 8356#ifdef BFD64
9b239e0e
NC
8357 case 8:
8358 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8359 break;
9b239e0e
NC
8360#endif
8361 default:
8362 abort ();
d9352518
DB
8363 }
8364 }
8365 return x;
8366}
8367
a0c8462f
AM
8368static void
8369decode_complex_addend (unsigned long *start, /* in bits */
8370 unsigned long *oplen, /* in bits */
8371 unsigned long *len, /* in bits */
8372 unsigned long *wordsz, /* in bytes */
8373 unsigned long *chunksz, /* in bytes */
8374 unsigned long *lsb0_p,
8375 unsigned long *signed_p,
8376 unsigned long *trunc_p,
8377 unsigned long encoded)
d9352518
DB
8378{
8379 * start = encoded & 0x3F;
8380 * len = (encoded >> 6) & 0x3F;
8381 * oplen = (encoded >> 12) & 0x3F;
8382 * wordsz = (encoded >> 18) & 0xF;
8383 * chunksz = (encoded >> 22) & 0xF;
8384 * lsb0_p = (encoded >> 27) & 1;
8385 * signed_p = (encoded >> 28) & 1;
8386 * trunc_p = (encoded >> 29) & 1;
8387}
8388
cdfeee4f 8389bfd_reloc_status_type
0f02bbd9 8390bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8391 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8392 bfd_byte *contents,
8393 Elf_Internal_Rela *rel,
8394 bfd_vma relocation)
d9352518 8395{
0f02bbd9
AM
8396 bfd_vma shift, x, mask;
8397 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8398 bfd_reloc_status_type r;
d9352518
DB
8399
8400 /* Perform this reloc, since it is complex.
8401 (this is not to say that it necessarily refers to a complex
8402 symbol; merely that it is a self-describing CGEN based reloc.
8403 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8404 word size, etc) encoded within it.). */
d9352518 8405
a0c8462f
AM
8406 decode_complex_addend (&start, &oplen, &len, &wordsz,
8407 &chunksz, &lsb0_p, &signed_p,
8408 &trunc_p, rel->r_addend);
d9352518
DB
8409
8410 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8411
8412 if (lsb0_p)
8413 shift = (start + 1) - len;
8414 else
8415 shift = (8 * wordsz) - (start + len);
8416
37b01f6a
DG
8417 x = get_value (wordsz, chunksz, input_bfd,
8418 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8419
8420#ifdef DEBUG
8421 printf ("Doing complex reloc: "
8422 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8423 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8424 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8425 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8426 oplen, (unsigned long) x, (unsigned long) mask,
8427 (unsigned long) relocation);
d9352518
DB
8428#endif
8429
cdfeee4f 8430 r = bfd_reloc_ok;
d9352518 8431 if (! trunc_p)
cdfeee4f
AM
8432 /* Now do an overflow check. */
8433 r = bfd_check_overflow ((signed_p
8434 ? complain_overflow_signed
8435 : complain_overflow_unsigned),
8436 len, 0, (8 * wordsz),
8437 relocation);
a0c8462f 8438
d9352518
DB
8439 /* Do the deed. */
8440 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8441
8442#ifdef DEBUG
8443 printf (" relocation: %8.8lx\n"
8444 " shifted mask: %8.8lx\n"
8445 " shifted/masked reloc: %8.8lx\n"
8446 " result: %8.8lx\n",
9ccb8af9
AM
8447 (unsigned long) relocation, (unsigned long) (mask << shift),
8448 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8449#endif
37b01f6a
DG
8450 put_value (wordsz, chunksz, input_bfd, x,
8451 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8452 return r;
d9352518
DB
8453}
8454
0e287786
AM
8455/* Functions to read r_offset from external (target order) reloc
8456 entry. Faster than bfd_getl32 et al, because we let the compiler
8457 know the value is aligned. */
53df40a4 8458
0e287786
AM
8459static bfd_vma
8460ext32l_r_offset (const void *p)
53df40a4
AM
8461{
8462 union aligned32
8463 {
8464 uint32_t v;
8465 unsigned char c[4];
8466 };
8467 const union aligned32 *a
0e287786 8468 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8469
8470 uint32_t aval = ( (uint32_t) a->c[0]
8471 | (uint32_t) a->c[1] << 8
8472 | (uint32_t) a->c[2] << 16
8473 | (uint32_t) a->c[3] << 24);
0e287786 8474 return aval;
53df40a4
AM
8475}
8476
0e287786
AM
8477static bfd_vma
8478ext32b_r_offset (const void *p)
53df40a4
AM
8479{
8480 union aligned32
8481 {
8482 uint32_t v;
8483 unsigned char c[4];
8484 };
8485 const union aligned32 *a
0e287786 8486 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8487
8488 uint32_t aval = ( (uint32_t) a->c[0] << 24
8489 | (uint32_t) a->c[1] << 16
8490 | (uint32_t) a->c[2] << 8
8491 | (uint32_t) a->c[3]);
0e287786 8492 return aval;
53df40a4
AM
8493}
8494
8495#ifdef BFD_HOST_64_BIT
0e287786
AM
8496static bfd_vma
8497ext64l_r_offset (const void *p)
53df40a4
AM
8498{
8499 union aligned64
8500 {
8501 uint64_t v;
8502 unsigned char c[8];
8503 };
8504 const union aligned64 *a
0e287786 8505 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8506
8507 uint64_t aval = ( (uint64_t) a->c[0]
8508 | (uint64_t) a->c[1] << 8
8509 | (uint64_t) a->c[2] << 16
8510 | (uint64_t) a->c[3] << 24
8511 | (uint64_t) a->c[4] << 32
8512 | (uint64_t) a->c[5] << 40
8513 | (uint64_t) a->c[6] << 48
8514 | (uint64_t) a->c[7] << 56);
0e287786 8515 return aval;
53df40a4
AM
8516}
8517
0e287786
AM
8518static bfd_vma
8519ext64b_r_offset (const void *p)
53df40a4
AM
8520{
8521 union aligned64
8522 {
8523 uint64_t v;
8524 unsigned char c[8];
8525 };
8526 const union aligned64 *a
0e287786 8527 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8528
8529 uint64_t aval = ( (uint64_t) a->c[0] << 56
8530 | (uint64_t) a->c[1] << 48
8531 | (uint64_t) a->c[2] << 40
8532 | (uint64_t) a->c[3] << 32
8533 | (uint64_t) a->c[4] << 24
8534 | (uint64_t) a->c[5] << 16
8535 | (uint64_t) a->c[6] << 8
8536 | (uint64_t) a->c[7]);
0e287786 8537 return aval;
53df40a4
AM
8538}
8539#endif
8540
c152c796
AM
8541/* When performing a relocatable link, the input relocations are
8542 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8543 referenced must be updated. Update all the relocations found in
8544 RELDATA. */
c152c796 8545
bca6d0e3 8546static bfd_boolean
c152c796 8547elf_link_adjust_relocs (bfd *abfd,
9eaff861 8548 asection *sec,
28dbcedc 8549 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8550 bfd_boolean sort,
8551 struct bfd_link_info *info)
c152c796
AM
8552{
8553 unsigned int i;
8554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8555 bfd_byte *erela;
8556 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8557 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8558 bfd_vma r_type_mask;
8559 int r_sym_shift;
d4730f92
BS
8560 unsigned int count = reldata->count;
8561 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8562
d4730f92 8563 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8564 {
8565 swap_in = bed->s->swap_reloc_in;
8566 swap_out = bed->s->swap_reloc_out;
8567 }
d4730f92 8568 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8569 {
8570 swap_in = bed->s->swap_reloca_in;
8571 swap_out = bed->s->swap_reloca_out;
8572 }
8573 else
8574 abort ();
8575
8576 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8577 abort ();
8578
8579 if (bed->s->arch_size == 32)
8580 {
8581 r_type_mask = 0xff;
8582 r_sym_shift = 8;
8583 }
8584 else
8585 {
8586 r_type_mask = 0xffffffff;
8587 r_sym_shift = 32;
8588 }
8589
d4730f92
BS
8590 erela = reldata->hdr->contents;
8591 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8592 {
8593 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8594 unsigned int j;
8595
8596 if (*rel_hash == NULL)
8597 continue;
8598
10bbbc1d
NC
8599 if ((*rel_hash)->indx == -2
8600 && info->gc_sections
8601 && ! info->gc_keep_exported)
8602 {
8603 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8604 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8605 abfd, sec,
8606 (*rel_hash)->root.root.string);
8607 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8608 abfd, sec,
8609 (*rel_hash)->root.root.string);
8610 bfd_set_error (bfd_error_invalid_operation);
8611 return FALSE;
8612 }
c152c796
AM
8613 BFD_ASSERT ((*rel_hash)->indx >= 0);
8614
8615 (*swap_in) (abfd, erela, irela);
8616 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8617 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8618 | (irela[j].r_info & r_type_mask));
8619 (*swap_out) (abfd, irela, erela);
8620 }
53df40a4 8621
9eaff861
AO
8622 if (bed->elf_backend_update_relocs)
8623 (*bed->elf_backend_update_relocs) (sec, reldata);
8624
0e287786 8625 if (sort && count != 0)
53df40a4 8626 {
0e287786
AM
8627 bfd_vma (*ext_r_off) (const void *);
8628 bfd_vma r_off;
8629 size_t elt_size;
8630 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8631 bfd_byte *buf = NULL;
28dbcedc
AM
8632
8633 if (bed->s->arch_size == 32)
8634 {
8635 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8636 ext_r_off = ext32l_r_offset;
28dbcedc 8637 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8638 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8639 else
8640 abort ();
8641 }
53df40a4 8642 else
28dbcedc 8643 {
53df40a4 8644#ifdef BFD_HOST_64_BIT
28dbcedc 8645 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8646 ext_r_off = ext64l_r_offset;
28dbcedc 8647 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8648 ext_r_off = ext64b_r_offset;
28dbcedc 8649 else
53df40a4 8650#endif
28dbcedc
AM
8651 abort ();
8652 }
0e287786 8653
bca6d0e3
AM
8654 /* Must use a stable sort here. A modified insertion sort,
8655 since the relocs are mostly sorted already. */
0e287786
AM
8656 elt_size = reldata->hdr->sh_entsize;
8657 base = reldata->hdr->contents;
8658 end = base + count * elt_size;
bca6d0e3 8659 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8660 abort ();
8661
8662 /* Ensure the first element is lowest. This acts as a sentinel,
8663 speeding the main loop below. */
8664 r_off = (*ext_r_off) (base);
8665 for (p = loc = base; (p += elt_size) < end; )
8666 {
8667 bfd_vma r_off2 = (*ext_r_off) (p);
8668 if (r_off > r_off2)
8669 {
8670 r_off = r_off2;
8671 loc = p;
8672 }
8673 }
8674 if (loc != base)
8675 {
8676 /* Don't just swap *base and *loc as that changes the order
8677 of the original base[0] and base[1] if they happen to
8678 have the same r_offset. */
bca6d0e3
AM
8679 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8680 memcpy (onebuf, loc, elt_size);
0e287786 8681 memmove (base + elt_size, base, loc - base);
bca6d0e3 8682 memcpy (base, onebuf, elt_size);
0e287786
AM
8683 }
8684
b29b8669 8685 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8686 {
8687 /* base to p is sorted, *p is next to insert. */
8688 r_off = (*ext_r_off) (p);
8689 /* Search the sorted region for location to insert. */
8690 loc = p - elt_size;
8691 while (r_off < (*ext_r_off) (loc))
8692 loc -= elt_size;
8693 loc += elt_size;
8694 if (loc != p)
8695 {
bca6d0e3
AM
8696 /* Chances are there is a run of relocs to insert here,
8697 from one of more input files. Files are not always
8698 linked in order due to the way elf_link_input_bfd is
8699 called. See pr17666. */
8700 size_t sortlen = p - loc;
8701 bfd_vma r_off2 = (*ext_r_off) (loc);
8702 size_t runlen = elt_size;
8703 size_t buf_size = 96 * 1024;
8704 while (p + runlen < end
8705 && (sortlen <= buf_size
8706 || runlen + elt_size <= buf_size)
8707 && r_off2 > (*ext_r_off) (p + runlen))
8708 runlen += elt_size;
8709 if (buf == NULL)
8710 {
8711 buf = bfd_malloc (buf_size);
8712 if (buf == NULL)
8713 return FALSE;
8714 }
8715 if (runlen < sortlen)
8716 {
8717 memcpy (buf, p, runlen);
8718 memmove (loc + runlen, loc, sortlen);
8719 memcpy (loc, buf, runlen);
8720 }
8721 else
8722 {
8723 memcpy (buf, loc, sortlen);
8724 memmove (loc, p, runlen);
8725 memcpy (loc + runlen, buf, sortlen);
8726 }
b29b8669 8727 p += runlen - elt_size;
0e287786
AM
8728 }
8729 }
8730 /* Hashes are no longer valid. */
28dbcedc
AM
8731 free (reldata->hashes);
8732 reldata->hashes = NULL;
bca6d0e3 8733 free (buf);
53df40a4 8734 }
bca6d0e3 8735 return TRUE;
c152c796
AM
8736}
8737
8738struct elf_link_sort_rela
8739{
8740 union {
8741 bfd_vma offset;
8742 bfd_vma sym_mask;
8743 } u;
8744 enum elf_reloc_type_class type;
8745 /* We use this as an array of size int_rels_per_ext_rel. */
8746 Elf_Internal_Rela rela[1];
8747};
8748
8749static int
8750elf_link_sort_cmp1 (const void *A, const void *B)
8751{
a50b1753
NC
8752 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8753 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8754 int relativea, relativeb;
8755
8756 relativea = a->type == reloc_class_relative;
8757 relativeb = b->type == reloc_class_relative;
8758
8759 if (relativea < relativeb)
8760 return 1;
8761 if (relativea > relativeb)
8762 return -1;
8763 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8764 return -1;
8765 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8766 return 1;
8767 if (a->rela->r_offset < b->rela->r_offset)
8768 return -1;
8769 if (a->rela->r_offset > b->rela->r_offset)
8770 return 1;
8771 return 0;
8772}
8773
8774static int
8775elf_link_sort_cmp2 (const void *A, const void *B)
8776{
a50b1753
NC
8777 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8778 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8779
7e612e98 8780 if (a->type < b->type)
c152c796 8781 return -1;
7e612e98 8782 if (a->type > b->type)
c152c796 8783 return 1;
7e612e98 8784 if (a->u.offset < b->u.offset)
c152c796 8785 return -1;
7e612e98 8786 if (a->u.offset > b->u.offset)
c152c796
AM
8787 return 1;
8788 if (a->rela->r_offset < b->rela->r_offset)
8789 return -1;
8790 if (a->rela->r_offset > b->rela->r_offset)
8791 return 1;
8792 return 0;
8793}
8794
8795static size_t
8796elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8797{
3410fea8 8798 asection *dynamic_relocs;
fc66a176
L
8799 asection *rela_dyn;
8800 asection *rel_dyn;
c152c796
AM
8801 bfd_size_type count, size;
8802 size_t i, ret, sort_elt, ext_size;
8803 bfd_byte *sort, *s_non_relative, *p;
8804 struct elf_link_sort_rela *sq;
8805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8806 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8807 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8808 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8809 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8810 struct bfd_link_order *lo;
8811 bfd_vma r_sym_mask;
3410fea8 8812 bfd_boolean use_rela;
c152c796 8813
3410fea8
NC
8814 /* Find a dynamic reloc section. */
8815 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8816 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8817 if (rela_dyn != NULL && rela_dyn->size > 0
8818 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8819 {
3410fea8
NC
8820 bfd_boolean use_rela_initialised = FALSE;
8821
8822 /* This is just here to stop gcc from complaining.
c8e44c6d 8823 Its initialization checking code is not perfect. */
3410fea8
NC
8824 use_rela = TRUE;
8825
8826 /* Both sections are present. Examine the sizes
8827 of the indirect sections to help us choose. */
8828 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8829 if (lo->type == bfd_indirect_link_order)
8830 {
8831 asection *o = lo->u.indirect.section;
8832
8833 if ((o->size % bed->s->sizeof_rela) == 0)
8834 {
8835 if ((o->size % bed->s->sizeof_rel) == 0)
8836 /* Section size is divisible by both rel and rela sizes.
8837 It is of no help to us. */
8838 ;
8839 else
8840 {
8841 /* Section size is only divisible by rela. */
535b785f 8842 if (use_rela_initialised && !use_rela)
3410fea8 8843 {
c8e44c6d
AM
8844 _bfd_error_handler (_("%B: Unable to sort relocs - "
8845 "they are in more than one size"),
8846 abfd);
3410fea8
NC
8847 bfd_set_error (bfd_error_invalid_operation);
8848 return 0;
8849 }
8850 else
8851 {
8852 use_rela = TRUE;
8853 use_rela_initialised = TRUE;
8854 }
8855 }
8856 }
8857 else if ((o->size % bed->s->sizeof_rel) == 0)
8858 {
8859 /* Section size is only divisible by rel. */
535b785f 8860 if (use_rela_initialised && use_rela)
3410fea8 8861 {
c8e44c6d
AM
8862 _bfd_error_handler (_("%B: Unable to sort relocs - "
8863 "they are in more than one size"),
8864 abfd);
3410fea8
NC
8865 bfd_set_error (bfd_error_invalid_operation);
8866 return 0;
8867 }
8868 else
8869 {
8870 use_rela = FALSE;
8871 use_rela_initialised = TRUE;
8872 }
8873 }
8874 else
8875 {
c8e44c6d
AM
8876 /* The section size is not divisible by either -
8877 something is wrong. */
8878 _bfd_error_handler (_("%B: Unable to sort relocs - "
8879 "they are of an unknown size"), abfd);
3410fea8
NC
8880 bfd_set_error (bfd_error_invalid_operation);
8881 return 0;
8882 }
8883 }
8884
8885 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8886 if (lo->type == bfd_indirect_link_order)
8887 {
8888 asection *o = lo->u.indirect.section;
8889
8890 if ((o->size % bed->s->sizeof_rela) == 0)
8891 {
8892 if ((o->size % bed->s->sizeof_rel) == 0)
8893 /* Section size is divisible by both rel and rela sizes.
8894 It is of no help to us. */
8895 ;
8896 else
8897 {
8898 /* Section size is only divisible by rela. */
535b785f 8899 if (use_rela_initialised && !use_rela)
3410fea8 8900 {
c8e44c6d
AM
8901 _bfd_error_handler (_("%B: Unable to sort relocs - "
8902 "they are in more than one size"),
8903 abfd);
3410fea8
NC
8904 bfd_set_error (bfd_error_invalid_operation);
8905 return 0;
8906 }
8907 else
8908 {
8909 use_rela = TRUE;
8910 use_rela_initialised = TRUE;
8911 }
8912 }
8913 }
8914 else if ((o->size % bed->s->sizeof_rel) == 0)
8915 {
8916 /* Section size is only divisible by rel. */
535b785f 8917 if (use_rela_initialised && use_rela)
3410fea8 8918 {
c8e44c6d
AM
8919 _bfd_error_handler (_("%B: Unable to sort relocs - "
8920 "they are in more than one size"),
8921 abfd);
3410fea8
NC
8922 bfd_set_error (bfd_error_invalid_operation);
8923 return 0;
8924 }
8925 else
8926 {
8927 use_rela = FALSE;
8928 use_rela_initialised = TRUE;
8929 }
8930 }
8931 else
8932 {
c8e44c6d
AM
8933 /* The section size is not divisible by either -
8934 something is wrong. */
8935 _bfd_error_handler (_("%B: Unable to sort relocs - "
8936 "they are of an unknown size"), abfd);
3410fea8
NC
8937 bfd_set_error (bfd_error_invalid_operation);
8938 return 0;
8939 }
8940 }
8941
8942 if (! use_rela_initialised)
8943 /* Make a guess. */
8944 use_rela = TRUE;
c152c796 8945 }
fc66a176
L
8946 else if (rela_dyn != NULL && rela_dyn->size > 0)
8947 use_rela = TRUE;
8948 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8949 use_rela = FALSE;
c152c796 8950 else
fc66a176 8951 return 0;
3410fea8
NC
8952
8953 if (use_rela)
c152c796 8954 {
3410fea8 8955 dynamic_relocs = rela_dyn;
c152c796
AM
8956 ext_size = bed->s->sizeof_rela;
8957 swap_in = bed->s->swap_reloca_in;
8958 swap_out = bed->s->swap_reloca_out;
8959 }
3410fea8
NC
8960 else
8961 {
8962 dynamic_relocs = rel_dyn;
8963 ext_size = bed->s->sizeof_rel;
8964 swap_in = bed->s->swap_reloc_in;
8965 swap_out = bed->s->swap_reloc_out;
8966 }
c152c796
AM
8967
8968 size = 0;
3410fea8 8969 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8970 if (lo->type == bfd_indirect_link_order)
3410fea8 8971 size += lo->u.indirect.section->size;
c152c796 8972
3410fea8 8973 if (size != dynamic_relocs->size)
c152c796
AM
8974 return 0;
8975
8976 sort_elt = (sizeof (struct elf_link_sort_rela)
8977 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8978
8979 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8980 if (count == 0)
8981 return 0;
a50b1753 8982 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8983
c152c796
AM
8984 if (sort == NULL)
8985 {
8986 (*info->callbacks->warning)
8987 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8988 return 0;
8989 }
8990
8991 if (bed->s->arch_size == 32)
8992 r_sym_mask = ~(bfd_vma) 0xff;
8993 else
8994 r_sym_mask = ~(bfd_vma) 0xffffffff;
8995
3410fea8 8996 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8997 if (lo->type == bfd_indirect_link_order)
8998 {
8999 bfd_byte *erel, *erelend;
9000 asection *o = lo->u.indirect.section;
9001
1da212d6
AM
9002 if (o->contents == NULL && o->size != 0)
9003 {
9004 /* This is a reloc section that is being handled as a normal
9005 section. See bfd_section_from_shdr. We can't combine
9006 relocs in this case. */
9007 free (sort);
9008 return 0;
9009 }
c152c796 9010 erel = o->contents;
eea6121a 9011 erelend = o->contents + o->size;
c8e44c6d 9012 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9013
c152c796
AM
9014 while (erel < erelend)
9015 {
9016 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9017
c152c796 9018 (*swap_in) (abfd, erel, s->rela);
7e612e98 9019 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9020 s->u.sym_mask = r_sym_mask;
9021 p += sort_elt;
9022 erel += ext_size;
9023 }
9024 }
9025
9026 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9027
9028 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9029 {
9030 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9031 if (s->type != reloc_class_relative)
9032 break;
9033 }
9034 ret = i;
9035 s_non_relative = p;
9036
9037 sq = (struct elf_link_sort_rela *) s_non_relative;
9038 for (; i < count; i++, p += sort_elt)
9039 {
9040 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9041 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9042 sq = sp;
9043 sp->u.offset = sq->rela->r_offset;
9044 }
9045
9046 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9047
c8e44c6d
AM
9048 struct elf_link_hash_table *htab = elf_hash_table (info);
9049 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9050 {
9051 /* We have plt relocs in .rela.dyn. */
9052 sq = (struct elf_link_sort_rela *) sort;
9053 for (i = 0; i < count; i++)
9054 if (sq[count - i - 1].type != reloc_class_plt)
9055 break;
9056 if (i != 0 && htab->srelplt->size == i * ext_size)
9057 {
9058 struct bfd_link_order **plo;
9059 /* Put srelplt link_order last. This is so the output_offset
9060 set in the next loop is correct for DT_JMPREL. */
9061 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9062 if ((*plo)->type == bfd_indirect_link_order
9063 && (*plo)->u.indirect.section == htab->srelplt)
9064 {
9065 lo = *plo;
9066 *plo = lo->next;
9067 }
9068 else
9069 plo = &(*plo)->next;
9070 *plo = lo;
9071 lo->next = NULL;
9072 dynamic_relocs->map_tail.link_order = lo;
9073 }
9074 }
9075
9076 p = sort;
3410fea8 9077 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9078 if (lo->type == bfd_indirect_link_order)
9079 {
9080 bfd_byte *erel, *erelend;
9081 asection *o = lo->u.indirect.section;
9082
9083 erel = o->contents;
eea6121a 9084 erelend = o->contents + o->size;
c8e44c6d 9085 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9086 while (erel < erelend)
9087 {
9088 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9089 (*swap_out) (abfd, s->rela, erel);
9090 p += sort_elt;
9091 erel += ext_size;
9092 }
9093 }
9094
9095 free (sort);
3410fea8 9096 *psec = dynamic_relocs;
c152c796
AM
9097 return ret;
9098}
9099
ef10c3ac 9100/* Add a symbol to the output symbol string table. */
c152c796 9101
6e0b88f1 9102static int
ef10c3ac
L
9103elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9104 const char *name,
9105 Elf_Internal_Sym *elfsym,
9106 asection *input_sec,
9107 struct elf_link_hash_entry *h)
c152c796 9108{
6e0b88f1 9109 int (*output_symbol_hook)
c152c796
AM
9110 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9111 struct elf_link_hash_entry *);
ef10c3ac 9112 struct elf_link_hash_table *hash_table;
c152c796 9113 const struct elf_backend_data *bed;
ef10c3ac 9114 bfd_size_type strtabsize;
c152c796 9115
8539e4e8
AM
9116 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9117
8b127cbc 9118 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9119 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9120 if (output_symbol_hook != NULL)
9121 {
8b127cbc 9122 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9123 if (ret != 1)
9124 return ret;
c152c796
AM
9125 }
9126
ef10c3ac
L
9127 if (name == NULL
9128 || *name == '\0'
9129 || (input_sec->flags & SEC_EXCLUDE))
9130 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9131 else
9132 {
ef10c3ac
L
9133 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9134 to get the final offset for st_name. */
9135 elfsym->st_name
9136 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9137 name, FALSE);
c152c796 9138 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9139 return 0;
c152c796
AM
9140 }
9141
ef10c3ac
L
9142 hash_table = elf_hash_table (flinfo->info);
9143 strtabsize = hash_table->strtabsize;
9144 if (strtabsize <= hash_table->strtabcount)
c152c796 9145 {
ef10c3ac
L
9146 strtabsize += strtabsize;
9147 hash_table->strtabsize = strtabsize;
9148 strtabsize *= sizeof (*hash_table->strtab);
9149 hash_table->strtab
9150 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9151 strtabsize);
9152 if (hash_table->strtab == NULL)
6e0b88f1 9153 return 0;
c152c796 9154 }
ef10c3ac
L
9155 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9156 hash_table->strtab[hash_table->strtabcount].dest_index
9157 = hash_table->strtabcount;
9158 hash_table->strtab[hash_table->strtabcount].destshndx_index
9159 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9160
9161 bfd_get_symcount (flinfo->output_bfd) += 1;
9162 hash_table->strtabcount += 1;
9163
9164 return 1;
9165}
9166
9167/* Swap symbols out to the symbol table and flush the output symbols to
9168 the file. */
9169
9170static bfd_boolean
9171elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9172{
9173 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9174 bfd_size_type amt;
9175 size_t i;
ef10c3ac
L
9176 const struct elf_backend_data *bed;
9177 bfd_byte *symbuf;
9178 Elf_Internal_Shdr *hdr;
9179 file_ptr pos;
9180 bfd_boolean ret;
9181
9182 if (!hash_table->strtabcount)
9183 return TRUE;
9184
9185 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9186
9187 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9188
ef10c3ac
L
9189 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9190 symbuf = (bfd_byte *) bfd_malloc (amt);
9191 if (symbuf == NULL)
9192 return FALSE;
1b786873 9193
ef10c3ac 9194 if (flinfo->symshndxbuf)
c152c796 9195 {
ef53be89
AM
9196 amt = sizeof (Elf_External_Sym_Shndx);
9197 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9198 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9199 if (flinfo->symshndxbuf == NULL)
c152c796 9200 {
ef10c3ac
L
9201 free (symbuf);
9202 return FALSE;
c152c796 9203 }
c152c796
AM
9204 }
9205
ef10c3ac
L
9206 for (i = 0; i < hash_table->strtabcount; i++)
9207 {
9208 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9209 if (elfsym->sym.st_name == (unsigned long) -1)
9210 elfsym->sym.st_name = 0;
9211 else
9212 elfsym->sym.st_name
9213 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9214 elfsym->sym.st_name);
9215 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9216 ((bfd_byte *) symbuf
9217 + (elfsym->dest_index
9218 * bed->s->sizeof_sym)),
9219 (flinfo->symshndxbuf
9220 + elfsym->destshndx_index));
9221 }
9222
9223 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9224 pos = hdr->sh_offset + hdr->sh_size;
9225 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9226 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9227 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9228 {
9229 hdr->sh_size += amt;
9230 ret = TRUE;
9231 }
9232 else
9233 ret = FALSE;
c152c796 9234
ef10c3ac
L
9235 free (symbuf);
9236
9237 free (hash_table->strtab);
9238 hash_table->strtab = NULL;
9239
9240 return ret;
c152c796
AM
9241}
9242
c0d5a53d
L
9243/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9244
9245static bfd_boolean
9246check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9247{
4fbb74a6
AM
9248 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9249 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9250 {
9251 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9252 beyond 64k. */
4eca0228 9253 _bfd_error_handler
695344c0 9254 /* xgettext:c-format */
c0d5a53d 9255 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9256 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9257 bfd_set_error (bfd_error_nonrepresentable_section);
9258 return FALSE;
9259 }
9260 return TRUE;
9261}
9262
c152c796
AM
9263/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9264 allowing an unsatisfied unversioned symbol in the DSO to match a
9265 versioned symbol that would normally require an explicit version.
9266 We also handle the case that a DSO references a hidden symbol
9267 which may be satisfied by a versioned symbol in another DSO. */
9268
9269static bfd_boolean
9270elf_link_check_versioned_symbol (struct bfd_link_info *info,
9271 const struct elf_backend_data *bed,
9272 struct elf_link_hash_entry *h)
9273{
9274 bfd *abfd;
9275 struct elf_link_loaded_list *loaded;
9276
9277 if (!is_elf_hash_table (info->hash))
9278 return FALSE;
9279
90c984fc
L
9280 /* Check indirect symbol. */
9281 while (h->root.type == bfd_link_hash_indirect)
9282 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9283
c152c796
AM
9284 switch (h->root.type)
9285 {
9286 default:
9287 abfd = NULL;
9288 break;
9289
9290 case bfd_link_hash_undefined:
9291 case bfd_link_hash_undefweak:
9292 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9293 if (abfd == NULL
9294 || (abfd->flags & DYNAMIC) == 0
e56f61be 9295 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9296 return FALSE;
9297 break;
9298
9299 case bfd_link_hash_defined:
9300 case bfd_link_hash_defweak:
9301 abfd = h->root.u.def.section->owner;
9302 break;
9303
9304 case bfd_link_hash_common:
9305 abfd = h->root.u.c.p->section->owner;
9306 break;
9307 }
9308 BFD_ASSERT (abfd != NULL);
9309
9310 for (loaded = elf_hash_table (info)->loaded;
9311 loaded != NULL;
9312 loaded = loaded->next)
9313 {
9314 bfd *input;
9315 Elf_Internal_Shdr *hdr;
ef53be89
AM
9316 size_t symcount;
9317 size_t extsymcount;
9318 size_t extsymoff;
c152c796
AM
9319 Elf_Internal_Shdr *versymhdr;
9320 Elf_Internal_Sym *isym;
9321 Elf_Internal_Sym *isymend;
9322 Elf_Internal_Sym *isymbuf;
9323 Elf_External_Versym *ever;
9324 Elf_External_Versym *extversym;
9325
9326 input = loaded->abfd;
9327
9328 /* We check each DSO for a possible hidden versioned definition. */
9329 if (input == abfd
9330 || (input->flags & DYNAMIC) == 0
9331 || elf_dynversym (input) == 0)
9332 continue;
9333
9334 hdr = &elf_tdata (input)->dynsymtab_hdr;
9335
9336 symcount = hdr->sh_size / bed->s->sizeof_sym;
9337 if (elf_bad_symtab (input))
9338 {
9339 extsymcount = symcount;
9340 extsymoff = 0;
9341 }
9342 else
9343 {
9344 extsymcount = symcount - hdr->sh_info;
9345 extsymoff = hdr->sh_info;
9346 }
9347
9348 if (extsymcount == 0)
9349 continue;
9350
9351 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9352 NULL, NULL, NULL);
9353 if (isymbuf == NULL)
9354 return FALSE;
9355
9356 /* Read in any version definitions. */
9357 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9358 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9359 if (extversym == NULL)
9360 goto error_ret;
9361
9362 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9363 || (bfd_bread (extversym, versymhdr->sh_size, input)
9364 != versymhdr->sh_size))
9365 {
9366 free (extversym);
9367 error_ret:
9368 free (isymbuf);
9369 return FALSE;
9370 }
9371
9372 ever = extversym + extsymoff;
9373 isymend = isymbuf + extsymcount;
9374 for (isym = isymbuf; isym < isymend; isym++, ever++)
9375 {
9376 const char *name;
9377 Elf_Internal_Versym iver;
9378 unsigned short version_index;
9379
9380 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9381 || isym->st_shndx == SHN_UNDEF)
9382 continue;
9383
9384 name = bfd_elf_string_from_elf_section (input,
9385 hdr->sh_link,
9386 isym->st_name);
9387 if (strcmp (name, h->root.root.string) != 0)
9388 continue;
9389
9390 _bfd_elf_swap_versym_in (input, ever, &iver);
9391
d023c380
L
9392 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9393 && !(h->def_regular
9394 && h->forced_local))
c152c796
AM
9395 {
9396 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9397 have provided a definition for the undefined sym unless
9398 it is defined in a non-shared object and forced local.
9399 */
c152c796
AM
9400 abort ();
9401 }
9402
9403 version_index = iver.vs_vers & VERSYM_VERSION;
9404 if (version_index == 1 || version_index == 2)
9405 {
9406 /* This is the base or first version. We can use it. */
9407 free (extversym);
9408 free (isymbuf);
9409 return TRUE;
9410 }
9411 }
9412
9413 free (extversym);
9414 free (isymbuf);
9415 }
9416
9417 return FALSE;
9418}
9419
b8871f35
L
9420/* Convert ELF common symbol TYPE. */
9421
9422static int
9423elf_link_convert_common_type (struct bfd_link_info *info, int type)
9424{
9425 /* Commom symbol can only appear in relocatable link. */
9426 if (!bfd_link_relocatable (info))
9427 abort ();
9428 switch (info->elf_stt_common)
9429 {
9430 case unchanged:
9431 break;
9432 case elf_stt_common:
9433 type = STT_COMMON;
9434 break;
9435 case no_elf_stt_common:
9436 type = STT_OBJECT;
9437 break;
9438 }
9439 return type;
9440}
9441
c152c796
AM
9442/* Add an external symbol to the symbol table. This is called from
9443 the hash table traversal routine. When generating a shared object,
9444 we go through the symbol table twice. The first time we output
9445 anything that might have been forced to local scope in a version
9446 script. The second time we output the symbols that are still
9447 global symbols. */
9448
9449static bfd_boolean
7686d77d 9450elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9451{
7686d77d 9452 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9453 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9454 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9455 bfd_boolean strip;
9456 Elf_Internal_Sym sym;
9457 asection *input_sec;
9458 const struct elf_backend_data *bed;
6e0b88f1
AM
9459 long indx;
9460 int ret;
b8871f35 9461 unsigned int type;
c152c796
AM
9462
9463 if (h->root.type == bfd_link_hash_warning)
9464 {
9465 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9466 if (h->root.type == bfd_link_hash_new)
9467 return TRUE;
9468 }
9469
9470 /* Decide whether to output this symbol in this pass. */
9471 if (eoinfo->localsyms)
9472 {
4deb8f71 9473 if (!h->forced_local)
c152c796
AM
9474 return TRUE;
9475 }
9476 else
9477 {
4deb8f71 9478 if (h->forced_local)
c152c796
AM
9479 return TRUE;
9480 }
9481
8b127cbc 9482 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9483
12ac1cf5 9484 if (h->root.type == bfd_link_hash_undefined)
c152c796 9485 {
12ac1cf5
NC
9486 /* If we have an undefined symbol reference here then it must have
9487 come from a shared library that is being linked in. (Undefined
98da7939
L
9488 references in regular files have already been handled unless
9489 they are in unreferenced sections which are removed by garbage
9490 collection). */
12ac1cf5
NC
9491 bfd_boolean ignore_undef = FALSE;
9492
9493 /* Some symbols may be special in that the fact that they're
9494 undefined can be safely ignored - let backend determine that. */
9495 if (bed->elf_backend_ignore_undef_symbol)
9496 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9497
9498 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9499 if (!ignore_undef
12ac1cf5 9500 && h->ref_dynamic
8b127cbc
AM
9501 && (!h->ref_regular || flinfo->info->gc_sections)
9502 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9503 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9504 (*flinfo->info->callbacks->undefined_symbol)
9505 (flinfo->info, h->root.root.string,
9506 h->ref_regular ? NULL : h->root.u.undef.abfd,
9507 NULL, 0,
9508 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9509
9510 /* Strip a global symbol defined in a discarded section. */
9511 if (h->indx == -3)
9512 return TRUE;
c152c796
AM
9513 }
9514
9515 /* We should also warn if a forced local symbol is referenced from
9516 shared libraries. */
0e1862bb 9517 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9518 && h->forced_local
9519 && h->ref_dynamic
371a5866 9520 && h->def_regular
f5385ebf 9521 && !h->dynamic_def
ee659f1f 9522 && h->ref_dynamic_nonweak
8b127cbc 9523 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9524 {
17d078c5
AM
9525 bfd *def_bfd;
9526 const char *msg;
90c984fc
L
9527 struct elf_link_hash_entry *hi = h;
9528
9529 /* Check indirect symbol. */
9530 while (hi->root.type == bfd_link_hash_indirect)
9531 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9532
9533 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9534 /* xgettext:c-format */
17d078c5
AM
9535 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9536 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9537 /* xgettext:c-format */
17d078c5
AM
9538 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9539 else
695344c0 9540 /* xgettext:c-format */
17d078c5 9541 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9542 def_bfd = flinfo->output_bfd;
90c984fc
L
9543 if (hi->root.u.def.section != bfd_abs_section_ptr)
9544 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9545 _bfd_error_handler (msg, flinfo->output_bfd,
9546 h->root.root.string, def_bfd);
17d078c5 9547 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9548 eoinfo->failed = TRUE;
9549 return FALSE;
9550 }
9551
9552 /* We don't want to output symbols that have never been mentioned by
9553 a regular file, or that we have been told to strip. However, if
9554 h->indx is set to -2, the symbol is used by a reloc and we must
9555 output it. */
d983c8c5 9556 strip = FALSE;
c152c796 9557 if (h->indx == -2)
d983c8c5 9558 ;
f5385ebf 9559 else if ((h->def_dynamic
77cfaee6
AM
9560 || h->ref_dynamic
9561 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9562 && !h->def_regular
9563 && !h->ref_regular)
c152c796 9564 strip = TRUE;
8b127cbc 9565 else if (flinfo->info->strip == strip_all)
c152c796 9566 strip = TRUE;
8b127cbc
AM
9567 else if (flinfo->info->strip == strip_some
9568 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9569 h->root.root.string, FALSE, FALSE) == NULL)
9570 strip = TRUE;
d56d55e7
AM
9571 else if ((h->root.type == bfd_link_hash_defined
9572 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9573 && ((flinfo->info->strip_discarded
dbaa2011 9574 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9575 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9576 && h->root.u.def.section->owner != NULL
d56d55e7 9577 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9578 strip = TRUE;
9e2278f5
AM
9579 else if ((h->root.type == bfd_link_hash_undefined
9580 || h->root.type == bfd_link_hash_undefweak)
9581 && h->root.u.undef.abfd != NULL
9582 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9583 strip = TRUE;
c152c796 9584
b8871f35
L
9585 type = h->type;
9586
c152c796 9587 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9588 nothing else to do. However, if it is a forced local symbol or
9589 an ifunc symbol we need to give the backend finish_dynamic_symbol
9590 function a chance to make it dynamic. */
c152c796
AM
9591 if (strip
9592 && h->dynindx == -1
b8871f35 9593 && type != STT_GNU_IFUNC
f5385ebf 9594 && !h->forced_local)
c152c796
AM
9595 return TRUE;
9596
9597 sym.st_value = 0;
9598 sym.st_size = h->size;
9599 sym.st_other = h->other;
c152c796
AM
9600 switch (h->root.type)
9601 {
9602 default:
9603 case bfd_link_hash_new:
9604 case bfd_link_hash_warning:
9605 abort ();
9606 return FALSE;
9607
9608 case bfd_link_hash_undefined:
9609 case bfd_link_hash_undefweak:
9610 input_sec = bfd_und_section_ptr;
9611 sym.st_shndx = SHN_UNDEF;
9612 break;
9613
9614 case bfd_link_hash_defined:
9615 case bfd_link_hash_defweak:
9616 {
9617 input_sec = h->root.u.def.section;
9618 if (input_sec->output_section != NULL)
9619 {
9620 sym.st_shndx =
8b127cbc 9621 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9622 input_sec->output_section);
9623 if (sym.st_shndx == SHN_BAD)
9624 {
4eca0228 9625 _bfd_error_handler
695344c0 9626 /* xgettext:c-format */
d003868e 9627 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9628 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9629 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9630 eoinfo->failed = TRUE;
9631 return FALSE;
9632 }
9633
9634 /* ELF symbols in relocatable files are section relative,
9635 but in nonrelocatable files they are virtual
9636 addresses. */
9637 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9638 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9639 {
9640 sym.st_value += input_sec->output_section->vma;
9641 if (h->type == STT_TLS)
9642 {
8b127cbc 9643 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9644 if (tls_sec != NULL)
9645 sym.st_value -= tls_sec->vma;
c152c796
AM
9646 }
9647 }
9648 }
9649 else
9650 {
9651 BFD_ASSERT (input_sec->owner == NULL
9652 || (input_sec->owner->flags & DYNAMIC) != 0);
9653 sym.st_shndx = SHN_UNDEF;
9654 input_sec = bfd_und_section_ptr;
9655 }
9656 }
9657 break;
9658
9659 case bfd_link_hash_common:
9660 input_sec = h->root.u.c.p->section;
a4d8e49b 9661 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9662 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9663 break;
9664
9665 case bfd_link_hash_indirect:
9666 /* These symbols are created by symbol versioning. They point
9667 to the decorated version of the name. For example, if the
9668 symbol foo@@GNU_1.2 is the default, which should be used when
9669 foo is used with no version, then we add an indirect symbol
9670 foo which points to foo@@GNU_1.2. We ignore these symbols,
9671 since the indirected symbol is already in the hash table. */
9672 return TRUE;
9673 }
9674
b8871f35
L
9675 if (type == STT_COMMON || type == STT_OBJECT)
9676 switch (h->root.type)
9677 {
9678 case bfd_link_hash_common:
9679 type = elf_link_convert_common_type (flinfo->info, type);
9680 break;
9681 case bfd_link_hash_defined:
9682 case bfd_link_hash_defweak:
9683 if (bed->common_definition (&sym))
9684 type = elf_link_convert_common_type (flinfo->info, type);
9685 else
9686 type = STT_OBJECT;
9687 break;
9688 case bfd_link_hash_undefined:
9689 case bfd_link_hash_undefweak:
9690 break;
9691 default:
9692 abort ();
9693 }
9694
4deb8f71 9695 if (h->forced_local)
b8871f35
L
9696 {
9697 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9698 /* Turn off visibility on local symbol. */
9699 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9700 }
9701 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9702 else if (h->unique_global && h->def_regular)
9703 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9704 else if (h->root.type == bfd_link_hash_undefweak
9705 || h->root.type == bfd_link_hash_defweak)
9706 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9707 else
9708 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9709 sym.st_target_internal = h->target_internal;
9710
c152c796
AM
9711 /* Give the processor backend a chance to tweak the symbol value,
9712 and also to finish up anything that needs to be done for this
9713 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9714 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9715 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9716 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9717 && h->def_regular
0e1862bb 9718 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9719 || ((h->dynindx != -1
9720 || h->forced_local)
0e1862bb 9721 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9722 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9723 || h->root.type != bfd_link_hash_undefweak))
9724 || !h->forced_local)
8b127cbc 9725 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9726 {
9727 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9728 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9729 {
9730 eoinfo->failed = TRUE;
9731 return FALSE;
9732 }
9733 }
9734
9735 /* If we are marking the symbol as undefined, and there are no
9736 non-weak references to this symbol from a regular object, then
9737 mark the symbol as weak undefined; if there are non-weak
9738 references, mark the symbol as strong. We can't do this earlier,
9739 because it might not be marked as undefined until the
9740 finish_dynamic_symbol routine gets through with it. */
9741 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9742 && h->ref_regular
c152c796
AM
9743 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9744 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9745 {
9746 int bindtype;
b8871f35 9747 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9748
9749 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9750 if (type == STT_GNU_IFUNC)
9751 type = STT_FUNC;
c152c796 9752
f5385ebf 9753 if (h->ref_regular_nonweak)
c152c796
AM
9754 bindtype = STB_GLOBAL;
9755 else
9756 bindtype = STB_WEAK;
2955ec4c 9757 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9758 }
9759
bda987c2
CD
9760 /* If this is a symbol defined in a dynamic library, don't use the
9761 symbol size from the dynamic library. Relinking an executable
9762 against a new library may introduce gratuitous changes in the
9763 executable's symbols if we keep the size. */
9764 if (sym.st_shndx == SHN_UNDEF
9765 && !h->def_regular
9766 && h->def_dynamic)
9767 sym.st_size = 0;
9768
c152c796
AM
9769 /* If a non-weak symbol with non-default visibility is not defined
9770 locally, it is a fatal error. */
0e1862bb 9771 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9772 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9773 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9774 && h->root.type == bfd_link_hash_undefined
f5385ebf 9775 && !h->def_regular)
c152c796 9776 {
17d078c5
AM
9777 const char *msg;
9778
9779 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9780 /* xgettext:c-format */
17d078c5
AM
9781 msg = _("%B: protected symbol `%s' isn't defined");
9782 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9783 /* xgettext:c-format */
17d078c5
AM
9784 msg = _("%B: internal symbol `%s' isn't defined");
9785 else
695344c0 9786 /* xgettext:c-format */
17d078c5 9787 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9788 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9789 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9790 eoinfo->failed = TRUE;
9791 return FALSE;
9792 }
9793
9794 /* If this symbol should be put in the .dynsym section, then put it
9795 there now. We already know the symbol index. We also fill in
9796 the entry in the .hash section. */
cae1fbbb 9797 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9798 && h->dynindx != -1
8b127cbc 9799 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9800 {
c152c796
AM
9801 bfd_byte *esym;
9802
90c984fc
L
9803 /* Since there is no version information in the dynamic string,
9804 if there is no version info in symbol version section, we will
1659f720 9805 have a run-time problem if not linking executable, referenced
4deb8f71 9806 by shared library, or not bound locally. */
1659f720 9807 if (h->verinfo.verdef == NULL
0e1862bb 9808 && (!bfd_link_executable (flinfo->info)
1659f720
L
9809 || h->ref_dynamic
9810 || !h->def_regular))
90c984fc
L
9811 {
9812 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9813
9814 if (p && p [1] != '\0')
9815 {
4eca0228 9816 _bfd_error_handler
695344c0 9817 /* xgettext:c-format */
90c984fc
L
9818 (_("%B: No symbol version section for versioned symbol `%s'"),
9819 flinfo->output_bfd, h->root.root.string);
9820 eoinfo->failed = TRUE;
9821 return FALSE;
9822 }
9823 }
9824
c152c796 9825 sym.st_name = h->dynstr_index;
cae1fbbb
L
9826 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9827 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9828 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9829 {
9830 eoinfo->failed = TRUE;
9831 return FALSE;
9832 }
8b127cbc 9833 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9834
8b127cbc 9835 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9836 {
9837 size_t hash_entry_size;
9838 bfd_byte *bucketpos;
9839 bfd_vma chain;
41198d0c
L
9840 size_t bucketcount;
9841 size_t bucket;
9842
8b127cbc 9843 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9844 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9845
9846 hash_entry_size
8b127cbc
AM
9847 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9848 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9849 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9850 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9851 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9852 bucketpos);
9853 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9854 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9855 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9856 }
c152c796 9857
8b127cbc 9858 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9859 {
9860 Elf_Internal_Versym iversym;
9861 Elf_External_Versym *eversym;
9862
f5385ebf 9863 if (!h->def_regular)
c152c796 9864 {
7b20f099
AM
9865 if (h->verinfo.verdef == NULL
9866 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9867 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9868 iversym.vs_vers = 0;
9869 else
9870 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9871 }
9872 else
9873 {
9874 if (h->verinfo.vertree == NULL)
9875 iversym.vs_vers = 1;
9876 else
9877 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9878 if (flinfo->info->create_default_symver)
3e3b46e5 9879 iversym.vs_vers++;
c152c796
AM
9880 }
9881
422f1182 9882 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9883 defined locally. */
422f1182 9884 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9885 iversym.vs_vers |= VERSYM_HIDDEN;
9886
8b127cbc 9887 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9888 eversym += h->dynindx;
8b127cbc 9889 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9890 }
9891 }
9892
d983c8c5
AM
9893 /* If the symbol is undefined, and we didn't output it to .dynsym,
9894 strip it from .symtab too. Obviously we can't do this for
9895 relocatable output or when needed for --emit-relocs. */
9896 else if (input_sec == bfd_und_section_ptr
9897 && h->indx != -2
0e1862bb 9898 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9899 return TRUE;
9900 /* Also strip others that we couldn't earlier due to dynamic symbol
9901 processing. */
9902 if (strip)
9903 return TRUE;
9904 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9905 return TRUE;
9906
2ec55de3
AM
9907 /* Output a FILE symbol so that following locals are not associated
9908 with the wrong input file. We need one for forced local symbols
9909 if we've seen more than one FILE symbol or when we have exactly
9910 one FILE symbol but global symbols are present in a file other
9911 than the one with the FILE symbol. We also need one if linker
9912 defined symbols are present. In practice these conditions are
9913 always met, so just emit the FILE symbol unconditionally. */
9914 if (eoinfo->localsyms
9915 && !eoinfo->file_sym_done
9916 && eoinfo->flinfo->filesym_count != 0)
9917 {
9918 Elf_Internal_Sym fsym;
9919
9920 memset (&fsym, 0, sizeof (fsym));
9921 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9922 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9923 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9924 bfd_und_section_ptr, NULL))
2ec55de3
AM
9925 return FALSE;
9926
9927 eoinfo->file_sym_done = TRUE;
9928 }
9929
8b127cbc 9930 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9931 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9932 input_sec, h);
6e0b88f1 9933 if (ret == 0)
c152c796
AM
9934 {
9935 eoinfo->failed = TRUE;
9936 return FALSE;
9937 }
6e0b88f1
AM
9938 else if (ret == 1)
9939 h->indx = indx;
9940 else if (h->indx == -2)
9941 abort();
c152c796
AM
9942
9943 return TRUE;
9944}
9945
cdd3575c
AM
9946/* Return TRUE if special handling is done for relocs in SEC against
9947 symbols defined in discarded sections. */
9948
c152c796
AM
9949static bfd_boolean
9950elf_section_ignore_discarded_relocs (asection *sec)
9951{
9952 const struct elf_backend_data *bed;
9953
cdd3575c
AM
9954 switch (sec->sec_info_type)
9955 {
dbaa2011
AM
9956 case SEC_INFO_TYPE_STABS:
9957 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9958 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9959 return TRUE;
9960 default:
9961 break;
9962 }
c152c796
AM
9963
9964 bed = get_elf_backend_data (sec->owner);
9965 if (bed->elf_backend_ignore_discarded_relocs != NULL
9966 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9967 return TRUE;
9968
9969 return FALSE;
9970}
9971
9e66c942
AM
9972/* Return a mask saying how ld should treat relocations in SEC against
9973 symbols defined in discarded sections. If this function returns
9974 COMPLAIN set, ld will issue a warning message. If this function
9975 returns PRETEND set, and the discarded section was link-once and the
9976 same size as the kept link-once section, ld will pretend that the
9977 symbol was actually defined in the kept section. Otherwise ld will
9978 zero the reloc (at least that is the intent, but some cooperation by
9979 the target dependent code is needed, particularly for REL targets). */
9980
8a696751
AM
9981unsigned int
9982_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9983{
9e66c942 9984 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9985 return PRETEND;
cdd3575c
AM
9986
9987 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9988 return 0;
cdd3575c
AM
9989
9990 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9991 return 0;
cdd3575c 9992
9e66c942 9993 return COMPLAIN | PRETEND;
cdd3575c
AM
9994}
9995
3d7f7666
L
9996/* Find a match between a section and a member of a section group. */
9997
9998static asection *
c0f00686
L
9999match_group_member (asection *sec, asection *group,
10000 struct bfd_link_info *info)
3d7f7666
L
10001{
10002 asection *first = elf_next_in_group (group);
10003 asection *s = first;
10004
10005 while (s != NULL)
10006 {
c0f00686 10007 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10008 return s;
10009
83180ade 10010 s = elf_next_in_group (s);
3d7f7666
L
10011 if (s == first)
10012 break;
10013 }
10014
10015 return NULL;
10016}
10017
01b3c8ab 10018/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10019 to replace it. Return the replacement if it is OK. Otherwise return
10020 NULL. */
01b3c8ab
L
10021
10022asection *
c0f00686 10023_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10024{
10025 asection *kept;
10026
10027 kept = sec->kept_section;
10028 if (kept != NULL)
10029 {
c2370991 10030 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10031 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10032 if (kept != NULL
10033 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10034 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10035 kept = NULL;
c2370991 10036 sec->kept_section = kept;
01b3c8ab
L
10037 }
10038 return kept;
10039}
10040
c152c796
AM
10041/* Link an input file into the linker output file. This function
10042 handles all the sections and relocations of the input file at once.
10043 This is so that we only have to read the local symbols once, and
10044 don't have to keep them in memory. */
10045
10046static bfd_boolean
8b127cbc 10047elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10048{
ece5ef60 10049 int (*relocate_section)
c152c796
AM
10050 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10051 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10052 bfd *output_bfd;
10053 Elf_Internal_Shdr *symtab_hdr;
10054 size_t locsymcount;
10055 size_t extsymoff;
10056 Elf_Internal_Sym *isymbuf;
10057 Elf_Internal_Sym *isym;
10058 Elf_Internal_Sym *isymend;
10059 long *pindex;
10060 asection **ppsection;
10061 asection *o;
10062 const struct elf_backend_data *bed;
c152c796 10063 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10064 bfd_size_type address_size;
10065 bfd_vma r_type_mask;
10066 int r_sym_shift;
ffbc01cc 10067 bfd_boolean have_file_sym = FALSE;
c152c796 10068
8b127cbc 10069 output_bfd = flinfo->output_bfd;
c152c796
AM
10070 bed = get_elf_backend_data (output_bfd);
10071 relocate_section = bed->elf_backend_relocate_section;
10072
10073 /* If this is a dynamic object, we don't want to do anything here:
10074 we don't want the local symbols, and we don't want the section
10075 contents. */
10076 if ((input_bfd->flags & DYNAMIC) != 0)
10077 return TRUE;
10078
c152c796
AM
10079 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10080 if (elf_bad_symtab (input_bfd))
10081 {
10082 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10083 extsymoff = 0;
10084 }
10085 else
10086 {
10087 locsymcount = symtab_hdr->sh_info;
10088 extsymoff = symtab_hdr->sh_info;
10089 }
10090
10091 /* Read the local symbols. */
10092 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10093 if (isymbuf == NULL && locsymcount != 0)
10094 {
10095 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10096 flinfo->internal_syms,
10097 flinfo->external_syms,
10098 flinfo->locsym_shndx);
c152c796
AM
10099 if (isymbuf == NULL)
10100 return FALSE;
10101 }
10102
10103 /* Find local symbol sections and adjust values of symbols in
10104 SEC_MERGE sections. Write out those local symbols we know are
10105 going into the output file. */
10106 isymend = isymbuf + locsymcount;
8b127cbc 10107 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10108 isym < isymend;
10109 isym++, pindex++, ppsection++)
10110 {
10111 asection *isec;
10112 const char *name;
10113 Elf_Internal_Sym osym;
6e0b88f1
AM
10114 long indx;
10115 int ret;
c152c796
AM
10116
10117 *pindex = -1;
10118
10119 if (elf_bad_symtab (input_bfd))
10120 {
10121 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10122 {
10123 *ppsection = NULL;
10124 continue;
10125 }
10126 }
10127
10128 if (isym->st_shndx == SHN_UNDEF)
10129 isec = bfd_und_section_ptr;
c152c796
AM
10130 else if (isym->st_shndx == SHN_ABS)
10131 isec = bfd_abs_section_ptr;
10132 else if (isym->st_shndx == SHN_COMMON)
10133 isec = bfd_com_section_ptr;
10134 else
10135 {
cb33740c
AM
10136 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10137 if (isec == NULL)
10138 {
10139 /* Don't attempt to output symbols with st_shnx in the
10140 reserved range other than SHN_ABS and SHN_COMMON. */
10141 *ppsection = NULL;
10142 continue;
10143 }
dbaa2011 10144 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10145 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10146 isym->st_value =
10147 _bfd_merged_section_offset (output_bfd, &isec,
10148 elf_section_data (isec)->sec_info,
10149 isym->st_value);
c152c796
AM
10150 }
10151
10152 *ppsection = isec;
10153
d983c8c5
AM
10154 /* Don't output the first, undefined, symbol. In fact, don't
10155 output any undefined local symbol. */
10156 if (isec == bfd_und_section_ptr)
c152c796
AM
10157 continue;
10158
10159 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10160 {
10161 /* We never output section symbols. Instead, we use the
10162 section symbol of the corresponding section in the output
10163 file. */
10164 continue;
10165 }
10166
10167 /* If we are stripping all symbols, we don't want to output this
10168 one. */
8b127cbc 10169 if (flinfo->info->strip == strip_all)
c152c796
AM
10170 continue;
10171
10172 /* If we are discarding all local symbols, we don't want to
10173 output this one. If we are generating a relocatable output
10174 file, then some of the local symbols may be required by
10175 relocs; we output them below as we discover that they are
10176 needed. */
8b127cbc 10177 if (flinfo->info->discard == discard_all)
c152c796
AM
10178 continue;
10179
10180 /* If this symbol is defined in a section which we are
f02571c5
AM
10181 discarding, we don't need to keep it. */
10182 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10183 && isym->st_shndx < SHN_LORESERVE
10184 && bfd_section_removed_from_list (output_bfd,
10185 isec->output_section))
e75a280b
L
10186 continue;
10187
c152c796
AM
10188 /* Get the name of the symbol. */
10189 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10190 isym->st_name);
10191 if (name == NULL)
10192 return FALSE;
10193
10194 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10195 if ((flinfo->info->strip == strip_some
10196 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10197 == NULL))
8b127cbc 10198 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10199 && (isec->flags & SEC_MERGE)
10200 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10201 || flinfo->info->discard == discard_l)
c152c796
AM
10202 && bfd_is_local_label_name (input_bfd, name)))
10203 continue;
10204
ffbc01cc
AM
10205 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10206 {
ce875075
AM
10207 if (input_bfd->lto_output)
10208 /* -flto puts a temp file name here. This means builds
10209 are not reproducible. Discard the symbol. */
10210 continue;
ffbc01cc
AM
10211 have_file_sym = TRUE;
10212 flinfo->filesym_count += 1;
10213 }
10214 if (!have_file_sym)
10215 {
10216 /* In the absence of debug info, bfd_find_nearest_line uses
10217 FILE symbols to determine the source file for local
10218 function symbols. Provide a FILE symbol here if input
10219 files lack such, so that their symbols won't be
10220 associated with a previous input file. It's not the
10221 source file, but the best we can do. */
10222 have_file_sym = TRUE;
10223 flinfo->filesym_count += 1;
10224 memset (&osym, 0, sizeof (osym));
10225 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10226 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10227 if (!elf_link_output_symstrtab (flinfo,
10228 (input_bfd->lto_output ? NULL
10229 : input_bfd->filename),
10230 &osym, bfd_abs_section_ptr,
10231 NULL))
ffbc01cc
AM
10232 return FALSE;
10233 }
10234
c152c796
AM
10235 osym = *isym;
10236
10237 /* Adjust the section index for the output file. */
10238 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10239 isec->output_section);
10240 if (osym.st_shndx == SHN_BAD)
10241 return FALSE;
10242
c152c796
AM
10243 /* ELF symbols in relocatable files are section relative, but
10244 in executable files they are virtual addresses. Note that
10245 this code assumes that all ELF sections have an associated
10246 BFD section with a reasonable value for output_offset; below
10247 we assume that they also have a reasonable value for
10248 output_section. Any special sections must be set up to meet
10249 these requirements. */
10250 osym.st_value += isec->output_offset;
0e1862bb 10251 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10252 {
10253 osym.st_value += isec->output_section->vma;
10254 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10255 {
10256 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10257 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10258 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10259 }
10260 }
10261
6e0b88f1 10262 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10263 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10264 if (ret == 0)
c152c796 10265 return FALSE;
6e0b88f1
AM
10266 else if (ret == 1)
10267 *pindex = indx;
c152c796
AM
10268 }
10269
310fd250
L
10270 if (bed->s->arch_size == 32)
10271 {
10272 r_type_mask = 0xff;
10273 r_sym_shift = 8;
10274 address_size = 4;
10275 }
10276 else
10277 {
10278 r_type_mask = 0xffffffff;
10279 r_sym_shift = 32;
10280 address_size = 8;
10281 }
10282
c152c796
AM
10283 /* Relocate the contents of each section. */
10284 sym_hashes = elf_sym_hashes (input_bfd);
10285 for (o = input_bfd->sections; o != NULL; o = o->next)
10286 {
10287 bfd_byte *contents;
10288
10289 if (! o->linker_mark)
10290 {
10291 /* This section was omitted from the link. */
10292 continue;
10293 }
10294
7bdf4127 10295 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10296 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10297 {
10298 /* Deal with the group signature symbol. */
10299 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10300 unsigned long symndx = sec_data->this_hdr.sh_info;
10301 asection *osec = o->output_section;
10302
7bdf4127 10303 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10304 if (symndx >= locsymcount
10305 || (elf_bad_symtab (input_bfd)
8b127cbc 10306 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10307 {
10308 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10309 while (h->root.type == bfd_link_hash_indirect
10310 || h->root.type == bfd_link_hash_warning)
10311 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10312 /* Arrange for symbol to be output. */
10313 h->indx = -2;
10314 elf_section_data (osec)->this_hdr.sh_info = -2;
10315 }
10316 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10317 {
10318 /* We'll use the output section target_index. */
8b127cbc 10319 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10320 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10321 }
10322 else
10323 {
8b127cbc 10324 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10325 {
10326 /* Otherwise output the local symbol now. */
10327 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10328 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10329 const char *name;
6e0b88f1
AM
10330 long indx;
10331 int ret;
bcacc0f5
AM
10332
10333 name = bfd_elf_string_from_elf_section (input_bfd,
10334 symtab_hdr->sh_link,
10335 sym.st_name);
10336 if (name == NULL)
10337 return FALSE;
10338
10339 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10340 sec);
10341 if (sym.st_shndx == SHN_BAD)
10342 return FALSE;
10343
10344 sym.st_value += o->output_offset;
10345
6e0b88f1 10346 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10347 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10348 NULL);
6e0b88f1 10349 if (ret == 0)
bcacc0f5 10350 return FALSE;
6e0b88f1 10351 else if (ret == 1)
8b127cbc 10352 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10353 else
10354 abort ();
bcacc0f5
AM
10355 }
10356 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10357 = flinfo->indices[symndx];
bcacc0f5
AM
10358 }
10359 }
10360
c152c796 10361 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10362 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10363 continue;
10364
10365 if ((o->flags & SEC_LINKER_CREATED) != 0)
10366 {
10367 /* Section was created by _bfd_elf_link_create_dynamic_sections
10368 or somesuch. */
10369 continue;
10370 }
10371
10372 /* Get the contents of the section. They have been cached by a
10373 relaxation routine. Note that o is a section in an input
10374 file, so the contents field will not have been set by any of
10375 the routines which work on output files. */
10376 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10377 {
10378 contents = elf_section_data (o)->this_hdr.contents;
10379 if (bed->caches_rawsize
10380 && o->rawsize != 0
10381 && o->rawsize < o->size)
10382 {
10383 memcpy (flinfo->contents, contents, o->rawsize);
10384 contents = flinfo->contents;
10385 }
10386 }
c152c796
AM
10387 else
10388 {
8b127cbc 10389 contents = flinfo->contents;
4a114e3e 10390 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10391 return FALSE;
10392 }
10393
10394 if ((o->flags & SEC_RELOC) != 0)
10395 {
10396 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10397 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10398 int action_discarded;
ece5ef60 10399 int ret;
c152c796
AM
10400
10401 /* Get the swapped relocs. */
10402 internal_relocs
8b127cbc
AM
10403 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10404 flinfo->internal_relocs, FALSE);
c152c796
AM
10405 if (internal_relocs == NULL
10406 && o->reloc_count > 0)
10407 return FALSE;
10408
310fd250
L
10409 /* We need to reverse-copy input .ctors/.dtors sections if
10410 they are placed in .init_array/.finit_array for output. */
10411 if (o->size > address_size
10412 && ((strncmp (o->name, ".ctors", 6) == 0
10413 && strcmp (o->output_section->name,
10414 ".init_array") == 0)
10415 || (strncmp (o->name, ".dtors", 6) == 0
10416 && strcmp (o->output_section->name,
10417 ".fini_array") == 0))
10418 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10419 {
056bafd4
MR
10420 if (o->size * bed->s->int_rels_per_ext_rel
10421 != o->reloc_count * address_size)
310fd250 10422 {
4eca0228 10423 _bfd_error_handler
695344c0 10424 /* xgettext:c-format */
310fd250
L
10425 (_("error: %B: size of section %A is not "
10426 "multiple of address size"),
10427 input_bfd, o);
10428 bfd_set_error (bfd_error_on_input);
10429 return FALSE;
10430 }
10431 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10432 }
10433
0f02bbd9 10434 action_discarded = -1;
c152c796 10435 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10436 action_discarded = (*bed->action_discarded) (o);
10437
10438 /* Run through the relocs evaluating complex reloc symbols and
10439 looking for relocs against symbols from discarded sections
10440 or section symbols from removed link-once sections.
10441 Complain about relocs against discarded sections. Zero
10442 relocs against removed link-once sections. */
10443
10444 rel = internal_relocs;
056bafd4 10445 relend = rel + o->reloc_count;
0f02bbd9 10446 for ( ; rel < relend; rel++)
c152c796 10447 {
0f02bbd9
AM
10448 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10449 unsigned int s_type;
10450 asection **ps, *sec;
10451 struct elf_link_hash_entry *h = NULL;
10452 const char *sym_name;
c152c796 10453
0f02bbd9
AM
10454 if (r_symndx == STN_UNDEF)
10455 continue;
c152c796 10456
0f02bbd9
AM
10457 if (r_symndx >= locsymcount
10458 || (elf_bad_symtab (input_bfd)
8b127cbc 10459 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10460 {
10461 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10462
0f02bbd9
AM
10463 /* Badly formatted input files can contain relocs that
10464 reference non-existant symbols. Check here so that
10465 we do not seg fault. */
10466 if (h == NULL)
c152c796 10467 {
0f02bbd9 10468 char buffer [32];
dce669a1 10469
0f02bbd9 10470 sprintf_vma (buffer, rel->r_info);
4eca0228 10471 _bfd_error_handler
695344c0 10472 /* xgettext:c-format */
0f02bbd9
AM
10473 (_("error: %B contains a reloc (0x%s) for section %A "
10474 "that references a non-existent global symbol"),
c08bb8dd 10475 input_bfd, buffer, o);
0f02bbd9
AM
10476 bfd_set_error (bfd_error_bad_value);
10477 return FALSE;
10478 }
3b36f7e6 10479
0f02bbd9
AM
10480 while (h->root.type == bfd_link_hash_indirect
10481 || h->root.type == bfd_link_hash_warning)
10482 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10483
0f02bbd9 10484 s_type = h->type;
cdd3575c 10485
9e2dec47 10486 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10487 mark the symbol as undefined. Note that the
10488 linker may attach linker created dynamic sections
10489 to the plugin bfd. Symbols defined in linker
10490 created sections are not plugin symbols. */
bc4e12de 10491 if ((h->root.non_ir_ref_regular
4070765b 10492 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10493 && (h->root.type == bfd_link_hash_defined
10494 || h->root.type == bfd_link_hash_defweak)
10495 && (h->root.u.def.section->flags
10496 & SEC_LINKER_CREATED) == 0
10497 && h->root.u.def.section->owner != NULL
10498 && (h->root.u.def.section->owner->flags
10499 & BFD_PLUGIN) != 0)
10500 {
10501 h->root.type = bfd_link_hash_undefined;
10502 h->root.u.undef.abfd = h->root.u.def.section->owner;
10503 }
10504
0f02bbd9
AM
10505 ps = NULL;
10506 if (h->root.type == bfd_link_hash_defined
10507 || h->root.type == bfd_link_hash_defweak)
10508 ps = &h->root.u.def.section;
10509
10510 sym_name = h->root.root.string;
10511 }
10512 else
10513 {
10514 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10515
10516 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10517 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10518 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10519 sym, *ps);
10520 }
c152c796 10521
c301e700 10522 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10523 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10524 {
10525 bfd_vma val;
10526 bfd_vma dot = (rel->r_offset
10527 + o->output_offset + o->output_section->vma);
10528#ifdef DEBUG
10529 printf ("Encountered a complex symbol!");
10530 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10531 input_bfd->filename, o->name,
10532 (long) (rel - internal_relocs));
0f02bbd9
AM
10533 printf (" symbol: idx %8.8lx, name %s\n",
10534 r_symndx, sym_name);
10535 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10536 (unsigned long) rel->r_info,
10537 (unsigned long) rel->r_offset);
10538#endif
8b127cbc 10539 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10540 isymbuf, locsymcount, s_type == STT_SRELC))
10541 return FALSE;
10542
10543 /* Symbol evaluated OK. Update to absolute value. */
10544 set_symbol_value (input_bfd, isymbuf, locsymcount,
10545 r_symndx, val);
10546 continue;
10547 }
10548
10549 if (action_discarded != -1 && ps != NULL)
10550 {
cdd3575c
AM
10551 /* Complain if the definition comes from a
10552 discarded section. */
dbaa2011 10553 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10554 {
cf35638d 10555 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10556 if (action_discarded & COMPLAIN)
8b127cbc 10557 (*flinfo->info->callbacks->einfo)
695344c0 10558 /* xgettext:c-format */
e1fffbe6 10559 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10560 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10561 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10562
87e5235d 10563 /* Try to do the best we can to support buggy old
e0ae6d6f 10564 versions of gcc. Pretend that the symbol is
87e5235d
AM
10565 really defined in the kept linkonce section.
10566 FIXME: This is quite broken. Modifying the
10567 symbol here means we will be changing all later
e0ae6d6f 10568 uses of the symbol, not just in this section. */
0f02bbd9 10569 if (action_discarded & PRETEND)
87e5235d 10570 {
01b3c8ab
L
10571 asection *kept;
10572
c0f00686 10573 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10574 flinfo->info);
01b3c8ab 10575 if (kept != NULL)
87e5235d
AM
10576 {
10577 *ps = kept;
10578 continue;
10579 }
10580 }
c152c796
AM
10581 }
10582 }
10583 }
10584
10585 /* Relocate the section by invoking a back end routine.
10586
10587 The back end routine is responsible for adjusting the
10588 section contents as necessary, and (if using Rela relocs
10589 and generating a relocatable output file) adjusting the
10590 reloc addend as necessary.
10591
10592 The back end routine does not have to worry about setting
10593 the reloc address or the reloc symbol index.
10594
10595 The back end routine is given a pointer to the swapped in
10596 internal symbols, and can access the hash table entries
10597 for the external symbols via elf_sym_hashes (input_bfd).
10598
10599 When generating relocatable output, the back end routine
10600 must handle STB_LOCAL/STT_SECTION symbols specially. The
10601 output symbol is going to be a section symbol
10602 corresponding to the output section, which will require
10603 the addend to be adjusted. */
10604
8b127cbc 10605 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10606 input_bfd, o, contents,
10607 internal_relocs,
10608 isymbuf,
8b127cbc 10609 flinfo->sections);
ece5ef60 10610 if (!ret)
c152c796
AM
10611 return FALSE;
10612
ece5ef60 10613 if (ret == 2
0e1862bb 10614 || bfd_link_relocatable (flinfo->info)
8b127cbc 10615 || flinfo->info->emitrelocations)
c152c796
AM
10616 {
10617 Elf_Internal_Rela *irela;
d4730f92 10618 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10619 bfd_vma last_offset;
10620 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10621 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10622 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10623 unsigned int next_erel;
c152c796 10624 bfd_boolean rela_normal;
d4730f92 10625 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10626
d4730f92
BS
10627 esdi = elf_section_data (o);
10628 esdo = elf_section_data (o->output_section);
10629 rela_normal = FALSE;
c152c796
AM
10630
10631 /* Adjust the reloc addresses and symbol indices. */
10632
10633 irela = internal_relocs;
056bafd4 10634 irelaend = irela + o->reloc_count;
d4730f92
BS
10635 rel_hash = esdo->rel.hashes + esdo->rel.count;
10636 /* We start processing the REL relocs, if any. When we reach
10637 IRELAMID in the loop, we switch to the RELA relocs. */
10638 irelamid = irela;
10639 if (esdi->rel.hdr != NULL)
10640 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10641 * bed->s->int_rels_per_ext_rel);
eac338cf 10642 rel_hash_list = rel_hash;
d4730f92 10643 rela_hash_list = NULL;
c152c796 10644 last_offset = o->output_offset;
0e1862bb 10645 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10646 last_offset += o->output_section->vma;
10647 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10648 {
10649 unsigned long r_symndx;
10650 asection *sec;
10651 Elf_Internal_Sym sym;
10652
10653 if (next_erel == bed->s->int_rels_per_ext_rel)
10654 {
10655 rel_hash++;
10656 next_erel = 0;
10657 }
10658
d4730f92
BS
10659 if (irela == irelamid)
10660 {
10661 rel_hash = esdo->rela.hashes + esdo->rela.count;
10662 rela_hash_list = rel_hash;
10663 rela_normal = bed->rela_normal;
10664 }
10665
c152c796 10666 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10667 flinfo->info, o,
c152c796
AM
10668 irela->r_offset);
10669 if (irela->r_offset >= (bfd_vma) -2)
10670 {
10671 /* This is a reloc for a deleted entry or somesuch.
10672 Turn it into an R_*_NONE reloc, at the same
10673 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10674 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10675 being ordered. */
10676 irela->r_offset = last_offset;
10677 irela->r_info = 0;
10678 irela->r_addend = 0;
10679 continue;
10680 }
10681
10682 irela->r_offset += o->output_offset;
10683
10684 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10685 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10686 irela->r_offset += o->output_section->vma;
10687
10688 last_offset = irela->r_offset;
10689
10690 r_symndx = irela->r_info >> r_sym_shift;
10691 if (r_symndx == STN_UNDEF)
10692 continue;
10693
10694 if (r_symndx >= locsymcount
10695 || (elf_bad_symtab (input_bfd)
8b127cbc 10696 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10697 {
10698 struct elf_link_hash_entry *rh;
10699 unsigned long indx;
10700
10701 /* This is a reloc against a global symbol. We
10702 have not yet output all the local symbols, so
10703 we do not know the symbol index of any global
10704 symbol. We set the rel_hash entry for this
10705 reloc to point to the global hash table entry
10706 for this symbol. The symbol index is then
ee75fd95 10707 set at the end of bfd_elf_final_link. */
c152c796
AM
10708 indx = r_symndx - extsymoff;
10709 rh = elf_sym_hashes (input_bfd)[indx];
10710 while (rh->root.type == bfd_link_hash_indirect
10711 || rh->root.type == bfd_link_hash_warning)
10712 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10713
10714 /* Setting the index to -2 tells
10715 elf_link_output_extsym that this symbol is
10716 used by a reloc. */
10717 BFD_ASSERT (rh->indx < 0);
10718 rh->indx = -2;
c152c796
AM
10719 *rel_hash = rh;
10720
10721 continue;
10722 }
10723
10724 /* This is a reloc against a local symbol. */
10725
10726 *rel_hash = NULL;
10727 sym = isymbuf[r_symndx];
8b127cbc 10728 sec = flinfo->sections[r_symndx];
c152c796
AM
10729 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10730 {
10731 /* I suppose the backend ought to fill in the
10732 section of any STT_SECTION symbol against a
6a8d1586 10733 processor specific section. */
cf35638d 10734 r_symndx = STN_UNDEF;
6a8d1586
AM
10735 if (bfd_is_abs_section (sec))
10736 ;
c152c796
AM
10737 else if (sec == NULL || sec->owner == NULL)
10738 {
10739 bfd_set_error (bfd_error_bad_value);
10740 return FALSE;
10741 }
10742 else
10743 {
6a8d1586
AM
10744 asection *osec = sec->output_section;
10745
10746 /* If we have discarded a section, the output
10747 section will be the absolute section. In
ab96bf03
AM
10748 case of discarded SEC_MERGE sections, use
10749 the kept section. relocate_section should
10750 have already handled discarded linkonce
10751 sections. */
6a8d1586
AM
10752 if (bfd_is_abs_section (osec)
10753 && sec->kept_section != NULL
10754 && sec->kept_section->output_section != NULL)
10755 {
10756 osec = sec->kept_section->output_section;
10757 irela->r_addend -= osec->vma;
10758 }
10759
10760 if (!bfd_is_abs_section (osec))
10761 {
10762 r_symndx = osec->target_index;
cf35638d 10763 if (r_symndx == STN_UNDEF)
74541ad4 10764 {
051d833a
AM
10765 irela->r_addend += osec->vma;
10766 osec = _bfd_nearby_section (output_bfd, osec,
10767 osec->vma);
10768 irela->r_addend -= osec->vma;
10769 r_symndx = osec->target_index;
74541ad4 10770 }
6a8d1586 10771 }
c152c796
AM
10772 }
10773
10774 /* Adjust the addend according to where the
10775 section winds up in the output section. */
10776 if (rela_normal)
10777 irela->r_addend += sec->output_offset;
10778 }
10779 else
10780 {
8b127cbc 10781 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10782 {
10783 unsigned long shlink;
10784 const char *name;
10785 asection *osec;
6e0b88f1 10786 long indx;
c152c796 10787
8b127cbc 10788 if (flinfo->info->strip == strip_all)
c152c796
AM
10789 {
10790 /* You can't do ld -r -s. */
10791 bfd_set_error (bfd_error_invalid_operation);
10792 return FALSE;
10793 }
10794
10795 /* This symbol was skipped earlier, but
10796 since it is needed by a reloc, we
10797 must output it now. */
10798 shlink = symtab_hdr->sh_link;
10799 name = (bfd_elf_string_from_elf_section
10800 (input_bfd, shlink, sym.st_name));
10801 if (name == NULL)
10802 return FALSE;
10803
10804 osec = sec->output_section;
10805 sym.st_shndx =
10806 _bfd_elf_section_from_bfd_section (output_bfd,
10807 osec);
10808 if (sym.st_shndx == SHN_BAD)
10809 return FALSE;
10810
10811 sym.st_value += sec->output_offset;
0e1862bb 10812 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10813 {
10814 sym.st_value += osec->vma;
10815 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10816 {
10817 /* STT_TLS symbols are relative to PT_TLS
10818 segment base. */
8b127cbc 10819 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10820 ->tls_sec != NULL);
8b127cbc 10821 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10822 ->tls_sec->vma);
10823 }
10824 }
10825
6e0b88f1 10826 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10827 ret = elf_link_output_symstrtab (flinfo, name,
10828 &sym, sec,
10829 NULL);
6e0b88f1 10830 if (ret == 0)
c152c796 10831 return FALSE;
6e0b88f1 10832 else if (ret == 1)
8b127cbc 10833 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10834 else
10835 abort ();
c152c796
AM
10836 }
10837
8b127cbc 10838 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10839 }
10840
10841 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10842 | (irela->r_info & r_type_mask));
10843 }
10844
10845 /* Swap out the relocs. */
d4730f92
BS
10846 input_rel_hdr = esdi->rel.hdr;
10847 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10848 {
d4730f92
BS
10849 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10850 input_rel_hdr,
10851 internal_relocs,
10852 rel_hash_list))
10853 return FALSE;
c152c796
AM
10854 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10855 * bed->s->int_rels_per_ext_rel);
eac338cf 10856 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10857 }
10858
10859 input_rela_hdr = esdi->rela.hdr;
10860 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10861 {
eac338cf 10862 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10863 input_rela_hdr,
eac338cf 10864 internal_relocs,
d4730f92 10865 rela_hash_list))
c152c796
AM
10866 return FALSE;
10867 }
10868 }
10869 }
10870
10871 /* Write out the modified section contents. */
10872 if (bed->elf_backend_write_section
8b127cbc 10873 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10874 contents))
c152c796
AM
10875 {
10876 /* Section written out. */
10877 }
10878 else switch (o->sec_info_type)
10879 {
dbaa2011 10880 case SEC_INFO_TYPE_STABS:
c152c796
AM
10881 if (! (_bfd_write_section_stabs
10882 (output_bfd,
8b127cbc 10883 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10884 o, &elf_section_data (o)->sec_info, contents)))
10885 return FALSE;
10886 break;
dbaa2011 10887 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10888 if (! _bfd_write_merged_section (output_bfd, o,
10889 elf_section_data (o)->sec_info))
10890 return FALSE;
10891 break;
dbaa2011 10892 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10893 {
8b127cbc 10894 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10895 o, contents))
10896 return FALSE;
10897 }
10898 break;
2f0c68f2
CM
10899 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10900 {
10901 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10902 flinfo->info,
10903 o, contents))
10904 return FALSE;
10905 }
10906 break;
c152c796
AM
10907 default:
10908 {
310fd250
L
10909 if (! (o->flags & SEC_EXCLUDE))
10910 {
10911 file_ptr offset = (file_ptr) o->output_offset;
10912 bfd_size_type todo = o->size;
37b01f6a
DG
10913
10914 offset *= bfd_octets_per_byte (output_bfd);
10915
310fd250
L
10916 if ((o->flags & SEC_ELF_REVERSE_COPY))
10917 {
10918 /* Reverse-copy input section to output. */
10919 do
10920 {
10921 todo -= address_size;
10922 if (! bfd_set_section_contents (output_bfd,
10923 o->output_section,
10924 contents + todo,
10925 offset,
10926 address_size))
10927 return FALSE;
10928 if (todo == 0)
10929 break;
10930 offset += address_size;
10931 }
10932 while (1);
10933 }
10934 else if (! bfd_set_section_contents (output_bfd,
10935 o->output_section,
10936 contents,
10937 offset, todo))
10938 return FALSE;
10939 }
c152c796
AM
10940 }
10941 break;
10942 }
10943 }
10944
10945 return TRUE;
10946}
10947
10948/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10949 requested by the linker, and does not come from any input file. This
c152c796
AM
10950 is used to build constructor and destructor tables when linking
10951 with -Ur. */
10952
10953static bfd_boolean
10954elf_reloc_link_order (bfd *output_bfd,
10955 struct bfd_link_info *info,
10956 asection *output_section,
10957 struct bfd_link_order *link_order)
10958{
10959 reloc_howto_type *howto;
10960 long indx;
10961 bfd_vma offset;
10962 bfd_vma addend;
d4730f92 10963 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10964 struct elf_link_hash_entry **rel_hash_ptr;
10965 Elf_Internal_Shdr *rel_hdr;
10966 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10967 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10968 bfd_byte *erel;
10969 unsigned int i;
d4730f92 10970 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10971
10972 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10973 if (howto == NULL)
10974 {
10975 bfd_set_error (bfd_error_bad_value);
10976 return FALSE;
10977 }
10978
10979 addend = link_order->u.reloc.p->addend;
10980
d4730f92
BS
10981 if (esdo->rel.hdr)
10982 reldata = &esdo->rel;
10983 else if (esdo->rela.hdr)
10984 reldata = &esdo->rela;
10985 else
10986 {
10987 reldata = NULL;
10988 BFD_ASSERT (0);
10989 }
10990
c152c796 10991 /* Figure out the symbol index. */
d4730f92 10992 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10993 if (link_order->type == bfd_section_reloc_link_order)
10994 {
10995 indx = link_order->u.reloc.p->u.section->target_index;
10996 BFD_ASSERT (indx != 0);
10997 *rel_hash_ptr = NULL;
10998 }
10999 else
11000 {
11001 struct elf_link_hash_entry *h;
11002
11003 /* Treat a reloc against a defined symbol as though it were
11004 actually against the section. */
11005 h = ((struct elf_link_hash_entry *)
11006 bfd_wrapped_link_hash_lookup (output_bfd, info,
11007 link_order->u.reloc.p->u.name,
11008 FALSE, FALSE, TRUE));
11009 if (h != NULL
11010 && (h->root.type == bfd_link_hash_defined
11011 || h->root.type == bfd_link_hash_defweak))
11012 {
11013 asection *section;
11014
11015 section = h->root.u.def.section;
11016 indx = section->output_section->target_index;
11017 *rel_hash_ptr = NULL;
11018 /* It seems that we ought to add the symbol value to the
11019 addend here, but in practice it has already been added
11020 because it was passed to constructor_callback. */
11021 addend += section->output_section->vma + section->output_offset;
11022 }
11023 else if (h != NULL)
11024 {
11025 /* Setting the index to -2 tells elf_link_output_extsym that
11026 this symbol is used by a reloc. */
11027 h->indx = -2;
11028 *rel_hash_ptr = h;
11029 indx = 0;
11030 }
11031 else
11032 {
1a72702b
AM
11033 (*info->callbacks->unattached_reloc)
11034 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11035 indx = 0;
11036 }
11037 }
11038
11039 /* If this is an inplace reloc, we must write the addend into the
11040 object file. */
11041 if (howto->partial_inplace && addend != 0)
11042 {
11043 bfd_size_type size;
11044 bfd_reloc_status_type rstat;
11045 bfd_byte *buf;
11046 bfd_boolean ok;
11047 const char *sym_name;
11048
a50b1753
NC
11049 size = (bfd_size_type) bfd_get_reloc_size (howto);
11050 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11051 if (buf == NULL && size != 0)
c152c796
AM
11052 return FALSE;
11053 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11054 switch (rstat)
11055 {
11056 case bfd_reloc_ok:
11057 break;
11058
11059 default:
11060 case bfd_reloc_outofrange:
11061 abort ();
11062
11063 case bfd_reloc_overflow:
11064 if (link_order->type == bfd_section_reloc_link_order)
11065 sym_name = bfd_section_name (output_bfd,
11066 link_order->u.reloc.p->u.section);
11067 else
11068 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11069 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11070 howto->name, addend, NULL, NULL,
11071 (bfd_vma) 0);
c152c796
AM
11072 break;
11073 }
37b01f6a 11074
c152c796 11075 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11076 link_order->offset
11077 * bfd_octets_per_byte (output_bfd),
11078 size);
c152c796
AM
11079 free (buf);
11080 if (! ok)
11081 return FALSE;
11082 }
11083
11084 /* The address of a reloc is relative to the section in a
11085 relocatable file, and is a virtual address in an executable
11086 file. */
11087 offset = link_order->offset;
0e1862bb 11088 if (! bfd_link_relocatable (info))
c152c796
AM
11089 offset += output_section->vma;
11090
11091 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11092 {
11093 irel[i].r_offset = offset;
11094 irel[i].r_info = 0;
11095 irel[i].r_addend = 0;
11096 }
11097 if (bed->s->arch_size == 32)
11098 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11099 else
11100 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11101
d4730f92 11102 rel_hdr = reldata->hdr;
c152c796
AM
11103 erel = rel_hdr->contents;
11104 if (rel_hdr->sh_type == SHT_REL)
11105 {
d4730f92 11106 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11107 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11108 }
11109 else
11110 {
11111 irel[0].r_addend = addend;
d4730f92 11112 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11113 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11114 }
11115
d4730f92 11116 ++reldata->count;
c152c796
AM
11117
11118 return TRUE;
11119}
11120
0b52efa6
PB
11121
11122/* Get the output vma of the section pointed to by the sh_link field. */
11123
11124static bfd_vma
11125elf_get_linked_section_vma (struct bfd_link_order *p)
11126{
11127 Elf_Internal_Shdr **elf_shdrp;
11128 asection *s;
11129 int elfsec;
11130
11131 s = p->u.indirect.section;
11132 elf_shdrp = elf_elfsections (s->owner);
11133 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11134 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11135 /* PR 290:
11136 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11137 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11138 sh_info fields. Hence we could get the situation
11139 where elfsec is 0. */
11140 if (elfsec == 0)
11141 {
11142 const struct elf_backend_data *bed
11143 = get_elf_backend_data (s->owner);
11144 if (bed->link_order_error_handler)
d003868e 11145 bed->link_order_error_handler
695344c0 11146 /* xgettext:c-format */
d003868e 11147 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11148 return 0;
11149 }
11150 else
11151 {
11152 s = elf_shdrp[elfsec]->bfd_section;
11153 return s->output_section->vma + s->output_offset;
11154 }
0b52efa6
PB
11155}
11156
11157
11158/* Compare two sections based on the locations of the sections they are
11159 linked to. Used by elf_fixup_link_order. */
11160
11161static int
11162compare_link_order (const void * a, const void * b)
11163{
11164 bfd_vma apos;
11165 bfd_vma bpos;
11166
11167 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11168 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11169 if (apos < bpos)
11170 return -1;
11171 return apos > bpos;
11172}
11173
11174
11175/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11176 order as their linked sections. Returns false if this could not be done
11177 because an output section includes both ordered and unordered
11178 sections. Ideally we'd do this in the linker proper. */
11179
11180static bfd_boolean
11181elf_fixup_link_order (bfd *abfd, asection *o)
11182{
11183 int seen_linkorder;
11184 int seen_other;
11185 int n;
11186 struct bfd_link_order *p;
11187 bfd *sub;
11188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11189 unsigned elfsec;
0b52efa6 11190 struct bfd_link_order **sections;
d33cdfe3 11191 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11192 bfd_vma offset;
3b36f7e6 11193
d33cdfe3
L
11194 other_sec = NULL;
11195 linkorder_sec = NULL;
0b52efa6
PB
11196 seen_other = 0;
11197 seen_linkorder = 0;
8423293d 11198 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11199 {
d33cdfe3 11200 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11201 {
11202 s = p->u.indirect.section;
d33cdfe3
L
11203 sub = s->owner;
11204 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11205 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11206 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11207 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11208 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11209 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11210 {
11211 seen_linkorder++;
11212 linkorder_sec = s;
11213 }
0b52efa6 11214 else
d33cdfe3
L
11215 {
11216 seen_other++;
11217 other_sec = s;
11218 }
0b52efa6
PB
11219 }
11220 else
11221 seen_other++;
d33cdfe3
L
11222
11223 if (seen_other && seen_linkorder)
11224 {
11225 if (other_sec && linkorder_sec)
4eca0228 11226 _bfd_error_handler
695344c0 11227 /* xgettext:c-format */
4eca0228
AM
11228 (_("%A has both ordered [`%A' in %B] "
11229 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11230 o, linkorder_sec, linkorder_sec->owner,
11231 other_sec, other_sec->owner);
d33cdfe3 11232 else
4eca0228
AM
11233 _bfd_error_handler
11234 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11235 bfd_set_error (bfd_error_bad_value);
11236 return FALSE;
11237 }
0b52efa6
PB
11238 }
11239
11240 if (!seen_linkorder)
11241 return TRUE;
11242
0b52efa6 11243 sections = (struct bfd_link_order **)
14b1c01e
AM
11244 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11245 if (sections == NULL)
11246 return FALSE;
0b52efa6 11247 seen_linkorder = 0;
3b36f7e6 11248
8423293d 11249 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11250 {
11251 sections[seen_linkorder++] = p;
11252 }
11253 /* Sort the input sections in the order of their linked section. */
11254 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11255 compare_link_order);
11256
11257 /* Change the offsets of the sections. */
11258 offset = 0;
11259 for (n = 0; n < seen_linkorder; n++)
11260 {
11261 s = sections[n]->u.indirect.section;
461686a3 11262 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11263 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11264 sections[n]->offset = offset;
11265 offset += sections[n]->size;
11266 }
11267
4dd07732 11268 free (sections);
0b52efa6
PB
11269 return TRUE;
11270}
11271
76359541
TP
11272/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11273 Returns TRUE upon success, FALSE otherwise. */
11274
11275static bfd_boolean
11276elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11277{
11278 bfd_boolean ret = FALSE;
11279 bfd *implib_bfd;
11280 const struct elf_backend_data *bed;
11281 flagword flags;
11282 enum bfd_architecture arch;
11283 unsigned int mach;
11284 asymbol **sympp = NULL;
11285 long symsize;
11286 long symcount;
11287 long src_count;
11288 elf_symbol_type *osymbuf;
11289
11290 implib_bfd = info->out_implib_bfd;
11291 bed = get_elf_backend_data (abfd);
11292
11293 if (!bfd_set_format (implib_bfd, bfd_object))
11294 return FALSE;
11295
046734ff 11296 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11297 flags = bfd_get_file_flags (abfd);
11298 flags &= ~HAS_RELOC;
11299 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11300 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11301 return FALSE;
11302
11303 /* Copy architecture of output file to import library file. */
11304 arch = bfd_get_arch (abfd);
11305 mach = bfd_get_mach (abfd);
11306 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11307 && (abfd->target_defaulted
11308 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11309 return FALSE;
11310
11311 /* Get symbol table size. */
11312 symsize = bfd_get_symtab_upper_bound (abfd);
11313 if (symsize < 0)
11314 return FALSE;
11315
11316 /* Read in the symbol table. */
11317 sympp = (asymbol **) xmalloc (symsize);
11318 symcount = bfd_canonicalize_symtab (abfd, sympp);
11319 if (symcount < 0)
11320 goto free_sym_buf;
11321
11322 /* Allow the BFD backend to copy any private header data it
11323 understands from the output BFD to the import library BFD. */
11324 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11325 goto free_sym_buf;
11326
11327 /* Filter symbols to appear in the import library. */
11328 if (bed->elf_backend_filter_implib_symbols)
11329 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11330 symcount);
11331 else
11332 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11333 if (symcount == 0)
11334 {
5df1bc57 11335 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11336 _bfd_error_handler (_("%B: no symbol found for import library"),
11337 implib_bfd);
76359541
TP
11338 goto free_sym_buf;
11339 }
11340
11341
11342 /* Make symbols absolute. */
11343 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11344 sizeof (*osymbuf));
11345 for (src_count = 0; src_count < symcount; src_count++)
11346 {
11347 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11348 sizeof (*osymbuf));
11349 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11350 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11351 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11352 osymbuf[src_count].internal_elf_sym.st_value =
11353 osymbuf[src_count].symbol.value;
11354 sympp[src_count] = &osymbuf[src_count].symbol;
11355 }
11356
11357 bfd_set_symtab (implib_bfd, sympp, symcount);
11358
11359 /* Allow the BFD backend to copy any private data it understands
11360 from the output BFD to the import library BFD. This is done last
11361 to permit the routine to look at the filtered symbol table. */
11362 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11363 goto free_sym_buf;
11364
11365 if (!bfd_close (implib_bfd))
11366 goto free_sym_buf;
11367
11368 ret = TRUE;
11369
11370free_sym_buf:
11371 free (sympp);
11372 return ret;
11373}
11374
9f7c3e5e
AM
11375static void
11376elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11377{
11378 asection *o;
11379
11380 if (flinfo->symstrtab != NULL)
ef10c3ac 11381 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11382 if (flinfo->contents != NULL)
11383 free (flinfo->contents);
11384 if (flinfo->external_relocs != NULL)
11385 free (flinfo->external_relocs);
11386 if (flinfo->internal_relocs != NULL)
11387 free (flinfo->internal_relocs);
11388 if (flinfo->external_syms != NULL)
11389 free (flinfo->external_syms);
11390 if (flinfo->locsym_shndx != NULL)
11391 free (flinfo->locsym_shndx);
11392 if (flinfo->internal_syms != NULL)
11393 free (flinfo->internal_syms);
11394 if (flinfo->indices != NULL)
11395 free (flinfo->indices);
11396 if (flinfo->sections != NULL)
11397 free (flinfo->sections);
9f7c3e5e
AM
11398 if (flinfo->symshndxbuf != NULL)
11399 free (flinfo->symshndxbuf);
11400 for (o = obfd->sections; o != NULL; o = o->next)
11401 {
11402 struct bfd_elf_section_data *esdo = elf_section_data (o);
11403 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11404 free (esdo->rel.hashes);
11405 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11406 free (esdo->rela.hashes);
11407 }
11408}
0b52efa6 11409
c152c796
AM
11410/* Do the final step of an ELF link. */
11411
11412bfd_boolean
11413bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11414{
11415 bfd_boolean dynamic;
11416 bfd_boolean emit_relocs;
11417 bfd *dynobj;
8b127cbc 11418 struct elf_final_link_info flinfo;
91d6fa6a
NC
11419 asection *o;
11420 struct bfd_link_order *p;
11421 bfd *sub;
c152c796
AM
11422 bfd_size_type max_contents_size;
11423 bfd_size_type max_external_reloc_size;
11424 bfd_size_type max_internal_reloc_count;
11425 bfd_size_type max_sym_count;
11426 bfd_size_type max_sym_shndx_count;
c152c796
AM
11427 Elf_Internal_Sym elfsym;
11428 unsigned int i;
11429 Elf_Internal_Shdr *symtab_hdr;
11430 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11431 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11432 struct elf_outext_info eoinfo;
11433 bfd_boolean merged;
11434 size_t relativecount = 0;
11435 asection *reldyn = 0;
11436 bfd_size_type amt;
104d59d1
JM
11437 asection *attr_section = NULL;
11438 bfd_vma attr_size = 0;
11439 const char *std_attrs_section;
64f52338 11440 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11441
64f52338 11442 if (!is_elf_hash_table (htab))
c152c796
AM
11443 return FALSE;
11444
0e1862bb 11445 if (bfd_link_pic (info))
c152c796
AM
11446 abfd->flags |= DYNAMIC;
11447
64f52338
AM
11448 dynamic = htab->dynamic_sections_created;
11449 dynobj = htab->dynobj;
c152c796 11450
0e1862bb 11451 emit_relocs = (bfd_link_relocatable (info)
a4676736 11452 || info->emitrelocations);
c152c796 11453
8b127cbc
AM
11454 flinfo.info = info;
11455 flinfo.output_bfd = abfd;
ef10c3ac 11456 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11457 if (flinfo.symstrtab == NULL)
c152c796
AM
11458 return FALSE;
11459
11460 if (! dynamic)
11461 {
8b127cbc
AM
11462 flinfo.hash_sec = NULL;
11463 flinfo.symver_sec = NULL;
c152c796
AM
11464 }
11465 else
11466 {
3d4d4302 11467 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11468 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11469 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11470 /* Note that it is OK if symver_sec is NULL. */
11471 }
11472
8b127cbc
AM
11473 flinfo.contents = NULL;
11474 flinfo.external_relocs = NULL;
11475 flinfo.internal_relocs = NULL;
11476 flinfo.external_syms = NULL;
11477 flinfo.locsym_shndx = NULL;
11478 flinfo.internal_syms = NULL;
11479 flinfo.indices = NULL;
11480 flinfo.sections = NULL;
8b127cbc 11481 flinfo.symshndxbuf = NULL;
ffbc01cc 11482 flinfo.filesym_count = 0;
c152c796 11483
104d59d1
JM
11484 /* The object attributes have been merged. Remove the input
11485 sections from the link, and set the contents of the output
11486 secton. */
11487 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11488 for (o = abfd->sections; o != NULL; o = o->next)
11489 {
11490 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11491 || strcmp (o->name, ".gnu.attributes") == 0)
11492 {
11493 for (p = o->map_head.link_order; p != NULL; p = p->next)
11494 {
11495 asection *input_section;
11496
11497 if (p->type != bfd_indirect_link_order)
11498 continue;
11499 input_section = p->u.indirect.section;
11500 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11501 elf_link_input_bfd ignores this section. */
11502 input_section->flags &= ~SEC_HAS_CONTENTS;
11503 }
a0c8462f 11504
104d59d1
JM
11505 attr_size = bfd_elf_obj_attr_size (abfd);
11506 if (attr_size)
11507 {
11508 bfd_set_section_size (abfd, o, attr_size);
11509 attr_section = o;
11510 /* Skip this section later on. */
11511 o->map_head.link_order = NULL;
11512 }
11513 else
11514 o->flags |= SEC_EXCLUDE;
11515 }
11516 }
11517
c152c796
AM
11518 /* Count up the number of relocations we will output for each output
11519 section, so that we know the sizes of the reloc sections. We
11520 also figure out some maximum sizes. */
11521 max_contents_size = 0;
11522 max_external_reloc_size = 0;
11523 max_internal_reloc_count = 0;
11524 max_sym_count = 0;
11525 max_sym_shndx_count = 0;
11526 merged = FALSE;
11527 for (o = abfd->sections; o != NULL; o = o->next)
11528 {
11529 struct bfd_elf_section_data *esdo = elf_section_data (o);
11530 o->reloc_count = 0;
11531
8423293d 11532 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11533 {
11534 unsigned int reloc_count = 0;
9eaff861 11535 unsigned int additional_reloc_count = 0;
c152c796 11536 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11537
11538 if (p->type == bfd_section_reloc_link_order
11539 || p->type == bfd_symbol_reloc_link_order)
11540 reloc_count = 1;
11541 else if (p->type == bfd_indirect_link_order)
11542 {
11543 asection *sec;
11544
11545 sec = p->u.indirect.section;
c152c796
AM
11546
11547 /* Mark all sections which are to be included in the
11548 link. This will normally be every section. We need
11549 to do this so that we can identify any sections which
11550 the linker has decided to not include. */
11551 sec->linker_mark = TRUE;
11552
11553 if (sec->flags & SEC_MERGE)
11554 merged = TRUE;
11555
eea6121a
AM
11556 if (sec->rawsize > max_contents_size)
11557 max_contents_size = sec->rawsize;
11558 if (sec->size > max_contents_size)
11559 max_contents_size = sec->size;
c152c796 11560
c152c796
AM
11561 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11562 && (sec->owner->flags & DYNAMIC) == 0)
11563 {
11564 size_t sym_count;
11565
a961cdd5
AM
11566 /* We are interested in just local symbols, not all
11567 symbols. */
c152c796
AM
11568 if (elf_bad_symtab (sec->owner))
11569 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11570 / bed->s->sizeof_sym);
11571 else
11572 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11573
11574 if (sym_count > max_sym_count)
11575 max_sym_count = sym_count;
11576
11577 if (sym_count > max_sym_shndx_count
6a40cf0c 11578 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11579 max_sym_shndx_count = sym_count;
11580
a961cdd5
AM
11581 if (esdo->this_hdr.sh_type == SHT_REL
11582 || esdo->this_hdr.sh_type == SHT_RELA)
11583 /* Some backends use reloc_count in relocation sections
11584 to count particular types of relocs. Of course,
11585 reloc sections themselves can't have relocations. */
11586 ;
11587 else if (emit_relocs)
11588 {
11589 reloc_count = sec->reloc_count;
11590 if (bed->elf_backend_count_additional_relocs)
11591 {
11592 int c;
11593 c = (*bed->elf_backend_count_additional_relocs) (sec);
11594 additional_reloc_count += c;
11595 }
11596 }
11597 else if (bed->elf_backend_count_relocs)
11598 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11599
11600 esdi = elf_section_data (sec);
11601
c152c796
AM
11602 if ((sec->flags & SEC_RELOC) != 0)
11603 {
d4730f92 11604 size_t ext_size = 0;
c152c796 11605
d4730f92
BS
11606 if (esdi->rel.hdr != NULL)
11607 ext_size = esdi->rel.hdr->sh_size;
11608 if (esdi->rela.hdr != NULL)
11609 ext_size += esdi->rela.hdr->sh_size;
7326c758 11610
c152c796
AM
11611 if (ext_size > max_external_reloc_size)
11612 max_external_reloc_size = ext_size;
11613 if (sec->reloc_count > max_internal_reloc_count)
11614 max_internal_reloc_count = sec->reloc_count;
11615 }
11616 }
11617 }
11618
11619 if (reloc_count == 0)
11620 continue;
11621
9eaff861 11622 reloc_count += additional_reloc_count;
c152c796
AM
11623 o->reloc_count += reloc_count;
11624
0e1862bb 11625 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11626 {
d4730f92 11627 if (esdi->rel.hdr)
9eaff861 11628 {
491d01d3 11629 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11630 esdo->rel.count += additional_reloc_count;
11631 }
d4730f92 11632 if (esdi->rela.hdr)
9eaff861 11633 {
491d01d3 11634 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11635 esdo->rela.count += additional_reloc_count;
11636 }
d4730f92
BS
11637 }
11638 else
11639 {
11640 if (o->use_rela_p)
11641 esdo->rela.count += reloc_count;
2c2b4ed4 11642 else
d4730f92 11643 esdo->rel.count += reloc_count;
c152c796 11644 }
c152c796
AM
11645 }
11646
9eaff861 11647 if (o->reloc_count > 0)
c152c796
AM
11648 o->flags |= SEC_RELOC;
11649 else
11650 {
11651 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11652 set it (this is probably a bug) and if it is set
11653 assign_section_numbers will create a reloc section. */
11654 o->flags &=~ SEC_RELOC;
11655 }
11656
11657 /* If the SEC_ALLOC flag is not set, force the section VMA to
11658 zero. This is done in elf_fake_sections as well, but forcing
11659 the VMA to 0 here will ensure that relocs against these
11660 sections are handled correctly. */
11661 if ((o->flags & SEC_ALLOC) == 0
11662 && ! o->user_set_vma)
11663 o->vma = 0;
11664 }
11665
0e1862bb 11666 if (! bfd_link_relocatable (info) && merged)
64f52338 11667 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11668
11669 /* Figure out the file positions for everything but the symbol table
11670 and the relocs. We set symcount to force assign_section_numbers
11671 to create a symbol table. */
8539e4e8 11672 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11673 BFD_ASSERT (! abfd->output_has_begun);
11674 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11675 goto error_return;
11676
ee75fd95 11677 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11678 for (o = abfd->sections; o != NULL; o = o->next)
11679 {
d4730f92 11680 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11681 if ((o->flags & SEC_RELOC) != 0)
11682 {
d4730f92 11683 if (esdo->rel.hdr
9eaff861 11684 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11685 goto error_return;
11686
d4730f92 11687 if (esdo->rela.hdr
9eaff861 11688 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11689 goto error_return;
11690 }
11691
11692 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11693 to count upwards while actually outputting the relocations. */
d4730f92
BS
11694 esdo->rel.count = 0;
11695 esdo->rela.count = 0;
0ce398f1
L
11696
11697 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11698 {
11699 /* Cache the section contents so that they can be compressed
11700 later. Use bfd_malloc since it will be freed by
11701 bfd_compress_section_contents. */
11702 unsigned char *contents = esdo->this_hdr.contents;
11703 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11704 abort ();
11705 contents
11706 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11707 if (contents == NULL)
11708 goto error_return;
11709 esdo->this_hdr.contents = contents;
11710 }
c152c796
AM
11711 }
11712
c152c796 11713 /* We have now assigned file positions for all the sections except
a485e98e
AM
11714 .symtab, .strtab, and non-loaded reloc sections. We start the
11715 .symtab section at the current file position, and write directly
11716 to it. We build the .strtab section in memory. */
c152c796
AM
11717 bfd_get_symcount (abfd) = 0;
11718 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11719 /* sh_name is set in prep_headers. */
11720 symtab_hdr->sh_type = SHT_SYMTAB;
11721 /* sh_flags, sh_addr and sh_size all start off zero. */
11722 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11723 /* sh_link is set in assign_section_numbers. */
11724 /* sh_info is set below. */
11725 /* sh_offset is set just below. */
72de5009 11726 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11727
ef10c3ac
L
11728 if (max_sym_count < 20)
11729 max_sym_count = 20;
64f52338 11730 htab->strtabsize = max_sym_count;
ef10c3ac 11731 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11732 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11733 if (htab->strtab == NULL)
c152c796 11734 goto error_return;
ef10c3ac
L
11735 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11736 flinfo.symshndxbuf
11737 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11738 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11739
8539e4e8 11740 if (info->strip != strip_all || emit_relocs)
c152c796 11741 {
8539e4e8
AM
11742 file_ptr off = elf_next_file_pos (abfd);
11743
11744 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11745
11746 /* Note that at this point elf_next_file_pos (abfd) is
11747 incorrect. We do not yet know the size of the .symtab section.
11748 We correct next_file_pos below, after we do know the size. */
11749
11750 /* Start writing out the symbol table. The first symbol is always a
11751 dummy symbol. */
c152c796
AM
11752 elfsym.st_value = 0;
11753 elfsym.st_size = 0;
11754 elfsym.st_info = 0;
11755 elfsym.st_other = 0;
11756 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11757 elfsym.st_target_internal = 0;
ef10c3ac
L
11758 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11759 bfd_und_section_ptr, NULL) != 1)
c152c796 11760 goto error_return;
c152c796 11761
8539e4e8
AM
11762 /* Output a symbol for each section. We output these even if we are
11763 discarding local symbols, since they are used for relocs. These
11764 symbols have no names. We store the index of each one in the
11765 index field of the section, so that we can find it again when
11766 outputting relocs. */
11767
c152c796
AM
11768 elfsym.st_size = 0;
11769 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11770 elfsym.st_other = 0;
f0b5bb34 11771 elfsym.st_value = 0;
35fc36a8 11772 elfsym.st_target_internal = 0;
c152c796
AM
11773 for (i = 1; i < elf_numsections (abfd); i++)
11774 {
11775 o = bfd_section_from_elf_index (abfd, i);
11776 if (o != NULL)
f0b5bb34
AM
11777 {
11778 o->target_index = bfd_get_symcount (abfd);
11779 elfsym.st_shndx = i;
0e1862bb 11780 if (!bfd_link_relocatable (info))
f0b5bb34 11781 elfsym.st_value = o->vma;
ef10c3ac
L
11782 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11783 NULL) != 1)
f0b5bb34
AM
11784 goto error_return;
11785 }
c152c796
AM
11786 }
11787 }
11788
11789 /* Allocate some memory to hold information read in from the input
11790 files. */
11791 if (max_contents_size != 0)
11792 {
8b127cbc
AM
11793 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11794 if (flinfo.contents == NULL)
c152c796
AM
11795 goto error_return;
11796 }
11797
11798 if (max_external_reloc_size != 0)
11799 {
8b127cbc
AM
11800 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11801 if (flinfo.external_relocs == NULL)
c152c796
AM
11802 goto error_return;
11803 }
11804
11805 if (max_internal_reloc_count != 0)
11806 {
056bafd4 11807 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
11808 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11809 if (flinfo.internal_relocs == NULL)
c152c796
AM
11810 goto error_return;
11811 }
11812
11813 if (max_sym_count != 0)
11814 {
11815 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11816 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11817 if (flinfo.external_syms == NULL)
c152c796
AM
11818 goto error_return;
11819
11820 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11821 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11822 if (flinfo.internal_syms == NULL)
c152c796
AM
11823 goto error_return;
11824
11825 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11826 flinfo.indices = (long int *) bfd_malloc (amt);
11827 if (flinfo.indices == NULL)
c152c796
AM
11828 goto error_return;
11829
11830 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11831 flinfo.sections = (asection **) bfd_malloc (amt);
11832 if (flinfo.sections == NULL)
c152c796
AM
11833 goto error_return;
11834 }
11835
11836 if (max_sym_shndx_count != 0)
11837 {
11838 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11839 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11840 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11841 goto error_return;
11842 }
11843
64f52338 11844 if (htab->tls_sec)
c152c796
AM
11845 {
11846 bfd_vma base, end = 0;
11847 asection *sec;
11848
64f52338 11849 for (sec = htab->tls_sec;
c152c796
AM
11850 sec && (sec->flags & SEC_THREAD_LOCAL);
11851 sec = sec->next)
11852 {
3a800eb9 11853 bfd_size_type size = sec->size;
c152c796 11854
3a800eb9
AM
11855 if (size == 0
11856 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11857 {
91d6fa6a
NC
11858 struct bfd_link_order *ord = sec->map_tail.link_order;
11859
11860 if (ord != NULL)
11861 size = ord->offset + ord->size;
c152c796
AM
11862 }
11863 end = sec->vma + size;
11864 }
64f52338 11865 base = htab->tls_sec->vma;
7dc98aea
RO
11866 /* Only align end of TLS section if static TLS doesn't have special
11867 alignment requirements. */
11868 if (bed->static_tls_alignment == 1)
64f52338
AM
11869 end = align_power (end, htab->tls_sec->alignment_power);
11870 htab->tls_size = end - base;
c152c796
AM
11871 }
11872
0b52efa6
PB
11873 /* Reorder SHF_LINK_ORDER sections. */
11874 for (o = abfd->sections; o != NULL; o = o->next)
11875 {
11876 if (!elf_fixup_link_order (abfd, o))
11877 return FALSE;
11878 }
11879
2f0c68f2
CM
11880 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11881 return FALSE;
11882
c152c796
AM
11883 /* Since ELF permits relocations to be against local symbols, we
11884 must have the local symbols available when we do the relocations.
11885 Since we would rather only read the local symbols once, and we
11886 would rather not keep them in memory, we handle all the
11887 relocations for a single input file at the same time.
11888
11889 Unfortunately, there is no way to know the total number of local
11890 symbols until we have seen all of them, and the local symbol
11891 indices precede the global symbol indices. This means that when
11892 we are generating relocatable output, and we see a reloc against
11893 a global symbol, we can not know the symbol index until we have
11894 finished examining all the local symbols to see which ones we are
11895 going to output. To deal with this, we keep the relocations in
11896 memory, and don't output them until the end of the link. This is
11897 an unfortunate waste of memory, but I don't see a good way around
11898 it. Fortunately, it only happens when performing a relocatable
11899 link, which is not the common case. FIXME: If keep_memory is set
11900 we could write the relocs out and then read them again; I don't
11901 know how bad the memory loss will be. */
11902
c72f2fb2 11903 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11904 sub->output_has_begun = FALSE;
11905 for (o = abfd->sections; o != NULL; o = o->next)
11906 {
8423293d 11907 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11908 {
11909 if (p->type == bfd_indirect_link_order
11910 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11911 == bfd_target_elf_flavour)
11912 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11913 {
11914 if (! sub->output_has_begun)
11915 {
8b127cbc 11916 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11917 goto error_return;
11918 sub->output_has_begun = TRUE;
11919 }
11920 }
11921 else if (p->type == bfd_section_reloc_link_order
11922 || p->type == bfd_symbol_reloc_link_order)
11923 {
11924 if (! elf_reloc_link_order (abfd, info, o, p))
11925 goto error_return;
11926 }
11927 else
11928 {
11929 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11930 {
11931 if (p->type == bfd_indirect_link_order
11932 && (bfd_get_flavour (sub)
11933 == bfd_target_elf_flavour)
11934 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11935 != bed->s->elfclass))
11936 {
11937 const char *iclass, *oclass;
11938
aebf9be7 11939 switch (bed->s->elfclass)
351f65ca 11940 {
aebf9be7
NC
11941 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11942 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11943 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11944 default: abort ();
351f65ca 11945 }
aebf9be7
NC
11946
11947 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11948 {
aebf9be7
NC
11949 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11950 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11951 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11952 default: abort ();
351f65ca
L
11953 }
11954
11955 bfd_set_error (bfd_error_wrong_format);
4eca0228 11956 _bfd_error_handler
695344c0 11957 /* xgettext:c-format */
351f65ca
L
11958 (_("%B: file class %s incompatible with %s"),
11959 sub, iclass, oclass);
11960 }
11961
11962 goto error_return;
11963 }
c152c796
AM
11964 }
11965 }
11966 }
11967
c0f00686
L
11968 /* Free symbol buffer if needed. */
11969 if (!info->reduce_memory_overheads)
11970 {
c72f2fb2 11971 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11972 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11973 && elf_tdata (sub)->symbuf)
c0f00686
L
11974 {
11975 free (elf_tdata (sub)->symbuf);
11976 elf_tdata (sub)->symbuf = NULL;
11977 }
11978 }
11979
c152c796
AM
11980 /* Output any global symbols that got converted to local in a
11981 version script or due to symbol visibility. We do this in a
11982 separate step since ELF requires all local symbols to appear
11983 prior to any global symbols. FIXME: We should only do this if
11984 some global symbols were, in fact, converted to become local.
11985 FIXME: Will this work correctly with the Irix 5 linker? */
11986 eoinfo.failed = FALSE;
8b127cbc 11987 eoinfo.flinfo = &flinfo;
c152c796 11988 eoinfo.localsyms = TRUE;
34a79995 11989 eoinfo.file_sym_done = FALSE;
7686d77d 11990 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11991 if (eoinfo.failed)
11992 return FALSE;
11993
4e617b1e
PB
11994 /* If backend needs to output some local symbols not present in the hash
11995 table, do it now. */
8539e4e8
AM
11996 if (bed->elf_backend_output_arch_local_syms
11997 && (info->strip != strip_all || emit_relocs))
4e617b1e 11998 {
6e0b88f1 11999 typedef int (*out_sym_func)
4e617b1e
PB
12000 (void *, const char *, Elf_Internal_Sym *, asection *,
12001 struct elf_link_hash_entry *);
12002
12003 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12004 (abfd, info, &flinfo,
12005 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12006 return FALSE;
12007 }
12008
c152c796
AM
12009 /* That wrote out all the local symbols. Finish up the symbol table
12010 with the global symbols. Even if we want to strip everything we
12011 can, we still need to deal with those global symbols that got
12012 converted to local in a version script. */
12013
12014 /* The sh_info field records the index of the first non local symbol. */
12015 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12016
12017 if (dynamic
64f52338
AM
12018 && htab->dynsym != NULL
12019 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12020 {
12021 Elf_Internal_Sym sym;
64f52338 12022 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12023
64f52338
AM
12024 o = htab->dynsym->output_section;
12025 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12026
12027 /* Write out the section symbols for the output sections. */
0e1862bb 12028 if (bfd_link_pic (info)
64f52338 12029 || htab->is_relocatable_executable)
c152c796
AM
12030 {
12031 asection *s;
12032
12033 sym.st_size = 0;
12034 sym.st_name = 0;
12035 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12036 sym.st_other = 0;
35fc36a8 12037 sym.st_target_internal = 0;
c152c796
AM
12038
12039 for (s = abfd->sections; s != NULL; s = s->next)
12040 {
12041 int indx;
12042 bfd_byte *dest;
12043 long dynindx;
12044
c152c796 12045 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12046 if (dynindx <= 0)
12047 continue;
12048 indx = elf_section_data (s)->this_idx;
c152c796
AM
12049 BFD_ASSERT (indx > 0);
12050 sym.st_shndx = indx;
c0d5a53d
L
12051 if (! check_dynsym (abfd, &sym))
12052 return FALSE;
c152c796
AM
12053 sym.st_value = s->vma;
12054 dest = dynsym + dynindx * bed->s->sizeof_sym;
12055 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12056 }
c152c796
AM
12057 }
12058
12059 /* Write out the local dynsyms. */
64f52338 12060 if (htab->dynlocal)
c152c796
AM
12061 {
12062 struct elf_link_local_dynamic_entry *e;
64f52338 12063 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12064 {
12065 asection *s;
12066 bfd_byte *dest;
12067
935bd1e0 12068 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12069 Note that we saved a word of storage and overwrote
12070 the original st_name with the dynstr_index. */
12071 sym = e->isym;
935bd1e0 12072 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12073
cb33740c
AM
12074 s = bfd_section_from_elf_index (e->input_bfd,
12075 e->isym.st_shndx);
12076 if (s != NULL)
c152c796 12077 {
c152c796
AM
12078 sym.st_shndx =
12079 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12080 if (! check_dynsym (abfd, &sym))
12081 return FALSE;
c152c796
AM
12082 sym.st_value = (s->output_section->vma
12083 + s->output_offset
12084 + e->isym.st_value);
12085 }
12086
c152c796
AM
12087 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12088 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12089 }
12090 }
c152c796
AM
12091 }
12092
12093 /* We get the global symbols from the hash table. */
12094 eoinfo.failed = FALSE;
12095 eoinfo.localsyms = FALSE;
8b127cbc 12096 eoinfo.flinfo = &flinfo;
7686d77d 12097 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12098 if (eoinfo.failed)
12099 return FALSE;
12100
12101 /* If backend needs to output some symbols not present in the hash
12102 table, do it now. */
8539e4e8
AM
12103 if (bed->elf_backend_output_arch_syms
12104 && (info->strip != strip_all || emit_relocs))
c152c796 12105 {
6e0b88f1 12106 typedef int (*out_sym_func)
c152c796
AM
12107 (void *, const char *, Elf_Internal_Sym *, asection *,
12108 struct elf_link_hash_entry *);
12109
12110 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12111 (abfd, info, &flinfo,
12112 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12113 return FALSE;
12114 }
12115
ef10c3ac
L
12116 /* Finalize the .strtab section. */
12117 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12118
12119 /* Swap out the .strtab section. */
12120 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12121 return FALSE;
12122
12123 /* Now we know the size of the symtab section. */
c152c796
AM
12124 if (bfd_get_symcount (abfd) > 0)
12125 {
ee3b52e9
L
12126 /* Finish up and write out the symbol string table (.strtab)
12127 section. */
ad32986f 12128 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12129 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12130
ad32986f 12131 if (elf_symtab_shndx_list (abfd))
8539e4e8 12132 {
ad32986f 12133 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12134
ad32986f
NC
12135 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12136 {
12137 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12138 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12139 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12140 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12141 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12142
ad32986f
NC
12143 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12144 off, TRUE);
12145
12146 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12147 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12148 return FALSE;
12149 }
8539e4e8 12150 }
ee3b52e9
L
12151
12152 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12153 /* sh_name was set in prep_headers. */
12154 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12155 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12156 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12157 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12158 symstrtab_hdr->sh_entsize = 0;
12159 symstrtab_hdr->sh_link = 0;
12160 symstrtab_hdr->sh_info = 0;
12161 /* sh_offset is set just below. */
12162 symstrtab_hdr->sh_addralign = 1;
12163
12164 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12165 off, TRUE);
12166 elf_next_file_pos (abfd) = off;
12167
c152c796 12168 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12169 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12170 return FALSE;
12171 }
12172
76359541
TP
12173 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12174 {
4eca0228
AM
12175 _bfd_error_handler (_("%B: failed to generate import library"),
12176 info->out_implib_bfd);
76359541
TP
12177 return FALSE;
12178 }
12179
c152c796
AM
12180 /* Adjust the relocs to have the correct symbol indices. */
12181 for (o = abfd->sections; o != NULL; o = o->next)
12182 {
d4730f92 12183 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12184 bfd_boolean sort;
10bbbc1d 12185
c152c796
AM
12186 if ((o->flags & SEC_RELOC) == 0)
12187 continue;
12188
28dbcedc 12189 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12190 if (esdo->rel.hdr != NULL
10bbbc1d 12191 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12192 return FALSE;
12193 if (esdo->rela.hdr != NULL
10bbbc1d 12194 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12195 return FALSE;
c152c796
AM
12196
12197 /* Set the reloc_count field to 0 to prevent write_relocs from
12198 trying to swap the relocs out itself. */
12199 o->reloc_count = 0;
12200 }
12201
12202 if (dynamic && info->combreloc && dynobj != NULL)
12203 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12204
12205 /* If we are linking against a dynamic object, or generating a
12206 shared library, finish up the dynamic linking information. */
12207 if (dynamic)
12208 {
12209 bfd_byte *dyncon, *dynconend;
12210
12211 /* Fix up .dynamic entries. */
3d4d4302 12212 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12213 BFD_ASSERT (o != NULL);
12214
12215 dyncon = o->contents;
eea6121a 12216 dynconend = o->contents + o->size;
c152c796
AM
12217 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12218 {
12219 Elf_Internal_Dyn dyn;
12220 const char *name;
12221 unsigned int type;
64487780
AM
12222 bfd_size_type sh_size;
12223 bfd_vma sh_addr;
c152c796
AM
12224
12225 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12226
12227 switch (dyn.d_tag)
12228 {
12229 default:
12230 continue;
12231 case DT_NULL:
12232 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12233 {
12234 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12235 {
12236 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12237 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12238 default: continue;
12239 }
12240 dyn.d_un.d_val = relativecount;
12241 relativecount = 0;
12242 break;
12243 }
12244 continue;
12245
12246 case DT_INIT:
12247 name = info->init_function;
12248 goto get_sym;
12249 case DT_FINI:
12250 name = info->fini_function;
12251 get_sym:
12252 {
12253 struct elf_link_hash_entry *h;
12254
64f52338 12255 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12256 if (h != NULL
12257 && (h->root.type == bfd_link_hash_defined
12258 || h->root.type == bfd_link_hash_defweak))
12259 {
bef26483 12260 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12261 o = h->root.u.def.section;
12262 if (o->output_section != NULL)
bef26483 12263 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12264 + o->output_offset);
12265 else
12266 {
12267 /* The symbol is imported from another shared
12268 library and does not apply to this one. */
bef26483 12269 dyn.d_un.d_ptr = 0;
c152c796
AM
12270 }
12271 break;
12272 }
12273 }
12274 continue;
12275
12276 case DT_PREINIT_ARRAYSZ:
12277 name = ".preinit_array";
4ade44b7 12278 goto get_out_size;
c152c796
AM
12279 case DT_INIT_ARRAYSZ:
12280 name = ".init_array";
4ade44b7 12281 goto get_out_size;
c152c796
AM
12282 case DT_FINI_ARRAYSZ:
12283 name = ".fini_array";
4ade44b7 12284 get_out_size:
c152c796
AM
12285 o = bfd_get_section_by_name (abfd, name);
12286 if (o == NULL)
12287 {
4eca0228 12288 _bfd_error_handler
4ade44b7 12289 (_("could not find section %s"), name);
c152c796
AM
12290 goto error_return;
12291 }
eea6121a 12292 if (o->size == 0)
4eca0228 12293 _bfd_error_handler
c152c796 12294 (_("warning: %s section has zero size"), name);
eea6121a 12295 dyn.d_un.d_val = o->size;
c152c796
AM
12296 break;
12297
12298 case DT_PREINIT_ARRAY:
12299 name = ".preinit_array";
4ade44b7 12300 goto get_out_vma;
c152c796
AM
12301 case DT_INIT_ARRAY:
12302 name = ".init_array";
4ade44b7 12303 goto get_out_vma;
c152c796
AM
12304 case DT_FINI_ARRAY:
12305 name = ".fini_array";
4ade44b7
AM
12306 get_out_vma:
12307 o = bfd_get_section_by_name (abfd, name);
12308 goto do_vma;
c152c796
AM
12309
12310 case DT_HASH:
12311 name = ".hash";
12312 goto get_vma;
fdc90cb4
JJ
12313 case DT_GNU_HASH:
12314 name = ".gnu.hash";
12315 goto get_vma;
c152c796
AM
12316 case DT_STRTAB:
12317 name = ".dynstr";
12318 goto get_vma;
12319 case DT_SYMTAB:
12320 name = ".dynsym";
12321 goto get_vma;
12322 case DT_VERDEF:
12323 name = ".gnu.version_d";
12324 goto get_vma;
12325 case DT_VERNEED:
12326 name = ".gnu.version_r";
12327 goto get_vma;
12328 case DT_VERSYM:
12329 name = ".gnu.version";
12330 get_vma:
4ade44b7
AM
12331 o = bfd_get_linker_section (dynobj, name);
12332 do_vma:
c152c796
AM
12333 if (o == NULL)
12334 {
4eca0228 12335 _bfd_error_handler
4ade44b7 12336 (_("could not find section %s"), name);
c152c796
AM
12337 goto error_return;
12338 }
894891db
NC
12339 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12340 {
4eca0228 12341 _bfd_error_handler
894891db
NC
12342 (_("warning: section '%s' is being made into a note"), name);
12343 bfd_set_error (bfd_error_nonrepresentable_section);
12344 goto error_return;
12345 }
4ade44b7 12346 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12347 break;
12348
12349 case DT_REL:
12350 case DT_RELA:
12351 case DT_RELSZ:
12352 case DT_RELASZ:
12353 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12354 type = SHT_REL;
12355 else
12356 type = SHT_RELA;
64487780
AM
12357 sh_size = 0;
12358 sh_addr = 0;
c152c796
AM
12359 for (i = 1; i < elf_numsections (abfd); i++)
12360 {
12361 Elf_Internal_Shdr *hdr;
12362
12363 hdr = elf_elfsections (abfd)[i];
12364 if (hdr->sh_type == type
12365 && (hdr->sh_flags & SHF_ALLOC) != 0)
12366 {
64487780
AM
12367 sh_size += hdr->sh_size;
12368 if (sh_addr == 0
12369 || sh_addr > hdr->sh_addr)
12370 sh_addr = hdr->sh_addr;
c152c796
AM
12371 }
12372 }
64487780 12373
64f52338
AM
12374 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12375 {
12376 /* Don't count procedure linkage table relocs in the
12377 overall reloc count. */
64487780
AM
12378 sh_size -= htab->srelplt->size;
12379 if (sh_size == 0)
12380 /* If the size is zero, make the address zero too.
12381 This is to avoid a glibc bug. If the backend
12382 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12383 zero, then we'll put DT_RELA at the end of
12384 DT_JMPREL. glibc will interpret the end of
12385 DT_RELA matching the end of DT_JMPREL as the
12386 case where DT_RELA includes DT_JMPREL, and for
12387 LD_BIND_NOW will decide that processing DT_RELA
12388 will process the PLT relocs too. Net result:
12389 No PLT relocs applied. */
12390 sh_addr = 0;
12391
64f52338
AM
12392 /* If .rela.plt is the first .rela section, exclude
12393 it from DT_RELA. */
64487780
AM
12394 else if (sh_addr == (htab->srelplt->output_section->vma
12395 + htab->srelplt->output_offset))
12396 sh_addr += htab->srelplt->size;
64f52338 12397 }
64487780
AM
12398
12399 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12400 dyn.d_un.d_val = sh_size;
12401 else
12402 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12403 break;
12404 }
12405 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12406 }
12407 }
12408
12409 /* If we have created any dynamic sections, then output them. */
12410 if (dynobj != NULL)
12411 {
12412 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12413 goto error_return;
12414
943284cc 12415 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12416 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12417 || info->error_textrel)
3d4d4302 12418 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12419 {
12420 bfd_byte *dyncon, *dynconend;
12421
943284cc
DJ
12422 dyncon = o->contents;
12423 dynconend = o->contents + o->size;
12424 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12425 {
12426 Elf_Internal_Dyn dyn;
12427
12428 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12429
12430 if (dyn.d_tag == DT_TEXTREL)
12431 {
c192a133
AM
12432 if (info->error_textrel)
12433 info->callbacks->einfo
12434 (_("%P%X: read-only segment has dynamic relocations.\n"));
12435 else
12436 info->callbacks->einfo
12437 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12438 break;
12439 }
12440 }
12441 }
12442
c152c796
AM
12443 for (o = dynobj->sections; o != NULL; o = o->next)
12444 {
12445 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12446 || o->size == 0
c152c796
AM
12447 || o->output_section == bfd_abs_section_ptr)
12448 continue;
12449 if ((o->flags & SEC_LINKER_CREATED) == 0)
12450 {
12451 /* At this point, we are only interested in sections
12452 created by _bfd_elf_link_create_dynamic_sections. */
12453 continue;
12454 }
64f52338 12455 if (htab->stab_info.stabstr == o)
3722b82f 12456 continue;
64f52338 12457 if (htab->eh_info.hdr_sec == o)
eea6121a 12458 continue;
3d4d4302 12459 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12460 {
12461 if (! bfd_set_section_contents (abfd, o->output_section,
12462 o->contents,
37b01f6a
DG
12463 (file_ptr) o->output_offset
12464 * bfd_octets_per_byte (abfd),
eea6121a 12465 o->size))
c152c796
AM
12466 goto error_return;
12467 }
12468 else
12469 {
12470 /* The contents of the .dynstr section are actually in a
12471 stringtab. */
8539e4e8
AM
12472 file_ptr off;
12473
c152c796
AM
12474 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12475 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12476 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12477 goto error_return;
12478 }
12479 }
12480 }
12481
7bdf4127 12482 if (!info->resolve_section_groups)
c152c796
AM
12483 {
12484 bfd_boolean failed = FALSE;
12485
7bdf4127 12486 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12487 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12488 if (failed)
12489 goto error_return;
12490 }
12491
12492 /* If we have optimized stabs strings, output them. */
64f52338 12493 if (htab->stab_info.stabstr != NULL)
c152c796 12494 {
64f52338 12495 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12496 goto error_return;
12497 }
12498
9f7c3e5e
AM
12499 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12500 goto error_return;
c152c796 12501
9f7c3e5e 12502 elf_final_link_free (abfd, &flinfo);
c152c796 12503
12bd6957 12504 elf_linker (abfd) = TRUE;
c152c796 12505
104d59d1
JM
12506 if (attr_section)
12507 {
a50b1753 12508 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12509 if (contents == NULL)
d0f16d5e 12510 return FALSE; /* Bail out and fail. */
104d59d1
JM
12511 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12512 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12513 free (contents);
12514 }
12515
c152c796
AM
12516 return TRUE;
12517
12518 error_return:
9f7c3e5e 12519 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12520 return FALSE;
12521}
12522\f
5241d853
RS
12523/* Initialize COOKIE for input bfd ABFD. */
12524
12525static bfd_boolean
12526init_reloc_cookie (struct elf_reloc_cookie *cookie,
12527 struct bfd_link_info *info, bfd *abfd)
12528{
12529 Elf_Internal_Shdr *symtab_hdr;
12530 const struct elf_backend_data *bed;
12531
12532 bed = get_elf_backend_data (abfd);
12533 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12534
12535 cookie->abfd = abfd;
12536 cookie->sym_hashes = elf_sym_hashes (abfd);
12537 cookie->bad_symtab = elf_bad_symtab (abfd);
12538 if (cookie->bad_symtab)
12539 {
12540 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12541 cookie->extsymoff = 0;
12542 }
12543 else
12544 {
12545 cookie->locsymcount = symtab_hdr->sh_info;
12546 cookie->extsymoff = symtab_hdr->sh_info;
12547 }
12548
12549 if (bed->s->arch_size == 32)
12550 cookie->r_sym_shift = 8;
12551 else
12552 cookie->r_sym_shift = 32;
12553
12554 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12555 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12556 {
12557 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12558 cookie->locsymcount, 0,
12559 NULL, NULL, NULL);
12560 if (cookie->locsyms == NULL)
12561 {
12562 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12563 return FALSE;
12564 }
12565 if (info->keep_memory)
12566 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12567 }
12568 return TRUE;
12569}
12570
12571/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12572
12573static void
12574fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12575{
12576 Elf_Internal_Shdr *symtab_hdr;
12577
12578 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12579 if (cookie->locsyms != NULL
12580 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12581 free (cookie->locsyms);
12582}
12583
12584/* Initialize the relocation information in COOKIE for input section SEC
12585 of input bfd ABFD. */
12586
12587static bfd_boolean
12588init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12589 struct bfd_link_info *info, bfd *abfd,
12590 asection *sec)
12591{
5241d853
RS
12592 if (sec->reloc_count == 0)
12593 {
12594 cookie->rels = NULL;
12595 cookie->relend = NULL;
12596 }
12597 else
12598 {
5241d853
RS
12599 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12600 info->keep_memory);
12601 if (cookie->rels == NULL)
12602 return FALSE;
12603 cookie->rel = cookie->rels;
056bafd4 12604 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12605 }
12606 cookie->rel = cookie->rels;
12607 return TRUE;
12608}
12609
12610/* Free the memory allocated by init_reloc_cookie_rels,
12611 if appropriate. */
12612
12613static void
12614fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12615 asection *sec)
12616{
12617 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12618 free (cookie->rels);
12619}
12620
12621/* Initialize the whole of COOKIE for input section SEC. */
12622
12623static bfd_boolean
12624init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12625 struct bfd_link_info *info,
12626 asection *sec)
12627{
12628 if (!init_reloc_cookie (cookie, info, sec->owner))
12629 goto error1;
12630 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12631 goto error2;
12632 return TRUE;
12633
12634 error2:
12635 fini_reloc_cookie (cookie, sec->owner);
12636 error1:
12637 return FALSE;
12638}
12639
12640/* Free the memory allocated by init_reloc_cookie_for_section,
12641 if appropriate. */
12642
12643static void
12644fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12645 asection *sec)
12646{
12647 fini_reloc_cookie_rels (cookie, sec);
12648 fini_reloc_cookie (cookie, sec->owner);
12649}
12650\f
c152c796
AM
12651/* Garbage collect unused sections. */
12652
07adf181
AM
12653/* Default gc_mark_hook. */
12654
12655asection *
12656_bfd_elf_gc_mark_hook (asection *sec,
12657 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12658 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12659 struct elf_link_hash_entry *h,
12660 Elf_Internal_Sym *sym)
12661{
12662 if (h != NULL)
12663 {
12664 switch (h->root.type)
12665 {
12666 case bfd_link_hash_defined:
12667 case bfd_link_hash_defweak:
12668 return h->root.u.def.section;
12669
12670 case bfd_link_hash_common:
12671 return h->root.u.c.p->section;
12672
12673 default:
12674 break;
12675 }
12676 }
12677 else
12678 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12679
12680 return NULL;
12681}
12682
b7c871ed
L
12683/* Return the global debug definition section. */
12684
12685static asection *
12686elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12687 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12688 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12689 struct elf_link_hash_entry *h,
12690 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12691{
12692 if (h != NULL
12693 && (h->root.type == bfd_link_hash_defined
12694 || h->root.type == bfd_link_hash_defweak)
12695 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12696 return h->root.u.def.section;
12697
12698 return NULL;
12699}
12700
5241d853
RS
12701/* COOKIE->rel describes a relocation against section SEC, which is
12702 a section we've decided to keep. Return the section that contains
12703 the relocation symbol, or NULL if no section contains it. */
12704
12705asection *
12706_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12707 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12708 struct elf_reloc_cookie *cookie,
12709 bfd_boolean *start_stop)
5241d853
RS
12710{
12711 unsigned long r_symndx;
12712 struct elf_link_hash_entry *h;
12713
12714 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12715 if (r_symndx == STN_UNDEF)
5241d853
RS
12716 return NULL;
12717
12718 if (r_symndx >= cookie->locsymcount
12719 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12720 {
12721 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12722 if (h == NULL)
12723 {
12724 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12725 sec->owner);
12726 return NULL;
12727 }
5241d853
RS
12728 while (h->root.type == bfd_link_hash_indirect
12729 || h->root.type == bfd_link_hash_warning)
12730 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12731 h->mark = 1;
4e6b54a6
AM
12732 /* If this symbol is weak and there is a non-weak definition, we
12733 keep the non-weak definition because many backends put
12734 dynamic reloc info on the non-weak definition for code
12735 handling copy relocs. */
12736 if (h->u.weakdef != NULL)
12737 h->u.weakdef->mark = 1;
1cce69b9 12738
a6a4679f 12739 if (start_stop != NULL)
1cce69b9
AM
12740 {
12741 /* To work around a glibc bug, mark all XXX input sections
12742 when there is an as yet undefined reference to __start_XXX
12743 or __stop_XXX symbols. The linker will later define such
12744 symbols for orphan input sections that have a name
12745 representable as a C identifier. */
cbd0eecf 12746 if (h->start_stop)
1cce69b9 12747 {
cbd0eecf 12748 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12749 *start_stop = !s->gc_mark;
12750 return s;
1cce69b9
AM
12751 }
12752 }
12753
5241d853
RS
12754 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12755 }
12756
12757 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12758 &cookie->locsyms[r_symndx]);
12759}
12760
12761/* COOKIE->rel describes a relocation against section SEC, which is
12762 a section we've decided to keep. Mark the section that contains
9d0a14d3 12763 the relocation symbol. */
5241d853
RS
12764
12765bfd_boolean
12766_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12767 asection *sec,
12768 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12769 struct elf_reloc_cookie *cookie)
5241d853
RS
12770{
12771 asection *rsec;
1cce69b9 12772 bfd_boolean start_stop = FALSE;
5241d853 12773
1cce69b9
AM
12774 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12775 while (rsec != NULL)
5241d853 12776 {
1cce69b9
AM
12777 if (!rsec->gc_mark)
12778 {
12779 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12780 || (rsec->owner->flags & DYNAMIC) != 0)
12781 rsec->gc_mark = 1;
12782 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12783 return FALSE;
12784 }
12785 if (!start_stop)
12786 break;
199af150 12787 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12788 }
12789 return TRUE;
12790}
12791
07adf181
AM
12792/* The mark phase of garbage collection. For a given section, mark
12793 it and any sections in this section's group, and all the sections
12794 which define symbols to which it refers. */
12795
ccfa59ea
AM
12796bfd_boolean
12797_bfd_elf_gc_mark (struct bfd_link_info *info,
12798 asection *sec,
6a5bb875 12799 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12800{
12801 bfd_boolean ret;
9d0a14d3 12802 asection *group_sec, *eh_frame;
c152c796
AM
12803
12804 sec->gc_mark = 1;
12805
12806 /* Mark all the sections in the group. */
12807 group_sec = elf_section_data (sec)->next_in_group;
12808 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12809 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12810 return FALSE;
12811
12812 /* Look through the section relocs. */
12813 ret = TRUE;
9d0a14d3
RS
12814 eh_frame = elf_eh_frame_section (sec->owner);
12815 if ((sec->flags & SEC_RELOC) != 0
12816 && sec->reloc_count > 0
12817 && sec != eh_frame)
c152c796 12818 {
5241d853 12819 struct elf_reloc_cookie cookie;
c152c796 12820
5241d853
RS
12821 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12822 ret = FALSE;
c152c796 12823 else
c152c796 12824 {
5241d853 12825 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12826 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12827 {
12828 ret = FALSE;
12829 break;
12830 }
12831 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12832 }
12833 }
9d0a14d3
RS
12834
12835 if (ret && eh_frame && elf_fde_list (sec))
12836 {
12837 struct elf_reloc_cookie cookie;
12838
12839 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12840 ret = FALSE;
12841 else
12842 {
12843 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12844 gc_mark_hook, &cookie))
12845 ret = FALSE;
12846 fini_reloc_cookie_for_section (&cookie, eh_frame);
12847 }
12848 }
12849
2f0c68f2
CM
12850 eh_frame = elf_section_eh_frame_entry (sec);
12851 if (ret && eh_frame && !eh_frame->gc_mark)
12852 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12853 ret = FALSE;
12854
c152c796
AM
12855 return ret;
12856}
12857
3c758495
TG
12858/* Scan and mark sections in a special or debug section group. */
12859
12860static void
12861_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12862{
12863 /* Point to first section of section group. */
12864 asection *ssec;
12865 /* Used to iterate the section group. */
12866 asection *msec;
12867
12868 bfd_boolean is_special_grp = TRUE;
12869 bfd_boolean is_debug_grp = TRUE;
12870
12871 /* First scan to see if group contains any section other than debug
12872 and special section. */
12873 ssec = msec = elf_next_in_group (grp);
12874 do
12875 {
12876 if ((msec->flags & SEC_DEBUGGING) == 0)
12877 is_debug_grp = FALSE;
12878
12879 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12880 is_special_grp = FALSE;
12881
12882 msec = elf_next_in_group (msec);
12883 }
12884 while (msec != ssec);
12885
12886 /* If this is a pure debug section group or pure special section group,
12887 keep all sections in this group. */
12888 if (is_debug_grp || is_special_grp)
12889 {
12890 do
12891 {
12892 msec->gc_mark = 1;
12893 msec = elf_next_in_group (msec);
12894 }
12895 while (msec != ssec);
12896 }
12897}
12898
7f6ab9f8
AM
12899/* Keep debug and special sections. */
12900
12901bfd_boolean
12902_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12903 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12904{
12905 bfd *ibfd;
12906
c72f2fb2 12907 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12908 {
12909 asection *isec;
12910 bfd_boolean some_kept;
b40bf0a2 12911 bfd_boolean debug_frag_seen;
b7c871ed 12912 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
12913
12914 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12915 continue;
12916
b40bf0a2
NC
12917 /* Ensure all linker created sections are kept,
12918 see if any other section is already marked,
12919 and note if we have any fragmented debug sections. */
b7c871ed 12920 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
12921 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12922 {
12923 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12924 isec->gc_mark = 1;
eb026f09
AM
12925 else if (isec->gc_mark
12926 && (isec->flags & SEC_ALLOC) != 0
12927 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 12928 some_kept = TRUE;
b40bf0a2 12929
535b785f 12930 if (!debug_frag_seen
b40bf0a2
NC
12931 && (isec->flags & SEC_DEBUGGING)
12932 && CONST_STRNEQ (isec->name, ".debug_line."))
12933 debug_frag_seen = TRUE;
7f6ab9f8
AM
12934 }
12935
eb026f09
AM
12936 /* If no non-note alloc section in this file will be kept, then
12937 we can toss out the debug and special sections. */
7f6ab9f8
AM
12938 if (!some_kept)
12939 continue;
12940
12941 /* Keep debug and special sections like .comment when they are
3c758495
TG
12942 not part of a group. Also keep section groups that contain
12943 just debug sections or special sections. */
7f6ab9f8 12944 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12945 {
12946 if ((isec->flags & SEC_GROUP) != 0)
12947 _bfd_elf_gc_mark_debug_special_section_group (isec);
12948 else if (((isec->flags & SEC_DEBUGGING) != 0
12949 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12950 && elf_next_in_group (isec) == NULL)
12951 isec->gc_mark = 1;
b7c871ed
L
12952 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12953 has_kept_debug_info = TRUE;
3c758495 12954 }
b40bf0a2 12955
b40bf0a2
NC
12956 /* Look for CODE sections which are going to be discarded,
12957 and find and discard any fragmented debug sections which
12958 are associated with that code section. */
b7c871ed
L
12959 if (debug_frag_seen)
12960 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12961 if ((isec->flags & SEC_CODE) != 0
12962 && isec->gc_mark == 0)
12963 {
12964 unsigned int ilen;
12965 asection *dsec;
b40bf0a2 12966
b7c871ed 12967 ilen = strlen (isec->name);
b40bf0a2 12968
b7c871ed
L
12969 /* Association is determined by the name of the debug
12970 section containing the name of the code section as
12971 a suffix. For example .debug_line.text.foo is a
12972 debug section associated with .text.foo. */
12973 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12974 {
12975 unsigned int dlen;
b40bf0a2 12976
b7c871ed
L
12977 if (dsec->gc_mark == 0
12978 || (dsec->flags & SEC_DEBUGGING) == 0)
12979 continue;
b40bf0a2 12980
b7c871ed 12981 dlen = strlen (dsec->name);
b40bf0a2 12982
b7c871ed
L
12983 if (dlen > ilen
12984 && strncmp (dsec->name + (dlen - ilen),
12985 isec->name, ilen) == 0)
b40bf0a2 12986 dsec->gc_mark = 0;
b7c871ed 12987 }
b40bf0a2 12988 }
b7c871ed
L
12989
12990 /* Mark debug sections referenced by kept debug sections. */
12991 if (has_kept_debug_info)
12992 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12993 if (isec->gc_mark
12994 && (isec->flags & SEC_DEBUGGING) != 0)
12995 if (!_bfd_elf_gc_mark (info, isec,
12996 elf_gc_mark_debug_section))
12997 return FALSE;
7f6ab9f8
AM
12998 }
12999 return TRUE;
13000}
13001
c152c796
AM
13002/* The sweep phase of garbage collection. Remove all garbage sections. */
13003
13004typedef bfd_boolean (*gc_sweep_hook_fn)
13005 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
13006
13007static bfd_boolean
ccabcbe5 13008elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13009{
13010 bfd *sub;
ccabcbe5
AM
13011 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13012 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 13013
c72f2fb2 13014 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13015 {
13016 asection *o;
13017
b19a8f85
L
13018 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13019 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13020 continue;
13021
13022 for (o = sub->sections; o != NULL; o = o->next)
13023 {
a33dafc3
L
13024 /* When any section in a section group is kept, we keep all
13025 sections in the section group. If the first member of
13026 the section group is excluded, we will also exclude the
13027 group section. */
13028 if (o->flags & SEC_GROUP)
13029 {
13030 asection *first = elf_next_in_group (o);
13031 o->gc_mark = first->gc_mark;
13032 }
c152c796 13033
1e7eae0d 13034 if (o->gc_mark)
c152c796
AM
13035 continue;
13036
13037 /* Skip sweeping sections already excluded. */
13038 if (o->flags & SEC_EXCLUDE)
13039 continue;
13040
13041 /* Since this is early in the link process, it is simple
13042 to remove a section from the output. */
13043 o->flags |= SEC_EXCLUDE;
13044
c55fe096 13045 if (info->print_gc_sections && o->size != 0)
695344c0 13046 /* xgettext:c-format */
c08bb8dd
AM
13047 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13048 o, sub);
c17d87de 13049
c152c796
AM
13050 /* But we also have to update some of the relocation
13051 info we collected before. */
13052 if (gc_sweep_hook
e8aaee2a 13053 && (o->flags & SEC_RELOC) != 0
9850436d
AM
13054 && o->reloc_count != 0
13055 && !((info->strip == strip_all || info->strip == strip_debugger)
13056 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 13057 && !bfd_is_abs_section (o->output_section))
c152c796
AM
13058 {
13059 Elf_Internal_Rela *internal_relocs;
13060 bfd_boolean r;
13061
13062 internal_relocs
13063 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13064 info->keep_memory);
13065 if (internal_relocs == NULL)
13066 return FALSE;
13067
13068 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13069
13070 if (elf_section_data (o)->relocs != internal_relocs)
13071 free (internal_relocs);
13072
13073 if (!r)
13074 return FALSE;
13075 }
13076 }
13077 }
13078
c152c796
AM
13079 return TRUE;
13080}
13081
13082/* Propagate collected vtable information. This is called through
13083 elf_link_hash_traverse. */
13084
13085static bfd_boolean
13086elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13087{
c152c796 13088 /* Those that are not vtables. */
cbd0eecf
L
13089 if (h->start_stop
13090 || h->u2.vtable == NULL
13091 || h->u2.vtable->parent == NULL)
c152c796
AM
13092 return TRUE;
13093
13094 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13095 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13096 return TRUE;
13097
13098 /* If we've already been done, exit. */
cbd0eecf 13099 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13100 return TRUE;
13101
13102 /* Make sure the parent's table is up to date. */
cbd0eecf 13103 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13104
cbd0eecf 13105 if (h->u2.vtable->used == NULL)
c152c796
AM
13106 {
13107 /* None of this table's entries were referenced. Re-use the
13108 parent's table. */
cbd0eecf
L
13109 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13110 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13111 }
13112 else
13113 {
13114 size_t n;
13115 bfd_boolean *cu, *pu;
13116
13117 /* Or the parent's entries into ours. */
cbd0eecf 13118 cu = h->u2.vtable->used;
c152c796 13119 cu[-1] = TRUE;
cbd0eecf 13120 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13121 if (pu != NULL)
13122 {
13123 const struct elf_backend_data *bed;
13124 unsigned int log_file_align;
13125
13126 bed = get_elf_backend_data (h->root.u.def.section->owner);
13127 log_file_align = bed->s->log_file_align;
cbd0eecf 13128 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13129 while (n--)
13130 {
13131 if (*pu)
13132 *cu = TRUE;
13133 pu++;
13134 cu++;
13135 }
13136 }
13137 }
13138
13139 return TRUE;
13140}
13141
13142static bfd_boolean
13143elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13144{
13145 asection *sec;
13146 bfd_vma hstart, hend;
13147 Elf_Internal_Rela *relstart, *relend, *rel;
13148 const struct elf_backend_data *bed;
13149 unsigned int log_file_align;
13150
c152c796
AM
13151 /* Take care of both those symbols that do not describe vtables as
13152 well as those that are not loaded. */
cbd0eecf
L
13153 if (h->start_stop
13154 || h->u2.vtable == NULL
13155 || h->u2.vtable->parent == NULL)
c152c796
AM
13156 return TRUE;
13157
13158 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13159 || h->root.type == bfd_link_hash_defweak);
13160
13161 sec = h->root.u.def.section;
13162 hstart = h->root.u.def.value;
13163 hend = hstart + h->size;
13164
13165 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13166 if (!relstart)
13167 return *(bfd_boolean *) okp = FALSE;
13168 bed = get_elf_backend_data (sec->owner);
13169 log_file_align = bed->s->log_file_align;
13170
056bafd4 13171 relend = relstart + sec->reloc_count;
c152c796
AM
13172
13173 for (rel = relstart; rel < relend; ++rel)
13174 if (rel->r_offset >= hstart && rel->r_offset < hend)
13175 {
13176 /* If the entry is in use, do nothing. */
cbd0eecf
L
13177 if (h->u2.vtable->used
13178 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13179 {
13180 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13181 if (h->u2.vtable->used[entry])
c152c796
AM
13182 continue;
13183 }
13184 /* Otherwise, kill it. */
13185 rel->r_offset = rel->r_info = rel->r_addend = 0;
13186 }
13187
13188 return TRUE;
13189}
13190
87538722
AM
13191/* Mark sections containing dynamically referenced symbols. When
13192 building shared libraries, we must assume that any visible symbol is
13193 referenced. */
715df9b8 13194
64d03ab5
AM
13195bfd_boolean
13196bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13197{
87538722 13198 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13199 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13200
715df9b8
EB
13201 if ((h->root.type == bfd_link_hash_defined
13202 || h->root.type == bfd_link_hash_defweak)
87538722 13203 && (h->ref_dynamic
c4621b33 13204 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13205 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13206 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13207 && (!bfd_link_executable (info)
22185505 13208 || info->gc_keep_exported
b407645f
AM
13209 || info->export_dynamic
13210 || (h->dynamic
13211 && d != NULL
13212 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13213 && (h->versioned >= versioned
54e8959c
L
13214 || !bfd_hide_sym_by_version (info->version_info,
13215 h->root.root.string)))))
715df9b8
EB
13216 h->root.u.def.section->flags |= SEC_KEEP;
13217
13218 return TRUE;
13219}
3b36f7e6 13220
74f0fb50
AM
13221/* Keep all sections containing symbols undefined on the command-line,
13222 and the section containing the entry symbol. */
13223
13224void
13225_bfd_elf_gc_keep (struct bfd_link_info *info)
13226{
13227 struct bfd_sym_chain *sym;
13228
13229 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13230 {
13231 struct elf_link_hash_entry *h;
13232
13233 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13234 FALSE, FALSE, FALSE);
13235
13236 if (h != NULL
13237 && (h->root.type == bfd_link_hash_defined
13238 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13239 && !bfd_is_abs_section (h->root.u.def.section)
13240 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13241 h->root.u.def.section->flags |= SEC_KEEP;
13242 }
13243}
13244
2f0c68f2
CM
13245bfd_boolean
13246bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13247 struct bfd_link_info *info)
13248{
13249 bfd *ibfd = info->input_bfds;
13250
13251 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13252 {
13253 asection *sec;
13254 struct elf_reloc_cookie cookie;
13255
13256 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13257 continue;
13258
13259 if (!init_reloc_cookie (&cookie, info, ibfd))
13260 return FALSE;
13261
13262 for (sec = ibfd->sections; sec; sec = sec->next)
13263 {
13264 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13265 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13266 {
13267 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13268 fini_reloc_cookie_rels (&cookie, sec);
13269 }
13270 }
13271 }
13272 return TRUE;
13273}
13274
c152c796
AM
13275/* Do mark and sweep of unused sections. */
13276
13277bfd_boolean
13278bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13279{
13280 bfd_boolean ok = TRUE;
13281 bfd *sub;
6a5bb875 13282 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13283 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13284 struct elf_link_hash_table *htab;
c152c796 13285
64d03ab5 13286 if (!bed->can_gc_sections
715df9b8 13287 || !is_elf_hash_table (info->hash))
c152c796 13288 {
4eca0228 13289 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13290 return TRUE;
13291 }
13292
74f0fb50 13293 bed->gc_keep (info);
da44f4e5 13294 htab = elf_hash_table (info);
74f0fb50 13295
9d0a14d3
RS
13296 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13297 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13298 for (sub = info->input_bfds;
13299 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13300 sub = sub->link.next)
9d0a14d3
RS
13301 {
13302 asection *sec;
13303 struct elf_reloc_cookie cookie;
13304
13305 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13306 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13307 {
13308 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13309 if (elf_section_data (sec)->sec_info
13310 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13311 elf_eh_frame_section (sub) = sec;
13312 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13313 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13314 }
13315 }
9d0a14d3 13316
c152c796 13317 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13318 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13319 if (!ok)
13320 return FALSE;
13321
13322 /* Kill the vtable relocations that were not used. */
da44f4e5 13323 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13324 if (!ok)
13325 return FALSE;
13326
715df9b8 13327 /* Mark dynamically referenced symbols. */
22185505 13328 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13329 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13330
715df9b8 13331 /* Grovel through relocs to find out who stays ... */
64d03ab5 13332 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13333 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13334 {
13335 asection *o;
13336
b19a8f85
L
13337 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13338 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13339 continue;
13340
7f6ab9f8
AM
13341 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13342 Also treat note sections as a root, if the section is not part
13343 of a group. */
c152c796 13344 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13345 if (!o->gc_mark
13346 && (o->flags & SEC_EXCLUDE) == 0
24007750 13347 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13348 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13349 && elf_next_in_group (o) == NULL )))
13350 {
13351 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13352 return FALSE;
13353 }
c152c796
AM
13354 }
13355
6a5bb875 13356 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13357 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13358
c152c796 13359 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13360 return elf_gc_sweep (abfd, info);
c152c796
AM
13361}
13362\f
13363/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13364
13365bfd_boolean
13366bfd_elf_gc_record_vtinherit (bfd *abfd,
13367 asection *sec,
13368 struct elf_link_hash_entry *h,
13369 bfd_vma offset)
13370{
13371 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13372 struct elf_link_hash_entry **search, *child;
ef53be89 13373 size_t extsymcount;
c152c796
AM
13374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13375
13376 /* The sh_info field of the symtab header tells us where the
13377 external symbols start. We don't care about the local symbols at
13378 this point. */
13379 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13380 if (!elf_bad_symtab (abfd))
13381 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13382
13383 sym_hashes = elf_sym_hashes (abfd);
13384 sym_hashes_end = sym_hashes + extsymcount;
13385
13386 /* Hunt down the child symbol, which is in this section at the same
13387 offset as the relocation. */
13388 for (search = sym_hashes; search != sym_hashes_end; ++search)
13389 {
13390 if ((child = *search) != NULL
13391 && (child->root.type == bfd_link_hash_defined
13392 || child->root.type == bfd_link_hash_defweak)
13393 && child->root.u.def.section == sec
13394 && child->root.u.def.value == offset)
13395 goto win;
13396 }
13397
695344c0
NC
13398 /* xgettext:c-format */
13399 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13400 abfd, sec, (unsigned long) offset);
c152c796
AM
13401 bfd_set_error (bfd_error_invalid_operation);
13402 return FALSE;
13403
13404 win:
cbd0eecf 13405 if (!child->u2.vtable)
f6e332e6 13406 {
cbd0eecf
L
13407 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13408 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13409 if (!child->u2.vtable)
f6e332e6
AM
13410 return FALSE;
13411 }
c152c796
AM
13412 if (!h)
13413 {
13414 /* This *should* only be the absolute section. It could potentially
13415 be that someone has defined a non-global vtable though, which
13416 would be bad. It isn't worth paging in the local symbols to be
13417 sure though; that case should simply be handled by the assembler. */
13418
cbd0eecf 13419 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13420 }
13421 else
cbd0eecf 13422 child->u2.vtable->parent = h;
c152c796
AM
13423
13424 return TRUE;
13425}
13426
13427/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13428
13429bfd_boolean
13430bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13431 asection *sec ATTRIBUTE_UNUSED,
13432 struct elf_link_hash_entry *h,
13433 bfd_vma addend)
13434{
13435 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13436 unsigned int log_file_align = bed->s->log_file_align;
13437
cbd0eecf 13438 if (!h->u2.vtable)
f6e332e6 13439 {
cbd0eecf
L
13440 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13441 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13442 if (!h->u2.vtable)
f6e332e6
AM
13443 return FALSE;
13444 }
13445
cbd0eecf 13446 if (addend >= h->u2.vtable->size)
c152c796
AM
13447 {
13448 size_t size, bytes, file_align;
cbd0eecf 13449 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13450
13451 /* While the symbol is undefined, we have to be prepared to handle
13452 a zero size. */
13453 file_align = 1 << log_file_align;
13454 if (h->root.type == bfd_link_hash_undefined)
13455 size = addend + file_align;
13456 else
13457 {
13458 size = h->size;
13459 if (addend >= size)
13460 {
13461 /* Oops! We've got a reference past the defined end of
13462 the table. This is probably a bug -- shall we warn? */
13463 size = addend + file_align;
13464 }
13465 }
13466 size = (size + file_align - 1) & -file_align;
13467
13468 /* Allocate one extra entry for use as a "done" flag for the
13469 consolidation pass. */
13470 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13471
13472 if (ptr)
13473 {
a50b1753 13474 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13475
13476 if (ptr != NULL)
13477 {
13478 size_t oldbytes;
13479
cbd0eecf 13480 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13481 * sizeof (bfd_boolean));
13482 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13483 }
13484 }
13485 else
a50b1753 13486 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13487
13488 if (ptr == NULL)
13489 return FALSE;
13490
13491 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13492 h->u2.vtable->used = ptr + 1;
13493 h->u2.vtable->size = size;
c152c796
AM
13494 }
13495
cbd0eecf 13496 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13497
13498 return TRUE;
13499}
13500
ae17ab41
CM
13501/* Map an ELF section header flag to its corresponding string. */
13502typedef struct
13503{
13504 char *flag_name;
13505 flagword flag_value;
13506} elf_flags_to_name_table;
13507
13508static elf_flags_to_name_table elf_flags_to_names [] =
13509{
13510 { "SHF_WRITE", SHF_WRITE },
13511 { "SHF_ALLOC", SHF_ALLOC },
13512 { "SHF_EXECINSTR", SHF_EXECINSTR },
13513 { "SHF_MERGE", SHF_MERGE },
13514 { "SHF_STRINGS", SHF_STRINGS },
13515 { "SHF_INFO_LINK", SHF_INFO_LINK},
13516 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13517 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13518 { "SHF_GROUP", SHF_GROUP },
13519 { "SHF_TLS", SHF_TLS },
13520 { "SHF_MASKOS", SHF_MASKOS },
13521 { "SHF_EXCLUDE", SHF_EXCLUDE },
13522};
13523
b9c361e0
JL
13524/* Returns TRUE if the section is to be included, otherwise FALSE. */
13525bfd_boolean
ae17ab41 13526bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13527 struct flag_info *flaginfo,
b9c361e0 13528 asection *section)
ae17ab41 13529{
8b127cbc 13530 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13531
8b127cbc 13532 if (!flaginfo->flags_initialized)
ae17ab41 13533 {
8b127cbc
AM
13534 bfd *obfd = info->output_bfd;
13535 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13536 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13537 int with_hex = 0;
13538 int without_hex = 0;
13539
8b127cbc 13540 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13541 {
b9c361e0 13542 unsigned i;
8b127cbc 13543 flagword (*lookup) (char *);
ae17ab41 13544
8b127cbc
AM
13545 lookup = bed->elf_backend_lookup_section_flags_hook;
13546 if (lookup != NULL)
ae17ab41 13547 {
8b127cbc 13548 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13549
13550 if (hexval != 0)
13551 {
13552 if (tf->with == with_flags)
13553 with_hex |= hexval;
13554 else if (tf->with == without_flags)
13555 without_hex |= hexval;
13556 tf->valid = TRUE;
13557 continue;
13558 }
ae17ab41 13559 }
8b127cbc 13560 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13561 {
8b127cbc 13562 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13563 {
13564 if (tf->with == with_flags)
13565 with_hex |= elf_flags_to_names[i].flag_value;
13566 else if (tf->with == without_flags)
13567 without_hex |= elf_flags_to_names[i].flag_value;
13568 tf->valid = TRUE;
13569 break;
13570 }
13571 }
8b127cbc 13572 if (!tf->valid)
b9c361e0 13573 {
68ffbac6 13574 info->callbacks->einfo
8b127cbc 13575 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13576 return FALSE;
ae17ab41
CM
13577 }
13578 }
8b127cbc
AM
13579 flaginfo->flags_initialized = TRUE;
13580 flaginfo->only_with_flags |= with_hex;
13581 flaginfo->not_with_flags |= without_hex;
ae17ab41 13582 }
ae17ab41 13583
8b127cbc 13584 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13585 return FALSE;
13586
8b127cbc 13587 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13588 return FALSE;
13589
13590 return TRUE;
ae17ab41
CM
13591}
13592
c152c796
AM
13593struct alloc_got_off_arg {
13594 bfd_vma gotoff;
10455f89 13595 struct bfd_link_info *info;
c152c796
AM
13596};
13597
13598/* We need a special top-level link routine to convert got reference counts
13599 to real got offsets. */
13600
13601static bfd_boolean
13602elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13603{
a50b1753 13604 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13605 bfd *obfd = gofarg->info->output_bfd;
13606 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13607
c152c796
AM
13608 if (h->got.refcount > 0)
13609 {
13610 h->got.offset = gofarg->gotoff;
10455f89 13611 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13612 }
13613 else
13614 h->got.offset = (bfd_vma) -1;
13615
13616 return TRUE;
13617}
13618
13619/* And an accompanying bit to work out final got entry offsets once
13620 we're done. Should be called from final_link. */
13621
13622bfd_boolean
13623bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13624 struct bfd_link_info *info)
13625{
13626 bfd *i;
13627 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13628 bfd_vma gotoff;
c152c796
AM
13629 struct alloc_got_off_arg gofarg;
13630
10455f89
HPN
13631 BFD_ASSERT (abfd == info->output_bfd);
13632
c152c796
AM
13633 if (! is_elf_hash_table (info->hash))
13634 return FALSE;
13635
13636 /* The GOT offset is relative to the .got section, but the GOT header is
13637 put into the .got.plt section, if the backend uses it. */
13638 if (bed->want_got_plt)
13639 gotoff = 0;
13640 else
13641 gotoff = bed->got_header_size;
13642
13643 /* Do the local .got entries first. */
c72f2fb2 13644 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13645 {
13646 bfd_signed_vma *local_got;
ef53be89 13647 size_t j, locsymcount;
c152c796
AM
13648 Elf_Internal_Shdr *symtab_hdr;
13649
13650 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13651 continue;
13652
13653 local_got = elf_local_got_refcounts (i);
13654 if (!local_got)
13655 continue;
13656
13657 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13658 if (elf_bad_symtab (i))
13659 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13660 else
13661 locsymcount = symtab_hdr->sh_info;
13662
13663 for (j = 0; j < locsymcount; ++j)
13664 {
13665 if (local_got[j] > 0)
13666 {
13667 local_got[j] = gotoff;
10455f89 13668 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13669 }
13670 else
13671 local_got[j] = (bfd_vma) -1;
13672 }
13673 }
13674
13675 /* Then the global .got entries. .plt refcounts are handled by
13676 adjust_dynamic_symbol */
13677 gofarg.gotoff = gotoff;
10455f89 13678 gofarg.info = info;
c152c796
AM
13679 elf_link_hash_traverse (elf_hash_table (info),
13680 elf_gc_allocate_got_offsets,
13681 &gofarg);
13682 return TRUE;
13683}
13684
13685/* Many folk need no more in the way of final link than this, once
13686 got entry reference counting is enabled. */
13687
13688bfd_boolean
13689bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13690{
13691 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13692 return FALSE;
13693
13694 /* Invoke the regular ELF backend linker to do all the work. */
13695 return bfd_elf_final_link (abfd, info);
13696}
13697
13698bfd_boolean
13699bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13700{
a50b1753 13701 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13702
13703 if (rcookie->bad_symtab)
13704 rcookie->rel = rcookie->rels;
13705
13706 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13707 {
13708 unsigned long r_symndx;
13709
13710 if (! rcookie->bad_symtab)
13711 if (rcookie->rel->r_offset > offset)
13712 return FALSE;
13713 if (rcookie->rel->r_offset != offset)
13714 continue;
13715
13716 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13717 if (r_symndx == STN_UNDEF)
c152c796
AM
13718 return TRUE;
13719
13720 if (r_symndx >= rcookie->locsymcount
13721 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13722 {
13723 struct elf_link_hash_entry *h;
13724
13725 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13726
13727 while (h->root.type == bfd_link_hash_indirect
13728 || h->root.type == bfd_link_hash_warning)
13729 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13730
13731 if ((h->root.type == bfd_link_hash_defined
13732 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13733 && (h->root.u.def.section->owner != rcookie->abfd
13734 || h->root.u.def.section->kept_section != NULL
13735 || discarded_section (h->root.u.def.section)))
c152c796 13736 return TRUE;
c152c796
AM
13737 }
13738 else
13739 {
13740 /* It's not a relocation against a global symbol,
13741 but it could be a relocation against a local
13742 symbol for a discarded section. */
13743 asection *isec;
13744 Elf_Internal_Sym *isym;
13745
13746 /* Need to: get the symbol; get the section. */
13747 isym = &rcookie->locsyms[r_symndx];
cb33740c 13748 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13749 if (isec != NULL
13750 && (isec->kept_section != NULL
13751 || discarded_section (isec)))
cb33740c 13752 return TRUE;
c152c796
AM
13753 }
13754 return FALSE;
13755 }
13756 return FALSE;
13757}
13758
13759/* Discard unneeded references to discarded sections.
75938853
AM
13760 Returns -1 on error, 1 if any section's size was changed, 0 if
13761 nothing changed. This function assumes that the relocations are in
13762 sorted order, which is true for all known assemblers. */
c152c796 13763
75938853 13764int
c152c796
AM
13765bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13766{
13767 struct elf_reloc_cookie cookie;
18cd5bce 13768 asection *o;
c152c796 13769 bfd *abfd;
75938853 13770 int changed = 0;
c152c796
AM
13771
13772 if (info->traditional_format
13773 || !is_elf_hash_table (info->hash))
75938853 13774 return 0;
c152c796 13775
18cd5bce
AM
13776 o = bfd_get_section_by_name (output_bfd, ".stab");
13777 if (o != NULL)
c152c796 13778 {
18cd5bce 13779 asection *i;
c152c796 13780
18cd5bce 13781 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13782 {
18cd5bce
AM
13783 if (i->size == 0
13784 || i->reloc_count == 0
13785 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13786 continue;
c152c796 13787
18cd5bce
AM
13788 abfd = i->owner;
13789 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13790 continue;
c152c796 13791
18cd5bce 13792 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13793 return -1;
c152c796 13794
18cd5bce
AM
13795 if (_bfd_discard_section_stabs (abfd, i,
13796 elf_section_data (i)->sec_info,
5241d853
RS
13797 bfd_elf_reloc_symbol_deleted_p,
13798 &cookie))
75938853 13799 changed = 1;
18cd5bce
AM
13800
13801 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13802 }
18cd5bce
AM
13803 }
13804
2f0c68f2
CM
13805 o = NULL;
13806 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13807 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13808 if (o != NULL)
13809 {
13810 asection *i;
d7153c4a 13811 int eh_changed = 0;
c152c796 13812
18cd5bce 13813 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13814 {
18cd5bce
AM
13815 if (i->size == 0)
13816 continue;
13817
13818 abfd = i->owner;
13819 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13820 continue;
13821
13822 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13823 return -1;
18cd5bce
AM
13824
13825 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13826 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13827 bfd_elf_reloc_symbol_deleted_p,
13828 &cookie))
d7153c4a
AM
13829 {
13830 eh_changed = 1;
13831 if (i->size != i->rawsize)
13832 changed = 1;
13833 }
18cd5bce
AM
13834
13835 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13836 }
d7153c4a
AM
13837 if (eh_changed)
13838 elf_link_hash_traverse (elf_hash_table (info),
13839 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13840 }
c152c796 13841
18cd5bce
AM
13842 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13843 {
13844 const struct elf_backend_data *bed;
c152c796 13845
18cd5bce
AM
13846 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13847 continue;
13848
13849 bed = get_elf_backend_data (abfd);
13850
13851 if (bed->elf_backend_discard_info != NULL)
13852 {
13853 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13854 return -1;
18cd5bce
AM
13855
13856 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13857 changed = 1;
18cd5bce
AM
13858
13859 fini_reloc_cookie (&cookie, abfd);
13860 }
c152c796
AM
13861 }
13862
2f0c68f2
CM
13863 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13864 _bfd_elf_end_eh_frame_parsing (info);
13865
13866 if (info->eh_frame_hdr_type
0e1862bb 13867 && !bfd_link_relocatable (info)
c152c796 13868 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13869 changed = 1;
c152c796 13870
75938853 13871 return changed;
c152c796 13872}
082b7297 13873
43e1669b 13874bfd_boolean
0c511000 13875_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13876 asection *sec,
c0f00686 13877 struct bfd_link_info *info)
082b7297
L
13878{
13879 flagword flags;
c77ec726 13880 const char *name, *key;
082b7297
L
13881 struct bfd_section_already_linked *l;
13882 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13883
c77ec726
AM
13884 if (sec->output_section == bfd_abs_section_ptr)
13885 return FALSE;
0c511000 13886
c77ec726 13887 flags = sec->flags;
0c511000 13888
c77ec726
AM
13889 /* Return if it isn't a linkonce section. A comdat group section
13890 also has SEC_LINK_ONCE set. */
13891 if ((flags & SEC_LINK_ONCE) == 0)
13892 return FALSE;
0c511000 13893
c77ec726
AM
13894 /* Don't put group member sections on our list of already linked
13895 sections. They are handled as a group via their group section. */
13896 if (elf_sec_group (sec) != NULL)
13897 return FALSE;
0c511000 13898
c77ec726
AM
13899 /* For a SHT_GROUP section, use the group signature as the key. */
13900 name = sec->name;
13901 if ((flags & SEC_GROUP) != 0
13902 && elf_next_in_group (sec) != NULL
13903 && elf_group_name (elf_next_in_group (sec)) != NULL)
13904 key = elf_group_name (elf_next_in_group (sec));
13905 else
13906 {
13907 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13908 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13909 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13910 key++;
0c511000 13911 else
c77ec726
AM
13912 /* Must be a user linkonce section that doesn't follow gcc's
13913 naming convention. In this case we won't be matching
13914 single member groups. */
13915 key = name;
0c511000 13916 }
6d2cd210 13917
c77ec726 13918 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13919
13920 for (l = already_linked_list->entry; l != NULL; l = l->next)
13921 {
c2370991 13922 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13923 sections with a signature of <key> (<key> is some string),
13924 and linkonce sections named .gnu.linkonce.<type>.<key>.
13925 Match like sections. LTO plugin sections are an exception.
13926 They are always named .gnu.linkonce.t.<key> and match either
13927 type of section. */
13928 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13929 && ((flags & SEC_GROUP) != 0
13930 || strcmp (name, l->sec->name) == 0))
13931 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13932 {
13933 /* The section has already been linked. See if we should
6d2cd210 13934 issue a warning. */
c77ec726
AM
13935 if (!_bfd_handle_already_linked (sec, l, info))
13936 return FALSE;
082b7297 13937
c77ec726 13938 if (flags & SEC_GROUP)
3d7f7666 13939 {
c77ec726
AM
13940 asection *first = elf_next_in_group (sec);
13941 asection *s = first;
3d7f7666 13942
c77ec726 13943 while (s != NULL)
3d7f7666 13944 {
c77ec726
AM
13945 s->output_section = bfd_abs_section_ptr;
13946 /* Record which group discards it. */
13947 s->kept_section = l->sec;
13948 s = elf_next_in_group (s);
13949 /* These lists are circular. */
13950 if (s == first)
13951 break;
3d7f7666
L
13952 }
13953 }
082b7297 13954
43e1669b 13955 return TRUE;
082b7297
L
13956 }
13957 }
13958
c77ec726
AM
13959 /* A single member comdat group section may be discarded by a
13960 linkonce section and vice versa. */
13961 if ((flags & SEC_GROUP) != 0)
3d7f7666 13962 {
c77ec726 13963 asection *first = elf_next_in_group (sec);
c2370991 13964
c77ec726
AM
13965 if (first != NULL && elf_next_in_group (first) == first)
13966 /* Check this single member group against linkonce sections. */
13967 for (l = already_linked_list->entry; l != NULL; l = l->next)
13968 if ((l->sec->flags & SEC_GROUP) == 0
13969 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13970 {
13971 first->output_section = bfd_abs_section_ptr;
13972 first->kept_section = l->sec;
13973 sec->output_section = bfd_abs_section_ptr;
13974 break;
13975 }
13976 }
13977 else
13978 /* Check this linkonce section against single member groups. */
13979 for (l = already_linked_list->entry; l != NULL; l = l->next)
13980 if (l->sec->flags & SEC_GROUP)
6d2cd210 13981 {
c77ec726 13982 asection *first = elf_next_in_group (l->sec);
6d2cd210 13983
c77ec726
AM
13984 if (first != NULL
13985 && elf_next_in_group (first) == first
13986 && bfd_elf_match_symbols_in_sections (first, sec, info))
13987 {
13988 sec->output_section = bfd_abs_section_ptr;
13989 sec->kept_section = first;
13990 break;
13991 }
6d2cd210 13992 }
0c511000 13993
c77ec726
AM
13994 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13995 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13996 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13997 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13998 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13999 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14000 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14001 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14002 The reverse order cannot happen as there is never a bfd with only the
14003 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14004 matter as here were are looking only for cross-bfd sections. */
14005
14006 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14007 for (l = already_linked_list->entry; l != NULL; l = l->next)
14008 if ((l->sec->flags & SEC_GROUP) == 0
14009 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14010 {
14011 if (abfd != l->sec->owner)
14012 sec->output_section = bfd_abs_section_ptr;
14013 break;
14014 }
80c29487 14015
082b7297 14016 /* This is the first section with this name. Record it. */
c77ec726 14017 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14018 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14019 return sec->output_section == bfd_abs_section_ptr;
082b7297 14020}
81e1b023 14021
a4d8e49b
L
14022bfd_boolean
14023_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14024{
14025 return sym->st_shndx == SHN_COMMON;
14026}
14027
14028unsigned int
14029_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14030{
14031 return SHN_COMMON;
14032}
14033
14034asection *
14035_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14036{
14037 return bfd_com_section_ptr;
14038}
10455f89
HPN
14039
14040bfd_vma
14041_bfd_elf_default_got_elt_size (bfd *abfd,
14042 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14043 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14044 bfd *ibfd ATTRIBUTE_UNUSED,
14045 unsigned long symndx ATTRIBUTE_UNUSED)
14046{
14047 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14048 return bed->s->arch_size / 8;
14049}
83bac4b0
NC
14050
14051/* Routines to support the creation of dynamic relocs. */
14052
83bac4b0
NC
14053/* Returns the name of the dynamic reloc section associated with SEC. */
14054
14055static const char *
14056get_dynamic_reloc_section_name (bfd * abfd,
14057 asection * sec,
14058 bfd_boolean is_rela)
14059{
ddcf1fcf
BS
14060 char *name;
14061 const char *old_name = bfd_get_section_name (NULL, sec);
14062 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14063
ddcf1fcf 14064 if (old_name == NULL)
83bac4b0
NC
14065 return NULL;
14066
ddcf1fcf 14067 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14068 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14069
14070 return name;
14071}
14072
14073/* Returns the dynamic reloc section associated with SEC.
14074 If necessary compute the name of the dynamic reloc section based
14075 on SEC's name (looked up in ABFD's string table) and the setting
14076 of IS_RELA. */
14077
14078asection *
14079_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14080 asection * sec,
14081 bfd_boolean is_rela)
14082{
14083 asection * reloc_sec = elf_section_data (sec)->sreloc;
14084
14085 if (reloc_sec == NULL)
14086 {
14087 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14088
14089 if (name != NULL)
14090 {
3d4d4302 14091 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14092
14093 if (reloc_sec != NULL)
14094 elf_section_data (sec)->sreloc = reloc_sec;
14095 }
14096 }
14097
14098 return reloc_sec;
14099}
14100
14101/* Returns the dynamic reloc section associated with SEC. If the
14102 section does not exist it is created and attached to the DYNOBJ
14103 bfd and stored in the SRELOC field of SEC's elf_section_data
14104 structure.
f8076f98 14105
83bac4b0
NC
14106 ALIGNMENT is the alignment for the newly created section and
14107 IS_RELA defines whether the name should be .rela.<SEC's name>
14108 or .rel.<SEC's name>. The section name is looked up in the
14109 string table associated with ABFD. */
14110
14111asection *
ca4be51c
AM
14112_bfd_elf_make_dynamic_reloc_section (asection *sec,
14113 bfd *dynobj,
14114 unsigned int alignment,
14115 bfd *abfd,
14116 bfd_boolean is_rela)
83bac4b0
NC
14117{
14118 asection * reloc_sec = elf_section_data (sec)->sreloc;
14119
14120 if (reloc_sec == NULL)
14121 {
14122 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14123
14124 if (name == NULL)
14125 return NULL;
14126
3d4d4302 14127 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14128
14129 if (reloc_sec == NULL)
14130 {
3d4d4302
AM
14131 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14132 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14133 if ((sec->flags & SEC_ALLOC) != 0)
14134 flags |= SEC_ALLOC | SEC_LOAD;
14135
3d4d4302 14136 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14137 if (reloc_sec != NULL)
14138 {
8877b5e5
AM
14139 /* _bfd_elf_get_sec_type_attr chooses a section type by
14140 name. Override as it may be wrong, eg. for a user
14141 section named "auto" we'll get ".relauto" which is
14142 seen to be a .rela section. */
14143 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14144 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14145 reloc_sec = NULL;
14146 }
14147 }
14148
14149 elf_section_data (sec)->sreloc = reloc_sec;
14150 }
14151
14152 return reloc_sec;
14153}
1338dd10 14154
bffebb6b
AM
14155/* Copy the ELF symbol type and other attributes for a linker script
14156 assignment from HSRC to HDEST. Generally this should be treated as
14157 if we found a strong non-dynamic definition for HDEST (except that
14158 ld ignores multiple definition errors). */
1338dd10 14159void
bffebb6b
AM
14160_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14161 struct bfd_link_hash_entry *hdest,
14162 struct bfd_link_hash_entry *hsrc)
1338dd10 14163{
bffebb6b
AM
14164 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14165 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14166 Elf_Internal_Sym isym;
1338dd10
PB
14167
14168 ehdest->type = ehsrc->type;
35fc36a8 14169 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14170
14171 isym.st_other = ehsrc->other;
b8417128 14172 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14173}
351f65ca
L
14174
14175/* Append a RELA relocation REL to section S in BFD. */
14176
14177void
14178elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14179{
14180 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14181 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14182 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14183 bed->s->swap_reloca_out (abfd, rel, loc);
14184}
14185
14186/* Append a REL relocation REL to section S in BFD. */
14187
14188void
14189elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14190{
14191 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14192 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14193 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14194 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14195}