Remove has_bnd_reloc from elf_x86_64_link_hash_entry
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
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
40b36307
L
583 && h->root.type == bfd_link_hash_new
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
00cbee0a 626 switch (h->root.type)
77cfaee6 627 {
00cbee0a
L
628 case bfd_link_hash_defined:
629 case bfd_link_hash_defweak:
630 case bfd_link_hash_common:
631 break;
632 case bfd_link_hash_undefweak:
633 case bfd_link_hash_undefined:
634 /* Since we're defining the symbol, don't let it seem to have not
635 been defined. record_dynamic_symbol and size_dynamic_sections
636 may depend on this. */
4ea42fb7 637 h->root.type = bfd_link_hash_new;
77cfaee6
AM
638 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
639 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
640 break;
641 case bfd_link_hash_new:
40b36307 642 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 643 h->non_elf = 0;
00cbee0a
L
644 break;
645 case bfd_link_hash_indirect:
646 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 647 the versioned symbol point to this one. */
00cbee0a
L
648 bed = get_elf_backend_data (output_bfd);
649 hv = h;
650 while (hv->root.type == bfd_link_hash_indirect
651 || hv->root.type == bfd_link_hash_warning)
652 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
653 /* We don't need to update h->root.u since linker will set them
654 later. */
655 h->root.type = bfd_link_hash_undefined;
656 hv->root.type = bfd_link_hash_indirect;
657 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
658 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
659 break;
8e2a4f11
AM
660 default:
661 BFD_FAIL ();
c2596ca5 662 return FALSE;
55255dae 663 }
45d6a902
AM
664
665 /* If this symbol is being provided by the linker script, and it is
666 currently defined by a dynamic object, but not by a regular
667 object, then mark it as undefined so that the generic linker will
668 force the correct value. */
669 if (provide
f5385ebf
AM
670 && h->def_dynamic
671 && !h->def_regular)
45d6a902
AM
672 h->root.type = bfd_link_hash_undefined;
673
674 /* If this symbol is not being provided by the linker script, and it is
675 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
676 then clear out any version information because the symbol will not be
677 associated with the dynamic object any more. */
45d6a902 678 if (!provide
f5385ebf
AM
679 && h->def_dynamic
680 && !h->def_regular)
b531344c
MR
681 h->verinfo.verdef = NULL;
682
683 /* Make sure this symbol is not garbage collected. */
684 h->mark = 1;
45d6a902 685
f5385ebf 686 h->def_regular = 1;
45d6a902 687
eb8476a6 688 if (hidden)
fe21a8fc 689 {
91d6fa6a 690 bed = get_elf_backend_data (output_bfd);
b8297068
AM
691 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
692 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
694 }
695
6fa3860b
PB
696 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
697 and executables. */
0e1862bb 698 if (!bfd_link_relocatable (info)
6fa3860b
PB
699 && h->dynindx != -1
700 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
701 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
702 h->forced_local = 1;
703
f5385ebf
AM
704 if ((h->def_dynamic
705 || h->ref_dynamic
6b3b0ab8
L
706 || bfd_link_dll (info)
707 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
708 && h->dynindx == -1)
709 {
c152c796 710 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
711 return FALSE;
712
713 /* If this is a weak defined symbol, and we know a corresponding
714 real symbol from the same dynamic object, make sure the real
715 symbol is also made into a dynamic symbol. */
f6e332e6
AM
716 if (h->u.weakdef != NULL
717 && h->u.weakdef->dynindx == -1)
45d6a902 718 {
f6e332e6 719 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
720 return FALSE;
721 }
722 }
723
724 return TRUE;
725}
42751cf3 726
8c58d23b
AM
727/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
728 success, and 2 on a failure caused by attempting to record a symbol
729 in a discarded section, eg. a discarded link-once section symbol. */
730
731int
c152c796
AM
732bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
733 bfd *input_bfd,
734 long input_indx)
8c58d23b
AM
735{
736 bfd_size_type amt;
737 struct elf_link_local_dynamic_entry *entry;
738 struct elf_link_hash_table *eht;
739 struct elf_strtab_hash *dynstr;
ef53be89 740 size_t dynstr_index;
8c58d23b
AM
741 char *name;
742 Elf_External_Sym_Shndx eshndx;
743 char esym[sizeof (Elf64_External_Sym)];
744
0eddce27 745 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
746 return 0;
747
748 /* See if the entry exists already. */
749 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
750 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
751 return 1;
752
753 amt = sizeof (*entry);
a50b1753 754 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
755 if (entry == NULL)
756 return 0;
757
758 /* Go find the symbol, so that we can find it's name. */
759 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 760 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
761 {
762 bfd_release (input_bfd, entry);
763 return 0;
764 }
765
766 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 767 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
768 {
769 asection *s;
770
771 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
772 if (s == NULL || bfd_is_abs_section (s->output_section))
773 {
774 /* We can still bfd_release here as nothing has done another
775 bfd_alloc. We can't do this later in this function. */
776 bfd_release (input_bfd, entry);
777 return 2;
778 }
779 }
780
781 name = (bfd_elf_string_from_elf_section
782 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
783 entry->isym.st_name));
784
785 dynstr = elf_hash_table (info)->dynstr;
786 if (dynstr == NULL)
787 {
788 /* Create a strtab to hold the dynamic symbol names. */
789 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
790 if (dynstr == NULL)
791 return 0;
792 }
793
b34976b6 794 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 795 if (dynstr_index == (size_t) -1)
8c58d23b
AM
796 return 0;
797 entry->isym.st_name = dynstr_index;
798
799 eht = elf_hash_table (info);
800
801 entry->next = eht->dynlocal;
802 eht->dynlocal = entry;
803 entry->input_bfd = input_bfd;
804 entry->input_indx = input_indx;
805 eht->dynsymcount++;
806
807 /* Whatever binding the symbol had before, it's now local. */
808 entry->isym.st_info
809 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
810
811 /* The dynindx will be set at the end of size_dynamic_sections. */
812
813 return 1;
814}
815
30b30c21 816/* Return the dynindex of a local dynamic symbol. */
42751cf3 817
30b30c21 818long
268b6b39
AM
819_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
820 bfd *input_bfd,
821 long input_indx)
30b30c21
RH
822{
823 struct elf_link_local_dynamic_entry *e;
824
825 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
826 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
827 return e->dynindx;
828 return -1;
829}
830
831/* This function is used to renumber the dynamic symbols, if some of
832 them are removed because they are marked as local. This is called
833 via elf_link_hash_traverse. */
834
b34976b6 835static bfd_boolean
268b6b39
AM
836elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
837 void *data)
42751cf3 838{
a50b1753 839 size_t *count = (size_t *) data;
30b30c21 840
6fa3860b
PB
841 if (h->forced_local)
842 return TRUE;
843
844 if (h->dynindx != -1)
845 h->dynindx = ++(*count);
846
847 return TRUE;
848}
849
850
851/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
852 STB_LOCAL binding. */
853
854static bfd_boolean
855elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
856 void *data)
857{
a50b1753 858 size_t *count = (size_t *) data;
6fa3860b 859
6fa3860b
PB
860 if (!h->forced_local)
861 return TRUE;
862
42751cf3 863 if (h->dynindx != -1)
30b30c21
RH
864 h->dynindx = ++(*count);
865
b34976b6 866 return TRUE;
42751cf3 867}
30b30c21 868
aee6f5b4
AO
869/* Return true if the dynamic symbol for a given section should be
870 omitted when creating a shared library. */
871bfd_boolean
872_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
873 struct bfd_link_info *info,
874 asection *p)
875{
74541ad4 876 struct elf_link_hash_table *htab;
ca55926c 877 asection *ip;
74541ad4 878
aee6f5b4
AO
879 switch (elf_section_data (p)->this_hdr.sh_type)
880 {
881 case SHT_PROGBITS:
882 case SHT_NOBITS:
883 /* If sh_type is yet undecided, assume it could be
884 SHT_PROGBITS/SHT_NOBITS. */
885 case SHT_NULL:
74541ad4
AM
886 htab = elf_hash_table (info);
887 if (p == htab->tls_sec)
888 return FALSE;
889
890 if (htab->text_index_section != NULL)
891 return p != htab->text_index_section && p != htab->data_index_section;
892
ca55926c 893 return (htab->dynobj != NULL
3d4d4302 894 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 895 && ip->output_section == p);
aee6f5b4
AO
896
897 /* There shouldn't be section relative relocations
898 against any other section. */
899 default:
900 return TRUE;
901 }
902}
903
062e2358 904/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
905 symbol for each output section, which come first. Next come symbols
906 which have been forced to local binding. Then all of the back-end
907 allocated local dynamic syms, followed by the rest of the global
908 symbols. */
30b30c21 909
554220db
AM
910static unsigned long
911_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
912 struct bfd_link_info *info,
913 unsigned long *section_sym_count)
30b30c21
RH
914{
915 unsigned long dynsymcount = 0;
916
0e1862bb
L
917 if (bfd_link_pic (info)
918 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 919 {
aee6f5b4 920 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
921 asection *p;
922 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 923 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
924 && (p->flags & SEC_ALLOC) != 0
925 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
926 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
927 else
928 elf_section_data (p)->dynindx = 0;
30b30c21 929 }
554220db 930 *section_sym_count = dynsymcount;
30b30c21 931
6fa3860b
PB
932 elf_link_hash_traverse (elf_hash_table (info),
933 elf_link_renumber_local_hash_table_dynsyms,
934 &dynsymcount);
935
30b30c21
RH
936 if (elf_hash_table (info)->dynlocal)
937 {
938 struct elf_link_local_dynamic_entry *p;
939 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
940 p->dynindx = ++dynsymcount;
941 }
90ac2420 942 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
943
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_hash_table_dynsyms,
946 &dynsymcount);
947
d5486c43
L
948 /* There is an unused NULL entry at the head of the table which we
949 must account for in our count even if the table is empty since it
950 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
951 .dynamic section. */
952 dynsymcount++;
30b30c21 953
ccabcbe5
AM
954 elf_hash_table (info)->dynsymcount = dynsymcount;
955 return dynsymcount;
30b30c21 956}
252b5132 957
54ac0771
L
958/* Merge st_other field. */
959
960static void
961elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 962 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 963 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
964{
965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
966
967 /* If st_other has a processor-specific meaning, specific
cd3416da 968 code might be needed here. */
54ac0771
L
969 if (bed->elf_backend_merge_symbol_attribute)
970 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
971 dynamic);
972
cd3416da 973 if (!dynamic)
54ac0771 974 {
cd3416da
AM
975 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
976 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 977
cd3416da
AM
978 /* Keep the most constraining visibility. Leave the remainder
979 of the st_other field to elf_backend_merge_symbol_attribute. */
980 if (symvis - 1 < hvis - 1)
981 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 982 }
b8417128
AM
983 else if (definition
984 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
985 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 986 h->protected_def = 1;
54ac0771
L
987}
988
4f3fedcf
AM
989/* This function is called when we want to merge a new symbol with an
990 existing symbol. It handles the various cases which arise when we
991 find a definition in a dynamic object, or when there is already a
992 definition in a dynamic object. The new symbol is described by
993 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
994 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
995 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
996 of an old common symbol. We set OVERRIDE if the old symbol is
997 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
998 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
999 to change. By OK to change, we mean that we shouldn't warn if the
1000 type or size does change. */
45d6a902 1001
8a56bd02 1002static bfd_boolean
268b6b39
AM
1003_bfd_elf_merge_symbol (bfd *abfd,
1004 struct bfd_link_info *info,
1005 const char *name,
1006 Elf_Internal_Sym *sym,
1007 asection **psec,
1008 bfd_vma *pvalue,
4f3fedcf
AM
1009 struct elf_link_hash_entry **sym_hash,
1010 bfd **poldbfd,
37a9e49a 1011 bfd_boolean *pold_weak,
af44c138 1012 unsigned int *pold_alignment,
268b6b39
AM
1013 bfd_boolean *skip,
1014 bfd_boolean *override,
1015 bfd_boolean *type_change_ok,
6e33951e
L
1016 bfd_boolean *size_change_ok,
1017 bfd_boolean *matched)
252b5132 1018{
7479dfd4 1019 asection *sec, *oldsec;
45d6a902 1020 struct elf_link_hash_entry *h;
90c984fc 1021 struct elf_link_hash_entry *hi;
45d6a902
AM
1022 struct elf_link_hash_entry *flip;
1023 int bind;
1024 bfd *oldbfd;
1025 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1026 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1027 const struct elf_backend_data *bed;
6e33951e 1028 char *new_version;
45d6a902
AM
1029
1030 *skip = FALSE;
1031 *override = FALSE;
1032
1033 sec = *psec;
1034 bind = ELF_ST_BIND (sym->st_info);
1035
1036 if (! bfd_is_und_section (sec))
1037 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1038 else
1039 h = ((struct elf_link_hash_entry *)
1040 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1041 if (h == NULL)
1042 return FALSE;
1043 *sym_hash = h;
252b5132 1044
88ba32a0
L
1045 bed = get_elf_backend_data (abfd);
1046
6e33951e 1047 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1048 if (h->versioned != unversioned)
6e33951e 1049 {
422f1182
L
1050 /* Symbol version is unknown or versioned. */
1051 new_version = strrchr (name, ELF_VER_CHR);
1052 if (new_version)
1053 {
1054 if (h->versioned == unknown)
1055 {
1056 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1057 h->versioned = versioned_hidden;
1058 else
1059 h->versioned = versioned;
1060 }
1061 new_version += 1;
1062 if (new_version[0] == '\0')
1063 new_version = NULL;
1064 }
1065 else
1066 h->versioned = unversioned;
6e33951e 1067 }
422f1182
L
1068 else
1069 new_version = NULL;
6e33951e 1070
90c984fc
L
1071 /* For merging, we only care about real symbols. But we need to make
1072 sure that indirect symbol dynamic flags are updated. */
1073 hi = h;
45d6a902
AM
1074 while (h->root.type == bfd_link_hash_indirect
1075 || h->root.type == bfd_link_hash_warning)
1076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1077
6e33951e
L
1078 if (!*matched)
1079 {
1080 if (hi == h || h->root.type == bfd_link_hash_new)
1081 *matched = TRUE;
1082 else
1083 {
ae7683d2 1084 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1085 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1086 true if the new symbol is only visible to the symbol with
6e33951e 1087 the same symbol version. */
422f1182
L
1088 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1089 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1090 if (!old_hidden && !new_hidden)
1091 /* The new symbol matches the existing symbol if both
1092 aren't hidden. */
1093 *matched = TRUE;
1094 else
1095 {
1096 /* OLD_VERSION is the symbol version of the existing
1097 symbol. */
422f1182
L
1098 char *old_version;
1099
1100 if (h->versioned >= versioned)
1101 old_version = strrchr (h->root.root.string,
1102 ELF_VER_CHR) + 1;
1103 else
1104 old_version = NULL;
6e33951e
L
1105
1106 /* The new symbol matches the existing symbol if they
1107 have the same symbol version. */
1108 *matched = (old_version == new_version
1109 || (old_version != NULL
1110 && new_version != NULL
1111 && strcmp (old_version, new_version) == 0));
1112 }
1113 }
1114 }
1115
934bce08
AM
1116 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1117 existing symbol. */
1118
1119 oldbfd = NULL;
1120 oldsec = NULL;
1121 switch (h->root.type)
1122 {
1123 default:
1124 break;
1125
1126 case bfd_link_hash_undefined:
1127 case bfd_link_hash_undefweak:
1128 oldbfd = h->root.u.undef.abfd;
1129 break;
1130
1131 case bfd_link_hash_defined:
1132 case bfd_link_hash_defweak:
1133 oldbfd = h->root.u.def.section->owner;
1134 oldsec = h->root.u.def.section;
1135 break;
1136
1137 case bfd_link_hash_common:
1138 oldbfd = h->root.u.c.p->section->owner;
1139 oldsec = h->root.u.c.p->section;
1140 if (pold_alignment)
1141 *pold_alignment = h->root.u.c.p->alignment_power;
1142 break;
1143 }
1144 if (poldbfd && *poldbfd == NULL)
1145 *poldbfd = oldbfd;
1146
1147 /* Differentiate strong and weak symbols. */
1148 newweak = bind == STB_WEAK;
1149 oldweak = (h->root.type == bfd_link_hash_defweak
1150 || h->root.type == bfd_link_hash_undefweak);
1151 if (pold_weak)
1152 *pold_weak = oldweak;
1153
1154 /* This code is for coping with dynamic objects, and is only useful
1155 if we are doing an ELF link. */
1156 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1157 return TRUE;
1158
40b36307 1159 /* We have to check it for every instance since the first few may be
ee659f1f 1160 references and not all compilers emit symbol type for undefined
40b36307
L
1161 symbols. */
1162 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1163
ee659f1f
AM
1164 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1165 respectively, is from a dynamic object. */
1166
1167 newdyn = (abfd->flags & DYNAMIC) != 0;
1168
1169 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1170 syms and defined syms in dynamic libraries respectively.
1171 ref_dynamic on the other hand can be set for a symbol defined in
1172 a dynamic library, and def_dynamic may not be set; When the
1173 definition in a dynamic lib is overridden by a definition in the
1174 executable use of the symbol in the dynamic lib becomes a
1175 reference to the executable symbol. */
1176 if (newdyn)
1177 {
1178 if (bfd_is_und_section (sec))
1179 {
1180 if (bind != STB_WEAK)
1181 {
1182 h->ref_dynamic_nonweak = 1;
1183 hi->ref_dynamic_nonweak = 1;
1184 }
1185 }
1186 else
1187 {
6e33951e
L
1188 /* Update the existing symbol only if they match. */
1189 if (*matched)
1190 h->dynamic_def = 1;
ee659f1f
AM
1191 hi->dynamic_def = 1;
1192 }
1193 }
1194
45d6a902
AM
1195 /* If we just created the symbol, mark it as being an ELF symbol.
1196 Other than that, there is nothing to do--there is no merge issue
1197 with a newly defined symbol--so we just return. */
1198
1199 if (h->root.type == bfd_link_hash_new)
252b5132 1200 {
f5385ebf 1201 h->non_elf = 0;
45d6a902
AM
1202 return TRUE;
1203 }
252b5132 1204
45d6a902
AM
1205 /* In cases involving weak versioned symbols, we may wind up trying
1206 to merge a symbol with itself. Catch that here, to avoid the
1207 confusion that results if we try to override a symbol with
1208 itself. The additional tests catch cases like
1209 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1210 dynamic object, which we do want to handle here. */
1211 if (abfd == oldbfd
895fa45f 1212 && (newweak || oldweak)
45d6a902 1213 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1214 || !h->def_regular))
45d6a902
AM
1215 return TRUE;
1216
707bba77 1217 olddyn = FALSE;
45d6a902
AM
1218 if (oldbfd != NULL)
1219 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1220 else if (oldsec != NULL)
45d6a902 1221 {
707bba77 1222 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1223 indices used by MIPS ELF. */
707bba77 1224 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1225 }
252b5132 1226
45d6a902
AM
1227 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1228 respectively, appear to be a definition rather than reference. */
1229
707bba77 1230 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1231
707bba77
AM
1232 olddef = (h->root.type != bfd_link_hash_undefined
1233 && h->root.type != bfd_link_hash_undefweak
202ac193 1234 && h->root.type != bfd_link_hash_common);
45d6a902 1235
0a36a439
L
1236 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1237 respectively, appear to be a function. */
1238
1239 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1240 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1241
1242 oldfunc = (h->type != STT_NOTYPE
1243 && bed->is_function_type (h->type));
1244
c5d37467 1245 if (!(newfunc && oldfunc)
5b677558
AM
1246 && ELF_ST_TYPE (sym->st_info) != h->type
1247 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1248 && h->type != STT_NOTYPE
c5d37467
AM
1249 && (newdef || bfd_is_com_section (sec))
1250 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1251 {
c5d37467
AM
1252 /* If creating a default indirect symbol ("foo" or "foo@") from
1253 a dynamic versioned definition ("foo@@") skip doing so if
1254 there is an existing regular definition with a different
1255 type. We don't want, for example, a "time" variable in the
1256 executable overriding a "time" function in a shared library. */
1257 if (newdyn
1258 && !olddyn)
1259 {
1260 *skip = TRUE;
1261 return TRUE;
1262 }
1263
1264 /* When adding a symbol from a regular object file after we have
1265 created indirect symbols, undo the indirection and any
1266 dynamic state. */
1267 if (hi != h
1268 && !newdyn
1269 && olddyn)
1270 {
1271 h = hi;
1272 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1273 h->forced_local = 0;
1274 h->ref_dynamic = 0;
1275 h->def_dynamic = 0;
1276 h->dynamic_def = 0;
1277 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1278 {
1279 h->root.type = bfd_link_hash_undefined;
1280 h->root.u.undef.abfd = abfd;
1281 }
1282 else
1283 {
1284 h->root.type = bfd_link_hash_new;
1285 h->root.u.undef.abfd = NULL;
1286 }
1287 return TRUE;
1288 }
580a2b6e
L
1289 }
1290
4c34aff8
AM
1291 /* Check TLS symbols. We don't check undefined symbols introduced
1292 by "ld -u" which have no type (and oldbfd NULL), and we don't
1293 check symbols from plugins because they also have no type. */
1294 if (oldbfd != NULL
1295 && (oldbfd->flags & BFD_PLUGIN) == 0
1296 && (abfd->flags & BFD_PLUGIN) == 0
1297 && ELF_ST_TYPE (sym->st_info) != h->type
1298 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1299 {
1300 bfd *ntbfd, *tbfd;
1301 bfd_boolean ntdef, tdef;
1302 asection *ntsec, *tsec;
1303
1304 if (h->type == STT_TLS)
1305 {
3b36f7e6 1306 ntbfd = abfd;
7479dfd4
L
1307 ntsec = sec;
1308 ntdef = newdef;
1309 tbfd = oldbfd;
1310 tsec = oldsec;
1311 tdef = olddef;
1312 }
1313 else
1314 {
1315 ntbfd = oldbfd;
1316 ntsec = oldsec;
1317 ntdef = olddef;
1318 tbfd = abfd;
1319 tsec = sec;
1320 tdef = newdef;
1321 }
1322
1323 if (tdef && ntdef)
4eca0228 1324 _bfd_error_handler
695344c0 1325 /* xgettext:c-format */
191c0c42
AM
1326 (_("%s: TLS definition in %B section %A "
1327 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1328 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1329 else if (!tdef && !ntdef)
4eca0228 1330 _bfd_error_handler
695344c0 1331 /* xgettext:c-format */
191c0c42
AM
1332 (_("%s: TLS reference in %B "
1333 "mismatches non-TLS reference in %B"),
c08bb8dd 1334 h->root.root.string, tbfd, ntbfd);
7479dfd4 1335 else if (tdef)
4eca0228 1336 _bfd_error_handler
695344c0 1337 /* xgettext:c-format */
191c0c42
AM
1338 (_("%s: TLS definition in %B section %A "
1339 "mismatches non-TLS reference in %B"),
c08bb8dd 1340 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1341 else
4eca0228 1342 _bfd_error_handler
695344c0 1343 /* xgettext:c-format */
191c0c42
AM
1344 (_("%s: TLS reference in %B "
1345 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1346 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1347
1348 bfd_set_error (bfd_error_bad_value);
1349 return FALSE;
1350 }
1351
45d6a902
AM
1352 /* If the old symbol has non-default visibility, we ignore the new
1353 definition from a dynamic object. */
1354 if (newdyn
9c7a29a3 1355 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1356 && !bfd_is_und_section (sec))
1357 {
1358 *skip = TRUE;
1359 /* Make sure this symbol is dynamic. */
f5385ebf 1360 h->ref_dynamic = 1;
90c984fc 1361 hi->ref_dynamic = 1;
45d6a902
AM
1362 /* A protected symbol has external availability. Make sure it is
1363 recorded as dynamic.
1364
1365 FIXME: Should we check type and size for protected symbol? */
1366 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1367 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1368 else
1369 return TRUE;
1370 }
1371 else if (!newdyn
9c7a29a3 1372 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1373 && h->def_dynamic)
45d6a902
AM
1374 {
1375 /* If the new symbol with non-default visibility comes from a
1376 relocatable file and the old definition comes from a dynamic
1377 object, we remove the old definition. */
6c9b78e6 1378 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1379 {
1380 /* Handle the case where the old dynamic definition is
1381 default versioned. We need to copy the symbol info from
1382 the symbol with default version to the normal one if it
1383 was referenced before. */
1384 if (h->ref_regular)
1385 {
6c9b78e6 1386 hi->root.type = h->root.type;
d2dee3b2 1387 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1388 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1389
6c9b78e6 1390 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1391 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1392 {
aed81c4e
MR
1393 /* If the new symbol is hidden or internal, completely undo
1394 any dynamic link state. */
1395 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1396 h->forced_local = 0;
1397 h->ref_dynamic = 0;
d2dee3b2
L
1398 }
1399 else
aed81c4e
MR
1400 h->ref_dynamic = 1;
1401
1402 h->def_dynamic = 0;
aed81c4e
MR
1403 /* FIXME: Should we check type and size for protected symbol? */
1404 h->size = 0;
1405 h->type = 0;
1406
6c9b78e6 1407 h = hi;
d2dee3b2
L
1408 }
1409 else
6c9b78e6 1410 h = hi;
d2dee3b2 1411 }
1de1a317 1412
f5eda473
AM
1413 /* If the old symbol was undefined before, then it will still be
1414 on the undefs list. If the new symbol is undefined or
1415 common, we can't make it bfd_link_hash_new here, because new
1416 undefined or common symbols will be added to the undefs list
1417 by _bfd_generic_link_add_one_symbol. Symbols may not be
1418 added twice to the undefs list. Also, if the new symbol is
1419 undefweak then we don't want to lose the strong undef. */
1420 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1421 {
1de1a317 1422 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1423 h->root.u.undef.abfd = abfd;
1424 }
1425 else
1426 {
1427 h->root.type = bfd_link_hash_new;
1428 h->root.u.undef.abfd = NULL;
1429 }
1430
f5eda473 1431 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1432 {
f5eda473
AM
1433 /* If the new symbol is hidden or internal, completely undo
1434 any dynamic link state. */
1435 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1436 h->forced_local = 0;
1437 h->ref_dynamic = 0;
45d6a902 1438 }
f5eda473
AM
1439 else
1440 h->ref_dynamic = 1;
1441 h->def_dynamic = 0;
45d6a902
AM
1442 /* FIXME: Should we check type and size for protected symbol? */
1443 h->size = 0;
1444 h->type = 0;
1445 return TRUE;
1446 }
14a793b2 1447
15b43f48
AM
1448 /* If a new weak symbol definition comes from a regular file and the
1449 old symbol comes from a dynamic library, we treat the new one as
1450 strong. Similarly, an old weak symbol definition from a regular
1451 file is treated as strong when the new symbol comes from a dynamic
1452 library. Further, an old weak symbol from a dynamic library is
1453 treated as strong if the new symbol is from a dynamic library.
1454 This reflects the way glibc's ld.so works.
1455
1456 Do this before setting *type_change_ok or *size_change_ok so that
1457 we warn properly when dynamic library symbols are overridden. */
1458
1459 if (newdef && !newdyn && olddyn)
0f8a2703 1460 newweak = FALSE;
15b43f48 1461 if (olddef && newdyn)
0f8a2703
AM
1462 oldweak = FALSE;
1463
d334575b 1464 /* Allow changes between different types of function symbol. */
0a36a439 1465 if (newfunc && oldfunc)
fcb93ecf
PB
1466 *type_change_ok = TRUE;
1467
79349b09
AM
1468 /* It's OK to change the type if either the existing symbol or the
1469 new symbol is weak. A type change is also OK if the old symbol
1470 is undefined and the new symbol is defined. */
252b5132 1471
79349b09
AM
1472 if (oldweak
1473 || newweak
1474 || (newdef
1475 && h->root.type == bfd_link_hash_undefined))
1476 *type_change_ok = TRUE;
1477
1478 /* It's OK to change the size if either the existing symbol or the
1479 new symbol is weak, or if the old symbol is undefined. */
1480
1481 if (*type_change_ok
1482 || h->root.type == bfd_link_hash_undefined)
1483 *size_change_ok = TRUE;
45d6a902 1484
45d6a902
AM
1485 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1486 symbol, respectively, appears to be a common symbol in a dynamic
1487 object. If a symbol appears in an uninitialized section, and is
1488 not weak, and is not a function, then it may be a common symbol
1489 which was resolved when the dynamic object was created. We want
1490 to treat such symbols specially, because they raise special
1491 considerations when setting the symbol size: if the symbol
1492 appears as a common symbol in a regular object, and the size in
1493 the regular object is larger, we must make sure that we use the
1494 larger size. This problematic case can always be avoided in C,
1495 but it must be handled correctly when using Fortran shared
1496 libraries.
1497
1498 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1499 likewise for OLDDYNCOMMON and OLDDEF.
1500
1501 Note that this test is just a heuristic, and that it is quite
1502 possible to have an uninitialized symbol in a shared object which
1503 is really a definition, rather than a common symbol. This could
1504 lead to some minor confusion when the symbol really is a common
1505 symbol in some regular object. However, I think it will be
1506 harmless. */
1507
1508 if (newdyn
1509 && newdef
79349b09 1510 && !newweak
45d6a902
AM
1511 && (sec->flags & SEC_ALLOC) != 0
1512 && (sec->flags & SEC_LOAD) == 0
1513 && sym->st_size > 0
0a36a439 1514 && !newfunc)
45d6a902
AM
1515 newdyncommon = TRUE;
1516 else
1517 newdyncommon = FALSE;
1518
1519 if (olddyn
1520 && olddef
1521 && h->root.type == bfd_link_hash_defined
f5385ebf 1522 && h->def_dynamic
45d6a902
AM
1523 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1524 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1525 && h->size > 0
0a36a439 1526 && !oldfunc)
45d6a902
AM
1527 olddyncommon = TRUE;
1528 else
1529 olddyncommon = FALSE;
1530
a4d8e49b
L
1531 /* We now know everything about the old and new symbols. We ask the
1532 backend to check if we can merge them. */
5d13b3b3
AM
1533 if (bed->merge_symbol != NULL)
1534 {
1535 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1536 return FALSE;
1537 sec = *psec;
1538 }
a4d8e49b 1539
45d6a902
AM
1540 /* If both the old and the new symbols look like common symbols in a
1541 dynamic object, set the size of the symbol to the larger of the
1542 two. */
1543
1544 if (olddyncommon
1545 && newdyncommon
1546 && sym->st_size != h->size)
1547 {
1548 /* Since we think we have two common symbols, issue a multiple
1549 common warning if desired. Note that we only warn if the
1550 size is different. If the size is the same, we simply let
1551 the old symbol override the new one as normally happens with
1552 symbols defined in dynamic objects. */
1553
1a72702b
AM
1554 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1555 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1556 if (sym->st_size > h->size)
1557 h->size = sym->st_size;
252b5132 1558
45d6a902 1559 *size_change_ok = TRUE;
252b5132
RH
1560 }
1561
45d6a902
AM
1562 /* If we are looking at a dynamic object, and we have found a
1563 definition, we need to see if the symbol was already defined by
1564 some other object. If so, we want to use the existing
1565 definition, and we do not want to report a multiple symbol
1566 definition error; we do this by clobbering *PSEC to be
1567 bfd_und_section_ptr.
1568
1569 We treat a common symbol as a definition if the symbol in the
1570 shared library is a function, since common symbols always
1571 represent variables; this can cause confusion in principle, but
1572 any such confusion would seem to indicate an erroneous program or
1573 shared library. We also permit a common symbol in a regular
8170f769 1574 object to override a weak symbol in a shared object. */
45d6a902
AM
1575
1576 if (newdyn
1577 && newdef
77cfaee6 1578 && (olddef
45d6a902 1579 || (h->root.type == bfd_link_hash_common
8170f769 1580 && (newweak || newfunc))))
45d6a902
AM
1581 {
1582 *override = TRUE;
1583 newdef = FALSE;
1584 newdyncommon = FALSE;
252b5132 1585
45d6a902
AM
1586 *psec = sec = bfd_und_section_ptr;
1587 *size_change_ok = TRUE;
252b5132 1588
45d6a902
AM
1589 /* If we get here when the old symbol is a common symbol, then
1590 we are explicitly letting it override a weak symbol or
1591 function in a dynamic object, and we don't want to warn about
1592 a type change. If the old symbol is a defined symbol, a type
1593 change warning may still be appropriate. */
252b5132 1594
45d6a902
AM
1595 if (h->root.type == bfd_link_hash_common)
1596 *type_change_ok = TRUE;
1597 }
1598
1599 /* Handle the special case of an old common symbol merging with a
1600 new symbol which looks like a common symbol in a shared object.
1601 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1602 common symbol, and let _bfd_generic_link_add_one_symbol do the
1603 right thing. */
45d6a902
AM
1604
1605 if (newdyncommon
1606 && h->root.type == bfd_link_hash_common)
1607 {
1608 *override = TRUE;
1609 newdef = FALSE;
1610 newdyncommon = FALSE;
1611 *pvalue = sym->st_size;
a4d8e49b 1612 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1613 *size_change_ok = TRUE;
1614 }
1615
c5e2cead 1616 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1617 if (newdef && olddef && newweak)
54ac0771 1618 {
35ed3f94 1619 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1620 if (!(oldbfd != NULL
1621 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1622 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1623 {
1624 newdef = FALSE;
1625 *skip = TRUE;
1626 }
54ac0771
L
1627
1628 /* Merge st_other. If the symbol already has a dynamic index,
1629 but visibility says it should not be visible, turn it into a
1630 local symbol. */
b8417128 1631 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1632 if (h->dynindx != -1)
1633 switch (ELF_ST_VISIBILITY (h->other))
1634 {
1635 case STV_INTERNAL:
1636 case STV_HIDDEN:
1637 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1638 break;
1639 }
1640 }
c5e2cead 1641
45d6a902
AM
1642 /* If the old symbol is from a dynamic object, and the new symbol is
1643 a definition which is not from a dynamic object, then the new
1644 symbol overrides the old symbol. Symbols from regular files
1645 always take precedence over symbols from dynamic objects, even if
1646 they are defined after the dynamic object in the link.
1647
1648 As above, we again permit a common symbol in a regular object to
1649 override a definition in a shared object if the shared object
0f8a2703 1650 symbol is a function or is weak. */
45d6a902
AM
1651
1652 flip = NULL;
77cfaee6 1653 if (!newdyn
45d6a902
AM
1654 && (newdef
1655 || (bfd_is_com_section (sec)
0a36a439 1656 && (oldweak || oldfunc)))
45d6a902
AM
1657 && olddyn
1658 && olddef
f5385ebf 1659 && h->def_dynamic)
45d6a902
AM
1660 {
1661 /* Change the hash table entry to undefined, and let
1662 _bfd_generic_link_add_one_symbol do the right thing with the
1663 new definition. */
1664
1665 h->root.type = bfd_link_hash_undefined;
1666 h->root.u.undef.abfd = h->root.u.def.section->owner;
1667 *size_change_ok = TRUE;
1668
1669 olddef = FALSE;
1670 olddyncommon = FALSE;
1671
1672 /* We again permit a type change when a common symbol may be
1673 overriding a function. */
1674
1675 if (bfd_is_com_section (sec))
0a36a439
L
1676 {
1677 if (oldfunc)
1678 {
1679 /* If a common symbol overrides a function, make sure
1680 that it isn't defined dynamically nor has type
1681 function. */
1682 h->def_dynamic = 0;
1683 h->type = STT_NOTYPE;
1684 }
1685 *type_change_ok = TRUE;
1686 }
45d6a902 1687
6c9b78e6
AM
1688 if (hi->root.type == bfd_link_hash_indirect)
1689 flip = hi;
45d6a902
AM
1690 else
1691 /* This union may have been set to be non-NULL when this symbol
1692 was seen in a dynamic object. We must force the union to be
1693 NULL, so that it is correct for a regular symbol. */
1694 h->verinfo.vertree = NULL;
1695 }
1696
1697 /* Handle the special case of a new common symbol merging with an
1698 old symbol that looks like it might be a common symbol defined in
1699 a shared object. Note that we have already handled the case in
1700 which a new common symbol should simply override the definition
1701 in the shared library. */
1702
1703 if (! newdyn
1704 && bfd_is_com_section (sec)
1705 && olddyncommon)
1706 {
1707 /* It would be best if we could set the hash table entry to a
1708 common symbol, but we don't know what to use for the section
1709 or the alignment. */
1a72702b
AM
1710 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1711 bfd_link_hash_common, sym->st_size);
45d6a902 1712
4cc11e76 1713 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1714 larger, pretend that the new symbol has its size. */
1715
1716 if (h->size > *pvalue)
1717 *pvalue = h->size;
1718
af44c138
L
1719 /* We need to remember the alignment required by the symbol
1720 in the dynamic object. */
1721 BFD_ASSERT (pold_alignment);
1722 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1723
1724 olddef = FALSE;
1725 olddyncommon = FALSE;
1726
1727 h->root.type = bfd_link_hash_undefined;
1728 h->root.u.undef.abfd = h->root.u.def.section->owner;
1729
1730 *size_change_ok = TRUE;
1731 *type_change_ok = TRUE;
1732
6c9b78e6
AM
1733 if (hi->root.type == bfd_link_hash_indirect)
1734 flip = hi;
45d6a902
AM
1735 else
1736 h->verinfo.vertree = NULL;
1737 }
1738
1739 if (flip != NULL)
1740 {
1741 /* Handle the case where we had a versioned symbol in a dynamic
1742 library and now find a definition in a normal object. In this
1743 case, we make the versioned symbol point to the normal one. */
45d6a902 1744 flip->root.type = h->root.type;
00cbee0a 1745 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1746 h->root.type = bfd_link_hash_indirect;
1747 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1748 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1749 if (h->def_dynamic)
45d6a902 1750 {
f5385ebf
AM
1751 h->def_dynamic = 0;
1752 flip->ref_dynamic = 1;
45d6a902
AM
1753 }
1754 }
1755
45d6a902
AM
1756 return TRUE;
1757}
1758
1759/* This function is called to create an indirect symbol from the
1760 default for the symbol with the default version if needed. The
4f3fedcf 1761 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1762 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1763
28caa186 1764static bfd_boolean
268b6b39
AM
1765_bfd_elf_add_default_symbol (bfd *abfd,
1766 struct bfd_link_info *info,
1767 struct elf_link_hash_entry *h,
1768 const char *name,
1769 Elf_Internal_Sym *sym,
4f3fedcf
AM
1770 asection *sec,
1771 bfd_vma value,
1772 bfd **poldbfd,
e3c9d234 1773 bfd_boolean *dynsym)
45d6a902
AM
1774{
1775 bfd_boolean type_change_ok;
1776 bfd_boolean size_change_ok;
1777 bfd_boolean skip;
1778 char *shortname;
1779 struct elf_link_hash_entry *hi;
1780 struct bfd_link_hash_entry *bh;
9c5bfbb7 1781 const struct elf_backend_data *bed;
45d6a902
AM
1782 bfd_boolean collect;
1783 bfd_boolean dynamic;
e3c9d234 1784 bfd_boolean override;
45d6a902
AM
1785 char *p;
1786 size_t len, shortlen;
ffd65175 1787 asection *tmp_sec;
6e33951e 1788 bfd_boolean matched;
45d6a902 1789
422f1182
L
1790 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1791 return TRUE;
1792
45d6a902
AM
1793 /* If this symbol has a version, and it is the default version, we
1794 create an indirect symbol from the default name to the fully
1795 decorated name. This will cause external references which do not
1796 specify a version to be bound to this version of the symbol. */
1797 p = strchr (name, ELF_VER_CHR);
422f1182
L
1798 if (h->versioned == unknown)
1799 {
1800 if (p == NULL)
1801 {
1802 h->versioned = unversioned;
1803 return TRUE;
1804 }
1805 else
1806 {
1807 if (p[1] != ELF_VER_CHR)
1808 {
1809 h->versioned = versioned_hidden;
1810 return TRUE;
1811 }
1812 else
1813 h->versioned = versioned;
1814 }
1815 }
4373f8af
L
1816 else
1817 {
1818 /* PR ld/19073: We may see an unversioned definition after the
1819 default version. */
1820 if (p == NULL)
1821 return TRUE;
1822 }
45d6a902 1823
45d6a902
AM
1824 bed = get_elf_backend_data (abfd);
1825 collect = bed->collect;
1826 dynamic = (abfd->flags & DYNAMIC) != 0;
1827
1828 shortlen = p - name;
a50b1753 1829 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1830 if (shortname == NULL)
1831 return FALSE;
1832 memcpy (shortname, name, shortlen);
1833 shortname[shortlen] = '\0';
1834
1835 /* We are going to create a new symbol. Merge it with any existing
1836 symbol with this name. For the purposes of the merge, act as
1837 though we were defining the symbol we just defined, although we
1838 actually going to define an indirect symbol. */
1839 type_change_ok = FALSE;
1840 size_change_ok = FALSE;
6e33951e 1841 matched = TRUE;
ffd65175
AM
1842 tmp_sec = sec;
1843 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1844 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1845 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1846 return FALSE;
1847
1848 if (skip)
1849 goto nondefault;
1850
5b677558
AM
1851 if (hi->def_regular)
1852 {
1853 /* If the undecorated symbol will have a version added by a
1854 script different to H, then don't indirect to/from the
1855 undecorated symbol. This isn't ideal because we may not yet
1856 have seen symbol versions, if given by a script on the
1857 command line rather than via --version-script. */
1858 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1859 {
1860 bfd_boolean hide;
1861
1862 hi->verinfo.vertree
1863 = bfd_find_version_for_sym (info->version_info,
1864 hi->root.root.string, &hide);
1865 if (hi->verinfo.vertree != NULL && hide)
1866 {
1867 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1868 goto nondefault;
1869 }
1870 }
1871 if (hi->verinfo.vertree != NULL
1872 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1873 goto nondefault;
1874 }
1875
45d6a902
AM
1876 if (! override)
1877 {
c6e8a9a8 1878 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1879 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1880 {
1881 bh = &hi->root;
1882 if (! (_bfd_generic_link_add_one_symbol
1883 (info, abfd, shortname, BSF_INDIRECT,
1884 bfd_ind_section_ptr,
1885 0, name, FALSE, collect, &bh)))
1886 return FALSE;
1887 hi = (struct elf_link_hash_entry *) bh;
1888 }
45d6a902
AM
1889 }
1890 else
1891 {
1892 /* In this case the symbol named SHORTNAME is overriding the
1893 indirect symbol we want to add. We were planning on making
1894 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1895 is the name without a version. NAME is the fully versioned
1896 name, and it is the default version.
1897
1898 Overriding means that we already saw a definition for the
1899 symbol SHORTNAME in a regular object, and it is overriding
1900 the symbol defined in the dynamic object.
1901
1902 When this happens, we actually want to change NAME, the
1903 symbol we just added, to refer to SHORTNAME. This will cause
1904 references to NAME in the shared object to become references
1905 to SHORTNAME in the regular object. This is what we expect
1906 when we override a function in a shared object: that the
1907 references in the shared object will be mapped to the
1908 definition in the regular object. */
1909
1910 while (hi->root.type == bfd_link_hash_indirect
1911 || hi->root.type == bfd_link_hash_warning)
1912 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1913
1914 h->root.type = bfd_link_hash_indirect;
1915 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1916 if (h->def_dynamic)
45d6a902 1917 {
f5385ebf
AM
1918 h->def_dynamic = 0;
1919 hi->ref_dynamic = 1;
1920 if (hi->ref_regular
1921 || hi->def_regular)
45d6a902 1922 {
c152c796 1923 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1924 return FALSE;
1925 }
1926 }
1927
1928 /* Now set HI to H, so that the following code will set the
1929 other fields correctly. */
1930 hi = h;
1931 }
1932
fab4a87f
L
1933 /* Check if HI is a warning symbol. */
1934 if (hi->root.type == bfd_link_hash_warning)
1935 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1936
45d6a902
AM
1937 /* If there is a duplicate definition somewhere, then HI may not
1938 point to an indirect symbol. We will have reported an error to
1939 the user in that case. */
1940
1941 if (hi->root.type == bfd_link_hash_indirect)
1942 {
1943 struct elf_link_hash_entry *ht;
1944
45d6a902 1945 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1946 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1947
68c88cd4
AM
1948 /* A reference to the SHORTNAME symbol from a dynamic library
1949 will be satisfied by the versioned symbol at runtime. In
1950 effect, we have a reference to the versioned symbol. */
1951 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1952 hi->dynamic_def |= ht->dynamic_def;
1953
45d6a902
AM
1954 /* See if the new flags lead us to realize that the symbol must
1955 be dynamic. */
1956 if (! *dynsym)
1957 {
1958 if (! dynamic)
1959 {
0e1862bb 1960 if (! bfd_link_executable (info)
90c984fc 1961 || hi->def_dynamic
f5385ebf 1962 || hi->ref_dynamic)
45d6a902
AM
1963 *dynsym = TRUE;
1964 }
1965 else
1966 {
f5385ebf 1967 if (hi->ref_regular)
45d6a902
AM
1968 *dynsym = TRUE;
1969 }
1970 }
1971 }
1972
1973 /* We also need to define an indirection from the nondefault version
1974 of the symbol. */
1975
1976nondefault:
1977 len = strlen (name);
a50b1753 1978 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1979 if (shortname == NULL)
1980 return FALSE;
1981 memcpy (shortname, name, shortlen);
1982 memcpy (shortname + shortlen, p + 1, len - shortlen);
1983
1984 /* Once again, merge with any existing symbol. */
1985 type_change_ok = FALSE;
1986 size_change_ok = FALSE;
ffd65175
AM
1987 tmp_sec = sec;
1988 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1989 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1990 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1991 return FALSE;
1992
1993 if (skip)
1994 return TRUE;
1995
1996 if (override)
1997 {
1998 /* Here SHORTNAME is a versioned name, so we don't expect to see
1999 the type of override we do in the case above unless it is
4cc11e76 2000 overridden by a versioned definition. */
45d6a902
AM
2001 if (hi->root.type != bfd_link_hash_defined
2002 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2003 _bfd_error_handler
695344c0 2004 /* xgettext:c-format */
d003868e
AM
2005 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2006 abfd, shortname);
45d6a902
AM
2007 }
2008 else
2009 {
2010 bh = &hi->root;
2011 if (! (_bfd_generic_link_add_one_symbol
2012 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2013 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2014 return FALSE;
2015 hi = (struct elf_link_hash_entry *) bh;
2016
2017 /* If there is a duplicate definition somewhere, then HI may not
2018 point to an indirect symbol. We will have reported an error
2019 to the user in that case. */
2020
2021 if (hi->root.type == bfd_link_hash_indirect)
2022 {
fcfa13d2 2023 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2024 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2025 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2026
2027 /* See if the new flags lead us to realize that the symbol
2028 must be dynamic. */
2029 if (! *dynsym)
2030 {
2031 if (! dynamic)
2032 {
0e1862bb 2033 if (! bfd_link_executable (info)
f5385ebf 2034 || hi->ref_dynamic)
45d6a902
AM
2035 *dynsym = TRUE;
2036 }
2037 else
2038 {
f5385ebf 2039 if (hi->ref_regular)
45d6a902
AM
2040 *dynsym = TRUE;
2041 }
2042 }
2043 }
2044 }
2045
2046 return TRUE;
2047}
2048\f
2049/* This routine is used to export all defined symbols into the dynamic
2050 symbol table. It is called via elf_link_hash_traverse. */
2051
28caa186 2052static bfd_boolean
268b6b39 2053_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2054{
a50b1753 2055 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2056
2057 /* Ignore indirect symbols. These are added by the versioning code. */
2058 if (h->root.type == bfd_link_hash_indirect)
2059 return TRUE;
2060
7686d77d
AM
2061 /* Ignore this if we won't export it. */
2062 if (!eif->info->export_dynamic && !h->dynamic)
2063 return TRUE;
45d6a902
AM
2064
2065 if (h->dynindx == -1
fd91d419
L
2066 && (h->def_regular || h->ref_regular)
2067 && ! bfd_hide_sym_by_version (eif->info->version_info,
2068 h->root.root.string))
45d6a902 2069 {
fd91d419 2070 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2071 {
fd91d419
L
2072 eif->failed = TRUE;
2073 return FALSE;
45d6a902
AM
2074 }
2075 }
2076
2077 return TRUE;
2078}
2079\f
2080/* Look through the symbols which are defined in other shared
2081 libraries and referenced here. Update the list of version
2082 dependencies. This will be put into the .gnu.version_r section.
2083 This function is called via elf_link_hash_traverse. */
2084
28caa186 2085static bfd_boolean
268b6b39
AM
2086_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2087 void *data)
45d6a902 2088{
a50b1753 2089 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2090 Elf_Internal_Verneed *t;
2091 Elf_Internal_Vernaux *a;
2092 bfd_size_type amt;
2093
45d6a902
AM
2094 /* We only care about symbols defined in shared objects with version
2095 information. */
f5385ebf
AM
2096 if (!h->def_dynamic
2097 || h->def_regular
45d6a902 2098 || h->dynindx == -1
7b20f099
AM
2099 || h->verinfo.verdef == NULL
2100 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2101 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2102 return TRUE;
2103
2104 /* See if we already know about this version. */
28caa186
AM
2105 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2106 t != NULL;
2107 t = t->vn_nextref)
45d6a902
AM
2108 {
2109 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2110 continue;
2111
2112 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2113 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2114 return TRUE;
2115
2116 break;
2117 }
2118
2119 /* This is a new version. Add it to tree we are building. */
2120
2121 if (t == NULL)
2122 {
2123 amt = sizeof *t;
a50b1753 2124 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2125 if (t == NULL)
2126 {
2127 rinfo->failed = TRUE;
2128 return FALSE;
2129 }
2130
2131 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2132 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2133 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2134 }
2135
2136 amt = sizeof *a;
a50b1753 2137 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2138 if (a == NULL)
2139 {
2140 rinfo->failed = TRUE;
2141 return FALSE;
2142 }
45d6a902
AM
2143
2144 /* Note that we are copying a string pointer here, and testing it
2145 above. If bfd_elf_string_from_elf_section is ever changed to
2146 discard the string data when low in memory, this will have to be
2147 fixed. */
2148 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2149
2150 a->vna_flags = h->verinfo.verdef->vd_flags;
2151 a->vna_nextptr = t->vn_auxptr;
2152
2153 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2154 ++rinfo->vers;
2155
2156 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2157
2158 t->vn_auxptr = a;
2159
2160 return TRUE;
2161}
2162
2163/* Figure out appropriate versions for all the symbols. We may not
2164 have the version number script until we have read all of the input
2165 files, so until that point we don't know which symbols should be
2166 local. This function is called via elf_link_hash_traverse. */
2167
28caa186 2168static bfd_boolean
268b6b39 2169_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2170{
28caa186 2171 struct elf_info_failed *sinfo;
45d6a902 2172 struct bfd_link_info *info;
9c5bfbb7 2173 const struct elf_backend_data *bed;
45d6a902
AM
2174 struct elf_info_failed eif;
2175 char *p;
45d6a902 2176
a50b1753 2177 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2178 info = sinfo->info;
2179
45d6a902
AM
2180 /* Fix the symbol flags. */
2181 eif.failed = FALSE;
2182 eif.info = info;
2183 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2184 {
2185 if (eif.failed)
2186 sinfo->failed = TRUE;
2187 return FALSE;
2188 }
2189
2190 /* We only need version numbers for symbols defined in regular
2191 objects. */
f5385ebf 2192 if (!h->def_regular)
45d6a902
AM
2193 return TRUE;
2194
28caa186 2195 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2196 p = strchr (h->root.root.string, ELF_VER_CHR);
2197 if (p != NULL && h->verinfo.vertree == NULL)
2198 {
2199 struct bfd_elf_version_tree *t;
45d6a902 2200
45d6a902
AM
2201 ++p;
2202 if (*p == ELF_VER_CHR)
6e33951e 2203 ++p;
45d6a902
AM
2204
2205 /* If there is no version string, we can just return out. */
2206 if (*p == '\0')
6e33951e 2207 return TRUE;
45d6a902
AM
2208
2209 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2210 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2211 {
2212 if (strcmp (t->name, p) == 0)
2213 {
2214 size_t len;
2215 char *alc;
2216 struct bfd_elf_version_expr *d;
2217
2218 len = p - h->root.root.string;
a50b1753 2219 alc = (char *) bfd_malloc (len);
45d6a902 2220 if (alc == NULL)
14b1c01e
AM
2221 {
2222 sinfo->failed = TRUE;
2223 return FALSE;
2224 }
45d6a902
AM
2225 memcpy (alc, h->root.root.string, len - 1);
2226 alc[len - 1] = '\0';
2227 if (alc[len - 2] == ELF_VER_CHR)
2228 alc[len - 2] = '\0';
2229
2230 h->verinfo.vertree = t;
2231 t->used = TRUE;
2232 d = NULL;
2233
108ba305
JJ
2234 if (t->globals.list != NULL)
2235 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2236
2237 /* See if there is anything to force this symbol to
2238 local scope. */
108ba305 2239 if (d == NULL && t->locals.list != NULL)
45d6a902 2240 {
108ba305
JJ
2241 d = (*t->match) (&t->locals, NULL, alc);
2242 if (d != NULL
2243 && h->dynindx != -1
108ba305
JJ
2244 && ! info->export_dynamic)
2245 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2246 }
2247
2248 free (alc);
2249 break;
2250 }
2251 }
2252
2253 /* If we are building an application, we need to create a
2254 version node for this version. */
0e1862bb 2255 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2256 {
2257 struct bfd_elf_version_tree **pp;
2258 int version_index;
2259
2260 /* If we aren't going to export this symbol, we don't need
2261 to worry about it. */
2262 if (h->dynindx == -1)
2263 return TRUE;
2264
ef53be89
AM
2265 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2266 sizeof *t);
45d6a902
AM
2267 if (t == NULL)
2268 {
2269 sinfo->failed = TRUE;
2270 return FALSE;
2271 }
2272
45d6a902 2273 t->name = p;
45d6a902
AM
2274 t->name_indx = (unsigned int) -1;
2275 t->used = TRUE;
2276
2277 version_index = 1;
2278 /* Don't count anonymous version tag. */
fd91d419
L
2279 if (sinfo->info->version_info != NULL
2280 && sinfo->info->version_info->vernum == 0)
45d6a902 2281 version_index = 0;
fd91d419
L
2282 for (pp = &sinfo->info->version_info;
2283 *pp != NULL;
2284 pp = &(*pp)->next)
45d6a902
AM
2285 ++version_index;
2286 t->vernum = version_index;
2287
2288 *pp = t;
2289
2290 h->verinfo.vertree = t;
2291 }
2292 else if (t == NULL)
2293 {
2294 /* We could not find the version for a symbol when
2295 generating a shared archive. Return an error. */
4eca0228 2296 _bfd_error_handler
695344c0 2297 /* xgettext:c-format */
c55fe096 2298 (_("%B: version node not found for symbol %s"),
28caa186 2299 info->output_bfd, h->root.root.string);
45d6a902
AM
2300 bfd_set_error (bfd_error_bad_value);
2301 sinfo->failed = TRUE;
2302 return FALSE;
2303 }
45d6a902
AM
2304 }
2305
2306 /* If we don't have a version for this symbol, see if we can find
2307 something. */
fd91d419 2308 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2309 {
1e8fa21e 2310 bfd_boolean hide;
ae5a3597 2311
fd91d419
L
2312 h->verinfo.vertree
2313 = bfd_find_version_for_sym (sinfo->info->version_info,
2314 h->root.root.string, &hide);
1e8fa21e
AM
2315 if (h->verinfo.vertree != NULL && hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2317 }
2318
2319 return TRUE;
2320}
2321\f
45d6a902
AM
2322/* Read and swap the relocs from the section indicated by SHDR. This
2323 may be either a REL or a RELA section. The relocations are
2324 translated into RELA relocations and stored in INTERNAL_RELOCS,
2325 which should have already been allocated to contain enough space.
2326 The EXTERNAL_RELOCS are a buffer where the external form of the
2327 relocations should be stored.
2328
2329 Returns FALSE if something goes wrong. */
2330
2331static bfd_boolean
268b6b39 2332elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2333 asection *sec,
268b6b39
AM
2334 Elf_Internal_Shdr *shdr,
2335 void *external_relocs,
2336 Elf_Internal_Rela *internal_relocs)
45d6a902 2337{
9c5bfbb7 2338 const struct elf_backend_data *bed;
268b6b39 2339 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2340 const bfd_byte *erela;
2341 const bfd_byte *erelaend;
2342 Elf_Internal_Rela *irela;
243ef1e0
L
2343 Elf_Internal_Shdr *symtab_hdr;
2344 size_t nsyms;
45d6a902 2345
45d6a902
AM
2346 /* Position ourselves at the start of the section. */
2347 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2348 return FALSE;
2349
2350 /* Read the relocations. */
2351 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2352 return FALSE;
2353
243ef1e0 2354 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2355 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2356
45d6a902
AM
2357 bed = get_elf_backend_data (abfd);
2358
2359 /* Convert the external relocations to the internal format. */
2360 if (shdr->sh_entsize == bed->s->sizeof_rel)
2361 swap_in = bed->s->swap_reloc_in;
2362 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2363 swap_in = bed->s->swap_reloca_in;
2364 else
2365 {
2366 bfd_set_error (bfd_error_wrong_format);
2367 return FALSE;
2368 }
2369
a50b1753 2370 erela = (const bfd_byte *) external_relocs;
51992aec 2371 erelaend = erela + shdr->sh_size;
45d6a902
AM
2372 irela = internal_relocs;
2373 while (erela < erelaend)
2374 {
243ef1e0
L
2375 bfd_vma r_symndx;
2376
45d6a902 2377 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2378 r_symndx = ELF32_R_SYM (irela->r_info);
2379 if (bed->s->arch_size == 64)
2380 r_symndx >>= 24;
ce98a316
NC
2381 if (nsyms > 0)
2382 {
2383 if ((size_t) r_symndx >= nsyms)
2384 {
4eca0228 2385 _bfd_error_handler
695344c0 2386 /* xgettext:c-format */
ce98a316
NC
2387 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2388 " for offset 0x%lx in section `%A'"),
c08bb8dd
AM
2389 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2390 irela->r_offset, sec);
ce98a316
NC
2391 bfd_set_error (bfd_error_bad_value);
2392 return FALSE;
2393 }
2394 }
cf35638d 2395 else if (r_symndx != STN_UNDEF)
243ef1e0 2396 {
4eca0228 2397 _bfd_error_handler
695344c0 2398 /* xgettext:c-format */
c08bb8dd
AM
2399 (_("%B: non-zero symbol index (0x%lx)"
2400 " for offset 0x%lx in section `%A'"
ce98a316 2401 " when the object file has no symbol table"),
c08bb8dd
AM
2402 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2403 irela->r_offset, sec);
243ef1e0
L
2404 bfd_set_error (bfd_error_bad_value);
2405 return FALSE;
2406 }
45d6a902
AM
2407 irela += bed->s->int_rels_per_ext_rel;
2408 erela += shdr->sh_entsize;
2409 }
2410
2411 return TRUE;
2412}
2413
2414/* Read and swap the relocs for a section O. They may have been
2415 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2416 not NULL, they are used as buffers to read into. They are known to
2417 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2418 the return value is allocated using either malloc or bfd_alloc,
2419 according to the KEEP_MEMORY argument. If O has two relocation
2420 sections (both REL and RELA relocations), then the REL_HDR
2421 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2422 RELA_HDR relocations. */
45d6a902
AM
2423
2424Elf_Internal_Rela *
268b6b39
AM
2425_bfd_elf_link_read_relocs (bfd *abfd,
2426 asection *o,
2427 void *external_relocs,
2428 Elf_Internal_Rela *internal_relocs,
2429 bfd_boolean keep_memory)
45d6a902 2430{
268b6b39 2431 void *alloc1 = NULL;
45d6a902 2432 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2433 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2434 struct bfd_elf_section_data *esdo = elf_section_data (o);
2435 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2436
d4730f92
BS
2437 if (esdo->relocs != NULL)
2438 return esdo->relocs;
45d6a902
AM
2439
2440 if (o->reloc_count == 0)
2441 return NULL;
2442
45d6a902
AM
2443 if (internal_relocs == NULL)
2444 {
2445 bfd_size_type size;
2446
2447 size = o->reloc_count;
2448 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2449 if (keep_memory)
a50b1753 2450 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2451 else
a50b1753 2452 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2453 if (internal_relocs == NULL)
2454 goto error_return;
2455 }
2456
2457 if (external_relocs == NULL)
2458 {
d4730f92
BS
2459 bfd_size_type size = 0;
2460
2461 if (esdo->rel.hdr)
2462 size += esdo->rel.hdr->sh_size;
2463 if (esdo->rela.hdr)
2464 size += esdo->rela.hdr->sh_size;
45d6a902 2465
268b6b39 2466 alloc1 = bfd_malloc (size);
45d6a902
AM
2467 if (alloc1 == NULL)
2468 goto error_return;
2469 external_relocs = alloc1;
2470 }
2471
d4730f92
BS
2472 internal_rela_relocs = internal_relocs;
2473 if (esdo->rel.hdr)
2474 {
2475 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2476 external_relocs,
2477 internal_relocs))
2478 goto error_return;
2479 external_relocs = (((bfd_byte *) external_relocs)
2480 + esdo->rel.hdr->sh_size);
2481 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2482 * bed->s->int_rels_per_ext_rel);
2483 }
2484
2485 if (esdo->rela.hdr
2486 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2487 external_relocs,
2488 internal_rela_relocs)))
45d6a902
AM
2489 goto error_return;
2490
2491 /* Cache the results for next time, if we can. */
2492 if (keep_memory)
d4730f92 2493 esdo->relocs = internal_relocs;
45d6a902
AM
2494
2495 if (alloc1 != NULL)
2496 free (alloc1);
2497
2498 /* Don't free alloc2, since if it was allocated we are passing it
2499 back (under the name of internal_relocs). */
2500
2501 return internal_relocs;
2502
2503 error_return:
2504 if (alloc1 != NULL)
2505 free (alloc1);
2506 if (alloc2 != NULL)
4dd07732
AM
2507 {
2508 if (keep_memory)
2509 bfd_release (abfd, alloc2);
2510 else
2511 free (alloc2);
2512 }
45d6a902
AM
2513 return NULL;
2514}
2515
2516/* Compute the size of, and allocate space for, REL_HDR which is the
2517 section header for a section containing relocations for O. */
2518
28caa186 2519static bfd_boolean
9eaff861
AO
2520_bfd_elf_link_size_reloc_section (bfd *abfd,
2521 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2522{
9eaff861 2523 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2524
2525 /* That allows us to calculate the size of the section. */
9eaff861 2526 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2527
2528 /* The contents field must last into write_object_contents, so we
2529 allocate it with bfd_alloc rather than malloc. Also since we
2530 cannot be sure that the contents will actually be filled in,
2531 we zero the allocated space. */
a50b1753 2532 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2533 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2534 return FALSE;
2535
d4730f92 2536 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2537 {
2538 struct elf_link_hash_entry **p;
2539
ca4be51c
AM
2540 p = ((struct elf_link_hash_entry **)
2541 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2542 if (p == NULL)
2543 return FALSE;
2544
d4730f92 2545 reldata->hashes = p;
45d6a902
AM
2546 }
2547
2548 return TRUE;
2549}
2550
2551/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2552 originated from the section given by INPUT_REL_HDR) to the
2553 OUTPUT_BFD. */
2554
2555bfd_boolean
268b6b39
AM
2556_bfd_elf_link_output_relocs (bfd *output_bfd,
2557 asection *input_section,
2558 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2559 Elf_Internal_Rela *internal_relocs,
2560 struct elf_link_hash_entry **rel_hash
2561 ATTRIBUTE_UNUSED)
45d6a902
AM
2562{
2563 Elf_Internal_Rela *irela;
2564 Elf_Internal_Rela *irelaend;
2565 bfd_byte *erel;
d4730f92 2566 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2567 asection *output_section;
9c5bfbb7 2568 const struct elf_backend_data *bed;
268b6b39 2569 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2570 struct bfd_elf_section_data *esdo;
45d6a902
AM
2571
2572 output_section = input_section->output_section;
45d6a902 2573
d4730f92
BS
2574 bed = get_elf_backend_data (output_bfd);
2575 esdo = elf_section_data (output_section);
2576 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2577 {
d4730f92
BS
2578 output_reldata = &esdo->rel;
2579 swap_out = bed->s->swap_reloc_out;
45d6a902 2580 }
d4730f92
BS
2581 else if (esdo->rela.hdr
2582 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2583 {
d4730f92
BS
2584 output_reldata = &esdo->rela;
2585 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2586 }
2587 else
2588 {
4eca0228 2589 _bfd_error_handler
695344c0 2590 /* xgettext:c-format */
d003868e
AM
2591 (_("%B: relocation size mismatch in %B section %A"),
2592 output_bfd, input_section->owner, input_section);
297d8443 2593 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2594 return FALSE;
2595 }
2596
d4730f92
BS
2597 erel = output_reldata->hdr->contents;
2598 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2599 irela = internal_relocs;
2600 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2601 * bed->s->int_rels_per_ext_rel);
2602 while (irela < irelaend)
2603 {
2604 (*swap_out) (output_bfd, irela, erel);
2605 irela += bed->s->int_rels_per_ext_rel;
2606 erel += input_rel_hdr->sh_entsize;
2607 }
2608
2609 /* Bump the counter, so that we know where to add the next set of
2610 relocations. */
d4730f92 2611 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2612
2613 return TRUE;
2614}
2615\f
508c3946
L
2616/* Make weak undefined symbols in PIE dynamic. */
2617
2618bfd_boolean
2619_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2620 struct elf_link_hash_entry *h)
2621{
0e1862bb 2622 if (bfd_link_pie (info)
508c3946
L
2623 && h->dynindx == -1
2624 && h->root.type == bfd_link_hash_undefweak)
2625 return bfd_elf_link_record_dynamic_symbol (info, h);
2626
2627 return TRUE;
2628}
2629
45d6a902
AM
2630/* Fix up the flags for a symbol. This handles various cases which
2631 can only be fixed after all the input files are seen. This is
2632 currently called by both adjust_dynamic_symbol and
2633 assign_sym_version, which is unnecessary but perhaps more robust in
2634 the face of future changes. */
2635
28caa186 2636static bfd_boolean
268b6b39
AM
2637_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2638 struct elf_info_failed *eif)
45d6a902 2639{
33774f08 2640 const struct elf_backend_data *bed;
508c3946 2641
45d6a902
AM
2642 /* If this symbol was mentioned in a non-ELF file, try to set
2643 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2644 permit a non-ELF file to correctly refer to a symbol defined in
2645 an ELF dynamic object. */
f5385ebf 2646 if (h->non_elf)
45d6a902
AM
2647 {
2648 while (h->root.type == bfd_link_hash_indirect)
2649 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2650
2651 if (h->root.type != bfd_link_hash_defined
2652 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2653 {
2654 h->ref_regular = 1;
2655 h->ref_regular_nonweak = 1;
2656 }
45d6a902
AM
2657 else
2658 {
2659 if (h->root.u.def.section->owner != NULL
2660 && (bfd_get_flavour (h->root.u.def.section->owner)
2661 == bfd_target_elf_flavour))
f5385ebf
AM
2662 {
2663 h->ref_regular = 1;
2664 h->ref_regular_nonweak = 1;
2665 }
45d6a902 2666 else
f5385ebf 2667 h->def_regular = 1;
45d6a902
AM
2668 }
2669
2670 if (h->dynindx == -1
f5385ebf
AM
2671 && (h->def_dynamic
2672 || h->ref_dynamic))
45d6a902 2673 {
c152c796 2674 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2675 {
2676 eif->failed = TRUE;
2677 return FALSE;
2678 }
2679 }
2680 }
2681 else
2682 {
f5385ebf 2683 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2684 was first seen in a non-ELF file. Fortunately, if the symbol
2685 was first seen in an ELF file, we're probably OK unless the
2686 symbol was defined in a non-ELF file. Catch that case here.
2687 FIXME: We're still in trouble if the symbol was first seen in
2688 a dynamic object, and then later in a non-ELF regular object. */
2689 if ((h->root.type == bfd_link_hash_defined
2690 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2691 && !h->def_regular
45d6a902
AM
2692 && (h->root.u.def.section->owner != NULL
2693 ? (bfd_get_flavour (h->root.u.def.section->owner)
2694 != bfd_target_elf_flavour)
2695 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2696 && !h->def_dynamic)))
2697 h->def_regular = 1;
45d6a902
AM
2698 }
2699
508c3946 2700 /* Backend specific symbol fixup. */
33774f08
AM
2701 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2702 if (bed->elf_backend_fixup_symbol
2703 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2704 return FALSE;
508c3946 2705
45d6a902
AM
2706 /* If this is a final link, and the symbol was defined as a common
2707 symbol in a regular object file, and there was no definition in
2708 any dynamic object, then the linker will have allocated space for
f5385ebf 2709 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2710 flag will not have been set. */
2711 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2712 && !h->def_regular
2713 && h->ref_regular
2714 && !h->def_dynamic
96f29d96 2715 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2716 h->def_regular = 1;
45d6a902 2717
4deb8f71
L
2718 /* If a weak undefined symbol has non-default visibility, we also
2719 hide it from the dynamic linker. */
2720 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2721 && h->root.type == bfd_link_hash_undefweak)
2722 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2723
2724 /* A hidden versioned symbol in executable should be forced local if
2725 it is is locally defined, not referenced by shared library and not
2726 exported. */
2727 else if (bfd_link_executable (eif->info)
2728 && h->versioned == versioned_hidden
2729 && !eif->info->export_dynamic
2730 && !h->dynamic
2731 && !h->ref_dynamic
2732 && h->def_regular)
2733 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2734
45d6a902
AM
2735 /* If -Bsymbolic was used (which means to bind references to global
2736 symbols to the definition within the shared object), and this
2737 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2738 need a PLT entry. Likewise, if the symbol has non-default
2739 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2740 will force it local. */
4deb8f71
L
2741 else if (h->needs_plt
2742 && bfd_link_pic (eif->info)
2743 && is_elf_hash_table (eif->info->hash)
2744 && (SYMBOLIC_BIND (eif->info, h)
2745 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2746 && h->def_regular)
45d6a902 2747 {
45d6a902
AM
2748 bfd_boolean force_local;
2749
45d6a902
AM
2750 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2751 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2752 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2753 }
2754
45d6a902
AM
2755 /* If this is a weak defined symbol in a dynamic object, and we know
2756 the real definition in the dynamic object, copy interesting flags
2757 over to the real definition. */
f6e332e6 2758 if (h->u.weakdef != NULL)
45d6a902 2759 {
45d6a902
AM
2760 /* If the real definition is defined by a regular object file,
2761 don't do anything special. See the longer description in
2762 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2763 if (h->u.weakdef->def_regular)
f6e332e6 2764 h->u.weakdef = NULL;
45d6a902 2765 else
a26587ba 2766 {
4e6b54a6
AM
2767 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2768
2769 while (h->root.type == bfd_link_hash_indirect)
2770 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2771
2772 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2773 || h->root.type == bfd_link_hash_defweak);
2774 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2775 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2776 || weakdef->root.type == bfd_link_hash_defweak);
2777 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2778 }
45d6a902
AM
2779 }
2780
2781 return TRUE;
2782}
2783
2784/* Make the backend pick a good value for a dynamic symbol. This is
2785 called via elf_link_hash_traverse, and also calls itself
2786 recursively. */
2787
28caa186 2788static bfd_boolean
268b6b39 2789_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2790{
a50b1753 2791 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2792 bfd *dynobj;
9c5bfbb7 2793 const struct elf_backend_data *bed;
45d6a902 2794
0eddce27 2795 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2796 return FALSE;
2797
45d6a902
AM
2798 /* Ignore indirect symbols. These are added by the versioning code. */
2799 if (h->root.type == bfd_link_hash_indirect)
2800 return TRUE;
2801
2802 /* Fix the symbol flags. */
2803 if (! _bfd_elf_fix_symbol_flags (h, eif))
2804 return FALSE;
2805
954b63d4
AM
2806 if (h->root.type == bfd_link_hash_undefweak)
2807 {
2808 if (eif->info->dynamic_undefined_weak == 0)
2809 _bfd_elf_link_hash_hide_symbol (eif->info, h, TRUE);
2810 else if (eif->info->dynamic_undefined_weak > 0
2811 && h->ref_regular
2812 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2813 && !bfd_hide_sym_by_version (eif->info->version_info,
2814 h->root.root.string))
2815 {
2816 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2817 {
2818 eif->failed = TRUE;
2819 return FALSE;
2820 }
2821 }
2822 }
2823
45d6a902
AM
2824 /* If this symbol does not require a PLT entry, and it is not
2825 defined by a dynamic object, or is not referenced by a regular
2826 object, ignore it. We do have to handle a weak defined symbol,
2827 even if no regular object refers to it, if we decided to add it
2828 to the dynamic symbol table. FIXME: Do we normally need to worry
2829 about symbols which are defined by one dynamic object and
2830 referenced by another one? */
f5385ebf 2831 if (!h->needs_plt
91e21fb7 2832 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2833 && (h->def_regular
2834 || !h->def_dynamic
2835 || (!h->ref_regular
f6e332e6 2836 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2837 {
a6aa5195 2838 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2839 return TRUE;
2840 }
2841
2842 /* If we've already adjusted this symbol, don't do it again. This
2843 can happen via a recursive call. */
f5385ebf 2844 if (h->dynamic_adjusted)
45d6a902
AM
2845 return TRUE;
2846
2847 /* Don't look at this symbol again. Note that we must set this
2848 after checking the above conditions, because we may look at a
2849 symbol once, decide not to do anything, and then get called
2850 recursively later after REF_REGULAR is set below. */
f5385ebf 2851 h->dynamic_adjusted = 1;
45d6a902
AM
2852
2853 /* If this is a weak definition, and we know a real definition, and
2854 the real symbol is not itself defined by a regular object file,
2855 then get a good value for the real definition. We handle the
2856 real symbol first, for the convenience of the backend routine.
2857
2858 Note that there is a confusing case here. If the real definition
2859 is defined by a regular object file, we don't get the real symbol
2860 from the dynamic object, but we do get the weak symbol. If the
2861 processor backend uses a COPY reloc, then if some routine in the
2862 dynamic object changes the real symbol, we will not see that
2863 change in the corresponding weak symbol. This is the way other
2864 ELF linkers work as well, and seems to be a result of the shared
2865 library model.
2866
2867 I will clarify this issue. Most SVR4 shared libraries define the
2868 variable _timezone and define timezone as a weak synonym. The
2869 tzset call changes _timezone. If you write
2870 extern int timezone;
2871 int _timezone = 5;
2872 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2873 you might expect that, since timezone is a synonym for _timezone,
2874 the same number will print both times. However, if the processor
2875 backend uses a COPY reloc, then actually timezone will be copied
2876 into your process image, and, since you define _timezone
2877 yourself, _timezone will not. Thus timezone and _timezone will
2878 wind up at different memory locations. The tzset call will set
2879 _timezone, leaving timezone unchanged. */
2880
f6e332e6 2881 if (h->u.weakdef != NULL)
45d6a902 2882 {
ec24dc88
AM
2883 /* If we get to this point, there is an implicit reference to
2884 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2885 h->u.weakdef->ref_regular = 1;
45d6a902 2886
ec24dc88
AM
2887 /* Ensure that the backend adjust_dynamic_symbol function sees
2888 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2889 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2890 return FALSE;
2891 }
2892
2893 /* If a symbol has no type and no size and does not require a PLT
2894 entry, then we are probably about to do the wrong thing here: we
2895 are probably going to create a COPY reloc for an empty object.
2896 This case can arise when a shared object is built with assembly
2897 code, and the assembly code fails to set the symbol type. */
2898 if (h->size == 0
2899 && h->type == STT_NOTYPE
f5385ebf 2900 && !h->needs_plt)
4eca0228 2901 _bfd_error_handler
45d6a902
AM
2902 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2903 h->root.root.string);
2904
2905 dynobj = elf_hash_table (eif->info)->dynobj;
2906 bed = get_elf_backend_data (dynobj);
e7c33416 2907
45d6a902
AM
2908 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2909 {
2910 eif->failed = TRUE;
2911 return FALSE;
2912 }
2913
2914 return TRUE;
2915}
2916
027297b7
L
2917/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2918 DYNBSS. */
2919
2920bfd_boolean
6cabe1ea
AM
2921_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2922 struct elf_link_hash_entry *h,
027297b7
L
2923 asection *dynbss)
2924{
91ac5911 2925 unsigned int power_of_two;
027297b7
L
2926 bfd_vma mask;
2927 asection *sec = h->root.u.def.section;
2928
2929 /* The section aligment of definition is the maximum alignment
91ac5911
L
2930 requirement of symbols defined in the section. Since we don't
2931 know the symbol alignment requirement, we start with the
2932 maximum alignment and check low bits of the symbol address
2933 for the minimum alignment. */
2934 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2935 mask = ((bfd_vma) 1 << power_of_two) - 1;
2936 while ((h->root.u.def.value & mask) != 0)
2937 {
2938 mask >>= 1;
2939 --power_of_two;
2940 }
027297b7 2941
91ac5911
L
2942 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2943 dynbss))
027297b7
L
2944 {
2945 /* Adjust the section alignment if needed. */
2946 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2947 power_of_two))
027297b7
L
2948 return FALSE;
2949 }
2950
91ac5911 2951 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2952 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2953
2954 /* Define the symbol as being at this point in DYNBSS. */
2955 h->root.u.def.section = dynbss;
2956 h->root.u.def.value = dynbss->size;
2957
2958 /* Increment the size of DYNBSS to make room for the symbol. */
2959 dynbss->size += h->size;
2960
f7483970
L
2961 /* No error if extern_protected_data is true. */
2962 if (h->protected_def
889c2a67
L
2963 && (!info->extern_protected_data
2964 || (info->extern_protected_data < 0
2965 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2966 info->callbacks->einfo
2967 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2968 h->root.root.string);
6cabe1ea 2969
027297b7
L
2970 return TRUE;
2971}
2972
45d6a902
AM
2973/* Adjust all external symbols pointing into SEC_MERGE sections
2974 to reflect the object merging within the sections. */
2975
28caa186 2976static bfd_boolean
268b6b39 2977_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2978{
2979 asection *sec;
2980
45d6a902
AM
2981 if ((h->root.type == bfd_link_hash_defined
2982 || h->root.type == bfd_link_hash_defweak)
2983 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2984 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2985 {
a50b1753 2986 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2987
2988 h->root.u.def.value =
2989 _bfd_merged_section_offset (output_bfd,
2990 &h->root.u.def.section,
2991 elf_section_data (sec)->sec_info,
753731ee 2992 h->root.u.def.value);
45d6a902
AM
2993 }
2994
2995 return TRUE;
2996}
986a241f
RH
2997
2998/* Returns false if the symbol referred to by H should be considered
2999 to resolve local to the current module, and true if it should be
3000 considered to bind dynamically. */
3001
3002bfd_boolean
268b6b39
AM
3003_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3004 struct bfd_link_info *info,
89a2ee5a 3005 bfd_boolean not_local_protected)
986a241f
RH
3006{
3007 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3008 const struct elf_backend_data *bed;
3009 struct elf_link_hash_table *hash_table;
986a241f
RH
3010
3011 if (h == NULL)
3012 return FALSE;
3013
3014 while (h->root.type == bfd_link_hash_indirect
3015 || h->root.type == bfd_link_hash_warning)
3016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3017
3018 /* If it was forced local, then clearly it's not dynamic. */
3019 if (h->dynindx == -1)
3020 return FALSE;
f5385ebf 3021 if (h->forced_local)
986a241f
RH
3022 return FALSE;
3023
3024 /* Identify the cases where name binding rules say that a
3025 visible symbol resolves locally. */
0e1862bb
L
3026 binding_stays_local_p = (bfd_link_executable (info)
3027 || SYMBOLIC_BIND (info, h));
986a241f
RH
3028
3029 switch (ELF_ST_VISIBILITY (h->other))
3030 {
3031 case STV_INTERNAL:
3032 case STV_HIDDEN:
3033 return FALSE;
3034
3035 case STV_PROTECTED:
fcb93ecf
PB
3036 hash_table = elf_hash_table (info);
3037 if (!is_elf_hash_table (hash_table))
3038 return FALSE;
3039
3040 bed = get_elf_backend_data (hash_table->dynobj);
3041
986a241f
RH
3042 /* Proper resolution for function pointer equality may require
3043 that these symbols perhaps be resolved dynamically, even though
3044 we should be resolving them to the current module. */
89a2ee5a 3045 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3046 binding_stays_local_p = TRUE;
3047 break;
3048
3049 default:
986a241f
RH
3050 break;
3051 }
3052
aa37626c 3053 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3054 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3055 return TRUE;
3056
986a241f
RH
3057 /* Otherwise, the symbol is dynamic if binding rules don't tell
3058 us that it remains local. */
3059 return !binding_stays_local_p;
3060}
f6c52c13
AM
3061
3062/* Return true if the symbol referred to by H should be considered
3063 to resolve local to the current module, and false otherwise. Differs
3064 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3065 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3066 for the place where dynindx == -1 is tested. If that test is true,
3067 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3068 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3069 defined symbols.
89a2ee5a
AM
3070 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3071 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3072 treatment of undefined weak symbols. For those that do not make
3073 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3074
3075bfd_boolean
268b6b39
AM
3076_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3077 struct bfd_link_info *info,
3078 bfd_boolean local_protected)
f6c52c13 3079{
fcb93ecf
PB
3080 const struct elf_backend_data *bed;
3081 struct elf_link_hash_table *hash_table;
3082
f6c52c13
AM
3083 /* If it's a local sym, of course we resolve locally. */
3084 if (h == NULL)
3085 return TRUE;
3086
d95edcac
L
3087 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3088 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3089 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3090 return TRUE;
3091
0fad2956
MR
3092 /* Forced local symbols resolve locally. */
3093 if (h->forced_local)
3094 return TRUE;
3095
7e2294f9
AO
3096 /* Common symbols that become definitions don't get the DEF_REGULAR
3097 flag set, so test it first, and don't bail out. */
3098 if (ELF_COMMON_DEF_P (h))
3099 /* Do nothing. */;
f6c52c13 3100 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3101 resolve locally. The sym is either undefined or dynamic. */
3102 else if (!h->def_regular)
f6c52c13
AM
3103 return FALSE;
3104
0fad2956 3105 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3106 if (h->dynindx == -1)
3107 return TRUE;
3108
3109 /* At this point, we know the symbol is defined and dynamic. In an
3110 executable it must resolve locally, likewise when building symbolic
3111 shared libraries. */
0e1862bb 3112 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3113 return TRUE;
3114
3115 /* Now deal with defined dynamic symbols in shared libraries. Ones
3116 with default visibility might not resolve locally. */
3117 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3118 return FALSE;
3119
fcb93ecf
PB
3120 hash_table = elf_hash_table (info);
3121 if (!is_elf_hash_table (hash_table))
3122 return TRUE;
3123
3124 bed = get_elf_backend_data (hash_table->dynobj);
3125
f7483970
L
3126 /* If extern_protected_data is false, STV_PROTECTED non-function
3127 symbols are local. */
889c2a67
L
3128 if ((!info->extern_protected_data
3129 || (info->extern_protected_data < 0
3130 && !bed->extern_protected_data))
3131 && !bed->is_function_type (h->type))
1c16dfa5
L
3132 return TRUE;
3133
f6c52c13 3134 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3135 symbols be treated as dynamic symbols. If the address of a
3136 function not defined in an executable is set to that function's
3137 plt entry in the executable, then the address of the function in
3138 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3139 return local_protected;
3140}
e1918d23
AM
3141
3142/* Caches some TLS segment info, and ensures that the TLS segment vma is
3143 aligned. Returns the first TLS output section. */
3144
3145struct bfd_section *
3146_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3147{
3148 struct bfd_section *sec, *tls;
3149 unsigned int align = 0;
3150
3151 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3152 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3153 break;
3154 tls = sec;
3155
3156 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3157 if (sec->alignment_power > align)
3158 align = sec->alignment_power;
3159
3160 elf_hash_table (info)->tls_sec = tls;
3161
3162 /* Ensure the alignment of the first section is the largest alignment,
3163 so that the tls segment starts aligned. */
3164 if (tls != NULL)
3165 tls->alignment_power = align;
3166
3167 return tls;
3168}
0ad989f9
L
3169
3170/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3171static bfd_boolean
3172is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3173 Elf_Internal_Sym *sym)
3174{
a4d8e49b
L
3175 const struct elf_backend_data *bed;
3176
0ad989f9
L
3177 /* Local symbols do not count, but target specific ones might. */
3178 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3179 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3180 return FALSE;
3181
fcb93ecf 3182 bed = get_elf_backend_data (abfd);
0ad989f9 3183 /* Function symbols do not count. */
fcb93ecf 3184 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3185 return FALSE;
3186
3187 /* If the section is undefined, then so is the symbol. */
3188 if (sym->st_shndx == SHN_UNDEF)
3189 return FALSE;
3190
3191 /* If the symbol is defined in the common section, then
3192 it is a common definition and so does not count. */
a4d8e49b 3193 if (bed->common_definition (sym))
0ad989f9
L
3194 return FALSE;
3195
3196 /* If the symbol is in a target specific section then we
3197 must rely upon the backend to tell us what it is. */
3198 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3199 /* FIXME - this function is not coded yet:
3200
3201 return _bfd_is_global_symbol_definition (abfd, sym);
3202
3203 Instead for now assume that the definition is not global,
3204 Even if this is wrong, at least the linker will behave
3205 in the same way that it used to do. */
3206 return FALSE;
3207
3208 return TRUE;
3209}
3210
3211/* Search the symbol table of the archive element of the archive ABFD
3212 whose archive map contains a mention of SYMDEF, and determine if
3213 the symbol is defined in this element. */
3214static bfd_boolean
3215elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3216{
3217 Elf_Internal_Shdr * hdr;
ef53be89
AM
3218 size_t symcount;
3219 size_t extsymcount;
3220 size_t extsymoff;
0ad989f9
L
3221 Elf_Internal_Sym *isymbuf;
3222 Elf_Internal_Sym *isym;
3223 Elf_Internal_Sym *isymend;
3224 bfd_boolean result;
3225
3226 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3227 if (abfd == NULL)
3228 return FALSE;
3229
3230 if (! bfd_check_format (abfd, bfd_object))
3231 return FALSE;
3232
7dc3990e
L
3233 /* Select the appropriate symbol table. If we don't know if the
3234 object file is an IR object, give linker LTO plugin a chance to
3235 get the correct symbol table. */
3236 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3237#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3238 || (abfd->plugin_format == bfd_plugin_unknown
3239 && bfd_link_plugin_object_p (abfd))
3240#endif
3241 )
3242 {
3243 /* Use the IR symbol table if the object has been claimed by
3244 plugin. */
3245 abfd = abfd->plugin_dummy_bfd;
3246 hdr = &elf_tdata (abfd)->symtab_hdr;
3247 }
3248 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3249 hdr = &elf_tdata (abfd)->symtab_hdr;
3250 else
3251 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3252
3253 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3254
3255 /* The sh_info field of the symtab header tells us where the
3256 external symbols start. We don't care about the local symbols. */
3257 if (elf_bad_symtab (abfd))
3258 {
3259 extsymcount = symcount;
3260 extsymoff = 0;
3261 }
3262 else
3263 {
3264 extsymcount = symcount - hdr->sh_info;
3265 extsymoff = hdr->sh_info;
3266 }
3267
3268 if (extsymcount == 0)
3269 return FALSE;
3270
3271 /* Read in the symbol table. */
3272 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3273 NULL, NULL, NULL);
3274 if (isymbuf == NULL)
3275 return FALSE;
3276
3277 /* Scan the symbol table looking for SYMDEF. */
3278 result = FALSE;
3279 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3280 {
3281 const char *name;
3282
3283 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3284 isym->st_name);
3285 if (name == NULL)
3286 break;
3287
3288 if (strcmp (name, symdef->name) == 0)
3289 {
3290 result = is_global_data_symbol_definition (abfd, isym);
3291 break;
3292 }
3293 }
3294
3295 free (isymbuf);
3296
3297 return result;
3298}
3299\f
5a580b3a
AM
3300/* Add an entry to the .dynamic table. */
3301
3302bfd_boolean
3303_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3304 bfd_vma tag,
3305 bfd_vma val)
3306{
3307 struct elf_link_hash_table *hash_table;
3308 const struct elf_backend_data *bed;
3309 asection *s;
3310 bfd_size_type newsize;
3311 bfd_byte *newcontents;
3312 Elf_Internal_Dyn dyn;
3313
3314 hash_table = elf_hash_table (info);
3315 if (! is_elf_hash_table (hash_table))
3316 return FALSE;
3317
3318 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3319 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3320 BFD_ASSERT (s != NULL);
3321
eea6121a 3322 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3323 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3324 if (newcontents == NULL)
3325 return FALSE;
3326
3327 dyn.d_tag = tag;
3328 dyn.d_un.d_val = val;
eea6121a 3329 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3330
eea6121a 3331 s->size = newsize;
5a580b3a
AM
3332 s->contents = newcontents;
3333
3334 return TRUE;
3335}
3336
3337/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3338 otherwise just check whether one already exists. Returns -1 on error,
3339 1 if a DT_NEEDED tag already exists, and 0 on success. */
3340
4ad4eba5 3341static int
7e9f0867
AM
3342elf_add_dt_needed_tag (bfd *abfd,
3343 struct bfd_link_info *info,
4ad4eba5
AM
3344 const char *soname,
3345 bfd_boolean do_it)
5a580b3a
AM
3346{
3347 struct elf_link_hash_table *hash_table;
ef53be89 3348 size_t strindex;
5a580b3a 3349
7e9f0867
AM
3350 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3351 return -1;
3352
5a580b3a 3353 hash_table = elf_hash_table (info);
5a580b3a 3354 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3355 if (strindex == (size_t) -1)
5a580b3a
AM
3356 return -1;
3357
02be4619 3358 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3359 {
3360 asection *sdyn;
3361 const struct elf_backend_data *bed;
3362 bfd_byte *extdyn;
3363
3364 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3365 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3366 if (sdyn != NULL)
3367 for (extdyn = sdyn->contents;
3368 extdyn < sdyn->contents + sdyn->size;
3369 extdyn += bed->s->sizeof_dyn)
3370 {
3371 Elf_Internal_Dyn dyn;
5a580b3a 3372
7e9f0867
AM
3373 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3374 if (dyn.d_tag == DT_NEEDED
3375 && dyn.d_un.d_val == strindex)
3376 {
3377 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3378 return 1;
3379 }
3380 }
5a580b3a
AM
3381 }
3382
3383 if (do_it)
3384 {
7e9f0867
AM
3385 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3386 return -1;
3387
5a580b3a
AM
3388 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3389 return -1;
3390 }
3391 else
3392 /* We were just checking for existence of the tag. */
3393 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3394
3395 return 0;
3396}
3397
7b15fa7a
AM
3398/* Return true if SONAME is on the needed list between NEEDED and STOP
3399 (or the end of list if STOP is NULL), and needed by a library that
3400 will be loaded. */
3401
010e5ae2 3402static bfd_boolean
7b15fa7a
AM
3403on_needed_list (const char *soname,
3404 struct bfd_link_needed_list *needed,
3405 struct bfd_link_needed_list *stop)
010e5ae2 3406{
7b15fa7a
AM
3407 struct bfd_link_needed_list *look;
3408 for (look = needed; look != stop; look = look->next)
3409 if (strcmp (soname, look->name) == 0
3410 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3411 /* If needed by a library that itself is not directly
3412 needed, recursively check whether that library is
3413 indirectly needed. Since we add DT_NEEDED entries to
3414 the end of the list, library dependencies appear after
3415 the library. Therefore search prior to the current
3416 LOOK, preventing possible infinite recursion. */
3417 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3418 return TRUE;
3419
3420 return FALSE;
3421}
3422
14160578 3423/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3424static int
3425elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3426{
3427 const struct elf_link_hash_entry *h1;
3428 const struct elf_link_hash_entry *h2;
10b7e05b 3429 bfd_signed_vma vdiff;
5a580b3a
AM
3430
3431 h1 = *(const struct elf_link_hash_entry **) arg1;
3432 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3433 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3434 if (vdiff != 0)
3435 return vdiff > 0 ? 1 : -1;
3436 else
3437 {
d3435ae8 3438 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3439 if (sdiff != 0)
3440 return sdiff > 0 ? 1 : -1;
3441 }
14160578
AM
3442 vdiff = h1->size - h2->size;
3443 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3444}
4ad4eba5 3445
5a580b3a
AM
3446/* This function is used to adjust offsets into .dynstr for
3447 dynamic symbols. This is called via elf_link_hash_traverse. */
3448
3449static bfd_boolean
3450elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3451{
a50b1753 3452 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3453
5a580b3a
AM
3454 if (h->dynindx != -1)
3455 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3456 return TRUE;
3457}
3458
3459/* Assign string offsets in .dynstr, update all structures referencing
3460 them. */
3461
4ad4eba5
AM
3462static bfd_boolean
3463elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3464{
3465 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3466 struct elf_link_local_dynamic_entry *entry;
3467 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3468 bfd *dynobj = hash_table->dynobj;
3469 asection *sdyn;
3470 bfd_size_type size;
3471 const struct elf_backend_data *bed;
3472 bfd_byte *extdyn;
3473
3474 _bfd_elf_strtab_finalize (dynstr);
3475 size = _bfd_elf_strtab_size (dynstr);
3476
3477 bed = get_elf_backend_data (dynobj);
3d4d4302 3478 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3479 BFD_ASSERT (sdyn != NULL);
3480
3481 /* Update all .dynamic entries referencing .dynstr strings. */
3482 for (extdyn = sdyn->contents;
eea6121a 3483 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3484 extdyn += bed->s->sizeof_dyn)
3485 {
3486 Elf_Internal_Dyn dyn;
3487
3488 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3489 switch (dyn.d_tag)
3490 {
3491 case DT_STRSZ:
3492 dyn.d_un.d_val = size;
3493 break;
3494 case DT_NEEDED:
3495 case DT_SONAME:
3496 case DT_RPATH:
3497 case DT_RUNPATH:
3498 case DT_FILTER:
3499 case DT_AUXILIARY:
7ee314fa
AM
3500 case DT_AUDIT:
3501 case DT_DEPAUDIT:
5a580b3a
AM
3502 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3503 break;
3504 default:
3505 continue;
3506 }
3507 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3508 }
3509
3510 /* Now update local dynamic symbols. */
3511 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3512 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3513 entry->isym.st_name);
3514
3515 /* And the rest of dynamic symbols. */
3516 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3517
3518 /* Adjust version definitions. */
3519 if (elf_tdata (output_bfd)->cverdefs)
3520 {
3521 asection *s;
3522 bfd_byte *p;
ef53be89 3523 size_t i;
5a580b3a
AM
3524 Elf_Internal_Verdef def;
3525 Elf_Internal_Verdaux defaux;
3526
3d4d4302 3527 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3528 p = s->contents;
3529 do
3530 {
3531 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3532 &def);
3533 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3534 if (def.vd_aux != sizeof (Elf_External_Verdef))
3535 continue;
5a580b3a
AM
3536 for (i = 0; i < def.vd_cnt; ++i)
3537 {
3538 _bfd_elf_swap_verdaux_in (output_bfd,
3539 (Elf_External_Verdaux *) p, &defaux);
3540 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3541 defaux.vda_name);
3542 _bfd_elf_swap_verdaux_out (output_bfd,
3543 &defaux, (Elf_External_Verdaux *) p);
3544 p += sizeof (Elf_External_Verdaux);
3545 }
3546 }
3547 while (def.vd_next);
3548 }
3549
3550 /* Adjust version references. */
3551 if (elf_tdata (output_bfd)->verref)
3552 {
3553 asection *s;
3554 bfd_byte *p;
ef53be89 3555 size_t i;
5a580b3a
AM
3556 Elf_Internal_Verneed need;
3557 Elf_Internal_Vernaux needaux;
3558
3d4d4302 3559 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3560 p = s->contents;
3561 do
3562 {
3563 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3564 &need);
3565 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3566 _bfd_elf_swap_verneed_out (output_bfd, &need,
3567 (Elf_External_Verneed *) p);
3568 p += sizeof (Elf_External_Verneed);
3569 for (i = 0; i < need.vn_cnt; ++i)
3570 {
3571 _bfd_elf_swap_vernaux_in (output_bfd,
3572 (Elf_External_Vernaux *) p, &needaux);
3573 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3574 needaux.vna_name);
3575 _bfd_elf_swap_vernaux_out (output_bfd,
3576 &needaux,
3577 (Elf_External_Vernaux *) p);
3578 p += sizeof (Elf_External_Vernaux);
3579 }
3580 }
3581 while (need.vn_next);
3582 }
3583
3584 return TRUE;
3585}
3586\f
13285a1b
AM
3587/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3588 The default is to only match when the INPUT and OUTPUT are exactly
3589 the same target. */
3590
3591bfd_boolean
3592_bfd_elf_default_relocs_compatible (const bfd_target *input,
3593 const bfd_target *output)
3594{
3595 return input == output;
3596}
3597
3598/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3599 This version is used when different targets for the same architecture
3600 are virtually identical. */
3601
3602bfd_boolean
3603_bfd_elf_relocs_compatible (const bfd_target *input,
3604 const bfd_target *output)
3605{
3606 const struct elf_backend_data *obed, *ibed;
3607
3608 if (input == output)
3609 return TRUE;
3610
3611 ibed = xvec_get_elf_backend_data (input);
3612 obed = xvec_get_elf_backend_data (output);
3613
3614 if (ibed->arch != obed->arch)
3615 return FALSE;
3616
3617 /* If both backends are using this function, deem them compatible. */
3618 return ibed->relocs_compatible == obed->relocs_compatible;
3619}
3620
e5034e59
AM
3621/* Make a special call to the linker "notice" function to tell it that
3622 we are about to handle an as-needed lib, or have finished
1b786873 3623 processing the lib. */
e5034e59
AM
3624
3625bfd_boolean
3626_bfd_elf_notice_as_needed (bfd *ibfd,
3627 struct bfd_link_info *info,
3628 enum notice_asneeded_action act)
3629{
46135103 3630 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3631}
3632
d9689752
L
3633/* Check relocations an ELF object file. */
3634
3635bfd_boolean
3636_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3637{
3638 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3639 struct elf_link_hash_table *htab = elf_hash_table (info);
3640
3641 /* If this object is the same format as the output object, and it is
3642 not a shared library, then let the backend look through the
3643 relocs.
3644
3645 This is required to build global offset table entries and to
3646 arrange for dynamic relocs. It is not required for the
3647 particular common case of linking non PIC code, even when linking
3648 against shared libraries, but unfortunately there is no way of
3649 knowing whether an object file has been compiled PIC or not.
3650 Looking through the relocs is not particularly time consuming.
3651 The problem is that we must either (1) keep the relocs in memory,
3652 which causes the linker to require additional runtime memory or
3653 (2) read the relocs twice from the input file, which wastes time.
3654 This would be a good case for using mmap.
3655
3656 I have no idea how to handle linking PIC code into a file of a
3657 different format. It probably can't be done. */
3658 if ((abfd->flags & DYNAMIC) == 0
3659 && is_elf_hash_table (htab)
3660 && bed->check_relocs != NULL
3661 && elf_object_id (abfd) == elf_hash_table_id (htab)
3662 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3663 {
3664 asection *o;
3665
3666 for (o = abfd->sections; o != NULL; o = o->next)
3667 {
3668 Elf_Internal_Rela *internal_relocs;
3669 bfd_boolean ok;
3670
5ce03cea 3671 /* Don't check relocations in excluded sections. */
d9689752 3672 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3673 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3674 || o->reloc_count == 0
3675 || ((info->strip == strip_all || info->strip == strip_debugger)
3676 && (o->flags & SEC_DEBUGGING) != 0)
3677 || bfd_is_abs_section (o->output_section))
3678 continue;
3679
3680 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3681 info->keep_memory);
3682 if (internal_relocs == NULL)
3683 return FALSE;
3684
3685 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3686
3687 if (elf_section_data (o)->relocs != internal_relocs)
3688 free (internal_relocs);
3689
3690 if (! ok)
3691 return FALSE;
3692 }
3693 }
3694
3695 return TRUE;
3696}
3697
4ad4eba5
AM
3698/* Add symbols from an ELF object file to the linker hash table. */
3699
3700static bfd_boolean
3701elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3702{
a0c402a5 3703 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3704 Elf_Internal_Shdr *hdr;
ef53be89
AM
3705 size_t symcount;
3706 size_t extsymcount;
3707 size_t extsymoff;
4ad4eba5
AM
3708 struct elf_link_hash_entry **sym_hash;
3709 bfd_boolean dynamic;
3710 Elf_External_Versym *extversym = NULL;
3711 Elf_External_Versym *ever;
3712 struct elf_link_hash_entry *weaks;
3713 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3714 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3715 Elf_Internal_Sym *isymbuf = NULL;
3716 Elf_Internal_Sym *isym;
3717 Elf_Internal_Sym *isymend;
3718 const struct elf_backend_data *bed;
3719 bfd_boolean add_needed;
66eb6687 3720 struct elf_link_hash_table *htab;
4ad4eba5 3721 bfd_size_type amt;
66eb6687 3722 void *alloc_mark = NULL;
4f87808c
AM
3723 struct bfd_hash_entry **old_table = NULL;
3724 unsigned int old_size = 0;
3725 unsigned int old_count = 0;
66eb6687 3726 void *old_tab = NULL;
66eb6687
AM
3727 void *old_ent;
3728 struct bfd_link_hash_entry *old_undefs = NULL;
3729 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3730 void *old_strtab = NULL;
66eb6687 3731 size_t tabsize = 0;
db6a5d5f 3732 asection *s;
29a9f53e 3733 bfd_boolean just_syms;
4ad4eba5 3734
66eb6687 3735 htab = elf_hash_table (info);
4ad4eba5 3736 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3737
3738 if ((abfd->flags & DYNAMIC) == 0)
3739 dynamic = FALSE;
3740 else
3741 {
3742 dynamic = TRUE;
3743
3744 /* You can't use -r against a dynamic object. Also, there's no
3745 hope of using a dynamic object which does not exactly match
3746 the format of the output file. */
0e1862bb 3747 if (bfd_link_relocatable (info)
66eb6687 3748 || !is_elf_hash_table (htab)
f13a99db 3749 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3750 {
0e1862bb 3751 if (bfd_link_relocatable (info))
9a0789ec
NC
3752 bfd_set_error (bfd_error_invalid_operation);
3753 else
3754 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3755 goto error_return;
3756 }
3757 }
3758
a0c402a5
L
3759 ehdr = elf_elfheader (abfd);
3760 if (info->warn_alternate_em
3761 && bed->elf_machine_code != ehdr->e_machine
3762 && ((bed->elf_machine_alt1 != 0
3763 && ehdr->e_machine == bed->elf_machine_alt1)
3764 || (bed->elf_machine_alt2 != 0
3765 && ehdr->e_machine == bed->elf_machine_alt2)))
3766 info->callbacks->einfo
695344c0 3767 /* xgettext:c-format */
a0c402a5
L
3768 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3769 ehdr->e_machine, abfd, bed->elf_machine_code);
3770
4ad4eba5
AM
3771 /* As a GNU extension, any input sections which are named
3772 .gnu.warning.SYMBOL are treated as warning symbols for the given
3773 symbol. This differs from .gnu.warning sections, which generate
3774 warnings when they are included in an output file. */
dd98f8d2 3775 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3776 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3777 {
db6a5d5f 3778 const char *name;
4ad4eba5 3779
db6a5d5f
AM
3780 name = bfd_get_section_name (abfd, s);
3781 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3782 {
db6a5d5f
AM
3783 char *msg;
3784 bfd_size_type sz;
3785
3786 name += sizeof ".gnu.warning." - 1;
3787
3788 /* If this is a shared object, then look up the symbol
3789 in the hash table. If it is there, and it is already
3790 been defined, then we will not be using the entry
3791 from this shared object, so we don't need to warn.
3792 FIXME: If we see the definition in a regular object
3793 later on, we will warn, but we shouldn't. The only
3794 fix is to keep track of what warnings we are supposed
3795 to emit, and then handle them all at the end of the
3796 link. */
3797 if (dynamic)
4ad4eba5 3798 {
db6a5d5f
AM
3799 struct elf_link_hash_entry *h;
3800
3801 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3802
3803 /* FIXME: What about bfd_link_hash_common? */
3804 if (h != NULL
3805 && (h->root.type == bfd_link_hash_defined
3806 || h->root.type == bfd_link_hash_defweak))
3807 continue;
3808 }
4ad4eba5 3809
db6a5d5f
AM
3810 sz = s->size;
3811 msg = (char *) bfd_alloc (abfd, sz + 1);
3812 if (msg == NULL)
3813 goto error_return;
4ad4eba5 3814
db6a5d5f
AM
3815 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3816 goto error_return;
4ad4eba5 3817
db6a5d5f 3818 msg[sz] = '\0';
4ad4eba5 3819
db6a5d5f
AM
3820 if (! (_bfd_generic_link_add_one_symbol
3821 (info, abfd, name, BSF_WARNING, s, 0, msg,
3822 FALSE, bed->collect, NULL)))
3823 goto error_return;
4ad4eba5 3824
0e1862bb 3825 if (bfd_link_executable (info))
db6a5d5f
AM
3826 {
3827 /* Clobber the section size so that the warning does
3828 not get copied into the output file. */
3829 s->size = 0;
11d2f718 3830
db6a5d5f
AM
3831 /* Also set SEC_EXCLUDE, so that symbols defined in
3832 the warning section don't get copied to the output. */
3833 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3834 }
3835 }
3836 }
3837
29a9f53e
L
3838 just_syms = ((s = abfd->sections) != NULL
3839 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3840
4ad4eba5
AM
3841 add_needed = TRUE;
3842 if (! dynamic)
3843 {
3844 /* If we are creating a shared library, create all the dynamic
3845 sections immediately. We need to attach them to something,
3846 so we attach them to this BFD, provided it is the right
bf89386a
L
3847 format and is not from ld --just-symbols. Always create the
3848 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3849 are no input BFD's of the same format as the output, we can't
3850 make a shared library. */
3851 if (!just_syms
bf89386a 3852 && (bfd_link_pic (info)
9c1d7a08 3853 || (!bfd_link_relocatable (info)
3c5fce9b 3854 && info->nointerp
9c1d7a08 3855 && (info->export_dynamic || info->dynamic)))
66eb6687 3856 && is_elf_hash_table (htab)
f13a99db 3857 && info->output_bfd->xvec == abfd->xvec
66eb6687 3858 && !htab->dynamic_sections_created)
4ad4eba5
AM
3859 {
3860 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3861 goto error_return;
3862 }
3863 }
66eb6687 3864 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3865 goto error_return;
3866 else
3867 {
4ad4eba5 3868 const char *soname = NULL;
7ee314fa 3869 char *audit = NULL;
4ad4eba5 3870 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3871 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3872 int ret;
3873
3874 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3875 ld shouldn't allow it. */
29a9f53e 3876 if (just_syms)
92fd189d 3877 abort ();
4ad4eba5
AM
3878
3879 /* If this dynamic lib was specified on the command line with
3880 --as-needed in effect, then we don't want to add a DT_NEEDED
3881 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3882 in by another lib's DT_NEEDED. When --no-add-needed is used
3883 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3884 any dynamic library in DT_NEEDED tags in the dynamic lib at
3885 all. */
3886 add_needed = (elf_dyn_lib_class (abfd)
3887 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3888 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3889
3890 s = bfd_get_section_by_name (abfd, ".dynamic");
3891 if (s != NULL)
3892 {
3893 bfd_byte *dynbuf;
3894 bfd_byte *extdyn;
cb33740c 3895 unsigned int elfsec;
4ad4eba5
AM
3896 unsigned long shlink;
3897
eea6121a 3898 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3899 {
3900error_free_dyn:
3901 free (dynbuf);
3902 goto error_return;
3903 }
4ad4eba5
AM
3904
3905 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3906 if (elfsec == SHN_BAD)
4ad4eba5
AM
3907 goto error_free_dyn;
3908 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3909
3910 for (extdyn = dynbuf;
eea6121a 3911 extdyn < dynbuf + s->size;
4ad4eba5
AM
3912 extdyn += bed->s->sizeof_dyn)
3913 {
3914 Elf_Internal_Dyn dyn;
3915
3916 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3917 if (dyn.d_tag == DT_SONAME)
3918 {
3919 unsigned int tagv = dyn.d_un.d_val;
3920 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3921 if (soname == NULL)
3922 goto error_free_dyn;
3923 }
3924 if (dyn.d_tag == DT_NEEDED)
3925 {
3926 struct bfd_link_needed_list *n, **pn;
3927 char *fnm, *anm;
3928 unsigned int tagv = dyn.d_un.d_val;
3929
3930 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3931 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3932 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3933 if (n == NULL || fnm == NULL)
3934 goto error_free_dyn;
3935 amt = strlen (fnm) + 1;
a50b1753 3936 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3937 if (anm == NULL)
3938 goto error_free_dyn;
3939 memcpy (anm, fnm, amt);
3940 n->name = anm;
3941 n->by = abfd;
3942 n->next = NULL;
66eb6687 3943 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3944 ;
3945 *pn = n;
3946 }
3947 if (dyn.d_tag == DT_RUNPATH)
3948 {
3949 struct bfd_link_needed_list *n, **pn;
3950 char *fnm, *anm;
3951 unsigned int tagv = dyn.d_un.d_val;
3952
3953 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3954 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3955 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3956 if (n == NULL || fnm == NULL)
3957 goto error_free_dyn;
3958 amt = strlen (fnm) + 1;
a50b1753 3959 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3960 if (anm == NULL)
3961 goto error_free_dyn;
3962 memcpy (anm, fnm, amt);
3963 n->name = anm;
3964 n->by = abfd;
3965 n->next = NULL;
3966 for (pn = & runpath;
3967 *pn != NULL;
3968 pn = &(*pn)->next)
3969 ;
3970 *pn = n;
3971 }
3972 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3973 if (!runpath && dyn.d_tag == DT_RPATH)
3974 {
3975 struct bfd_link_needed_list *n, **pn;
3976 char *fnm, *anm;
3977 unsigned int tagv = dyn.d_un.d_val;
3978
3979 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3980 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3981 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3982 if (n == NULL || fnm == NULL)
3983 goto error_free_dyn;
3984 amt = strlen (fnm) + 1;
a50b1753 3985 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3986 if (anm == NULL)
f8703194 3987 goto error_free_dyn;
4ad4eba5
AM
3988 memcpy (anm, fnm, amt);
3989 n->name = anm;
3990 n->by = abfd;
3991 n->next = NULL;
3992 for (pn = & rpath;
3993 *pn != NULL;
3994 pn = &(*pn)->next)
3995 ;
3996 *pn = n;
3997 }
7ee314fa
AM
3998 if (dyn.d_tag == DT_AUDIT)
3999 {
4000 unsigned int tagv = dyn.d_un.d_val;
4001 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4002 }
4ad4eba5
AM
4003 }
4004
4005 free (dynbuf);
4006 }
4007
4008 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4009 frees all more recently bfd_alloc'd blocks as well. */
4010 if (runpath)
4011 rpath = runpath;
4012
4013 if (rpath)
4014 {
4015 struct bfd_link_needed_list **pn;
66eb6687 4016 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4017 ;
4018 *pn = rpath;
4019 }
4020
9acc85a6
AM
4021 /* If we have a PT_GNU_RELRO program header, mark as read-only
4022 all sections contained fully therein. This makes relro
4023 shared library sections appear as they will at run-time. */
4024 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4025 while (--phdr >= elf_tdata (abfd)->phdr)
4026 if (phdr->p_type == PT_GNU_RELRO)
4027 {
4028 for (s = abfd->sections; s != NULL; s = s->next)
4029 if ((s->flags & SEC_ALLOC) != 0
4030 && s->vma >= phdr->p_vaddr
4031 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4032 s->flags |= SEC_READONLY;
4033 break;
4034 }
4035
4ad4eba5
AM
4036 /* We do not want to include any of the sections in a dynamic
4037 object in the output file. We hack by simply clobbering the
4038 list of sections in the BFD. This could be handled more
4039 cleanly by, say, a new section flag; the existing
4040 SEC_NEVER_LOAD flag is not the one we want, because that one
4041 still implies that the section takes up space in the output
4042 file. */
4043 bfd_section_list_clear (abfd);
4044
4ad4eba5
AM
4045 /* Find the name to use in a DT_NEEDED entry that refers to this
4046 object. If the object has a DT_SONAME entry, we use it.
4047 Otherwise, if the generic linker stuck something in
4048 elf_dt_name, we use that. Otherwise, we just use the file
4049 name. */
4050 if (soname == NULL || *soname == '\0')
4051 {
4052 soname = elf_dt_name (abfd);
4053 if (soname == NULL || *soname == '\0')
4054 soname = bfd_get_filename (abfd);
4055 }
4056
4057 /* Save the SONAME because sometimes the linker emulation code
4058 will need to know it. */
4059 elf_dt_name (abfd) = soname;
4060
7e9f0867 4061 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4062 if (ret < 0)
4063 goto error_return;
4064
4065 /* If we have already included this dynamic object in the
4066 link, just ignore it. There is no reason to include a
4067 particular dynamic object more than once. */
4068 if (ret > 0)
4069 return TRUE;
7ee314fa
AM
4070
4071 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4072 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4073 }
4074
4075 /* If this is a dynamic object, we always link against the .dynsym
4076 symbol table, not the .symtab symbol table. The dynamic linker
4077 will only see the .dynsym symbol table, so there is no reason to
4078 look at .symtab for a dynamic object. */
4079
4080 if (! dynamic || elf_dynsymtab (abfd) == 0)
4081 hdr = &elf_tdata (abfd)->symtab_hdr;
4082 else
4083 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4084
4085 symcount = hdr->sh_size / bed->s->sizeof_sym;
4086
4087 /* The sh_info field of the symtab header tells us where the
4088 external symbols start. We don't care about the local symbols at
4089 this point. */
4090 if (elf_bad_symtab (abfd))
4091 {
4092 extsymcount = symcount;
4093 extsymoff = 0;
4094 }
4095 else
4096 {
4097 extsymcount = symcount - hdr->sh_info;
4098 extsymoff = hdr->sh_info;
4099 }
4100
f45794cb 4101 sym_hash = elf_sym_hashes (abfd);
012b2306 4102 if (extsymcount != 0)
4ad4eba5
AM
4103 {
4104 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4105 NULL, NULL, NULL);
4106 if (isymbuf == NULL)
4107 goto error_return;
4108
4ad4eba5 4109 if (sym_hash == NULL)
012b2306
AM
4110 {
4111 /* We store a pointer to the hash table entry for each
4112 external symbol. */
ef53be89
AM
4113 amt = extsymcount;
4114 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4115 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4116 if (sym_hash == NULL)
4117 goto error_free_sym;
4118 elf_sym_hashes (abfd) = sym_hash;
4119 }
4ad4eba5
AM
4120 }
4121
4122 if (dynamic)
4123 {
4124 /* Read in any version definitions. */
fc0e6df6
PB
4125 if (!_bfd_elf_slurp_version_tables (abfd,
4126 info->default_imported_symver))
4ad4eba5
AM
4127 goto error_free_sym;
4128
4129 /* Read in the symbol versions, but don't bother to convert them
4130 to internal format. */
4131 if (elf_dynversym (abfd) != 0)
4132 {
4133 Elf_Internal_Shdr *versymhdr;
4134
4135 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4136 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4137 if (extversym == NULL)
4138 goto error_free_sym;
4139 amt = versymhdr->sh_size;
4140 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4141 || bfd_bread (extversym, amt, abfd) != amt)
4142 goto error_free_vers;
4143 }
4144 }
4145
66eb6687
AM
4146 /* If we are loading an as-needed shared lib, save the symbol table
4147 state before we start adding symbols. If the lib turns out
4148 to be unneeded, restore the state. */
4149 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4150 {
4151 unsigned int i;
4152 size_t entsize;
4153
4154 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4155 {
4156 struct bfd_hash_entry *p;
2de92251 4157 struct elf_link_hash_entry *h;
66eb6687
AM
4158
4159 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4160 {
4161 h = (struct elf_link_hash_entry *) p;
4162 entsize += htab->root.table.entsize;
4163 if (h->root.type == bfd_link_hash_warning)
4164 entsize += htab->root.table.entsize;
4165 }
66eb6687
AM
4166 }
4167
4168 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4169 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4170 if (old_tab == NULL)
4171 goto error_free_vers;
4172
4173 /* Remember the current objalloc pointer, so that all mem for
4174 symbols added can later be reclaimed. */
4175 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4176 if (alloc_mark == NULL)
4177 goto error_free_vers;
4178
5061a885
AM
4179 /* Make a special call to the linker "notice" function to
4180 tell it that we are about to handle an as-needed lib. */
e5034e59 4181 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4182 goto error_free_vers;
5061a885 4183
f45794cb
AM
4184 /* Clone the symbol table. Remember some pointers into the
4185 symbol table, and dynamic symbol count. */
4186 old_ent = (char *) old_tab + tabsize;
66eb6687 4187 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4188 old_undefs = htab->root.undefs;
4189 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4190 old_table = htab->root.table.table;
4191 old_size = htab->root.table.size;
4192 old_count = htab->root.table.count;
5b677558
AM
4193 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4194 if (old_strtab == NULL)
4195 goto error_free_vers;
66eb6687
AM
4196
4197 for (i = 0; i < htab->root.table.size; i++)
4198 {
4199 struct bfd_hash_entry *p;
2de92251 4200 struct elf_link_hash_entry *h;
66eb6687
AM
4201
4202 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4203 {
4204 memcpy (old_ent, p, htab->root.table.entsize);
4205 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4206 h = (struct elf_link_hash_entry *) p;
4207 if (h->root.type == bfd_link_hash_warning)
4208 {
4209 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4210 old_ent = (char *) old_ent + htab->root.table.entsize;
4211 }
66eb6687
AM
4212 }
4213 }
4214 }
4ad4eba5 4215
66eb6687 4216 weaks = NULL;
4ad4eba5
AM
4217 ever = extversym != NULL ? extversym + extsymoff : NULL;
4218 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4219 isym < isymend;
4220 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4221 {
4222 int bind;
4223 bfd_vma value;
af44c138 4224 asection *sec, *new_sec;
4ad4eba5
AM
4225 flagword flags;
4226 const char *name;
4227 struct elf_link_hash_entry *h;
90c984fc 4228 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4229 bfd_boolean definition;
4230 bfd_boolean size_change_ok;
4231 bfd_boolean type_change_ok;
4232 bfd_boolean new_weakdef;
37a9e49a
L
4233 bfd_boolean new_weak;
4234 bfd_boolean old_weak;
4ad4eba5 4235 bfd_boolean override;
a4d8e49b 4236 bfd_boolean common;
97196564 4237 bfd_boolean discarded;
4ad4eba5
AM
4238 unsigned int old_alignment;
4239 bfd *old_bfd;
6e33951e 4240 bfd_boolean matched;
4ad4eba5
AM
4241
4242 override = FALSE;
4243
4244 flags = BSF_NO_FLAGS;
4245 sec = NULL;
4246 value = isym->st_value;
a4d8e49b 4247 common = bed->common_definition (isym);
97196564 4248 discarded = FALSE;
4ad4eba5
AM
4249
4250 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4251 switch (bind)
4ad4eba5 4252 {
3e7a7d11 4253 case STB_LOCAL:
4ad4eba5
AM
4254 /* This should be impossible, since ELF requires that all
4255 global symbols follow all local symbols, and that sh_info
4256 point to the first global symbol. Unfortunately, Irix 5
4257 screws this up. */
4258 continue;
3e7a7d11
NC
4259
4260 case STB_GLOBAL:
a4d8e49b 4261 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4262 flags = BSF_GLOBAL;
3e7a7d11
NC
4263 break;
4264
4265 case STB_WEAK:
4266 flags = BSF_WEAK;
4267 break;
4268
4269 case STB_GNU_UNIQUE:
4270 flags = BSF_GNU_UNIQUE;
4271 break;
4272
4273 default:
4ad4eba5 4274 /* Leave it up to the processor backend. */
3e7a7d11 4275 break;
4ad4eba5
AM
4276 }
4277
4278 if (isym->st_shndx == SHN_UNDEF)
4279 sec = bfd_und_section_ptr;
cb33740c
AM
4280 else if (isym->st_shndx == SHN_ABS)
4281 sec = bfd_abs_section_ptr;
4282 else if (isym->st_shndx == SHN_COMMON)
4283 {
4284 sec = bfd_com_section_ptr;
4285 /* What ELF calls the size we call the value. What ELF
4286 calls the value we call the alignment. */
4287 value = isym->st_size;
4288 }
4289 else
4ad4eba5
AM
4290 {
4291 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4292 if (sec == NULL)
4293 sec = bfd_abs_section_ptr;
dbaa2011 4294 else if (discarded_section (sec))
529fcb95 4295 {
e5d08002
L
4296 /* Symbols from discarded section are undefined. We keep
4297 its visibility. */
529fcb95 4298 sec = bfd_und_section_ptr;
97196564 4299 discarded = TRUE;
529fcb95
PB
4300 isym->st_shndx = SHN_UNDEF;
4301 }
4ad4eba5
AM
4302 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4303 value -= sec->vma;
4304 }
4ad4eba5
AM
4305
4306 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4307 isym->st_name);
4308 if (name == NULL)
4309 goto error_free_vers;
4310
4311 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4312 && (abfd->flags & BFD_PLUGIN) != 0)
4313 {
4314 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4315
4316 if (xc == NULL)
4317 {
4318 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4319 | SEC_EXCLUDE);
4320 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4321 if (xc == NULL)
4322 goto error_free_vers;
4323 }
4324 sec = xc;
4325 }
4326 else if (isym->st_shndx == SHN_COMMON
4327 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4328 && !bfd_link_relocatable (info))
4ad4eba5
AM
4329 {
4330 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4331
4332 if (tcomm == NULL)
4333 {
02d00247
AM
4334 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4335 | SEC_LINKER_CREATED);
4336 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4337 if (tcomm == NULL)
4ad4eba5
AM
4338 goto error_free_vers;
4339 }
4340 sec = tcomm;
4341 }
66eb6687 4342 else if (bed->elf_add_symbol_hook)
4ad4eba5 4343 {
66eb6687
AM
4344 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4345 &sec, &value))
4ad4eba5
AM
4346 goto error_free_vers;
4347
4348 /* The hook function sets the name to NULL if this symbol
4349 should be skipped for some reason. */
4350 if (name == NULL)
4351 continue;
4352 }
4353
4354 /* Sanity check that all possibilities were handled. */
4355 if (sec == NULL)
4356 {
4357 bfd_set_error (bfd_error_bad_value);
4358 goto error_free_vers;
4359 }
4360
191c0c42
AM
4361 /* Silently discard TLS symbols from --just-syms. There's
4362 no way to combine a static TLS block with a new TLS block
4363 for this executable. */
4364 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4365 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4366 continue;
4367
4ad4eba5
AM
4368 if (bfd_is_und_section (sec)
4369 || bfd_is_com_section (sec))
4370 definition = FALSE;
4371 else
4372 definition = TRUE;
4373
4374 size_change_ok = FALSE;
66eb6687 4375 type_change_ok = bed->type_change_ok;
37a9e49a 4376 old_weak = FALSE;
6e33951e 4377 matched = FALSE;
4ad4eba5
AM
4378 old_alignment = 0;
4379 old_bfd = NULL;
af44c138 4380 new_sec = sec;
4ad4eba5 4381
66eb6687 4382 if (is_elf_hash_table (htab))
4ad4eba5
AM
4383 {
4384 Elf_Internal_Versym iver;
4385 unsigned int vernum = 0;
4386 bfd_boolean skip;
4387
fc0e6df6 4388 if (ever == NULL)
4ad4eba5 4389 {
fc0e6df6
PB
4390 if (info->default_imported_symver)
4391 /* Use the default symbol version created earlier. */
4392 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4393 else
4394 iver.vs_vers = 0;
4395 }
4396 else
4397 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4398
4399 vernum = iver.vs_vers & VERSYM_VERSION;
4400
4401 /* If this is a hidden symbol, or if it is not version
4402 1, we append the version name to the symbol name.
cc86ff91
EB
4403 However, we do not modify a non-hidden absolute symbol
4404 if it is not a function, because it might be the version
4405 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4406 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4407 || (vernum > 1
4408 && (!bfd_is_abs_section (sec)
4409 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4410 {
4411 const char *verstr;
4412 size_t namelen, verlen, newlen;
4413 char *newname, *p;
4414
4415 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4416 {
fc0e6df6
PB
4417 if (vernum > elf_tdata (abfd)->cverdefs)
4418 verstr = NULL;
4419 else if (vernum > 1)
4420 verstr =
4421 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4422 else
4423 verstr = "";
4ad4eba5 4424
fc0e6df6 4425 if (verstr == NULL)
4ad4eba5 4426 {
4eca0228 4427 _bfd_error_handler
695344c0 4428 /* xgettext:c-format */
fc0e6df6
PB
4429 (_("%B: %s: invalid version %u (max %d)"),
4430 abfd, name, vernum,
4431 elf_tdata (abfd)->cverdefs);
4432 bfd_set_error (bfd_error_bad_value);
4433 goto error_free_vers;
4ad4eba5 4434 }
fc0e6df6
PB
4435 }
4436 else
4437 {
4438 /* We cannot simply test for the number of
4439 entries in the VERNEED section since the
4440 numbers for the needed versions do not start
4441 at 0. */
4442 Elf_Internal_Verneed *t;
4443
4444 verstr = NULL;
4445 for (t = elf_tdata (abfd)->verref;
4446 t != NULL;
4447 t = t->vn_nextref)
4ad4eba5 4448 {
fc0e6df6 4449 Elf_Internal_Vernaux *a;
4ad4eba5 4450
fc0e6df6
PB
4451 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4452 {
4453 if (a->vna_other == vernum)
4ad4eba5 4454 {
fc0e6df6
PB
4455 verstr = a->vna_nodename;
4456 break;
4ad4eba5 4457 }
4ad4eba5 4458 }
fc0e6df6
PB
4459 if (a != NULL)
4460 break;
4461 }
4462 if (verstr == NULL)
4463 {
4eca0228 4464 _bfd_error_handler
695344c0 4465 /* xgettext:c-format */
fc0e6df6
PB
4466 (_("%B: %s: invalid needed version %d"),
4467 abfd, name, vernum);
4468 bfd_set_error (bfd_error_bad_value);
4469 goto error_free_vers;
4ad4eba5 4470 }
4ad4eba5 4471 }
fc0e6df6
PB
4472
4473 namelen = strlen (name);
4474 verlen = strlen (verstr);
4475 newlen = namelen + verlen + 2;
4476 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4477 && isym->st_shndx != SHN_UNDEF)
4478 ++newlen;
4479
a50b1753 4480 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4481 if (newname == NULL)
4482 goto error_free_vers;
4483 memcpy (newname, name, namelen);
4484 p = newname + namelen;
4485 *p++ = ELF_VER_CHR;
4486 /* If this is a defined non-hidden version symbol,
4487 we add another @ to the name. This indicates the
4488 default version of the symbol. */
4489 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4490 && isym->st_shndx != SHN_UNDEF)
4491 *p++ = ELF_VER_CHR;
4492 memcpy (p, verstr, verlen + 1);
4493
4494 name = newname;
4ad4eba5
AM
4495 }
4496
cd3416da
AM
4497 /* If this symbol has default visibility and the user has
4498 requested we not re-export it, then mark it as hidden. */
a0d49154 4499 if (!bfd_is_und_section (sec)
cd3416da 4500 && !dynamic
ce875075 4501 && abfd->no_export
cd3416da
AM
4502 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4503 isym->st_other = (STV_HIDDEN
4504 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4505
4f3fedcf
AM
4506 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4507 sym_hash, &old_bfd, &old_weak,
4508 &old_alignment, &skip, &override,
6e33951e
L
4509 &type_change_ok, &size_change_ok,
4510 &matched))
4ad4eba5
AM
4511 goto error_free_vers;
4512
4513 if (skip)
4514 continue;
4515
6e33951e
L
4516 /* Override a definition only if the new symbol matches the
4517 existing one. */
4518 if (override && matched)
4ad4eba5
AM
4519 definition = FALSE;
4520
4521 h = *sym_hash;
4522 while (h->root.type == bfd_link_hash_indirect
4523 || h->root.type == bfd_link_hash_warning)
4524 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4525
4ad4eba5 4526 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4527 && vernum > 1
4528 && definition)
4529 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4530 }
4531
4532 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4533 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4534 (struct bfd_link_hash_entry **) sym_hash)))
4535 goto error_free_vers;
4536
a43942db
MR
4537 if ((flags & BSF_GNU_UNIQUE)
4538 && (abfd->flags & DYNAMIC) == 0
4539 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4540 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4541
4ad4eba5 4542 h = *sym_hash;
90c984fc
L
4543 /* We need to make sure that indirect symbol dynamic flags are
4544 updated. */
4545 hi = h;
4ad4eba5
AM
4546 while (h->root.type == bfd_link_hash_indirect
4547 || h->root.type == bfd_link_hash_warning)
4548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4549
97196564
L
4550 /* Setting the index to -3 tells elf_link_output_extsym that
4551 this symbol is defined in a discarded section. */
4552 if (discarded)
4553 h->indx = -3;
4554
4ad4eba5
AM
4555 *sym_hash = h;
4556
37a9e49a 4557 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4558 new_weakdef = FALSE;
4559 if (dynamic
4560 && definition
37a9e49a 4561 && new_weak
fcb93ecf 4562 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4563 && is_elf_hash_table (htab)
f6e332e6 4564 && h->u.weakdef == NULL)
4ad4eba5
AM
4565 {
4566 /* Keep a list of all weak defined non function symbols from
4567 a dynamic object, using the weakdef field. Later in this
4568 function we will set the weakdef field to the correct
4569 value. We only put non-function symbols from dynamic
4570 objects on this list, because that happens to be the only
4571 time we need to know the normal symbol corresponding to a
4572 weak symbol, and the information is time consuming to
4573 figure out. If the weakdef field is not already NULL,
4574 then this symbol was already defined by some previous
4575 dynamic object, and we will be using that previous
4576 definition anyhow. */
4577
f6e332e6 4578 h->u.weakdef = weaks;
4ad4eba5
AM
4579 weaks = h;
4580 new_weakdef = TRUE;
4581 }
4582
4583 /* Set the alignment of a common symbol. */
a4d8e49b 4584 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4585 && h->root.type == bfd_link_hash_common)
4586 {
4587 unsigned int align;
4588
a4d8e49b 4589 if (common)
af44c138
L
4590 align = bfd_log2 (isym->st_value);
4591 else
4592 {
4593 /* The new symbol is a common symbol in a shared object.
4594 We need to get the alignment from the section. */
4595 align = new_sec->alignment_power;
4596 }
595213d4 4597 if (align > old_alignment)
4ad4eba5
AM
4598 h->root.u.c.p->alignment_power = align;
4599 else
4600 h->root.u.c.p->alignment_power = old_alignment;
4601 }
4602
66eb6687 4603 if (is_elf_hash_table (htab))
4ad4eba5 4604 {
4f3fedcf
AM
4605 /* Set a flag in the hash table entry indicating the type of
4606 reference or definition we just found. A dynamic symbol
4607 is one which is referenced or defined by both a regular
4608 object and a shared object. */
4609 bfd_boolean dynsym = FALSE;
4610
4611 /* Plugin symbols aren't normal. Don't set def_regular or
4612 ref_regular for them, or make them dynamic. */
4613 if ((abfd->flags & BFD_PLUGIN) != 0)
4614 ;
4615 else if (! dynamic)
4616 {
4617 if (! definition)
4618 {
4619 h->ref_regular = 1;
4620 if (bind != STB_WEAK)
4621 h->ref_regular_nonweak = 1;
4622 }
4623 else
4624 {
4625 h->def_regular = 1;
4626 if (h->def_dynamic)
4627 {
4628 h->def_dynamic = 0;
4629 h->ref_dynamic = 1;
4630 }
4631 }
4632
4633 /* If the indirect symbol has been forced local, don't
4634 make the real symbol dynamic. */
4635 if ((h == hi || !hi->forced_local)
0e1862bb 4636 && (bfd_link_dll (info)
4f3fedcf
AM
4637 || h->def_dynamic
4638 || h->ref_dynamic))
4639 dynsym = TRUE;
4640 }
4641 else
4642 {
4643 if (! definition)
4644 {
4645 h->ref_dynamic = 1;
4646 hi->ref_dynamic = 1;
4647 }
4648 else
4649 {
4650 h->def_dynamic = 1;
4651 hi->def_dynamic = 1;
4652 }
4653
4654 /* If the indirect symbol has been forced local, don't
4655 make the real symbol dynamic. */
4656 if ((h == hi || !hi->forced_local)
4657 && (h->def_regular
4658 || h->ref_regular
4659 || (h->u.weakdef != NULL
4660 && ! new_weakdef
4661 && h->u.weakdef->dynindx != -1)))
4662 dynsym = TRUE;
4663 }
4664
4665 /* Check to see if we need to add an indirect symbol for
4666 the default name. */
4667 if (definition
4668 || (!override && h->root.type == bfd_link_hash_common))
4669 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4670 sec, value, &old_bfd, &dynsym))
4671 goto error_free_vers;
4ad4eba5
AM
4672
4673 /* Check the alignment when a common symbol is involved. This
4674 can change when a common symbol is overridden by a normal
4675 definition or a common symbol is ignored due to the old
4676 normal definition. We need to make sure the maximum
4677 alignment is maintained. */
a4d8e49b 4678 if ((old_alignment || common)
4ad4eba5
AM
4679 && h->root.type != bfd_link_hash_common)
4680 {
4681 unsigned int common_align;
4682 unsigned int normal_align;
4683 unsigned int symbol_align;
4684 bfd *normal_bfd;
4685 bfd *common_bfd;
4686
3a81e825
AM
4687 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4688 || h->root.type == bfd_link_hash_defweak);
4689
4ad4eba5
AM
4690 symbol_align = ffs (h->root.u.def.value) - 1;
4691 if (h->root.u.def.section->owner != NULL
0616a280
AM
4692 && (h->root.u.def.section->owner->flags
4693 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4694 {
4695 normal_align = h->root.u.def.section->alignment_power;
4696 if (normal_align > symbol_align)
4697 normal_align = symbol_align;
4698 }
4699 else
4700 normal_align = symbol_align;
4701
4702 if (old_alignment)
4703 {
4704 common_align = old_alignment;
4705 common_bfd = old_bfd;
4706 normal_bfd = abfd;
4707 }
4708 else
4709 {
4710 common_align = bfd_log2 (isym->st_value);
4711 common_bfd = abfd;
4712 normal_bfd = old_bfd;
4713 }
4714
4715 if (normal_align < common_align)
d07676f8
NC
4716 {
4717 /* PR binutils/2735 */
4718 if (normal_bfd == NULL)
4eca0228 4719 _bfd_error_handler
695344c0 4720 /* xgettext:c-format */
4f3fedcf
AM
4721 (_("Warning: alignment %u of common symbol `%s' in %B is"
4722 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4723 1 << common_align, name, common_bfd,
4724 1 << normal_align, h->root.u.def.section);
d07676f8 4725 else
4eca0228 4726 _bfd_error_handler
695344c0 4727 /* xgettext:c-format */
d07676f8
NC
4728 (_("Warning: alignment %u of symbol `%s' in %B"
4729 " is smaller than %u in %B"),
c08bb8dd
AM
4730 1 << normal_align, name, normal_bfd,
4731 1 << common_align, common_bfd);
d07676f8 4732 }
4ad4eba5
AM
4733 }
4734
83ad0046 4735 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4736 if (isym->st_size != 0
4737 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4738 && (definition || h->size == 0))
4739 {
83ad0046
L
4740 if (h->size != 0
4741 && h->size != isym->st_size
4742 && ! size_change_ok)
4eca0228 4743 _bfd_error_handler
695344c0 4744 /* xgettext:c-format */
d003868e
AM
4745 (_("Warning: size of symbol `%s' changed"
4746 " from %lu in %B to %lu in %B"),
c08bb8dd
AM
4747 name, (unsigned long) h->size, old_bfd,
4748 (unsigned long) isym->st_size, abfd);
4ad4eba5
AM
4749
4750 h->size = isym->st_size;
4751 }
4752
4753 /* If this is a common symbol, then we always want H->SIZE
4754 to be the size of the common symbol. The code just above
4755 won't fix the size if a common symbol becomes larger. We
4756 don't warn about a size change here, because that is
4f3fedcf 4757 covered by --warn-common. Allow changes between different
fcb93ecf 4758 function types. */
4ad4eba5
AM
4759 if (h->root.type == bfd_link_hash_common)
4760 h->size = h->root.u.c.size;
4761
4762 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4763 && ((definition && !new_weak)
4764 || (old_weak && h->root.type == bfd_link_hash_common)
4765 || h->type == STT_NOTYPE))
4ad4eba5 4766 {
2955ec4c
L
4767 unsigned int type = ELF_ST_TYPE (isym->st_info);
4768
4769 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4770 symbol. */
4771 if (type == STT_GNU_IFUNC
4772 && (abfd->flags & DYNAMIC) != 0)
4773 type = STT_FUNC;
4ad4eba5 4774
2955ec4c
L
4775 if (h->type != type)
4776 {
4777 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4778 /* xgettext:c-format */
4eca0228 4779 _bfd_error_handler
2955ec4c
L
4780 (_("Warning: type of symbol `%s' changed"
4781 " from %d to %d in %B"),
c08bb8dd 4782 name, h->type, type, abfd);
2955ec4c
L
4783
4784 h->type = type;
4785 }
4ad4eba5
AM
4786 }
4787
54ac0771 4788 /* Merge st_other field. */
b8417128 4789 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4790
c3df8c14 4791 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4792 if (definition
4793 && (sec->flags & SEC_DEBUGGING)
4794 && !bfd_link_relocatable (info))
c3df8c14
AM
4795 dynsym = FALSE;
4796
4f3fedcf
AM
4797 /* Nor should we make plugin symbols dynamic. */
4798 if ((abfd->flags & BFD_PLUGIN) != 0)
4799 dynsym = FALSE;
4800
35fc36a8 4801 if (definition)
35399224
L
4802 {
4803 h->target_internal = isym->st_target_internal;
4804 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4805 }
35fc36a8 4806
4ad4eba5
AM
4807 if (definition && !dynamic)
4808 {
4809 char *p = strchr (name, ELF_VER_CHR);
4810 if (p != NULL && p[1] != ELF_VER_CHR)
4811 {
4812 /* Queue non-default versions so that .symver x, x@FOO
4813 aliases can be checked. */
66eb6687 4814 if (!nondeflt_vers)
4ad4eba5 4815 {
66eb6687
AM
4816 amt = ((isymend - isym + 1)
4817 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4818 nondeflt_vers
4819 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4820 if (!nondeflt_vers)
4821 goto error_free_vers;
4ad4eba5 4822 }
66eb6687 4823 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4824 }
4825 }
4826
4827 if (dynsym && h->dynindx == -1)
4828 {
c152c796 4829 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4830 goto error_free_vers;
f6e332e6 4831 if (h->u.weakdef != NULL
4ad4eba5 4832 && ! new_weakdef
f6e332e6 4833 && h->u.weakdef->dynindx == -1)
4ad4eba5 4834 {
66eb6687 4835 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4836 goto error_free_vers;
4837 }
4838 }
1f599d0e 4839 else if (h->dynindx != -1)
4ad4eba5
AM
4840 /* If the symbol already has a dynamic index, but
4841 visibility says it should not be visible, turn it into
4842 a local symbol. */
4843 switch (ELF_ST_VISIBILITY (h->other))
4844 {
4845 case STV_INTERNAL:
4846 case STV_HIDDEN:
4847 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4848 dynsym = FALSE;
4849 break;
4850 }
4851
aef28989
L
4852 /* Don't add DT_NEEDED for references from the dummy bfd nor
4853 for unmatched symbol. */
4ad4eba5 4854 if (!add_needed
aef28989 4855 && matched
4ad4eba5 4856 && definition
010e5ae2 4857 && ((dynsym
ffa9430d 4858 && h->ref_regular_nonweak
4f3fedcf
AM
4859 && (old_bfd == NULL
4860 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4861 || (h->ref_dynamic_nonweak
010e5ae2 4862 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4863 && !on_needed_list (elf_dt_name (abfd),
4864 htab->needed, NULL))))
4ad4eba5
AM
4865 {
4866 int ret;
4867 const char *soname = elf_dt_name (abfd);
4868
16e4ecc0
AM
4869 info->callbacks->minfo ("%!", soname, old_bfd,
4870 h->root.root.string);
4871
4ad4eba5
AM
4872 /* A symbol from a library loaded via DT_NEEDED of some
4873 other library is referenced by a regular object.
e56f61be 4874 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4875 --no-add-needed is used and the reference was not
4876 a weak one. */
4f3fedcf 4877 if (old_bfd != NULL
b918acf9 4878 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4879 {
4eca0228 4880 _bfd_error_handler
695344c0 4881 /* xgettext:c-format */
3cbc5de0 4882 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4883 old_bfd, name);
ff5ac77b 4884 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4885 goto error_free_vers;
4886 }
4887
a50b1753 4888 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4889 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4890
4ad4eba5 4891 add_needed = TRUE;
7e9f0867 4892 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4893 if (ret < 0)
4894 goto error_free_vers;
4895
4896 BFD_ASSERT (ret == 0);
4897 }
4898 }
4899 }
4900
66eb6687
AM
4901 if (extversym != NULL)
4902 {
4903 free (extversym);
4904 extversym = NULL;
4905 }
4906
4907 if (isymbuf != NULL)
4908 {
4909 free (isymbuf);
4910 isymbuf = NULL;
4911 }
4912
4913 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4914 {
4915 unsigned int i;
4916
4917 /* Restore the symbol table. */
f45794cb
AM
4918 old_ent = (char *) old_tab + tabsize;
4919 memset (elf_sym_hashes (abfd), 0,
4920 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4921 htab->root.table.table = old_table;
4922 htab->root.table.size = old_size;
4923 htab->root.table.count = old_count;
66eb6687 4924 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4925 htab->root.undefs = old_undefs;
4926 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4927 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4928 free (old_strtab);
4929 old_strtab = NULL;
66eb6687
AM
4930 for (i = 0; i < htab->root.table.size; i++)
4931 {
4932 struct bfd_hash_entry *p;
4933 struct elf_link_hash_entry *h;
3e0882af
L
4934 bfd_size_type size;
4935 unsigned int alignment_power;
59fa66c5 4936 unsigned int dynamic_ref_after_ir_def;
66eb6687
AM
4937
4938 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4939 {
4940 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4941 if (h->root.type == bfd_link_hash_warning)
4942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4943
3e0882af
L
4944 /* Preserve the maximum alignment and size for common
4945 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4946 since it can still be loaded at run time by another
3e0882af
L
4947 dynamic lib. */
4948 if (h->root.type == bfd_link_hash_common)
4949 {
4950 size = h->root.u.c.size;
4951 alignment_power = h->root.u.c.p->alignment_power;
4952 }
4953 else
4954 {
4955 size = 0;
4956 alignment_power = 0;
4957 }
59fa66c5
L
4958 /* Preserve dynamic_ref_after_ir_def so that this symbol
4959 will be exported when the dynamic lib becomes needed
4960 in the second pass. */
4961 dynamic_ref_after_ir_def = h->root.dynamic_ref_after_ir_def;
66eb6687
AM
4962 memcpy (p, old_ent, htab->root.table.entsize);
4963 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4964 h = (struct elf_link_hash_entry *) p;
4965 if (h->root.type == bfd_link_hash_warning)
4966 {
4967 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4968 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4969 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4970 }
a4542f1b 4971 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4972 {
4973 if (size > h->root.u.c.size)
4974 h->root.u.c.size = size;
4975 if (alignment_power > h->root.u.c.p->alignment_power)
4976 h->root.u.c.p->alignment_power = alignment_power;
4977 }
59fa66c5 4978 h->root.dynamic_ref_after_ir_def = dynamic_ref_after_ir_def;
66eb6687
AM
4979 }
4980 }
4981
5061a885
AM
4982 /* Make a special call to the linker "notice" function to
4983 tell it that symbols added for crefs may need to be removed. */
e5034e59 4984 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4985 goto error_free_vers;
5061a885 4986
66eb6687
AM
4987 free (old_tab);
4988 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4989 alloc_mark);
4990 if (nondeflt_vers != NULL)
4991 free (nondeflt_vers);
4992 return TRUE;
4993 }
2de92251 4994
66eb6687
AM
4995 if (old_tab != NULL)
4996 {
e5034e59 4997 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4998 goto error_free_vers;
66eb6687
AM
4999 free (old_tab);
5000 old_tab = NULL;
5001 }
5002
c6e8a9a8
L
5003 /* Now that all the symbols from this input file are created, if
5004 not performing a relocatable link, handle .symver foo, foo@BAR
5005 such that any relocs against foo become foo@BAR. */
0e1862bb 5006 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5007 {
ef53be89 5008 size_t cnt, symidx;
4ad4eba5
AM
5009
5010 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5011 {
5012 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5013 char *shortname, *p;
5014
5015 p = strchr (h->root.root.string, ELF_VER_CHR);
5016 if (p == NULL
5017 || (h->root.type != bfd_link_hash_defined
5018 && h->root.type != bfd_link_hash_defweak))
5019 continue;
5020
5021 amt = p - h->root.root.string;
a50b1753 5022 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5023 if (!shortname)
5024 goto error_free_vers;
4ad4eba5
AM
5025 memcpy (shortname, h->root.root.string, amt);
5026 shortname[amt] = '\0';
5027
5028 hi = (struct elf_link_hash_entry *)
66eb6687 5029 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5030 FALSE, FALSE, FALSE);
5031 if (hi != NULL
5032 && hi->root.type == h->root.type
5033 && hi->root.u.def.value == h->root.u.def.value
5034 && hi->root.u.def.section == h->root.u.def.section)
5035 {
5036 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5037 hi->root.type = bfd_link_hash_indirect;
5038 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5039 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5040 sym_hash = elf_sym_hashes (abfd);
5041 if (sym_hash)
5042 for (symidx = 0; symidx < extsymcount; ++symidx)
5043 if (sym_hash[symidx] == hi)
5044 {
5045 sym_hash[symidx] = h;
5046 break;
5047 }
5048 }
5049 free (shortname);
5050 }
5051 free (nondeflt_vers);
5052 nondeflt_vers = NULL;
5053 }
5054
4ad4eba5
AM
5055 /* Now set the weakdefs field correctly for all the weak defined
5056 symbols we found. The only way to do this is to search all the
5057 symbols. Since we only need the information for non functions in
5058 dynamic objects, that's the only time we actually put anything on
5059 the list WEAKS. We need this information so that if a regular
5060 object refers to a symbol defined weakly in a dynamic object, the
5061 real symbol in the dynamic object is also put in the dynamic
5062 symbols; we also must arrange for both symbols to point to the
5063 same memory location. We could handle the general case of symbol
5064 aliasing, but a general symbol alias can only be generated in
5065 assembler code, handling it correctly would be very time
5066 consuming, and other ELF linkers don't handle general aliasing
5067 either. */
5068 if (weaks != NULL)
5069 {
5070 struct elf_link_hash_entry **hpp;
5071 struct elf_link_hash_entry **hppend;
5072 struct elf_link_hash_entry **sorted_sym_hash;
5073 struct elf_link_hash_entry *h;
5074 size_t sym_count;
5075
5076 /* Since we have to search the whole symbol list for each weak
5077 defined symbol, search time for N weak defined symbols will be
5078 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5079 amt = extsymcount;
5080 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5081 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5082 if (sorted_sym_hash == NULL)
5083 goto error_return;
5084 sym_hash = sorted_sym_hash;
5085 hpp = elf_sym_hashes (abfd);
5086 hppend = hpp + extsymcount;
5087 sym_count = 0;
5088 for (; hpp < hppend; hpp++)
5089 {
5090 h = *hpp;
5091 if (h != NULL
5092 && h->root.type == bfd_link_hash_defined
fcb93ecf 5093 && !bed->is_function_type (h->type))
4ad4eba5
AM
5094 {
5095 *sym_hash = h;
5096 sym_hash++;
5097 sym_count++;
5098 }
5099 }
5100
5101 qsort (sorted_sym_hash, sym_count,
5102 sizeof (struct elf_link_hash_entry *),
5103 elf_sort_symbol);
5104
5105 while (weaks != NULL)
5106 {
5107 struct elf_link_hash_entry *hlook;
5108 asection *slook;
5109 bfd_vma vlook;
ed54588d 5110 size_t i, j, idx = 0;
4ad4eba5
AM
5111
5112 hlook = weaks;
f6e332e6
AM
5113 weaks = hlook->u.weakdef;
5114 hlook->u.weakdef = NULL;
4ad4eba5
AM
5115
5116 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5117 || hlook->root.type == bfd_link_hash_defweak
5118 || hlook->root.type == bfd_link_hash_common
5119 || hlook->root.type == bfd_link_hash_indirect);
5120 slook = hlook->root.u.def.section;
5121 vlook = hlook->root.u.def.value;
5122
4ad4eba5
AM
5123 i = 0;
5124 j = sym_count;
14160578 5125 while (i != j)
4ad4eba5
AM
5126 {
5127 bfd_signed_vma vdiff;
5128 idx = (i + j) / 2;
14160578 5129 h = sorted_sym_hash[idx];
4ad4eba5
AM
5130 vdiff = vlook - h->root.u.def.value;
5131 if (vdiff < 0)
5132 j = idx;
5133 else if (vdiff > 0)
5134 i = idx + 1;
5135 else
5136 {
d3435ae8 5137 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5138 if (sdiff < 0)
5139 j = idx;
5140 else if (sdiff > 0)
5141 i = idx + 1;
5142 else
14160578 5143 break;
4ad4eba5
AM
5144 }
5145 }
5146
5147 /* We didn't find a value/section match. */
14160578 5148 if (i == j)
4ad4eba5
AM
5149 continue;
5150
14160578
AM
5151 /* With multiple aliases, or when the weak symbol is already
5152 strongly defined, we have multiple matching symbols and
5153 the binary search above may land on any of them. Step
5154 one past the matching symbol(s). */
5155 while (++idx != j)
5156 {
5157 h = sorted_sym_hash[idx];
5158 if (h->root.u.def.section != slook
5159 || h->root.u.def.value != vlook)
5160 break;
5161 }
5162
5163 /* Now look back over the aliases. Since we sorted by size
5164 as well as value and section, we'll choose the one with
5165 the largest size. */
5166 while (idx-- != i)
4ad4eba5 5167 {
14160578 5168 h = sorted_sym_hash[idx];
4ad4eba5
AM
5169
5170 /* Stop if value or section doesn't match. */
14160578
AM
5171 if (h->root.u.def.section != slook
5172 || h->root.u.def.value != vlook)
4ad4eba5
AM
5173 break;
5174 else if (h != hlook)
5175 {
f6e332e6 5176 hlook->u.weakdef = h;
4ad4eba5
AM
5177
5178 /* If the weak definition is in the list of dynamic
5179 symbols, make sure the real definition is put
5180 there as well. */
5181 if (hlook->dynindx != -1 && h->dynindx == -1)
5182 {
c152c796 5183 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5184 {
5185 err_free_sym_hash:
5186 free (sorted_sym_hash);
5187 goto error_return;
5188 }
4ad4eba5
AM
5189 }
5190
5191 /* If the real definition is in the list of dynamic
5192 symbols, make sure the weak definition is put
5193 there as well. If we don't do this, then the
5194 dynamic loader might not merge the entries for the
5195 real definition and the weak definition. */
5196 if (h->dynindx != -1 && hlook->dynindx == -1)
5197 {
c152c796 5198 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5199 goto err_free_sym_hash;
4ad4eba5
AM
5200 }
5201 break;
5202 }
5203 }
5204 }
5205
5206 free (sorted_sym_hash);
5207 }
5208
33177bb1
AM
5209 if (bed->check_directives
5210 && !(*bed->check_directives) (abfd, info))
5211 return FALSE;
85fbca6a 5212
d9689752
L
5213 if (!info->check_relocs_after_open_input
5214 && !_bfd_elf_link_check_relocs (abfd, info))
5215 return FALSE;
4ad4eba5
AM
5216
5217 /* If this is a non-traditional link, try to optimize the handling
5218 of the .stab/.stabstr sections. */
5219 if (! dynamic
5220 && ! info->traditional_format
66eb6687 5221 && is_elf_hash_table (htab)
4ad4eba5
AM
5222 && (info->strip != strip_all && info->strip != strip_debugger))
5223 {
5224 asection *stabstr;
5225
5226 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5227 if (stabstr != NULL)
5228 {
5229 bfd_size_type string_offset = 0;
5230 asection *stab;
5231
5232 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5233 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5234 && (!stab->name[5] ||
5235 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5236 && (stab->flags & SEC_MERGE) == 0
5237 && !bfd_is_abs_section (stab->output_section))
5238 {
5239 struct bfd_elf_section_data *secdata;
5240
5241 secdata = elf_section_data (stab);
66eb6687
AM
5242 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5243 stabstr, &secdata->sec_info,
4ad4eba5
AM
5244 &string_offset))
5245 goto error_return;
5246 if (secdata->sec_info)
dbaa2011 5247 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5248 }
5249 }
5250 }
5251
66eb6687 5252 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5253 {
5254 /* Add this bfd to the loaded list. */
5255 struct elf_link_loaded_list *n;
5256
ca4be51c 5257 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5258 if (n == NULL)
5259 goto error_return;
5260 n->abfd = abfd;
66eb6687
AM
5261 n->next = htab->loaded;
5262 htab->loaded = n;
4ad4eba5
AM
5263 }
5264
5265 return TRUE;
5266
5267 error_free_vers:
66eb6687
AM
5268 if (old_tab != NULL)
5269 free (old_tab);
5b677558
AM
5270 if (old_strtab != NULL)
5271 free (old_strtab);
4ad4eba5
AM
5272 if (nondeflt_vers != NULL)
5273 free (nondeflt_vers);
5274 if (extversym != NULL)
5275 free (extversym);
5276 error_free_sym:
5277 if (isymbuf != NULL)
5278 free (isymbuf);
5279 error_return:
5280 return FALSE;
5281}
5282
8387904d
AM
5283/* Return the linker hash table entry of a symbol that might be
5284 satisfied by an archive symbol. Return -1 on error. */
5285
5286struct elf_link_hash_entry *
5287_bfd_elf_archive_symbol_lookup (bfd *abfd,
5288 struct bfd_link_info *info,
5289 const char *name)
5290{
5291 struct elf_link_hash_entry *h;
5292 char *p, *copy;
5293 size_t len, first;
5294
2a41f396 5295 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5296 if (h != NULL)
5297 return h;
5298
5299 /* If this is a default version (the name contains @@), look up the
5300 symbol again with only one `@' as well as without the version.
5301 The effect is that references to the symbol with and without the
5302 version will be matched by the default symbol in the archive. */
5303
5304 p = strchr (name, ELF_VER_CHR);
5305 if (p == NULL || p[1] != ELF_VER_CHR)
5306 return h;
5307
5308 /* First check with only one `@'. */
5309 len = strlen (name);
a50b1753 5310 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5311 if (copy == NULL)
5312 return (struct elf_link_hash_entry *) 0 - 1;
5313
5314 first = p - name + 1;
5315 memcpy (copy, name, first);
5316 memcpy (copy + first, name + first + 1, len - first);
5317
2a41f396 5318 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5319 if (h == NULL)
5320 {
5321 /* We also need to check references to the symbol without the
5322 version. */
5323 copy[first - 1] = '\0';
5324 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5325 FALSE, FALSE, TRUE);
8387904d
AM
5326 }
5327
5328 bfd_release (abfd, copy);
5329 return h;
5330}
5331
0ad989f9 5332/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5333 don't use _bfd_generic_link_add_archive_symbols because we need to
5334 handle versioned symbols.
0ad989f9
L
5335
5336 Fortunately, ELF archive handling is simpler than that done by
5337 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5338 oddities. In ELF, if we find a symbol in the archive map, and the
5339 symbol is currently undefined, we know that we must pull in that
5340 object file.
5341
5342 Unfortunately, we do have to make multiple passes over the symbol
5343 table until nothing further is resolved. */
5344
4ad4eba5
AM
5345static bfd_boolean
5346elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5347{
5348 symindex c;
13e570f8 5349 unsigned char *included = NULL;
0ad989f9
L
5350 carsym *symdefs;
5351 bfd_boolean loop;
5352 bfd_size_type amt;
8387904d
AM
5353 const struct elf_backend_data *bed;
5354 struct elf_link_hash_entry * (*archive_symbol_lookup)
5355 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5356
5357 if (! bfd_has_map (abfd))
5358 {
5359 /* An empty archive is a special case. */
5360 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5361 return TRUE;
5362 bfd_set_error (bfd_error_no_armap);
5363 return FALSE;
5364 }
5365
5366 /* Keep track of all symbols we know to be already defined, and all
5367 files we know to be already included. This is to speed up the
5368 second and subsequent passes. */
5369 c = bfd_ardata (abfd)->symdef_count;
5370 if (c == 0)
5371 return TRUE;
5372 amt = c;
13e570f8
AM
5373 amt *= sizeof (*included);
5374 included = (unsigned char *) bfd_zmalloc (amt);
5375 if (included == NULL)
5376 return FALSE;
0ad989f9
L
5377
5378 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5379 bed = get_elf_backend_data (abfd);
5380 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5381
5382 do
5383 {
5384 file_ptr last;
5385 symindex i;
5386 carsym *symdef;
5387 carsym *symdefend;
5388
5389 loop = FALSE;
5390 last = -1;
5391
5392 symdef = symdefs;
5393 symdefend = symdef + c;
5394 for (i = 0; symdef < symdefend; symdef++, i++)
5395 {
5396 struct elf_link_hash_entry *h;
5397 bfd *element;
5398 struct bfd_link_hash_entry *undefs_tail;
5399 symindex mark;
5400
13e570f8 5401 if (included[i])
0ad989f9
L
5402 continue;
5403 if (symdef->file_offset == last)
5404 {
5405 included[i] = TRUE;
5406 continue;
5407 }
5408
8387904d
AM
5409 h = archive_symbol_lookup (abfd, info, symdef->name);
5410 if (h == (struct elf_link_hash_entry *) 0 - 1)
5411 goto error_return;
0ad989f9
L
5412
5413 if (h == NULL)
5414 continue;
5415
5416 if (h->root.type == bfd_link_hash_common)
5417 {
5418 /* We currently have a common symbol. The archive map contains
5419 a reference to this symbol, so we may want to include it. We
5420 only want to include it however, if this archive element
5421 contains a definition of the symbol, not just another common
5422 declaration of it.
5423
5424 Unfortunately some archivers (including GNU ar) will put
5425 declarations of common symbols into their archive maps, as
5426 well as real definitions, so we cannot just go by the archive
5427 map alone. Instead we must read in the element's symbol
5428 table and check that to see what kind of symbol definition
5429 this is. */
5430 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5431 continue;
5432 }
5433 else if (h->root.type != bfd_link_hash_undefined)
5434 {
5435 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5436 /* Symbol must be defined. Don't check it again. */
5437 included[i] = TRUE;
0ad989f9
L
5438 continue;
5439 }
5440
5441 /* We need to include this archive member. */
5442 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5443 if (element == NULL)
5444 goto error_return;
5445
5446 if (! bfd_check_format (element, bfd_object))
5447 goto error_return;
5448
0ad989f9
L
5449 undefs_tail = info->hash->undefs_tail;
5450
0e144ba7
AM
5451 if (!(*info->callbacks
5452 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5453 continue;
0e144ba7 5454 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5455 goto error_return;
5456
5457 /* If there are any new undefined symbols, we need to make
5458 another pass through the archive in order to see whether
5459 they can be defined. FIXME: This isn't perfect, because
5460 common symbols wind up on undefs_tail and because an
5461 undefined symbol which is defined later on in this pass
5462 does not require another pass. This isn't a bug, but it
5463 does make the code less efficient than it could be. */
5464 if (undefs_tail != info->hash->undefs_tail)
5465 loop = TRUE;
5466
5467 /* Look backward to mark all symbols from this object file
5468 which we have already seen in this pass. */
5469 mark = i;
5470 do
5471 {
5472 included[mark] = TRUE;
5473 if (mark == 0)
5474 break;
5475 --mark;
5476 }
5477 while (symdefs[mark].file_offset == symdef->file_offset);
5478
5479 /* We mark subsequent symbols from this object file as we go
5480 on through the loop. */
5481 last = symdef->file_offset;
5482 }
5483 }
5484 while (loop);
5485
0ad989f9
L
5486 free (included);
5487
5488 return TRUE;
5489
5490 error_return:
0ad989f9
L
5491 if (included != NULL)
5492 free (included);
5493 return FALSE;
5494}
4ad4eba5
AM
5495
5496/* Given an ELF BFD, add symbols to the global hash table as
5497 appropriate. */
5498
5499bfd_boolean
5500bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5501{
5502 switch (bfd_get_format (abfd))
5503 {
5504 case bfd_object:
5505 return elf_link_add_object_symbols (abfd, info);
5506 case bfd_archive:
5507 return elf_link_add_archive_symbols (abfd, info);
5508 default:
5509 bfd_set_error (bfd_error_wrong_format);
5510 return FALSE;
5511 }
5512}
5a580b3a 5513\f
14b1c01e
AM
5514struct hash_codes_info
5515{
5516 unsigned long *hashcodes;
5517 bfd_boolean error;
5518};
a0c8462f 5519
5a580b3a
AM
5520/* This function will be called though elf_link_hash_traverse to store
5521 all hash value of the exported symbols in an array. */
5522
5523static bfd_boolean
5524elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5525{
a50b1753 5526 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5527 const char *name;
5a580b3a
AM
5528 unsigned long ha;
5529 char *alc = NULL;
5530
5a580b3a
AM
5531 /* Ignore indirect symbols. These are added by the versioning code. */
5532 if (h->dynindx == -1)
5533 return TRUE;
5534
5535 name = h->root.root.string;
422f1182 5536 if (h->versioned >= versioned)
5a580b3a 5537 {
422f1182
L
5538 char *p = strchr (name, ELF_VER_CHR);
5539 if (p != NULL)
14b1c01e 5540 {
422f1182
L
5541 alc = (char *) bfd_malloc (p - name + 1);
5542 if (alc == NULL)
5543 {
5544 inf->error = TRUE;
5545 return FALSE;
5546 }
5547 memcpy (alc, name, p - name);
5548 alc[p - name] = '\0';
5549 name = alc;
14b1c01e 5550 }
5a580b3a
AM
5551 }
5552
5553 /* Compute the hash value. */
5554 ha = bfd_elf_hash (name);
5555
5556 /* Store the found hash value in the array given as the argument. */
14b1c01e 5557 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5558
5559 /* And store it in the struct so that we can put it in the hash table
5560 later. */
f6e332e6 5561 h->u.elf_hash_value = ha;
5a580b3a
AM
5562
5563 if (alc != NULL)
5564 free (alc);
5565
5566 return TRUE;
5567}
5568
fdc90cb4
JJ
5569struct collect_gnu_hash_codes
5570{
5571 bfd *output_bfd;
5572 const struct elf_backend_data *bed;
5573 unsigned long int nsyms;
5574 unsigned long int maskbits;
5575 unsigned long int *hashcodes;
5576 unsigned long int *hashval;
5577 unsigned long int *indx;
5578 unsigned long int *counts;
5579 bfd_vma *bitmask;
5580 bfd_byte *contents;
5581 long int min_dynindx;
5582 unsigned long int bucketcount;
5583 unsigned long int symindx;
5584 long int local_indx;
5585 long int shift1, shift2;
5586 unsigned long int mask;
14b1c01e 5587 bfd_boolean error;
fdc90cb4
JJ
5588};
5589
5590/* This function will be called though elf_link_hash_traverse to store
5591 all hash value of the exported symbols in an array. */
5592
5593static bfd_boolean
5594elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5595{
a50b1753 5596 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5597 const char *name;
fdc90cb4
JJ
5598 unsigned long ha;
5599 char *alc = NULL;
5600
fdc90cb4
JJ
5601 /* Ignore indirect symbols. These are added by the versioning code. */
5602 if (h->dynindx == -1)
5603 return TRUE;
5604
5605 /* Ignore also local symbols and undefined symbols. */
5606 if (! (*s->bed->elf_hash_symbol) (h))
5607 return TRUE;
5608
5609 name = h->root.root.string;
422f1182 5610 if (h->versioned >= versioned)
fdc90cb4 5611 {
422f1182
L
5612 char *p = strchr (name, ELF_VER_CHR);
5613 if (p != NULL)
14b1c01e 5614 {
422f1182
L
5615 alc = (char *) bfd_malloc (p - name + 1);
5616 if (alc == NULL)
5617 {
5618 s->error = TRUE;
5619 return FALSE;
5620 }
5621 memcpy (alc, name, p - name);
5622 alc[p - name] = '\0';
5623 name = alc;
14b1c01e 5624 }
fdc90cb4
JJ
5625 }
5626
5627 /* Compute the hash value. */
5628 ha = bfd_elf_gnu_hash (name);
5629
5630 /* Store the found hash value in the array for compute_bucket_count,
5631 and also for .dynsym reordering purposes. */
5632 s->hashcodes[s->nsyms] = ha;
5633 s->hashval[h->dynindx] = ha;
5634 ++s->nsyms;
5635 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5636 s->min_dynindx = h->dynindx;
5637
5638 if (alc != NULL)
5639 free (alc);
5640
5641 return TRUE;
5642}
5643
5644/* This function will be called though elf_link_hash_traverse to do
5645 final dynaminc symbol renumbering. */
5646
5647static bfd_boolean
5648elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5649{
a50b1753 5650 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5651 unsigned long int bucket;
5652 unsigned long int val;
5653
fdc90cb4
JJ
5654 /* Ignore indirect symbols. */
5655 if (h->dynindx == -1)
5656 return TRUE;
5657
5658 /* Ignore also local symbols and undefined symbols. */
5659 if (! (*s->bed->elf_hash_symbol) (h))
5660 {
5661 if (h->dynindx >= s->min_dynindx)
5662 h->dynindx = s->local_indx++;
5663 return TRUE;
5664 }
5665
5666 bucket = s->hashval[h->dynindx] % s->bucketcount;
5667 val = (s->hashval[h->dynindx] >> s->shift1)
5668 & ((s->maskbits >> s->shift1) - 1);
5669 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5670 s->bitmask[val]
5671 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5672 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5673 if (s->counts[bucket] == 1)
5674 /* Last element terminates the chain. */
5675 val |= 1;
5676 bfd_put_32 (s->output_bfd, val,
5677 s->contents + (s->indx[bucket] - s->symindx) * 4);
5678 --s->counts[bucket];
5679 h->dynindx = s->indx[bucket]++;
5680 return TRUE;
5681}
5682
5683/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5684
5685bfd_boolean
5686_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5687{
5688 return !(h->forced_local
5689 || h->root.type == bfd_link_hash_undefined
5690 || h->root.type == bfd_link_hash_undefweak
5691 || ((h->root.type == bfd_link_hash_defined
5692 || h->root.type == bfd_link_hash_defweak)
5693 && h->root.u.def.section->output_section == NULL));
5694}
5695
5a580b3a
AM
5696/* Array used to determine the number of hash table buckets to use
5697 based on the number of symbols there are. If there are fewer than
5698 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5699 fewer than 37 we use 17 buckets, and so forth. We never use more
5700 than 32771 buckets. */
5701
5702static const size_t elf_buckets[] =
5703{
5704 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5705 16411, 32771, 0
5706};
5707
5708/* Compute bucket count for hashing table. We do not use a static set
5709 of possible tables sizes anymore. Instead we determine for all
5710 possible reasonable sizes of the table the outcome (i.e., the
5711 number of collisions etc) and choose the best solution. The
5712 weighting functions are not too simple to allow the table to grow
5713 without bounds. Instead one of the weighting factors is the size.
5714 Therefore the result is always a good payoff between few collisions
5715 (= short chain lengths) and table size. */
5716static size_t
b20dd2ce 5717compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5718 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5719 unsigned long int nsyms,
5720 int gnu_hash)
5a580b3a 5721{
5a580b3a 5722 size_t best_size = 0;
5a580b3a 5723 unsigned long int i;
5a580b3a 5724
5a580b3a
AM
5725 /* We have a problem here. The following code to optimize the table
5726 size requires an integer type with more the 32 bits. If
5727 BFD_HOST_U_64_BIT is set we know about such a type. */
5728#ifdef BFD_HOST_U_64_BIT
5729 if (info->optimize)
5730 {
5a580b3a
AM
5731 size_t minsize;
5732 size_t maxsize;
5733 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5734 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5735 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5736 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5737 unsigned long int *counts;
d40f3da9 5738 bfd_size_type amt;
0883b6e0 5739 unsigned int no_improvement_count = 0;
5a580b3a
AM
5740
5741 /* Possible optimization parameters: if we have NSYMS symbols we say
5742 that the hashing table must at least have NSYMS/4 and at most
5743 2*NSYMS buckets. */
5744 minsize = nsyms / 4;
5745 if (minsize == 0)
5746 minsize = 1;
5747 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5748 if (gnu_hash)
5749 {
5750 if (minsize < 2)
5751 minsize = 2;
5752 if ((best_size & 31) == 0)
5753 ++best_size;
5754 }
5a580b3a
AM
5755
5756 /* Create array where we count the collisions in. We must use bfd_malloc
5757 since the size could be large. */
5758 amt = maxsize;
5759 amt *= sizeof (unsigned long int);
a50b1753 5760 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5761 if (counts == NULL)
fdc90cb4 5762 return 0;
5a580b3a
AM
5763
5764 /* Compute the "optimal" size for the hash table. The criteria is a
5765 minimal chain length. The minor criteria is (of course) the size
5766 of the table. */
5767 for (i = minsize; i < maxsize; ++i)
5768 {
5769 /* Walk through the array of hashcodes and count the collisions. */
5770 BFD_HOST_U_64_BIT max;
5771 unsigned long int j;
5772 unsigned long int fact;
5773
fdc90cb4
JJ
5774 if (gnu_hash && (i & 31) == 0)
5775 continue;
5776
5a580b3a
AM
5777 memset (counts, '\0', i * sizeof (unsigned long int));
5778
5779 /* Determine how often each hash bucket is used. */
5780 for (j = 0; j < nsyms; ++j)
5781 ++counts[hashcodes[j] % i];
5782
5783 /* For the weight function we need some information about the
5784 pagesize on the target. This is information need not be 100%
5785 accurate. Since this information is not available (so far) we
5786 define it here to a reasonable default value. If it is crucial
5787 to have a better value some day simply define this value. */
5788# ifndef BFD_TARGET_PAGESIZE
5789# define BFD_TARGET_PAGESIZE (4096)
5790# endif
5791
fdc90cb4
JJ
5792 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5793 and the chains. */
5794 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5795
5796# if 1
5797 /* Variant 1: optimize for short chains. We add the squares
5798 of all the chain lengths (which favors many small chain
5799 over a few long chains). */
5800 for (j = 0; j < i; ++j)
5801 max += counts[j] * counts[j];
5802
5803 /* This adds penalties for the overall size of the table. */
fdc90cb4 5804 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5805 max *= fact * fact;
5806# else
5807 /* Variant 2: Optimize a lot more for small table. Here we
5808 also add squares of the size but we also add penalties for
5809 empty slots (the +1 term). */
5810 for (j = 0; j < i; ++j)
5811 max += (1 + counts[j]) * (1 + counts[j]);
5812
5813 /* The overall size of the table is considered, but not as
5814 strong as in variant 1, where it is squared. */
fdc90cb4 5815 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5816 max *= fact;
5817# endif
5818
5819 /* Compare with current best results. */
5820 if (max < best_chlen)
5821 {
5822 best_chlen = max;
5823 best_size = i;
ca4be51c 5824 no_improvement_count = 0;
5a580b3a 5825 }
0883b6e0
NC
5826 /* PR 11843: Avoid futile long searches for the best bucket size
5827 when there are a large number of symbols. */
5828 else if (++no_improvement_count == 100)
5829 break;
5a580b3a
AM
5830 }
5831
5832 free (counts);
5833 }
5834 else
5835#endif /* defined (BFD_HOST_U_64_BIT) */
5836 {
5837 /* This is the fallback solution if no 64bit type is available or if we
5838 are not supposed to spend much time on optimizations. We select the
5839 bucket count using a fixed set of numbers. */
5840 for (i = 0; elf_buckets[i] != 0; i++)
5841 {
5842 best_size = elf_buckets[i];
fdc90cb4 5843 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5844 break;
5845 }
fdc90cb4
JJ
5846 if (gnu_hash && best_size < 2)
5847 best_size = 2;
5a580b3a
AM
5848 }
5849
5a580b3a
AM
5850 return best_size;
5851}
5852
d0bf826b
AM
5853/* Size any SHT_GROUP section for ld -r. */
5854
5855bfd_boolean
5856_bfd_elf_size_group_sections (struct bfd_link_info *info)
5857{
5858 bfd *ibfd;
5859
c72f2fb2 5860 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5861 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5862 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5863 return FALSE;
5864 return TRUE;
5865}
5866
04c3a755
NS
5867/* Set a default stack segment size. The value in INFO wins. If it
5868 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5869 undefined it is initialized. */
5870
5871bfd_boolean
5872bfd_elf_stack_segment_size (bfd *output_bfd,
5873 struct bfd_link_info *info,
5874 const char *legacy_symbol,
5875 bfd_vma default_size)
5876{
5877 struct elf_link_hash_entry *h = NULL;
5878
5879 /* Look for legacy symbol. */
5880 if (legacy_symbol)
5881 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5882 FALSE, FALSE, FALSE);
5883 if (h && (h->root.type == bfd_link_hash_defined
5884 || h->root.type == bfd_link_hash_defweak)
5885 && h->def_regular
5886 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5887 {
5888 /* The symbol has no type if specified on the command line. */
5889 h->type = STT_OBJECT;
5890 if (info->stacksize)
695344c0 5891 /* xgettext:c-format */
4eca0228
AM
5892 _bfd_error_handler (_("%B: stack size specified and %s set"),
5893 output_bfd, legacy_symbol);
04c3a755 5894 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5895 /* xgettext:c-format */
4eca0228
AM
5896 _bfd_error_handler (_("%B: %s not absolute"),
5897 output_bfd, legacy_symbol);
04c3a755
NS
5898 else
5899 info->stacksize = h->root.u.def.value;
5900 }
5901
5902 if (!info->stacksize)
5903 /* If the user didn't set a size, or explicitly inhibit the
5904 size, set it now. */
5905 info->stacksize = default_size;
5906
5907 /* Provide the legacy symbol, if it is referenced. */
5908 if (h && (h->root.type == bfd_link_hash_undefined
5909 || h->root.type == bfd_link_hash_undefweak))
5910 {
5911 struct bfd_link_hash_entry *bh = NULL;
5912
5913 if (!(_bfd_generic_link_add_one_symbol
5914 (info, output_bfd, legacy_symbol,
5915 BSF_GLOBAL, bfd_abs_section_ptr,
5916 info->stacksize >= 0 ? info->stacksize : 0,
5917 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5918 return FALSE;
5919
5920 h = (struct elf_link_hash_entry *) bh;
5921 h->def_regular = 1;
5922 h->type = STT_OBJECT;
5923 }
5924
5925 return TRUE;
5926}
5927
b531344c
MR
5928/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5929
5930struct elf_gc_sweep_symbol_info
5931{
5932 struct bfd_link_info *info;
5933 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5934 bfd_boolean);
5935};
5936
5937static bfd_boolean
5938elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5939{
5940 if (!h->mark
5941 && (((h->root.type == bfd_link_hash_defined
5942 || h->root.type == bfd_link_hash_defweak)
5943 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5944 && h->root.u.def.section->gc_mark))
5945 || h->root.type == bfd_link_hash_undefined
5946 || h->root.type == bfd_link_hash_undefweak))
5947 {
5948 struct elf_gc_sweep_symbol_info *inf;
5949
5950 inf = (struct elf_gc_sweep_symbol_info *) data;
5951 (*inf->hide_symbol) (inf->info, h, TRUE);
5952 h->def_regular = 0;
5953 h->ref_regular = 0;
5954 h->ref_regular_nonweak = 0;
5955 }
5956
5957 return TRUE;
5958}
5959
5a580b3a
AM
5960/* Set up the sizes and contents of the ELF dynamic sections. This is
5961 called by the ELF linker emulation before_allocation routine. We
5962 must set the sizes of the sections before the linker sets the
5963 addresses of the various sections. */
5964
5965bfd_boolean
5966bfd_elf_size_dynamic_sections (bfd *output_bfd,
5967 const char *soname,
5968 const char *rpath,
5969 const char *filter_shlib,
7ee314fa
AM
5970 const char *audit,
5971 const char *depaudit,
5a580b3a
AM
5972 const char * const *auxiliary_filters,
5973 struct bfd_link_info *info,
fd91d419 5974 asection **sinterpptr)
5a580b3a 5975{
5a580b3a
AM
5976 bfd *dynobj;
5977 const struct elf_backend_data *bed;
5a580b3a
AM
5978
5979 *sinterpptr = NULL;
5980
5a580b3a
AM
5981 if (!is_elf_hash_table (info->hash))
5982 return TRUE;
5983
5a580b3a
AM
5984 dynobj = elf_hash_table (info)->dynobj;
5985
9a2a56cc 5986 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 5987 {
902e9fc7
MR
5988 struct bfd_elf_version_tree *verdefs;
5989 struct elf_info_failed asvinfo;
5a580b3a
AM
5990 struct bfd_elf_version_tree *t;
5991 struct bfd_elf_version_expr *d;
902e9fc7 5992 struct elf_info_failed eif;
5a580b3a 5993 bfd_boolean all_defined;
902e9fc7 5994 asection *s;
e6699019 5995 size_t soname_indx;
7ee314fa 5996
5a580b3a 5997 eif.info = info;
5a580b3a
AM
5998 eif.failed = FALSE;
5999
6000 /* If we are supposed to export all symbols into the dynamic symbol
6001 table (this is not the normal case), then do so. */
55255dae 6002 if (info->export_dynamic
0e1862bb 6003 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6004 {
6005 elf_link_hash_traverse (elf_hash_table (info),
6006 _bfd_elf_export_symbol,
6007 &eif);
6008 if (eif.failed)
6009 return FALSE;
6010 }
6011
e6699019
L
6012 if (soname != NULL)
6013 {
6014 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6015 soname, TRUE);
6016 if (soname_indx == (size_t) -1
6017 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6018 return FALSE;
6019 }
6020 else
6021 soname_indx = (size_t) -1;
6022
5a580b3a 6023 /* Make all global versions with definition. */
fd91d419 6024 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6025 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6026 if (!d->symver && d->literal)
5a580b3a
AM
6027 {
6028 const char *verstr, *name;
6029 size_t namelen, verlen, newlen;
93252b1c 6030 char *newname, *p, leading_char;
5a580b3a
AM
6031 struct elf_link_hash_entry *newh;
6032
93252b1c 6033 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6034 name = d->pattern;
93252b1c 6035 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6036 verstr = t->name;
6037 verlen = strlen (verstr);
6038 newlen = namelen + verlen + 3;
6039
a50b1753 6040 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6041 if (newname == NULL)
6042 return FALSE;
93252b1c
MF
6043 newname[0] = leading_char;
6044 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6045
6046 /* Check the hidden versioned definition. */
6047 p = newname + namelen;
6048 *p++ = ELF_VER_CHR;
6049 memcpy (p, verstr, verlen + 1);
6050 newh = elf_link_hash_lookup (elf_hash_table (info),
6051 newname, FALSE, FALSE,
6052 FALSE);
6053 if (newh == NULL
6054 || (newh->root.type != bfd_link_hash_defined
6055 && newh->root.type != bfd_link_hash_defweak))
6056 {
6057 /* Check the default versioned definition. */
6058 *p++ = ELF_VER_CHR;
6059 memcpy (p, verstr, verlen + 1);
6060 newh = elf_link_hash_lookup (elf_hash_table (info),
6061 newname, FALSE, FALSE,
6062 FALSE);
6063 }
6064 free (newname);
6065
6066 /* Mark this version if there is a definition and it is
6067 not defined in a shared object. */
6068 if (newh != NULL
f5385ebf 6069 && !newh->def_dynamic
5a580b3a
AM
6070 && (newh->root.type == bfd_link_hash_defined
6071 || newh->root.type == bfd_link_hash_defweak))
6072 d->symver = 1;
6073 }
6074
6075 /* Attach all the symbols to their version information. */
5a580b3a 6076 asvinfo.info = info;
5a580b3a
AM
6077 asvinfo.failed = FALSE;
6078
6079 elf_link_hash_traverse (elf_hash_table (info),
6080 _bfd_elf_link_assign_sym_version,
6081 &asvinfo);
6082 if (asvinfo.failed)
6083 return FALSE;
6084
6085 if (!info->allow_undefined_version)
6086 {
6087 /* Check if all global versions have a definition. */
6088 all_defined = TRUE;
fd91d419 6089 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6090 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6091 if (d->literal && !d->symver && !d->script)
5a580b3a 6092 {
4eca0228 6093 _bfd_error_handler
5a580b3a
AM
6094 (_("%s: undefined version: %s"),
6095 d->pattern, t->name);
6096 all_defined = FALSE;
6097 }
6098
6099 if (!all_defined)
6100 {
6101 bfd_set_error (bfd_error_bad_value);
6102 return FALSE;
6103 }
6104 }
6105
902e9fc7
MR
6106 /* Set up the version definition section. */
6107 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6108 BFD_ASSERT (s != NULL);
5a580b3a 6109
902e9fc7
MR
6110 /* We may have created additional version definitions if we are
6111 just linking a regular application. */
6112 verdefs = info->version_info;
5a580b3a 6113
902e9fc7
MR
6114 /* Skip anonymous version tag. */
6115 if (verdefs != NULL && verdefs->vernum == 0)
6116 verdefs = verdefs->next;
5a580b3a 6117
902e9fc7
MR
6118 if (verdefs == NULL && !info->create_default_symver)
6119 s->flags |= SEC_EXCLUDE;
6120 else
5a580b3a 6121 {
902e9fc7
MR
6122 unsigned int cdefs;
6123 bfd_size_type size;
6124 bfd_byte *p;
6125 Elf_Internal_Verdef def;
6126 Elf_Internal_Verdaux defaux;
6127 struct bfd_link_hash_entry *bh;
6128 struct elf_link_hash_entry *h;
6129 const char *name;
5a580b3a 6130
902e9fc7
MR
6131 cdefs = 0;
6132 size = 0;
5a580b3a 6133
902e9fc7
MR
6134 /* Make space for the base version. */
6135 size += sizeof (Elf_External_Verdef);
6136 size += sizeof (Elf_External_Verdaux);
6137 ++cdefs;
6138
6139 /* Make space for the default version. */
6140 if (info->create_default_symver)
6141 {
6142 size += sizeof (Elf_External_Verdef);
6143 ++cdefs;
3e3b46e5
PB
6144 }
6145
5a580b3a
AM
6146 for (t = verdefs; t != NULL; t = t->next)
6147 {
6148 struct bfd_elf_version_deps *n;
6149
a6cc6b3b
RO
6150 /* Don't emit base version twice. */
6151 if (t->vernum == 0)
6152 continue;
6153
5a580b3a
AM
6154 size += sizeof (Elf_External_Verdef);
6155 size += sizeof (Elf_External_Verdaux);
6156 ++cdefs;
6157
6158 for (n = t->deps; n != NULL; n = n->next)
6159 size += sizeof (Elf_External_Verdaux);
6160 }
6161
eea6121a 6162 s->size = size;
a50b1753 6163 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6164 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6165 return FALSE;
6166
6167 /* Fill in the version definition section. */
6168
6169 p = s->contents;
6170
6171 def.vd_version = VER_DEF_CURRENT;
6172 def.vd_flags = VER_FLG_BASE;
6173 def.vd_ndx = 1;
6174 def.vd_cnt = 1;
3e3b46e5
PB
6175 if (info->create_default_symver)
6176 {
6177 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6178 def.vd_next = sizeof (Elf_External_Verdef);
6179 }
6180 else
6181 {
6182 def.vd_aux = sizeof (Elf_External_Verdef);
6183 def.vd_next = (sizeof (Elf_External_Verdef)
6184 + sizeof (Elf_External_Verdaux));
6185 }
5a580b3a 6186
ef53be89 6187 if (soname_indx != (size_t) -1)
5a580b3a
AM
6188 {
6189 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6190 soname_indx);
6191 def.vd_hash = bfd_elf_hash (soname);
6192 defaux.vda_name = soname_indx;
3e3b46e5 6193 name = soname;
5a580b3a
AM
6194 }
6195 else
6196 {
ef53be89 6197 size_t indx;
5a580b3a 6198
06084812 6199 name = lbasename (output_bfd->filename);
5a580b3a
AM
6200 def.vd_hash = bfd_elf_hash (name);
6201 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6202 name, FALSE);
ef53be89 6203 if (indx == (size_t) -1)
5a580b3a
AM
6204 return FALSE;
6205 defaux.vda_name = indx;
6206 }
6207 defaux.vda_next = 0;
6208
6209 _bfd_elf_swap_verdef_out (output_bfd, &def,
6210 (Elf_External_Verdef *) p);
6211 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6212 if (info->create_default_symver)
6213 {
6214 /* Add a symbol representing this version. */
6215 bh = NULL;
6216 if (! (_bfd_generic_link_add_one_symbol
6217 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6218 0, NULL, FALSE,
6219 get_elf_backend_data (dynobj)->collect, &bh)))
6220 return FALSE;
6221 h = (struct elf_link_hash_entry *) bh;
6222 h->non_elf = 0;
6223 h->def_regular = 1;
6224 h->type = STT_OBJECT;
6225 h->verinfo.vertree = NULL;
6226
6227 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6228 return FALSE;
6229
6230 /* Create a duplicate of the base version with the same
6231 aux block, but different flags. */
6232 def.vd_flags = 0;
6233 def.vd_ndx = 2;
6234 def.vd_aux = sizeof (Elf_External_Verdef);
6235 if (verdefs)
6236 def.vd_next = (sizeof (Elf_External_Verdef)
6237 + sizeof (Elf_External_Verdaux));
6238 else
6239 def.vd_next = 0;
6240 _bfd_elf_swap_verdef_out (output_bfd, &def,
6241 (Elf_External_Verdef *) p);
6242 p += sizeof (Elf_External_Verdef);
6243 }
5a580b3a
AM
6244 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6245 (Elf_External_Verdaux *) p);
6246 p += sizeof (Elf_External_Verdaux);
6247
6248 for (t = verdefs; t != NULL; t = t->next)
6249 {
6250 unsigned int cdeps;
6251 struct bfd_elf_version_deps *n;
5a580b3a 6252
a6cc6b3b
RO
6253 /* Don't emit the base version twice. */
6254 if (t->vernum == 0)
6255 continue;
6256
5a580b3a
AM
6257 cdeps = 0;
6258 for (n = t->deps; n != NULL; n = n->next)
6259 ++cdeps;
6260
6261 /* Add a symbol representing this version. */
6262 bh = NULL;
6263 if (! (_bfd_generic_link_add_one_symbol
6264 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6265 0, NULL, FALSE,
6266 get_elf_backend_data (dynobj)->collect, &bh)))
6267 return FALSE;
6268 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6269 h->non_elf = 0;
6270 h->def_regular = 1;
5a580b3a
AM
6271 h->type = STT_OBJECT;
6272 h->verinfo.vertree = t;
6273
c152c796 6274 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6275 return FALSE;
6276
6277 def.vd_version = VER_DEF_CURRENT;
6278 def.vd_flags = 0;
6279 if (t->globals.list == NULL
6280 && t->locals.list == NULL
6281 && ! t->used)
6282 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6283 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6284 def.vd_cnt = cdeps + 1;
6285 def.vd_hash = bfd_elf_hash (t->name);
6286 def.vd_aux = sizeof (Elf_External_Verdef);
6287 def.vd_next = 0;
a6cc6b3b
RO
6288
6289 /* If a basever node is next, it *must* be the last node in
6290 the chain, otherwise Verdef construction breaks. */
6291 if (t->next != NULL && t->next->vernum == 0)
6292 BFD_ASSERT (t->next->next == NULL);
6293
6294 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6295 def.vd_next = (sizeof (Elf_External_Verdef)
6296 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6297
6298 _bfd_elf_swap_verdef_out (output_bfd, &def,
6299 (Elf_External_Verdef *) p);
6300 p += sizeof (Elf_External_Verdef);
6301
6302 defaux.vda_name = h->dynstr_index;
6303 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6304 h->dynstr_index);
6305 defaux.vda_next = 0;
6306 if (t->deps != NULL)
6307 defaux.vda_next = sizeof (Elf_External_Verdaux);
6308 t->name_indx = defaux.vda_name;
6309
6310 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6311 (Elf_External_Verdaux *) p);
6312 p += sizeof (Elf_External_Verdaux);
6313
6314 for (n = t->deps; n != NULL; n = n->next)
6315 {
6316 if (n->version_needed == NULL)
6317 {
6318 /* This can happen if there was an error in the
6319 version script. */
6320 defaux.vda_name = 0;
6321 }
6322 else
6323 {
6324 defaux.vda_name = n->version_needed->name_indx;
6325 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6326 defaux.vda_name);
6327 }
6328 if (n->next == NULL)
6329 defaux.vda_next = 0;
6330 else
6331 defaux.vda_next = sizeof (Elf_External_Verdaux);
6332
6333 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6334 (Elf_External_Verdaux *) p);
6335 p += sizeof (Elf_External_Verdaux);
6336 }
6337 }
6338
5a580b3a
AM
6339 elf_tdata (output_bfd)->cverdefs = cdefs;
6340 }
6341
5a580b3a
AM
6342 /* Work out the size of the version reference section. */
6343
3d4d4302 6344 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6345 BFD_ASSERT (s != NULL);
6346 {
6347 struct elf_find_verdep_info sinfo;
6348
5a580b3a
AM
6349 sinfo.info = info;
6350 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6351 if (sinfo.vers == 0)
6352 sinfo.vers = 1;
6353 sinfo.failed = FALSE;
6354
6355 elf_link_hash_traverse (elf_hash_table (info),
6356 _bfd_elf_link_find_version_dependencies,
6357 &sinfo);
14b1c01e
AM
6358 if (sinfo.failed)
6359 return FALSE;
5a580b3a
AM
6360
6361 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6362 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6363 else
6364 {
902e9fc7 6365 Elf_Internal_Verneed *vn;
5a580b3a
AM
6366 unsigned int size;
6367 unsigned int crefs;
6368 bfd_byte *p;
6369
a6cc6b3b 6370 /* Build the version dependency section. */
5a580b3a
AM
6371 size = 0;
6372 crefs = 0;
902e9fc7
MR
6373 for (vn = elf_tdata (output_bfd)->verref;
6374 vn != NULL;
6375 vn = vn->vn_nextref)
5a580b3a
AM
6376 {
6377 Elf_Internal_Vernaux *a;
6378
6379 size += sizeof (Elf_External_Verneed);
6380 ++crefs;
902e9fc7 6381 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6382 size += sizeof (Elf_External_Vernaux);
6383 }
6384
eea6121a 6385 s->size = size;
a50b1753 6386 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6387 if (s->contents == NULL)
6388 return FALSE;
6389
6390 p = s->contents;
902e9fc7
MR
6391 for (vn = elf_tdata (output_bfd)->verref;
6392 vn != NULL;
6393 vn = vn->vn_nextref)
5a580b3a
AM
6394 {
6395 unsigned int caux;
6396 Elf_Internal_Vernaux *a;
ef53be89 6397 size_t indx;
5a580b3a
AM
6398
6399 caux = 0;
902e9fc7 6400 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6401 ++caux;
6402
902e9fc7
MR
6403 vn->vn_version = VER_NEED_CURRENT;
6404 vn->vn_cnt = caux;
5a580b3a 6405 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
902e9fc7
MR
6406 elf_dt_name (vn->vn_bfd) != NULL
6407 ? elf_dt_name (vn->vn_bfd)
6408 : lbasename (vn->vn_bfd->filename),
5a580b3a 6409 FALSE);
ef53be89 6410 if (indx == (size_t) -1)
5a580b3a 6411 return FALSE;
902e9fc7
MR
6412 vn->vn_file = indx;
6413 vn->vn_aux = sizeof (Elf_External_Verneed);
6414 if (vn->vn_nextref == NULL)
6415 vn->vn_next = 0;
5a580b3a 6416 else
902e9fc7 6417 vn->vn_next = (sizeof (Elf_External_Verneed)
5a580b3a
AM
6418 + caux * sizeof (Elf_External_Vernaux));
6419
902e9fc7 6420 _bfd_elf_swap_verneed_out (output_bfd, vn,
5a580b3a
AM
6421 (Elf_External_Verneed *) p);
6422 p += sizeof (Elf_External_Verneed);
6423
902e9fc7 6424 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6425 {
6426 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6427 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6428 a->vna_nodename, FALSE);
ef53be89 6429 if (indx == (size_t) -1)
5a580b3a
AM
6430 return FALSE;
6431 a->vna_name = indx;
6432 if (a->vna_nextptr == NULL)
6433 a->vna_next = 0;
6434 else
6435 a->vna_next = sizeof (Elf_External_Vernaux);
6436
6437 _bfd_elf_swap_vernaux_out (output_bfd, a,
6438 (Elf_External_Vernaux *) p);
6439 p += sizeof (Elf_External_Vernaux);
6440 }
6441 }
6442
5a580b3a
AM
6443 elf_tdata (output_bfd)->cverrefs = crefs;
6444 }
6445 }
902e9fc7
MR
6446 }
6447
6448 bed = get_elf_backend_data (output_bfd);
6449
6450 if (info->gc_sections && bed->can_gc_sections)
6451 {
6452 struct elf_gc_sweep_symbol_info sweep_info;
6453 unsigned long section_sym_count;
6454
6455 /* Remove the symbols that were in the swept sections from the
6456 dynamic symbol table. GCFIXME: Anyone know how to get them
6457 out of the static symbol table as well? */
6458 sweep_info.info = info;
6459 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6460 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6461 &sweep_info);
6462
23ec1e32
MR
6463 /* We need to reassign dynsym indices now that symbols may have
6464 been removed. See the call in `bfd_elf_size_dynsym_hash_dynstr'
6465 for the details of the conditions used here. */
6466 if (elf_hash_table (info)->dynamic_sections_created
6467 || bed->always_renumber_dynsyms)
c46cec3a 6468 _bfd_elf_link_renumber_dynsyms (output_bfd, info, &section_sym_count);
902e9fc7
MR
6469 }
6470
6471 /* Any syms created from now on start with -1 in
6472 got.refcount/offset and plt.refcount/offset. */
6473 elf_hash_table (info)->init_got_refcount
6474 = elf_hash_table (info)->init_got_offset;
6475 elf_hash_table (info)->init_plt_refcount
6476 = elf_hash_table (info)->init_plt_offset;
6477
6478 if (bfd_link_relocatable (info)
6479 && !_bfd_elf_size_group_sections (info))
6480 return FALSE;
6481
6482 /* The backend may have to create some sections regardless of whether
6483 we're dynamic or not. */
6484 if (bed->elf_backend_always_size_sections
6485 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6486 return FALSE;
6487
6488 /* Determine any GNU_STACK segment requirements, after the backend
6489 has had a chance to set a default segment size. */
6490 if (info->execstack)
6491 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6492 else if (info->noexecstack)
6493 elf_stack_flags (output_bfd) = PF_R | PF_W;
6494 else
6495 {
6496 bfd *inputobj;
6497 asection *notesec = NULL;
6498 int exec = 0;
6499
6500 for (inputobj = info->input_bfds;
6501 inputobj;
6502 inputobj = inputobj->link.next)
6503 {
6504 asection *s;
6505
6506 if (inputobj->flags
6507 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6508 continue;
6509 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6510 if (s)
6511 {
6512 if (s->flags & SEC_CODE)
6513 exec = PF_X;
6514 notesec = s;
6515 }
6516 else if (bed->default_execstack)
6517 exec = PF_X;
6518 }
6519 if (notesec || info->stacksize > 0)
6520 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6521 if (notesec && exec && bfd_link_relocatable (info)
6522 && notesec->output_section != bfd_abs_section_ptr)
6523 notesec->output_section->flags |= SEC_CODE;
6524 }
6525
6526 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6527 {
6528 struct elf_info_failed eif;
6529 struct elf_link_hash_entry *h;
6530 asection *dynstr;
6531 asection *s;
6532
6533 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6534 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6535
902e9fc7
MR
6536 if (info->symbolic)
6537 {
6538 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6539 return FALSE;
6540 info->flags |= DF_SYMBOLIC;
6541 }
6542
6543 if (rpath != NULL)
6544 {
6545 size_t indx;
6546 bfd_vma tag;
6547
6548 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6549 TRUE);
6550 if (indx == (size_t) -1)
6551 return FALSE;
6552
6553 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6554 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6555 return FALSE;
6556 }
6557
6558 if (filter_shlib != NULL)
6559 {
6560 size_t indx;
6561
6562 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6563 filter_shlib, TRUE);
6564 if (indx == (size_t) -1
6565 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6566 return FALSE;
6567 }
6568
6569 if (auxiliary_filters != NULL)
6570 {
6571 const char * const *p;
6572
6573 for (p = auxiliary_filters; *p != NULL; p++)
6574 {
6575 size_t indx;
6576
6577 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6578 *p, TRUE);
6579 if (indx == (size_t) -1
6580 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6581 return FALSE;
6582 }
6583 }
6584
6585 if (audit != NULL)
6586 {
6587 size_t indx;
6588
6589 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6590 TRUE);
6591 if (indx == (size_t) -1
6592 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6593 return FALSE;
6594 }
6595
6596 if (depaudit != NULL)
6597 {
6598 size_t indx;
6599
6600 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6601 TRUE);
6602 if (indx == (size_t) -1
6603 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6604 return FALSE;
6605 }
6606
6607 eif.info = info;
6608 eif.failed = FALSE;
6609
6610 /* Find all symbols which were defined in a dynamic object and make
6611 the backend pick a reasonable value for them. */
6612 elf_link_hash_traverse (elf_hash_table (info),
6613 _bfd_elf_adjust_dynamic_symbol,
6614 &eif);
6615 if (eif.failed)
6616 return FALSE;
6617
6618 /* Add some entries to the .dynamic section. We fill in some of the
6619 values later, in bfd_elf_final_link, but we must add the entries
6620 now so that we know the final size of the .dynamic section. */
6621
6622 /* If there are initialization and/or finalization functions to
6623 call then add the corresponding DT_INIT/DT_FINI entries. */
6624 h = (info->init_function
6625 ? elf_link_hash_lookup (elf_hash_table (info),
6626 info->init_function, FALSE,
6627 FALSE, FALSE)
6628 : NULL);
6629 if (h != NULL
6630 && (h->ref_regular
6631 || h->def_regular))
6632 {
6633 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6634 return FALSE;
6635 }
6636 h = (info->fini_function
6637 ? elf_link_hash_lookup (elf_hash_table (info),
6638 info->fini_function, FALSE,
6639 FALSE, FALSE)
6640 : NULL);
6641 if (h != NULL
6642 && (h->ref_regular
6643 || h->def_regular))
6644 {
6645 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6646 return FALSE;
6647 }
6648
6649 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6650 if (s != NULL && s->linker_has_input)
6651 {
6652 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6653 if (! bfd_link_executable (info))
6654 {
6655 bfd *sub;
6656 asection *o;
6657
6658 for (sub = info->input_bfds; sub != NULL;
6659 sub = sub->link.next)
6660 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6661 for (o = sub->sections; o != NULL; o = o->next)
6662 if (elf_section_data (o)->this_hdr.sh_type
6663 == SHT_PREINIT_ARRAY)
6664 {
6665 _bfd_error_handler
6666 (_("%B: .preinit_array section is not allowed in DSO"),
6667 sub);
6668 break;
6669 }
6670
6671 bfd_set_error (bfd_error_nonrepresentable_section);
6672 return FALSE;
6673 }
6674
6675 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6676 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6677 return FALSE;
6678 }
6679 s = bfd_get_section_by_name (output_bfd, ".init_array");
6680 if (s != NULL && s->linker_has_input)
6681 {
6682 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6683 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6684 return FALSE;
6685 }
6686 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6687 if (s != NULL && s->linker_has_input)
6688 {
6689 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6690 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6691 return FALSE;
6692 }
6693
6694 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6695 /* If .dynstr is excluded from the link, we don't want any of
6696 these tags. Strictly, we should be checking each section
6697 individually; This quick check covers for the case where
6698 someone does a /DISCARD/ : { *(*) }. */
6699 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6700 {
6701 bfd_size_type strsize;
6702
6703 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6704 if ((info->emit_hash
6705 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6706 || (info->emit_gnu_hash
6707 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6708 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6709 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6710 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6711 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6712 bed->s->sizeof_sym))
6713 return FALSE;
6714 }
6715 }
6716
6717 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6718 return FALSE;
6719
6720 /* The backend must work out the sizes of all the other dynamic
6721 sections. */
6722 if (dynobj != NULL
6723 && bed->elf_backend_size_dynamic_sections != NULL
6724 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6725 return FALSE;
6726
6727 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6728 {
6729 unsigned long section_sym_count;
6730
6731 if (elf_tdata (output_bfd)->cverdefs)
6732 {
6733 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6734
6735 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6736 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6737 return FALSE;
6738 }
6739
6740 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6741 {
6742 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6743 return FALSE;
6744 }
6745 else if (info->flags & DF_BIND_NOW)
6746 {
6747 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6748 return FALSE;
6749 }
6750
6751 if (info->flags_1)
6752 {
6753 if (bfd_link_executable (info))
6754 info->flags_1 &= ~ (DF_1_INITFIRST
6755 | DF_1_NODELETE
6756 | DF_1_NOOPEN);
6757 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6758 return FALSE;
6759 }
6760
6761 if (elf_tdata (output_bfd)->cverrefs)
6762 {
6763 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6764
6765 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6766 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6767 return FALSE;
6768 }
5a580b3a 6769
8423293d
AM
6770 if ((elf_tdata (output_bfd)->cverrefs == 0
6771 && elf_tdata (output_bfd)->cverdefs == 0)
6772 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6773 &section_sym_count) == 0)
6774 {
902e9fc7
MR
6775 asection *s;
6776
3d4d4302 6777 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6778 s->flags |= SEC_EXCLUDE;
6779 }
6780 }
6781 return TRUE;
6782}
6783
74541ad4
AM
6784/* Find the first non-excluded output section. We'll use its
6785 section symbol for some emitted relocs. */
6786void
6787_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6788{
6789 asection *s;
6790
6791 for (s = output_bfd->sections; s != NULL; s = s->next)
6792 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6793 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6794 {
6795 elf_hash_table (info)->text_index_section = s;
6796 break;
6797 }
6798}
6799
6800/* Find two non-excluded output sections, one for code, one for data.
6801 We'll use their section symbols for some emitted relocs. */
6802void
6803_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6804{
6805 asection *s;
6806
266b05cf
DJ
6807 /* Data first, since setting text_index_section changes
6808 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6809 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6810 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6811 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6812 {
266b05cf 6813 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6814 break;
6815 }
6816
6817 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6818 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6819 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6820 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6821 {
266b05cf 6822 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6823 break;
6824 }
6825
6826 if (elf_hash_table (info)->text_index_section == NULL)
6827 elf_hash_table (info)->text_index_section
6828 = elf_hash_table (info)->data_index_section;
6829}
6830
8423293d
AM
6831bfd_boolean
6832bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6833{
74541ad4 6834 const struct elf_backend_data *bed;
23ec1e32
MR
6835 unsigned long section_sym_count;
6836 bfd_size_type dynsymcount;
74541ad4 6837
8423293d
AM
6838 if (!is_elf_hash_table (info->hash))
6839 return TRUE;
6840
74541ad4
AM
6841 bed = get_elf_backend_data (output_bfd);
6842 (*bed->elf_backend_init_index_section) (output_bfd, info);
6843
23ec1e32
MR
6844 /* Assign dynsym indices. In a shared library we generate a section
6845 symbol for each output section, which come first. Next come all
6846 of the back-end allocated local dynamic syms, followed by the rest
6847 of the global symbols.
6848
6849 This is usually not needed for static binaries, however backends
6850 can request to always do it, e.g. the MIPS backend uses dynamic
6851 symbol counts to lay out GOT, which will be produced in the
6852 presence of GOT relocations even in static binaries (holding fixed
6853 data in that case, to satisfy those relocations). */
6854
6855 if (elf_hash_table (info)->dynamic_sections_created
6856 || bed->always_renumber_dynsyms)
6857 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6858 &section_sym_count);
6859
8423293d
AM
6860 if (elf_hash_table (info)->dynamic_sections_created)
6861 {
6862 bfd *dynobj;
8423293d 6863 asection *s;
8423293d
AM
6864 unsigned int dtagcount;
6865
6866 dynobj = elf_hash_table (info)->dynobj;
6867
5a580b3a 6868 /* Work out the size of the symbol version section. */
3d4d4302 6869 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6870 BFD_ASSERT (s != NULL);
d5486c43 6871 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6872 {
eea6121a 6873 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6874 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6875 if (s->contents == NULL)
6876 return FALSE;
6877
6878 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6879 return FALSE;
6880 }
6881
6882 /* Set the size of the .dynsym and .hash sections. We counted
6883 the number of dynamic symbols in elf_link_add_object_symbols.
6884 We will build the contents of .dynsym and .hash when we build
6885 the final symbol table, because until then we do not know the
6886 correct value to give the symbols. We built the .dynstr
6887 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6888 s = elf_hash_table (info)->dynsym;
5a580b3a 6889 BFD_ASSERT (s != NULL);
eea6121a 6890 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6891
d5486c43
L
6892 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6893 if (s->contents == NULL)
6894 return FALSE;
5a580b3a 6895
d5486c43
L
6896 /* The first entry in .dynsym is a dummy symbol. Clear all the
6897 section syms, in case we don't output them all. */
6898 ++section_sym_count;
6899 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6900
fdc90cb4
JJ
6901 elf_hash_table (info)->bucketcount = 0;
6902
5a580b3a
AM
6903 /* Compute the size of the hashing table. As a side effect this
6904 computes the hash values for all the names we export. */
fdc90cb4
JJ
6905 if (info->emit_hash)
6906 {
6907 unsigned long int *hashcodes;
14b1c01e 6908 struct hash_codes_info hashinf;
fdc90cb4
JJ
6909 bfd_size_type amt;
6910 unsigned long int nsyms;
6911 size_t bucketcount;
6912 size_t hash_entry_size;
6913
6914 /* Compute the hash values for all exported symbols. At the same
6915 time store the values in an array so that we could use them for
6916 optimizations. */
6917 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6918 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6919 if (hashcodes == NULL)
6920 return FALSE;
14b1c01e
AM
6921 hashinf.hashcodes = hashcodes;
6922 hashinf.error = FALSE;
5a580b3a 6923
fdc90cb4
JJ
6924 /* Put all hash values in HASHCODES. */
6925 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6926 elf_collect_hash_codes, &hashinf);
6927 if (hashinf.error)
4dd07732
AM
6928 {
6929 free (hashcodes);
6930 return FALSE;
6931 }
5a580b3a 6932
14b1c01e 6933 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6934 bucketcount
6935 = compute_bucket_count (info, hashcodes, nsyms, 0);
6936 free (hashcodes);
6937
6938 if (bucketcount == 0)
6939 return FALSE;
5a580b3a 6940
fdc90cb4
JJ
6941 elf_hash_table (info)->bucketcount = bucketcount;
6942
3d4d4302 6943 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6944 BFD_ASSERT (s != NULL);
6945 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6946 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6947 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6948 if (s->contents == NULL)
6949 return FALSE;
6950
6951 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6952 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6953 s->contents + hash_entry_size);
6954 }
6955
6956 if (info->emit_gnu_hash)
6957 {
6958 size_t i, cnt;
6959 unsigned char *contents;
6960 struct collect_gnu_hash_codes cinfo;
6961 bfd_size_type amt;
6962 size_t bucketcount;
6963
6964 memset (&cinfo, 0, sizeof (cinfo));
6965
6966 /* Compute the hash values for all exported symbols. At the same
6967 time store the values in an array so that we could use them for
6968 optimizations. */
6969 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6970 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6971 if (cinfo.hashcodes == NULL)
6972 return FALSE;
6973
6974 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6975 cinfo.min_dynindx = -1;
6976 cinfo.output_bfd = output_bfd;
6977 cinfo.bed = bed;
6978
6979 /* Put all hash values in HASHCODES. */
6980 elf_link_hash_traverse (elf_hash_table (info),
6981 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6982 if (cinfo.error)
4dd07732
AM
6983 {
6984 free (cinfo.hashcodes);
6985 return FALSE;
6986 }
fdc90cb4
JJ
6987
6988 bucketcount
6989 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6990
6991 if (bucketcount == 0)
6992 {
6993 free (cinfo.hashcodes);
6994 return FALSE;
6995 }
6996
3d4d4302 6997 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6998 BFD_ASSERT (s != NULL);
6999
7000 if (cinfo.nsyms == 0)
7001 {
7002 /* Empty .gnu.hash section is special. */
7003 BFD_ASSERT (cinfo.min_dynindx == -1);
7004 free (cinfo.hashcodes);
7005 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7006 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7007 if (contents == NULL)
7008 return FALSE;
7009 s->contents = contents;
7010 /* 1 empty bucket. */
7011 bfd_put_32 (output_bfd, 1, contents);
7012 /* SYMIDX above the special symbol 0. */
7013 bfd_put_32 (output_bfd, 1, contents + 4);
7014 /* Just one word for bitmask. */
7015 bfd_put_32 (output_bfd, 1, contents + 8);
7016 /* Only hash fn bloom filter. */
7017 bfd_put_32 (output_bfd, 0, contents + 12);
7018 /* No hashes are valid - empty bitmask. */
7019 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7020 /* No hashes in the only bucket. */
7021 bfd_put_32 (output_bfd, 0,
7022 contents + 16 + bed->s->arch_size / 8);
7023 }
7024 else
7025 {
9e6619e2 7026 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7027 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7028
9e6619e2
AM
7029 x = cinfo.nsyms;
7030 maskbitslog2 = 1;
7031 while ((x >>= 1) != 0)
7032 ++maskbitslog2;
fdc90cb4
JJ
7033 if (maskbitslog2 < 3)
7034 maskbitslog2 = 5;
7035 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7036 maskbitslog2 = maskbitslog2 + 3;
7037 else
7038 maskbitslog2 = maskbitslog2 + 2;
7039 if (bed->s->arch_size == 64)
7040 {
7041 if (maskbitslog2 == 5)
7042 maskbitslog2 = 6;
7043 cinfo.shift1 = 6;
7044 }
7045 else
7046 cinfo.shift1 = 5;
7047 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7048 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7049 cinfo.maskbits = 1 << maskbitslog2;
7050 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7051 amt = bucketcount * sizeof (unsigned long int) * 2;
7052 amt += maskwords * sizeof (bfd_vma);
a50b1753 7053 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7054 if (cinfo.bitmask == NULL)
7055 {
7056 free (cinfo.hashcodes);
7057 return FALSE;
7058 }
7059
a50b1753 7060 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7061 cinfo.indx = cinfo.counts + bucketcount;
7062 cinfo.symindx = dynsymcount - cinfo.nsyms;
7063 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7064
7065 /* Determine how often each hash bucket is used. */
7066 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7067 for (i = 0; i < cinfo.nsyms; ++i)
7068 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7069
7070 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7071 if (cinfo.counts[i] != 0)
7072 {
7073 cinfo.indx[i] = cnt;
7074 cnt += cinfo.counts[i];
7075 }
7076 BFD_ASSERT (cnt == dynsymcount);
7077 cinfo.bucketcount = bucketcount;
7078 cinfo.local_indx = cinfo.min_dynindx;
7079
7080 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7081 s->size += cinfo.maskbits / 8;
a50b1753 7082 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7083 if (contents == NULL)
7084 {
7085 free (cinfo.bitmask);
7086 free (cinfo.hashcodes);
7087 return FALSE;
7088 }
7089
7090 s->contents = contents;
7091 bfd_put_32 (output_bfd, bucketcount, contents);
7092 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7093 bfd_put_32 (output_bfd, maskwords, contents + 8);
7094 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7095 contents += 16 + cinfo.maskbits / 8;
7096
7097 for (i = 0; i < bucketcount; ++i)
7098 {
7099 if (cinfo.counts[i] == 0)
7100 bfd_put_32 (output_bfd, 0, contents);
7101 else
7102 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7103 contents += 4;
7104 }
7105
7106 cinfo.contents = contents;
7107
7108 /* Renumber dynamic symbols, populate .gnu.hash section. */
7109 elf_link_hash_traverse (elf_hash_table (info),
7110 elf_renumber_gnu_hash_syms, &cinfo);
7111
7112 contents = s->contents + 16;
7113 for (i = 0; i < maskwords; ++i)
7114 {
7115 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7116 contents);
7117 contents += bed->s->arch_size / 8;
7118 }
7119
7120 free (cinfo.bitmask);
7121 free (cinfo.hashcodes);
7122 }
7123 }
5a580b3a 7124
3d4d4302 7125 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7126 BFD_ASSERT (s != NULL);
7127
4ad4eba5 7128 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7129
eea6121a 7130 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7131
7132 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7133 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7134 return FALSE;
7135 }
7136
7137 return TRUE;
7138}
4d269e42 7139\f
4d269e42
AM
7140/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7141
7142static void
7143merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7144 asection *sec)
7145{
dbaa2011
AM
7146 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7147 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7148}
7149
7150/* Finish SHF_MERGE section merging. */
7151
7152bfd_boolean
630993ec 7153_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7154{
7155 bfd *ibfd;
7156 asection *sec;
7157
7158 if (!is_elf_hash_table (info->hash))
7159 return FALSE;
7160
c72f2fb2 7161 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7162 if ((ibfd->flags & DYNAMIC) == 0
7163 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7164 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7165 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7166 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7167 if ((sec->flags & SEC_MERGE) != 0
7168 && !bfd_is_abs_section (sec->output_section))
7169 {
7170 struct bfd_elf_section_data *secdata;
7171
7172 secdata = elf_section_data (sec);
630993ec 7173 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7174 &elf_hash_table (info)->merge_info,
7175 sec, &secdata->sec_info))
7176 return FALSE;
7177 else if (secdata->sec_info)
dbaa2011 7178 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7179 }
7180
7181 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7182 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7183 merge_sections_remove_hook);
7184 return TRUE;
7185}
7186
7187/* Create an entry in an ELF linker hash table. */
7188
7189struct bfd_hash_entry *
7190_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7191 struct bfd_hash_table *table,
7192 const char *string)
7193{
7194 /* Allocate the structure if it has not already been allocated by a
7195 subclass. */
7196 if (entry == NULL)
7197 {
a50b1753 7198 entry = (struct bfd_hash_entry *)
ca4be51c 7199 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7200 if (entry == NULL)
7201 return entry;
7202 }
7203
7204 /* Call the allocation method of the superclass. */
7205 entry = _bfd_link_hash_newfunc (entry, table, string);
7206 if (entry != NULL)
7207 {
7208 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7209 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7210
7211 /* Set local fields. */
7212 ret->indx = -1;
7213 ret->dynindx = -1;
7214 ret->got = htab->init_got_refcount;
7215 ret->plt = htab->init_plt_refcount;
7216 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7217 - offsetof (struct elf_link_hash_entry, size)));
7218 /* Assume that we have been called by a non-ELF symbol reader.
7219 This flag is then reset by the code which reads an ELF input
7220 file. This ensures that a symbol created by a non-ELF symbol
7221 reader will have the flag set correctly. */
7222 ret->non_elf = 1;
7223 }
7224
7225 return entry;
7226}
7227
7228/* Copy data from an indirect symbol to its direct symbol, hiding the
7229 old indirect symbol. Also used for copying flags to a weakdef. */
7230
7231void
7232_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7233 struct elf_link_hash_entry *dir,
7234 struct elf_link_hash_entry *ind)
7235{
7236 struct elf_link_hash_table *htab;
7237
7238 /* Copy down any references that we may have already seen to the
e81830c5 7239 symbol which just became indirect. */
4d269e42 7240
422f1182 7241 if (dir->versioned != versioned_hidden)
e81830c5
AM
7242 dir->ref_dynamic |= ind->ref_dynamic;
7243 dir->ref_regular |= ind->ref_regular;
7244 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7245 dir->non_got_ref |= ind->non_got_ref;
7246 dir->needs_plt |= ind->needs_plt;
7247 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7248
7249 if (ind->root.type != bfd_link_hash_indirect)
7250 return;
7251
7252 /* Copy over the global and procedure linkage table refcount entries.
7253 These may have been already set up by a check_relocs routine. */
7254 htab = elf_hash_table (info);
7255 if (ind->got.refcount > htab->init_got_refcount.refcount)
7256 {
7257 if (dir->got.refcount < 0)
7258 dir->got.refcount = 0;
7259 dir->got.refcount += ind->got.refcount;
7260 ind->got.refcount = htab->init_got_refcount.refcount;
7261 }
7262
7263 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7264 {
7265 if (dir->plt.refcount < 0)
7266 dir->plt.refcount = 0;
7267 dir->plt.refcount += ind->plt.refcount;
7268 ind->plt.refcount = htab->init_plt_refcount.refcount;
7269 }
7270
7271 if (ind->dynindx != -1)
7272 {
7273 if (dir->dynindx != -1)
7274 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7275 dir->dynindx = ind->dynindx;
7276 dir->dynstr_index = ind->dynstr_index;
7277 ind->dynindx = -1;
7278 ind->dynstr_index = 0;
7279 }
7280}
7281
7282void
7283_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7284 struct elf_link_hash_entry *h,
7285 bfd_boolean force_local)
7286{
3aa14d16
L
7287 /* STT_GNU_IFUNC symbol must go through PLT. */
7288 if (h->type != STT_GNU_IFUNC)
7289 {
7290 h->plt = elf_hash_table (info)->init_plt_offset;
7291 h->needs_plt = 0;
7292 }
4d269e42
AM
7293 if (force_local)
7294 {
7295 h->forced_local = 1;
7296 if (h->dynindx != -1)
7297 {
4d269e42
AM
7298 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7299 h->dynstr_index);
641338d8
AM
7300 h->dynindx = -1;
7301 h->dynstr_index = 0;
4d269e42
AM
7302 }
7303 }
7304}
7305
7bf52ea2
AM
7306/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7307 caller. */
4d269e42
AM
7308
7309bfd_boolean
7310_bfd_elf_link_hash_table_init
7311 (struct elf_link_hash_table *table,
7312 bfd *abfd,
7313 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7314 struct bfd_hash_table *,
7315 const char *),
4dfe6ac6
NC
7316 unsigned int entsize,
7317 enum elf_target_id target_id)
4d269e42
AM
7318{
7319 bfd_boolean ret;
7320 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7321
4d269e42
AM
7322 table->init_got_refcount.refcount = can_refcount - 1;
7323 table->init_plt_refcount.refcount = can_refcount - 1;
7324 table->init_got_offset.offset = -(bfd_vma) 1;
7325 table->init_plt_offset.offset = -(bfd_vma) 1;
7326 /* The first dynamic symbol is a dummy. */
7327 table->dynsymcount = 1;
7328
7329 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7330
4d269e42 7331 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7332 table->hash_table_id = target_id;
4d269e42
AM
7333
7334 return ret;
7335}
7336
7337/* Create an ELF linker hash table. */
7338
7339struct bfd_link_hash_table *
7340_bfd_elf_link_hash_table_create (bfd *abfd)
7341{
7342 struct elf_link_hash_table *ret;
7343 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7344
7bf52ea2 7345 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7346 if (ret == NULL)
7347 return NULL;
7348
7349 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7350 sizeof (struct elf_link_hash_entry),
7351 GENERIC_ELF_DATA))
4d269e42
AM
7352 {
7353 free (ret);
7354 return NULL;
7355 }
d495ab0d 7356 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7357
7358 return &ret->root;
7359}
7360
9f7c3e5e
AM
7361/* Destroy an ELF linker hash table. */
7362
7363void
d495ab0d 7364_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7365{
d495ab0d
AM
7366 struct elf_link_hash_table *htab;
7367
7368 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7369 if (htab->dynstr != NULL)
7370 _bfd_elf_strtab_free (htab->dynstr);
7371 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7372 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7373}
7374
4d269e42
AM
7375/* This is a hook for the ELF emulation code in the generic linker to
7376 tell the backend linker what file name to use for the DT_NEEDED
7377 entry for a dynamic object. */
7378
7379void
7380bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7381{
7382 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7383 && bfd_get_format (abfd) == bfd_object)
7384 elf_dt_name (abfd) = name;
7385}
7386
7387int
7388bfd_elf_get_dyn_lib_class (bfd *abfd)
7389{
7390 int lib_class;
7391 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7392 && bfd_get_format (abfd) == bfd_object)
7393 lib_class = elf_dyn_lib_class (abfd);
7394 else
7395 lib_class = 0;
7396 return lib_class;
7397}
7398
7399void
7400bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7401{
7402 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7403 && bfd_get_format (abfd) == bfd_object)
7404 elf_dyn_lib_class (abfd) = lib_class;
7405}
7406
7407/* Get the list of DT_NEEDED entries for a link. This is a hook for
7408 the linker ELF emulation code. */
7409
7410struct bfd_link_needed_list *
7411bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7412 struct bfd_link_info *info)
7413{
7414 if (! is_elf_hash_table (info->hash))
7415 return NULL;
7416 return elf_hash_table (info)->needed;
7417}
7418
7419/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7420 hook for the linker ELF emulation code. */
7421
7422struct bfd_link_needed_list *
7423bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7424 struct bfd_link_info *info)
7425{
7426 if (! is_elf_hash_table (info->hash))
7427 return NULL;
7428 return elf_hash_table (info)->runpath;
7429}
7430
7431/* Get the name actually used for a dynamic object for a link. This
7432 is the SONAME entry if there is one. Otherwise, it is the string
7433 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7434
7435const char *
7436bfd_elf_get_dt_soname (bfd *abfd)
7437{
7438 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7439 && bfd_get_format (abfd) == bfd_object)
7440 return elf_dt_name (abfd);
7441 return NULL;
7442}
7443
7444/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7445 the ELF linker emulation code. */
7446
7447bfd_boolean
7448bfd_elf_get_bfd_needed_list (bfd *abfd,
7449 struct bfd_link_needed_list **pneeded)
7450{
7451 asection *s;
7452 bfd_byte *dynbuf = NULL;
cb33740c 7453 unsigned int elfsec;
4d269e42
AM
7454 unsigned long shlink;
7455 bfd_byte *extdyn, *extdynend;
7456 size_t extdynsize;
7457 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7458
7459 *pneeded = NULL;
7460
7461 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7462 || bfd_get_format (abfd) != bfd_object)
7463 return TRUE;
7464
7465 s = bfd_get_section_by_name (abfd, ".dynamic");
7466 if (s == NULL || s->size == 0)
7467 return TRUE;
7468
7469 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7470 goto error_return;
7471
7472 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7473 if (elfsec == SHN_BAD)
4d269e42
AM
7474 goto error_return;
7475
7476 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7477
4d269e42
AM
7478 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7479 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7480
7481 extdyn = dynbuf;
7482 extdynend = extdyn + s->size;
7483 for (; extdyn < extdynend; extdyn += extdynsize)
7484 {
7485 Elf_Internal_Dyn dyn;
7486
7487 (*swap_dyn_in) (abfd, extdyn, &dyn);
7488
7489 if (dyn.d_tag == DT_NULL)
7490 break;
7491
7492 if (dyn.d_tag == DT_NEEDED)
7493 {
7494 const char *string;
7495 struct bfd_link_needed_list *l;
7496 unsigned int tagv = dyn.d_un.d_val;
7497 bfd_size_type amt;
7498
7499 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7500 if (string == NULL)
7501 goto error_return;
7502
7503 amt = sizeof *l;
a50b1753 7504 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7505 if (l == NULL)
7506 goto error_return;
7507
7508 l->by = abfd;
7509 l->name = string;
7510 l->next = *pneeded;
7511 *pneeded = l;
7512 }
7513 }
7514
7515 free (dynbuf);
7516
7517 return TRUE;
7518
7519 error_return:
7520 if (dynbuf != NULL)
7521 free (dynbuf);
7522 return FALSE;
7523}
7524
7525struct elf_symbuf_symbol
7526{
7527 unsigned long st_name; /* Symbol name, index in string tbl */
7528 unsigned char st_info; /* Type and binding attributes */
7529 unsigned char st_other; /* Visibilty, and target specific */
7530};
7531
7532struct elf_symbuf_head
7533{
7534 struct elf_symbuf_symbol *ssym;
ef53be89 7535 size_t count;
4d269e42
AM
7536 unsigned int st_shndx;
7537};
7538
7539struct elf_symbol
7540{
7541 union
7542 {
7543 Elf_Internal_Sym *isym;
7544 struct elf_symbuf_symbol *ssym;
7545 } u;
7546 const char *name;
7547};
7548
7549/* Sort references to symbols by ascending section number. */
7550
7551static int
7552elf_sort_elf_symbol (const void *arg1, const void *arg2)
7553{
7554 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7555 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7556
7557 return s1->st_shndx - s2->st_shndx;
7558}
7559
7560static int
7561elf_sym_name_compare (const void *arg1, const void *arg2)
7562{
7563 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7564 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7565 return strcmp (s1->name, s2->name);
7566}
7567
7568static struct elf_symbuf_head *
ef53be89 7569elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7570{
14b1c01e 7571 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7572 struct elf_symbuf_symbol *ssym;
7573 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7574 size_t i, shndx_count, total_size;
4d269e42 7575
a50b1753 7576 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7577 if (indbuf == NULL)
7578 return NULL;
7579
7580 for (ind = indbuf, i = 0; i < symcount; i++)
7581 if (isymbuf[i].st_shndx != SHN_UNDEF)
7582 *ind++ = &isymbuf[i];
7583 indbufend = ind;
7584
7585 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7586 elf_sort_elf_symbol);
7587
7588 shndx_count = 0;
7589 if (indbufend > indbuf)
7590 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7591 if (ind[0]->st_shndx != ind[1]->st_shndx)
7592 shndx_count++;
7593
3ae181ee
L
7594 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7595 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7596 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7597 if (ssymbuf == NULL)
7598 {
7599 free (indbuf);
7600 return NULL;
7601 }
7602
3ae181ee 7603 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7604 ssymbuf->ssym = NULL;
7605 ssymbuf->count = shndx_count;
7606 ssymbuf->st_shndx = 0;
7607 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7608 {
7609 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7610 {
7611 ssymhead++;
7612 ssymhead->ssym = ssym;
7613 ssymhead->count = 0;
7614 ssymhead->st_shndx = (*ind)->st_shndx;
7615 }
7616 ssym->st_name = (*ind)->st_name;
7617 ssym->st_info = (*ind)->st_info;
7618 ssym->st_other = (*ind)->st_other;
7619 ssymhead->count++;
7620 }
ef53be89 7621 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7622 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7623 == total_size));
4d269e42
AM
7624
7625 free (indbuf);
7626 return ssymbuf;
7627}
7628
7629/* Check if 2 sections define the same set of local and global
7630 symbols. */
7631
8f317e31 7632static bfd_boolean
4d269e42
AM
7633bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7634 struct bfd_link_info *info)
7635{
7636 bfd *bfd1, *bfd2;
7637 const struct elf_backend_data *bed1, *bed2;
7638 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7639 size_t symcount1, symcount2;
4d269e42
AM
7640 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7641 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7642 Elf_Internal_Sym *isym, *isymend;
7643 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7644 size_t count1, count2, i;
cb33740c 7645 unsigned int shndx1, shndx2;
4d269e42
AM
7646 bfd_boolean result;
7647
7648 bfd1 = sec1->owner;
7649 bfd2 = sec2->owner;
7650
4d269e42
AM
7651 /* Both sections have to be in ELF. */
7652 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7653 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7654 return FALSE;
7655
7656 if (elf_section_type (sec1) != elf_section_type (sec2))
7657 return FALSE;
7658
4d269e42
AM
7659 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7660 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7661 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7662 return FALSE;
7663
7664 bed1 = get_elf_backend_data (bfd1);
7665 bed2 = get_elf_backend_data (bfd2);
7666 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7667 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7668 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7669 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7670
7671 if (symcount1 == 0 || symcount2 == 0)
7672 return FALSE;
7673
7674 result = FALSE;
7675 isymbuf1 = NULL;
7676 isymbuf2 = NULL;
a50b1753
NC
7677 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7678 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7679
7680 if (ssymbuf1 == NULL)
7681 {
7682 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7683 NULL, NULL, NULL);
7684 if (isymbuf1 == NULL)
7685 goto done;
7686
7687 if (!info->reduce_memory_overheads)
7688 elf_tdata (bfd1)->symbuf = ssymbuf1
7689 = elf_create_symbuf (symcount1, isymbuf1);
7690 }
7691
7692 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7693 {
7694 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7695 NULL, NULL, NULL);
7696 if (isymbuf2 == NULL)
7697 goto done;
7698
7699 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7700 elf_tdata (bfd2)->symbuf = ssymbuf2
7701 = elf_create_symbuf (symcount2, isymbuf2);
7702 }
7703
7704 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7705 {
7706 /* Optimized faster version. */
ef53be89 7707 size_t lo, hi, mid;
4d269e42
AM
7708 struct elf_symbol *symp;
7709 struct elf_symbuf_symbol *ssym, *ssymend;
7710
7711 lo = 0;
7712 hi = ssymbuf1->count;
7713 ssymbuf1++;
7714 count1 = 0;
7715 while (lo < hi)
7716 {
7717 mid = (lo + hi) / 2;
cb33740c 7718 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7719 hi = mid;
cb33740c 7720 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7721 lo = mid + 1;
7722 else
7723 {
7724 count1 = ssymbuf1[mid].count;
7725 ssymbuf1 += mid;
7726 break;
7727 }
7728 }
7729
7730 lo = 0;
7731 hi = ssymbuf2->count;
7732 ssymbuf2++;
7733 count2 = 0;
7734 while (lo < hi)
7735 {
7736 mid = (lo + hi) / 2;
cb33740c 7737 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7738 hi = mid;
cb33740c 7739 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7740 lo = mid + 1;
7741 else
7742 {
7743 count2 = ssymbuf2[mid].count;
7744 ssymbuf2 += mid;
7745 break;
7746 }
7747 }
7748
7749 if (count1 == 0 || count2 == 0 || count1 != count2)
7750 goto done;
7751
ca4be51c
AM
7752 symtable1
7753 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7754 symtable2
7755 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7756 if (symtable1 == NULL || symtable2 == NULL)
7757 goto done;
7758
7759 symp = symtable1;
7760 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7761 ssym < ssymend; ssym++, symp++)
7762 {
7763 symp->u.ssym = ssym;
7764 symp->name = bfd_elf_string_from_elf_section (bfd1,
7765 hdr1->sh_link,
7766 ssym->st_name);
7767 }
7768
7769 symp = symtable2;
7770 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7771 ssym < ssymend; ssym++, symp++)
7772 {
7773 symp->u.ssym = ssym;
7774 symp->name = bfd_elf_string_from_elf_section (bfd2,
7775 hdr2->sh_link,
7776 ssym->st_name);
7777 }
7778
7779 /* Sort symbol by name. */
7780 qsort (symtable1, count1, sizeof (struct elf_symbol),
7781 elf_sym_name_compare);
7782 qsort (symtable2, count1, sizeof (struct elf_symbol),
7783 elf_sym_name_compare);
7784
7785 for (i = 0; i < count1; i++)
7786 /* Two symbols must have the same binding, type and name. */
7787 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7788 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7789 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7790 goto done;
7791
7792 result = TRUE;
7793 goto done;
7794 }
7795
a50b1753
NC
7796 symtable1 = (struct elf_symbol *)
7797 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7798 symtable2 = (struct elf_symbol *)
7799 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7800 if (symtable1 == NULL || symtable2 == NULL)
7801 goto done;
7802
7803 /* Count definitions in the section. */
7804 count1 = 0;
7805 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7806 if (isym->st_shndx == shndx1)
4d269e42
AM
7807 symtable1[count1++].u.isym = isym;
7808
7809 count2 = 0;
7810 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7811 if (isym->st_shndx == shndx2)
4d269e42
AM
7812 symtable2[count2++].u.isym = isym;
7813
7814 if (count1 == 0 || count2 == 0 || count1 != count2)
7815 goto done;
7816
7817 for (i = 0; i < count1; i++)
7818 symtable1[i].name
7819 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7820 symtable1[i].u.isym->st_name);
7821
7822 for (i = 0; i < count2; i++)
7823 symtable2[i].name
7824 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7825 symtable2[i].u.isym->st_name);
7826
7827 /* Sort symbol by name. */
7828 qsort (symtable1, count1, sizeof (struct elf_symbol),
7829 elf_sym_name_compare);
7830 qsort (symtable2, count1, sizeof (struct elf_symbol),
7831 elf_sym_name_compare);
7832
7833 for (i = 0; i < count1; i++)
7834 /* Two symbols must have the same binding, type and name. */
7835 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7836 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7837 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7838 goto done;
7839
7840 result = TRUE;
7841
7842done:
7843 if (symtable1)
7844 free (symtable1);
7845 if (symtable2)
7846 free (symtable2);
7847 if (isymbuf1)
7848 free (isymbuf1);
7849 if (isymbuf2)
7850 free (isymbuf2);
7851
7852 return result;
7853}
7854
7855/* Return TRUE if 2 section types are compatible. */
7856
7857bfd_boolean
7858_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7859 bfd *bbfd, const asection *bsec)
7860{
7861 if (asec == NULL
7862 || bsec == NULL
7863 || abfd->xvec->flavour != bfd_target_elf_flavour
7864 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7865 return TRUE;
7866
7867 return elf_section_type (asec) == elf_section_type (bsec);
7868}
7869\f
c152c796
AM
7870/* Final phase of ELF linker. */
7871
7872/* A structure we use to avoid passing large numbers of arguments. */
7873
7874struct elf_final_link_info
7875{
7876 /* General link information. */
7877 struct bfd_link_info *info;
7878 /* Output BFD. */
7879 bfd *output_bfd;
7880 /* Symbol string table. */
ef10c3ac 7881 struct elf_strtab_hash *symstrtab;
c152c796
AM
7882 /* .hash section. */
7883 asection *hash_sec;
7884 /* symbol version section (.gnu.version). */
7885 asection *symver_sec;
7886 /* Buffer large enough to hold contents of any section. */
7887 bfd_byte *contents;
7888 /* Buffer large enough to hold external relocs of any section. */
7889 void *external_relocs;
7890 /* Buffer large enough to hold internal relocs of any section. */
7891 Elf_Internal_Rela *internal_relocs;
7892 /* Buffer large enough to hold external local symbols of any input
7893 BFD. */
7894 bfd_byte *external_syms;
7895 /* And a buffer for symbol section indices. */
7896 Elf_External_Sym_Shndx *locsym_shndx;
7897 /* Buffer large enough to hold internal local symbols of any input
7898 BFD. */
7899 Elf_Internal_Sym *internal_syms;
7900 /* Array large enough to hold a symbol index for each local symbol
7901 of any input BFD. */
7902 long *indices;
7903 /* Array large enough to hold a section pointer for each local
7904 symbol of any input BFD. */
7905 asection **sections;
ef10c3ac 7906 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7907 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7908 /* Number of STT_FILE syms seen. */
7909 size_t filesym_count;
c152c796
AM
7910};
7911
7912/* This struct is used to pass information to elf_link_output_extsym. */
7913
7914struct elf_outext_info
7915{
7916 bfd_boolean failed;
7917 bfd_boolean localsyms;
34a79995 7918 bfd_boolean file_sym_done;
8b127cbc 7919 struct elf_final_link_info *flinfo;
c152c796
AM
7920};
7921
d9352518
DB
7922
7923/* Support for evaluating a complex relocation.
7924
7925 Complex relocations are generalized, self-describing relocations. The
7926 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7927 relocations themselves.
d9352518
DB
7928
7929 The relocations are use a reserved elf-wide relocation type code (R_RELC
7930 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7931 information (start bit, end bit, word width, etc) into the addend. This
7932 information is extracted from CGEN-generated operand tables within gas.
7933
7934 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7935 internal) representing prefix-notation expressions, including but not
7936 limited to those sorts of expressions normally encoded as addends in the
7937 addend field. The symbol mangling format is:
7938
7939 <node> := <literal>
7940 | <unary-operator> ':' <node>
7941 | <binary-operator> ':' <node> ':' <node>
7942 ;
7943
7944 <literal> := 's' <digits=N> ':' <N character symbol name>
7945 | 'S' <digits=N> ':' <N character section name>
7946 | '#' <hexdigits>
7947 ;
7948
7949 <binary-operator> := as in C
7950 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7951
7952static void
a0c8462f
AM
7953set_symbol_value (bfd *bfd_with_globals,
7954 Elf_Internal_Sym *isymbuf,
7955 size_t locsymcount,
7956 size_t symidx,
7957 bfd_vma val)
d9352518 7958{
8977835c
AM
7959 struct elf_link_hash_entry **sym_hashes;
7960 struct elf_link_hash_entry *h;
7961 size_t extsymoff = locsymcount;
d9352518 7962
8977835c 7963 if (symidx < locsymcount)
d9352518 7964 {
8977835c
AM
7965 Elf_Internal_Sym *sym;
7966
7967 sym = isymbuf + symidx;
7968 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7969 {
7970 /* It is a local symbol: move it to the
7971 "absolute" section and give it a value. */
7972 sym->st_shndx = SHN_ABS;
7973 sym->st_value = val;
7974 return;
7975 }
7976 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7977 extsymoff = 0;
d9352518 7978 }
8977835c
AM
7979
7980 /* It is a global symbol: set its link type
7981 to "defined" and give it a value. */
7982
7983 sym_hashes = elf_sym_hashes (bfd_with_globals);
7984 h = sym_hashes [symidx - extsymoff];
7985 while (h->root.type == bfd_link_hash_indirect
7986 || h->root.type == bfd_link_hash_warning)
7987 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7988 h->root.type = bfd_link_hash_defined;
7989 h->root.u.def.value = val;
7990 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7991}
7992
a0c8462f
AM
7993static bfd_boolean
7994resolve_symbol (const char *name,
7995 bfd *input_bfd,
8b127cbc 7996 struct elf_final_link_info *flinfo,
a0c8462f
AM
7997 bfd_vma *result,
7998 Elf_Internal_Sym *isymbuf,
7999 size_t locsymcount)
d9352518 8000{
a0c8462f
AM
8001 Elf_Internal_Sym *sym;
8002 struct bfd_link_hash_entry *global_entry;
8003 const char *candidate = NULL;
8004 Elf_Internal_Shdr *symtab_hdr;
8005 size_t i;
8006
d9352518
DB
8007 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8008
8009 for (i = 0; i < locsymcount; ++ i)
8010 {
8977835c 8011 sym = isymbuf + i;
d9352518
DB
8012
8013 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8014 continue;
8015
8016 candidate = bfd_elf_string_from_elf_section (input_bfd,
8017 symtab_hdr->sh_link,
8018 sym->st_name);
8019#ifdef DEBUG
0f02bbd9
AM
8020 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8021 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8022#endif
8023 if (candidate && strcmp (candidate, name) == 0)
8024 {
8b127cbc 8025 asection *sec = flinfo->sections [i];
d9352518 8026
0f02bbd9
AM
8027 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8028 *result += sec->output_offset + sec->output_section->vma;
d9352518 8029#ifdef DEBUG
0f02bbd9
AM
8030 printf ("Found symbol with value %8.8lx\n",
8031 (unsigned long) *result);
d9352518
DB
8032#endif
8033 return TRUE;
8034 }
8035 }
8036
8037 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8038 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8039 FALSE, FALSE, TRUE);
d9352518
DB
8040 if (!global_entry)
8041 return FALSE;
a0c8462f 8042
d9352518
DB
8043 if (global_entry->type == bfd_link_hash_defined
8044 || global_entry->type == bfd_link_hash_defweak)
8045 {
a0c8462f
AM
8046 *result = (global_entry->u.def.value
8047 + global_entry->u.def.section->output_section->vma
8048 + global_entry->u.def.section->output_offset);
d9352518 8049#ifdef DEBUG
0f02bbd9
AM
8050 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8051 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8052#endif
8053 return TRUE;
a0c8462f 8054 }
d9352518 8055
d9352518
DB
8056 return FALSE;
8057}
8058
37b01f6a
DG
8059/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8060 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8061 names like "foo.end" which is the end address of section "foo". */
8062
d9352518 8063static bfd_boolean
a0c8462f
AM
8064resolve_section (const char *name,
8065 asection *sections,
37b01f6a
DG
8066 bfd_vma *result,
8067 bfd * abfd)
d9352518 8068{
a0c8462f
AM
8069 asection *curr;
8070 unsigned int len;
d9352518 8071
a0c8462f 8072 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8073 if (strcmp (curr->name, name) == 0)
8074 {
8075 *result = curr->vma;
8076 return TRUE;
8077 }
8078
8079 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8080 /* FIXME: This could be coded more efficiently... */
a0c8462f 8081 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8082 {
8083 len = strlen (curr->name);
a0c8462f 8084 if (len > strlen (name))
d9352518
DB
8085 continue;
8086
8087 if (strncmp (curr->name, name, len) == 0)
8088 {
8089 if (strncmp (".end", name + len, 4) == 0)
8090 {
37b01f6a 8091 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8092 return TRUE;
8093 }
8094
8095 /* Insert more pseudo-section names here, if you like. */
8096 }
8097 }
a0c8462f 8098
d9352518
DB
8099 return FALSE;
8100}
8101
8102static void
a0c8462f 8103undefined_reference (const char *reftype, const char *name)
d9352518 8104{
695344c0 8105 /* xgettext:c-format */
a0c8462f
AM
8106 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8107 reftype, name);
d9352518
DB
8108}
8109
8110static bfd_boolean
a0c8462f
AM
8111eval_symbol (bfd_vma *result,
8112 const char **symp,
8113 bfd *input_bfd,
8b127cbc 8114 struct elf_final_link_info *flinfo,
a0c8462f
AM
8115 bfd_vma dot,
8116 Elf_Internal_Sym *isymbuf,
8117 size_t locsymcount,
8118 int signed_p)
d9352518 8119{
4b93929b
NC
8120 size_t len;
8121 size_t symlen;
a0c8462f
AM
8122 bfd_vma a;
8123 bfd_vma b;
4b93929b 8124 char symbuf[4096];
0f02bbd9 8125 const char *sym = *symp;
a0c8462f
AM
8126 const char *symend;
8127 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8128
8129 len = strlen (sym);
8130 symend = sym + len;
8131
4b93929b 8132 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8133 {
8134 bfd_set_error (bfd_error_invalid_operation);
8135 return FALSE;
8136 }
a0c8462f 8137
d9352518
DB
8138 switch (* sym)
8139 {
8140 case '.':
0f02bbd9
AM
8141 *result = dot;
8142 *symp = sym + 1;
d9352518
DB
8143 return TRUE;
8144
8145 case '#':
0f02bbd9
AM
8146 ++sym;
8147 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8148 return TRUE;
8149
8150 case 'S':
8151 symbol_is_section = TRUE;
1a0670f3 8152 /* Fall through. */
a0c8462f 8153 case 's':
0f02bbd9
AM
8154 ++sym;
8155 symlen = strtol (sym, (char **) symp, 10);
8156 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8157
4b93929b 8158 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8159 {
8160 bfd_set_error (bfd_error_invalid_operation);
8161 return FALSE;
8162 }
8163
8164 memcpy (symbuf, sym, symlen);
a0c8462f 8165 symbuf[symlen] = '\0';
0f02bbd9 8166 *symp = sym + symlen;
a0c8462f
AM
8167
8168 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8169 the symbol as a section, or vice-versa. so we're pretty liberal in our
8170 interpretation here; section means "try section first", not "must be a
8171 section", and likewise with symbol. */
8172
a0c8462f 8173 if (symbol_is_section)
d9352518 8174 {
37b01f6a 8175 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8176 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8177 isymbuf, locsymcount))
d9352518
DB
8178 {
8179 undefined_reference ("section", symbuf);
8180 return FALSE;
8181 }
a0c8462f
AM
8182 }
8183 else
d9352518 8184 {
8b127cbc 8185 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8186 isymbuf, locsymcount)
8b127cbc 8187 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8188 result, input_bfd))
d9352518
DB
8189 {
8190 undefined_reference ("symbol", symbuf);
8191 return FALSE;
8192 }
8193 }
8194
8195 return TRUE;
a0c8462f 8196
d9352518
DB
8197 /* All that remains are operators. */
8198
8199#define UNARY_OP(op) \
8200 if (strncmp (sym, #op, strlen (#op)) == 0) \
8201 { \
8202 sym += strlen (#op); \
a0c8462f
AM
8203 if (*sym == ':') \
8204 ++sym; \
0f02bbd9 8205 *symp = sym; \
8b127cbc 8206 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8207 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8208 return FALSE; \
8209 if (signed_p) \
0f02bbd9 8210 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8211 else \
8212 *result = op a; \
d9352518
DB
8213 return TRUE; \
8214 }
8215
8216#define BINARY_OP(op) \
8217 if (strncmp (sym, #op, strlen (#op)) == 0) \
8218 { \
8219 sym += strlen (#op); \
a0c8462f
AM
8220 if (*sym == ':') \
8221 ++sym; \
0f02bbd9 8222 *symp = sym; \
8b127cbc 8223 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8224 isymbuf, locsymcount, signed_p)) \
a0c8462f 8225 return FALSE; \
0f02bbd9 8226 ++*symp; \
8b127cbc 8227 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8228 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8229 return FALSE; \
8230 if (signed_p) \
0f02bbd9 8231 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8232 else \
8233 *result = a op b; \
d9352518
DB
8234 return TRUE; \
8235 }
8236
8237 default:
8238 UNARY_OP (0-);
8239 BINARY_OP (<<);
8240 BINARY_OP (>>);
8241 BINARY_OP (==);
8242 BINARY_OP (!=);
8243 BINARY_OP (<=);
8244 BINARY_OP (>=);
8245 BINARY_OP (&&);
8246 BINARY_OP (||);
8247 UNARY_OP (~);
8248 UNARY_OP (!);
8249 BINARY_OP (*);
8250 BINARY_OP (/);
8251 BINARY_OP (%);
8252 BINARY_OP (^);
8253 BINARY_OP (|);
8254 BINARY_OP (&);
8255 BINARY_OP (+);
8256 BINARY_OP (-);
8257 BINARY_OP (<);
8258 BINARY_OP (>);
8259#undef UNARY_OP
8260#undef BINARY_OP
8261 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8262 bfd_set_error (bfd_error_invalid_operation);
8263 return FALSE;
8264 }
8265}
8266
d9352518 8267static void
a0c8462f
AM
8268put_value (bfd_vma size,
8269 unsigned long chunksz,
8270 bfd *input_bfd,
8271 bfd_vma x,
8272 bfd_byte *location)
d9352518
DB
8273{
8274 location += (size - chunksz);
8275
41cd1ad1 8276 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8277 {
8278 switch (chunksz)
8279 {
d9352518
DB
8280 case 1:
8281 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8282 x >>= 8;
d9352518
DB
8283 break;
8284 case 2:
8285 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8286 x >>= 16;
d9352518
DB
8287 break;
8288 case 4:
8289 bfd_put_32 (input_bfd, x, location);
65164438
NC
8290 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8291 x >>= 16;
8292 x >>= 16;
d9352518 8293 break;
d9352518 8294#ifdef BFD64
41cd1ad1 8295 case 8:
d9352518 8296 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8297 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8298 x >>= 32;
8299 x >>= 32;
8300 break;
d9352518 8301#endif
41cd1ad1
NC
8302 default:
8303 abort ();
d9352518
DB
8304 break;
8305 }
8306 }
8307}
8308
a0c8462f
AM
8309static bfd_vma
8310get_value (bfd_vma size,
8311 unsigned long chunksz,
8312 bfd *input_bfd,
8313 bfd_byte *location)
d9352518 8314{
9b239e0e 8315 int shift;
d9352518
DB
8316 bfd_vma x = 0;
8317
9b239e0e
NC
8318 /* Sanity checks. */
8319 BFD_ASSERT (chunksz <= sizeof (x)
8320 && size >= chunksz
8321 && chunksz != 0
8322 && (size % chunksz) == 0
8323 && input_bfd != NULL
8324 && location != NULL);
8325
8326 if (chunksz == sizeof (x))
8327 {
8328 BFD_ASSERT (size == chunksz);
8329
8330 /* Make sure that we do not perform an undefined shift operation.
8331 We know that size == chunksz so there will only be one iteration
8332 of the loop below. */
8333 shift = 0;
8334 }
8335 else
8336 shift = 8 * chunksz;
8337
a0c8462f 8338 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8339 {
8340 switch (chunksz)
8341 {
d9352518 8342 case 1:
9b239e0e 8343 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8344 break;
8345 case 2:
9b239e0e 8346 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8347 break;
8348 case 4:
9b239e0e 8349 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8350 break;
d9352518 8351#ifdef BFD64
9b239e0e
NC
8352 case 8:
8353 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8354 break;
9b239e0e
NC
8355#endif
8356 default:
8357 abort ();
d9352518
DB
8358 }
8359 }
8360 return x;
8361}
8362
a0c8462f
AM
8363static void
8364decode_complex_addend (unsigned long *start, /* in bits */
8365 unsigned long *oplen, /* in bits */
8366 unsigned long *len, /* in bits */
8367 unsigned long *wordsz, /* in bytes */
8368 unsigned long *chunksz, /* in bytes */
8369 unsigned long *lsb0_p,
8370 unsigned long *signed_p,
8371 unsigned long *trunc_p,
8372 unsigned long encoded)
d9352518
DB
8373{
8374 * start = encoded & 0x3F;
8375 * len = (encoded >> 6) & 0x3F;
8376 * oplen = (encoded >> 12) & 0x3F;
8377 * wordsz = (encoded >> 18) & 0xF;
8378 * chunksz = (encoded >> 22) & 0xF;
8379 * lsb0_p = (encoded >> 27) & 1;
8380 * signed_p = (encoded >> 28) & 1;
8381 * trunc_p = (encoded >> 29) & 1;
8382}
8383
cdfeee4f 8384bfd_reloc_status_type
0f02bbd9 8385bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8386 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8387 bfd_byte *contents,
8388 Elf_Internal_Rela *rel,
8389 bfd_vma relocation)
d9352518 8390{
0f02bbd9
AM
8391 bfd_vma shift, x, mask;
8392 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8393 bfd_reloc_status_type r;
d9352518
DB
8394
8395 /* Perform this reloc, since it is complex.
8396 (this is not to say that it necessarily refers to a complex
8397 symbol; merely that it is a self-describing CGEN based reloc.
8398 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8399 word size, etc) encoded within it.). */
d9352518 8400
a0c8462f
AM
8401 decode_complex_addend (&start, &oplen, &len, &wordsz,
8402 &chunksz, &lsb0_p, &signed_p,
8403 &trunc_p, rel->r_addend);
d9352518
DB
8404
8405 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8406
8407 if (lsb0_p)
8408 shift = (start + 1) - len;
8409 else
8410 shift = (8 * wordsz) - (start + len);
8411
37b01f6a
DG
8412 x = get_value (wordsz, chunksz, input_bfd,
8413 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8414
8415#ifdef DEBUG
8416 printf ("Doing complex reloc: "
8417 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8418 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8419 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8420 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8421 oplen, (unsigned long) x, (unsigned long) mask,
8422 (unsigned long) relocation);
d9352518
DB
8423#endif
8424
cdfeee4f 8425 r = bfd_reloc_ok;
d9352518 8426 if (! trunc_p)
cdfeee4f
AM
8427 /* Now do an overflow check. */
8428 r = bfd_check_overflow ((signed_p
8429 ? complain_overflow_signed
8430 : complain_overflow_unsigned),
8431 len, 0, (8 * wordsz),
8432 relocation);
a0c8462f 8433
d9352518
DB
8434 /* Do the deed. */
8435 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8436
8437#ifdef DEBUG
8438 printf (" relocation: %8.8lx\n"
8439 " shifted mask: %8.8lx\n"
8440 " shifted/masked reloc: %8.8lx\n"
8441 " result: %8.8lx\n",
9ccb8af9
AM
8442 (unsigned long) relocation, (unsigned long) (mask << shift),
8443 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8444#endif
37b01f6a
DG
8445 put_value (wordsz, chunksz, input_bfd, x,
8446 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8447 return r;
d9352518
DB
8448}
8449
0e287786
AM
8450/* Functions to read r_offset from external (target order) reloc
8451 entry. Faster than bfd_getl32 et al, because we let the compiler
8452 know the value is aligned. */
53df40a4 8453
0e287786
AM
8454static bfd_vma
8455ext32l_r_offset (const void *p)
53df40a4
AM
8456{
8457 union aligned32
8458 {
8459 uint32_t v;
8460 unsigned char c[4];
8461 };
8462 const union aligned32 *a
0e287786 8463 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8464
8465 uint32_t aval = ( (uint32_t) a->c[0]
8466 | (uint32_t) a->c[1] << 8
8467 | (uint32_t) a->c[2] << 16
8468 | (uint32_t) a->c[3] << 24);
0e287786 8469 return aval;
53df40a4
AM
8470}
8471
0e287786
AM
8472static bfd_vma
8473ext32b_r_offset (const void *p)
53df40a4
AM
8474{
8475 union aligned32
8476 {
8477 uint32_t v;
8478 unsigned char c[4];
8479 };
8480 const union aligned32 *a
0e287786 8481 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8482
8483 uint32_t aval = ( (uint32_t) a->c[0] << 24
8484 | (uint32_t) a->c[1] << 16
8485 | (uint32_t) a->c[2] << 8
8486 | (uint32_t) a->c[3]);
0e287786 8487 return aval;
53df40a4
AM
8488}
8489
8490#ifdef BFD_HOST_64_BIT
0e287786
AM
8491static bfd_vma
8492ext64l_r_offset (const void *p)
53df40a4
AM
8493{
8494 union aligned64
8495 {
8496 uint64_t v;
8497 unsigned char c[8];
8498 };
8499 const union aligned64 *a
0e287786 8500 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8501
8502 uint64_t aval = ( (uint64_t) a->c[0]
8503 | (uint64_t) a->c[1] << 8
8504 | (uint64_t) a->c[2] << 16
8505 | (uint64_t) a->c[3] << 24
8506 | (uint64_t) a->c[4] << 32
8507 | (uint64_t) a->c[5] << 40
8508 | (uint64_t) a->c[6] << 48
8509 | (uint64_t) a->c[7] << 56);
0e287786 8510 return aval;
53df40a4
AM
8511}
8512
0e287786
AM
8513static bfd_vma
8514ext64b_r_offset (const void *p)
53df40a4
AM
8515{
8516 union aligned64
8517 {
8518 uint64_t v;
8519 unsigned char c[8];
8520 };
8521 const union aligned64 *a
0e287786 8522 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8523
8524 uint64_t aval = ( (uint64_t) a->c[0] << 56
8525 | (uint64_t) a->c[1] << 48
8526 | (uint64_t) a->c[2] << 40
8527 | (uint64_t) a->c[3] << 32
8528 | (uint64_t) a->c[4] << 24
8529 | (uint64_t) a->c[5] << 16
8530 | (uint64_t) a->c[6] << 8
8531 | (uint64_t) a->c[7]);
0e287786 8532 return aval;
53df40a4
AM
8533}
8534#endif
8535
c152c796
AM
8536/* When performing a relocatable link, the input relocations are
8537 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8538 referenced must be updated. Update all the relocations found in
8539 RELDATA. */
c152c796 8540
bca6d0e3 8541static bfd_boolean
c152c796 8542elf_link_adjust_relocs (bfd *abfd,
9eaff861 8543 asection *sec,
28dbcedc
AM
8544 struct bfd_elf_section_reloc_data *reldata,
8545 bfd_boolean sort)
c152c796
AM
8546{
8547 unsigned int i;
8548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8549 bfd_byte *erela;
8550 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8551 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8552 bfd_vma r_type_mask;
8553 int r_sym_shift;
d4730f92
BS
8554 unsigned int count = reldata->count;
8555 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8556
d4730f92 8557 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8558 {
8559 swap_in = bed->s->swap_reloc_in;
8560 swap_out = bed->s->swap_reloc_out;
8561 }
d4730f92 8562 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8563 {
8564 swap_in = bed->s->swap_reloca_in;
8565 swap_out = bed->s->swap_reloca_out;
8566 }
8567 else
8568 abort ();
8569
8570 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8571 abort ();
8572
8573 if (bed->s->arch_size == 32)
8574 {
8575 r_type_mask = 0xff;
8576 r_sym_shift = 8;
8577 }
8578 else
8579 {
8580 r_type_mask = 0xffffffff;
8581 r_sym_shift = 32;
8582 }
8583
d4730f92
BS
8584 erela = reldata->hdr->contents;
8585 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8586 {
8587 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8588 unsigned int j;
8589
8590 if (*rel_hash == NULL)
8591 continue;
8592
8593 BFD_ASSERT ((*rel_hash)->indx >= 0);
8594
8595 (*swap_in) (abfd, erela, irela);
8596 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8597 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8598 | (irela[j].r_info & r_type_mask));
8599 (*swap_out) (abfd, irela, erela);
8600 }
53df40a4 8601
9eaff861
AO
8602 if (bed->elf_backend_update_relocs)
8603 (*bed->elf_backend_update_relocs) (sec, reldata);
8604
0e287786 8605 if (sort && count != 0)
53df40a4 8606 {
0e287786
AM
8607 bfd_vma (*ext_r_off) (const void *);
8608 bfd_vma r_off;
8609 size_t elt_size;
8610 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8611 bfd_byte *buf = NULL;
28dbcedc
AM
8612
8613 if (bed->s->arch_size == 32)
8614 {
8615 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8616 ext_r_off = ext32l_r_offset;
28dbcedc 8617 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8618 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8619 else
8620 abort ();
8621 }
53df40a4 8622 else
28dbcedc 8623 {
53df40a4 8624#ifdef BFD_HOST_64_BIT
28dbcedc 8625 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8626 ext_r_off = ext64l_r_offset;
28dbcedc 8627 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8628 ext_r_off = ext64b_r_offset;
28dbcedc 8629 else
53df40a4 8630#endif
28dbcedc
AM
8631 abort ();
8632 }
0e287786 8633
bca6d0e3
AM
8634 /* Must use a stable sort here. A modified insertion sort,
8635 since the relocs are mostly sorted already. */
0e287786
AM
8636 elt_size = reldata->hdr->sh_entsize;
8637 base = reldata->hdr->contents;
8638 end = base + count * elt_size;
bca6d0e3 8639 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8640 abort ();
8641
8642 /* Ensure the first element is lowest. This acts as a sentinel,
8643 speeding the main loop below. */
8644 r_off = (*ext_r_off) (base);
8645 for (p = loc = base; (p += elt_size) < end; )
8646 {
8647 bfd_vma r_off2 = (*ext_r_off) (p);
8648 if (r_off > r_off2)
8649 {
8650 r_off = r_off2;
8651 loc = p;
8652 }
8653 }
8654 if (loc != base)
8655 {
8656 /* Don't just swap *base and *loc as that changes the order
8657 of the original base[0] and base[1] if they happen to
8658 have the same r_offset. */
bca6d0e3
AM
8659 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8660 memcpy (onebuf, loc, elt_size);
0e287786 8661 memmove (base + elt_size, base, loc - base);
bca6d0e3 8662 memcpy (base, onebuf, elt_size);
0e287786
AM
8663 }
8664
b29b8669 8665 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8666 {
8667 /* base to p is sorted, *p is next to insert. */
8668 r_off = (*ext_r_off) (p);
8669 /* Search the sorted region for location to insert. */
8670 loc = p - elt_size;
8671 while (r_off < (*ext_r_off) (loc))
8672 loc -= elt_size;
8673 loc += elt_size;
8674 if (loc != p)
8675 {
bca6d0e3
AM
8676 /* Chances are there is a run of relocs to insert here,
8677 from one of more input files. Files are not always
8678 linked in order due to the way elf_link_input_bfd is
8679 called. See pr17666. */
8680 size_t sortlen = p - loc;
8681 bfd_vma r_off2 = (*ext_r_off) (loc);
8682 size_t runlen = elt_size;
8683 size_t buf_size = 96 * 1024;
8684 while (p + runlen < end
8685 && (sortlen <= buf_size
8686 || runlen + elt_size <= buf_size)
8687 && r_off2 > (*ext_r_off) (p + runlen))
8688 runlen += elt_size;
8689 if (buf == NULL)
8690 {
8691 buf = bfd_malloc (buf_size);
8692 if (buf == NULL)
8693 return FALSE;
8694 }
8695 if (runlen < sortlen)
8696 {
8697 memcpy (buf, p, runlen);
8698 memmove (loc + runlen, loc, sortlen);
8699 memcpy (loc, buf, runlen);
8700 }
8701 else
8702 {
8703 memcpy (buf, loc, sortlen);
8704 memmove (loc, p, runlen);
8705 memcpy (loc + runlen, buf, sortlen);
8706 }
b29b8669 8707 p += runlen - elt_size;
0e287786
AM
8708 }
8709 }
8710 /* Hashes are no longer valid. */
28dbcedc
AM
8711 free (reldata->hashes);
8712 reldata->hashes = NULL;
bca6d0e3 8713 free (buf);
53df40a4 8714 }
bca6d0e3 8715 return TRUE;
c152c796
AM
8716}
8717
8718struct elf_link_sort_rela
8719{
8720 union {
8721 bfd_vma offset;
8722 bfd_vma sym_mask;
8723 } u;
8724 enum elf_reloc_type_class type;
8725 /* We use this as an array of size int_rels_per_ext_rel. */
8726 Elf_Internal_Rela rela[1];
8727};
8728
8729static int
8730elf_link_sort_cmp1 (const void *A, const void *B)
8731{
a50b1753
NC
8732 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8733 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8734 int relativea, relativeb;
8735
8736 relativea = a->type == reloc_class_relative;
8737 relativeb = b->type == reloc_class_relative;
8738
8739 if (relativea < relativeb)
8740 return 1;
8741 if (relativea > relativeb)
8742 return -1;
8743 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8744 return -1;
8745 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8746 return 1;
8747 if (a->rela->r_offset < b->rela->r_offset)
8748 return -1;
8749 if (a->rela->r_offset > b->rela->r_offset)
8750 return 1;
8751 return 0;
8752}
8753
8754static int
8755elf_link_sort_cmp2 (const void *A, const void *B)
8756{
a50b1753
NC
8757 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8758 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8759
7e612e98 8760 if (a->type < b->type)
c152c796 8761 return -1;
7e612e98 8762 if (a->type > b->type)
c152c796 8763 return 1;
7e612e98 8764 if (a->u.offset < b->u.offset)
c152c796 8765 return -1;
7e612e98 8766 if (a->u.offset > b->u.offset)
c152c796
AM
8767 return 1;
8768 if (a->rela->r_offset < b->rela->r_offset)
8769 return -1;
8770 if (a->rela->r_offset > b->rela->r_offset)
8771 return 1;
8772 return 0;
8773}
8774
8775static size_t
8776elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8777{
3410fea8 8778 asection *dynamic_relocs;
fc66a176
L
8779 asection *rela_dyn;
8780 asection *rel_dyn;
c152c796
AM
8781 bfd_size_type count, size;
8782 size_t i, ret, sort_elt, ext_size;
8783 bfd_byte *sort, *s_non_relative, *p;
8784 struct elf_link_sort_rela *sq;
8785 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8786 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8787 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8788 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8789 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8790 struct bfd_link_order *lo;
8791 bfd_vma r_sym_mask;
3410fea8 8792 bfd_boolean use_rela;
c152c796 8793
3410fea8
NC
8794 /* Find a dynamic reloc section. */
8795 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8796 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8797 if (rela_dyn != NULL && rela_dyn->size > 0
8798 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8799 {
3410fea8
NC
8800 bfd_boolean use_rela_initialised = FALSE;
8801
8802 /* This is just here to stop gcc from complaining.
c8e44c6d 8803 Its initialization checking code is not perfect. */
3410fea8
NC
8804 use_rela = TRUE;
8805
8806 /* Both sections are present. Examine the sizes
8807 of the indirect sections to help us choose. */
8808 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8809 if (lo->type == bfd_indirect_link_order)
8810 {
8811 asection *o = lo->u.indirect.section;
8812
8813 if ((o->size % bed->s->sizeof_rela) == 0)
8814 {
8815 if ((o->size % bed->s->sizeof_rel) == 0)
8816 /* Section size is divisible by both rel and rela sizes.
8817 It is of no help to us. */
8818 ;
8819 else
8820 {
8821 /* Section size is only divisible by rela. */
8822 if (use_rela_initialised && (use_rela == FALSE))
8823 {
c8e44c6d
AM
8824 _bfd_error_handler (_("%B: Unable to sort relocs - "
8825 "they are in more than one size"),
8826 abfd);
3410fea8
NC
8827 bfd_set_error (bfd_error_invalid_operation);
8828 return 0;
8829 }
8830 else
8831 {
8832 use_rela = TRUE;
8833 use_rela_initialised = TRUE;
8834 }
8835 }
8836 }
8837 else if ((o->size % bed->s->sizeof_rel) == 0)
8838 {
8839 /* Section size is only divisible by rel. */
8840 if (use_rela_initialised && (use_rela == TRUE))
8841 {
c8e44c6d
AM
8842 _bfd_error_handler (_("%B: Unable to sort relocs - "
8843 "they are in more than one size"),
8844 abfd);
3410fea8
NC
8845 bfd_set_error (bfd_error_invalid_operation);
8846 return 0;
8847 }
8848 else
8849 {
8850 use_rela = FALSE;
8851 use_rela_initialised = TRUE;
8852 }
8853 }
8854 else
8855 {
c8e44c6d
AM
8856 /* The section size is not divisible by either -
8857 something is wrong. */
8858 _bfd_error_handler (_("%B: Unable to sort relocs - "
8859 "they are of an unknown size"), abfd);
3410fea8
NC
8860 bfd_set_error (bfd_error_invalid_operation);
8861 return 0;
8862 }
8863 }
8864
8865 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8866 if (lo->type == bfd_indirect_link_order)
8867 {
8868 asection *o = lo->u.indirect.section;
8869
8870 if ((o->size % bed->s->sizeof_rela) == 0)
8871 {
8872 if ((o->size % bed->s->sizeof_rel) == 0)
8873 /* Section size is divisible by both rel and rela sizes.
8874 It is of no help to us. */
8875 ;
8876 else
8877 {
8878 /* Section size is only divisible by rela. */
8879 if (use_rela_initialised && (use_rela == FALSE))
8880 {
c8e44c6d
AM
8881 _bfd_error_handler (_("%B: Unable to sort relocs - "
8882 "they are in more than one size"),
8883 abfd);
3410fea8
NC
8884 bfd_set_error (bfd_error_invalid_operation);
8885 return 0;
8886 }
8887 else
8888 {
8889 use_rela = TRUE;
8890 use_rela_initialised = TRUE;
8891 }
8892 }
8893 }
8894 else if ((o->size % bed->s->sizeof_rel) == 0)
8895 {
8896 /* Section size is only divisible by rel. */
8897 if (use_rela_initialised && (use_rela == TRUE))
8898 {
c8e44c6d
AM
8899 _bfd_error_handler (_("%B: Unable to sort relocs - "
8900 "they are in more than one size"),
8901 abfd);
3410fea8
NC
8902 bfd_set_error (bfd_error_invalid_operation);
8903 return 0;
8904 }
8905 else
8906 {
8907 use_rela = FALSE;
8908 use_rela_initialised = TRUE;
8909 }
8910 }
8911 else
8912 {
c8e44c6d
AM
8913 /* The section size is not divisible by either -
8914 something is wrong. */
8915 _bfd_error_handler (_("%B: Unable to sort relocs - "
8916 "they are of an unknown size"), abfd);
3410fea8
NC
8917 bfd_set_error (bfd_error_invalid_operation);
8918 return 0;
8919 }
8920 }
8921
8922 if (! use_rela_initialised)
8923 /* Make a guess. */
8924 use_rela = TRUE;
c152c796 8925 }
fc66a176
L
8926 else if (rela_dyn != NULL && rela_dyn->size > 0)
8927 use_rela = TRUE;
8928 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8929 use_rela = FALSE;
c152c796 8930 else
fc66a176 8931 return 0;
3410fea8
NC
8932
8933 if (use_rela)
c152c796 8934 {
3410fea8 8935 dynamic_relocs = rela_dyn;
c152c796
AM
8936 ext_size = bed->s->sizeof_rela;
8937 swap_in = bed->s->swap_reloca_in;
8938 swap_out = bed->s->swap_reloca_out;
8939 }
3410fea8
NC
8940 else
8941 {
8942 dynamic_relocs = rel_dyn;
8943 ext_size = bed->s->sizeof_rel;
8944 swap_in = bed->s->swap_reloc_in;
8945 swap_out = bed->s->swap_reloc_out;
8946 }
c152c796
AM
8947
8948 size = 0;
3410fea8 8949 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8950 if (lo->type == bfd_indirect_link_order)
3410fea8 8951 size += lo->u.indirect.section->size;
c152c796 8952
3410fea8 8953 if (size != dynamic_relocs->size)
c152c796
AM
8954 return 0;
8955
8956 sort_elt = (sizeof (struct elf_link_sort_rela)
8957 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8958
8959 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8960 if (count == 0)
8961 return 0;
a50b1753 8962 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8963
c152c796
AM
8964 if (sort == NULL)
8965 {
8966 (*info->callbacks->warning)
8967 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8968 return 0;
8969 }
8970
8971 if (bed->s->arch_size == 32)
8972 r_sym_mask = ~(bfd_vma) 0xff;
8973 else
8974 r_sym_mask = ~(bfd_vma) 0xffffffff;
8975
3410fea8 8976 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8977 if (lo->type == bfd_indirect_link_order)
8978 {
8979 bfd_byte *erel, *erelend;
8980 asection *o = lo->u.indirect.section;
8981
1da212d6
AM
8982 if (o->contents == NULL && o->size != 0)
8983 {
8984 /* This is a reloc section that is being handled as a normal
8985 section. See bfd_section_from_shdr. We can't combine
8986 relocs in this case. */
8987 free (sort);
8988 return 0;
8989 }
c152c796 8990 erel = o->contents;
eea6121a 8991 erelend = o->contents + o->size;
c8e44c6d 8992 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8993
c152c796
AM
8994 while (erel < erelend)
8995 {
8996 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8997
c152c796 8998 (*swap_in) (abfd, erel, s->rela);
7e612e98 8999 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9000 s->u.sym_mask = r_sym_mask;
9001 p += sort_elt;
9002 erel += ext_size;
9003 }
9004 }
9005
9006 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9007
9008 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9009 {
9010 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9011 if (s->type != reloc_class_relative)
9012 break;
9013 }
9014 ret = i;
9015 s_non_relative = p;
9016
9017 sq = (struct elf_link_sort_rela *) s_non_relative;
9018 for (; i < count; i++, p += sort_elt)
9019 {
9020 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9021 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9022 sq = sp;
9023 sp->u.offset = sq->rela->r_offset;
9024 }
9025
9026 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9027
c8e44c6d
AM
9028 struct elf_link_hash_table *htab = elf_hash_table (info);
9029 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9030 {
9031 /* We have plt relocs in .rela.dyn. */
9032 sq = (struct elf_link_sort_rela *) sort;
9033 for (i = 0; i < count; i++)
9034 if (sq[count - i - 1].type != reloc_class_plt)
9035 break;
9036 if (i != 0 && htab->srelplt->size == i * ext_size)
9037 {
9038 struct bfd_link_order **plo;
9039 /* Put srelplt link_order last. This is so the output_offset
9040 set in the next loop is correct for DT_JMPREL. */
9041 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9042 if ((*plo)->type == bfd_indirect_link_order
9043 && (*plo)->u.indirect.section == htab->srelplt)
9044 {
9045 lo = *plo;
9046 *plo = lo->next;
9047 }
9048 else
9049 plo = &(*plo)->next;
9050 *plo = lo;
9051 lo->next = NULL;
9052 dynamic_relocs->map_tail.link_order = lo;
9053 }
9054 }
9055
9056 p = sort;
3410fea8 9057 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9058 if (lo->type == bfd_indirect_link_order)
9059 {
9060 bfd_byte *erel, *erelend;
9061 asection *o = lo->u.indirect.section;
9062
9063 erel = o->contents;
eea6121a 9064 erelend = o->contents + o->size;
c8e44c6d 9065 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9066 while (erel < erelend)
9067 {
9068 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9069 (*swap_out) (abfd, s->rela, erel);
9070 p += sort_elt;
9071 erel += ext_size;
9072 }
9073 }
9074
9075 free (sort);
3410fea8 9076 *psec = dynamic_relocs;
c152c796
AM
9077 return ret;
9078}
9079
ef10c3ac 9080/* Add a symbol to the output symbol string table. */
c152c796 9081
6e0b88f1 9082static int
ef10c3ac
L
9083elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9084 const char *name,
9085 Elf_Internal_Sym *elfsym,
9086 asection *input_sec,
9087 struct elf_link_hash_entry *h)
c152c796 9088{
6e0b88f1 9089 int (*output_symbol_hook)
c152c796
AM
9090 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9091 struct elf_link_hash_entry *);
ef10c3ac 9092 struct elf_link_hash_table *hash_table;
c152c796 9093 const struct elf_backend_data *bed;
ef10c3ac 9094 bfd_size_type strtabsize;
c152c796 9095
8539e4e8
AM
9096 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9097
8b127cbc 9098 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9099 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9100 if (output_symbol_hook != NULL)
9101 {
8b127cbc 9102 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9103 if (ret != 1)
9104 return ret;
c152c796
AM
9105 }
9106
ef10c3ac
L
9107 if (name == NULL
9108 || *name == '\0'
9109 || (input_sec->flags & SEC_EXCLUDE))
9110 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9111 else
9112 {
ef10c3ac
L
9113 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9114 to get the final offset for st_name. */
9115 elfsym->st_name
9116 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9117 name, FALSE);
c152c796 9118 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9119 return 0;
c152c796
AM
9120 }
9121
ef10c3ac
L
9122 hash_table = elf_hash_table (flinfo->info);
9123 strtabsize = hash_table->strtabsize;
9124 if (strtabsize <= hash_table->strtabcount)
c152c796 9125 {
ef10c3ac
L
9126 strtabsize += strtabsize;
9127 hash_table->strtabsize = strtabsize;
9128 strtabsize *= sizeof (*hash_table->strtab);
9129 hash_table->strtab
9130 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9131 strtabsize);
9132 if (hash_table->strtab == NULL)
6e0b88f1 9133 return 0;
c152c796 9134 }
ef10c3ac
L
9135 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9136 hash_table->strtab[hash_table->strtabcount].dest_index
9137 = hash_table->strtabcount;
9138 hash_table->strtab[hash_table->strtabcount].destshndx_index
9139 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9140
9141 bfd_get_symcount (flinfo->output_bfd) += 1;
9142 hash_table->strtabcount += 1;
9143
9144 return 1;
9145}
9146
9147/* Swap symbols out to the symbol table and flush the output symbols to
9148 the file. */
9149
9150static bfd_boolean
9151elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9152{
9153 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9154 bfd_size_type amt;
9155 size_t i;
ef10c3ac
L
9156 const struct elf_backend_data *bed;
9157 bfd_byte *symbuf;
9158 Elf_Internal_Shdr *hdr;
9159 file_ptr pos;
9160 bfd_boolean ret;
9161
9162 if (!hash_table->strtabcount)
9163 return TRUE;
9164
9165 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9166
9167 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9168
ef10c3ac
L
9169 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9170 symbuf = (bfd_byte *) bfd_malloc (amt);
9171 if (symbuf == NULL)
9172 return FALSE;
1b786873 9173
ef10c3ac 9174 if (flinfo->symshndxbuf)
c152c796 9175 {
ef53be89
AM
9176 amt = sizeof (Elf_External_Sym_Shndx);
9177 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9178 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9179 if (flinfo->symshndxbuf == NULL)
c152c796 9180 {
ef10c3ac
L
9181 free (symbuf);
9182 return FALSE;
c152c796 9183 }
c152c796
AM
9184 }
9185
ef10c3ac
L
9186 for (i = 0; i < hash_table->strtabcount; i++)
9187 {
9188 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9189 if (elfsym->sym.st_name == (unsigned long) -1)
9190 elfsym->sym.st_name = 0;
9191 else
9192 elfsym->sym.st_name
9193 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9194 elfsym->sym.st_name);
9195 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9196 ((bfd_byte *) symbuf
9197 + (elfsym->dest_index
9198 * bed->s->sizeof_sym)),
9199 (flinfo->symshndxbuf
9200 + elfsym->destshndx_index));
9201 }
9202
9203 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9204 pos = hdr->sh_offset + hdr->sh_size;
9205 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9206 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9207 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9208 {
9209 hdr->sh_size += amt;
9210 ret = TRUE;
9211 }
9212 else
9213 ret = FALSE;
c152c796 9214
ef10c3ac
L
9215 free (symbuf);
9216
9217 free (hash_table->strtab);
9218 hash_table->strtab = NULL;
9219
9220 return ret;
c152c796
AM
9221}
9222
c0d5a53d
L
9223/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9224
9225static bfd_boolean
9226check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9227{
4fbb74a6
AM
9228 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9229 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9230 {
9231 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9232 beyond 64k. */
4eca0228 9233 _bfd_error_handler
695344c0 9234 /* xgettext:c-format */
c0d5a53d 9235 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9236 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9237 bfd_set_error (bfd_error_nonrepresentable_section);
9238 return FALSE;
9239 }
9240 return TRUE;
9241}
9242
c152c796
AM
9243/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9244 allowing an unsatisfied unversioned symbol in the DSO to match a
9245 versioned symbol that would normally require an explicit version.
9246 We also handle the case that a DSO references a hidden symbol
9247 which may be satisfied by a versioned symbol in another DSO. */
9248
9249static bfd_boolean
9250elf_link_check_versioned_symbol (struct bfd_link_info *info,
9251 const struct elf_backend_data *bed,
9252 struct elf_link_hash_entry *h)
9253{
9254 bfd *abfd;
9255 struct elf_link_loaded_list *loaded;
9256
9257 if (!is_elf_hash_table (info->hash))
9258 return FALSE;
9259
90c984fc
L
9260 /* Check indirect symbol. */
9261 while (h->root.type == bfd_link_hash_indirect)
9262 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9263
c152c796
AM
9264 switch (h->root.type)
9265 {
9266 default:
9267 abfd = NULL;
9268 break;
9269
9270 case bfd_link_hash_undefined:
9271 case bfd_link_hash_undefweak:
9272 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9273 if (abfd == NULL
9274 || (abfd->flags & DYNAMIC) == 0
e56f61be 9275 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9276 return FALSE;
9277 break;
9278
9279 case bfd_link_hash_defined:
9280 case bfd_link_hash_defweak:
9281 abfd = h->root.u.def.section->owner;
9282 break;
9283
9284 case bfd_link_hash_common:
9285 abfd = h->root.u.c.p->section->owner;
9286 break;
9287 }
9288 BFD_ASSERT (abfd != NULL);
9289
9290 for (loaded = elf_hash_table (info)->loaded;
9291 loaded != NULL;
9292 loaded = loaded->next)
9293 {
9294 bfd *input;
9295 Elf_Internal_Shdr *hdr;
ef53be89
AM
9296 size_t symcount;
9297 size_t extsymcount;
9298 size_t extsymoff;
c152c796
AM
9299 Elf_Internal_Shdr *versymhdr;
9300 Elf_Internal_Sym *isym;
9301 Elf_Internal_Sym *isymend;
9302 Elf_Internal_Sym *isymbuf;
9303 Elf_External_Versym *ever;
9304 Elf_External_Versym *extversym;
9305
9306 input = loaded->abfd;
9307
9308 /* We check each DSO for a possible hidden versioned definition. */
9309 if (input == abfd
9310 || (input->flags & DYNAMIC) == 0
9311 || elf_dynversym (input) == 0)
9312 continue;
9313
9314 hdr = &elf_tdata (input)->dynsymtab_hdr;
9315
9316 symcount = hdr->sh_size / bed->s->sizeof_sym;
9317 if (elf_bad_symtab (input))
9318 {
9319 extsymcount = symcount;
9320 extsymoff = 0;
9321 }
9322 else
9323 {
9324 extsymcount = symcount - hdr->sh_info;
9325 extsymoff = hdr->sh_info;
9326 }
9327
9328 if (extsymcount == 0)
9329 continue;
9330
9331 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9332 NULL, NULL, NULL);
9333 if (isymbuf == NULL)
9334 return FALSE;
9335
9336 /* Read in any version definitions. */
9337 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9338 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9339 if (extversym == NULL)
9340 goto error_ret;
9341
9342 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9343 || (bfd_bread (extversym, versymhdr->sh_size, input)
9344 != versymhdr->sh_size))
9345 {
9346 free (extversym);
9347 error_ret:
9348 free (isymbuf);
9349 return FALSE;
9350 }
9351
9352 ever = extversym + extsymoff;
9353 isymend = isymbuf + extsymcount;
9354 for (isym = isymbuf; isym < isymend; isym++, ever++)
9355 {
9356 const char *name;
9357 Elf_Internal_Versym iver;
9358 unsigned short version_index;
9359
9360 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9361 || isym->st_shndx == SHN_UNDEF)
9362 continue;
9363
9364 name = bfd_elf_string_from_elf_section (input,
9365 hdr->sh_link,
9366 isym->st_name);
9367 if (strcmp (name, h->root.root.string) != 0)
9368 continue;
9369
9370 _bfd_elf_swap_versym_in (input, ever, &iver);
9371
d023c380
L
9372 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9373 && !(h->def_regular
9374 && h->forced_local))
c152c796
AM
9375 {
9376 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9377 have provided a definition for the undefined sym unless
9378 it is defined in a non-shared object and forced local.
9379 */
c152c796
AM
9380 abort ();
9381 }
9382
9383 version_index = iver.vs_vers & VERSYM_VERSION;
9384 if (version_index == 1 || version_index == 2)
9385 {
9386 /* This is the base or first version. We can use it. */
9387 free (extversym);
9388 free (isymbuf);
9389 return TRUE;
9390 }
9391 }
9392
9393 free (extversym);
9394 free (isymbuf);
9395 }
9396
9397 return FALSE;
9398}
9399
b8871f35
L
9400/* Convert ELF common symbol TYPE. */
9401
9402static int
9403elf_link_convert_common_type (struct bfd_link_info *info, int type)
9404{
9405 /* Commom symbol can only appear in relocatable link. */
9406 if (!bfd_link_relocatable (info))
9407 abort ();
9408 switch (info->elf_stt_common)
9409 {
9410 case unchanged:
9411 break;
9412 case elf_stt_common:
9413 type = STT_COMMON;
9414 break;
9415 case no_elf_stt_common:
9416 type = STT_OBJECT;
9417 break;
9418 }
9419 return type;
9420}
9421
c152c796
AM
9422/* Add an external symbol to the symbol table. This is called from
9423 the hash table traversal routine. When generating a shared object,
9424 we go through the symbol table twice. The first time we output
9425 anything that might have been forced to local scope in a version
9426 script. The second time we output the symbols that are still
9427 global symbols. */
9428
9429static bfd_boolean
7686d77d 9430elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9431{
7686d77d 9432 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9433 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9434 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9435 bfd_boolean strip;
9436 Elf_Internal_Sym sym;
9437 asection *input_sec;
9438 const struct elf_backend_data *bed;
6e0b88f1
AM
9439 long indx;
9440 int ret;
b8871f35 9441 unsigned int type;
c152c796
AM
9442
9443 if (h->root.type == bfd_link_hash_warning)
9444 {
9445 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9446 if (h->root.type == bfd_link_hash_new)
9447 return TRUE;
9448 }
9449
9450 /* Decide whether to output this symbol in this pass. */
9451 if (eoinfo->localsyms)
9452 {
4deb8f71 9453 if (!h->forced_local)
c152c796
AM
9454 return TRUE;
9455 }
9456 else
9457 {
4deb8f71 9458 if (h->forced_local)
c152c796
AM
9459 return TRUE;
9460 }
9461
8b127cbc 9462 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9463
12ac1cf5 9464 if (h->root.type == bfd_link_hash_undefined)
c152c796 9465 {
12ac1cf5
NC
9466 /* If we have an undefined symbol reference here then it must have
9467 come from a shared library that is being linked in. (Undefined
98da7939
L
9468 references in regular files have already been handled unless
9469 they are in unreferenced sections which are removed by garbage
9470 collection). */
12ac1cf5
NC
9471 bfd_boolean ignore_undef = FALSE;
9472
9473 /* Some symbols may be special in that the fact that they're
9474 undefined can be safely ignored - let backend determine that. */
9475 if (bed->elf_backend_ignore_undef_symbol)
9476 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9477
9478 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9479 if (!ignore_undef
12ac1cf5 9480 && h->ref_dynamic
8b127cbc
AM
9481 && (!h->ref_regular || flinfo->info->gc_sections)
9482 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9483 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9484 (*flinfo->info->callbacks->undefined_symbol)
9485 (flinfo->info, h->root.root.string,
9486 h->ref_regular ? NULL : h->root.u.undef.abfd,
9487 NULL, 0,
9488 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9489
9490 /* Strip a global symbol defined in a discarded section. */
9491 if (h->indx == -3)
9492 return TRUE;
c152c796
AM
9493 }
9494
9495 /* We should also warn if a forced local symbol is referenced from
9496 shared libraries. */
0e1862bb 9497 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9498 && h->forced_local
9499 && h->ref_dynamic
371a5866 9500 && h->def_regular
f5385ebf 9501 && !h->dynamic_def
ee659f1f 9502 && h->ref_dynamic_nonweak
8b127cbc 9503 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9504 {
17d078c5
AM
9505 bfd *def_bfd;
9506 const char *msg;
90c984fc
L
9507 struct elf_link_hash_entry *hi = h;
9508
9509 /* Check indirect symbol. */
9510 while (hi->root.type == bfd_link_hash_indirect)
9511 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9512
9513 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9514 /* xgettext:c-format */
17d078c5
AM
9515 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9516 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9517 /* xgettext:c-format */
17d078c5
AM
9518 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9519 else
695344c0 9520 /* xgettext:c-format */
17d078c5 9521 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9522 def_bfd = flinfo->output_bfd;
90c984fc
L
9523 if (hi->root.u.def.section != bfd_abs_section_ptr)
9524 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9525 _bfd_error_handler (msg, flinfo->output_bfd,
9526 h->root.root.string, def_bfd);
17d078c5 9527 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9528 eoinfo->failed = TRUE;
9529 return FALSE;
9530 }
9531
9532 /* We don't want to output symbols that have never been mentioned by
9533 a regular file, or that we have been told to strip. However, if
9534 h->indx is set to -2, the symbol is used by a reloc and we must
9535 output it. */
d983c8c5 9536 strip = FALSE;
c152c796 9537 if (h->indx == -2)
d983c8c5 9538 ;
f5385ebf 9539 else if ((h->def_dynamic
77cfaee6
AM
9540 || h->ref_dynamic
9541 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9542 && !h->def_regular
9543 && !h->ref_regular)
c152c796 9544 strip = TRUE;
8b127cbc 9545 else if (flinfo->info->strip == strip_all)
c152c796 9546 strip = TRUE;
8b127cbc
AM
9547 else if (flinfo->info->strip == strip_some
9548 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9549 h->root.root.string, FALSE, FALSE) == NULL)
9550 strip = TRUE;
d56d55e7
AM
9551 else if ((h->root.type == bfd_link_hash_defined
9552 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9553 && ((flinfo->info->strip_discarded
dbaa2011 9554 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9555 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9556 && h->root.u.def.section->owner != NULL
d56d55e7 9557 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9558 strip = TRUE;
9e2278f5
AM
9559 else if ((h->root.type == bfd_link_hash_undefined
9560 || h->root.type == bfd_link_hash_undefweak)
9561 && h->root.u.undef.abfd != NULL
9562 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9563 strip = TRUE;
c152c796 9564
b8871f35
L
9565 type = h->type;
9566
c152c796 9567 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9568 nothing else to do. However, if it is a forced local symbol or
9569 an ifunc symbol we need to give the backend finish_dynamic_symbol
9570 function a chance to make it dynamic. */
c152c796
AM
9571 if (strip
9572 && h->dynindx == -1
b8871f35 9573 && type != STT_GNU_IFUNC
f5385ebf 9574 && !h->forced_local)
c152c796
AM
9575 return TRUE;
9576
9577 sym.st_value = 0;
9578 sym.st_size = h->size;
9579 sym.st_other = h->other;
c152c796
AM
9580 switch (h->root.type)
9581 {
9582 default:
9583 case bfd_link_hash_new:
9584 case bfd_link_hash_warning:
9585 abort ();
9586 return FALSE;
9587
9588 case bfd_link_hash_undefined:
9589 case bfd_link_hash_undefweak:
9590 input_sec = bfd_und_section_ptr;
9591 sym.st_shndx = SHN_UNDEF;
9592 break;
9593
9594 case bfd_link_hash_defined:
9595 case bfd_link_hash_defweak:
9596 {
9597 input_sec = h->root.u.def.section;
9598 if (input_sec->output_section != NULL)
9599 {
9600 sym.st_shndx =
8b127cbc 9601 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9602 input_sec->output_section);
9603 if (sym.st_shndx == SHN_BAD)
9604 {
4eca0228 9605 _bfd_error_handler
695344c0 9606 /* xgettext:c-format */
d003868e 9607 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9608 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9609 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9610 eoinfo->failed = TRUE;
9611 return FALSE;
9612 }
9613
9614 /* ELF symbols in relocatable files are section relative,
9615 but in nonrelocatable files they are virtual
9616 addresses. */
9617 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9618 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9619 {
9620 sym.st_value += input_sec->output_section->vma;
9621 if (h->type == STT_TLS)
9622 {
8b127cbc 9623 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9624 if (tls_sec != NULL)
9625 sym.st_value -= tls_sec->vma;
c152c796
AM
9626 }
9627 }
9628 }
9629 else
9630 {
9631 BFD_ASSERT (input_sec->owner == NULL
9632 || (input_sec->owner->flags & DYNAMIC) != 0);
9633 sym.st_shndx = SHN_UNDEF;
9634 input_sec = bfd_und_section_ptr;
9635 }
9636 }
9637 break;
9638
9639 case bfd_link_hash_common:
9640 input_sec = h->root.u.c.p->section;
a4d8e49b 9641 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9642 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9643 break;
9644
9645 case bfd_link_hash_indirect:
9646 /* These symbols are created by symbol versioning. They point
9647 to the decorated version of the name. For example, if the
9648 symbol foo@@GNU_1.2 is the default, which should be used when
9649 foo is used with no version, then we add an indirect symbol
9650 foo which points to foo@@GNU_1.2. We ignore these symbols,
9651 since the indirected symbol is already in the hash table. */
9652 return TRUE;
9653 }
9654
b8871f35
L
9655 if (type == STT_COMMON || type == STT_OBJECT)
9656 switch (h->root.type)
9657 {
9658 case bfd_link_hash_common:
9659 type = elf_link_convert_common_type (flinfo->info, type);
9660 break;
9661 case bfd_link_hash_defined:
9662 case bfd_link_hash_defweak:
9663 if (bed->common_definition (&sym))
9664 type = elf_link_convert_common_type (flinfo->info, type);
9665 else
9666 type = STT_OBJECT;
9667 break;
9668 case bfd_link_hash_undefined:
9669 case bfd_link_hash_undefweak:
9670 break;
9671 default:
9672 abort ();
9673 }
9674
4deb8f71 9675 if (h->forced_local)
b8871f35
L
9676 {
9677 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9678 /* Turn off visibility on local symbol. */
9679 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9680 }
9681 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9682 else if (h->unique_global && h->def_regular)
9683 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9684 else if (h->root.type == bfd_link_hash_undefweak
9685 || h->root.type == bfd_link_hash_defweak)
9686 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9687 else
9688 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9689 sym.st_target_internal = h->target_internal;
9690
c152c796
AM
9691 /* Give the processor backend a chance to tweak the symbol value,
9692 and also to finish up anything that needs to be done for this
9693 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9694 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9695 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9696 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9697 && h->def_regular
0e1862bb 9698 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9699 || ((h->dynindx != -1
9700 || h->forced_local)
0e1862bb 9701 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9702 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9703 || h->root.type != bfd_link_hash_undefweak))
9704 || !h->forced_local)
8b127cbc 9705 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9706 {
9707 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9708 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9709 {
9710 eoinfo->failed = TRUE;
9711 return FALSE;
9712 }
9713 }
9714
9715 /* If we are marking the symbol as undefined, and there are no
9716 non-weak references to this symbol from a regular object, then
9717 mark the symbol as weak undefined; if there are non-weak
9718 references, mark the symbol as strong. We can't do this earlier,
9719 because it might not be marked as undefined until the
9720 finish_dynamic_symbol routine gets through with it. */
9721 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9722 && h->ref_regular
c152c796
AM
9723 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9724 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9725 {
9726 int bindtype;
b8871f35 9727 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9728
9729 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9730 if (type == STT_GNU_IFUNC)
9731 type = STT_FUNC;
c152c796 9732
f5385ebf 9733 if (h->ref_regular_nonweak)
c152c796
AM
9734 bindtype = STB_GLOBAL;
9735 else
9736 bindtype = STB_WEAK;
2955ec4c 9737 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9738 }
9739
bda987c2
CD
9740 /* If this is a symbol defined in a dynamic library, don't use the
9741 symbol size from the dynamic library. Relinking an executable
9742 against a new library may introduce gratuitous changes in the
9743 executable's symbols if we keep the size. */
9744 if (sym.st_shndx == SHN_UNDEF
9745 && !h->def_regular
9746 && h->def_dynamic)
9747 sym.st_size = 0;
9748
c152c796
AM
9749 /* If a non-weak symbol with non-default visibility is not defined
9750 locally, it is a fatal error. */
0e1862bb 9751 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9752 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9753 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9754 && h->root.type == bfd_link_hash_undefined
f5385ebf 9755 && !h->def_regular)
c152c796 9756 {
17d078c5
AM
9757 const char *msg;
9758
9759 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9760 /* xgettext:c-format */
17d078c5
AM
9761 msg = _("%B: protected symbol `%s' isn't defined");
9762 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9763 /* xgettext:c-format */
17d078c5
AM
9764 msg = _("%B: internal symbol `%s' isn't defined");
9765 else
695344c0 9766 /* xgettext:c-format */
17d078c5 9767 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9768 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9769 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9770 eoinfo->failed = TRUE;
9771 return FALSE;
9772 }
9773
9774 /* If this symbol should be put in the .dynsym section, then put it
9775 there now. We already know the symbol index. We also fill in
9776 the entry in the .hash section. */
cae1fbbb 9777 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9778 && h->dynindx != -1
8b127cbc 9779 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9780 {
c152c796
AM
9781 bfd_byte *esym;
9782
90c984fc
L
9783 /* Since there is no version information in the dynamic string,
9784 if there is no version info in symbol version section, we will
1659f720 9785 have a run-time problem if not linking executable, referenced
4deb8f71 9786 by shared library, or not bound locally. */
1659f720 9787 if (h->verinfo.verdef == NULL
0e1862bb 9788 && (!bfd_link_executable (flinfo->info)
1659f720
L
9789 || h->ref_dynamic
9790 || !h->def_regular))
90c984fc
L
9791 {
9792 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9793
9794 if (p && p [1] != '\0')
9795 {
4eca0228 9796 _bfd_error_handler
695344c0 9797 /* xgettext:c-format */
90c984fc
L
9798 (_("%B: No symbol version section for versioned symbol `%s'"),
9799 flinfo->output_bfd, h->root.root.string);
9800 eoinfo->failed = TRUE;
9801 return FALSE;
9802 }
9803 }
9804
c152c796 9805 sym.st_name = h->dynstr_index;
cae1fbbb
L
9806 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9807 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9808 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9809 {
9810 eoinfo->failed = TRUE;
9811 return FALSE;
9812 }
8b127cbc 9813 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9814
8b127cbc 9815 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9816 {
9817 size_t hash_entry_size;
9818 bfd_byte *bucketpos;
9819 bfd_vma chain;
41198d0c
L
9820 size_t bucketcount;
9821 size_t bucket;
9822
8b127cbc 9823 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9824 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9825
9826 hash_entry_size
8b127cbc
AM
9827 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9828 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9829 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9830 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9831 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9832 bucketpos);
9833 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9834 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9835 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9836 }
c152c796 9837
8b127cbc 9838 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9839 {
9840 Elf_Internal_Versym iversym;
9841 Elf_External_Versym *eversym;
9842
f5385ebf 9843 if (!h->def_regular)
c152c796 9844 {
7b20f099
AM
9845 if (h->verinfo.verdef == NULL
9846 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9847 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9848 iversym.vs_vers = 0;
9849 else
9850 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9851 }
9852 else
9853 {
9854 if (h->verinfo.vertree == NULL)
9855 iversym.vs_vers = 1;
9856 else
9857 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9858 if (flinfo->info->create_default_symver)
3e3b46e5 9859 iversym.vs_vers++;
c152c796
AM
9860 }
9861
422f1182 9862 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9863 defined locally. */
422f1182 9864 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9865 iversym.vs_vers |= VERSYM_HIDDEN;
9866
8b127cbc 9867 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9868 eversym += h->dynindx;
8b127cbc 9869 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9870 }
9871 }
9872
d983c8c5
AM
9873 /* If the symbol is undefined, and we didn't output it to .dynsym,
9874 strip it from .symtab too. Obviously we can't do this for
9875 relocatable output or when needed for --emit-relocs. */
9876 else if (input_sec == bfd_und_section_ptr
9877 && h->indx != -2
0e1862bb 9878 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9879 return TRUE;
9880 /* Also strip others that we couldn't earlier due to dynamic symbol
9881 processing. */
9882 if (strip)
9883 return TRUE;
9884 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9885 return TRUE;
9886
2ec55de3
AM
9887 /* Output a FILE symbol so that following locals are not associated
9888 with the wrong input file. We need one for forced local symbols
9889 if we've seen more than one FILE symbol or when we have exactly
9890 one FILE symbol but global symbols are present in a file other
9891 than the one with the FILE symbol. We also need one if linker
9892 defined symbols are present. In practice these conditions are
9893 always met, so just emit the FILE symbol unconditionally. */
9894 if (eoinfo->localsyms
9895 && !eoinfo->file_sym_done
9896 && eoinfo->flinfo->filesym_count != 0)
9897 {
9898 Elf_Internal_Sym fsym;
9899
9900 memset (&fsym, 0, sizeof (fsym));
9901 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9902 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9903 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9904 bfd_und_section_ptr, NULL))
2ec55de3
AM
9905 return FALSE;
9906
9907 eoinfo->file_sym_done = TRUE;
9908 }
9909
8b127cbc 9910 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9911 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9912 input_sec, h);
6e0b88f1 9913 if (ret == 0)
c152c796
AM
9914 {
9915 eoinfo->failed = TRUE;
9916 return FALSE;
9917 }
6e0b88f1
AM
9918 else if (ret == 1)
9919 h->indx = indx;
9920 else if (h->indx == -2)
9921 abort();
c152c796
AM
9922
9923 return TRUE;
9924}
9925
cdd3575c
AM
9926/* Return TRUE if special handling is done for relocs in SEC against
9927 symbols defined in discarded sections. */
9928
c152c796
AM
9929static bfd_boolean
9930elf_section_ignore_discarded_relocs (asection *sec)
9931{
9932 const struct elf_backend_data *bed;
9933
cdd3575c
AM
9934 switch (sec->sec_info_type)
9935 {
dbaa2011
AM
9936 case SEC_INFO_TYPE_STABS:
9937 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9938 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9939 return TRUE;
9940 default:
9941 break;
9942 }
c152c796
AM
9943
9944 bed = get_elf_backend_data (sec->owner);
9945 if (bed->elf_backend_ignore_discarded_relocs != NULL
9946 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9947 return TRUE;
9948
9949 return FALSE;
9950}
9951
9e66c942
AM
9952/* Return a mask saying how ld should treat relocations in SEC against
9953 symbols defined in discarded sections. If this function returns
9954 COMPLAIN set, ld will issue a warning message. If this function
9955 returns PRETEND set, and the discarded section was link-once and the
9956 same size as the kept link-once section, ld will pretend that the
9957 symbol was actually defined in the kept section. Otherwise ld will
9958 zero the reloc (at least that is the intent, but some cooperation by
9959 the target dependent code is needed, particularly for REL targets). */
9960
8a696751
AM
9961unsigned int
9962_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9963{
9e66c942 9964 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9965 return PRETEND;
cdd3575c
AM
9966
9967 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9968 return 0;
cdd3575c
AM
9969
9970 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9971 return 0;
cdd3575c 9972
9e66c942 9973 return COMPLAIN | PRETEND;
cdd3575c
AM
9974}
9975
3d7f7666
L
9976/* Find a match between a section and a member of a section group. */
9977
9978static asection *
c0f00686
L
9979match_group_member (asection *sec, asection *group,
9980 struct bfd_link_info *info)
3d7f7666
L
9981{
9982 asection *first = elf_next_in_group (group);
9983 asection *s = first;
9984
9985 while (s != NULL)
9986 {
c0f00686 9987 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9988 return s;
9989
83180ade 9990 s = elf_next_in_group (s);
3d7f7666
L
9991 if (s == first)
9992 break;
9993 }
9994
9995 return NULL;
9996}
9997
01b3c8ab 9998/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9999 to replace it. Return the replacement if it is OK. Otherwise return
10000 NULL. */
01b3c8ab
L
10001
10002asection *
c0f00686 10003_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10004{
10005 asection *kept;
10006
10007 kept = sec->kept_section;
10008 if (kept != NULL)
10009 {
c2370991 10010 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10011 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10012 if (kept != NULL
10013 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10014 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10015 kept = NULL;
c2370991 10016 sec->kept_section = kept;
01b3c8ab
L
10017 }
10018 return kept;
10019}
10020
c152c796
AM
10021/* Link an input file into the linker output file. This function
10022 handles all the sections and relocations of the input file at once.
10023 This is so that we only have to read the local symbols once, and
10024 don't have to keep them in memory. */
10025
10026static bfd_boolean
8b127cbc 10027elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10028{
ece5ef60 10029 int (*relocate_section)
c152c796
AM
10030 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10031 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10032 bfd *output_bfd;
10033 Elf_Internal_Shdr *symtab_hdr;
10034 size_t locsymcount;
10035 size_t extsymoff;
10036 Elf_Internal_Sym *isymbuf;
10037 Elf_Internal_Sym *isym;
10038 Elf_Internal_Sym *isymend;
10039 long *pindex;
10040 asection **ppsection;
10041 asection *o;
10042 const struct elf_backend_data *bed;
c152c796 10043 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10044 bfd_size_type address_size;
10045 bfd_vma r_type_mask;
10046 int r_sym_shift;
ffbc01cc 10047 bfd_boolean have_file_sym = FALSE;
c152c796 10048
8b127cbc 10049 output_bfd = flinfo->output_bfd;
c152c796
AM
10050 bed = get_elf_backend_data (output_bfd);
10051 relocate_section = bed->elf_backend_relocate_section;
10052
10053 /* If this is a dynamic object, we don't want to do anything here:
10054 we don't want the local symbols, and we don't want the section
10055 contents. */
10056 if ((input_bfd->flags & DYNAMIC) != 0)
10057 return TRUE;
10058
c152c796
AM
10059 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10060 if (elf_bad_symtab (input_bfd))
10061 {
10062 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10063 extsymoff = 0;
10064 }
10065 else
10066 {
10067 locsymcount = symtab_hdr->sh_info;
10068 extsymoff = symtab_hdr->sh_info;
10069 }
10070
10071 /* Read the local symbols. */
10072 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10073 if (isymbuf == NULL && locsymcount != 0)
10074 {
10075 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10076 flinfo->internal_syms,
10077 flinfo->external_syms,
10078 flinfo->locsym_shndx);
c152c796
AM
10079 if (isymbuf == NULL)
10080 return FALSE;
10081 }
10082
10083 /* Find local symbol sections and adjust values of symbols in
10084 SEC_MERGE sections. Write out those local symbols we know are
10085 going into the output file. */
10086 isymend = isymbuf + locsymcount;
8b127cbc 10087 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10088 isym < isymend;
10089 isym++, pindex++, ppsection++)
10090 {
10091 asection *isec;
10092 const char *name;
10093 Elf_Internal_Sym osym;
6e0b88f1
AM
10094 long indx;
10095 int ret;
c152c796
AM
10096
10097 *pindex = -1;
10098
10099 if (elf_bad_symtab (input_bfd))
10100 {
10101 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10102 {
10103 *ppsection = NULL;
10104 continue;
10105 }
10106 }
10107
10108 if (isym->st_shndx == SHN_UNDEF)
10109 isec = bfd_und_section_ptr;
c152c796
AM
10110 else if (isym->st_shndx == SHN_ABS)
10111 isec = bfd_abs_section_ptr;
10112 else if (isym->st_shndx == SHN_COMMON)
10113 isec = bfd_com_section_ptr;
10114 else
10115 {
cb33740c
AM
10116 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10117 if (isec == NULL)
10118 {
10119 /* Don't attempt to output symbols with st_shnx in the
10120 reserved range other than SHN_ABS and SHN_COMMON. */
10121 *ppsection = NULL;
10122 continue;
10123 }
dbaa2011 10124 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10125 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10126 isym->st_value =
10127 _bfd_merged_section_offset (output_bfd, &isec,
10128 elf_section_data (isec)->sec_info,
10129 isym->st_value);
c152c796
AM
10130 }
10131
10132 *ppsection = isec;
10133
d983c8c5
AM
10134 /* Don't output the first, undefined, symbol. In fact, don't
10135 output any undefined local symbol. */
10136 if (isec == bfd_und_section_ptr)
c152c796
AM
10137 continue;
10138
10139 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10140 {
10141 /* We never output section symbols. Instead, we use the
10142 section symbol of the corresponding section in the output
10143 file. */
10144 continue;
10145 }
10146
10147 /* If we are stripping all symbols, we don't want to output this
10148 one. */
8b127cbc 10149 if (flinfo->info->strip == strip_all)
c152c796
AM
10150 continue;
10151
10152 /* If we are discarding all local symbols, we don't want to
10153 output this one. If we are generating a relocatable output
10154 file, then some of the local symbols may be required by
10155 relocs; we output them below as we discover that they are
10156 needed. */
8b127cbc 10157 if (flinfo->info->discard == discard_all)
c152c796
AM
10158 continue;
10159
10160 /* If this symbol is defined in a section which we are
f02571c5
AM
10161 discarding, we don't need to keep it. */
10162 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10163 && isym->st_shndx < SHN_LORESERVE
10164 && bfd_section_removed_from_list (output_bfd,
10165 isec->output_section))
e75a280b
L
10166 continue;
10167
c152c796
AM
10168 /* Get the name of the symbol. */
10169 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10170 isym->st_name);
10171 if (name == NULL)
10172 return FALSE;
10173
10174 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10175 if ((flinfo->info->strip == strip_some
10176 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10177 == NULL))
8b127cbc 10178 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10179 && (isec->flags & SEC_MERGE)
10180 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10181 || flinfo->info->discard == discard_l)
c152c796
AM
10182 && bfd_is_local_label_name (input_bfd, name)))
10183 continue;
10184
ffbc01cc
AM
10185 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10186 {
ce875075
AM
10187 if (input_bfd->lto_output)
10188 /* -flto puts a temp file name here. This means builds
10189 are not reproducible. Discard the symbol. */
10190 continue;
ffbc01cc
AM
10191 have_file_sym = TRUE;
10192 flinfo->filesym_count += 1;
10193 }
10194 if (!have_file_sym)
10195 {
10196 /* In the absence of debug info, bfd_find_nearest_line uses
10197 FILE symbols to determine the source file for local
10198 function symbols. Provide a FILE symbol here if input
10199 files lack such, so that their symbols won't be
10200 associated with a previous input file. It's not the
10201 source file, but the best we can do. */
10202 have_file_sym = TRUE;
10203 flinfo->filesym_count += 1;
10204 memset (&osym, 0, sizeof (osym));
10205 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10206 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10207 if (!elf_link_output_symstrtab (flinfo,
10208 (input_bfd->lto_output ? NULL
10209 : input_bfd->filename),
10210 &osym, bfd_abs_section_ptr,
10211 NULL))
ffbc01cc
AM
10212 return FALSE;
10213 }
10214
c152c796
AM
10215 osym = *isym;
10216
10217 /* Adjust the section index for the output file. */
10218 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10219 isec->output_section);
10220 if (osym.st_shndx == SHN_BAD)
10221 return FALSE;
10222
c152c796
AM
10223 /* ELF symbols in relocatable files are section relative, but
10224 in executable files they are virtual addresses. Note that
10225 this code assumes that all ELF sections have an associated
10226 BFD section with a reasonable value for output_offset; below
10227 we assume that they also have a reasonable value for
10228 output_section. Any special sections must be set up to meet
10229 these requirements. */
10230 osym.st_value += isec->output_offset;
0e1862bb 10231 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10232 {
10233 osym.st_value += isec->output_section->vma;
10234 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10235 {
10236 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10237 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10238 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10239 }
10240 }
10241
6e0b88f1 10242 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10243 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10244 if (ret == 0)
c152c796 10245 return FALSE;
6e0b88f1
AM
10246 else if (ret == 1)
10247 *pindex = indx;
c152c796
AM
10248 }
10249
310fd250
L
10250 if (bed->s->arch_size == 32)
10251 {
10252 r_type_mask = 0xff;
10253 r_sym_shift = 8;
10254 address_size = 4;
10255 }
10256 else
10257 {
10258 r_type_mask = 0xffffffff;
10259 r_sym_shift = 32;
10260 address_size = 8;
10261 }
10262
c152c796
AM
10263 /* Relocate the contents of each section. */
10264 sym_hashes = elf_sym_hashes (input_bfd);
10265 for (o = input_bfd->sections; o != NULL; o = o->next)
10266 {
10267 bfd_byte *contents;
10268
10269 if (! o->linker_mark)
10270 {
10271 /* This section was omitted from the link. */
10272 continue;
10273 }
10274
0e1862bb 10275 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10276 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10277 {
10278 /* Deal with the group signature symbol. */
10279 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10280 unsigned long symndx = sec_data->this_hdr.sh_info;
10281 asection *osec = o->output_section;
10282
10283 if (symndx >= locsymcount
10284 || (elf_bad_symtab (input_bfd)
8b127cbc 10285 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10286 {
10287 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10288 while (h->root.type == bfd_link_hash_indirect
10289 || h->root.type == bfd_link_hash_warning)
10290 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10291 /* Arrange for symbol to be output. */
10292 h->indx = -2;
10293 elf_section_data (osec)->this_hdr.sh_info = -2;
10294 }
10295 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10296 {
10297 /* We'll use the output section target_index. */
8b127cbc 10298 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10299 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10300 }
10301 else
10302 {
8b127cbc 10303 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10304 {
10305 /* Otherwise output the local symbol now. */
10306 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10307 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10308 const char *name;
6e0b88f1
AM
10309 long indx;
10310 int ret;
bcacc0f5
AM
10311
10312 name = bfd_elf_string_from_elf_section (input_bfd,
10313 symtab_hdr->sh_link,
10314 sym.st_name);
10315 if (name == NULL)
10316 return FALSE;
10317
10318 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10319 sec);
10320 if (sym.st_shndx == SHN_BAD)
10321 return FALSE;
10322
10323 sym.st_value += o->output_offset;
10324
6e0b88f1 10325 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10326 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10327 NULL);
6e0b88f1 10328 if (ret == 0)
bcacc0f5 10329 return FALSE;
6e0b88f1 10330 else if (ret == 1)
8b127cbc 10331 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10332 else
10333 abort ();
bcacc0f5
AM
10334 }
10335 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10336 = flinfo->indices[symndx];
bcacc0f5
AM
10337 }
10338 }
10339
c152c796 10340 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10341 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10342 continue;
10343
10344 if ((o->flags & SEC_LINKER_CREATED) != 0)
10345 {
10346 /* Section was created by _bfd_elf_link_create_dynamic_sections
10347 or somesuch. */
10348 continue;
10349 }
10350
10351 /* Get the contents of the section. They have been cached by a
10352 relaxation routine. Note that o is a section in an input
10353 file, so the contents field will not have been set by any of
10354 the routines which work on output files. */
10355 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10356 {
10357 contents = elf_section_data (o)->this_hdr.contents;
10358 if (bed->caches_rawsize
10359 && o->rawsize != 0
10360 && o->rawsize < o->size)
10361 {
10362 memcpy (flinfo->contents, contents, o->rawsize);
10363 contents = flinfo->contents;
10364 }
10365 }
c152c796
AM
10366 else
10367 {
8b127cbc 10368 contents = flinfo->contents;
4a114e3e 10369 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10370 return FALSE;
10371 }
10372
10373 if ((o->flags & SEC_RELOC) != 0)
10374 {
10375 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10376 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10377 int action_discarded;
ece5ef60 10378 int ret;
c152c796
AM
10379
10380 /* Get the swapped relocs. */
10381 internal_relocs
8b127cbc
AM
10382 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10383 flinfo->internal_relocs, FALSE);
c152c796
AM
10384 if (internal_relocs == NULL
10385 && o->reloc_count > 0)
10386 return FALSE;
10387
310fd250
L
10388 /* We need to reverse-copy input .ctors/.dtors sections if
10389 they are placed in .init_array/.finit_array for output. */
10390 if (o->size > address_size
10391 && ((strncmp (o->name, ".ctors", 6) == 0
10392 && strcmp (o->output_section->name,
10393 ".init_array") == 0)
10394 || (strncmp (o->name, ".dtors", 6) == 0
10395 && strcmp (o->output_section->name,
10396 ".fini_array") == 0))
10397 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10398 {
310fd250
L
10399 if (o->size != o->reloc_count * address_size)
10400 {
4eca0228 10401 _bfd_error_handler
695344c0 10402 /* xgettext:c-format */
310fd250
L
10403 (_("error: %B: size of section %A is not "
10404 "multiple of address size"),
10405 input_bfd, o);
10406 bfd_set_error (bfd_error_on_input);
10407 return FALSE;
10408 }
10409 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10410 }
10411
0f02bbd9 10412 action_discarded = -1;
c152c796 10413 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10414 action_discarded = (*bed->action_discarded) (o);
10415
10416 /* Run through the relocs evaluating complex reloc symbols and
10417 looking for relocs against symbols from discarded sections
10418 or section symbols from removed link-once sections.
10419 Complain about relocs against discarded sections. Zero
10420 relocs against removed link-once sections. */
10421
10422 rel = internal_relocs;
10423 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10424 for ( ; rel < relend; rel++)
c152c796 10425 {
0f02bbd9
AM
10426 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10427 unsigned int s_type;
10428 asection **ps, *sec;
10429 struct elf_link_hash_entry *h = NULL;
10430 const char *sym_name;
c152c796 10431
0f02bbd9
AM
10432 if (r_symndx == STN_UNDEF)
10433 continue;
c152c796 10434
0f02bbd9
AM
10435 if (r_symndx >= locsymcount
10436 || (elf_bad_symtab (input_bfd)
8b127cbc 10437 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10438 {
10439 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10440
0f02bbd9
AM
10441 /* Badly formatted input files can contain relocs that
10442 reference non-existant symbols. Check here so that
10443 we do not seg fault. */
10444 if (h == NULL)
c152c796 10445 {
0f02bbd9 10446 char buffer [32];
dce669a1 10447
0f02bbd9 10448 sprintf_vma (buffer, rel->r_info);
4eca0228 10449 _bfd_error_handler
695344c0 10450 /* xgettext:c-format */
0f02bbd9
AM
10451 (_("error: %B contains a reloc (0x%s) for section %A "
10452 "that references a non-existent global symbol"),
c08bb8dd 10453 input_bfd, buffer, o);
0f02bbd9
AM
10454 bfd_set_error (bfd_error_bad_value);
10455 return FALSE;
10456 }
3b36f7e6 10457
0f02bbd9
AM
10458 while (h->root.type == bfd_link_hash_indirect
10459 || h->root.type == bfd_link_hash_warning)
10460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10461
0f02bbd9 10462 s_type = h->type;
cdd3575c 10463
9e2dec47 10464 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10465 mark the symbol as undefined. Note that the
10466 linker may attach linker created dynamic sections
10467 to the plugin bfd. Symbols defined in linker
10468 created sections are not plugin symbols. */
9e2dec47
L
10469 if (h->root.non_ir_ref
10470 && (h->root.type == bfd_link_hash_defined
10471 || h->root.type == bfd_link_hash_defweak)
10472 && (h->root.u.def.section->flags
10473 & SEC_LINKER_CREATED) == 0
10474 && h->root.u.def.section->owner != NULL
10475 && (h->root.u.def.section->owner->flags
10476 & BFD_PLUGIN) != 0)
10477 {
10478 h->root.type = bfd_link_hash_undefined;
10479 h->root.u.undef.abfd = h->root.u.def.section->owner;
10480 }
10481
0f02bbd9
AM
10482 ps = NULL;
10483 if (h->root.type == bfd_link_hash_defined
10484 || h->root.type == bfd_link_hash_defweak)
10485 ps = &h->root.u.def.section;
10486
10487 sym_name = h->root.root.string;
10488 }
10489 else
10490 {
10491 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10492
10493 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10494 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10495 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10496 sym, *ps);
10497 }
c152c796 10498
c301e700 10499 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10500 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10501 {
10502 bfd_vma val;
10503 bfd_vma dot = (rel->r_offset
10504 + o->output_offset + o->output_section->vma);
10505#ifdef DEBUG
10506 printf ("Encountered a complex symbol!");
10507 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10508 input_bfd->filename, o->name,
10509 (long) (rel - internal_relocs));
0f02bbd9
AM
10510 printf (" symbol: idx %8.8lx, name %s\n",
10511 r_symndx, sym_name);
10512 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10513 (unsigned long) rel->r_info,
10514 (unsigned long) rel->r_offset);
10515#endif
8b127cbc 10516 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10517 isymbuf, locsymcount, s_type == STT_SRELC))
10518 return FALSE;
10519
10520 /* Symbol evaluated OK. Update to absolute value. */
10521 set_symbol_value (input_bfd, isymbuf, locsymcount,
10522 r_symndx, val);
10523 continue;
10524 }
10525
10526 if (action_discarded != -1 && ps != NULL)
10527 {
cdd3575c
AM
10528 /* Complain if the definition comes from a
10529 discarded section. */
dbaa2011 10530 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10531 {
cf35638d 10532 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10533 if (action_discarded & COMPLAIN)
8b127cbc 10534 (*flinfo->info->callbacks->einfo)
695344c0 10535 /* xgettext:c-format */
e1fffbe6 10536 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10537 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10538 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10539
87e5235d 10540 /* Try to do the best we can to support buggy old
e0ae6d6f 10541 versions of gcc. Pretend that the symbol is
87e5235d
AM
10542 really defined in the kept linkonce section.
10543 FIXME: This is quite broken. Modifying the
10544 symbol here means we will be changing all later
e0ae6d6f 10545 uses of the symbol, not just in this section. */
0f02bbd9 10546 if (action_discarded & PRETEND)
87e5235d 10547 {
01b3c8ab
L
10548 asection *kept;
10549
c0f00686 10550 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10551 flinfo->info);
01b3c8ab 10552 if (kept != NULL)
87e5235d
AM
10553 {
10554 *ps = kept;
10555 continue;
10556 }
10557 }
c152c796
AM
10558 }
10559 }
10560 }
10561
10562 /* Relocate the section by invoking a back end routine.
10563
10564 The back end routine is responsible for adjusting the
10565 section contents as necessary, and (if using Rela relocs
10566 and generating a relocatable output file) adjusting the
10567 reloc addend as necessary.
10568
10569 The back end routine does not have to worry about setting
10570 the reloc address or the reloc symbol index.
10571
10572 The back end routine is given a pointer to the swapped in
10573 internal symbols, and can access the hash table entries
10574 for the external symbols via elf_sym_hashes (input_bfd).
10575
10576 When generating relocatable output, the back end routine
10577 must handle STB_LOCAL/STT_SECTION symbols specially. The
10578 output symbol is going to be a section symbol
10579 corresponding to the output section, which will require
10580 the addend to be adjusted. */
10581
8b127cbc 10582 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10583 input_bfd, o, contents,
10584 internal_relocs,
10585 isymbuf,
8b127cbc 10586 flinfo->sections);
ece5ef60 10587 if (!ret)
c152c796
AM
10588 return FALSE;
10589
ece5ef60 10590 if (ret == 2
0e1862bb 10591 || bfd_link_relocatable (flinfo->info)
8b127cbc 10592 || flinfo->info->emitrelocations)
c152c796
AM
10593 {
10594 Elf_Internal_Rela *irela;
d4730f92 10595 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10596 bfd_vma last_offset;
10597 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10598 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10599 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10600 unsigned int next_erel;
c152c796 10601 bfd_boolean rela_normal;
d4730f92 10602 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10603
d4730f92
BS
10604 esdi = elf_section_data (o);
10605 esdo = elf_section_data (o->output_section);
10606 rela_normal = FALSE;
c152c796
AM
10607
10608 /* Adjust the reloc addresses and symbol indices. */
10609
10610 irela = internal_relocs;
10611 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10612 rel_hash = esdo->rel.hashes + esdo->rel.count;
10613 /* We start processing the REL relocs, if any. When we reach
10614 IRELAMID in the loop, we switch to the RELA relocs. */
10615 irelamid = irela;
10616 if (esdi->rel.hdr != NULL)
10617 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10618 * bed->s->int_rels_per_ext_rel);
eac338cf 10619 rel_hash_list = rel_hash;
d4730f92 10620 rela_hash_list = NULL;
c152c796 10621 last_offset = o->output_offset;
0e1862bb 10622 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10623 last_offset += o->output_section->vma;
10624 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10625 {
10626 unsigned long r_symndx;
10627 asection *sec;
10628 Elf_Internal_Sym sym;
10629
10630 if (next_erel == bed->s->int_rels_per_ext_rel)
10631 {
10632 rel_hash++;
10633 next_erel = 0;
10634 }
10635
d4730f92
BS
10636 if (irela == irelamid)
10637 {
10638 rel_hash = esdo->rela.hashes + esdo->rela.count;
10639 rela_hash_list = rel_hash;
10640 rela_normal = bed->rela_normal;
10641 }
10642
c152c796 10643 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10644 flinfo->info, o,
c152c796
AM
10645 irela->r_offset);
10646 if (irela->r_offset >= (bfd_vma) -2)
10647 {
10648 /* This is a reloc for a deleted entry or somesuch.
10649 Turn it into an R_*_NONE reloc, at the same
10650 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10651 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10652 being ordered. */
10653 irela->r_offset = last_offset;
10654 irela->r_info = 0;
10655 irela->r_addend = 0;
10656 continue;
10657 }
10658
10659 irela->r_offset += o->output_offset;
10660
10661 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10662 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10663 irela->r_offset += o->output_section->vma;
10664
10665 last_offset = irela->r_offset;
10666
10667 r_symndx = irela->r_info >> r_sym_shift;
10668 if (r_symndx == STN_UNDEF)
10669 continue;
10670
10671 if (r_symndx >= locsymcount
10672 || (elf_bad_symtab (input_bfd)
8b127cbc 10673 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10674 {
10675 struct elf_link_hash_entry *rh;
10676 unsigned long indx;
10677
10678 /* This is a reloc against a global symbol. We
10679 have not yet output all the local symbols, so
10680 we do not know the symbol index of any global
10681 symbol. We set the rel_hash entry for this
10682 reloc to point to the global hash table entry
10683 for this symbol. The symbol index is then
ee75fd95 10684 set at the end of bfd_elf_final_link. */
c152c796
AM
10685 indx = r_symndx - extsymoff;
10686 rh = elf_sym_hashes (input_bfd)[indx];
10687 while (rh->root.type == bfd_link_hash_indirect
10688 || rh->root.type == bfd_link_hash_warning)
10689 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10690
10691 /* Setting the index to -2 tells
10692 elf_link_output_extsym that this symbol is
10693 used by a reloc. */
10694 BFD_ASSERT (rh->indx < 0);
10695 rh->indx = -2;
10696
10697 *rel_hash = rh;
10698
10699 continue;
10700 }
10701
10702 /* This is a reloc against a local symbol. */
10703
10704 *rel_hash = NULL;
10705 sym = isymbuf[r_symndx];
8b127cbc 10706 sec = flinfo->sections[r_symndx];
c152c796
AM
10707 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10708 {
10709 /* I suppose the backend ought to fill in the
10710 section of any STT_SECTION symbol against a
6a8d1586 10711 processor specific section. */
cf35638d 10712 r_symndx = STN_UNDEF;
6a8d1586
AM
10713 if (bfd_is_abs_section (sec))
10714 ;
c152c796
AM
10715 else if (sec == NULL || sec->owner == NULL)
10716 {
10717 bfd_set_error (bfd_error_bad_value);
10718 return FALSE;
10719 }
10720 else
10721 {
6a8d1586
AM
10722 asection *osec = sec->output_section;
10723
10724 /* If we have discarded a section, the output
10725 section will be the absolute section. In
ab96bf03
AM
10726 case of discarded SEC_MERGE sections, use
10727 the kept section. relocate_section should
10728 have already handled discarded linkonce
10729 sections. */
6a8d1586
AM
10730 if (bfd_is_abs_section (osec)
10731 && sec->kept_section != NULL
10732 && sec->kept_section->output_section != NULL)
10733 {
10734 osec = sec->kept_section->output_section;
10735 irela->r_addend -= osec->vma;
10736 }
10737
10738 if (!bfd_is_abs_section (osec))
10739 {
10740 r_symndx = osec->target_index;
cf35638d 10741 if (r_symndx == STN_UNDEF)
74541ad4 10742 {
051d833a
AM
10743 irela->r_addend += osec->vma;
10744 osec = _bfd_nearby_section (output_bfd, osec,
10745 osec->vma);
10746 irela->r_addend -= osec->vma;
10747 r_symndx = osec->target_index;
74541ad4 10748 }
6a8d1586 10749 }
c152c796
AM
10750 }
10751
10752 /* Adjust the addend according to where the
10753 section winds up in the output section. */
10754 if (rela_normal)
10755 irela->r_addend += sec->output_offset;
10756 }
10757 else
10758 {
8b127cbc 10759 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10760 {
10761 unsigned long shlink;
10762 const char *name;
10763 asection *osec;
6e0b88f1 10764 long indx;
c152c796 10765
8b127cbc 10766 if (flinfo->info->strip == strip_all)
c152c796
AM
10767 {
10768 /* You can't do ld -r -s. */
10769 bfd_set_error (bfd_error_invalid_operation);
10770 return FALSE;
10771 }
10772
10773 /* This symbol was skipped earlier, but
10774 since it is needed by a reloc, we
10775 must output it now. */
10776 shlink = symtab_hdr->sh_link;
10777 name = (bfd_elf_string_from_elf_section
10778 (input_bfd, shlink, sym.st_name));
10779 if (name == NULL)
10780 return FALSE;
10781
10782 osec = sec->output_section;
10783 sym.st_shndx =
10784 _bfd_elf_section_from_bfd_section (output_bfd,
10785 osec);
10786 if (sym.st_shndx == SHN_BAD)
10787 return FALSE;
10788
10789 sym.st_value += sec->output_offset;
0e1862bb 10790 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10791 {
10792 sym.st_value += osec->vma;
10793 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10794 {
10795 /* STT_TLS symbols are relative to PT_TLS
10796 segment base. */
8b127cbc 10797 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10798 ->tls_sec != NULL);
8b127cbc 10799 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10800 ->tls_sec->vma);
10801 }
10802 }
10803
6e0b88f1 10804 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10805 ret = elf_link_output_symstrtab (flinfo, name,
10806 &sym, sec,
10807 NULL);
6e0b88f1 10808 if (ret == 0)
c152c796 10809 return FALSE;
6e0b88f1 10810 else if (ret == 1)
8b127cbc 10811 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10812 else
10813 abort ();
c152c796
AM
10814 }
10815
8b127cbc 10816 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10817 }
10818
10819 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10820 | (irela->r_info & r_type_mask));
10821 }
10822
10823 /* Swap out the relocs. */
d4730f92
BS
10824 input_rel_hdr = esdi->rel.hdr;
10825 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10826 {
d4730f92
BS
10827 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10828 input_rel_hdr,
10829 internal_relocs,
10830 rel_hash_list))
10831 return FALSE;
c152c796
AM
10832 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10833 * bed->s->int_rels_per_ext_rel);
eac338cf 10834 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10835 }
10836
10837 input_rela_hdr = esdi->rela.hdr;
10838 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10839 {
eac338cf 10840 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10841 input_rela_hdr,
eac338cf 10842 internal_relocs,
d4730f92 10843 rela_hash_list))
c152c796
AM
10844 return FALSE;
10845 }
10846 }
10847 }
10848
10849 /* Write out the modified section contents. */
10850 if (bed->elf_backend_write_section
8b127cbc 10851 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10852 contents))
c152c796
AM
10853 {
10854 /* Section written out. */
10855 }
10856 else switch (o->sec_info_type)
10857 {
dbaa2011 10858 case SEC_INFO_TYPE_STABS:
c152c796
AM
10859 if (! (_bfd_write_section_stabs
10860 (output_bfd,
8b127cbc 10861 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10862 o, &elf_section_data (o)->sec_info, contents)))
10863 return FALSE;
10864 break;
dbaa2011 10865 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10866 if (! _bfd_write_merged_section (output_bfd, o,
10867 elf_section_data (o)->sec_info))
10868 return FALSE;
10869 break;
dbaa2011 10870 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10871 {
8b127cbc 10872 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10873 o, contents))
10874 return FALSE;
10875 }
10876 break;
2f0c68f2
CM
10877 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10878 {
10879 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10880 flinfo->info,
10881 o, contents))
10882 return FALSE;
10883 }
10884 break;
c152c796
AM
10885 default:
10886 {
310fd250
L
10887 if (! (o->flags & SEC_EXCLUDE))
10888 {
10889 file_ptr offset = (file_ptr) o->output_offset;
10890 bfd_size_type todo = o->size;
37b01f6a
DG
10891
10892 offset *= bfd_octets_per_byte (output_bfd);
10893
310fd250
L
10894 if ((o->flags & SEC_ELF_REVERSE_COPY))
10895 {
10896 /* Reverse-copy input section to output. */
10897 do
10898 {
10899 todo -= address_size;
10900 if (! bfd_set_section_contents (output_bfd,
10901 o->output_section,
10902 contents + todo,
10903 offset,
10904 address_size))
10905 return FALSE;
10906 if (todo == 0)
10907 break;
10908 offset += address_size;
10909 }
10910 while (1);
10911 }
10912 else if (! bfd_set_section_contents (output_bfd,
10913 o->output_section,
10914 contents,
10915 offset, todo))
10916 return FALSE;
10917 }
c152c796
AM
10918 }
10919 break;
10920 }
10921 }
10922
10923 return TRUE;
10924}
10925
10926/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10927 requested by the linker, and does not come from any input file. This
c152c796
AM
10928 is used to build constructor and destructor tables when linking
10929 with -Ur. */
10930
10931static bfd_boolean
10932elf_reloc_link_order (bfd *output_bfd,
10933 struct bfd_link_info *info,
10934 asection *output_section,
10935 struct bfd_link_order *link_order)
10936{
10937 reloc_howto_type *howto;
10938 long indx;
10939 bfd_vma offset;
10940 bfd_vma addend;
d4730f92 10941 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10942 struct elf_link_hash_entry **rel_hash_ptr;
10943 Elf_Internal_Shdr *rel_hdr;
10944 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10945 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10946 bfd_byte *erel;
10947 unsigned int i;
d4730f92 10948 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10949
10950 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10951 if (howto == NULL)
10952 {
10953 bfd_set_error (bfd_error_bad_value);
10954 return FALSE;
10955 }
10956
10957 addend = link_order->u.reloc.p->addend;
10958
d4730f92
BS
10959 if (esdo->rel.hdr)
10960 reldata = &esdo->rel;
10961 else if (esdo->rela.hdr)
10962 reldata = &esdo->rela;
10963 else
10964 {
10965 reldata = NULL;
10966 BFD_ASSERT (0);
10967 }
10968
c152c796 10969 /* Figure out the symbol index. */
d4730f92 10970 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10971 if (link_order->type == bfd_section_reloc_link_order)
10972 {
10973 indx = link_order->u.reloc.p->u.section->target_index;
10974 BFD_ASSERT (indx != 0);
10975 *rel_hash_ptr = NULL;
10976 }
10977 else
10978 {
10979 struct elf_link_hash_entry *h;
10980
10981 /* Treat a reloc against a defined symbol as though it were
10982 actually against the section. */
10983 h = ((struct elf_link_hash_entry *)
10984 bfd_wrapped_link_hash_lookup (output_bfd, info,
10985 link_order->u.reloc.p->u.name,
10986 FALSE, FALSE, TRUE));
10987 if (h != NULL
10988 && (h->root.type == bfd_link_hash_defined
10989 || h->root.type == bfd_link_hash_defweak))
10990 {
10991 asection *section;
10992
10993 section = h->root.u.def.section;
10994 indx = section->output_section->target_index;
10995 *rel_hash_ptr = NULL;
10996 /* It seems that we ought to add the symbol value to the
10997 addend here, but in practice it has already been added
10998 because it was passed to constructor_callback. */
10999 addend += section->output_section->vma + section->output_offset;
11000 }
11001 else if (h != NULL)
11002 {
11003 /* Setting the index to -2 tells elf_link_output_extsym that
11004 this symbol is used by a reloc. */
11005 h->indx = -2;
11006 *rel_hash_ptr = h;
11007 indx = 0;
11008 }
11009 else
11010 {
1a72702b
AM
11011 (*info->callbacks->unattached_reloc)
11012 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11013 indx = 0;
11014 }
11015 }
11016
11017 /* If this is an inplace reloc, we must write the addend into the
11018 object file. */
11019 if (howto->partial_inplace && addend != 0)
11020 {
11021 bfd_size_type size;
11022 bfd_reloc_status_type rstat;
11023 bfd_byte *buf;
11024 bfd_boolean ok;
11025 const char *sym_name;
11026
a50b1753
NC
11027 size = (bfd_size_type) bfd_get_reloc_size (howto);
11028 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11029 if (buf == NULL && size != 0)
c152c796
AM
11030 return FALSE;
11031 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11032 switch (rstat)
11033 {
11034 case bfd_reloc_ok:
11035 break;
11036
11037 default:
11038 case bfd_reloc_outofrange:
11039 abort ();
11040
11041 case bfd_reloc_overflow:
11042 if (link_order->type == bfd_section_reloc_link_order)
11043 sym_name = bfd_section_name (output_bfd,
11044 link_order->u.reloc.p->u.section);
11045 else
11046 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11047 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11048 howto->name, addend, NULL, NULL,
11049 (bfd_vma) 0);
c152c796
AM
11050 break;
11051 }
37b01f6a 11052
c152c796 11053 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11054 link_order->offset
11055 * bfd_octets_per_byte (output_bfd),
11056 size);
c152c796
AM
11057 free (buf);
11058 if (! ok)
11059 return FALSE;
11060 }
11061
11062 /* The address of a reloc is relative to the section in a
11063 relocatable file, and is a virtual address in an executable
11064 file. */
11065 offset = link_order->offset;
0e1862bb 11066 if (! bfd_link_relocatable (info))
c152c796
AM
11067 offset += output_section->vma;
11068
11069 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11070 {
11071 irel[i].r_offset = offset;
11072 irel[i].r_info = 0;
11073 irel[i].r_addend = 0;
11074 }
11075 if (bed->s->arch_size == 32)
11076 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11077 else
11078 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11079
d4730f92 11080 rel_hdr = reldata->hdr;
c152c796
AM
11081 erel = rel_hdr->contents;
11082 if (rel_hdr->sh_type == SHT_REL)
11083 {
d4730f92 11084 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11085 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11086 }
11087 else
11088 {
11089 irel[0].r_addend = addend;
d4730f92 11090 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11091 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11092 }
11093
d4730f92 11094 ++reldata->count;
c152c796
AM
11095
11096 return TRUE;
11097}
11098
0b52efa6
PB
11099
11100/* Get the output vma of the section pointed to by the sh_link field. */
11101
11102static bfd_vma
11103elf_get_linked_section_vma (struct bfd_link_order *p)
11104{
11105 Elf_Internal_Shdr **elf_shdrp;
11106 asection *s;
11107 int elfsec;
11108
11109 s = p->u.indirect.section;
11110 elf_shdrp = elf_elfsections (s->owner);
11111 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11112 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11113 /* PR 290:
11114 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11115 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11116 sh_info fields. Hence we could get the situation
11117 where elfsec is 0. */
11118 if (elfsec == 0)
11119 {
11120 const struct elf_backend_data *bed
11121 = get_elf_backend_data (s->owner);
11122 if (bed->link_order_error_handler)
d003868e 11123 bed->link_order_error_handler
695344c0 11124 /* xgettext:c-format */
d003868e 11125 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11126 return 0;
11127 }
11128 else
11129 {
11130 s = elf_shdrp[elfsec]->bfd_section;
11131 return s->output_section->vma + s->output_offset;
11132 }
0b52efa6
PB
11133}
11134
11135
11136/* Compare two sections based on the locations of the sections they are
11137 linked to. Used by elf_fixup_link_order. */
11138
11139static int
11140compare_link_order (const void * a, const void * b)
11141{
11142 bfd_vma apos;
11143 bfd_vma bpos;
11144
11145 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11146 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11147 if (apos < bpos)
11148 return -1;
11149 return apos > bpos;
11150}
11151
11152
11153/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11154 order as their linked sections. Returns false if this could not be done
11155 because an output section includes both ordered and unordered
11156 sections. Ideally we'd do this in the linker proper. */
11157
11158static bfd_boolean
11159elf_fixup_link_order (bfd *abfd, asection *o)
11160{
11161 int seen_linkorder;
11162 int seen_other;
11163 int n;
11164 struct bfd_link_order *p;
11165 bfd *sub;
11166 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11167 unsigned elfsec;
0b52efa6 11168 struct bfd_link_order **sections;
d33cdfe3 11169 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11170 bfd_vma offset;
3b36f7e6 11171
d33cdfe3
L
11172 other_sec = NULL;
11173 linkorder_sec = NULL;
0b52efa6
PB
11174 seen_other = 0;
11175 seen_linkorder = 0;
8423293d 11176 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11177 {
d33cdfe3 11178 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11179 {
11180 s = p->u.indirect.section;
d33cdfe3
L
11181 sub = s->owner;
11182 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11183 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11184 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11185 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11186 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11187 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11188 {
11189 seen_linkorder++;
11190 linkorder_sec = s;
11191 }
0b52efa6 11192 else
d33cdfe3
L
11193 {
11194 seen_other++;
11195 other_sec = s;
11196 }
0b52efa6
PB
11197 }
11198 else
11199 seen_other++;
d33cdfe3
L
11200
11201 if (seen_other && seen_linkorder)
11202 {
11203 if (other_sec && linkorder_sec)
4eca0228 11204 _bfd_error_handler
695344c0 11205 /* xgettext:c-format */
4eca0228
AM
11206 (_("%A has both ordered [`%A' in %B] "
11207 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11208 o, linkorder_sec, linkorder_sec->owner,
11209 other_sec, other_sec->owner);
d33cdfe3 11210 else
4eca0228
AM
11211 _bfd_error_handler
11212 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11213 bfd_set_error (bfd_error_bad_value);
11214 return FALSE;
11215 }
0b52efa6
PB
11216 }
11217
11218 if (!seen_linkorder)
11219 return TRUE;
11220
0b52efa6 11221 sections = (struct bfd_link_order **)
14b1c01e
AM
11222 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11223 if (sections == NULL)
11224 return FALSE;
0b52efa6 11225 seen_linkorder = 0;
3b36f7e6 11226
8423293d 11227 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11228 {
11229 sections[seen_linkorder++] = p;
11230 }
11231 /* Sort the input sections in the order of their linked section. */
11232 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11233 compare_link_order);
11234
11235 /* Change the offsets of the sections. */
11236 offset = 0;
11237 for (n = 0; n < seen_linkorder; n++)
11238 {
11239 s = sections[n]->u.indirect.section;
461686a3 11240 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11241 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11242 sections[n]->offset = offset;
11243 offset += sections[n]->size;
11244 }
11245
4dd07732 11246 free (sections);
0b52efa6
PB
11247 return TRUE;
11248}
11249
76359541
TP
11250/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11251 Returns TRUE upon success, FALSE otherwise. */
11252
11253static bfd_boolean
11254elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11255{
11256 bfd_boolean ret = FALSE;
11257 bfd *implib_bfd;
11258 const struct elf_backend_data *bed;
11259 flagword flags;
11260 enum bfd_architecture arch;
11261 unsigned int mach;
11262 asymbol **sympp = NULL;
11263 long symsize;
11264 long symcount;
11265 long src_count;
11266 elf_symbol_type *osymbuf;
11267
11268 implib_bfd = info->out_implib_bfd;
11269 bed = get_elf_backend_data (abfd);
11270
11271 if (!bfd_set_format (implib_bfd, bfd_object))
11272 return FALSE;
11273
11274 flags = bfd_get_file_flags (abfd);
11275 flags &= ~HAS_RELOC;
11276 if (!bfd_set_start_address (implib_bfd, 0)
11277 || !bfd_set_file_flags (implib_bfd, flags))
11278 return FALSE;
11279
11280 /* Copy architecture of output file to import library file. */
11281 arch = bfd_get_arch (abfd);
11282 mach = bfd_get_mach (abfd);
11283 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11284 && (abfd->target_defaulted
11285 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11286 return FALSE;
11287
11288 /* Get symbol table size. */
11289 symsize = bfd_get_symtab_upper_bound (abfd);
11290 if (symsize < 0)
11291 return FALSE;
11292
11293 /* Read in the symbol table. */
11294 sympp = (asymbol **) xmalloc (symsize);
11295 symcount = bfd_canonicalize_symtab (abfd, sympp);
11296 if (symcount < 0)
11297 goto free_sym_buf;
11298
11299 /* Allow the BFD backend to copy any private header data it
11300 understands from the output BFD to the import library BFD. */
11301 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11302 goto free_sym_buf;
11303
11304 /* Filter symbols to appear in the import library. */
11305 if (bed->elf_backend_filter_implib_symbols)
11306 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11307 symcount);
11308 else
11309 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11310 if (symcount == 0)
11311 {
5df1bc57 11312 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11313 _bfd_error_handler (_("%B: no symbol found for import library"),
11314 implib_bfd);
76359541
TP
11315 goto free_sym_buf;
11316 }
11317
11318
11319 /* Make symbols absolute. */
11320 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11321 sizeof (*osymbuf));
11322 for (src_count = 0; src_count < symcount; src_count++)
11323 {
11324 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11325 sizeof (*osymbuf));
11326 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11327 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11328 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11329 osymbuf[src_count].internal_elf_sym.st_value =
11330 osymbuf[src_count].symbol.value;
11331 sympp[src_count] = &osymbuf[src_count].symbol;
11332 }
11333
11334 bfd_set_symtab (implib_bfd, sympp, symcount);
11335
11336 /* Allow the BFD backend to copy any private data it understands
11337 from the output BFD to the import library BFD. This is done last
11338 to permit the routine to look at the filtered symbol table. */
11339 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11340 goto free_sym_buf;
11341
11342 if (!bfd_close (implib_bfd))
11343 goto free_sym_buf;
11344
11345 ret = TRUE;
11346
11347free_sym_buf:
11348 free (sympp);
11349 return ret;
11350}
11351
9f7c3e5e
AM
11352static void
11353elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11354{
11355 asection *o;
11356
11357 if (flinfo->symstrtab != NULL)
ef10c3ac 11358 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11359 if (flinfo->contents != NULL)
11360 free (flinfo->contents);
11361 if (flinfo->external_relocs != NULL)
11362 free (flinfo->external_relocs);
11363 if (flinfo->internal_relocs != NULL)
11364 free (flinfo->internal_relocs);
11365 if (flinfo->external_syms != NULL)
11366 free (flinfo->external_syms);
11367 if (flinfo->locsym_shndx != NULL)
11368 free (flinfo->locsym_shndx);
11369 if (flinfo->internal_syms != NULL)
11370 free (flinfo->internal_syms);
11371 if (flinfo->indices != NULL)
11372 free (flinfo->indices);
11373 if (flinfo->sections != NULL)
11374 free (flinfo->sections);
9f7c3e5e
AM
11375 if (flinfo->symshndxbuf != NULL)
11376 free (flinfo->symshndxbuf);
11377 for (o = obfd->sections; o != NULL; o = o->next)
11378 {
11379 struct bfd_elf_section_data *esdo = elf_section_data (o);
11380 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11381 free (esdo->rel.hashes);
11382 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11383 free (esdo->rela.hashes);
11384 }
11385}
0b52efa6 11386
c152c796
AM
11387/* Do the final step of an ELF link. */
11388
11389bfd_boolean
11390bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11391{
11392 bfd_boolean dynamic;
11393 bfd_boolean emit_relocs;
11394 bfd *dynobj;
8b127cbc 11395 struct elf_final_link_info flinfo;
91d6fa6a
NC
11396 asection *o;
11397 struct bfd_link_order *p;
11398 bfd *sub;
c152c796
AM
11399 bfd_size_type max_contents_size;
11400 bfd_size_type max_external_reloc_size;
11401 bfd_size_type max_internal_reloc_count;
11402 bfd_size_type max_sym_count;
11403 bfd_size_type max_sym_shndx_count;
c152c796
AM
11404 Elf_Internal_Sym elfsym;
11405 unsigned int i;
11406 Elf_Internal_Shdr *symtab_hdr;
11407 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11409 struct elf_outext_info eoinfo;
11410 bfd_boolean merged;
11411 size_t relativecount = 0;
11412 asection *reldyn = 0;
11413 bfd_size_type amt;
104d59d1
JM
11414 asection *attr_section = NULL;
11415 bfd_vma attr_size = 0;
11416 const char *std_attrs_section;
64f52338 11417 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11418
64f52338 11419 if (!is_elf_hash_table (htab))
c152c796
AM
11420 return FALSE;
11421
0e1862bb 11422 if (bfd_link_pic (info))
c152c796
AM
11423 abfd->flags |= DYNAMIC;
11424
64f52338
AM
11425 dynamic = htab->dynamic_sections_created;
11426 dynobj = htab->dynobj;
c152c796 11427
0e1862bb 11428 emit_relocs = (bfd_link_relocatable (info)
a4676736 11429 || info->emitrelocations);
c152c796 11430
8b127cbc
AM
11431 flinfo.info = info;
11432 flinfo.output_bfd = abfd;
ef10c3ac 11433 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11434 if (flinfo.symstrtab == NULL)
c152c796
AM
11435 return FALSE;
11436
11437 if (! dynamic)
11438 {
8b127cbc
AM
11439 flinfo.hash_sec = NULL;
11440 flinfo.symver_sec = NULL;
c152c796
AM
11441 }
11442 else
11443 {
3d4d4302 11444 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11445 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11446 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11447 /* Note that it is OK if symver_sec is NULL. */
11448 }
11449
8b127cbc
AM
11450 flinfo.contents = NULL;
11451 flinfo.external_relocs = NULL;
11452 flinfo.internal_relocs = NULL;
11453 flinfo.external_syms = NULL;
11454 flinfo.locsym_shndx = NULL;
11455 flinfo.internal_syms = NULL;
11456 flinfo.indices = NULL;
11457 flinfo.sections = NULL;
8b127cbc 11458 flinfo.symshndxbuf = NULL;
ffbc01cc 11459 flinfo.filesym_count = 0;
c152c796 11460
104d59d1
JM
11461 /* The object attributes have been merged. Remove the input
11462 sections from the link, and set the contents of the output
11463 secton. */
11464 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11465 for (o = abfd->sections; o != NULL; o = o->next)
11466 {
11467 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11468 || strcmp (o->name, ".gnu.attributes") == 0)
11469 {
11470 for (p = o->map_head.link_order; p != NULL; p = p->next)
11471 {
11472 asection *input_section;
11473
11474 if (p->type != bfd_indirect_link_order)
11475 continue;
11476 input_section = p->u.indirect.section;
11477 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11478 elf_link_input_bfd ignores this section. */
11479 input_section->flags &= ~SEC_HAS_CONTENTS;
11480 }
a0c8462f 11481
104d59d1
JM
11482 attr_size = bfd_elf_obj_attr_size (abfd);
11483 if (attr_size)
11484 {
11485 bfd_set_section_size (abfd, o, attr_size);
11486 attr_section = o;
11487 /* Skip this section later on. */
11488 o->map_head.link_order = NULL;
11489 }
11490 else
11491 o->flags |= SEC_EXCLUDE;
11492 }
11493 }
11494
c152c796
AM
11495 /* Count up the number of relocations we will output for each output
11496 section, so that we know the sizes of the reloc sections. We
11497 also figure out some maximum sizes. */
11498 max_contents_size = 0;
11499 max_external_reloc_size = 0;
11500 max_internal_reloc_count = 0;
11501 max_sym_count = 0;
11502 max_sym_shndx_count = 0;
11503 merged = FALSE;
11504 for (o = abfd->sections; o != NULL; o = o->next)
11505 {
11506 struct bfd_elf_section_data *esdo = elf_section_data (o);
11507 o->reloc_count = 0;
11508
8423293d 11509 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11510 {
11511 unsigned int reloc_count = 0;
9eaff861 11512 unsigned int additional_reloc_count = 0;
c152c796 11513 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11514
11515 if (p->type == bfd_section_reloc_link_order
11516 || p->type == bfd_symbol_reloc_link_order)
11517 reloc_count = 1;
11518 else if (p->type == bfd_indirect_link_order)
11519 {
11520 asection *sec;
11521
11522 sec = p->u.indirect.section;
c152c796
AM
11523
11524 /* Mark all sections which are to be included in the
11525 link. This will normally be every section. We need
11526 to do this so that we can identify any sections which
11527 the linker has decided to not include. */
11528 sec->linker_mark = TRUE;
11529
11530 if (sec->flags & SEC_MERGE)
11531 merged = TRUE;
11532
eea6121a
AM
11533 if (sec->rawsize > max_contents_size)
11534 max_contents_size = sec->rawsize;
11535 if (sec->size > max_contents_size)
11536 max_contents_size = sec->size;
c152c796 11537
c152c796
AM
11538 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11539 && (sec->owner->flags & DYNAMIC) == 0)
11540 {
11541 size_t sym_count;
11542
a961cdd5
AM
11543 /* We are interested in just local symbols, not all
11544 symbols. */
c152c796
AM
11545 if (elf_bad_symtab (sec->owner))
11546 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11547 / bed->s->sizeof_sym);
11548 else
11549 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11550
11551 if (sym_count > max_sym_count)
11552 max_sym_count = sym_count;
11553
11554 if (sym_count > max_sym_shndx_count
6a40cf0c 11555 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11556 max_sym_shndx_count = sym_count;
11557
a961cdd5
AM
11558 if (esdo->this_hdr.sh_type == SHT_REL
11559 || esdo->this_hdr.sh_type == SHT_RELA)
11560 /* Some backends use reloc_count in relocation sections
11561 to count particular types of relocs. Of course,
11562 reloc sections themselves can't have relocations. */
11563 ;
11564 else if (emit_relocs)
11565 {
11566 reloc_count = sec->reloc_count;
11567 if (bed->elf_backend_count_additional_relocs)
11568 {
11569 int c;
11570 c = (*bed->elf_backend_count_additional_relocs) (sec);
11571 additional_reloc_count += c;
11572 }
11573 }
11574 else if (bed->elf_backend_count_relocs)
11575 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11576
11577 esdi = elf_section_data (sec);
11578
c152c796
AM
11579 if ((sec->flags & SEC_RELOC) != 0)
11580 {
d4730f92 11581 size_t ext_size = 0;
c152c796 11582
d4730f92
BS
11583 if (esdi->rel.hdr != NULL)
11584 ext_size = esdi->rel.hdr->sh_size;
11585 if (esdi->rela.hdr != NULL)
11586 ext_size += esdi->rela.hdr->sh_size;
7326c758 11587
c152c796
AM
11588 if (ext_size > max_external_reloc_size)
11589 max_external_reloc_size = ext_size;
11590 if (sec->reloc_count > max_internal_reloc_count)
11591 max_internal_reloc_count = sec->reloc_count;
11592 }
11593 }
11594 }
11595
11596 if (reloc_count == 0)
11597 continue;
11598
9eaff861 11599 reloc_count += additional_reloc_count;
c152c796
AM
11600 o->reloc_count += reloc_count;
11601
0e1862bb 11602 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11603 {
d4730f92 11604 if (esdi->rel.hdr)
9eaff861 11605 {
491d01d3 11606 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11607 esdo->rel.count += additional_reloc_count;
11608 }
d4730f92 11609 if (esdi->rela.hdr)
9eaff861 11610 {
491d01d3 11611 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11612 esdo->rela.count += additional_reloc_count;
11613 }
d4730f92
BS
11614 }
11615 else
11616 {
11617 if (o->use_rela_p)
11618 esdo->rela.count += reloc_count;
2c2b4ed4 11619 else
d4730f92 11620 esdo->rel.count += reloc_count;
c152c796 11621 }
c152c796
AM
11622 }
11623
9eaff861 11624 if (o->reloc_count > 0)
c152c796
AM
11625 o->flags |= SEC_RELOC;
11626 else
11627 {
11628 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11629 set it (this is probably a bug) and if it is set
11630 assign_section_numbers will create a reloc section. */
11631 o->flags &=~ SEC_RELOC;
11632 }
11633
11634 /* If the SEC_ALLOC flag is not set, force the section VMA to
11635 zero. This is done in elf_fake_sections as well, but forcing
11636 the VMA to 0 here will ensure that relocs against these
11637 sections are handled correctly. */
11638 if ((o->flags & SEC_ALLOC) == 0
11639 && ! o->user_set_vma)
11640 o->vma = 0;
11641 }
11642
0e1862bb 11643 if (! bfd_link_relocatable (info) && merged)
64f52338 11644 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11645
11646 /* Figure out the file positions for everything but the symbol table
11647 and the relocs. We set symcount to force assign_section_numbers
11648 to create a symbol table. */
8539e4e8 11649 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11650 BFD_ASSERT (! abfd->output_has_begun);
11651 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11652 goto error_return;
11653
ee75fd95 11654 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11655 for (o = abfd->sections; o != NULL; o = o->next)
11656 {
d4730f92 11657 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11658 if ((o->flags & SEC_RELOC) != 0)
11659 {
d4730f92 11660 if (esdo->rel.hdr
9eaff861 11661 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11662 goto error_return;
11663
d4730f92 11664 if (esdo->rela.hdr
9eaff861 11665 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11666 goto error_return;
11667 }
11668
11669 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11670 to count upwards while actually outputting the relocations. */
d4730f92
BS
11671 esdo->rel.count = 0;
11672 esdo->rela.count = 0;
0ce398f1
L
11673
11674 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11675 {
11676 /* Cache the section contents so that they can be compressed
11677 later. Use bfd_malloc since it will be freed by
11678 bfd_compress_section_contents. */
11679 unsigned char *contents = esdo->this_hdr.contents;
11680 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11681 abort ();
11682 contents
11683 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11684 if (contents == NULL)
11685 goto error_return;
11686 esdo->this_hdr.contents = contents;
11687 }
c152c796
AM
11688 }
11689
c152c796 11690 /* We have now assigned file positions for all the sections except
a485e98e
AM
11691 .symtab, .strtab, and non-loaded reloc sections. We start the
11692 .symtab section at the current file position, and write directly
11693 to it. We build the .strtab section in memory. */
c152c796
AM
11694 bfd_get_symcount (abfd) = 0;
11695 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11696 /* sh_name is set in prep_headers. */
11697 symtab_hdr->sh_type = SHT_SYMTAB;
11698 /* sh_flags, sh_addr and sh_size all start off zero. */
11699 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11700 /* sh_link is set in assign_section_numbers. */
11701 /* sh_info is set below. */
11702 /* sh_offset is set just below. */
72de5009 11703 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11704
ef10c3ac
L
11705 if (max_sym_count < 20)
11706 max_sym_count = 20;
64f52338 11707 htab->strtabsize = max_sym_count;
ef10c3ac 11708 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11709 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11710 if (htab->strtab == NULL)
c152c796 11711 goto error_return;
ef10c3ac
L
11712 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11713 flinfo.symshndxbuf
11714 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11715 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11716
8539e4e8 11717 if (info->strip != strip_all || emit_relocs)
c152c796 11718 {
8539e4e8
AM
11719 file_ptr off = elf_next_file_pos (abfd);
11720
11721 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11722
11723 /* Note that at this point elf_next_file_pos (abfd) is
11724 incorrect. We do not yet know the size of the .symtab section.
11725 We correct next_file_pos below, after we do know the size. */
11726
11727 /* Start writing out the symbol table. The first symbol is always a
11728 dummy symbol. */
c152c796
AM
11729 elfsym.st_value = 0;
11730 elfsym.st_size = 0;
11731 elfsym.st_info = 0;
11732 elfsym.st_other = 0;
11733 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11734 elfsym.st_target_internal = 0;
ef10c3ac
L
11735 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11736 bfd_und_section_ptr, NULL) != 1)
c152c796 11737 goto error_return;
c152c796 11738
8539e4e8
AM
11739 /* Output a symbol for each section. We output these even if we are
11740 discarding local symbols, since they are used for relocs. These
11741 symbols have no names. We store the index of each one in the
11742 index field of the section, so that we can find it again when
11743 outputting relocs. */
11744
c152c796
AM
11745 elfsym.st_size = 0;
11746 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11747 elfsym.st_other = 0;
f0b5bb34 11748 elfsym.st_value = 0;
35fc36a8 11749 elfsym.st_target_internal = 0;
c152c796
AM
11750 for (i = 1; i < elf_numsections (abfd); i++)
11751 {
11752 o = bfd_section_from_elf_index (abfd, i);
11753 if (o != NULL)
f0b5bb34
AM
11754 {
11755 o->target_index = bfd_get_symcount (abfd);
11756 elfsym.st_shndx = i;
0e1862bb 11757 if (!bfd_link_relocatable (info))
f0b5bb34 11758 elfsym.st_value = o->vma;
ef10c3ac
L
11759 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11760 NULL) != 1)
f0b5bb34
AM
11761 goto error_return;
11762 }
c152c796
AM
11763 }
11764 }
11765
11766 /* Allocate some memory to hold information read in from the input
11767 files. */
11768 if (max_contents_size != 0)
11769 {
8b127cbc
AM
11770 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11771 if (flinfo.contents == NULL)
c152c796
AM
11772 goto error_return;
11773 }
11774
11775 if (max_external_reloc_size != 0)
11776 {
8b127cbc
AM
11777 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11778 if (flinfo.external_relocs == NULL)
c152c796
AM
11779 goto error_return;
11780 }
11781
11782 if (max_internal_reloc_count != 0)
11783 {
11784 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11785 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11786 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11787 if (flinfo.internal_relocs == NULL)
c152c796
AM
11788 goto error_return;
11789 }
11790
11791 if (max_sym_count != 0)
11792 {
11793 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11794 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11795 if (flinfo.external_syms == NULL)
c152c796
AM
11796 goto error_return;
11797
11798 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11799 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11800 if (flinfo.internal_syms == NULL)
c152c796
AM
11801 goto error_return;
11802
11803 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11804 flinfo.indices = (long int *) bfd_malloc (amt);
11805 if (flinfo.indices == NULL)
c152c796
AM
11806 goto error_return;
11807
11808 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11809 flinfo.sections = (asection **) bfd_malloc (amt);
11810 if (flinfo.sections == NULL)
c152c796
AM
11811 goto error_return;
11812 }
11813
11814 if (max_sym_shndx_count != 0)
11815 {
11816 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11817 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11818 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11819 goto error_return;
11820 }
11821
64f52338 11822 if (htab->tls_sec)
c152c796
AM
11823 {
11824 bfd_vma base, end = 0;
11825 asection *sec;
11826
64f52338 11827 for (sec = htab->tls_sec;
c152c796
AM
11828 sec && (sec->flags & SEC_THREAD_LOCAL);
11829 sec = sec->next)
11830 {
3a800eb9 11831 bfd_size_type size = sec->size;
c152c796 11832
3a800eb9
AM
11833 if (size == 0
11834 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11835 {
91d6fa6a
NC
11836 struct bfd_link_order *ord = sec->map_tail.link_order;
11837
11838 if (ord != NULL)
11839 size = ord->offset + ord->size;
c152c796
AM
11840 }
11841 end = sec->vma + size;
11842 }
64f52338 11843 base = htab->tls_sec->vma;
7dc98aea
RO
11844 /* Only align end of TLS section if static TLS doesn't have special
11845 alignment requirements. */
11846 if (bed->static_tls_alignment == 1)
64f52338
AM
11847 end = align_power (end, htab->tls_sec->alignment_power);
11848 htab->tls_size = end - base;
c152c796
AM
11849 }
11850
0b52efa6
PB
11851 /* Reorder SHF_LINK_ORDER sections. */
11852 for (o = abfd->sections; o != NULL; o = o->next)
11853 {
11854 if (!elf_fixup_link_order (abfd, o))
11855 return FALSE;
11856 }
11857
2f0c68f2
CM
11858 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11859 return FALSE;
11860
c152c796
AM
11861 /* Since ELF permits relocations to be against local symbols, we
11862 must have the local symbols available when we do the relocations.
11863 Since we would rather only read the local symbols once, and we
11864 would rather not keep them in memory, we handle all the
11865 relocations for a single input file at the same time.
11866
11867 Unfortunately, there is no way to know the total number of local
11868 symbols until we have seen all of them, and the local symbol
11869 indices precede the global symbol indices. This means that when
11870 we are generating relocatable output, and we see a reloc against
11871 a global symbol, we can not know the symbol index until we have
11872 finished examining all the local symbols to see which ones we are
11873 going to output. To deal with this, we keep the relocations in
11874 memory, and don't output them until the end of the link. This is
11875 an unfortunate waste of memory, but I don't see a good way around
11876 it. Fortunately, it only happens when performing a relocatable
11877 link, which is not the common case. FIXME: If keep_memory is set
11878 we could write the relocs out and then read them again; I don't
11879 know how bad the memory loss will be. */
11880
c72f2fb2 11881 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11882 sub->output_has_begun = FALSE;
11883 for (o = abfd->sections; o != NULL; o = o->next)
11884 {
8423293d 11885 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11886 {
11887 if (p->type == bfd_indirect_link_order
11888 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11889 == bfd_target_elf_flavour)
11890 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11891 {
11892 if (! sub->output_has_begun)
11893 {
8b127cbc 11894 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11895 goto error_return;
11896 sub->output_has_begun = TRUE;
11897 }
11898 }
11899 else if (p->type == bfd_section_reloc_link_order
11900 || p->type == bfd_symbol_reloc_link_order)
11901 {
11902 if (! elf_reloc_link_order (abfd, info, o, p))
11903 goto error_return;
11904 }
11905 else
11906 {
11907 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11908 {
11909 if (p->type == bfd_indirect_link_order
11910 && (bfd_get_flavour (sub)
11911 == bfd_target_elf_flavour)
11912 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11913 != bed->s->elfclass))
11914 {
11915 const char *iclass, *oclass;
11916
aebf9be7 11917 switch (bed->s->elfclass)
351f65ca 11918 {
aebf9be7
NC
11919 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11920 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11921 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11922 default: abort ();
351f65ca 11923 }
aebf9be7
NC
11924
11925 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11926 {
aebf9be7
NC
11927 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11928 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11929 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11930 default: abort ();
351f65ca
L
11931 }
11932
11933 bfd_set_error (bfd_error_wrong_format);
4eca0228 11934 _bfd_error_handler
695344c0 11935 /* xgettext:c-format */
351f65ca
L
11936 (_("%B: file class %s incompatible with %s"),
11937 sub, iclass, oclass);
11938 }
11939
11940 goto error_return;
11941 }
c152c796
AM
11942 }
11943 }
11944 }
11945
c0f00686
L
11946 /* Free symbol buffer if needed. */
11947 if (!info->reduce_memory_overheads)
11948 {
c72f2fb2 11949 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11950 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11951 && elf_tdata (sub)->symbuf)
c0f00686
L
11952 {
11953 free (elf_tdata (sub)->symbuf);
11954 elf_tdata (sub)->symbuf = NULL;
11955 }
11956 }
11957
c152c796
AM
11958 /* Output any global symbols that got converted to local in a
11959 version script or due to symbol visibility. We do this in a
11960 separate step since ELF requires all local symbols to appear
11961 prior to any global symbols. FIXME: We should only do this if
11962 some global symbols were, in fact, converted to become local.
11963 FIXME: Will this work correctly with the Irix 5 linker? */
11964 eoinfo.failed = FALSE;
8b127cbc 11965 eoinfo.flinfo = &flinfo;
c152c796 11966 eoinfo.localsyms = TRUE;
34a79995 11967 eoinfo.file_sym_done = FALSE;
7686d77d 11968 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11969 if (eoinfo.failed)
11970 return FALSE;
11971
4e617b1e
PB
11972 /* If backend needs to output some local symbols not present in the hash
11973 table, do it now. */
8539e4e8
AM
11974 if (bed->elf_backend_output_arch_local_syms
11975 && (info->strip != strip_all || emit_relocs))
4e617b1e 11976 {
6e0b88f1 11977 typedef int (*out_sym_func)
4e617b1e
PB
11978 (void *, const char *, Elf_Internal_Sym *, asection *,
11979 struct elf_link_hash_entry *);
11980
11981 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11982 (abfd, info, &flinfo,
11983 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11984 return FALSE;
11985 }
11986
c152c796
AM
11987 /* That wrote out all the local symbols. Finish up the symbol table
11988 with the global symbols. Even if we want to strip everything we
11989 can, we still need to deal with those global symbols that got
11990 converted to local in a version script. */
11991
11992 /* The sh_info field records the index of the first non local symbol. */
11993 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11994
11995 if (dynamic
64f52338
AM
11996 && htab->dynsym != NULL
11997 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
11998 {
11999 Elf_Internal_Sym sym;
64f52338 12000 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12001
64f52338
AM
12002 o = htab->dynsym->output_section;
12003 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12004
12005 /* Write out the section symbols for the output sections. */
0e1862bb 12006 if (bfd_link_pic (info)
64f52338 12007 || htab->is_relocatable_executable)
c152c796
AM
12008 {
12009 asection *s;
12010
12011 sym.st_size = 0;
12012 sym.st_name = 0;
12013 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12014 sym.st_other = 0;
35fc36a8 12015 sym.st_target_internal = 0;
c152c796
AM
12016
12017 for (s = abfd->sections; s != NULL; s = s->next)
12018 {
12019 int indx;
12020 bfd_byte *dest;
12021 long dynindx;
12022
c152c796 12023 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12024 if (dynindx <= 0)
12025 continue;
12026 indx = elf_section_data (s)->this_idx;
c152c796
AM
12027 BFD_ASSERT (indx > 0);
12028 sym.st_shndx = indx;
c0d5a53d
L
12029 if (! check_dynsym (abfd, &sym))
12030 return FALSE;
c152c796
AM
12031 sym.st_value = s->vma;
12032 dest = dynsym + dynindx * bed->s->sizeof_sym;
12033 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12034 }
c152c796
AM
12035 }
12036
12037 /* Write out the local dynsyms. */
64f52338 12038 if (htab->dynlocal)
c152c796
AM
12039 {
12040 struct elf_link_local_dynamic_entry *e;
64f52338 12041 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12042 {
12043 asection *s;
12044 bfd_byte *dest;
12045
935bd1e0 12046 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12047 Note that we saved a word of storage and overwrote
12048 the original st_name with the dynstr_index. */
12049 sym = e->isym;
935bd1e0 12050 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12051
cb33740c
AM
12052 s = bfd_section_from_elf_index (e->input_bfd,
12053 e->isym.st_shndx);
12054 if (s != NULL)
c152c796 12055 {
c152c796
AM
12056 sym.st_shndx =
12057 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12058 if (! check_dynsym (abfd, &sym))
12059 return FALSE;
c152c796
AM
12060 sym.st_value = (s->output_section->vma
12061 + s->output_offset
12062 + e->isym.st_value);
12063 }
12064
c152c796
AM
12065 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12066 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12067 }
12068 }
c152c796
AM
12069 }
12070
12071 /* We get the global symbols from the hash table. */
12072 eoinfo.failed = FALSE;
12073 eoinfo.localsyms = FALSE;
8b127cbc 12074 eoinfo.flinfo = &flinfo;
7686d77d 12075 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12076 if (eoinfo.failed)
12077 return FALSE;
12078
12079 /* If backend needs to output some symbols not present in the hash
12080 table, do it now. */
8539e4e8
AM
12081 if (bed->elf_backend_output_arch_syms
12082 && (info->strip != strip_all || emit_relocs))
c152c796 12083 {
6e0b88f1 12084 typedef int (*out_sym_func)
c152c796
AM
12085 (void *, const char *, Elf_Internal_Sym *, asection *,
12086 struct elf_link_hash_entry *);
12087
12088 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12089 (abfd, info, &flinfo,
12090 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12091 return FALSE;
12092 }
12093
ef10c3ac
L
12094 /* Finalize the .strtab section. */
12095 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12096
12097 /* Swap out the .strtab section. */
12098 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12099 return FALSE;
12100
12101 /* Now we know the size of the symtab section. */
c152c796
AM
12102 if (bfd_get_symcount (abfd) > 0)
12103 {
ee3b52e9
L
12104 /* Finish up and write out the symbol string table (.strtab)
12105 section. */
ad32986f 12106 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12107 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12108
ad32986f 12109 if (elf_symtab_shndx_list (abfd))
8539e4e8 12110 {
ad32986f 12111 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12112
ad32986f
NC
12113 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12114 {
12115 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12116 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12117 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12118 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12119 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12120
ad32986f
NC
12121 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12122 off, TRUE);
12123
12124 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12125 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12126 return FALSE;
12127 }
8539e4e8 12128 }
ee3b52e9
L
12129
12130 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12131 /* sh_name was set in prep_headers. */
12132 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12133 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12134 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12135 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12136 symstrtab_hdr->sh_entsize = 0;
12137 symstrtab_hdr->sh_link = 0;
12138 symstrtab_hdr->sh_info = 0;
12139 /* sh_offset is set just below. */
12140 symstrtab_hdr->sh_addralign = 1;
12141
12142 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12143 off, TRUE);
12144 elf_next_file_pos (abfd) = off;
12145
c152c796 12146 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12147 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12148 return FALSE;
12149 }
12150
76359541
TP
12151 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12152 {
4eca0228
AM
12153 _bfd_error_handler (_("%B: failed to generate import library"),
12154 info->out_implib_bfd);
76359541
TP
12155 return FALSE;
12156 }
12157
c152c796
AM
12158 /* Adjust the relocs to have the correct symbol indices. */
12159 for (o = abfd->sections; o != NULL; o = o->next)
12160 {
d4730f92 12161 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12162 bfd_boolean sort;
c152c796
AM
12163 if ((o->flags & SEC_RELOC) == 0)
12164 continue;
12165
28dbcedc 12166 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12167 if (esdo->rel.hdr != NULL
9eaff861 12168 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
12169 return FALSE;
12170 if (esdo->rela.hdr != NULL
9eaff861 12171 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 12172 return FALSE;
c152c796
AM
12173
12174 /* Set the reloc_count field to 0 to prevent write_relocs from
12175 trying to swap the relocs out itself. */
12176 o->reloc_count = 0;
12177 }
12178
12179 if (dynamic && info->combreloc && dynobj != NULL)
12180 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12181
12182 /* If we are linking against a dynamic object, or generating a
12183 shared library, finish up the dynamic linking information. */
12184 if (dynamic)
12185 {
12186 bfd_byte *dyncon, *dynconend;
12187
12188 /* Fix up .dynamic entries. */
3d4d4302 12189 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12190 BFD_ASSERT (o != NULL);
12191
12192 dyncon = o->contents;
eea6121a 12193 dynconend = o->contents + o->size;
c152c796
AM
12194 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12195 {
12196 Elf_Internal_Dyn dyn;
12197 const char *name;
12198 unsigned int type;
64487780
AM
12199 bfd_size_type sh_size;
12200 bfd_vma sh_addr;
c152c796
AM
12201
12202 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12203
12204 switch (dyn.d_tag)
12205 {
12206 default:
12207 continue;
12208 case DT_NULL:
12209 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12210 {
12211 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12212 {
12213 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12214 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12215 default: continue;
12216 }
12217 dyn.d_un.d_val = relativecount;
12218 relativecount = 0;
12219 break;
12220 }
12221 continue;
12222
12223 case DT_INIT:
12224 name = info->init_function;
12225 goto get_sym;
12226 case DT_FINI:
12227 name = info->fini_function;
12228 get_sym:
12229 {
12230 struct elf_link_hash_entry *h;
12231
64f52338 12232 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12233 if (h != NULL
12234 && (h->root.type == bfd_link_hash_defined
12235 || h->root.type == bfd_link_hash_defweak))
12236 {
bef26483 12237 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12238 o = h->root.u.def.section;
12239 if (o->output_section != NULL)
bef26483 12240 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12241 + o->output_offset);
12242 else
12243 {
12244 /* The symbol is imported from another shared
12245 library and does not apply to this one. */
bef26483 12246 dyn.d_un.d_ptr = 0;
c152c796
AM
12247 }
12248 break;
12249 }
12250 }
12251 continue;
12252
12253 case DT_PREINIT_ARRAYSZ:
12254 name = ".preinit_array";
4ade44b7 12255 goto get_out_size;
c152c796
AM
12256 case DT_INIT_ARRAYSZ:
12257 name = ".init_array";
4ade44b7 12258 goto get_out_size;
c152c796
AM
12259 case DT_FINI_ARRAYSZ:
12260 name = ".fini_array";
4ade44b7 12261 get_out_size:
c152c796
AM
12262 o = bfd_get_section_by_name (abfd, name);
12263 if (o == NULL)
12264 {
4eca0228 12265 _bfd_error_handler
4ade44b7 12266 (_("could not find section %s"), name);
c152c796
AM
12267 goto error_return;
12268 }
eea6121a 12269 if (o->size == 0)
4eca0228 12270 _bfd_error_handler
c152c796 12271 (_("warning: %s section has zero size"), name);
eea6121a 12272 dyn.d_un.d_val = o->size;
c152c796
AM
12273 break;
12274
12275 case DT_PREINIT_ARRAY:
12276 name = ".preinit_array";
4ade44b7 12277 goto get_out_vma;
c152c796
AM
12278 case DT_INIT_ARRAY:
12279 name = ".init_array";
4ade44b7 12280 goto get_out_vma;
c152c796
AM
12281 case DT_FINI_ARRAY:
12282 name = ".fini_array";
4ade44b7
AM
12283 get_out_vma:
12284 o = bfd_get_section_by_name (abfd, name);
12285 goto do_vma;
c152c796
AM
12286
12287 case DT_HASH:
12288 name = ".hash";
12289 goto get_vma;
fdc90cb4
JJ
12290 case DT_GNU_HASH:
12291 name = ".gnu.hash";
12292 goto get_vma;
c152c796
AM
12293 case DT_STRTAB:
12294 name = ".dynstr";
12295 goto get_vma;
12296 case DT_SYMTAB:
12297 name = ".dynsym";
12298 goto get_vma;
12299 case DT_VERDEF:
12300 name = ".gnu.version_d";
12301 goto get_vma;
12302 case DT_VERNEED:
12303 name = ".gnu.version_r";
12304 goto get_vma;
12305 case DT_VERSYM:
12306 name = ".gnu.version";
12307 get_vma:
4ade44b7
AM
12308 o = bfd_get_linker_section (dynobj, name);
12309 do_vma:
c152c796
AM
12310 if (o == NULL)
12311 {
4eca0228 12312 _bfd_error_handler
4ade44b7 12313 (_("could not find section %s"), name);
c152c796
AM
12314 goto error_return;
12315 }
894891db
NC
12316 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12317 {
4eca0228 12318 _bfd_error_handler
894891db
NC
12319 (_("warning: section '%s' is being made into a note"), name);
12320 bfd_set_error (bfd_error_nonrepresentable_section);
12321 goto error_return;
12322 }
4ade44b7 12323 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12324 break;
12325
12326 case DT_REL:
12327 case DT_RELA:
12328 case DT_RELSZ:
12329 case DT_RELASZ:
12330 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12331 type = SHT_REL;
12332 else
12333 type = SHT_RELA;
64487780
AM
12334 sh_size = 0;
12335 sh_addr = 0;
c152c796
AM
12336 for (i = 1; i < elf_numsections (abfd); i++)
12337 {
12338 Elf_Internal_Shdr *hdr;
12339
12340 hdr = elf_elfsections (abfd)[i];
12341 if (hdr->sh_type == type
12342 && (hdr->sh_flags & SHF_ALLOC) != 0)
12343 {
64487780
AM
12344 sh_size += hdr->sh_size;
12345 if (sh_addr == 0
12346 || sh_addr > hdr->sh_addr)
12347 sh_addr = hdr->sh_addr;
c152c796
AM
12348 }
12349 }
64487780 12350
64f52338
AM
12351 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12352 {
12353 /* Don't count procedure linkage table relocs in the
12354 overall reloc count. */
64487780
AM
12355 sh_size -= htab->srelplt->size;
12356 if (sh_size == 0)
12357 /* If the size is zero, make the address zero too.
12358 This is to avoid a glibc bug. If the backend
12359 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12360 zero, then we'll put DT_RELA at the end of
12361 DT_JMPREL. glibc will interpret the end of
12362 DT_RELA matching the end of DT_JMPREL as the
12363 case where DT_RELA includes DT_JMPREL, and for
12364 LD_BIND_NOW will decide that processing DT_RELA
12365 will process the PLT relocs too. Net result:
12366 No PLT relocs applied. */
12367 sh_addr = 0;
12368
64f52338
AM
12369 /* If .rela.plt is the first .rela section, exclude
12370 it from DT_RELA. */
64487780
AM
12371 else if (sh_addr == (htab->srelplt->output_section->vma
12372 + htab->srelplt->output_offset))
12373 sh_addr += htab->srelplt->size;
64f52338 12374 }
64487780
AM
12375
12376 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12377 dyn.d_un.d_val = sh_size;
12378 else
12379 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12380 break;
12381 }
12382 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12383 }
12384 }
12385
12386 /* If we have created any dynamic sections, then output them. */
12387 if (dynobj != NULL)
12388 {
12389 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12390 goto error_return;
12391
943284cc 12392 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12393 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12394 || info->error_textrel)
3d4d4302 12395 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12396 {
12397 bfd_byte *dyncon, *dynconend;
12398
943284cc
DJ
12399 dyncon = o->contents;
12400 dynconend = o->contents + o->size;
12401 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12402 {
12403 Elf_Internal_Dyn dyn;
12404
12405 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12406
12407 if (dyn.d_tag == DT_TEXTREL)
12408 {
c192a133
AM
12409 if (info->error_textrel)
12410 info->callbacks->einfo
12411 (_("%P%X: read-only segment has dynamic relocations.\n"));
12412 else
12413 info->callbacks->einfo
12414 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12415 break;
12416 }
12417 }
12418 }
12419
c152c796
AM
12420 for (o = dynobj->sections; o != NULL; o = o->next)
12421 {
12422 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12423 || o->size == 0
c152c796
AM
12424 || o->output_section == bfd_abs_section_ptr)
12425 continue;
12426 if ((o->flags & SEC_LINKER_CREATED) == 0)
12427 {
12428 /* At this point, we are only interested in sections
12429 created by _bfd_elf_link_create_dynamic_sections. */
12430 continue;
12431 }
64f52338 12432 if (htab->stab_info.stabstr == o)
3722b82f 12433 continue;
64f52338 12434 if (htab->eh_info.hdr_sec == o)
eea6121a 12435 continue;
3d4d4302 12436 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12437 {
12438 if (! bfd_set_section_contents (abfd, o->output_section,
12439 o->contents,
37b01f6a
DG
12440 (file_ptr) o->output_offset
12441 * bfd_octets_per_byte (abfd),
eea6121a 12442 o->size))
c152c796
AM
12443 goto error_return;
12444 }
12445 else
12446 {
12447 /* The contents of the .dynstr section are actually in a
12448 stringtab. */
8539e4e8
AM
12449 file_ptr off;
12450
c152c796
AM
12451 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12452 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12453 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12454 goto error_return;
12455 }
12456 }
12457 }
12458
0e1862bb 12459 if (bfd_link_relocatable (info))
c152c796
AM
12460 {
12461 bfd_boolean failed = FALSE;
12462
12463 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12464 if (failed)
12465 goto error_return;
12466 }
12467
12468 /* If we have optimized stabs strings, output them. */
64f52338 12469 if (htab->stab_info.stabstr != NULL)
c152c796 12470 {
64f52338 12471 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12472 goto error_return;
12473 }
12474
9f7c3e5e
AM
12475 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12476 goto error_return;
c152c796 12477
9f7c3e5e 12478 elf_final_link_free (abfd, &flinfo);
c152c796 12479
12bd6957 12480 elf_linker (abfd) = TRUE;
c152c796 12481
104d59d1
JM
12482 if (attr_section)
12483 {
a50b1753 12484 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12485 if (contents == NULL)
d0f16d5e 12486 return FALSE; /* Bail out and fail. */
104d59d1
JM
12487 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12488 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12489 free (contents);
12490 }
12491
c152c796
AM
12492 return TRUE;
12493
12494 error_return:
9f7c3e5e 12495 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12496 return FALSE;
12497}
12498\f
5241d853
RS
12499/* Initialize COOKIE for input bfd ABFD. */
12500
12501static bfd_boolean
12502init_reloc_cookie (struct elf_reloc_cookie *cookie,
12503 struct bfd_link_info *info, bfd *abfd)
12504{
12505 Elf_Internal_Shdr *symtab_hdr;
12506 const struct elf_backend_data *bed;
12507
12508 bed = get_elf_backend_data (abfd);
12509 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12510
12511 cookie->abfd = abfd;
12512 cookie->sym_hashes = elf_sym_hashes (abfd);
12513 cookie->bad_symtab = elf_bad_symtab (abfd);
12514 if (cookie->bad_symtab)
12515 {
12516 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12517 cookie->extsymoff = 0;
12518 }
12519 else
12520 {
12521 cookie->locsymcount = symtab_hdr->sh_info;
12522 cookie->extsymoff = symtab_hdr->sh_info;
12523 }
12524
12525 if (bed->s->arch_size == 32)
12526 cookie->r_sym_shift = 8;
12527 else
12528 cookie->r_sym_shift = 32;
12529
12530 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12531 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12532 {
12533 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12534 cookie->locsymcount, 0,
12535 NULL, NULL, NULL);
12536 if (cookie->locsyms == NULL)
12537 {
12538 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12539 return FALSE;
12540 }
12541 if (info->keep_memory)
12542 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12543 }
12544 return TRUE;
12545}
12546
12547/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12548
12549static void
12550fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12551{
12552 Elf_Internal_Shdr *symtab_hdr;
12553
12554 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12555 if (cookie->locsyms != NULL
12556 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12557 free (cookie->locsyms);
12558}
12559
12560/* Initialize the relocation information in COOKIE for input section SEC
12561 of input bfd ABFD. */
12562
12563static bfd_boolean
12564init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12565 struct bfd_link_info *info, bfd *abfd,
12566 asection *sec)
12567{
12568 const struct elf_backend_data *bed;
12569
12570 if (sec->reloc_count == 0)
12571 {
12572 cookie->rels = NULL;
12573 cookie->relend = NULL;
12574 }
12575 else
12576 {
12577 bed = get_elf_backend_data (abfd);
12578
12579 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12580 info->keep_memory);
12581 if (cookie->rels == NULL)
12582 return FALSE;
12583 cookie->rel = cookie->rels;
12584 cookie->relend = (cookie->rels
12585 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12586 }
12587 cookie->rel = cookie->rels;
12588 return TRUE;
12589}
12590
12591/* Free the memory allocated by init_reloc_cookie_rels,
12592 if appropriate. */
12593
12594static void
12595fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12596 asection *sec)
12597{
12598 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12599 free (cookie->rels);
12600}
12601
12602/* Initialize the whole of COOKIE for input section SEC. */
12603
12604static bfd_boolean
12605init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12606 struct bfd_link_info *info,
12607 asection *sec)
12608{
12609 if (!init_reloc_cookie (cookie, info, sec->owner))
12610 goto error1;
12611 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12612 goto error2;
12613 return TRUE;
12614
12615 error2:
12616 fini_reloc_cookie (cookie, sec->owner);
12617 error1:
12618 return FALSE;
12619}
12620
12621/* Free the memory allocated by init_reloc_cookie_for_section,
12622 if appropriate. */
12623
12624static void
12625fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12626 asection *sec)
12627{
12628 fini_reloc_cookie_rels (cookie, sec);
12629 fini_reloc_cookie (cookie, sec->owner);
12630}
12631\f
c152c796
AM
12632/* Garbage collect unused sections. */
12633
07adf181
AM
12634/* Default gc_mark_hook. */
12635
12636asection *
12637_bfd_elf_gc_mark_hook (asection *sec,
12638 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12639 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12640 struct elf_link_hash_entry *h,
12641 Elf_Internal_Sym *sym)
12642{
12643 if (h != NULL)
12644 {
12645 switch (h->root.type)
12646 {
12647 case bfd_link_hash_defined:
12648 case bfd_link_hash_defweak:
12649 return h->root.u.def.section;
12650
12651 case bfd_link_hash_common:
12652 return h->root.u.c.p->section;
12653
12654 default:
12655 break;
12656 }
12657 }
12658 else
12659 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12660
12661 return NULL;
12662}
12663
a6a4679f
AM
12664/* For undefined __start_<name> and __stop_<name> symbols, return the
12665 first input section matching <name>. Return NULL otherwise. */
12666
12667asection *
12668_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12669 struct elf_link_hash_entry *h)
12670{
12671 asection *s;
12672 const char *sec_name;
12673
12674 if (h->root.type != bfd_link_hash_undefined
12675 && h->root.type != bfd_link_hash_undefweak)
12676 return NULL;
12677
12678 s = h->root.u.undef.section;
12679 if (s != NULL)
12680 {
12681 if (s == (asection *) 0 - 1)
12682 return NULL;
12683 return s;
12684 }
12685
12686 sec_name = NULL;
12687 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12688 sec_name = h->root.root.string + 8;
12689 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12690 sec_name = h->root.root.string + 7;
12691
12692 if (sec_name != NULL && *sec_name != '\0')
12693 {
12694 bfd *i;
12695
12696 for (i = info->input_bfds; i != NULL; i = i->link.next)
12697 {
12698 s = bfd_get_section_by_name (i, sec_name);
12699 if (s != NULL)
12700 {
12701 h->root.u.undef.section = s;
12702 break;
12703 }
12704 }
12705 }
12706
12707 if (s == NULL)
12708 h->root.u.undef.section = (asection *) 0 - 1;
12709
12710 return s;
12711}
12712
5241d853
RS
12713/* COOKIE->rel describes a relocation against section SEC, which is
12714 a section we've decided to keep. Return the section that contains
12715 the relocation symbol, or NULL if no section contains it. */
12716
12717asection *
12718_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12719 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12720 struct elf_reloc_cookie *cookie,
12721 bfd_boolean *start_stop)
5241d853
RS
12722{
12723 unsigned long r_symndx;
12724 struct elf_link_hash_entry *h;
12725
12726 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12727 if (r_symndx == STN_UNDEF)
5241d853
RS
12728 return NULL;
12729
12730 if (r_symndx >= cookie->locsymcount
12731 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12732 {
12733 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12734 if (h == NULL)
12735 {
12736 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12737 sec->owner);
12738 return NULL;
12739 }
5241d853
RS
12740 while (h->root.type == bfd_link_hash_indirect
12741 || h->root.type == bfd_link_hash_warning)
12742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12743 h->mark = 1;
4e6b54a6
AM
12744 /* If this symbol is weak and there is a non-weak definition, we
12745 keep the non-weak definition because many backends put
12746 dynamic reloc info on the non-weak definition for code
12747 handling copy relocs. */
12748 if (h->u.weakdef != NULL)
12749 h->u.weakdef->mark = 1;
1cce69b9 12750
a6a4679f 12751 if (start_stop != NULL)
1cce69b9
AM
12752 {
12753 /* To work around a glibc bug, mark all XXX input sections
12754 when there is an as yet undefined reference to __start_XXX
12755 or __stop_XXX symbols. The linker will later define such
12756 symbols for orphan input sections that have a name
12757 representable as a C identifier. */
a6a4679f 12758 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12759
a6a4679f 12760 if (s != NULL)
1cce69b9 12761 {
a6a4679f
AM
12762 *start_stop = !s->gc_mark;
12763 return s;
1cce69b9
AM
12764 }
12765 }
12766
5241d853
RS
12767 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12768 }
12769
12770 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12771 &cookie->locsyms[r_symndx]);
12772}
12773
12774/* COOKIE->rel describes a relocation against section SEC, which is
12775 a section we've decided to keep. Mark the section that contains
9d0a14d3 12776 the relocation symbol. */
5241d853
RS
12777
12778bfd_boolean
12779_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12780 asection *sec,
12781 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12782 struct elf_reloc_cookie *cookie)
5241d853
RS
12783{
12784 asection *rsec;
1cce69b9 12785 bfd_boolean start_stop = FALSE;
5241d853 12786
1cce69b9
AM
12787 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12788 while (rsec != NULL)
5241d853 12789 {
1cce69b9
AM
12790 if (!rsec->gc_mark)
12791 {
12792 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12793 || (rsec->owner->flags & DYNAMIC) != 0)
12794 rsec->gc_mark = 1;
12795 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12796 return FALSE;
12797 }
12798 if (!start_stop)
12799 break;
199af150 12800 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12801 }
12802 return TRUE;
12803}
12804
07adf181
AM
12805/* The mark phase of garbage collection. For a given section, mark
12806 it and any sections in this section's group, and all the sections
12807 which define symbols to which it refers. */
12808
ccfa59ea
AM
12809bfd_boolean
12810_bfd_elf_gc_mark (struct bfd_link_info *info,
12811 asection *sec,
6a5bb875 12812 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12813{
12814 bfd_boolean ret;
9d0a14d3 12815 asection *group_sec, *eh_frame;
c152c796
AM
12816
12817 sec->gc_mark = 1;
12818
12819 /* Mark all the sections in the group. */
12820 group_sec = elf_section_data (sec)->next_in_group;
12821 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12822 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12823 return FALSE;
12824
12825 /* Look through the section relocs. */
12826 ret = TRUE;
9d0a14d3
RS
12827 eh_frame = elf_eh_frame_section (sec->owner);
12828 if ((sec->flags & SEC_RELOC) != 0
12829 && sec->reloc_count > 0
12830 && sec != eh_frame)
c152c796 12831 {
5241d853 12832 struct elf_reloc_cookie cookie;
c152c796 12833
5241d853
RS
12834 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12835 ret = FALSE;
c152c796 12836 else
c152c796 12837 {
5241d853 12838 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12839 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12840 {
12841 ret = FALSE;
12842 break;
12843 }
12844 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12845 }
12846 }
9d0a14d3
RS
12847
12848 if (ret && eh_frame && elf_fde_list (sec))
12849 {
12850 struct elf_reloc_cookie cookie;
12851
12852 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12853 ret = FALSE;
12854 else
12855 {
12856 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12857 gc_mark_hook, &cookie))
12858 ret = FALSE;
12859 fini_reloc_cookie_for_section (&cookie, eh_frame);
12860 }
12861 }
12862
2f0c68f2
CM
12863 eh_frame = elf_section_eh_frame_entry (sec);
12864 if (ret && eh_frame && !eh_frame->gc_mark)
12865 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12866 ret = FALSE;
12867
c152c796
AM
12868 return ret;
12869}
12870
3c758495
TG
12871/* Scan and mark sections in a special or debug section group. */
12872
12873static void
12874_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12875{
12876 /* Point to first section of section group. */
12877 asection *ssec;
12878 /* Used to iterate the section group. */
12879 asection *msec;
12880
12881 bfd_boolean is_special_grp = TRUE;
12882 bfd_boolean is_debug_grp = TRUE;
12883
12884 /* First scan to see if group contains any section other than debug
12885 and special section. */
12886 ssec = msec = elf_next_in_group (grp);
12887 do
12888 {
12889 if ((msec->flags & SEC_DEBUGGING) == 0)
12890 is_debug_grp = FALSE;
12891
12892 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12893 is_special_grp = FALSE;
12894
12895 msec = elf_next_in_group (msec);
12896 }
12897 while (msec != ssec);
12898
12899 /* If this is a pure debug section group or pure special section group,
12900 keep all sections in this group. */
12901 if (is_debug_grp || is_special_grp)
12902 {
12903 do
12904 {
12905 msec->gc_mark = 1;
12906 msec = elf_next_in_group (msec);
12907 }
12908 while (msec != ssec);
12909 }
12910}
12911
7f6ab9f8
AM
12912/* Keep debug and special sections. */
12913
12914bfd_boolean
12915_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12916 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12917{
12918 bfd *ibfd;
12919
c72f2fb2 12920 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12921 {
12922 asection *isec;
12923 bfd_boolean some_kept;
b40bf0a2 12924 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12925
12926 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12927 continue;
12928
b40bf0a2
NC
12929 /* Ensure all linker created sections are kept,
12930 see if any other section is already marked,
12931 and note if we have any fragmented debug sections. */
12932 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12933 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12934 {
12935 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12936 isec->gc_mark = 1;
12937 else if (isec->gc_mark)
12938 some_kept = TRUE;
b40bf0a2
NC
12939
12940 if (debug_frag_seen == FALSE
12941 && (isec->flags & SEC_DEBUGGING)
12942 && CONST_STRNEQ (isec->name, ".debug_line."))
12943 debug_frag_seen = TRUE;
7f6ab9f8
AM
12944 }
12945
12946 /* If no section in this file will be kept, then we can
b40bf0a2 12947 toss out the debug and special sections. */
7f6ab9f8
AM
12948 if (!some_kept)
12949 continue;
12950
12951 /* Keep debug and special sections like .comment when they are
3c758495
TG
12952 not part of a group. Also keep section groups that contain
12953 just debug sections or special sections. */
7f6ab9f8 12954 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12955 {
12956 if ((isec->flags & SEC_GROUP) != 0)
12957 _bfd_elf_gc_mark_debug_special_section_group (isec);
12958 else if (((isec->flags & SEC_DEBUGGING) != 0
12959 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12960 && elf_next_in_group (isec) == NULL)
12961 isec->gc_mark = 1;
12962 }
b40bf0a2
NC
12963
12964 if (! debug_frag_seen)
12965 continue;
12966
12967 /* Look for CODE sections which are going to be discarded,
12968 and find and discard any fragmented debug sections which
12969 are associated with that code section. */
12970 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12971 if ((isec->flags & SEC_CODE) != 0
12972 && isec->gc_mark == 0)
12973 {
12974 unsigned int ilen;
12975 asection *dsec;
12976
12977 ilen = strlen (isec->name);
12978
12979 /* Association is determined by the name of the debug section
12980 containing the name of the code section as a suffix. For
12981 example .debug_line.text.foo is a debug section associated
12982 with .text.foo. */
12983 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12984 {
12985 unsigned int dlen;
12986
12987 if (dsec->gc_mark == 0
12988 || (dsec->flags & SEC_DEBUGGING) == 0)
12989 continue;
12990
12991 dlen = strlen (dsec->name);
12992
12993 if (dlen > ilen
12994 && strncmp (dsec->name + (dlen - ilen),
12995 isec->name, ilen) == 0)
12996 {
12997 dsec->gc_mark = 0;
b40bf0a2
NC
12998 }
12999 }
13000 }
7f6ab9f8
AM
13001 }
13002 return TRUE;
13003}
13004
c152c796
AM
13005/* The sweep phase of garbage collection. Remove all garbage sections. */
13006
13007typedef bfd_boolean (*gc_sweep_hook_fn)
13008 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
13009
13010static bfd_boolean
ccabcbe5 13011elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13012{
13013 bfd *sub;
ccabcbe5
AM
13014 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13015 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 13016
c72f2fb2 13017 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13018 {
13019 asection *o;
13020
b19a8f85
L
13021 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13022 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13023 continue;
13024
13025 for (o = sub->sections; o != NULL; o = o->next)
13026 {
a33dafc3
L
13027 /* When any section in a section group is kept, we keep all
13028 sections in the section group. If the first member of
13029 the section group is excluded, we will also exclude the
13030 group section. */
13031 if (o->flags & SEC_GROUP)
13032 {
13033 asection *first = elf_next_in_group (o);
13034 o->gc_mark = first->gc_mark;
13035 }
c152c796 13036
1e7eae0d 13037 if (o->gc_mark)
c152c796
AM
13038 continue;
13039
13040 /* Skip sweeping sections already excluded. */
13041 if (o->flags & SEC_EXCLUDE)
13042 continue;
13043
13044 /* Since this is early in the link process, it is simple
13045 to remove a section from the output. */
13046 o->flags |= SEC_EXCLUDE;
13047
c55fe096 13048 if (info->print_gc_sections && o->size != 0)
695344c0 13049 /* xgettext:c-format */
c08bb8dd
AM
13050 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13051 o, sub);
c17d87de 13052
c152c796
AM
13053 /* But we also have to update some of the relocation
13054 info we collected before. */
13055 if (gc_sweep_hook
e8aaee2a 13056 && (o->flags & SEC_RELOC) != 0
9850436d
AM
13057 && o->reloc_count != 0
13058 && !((info->strip == strip_all || info->strip == strip_debugger)
13059 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 13060 && !bfd_is_abs_section (o->output_section))
c152c796
AM
13061 {
13062 Elf_Internal_Rela *internal_relocs;
13063 bfd_boolean r;
13064
13065 internal_relocs
13066 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13067 info->keep_memory);
13068 if (internal_relocs == NULL)
13069 return FALSE;
13070
13071 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13072
13073 if (elf_section_data (o)->relocs != internal_relocs)
13074 free (internal_relocs);
13075
13076 if (!r)
13077 return FALSE;
13078 }
13079 }
13080 }
13081
c152c796
AM
13082 return TRUE;
13083}
13084
13085/* Propagate collected vtable information. This is called through
13086 elf_link_hash_traverse. */
13087
13088static bfd_boolean
13089elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13090{
c152c796 13091 /* Those that are not vtables. */
f6e332e6 13092 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13093 return TRUE;
13094
13095 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 13096 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13097 return TRUE;
13098
13099 /* If we've already been done, exit. */
f6e332e6 13100 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
13101 return TRUE;
13102
13103 /* Make sure the parent's table is up to date. */
f6e332e6 13104 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 13105
f6e332e6 13106 if (h->vtable->used == NULL)
c152c796
AM
13107 {
13108 /* None of this table's entries were referenced. Re-use the
13109 parent's table. */
f6e332e6
AM
13110 h->vtable->used = h->vtable->parent->vtable->used;
13111 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
13112 }
13113 else
13114 {
13115 size_t n;
13116 bfd_boolean *cu, *pu;
13117
13118 /* Or the parent's entries into ours. */
f6e332e6 13119 cu = h->vtable->used;
c152c796 13120 cu[-1] = TRUE;
f6e332e6 13121 pu = h->vtable->parent->vtable->used;
c152c796
AM
13122 if (pu != NULL)
13123 {
13124 const struct elf_backend_data *bed;
13125 unsigned int log_file_align;
13126
13127 bed = get_elf_backend_data (h->root.u.def.section->owner);
13128 log_file_align = bed->s->log_file_align;
f6e332e6 13129 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
13130 while (n--)
13131 {
13132 if (*pu)
13133 *cu = TRUE;
13134 pu++;
13135 cu++;
13136 }
13137 }
13138 }
13139
13140 return TRUE;
13141}
13142
13143static bfd_boolean
13144elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13145{
13146 asection *sec;
13147 bfd_vma hstart, hend;
13148 Elf_Internal_Rela *relstart, *relend, *rel;
13149 const struct elf_backend_data *bed;
13150 unsigned int log_file_align;
13151
c152c796
AM
13152 /* Take care of both those symbols that do not describe vtables as
13153 well as those that are not loaded. */
f6e332e6 13154 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13155 return TRUE;
13156
13157 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13158 || h->root.type == bfd_link_hash_defweak);
13159
13160 sec = h->root.u.def.section;
13161 hstart = h->root.u.def.value;
13162 hend = hstart + h->size;
13163
13164 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13165 if (!relstart)
13166 return *(bfd_boolean *) okp = FALSE;
13167 bed = get_elf_backend_data (sec->owner);
13168 log_file_align = bed->s->log_file_align;
13169
13170 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13171
13172 for (rel = relstart; rel < relend; ++rel)
13173 if (rel->r_offset >= hstart && rel->r_offset < hend)
13174 {
13175 /* If the entry is in use, do nothing. */
f6e332e6
AM
13176 if (h->vtable->used
13177 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13178 {
13179 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13180 if (h->vtable->used[entry])
c152c796
AM
13181 continue;
13182 }
13183 /* Otherwise, kill it. */
13184 rel->r_offset = rel->r_info = rel->r_addend = 0;
13185 }
13186
13187 return TRUE;
13188}
13189
87538722
AM
13190/* Mark sections containing dynamically referenced symbols. When
13191 building shared libraries, we must assume that any visible symbol is
13192 referenced. */
715df9b8 13193
64d03ab5
AM
13194bfd_boolean
13195bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13196{
87538722 13197 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13198 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13199
715df9b8
EB
13200 if ((h->root.type == bfd_link_hash_defined
13201 || h->root.type == bfd_link_hash_defweak)
87538722 13202 && (h->ref_dynamic
c4621b33 13203 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13204 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13205 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13206 && (!bfd_link_executable (info)
22185505 13207 || info->gc_keep_exported
b407645f
AM
13208 || info->export_dynamic
13209 || (h->dynamic
13210 && d != NULL
13211 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13212 && (h->versioned >= versioned
54e8959c
L
13213 || !bfd_hide_sym_by_version (info->version_info,
13214 h->root.root.string)))))
715df9b8
EB
13215 h->root.u.def.section->flags |= SEC_KEEP;
13216
13217 return TRUE;
13218}
3b36f7e6 13219
74f0fb50
AM
13220/* Keep all sections containing symbols undefined on the command-line,
13221 and the section containing the entry symbol. */
13222
13223void
13224_bfd_elf_gc_keep (struct bfd_link_info *info)
13225{
13226 struct bfd_sym_chain *sym;
13227
13228 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13229 {
13230 struct elf_link_hash_entry *h;
13231
13232 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13233 FALSE, FALSE, FALSE);
13234
13235 if (h != NULL
13236 && (h->root.type == bfd_link_hash_defined
13237 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13238 && !bfd_is_abs_section (h->root.u.def.section)
13239 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13240 h->root.u.def.section->flags |= SEC_KEEP;
13241 }
13242}
13243
2f0c68f2
CM
13244bfd_boolean
13245bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13246 struct bfd_link_info *info)
13247{
13248 bfd *ibfd = info->input_bfds;
13249
13250 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13251 {
13252 asection *sec;
13253 struct elf_reloc_cookie cookie;
13254
13255 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13256 continue;
13257
13258 if (!init_reloc_cookie (&cookie, info, ibfd))
13259 return FALSE;
13260
13261 for (sec = ibfd->sections; sec; sec = sec->next)
13262 {
13263 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13264 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13265 {
13266 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13267 fini_reloc_cookie_rels (&cookie, sec);
13268 }
13269 }
13270 }
13271 return TRUE;
13272}
13273
c152c796
AM
13274/* Do mark and sweep of unused sections. */
13275
13276bfd_boolean
13277bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13278{
13279 bfd_boolean ok = TRUE;
13280 bfd *sub;
6a5bb875 13281 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13283 struct elf_link_hash_table *htab;
c152c796 13284
64d03ab5 13285 if (!bed->can_gc_sections
715df9b8 13286 || !is_elf_hash_table (info->hash))
c152c796 13287 {
4eca0228 13288 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13289 return TRUE;
13290 }
13291
74f0fb50 13292 bed->gc_keep (info);
da44f4e5 13293 htab = elf_hash_table (info);
74f0fb50 13294
9d0a14d3
RS
13295 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13296 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13297 for (sub = info->input_bfds;
13298 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13299 sub = sub->link.next)
9d0a14d3
RS
13300 {
13301 asection *sec;
13302 struct elf_reloc_cookie cookie;
13303
13304 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13305 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13306 {
13307 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13308 if (elf_section_data (sec)->sec_info
13309 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13310 elf_eh_frame_section (sub) = sec;
13311 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13312 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13313 }
13314 }
9d0a14d3 13315
c152c796 13316 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13317 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13318 if (!ok)
13319 return FALSE;
13320
13321 /* Kill the vtable relocations that were not used. */
da44f4e5 13322 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13323 if (!ok)
13324 return FALSE;
13325
715df9b8 13326 /* Mark dynamically referenced symbols. */
22185505 13327 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13328 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13329
715df9b8 13330 /* Grovel through relocs to find out who stays ... */
64d03ab5 13331 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13332 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13333 {
13334 asection *o;
13335
b19a8f85
L
13336 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13337 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13338 continue;
13339
7f6ab9f8
AM
13340 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13341 Also treat note sections as a root, if the section is not part
13342 of a group. */
c152c796 13343 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13344 if (!o->gc_mark
13345 && (o->flags & SEC_EXCLUDE) == 0
24007750 13346 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13347 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13348 && elf_next_in_group (o) == NULL )))
13349 {
13350 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13351 return FALSE;
13352 }
c152c796
AM
13353 }
13354
6a5bb875 13355 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13356 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13357
c152c796 13358 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13359 return elf_gc_sweep (abfd, info);
c152c796
AM
13360}
13361\f
13362/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13363
13364bfd_boolean
13365bfd_elf_gc_record_vtinherit (bfd *abfd,
13366 asection *sec,
13367 struct elf_link_hash_entry *h,
13368 bfd_vma offset)
13369{
13370 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13371 struct elf_link_hash_entry **search, *child;
ef53be89 13372 size_t extsymcount;
c152c796
AM
13373 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13374
13375 /* The sh_info field of the symtab header tells us where the
13376 external symbols start. We don't care about the local symbols at
13377 this point. */
13378 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13379 if (!elf_bad_symtab (abfd))
13380 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13381
13382 sym_hashes = elf_sym_hashes (abfd);
13383 sym_hashes_end = sym_hashes + extsymcount;
13384
13385 /* Hunt down the child symbol, which is in this section at the same
13386 offset as the relocation. */
13387 for (search = sym_hashes; search != sym_hashes_end; ++search)
13388 {
13389 if ((child = *search) != NULL
13390 && (child->root.type == bfd_link_hash_defined
13391 || child->root.type == bfd_link_hash_defweak)
13392 && child->root.u.def.section == sec
13393 && child->root.u.def.value == offset)
13394 goto win;
13395 }
13396
695344c0
NC
13397 /* xgettext:c-format */
13398 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13399 abfd, sec, (unsigned long) offset);
c152c796
AM
13400 bfd_set_error (bfd_error_invalid_operation);
13401 return FALSE;
13402
13403 win:
f6e332e6
AM
13404 if (!child->vtable)
13405 {
ca4be51c
AM
13406 child->vtable = ((struct elf_link_virtual_table_entry *)
13407 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13408 if (!child->vtable)
13409 return FALSE;
13410 }
c152c796
AM
13411 if (!h)
13412 {
13413 /* This *should* only be the absolute section. It could potentially
13414 be that someone has defined a non-global vtable though, which
13415 would be bad. It isn't worth paging in the local symbols to be
13416 sure though; that case should simply be handled by the assembler. */
13417
f6e332e6 13418 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13419 }
13420 else
f6e332e6 13421 child->vtable->parent = h;
c152c796
AM
13422
13423 return TRUE;
13424}
13425
13426/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13427
13428bfd_boolean
13429bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13430 asection *sec ATTRIBUTE_UNUSED,
13431 struct elf_link_hash_entry *h,
13432 bfd_vma addend)
13433{
13434 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13435 unsigned int log_file_align = bed->s->log_file_align;
13436
f6e332e6
AM
13437 if (!h->vtable)
13438 {
ca4be51c
AM
13439 h->vtable = ((struct elf_link_virtual_table_entry *)
13440 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13441 if (!h->vtable)
13442 return FALSE;
13443 }
13444
13445 if (addend >= h->vtable->size)
c152c796
AM
13446 {
13447 size_t size, bytes, file_align;
f6e332e6 13448 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13449
13450 /* While the symbol is undefined, we have to be prepared to handle
13451 a zero size. */
13452 file_align = 1 << log_file_align;
13453 if (h->root.type == bfd_link_hash_undefined)
13454 size = addend + file_align;
13455 else
13456 {
13457 size = h->size;
13458 if (addend >= size)
13459 {
13460 /* Oops! We've got a reference past the defined end of
13461 the table. This is probably a bug -- shall we warn? */
13462 size = addend + file_align;
13463 }
13464 }
13465 size = (size + file_align - 1) & -file_align;
13466
13467 /* Allocate one extra entry for use as a "done" flag for the
13468 consolidation pass. */
13469 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13470
13471 if (ptr)
13472 {
a50b1753 13473 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13474
13475 if (ptr != NULL)
13476 {
13477 size_t oldbytes;
13478
f6e332e6 13479 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13480 * sizeof (bfd_boolean));
13481 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13482 }
13483 }
13484 else
a50b1753 13485 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13486
13487 if (ptr == NULL)
13488 return FALSE;
13489
13490 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13491 h->vtable->used = ptr + 1;
13492 h->vtable->size = size;
c152c796
AM
13493 }
13494
f6e332e6 13495 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13496
13497 return TRUE;
13498}
13499
ae17ab41
CM
13500/* Map an ELF section header flag to its corresponding string. */
13501typedef struct
13502{
13503 char *flag_name;
13504 flagword flag_value;
13505} elf_flags_to_name_table;
13506
13507static elf_flags_to_name_table elf_flags_to_names [] =
13508{
13509 { "SHF_WRITE", SHF_WRITE },
13510 { "SHF_ALLOC", SHF_ALLOC },
13511 { "SHF_EXECINSTR", SHF_EXECINSTR },
13512 { "SHF_MERGE", SHF_MERGE },
13513 { "SHF_STRINGS", SHF_STRINGS },
13514 { "SHF_INFO_LINK", SHF_INFO_LINK},
13515 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13516 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13517 { "SHF_GROUP", SHF_GROUP },
13518 { "SHF_TLS", SHF_TLS },
13519 { "SHF_MASKOS", SHF_MASKOS },
13520 { "SHF_EXCLUDE", SHF_EXCLUDE },
13521};
13522
b9c361e0
JL
13523/* Returns TRUE if the section is to be included, otherwise FALSE. */
13524bfd_boolean
ae17ab41 13525bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13526 struct flag_info *flaginfo,
b9c361e0 13527 asection *section)
ae17ab41 13528{
8b127cbc 13529 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13530
8b127cbc 13531 if (!flaginfo->flags_initialized)
ae17ab41 13532 {
8b127cbc
AM
13533 bfd *obfd = info->output_bfd;
13534 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13535 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13536 int with_hex = 0;
13537 int without_hex = 0;
13538
8b127cbc 13539 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13540 {
b9c361e0 13541 unsigned i;
8b127cbc 13542 flagword (*lookup) (char *);
ae17ab41 13543
8b127cbc
AM
13544 lookup = bed->elf_backend_lookup_section_flags_hook;
13545 if (lookup != NULL)
ae17ab41 13546 {
8b127cbc 13547 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13548
13549 if (hexval != 0)
13550 {
13551 if (tf->with == with_flags)
13552 with_hex |= hexval;
13553 else if (tf->with == without_flags)
13554 without_hex |= hexval;
13555 tf->valid = TRUE;
13556 continue;
13557 }
ae17ab41 13558 }
8b127cbc 13559 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13560 {
8b127cbc 13561 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13562 {
13563 if (tf->with == with_flags)
13564 with_hex |= elf_flags_to_names[i].flag_value;
13565 else if (tf->with == without_flags)
13566 without_hex |= elf_flags_to_names[i].flag_value;
13567 tf->valid = TRUE;
13568 break;
13569 }
13570 }
8b127cbc 13571 if (!tf->valid)
b9c361e0 13572 {
68ffbac6 13573 info->callbacks->einfo
8b127cbc 13574 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13575 return FALSE;
ae17ab41
CM
13576 }
13577 }
8b127cbc
AM
13578 flaginfo->flags_initialized = TRUE;
13579 flaginfo->only_with_flags |= with_hex;
13580 flaginfo->not_with_flags |= without_hex;
ae17ab41 13581 }
ae17ab41 13582
8b127cbc 13583 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13584 return FALSE;
13585
8b127cbc 13586 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13587 return FALSE;
13588
13589 return TRUE;
ae17ab41
CM
13590}
13591
c152c796
AM
13592struct alloc_got_off_arg {
13593 bfd_vma gotoff;
10455f89 13594 struct bfd_link_info *info;
c152c796
AM
13595};
13596
13597/* We need a special top-level link routine to convert got reference counts
13598 to real got offsets. */
13599
13600static bfd_boolean
13601elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13602{
a50b1753 13603 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13604 bfd *obfd = gofarg->info->output_bfd;
13605 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13606
c152c796
AM
13607 if (h->got.refcount > 0)
13608 {
13609 h->got.offset = gofarg->gotoff;
10455f89 13610 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13611 }
13612 else
13613 h->got.offset = (bfd_vma) -1;
13614
13615 return TRUE;
13616}
13617
13618/* And an accompanying bit to work out final got entry offsets once
13619 we're done. Should be called from final_link. */
13620
13621bfd_boolean
13622bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13623 struct bfd_link_info *info)
13624{
13625 bfd *i;
13626 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13627 bfd_vma gotoff;
c152c796
AM
13628 struct alloc_got_off_arg gofarg;
13629
10455f89
HPN
13630 BFD_ASSERT (abfd == info->output_bfd);
13631
c152c796
AM
13632 if (! is_elf_hash_table (info->hash))
13633 return FALSE;
13634
13635 /* The GOT offset is relative to the .got section, but the GOT header is
13636 put into the .got.plt section, if the backend uses it. */
13637 if (bed->want_got_plt)
13638 gotoff = 0;
13639 else
13640 gotoff = bed->got_header_size;
13641
13642 /* Do the local .got entries first. */
c72f2fb2 13643 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13644 {
13645 bfd_signed_vma *local_got;
ef53be89 13646 size_t j, locsymcount;
c152c796
AM
13647 Elf_Internal_Shdr *symtab_hdr;
13648
13649 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13650 continue;
13651
13652 local_got = elf_local_got_refcounts (i);
13653 if (!local_got)
13654 continue;
13655
13656 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13657 if (elf_bad_symtab (i))
13658 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13659 else
13660 locsymcount = symtab_hdr->sh_info;
13661
13662 for (j = 0; j < locsymcount; ++j)
13663 {
13664 if (local_got[j] > 0)
13665 {
13666 local_got[j] = gotoff;
10455f89 13667 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13668 }
13669 else
13670 local_got[j] = (bfd_vma) -1;
13671 }
13672 }
13673
13674 /* Then the global .got entries. .plt refcounts are handled by
13675 adjust_dynamic_symbol */
13676 gofarg.gotoff = gotoff;
10455f89 13677 gofarg.info = info;
c152c796
AM
13678 elf_link_hash_traverse (elf_hash_table (info),
13679 elf_gc_allocate_got_offsets,
13680 &gofarg);
13681 return TRUE;
13682}
13683
13684/* Many folk need no more in the way of final link than this, once
13685 got entry reference counting is enabled. */
13686
13687bfd_boolean
13688bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13689{
13690 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13691 return FALSE;
13692
13693 /* Invoke the regular ELF backend linker to do all the work. */
13694 return bfd_elf_final_link (abfd, info);
13695}
13696
13697bfd_boolean
13698bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13699{
a50b1753 13700 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13701
13702 if (rcookie->bad_symtab)
13703 rcookie->rel = rcookie->rels;
13704
13705 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13706 {
13707 unsigned long r_symndx;
13708
13709 if (! rcookie->bad_symtab)
13710 if (rcookie->rel->r_offset > offset)
13711 return FALSE;
13712 if (rcookie->rel->r_offset != offset)
13713 continue;
13714
13715 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13716 if (r_symndx == STN_UNDEF)
c152c796
AM
13717 return TRUE;
13718
13719 if (r_symndx >= rcookie->locsymcount
13720 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13721 {
13722 struct elf_link_hash_entry *h;
13723
13724 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13725
13726 while (h->root.type == bfd_link_hash_indirect
13727 || h->root.type == bfd_link_hash_warning)
13728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13729
13730 if ((h->root.type == bfd_link_hash_defined
13731 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13732 && (h->root.u.def.section->owner != rcookie->abfd
13733 || h->root.u.def.section->kept_section != NULL
13734 || discarded_section (h->root.u.def.section)))
c152c796 13735 return TRUE;
c152c796
AM
13736 }
13737 else
13738 {
13739 /* It's not a relocation against a global symbol,
13740 but it could be a relocation against a local
13741 symbol for a discarded section. */
13742 asection *isec;
13743 Elf_Internal_Sym *isym;
13744
13745 /* Need to: get the symbol; get the section. */
13746 isym = &rcookie->locsyms[r_symndx];
cb33740c 13747 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13748 if (isec != NULL
13749 && (isec->kept_section != NULL
13750 || discarded_section (isec)))
cb33740c 13751 return TRUE;
c152c796
AM
13752 }
13753 return FALSE;
13754 }
13755 return FALSE;
13756}
13757
13758/* Discard unneeded references to discarded sections.
75938853
AM
13759 Returns -1 on error, 1 if any section's size was changed, 0 if
13760 nothing changed. This function assumes that the relocations are in
13761 sorted order, which is true for all known assemblers. */
c152c796 13762
75938853 13763int
c152c796
AM
13764bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13765{
13766 struct elf_reloc_cookie cookie;
18cd5bce 13767 asection *o;
c152c796 13768 bfd *abfd;
75938853 13769 int changed = 0;
c152c796
AM
13770
13771 if (info->traditional_format
13772 || !is_elf_hash_table (info->hash))
75938853 13773 return 0;
c152c796 13774
18cd5bce
AM
13775 o = bfd_get_section_by_name (output_bfd, ".stab");
13776 if (o != NULL)
c152c796 13777 {
18cd5bce 13778 asection *i;
c152c796 13779
18cd5bce 13780 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13781 {
18cd5bce
AM
13782 if (i->size == 0
13783 || i->reloc_count == 0
13784 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13785 continue;
c152c796 13786
18cd5bce
AM
13787 abfd = i->owner;
13788 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13789 continue;
c152c796 13790
18cd5bce 13791 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13792 return -1;
c152c796 13793
18cd5bce
AM
13794 if (_bfd_discard_section_stabs (abfd, i,
13795 elf_section_data (i)->sec_info,
5241d853
RS
13796 bfd_elf_reloc_symbol_deleted_p,
13797 &cookie))
75938853 13798 changed = 1;
18cd5bce
AM
13799
13800 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13801 }
18cd5bce
AM
13802 }
13803
2f0c68f2
CM
13804 o = NULL;
13805 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13806 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13807 if (o != NULL)
13808 {
13809 asection *i;
d7153c4a 13810 int eh_changed = 0;
c152c796 13811
18cd5bce 13812 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13813 {
18cd5bce
AM
13814 if (i->size == 0)
13815 continue;
13816
13817 abfd = i->owner;
13818 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13819 continue;
13820
13821 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13822 return -1;
18cd5bce
AM
13823
13824 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13825 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13826 bfd_elf_reloc_symbol_deleted_p,
13827 &cookie))
d7153c4a
AM
13828 {
13829 eh_changed = 1;
13830 if (i->size != i->rawsize)
13831 changed = 1;
13832 }
18cd5bce
AM
13833
13834 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13835 }
d7153c4a
AM
13836 if (eh_changed)
13837 elf_link_hash_traverse (elf_hash_table (info),
13838 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13839 }
c152c796 13840
18cd5bce
AM
13841 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13842 {
13843 const struct elf_backend_data *bed;
c152c796 13844
18cd5bce
AM
13845 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13846 continue;
13847
13848 bed = get_elf_backend_data (abfd);
13849
13850 if (bed->elf_backend_discard_info != NULL)
13851 {
13852 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13853 return -1;
18cd5bce
AM
13854
13855 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13856 changed = 1;
18cd5bce
AM
13857
13858 fini_reloc_cookie (&cookie, abfd);
13859 }
c152c796
AM
13860 }
13861
2f0c68f2
CM
13862 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13863 _bfd_elf_end_eh_frame_parsing (info);
13864
13865 if (info->eh_frame_hdr_type
0e1862bb 13866 && !bfd_link_relocatable (info)
c152c796 13867 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13868 changed = 1;
c152c796 13869
75938853 13870 return changed;
c152c796 13871}
082b7297 13872
43e1669b 13873bfd_boolean
0c511000 13874_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13875 asection *sec,
c0f00686 13876 struct bfd_link_info *info)
082b7297
L
13877{
13878 flagword flags;
c77ec726 13879 const char *name, *key;
082b7297
L
13880 struct bfd_section_already_linked *l;
13881 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13882
c77ec726
AM
13883 if (sec->output_section == bfd_abs_section_ptr)
13884 return FALSE;
0c511000 13885
c77ec726 13886 flags = sec->flags;
0c511000 13887
c77ec726
AM
13888 /* Return if it isn't a linkonce section. A comdat group section
13889 also has SEC_LINK_ONCE set. */
13890 if ((flags & SEC_LINK_ONCE) == 0)
13891 return FALSE;
0c511000 13892
c77ec726
AM
13893 /* Don't put group member sections on our list of already linked
13894 sections. They are handled as a group via their group section. */
13895 if (elf_sec_group (sec) != NULL)
13896 return FALSE;
0c511000 13897
c77ec726
AM
13898 /* For a SHT_GROUP section, use the group signature as the key. */
13899 name = sec->name;
13900 if ((flags & SEC_GROUP) != 0
13901 && elf_next_in_group (sec) != NULL
13902 && elf_group_name (elf_next_in_group (sec)) != NULL)
13903 key = elf_group_name (elf_next_in_group (sec));
13904 else
13905 {
13906 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13907 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13908 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13909 key++;
0c511000 13910 else
c77ec726
AM
13911 /* Must be a user linkonce section that doesn't follow gcc's
13912 naming convention. In this case we won't be matching
13913 single member groups. */
13914 key = name;
0c511000 13915 }
6d2cd210 13916
c77ec726 13917 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13918
13919 for (l = already_linked_list->entry; l != NULL; l = l->next)
13920 {
c2370991 13921 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13922 sections with a signature of <key> (<key> is some string),
13923 and linkonce sections named .gnu.linkonce.<type>.<key>.
13924 Match like sections. LTO plugin sections are an exception.
13925 They are always named .gnu.linkonce.t.<key> and match either
13926 type of section. */
13927 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13928 && ((flags & SEC_GROUP) != 0
13929 || strcmp (name, l->sec->name) == 0))
13930 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13931 {
13932 /* The section has already been linked. See if we should
6d2cd210 13933 issue a warning. */
c77ec726
AM
13934 if (!_bfd_handle_already_linked (sec, l, info))
13935 return FALSE;
082b7297 13936
c77ec726 13937 if (flags & SEC_GROUP)
3d7f7666 13938 {
c77ec726
AM
13939 asection *first = elf_next_in_group (sec);
13940 asection *s = first;
3d7f7666 13941
c77ec726 13942 while (s != NULL)
3d7f7666 13943 {
c77ec726
AM
13944 s->output_section = bfd_abs_section_ptr;
13945 /* Record which group discards it. */
13946 s->kept_section = l->sec;
13947 s = elf_next_in_group (s);
13948 /* These lists are circular. */
13949 if (s == first)
13950 break;
3d7f7666
L
13951 }
13952 }
082b7297 13953
43e1669b 13954 return TRUE;
082b7297
L
13955 }
13956 }
13957
c77ec726
AM
13958 /* A single member comdat group section may be discarded by a
13959 linkonce section and vice versa. */
13960 if ((flags & SEC_GROUP) != 0)
3d7f7666 13961 {
c77ec726 13962 asection *first = elf_next_in_group (sec);
c2370991 13963
c77ec726
AM
13964 if (first != NULL && elf_next_in_group (first) == first)
13965 /* Check this single member group against linkonce sections. */
13966 for (l = already_linked_list->entry; l != NULL; l = l->next)
13967 if ((l->sec->flags & SEC_GROUP) == 0
13968 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13969 {
13970 first->output_section = bfd_abs_section_ptr;
13971 first->kept_section = l->sec;
13972 sec->output_section = bfd_abs_section_ptr;
13973 break;
13974 }
13975 }
13976 else
13977 /* Check this linkonce section against single member groups. */
13978 for (l = already_linked_list->entry; l != NULL; l = l->next)
13979 if (l->sec->flags & SEC_GROUP)
6d2cd210 13980 {
c77ec726 13981 asection *first = elf_next_in_group (l->sec);
6d2cd210 13982
c77ec726
AM
13983 if (first != NULL
13984 && elf_next_in_group (first) == first
13985 && bfd_elf_match_symbols_in_sections (first, sec, info))
13986 {
13987 sec->output_section = bfd_abs_section_ptr;
13988 sec->kept_section = first;
13989 break;
13990 }
6d2cd210 13991 }
0c511000 13992
c77ec726
AM
13993 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13994 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13995 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13996 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13997 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13998 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13999 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14000 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14001 The reverse order cannot happen as there is never a bfd with only the
14002 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14003 matter as here were are looking only for cross-bfd sections. */
14004
14005 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14006 for (l = already_linked_list->entry; l != NULL; l = l->next)
14007 if ((l->sec->flags & SEC_GROUP) == 0
14008 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14009 {
14010 if (abfd != l->sec->owner)
14011 sec->output_section = bfd_abs_section_ptr;
14012 break;
14013 }
80c29487 14014
082b7297 14015 /* This is the first section with this name. Record it. */
c77ec726 14016 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14017 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14018 return sec->output_section == bfd_abs_section_ptr;
082b7297 14019}
81e1b023 14020
a4d8e49b
L
14021bfd_boolean
14022_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14023{
14024 return sym->st_shndx == SHN_COMMON;
14025}
14026
14027unsigned int
14028_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14029{
14030 return SHN_COMMON;
14031}
14032
14033asection *
14034_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14035{
14036 return bfd_com_section_ptr;
14037}
10455f89
HPN
14038
14039bfd_vma
14040_bfd_elf_default_got_elt_size (bfd *abfd,
14041 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14042 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14043 bfd *ibfd ATTRIBUTE_UNUSED,
14044 unsigned long symndx ATTRIBUTE_UNUSED)
14045{
14046 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14047 return bed->s->arch_size / 8;
14048}
83bac4b0
NC
14049
14050/* Routines to support the creation of dynamic relocs. */
14051
83bac4b0
NC
14052/* Returns the name of the dynamic reloc section associated with SEC. */
14053
14054static const char *
14055get_dynamic_reloc_section_name (bfd * abfd,
14056 asection * sec,
14057 bfd_boolean is_rela)
14058{
ddcf1fcf
BS
14059 char *name;
14060 const char *old_name = bfd_get_section_name (NULL, sec);
14061 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14062
ddcf1fcf 14063 if (old_name == NULL)
83bac4b0
NC
14064 return NULL;
14065
ddcf1fcf 14066 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14067 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14068
14069 return name;
14070}
14071
14072/* Returns the dynamic reloc section associated with SEC.
14073 If necessary compute the name of the dynamic reloc section based
14074 on SEC's name (looked up in ABFD's string table) and the setting
14075 of IS_RELA. */
14076
14077asection *
14078_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14079 asection * sec,
14080 bfd_boolean is_rela)
14081{
14082 asection * reloc_sec = elf_section_data (sec)->sreloc;
14083
14084 if (reloc_sec == NULL)
14085 {
14086 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14087
14088 if (name != NULL)
14089 {
3d4d4302 14090 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14091
14092 if (reloc_sec != NULL)
14093 elf_section_data (sec)->sreloc = reloc_sec;
14094 }
14095 }
14096
14097 return reloc_sec;
14098}
14099
14100/* Returns the dynamic reloc section associated with SEC. If the
14101 section does not exist it is created and attached to the DYNOBJ
14102 bfd and stored in the SRELOC field of SEC's elf_section_data
14103 structure.
f8076f98 14104
83bac4b0
NC
14105 ALIGNMENT is the alignment for the newly created section and
14106 IS_RELA defines whether the name should be .rela.<SEC's name>
14107 or .rel.<SEC's name>. The section name is looked up in the
14108 string table associated with ABFD. */
14109
14110asection *
ca4be51c
AM
14111_bfd_elf_make_dynamic_reloc_section (asection *sec,
14112 bfd *dynobj,
14113 unsigned int alignment,
14114 bfd *abfd,
14115 bfd_boolean is_rela)
83bac4b0
NC
14116{
14117 asection * reloc_sec = elf_section_data (sec)->sreloc;
14118
14119 if (reloc_sec == NULL)
14120 {
14121 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14122
14123 if (name == NULL)
14124 return NULL;
14125
3d4d4302 14126 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14127
14128 if (reloc_sec == NULL)
14129 {
3d4d4302
AM
14130 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14131 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14132 if ((sec->flags & SEC_ALLOC) != 0)
14133 flags |= SEC_ALLOC | SEC_LOAD;
14134
3d4d4302 14135 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14136 if (reloc_sec != NULL)
14137 {
8877b5e5
AM
14138 /* _bfd_elf_get_sec_type_attr chooses a section type by
14139 name. Override as it may be wrong, eg. for a user
14140 section named "auto" we'll get ".relauto" which is
14141 seen to be a .rela section. */
14142 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14143 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14144 reloc_sec = NULL;
14145 }
14146 }
14147
14148 elf_section_data (sec)->sreloc = reloc_sec;
14149 }
14150
14151 return reloc_sec;
14152}
1338dd10 14153
bffebb6b
AM
14154/* Copy the ELF symbol type and other attributes for a linker script
14155 assignment from HSRC to HDEST. Generally this should be treated as
14156 if we found a strong non-dynamic definition for HDEST (except that
14157 ld ignores multiple definition errors). */
1338dd10 14158void
bffebb6b
AM
14159_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14160 struct bfd_link_hash_entry *hdest,
14161 struct bfd_link_hash_entry *hsrc)
1338dd10 14162{
bffebb6b
AM
14163 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14164 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14165 Elf_Internal_Sym isym;
1338dd10
PB
14166
14167 ehdest->type = ehsrc->type;
35fc36a8 14168 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14169
14170 isym.st_other = ehsrc->other;
b8417128 14171 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14172}
351f65ca
L
14173
14174/* Append a RELA relocation REL to section S in BFD. */
14175
14176void
14177elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14178{
14179 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14180 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14181 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14182 bed->s->swap_reloca_out (abfd, rel, loc);
14183}
14184
14185/* Append a REL relocation REL to section S in BFD. */
14186
14187void
14188elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14189{
14190 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14191 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14192 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14193 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14194}
This page took 2.218795 seconds and 4 git commands to generate.