]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
pr20882 testcase
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
222 if ((ibfd->flags
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
224 {
225 abfd = ibfd;
226 break;
227 }
228 }
229 hash_table->dynobj = abfd;
230 }
7e9f0867
AM
231
232 if (hash_table->dynstr == NULL)
233 {
234 hash_table->dynstr = _bfd_elf_strtab_init ();
235 if (hash_table->dynstr == NULL)
236 return FALSE;
237 }
238 return TRUE;
239}
240
45d6a902
AM
241/* Create some sections which will be filled in with dynamic linking
242 information. ABFD is an input file which requires dynamic sections
243 to be created. The dynamic sections take up virtual memory space
244 when the final executable is run, so we need to create them before
245 addresses are assigned to the output sections. We work out the
246 actual contents and size of these sections later. */
252b5132 247
b34976b6 248bfd_boolean
268b6b39 249_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 250{
45d6a902 251 flagword flags;
91d6fa6a 252 asection *s;
9c5bfbb7 253 const struct elf_backend_data *bed;
9637f6ef 254 struct elf_link_hash_entry *h;
252b5132 255
0eddce27 256 if (! is_elf_hash_table (info->hash))
45d6a902
AM
257 return FALSE;
258
259 if (elf_hash_table (info)->dynamic_sections_created)
260 return TRUE;
261
7e9f0867
AM
262 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
263 return FALSE;
45d6a902 264
7e9f0867 265 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
266 bed = get_elf_backend_data (abfd);
267
268 flags = bed->dynamic_sec_flags;
45d6a902
AM
269
270 /* A dynamically linked executable has a .interp section, but a
271 shared library does not. */
9b8b325a 272 if (bfd_link_executable (info) && !info->nointerp)
252b5132 273 {
14b2f831
AM
274 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
275 flags | SEC_READONLY);
3496cb2a 276 if (s == NULL)
45d6a902
AM
277 return FALSE;
278 }
bb0deeff 279
45d6a902
AM
280 /* Create sections to hold version informations. These are removed
281 if they are not needed. */
14b2f831
AM
282 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
283 flags | SEC_READONLY);
45d6a902 284 if (s == NULL
45d6a902
AM
285 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
286 return FALSE;
287
14b2f831
AM
288 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
289 flags | SEC_READONLY);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, 1))
292 return FALSE;
293
14b2f831
AM
294 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
295 flags | SEC_READONLY);
45d6a902 296 if (s == NULL
45d6a902
AM
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 return FALSE;
299
14b2f831
AM
300 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
301 flags | SEC_READONLY);
45d6a902 302 if (s == NULL
45d6a902
AM
303 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 return FALSE;
cae1fbbb 305 elf_hash_table (info)->dynsym = s;
45d6a902 306
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
308 flags | SEC_READONLY);
3496cb2a 309 if (s == NULL)
45d6a902
AM
310 return FALSE;
311
14b2f831 312 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 313 if (s == NULL
45d6a902
AM
314 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
315 return FALSE;
316
317 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
318 .dynamic section. We could set _DYNAMIC in a linker script, but we
319 only want to define it if we are, in fact, creating a .dynamic
320 section. We don't want to define it if there is no .dynamic
321 section, since on some ELF platforms the start up code examines it
322 to decide how to initialize the process. */
9637f6ef
L
323 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
324 elf_hash_table (info)->hdynamic = h;
325 if (h == NULL)
45d6a902
AM
326 return FALSE;
327
fdc90cb4
JJ
328 if (info->emit_hash)
329 {
14b2f831
AM
330 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
331 flags | SEC_READONLY);
fdc90cb4
JJ
332 if (s == NULL
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
335 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
336 }
337
338 if (info->emit_gnu_hash)
339 {
14b2f831
AM
340 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
341 flags | SEC_READONLY);
fdc90cb4
JJ
342 if (s == NULL
343 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
344 return FALSE;
345 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
346 4 32-bit words followed by variable count of 64-bit words, then
347 variable count of 32-bit words. */
348 if (bed->s->arch_size == 64)
349 elf_section_data (s)->this_hdr.sh_entsize = 0;
350 else
351 elf_section_data (s)->this_hdr.sh_entsize = 4;
352 }
45d6a902
AM
353
354 /* Let the backend create the rest of the sections. This lets the
355 backend set the right flags. The backend will normally create
356 the .got and .plt sections. */
894891db
NC
357 if (bed->elf_backend_create_dynamic_sections == NULL
358 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
359 return FALSE;
360
361 elf_hash_table (info)->dynamic_sections_created = TRUE;
362
363 return TRUE;
364}
365
366/* Create dynamic sections when linking against a dynamic object. */
367
368bfd_boolean
268b6b39 369_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
370{
371 flagword flags, pltflags;
7325306f 372 struct elf_link_hash_entry *h;
45d6a902 373 asection *s;
9c5bfbb7 374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 375 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 376
252b5132
RH
377 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
378 .rel[a].bss sections. */
e5a52504 379 flags = bed->dynamic_sec_flags;
252b5132
RH
380
381 pltflags = flags;
252b5132 382 if (bed->plt_not_loaded)
6df4d94c
MM
383 /* We do not clear SEC_ALLOC here because we still want the OS to
384 allocate space for the section; it's just that there's nothing
385 to read in from the object file. */
5d1634d7 386 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
387 else
388 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
389 if (bed->plt_readonly)
390 pltflags |= SEC_READONLY;
391
14b2f831 392 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 393 if (s == NULL
252b5132 394 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 395 return FALSE;
6de2ae4a 396 htab->splt = s;
252b5132 397
d98685ac
AM
398 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
399 .plt section. */
7325306f
RS
400 if (bed->want_plt_sym)
401 {
402 h = _bfd_elf_define_linkage_sym (abfd, info, s,
403 "_PROCEDURE_LINKAGE_TABLE_");
404 elf_hash_table (info)->hplt = h;
405 if (h == NULL)
406 return FALSE;
407 }
252b5132 408
14b2f831
AM
409 s = bfd_make_section_anyway_with_flags (abfd,
410 (bed->rela_plts_and_copies_p
411 ? ".rela.plt" : ".rel.plt"),
412 flags | SEC_READONLY);
252b5132 413 if (s == NULL
45d6a902 414 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 415 return FALSE;
6de2ae4a 416 htab->srelplt = s;
252b5132
RH
417
418 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 419 return FALSE;
252b5132 420
3018b441
RH
421 if (bed->want_dynbss)
422 {
423 /* The .dynbss section is a place to put symbols which are defined
424 by dynamic objects, are referenced by regular objects, and are
425 not functions. We must allocate space for them in the process
426 image and use a R_*_COPY reloc to tell the dynamic linker to
427 initialize them at run time. The linker script puts the .dynbss
428 section into the .bss section of the final image. */
14b2f831 429 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 430 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 431 if (s == NULL)
b34976b6 432 return FALSE;
9d19e4fd 433 htab->sdynbss = s;
252b5132 434
5474d94f
AM
435 if (bed->want_dynrelro)
436 {
437 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
438 sections. This section doesn't really need to have contents,
439 but make it like other .data.rel.ro sections. */
5474d94f 440 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 441 flags);
5474d94f
AM
442 if (s == NULL)
443 return FALSE;
444 htab->sdynrelro = s;
445 }
446
3018b441 447 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
448 normally needed. We need to create it here, though, so that the
449 linker will map it to an output section. We can't just create it
450 only if we need it, because we will not know whether we need it
451 until we have seen all the input files, and the first time the
452 main linker code calls BFD after examining all the input files
453 (size_dynamic_sections) the input sections have already been
454 mapped to the output sections. If the section turns out not to
455 be needed, we can discard it later. We will never need this
456 section when generating a shared object, since they do not use
457 copy relocs. */
9d19e4fd 458 if (bfd_link_executable (info))
3018b441 459 {
14b2f831
AM
460 s = bfd_make_section_anyway_with_flags (abfd,
461 (bed->rela_plts_and_copies_p
462 ? ".rela.bss" : ".rel.bss"),
463 flags | SEC_READONLY);
3018b441 464 if (s == NULL
45d6a902 465 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 466 return FALSE;
9d19e4fd 467 htab->srelbss = s;
5474d94f
AM
468
469 if (bed->want_dynrelro)
470 {
471 s = (bfd_make_section_anyway_with_flags
472 (abfd, (bed->rela_plts_and_copies_p
473 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
474 flags | SEC_READONLY));
475 if (s == NULL
476 || ! bfd_set_section_alignment (abfd, s,
477 bed->s->log_file_align))
478 return FALSE;
479 htab->sreldynrelro = s;
480 }
3018b441 481 }
252b5132
RH
482 }
483
b34976b6 484 return TRUE;
252b5132
RH
485}
486\f
252b5132
RH
487/* Record a new dynamic symbol. We record the dynamic symbols as we
488 read the input files, since we need to have a list of all of them
489 before we can determine the final sizes of the output sections.
490 Note that we may actually call this function even though we are not
491 going to output any dynamic symbols; in some cases we know that a
492 symbol should be in the dynamic symbol table, but only if there is
493 one. */
494
b34976b6 495bfd_boolean
c152c796
AM
496bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
497 struct elf_link_hash_entry *h)
252b5132
RH
498{
499 if (h->dynindx == -1)
500 {
2b0f7ef9 501 struct elf_strtab_hash *dynstr;
68b6ddd0 502 char *p;
252b5132 503 const char *name;
ef53be89 504 size_t indx;
252b5132 505
7a13edea
NC
506 /* XXX: The ABI draft says the linker must turn hidden and
507 internal symbols into STB_LOCAL symbols when producing the
508 DSO. However, if ld.so honors st_other in the dynamic table,
509 this would not be necessary. */
510 switch (ELF_ST_VISIBILITY (h->other))
511 {
512 case STV_INTERNAL:
513 case STV_HIDDEN:
9d6eee78
L
514 if (h->root.type != bfd_link_hash_undefined
515 && h->root.type != bfd_link_hash_undefweak)
38048eb9 516 {
f5385ebf 517 h->forced_local = 1;
67687978
PB
518 if (!elf_hash_table (info)->is_relocatable_executable)
519 return TRUE;
7a13edea 520 }
0444bdd4 521
7a13edea
NC
522 default:
523 break;
524 }
525
252b5132
RH
526 h->dynindx = elf_hash_table (info)->dynsymcount;
527 ++elf_hash_table (info)->dynsymcount;
528
529 dynstr = elf_hash_table (info)->dynstr;
530 if (dynstr == NULL)
531 {
532 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 533 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 534 if (dynstr == NULL)
b34976b6 535 return FALSE;
252b5132
RH
536 }
537
538 /* We don't put any version information in the dynamic string
aad5d350 539 table. */
252b5132
RH
540 name = h->root.root.string;
541 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
542 if (p != NULL)
543 /* We know that the p points into writable memory. In fact,
544 there are only a few symbols that have read-only names, being
545 those like _GLOBAL_OFFSET_TABLE_ that are created specially
546 by the backends. Most symbols will have names pointing into
547 an ELF string table read from a file, or to objalloc memory. */
548 *p = 0;
549
550 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
551
552 if (p != NULL)
553 *p = ELF_VER_CHR;
252b5132 554
ef53be89 555 if (indx == (size_t) -1)
b34976b6 556 return FALSE;
252b5132
RH
557 h->dynstr_index = indx;
558 }
559
b34976b6 560 return TRUE;
252b5132 561}
45d6a902 562\f
55255dae
L
563/* Mark a symbol dynamic. */
564
28caa186 565static void
55255dae 566bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
567 struct elf_link_hash_entry *h,
568 Elf_Internal_Sym *sym)
55255dae 569{
40b36307 570 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 571
40b36307 572 /* It may be called more than once on the same H. */
0e1862bb 573 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
574 return;
575
40b36307
L
576 if ((info->dynamic_data
577 && (h->type == STT_OBJECT
b8871f35 578 || h->type == STT_COMMON
40b36307 579 || (sym != NULL
b8871f35
L
580 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
581 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 582 || (d != NULL
73ec947d 583 && h->non_elf
40b36307 584 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
585 h->dynamic = 1;
586}
587
45d6a902
AM
588/* Record an assignment to a symbol made by a linker script. We need
589 this in case some dynamic object refers to this symbol. */
590
591bfd_boolean
fe21a8fc
L
592bfd_elf_record_link_assignment (bfd *output_bfd,
593 struct bfd_link_info *info,
268b6b39 594 const char *name,
fe21a8fc
L
595 bfd_boolean provide,
596 bfd_boolean hidden)
45d6a902 597{
00cbee0a 598 struct elf_link_hash_entry *h, *hv;
4ea42fb7 599 struct elf_link_hash_table *htab;
00cbee0a 600 const struct elf_backend_data *bed;
45d6a902 601
0eddce27 602 if (!is_elf_hash_table (info->hash))
45d6a902
AM
603 return TRUE;
604
4ea42fb7
AM
605 htab = elf_hash_table (info);
606 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 607 if (h == NULL)
4ea42fb7 608 return provide;
45d6a902 609
8e2a4f11
AM
610 if (h->root.type == bfd_link_hash_warning)
611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
612
0f550b3d
L
613 if (h->versioned == unknown)
614 {
615 /* Set versioned if symbol version is unknown. */
616 char *version = strrchr (name, ELF_VER_CHR);
617 if (version)
618 {
619 if (version > name && version[-1] != ELF_VER_CHR)
620 h->versioned = versioned_hidden;
621 else
622 h->versioned = versioned;
623 }
624 }
625
73ec947d
AM
626 /* Symbols defined in a linker script but not referenced anywhere
627 else will have non_elf set. */
628 if (h->non_elf)
629 {
630 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
631 h->non_elf = 0;
632 }
633
00cbee0a 634 switch (h->root.type)
77cfaee6 635 {
00cbee0a
L
636 case bfd_link_hash_defined:
637 case bfd_link_hash_defweak:
638 case bfd_link_hash_common:
639 break;
640 case bfd_link_hash_undefweak:
641 case bfd_link_hash_undefined:
642 /* Since we're defining the symbol, don't let it seem to have not
643 been defined. record_dynamic_symbol and size_dynamic_sections
644 may depend on this. */
4ea42fb7 645 h->root.type = bfd_link_hash_new;
77cfaee6
AM
646 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
647 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
648 break;
649 case bfd_link_hash_new:
00cbee0a
L
650 break;
651 case bfd_link_hash_indirect:
652 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 653 the versioned symbol point to this one. */
00cbee0a
L
654 bed = get_elf_backend_data (output_bfd);
655 hv = h;
656 while (hv->root.type == bfd_link_hash_indirect
657 || hv->root.type == bfd_link_hash_warning)
658 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
659 /* We don't need to update h->root.u since linker will set them
660 later. */
661 h->root.type = bfd_link_hash_undefined;
662 hv->root.type = bfd_link_hash_indirect;
663 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
664 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
665 break;
8e2a4f11
AM
666 default:
667 BFD_FAIL ();
c2596ca5 668 return FALSE;
55255dae 669 }
45d6a902
AM
670
671 /* If this symbol is being provided by the linker script, and it is
672 currently defined by a dynamic object, but not by a regular
673 object, then mark it as undefined so that the generic linker will
674 force the correct value. */
675 if (provide
f5385ebf
AM
676 && h->def_dynamic
677 && !h->def_regular)
45d6a902
AM
678 h->root.type = bfd_link_hash_undefined;
679
680 /* If this symbol is not being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
682 then clear out any version information because the symbol will not be
683 associated with the dynamic object any more. */
45d6a902 684 if (!provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
b531344c
MR
687 h->verinfo.verdef = NULL;
688
689 /* Make sure this symbol is not garbage collected. */
690 h->mark = 1;
45d6a902 691
f5385ebf 692 h->def_regular = 1;
45d6a902 693
eb8476a6 694 if (hidden)
fe21a8fc 695 {
91d6fa6a 696 bed = get_elf_backend_data (output_bfd);
b8297068
AM
697 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
698 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
699 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
700 }
701
6fa3860b
PB
702 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
703 and executables. */
0e1862bb 704 if (!bfd_link_relocatable (info)
6fa3860b
PB
705 && h->dynindx != -1
706 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
707 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
708 h->forced_local = 1;
709
f5385ebf
AM
710 if ((h->def_dynamic
711 || h->ref_dynamic
6b3b0ab8
L
712 || bfd_link_dll (info)
713 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
714 && h->dynindx == -1)
715 {
c152c796 716 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
717 return FALSE;
718
719 /* If this is a weak defined symbol, and we know a corresponding
720 real symbol from the same dynamic object, make sure the real
721 symbol is also made into a dynamic symbol. */
f6e332e6
AM
722 if (h->u.weakdef != NULL
723 && h->u.weakdef->dynindx == -1)
45d6a902 724 {
f6e332e6 725 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
726 return FALSE;
727 }
728 }
729
730 return TRUE;
731}
42751cf3 732
8c58d23b
AM
733/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
734 success, and 2 on a failure caused by attempting to record a symbol
735 in a discarded section, eg. a discarded link-once section symbol. */
736
737int
c152c796
AM
738bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
739 bfd *input_bfd,
740 long input_indx)
8c58d23b
AM
741{
742 bfd_size_type amt;
743 struct elf_link_local_dynamic_entry *entry;
744 struct elf_link_hash_table *eht;
745 struct elf_strtab_hash *dynstr;
ef53be89 746 size_t dynstr_index;
8c58d23b
AM
747 char *name;
748 Elf_External_Sym_Shndx eshndx;
749 char esym[sizeof (Elf64_External_Sym)];
750
0eddce27 751 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
752 return 0;
753
754 /* See if the entry exists already. */
755 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
756 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
757 return 1;
758
759 amt = sizeof (*entry);
a50b1753 760 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
761 if (entry == NULL)
762 return 0;
763
764 /* Go find the symbol, so that we can find it's name. */
765 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 766 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
767 {
768 bfd_release (input_bfd, entry);
769 return 0;
770 }
771
772 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 773 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
774 {
775 asection *s;
776
777 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
778 if (s == NULL || bfd_is_abs_section (s->output_section))
779 {
780 /* We can still bfd_release here as nothing has done another
781 bfd_alloc. We can't do this later in this function. */
782 bfd_release (input_bfd, entry);
783 return 2;
784 }
785 }
786
787 name = (bfd_elf_string_from_elf_section
788 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
789 entry->isym.st_name));
790
791 dynstr = elf_hash_table (info)->dynstr;
792 if (dynstr == NULL)
793 {
794 /* Create a strtab to hold the dynamic symbol names. */
795 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
796 if (dynstr == NULL)
797 return 0;
798 }
799
b34976b6 800 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 801 if (dynstr_index == (size_t) -1)
8c58d23b
AM
802 return 0;
803 entry->isym.st_name = dynstr_index;
804
805 eht = elf_hash_table (info);
806
807 entry->next = eht->dynlocal;
808 eht->dynlocal = entry;
809 entry->input_bfd = input_bfd;
810 entry->input_indx = input_indx;
811 eht->dynsymcount++;
812
813 /* Whatever binding the symbol had before, it's now local. */
814 entry->isym.st_info
815 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
816
817 /* The dynindx will be set at the end of size_dynamic_sections. */
818
819 return 1;
820}
821
30b30c21 822/* Return the dynindex of a local dynamic symbol. */
42751cf3 823
30b30c21 824long
268b6b39
AM
825_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
826 bfd *input_bfd,
827 long input_indx)
30b30c21
RH
828{
829 struct elf_link_local_dynamic_entry *e;
830
831 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
832 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
833 return e->dynindx;
834 return -1;
835}
836
837/* This function is used to renumber the dynamic symbols, if some of
838 them are removed because they are marked as local. This is called
839 via elf_link_hash_traverse. */
840
b34976b6 841static bfd_boolean
268b6b39
AM
842elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
843 void *data)
42751cf3 844{
a50b1753 845 size_t *count = (size_t *) data;
30b30c21 846
6fa3860b
PB
847 if (h->forced_local)
848 return TRUE;
849
850 if (h->dynindx != -1)
851 h->dynindx = ++(*count);
852
853 return TRUE;
854}
855
856
857/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
858 STB_LOCAL binding. */
859
860static bfd_boolean
861elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
862 void *data)
863{
a50b1753 864 size_t *count = (size_t *) data;
6fa3860b 865
6fa3860b
PB
866 if (!h->forced_local)
867 return TRUE;
868
42751cf3 869 if (h->dynindx != -1)
30b30c21
RH
870 h->dynindx = ++(*count);
871
b34976b6 872 return TRUE;
42751cf3 873}
30b30c21 874
aee6f5b4
AO
875/* Return true if the dynamic symbol for a given section should be
876 omitted when creating a shared library. */
877bfd_boolean
878_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
879 struct bfd_link_info *info,
880 asection *p)
881{
74541ad4 882 struct elf_link_hash_table *htab;
ca55926c 883 asection *ip;
74541ad4 884
aee6f5b4
AO
885 switch (elf_section_data (p)->this_hdr.sh_type)
886 {
887 case SHT_PROGBITS:
888 case SHT_NOBITS:
889 /* If sh_type is yet undecided, assume it could be
890 SHT_PROGBITS/SHT_NOBITS. */
891 case SHT_NULL:
74541ad4
AM
892 htab = elf_hash_table (info);
893 if (p == htab->tls_sec)
894 return FALSE;
895
896 if (htab->text_index_section != NULL)
897 return p != htab->text_index_section && p != htab->data_index_section;
898
ca55926c 899 return (htab->dynobj != NULL
3d4d4302 900 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 901 && ip->output_section == p);
aee6f5b4
AO
902
903 /* There shouldn't be section relative relocations
904 against any other section. */
905 default:
906 return TRUE;
907 }
908}
909
062e2358 910/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
911 symbol for each output section, which come first. Next come symbols
912 which have been forced to local binding. Then all of the back-end
913 allocated local dynamic syms, followed by the rest of the global
914 symbols. */
30b30c21 915
554220db
AM
916static unsigned long
917_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
918 struct bfd_link_info *info,
919 unsigned long *section_sym_count)
30b30c21
RH
920{
921 unsigned long dynsymcount = 0;
922
0e1862bb
L
923 if (bfd_link_pic (info)
924 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 925 {
aee6f5b4 926 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
927 asection *p;
928 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 929 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
930 && (p->flags & SEC_ALLOC) != 0
931 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
932 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
933 else
934 elf_section_data (p)->dynindx = 0;
30b30c21 935 }
554220db 936 *section_sym_count = dynsymcount;
30b30c21 937
6fa3860b
PB
938 elf_link_hash_traverse (elf_hash_table (info),
939 elf_link_renumber_local_hash_table_dynsyms,
940 &dynsymcount);
941
30b30c21
RH
942 if (elf_hash_table (info)->dynlocal)
943 {
944 struct elf_link_local_dynamic_entry *p;
945 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
946 p->dynindx = ++dynsymcount;
947 }
90ac2420 948 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
949
950 elf_link_hash_traverse (elf_hash_table (info),
951 elf_link_renumber_hash_table_dynsyms,
952 &dynsymcount);
953
d5486c43
L
954 /* There is an unused NULL entry at the head of the table which we
955 must account for in our count even if the table is empty since it
956 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
957 .dynamic section. */
958 dynsymcount++;
30b30c21 959
ccabcbe5
AM
960 elf_hash_table (info)->dynsymcount = dynsymcount;
961 return dynsymcount;
30b30c21 962}
252b5132 963
54ac0771
L
964/* Merge st_other field. */
965
966static void
967elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 968 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 969 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
970{
971 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
972
973 /* If st_other has a processor-specific meaning, specific
cd3416da 974 code might be needed here. */
54ac0771
L
975 if (bed->elf_backend_merge_symbol_attribute)
976 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
977 dynamic);
978
cd3416da 979 if (!dynamic)
54ac0771 980 {
cd3416da
AM
981 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
982 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 983
cd3416da
AM
984 /* Keep the most constraining visibility. Leave the remainder
985 of the st_other field to elf_backend_merge_symbol_attribute. */
986 if (symvis - 1 < hvis - 1)
987 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 988 }
b8417128
AM
989 else if (definition
990 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
991 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 992 h->protected_def = 1;
54ac0771
L
993}
994
4f3fedcf
AM
995/* This function is called when we want to merge a new symbol with an
996 existing symbol. It handles the various cases which arise when we
997 find a definition in a dynamic object, or when there is already a
998 definition in a dynamic object. The new symbol is described by
999 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1000 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1001 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1002 of an old common symbol. We set OVERRIDE if the old symbol is
1003 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1004 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1005 to change. By OK to change, we mean that we shouldn't warn if the
1006 type or size does change. */
45d6a902 1007
8a56bd02 1008static bfd_boolean
268b6b39
AM
1009_bfd_elf_merge_symbol (bfd *abfd,
1010 struct bfd_link_info *info,
1011 const char *name,
1012 Elf_Internal_Sym *sym,
1013 asection **psec,
1014 bfd_vma *pvalue,
4f3fedcf
AM
1015 struct elf_link_hash_entry **sym_hash,
1016 bfd **poldbfd,
37a9e49a 1017 bfd_boolean *pold_weak,
af44c138 1018 unsigned int *pold_alignment,
268b6b39
AM
1019 bfd_boolean *skip,
1020 bfd_boolean *override,
1021 bfd_boolean *type_change_ok,
6e33951e
L
1022 bfd_boolean *size_change_ok,
1023 bfd_boolean *matched)
252b5132 1024{
7479dfd4 1025 asection *sec, *oldsec;
45d6a902 1026 struct elf_link_hash_entry *h;
90c984fc 1027 struct elf_link_hash_entry *hi;
45d6a902
AM
1028 struct elf_link_hash_entry *flip;
1029 int bind;
1030 bfd *oldbfd;
1031 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1032 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1033 const struct elf_backend_data *bed;
6e33951e 1034 char *new_version;
45d6a902
AM
1035
1036 *skip = FALSE;
1037 *override = FALSE;
1038
1039 sec = *psec;
1040 bind = ELF_ST_BIND (sym->st_info);
1041
1042 if (! bfd_is_und_section (sec))
1043 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1044 else
1045 h = ((struct elf_link_hash_entry *)
1046 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1047 if (h == NULL)
1048 return FALSE;
1049 *sym_hash = h;
252b5132 1050
88ba32a0
L
1051 bed = get_elf_backend_data (abfd);
1052
6e33951e 1053 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1054 if (h->versioned != unversioned)
6e33951e 1055 {
422f1182
L
1056 /* Symbol version is unknown or versioned. */
1057 new_version = strrchr (name, ELF_VER_CHR);
1058 if (new_version)
1059 {
1060 if (h->versioned == unknown)
1061 {
1062 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1063 h->versioned = versioned_hidden;
1064 else
1065 h->versioned = versioned;
1066 }
1067 new_version += 1;
1068 if (new_version[0] == '\0')
1069 new_version = NULL;
1070 }
1071 else
1072 h->versioned = unversioned;
6e33951e 1073 }
422f1182
L
1074 else
1075 new_version = NULL;
6e33951e 1076
90c984fc
L
1077 /* For merging, we only care about real symbols. But we need to make
1078 sure that indirect symbol dynamic flags are updated. */
1079 hi = h;
45d6a902
AM
1080 while (h->root.type == bfd_link_hash_indirect
1081 || h->root.type == bfd_link_hash_warning)
1082 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1083
6e33951e
L
1084 if (!*matched)
1085 {
1086 if (hi == h || h->root.type == bfd_link_hash_new)
1087 *matched = TRUE;
1088 else
1089 {
ae7683d2 1090 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1091 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1092 true if the new symbol is only visible to the symbol with
6e33951e 1093 the same symbol version. */
422f1182
L
1094 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1095 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1096 if (!old_hidden && !new_hidden)
1097 /* The new symbol matches the existing symbol if both
1098 aren't hidden. */
1099 *matched = TRUE;
1100 else
1101 {
1102 /* OLD_VERSION is the symbol version of the existing
1103 symbol. */
422f1182
L
1104 char *old_version;
1105
1106 if (h->versioned >= versioned)
1107 old_version = strrchr (h->root.root.string,
1108 ELF_VER_CHR) + 1;
1109 else
1110 old_version = NULL;
6e33951e
L
1111
1112 /* The new symbol matches the existing symbol if they
1113 have the same symbol version. */
1114 *matched = (old_version == new_version
1115 || (old_version != NULL
1116 && new_version != NULL
1117 && strcmp (old_version, new_version) == 0));
1118 }
1119 }
1120 }
1121
934bce08
AM
1122 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1123 existing symbol. */
1124
1125 oldbfd = NULL;
1126 oldsec = NULL;
1127 switch (h->root.type)
1128 {
1129 default:
1130 break;
1131
1132 case bfd_link_hash_undefined:
1133 case bfd_link_hash_undefweak:
1134 oldbfd = h->root.u.undef.abfd;
1135 break;
1136
1137 case bfd_link_hash_defined:
1138 case bfd_link_hash_defweak:
1139 oldbfd = h->root.u.def.section->owner;
1140 oldsec = h->root.u.def.section;
1141 break;
1142
1143 case bfd_link_hash_common:
1144 oldbfd = h->root.u.c.p->section->owner;
1145 oldsec = h->root.u.c.p->section;
1146 if (pold_alignment)
1147 *pold_alignment = h->root.u.c.p->alignment_power;
1148 break;
1149 }
1150 if (poldbfd && *poldbfd == NULL)
1151 *poldbfd = oldbfd;
1152
1153 /* Differentiate strong and weak symbols. */
1154 newweak = bind == STB_WEAK;
1155 oldweak = (h->root.type == bfd_link_hash_defweak
1156 || h->root.type == bfd_link_hash_undefweak);
1157 if (pold_weak)
1158 *pold_weak = oldweak;
1159
1160 /* This code is for coping with dynamic objects, and is only useful
1161 if we are doing an ELF link. */
1162 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1163 return TRUE;
1164
40b36307 1165 /* We have to check it for every instance since the first few may be
ee659f1f 1166 references and not all compilers emit symbol type for undefined
40b36307
L
1167 symbols. */
1168 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1169
ee659f1f
AM
1170 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1171 respectively, is from a dynamic object. */
1172
1173 newdyn = (abfd->flags & DYNAMIC) != 0;
1174
1175 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1176 syms and defined syms in dynamic libraries respectively.
1177 ref_dynamic on the other hand can be set for a symbol defined in
1178 a dynamic library, and def_dynamic may not be set; When the
1179 definition in a dynamic lib is overridden by a definition in the
1180 executable use of the symbol in the dynamic lib becomes a
1181 reference to the executable symbol. */
1182 if (newdyn)
1183 {
1184 if (bfd_is_und_section (sec))
1185 {
1186 if (bind != STB_WEAK)
1187 {
1188 h->ref_dynamic_nonweak = 1;
1189 hi->ref_dynamic_nonweak = 1;
1190 }
1191 }
1192 else
1193 {
6e33951e
L
1194 /* Update the existing symbol only if they match. */
1195 if (*matched)
1196 h->dynamic_def = 1;
ee659f1f
AM
1197 hi->dynamic_def = 1;
1198 }
1199 }
1200
45d6a902
AM
1201 /* If we just created the symbol, mark it as being an ELF symbol.
1202 Other than that, there is nothing to do--there is no merge issue
1203 with a newly defined symbol--so we just return. */
1204
1205 if (h->root.type == bfd_link_hash_new)
252b5132 1206 {
f5385ebf 1207 h->non_elf = 0;
45d6a902
AM
1208 return TRUE;
1209 }
252b5132 1210
45d6a902
AM
1211 /* In cases involving weak versioned symbols, we may wind up trying
1212 to merge a symbol with itself. Catch that here, to avoid the
1213 confusion that results if we try to override a symbol with
1214 itself. The additional tests catch cases like
1215 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1216 dynamic object, which we do want to handle here. */
1217 if (abfd == oldbfd
895fa45f 1218 && (newweak || oldweak)
45d6a902 1219 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1220 || !h->def_regular))
45d6a902
AM
1221 return TRUE;
1222
707bba77 1223 olddyn = FALSE;
45d6a902
AM
1224 if (oldbfd != NULL)
1225 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1226 else if (oldsec != NULL)
45d6a902 1227 {
707bba77 1228 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1229 indices used by MIPS ELF. */
707bba77 1230 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1231 }
252b5132 1232
45d6a902
AM
1233 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1234 respectively, appear to be a definition rather than reference. */
1235
707bba77 1236 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1237
707bba77
AM
1238 olddef = (h->root.type != bfd_link_hash_undefined
1239 && h->root.type != bfd_link_hash_undefweak
202ac193 1240 && h->root.type != bfd_link_hash_common);
45d6a902 1241
0a36a439
L
1242 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1243 respectively, appear to be a function. */
1244
1245 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1246 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1247
1248 oldfunc = (h->type != STT_NOTYPE
1249 && bed->is_function_type (h->type));
1250
c5d37467 1251 if (!(newfunc && oldfunc)
5b677558
AM
1252 && ELF_ST_TYPE (sym->st_info) != h->type
1253 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1254 && h->type != STT_NOTYPE
c5d37467
AM
1255 && (newdef || bfd_is_com_section (sec))
1256 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1257 {
c5d37467
AM
1258 /* If creating a default indirect symbol ("foo" or "foo@") from
1259 a dynamic versioned definition ("foo@@") skip doing so if
1260 there is an existing regular definition with a different
1261 type. We don't want, for example, a "time" variable in the
1262 executable overriding a "time" function in a shared library. */
1263 if (newdyn
1264 && !olddyn)
1265 {
1266 *skip = TRUE;
1267 return TRUE;
1268 }
1269
1270 /* When adding a symbol from a regular object file after we have
1271 created indirect symbols, undo the indirection and any
1272 dynamic state. */
1273 if (hi != h
1274 && !newdyn
1275 && olddyn)
1276 {
1277 h = hi;
1278 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1279 h->forced_local = 0;
1280 h->ref_dynamic = 0;
1281 h->def_dynamic = 0;
1282 h->dynamic_def = 0;
1283 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1284 {
1285 h->root.type = bfd_link_hash_undefined;
1286 h->root.u.undef.abfd = abfd;
1287 }
1288 else
1289 {
1290 h->root.type = bfd_link_hash_new;
1291 h->root.u.undef.abfd = NULL;
1292 }
1293 return TRUE;
1294 }
580a2b6e
L
1295 }
1296
4c34aff8
AM
1297 /* Check TLS symbols. We don't check undefined symbols introduced
1298 by "ld -u" which have no type (and oldbfd NULL), and we don't
1299 check symbols from plugins because they also have no type. */
1300 if (oldbfd != NULL
1301 && (oldbfd->flags & BFD_PLUGIN) == 0
1302 && (abfd->flags & BFD_PLUGIN) == 0
1303 && ELF_ST_TYPE (sym->st_info) != h->type
1304 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1305 {
1306 bfd *ntbfd, *tbfd;
1307 bfd_boolean ntdef, tdef;
1308 asection *ntsec, *tsec;
1309
1310 if (h->type == STT_TLS)
1311 {
3b36f7e6 1312 ntbfd = abfd;
7479dfd4
L
1313 ntsec = sec;
1314 ntdef = newdef;
1315 tbfd = oldbfd;
1316 tsec = oldsec;
1317 tdef = olddef;
1318 }
1319 else
1320 {
1321 ntbfd = oldbfd;
1322 ntsec = oldsec;
1323 ntdef = olddef;
1324 tbfd = abfd;
1325 tsec = sec;
1326 tdef = newdef;
1327 }
1328
1329 if (tdef && ntdef)
4eca0228 1330 _bfd_error_handler
695344c0 1331 /* xgettext:c-format */
191c0c42
AM
1332 (_("%s: TLS definition in %B section %A "
1333 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1334 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1335 else if (!tdef && !ntdef)
4eca0228 1336 _bfd_error_handler
695344c0 1337 /* xgettext:c-format */
191c0c42
AM
1338 (_("%s: TLS reference in %B "
1339 "mismatches non-TLS reference in %B"),
c08bb8dd 1340 h->root.root.string, tbfd, ntbfd);
7479dfd4 1341 else if (tdef)
4eca0228 1342 _bfd_error_handler
695344c0 1343 /* xgettext:c-format */
191c0c42
AM
1344 (_("%s: TLS definition in %B section %A "
1345 "mismatches non-TLS reference in %B"),
c08bb8dd 1346 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1347 else
4eca0228 1348 _bfd_error_handler
695344c0 1349 /* xgettext:c-format */
191c0c42
AM
1350 (_("%s: TLS reference in %B "
1351 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1352 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1353
1354 bfd_set_error (bfd_error_bad_value);
1355 return FALSE;
1356 }
1357
45d6a902
AM
1358 /* If the old symbol has non-default visibility, we ignore the new
1359 definition from a dynamic object. */
1360 if (newdyn
9c7a29a3 1361 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1362 && !bfd_is_und_section (sec))
1363 {
1364 *skip = TRUE;
1365 /* Make sure this symbol is dynamic. */
f5385ebf 1366 h->ref_dynamic = 1;
90c984fc 1367 hi->ref_dynamic = 1;
45d6a902
AM
1368 /* A protected symbol has external availability. Make sure it is
1369 recorded as dynamic.
1370
1371 FIXME: Should we check type and size for protected symbol? */
1372 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1373 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1374 else
1375 return TRUE;
1376 }
1377 else if (!newdyn
9c7a29a3 1378 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1379 && h->def_dynamic)
45d6a902
AM
1380 {
1381 /* If the new symbol with non-default visibility comes from a
1382 relocatable file and the old definition comes from a dynamic
1383 object, we remove the old definition. */
6c9b78e6 1384 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1385 {
1386 /* Handle the case where the old dynamic definition is
1387 default versioned. We need to copy the symbol info from
1388 the symbol with default version to the normal one if it
1389 was referenced before. */
1390 if (h->ref_regular)
1391 {
6c9b78e6 1392 hi->root.type = h->root.type;
d2dee3b2 1393 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1394 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1395
6c9b78e6 1396 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1397 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1398 {
aed81c4e
MR
1399 /* If the new symbol is hidden or internal, completely undo
1400 any dynamic link state. */
1401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1402 h->forced_local = 0;
1403 h->ref_dynamic = 0;
d2dee3b2
L
1404 }
1405 else
aed81c4e
MR
1406 h->ref_dynamic = 1;
1407
1408 h->def_dynamic = 0;
aed81c4e
MR
1409 /* FIXME: Should we check type and size for protected symbol? */
1410 h->size = 0;
1411 h->type = 0;
1412
6c9b78e6 1413 h = hi;
d2dee3b2
L
1414 }
1415 else
6c9b78e6 1416 h = hi;
d2dee3b2 1417 }
1de1a317 1418
f5eda473
AM
1419 /* If the old symbol was undefined before, then it will still be
1420 on the undefs list. If the new symbol is undefined or
1421 common, we can't make it bfd_link_hash_new here, because new
1422 undefined or common symbols will be added to the undefs list
1423 by _bfd_generic_link_add_one_symbol. Symbols may not be
1424 added twice to the undefs list. Also, if the new symbol is
1425 undefweak then we don't want to lose the strong undef. */
1426 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1427 {
1de1a317 1428 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1429 h->root.u.undef.abfd = abfd;
1430 }
1431 else
1432 {
1433 h->root.type = bfd_link_hash_new;
1434 h->root.u.undef.abfd = NULL;
1435 }
1436
f5eda473 1437 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1438 {
f5eda473
AM
1439 /* If the new symbol is hidden or internal, completely undo
1440 any dynamic link state. */
1441 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1442 h->forced_local = 0;
1443 h->ref_dynamic = 0;
45d6a902 1444 }
f5eda473
AM
1445 else
1446 h->ref_dynamic = 1;
1447 h->def_dynamic = 0;
45d6a902
AM
1448 /* FIXME: Should we check type and size for protected symbol? */
1449 h->size = 0;
1450 h->type = 0;
1451 return TRUE;
1452 }
14a793b2 1453
15b43f48
AM
1454 /* If a new weak symbol definition comes from a regular file and the
1455 old symbol comes from a dynamic library, we treat the new one as
1456 strong. Similarly, an old weak symbol definition from a regular
1457 file is treated as strong when the new symbol comes from a dynamic
1458 library. Further, an old weak symbol from a dynamic library is
1459 treated as strong if the new symbol is from a dynamic library.
1460 This reflects the way glibc's ld.so works.
1461
1462 Do this before setting *type_change_ok or *size_change_ok so that
1463 we warn properly when dynamic library symbols are overridden. */
1464
1465 if (newdef && !newdyn && olddyn)
0f8a2703 1466 newweak = FALSE;
15b43f48 1467 if (olddef && newdyn)
0f8a2703
AM
1468 oldweak = FALSE;
1469
d334575b 1470 /* Allow changes between different types of function symbol. */
0a36a439 1471 if (newfunc && oldfunc)
fcb93ecf
PB
1472 *type_change_ok = TRUE;
1473
79349b09
AM
1474 /* It's OK to change the type if either the existing symbol or the
1475 new symbol is weak. A type change is also OK if the old symbol
1476 is undefined and the new symbol is defined. */
252b5132 1477
79349b09
AM
1478 if (oldweak
1479 || newweak
1480 || (newdef
1481 && h->root.type == bfd_link_hash_undefined))
1482 *type_change_ok = TRUE;
1483
1484 /* It's OK to change the size if either the existing symbol or the
1485 new symbol is weak, or if the old symbol is undefined. */
1486
1487 if (*type_change_ok
1488 || h->root.type == bfd_link_hash_undefined)
1489 *size_change_ok = TRUE;
45d6a902 1490
45d6a902
AM
1491 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1492 symbol, respectively, appears to be a common symbol in a dynamic
1493 object. If a symbol appears in an uninitialized section, and is
1494 not weak, and is not a function, then it may be a common symbol
1495 which was resolved when the dynamic object was created. We want
1496 to treat such symbols specially, because they raise special
1497 considerations when setting the symbol size: if the symbol
1498 appears as a common symbol in a regular object, and the size in
1499 the regular object is larger, we must make sure that we use the
1500 larger size. This problematic case can always be avoided in C,
1501 but it must be handled correctly when using Fortran shared
1502 libraries.
1503
1504 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1505 likewise for OLDDYNCOMMON and OLDDEF.
1506
1507 Note that this test is just a heuristic, and that it is quite
1508 possible to have an uninitialized symbol in a shared object which
1509 is really a definition, rather than a common symbol. This could
1510 lead to some minor confusion when the symbol really is a common
1511 symbol in some regular object. However, I think it will be
1512 harmless. */
1513
1514 if (newdyn
1515 && newdef
79349b09 1516 && !newweak
45d6a902
AM
1517 && (sec->flags & SEC_ALLOC) != 0
1518 && (sec->flags & SEC_LOAD) == 0
1519 && sym->st_size > 0
0a36a439 1520 && !newfunc)
45d6a902
AM
1521 newdyncommon = TRUE;
1522 else
1523 newdyncommon = FALSE;
1524
1525 if (olddyn
1526 && olddef
1527 && h->root.type == bfd_link_hash_defined
f5385ebf 1528 && h->def_dynamic
45d6a902
AM
1529 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1530 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1531 && h->size > 0
0a36a439 1532 && !oldfunc)
45d6a902
AM
1533 olddyncommon = TRUE;
1534 else
1535 olddyncommon = FALSE;
1536
a4d8e49b
L
1537 /* We now know everything about the old and new symbols. We ask the
1538 backend to check if we can merge them. */
5d13b3b3
AM
1539 if (bed->merge_symbol != NULL)
1540 {
1541 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1542 return FALSE;
1543 sec = *psec;
1544 }
a4d8e49b 1545
45d6a902
AM
1546 /* If both the old and the new symbols look like common symbols in a
1547 dynamic object, set the size of the symbol to the larger of the
1548 two. */
1549
1550 if (olddyncommon
1551 && newdyncommon
1552 && sym->st_size != h->size)
1553 {
1554 /* Since we think we have two common symbols, issue a multiple
1555 common warning if desired. Note that we only warn if the
1556 size is different. If the size is the same, we simply let
1557 the old symbol override the new one as normally happens with
1558 symbols defined in dynamic objects. */
1559
1a72702b
AM
1560 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1561 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1562 if (sym->st_size > h->size)
1563 h->size = sym->st_size;
252b5132 1564
45d6a902 1565 *size_change_ok = TRUE;
252b5132
RH
1566 }
1567
45d6a902
AM
1568 /* If we are looking at a dynamic object, and we have found a
1569 definition, we need to see if the symbol was already defined by
1570 some other object. If so, we want to use the existing
1571 definition, and we do not want to report a multiple symbol
1572 definition error; we do this by clobbering *PSEC to be
1573 bfd_und_section_ptr.
1574
1575 We treat a common symbol as a definition if the symbol in the
1576 shared library is a function, since common symbols always
1577 represent variables; this can cause confusion in principle, but
1578 any such confusion would seem to indicate an erroneous program or
1579 shared library. We also permit a common symbol in a regular
8170f769 1580 object to override a weak symbol in a shared object. */
45d6a902
AM
1581
1582 if (newdyn
1583 && newdef
77cfaee6 1584 && (olddef
45d6a902 1585 || (h->root.type == bfd_link_hash_common
8170f769 1586 && (newweak || newfunc))))
45d6a902
AM
1587 {
1588 *override = TRUE;
1589 newdef = FALSE;
1590 newdyncommon = FALSE;
252b5132 1591
45d6a902
AM
1592 *psec = sec = bfd_und_section_ptr;
1593 *size_change_ok = TRUE;
252b5132 1594
45d6a902
AM
1595 /* If we get here when the old symbol is a common symbol, then
1596 we are explicitly letting it override a weak symbol or
1597 function in a dynamic object, and we don't want to warn about
1598 a type change. If the old symbol is a defined symbol, a type
1599 change warning may still be appropriate. */
252b5132 1600
45d6a902
AM
1601 if (h->root.type == bfd_link_hash_common)
1602 *type_change_ok = TRUE;
1603 }
1604
1605 /* Handle the special case of an old common symbol merging with a
1606 new symbol which looks like a common symbol in a shared object.
1607 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1608 common symbol, and let _bfd_generic_link_add_one_symbol do the
1609 right thing. */
45d6a902
AM
1610
1611 if (newdyncommon
1612 && h->root.type == bfd_link_hash_common)
1613 {
1614 *override = TRUE;
1615 newdef = FALSE;
1616 newdyncommon = FALSE;
1617 *pvalue = sym->st_size;
a4d8e49b 1618 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1619 *size_change_ok = TRUE;
1620 }
1621
c5e2cead 1622 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1623 if (newdef && olddef && newweak)
54ac0771 1624 {
35ed3f94 1625 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1626 if (!(oldbfd != NULL
1627 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1628 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1629 {
1630 newdef = FALSE;
1631 *skip = TRUE;
1632 }
54ac0771
L
1633
1634 /* Merge st_other. If the symbol already has a dynamic index,
1635 but visibility says it should not be visible, turn it into a
1636 local symbol. */
b8417128 1637 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1638 if (h->dynindx != -1)
1639 switch (ELF_ST_VISIBILITY (h->other))
1640 {
1641 case STV_INTERNAL:
1642 case STV_HIDDEN:
1643 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1644 break;
1645 }
1646 }
c5e2cead 1647
45d6a902
AM
1648 /* If the old symbol is from a dynamic object, and the new symbol is
1649 a definition which is not from a dynamic object, then the new
1650 symbol overrides the old symbol. Symbols from regular files
1651 always take precedence over symbols from dynamic objects, even if
1652 they are defined after the dynamic object in the link.
1653
1654 As above, we again permit a common symbol in a regular object to
1655 override a definition in a shared object if the shared object
0f8a2703 1656 symbol is a function or is weak. */
45d6a902
AM
1657
1658 flip = NULL;
77cfaee6 1659 if (!newdyn
45d6a902
AM
1660 && (newdef
1661 || (bfd_is_com_section (sec)
0a36a439 1662 && (oldweak || oldfunc)))
45d6a902
AM
1663 && olddyn
1664 && olddef
f5385ebf 1665 && h->def_dynamic)
45d6a902
AM
1666 {
1667 /* Change the hash table entry to undefined, and let
1668 _bfd_generic_link_add_one_symbol do the right thing with the
1669 new definition. */
1670
1671 h->root.type = bfd_link_hash_undefined;
1672 h->root.u.undef.abfd = h->root.u.def.section->owner;
1673 *size_change_ok = TRUE;
1674
1675 olddef = FALSE;
1676 olddyncommon = FALSE;
1677
1678 /* We again permit a type change when a common symbol may be
1679 overriding a function. */
1680
1681 if (bfd_is_com_section (sec))
0a36a439
L
1682 {
1683 if (oldfunc)
1684 {
1685 /* If a common symbol overrides a function, make sure
1686 that it isn't defined dynamically nor has type
1687 function. */
1688 h->def_dynamic = 0;
1689 h->type = STT_NOTYPE;
1690 }
1691 *type_change_ok = TRUE;
1692 }
45d6a902 1693
6c9b78e6
AM
1694 if (hi->root.type == bfd_link_hash_indirect)
1695 flip = hi;
45d6a902
AM
1696 else
1697 /* This union may have been set to be non-NULL when this symbol
1698 was seen in a dynamic object. We must force the union to be
1699 NULL, so that it is correct for a regular symbol. */
1700 h->verinfo.vertree = NULL;
1701 }
1702
1703 /* Handle the special case of a new common symbol merging with an
1704 old symbol that looks like it might be a common symbol defined in
1705 a shared object. Note that we have already handled the case in
1706 which a new common symbol should simply override the definition
1707 in the shared library. */
1708
1709 if (! newdyn
1710 && bfd_is_com_section (sec)
1711 && olddyncommon)
1712 {
1713 /* It would be best if we could set the hash table entry to a
1714 common symbol, but we don't know what to use for the section
1715 or the alignment. */
1a72702b
AM
1716 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1717 bfd_link_hash_common, sym->st_size);
45d6a902 1718
4cc11e76 1719 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1720 larger, pretend that the new symbol has its size. */
1721
1722 if (h->size > *pvalue)
1723 *pvalue = h->size;
1724
af44c138
L
1725 /* We need to remember the alignment required by the symbol
1726 in the dynamic object. */
1727 BFD_ASSERT (pold_alignment);
1728 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1729
1730 olddef = FALSE;
1731 olddyncommon = FALSE;
1732
1733 h->root.type = bfd_link_hash_undefined;
1734 h->root.u.undef.abfd = h->root.u.def.section->owner;
1735
1736 *size_change_ok = TRUE;
1737 *type_change_ok = TRUE;
1738
6c9b78e6
AM
1739 if (hi->root.type == bfd_link_hash_indirect)
1740 flip = hi;
45d6a902
AM
1741 else
1742 h->verinfo.vertree = NULL;
1743 }
1744
1745 if (flip != NULL)
1746 {
1747 /* Handle the case where we had a versioned symbol in a dynamic
1748 library and now find a definition in a normal object. In this
1749 case, we make the versioned symbol point to the normal one. */
45d6a902 1750 flip->root.type = h->root.type;
00cbee0a 1751 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1752 h->root.type = bfd_link_hash_indirect;
1753 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1754 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1755 if (h->def_dynamic)
45d6a902 1756 {
f5385ebf
AM
1757 h->def_dynamic = 0;
1758 flip->ref_dynamic = 1;
45d6a902
AM
1759 }
1760 }
1761
45d6a902
AM
1762 return TRUE;
1763}
1764
1765/* This function is called to create an indirect symbol from the
1766 default for the symbol with the default version if needed. The
4f3fedcf 1767 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1768 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1769
28caa186 1770static bfd_boolean
268b6b39
AM
1771_bfd_elf_add_default_symbol (bfd *abfd,
1772 struct bfd_link_info *info,
1773 struct elf_link_hash_entry *h,
1774 const char *name,
1775 Elf_Internal_Sym *sym,
4f3fedcf
AM
1776 asection *sec,
1777 bfd_vma value,
1778 bfd **poldbfd,
e3c9d234 1779 bfd_boolean *dynsym)
45d6a902
AM
1780{
1781 bfd_boolean type_change_ok;
1782 bfd_boolean size_change_ok;
1783 bfd_boolean skip;
1784 char *shortname;
1785 struct elf_link_hash_entry *hi;
1786 struct bfd_link_hash_entry *bh;
9c5bfbb7 1787 const struct elf_backend_data *bed;
45d6a902
AM
1788 bfd_boolean collect;
1789 bfd_boolean dynamic;
e3c9d234 1790 bfd_boolean override;
45d6a902
AM
1791 char *p;
1792 size_t len, shortlen;
ffd65175 1793 asection *tmp_sec;
6e33951e 1794 bfd_boolean matched;
45d6a902 1795
422f1182
L
1796 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1797 return TRUE;
1798
45d6a902
AM
1799 /* If this symbol has a version, and it is the default version, we
1800 create an indirect symbol from the default name to the fully
1801 decorated name. This will cause external references which do not
1802 specify a version to be bound to this version of the symbol. */
1803 p = strchr (name, ELF_VER_CHR);
422f1182
L
1804 if (h->versioned == unknown)
1805 {
1806 if (p == NULL)
1807 {
1808 h->versioned = unversioned;
1809 return TRUE;
1810 }
1811 else
1812 {
1813 if (p[1] != ELF_VER_CHR)
1814 {
1815 h->versioned = versioned_hidden;
1816 return TRUE;
1817 }
1818 else
1819 h->versioned = versioned;
1820 }
1821 }
4373f8af
L
1822 else
1823 {
1824 /* PR ld/19073: We may see an unversioned definition after the
1825 default version. */
1826 if (p == NULL)
1827 return TRUE;
1828 }
45d6a902 1829
45d6a902
AM
1830 bed = get_elf_backend_data (abfd);
1831 collect = bed->collect;
1832 dynamic = (abfd->flags & DYNAMIC) != 0;
1833
1834 shortlen = p - name;
a50b1753 1835 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1836 if (shortname == NULL)
1837 return FALSE;
1838 memcpy (shortname, name, shortlen);
1839 shortname[shortlen] = '\0';
1840
1841 /* We are going to create a new symbol. Merge it with any existing
1842 symbol with this name. For the purposes of the merge, act as
1843 though we were defining the symbol we just defined, although we
1844 actually going to define an indirect symbol. */
1845 type_change_ok = FALSE;
1846 size_change_ok = FALSE;
6e33951e 1847 matched = TRUE;
ffd65175
AM
1848 tmp_sec = sec;
1849 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1850 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1851 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1852 return FALSE;
1853
1854 if (skip)
1855 goto nondefault;
1856
5b677558
AM
1857 if (hi->def_regular)
1858 {
1859 /* If the undecorated symbol will have a version added by a
1860 script different to H, then don't indirect to/from the
1861 undecorated symbol. This isn't ideal because we may not yet
1862 have seen symbol versions, if given by a script on the
1863 command line rather than via --version-script. */
1864 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1865 {
1866 bfd_boolean hide;
1867
1868 hi->verinfo.vertree
1869 = bfd_find_version_for_sym (info->version_info,
1870 hi->root.root.string, &hide);
1871 if (hi->verinfo.vertree != NULL && hide)
1872 {
1873 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1874 goto nondefault;
1875 }
1876 }
1877 if (hi->verinfo.vertree != NULL
1878 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1879 goto nondefault;
1880 }
1881
45d6a902
AM
1882 if (! override)
1883 {
c6e8a9a8 1884 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1885 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1886 {
1887 bh = &hi->root;
1888 if (! (_bfd_generic_link_add_one_symbol
1889 (info, abfd, shortname, BSF_INDIRECT,
1890 bfd_ind_section_ptr,
1891 0, name, FALSE, collect, &bh)))
1892 return FALSE;
1893 hi = (struct elf_link_hash_entry *) bh;
1894 }
45d6a902
AM
1895 }
1896 else
1897 {
1898 /* In this case the symbol named SHORTNAME is overriding the
1899 indirect symbol we want to add. We were planning on making
1900 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1901 is the name without a version. NAME is the fully versioned
1902 name, and it is the default version.
1903
1904 Overriding means that we already saw a definition for the
1905 symbol SHORTNAME in a regular object, and it is overriding
1906 the symbol defined in the dynamic object.
1907
1908 When this happens, we actually want to change NAME, the
1909 symbol we just added, to refer to SHORTNAME. This will cause
1910 references to NAME in the shared object to become references
1911 to SHORTNAME in the regular object. This is what we expect
1912 when we override a function in a shared object: that the
1913 references in the shared object will be mapped to the
1914 definition in the regular object. */
1915
1916 while (hi->root.type == bfd_link_hash_indirect
1917 || hi->root.type == bfd_link_hash_warning)
1918 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1919
1920 h->root.type = bfd_link_hash_indirect;
1921 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1922 if (h->def_dynamic)
45d6a902 1923 {
f5385ebf
AM
1924 h->def_dynamic = 0;
1925 hi->ref_dynamic = 1;
1926 if (hi->ref_regular
1927 || hi->def_regular)
45d6a902 1928 {
c152c796 1929 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1930 return FALSE;
1931 }
1932 }
1933
1934 /* Now set HI to H, so that the following code will set the
1935 other fields correctly. */
1936 hi = h;
1937 }
1938
fab4a87f
L
1939 /* Check if HI is a warning symbol. */
1940 if (hi->root.type == bfd_link_hash_warning)
1941 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1942
45d6a902
AM
1943 /* If there is a duplicate definition somewhere, then HI may not
1944 point to an indirect symbol. We will have reported an error to
1945 the user in that case. */
1946
1947 if (hi->root.type == bfd_link_hash_indirect)
1948 {
1949 struct elf_link_hash_entry *ht;
1950
45d6a902 1951 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1952 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1953
68c88cd4
AM
1954 /* A reference to the SHORTNAME symbol from a dynamic library
1955 will be satisfied by the versioned symbol at runtime. In
1956 effect, we have a reference to the versioned symbol. */
1957 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1958 hi->dynamic_def |= ht->dynamic_def;
1959
45d6a902
AM
1960 /* See if the new flags lead us to realize that the symbol must
1961 be dynamic. */
1962 if (! *dynsym)
1963 {
1964 if (! dynamic)
1965 {
0e1862bb 1966 if (! bfd_link_executable (info)
90c984fc 1967 || hi->def_dynamic
f5385ebf 1968 || hi->ref_dynamic)
45d6a902
AM
1969 *dynsym = TRUE;
1970 }
1971 else
1972 {
f5385ebf 1973 if (hi->ref_regular)
45d6a902
AM
1974 *dynsym = TRUE;
1975 }
1976 }
1977 }
1978
1979 /* We also need to define an indirection from the nondefault version
1980 of the symbol. */
1981
1982nondefault:
1983 len = strlen (name);
a50b1753 1984 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1985 if (shortname == NULL)
1986 return FALSE;
1987 memcpy (shortname, name, shortlen);
1988 memcpy (shortname + shortlen, p + 1, len - shortlen);
1989
1990 /* Once again, merge with any existing symbol. */
1991 type_change_ok = FALSE;
1992 size_change_ok = FALSE;
ffd65175
AM
1993 tmp_sec = sec;
1994 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1995 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1996 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1997 return FALSE;
1998
1999 if (skip)
2000 return TRUE;
2001
2002 if (override)
2003 {
2004 /* Here SHORTNAME is a versioned name, so we don't expect to see
2005 the type of override we do in the case above unless it is
4cc11e76 2006 overridden by a versioned definition. */
45d6a902
AM
2007 if (hi->root.type != bfd_link_hash_defined
2008 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2009 _bfd_error_handler
695344c0 2010 /* xgettext:c-format */
d003868e
AM
2011 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2012 abfd, shortname);
45d6a902
AM
2013 }
2014 else
2015 {
2016 bh = &hi->root;
2017 if (! (_bfd_generic_link_add_one_symbol
2018 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2019 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2020 return FALSE;
2021 hi = (struct elf_link_hash_entry *) bh;
2022
2023 /* If there is a duplicate definition somewhere, then HI may not
2024 point to an indirect symbol. We will have reported an error
2025 to the user in that case. */
2026
2027 if (hi->root.type == bfd_link_hash_indirect)
2028 {
fcfa13d2 2029 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2030 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2031 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2032
2033 /* See if the new flags lead us to realize that the symbol
2034 must be dynamic. */
2035 if (! *dynsym)
2036 {
2037 if (! dynamic)
2038 {
0e1862bb 2039 if (! bfd_link_executable (info)
f5385ebf 2040 || hi->ref_dynamic)
45d6a902
AM
2041 *dynsym = TRUE;
2042 }
2043 else
2044 {
f5385ebf 2045 if (hi->ref_regular)
45d6a902
AM
2046 *dynsym = TRUE;
2047 }
2048 }
2049 }
2050 }
2051
2052 return TRUE;
2053}
2054\f
2055/* This routine is used to export all defined symbols into the dynamic
2056 symbol table. It is called via elf_link_hash_traverse. */
2057
28caa186 2058static bfd_boolean
268b6b39 2059_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2060{
a50b1753 2061 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2062
2063 /* Ignore indirect symbols. These are added by the versioning code. */
2064 if (h->root.type == bfd_link_hash_indirect)
2065 return TRUE;
2066
7686d77d
AM
2067 /* Ignore this if we won't export it. */
2068 if (!eif->info->export_dynamic && !h->dynamic)
2069 return TRUE;
45d6a902
AM
2070
2071 if (h->dynindx == -1
fd91d419
L
2072 && (h->def_regular || h->ref_regular)
2073 && ! bfd_hide_sym_by_version (eif->info->version_info,
2074 h->root.root.string))
45d6a902 2075 {
fd91d419 2076 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2077 {
fd91d419
L
2078 eif->failed = TRUE;
2079 return FALSE;
45d6a902
AM
2080 }
2081 }
2082
2083 return TRUE;
2084}
2085\f
2086/* Look through the symbols which are defined in other shared
2087 libraries and referenced here. Update the list of version
2088 dependencies. This will be put into the .gnu.version_r section.
2089 This function is called via elf_link_hash_traverse. */
2090
28caa186 2091static bfd_boolean
268b6b39
AM
2092_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2093 void *data)
45d6a902 2094{
a50b1753 2095 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2096 Elf_Internal_Verneed *t;
2097 Elf_Internal_Vernaux *a;
2098 bfd_size_type amt;
2099
45d6a902
AM
2100 /* We only care about symbols defined in shared objects with version
2101 information. */
f5385ebf
AM
2102 if (!h->def_dynamic
2103 || h->def_regular
45d6a902 2104 || h->dynindx == -1
7b20f099
AM
2105 || h->verinfo.verdef == NULL
2106 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2107 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2108 return TRUE;
2109
2110 /* See if we already know about this version. */
28caa186
AM
2111 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2112 t != NULL;
2113 t = t->vn_nextref)
45d6a902
AM
2114 {
2115 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2116 continue;
2117
2118 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2119 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2120 return TRUE;
2121
2122 break;
2123 }
2124
2125 /* This is a new version. Add it to tree we are building. */
2126
2127 if (t == NULL)
2128 {
2129 amt = sizeof *t;
a50b1753 2130 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2131 if (t == NULL)
2132 {
2133 rinfo->failed = TRUE;
2134 return FALSE;
2135 }
2136
2137 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2138 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2139 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2140 }
2141
2142 amt = sizeof *a;
a50b1753 2143 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2144 if (a == NULL)
2145 {
2146 rinfo->failed = TRUE;
2147 return FALSE;
2148 }
45d6a902
AM
2149
2150 /* Note that we are copying a string pointer here, and testing it
2151 above. If bfd_elf_string_from_elf_section is ever changed to
2152 discard the string data when low in memory, this will have to be
2153 fixed. */
2154 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2155
2156 a->vna_flags = h->verinfo.verdef->vd_flags;
2157 a->vna_nextptr = t->vn_auxptr;
2158
2159 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2160 ++rinfo->vers;
2161
2162 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2163
2164 t->vn_auxptr = a;
2165
2166 return TRUE;
2167}
2168
2169/* Figure out appropriate versions for all the symbols. We may not
2170 have the version number script until we have read all of the input
2171 files, so until that point we don't know which symbols should be
2172 local. This function is called via elf_link_hash_traverse. */
2173
28caa186 2174static bfd_boolean
268b6b39 2175_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2176{
28caa186 2177 struct elf_info_failed *sinfo;
45d6a902 2178 struct bfd_link_info *info;
9c5bfbb7 2179 const struct elf_backend_data *bed;
45d6a902
AM
2180 struct elf_info_failed eif;
2181 char *p;
45d6a902 2182
a50b1753 2183 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2184 info = sinfo->info;
2185
45d6a902
AM
2186 /* Fix the symbol flags. */
2187 eif.failed = FALSE;
2188 eif.info = info;
2189 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2190 {
2191 if (eif.failed)
2192 sinfo->failed = TRUE;
2193 return FALSE;
2194 }
2195
2196 /* We only need version numbers for symbols defined in regular
2197 objects. */
f5385ebf 2198 if (!h->def_regular)
45d6a902
AM
2199 return TRUE;
2200
28caa186 2201 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2202 p = strchr (h->root.root.string, ELF_VER_CHR);
2203 if (p != NULL && h->verinfo.vertree == NULL)
2204 {
2205 struct bfd_elf_version_tree *t;
45d6a902 2206
45d6a902
AM
2207 ++p;
2208 if (*p == ELF_VER_CHR)
6e33951e 2209 ++p;
45d6a902
AM
2210
2211 /* If there is no version string, we can just return out. */
2212 if (*p == '\0')
6e33951e 2213 return TRUE;
45d6a902
AM
2214
2215 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2216 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2217 {
2218 if (strcmp (t->name, p) == 0)
2219 {
2220 size_t len;
2221 char *alc;
2222 struct bfd_elf_version_expr *d;
2223
2224 len = p - h->root.root.string;
a50b1753 2225 alc = (char *) bfd_malloc (len);
45d6a902 2226 if (alc == NULL)
14b1c01e
AM
2227 {
2228 sinfo->failed = TRUE;
2229 return FALSE;
2230 }
45d6a902
AM
2231 memcpy (alc, h->root.root.string, len - 1);
2232 alc[len - 1] = '\0';
2233 if (alc[len - 2] == ELF_VER_CHR)
2234 alc[len - 2] = '\0';
2235
2236 h->verinfo.vertree = t;
2237 t->used = TRUE;
2238 d = NULL;
2239
108ba305
JJ
2240 if (t->globals.list != NULL)
2241 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2242
2243 /* See if there is anything to force this symbol to
2244 local scope. */
108ba305 2245 if (d == NULL && t->locals.list != NULL)
45d6a902 2246 {
108ba305
JJ
2247 d = (*t->match) (&t->locals, NULL, alc);
2248 if (d != NULL
2249 && h->dynindx != -1
108ba305
JJ
2250 && ! info->export_dynamic)
2251 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2252 }
2253
2254 free (alc);
2255 break;
2256 }
2257 }
2258
2259 /* If we are building an application, we need to create a
2260 version node for this version. */
0e1862bb 2261 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2262 {
2263 struct bfd_elf_version_tree **pp;
2264 int version_index;
2265
2266 /* If we aren't going to export this symbol, we don't need
2267 to worry about it. */
2268 if (h->dynindx == -1)
2269 return TRUE;
2270
ef53be89
AM
2271 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2272 sizeof *t);
45d6a902
AM
2273 if (t == NULL)
2274 {
2275 sinfo->failed = TRUE;
2276 return FALSE;
2277 }
2278
45d6a902 2279 t->name = p;
45d6a902
AM
2280 t->name_indx = (unsigned int) -1;
2281 t->used = TRUE;
2282
2283 version_index = 1;
2284 /* Don't count anonymous version tag. */
fd91d419
L
2285 if (sinfo->info->version_info != NULL
2286 && sinfo->info->version_info->vernum == 0)
45d6a902 2287 version_index = 0;
fd91d419
L
2288 for (pp = &sinfo->info->version_info;
2289 *pp != NULL;
2290 pp = &(*pp)->next)
45d6a902
AM
2291 ++version_index;
2292 t->vernum = version_index;
2293
2294 *pp = t;
2295
2296 h->verinfo.vertree = t;
2297 }
2298 else if (t == NULL)
2299 {
2300 /* We could not find the version for a symbol when
2301 generating a shared archive. Return an error. */
4eca0228 2302 _bfd_error_handler
695344c0 2303 /* xgettext:c-format */
c55fe096 2304 (_("%B: version node not found for symbol %s"),
28caa186 2305 info->output_bfd, h->root.root.string);
45d6a902
AM
2306 bfd_set_error (bfd_error_bad_value);
2307 sinfo->failed = TRUE;
2308 return FALSE;
2309 }
45d6a902
AM
2310 }
2311
2312 /* If we don't have a version for this symbol, see if we can find
2313 something. */
fd91d419 2314 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2315 {
1e8fa21e 2316 bfd_boolean hide;
ae5a3597 2317
fd91d419
L
2318 h->verinfo.vertree
2319 = bfd_find_version_for_sym (sinfo->info->version_info,
2320 h->root.root.string, &hide);
1e8fa21e
AM
2321 if (h->verinfo.vertree != NULL && hide)
2322 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2323 }
2324
2325 return TRUE;
2326}
2327\f
45d6a902
AM
2328/* Read and swap the relocs from the section indicated by SHDR. This
2329 may be either a REL or a RELA section. The relocations are
2330 translated into RELA relocations and stored in INTERNAL_RELOCS,
2331 which should have already been allocated to contain enough space.
2332 The EXTERNAL_RELOCS are a buffer where the external form of the
2333 relocations should be stored.
2334
2335 Returns FALSE if something goes wrong. */
2336
2337static bfd_boolean
268b6b39 2338elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2339 asection *sec,
268b6b39
AM
2340 Elf_Internal_Shdr *shdr,
2341 void *external_relocs,
2342 Elf_Internal_Rela *internal_relocs)
45d6a902 2343{
9c5bfbb7 2344 const struct elf_backend_data *bed;
268b6b39 2345 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2346 const bfd_byte *erela;
2347 const bfd_byte *erelaend;
2348 Elf_Internal_Rela *irela;
243ef1e0
L
2349 Elf_Internal_Shdr *symtab_hdr;
2350 size_t nsyms;
45d6a902 2351
45d6a902
AM
2352 /* Position ourselves at the start of the section. */
2353 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2354 return FALSE;
2355
2356 /* Read the relocations. */
2357 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2358 return FALSE;
2359
243ef1e0 2360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2361 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2362
45d6a902
AM
2363 bed = get_elf_backend_data (abfd);
2364
2365 /* Convert the external relocations to the internal format. */
2366 if (shdr->sh_entsize == bed->s->sizeof_rel)
2367 swap_in = bed->s->swap_reloc_in;
2368 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2369 swap_in = bed->s->swap_reloca_in;
2370 else
2371 {
2372 bfd_set_error (bfd_error_wrong_format);
2373 return FALSE;
2374 }
2375
a50b1753 2376 erela = (const bfd_byte *) external_relocs;
51992aec 2377 erelaend = erela + shdr->sh_size;
45d6a902
AM
2378 irela = internal_relocs;
2379 while (erela < erelaend)
2380 {
243ef1e0
L
2381 bfd_vma r_symndx;
2382
45d6a902 2383 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2384 r_symndx = ELF32_R_SYM (irela->r_info);
2385 if (bed->s->arch_size == 64)
2386 r_symndx >>= 24;
ce98a316
NC
2387 if (nsyms > 0)
2388 {
2389 if ((size_t) r_symndx >= nsyms)
2390 {
4eca0228 2391 _bfd_error_handler
695344c0 2392 /* xgettext:c-format */
ce98a316
NC
2393 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2394 " for offset 0x%lx in section `%A'"),
c08bb8dd
AM
2395 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2396 irela->r_offset, sec);
ce98a316
NC
2397 bfd_set_error (bfd_error_bad_value);
2398 return FALSE;
2399 }
2400 }
cf35638d 2401 else if (r_symndx != STN_UNDEF)
243ef1e0 2402 {
4eca0228 2403 _bfd_error_handler
695344c0 2404 /* xgettext:c-format */
c08bb8dd
AM
2405 (_("%B: non-zero symbol index (0x%lx)"
2406 " for offset 0x%lx in section `%A'"
ce98a316 2407 " when the object file has no symbol table"),
c08bb8dd
AM
2408 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2409 irela->r_offset, sec);
243ef1e0
L
2410 bfd_set_error (bfd_error_bad_value);
2411 return FALSE;
2412 }
45d6a902
AM
2413 irela += bed->s->int_rels_per_ext_rel;
2414 erela += shdr->sh_entsize;
2415 }
2416
2417 return TRUE;
2418}
2419
2420/* Read and swap the relocs for a section O. They may have been
2421 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2422 not NULL, they are used as buffers to read into. They are known to
2423 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2424 the return value is allocated using either malloc or bfd_alloc,
2425 according to the KEEP_MEMORY argument. If O has two relocation
2426 sections (both REL and RELA relocations), then the REL_HDR
2427 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2428 RELA_HDR relocations. */
45d6a902
AM
2429
2430Elf_Internal_Rela *
268b6b39
AM
2431_bfd_elf_link_read_relocs (bfd *abfd,
2432 asection *o,
2433 void *external_relocs,
2434 Elf_Internal_Rela *internal_relocs,
2435 bfd_boolean keep_memory)
45d6a902 2436{
268b6b39 2437 void *alloc1 = NULL;
45d6a902 2438 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2439 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2440 struct bfd_elf_section_data *esdo = elf_section_data (o);
2441 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2442
d4730f92
BS
2443 if (esdo->relocs != NULL)
2444 return esdo->relocs;
45d6a902
AM
2445
2446 if (o->reloc_count == 0)
2447 return NULL;
2448
45d6a902
AM
2449 if (internal_relocs == NULL)
2450 {
2451 bfd_size_type size;
2452
2453 size = o->reloc_count;
2454 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2455 if (keep_memory)
a50b1753 2456 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2457 else
a50b1753 2458 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2459 if (internal_relocs == NULL)
2460 goto error_return;
2461 }
2462
2463 if (external_relocs == NULL)
2464 {
d4730f92
BS
2465 bfd_size_type size = 0;
2466
2467 if (esdo->rel.hdr)
2468 size += esdo->rel.hdr->sh_size;
2469 if (esdo->rela.hdr)
2470 size += esdo->rela.hdr->sh_size;
45d6a902 2471
268b6b39 2472 alloc1 = bfd_malloc (size);
45d6a902
AM
2473 if (alloc1 == NULL)
2474 goto error_return;
2475 external_relocs = alloc1;
2476 }
2477
d4730f92
BS
2478 internal_rela_relocs = internal_relocs;
2479 if (esdo->rel.hdr)
2480 {
2481 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2482 external_relocs,
2483 internal_relocs))
2484 goto error_return;
2485 external_relocs = (((bfd_byte *) external_relocs)
2486 + esdo->rel.hdr->sh_size);
2487 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2488 * bed->s->int_rels_per_ext_rel);
2489 }
2490
2491 if (esdo->rela.hdr
2492 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2493 external_relocs,
2494 internal_rela_relocs)))
45d6a902
AM
2495 goto error_return;
2496
2497 /* Cache the results for next time, if we can. */
2498 if (keep_memory)
d4730f92 2499 esdo->relocs = internal_relocs;
45d6a902
AM
2500
2501 if (alloc1 != NULL)
2502 free (alloc1);
2503
2504 /* Don't free alloc2, since if it was allocated we are passing it
2505 back (under the name of internal_relocs). */
2506
2507 return internal_relocs;
2508
2509 error_return:
2510 if (alloc1 != NULL)
2511 free (alloc1);
2512 if (alloc2 != NULL)
4dd07732
AM
2513 {
2514 if (keep_memory)
2515 bfd_release (abfd, alloc2);
2516 else
2517 free (alloc2);
2518 }
45d6a902
AM
2519 return NULL;
2520}
2521
2522/* Compute the size of, and allocate space for, REL_HDR which is the
2523 section header for a section containing relocations for O. */
2524
28caa186 2525static bfd_boolean
9eaff861
AO
2526_bfd_elf_link_size_reloc_section (bfd *abfd,
2527 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2528{
9eaff861 2529 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2530
2531 /* That allows us to calculate the size of the section. */
9eaff861 2532 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2533
2534 /* The contents field must last into write_object_contents, so we
2535 allocate it with bfd_alloc rather than malloc. Also since we
2536 cannot be sure that the contents will actually be filled in,
2537 we zero the allocated space. */
a50b1753 2538 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2539 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2540 return FALSE;
2541
d4730f92 2542 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2543 {
2544 struct elf_link_hash_entry **p;
2545
ca4be51c
AM
2546 p = ((struct elf_link_hash_entry **)
2547 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2548 if (p == NULL)
2549 return FALSE;
2550
d4730f92 2551 reldata->hashes = p;
45d6a902
AM
2552 }
2553
2554 return TRUE;
2555}
2556
2557/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2558 originated from the section given by INPUT_REL_HDR) to the
2559 OUTPUT_BFD. */
2560
2561bfd_boolean
268b6b39
AM
2562_bfd_elf_link_output_relocs (bfd *output_bfd,
2563 asection *input_section,
2564 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2565 Elf_Internal_Rela *internal_relocs,
2566 struct elf_link_hash_entry **rel_hash
2567 ATTRIBUTE_UNUSED)
45d6a902
AM
2568{
2569 Elf_Internal_Rela *irela;
2570 Elf_Internal_Rela *irelaend;
2571 bfd_byte *erel;
d4730f92 2572 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2573 asection *output_section;
9c5bfbb7 2574 const struct elf_backend_data *bed;
268b6b39 2575 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2576 struct bfd_elf_section_data *esdo;
45d6a902
AM
2577
2578 output_section = input_section->output_section;
45d6a902 2579
d4730f92
BS
2580 bed = get_elf_backend_data (output_bfd);
2581 esdo = elf_section_data (output_section);
2582 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2583 {
d4730f92
BS
2584 output_reldata = &esdo->rel;
2585 swap_out = bed->s->swap_reloc_out;
45d6a902 2586 }
d4730f92
BS
2587 else if (esdo->rela.hdr
2588 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2589 {
d4730f92
BS
2590 output_reldata = &esdo->rela;
2591 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2592 }
2593 else
2594 {
4eca0228 2595 _bfd_error_handler
695344c0 2596 /* xgettext:c-format */
d003868e
AM
2597 (_("%B: relocation size mismatch in %B section %A"),
2598 output_bfd, input_section->owner, input_section);
297d8443 2599 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2600 return FALSE;
2601 }
2602
d4730f92
BS
2603 erel = output_reldata->hdr->contents;
2604 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2605 irela = internal_relocs;
2606 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2607 * bed->s->int_rels_per_ext_rel);
2608 while (irela < irelaend)
2609 {
2610 (*swap_out) (output_bfd, irela, erel);
2611 irela += bed->s->int_rels_per_ext_rel;
2612 erel += input_rel_hdr->sh_entsize;
2613 }
2614
2615 /* Bump the counter, so that we know where to add the next set of
2616 relocations. */
d4730f92 2617 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2618
2619 return TRUE;
2620}
2621\f
508c3946
L
2622/* Make weak undefined symbols in PIE dynamic. */
2623
2624bfd_boolean
2625_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2626 struct elf_link_hash_entry *h)
2627{
0e1862bb 2628 if (bfd_link_pie (info)
508c3946
L
2629 && h->dynindx == -1
2630 && h->root.type == bfd_link_hash_undefweak)
2631 return bfd_elf_link_record_dynamic_symbol (info, h);
2632
2633 return TRUE;
2634}
2635
45d6a902
AM
2636/* Fix up the flags for a symbol. This handles various cases which
2637 can only be fixed after all the input files are seen. This is
2638 currently called by both adjust_dynamic_symbol and
2639 assign_sym_version, which is unnecessary but perhaps more robust in
2640 the face of future changes. */
2641
28caa186 2642static bfd_boolean
268b6b39
AM
2643_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2644 struct elf_info_failed *eif)
45d6a902 2645{
33774f08 2646 const struct elf_backend_data *bed;
508c3946 2647
45d6a902
AM
2648 /* If this symbol was mentioned in a non-ELF file, try to set
2649 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2650 permit a non-ELF file to correctly refer to a symbol defined in
2651 an ELF dynamic object. */
f5385ebf 2652 if (h->non_elf)
45d6a902
AM
2653 {
2654 while (h->root.type == bfd_link_hash_indirect)
2655 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2656
2657 if (h->root.type != bfd_link_hash_defined
2658 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2659 {
2660 h->ref_regular = 1;
2661 h->ref_regular_nonweak = 1;
2662 }
45d6a902
AM
2663 else
2664 {
2665 if (h->root.u.def.section->owner != NULL
2666 && (bfd_get_flavour (h->root.u.def.section->owner)
2667 == bfd_target_elf_flavour))
f5385ebf
AM
2668 {
2669 h->ref_regular = 1;
2670 h->ref_regular_nonweak = 1;
2671 }
45d6a902 2672 else
f5385ebf 2673 h->def_regular = 1;
45d6a902
AM
2674 }
2675
2676 if (h->dynindx == -1
f5385ebf
AM
2677 && (h->def_dynamic
2678 || h->ref_dynamic))
45d6a902 2679 {
c152c796 2680 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2681 {
2682 eif->failed = TRUE;
2683 return FALSE;
2684 }
2685 }
2686 }
2687 else
2688 {
f5385ebf 2689 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2690 was first seen in a non-ELF file. Fortunately, if the symbol
2691 was first seen in an ELF file, we're probably OK unless the
2692 symbol was defined in a non-ELF file. Catch that case here.
2693 FIXME: We're still in trouble if the symbol was first seen in
2694 a dynamic object, and then later in a non-ELF regular object. */
2695 if ((h->root.type == bfd_link_hash_defined
2696 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2697 && !h->def_regular
45d6a902
AM
2698 && (h->root.u.def.section->owner != NULL
2699 ? (bfd_get_flavour (h->root.u.def.section->owner)
2700 != bfd_target_elf_flavour)
2701 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2702 && !h->def_dynamic)))
2703 h->def_regular = 1;
45d6a902
AM
2704 }
2705
508c3946 2706 /* Backend specific symbol fixup. */
33774f08
AM
2707 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2708 if (bed->elf_backend_fixup_symbol
2709 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2710 return FALSE;
508c3946 2711
45d6a902
AM
2712 /* If this is a final link, and the symbol was defined as a common
2713 symbol in a regular object file, and there was no definition in
2714 any dynamic object, then the linker will have allocated space for
f5385ebf 2715 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2716 flag will not have been set. */
2717 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2718 && !h->def_regular
2719 && h->ref_regular
2720 && !h->def_dynamic
96f29d96 2721 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2722 h->def_regular = 1;
45d6a902 2723
4deb8f71
L
2724 /* If a weak undefined symbol has non-default visibility, we also
2725 hide it from the dynamic linker. */
2726 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2727 && h->root.type == bfd_link_hash_undefweak)
2728 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2729
2730 /* A hidden versioned symbol in executable should be forced local if
2731 it is is locally defined, not referenced by shared library and not
2732 exported. */
2733 else if (bfd_link_executable (eif->info)
2734 && h->versioned == versioned_hidden
2735 && !eif->info->export_dynamic
2736 && !h->dynamic
2737 && !h->ref_dynamic
2738 && h->def_regular)
2739 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2740
45d6a902
AM
2741 /* If -Bsymbolic was used (which means to bind references to global
2742 symbols to the definition within the shared object), and this
2743 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2744 need a PLT entry. Likewise, if the symbol has non-default
2745 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2746 will force it local. */
4deb8f71
L
2747 else if (h->needs_plt
2748 && bfd_link_pic (eif->info)
2749 && is_elf_hash_table (eif->info->hash)
2750 && (SYMBOLIC_BIND (eif->info, h)
2751 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2752 && h->def_regular)
45d6a902 2753 {
45d6a902
AM
2754 bfd_boolean force_local;
2755
45d6a902
AM
2756 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2757 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2758 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2759 }
2760
45d6a902
AM
2761 /* If this is a weak defined symbol in a dynamic object, and we know
2762 the real definition in the dynamic object, copy interesting flags
2763 over to the real definition. */
f6e332e6 2764 if (h->u.weakdef != NULL)
45d6a902 2765 {
45d6a902
AM
2766 /* If the real definition is defined by a regular object file,
2767 don't do anything special. See the longer description in
2768 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2769 if (h->u.weakdef->def_regular)
f6e332e6 2770 h->u.weakdef = NULL;
45d6a902 2771 else
a26587ba 2772 {
4e6b54a6
AM
2773 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2774
2775 while (h->root.type == bfd_link_hash_indirect)
2776 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2777
2778 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2779 || h->root.type == bfd_link_hash_defweak);
2780 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2781 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2782 || weakdef->root.type == bfd_link_hash_defweak);
2783 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2784 }
45d6a902
AM
2785 }
2786
2787 return TRUE;
2788}
2789
2790/* Make the backend pick a good value for a dynamic symbol. This is
2791 called via elf_link_hash_traverse, and also calls itself
2792 recursively. */
2793
28caa186 2794static bfd_boolean
268b6b39 2795_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2796{
a50b1753 2797 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2798 bfd *dynobj;
9c5bfbb7 2799 const struct elf_backend_data *bed;
45d6a902 2800
0eddce27 2801 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2802 return FALSE;
2803
45d6a902
AM
2804 /* Ignore indirect symbols. These are added by the versioning code. */
2805 if (h->root.type == bfd_link_hash_indirect)
2806 return TRUE;
2807
2808 /* Fix the symbol flags. */
2809 if (! _bfd_elf_fix_symbol_flags (h, eif))
2810 return FALSE;
2811
954b63d4
AM
2812 if (h->root.type == bfd_link_hash_undefweak)
2813 {
2814 if (eif->info->dynamic_undefined_weak == 0)
2815 _bfd_elf_link_hash_hide_symbol (eif->info, h, TRUE);
2816 else if (eif->info->dynamic_undefined_weak > 0
2817 && h->ref_regular
2818 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2819 && !bfd_hide_sym_by_version (eif->info->version_info,
2820 h->root.root.string))
2821 {
2822 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2823 {
2824 eif->failed = TRUE;
2825 return FALSE;
2826 }
2827 }
2828 }
2829
45d6a902
AM
2830 /* If this symbol does not require a PLT entry, and it is not
2831 defined by a dynamic object, or is not referenced by a regular
2832 object, ignore it. We do have to handle a weak defined symbol,
2833 even if no regular object refers to it, if we decided to add it
2834 to the dynamic symbol table. FIXME: Do we normally need to worry
2835 about symbols which are defined by one dynamic object and
2836 referenced by another one? */
f5385ebf 2837 if (!h->needs_plt
91e21fb7 2838 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2839 && (h->def_regular
2840 || !h->def_dynamic
2841 || (!h->ref_regular
f6e332e6 2842 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2843 {
a6aa5195 2844 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2845 return TRUE;
2846 }
2847
2848 /* If we've already adjusted this symbol, don't do it again. This
2849 can happen via a recursive call. */
f5385ebf 2850 if (h->dynamic_adjusted)
45d6a902
AM
2851 return TRUE;
2852
2853 /* Don't look at this symbol again. Note that we must set this
2854 after checking the above conditions, because we may look at a
2855 symbol once, decide not to do anything, and then get called
2856 recursively later after REF_REGULAR is set below. */
f5385ebf 2857 h->dynamic_adjusted = 1;
45d6a902
AM
2858
2859 /* If this is a weak definition, and we know a real definition, and
2860 the real symbol is not itself defined by a regular object file,
2861 then get a good value for the real definition. We handle the
2862 real symbol first, for the convenience of the backend routine.
2863
2864 Note that there is a confusing case here. If the real definition
2865 is defined by a regular object file, we don't get the real symbol
2866 from the dynamic object, but we do get the weak symbol. If the
2867 processor backend uses a COPY reloc, then if some routine in the
2868 dynamic object changes the real symbol, we will not see that
2869 change in the corresponding weak symbol. This is the way other
2870 ELF linkers work as well, and seems to be a result of the shared
2871 library model.
2872
2873 I will clarify this issue. Most SVR4 shared libraries define the
2874 variable _timezone and define timezone as a weak synonym. The
2875 tzset call changes _timezone. If you write
2876 extern int timezone;
2877 int _timezone = 5;
2878 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2879 you might expect that, since timezone is a synonym for _timezone,
2880 the same number will print both times. However, if the processor
2881 backend uses a COPY reloc, then actually timezone will be copied
2882 into your process image, and, since you define _timezone
2883 yourself, _timezone will not. Thus timezone and _timezone will
2884 wind up at different memory locations. The tzset call will set
2885 _timezone, leaving timezone unchanged. */
2886
f6e332e6 2887 if (h->u.weakdef != NULL)
45d6a902 2888 {
ec24dc88
AM
2889 /* If we get to this point, there is an implicit reference to
2890 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2891 h->u.weakdef->ref_regular = 1;
45d6a902 2892
ec24dc88
AM
2893 /* Ensure that the backend adjust_dynamic_symbol function sees
2894 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2895 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2896 return FALSE;
2897 }
2898
2899 /* If a symbol has no type and no size and does not require a PLT
2900 entry, then we are probably about to do the wrong thing here: we
2901 are probably going to create a COPY reloc for an empty object.
2902 This case can arise when a shared object is built with assembly
2903 code, and the assembly code fails to set the symbol type. */
2904 if (h->size == 0
2905 && h->type == STT_NOTYPE
f5385ebf 2906 && !h->needs_plt)
4eca0228 2907 _bfd_error_handler
45d6a902
AM
2908 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2909 h->root.root.string);
2910
2911 dynobj = elf_hash_table (eif->info)->dynobj;
2912 bed = get_elf_backend_data (dynobj);
e7c33416 2913
45d6a902
AM
2914 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2915 {
2916 eif->failed = TRUE;
2917 return FALSE;
2918 }
2919
2920 return TRUE;
2921}
2922
027297b7
L
2923/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2924 DYNBSS. */
2925
2926bfd_boolean
6cabe1ea
AM
2927_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2928 struct elf_link_hash_entry *h,
027297b7
L
2929 asection *dynbss)
2930{
91ac5911 2931 unsigned int power_of_two;
027297b7
L
2932 bfd_vma mask;
2933 asection *sec = h->root.u.def.section;
2934
2935 /* The section aligment of definition is the maximum alignment
91ac5911
L
2936 requirement of symbols defined in the section. Since we don't
2937 know the symbol alignment requirement, we start with the
2938 maximum alignment and check low bits of the symbol address
2939 for the minimum alignment. */
2940 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2941 mask = ((bfd_vma) 1 << power_of_two) - 1;
2942 while ((h->root.u.def.value & mask) != 0)
2943 {
2944 mask >>= 1;
2945 --power_of_two;
2946 }
027297b7 2947
91ac5911
L
2948 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2949 dynbss))
027297b7
L
2950 {
2951 /* Adjust the section alignment if needed. */
2952 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2953 power_of_two))
027297b7
L
2954 return FALSE;
2955 }
2956
91ac5911 2957 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2958 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2959
2960 /* Define the symbol as being at this point in DYNBSS. */
2961 h->root.u.def.section = dynbss;
2962 h->root.u.def.value = dynbss->size;
2963
2964 /* Increment the size of DYNBSS to make room for the symbol. */
2965 dynbss->size += h->size;
2966
f7483970
L
2967 /* No error if extern_protected_data is true. */
2968 if (h->protected_def
889c2a67
L
2969 && (!info->extern_protected_data
2970 || (info->extern_protected_data < 0
2971 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2972 info->callbacks->einfo
2973 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2974 h->root.root.string);
6cabe1ea 2975
027297b7
L
2976 return TRUE;
2977}
2978
45d6a902
AM
2979/* Adjust all external symbols pointing into SEC_MERGE sections
2980 to reflect the object merging within the sections. */
2981
28caa186 2982static bfd_boolean
268b6b39 2983_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2984{
2985 asection *sec;
2986
45d6a902
AM
2987 if ((h->root.type == bfd_link_hash_defined
2988 || h->root.type == bfd_link_hash_defweak)
2989 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2990 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2991 {
a50b1753 2992 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2993
2994 h->root.u.def.value =
2995 _bfd_merged_section_offset (output_bfd,
2996 &h->root.u.def.section,
2997 elf_section_data (sec)->sec_info,
753731ee 2998 h->root.u.def.value);
45d6a902
AM
2999 }
3000
3001 return TRUE;
3002}
986a241f
RH
3003
3004/* Returns false if the symbol referred to by H should be considered
3005 to resolve local to the current module, and true if it should be
3006 considered to bind dynamically. */
3007
3008bfd_boolean
268b6b39
AM
3009_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3010 struct bfd_link_info *info,
89a2ee5a 3011 bfd_boolean not_local_protected)
986a241f
RH
3012{
3013 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3014 const struct elf_backend_data *bed;
3015 struct elf_link_hash_table *hash_table;
986a241f
RH
3016
3017 if (h == NULL)
3018 return FALSE;
3019
3020 while (h->root.type == bfd_link_hash_indirect
3021 || h->root.type == bfd_link_hash_warning)
3022 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3023
3024 /* If it was forced local, then clearly it's not dynamic. */
3025 if (h->dynindx == -1)
3026 return FALSE;
f5385ebf 3027 if (h->forced_local)
986a241f
RH
3028 return FALSE;
3029
3030 /* Identify the cases where name binding rules say that a
3031 visible symbol resolves locally. */
0e1862bb
L
3032 binding_stays_local_p = (bfd_link_executable (info)
3033 || SYMBOLIC_BIND (info, h));
986a241f
RH
3034
3035 switch (ELF_ST_VISIBILITY (h->other))
3036 {
3037 case STV_INTERNAL:
3038 case STV_HIDDEN:
3039 return FALSE;
3040
3041 case STV_PROTECTED:
fcb93ecf
PB
3042 hash_table = elf_hash_table (info);
3043 if (!is_elf_hash_table (hash_table))
3044 return FALSE;
3045
3046 bed = get_elf_backend_data (hash_table->dynobj);
3047
986a241f
RH
3048 /* Proper resolution for function pointer equality may require
3049 that these symbols perhaps be resolved dynamically, even though
3050 we should be resolving them to the current module. */
89a2ee5a 3051 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3052 binding_stays_local_p = TRUE;
3053 break;
3054
3055 default:
986a241f
RH
3056 break;
3057 }
3058
aa37626c 3059 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3060 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3061 return TRUE;
3062
986a241f
RH
3063 /* Otherwise, the symbol is dynamic if binding rules don't tell
3064 us that it remains local. */
3065 return !binding_stays_local_p;
3066}
f6c52c13
AM
3067
3068/* Return true if the symbol referred to by H should be considered
3069 to resolve local to the current module, and false otherwise. Differs
3070 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3071 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3072 for the place where dynindx == -1 is tested. If that test is true,
3073 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3074 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3075 defined symbols.
89a2ee5a
AM
3076 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3077 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3078 treatment of undefined weak symbols. For those that do not make
3079 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3080
3081bfd_boolean
268b6b39
AM
3082_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3083 struct bfd_link_info *info,
3084 bfd_boolean local_protected)
f6c52c13 3085{
fcb93ecf
PB
3086 const struct elf_backend_data *bed;
3087 struct elf_link_hash_table *hash_table;
3088
f6c52c13
AM
3089 /* If it's a local sym, of course we resolve locally. */
3090 if (h == NULL)
3091 return TRUE;
3092
d95edcac
L
3093 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3094 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3095 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3096 return TRUE;
3097
0fad2956
MR
3098 /* Forced local symbols resolve locally. */
3099 if (h->forced_local)
3100 return TRUE;
3101
7e2294f9
AO
3102 /* Common symbols that become definitions don't get the DEF_REGULAR
3103 flag set, so test it first, and don't bail out. */
3104 if (ELF_COMMON_DEF_P (h))
3105 /* Do nothing. */;
f6c52c13 3106 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3107 resolve locally. The sym is either undefined or dynamic. */
3108 else if (!h->def_regular)
f6c52c13
AM
3109 return FALSE;
3110
0fad2956 3111 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3112 if (h->dynindx == -1)
3113 return TRUE;
3114
3115 /* At this point, we know the symbol is defined and dynamic. In an
3116 executable it must resolve locally, likewise when building symbolic
3117 shared libraries. */
0e1862bb 3118 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3119 return TRUE;
3120
3121 /* Now deal with defined dynamic symbols in shared libraries. Ones
3122 with default visibility might not resolve locally. */
3123 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3124 return FALSE;
3125
fcb93ecf
PB
3126 hash_table = elf_hash_table (info);
3127 if (!is_elf_hash_table (hash_table))
3128 return TRUE;
3129
3130 bed = get_elf_backend_data (hash_table->dynobj);
3131
f7483970
L
3132 /* If extern_protected_data is false, STV_PROTECTED non-function
3133 symbols are local. */
889c2a67
L
3134 if ((!info->extern_protected_data
3135 || (info->extern_protected_data < 0
3136 && !bed->extern_protected_data))
3137 && !bed->is_function_type (h->type))
1c16dfa5
L
3138 return TRUE;
3139
f6c52c13 3140 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3141 symbols be treated as dynamic symbols. If the address of a
3142 function not defined in an executable is set to that function's
3143 plt entry in the executable, then the address of the function in
3144 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3145 return local_protected;
3146}
e1918d23
AM
3147
3148/* Caches some TLS segment info, and ensures that the TLS segment vma is
3149 aligned. Returns the first TLS output section. */
3150
3151struct bfd_section *
3152_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3153{
3154 struct bfd_section *sec, *tls;
3155 unsigned int align = 0;
3156
3157 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3158 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3159 break;
3160 tls = sec;
3161
3162 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3163 if (sec->alignment_power > align)
3164 align = sec->alignment_power;
3165
3166 elf_hash_table (info)->tls_sec = tls;
3167
3168 /* Ensure the alignment of the first section is the largest alignment,
3169 so that the tls segment starts aligned. */
3170 if (tls != NULL)
3171 tls->alignment_power = align;
3172
3173 return tls;
3174}
0ad989f9
L
3175
3176/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3177static bfd_boolean
3178is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3179 Elf_Internal_Sym *sym)
3180{
a4d8e49b
L
3181 const struct elf_backend_data *bed;
3182
0ad989f9
L
3183 /* Local symbols do not count, but target specific ones might. */
3184 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3185 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3186 return FALSE;
3187
fcb93ecf 3188 bed = get_elf_backend_data (abfd);
0ad989f9 3189 /* Function symbols do not count. */
fcb93ecf 3190 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3191 return FALSE;
3192
3193 /* If the section is undefined, then so is the symbol. */
3194 if (sym->st_shndx == SHN_UNDEF)
3195 return FALSE;
3196
3197 /* If the symbol is defined in the common section, then
3198 it is a common definition and so does not count. */
a4d8e49b 3199 if (bed->common_definition (sym))
0ad989f9
L
3200 return FALSE;
3201
3202 /* If the symbol is in a target specific section then we
3203 must rely upon the backend to tell us what it is. */
3204 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3205 /* FIXME - this function is not coded yet:
3206
3207 return _bfd_is_global_symbol_definition (abfd, sym);
3208
3209 Instead for now assume that the definition is not global,
3210 Even if this is wrong, at least the linker will behave
3211 in the same way that it used to do. */
3212 return FALSE;
3213
3214 return TRUE;
3215}
3216
3217/* Search the symbol table of the archive element of the archive ABFD
3218 whose archive map contains a mention of SYMDEF, and determine if
3219 the symbol is defined in this element. */
3220static bfd_boolean
3221elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3222{
3223 Elf_Internal_Shdr * hdr;
ef53be89
AM
3224 size_t symcount;
3225 size_t extsymcount;
3226 size_t extsymoff;
0ad989f9
L
3227 Elf_Internal_Sym *isymbuf;
3228 Elf_Internal_Sym *isym;
3229 Elf_Internal_Sym *isymend;
3230 bfd_boolean result;
3231
3232 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3233 if (abfd == NULL)
3234 return FALSE;
3235
3236 if (! bfd_check_format (abfd, bfd_object))
3237 return FALSE;
3238
7dc3990e
L
3239 /* Select the appropriate symbol table. If we don't know if the
3240 object file is an IR object, give linker LTO plugin a chance to
3241 get the correct symbol table. */
3242 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3243#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3244 || (abfd->plugin_format == bfd_plugin_unknown
3245 && bfd_link_plugin_object_p (abfd))
3246#endif
3247 )
3248 {
3249 /* Use the IR symbol table if the object has been claimed by
3250 plugin. */
3251 abfd = abfd->plugin_dummy_bfd;
3252 hdr = &elf_tdata (abfd)->symtab_hdr;
3253 }
3254 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3255 hdr = &elf_tdata (abfd)->symtab_hdr;
3256 else
3257 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3258
3259 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3260
3261 /* The sh_info field of the symtab header tells us where the
3262 external symbols start. We don't care about the local symbols. */
3263 if (elf_bad_symtab (abfd))
3264 {
3265 extsymcount = symcount;
3266 extsymoff = 0;
3267 }
3268 else
3269 {
3270 extsymcount = symcount - hdr->sh_info;
3271 extsymoff = hdr->sh_info;
3272 }
3273
3274 if (extsymcount == 0)
3275 return FALSE;
3276
3277 /* Read in the symbol table. */
3278 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3279 NULL, NULL, NULL);
3280 if (isymbuf == NULL)
3281 return FALSE;
3282
3283 /* Scan the symbol table looking for SYMDEF. */
3284 result = FALSE;
3285 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3286 {
3287 const char *name;
3288
3289 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3290 isym->st_name);
3291 if (name == NULL)
3292 break;
3293
3294 if (strcmp (name, symdef->name) == 0)
3295 {
3296 result = is_global_data_symbol_definition (abfd, isym);
3297 break;
3298 }
3299 }
3300
3301 free (isymbuf);
3302
3303 return result;
3304}
3305\f
5a580b3a
AM
3306/* Add an entry to the .dynamic table. */
3307
3308bfd_boolean
3309_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3310 bfd_vma tag,
3311 bfd_vma val)
3312{
3313 struct elf_link_hash_table *hash_table;
3314 const struct elf_backend_data *bed;
3315 asection *s;
3316 bfd_size_type newsize;
3317 bfd_byte *newcontents;
3318 Elf_Internal_Dyn dyn;
3319
3320 hash_table = elf_hash_table (info);
3321 if (! is_elf_hash_table (hash_table))
3322 return FALSE;
3323
3324 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3325 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3326 BFD_ASSERT (s != NULL);
3327
eea6121a 3328 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3329 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3330 if (newcontents == NULL)
3331 return FALSE;
3332
3333 dyn.d_tag = tag;
3334 dyn.d_un.d_val = val;
eea6121a 3335 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3336
eea6121a 3337 s->size = newsize;
5a580b3a
AM
3338 s->contents = newcontents;
3339
3340 return TRUE;
3341}
3342
3343/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3344 otherwise just check whether one already exists. Returns -1 on error,
3345 1 if a DT_NEEDED tag already exists, and 0 on success. */
3346
4ad4eba5 3347static int
7e9f0867
AM
3348elf_add_dt_needed_tag (bfd *abfd,
3349 struct bfd_link_info *info,
4ad4eba5
AM
3350 const char *soname,
3351 bfd_boolean do_it)
5a580b3a
AM
3352{
3353 struct elf_link_hash_table *hash_table;
ef53be89 3354 size_t strindex;
5a580b3a 3355
7e9f0867
AM
3356 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3357 return -1;
3358
5a580b3a 3359 hash_table = elf_hash_table (info);
5a580b3a 3360 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3361 if (strindex == (size_t) -1)
5a580b3a
AM
3362 return -1;
3363
02be4619 3364 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3365 {
3366 asection *sdyn;
3367 const struct elf_backend_data *bed;
3368 bfd_byte *extdyn;
3369
3370 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3371 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3372 if (sdyn != NULL)
3373 for (extdyn = sdyn->contents;
3374 extdyn < sdyn->contents + sdyn->size;
3375 extdyn += bed->s->sizeof_dyn)
3376 {
3377 Elf_Internal_Dyn dyn;
5a580b3a 3378
7e9f0867
AM
3379 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3380 if (dyn.d_tag == DT_NEEDED
3381 && dyn.d_un.d_val == strindex)
3382 {
3383 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3384 return 1;
3385 }
3386 }
5a580b3a
AM
3387 }
3388
3389 if (do_it)
3390 {
7e9f0867
AM
3391 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3392 return -1;
3393
5a580b3a
AM
3394 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3395 return -1;
3396 }
3397 else
3398 /* We were just checking for existence of the tag. */
3399 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3400
3401 return 0;
3402}
3403
7b15fa7a
AM
3404/* Return true if SONAME is on the needed list between NEEDED and STOP
3405 (or the end of list if STOP is NULL), and needed by a library that
3406 will be loaded. */
3407
010e5ae2 3408static bfd_boolean
7b15fa7a
AM
3409on_needed_list (const char *soname,
3410 struct bfd_link_needed_list *needed,
3411 struct bfd_link_needed_list *stop)
010e5ae2 3412{
7b15fa7a
AM
3413 struct bfd_link_needed_list *look;
3414 for (look = needed; look != stop; look = look->next)
3415 if (strcmp (soname, look->name) == 0
3416 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3417 /* If needed by a library that itself is not directly
3418 needed, recursively check whether that library is
3419 indirectly needed. Since we add DT_NEEDED entries to
3420 the end of the list, library dependencies appear after
3421 the library. Therefore search prior to the current
3422 LOOK, preventing possible infinite recursion. */
3423 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3424 return TRUE;
3425
3426 return FALSE;
3427}
3428
14160578 3429/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3430static int
3431elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3432{
3433 const struct elf_link_hash_entry *h1;
3434 const struct elf_link_hash_entry *h2;
10b7e05b 3435 bfd_signed_vma vdiff;
5a580b3a
AM
3436
3437 h1 = *(const struct elf_link_hash_entry **) arg1;
3438 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3439 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3440 if (vdiff != 0)
3441 return vdiff > 0 ? 1 : -1;
3442 else
3443 {
d3435ae8 3444 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3445 if (sdiff != 0)
3446 return sdiff > 0 ? 1 : -1;
3447 }
14160578
AM
3448 vdiff = h1->size - h2->size;
3449 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3450}
4ad4eba5 3451
5a580b3a
AM
3452/* This function is used to adjust offsets into .dynstr for
3453 dynamic symbols. This is called via elf_link_hash_traverse. */
3454
3455static bfd_boolean
3456elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3457{
a50b1753 3458 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3459
5a580b3a
AM
3460 if (h->dynindx != -1)
3461 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3462 return TRUE;
3463}
3464
3465/* Assign string offsets in .dynstr, update all structures referencing
3466 them. */
3467
4ad4eba5
AM
3468static bfd_boolean
3469elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3470{
3471 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3472 struct elf_link_local_dynamic_entry *entry;
3473 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3474 bfd *dynobj = hash_table->dynobj;
3475 asection *sdyn;
3476 bfd_size_type size;
3477 const struct elf_backend_data *bed;
3478 bfd_byte *extdyn;
3479
3480 _bfd_elf_strtab_finalize (dynstr);
3481 size = _bfd_elf_strtab_size (dynstr);
3482
3483 bed = get_elf_backend_data (dynobj);
3d4d4302 3484 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3485 BFD_ASSERT (sdyn != NULL);
3486
3487 /* Update all .dynamic entries referencing .dynstr strings. */
3488 for (extdyn = sdyn->contents;
eea6121a 3489 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3490 extdyn += bed->s->sizeof_dyn)
3491 {
3492 Elf_Internal_Dyn dyn;
3493
3494 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3495 switch (dyn.d_tag)
3496 {
3497 case DT_STRSZ:
3498 dyn.d_un.d_val = size;
3499 break;
3500 case DT_NEEDED:
3501 case DT_SONAME:
3502 case DT_RPATH:
3503 case DT_RUNPATH:
3504 case DT_FILTER:
3505 case DT_AUXILIARY:
7ee314fa
AM
3506 case DT_AUDIT:
3507 case DT_DEPAUDIT:
5a580b3a
AM
3508 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3509 break;
3510 default:
3511 continue;
3512 }
3513 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3514 }
3515
3516 /* Now update local dynamic symbols. */
3517 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3518 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3519 entry->isym.st_name);
3520
3521 /* And the rest of dynamic symbols. */
3522 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3523
3524 /* Adjust version definitions. */
3525 if (elf_tdata (output_bfd)->cverdefs)
3526 {
3527 asection *s;
3528 bfd_byte *p;
ef53be89 3529 size_t i;
5a580b3a
AM
3530 Elf_Internal_Verdef def;
3531 Elf_Internal_Verdaux defaux;
3532
3d4d4302 3533 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3534 p = s->contents;
3535 do
3536 {
3537 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3538 &def);
3539 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3540 if (def.vd_aux != sizeof (Elf_External_Verdef))
3541 continue;
5a580b3a
AM
3542 for (i = 0; i < def.vd_cnt; ++i)
3543 {
3544 _bfd_elf_swap_verdaux_in (output_bfd,
3545 (Elf_External_Verdaux *) p, &defaux);
3546 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3547 defaux.vda_name);
3548 _bfd_elf_swap_verdaux_out (output_bfd,
3549 &defaux, (Elf_External_Verdaux *) p);
3550 p += sizeof (Elf_External_Verdaux);
3551 }
3552 }
3553 while (def.vd_next);
3554 }
3555
3556 /* Adjust version references. */
3557 if (elf_tdata (output_bfd)->verref)
3558 {
3559 asection *s;
3560 bfd_byte *p;
ef53be89 3561 size_t i;
5a580b3a
AM
3562 Elf_Internal_Verneed need;
3563 Elf_Internal_Vernaux needaux;
3564
3d4d4302 3565 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3566 p = s->contents;
3567 do
3568 {
3569 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3570 &need);
3571 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3572 _bfd_elf_swap_verneed_out (output_bfd, &need,
3573 (Elf_External_Verneed *) p);
3574 p += sizeof (Elf_External_Verneed);
3575 for (i = 0; i < need.vn_cnt; ++i)
3576 {
3577 _bfd_elf_swap_vernaux_in (output_bfd,
3578 (Elf_External_Vernaux *) p, &needaux);
3579 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3580 needaux.vna_name);
3581 _bfd_elf_swap_vernaux_out (output_bfd,
3582 &needaux,
3583 (Elf_External_Vernaux *) p);
3584 p += sizeof (Elf_External_Vernaux);
3585 }
3586 }
3587 while (need.vn_next);
3588 }
3589
3590 return TRUE;
3591}
3592\f
13285a1b
AM
3593/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3594 The default is to only match when the INPUT and OUTPUT are exactly
3595 the same target. */
3596
3597bfd_boolean
3598_bfd_elf_default_relocs_compatible (const bfd_target *input,
3599 const bfd_target *output)
3600{
3601 return input == output;
3602}
3603
3604/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3605 This version is used when different targets for the same architecture
3606 are virtually identical. */
3607
3608bfd_boolean
3609_bfd_elf_relocs_compatible (const bfd_target *input,
3610 const bfd_target *output)
3611{
3612 const struct elf_backend_data *obed, *ibed;
3613
3614 if (input == output)
3615 return TRUE;
3616
3617 ibed = xvec_get_elf_backend_data (input);
3618 obed = xvec_get_elf_backend_data (output);
3619
3620 if (ibed->arch != obed->arch)
3621 return FALSE;
3622
3623 /* If both backends are using this function, deem them compatible. */
3624 return ibed->relocs_compatible == obed->relocs_compatible;
3625}
3626
e5034e59
AM
3627/* Make a special call to the linker "notice" function to tell it that
3628 we are about to handle an as-needed lib, or have finished
1b786873 3629 processing the lib. */
e5034e59
AM
3630
3631bfd_boolean
3632_bfd_elf_notice_as_needed (bfd *ibfd,
3633 struct bfd_link_info *info,
3634 enum notice_asneeded_action act)
3635{
46135103 3636 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3637}
3638
d9689752
L
3639/* Check relocations an ELF object file. */
3640
3641bfd_boolean
3642_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3643{
3644 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3645 struct elf_link_hash_table *htab = elf_hash_table (info);
3646
3647 /* If this object is the same format as the output object, and it is
3648 not a shared library, then let the backend look through the
3649 relocs.
3650
3651 This is required to build global offset table entries and to
3652 arrange for dynamic relocs. It is not required for the
3653 particular common case of linking non PIC code, even when linking
3654 against shared libraries, but unfortunately there is no way of
3655 knowing whether an object file has been compiled PIC or not.
3656 Looking through the relocs is not particularly time consuming.
3657 The problem is that we must either (1) keep the relocs in memory,
3658 which causes the linker to require additional runtime memory or
3659 (2) read the relocs twice from the input file, which wastes time.
3660 This would be a good case for using mmap.
3661
3662 I have no idea how to handle linking PIC code into a file of a
3663 different format. It probably can't be done. */
3664 if ((abfd->flags & DYNAMIC) == 0
3665 && is_elf_hash_table (htab)
3666 && bed->check_relocs != NULL
3667 && elf_object_id (abfd) == elf_hash_table_id (htab)
3668 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3669 {
3670 asection *o;
3671
3672 for (o = abfd->sections; o != NULL; o = o->next)
3673 {
3674 Elf_Internal_Rela *internal_relocs;
3675 bfd_boolean ok;
3676
5ce03cea 3677 /* Don't check relocations in excluded sections. */
d9689752 3678 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3679 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3680 || o->reloc_count == 0
3681 || ((info->strip == strip_all || info->strip == strip_debugger)
3682 && (o->flags & SEC_DEBUGGING) != 0)
3683 || bfd_is_abs_section (o->output_section))
3684 continue;
3685
3686 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3687 info->keep_memory);
3688 if (internal_relocs == NULL)
3689 return FALSE;
3690
3691 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3692
3693 if (elf_section_data (o)->relocs != internal_relocs)
3694 free (internal_relocs);
3695
3696 if (! ok)
3697 return FALSE;
3698 }
3699 }
3700
3701 return TRUE;
3702}
3703
4ad4eba5
AM
3704/* Add symbols from an ELF object file to the linker hash table. */
3705
3706static bfd_boolean
3707elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3708{
a0c402a5 3709 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3710 Elf_Internal_Shdr *hdr;
ef53be89
AM
3711 size_t symcount;
3712 size_t extsymcount;
3713 size_t extsymoff;
4ad4eba5
AM
3714 struct elf_link_hash_entry **sym_hash;
3715 bfd_boolean dynamic;
3716 Elf_External_Versym *extversym = NULL;
3717 Elf_External_Versym *ever;
3718 struct elf_link_hash_entry *weaks;
3719 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3720 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3721 Elf_Internal_Sym *isymbuf = NULL;
3722 Elf_Internal_Sym *isym;
3723 Elf_Internal_Sym *isymend;
3724 const struct elf_backend_data *bed;
3725 bfd_boolean add_needed;
66eb6687 3726 struct elf_link_hash_table *htab;
4ad4eba5 3727 bfd_size_type amt;
66eb6687 3728 void *alloc_mark = NULL;
4f87808c
AM
3729 struct bfd_hash_entry **old_table = NULL;
3730 unsigned int old_size = 0;
3731 unsigned int old_count = 0;
66eb6687 3732 void *old_tab = NULL;
66eb6687
AM
3733 void *old_ent;
3734 struct bfd_link_hash_entry *old_undefs = NULL;
3735 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3736 void *old_strtab = NULL;
66eb6687 3737 size_t tabsize = 0;
db6a5d5f 3738 asection *s;
29a9f53e 3739 bfd_boolean just_syms;
4ad4eba5 3740
66eb6687 3741 htab = elf_hash_table (info);
4ad4eba5 3742 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3743
3744 if ((abfd->flags & DYNAMIC) == 0)
3745 dynamic = FALSE;
3746 else
3747 {
3748 dynamic = TRUE;
3749
3750 /* You can't use -r against a dynamic object. Also, there's no
3751 hope of using a dynamic object which does not exactly match
3752 the format of the output file. */
0e1862bb 3753 if (bfd_link_relocatable (info)
66eb6687 3754 || !is_elf_hash_table (htab)
f13a99db 3755 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3756 {
0e1862bb 3757 if (bfd_link_relocatable (info))
9a0789ec
NC
3758 bfd_set_error (bfd_error_invalid_operation);
3759 else
3760 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3761 goto error_return;
3762 }
3763 }
3764
a0c402a5
L
3765 ehdr = elf_elfheader (abfd);
3766 if (info->warn_alternate_em
3767 && bed->elf_machine_code != ehdr->e_machine
3768 && ((bed->elf_machine_alt1 != 0
3769 && ehdr->e_machine == bed->elf_machine_alt1)
3770 || (bed->elf_machine_alt2 != 0
3771 && ehdr->e_machine == bed->elf_machine_alt2)))
3772 info->callbacks->einfo
695344c0 3773 /* xgettext:c-format */
a0c402a5
L
3774 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3775 ehdr->e_machine, abfd, bed->elf_machine_code);
3776
4ad4eba5
AM
3777 /* As a GNU extension, any input sections which are named
3778 .gnu.warning.SYMBOL are treated as warning symbols for the given
3779 symbol. This differs from .gnu.warning sections, which generate
3780 warnings when they are included in an output file. */
dd98f8d2 3781 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3782 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3783 {
db6a5d5f 3784 const char *name;
4ad4eba5 3785
db6a5d5f
AM
3786 name = bfd_get_section_name (abfd, s);
3787 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3788 {
db6a5d5f
AM
3789 char *msg;
3790 bfd_size_type sz;
3791
3792 name += sizeof ".gnu.warning." - 1;
3793
3794 /* If this is a shared object, then look up the symbol
3795 in the hash table. If it is there, and it is already
3796 been defined, then we will not be using the entry
3797 from this shared object, so we don't need to warn.
3798 FIXME: If we see the definition in a regular object
3799 later on, we will warn, but we shouldn't. The only
3800 fix is to keep track of what warnings we are supposed
3801 to emit, and then handle them all at the end of the
3802 link. */
3803 if (dynamic)
4ad4eba5 3804 {
db6a5d5f
AM
3805 struct elf_link_hash_entry *h;
3806
3807 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3808
3809 /* FIXME: What about bfd_link_hash_common? */
3810 if (h != NULL
3811 && (h->root.type == bfd_link_hash_defined
3812 || h->root.type == bfd_link_hash_defweak))
3813 continue;
3814 }
4ad4eba5 3815
db6a5d5f
AM
3816 sz = s->size;
3817 msg = (char *) bfd_alloc (abfd, sz + 1);
3818 if (msg == NULL)
3819 goto error_return;
4ad4eba5 3820
db6a5d5f
AM
3821 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3822 goto error_return;
4ad4eba5 3823
db6a5d5f 3824 msg[sz] = '\0';
4ad4eba5 3825
db6a5d5f
AM
3826 if (! (_bfd_generic_link_add_one_symbol
3827 (info, abfd, name, BSF_WARNING, s, 0, msg,
3828 FALSE, bed->collect, NULL)))
3829 goto error_return;
4ad4eba5 3830
0e1862bb 3831 if (bfd_link_executable (info))
db6a5d5f
AM
3832 {
3833 /* Clobber the section size so that the warning does
3834 not get copied into the output file. */
3835 s->size = 0;
11d2f718 3836
db6a5d5f
AM
3837 /* Also set SEC_EXCLUDE, so that symbols defined in
3838 the warning section don't get copied to the output. */
3839 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3840 }
3841 }
3842 }
3843
29a9f53e
L
3844 just_syms = ((s = abfd->sections) != NULL
3845 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3846
4ad4eba5
AM
3847 add_needed = TRUE;
3848 if (! dynamic)
3849 {
3850 /* If we are creating a shared library, create all the dynamic
3851 sections immediately. We need to attach them to something,
3852 so we attach them to this BFD, provided it is the right
bf89386a
L
3853 format and is not from ld --just-symbols. Always create the
3854 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3855 are no input BFD's of the same format as the output, we can't
3856 make a shared library. */
3857 if (!just_syms
bf89386a 3858 && (bfd_link_pic (info)
9c1d7a08 3859 || (!bfd_link_relocatable (info)
3c5fce9b 3860 && info->nointerp
9c1d7a08 3861 && (info->export_dynamic || info->dynamic)))
66eb6687 3862 && is_elf_hash_table (htab)
f13a99db 3863 && info->output_bfd->xvec == abfd->xvec
66eb6687 3864 && !htab->dynamic_sections_created)
4ad4eba5
AM
3865 {
3866 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3867 goto error_return;
3868 }
3869 }
66eb6687 3870 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3871 goto error_return;
3872 else
3873 {
4ad4eba5 3874 const char *soname = NULL;
7ee314fa 3875 char *audit = NULL;
4ad4eba5 3876 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3877 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3878 int ret;
3879
3880 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3881 ld shouldn't allow it. */
29a9f53e 3882 if (just_syms)
92fd189d 3883 abort ();
4ad4eba5
AM
3884
3885 /* If this dynamic lib was specified on the command line with
3886 --as-needed in effect, then we don't want to add a DT_NEEDED
3887 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3888 in by another lib's DT_NEEDED. When --no-add-needed is used
3889 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3890 any dynamic library in DT_NEEDED tags in the dynamic lib at
3891 all. */
3892 add_needed = (elf_dyn_lib_class (abfd)
3893 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3894 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3895
3896 s = bfd_get_section_by_name (abfd, ".dynamic");
3897 if (s != NULL)
3898 {
3899 bfd_byte *dynbuf;
3900 bfd_byte *extdyn;
cb33740c 3901 unsigned int elfsec;
4ad4eba5
AM
3902 unsigned long shlink;
3903
eea6121a 3904 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3905 {
3906error_free_dyn:
3907 free (dynbuf);
3908 goto error_return;
3909 }
4ad4eba5
AM
3910
3911 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3912 if (elfsec == SHN_BAD)
4ad4eba5
AM
3913 goto error_free_dyn;
3914 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3915
3916 for (extdyn = dynbuf;
eea6121a 3917 extdyn < dynbuf + s->size;
4ad4eba5
AM
3918 extdyn += bed->s->sizeof_dyn)
3919 {
3920 Elf_Internal_Dyn dyn;
3921
3922 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3923 if (dyn.d_tag == DT_SONAME)
3924 {
3925 unsigned int tagv = dyn.d_un.d_val;
3926 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3927 if (soname == NULL)
3928 goto error_free_dyn;
3929 }
3930 if (dyn.d_tag == DT_NEEDED)
3931 {
3932 struct bfd_link_needed_list *n, **pn;
3933 char *fnm, *anm;
3934 unsigned int tagv = dyn.d_un.d_val;
3935
3936 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3937 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3938 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3939 if (n == NULL || fnm == NULL)
3940 goto error_free_dyn;
3941 amt = strlen (fnm) + 1;
a50b1753 3942 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3943 if (anm == NULL)
3944 goto error_free_dyn;
3945 memcpy (anm, fnm, amt);
3946 n->name = anm;
3947 n->by = abfd;
3948 n->next = NULL;
66eb6687 3949 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3950 ;
3951 *pn = n;
3952 }
3953 if (dyn.d_tag == DT_RUNPATH)
3954 {
3955 struct bfd_link_needed_list *n, **pn;
3956 char *fnm, *anm;
3957 unsigned int tagv = dyn.d_un.d_val;
3958
3959 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3960 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3961 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3962 if (n == NULL || fnm == NULL)
3963 goto error_free_dyn;
3964 amt = strlen (fnm) + 1;
a50b1753 3965 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3966 if (anm == NULL)
3967 goto error_free_dyn;
3968 memcpy (anm, fnm, amt);
3969 n->name = anm;
3970 n->by = abfd;
3971 n->next = NULL;
3972 for (pn = & runpath;
3973 *pn != NULL;
3974 pn = &(*pn)->next)
3975 ;
3976 *pn = n;
3977 }
3978 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3979 if (!runpath && dyn.d_tag == DT_RPATH)
3980 {
3981 struct bfd_link_needed_list *n, **pn;
3982 char *fnm, *anm;
3983 unsigned int tagv = dyn.d_un.d_val;
3984
3985 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3986 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3987 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3988 if (n == NULL || fnm == NULL)
3989 goto error_free_dyn;
3990 amt = strlen (fnm) + 1;
a50b1753 3991 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3992 if (anm == NULL)
f8703194 3993 goto error_free_dyn;
4ad4eba5
AM
3994 memcpy (anm, fnm, amt);
3995 n->name = anm;
3996 n->by = abfd;
3997 n->next = NULL;
3998 for (pn = & rpath;
3999 *pn != NULL;
4000 pn = &(*pn)->next)
4001 ;
4002 *pn = n;
4003 }
7ee314fa
AM
4004 if (dyn.d_tag == DT_AUDIT)
4005 {
4006 unsigned int tagv = dyn.d_un.d_val;
4007 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4008 }
4ad4eba5
AM
4009 }
4010
4011 free (dynbuf);
4012 }
4013
4014 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4015 frees all more recently bfd_alloc'd blocks as well. */
4016 if (runpath)
4017 rpath = runpath;
4018
4019 if (rpath)
4020 {
4021 struct bfd_link_needed_list **pn;
66eb6687 4022 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4023 ;
4024 *pn = rpath;
4025 }
4026
9acc85a6
AM
4027 /* If we have a PT_GNU_RELRO program header, mark as read-only
4028 all sections contained fully therein. This makes relro
4029 shared library sections appear as they will at run-time. */
4030 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4031 while (--phdr >= elf_tdata (abfd)->phdr)
4032 if (phdr->p_type == PT_GNU_RELRO)
4033 {
4034 for (s = abfd->sections; s != NULL; s = s->next)
4035 if ((s->flags & SEC_ALLOC) != 0
4036 && s->vma >= phdr->p_vaddr
4037 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4038 s->flags |= SEC_READONLY;
4039 break;
4040 }
4041
4ad4eba5
AM
4042 /* We do not want to include any of the sections in a dynamic
4043 object in the output file. We hack by simply clobbering the
4044 list of sections in the BFD. This could be handled more
4045 cleanly by, say, a new section flag; the existing
4046 SEC_NEVER_LOAD flag is not the one we want, because that one
4047 still implies that the section takes up space in the output
4048 file. */
4049 bfd_section_list_clear (abfd);
4050
4ad4eba5
AM
4051 /* Find the name to use in a DT_NEEDED entry that refers to this
4052 object. If the object has a DT_SONAME entry, we use it.
4053 Otherwise, if the generic linker stuck something in
4054 elf_dt_name, we use that. Otherwise, we just use the file
4055 name. */
4056 if (soname == NULL || *soname == '\0')
4057 {
4058 soname = elf_dt_name (abfd);
4059 if (soname == NULL || *soname == '\0')
4060 soname = bfd_get_filename (abfd);
4061 }
4062
4063 /* Save the SONAME because sometimes the linker emulation code
4064 will need to know it. */
4065 elf_dt_name (abfd) = soname;
4066
7e9f0867 4067 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4068 if (ret < 0)
4069 goto error_return;
4070
4071 /* If we have already included this dynamic object in the
4072 link, just ignore it. There is no reason to include a
4073 particular dynamic object more than once. */
4074 if (ret > 0)
4075 return TRUE;
7ee314fa
AM
4076
4077 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4078 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4079 }
4080
4081 /* If this is a dynamic object, we always link against the .dynsym
4082 symbol table, not the .symtab symbol table. The dynamic linker
4083 will only see the .dynsym symbol table, so there is no reason to
4084 look at .symtab for a dynamic object. */
4085
4086 if (! dynamic || elf_dynsymtab (abfd) == 0)
4087 hdr = &elf_tdata (abfd)->symtab_hdr;
4088 else
4089 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4090
4091 symcount = hdr->sh_size / bed->s->sizeof_sym;
4092
4093 /* The sh_info field of the symtab header tells us where the
4094 external symbols start. We don't care about the local symbols at
4095 this point. */
4096 if (elf_bad_symtab (abfd))
4097 {
4098 extsymcount = symcount;
4099 extsymoff = 0;
4100 }
4101 else
4102 {
4103 extsymcount = symcount - hdr->sh_info;
4104 extsymoff = hdr->sh_info;
4105 }
4106
f45794cb 4107 sym_hash = elf_sym_hashes (abfd);
012b2306 4108 if (extsymcount != 0)
4ad4eba5
AM
4109 {
4110 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4111 NULL, NULL, NULL);
4112 if (isymbuf == NULL)
4113 goto error_return;
4114
4ad4eba5 4115 if (sym_hash == NULL)
012b2306
AM
4116 {
4117 /* We store a pointer to the hash table entry for each
4118 external symbol. */
ef53be89
AM
4119 amt = extsymcount;
4120 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4121 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4122 if (sym_hash == NULL)
4123 goto error_free_sym;
4124 elf_sym_hashes (abfd) = sym_hash;
4125 }
4ad4eba5
AM
4126 }
4127
4128 if (dynamic)
4129 {
4130 /* Read in any version definitions. */
fc0e6df6
PB
4131 if (!_bfd_elf_slurp_version_tables (abfd,
4132 info->default_imported_symver))
4ad4eba5
AM
4133 goto error_free_sym;
4134
4135 /* Read in the symbol versions, but don't bother to convert them
4136 to internal format. */
4137 if (elf_dynversym (abfd) != 0)
4138 {
4139 Elf_Internal_Shdr *versymhdr;
4140
4141 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4142 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4143 if (extversym == NULL)
4144 goto error_free_sym;
4145 amt = versymhdr->sh_size;
4146 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4147 || bfd_bread (extversym, amt, abfd) != amt)
4148 goto error_free_vers;
4149 }
4150 }
4151
66eb6687
AM
4152 /* If we are loading an as-needed shared lib, save the symbol table
4153 state before we start adding symbols. If the lib turns out
4154 to be unneeded, restore the state. */
4155 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4156 {
4157 unsigned int i;
4158 size_t entsize;
4159
4160 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4161 {
4162 struct bfd_hash_entry *p;
2de92251 4163 struct elf_link_hash_entry *h;
66eb6687
AM
4164
4165 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4166 {
4167 h = (struct elf_link_hash_entry *) p;
4168 entsize += htab->root.table.entsize;
4169 if (h->root.type == bfd_link_hash_warning)
4170 entsize += htab->root.table.entsize;
4171 }
66eb6687
AM
4172 }
4173
4174 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4175 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4176 if (old_tab == NULL)
4177 goto error_free_vers;
4178
4179 /* Remember the current objalloc pointer, so that all mem for
4180 symbols added can later be reclaimed. */
4181 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4182 if (alloc_mark == NULL)
4183 goto error_free_vers;
4184
5061a885
AM
4185 /* Make a special call to the linker "notice" function to
4186 tell it that we are about to handle an as-needed lib. */
e5034e59 4187 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4188 goto error_free_vers;
5061a885 4189
f45794cb
AM
4190 /* Clone the symbol table. Remember some pointers into the
4191 symbol table, and dynamic symbol count. */
4192 old_ent = (char *) old_tab + tabsize;
66eb6687 4193 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4194 old_undefs = htab->root.undefs;
4195 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4196 old_table = htab->root.table.table;
4197 old_size = htab->root.table.size;
4198 old_count = htab->root.table.count;
5b677558
AM
4199 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4200 if (old_strtab == NULL)
4201 goto error_free_vers;
66eb6687
AM
4202
4203 for (i = 0; i < htab->root.table.size; i++)
4204 {
4205 struct bfd_hash_entry *p;
2de92251 4206 struct elf_link_hash_entry *h;
66eb6687
AM
4207
4208 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4209 {
4210 memcpy (old_ent, p, htab->root.table.entsize);
4211 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4212 h = (struct elf_link_hash_entry *) p;
4213 if (h->root.type == bfd_link_hash_warning)
4214 {
4215 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4216 old_ent = (char *) old_ent + htab->root.table.entsize;
4217 }
66eb6687
AM
4218 }
4219 }
4220 }
4ad4eba5 4221
66eb6687 4222 weaks = NULL;
4ad4eba5
AM
4223 ever = extversym != NULL ? extversym + extsymoff : NULL;
4224 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4225 isym < isymend;
4226 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4227 {
4228 int bind;
4229 bfd_vma value;
af44c138 4230 asection *sec, *new_sec;
4ad4eba5
AM
4231 flagword flags;
4232 const char *name;
4233 struct elf_link_hash_entry *h;
90c984fc 4234 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4235 bfd_boolean definition;
4236 bfd_boolean size_change_ok;
4237 bfd_boolean type_change_ok;
4238 bfd_boolean new_weakdef;
37a9e49a
L
4239 bfd_boolean new_weak;
4240 bfd_boolean old_weak;
4ad4eba5 4241 bfd_boolean override;
a4d8e49b 4242 bfd_boolean common;
97196564 4243 bfd_boolean discarded;
4ad4eba5
AM
4244 unsigned int old_alignment;
4245 bfd *old_bfd;
6e33951e 4246 bfd_boolean matched;
4ad4eba5
AM
4247
4248 override = FALSE;
4249
4250 flags = BSF_NO_FLAGS;
4251 sec = NULL;
4252 value = isym->st_value;
a4d8e49b 4253 common = bed->common_definition (isym);
97196564 4254 discarded = FALSE;
4ad4eba5
AM
4255
4256 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4257 switch (bind)
4ad4eba5 4258 {
3e7a7d11 4259 case STB_LOCAL:
4ad4eba5
AM
4260 /* This should be impossible, since ELF requires that all
4261 global symbols follow all local symbols, and that sh_info
4262 point to the first global symbol. Unfortunately, Irix 5
4263 screws this up. */
4264 continue;
3e7a7d11
NC
4265
4266 case STB_GLOBAL:
a4d8e49b 4267 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4268 flags = BSF_GLOBAL;
3e7a7d11
NC
4269 break;
4270
4271 case STB_WEAK:
4272 flags = BSF_WEAK;
4273 break;
4274
4275 case STB_GNU_UNIQUE:
4276 flags = BSF_GNU_UNIQUE;
4277 break;
4278
4279 default:
4ad4eba5 4280 /* Leave it up to the processor backend. */
3e7a7d11 4281 break;
4ad4eba5
AM
4282 }
4283
4284 if (isym->st_shndx == SHN_UNDEF)
4285 sec = bfd_und_section_ptr;
cb33740c
AM
4286 else if (isym->st_shndx == SHN_ABS)
4287 sec = bfd_abs_section_ptr;
4288 else if (isym->st_shndx == SHN_COMMON)
4289 {
4290 sec = bfd_com_section_ptr;
4291 /* What ELF calls the size we call the value. What ELF
4292 calls the value we call the alignment. */
4293 value = isym->st_size;
4294 }
4295 else
4ad4eba5
AM
4296 {
4297 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4298 if (sec == NULL)
4299 sec = bfd_abs_section_ptr;
dbaa2011 4300 else if (discarded_section (sec))
529fcb95 4301 {
e5d08002
L
4302 /* Symbols from discarded section are undefined. We keep
4303 its visibility. */
529fcb95 4304 sec = bfd_und_section_ptr;
97196564 4305 discarded = TRUE;
529fcb95
PB
4306 isym->st_shndx = SHN_UNDEF;
4307 }
4ad4eba5
AM
4308 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4309 value -= sec->vma;
4310 }
4ad4eba5
AM
4311
4312 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4313 isym->st_name);
4314 if (name == NULL)
4315 goto error_free_vers;
4316
4317 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4318 && (abfd->flags & BFD_PLUGIN) != 0)
4319 {
4320 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4321
4322 if (xc == NULL)
4323 {
4324 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4325 | SEC_EXCLUDE);
4326 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4327 if (xc == NULL)
4328 goto error_free_vers;
4329 }
4330 sec = xc;
4331 }
4332 else if (isym->st_shndx == SHN_COMMON
4333 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4334 && !bfd_link_relocatable (info))
4ad4eba5
AM
4335 {
4336 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4337
4338 if (tcomm == NULL)
4339 {
02d00247
AM
4340 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4341 | SEC_LINKER_CREATED);
4342 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4343 if (tcomm == NULL)
4ad4eba5
AM
4344 goto error_free_vers;
4345 }
4346 sec = tcomm;
4347 }
66eb6687 4348 else if (bed->elf_add_symbol_hook)
4ad4eba5 4349 {
66eb6687
AM
4350 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4351 &sec, &value))
4ad4eba5
AM
4352 goto error_free_vers;
4353
4354 /* The hook function sets the name to NULL if this symbol
4355 should be skipped for some reason. */
4356 if (name == NULL)
4357 continue;
4358 }
4359
4360 /* Sanity check that all possibilities were handled. */
4361 if (sec == NULL)
4362 {
4363 bfd_set_error (bfd_error_bad_value);
4364 goto error_free_vers;
4365 }
4366
191c0c42
AM
4367 /* Silently discard TLS symbols from --just-syms. There's
4368 no way to combine a static TLS block with a new TLS block
4369 for this executable. */
4370 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4371 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4372 continue;
4373
4ad4eba5
AM
4374 if (bfd_is_und_section (sec)
4375 || bfd_is_com_section (sec))
4376 definition = FALSE;
4377 else
4378 definition = TRUE;
4379
4380 size_change_ok = FALSE;
66eb6687 4381 type_change_ok = bed->type_change_ok;
37a9e49a 4382 old_weak = FALSE;
6e33951e 4383 matched = FALSE;
4ad4eba5
AM
4384 old_alignment = 0;
4385 old_bfd = NULL;
af44c138 4386 new_sec = sec;
4ad4eba5 4387
66eb6687 4388 if (is_elf_hash_table (htab))
4ad4eba5
AM
4389 {
4390 Elf_Internal_Versym iver;
4391 unsigned int vernum = 0;
4392 bfd_boolean skip;
4393
fc0e6df6 4394 if (ever == NULL)
4ad4eba5 4395 {
fc0e6df6
PB
4396 if (info->default_imported_symver)
4397 /* Use the default symbol version created earlier. */
4398 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4399 else
4400 iver.vs_vers = 0;
4401 }
4402 else
4403 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4404
4405 vernum = iver.vs_vers & VERSYM_VERSION;
4406
4407 /* If this is a hidden symbol, or if it is not version
4408 1, we append the version name to the symbol name.
cc86ff91
EB
4409 However, we do not modify a non-hidden absolute symbol
4410 if it is not a function, because it might be the version
4411 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4412 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4413 || (vernum > 1
4414 && (!bfd_is_abs_section (sec)
4415 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4416 {
4417 const char *verstr;
4418 size_t namelen, verlen, newlen;
4419 char *newname, *p;
4420
4421 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4422 {
fc0e6df6
PB
4423 if (vernum > elf_tdata (abfd)->cverdefs)
4424 verstr = NULL;
4425 else if (vernum > 1)
4426 verstr =
4427 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4428 else
4429 verstr = "";
4ad4eba5 4430
fc0e6df6 4431 if (verstr == NULL)
4ad4eba5 4432 {
4eca0228 4433 _bfd_error_handler
695344c0 4434 /* xgettext:c-format */
fc0e6df6
PB
4435 (_("%B: %s: invalid version %u (max %d)"),
4436 abfd, name, vernum,
4437 elf_tdata (abfd)->cverdefs);
4438 bfd_set_error (bfd_error_bad_value);
4439 goto error_free_vers;
4ad4eba5 4440 }
fc0e6df6
PB
4441 }
4442 else
4443 {
4444 /* We cannot simply test for the number of
4445 entries in the VERNEED section since the
4446 numbers for the needed versions do not start
4447 at 0. */
4448 Elf_Internal_Verneed *t;
4449
4450 verstr = NULL;
4451 for (t = elf_tdata (abfd)->verref;
4452 t != NULL;
4453 t = t->vn_nextref)
4ad4eba5 4454 {
fc0e6df6 4455 Elf_Internal_Vernaux *a;
4ad4eba5 4456
fc0e6df6
PB
4457 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4458 {
4459 if (a->vna_other == vernum)
4ad4eba5 4460 {
fc0e6df6
PB
4461 verstr = a->vna_nodename;
4462 break;
4ad4eba5 4463 }
4ad4eba5 4464 }
fc0e6df6
PB
4465 if (a != NULL)
4466 break;
4467 }
4468 if (verstr == NULL)
4469 {
4eca0228 4470 _bfd_error_handler
695344c0 4471 /* xgettext:c-format */
fc0e6df6
PB
4472 (_("%B: %s: invalid needed version %d"),
4473 abfd, name, vernum);
4474 bfd_set_error (bfd_error_bad_value);
4475 goto error_free_vers;
4ad4eba5 4476 }
4ad4eba5 4477 }
fc0e6df6
PB
4478
4479 namelen = strlen (name);
4480 verlen = strlen (verstr);
4481 newlen = namelen + verlen + 2;
4482 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4483 && isym->st_shndx != SHN_UNDEF)
4484 ++newlen;
4485
a50b1753 4486 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4487 if (newname == NULL)
4488 goto error_free_vers;
4489 memcpy (newname, name, namelen);
4490 p = newname + namelen;
4491 *p++ = ELF_VER_CHR;
4492 /* If this is a defined non-hidden version symbol,
4493 we add another @ to the name. This indicates the
4494 default version of the symbol. */
4495 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4496 && isym->st_shndx != SHN_UNDEF)
4497 *p++ = ELF_VER_CHR;
4498 memcpy (p, verstr, verlen + 1);
4499
4500 name = newname;
4ad4eba5
AM
4501 }
4502
cd3416da
AM
4503 /* If this symbol has default visibility and the user has
4504 requested we not re-export it, then mark it as hidden. */
a0d49154 4505 if (!bfd_is_und_section (sec)
cd3416da 4506 && !dynamic
ce875075 4507 && abfd->no_export
cd3416da
AM
4508 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4509 isym->st_other = (STV_HIDDEN
4510 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4511
4f3fedcf
AM
4512 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4513 sym_hash, &old_bfd, &old_weak,
4514 &old_alignment, &skip, &override,
6e33951e
L
4515 &type_change_ok, &size_change_ok,
4516 &matched))
4ad4eba5
AM
4517 goto error_free_vers;
4518
4519 if (skip)
4520 continue;
4521
6e33951e
L
4522 /* Override a definition only if the new symbol matches the
4523 existing one. */
4524 if (override && matched)
4ad4eba5
AM
4525 definition = FALSE;
4526
4527 h = *sym_hash;
4528 while (h->root.type == bfd_link_hash_indirect
4529 || h->root.type == bfd_link_hash_warning)
4530 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4531
4ad4eba5 4532 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4533 && vernum > 1
4534 && definition)
4535 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4536 }
4537
4538 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4539 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4540 (struct bfd_link_hash_entry **) sym_hash)))
4541 goto error_free_vers;
4542
a43942db
MR
4543 if ((flags & BSF_GNU_UNIQUE)
4544 && (abfd->flags & DYNAMIC) == 0
4545 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4546 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4547
4ad4eba5 4548 h = *sym_hash;
90c984fc
L
4549 /* We need to make sure that indirect symbol dynamic flags are
4550 updated. */
4551 hi = h;
4ad4eba5
AM
4552 while (h->root.type == bfd_link_hash_indirect
4553 || h->root.type == bfd_link_hash_warning)
4554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4555
97196564
L
4556 /* Setting the index to -3 tells elf_link_output_extsym that
4557 this symbol is defined in a discarded section. */
4558 if (discarded)
4559 h->indx = -3;
4560
4ad4eba5
AM
4561 *sym_hash = h;
4562
37a9e49a 4563 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4564 new_weakdef = FALSE;
4565 if (dynamic
4566 && definition
37a9e49a 4567 && new_weak
fcb93ecf 4568 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4569 && is_elf_hash_table (htab)
f6e332e6 4570 && h->u.weakdef == NULL)
4ad4eba5
AM
4571 {
4572 /* Keep a list of all weak defined non function symbols from
4573 a dynamic object, using the weakdef field. Later in this
4574 function we will set the weakdef field to the correct
4575 value. We only put non-function symbols from dynamic
4576 objects on this list, because that happens to be the only
4577 time we need to know the normal symbol corresponding to a
4578 weak symbol, and the information is time consuming to
4579 figure out. If the weakdef field is not already NULL,
4580 then this symbol was already defined by some previous
4581 dynamic object, and we will be using that previous
4582 definition anyhow. */
4583
f6e332e6 4584 h->u.weakdef = weaks;
4ad4eba5
AM
4585 weaks = h;
4586 new_weakdef = TRUE;
4587 }
4588
4589 /* Set the alignment of a common symbol. */
a4d8e49b 4590 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4591 && h->root.type == bfd_link_hash_common)
4592 {
4593 unsigned int align;
4594
a4d8e49b 4595 if (common)
af44c138
L
4596 align = bfd_log2 (isym->st_value);
4597 else
4598 {
4599 /* The new symbol is a common symbol in a shared object.
4600 We need to get the alignment from the section. */
4601 align = new_sec->alignment_power;
4602 }
595213d4 4603 if (align > old_alignment)
4ad4eba5
AM
4604 h->root.u.c.p->alignment_power = align;
4605 else
4606 h->root.u.c.p->alignment_power = old_alignment;
4607 }
4608
66eb6687 4609 if (is_elf_hash_table (htab))
4ad4eba5 4610 {
4f3fedcf
AM
4611 /* Set a flag in the hash table entry indicating the type of
4612 reference or definition we just found. A dynamic symbol
4613 is one which is referenced or defined by both a regular
4614 object and a shared object. */
4615 bfd_boolean dynsym = FALSE;
4616
4617 /* Plugin symbols aren't normal. Don't set def_regular or
4618 ref_regular for them, or make them dynamic. */
4619 if ((abfd->flags & BFD_PLUGIN) != 0)
4620 ;
4621 else if (! dynamic)
4622 {
4623 if (! definition)
4624 {
4625 h->ref_regular = 1;
4626 if (bind != STB_WEAK)
4627 h->ref_regular_nonweak = 1;
4628 }
4629 else
4630 {
4631 h->def_regular = 1;
4632 if (h->def_dynamic)
4633 {
4634 h->def_dynamic = 0;
4635 h->ref_dynamic = 1;
4636 }
4637 }
4638
4639 /* If the indirect symbol has been forced local, don't
4640 make the real symbol dynamic. */
4641 if ((h == hi || !hi->forced_local)
0e1862bb 4642 && (bfd_link_dll (info)
4f3fedcf
AM
4643 || h->def_dynamic
4644 || h->ref_dynamic))
4645 dynsym = TRUE;
4646 }
4647 else
4648 {
4649 if (! definition)
4650 {
4651 h->ref_dynamic = 1;
4652 hi->ref_dynamic = 1;
4653 }
4654 else
4655 {
4656 h->def_dynamic = 1;
4657 hi->def_dynamic = 1;
4658 }
4659
4660 /* If the indirect symbol has been forced local, don't
4661 make the real symbol dynamic. */
4662 if ((h == hi || !hi->forced_local)
4663 && (h->def_regular
4664 || h->ref_regular
4665 || (h->u.weakdef != NULL
4666 && ! new_weakdef
4667 && h->u.weakdef->dynindx != -1)))
4668 dynsym = TRUE;
4669 }
4670
4671 /* Check to see if we need to add an indirect symbol for
4672 the default name. */
4673 if (definition
4674 || (!override && h->root.type == bfd_link_hash_common))
4675 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4676 sec, value, &old_bfd, &dynsym))
4677 goto error_free_vers;
4ad4eba5
AM
4678
4679 /* Check the alignment when a common symbol is involved. This
4680 can change when a common symbol is overridden by a normal
4681 definition or a common symbol is ignored due to the old
4682 normal definition. We need to make sure the maximum
4683 alignment is maintained. */
a4d8e49b 4684 if ((old_alignment || common)
4ad4eba5
AM
4685 && h->root.type != bfd_link_hash_common)
4686 {
4687 unsigned int common_align;
4688 unsigned int normal_align;
4689 unsigned int symbol_align;
4690 bfd *normal_bfd;
4691 bfd *common_bfd;
4692
3a81e825
AM
4693 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4694 || h->root.type == bfd_link_hash_defweak);
4695
4ad4eba5
AM
4696 symbol_align = ffs (h->root.u.def.value) - 1;
4697 if (h->root.u.def.section->owner != NULL
0616a280
AM
4698 && (h->root.u.def.section->owner->flags
4699 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4700 {
4701 normal_align = h->root.u.def.section->alignment_power;
4702 if (normal_align > symbol_align)
4703 normal_align = symbol_align;
4704 }
4705 else
4706 normal_align = symbol_align;
4707
4708 if (old_alignment)
4709 {
4710 common_align = old_alignment;
4711 common_bfd = old_bfd;
4712 normal_bfd = abfd;
4713 }
4714 else
4715 {
4716 common_align = bfd_log2 (isym->st_value);
4717 common_bfd = abfd;
4718 normal_bfd = old_bfd;
4719 }
4720
4721 if (normal_align < common_align)
d07676f8
NC
4722 {
4723 /* PR binutils/2735 */
4724 if (normal_bfd == NULL)
4eca0228 4725 _bfd_error_handler
695344c0 4726 /* xgettext:c-format */
4f3fedcf
AM
4727 (_("Warning: alignment %u of common symbol `%s' in %B is"
4728 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4729 1 << common_align, name, common_bfd,
4730 1 << normal_align, h->root.u.def.section);
d07676f8 4731 else
4eca0228 4732 _bfd_error_handler
695344c0 4733 /* xgettext:c-format */
d07676f8
NC
4734 (_("Warning: alignment %u of symbol `%s' in %B"
4735 " is smaller than %u in %B"),
c08bb8dd
AM
4736 1 << normal_align, name, normal_bfd,
4737 1 << common_align, common_bfd);
d07676f8 4738 }
4ad4eba5
AM
4739 }
4740
83ad0046 4741 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4742 if (isym->st_size != 0
4743 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4744 && (definition || h->size == 0))
4745 {
83ad0046
L
4746 if (h->size != 0
4747 && h->size != isym->st_size
4748 && ! size_change_ok)
4eca0228 4749 _bfd_error_handler
695344c0 4750 /* xgettext:c-format */
d003868e
AM
4751 (_("Warning: size of symbol `%s' changed"
4752 " from %lu in %B to %lu in %B"),
c08bb8dd
AM
4753 name, (unsigned long) h->size, old_bfd,
4754 (unsigned long) isym->st_size, abfd);
4ad4eba5
AM
4755
4756 h->size = isym->st_size;
4757 }
4758
4759 /* If this is a common symbol, then we always want H->SIZE
4760 to be the size of the common symbol. The code just above
4761 won't fix the size if a common symbol becomes larger. We
4762 don't warn about a size change here, because that is
4f3fedcf 4763 covered by --warn-common. Allow changes between different
fcb93ecf 4764 function types. */
4ad4eba5
AM
4765 if (h->root.type == bfd_link_hash_common)
4766 h->size = h->root.u.c.size;
4767
4768 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4769 && ((definition && !new_weak)
4770 || (old_weak && h->root.type == bfd_link_hash_common)
4771 || h->type == STT_NOTYPE))
4ad4eba5 4772 {
2955ec4c
L
4773 unsigned int type = ELF_ST_TYPE (isym->st_info);
4774
4775 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4776 symbol. */
4777 if (type == STT_GNU_IFUNC
4778 && (abfd->flags & DYNAMIC) != 0)
4779 type = STT_FUNC;
4ad4eba5 4780
2955ec4c
L
4781 if (h->type != type)
4782 {
4783 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4784 /* xgettext:c-format */
4eca0228 4785 _bfd_error_handler
2955ec4c
L
4786 (_("Warning: type of symbol `%s' changed"
4787 " from %d to %d in %B"),
c08bb8dd 4788 name, h->type, type, abfd);
2955ec4c
L
4789
4790 h->type = type;
4791 }
4ad4eba5
AM
4792 }
4793
54ac0771 4794 /* Merge st_other field. */
b8417128 4795 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4796
c3df8c14 4797 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4798 if (definition
4799 && (sec->flags & SEC_DEBUGGING)
4800 && !bfd_link_relocatable (info))
c3df8c14
AM
4801 dynsym = FALSE;
4802
4f3fedcf
AM
4803 /* Nor should we make plugin symbols dynamic. */
4804 if ((abfd->flags & BFD_PLUGIN) != 0)
4805 dynsym = FALSE;
4806
35fc36a8 4807 if (definition)
35399224
L
4808 {
4809 h->target_internal = isym->st_target_internal;
4810 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4811 }
35fc36a8 4812
4ad4eba5
AM
4813 if (definition && !dynamic)
4814 {
4815 char *p = strchr (name, ELF_VER_CHR);
4816 if (p != NULL && p[1] != ELF_VER_CHR)
4817 {
4818 /* Queue non-default versions so that .symver x, x@FOO
4819 aliases can be checked. */
66eb6687 4820 if (!nondeflt_vers)
4ad4eba5 4821 {
66eb6687
AM
4822 amt = ((isymend - isym + 1)
4823 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4824 nondeflt_vers
4825 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4826 if (!nondeflt_vers)
4827 goto error_free_vers;
4ad4eba5 4828 }
66eb6687 4829 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4830 }
4831 }
4832
4833 if (dynsym && h->dynindx == -1)
4834 {
c152c796 4835 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4836 goto error_free_vers;
f6e332e6 4837 if (h->u.weakdef != NULL
4ad4eba5 4838 && ! new_weakdef
f6e332e6 4839 && h->u.weakdef->dynindx == -1)
4ad4eba5 4840 {
66eb6687 4841 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4842 goto error_free_vers;
4843 }
4844 }
1f599d0e 4845 else if (h->dynindx != -1)
4ad4eba5
AM
4846 /* If the symbol already has a dynamic index, but
4847 visibility says it should not be visible, turn it into
4848 a local symbol. */
4849 switch (ELF_ST_VISIBILITY (h->other))
4850 {
4851 case STV_INTERNAL:
4852 case STV_HIDDEN:
4853 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4854 dynsym = FALSE;
4855 break;
4856 }
4857
aef28989
L
4858 /* Don't add DT_NEEDED for references from the dummy bfd nor
4859 for unmatched symbol. */
4ad4eba5 4860 if (!add_needed
aef28989 4861 && matched
4ad4eba5 4862 && definition
010e5ae2 4863 && ((dynsym
ffa9430d 4864 && h->ref_regular_nonweak
4f3fedcf
AM
4865 && (old_bfd == NULL
4866 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4867 || (h->ref_dynamic_nonweak
010e5ae2 4868 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4869 && !on_needed_list (elf_dt_name (abfd),
4870 htab->needed, NULL))))
4ad4eba5
AM
4871 {
4872 int ret;
4873 const char *soname = elf_dt_name (abfd);
4874
16e4ecc0
AM
4875 info->callbacks->minfo ("%!", soname, old_bfd,
4876 h->root.root.string);
4877
4ad4eba5
AM
4878 /* A symbol from a library loaded via DT_NEEDED of some
4879 other library is referenced by a regular object.
e56f61be 4880 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4881 --no-add-needed is used and the reference was not
4882 a weak one. */
4f3fedcf 4883 if (old_bfd != NULL
b918acf9 4884 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4885 {
4eca0228 4886 _bfd_error_handler
695344c0 4887 /* xgettext:c-format */
3cbc5de0 4888 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4889 old_bfd, name);
ff5ac77b 4890 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4891 goto error_free_vers;
4892 }
4893
a50b1753 4894 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4895 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4896
4ad4eba5 4897 add_needed = TRUE;
7e9f0867 4898 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4899 if (ret < 0)
4900 goto error_free_vers;
4901
4902 BFD_ASSERT (ret == 0);
4903 }
4904 }
4905 }
4906
66eb6687
AM
4907 if (extversym != NULL)
4908 {
4909 free (extversym);
4910 extversym = NULL;
4911 }
4912
4913 if (isymbuf != NULL)
4914 {
4915 free (isymbuf);
4916 isymbuf = NULL;
4917 }
4918
4919 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4920 {
4921 unsigned int i;
4922
4923 /* Restore the symbol table. */
f45794cb
AM
4924 old_ent = (char *) old_tab + tabsize;
4925 memset (elf_sym_hashes (abfd), 0,
4926 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4927 htab->root.table.table = old_table;
4928 htab->root.table.size = old_size;
4929 htab->root.table.count = old_count;
66eb6687 4930 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4931 htab->root.undefs = old_undefs;
4932 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4933 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4934 free (old_strtab);
4935 old_strtab = NULL;
66eb6687
AM
4936 for (i = 0; i < htab->root.table.size; i++)
4937 {
4938 struct bfd_hash_entry *p;
4939 struct elf_link_hash_entry *h;
3e0882af
L
4940 bfd_size_type size;
4941 unsigned int alignment_power;
4070765b 4942 unsigned int non_ir_ref_dynamic;
66eb6687
AM
4943
4944 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4945 {
4946 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4947 if (h->root.type == bfd_link_hash_warning)
4948 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4949
3e0882af
L
4950 /* Preserve the maximum alignment and size for common
4951 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4952 since it can still be loaded at run time by another
3e0882af
L
4953 dynamic lib. */
4954 if (h->root.type == bfd_link_hash_common)
4955 {
4956 size = h->root.u.c.size;
4957 alignment_power = h->root.u.c.p->alignment_power;
4958 }
4959 else
4960 {
4961 size = 0;
4962 alignment_power = 0;
4963 }
4070765b 4964 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
4965 will be exported when the dynamic lib becomes needed
4966 in the second pass. */
4070765b 4967 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
4968 memcpy (p, old_ent, htab->root.table.entsize);
4969 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4970 h = (struct elf_link_hash_entry *) p;
4971 if (h->root.type == bfd_link_hash_warning)
4972 {
4973 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4974 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4975 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4976 }
a4542f1b 4977 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4978 {
4979 if (size > h->root.u.c.size)
4980 h->root.u.c.size = size;
4981 if (alignment_power > h->root.u.c.p->alignment_power)
4982 h->root.u.c.p->alignment_power = alignment_power;
4983 }
4070765b 4984 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
4985 }
4986 }
4987
5061a885
AM
4988 /* Make a special call to the linker "notice" function to
4989 tell it that symbols added for crefs may need to be removed. */
e5034e59 4990 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4991 goto error_free_vers;
5061a885 4992
66eb6687
AM
4993 free (old_tab);
4994 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4995 alloc_mark);
4996 if (nondeflt_vers != NULL)
4997 free (nondeflt_vers);
4998 return TRUE;
4999 }
2de92251 5000
66eb6687
AM
5001 if (old_tab != NULL)
5002 {
e5034e59 5003 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5004 goto error_free_vers;
66eb6687
AM
5005 free (old_tab);
5006 old_tab = NULL;
5007 }
5008
c6e8a9a8
L
5009 /* Now that all the symbols from this input file are created, if
5010 not performing a relocatable link, handle .symver foo, foo@BAR
5011 such that any relocs against foo become foo@BAR. */
0e1862bb 5012 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5013 {
ef53be89 5014 size_t cnt, symidx;
4ad4eba5
AM
5015
5016 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5017 {
5018 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5019 char *shortname, *p;
5020
5021 p = strchr (h->root.root.string, ELF_VER_CHR);
5022 if (p == NULL
5023 || (h->root.type != bfd_link_hash_defined
5024 && h->root.type != bfd_link_hash_defweak))
5025 continue;
5026
5027 amt = p - h->root.root.string;
a50b1753 5028 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5029 if (!shortname)
5030 goto error_free_vers;
4ad4eba5
AM
5031 memcpy (shortname, h->root.root.string, amt);
5032 shortname[amt] = '\0';
5033
5034 hi = (struct elf_link_hash_entry *)
66eb6687 5035 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5036 FALSE, FALSE, FALSE);
5037 if (hi != NULL
5038 && hi->root.type == h->root.type
5039 && hi->root.u.def.value == h->root.u.def.value
5040 && hi->root.u.def.section == h->root.u.def.section)
5041 {
5042 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5043 hi->root.type = bfd_link_hash_indirect;
5044 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5045 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5046 sym_hash = elf_sym_hashes (abfd);
5047 if (sym_hash)
5048 for (symidx = 0; symidx < extsymcount; ++symidx)
5049 if (sym_hash[symidx] == hi)
5050 {
5051 sym_hash[symidx] = h;
5052 break;
5053 }
5054 }
5055 free (shortname);
5056 }
5057 free (nondeflt_vers);
5058 nondeflt_vers = NULL;
5059 }
5060
4ad4eba5
AM
5061 /* Now set the weakdefs field correctly for all the weak defined
5062 symbols we found. The only way to do this is to search all the
5063 symbols. Since we only need the information for non functions in
5064 dynamic objects, that's the only time we actually put anything on
5065 the list WEAKS. We need this information so that if a regular
5066 object refers to a symbol defined weakly in a dynamic object, the
5067 real symbol in the dynamic object is also put in the dynamic
5068 symbols; we also must arrange for both symbols to point to the
5069 same memory location. We could handle the general case of symbol
5070 aliasing, but a general symbol alias can only be generated in
5071 assembler code, handling it correctly would be very time
5072 consuming, and other ELF linkers don't handle general aliasing
5073 either. */
5074 if (weaks != NULL)
5075 {
5076 struct elf_link_hash_entry **hpp;
5077 struct elf_link_hash_entry **hppend;
5078 struct elf_link_hash_entry **sorted_sym_hash;
5079 struct elf_link_hash_entry *h;
5080 size_t sym_count;
5081
5082 /* Since we have to search the whole symbol list for each weak
5083 defined symbol, search time for N weak defined symbols will be
5084 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5085 amt = extsymcount;
5086 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5087 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5088 if (sorted_sym_hash == NULL)
5089 goto error_return;
5090 sym_hash = sorted_sym_hash;
5091 hpp = elf_sym_hashes (abfd);
5092 hppend = hpp + extsymcount;
5093 sym_count = 0;
5094 for (; hpp < hppend; hpp++)
5095 {
5096 h = *hpp;
5097 if (h != NULL
5098 && h->root.type == bfd_link_hash_defined
fcb93ecf 5099 && !bed->is_function_type (h->type))
4ad4eba5
AM
5100 {
5101 *sym_hash = h;
5102 sym_hash++;
5103 sym_count++;
5104 }
5105 }
5106
5107 qsort (sorted_sym_hash, sym_count,
5108 sizeof (struct elf_link_hash_entry *),
5109 elf_sort_symbol);
5110
5111 while (weaks != NULL)
5112 {
5113 struct elf_link_hash_entry *hlook;
5114 asection *slook;
5115 bfd_vma vlook;
ed54588d 5116 size_t i, j, idx = 0;
4ad4eba5
AM
5117
5118 hlook = weaks;
f6e332e6
AM
5119 weaks = hlook->u.weakdef;
5120 hlook->u.weakdef = NULL;
4ad4eba5
AM
5121
5122 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5123 || hlook->root.type == bfd_link_hash_defweak
5124 || hlook->root.type == bfd_link_hash_common
5125 || hlook->root.type == bfd_link_hash_indirect);
5126 slook = hlook->root.u.def.section;
5127 vlook = hlook->root.u.def.value;
5128
4ad4eba5
AM
5129 i = 0;
5130 j = sym_count;
14160578 5131 while (i != j)
4ad4eba5
AM
5132 {
5133 bfd_signed_vma vdiff;
5134 idx = (i + j) / 2;
14160578 5135 h = sorted_sym_hash[idx];
4ad4eba5
AM
5136 vdiff = vlook - h->root.u.def.value;
5137 if (vdiff < 0)
5138 j = idx;
5139 else if (vdiff > 0)
5140 i = idx + 1;
5141 else
5142 {
d3435ae8 5143 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5144 if (sdiff < 0)
5145 j = idx;
5146 else if (sdiff > 0)
5147 i = idx + 1;
5148 else
14160578 5149 break;
4ad4eba5
AM
5150 }
5151 }
5152
5153 /* We didn't find a value/section match. */
14160578 5154 if (i == j)
4ad4eba5
AM
5155 continue;
5156
14160578
AM
5157 /* With multiple aliases, or when the weak symbol is already
5158 strongly defined, we have multiple matching symbols and
5159 the binary search above may land on any of them. Step
5160 one past the matching symbol(s). */
5161 while (++idx != j)
5162 {
5163 h = sorted_sym_hash[idx];
5164 if (h->root.u.def.section != slook
5165 || h->root.u.def.value != vlook)
5166 break;
5167 }
5168
5169 /* Now look back over the aliases. Since we sorted by size
5170 as well as value and section, we'll choose the one with
5171 the largest size. */
5172 while (idx-- != i)
4ad4eba5 5173 {
14160578 5174 h = sorted_sym_hash[idx];
4ad4eba5
AM
5175
5176 /* Stop if value or section doesn't match. */
14160578
AM
5177 if (h->root.u.def.section != slook
5178 || h->root.u.def.value != vlook)
4ad4eba5
AM
5179 break;
5180 else if (h != hlook)
5181 {
f6e332e6 5182 hlook->u.weakdef = h;
4ad4eba5
AM
5183
5184 /* If the weak definition is in the list of dynamic
5185 symbols, make sure the real definition is put
5186 there as well. */
5187 if (hlook->dynindx != -1 && h->dynindx == -1)
5188 {
c152c796 5189 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5190 {
5191 err_free_sym_hash:
5192 free (sorted_sym_hash);
5193 goto error_return;
5194 }
4ad4eba5
AM
5195 }
5196
5197 /* If the real definition is in the list of dynamic
5198 symbols, make sure the weak definition is put
5199 there as well. If we don't do this, then the
5200 dynamic loader might not merge the entries for the
5201 real definition and the weak definition. */
5202 if (h->dynindx != -1 && hlook->dynindx == -1)
5203 {
c152c796 5204 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5205 goto err_free_sym_hash;
4ad4eba5
AM
5206 }
5207 break;
5208 }
5209 }
5210 }
5211
5212 free (sorted_sym_hash);
5213 }
5214
33177bb1
AM
5215 if (bed->check_directives
5216 && !(*bed->check_directives) (abfd, info))
5217 return FALSE;
85fbca6a 5218
d9689752
L
5219 if (!info->check_relocs_after_open_input
5220 && !_bfd_elf_link_check_relocs (abfd, info))
5221 return FALSE;
4ad4eba5
AM
5222
5223 /* If this is a non-traditional link, try to optimize the handling
5224 of the .stab/.stabstr sections. */
5225 if (! dynamic
5226 && ! info->traditional_format
66eb6687 5227 && is_elf_hash_table (htab)
4ad4eba5
AM
5228 && (info->strip != strip_all && info->strip != strip_debugger))
5229 {
5230 asection *stabstr;
5231
5232 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5233 if (stabstr != NULL)
5234 {
5235 bfd_size_type string_offset = 0;
5236 asection *stab;
5237
5238 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5239 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5240 && (!stab->name[5] ||
5241 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5242 && (stab->flags & SEC_MERGE) == 0
5243 && !bfd_is_abs_section (stab->output_section))
5244 {
5245 struct bfd_elf_section_data *secdata;
5246
5247 secdata = elf_section_data (stab);
66eb6687
AM
5248 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5249 stabstr, &secdata->sec_info,
4ad4eba5
AM
5250 &string_offset))
5251 goto error_return;
5252 if (secdata->sec_info)
dbaa2011 5253 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5254 }
5255 }
5256 }
5257
66eb6687 5258 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5259 {
5260 /* Add this bfd to the loaded list. */
5261 struct elf_link_loaded_list *n;
5262
ca4be51c 5263 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5264 if (n == NULL)
5265 goto error_return;
5266 n->abfd = abfd;
66eb6687
AM
5267 n->next = htab->loaded;
5268 htab->loaded = n;
4ad4eba5
AM
5269 }
5270
5271 return TRUE;
5272
5273 error_free_vers:
66eb6687
AM
5274 if (old_tab != NULL)
5275 free (old_tab);
5b677558
AM
5276 if (old_strtab != NULL)
5277 free (old_strtab);
4ad4eba5
AM
5278 if (nondeflt_vers != NULL)
5279 free (nondeflt_vers);
5280 if (extversym != NULL)
5281 free (extversym);
5282 error_free_sym:
5283 if (isymbuf != NULL)
5284 free (isymbuf);
5285 error_return:
5286 return FALSE;
5287}
5288
8387904d
AM
5289/* Return the linker hash table entry of a symbol that might be
5290 satisfied by an archive symbol. Return -1 on error. */
5291
5292struct elf_link_hash_entry *
5293_bfd_elf_archive_symbol_lookup (bfd *abfd,
5294 struct bfd_link_info *info,
5295 const char *name)
5296{
5297 struct elf_link_hash_entry *h;
5298 char *p, *copy;
5299 size_t len, first;
5300
2a41f396 5301 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5302 if (h != NULL)
5303 return h;
5304
5305 /* If this is a default version (the name contains @@), look up the
5306 symbol again with only one `@' as well as without the version.
5307 The effect is that references to the symbol with and without the
5308 version will be matched by the default symbol in the archive. */
5309
5310 p = strchr (name, ELF_VER_CHR);
5311 if (p == NULL || p[1] != ELF_VER_CHR)
5312 return h;
5313
5314 /* First check with only one `@'. */
5315 len = strlen (name);
a50b1753 5316 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5317 if (copy == NULL)
5318 return (struct elf_link_hash_entry *) 0 - 1;
5319
5320 first = p - name + 1;
5321 memcpy (copy, name, first);
5322 memcpy (copy + first, name + first + 1, len - first);
5323
2a41f396 5324 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5325 if (h == NULL)
5326 {
5327 /* We also need to check references to the symbol without the
5328 version. */
5329 copy[first - 1] = '\0';
5330 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5331 FALSE, FALSE, TRUE);
8387904d
AM
5332 }
5333
5334 bfd_release (abfd, copy);
5335 return h;
5336}
5337
0ad989f9 5338/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5339 don't use _bfd_generic_link_add_archive_symbols because we need to
5340 handle versioned symbols.
0ad989f9
L
5341
5342 Fortunately, ELF archive handling is simpler than that done by
5343 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5344 oddities. In ELF, if we find a symbol in the archive map, and the
5345 symbol is currently undefined, we know that we must pull in that
5346 object file.
5347
5348 Unfortunately, we do have to make multiple passes over the symbol
5349 table until nothing further is resolved. */
5350
4ad4eba5
AM
5351static bfd_boolean
5352elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5353{
5354 symindex c;
13e570f8 5355 unsigned char *included = NULL;
0ad989f9
L
5356 carsym *symdefs;
5357 bfd_boolean loop;
5358 bfd_size_type amt;
8387904d
AM
5359 const struct elf_backend_data *bed;
5360 struct elf_link_hash_entry * (*archive_symbol_lookup)
5361 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5362
5363 if (! bfd_has_map (abfd))
5364 {
5365 /* An empty archive is a special case. */
5366 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5367 return TRUE;
5368 bfd_set_error (bfd_error_no_armap);
5369 return FALSE;
5370 }
5371
5372 /* Keep track of all symbols we know to be already defined, and all
5373 files we know to be already included. This is to speed up the
5374 second and subsequent passes. */
5375 c = bfd_ardata (abfd)->symdef_count;
5376 if (c == 0)
5377 return TRUE;
5378 amt = c;
13e570f8
AM
5379 amt *= sizeof (*included);
5380 included = (unsigned char *) bfd_zmalloc (amt);
5381 if (included == NULL)
5382 return FALSE;
0ad989f9
L
5383
5384 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5385 bed = get_elf_backend_data (abfd);
5386 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5387
5388 do
5389 {
5390 file_ptr last;
5391 symindex i;
5392 carsym *symdef;
5393 carsym *symdefend;
5394
5395 loop = FALSE;
5396 last = -1;
5397
5398 symdef = symdefs;
5399 symdefend = symdef + c;
5400 for (i = 0; symdef < symdefend; symdef++, i++)
5401 {
5402 struct elf_link_hash_entry *h;
5403 bfd *element;
5404 struct bfd_link_hash_entry *undefs_tail;
5405 symindex mark;
5406
13e570f8 5407 if (included[i])
0ad989f9
L
5408 continue;
5409 if (symdef->file_offset == last)
5410 {
5411 included[i] = TRUE;
5412 continue;
5413 }
5414
8387904d
AM
5415 h = archive_symbol_lookup (abfd, info, symdef->name);
5416 if (h == (struct elf_link_hash_entry *) 0 - 1)
5417 goto error_return;
0ad989f9
L
5418
5419 if (h == NULL)
5420 continue;
5421
5422 if (h->root.type == bfd_link_hash_common)
5423 {
5424 /* We currently have a common symbol. The archive map contains
5425 a reference to this symbol, so we may want to include it. We
5426 only want to include it however, if this archive element
5427 contains a definition of the symbol, not just another common
5428 declaration of it.
5429
5430 Unfortunately some archivers (including GNU ar) will put
5431 declarations of common symbols into their archive maps, as
5432 well as real definitions, so we cannot just go by the archive
5433 map alone. Instead we must read in the element's symbol
5434 table and check that to see what kind of symbol definition
5435 this is. */
5436 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5437 continue;
5438 }
5439 else if (h->root.type != bfd_link_hash_undefined)
5440 {
5441 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5442 /* Symbol must be defined. Don't check it again. */
5443 included[i] = TRUE;
0ad989f9
L
5444 continue;
5445 }
5446
5447 /* We need to include this archive member. */
5448 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5449 if (element == NULL)
5450 goto error_return;
5451
5452 if (! bfd_check_format (element, bfd_object))
5453 goto error_return;
5454
0ad989f9
L
5455 undefs_tail = info->hash->undefs_tail;
5456
0e144ba7
AM
5457 if (!(*info->callbacks
5458 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5459 continue;
0e144ba7 5460 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5461 goto error_return;
5462
5463 /* If there are any new undefined symbols, we need to make
5464 another pass through the archive in order to see whether
5465 they can be defined. FIXME: This isn't perfect, because
5466 common symbols wind up on undefs_tail and because an
5467 undefined symbol which is defined later on in this pass
5468 does not require another pass. This isn't a bug, but it
5469 does make the code less efficient than it could be. */
5470 if (undefs_tail != info->hash->undefs_tail)
5471 loop = TRUE;
5472
5473 /* Look backward to mark all symbols from this object file
5474 which we have already seen in this pass. */
5475 mark = i;
5476 do
5477 {
5478 included[mark] = TRUE;
5479 if (mark == 0)
5480 break;
5481 --mark;
5482 }
5483 while (symdefs[mark].file_offset == symdef->file_offset);
5484
5485 /* We mark subsequent symbols from this object file as we go
5486 on through the loop. */
5487 last = symdef->file_offset;
5488 }
5489 }
5490 while (loop);
5491
0ad989f9
L
5492 free (included);
5493
5494 return TRUE;
5495
5496 error_return:
0ad989f9
L
5497 if (included != NULL)
5498 free (included);
5499 return FALSE;
5500}
4ad4eba5
AM
5501
5502/* Given an ELF BFD, add symbols to the global hash table as
5503 appropriate. */
5504
5505bfd_boolean
5506bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5507{
5508 switch (bfd_get_format (abfd))
5509 {
5510 case bfd_object:
5511 return elf_link_add_object_symbols (abfd, info);
5512 case bfd_archive:
5513 return elf_link_add_archive_symbols (abfd, info);
5514 default:
5515 bfd_set_error (bfd_error_wrong_format);
5516 return FALSE;
5517 }
5518}
5a580b3a 5519\f
14b1c01e
AM
5520struct hash_codes_info
5521{
5522 unsigned long *hashcodes;
5523 bfd_boolean error;
5524};
a0c8462f 5525
5a580b3a
AM
5526/* This function will be called though elf_link_hash_traverse to store
5527 all hash value of the exported symbols in an array. */
5528
5529static bfd_boolean
5530elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5531{
a50b1753 5532 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5533 const char *name;
5a580b3a
AM
5534 unsigned long ha;
5535 char *alc = NULL;
5536
5a580b3a
AM
5537 /* Ignore indirect symbols. These are added by the versioning code. */
5538 if (h->dynindx == -1)
5539 return TRUE;
5540
5541 name = h->root.root.string;
422f1182 5542 if (h->versioned >= versioned)
5a580b3a 5543 {
422f1182
L
5544 char *p = strchr (name, ELF_VER_CHR);
5545 if (p != NULL)
14b1c01e 5546 {
422f1182
L
5547 alc = (char *) bfd_malloc (p - name + 1);
5548 if (alc == NULL)
5549 {
5550 inf->error = TRUE;
5551 return FALSE;
5552 }
5553 memcpy (alc, name, p - name);
5554 alc[p - name] = '\0';
5555 name = alc;
14b1c01e 5556 }
5a580b3a
AM
5557 }
5558
5559 /* Compute the hash value. */
5560 ha = bfd_elf_hash (name);
5561
5562 /* Store the found hash value in the array given as the argument. */
14b1c01e 5563 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5564
5565 /* And store it in the struct so that we can put it in the hash table
5566 later. */
f6e332e6 5567 h->u.elf_hash_value = ha;
5a580b3a
AM
5568
5569 if (alc != NULL)
5570 free (alc);
5571
5572 return TRUE;
5573}
5574
fdc90cb4
JJ
5575struct collect_gnu_hash_codes
5576{
5577 bfd *output_bfd;
5578 const struct elf_backend_data *bed;
5579 unsigned long int nsyms;
5580 unsigned long int maskbits;
5581 unsigned long int *hashcodes;
5582 unsigned long int *hashval;
5583 unsigned long int *indx;
5584 unsigned long int *counts;
5585 bfd_vma *bitmask;
5586 bfd_byte *contents;
5587 long int min_dynindx;
5588 unsigned long int bucketcount;
5589 unsigned long int symindx;
5590 long int local_indx;
5591 long int shift1, shift2;
5592 unsigned long int mask;
14b1c01e 5593 bfd_boolean error;
fdc90cb4
JJ
5594};
5595
5596/* This function will be called though elf_link_hash_traverse to store
5597 all hash value of the exported symbols in an array. */
5598
5599static bfd_boolean
5600elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5601{
a50b1753 5602 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5603 const char *name;
fdc90cb4
JJ
5604 unsigned long ha;
5605 char *alc = NULL;
5606
fdc90cb4
JJ
5607 /* Ignore indirect symbols. These are added by the versioning code. */
5608 if (h->dynindx == -1)
5609 return TRUE;
5610
5611 /* Ignore also local symbols and undefined symbols. */
5612 if (! (*s->bed->elf_hash_symbol) (h))
5613 return TRUE;
5614
5615 name = h->root.root.string;
422f1182 5616 if (h->versioned >= versioned)
fdc90cb4 5617 {
422f1182
L
5618 char *p = strchr (name, ELF_VER_CHR);
5619 if (p != NULL)
14b1c01e 5620 {
422f1182
L
5621 alc = (char *) bfd_malloc (p - name + 1);
5622 if (alc == NULL)
5623 {
5624 s->error = TRUE;
5625 return FALSE;
5626 }
5627 memcpy (alc, name, p - name);
5628 alc[p - name] = '\0';
5629 name = alc;
14b1c01e 5630 }
fdc90cb4
JJ
5631 }
5632
5633 /* Compute the hash value. */
5634 ha = bfd_elf_gnu_hash (name);
5635
5636 /* Store the found hash value in the array for compute_bucket_count,
5637 and also for .dynsym reordering purposes. */
5638 s->hashcodes[s->nsyms] = ha;
5639 s->hashval[h->dynindx] = ha;
5640 ++s->nsyms;
5641 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5642 s->min_dynindx = h->dynindx;
5643
5644 if (alc != NULL)
5645 free (alc);
5646
5647 return TRUE;
5648}
5649
5650/* This function will be called though elf_link_hash_traverse to do
5651 final dynaminc symbol renumbering. */
5652
5653static bfd_boolean
5654elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5655{
a50b1753 5656 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5657 unsigned long int bucket;
5658 unsigned long int val;
5659
fdc90cb4
JJ
5660 /* Ignore indirect symbols. */
5661 if (h->dynindx == -1)
5662 return TRUE;
5663
5664 /* Ignore also local symbols and undefined symbols. */
5665 if (! (*s->bed->elf_hash_symbol) (h))
5666 {
5667 if (h->dynindx >= s->min_dynindx)
5668 h->dynindx = s->local_indx++;
5669 return TRUE;
5670 }
5671
5672 bucket = s->hashval[h->dynindx] % s->bucketcount;
5673 val = (s->hashval[h->dynindx] >> s->shift1)
5674 & ((s->maskbits >> s->shift1) - 1);
5675 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5676 s->bitmask[val]
5677 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5678 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5679 if (s->counts[bucket] == 1)
5680 /* Last element terminates the chain. */
5681 val |= 1;
5682 bfd_put_32 (s->output_bfd, val,
5683 s->contents + (s->indx[bucket] - s->symindx) * 4);
5684 --s->counts[bucket];
5685 h->dynindx = s->indx[bucket]++;
5686 return TRUE;
5687}
5688
5689/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5690
5691bfd_boolean
5692_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5693{
5694 return !(h->forced_local
5695 || h->root.type == bfd_link_hash_undefined
5696 || h->root.type == bfd_link_hash_undefweak
5697 || ((h->root.type == bfd_link_hash_defined
5698 || h->root.type == bfd_link_hash_defweak)
5699 && h->root.u.def.section->output_section == NULL));
5700}
5701
5a580b3a
AM
5702/* Array used to determine the number of hash table buckets to use
5703 based on the number of symbols there are. If there are fewer than
5704 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5705 fewer than 37 we use 17 buckets, and so forth. We never use more
5706 than 32771 buckets. */
5707
5708static const size_t elf_buckets[] =
5709{
5710 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5711 16411, 32771, 0
5712};
5713
5714/* Compute bucket count for hashing table. We do not use a static set
5715 of possible tables sizes anymore. Instead we determine for all
5716 possible reasonable sizes of the table the outcome (i.e., the
5717 number of collisions etc) and choose the best solution. The
5718 weighting functions are not too simple to allow the table to grow
5719 without bounds. Instead one of the weighting factors is the size.
5720 Therefore the result is always a good payoff between few collisions
5721 (= short chain lengths) and table size. */
5722static size_t
b20dd2ce 5723compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5724 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5725 unsigned long int nsyms,
5726 int gnu_hash)
5a580b3a 5727{
5a580b3a 5728 size_t best_size = 0;
5a580b3a 5729 unsigned long int i;
5a580b3a 5730
5a580b3a
AM
5731 /* We have a problem here. The following code to optimize the table
5732 size requires an integer type with more the 32 bits. If
5733 BFD_HOST_U_64_BIT is set we know about such a type. */
5734#ifdef BFD_HOST_U_64_BIT
5735 if (info->optimize)
5736 {
5a580b3a
AM
5737 size_t minsize;
5738 size_t maxsize;
5739 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5740 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5741 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5742 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5743 unsigned long int *counts;
d40f3da9 5744 bfd_size_type amt;
0883b6e0 5745 unsigned int no_improvement_count = 0;
5a580b3a
AM
5746
5747 /* Possible optimization parameters: if we have NSYMS symbols we say
5748 that the hashing table must at least have NSYMS/4 and at most
5749 2*NSYMS buckets. */
5750 minsize = nsyms / 4;
5751 if (minsize == 0)
5752 minsize = 1;
5753 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5754 if (gnu_hash)
5755 {
5756 if (minsize < 2)
5757 minsize = 2;
5758 if ((best_size & 31) == 0)
5759 ++best_size;
5760 }
5a580b3a
AM
5761
5762 /* Create array where we count the collisions in. We must use bfd_malloc
5763 since the size could be large. */
5764 amt = maxsize;
5765 amt *= sizeof (unsigned long int);
a50b1753 5766 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5767 if (counts == NULL)
fdc90cb4 5768 return 0;
5a580b3a
AM
5769
5770 /* Compute the "optimal" size for the hash table. The criteria is a
5771 minimal chain length. The minor criteria is (of course) the size
5772 of the table. */
5773 for (i = minsize; i < maxsize; ++i)
5774 {
5775 /* Walk through the array of hashcodes and count the collisions. */
5776 BFD_HOST_U_64_BIT max;
5777 unsigned long int j;
5778 unsigned long int fact;
5779
fdc90cb4
JJ
5780 if (gnu_hash && (i & 31) == 0)
5781 continue;
5782
5a580b3a
AM
5783 memset (counts, '\0', i * sizeof (unsigned long int));
5784
5785 /* Determine how often each hash bucket is used. */
5786 for (j = 0; j < nsyms; ++j)
5787 ++counts[hashcodes[j] % i];
5788
5789 /* For the weight function we need some information about the
5790 pagesize on the target. This is information need not be 100%
5791 accurate. Since this information is not available (so far) we
5792 define it here to a reasonable default value. If it is crucial
5793 to have a better value some day simply define this value. */
5794# ifndef BFD_TARGET_PAGESIZE
5795# define BFD_TARGET_PAGESIZE (4096)
5796# endif
5797
fdc90cb4
JJ
5798 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5799 and the chains. */
5800 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5801
5802# if 1
5803 /* Variant 1: optimize for short chains. We add the squares
5804 of all the chain lengths (which favors many small chain
5805 over a few long chains). */
5806 for (j = 0; j < i; ++j)
5807 max += counts[j] * counts[j];
5808
5809 /* This adds penalties for the overall size of the table. */
fdc90cb4 5810 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5811 max *= fact * fact;
5812# else
5813 /* Variant 2: Optimize a lot more for small table. Here we
5814 also add squares of the size but we also add penalties for
5815 empty slots (the +1 term). */
5816 for (j = 0; j < i; ++j)
5817 max += (1 + counts[j]) * (1 + counts[j]);
5818
5819 /* The overall size of the table is considered, but not as
5820 strong as in variant 1, where it is squared. */
fdc90cb4 5821 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5822 max *= fact;
5823# endif
5824
5825 /* Compare with current best results. */
5826 if (max < best_chlen)
5827 {
5828 best_chlen = max;
5829 best_size = i;
ca4be51c 5830 no_improvement_count = 0;
5a580b3a 5831 }
0883b6e0
NC
5832 /* PR 11843: Avoid futile long searches for the best bucket size
5833 when there are a large number of symbols. */
5834 else if (++no_improvement_count == 100)
5835 break;
5a580b3a
AM
5836 }
5837
5838 free (counts);
5839 }
5840 else
5841#endif /* defined (BFD_HOST_U_64_BIT) */
5842 {
5843 /* This is the fallback solution if no 64bit type is available or if we
5844 are not supposed to spend much time on optimizations. We select the
5845 bucket count using a fixed set of numbers. */
5846 for (i = 0; elf_buckets[i] != 0; i++)
5847 {
5848 best_size = elf_buckets[i];
fdc90cb4 5849 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5850 break;
5851 }
fdc90cb4
JJ
5852 if (gnu_hash && best_size < 2)
5853 best_size = 2;
5a580b3a
AM
5854 }
5855
5a580b3a
AM
5856 return best_size;
5857}
5858
d0bf826b
AM
5859/* Size any SHT_GROUP section for ld -r. */
5860
5861bfd_boolean
5862_bfd_elf_size_group_sections (struct bfd_link_info *info)
5863{
5864 bfd *ibfd;
5865
c72f2fb2 5866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5867 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5868 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5869 return FALSE;
5870 return TRUE;
5871}
5872
04c3a755
NS
5873/* Set a default stack segment size. The value in INFO wins. If it
5874 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5875 undefined it is initialized. */
5876
5877bfd_boolean
5878bfd_elf_stack_segment_size (bfd *output_bfd,
5879 struct bfd_link_info *info,
5880 const char *legacy_symbol,
5881 bfd_vma default_size)
5882{
5883 struct elf_link_hash_entry *h = NULL;
5884
5885 /* Look for legacy symbol. */
5886 if (legacy_symbol)
5887 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5888 FALSE, FALSE, FALSE);
5889 if (h && (h->root.type == bfd_link_hash_defined
5890 || h->root.type == bfd_link_hash_defweak)
5891 && h->def_regular
5892 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5893 {
5894 /* The symbol has no type if specified on the command line. */
5895 h->type = STT_OBJECT;
5896 if (info->stacksize)
695344c0 5897 /* xgettext:c-format */
4eca0228
AM
5898 _bfd_error_handler (_("%B: stack size specified and %s set"),
5899 output_bfd, legacy_symbol);
04c3a755 5900 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5901 /* xgettext:c-format */
4eca0228
AM
5902 _bfd_error_handler (_("%B: %s not absolute"),
5903 output_bfd, legacy_symbol);
04c3a755
NS
5904 else
5905 info->stacksize = h->root.u.def.value;
5906 }
5907
5908 if (!info->stacksize)
5909 /* If the user didn't set a size, or explicitly inhibit the
5910 size, set it now. */
5911 info->stacksize = default_size;
5912
5913 /* Provide the legacy symbol, if it is referenced. */
5914 if (h && (h->root.type == bfd_link_hash_undefined
5915 || h->root.type == bfd_link_hash_undefweak))
5916 {
5917 struct bfd_link_hash_entry *bh = NULL;
5918
5919 if (!(_bfd_generic_link_add_one_symbol
5920 (info, output_bfd, legacy_symbol,
5921 BSF_GLOBAL, bfd_abs_section_ptr,
5922 info->stacksize >= 0 ? info->stacksize : 0,
5923 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5924 return FALSE;
5925
5926 h = (struct elf_link_hash_entry *) bh;
5927 h->def_regular = 1;
5928 h->type = STT_OBJECT;
5929 }
5930
5931 return TRUE;
5932}
5933
b531344c
MR
5934/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5935
5936struct elf_gc_sweep_symbol_info
5937{
5938 struct bfd_link_info *info;
5939 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5940 bfd_boolean);
5941};
5942
5943static bfd_boolean
5944elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5945{
5946 if (!h->mark
5947 && (((h->root.type == bfd_link_hash_defined
5948 || h->root.type == bfd_link_hash_defweak)
5949 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5950 && h->root.u.def.section->gc_mark))
5951 || h->root.type == bfd_link_hash_undefined
5952 || h->root.type == bfd_link_hash_undefweak))
5953 {
5954 struct elf_gc_sweep_symbol_info *inf;
5955
5956 inf = (struct elf_gc_sweep_symbol_info *) data;
5957 (*inf->hide_symbol) (inf->info, h, TRUE);
5958 h->def_regular = 0;
5959 h->ref_regular = 0;
5960 h->ref_regular_nonweak = 0;
5961 }
5962
5963 return TRUE;
5964}
5965
5a580b3a
AM
5966/* Set up the sizes and contents of the ELF dynamic sections. This is
5967 called by the ELF linker emulation before_allocation routine. We
5968 must set the sizes of the sections before the linker sets the
5969 addresses of the various sections. */
5970
5971bfd_boolean
5972bfd_elf_size_dynamic_sections (bfd *output_bfd,
5973 const char *soname,
5974 const char *rpath,
5975 const char *filter_shlib,
7ee314fa
AM
5976 const char *audit,
5977 const char *depaudit,
5a580b3a
AM
5978 const char * const *auxiliary_filters,
5979 struct bfd_link_info *info,
fd91d419 5980 asection **sinterpptr)
5a580b3a 5981{
5a580b3a
AM
5982 bfd *dynobj;
5983 const struct elf_backend_data *bed;
5a580b3a
AM
5984
5985 *sinterpptr = NULL;
5986
5a580b3a
AM
5987 if (!is_elf_hash_table (info->hash))
5988 return TRUE;
5989
5a580b3a
AM
5990 dynobj = elf_hash_table (info)->dynobj;
5991
9a2a56cc 5992 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 5993 {
902e9fc7
MR
5994 struct bfd_elf_version_tree *verdefs;
5995 struct elf_info_failed asvinfo;
5a580b3a
AM
5996 struct bfd_elf_version_tree *t;
5997 struct bfd_elf_version_expr *d;
902e9fc7 5998 struct elf_info_failed eif;
5a580b3a 5999 bfd_boolean all_defined;
902e9fc7 6000 asection *s;
e6699019 6001 size_t soname_indx;
7ee314fa 6002
5a580b3a 6003 eif.info = info;
5a580b3a
AM
6004 eif.failed = FALSE;
6005
6006 /* If we are supposed to export all symbols into the dynamic symbol
6007 table (this is not the normal case), then do so. */
55255dae 6008 if (info->export_dynamic
0e1862bb 6009 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6010 {
6011 elf_link_hash_traverse (elf_hash_table (info),
6012 _bfd_elf_export_symbol,
6013 &eif);
6014 if (eif.failed)
6015 return FALSE;
6016 }
6017
e6699019
L
6018 if (soname != NULL)
6019 {
6020 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6021 soname, TRUE);
6022 if (soname_indx == (size_t) -1
6023 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6024 return FALSE;
6025 }
6026 else
6027 soname_indx = (size_t) -1;
6028
5a580b3a 6029 /* Make all global versions with definition. */
fd91d419 6030 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6031 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6032 if (!d->symver && d->literal)
5a580b3a
AM
6033 {
6034 const char *verstr, *name;
6035 size_t namelen, verlen, newlen;
93252b1c 6036 char *newname, *p, leading_char;
5a580b3a
AM
6037 struct elf_link_hash_entry *newh;
6038
93252b1c 6039 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6040 name = d->pattern;
93252b1c 6041 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6042 verstr = t->name;
6043 verlen = strlen (verstr);
6044 newlen = namelen + verlen + 3;
6045
a50b1753 6046 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6047 if (newname == NULL)
6048 return FALSE;
93252b1c
MF
6049 newname[0] = leading_char;
6050 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6051
6052 /* Check the hidden versioned definition. */
6053 p = newname + namelen;
6054 *p++ = ELF_VER_CHR;
6055 memcpy (p, verstr, verlen + 1);
6056 newh = elf_link_hash_lookup (elf_hash_table (info),
6057 newname, FALSE, FALSE,
6058 FALSE);
6059 if (newh == NULL
6060 || (newh->root.type != bfd_link_hash_defined
6061 && newh->root.type != bfd_link_hash_defweak))
6062 {
6063 /* Check the default versioned definition. */
6064 *p++ = ELF_VER_CHR;
6065 memcpy (p, verstr, verlen + 1);
6066 newh = elf_link_hash_lookup (elf_hash_table (info),
6067 newname, FALSE, FALSE,
6068 FALSE);
6069 }
6070 free (newname);
6071
6072 /* Mark this version if there is a definition and it is
6073 not defined in a shared object. */
6074 if (newh != NULL
f5385ebf 6075 && !newh->def_dynamic
5a580b3a
AM
6076 && (newh->root.type == bfd_link_hash_defined
6077 || newh->root.type == bfd_link_hash_defweak))
6078 d->symver = 1;
6079 }
6080
6081 /* Attach all the symbols to their version information. */
5a580b3a 6082 asvinfo.info = info;
5a580b3a
AM
6083 asvinfo.failed = FALSE;
6084
6085 elf_link_hash_traverse (elf_hash_table (info),
6086 _bfd_elf_link_assign_sym_version,
6087 &asvinfo);
6088 if (asvinfo.failed)
6089 return FALSE;
6090
6091 if (!info->allow_undefined_version)
6092 {
6093 /* Check if all global versions have a definition. */
6094 all_defined = TRUE;
fd91d419 6095 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6096 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6097 if (d->literal && !d->symver && !d->script)
5a580b3a 6098 {
4eca0228 6099 _bfd_error_handler
5a580b3a
AM
6100 (_("%s: undefined version: %s"),
6101 d->pattern, t->name);
6102 all_defined = FALSE;
6103 }
6104
6105 if (!all_defined)
6106 {
6107 bfd_set_error (bfd_error_bad_value);
6108 return FALSE;
6109 }
6110 }
6111
902e9fc7
MR
6112 /* Set up the version definition section. */
6113 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6114 BFD_ASSERT (s != NULL);
5a580b3a 6115
902e9fc7
MR
6116 /* We may have created additional version definitions if we are
6117 just linking a regular application. */
6118 verdefs = info->version_info;
5a580b3a 6119
902e9fc7
MR
6120 /* Skip anonymous version tag. */
6121 if (verdefs != NULL && verdefs->vernum == 0)
6122 verdefs = verdefs->next;
5a580b3a 6123
902e9fc7
MR
6124 if (verdefs == NULL && !info->create_default_symver)
6125 s->flags |= SEC_EXCLUDE;
6126 else
5a580b3a 6127 {
902e9fc7
MR
6128 unsigned int cdefs;
6129 bfd_size_type size;
6130 bfd_byte *p;
6131 Elf_Internal_Verdef def;
6132 Elf_Internal_Verdaux defaux;
6133 struct bfd_link_hash_entry *bh;
6134 struct elf_link_hash_entry *h;
6135 const char *name;
5a580b3a 6136
902e9fc7
MR
6137 cdefs = 0;
6138 size = 0;
5a580b3a 6139
902e9fc7
MR
6140 /* Make space for the base version. */
6141 size += sizeof (Elf_External_Verdef);
6142 size += sizeof (Elf_External_Verdaux);
6143 ++cdefs;
6144
6145 /* Make space for the default version. */
6146 if (info->create_default_symver)
6147 {
6148 size += sizeof (Elf_External_Verdef);
6149 ++cdefs;
3e3b46e5
PB
6150 }
6151
5a580b3a
AM
6152 for (t = verdefs; t != NULL; t = t->next)
6153 {
6154 struct bfd_elf_version_deps *n;
6155
a6cc6b3b
RO
6156 /* Don't emit base version twice. */
6157 if (t->vernum == 0)
6158 continue;
6159
5a580b3a
AM
6160 size += sizeof (Elf_External_Verdef);
6161 size += sizeof (Elf_External_Verdaux);
6162 ++cdefs;
6163
6164 for (n = t->deps; n != NULL; n = n->next)
6165 size += sizeof (Elf_External_Verdaux);
6166 }
6167
eea6121a 6168 s->size = size;
a50b1753 6169 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6170 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6171 return FALSE;
6172
6173 /* Fill in the version definition section. */
6174
6175 p = s->contents;
6176
6177 def.vd_version = VER_DEF_CURRENT;
6178 def.vd_flags = VER_FLG_BASE;
6179 def.vd_ndx = 1;
6180 def.vd_cnt = 1;
3e3b46e5
PB
6181 if (info->create_default_symver)
6182 {
6183 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6184 def.vd_next = sizeof (Elf_External_Verdef);
6185 }
6186 else
6187 {
6188 def.vd_aux = sizeof (Elf_External_Verdef);
6189 def.vd_next = (sizeof (Elf_External_Verdef)
6190 + sizeof (Elf_External_Verdaux));
6191 }
5a580b3a 6192
ef53be89 6193 if (soname_indx != (size_t) -1)
5a580b3a
AM
6194 {
6195 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6196 soname_indx);
6197 def.vd_hash = bfd_elf_hash (soname);
6198 defaux.vda_name = soname_indx;
3e3b46e5 6199 name = soname;
5a580b3a
AM
6200 }
6201 else
6202 {
ef53be89 6203 size_t indx;
5a580b3a 6204
06084812 6205 name = lbasename (output_bfd->filename);
5a580b3a
AM
6206 def.vd_hash = bfd_elf_hash (name);
6207 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6208 name, FALSE);
ef53be89 6209 if (indx == (size_t) -1)
5a580b3a
AM
6210 return FALSE;
6211 defaux.vda_name = indx;
6212 }
6213 defaux.vda_next = 0;
6214
6215 _bfd_elf_swap_verdef_out (output_bfd, &def,
6216 (Elf_External_Verdef *) p);
6217 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6218 if (info->create_default_symver)
6219 {
6220 /* Add a symbol representing this version. */
6221 bh = NULL;
6222 if (! (_bfd_generic_link_add_one_symbol
6223 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6224 0, NULL, FALSE,
6225 get_elf_backend_data (dynobj)->collect, &bh)))
6226 return FALSE;
6227 h = (struct elf_link_hash_entry *) bh;
6228 h->non_elf = 0;
6229 h->def_regular = 1;
6230 h->type = STT_OBJECT;
6231 h->verinfo.vertree = NULL;
6232
6233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6234 return FALSE;
6235
6236 /* Create a duplicate of the base version with the same
6237 aux block, but different flags. */
6238 def.vd_flags = 0;
6239 def.vd_ndx = 2;
6240 def.vd_aux = sizeof (Elf_External_Verdef);
6241 if (verdefs)
6242 def.vd_next = (sizeof (Elf_External_Verdef)
6243 + sizeof (Elf_External_Verdaux));
6244 else
6245 def.vd_next = 0;
6246 _bfd_elf_swap_verdef_out (output_bfd, &def,
6247 (Elf_External_Verdef *) p);
6248 p += sizeof (Elf_External_Verdef);
6249 }
5a580b3a
AM
6250 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6251 (Elf_External_Verdaux *) p);
6252 p += sizeof (Elf_External_Verdaux);
6253
6254 for (t = verdefs; t != NULL; t = t->next)
6255 {
6256 unsigned int cdeps;
6257 struct bfd_elf_version_deps *n;
5a580b3a 6258
a6cc6b3b
RO
6259 /* Don't emit the base version twice. */
6260 if (t->vernum == 0)
6261 continue;
6262
5a580b3a
AM
6263 cdeps = 0;
6264 for (n = t->deps; n != NULL; n = n->next)
6265 ++cdeps;
6266
6267 /* Add a symbol representing this version. */
6268 bh = NULL;
6269 if (! (_bfd_generic_link_add_one_symbol
6270 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6271 0, NULL, FALSE,
6272 get_elf_backend_data (dynobj)->collect, &bh)))
6273 return FALSE;
6274 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6275 h->non_elf = 0;
6276 h->def_regular = 1;
5a580b3a
AM
6277 h->type = STT_OBJECT;
6278 h->verinfo.vertree = t;
6279
c152c796 6280 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6281 return FALSE;
6282
6283 def.vd_version = VER_DEF_CURRENT;
6284 def.vd_flags = 0;
6285 if (t->globals.list == NULL
6286 && t->locals.list == NULL
6287 && ! t->used)
6288 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6289 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6290 def.vd_cnt = cdeps + 1;
6291 def.vd_hash = bfd_elf_hash (t->name);
6292 def.vd_aux = sizeof (Elf_External_Verdef);
6293 def.vd_next = 0;
a6cc6b3b
RO
6294
6295 /* If a basever node is next, it *must* be the last node in
6296 the chain, otherwise Verdef construction breaks. */
6297 if (t->next != NULL && t->next->vernum == 0)
6298 BFD_ASSERT (t->next->next == NULL);
6299
6300 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6301 def.vd_next = (sizeof (Elf_External_Verdef)
6302 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6303
6304 _bfd_elf_swap_verdef_out (output_bfd, &def,
6305 (Elf_External_Verdef *) p);
6306 p += sizeof (Elf_External_Verdef);
6307
6308 defaux.vda_name = h->dynstr_index;
6309 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6310 h->dynstr_index);
6311 defaux.vda_next = 0;
6312 if (t->deps != NULL)
6313 defaux.vda_next = sizeof (Elf_External_Verdaux);
6314 t->name_indx = defaux.vda_name;
6315
6316 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6317 (Elf_External_Verdaux *) p);
6318 p += sizeof (Elf_External_Verdaux);
6319
6320 for (n = t->deps; n != NULL; n = n->next)
6321 {
6322 if (n->version_needed == NULL)
6323 {
6324 /* This can happen if there was an error in the
6325 version script. */
6326 defaux.vda_name = 0;
6327 }
6328 else
6329 {
6330 defaux.vda_name = n->version_needed->name_indx;
6331 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6332 defaux.vda_name);
6333 }
6334 if (n->next == NULL)
6335 defaux.vda_next = 0;
6336 else
6337 defaux.vda_next = sizeof (Elf_External_Verdaux);
6338
6339 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6340 (Elf_External_Verdaux *) p);
6341 p += sizeof (Elf_External_Verdaux);
6342 }
6343 }
6344
5a580b3a
AM
6345 elf_tdata (output_bfd)->cverdefs = cdefs;
6346 }
6347
5a580b3a
AM
6348 /* Work out the size of the version reference section. */
6349
3d4d4302 6350 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6351 BFD_ASSERT (s != NULL);
6352 {
6353 struct elf_find_verdep_info sinfo;
6354
5a580b3a
AM
6355 sinfo.info = info;
6356 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6357 if (sinfo.vers == 0)
6358 sinfo.vers = 1;
6359 sinfo.failed = FALSE;
6360
6361 elf_link_hash_traverse (elf_hash_table (info),
6362 _bfd_elf_link_find_version_dependencies,
6363 &sinfo);
14b1c01e
AM
6364 if (sinfo.failed)
6365 return FALSE;
5a580b3a
AM
6366
6367 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6368 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6369 else
6370 {
902e9fc7 6371 Elf_Internal_Verneed *vn;
5a580b3a
AM
6372 unsigned int size;
6373 unsigned int crefs;
6374 bfd_byte *p;
6375
a6cc6b3b 6376 /* Build the version dependency section. */
5a580b3a
AM
6377 size = 0;
6378 crefs = 0;
902e9fc7
MR
6379 for (vn = elf_tdata (output_bfd)->verref;
6380 vn != NULL;
6381 vn = vn->vn_nextref)
5a580b3a
AM
6382 {
6383 Elf_Internal_Vernaux *a;
6384
6385 size += sizeof (Elf_External_Verneed);
6386 ++crefs;
902e9fc7 6387 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6388 size += sizeof (Elf_External_Vernaux);
6389 }
6390
eea6121a 6391 s->size = size;
a50b1753 6392 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6393 if (s->contents == NULL)
6394 return FALSE;
6395
6396 p = s->contents;
902e9fc7
MR
6397 for (vn = elf_tdata (output_bfd)->verref;
6398 vn != NULL;
6399 vn = vn->vn_nextref)
5a580b3a
AM
6400 {
6401 unsigned int caux;
6402 Elf_Internal_Vernaux *a;
ef53be89 6403 size_t indx;
5a580b3a
AM
6404
6405 caux = 0;
902e9fc7 6406 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6407 ++caux;
6408
902e9fc7
MR
6409 vn->vn_version = VER_NEED_CURRENT;
6410 vn->vn_cnt = caux;
5a580b3a 6411 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
902e9fc7
MR
6412 elf_dt_name (vn->vn_bfd) != NULL
6413 ? elf_dt_name (vn->vn_bfd)
6414 : lbasename (vn->vn_bfd->filename),
5a580b3a 6415 FALSE);
ef53be89 6416 if (indx == (size_t) -1)
5a580b3a 6417 return FALSE;
902e9fc7
MR
6418 vn->vn_file = indx;
6419 vn->vn_aux = sizeof (Elf_External_Verneed);
6420 if (vn->vn_nextref == NULL)
6421 vn->vn_next = 0;
5a580b3a 6422 else
902e9fc7 6423 vn->vn_next = (sizeof (Elf_External_Verneed)
5a580b3a
AM
6424 + caux * sizeof (Elf_External_Vernaux));
6425
902e9fc7 6426 _bfd_elf_swap_verneed_out (output_bfd, vn,
5a580b3a
AM
6427 (Elf_External_Verneed *) p);
6428 p += sizeof (Elf_External_Verneed);
6429
902e9fc7 6430 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6431 {
6432 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6433 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6434 a->vna_nodename, FALSE);
ef53be89 6435 if (indx == (size_t) -1)
5a580b3a
AM
6436 return FALSE;
6437 a->vna_name = indx;
6438 if (a->vna_nextptr == NULL)
6439 a->vna_next = 0;
6440 else
6441 a->vna_next = sizeof (Elf_External_Vernaux);
6442
6443 _bfd_elf_swap_vernaux_out (output_bfd, a,
6444 (Elf_External_Vernaux *) p);
6445 p += sizeof (Elf_External_Vernaux);
6446 }
6447 }
6448
5a580b3a
AM
6449 elf_tdata (output_bfd)->cverrefs = crefs;
6450 }
6451 }
902e9fc7
MR
6452 }
6453
6454 bed = get_elf_backend_data (output_bfd);
6455
6456 if (info->gc_sections && bed->can_gc_sections)
6457 {
6458 struct elf_gc_sweep_symbol_info sweep_info;
6459 unsigned long section_sym_count;
6460
6461 /* Remove the symbols that were in the swept sections from the
6462 dynamic symbol table. GCFIXME: Anyone know how to get them
6463 out of the static symbol table as well? */
6464 sweep_info.info = info;
6465 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6466 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6467 &sweep_info);
6468
23ec1e32
MR
6469 /* We need to reassign dynsym indices now that symbols may have
6470 been removed. See the call in `bfd_elf_size_dynsym_hash_dynstr'
6471 for the details of the conditions used here. */
6472 if (elf_hash_table (info)->dynamic_sections_created
6473 || bed->always_renumber_dynsyms)
c46cec3a 6474 _bfd_elf_link_renumber_dynsyms (output_bfd, info, &section_sym_count);
902e9fc7
MR
6475 }
6476
6477 /* Any syms created from now on start with -1 in
6478 got.refcount/offset and plt.refcount/offset. */
6479 elf_hash_table (info)->init_got_refcount
6480 = elf_hash_table (info)->init_got_offset;
6481 elf_hash_table (info)->init_plt_refcount
6482 = elf_hash_table (info)->init_plt_offset;
6483
6484 if (bfd_link_relocatable (info)
6485 && !_bfd_elf_size_group_sections (info))
6486 return FALSE;
6487
6488 /* The backend may have to create some sections regardless of whether
6489 we're dynamic or not. */
6490 if (bed->elf_backend_always_size_sections
6491 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6492 return FALSE;
6493
6494 /* Determine any GNU_STACK segment requirements, after the backend
6495 has had a chance to set a default segment size. */
6496 if (info->execstack)
6497 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6498 else if (info->noexecstack)
6499 elf_stack_flags (output_bfd) = PF_R | PF_W;
6500 else
6501 {
6502 bfd *inputobj;
6503 asection *notesec = NULL;
6504 int exec = 0;
6505
6506 for (inputobj = info->input_bfds;
6507 inputobj;
6508 inputobj = inputobj->link.next)
6509 {
6510 asection *s;
6511
6512 if (inputobj->flags
6513 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6514 continue;
6515 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6516 if (s)
6517 {
6518 if (s->flags & SEC_CODE)
6519 exec = PF_X;
6520 notesec = s;
6521 }
6522 else if (bed->default_execstack)
6523 exec = PF_X;
6524 }
6525 if (notesec || info->stacksize > 0)
6526 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6527 if (notesec && exec && bfd_link_relocatable (info)
6528 && notesec->output_section != bfd_abs_section_ptr)
6529 notesec->output_section->flags |= SEC_CODE;
6530 }
6531
6532 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6533 {
6534 struct elf_info_failed eif;
6535 struct elf_link_hash_entry *h;
6536 asection *dynstr;
6537 asection *s;
6538
6539 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6540 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6541
902e9fc7
MR
6542 if (info->symbolic)
6543 {
6544 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6545 return FALSE;
6546 info->flags |= DF_SYMBOLIC;
6547 }
6548
6549 if (rpath != NULL)
6550 {
6551 size_t indx;
6552 bfd_vma tag;
6553
6554 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6555 TRUE);
6556 if (indx == (size_t) -1)
6557 return FALSE;
6558
6559 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6560 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6561 return FALSE;
6562 }
6563
6564 if (filter_shlib != NULL)
6565 {
6566 size_t indx;
6567
6568 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6569 filter_shlib, TRUE);
6570 if (indx == (size_t) -1
6571 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6572 return FALSE;
6573 }
6574
6575 if (auxiliary_filters != NULL)
6576 {
6577 const char * const *p;
6578
6579 for (p = auxiliary_filters; *p != NULL; p++)
6580 {
6581 size_t indx;
6582
6583 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6584 *p, TRUE);
6585 if (indx == (size_t) -1
6586 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6587 return FALSE;
6588 }
6589 }
6590
6591 if (audit != NULL)
6592 {
6593 size_t indx;
6594
6595 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6596 TRUE);
6597 if (indx == (size_t) -1
6598 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6599 return FALSE;
6600 }
6601
6602 if (depaudit != NULL)
6603 {
6604 size_t indx;
6605
6606 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6607 TRUE);
6608 if (indx == (size_t) -1
6609 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6610 return FALSE;
6611 }
6612
6613 eif.info = info;
6614 eif.failed = FALSE;
6615
6616 /* Find all symbols which were defined in a dynamic object and make
6617 the backend pick a reasonable value for them. */
6618 elf_link_hash_traverse (elf_hash_table (info),
6619 _bfd_elf_adjust_dynamic_symbol,
6620 &eif);
6621 if (eif.failed)
6622 return FALSE;
6623
6624 /* Add some entries to the .dynamic section. We fill in some of the
6625 values later, in bfd_elf_final_link, but we must add the entries
6626 now so that we know the final size of the .dynamic section. */
6627
6628 /* If there are initialization and/or finalization functions to
6629 call then add the corresponding DT_INIT/DT_FINI entries. */
6630 h = (info->init_function
6631 ? elf_link_hash_lookup (elf_hash_table (info),
6632 info->init_function, FALSE,
6633 FALSE, FALSE)
6634 : NULL);
6635 if (h != NULL
6636 && (h->ref_regular
6637 || h->def_regular))
6638 {
6639 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6640 return FALSE;
6641 }
6642 h = (info->fini_function
6643 ? elf_link_hash_lookup (elf_hash_table (info),
6644 info->fini_function, FALSE,
6645 FALSE, FALSE)
6646 : NULL);
6647 if (h != NULL
6648 && (h->ref_regular
6649 || h->def_regular))
6650 {
6651 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6652 return FALSE;
6653 }
6654
6655 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6656 if (s != NULL && s->linker_has_input)
6657 {
6658 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6659 if (! bfd_link_executable (info))
6660 {
6661 bfd *sub;
6662 asection *o;
6663
6664 for (sub = info->input_bfds; sub != NULL;
6665 sub = sub->link.next)
6666 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6667 for (o = sub->sections; o != NULL; o = o->next)
6668 if (elf_section_data (o)->this_hdr.sh_type
6669 == SHT_PREINIT_ARRAY)
6670 {
6671 _bfd_error_handler
6672 (_("%B: .preinit_array section is not allowed in DSO"),
6673 sub);
6674 break;
6675 }
6676
6677 bfd_set_error (bfd_error_nonrepresentable_section);
6678 return FALSE;
6679 }
6680
6681 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6682 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6683 return FALSE;
6684 }
6685 s = bfd_get_section_by_name (output_bfd, ".init_array");
6686 if (s != NULL && s->linker_has_input)
6687 {
6688 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6689 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6690 return FALSE;
6691 }
6692 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6693 if (s != NULL && s->linker_has_input)
6694 {
6695 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6696 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6697 return FALSE;
6698 }
6699
6700 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6701 /* If .dynstr is excluded from the link, we don't want any of
6702 these tags. Strictly, we should be checking each section
6703 individually; This quick check covers for the case where
6704 someone does a /DISCARD/ : { *(*) }. */
6705 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6706 {
6707 bfd_size_type strsize;
6708
6709 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6710 if ((info->emit_hash
6711 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6712 || (info->emit_gnu_hash
6713 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6714 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6715 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6716 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6717 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6718 bed->s->sizeof_sym))
6719 return FALSE;
6720 }
6721 }
6722
6723 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6724 return FALSE;
6725
6726 /* The backend must work out the sizes of all the other dynamic
6727 sections. */
6728 if (dynobj != NULL
6729 && bed->elf_backend_size_dynamic_sections != NULL
6730 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6731 return FALSE;
6732
6733 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6734 {
6735 unsigned long section_sym_count;
6736
6737 if (elf_tdata (output_bfd)->cverdefs)
6738 {
6739 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6740
6741 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6742 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6743 return FALSE;
6744 }
6745
6746 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6747 {
6748 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6749 return FALSE;
6750 }
6751 else if (info->flags & DF_BIND_NOW)
6752 {
6753 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6754 return FALSE;
6755 }
6756
6757 if (info->flags_1)
6758 {
6759 if (bfd_link_executable (info))
6760 info->flags_1 &= ~ (DF_1_INITFIRST
6761 | DF_1_NODELETE
6762 | DF_1_NOOPEN);
6763 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6764 return FALSE;
6765 }
6766
6767 if (elf_tdata (output_bfd)->cverrefs)
6768 {
6769 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6770
6771 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6772 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6773 return FALSE;
6774 }
5a580b3a 6775
8423293d
AM
6776 if ((elf_tdata (output_bfd)->cverrefs == 0
6777 && elf_tdata (output_bfd)->cverdefs == 0)
6778 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6779 &section_sym_count) == 0)
6780 {
902e9fc7
MR
6781 asection *s;
6782
3d4d4302 6783 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6784 s->flags |= SEC_EXCLUDE;
6785 }
6786 }
6787 return TRUE;
6788}
6789
74541ad4
AM
6790/* Find the first non-excluded output section. We'll use its
6791 section symbol for some emitted relocs. */
6792void
6793_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6794{
6795 asection *s;
6796
6797 for (s = output_bfd->sections; s != NULL; s = s->next)
6798 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6799 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6800 {
6801 elf_hash_table (info)->text_index_section = s;
6802 break;
6803 }
6804}
6805
6806/* Find two non-excluded output sections, one for code, one for data.
6807 We'll use their section symbols for some emitted relocs. */
6808void
6809_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6810{
6811 asection *s;
6812
266b05cf
DJ
6813 /* Data first, since setting text_index_section changes
6814 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6815 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6816 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6817 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6818 {
266b05cf 6819 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6820 break;
6821 }
6822
6823 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6824 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6825 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6826 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6827 {
266b05cf 6828 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6829 break;
6830 }
6831
6832 if (elf_hash_table (info)->text_index_section == NULL)
6833 elf_hash_table (info)->text_index_section
6834 = elf_hash_table (info)->data_index_section;
6835}
6836
8423293d
AM
6837bfd_boolean
6838bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6839{
74541ad4 6840 const struct elf_backend_data *bed;
23ec1e32
MR
6841 unsigned long section_sym_count;
6842 bfd_size_type dynsymcount;
74541ad4 6843
8423293d
AM
6844 if (!is_elf_hash_table (info->hash))
6845 return TRUE;
6846
74541ad4
AM
6847 bed = get_elf_backend_data (output_bfd);
6848 (*bed->elf_backend_init_index_section) (output_bfd, info);
6849
23ec1e32
MR
6850 /* Assign dynsym indices. In a shared library we generate a section
6851 symbol for each output section, which come first. Next come all
6852 of the back-end allocated local dynamic syms, followed by the rest
6853 of the global symbols.
6854
6855 This is usually not needed for static binaries, however backends
6856 can request to always do it, e.g. the MIPS backend uses dynamic
6857 symbol counts to lay out GOT, which will be produced in the
6858 presence of GOT relocations even in static binaries (holding fixed
6859 data in that case, to satisfy those relocations). */
6860
6861 if (elf_hash_table (info)->dynamic_sections_created
6862 || bed->always_renumber_dynsyms)
6863 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6864 &section_sym_count);
6865
8423293d
AM
6866 if (elf_hash_table (info)->dynamic_sections_created)
6867 {
6868 bfd *dynobj;
8423293d 6869 asection *s;
8423293d
AM
6870 unsigned int dtagcount;
6871
6872 dynobj = elf_hash_table (info)->dynobj;
6873
5a580b3a 6874 /* Work out the size of the symbol version section. */
3d4d4302 6875 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6876 BFD_ASSERT (s != NULL);
d5486c43 6877 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6878 {
eea6121a 6879 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6880 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6881 if (s->contents == NULL)
6882 return FALSE;
6883
6884 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6885 return FALSE;
6886 }
6887
6888 /* Set the size of the .dynsym and .hash sections. We counted
6889 the number of dynamic symbols in elf_link_add_object_symbols.
6890 We will build the contents of .dynsym and .hash when we build
6891 the final symbol table, because until then we do not know the
6892 correct value to give the symbols. We built the .dynstr
6893 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6894 s = elf_hash_table (info)->dynsym;
5a580b3a 6895 BFD_ASSERT (s != NULL);
eea6121a 6896 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6897
d5486c43
L
6898 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6899 if (s->contents == NULL)
6900 return FALSE;
5a580b3a 6901
d5486c43
L
6902 /* The first entry in .dynsym is a dummy symbol. Clear all the
6903 section syms, in case we don't output them all. */
6904 ++section_sym_count;
6905 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6906
fdc90cb4
JJ
6907 elf_hash_table (info)->bucketcount = 0;
6908
5a580b3a
AM
6909 /* Compute the size of the hashing table. As a side effect this
6910 computes the hash values for all the names we export. */
fdc90cb4
JJ
6911 if (info->emit_hash)
6912 {
6913 unsigned long int *hashcodes;
14b1c01e 6914 struct hash_codes_info hashinf;
fdc90cb4
JJ
6915 bfd_size_type amt;
6916 unsigned long int nsyms;
6917 size_t bucketcount;
6918 size_t hash_entry_size;
6919
6920 /* Compute the hash values for all exported symbols. At the same
6921 time store the values in an array so that we could use them for
6922 optimizations. */
6923 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6924 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6925 if (hashcodes == NULL)
6926 return FALSE;
14b1c01e
AM
6927 hashinf.hashcodes = hashcodes;
6928 hashinf.error = FALSE;
5a580b3a 6929
fdc90cb4
JJ
6930 /* Put all hash values in HASHCODES. */
6931 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6932 elf_collect_hash_codes, &hashinf);
6933 if (hashinf.error)
4dd07732
AM
6934 {
6935 free (hashcodes);
6936 return FALSE;
6937 }
5a580b3a 6938
14b1c01e 6939 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6940 bucketcount
6941 = compute_bucket_count (info, hashcodes, nsyms, 0);
6942 free (hashcodes);
6943
6944 if (bucketcount == 0)
6945 return FALSE;
5a580b3a 6946
fdc90cb4
JJ
6947 elf_hash_table (info)->bucketcount = bucketcount;
6948
3d4d4302 6949 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6950 BFD_ASSERT (s != NULL);
6951 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6952 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6953 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6954 if (s->contents == NULL)
6955 return FALSE;
6956
6957 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6958 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6959 s->contents + hash_entry_size);
6960 }
6961
6962 if (info->emit_gnu_hash)
6963 {
6964 size_t i, cnt;
6965 unsigned char *contents;
6966 struct collect_gnu_hash_codes cinfo;
6967 bfd_size_type amt;
6968 size_t bucketcount;
6969
6970 memset (&cinfo, 0, sizeof (cinfo));
6971
6972 /* Compute the hash values for all exported symbols. At the same
6973 time store the values in an array so that we could use them for
6974 optimizations. */
6975 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6976 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6977 if (cinfo.hashcodes == NULL)
6978 return FALSE;
6979
6980 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6981 cinfo.min_dynindx = -1;
6982 cinfo.output_bfd = output_bfd;
6983 cinfo.bed = bed;
6984
6985 /* Put all hash values in HASHCODES. */
6986 elf_link_hash_traverse (elf_hash_table (info),
6987 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6988 if (cinfo.error)
4dd07732
AM
6989 {
6990 free (cinfo.hashcodes);
6991 return FALSE;
6992 }
fdc90cb4
JJ
6993
6994 bucketcount
6995 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6996
6997 if (bucketcount == 0)
6998 {
6999 free (cinfo.hashcodes);
7000 return FALSE;
7001 }
7002
3d4d4302 7003 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7004 BFD_ASSERT (s != NULL);
7005
7006 if (cinfo.nsyms == 0)
7007 {
7008 /* Empty .gnu.hash section is special. */
7009 BFD_ASSERT (cinfo.min_dynindx == -1);
7010 free (cinfo.hashcodes);
7011 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7012 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7013 if (contents == NULL)
7014 return FALSE;
7015 s->contents = contents;
7016 /* 1 empty bucket. */
7017 bfd_put_32 (output_bfd, 1, contents);
7018 /* SYMIDX above the special symbol 0. */
7019 bfd_put_32 (output_bfd, 1, contents + 4);
7020 /* Just one word for bitmask. */
7021 bfd_put_32 (output_bfd, 1, contents + 8);
7022 /* Only hash fn bloom filter. */
7023 bfd_put_32 (output_bfd, 0, contents + 12);
7024 /* No hashes are valid - empty bitmask. */
7025 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7026 /* No hashes in the only bucket. */
7027 bfd_put_32 (output_bfd, 0,
7028 contents + 16 + bed->s->arch_size / 8);
7029 }
7030 else
7031 {
9e6619e2 7032 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7033 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7034
9e6619e2
AM
7035 x = cinfo.nsyms;
7036 maskbitslog2 = 1;
7037 while ((x >>= 1) != 0)
7038 ++maskbitslog2;
fdc90cb4
JJ
7039 if (maskbitslog2 < 3)
7040 maskbitslog2 = 5;
7041 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7042 maskbitslog2 = maskbitslog2 + 3;
7043 else
7044 maskbitslog2 = maskbitslog2 + 2;
7045 if (bed->s->arch_size == 64)
7046 {
7047 if (maskbitslog2 == 5)
7048 maskbitslog2 = 6;
7049 cinfo.shift1 = 6;
7050 }
7051 else
7052 cinfo.shift1 = 5;
7053 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7054 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7055 cinfo.maskbits = 1 << maskbitslog2;
7056 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7057 amt = bucketcount * sizeof (unsigned long int) * 2;
7058 amt += maskwords * sizeof (bfd_vma);
a50b1753 7059 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7060 if (cinfo.bitmask == NULL)
7061 {
7062 free (cinfo.hashcodes);
7063 return FALSE;
7064 }
7065
a50b1753 7066 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7067 cinfo.indx = cinfo.counts + bucketcount;
7068 cinfo.symindx = dynsymcount - cinfo.nsyms;
7069 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7070
7071 /* Determine how often each hash bucket is used. */
7072 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7073 for (i = 0; i < cinfo.nsyms; ++i)
7074 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7075
7076 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7077 if (cinfo.counts[i] != 0)
7078 {
7079 cinfo.indx[i] = cnt;
7080 cnt += cinfo.counts[i];
7081 }
7082 BFD_ASSERT (cnt == dynsymcount);
7083 cinfo.bucketcount = bucketcount;
7084 cinfo.local_indx = cinfo.min_dynindx;
7085
7086 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7087 s->size += cinfo.maskbits / 8;
a50b1753 7088 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7089 if (contents == NULL)
7090 {
7091 free (cinfo.bitmask);
7092 free (cinfo.hashcodes);
7093 return FALSE;
7094 }
7095
7096 s->contents = contents;
7097 bfd_put_32 (output_bfd, bucketcount, contents);
7098 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7099 bfd_put_32 (output_bfd, maskwords, contents + 8);
7100 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7101 contents += 16 + cinfo.maskbits / 8;
7102
7103 for (i = 0; i < bucketcount; ++i)
7104 {
7105 if (cinfo.counts[i] == 0)
7106 bfd_put_32 (output_bfd, 0, contents);
7107 else
7108 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7109 contents += 4;
7110 }
7111
7112 cinfo.contents = contents;
7113
7114 /* Renumber dynamic symbols, populate .gnu.hash section. */
7115 elf_link_hash_traverse (elf_hash_table (info),
7116 elf_renumber_gnu_hash_syms, &cinfo);
7117
7118 contents = s->contents + 16;
7119 for (i = 0; i < maskwords; ++i)
7120 {
7121 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7122 contents);
7123 contents += bed->s->arch_size / 8;
7124 }
7125
7126 free (cinfo.bitmask);
7127 free (cinfo.hashcodes);
7128 }
7129 }
5a580b3a 7130
3d4d4302 7131 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7132 BFD_ASSERT (s != NULL);
7133
4ad4eba5 7134 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7135
eea6121a 7136 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7137
7138 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7139 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7140 return FALSE;
7141 }
7142
7143 return TRUE;
7144}
4d269e42 7145\f
4d269e42
AM
7146/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7147
7148static void
7149merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7150 asection *sec)
7151{
dbaa2011
AM
7152 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7153 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7154}
7155
7156/* Finish SHF_MERGE section merging. */
7157
7158bfd_boolean
630993ec 7159_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7160{
7161 bfd *ibfd;
7162 asection *sec;
7163
7164 if (!is_elf_hash_table (info->hash))
7165 return FALSE;
7166
c72f2fb2 7167 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7168 if ((ibfd->flags & DYNAMIC) == 0
7169 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7170 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7171 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7172 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7173 if ((sec->flags & SEC_MERGE) != 0
7174 && !bfd_is_abs_section (sec->output_section))
7175 {
7176 struct bfd_elf_section_data *secdata;
7177
7178 secdata = elf_section_data (sec);
630993ec 7179 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7180 &elf_hash_table (info)->merge_info,
7181 sec, &secdata->sec_info))
7182 return FALSE;
7183 else if (secdata->sec_info)
dbaa2011 7184 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7185 }
7186
7187 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7188 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7189 merge_sections_remove_hook);
7190 return TRUE;
7191}
7192
7193/* Create an entry in an ELF linker hash table. */
7194
7195struct bfd_hash_entry *
7196_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7197 struct bfd_hash_table *table,
7198 const char *string)
7199{
7200 /* Allocate the structure if it has not already been allocated by a
7201 subclass. */
7202 if (entry == NULL)
7203 {
a50b1753 7204 entry = (struct bfd_hash_entry *)
ca4be51c 7205 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7206 if (entry == NULL)
7207 return entry;
7208 }
7209
7210 /* Call the allocation method of the superclass. */
7211 entry = _bfd_link_hash_newfunc (entry, table, string);
7212 if (entry != NULL)
7213 {
7214 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7215 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7216
7217 /* Set local fields. */
7218 ret->indx = -1;
7219 ret->dynindx = -1;
7220 ret->got = htab->init_got_refcount;
7221 ret->plt = htab->init_plt_refcount;
7222 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7223 - offsetof (struct elf_link_hash_entry, size)));
7224 /* Assume that we have been called by a non-ELF symbol reader.
7225 This flag is then reset by the code which reads an ELF input
7226 file. This ensures that a symbol created by a non-ELF symbol
7227 reader will have the flag set correctly. */
7228 ret->non_elf = 1;
7229 }
7230
7231 return entry;
7232}
7233
7234/* Copy data from an indirect symbol to its direct symbol, hiding the
7235 old indirect symbol. Also used for copying flags to a weakdef. */
7236
7237void
7238_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7239 struct elf_link_hash_entry *dir,
7240 struct elf_link_hash_entry *ind)
7241{
7242 struct elf_link_hash_table *htab;
7243
7244 /* Copy down any references that we may have already seen to the
e81830c5 7245 symbol which just became indirect. */
4d269e42 7246
422f1182 7247 if (dir->versioned != versioned_hidden)
e81830c5
AM
7248 dir->ref_dynamic |= ind->ref_dynamic;
7249 dir->ref_regular |= ind->ref_regular;
7250 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7251 dir->non_got_ref |= ind->non_got_ref;
7252 dir->needs_plt |= ind->needs_plt;
7253 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7254
7255 if (ind->root.type != bfd_link_hash_indirect)
7256 return;
7257
7258 /* Copy over the global and procedure linkage table refcount entries.
7259 These may have been already set up by a check_relocs routine. */
7260 htab = elf_hash_table (info);
7261 if (ind->got.refcount > htab->init_got_refcount.refcount)
7262 {
7263 if (dir->got.refcount < 0)
7264 dir->got.refcount = 0;
7265 dir->got.refcount += ind->got.refcount;
7266 ind->got.refcount = htab->init_got_refcount.refcount;
7267 }
7268
7269 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7270 {
7271 if (dir->plt.refcount < 0)
7272 dir->plt.refcount = 0;
7273 dir->plt.refcount += ind->plt.refcount;
7274 ind->plt.refcount = htab->init_plt_refcount.refcount;
7275 }
7276
7277 if (ind->dynindx != -1)
7278 {
7279 if (dir->dynindx != -1)
7280 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7281 dir->dynindx = ind->dynindx;
7282 dir->dynstr_index = ind->dynstr_index;
7283 ind->dynindx = -1;
7284 ind->dynstr_index = 0;
7285 }
7286}
7287
7288void
7289_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7290 struct elf_link_hash_entry *h,
7291 bfd_boolean force_local)
7292{
3aa14d16
L
7293 /* STT_GNU_IFUNC symbol must go through PLT. */
7294 if (h->type != STT_GNU_IFUNC)
7295 {
7296 h->plt = elf_hash_table (info)->init_plt_offset;
7297 h->needs_plt = 0;
7298 }
4d269e42
AM
7299 if (force_local)
7300 {
7301 h->forced_local = 1;
7302 if (h->dynindx != -1)
7303 {
4d269e42
AM
7304 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7305 h->dynstr_index);
641338d8
AM
7306 h->dynindx = -1;
7307 h->dynstr_index = 0;
4d269e42
AM
7308 }
7309 }
7310}
7311
7bf52ea2
AM
7312/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7313 caller. */
4d269e42
AM
7314
7315bfd_boolean
7316_bfd_elf_link_hash_table_init
7317 (struct elf_link_hash_table *table,
7318 bfd *abfd,
7319 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7320 struct bfd_hash_table *,
7321 const char *),
4dfe6ac6
NC
7322 unsigned int entsize,
7323 enum elf_target_id target_id)
4d269e42
AM
7324{
7325 bfd_boolean ret;
7326 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7327
4d269e42
AM
7328 table->init_got_refcount.refcount = can_refcount - 1;
7329 table->init_plt_refcount.refcount = can_refcount - 1;
7330 table->init_got_offset.offset = -(bfd_vma) 1;
7331 table->init_plt_offset.offset = -(bfd_vma) 1;
7332 /* The first dynamic symbol is a dummy. */
7333 table->dynsymcount = 1;
7334
7335 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7336
4d269e42 7337 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7338 table->hash_table_id = target_id;
4d269e42
AM
7339
7340 return ret;
7341}
7342
7343/* Create an ELF linker hash table. */
7344
7345struct bfd_link_hash_table *
7346_bfd_elf_link_hash_table_create (bfd *abfd)
7347{
7348 struct elf_link_hash_table *ret;
7349 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7350
7bf52ea2 7351 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7352 if (ret == NULL)
7353 return NULL;
7354
7355 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7356 sizeof (struct elf_link_hash_entry),
7357 GENERIC_ELF_DATA))
4d269e42
AM
7358 {
7359 free (ret);
7360 return NULL;
7361 }
d495ab0d 7362 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7363
7364 return &ret->root;
7365}
7366
9f7c3e5e
AM
7367/* Destroy an ELF linker hash table. */
7368
7369void
d495ab0d 7370_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7371{
d495ab0d
AM
7372 struct elf_link_hash_table *htab;
7373
7374 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7375 if (htab->dynstr != NULL)
7376 _bfd_elf_strtab_free (htab->dynstr);
7377 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7378 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7379}
7380
4d269e42
AM
7381/* This is a hook for the ELF emulation code in the generic linker to
7382 tell the backend linker what file name to use for the DT_NEEDED
7383 entry for a dynamic object. */
7384
7385void
7386bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7387{
7388 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7389 && bfd_get_format (abfd) == bfd_object)
7390 elf_dt_name (abfd) = name;
7391}
7392
7393int
7394bfd_elf_get_dyn_lib_class (bfd *abfd)
7395{
7396 int lib_class;
7397 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7398 && bfd_get_format (abfd) == bfd_object)
7399 lib_class = elf_dyn_lib_class (abfd);
7400 else
7401 lib_class = 0;
7402 return lib_class;
7403}
7404
7405void
7406bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7407{
7408 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7409 && bfd_get_format (abfd) == bfd_object)
7410 elf_dyn_lib_class (abfd) = lib_class;
7411}
7412
7413/* Get the list of DT_NEEDED entries for a link. This is a hook for
7414 the linker ELF emulation code. */
7415
7416struct bfd_link_needed_list *
7417bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7418 struct bfd_link_info *info)
7419{
7420 if (! is_elf_hash_table (info->hash))
7421 return NULL;
7422 return elf_hash_table (info)->needed;
7423}
7424
7425/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7426 hook for the linker ELF emulation code. */
7427
7428struct bfd_link_needed_list *
7429bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7430 struct bfd_link_info *info)
7431{
7432 if (! is_elf_hash_table (info->hash))
7433 return NULL;
7434 return elf_hash_table (info)->runpath;
7435}
7436
7437/* Get the name actually used for a dynamic object for a link. This
7438 is the SONAME entry if there is one. Otherwise, it is the string
7439 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7440
7441const char *
7442bfd_elf_get_dt_soname (bfd *abfd)
7443{
7444 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7445 && bfd_get_format (abfd) == bfd_object)
7446 return elf_dt_name (abfd);
7447 return NULL;
7448}
7449
7450/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7451 the ELF linker emulation code. */
7452
7453bfd_boolean
7454bfd_elf_get_bfd_needed_list (bfd *abfd,
7455 struct bfd_link_needed_list **pneeded)
7456{
7457 asection *s;
7458 bfd_byte *dynbuf = NULL;
cb33740c 7459 unsigned int elfsec;
4d269e42
AM
7460 unsigned long shlink;
7461 bfd_byte *extdyn, *extdynend;
7462 size_t extdynsize;
7463 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7464
7465 *pneeded = NULL;
7466
7467 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7468 || bfd_get_format (abfd) != bfd_object)
7469 return TRUE;
7470
7471 s = bfd_get_section_by_name (abfd, ".dynamic");
7472 if (s == NULL || s->size == 0)
7473 return TRUE;
7474
7475 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7476 goto error_return;
7477
7478 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7479 if (elfsec == SHN_BAD)
4d269e42
AM
7480 goto error_return;
7481
7482 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7483
4d269e42
AM
7484 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7485 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7486
7487 extdyn = dynbuf;
7488 extdynend = extdyn + s->size;
7489 for (; extdyn < extdynend; extdyn += extdynsize)
7490 {
7491 Elf_Internal_Dyn dyn;
7492
7493 (*swap_dyn_in) (abfd, extdyn, &dyn);
7494
7495 if (dyn.d_tag == DT_NULL)
7496 break;
7497
7498 if (dyn.d_tag == DT_NEEDED)
7499 {
7500 const char *string;
7501 struct bfd_link_needed_list *l;
7502 unsigned int tagv = dyn.d_un.d_val;
7503 bfd_size_type amt;
7504
7505 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7506 if (string == NULL)
7507 goto error_return;
7508
7509 amt = sizeof *l;
a50b1753 7510 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7511 if (l == NULL)
7512 goto error_return;
7513
7514 l->by = abfd;
7515 l->name = string;
7516 l->next = *pneeded;
7517 *pneeded = l;
7518 }
7519 }
7520
7521 free (dynbuf);
7522
7523 return TRUE;
7524
7525 error_return:
7526 if (dynbuf != NULL)
7527 free (dynbuf);
7528 return FALSE;
7529}
7530
7531struct elf_symbuf_symbol
7532{
7533 unsigned long st_name; /* Symbol name, index in string tbl */
7534 unsigned char st_info; /* Type and binding attributes */
7535 unsigned char st_other; /* Visibilty, and target specific */
7536};
7537
7538struct elf_symbuf_head
7539{
7540 struct elf_symbuf_symbol *ssym;
ef53be89 7541 size_t count;
4d269e42
AM
7542 unsigned int st_shndx;
7543};
7544
7545struct elf_symbol
7546{
7547 union
7548 {
7549 Elf_Internal_Sym *isym;
7550 struct elf_symbuf_symbol *ssym;
7551 } u;
7552 const char *name;
7553};
7554
7555/* Sort references to symbols by ascending section number. */
7556
7557static int
7558elf_sort_elf_symbol (const void *arg1, const void *arg2)
7559{
7560 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7561 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7562
7563 return s1->st_shndx - s2->st_shndx;
7564}
7565
7566static int
7567elf_sym_name_compare (const void *arg1, const void *arg2)
7568{
7569 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7570 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7571 return strcmp (s1->name, s2->name);
7572}
7573
7574static struct elf_symbuf_head *
ef53be89 7575elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7576{
14b1c01e 7577 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7578 struct elf_symbuf_symbol *ssym;
7579 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7580 size_t i, shndx_count, total_size;
4d269e42 7581
a50b1753 7582 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7583 if (indbuf == NULL)
7584 return NULL;
7585
7586 for (ind = indbuf, i = 0; i < symcount; i++)
7587 if (isymbuf[i].st_shndx != SHN_UNDEF)
7588 *ind++ = &isymbuf[i];
7589 indbufend = ind;
7590
7591 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7592 elf_sort_elf_symbol);
7593
7594 shndx_count = 0;
7595 if (indbufend > indbuf)
7596 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7597 if (ind[0]->st_shndx != ind[1]->st_shndx)
7598 shndx_count++;
7599
3ae181ee
L
7600 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7601 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7602 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7603 if (ssymbuf == NULL)
7604 {
7605 free (indbuf);
7606 return NULL;
7607 }
7608
3ae181ee 7609 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7610 ssymbuf->ssym = NULL;
7611 ssymbuf->count = shndx_count;
7612 ssymbuf->st_shndx = 0;
7613 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7614 {
7615 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7616 {
7617 ssymhead++;
7618 ssymhead->ssym = ssym;
7619 ssymhead->count = 0;
7620 ssymhead->st_shndx = (*ind)->st_shndx;
7621 }
7622 ssym->st_name = (*ind)->st_name;
7623 ssym->st_info = (*ind)->st_info;
7624 ssym->st_other = (*ind)->st_other;
7625 ssymhead->count++;
7626 }
ef53be89 7627 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7628 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7629 == total_size));
4d269e42
AM
7630
7631 free (indbuf);
7632 return ssymbuf;
7633}
7634
7635/* Check if 2 sections define the same set of local and global
7636 symbols. */
7637
8f317e31 7638static bfd_boolean
4d269e42
AM
7639bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7640 struct bfd_link_info *info)
7641{
7642 bfd *bfd1, *bfd2;
7643 const struct elf_backend_data *bed1, *bed2;
7644 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7645 size_t symcount1, symcount2;
4d269e42
AM
7646 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7647 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7648 Elf_Internal_Sym *isym, *isymend;
7649 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7650 size_t count1, count2, i;
cb33740c 7651 unsigned int shndx1, shndx2;
4d269e42
AM
7652 bfd_boolean result;
7653
7654 bfd1 = sec1->owner;
7655 bfd2 = sec2->owner;
7656
4d269e42
AM
7657 /* Both sections have to be in ELF. */
7658 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7659 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7660 return FALSE;
7661
7662 if (elf_section_type (sec1) != elf_section_type (sec2))
7663 return FALSE;
7664
4d269e42
AM
7665 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7666 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7667 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7668 return FALSE;
7669
7670 bed1 = get_elf_backend_data (bfd1);
7671 bed2 = get_elf_backend_data (bfd2);
7672 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7673 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7674 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7675 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7676
7677 if (symcount1 == 0 || symcount2 == 0)
7678 return FALSE;
7679
7680 result = FALSE;
7681 isymbuf1 = NULL;
7682 isymbuf2 = NULL;
a50b1753
NC
7683 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7684 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7685
7686 if (ssymbuf1 == NULL)
7687 {
7688 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7689 NULL, NULL, NULL);
7690 if (isymbuf1 == NULL)
7691 goto done;
7692
7693 if (!info->reduce_memory_overheads)
7694 elf_tdata (bfd1)->symbuf = ssymbuf1
7695 = elf_create_symbuf (symcount1, isymbuf1);
7696 }
7697
7698 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7699 {
7700 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7701 NULL, NULL, NULL);
7702 if (isymbuf2 == NULL)
7703 goto done;
7704
7705 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7706 elf_tdata (bfd2)->symbuf = ssymbuf2
7707 = elf_create_symbuf (symcount2, isymbuf2);
7708 }
7709
7710 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7711 {
7712 /* Optimized faster version. */
ef53be89 7713 size_t lo, hi, mid;
4d269e42
AM
7714 struct elf_symbol *symp;
7715 struct elf_symbuf_symbol *ssym, *ssymend;
7716
7717 lo = 0;
7718 hi = ssymbuf1->count;
7719 ssymbuf1++;
7720 count1 = 0;
7721 while (lo < hi)
7722 {
7723 mid = (lo + hi) / 2;
cb33740c 7724 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7725 hi = mid;
cb33740c 7726 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7727 lo = mid + 1;
7728 else
7729 {
7730 count1 = ssymbuf1[mid].count;
7731 ssymbuf1 += mid;
7732 break;
7733 }
7734 }
7735
7736 lo = 0;
7737 hi = ssymbuf2->count;
7738 ssymbuf2++;
7739 count2 = 0;
7740 while (lo < hi)
7741 {
7742 mid = (lo + hi) / 2;
cb33740c 7743 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7744 hi = mid;
cb33740c 7745 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7746 lo = mid + 1;
7747 else
7748 {
7749 count2 = ssymbuf2[mid].count;
7750 ssymbuf2 += mid;
7751 break;
7752 }
7753 }
7754
7755 if (count1 == 0 || count2 == 0 || count1 != count2)
7756 goto done;
7757
ca4be51c
AM
7758 symtable1
7759 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7760 symtable2
7761 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7762 if (symtable1 == NULL || symtable2 == NULL)
7763 goto done;
7764
7765 symp = symtable1;
7766 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7767 ssym < ssymend; ssym++, symp++)
7768 {
7769 symp->u.ssym = ssym;
7770 symp->name = bfd_elf_string_from_elf_section (bfd1,
7771 hdr1->sh_link,
7772 ssym->st_name);
7773 }
7774
7775 symp = symtable2;
7776 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7777 ssym < ssymend; ssym++, symp++)
7778 {
7779 symp->u.ssym = ssym;
7780 symp->name = bfd_elf_string_from_elf_section (bfd2,
7781 hdr2->sh_link,
7782 ssym->st_name);
7783 }
7784
7785 /* Sort symbol by name. */
7786 qsort (symtable1, count1, sizeof (struct elf_symbol),
7787 elf_sym_name_compare);
7788 qsort (symtable2, count1, sizeof (struct elf_symbol),
7789 elf_sym_name_compare);
7790
7791 for (i = 0; i < count1; i++)
7792 /* Two symbols must have the same binding, type and name. */
7793 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7794 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7795 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7796 goto done;
7797
7798 result = TRUE;
7799 goto done;
7800 }
7801
a50b1753
NC
7802 symtable1 = (struct elf_symbol *)
7803 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7804 symtable2 = (struct elf_symbol *)
7805 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7806 if (symtable1 == NULL || symtable2 == NULL)
7807 goto done;
7808
7809 /* Count definitions in the section. */
7810 count1 = 0;
7811 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7812 if (isym->st_shndx == shndx1)
4d269e42
AM
7813 symtable1[count1++].u.isym = isym;
7814
7815 count2 = 0;
7816 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7817 if (isym->st_shndx == shndx2)
4d269e42
AM
7818 symtable2[count2++].u.isym = isym;
7819
7820 if (count1 == 0 || count2 == 0 || count1 != count2)
7821 goto done;
7822
7823 for (i = 0; i < count1; i++)
7824 symtable1[i].name
7825 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7826 symtable1[i].u.isym->st_name);
7827
7828 for (i = 0; i < count2; i++)
7829 symtable2[i].name
7830 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7831 symtable2[i].u.isym->st_name);
7832
7833 /* Sort symbol by name. */
7834 qsort (symtable1, count1, sizeof (struct elf_symbol),
7835 elf_sym_name_compare);
7836 qsort (symtable2, count1, sizeof (struct elf_symbol),
7837 elf_sym_name_compare);
7838
7839 for (i = 0; i < count1; i++)
7840 /* Two symbols must have the same binding, type and name. */
7841 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7842 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7843 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7844 goto done;
7845
7846 result = TRUE;
7847
7848done:
7849 if (symtable1)
7850 free (symtable1);
7851 if (symtable2)
7852 free (symtable2);
7853 if (isymbuf1)
7854 free (isymbuf1);
7855 if (isymbuf2)
7856 free (isymbuf2);
7857
7858 return result;
7859}
7860
7861/* Return TRUE if 2 section types are compatible. */
7862
7863bfd_boolean
7864_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7865 bfd *bbfd, const asection *bsec)
7866{
7867 if (asec == NULL
7868 || bsec == NULL
7869 || abfd->xvec->flavour != bfd_target_elf_flavour
7870 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7871 return TRUE;
7872
7873 return elf_section_type (asec) == elf_section_type (bsec);
7874}
7875\f
c152c796
AM
7876/* Final phase of ELF linker. */
7877
7878/* A structure we use to avoid passing large numbers of arguments. */
7879
7880struct elf_final_link_info
7881{
7882 /* General link information. */
7883 struct bfd_link_info *info;
7884 /* Output BFD. */
7885 bfd *output_bfd;
7886 /* Symbol string table. */
ef10c3ac 7887 struct elf_strtab_hash *symstrtab;
c152c796
AM
7888 /* .hash section. */
7889 asection *hash_sec;
7890 /* symbol version section (.gnu.version). */
7891 asection *symver_sec;
7892 /* Buffer large enough to hold contents of any section. */
7893 bfd_byte *contents;
7894 /* Buffer large enough to hold external relocs of any section. */
7895 void *external_relocs;
7896 /* Buffer large enough to hold internal relocs of any section. */
7897 Elf_Internal_Rela *internal_relocs;
7898 /* Buffer large enough to hold external local symbols of any input
7899 BFD. */
7900 bfd_byte *external_syms;
7901 /* And a buffer for symbol section indices. */
7902 Elf_External_Sym_Shndx *locsym_shndx;
7903 /* Buffer large enough to hold internal local symbols of any input
7904 BFD. */
7905 Elf_Internal_Sym *internal_syms;
7906 /* Array large enough to hold a symbol index for each local symbol
7907 of any input BFD. */
7908 long *indices;
7909 /* Array large enough to hold a section pointer for each local
7910 symbol of any input BFD. */
7911 asection **sections;
ef10c3ac 7912 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7913 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7914 /* Number of STT_FILE syms seen. */
7915 size_t filesym_count;
c152c796
AM
7916};
7917
7918/* This struct is used to pass information to elf_link_output_extsym. */
7919
7920struct elf_outext_info
7921{
7922 bfd_boolean failed;
7923 bfd_boolean localsyms;
34a79995 7924 bfd_boolean file_sym_done;
8b127cbc 7925 struct elf_final_link_info *flinfo;
c152c796
AM
7926};
7927
d9352518
DB
7928
7929/* Support for evaluating a complex relocation.
7930
7931 Complex relocations are generalized, self-describing relocations. The
7932 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7933 relocations themselves.
d9352518
DB
7934
7935 The relocations are use a reserved elf-wide relocation type code (R_RELC
7936 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7937 information (start bit, end bit, word width, etc) into the addend. This
7938 information is extracted from CGEN-generated operand tables within gas.
7939
7940 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7941 internal) representing prefix-notation expressions, including but not
7942 limited to those sorts of expressions normally encoded as addends in the
7943 addend field. The symbol mangling format is:
7944
7945 <node> := <literal>
7946 | <unary-operator> ':' <node>
7947 | <binary-operator> ':' <node> ':' <node>
7948 ;
7949
7950 <literal> := 's' <digits=N> ':' <N character symbol name>
7951 | 'S' <digits=N> ':' <N character section name>
7952 | '#' <hexdigits>
7953 ;
7954
7955 <binary-operator> := as in C
7956 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7957
7958static void
a0c8462f
AM
7959set_symbol_value (bfd *bfd_with_globals,
7960 Elf_Internal_Sym *isymbuf,
7961 size_t locsymcount,
7962 size_t symidx,
7963 bfd_vma val)
d9352518 7964{
8977835c
AM
7965 struct elf_link_hash_entry **sym_hashes;
7966 struct elf_link_hash_entry *h;
7967 size_t extsymoff = locsymcount;
d9352518 7968
8977835c 7969 if (symidx < locsymcount)
d9352518 7970 {
8977835c
AM
7971 Elf_Internal_Sym *sym;
7972
7973 sym = isymbuf + symidx;
7974 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7975 {
7976 /* It is a local symbol: move it to the
7977 "absolute" section and give it a value. */
7978 sym->st_shndx = SHN_ABS;
7979 sym->st_value = val;
7980 return;
7981 }
7982 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7983 extsymoff = 0;
d9352518 7984 }
8977835c
AM
7985
7986 /* It is a global symbol: set its link type
7987 to "defined" and give it a value. */
7988
7989 sym_hashes = elf_sym_hashes (bfd_with_globals);
7990 h = sym_hashes [symidx - extsymoff];
7991 while (h->root.type == bfd_link_hash_indirect
7992 || h->root.type == bfd_link_hash_warning)
7993 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7994 h->root.type = bfd_link_hash_defined;
7995 h->root.u.def.value = val;
7996 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7997}
7998
a0c8462f
AM
7999static bfd_boolean
8000resolve_symbol (const char *name,
8001 bfd *input_bfd,
8b127cbc 8002 struct elf_final_link_info *flinfo,
a0c8462f
AM
8003 bfd_vma *result,
8004 Elf_Internal_Sym *isymbuf,
8005 size_t locsymcount)
d9352518 8006{
a0c8462f
AM
8007 Elf_Internal_Sym *sym;
8008 struct bfd_link_hash_entry *global_entry;
8009 const char *candidate = NULL;
8010 Elf_Internal_Shdr *symtab_hdr;
8011 size_t i;
8012
d9352518
DB
8013 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8014
8015 for (i = 0; i < locsymcount; ++ i)
8016 {
8977835c 8017 sym = isymbuf + i;
d9352518
DB
8018
8019 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8020 continue;
8021
8022 candidate = bfd_elf_string_from_elf_section (input_bfd,
8023 symtab_hdr->sh_link,
8024 sym->st_name);
8025#ifdef DEBUG
0f02bbd9
AM
8026 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8027 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8028#endif
8029 if (candidate && strcmp (candidate, name) == 0)
8030 {
8b127cbc 8031 asection *sec = flinfo->sections [i];
d9352518 8032
0f02bbd9
AM
8033 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8034 *result += sec->output_offset + sec->output_section->vma;
d9352518 8035#ifdef DEBUG
0f02bbd9
AM
8036 printf ("Found symbol with value %8.8lx\n",
8037 (unsigned long) *result);
d9352518
DB
8038#endif
8039 return TRUE;
8040 }
8041 }
8042
8043 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8044 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8045 FALSE, FALSE, TRUE);
d9352518
DB
8046 if (!global_entry)
8047 return FALSE;
a0c8462f 8048
d9352518
DB
8049 if (global_entry->type == bfd_link_hash_defined
8050 || global_entry->type == bfd_link_hash_defweak)
8051 {
a0c8462f
AM
8052 *result = (global_entry->u.def.value
8053 + global_entry->u.def.section->output_section->vma
8054 + global_entry->u.def.section->output_offset);
d9352518 8055#ifdef DEBUG
0f02bbd9
AM
8056 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8057 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8058#endif
8059 return TRUE;
a0c8462f 8060 }
d9352518 8061
d9352518
DB
8062 return FALSE;
8063}
8064
37b01f6a
DG
8065/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8066 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8067 names like "foo.end" which is the end address of section "foo". */
8068
d9352518 8069static bfd_boolean
a0c8462f
AM
8070resolve_section (const char *name,
8071 asection *sections,
37b01f6a
DG
8072 bfd_vma *result,
8073 bfd * abfd)
d9352518 8074{
a0c8462f
AM
8075 asection *curr;
8076 unsigned int len;
d9352518 8077
a0c8462f 8078 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8079 if (strcmp (curr->name, name) == 0)
8080 {
8081 *result = curr->vma;
8082 return TRUE;
8083 }
8084
8085 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8086 /* FIXME: This could be coded more efficiently... */
a0c8462f 8087 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8088 {
8089 len = strlen (curr->name);
a0c8462f 8090 if (len > strlen (name))
d9352518
DB
8091 continue;
8092
8093 if (strncmp (curr->name, name, len) == 0)
8094 {
8095 if (strncmp (".end", name + len, 4) == 0)
8096 {
37b01f6a 8097 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8098 return TRUE;
8099 }
8100
8101 /* Insert more pseudo-section names here, if you like. */
8102 }
8103 }
a0c8462f 8104
d9352518
DB
8105 return FALSE;
8106}
8107
8108static void
a0c8462f 8109undefined_reference (const char *reftype, const char *name)
d9352518 8110{
695344c0 8111 /* xgettext:c-format */
a0c8462f
AM
8112 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8113 reftype, name);
d9352518
DB
8114}
8115
8116static bfd_boolean
a0c8462f
AM
8117eval_symbol (bfd_vma *result,
8118 const char **symp,
8119 bfd *input_bfd,
8b127cbc 8120 struct elf_final_link_info *flinfo,
a0c8462f
AM
8121 bfd_vma dot,
8122 Elf_Internal_Sym *isymbuf,
8123 size_t locsymcount,
8124 int signed_p)
d9352518 8125{
4b93929b
NC
8126 size_t len;
8127 size_t symlen;
a0c8462f
AM
8128 bfd_vma a;
8129 bfd_vma b;
4b93929b 8130 char symbuf[4096];
0f02bbd9 8131 const char *sym = *symp;
a0c8462f
AM
8132 const char *symend;
8133 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8134
8135 len = strlen (sym);
8136 symend = sym + len;
8137
4b93929b 8138 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8139 {
8140 bfd_set_error (bfd_error_invalid_operation);
8141 return FALSE;
8142 }
a0c8462f 8143
d9352518
DB
8144 switch (* sym)
8145 {
8146 case '.':
0f02bbd9
AM
8147 *result = dot;
8148 *symp = sym + 1;
d9352518
DB
8149 return TRUE;
8150
8151 case '#':
0f02bbd9
AM
8152 ++sym;
8153 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8154 return TRUE;
8155
8156 case 'S':
8157 symbol_is_section = TRUE;
1a0670f3 8158 /* Fall through. */
a0c8462f 8159 case 's':
0f02bbd9
AM
8160 ++sym;
8161 symlen = strtol (sym, (char **) symp, 10);
8162 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8163
4b93929b 8164 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8165 {
8166 bfd_set_error (bfd_error_invalid_operation);
8167 return FALSE;
8168 }
8169
8170 memcpy (symbuf, sym, symlen);
a0c8462f 8171 symbuf[symlen] = '\0';
0f02bbd9 8172 *symp = sym + symlen;
a0c8462f
AM
8173
8174 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8175 the symbol as a section, or vice-versa. so we're pretty liberal in our
8176 interpretation here; section means "try section first", not "must be a
8177 section", and likewise with symbol. */
8178
a0c8462f 8179 if (symbol_is_section)
d9352518 8180 {
37b01f6a 8181 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8182 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8183 isymbuf, locsymcount))
d9352518
DB
8184 {
8185 undefined_reference ("section", symbuf);
8186 return FALSE;
8187 }
a0c8462f
AM
8188 }
8189 else
d9352518 8190 {
8b127cbc 8191 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8192 isymbuf, locsymcount)
8b127cbc 8193 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8194 result, input_bfd))
d9352518
DB
8195 {
8196 undefined_reference ("symbol", symbuf);
8197 return FALSE;
8198 }
8199 }
8200
8201 return TRUE;
a0c8462f 8202
d9352518
DB
8203 /* All that remains are operators. */
8204
8205#define UNARY_OP(op) \
8206 if (strncmp (sym, #op, strlen (#op)) == 0) \
8207 { \
8208 sym += strlen (#op); \
a0c8462f
AM
8209 if (*sym == ':') \
8210 ++sym; \
0f02bbd9 8211 *symp = sym; \
8b127cbc 8212 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8213 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8214 return FALSE; \
8215 if (signed_p) \
0f02bbd9 8216 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8217 else \
8218 *result = op a; \
d9352518
DB
8219 return TRUE; \
8220 }
8221
8222#define BINARY_OP(op) \
8223 if (strncmp (sym, #op, strlen (#op)) == 0) \
8224 { \
8225 sym += strlen (#op); \
a0c8462f
AM
8226 if (*sym == ':') \
8227 ++sym; \
0f02bbd9 8228 *symp = sym; \
8b127cbc 8229 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8230 isymbuf, locsymcount, signed_p)) \
a0c8462f 8231 return FALSE; \
0f02bbd9 8232 ++*symp; \
8b127cbc 8233 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8234 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8235 return FALSE; \
8236 if (signed_p) \
0f02bbd9 8237 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8238 else \
8239 *result = a op b; \
d9352518
DB
8240 return TRUE; \
8241 }
8242
8243 default:
8244 UNARY_OP (0-);
8245 BINARY_OP (<<);
8246 BINARY_OP (>>);
8247 BINARY_OP (==);
8248 BINARY_OP (!=);
8249 BINARY_OP (<=);
8250 BINARY_OP (>=);
8251 BINARY_OP (&&);
8252 BINARY_OP (||);
8253 UNARY_OP (~);
8254 UNARY_OP (!);
8255 BINARY_OP (*);
8256 BINARY_OP (/);
8257 BINARY_OP (%);
8258 BINARY_OP (^);
8259 BINARY_OP (|);
8260 BINARY_OP (&);
8261 BINARY_OP (+);
8262 BINARY_OP (-);
8263 BINARY_OP (<);
8264 BINARY_OP (>);
8265#undef UNARY_OP
8266#undef BINARY_OP
8267 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8268 bfd_set_error (bfd_error_invalid_operation);
8269 return FALSE;
8270 }
8271}
8272
d9352518 8273static void
a0c8462f
AM
8274put_value (bfd_vma size,
8275 unsigned long chunksz,
8276 bfd *input_bfd,
8277 bfd_vma x,
8278 bfd_byte *location)
d9352518
DB
8279{
8280 location += (size - chunksz);
8281
41cd1ad1 8282 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8283 {
8284 switch (chunksz)
8285 {
d9352518
DB
8286 case 1:
8287 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8288 x >>= 8;
d9352518
DB
8289 break;
8290 case 2:
8291 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8292 x >>= 16;
d9352518
DB
8293 break;
8294 case 4:
8295 bfd_put_32 (input_bfd, x, location);
65164438
NC
8296 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8297 x >>= 16;
8298 x >>= 16;
d9352518 8299 break;
d9352518 8300#ifdef BFD64
41cd1ad1 8301 case 8:
d9352518 8302 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8303 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8304 x >>= 32;
8305 x >>= 32;
8306 break;
d9352518 8307#endif
41cd1ad1
NC
8308 default:
8309 abort ();
d9352518
DB
8310 break;
8311 }
8312 }
8313}
8314
a0c8462f
AM
8315static bfd_vma
8316get_value (bfd_vma size,
8317 unsigned long chunksz,
8318 bfd *input_bfd,
8319 bfd_byte *location)
d9352518 8320{
9b239e0e 8321 int shift;
d9352518
DB
8322 bfd_vma x = 0;
8323
9b239e0e
NC
8324 /* Sanity checks. */
8325 BFD_ASSERT (chunksz <= sizeof (x)
8326 && size >= chunksz
8327 && chunksz != 0
8328 && (size % chunksz) == 0
8329 && input_bfd != NULL
8330 && location != NULL);
8331
8332 if (chunksz == sizeof (x))
8333 {
8334 BFD_ASSERT (size == chunksz);
8335
8336 /* Make sure that we do not perform an undefined shift operation.
8337 We know that size == chunksz so there will only be one iteration
8338 of the loop below. */
8339 shift = 0;
8340 }
8341 else
8342 shift = 8 * chunksz;
8343
a0c8462f 8344 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8345 {
8346 switch (chunksz)
8347 {
d9352518 8348 case 1:
9b239e0e 8349 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8350 break;
8351 case 2:
9b239e0e 8352 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8353 break;
8354 case 4:
9b239e0e 8355 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8356 break;
d9352518 8357#ifdef BFD64
9b239e0e
NC
8358 case 8:
8359 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8360 break;
9b239e0e
NC
8361#endif
8362 default:
8363 abort ();
d9352518
DB
8364 }
8365 }
8366 return x;
8367}
8368
a0c8462f
AM
8369static void
8370decode_complex_addend (unsigned long *start, /* in bits */
8371 unsigned long *oplen, /* in bits */
8372 unsigned long *len, /* in bits */
8373 unsigned long *wordsz, /* in bytes */
8374 unsigned long *chunksz, /* in bytes */
8375 unsigned long *lsb0_p,
8376 unsigned long *signed_p,
8377 unsigned long *trunc_p,
8378 unsigned long encoded)
d9352518
DB
8379{
8380 * start = encoded & 0x3F;
8381 * len = (encoded >> 6) & 0x3F;
8382 * oplen = (encoded >> 12) & 0x3F;
8383 * wordsz = (encoded >> 18) & 0xF;
8384 * chunksz = (encoded >> 22) & 0xF;
8385 * lsb0_p = (encoded >> 27) & 1;
8386 * signed_p = (encoded >> 28) & 1;
8387 * trunc_p = (encoded >> 29) & 1;
8388}
8389
cdfeee4f 8390bfd_reloc_status_type
0f02bbd9 8391bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8392 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8393 bfd_byte *contents,
8394 Elf_Internal_Rela *rel,
8395 bfd_vma relocation)
d9352518 8396{
0f02bbd9
AM
8397 bfd_vma shift, x, mask;
8398 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8399 bfd_reloc_status_type r;
d9352518
DB
8400
8401 /* Perform this reloc, since it is complex.
8402 (this is not to say that it necessarily refers to a complex
8403 symbol; merely that it is a self-describing CGEN based reloc.
8404 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8405 word size, etc) encoded within it.). */
d9352518 8406
a0c8462f
AM
8407 decode_complex_addend (&start, &oplen, &len, &wordsz,
8408 &chunksz, &lsb0_p, &signed_p,
8409 &trunc_p, rel->r_addend);
d9352518
DB
8410
8411 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8412
8413 if (lsb0_p)
8414 shift = (start + 1) - len;
8415 else
8416 shift = (8 * wordsz) - (start + len);
8417
37b01f6a
DG
8418 x = get_value (wordsz, chunksz, input_bfd,
8419 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8420
8421#ifdef DEBUG
8422 printf ("Doing complex reloc: "
8423 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8424 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8425 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8426 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8427 oplen, (unsigned long) x, (unsigned long) mask,
8428 (unsigned long) relocation);
d9352518
DB
8429#endif
8430
cdfeee4f 8431 r = bfd_reloc_ok;
d9352518 8432 if (! trunc_p)
cdfeee4f
AM
8433 /* Now do an overflow check. */
8434 r = bfd_check_overflow ((signed_p
8435 ? complain_overflow_signed
8436 : complain_overflow_unsigned),
8437 len, 0, (8 * wordsz),
8438 relocation);
a0c8462f 8439
d9352518
DB
8440 /* Do the deed. */
8441 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8442
8443#ifdef DEBUG
8444 printf (" relocation: %8.8lx\n"
8445 " shifted mask: %8.8lx\n"
8446 " shifted/masked reloc: %8.8lx\n"
8447 " result: %8.8lx\n",
9ccb8af9
AM
8448 (unsigned long) relocation, (unsigned long) (mask << shift),
8449 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8450#endif
37b01f6a
DG
8451 put_value (wordsz, chunksz, input_bfd, x,
8452 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8453 return r;
d9352518
DB
8454}
8455
0e287786
AM
8456/* Functions to read r_offset from external (target order) reloc
8457 entry. Faster than bfd_getl32 et al, because we let the compiler
8458 know the value is aligned. */
53df40a4 8459
0e287786
AM
8460static bfd_vma
8461ext32l_r_offset (const void *p)
53df40a4
AM
8462{
8463 union aligned32
8464 {
8465 uint32_t v;
8466 unsigned char c[4];
8467 };
8468 const union aligned32 *a
0e287786 8469 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8470
8471 uint32_t aval = ( (uint32_t) a->c[0]
8472 | (uint32_t) a->c[1] << 8
8473 | (uint32_t) a->c[2] << 16
8474 | (uint32_t) a->c[3] << 24);
0e287786 8475 return aval;
53df40a4
AM
8476}
8477
0e287786
AM
8478static bfd_vma
8479ext32b_r_offset (const void *p)
53df40a4
AM
8480{
8481 union aligned32
8482 {
8483 uint32_t v;
8484 unsigned char c[4];
8485 };
8486 const union aligned32 *a
0e287786 8487 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8488
8489 uint32_t aval = ( (uint32_t) a->c[0] << 24
8490 | (uint32_t) a->c[1] << 16
8491 | (uint32_t) a->c[2] << 8
8492 | (uint32_t) a->c[3]);
0e287786 8493 return aval;
53df40a4
AM
8494}
8495
8496#ifdef BFD_HOST_64_BIT
0e287786
AM
8497static bfd_vma
8498ext64l_r_offset (const void *p)
53df40a4
AM
8499{
8500 union aligned64
8501 {
8502 uint64_t v;
8503 unsigned char c[8];
8504 };
8505 const union aligned64 *a
0e287786 8506 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8507
8508 uint64_t aval = ( (uint64_t) a->c[0]
8509 | (uint64_t) a->c[1] << 8
8510 | (uint64_t) a->c[2] << 16
8511 | (uint64_t) a->c[3] << 24
8512 | (uint64_t) a->c[4] << 32
8513 | (uint64_t) a->c[5] << 40
8514 | (uint64_t) a->c[6] << 48
8515 | (uint64_t) a->c[7] << 56);
0e287786 8516 return aval;
53df40a4
AM
8517}
8518
0e287786
AM
8519static bfd_vma
8520ext64b_r_offset (const void *p)
53df40a4
AM
8521{
8522 union aligned64
8523 {
8524 uint64_t v;
8525 unsigned char c[8];
8526 };
8527 const union aligned64 *a
0e287786 8528 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8529
8530 uint64_t aval = ( (uint64_t) a->c[0] << 56
8531 | (uint64_t) a->c[1] << 48
8532 | (uint64_t) a->c[2] << 40
8533 | (uint64_t) a->c[3] << 32
8534 | (uint64_t) a->c[4] << 24
8535 | (uint64_t) a->c[5] << 16
8536 | (uint64_t) a->c[6] << 8
8537 | (uint64_t) a->c[7]);
0e287786 8538 return aval;
53df40a4
AM
8539}
8540#endif
8541
c152c796
AM
8542/* When performing a relocatable link, the input relocations are
8543 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8544 referenced must be updated. Update all the relocations found in
8545 RELDATA. */
c152c796 8546
bca6d0e3 8547static bfd_boolean
c152c796 8548elf_link_adjust_relocs (bfd *abfd,
9eaff861 8549 asection *sec,
28dbcedc
AM
8550 struct bfd_elf_section_reloc_data *reldata,
8551 bfd_boolean sort)
c152c796
AM
8552{
8553 unsigned int i;
8554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8555 bfd_byte *erela;
8556 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8557 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8558 bfd_vma r_type_mask;
8559 int r_sym_shift;
d4730f92
BS
8560 unsigned int count = reldata->count;
8561 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8562
d4730f92 8563 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8564 {
8565 swap_in = bed->s->swap_reloc_in;
8566 swap_out = bed->s->swap_reloc_out;
8567 }
d4730f92 8568 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8569 {
8570 swap_in = bed->s->swap_reloca_in;
8571 swap_out = bed->s->swap_reloca_out;
8572 }
8573 else
8574 abort ();
8575
8576 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8577 abort ();
8578
8579 if (bed->s->arch_size == 32)
8580 {
8581 r_type_mask = 0xff;
8582 r_sym_shift = 8;
8583 }
8584 else
8585 {
8586 r_type_mask = 0xffffffff;
8587 r_sym_shift = 32;
8588 }
8589
d4730f92
BS
8590 erela = reldata->hdr->contents;
8591 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8592 {
8593 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8594 unsigned int j;
8595
8596 if (*rel_hash == NULL)
8597 continue;
8598
8599 BFD_ASSERT ((*rel_hash)->indx >= 0);
8600
8601 (*swap_in) (abfd, erela, irela);
8602 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8603 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8604 | (irela[j].r_info & r_type_mask));
8605 (*swap_out) (abfd, irela, erela);
8606 }
53df40a4 8607
9eaff861
AO
8608 if (bed->elf_backend_update_relocs)
8609 (*bed->elf_backend_update_relocs) (sec, reldata);
8610
0e287786 8611 if (sort && count != 0)
53df40a4 8612 {
0e287786
AM
8613 bfd_vma (*ext_r_off) (const void *);
8614 bfd_vma r_off;
8615 size_t elt_size;
8616 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8617 bfd_byte *buf = NULL;
28dbcedc
AM
8618
8619 if (bed->s->arch_size == 32)
8620 {
8621 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8622 ext_r_off = ext32l_r_offset;
28dbcedc 8623 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8624 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8625 else
8626 abort ();
8627 }
53df40a4 8628 else
28dbcedc 8629 {
53df40a4 8630#ifdef BFD_HOST_64_BIT
28dbcedc 8631 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8632 ext_r_off = ext64l_r_offset;
28dbcedc 8633 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8634 ext_r_off = ext64b_r_offset;
28dbcedc 8635 else
53df40a4 8636#endif
28dbcedc
AM
8637 abort ();
8638 }
0e287786 8639
bca6d0e3
AM
8640 /* Must use a stable sort here. A modified insertion sort,
8641 since the relocs are mostly sorted already. */
0e287786
AM
8642 elt_size = reldata->hdr->sh_entsize;
8643 base = reldata->hdr->contents;
8644 end = base + count * elt_size;
bca6d0e3 8645 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8646 abort ();
8647
8648 /* Ensure the first element is lowest. This acts as a sentinel,
8649 speeding the main loop below. */
8650 r_off = (*ext_r_off) (base);
8651 for (p = loc = base; (p += elt_size) < end; )
8652 {
8653 bfd_vma r_off2 = (*ext_r_off) (p);
8654 if (r_off > r_off2)
8655 {
8656 r_off = r_off2;
8657 loc = p;
8658 }
8659 }
8660 if (loc != base)
8661 {
8662 /* Don't just swap *base and *loc as that changes the order
8663 of the original base[0] and base[1] if they happen to
8664 have the same r_offset. */
bca6d0e3
AM
8665 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8666 memcpy (onebuf, loc, elt_size);
0e287786 8667 memmove (base + elt_size, base, loc - base);
bca6d0e3 8668 memcpy (base, onebuf, elt_size);
0e287786
AM
8669 }
8670
b29b8669 8671 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8672 {
8673 /* base to p is sorted, *p is next to insert. */
8674 r_off = (*ext_r_off) (p);
8675 /* Search the sorted region for location to insert. */
8676 loc = p - elt_size;
8677 while (r_off < (*ext_r_off) (loc))
8678 loc -= elt_size;
8679 loc += elt_size;
8680 if (loc != p)
8681 {
bca6d0e3
AM
8682 /* Chances are there is a run of relocs to insert here,
8683 from one of more input files. Files are not always
8684 linked in order due to the way elf_link_input_bfd is
8685 called. See pr17666. */
8686 size_t sortlen = p - loc;
8687 bfd_vma r_off2 = (*ext_r_off) (loc);
8688 size_t runlen = elt_size;
8689 size_t buf_size = 96 * 1024;
8690 while (p + runlen < end
8691 && (sortlen <= buf_size
8692 || runlen + elt_size <= buf_size)
8693 && r_off2 > (*ext_r_off) (p + runlen))
8694 runlen += elt_size;
8695 if (buf == NULL)
8696 {
8697 buf = bfd_malloc (buf_size);
8698 if (buf == NULL)
8699 return FALSE;
8700 }
8701 if (runlen < sortlen)
8702 {
8703 memcpy (buf, p, runlen);
8704 memmove (loc + runlen, loc, sortlen);
8705 memcpy (loc, buf, runlen);
8706 }
8707 else
8708 {
8709 memcpy (buf, loc, sortlen);
8710 memmove (loc, p, runlen);
8711 memcpy (loc + runlen, buf, sortlen);
8712 }
b29b8669 8713 p += runlen - elt_size;
0e287786
AM
8714 }
8715 }
8716 /* Hashes are no longer valid. */
28dbcedc
AM
8717 free (reldata->hashes);
8718 reldata->hashes = NULL;
bca6d0e3 8719 free (buf);
53df40a4 8720 }
bca6d0e3 8721 return TRUE;
c152c796
AM
8722}
8723
8724struct elf_link_sort_rela
8725{
8726 union {
8727 bfd_vma offset;
8728 bfd_vma sym_mask;
8729 } u;
8730 enum elf_reloc_type_class type;
8731 /* We use this as an array of size int_rels_per_ext_rel. */
8732 Elf_Internal_Rela rela[1];
8733};
8734
8735static int
8736elf_link_sort_cmp1 (const void *A, const void *B)
8737{
a50b1753
NC
8738 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8739 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8740 int relativea, relativeb;
8741
8742 relativea = a->type == reloc_class_relative;
8743 relativeb = b->type == reloc_class_relative;
8744
8745 if (relativea < relativeb)
8746 return 1;
8747 if (relativea > relativeb)
8748 return -1;
8749 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8750 return -1;
8751 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8752 return 1;
8753 if (a->rela->r_offset < b->rela->r_offset)
8754 return -1;
8755 if (a->rela->r_offset > b->rela->r_offset)
8756 return 1;
8757 return 0;
8758}
8759
8760static int
8761elf_link_sort_cmp2 (const void *A, const void *B)
8762{
a50b1753
NC
8763 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8764 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8765
7e612e98 8766 if (a->type < b->type)
c152c796 8767 return -1;
7e612e98 8768 if (a->type > b->type)
c152c796 8769 return 1;
7e612e98 8770 if (a->u.offset < b->u.offset)
c152c796 8771 return -1;
7e612e98 8772 if (a->u.offset > b->u.offset)
c152c796
AM
8773 return 1;
8774 if (a->rela->r_offset < b->rela->r_offset)
8775 return -1;
8776 if (a->rela->r_offset > b->rela->r_offset)
8777 return 1;
8778 return 0;
8779}
8780
8781static size_t
8782elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8783{
3410fea8 8784 asection *dynamic_relocs;
fc66a176
L
8785 asection *rela_dyn;
8786 asection *rel_dyn;
c152c796
AM
8787 bfd_size_type count, size;
8788 size_t i, ret, sort_elt, ext_size;
8789 bfd_byte *sort, *s_non_relative, *p;
8790 struct elf_link_sort_rela *sq;
8791 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8792 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8793 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8794 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8795 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8796 struct bfd_link_order *lo;
8797 bfd_vma r_sym_mask;
3410fea8 8798 bfd_boolean use_rela;
c152c796 8799
3410fea8
NC
8800 /* Find a dynamic reloc section. */
8801 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8802 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8803 if (rela_dyn != NULL && rela_dyn->size > 0
8804 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8805 {
3410fea8
NC
8806 bfd_boolean use_rela_initialised = FALSE;
8807
8808 /* This is just here to stop gcc from complaining.
c8e44c6d 8809 Its initialization checking code is not perfect. */
3410fea8
NC
8810 use_rela = TRUE;
8811
8812 /* Both sections are present. Examine the sizes
8813 of the indirect sections to help us choose. */
8814 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8815 if (lo->type == bfd_indirect_link_order)
8816 {
8817 asection *o = lo->u.indirect.section;
8818
8819 if ((o->size % bed->s->sizeof_rela) == 0)
8820 {
8821 if ((o->size % bed->s->sizeof_rel) == 0)
8822 /* Section size is divisible by both rel and rela sizes.
8823 It is of no help to us. */
8824 ;
8825 else
8826 {
8827 /* Section size is only divisible by rela. */
8828 if (use_rela_initialised && (use_rela == FALSE))
8829 {
c8e44c6d
AM
8830 _bfd_error_handler (_("%B: Unable to sort relocs - "
8831 "they are in more than one size"),
8832 abfd);
3410fea8
NC
8833 bfd_set_error (bfd_error_invalid_operation);
8834 return 0;
8835 }
8836 else
8837 {
8838 use_rela = TRUE;
8839 use_rela_initialised = TRUE;
8840 }
8841 }
8842 }
8843 else if ((o->size % bed->s->sizeof_rel) == 0)
8844 {
8845 /* Section size is only divisible by rel. */
8846 if (use_rela_initialised && (use_rela == TRUE))
8847 {
c8e44c6d
AM
8848 _bfd_error_handler (_("%B: Unable to sort relocs - "
8849 "they are in more than one size"),
8850 abfd);
3410fea8
NC
8851 bfd_set_error (bfd_error_invalid_operation);
8852 return 0;
8853 }
8854 else
8855 {
8856 use_rela = FALSE;
8857 use_rela_initialised = TRUE;
8858 }
8859 }
8860 else
8861 {
c8e44c6d
AM
8862 /* The section size is not divisible by either -
8863 something is wrong. */
8864 _bfd_error_handler (_("%B: Unable to sort relocs - "
8865 "they are of an unknown size"), abfd);
3410fea8
NC
8866 bfd_set_error (bfd_error_invalid_operation);
8867 return 0;
8868 }
8869 }
8870
8871 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8872 if (lo->type == bfd_indirect_link_order)
8873 {
8874 asection *o = lo->u.indirect.section;
8875
8876 if ((o->size % bed->s->sizeof_rela) == 0)
8877 {
8878 if ((o->size % bed->s->sizeof_rel) == 0)
8879 /* Section size is divisible by both rel and rela sizes.
8880 It is of no help to us. */
8881 ;
8882 else
8883 {
8884 /* Section size is only divisible by rela. */
8885 if (use_rela_initialised && (use_rela == FALSE))
8886 {
c8e44c6d
AM
8887 _bfd_error_handler (_("%B: Unable to sort relocs - "
8888 "they are in more than one size"),
8889 abfd);
3410fea8
NC
8890 bfd_set_error (bfd_error_invalid_operation);
8891 return 0;
8892 }
8893 else
8894 {
8895 use_rela = TRUE;
8896 use_rela_initialised = TRUE;
8897 }
8898 }
8899 }
8900 else if ((o->size % bed->s->sizeof_rel) == 0)
8901 {
8902 /* Section size is only divisible by rel. */
8903 if (use_rela_initialised && (use_rela == TRUE))
8904 {
c8e44c6d
AM
8905 _bfd_error_handler (_("%B: Unable to sort relocs - "
8906 "they are in more than one size"),
8907 abfd);
3410fea8
NC
8908 bfd_set_error (bfd_error_invalid_operation);
8909 return 0;
8910 }
8911 else
8912 {
8913 use_rela = FALSE;
8914 use_rela_initialised = TRUE;
8915 }
8916 }
8917 else
8918 {
c8e44c6d
AM
8919 /* The section size is not divisible by either -
8920 something is wrong. */
8921 _bfd_error_handler (_("%B: Unable to sort relocs - "
8922 "they are of an unknown size"), abfd);
3410fea8
NC
8923 bfd_set_error (bfd_error_invalid_operation);
8924 return 0;
8925 }
8926 }
8927
8928 if (! use_rela_initialised)
8929 /* Make a guess. */
8930 use_rela = TRUE;
c152c796 8931 }
fc66a176
L
8932 else if (rela_dyn != NULL && rela_dyn->size > 0)
8933 use_rela = TRUE;
8934 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8935 use_rela = FALSE;
c152c796 8936 else
fc66a176 8937 return 0;
3410fea8
NC
8938
8939 if (use_rela)
c152c796 8940 {
3410fea8 8941 dynamic_relocs = rela_dyn;
c152c796
AM
8942 ext_size = bed->s->sizeof_rela;
8943 swap_in = bed->s->swap_reloca_in;
8944 swap_out = bed->s->swap_reloca_out;
8945 }
3410fea8
NC
8946 else
8947 {
8948 dynamic_relocs = rel_dyn;
8949 ext_size = bed->s->sizeof_rel;
8950 swap_in = bed->s->swap_reloc_in;
8951 swap_out = bed->s->swap_reloc_out;
8952 }
c152c796
AM
8953
8954 size = 0;
3410fea8 8955 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8956 if (lo->type == bfd_indirect_link_order)
3410fea8 8957 size += lo->u.indirect.section->size;
c152c796 8958
3410fea8 8959 if (size != dynamic_relocs->size)
c152c796
AM
8960 return 0;
8961
8962 sort_elt = (sizeof (struct elf_link_sort_rela)
8963 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8964
8965 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8966 if (count == 0)
8967 return 0;
a50b1753 8968 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8969
c152c796
AM
8970 if (sort == NULL)
8971 {
8972 (*info->callbacks->warning)
8973 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8974 return 0;
8975 }
8976
8977 if (bed->s->arch_size == 32)
8978 r_sym_mask = ~(bfd_vma) 0xff;
8979 else
8980 r_sym_mask = ~(bfd_vma) 0xffffffff;
8981
3410fea8 8982 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8983 if (lo->type == bfd_indirect_link_order)
8984 {
8985 bfd_byte *erel, *erelend;
8986 asection *o = lo->u.indirect.section;
8987
1da212d6
AM
8988 if (o->contents == NULL && o->size != 0)
8989 {
8990 /* This is a reloc section that is being handled as a normal
8991 section. See bfd_section_from_shdr. We can't combine
8992 relocs in this case. */
8993 free (sort);
8994 return 0;
8995 }
c152c796 8996 erel = o->contents;
eea6121a 8997 erelend = o->contents + o->size;
c8e44c6d 8998 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8999
c152c796
AM
9000 while (erel < erelend)
9001 {
9002 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9003
c152c796 9004 (*swap_in) (abfd, erel, s->rela);
7e612e98 9005 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9006 s->u.sym_mask = r_sym_mask;
9007 p += sort_elt;
9008 erel += ext_size;
9009 }
9010 }
9011
9012 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9013
9014 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9015 {
9016 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9017 if (s->type != reloc_class_relative)
9018 break;
9019 }
9020 ret = i;
9021 s_non_relative = p;
9022
9023 sq = (struct elf_link_sort_rela *) s_non_relative;
9024 for (; i < count; i++, p += sort_elt)
9025 {
9026 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9027 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9028 sq = sp;
9029 sp->u.offset = sq->rela->r_offset;
9030 }
9031
9032 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9033
c8e44c6d
AM
9034 struct elf_link_hash_table *htab = elf_hash_table (info);
9035 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9036 {
9037 /* We have plt relocs in .rela.dyn. */
9038 sq = (struct elf_link_sort_rela *) sort;
9039 for (i = 0; i < count; i++)
9040 if (sq[count - i - 1].type != reloc_class_plt)
9041 break;
9042 if (i != 0 && htab->srelplt->size == i * ext_size)
9043 {
9044 struct bfd_link_order **plo;
9045 /* Put srelplt link_order last. This is so the output_offset
9046 set in the next loop is correct for DT_JMPREL. */
9047 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9048 if ((*plo)->type == bfd_indirect_link_order
9049 && (*plo)->u.indirect.section == htab->srelplt)
9050 {
9051 lo = *plo;
9052 *plo = lo->next;
9053 }
9054 else
9055 plo = &(*plo)->next;
9056 *plo = lo;
9057 lo->next = NULL;
9058 dynamic_relocs->map_tail.link_order = lo;
9059 }
9060 }
9061
9062 p = sort;
3410fea8 9063 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9064 if (lo->type == bfd_indirect_link_order)
9065 {
9066 bfd_byte *erel, *erelend;
9067 asection *o = lo->u.indirect.section;
9068
9069 erel = o->contents;
eea6121a 9070 erelend = o->contents + o->size;
c8e44c6d 9071 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9072 while (erel < erelend)
9073 {
9074 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9075 (*swap_out) (abfd, s->rela, erel);
9076 p += sort_elt;
9077 erel += ext_size;
9078 }
9079 }
9080
9081 free (sort);
3410fea8 9082 *psec = dynamic_relocs;
c152c796
AM
9083 return ret;
9084}
9085
ef10c3ac 9086/* Add a symbol to the output symbol string table. */
c152c796 9087
6e0b88f1 9088static int
ef10c3ac
L
9089elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9090 const char *name,
9091 Elf_Internal_Sym *elfsym,
9092 asection *input_sec,
9093 struct elf_link_hash_entry *h)
c152c796 9094{
6e0b88f1 9095 int (*output_symbol_hook)
c152c796
AM
9096 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9097 struct elf_link_hash_entry *);
ef10c3ac 9098 struct elf_link_hash_table *hash_table;
c152c796 9099 const struct elf_backend_data *bed;
ef10c3ac 9100 bfd_size_type strtabsize;
c152c796 9101
8539e4e8
AM
9102 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9103
8b127cbc 9104 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9105 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9106 if (output_symbol_hook != NULL)
9107 {
8b127cbc 9108 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9109 if (ret != 1)
9110 return ret;
c152c796
AM
9111 }
9112
ef10c3ac
L
9113 if (name == NULL
9114 || *name == '\0'
9115 || (input_sec->flags & SEC_EXCLUDE))
9116 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9117 else
9118 {
ef10c3ac
L
9119 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9120 to get the final offset for st_name. */
9121 elfsym->st_name
9122 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9123 name, FALSE);
c152c796 9124 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9125 return 0;
c152c796
AM
9126 }
9127
ef10c3ac
L
9128 hash_table = elf_hash_table (flinfo->info);
9129 strtabsize = hash_table->strtabsize;
9130 if (strtabsize <= hash_table->strtabcount)
c152c796 9131 {
ef10c3ac
L
9132 strtabsize += strtabsize;
9133 hash_table->strtabsize = strtabsize;
9134 strtabsize *= sizeof (*hash_table->strtab);
9135 hash_table->strtab
9136 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9137 strtabsize);
9138 if (hash_table->strtab == NULL)
6e0b88f1 9139 return 0;
c152c796 9140 }
ef10c3ac
L
9141 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9142 hash_table->strtab[hash_table->strtabcount].dest_index
9143 = hash_table->strtabcount;
9144 hash_table->strtab[hash_table->strtabcount].destshndx_index
9145 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9146
9147 bfd_get_symcount (flinfo->output_bfd) += 1;
9148 hash_table->strtabcount += 1;
9149
9150 return 1;
9151}
9152
9153/* Swap symbols out to the symbol table and flush the output symbols to
9154 the file. */
9155
9156static bfd_boolean
9157elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9158{
9159 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9160 bfd_size_type amt;
9161 size_t i;
ef10c3ac
L
9162 const struct elf_backend_data *bed;
9163 bfd_byte *symbuf;
9164 Elf_Internal_Shdr *hdr;
9165 file_ptr pos;
9166 bfd_boolean ret;
9167
9168 if (!hash_table->strtabcount)
9169 return TRUE;
9170
9171 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9172
9173 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9174
ef10c3ac
L
9175 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9176 symbuf = (bfd_byte *) bfd_malloc (amt);
9177 if (symbuf == NULL)
9178 return FALSE;
1b786873 9179
ef10c3ac 9180 if (flinfo->symshndxbuf)
c152c796 9181 {
ef53be89
AM
9182 amt = sizeof (Elf_External_Sym_Shndx);
9183 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9184 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9185 if (flinfo->symshndxbuf == NULL)
c152c796 9186 {
ef10c3ac
L
9187 free (symbuf);
9188 return FALSE;
c152c796 9189 }
c152c796
AM
9190 }
9191
ef10c3ac
L
9192 for (i = 0; i < hash_table->strtabcount; i++)
9193 {
9194 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9195 if (elfsym->sym.st_name == (unsigned long) -1)
9196 elfsym->sym.st_name = 0;
9197 else
9198 elfsym->sym.st_name
9199 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9200 elfsym->sym.st_name);
9201 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9202 ((bfd_byte *) symbuf
9203 + (elfsym->dest_index
9204 * bed->s->sizeof_sym)),
9205 (flinfo->symshndxbuf
9206 + elfsym->destshndx_index));
9207 }
9208
9209 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9210 pos = hdr->sh_offset + hdr->sh_size;
9211 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9212 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9213 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9214 {
9215 hdr->sh_size += amt;
9216 ret = TRUE;
9217 }
9218 else
9219 ret = FALSE;
c152c796 9220
ef10c3ac
L
9221 free (symbuf);
9222
9223 free (hash_table->strtab);
9224 hash_table->strtab = NULL;
9225
9226 return ret;
c152c796
AM
9227}
9228
c0d5a53d
L
9229/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9230
9231static bfd_boolean
9232check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9233{
4fbb74a6
AM
9234 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9235 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9236 {
9237 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9238 beyond 64k. */
4eca0228 9239 _bfd_error_handler
695344c0 9240 /* xgettext:c-format */
c0d5a53d 9241 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9242 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9243 bfd_set_error (bfd_error_nonrepresentable_section);
9244 return FALSE;
9245 }
9246 return TRUE;
9247}
9248
c152c796
AM
9249/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9250 allowing an unsatisfied unversioned symbol in the DSO to match a
9251 versioned symbol that would normally require an explicit version.
9252 We also handle the case that a DSO references a hidden symbol
9253 which may be satisfied by a versioned symbol in another DSO. */
9254
9255static bfd_boolean
9256elf_link_check_versioned_symbol (struct bfd_link_info *info,
9257 const struct elf_backend_data *bed,
9258 struct elf_link_hash_entry *h)
9259{
9260 bfd *abfd;
9261 struct elf_link_loaded_list *loaded;
9262
9263 if (!is_elf_hash_table (info->hash))
9264 return FALSE;
9265
90c984fc
L
9266 /* Check indirect symbol. */
9267 while (h->root.type == bfd_link_hash_indirect)
9268 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9269
c152c796
AM
9270 switch (h->root.type)
9271 {
9272 default:
9273 abfd = NULL;
9274 break;
9275
9276 case bfd_link_hash_undefined:
9277 case bfd_link_hash_undefweak:
9278 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9279 if (abfd == NULL
9280 || (abfd->flags & DYNAMIC) == 0
e56f61be 9281 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9282 return FALSE;
9283 break;
9284
9285 case bfd_link_hash_defined:
9286 case bfd_link_hash_defweak:
9287 abfd = h->root.u.def.section->owner;
9288 break;
9289
9290 case bfd_link_hash_common:
9291 abfd = h->root.u.c.p->section->owner;
9292 break;
9293 }
9294 BFD_ASSERT (abfd != NULL);
9295
9296 for (loaded = elf_hash_table (info)->loaded;
9297 loaded != NULL;
9298 loaded = loaded->next)
9299 {
9300 bfd *input;
9301 Elf_Internal_Shdr *hdr;
ef53be89
AM
9302 size_t symcount;
9303 size_t extsymcount;
9304 size_t extsymoff;
c152c796
AM
9305 Elf_Internal_Shdr *versymhdr;
9306 Elf_Internal_Sym *isym;
9307 Elf_Internal_Sym *isymend;
9308 Elf_Internal_Sym *isymbuf;
9309 Elf_External_Versym *ever;
9310 Elf_External_Versym *extversym;
9311
9312 input = loaded->abfd;
9313
9314 /* We check each DSO for a possible hidden versioned definition. */
9315 if (input == abfd
9316 || (input->flags & DYNAMIC) == 0
9317 || elf_dynversym (input) == 0)
9318 continue;
9319
9320 hdr = &elf_tdata (input)->dynsymtab_hdr;
9321
9322 symcount = hdr->sh_size / bed->s->sizeof_sym;
9323 if (elf_bad_symtab (input))
9324 {
9325 extsymcount = symcount;
9326 extsymoff = 0;
9327 }
9328 else
9329 {
9330 extsymcount = symcount - hdr->sh_info;
9331 extsymoff = hdr->sh_info;
9332 }
9333
9334 if (extsymcount == 0)
9335 continue;
9336
9337 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9338 NULL, NULL, NULL);
9339 if (isymbuf == NULL)
9340 return FALSE;
9341
9342 /* Read in any version definitions. */
9343 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9344 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9345 if (extversym == NULL)
9346 goto error_ret;
9347
9348 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9349 || (bfd_bread (extversym, versymhdr->sh_size, input)
9350 != versymhdr->sh_size))
9351 {
9352 free (extversym);
9353 error_ret:
9354 free (isymbuf);
9355 return FALSE;
9356 }
9357
9358 ever = extversym + extsymoff;
9359 isymend = isymbuf + extsymcount;
9360 for (isym = isymbuf; isym < isymend; isym++, ever++)
9361 {
9362 const char *name;
9363 Elf_Internal_Versym iver;
9364 unsigned short version_index;
9365
9366 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9367 || isym->st_shndx == SHN_UNDEF)
9368 continue;
9369
9370 name = bfd_elf_string_from_elf_section (input,
9371 hdr->sh_link,
9372 isym->st_name);
9373 if (strcmp (name, h->root.root.string) != 0)
9374 continue;
9375
9376 _bfd_elf_swap_versym_in (input, ever, &iver);
9377
d023c380
L
9378 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9379 && !(h->def_regular
9380 && h->forced_local))
c152c796
AM
9381 {
9382 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9383 have provided a definition for the undefined sym unless
9384 it is defined in a non-shared object and forced local.
9385 */
c152c796
AM
9386 abort ();
9387 }
9388
9389 version_index = iver.vs_vers & VERSYM_VERSION;
9390 if (version_index == 1 || version_index == 2)
9391 {
9392 /* This is the base or first version. We can use it. */
9393 free (extversym);
9394 free (isymbuf);
9395 return TRUE;
9396 }
9397 }
9398
9399 free (extversym);
9400 free (isymbuf);
9401 }
9402
9403 return FALSE;
9404}
9405
b8871f35
L
9406/* Convert ELF common symbol TYPE. */
9407
9408static int
9409elf_link_convert_common_type (struct bfd_link_info *info, int type)
9410{
9411 /* Commom symbol can only appear in relocatable link. */
9412 if (!bfd_link_relocatable (info))
9413 abort ();
9414 switch (info->elf_stt_common)
9415 {
9416 case unchanged:
9417 break;
9418 case elf_stt_common:
9419 type = STT_COMMON;
9420 break;
9421 case no_elf_stt_common:
9422 type = STT_OBJECT;
9423 break;
9424 }
9425 return type;
9426}
9427
c152c796
AM
9428/* Add an external symbol to the symbol table. This is called from
9429 the hash table traversal routine. When generating a shared object,
9430 we go through the symbol table twice. The first time we output
9431 anything that might have been forced to local scope in a version
9432 script. The second time we output the symbols that are still
9433 global symbols. */
9434
9435static bfd_boolean
7686d77d 9436elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9437{
7686d77d 9438 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9439 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9440 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9441 bfd_boolean strip;
9442 Elf_Internal_Sym sym;
9443 asection *input_sec;
9444 const struct elf_backend_data *bed;
6e0b88f1
AM
9445 long indx;
9446 int ret;
b8871f35 9447 unsigned int type;
c152c796
AM
9448
9449 if (h->root.type == bfd_link_hash_warning)
9450 {
9451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9452 if (h->root.type == bfd_link_hash_new)
9453 return TRUE;
9454 }
9455
9456 /* Decide whether to output this symbol in this pass. */
9457 if (eoinfo->localsyms)
9458 {
4deb8f71 9459 if (!h->forced_local)
c152c796
AM
9460 return TRUE;
9461 }
9462 else
9463 {
4deb8f71 9464 if (h->forced_local)
c152c796
AM
9465 return TRUE;
9466 }
9467
8b127cbc 9468 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9469
12ac1cf5 9470 if (h->root.type == bfd_link_hash_undefined)
c152c796 9471 {
12ac1cf5
NC
9472 /* If we have an undefined symbol reference here then it must have
9473 come from a shared library that is being linked in. (Undefined
98da7939
L
9474 references in regular files have already been handled unless
9475 they are in unreferenced sections which are removed by garbage
9476 collection). */
12ac1cf5
NC
9477 bfd_boolean ignore_undef = FALSE;
9478
9479 /* Some symbols may be special in that the fact that they're
9480 undefined can be safely ignored - let backend determine that. */
9481 if (bed->elf_backend_ignore_undef_symbol)
9482 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9483
9484 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9485 if (!ignore_undef
12ac1cf5 9486 && h->ref_dynamic
8b127cbc
AM
9487 && (!h->ref_regular || flinfo->info->gc_sections)
9488 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9489 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9490 (*flinfo->info->callbacks->undefined_symbol)
9491 (flinfo->info, h->root.root.string,
9492 h->ref_regular ? NULL : h->root.u.undef.abfd,
9493 NULL, 0,
9494 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9495
9496 /* Strip a global symbol defined in a discarded section. */
9497 if (h->indx == -3)
9498 return TRUE;
c152c796
AM
9499 }
9500
9501 /* We should also warn if a forced local symbol is referenced from
9502 shared libraries. */
0e1862bb 9503 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9504 && h->forced_local
9505 && h->ref_dynamic
371a5866 9506 && h->def_regular
f5385ebf 9507 && !h->dynamic_def
ee659f1f 9508 && h->ref_dynamic_nonweak
8b127cbc 9509 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9510 {
17d078c5
AM
9511 bfd *def_bfd;
9512 const char *msg;
90c984fc
L
9513 struct elf_link_hash_entry *hi = h;
9514
9515 /* Check indirect symbol. */
9516 while (hi->root.type == bfd_link_hash_indirect)
9517 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9518
9519 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9520 /* xgettext:c-format */
17d078c5
AM
9521 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9522 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9523 /* xgettext:c-format */
17d078c5
AM
9524 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9525 else
695344c0 9526 /* xgettext:c-format */
17d078c5 9527 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9528 def_bfd = flinfo->output_bfd;
90c984fc
L
9529 if (hi->root.u.def.section != bfd_abs_section_ptr)
9530 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9531 _bfd_error_handler (msg, flinfo->output_bfd,
9532 h->root.root.string, def_bfd);
17d078c5 9533 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9534 eoinfo->failed = TRUE;
9535 return FALSE;
9536 }
9537
9538 /* We don't want to output symbols that have never been mentioned by
9539 a regular file, or that we have been told to strip. However, if
9540 h->indx is set to -2, the symbol is used by a reloc and we must
9541 output it. */
d983c8c5 9542 strip = FALSE;
c152c796 9543 if (h->indx == -2)
d983c8c5 9544 ;
f5385ebf 9545 else if ((h->def_dynamic
77cfaee6
AM
9546 || h->ref_dynamic
9547 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9548 && !h->def_regular
9549 && !h->ref_regular)
c152c796 9550 strip = TRUE;
8b127cbc 9551 else if (flinfo->info->strip == strip_all)
c152c796 9552 strip = TRUE;
8b127cbc
AM
9553 else if (flinfo->info->strip == strip_some
9554 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9555 h->root.root.string, FALSE, FALSE) == NULL)
9556 strip = TRUE;
d56d55e7
AM
9557 else if ((h->root.type == bfd_link_hash_defined
9558 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9559 && ((flinfo->info->strip_discarded
dbaa2011 9560 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9561 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9562 && h->root.u.def.section->owner != NULL
d56d55e7 9563 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9564 strip = TRUE;
9e2278f5
AM
9565 else if ((h->root.type == bfd_link_hash_undefined
9566 || h->root.type == bfd_link_hash_undefweak)
9567 && h->root.u.undef.abfd != NULL
9568 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9569 strip = TRUE;
c152c796 9570
b8871f35
L
9571 type = h->type;
9572
c152c796 9573 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9574 nothing else to do. However, if it is a forced local symbol or
9575 an ifunc symbol we need to give the backend finish_dynamic_symbol
9576 function a chance to make it dynamic. */
c152c796
AM
9577 if (strip
9578 && h->dynindx == -1
b8871f35 9579 && type != STT_GNU_IFUNC
f5385ebf 9580 && !h->forced_local)
c152c796
AM
9581 return TRUE;
9582
9583 sym.st_value = 0;
9584 sym.st_size = h->size;
9585 sym.st_other = h->other;
c152c796
AM
9586 switch (h->root.type)
9587 {
9588 default:
9589 case bfd_link_hash_new:
9590 case bfd_link_hash_warning:
9591 abort ();
9592 return FALSE;
9593
9594 case bfd_link_hash_undefined:
9595 case bfd_link_hash_undefweak:
9596 input_sec = bfd_und_section_ptr;
9597 sym.st_shndx = SHN_UNDEF;
9598 break;
9599
9600 case bfd_link_hash_defined:
9601 case bfd_link_hash_defweak:
9602 {
9603 input_sec = h->root.u.def.section;
9604 if (input_sec->output_section != NULL)
9605 {
9606 sym.st_shndx =
8b127cbc 9607 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9608 input_sec->output_section);
9609 if (sym.st_shndx == SHN_BAD)
9610 {
4eca0228 9611 _bfd_error_handler
695344c0 9612 /* xgettext:c-format */
d003868e 9613 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9614 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9615 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9616 eoinfo->failed = TRUE;
9617 return FALSE;
9618 }
9619
9620 /* ELF symbols in relocatable files are section relative,
9621 but in nonrelocatable files they are virtual
9622 addresses. */
9623 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9624 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9625 {
9626 sym.st_value += input_sec->output_section->vma;
9627 if (h->type == STT_TLS)
9628 {
8b127cbc 9629 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9630 if (tls_sec != NULL)
9631 sym.st_value -= tls_sec->vma;
c152c796
AM
9632 }
9633 }
9634 }
9635 else
9636 {
9637 BFD_ASSERT (input_sec->owner == NULL
9638 || (input_sec->owner->flags & DYNAMIC) != 0);
9639 sym.st_shndx = SHN_UNDEF;
9640 input_sec = bfd_und_section_ptr;
9641 }
9642 }
9643 break;
9644
9645 case bfd_link_hash_common:
9646 input_sec = h->root.u.c.p->section;
a4d8e49b 9647 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9648 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9649 break;
9650
9651 case bfd_link_hash_indirect:
9652 /* These symbols are created by symbol versioning. They point
9653 to the decorated version of the name. For example, if the
9654 symbol foo@@GNU_1.2 is the default, which should be used when
9655 foo is used with no version, then we add an indirect symbol
9656 foo which points to foo@@GNU_1.2. We ignore these symbols,
9657 since the indirected symbol is already in the hash table. */
9658 return TRUE;
9659 }
9660
b8871f35
L
9661 if (type == STT_COMMON || type == STT_OBJECT)
9662 switch (h->root.type)
9663 {
9664 case bfd_link_hash_common:
9665 type = elf_link_convert_common_type (flinfo->info, type);
9666 break;
9667 case bfd_link_hash_defined:
9668 case bfd_link_hash_defweak:
9669 if (bed->common_definition (&sym))
9670 type = elf_link_convert_common_type (flinfo->info, type);
9671 else
9672 type = STT_OBJECT;
9673 break;
9674 case bfd_link_hash_undefined:
9675 case bfd_link_hash_undefweak:
9676 break;
9677 default:
9678 abort ();
9679 }
9680
4deb8f71 9681 if (h->forced_local)
b8871f35
L
9682 {
9683 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9684 /* Turn off visibility on local symbol. */
9685 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9686 }
9687 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9688 else if (h->unique_global && h->def_regular)
9689 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9690 else if (h->root.type == bfd_link_hash_undefweak
9691 || h->root.type == bfd_link_hash_defweak)
9692 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9693 else
9694 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9695 sym.st_target_internal = h->target_internal;
9696
c152c796
AM
9697 /* Give the processor backend a chance to tweak the symbol value,
9698 and also to finish up anything that needs to be done for this
9699 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9700 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9701 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9702 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9703 && h->def_regular
0e1862bb 9704 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9705 || ((h->dynindx != -1
9706 || h->forced_local)
0e1862bb 9707 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9708 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9709 || h->root.type != bfd_link_hash_undefweak))
9710 || !h->forced_local)
8b127cbc 9711 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9712 {
9713 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9714 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9715 {
9716 eoinfo->failed = TRUE;
9717 return FALSE;
9718 }
9719 }
9720
9721 /* If we are marking the symbol as undefined, and there are no
9722 non-weak references to this symbol from a regular object, then
9723 mark the symbol as weak undefined; if there are non-weak
9724 references, mark the symbol as strong. We can't do this earlier,
9725 because it might not be marked as undefined until the
9726 finish_dynamic_symbol routine gets through with it. */
9727 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9728 && h->ref_regular
c152c796
AM
9729 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9730 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9731 {
9732 int bindtype;
b8871f35 9733 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9734
9735 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9736 if (type == STT_GNU_IFUNC)
9737 type = STT_FUNC;
c152c796 9738
f5385ebf 9739 if (h->ref_regular_nonweak)
c152c796
AM
9740 bindtype = STB_GLOBAL;
9741 else
9742 bindtype = STB_WEAK;
2955ec4c 9743 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9744 }
9745
bda987c2
CD
9746 /* If this is a symbol defined in a dynamic library, don't use the
9747 symbol size from the dynamic library. Relinking an executable
9748 against a new library may introduce gratuitous changes in the
9749 executable's symbols if we keep the size. */
9750 if (sym.st_shndx == SHN_UNDEF
9751 && !h->def_regular
9752 && h->def_dynamic)
9753 sym.st_size = 0;
9754
c152c796
AM
9755 /* If a non-weak symbol with non-default visibility is not defined
9756 locally, it is a fatal error. */
0e1862bb 9757 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9758 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9759 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9760 && h->root.type == bfd_link_hash_undefined
f5385ebf 9761 && !h->def_regular)
c152c796 9762 {
17d078c5
AM
9763 const char *msg;
9764
9765 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9766 /* xgettext:c-format */
17d078c5
AM
9767 msg = _("%B: protected symbol `%s' isn't defined");
9768 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9769 /* xgettext:c-format */
17d078c5
AM
9770 msg = _("%B: internal symbol `%s' isn't defined");
9771 else
695344c0 9772 /* xgettext:c-format */
17d078c5 9773 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9774 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9775 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9776 eoinfo->failed = TRUE;
9777 return FALSE;
9778 }
9779
9780 /* If this symbol should be put in the .dynsym section, then put it
9781 there now. We already know the symbol index. We also fill in
9782 the entry in the .hash section. */
cae1fbbb 9783 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9784 && h->dynindx != -1
8b127cbc 9785 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9786 {
c152c796
AM
9787 bfd_byte *esym;
9788
90c984fc
L
9789 /* Since there is no version information in the dynamic string,
9790 if there is no version info in symbol version section, we will
1659f720 9791 have a run-time problem if not linking executable, referenced
4deb8f71 9792 by shared library, or not bound locally. */
1659f720 9793 if (h->verinfo.verdef == NULL
0e1862bb 9794 && (!bfd_link_executable (flinfo->info)
1659f720
L
9795 || h->ref_dynamic
9796 || !h->def_regular))
90c984fc
L
9797 {
9798 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9799
9800 if (p && p [1] != '\0')
9801 {
4eca0228 9802 _bfd_error_handler
695344c0 9803 /* xgettext:c-format */
90c984fc
L
9804 (_("%B: No symbol version section for versioned symbol `%s'"),
9805 flinfo->output_bfd, h->root.root.string);
9806 eoinfo->failed = TRUE;
9807 return FALSE;
9808 }
9809 }
9810
c152c796 9811 sym.st_name = h->dynstr_index;
cae1fbbb
L
9812 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9813 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9814 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9815 {
9816 eoinfo->failed = TRUE;
9817 return FALSE;
9818 }
8b127cbc 9819 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9820
8b127cbc 9821 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9822 {
9823 size_t hash_entry_size;
9824 bfd_byte *bucketpos;
9825 bfd_vma chain;
41198d0c
L
9826 size_t bucketcount;
9827 size_t bucket;
9828
8b127cbc 9829 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9830 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9831
9832 hash_entry_size
8b127cbc
AM
9833 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9834 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9835 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9836 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9837 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9838 bucketpos);
9839 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9840 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9841 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9842 }
c152c796 9843
8b127cbc 9844 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9845 {
9846 Elf_Internal_Versym iversym;
9847 Elf_External_Versym *eversym;
9848
f5385ebf 9849 if (!h->def_regular)
c152c796 9850 {
7b20f099
AM
9851 if (h->verinfo.verdef == NULL
9852 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9853 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9854 iversym.vs_vers = 0;
9855 else
9856 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9857 }
9858 else
9859 {
9860 if (h->verinfo.vertree == NULL)
9861 iversym.vs_vers = 1;
9862 else
9863 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9864 if (flinfo->info->create_default_symver)
3e3b46e5 9865 iversym.vs_vers++;
c152c796
AM
9866 }
9867
422f1182 9868 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9869 defined locally. */
422f1182 9870 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9871 iversym.vs_vers |= VERSYM_HIDDEN;
9872
8b127cbc 9873 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9874 eversym += h->dynindx;
8b127cbc 9875 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9876 }
9877 }
9878
d983c8c5
AM
9879 /* If the symbol is undefined, and we didn't output it to .dynsym,
9880 strip it from .symtab too. Obviously we can't do this for
9881 relocatable output or when needed for --emit-relocs. */
9882 else if (input_sec == bfd_und_section_ptr
9883 && h->indx != -2
0e1862bb 9884 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9885 return TRUE;
9886 /* Also strip others that we couldn't earlier due to dynamic symbol
9887 processing. */
9888 if (strip)
9889 return TRUE;
9890 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9891 return TRUE;
9892
2ec55de3
AM
9893 /* Output a FILE symbol so that following locals are not associated
9894 with the wrong input file. We need one for forced local symbols
9895 if we've seen more than one FILE symbol or when we have exactly
9896 one FILE symbol but global symbols are present in a file other
9897 than the one with the FILE symbol. We also need one if linker
9898 defined symbols are present. In practice these conditions are
9899 always met, so just emit the FILE symbol unconditionally. */
9900 if (eoinfo->localsyms
9901 && !eoinfo->file_sym_done
9902 && eoinfo->flinfo->filesym_count != 0)
9903 {
9904 Elf_Internal_Sym fsym;
9905
9906 memset (&fsym, 0, sizeof (fsym));
9907 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9908 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9909 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9910 bfd_und_section_ptr, NULL))
2ec55de3
AM
9911 return FALSE;
9912
9913 eoinfo->file_sym_done = TRUE;
9914 }
9915
8b127cbc 9916 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9917 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9918 input_sec, h);
6e0b88f1 9919 if (ret == 0)
c152c796
AM
9920 {
9921 eoinfo->failed = TRUE;
9922 return FALSE;
9923 }
6e0b88f1
AM
9924 else if (ret == 1)
9925 h->indx = indx;
9926 else if (h->indx == -2)
9927 abort();
c152c796
AM
9928
9929 return TRUE;
9930}
9931
cdd3575c
AM
9932/* Return TRUE if special handling is done for relocs in SEC against
9933 symbols defined in discarded sections. */
9934
c152c796
AM
9935static bfd_boolean
9936elf_section_ignore_discarded_relocs (asection *sec)
9937{
9938 const struct elf_backend_data *bed;
9939
cdd3575c
AM
9940 switch (sec->sec_info_type)
9941 {
dbaa2011
AM
9942 case SEC_INFO_TYPE_STABS:
9943 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9944 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9945 return TRUE;
9946 default:
9947 break;
9948 }
c152c796
AM
9949
9950 bed = get_elf_backend_data (sec->owner);
9951 if (bed->elf_backend_ignore_discarded_relocs != NULL
9952 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9953 return TRUE;
9954
9955 return FALSE;
9956}
9957
9e66c942
AM
9958/* Return a mask saying how ld should treat relocations in SEC against
9959 symbols defined in discarded sections. If this function returns
9960 COMPLAIN set, ld will issue a warning message. If this function
9961 returns PRETEND set, and the discarded section was link-once and the
9962 same size as the kept link-once section, ld will pretend that the
9963 symbol was actually defined in the kept section. Otherwise ld will
9964 zero the reloc (at least that is the intent, but some cooperation by
9965 the target dependent code is needed, particularly for REL targets). */
9966
8a696751
AM
9967unsigned int
9968_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9969{
9e66c942 9970 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9971 return PRETEND;
cdd3575c
AM
9972
9973 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9974 return 0;
cdd3575c
AM
9975
9976 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9977 return 0;
cdd3575c 9978
9e66c942 9979 return COMPLAIN | PRETEND;
cdd3575c
AM
9980}
9981
3d7f7666
L
9982/* Find a match between a section and a member of a section group. */
9983
9984static asection *
c0f00686
L
9985match_group_member (asection *sec, asection *group,
9986 struct bfd_link_info *info)
3d7f7666
L
9987{
9988 asection *first = elf_next_in_group (group);
9989 asection *s = first;
9990
9991 while (s != NULL)
9992 {
c0f00686 9993 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9994 return s;
9995
83180ade 9996 s = elf_next_in_group (s);
3d7f7666
L
9997 if (s == first)
9998 break;
9999 }
10000
10001 return NULL;
10002}
10003
01b3c8ab 10004/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10005 to replace it. Return the replacement if it is OK. Otherwise return
10006 NULL. */
01b3c8ab
L
10007
10008asection *
c0f00686 10009_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10010{
10011 asection *kept;
10012
10013 kept = sec->kept_section;
10014 if (kept != NULL)
10015 {
c2370991 10016 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10017 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10018 if (kept != NULL
10019 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10020 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10021 kept = NULL;
c2370991 10022 sec->kept_section = kept;
01b3c8ab
L
10023 }
10024 return kept;
10025}
10026
c152c796
AM
10027/* Link an input file into the linker output file. This function
10028 handles all the sections and relocations of the input file at once.
10029 This is so that we only have to read the local symbols once, and
10030 don't have to keep them in memory. */
10031
10032static bfd_boolean
8b127cbc 10033elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10034{
ece5ef60 10035 int (*relocate_section)
c152c796
AM
10036 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10037 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10038 bfd *output_bfd;
10039 Elf_Internal_Shdr *symtab_hdr;
10040 size_t locsymcount;
10041 size_t extsymoff;
10042 Elf_Internal_Sym *isymbuf;
10043 Elf_Internal_Sym *isym;
10044 Elf_Internal_Sym *isymend;
10045 long *pindex;
10046 asection **ppsection;
10047 asection *o;
10048 const struct elf_backend_data *bed;
c152c796 10049 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10050 bfd_size_type address_size;
10051 bfd_vma r_type_mask;
10052 int r_sym_shift;
ffbc01cc 10053 bfd_boolean have_file_sym = FALSE;
c152c796 10054
8b127cbc 10055 output_bfd = flinfo->output_bfd;
c152c796
AM
10056 bed = get_elf_backend_data (output_bfd);
10057 relocate_section = bed->elf_backend_relocate_section;
10058
10059 /* If this is a dynamic object, we don't want to do anything here:
10060 we don't want the local symbols, and we don't want the section
10061 contents. */
10062 if ((input_bfd->flags & DYNAMIC) != 0)
10063 return TRUE;
10064
c152c796
AM
10065 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10066 if (elf_bad_symtab (input_bfd))
10067 {
10068 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10069 extsymoff = 0;
10070 }
10071 else
10072 {
10073 locsymcount = symtab_hdr->sh_info;
10074 extsymoff = symtab_hdr->sh_info;
10075 }
10076
10077 /* Read the local symbols. */
10078 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10079 if (isymbuf == NULL && locsymcount != 0)
10080 {
10081 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10082 flinfo->internal_syms,
10083 flinfo->external_syms,
10084 flinfo->locsym_shndx);
c152c796
AM
10085 if (isymbuf == NULL)
10086 return FALSE;
10087 }
10088
10089 /* Find local symbol sections and adjust values of symbols in
10090 SEC_MERGE sections. Write out those local symbols we know are
10091 going into the output file. */
10092 isymend = isymbuf + locsymcount;
8b127cbc 10093 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10094 isym < isymend;
10095 isym++, pindex++, ppsection++)
10096 {
10097 asection *isec;
10098 const char *name;
10099 Elf_Internal_Sym osym;
6e0b88f1
AM
10100 long indx;
10101 int ret;
c152c796
AM
10102
10103 *pindex = -1;
10104
10105 if (elf_bad_symtab (input_bfd))
10106 {
10107 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10108 {
10109 *ppsection = NULL;
10110 continue;
10111 }
10112 }
10113
10114 if (isym->st_shndx == SHN_UNDEF)
10115 isec = bfd_und_section_ptr;
c152c796
AM
10116 else if (isym->st_shndx == SHN_ABS)
10117 isec = bfd_abs_section_ptr;
10118 else if (isym->st_shndx == SHN_COMMON)
10119 isec = bfd_com_section_ptr;
10120 else
10121 {
cb33740c
AM
10122 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10123 if (isec == NULL)
10124 {
10125 /* Don't attempt to output symbols with st_shnx in the
10126 reserved range other than SHN_ABS and SHN_COMMON. */
10127 *ppsection = NULL;
10128 continue;
10129 }
dbaa2011 10130 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10131 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10132 isym->st_value =
10133 _bfd_merged_section_offset (output_bfd, &isec,
10134 elf_section_data (isec)->sec_info,
10135 isym->st_value);
c152c796
AM
10136 }
10137
10138 *ppsection = isec;
10139
d983c8c5
AM
10140 /* Don't output the first, undefined, symbol. In fact, don't
10141 output any undefined local symbol. */
10142 if (isec == bfd_und_section_ptr)
c152c796
AM
10143 continue;
10144
10145 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10146 {
10147 /* We never output section symbols. Instead, we use the
10148 section symbol of the corresponding section in the output
10149 file. */
10150 continue;
10151 }
10152
10153 /* If we are stripping all symbols, we don't want to output this
10154 one. */
8b127cbc 10155 if (flinfo->info->strip == strip_all)
c152c796
AM
10156 continue;
10157
10158 /* If we are discarding all local symbols, we don't want to
10159 output this one. If we are generating a relocatable output
10160 file, then some of the local symbols may be required by
10161 relocs; we output them below as we discover that they are
10162 needed. */
8b127cbc 10163 if (flinfo->info->discard == discard_all)
c152c796
AM
10164 continue;
10165
10166 /* If this symbol is defined in a section which we are
f02571c5
AM
10167 discarding, we don't need to keep it. */
10168 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10169 && isym->st_shndx < SHN_LORESERVE
10170 && bfd_section_removed_from_list (output_bfd,
10171 isec->output_section))
e75a280b
L
10172 continue;
10173
c152c796
AM
10174 /* Get the name of the symbol. */
10175 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10176 isym->st_name);
10177 if (name == NULL)
10178 return FALSE;
10179
10180 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10181 if ((flinfo->info->strip == strip_some
10182 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10183 == NULL))
8b127cbc 10184 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10185 && (isec->flags & SEC_MERGE)
10186 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10187 || flinfo->info->discard == discard_l)
c152c796
AM
10188 && bfd_is_local_label_name (input_bfd, name)))
10189 continue;
10190
ffbc01cc
AM
10191 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10192 {
ce875075
AM
10193 if (input_bfd->lto_output)
10194 /* -flto puts a temp file name here. This means builds
10195 are not reproducible. Discard the symbol. */
10196 continue;
ffbc01cc
AM
10197 have_file_sym = TRUE;
10198 flinfo->filesym_count += 1;
10199 }
10200 if (!have_file_sym)
10201 {
10202 /* In the absence of debug info, bfd_find_nearest_line uses
10203 FILE symbols to determine the source file for local
10204 function symbols. Provide a FILE symbol here if input
10205 files lack such, so that their symbols won't be
10206 associated with a previous input file. It's not the
10207 source file, but the best we can do. */
10208 have_file_sym = TRUE;
10209 flinfo->filesym_count += 1;
10210 memset (&osym, 0, sizeof (osym));
10211 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10212 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10213 if (!elf_link_output_symstrtab (flinfo,
10214 (input_bfd->lto_output ? NULL
10215 : input_bfd->filename),
10216 &osym, bfd_abs_section_ptr,
10217 NULL))
ffbc01cc
AM
10218 return FALSE;
10219 }
10220
c152c796
AM
10221 osym = *isym;
10222
10223 /* Adjust the section index for the output file. */
10224 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10225 isec->output_section);
10226 if (osym.st_shndx == SHN_BAD)
10227 return FALSE;
10228
c152c796
AM
10229 /* ELF symbols in relocatable files are section relative, but
10230 in executable files they are virtual addresses. Note that
10231 this code assumes that all ELF sections have an associated
10232 BFD section with a reasonable value for output_offset; below
10233 we assume that they also have a reasonable value for
10234 output_section. Any special sections must be set up to meet
10235 these requirements. */
10236 osym.st_value += isec->output_offset;
0e1862bb 10237 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10238 {
10239 osym.st_value += isec->output_section->vma;
10240 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10241 {
10242 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10243 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10244 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10245 }
10246 }
10247
6e0b88f1 10248 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10249 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10250 if (ret == 0)
c152c796 10251 return FALSE;
6e0b88f1
AM
10252 else if (ret == 1)
10253 *pindex = indx;
c152c796
AM
10254 }
10255
310fd250
L
10256 if (bed->s->arch_size == 32)
10257 {
10258 r_type_mask = 0xff;
10259 r_sym_shift = 8;
10260 address_size = 4;
10261 }
10262 else
10263 {
10264 r_type_mask = 0xffffffff;
10265 r_sym_shift = 32;
10266 address_size = 8;
10267 }
10268
c152c796
AM
10269 /* Relocate the contents of each section. */
10270 sym_hashes = elf_sym_hashes (input_bfd);
10271 for (o = input_bfd->sections; o != NULL; o = o->next)
10272 {
10273 bfd_byte *contents;
10274
10275 if (! o->linker_mark)
10276 {
10277 /* This section was omitted from the link. */
10278 continue;
10279 }
10280
0e1862bb 10281 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10282 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10283 {
10284 /* Deal with the group signature symbol. */
10285 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10286 unsigned long symndx = sec_data->this_hdr.sh_info;
10287 asection *osec = o->output_section;
10288
10289 if (symndx >= locsymcount
10290 || (elf_bad_symtab (input_bfd)
8b127cbc 10291 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10292 {
10293 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10294 while (h->root.type == bfd_link_hash_indirect
10295 || h->root.type == bfd_link_hash_warning)
10296 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10297 /* Arrange for symbol to be output. */
10298 h->indx = -2;
10299 elf_section_data (osec)->this_hdr.sh_info = -2;
10300 }
10301 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10302 {
10303 /* We'll use the output section target_index. */
8b127cbc 10304 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10305 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10306 }
10307 else
10308 {
8b127cbc 10309 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10310 {
10311 /* Otherwise output the local symbol now. */
10312 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10313 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10314 const char *name;
6e0b88f1
AM
10315 long indx;
10316 int ret;
bcacc0f5
AM
10317
10318 name = bfd_elf_string_from_elf_section (input_bfd,
10319 symtab_hdr->sh_link,
10320 sym.st_name);
10321 if (name == NULL)
10322 return FALSE;
10323
10324 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10325 sec);
10326 if (sym.st_shndx == SHN_BAD)
10327 return FALSE;
10328
10329 sym.st_value += o->output_offset;
10330
6e0b88f1 10331 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10332 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10333 NULL);
6e0b88f1 10334 if (ret == 0)
bcacc0f5 10335 return FALSE;
6e0b88f1 10336 else if (ret == 1)
8b127cbc 10337 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10338 else
10339 abort ();
bcacc0f5
AM
10340 }
10341 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10342 = flinfo->indices[symndx];
bcacc0f5
AM
10343 }
10344 }
10345
c152c796 10346 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10347 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10348 continue;
10349
10350 if ((o->flags & SEC_LINKER_CREATED) != 0)
10351 {
10352 /* Section was created by _bfd_elf_link_create_dynamic_sections
10353 or somesuch. */
10354 continue;
10355 }
10356
10357 /* Get the contents of the section. They have been cached by a
10358 relaxation routine. Note that o is a section in an input
10359 file, so the contents field will not have been set by any of
10360 the routines which work on output files. */
10361 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10362 {
10363 contents = elf_section_data (o)->this_hdr.contents;
10364 if (bed->caches_rawsize
10365 && o->rawsize != 0
10366 && o->rawsize < o->size)
10367 {
10368 memcpy (flinfo->contents, contents, o->rawsize);
10369 contents = flinfo->contents;
10370 }
10371 }
c152c796
AM
10372 else
10373 {
8b127cbc 10374 contents = flinfo->contents;
4a114e3e 10375 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10376 return FALSE;
10377 }
10378
10379 if ((o->flags & SEC_RELOC) != 0)
10380 {
10381 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10382 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10383 int action_discarded;
ece5ef60 10384 int ret;
c152c796
AM
10385
10386 /* Get the swapped relocs. */
10387 internal_relocs
8b127cbc
AM
10388 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10389 flinfo->internal_relocs, FALSE);
c152c796
AM
10390 if (internal_relocs == NULL
10391 && o->reloc_count > 0)
10392 return FALSE;
10393
310fd250
L
10394 /* We need to reverse-copy input .ctors/.dtors sections if
10395 they are placed in .init_array/.finit_array for output. */
10396 if (o->size > address_size
10397 && ((strncmp (o->name, ".ctors", 6) == 0
10398 && strcmp (o->output_section->name,
10399 ".init_array") == 0)
10400 || (strncmp (o->name, ".dtors", 6) == 0
10401 && strcmp (o->output_section->name,
10402 ".fini_array") == 0))
10403 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10404 {
310fd250
L
10405 if (o->size != o->reloc_count * address_size)
10406 {
4eca0228 10407 _bfd_error_handler
695344c0 10408 /* xgettext:c-format */
310fd250
L
10409 (_("error: %B: size of section %A is not "
10410 "multiple of address size"),
10411 input_bfd, o);
10412 bfd_set_error (bfd_error_on_input);
10413 return FALSE;
10414 }
10415 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10416 }
10417
0f02bbd9 10418 action_discarded = -1;
c152c796 10419 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10420 action_discarded = (*bed->action_discarded) (o);
10421
10422 /* Run through the relocs evaluating complex reloc symbols and
10423 looking for relocs against symbols from discarded sections
10424 or section symbols from removed link-once sections.
10425 Complain about relocs against discarded sections. Zero
10426 relocs against removed link-once sections. */
10427
10428 rel = internal_relocs;
10429 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10430 for ( ; rel < relend; rel++)
c152c796 10431 {
0f02bbd9
AM
10432 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10433 unsigned int s_type;
10434 asection **ps, *sec;
10435 struct elf_link_hash_entry *h = NULL;
10436 const char *sym_name;
c152c796 10437
0f02bbd9
AM
10438 if (r_symndx == STN_UNDEF)
10439 continue;
c152c796 10440
0f02bbd9
AM
10441 if (r_symndx >= locsymcount
10442 || (elf_bad_symtab (input_bfd)
8b127cbc 10443 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10444 {
10445 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10446
0f02bbd9
AM
10447 /* Badly formatted input files can contain relocs that
10448 reference non-existant symbols. Check here so that
10449 we do not seg fault. */
10450 if (h == NULL)
c152c796 10451 {
0f02bbd9 10452 char buffer [32];
dce669a1 10453
0f02bbd9 10454 sprintf_vma (buffer, rel->r_info);
4eca0228 10455 _bfd_error_handler
695344c0 10456 /* xgettext:c-format */
0f02bbd9
AM
10457 (_("error: %B contains a reloc (0x%s) for section %A "
10458 "that references a non-existent global symbol"),
c08bb8dd 10459 input_bfd, buffer, o);
0f02bbd9
AM
10460 bfd_set_error (bfd_error_bad_value);
10461 return FALSE;
10462 }
3b36f7e6 10463
0f02bbd9
AM
10464 while (h->root.type == bfd_link_hash_indirect
10465 || h->root.type == bfd_link_hash_warning)
10466 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10467
0f02bbd9 10468 s_type = h->type;
cdd3575c 10469
9e2dec47 10470 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10471 mark the symbol as undefined. Note that the
10472 linker may attach linker created dynamic sections
10473 to the plugin bfd. Symbols defined in linker
10474 created sections are not plugin symbols. */
bc4e12de 10475 if ((h->root.non_ir_ref_regular
4070765b 10476 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10477 && (h->root.type == bfd_link_hash_defined
10478 || h->root.type == bfd_link_hash_defweak)
10479 && (h->root.u.def.section->flags
10480 & SEC_LINKER_CREATED) == 0
10481 && h->root.u.def.section->owner != NULL
10482 && (h->root.u.def.section->owner->flags
10483 & BFD_PLUGIN) != 0)
10484 {
10485 h->root.type = bfd_link_hash_undefined;
10486 h->root.u.undef.abfd = h->root.u.def.section->owner;
10487 }
10488
0f02bbd9
AM
10489 ps = NULL;
10490 if (h->root.type == bfd_link_hash_defined
10491 || h->root.type == bfd_link_hash_defweak)
10492 ps = &h->root.u.def.section;
10493
10494 sym_name = h->root.root.string;
10495 }
10496 else
10497 {
10498 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10499
10500 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10501 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10502 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10503 sym, *ps);
10504 }
c152c796 10505
c301e700 10506 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10507 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10508 {
10509 bfd_vma val;
10510 bfd_vma dot = (rel->r_offset
10511 + o->output_offset + o->output_section->vma);
10512#ifdef DEBUG
10513 printf ("Encountered a complex symbol!");
10514 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10515 input_bfd->filename, o->name,
10516 (long) (rel - internal_relocs));
0f02bbd9
AM
10517 printf (" symbol: idx %8.8lx, name %s\n",
10518 r_symndx, sym_name);
10519 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10520 (unsigned long) rel->r_info,
10521 (unsigned long) rel->r_offset);
10522#endif
8b127cbc 10523 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10524 isymbuf, locsymcount, s_type == STT_SRELC))
10525 return FALSE;
10526
10527 /* Symbol evaluated OK. Update to absolute value. */
10528 set_symbol_value (input_bfd, isymbuf, locsymcount,
10529 r_symndx, val);
10530 continue;
10531 }
10532
10533 if (action_discarded != -1 && ps != NULL)
10534 {
cdd3575c
AM
10535 /* Complain if the definition comes from a
10536 discarded section. */
dbaa2011 10537 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10538 {
cf35638d 10539 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10540 if (action_discarded & COMPLAIN)
8b127cbc 10541 (*flinfo->info->callbacks->einfo)
695344c0 10542 /* xgettext:c-format */
e1fffbe6 10543 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10544 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10545 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10546
87e5235d 10547 /* Try to do the best we can to support buggy old
e0ae6d6f 10548 versions of gcc. Pretend that the symbol is
87e5235d
AM
10549 really defined in the kept linkonce section.
10550 FIXME: This is quite broken. Modifying the
10551 symbol here means we will be changing all later
e0ae6d6f 10552 uses of the symbol, not just in this section. */
0f02bbd9 10553 if (action_discarded & PRETEND)
87e5235d 10554 {
01b3c8ab
L
10555 asection *kept;
10556
c0f00686 10557 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10558 flinfo->info);
01b3c8ab 10559 if (kept != NULL)
87e5235d
AM
10560 {
10561 *ps = kept;
10562 continue;
10563 }
10564 }
c152c796
AM
10565 }
10566 }
10567 }
10568
10569 /* Relocate the section by invoking a back end routine.
10570
10571 The back end routine is responsible for adjusting the
10572 section contents as necessary, and (if using Rela relocs
10573 and generating a relocatable output file) adjusting the
10574 reloc addend as necessary.
10575
10576 The back end routine does not have to worry about setting
10577 the reloc address or the reloc symbol index.
10578
10579 The back end routine is given a pointer to the swapped in
10580 internal symbols, and can access the hash table entries
10581 for the external symbols via elf_sym_hashes (input_bfd).
10582
10583 When generating relocatable output, the back end routine
10584 must handle STB_LOCAL/STT_SECTION symbols specially. The
10585 output symbol is going to be a section symbol
10586 corresponding to the output section, which will require
10587 the addend to be adjusted. */
10588
8b127cbc 10589 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10590 input_bfd, o, contents,
10591 internal_relocs,
10592 isymbuf,
8b127cbc 10593 flinfo->sections);
ece5ef60 10594 if (!ret)
c152c796
AM
10595 return FALSE;
10596
ece5ef60 10597 if (ret == 2
0e1862bb 10598 || bfd_link_relocatable (flinfo->info)
8b127cbc 10599 || flinfo->info->emitrelocations)
c152c796
AM
10600 {
10601 Elf_Internal_Rela *irela;
d4730f92 10602 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10603 bfd_vma last_offset;
10604 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10605 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10606 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10607 unsigned int next_erel;
c152c796 10608 bfd_boolean rela_normal;
d4730f92 10609 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10610
d4730f92
BS
10611 esdi = elf_section_data (o);
10612 esdo = elf_section_data (o->output_section);
10613 rela_normal = FALSE;
c152c796
AM
10614
10615 /* Adjust the reloc addresses and symbol indices. */
10616
10617 irela = internal_relocs;
10618 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10619 rel_hash = esdo->rel.hashes + esdo->rel.count;
10620 /* We start processing the REL relocs, if any. When we reach
10621 IRELAMID in the loop, we switch to the RELA relocs. */
10622 irelamid = irela;
10623 if (esdi->rel.hdr != NULL)
10624 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10625 * bed->s->int_rels_per_ext_rel);
eac338cf 10626 rel_hash_list = rel_hash;
d4730f92 10627 rela_hash_list = NULL;
c152c796 10628 last_offset = o->output_offset;
0e1862bb 10629 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10630 last_offset += o->output_section->vma;
10631 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10632 {
10633 unsigned long r_symndx;
10634 asection *sec;
10635 Elf_Internal_Sym sym;
10636
10637 if (next_erel == bed->s->int_rels_per_ext_rel)
10638 {
10639 rel_hash++;
10640 next_erel = 0;
10641 }
10642
d4730f92
BS
10643 if (irela == irelamid)
10644 {
10645 rel_hash = esdo->rela.hashes + esdo->rela.count;
10646 rela_hash_list = rel_hash;
10647 rela_normal = bed->rela_normal;
10648 }
10649
c152c796 10650 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10651 flinfo->info, o,
c152c796
AM
10652 irela->r_offset);
10653 if (irela->r_offset >= (bfd_vma) -2)
10654 {
10655 /* This is a reloc for a deleted entry or somesuch.
10656 Turn it into an R_*_NONE reloc, at the same
10657 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10658 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10659 being ordered. */
10660 irela->r_offset = last_offset;
10661 irela->r_info = 0;
10662 irela->r_addend = 0;
10663 continue;
10664 }
10665
10666 irela->r_offset += o->output_offset;
10667
10668 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10669 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10670 irela->r_offset += o->output_section->vma;
10671
10672 last_offset = irela->r_offset;
10673
10674 r_symndx = irela->r_info >> r_sym_shift;
10675 if (r_symndx == STN_UNDEF)
10676 continue;
10677
10678 if (r_symndx >= locsymcount
10679 || (elf_bad_symtab (input_bfd)
8b127cbc 10680 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10681 {
10682 struct elf_link_hash_entry *rh;
10683 unsigned long indx;
10684
10685 /* This is a reloc against a global symbol. We
10686 have not yet output all the local symbols, so
10687 we do not know the symbol index of any global
10688 symbol. We set the rel_hash entry for this
10689 reloc to point to the global hash table entry
10690 for this symbol. The symbol index is then
ee75fd95 10691 set at the end of bfd_elf_final_link. */
c152c796
AM
10692 indx = r_symndx - extsymoff;
10693 rh = elf_sym_hashes (input_bfd)[indx];
10694 while (rh->root.type == bfd_link_hash_indirect
10695 || rh->root.type == bfd_link_hash_warning)
10696 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10697
10698 /* Setting the index to -2 tells
10699 elf_link_output_extsym that this symbol is
10700 used by a reloc. */
10701 BFD_ASSERT (rh->indx < 0);
10702 rh->indx = -2;
10703
10704 *rel_hash = rh;
10705
10706 continue;
10707 }
10708
10709 /* This is a reloc against a local symbol. */
10710
10711 *rel_hash = NULL;
10712 sym = isymbuf[r_symndx];
8b127cbc 10713 sec = flinfo->sections[r_symndx];
c152c796
AM
10714 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10715 {
10716 /* I suppose the backend ought to fill in the
10717 section of any STT_SECTION symbol against a
6a8d1586 10718 processor specific section. */
cf35638d 10719 r_symndx = STN_UNDEF;
6a8d1586
AM
10720 if (bfd_is_abs_section (sec))
10721 ;
c152c796
AM
10722 else if (sec == NULL || sec->owner == NULL)
10723 {
10724 bfd_set_error (bfd_error_bad_value);
10725 return FALSE;
10726 }
10727 else
10728 {
6a8d1586
AM
10729 asection *osec = sec->output_section;
10730
10731 /* If we have discarded a section, the output
10732 section will be the absolute section. In
ab96bf03
AM
10733 case of discarded SEC_MERGE sections, use
10734 the kept section. relocate_section should
10735 have already handled discarded linkonce
10736 sections. */
6a8d1586
AM
10737 if (bfd_is_abs_section (osec)
10738 && sec->kept_section != NULL
10739 && sec->kept_section->output_section != NULL)
10740 {
10741 osec = sec->kept_section->output_section;
10742 irela->r_addend -= osec->vma;
10743 }
10744
10745 if (!bfd_is_abs_section (osec))
10746 {
10747 r_symndx = osec->target_index;
cf35638d 10748 if (r_symndx == STN_UNDEF)
74541ad4 10749 {
051d833a
AM
10750 irela->r_addend += osec->vma;
10751 osec = _bfd_nearby_section (output_bfd, osec,
10752 osec->vma);
10753 irela->r_addend -= osec->vma;
10754 r_symndx = osec->target_index;
74541ad4 10755 }
6a8d1586 10756 }
c152c796
AM
10757 }
10758
10759 /* Adjust the addend according to where the
10760 section winds up in the output section. */
10761 if (rela_normal)
10762 irela->r_addend += sec->output_offset;
10763 }
10764 else
10765 {
8b127cbc 10766 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10767 {
10768 unsigned long shlink;
10769 const char *name;
10770 asection *osec;
6e0b88f1 10771 long indx;
c152c796 10772
8b127cbc 10773 if (flinfo->info->strip == strip_all)
c152c796
AM
10774 {
10775 /* You can't do ld -r -s. */
10776 bfd_set_error (bfd_error_invalid_operation);
10777 return FALSE;
10778 }
10779
10780 /* This symbol was skipped earlier, but
10781 since it is needed by a reloc, we
10782 must output it now. */
10783 shlink = symtab_hdr->sh_link;
10784 name = (bfd_elf_string_from_elf_section
10785 (input_bfd, shlink, sym.st_name));
10786 if (name == NULL)
10787 return FALSE;
10788
10789 osec = sec->output_section;
10790 sym.st_shndx =
10791 _bfd_elf_section_from_bfd_section (output_bfd,
10792 osec);
10793 if (sym.st_shndx == SHN_BAD)
10794 return FALSE;
10795
10796 sym.st_value += sec->output_offset;
0e1862bb 10797 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10798 {
10799 sym.st_value += osec->vma;
10800 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10801 {
10802 /* STT_TLS symbols are relative to PT_TLS
10803 segment base. */
8b127cbc 10804 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10805 ->tls_sec != NULL);
8b127cbc 10806 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10807 ->tls_sec->vma);
10808 }
10809 }
10810
6e0b88f1 10811 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10812 ret = elf_link_output_symstrtab (flinfo, name,
10813 &sym, sec,
10814 NULL);
6e0b88f1 10815 if (ret == 0)
c152c796 10816 return FALSE;
6e0b88f1 10817 else if (ret == 1)
8b127cbc 10818 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10819 else
10820 abort ();
c152c796
AM
10821 }
10822
8b127cbc 10823 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10824 }
10825
10826 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10827 | (irela->r_info & r_type_mask));
10828 }
10829
10830 /* Swap out the relocs. */
d4730f92
BS
10831 input_rel_hdr = esdi->rel.hdr;
10832 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10833 {
d4730f92
BS
10834 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10835 input_rel_hdr,
10836 internal_relocs,
10837 rel_hash_list))
10838 return FALSE;
c152c796
AM
10839 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10840 * bed->s->int_rels_per_ext_rel);
eac338cf 10841 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10842 }
10843
10844 input_rela_hdr = esdi->rela.hdr;
10845 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10846 {
eac338cf 10847 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10848 input_rela_hdr,
eac338cf 10849 internal_relocs,
d4730f92 10850 rela_hash_list))
c152c796
AM
10851 return FALSE;
10852 }
10853 }
10854 }
10855
10856 /* Write out the modified section contents. */
10857 if (bed->elf_backend_write_section
8b127cbc 10858 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10859 contents))
c152c796
AM
10860 {
10861 /* Section written out. */
10862 }
10863 else switch (o->sec_info_type)
10864 {
dbaa2011 10865 case SEC_INFO_TYPE_STABS:
c152c796
AM
10866 if (! (_bfd_write_section_stabs
10867 (output_bfd,
8b127cbc 10868 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10869 o, &elf_section_data (o)->sec_info, contents)))
10870 return FALSE;
10871 break;
dbaa2011 10872 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10873 if (! _bfd_write_merged_section (output_bfd, o,
10874 elf_section_data (o)->sec_info))
10875 return FALSE;
10876 break;
dbaa2011 10877 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10878 {
8b127cbc 10879 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10880 o, contents))
10881 return FALSE;
10882 }
10883 break;
2f0c68f2
CM
10884 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10885 {
10886 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10887 flinfo->info,
10888 o, contents))
10889 return FALSE;
10890 }
10891 break;
c152c796
AM
10892 default:
10893 {
310fd250
L
10894 if (! (o->flags & SEC_EXCLUDE))
10895 {
10896 file_ptr offset = (file_ptr) o->output_offset;
10897 bfd_size_type todo = o->size;
37b01f6a
DG
10898
10899 offset *= bfd_octets_per_byte (output_bfd);
10900
310fd250
L
10901 if ((o->flags & SEC_ELF_REVERSE_COPY))
10902 {
10903 /* Reverse-copy input section to output. */
10904 do
10905 {
10906 todo -= address_size;
10907 if (! bfd_set_section_contents (output_bfd,
10908 o->output_section,
10909 contents + todo,
10910 offset,
10911 address_size))
10912 return FALSE;
10913 if (todo == 0)
10914 break;
10915 offset += address_size;
10916 }
10917 while (1);
10918 }
10919 else if (! bfd_set_section_contents (output_bfd,
10920 o->output_section,
10921 contents,
10922 offset, todo))
10923 return FALSE;
10924 }
c152c796
AM
10925 }
10926 break;
10927 }
10928 }
10929
10930 return TRUE;
10931}
10932
10933/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10934 requested by the linker, and does not come from any input file. This
c152c796
AM
10935 is used to build constructor and destructor tables when linking
10936 with -Ur. */
10937
10938static bfd_boolean
10939elf_reloc_link_order (bfd *output_bfd,
10940 struct bfd_link_info *info,
10941 asection *output_section,
10942 struct bfd_link_order *link_order)
10943{
10944 reloc_howto_type *howto;
10945 long indx;
10946 bfd_vma offset;
10947 bfd_vma addend;
d4730f92 10948 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10949 struct elf_link_hash_entry **rel_hash_ptr;
10950 Elf_Internal_Shdr *rel_hdr;
10951 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10952 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10953 bfd_byte *erel;
10954 unsigned int i;
d4730f92 10955 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10956
10957 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10958 if (howto == NULL)
10959 {
10960 bfd_set_error (bfd_error_bad_value);
10961 return FALSE;
10962 }
10963
10964 addend = link_order->u.reloc.p->addend;
10965
d4730f92
BS
10966 if (esdo->rel.hdr)
10967 reldata = &esdo->rel;
10968 else if (esdo->rela.hdr)
10969 reldata = &esdo->rela;
10970 else
10971 {
10972 reldata = NULL;
10973 BFD_ASSERT (0);
10974 }
10975
c152c796 10976 /* Figure out the symbol index. */
d4730f92 10977 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10978 if (link_order->type == bfd_section_reloc_link_order)
10979 {
10980 indx = link_order->u.reloc.p->u.section->target_index;
10981 BFD_ASSERT (indx != 0);
10982 *rel_hash_ptr = NULL;
10983 }
10984 else
10985 {
10986 struct elf_link_hash_entry *h;
10987
10988 /* Treat a reloc against a defined symbol as though it were
10989 actually against the section. */
10990 h = ((struct elf_link_hash_entry *)
10991 bfd_wrapped_link_hash_lookup (output_bfd, info,
10992 link_order->u.reloc.p->u.name,
10993 FALSE, FALSE, TRUE));
10994 if (h != NULL
10995 && (h->root.type == bfd_link_hash_defined
10996 || h->root.type == bfd_link_hash_defweak))
10997 {
10998 asection *section;
10999
11000 section = h->root.u.def.section;
11001 indx = section->output_section->target_index;
11002 *rel_hash_ptr = NULL;
11003 /* It seems that we ought to add the symbol value to the
11004 addend here, but in practice it has already been added
11005 because it was passed to constructor_callback. */
11006 addend += section->output_section->vma + section->output_offset;
11007 }
11008 else if (h != NULL)
11009 {
11010 /* Setting the index to -2 tells elf_link_output_extsym that
11011 this symbol is used by a reloc. */
11012 h->indx = -2;
11013 *rel_hash_ptr = h;
11014 indx = 0;
11015 }
11016 else
11017 {
1a72702b
AM
11018 (*info->callbacks->unattached_reloc)
11019 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11020 indx = 0;
11021 }
11022 }
11023
11024 /* If this is an inplace reloc, we must write the addend into the
11025 object file. */
11026 if (howto->partial_inplace && addend != 0)
11027 {
11028 bfd_size_type size;
11029 bfd_reloc_status_type rstat;
11030 bfd_byte *buf;
11031 bfd_boolean ok;
11032 const char *sym_name;
11033
a50b1753
NC
11034 size = (bfd_size_type) bfd_get_reloc_size (howto);
11035 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11036 if (buf == NULL && size != 0)
c152c796
AM
11037 return FALSE;
11038 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11039 switch (rstat)
11040 {
11041 case bfd_reloc_ok:
11042 break;
11043
11044 default:
11045 case bfd_reloc_outofrange:
11046 abort ();
11047
11048 case bfd_reloc_overflow:
11049 if (link_order->type == bfd_section_reloc_link_order)
11050 sym_name = bfd_section_name (output_bfd,
11051 link_order->u.reloc.p->u.section);
11052 else
11053 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11054 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11055 howto->name, addend, NULL, NULL,
11056 (bfd_vma) 0);
c152c796
AM
11057 break;
11058 }
37b01f6a 11059
c152c796 11060 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11061 link_order->offset
11062 * bfd_octets_per_byte (output_bfd),
11063 size);
c152c796
AM
11064 free (buf);
11065 if (! ok)
11066 return FALSE;
11067 }
11068
11069 /* The address of a reloc is relative to the section in a
11070 relocatable file, and is a virtual address in an executable
11071 file. */
11072 offset = link_order->offset;
0e1862bb 11073 if (! bfd_link_relocatable (info))
c152c796
AM
11074 offset += output_section->vma;
11075
11076 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11077 {
11078 irel[i].r_offset = offset;
11079 irel[i].r_info = 0;
11080 irel[i].r_addend = 0;
11081 }
11082 if (bed->s->arch_size == 32)
11083 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11084 else
11085 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11086
d4730f92 11087 rel_hdr = reldata->hdr;
c152c796
AM
11088 erel = rel_hdr->contents;
11089 if (rel_hdr->sh_type == SHT_REL)
11090 {
d4730f92 11091 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11092 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11093 }
11094 else
11095 {
11096 irel[0].r_addend = addend;
d4730f92 11097 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11098 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11099 }
11100
d4730f92 11101 ++reldata->count;
c152c796
AM
11102
11103 return TRUE;
11104}
11105
0b52efa6
PB
11106
11107/* Get the output vma of the section pointed to by the sh_link field. */
11108
11109static bfd_vma
11110elf_get_linked_section_vma (struct bfd_link_order *p)
11111{
11112 Elf_Internal_Shdr **elf_shdrp;
11113 asection *s;
11114 int elfsec;
11115
11116 s = p->u.indirect.section;
11117 elf_shdrp = elf_elfsections (s->owner);
11118 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11119 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11120 /* PR 290:
11121 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11122 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11123 sh_info fields. Hence we could get the situation
11124 where elfsec is 0. */
11125 if (elfsec == 0)
11126 {
11127 const struct elf_backend_data *bed
11128 = get_elf_backend_data (s->owner);
11129 if (bed->link_order_error_handler)
d003868e 11130 bed->link_order_error_handler
695344c0 11131 /* xgettext:c-format */
d003868e 11132 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11133 return 0;
11134 }
11135 else
11136 {
11137 s = elf_shdrp[elfsec]->bfd_section;
11138 return s->output_section->vma + s->output_offset;
11139 }
0b52efa6
PB
11140}
11141
11142
11143/* Compare two sections based on the locations of the sections they are
11144 linked to. Used by elf_fixup_link_order. */
11145
11146static int
11147compare_link_order (const void * a, const void * b)
11148{
11149 bfd_vma apos;
11150 bfd_vma bpos;
11151
11152 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11153 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11154 if (apos < bpos)
11155 return -1;
11156 return apos > bpos;
11157}
11158
11159
11160/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11161 order as their linked sections. Returns false if this could not be done
11162 because an output section includes both ordered and unordered
11163 sections. Ideally we'd do this in the linker proper. */
11164
11165static bfd_boolean
11166elf_fixup_link_order (bfd *abfd, asection *o)
11167{
11168 int seen_linkorder;
11169 int seen_other;
11170 int n;
11171 struct bfd_link_order *p;
11172 bfd *sub;
11173 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11174 unsigned elfsec;
0b52efa6 11175 struct bfd_link_order **sections;
d33cdfe3 11176 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11177 bfd_vma offset;
3b36f7e6 11178
d33cdfe3
L
11179 other_sec = NULL;
11180 linkorder_sec = NULL;
0b52efa6
PB
11181 seen_other = 0;
11182 seen_linkorder = 0;
8423293d 11183 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11184 {
d33cdfe3 11185 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11186 {
11187 s = p->u.indirect.section;
d33cdfe3
L
11188 sub = s->owner;
11189 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11190 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11191 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11192 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11193 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11194 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11195 {
11196 seen_linkorder++;
11197 linkorder_sec = s;
11198 }
0b52efa6 11199 else
d33cdfe3
L
11200 {
11201 seen_other++;
11202 other_sec = s;
11203 }
0b52efa6
PB
11204 }
11205 else
11206 seen_other++;
d33cdfe3
L
11207
11208 if (seen_other && seen_linkorder)
11209 {
11210 if (other_sec && linkorder_sec)
4eca0228 11211 _bfd_error_handler
695344c0 11212 /* xgettext:c-format */
4eca0228
AM
11213 (_("%A has both ordered [`%A' in %B] "
11214 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11215 o, linkorder_sec, linkorder_sec->owner,
11216 other_sec, other_sec->owner);
d33cdfe3 11217 else
4eca0228
AM
11218 _bfd_error_handler
11219 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11220 bfd_set_error (bfd_error_bad_value);
11221 return FALSE;
11222 }
0b52efa6
PB
11223 }
11224
11225 if (!seen_linkorder)
11226 return TRUE;
11227
0b52efa6 11228 sections = (struct bfd_link_order **)
14b1c01e
AM
11229 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11230 if (sections == NULL)
11231 return FALSE;
0b52efa6 11232 seen_linkorder = 0;
3b36f7e6 11233
8423293d 11234 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11235 {
11236 sections[seen_linkorder++] = p;
11237 }
11238 /* Sort the input sections in the order of their linked section. */
11239 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11240 compare_link_order);
11241
11242 /* Change the offsets of the sections. */
11243 offset = 0;
11244 for (n = 0; n < seen_linkorder; n++)
11245 {
11246 s = sections[n]->u.indirect.section;
461686a3 11247 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11248 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11249 sections[n]->offset = offset;
11250 offset += sections[n]->size;
11251 }
11252
4dd07732 11253 free (sections);
0b52efa6
PB
11254 return TRUE;
11255}
11256
76359541
TP
11257/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11258 Returns TRUE upon success, FALSE otherwise. */
11259
11260static bfd_boolean
11261elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11262{
11263 bfd_boolean ret = FALSE;
11264 bfd *implib_bfd;
11265 const struct elf_backend_data *bed;
11266 flagword flags;
11267 enum bfd_architecture arch;
11268 unsigned int mach;
11269 asymbol **sympp = NULL;
11270 long symsize;
11271 long symcount;
11272 long src_count;
11273 elf_symbol_type *osymbuf;
11274
11275 implib_bfd = info->out_implib_bfd;
11276 bed = get_elf_backend_data (abfd);
11277
11278 if (!bfd_set_format (implib_bfd, bfd_object))
11279 return FALSE;
11280
046734ff 11281 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11282 flags = bfd_get_file_flags (abfd);
11283 flags &= ~HAS_RELOC;
11284 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11285 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11286 return FALSE;
11287
11288 /* Copy architecture of output file to import library file. */
11289 arch = bfd_get_arch (abfd);
11290 mach = bfd_get_mach (abfd);
11291 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11292 && (abfd->target_defaulted
11293 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11294 return FALSE;
11295
11296 /* Get symbol table size. */
11297 symsize = bfd_get_symtab_upper_bound (abfd);
11298 if (symsize < 0)
11299 return FALSE;
11300
11301 /* Read in the symbol table. */
11302 sympp = (asymbol **) xmalloc (symsize);
11303 symcount = bfd_canonicalize_symtab (abfd, sympp);
11304 if (symcount < 0)
11305 goto free_sym_buf;
11306
11307 /* Allow the BFD backend to copy any private header data it
11308 understands from the output BFD to the import library BFD. */
11309 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11310 goto free_sym_buf;
11311
11312 /* Filter symbols to appear in the import library. */
11313 if (bed->elf_backend_filter_implib_symbols)
11314 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11315 symcount);
11316 else
11317 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11318 if (symcount == 0)
11319 {
5df1bc57 11320 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11321 _bfd_error_handler (_("%B: no symbol found for import library"),
11322 implib_bfd);
76359541
TP
11323 goto free_sym_buf;
11324 }
11325
11326
11327 /* Make symbols absolute. */
11328 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11329 sizeof (*osymbuf));
11330 for (src_count = 0; src_count < symcount; src_count++)
11331 {
11332 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11333 sizeof (*osymbuf));
11334 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11335 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11336 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11337 osymbuf[src_count].internal_elf_sym.st_value =
11338 osymbuf[src_count].symbol.value;
11339 sympp[src_count] = &osymbuf[src_count].symbol;
11340 }
11341
11342 bfd_set_symtab (implib_bfd, sympp, symcount);
11343
11344 /* Allow the BFD backend to copy any private data it understands
11345 from the output BFD to the import library BFD. This is done last
11346 to permit the routine to look at the filtered symbol table. */
11347 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11348 goto free_sym_buf;
11349
11350 if (!bfd_close (implib_bfd))
11351 goto free_sym_buf;
11352
11353 ret = TRUE;
11354
11355free_sym_buf:
11356 free (sympp);
11357 return ret;
11358}
11359
9f7c3e5e
AM
11360static void
11361elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11362{
11363 asection *o;
11364
11365 if (flinfo->symstrtab != NULL)
ef10c3ac 11366 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11367 if (flinfo->contents != NULL)
11368 free (flinfo->contents);
11369 if (flinfo->external_relocs != NULL)
11370 free (flinfo->external_relocs);
11371 if (flinfo->internal_relocs != NULL)
11372 free (flinfo->internal_relocs);
11373 if (flinfo->external_syms != NULL)
11374 free (flinfo->external_syms);
11375 if (flinfo->locsym_shndx != NULL)
11376 free (flinfo->locsym_shndx);
11377 if (flinfo->internal_syms != NULL)
11378 free (flinfo->internal_syms);
11379 if (flinfo->indices != NULL)
11380 free (flinfo->indices);
11381 if (flinfo->sections != NULL)
11382 free (flinfo->sections);
9f7c3e5e
AM
11383 if (flinfo->symshndxbuf != NULL)
11384 free (flinfo->symshndxbuf);
11385 for (o = obfd->sections; o != NULL; o = o->next)
11386 {
11387 struct bfd_elf_section_data *esdo = elf_section_data (o);
11388 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11389 free (esdo->rel.hashes);
11390 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11391 free (esdo->rela.hashes);
11392 }
11393}
0b52efa6 11394
c152c796
AM
11395/* Do the final step of an ELF link. */
11396
11397bfd_boolean
11398bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11399{
11400 bfd_boolean dynamic;
11401 bfd_boolean emit_relocs;
11402 bfd *dynobj;
8b127cbc 11403 struct elf_final_link_info flinfo;
91d6fa6a
NC
11404 asection *o;
11405 struct bfd_link_order *p;
11406 bfd *sub;
c152c796
AM
11407 bfd_size_type max_contents_size;
11408 bfd_size_type max_external_reloc_size;
11409 bfd_size_type max_internal_reloc_count;
11410 bfd_size_type max_sym_count;
11411 bfd_size_type max_sym_shndx_count;
c152c796
AM
11412 Elf_Internal_Sym elfsym;
11413 unsigned int i;
11414 Elf_Internal_Shdr *symtab_hdr;
11415 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11416 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11417 struct elf_outext_info eoinfo;
11418 bfd_boolean merged;
11419 size_t relativecount = 0;
11420 asection *reldyn = 0;
11421 bfd_size_type amt;
104d59d1
JM
11422 asection *attr_section = NULL;
11423 bfd_vma attr_size = 0;
11424 const char *std_attrs_section;
64f52338 11425 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11426
64f52338 11427 if (!is_elf_hash_table (htab))
c152c796
AM
11428 return FALSE;
11429
0e1862bb 11430 if (bfd_link_pic (info))
c152c796
AM
11431 abfd->flags |= DYNAMIC;
11432
64f52338
AM
11433 dynamic = htab->dynamic_sections_created;
11434 dynobj = htab->dynobj;
c152c796 11435
0e1862bb 11436 emit_relocs = (bfd_link_relocatable (info)
a4676736 11437 || info->emitrelocations);
c152c796 11438
8b127cbc
AM
11439 flinfo.info = info;
11440 flinfo.output_bfd = abfd;
ef10c3ac 11441 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11442 if (flinfo.symstrtab == NULL)
c152c796
AM
11443 return FALSE;
11444
11445 if (! dynamic)
11446 {
8b127cbc
AM
11447 flinfo.hash_sec = NULL;
11448 flinfo.symver_sec = NULL;
c152c796
AM
11449 }
11450 else
11451 {
3d4d4302 11452 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11453 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11454 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11455 /* Note that it is OK if symver_sec is NULL. */
11456 }
11457
8b127cbc
AM
11458 flinfo.contents = NULL;
11459 flinfo.external_relocs = NULL;
11460 flinfo.internal_relocs = NULL;
11461 flinfo.external_syms = NULL;
11462 flinfo.locsym_shndx = NULL;
11463 flinfo.internal_syms = NULL;
11464 flinfo.indices = NULL;
11465 flinfo.sections = NULL;
8b127cbc 11466 flinfo.symshndxbuf = NULL;
ffbc01cc 11467 flinfo.filesym_count = 0;
c152c796 11468
104d59d1
JM
11469 /* The object attributes have been merged. Remove the input
11470 sections from the link, and set the contents of the output
11471 secton. */
11472 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11473 for (o = abfd->sections; o != NULL; o = o->next)
11474 {
11475 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11476 || strcmp (o->name, ".gnu.attributes") == 0)
11477 {
11478 for (p = o->map_head.link_order; p != NULL; p = p->next)
11479 {
11480 asection *input_section;
11481
11482 if (p->type != bfd_indirect_link_order)
11483 continue;
11484 input_section = p->u.indirect.section;
11485 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11486 elf_link_input_bfd ignores this section. */
11487 input_section->flags &= ~SEC_HAS_CONTENTS;
11488 }
a0c8462f 11489
104d59d1
JM
11490 attr_size = bfd_elf_obj_attr_size (abfd);
11491 if (attr_size)
11492 {
11493 bfd_set_section_size (abfd, o, attr_size);
11494 attr_section = o;
11495 /* Skip this section later on. */
11496 o->map_head.link_order = NULL;
11497 }
11498 else
11499 o->flags |= SEC_EXCLUDE;
11500 }
11501 }
11502
c152c796
AM
11503 /* Count up the number of relocations we will output for each output
11504 section, so that we know the sizes of the reloc sections. We
11505 also figure out some maximum sizes. */
11506 max_contents_size = 0;
11507 max_external_reloc_size = 0;
11508 max_internal_reloc_count = 0;
11509 max_sym_count = 0;
11510 max_sym_shndx_count = 0;
11511 merged = FALSE;
11512 for (o = abfd->sections; o != NULL; o = o->next)
11513 {
11514 struct bfd_elf_section_data *esdo = elf_section_data (o);
11515 o->reloc_count = 0;
11516
8423293d 11517 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11518 {
11519 unsigned int reloc_count = 0;
9eaff861 11520 unsigned int additional_reloc_count = 0;
c152c796 11521 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11522
11523 if (p->type == bfd_section_reloc_link_order
11524 || p->type == bfd_symbol_reloc_link_order)
11525 reloc_count = 1;
11526 else if (p->type == bfd_indirect_link_order)
11527 {
11528 asection *sec;
11529
11530 sec = p->u.indirect.section;
c152c796
AM
11531
11532 /* Mark all sections which are to be included in the
11533 link. This will normally be every section. We need
11534 to do this so that we can identify any sections which
11535 the linker has decided to not include. */
11536 sec->linker_mark = TRUE;
11537
11538 if (sec->flags & SEC_MERGE)
11539 merged = TRUE;
11540
eea6121a
AM
11541 if (sec->rawsize > max_contents_size)
11542 max_contents_size = sec->rawsize;
11543 if (sec->size > max_contents_size)
11544 max_contents_size = sec->size;
c152c796 11545
c152c796
AM
11546 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11547 && (sec->owner->flags & DYNAMIC) == 0)
11548 {
11549 size_t sym_count;
11550
a961cdd5
AM
11551 /* We are interested in just local symbols, not all
11552 symbols. */
c152c796
AM
11553 if (elf_bad_symtab (sec->owner))
11554 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11555 / bed->s->sizeof_sym);
11556 else
11557 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11558
11559 if (sym_count > max_sym_count)
11560 max_sym_count = sym_count;
11561
11562 if (sym_count > max_sym_shndx_count
6a40cf0c 11563 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11564 max_sym_shndx_count = sym_count;
11565
a961cdd5
AM
11566 if (esdo->this_hdr.sh_type == SHT_REL
11567 || esdo->this_hdr.sh_type == SHT_RELA)
11568 /* Some backends use reloc_count in relocation sections
11569 to count particular types of relocs. Of course,
11570 reloc sections themselves can't have relocations. */
11571 ;
11572 else if (emit_relocs)
11573 {
11574 reloc_count = sec->reloc_count;
11575 if (bed->elf_backend_count_additional_relocs)
11576 {
11577 int c;
11578 c = (*bed->elf_backend_count_additional_relocs) (sec);
11579 additional_reloc_count += c;
11580 }
11581 }
11582 else if (bed->elf_backend_count_relocs)
11583 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11584
11585 esdi = elf_section_data (sec);
11586
c152c796
AM
11587 if ((sec->flags & SEC_RELOC) != 0)
11588 {
d4730f92 11589 size_t ext_size = 0;
c152c796 11590
d4730f92
BS
11591 if (esdi->rel.hdr != NULL)
11592 ext_size = esdi->rel.hdr->sh_size;
11593 if (esdi->rela.hdr != NULL)
11594 ext_size += esdi->rela.hdr->sh_size;
7326c758 11595
c152c796
AM
11596 if (ext_size > max_external_reloc_size)
11597 max_external_reloc_size = ext_size;
11598 if (sec->reloc_count > max_internal_reloc_count)
11599 max_internal_reloc_count = sec->reloc_count;
11600 }
11601 }
11602 }
11603
11604 if (reloc_count == 0)
11605 continue;
11606
9eaff861 11607 reloc_count += additional_reloc_count;
c152c796
AM
11608 o->reloc_count += reloc_count;
11609
0e1862bb 11610 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11611 {
d4730f92 11612 if (esdi->rel.hdr)
9eaff861 11613 {
491d01d3 11614 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11615 esdo->rel.count += additional_reloc_count;
11616 }
d4730f92 11617 if (esdi->rela.hdr)
9eaff861 11618 {
491d01d3 11619 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11620 esdo->rela.count += additional_reloc_count;
11621 }
d4730f92
BS
11622 }
11623 else
11624 {
11625 if (o->use_rela_p)
11626 esdo->rela.count += reloc_count;
2c2b4ed4 11627 else
d4730f92 11628 esdo->rel.count += reloc_count;
c152c796 11629 }
c152c796
AM
11630 }
11631
9eaff861 11632 if (o->reloc_count > 0)
c152c796
AM
11633 o->flags |= SEC_RELOC;
11634 else
11635 {
11636 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11637 set it (this is probably a bug) and if it is set
11638 assign_section_numbers will create a reloc section. */
11639 o->flags &=~ SEC_RELOC;
11640 }
11641
11642 /* If the SEC_ALLOC flag is not set, force the section VMA to
11643 zero. This is done in elf_fake_sections as well, but forcing
11644 the VMA to 0 here will ensure that relocs against these
11645 sections are handled correctly. */
11646 if ((o->flags & SEC_ALLOC) == 0
11647 && ! o->user_set_vma)
11648 o->vma = 0;
11649 }
11650
0e1862bb 11651 if (! bfd_link_relocatable (info) && merged)
64f52338 11652 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11653
11654 /* Figure out the file positions for everything but the symbol table
11655 and the relocs. We set symcount to force assign_section_numbers
11656 to create a symbol table. */
8539e4e8 11657 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11658 BFD_ASSERT (! abfd->output_has_begun);
11659 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11660 goto error_return;
11661
ee75fd95 11662 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11663 for (o = abfd->sections; o != NULL; o = o->next)
11664 {
d4730f92 11665 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11666 if ((o->flags & SEC_RELOC) != 0)
11667 {
d4730f92 11668 if (esdo->rel.hdr
9eaff861 11669 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11670 goto error_return;
11671
d4730f92 11672 if (esdo->rela.hdr
9eaff861 11673 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11674 goto error_return;
11675 }
11676
11677 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11678 to count upwards while actually outputting the relocations. */
d4730f92
BS
11679 esdo->rel.count = 0;
11680 esdo->rela.count = 0;
0ce398f1
L
11681
11682 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11683 {
11684 /* Cache the section contents so that they can be compressed
11685 later. Use bfd_malloc since it will be freed by
11686 bfd_compress_section_contents. */
11687 unsigned char *contents = esdo->this_hdr.contents;
11688 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11689 abort ();
11690 contents
11691 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11692 if (contents == NULL)
11693 goto error_return;
11694 esdo->this_hdr.contents = contents;
11695 }
c152c796
AM
11696 }
11697
c152c796 11698 /* We have now assigned file positions for all the sections except
a485e98e
AM
11699 .symtab, .strtab, and non-loaded reloc sections. We start the
11700 .symtab section at the current file position, and write directly
11701 to it. We build the .strtab section in memory. */
c152c796
AM
11702 bfd_get_symcount (abfd) = 0;
11703 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11704 /* sh_name is set in prep_headers. */
11705 symtab_hdr->sh_type = SHT_SYMTAB;
11706 /* sh_flags, sh_addr and sh_size all start off zero. */
11707 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11708 /* sh_link is set in assign_section_numbers. */
11709 /* sh_info is set below. */
11710 /* sh_offset is set just below. */
72de5009 11711 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11712
ef10c3ac
L
11713 if (max_sym_count < 20)
11714 max_sym_count = 20;
64f52338 11715 htab->strtabsize = max_sym_count;
ef10c3ac 11716 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11717 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11718 if (htab->strtab == NULL)
c152c796 11719 goto error_return;
ef10c3ac
L
11720 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11721 flinfo.symshndxbuf
11722 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11723 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11724
8539e4e8 11725 if (info->strip != strip_all || emit_relocs)
c152c796 11726 {
8539e4e8
AM
11727 file_ptr off = elf_next_file_pos (abfd);
11728
11729 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11730
11731 /* Note that at this point elf_next_file_pos (abfd) is
11732 incorrect. We do not yet know the size of the .symtab section.
11733 We correct next_file_pos below, after we do know the size. */
11734
11735 /* Start writing out the symbol table. The first symbol is always a
11736 dummy symbol. */
c152c796
AM
11737 elfsym.st_value = 0;
11738 elfsym.st_size = 0;
11739 elfsym.st_info = 0;
11740 elfsym.st_other = 0;
11741 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11742 elfsym.st_target_internal = 0;
ef10c3ac
L
11743 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11744 bfd_und_section_ptr, NULL) != 1)
c152c796 11745 goto error_return;
c152c796 11746
8539e4e8
AM
11747 /* Output a symbol for each section. We output these even if we are
11748 discarding local symbols, since they are used for relocs. These
11749 symbols have no names. We store the index of each one in the
11750 index field of the section, so that we can find it again when
11751 outputting relocs. */
11752
c152c796
AM
11753 elfsym.st_size = 0;
11754 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11755 elfsym.st_other = 0;
f0b5bb34 11756 elfsym.st_value = 0;
35fc36a8 11757 elfsym.st_target_internal = 0;
c152c796
AM
11758 for (i = 1; i < elf_numsections (abfd); i++)
11759 {
11760 o = bfd_section_from_elf_index (abfd, i);
11761 if (o != NULL)
f0b5bb34
AM
11762 {
11763 o->target_index = bfd_get_symcount (abfd);
11764 elfsym.st_shndx = i;
0e1862bb 11765 if (!bfd_link_relocatable (info))
f0b5bb34 11766 elfsym.st_value = o->vma;
ef10c3ac
L
11767 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11768 NULL) != 1)
f0b5bb34
AM
11769 goto error_return;
11770 }
c152c796
AM
11771 }
11772 }
11773
11774 /* Allocate some memory to hold information read in from the input
11775 files. */
11776 if (max_contents_size != 0)
11777 {
8b127cbc
AM
11778 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11779 if (flinfo.contents == NULL)
c152c796
AM
11780 goto error_return;
11781 }
11782
11783 if (max_external_reloc_size != 0)
11784 {
8b127cbc
AM
11785 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11786 if (flinfo.external_relocs == NULL)
c152c796
AM
11787 goto error_return;
11788 }
11789
11790 if (max_internal_reloc_count != 0)
11791 {
11792 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11793 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11794 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11795 if (flinfo.internal_relocs == NULL)
c152c796
AM
11796 goto error_return;
11797 }
11798
11799 if (max_sym_count != 0)
11800 {
11801 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11802 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11803 if (flinfo.external_syms == NULL)
c152c796
AM
11804 goto error_return;
11805
11806 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11807 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11808 if (flinfo.internal_syms == NULL)
c152c796
AM
11809 goto error_return;
11810
11811 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11812 flinfo.indices = (long int *) bfd_malloc (amt);
11813 if (flinfo.indices == NULL)
c152c796
AM
11814 goto error_return;
11815
11816 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11817 flinfo.sections = (asection **) bfd_malloc (amt);
11818 if (flinfo.sections == NULL)
c152c796
AM
11819 goto error_return;
11820 }
11821
11822 if (max_sym_shndx_count != 0)
11823 {
11824 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11825 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11826 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11827 goto error_return;
11828 }
11829
64f52338 11830 if (htab->tls_sec)
c152c796
AM
11831 {
11832 bfd_vma base, end = 0;
11833 asection *sec;
11834
64f52338 11835 for (sec = htab->tls_sec;
c152c796
AM
11836 sec && (sec->flags & SEC_THREAD_LOCAL);
11837 sec = sec->next)
11838 {
3a800eb9 11839 bfd_size_type size = sec->size;
c152c796 11840
3a800eb9
AM
11841 if (size == 0
11842 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11843 {
91d6fa6a
NC
11844 struct bfd_link_order *ord = sec->map_tail.link_order;
11845
11846 if (ord != NULL)
11847 size = ord->offset + ord->size;
c152c796
AM
11848 }
11849 end = sec->vma + size;
11850 }
64f52338 11851 base = htab->tls_sec->vma;
7dc98aea
RO
11852 /* Only align end of TLS section if static TLS doesn't have special
11853 alignment requirements. */
11854 if (bed->static_tls_alignment == 1)
64f52338
AM
11855 end = align_power (end, htab->tls_sec->alignment_power);
11856 htab->tls_size = end - base;
c152c796
AM
11857 }
11858
0b52efa6
PB
11859 /* Reorder SHF_LINK_ORDER sections. */
11860 for (o = abfd->sections; o != NULL; o = o->next)
11861 {
11862 if (!elf_fixup_link_order (abfd, o))
11863 return FALSE;
11864 }
11865
2f0c68f2
CM
11866 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11867 return FALSE;
11868
c152c796
AM
11869 /* Since ELF permits relocations to be against local symbols, we
11870 must have the local symbols available when we do the relocations.
11871 Since we would rather only read the local symbols once, and we
11872 would rather not keep them in memory, we handle all the
11873 relocations for a single input file at the same time.
11874
11875 Unfortunately, there is no way to know the total number of local
11876 symbols until we have seen all of them, and the local symbol
11877 indices precede the global symbol indices. This means that when
11878 we are generating relocatable output, and we see a reloc against
11879 a global symbol, we can not know the symbol index until we have
11880 finished examining all the local symbols to see which ones we are
11881 going to output. To deal with this, we keep the relocations in
11882 memory, and don't output them until the end of the link. This is
11883 an unfortunate waste of memory, but I don't see a good way around
11884 it. Fortunately, it only happens when performing a relocatable
11885 link, which is not the common case. FIXME: If keep_memory is set
11886 we could write the relocs out and then read them again; I don't
11887 know how bad the memory loss will be. */
11888
c72f2fb2 11889 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11890 sub->output_has_begun = FALSE;
11891 for (o = abfd->sections; o != NULL; o = o->next)
11892 {
8423293d 11893 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11894 {
11895 if (p->type == bfd_indirect_link_order
11896 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11897 == bfd_target_elf_flavour)
11898 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11899 {
11900 if (! sub->output_has_begun)
11901 {
8b127cbc 11902 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11903 goto error_return;
11904 sub->output_has_begun = TRUE;
11905 }
11906 }
11907 else if (p->type == bfd_section_reloc_link_order
11908 || p->type == bfd_symbol_reloc_link_order)
11909 {
11910 if (! elf_reloc_link_order (abfd, info, o, p))
11911 goto error_return;
11912 }
11913 else
11914 {
11915 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11916 {
11917 if (p->type == bfd_indirect_link_order
11918 && (bfd_get_flavour (sub)
11919 == bfd_target_elf_flavour)
11920 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11921 != bed->s->elfclass))
11922 {
11923 const char *iclass, *oclass;
11924
aebf9be7 11925 switch (bed->s->elfclass)
351f65ca 11926 {
aebf9be7
NC
11927 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11928 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11929 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11930 default: abort ();
351f65ca 11931 }
aebf9be7
NC
11932
11933 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11934 {
aebf9be7
NC
11935 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11936 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11937 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11938 default: abort ();
351f65ca
L
11939 }
11940
11941 bfd_set_error (bfd_error_wrong_format);
4eca0228 11942 _bfd_error_handler
695344c0 11943 /* xgettext:c-format */
351f65ca
L
11944 (_("%B: file class %s incompatible with %s"),
11945 sub, iclass, oclass);
11946 }
11947
11948 goto error_return;
11949 }
c152c796
AM
11950 }
11951 }
11952 }
11953
c0f00686
L
11954 /* Free symbol buffer if needed. */
11955 if (!info->reduce_memory_overheads)
11956 {
c72f2fb2 11957 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11958 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11959 && elf_tdata (sub)->symbuf)
c0f00686
L
11960 {
11961 free (elf_tdata (sub)->symbuf);
11962 elf_tdata (sub)->symbuf = NULL;
11963 }
11964 }
11965
c152c796
AM
11966 /* Output any global symbols that got converted to local in a
11967 version script or due to symbol visibility. We do this in a
11968 separate step since ELF requires all local symbols to appear
11969 prior to any global symbols. FIXME: We should only do this if
11970 some global symbols were, in fact, converted to become local.
11971 FIXME: Will this work correctly with the Irix 5 linker? */
11972 eoinfo.failed = FALSE;
8b127cbc 11973 eoinfo.flinfo = &flinfo;
c152c796 11974 eoinfo.localsyms = TRUE;
34a79995 11975 eoinfo.file_sym_done = FALSE;
7686d77d 11976 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11977 if (eoinfo.failed)
11978 return FALSE;
11979
4e617b1e
PB
11980 /* If backend needs to output some local symbols not present in the hash
11981 table, do it now. */
8539e4e8
AM
11982 if (bed->elf_backend_output_arch_local_syms
11983 && (info->strip != strip_all || emit_relocs))
4e617b1e 11984 {
6e0b88f1 11985 typedef int (*out_sym_func)
4e617b1e
PB
11986 (void *, const char *, Elf_Internal_Sym *, asection *,
11987 struct elf_link_hash_entry *);
11988
11989 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11990 (abfd, info, &flinfo,
11991 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11992 return FALSE;
11993 }
11994
c152c796
AM
11995 /* That wrote out all the local symbols. Finish up the symbol table
11996 with the global symbols. Even if we want to strip everything we
11997 can, we still need to deal with those global symbols that got
11998 converted to local in a version script. */
11999
12000 /* The sh_info field records the index of the first non local symbol. */
12001 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12002
12003 if (dynamic
64f52338
AM
12004 && htab->dynsym != NULL
12005 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12006 {
12007 Elf_Internal_Sym sym;
64f52338 12008 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12009
64f52338
AM
12010 o = htab->dynsym->output_section;
12011 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12012
12013 /* Write out the section symbols for the output sections. */
0e1862bb 12014 if (bfd_link_pic (info)
64f52338 12015 || htab->is_relocatable_executable)
c152c796
AM
12016 {
12017 asection *s;
12018
12019 sym.st_size = 0;
12020 sym.st_name = 0;
12021 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12022 sym.st_other = 0;
35fc36a8 12023 sym.st_target_internal = 0;
c152c796
AM
12024
12025 for (s = abfd->sections; s != NULL; s = s->next)
12026 {
12027 int indx;
12028 bfd_byte *dest;
12029 long dynindx;
12030
c152c796 12031 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12032 if (dynindx <= 0)
12033 continue;
12034 indx = elf_section_data (s)->this_idx;
c152c796
AM
12035 BFD_ASSERT (indx > 0);
12036 sym.st_shndx = indx;
c0d5a53d
L
12037 if (! check_dynsym (abfd, &sym))
12038 return FALSE;
c152c796
AM
12039 sym.st_value = s->vma;
12040 dest = dynsym + dynindx * bed->s->sizeof_sym;
12041 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12042 }
c152c796
AM
12043 }
12044
12045 /* Write out the local dynsyms. */
64f52338 12046 if (htab->dynlocal)
c152c796
AM
12047 {
12048 struct elf_link_local_dynamic_entry *e;
64f52338 12049 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12050 {
12051 asection *s;
12052 bfd_byte *dest;
12053
935bd1e0 12054 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12055 Note that we saved a word of storage and overwrote
12056 the original st_name with the dynstr_index. */
12057 sym = e->isym;
935bd1e0 12058 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12059
cb33740c
AM
12060 s = bfd_section_from_elf_index (e->input_bfd,
12061 e->isym.st_shndx);
12062 if (s != NULL)
c152c796 12063 {
c152c796
AM
12064 sym.st_shndx =
12065 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12066 if (! check_dynsym (abfd, &sym))
12067 return FALSE;
c152c796
AM
12068 sym.st_value = (s->output_section->vma
12069 + s->output_offset
12070 + e->isym.st_value);
12071 }
12072
c152c796
AM
12073 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12074 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12075 }
12076 }
c152c796
AM
12077 }
12078
12079 /* We get the global symbols from the hash table. */
12080 eoinfo.failed = FALSE;
12081 eoinfo.localsyms = FALSE;
8b127cbc 12082 eoinfo.flinfo = &flinfo;
7686d77d 12083 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12084 if (eoinfo.failed)
12085 return FALSE;
12086
12087 /* If backend needs to output some symbols not present in the hash
12088 table, do it now. */
8539e4e8
AM
12089 if (bed->elf_backend_output_arch_syms
12090 && (info->strip != strip_all || emit_relocs))
c152c796 12091 {
6e0b88f1 12092 typedef int (*out_sym_func)
c152c796
AM
12093 (void *, const char *, Elf_Internal_Sym *, asection *,
12094 struct elf_link_hash_entry *);
12095
12096 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12097 (abfd, info, &flinfo,
12098 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12099 return FALSE;
12100 }
12101
ef10c3ac
L
12102 /* Finalize the .strtab section. */
12103 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12104
12105 /* Swap out the .strtab section. */
12106 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12107 return FALSE;
12108
12109 /* Now we know the size of the symtab section. */
c152c796
AM
12110 if (bfd_get_symcount (abfd) > 0)
12111 {
ee3b52e9
L
12112 /* Finish up and write out the symbol string table (.strtab)
12113 section. */
ad32986f 12114 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12115 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12116
ad32986f 12117 if (elf_symtab_shndx_list (abfd))
8539e4e8 12118 {
ad32986f 12119 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12120
ad32986f
NC
12121 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12122 {
12123 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12124 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12125 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12126 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12127 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12128
ad32986f
NC
12129 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12130 off, TRUE);
12131
12132 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12133 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12134 return FALSE;
12135 }
8539e4e8 12136 }
ee3b52e9
L
12137
12138 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12139 /* sh_name was set in prep_headers. */
12140 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12141 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12142 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12143 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12144 symstrtab_hdr->sh_entsize = 0;
12145 symstrtab_hdr->sh_link = 0;
12146 symstrtab_hdr->sh_info = 0;
12147 /* sh_offset is set just below. */
12148 symstrtab_hdr->sh_addralign = 1;
12149
12150 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12151 off, TRUE);
12152 elf_next_file_pos (abfd) = off;
12153
c152c796 12154 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12155 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12156 return FALSE;
12157 }
12158
76359541
TP
12159 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12160 {
4eca0228
AM
12161 _bfd_error_handler (_("%B: failed to generate import library"),
12162 info->out_implib_bfd);
76359541
TP
12163 return FALSE;
12164 }
12165
c152c796
AM
12166 /* Adjust the relocs to have the correct symbol indices. */
12167 for (o = abfd->sections; o != NULL; o = o->next)
12168 {
d4730f92 12169 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12170 bfd_boolean sort;
c152c796
AM
12171 if ((o->flags & SEC_RELOC) == 0)
12172 continue;
12173
28dbcedc 12174 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12175 if (esdo->rel.hdr != NULL
9eaff861 12176 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
12177 return FALSE;
12178 if (esdo->rela.hdr != NULL
9eaff861 12179 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 12180 return FALSE;
c152c796
AM
12181
12182 /* Set the reloc_count field to 0 to prevent write_relocs from
12183 trying to swap the relocs out itself. */
12184 o->reloc_count = 0;
12185 }
12186
12187 if (dynamic && info->combreloc && dynobj != NULL)
12188 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12189
12190 /* If we are linking against a dynamic object, or generating a
12191 shared library, finish up the dynamic linking information. */
12192 if (dynamic)
12193 {
12194 bfd_byte *dyncon, *dynconend;
12195
12196 /* Fix up .dynamic entries. */
3d4d4302 12197 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12198 BFD_ASSERT (o != NULL);
12199
12200 dyncon = o->contents;
eea6121a 12201 dynconend = o->contents + o->size;
c152c796
AM
12202 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12203 {
12204 Elf_Internal_Dyn dyn;
12205 const char *name;
12206 unsigned int type;
64487780
AM
12207 bfd_size_type sh_size;
12208 bfd_vma sh_addr;
c152c796
AM
12209
12210 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12211
12212 switch (dyn.d_tag)
12213 {
12214 default:
12215 continue;
12216 case DT_NULL:
12217 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12218 {
12219 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12220 {
12221 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12222 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12223 default: continue;
12224 }
12225 dyn.d_un.d_val = relativecount;
12226 relativecount = 0;
12227 break;
12228 }
12229 continue;
12230
12231 case DT_INIT:
12232 name = info->init_function;
12233 goto get_sym;
12234 case DT_FINI:
12235 name = info->fini_function;
12236 get_sym:
12237 {
12238 struct elf_link_hash_entry *h;
12239
64f52338 12240 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12241 if (h != NULL
12242 && (h->root.type == bfd_link_hash_defined
12243 || h->root.type == bfd_link_hash_defweak))
12244 {
bef26483 12245 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12246 o = h->root.u.def.section;
12247 if (o->output_section != NULL)
bef26483 12248 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12249 + o->output_offset);
12250 else
12251 {
12252 /* The symbol is imported from another shared
12253 library and does not apply to this one. */
bef26483 12254 dyn.d_un.d_ptr = 0;
c152c796
AM
12255 }
12256 break;
12257 }
12258 }
12259 continue;
12260
12261 case DT_PREINIT_ARRAYSZ:
12262 name = ".preinit_array";
4ade44b7 12263 goto get_out_size;
c152c796
AM
12264 case DT_INIT_ARRAYSZ:
12265 name = ".init_array";
4ade44b7 12266 goto get_out_size;
c152c796
AM
12267 case DT_FINI_ARRAYSZ:
12268 name = ".fini_array";
4ade44b7 12269 get_out_size:
c152c796
AM
12270 o = bfd_get_section_by_name (abfd, name);
12271 if (o == NULL)
12272 {
4eca0228 12273 _bfd_error_handler
4ade44b7 12274 (_("could not find section %s"), name);
c152c796
AM
12275 goto error_return;
12276 }
eea6121a 12277 if (o->size == 0)
4eca0228 12278 _bfd_error_handler
c152c796 12279 (_("warning: %s section has zero size"), name);
eea6121a 12280 dyn.d_un.d_val = o->size;
c152c796
AM
12281 break;
12282
12283 case DT_PREINIT_ARRAY:
12284 name = ".preinit_array";
4ade44b7 12285 goto get_out_vma;
c152c796
AM
12286 case DT_INIT_ARRAY:
12287 name = ".init_array";
4ade44b7 12288 goto get_out_vma;
c152c796
AM
12289 case DT_FINI_ARRAY:
12290 name = ".fini_array";
4ade44b7
AM
12291 get_out_vma:
12292 o = bfd_get_section_by_name (abfd, name);
12293 goto do_vma;
c152c796
AM
12294
12295 case DT_HASH:
12296 name = ".hash";
12297 goto get_vma;
fdc90cb4
JJ
12298 case DT_GNU_HASH:
12299 name = ".gnu.hash";
12300 goto get_vma;
c152c796
AM
12301 case DT_STRTAB:
12302 name = ".dynstr";
12303 goto get_vma;
12304 case DT_SYMTAB:
12305 name = ".dynsym";
12306 goto get_vma;
12307 case DT_VERDEF:
12308 name = ".gnu.version_d";
12309 goto get_vma;
12310 case DT_VERNEED:
12311 name = ".gnu.version_r";
12312 goto get_vma;
12313 case DT_VERSYM:
12314 name = ".gnu.version";
12315 get_vma:
4ade44b7
AM
12316 o = bfd_get_linker_section (dynobj, name);
12317 do_vma:
c152c796
AM
12318 if (o == NULL)
12319 {
4eca0228 12320 _bfd_error_handler
4ade44b7 12321 (_("could not find section %s"), name);
c152c796
AM
12322 goto error_return;
12323 }
894891db
NC
12324 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12325 {
4eca0228 12326 _bfd_error_handler
894891db
NC
12327 (_("warning: section '%s' is being made into a note"), name);
12328 bfd_set_error (bfd_error_nonrepresentable_section);
12329 goto error_return;
12330 }
4ade44b7 12331 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12332 break;
12333
12334 case DT_REL:
12335 case DT_RELA:
12336 case DT_RELSZ:
12337 case DT_RELASZ:
12338 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12339 type = SHT_REL;
12340 else
12341 type = SHT_RELA;
64487780
AM
12342 sh_size = 0;
12343 sh_addr = 0;
c152c796
AM
12344 for (i = 1; i < elf_numsections (abfd); i++)
12345 {
12346 Elf_Internal_Shdr *hdr;
12347
12348 hdr = elf_elfsections (abfd)[i];
12349 if (hdr->sh_type == type
12350 && (hdr->sh_flags & SHF_ALLOC) != 0)
12351 {
64487780
AM
12352 sh_size += hdr->sh_size;
12353 if (sh_addr == 0
12354 || sh_addr > hdr->sh_addr)
12355 sh_addr = hdr->sh_addr;
c152c796
AM
12356 }
12357 }
64487780 12358
64f52338
AM
12359 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12360 {
12361 /* Don't count procedure linkage table relocs in the
12362 overall reloc count. */
64487780
AM
12363 sh_size -= htab->srelplt->size;
12364 if (sh_size == 0)
12365 /* If the size is zero, make the address zero too.
12366 This is to avoid a glibc bug. If the backend
12367 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12368 zero, then we'll put DT_RELA at the end of
12369 DT_JMPREL. glibc will interpret the end of
12370 DT_RELA matching the end of DT_JMPREL as the
12371 case where DT_RELA includes DT_JMPREL, and for
12372 LD_BIND_NOW will decide that processing DT_RELA
12373 will process the PLT relocs too. Net result:
12374 No PLT relocs applied. */
12375 sh_addr = 0;
12376
64f52338
AM
12377 /* If .rela.plt is the first .rela section, exclude
12378 it from DT_RELA. */
64487780
AM
12379 else if (sh_addr == (htab->srelplt->output_section->vma
12380 + htab->srelplt->output_offset))
12381 sh_addr += htab->srelplt->size;
64f52338 12382 }
64487780
AM
12383
12384 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12385 dyn.d_un.d_val = sh_size;
12386 else
12387 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12388 break;
12389 }
12390 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12391 }
12392 }
12393
12394 /* If we have created any dynamic sections, then output them. */
12395 if (dynobj != NULL)
12396 {
12397 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12398 goto error_return;
12399
943284cc 12400 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12401 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12402 || info->error_textrel)
3d4d4302 12403 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12404 {
12405 bfd_byte *dyncon, *dynconend;
12406
943284cc
DJ
12407 dyncon = o->contents;
12408 dynconend = o->contents + o->size;
12409 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12410 {
12411 Elf_Internal_Dyn dyn;
12412
12413 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12414
12415 if (dyn.d_tag == DT_TEXTREL)
12416 {
c192a133
AM
12417 if (info->error_textrel)
12418 info->callbacks->einfo
12419 (_("%P%X: read-only segment has dynamic relocations.\n"));
12420 else
12421 info->callbacks->einfo
12422 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12423 break;
12424 }
12425 }
12426 }
12427
c152c796
AM
12428 for (o = dynobj->sections; o != NULL; o = o->next)
12429 {
12430 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12431 || o->size == 0
c152c796
AM
12432 || o->output_section == bfd_abs_section_ptr)
12433 continue;
12434 if ((o->flags & SEC_LINKER_CREATED) == 0)
12435 {
12436 /* At this point, we are only interested in sections
12437 created by _bfd_elf_link_create_dynamic_sections. */
12438 continue;
12439 }
64f52338 12440 if (htab->stab_info.stabstr == o)
3722b82f 12441 continue;
64f52338 12442 if (htab->eh_info.hdr_sec == o)
eea6121a 12443 continue;
3d4d4302 12444 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12445 {
12446 if (! bfd_set_section_contents (abfd, o->output_section,
12447 o->contents,
37b01f6a
DG
12448 (file_ptr) o->output_offset
12449 * bfd_octets_per_byte (abfd),
eea6121a 12450 o->size))
c152c796
AM
12451 goto error_return;
12452 }
12453 else
12454 {
12455 /* The contents of the .dynstr section are actually in a
12456 stringtab. */
8539e4e8
AM
12457 file_ptr off;
12458
c152c796
AM
12459 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12460 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12461 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12462 goto error_return;
12463 }
12464 }
12465 }
12466
0e1862bb 12467 if (bfd_link_relocatable (info))
c152c796
AM
12468 {
12469 bfd_boolean failed = FALSE;
12470
12471 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12472 if (failed)
12473 goto error_return;
12474 }
12475
12476 /* If we have optimized stabs strings, output them. */
64f52338 12477 if (htab->stab_info.stabstr != NULL)
c152c796 12478 {
64f52338 12479 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12480 goto error_return;
12481 }
12482
9f7c3e5e
AM
12483 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12484 goto error_return;
c152c796 12485
9f7c3e5e 12486 elf_final_link_free (abfd, &flinfo);
c152c796 12487
12bd6957 12488 elf_linker (abfd) = TRUE;
c152c796 12489
104d59d1
JM
12490 if (attr_section)
12491 {
a50b1753 12492 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12493 if (contents == NULL)
d0f16d5e 12494 return FALSE; /* Bail out and fail. */
104d59d1
JM
12495 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12496 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12497 free (contents);
12498 }
12499
c152c796
AM
12500 return TRUE;
12501
12502 error_return:
9f7c3e5e 12503 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12504 return FALSE;
12505}
12506\f
5241d853
RS
12507/* Initialize COOKIE for input bfd ABFD. */
12508
12509static bfd_boolean
12510init_reloc_cookie (struct elf_reloc_cookie *cookie,
12511 struct bfd_link_info *info, bfd *abfd)
12512{
12513 Elf_Internal_Shdr *symtab_hdr;
12514 const struct elf_backend_data *bed;
12515
12516 bed = get_elf_backend_data (abfd);
12517 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12518
12519 cookie->abfd = abfd;
12520 cookie->sym_hashes = elf_sym_hashes (abfd);
12521 cookie->bad_symtab = elf_bad_symtab (abfd);
12522 if (cookie->bad_symtab)
12523 {
12524 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12525 cookie->extsymoff = 0;
12526 }
12527 else
12528 {
12529 cookie->locsymcount = symtab_hdr->sh_info;
12530 cookie->extsymoff = symtab_hdr->sh_info;
12531 }
12532
12533 if (bed->s->arch_size == 32)
12534 cookie->r_sym_shift = 8;
12535 else
12536 cookie->r_sym_shift = 32;
12537
12538 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12539 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12540 {
12541 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12542 cookie->locsymcount, 0,
12543 NULL, NULL, NULL);
12544 if (cookie->locsyms == NULL)
12545 {
12546 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12547 return FALSE;
12548 }
12549 if (info->keep_memory)
12550 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12551 }
12552 return TRUE;
12553}
12554
12555/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12556
12557static void
12558fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12559{
12560 Elf_Internal_Shdr *symtab_hdr;
12561
12562 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12563 if (cookie->locsyms != NULL
12564 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12565 free (cookie->locsyms);
12566}
12567
12568/* Initialize the relocation information in COOKIE for input section SEC
12569 of input bfd ABFD. */
12570
12571static bfd_boolean
12572init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12573 struct bfd_link_info *info, bfd *abfd,
12574 asection *sec)
12575{
12576 const struct elf_backend_data *bed;
12577
12578 if (sec->reloc_count == 0)
12579 {
12580 cookie->rels = NULL;
12581 cookie->relend = NULL;
12582 }
12583 else
12584 {
12585 bed = get_elf_backend_data (abfd);
12586
12587 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12588 info->keep_memory);
12589 if (cookie->rels == NULL)
12590 return FALSE;
12591 cookie->rel = cookie->rels;
12592 cookie->relend = (cookie->rels
12593 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12594 }
12595 cookie->rel = cookie->rels;
12596 return TRUE;
12597}
12598
12599/* Free the memory allocated by init_reloc_cookie_rels,
12600 if appropriate. */
12601
12602static void
12603fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12604 asection *sec)
12605{
12606 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12607 free (cookie->rels);
12608}
12609
12610/* Initialize the whole of COOKIE for input section SEC. */
12611
12612static bfd_boolean
12613init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12614 struct bfd_link_info *info,
12615 asection *sec)
12616{
12617 if (!init_reloc_cookie (cookie, info, sec->owner))
12618 goto error1;
12619 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12620 goto error2;
12621 return TRUE;
12622
12623 error2:
12624 fini_reloc_cookie (cookie, sec->owner);
12625 error1:
12626 return FALSE;
12627}
12628
12629/* Free the memory allocated by init_reloc_cookie_for_section,
12630 if appropriate. */
12631
12632static void
12633fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12634 asection *sec)
12635{
12636 fini_reloc_cookie_rels (cookie, sec);
12637 fini_reloc_cookie (cookie, sec->owner);
12638}
12639\f
c152c796
AM
12640/* Garbage collect unused sections. */
12641
07adf181
AM
12642/* Default gc_mark_hook. */
12643
12644asection *
12645_bfd_elf_gc_mark_hook (asection *sec,
12646 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12647 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12648 struct elf_link_hash_entry *h,
12649 Elf_Internal_Sym *sym)
12650{
12651 if (h != NULL)
12652 {
12653 switch (h->root.type)
12654 {
12655 case bfd_link_hash_defined:
12656 case bfd_link_hash_defweak:
12657 return h->root.u.def.section;
12658
12659 case bfd_link_hash_common:
12660 return h->root.u.c.p->section;
12661
12662 default:
12663 break;
12664 }
12665 }
12666 else
12667 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12668
12669 return NULL;
12670}
12671
b7c871ed
L
12672/* Return the global debug definition section. */
12673
12674static asection *
12675elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12676 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12677 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12678 struct elf_link_hash_entry *h,
12679 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12680{
12681 if (h != NULL
12682 && (h->root.type == bfd_link_hash_defined
12683 || h->root.type == bfd_link_hash_defweak)
12684 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12685 return h->root.u.def.section;
12686
12687 return NULL;
12688}
12689
a6a4679f
AM
12690/* For undefined __start_<name> and __stop_<name> symbols, return the
12691 first input section matching <name>. Return NULL otherwise. */
12692
12693asection *
12694_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12695 struct elf_link_hash_entry *h)
12696{
12697 asection *s;
12698 const char *sec_name;
12699
12700 if (h->root.type != bfd_link_hash_undefined
12701 && h->root.type != bfd_link_hash_undefweak)
12702 return NULL;
12703
12704 s = h->root.u.undef.section;
12705 if (s != NULL)
12706 {
12707 if (s == (asection *) 0 - 1)
12708 return NULL;
12709 return s;
12710 }
12711
12712 sec_name = NULL;
12713 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12714 sec_name = h->root.root.string + 8;
12715 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12716 sec_name = h->root.root.string + 7;
12717
12718 if (sec_name != NULL && *sec_name != '\0')
12719 {
12720 bfd *i;
12721
12722 for (i = info->input_bfds; i != NULL; i = i->link.next)
12723 {
12724 s = bfd_get_section_by_name (i, sec_name);
12725 if (s != NULL)
12726 {
12727 h->root.u.undef.section = s;
12728 break;
12729 }
12730 }
12731 }
12732
12733 if (s == NULL)
12734 h->root.u.undef.section = (asection *) 0 - 1;
12735
12736 return s;
12737}
12738
5241d853
RS
12739/* COOKIE->rel describes a relocation against section SEC, which is
12740 a section we've decided to keep. Return the section that contains
12741 the relocation symbol, or NULL if no section contains it. */
12742
12743asection *
12744_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12745 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12746 struct elf_reloc_cookie *cookie,
12747 bfd_boolean *start_stop)
5241d853
RS
12748{
12749 unsigned long r_symndx;
12750 struct elf_link_hash_entry *h;
12751
12752 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12753 if (r_symndx == STN_UNDEF)
5241d853
RS
12754 return NULL;
12755
12756 if (r_symndx >= cookie->locsymcount
12757 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12758 {
12759 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12760 if (h == NULL)
12761 {
12762 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12763 sec->owner);
12764 return NULL;
12765 }
5241d853
RS
12766 while (h->root.type == bfd_link_hash_indirect
12767 || h->root.type == bfd_link_hash_warning)
12768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12769 h->mark = 1;
4e6b54a6
AM
12770 /* If this symbol is weak and there is a non-weak definition, we
12771 keep the non-weak definition because many backends put
12772 dynamic reloc info on the non-weak definition for code
12773 handling copy relocs. */
12774 if (h->u.weakdef != NULL)
12775 h->u.weakdef->mark = 1;
1cce69b9 12776
a6a4679f 12777 if (start_stop != NULL)
1cce69b9
AM
12778 {
12779 /* To work around a glibc bug, mark all XXX input sections
12780 when there is an as yet undefined reference to __start_XXX
12781 or __stop_XXX symbols. The linker will later define such
12782 symbols for orphan input sections that have a name
12783 representable as a C identifier. */
a6a4679f 12784 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12785
a6a4679f 12786 if (s != NULL)
1cce69b9 12787 {
a6a4679f
AM
12788 *start_stop = !s->gc_mark;
12789 return s;
1cce69b9
AM
12790 }
12791 }
12792
5241d853
RS
12793 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12794 }
12795
12796 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12797 &cookie->locsyms[r_symndx]);
12798}
12799
12800/* COOKIE->rel describes a relocation against section SEC, which is
12801 a section we've decided to keep. Mark the section that contains
9d0a14d3 12802 the relocation symbol. */
5241d853
RS
12803
12804bfd_boolean
12805_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12806 asection *sec,
12807 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12808 struct elf_reloc_cookie *cookie)
5241d853
RS
12809{
12810 asection *rsec;
1cce69b9 12811 bfd_boolean start_stop = FALSE;
5241d853 12812
1cce69b9
AM
12813 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12814 while (rsec != NULL)
5241d853 12815 {
1cce69b9
AM
12816 if (!rsec->gc_mark)
12817 {
12818 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12819 || (rsec->owner->flags & DYNAMIC) != 0)
12820 rsec->gc_mark = 1;
12821 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12822 return FALSE;
12823 }
12824 if (!start_stop)
12825 break;
199af150 12826 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12827 }
12828 return TRUE;
12829}
12830
07adf181
AM
12831/* The mark phase of garbage collection. For a given section, mark
12832 it and any sections in this section's group, and all the sections
12833 which define symbols to which it refers. */
12834
ccfa59ea
AM
12835bfd_boolean
12836_bfd_elf_gc_mark (struct bfd_link_info *info,
12837 asection *sec,
6a5bb875 12838 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12839{
12840 bfd_boolean ret;
9d0a14d3 12841 asection *group_sec, *eh_frame;
c152c796
AM
12842
12843 sec->gc_mark = 1;
12844
12845 /* Mark all the sections in the group. */
12846 group_sec = elf_section_data (sec)->next_in_group;
12847 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12848 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12849 return FALSE;
12850
12851 /* Look through the section relocs. */
12852 ret = TRUE;
9d0a14d3
RS
12853 eh_frame = elf_eh_frame_section (sec->owner);
12854 if ((sec->flags & SEC_RELOC) != 0
12855 && sec->reloc_count > 0
12856 && sec != eh_frame)
c152c796 12857 {
5241d853 12858 struct elf_reloc_cookie cookie;
c152c796 12859
5241d853
RS
12860 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12861 ret = FALSE;
c152c796 12862 else
c152c796 12863 {
5241d853 12864 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12865 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12866 {
12867 ret = FALSE;
12868 break;
12869 }
12870 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12871 }
12872 }
9d0a14d3
RS
12873
12874 if (ret && eh_frame && elf_fde_list (sec))
12875 {
12876 struct elf_reloc_cookie cookie;
12877
12878 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12879 ret = FALSE;
12880 else
12881 {
12882 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12883 gc_mark_hook, &cookie))
12884 ret = FALSE;
12885 fini_reloc_cookie_for_section (&cookie, eh_frame);
12886 }
12887 }
12888
2f0c68f2
CM
12889 eh_frame = elf_section_eh_frame_entry (sec);
12890 if (ret && eh_frame && !eh_frame->gc_mark)
12891 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12892 ret = FALSE;
12893
c152c796
AM
12894 return ret;
12895}
12896
3c758495
TG
12897/* Scan and mark sections in a special or debug section group. */
12898
12899static void
12900_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12901{
12902 /* Point to first section of section group. */
12903 asection *ssec;
12904 /* Used to iterate the section group. */
12905 asection *msec;
12906
12907 bfd_boolean is_special_grp = TRUE;
12908 bfd_boolean is_debug_grp = TRUE;
12909
12910 /* First scan to see if group contains any section other than debug
12911 and special section. */
12912 ssec = msec = elf_next_in_group (grp);
12913 do
12914 {
12915 if ((msec->flags & SEC_DEBUGGING) == 0)
12916 is_debug_grp = FALSE;
12917
12918 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12919 is_special_grp = FALSE;
12920
12921 msec = elf_next_in_group (msec);
12922 }
12923 while (msec != ssec);
12924
12925 /* If this is a pure debug section group or pure special section group,
12926 keep all sections in this group. */
12927 if (is_debug_grp || is_special_grp)
12928 {
12929 do
12930 {
12931 msec->gc_mark = 1;
12932 msec = elf_next_in_group (msec);
12933 }
12934 while (msec != ssec);
12935 }
12936}
12937
7f6ab9f8
AM
12938/* Keep debug and special sections. */
12939
12940bfd_boolean
12941_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12942 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12943{
12944 bfd *ibfd;
12945
c72f2fb2 12946 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12947 {
12948 asection *isec;
12949 bfd_boolean some_kept;
b40bf0a2 12950 bfd_boolean debug_frag_seen;
b7c871ed 12951 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
12952
12953 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12954 continue;
12955
b40bf0a2
NC
12956 /* Ensure all linker created sections are kept,
12957 see if any other section is already marked,
12958 and note if we have any fragmented debug sections. */
b7c871ed 12959 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
12960 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12961 {
12962 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12963 isec->gc_mark = 1;
12964 else if (isec->gc_mark)
12965 some_kept = TRUE;
b40bf0a2
NC
12966
12967 if (debug_frag_seen == FALSE
12968 && (isec->flags & SEC_DEBUGGING)
12969 && CONST_STRNEQ (isec->name, ".debug_line."))
12970 debug_frag_seen = TRUE;
7f6ab9f8
AM
12971 }
12972
12973 /* If no section in this file will be kept, then we can
b40bf0a2 12974 toss out the debug and special sections. */
7f6ab9f8
AM
12975 if (!some_kept)
12976 continue;
12977
12978 /* Keep debug and special sections like .comment when they are
3c758495
TG
12979 not part of a group. Also keep section groups that contain
12980 just debug sections or special sections. */
7f6ab9f8 12981 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12982 {
12983 if ((isec->flags & SEC_GROUP) != 0)
12984 _bfd_elf_gc_mark_debug_special_section_group (isec);
12985 else if (((isec->flags & SEC_DEBUGGING) != 0
12986 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12987 && elf_next_in_group (isec) == NULL)
12988 isec->gc_mark = 1;
b7c871ed
L
12989 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12990 has_kept_debug_info = TRUE;
3c758495 12991 }
b40bf0a2 12992
b40bf0a2
NC
12993 /* Look for CODE sections which are going to be discarded,
12994 and find and discard any fragmented debug sections which
12995 are associated with that code section. */
b7c871ed
L
12996 if (debug_frag_seen)
12997 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12998 if ((isec->flags & SEC_CODE) != 0
12999 && isec->gc_mark == 0)
13000 {
13001 unsigned int ilen;
13002 asection *dsec;
b40bf0a2 13003
b7c871ed 13004 ilen = strlen (isec->name);
b40bf0a2 13005
b7c871ed
L
13006 /* Association is determined by the name of the debug
13007 section containing the name of the code section as
13008 a suffix. For example .debug_line.text.foo is a
13009 debug section associated with .text.foo. */
13010 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13011 {
13012 unsigned int dlen;
b40bf0a2 13013
b7c871ed
L
13014 if (dsec->gc_mark == 0
13015 || (dsec->flags & SEC_DEBUGGING) == 0)
13016 continue;
b40bf0a2 13017
b7c871ed 13018 dlen = strlen (dsec->name);
b40bf0a2 13019
b7c871ed
L
13020 if (dlen > ilen
13021 && strncmp (dsec->name + (dlen - ilen),
13022 isec->name, ilen) == 0)
b40bf0a2 13023 dsec->gc_mark = 0;
b7c871ed 13024 }
b40bf0a2 13025 }
b7c871ed
L
13026
13027 /* Mark debug sections referenced by kept debug sections. */
13028 if (has_kept_debug_info)
13029 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13030 if (isec->gc_mark
13031 && (isec->flags & SEC_DEBUGGING) != 0)
13032 if (!_bfd_elf_gc_mark (info, isec,
13033 elf_gc_mark_debug_section))
13034 return FALSE;
7f6ab9f8
AM
13035 }
13036 return TRUE;
13037}
13038
c152c796
AM
13039/* The sweep phase of garbage collection. Remove all garbage sections. */
13040
13041typedef bfd_boolean (*gc_sweep_hook_fn)
13042 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
13043
13044static bfd_boolean
ccabcbe5 13045elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13046{
13047 bfd *sub;
ccabcbe5
AM
13048 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13049 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 13050
c72f2fb2 13051 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13052 {
13053 asection *o;
13054
b19a8f85
L
13055 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13056 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13057 continue;
13058
13059 for (o = sub->sections; o != NULL; o = o->next)
13060 {
a33dafc3
L
13061 /* When any section in a section group is kept, we keep all
13062 sections in the section group. If the first member of
13063 the section group is excluded, we will also exclude the
13064 group section. */
13065 if (o->flags & SEC_GROUP)
13066 {
13067 asection *first = elf_next_in_group (o);
13068 o->gc_mark = first->gc_mark;
13069 }
c152c796 13070
1e7eae0d 13071 if (o->gc_mark)
c152c796
AM
13072 continue;
13073
13074 /* Skip sweeping sections already excluded. */
13075 if (o->flags & SEC_EXCLUDE)
13076 continue;
13077
13078 /* Since this is early in the link process, it is simple
13079 to remove a section from the output. */
13080 o->flags |= SEC_EXCLUDE;
13081
c55fe096 13082 if (info->print_gc_sections && o->size != 0)
695344c0 13083 /* xgettext:c-format */
c08bb8dd
AM
13084 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13085 o, sub);
c17d87de 13086
c152c796
AM
13087 /* But we also have to update some of the relocation
13088 info we collected before. */
13089 if (gc_sweep_hook
e8aaee2a 13090 && (o->flags & SEC_RELOC) != 0
9850436d
AM
13091 && o->reloc_count != 0
13092 && !((info->strip == strip_all || info->strip == strip_debugger)
13093 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 13094 && !bfd_is_abs_section (o->output_section))
c152c796
AM
13095 {
13096 Elf_Internal_Rela *internal_relocs;
13097 bfd_boolean r;
13098
13099 internal_relocs
13100 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13101 info->keep_memory);
13102 if (internal_relocs == NULL)
13103 return FALSE;
13104
13105 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13106
13107 if (elf_section_data (o)->relocs != internal_relocs)
13108 free (internal_relocs);
13109
13110 if (!r)
13111 return FALSE;
13112 }
13113 }
13114 }
13115
c152c796
AM
13116 return TRUE;
13117}
13118
13119/* Propagate collected vtable information. This is called through
13120 elf_link_hash_traverse. */
13121
13122static bfd_boolean
13123elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13124{
c152c796 13125 /* Those that are not vtables. */
f6e332e6 13126 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13127 return TRUE;
13128
13129 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 13130 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13131 return TRUE;
13132
13133 /* If we've already been done, exit. */
f6e332e6 13134 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
13135 return TRUE;
13136
13137 /* Make sure the parent's table is up to date. */
f6e332e6 13138 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 13139
f6e332e6 13140 if (h->vtable->used == NULL)
c152c796
AM
13141 {
13142 /* None of this table's entries were referenced. Re-use the
13143 parent's table. */
f6e332e6
AM
13144 h->vtable->used = h->vtable->parent->vtable->used;
13145 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
13146 }
13147 else
13148 {
13149 size_t n;
13150 bfd_boolean *cu, *pu;
13151
13152 /* Or the parent's entries into ours. */
f6e332e6 13153 cu = h->vtable->used;
c152c796 13154 cu[-1] = TRUE;
f6e332e6 13155 pu = h->vtable->parent->vtable->used;
c152c796
AM
13156 if (pu != NULL)
13157 {
13158 const struct elf_backend_data *bed;
13159 unsigned int log_file_align;
13160
13161 bed = get_elf_backend_data (h->root.u.def.section->owner);
13162 log_file_align = bed->s->log_file_align;
f6e332e6 13163 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
13164 while (n--)
13165 {
13166 if (*pu)
13167 *cu = TRUE;
13168 pu++;
13169 cu++;
13170 }
13171 }
13172 }
13173
13174 return TRUE;
13175}
13176
13177static bfd_boolean
13178elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13179{
13180 asection *sec;
13181 bfd_vma hstart, hend;
13182 Elf_Internal_Rela *relstart, *relend, *rel;
13183 const struct elf_backend_data *bed;
13184 unsigned int log_file_align;
13185
c152c796
AM
13186 /* Take care of both those symbols that do not describe vtables as
13187 well as those that are not loaded. */
f6e332e6 13188 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13189 return TRUE;
13190
13191 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13192 || h->root.type == bfd_link_hash_defweak);
13193
13194 sec = h->root.u.def.section;
13195 hstart = h->root.u.def.value;
13196 hend = hstart + h->size;
13197
13198 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13199 if (!relstart)
13200 return *(bfd_boolean *) okp = FALSE;
13201 bed = get_elf_backend_data (sec->owner);
13202 log_file_align = bed->s->log_file_align;
13203
13204 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13205
13206 for (rel = relstart; rel < relend; ++rel)
13207 if (rel->r_offset >= hstart && rel->r_offset < hend)
13208 {
13209 /* If the entry is in use, do nothing. */
f6e332e6
AM
13210 if (h->vtable->used
13211 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13212 {
13213 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13214 if (h->vtable->used[entry])
c152c796
AM
13215 continue;
13216 }
13217 /* Otherwise, kill it. */
13218 rel->r_offset = rel->r_info = rel->r_addend = 0;
13219 }
13220
13221 return TRUE;
13222}
13223
87538722
AM
13224/* Mark sections containing dynamically referenced symbols. When
13225 building shared libraries, we must assume that any visible symbol is
13226 referenced. */
715df9b8 13227
64d03ab5
AM
13228bfd_boolean
13229bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13230{
87538722 13231 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13232 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13233
715df9b8
EB
13234 if ((h->root.type == bfd_link_hash_defined
13235 || h->root.type == bfd_link_hash_defweak)
87538722 13236 && (h->ref_dynamic
c4621b33 13237 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13238 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13239 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13240 && (!bfd_link_executable (info)
22185505 13241 || info->gc_keep_exported
b407645f
AM
13242 || info->export_dynamic
13243 || (h->dynamic
13244 && d != NULL
13245 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13246 && (h->versioned >= versioned
54e8959c
L
13247 || !bfd_hide_sym_by_version (info->version_info,
13248 h->root.root.string)))))
715df9b8
EB
13249 h->root.u.def.section->flags |= SEC_KEEP;
13250
13251 return TRUE;
13252}
3b36f7e6 13253
74f0fb50
AM
13254/* Keep all sections containing symbols undefined on the command-line,
13255 and the section containing the entry symbol. */
13256
13257void
13258_bfd_elf_gc_keep (struct bfd_link_info *info)
13259{
13260 struct bfd_sym_chain *sym;
13261
13262 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13263 {
13264 struct elf_link_hash_entry *h;
13265
13266 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13267 FALSE, FALSE, FALSE);
13268
13269 if (h != NULL
13270 && (h->root.type == bfd_link_hash_defined
13271 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13272 && !bfd_is_abs_section (h->root.u.def.section)
13273 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13274 h->root.u.def.section->flags |= SEC_KEEP;
13275 }
13276}
13277
2f0c68f2
CM
13278bfd_boolean
13279bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13280 struct bfd_link_info *info)
13281{
13282 bfd *ibfd = info->input_bfds;
13283
13284 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13285 {
13286 asection *sec;
13287 struct elf_reloc_cookie cookie;
13288
13289 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13290 continue;
13291
13292 if (!init_reloc_cookie (&cookie, info, ibfd))
13293 return FALSE;
13294
13295 for (sec = ibfd->sections; sec; sec = sec->next)
13296 {
13297 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13298 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13299 {
13300 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13301 fini_reloc_cookie_rels (&cookie, sec);
13302 }
13303 }
13304 }
13305 return TRUE;
13306}
13307
c152c796
AM
13308/* Do mark and sweep of unused sections. */
13309
13310bfd_boolean
13311bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13312{
13313 bfd_boolean ok = TRUE;
13314 bfd *sub;
6a5bb875 13315 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13316 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13317 struct elf_link_hash_table *htab;
c152c796 13318
64d03ab5 13319 if (!bed->can_gc_sections
715df9b8 13320 || !is_elf_hash_table (info->hash))
c152c796 13321 {
4eca0228 13322 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13323 return TRUE;
13324 }
13325
74f0fb50 13326 bed->gc_keep (info);
da44f4e5 13327 htab = elf_hash_table (info);
74f0fb50 13328
9d0a14d3
RS
13329 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13330 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13331 for (sub = info->input_bfds;
13332 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13333 sub = sub->link.next)
9d0a14d3
RS
13334 {
13335 asection *sec;
13336 struct elf_reloc_cookie cookie;
13337
13338 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13339 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13340 {
13341 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13342 if (elf_section_data (sec)->sec_info
13343 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13344 elf_eh_frame_section (sub) = sec;
13345 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13346 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13347 }
13348 }
9d0a14d3 13349
c152c796 13350 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13351 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13352 if (!ok)
13353 return FALSE;
13354
13355 /* Kill the vtable relocations that were not used. */
da44f4e5 13356 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13357 if (!ok)
13358 return FALSE;
13359
715df9b8 13360 /* Mark dynamically referenced symbols. */
22185505 13361 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13362 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13363
715df9b8 13364 /* Grovel through relocs to find out who stays ... */
64d03ab5 13365 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13366 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13367 {
13368 asection *o;
13369
b19a8f85
L
13370 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13371 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13372 continue;
13373
7f6ab9f8
AM
13374 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13375 Also treat note sections as a root, if the section is not part
13376 of a group. */
c152c796 13377 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13378 if (!o->gc_mark
13379 && (o->flags & SEC_EXCLUDE) == 0
24007750 13380 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13381 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13382 && elf_next_in_group (o) == NULL )))
13383 {
13384 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13385 return FALSE;
13386 }
c152c796
AM
13387 }
13388
6a5bb875 13389 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13390 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13391
c152c796 13392 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13393 return elf_gc_sweep (abfd, info);
c152c796
AM
13394}
13395\f
13396/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13397
13398bfd_boolean
13399bfd_elf_gc_record_vtinherit (bfd *abfd,
13400 asection *sec,
13401 struct elf_link_hash_entry *h,
13402 bfd_vma offset)
13403{
13404 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13405 struct elf_link_hash_entry **search, *child;
ef53be89 13406 size_t extsymcount;
c152c796
AM
13407 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13408
13409 /* The sh_info field of the symtab header tells us where the
13410 external symbols start. We don't care about the local symbols at
13411 this point. */
13412 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13413 if (!elf_bad_symtab (abfd))
13414 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13415
13416 sym_hashes = elf_sym_hashes (abfd);
13417 sym_hashes_end = sym_hashes + extsymcount;
13418
13419 /* Hunt down the child symbol, which is in this section at the same
13420 offset as the relocation. */
13421 for (search = sym_hashes; search != sym_hashes_end; ++search)
13422 {
13423 if ((child = *search) != NULL
13424 && (child->root.type == bfd_link_hash_defined
13425 || child->root.type == bfd_link_hash_defweak)
13426 && child->root.u.def.section == sec
13427 && child->root.u.def.value == offset)
13428 goto win;
13429 }
13430
695344c0
NC
13431 /* xgettext:c-format */
13432 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13433 abfd, sec, (unsigned long) offset);
c152c796
AM
13434 bfd_set_error (bfd_error_invalid_operation);
13435 return FALSE;
13436
13437 win:
f6e332e6
AM
13438 if (!child->vtable)
13439 {
ca4be51c
AM
13440 child->vtable = ((struct elf_link_virtual_table_entry *)
13441 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13442 if (!child->vtable)
13443 return FALSE;
13444 }
c152c796
AM
13445 if (!h)
13446 {
13447 /* This *should* only be the absolute section. It could potentially
13448 be that someone has defined a non-global vtable though, which
13449 would be bad. It isn't worth paging in the local symbols to be
13450 sure though; that case should simply be handled by the assembler. */
13451
f6e332e6 13452 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13453 }
13454 else
f6e332e6 13455 child->vtable->parent = h;
c152c796
AM
13456
13457 return TRUE;
13458}
13459
13460/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13461
13462bfd_boolean
13463bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13464 asection *sec ATTRIBUTE_UNUSED,
13465 struct elf_link_hash_entry *h,
13466 bfd_vma addend)
13467{
13468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13469 unsigned int log_file_align = bed->s->log_file_align;
13470
f6e332e6
AM
13471 if (!h->vtable)
13472 {
ca4be51c
AM
13473 h->vtable = ((struct elf_link_virtual_table_entry *)
13474 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13475 if (!h->vtable)
13476 return FALSE;
13477 }
13478
13479 if (addend >= h->vtable->size)
c152c796
AM
13480 {
13481 size_t size, bytes, file_align;
f6e332e6 13482 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13483
13484 /* While the symbol is undefined, we have to be prepared to handle
13485 a zero size. */
13486 file_align = 1 << log_file_align;
13487 if (h->root.type == bfd_link_hash_undefined)
13488 size = addend + file_align;
13489 else
13490 {
13491 size = h->size;
13492 if (addend >= size)
13493 {
13494 /* Oops! We've got a reference past the defined end of
13495 the table. This is probably a bug -- shall we warn? */
13496 size = addend + file_align;
13497 }
13498 }
13499 size = (size + file_align - 1) & -file_align;
13500
13501 /* Allocate one extra entry for use as a "done" flag for the
13502 consolidation pass. */
13503 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13504
13505 if (ptr)
13506 {
a50b1753 13507 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13508
13509 if (ptr != NULL)
13510 {
13511 size_t oldbytes;
13512
f6e332e6 13513 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13514 * sizeof (bfd_boolean));
13515 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13516 }
13517 }
13518 else
a50b1753 13519 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13520
13521 if (ptr == NULL)
13522 return FALSE;
13523
13524 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13525 h->vtable->used = ptr + 1;
13526 h->vtable->size = size;
c152c796
AM
13527 }
13528
f6e332e6 13529 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13530
13531 return TRUE;
13532}
13533
ae17ab41
CM
13534/* Map an ELF section header flag to its corresponding string. */
13535typedef struct
13536{
13537 char *flag_name;
13538 flagword flag_value;
13539} elf_flags_to_name_table;
13540
13541static elf_flags_to_name_table elf_flags_to_names [] =
13542{
13543 { "SHF_WRITE", SHF_WRITE },
13544 { "SHF_ALLOC", SHF_ALLOC },
13545 { "SHF_EXECINSTR", SHF_EXECINSTR },
13546 { "SHF_MERGE", SHF_MERGE },
13547 { "SHF_STRINGS", SHF_STRINGS },
13548 { "SHF_INFO_LINK", SHF_INFO_LINK},
13549 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13550 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13551 { "SHF_GROUP", SHF_GROUP },
13552 { "SHF_TLS", SHF_TLS },
13553 { "SHF_MASKOS", SHF_MASKOS },
13554 { "SHF_EXCLUDE", SHF_EXCLUDE },
13555};
13556
b9c361e0
JL
13557/* Returns TRUE if the section is to be included, otherwise FALSE. */
13558bfd_boolean
ae17ab41 13559bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13560 struct flag_info *flaginfo,
b9c361e0 13561 asection *section)
ae17ab41 13562{
8b127cbc 13563 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13564
8b127cbc 13565 if (!flaginfo->flags_initialized)
ae17ab41 13566 {
8b127cbc
AM
13567 bfd *obfd = info->output_bfd;
13568 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13569 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13570 int with_hex = 0;
13571 int without_hex = 0;
13572
8b127cbc 13573 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13574 {
b9c361e0 13575 unsigned i;
8b127cbc 13576 flagword (*lookup) (char *);
ae17ab41 13577
8b127cbc
AM
13578 lookup = bed->elf_backend_lookup_section_flags_hook;
13579 if (lookup != NULL)
ae17ab41 13580 {
8b127cbc 13581 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13582
13583 if (hexval != 0)
13584 {
13585 if (tf->with == with_flags)
13586 with_hex |= hexval;
13587 else if (tf->with == without_flags)
13588 without_hex |= hexval;
13589 tf->valid = TRUE;
13590 continue;
13591 }
ae17ab41 13592 }
8b127cbc 13593 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13594 {
8b127cbc 13595 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13596 {
13597 if (tf->with == with_flags)
13598 with_hex |= elf_flags_to_names[i].flag_value;
13599 else if (tf->with == without_flags)
13600 without_hex |= elf_flags_to_names[i].flag_value;
13601 tf->valid = TRUE;
13602 break;
13603 }
13604 }
8b127cbc 13605 if (!tf->valid)
b9c361e0 13606 {
68ffbac6 13607 info->callbacks->einfo
8b127cbc 13608 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13609 return FALSE;
ae17ab41
CM
13610 }
13611 }
8b127cbc
AM
13612 flaginfo->flags_initialized = TRUE;
13613 flaginfo->only_with_flags |= with_hex;
13614 flaginfo->not_with_flags |= without_hex;
ae17ab41 13615 }
ae17ab41 13616
8b127cbc 13617 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13618 return FALSE;
13619
8b127cbc 13620 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13621 return FALSE;
13622
13623 return TRUE;
ae17ab41
CM
13624}
13625
c152c796
AM
13626struct alloc_got_off_arg {
13627 bfd_vma gotoff;
10455f89 13628 struct bfd_link_info *info;
c152c796
AM
13629};
13630
13631/* We need a special top-level link routine to convert got reference counts
13632 to real got offsets. */
13633
13634static bfd_boolean
13635elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13636{
a50b1753 13637 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13638 bfd *obfd = gofarg->info->output_bfd;
13639 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13640
c152c796
AM
13641 if (h->got.refcount > 0)
13642 {
13643 h->got.offset = gofarg->gotoff;
10455f89 13644 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13645 }
13646 else
13647 h->got.offset = (bfd_vma) -1;
13648
13649 return TRUE;
13650}
13651
13652/* And an accompanying bit to work out final got entry offsets once
13653 we're done. Should be called from final_link. */
13654
13655bfd_boolean
13656bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13657 struct bfd_link_info *info)
13658{
13659 bfd *i;
13660 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13661 bfd_vma gotoff;
c152c796
AM
13662 struct alloc_got_off_arg gofarg;
13663
10455f89
HPN
13664 BFD_ASSERT (abfd == info->output_bfd);
13665
c152c796
AM
13666 if (! is_elf_hash_table (info->hash))
13667 return FALSE;
13668
13669 /* The GOT offset is relative to the .got section, but the GOT header is
13670 put into the .got.plt section, if the backend uses it. */
13671 if (bed->want_got_plt)
13672 gotoff = 0;
13673 else
13674 gotoff = bed->got_header_size;
13675
13676 /* Do the local .got entries first. */
c72f2fb2 13677 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13678 {
13679 bfd_signed_vma *local_got;
ef53be89 13680 size_t j, locsymcount;
c152c796
AM
13681 Elf_Internal_Shdr *symtab_hdr;
13682
13683 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13684 continue;
13685
13686 local_got = elf_local_got_refcounts (i);
13687 if (!local_got)
13688 continue;
13689
13690 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13691 if (elf_bad_symtab (i))
13692 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13693 else
13694 locsymcount = symtab_hdr->sh_info;
13695
13696 for (j = 0; j < locsymcount; ++j)
13697 {
13698 if (local_got[j] > 0)
13699 {
13700 local_got[j] = gotoff;
10455f89 13701 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13702 }
13703 else
13704 local_got[j] = (bfd_vma) -1;
13705 }
13706 }
13707
13708 /* Then the global .got entries. .plt refcounts are handled by
13709 adjust_dynamic_symbol */
13710 gofarg.gotoff = gotoff;
10455f89 13711 gofarg.info = info;
c152c796
AM
13712 elf_link_hash_traverse (elf_hash_table (info),
13713 elf_gc_allocate_got_offsets,
13714 &gofarg);
13715 return TRUE;
13716}
13717
13718/* Many folk need no more in the way of final link than this, once
13719 got entry reference counting is enabled. */
13720
13721bfd_boolean
13722bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13723{
13724 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13725 return FALSE;
13726
13727 /* Invoke the regular ELF backend linker to do all the work. */
13728 return bfd_elf_final_link (abfd, info);
13729}
13730
13731bfd_boolean
13732bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13733{
a50b1753 13734 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13735
13736 if (rcookie->bad_symtab)
13737 rcookie->rel = rcookie->rels;
13738
13739 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13740 {
13741 unsigned long r_symndx;
13742
13743 if (! rcookie->bad_symtab)
13744 if (rcookie->rel->r_offset > offset)
13745 return FALSE;
13746 if (rcookie->rel->r_offset != offset)
13747 continue;
13748
13749 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13750 if (r_symndx == STN_UNDEF)
c152c796
AM
13751 return TRUE;
13752
13753 if (r_symndx >= rcookie->locsymcount
13754 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13755 {
13756 struct elf_link_hash_entry *h;
13757
13758 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13759
13760 while (h->root.type == bfd_link_hash_indirect
13761 || h->root.type == bfd_link_hash_warning)
13762 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13763
13764 if ((h->root.type == bfd_link_hash_defined
13765 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13766 && (h->root.u.def.section->owner != rcookie->abfd
13767 || h->root.u.def.section->kept_section != NULL
13768 || discarded_section (h->root.u.def.section)))
c152c796 13769 return TRUE;
c152c796
AM
13770 }
13771 else
13772 {
13773 /* It's not a relocation against a global symbol,
13774 but it could be a relocation against a local
13775 symbol for a discarded section. */
13776 asection *isec;
13777 Elf_Internal_Sym *isym;
13778
13779 /* Need to: get the symbol; get the section. */
13780 isym = &rcookie->locsyms[r_symndx];
cb33740c 13781 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13782 if (isec != NULL
13783 && (isec->kept_section != NULL
13784 || discarded_section (isec)))
cb33740c 13785 return TRUE;
c152c796
AM
13786 }
13787 return FALSE;
13788 }
13789 return FALSE;
13790}
13791
13792/* Discard unneeded references to discarded sections.
75938853
AM
13793 Returns -1 on error, 1 if any section's size was changed, 0 if
13794 nothing changed. This function assumes that the relocations are in
13795 sorted order, which is true for all known assemblers. */
c152c796 13796
75938853 13797int
c152c796
AM
13798bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13799{
13800 struct elf_reloc_cookie cookie;
18cd5bce 13801 asection *o;
c152c796 13802 bfd *abfd;
75938853 13803 int changed = 0;
c152c796
AM
13804
13805 if (info->traditional_format
13806 || !is_elf_hash_table (info->hash))
75938853 13807 return 0;
c152c796 13808
18cd5bce
AM
13809 o = bfd_get_section_by_name (output_bfd, ".stab");
13810 if (o != NULL)
c152c796 13811 {
18cd5bce 13812 asection *i;
c152c796 13813
18cd5bce 13814 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13815 {
18cd5bce
AM
13816 if (i->size == 0
13817 || i->reloc_count == 0
13818 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13819 continue;
c152c796 13820
18cd5bce
AM
13821 abfd = i->owner;
13822 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13823 continue;
c152c796 13824
18cd5bce 13825 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13826 return -1;
c152c796 13827
18cd5bce
AM
13828 if (_bfd_discard_section_stabs (abfd, i,
13829 elf_section_data (i)->sec_info,
5241d853
RS
13830 bfd_elf_reloc_symbol_deleted_p,
13831 &cookie))
75938853 13832 changed = 1;
18cd5bce
AM
13833
13834 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13835 }
18cd5bce
AM
13836 }
13837
2f0c68f2
CM
13838 o = NULL;
13839 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13840 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13841 if (o != NULL)
13842 {
13843 asection *i;
d7153c4a 13844 int eh_changed = 0;
c152c796 13845
18cd5bce 13846 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13847 {
18cd5bce
AM
13848 if (i->size == 0)
13849 continue;
13850
13851 abfd = i->owner;
13852 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13853 continue;
13854
13855 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13856 return -1;
18cd5bce
AM
13857
13858 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13859 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13860 bfd_elf_reloc_symbol_deleted_p,
13861 &cookie))
d7153c4a
AM
13862 {
13863 eh_changed = 1;
13864 if (i->size != i->rawsize)
13865 changed = 1;
13866 }
18cd5bce
AM
13867
13868 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13869 }
d7153c4a
AM
13870 if (eh_changed)
13871 elf_link_hash_traverse (elf_hash_table (info),
13872 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13873 }
c152c796 13874
18cd5bce
AM
13875 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13876 {
13877 const struct elf_backend_data *bed;
c152c796 13878
18cd5bce
AM
13879 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13880 continue;
13881
13882 bed = get_elf_backend_data (abfd);
13883
13884 if (bed->elf_backend_discard_info != NULL)
13885 {
13886 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13887 return -1;
18cd5bce
AM
13888
13889 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13890 changed = 1;
18cd5bce
AM
13891
13892 fini_reloc_cookie (&cookie, abfd);
13893 }
c152c796
AM
13894 }
13895
2f0c68f2
CM
13896 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13897 _bfd_elf_end_eh_frame_parsing (info);
13898
13899 if (info->eh_frame_hdr_type
0e1862bb 13900 && !bfd_link_relocatable (info)
c152c796 13901 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13902 changed = 1;
c152c796 13903
75938853 13904 return changed;
c152c796 13905}
082b7297 13906
43e1669b 13907bfd_boolean
0c511000 13908_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13909 asection *sec,
c0f00686 13910 struct bfd_link_info *info)
082b7297
L
13911{
13912 flagword flags;
c77ec726 13913 const char *name, *key;
082b7297
L
13914 struct bfd_section_already_linked *l;
13915 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13916
c77ec726
AM
13917 if (sec->output_section == bfd_abs_section_ptr)
13918 return FALSE;
0c511000 13919
c77ec726 13920 flags = sec->flags;
0c511000 13921
c77ec726
AM
13922 /* Return if it isn't a linkonce section. A comdat group section
13923 also has SEC_LINK_ONCE set. */
13924 if ((flags & SEC_LINK_ONCE) == 0)
13925 return FALSE;
0c511000 13926
c77ec726
AM
13927 /* Don't put group member sections on our list of already linked
13928 sections. They are handled as a group via their group section. */
13929 if (elf_sec_group (sec) != NULL)
13930 return FALSE;
0c511000 13931
c77ec726
AM
13932 /* For a SHT_GROUP section, use the group signature as the key. */
13933 name = sec->name;
13934 if ((flags & SEC_GROUP) != 0
13935 && elf_next_in_group (sec) != NULL
13936 && elf_group_name (elf_next_in_group (sec)) != NULL)
13937 key = elf_group_name (elf_next_in_group (sec));
13938 else
13939 {
13940 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13941 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13942 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13943 key++;
0c511000 13944 else
c77ec726
AM
13945 /* Must be a user linkonce section that doesn't follow gcc's
13946 naming convention. In this case we won't be matching
13947 single member groups. */
13948 key = name;
0c511000 13949 }
6d2cd210 13950
c77ec726 13951 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13952
13953 for (l = already_linked_list->entry; l != NULL; l = l->next)
13954 {
c2370991 13955 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13956 sections with a signature of <key> (<key> is some string),
13957 and linkonce sections named .gnu.linkonce.<type>.<key>.
13958 Match like sections. LTO plugin sections are an exception.
13959 They are always named .gnu.linkonce.t.<key> and match either
13960 type of section. */
13961 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13962 && ((flags & SEC_GROUP) != 0
13963 || strcmp (name, l->sec->name) == 0))
13964 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13965 {
13966 /* The section has already been linked. See if we should
6d2cd210 13967 issue a warning. */
c77ec726
AM
13968 if (!_bfd_handle_already_linked (sec, l, info))
13969 return FALSE;
082b7297 13970
c77ec726 13971 if (flags & SEC_GROUP)
3d7f7666 13972 {
c77ec726
AM
13973 asection *first = elf_next_in_group (sec);
13974 asection *s = first;
3d7f7666 13975
c77ec726 13976 while (s != NULL)
3d7f7666 13977 {
c77ec726
AM
13978 s->output_section = bfd_abs_section_ptr;
13979 /* Record which group discards it. */
13980 s->kept_section = l->sec;
13981 s = elf_next_in_group (s);
13982 /* These lists are circular. */
13983 if (s == first)
13984 break;
3d7f7666
L
13985 }
13986 }
082b7297 13987
43e1669b 13988 return TRUE;
082b7297
L
13989 }
13990 }
13991
c77ec726
AM
13992 /* A single member comdat group section may be discarded by a
13993 linkonce section and vice versa. */
13994 if ((flags & SEC_GROUP) != 0)
3d7f7666 13995 {
c77ec726 13996 asection *first = elf_next_in_group (sec);
c2370991 13997
c77ec726
AM
13998 if (first != NULL && elf_next_in_group (first) == first)
13999 /* Check this single member group against linkonce sections. */
14000 for (l = already_linked_list->entry; l != NULL; l = l->next)
14001 if ((l->sec->flags & SEC_GROUP) == 0
14002 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14003 {
14004 first->output_section = bfd_abs_section_ptr;
14005 first->kept_section = l->sec;
14006 sec->output_section = bfd_abs_section_ptr;
14007 break;
14008 }
14009 }
14010 else
14011 /* Check this linkonce section against single member groups. */
14012 for (l = already_linked_list->entry; l != NULL; l = l->next)
14013 if (l->sec->flags & SEC_GROUP)
6d2cd210 14014 {
c77ec726 14015 asection *first = elf_next_in_group (l->sec);
6d2cd210 14016
c77ec726
AM
14017 if (first != NULL
14018 && elf_next_in_group (first) == first
14019 && bfd_elf_match_symbols_in_sections (first, sec, info))
14020 {
14021 sec->output_section = bfd_abs_section_ptr;
14022 sec->kept_section = first;
14023 break;
14024 }
6d2cd210 14025 }
0c511000 14026
c77ec726
AM
14027 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14028 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14029 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14030 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14031 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14032 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14033 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14034 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14035 The reverse order cannot happen as there is never a bfd with only the
14036 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14037 matter as here were are looking only for cross-bfd sections. */
14038
14039 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14040 for (l = already_linked_list->entry; l != NULL; l = l->next)
14041 if ((l->sec->flags & SEC_GROUP) == 0
14042 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14043 {
14044 if (abfd != l->sec->owner)
14045 sec->output_section = bfd_abs_section_ptr;
14046 break;
14047 }
80c29487 14048
082b7297 14049 /* This is the first section with this name. Record it. */
c77ec726 14050 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14051 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14052 return sec->output_section == bfd_abs_section_ptr;
082b7297 14053}
81e1b023 14054
a4d8e49b
L
14055bfd_boolean
14056_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14057{
14058 return sym->st_shndx == SHN_COMMON;
14059}
14060
14061unsigned int
14062_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14063{
14064 return SHN_COMMON;
14065}
14066
14067asection *
14068_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14069{
14070 return bfd_com_section_ptr;
14071}
10455f89
HPN
14072
14073bfd_vma
14074_bfd_elf_default_got_elt_size (bfd *abfd,
14075 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14076 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14077 bfd *ibfd ATTRIBUTE_UNUSED,
14078 unsigned long symndx ATTRIBUTE_UNUSED)
14079{
14080 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14081 return bed->s->arch_size / 8;
14082}
83bac4b0
NC
14083
14084/* Routines to support the creation of dynamic relocs. */
14085
83bac4b0
NC
14086/* Returns the name of the dynamic reloc section associated with SEC. */
14087
14088static const char *
14089get_dynamic_reloc_section_name (bfd * abfd,
14090 asection * sec,
14091 bfd_boolean is_rela)
14092{
ddcf1fcf
BS
14093 char *name;
14094 const char *old_name = bfd_get_section_name (NULL, sec);
14095 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14096
ddcf1fcf 14097 if (old_name == NULL)
83bac4b0
NC
14098 return NULL;
14099
ddcf1fcf 14100 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14101 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14102
14103 return name;
14104}
14105
14106/* Returns the dynamic reloc section associated with SEC.
14107 If necessary compute the name of the dynamic reloc section based
14108 on SEC's name (looked up in ABFD's string table) and the setting
14109 of IS_RELA. */
14110
14111asection *
14112_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14113 asection * sec,
14114 bfd_boolean is_rela)
14115{
14116 asection * reloc_sec = elf_section_data (sec)->sreloc;
14117
14118 if (reloc_sec == NULL)
14119 {
14120 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14121
14122 if (name != NULL)
14123 {
3d4d4302 14124 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14125
14126 if (reloc_sec != NULL)
14127 elf_section_data (sec)->sreloc = reloc_sec;
14128 }
14129 }
14130
14131 return reloc_sec;
14132}
14133
14134/* Returns the dynamic reloc section associated with SEC. If the
14135 section does not exist it is created and attached to the DYNOBJ
14136 bfd and stored in the SRELOC field of SEC's elf_section_data
14137 structure.
f8076f98 14138
83bac4b0
NC
14139 ALIGNMENT is the alignment for the newly created section and
14140 IS_RELA defines whether the name should be .rela.<SEC's name>
14141 or .rel.<SEC's name>. The section name is looked up in the
14142 string table associated with ABFD. */
14143
14144asection *
ca4be51c
AM
14145_bfd_elf_make_dynamic_reloc_section (asection *sec,
14146 bfd *dynobj,
14147 unsigned int alignment,
14148 bfd *abfd,
14149 bfd_boolean is_rela)
83bac4b0
NC
14150{
14151 asection * reloc_sec = elf_section_data (sec)->sreloc;
14152
14153 if (reloc_sec == NULL)
14154 {
14155 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14156
14157 if (name == NULL)
14158 return NULL;
14159
3d4d4302 14160 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14161
14162 if (reloc_sec == NULL)
14163 {
3d4d4302
AM
14164 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14165 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14166 if ((sec->flags & SEC_ALLOC) != 0)
14167 flags |= SEC_ALLOC | SEC_LOAD;
14168
3d4d4302 14169 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14170 if (reloc_sec != NULL)
14171 {
8877b5e5
AM
14172 /* _bfd_elf_get_sec_type_attr chooses a section type by
14173 name. Override as it may be wrong, eg. for a user
14174 section named "auto" we'll get ".relauto" which is
14175 seen to be a .rela section. */
14176 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14177 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14178 reloc_sec = NULL;
14179 }
14180 }
14181
14182 elf_section_data (sec)->sreloc = reloc_sec;
14183 }
14184
14185 return reloc_sec;
14186}
1338dd10 14187
bffebb6b
AM
14188/* Copy the ELF symbol type and other attributes for a linker script
14189 assignment from HSRC to HDEST. Generally this should be treated as
14190 if we found a strong non-dynamic definition for HDEST (except that
14191 ld ignores multiple definition errors). */
1338dd10 14192void
bffebb6b
AM
14193_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14194 struct bfd_link_hash_entry *hdest,
14195 struct bfd_link_hash_entry *hsrc)
1338dd10 14196{
bffebb6b
AM
14197 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14198 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14199 Elf_Internal_Sym isym;
1338dd10
PB
14200
14201 ehdest->type = ehsrc->type;
35fc36a8 14202 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14203
14204 isym.st_other = ehsrc->other;
b8417128 14205 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14206}
351f65ca
L
14207
14208/* Append a RELA relocation REL to section S in BFD. */
14209
14210void
14211elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14212{
14213 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14214 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14215 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14216 bed->s->swap_reloca_out (abfd, rel, loc);
14217}
14218
14219/* Append a REL relocation REL to section S in BFD. */
14220
14221void
14222elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14223{
14224 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14225 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14226 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14227 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14228}