Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
222 if ((ibfd->flags
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
224 {
225 abfd = ibfd;
226 break;
227 }
228 }
229 hash_table->dynobj = abfd;
230 }
7e9f0867
AM
231
232 if (hash_table->dynstr == NULL)
233 {
234 hash_table->dynstr = _bfd_elf_strtab_init ();
235 if (hash_table->dynstr == NULL)
236 return FALSE;
237 }
238 return TRUE;
239}
240
45d6a902
AM
241/* Create some sections which will be filled in with dynamic linking
242 information. ABFD is an input file which requires dynamic sections
243 to be created. The dynamic sections take up virtual memory space
244 when the final executable is run, so we need to create them before
245 addresses are assigned to the output sections. We work out the
246 actual contents and size of these sections later. */
252b5132 247
b34976b6 248bfd_boolean
268b6b39 249_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 250{
45d6a902 251 flagword flags;
91d6fa6a 252 asection *s;
9c5bfbb7 253 const struct elf_backend_data *bed;
9637f6ef 254 struct elf_link_hash_entry *h;
252b5132 255
0eddce27 256 if (! is_elf_hash_table (info->hash))
45d6a902
AM
257 return FALSE;
258
259 if (elf_hash_table (info)->dynamic_sections_created)
260 return TRUE;
261
7e9f0867
AM
262 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
263 return FALSE;
45d6a902 264
7e9f0867 265 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
266 bed = get_elf_backend_data (abfd);
267
268 flags = bed->dynamic_sec_flags;
45d6a902
AM
269
270 /* A dynamically linked executable has a .interp section, but a
271 shared library does not. */
9b8b325a 272 if (bfd_link_executable (info) && !info->nointerp)
252b5132 273 {
14b2f831
AM
274 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
275 flags | SEC_READONLY);
3496cb2a 276 if (s == NULL)
45d6a902
AM
277 return FALSE;
278 }
bb0deeff 279
45d6a902
AM
280 /* Create sections to hold version informations. These are removed
281 if they are not needed. */
14b2f831
AM
282 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
283 flags | SEC_READONLY);
45d6a902 284 if (s == NULL
45d6a902
AM
285 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
286 return FALSE;
287
14b2f831
AM
288 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
289 flags | SEC_READONLY);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, 1))
292 return FALSE;
293
14b2f831
AM
294 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
295 flags | SEC_READONLY);
45d6a902 296 if (s == NULL
45d6a902
AM
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 return FALSE;
299
14b2f831
AM
300 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
301 flags | SEC_READONLY);
45d6a902 302 if (s == NULL
45d6a902
AM
303 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 return FALSE;
cae1fbbb 305 elf_hash_table (info)->dynsym = s;
45d6a902 306
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
308 flags | SEC_READONLY);
3496cb2a 309 if (s == NULL)
45d6a902
AM
310 return FALSE;
311
14b2f831 312 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 313 if (s == NULL
45d6a902
AM
314 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
315 return FALSE;
316
317 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
318 .dynamic section. We could set _DYNAMIC in a linker script, but we
319 only want to define it if we are, in fact, creating a .dynamic
320 section. We don't want to define it if there is no .dynamic
321 section, since on some ELF platforms the start up code examines it
322 to decide how to initialize the process. */
9637f6ef
L
323 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
324 elf_hash_table (info)->hdynamic = h;
325 if (h == NULL)
45d6a902
AM
326 return FALSE;
327
fdc90cb4
JJ
328 if (info->emit_hash)
329 {
14b2f831
AM
330 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
331 flags | SEC_READONLY);
fdc90cb4
JJ
332 if (s == NULL
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
335 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
336 }
337
338 if (info->emit_gnu_hash)
339 {
14b2f831
AM
340 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
341 flags | SEC_READONLY);
fdc90cb4
JJ
342 if (s == NULL
343 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
344 return FALSE;
345 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
346 4 32-bit words followed by variable count of 64-bit words, then
347 variable count of 32-bit words. */
348 if (bed->s->arch_size == 64)
349 elf_section_data (s)->this_hdr.sh_entsize = 0;
350 else
351 elf_section_data (s)->this_hdr.sh_entsize = 4;
352 }
45d6a902
AM
353
354 /* Let the backend create the rest of the sections. This lets the
355 backend set the right flags. The backend will normally create
356 the .got and .plt sections. */
894891db
NC
357 if (bed->elf_backend_create_dynamic_sections == NULL
358 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
359 return FALSE;
360
361 elf_hash_table (info)->dynamic_sections_created = TRUE;
362
363 return TRUE;
364}
365
366/* Create dynamic sections when linking against a dynamic object. */
367
368bfd_boolean
268b6b39 369_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
370{
371 flagword flags, pltflags;
7325306f 372 struct elf_link_hash_entry *h;
45d6a902 373 asection *s;
9c5bfbb7 374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 375 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 376
252b5132
RH
377 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
378 .rel[a].bss sections. */
e5a52504 379 flags = bed->dynamic_sec_flags;
252b5132
RH
380
381 pltflags = flags;
252b5132 382 if (bed->plt_not_loaded)
6df4d94c
MM
383 /* We do not clear SEC_ALLOC here because we still want the OS to
384 allocate space for the section; it's just that there's nothing
385 to read in from the object file. */
5d1634d7 386 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
387 else
388 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
389 if (bed->plt_readonly)
390 pltflags |= SEC_READONLY;
391
14b2f831 392 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 393 if (s == NULL
252b5132 394 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 395 return FALSE;
6de2ae4a 396 htab->splt = s;
252b5132 397
d98685ac
AM
398 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
399 .plt section. */
7325306f
RS
400 if (bed->want_plt_sym)
401 {
402 h = _bfd_elf_define_linkage_sym (abfd, info, s,
403 "_PROCEDURE_LINKAGE_TABLE_");
404 elf_hash_table (info)->hplt = h;
405 if (h == NULL)
406 return FALSE;
407 }
252b5132 408
14b2f831
AM
409 s = bfd_make_section_anyway_with_flags (abfd,
410 (bed->rela_plts_and_copies_p
411 ? ".rela.plt" : ".rel.plt"),
412 flags | SEC_READONLY);
252b5132 413 if (s == NULL
45d6a902 414 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 415 return FALSE;
6de2ae4a 416 htab->srelplt = s;
252b5132
RH
417
418 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 419 return FALSE;
252b5132 420
3018b441
RH
421 if (bed->want_dynbss)
422 {
423 /* The .dynbss section is a place to put symbols which are defined
424 by dynamic objects, are referenced by regular objects, and are
425 not functions. We must allocate space for them in the process
426 image and use a R_*_COPY reloc to tell the dynamic linker to
427 initialize them at run time. The linker script puts the .dynbss
428 section into the .bss section of the final image. */
14b2f831 429 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 430 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 431 if (s == NULL)
b34976b6 432 return FALSE;
9d19e4fd 433 htab->sdynbss = s;
252b5132 434
5474d94f
AM
435 if (bed->want_dynrelro)
436 {
437 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
438 sections. This section doesn't really need to have contents,
439 but make it like other .data.rel.ro sections. */
5474d94f 440 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 441 flags);
5474d94f
AM
442 if (s == NULL)
443 return FALSE;
444 htab->sdynrelro = s;
445 }
446
3018b441 447 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
448 normally needed. We need to create it here, though, so that the
449 linker will map it to an output section. We can't just create it
450 only if we need it, because we will not know whether we need it
451 until we have seen all the input files, and the first time the
452 main linker code calls BFD after examining all the input files
453 (size_dynamic_sections) the input sections have already been
454 mapped to the output sections. If the section turns out not to
455 be needed, we can discard it later. We will never need this
456 section when generating a shared object, since they do not use
457 copy relocs. */
9d19e4fd 458 if (bfd_link_executable (info))
3018b441 459 {
14b2f831
AM
460 s = bfd_make_section_anyway_with_flags (abfd,
461 (bed->rela_plts_and_copies_p
462 ? ".rela.bss" : ".rel.bss"),
463 flags | SEC_READONLY);
3018b441 464 if (s == NULL
45d6a902 465 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 466 return FALSE;
9d19e4fd 467 htab->srelbss = s;
5474d94f
AM
468
469 if (bed->want_dynrelro)
470 {
471 s = (bfd_make_section_anyway_with_flags
472 (abfd, (bed->rela_plts_and_copies_p
473 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
474 flags | SEC_READONLY));
475 if (s == NULL
476 || ! bfd_set_section_alignment (abfd, s,
477 bed->s->log_file_align))
478 return FALSE;
479 htab->sreldynrelro = s;
480 }
3018b441 481 }
252b5132
RH
482 }
483
b34976b6 484 return TRUE;
252b5132
RH
485}
486\f
252b5132
RH
487/* Record a new dynamic symbol. We record the dynamic symbols as we
488 read the input files, since we need to have a list of all of them
489 before we can determine the final sizes of the output sections.
490 Note that we may actually call this function even though we are not
491 going to output any dynamic symbols; in some cases we know that a
492 symbol should be in the dynamic symbol table, but only if there is
493 one. */
494
b34976b6 495bfd_boolean
c152c796
AM
496bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
497 struct elf_link_hash_entry *h)
252b5132
RH
498{
499 if (h->dynindx == -1)
500 {
2b0f7ef9 501 struct elf_strtab_hash *dynstr;
68b6ddd0 502 char *p;
252b5132 503 const char *name;
ef53be89 504 size_t indx;
252b5132 505
7a13edea
NC
506 /* XXX: The ABI draft says the linker must turn hidden and
507 internal symbols into STB_LOCAL symbols when producing the
508 DSO. However, if ld.so honors st_other in the dynamic table,
509 this would not be necessary. */
510 switch (ELF_ST_VISIBILITY (h->other))
511 {
512 case STV_INTERNAL:
513 case STV_HIDDEN:
9d6eee78
L
514 if (h->root.type != bfd_link_hash_undefined
515 && h->root.type != bfd_link_hash_undefweak)
38048eb9 516 {
f5385ebf 517 h->forced_local = 1;
67687978
PB
518 if (!elf_hash_table (info)->is_relocatable_executable)
519 return TRUE;
7a13edea 520 }
0444bdd4 521
7a13edea
NC
522 default:
523 break;
524 }
525
252b5132
RH
526 h->dynindx = elf_hash_table (info)->dynsymcount;
527 ++elf_hash_table (info)->dynsymcount;
528
529 dynstr = elf_hash_table (info)->dynstr;
530 if (dynstr == NULL)
531 {
532 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 533 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 534 if (dynstr == NULL)
b34976b6 535 return FALSE;
252b5132
RH
536 }
537
538 /* We don't put any version information in the dynamic string
aad5d350 539 table. */
252b5132
RH
540 name = h->root.root.string;
541 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
542 if (p != NULL)
543 /* We know that the p points into writable memory. In fact,
544 there are only a few symbols that have read-only names, being
545 those like _GLOBAL_OFFSET_TABLE_ that are created specially
546 by the backends. Most symbols will have names pointing into
547 an ELF string table read from a file, or to objalloc memory. */
548 *p = 0;
549
550 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
551
552 if (p != NULL)
553 *p = ELF_VER_CHR;
252b5132 554
ef53be89 555 if (indx == (size_t) -1)
b34976b6 556 return FALSE;
252b5132
RH
557 h->dynstr_index = indx;
558 }
559
b34976b6 560 return TRUE;
252b5132 561}
45d6a902 562\f
55255dae
L
563/* Mark a symbol dynamic. */
564
28caa186 565static void
55255dae 566bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
567 struct elf_link_hash_entry *h,
568 Elf_Internal_Sym *sym)
55255dae 569{
40b36307 570 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 571
40b36307 572 /* It may be called more than once on the same H. */
0e1862bb 573 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
574 return;
575
40b36307
L
576 if ((info->dynamic_data
577 && (h->type == STT_OBJECT
b8871f35 578 || h->type == STT_COMMON
40b36307 579 || (sym != NULL
b8871f35
L
580 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
581 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 582 || (d != NULL
40b36307
L
583 && h->root.type == bfd_link_hash_new
584 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
585 h->dynamic = 1;
586}
587
45d6a902
AM
588/* Record an assignment to a symbol made by a linker script. We need
589 this in case some dynamic object refers to this symbol. */
590
591bfd_boolean
fe21a8fc
L
592bfd_elf_record_link_assignment (bfd *output_bfd,
593 struct bfd_link_info *info,
268b6b39 594 const char *name,
fe21a8fc
L
595 bfd_boolean provide,
596 bfd_boolean hidden)
45d6a902 597{
00cbee0a 598 struct elf_link_hash_entry *h, *hv;
4ea42fb7 599 struct elf_link_hash_table *htab;
00cbee0a 600 const struct elf_backend_data *bed;
45d6a902 601
0eddce27 602 if (!is_elf_hash_table (info->hash))
45d6a902
AM
603 return TRUE;
604
4ea42fb7
AM
605 htab = elf_hash_table (info);
606 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 607 if (h == NULL)
4ea42fb7 608 return provide;
45d6a902 609
8e2a4f11
AM
610 if (h->root.type == bfd_link_hash_warning)
611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
612
0f550b3d
L
613 if (h->versioned == unknown)
614 {
615 /* Set versioned if symbol version is unknown. */
616 char *version = strrchr (name, ELF_VER_CHR);
617 if (version)
618 {
619 if (version > name && version[-1] != ELF_VER_CHR)
620 h->versioned = versioned_hidden;
621 else
622 h->versioned = versioned;
623 }
624 }
625
00cbee0a 626 switch (h->root.type)
77cfaee6 627 {
00cbee0a
L
628 case bfd_link_hash_defined:
629 case bfd_link_hash_defweak:
630 case bfd_link_hash_common:
631 break;
632 case bfd_link_hash_undefweak:
633 case bfd_link_hash_undefined:
634 /* Since we're defining the symbol, don't let it seem to have not
635 been defined. record_dynamic_symbol and size_dynamic_sections
636 may depend on this. */
4ea42fb7 637 h->root.type = bfd_link_hash_new;
77cfaee6
AM
638 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
639 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
640 break;
641 case bfd_link_hash_new:
40b36307 642 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 643 h->non_elf = 0;
00cbee0a
L
644 break;
645 case bfd_link_hash_indirect:
646 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 647 the versioned symbol point to this one. */
00cbee0a
L
648 bed = get_elf_backend_data (output_bfd);
649 hv = h;
650 while (hv->root.type == bfd_link_hash_indirect
651 || hv->root.type == bfd_link_hash_warning)
652 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
653 /* We don't need to update h->root.u since linker will set them
654 later. */
655 h->root.type = bfd_link_hash_undefined;
656 hv->root.type = bfd_link_hash_indirect;
657 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
658 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
659 break;
8e2a4f11
AM
660 default:
661 BFD_FAIL ();
c2596ca5 662 return FALSE;
55255dae 663 }
45d6a902
AM
664
665 /* If this symbol is being provided by the linker script, and it is
666 currently defined by a dynamic object, but not by a regular
667 object, then mark it as undefined so that the generic linker will
668 force the correct value. */
669 if (provide
f5385ebf
AM
670 && h->def_dynamic
671 && !h->def_regular)
45d6a902
AM
672 h->root.type = bfd_link_hash_undefined;
673
674 /* If this symbol is not being provided by the linker script, and it is
675 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
676 then clear out any version information because the symbol will not be
677 associated with the dynamic object any more. */
45d6a902 678 if (!provide
f5385ebf
AM
679 && h->def_dynamic
680 && !h->def_regular)
b531344c
MR
681 h->verinfo.verdef = NULL;
682
683 /* Make sure this symbol is not garbage collected. */
684 h->mark = 1;
45d6a902 685
f5385ebf 686 h->def_regular = 1;
45d6a902 687
eb8476a6 688 if (hidden)
fe21a8fc 689 {
91d6fa6a 690 bed = get_elf_backend_data (output_bfd);
b8297068
AM
691 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
692 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
694 }
695
6fa3860b
PB
696 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
697 and executables. */
0e1862bb 698 if (!bfd_link_relocatable (info)
6fa3860b
PB
699 && h->dynindx != -1
700 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
701 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
702 h->forced_local = 1;
703
f5385ebf
AM
704 if ((h->def_dynamic
705 || h->ref_dynamic
6b3b0ab8
L
706 || bfd_link_dll (info)
707 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
708 && h->dynindx == -1)
709 {
c152c796 710 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
711 return FALSE;
712
713 /* If this is a weak defined symbol, and we know a corresponding
714 real symbol from the same dynamic object, make sure the real
715 symbol is also made into a dynamic symbol. */
f6e332e6
AM
716 if (h->u.weakdef != NULL
717 && h->u.weakdef->dynindx == -1)
45d6a902 718 {
f6e332e6 719 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
720 return FALSE;
721 }
722 }
723
724 return TRUE;
725}
42751cf3 726
8c58d23b
AM
727/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
728 success, and 2 on a failure caused by attempting to record a symbol
729 in a discarded section, eg. a discarded link-once section symbol. */
730
731int
c152c796
AM
732bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
733 bfd *input_bfd,
734 long input_indx)
8c58d23b
AM
735{
736 bfd_size_type amt;
737 struct elf_link_local_dynamic_entry *entry;
738 struct elf_link_hash_table *eht;
739 struct elf_strtab_hash *dynstr;
ef53be89 740 size_t dynstr_index;
8c58d23b
AM
741 char *name;
742 Elf_External_Sym_Shndx eshndx;
743 char esym[sizeof (Elf64_External_Sym)];
744
0eddce27 745 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
746 return 0;
747
748 /* See if the entry exists already. */
749 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
750 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
751 return 1;
752
753 amt = sizeof (*entry);
a50b1753 754 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
755 if (entry == NULL)
756 return 0;
757
758 /* Go find the symbol, so that we can find it's name. */
759 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 760 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
761 {
762 bfd_release (input_bfd, entry);
763 return 0;
764 }
765
766 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 767 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
768 {
769 asection *s;
770
771 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
772 if (s == NULL || bfd_is_abs_section (s->output_section))
773 {
774 /* We can still bfd_release here as nothing has done another
775 bfd_alloc. We can't do this later in this function. */
776 bfd_release (input_bfd, entry);
777 return 2;
778 }
779 }
780
781 name = (bfd_elf_string_from_elf_section
782 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
783 entry->isym.st_name));
784
785 dynstr = elf_hash_table (info)->dynstr;
786 if (dynstr == NULL)
787 {
788 /* Create a strtab to hold the dynamic symbol names. */
789 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
790 if (dynstr == NULL)
791 return 0;
792 }
793
b34976b6 794 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 795 if (dynstr_index == (size_t) -1)
8c58d23b
AM
796 return 0;
797 entry->isym.st_name = dynstr_index;
798
799 eht = elf_hash_table (info);
800
801 entry->next = eht->dynlocal;
802 eht->dynlocal = entry;
803 entry->input_bfd = input_bfd;
804 entry->input_indx = input_indx;
805 eht->dynsymcount++;
806
807 /* Whatever binding the symbol had before, it's now local. */
808 entry->isym.st_info
809 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
810
811 /* The dynindx will be set at the end of size_dynamic_sections. */
812
813 return 1;
814}
815
30b30c21 816/* Return the dynindex of a local dynamic symbol. */
42751cf3 817
30b30c21 818long
268b6b39
AM
819_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
820 bfd *input_bfd,
821 long input_indx)
30b30c21
RH
822{
823 struct elf_link_local_dynamic_entry *e;
824
825 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
826 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
827 return e->dynindx;
828 return -1;
829}
830
831/* This function is used to renumber the dynamic symbols, if some of
832 them are removed because they are marked as local. This is called
833 via elf_link_hash_traverse. */
834
b34976b6 835static bfd_boolean
268b6b39
AM
836elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
837 void *data)
42751cf3 838{
a50b1753 839 size_t *count = (size_t *) data;
30b30c21 840
6fa3860b
PB
841 if (h->forced_local)
842 return TRUE;
843
844 if (h->dynindx != -1)
845 h->dynindx = ++(*count);
846
847 return TRUE;
848}
849
850
851/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
852 STB_LOCAL binding. */
853
854static bfd_boolean
855elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
856 void *data)
857{
a50b1753 858 size_t *count = (size_t *) data;
6fa3860b 859
6fa3860b
PB
860 if (!h->forced_local)
861 return TRUE;
862
42751cf3 863 if (h->dynindx != -1)
30b30c21
RH
864 h->dynindx = ++(*count);
865
b34976b6 866 return TRUE;
42751cf3 867}
30b30c21 868
aee6f5b4
AO
869/* Return true if the dynamic symbol for a given section should be
870 omitted when creating a shared library. */
871bfd_boolean
872_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
873 struct bfd_link_info *info,
874 asection *p)
875{
74541ad4 876 struct elf_link_hash_table *htab;
ca55926c 877 asection *ip;
74541ad4 878
aee6f5b4
AO
879 switch (elf_section_data (p)->this_hdr.sh_type)
880 {
881 case SHT_PROGBITS:
882 case SHT_NOBITS:
883 /* If sh_type is yet undecided, assume it could be
884 SHT_PROGBITS/SHT_NOBITS. */
885 case SHT_NULL:
74541ad4
AM
886 htab = elf_hash_table (info);
887 if (p == htab->tls_sec)
888 return FALSE;
889
890 if (htab->text_index_section != NULL)
891 return p != htab->text_index_section && p != htab->data_index_section;
892
ca55926c 893 return (htab->dynobj != NULL
3d4d4302 894 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 895 && ip->output_section == p);
aee6f5b4
AO
896
897 /* There shouldn't be section relative relocations
898 against any other section. */
899 default:
900 return TRUE;
901 }
902}
903
062e2358 904/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
905 symbol for each output section, which come first. Next come symbols
906 which have been forced to local binding. Then all of the back-end
907 allocated local dynamic syms, followed by the rest of the global
908 symbols. */
30b30c21 909
554220db
AM
910static unsigned long
911_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
912 struct bfd_link_info *info,
913 unsigned long *section_sym_count)
30b30c21
RH
914{
915 unsigned long dynsymcount = 0;
916
0e1862bb
L
917 if (bfd_link_pic (info)
918 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 919 {
aee6f5b4 920 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
921 asection *p;
922 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 923 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
924 && (p->flags & SEC_ALLOC) != 0
925 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
926 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
927 else
928 elf_section_data (p)->dynindx = 0;
30b30c21 929 }
554220db 930 *section_sym_count = dynsymcount;
30b30c21 931
6fa3860b
PB
932 elf_link_hash_traverse (elf_hash_table (info),
933 elf_link_renumber_local_hash_table_dynsyms,
934 &dynsymcount);
935
30b30c21
RH
936 if (elf_hash_table (info)->dynlocal)
937 {
938 struct elf_link_local_dynamic_entry *p;
939 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
940 p->dynindx = ++dynsymcount;
941 }
90ac2420 942 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
943
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_hash_table_dynsyms,
946 &dynsymcount);
947
d5486c43
L
948 /* There is an unused NULL entry at the head of the table which we
949 must account for in our count even if the table is empty since it
950 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
951 .dynamic section. */
952 dynsymcount++;
30b30c21 953
ccabcbe5
AM
954 elf_hash_table (info)->dynsymcount = dynsymcount;
955 return dynsymcount;
30b30c21 956}
252b5132 957
54ac0771
L
958/* Merge st_other field. */
959
960static void
961elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 962 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 963 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
964{
965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
966
967 /* If st_other has a processor-specific meaning, specific
cd3416da 968 code might be needed here. */
54ac0771
L
969 if (bed->elf_backend_merge_symbol_attribute)
970 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
971 dynamic);
972
cd3416da 973 if (!dynamic)
54ac0771 974 {
cd3416da
AM
975 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
976 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 977
cd3416da
AM
978 /* Keep the most constraining visibility. Leave the remainder
979 of the st_other field to elf_backend_merge_symbol_attribute. */
980 if (symvis - 1 < hvis - 1)
981 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 982 }
b8417128
AM
983 else if (definition
984 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
985 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 986 h->protected_def = 1;
54ac0771
L
987}
988
4f3fedcf
AM
989/* This function is called when we want to merge a new symbol with an
990 existing symbol. It handles the various cases which arise when we
991 find a definition in a dynamic object, or when there is already a
992 definition in a dynamic object. The new symbol is described by
993 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
994 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
995 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
996 of an old common symbol. We set OVERRIDE if the old symbol is
997 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
998 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
999 to change. By OK to change, we mean that we shouldn't warn if the
1000 type or size does change. */
45d6a902 1001
8a56bd02 1002static bfd_boolean
268b6b39
AM
1003_bfd_elf_merge_symbol (bfd *abfd,
1004 struct bfd_link_info *info,
1005 const char *name,
1006 Elf_Internal_Sym *sym,
1007 asection **psec,
1008 bfd_vma *pvalue,
4f3fedcf
AM
1009 struct elf_link_hash_entry **sym_hash,
1010 bfd **poldbfd,
37a9e49a 1011 bfd_boolean *pold_weak,
af44c138 1012 unsigned int *pold_alignment,
268b6b39
AM
1013 bfd_boolean *skip,
1014 bfd_boolean *override,
1015 bfd_boolean *type_change_ok,
6e33951e
L
1016 bfd_boolean *size_change_ok,
1017 bfd_boolean *matched)
252b5132 1018{
7479dfd4 1019 asection *sec, *oldsec;
45d6a902 1020 struct elf_link_hash_entry *h;
90c984fc 1021 struct elf_link_hash_entry *hi;
45d6a902
AM
1022 struct elf_link_hash_entry *flip;
1023 int bind;
1024 bfd *oldbfd;
1025 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1026 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1027 const struct elf_backend_data *bed;
6e33951e 1028 char *new_version;
45d6a902
AM
1029
1030 *skip = FALSE;
1031 *override = FALSE;
1032
1033 sec = *psec;
1034 bind = ELF_ST_BIND (sym->st_info);
1035
1036 if (! bfd_is_und_section (sec))
1037 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1038 else
1039 h = ((struct elf_link_hash_entry *)
1040 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1041 if (h == NULL)
1042 return FALSE;
1043 *sym_hash = h;
252b5132 1044
88ba32a0
L
1045 bed = get_elf_backend_data (abfd);
1046
6e33951e 1047 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1048 if (h->versioned != unversioned)
6e33951e 1049 {
422f1182
L
1050 /* Symbol version is unknown or versioned. */
1051 new_version = strrchr (name, ELF_VER_CHR);
1052 if (new_version)
1053 {
1054 if (h->versioned == unknown)
1055 {
1056 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1057 h->versioned = versioned_hidden;
1058 else
1059 h->versioned = versioned;
1060 }
1061 new_version += 1;
1062 if (new_version[0] == '\0')
1063 new_version = NULL;
1064 }
1065 else
1066 h->versioned = unversioned;
6e33951e 1067 }
422f1182
L
1068 else
1069 new_version = NULL;
6e33951e 1070
90c984fc
L
1071 /* For merging, we only care about real symbols. But we need to make
1072 sure that indirect symbol dynamic flags are updated. */
1073 hi = h;
45d6a902
AM
1074 while (h->root.type == bfd_link_hash_indirect
1075 || h->root.type == bfd_link_hash_warning)
1076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1077
6e33951e
L
1078 if (!*matched)
1079 {
1080 if (hi == h || h->root.type == bfd_link_hash_new)
1081 *matched = TRUE;
1082 else
1083 {
ae7683d2 1084 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1085 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1086 true if the new symbol is only visible to the symbol with
6e33951e 1087 the same symbol version. */
422f1182
L
1088 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1089 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1090 if (!old_hidden && !new_hidden)
1091 /* The new symbol matches the existing symbol if both
1092 aren't hidden. */
1093 *matched = TRUE;
1094 else
1095 {
1096 /* OLD_VERSION is the symbol version of the existing
1097 symbol. */
422f1182
L
1098 char *old_version;
1099
1100 if (h->versioned >= versioned)
1101 old_version = strrchr (h->root.root.string,
1102 ELF_VER_CHR) + 1;
1103 else
1104 old_version = NULL;
6e33951e
L
1105
1106 /* The new symbol matches the existing symbol if they
1107 have the same symbol version. */
1108 *matched = (old_version == new_version
1109 || (old_version != NULL
1110 && new_version != NULL
1111 && strcmp (old_version, new_version) == 0));
1112 }
1113 }
1114 }
1115
934bce08
AM
1116 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1117 existing symbol. */
1118
1119 oldbfd = NULL;
1120 oldsec = NULL;
1121 switch (h->root.type)
1122 {
1123 default:
1124 break;
1125
1126 case bfd_link_hash_undefined:
1127 case bfd_link_hash_undefweak:
1128 oldbfd = h->root.u.undef.abfd;
1129 break;
1130
1131 case bfd_link_hash_defined:
1132 case bfd_link_hash_defweak:
1133 oldbfd = h->root.u.def.section->owner;
1134 oldsec = h->root.u.def.section;
1135 break;
1136
1137 case bfd_link_hash_common:
1138 oldbfd = h->root.u.c.p->section->owner;
1139 oldsec = h->root.u.c.p->section;
1140 if (pold_alignment)
1141 *pold_alignment = h->root.u.c.p->alignment_power;
1142 break;
1143 }
1144 if (poldbfd && *poldbfd == NULL)
1145 *poldbfd = oldbfd;
1146
1147 /* Differentiate strong and weak symbols. */
1148 newweak = bind == STB_WEAK;
1149 oldweak = (h->root.type == bfd_link_hash_defweak
1150 || h->root.type == bfd_link_hash_undefweak);
1151 if (pold_weak)
1152 *pold_weak = oldweak;
1153
1154 /* This code is for coping with dynamic objects, and is only useful
1155 if we are doing an ELF link. */
1156 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1157 return TRUE;
1158
40b36307 1159 /* We have to check it for every instance since the first few may be
ee659f1f 1160 references and not all compilers emit symbol type for undefined
40b36307
L
1161 symbols. */
1162 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1163
ee659f1f
AM
1164 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1165 respectively, is from a dynamic object. */
1166
1167 newdyn = (abfd->flags & DYNAMIC) != 0;
1168
1169 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1170 syms and defined syms in dynamic libraries respectively.
1171 ref_dynamic on the other hand can be set for a symbol defined in
1172 a dynamic library, and def_dynamic may not be set; When the
1173 definition in a dynamic lib is overridden by a definition in the
1174 executable use of the symbol in the dynamic lib becomes a
1175 reference to the executable symbol. */
1176 if (newdyn)
1177 {
1178 if (bfd_is_und_section (sec))
1179 {
1180 if (bind != STB_WEAK)
1181 {
1182 h->ref_dynamic_nonweak = 1;
1183 hi->ref_dynamic_nonweak = 1;
1184 }
1185 }
1186 else
1187 {
6e33951e
L
1188 /* Update the existing symbol only if they match. */
1189 if (*matched)
1190 h->dynamic_def = 1;
ee659f1f
AM
1191 hi->dynamic_def = 1;
1192 }
1193 }
1194
45d6a902
AM
1195 /* If we just created the symbol, mark it as being an ELF symbol.
1196 Other than that, there is nothing to do--there is no merge issue
1197 with a newly defined symbol--so we just return. */
1198
1199 if (h->root.type == bfd_link_hash_new)
252b5132 1200 {
f5385ebf 1201 h->non_elf = 0;
45d6a902
AM
1202 return TRUE;
1203 }
252b5132 1204
45d6a902
AM
1205 /* In cases involving weak versioned symbols, we may wind up trying
1206 to merge a symbol with itself. Catch that here, to avoid the
1207 confusion that results if we try to override a symbol with
1208 itself. The additional tests catch cases like
1209 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1210 dynamic object, which we do want to handle here. */
1211 if (abfd == oldbfd
895fa45f 1212 && (newweak || oldweak)
45d6a902 1213 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1214 || !h->def_regular))
45d6a902
AM
1215 return TRUE;
1216
707bba77 1217 olddyn = FALSE;
45d6a902
AM
1218 if (oldbfd != NULL)
1219 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1220 else if (oldsec != NULL)
45d6a902 1221 {
707bba77 1222 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1223 indices used by MIPS ELF. */
707bba77 1224 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1225 }
252b5132 1226
45d6a902
AM
1227 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1228 respectively, appear to be a definition rather than reference. */
1229
707bba77 1230 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1231
707bba77
AM
1232 olddef = (h->root.type != bfd_link_hash_undefined
1233 && h->root.type != bfd_link_hash_undefweak
202ac193 1234 && h->root.type != bfd_link_hash_common);
45d6a902 1235
0a36a439
L
1236 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1237 respectively, appear to be a function. */
1238
1239 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1240 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1241
1242 oldfunc = (h->type != STT_NOTYPE
1243 && bed->is_function_type (h->type));
1244
5b677558
AM
1245 /* If creating a default indirect symbol ("foo" or "foo@") from a
1246 dynamic versioned definition ("foo@@") skip doing so if there is
1247 an existing regular definition with a different type. We don't
1248 want, for example, a "time" variable in the executable overriding
1249 a "time" function in a shared library. */
580a2b6e 1250 if (pold_alignment == NULL
580a2b6e
L
1251 && newdyn
1252 && newdef
1253 && !olddyn
5b677558
AM
1254 && (olddef || h->root.type == bfd_link_hash_common)
1255 && ELF_ST_TYPE (sym->st_info) != h->type
1256 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1257 && h->type != STT_NOTYPE
1258 && !(newfunc && oldfunc))
580a2b6e
L
1259 {
1260 *skip = TRUE;
1261 return TRUE;
1262 }
1263
4c34aff8
AM
1264 /* Check TLS symbols. We don't check undefined symbols introduced
1265 by "ld -u" which have no type (and oldbfd NULL), and we don't
1266 check symbols from plugins because they also have no type. */
1267 if (oldbfd != NULL
1268 && (oldbfd->flags & BFD_PLUGIN) == 0
1269 && (abfd->flags & BFD_PLUGIN) == 0
1270 && ELF_ST_TYPE (sym->st_info) != h->type
1271 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1272 {
1273 bfd *ntbfd, *tbfd;
1274 bfd_boolean ntdef, tdef;
1275 asection *ntsec, *tsec;
1276
1277 if (h->type == STT_TLS)
1278 {
3b36f7e6 1279 ntbfd = abfd;
7479dfd4
L
1280 ntsec = sec;
1281 ntdef = newdef;
1282 tbfd = oldbfd;
1283 tsec = oldsec;
1284 tdef = olddef;
1285 }
1286 else
1287 {
1288 ntbfd = oldbfd;
1289 ntsec = oldsec;
1290 ntdef = olddef;
1291 tbfd = abfd;
1292 tsec = sec;
1293 tdef = newdef;
1294 }
1295
1296 if (tdef && ntdef)
4eca0228 1297 _bfd_error_handler
695344c0 1298 /* xgettext:c-format */
191c0c42
AM
1299 (_("%s: TLS definition in %B section %A "
1300 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1301 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1302 else if (!tdef && !ntdef)
4eca0228 1303 _bfd_error_handler
695344c0 1304 /* xgettext:c-format */
191c0c42
AM
1305 (_("%s: TLS reference in %B "
1306 "mismatches non-TLS reference in %B"),
c08bb8dd 1307 h->root.root.string, tbfd, ntbfd);
7479dfd4 1308 else if (tdef)
4eca0228 1309 _bfd_error_handler
695344c0 1310 /* xgettext:c-format */
191c0c42
AM
1311 (_("%s: TLS definition in %B section %A "
1312 "mismatches non-TLS reference in %B"),
c08bb8dd 1313 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1314 else
4eca0228 1315 _bfd_error_handler
695344c0 1316 /* xgettext:c-format */
191c0c42
AM
1317 (_("%s: TLS reference in %B "
1318 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1319 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1320
1321 bfd_set_error (bfd_error_bad_value);
1322 return FALSE;
1323 }
1324
45d6a902
AM
1325 /* If the old symbol has non-default visibility, we ignore the new
1326 definition from a dynamic object. */
1327 if (newdyn
9c7a29a3 1328 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1329 && !bfd_is_und_section (sec))
1330 {
1331 *skip = TRUE;
1332 /* Make sure this symbol is dynamic. */
f5385ebf 1333 h->ref_dynamic = 1;
90c984fc 1334 hi->ref_dynamic = 1;
45d6a902
AM
1335 /* A protected symbol has external availability. Make sure it is
1336 recorded as dynamic.
1337
1338 FIXME: Should we check type and size for protected symbol? */
1339 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1340 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1341 else
1342 return TRUE;
1343 }
1344 else if (!newdyn
9c7a29a3 1345 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1346 && h->def_dynamic)
45d6a902
AM
1347 {
1348 /* If the new symbol with non-default visibility comes from a
1349 relocatable file and the old definition comes from a dynamic
1350 object, we remove the old definition. */
6c9b78e6 1351 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1352 {
1353 /* Handle the case where the old dynamic definition is
1354 default versioned. We need to copy the symbol info from
1355 the symbol with default version to the normal one if it
1356 was referenced before. */
1357 if (h->ref_regular)
1358 {
6c9b78e6 1359 hi->root.type = h->root.type;
d2dee3b2 1360 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1361 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1362
6c9b78e6 1363 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1364 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1365 {
aed81c4e
MR
1366 /* If the new symbol is hidden or internal, completely undo
1367 any dynamic link state. */
1368 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1369 h->forced_local = 0;
1370 h->ref_dynamic = 0;
d2dee3b2
L
1371 }
1372 else
aed81c4e
MR
1373 h->ref_dynamic = 1;
1374
1375 h->def_dynamic = 0;
aed81c4e
MR
1376 /* FIXME: Should we check type and size for protected symbol? */
1377 h->size = 0;
1378 h->type = 0;
1379
6c9b78e6 1380 h = hi;
d2dee3b2
L
1381 }
1382 else
6c9b78e6 1383 h = hi;
d2dee3b2 1384 }
1de1a317 1385
f5eda473
AM
1386 /* If the old symbol was undefined before, then it will still be
1387 on the undefs list. If the new symbol is undefined or
1388 common, we can't make it bfd_link_hash_new here, because new
1389 undefined or common symbols will be added to the undefs list
1390 by _bfd_generic_link_add_one_symbol. Symbols may not be
1391 added twice to the undefs list. Also, if the new symbol is
1392 undefweak then we don't want to lose the strong undef. */
1393 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1394 {
1de1a317 1395 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1396 h->root.u.undef.abfd = abfd;
1397 }
1398 else
1399 {
1400 h->root.type = bfd_link_hash_new;
1401 h->root.u.undef.abfd = NULL;
1402 }
1403
f5eda473 1404 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1405 {
f5eda473
AM
1406 /* If the new symbol is hidden or internal, completely undo
1407 any dynamic link state. */
1408 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1409 h->forced_local = 0;
1410 h->ref_dynamic = 0;
45d6a902 1411 }
f5eda473
AM
1412 else
1413 h->ref_dynamic = 1;
1414 h->def_dynamic = 0;
45d6a902
AM
1415 /* FIXME: Should we check type and size for protected symbol? */
1416 h->size = 0;
1417 h->type = 0;
1418 return TRUE;
1419 }
14a793b2 1420
15b43f48
AM
1421 /* If a new weak symbol definition comes from a regular file and the
1422 old symbol comes from a dynamic library, we treat the new one as
1423 strong. Similarly, an old weak symbol definition from a regular
1424 file is treated as strong when the new symbol comes from a dynamic
1425 library. Further, an old weak symbol from a dynamic library is
1426 treated as strong if the new symbol is from a dynamic library.
1427 This reflects the way glibc's ld.so works.
1428
1429 Do this before setting *type_change_ok or *size_change_ok so that
1430 we warn properly when dynamic library symbols are overridden. */
1431
1432 if (newdef && !newdyn && olddyn)
0f8a2703 1433 newweak = FALSE;
15b43f48 1434 if (olddef && newdyn)
0f8a2703
AM
1435 oldweak = FALSE;
1436
d334575b 1437 /* Allow changes between different types of function symbol. */
0a36a439 1438 if (newfunc && oldfunc)
fcb93ecf
PB
1439 *type_change_ok = TRUE;
1440
79349b09
AM
1441 /* It's OK to change the type if either the existing symbol or the
1442 new symbol is weak. A type change is also OK if the old symbol
1443 is undefined and the new symbol is defined. */
252b5132 1444
79349b09
AM
1445 if (oldweak
1446 || newweak
1447 || (newdef
1448 && h->root.type == bfd_link_hash_undefined))
1449 *type_change_ok = TRUE;
1450
1451 /* It's OK to change the size if either the existing symbol or the
1452 new symbol is weak, or if the old symbol is undefined. */
1453
1454 if (*type_change_ok
1455 || h->root.type == bfd_link_hash_undefined)
1456 *size_change_ok = TRUE;
45d6a902 1457
45d6a902
AM
1458 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1459 symbol, respectively, appears to be a common symbol in a dynamic
1460 object. If a symbol appears in an uninitialized section, and is
1461 not weak, and is not a function, then it may be a common symbol
1462 which was resolved when the dynamic object was created. We want
1463 to treat such symbols specially, because they raise special
1464 considerations when setting the symbol size: if the symbol
1465 appears as a common symbol in a regular object, and the size in
1466 the regular object is larger, we must make sure that we use the
1467 larger size. This problematic case can always be avoided in C,
1468 but it must be handled correctly when using Fortran shared
1469 libraries.
1470
1471 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1472 likewise for OLDDYNCOMMON and OLDDEF.
1473
1474 Note that this test is just a heuristic, and that it is quite
1475 possible to have an uninitialized symbol in a shared object which
1476 is really a definition, rather than a common symbol. This could
1477 lead to some minor confusion when the symbol really is a common
1478 symbol in some regular object. However, I think it will be
1479 harmless. */
1480
1481 if (newdyn
1482 && newdef
79349b09 1483 && !newweak
45d6a902
AM
1484 && (sec->flags & SEC_ALLOC) != 0
1485 && (sec->flags & SEC_LOAD) == 0
1486 && sym->st_size > 0
0a36a439 1487 && !newfunc)
45d6a902
AM
1488 newdyncommon = TRUE;
1489 else
1490 newdyncommon = FALSE;
1491
1492 if (olddyn
1493 && olddef
1494 && h->root.type == bfd_link_hash_defined
f5385ebf 1495 && h->def_dynamic
45d6a902
AM
1496 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1497 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1498 && h->size > 0
0a36a439 1499 && !oldfunc)
45d6a902
AM
1500 olddyncommon = TRUE;
1501 else
1502 olddyncommon = FALSE;
1503
a4d8e49b
L
1504 /* We now know everything about the old and new symbols. We ask the
1505 backend to check if we can merge them. */
5d13b3b3
AM
1506 if (bed->merge_symbol != NULL)
1507 {
1508 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1509 return FALSE;
1510 sec = *psec;
1511 }
a4d8e49b 1512
45d6a902
AM
1513 /* If both the old and the new symbols look like common symbols in a
1514 dynamic object, set the size of the symbol to the larger of the
1515 two. */
1516
1517 if (olddyncommon
1518 && newdyncommon
1519 && sym->st_size != h->size)
1520 {
1521 /* Since we think we have two common symbols, issue a multiple
1522 common warning if desired. Note that we only warn if the
1523 size is different. If the size is the same, we simply let
1524 the old symbol override the new one as normally happens with
1525 symbols defined in dynamic objects. */
1526
1a72702b
AM
1527 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1528 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1529 if (sym->st_size > h->size)
1530 h->size = sym->st_size;
252b5132 1531
45d6a902 1532 *size_change_ok = TRUE;
252b5132
RH
1533 }
1534
45d6a902
AM
1535 /* If we are looking at a dynamic object, and we have found a
1536 definition, we need to see if the symbol was already defined by
1537 some other object. If so, we want to use the existing
1538 definition, and we do not want to report a multiple symbol
1539 definition error; we do this by clobbering *PSEC to be
1540 bfd_und_section_ptr.
1541
1542 We treat a common symbol as a definition if the symbol in the
1543 shared library is a function, since common symbols always
1544 represent variables; this can cause confusion in principle, but
1545 any such confusion would seem to indicate an erroneous program or
1546 shared library. We also permit a common symbol in a regular
8170f769 1547 object to override a weak symbol in a shared object. */
45d6a902
AM
1548
1549 if (newdyn
1550 && newdef
77cfaee6 1551 && (olddef
45d6a902 1552 || (h->root.type == bfd_link_hash_common
8170f769 1553 && (newweak || newfunc))))
45d6a902
AM
1554 {
1555 *override = TRUE;
1556 newdef = FALSE;
1557 newdyncommon = FALSE;
252b5132 1558
45d6a902
AM
1559 *psec = sec = bfd_und_section_ptr;
1560 *size_change_ok = TRUE;
252b5132 1561
45d6a902
AM
1562 /* If we get here when the old symbol is a common symbol, then
1563 we are explicitly letting it override a weak symbol or
1564 function in a dynamic object, and we don't want to warn about
1565 a type change. If the old symbol is a defined symbol, a type
1566 change warning may still be appropriate. */
252b5132 1567
45d6a902
AM
1568 if (h->root.type == bfd_link_hash_common)
1569 *type_change_ok = TRUE;
1570 }
1571
1572 /* Handle the special case of an old common symbol merging with a
1573 new symbol which looks like a common symbol in a shared object.
1574 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1575 common symbol, and let _bfd_generic_link_add_one_symbol do the
1576 right thing. */
45d6a902
AM
1577
1578 if (newdyncommon
1579 && h->root.type == bfd_link_hash_common)
1580 {
1581 *override = TRUE;
1582 newdef = FALSE;
1583 newdyncommon = FALSE;
1584 *pvalue = sym->st_size;
a4d8e49b 1585 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1586 *size_change_ok = TRUE;
1587 }
1588
c5e2cead 1589 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1590 if (newdef && olddef && newweak)
54ac0771 1591 {
35ed3f94 1592 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1593 if (!(oldbfd != NULL
1594 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1595 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1596 {
1597 newdef = FALSE;
1598 *skip = TRUE;
1599 }
54ac0771
L
1600
1601 /* Merge st_other. If the symbol already has a dynamic index,
1602 but visibility says it should not be visible, turn it into a
1603 local symbol. */
b8417128 1604 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1605 if (h->dynindx != -1)
1606 switch (ELF_ST_VISIBILITY (h->other))
1607 {
1608 case STV_INTERNAL:
1609 case STV_HIDDEN:
1610 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1611 break;
1612 }
1613 }
c5e2cead 1614
45d6a902
AM
1615 /* If the old symbol is from a dynamic object, and the new symbol is
1616 a definition which is not from a dynamic object, then the new
1617 symbol overrides the old symbol. Symbols from regular files
1618 always take precedence over symbols from dynamic objects, even if
1619 they are defined after the dynamic object in the link.
1620
1621 As above, we again permit a common symbol in a regular object to
1622 override a definition in a shared object if the shared object
0f8a2703 1623 symbol is a function or is weak. */
45d6a902
AM
1624
1625 flip = NULL;
77cfaee6 1626 if (!newdyn
45d6a902
AM
1627 && (newdef
1628 || (bfd_is_com_section (sec)
0a36a439 1629 && (oldweak || oldfunc)))
45d6a902
AM
1630 && olddyn
1631 && olddef
f5385ebf 1632 && h->def_dynamic)
45d6a902
AM
1633 {
1634 /* Change the hash table entry to undefined, and let
1635 _bfd_generic_link_add_one_symbol do the right thing with the
1636 new definition. */
1637
1638 h->root.type = bfd_link_hash_undefined;
1639 h->root.u.undef.abfd = h->root.u.def.section->owner;
1640 *size_change_ok = TRUE;
1641
1642 olddef = FALSE;
1643 olddyncommon = FALSE;
1644
1645 /* We again permit a type change when a common symbol may be
1646 overriding a function. */
1647
1648 if (bfd_is_com_section (sec))
0a36a439
L
1649 {
1650 if (oldfunc)
1651 {
1652 /* If a common symbol overrides a function, make sure
1653 that it isn't defined dynamically nor has type
1654 function. */
1655 h->def_dynamic = 0;
1656 h->type = STT_NOTYPE;
1657 }
1658 *type_change_ok = TRUE;
1659 }
45d6a902 1660
6c9b78e6
AM
1661 if (hi->root.type == bfd_link_hash_indirect)
1662 flip = hi;
45d6a902
AM
1663 else
1664 /* This union may have been set to be non-NULL when this symbol
1665 was seen in a dynamic object. We must force the union to be
1666 NULL, so that it is correct for a regular symbol. */
1667 h->verinfo.vertree = NULL;
1668 }
1669
1670 /* Handle the special case of a new common symbol merging with an
1671 old symbol that looks like it might be a common symbol defined in
1672 a shared object. Note that we have already handled the case in
1673 which a new common symbol should simply override the definition
1674 in the shared library. */
1675
1676 if (! newdyn
1677 && bfd_is_com_section (sec)
1678 && olddyncommon)
1679 {
1680 /* It would be best if we could set the hash table entry to a
1681 common symbol, but we don't know what to use for the section
1682 or the alignment. */
1a72702b
AM
1683 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1684 bfd_link_hash_common, sym->st_size);
45d6a902 1685
4cc11e76 1686 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1687 larger, pretend that the new symbol has its size. */
1688
1689 if (h->size > *pvalue)
1690 *pvalue = h->size;
1691
af44c138
L
1692 /* We need to remember the alignment required by the symbol
1693 in the dynamic object. */
1694 BFD_ASSERT (pold_alignment);
1695 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1696
1697 olddef = FALSE;
1698 olddyncommon = FALSE;
1699
1700 h->root.type = bfd_link_hash_undefined;
1701 h->root.u.undef.abfd = h->root.u.def.section->owner;
1702
1703 *size_change_ok = TRUE;
1704 *type_change_ok = TRUE;
1705
6c9b78e6
AM
1706 if (hi->root.type == bfd_link_hash_indirect)
1707 flip = hi;
45d6a902
AM
1708 else
1709 h->verinfo.vertree = NULL;
1710 }
1711
1712 if (flip != NULL)
1713 {
1714 /* Handle the case where we had a versioned symbol in a dynamic
1715 library and now find a definition in a normal object. In this
1716 case, we make the versioned symbol point to the normal one. */
45d6a902 1717 flip->root.type = h->root.type;
00cbee0a 1718 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1719 h->root.type = bfd_link_hash_indirect;
1720 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1721 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1722 if (h->def_dynamic)
45d6a902 1723 {
f5385ebf
AM
1724 h->def_dynamic = 0;
1725 flip->ref_dynamic = 1;
45d6a902
AM
1726 }
1727 }
1728
45d6a902
AM
1729 return TRUE;
1730}
1731
1732/* This function is called to create an indirect symbol from the
1733 default for the symbol with the default version if needed. The
4f3fedcf 1734 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1735 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1736
28caa186 1737static bfd_boolean
268b6b39
AM
1738_bfd_elf_add_default_symbol (bfd *abfd,
1739 struct bfd_link_info *info,
1740 struct elf_link_hash_entry *h,
1741 const char *name,
1742 Elf_Internal_Sym *sym,
4f3fedcf
AM
1743 asection *sec,
1744 bfd_vma value,
1745 bfd **poldbfd,
e3c9d234 1746 bfd_boolean *dynsym)
45d6a902
AM
1747{
1748 bfd_boolean type_change_ok;
1749 bfd_boolean size_change_ok;
1750 bfd_boolean skip;
1751 char *shortname;
1752 struct elf_link_hash_entry *hi;
1753 struct bfd_link_hash_entry *bh;
9c5bfbb7 1754 const struct elf_backend_data *bed;
45d6a902
AM
1755 bfd_boolean collect;
1756 bfd_boolean dynamic;
e3c9d234 1757 bfd_boolean override;
45d6a902
AM
1758 char *p;
1759 size_t len, shortlen;
ffd65175 1760 asection *tmp_sec;
6e33951e 1761 bfd_boolean matched;
45d6a902 1762
422f1182
L
1763 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1764 return TRUE;
1765
45d6a902
AM
1766 /* If this symbol has a version, and it is the default version, we
1767 create an indirect symbol from the default name to the fully
1768 decorated name. This will cause external references which do not
1769 specify a version to be bound to this version of the symbol. */
1770 p = strchr (name, ELF_VER_CHR);
422f1182
L
1771 if (h->versioned == unknown)
1772 {
1773 if (p == NULL)
1774 {
1775 h->versioned = unversioned;
1776 return TRUE;
1777 }
1778 else
1779 {
1780 if (p[1] != ELF_VER_CHR)
1781 {
1782 h->versioned = versioned_hidden;
1783 return TRUE;
1784 }
1785 else
1786 h->versioned = versioned;
1787 }
1788 }
4373f8af
L
1789 else
1790 {
1791 /* PR ld/19073: We may see an unversioned definition after the
1792 default version. */
1793 if (p == NULL)
1794 return TRUE;
1795 }
45d6a902 1796
45d6a902
AM
1797 bed = get_elf_backend_data (abfd);
1798 collect = bed->collect;
1799 dynamic = (abfd->flags & DYNAMIC) != 0;
1800
1801 shortlen = p - name;
a50b1753 1802 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1803 if (shortname == NULL)
1804 return FALSE;
1805 memcpy (shortname, name, shortlen);
1806 shortname[shortlen] = '\0';
1807
1808 /* We are going to create a new symbol. Merge it with any existing
1809 symbol with this name. For the purposes of the merge, act as
1810 though we were defining the symbol we just defined, although we
1811 actually going to define an indirect symbol. */
1812 type_change_ok = FALSE;
1813 size_change_ok = FALSE;
6e33951e 1814 matched = TRUE;
ffd65175
AM
1815 tmp_sec = sec;
1816 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1817 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1818 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1819 return FALSE;
1820
1821 if (skip)
1822 goto nondefault;
1823
5b677558
AM
1824 if (hi->def_regular)
1825 {
1826 /* If the undecorated symbol will have a version added by a
1827 script different to H, then don't indirect to/from the
1828 undecorated symbol. This isn't ideal because we may not yet
1829 have seen symbol versions, if given by a script on the
1830 command line rather than via --version-script. */
1831 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1832 {
1833 bfd_boolean hide;
1834
1835 hi->verinfo.vertree
1836 = bfd_find_version_for_sym (info->version_info,
1837 hi->root.root.string, &hide);
1838 if (hi->verinfo.vertree != NULL && hide)
1839 {
1840 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1841 goto nondefault;
1842 }
1843 }
1844 if (hi->verinfo.vertree != NULL
1845 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1846 goto nondefault;
1847 }
1848
45d6a902
AM
1849 if (! override)
1850 {
c6e8a9a8 1851 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1852 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1853 {
1854 bh = &hi->root;
1855 if (! (_bfd_generic_link_add_one_symbol
1856 (info, abfd, shortname, BSF_INDIRECT,
1857 bfd_ind_section_ptr,
1858 0, name, FALSE, collect, &bh)))
1859 return FALSE;
1860 hi = (struct elf_link_hash_entry *) bh;
1861 }
45d6a902
AM
1862 }
1863 else
1864 {
1865 /* In this case the symbol named SHORTNAME is overriding the
1866 indirect symbol we want to add. We were planning on making
1867 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1868 is the name without a version. NAME is the fully versioned
1869 name, and it is the default version.
1870
1871 Overriding means that we already saw a definition for the
1872 symbol SHORTNAME in a regular object, and it is overriding
1873 the symbol defined in the dynamic object.
1874
1875 When this happens, we actually want to change NAME, the
1876 symbol we just added, to refer to SHORTNAME. This will cause
1877 references to NAME in the shared object to become references
1878 to SHORTNAME in the regular object. This is what we expect
1879 when we override a function in a shared object: that the
1880 references in the shared object will be mapped to the
1881 definition in the regular object. */
1882
1883 while (hi->root.type == bfd_link_hash_indirect
1884 || hi->root.type == bfd_link_hash_warning)
1885 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1886
1887 h->root.type = bfd_link_hash_indirect;
1888 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1889 if (h->def_dynamic)
45d6a902 1890 {
f5385ebf
AM
1891 h->def_dynamic = 0;
1892 hi->ref_dynamic = 1;
1893 if (hi->ref_regular
1894 || hi->def_regular)
45d6a902 1895 {
c152c796 1896 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1897 return FALSE;
1898 }
1899 }
1900
1901 /* Now set HI to H, so that the following code will set the
1902 other fields correctly. */
1903 hi = h;
1904 }
1905
fab4a87f
L
1906 /* Check if HI is a warning symbol. */
1907 if (hi->root.type == bfd_link_hash_warning)
1908 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1909
45d6a902
AM
1910 /* If there is a duplicate definition somewhere, then HI may not
1911 point to an indirect symbol. We will have reported an error to
1912 the user in that case. */
1913
1914 if (hi->root.type == bfd_link_hash_indirect)
1915 {
1916 struct elf_link_hash_entry *ht;
1917
45d6a902 1918 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1919 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1920
68c88cd4
AM
1921 /* A reference to the SHORTNAME symbol from a dynamic library
1922 will be satisfied by the versioned symbol at runtime. In
1923 effect, we have a reference to the versioned symbol. */
1924 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1925 hi->dynamic_def |= ht->dynamic_def;
1926
45d6a902
AM
1927 /* See if the new flags lead us to realize that the symbol must
1928 be dynamic. */
1929 if (! *dynsym)
1930 {
1931 if (! dynamic)
1932 {
0e1862bb 1933 if (! bfd_link_executable (info)
90c984fc 1934 || hi->def_dynamic
f5385ebf 1935 || hi->ref_dynamic)
45d6a902
AM
1936 *dynsym = TRUE;
1937 }
1938 else
1939 {
f5385ebf 1940 if (hi->ref_regular)
45d6a902
AM
1941 *dynsym = TRUE;
1942 }
1943 }
1944 }
1945
1946 /* We also need to define an indirection from the nondefault version
1947 of the symbol. */
1948
1949nondefault:
1950 len = strlen (name);
a50b1753 1951 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1952 if (shortname == NULL)
1953 return FALSE;
1954 memcpy (shortname, name, shortlen);
1955 memcpy (shortname + shortlen, p + 1, len - shortlen);
1956
1957 /* Once again, merge with any existing symbol. */
1958 type_change_ok = FALSE;
1959 size_change_ok = FALSE;
ffd65175
AM
1960 tmp_sec = sec;
1961 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1962 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1963 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1964 return FALSE;
1965
1966 if (skip)
1967 return TRUE;
1968
1969 if (override)
1970 {
1971 /* Here SHORTNAME is a versioned name, so we don't expect to see
1972 the type of override we do in the case above unless it is
4cc11e76 1973 overridden by a versioned definition. */
45d6a902
AM
1974 if (hi->root.type != bfd_link_hash_defined
1975 && hi->root.type != bfd_link_hash_defweak)
4eca0228 1976 _bfd_error_handler
695344c0 1977 /* xgettext:c-format */
d003868e
AM
1978 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1979 abfd, shortname);
45d6a902
AM
1980 }
1981 else
1982 {
1983 bh = &hi->root;
1984 if (! (_bfd_generic_link_add_one_symbol
1985 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1986 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1987 return FALSE;
1988 hi = (struct elf_link_hash_entry *) bh;
1989
1990 /* If there is a duplicate definition somewhere, then HI may not
1991 point to an indirect symbol. We will have reported an error
1992 to the user in that case. */
1993
1994 if (hi->root.type == bfd_link_hash_indirect)
1995 {
fcfa13d2 1996 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1997 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1998 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1999
2000 /* See if the new flags lead us to realize that the symbol
2001 must be dynamic. */
2002 if (! *dynsym)
2003 {
2004 if (! dynamic)
2005 {
0e1862bb 2006 if (! bfd_link_executable (info)
f5385ebf 2007 || hi->ref_dynamic)
45d6a902
AM
2008 *dynsym = TRUE;
2009 }
2010 else
2011 {
f5385ebf 2012 if (hi->ref_regular)
45d6a902
AM
2013 *dynsym = TRUE;
2014 }
2015 }
2016 }
2017 }
2018
2019 return TRUE;
2020}
2021\f
2022/* This routine is used to export all defined symbols into the dynamic
2023 symbol table. It is called via elf_link_hash_traverse. */
2024
28caa186 2025static bfd_boolean
268b6b39 2026_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2027{
a50b1753 2028 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2029
2030 /* Ignore indirect symbols. These are added by the versioning code. */
2031 if (h->root.type == bfd_link_hash_indirect)
2032 return TRUE;
2033
7686d77d
AM
2034 /* Ignore this if we won't export it. */
2035 if (!eif->info->export_dynamic && !h->dynamic)
2036 return TRUE;
45d6a902
AM
2037
2038 if (h->dynindx == -1
fd91d419
L
2039 && (h->def_regular || h->ref_regular)
2040 && ! bfd_hide_sym_by_version (eif->info->version_info,
2041 h->root.root.string))
45d6a902 2042 {
fd91d419 2043 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2044 {
fd91d419
L
2045 eif->failed = TRUE;
2046 return FALSE;
45d6a902
AM
2047 }
2048 }
2049
2050 return TRUE;
2051}
2052\f
2053/* Look through the symbols which are defined in other shared
2054 libraries and referenced here. Update the list of version
2055 dependencies. This will be put into the .gnu.version_r section.
2056 This function is called via elf_link_hash_traverse. */
2057
28caa186 2058static bfd_boolean
268b6b39
AM
2059_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2060 void *data)
45d6a902 2061{
a50b1753 2062 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2063 Elf_Internal_Verneed *t;
2064 Elf_Internal_Vernaux *a;
2065 bfd_size_type amt;
2066
45d6a902
AM
2067 /* We only care about symbols defined in shared objects with version
2068 information. */
f5385ebf
AM
2069 if (!h->def_dynamic
2070 || h->def_regular
45d6a902 2071 || h->dynindx == -1
7b20f099
AM
2072 || h->verinfo.verdef == NULL
2073 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2074 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2075 return TRUE;
2076
2077 /* See if we already know about this version. */
28caa186
AM
2078 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2079 t != NULL;
2080 t = t->vn_nextref)
45d6a902
AM
2081 {
2082 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2083 continue;
2084
2085 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2086 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2087 return TRUE;
2088
2089 break;
2090 }
2091
2092 /* This is a new version. Add it to tree we are building. */
2093
2094 if (t == NULL)
2095 {
2096 amt = sizeof *t;
a50b1753 2097 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2098 if (t == NULL)
2099 {
2100 rinfo->failed = TRUE;
2101 return FALSE;
2102 }
2103
2104 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2105 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2106 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2107 }
2108
2109 amt = sizeof *a;
a50b1753 2110 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2111 if (a == NULL)
2112 {
2113 rinfo->failed = TRUE;
2114 return FALSE;
2115 }
45d6a902
AM
2116
2117 /* Note that we are copying a string pointer here, and testing it
2118 above. If bfd_elf_string_from_elf_section is ever changed to
2119 discard the string data when low in memory, this will have to be
2120 fixed. */
2121 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2122
2123 a->vna_flags = h->verinfo.verdef->vd_flags;
2124 a->vna_nextptr = t->vn_auxptr;
2125
2126 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2127 ++rinfo->vers;
2128
2129 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2130
2131 t->vn_auxptr = a;
2132
2133 return TRUE;
2134}
2135
2136/* Figure out appropriate versions for all the symbols. We may not
2137 have the version number script until we have read all of the input
2138 files, so until that point we don't know which symbols should be
2139 local. This function is called via elf_link_hash_traverse. */
2140
28caa186 2141static bfd_boolean
268b6b39 2142_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2143{
28caa186 2144 struct elf_info_failed *sinfo;
45d6a902 2145 struct bfd_link_info *info;
9c5bfbb7 2146 const struct elf_backend_data *bed;
45d6a902
AM
2147 struct elf_info_failed eif;
2148 char *p;
45d6a902 2149
a50b1753 2150 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2151 info = sinfo->info;
2152
45d6a902
AM
2153 /* Fix the symbol flags. */
2154 eif.failed = FALSE;
2155 eif.info = info;
2156 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2157 {
2158 if (eif.failed)
2159 sinfo->failed = TRUE;
2160 return FALSE;
2161 }
2162
2163 /* We only need version numbers for symbols defined in regular
2164 objects. */
f5385ebf 2165 if (!h->def_regular)
45d6a902
AM
2166 return TRUE;
2167
28caa186 2168 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2169 p = strchr (h->root.root.string, ELF_VER_CHR);
2170 if (p != NULL && h->verinfo.vertree == NULL)
2171 {
2172 struct bfd_elf_version_tree *t;
45d6a902 2173
45d6a902
AM
2174 ++p;
2175 if (*p == ELF_VER_CHR)
6e33951e 2176 ++p;
45d6a902
AM
2177
2178 /* If there is no version string, we can just return out. */
2179 if (*p == '\0')
6e33951e 2180 return TRUE;
45d6a902
AM
2181
2182 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2183 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2184 {
2185 if (strcmp (t->name, p) == 0)
2186 {
2187 size_t len;
2188 char *alc;
2189 struct bfd_elf_version_expr *d;
2190
2191 len = p - h->root.root.string;
a50b1753 2192 alc = (char *) bfd_malloc (len);
45d6a902 2193 if (alc == NULL)
14b1c01e
AM
2194 {
2195 sinfo->failed = TRUE;
2196 return FALSE;
2197 }
45d6a902
AM
2198 memcpy (alc, h->root.root.string, len - 1);
2199 alc[len - 1] = '\0';
2200 if (alc[len - 2] == ELF_VER_CHR)
2201 alc[len - 2] = '\0';
2202
2203 h->verinfo.vertree = t;
2204 t->used = TRUE;
2205 d = NULL;
2206
108ba305
JJ
2207 if (t->globals.list != NULL)
2208 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2209
2210 /* See if there is anything to force this symbol to
2211 local scope. */
108ba305 2212 if (d == NULL && t->locals.list != NULL)
45d6a902 2213 {
108ba305
JJ
2214 d = (*t->match) (&t->locals, NULL, alc);
2215 if (d != NULL
2216 && h->dynindx != -1
108ba305
JJ
2217 && ! info->export_dynamic)
2218 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2219 }
2220
2221 free (alc);
2222 break;
2223 }
2224 }
2225
2226 /* If we are building an application, we need to create a
2227 version node for this version. */
0e1862bb 2228 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2229 {
2230 struct bfd_elf_version_tree **pp;
2231 int version_index;
2232
2233 /* If we aren't going to export this symbol, we don't need
2234 to worry about it. */
2235 if (h->dynindx == -1)
2236 return TRUE;
2237
ef53be89
AM
2238 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2239 sizeof *t);
45d6a902
AM
2240 if (t == NULL)
2241 {
2242 sinfo->failed = TRUE;
2243 return FALSE;
2244 }
2245
45d6a902 2246 t->name = p;
45d6a902
AM
2247 t->name_indx = (unsigned int) -1;
2248 t->used = TRUE;
2249
2250 version_index = 1;
2251 /* Don't count anonymous version tag. */
fd91d419
L
2252 if (sinfo->info->version_info != NULL
2253 && sinfo->info->version_info->vernum == 0)
45d6a902 2254 version_index = 0;
fd91d419
L
2255 for (pp = &sinfo->info->version_info;
2256 *pp != NULL;
2257 pp = &(*pp)->next)
45d6a902
AM
2258 ++version_index;
2259 t->vernum = version_index;
2260
2261 *pp = t;
2262
2263 h->verinfo.vertree = t;
2264 }
2265 else if (t == NULL)
2266 {
2267 /* We could not find the version for a symbol when
2268 generating a shared archive. Return an error. */
4eca0228 2269 _bfd_error_handler
695344c0 2270 /* xgettext:c-format */
c55fe096 2271 (_("%B: version node not found for symbol %s"),
28caa186 2272 info->output_bfd, h->root.root.string);
45d6a902
AM
2273 bfd_set_error (bfd_error_bad_value);
2274 sinfo->failed = TRUE;
2275 return FALSE;
2276 }
45d6a902
AM
2277 }
2278
2279 /* If we don't have a version for this symbol, see if we can find
2280 something. */
fd91d419 2281 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2282 {
1e8fa21e 2283 bfd_boolean hide;
ae5a3597 2284
fd91d419
L
2285 h->verinfo.vertree
2286 = bfd_find_version_for_sym (sinfo->info->version_info,
2287 h->root.root.string, &hide);
1e8fa21e
AM
2288 if (h->verinfo.vertree != NULL && hide)
2289 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2290 }
2291
2292 return TRUE;
2293}
2294\f
45d6a902
AM
2295/* Read and swap the relocs from the section indicated by SHDR. This
2296 may be either a REL or a RELA section. The relocations are
2297 translated into RELA relocations and stored in INTERNAL_RELOCS,
2298 which should have already been allocated to contain enough space.
2299 The EXTERNAL_RELOCS are a buffer where the external form of the
2300 relocations should be stored.
2301
2302 Returns FALSE if something goes wrong. */
2303
2304static bfd_boolean
268b6b39 2305elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2306 asection *sec,
268b6b39
AM
2307 Elf_Internal_Shdr *shdr,
2308 void *external_relocs,
2309 Elf_Internal_Rela *internal_relocs)
45d6a902 2310{
9c5bfbb7 2311 const struct elf_backend_data *bed;
268b6b39 2312 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2313 const bfd_byte *erela;
2314 const bfd_byte *erelaend;
2315 Elf_Internal_Rela *irela;
243ef1e0
L
2316 Elf_Internal_Shdr *symtab_hdr;
2317 size_t nsyms;
45d6a902 2318
45d6a902
AM
2319 /* Position ourselves at the start of the section. */
2320 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2321 return FALSE;
2322
2323 /* Read the relocations. */
2324 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2325 return FALSE;
2326
243ef1e0 2327 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2328 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2329
45d6a902
AM
2330 bed = get_elf_backend_data (abfd);
2331
2332 /* Convert the external relocations to the internal format. */
2333 if (shdr->sh_entsize == bed->s->sizeof_rel)
2334 swap_in = bed->s->swap_reloc_in;
2335 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2336 swap_in = bed->s->swap_reloca_in;
2337 else
2338 {
2339 bfd_set_error (bfd_error_wrong_format);
2340 return FALSE;
2341 }
2342
a50b1753 2343 erela = (const bfd_byte *) external_relocs;
51992aec 2344 erelaend = erela + shdr->sh_size;
45d6a902
AM
2345 irela = internal_relocs;
2346 while (erela < erelaend)
2347 {
243ef1e0
L
2348 bfd_vma r_symndx;
2349
45d6a902 2350 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2351 r_symndx = ELF32_R_SYM (irela->r_info);
2352 if (bed->s->arch_size == 64)
2353 r_symndx >>= 24;
ce98a316
NC
2354 if (nsyms > 0)
2355 {
2356 if ((size_t) r_symndx >= nsyms)
2357 {
4eca0228 2358 _bfd_error_handler
695344c0 2359 /* xgettext:c-format */
ce98a316
NC
2360 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2361 " for offset 0x%lx in section `%A'"),
c08bb8dd
AM
2362 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2363 irela->r_offset, sec);
ce98a316
NC
2364 bfd_set_error (bfd_error_bad_value);
2365 return FALSE;
2366 }
2367 }
cf35638d 2368 else if (r_symndx != STN_UNDEF)
243ef1e0 2369 {
4eca0228 2370 _bfd_error_handler
695344c0 2371 /* xgettext:c-format */
c08bb8dd
AM
2372 (_("%B: non-zero symbol index (0x%lx)"
2373 " for offset 0x%lx in section `%A'"
ce98a316 2374 " when the object file has no symbol table"),
c08bb8dd
AM
2375 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2376 irela->r_offset, sec);
243ef1e0
L
2377 bfd_set_error (bfd_error_bad_value);
2378 return FALSE;
2379 }
45d6a902
AM
2380 irela += bed->s->int_rels_per_ext_rel;
2381 erela += shdr->sh_entsize;
2382 }
2383
2384 return TRUE;
2385}
2386
2387/* Read and swap the relocs for a section O. They may have been
2388 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2389 not NULL, they are used as buffers to read into. They are known to
2390 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2391 the return value is allocated using either malloc or bfd_alloc,
2392 according to the KEEP_MEMORY argument. If O has two relocation
2393 sections (both REL and RELA relocations), then the REL_HDR
2394 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2395 RELA_HDR relocations. */
45d6a902
AM
2396
2397Elf_Internal_Rela *
268b6b39
AM
2398_bfd_elf_link_read_relocs (bfd *abfd,
2399 asection *o,
2400 void *external_relocs,
2401 Elf_Internal_Rela *internal_relocs,
2402 bfd_boolean keep_memory)
45d6a902 2403{
268b6b39 2404 void *alloc1 = NULL;
45d6a902 2405 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2406 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2407 struct bfd_elf_section_data *esdo = elf_section_data (o);
2408 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2409
d4730f92
BS
2410 if (esdo->relocs != NULL)
2411 return esdo->relocs;
45d6a902
AM
2412
2413 if (o->reloc_count == 0)
2414 return NULL;
2415
45d6a902
AM
2416 if (internal_relocs == NULL)
2417 {
2418 bfd_size_type size;
2419
2420 size = o->reloc_count;
2421 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2422 if (keep_memory)
a50b1753 2423 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2424 else
a50b1753 2425 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2426 if (internal_relocs == NULL)
2427 goto error_return;
2428 }
2429
2430 if (external_relocs == NULL)
2431 {
d4730f92
BS
2432 bfd_size_type size = 0;
2433
2434 if (esdo->rel.hdr)
2435 size += esdo->rel.hdr->sh_size;
2436 if (esdo->rela.hdr)
2437 size += esdo->rela.hdr->sh_size;
45d6a902 2438
268b6b39 2439 alloc1 = bfd_malloc (size);
45d6a902
AM
2440 if (alloc1 == NULL)
2441 goto error_return;
2442 external_relocs = alloc1;
2443 }
2444
d4730f92
BS
2445 internal_rela_relocs = internal_relocs;
2446 if (esdo->rel.hdr)
2447 {
2448 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2449 external_relocs,
2450 internal_relocs))
2451 goto error_return;
2452 external_relocs = (((bfd_byte *) external_relocs)
2453 + esdo->rel.hdr->sh_size);
2454 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2455 * bed->s->int_rels_per_ext_rel);
2456 }
2457
2458 if (esdo->rela.hdr
2459 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2460 external_relocs,
2461 internal_rela_relocs)))
45d6a902
AM
2462 goto error_return;
2463
2464 /* Cache the results for next time, if we can. */
2465 if (keep_memory)
d4730f92 2466 esdo->relocs = internal_relocs;
45d6a902
AM
2467
2468 if (alloc1 != NULL)
2469 free (alloc1);
2470
2471 /* Don't free alloc2, since if it was allocated we are passing it
2472 back (under the name of internal_relocs). */
2473
2474 return internal_relocs;
2475
2476 error_return:
2477 if (alloc1 != NULL)
2478 free (alloc1);
2479 if (alloc2 != NULL)
4dd07732
AM
2480 {
2481 if (keep_memory)
2482 bfd_release (abfd, alloc2);
2483 else
2484 free (alloc2);
2485 }
45d6a902
AM
2486 return NULL;
2487}
2488
2489/* Compute the size of, and allocate space for, REL_HDR which is the
2490 section header for a section containing relocations for O. */
2491
28caa186 2492static bfd_boolean
9eaff861
AO
2493_bfd_elf_link_size_reloc_section (bfd *abfd,
2494 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2495{
9eaff861 2496 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2497
2498 /* That allows us to calculate the size of the section. */
9eaff861 2499 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2500
2501 /* The contents field must last into write_object_contents, so we
2502 allocate it with bfd_alloc rather than malloc. Also since we
2503 cannot be sure that the contents will actually be filled in,
2504 we zero the allocated space. */
a50b1753 2505 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2506 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2507 return FALSE;
2508
d4730f92 2509 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2510 {
2511 struct elf_link_hash_entry **p;
2512
ca4be51c
AM
2513 p = ((struct elf_link_hash_entry **)
2514 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2515 if (p == NULL)
2516 return FALSE;
2517
d4730f92 2518 reldata->hashes = p;
45d6a902
AM
2519 }
2520
2521 return TRUE;
2522}
2523
2524/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2525 originated from the section given by INPUT_REL_HDR) to the
2526 OUTPUT_BFD. */
2527
2528bfd_boolean
268b6b39
AM
2529_bfd_elf_link_output_relocs (bfd *output_bfd,
2530 asection *input_section,
2531 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2532 Elf_Internal_Rela *internal_relocs,
2533 struct elf_link_hash_entry **rel_hash
2534 ATTRIBUTE_UNUSED)
45d6a902
AM
2535{
2536 Elf_Internal_Rela *irela;
2537 Elf_Internal_Rela *irelaend;
2538 bfd_byte *erel;
d4730f92 2539 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2540 asection *output_section;
9c5bfbb7 2541 const struct elf_backend_data *bed;
268b6b39 2542 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2543 struct bfd_elf_section_data *esdo;
45d6a902
AM
2544
2545 output_section = input_section->output_section;
45d6a902 2546
d4730f92
BS
2547 bed = get_elf_backend_data (output_bfd);
2548 esdo = elf_section_data (output_section);
2549 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2550 {
d4730f92
BS
2551 output_reldata = &esdo->rel;
2552 swap_out = bed->s->swap_reloc_out;
45d6a902 2553 }
d4730f92
BS
2554 else if (esdo->rela.hdr
2555 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2556 {
d4730f92
BS
2557 output_reldata = &esdo->rela;
2558 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2559 }
2560 else
2561 {
4eca0228 2562 _bfd_error_handler
695344c0 2563 /* xgettext:c-format */
d003868e
AM
2564 (_("%B: relocation size mismatch in %B section %A"),
2565 output_bfd, input_section->owner, input_section);
297d8443 2566 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2567 return FALSE;
2568 }
2569
d4730f92
BS
2570 erel = output_reldata->hdr->contents;
2571 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2572 irela = internal_relocs;
2573 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2574 * bed->s->int_rels_per_ext_rel);
2575 while (irela < irelaend)
2576 {
2577 (*swap_out) (output_bfd, irela, erel);
2578 irela += bed->s->int_rels_per_ext_rel;
2579 erel += input_rel_hdr->sh_entsize;
2580 }
2581
2582 /* Bump the counter, so that we know where to add the next set of
2583 relocations. */
d4730f92 2584 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2585
2586 return TRUE;
2587}
2588\f
508c3946
L
2589/* Make weak undefined symbols in PIE dynamic. */
2590
2591bfd_boolean
2592_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2593 struct elf_link_hash_entry *h)
2594{
0e1862bb 2595 if (bfd_link_pie (info)
508c3946
L
2596 && h->dynindx == -1
2597 && h->root.type == bfd_link_hash_undefweak)
2598 return bfd_elf_link_record_dynamic_symbol (info, h);
2599
2600 return TRUE;
2601}
2602
45d6a902
AM
2603/* Fix up the flags for a symbol. This handles various cases which
2604 can only be fixed after all the input files are seen. This is
2605 currently called by both adjust_dynamic_symbol and
2606 assign_sym_version, which is unnecessary but perhaps more robust in
2607 the face of future changes. */
2608
28caa186 2609static bfd_boolean
268b6b39
AM
2610_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2611 struct elf_info_failed *eif)
45d6a902 2612{
33774f08 2613 const struct elf_backend_data *bed;
508c3946 2614
45d6a902
AM
2615 /* If this symbol was mentioned in a non-ELF file, try to set
2616 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2617 permit a non-ELF file to correctly refer to a symbol defined in
2618 an ELF dynamic object. */
f5385ebf 2619 if (h->non_elf)
45d6a902
AM
2620 {
2621 while (h->root.type == bfd_link_hash_indirect)
2622 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2623
2624 if (h->root.type != bfd_link_hash_defined
2625 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2626 {
2627 h->ref_regular = 1;
2628 h->ref_regular_nonweak = 1;
2629 }
45d6a902
AM
2630 else
2631 {
2632 if (h->root.u.def.section->owner != NULL
2633 && (bfd_get_flavour (h->root.u.def.section->owner)
2634 == bfd_target_elf_flavour))
f5385ebf
AM
2635 {
2636 h->ref_regular = 1;
2637 h->ref_regular_nonweak = 1;
2638 }
45d6a902 2639 else
f5385ebf 2640 h->def_regular = 1;
45d6a902
AM
2641 }
2642
2643 if (h->dynindx == -1
f5385ebf
AM
2644 && (h->def_dynamic
2645 || h->ref_dynamic))
45d6a902 2646 {
c152c796 2647 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2648 {
2649 eif->failed = TRUE;
2650 return FALSE;
2651 }
2652 }
2653 }
2654 else
2655 {
f5385ebf 2656 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2657 was first seen in a non-ELF file. Fortunately, if the symbol
2658 was first seen in an ELF file, we're probably OK unless the
2659 symbol was defined in a non-ELF file. Catch that case here.
2660 FIXME: We're still in trouble if the symbol was first seen in
2661 a dynamic object, and then later in a non-ELF regular object. */
2662 if ((h->root.type == bfd_link_hash_defined
2663 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2664 && !h->def_regular
45d6a902
AM
2665 && (h->root.u.def.section->owner != NULL
2666 ? (bfd_get_flavour (h->root.u.def.section->owner)
2667 != bfd_target_elf_flavour)
2668 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2669 && !h->def_dynamic)))
2670 h->def_regular = 1;
45d6a902
AM
2671 }
2672
508c3946 2673 /* Backend specific symbol fixup. */
33774f08
AM
2674 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2675 if (bed->elf_backend_fixup_symbol
2676 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2677 return FALSE;
508c3946 2678
45d6a902
AM
2679 /* If this is a final link, and the symbol was defined as a common
2680 symbol in a regular object file, and there was no definition in
2681 any dynamic object, then the linker will have allocated space for
f5385ebf 2682 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2683 flag will not have been set. */
2684 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2685 && !h->def_regular
2686 && h->ref_regular
2687 && !h->def_dynamic
96f29d96 2688 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2689 h->def_regular = 1;
45d6a902 2690
4deb8f71
L
2691 /* If a weak undefined symbol has non-default visibility, we also
2692 hide it from the dynamic linker. */
2693 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2694 && h->root.type == bfd_link_hash_undefweak)
2695 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2696
2697 /* A hidden versioned symbol in executable should be forced local if
2698 it is is locally defined, not referenced by shared library and not
2699 exported. */
2700 else if (bfd_link_executable (eif->info)
2701 && h->versioned == versioned_hidden
2702 && !eif->info->export_dynamic
2703 && !h->dynamic
2704 && !h->ref_dynamic
2705 && h->def_regular)
2706 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2707
45d6a902
AM
2708 /* If -Bsymbolic was used (which means to bind references to global
2709 symbols to the definition within the shared object), and this
2710 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2711 need a PLT entry. Likewise, if the symbol has non-default
2712 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2713 will force it local. */
4deb8f71
L
2714 else if (h->needs_plt
2715 && bfd_link_pic (eif->info)
2716 && is_elf_hash_table (eif->info->hash)
2717 && (SYMBOLIC_BIND (eif->info, h)
2718 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2719 && h->def_regular)
45d6a902 2720 {
45d6a902
AM
2721 bfd_boolean force_local;
2722
45d6a902
AM
2723 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2724 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2725 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2726 }
2727
45d6a902
AM
2728 /* If this is a weak defined symbol in a dynamic object, and we know
2729 the real definition in the dynamic object, copy interesting flags
2730 over to the real definition. */
f6e332e6 2731 if (h->u.weakdef != NULL)
45d6a902 2732 {
45d6a902
AM
2733 /* If the real definition is defined by a regular object file,
2734 don't do anything special. See the longer description in
2735 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2736 if (h->u.weakdef->def_regular)
f6e332e6 2737 h->u.weakdef = NULL;
45d6a902 2738 else
a26587ba 2739 {
4e6b54a6
AM
2740 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2741
2742 while (h->root.type == bfd_link_hash_indirect)
2743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2744
2745 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2746 || h->root.type == bfd_link_hash_defweak);
2747 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2748 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2749 || weakdef->root.type == bfd_link_hash_defweak);
2750 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2751 }
45d6a902
AM
2752 }
2753
2754 return TRUE;
2755}
2756
2757/* Make the backend pick a good value for a dynamic symbol. This is
2758 called via elf_link_hash_traverse, and also calls itself
2759 recursively. */
2760
28caa186 2761static bfd_boolean
268b6b39 2762_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2763{
a50b1753 2764 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2765 bfd *dynobj;
9c5bfbb7 2766 const struct elf_backend_data *bed;
45d6a902 2767
0eddce27 2768 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2769 return FALSE;
2770
45d6a902
AM
2771 /* Ignore indirect symbols. These are added by the versioning code. */
2772 if (h->root.type == bfd_link_hash_indirect)
2773 return TRUE;
2774
2775 /* Fix the symbol flags. */
2776 if (! _bfd_elf_fix_symbol_flags (h, eif))
2777 return FALSE;
2778
2779 /* If this symbol does not require a PLT entry, and it is not
2780 defined by a dynamic object, or is not referenced by a regular
2781 object, ignore it. We do have to handle a weak defined symbol,
2782 even if no regular object refers to it, if we decided to add it
2783 to the dynamic symbol table. FIXME: Do we normally need to worry
2784 about symbols which are defined by one dynamic object and
2785 referenced by another one? */
f5385ebf 2786 if (!h->needs_plt
91e21fb7 2787 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2788 && (h->def_regular
2789 || !h->def_dynamic
2790 || (!h->ref_regular
f6e332e6 2791 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2792 {
a6aa5195 2793 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2794 return TRUE;
2795 }
2796
2797 /* If we've already adjusted this symbol, don't do it again. This
2798 can happen via a recursive call. */
f5385ebf 2799 if (h->dynamic_adjusted)
45d6a902
AM
2800 return TRUE;
2801
2802 /* Don't look at this symbol again. Note that we must set this
2803 after checking the above conditions, because we may look at a
2804 symbol once, decide not to do anything, and then get called
2805 recursively later after REF_REGULAR is set below. */
f5385ebf 2806 h->dynamic_adjusted = 1;
45d6a902
AM
2807
2808 /* If this is a weak definition, and we know a real definition, and
2809 the real symbol is not itself defined by a regular object file,
2810 then get a good value for the real definition. We handle the
2811 real symbol first, for the convenience of the backend routine.
2812
2813 Note that there is a confusing case here. If the real definition
2814 is defined by a regular object file, we don't get the real symbol
2815 from the dynamic object, but we do get the weak symbol. If the
2816 processor backend uses a COPY reloc, then if some routine in the
2817 dynamic object changes the real symbol, we will not see that
2818 change in the corresponding weak symbol. This is the way other
2819 ELF linkers work as well, and seems to be a result of the shared
2820 library model.
2821
2822 I will clarify this issue. Most SVR4 shared libraries define the
2823 variable _timezone and define timezone as a weak synonym. The
2824 tzset call changes _timezone. If you write
2825 extern int timezone;
2826 int _timezone = 5;
2827 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2828 you might expect that, since timezone is a synonym for _timezone,
2829 the same number will print both times. However, if the processor
2830 backend uses a COPY reloc, then actually timezone will be copied
2831 into your process image, and, since you define _timezone
2832 yourself, _timezone will not. Thus timezone and _timezone will
2833 wind up at different memory locations. The tzset call will set
2834 _timezone, leaving timezone unchanged. */
2835
f6e332e6 2836 if (h->u.weakdef != NULL)
45d6a902 2837 {
ec24dc88
AM
2838 /* If we get to this point, there is an implicit reference to
2839 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2840 h->u.weakdef->ref_regular = 1;
45d6a902 2841
ec24dc88
AM
2842 /* Ensure that the backend adjust_dynamic_symbol function sees
2843 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2844 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2845 return FALSE;
2846 }
2847
2848 /* If a symbol has no type and no size and does not require a PLT
2849 entry, then we are probably about to do the wrong thing here: we
2850 are probably going to create a COPY reloc for an empty object.
2851 This case can arise when a shared object is built with assembly
2852 code, and the assembly code fails to set the symbol type. */
2853 if (h->size == 0
2854 && h->type == STT_NOTYPE
f5385ebf 2855 && !h->needs_plt)
4eca0228 2856 _bfd_error_handler
45d6a902
AM
2857 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2858 h->root.root.string);
2859
2860 dynobj = elf_hash_table (eif->info)->dynobj;
2861 bed = get_elf_backend_data (dynobj);
e7c33416 2862
45d6a902
AM
2863 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2864 {
2865 eif->failed = TRUE;
2866 return FALSE;
2867 }
2868
2869 return TRUE;
2870}
2871
027297b7
L
2872/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2873 DYNBSS. */
2874
2875bfd_boolean
6cabe1ea
AM
2876_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2877 struct elf_link_hash_entry *h,
027297b7
L
2878 asection *dynbss)
2879{
91ac5911 2880 unsigned int power_of_two;
027297b7
L
2881 bfd_vma mask;
2882 asection *sec = h->root.u.def.section;
2883
2884 /* The section aligment of definition is the maximum alignment
91ac5911
L
2885 requirement of symbols defined in the section. Since we don't
2886 know the symbol alignment requirement, we start with the
2887 maximum alignment and check low bits of the symbol address
2888 for the minimum alignment. */
2889 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2890 mask = ((bfd_vma) 1 << power_of_two) - 1;
2891 while ((h->root.u.def.value & mask) != 0)
2892 {
2893 mask >>= 1;
2894 --power_of_two;
2895 }
027297b7 2896
91ac5911
L
2897 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2898 dynbss))
027297b7
L
2899 {
2900 /* Adjust the section alignment if needed. */
2901 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2902 power_of_two))
027297b7
L
2903 return FALSE;
2904 }
2905
91ac5911 2906 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2907 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2908
2909 /* Define the symbol as being at this point in DYNBSS. */
2910 h->root.u.def.section = dynbss;
2911 h->root.u.def.value = dynbss->size;
2912
2913 /* Increment the size of DYNBSS to make room for the symbol. */
2914 dynbss->size += h->size;
2915
f7483970
L
2916 /* No error if extern_protected_data is true. */
2917 if (h->protected_def
889c2a67
L
2918 && (!info->extern_protected_data
2919 || (info->extern_protected_data < 0
2920 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2921 info->callbacks->einfo
2922 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2923 h->root.root.string);
6cabe1ea 2924
027297b7
L
2925 return TRUE;
2926}
2927
45d6a902
AM
2928/* Adjust all external symbols pointing into SEC_MERGE sections
2929 to reflect the object merging within the sections. */
2930
28caa186 2931static bfd_boolean
268b6b39 2932_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2933{
2934 asection *sec;
2935
45d6a902
AM
2936 if ((h->root.type == bfd_link_hash_defined
2937 || h->root.type == bfd_link_hash_defweak)
2938 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2939 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2940 {
a50b1753 2941 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2942
2943 h->root.u.def.value =
2944 _bfd_merged_section_offset (output_bfd,
2945 &h->root.u.def.section,
2946 elf_section_data (sec)->sec_info,
753731ee 2947 h->root.u.def.value);
45d6a902
AM
2948 }
2949
2950 return TRUE;
2951}
986a241f
RH
2952
2953/* Returns false if the symbol referred to by H should be considered
2954 to resolve local to the current module, and true if it should be
2955 considered to bind dynamically. */
2956
2957bfd_boolean
268b6b39
AM
2958_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2959 struct bfd_link_info *info,
89a2ee5a 2960 bfd_boolean not_local_protected)
986a241f
RH
2961{
2962 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2963 const struct elf_backend_data *bed;
2964 struct elf_link_hash_table *hash_table;
986a241f
RH
2965
2966 if (h == NULL)
2967 return FALSE;
2968
2969 while (h->root.type == bfd_link_hash_indirect
2970 || h->root.type == bfd_link_hash_warning)
2971 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2972
2973 /* If it was forced local, then clearly it's not dynamic. */
2974 if (h->dynindx == -1)
2975 return FALSE;
f5385ebf 2976 if (h->forced_local)
986a241f
RH
2977 return FALSE;
2978
2979 /* Identify the cases where name binding rules say that a
2980 visible symbol resolves locally. */
0e1862bb
L
2981 binding_stays_local_p = (bfd_link_executable (info)
2982 || SYMBOLIC_BIND (info, h));
986a241f
RH
2983
2984 switch (ELF_ST_VISIBILITY (h->other))
2985 {
2986 case STV_INTERNAL:
2987 case STV_HIDDEN:
2988 return FALSE;
2989
2990 case STV_PROTECTED:
fcb93ecf
PB
2991 hash_table = elf_hash_table (info);
2992 if (!is_elf_hash_table (hash_table))
2993 return FALSE;
2994
2995 bed = get_elf_backend_data (hash_table->dynobj);
2996
986a241f
RH
2997 /* Proper resolution for function pointer equality may require
2998 that these symbols perhaps be resolved dynamically, even though
2999 we should be resolving them to the current module. */
89a2ee5a 3000 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3001 binding_stays_local_p = TRUE;
3002 break;
3003
3004 default:
986a241f
RH
3005 break;
3006 }
3007
aa37626c 3008 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3009 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3010 return TRUE;
3011
986a241f
RH
3012 /* Otherwise, the symbol is dynamic if binding rules don't tell
3013 us that it remains local. */
3014 return !binding_stays_local_p;
3015}
f6c52c13
AM
3016
3017/* Return true if the symbol referred to by H should be considered
3018 to resolve local to the current module, and false otherwise. Differs
3019 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3020 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
3021 for the place where forced_local and dynindx == -1 are tested. If
3022 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3023 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3024 the symbol is local only for defined symbols.
3025 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3026 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3027 treatment of undefined weak symbols. For those that do not make
3028 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3029
3030bfd_boolean
268b6b39
AM
3031_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3032 struct bfd_link_info *info,
3033 bfd_boolean local_protected)
f6c52c13 3034{
fcb93ecf
PB
3035 const struct elf_backend_data *bed;
3036 struct elf_link_hash_table *hash_table;
3037
f6c52c13
AM
3038 /* If it's a local sym, of course we resolve locally. */
3039 if (h == NULL)
3040 return TRUE;
3041
d95edcac
L
3042 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3043 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3044 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3045 return TRUE;
3046
7e2294f9
AO
3047 /* Common symbols that become definitions don't get the DEF_REGULAR
3048 flag set, so test it first, and don't bail out. */
3049 if (ELF_COMMON_DEF_P (h))
3050 /* Do nothing. */;
f6c52c13 3051 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3052 resolve locally. The sym is either undefined or dynamic. */
3053 else if (!h->def_regular)
f6c52c13
AM
3054 return FALSE;
3055
3056 /* Forced local symbols resolve locally. */
f5385ebf 3057 if (h->forced_local)
f6c52c13
AM
3058 return TRUE;
3059
3060 /* As do non-dynamic symbols. */
3061 if (h->dynindx == -1)
3062 return TRUE;
3063
3064 /* At this point, we know the symbol is defined and dynamic. In an
3065 executable it must resolve locally, likewise when building symbolic
3066 shared libraries. */
0e1862bb 3067 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3068 return TRUE;
3069
3070 /* Now deal with defined dynamic symbols in shared libraries. Ones
3071 with default visibility might not resolve locally. */
3072 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3073 return FALSE;
3074
fcb93ecf
PB
3075 hash_table = elf_hash_table (info);
3076 if (!is_elf_hash_table (hash_table))
3077 return TRUE;
3078
3079 bed = get_elf_backend_data (hash_table->dynobj);
3080
f7483970
L
3081 /* If extern_protected_data is false, STV_PROTECTED non-function
3082 symbols are local. */
889c2a67
L
3083 if ((!info->extern_protected_data
3084 || (info->extern_protected_data < 0
3085 && !bed->extern_protected_data))
3086 && !bed->is_function_type (h->type))
1c16dfa5
L
3087 return TRUE;
3088
f6c52c13 3089 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3090 symbols be treated as dynamic symbols. If the address of a
3091 function not defined in an executable is set to that function's
3092 plt entry in the executable, then the address of the function in
3093 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3094 return local_protected;
3095}
e1918d23
AM
3096
3097/* Caches some TLS segment info, and ensures that the TLS segment vma is
3098 aligned. Returns the first TLS output section. */
3099
3100struct bfd_section *
3101_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3102{
3103 struct bfd_section *sec, *tls;
3104 unsigned int align = 0;
3105
3106 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3107 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3108 break;
3109 tls = sec;
3110
3111 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3112 if (sec->alignment_power > align)
3113 align = sec->alignment_power;
3114
3115 elf_hash_table (info)->tls_sec = tls;
3116
3117 /* Ensure the alignment of the first section is the largest alignment,
3118 so that the tls segment starts aligned. */
3119 if (tls != NULL)
3120 tls->alignment_power = align;
3121
3122 return tls;
3123}
0ad989f9
L
3124
3125/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3126static bfd_boolean
3127is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3128 Elf_Internal_Sym *sym)
3129{
a4d8e49b
L
3130 const struct elf_backend_data *bed;
3131
0ad989f9
L
3132 /* Local symbols do not count, but target specific ones might. */
3133 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3134 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3135 return FALSE;
3136
fcb93ecf 3137 bed = get_elf_backend_data (abfd);
0ad989f9 3138 /* Function symbols do not count. */
fcb93ecf 3139 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3140 return FALSE;
3141
3142 /* If the section is undefined, then so is the symbol. */
3143 if (sym->st_shndx == SHN_UNDEF)
3144 return FALSE;
3145
3146 /* If the symbol is defined in the common section, then
3147 it is a common definition and so does not count. */
a4d8e49b 3148 if (bed->common_definition (sym))
0ad989f9
L
3149 return FALSE;
3150
3151 /* If the symbol is in a target specific section then we
3152 must rely upon the backend to tell us what it is. */
3153 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3154 /* FIXME - this function is not coded yet:
3155
3156 return _bfd_is_global_symbol_definition (abfd, sym);
3157
3158 Instead for now assume that the definition is not global,
3159 Even if this is wrong, at least the linker will behave
3160 in the same way that it used to do. */
3161 return FALSE;
3162
3163 return TRUE;
3164}
3165
3166/* Search the symbol table of the archive element of the archive ABFD
3167 whose archive map contains a mention of SYMDEF, and determine if
3168 the symbol is defined in this element. */
3169static bfd_boolean
3170elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3171{
3172 Elf_Internal_Shdr * hdr;
ef53be89
AM
3173 size_t symcount;
3174 size_t extsymcount;
3175 size_t extsymoff;
0ad989f9
L
3176 Elf_Internal_Sym *isymbuf;
3177 Elf_Internal_Sym *isym;
3178 Elf_Internal_Sym *isymend;
3179 bfd_boolean result;
3180
3181 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3182 if (abfd == NULL)
3183 return FALSE;
3184
3185 if (! bfd_check_format (abfd, bfd_object))
3186 return FALSE;
3187
7dc3990e
L
3188 /* Select the appropriate symbol table. If we don't know if the
3189 object file is an IR object, give linker LTO plugin a chance to
3190 get the correct symbol table. */
3191 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3192#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3193 || (abfd->plugin_format == bfd_plugin_unknown
3194 && bfd_link_plugin_object_p (abfd))
3195#endif
3196 )
3197 {
3198 /* Use the IR symbol table if the object has been claimed by
3199 plugin. */
3200 abfd = abfd->plugin_dummy_bfd;
3201 hdr = &elf_tdata (abfd)->symtab_hdr;
3202 }
3203 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3204 hdr = &elf_tdata (abfd)->symtab_hdr;
3205 else
3206 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3207
3208 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3209
3210 /* The sh_info field of the symtab header tells us where the
3211 external symbols start. We don't care about the local symbols. */
3212 if (elf_bad_symtab (abfd))
3213 {
3214 extsymcount = symcount;
3215 extsymoff = 0;
3216 }
3217 else
3218 {
3219 extsymcount = symcount - hdr->sh_info;
3220 extsymoff = hdr->sh_info;
3221 }
3222
3223 if (extsymcount == 0)
3224 return FALSE;
3225
3226 /* Read in the symbol table. */
3227 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3228 NULL, NULL, NULL);
3229 if (isymbuf == NULL)
3230 return FALSE;
3231
3232 /* Scan the symbol table looking for SYMDEF. */
3233 result = FALSE;
3234 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3235 {
3236 const char *name;
3237
3238 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3239 isym->st_name);
3240 if (name == NULL)
3241 break;
3242
3243 if (strcmp (name, symdef->name) == 0)
3244 {
3245 result = is_global_data_symbol_definition (abfd, isym);
3246 break;
3247 }
3248 }
3249
3250 free (isymbuf);
3251
3252 return result;
3253}
3254\f
5a580b3a
AM
3255/* Add an entry to the .dynamic table. */
3256
3257bfd_boolean
3258_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3259 bfd_vma tag,
3260 bfd_vma val)
3261{
3262 struct elf_link_hash_table *hash_table;
3263 const struct elf_backend_data *bed;
3264 asection *s;
3265 bfd_size_type newsize;
3266 bfd_byte *newcontents;
3267 Elf_Internal_Dyn dyn;
3268
3269 hash_table = elf_hash_table (info);
3270 if (! is_elf_hash_table (hash_table))
3271 return FALSE;
3272
3273 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3274 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3275 BFD_ASSERT (s != NULL);
3276
eea6121a 3277 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3278 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3279 if (newcontents == NULL)
3280 return FALSE;
3281
3282 dyn.d_tag = tag;
3283 dyn.d_un.d_val = val;
eea6121a 3284 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3285
eea6121a 3286 s->size = newsize;
5a580b3a
AM
3287 s->contents = newcontents;
3288
3289 return TRUE;
3290}
3291
3292/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3293 otherwise just check whether one already exists. Returns -1 on error,
3294 1 if a DT_NEEDED tag already exists, and 0 on success. */
3295
4ad4eba5 3296static int
7e9f0867
AM
3297elf_add_dt_needed_tag (bfd *abfd,
3298 struct bfd_link_info *info,
4ad4eba5
AM
3299 const char *soname,
3300 bfd_boolean do_it)
5a580b3a
AM
3301{
3302 struct elf_link_hash_table *hash_table;
ef53be89 3303 size_t strindex;
5a580b3a 3304
7e9f0867
AM
3305 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3306 return -1;
3307
5a580b3a 3308 hash_table = elf_hash_table (info);
5a580b3a 3309 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3310 if (strindex == (size_t) -1)
5a580b3a
AM
3311 return -1;
3312
02be4619 3313 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3314 {
3315 asection *sdyn;
3316 const struct elf_backend_data *bed;
3317 bfd_byte *extdyn;
3318
3319 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3320 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3321 if (sdyn != NULL)
3322 for (extdyn = sdyn->contents;
3323 extdyn < sdyn->contents + sdyn->size;
3324 extdyn += bed->s->sizeof_dyn)
3325 {
3326 Elf_Internal_Dyn dyn;
5a580b3a 3327
7e9f0867
AM
3328 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3329 if (dyn.d_tag == DT_NEEDED
3330 && dyn.d_un.d_val == strindex)
3331 {
3332 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3333 return 1;
3334 }
3335 }
5a580b3a
AM
3336 }
3337
3338 if (do_it)
3339 {
7e9f0867
AM
3340 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3341 return -1;
3342
5a580b3a
AM
3343 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3344 return -1;
3345 }
3346 else
3347 /* We were just checking for existence of the tag. */
3348 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3349
3350 return 0;
3351}
3352
7b15fa7a
AM
3353/* Return true if SONAME is on the needed list between NEEDED and STOP
3354 (or the end of list if STOP is NULL), and needed by a library that
3355 will be loaded. */
3356
010e5ae2 3357static bfd_boolean
7b15fa7a
AM
3358on_needed_list (const char *soname,
3359 struct bfd_link_needed_list *needed,
3360 struct bfd_link_needed_list *stop)
010e5ae2 3361{
7b15fa7a
AM
3362 struct bfd_link_needed_list *look;
3363 for (look = needed; look != stop; look = look->next)
3364 if (strcmp (soname, look->name) == 0
3365 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3366 /* If needed by a library that itself is not directly
3367 needed, recursively check whether that library is
3368 indirectly needed. Since we add DT_NEEDED entries to
3369 the end of the list, library dependencies appear after
3370 the library. Therefore search prior to the current
3371 LOOK, preventing possible infinite recursion. */
3372 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3373 return TRUE;
3374
3375 return FALSE;
3376}
3377
14160578 3378/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3379static int
3380elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3381{
3382 const struct elf_link_hash_entry *h1;
3383 const struct elf_link_hash_entry *h2;
10b7e05b 3384 bfd_signed_vma vdiff;
5a580b3a
AM
3385
3386 h1 = *(const struct elf_link_hash_entry **) arg1;
3387 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3388 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3389 if (vdiff != 0)
3390 return vdiff > 0 ? 1 : -1;
3391 else
3392 {
d3435ae8 3393 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3394 if (sdiff != 0)
3395 return sdiff > 0 ? 1 : -1;
3396 }
14160578
AM
3397 vdiff = h1->size - h2->size;
3398 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3399}
4ad4eba5 3400
5a580b3a
AM
3401/* This function is used to adjust offsets into .dynstr for
3402 dynamic symbols. This is called via elf_link_hash_traverse. */
3403
3404static bfd_boolean
3405elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3406{
a50b1753 3407 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3408
5a580b3a
AM
3409 if (h->dynindx != -1)
3410 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3411 return TRUE;
3412}
3413
3414/* Assign string offsets in .dynstr, update all structures referencing
3415 them. */
3416
4ad4eba5
AM
3417static bfd_boolean
3418elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3419{
3420 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3421 struct elf_link_local_dynamic_entry *entry;
3422 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3423 bfd *dynobj = hash_table->dynobj;
3424 asection *sdyn;
3425 bfd_size_type size;
3426 const struct elf_backend_data *bed;
3427 bfd_byte *extdyn;
3428
3429 _bfd_elf_strtab_finalize (dynstr);
3430 size = _bfd_elf_strtab_size (dynstr);
3431
3432 bed = get_elf_backend_data (dynobj);
3d4d4302 3433 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3434 BFD_ASSERT (sdyn != NULL);
3435
3436 /* Update all .dynamic entries referencing .dynstr strings. */
3437 for (extdyn = sdyn->contents;
eea6121a 3438 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3439 extdyn += bed->s->sizeof_dyn)
3440 {
3441 Elf_Internal_Dyn dyn;
3442
3443 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3444 switch (dyn.d_tag)
3445 {
3446 case DT_STRSZ:
3447 dyn.d_un.d_val = size;
3448 break;
3449 case DT_NEEDED:
3450 case DT_SONAME:
3451 case DT_RPATH:
3452 case DT_RUNPATH:
3453 case DT_FILTER:
3454 case DT_AUXILIARY:
7ee314fa
AM
3455 case DT_AUDIT:
3456 case DT_DEPAUDIT:
5a580b3a
AM
3457 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3458 break;
3459 default:
3460 continue;
3461 }
3462 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3463 }
3464
3465 /* Now update local dynamic symbols. */
3466 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3467 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3468 entry->isym.st_name);
3469
3470 /* And the rest of dynamic symbols. */
3471 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3472
3473 /* Adjust version definitions. */
3474 if (elf_tdata (output_bfd)->cverdefs)
3475 {
3476 asection *s;
3477 bfd_byte *p;
ef53be89 3478 size_t i;
5a580b3a
AM
3479 Elf_Internal_Verdef def;
3480 Elf_Internal_Verdaux defaux;
3481
3d4d4302 3482 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3483 p = s->contents;
3484 do
3485 {
3486 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3487 &def);
3488 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3489 if (def.vd_aux != sizeof (Elf_External_Verdef))
3490 continue;
5a580b3a
AM
3491 for (i = 0; i < def.vd_cnt; ++i)
3492 {
3493 _bfd_elf_swap_verdaux_in (output_bfd,
3494 (Elf_External_Verdaux *) p, &defaux);
3495 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3496 defaux.vda_name);
3497 _bfd_elf_swap_verdaux_out (output_bfd,
3498 &defaux, (Elf_External_Verdaux *) p);
3499 p += sizeof (Elf_External_Verdaux);
3500 }
3501 }
3502 while (def.vd_next);
3503 }
3504
3505 /* Adjust version references. */
3506 if (elf_tdata (output_bfd)->verref)
3507 {
3508 asection *s;
3509 bfd_byte *p;
ef53be89 3510 size_t i;
5a580b3a
AM
3511 Elf_Internal_Verneed need;
3512 Elf_Internal_Vernaux needaux;
3513
3d4d4302 3514 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3515 p = s->contents;
3516 do
3517 {
3518 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3519 &need);
3520 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3521 _bfd_elf_swap_verneed_out (output_bfd, &need,
3522 (Elf_External_Verneed *) p);
3523 p += sizeof (Elf_External_Verneed);
3524 for (i = 0; i < need.vn_cnt; ++i)
3525 {
3526 _bfd_elf_swap_vernaux_in (output_bfd,
3527 (Elf_External_Vernaux *) p, &needaux);
3528 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3529 needaux.vna_name);
3530 _bfd_elf_swap_vernaux_out (output_bfd,
3531 &needaux,
3532 (Elf_External_Vernaux *) p);
3533 p += sizeof (Elf_External_Vernaux);
3534 }
3535 }
3536 while (need.vn_next);
3537 }
3538
3539 return TRUE;
3540}
3541\f
13285a1b
AM
3542/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3543 The default is to only match when the INPUT and OUTPUT are exactly
3544 the same target. */
3545
3546bfd_boolean
3547_bfd_elf_default_relocs_compatible (const bfd_target *input,
3548 const bfd_target *output)
3549{
3550 return input == output;
3551}
3552
3553/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3554 This version is used when different targets for the same architecture
3555 are virtually identical. */
3556
3557bfd_boolean
3558_bfd_elf_relocs_compatible (const bfd_target *input,
3559 const bfd_target *output)
3560{
3561 const struct elf_backend_data *obed, *ibed;
3562
3563 if (input == output)
3564 return TRUE;
3565
3566 ibed = xvec_get_elf_backend_data (input);
3567 obed = xvec_get_elf_backend_data (output);
3568
3569 if (ibed->arch != obed->arch)
3570 return FALSE;
3571
3572 /* If both backends are using this function, deem them compatible. */
3573 return ibed->relocs_compatible == obed->relocs_compatible;
3574}
3575
e5034e59
AM
3576/* Make a special call to the linker "notice" function to tell it that
3577 we are about to handle an as-needed lib, or have finished
1b786873 3578 processing the lib. */
e5034e59
AM
3579
3580bfd_boolean
3581_bfd_elf_notice_as_needed (bfd *ibfd,
3582 struct bfd_link_info *info,
3583 enum notice_asneeded_action act)
3584{
46135103 3585 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3586}
3587
d9689752
L
3588/* Check relocations an ELF object file. */
3589
3590bfd_boolean
3591_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3592{
3593 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3594 struct elf_link_hash_table *htab = elf_hash_table (info);
3595
3596 /* If this object is the same format as the output object, and it is
3597 not a shared library, then let the backend look through the
3598 relocs.
3599
3600 This is required to build global offset table entries and to
3601 arrange for dynamic relocs. It is not required for the
3602 particular common case of linking non PIC code, even when linking
3603 against shared libraries, but unfortunately there is no way of
3604 knowing whether an object file has been compiled PIC or not.
3605 Looking through the relocs is not particularly time consuming.
3606 The problem is that we must either (1) keep the relocs in memory,
3607 which causes the linker to require additional runtime memory or
3608 (2) read the relocs twice from the input file, which wastes time.
3609 This would be a good case for using mmap.
3610
3611 I have no idea how to handle linking PIC code into a file of a
3612 different format. It probably can't be done. */
3613 if ((abfd->flags & DYNAMIC) == 0
3614 && is_elf_hash_table (htab)
3615 && bed->check_relocs != NULL
3616 && elf_object_id (abfd) == elf_hash_table_id (htab)
3617 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3618 {
3619 asection *o;
3620
3621 for (o = abfd->sections; o != NULL; o = o->next)
3622 {
3623 Elf_Internal_Rela *internal_relocs;
3624 bfd_boolean ok;
3625
5ce03cea 3626 /* Don't check relocations in excluded sections. */
d9689752 3627 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3628 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3629 || o->reloc_count == 0
3630 || ((info->strip == strip_all || info->strip == strip_debugger)
3631 && (o->flags & SEC_DEBUGGING) != 0)
3632 || bfd_is_abs_section (o->output_section))
3633 continue;
3634
3635 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3636 info->keep_memory);
3637 if (internal_relocs == NULL)
3638 return FALSE;
3639
3640 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3641
3642 if (elf_section_data (o)->relocs != internal_relocs)
3643 free (internal_relocs);
3644
3645 if (! ok)
3646 return FALSE;
3647 }
3648 }
3649
3650 return TRUE;
3651}
3652
4ad4eba5
AM
3653/* Add symbols from an ELF object file to the linker hash table. */
3654
3655static bfd_boolean
3656elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3657{
a0c402a5 3658 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3659 Elf_Internal_Shdr *hdr;
ef53be89
AM
3660 size_t symcount;
3661 size_t extsymcount;
3662 size_t extsymoff;
4ad4eba5
AM
3663 struct elf_link_hash_entry **sym_hash;
3664 bfd_boolean dynamic;
3665 Elf_External_Versym *extversym = NULL;
3666 Elf_External_Versym *ever;
3667 struct elf_link_hash_entry *weaks;
3668 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3669 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3670 Elf_Internal_Sym *isymbuf = NULL;
3671 Elf_Internal_Sym *isym;
3672 Elf_Internal_Sym *isymend;
3673 const struct elf_backend_data *bed;
3674 bfd_boolean add_needed;
66eb6687 3675 struct elf_link_hash_table *htab;
4ad4eba5 3676 bfd_size_type amt;
66eb6687 3677 void *alloc_mark = NULL;
4f87808c
AM
3678 struct bfd_hash_entry **old_table = NULL;
3679 unsigned int old_size = 0;
3680 unsigned int old_count = 0;
66eb6687 3681 void *old_tab = NULL;
66eb6687
AM
3682 void *old_ent;
3683 struct bfd_link_hash_entry *old_undefs = NULL;
3684 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3685 void *old_strtab = NULL;
66eb6687 3686 size_t tabsize = 0;
db6a5d5f 3687 asection *s;
29a9f53e 3688 bfd_boolean just_syms;
4ad4eba5 3689
66eb6687 3690 htab = elf_hash_table (info);
4ad4eba5 3691 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3692
3693 if ((abfd->flags & DYNAMIC) == 0)
3694 dynamic = FALSE;
3695 else
3696 {
3697 dynamic = TRUE;
3698
3699 /* You can't use -r against a dynamic object. Also, there's no
3700 hope of using a dynamic object which does not exactly match
3701 the format of the output file. */
0e1862bb 3702 if (bfd_link_relocatable (info)
66eb6687 3703 || !is_elf_hash_table (htab)
f13a99db 3704 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3705 {
0e1862bb 3706 if (bfd_link_relocatable (info))
9a0789ec
NC
3707 bfd_set_error (bfd_error_invalid_operation);
3708 else
3709 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3710 goto error_return;
3711 }
3712 }
3713
a0c402a5
L
3714 ehdr = elf_elfheader (abfd);
3715 if (info->warn_alternate_em
3716 && bed->elf_machine_code != ehdr->e_machine
3717 && ((bed->elf_machine_alt1 != 0
3718 && ehdr->e_machine == bed->elf_machine_alt1)
3719 || (bed->elf_machine_alt2 != 0
3720 && ehdr->e_machine == bed->elf_machine_alt2)))
3721 info->callbacks->einfo
695344c0 3722 /* xgettext:c-format */
a0c402a5
L
3723 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3724 ehdr->e_machine, abfd, bed->elf_machine_code);
3725
4ad4eba5
AM
3726 /* As a GNU extension, any input sections which are named
3727 .gnu.warning.SYMBOL are treated as warning symbols for the given
3728 symbol. This differs from .gnu.warning sections, which generate
3729 warnings when they are included in an output file. */
dd98f8d2 3730 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3731 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3732 {
db6a5d5f 3733 const char *name;
4ad4eba5 3734
db6a5d5f
AM
3735 name = bfd_get_section_name (abfd, s);
3736 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3737 {
db6a5d5f
AM
3738 char *msg;
3739 bfd_size_type sz;
3740
3741 name += sizeof ".gnu.warning." - 1;
3742
3743 /* If this is a shared object, then look up the symbol
3744 in the hash table. If it is there, and it is already
3745 been defined, then we will not be using the entry
3746 from this shared object, so we don't need to warn.
3747 FIXME: If we see the definition in a regular object
3748 later on, we will warn, but we shouldn't. The only
3749 fix is to keep track of what warnings we are supposed
3750 to emit, and then handle them all at the end of the
3751 link. */
3752 if (dynamic)
4ad4eba5 3753 {
db6a5d5f
AM
3754 struct elf_link_hash_entry *h;
3755
3756 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3757
3758 /* FIXME: What about bfd_link_hash_common? */
3759 if (h != NULL
3760 && (h->root.type == bfd_link_hash_defined
3761 || h->root.type == bfd_link_hash_defweak))
3762 continue;
3763 }
4ad4eba5 3764
db6a5d5f
AM
3765 sz = s->size;
3766 msg = (char *) bfd_alloc (abfd, sz + 1);
3767 if (msg == NULL)
3768 goto error_return;
4ad4eba5 3769
db6a5d5f
AM
3770 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3771 goto error_return;
4ad4eba5 3772
db6a5d5f 3773 msg[sz] = '\0';
4ad4eba5 3774
db6a5d5f
AM
3775 if (! (_bfd_generic_link_add_one_symbol
3776 (info, abfd, name, BSF_WARNING, s, 0, msg,
3777 FALSE, bed->collect, NULL)))
3778 goto error_return;
4ad4eba5 3779
0e1862bb 3780 if (bfd_link_executable (info))
db6a5d5f
AM
3781 {
3782 /* Clobber the section size so that the warning does
3783 not get copied into the output file. */
3784 s->size = 0;
11d2f718 3785
db6a5d5f
AM
3786 /* Also set SEC_EXCLUDE, so that symbols defined in
3787 the warning section don't get copied to the output. */
3788 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3789 }
3790 }
3791 }
3792
29a9f53e
L
3793 just_syms = ((s = abfd->sections) != NULL
3794 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3795
4ad4eba5
AM
3796 add_needed = TRUE;
3797 if (! dynamic)
3798 {
3799 /* If we are creating a shared library, create all the dynamic
3800 sections immediately. We need to attach them to something,
3801 so we attach them to this BFD, provided it is the right
bf89386a
L
3802 format and is not from ld --just-symbols. Always create the
3803 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3804 are no input BFD's of the same format as the output, we can't
3805 make a shared library. */
3806 if (!just_syms
bf89386a 3807 && (bfd_link_pic (info)
9c1d7a08
L
3808 || (!bfd_link_relocatable (info)
3809 && (info->export_dynamic || info->dynamic)))
66eb6687 3810 && is_elf_hash_table (htab)
f13a99db 3811 && info->output_bfd->xvec == abfd->xvec
66eb6687 3812 && !htab->dynamic_sections_created)
4ad4eba5
AM
3813 {
3814 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3815 goto error_return;
3816 }
3817 }
66eb6687 3818 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3819 goto error_return;
3820 else
3821 {
4ad4eba5 3822 const char *soname = NULL;
7ee314fa 3823 char *audit = NULL;
4ad4eba5 3824 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3825 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3826 int ret;
3827
3828 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3829 ld shouldn't allow it. */
29a9f53e 3830 if (just_syms)
92fd189d 3831 abort ();
4ad4eba5
AM
3832
3833 /* If this dynamic lib was specified on the command line with
3834 --as-needed in effect, then we don't want to add a DT_NEEDED
3835 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3836 in by another lib's DT_NEEDED. When --no-add-needed is used
3837 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3838 any dynamic library in DT_NEEDED tags in the dynamic lib at
3839 all. */
3840 add_needed = (elf_dyn_lib_class (abfd)
3841 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3842 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3843
3844 s = bfd_get_section_by_name (abfd, ".dynamic");
3845 if (s != NULL)
3846 {
3847 bfd_byte *dynbuf;
3848 bfd_byte *extdyn;
cb33740c 3849 unsigned int elfsec;
4ad4eba5
AM
3850 unsigned long shlink;
3851
eea6121a 3852 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3853 {
3854error_free_dyn:
3855 free (dynbuf);
3856 goto error_return;
3857 }
4ad4eba5
AM
3858
3859 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3860 if (elfsec == SHN_BAD)
4ad4eba5
AM
3861 goto error_free_dyn;
3862 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3863
3864 for (extdyn = dynbuf;
eea6121a 3865 extdyn < dynbuf + s->size;
4ad4eba5
AM
3866 extdyn += bed->s->sizeof_dyn)
3867 {
3868 Elf_Internal_Dyn dyn;
3869
3870 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3871 if (dyn.d_tag == DT_SONAME)
3872 {
3873 unsigned int tagv = dyn.d_un.d_val;
3874 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3875 if (soname == NULL)
3876 goto error_free_dyn;
3877 }
3878 if (dyn.d_tag == DT_NEEDED)
3879 {
3880 struct bfd_link_needed_list *n, **pn;
3881 char *fnm, *anm;
3882 unsigned int tagv = dyn.d_un.d_val;
3883
3884 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3885 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3886 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3887 if (n == NULL || fnm == NULL)
3888 goto error_free_dyn;
3889 amt = strlen (fnm) + 1;
a50b1753 3890 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3891 if (anm == NULL)
3892 goto error_free_dyn;
3893 memcpy (anm, fnm, amt);
3894 n->name = anm;
3895 n->by = abfd;
3896 n->next = NULL;
66eb6687 3897 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3898 ;
3899 *pn = n;
3900 }
3901 if (dyn.d_tag == DT_RUNPATH)
3902 {
3903 struct bfd_link_needed_list *n, **pn;
3904 char *fnm, *anm;
3905 unsigned int tagv = dyn.d_un.d_val;
3906
3907 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3908 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3909 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3910 if (n == NULL || fnm == NULL)
3911 goto error_free_dyn;
3912 amt = strlen (fnm) + 1;
a50b1753 3913 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3914 if (anm == NULL)
3915 goto error_free_dyn;
3916 memcpy (anm, fnm, amt);
3917 n->name = anm;
3918 n->by = abfd;
3919 n->next = NULL;
3920 for (pn = & runpath;
3921 *pn != NULL;
3922 pn = &(*pn)->next)
3923 ;
3924 *pn = n;
3925 }
3926 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3927 if (!runpath && dyn.d_tag == DT_RPATH)
3928 {
3929 struct bfd_link_needed_list *n, **pn;
3930 char *fnm, *anm;
3931 unsigned int tagv = dyn.d_un.d_val;
3932
3933 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3934 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3935 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3936 if (n == NULL || fnm == NULL)
3937 goto error_free_dyn;
3938 amt = strlen (fnm) + 1;
a50b1753 3939 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3940 if (anm == NULL)
f8703194 3941 goto error_free_dyn;
4ad4eba5
AM
3942 memcpy (anm, fnm, amt);
3943 n->name = anm;
3944 n->by = abfd;
3945 n->next = NULL;
3946 for (pn = & rpath;
3947 *pn != NULL;
3948 pn = &(*pn)->next)
3949 ;
3950 *pn = n;
3951 }
7ee314fa
AM
3952 if (dyn.d_tag == DT_AUDIT)
3953 {
3954 unsigned int tagv = dyn.d_un.d_val;
3955 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3956 }
4ad4eba5
AM
3957 }
3958
3959 free (dynbuf);
3960 }
3961
3962 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3963 frees all more recently bfd_alloc'd blocks as well. */
3964 if (runpath)
3965 rpath = runpath;
3966
3967 if (rpath)
3968 {
3969 struct bfd_link_needed_list **pn;
66eb6687 3970 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3971 ;
3972 *pn = rpath;
3973 }
3974
9acc85a6
AM
3975 /* If we have a PT_GNU_RELRO program header, mark as read-only
3976 all sections contained fully therein. This makes relro
3977 shared library sections appear as they will at run-time. */
3978 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
3979 while (--phdr >= elf_tdata (abfd)->phdr)
3980 if (phdr->p_type == PT_GNU_RELRO)
3981 {
3982 for (s = abfd->sections; s != NULL; s = s->next)
3983 if ((s->flags & SEC_ALLOC) != 0
3984 && s->vma >= phdr->p_vaddr
3985 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
3986 s->flags |= SEC_READONLY;
3987 break;
3988 }
3989
4ad4eba5
AM
3990 /* We do not want to include any of the sections in a dynamic
3991 object in the output file. We hack by simply clobbering the
3992 list of sections in the BFD. This could be handled more
3993 cleanly by, say, a new section flag; the existing
3994 SEC_NEVER_LOAD flag is not the one we want, because that one
3995 still implies that the section takes up space in the output
3996 file. */
3997 bfd_section_list_clear (abfd);
3998
4ad4eba5
AM
3999 /* Find the name to use in a DT_NEEDED entry that refers to this
4000 object. If the object has a DT_SONAME entry, we use it.
4001 Otherwise, if the generic linker stuck something in
4002 elf_dt_name, we use that. Otherwise, we just use the file
4003 name. */
4004 if (soname == NULL || *soname == '\0')
4005 {
4006 soname = elf_dt_name (abfd);
4007 if (soname == NULL || *soname == '\0')
4008 soname = bfd_get_filename (abfd);
4009 }
4010
4011 /* Save the SONAME because sometimes the linker emulation code
4012 will need to know it. */
4013 elf_dt_name (abfd) = soname;
4014
7e9f0867 4015 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4016 if (ret < 0)
4017 goto error_return;
4018
4019 /* If we have already included this dynamic object in the
4020 link, just ignore it. There is no reason to include a
4021 particular dynamic object more than once. */
4022 if (ret > 0)
4023 return TRUE;
7ee314fa
AM
4024
4025 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4026 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4027 }
4028
4029 /* If this is a dynamic object, we always link against the .dynsym
4030 symbol table, not the .symtab symbol table. The dynamic linker
4031 will only see the .dynsym symbol table, so there is no reason to
4032 look at .symtab for a dynamic object. */
4033
4034 if (! dynamic || elf_dynsymtab (abfd) == 0)
4035 hdr = &elf_tdata (abfd)->symtab_hdr;
4036 else
4037 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4038
4039 symcount = hdr->sh_size / bed->s->sizeof_sym;
4040
4041 /* The sh_info field of the symtab header tells us where the
4042 external symbols start. We don't care about the local symbols at
4043 this point. */
4044 if (elf_bad_symtab (abfd))
4045 {
4046 extsymcount = symcount;
4047 extsymoff = 0;
4048 }
4049 else
4050 {
4051 extsymcount = symcount - hdr->sh_info;
4052 extsymoff = hdr->sh_info;
4053 }
4054
f45794cb 4055 sym_hash = elf_sym_hashes (abfd);
012b2306 4056 if (extsymcount != 0)
4ad4eba5
AM
4057 {
4058 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4059 NULL, NULL, NULL);
4060 if (isymbuf == NULL)
4061 goto error_return;
4062
4ad4eba5 4063 if (sym_hash == NULL)
012b2306
AM
4064 {
4065 /* We store a pointer to the hash table entry for each
4066 external symbol. */
ef53be89
AM
4067 amt = extsymcount;
4068 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4069 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4070 if (sym_hash == NULL)
4071 goto error_free_sym;
4072 elf_sym_hashes (abfd) = sym_hash;
4073 }
4ad4eba5
AM
4074 }
4075
4076 if (dynamic)
4077 {
4078 /* Read in any version definitions. */
fc0e6df6
PB
4079 if (!_bfd_elf_slurp_version_tables (abfd,
4080 info->default_imported_symver))
4ad4eba5
AM
4081 goto error_free_sym;
4082
4083 /* Read in the symbol versions, but don't bother to convert them
4084 to internal format. */
4085 if (elf_dynversym (abfd) != 0)
4086 {
4087 Elf_Internal_Shdr *versymhdr;
4088
4089 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4090 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4091 if (extversym == NULL)
4092 goto error_free_sym;
4093 amt = versymhdr->sh_size;
4094 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4095 || bfd_bread (extversym, amt, abfd) != amt)
4096 goto error_free_vers;
4097 }
4098 }
4099
66eb6687
AM
4100 /* If we are loading an as-needed shared lib, save the symbol table
4101 state before we start adding symbols. If the lib turns out
4102 to be unneeded, restore the state. */
4103 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4104 {
4105 unsigned int i;
4106 size_t entsize;
4107
4108 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4109 {
4110 struct bfd_hash_entry *p;
2de92251 4111 struct elf_link_hash_entry *h;
66eb6687
AM
4112
4113 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4114 {
4115 h = (struct elf_link_hash_entry *) p;
4116 entsize += htab->root.table.entsize;
4117 if (h->root.type == bfd_link_hash_warning)
4118 entsize += htab->root.table.entsize;
4119 }
66eb6687
AM
4120 }
4121
4122 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4123 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4124 if (old_tab == NULL)
4125 goto error_free_vers;
4126
4127 /* Remember the current objalloc pointer, so that all mem for
4128 symbols added can later be reclaimed. */
4129 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4130 if (alloc_mark == NULL)
4131 goto error_free_vers;
4132
5061a885
AM
4133 /* Make a special call to the linker "notice" function to
4134 tell it that we are about to handle an as-needed lib. */
e5034e59 4135 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4136 goto error_free_vers;
5061a885 4137
f45794cb
AM
4138 /* Clone the symbol table. Remember some pointers into the
4139 symbol table, and dynamic symbol count. */
4140 old_ent = (char *) old_tab + tabsize;
66eb6687 4141 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4142 old_undefs = htab->root.undefs;
4143 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4144 old_table = htab->root.table.table;
4145 old_size = htab->root.table.size;
4146 old_count = htab->root.table.count;
5b677558
AM
4147 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4148 if (old_strtab == NULL)
4149 goto error_free_vers;
66eb6687
AM
4150
4151 for (i = 0; i < htab->root.table.size; i++)
4152 {
4153 struct bfd_hash_entry *p;
2de92251 4154 struct elf_link_hash_entry *h;
66eb6687
AM
4155
4156 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4157 {
4158 memcpy (old_ent, p, htab->root.table.entsize);
4159 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4160 h = (struct elf_link_hash_entry *) p;
4161 if (h->root.type == bfd_link_hash_warning)
4162 {
4163 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4164 old_ent = (char *) old_ent + htab->root.table.entsize;
4165 }
66eb6687
AM
4166 }
4167 }
4168 }
4ad4eba5 4169
66eb6687 4170 weaks = NULL;
4ad4eba5
AM
4171 ever = extversym != NULL ? extversym + extsymoff : NULL;
4172 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4173 isym < isymend;
4174 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4175 {
4176 int bind;
4177 bfd_vma value;
af44c138 4178 asection *sec, *new_sec;
4ad4eba5
AM
4179 flagword flags;
4180 const char *name;
4181 struct elf_link_hash_entry *h;
90c984fc 4182 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4183 bfd_boolean definition;
4184 bfd_boolean size_change_ok;
4185 bfd_boolean type_change_ok;
4186 bfd_boolean new_weakdef;
37a9e49a
L
4187 bfd_boolean new_weak;
4188 bfd_boolean old_weak;
4ad4eba5 4189 bfd_boolean override;
a4d8e49b 4190 bfd_boolean common;
97196564 4191 bfd_boolean discarded;
4ad4eba5
AM
4192 unsigned int old_alignment;
4193 bfd *old_bfd;
6e33951e 4194 bfd_boolean matched;
4ad4eba5
AM
4195
4196 override = FALSE;
4197
4198 flags = BSF_NO_FLAGS;
4199 sec = NULL;
4200 value = isym->st_value;
a4d8e49b 4201 common = bed->common_definition (isym);
97196564 4202 discarded = FALSE;
4ad4eba5
AM
4203
4204 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4205 switch (bind)
4ad4eba5 4206 {
3e7a7d11 4207 case STB_LOCAL:
4ad4eba5
AM
4208 /* This should be impossible, since ELF requires that all
4209 global symbols follow all local symbols, and that sh_info
4210 point to the first global symbol. Unfortunately, Irix 5
4211 screws this up. */
4212 continue;
3e7a7d11
NC
4213
4214 case STB_GLOBAL:
a4d8e49b 4215 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4216 flags = BSF_GLOBAL;
3e7a7d11
NC
4217 break;
4218
4219 case STB_WEAK:
4220 flags = BSF_WEAK;
4221 break;
4222
4223 case STB_GNU_UNIQUE:
4224 flags = BSF_GNU_UNIQUE;
4225 break;
4226
4227 default:
4ad4eba5 4228 /* Leave it up to the processor backend. */
3e7a7d11 4229 break;
4ad4eba5
AM
4230 }
4231
4232 if (isym->st_shndx == SHN_UNDEF)
4233 sec = bfd_und_section_ptr;
cb33740c
AM
4234 else if (isym->st_shndx == SHN_ABS)
4235 sec = bfd_abs_section_ptr;
4236 else if (isym->st_shndx == SHN_COMMON)
4237 {
4238 sec = bfd_com_section_ptr;
4239 /* What ELF calls the size we call the value. What ELF
4240 calls the value we call the alignment. */
4241 value = isym->st_size;
4242 }
4243 else
4ad4eba5
AM
4244 {
4245 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4246 if (sec == NULL)
4247 sec = bfd_abs_section_ptr;
dbaa2011 4248 else if (discarded_section (sec))
529fcb95 4249 {
e5d08002
L
4250 /* Symbols from discarded section are undefined. We keep
4251 its visibility. */
529fcb95 4252 sec = bfd_und_section_ptr;
97196564 4253 discarded = TRUE;
529fcb95
PB
4254 isym->st_shndx = SHN_UNDEF;
4255 }
4ad4eba5
AM
4256 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4257 value -= sec->vma;
4258 }
4ad4eba5
AM
4259
4260 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4261 isym->st_name);
4262 if (name == NULL)
4263 goto error_free_vers;
4264
4265 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4266 && (abfd->flags & BFD_PLUGIN) != 0)
4267 {
4268 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4269
4270 if (xc == NULL)
4271 {
4272 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4273 | SEC_EXCLUDE);
4274 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4275 if (xc == NULL)
4276 goto error_free_vers;
4277 }
4278 sec = xc;
4279 }
4280 else if (isym->st_shndx == SHN_COMMON
4281 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4282 && !bfd_link_relocatable (info))
4ad4eba5
AM
4283 {
4284 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4285
4286 if (tcomm == NULL)
4287 {
02d00247
AM
4288 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4289 | SEC_LINKER_CREATED);
4290 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4291 if (tcomm == NULL)
4ad4eba5
AM
4292 goto error_free_vers;
4293 }
4294 sec = tcomm;
4295 }
66eb6687 4296 else if (bed->elf_add_symbol_hook)
4ad4eba5 4297 {
66eb6687
AM
4298 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4299 &sec, &value))
4ad4eba5
AM
4300 goto error_free_vers;
4301
4302 /* The hook function sets the name to NULL if this symbol
4303 should be skipped for some reason. */
4304 if (name == NULL)
4305 continue;
4306 }
4307
4308 /* Sanity check that all possibilities were handled. */
4309 if (sec == NULL)
4310 {
4311 bfd_set_error (bfd_error_bad_value);
4312 goto error_free_vers;
4313 }
4314
191c0c42
AM
4315 /* Silently discard TLS symbols from --just-syms. There's
4316 no way to combine a static TLS block with a new TLS block
4317 for this executable. */
4318 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4319 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4320 continue;
4321
4ad4eba5
AM
4322 if (bfd_is_und_section (sec)
4323 || bfd_is_com_section (sec))
4324 definition = FALSE;
4325 else
4326 definition = TRUE;
4327
4328 size_change_ok = FALSE;
66eb6687 4329 type_change_ok = bed->type_change_ok;
37a9e49a 4330 old_weak = FALSE;
6e33951e 4331 matched = FALSE;
4ad4eba5
AM
4332 old_alignment = 0;
4333 old_bfd = NULL;
af44c138 4334 new_sec = sec;
4ad4eba5 4335
66eb6687 4336 if (is_elf_hash_table (htab))
4ad4eba5
AM
4337 {
4338 Elf_Internal_Versym iver;
4339 unsigned int vernum = 0;
4340 bfd_boolean skip;
4341
fc0e6df6 4342 if (ever == NULL)
4ad4eba5 4343 {
fc0e6df6
PB
4344 if (info->default_imported_symver)
4345 /* Use the default symbol version created earlier. */
4346 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4347 else
4348 iver.vs_vers = 0;
4349 }
4350 else
4351 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4352
4353 vernum = iver.vs_vers & VERSYM_VERSION;
4354
4355 /* If this is a hidden symbol, or if it is not version
4356 1, we append the version name to the symbol name.
cc86ff91
EB
4357 However, we do not modify a non-hidden absolute symbol
4358 if it is not a function, because it might be the version
4359 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4360 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4361 || (vernum > 1
4362 && (!bfd_is_abs_section (sec)
4363 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4364 {
4365 const char *verstr;
4366 size_t namelen, verlen, newlen;
4367 char *newname, *p;
4368
4369 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4370 {
fc0e6df6
PB
4371 if (vernum > elf_tdata (abfd)->cverdefs)
4372 verstr = NULL;
4373 else if (vernum > 1)
4374 verstr =
4375 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4376 else
4377 verstr = "";
4ad4eba5 4378
fc0e6df6 4379 if (verstr == NULL)
4ad4eba5 4380 {
4eca0228 4381 _bfd_error_handler
695344c0 4382 /* xgettext:c-format */
fc0e6df6
PB
4383 (_("%B: %s: invalid version %u (max %d)"),
4384 abfd, name, vernum,
4385 elf_tdata (abfd)->cverdefs);
4386 bfd_set_error (bfd_error_bad_value);
4387 goto error_free_vers;
4ad4eba5 4388 }
fc0e6df6
PB
4389 }
4390 else
4391 {
4392 /* We cannot simply test for the number of
4393 entries in the VERNEED section since the
4394 numbers for the needed versions do not start
4395 at 0. */
4396 Elf_Internal_Verneed *t;
4397
4398 verstr = NULL;
4399 for (t = elf_tdata (abfd)->verref;
4400 t != NULL;
4401 t = t->vn_nextref)
4ad4eba5 4402 {
fc0e6df6 4403 Elf_Internal_Vernaux *a;
4ad4eba5 4404
fc0e6df6
PB
4405 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4406 {
4407 if (a->vna_other == vernum)
4ad4eba5 4408 {
fc0e6df6
PB
4409 verstr = a->vna_nodename;
4410 break;
4ad4eba5 4411 }
4ad4eba5 4412 }
fc0e6df6
PB
4413 if (a != NULL)
4414 break;
4415 }
4416 if (verstr == NULL)
4417 {
4eca0228 4418 _bfd_error_handler
695344c0 4419 /* xgettext:c-format */
fc0e6df6
PB
4420 (_("%B: %s: invalid needed version %d"),
4421 abfd, name, vernum);
4422 bfd_set_error (bfd_error_bad_value);
4423 goto error_free_vers;
4ad4eba5 4424 }
4ad4eba5 4425 }
fc0e6df6
PB
4426
4427 namelen = strlen (name);
4428 verlen = strlen (verstr);
4429 newlen = namelen + verlen + 2;
4430 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4431 && isym->st_shndx != SHN_UNDEF)
4432 ++newlen;
4433
a50b1753 4434 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4435 if (newname == NULL)
4436 goto error_free_vers;
4437 memcpy (newname, name, namelen);
4438 p = newname + namelen;
4439 *p++ = ELF_VER_CHR;
4440 /* If this is a defined non-hidden version symbol,
4441 we add another @ to the name. This indicates the
4442 default version of the symbol. */
4443 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4444 && isym->st_shndx != SHN_UNDEF)
4445 *p++ = ELF_VER_CHR;
4446 memcpy (p, verstr, verlen + 1);
4447
4448 name = newname;
4ad4eba5
AM
4449 }
4450
cd3416da
AM
4451 /* If this symbol has default visibility and the user has
4452 requested we not re-export it, then mark it as hidden. */
a0d49154 4453 if (!bfd_is_und_section (sec)
cd3416da 4454 && !dynamic
ce875075 4455 && abfd->no_export
cd3416da
AM
4456 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4457 isym->st_other = (STV_HIDDEN
4458 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4459
4f3fedcf
AM
4460 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4461 sym_hash, &old_bfd, &old_weak,
4462 &old_alignment, &skip, &override,
6e33951e
L
4463 &type_change_ok, &size_change_ok,
4464 &matched))
4ad4eba5
AM
4465 goto error_free_vers;
4466
4467 if (skip)
4468 continue;
4469
6e33951e
L
4470 /* Override a definition only if the new symbol matches the
4471 existing one. */
4472 if (override && matched)
4ad4eba5
AM
4473 definition = FALSE;
4474
4475 h = *sym_hash;
4476 while (h->root.type == bfd_link_hash_indirect
4477 || h->root.type == bfd_link_hash_warning)
4478 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4479
4ad4eba5 4480 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4481 && vernum > 1
4482 && definition)
4483 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4484 }
4485
4486 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4487 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4488 (struct bfd_link_hash_entry **) sym_hash)))
4489 goto error_free_vers;
4490
a43942db
MR
4491 if ((flags & BSF_GNU_UNIQUE)
4492 && (abfd->flags & DYNAMIC) == 0
4493 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4494 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4495
4ad4eba5 4496 h = *sym_hash;
90c984fc
L
4497 /* We need to make sure that indirect symbol dynamic flags are
4498 updated. */
4499 hi = h;
4ad4eba5
AM
4500 while (h->root.type == bfd_link_hash_indirect
4501 || h->root.type == bfd_link_hash_warning)
4502 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4503
97196564
L
4504 /* Setting the index to -3 tells elf_link_output_extsym that
4505 this symbol is defined in a discarded section. */
4506 if (discarded)
4507 h->indx = -3;
4508
4ad4eba5
AM
4509 *sym_hash = h;
4510
37a9e49a 4511 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4512 new_weakdef = FALSE;
4513 if (dynamic
4514 && definition
37a9e49a 4515 && new_weak
fcb93ecf 4516 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4517 && is_elf_hash_table (htab)
f6e332e6 4518 && h->u.weakdef == NULL)
4ad4eba5
AM
4519 {
4520 /* Keep a list of all weak defined non function symbols from
4521 a dynamic object, using the weakdef field. Later in this
4522 function we will set the weakdef field to the correct
4523 value. We only put non-function symbols from dynamic
4524 objects on this list, because that happens to be the only
4525 time we need to know the normal symbol corresponding to a
4526 weak symbol, and the information is time consuming to
4527 figure out. If the weakdef field is not already NULL,
4528 then this symbol was already defined by some previous
4529 dynamic object, and we will be using that previous
4530 definition anyhow. */
4531
f6e332e6 4532 h->u.weakdef = weaks;
4ad4eba5
AM
4533 weaks = h;
4534 new_weakdef = TRUE;
4535 }
4536
4537 /* Set the alignment of a common symbol. */
a4d8e49b 4538 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4539 && h->root.type == bfd_link_hash_common)
4540 {
4541 unsigned int align;
4542
a4d8e49b 4543 if (common)
af44c138
L
4544 align = bfd_log2 (isym->st_value);
4545 else
4546 {
4547 /* The new symbol is a common symbol in a shared object.
4548 We need to get the alignment from the section. */
4549 align = new_sec->alignment_power;
4550 }
595213d4 4551 if (align > old_alignment)
4ad4eba5
AM
4552 h->root.u.c.p->alignment_power = align;
4553 else
4554 h->root.u.c.p->alignment_power = old_alignment;
4555 }
4556
66eb6687 4557 if (is_elf_hash_table (htab))
4ad4eba5 4558 {
4f3fedcf
AM
4559 /* Set a flag in the hash table entry indicating the type of
4560 reference or definition we just found. A dynamic symbol
4561 is one which is referenced or defined by both a regular
4562 object and a shared object. */
4563 bfd_boolean dynsym = FALSE;
4564
4565 /* Plugin symbols aren't normal. Don't set def_regular or
4566 ref_regular for them, or make them dynamic. */
4567 if ((abfd->flags & BFD_PLUGIN) != 0)
4568 ;
4569 else if (! dynamic)
4570 {
4571 if (! definition)
4572 {
4573 h->ref_regular = 1;
4574 if (bind != STB_WEAK)
4575 h->ref_regular_nonweak = 1;
4576 }
4577 else
4578 {
4579 h->def_regular = 1;
4580 if (h->def_dynamic)
4581 {
4582 h->def_dynamic = 0;
4583 h->ref_dynamic = 1;
4584 }
4585 }
4586
4587 /* If the indirect symbol has been forced local, don't
4588 make the real symbol dynamic. */
4589 if ((h == hi || !hi->forced_local)
0e1862bb 4590 && (bfd_link_dll (info)
4f3fedcf
AM
4591 || h->def_dynamic
4592 || h->ref_dynamic))
4593 dynsym = TRUE;
4594 }
4595 else
4596 {
4597 if (! definition)
4598 {
4599 h->ref_dynamic = 1;
4600 hi->ref_dynamic = 1;
4601 }
4602 else
4603 {
4604 h->def_dynamic = 1;
4605 hi->def_dynamic = 1;
4606 }
4607
4608 /* If the indirect symbol has been forced local, don't
4609 make the real symbol dynamic. */
4610 if ((h == hi || !hi->forced_local)
4611 && (h->def_regular
4612 || h->ref_regular
4613 || (h->u.weakdef != NULL
4614 && ! new_weakdef
4615 && h->u.weakdef->dynindx != -1)))
4616 dynsym = TRUE;
4617 }
4618
4619 /* Check to see if we need to add an indirect symbol for
4620 the default name. */
4621 if (definition
4622 || (!override && h->root.type == bfd_link_hash_common))
4623 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4624 sec, value, &old_bfd, &dynsym))
4625 goto error_free_vers;
4ad4eba5
AM
4626
4627 /* Check the alignment when a common symbol is involved. This
4628 can change when a common symbol is overridden by a normal
4629 definition or a common symbol is ignored due to the old
4630 normal definition. We need to make sure the maximum
4631 alignment is maintained. */
a4d8e49b 4632 if ((old_alignment || common)
4ad4eba5
AM
4633 && h->root.type != bfd_link_hash_common)
4634 {
4635 unsigned int common_align;
4636 unsigned int normal_align;
4637 unsigned int symbol_align;
4638 bfd *normal_bfd;
4639 bfd *common_bfd;
4640
3a81e825
AM
4641 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4642 || h->root.type == bfd_link_hash_defweak);
4643
4ad4eba5
AM
4644 symbol_align = ffs (h->root.u.def.value) - 1;
4645 if (h->root.u.def.section->owner != NULL
0616a280
AM
4646 && (h->root.u.def.section->owner->flags
4647 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4648 {
4649 normal_align = h->root.u.def.section->alignment_power;
4650 if (normal_align > symbol_align)
4651 normal_align = symbol_align;
4652 }
4653 else
4654 normal_align = symbol_align;
4655
4656 if (old_alignment)
4657 {
4658 common_align = old_alignment;
4659 common_bfd = old_bfd;
4660 normal_bfd = abfd;
4661 }
4662 else
4663 {
4664 common_align = bfd_log2 (isym->st_value);
4665 common_bfd = abfd;
4666 normal_bfd = old_bfd;
4667 }
4668
4669 if (normal_align < common_align)
d07676f8
NC
4670 {
4671 /* PR binutils/2735 */
4672 if (normal_bfd == NULL)
4eca0228 4673 _bfd_error_handler
695344c0 4674 /* xgettext:c-format */
4f3fedcf
AM
4675 (_("Warning: alignment %u of common symbol `%s' in %B is"
4676 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4677 1 << common_align, name, common_bfd,
4678 1 << normal_align, h->root.u.def.section);
d07676f8 4679 else
4eca0228 4680 _bfd_error_handler
695344c0 4681 /* xgettext:c-format */
d07676f8
NC
4682 (_("Warning: alignment %u of symbol `%s' in %B"
4683 " is smaller than %u in %B"),
c08bb8dd
AM
4684 1 << normal_align, name, normal_bfd,
4685 1 << common_align, common_bfd);
d07676f8 4686 }
4ad4eba5
AM
4687 }
4688
83ad0046 4689 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4690 if (isym->st_size != 0
4691 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4692 && (definition || h->size == 0))
4693 {
83ad0046
L
4694 if (h->size != 0
4695 && h->size != isym->st_size
4696 && ! size_change_ok)
4eca0228 4697 _bfd_error_handler
695344c0 4698 /* xgettext:c-format */
d003868e
AM
4699 (_("Warning: size of symbol `%s' changed"
4700 " from %lu in %B to %lu in %B"),
c08bb8dd
AM
4701 name, (unsigned long) h->size, old_bfd,
4702 (unsigned long) isym->st_size, abfd);
4ad4eba5
AM
4703
4704 h->size = isym->st_size;
4705 }
4706
4707 /* If this is a common symbol, then we always want H->SIZE
4708 to be the size of the common symbol. The code just above
4709 won't fix the size if a common symbol becomes larger. We
4710 don't warn about a size change here, because that is
4f3fedcf 4711 covered by --warn-common. Allow changes between different
fcb93ecf 4712 function types. */
4ad4eba5
AM
4713 if (h->root.type == bfd_link_hash_common)
4714 h->size = h->root.u.c.size;
4715
4716 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4717 && ((definition && !new_weak)
4718 || (old_weak && h->root.type == bfd_link_hash_common)
4719 || h->type == STT_NOTYPE))
4ad4eba5 4720 {
2955ec4c
L
4721 unsigned int type = ELF_ST_TYPE (isym->st_info);
4722
4723 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4724 symbol. */
4725 if (type == STT_GNU_IFUNC
4726 && (abfd->flags & DYNAMIC) != 0)
4727 type = STT_FUNC;
4ad4eba5 4728
2955ec4c
L
4729 if (h->type != type)
4730 {
4731 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4732 /* xgettext:c-format */
4eca0228 4733 _bfd_error_handler
2955ec4c
L
4734 (_("Warning: type of symbol `%s' changed"
4735 " from %d to %d in %B"),
c08bb8dd 4736 name, h->type, type, abfd);
2955ec4c
L
4737
4738 h->type = type;
4739 }
4ad4eba5
AM
4740 }
4741
54ac0771 4742 /* Merge st_other field. */
b8417128 4743 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4744
c3df8c14 4745 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4746 if (definition
4747 && (sec->flags & SEC_DEBUGGING)
4748 && !bfd_link_relocatable (info))
c3df8c14
AM
4749 dynsym = FALSE;
4750
4f3fedcf
AM
4751 /* Nor should we make plugin symbols dynamic. */
4752 if ((abfd->flags & BFD_PLUGIN) != 0)
4753 dynsym = FALSE;
4754
35fc36a8 4755 if (definition)
35399224
L
4756 {
4757 h->target_internal = isym->st_target_internal;
4758 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4759 }
35fc36a8 4760
4ad4eba5
AM
4761 if (definition && !dynamic)
4762 {
4763 char *p = strchr (name, ELF_VER_CHR);
4764 if (p != NULL && p[1] != ELF_VER_CHR)
4765 {
4766 /* Queue non-default versions so that .symver x, x@FOO
4767 aliases can be checked. */
66eb6687 4768 if (!nondeflt_vers)
4ad4eba5 4769 {
66eb6687
AM
4770 amt = ((isymend - isym + 1)
4771 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4772 nondeflt_vers
4773 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4774 if (!nondeflt_vers)
4775 goto error_free_vers;
4ad4eba5 4776 }
66eb6687 4777 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4778 }
4779 }
4780
4781 if (dynsym && h->dynindx == -1)
4782 {
c152c796 4783 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4784 goto error_free_vers;
f6e332e6 4785 if (h->u.weakdef != NULL
4ad4eba5 4786 && ! new_weakdef
f6e332e6 4787 && h->u.weakdef->dynindx == -1)
4ad4eba5 4788 {
66eb6687 4789 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4790 goto error_free_vers;
4791 }
4792 }
1f599d0e 4793 else if (h->dynindx != -1)
4ad4eba5
AM
4794 /* If the symbol already has a dynamic index, but
4795 visibility says it should not be visible, turn it into
4796 a local symbol. */
4797 switch (ELF_ST_VISIBILITY (h->other))
4798 {
4799 case STV_INTERNAL:
4800 case STV_HIDDEN:
4801 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4802 dynsym = FALSE;
4803 break;
4804 }
4805
aef28989
L
4806 /* Don't add DT_NEEDED for references from the dummy bfd nor
4807 for unmatched symbol. */
4ad4eba5 4808 if (!add_needed
aef28989 4809 && matched
4ad4eba5 4810 && definition
010e5ae2 4811 && ((dynsym
ffa9430d 4812 && h->ref_regular_nonweak
4f3fedcf
AM
4813 && (old_bfd == NULL
4814 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4815 || (h->ref_dynamic_nonweak
010e5ae2 4816 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4817 && !on_needed_list (elf_dt_name (abfd),
4818 htab->needed, NULL))))
4ad4eba5
AM
4819 {
4820 int ret;
4821 const char *soname = elf_dt_name (abfd);
4822
16e4ecc0
AM
4823 info->callbacks->minfo ("%!", soname, old_bfd,
4824 h->root.root.string);
4825
4ad4eba5
AM
4826 /* A symbol from a library loaded via DT_NEEDED of some
4827 other library is referenced by a regular object.
e56f61be 4828 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4829 --no-add-needed is used and the reference was not
4830 a weak one. */
4f3fedcf 4831 if (old_bfd != NULL
b918acf9 4832 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4833 {
4eca0228 4834 _bfd_error_handler
695344c0 4835 /* xgettext:c-format */
3cbc5de0 4836 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4837 old_bfd, name);
ff5ac77b 4838 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4839 goto error_free_vers;
4840 }
4841
a50b1753 4842 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4843 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4844
4ad4eba5 4845 add_needed = TRUE;
7e9f0867 4846 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4847 if (ret < 0)
4848 goto error_free_vers;
4849
4850 BFD_ASSERT (ret == 0);
4851 }
4852 }
4853 }
4854
66eb6687
AM
4855 if (extversym != NULL)
4856 {
4857 free (extversym);
4858 extversym = NULL;
4859 }
4860
4861 if (isymbuf != NULL)
4862 {
4863 free (isymbuf);
4864 isymbuf = NULL;
4865 }
4866
4867 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4868 {
4869 unsigned int i;
4870
4871 /* Restore the symbol table. */
f45794cb
AM
4872 old_ent = (char *) old_tab + tabsize;
4873 memset (elf_sym_hashes (abfd), 0,
4874 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4875 htab->root.table.table = old_table;
4876 htab->root.table.size = old_size;
4877 htab->root.table.count = old_count;
66eb6687 4878 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4879 htab->root.undefs = old_undefs;
4880 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4881 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4882 free (old_strtab);
4883 old_strtab = NULL;
66eb6687
AM
4884 for (i = 0; i < htab->root.table.size; i++)
4885 {
4886 struct bfd_hash_entry *p;
4887 struct elf_link_hash_entry *h;
3e0882af
L
4888 bfd_size_type size;
4889 unsigned int alignment_power;
66eb6687
AM
4890
4891 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4892 {
4893 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4894 if (h->root.type == bfd_link_hash_warning)
4895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4896
3e0882af
L
4897 /* Preserve the maximum alignment and size for common
4898 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4899 since it can still be loaded at run time by another
3e0882af
L
4900 dynamic lib. */
4901 if (h->root.type == bfd_link_hash_common)
4902 {
4903 size = h->root.u.c.size;
4904 alignment_power = h->root.u.c.p->alignment_power;
4905 }
4906 else
4907 {
4908 size = 0;
4909 alignment_power = 0;
4910 }
66eb6687
AM
4911 memcpy (p, old_ent, htab->root.table.entsize);
4912 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4913 h = (struct elf_link_hash_entry *) p;
4914 if (h->root.type == bfd_link_hash_warning)
4915 {
4916 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4917 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4918 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4919 }
a4542f1b 4920 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4921 {
4922 if (size > h->root.u.c.size)
4923 h->root.u.c.size = size;
4924 if (alignment_power > h->root.u.c.p->alignment_power)
4925 h->root.u.c.p->alignment_power = alignment_power;
4926 }
66eb6687
AM
4927 }
4928 }
4929
5061a885
AM
4930 /* Make a special call to the linker "notice" function to
4931 tell it that symbols added for crefs may need to be removed. */
e5034e59 4932 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4933 goto error_free_vers;
5061a885 4934
66eb6687
AM
4935 free (old_tab);
4936 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4937 alloc_mark);
4938 if (nondeflt_vers != NULL)
4939 free (nondeflt_vers);
4940 return TRUE;
4941 }
2de92251 4942
66eb6687
AM
4943 if (old_tab != NULL)
4944 {
e5034e59 4945 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4946 goto error_free_vers;
66eb6687
AM
4947 free (old_tab);
4948 old_tab = NULL;
4949 }
4950
c6e8a9a8
L
4951 /* Now that all the symbols from this input file are created, if
4952 not performing a relocatable link, handle .symver foo, foo@BAR
4953 such that any relocs against foo become foo@BAR. */
0e1862bb 4954 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4955 {
ef53be89 4956 size_t cnt, symidx;
4ad4eba5
AM
4957
4958 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4959 {
4960 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4961 char *shortname, *p;
4962
4963 p = strchr (h->root.root.string, ELF_VER_CHR);
4964 if (p == NULL
4965 || (h->root.type != bfd_link_hash_defined
4966 && h->root.type != bfd_link_hash_defweak))
4967 continue;
4968
4969 amt = p - h->root.root.string;
a50b1753 4970 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4971 if (!shortname)
4972 goto error_free_vers;
4ad4eba5
AM
4973 memcpy (shortname, h->root.root.string, amt);
4974 shortname[amt] = '\0';
4975
4976 hi = (struct elf_link_hash_entry *)
66eb6687 4977 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4978 FALSE, FALSE, FALSE);
4979 if (hi != NULL
4980 && hi->root.type == h->root.type
4981 && hi->root.u.def.value == h->root.u.def.value
4982 && hi->root.u.def.section == h->root.u.def.section)
4983 {
4984 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4985 hi->root.type = bfd_link_hash_indirect;
4986 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4987 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4988 sym_hash = elf_sym_hashes (abfd);
4989 if (sym_hash)
4990 for (symidx = 0; symidx < extsymcount; ++symidx)
4991 if (sym_hash[symidx] == hi)
4992 {
4993 sym_hash[symidx] = h;
4994 break;
4995 }
4996 }
4997 free (shortname);
4998 }
4999 free (nondeflt_vers);
5000 nondeflt_vers = NULL;
5001 }
5002
4ad4eba5
AM
5003 /* Now set the weakdefs field correctly for all the weak defined
5004 symbols we found. The only way to do this is to search all the
5005 symbols. Since we only need the information for non functions in
5006 dynamic objects, that's the only time we actually put anything on
5007 the list WEAKS. We need this information so that if a regular
5008 object refers to a symbol defined weakly in a dynamic object, the
5009 real symbol in the dynamic object is also put in the dynamic
5010 symbols; we also must arrange for both symbols to point to the
5011 same memory location. We could handle the general case of symbol
5012 aliasing, but a general symbol alias can only be generated in
5013 assembler code, handling it correctly would be very time
5014 consuming, and other ELF linkers don't handle general aliasing
5015 either. */
5016 if (weaks != NULL)
5017 {
5018 struct elf_link_hash_entry **hpp;
5019 struct elf_link_hash_entry **hppend;
5020 struct elf_link_hash_entry **sorted_sym_hash;
5021 struct elf_link_hash_entry *h;
5022 size_t sym_count;
5023
5024 /* Since we have to search the whole symbol list for each weak
5025 defined symbol, search time for N weak defined symbols will be
5026 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5027 amt = extsymcount;
5028 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5029 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5030 if (sorted_sym_hash == NULL)
5031 goto error_return;
5032 sym_hash = sorted_sym_hash;
5033 hpp = elf_sym_hashes (abfd);
5034 hppend = hpp + extsymcount;
5035 sym_count = 0;
5036 for (; hpp < hppend; hpp++)
5037 {
5038 h = *hpp;
5039 if (h != NULL
5040 && h->root.type == bfd_link_hash_defined
fcb93ecf 5041 && !bed->is_function_type (h->type))
4ad4eba5
AM
5042 {
5043 *sym_hash = h;
5044 sym_hash++;
5045 sym_count++;
5046 }
5047 }
5048
5049 qsort (sorted_sym_hash, sym_count,
5050 sizeof (struct elf_link_hash_entry *),
5051 elf_sort_symbol);
5052
5053 while (weaks != NULL)
5054 {
5055 struct elf_link_hash_entry *hlook;
5056 asection *slook;
5057 bfd_vma vlook;
ed54588d 5058 size_t i, j, idx = 0;
4ad4eba5
AM
5059
5060 hlook = weaks;
f6e332e6
AM
5061 weaks = hlook->u.weakdef;
5062 hlook->u.weakdef = NULL;
4ad4eba5
AM
5063
5064 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5065 || hlook->root.type == bfd_link_hash_defweak
5066 || hlook->root.type == bfd_link_hash_common
5067 || hlook->root.type == bfd_link_hash_indirect);
5068 slook = hlook->root.u.def.section;
5069 vlook = hlook->root.u.def.value;
5070
4ad4eba5
AM
5071 i = 0;
5072 j = sym_count;
14160578 5073 while (i != j)
4ad4eba5
AM
5074 {
5075 bfd_signed_vma vdiff;
5076 idx = (i + j) / 2;
14160578 5077 h = sorted_sym_hash[idx];
4ad4eba5
AM
5078 vdiff = vlook - h->root.u.def.value;
5079 if (vdiff < 0)
5080 j = idx;
5081 else if (vdiff > 0)
5082 i = idx + 1;
5083 else
5084 {
d3435ae8 5085 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5086 if (sdiff < 0)
5087 j = idx;
5088 else if (sdiff > 0)
5089 i = idx + 1;
5090 else
14160578 5091 break;
4ad4eba5
AM
5092 }
5093 }
5094
5095 /* We didn't find a value/section match. */
14160578 5096 if (i == j)
4ad4eba5
AM
5097 continue;
5098
14160578
AM
5099 /* With multiple aliases, or when the weak symbol is already
5100 strongly defined, we have multiple matching symbols and
5101 the binary search above may land on any of them. Step
5102 one past the matching symbol(s). */
5103 while (++idx != j)
5104 {
5105 h = sorted_sym_hash[idx];
5106 if (h->root.u.def.section != slook
5107 || h->root.u.def.value != vlook)
5108 break;
5109 }
5110
5111 /* Now look back over the aliases. Since we sorted by size
5112 as well as value and section, we'll choose the one with
5113 the largest size. */
5114 while (idx-- != i)
4ad4eba5 5115 {
14160578 5116 h = sorted_sym_hash[idx];
4ad4eba5
AM
5117
5118 /* Stop if value or section doesn't match. */
14160578
AM
5119 if (h->root.u.def.section != slook
5120 || h->root.u.def.value != vlook)
4ad4eba5
AM
5121 break;
5122 else if (h != hlook)
5123 {
f6e332e6 5124 hlook->u.weakdef = h;
4ad4eba5
AM
5125
5126 /* If the weak definition is in the list of dynamic
5127 symbols, make sure the real definition is put
5128 there as well. */
5129 if (hlook->dynindx != -1 && h->dynindx == -1)
5130 {
c152c796 5131 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5132 {
5133 err_free_sym_hash:
5134 free (sorted_sym_hash);
5135 goto error_return;
5136 }
4ad4eba5
AM
5137 }
5138
5139 /* If the real definition is in the list of dynamic
5140 symbols, make sure the weak definition is put
5141 there as well. If we don't do this, then the
5142 dynamic loader might not merge the entries for the
5143 real definition and the weak definition. */
5144 if (h->dynindx != -1 && hlook->dynindx == -1)
5145 {
c152c796 5146 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5147 goto err_free_sym_hash;
4ad4eba5
AM
5148 }
5149 break;
5150 }
5151 }
5152 }
5153
5154 free (sorted_sym_hash);
5155 }
5156
33177bb1
AM
5157 if (bed->check_directives
5158 && !(*bed->check_directives) (abfd, info))
5159 return FALSE;
85fbca6a 5160
d9689752
L
5161 if (!info->check_relocs_after_open_input
5162 && !_bfd_elf_link_check_relocs (abfd, info))
5163 return FALSE;
4ad4eba5
AM
5164
5165 /* If this is a non-traditional link, try to optimize the handling
5166 of the .stab/.stabstr sections. */
5167 if (! dynamic
5168 && ! info->traditional_format
66eb6687 5169 && is_elf_hash_table (htab)
4ad4eba5
AM
5170 && (info->strip != strip_all && info->strip != strip_debugger))
5171 {
5172 asection *stabstr;
5173
5174 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5175 if (stabstr != NULL)
5176 {
5177 bfd_size_type string_offset = 0;
5178 asection *stab;
5179
5180 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5181 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5182 && (!stab->name[5] ||
5183 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5184 && (stab->flags & SEC_MERGE) == 0
5185 && !bfd_is_abs_section (stab->output_section))
5186 {
5187 struct bfd_elf_section_data *secdata;
5188
5189 secdata = elf_section_data (stab);
66eb6687
AM
5190 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5191 stabstr, &secdata->sec_info,
4ad4eba5
AM
5192 &string_offset))
5193 goto error_return;
5194 if (secdata->sec_info)
dbaa2011 5195 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5196 }
5197 }
5198 }
5199
66eb6687 5200 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5201 {
5202 /* Add this bfd to the loaded list. */
5203 struct elf_link_loaded_list *n;
5204
ca4be51c 5205 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5206 if (n == NULL)
5207 goto error_return;
5208 n->abfd = abfd;
66eb6687
AM
5209 n->next = htab->loaded;
5210 htab->loaded = n;
4ad4eba5
AM
5211 }
5212
5213 return TRUE;
5214
5215 error_free_vers:
66eb6687
AM
5216 if (old_tab != NULL)
5217 free (old_tab);
5b677558
AM
5218 if (old_strtab != NULL)
5219 free (old_strtab);
4ad4eba5
AM
5220 if (nondeflt_vers != NULL)
5221 free (nondeflt_vers);
5222 if (extversym != NULL)
5223 free (extversym);
5224 error_free_sym:
5225 if (isymbuf != NULL)
5226 free (isymbuf);
5227 error_return:
5228 return FALSE;
5229}
5230
8387904d
AM
5231/* Return the linker hash table entry of a symbol that might be
5232 satisfied by an archive symbol. Return -1 on error. */
5233
5234struct elf_link_hash_entry *
5235_bfd_elf_archive_symbol_lookup (bfd *abfd,
5236 struct bfd_link_info *info,
5237 const char *name)
5238{
5239 struct elf_link_hash_entry *h;
5240 char *p, *copy;
5241 size_t len, first;
5242
2a41f396 5243 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5244 if (h != NULL)
5245 return h;
5246
5247 /* If this is a default version (the name contains @@), look up the
5248 symbol again with only one `@' as well as without the version.
5249 The effect is that references to the symbol with and without the
5250 version will be matched by the default symbol in the archive. */
5251
5252 p = strchr (name, ELF_VER_CHR);
5253 if (p == NULL || p[1] != ELF_VER_CHR)
5254 return h;
5255
5256 /* First check with only one `@'. */
5257 len = strlen (name);
a50b1753 5258 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5259 if (copy == NULL)
5260 return (struct elf_link_hash_entry *) 0 - 1;
5261
5262 first = p - name + 1;
5263 memcpy (copy, name, first);
5264 memcpy (copy + first, name + first + 1, len - first);
5265
2a41f396 5266 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5267 if (h == NULL)
5268 {
5269 /* We also need to check references to the symbol without the
5270 version. */
5271 copy[first - 1] = '\0';
5272 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5273 FALSE, FALSE, TRUE);
8387904d
AM
5274 }
5275
5276 bfd_release (abfd, copy);
5277 return h;
5278}
5279
0ad989f9 5280/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5281 don't use _bfd_generic_link_add_archive_symbols because we need to
5282 handle versioned symbols.
0ad989f9
L
5283
5284 Fortunately, ELF archive handling is simpler than that done by
5285 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5286 oddities. In ELF, if we find a symbol in the archive map, and the
5287 symbol is currently undefined, we know that we must pull in that
5288 object file.
5289
5290 Unfortunately, we do have to make multiple passes over the symbol
5291 table until nothing further is resolved. */
5292
4ad4eba5
AM
5293static bfd_boolean
5294elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5295{
5296 symindex c;
13e570f8 5297 unsigned char *included = NULL;
0ad989f9
L
5298 carsym *symdefs;
5299 bfd_boolean loop;
5300 bfd_size_type amt;
8387904d
AM
5301 const struct elf_backend_data *bed;
5302 struct elf_link_hash_entry * (*archive_symbol_lookup)
5303 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5304
5305 if (! bfd_has_map (abfd))
5306 {
5307 /* An empty archive is a special case. */
5308 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5309 return TRUE;
5310 bfd_set_error (bfd_error_no_armap);
5311 return FALSE;
5312 }
5313
5314 /* Keep track of all symbols we know to be already defined, and all
5315 files we know to be already included. This is to speed up the
5316 second and subsequent passes. */
5317 c = bfd_ardata (abfd)->symdef_count;
5318 if (c == 0)
5319 return TRUE;
5320 amt = c;
13e570f8
AM
5321 amt *= sizeof (*included);
5322 included = (unsigned char *) bfd_zmalloc (amt);
5323 if (included == NULL)
5324 return FALSE;
0ad989f9
L
5325
5326 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5327 bed = get_elf_backend_data (abfd);
5328 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5329
5330 do
5331 {
5332 file_ptr last;
5333 symindex i;
5334 carsym *symdef;
5335 carsym *symdefend;
5336
5337 loop = FALSE;
5338 last = -1;
5339
5340 symdef = symdefs;
5341 symdefend = symdef + c;
5342 for (i = 0; symdef < symdefend; symdef++, i++)
5343 {
5344 struct elf_link_hash_entry *h;
5345 bfd *element;
5346 struct bfd_link_hash_entry *undefs_tail;
5347 symindex mark;
5348
13e570f8 5349 if (included[i])
0ad989f9
L
5350 continue;
5351 if (symdef->file_offset == last)
5352 {
5353 included[i] = TRUE;
5354 continue;
5355 }
5356
8387904d
AM
5357 h = archive_symbol_lookup (abfd, info, symdef->name);
5358 if (h == (struct elf_link_hash_entry *) 0 - 1)
5359 goto error_return;
0ad989f9
L
5360
5361 if (h == NULL)
5362 continue;
5363
5364 if (h->root.type == bfd_link_hash_common)
5365 {
5366 /* We currently have a common symbol. The archive map contains
5367 a reference to this symbol, so we may want to include it. We
5368 only want to include it however, if this archive element
5369 contains a definition of the symbol, not just another common
5370 declaration of it.
5371
5372 Unfortunately some archivers (including GNU ar) will put
5373 declarations of common symbols into their archive maps, as
5374 well as real definitions, so we cannot just go by the archive
5375 map alone. Instead we must read in the element's symbol
5376 table and check that to see what kind of symbol definition
5377 this is. */
5378 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5379 continue;
5380 }
5381 else if (h->root.type != bfd_link_hash_undefined)
5382 {
5383 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5384 /* Symbol must be defined. Don't check it again. */
5385 included[i] = TRUE;
0ad989f9
L
5386 continue;
5387 }
5388
5389 /* We need to include this archive member. */
5390 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5391 if (element == NULL)
5392 goto error_return;
5393
5394 if (! bfd_check_format (element, bfd_object))
5395 goto error_return;
5396
0ad989f9
L
5397 undefs_tail = info->hash->undefs_tail;
5398
0e144ba7
AM
5399 if (!(*info->callbacks
5400 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5401 continue;
0e144ba7 5402 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5403 goto error_return;
5404
5405 /* If there are any new undefined symbols, we need to make
5406 another pass through the archive in order to see whether
5407 they can be defined. FIXME: This isn't perfect, because
5408 common symbols wind up on undefs_tail and because an
5409 undefined symbol which is defined later on in this pass
5410 does not require another pass. This isn't a bug, but it
5411 does make the code less efficient than it could be. */
5412 if (undefs_tail != info->hash->undefs_tail)
5413 loop = TRUE;
5414
5415 /* Look backward to mark all symbols from this object file
5416 which we have already seen in this pass. */
5417 mark = i;
5418 do
5419 {
5420 included[mark] = TRUE;
5421 if (mark == 0)
5422 break;
5423 --mark;
5424 }
5425 while (symdefs[mark].file_offset == symdef->file_offset);
5426
5427 /* We mark subsequent symbols from this object file as we go
5428 on through the loop. */
5429 last = symdef->file_offset;
5430 }
5431 }
5432 while (loop);
5433
0ad989f9
L
5434 free (included);
5435
5436 return TRUE;
5437
5438 error_return:
0ad989f9
L
5439 if (included != NULL)
5440 free (included);
5441 return FALSE;
5442}
4ad4eba5
AM
5443
5444/* Given an ELF BFD, add symbols to the global hash table as
5445 appropriate. */
5446
5447bfd_boolean
5448bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5449{
5450 switch (bfd_get_format (abfd))
5451 {
5452 case bfd_object:
5453 return elf_link_add_object_symbols (abfd, info);
5454 case bfd_archive:
5455 return elf_link_add_archive_symbols (abfd, info);
5456 default:
5457 bfd_set_error (bfd_error_wrong_format);
5458 return FALSE;
5459 }
5460}
5a580b3a 5461\f
14b1c01e
AM
5462struct hash_codes_info
5463{
5464 unsigned long *hashcodes;
5465 bfd_boolean error;
5466};
a0c8462f 5467
5a580b3a
AM
5468/* This function will be called though elf_link_hash_traverse to store
5469 all hash value of the exported symbols in an array. */
5470
5471static bfd_boolean
5472elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5473{
a50b1753 5474 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5475 const char *name;
5a580b3a
AM
5476 unsigned long ha;
5477 char *alc = NULL;
5478
5a580b3a
AM
5479 /* Ignore indirect symbols. These are added by the versioning code. */
5480 if (h->dynindx == -1)
5481 return TRUE;
5482
5483 name = h->root.root.string;
422f1182 5484 if (h->versioned >= versioned)
5a580b3a 5485 {
422f1182
L
5486 char *p = strchr (name, ELF_VER_CHR);
5487 if (p != NULL)
14b1c01e 5488 {
422f1182
L
5489 alc = (char *) bfd_malloc (p - name + 1);
5490 if (alc == NULL)
5491 {
5492 inf->error = TRUE;
5493 return FALSE;
5494 }
5495 memcpy (alc, name, p - name);
5496 alc[p - name] = '\0';
5497 name = alc;
14b1c01e 5498 }
5a580b3a
AM
5499 }
5500
5501 /* Compute the hash value. */
5502 ha = bfd_elf_hash (name);
5503
5504 /* Store the found hash value in the array given as the argument. */
14b1c01e 5505 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5506
5507 /* And store it in the struct so that we can put it in the hash table
5508 later. */
f6e332e6 5509 h->u.elf_hash_value = ha;
5a580b3a
AM
5510
5511 if (alc != NULL)
5512 free (alc);
5513
5514 return TRUE;
5515}
5516
fdc90cb4
JJ
5517struct collect_gnu_hash_codes
5518{
5519 bfd *output_bfd;
5520 const struct elf_backend_data *bed;
5521 unsigned long int nsyms;
5522 unsigned long int maskbits;
5523 unsigned long int *hashcodes;
5524 unsigned long int *hashval;
5525 unsigned long int *indx;
5526 unsigned long int *counts;
5527 bfd_vma *bitmask;
5528 bfd_byte *contents;
5529 long int min_dynindx;
5530 unsigned long int bucketcount;
5531 unsigned long int symindx;
5532 long int local_indx;
5533 long int shift1, shift2;
5534 unsigned long int mask;
14b1c01e 5535 bfd_boolean error;
fdc90cb4
JJ
5536};
5537
5538/* This function will be called though elf_link_hash_traverse to store
5539 all hash value of the exported symbols in an array. */
5540
5541static bfd_boolean
5542elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5543{
a50b1753 5544 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5545 const char *name;
fdc90cb4
JJ
5546 unsigned long ha;
5547 char *alc = NULL;
5548
fdc90cb4
JJ
5549 /* Ignore indirect symbols. These are added by the versioning code. */
5550 if (h->dynindx == -1)
5551 return TRUE;
5552
5553 /* Ignore also local symbols and undefined symbols. */
5554 if (! (*s->bed->elf_hash_symbol) (h))
5555 return TRUE;
5556
5557 name = h->root.root.string;
422f1182 5558 if (h->versioned >= versioned)
fdc90cb4 5559 {
422f1182
L
5560 char *p = strchr (name, ELF_VER_CHR);
5561 if (p != NULL)
14b1c01e 5562 {
422f1182
L
5563 alc = (char *) bfd_malloc (p - name + 1);
5564 if (alc == NULL)
5565 {
5566 s->error = TRUE;
5567 return FALSE;
5568 }
5569 memcpy (alc, name, p - name);
5570 alc[p - name] = '\0';
5571 name = alc;
14b1c01e 5572 }
fdc90cb4
JJ
5573 }
5574
5575 /* Compute the hash value. */
5576 ha = bfd_elf_gnu_hash (name);
5577
5578 /* Store the found hash value in the array for compute_bucket_count,
5579 and also for .dynsym reordering purposes. */
5580 s->hashcodes[s->nsyms] = ha;
5581 s->hashval[h->dynindx] = ha;
5582 ++s->nsyms;
5583 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5584 s->min_dynindx = h->dynindx;
5585
5586 if (alc != NULL)
5587 free (alc);
5588
5589 return TRUE;
5590}
5591
5592/* This function will be called though elf_link_hash_traverse to do
5593 final dynaminc symbol renumbering. */
5594
5595static bfd_boolean
5596elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5597{
a50b1753 5598 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5599 unsigned long int bucket;
5600 unsigned long int val;
5601
fdc90cb4
JJ
5602 /* Ignore indirect symbols. */
5603 if (h->dynindx == -1)
5604 return TRUE;
5605
5606 /* Ignore also local symbols and undefined symbols. */
5607 if (! (*s->bed->elf_hash_symbol) (h))
5608 {
5609 if (h->dynindx >= s->min_dynindx)
5610 h->dynindx = s->local_indx++;
5611 return TRUE;
5612 }
5613
5614 bucket = s->hashval[h->dynindx] % s->bucketcount;
5615 val = (s->hashval[h->dynindx] >> s->shift1)
5616 & ((s->maskbits >> s->shift1) - 1);
5617 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5618 s->bitmask[val]
5619 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5620 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5621 if (s->counts[bucket] == 1)
5622 /* Last element terminates the chain. */
5623 val |= 1;
5624 bfd_put_32 (s->output_bfd, val,
5625 s->contents + (s->indx[bucket] - s->symindx) * 4);
5626 --s->counts[bucket];
5627 h->dynindx = s->indx[bucket]++;
5628 return TRUE;
5629}
5630
5631/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5632
5633bfd_boolean
5634_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5635{
5636 return !(h->forced_local
5637 || h->root.type == bfd_link_hash_undefined
5638 || h->root.type == bfd_link_hash_undefweak
5639 || ((h->root.type == bfd_link_hash_defined
5640 || h->root.type == bfd_link_hash_defweak)
5641 && h->root.u.def.section->output_section == NULL));
5642}
5643
5a580b3a
AM
5644/* Array used to determine the number of hash table buckets to use
5645 based on the number of symbols there are. If there are fewer than
5646 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5647 fewer than 37 we use 17 buckets, and so forth. We never use more
5648 than 32771 buckets. */
5649
5650static const size_t elf_buckets[] =
5651{
5652 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5653 16411, 32771, 0
5654};
5655
5656/* Compute bucket count for hashing table. We do not use a static set
5657 of possible tables sizes anymore. Instead we determine for all
5658 possible reasonable sizes of the table the outcome (i.e., the
5659 number of collisions etc) and choose the best solution. The
5660 weighting functions are not too simple to allow the table to grow
5661 without bounds. Instead one of the weighting factors is the size.
5662 Therefore the result is always a good payoff between few collisions
5663 (= short chain lengths) and table size. */
5664static size_t
b20dd2ce 5665compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5666 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5667 unsigned long int nsyms,
5668 int gnu_hash)
5a580b3a 5669{
5a580b3a 5670 size_t best_size = 0;
5a580b3a 5671 unsigned long int i;
5a580b3a 5672
5a580b3a
AM
5673 /* We have a problem here. The following code to optimize the table
5674 size requires an integer type with more the 32 bits. If
5675 BFD_HOST_U_64_BIT is set we know about such a type. */
5676#ifdef BFD_HOST_U_64_BIT
5677 if (info->optimize)
5678 {
5a580b3a
AM
5679 size_t minsize;
5680 size_t maxsize;
5681 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5682 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5683 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5684 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5685 unsigned long int *counts;
d40f3da9 5686 bfd_size_type amt;
0883b6e0 5687 unsigned int no_improvement_count = 0;
5a580b3a
AM
5688
5689 /* Possible optimization parameters: if we have NSYMS symbols we say
5690 that the hashing table must at least have NSYMS/4 and at most
5691 2*NSYMS buckets. */
5692 minsize = nsyms / 4;
5693 if (minsize == 0)
5694 minsize = 1;
5695 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5696 if (gnu_hash)
5697 {
5698 if (minsize < 2)
5699 minsize = 2;
5700 if ((best_size & 31) == 0)
5701 ++best_size;
5702 }
5a580b3a
AM
5703
5704 /* Create array where we count the collisions in. We must use bfd_malloc
5705 since the size could be large. */
5706 amt = maxsize;
5707 amt *= sizeof (unsigned long int);
a50b1753 5708 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5709 if (counts == NULL)
fdc90cb4 5710 return 0;
5a580b3a
AM
5711
5712 /* Compute the "optimal" size for the hash table. The criteria is a
5713 minimal chain length. The minor criteria is (of course) the size
5714 of the table. */
5715 for (i = minsize; i < maxsize; ++i)
5716 {
5717 /* Walk through the array of hashcodes and count the collisions. */
5718 BFD_HOST_U_64_BIT max;
5719 unsigned long int j;
5720 unsigned long int fact;
5721
fdc90cb4
JJ
5722 if (gnu_hash && (i & 31) == 0)
5723 continue;
5724
5a580b3a
AM
5725 memset (counts, '\0', i * sizeof (unsigned long int));
5726
5727 /* Determine how often each hash bucket is used. */
5728 for (j = 0; j < nsyms; ++j)
5729 ++counts[hashcodes[j] % i];
5730
5731 /* For the weight function we need some information about the
5732 pagesize on the target. This is information need not be 100%
5733 accurate. Since this information is not available (so far) we
5734 define it here to a reasonable default value. If it is crucial
5735 to have a better value some day simply define this value. */
5736# ifndef BFD_TARGET_PAGESIZE
5737# define BFD_TARGET_PAGESIZE (4096)
5738# endif
5739
fdc90cb4
JJ
5740 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5741 and the chains. */
5742 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5743
5744# if 1
5745 /* Variant 1: optimize for short chains. We add the squares
5746 of all the chain lengths (which favors many small chain
5747 over a few long chains). */
5748 for (j = 0; j < i; ++j)
5749 max += counts[j] * counts[j];
5750
5751 /* This adds penalties for the overall size of the table. */
fdc90cb4 5752 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5753 max *= fact * fact;
5754# else
5755 /* Variant 2: Optimize a lot more for small table. Here we
5756 also add squares of the size but we also add penalties for
5757 empty slots (the +1 term). */
5758 for (j = 0; j < i; ++j)
5759 max += (1 + counts[j]) * (1 + counts[j]);
5760
5761 /* The overall size of the table is considered, but not as
5762 strong as in variant 1, where it is squared. */
fdc90cb4 5763 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5764 max *= fact;
5765# endif
5766
5767 /* Compare with current best results. */
5768 if (max < best_chlen)
5769 {
5770 best_chlen = max;
5771 best_size = i;
ca4be51c 5772 no_improvement_count = 0;
5a580b3a 5773 }
0883b6e0
NC
5774 /* PR 11843: Avoid futile long searches for the best bucket size
5775 when there are a large number of symbols. */
5776 else if (++no_improvement_count == 100)
5777 break;
5a580b3a
AM
5778 }
5779
5780 free (counts);
5781 }
5782 else
5783#endif /* defined (BFD_HOST_U_64_BIT) */
5784 {
5785 /* This is the fallback solution if no 64bit type is available or if we
5786 are not supposed to spend much time on optimizations. We select the
5787 bucket count using a fixed set of numbers. */
5788 for (i = 0; elf_buckets[i] != 0; i++)
5789 {
5790 best_size = elf_buckets[i];
fdc90cb4 5791 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5792 break;
5793 }
fdc90cb4
JJ
5794 if (gnu_hash && best_size < 2)
5795 best_size = 2;
5a580b3a
AM
5796 }
5797
5a580b3a
AM
5798 return best_size;
5799}
5800
d0bf826b
AM
5801/* Size any SHT_GROUP section for ld -r. */
5802
5803bfd_boolean
5804_bfd_elf_size_group_sections (struct bfd_link_info *info)
5805{
5806 bfd *ibfd;
5807
c72f2fb2 5808 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5809 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5810 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5811 return FALSE;
5812 return TRUE;
5813}
5814
04c3a755
NS
5815/* Set a default stack segment size. The value in INFO wins. If it
5816 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5817 undefined it is initialized. */
5818
5819bfd_boolean
5820bfd_elf_stack_segment_size (bfd *output_bfd,
5821 struct bfd_link_info *info,
5822 const char *legacy_symbol,
5823 bfd_vma default_size)
5824{
5825 struct elf_link_hash_entry *h = NULL;
5826
5827 /* Look for legacy symbol. */
5828 if (legacy_symbol)
5829 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5830 FALSE, FALSE, FALSE);
5831 if (h && (h->root.type == bfd_link_hash_defined
5832 || h->root.type == bfd_link_hash_defweak)
5833 && h->def_regular
5834 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5835 {
5836 /* The symbol has no type if specified on the command line. */
5837 h->type = STT_OBJECT;
5838 if (info->stacksize)
695344c0 5839 /* xgettext:c-format */
4eca0228
AM
5840 _bfd_error_handler (_("%B: stack size specified and %s set"),
5841 output_bfd, legacy_symbol);
04c3a755 5842 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5843 /* xgettext:c-format */
4eca0228
AM
5844 _bfd_error_handler (_("%B: %s not absolute"),
5845 output_bfd, legacy_symbol);
04c3a755
NS
5846 else
5847 info->stacksize = h->root.u.def.value;
5848 }
5849
5850 if (!info->stacksize)
5851 /* If the user didn't set a size, or explicitly inhibit the
5852 size, set it now. */
5853 info->stacksize = default_size;
5854
5855 /* Provide the legacy symbol, if it is referenced. */
5856 if (h && (h->root.type == bfd_link_hash_undefined
5857 || h->root.type == bfd_link_hash_undefweak))
5858 {
5859 struct bfd_link_hash_entry *bh = NULL;
5860
5861 if (!(_bfd_generic_link_add_one_symbol
5862 (info, output_bfd, legacy_symbol,
5863 BSF_GLOBAL, bfd_abs_section_ptr,
5864 info->stacksize >= 0 ? info->stacksize : 0,
5865 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5866 return FALSE;
5867
5868 h = (struct elf_link_hash_entry *) bh;
5869 h->def_regular = 1;
5870 h->type = STT_OBJECT;
5871 }
5872
5873 return TRUE;
5874}
5875
b531344c
MR
5876/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5877
5878struct elf_gc_sweep_symbol_info
5879{
5880 struct bfd_link_info *info;
5881 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5882 bfd_boolean);
5883};
5884
5885static bfd_boolean
5886elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5887{
5888 if (!h->mark
5889 && (((h->root.type == bfd_link_hash_defined
5890 || h->root.type == bfd_link_hash_defweak)
5891 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5892 && h->root.u.def.section->gc_mark))
5893 || h->root.type == bfd_link_hash_undefined
5894 || h->root.type == bfd_link_hash_undefweak))
5895 {
5896 struct elf_gc_sweep_symbol_info *inf;
5897
5898 inf = (struct elf_gc_sweep_symbol_info *) data;
5899 (*inf->hide_symbol) (inf->info, h, TRUE);
5900 h->def_regular = 0;
5901 h->ref_regular = 0;
5902 h->ref_regular_nonweak = 0;
5903 }
5904
5905 return TRUE;
5906}
5907
5a580b3a
AM
5908/* Set up the sizes and contents of the ELF dynamic sections. This is
5909 called by the ELF linker emulation before_allocation routine. We
5910 must set the sizes of the sections before the linker sets the
5911 addresses of the various sections. */
5912
5913bfd_boolean
5914bfd_elf_size_dynamic_sections (bfd *output_bfd,
5915 const char *soname,
5916 const char *rpath,
5917 const char *filter_shlib,
7ee314fa
AM
5918 const char *audit,
5919 const char *depaudit,
5a580b3a
AM
5920 const char * const *auxiliary_filters,
5921 struct bfd_link_info *info,
fd91d419 5922 asection **sinterpptr)
5a580b3a 5923{
ef53be89 5924 size_t soname_indx;
5a580b3a
AM
5925 bfd *dynobj;
5926 const struct elf_backend_data *bed;
5a580b3a
AM
5927
5928 *sinterpptr = NULL;
5929
ef53be89 5930 soname_indx = (size_t) -1;
5a580b3a
AM
5931
5932 if (!is_elf_hash_table (info->hash))
5933 return TRUE;
5934
5a580b3a
AM
5935 dynobj = elf_hash_table (info)->dynobj;
5936
9a2a56cc 5937 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 5938 {
902e9fc7
MR
5939 struct bfd_elf_version_tree *verdefs;
5940 struct elf_info_failed asvinfo;
5a580b3a
AM
5941 struct bfd_elf_version_tree *t;
5942 struct bfd_elf_version_expr *d;
902e9fc7 5943 struct elf_info_failed eif;
5a580b3a 5944 bfd_boolean all_defined;
902e9fc7 5945 asection *s;
7ee314fa 5946
5a580b3a 5947 eif.info = info;
5a580b3a
AM
5948 eif.failed = FALSE;
5949
5950 /* If we are supposed to export all symbols into the dynamic symbol
5951 table (this is not the normal case), then do so. */
55255dae 5952 if (info->export_dynamic
0e1862bb 5953 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5954 {
5955 elf_link_hash_traverse (elf_hash_table (info),
5956 _bfd_elf_export_symbol,
5957 &eif);
5958 if (eif.failed)
5959 return FALSE;
5960 }
5961
5962 /* Make all global versions with definition. */
fd91d419 5963 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5964 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5965 if (!d->symver && d->literal)
5a580b3a
AM
5966 {
5967 const char *verstr, *name;
5968 size_t namelen, verlen, newlen;
93252b1c 5969 char *newname, *p, leading_char;
5a580b3a
AM
5970 struct elf_link_hash_entry *newh;
5971
93252b1c 5972 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5973 name = d->pattern;
93252b1c 5974 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5975 verstr = t->name;
5976 verlen = strlen (verstr);
5977 newlen = namelen + verlen + 3;
5978
a50b1753 5979 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5980 if (newname == NULL)
5981 return FALSE;
93252b1c
MF
5982 newname[0] = leading_char;
5983 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5984
5985 /* Check the hidden versioned definition. */
5986 p = newname + namelen;
5987 *p++ = ELF_VER_CHR;
5988 memcpy (p, verstr, verlen + 1);
5989 newh = elf_link_hash_lookup (elf_hash_table (info),
5990 newname, FALSE, FALSE,
5991 FALSE);
5992 if (newh == NULL
5993 || (newh->root.type != bfd_link_hash_defined
5994 && newh->root.type != bfd_link_hash_defweak))
5995 {
5996 /* Check the default versioned definition. */
5997 *p++ = ELF_VER_CHR;
5998 memcpy (p, verstr, verlen + 1);
5999 newh = elf_link_hash_lookup (elf_hash_table (info),
6000 newname, FALSE, FALSE,
6001 FALSE);
6002 }
6003 free (newname);
6004
6005 /* Mark this version if there is a definition and it is
6006 not defined in a shared object. */
6007 if (newh != NULL
f5385ebf 6008 && !newh->def_dynamic
5a580b3a
AM
6009 && (newh->root.type == bfd_link_hash_defined
6010 || newh->root.type == bfd_link_hash_defweak))
6011 d->symver = 1;
6012 }
6013
6014 /* Attach all the symbols to their version information. */
5a580b3a 6015 asvinfo.info = info;
5a580b3a
AM
6016 asvinfo.failed = FALSE;
6017
6018 elf_link_hash_traverse (elf_hash_table (info),
6019 _bfd_elf_link_assign_sym_version,
6020 &asvinfo);
6021 if (asvinfo.failed)
6022 return FALSE;
6023
6024 if (!info->allow_undefined_version)
6025 {
6026 /* Check if all global versions have a definition. */
6027 all_defined = TRUE;
fd91d419 6028 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6029 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6030 if (d->literal && !d->symver && !d->script)
5a580b3a 6031 {
4eca0228 6032 _bfd_error_handler
5a580b3a
AM
6033 (_("%s: undefined version: %s"),
6034 d->pattern, t->name);
6035 all_defined = FALSE;
6036 }
6037
6038 if (!all_defined)
6039 {
6040 bfd_set_error (bfd_error_bad_value);
6041 return FALSE;
6042 }
6043 }
6044
902e9fc7
MR
6045 /* Set up the version definition section. */
6046 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6047 BFD_ASSERT (s != NULL);
5a580b3a 6048
902e9fc7
MR
6049 /* We may have created additional version definitions if we are
6050 just linking a regular application. */
6051 verdefs = info->version_info;
5a580b3a 6052
902e9fc7
MR
6053 /* Skip anonymous version tag. */
6054 if (verdefs != NULL && verdefs->vernum == 0)
6055 verdefs = verdefs->next;
5a580b3a 6056
902e9fc7
MR
6057 if (verdefs == NULL && !info->create_default_symver)
6058 s->flags |= SEC_EXCLUDE;
6059 else
5a580b3a 6060 {
902e9fc7
MR
6061 unsigned int cdefs;
6062 bfd_size_type size;
6063 bfd_byte *p;
6064 Elf_Internal_Verdef def;
6065 Elf_Internal_Verdaux defaux;
6066 struct bfd_link_hash_entry *bh;
6067 struct elf_link_hash_entry *h;
6068 const char *name;
5a580b3a 6069
902e9fc7
MR
6070 cdefs = 0;
6071 size = 0;
5a580b3a 6072
902e9fc7
MR
6073 /* Make space for the base version. */
6074 size += sizeof (Elf_External_Verdef);
6075 size += sizeof (Elf_External_Verdaux);
6076 ++cdefs;
6077
6078 /* Make space for the default version. */
6079 if (info->create_default_symver)
6080 {
6081 size += sizeof (Elf_External_Verdef);
6082 ++cdefs;
3e3b46e5
PB
6083 }
6084
5a580b3a
AM
6085 for (t = verdefs; t != NULL; t = t->next)
6086 {
6087 struct bfd_elf_version_deps *n;
6088
a6cc6b3b
RO
6089 /* Don't emit base version twice. */
6090 if (t->vernum == 0)
6091 continue;
6092
5a580b3a
AM
6093 size += sizeof (Elf_External_Verdef);
6094 size += sizeof (Elf_External_Verdaux);
6095 ++cdefs;
6096
6097 for (n = t->deps; n != NULL; n = n->next)
6098 size += sizeof (Elf_External_Verdaux);
6099 }
6100
eea6121a 6101 s->size = size;
a50b1753 6102 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6103 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6104 return FALSE;
6105
6106 /* Fill in the version definition section. */
6107
6108 p = s->contents;
6109
6110 def.vd_version = VER_DEF_CURRENT;
6111 def.vd_flags = VER_FLG_BASE;
6112 def.vd_ndx = 1;
6113 def.vd_cnt = 1;
3e3b46e5
PB
6114 if (info->create_default_symver)
6115 {
6116 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6117 def.vd_next = sizeof (Elf_External_Verdef);
6118 }
6119 else
6120 {
6121 def.vd_aux = sizeof (Elf_External_Verdef);
6122 def.vd_next = (sizeof (Elf_External_Verdef)
6123 + sizeof (Elf_External_Verdaux));
6124 }
5a580b3a 6125
ef53be89 6126 if (soname_indx != (size_t) -1)
5a580b3a
AM
6127 {
6128 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6129 soname_indx);
6130 def.vd_hash = bfd_elf_hash (soname);
6131 defaux.vda_name = soname_indx;
3e3b46e5 6132 name = soname;
5a580b3a
AM
6133 }
6134 else
6135 {
ef53be89 6136 size_t indx;
5a580b3a 6137
06084812 6138 name = lbasename (output_bfd->filename);
5a580b3a
AM
6139 def.vd_hash = bfd_elf_hash (name);
6140 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6141 name, FALSE);
ef53be89 6142 if (indx == (size_t) -1)
5a580b3a
AM
6143 return FALSE;
6144 defaux.vda_name = indx;
6145 }
6146 defaux.vda_next = 0;
6147
6148 _bfd_elf_swap_verdef_out (output_bfd, &def,
6149 (Elf_External_Verdef *) p);
6150 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6151 if (info->create_default_symver)
6152 {
6153 /* Add a symbol representing this version. */
6154 bh = NULL;
6155 if (! (_bfd_generic_link_add_one_symbol
6156 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6157 0, NULL, FALSE,
6158 get_elf_backend_data (dynobj)->collect, &bh)))
6159 return FALSE;
6160 h = (struct elf_link_hash_entry *) bh;
6161 h->non_elf = 0;
6162 h->def_regular = 1;
6163 h->type = STT_OBJECT;
6164 h->verinfo.vertree = NULL;
6165
6166 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6167 return FALSE;
6168
6169 /* Create a duplicate of the base version with the same
6170 aux block, but different flags. */
6171 def.vd_flags = 0;
6172 def.vd_ndx = 2;
6173 def.vd_aux = sizeof (Elf_External_Verdef);
6174 if (verdefs)
6175 def.vd_next = (sizeof (Elf_External_Verdef)
6176 + sizeof (Elf_External_Verdaux));
6177 else
6178 def.vd_next = 0;
6179 _bfd_elf_swap_verdef_out (output_bfd, &def,
6180 (Elf_External_Verdef *) p);
6181 p += sizeof (Elf_External_Verdef);
6182 }
5a580b3a
AM
6183 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6184 (Elf_External_Verdaux *) p);
6185 p += sizeof (Elf_External_Verdaux);
6186
6187 for (t = verdefs; t != NULL; t = t->next)
6188 {
6189 unsigned int cdeps;
6190 struct bfd_elf_version_deps *n;
5a580b3a 6191
a6cc6b3b
RO
6192 /* Don't emit the base version twice. */
6193 if (t->vernum == 0)
6194 continue;
6195
5a580b3a
AM
6196 cdeps = 0;
6197 for (n = t->deps; n != NULL; n = n->next)
6198 ++cdeps;
6199
6200 /* Add a symbol representing this version. */
6201 bh = NULL;
6202 if (! (_bfd_generic_link_add_one_symbol
6203 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6204 0, NULL, FALSE,
6205 get_elf_backend_data (dynobj)->collect, &bh)))
6206 return FALSE;
6207 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6208 h->non_elf = 0;
6209 h->def_regular = 1;
5a580b3a
AM
6210 h->type = STT_OBJECT;
6211 h->verinfo.vertree = t;
6212
c152c796 6213 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6214 return FALSE;
6215
6216 def.vd_version = VER_DEF_CURRENT;
6217 def.vd_flags = 0;
6218 if (t->globals.list == NULL
6219 && t->locals.list == NULL
6220 && ! t->used)
6221 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6222 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6223 def.vd_cnt = cdeps + 1;
6224 def.vd_hash = bfd_elf_hash (t->name);
6225 def.vd_aux = sizeof (Elf_External_Verdef);
6226 def.vd_next = 0;
a6cc6b3b
RO
6227
6228 /* If a basever node is next, it *must* be the last node in
6229 the chain, otherwise Verdef construction breaks. */
6230 if (t->next != NULL && t->next->vernum == 0)
6231 BFD_ASSERT (t->next->next == NULL);
6232
6233 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6234 def.vd_next = (sizeof (Elf_External_Verdef)
6235 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6236
6237 _bfd_elf_swap_verdef_out (output_bfd, &def,
6238 (Elf_External_Verdef *) p);
6239 p += sizeof (Elf_External_Verdef);
6240
6241 defaux.vda_name = h->dynstr_index;
6242 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6243 h->dynstr_index);
6244 defaux.vda_next = 0;
6245 if (t->deps != NULL)
6246 defaux.vda_next = sizeof (Elf_External_Verdaux);
6247 t->name_indx = defaux.vda_name;
6248
6249 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6250 (Elf_External_Verdaux *) p);
6251 p += sizeof (Elf_External_Verdaux);
6252
6253 for (n = t->deps; n != NULL; n = n->next)
6254 {
6255 if (n->version_needed == NULL)
6256 {
6257 /* This can happen if there was an error in the
6258 version script. */
6259 defaux.vda_name = 0;
6260 }
6261 else
6262 {
6263 defaux.vda_name = n->version_needed->name_indx;
6264 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6265 defaux.vda_name);
6266 }
6267 if (n->next == NULL)
6268 defaux.vda_next = 0;
6269 else
6270 defaux.vda_next = sizeof (Elf_External_Verdaux);
6271
6272 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6273 (Elf_External_Verdaux *) p);
6274 p += sizeof (Elf_External_Verdaux);
6275 }
6276 }
6277
5a580b3a
AM
6278 elf_tdata (output_bfd)->cverdefs = cdefs;
6279 }
6280
5a580b3a
AM
6281 /* Work out the size of the version reference section. */
6282
3d4d4302 6283 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6284 BFD_ASSERT (s != NULL);
6285 {
6286 struct elf_find_verdep_info sinfo;
6287
5a580b3a
AM
6288 sinfo.info = info;
6289 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6290 if (sinfo.vers == 0)
6291 sinfo.vers = 1;
6292 sinfo.failed = FALSE;
6293
6294 elf_link_hash_traverse (elf_hash_table (info),
6295 _bfd_elf_link_find_version_dependencies,
6296 &sinfo);
14b1c01e
AM
6297 if (sinfo.failed)
6298 return FALSE;
5a580b3a
AM
6299
6300 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6301 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6302 else
6303 {
902e9fc7 6304 Elf_Internal_Verneed *vn;
5a580b3a
AM
6305 unsigned int size;
6306 unsigned int crefs;
6307 bfd_byte *p;
6308
a6cc6b3b 6309 /* Build the version dependency section. */
5a580b3a
AM
6310 size = 0;
6311 crefs = 0;
902e9fc7
MR
6312 for (vn = elf_tdata (output_bfd)->verref;
6313 vn != NULL;
6314 vn = vn->vn_nextref)
5a580b3a
AM
6315 {
6316 Elf_Internal_Vernaux *a;
6317
6318 size += sizeof (Elf_External_Verneed);
6319 ++crefs;
902e9fc7 6320 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6321 size += sizeof (Elf_External_Vernaux);
6322 }
6323
eea6121a 6324 s->size = size;
a50b1753 6325 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6326 if (s->contents == NULL)
6327 return FALSE;
6328
6329 p = s->contents;
902e9fc7
MR
6330 for (vn = elf_tdata (output_bfd)->verref;
6331 vn != NULL;
6332 vn = vn->vn_nextref)
5a580b3a
AM
6333 {
6334 unsigned int caux;
6335 Elf_Internal_Vernaux *a;
ef53be89 6336 size_t indx;
5a580b3a
AM
6337
6338 caux = 0;
902e9fc7 6339 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6340 ++caux;
6341
902e9fc7
MR
6342 vn->vn_version = VER_NEED_CURRENT;
6343 vn->vn_cnt = caux;
5a580b3a 6344 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
902e9fc7
MR
6345 elf_dt_name (vn->vn_bfd) != NULL
6346 ? elf_dt_name (vn->vn_bfd)
6347 : lbasename (vn->vn_bfd->filename),
5a580b3a 6348 FALSE);
ef53be89 6349 if (indx == (size_t) -1)
5a580b3a 6350 return FALSE;
902e9fc7
MR
6351 vn->vn_file = indx;
6352 vn->vn_aux = sizeof (Elf_External_Verneed);
6353 if (vn->vn_nextref == NULL)
6354 vn->vn_next = 0;
5a580b3a 6355 else
902e9fc7 6356 vn->vn_next = (sizeof (Elf_External_Verneed)
5a580b3a
AM
6357 + caux * sizeof (Elf_External_Vernaux));
6358
902e9fc7 6359 _bfd_elf_swap_verneed_out (output_bfd, vn,
5a580b3a
AM
6360 (Elf_External_Verneed *) p);
6361 p += sizeof (Elf_External_Verneed);
6362
902e9fc7 6363 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6364 {
6365 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6366 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6367 a->vna_nodename, FALSE);
ef53be89 6368 if (indx == (size_t) -1)
5a580b3a
AM
6369 return FALSE;
6370 a->vna_name = indx;
6371 if (a->vna_nextptr == NULL)
6372 a->vna_next = 0;
6373 else
6374 a->vna_next = sizeof (Elf_External_Vernaux);
6375
6376 _bfd_elf_swap_vernaux_out (output_bfd, a,
6377 (Elf_External_Vernaux *) p);
6378 p += sizeof (Elf_External_Vernaux);
6379 }
6380 }
6381
5a580b3a
AM
6382 elf_tdata (output_bfd)->cverrefs = crefs;
6383 }
6384 }
902e9fc7
MR
6385 }
6386
6387 bed = get_elf_backend_data (output_bfd);
6388
6389 if (info->gc_sections && bed->can_gc_sections)
6390 {
6391 struct elf_gc_sweep_symbol_info sweep_info;
6392 unsigned long section_sym_count;
6393
6394 /* Remove the symbols that were in the swept sections from the
6395 dynamic symbol table. GCFIXME: Anyone know how to get them
6396 out of the static symbol table as well? */
6397 sweep_info.info = info;
6398 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6399 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6400 &sweep_info);
6401
6402 _bfd_elf_link_renumber_dynsyms (output_bfd, info, &section_sym_count);
6403 }
6404
6405 /* Any syms created from now on start with -1 in
6406 got.refcount/offset and plt.refcount/offset. */
6407 elf_hash_table (info)->init_got_refcount
6408 = elf_hash_table (info)->init_got_offset;
6409 elf_hash_table (info)->init_plt_refcount
6410 = elf_hash_table (info)->init_plt_offset;
6411
6412 if (bfd_link_relocatable (info)
6413 && !_bfd_elf_size_group_sections (info))
6414 return FALSE;
6415
6416 /* The backend may have to create some sections regardless of whether
6417 we're dynamic or not. */
6418 if (bed->elf_backend_always_size_sections
6419 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6420 return FALSE;
6421
6422 /* Determine any GNU_STACK segment requirements, after the backend
6423 has had a chance to set a default segment size. */
6424 if (info->execstack)
6425 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6426 else if (info->noexecstack)
6427 elf_stack_flags (output_bfd) = PF_R | PF_W;
6428 else
6429 {
6430 bfd *inputobj;
6431 asection *notesec = NULL;
6432 int exec = 0;
6433
6434 for (inputobj = info->input_bfds;
6435 inputobj;
6436 inputobj = inputobj->link.next)
6437 {
6438 asection *s;
6439
6440 if (inputobj->flags
6441 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6442 continue;
6443 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6444 if (s)
6445 {
6446 if (s->flags & SEC_CODE)
6447 exec = PF_X;
6448 notesec = s;
6449 }
6450 else if (bed->default_execstack)
6451 exec = PF_X;
6452 }
6453 if (notesec || info->stacksize > 0)
6454 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6455 if (notesec && exec && bfd_link_relocatable (info)
6456 && notesec->output_section != bfd_abs_section_ptr)
6457 notesec->output_section->flags |= SEC_CODE;
6458 }
6459
6460 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6461 {
6462 struct elf_info_failed eif;
6463 struct elf_link_hash_entry *h;
6464 asection *dynstr;
6465 asection *s;
6466
6467 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6468 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6469
6470 if (soname != NULL)
6471 {
6472 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6473 soname, TRUE);
6474 if (soname_indx == (size_t) -1
6475 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6476 return FALSE;
6477 }
6478
6479 if (info->symbolic)
6480 {
6481 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6482 return FALSE;
6483 info->flags |= DF_SYMBOLIC;
6484 }
6485
6486 if (rpath != NULL)
6487 {
6488 size_t indx;
6489 bfd_vma tag;
6490
6491 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6492 TRUE);
6493 if (indx == (size_t) -1)
6494 return FALSE;
6495
6496 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6497 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6498 return FALSE;
6499 }
6500
6501 if (filter_shlib != NULL)
6502 {
6503 size_t indx;
6504
6505 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6506 filter_shlib, TRUE);
6507 if (indx == (size_t) -1
6508 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6509 return FALSE;
6510 }
6511
6512 if (auxiliary_filters != NULL)
6513 {
6514 const char * const *p;
6515
6516 for (p = auxiliary_filters; *p != NULL; p++)
6517 {
6518 size_t indx;
6519
6520 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6521 *p, TRUE);
6522 if (indx == (size_t) -1
6523 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6524 return FALSE;
6525 }
6526 }
6527
6528 if (audit != NULL)
6529 {
6530 size_t indx;
6531
6532 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6533 TRUE);
6534 if (indx == (size_t) -1
6535 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6536 return FALSE;
6537 }
6538
6539 if (depaudit != NULL)
6540 {
6541 size_t indx;
6542
6543 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6544 TRUE);
6545 if (indx == (size_t) -1
6546 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6547 return FALSE;
6548 }
6549
6550 eif.info = info;
6551 eif.failed = FALSE;
6552
6553 /* Find all symbols which were defined in a dynamic object and make
6554 the backend pick a reasonable value for them. */
6555 elf_link_hash_traverse (elf_hash_table (info),
6556 _bfd_elf_adjust_dynamic_symbol,
6557 &eif);
6558 if (eif.failed)
6559 return FALSE;
6560
6561 /* Add some entries to the .dynamic section. We fill in some of the
6562 values later, in bfd_elf_final_link, but we must add the entries
6563 now so that we know the final size of the .dynamic section. */
6564
6565 /* If there are initialization and/or finalization functions to
6566 call then add the corresponding DT_INIT/DT_FINI entries. */
6567 h = (info->init_function
6568 ? elf_link_hash_lookup (elf_hash_table (info),
6569 info->init_function, FALSE,
6570 FALSE, FALSE)
6571 : NULL);
6572 if (h != NULL
6573 && (h->ref_regular
6574 || h->def_regular))
6575 {
6576 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6577 return FALSE;
6578 }
6579 h = (info->fini_function
6580 ? elf_link_hash_lookup (elf_hash_table (info),
6581 info->fini_function, FALSE,
6582 FALSE, FALSE)
6583 : NULL);
6584 if (h != NULL
6585 && (h->ref_regular
6586 || h->def_regular))
6587 {
6588 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6589 return FALSE;
6590 }
6591
6592 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6593 if (s != NULL && s->linker_has_input)
6594 {
6595 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6596 if (! bfd_link_executable (info))
6597 {
6598 bfd *sub;
6599 asection *o;
6600
6601 for (sub = info->input_bfds; sub != NULL;
6602 sub = sub->link.next)
6603 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6604 for (o = sub->sections; o != NULL; o = o->next)
6605 if (elf_section_data (o)->this_hdr.sh_type
6606 == SHT_PREINIT_ARRAY)
6607 {
6608 _bfd_error_handler
6609 (_("%B: .preinit_array section is not allowed in DSO"),
6610 sub);
6611 break;
6612 }
6613
6614 bfd_set_error (bfd_error_nonrepresentable_section);
6615 return FALSE;
6616 }
6617
6618 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6619 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6620 return FALSE;
6621 }
6622 s = bfd_get_section_by_name (output_bfd, ".init_array");
6623 if (s != NULL && s->linker_has_input)
6624 {
6625 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6626 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6627 return FALSE;
6628 }
6629 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6630 if (s != NULL && s->linker_has_input)
6631 {
6632 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6633 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6634 return FALSE;
6635 }
6636
6637 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6638 /* If .dynstr is excluded from the link, we don't want any of
6639 these tags. Strictly, we should be checking each section
6640 individually; This quick check covers for the case where
6641 someone does a /DISCARD/ : { *(*) }. */
6642 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6643 {
6644 bfd_size_type strsize;
6645
6646 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6647 if ((info->emit_hash
6648 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6649 || (info->emit_gnu_hash
6650 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6651 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6652 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6653 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6654 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6655 bed->s->sizeof_sym))
6656 return FALSE;
6657 }
6658 }
6659
6660 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6661 return FALSE;
6662
6663 /* The backend must work out the sizes of all the other dynamic
6664 sections. */
6665 if (dynobj != NULL
6666 && bed->elf_backend_size_dynamic_sections != NULL
6667 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6668 return FALSE;
6669
6670 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6671 {
6672 unsigned long section_sym_count;
6673
6674 if (elf_tdata (output_bfd)->cverdefs)
6675 {
6676 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6677
6678 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6679 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6680 return FALSE;
6681 }
6682
6683 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6684 {
6685 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6686 return FALSE;
6687 }
6688 else if (info->flags & DF_BIND_NOW)
6689 {
6690 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6691 return FALSE;
6692 }
6693
6694 if (info->flags_1)
6695 {
6696 if (bfd_link_executable (info))
6697 info->flags_1 &= ~ (DF_1_INITFIRST
6698 | DF_1_NODELETE
6699 | DF_1_NOOPEN);
6700 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6701 return FALSE;
6702 }
6703
6704 if (elf_tdata (output_bfd)->cverrefs)
6705 {
6706 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6707
6708 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6709 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6710 return FALSE;
6711 }
5a580b3a 6712
8423293d
AM
6713 if ((elf_tdata (output_bfd)->cverrefs == 0
6714 && elf_tdata (output_bfd)->cverdefs == 0)
6715 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6716 &section_sym_count) == 0)
6717 {
902e9fc7
MR
6718 asection *s;
6719
3d4d4302 6720 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6721 s->flags |= SEC_EXCLUDE;
6722 }
6723 }
6724 return TRUE;
6725}
6726
74541ad4
AM
6727/* Find the first non-excluded output section. We'll use its
6728 section symbol for some emitted relocs. */
6729void
6730_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6731{
6732 asection *s;
6733
6734 for (s = output_bfd->sections; s != NULL; s = s->next)
6735 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6736 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6737 {
6738 elf_hash_table (info)->text_index_section = s;
6739 break;
6740 }
6741}
6742
6743/* Find two non-excluded output sections, one for code, one for data.
6744 We'll use their section symbols for some emitted relocs. */
6745void
6746_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6747{
6748 asection *s;
6749
266b05cf
DJ
6750 /* Data first, since setting text_index_section changes
6751 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6752 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6753 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6754 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6755 {
266b05cf 6756 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6757 break;
6758 }
6759
6760 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6761 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6762 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6763 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6764 {
266b05cf 6765 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6766 break;
6767 }
6768
6769 if (elf_hash_table (info)->text_index_section == NULL)
6770 elf_hash_table (info)->text_index_section
6771 = elf_hash_table (info)->data_index_section;
6772}
6773
8423293d
AM
6774bfd_boolean
6775bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6776{
74541ad4
AM
6777 const struct elf_backend_data *bed;
6778
8423293d
AM
6779 if (!is_elf_hash_table (info->hash))
6780 return TRUE;
6781
74541ad4
AM
6782 bed = get_elf_backend_data (output_bfd);
6783 (*bed->elf_backend_init_index_section) (output_bfd, info);
6784
8423293d
AM
6785 if (elf_hash_table (info)->dynamic_sections_created)
6786 {
6787 bfd *dynobj;
8423293d
AM
6788 asection *s;
6789 bfd_size_type dynsymcount;
6790 unsigned long section_sym_count;
8423293d
AM
6791 unsigned int dtagcount;
6792
6793 dynobj = elf_hash_table (info)->dynobj;
6794
5a580b3a
AM
6795 /* Assign dynsym indicies. In a shared library we generate a
6796 section symbol for each output section, which come first.
6797 Next come all of the back-end allocated local dynamic syms,
6798 followed by the rest of the global symbols. */
6799
554220db
AM
6800 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6801 &section_sym_count);
5a580b3a
AM
6802
6803 /* Work out the size of the symbol version section. */
3d4d4302 6804 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6805 BFD_ASSERT (s != NULL);
d5486c43 6806 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6807 {
eea6121a 6808 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6809 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6810 if (s->contents == NULL)
6811 return FALSE;
6812
6813 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6814 return FALSE;
6815 }
6816
6817 /* Set the size of the .dynsym and .hash sections. We counted
6818 the number of dynamic symbols in elf_link_add_object_symbols.
6819 We will build the contents of .dynsym and .hash when we build
6820 the final symbol table, because until then we do not know the
6821 correct value to give the symbols. We built the .dynstr
6822 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6823 s = elf_hash_table (info)->dynsym;
5a580b3a 6824 BFD_ASSERT (s != NULL);
eea6121a 6825 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6826
d5486c43
L
6827 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6828 if (s->contents == NULL)
6829 return FALSE;
5a580b3a 6830
d5486c43
L
6831 /* The first entry in .dynsym is a dummy symbol. Clear all the
6832 section syms, in case we don't output them all. */
6833 ++section_sym_count;
6834 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6835
fdc90cb4
JJ
6836 elf_hash_table (info)->bucketcount = 0;
6837
5a580b3a
AM
6838 /* Compute the size of the hashing table. As a side effect this
6839 computes the hash values for all the names we export. */
fdc90cb4
JJ
6840 if (info->emit_hash)
6841 {
6842 unsigned long int *hashcodes;
14b1c01e 6843 struct hash_codes_info hashinf;
fdc90cb4
JJ
6844 bfd_size_type amt;
6845 unsigned long int nsyms;
6846 size_t bucketcount;
6847 size_t hash_entry_size;
6848
6849 /* Compute the hash values for all exported symbols. At the same
6850 time store the values in an array so that we could use them for
6851 optimizations. */
6852 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6853 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6854 if (hashcodes == NULL)
6855 return FALSE;
14b1c01e
AM
6856 hashinf.hashcodes = hashcodes;
6857 hashinf.error = FALSE;
5a580b3a 6858
fdc90cb4
JJ
6859 /* Put all hash values in HASHCODES. */
6860 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6861 elf_collect_hash_codes, &hashinf);
6862 if (hashinf.error)
4dd07732
AM
6863 {
6864 free (hashcodes);
6865 return FALSE;
6866 }
5a580b3a 6867
14b1c01e 6868 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6869 bucketcount
6870 = compute_bucket_count (info, hashcodes, nsyms, 0);
6871 free (hashcodes);
6872
6873 if (bucketcount == 0)
6874 return FALSE;
5a580b3a 6875
fdc90cb4
JJ
6876 elf_hash_table (info)->bucketcount = bucketcount;
6877
3d4d4302 6878 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6879 BFD_ASSERT (s != NULL);
6880 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6881 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6882 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6883 if (s->contents == NULL)
6884 return FALSE;
6885
6886 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6887 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6888 s->contents + hash_entry_size);
6889 }
6890
6891 if (info->emit_gnu_hash)
6892 {
6893 size_t i, cnt;
6894 unsigned char *contents;
6895 struct collect_gnu_hash_codes cinfo;
6896 bfd_size_type amt;
6897 size_t bucketcount;
6898
6899 memset (&cinfo, 0, sizeof (cinfo));
6900
6901 /* Compute the hash values for all exported symbols. At the same
6902 time store the values in an array so that we could use them for
6903 optimizations. */
6904 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6905 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6906 if (cinfo.hashcodes == NULL)
6907 return FALSE;
6908
6909 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6910 cinfo.min_dynindx = -1;
6911 cinfo.output_bfd = output_bfd;
6912 cinfo.bed = bed;
6913
6914 /* Put all hash values in HASHCODES. */
6915 elf_link_hash_traverse (elf_hash_table (info),
6916 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6917 if (cinfo.error)
4dd07732
AM
6918 {
6919 free (cinfo.hashcodes);
6920 return FALSE;
6921 }
fdc90cb4
JJ
6922
6923 bucketcount
6924 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6925
6926 if (bucketcount == 0)
6927 {
6928 free (cinfo.hashcodes);
6929 return FALSE;
6930 }
6931
3d4d4302 6932 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6933 BFD_ASSERT (s != NULL);
6934
6935 if (cinfo.nsyms == 0)
6936 {
6937 /* Empty .gnu.hash section is special. */
6938 BFD_ASSERT (cinfo.min_dynindx == -1);
6939 free (cinfo.hashcodes);
6940 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6941 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6942 if (contents == NULL)
6943 return FALSE;
6944 s->contents = contents;
6945 /* 1 empty bucket. */
6946 bfd_put_32 (output_bfd, 1, contents);
6947 /* SYMIDX above the special symbol 0. */
6948 bfd_put_32 (output_bfd, 1, contents + 4);
6949 /* Just one word for bitmask. */
6950 bfd_put_32 (output_bfd, 1, contents + 8);
6951 /* Only hash fn bloom filter. */
6952 bfd_put_32 (output_bfd, 0, contents + 12);
6953 /* No hashes are valid - empty bitmask. */
6954 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6955 /* No hashes in the only bucket. */
6956 bfd_put_32 (output_bfd, 0,
6957 contents + 16 + bed->s->arch_size / 8);
6958 }
6959 else
6960 {
9e6619e2 6961 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6962 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6963
9e6619e2
AM
6964 x = cinfo.nsyms;
6965 maskbitslog2 = 1;
6966 while ((x >>= 1) != 0)
6967 ++maskbitslog2;
fdc90cb4
JJ
6968 if (maskbitslog2 < 3)
6969 maskbitslog2 = 5;
6970 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6971 maskbitslog2 = maskbitslog2 + 3;
6972 else
6973 maskbitslog2 = maskbitslog2 + 2;
6974 if (bed->s->arch_size == 64)
6975 {
6976 if (maskbitslog2 == 5)
6977 maskbitslog2 = 6;
6978 cinfo.shift1 = 6;
6979 }
6980 else
6981 cinfo.shift1 = 5;
6982 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6983 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6984 cinfo.maskbits = 1 << maskbitslog2;
6985 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6986 amt = bucketcount * sizeof (unsigned long int) * 2;
6987 amt += maskwords * sizeof (bfd_vma);
a50b1753 6988 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6989 if (cinfo.bitmask == NULL)
6990 {
6991 free (cinfo.hashcodes);
6992 return FALSE;
6993 }
6994
a50b1753 6995 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6996 cinfo.indx = cinfo.counts + bucketcount;
6997 cinfo.symindx = dynsymcount - cinfo.nsyms;
6998 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6999
7000 /* Determine how often each hash bucket is used. */
7001 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7002 for (i = 0; i < cinfo.nsyms; ++i)
7003 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7004
7005 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7006 if (cinfo.counts[i] != 0)
7007 {
7008 cinfo.indx[i] = cnt;
7009 cnt += cinfo.counts[i];
7010 }
7011 BFD_ASSERT (cnt == dynsymcount);
7012 cinfo.bucketcount = bucketcount;
7013 cinfo.local_indx = cinfo.min_dynindx;
7014
7015 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7016 s->size += cinfo.maskbits / 8;
a50b1753 7017 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7018 if (contents == NULL)
7019 {
7020 free (cinfo.bitmask);
7021 free (cinfo.hashcodes);
7022 return FALSE;
7023 }
7024
7025 s->contents = contents;
7026 bfd_put_32 (output_bfd, bucketcount, contents);
7027 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7028 bfd_put_32 (output_bfd, maskwords, contents + 8);
7029 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7030 contents += 16 + cinfo.maskbits / 8;
7031
7032 for (i = 0; i < bucketcount; ++i)
7033 {
7034 if (cinfo.counts[i] == 0)
7035 bfd_put_32 (output_bfd, 0, contents);
7036 else
7037 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7038 contents += 4;
7039 }
7040
7041 cinfo.contents = contents;
7042
7043 /* Renumber dynamic symbols, populate .gnu.hash section. */
7044 elf_link_hash_traverse (elf_hash_table (info),
7045 elf_renumber_gnu_hash_syms, &cinfo);
7046
7047 contents = s->contents + 16;
7048 for (i = 0; i < maskwords; ++i)
7049 {
7050 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7051 contents);
7052 contents += bed->s->arch_size / 8;
7053 }
7054
7055 free (cinfo.bitmask);
7056 free (cinfo.hashcodes);
7057 }
7058 }
5a580b3a 7059
3d4d4302 7060 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7061 BFD_ASSERT (s != NULL);
7062
4ad4eba5 7063 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7064
eea6121a 7065 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7066
7067 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7068 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7069 return FALSE;
7070 }
7071
7072 return TRUE;
7073}
4d269e42 7074\f
4d269e42
AM
7075/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7076
7077static void
7078merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7079 asection *sec)
7080{
dbaa2011
AM
7081 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7082 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7083}
7084
7085/* Finish SHF_MERGE section merging. */
7086
7087bfd_boolean
630993ec 7088_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7089{
7090 bfd *ibfd;
7091 asection *sec;
7092
7093 if (!is_elf_hash_table (info->hash))
7094 return FALSE;
7095
c72f2fb2 7096 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7097 if ((ibfd->flags & DYNAMIC) == 0
7098 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7099 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7100 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7101 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7102 if ((sec->flags & SEC_MERGE) != 0
7103 && !bfd_is_abs_section (sec->output_section))
7104 {
7105 struct bfd_elf_section_data *secdata;
7106
7107 secdata = elf_section_data (sec);
630993ec 7108 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7109 &elf_hash_table (info)->merge_info,
7110 sec, &secdata->sec_info))
7111 return FALSE;
7112 else if (secdata->sec_info)
dbaa2011 7113 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7114 }
7115
7116 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7117 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7118 merge_sections_remove_hook);
7119 return TRUE;
7120}
7121
7122/* Create an entry in an ELF linker hash table. */
7123
7124struct bfd_hash_entry *
7125_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7126 struct bfd_hash_table *table,
7127 const char *string)
7128{
7129 /* Allocate the structure if it has not already been allocated by a
7130 subclass. */
7131 if (entry == NULL)
7132 {
a50b1753 7133 entry = (struct bfd_hash_entry *)
ca4be51c 7134 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7135 if (entry == NULL)
7136 return entry;
7137 }
7138
7139 /* Call the allocation method of the superclass. */
7140 entry = _bfd_link_hash_newfunc (entry, table, string);
7141 if (entry != NULL)
7142 {
7143 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7144 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7145
7146 /* Set local fields. */
7147 ret->indx = -1;
7148 ret->dynindx = -1;
7149 ret->got = htab->init_got_refcount;
7150 ret->plt = htab->init_plt_refcount;
7151 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7152 - offsetof (struct elf_link_hash_entry, size)));
7153 /* Assume that we have been called by a non-ELF symbol reader.
7154 This flag is then reset by the code which reads an ELF input
7155 file. This ensures that a symbol created by a non-ELF symbol
7156 reader will have the flag set correctly. */
7157 ret->non_elf = 1;
7158 }
7159
7160 return entry;
7161}
7162
7163/* Copy data from an indirect symbol to its direct symbol, hiding the
7164 old indirect symbol. Also used for copying flags to a weakdef. */
7165
7166void
7167_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7168 struct elf_link_hash_entry *dir,
7169 struct elf_link_hash_entry *ind)
7170{
7171 struct elf_link_hash_table *htab;
7172
7173 /* Copy down any references that we may have already seen to the
e81830c5 7174 symbol which just became indirect. */
4d269e42 7175
422f1182 7176 if (dir->versioned != versioned_hidden)
e81830c5
AM
7177 dir->ref_dynamic |= ind->ref_dynamic;
7178 dir->ref_regular |= ind->ref_regular;
7179 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7180 dir->non_got_ref |= ind->non_got_ref;
7181 dir->needs_plt |= ind->needs_plt;
7182 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7183
7184 if (ind->root.type != bfd_link_hash_indirect)
7185 return;
7186
7187 /* Copy over the global and procedure linkage table refcount entries.
7188 These may have been already set up by a check_relocs routine. */
7189 htab = elf_hash_table (info);
7190 if (ind->got.refcount > htab->init_got_refcount.refcount)
7191 {
7192 if (dir->got.refcount < 0)
7193 dir->got.refcount = 0;
7194 dir->got.refcount += ind->got.refcount;
7195 ind->got.refcount = htab->init_got_refcount.refcount;
7196 }
7197
7198 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7199 {
7200 if (dir->plt.refcount < 0)
7201 dir->plt.refcount = 0;
7202 dir->plt.refcount += ind->plt.refcount;
7203 ind->plt.refcount = htab->init_plt_refcount.refcount;
7204 }
7205
7206 if (ind->dynindx != -1)
7207 {
7208 if (dir->dynindx != -1)
7209 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7210 dir->dynindx = ind->dynindx;
7211 dir->dynstr_index = ind->dynstr_index;
7212 ind->dynindx = -1;
7213 ind->dynstr_index = 0;
7214 }
7215}
7216
7217void
7218_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7219 struct elf_link_hash_entry *h,
7220 bfd_boolean force_local)
7221{
3aa14d16
L
7222 /* STT_GNU_IFUNC symbol must go through PLT. */
7223 if (h->type != STT_GNU_IFUNC)
7224 {
7225 h->plt = elf_hash_table (info)->init_plt_offset;
7226 h->needs_plt = 0;
7227 }
4d269e42
AM
7228 if (force_local)
7229 {
7230 h->forced_local = 1;
7231 if (h->dynindx != -1)
7232 {
7233 h->dynindx = -1;
7234 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7235 h->dynstr_index);
7236 }
7237 }
7238}
7239
7bf52ea2
AM
7240/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7241 caller. */
4d269e42
AM
7242
7243bfd_boolean
7244_bfd_elf_link_hash_table_init
7245 (struct elf_link_hash_table *table,
7246 bfd *abfd,
7247 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7248 struct bfd_hash_table *,
7249 const char *),
4dfe6ac6
NC
7250 unsigned int entsize,
7251 enum elf_target_id target_id)
4d269e42
AM
7252{
7253 bfd_boolean ret;
7254 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7255
4d269e42
AM
7256 table->init_got_refcount.refcount = can_refcount - 1;
7257 table->init_plt_refcount.refcount = can_refcount - 1;
7258 table->init_got_offset.offset = -(bfd_vma) 1;
7259 table->init_plt_offset.offset = -(bfd_vma) 1;
7260 /* The first dynamic symbol is a dummy. */
7261 table->dynsymcount = 1;
7262
7263 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7264
4d269e42 7265 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7266 table->hash_table_id = target_id;
4d269e42
AM
7267
7268 return ret;
7269}
7270
7271/* Create an ELF linker hash table. */
7272
7273struct bfd_link_hash_table *
7274_bfd_elf_link_hash_table_create (bfd *abfd)
7275{
7276 struct elf_link_hash_table *ret;
7277 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7278
7bf52ea2 7279 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7280 if (ret == NULL)
7281 return NULL;
7282
7283 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7284 sizeof (struct elf_link_hash_entry),
7285 GENERIC_ELF_DATA))
4d269e42
AM
7286 {
7287 free (ret);
7288 return NULL;
7289 }
d495ab0d 7290 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7291
7292 return &ret->root;
7293}
7294
9f7c3e5e
AM
7295/* Destroy an ELF linker hash table. */
7296
7297void
d495ab0d 7298_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7299{
d495ab0d
AM
7300 struct elf_link_hash_table *htab;
7301
7302 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7303 if (htab->dynstr != NULL)
7304 _bfd_elf_strtab_free (htab->dynstr);
7305 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7306 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7307}
7308
4d269e42
AM
7309/* This is a hook for the ELF emulation code in the generic linker to
7310 tell the backend linker what file name to use for the DT_NEEDED
7311 entry for a dynamic object. */
7312
7313void
7314bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7315{
7316 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7317 && bfd_get_format (abfd) == bfd_object)
7318 elf_dt_name (abfd) = name;
7319}
7320
7321int
7322bfd_elf_get_dyn_lib_class (bfd *abfd)
7323{
7324 int lib_class;
7325 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7326 && bfd_get_format (abfd) == bfd_object)
7327 lib_class = elf_dyn_lib_class (abfd);
7328 else
7329 lib_class = 0;
7330 return lib_class;
7331}
7332
7333void
7334bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7335{
7336 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7337 && bfd_get_format (abfd) == bfd_object)
7338 elf_dyn_lib_class (abfd) = lib_class;
7339}
7340
7341/* Get the list of DT_NEEDED entries for a link. This is a hook for
7342 the linker ELF emulation code. */
7343
7344struct bfd_link_needed_list *
7345bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7346 struct bfd_link_info *info)
7347{
7348 if (! is_elf_hash_table (info->hash))
7349 return NULL;
7350 return elf_hash_table (info)->needed;
7351}
7352
7353/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7354 hook for the linker ELF emulation code. */
7355
7356struct bfd_link_needed_list *
7357bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7358 struct bfd_link_info *info)
7359{
7360 if (! is_elf_hash_table (info->hash))
7361 return NULL;
7362 return elf_hash_table (info)->runpath;
7363}
7364
7365/* Get the name actually used for a dynamic object for a link. This
7366 is the SONAME entry if there is one. Otherwise, it is the string
7367 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7368
7369const char *
7370bfd_elf_get_dt_soname (bfd *abfd)
7371{
7372 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7373 && bfd_get_format (abfd) == bfd_object)
7374 return elf_dt_name (abfd);
7375 return NULL;
7376}
7377
7378/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7379 the ELF linker emulation code. */
7380
7381bfd_boolean
7382bfd_elf_get_bfd_needed_list (bfd *abfd,
7383 struct bfd_link_needed_list **pneeded)
7384{
7385 asection *s;
7386 bfd_byte *dynbuf = NULL;
cb33740c 7387 unsigned int elfsec;
4d269e42
AM
7388 unsigned long shlink;
7389 bfd_byte *extdyn, *extdynend;
7390 size_t extdynsize;
7391 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7392
7393 *pneeded = NULL;
7394
7395 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7396 || bfd_get_format (abfd) != bfd_object)
7397 return TRUE;
7398
7399 s = bfd_get_section_by_name (abfd, ".dynamic");
7400 if (s == NULL || s->size == 0)
7401 return TRUE;
7402
7403 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7404 goto error_return;
7405
7406 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7407 if (elfsec == SHN_BAD)
4d269e42
AM
7408 goto error_return;
7409
7410 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7411
4d269e42
AM
7412 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7413 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7414
7415 extdyn = dynbuf;
7416 extdynend = extdyn + s->size;
7417 for (; extdyn < extdynend; extdyn += extdynsize)
7418 {
7419 Elf_Internal_Dyn dyn;
7420
7421 (*swap_dyn_in) (abfd, extdyn, &dyn);
7422
7423 if (dyn.d_tag == DT_NULL)
7424 break;
7425
7426 if (dyn.d_tag == DT_NEEDED)
7427 {
7428 const char *string;
7429 struct bfd_link_needed_list *l;
7430 unsigned int tagv = dyn.d_un.d_val;
7431 bfd_size_type amt;
7432
7433 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7434 if (string == NULL)
7435 goto error_return;
7436
7437 amt = sizeof *l;
a50b1753 7438 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7439 if (l == NULL)
7440 goto error_return;
7441
7442 l->by = abfd;
7443 l->name = string;
7444 l->next = *pneeded;
7445 *pneeded = l;
7446 }
7447 }
7448
7449 free (dynbuf);
7450
7451 return TRUE;
7452
7453 error_return:
7454 if (dynbuf != NULL)
7455 free (dynbuf);
7456 return FALSE;
7457}
7458
7459struct elf_symbuf_symbol
7460{
7461 unsigned long st_name; /* Symbol name, index in string tbl */
7462 unsigned char st_info; /* Type and binding attributes */
7463 unsigned char st_other; /* Visibilty, and target specific */
7464};
7465
7466struct elf_symbuf_head
7467{
7468 struct elf_symbuf_symbol *ssym;
ef53be89 7469 size_t count;
4d269e42
AM
7470 unsigned int st_shndx;
7471};
7472
7473struct elf_symbol
7474{
7475 union
7476 {
7477 Elf_Internal_Sym *isym;
7478 struct elf_symbuf_symbol *ssym;
7479 } u;
7480 const char *name;
7481};
7482
7483/* Sort references to symbols by ascending section number. */
7484
7485static int
7486elf_sort_elf_symbol (const void *arg1, const void *arg2)
7487{
7488 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7489 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7490
7491 return s1->st_shndx - s2->st_shndx;
7492}
7493
7494static int
7495elf_sym_name_compare (const void *arg1, const void *arg2)
7496{
7497 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7498 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7499 return strcmp (s1->name, s2->name);
7500}
7501
7502static struct elf_symbuf_head *
ef53be89 7503elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7504{
14b1c01e 7505 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7506 struct elf_symbuf_symbol *ssym;
7507 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7508 size_t i, shndx_count, total_size;
4d269e42 7509
a50b1753 7510 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7511 if (indbuf == NULL)
7512 return NULL;
7513
7514 for (ind = indbuf, i = 0; i < symcount; i++)
7515 if (isymbuf[i].st_shndx != SHN_UNDEF)
7516 *ind++ = &isymbuf[i];
7517 indbufend = ind;
7518
7519 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7520 elf_sort_elf_symbol);
7521
7522 shndx_count = 0;
7523 if (indbufend > indbuf)
7524 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7525 if (ind[0]->st_shndx != ind[1]->st_shndx)
7526 shndx_count++;
7527
3ae181ee
L
7528 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7529 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7530 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7531 if (ssymbuf == NULL)
7532 {
7533 free (indbuf);
7534 return NULL;
7535 }
7536
3ae181ee 7537 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7538 ssymbuf->ssym = NULL;
7539 ssymbuf->count = shndx_count;
7540 ssymbuf->st_shndx = 0;
7541 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7542 {
7543 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7544 {
7545 ssymhead++;
7546 ssymhead->ssym = ssym;
7547 ssymhead->count = 0;
7548 ssymhead->st_shndx = (*ind)->st_shndx;
7549 }
7550 ssym->st_name = (*ind)->st_name;
7551 ssym->st_info = (*ind)->st_info;
7552 ssym->st_other = (*ind)->st_other;
7553 ssymhead->count++;
7554 }
ef53be89 7555 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7556 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7557 == total_size));
4d269e42
AM
7558
7559 free (indbuf);
7560 return ssymbuf;
7561}
7562
7563/* Check if 2 sections define the same set of local and global
7564 symbols. */
7565
8f317e31 7566static bfd_boolean
4d269e42
AM
7567bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7568 struct bfd_link_info *info)
7569{
7570 bfd *bfd1, *bfd2;
7571 const struct elf_backend_data *bed1, *bed2;
7572 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7573 size_t symcount1, symcount2;
4d269e42
AM
7574 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7575 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7576 Elf_Internal_Sym *isym, *isymend;
7577 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7578 size_t count1, count2, i;
cb33740c 7579 unsigned int shndx1, shndx2;
4d269e42
AM
7580 bfd_boolean result;
7581
7582 bfd1 = sec1->owner;
7583 bfd2 = sec2->owner;
7584
4d269e42
AM
7585 /* Both sections have to be in ELF. */
7586 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7587 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7588 return FALSE;
7589
7590 if (elf_section_type (sec1) != elf_section_type (sec2))
7591 return FALSE;
7592
4d269e42
AM
7593 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7594 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7595 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7596 return FALSE;
7597
7598 bed1 = get_elf_backend_data (bfd1);
7599 bed2 = get_elf_backend_data (bfd2);
7600 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7601 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7602 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7603 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7604
7605 if (symcount1 == 0 || symcount2 == 0)
7606 return FALSE;
7607
7608 result = FALSE;
7609 isymbuf1 = NULL;
7610 isymbuf2 = NULL;
a50b1753
NC
7611 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7612 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7613
7614 if (ssymbuf1 == NULL)
7615 {
7616 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7617 NULL, NULL, NULL);
7618 if (isymbuf1 == NULL)
7619 goto done;
7620
7621 if (!info->reduce_memory_overheads)
7622 elf_tdata (bfd1)->symbuf = ssymbuf1
7623 = elf_create_symbuf (symcount1, isymbuf1);
7624 }
7625
7626 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7627 {
7628 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7629 NULL, NULL, NULL);
7630 if (isymbuf2 == NULL)
7631 goto done;
7632
7633 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7634 elf_tdata (bfd2)->symbuf = ssymbuf2
7635 = elf_create_symbuf (symcount2, isymbuf2);
7636 }
7637
7638 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7639 {
7640 /* Optimized faster version. */
ef53be89 7641 size_t lo, hi, mid;
4d269e42
AM
7642 struct elf_symbol *symp;
7643 struct elf_symbuf_symbol *ssym, *ssymend;
7644
7645 lo = 0;
7646 hi = ssymbuf1->count;
7647 ssymbuf1++;
7648 count1 = 0;
7649 while (lo < hi)
7650 {
7651 mid = (lo + hi) / 2;
cb33740c 7652 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7653 hi = mid;
cb33740c 7654 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7655 lo = mid + 1;
7656 else
7657 {
7658 count1 = ssymbuf1[mid].count;
7659 ssymbuf1 += mid;
7660 break;
7661 }
7662 }
7663
7664 lo = 0;
7665 hi = ssymbuf2->count;
7666 ssymbuf2++;
7667 count2 = 0;
7668 while (lo < hi)
7669 {
7670 mid = (lo + hi) / 2;
cb33740c 7671 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7672 hi = mid;
cb33740c 7673 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7674 lo = mid + 1;
7675 else
7676 {
7677 count2 = ssymbuf2[mid].count;
7678 ssymbuf2 += mid;
7679 break;
7680 }
7681 }
7682
7683 if (count1 == 0 || count2 == 0 || count1 != count2)
7684 goto done;
7685
ca4be51c
AM
7686 symtable1
7687 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7688 symtable2
7689 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7690 if (symtable1 == NULL || symtable2 == NULL)
7691 goto done;
7692
7693 symp = symtable1;
7694 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7695 ssym < ssymend; ssym++, symp++)
7696 {
7697 symp->u.ssym = ssym;
7698 symp->name = bfd_elf_string_from_elf_section (bfd1,
7699 hdr1->sh_link,
7700 ssym->st_name);
7701 }
7702
7703 symp = symtable2;
7704 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7705 ssym < ssymend; ssym++, symp++)
7706 {
7707 symp->u.ssym = ssym;
7708 symp->name = bfd_elf_string_from_elf_section (bfd2,
7709 hdr2->sh_link,
7710 ssym->st_name);
7711 }
7712
7713 /* Sort symbol by name. */
7714 qsort (symtable1, count1, sizeof (struct elf_symbol),
7715 elf_sym_name_compare);
7716 qsort (symtable2, count1, sizeof (struct elf_symbol),
7717 elf_sym_name_compare);
7718
7719 for (i = 0; i < count1; i++)
7720 /* Two symbols must have the same binding, type and name. */
7721 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7722 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7723 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7724 goto done;
7725
7726 result = TRUE;
7727 goto done;
7728 }
7729
a50b1753
NC
7730 symtable1 = (struct elf_symbol *)
7731 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7732 symtable2 = (struct elf_symbol *)
7733 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7734 if (symtable1 == NULL || symtable2 == NULL)
7735 goto done;
7736
7737 /* Count definitions in the section. */
7738 count1 = 0;
7739 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7740 if (isym->st_shndx == shndx1)
4d269e42
AM
7741 symtable1[count1++].u.isym = isym;
7742
7743 count2 = 0;
7744 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7745 if (isym->st_shndx == shndx2)
4d269e42
AM
7746 symtable2[count2++].u.isym = isym;
7747
7748 if (count1 == 0 || count2 == 0 || count1 != count2)
7749 goto done;
7750
7751 for (i = 0; i < count1; i++)
7752 symtable1[i].name
7753 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7754 symtable1[i].u.isym->st_name);
7755
7756 for (i = 0; i < count2; i++)
7757 symtable2[i].name
7758 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7759 symtable2[i].u.isym->st_name);
7760
7761 /* Sort symbol by name. */
7762 qsort (symtable1, count1, sizeof (struct elf_symbol),
7763 elf_sym_name_compare);
7764 qsort (symtable2, count1, sizeof (struct elf_symbol),
7765 elf_sym_name_compare);
7766
7767 for (i = 0; i < count1; i++)
7768 /* Two symbols must have the same binding, type and name. */
7769 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7770 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7771 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7772 goto done;
7773
7774 result = TRUE;
7775
7776done:
7777 if (symtable1)
7778 free (symtable1);
7779 if (symtable2)
7780 free (symtable2);
7781 if (isymbuf1)
7782 free (isymbuf1);
7783 if (isymbuf2)
7784 free (isymbuf2);
7785
7786 return result;
7787}
7788
7789/* Return TRUE if 2 section types are compatible. */
7790
7791bfd_boolean
7792_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7793 bfd *bbfd, const asection *bsec)
7794{
7795 if (asec == NULL
7796 || bsec == NULL
7797 || abfd->xvec->flavour != bfd_target_elf_flavour
7798 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7799 return TRUE;
7800
7801 return elf_section_type (asec) == elf_section_type (bsec);
7802}
7803\f
c152c796
AM
7804/* Final phase of ELF linker. */
7805
7806/* A structure we use to avoid passing large numbers of arguments. */
7807
7808struct elf_final_link_info
7809{
7810 /* General link information. */
7811 struct bfd_link_info *info;
7812 /* Output BFD. */
7813 bfd *output_bfd;
7814 /* Symbol string table. */
ef10c3ac 7815 struct elf_strtab_hash *symstrtab;
c152c796
AM
7816 /* .hash section. */
7817 asection *hash_sec;
7818 /* symbol version section (.gnu.version). */
7819 asection *symver_sec;
7820 /* Buffer large enough to hold contents of any section. */
7821 bfd_byte *contents;
7822 /* Buffer large enough to hold external relocs of any section. */
7823 void *external_relocs;
7824 /* Buffer large enough to hold internal relocs of any section. */
7825 Elf_Internal_Rela *internal_relocs;
7826 /* Buffer large enough to hold external local symbols of any input
7827 BFD. */
7828 bfd_byte *external_syms;
7829 /* And a buffer for symbol section indices. */
7830 Elf_External_Sym_Shndx *locsym_shndx;
7831 /* Buffer large enough to hold internal local symbols of any input
7832 BFD. */
7833 Elf_Internal_Sym *internal_syms;
7834 /* Array large enough to hold a symbol index for each local symbol
7835 of any input BFD. */
7836 long *indices;
7837 /* Array large enough to hold a section pointer for each local
7838 symbol of any input BFD. */
7839 asection **sections;
ef10c3ac 7840 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7841 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7842 /* Number of STT_FILE syms seen. */
7843 size_t filesym_count;
c152c796
AM
7844};
7845
7846/* This struct is used to pass information to elf_link_output_extsym. */
7847
7848struct elf_outext_info
7849{
7850 bfd_boolean failed;
7851 bfd_boolean localsyms;
34a79995 7852 bfd_boolean file_sym_done;
8b127cbc 7853 struct elf_final_link_info *flinfo;
c152c796
AM
7854};
7855
d9352518
DB
7856
7857/* Support for evaluating a complex relocation.
7858
7859 Complex relocations are generalized, self-describing relocations. The
7860 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7861 relocations themselves.
d9352518
DB
7862
7863 The relocations are use a reserved elf-wide relocation type code (R_RELC
7864 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7865 information (start bit, end bit, word width, etc) into the addend. This
7866 information is extracted from CGEN-generated operand tables within gas.
7867
7868 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7869 internal) representing prefix-notation expressions, including but not
7870 limited to those sorts of expressions normally encoded as addends in the
7871 addend field. The symbol mangling format is:
7872
7873 <node> := <literal>
7874 | <unary-operator> ':' <node>
7875 | <binary-operator> ':' <node> ':' <node>
7876 ;
7877
7878 <literal> := 's' <digits=N> ':' <N character symbol name>
7879 | 'S' <digits=N> ':' <N character section name>
7880 | '#' <hexdigits>
7881 ;
7882
7883 <binary-operator> := as in C
7884 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7885
7886static void
a0c8462f
AM
7887set_symbol_value (bfd *bfd_with_globals,
7888 Elf_Internal_Sym *isymbuf,
7889 size_t locsymcount,
7890 size_t symidx,
7891 bfd_vma val)
d9352518 7892{
8977835c
AM
7893 struct elf_link_hash_entry **sym_hashes;
7894 struct elf_link_hash_entry *h;
7895 size_t extsymoff = locsymcount;
d9352518 7896
8977835c 7897 if (symidx < locsymcount)
d9352518 7898 {
8977835c
AM
7899 Elf_Internal_Sym *sym;
7900
7901 sym = isymbuf + symidx;
7902 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7903 {
7904 /* It is a local symbol: move it to the
7905 "absolute" section and give it a value. */
7906 sym->st_shndx = SHN_ABS;
7907 sym->st_value = val;
7908 return;
7909 }
7910 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7911 extsymoff = 0;
d9352518 7912 }
8977835c
AM
7913
7914 /* It is a global symbol: set its link type
7915 to "defined" and give it a value. */
7916
7917 sym_hashes = elf_sym_hashes (bfd_with_globals);
7918 h = sym_hashes [symidx - extsymoff];
7919 while (h->root.type == bfd_link_hash_indirect
7920 || h->root.type == bfd_link_hash_warning)
7921 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7922 h->root.type = bfd_link_hash_defined;
7923 h->root.u.def.value = val;
7924 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7925}
7926
a0c8462f
AM
7927static bfd_boolean
7928resolve_symbol (const char *name,
7929 bfd *input_bfd,
8b127cbc 7930 struct elf_final_link_info *flinfo,
a0c8462f
AM
7931 bfd_vma *result,
7932 Elf_Internal_Sym *isymbuf,
7933 size_t locsymcount)
d9352518 7934{
a0c8462f
AM
7935 Elf_Internal_Sym *sym;
7936 struct bfd_link_hash_entry *global_entry;
7937 const char *candidate = NULL;
7938 Elf_Internal_Shdr *symtab_hdr;
7939 size_t i;
7940
d9352518
DB
7941 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7942
7943 for (i = 0; i < locsymcount; ++ i)
7944 {
8977835c 7945 sym = isymbuf + i;
d9352518
DB
7946
7947 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7948 continue;
7949
7950 candidate = bfd_elf_string_from_elf_section (input_bfd,
7951 symtab_hdr->sh_link,
7952 sym->st_name);
7953#ifdef DEBUG
0f02bbd9
AM
7954 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7955 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7956#endif
7957 if (candidate && strcmp (candidate, name) == 0)
7958 {
8b127cbc 7959 asection *sec = flinfo->sections [i];
d9352518 7960
0f02bbd9
AM
7961 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7962 *result += sec->output_offset + sec->output_section->vma;
d9352518 7963#ifdef DEBUG
0f02bbd9
AM
7964 printf ("Found symbol with value %8.8lx\n",
7965 (unsigned long) *result);
d9352518
DB
7966#endif
7967 return TRUE;
7968 }
7969 }
7970
7971 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7972 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7973 FALSE, FALSE, TRUE);
d9352518
DB
7974 if (!global_entry)
7975 return FALSE;
a0c8462f 7976
d9352518
DB
7977 if (global_entry->type == bfd_link_hash_defined
7978 || global_entry->type == bfd_link_hash_defweak)
7979 {
a0c8462f
AM
7980 *result = (global_entry->u.def.value
7981 + global_entry->u.def.section->output_section->vma
7982 + global_entry->u.def.section->output_offset);
d9352518 7983#ifdef DEBUG
0f02bbd9
AM
7984 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7985 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7986#endif
7987 return TRUE;
a0c8462f 7988 }
d9352518 7989
d9352518
DB
7990 return FALSE;
7991}
7992
37b01f6a
DG
7993/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7994 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7995 names like "foo.end" which is the end address of section "foo". */
7996
d9352518 7997static bfd_boolean
a0c8462f
AM
7998resolve_section (const char *name,
7999 asection *sections,
37b01f6a
DG
8000 bfd_vma *result,
8001 bfd * abfd)
d9352518 8002{
a0c8462f
AM
8003 asection *curr;
8004 unsigned int len;
d9352518 8005
a0c8462f 8006 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8007 if (strcmp (curr->name, name) == 0)
8008 {
8009 *result = curr->vma;
8010 return TRUE;
8011 }
8012
8013 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8014 /* FIXME: This could be coded more efficiently... */
a0c8462f 8015 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8016 {
8017 len = strlen (curr->name);
a0c8462f 8018 if (len > strlen (name))
d9352518
DB
8019 continue;
8020
8021 if (strncmp (curr->name, name, len) == 0)
8022 {
8023 if (strncmp (".end", name + len, 4) == 0)
8024 {
37b01f6a 8025 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8026 return TRUE;
8027 }
8028
8029 /* Insert more pseudo-section names here, if you like. */
8030 }
8031 }
a0c8462f 8032
d9352518
DB
8033 return FALSE;
8034}
8035
8036static void
a0c8462f 8037undefined_reference (const char *reftype, const char *name)
d9352518 8038{
695344c0 8039 /* xgettext:c-format */
a0c8462f
AM
8040 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8041 reftype, name);
d9352518
DB
8042}
8043
8044static bfd_boolean
a0c8462f
AM
8045eval_symbol (bfd_vma *result,
8046 const char **symp,
8047 bfd *input_bfd,
8b127cbc 8048 struct elf_final_link_info *flinfo,
a0c8462f
AM
8049 bfd_vma dot,
8050 Elf_Internal_Sym *isymbuf,
8051 size_t locsymcount,
8052 int signed_p)
d9352518 8053{
4b93929b
NC
8054 size_t len;
8055 size_t symlen;
a0c8462f
AM
8056 bfd_vma a;
8057 bfd_vma b;
4b93929b 8058 char symbuf[4096];
0f02bbd9 8059 const char *sym = *symp;
a0c8462f
AM
8060 const char *symend;
8061 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8062
8063 len = strlen (sym);
8064 symend = sym + len;
8065
4b93929b 8066 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8067 {
8068 bfd_set_error (bfd_error_invalid_operation);
8069 return FALSE;
8070 }
a0c8462f 8071
d9352518
DB
8072 switch (* sym)
8073 {
8074 case '.':
0f02bbd9
AM
8075 *result = dot;
8076 *symp = sym + 1;
d9352518
DB
8077 return TRUE;
8078
8079 case '#':
0f02bbd9
AM
8080 ++sym;
8081 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8082 return TRUE;
8083
8084 case 'S':
8085 symbol_is_section = TRUE;
1a0670f3 8086 /* Fall through. */
a0c8462f 8087 case 's':
0f02bbd9
AM
8088 ++sym;
8089 symlen = strtol (sym, (char **) symp, 10);
8090 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8091
4b93929b 8092 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8093 {
8094 bfd_set_error (bfd_error_invalid_operation);
8095 return FALSE;
8096 }
8097
8098 memcpy (symbuf, sym, symlen);
a0c8462f 8099 symbuf[symlen] = '\0';
0f02bbd9 8100 *symp = sym + symlen;
a0c8462f
AM
8101
8102 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8103 the symbol as a section, or vice-versa. so we're pretty liberal in our
8104 interpretation here; section means "try section first", not "must be a
8105 section", and likewise with symbol. */
8106
a0c8462f 8107 if (symbol_is_section)
d9352518 8108 {
37b01f6a 8109 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8110 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8111 isymbuf, locsymcount))
d9352518
DB
8112 {
8113 undefined_reference ("section", symbuf);
8114 return FALSE;
8115 }
a0c8462f
AM
8116 }
8117 else
d9352518 8118 {
8b127cbc 8119 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8120 isymbuf, locsymcount)
8b127cbc 8121 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8122 result, input_bfd))
d9352518
DB
8123 {
8124 undefined_reference ("symbol", symbuf);
8125 return FALSE;
8126 }
8127 }
8128
8129 return TRUE;
a0c8462f 8130
d9352518
DB
8131 /* All that remains are operators. */
8132
8133#define UNARY_OP(op) \
8134 if (strncmp (sym, #op, strlen (#op)) == 0) \
8135 { \
8136 sym += strlen (#op); \
a0c8462f
AM
8137 if (*sym == ':') \
8138 ++sym; \
0f02bbd9 8139 *symp = sym; \
8b127cbc 8140 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8141 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8142 return FALSE; \
8143 if (signed_p) \
0f02bbd9 8144 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8145 else \
8146 *result = op a; \
d9352518
DB
8147 return TRUE; \
8148 }
8149
8150#define BINARY_OP(op) \
8151 if (strncmp (sym, #op, strlen (#op)) == 0) \
8152 { \
8153 sym += strlen (#op); \
a0c8462f
AM
8154 if (*sym == ':') \
8155 ++sym; \
0f02bbd9 8156 *symp = sym; \
8b127cbc 8157 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8158 isymbuf, locsymcount, signed_p)) \
a0c8462f 8159 return FALSE; \
0f02bbd9 8160 ++*symp; \
8b127cbc 8161 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8162 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8163 return FALSE; \
8164 if (signed_p) \
0f02bbd9 8165 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8166 else \
8167 *result = a op b; \
d9352518
DB
8168 return TRUE; \
8169 }
8170
8171 default:
8172 UNARY_OP (0-);
8173 BINARY_OP (<<);
8174 BINARY_OP (>>);
8175 BINARY_OP (==);
8176 BINARY_OP (!=);
8177 BINARY_OP (<=);
8178 BINARY_OP (>=);
8179 BINARY_OP (&&);
8180 BINARY_OP (||);
8181 UNARY_OP (~);
8182 UNARY_OP (!);
8183 BINARY_OP (*);
8184 BINARY_OP (/);
8185 BINARY_OP (%);
8186 BINARY_OP (^);
8187 BINARY_OP (|);
8188 BINARY_OP (&);
8189 BINARY_OP (+);
8190 BINARY_OP (-);
8191 BINARY_OP (<);
8192 BINARY_OP (>);
8193#undef UNARY_OP
8194#undef BINARY_OP
8195 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8196 bfd_set_error (bfd_error_invalid_operation);
8197 return FALSE;
8198 }
8199}
8200
d9352518 8201static void
a0c8462f
AM
8202put_value (bfd_vma size,
8203 unsigned long chunksz,
8204 bfd *input_bfd,
8205 bfd_vma x,
8206 bfd_byte *location)
d9352518
DB
8207{
8208 location += (size - chunksz);
8209
41cd1ad1 8210 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8211 {
8212 switch (chunksz)
8213 {
d9352518
DB
8214 case 1:
8215 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8216 x >>= 8;
d9352518
DB
8217 break;
8218 case 2:
8219 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8220 x >>= 16;
d9352518
DB
8221 break;
8222 case 4:
8223 bfd_put_32 (input_bfd, x, location);
65164438
NC
8224 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8225 x >>= 16;
8226 x >>= 16;
d9352518 8227 break;
d9352518 8228#ifdef BFD64
41cd1ad1 8229 case 8:
d9352518 8230 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8231 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8232 x >>= 32;
8233 x >>= 32;
8234 break;
d9352518 8235#endif
41cd1ad1
NC
8236 default:
8237 abort ();
d9352518
DB
8238 break;
8239 }
8240 }
8241}
8242
a0c8462f
AM
8243static bfd_vma
8244get_value (bfd_vma size,
8245 unsigned long chunksz,
8246 bfd *input_bfd,
8247 bfd_byte *location)
d9352518 8248{
9b239e0e 8249 int shift;
d9352518
DB
8250 bfd_vma x = 0;
8251
9b239e0e
NC
8252 /* Sanity checks. */
8253 BFD_ASSERT (chunksz <= sizeof (x)
8254 && size >= chunksz
8255 && chunksz != 0
8256 && (size % chunksz) == 0
8257 && input_bfd != NULL
8258 && location != NULL);
8259
8260 if (chunksz == sizeof (x))
8261 {
8262 BFD_ASSERT (size == chunksz);
8263
8264 /* Make sure that we do not perform an undefined shift operation.
8265 We know that size == chunksz so there will only be one iteration
8266 of the loop below. */
8267 shift = 0;
8268 }
8269 else
8270 shift = 8 * chunksz;
8271
a0c8462f 8272 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8273 {
8274 switch (chunksz)
8275 {
d9352518 8276 case 1:
9b239e0e 8277 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8278 break;
8279 case 2:
9b239e0e 8280 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8281 break;
8282 case 4:
9b239e0e 8283 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8284 break;
d9352518 8285#ifdef BFD64
9b239e0e
NC
8286 case 8:
8287 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8288 break;
9b239e0e
NC
8289#endif
8290 default:
8291 abort ();
d9352518
DB
8292 }
8293 }
8294 return x;
8295}
8296
a0c8462f
AM
8297static void
8298decode_complex_addend (unsigned long *start, /* in bits */
8299 unsigned long *oplen, /* in bits */
8300 unsigned long *len, /* in bits */
8301 unsigned long *wordsz, /* in bytes */
8302 unsigned long *chunksz, /* in bytes */
8303 unsigned long *lsb0_p,
8304 unsigned long *signed_p,
8305 unsigned long *trunc_p,
8306 unsigned long encoded)
d9352518
DB
8307{
8308 * start = encoded & 0x3F;
8309 * len = (encoded >> 6) & 0x3F;
8310 * oplen = (encoded >> 12) & 0x3F;
8311 * wordsz = (encoded >> 18) & 0xF;
8312 * chunksz = (encoded >> 22) & 0xF;
8313 * lsb0_p = (encoded >> 27) & 1;
8314 * signed_p = (encoded >> 28) & 1;
8315 * trunc_p = (encoded >> 29) & 1;
8316}
8317
cdfeee4f 8318bfd_reloc_status_type
0f02bbd9 8319bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8320 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8321 bfd_byte *contents,
8322 Elf_Internal_Rela *rel,
8323 bfd_vma relocation)
d9352518 8324{
0f02bbd9
AM
8325 bfd_vma shift, x, mask;
8326 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8327 bfd_reloc_status_type r;
d9352518
DB
8328
8329 /* Perform this reloc, since it is complex.
8330 (this is not to say that it necessarily refers to a complex
8331 symbol; merely that it is a self-describing CGEN based reloc.
8332 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8333 word size, etc) encoded within it.). */
d9352518 8334
a0c8462f
AM
8335 decode_complex_addend (&start, &oplen, &len, &wordsz,
8336 &chunksz, &lsb0_p, &signed_p,
8337 &trunc_p, rel->r_addend);
d9352518
DB
8338
8339 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8340
8341 if (lsb0_p)
8342 shift = (start + 1) - len;
8343 else
8344 shift = (8 * wordsz) - (start + len);
8345
37b01f6a
DG
8346 x = get_value (wordsz, chunksz, input_bfd,
8347 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8348
8349#ifdef DEBUG
8350 printf ("Doing complex reloc: "
8351 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8352 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8353 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8354 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8355 oplen, (unsigned long) x, (unsigned long) mask,
8356 (unsigned long) relocation);
d9352518
DB
8357#endif
8358
cdfeee4f 8359 r = bfd_reloc_ok;
d9352518 8360 if (! trunc_p)
cdfeee4f
AM
8361 /* Now do an overflow check. */
8362 r = bfd_check_overflow ((signed_p
8363 ? complain_overflow_signed
8364 : complain_overflow_unsigned),
8365 len, 0, (8 * wordsz),
8366 relocation);
a0c8462f 8367
d9352518
DB
8368 /* Do the deed. */
8369 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8370
8371#ifdef DEBUG
8372 printf (" relocation: %8.8lx\n"
8373 " shifted mask: %8.8lx\n"
8374 " shifted/masked reloc: %8.8lx\n"
8375 " result: %8.8lx\n",
9ccb8af9
AM
8376 (unsigned long) relocation, (unsigned long) (mask << shift),
8377 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8378#endif
37b01f6a
DG
8379 put_value (wordsz, chunksz, input_bfd, x,
8380 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8381 return r;
d9352518
DB
8382}
8383
0e287786
AM
8384/* Functions to read r_offset from external (target order) reloc
8385 entry. Faster than bfd_getl32 et al, because we let the compiler
8386 know the value is aligned. */
53df40a4 8387
0e287786
AM
8388static bfd_vma
8389ext32l_r_offset (const void *p)
53df40a4
AM
8390{
8391 union aligned32
8392 {
8393 uint32_t v;
8394 unsigned char c[4];
8395 };
8396 const union aligned32 *a
0e287786 8397 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8398
8399 uint32_t aval = ( (uint32_t) a->c[0]
8400 | (uint32_t) a->c[1] << 8
8401 | (uint32_t) a->c[2] << 16
8402 | (uint32_t) a->c[3] << 24);
0e287786 8403 return aval;
53df40a4
AM
8404}
8405
0e287786
AM
8406static bfd_vma
8407ext32b_r_offset (const void *p)
53df40a4
AM
8408{
8409 union aligned32
8410 {
8411 uint32_t v;
8412 unsigned char c[4];
8413 };
8414 const union aligned32 *a
0e287786 8415 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8416
8417 uint32_t aval = ( (uint32_t) a->c[0] << 24
8418 | (uint32_t) a->c[1] << 16
8419 | (uint32_t) a->c[2] << 8
8420 | (uint32_t) a->c[3]);
0e287786 8421 return aval;
53df40a4
AM
8422}
8423
8424#ifdef BFD_HOST_64_BIT
0e287786
AM
8425static bfd_vma
8426ext64l_r_offset (const void *p)
53df40a4
AM
8427{
8428 union aligned64
8429 {
8430 uint64_t v;
8431 unsigned char c[8];
8432 };
8433 const union aligned64 *a
0e287786 8434 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8435
8436 uint64_t aval = ( (uint64_t) a->c[0]
8437 | (uint64_t) a->c[1] << 8
8438 | (uint64_t) a->c[2] << 16
8439 | (uint64_t) a->c[3] << 24
8440 | (uint64_t) a->c[4] << 32
8441 | (uint64_t) a->c[5] << 40
8442 | (uint64_t) a->c[6] << 48
8443 | (uint64_t) a->c[7] << 56);
0e287786 8444 return aval;
53df40a4
AM
8445}
8446
0e287786
AM
8447static bfd_vma
8448ext64b_r_offset (const void *p)
53df40a4
AM
8449{
8450 union aligned64
8451 {
8452 uint64_t v;
8453 unsigned char c[8];
8454 };
8455 const union aligned64 *a
0e287786 8456 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8457
8458 uint64_t aval = ( (uint64_t) a->c[0] << 56
8459 | (uint64_t) a->c[1] << 48
8460 | (uint64_t) a->c[2] << 40
8461 | (uint64_t) a->c[3] << 32
8462 | (uint64_t) a->c[4] << 24
8463 | (uint64_t) a->c[5] << 16
8464 | (uint64_t) a->c[6] << 8
8465 | (uint64_t) a->c[7]);
0e287786 8466 return aval;
53df40a4
AM
8467}
8468#endif
8469
c152c796
AM
8470/* When performing a relocatable link, the input relocations are
8471 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8472 referenced must be updated. Update all the relocations found in
8473 RELDATA. */
c152c796 8474
bca6d0e3 8475static bfd_boolean
c152c796 8476elf_link_adjust_relocs (bfd *abfd,
9eaff861 8477 asection *sec,
28dbcedc
AM
8478 struct bfd_elf_section_reloc_data *reldata,
8479 bfd_boolean sort)
c152c796
AM
8480{
8481 unsigned int i;
8482 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8483 bfd_byte *erela;
8484 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8485 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8486 bfd_vma r_type_mask;
8487 int r_sym_shift;
d4730f92
BS
8488 unsigned int count = reldata->count;
8489 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8490
d4730f92 8491 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8492 {
8493 swap_in = bed->s->swap_reloc_in;
8494 swap_out = bed->s->swap_reloc_out;
8495 }
d4730f92 8496 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8497 {
8498 swap_in = bed->s->swap_reloca_in;
8499 swap_out = bed->s->swap_reloca_out;
8500 }
8501 else
8502 abort ();
8503
8504 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8505 abort ();
8506
8507 if (bed->s->arch_size == 32)
8508 {
8509 r_type_mask = 0xff;
8510 r_sym_shift = 8;
8511 }
8512 else
8513 {
8514 r_type_mask = 0xffffffff;
8515 r_sym_shift = 32;
8516 }
8517
d4730f92
BS
8518 erela = reldata->hdr->contents;
8519 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8520 {
8521 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8522 unsigned int j;
8523
8524 if (*rel_hash == NULL)
8525 continue;
8526
8527 BFD_ASSERT ((*rel_hash)->indx >= 0);
8528
8529 (*swap_in) (abfd, erela, irela);
8530 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8531 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8532 | (irela[j].r_info & r_type_mask));
8533 (*swap_out) (abfd, irela, erela);
8534 }
53df40a4 8535
9eaff861
AO
8536 if (bed->elf_backend_update_relocs)
8537 (*bed->elf_backend_update_relocs) (sec, reldata);
8538
0e287786 8539 if (sort && count != 0)
53df40a4 8540 {
0e287786
AM
8541 bfd_vma (*ext_r_off) (const void *);
8542 bfd_vma r_off;
8543 size_t elt_size;
8544 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8545 bfd_byte *buf = NULL;
28dbcedc
AM
8546
8547 if (bed->s->arch_size == 32)
8548 {
8549 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8550 ext_r_off = ext32l_r_offset;
28dbcedc 8551 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8552 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8553 else
8554 abort ();
8555 }
53df40a4 8556 else
28dbcedc 8557 {
53df40a4 8558#ifdef BFD_HOST_64_BIT
28dbcedc 8559 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8560 ext_r_off = ext64l_r_offset;
28dbcedc 8561 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8562 ext_r_off = ext64b_r_offset;
28dbcedc 8563 else
53df40a4 8564#endif
28dbcedc
AM
8565 abort ();
8566 }
0e287786 8567
bca6d0e3
AM
8568 /* Must use a stable sort here. A modified insertion sort,
8569 since the relocs are mostly sorted already. */
0e287786
AM
8570 elt_size = reldata->hdr->sh_entsize;
8571 base = reldata->hdr->contents;
8572 end = base + count * elt_size;
bca6d0e3 8573 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8574 abort ();
8575
8576 /* Ensure the first element is lowest. This acts as a sentinel,
8577 speeding the main loop below. */
8578 r_off = (*ext_r_off) (base);
8579 for (p = loc = base; (p += elt_size) < end; )
8580 {
8581 bfd_vma r_off2 = (*ext_r_off) (p);
8582 if (r_off > r_off2)
8583 {
8584 r_off = r_off2;
8585 loc = p;
8586 }
8587 }
8588 if (loc != base)
8589 {
8590 /* Don't just swap *base and *loc as that changes the order
8591 of the original base[0] and base[1] if they happen to
8592 have the same r_offset. */
bca6d0e3
AM
8593 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8594 memcpy (onebuf, loc, elt_size);
0e287786 8595 memmove (base + elt_size, base, loc - base);
bca6d0e3 8596 memcpy (base, onebuf, elt_size);
0e287786
AM
8597 }
8598
b29b8669 8599 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8600 {
8601 /* base to p is sorted, *p is next to insert. */
8602 r_off = (*ext_r_off) (p);
8603 /* Search the sorted region for location to insert. */
8604 loc = p - elt_size;
8605 while (r_off < (*ext_r_off) (loc))
8606 loc -= elt_size;
8607 loc += elt_size;
8608 if (loc != p)
8609 {
bca6d0e3
AM
8610 /* Chances are there is a run of relocs to insert here,
8611 from one of more input files. Files are not always
8612 linked in order due to the way elf_link_input_bfd is
8613 called. See pr17666. */
8614 size_t sortlen = p - loc;
8615 bfd_vma r_off2 = (*ext_r_off) (loc);
8616 size_t runlen = elt_size;
8617 size_t buf_size = 96 * 1024;
8618 while (p + runlen < end
8619 && (sortlen <= buf_size
8620 || runlen + elt_size <= buf_size)
8621 && r_off2 > (*ext_r_off) (p + runlen))
8622 runlen += elt_size;
8623 if (buf == NULL)
8624 {
8625 buf = bfd_malloc (buf_size);
8626 if (buf == NULL)
8627 return FALSE;
8628 }
8629 if (runlen < sortlen)
8630 {
8631 memcpy (buf, p, runlen);
8632 memmove (loc + runlen, loc, sortlen);
8633 memcpy (loc, buf, runlen);
8634 }
8635 else
8636 {
8637 memcpy (buf, loc, sortlen);
8638 memmove (loc, p, runlen);
8639 memcpy (loc + runlen, buf, sortlen);
8640 }
b29b8669 8641 p += runlen - elt_size;
0e287786
AM
8642 }
8643 }
8644 /* Hashes are no longer valid. */
28dbcedc
AM
8645 free (reldata->hashes);
8646 reldata->hashes = NULL;
bca6d0e3 8647 free (buf);
53df40a4 8648 }
bca6d0e3 8649 return TRUE;
c152c796
AM
8650}
8651
8652struct elf_link_sort_rela
8653{
8654 union {
8655 bfd_vma offset;
8656 bfd_vma sym_mask;
8657 } u;
8658 enum elf_reloc_type_class type;
8659 /* We use this as an array of size int_rels_per_ext_rel. */
8660 Elf_Internal_Rela rela[1];
8661};
8662
8663static int
8664elf_link_sort_cmp1 (const void *A, const void *B)
8665{
a50b1753
NC
8666 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8667 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8668 int relativea, relativeb;
8669
8670 relativea = a->type == reloc_class_relative;
8671 relativeb = b->type == reloc_class_relative;
8672
8673 if (relativea < relativeb)
8674 return 1;
8675 if (relativea > relativeb)
8676 return -1;
8677 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8678 return -1;
8679 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8680 return 1;
8681 if (a->rela->r_offset < b->rela->r_offset)
8682 return -1;
8683 if (a->rela->r_offset > b->rela->r_offset)
8684 return 1;
8685 return 0;
8686}
8687
8688static int
8689elf_link_sort_cmp2 (const void *A, const void *B)
8690{
a50b1753
NC
8691 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8692 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8693
7e612e98 8694 if (a->type < b->type)
c152c796 8695 return -1;
7e612e98 8696 if (a->type > b->type)
c152c796 8697 return 1;
7e612e98 8698 if (a->u.offset < b->u.offset)
c152c796 8699 return -1;
7e612e98 8700 if (a->u.offset > b->u.offset)
c152c796
AM
8701 return 1;
8702 if (a->rela->r_offset < b->rela->r_offset)
8703 return -1;
8704 if (a->rela->r_offset > b->rela->r_offset)
8705 return 1;
8706 return 0;
8707}
8708
8709static size_t
8710elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8711{
3410fea8 8712 asection *dynamic_relocs;
fc66a176
L
8713 asection *rela_dyn;
8714 asection *rel_dyn;
c152c796
AM
8715 bfd_size_type count, size;
8716 size_t i, ret, sort_elt, ext_size;
8717 bfd_byte *sort, *s_non_relative, *p;
8718 struct elf_link_sort_rela *sq;
8719 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8720 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8721 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8722 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8723 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8724 struct bfd_link_order *lo;
8725 bfd_vma r_sym_mask;
3410fea8 8726 bfd_boolean use_rela;
c152c796 8727
3410fea8
NC
8728 /* Find a dynamic reloc section. */
8729 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8730 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8731 if (rela_dyn != NULL && rela_dyn->size > 0
8732 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8733 {
3410fea8
NC
8734 bfd_boolean use_rela_initialised = FALSE;
8735
8736 /* This is just here to stop gcc from complaining.
c8e44c6d 8737 Its initialization checking code is not perfect. */
3410fea8
NC
8738 use_rela = TRUE;
8739
8740 /* Both sections are present. Examine the sizes
8741 of the indirect sections to help us choose. */
8742 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8743 if (lo->type == bfd_indirect_link_order)
8744 {
8745 asection *o = lo->u.indirect.section;
8746
8747 if ((o->size % bed->s->sizeof_rela) == 0)
8748 {
8749 if ((o->size % bed->s->sizeof_rel) == 0)
8750 /* Section size is divisible by both rel and rela sizes.
8751 It is of no help to us. */
8752 ;
8753 else
8754 {
8755 /* Section size is only divisible by rela. */
8756 if (use_rela_initialised && (use_rela == FALSE))
8757 {
c8e44c6d
AM
8758 _bfd_error_handler (_("%B: Unable to sort relocs - "
8759 "they are in more than one size"),
8760 abfd);
3410fea8
NC
8761 bfd_set_error (bfd_error_invalid_operation);
8762 return 0;
8763 }
8764 else
8765 {
8766 use_rela = TRUE;
8767 use_rela_initialised = TRUE;
8768 }
8769 }
8770 }
8771 else if ((o->size % bed->s->sizeof_rel) == 0)
8772 {
8773 /* Section size is only divisible by rel. */
8774 if (use_rela_initialised && (use_rela == TRUE))
8775 {
c8e44c6d
AM
8776 _bfd_error_handler (_("%B: Unable to sort relocs - "
8777 "they are in more than one size"),
8778 abfd);
3410fea8
NC
8779 bfd_set_error (bfd_error_invalid_operation);
8780 return 0;
8781 }
8782 else
8783 {
8784 use_rela = FALSE;
8785 use_rela_initialised = TRUE;
8786 }
8787 }
8788 else
8789 {
c8e44c6d
AM
8790 /* The section size is not divisible by either -
8791 something is wrong. */
8792 _bfd_error_handler (_("%B: Unable to sort relocs - "
8793 "they are of an unknown size"), abfd);
3410fea8
NC
8794 bfd_set_error (bfd_error_invalid_operation);
8795 return 0;
8796 }
8797 }
8798
8799 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8800 if (lo->type == bfd_indirect_link_order)
8801 {
8802 asection *o = lo->u.indirect.section;
8803
8804 if ((o->size % bed->s->sizeof_rela) == 0)
8805 {
8806 if ((o->size % bed->s->sizeof_rel) == 0)
8807 /* Section size is divisible by both rel and rela sizes.
8808 It is of no help to us. */
8809 ;
8810 else
8811 {
8812 /* Section size is only divisible by rela. */
8813 if (use_rela_initialised && (use_rela == FALSE))
8814 {
c8e44c6d
AM
8815 _bfd_error_handler (_("%B: Unable to sort relocs - "
8816 "they are in more than one size"),
8817 abfd);
3410fea8
NC
8818 bfd_set_error (bfd_error_invalid_operation);
8819 return 0;
8820 }
8821 else
8822 {
8823 use_rela = TRUE;
8824 use_rela_initialised = TRUE;
8825 }
8826 }
8827 }
8828 else if ((o->size % bed->s->sizeof_rel) == 0)
8829 {
8830 /* Section size is only divisible by rel. */
8831 if (use_rela_initialised && (use_rela == TRUE))
8832 {
c8e44c6d
AM
8833 _bfd_error_handler (_("%B: Unable to sort relocs - "
8834 "they are in more than one size"),
8835 abfd);
3410fea8
NC
8836 bfd_set_error (bfd_error_invalid_operation);
8837 return 0;
8838 }
8839 else
8840 {
8841 use_rela = FALSE;
8842 use_rela_initialised = TRUE;
8843 }
8844 }
8845 else
8846 {
c8e44c6d
AM
8847 /* The section size is not divisible by either -
8848 something is wrong. */
8849 _bfd_error_handler (_("%B: Unable to sort relocs - "
8850 "they are of an unknown size"), abfd);
3410fea8
NC
8851 bfd_set_error (bfd_error_invalid_operation);
8852 return 0;
8853 }
8854 }
8855
8856 if (! use_rela_initialised)
8857 /* Make a guess. */
8858 use_rela = TRUE;
c152c796 8859 }
fc66a176
L
8860 else if (rela_dyn != NULL && rela_dyn->size > 0)
8861 use_rela = TRUE;
8862 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8863 use_rela = FALSE;
c152c796 8864 else
fc66a176 8865 return 0;
3410fea8
NC
8866
8867 if (use_rela)
c152c796 8868 {
3410fea8 8869 dynamic_relocs = rela_dyn;
c152c796
AM
8870 ext_size = bed->s->sizeof_rela;
8871 swap_in = bed->s->swap_reloca_in;
8872 swap_out = bed->s->swap_reloca_out;
8873 }
3410fea8
NC
8874 else
8875 {
8876 dynamic_relocs = rel_dyn;
8877 ext_size = bed->s->sizeof_rel;
8878 swap_in = bed->s->swap_reloc_in;
8879 swap_out = bed->s->swap_reloc_out;
8880 }
c152c796
AM
8881
8882 size = 0;
3410fea8 8883 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8884 if (lo->type == bfd_indirect_link_order)
3410fea8 8885 size += lo->u.indirect.section->size;
c152c796 8886
3410fea8 8887 if (size != dynamic_relocs->size)
c152c796
AM
8888 return 0;
8889
8890 sort_elt = (sizeof (struct elf_link_sort_rela)
8891 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8892
8893 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8894 if (count == 0)
8895 return 0;
a50b1753 8896 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8897
c152c796
AM
8898 if (sort == NULL)
8899 {
8900 (*info->callbacks->warning)
8901 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8902 return 0;
8903 }
8904
8905 if (bed->s->arch_size == 32)
8906 r_sym_mask = ~(bfd_vma) 0xff;
8907 else
8908 r_sym_mask = ~(bfd_vma) 0xffffffff;
8909
3410fea8 8910 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8911 if (lo->type == bfd_indirect_link_order)
8912 {
8913 bfd_byte *erel, *erelend;
8914 asection *o = lo->u.indirect.section;
8915
1da212d6
AM
8916 if (o->contents == NULL && o->size != 0)
8917 {
8918 /* This is a reloc section that is being handled as a normal
8919 section. See bfd_section_from_shdr. We can't combine
8920 relocs in this case. */
8921 free (sort);
8922 return 0;
8923 }
c152c796 8924 erel = o->contents;
eea6121a 8925 erelend = o->contents + o->size;
c8e44c6d 8926 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8927
c152c796
AM
8928 while (erel < erelend)
8929 {
8930 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8931
c152c796 8932 (*swap_in) (abfd, erel, s->rela);
7e612e98 8933 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8934 s->u.sym_mask = r_sym_mask;
8935 p += sort_elt;
8936 erel += ext_size;
8937 }
8938 }
8939
8940 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8941
8942 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8943 {
8944 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8945 if (s->type != reloc_class_relative)
8946 break;
8947 }
8948 ret = i;
8949 s_non_relative = p;
8950
8951 sq = (struct elf_link_sort_rela *) s_non_relative;
8952 for (; i < count; i++, p += sort_elt)
8953 {
8954 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8955 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8956 sq = sp;
8957 sp->u.offset = sq->rela->r_offset;
8958 }
8959
8960 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8961
c8e44c6d
AM
8962 struct elf_link_hash_table *htab = elf_hash_table (info);
8963 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8964 {
8965 /* We have plt relocs in .rela.dyn. */
8966 sq = (struct elf_link_sort_rela *) sort;
8967 for (i = 0; i < count; i++)
8968 if (sq[count - i - 1].type != reloc_class_plt)
8969 break;
8970 if (i != 0 && htab->srelplt->size == i * ext_size)
8971 {
8972 struct bfd_link_order **plo;
8973 /* Put srelplt link_order last. This is so the output_offset
8974 set in the next loop is correct for DT_JMPREL. */
8975 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8976 if ((*plo)->type == bfd_indirect_link_order
8977 && (*plo)->u.indirect.section == htab->srelplt)
8978 {
8979 lo = *plo;
8980 *plo = lo->next;
8981 }
8982 else
8983 plo = &(*plo)->next;
8984 *plo = lo;
8985 lo->next = NULL;
8986 dynamic_relocs->map_tail.link_order = lo;
8987 }
8988 }
8989
8990 p = sort;
3410fea8 8991 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8992 if (lo->type == bfd_indirect_link_order)
8993 {
8994 bfd_byte *erel, *erelend;
8995 asection *o = lo->u.indirect.section;
8996
8997 erel = o->contents;
eea6121a 8998 erelend = o->contents + o->size;
c8e44c6d 8999 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9000 while (erel < erelend)
9001 {
9002 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9003 (*swap_out) (abfd, s->rela, erel);
9004 p += sort_elt;
9005 erel += ext_size;
9006 }
9007 }
9008
9009 free (sort);
3410fea8 9010 *psec = dynamic_relocs;
c152c796
AM
9011 return ret;
9012}
9013
ef10c3ac 9014/* Add a symbol to the output symbol string table. */
c152c796 9015
6e0b88f1 9016static int
ef10c3ac
L
9017elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9018 const char *name,
9019 Elf_Internal_Sym *elfsym,
9020 asection *input_sec,
9021 struct elf_link_hash_entry *h)
c152c796 9022{
6e0b88f1 9023 int (*output_symbol_hook)
c152c796
AM
9024 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9025 struct elf_link_hash_entry *);
ef10c3ac 9026 struct elf_link_hash_table *hash_table;
c152c796 9027 const struct elf_backend_data *bed;
ef10c3ac 9028 bfd_size_type strtabsize;
c152c796 9029
8539e4e8
AM
9030 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9031
8b127cbc 9032 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9033 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9034 if (output_symbol_hook != NULL)
9035 {
8b127cbc 9036 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9037 if (ret != 1)
9038 return ret;
c152c796
AM
9039 }
9040
ef10c3ac
L
9041 if (name == NULL
9042 || *name == '\0'
9043 || (input_sec->flags & SEC_EXCLUDE))
9044 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9045 else
9046 {
ef10c3ac
L
9047 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9048 to get the final offset for st_name. */
9049 elfsym->st_name
9050 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9051 name, FALSE);
c152c796 9052 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9053 return 0;
c152c796
AM
9054 }
9055
ef10c3ac
L
9056 hash_table = elf_hash_table (flinfo->info);
9057 strtabsize = hash_table->strtabsize;
9058 if (strtabsize <= hash_table->strtabcount)
c152c796 9059 {
ef10c3ac
L
9060 strtabsize += strtabsize;
9061 hash_table->strtabsize = strtabsize;
9062 strtabsize *= sizeof (*hash_table->strtab);
9063 hash_table->strtab
9064 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9065 strtabsize);
9066 if (hash_table->strtab == NULL)
6e0b88f1 9067 return 0;
c152c796 9068 }
ef10c3ac
L
9069 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9070 hash_table->strtab[hash_table->strtabcount].dest_index
9071 = hash_table->strtabcount;
9072 hash_table->strtab[hash_table->strtabcount].destshndx_index
9073 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9074
9075 bfd_get_symcount (flinfo->output_bfd) += 1;
9076 hash_table->strtabcount += 1;
9077
9078 return 1;
9079}
9080
9081/* Swap symbols out to the symbol table and flush the output symbols to
9082 the file. */
9083
9084static bfd_boolean
9085elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9086{
9087 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9088 bfd_size_type amt;
9089 size_t i;
ef10c3ac
L
9090 const struct elf_backend_data *bed;
9091 bfd_byte *symbuf;
9092 Elf_Internal_Shdr *hdr;
9093 file_ptr pos;
9094 bfd_boolean ret;
9095
9096 if (!hash_table->strtabcount)
9097 return TRUE;
9098
9099 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9100
9101 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9102
ef10c3ac
L
9103 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9104 symbuf = (bfd_byte *) bfd_malloc (amt);
9105 if (symbuf == NULL)
9106 return FALSE;
1b786873 9107
ef10c3ac 9108 if (flinfo->symshndxbuf)
c152c796 9109 {
ef53be89
AM
9110 amt = sizeof (Elf_External_Sym_Shndx);
9111 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9112 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9113 if (flinfo->symshndxbuf == NULL)
c152c796 9114 {
ef10c3ac
L
9115 free (symbuf);
9116 return FALSE;
c152c796 9117 }
c152c796
AM
9118 }
9119
ef10c3ac
L
9120 for (i = 0; i < hash_table->strtabcount; i++)
9121 {
9122 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9123 if (elfsym->sym.st_name == (unsigned long) -1)
9124 elfsym->sym.st_name = 0;
9125 else
9126 elfsym->sym.st_name
9127 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9128 elfsym->sym.st_name);
9129 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9130 ((bfd_byte *) symbuf
9131 + (elfsym->dest_index
9132 * bed->s->sizeof_sym)),
9133 (flinfo->symshndxbuf
9134 + elfsym->destshndx_index));
9135 }
9136
9137 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9138 pos = hdr->sh_offset + hdr->sh_size;
9139 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9140 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9141 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9142 {
9143 hdr->sh_size += amt;
9144 ret = TRUE;
9145 }
9146 else
9147 ret = FALSE;
c152c796 9148
ef10c3ac
L
9149 free (symbuf);
9150
9151 free (hash_table->strtab);
9152 hash_table->strtab = NULL;
9153
9154 return ret;
c152c796
AM
9155}
9156
c0d5a53d
L
9157/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9158
9159static bfd_boolean
9160check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9161{
4fbb74a6
AM
9162 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9163 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9164 {
9165 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9166 beyond 64k. */
4eca0228 9167 _bfd_error_handler
695344c0 9168 /* xgettext:c-format */
c0d5a53d 9169 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9170 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9171 bfd_set_error (bfd_error_nonrepresentable_section);
9172 return FALSE;
9173 }
9174 return TRUE;
9175}
9176
c152c796
AM
9177/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9178 allowing an unsatisfied unversioned symbol in the DSO to match a
9179 versioned symbol that would normally require an explicit version.
9180 We also handle the case that a DSO references a hidden symbol
9181 which may be satisfied by a versioned symbol in another DSO. */
9182
9183static bfd_boolean
9184elf_link_check_versioned_symbol (struct bfd_link_info *info,
9185 const struct elf_backend_data *bed,
9186 struct elf_link_hash_entry *h)
9187{
9188 bfd *abfd;
9189 struct elf_link_loaded_list *loaded;
9190
9191 if (!is_elf_hash_table (info->hash))
9192 return FALSE;
9193
90c984fc
L
9194 /* Check indirect symbol. */
9195 while (h->root.type == bfd_link_hash_indirect)
9196 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9197
c152c796
AM
9198 switch (h->root.type)
9199 {
9200 default:
9201 abfd = NULL;
9202 break;
9203
9204 case bfd_link_hash_undefined:
9205 case bfd_link_hash_undefweak:
9206 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9207 if (abfd == NULL
9208 || (abfd->flags & DYNAMIC) == 0
e56f61be 9209 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9210 return FALSE;
9211 break;
9212
9213 case bfd_link_hash_defined:
9214 case bfd_link_hash_defweak:
9215 abfd = h->root.u.def.section->owner;
9216 break;
9217
9218 case bfd_link_hash_common:
9219 abfd = h->root.u.c.p->section->owner;
9220 break;
9221 }
9222 BFD_ASSERT (abfd != NULL);
9223
9224 for (loaded = elf_hash_table (info)->loaded;
9225 loaded != NULL;
9226 loaded = loaded->next)
9227 {
9228 bfd *input;
9229 Elf_Internal_Shdr *hdr;
ef53be89
AM
9230 size_t symcount;
9231 size_t extsymcount;
9232 size_t extsymoff;
c152c796
AM
9233 Elf_Internal_Shdr *versymhdr;
9234 Elf_Internal_Sym *isym;
9235 Elf_Internal_Sym *isymend;
9236 Elf_Internal_Sym *isymbuf;
9237 Elf_External_Versym *ever;
9238 Elf_External_Versym *extversym;
9239
9240 input = loaded->abfd;
9241
9242 /* We check each DSO for a possible hidden versioned definition. */
9243 if (input == abfd
9244 || (input->flags & DYNAMIC) == 0
9245 || elf_dynversym (input) == 0)
9246 continue;
9247
9248 hdr = &elf_tdata (input)->dynsymtab_hdr;
9249
9250 symcount = hdr->sh_size / bed->s->sizeof_sym;
9251 if (elf_bad_symtab (input))
9252 {
9253 extsymcount = symcount;
9254 extsymoff = 0;
9255 }
9256 else
9257 {
9258 extsymcount = symcount - hdr->sh_info;
9259 extsymoff = hdr->sh_info;
9260 }
9261
9262 if (extsymcount == 0)
9263 continue;
9264
9265 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9266 NULL, NULL, NULL);
9267 if (isymbuf == NULL)
9268 return FALSE;
9269
9270 /* Read in any version definitions. */
9271 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9272 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9273 if (extversym == NULL)
9274 goto error_ret;
9275
9276 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9277 || (bfd_bread (extversym, versymhdr->sh_size, input)
9278 != versymhdr->sh_size))
9279 {
9280 free (extversym);
9281 error_ret:
9282 free (isymbuf);
9283 return FALSE;
9284 }
9285
9286 ever = extversym + extsymoff;
9287 isymend = isymbuf + extsymcount;
9288 for (isym = isymbuf; isym < isymend; isym++, ever++)
9289 {
9290 const char *name;
9291 Elf_Internal_Versym iver;
9292 unsigned short version_index;
9293
9294 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9295 || isym->st_shndx == SHN_UNDEF)
9296 continue;
9297
9298 name = bfd_elf_string_from_elf_section (input,
9299 hdr->sh_link,
9300 isym->st_name);
9301 if (strcmp (name, h->root.root.string) != 0)
9302 continue;
9303
9304 _bfd_elf_swap_versym_in (input, ever, &iver);
9305
d023c380
L
9306 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9307 && !(h->def_regular
9308 && h->forced_local))
c152c796
AM
9309 {
9310 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9311 have provided a definition for the undefined sym unless
9312 it is defined in a non-shared object and forced local.
9313 */
c152c796
AM
9314 abort ();
9315 }
9316
9317 version_index = iver.vs_vers & VERSYM_VERSION;
9318 if (version_index == 1 || version_index == 2)
9319 {
9320 /* This is the base or first version. We can use it. */
9321 free (extversym);
9322 free (isymbuf);
9323 return TRUE;
9324 }
9325 }
9326
9327 free (extversym);
9328 free (isymbuf);
9329 }
9330
9331 return FALSE;
9332}
9333
b8871f35
L
9334/* Convert ELF common symbol TYPE. */
9335
9336static int
9337elf_link_convert_common_type (struct bfd_link_info *info, int type)
9338{
9339 /* Commom symbol can only appear in relocatable link. */
9340 if (!bfd_link_relocatable (info))
9341 abort ();
9342 switch (info->elf_stt_common)
9343 {
9344 case unchanged:
9345 break;
9346 case elf_stt_common:
9347 type = STT_COMMON;
9348 break;
9349 case no_elf_stt_common:
9350 type = STT_OBJECT;
9351 break;
9352 }
9353 return type;
9354}
9355
c152c796
AM
9356/* Add an external symbol to the symbol table. This is called from
9357 the hash table traversal routine. When generating a shared object,
9358 we go through the symbol table twice. The first time we output
9359 anything that might have been forced to local scope in a version
9360 script. The second time we output the symbols that are still
9361 global symbols. */
9362
9363static bfd_boolean
7686d77d 9364elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9365{
7686d77d 9366 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9367 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9368 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9369 bfd_boolean strip;
9370 Elf_Internal_Sym sym;
9371 asection *input_sec;
9372 const struct elf_backend_data *bed;
6e0b88f1
AM
9373 long indx;
9374 int ret;
b8871f35 9375 unsigned int type;
c152c796
AM
9376
9377 if (h->root.type == bfd_link_hash_warning)
9378 {
9379 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9380 if (h->root.type == bfd_link_hash_new)
9381 return TRUE;
9382 }
9383
9384 /* Decide whether to output this symbol in this pass. */
9385 if (eoinfo->localsyms)
9386 {
4deb8f71 9387 if (!h->forced_local)
c152c796
AM
9388 return TRUE;
9389 }
9390 else
9391 {
4deb8f71 9392 if (h->forced_local)
c152c796
AM
9393 return TRUE;
9394 }
9395
8b127cbc 9396 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9397
12ac1cf5 9398 if (h->root.type == bfd_link_hash_undefined)
c152c796 9399 {
12ac1cf5
NC
9400 /* If we have an undefined symbol reference here then it must have
9401 come from a shared library that is being linked in. (Undefined
98da7939
L
9402 references in regular files have already been handled unless
9403 they are in unreferenced sections which are removed by garbage
9404 collection). */
12ac1cf5
NC
9405 bfd_boolean ignore_undef = FALSE;
9406
9407 /* Some symbols may be special in that the fact that they're
9408 undefined can be safely ignored - let backend determine that. */
9409 if (bed->elf_backend_ignore_undef_symbol)
9410 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9411
9412 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9413 if (!ignore_undef
12ac1cf5 9414 && h->ref_dynamic
8b127cbc
AM
9415 && (!h->ref_regular || flinfo->info->gc_sections)
9416 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9417 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9418 (*flinfo->info->callbacks->undefined_symbol)
9419 (flinfo->info, h->root.root.string,
9420 h->ref_regular ? NULL : h->root.u.undef.abfd,
9421 NULL, 0,
9422 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9423
9424 /* Strip a global symbol defined in a discarded section. */
9425 if (h->indx == -3)
9426 return TRUE;
c152c796
AM
9427 }
9428
9429 /* We should also warn if a forced local symbol is referenced from
9430 shared libraries. */
0e1862bb 9431 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9432 && h->forced_local
9433 && h->ref_dynamic
371a5866 9434 && h->def_regular
f5385ebf 9435 && !h->dynamic_def
ee659f1f 9436 && h->ref_dynamic_nonweak
8b127cbc 9437 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9438 {
17d078c5
AM
9439 bfd *def_bfd;
9440 const char *msg;
90c984fc
L
9441 struct elf_link_hash_entry *hi = h;
9442
9443 /* Check indirect symbol. */
9444 while (hi->root.type == bfd_link_hash_indirect)
9445 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9446
9447 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9448 /* xgettext:c-format */
17d078c5
AM
9449 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9450 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9451 /* xgettext:c-format */
17d078c5
AM
9452 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9453 else
695344c0 9454 /* xgettext:c-format */
17d078c5 9455 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9456 def_bfd = flinfo->output_bfd;
90c984fc
L
9457 if (hi->root.u.def.section != bfd_abs_section_ptr)
9458 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9459 _bfd_error_handler (msg, flinfo->output_bfd,
9460 h->root.root.string, def_bfd);
17d078c5 9461 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9462 eoinfo->failed = TRUE;
9463 return FALSE;
9464 }
9465
9466 /* We don't want to output symbols that have never been mentioned by
9467 a regular file, or that we have been told to strip. However, if
9468 h->indx is set to -2, the symbol is used by a reloc and we must
9469 output it. */
d983c8c5 9470 strip = FALSE;
c152c796 9471 if (h->indx == -2)
d983c8c5 9472 ;
f5385ebf 9473 else if ((h->def_dynamic
77cfaee6
AM
9474 || h->ref_dynamic
9475 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9476 && !h->def_regular
9477 && !h->ref_regular)
c152c796 9478 strip = TRUE;
8b127cbc 9479 else if (flinfo->info->strip == strip_all)
c152c796 9480 strip = TRUE;
8b127cbc
AM
9481 else if (flinfo->info->strip == strip_some
9482 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9483 h->root.root.string, FALSE, FALSE) == NULL)
9484 strip = TRUE;
d56d55e7
AM
9485 else if ((h->root.type == bfd_link_hash_defined
9486 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9487 && ((flinfo->info->strip_discarded
dbaa2011 9488 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9489 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9490 && h->root.u.def.section->owner != NULL
d56d55e7 9491 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9492 strip = TRUE;
9e2278f5
AM
9493 else if ((h->root.type == bfd_link_hash_undefined
9494 || h->root.type == bfd_link_hash_undefweak)
9495 && h->root.u.undef.abfd != NULL
9496 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9497 strip = TRUE;
c152c796 9498
b8871f35
L
9499 type = h->type;
9500
c152c796 9501 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9502 nothing else to do. However, if it is a forced local symbol or
9503 an ifunc symbol we need to give the backend finish_dynamic_symbol
9504 function a chance to make it dynamic. */
c152c796
AM
9505 if (strip
9506 && h->dynindx == -1
b8871f35 9507 && type != STT_GNU_IFUNC
f5385ebf 9508 && !h->forced_local)
c152c796
AM
9509 return TRUE;
9510
9511 sym.st_value = 0;
9512 sym.st_size = h->size;
9513 sym.st_other = h->other;
c152c796
AM
9514 switch (h->root.type)
9515 {
9516 default:
9517 case bfd_link_hash_new:
9518 case bfd_link_hash_warning:
9519 abort ();
9520 return FALSE;
9521
9522 case bfd_link_hash_undefined:
9523 case bfd_link_hash_undefweak:
9524 input_sec = bfd_und_section_ptr;
9525 sym.st_shndx = SHN_UNDEF;
9526 break;
9527
9528 case bfd_link_hash_defined:
9529 case bfd_link_hash_defweak:
9530 {
9531 input_sec = h->root.u.def.section;
9532 if (input_sec->output_section != NULL)
9533 {
9534 sym.st_shndx =
8b127cbc 9535 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9536 input_sec->output_section);
9537 if (sym.st_shndx == SHN_BAD)
9538 {
4eca0228 9539 _bfd_error_handler
695344c0 9540 /* xgettext:c-format */
d003868e 9541 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9542 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9543 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9544 eoinfo->failed = TRUE;
9545 return FALSE;
9546 }
9547
9548 /* ELF symbols in relocatable files are section relative,
9549 but in nonrelocatable files they are virtual
9550 addresses. */
9551 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9552 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9553 {
9554 sym.st_value += input_sec->output_section->vma;
9555 if (h->type == STT_TLS)
9556 {
8b127cbc 9557 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9558 if (tls_sec != NULL)
9559 sym.st_value -= tls_sec->vma;
c152c796
AM
9560 }
9561 }
9562 }
9563 else
9564 {
9565 BFD_ASSERT (input_sec->owner == NULL
9566 || (input_sec->owner->flags & DYNAMIC) != 0);
9567 sym.st_shndx = SHN_UNDEF;
9568 input_sec = bfd_und_section_ptr;
9569 }
9570 }
9571 break;
9572
9573 case bfd_link_hash_common:
9574 input_sec = h->root.u.c.p->section;
a4d8e49b 9575 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9576 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9577 break;
9578
9579 case bfd_link_hash_indirect:
9580 /* These symbols are created by symbol versioning. They point
9581 to the decorated version of the name. For example, if the
9582 symbol foo@@GNU_1.2 is the default, which should be used when
9583 foo is used with no version, then we add an indirect symbol
9584 foo which points to foo@@GNU_1.2. We ignore these symbols,
9585 since the indirected symbol is already in the hash table. */
9586 return TRUE;
9587 }
9588
b8871f35
L
9589 if (type == STT_COMMON || type == STT_OBJECT)
9590 switch (h->root.type)
9591 {
9592 case bfd_link_hash_common:
9593 type = elf_link_convert_common_type (flinfo->info, type);
9594 break;
9595 case bfd_link_hash_defined:
9596 case bfd_link_hash_defweak:
9597 if (bed->common_definition (&sym))
9598 type = elf_link_convert_common_type (flinfo->info, type);
9599 else
9600 type = STT_OBJECT;
9601 break;
9602 case bfd_link_hash_undefined:
9603 case bfd_link_hash_undefweak:
9604 break;
9605 default:
9606 abort ();
9607 }
9608
4deb8f71 9609 if (h->forced_local)
b8871f35
L
9610 {
9611 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9612 /* Turn off visibility on local symbol. */
9613 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9614 }
9615 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9616 else if (h->unique_global && h->def_regular)
9617 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9618 else if (h->root.type == bfd_link_hash_undefweak
9619 || h->root.type == bfd_link_hash_defweak)
9620 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9621 else
9622 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9623 sym.st_target_internal = h->target_internal;
9624
c152c796
AM
9625 /* Give the processor backend a chance to tweak the symbol value,
9626 and also to finish up anything that needs to be done for this
9627 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9628 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9629 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9630 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9631 && h->def_regular
0e1862bb 9632 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9633 || ((h->dynindx != -1
9634 || h->forced_local)
0e1862bb 9635 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9636 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9637 || h->root.type != bfd_link_hash_undefweak))
9638 || !h->forced_local)
8b127cbc 9639 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9640 {
9641 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9642 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9643 {
9644 eoinfo->failed = TRUE;
9645 return FALSE;
9646 }
9647 }
9648
9649 /* If we are marking the symbol as undefined, and there are no
9650 non-weak references to this symbol from a regular object, then
9651 mark the symbol as weak undefined; if there are non-weak
9652 references, mark the symbol as strong. We can't do this earlier,
9653 because it might not be marked as undefined until the
9654 finish_dynamic_symbol routine gets through with it. */
9655 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9656 && h->ref_regular
c152c796
AM
9657 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9658 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9659 {
9660 int bindtype;
b8871f35 9661 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9662
9663 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9664 if (type == STT_GNU_IFUNC)
9665 type = STT_FUNC;
c152c796 9666
f5385ebf 9667 if (h->ref_regular_nonweak)
c152c796
AM
9668 bindtype = STB_GLOBAL;
9669 else
9670 bindtype = STB_WEAK;
2955ec4c 9671 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9672 }
9673
bda987c2
CD
9674 /* If this is a symbol defined in a dynamic library, don't use the
9675 symbol size from the dynamic library. Relinking an executable
9676 against a new library may introduce gratuitous changes in the
9677 executable's symbols if we keep the size. */
9678 if (sym.st_shndx == SHN_UNDEF
9679 && !h->def_regular
9680 && h->def_dynamic)
9681 sym.st_size = 0;
9682
c152c796
AM
9683 /* If a non-weak symbol with non-default visibility is not defined
9684 locally, it is a fatal error. */
0e1862bb 9685 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9686 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9687 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9688 && h->root.type == bfd_link_hash_undefined
f5385ebf 9689 && !h->def_regular)
c152c796 9690 {
17d078c5
AM
9691 const char *msg;
9692
9693 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9694 /* xgettext:c-format */
17d078c5
AM
9695 msg = _("%B: protected symbol `%s' isn't defined");
9696 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9697 /* xgettext:c-format */
17d078c5
AM
9698 msg = _("%B: internal symbol `%s' isn't defined");
9699 else
695344c0 9700 /* xgettext:c-format */
17d078c5 9701 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9702 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9703 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9704 eoinfo->failed = TRUE;
9705 return FALSE;
9706 }
9707
9708 /* If this symbol should be put in the .dynsym section, then put it
9709 there now. We already know the symbol index. We also fill in
9710 the entry in the .hash section. */
cae1fbbb 9711 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9712 && h->dynindx != -1
8b127cbc 9713 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9714 {
c152c796
AM
9715 bfd_byte *esym;
9716
90c984fc
L
9717 /* Since there is no version information in the dynamic string,
9718 if there is no version info in symbol version section, we will
1659f720 9719 have a run-time problem if not linking executable, referenced
4deb8f71 9720 by shared library, or not bound locally. */
1659f720 9721 if (h->verinfo.verdef == NULL
0e1862bb 9722 && (!bfd_link_executable (flinfo->info)
1659f720
L
9723 || h->ref_dynamic
9724 || !h->def_regular))
90c984fc
L
9725 {
9726 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9727
9728 if (p && p [1] != '\0')
9729 {
4eca0228 9730 _bfd_error_handler
695344c0 9731 /* xgettext:c-format */
90c984fc
L
9732 (_("%B: No symbol version section for versioned symbol `%s'"),
9733 flinfo->output_bfd, h->root.root.string);
9734 eoinfo->failed = TRUE;
9735 return FALSE;
9736 }
9737 }
9738
c152c796 9739 sym.st_name = h->dynstr_index;
cae1fbbb
L
9740 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9741 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9742 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9743 {
9744 eoinfo->failed = TRUE;
9745 return FALSE;
9746 }
8b127cbc 9747 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9748
8b127cbc 9749 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9750 {
9751 size_t hash_entry_size;
9752 bfd_byte *bucketpos;
9753 bfd_vma chain;
41198d0c
L
9754 size_t bucketcount;
9755 size_t bucket;
9756
8b127cbc 9757 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9758 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9759
9760 hash_entry_size
8b127cbc
AM
9761 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9762 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9763 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9764 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9765 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9766 bucketpos);
9767 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9768 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9769 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9770 }
c152c796 9771
8b127cbc 9772 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9773 {
9774 Elf_Internal_Versym iversym;
9775 Elf_External_Versym *eversym;
9776
f5385ebf 9777 if (!h->def_regular)
c152c796 9778 {
7b20f099
AM
9779 if (h->verinfo.verdef == NULL
9780 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9781 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9782 iversym.vs_vers = 0;
9783 else
9784 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9785 }
9786 else
9787 {
9788 if (h->verinfo.vertree == NULL)
9789 iversym.vs_vers = 1;
9790 else
9791 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9792 if (flinfo->info->create_default_symver)
3e3b46e5 9793 iversym.vs_vers++;
c152c796
AM
9794 }
9795
422f1182 9796 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9797 defined locally. */
422f1182 9798 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9799 iversym.vs_vers |= VERSYM_HIDDEN;
9800
8b127cbc 9801 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9802 eversym += h->dynindx;
8b127cbc 9803 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9804 }
9805 }
9806
d983c8c5
AM
9807 /* If the symbol is undefined, and we didn't output it to .dynsym,
9808 strip it from .symtab too. Obviously we can't do this for
9809 relocatable output or when needed for --emit-relocs. */
9810 else if (input_sec == bfd_und_section_ptr
9811 && h->indx != -2
0e1862bb 9812 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9813 return TRUE;
9814 /* Also strip others that we couldn't earlier due to dynamic symbol
9815 processing. */
9816 if (strip)
9817 return TRUE;
9818 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9819 return TRUE;
9820
2ec55de3
AM
9821 /* Output a FILE symbol so that following locals are not associated
9822 with the wrong input file. We need one for forced local symbols
9823 if we've seen more than one FILE symbol or when we have exactly
9824 one FILE symbol but global symbols are present in a file other
9825 than the one with the FILE symbol. We also need one if linker
9826 defined symbols are present. In practice these conditions are
9827 always met, so just emit the FILE symbol unconditionally. */
9828 if (eoinfo->localsyms
9829 && !eoinfo->file_sym_done
9830 && eoinfo->flinfo->filesym_count != 0)
9831 {
9832 Elf_Internal_Sym fsym;
9833
9834 memset (&fsym, 0, sizeof (fsym));
9835 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9836 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9837 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9838 bfd_und_section_ptr, NULL))
2ec55de3
AM
9839 return FALSE;
9840
9841 eoinfo->file_sym_done = TRUE;
9842 }
9843
8b127cbc 9844 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9845 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9846 input_sec, h);
6e0b88f1 9847 if (ret == 0)
c152c796
AM
9848 {
9849 eoinfo->failed = TRUE;
9850 return FALSE;
9851 }
6e0b88f1
AM
9852 else if (ret == 1)
9853 h->indx = indx;
9854 else if (h->indx == -2)
9855 abort();
c152c796
AM
9856
9857 return TRUE;
9858}
9859
cdd3575c
AM
9860/* Return TRUE if special handling is done for relocs in SEC against
9861 symbols defined in discarded sections. */
9862
c152c796
AM
9863static bfd_boolean
9864elf_section_ignore_discarded_relocs (asection *sec)
9865{
9866 const struct elf_backend_data *bed;
9867
cdd3575c
AM
9868 switch (sec->sec_info_type)
9869 {
dbaa2011
AM
9870 case SEC_INFO_TYPE_STABS:
9871 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9872 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9873 return TRUE;
9874 default:
9875 break;
9876 }
c152c796
AM
9877
9878 bed = get_elf_backend_data (sec->owner);
9879 if (bed->elf_backend_ignore_discarded_relocs != NULL
9880 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9881 return TRUE;
9882
9883 return FALSE;
9884}
9885
9e66c942
AM
9886/* Return a mask saying how ld should treat relocations in SEC against
9887 symbols defined in discarded sections. If this function returns
9888 COMPLAIN set, ld will issue a warning message. If this function
9889 returns PRETEND set, and the discarded section was link-once and the
9890 same size as the kept link-once section, ld will pretend that the
9891 symbol was actually defined in the kept section. Otherwise ld will
9892 zero the reloc (at least that is the intent, but some cooperation by
9893 the target dependent code is needed, particularly for REL targets). */
9894
8a696751
AM
9895unsigned int
9896_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9897{
9e66c942 9898 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9899 return PRETEND;
cdd3575c
AM
9900
9901 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9902 return 0;
cdd3575c
AM
9903
9904 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9905 return 0;
cdd3575c 9906
9e66c942 9907 return COMPLAIN | PRETEND;
cdd3575c
AM
9908}
9909
3d7f7666
L
9910/* Find a match between a section and a member of a section group. */
9911
9912static asection *
c0f00686
L
9913match_group_member (asection *sec, asection *group,
9914 struct bfd_link_info *info)
3d7f7666
L
9915{
9916 asection *first = elf_next_in_group (group);
9917 asection *s = first;
9918
9919 while (s != NULL)
9920 {
c0f00686 9921 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9922 return s;
9923
83180ade 9924 s = elf_next_in_group (s);
3d7f7666
L
9925 if (s == first)
9926 break;
9927 }
9928
9929 return NULL;
9930}
9931
01b3c8ab 9932/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9933 to replace it. Return the replacement if it is OK. Otherwise return
9934 NULL. */
01b3c8ab
L
9935
9936asection *
c0f00686 9937_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9938{
9939 asection *kept;
9940
9941 kept = sec->kept_section;
9942 if (kept != NULL)
9943 {
c2370991 9944 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9945 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9946 if (kept != NULL
9947 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9948 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9949 kept = NULL;
c2370991 9950 sec->kept_section = kept;
01b3c8ab
L
9951 }
9952 return kept;
9953}
9954
c152c796
AM
9955/* Link an input file into the linker output file. This function
9956 handles all the sections and relocations of the input file at once.
9957 This is so that we only have to read the local symbols once, and
9958 don't have to keep them in memory. */
9959
9960static bfd_boolean
8b127cbc 9961elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9962{
ece5ef60 9963 int (*relocate_section)
c152c796
AM
9964 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9965 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9966 bfd *output_bfd;
9967 Elf_Internal_Shdr *symtab_hdr;
9968 size_t locsymcount;
9969 size_t extsymoff;
9970 Elf_Internal_Sym *isymbuf;
9971 Elf_Internal_Sym *isym;
9972 Elf_Internal_Sym *isymend;
9973 long *pindex;
9974 asection **ppsection;
9975 asection *o;
9976 const struct elf_backend_data *bed;
c152c796 9977 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9978 bfd_size_type address_size;
9979 bfd_vma r_type_mask;
9980 int r_sym_shift;
ffbc01cc 9981 bfd_boolean have_file_sym = FALSE;
c152c796 9982
8b127cbc 9983 output_bfd = flinfo->output_bfd;
c152c796
AM
9984 bed = get_elf_backend_data (output_bfd);
9985 relocate_section = bed->elf_backend_relocate_section;
9986
9987 /* If this is a dynamic object, we don't want to do anything here:
9988 we don't want the local symbols, and we don't want the section
9989 contents. */
9990 if ((input_bfd->flags & DYNAMIC) != 0)
9991 return TRUE;
9992
c152c796
AM
9993 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9994 if (elf_bad_symtab (input_bfd))
9995 {
9996 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9997 extsymoff = 0;
9998 }
9999 else
10000 {
10001 locsymcount = symtab_hdr->sh_info;
10002 extsymoff = symtab_hdr->sh_info;
10003 }
10004
10005 /* Read the local symbols. */
10006 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10007 if (isymbuf == NULL && locsymcount != 0)
10008 {
10009 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10010 flinfo->internal_syms,
10011 flinfo->external_syms,
10012 flinfo->locsym_shndx);
c152c796
AM
10013 if (isymbuf == NULL)
10014 return FALSE;
10015 }
10016
10017 /* Find local symbol sections and adjust values of symbols in
10018 SEC_MERGE sections. Write out those local symbols we know are
10019 going into the output file. */
10020 isymend = isymbuf + locsymcount;
8b127cbc 10021 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10022 isym < isymend;
10023 isym++, pindex++, ppsection++)
10024 {
10025 asection *isec;
10026 const char *name;
10027 Elf_Internal_Sym osym;
6e0b88f1
AM
10028 long indx;
10029 int ret;
c152c796
AM
10030
10031 *pindex = -1;
10032
10033 if (elf_bad_symtab (input_bfd))
10034 {
10035 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10036 {
10037 *ppsection = NULL;
10038 continue;
10039 }
10040 }
10041
10042 if (isym->st_shndx == SHN_UNDEF)
10043 isec = bfd_und_section_ptr;
c152c796
AM
10044 else if (isym->st_shndx == SHN_ABS)
10045 isec = bfd_abs_section_ptr;
10046 else if (isym->st_shndx == SHN_COMMON)
10047 isec = bfd_com_section_ptr;
10048 else
10049 {
cb33740c
AM
10050 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10051 if (isec == NULL)
10052 {
10053 /* Don't attempt to output symbols with st_shnx in the
10054 reserved range other than SHN_ABS and SHN_COMMON. */
10055 *ppsection = NULL;
10056 continue;
10057 }
dbaa2011 10058 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10059 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10060 isym->st_value =
10061 _bfd_merged_section_offset (output_bfd, &isec,
10062 elf_section_data (isec)->sec_info,
10063 isym->st_value);
c152c796
AM
10064 }
10065
10066 *ppsection = isec;
10067
d983c8c5
AM
10068 /* Don't output the first, undefined, symbol. In fact, don't
10069 output any undefined local symbol. */
10070 if (isec == bfd_und_section_ptr)
c152c796
AM
10071 continue;
10072
10073 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10074 {
10075 /* We never output section symbols. Instead, we use the
10076 section symbol of the corresponding section in the output
10077 file. */
10078 continue;
10079 }
10080
10081 /* If we are stripping all symbols, we don't want to output this
10082 one. */
8b127cbc 10083 if (flinfo->info->strip == strip_all)
c152c796
AM
10084 continue;
10085
10086 /* If we are discarding all local symbols, we don't want to
10087 output this one. If we are generating a relocatable output
10088 file, then some of the local symbols may be required by
10089 relocs; we output them below as we discover that they are
10090 needed. */
8b127cbc 10091 if (flinfo->info->discard == discard_all)
c152c796
AM
10092 continue;
10093
10094 /* If this symbol is defined in a section which we are
f02571c5
AM
10095 discarding, we don't need to keep it. */
10096 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10097 && isym->st_shndx < SHN_LORESERVE
10098 && bfd_section_removed_from_list (output_bfd,
10099 isec->output_section))
e75a280b
L
10100 continue;
10101
c152c796
AM
10102 /* Get the name of the symbol. */
10103 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10104 isym->st_name);
10105 if (name == NULL)
10106 return FALSE;
10107
10108 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10109 if ((flinfo->info->strip == strip_some
10110 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10111 == NULL))
8b127cbc 10112 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10113 && (isec->flags & SEC_MERGE)
10114 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10115 || flinfo->info->discard == discard_l)
c152c796
AM
10116 && bfd_is_local_label_name (input_bfd, name)))
10117 continue;
10118
ffbc01cc
AM
10119 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10120 {
ce875075
AM
10121 if (input_bfd->lto_output)
10122 /* -flto puts a temp file name here. This means builds
10123 are not reproducible. Discard the symbol. */
10124 continue;
ffbc01cc
AM
10125 have_file_sym = TRUE;
10126 flinfo->filesym_count += 1;
10127 }
10128 if (!have_file_sym)
10129 {
10130 /* In the absence of debug info, bfd_find_nearest_line uses
10131 FILE symbols to determine the source file for local
10132 function symbols. Provide a FILE symbol here if input
10133 files lack such, so that their symbols won't be
10134 associated with a previous input file. It's not the
10135 source file, but the best we can do. */
10136 have_file_sym = TRUE;
10137 flinfo->filesym_count += 1;
10138 memset (&osym, 0, sizeof (osym));
10139 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10140 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10141 if (!elf_link_output_symstrtab (flinfo,
10142 (input_bfd->lto_output ? NULL
10143 : input_bfd->filename),
10144 &osym, bfd_abs_section_ptr,
10145 NULL))
ffbc01cc
AM
10146 return FALSE;
10147 }
10148
c152c796
AM
10149 osym = *isym;
10150
10151 /* Adjust the section index for the output file. */
10152 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10153 isec->output_section);
10154 if (osym.st_shndx == SHN_BAD)
10155 return FALSE;
10156
c152c796
AM
10157 /* ELF symbols in relocatable files are section relative, but
10158 in executable files they are virtual addresses. Note that
10159 this code assumes that all ELF sections have an associated
10160 BFD section with a reasonable value for output_offset; below
10161 we assume that they also have a reasonable value for
10162 output_section. Any special sections must be set up to meet
10163 these requirements. */
10164 osym.st_value += isec->output_offset;
0e1862bb 10165 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10166 {
10167 osym.st_value += isec->output_section->vma;
10168 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10169 {
10170 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10171 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10172 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10173 }
10174 }
10175
6e0b88f1 10176 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10177 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10178 if (ret == 0)
c152c796 10179 return FALSE;
6e0b88f1
AM
10180 else if (ret == 1)
10181 *pindex = indx;
c152c796
AM
10182 }
10183
310fd250
L
10184 if (bed->s->arch_size == 32)
10185 {
10186 r_type_mask = 0xff;
10187 r_sym_shift = 8;
10188 address_size = 4;
10189 }
10190 else
10191 {
10192 r_type_mask = 0xffffffff;
10193 r_sym_shift = 32;
10194 address_size = 8;
10195 }
10196
c152c796
AM
10197 /* Relocate the contents of each section. */
10198 sym_hashes = elf_sym_hashes (input_bfd);
10199 for (o = input_bfd->sections; o != NULL; o = o->next)
10200 {
10201 bfd_byte *contents;
10202
10203 if (! o->linker_mark)
10204 {
10205 /* This section was omitted from the link. */
10206 continue;
10207 }
10208
0e1862bb 10209 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10210 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10211 {
10212 /* Deal with the group signature symbol. */
10213 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10214 unsigned long symndx = sec_data->this_hdr.sh_info;
10215 asection *osec = o->output_section;
10216
10217 if (symndx >= locsymcount
10218 || (elf_bad_symtab (input_bfd)
8b127cbc 10219 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10220 {
10221 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10222 while (h->root.type == bfd_link_hash_indirect
10223 || h->root.type == bfd_link_hash_warning)
10224 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10225 /* Arrange for symbol to be output. */
10226 h->indx = -2;
10227 elf_section_data (osec)->this_hdr.sh_info = -2;
10228 }
10229 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10230 {
10231 /* We'll use the output section target_index. */
8b127cbc 10232 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10233 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10234 }
10235 else
10236 {
8b127cbc 10237 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10238 {
10239 /* Otherwise output the local symbol now. */
10240 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10241 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10242 const char *name;
6e0b88f1
AM
10243 long indx;
10244 int ret;
bcacc0f5
AM
10245
10246 name = bfd_elf_string_from_elf_section (input_bfd,
10247 symtab_hdr->sh_link,
10248 sym.st_name);
10249 if (name == NULL)
10250 return FALSE;
10251
10252 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10253 sec);
10254 if (sym.st_shndx == SHN_BAD)
10255 return FALSE;
10256
10257 sym.st_value += o->output_offset;
10258
6e0b88f1 10259 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10260 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10261 NULL);
6e0b88f1 10262 if (ret == 0)
bcacc0f5 10263 return FALSE;
6e0b88f1 10264 else if (ret == 1)
8b127cbc 10265 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10266 else
10267 abort ();
bcacc0f5
AM
10268 }
10269 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10270 = flinfo->indices[symndx];
bcacc0f5
AM
10271 }
10272 }
10273
c152c796 10274 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10275 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10276 continue;
10277
10278 if ((o->flags & SEC_LINKER_CREATED) != 0)
10279 {
10280 /* Section was created by _bfd_elf_link_create_dynamic_sections
10281 or somesuch. */
10282 continue;
10283 }
10284
10285 /* Get the contents of the section. They have been cached by a
10286 relaxation routine. Note that o is a section in an input
10287 file, so the contents field will not have been set by any of
10288 the routines which work on output files. */
10289 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10290 {
10291 contents = elf_section_data (o)->this_hdr.contents;
10292 if (bed->caches_rawsize
10293 && o->rawsize != 0
10294 && o->rawsize < o->size)
10295 {
10296 memcpy (flinfo->contents, contents, o->rawsize);
10297 contents = flinfo->contents;
10298 }
10299 }
c152c796
AM
10300 else
10301 {
8b127cbc 10302 contents = flinfo->contents;
4a114e3e 10303 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10304 return FALSE;
10305 }
10306
10307 if ((o->flags & SEC_RELOC) != 0)
10308 {
10309 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10310 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10311 int action_discarded;
ece5ef60 10312 int ret;
c152c796
AM
10313
10314 /* Get the swapped relocs. */
10315 internal_relocs
8b127cbc
AM
10316 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10317 flinfo->internal_relocs, FALSE);
c152c796
AM
10318 if (internal_relocs == NULL
10319 && o->reloc_count > 0)
10320 return FALSE;
10321
310fd250
L
10322 /* We need to reverse-copy input .ctors/.dtors sections if
10323 they are placed in .init_array/.finit_array for output. */
10324 if (o->size > address_size
10325 && ((strncmp (o->name, ".ctors", 6) == 0
10326 && strcmp (o->output_section->name,
10327 ".init_array") == 0)
10328 || (strncmp (o->name, ".dtors", 6) == 0
10329 && strcmp (o->output_section->name,
10330 ".fini_array") == 0))
10331 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10332 {
310fd250
L
10333 if (o->size != o->reloc_count * address_size)
10334 {
4eca0228 10335 _bfd_error_handler
695344c0 10336 /* xgettext:c-format */
310fd250
L
10337 (_("error: %B: size of section %A is not "
10338 "multiple of address size"),
10339 input_bfd, o);
10340 bfd_set_error (bfd_error_on_input);
10341 return FALSE;
10342 }
10343 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10344 }
10345
0f02bbd9 10346 action_discarded = -1;
c152c796 10347 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10348 action_discarded = (*bed->action_discarded) (o);
10349
10350 /* Run through the relocs evaluating complex reloc symbols and
10351 looking for relocs against symbols from discarded sections
10352 or section symbols from removed link-once sections.
10353 Complain about relocs against discarded sections. Zero
10354 relocs against removed link-once sections. */
10355
10356 rel = internal_relocs;
10357 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10358 for ( ; rel < relend; rel++)
c152c796 10359 {
0f02bbd9
AM
10360 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10361 unsigned int s_type;
10362 asection **ps, *sec;
10363 struct elf_link_hash_entry *h = NULL;
10364 const char *sym_name;
c152c796 10365
0f02bbd9
AM
10366 if (r_symndx == STN_UNDEF)
10367 continue;
c152c796 10368
0f02bbd9
AM
10369 if (r_symndx >= locsymcount
10370 || (elf_bad_symtab (input_bfd)
8b127cbc 10371 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10372 {
10373 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10374
0f02bbd9
AM
10375 /* Badly formatted input files can contain relocs that
10376 reference non-existant symbols. Check here so that
10377 we do not seg fault. */
10378 if (h == NULL)
c152c796 10379 {
0f02bbd9 10380 char buffer [32];
dce669a1 10381
0f02bbd9 10382 sprintf_vma (buffer, rel->r_info);
4eca0228 10383 _bfd_error_handler
695344c0 10384 /* xgettext:c-format */
0f02bbd9
AM
10385 (_("error: %B contains a reloc (0x%s) for section %A "
10386 "that references a non-existent global symbol"),
c08bb8dd 10387 input_bfd, buffer, o);
0f02bbd9
AM
10388 bfd_set_error (bfd_error_bad_value);
10389 return FALSE;
10390 }
3b36f7e6 10391
0f02bbd9
AM
10392 while (h->root.type == bfd_link_hash_indirect
10393 || h->root.type == bfd_link_hash_warning)
10394 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10395
0f02bbd9 10396 s_type = h->type;
cdd3575c 10397
9e2dec47 10398 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10399 mark the symbol as undefined. Note that the
10400 linker may attach linker created dynamic sections
10401 to the plugin bfd. Symbols defined in linker
10402 created sections are not plugin symbols. */
9e2dec47
L
10403 if (h->root.non_ir_ref
10404 && (h->root.type == bfd_link_hash_defined
10405 || h->root.type == bfd_link_hash_defweak)
10406 && (h->root.u.def.section->flags
10407 & SEC_LINKER_CREATED) == 0
10408 && h->root.u.def.section->owner != NULL
10409 && (h->root.u.def.section->owner->flags
10410 & BFD_PLUGIN) != 0)
10411 {
10412 h->root.type = bfd_link_hash_undefined;
10413 h->root.u.undef.abfd = h->root.u.def.section->owner;
10414 }
10415
0f02bbd9
AM
10416 ps = NULL;
10417 if (h->root.type == bfd_link_hash_defined
10418 || h->root.type == bfd_link_hash_defweak)
10419 ps = &h->root.u.def.section;
10420
10421 sym_name = h->root.root.string;
10422 }
10423 else
10424 {
10425 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10426
10427 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10428 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10429 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10430 sym, *ps);
10431 }
c152c796 10432
c301e700 10433 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10434 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10435 {
10436 bfd_vma val;
10437 bfd_vma dot = (rel->r_offset
10438 + o->output_offset + o->output_section->vma);
10439#ifdef DEBUG
10440 printf ("Encountered a complex symbol!");
10441 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10442 input_bfd->filename, o->name,
10443 (long) (rel - internal_relocs));
0f02bbd9
AM
10444 printf (" symbol: idx %8.8lx, name %s\n",
10445 r_symndx, sym_name);
10446 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10447 (unsigned long) rel->r_info,
10448 (unsigned long) rel->r_offset);
10449#endif
8b127cbc 10450 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10451 isymbuf, locsymcount, s_type == STT_SRELC))
10452 return FALSE;
10453
10454 /* Symbol evaluated OK. Update to absolute value. */
10455 set_symbol_value (input_bfd, isymbuf, locsymcount,
10456 r_symndx, val);
10457 continue;
10458 }
10459
10460 if (action_discarded != -1 && ps != NULL)
10461 {
cdd3575c
AM
10462 /* Complain if the definition comes from a
10463 discarded section. */
dbaa2011 10464 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10465 {
cf35638d 10466 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10467 if (action_discarded & COMPLAIN)
8b127cbc 10468 (*flinfo->info->callbacks->einfo)
695344c0 10469 /* xgettext:c-format */
e1fffbe6 10470 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10471 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10472 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10473
87e5235d 10474 /* Try to do the best we can to support buggy old
e0ae6d6f 10475 versions of gcc. Pretend that the symbol is
87e5235d
AM
10476 really defined in the kept linkonce section.
10477 FIXME: This is quite broken. Modifying the
10478 symbol here means we will be changing all later
e0ae6d6f 10479 uses of the symbol, not just in this section. */
0f02bbd9 10480 if (action_discarded & PRETEND)
87e5235d 10481 {
01b3c8ab
L
10482 asection *kept;
10483
c0f00686 10484 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10485 flinfo->info);
01b3c8ab 10486 if (kept != NULL)
87e5235d
AM
10487 {
10488 *ps = kept;
10489 continue;
10490 }
10491 }
c152c796
AM
10492 }
10493 }
10494 }
10495
10496 /* Relocate the section by invoking a back end routine.
10497
10498 The back end routine is responsible for adjusting the
10499 section contents as necessary, and (if using Rela relocs
10500 and generating a relocatable output file) adjusting the
10501 reloc addend as necessary.
10502
10503 The back end routine does not have to worry about setting
10504 the reloc address or the reloc symbol index.
10505
10506 The back end routine is given a pointer to the swapped in
10507 internal symbols, and can access the hash table entries
10508 for the external symbols via elf_sym_hashes (input_bfd).
10509
10510 When generating relocatable output, the back end routine
10511 must handle STB_LOCAL/STT_SECTION symbols specially. The
10512 output symbol is going to be a section symbol
10513 corresponding to the output section, which will require
10514 the addend to be adjusted. */
10515
8b127cbc 10516 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10517 input_bfd, o, contents,
10518 internal_relocs,
10519 isymbuf,
8b127cbc 10520 flinfo->sections);
ece5ef60 10521 if (!ret)
c152c796
AM
10522 return FALSE;
10523
ece5ef60 10524 if (ret == 2
0e1862bb 10525 || bfd_link_relocatable (flinfo->info)
8b127cbc 10526 || flinfo->info->emitrelocations)
c152c796
AM
10527 {
10528 Elf_Internal_Rela *irela;
d4730f92 10529 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10530 bfd_vma last_offset;
10531 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10532 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10533 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10534 unsigned int next_erel;
c152c796 10535 bfd_boolean rela_normal;
d4730f92 10536 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10537
d4730f92
BS
10538 esdi = elf_section_data (o);
10539 esdo = elf_section_data (o->output_section);
10540 rela_normal = FALSE;
c152c796
AM
10541
10542 /* Adjust the reloc addresses and symbol indices. */
10543
10544 irela = internal_relocs;
10545 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10546 rel_hash = esdo->rel.hashes + esdo->rel.count;
10547 /* We start processing the REL relocs, if any. When we reach
10548 IRELAMID in the loop, we switch to the RELA relocs. */
10549 irelamid = irela;
10550 if (esdi->rel.hdr != NULL)
10551 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10552 * bed->s->int_rels_per_ext_rel);
eac338cf 10553 rel_hash_list = rel_hash;
d4730f92 10554 rela_hash_list = NULL;
c152c796 10555 last_offset = o->output_offset;
0e1862bb 10556 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10557 last_offset += o->output_section->vma;
10558 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10559 {
10560 unsigned long r_symndx;
10561 asection *sec;
10562 Elf_Internal_Sym sym;
10563
10564 if (next_erel == bed->s->int_rels_per_ext_rel)
10565 {
10566 rel_hash++;
10567 next_erel = 0;
10568 }
10569
d4730f92
BS
10570 if (irela == irelamid)
10571 {
10572 rel_hash = esdo->rela.hashes + esdo->rela.count;
10573 rela_hash_list = rel_hash;
10574 rela_normal = bed->rela_normal;
10575 }
10576
c152c796 10577 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10578 flinfo->info, o,
c152c796
AM
10579 irela->r_offset);
10580 if (irela->r_offset >= (bfd_vma) -2)
10581 {
10582 /* This is a reloc for a deleted entry or somesuch.
10583 Turn it into an R_*_NONE reloc, at the same
10584 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10585 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10586 being ordered. */
10587 irela->r_offset = last_offset;
10588 irela->r_info = 0;
10589 irela->r_addend = 0;
10590 continue;
10591 }
10592
10593 irela->r_offset += o->output_offset;
10594
10595 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10596 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10597 irela->r_offset += o->output_section->vma;
10598
10599 last_offset = irela->r_offset;
10600
10601 r_symndx = irela->r_info >> r_sym_shift;
10602 if (r_symndx == STN_UNDEF)
10603 continue;
10604
10605 if (r_symndx >= locsymcount
10606 || (elf_bad_symtab (input_bfd)
8b127cbc 10607 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10608 {
10609 struct elf_link_hash_entry *rh;
10610 unsigned long indx;
10611
10612 /* This is a reloc against a global symbol. We
10613 have not yet output all the local symbols, so
10614 we do not know the symbol index of any global
10615 symbol. We set the rel_hash entry for this
10616 reloc to point to the global hash table entry
10617 for this symbol. The symbol index is then
ee75fd95 10618 set at the end of bfd_elf_final_link. */
c152c796
AM
10619 indx = r_symndx - extsymoff;
10620 rh = elf_sym_hashes (input_bfd)[indx];
10621 while (rh->root.type == bfd_link_hash_indirect
10622 || rh->root.type == bfd_link_hash_warning)
10623 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10624
10625 /* Setting the index to -2 tells
10626 elf_link_output_extsym that this symbol is
10627 used by a reloc. */
10628 BFD_ASSERT (rh->indx < 0);
10629 rh->indx = -2;
10630
10631 *rel_hash = rh;
10632
10633 continue;
10634 }
10635
10636 /* This is a reloc against a local symbol. */
10637
10638 *rel_hash = NULL;
10639 sym = isymbuf[r_symndx];
8b127cbc 10640 sec = flinfo->sections[r_symndx];
c152c796
AM
10641 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10642 {
10643 /* I suppose the backend ought to fill in the
10644 section of any STT_SECTION symbol against a
6a8d1586 10645 processor specific section. */
cf35638d 10646 r_symndx = STN_UNDEF;
6a8d1586
AM
10647 if (bfd_is_abs_section (sec))
10648 ;
c152c796
AM
10649 else if (sec == NULL || sec->owner == NULL)
10650 {
10651 bfd_set_error (bfd_error_bad_value);
10652 return FALSE;
10653 }
10654 else
10655 {
6a8d1586
AM
10656 asection *osec = sec->output_section;
10657
10658 /* If we have discarded a section, the output
10659 section will be the absolute section. In
ab96bf03
AM
10660 case of discarded SEC_MERGE sections, use
10661 the kept section. relocate_section should
10662 have already handled discarded linkonce
10663 sections. */
6a8d1586
AM
10664 if (bfd_is_abs_section (osec)
10665 && sec->kept_section != NULL
10666 && sec->kept_section->output_section != NULL)
10667 {
10668 osec = sec->kept_section->output_section;
10669 irela->r_addend -= osec->vma;
10670 }
10671
10672 if (!bfd_is_abs_section (osec))
10673 {
10674 r_symndx = osec->target_index;
cf35638d 10675 if (r_symndx == STN_UNDEF)
74541ad4 10676 {
051d833a
AM
10677 irela->r_addend += osec->vma;
10678 osec = _bfd_nearby_section (output_bfd, osec,
10679 osec->vma);
10680 irela->r_addend -= osec->vma;
10681 r_symndx = osec->target_index;
74541ad4 10682 }
6a8d1586 10683 }
c152c796
AM
10684 }
10685
10686 /* Adjust the addend according to where the
10687 section winds up in the output section. */
10688 if (rela_normal)
10689 irela->r_addend += sec->output_offset;
10690 }
10691 else
10692 {
8b127cbc 10693 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10694 {
10695 unsigned long shlink;
10696 const char *name;
10697 asection *osec;
6e0b88f1 10698 long indx;
c152c796 10699
8b127cbc 10700 if (flinfo->info->strip == strip_all)
c152c796
AM
10701 {
10702 /* You can't do ld -r -s. */
10703 bfd_set_error (bfd_error_invalid_operation);
10704 return FALSE;
10705 }
10706
10707 /* This symbol was skipped earlier, but
10708 since it is needed by a reloc, we
10709 must output it now. */
10710 shlink = symtab_hdr->sh_link;
10711 name = (bfd_elf_string_from_elf_section
10712 (input_bfd, shlink, sym.st_name));
10713 if (name == NULL)
10714 return FALSE;
10715
10716 osec = sec->output_section;
10717 sym.st_shndx =
10718 _bfd_elf_section_from_bfd_section (output_bfd,
10719 osec);
10720 if (sym.st_shndx == SHN_BAD)
10721 return FALSE;
10722
10723 sym.st_value += sec->output_offset;
0e1862bb 10724 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10725 {
10726 sym.st_value += osec->vma;
10727 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10728 {
10729 /* STT_TLS symbols are relative to PT_TLS
10730 segment base. */
8b127cbc 10731 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10732 ->tls_sec != NULL);
8b127cbc 10733 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10734 ->tls_sec->vma);
10735 }
10736 }
10737
6e0b88f1 10738 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10739 ret = elf_link_output_symstrtab (flinfo, name,
10740 &sym, sec,
10741 NULL);
6e0b88f1 10742 if (ret == 0)
c152c796 10743 return FALSE;
6e0b88f1 10744 else if (ret == 1)
8b127cbc 10745 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10746 else
10747 abort ();
c152c796
AM
10748 }
10749
8b127cbc 10750 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10751 }
10752
10753 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10754 | (irela->r_info & r_type_mask));
10755 }
10756
10757 /* Swap out the relocs. */
d4730f92
BS
10758 input_rel_hdr = esdi->rel.hdr;
10759 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10760 {
d4730f92
BS
10761 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10762 input_rel_hdr,
10763 internal_relocs,
10764 rel_hash_list))
10765 return FALSE;
c152c796
AM
10766 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10767 * bed->s->int_rels_per_ext_rel);
eac338cf 10768 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10769 }
10770
10771 input_rela_hdr = esdi->rela.hdr;
10772 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10773 {
eac338cf 10774 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10775 input_rela_hdr,
eac338cf 10776 internal_relocs,
d4730f92 10777 rela_hash_list))
c152c796
AM
10778 return FALSE;
10779 }
10780 }
10781 }
10782
10783 /* Write out the modified section contents. */
10784 if (bed->elf_backend_write_section
8b127cbc 10785 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10786 contents))
c152c796
AM
10787 {
10788 /* Section written out. */
10789 }
10790 else switch (o->sec_info_type)
10791 {
dbaa2011 10792 case SEC_INFO_TYPE_STABS:
c152c796
AM
10793 if (! (_bfd_write_section_stabs
10794 (output_bfd,
8b127cbc 10795 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10796 o, &elf_section_data (o)->sec_info, contents)))
10797 return FALSE;
10798 break;
dbaa2011 10799 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10800 if (! _bfd_write_merged_section (output_bfd, o,
10801 elf_section_data (o)->sec_info))
10802 return FALSE;
10803 break;
dbaa2011 10804 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10805 {
8b127cbc 10806 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10807 o, contents))
10808 return FALSE;
10809 }
10810 break;
2f0c68f2
CM
10811 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10812 {
10813 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10814 flinfo->info,
10815 o, contents))
10816 return FALSE;
10817 }
10818 break;
c152c796
AM
10819 default:
10820 {
310fd250
L
10821 if (! (o->flags & SEC_EXCLUDE))
10822 {
10823 file_ptr offset = (file_ptr) o->output_offset;
10824 bfd_size_type todo = o->size;
37b01f6a
DG
10825
10826 offset *= bfd_octets_per_byte (output_bfd);
10827
310fd250
L
10828 if ((o->flags & SEC_ELF_REVERSE_COPY))
10829 {
10830 /* Reverse-copy input section to output. */
10831 do
10832 {
10833 todo -= address_size;
10834 if (! bfd_set_section_contents (output_bfd,
10835 o->output_section,
10836 contents + todo,
10837 offset,
10838 address_size))
10839 return FALSE;
10840 if (todo == 0)
10841 break;
10842 offset += address_size;
10843 }
10844 while (1);
10845 }
10846 else if (! bfd_set_section_contents (output_bfd,
10847 o->output_section,
10848 contents,
10849 offset, todo))
10850 return FALSE;
10851 }
c152c796
AM
10852 }
10853 break;
10854 }
10855 }
10856
10857 return TRUE;
10858}
10859
10860/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10861 requested by the linker, and does not come from any input file. This
c152c796
AM
10862 is used to build constructor and destructor tables when linking
10863 with -Ur. */
10864
10865static bfd_boolean
10866elf_reloc_link_order (bfd *output_bfd,
10867 struct bfd_link_info *info,
10868 asection *output_section,
10869 struct bfd_link_order *link_order)
10870{
10871 reloc_howto_type *howto;
10872 long indx;
10873 bfd_vma offset;
10874 bfd_vma addend;
d4730f92 10875 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10876 struct elf_link_hash_entry **rel_hash_ptr;
10877 Elf_Internal_Shdr *rel_hdr;
10878 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10879 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10880 bfd_byte *erel;
10881 unsigned int i;
d4730f92 10882 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10883
10884 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10885 if (howto == NULL)
10886 {
10887 bfd_set_error (bfd_error_bad_value);
10888 return FALSE;
10889 }
10890
10891 addend = link_order->u.reloc.p->addend;
10892
d4730f92
BS
10893 if (esdo->rel.hdr)
10894 reldata = &esdo->rel;
10895 else if (esdo->rela.hdr)
10896 reldata = &esdo->rela;
10897 else
10898 {
10899 reldata = NULL;
10900 BFD_ASSERT (0);
10901 }
10902
c152c796 10903 /* Figure out the symbol index. */
d4730f92 10904 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10905 if (link_order->type == bfd_section_reloc_link_order)
10906 {
10907 indx = link_order->u.reloc.p->u.section->target_index;
10908 BFD_ASSERT (indx != 0);
10909 *rel_hash_ptr = NULL;
10910 }
10911 else
10912 {
10913 struct elf_link_hash_entry *h;
10914
10915 /* Treat a reloc against a defined symbol as though it were
10916 actually against the section. */
10917 h = ((struct elf_link_hash_entry *)
10918 bfd_wrapped_link_hash_lookup (output_bfd, info,
10919 link_order->u.reloc.p->u.name,
10920 FALSE, FALSE, TRUE));
10921 if (h != NULL
10922 && (h->root.type == bfd_link_hash_defined
10923 || h->root.type == bfd_link_hash_defweak))
10924 {
10925 asection *section;
10926
10927 section = h->root.u.def.section;
10928 indx = section->output_section->target_index;
10929 *rel_hash_ptr = NULL;
10930 /* It seems that we ought to add the symbol value to the
10931 addend here, but in practice it has already been added
10932 because it was passed to constructor_callback. */
10933 addend += section->output_section->vma + section->output_offset;
10934 }
10935 else if (h != NULL)
10936 {
10937 /* Setting the index to -2 tells elf_link_output_extsym that
10938 this symbol is used by a reloc. */
10939 h->indx = -2;
10940 *rel_hash_ptr = h;
10941 indx = 0;
10942 }
10943 else
10944 {
1a72702b
AM
10945 (*info->callbacks->unattached_reloc)
10946 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10947 indx = 0;
10948 }
10949 }
10950
10951 /* If this is an inplace reloc, we must write the addend into the
10952 object file. */
10953 if (howto->partial_inplace && addend != 0)
10954 {
10955 bfd_size_type size;
10956 bfd_reloc_status_type rstat;
10957 bfd_byte *buf;
10958 bfd_boolean ok;
10959 const char *sym_name;
10960
a50b1753
NC
10961 size = (bfd_size_type) bfd_get_reloc_size (howto);
10962 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10963 if (buf == NULL && size != 0)
c152c796
AM
10964 return FALSE;
10965 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10966 switch (rstat)
10967 {
10968 case bfd_reloc_ok:
10969 break;
10970
10971 default:
10972 case bfd_reloc_outofrange:
10973 abort ();
10974
10975 case bfd_reloc_overflow:
10976 if (link_order->type == bfd_section_reloc_link_order)
10977 sym_name = bfd_section_name (output_bfd,
10978 link_order->u.reloc.p->u.section);
10979 else
10980 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10981 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10982 howto->name, addend, NULL, NULL,
10983 (bfd_vma) 0);
c152c796
AM
10984 break;
10985 }
37b01f6a 10986
c152c796 10987 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10988 link_order->offset
10989 * bfd_octets_per_byte (output_bfd),
10990 size);
c152c796
AM
10991 free (buf);
10992 if (! ok)
10993 return FALSE;
10994 }
10995
10996 /* The address of a reloc is relative to the section in a
10997 relocatable file, and is a virtual address in an executable
10998 file. */
10999 offset = link_order->offset;
0e1862bb 11000 if (! bfd_link_relocatable (info))
c152c796
AM
11001 offset += output_section->vma;
11002
11003 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11004 {
11005 irel[i].r_offset = offset;
11006 irel[i].r_info = 0;
11007 irel[i].r_addend = 0;
11008 }
11009 if (bed->s->arch_size == 32)
11010 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11011 else
11012 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11013
d4730f92 11014 rel_hdr = reldata->hdr;
c152c796
AM
11015 erel = rel_hdr->contents;
11016 if (rel_hdr->sh_type == SHT_REL)
11017 {
d4730f92 11018 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11019 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11020 }
11021 else
11022 {
11023 irel[0].r_addend = addend;
d4730f92 11024 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11025 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11026 }
11027
d4730f92 11028 ++reldata->count;
c152c796
AM
11029
11030 return TRUE;
11031}
11032
0b52efa6
PB
11033
11034/* Get the output vma of the section pointed to by the sh_link field. */
11035
11036static bfd_vma
11037elf_get_linked_section_vma (struct bfd_link_order *p)
11038{
11039 Elf_Internal_Shdr **elf_shdrp;
11040 asection *s;
11041 int elfsec;
11042
11043 s = p->u.indirect.section;
11044 elf_shdrp = elf_elfsections (s->owner);
11045 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11046 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11047 /* PR 290:
11048 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11049 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11050 sh_info fields. Hence we could get the situation
11051 where elfsec is 0. */
11052 if (elfsec == 0)
11053 {
11054 const struct elf_backend_data *bed
11055 = get_elf_backend_data (s->owner);
11056 if (bed->link_order_error_handler)
d003868e 11057 bed->link_order_error_handler
695344c0 11058 /* xgettext:c-format */
d003868e 11059 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11060 return 0;
11061 }
11062 else
11063 {
11064 s = elf_shdrp[elfsec]->bfd_section;
11065 return s->output_section->vma + s->output_offset;
11066 }
0b52efa6
PB
11067}
11068
11069
11070/* Compare two sections based on the locations of the sections they are
11071 linked to. Used by elf_fixup_link_order. */
11072
11073static int
11074compare_link_order (const void * a, const void * b)
11075{
11076 bfd_vma apos;
11077 bfd_vma bpos;
11078
11079 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11080 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11081 if (apos < bpos)
11082 return -1;
11083 return apos > bpos;
11084}
11085
11086
11087/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11088 order as their linked sections. Returns false if this could not be done
11089 because an output section includes both ordered and unordered
11090 sections. Ideally we'd do this in the linker proper. */
11091
11092static bfd_boolean
11093elf_fixup_link_order (bfd *abfd, asection *o)
11094{
11095 int seen_linkorder;
11096 int seen_other;
11097 int n;
11098 struct bfd_link_order *p;
11099 bfd *sub;
11100 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11101 unsigned elfsec;
0b52efa6 11102 struct bfd_link_order **sections;
d33cdfe3 11103 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11104 bfd_vma offset;
3b36f7e6 11105
d33cdfe3
L
11106 other_sec = NULL;
11107 linkorder_sec = NULL;
0b52efa6
PB
11108 seen_other = 0;
11109 seen_linkorder = 0;
8423293d 11110 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11111 {
d33cdfe3 11112 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11113 {
11114 s = p->u.indirect.section;
d33cdfe3
L
11115 sub = s->owner;
11116 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11117 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11118 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11119 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11120 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11121 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11122 {
11123 seen_linkorder++;
11124 linkorder_sec = s;
11125 }
0b52efa6 11126 else
d33cdfe3
L
11127 {
11128 seen_other++;
11129 other_sec = s;
11130 }
0b52efa6
PB
11131 }
11132 else
11133 seen_other++;
d33cdfe3
L
11134
11135 if (seen_other && seen_linkorder)
11136 {
11137 if (other_sec && linkorder_sec)
4eca0228 11138 _bfd_error_handler
695344c0 11139 /* xgettext:c-format */
4eca0228
AM
11140 (_("%A has both ordered [`%A' in %B] "
11141 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11142 o, linkorder_sec, linkorder_sec->owner,
11143 other_sec, other_sec->owner);
d33cdfe3 11144 else
4eca0228
AM
11145 _bfd_error_handler
11146 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11147 bfd_set_error (bfd_error_bad_value);
11148 return FALSE;
11149 }
0b52efa6
PB
11150 }
11151
11152 if (!seen_linkorder)
11153 return TRUE;
11154
0b52efa6 11155 sections = (struct bfd_link_order **)
14b1c01e
AM
11156 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11157 if (sections == NULL)
11158 return FALSE;
0b52efa6 11159 seen_linkorder = 0;
3b36f7e6 11160
8423293d 11161 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11162 {
11163 sections[seen_linkorder++] = p;
11164 }
11165 /* Sort the input sections in the order of their linked section. */
11166 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11167 compare_link_order);
11168
11169 /* Change the offsets of the sections. */
11170 offset = 0;
11171 for (n = 0; n < seen_linkorder; n++)
11172 {
11173 s = sections[n]->u.indirect.section;
461686a3 11174 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11175 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11176 sections[n]->offset = offset;
11177 offset += sections[n]->size;
11178 }
11179
4dd07732 11180 free (sections);
0b52efa6
PB
11181 return TRUE;
11182}
11183
76359541
TP
11184/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11185 Returns TRUE upon success, FALSE otherwise. */
11186
11187static bfd_boolean
11188elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11189{
11190 bfd_boolean ret = FALSE;
11191 bfd *implib_bfd;
11192 const struct elf_backend_data *bed;
11193 flagword flags;
11194 enum bfd_architecture arch;
11195 unsigned int mach;
11196 asymbol **sympp = NULL;
11197 long symsize;
11198 long symcount;
11199 long src_count;
11200 elf_symbol_type *osymbuf;
11201
11202 implib_bfd = info->out_implib_bfd;
11203 bed = get_elf_backend_data (abfd);
11204
11205 if (!bfd_set_format (implib_bfd, bfd_object))
11206 return FALSE;
11207
11208 flags = bfd_get_file_flags (abfd);
11209 flags &= ~HAS_RELOC;
11210 if (!bfd_set_start_address (implib_bfd, 0)
11211 || !bfd_set_file_flags (implib_bfd, flags))
11212 return FALSE;
11213
11214 /* Copy architecture of output file to import library file. */
11215 arch = bfd_get_arch (abfd);
11216 mach = bfd_get_mach (abfd);
11217 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11218 && (abfd->target_defaulted
11219 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11220 return FALSE;
11221
11222 /* Get symbol table size. */
11223 symsize = bfd_get_symtab_upper_bound (abfd);
11224 if (symsize < 0)
11225 return FALSE;
11226
11227 /* Read in the symbol table. */
11228 sympp = (asymbol **) xmalloc (symsize);
11229 symcount = bfd_canonicalize_symtab (abfd, sympp);
11230 if (symcount < 0)
11231 goto free_sym_buf;
11232
11233 /* Allow the BFD backend to copy any private header data it
11234 understands from the output BFD to the import library BFD. */
11235 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11236 goto free_sym_buf;
11237
11238 /* Filter symbols to appear in the import library. */
11239 if (bed->elf_backend_filter_implib_symbols)
11240 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11241 symcount);
11242 else
11243 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11244 if (symcount == 0)
11245 {
5df1bc57 11246 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11247 _bfd_error_handler (_("%B: no symbol found for import library"),
11248 implib_bfd);
76359541
TP
11249 goto free_sym_buf;
11250 }
11251
11252
11253 /* Make symbols absolute. */
11254 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11255 sizeof (*osymbuf));
11256 for (src_count = 0; src_count < symcount; src_count++)
11257 {
11258 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11259 sizeof (*osymbuf));
11260 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11261 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11262 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11263 osymbuf[src_count].internal_elf_sym.st_value =
11264 osymbuf[src_count].symbol.value;
11265 sympp[src_count] = &osymbuf[src_count].symbol;
11266 }
11267
11268 bfd_set_symtab (implib_bfd, sympp, symcount);
11269
11270 /* Allow the BFD backend to copy any private data it understands
11271 from the output BFD to the import library BFD. This is done last
11272 to permit the routine to look at the filtered symbol table. */
11273 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11274 goto free_sym_buf;
11275
11276 if (!bfd_close (implib_bfd))
11277 goto free_sym_buf;
11278
11279 ret = TRUE;
11280
11281free_sym_buf:
11282 free (sympp);
11283 return ret;
11284}
11285
9f7c3e5e
AM
11286static void
11287elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11288{
11289 asection *o;
11290
11291 if (flinfo->symstrtab != NULL)
ef10c3ac 11292 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11293 if (flinfo->contents != NULL)
11294 free (flinfo->contents);
11295 if (flinfo->external_relocs != NULL)
11296 free (flinfo->external_relocs);
11297 if (flinfo->internal_relocs != NULL)
11298 free (flinfo->internal_relocs);
11299 if (flinfo->external_syms != NULL)
11300 free (flinfo->external_syms);
11301 if (flinfo->locsym_shndx != NULL)
11302 free (flinfo->locsym_shndx);
11303 if (flinfo->internal_syms != NULL)
11304 free (flinfo->internal_syms);
11305 if (flinfo->indices != NULL)
11306 free (flinfo->indices);
11307 if (flinfo->sections != NULL)
11308 free (flinfo->sections);
9f7c3e5e
AM
11309 if (flinfo->symshndxbuf != NULL)
11310 free (flinfo->symshndxbuf);
11311 for (o = obfd->sections; o != NULL; o = o->next)
11312 {
11313 struct bfd_elf_section_data *esdo = elf_section_data (o);
11314 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11315 free (esdo->rel.hashes);
11316 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11317 free (esdo->rela.hashes);
11318 }
11319}
0b52efa6 11320
c152c796
AM
11321/* Do the final step of an ELF link. */
11322
11323bfd_boolean
11324bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11325{
11326 bfd_boolean dynamic;
11327 bfd_boolean emit_relocs;
11328 bfd *dynobj;
8b127cbc 11329 struct elf_final_link_info flinfo;
91d6fa6a
NC
11330 asection *o;
11331 struct bfd_link_order *p;
11332 bfd *sub;
c152c796
AM
11333 bfd_size_type max_contents_size;
11334 bfd_size_type max_external_reloc_size;
11335 bfd_size_type max_internal_reloc_count;
11336 bfd_size_type max_sym_count;
11337 bfd_size_type max_sym_shndx_count;
c152c796
AM
11338 Elf_Internal_Sym elfsym;
11339 unsigned int i;
11340 Elf_Internal_Shdr *symtab_hdr;
11341 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11342 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11343 struct elf_outext_info eoinfo;
11344 bfd_boolean merged;
11345 size_t relativecount = 0;
11346 asection *reldyn = 0;
11347 bfd_size_type amt;
104d59d1
JM
11348 asection *attr_section = NULL;
11349 bfd_vma attr_size = 0;
11350 const char *std_attrs_section;
64f52338 11351 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11352
64f52338 11353 if (!is_elf_hash_table (htab))
c152c796
AM
11354 return FALSE;
11355
0e1862bb 11356 if (bfd_link_pic (info))
c152c796
AM
11357 abfd->flags |= DYNAMIC;
11358
64f52338
AM
11359 dynamic = htab->dynamic_sections_created;
11360 dynobj = htab->dynobj;
c152c796 11361
0e1862bb 11362 emit_relocs = (bfd_link_relocatable (info)
a4676736 11363 || info->emitrelocations);
c152c796 11364
8b127cbc
AM
11365 flinfo.info = info;
11366 flinfo.output_bfd = abfd;
ef10c3ac 11367 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11368 if (flinfo.symstrtab == NULL)
c152c796
AM
11369 return FALSE;
11370
11371 if (! dynamic)
11372 {
8b127cbc
AM
11373 flinfo.hash_sec = NULL;
11374 flinfo.symver_sec = NULL;
c152c796
AM
11375 }
11376 else
11377 {
3d4d4302 11378 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11379 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11380 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11381 /* Note that it is OK if symver_sec is NULL. */
11382 }
11383
8b127cbc
AM
11384 flinfo.contents = NULL;
11385 flinfo.external_relocs = NULL;
11386 flinfo.internal_relocs = NULL;
11387 flinfo.external_syms = NULL;
11388 flinfo.locsym_shndx = NULL;
11389 flinfo.internal_syms = NULL;
11390 flinfo.indices = NULL;
11391 flinfo.sections = NULL;
8b127cbc 11392 flinfo.symshndxbuf = NULL;
ffbc01cc 11393 flinfo.filesym_count = 0;
c152c796 11394
104d59d1
JM
11395 /* The object attributes have been merged. Remove the input
11396 sections from the link, and set the contents of the output
11397 secton. */
11398 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11399 for (o = abfd->sections; o != NULL; o = o->next)
11400 {
11401 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11402 || strcmp (o->name, ".gnu.attributes") == 0)
11403 {
11404 for (p = o->map_head.link_order; p != NULL; p = p->next)
11405 {
11406 asection *input_section;
11407
11408 if (p->type != bfd_indirect_link_order)
11409 continue;
11410 input_section = p->u.indirect.section;
11411 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11412 elf_link_input_bfd ignores this section. */
11413 input_section->flags &= ~SEC_HAS_CONTENTS;
11414 }
a0c8462f 11415
104d59d1
JM
11416 attr_size = bfd_elf_obj_attr_size (abfd);
11417 if (attr_size)
11418 {
11419 bfd_set_section_size (abfd, o, attr_size);
11420 attr_section = o;
11421 /* Skip this section later on. */
11422 o->map_head.link_order = NULL;
11423 }
11424 else
11425 o->flags |= SEC_EXCLUDE;
11426 }
11427 }
11428
c152c796
AM
11429 /* Count up the number of relocations we will output for each output
11430 section, so that we know the sizes of the reloc sections. We
11431 also figure out some maximum sizes. */
11432 max_contents_size = 0;
11433 max_external_reloc_size = 0;
11434 max_internal_reloc_count = 0;
11435 max_sym_count = 0;
11436 max_sym_shndx_count = 0;
11437 merged = FALSE;
11438 for (o = abfd->sections; o != NULL; o = o->next)
11439 {
11440 struct bfd_elf_section_data *esdo = elf_section_data (o);
11441 o->reloc_count = 0;
11442
8423293d 11443 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11444 {
11445 unsigned int reloc_count = 0;
9eaff861 11446 unsigned int additional_reloc_count = 0;
c152c796 11447 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11448
11449 if (p->type == bfd_section_reloc_link_order
11450 || p->type == bfd_symbol_reloc_link_order)
11451 reloc_count = 1;
11452 else if (p->type == bfd_indirect_link_order)
11453 {
11454 asection *sec;
11455
11456 sec = p->u.indirect.section;
c152c796
AM
11457
11458 /* Mark all sections which are to be included in the
11459 link. This will normally be every section. We need
11460 to do this so that we can identify any sections which
11461 the linker has decided to not include. */
11462 sec->linker_mark = TRUE;
11463
11464 if (sec->flags & SEC_MERGE)
11465 merged = TRUE;
11466
eea6121a
AM
11467 if (sec->rawsize > max_contents_size)
11468 max_contents_size = sec->rawsize;
11469 if (sec->size > max_contents_size)
11470 max_contents_size = sec->size;
c152c796 11471
c152c796
AM
11472 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11473 && (sec->owner->flags & DYNAMIC) == 0)
11474 {
11475 size_t sym_count;
11476
a961cdd5
AM
11477 /* We are interested in just local symbols, not all
11478 symbols. */
c152c796
AM
11479 if (elf_bad_symtab (sec->owner))
11480 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11481 / bed->s->sizeof_sym);
11482 else
11483 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11484
11485 if (sym_count > max_sym_count)
11486 max_sym_count = sym_count;
11487
11488 if (sym_count > max_sym_shndx_count
6a40cf0c 11489 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11490 max_sym_shndx_count = sym_count;
11491
a961cdd5
AM
11492 if (esdo->this_hdr.sh_type == SHT_REL
11493 || esdo->this_hdr.sh_type == SHT_RELA)
11494 /* Some backends use reloc_count in relocation sections
11495 to count particular types of relocs. Of course,
11496 reloc sections themselves can't have relocations. */
11497 ;
11498 else if (emit_relocs)
11499 {
11500 reloc_count = sec->reloc_count;
11501 if (bed->elf_backend_count_additional_relocs)
11502 {
11503 int c;
11504 c = (*bed->elf_backend_count_additional_relocs) (sec);
11505 additional_reloc_count += c;
11506 }
11507 }
11508 else if (bed->elf_backend_count_relocs)
11509 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11510
11511 esdi = elf_section_data (sec);
11512
c152c796
AM
11513 if ((sec->flags & SEC_RELOC) != 0)
11514 {
d4730f92 11515 size_t ext_size = 0;
c152c796 11516
d4730f92
BS
11517 if (esdi->rel.hdr != NULL)
11518 ext_size = esdi->rel.hdr->sh_size;
11519 if (esdi->rela.hdr != NULL)
11520 ext_size += esdi->rela.hdr->sh_size;
7326c758 11521
c152c796
AM
11522 if (ext_size > max_external_reloc_size)
11523 max_external_reloc_size = ext_size;
11524 if (sec->reloc_count > max_internal_reloc_count)
11525 max_internal_reloc_count = sec->reloc_count;
11526 }
11527 }
11528 }
11529
11530 if (reloc_count == 0)
11531 continue;
11532
9eaff861 11533 reloc_count += additional_reloc_count;
c152c796
AM
11534 o->reloc_count += reloc_count;
11535
0e1862bb 11536 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11537 {
d4730f92 11538 if (esdi->rel.hdr)
9eaff861 11539 {
491d01d3 11540 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11541 esdo->rel.count += additional_reloc_count;
11542 }
d4730f92 11543 if (esdi->rela.hdr)
9eaff861 11544 {
491d01d3 11545 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11546 esdo->rela.count += additional_reloc_count;
11547 }
d4730f92
BS
11548 }
11549 else
11550 {
11551 if (o->use_rela_p)
11552 esdo->rela.count += reloc_count;
2c2b4ed4 11553 else
d4730f92 11554 esdo->rel.count += reloc_count;
c152c796 11555 }
c152c796
AM
11556 }
11557
9eaff861 11558 if (o->reloc_count > 0)
c152c796
AM
11559 o->flags |= SEC_RELOC;
11560 else
11561 {
11562 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11563 set it (this is probably a bug) and if it is set
11564 assign_section_numbers will create a reloc section. */
11565 o->flags &=~ SEC_RELOC;
11566 }
11567
11568 /* If the SEC_ALLOC flag is not set, force the section VMA to
11569 zero. This is done in elf_fake_sections as well, but forcing
11570 the VMA to 0 here will ensure that relocs against these
11571 sections are handled correctly. */
11572 if ((o->flags & SEC_ALLOC) == 0
11573 && ! o->user_set_vma)
11574 o->vma = 0;
11575 }
11576
0e1862bb 11577 if (! bfd_link_relocatable (info) && merged)
64f52338 11578 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11579
11580 /* Figure out the file positions for everything but the symbol table
11581 and the relocs. We set symcount to force assign_section_numbers
11582 to create a symbol table. */
8539e4e8 11583 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11584 BFD_ASSERT (! abfd->output_has_begun);
11585 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11586 goto error_return;
11587
ee75fd95 11588 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11589 for (o = abfd->sections; o != NULL; o = o->next)
11590 {
d4730f92 11591 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11592 if ((o->flags & SEC_RELOC) != 0)
11593 {
d4730f92 11594 if (esdo->rel.hdr
9eaff861 11595 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11596 goto error_return;
11597
d4730f92 11598 if (esdo->rela.hdr
9eaff861 11599 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11600 goto error_return;
11601 }
11602
11603 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11604 to count upwards while actually outputting the relocations. */
d4730f92
BS
11605 esdo->rel.count = 0;
11606 esdo->rela.count = 0;
0ce398f1
L
11607
11608 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11609 {
11610 /* Cache the section contents so that they can be compressed
11611 later. Use bfd_malloc since it will be freed by
11612 bfd_compress_section_contents. */
11613 unsigned char *contents = esdo->this_hdr.contents;
11614 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11615 abort ();
11616 contents
11617 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11618 if (contents == NULL)
11619 goto error_return;
11620 esdo->this_hdr.contents = contents;
11621 }
c152c796
AM
11622 }
11623
c152c796 11624 /* We have now assigned file positions for all the sections except
a485e98e
AM
11625 .symtab, .strtab, and non-loaded reloc sections. We start the
11626 .symtab section at the current file position, and write directly
11627 to it. We build the .strtab section in memory. */
c152c796
AM
11628 bfd_get_symcount (abfd) = 0;
11629 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11630 /* sh_name is set in prep_headers. */
11631 symtab_hdr->sh_type = SHT_SYMTAB;
11632 /* sh_flags, sh_addr and sh_size all start off zero. */
11633 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11634 /* sh_link is set in assign_section_numbers. */
11635 /* sh_info is set below. */
11636 /* sh_offset is set just below. */
72de5009 11637 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11638
ef10c3ac
L
11639 if (max_sym_count < 20)
11640 max_sym_count = 20;
64f52338 11641 htab->strtabsize = max_sym_count;
ef10c3ac 11642 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11643 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11644 if (htab->strtab == NULL)
c152c796 11645 goto error_return;
ef10c3ac
L
11646 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11647 flinfo.symshndxbuf
11648 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11649 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11650
8539e4e8 11651 if (info->strip != strip_all || emit_relocs)
c152c796 11652 {
8539e4e8
AM
11653 file_ptr off = elf_next_file_pos (abfd);
11654
11655 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11656
11657 /* Note that at this point elf_next_file_pos (abfd) is
11658 incorrect. We do not yet know the size of the .symtab section.
11659 We correct next_file_pos below, after we do know the size. */
11660
11661 /* Start writing out the symbol table. The first symbol is always a
11662 dummy symbol. */
c152c796
AM
11663 elfsym.st_value = 0;
11664 elfsym.st_size = 0;
11665 elfsym.st_info = 0;
11666 elfsym.st_other = 0;
11667 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11668 elfsym.st_target_internal = 0;
ef10c3ac
L
11669 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11670 bfd_und_section_ptr, NULL) != 1)
c152c796 11671 goto error_return;
c152c796 11672
8539e4e8
AM
11673 /* Output a symbol for each section. We output these even if we are
11674 discarding local symbols, since they are used for relocs. These
11675 symbols have no names. We store the index of each one in the
11676 index field of the section, so that we can find it again when
11677 outputting relocs. */
11678
c152c796
AM
11679 elfsym.st_size = 0;
11680 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11681 elfsym.st_other = 0;
f0b5bb34 11682 elfsym.st_value = 0;
35fc36a8 11683 elfsym.st_target_internal = 0;
c152c796
AM
11684 for (i = 1; i < elf_numsections (abfd); i++)
11685 {
11686 o = bfd_section_from_elf_index (abfd, i);
11687 if (o != NULL)
f0b5bb34
AM
11688 {
11689 o->target_index = bfd_get_symcount (abfd);
11690 elfsym.st_shndx = i;
0e1862bb 11691 if (!bfd_link_relocatable (info))
f0b5bb34 11692 elfsym.st_value = o->vma;
ef10c3ac
L
11693 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11694 NULL) != 1)
f0b5bb34
AM
11695 goto error_return;
11696 }
c152c796
AM
11697 }
11698 }
11699
11700 /* Allocate some memory to hold information read in from the input
11701 files. */
11702 if (max_contents_size != 0)
11703 {
8b127cbc
AM
11704 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11705 if (flinfo.contents == NULL)
c152c796
AM
11706 goto error_return;
11707 }
11708
11709 if (max_external_reloc_size != 0)
11710 {
8b127cbc
AM
11711 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11712 if (flinfo.external_relocs == NULL)
c152c796
AM
11713 goto error_return;
11714 }
11715
11716 if (max_internal_reloc_count != 0)
11717 {
11718 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11719 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11720 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11721 if (flinfo.internal_relocs == NULL)
c152c796
AM
11722 goto error_return;
11723 }
11724
11725 if (max_sym_count != 0)
11726 {
11727 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11728 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11729 if (flinfo.external_syms == NULL)
c152c796
AM
11730 goto error_return;
11731
11732 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11733 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11734 if (flinfo.internal_syms == NULL)
c152c796
AM
11735 goto error_return;
11736
11737 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11738 flinfo.indices = (long int *) bfd_malloc (amt);
11739 if (flinfo.indices == NULL)
c152c796
AM
11740 goto error_return;
11741
11742 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11743 flinfo.sections = (asection **) bfd_malloc (amt);
11744 if (flinfo.sections == NULL)
c152c796
AM
11745 goto error_return;
11746 }
11747
11748 if (max_sym_shndx_count != 0)
11749 {
11750 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11751 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11752 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11753 goto error_return;
11754 }
11755
64f52338 11756 if (htab->tls_sec)
c152c796
AM
11757 {
11758 bfd_vma base, end = 0;
11759 asection *sec;
11760
64f52338 11761 for (sec = htab->tls_sec;
c152c796
AM
11762 sec && (sec->flags & SEC_THREAD_LOCAL);
11763 sec = sec->next)
11764 {
3a800eb9 11765 bfd_size_type size = sec->size;
c152c796 11766
3a800eb9
AM
11767 if (size == 0
11768 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11769 {
91d6fa6a
NC
11770 struct bfd_link_order *ord = sec->map_tail.link_order;
11771
11772 if (ord != NULL)
11773 size = ord->offset + ord->size;
c152c796
AM
11774 }
11775 end = sec->vma + size;
11776 }
64f52338 11777 base = htab->tls_sec->vma;
7dc98aea
RO
11778 /* Only align end of TLS section if static TLS doesn't have special
11779 alignment requirements. */
11780 if (bed->static_tls_alignment == 1)
64f52338
AM
11781 end = align_power (end, htab->tls_sec->alignment_power);
11782 htab->tls_size = end - base;
c152c796
AM
11783 }
11784
0b52efa6
PB
11785 /* Reorder SHF_LINK_ORDER sections. */
11786 for (o = abfd->sections; o != NULL; o = o->next)
11787 {
11788 if (!elf_fixup_link_order (abfd, o))
11789 return FALSE;
11790 }
11791
2f0c68f2
CM
11792 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11793 return FALSE;
11794
c152c796
AM
11795 /* Since ELF permits relocations to be against local symbols, we
11796 must have the local symbols available when we do the relocations.
11797 Since we would rather only read the local symbols once, and we
11798 would rather not keep them in memory, we handle all the
11799 relocations for a single input file at the same time.
11800
11801 Unfortunately, there is no way to know the total number of local
11802 symbols until we have seen all of them, and the local symbol
11803 indices precede the global symbol indices. This means that when
11804 we are generating relocatable output, and we see a reloc against
11805 a global symbol, we can not know the symbol index until we have
11806 finished examining all the local symbols to see which ones we are
11807 going to output. To deal with this, we keep the relocations in
11808 memory, and don't output them until the end of the link. This is
11809 an unfortunate waste of memory, but I don't see a good way around
11810 it. Fortunately, it only happens when performing a relocatable
11811 link, which is not the common case. FIXME: If keep_memory is set
11812 we could write the relocs out and then read them again; I don't
11813 know how bad the memory loss will be. */
11814
c72f2fb2 11815 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11816 sub->output_has_begun = FALSE;
11817 for (o = abfd->sections; o != NULL; o = o->next)
11818 {
8423293d 11819 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11820 {
11821 if (p->type == bfd_indirect_link_order
11822 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11823 == bfd_target_elf_flavour)
11824 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11825 {
11826 if (! sub->output_has_begun)
11827 {
8b127cbc 11828 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11829 goto error_return;
11830 sub->output_has_begun = TRUE;
11831 }
11832 }
11833 else if (p->type == bfd_section_reloc_link_order
11834 || p->type == bfd_symbol_reloc_link_order)
11835 {
11836 if (! elf_reloc_link_order (abfd, info, o, p))
11837 goto error_return;
11838 }
11839 else
11840 {
11841 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11842 {
11843 if (p->type == bfd_indirect_link_order
11844 && (bfd_get_flavour (sub)
11845 == bfd_target_elf_flavour)
11846 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11847 != bed->s->elfclass))
11848 {
11849 const char *iclass, *oclass;
11850
aebf9be7 11851 switch (bed->s->elfclass)
351f65ca 11852 {
aebf9be7
NC
11853 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11854 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11855 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11856 default: abort ();
351f65ca 11857 }
aebf9be7
NC
11858
11859 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11860 {
aebf9be7
NC
11861 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11862 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11863 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11864 default: abort ();
351f65ca
L
11865 }
11866
11867 bfd_set_error (bfd_error_wrong_format);
4eca0228 11868 _bfd_error_handler
695344c0 11869 /* xgettext:c-format */
351f65ca
L
11870 (_("%B: file class %s incompatible with %s"),
11871 sub, iclass, oclass);
11872 }
11873
11874 goto error_return;
11875 }
c152c796
AM
11876 }
11877 }
11878 }
11879
c0f00686
L
11880 /* Free symbol buffer if needed. */
11881 if (!info->reduce_memory_overheads)
11882 {
c72f2fb2 11883 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11884 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11885 && elf_tdata (sub)->symbuf)
c0f00686
L
11886 {
11887 free (elf_tdata (sub)->symbuf);
11888 elf_tdata (sub)->symbuf = NULL;
11889 }
11890 }
11891
c152c796
AM
11892 /* Output any global symbols that got converted to local in a
11893 version script or due to symbol visibility. We do this in a
11894 separate step since ELF requires all local symbols to appear
11895 prior to any global symbols. FIXME: We should only do this if
11896 some global symbols were, in fact, converted to become local.
11897 FIXME: Will this work correctly with the Irix 5 linker? */
11898 eoinfo.failed = FALSE;
8b127cbc 11899 eoinfo.flinfo = &flinfo;
c152c796 11900 eoinfo.localsyms = TRUE;
34a79995 11901 eoinfo.file_sym_done = FALSE;
7686d77d 11902 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11903 if (eoinfo.failed)
11904 return FALSE;
11905
4e617b1e
PB
11906 /* If backend needs to output some local symbols not present in the hash
11907 table, do it now. */
8539e4e8
AM
11908 if (bed->elf_backend_output_arch_local_syms
11909 && (info->strip != strip_all || emit_relocs))
4e617b1e 11910 {
6e0b88f1 11911 typedef int (*out_sym_func)
4e617b1e
PB
11912 (void *, const char *, Elf_Internal_Sym *, asection *,
11913 struct elf_link_hash_entry *);
11914
11915 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11916 (abfd, info, &flinfo,
11917 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11918 return FALSE;
11919 }
11920
c152c796
AM
11921 /* That wrote out all the local symbols. Finish up the symbol table
11922 with the global symbols. Even if we want to strip everything we
11923 can, we still need to deal with those global symbols that got
11924 converted to local in a version script. */
11925
11926 /* The sh_info field records the index of the first non local symbol. */
11927 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11928
11929 if (dynamic
64f52338
AM
11930 && htab->dynsym != NULL
11931 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
11932 {
11933 Elf_Internal_Sym sym;
64f52338 11934 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 11935
64f52338
AM
11936 o = htab->dynsym->output_section;
11937 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
11938
11939 /* Write out the section symbols for the output sections. */
0e1862bb 11940 if (bfd_link_pic (info)
64f52338 11941 || htab->is_relocatable_executable)
c152c796
AM
11942 {
11943 asection *s;
11944
11945 sym.st_size = 0;
11946 sym.st_name = 0;
11947 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11948 sym.st_other = 0;
35fc36a8 11949 sym.st_target_internal = 0;
c152c796
AM
11950
11951 for (s = abfd->sections; s != NULL; s = s->next)
11952 {
11953 int indx;
11954 bfd_byte *dest;
11955 long dynindx;
11956
c152c796 11957 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11958 if (dynindx <= 0)
11959 continue;
11960 indx = elf_section_data (s)->this_idx;
c152c796
AM
11961 BFD_ASSERT (indx > 0);
11962 sym.st_shndx = indx;
c0d5a53d
L
11963 if (! check_dynsym (abfd, &sym))
11964 return FALSE;
c152c796
AM
11965 sym.st_value = s->vma;
11966 dest = dynsym + dynindx * bed->s->sizeof_sym;
11967 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11968 }
c152c796
AM
11969 }
11970
11971 /* Write out the local dynsyms. */
64f52338 11972 if (htab->dynlocal)
c152c796
AM
11973 {
11974 struct elf_link_local_dynamic_entry *e;
64f52338 11975 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
11976 {
11977 asection *s;
11978 bfd_byte *dest;
11979
935bd1e0 11980 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11981 Note that we saved a word of storage and overwrote
11982 the original st_name with the dynstr_index. */
11983 sym = e->isym;
935bd1e0 11984 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11985
cb33740c
AM
11986 s = bfd_section_from_elf_index (e->input_bfd,
11987 e->isym.st_shndx);
11988 if (s != NULL)
c152c796 11989 {
c152c796
AM
11990 sym.st_shndx =
11991 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11992 if (! check_dynsym (abfd, &sym))
11993 return FALSE;
c152c796
AM
11994 sym.st_value = (s->output_section->vma
11995 + s->output_offset
11996 + e->isym.st_value);
11997 }
11998
c152c796
AM
11999 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12000 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12001 }
12002 }
c152c796
AM
12003 }
12004
12005 /* We get the global symbols from the hash table. */
12006 eoinfo.failed = FALSE;
12007 eoinfo.localsyms = FALSE;
8b127cbc 12008 eoinfo.flinfo = &flinfo;
7686d77d 12009 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12010 if (eoinfo.failed)
12011 return FALSE;
12012
12013 /* If backend needs to output some symbols not present in the hash
12014 table, do it now. */
8539e4e8
AM
12015 if (bed->elf_backend_output_arch_syms
12016 && (info->strip != strip_all || emit_relocs))
c152c796 12017 {
6e0b88f1 12018 typedef int (*out_sym_func)
c152c796
AM
12019 (void *, const char *, Elf_Internal_Sym *, asection *,
12020 struct elf_link_hash_entry *);
12021
12022 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12023 (abfd, info, &flinfo,
12024 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12025 return FALSE;
12026 }
12027
ef10c3ac
L
12028 /* Finalize the .strtab section. */
12029 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12030
12031 /* Swap out the .strtab section. */
12032 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12033 return FALSE;
12034
12035 /* Now we know the size of the symtab section. */
c152c796
AM
12036 if (bfd_get_symcount (abfd) > 0)
12037 {
ee3b52e9
L
12038 /* Finish up and write out the symbol string table (.strtab)
12039 section. */
ad32986f 12040 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12041 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12042
ad32986f 12043 if (elf_symtab_shndx_list (abfd))
8539e4e8 12044 {
ad32986f 12045 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12046
ad32986f
NC
12047 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12048 {
12049 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12050 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12051 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12052 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12053 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12054
ad32986f
NC
12055 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12056 off, TRUE);
12057
12058 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12059 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12060 return FALSE;
12061 }
8539e4e8 12062 }
ee3b52e9
L
12063
12064 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12065 /* sh_name was set in prep_headers. */
12066 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12067 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12068 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12069 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12070 symstrtab_hdr->sh_entsize = 0;
12071 symstrtab_hdr->sh_link = 0;
12072 symstrtab_hdr->sh_info = 0;
12073 /* sh_offset is set just below. */
12074 symstrtab_hdr->sh_addralign = 1;
12075
12076 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12077 off, TRUE);
12078 elf_next_file_pos (abfd) = off;
12079
c152c796 12080 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12081 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12082 return FALSE;
12083 }
12084
76359541
TP
12085 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12086 {
4eca0228
AM
12087 _bfd_error_handler (_("%B: failed to generate import library"),
12088 info->out_implib_bfd);
76359541
TP
12089 return FALSE;
12090 }
12091
c152c796
AM
12092 /* Adjust the relocs to have the correct symbol indices. */
12093 for (o = abfd->sections; o != NULL; o = o->next)
12094 {
d4730f92 12095 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12096 bfd_boolean sort;
c152c796
AM
12097 if ((o->flags & SEC_RELOC) == 0)
12098 continue;
12099
28dbcedc 12100 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12101 if (esdo->rel.hdr != NULL
9eaff861 12102 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
12103 return FALSE;
12104 if (esdo->rela.hdr != NULL
9eaff861 12105 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 12106 return FALSE;
c152c796
AM
12107
12108 /* Set the reloc_count field to 0 to prevent write_relocs from
12109 trying to swap the relocs out itself. */
12110 o->reloc_count = 0;
12111 }
12112
12113 if (dynamic && info->combreloc && dynobj != NULL)
12114 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12115
12116 /* If we are linking against a dynamic object, or generating a
12117 shared library, finish up the dynamic linking information. */
12118 if (dynamic)
12119 {
12120 bfd_byte *dyncon, *dynconend;
12121
12122 /* Fix up .dynamic entries. */
3d4d4302 12123 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12124 BFD_ASSERT (o != NULL);
12125
12126 dyncon = o->contents;
eea6121a 12127 dynconend = o->contents + o->size;
c152c796
AM
12128 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12129 {
12130 Elf_Internal_Dyn dyn;
12131 const char *name;
12132 unsigned int type;
64487780
AM
12133 bfd_size_type sh_size;
12134 bfd_vma sh_addr;
c152c796
AM
12135
12136 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12137
12138 switch (dyn.d_tag)
12139 {
12140 default:
12141 continue;
12142 case DT_NULL:
12143 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12144 {
12145 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12146 {
12147 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12148 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12149 default: continue;
12150 }
12151 dyn.d_un.d_val = relativecount;
12152 relativecount = 0;
12153 break;
12154 }
12155 continue;
12156
12157 case DT_INIT:
12158 name = info->init_function;
12159 goto get_sym;
12160 case DT_FINI:
12161 name = info->fini_function;
12162 get_sym:
12163 {
12164 struct elf_link_hash_entry *h;
12165
64f52338 12166 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12167 if (h != NULL
12168 && (h->root.type == bfd_link_hash_defined
12169 || h->root.type == bfd_link_hash_defweak))
12170 {
bef26483 12171 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12172 o = h->root.u.def.section;
12173 if (o->output_section != NULL)
bef26483 12174 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12175 + o->output_offset);
12176 else
12177 {
12178 /* The symbol is imported from another shared
12179 library and does not apply to this one. */
bef26483 12180 dyn.d_un.d_ptr = 0;
c152c796
AM
12181 }
12182 break;
12183 }
12184 }
12185 continue;
12186
12187 case DT_PREINIT_ARRAYSZ:
12188 name = ".preinit_array";
4ade44b7 12189 goto get_out_size;
c152c796
AM
12190 case DT_INIT_ARRAYSZ:
12191 name = ".init_array";
4ade44b7 12192 goto get_out_size;
c152c796
AM
12193 case DT_FINI_ARRAYSZ:
12194 name = ".fini_array";
4ade44b7 12195 get_out_size:
c152c796
AM
12196 o = bfd_get_section_by_name (abfd, name);
12197 if (o == NULL)
12198 {
4eca0228 12199 _bfd_error_handler
4ade44b7 12200 (_("could not find section %s"), name);
c152c796
AM
12201 goto error_return;
12202 }
eea6121a 12203 if (o->size == 0)
4eca0228 12204 _bfd_error_handler
c152c796 12205 (_("warning: %s section has zero size"), name);
eea6121a 12206 dyn.d_un.d_val = o->size;
c152c796
AM
12207 break;
12208
12209 case DT_PREINIT_ARRAY:
12210 name = ".preinit_array";
4ade44b7 12211 goto get_out_vma;
c152c796
AM
12212 case DT_INIT_ARRAY:
12213 name = ".init_array";
4ade44b7 12214 goto get_out_vma;
c152c796
AM
12215 case DT_FINI_ARRAY:
12216 name = ".fini_array";
4ade44b7
AM
12217 get_out_vma:
12218 o = bfd_get_section_by_name (abfd, name);
12219 goto do_vma;
c152c796
AM
12220
12221 case DT_HASH:
12222 name = ".hash";
12223 goto get_vma;
fdc90cb4
JJ
12224 case DT_GNU_HASH:
12225 name = ".gnu.hash";
12226 goto get_vma;
c152c796
AM
12227 case DT_STRTAB:
12228 name = ".dynstr";
12229 goto get_vma;
12230 case DT_SYMTAB:
12231 name = ".dynsym";
12232 goto get_vma;
12233 case DT_VERDEF:
12234 name = ".gnu.version_d";
12235 goto get_vma;
12236 case DT_VERNEED:
12237 name = ".gnu.version_r";
12238 goto get_vma;
12239 case DT_VERSYM:
12240 name = ".gnu.version";
12241 get_vma:
4ade44b7
AM
12242 o = bfd_get_linker_section (dynobj, name);
12243 do_vma:
c152c796
AM
12244 if (o == NULL)
12245 {
4eca0228 12246 _bfd_error_handler
4ade44b7 12247 (_("could not find section %s"), name);
c152c796
AM
12248 goto error_return;
12249 }
894891db
NC
12250 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12251 {
4eca0228 12252 _bfd_error_handler
894891db
NC
12253 (_("warning: section '%s' is being made into a note"), name);
12254 bfd_set_error (bfd_error_nonrepresentable_section);
12255 goto error_return;
12256 }
4ade44b7 12257 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12258 break;
12259
12260 case DT_REL:
12261 case DT_RELA:
12262 case DT_RELSZ:
12263 case DT_RELASZ:
12264 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12265 type = SHT_REL;
12266 else
12267 type = SHT_RELA;
64487780
AM
12268 sh_size = 0;
12269 sh_addr = 0;
c152c796
AM
12270 for (i = 1; i < elf_numsections (abfd); i++)
12271 {
12272 Elf_Internal_Shdr *hdr;
12273
12274 hdr = elf_elfsections (abfd)[i];
12275 if (hdr->sh_type == type
12276 && (hdr->sh_flags & SHF_ALLOC) != 0)
12277 {
64487780
AM
12278 sh_size += hdr->sh_size;
12279 if (sh_addr == 0
12280 || sh_addr > hdr->sh_addr)
12281 sh_addr = hdr->sh_addr;
c152c796
AM
12282 }
12283 }
64487780 12284
64f52338
AM
12285 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12286 {
12287 /* Don't count procedure linkage table relocs in the
12288 overall reloc count. */
64487780
AM
12289 sh_size -= htab->srelplt->size;
12290 if (sh_size == 0)
12291 /* If the size is zero, make the address zero too.
12292 This is to avoid a glibc bug. If the backend
12293 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12294 zero, then we'll put DT_RELA at the end of
12295 DT_JMPREL. glibc will interpret the end of
12296 DT_RELA matching the end of DT_JMPREL as the
12297 case where DT_RELA includes DT_JMPREL, and for
12298 LD_BIND_NOW will decide that processing DT_RELA
12299 will process the PLT relocs too. Net result:
12300 No PLT relocs applied. */
12301 sh_addr = 0;
12302
64f52338
AM
12303 /* If .rela.plt is the first .rela section, exclude
12304 it from DT_RELA. */
64487780
AM
12305 else if (sh_addr == (htab->srelplt->output_section->vma
12306 + htab->srelplt->output_offset))
12307 sh_addr += htab->srelplt->size;
64f52338 12308 }
64487780
AM
12309
12310 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12311 dyn.d_un.d_val = sh_size;
12312 else
12313 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12314 break;
12315 }
12316 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12317 }
12318 }
12319
12320 /* If we have created any dynamic sections, then output them. */
12321 if (dynobj != NULL)
12322 {
12323 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12324 goto error_return;
12325
943284cc 12326 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12327 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12328 || info->error_textrel)
3d4d4302 12329 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12330 {
12331 bfd_byte *dyncon, *dynconend;
12332
943284cc
DJ
12333 dyncon = o->contents;
12334 dynconend = o->contents + o->size;
12335 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12336 {
12337 Elf_Internal_Dyn dyn;
12338
12339 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12340
12341 if (dyn.d_tag == DT_TEXTREL)
12342 {
c192a133
AM
12343 if (info->error_textrel)
12344 info->callbacks->einfo
12345 (_("%P%X: read-only segment has dynamic relocations.\n"));
12346 else
12347 info->callbacks->einfo
12348 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12349 break;
12350 }
12351 }
12352 }
12353
c152c796
AM
12354 for (o = dynobj->sections; o != NULL; o = o->next)
12355 {
12356 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12357 || o->size == 0
c152c796
AM
12358 || o->output_section == bfd_abs_section_ptr)
12359 continue;
12360 if ((o->flags & SEC_LINKER_CREATED) == 0)
12361 {
12362 /* At this point, we are only interested in sections
12363 created by _bfd_elf_link_create_dynamic_sections. */
12364 continue;
12365 }
64f52338 12366 if (htab->stab_info.stabstr == o)
3722b82f 12367 continue;
64f52338 12368 if (htab->eh_info.hdr_sec == o)
eea6121a 12369 continue;
3d4d4302 12370 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12371 {
12372 if (! bfd_set_section_contents (abfd, o->output_section,
12373 o->contents,
37b01f6a
DG
12374 (file_ptr) o->output_offset
12375 * bfd_octets_per_byte (abfd),
eea6121a 12376 o->size))
c152c796
AM
12377 goto error_return;
12378 }
12379 else
12380 {
12381 /* The contents of the .dynstr section are actually in a
12382 stringtab. */
8539e4e8
AM
12383 file_ptr off;
12384
c152c796
AM
12385 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12386 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12387 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12388 goto error_return;
12389 }
12390 }
12391 }
12392
0e1862bb 12393 if (bfd_link_relocatable (info))
c152c796
AM
12394 {
12395 bfd_boolean failed = FALSE;
12396
12397 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12398 if (failed)
12399 goto error_return;
12400 }
12401
12402 /* If we have optimized stabs strings, output them. */
64f52338 12403 if (htab->stab_info.stabstr != NULL)
c152c796 12404 {
64f52338 12405 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12406 goto error_return;
12407 }
12408
9f7c3e5e
AM
12409 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12410 goto error_return;
c152c796 12411
9f7c3e5e 12412 elf_final_link_free (abfd, &flinfo);
c152c796 12413
12bd6957 12414 elf_linker (abfd) = TRUE;
c152c796 12415
104d59d1
JM
12416 if (attr_section)
12417 {
a50b1753 12418 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12419 if (contents == NULL)
d0f16d5e 12420 return FALSE; /* Bail out and fail. */
104d59d1
JM
12421 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12422 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12423 free (contents);
12424 }
12425
c152c796
AM
12426 return TRUE;
12427
12428 error_return:
9f7c3e5e 12429 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12430 return FALSE;
12431}
12432\f
5241d853
RS
12433/* Initialize COOKIE for input bfd ABFD. */
12434
12435static bfd_boolean
12436init_reloc_cookie (struct elf_reloc_cookie *cookie,
12437 struct bfd_link_info *info, bfd *abfd)
12438{
12439 Elf_Internal_Shdr *symtab_hdr;
12440 const struct elf_backend_data *bed;
12441
12442 bed = get_elf_backend_data (abfd);
12443 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12444
12445 cookie->abfd = abfd;
12446 cookie->sym_hashes = elf_sym_hashes (abfd);
12447 cookie->bad_symtab = elf_bad_symtab (abfd);
12448 if (cookie->bad_symtab)
12449 {
12450 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12451 cookie->extsymoff = 0;
12452 }
12453 else
12454 {
12455 cookie->locsymcount = symtab_hdr->sh_info;
12456 cookie->extsymoff = symtab_hdr->sh_info;
12457 }
12458
12459 if (bed->s->arch_size == 32)
12460 cookie->r_sym_shift = 8;
12461 else
12462 cookie->r_sym_shift = 32;
12463
12464 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12465 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12466 {
12467 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12468 cookie->locsymcount, 0,
12469 NULL, NULL, NULL);
12470 if (cookie->locsyms == NULL)
12471 {
12472 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12473 return FALSE;
12474 }
12475 if (info->keep_memory)
12476 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12477 }
12478 return TRUE;
12479}
12480
12481/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12482
12483static void
12484fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12485{
12486 Elf_Internal_Shdr *symtab_hdr;
12487
12488 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12489 if (cookie->locsyms != NULL
12490 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12491 free (cookie->locsyms);
12492}
12493
12494/* Initialize the relocation information in COOKIE for input section SEC
12495 of input bfd ABFD. */
12496
12497static bfd_boolean
12498init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12499 struct bfd_link_info *info, bfd *abfd,
12500 asection *sec)
12501{
12502 const struct elf_backend_data *bed;
12503
12504 if (sec->reloc_count == 0)
12505 {
12506 cookie->rels = NULL;
12507 cookie->relend = NULL;
12508 }
12509 else
12510 {
12511 bed = get_elf_backend_data (abfd);
12512
12513 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12514 info->keep_memory);
12515 if (cookie->rels == NULL)
12516 return FALSE;
12517 cookie->rel = cookie->rels;
12518 cookie->relend = (cookie->rels
12519 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12520 }
12521 cookie->rel = cookie->rels;
12522 return TRUE;
12523}
12524
12525/* Free the memory allocated by init_reloc_cookie_rels,
12526 if appropriate. */
12527
12528static void
12529fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12530 asection *sec)
12531{
12532 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12533 free (cookie->rels);
12534}
12535
12536/* Initialize the whole of COOKIE for input section SEC. */
12537
12538static bfd_boolean
12539init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12540 struct bfd_link_info *info,
12541 asection *sec)
12542{
12543 if (!init_reloc_cookie (cookie, info, sec->owner))
12544 goto error1;
12545 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12546 goto error2;
12547 return TRUE;
12548
12549 error2:
12550 fini_reloc_cookie (cookie, sec->owner);
12551 error1:
12552 return FALSE;
12553}
12554
12555/* Free the memory allocated by init_reloc_cookie_for_section,
12556 if appropriate. */
12557
12558static void
12559fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12560 asection *sec)
12561{
12562 fini_reloc_cookie_rels (cookie, sec);
12563 fini_reloc_cookie (cookie, sec->owner);
12564}
12565\f
c152c796
AM
12566/* Garbage collect unused sections. */
12567
07adf181
AM
12568/* Default gc_mark_hook. */
12569
12570asection *
12571_bfd_elf_gc_mark_hook (asection *sec,
12572 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12573 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12574 struct elf_link_hash_entry *h,
12575 Elf_Internal_Sym *sym)
12576{
12577 if (h != NULL)
12578 {
12579 switch (h->root.type)
12580 {
12581 case bfd_link_hash_defined:
12582 case bfd_link_hash_defweak:
12583 return h->root.u.def.section;
12584
12585 case bfd_link_hash_common:
12586 return h->root.u.c.p->section;
12587
12588 default:
12589 break;
12590 }
12591 }
12592 else
12593 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12594
12595 return NULL;
12596}
12597
a6a4679f
AM
12598/* For undefined __start_<name> and __stop_<name> symbols, return the
12599 first input section matching <name>. Return NULL otherwise. */
12600
12601asection *
12602_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12603 struct elf_link_hash_entry *h)
12604{
12605 asection *s;
12606 const char *sec_name;
12607
12608 if (h->root.type != bfd_link_hash_undefined
12609 && h->root.type != bfd_link_hash_undefweak)
12610 return NULL;
12611
12612 s = h->root.u.undef.section;
12613 if (s != NULL)
12614 {
12615 if (s == (asection *) 0 - 1)
12616 return NULL;
12617 return s;
12618 }
12619
12620 sec_name = NULL;
12621 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12622 sec_name = h->root.root.string + 8;
12623 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12624 sec_name = h->root.root.string + 7;
12625
12626 if (sec_name != NULL && *sec_name != '\0')
12627 {
12628 bfd *i;
12629
12630 for (i = info->input_bfds; i != NULL; i = i->link.next)
12631 {
12632 s = bfd_get_section_by_name (i, sec_name);
12633 if (s != NULL)
12634 {
12635 h->root.u.undef.section = s;
12636 break;
12637 }
12638 }
12639 }
12640
12641 if (s == NULL)
12642 h->root.u.undef.section = (asection *) 0 - 1;
12643
12644 return s;
12645}
12646
5241d853
RS
12647/* COOKIE->rel describes a relocation against section SEC, which is
12648 a section we've decided to keep. Return the section that contains
12649 the relocation symbol, or NULL if no section contains it. */
12650
12651asection *
12652_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12653 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12654 struct elf_reloc_cookie *cookie,
12655 bfd_boolean *start_stop)
5241d853
RS
12656{
12657 unsigned long r_symndx;
12658 struct elf_link_hash_entry *h;
12659
12660 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12661 if (r_symndx == STN_UNDEF)
5241d853
RS
12662 return NULL;
12663
12664 if (r_symndx >= cookie->locsymcount
12665 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12666 {
12667 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12668 if (h == NULL)
12669 {
12670 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12671 sec->owner);
12672 return NULL;
12673 }
5241d853
RS
12674 while (h->root.type == bfd_link_hash_indirect
12675 || h->root.type == bfd_link_hash_warning)
12676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12677 h->mark = 1;
4e6b54a6
AM
12678 /* If this symbol is weak and there is a non-weak definition, we
12679 keep the non-weak definition because many backends put
12680 dynamic reloc info on the non-weak definition for code
12681 handling copy relocs. */
12682 if (h->u.weakdef != NULL)
12683 h->u.weakdef->mark = 1;
1cce69b9 12684
a6a4679f 12685 if (start_stop != NULL)
1cce69b9
AM
12686 {
12687 /* To work around a glibc bug, mark all XXX input sections
12688 when there is an as yet undefined reference to __start_XXX
12689 or __stop_XXX symbols. The linker will later define such
12690 symbols for orphan input sections that have a name
12691 representable as a C identifier. */
a6a4679f 12692 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12693
a6a4679f 12694 if (s != NULL)
1cce69b9 12695 {
a6a4679f
AM
12696 *start_stop = !s->gc_mark;
12697 return s;
1cce69b9
AM
12698 }
12699 }
12700
5241d853
RS
12701 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12702 }
12703
12704 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12705 &cookie->locsyms[r_symndx]);
12706}
12707
12708/* COOKIE->rel describes a relocation against section SEC, which is
12709 a section we've decided to keep. Mark the section that contains
9d0a14d3 12710 the relocation symbol. */
5241d853
RS
12711
12712bfd_boolean
12713_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12714 asection *sec,
12715 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12716 struct elf_reloc_cookie *cookie)
5241d853
RS
12717{
12718 asection *rsec;
1cce69b9 12719 bfd_boolean start_stop = FALSE;
5241d853 12720
1cce69b9
AM
12721 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12722 while (rsec != NULL)
5241d853 12723 {
1cce69b9
AM
12724 if (!rsec->gc_mark)
12725 {
12726 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12727 || (rsec->owner->flags & DYNAMIC) != 0)
12728 rsec->gc_mark = 1;
12729 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12730 return FALSE;
12731 }
12732 if (!start_stop)
12733 break;
199af150 12734 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12735 }
12736 return TRUE;
12737}
12738
07adf181
AM
12739/* The mark phase of garbage collection. For a given section, mark
12740 it and any sections in this section's group, and all the sections
12741 which define symbols to which it refers. */
12742
ccfa59ea
AM
12743bfd_boolean
12744_bfd_elf_gc_mark (struct bfd_link_info *info,
12745 asection *sec,
6a5bb875 12746 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12747{
12748 bfd_boolean ret;
9d0a14d3 12749 asection *group_sec, *eh_frame;
c152c796
AM
12750
12751 sec->gc_mark = 1;
12752
12753 /* Mark all the sections in the group. */
12754 group_sec = elf_section_data (sec)->next_in_group;
12755 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12756 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12757 return FALSE;
12758
12759 /* Look through the section relocs. */
12760 ret = TRUE;
9d0a14d3
RS
12761 eh_frame = elf_eh_frame_section (sec->owner);
12762 if ((sec->flags & SEC_RELOC) != 0
12763 && sec->reloc_count > 0
12764 && sec != eh_frame)
c152c796 12765 {
5241d853 12766 struct elf_reloc_cookie cookie;
c152c796 12767
5241d853
RS
12768 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12769 ret = FALSE;
c152c796 12770 else
c152c796 12771 {
5241d853 12772 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12773 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12774 {
12775 ret = FALSE;
12776 break;
12777 }
12778 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12779 }
12780 }
9d0a14d3
RS
12781
12782 if (ret && eh_frame && elf_fde_list (sec))
12783 {
12784 struct elf_reloc_cookie cookie;
12785
12786 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12787 ret = FALSE;
12788 else
12789 {
12790 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12791 gc_mark_hook, &cookie))
12792 ret = FALSE;
12793 fini_reloc_cookie_for_section (&cookie, eh_frame);
12794 }
12795 }
12796
2f0c68f2
CM
12797 eh_frame = elf_section_eh_frame_entry (sec);
12798 if (ret && eh_frame && !eh_frame->gc_mark)
12799 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12800 ret = FALSE;
12801
c152c796
AM
12802 return ret;
12803}
12804
3c758495
TG
12805/* Scan and mark sections in a special or debug section group. */
12806
12807static void
12808_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12809{
12810 /* Point to first section of section group. */
12811 asection *ssec;
12812 /* Used to iterate the section group. */
12813 asection *msec;
12814
12815 bfd_boolean is_special_grp = TRUE;
12816 bfd_boolean is_debug_grp = TRUE;
12817
12818 /* First scan to see if group contains any section other than debug
12819 and special section. */
12820 ssec = msec = elf_next_in_group (grp);
12821 do
12822 {
12823 if ((msec->flags & SEC_DEBUGGING) == 0)
12824 is_debug_grp = FALSE;
12825
12826 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12827 is_special_grp = FALSE;
12828
12829 msec = elf_next_in_group (msec);
12830 }
12831 while (msec != ssec);
12832
12833 /* If this is a pure debug section group or pure special section group,
12834 keep all sections in this group. */
12835 if (is_debug_grp || is_special_grp)
12836 {
12837 do
12838 {
12839 msec->gc_mark = 1;
12840 msec = elf_next_in_group (msec);
12841 }
12842 while (msec != ssec);
12843 }
12844}
12845
7f6ab9f8
AM
12846/* Keep debug and special sections. */
12847
12848bfd_boolean
12849_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12850 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12851{
12852 bfd *ibfd;
12853
c72f2fb2 12854 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12855 {
12856 asection *isec;
12857 bfd_boolean some_kept;
b40bf0a2 12858 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12859
12860 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12861 continue;
12862
b40bf0a2
NC
12863 /* Ensure all linker created sections are kept,
12864 see if any other section is already marked,
12865 and note if we have any fragmented debug sections. */
12866 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12867 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12868 {
12869 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12870 isec->gc_mark = 1;
12871 else if (isec->gc_mark)
12872 some_kept = TRUE;
b40bf0a2
NC
12873
12874 if (debug_frag_seen == FALSE
12875 && (isec->flags & SEC_DEBUGGING)
12876 && CONST_STRNEQ (isec->name, ".debug_line."))
12877 debug_frag_seen = TRUE;
7f6ab9f8
AM
12878 }
12879
12880 /* If no section in this file will be kept, then we can
b40bf0a2 12881 toss out the debug and special sections. */
7f6ab9f8
AM
12882 if (!some_kept)
12883 continue;
12884
12885 /* Keep debug and special sections like .comment when they are
3c758495
TG
12886 not part of a group. Also keep section groups that contain
12887 just debug sections or special sections. */
7f6ab9f8 12888 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12889 {
12890 if ((isec->flags & SEC_GROUP) != 0)
12891 _bfd_elf_gc_mark_debug_special_section_group (isec);
12892 else if (((isec->flags & SEC_DEBUGGING) != 0
12893 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12894 && elf_next_in_group (isec) == NULL)
12895 isec->gc_mark = 1;
12896 }
b40bf0a2
NC
12897
12898 if (! debug_frag_seen)
12899 continue;
12900
12901 /* Look for CODE sections which are going to be discarded,
12902 and find and discard any fragmented debug sections which
12903 are associated with that code section. */
12904 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12905 if ((isec->flags & SEC_CODE) != 0
12906 && isec->gc_mark == 0)
12907 {
12908 unsigned int ilen;
12909 asection *dsec;
12910
12911 ilen = strlen (isec->name);
12912
12913 /* Association is determined by the name of the debug section
12914 containing the name of the code section as a suffix. For
12915 example .debug_line.text.foo is a debug section associated
12916 with .text.foo. */
12917 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12918 {
12919 unsigned int dlen;
12920
12921 if (dsec->gc_mark == 0
12922 || (dsec->flags & SEC_DEBUGGING) == 0)
12923 continue;
12924
12925 dlen = strlen (dsec->name);
12926
12927 if (dlen > ilen
12928 && strncmp (dsec->name + (dlen - ilen),
12929 isec->name, ilen) == 0)
12930 {
12931 dsec->gc_mark = 0;
b40bf0a2
NC
12932 }
12933 }
12934 }
7f6ab9f8
AM
12935 }
12936 return TRUE;
12937}
12938
c152c796
AM
12939/* The sweep phase of garbage collection. Remove all garbage sections. */
12940
12941typedef bfd_boolean (*gc_sweep_hook_fn)
12942 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12943
12944static bfd_boolean
ccabcbe5 12945elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12946{
12947 bfd *sub;
ccabcbe5
AM
12948 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12949 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 12950
c72f2fb2 12951 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12952 {
12953 asection *o;
12954
b19a8f85
L
12955 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12956 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12957 continue;
12958
12959 for (o = sub->sections; o != NULL; o = o->next)
12960 {
a33dafc3
L
12961 /* When any section in a section group is kept, we keep all
12962 sections in the section group. If the first member of
12963 the section group is excluded, we will also exclude the
12964 group section. */
12965 if (o->flags & SEC_GROUP)
12966 {
12967 asection *first = elf_next_in_group (o);
12968 o->gc_mark = first->gc_mark;
12969 }
c152c796 12970
1e7eae0d 12971 if (o->gc_mark)
c152c796
AM
12972 continue;
12973
12974 /* Skip sweeping sections already excluded. */
12975 if (o->flags & SEC_EXCLUDE)
12976 continue;
12977
12978 /* Since this is early in the link process, it is simple
12979 to remove a section from the output. */
12980 o->flags |= SEC_EXCLUDE;
12981
c55fe096 12982 if (info->print_gc_sections && o->size != 0)
695344c0 12983 /* xgettext:c-format */
c08bb8dd
AM
12984 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
12985 o, sub);
c17d87de 12986
c152c796
AM
12987 /* But we also have to update some of the relocation
12988 info we collected before. */
12989 if (gc_sweep_hook
e8aaee2a 12990 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12991 && o->reloc_count != 0
12992 && !((info->strip == strip_all || info->strip == strip_debugger)
12993 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12994 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12995 {
12996 Elf_Internal_Rela *internal_relocs;
12997 bfd_boolean r;
12998
12999 internal_relocs
13000 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13001 info->keep_memory);
13002 if (internal_relocs == NULL)
13003 return FALSE;
13004
13005 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13006
13007 if (elf_section_data (o)->relocs != internal_relocs)
13008 free (internal_relocs);
13009
13010 if (!r)
13011 return FALSE;
13012 }
13013 }
13014 }
13015
c152c796
AM
13016 return TRUE;
13017}
13018
13019/* Propagate collected vtable information. This is called through
13020 elf_link_hash_traverse. */
13021
13022static bfd_boolean
13023elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13024{
c152c796 13025 /* Those that are not vtables. */
f6e332e6 13026 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13027 return TRUE;
13028
13029 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 13030 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13031 return TRUE;
13032
13033 /* If we've already been done, exit. */
f6e332e6 13034 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
13035 return TRUE;
13036
13037 /* Make sure the parent's table is up to date. */
f6e332e6 13038 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 13039
f6e332e6 13040 if (h->vtable->used == NULL)
c152c796
AM
13041 {
13042 /* None of this table's entries were referenced. Re-use the
13043 parent's table. */
f6e332e6
AM
13044 h->vtable->used = h->vtable->parent->vtable->used;
13045 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
13046 }
13047 else
13048 {
13049 size_t n;
13050 bfd_boolean *cu, *pu;
13051
13052 /* Or the parent's entries into ours. */
f6e332e6 13053 cu = h->vtable->used;
c152c796 13054 cu[-1] = TRUE;
f6e332e6 13055 pu = h->vtable->parent->vtable->used;
c152c796
AM
13056 if (pu != NULL)
13057 {
13058 const struct elf_backend_data *bed;
13059 unsigned int log_file_align;
13060
13061 bed = get_elf_backend_data (h->root.u.def.section->owner);
13062 log_file_align = bed->s->log_file_align;
f6e332e6 13063 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
13064 while (n--)
13065 {
13066 if (*pu)
13067 *cu = TRUE;
13068 pu++;
13069 cu++;
13070 }
13071 }
13072 }
13073
13074 return TRUE;
13075}
13076
13077static bfd_boolean
13078elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13079{
13080 asection *sec;
13081 bfd_vma hstart, hend;
13082 Elf_Internal_Rela *relstart, *relend, *rel;
13083 const struct elf_backend_data *bed;
13084 unsigned int log_file_align;
13085
c152c796
AM
13086 /* Take care of both those symbols that do not describe vtables as
13087 well as those that are not loaded. */
f6e332e6 13088 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13089 return TRUE;
13090
13091 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13092 || h->root.type == bfd_link_hash_defweak);
13093
13094 sec = h->root.u.def.section;
13095 hstart = h->root.u.def.value;
13096 hend = hstart + h->size;
13097
13098 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13099 if (!relstart)
13100 return *(bfd_boolean *) okp = FALSE;
13101 bed = get_elf_backend_data (sec->owner);
13102 log_file_align = bed->s->log_file_align;
13103
13104 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13105
13106 for (rel = relstart; rel < relend; ++rel)
13107 if (rel->r_offset >= hstart && rel->r_offset < hend)
13108 {
13109 /* If the entry is in use, do nothing. */
f6e332e6
AM
13110 if (h->vtable->used
13111 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13112 {
13113 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13114 if (h->vtable->used[entry])
c152c796
AM
13115 continue;
13116 }
13117 /* Otherwise, kill it. */
13118 rel->r_offset = rel->r_info = rel->r_addend = 0;
13119 }
13120
13121 return TRUE;
13122}
13123
87538722
AM
13124/* Mark sections containing dynamically referenced symbols. When
13125 building shared libraries, we must assume that any visible symbol is
13126 referenced. */
715df9b8 13127
64d03ab5
AM
13128bfd_boolean
13129bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13130{
87538722 13131 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13132 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13133
715df9b8
EB
13134 if ((h->root.type == bfd_link_hash_defined
13135 || h->root.type == bfd_link_hash_defweak)
87538722 13136 && (h->ref_dynamic
c4621b33 13137 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13138 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13139 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13140 && (!bfd_link_executable (info)
22185505 13141 || info->gc_keep_exported
b407645f
AM
13142 || info->export_dynamic
13143 || (h->dynamic
13144 && d != NULL
13145 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13146 && (h->versioned >= versioned
54e8959c
L
13147 || !bfd_hide_sym_by_version (info->version_info,
13148 h->root.root.string)))))
715df9b8
EB
13149 h->root.u.def.section->flags |= SEC_KEEP;
13150
13151 return TRUE;
13152}
3b36f7e6 13153
74f0fb50
AM
13154/* Keep all sections containing symbols undefined on the command-line,
13155 and the section containing the entry symbol. */
13156
13157void
13158_bfd_elf_gc_keep (struct bfd_link_info *info)
13159{
13160 struct bfd_sym_chain *sym;
13161
13162 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13163 {
13164 struct elf_link_hash_entry *h;
13165
13166 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13167 FALSE, FALSE, FALSE);
13168
13169 if (h != NULL
13170 && (h->root.type == bfd_link_hash_defined
13171 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13172 && !bfd_is_abs_section (h->root.u.def.section)
13173 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13174 h->root.u.def.section->flags |= SEC_KEEP;
13175 }
13176}
13177
2f0c68f2
CM
13178bfd_boolean
13179bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13180 struct bfd_link_info *info)
13181{
13182 bfd *ibfd = info->input_bfds;
13183
13184 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13185 {
13186 asection *sec;
13187 struct elf_reloc_cookie cookie;
13188
13189 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13190 continue;
13191
13192 if (!init_reloc_cookie (&cookie, info, ibfd))
13193 return FALSE;
13194
13195 for (sec = ibfd->sections; sec; sec = sec->next)
13196 {
13197 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13198 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13199 {
13200 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13201 fini_reloc_cookie_rels (&cookie, sec);
13202 }
13203 }
13204 }
13205 return TRUE;
13206}
13207
c152c796
AM
13208/* Do mark and sweep of unused sections. */
13209
13210bfd_boolean
13211bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13212{
13213 bfd_boolean ok = TRUE;
13214 bfd *sub;
6a5bb875 13215 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13216 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13217 struct elf_link_hash_table *htab;
c152c796 13218
64d03ab5 13219 if (!bed->can_gc_sections
715df9b8 13220 || !is_elf_hash_table (info->hash))
c152c796 13221 {
4eca0228 13222 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13223 return TRUE;
13224 }
13225
74f0fb50 13226 bed->gc_keep (info);
da44f4e5 13227 htab = elf_hash_table (info);
74f0fb50 13228
9d0a14d3
RS
13229 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13230 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13231 for (sub = info->input_bfds;
13232 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13233 sub = sub->link.next)
9d0a14d3
RS
13234 {
13235 asection *sec;
13236 struct elf_reloc_cookie cookie;
13237
13238 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13239 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13240 {
13241 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13242 if (elf_section_data (sec)->sec_info
13243 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13244 elf_eh_frame_section (sub) = sec;
13245 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13246 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13247 }
13248 }
9d0a14d3 13249
c152c796 13250 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13251 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13252 if (!ok)
13253 return FALSE;
13254
13255 /* Kill the vtable relocations that were not used. */
da44f4e5 13256 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13257 if (!ok)
13258 return FALSE;
13259
715df9b8 13260 /* Mark dynamically referenced symbols. */
22185505 13261 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13262 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13263
715df9b8 13264 /* Grovel through relocs to find out who stays ... */
64d03ab5 13265 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13266 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13267 {
13268 asection *o;
13269
b19a8f85
L
13270 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13271 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13272 continue;
13273
7f6ab9f8
AM
13274 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13275 Also treat note sections as a root, if the section is not part
13276 of a group. */
c152c796 13277 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13278 if (!o->gc_mark
13279 && (o->flags & SEC_EXCLUDE) == 0
24007750 13280 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13281 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13282 && elf_next_in_group (o) == NULL )))
13283 {
13284 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13285 return FALSE;
13286 }
c152c796
AM
13287 }
13288
6a5bb875 13289 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13290 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13291
c152c796 13292 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13293 return elf_gc_sweep (abfd, info);
c152c796
AM
13294}
13295\f
13296/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13297
13298bfd_boolean
13299bfd_elf_gc_record_vtinherit (bfd *abfd,
13300 asection *sec,
13301 struct elf_link_hash_entry *h,
13302 bfd_vma offset)
13303{
13304 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13305 struct elf_link_hash_entry **search, *child;
ef53be89 13306 size_t extsymcount;
c152c796
AM
13307 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13308
13309 /* The sh_info field of the symtab header tells us where the
13310 external symbols start. We don't care about the local symbols at
13311 this point. */
13312 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13313 if (!elf_bad_symtab (abfd))
13314 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13315
13316 sym_hashes = elf_sym_hashes (abfd);
13317 sym_hashes_end = sym_hashes + extsymcount;
13318
13319 /* Hunt down the child symbol, which is in this section at the same
13320 offset as the relocation. */
13321 for (search = sym_hashes; search != sym_hashes_end; ++search)
13322 {
13323 if ((child = *search) != NULL
13324 && (child->root.type == bfd_link_hash_defined
13325 || child->root.type == bfd_link_hash_defweak)
13326 && child->root.u.def.section == sec
13327 && child->root.u.def.value == offset)
13328 goto win;
13329 }
13330
695344c0
NC
13331 /* xgettext:c-format */
13332 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13333 abfd, sec, (unsigned long) offset);
c152c796
AM
13334 bfd_set_error (bfd_error_invalid_operation);
13335 return FALSE;
13336
13337 win:
f6e332e6
AM
13338 if (!child->vtable)
13339 {
ca4be51c
AM
13340 child->vtable = ((struct elf_link_virtual_table_entry *)
13341 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13342 if (!child->vtable)
13343 return FALSE;
13344 }
c152c796
AM
13345 if (!h)
13346 {
13347 /* This *should* only be the absolute section. It could potentially
13348 be that someone has defined a non-global vtable though, which
13349 would be bad. It isn't worth paging in the local symbols to be
13350 sure though; that case should simply be handled by the assembler. */
13351
f6e332e6 13352 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13353 }
13354 else
f6e332e6 13355 child->vtable->parent = h;
c152c796
AM
13356
13357 return TRUE;
13358}
13359
13360/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13361
13362bfd_boolean
13363bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13364 asection *sec ATTRIBUTE_UNUSED,
13365 struct elf_link_hash_entry *h,
13366 bfd_vma addend)
13367{
13368 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13369 unsigned int log_file_align = bed->s->log_file_align;
13370
f6e332e6
AM
13371 if (!h->vtable)
13372 {
ca4be51c
AM
13373 h->vtable = ((struct elf_link_virtual_table_entry *)
13374 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13375 if (!h->vtable)
13376 return FALSE;
13377 }
13378
13379 if (addend >= h->vtable->size)
c152c796
AM
13380 {
13381 size_t size, bytes, file_align;
f6e332e6 13382 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13383
13384 /* While the symbol is undefined, we have to be prepared to handle
13385 a zero size. */
13386 file_align = 1 << log_file_align;
13387 if (h->root.type == bfd_link_hash_undefined)
13388 size = addend + file_align;
13389 else
13390 {
13391 size = h->size;
13392 if (addend >= size)
13393 {
13394 /* Oops! We've got a reference past the defined end of
13395 the table. This is probably a bug -- shall we warn? */
13396 size = addend + file_align;
13397 }
13398 }
13399 size = (size + file_align - 1) & -file_align;
13400
13401 /* Allocate one extra entry for use as a "done" flag for the
13402 consolidation pass. */
13403 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13404
13405 if (ptr)
13406 {
a50b1753 13407 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13408
13409 if (ptr != NULL)
13410 {
13411 size_t oldbytes;
13412
f6e332e6 13413 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13414 * sizeof (bfd_boolean));
13415 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13416 }
13417 }
13418 else
a50b1753 13419 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13420
13421 if (ptr == NULL)
13422 return FALSE;
13423
13424 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13425 h->vtable->used = ptr + 1;
13426 h->vtable->size = size;
c152c796
AM
13427 }
13428
f6e332e6 13429 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13430
13431 return TRUE;
13432}
13433
ae17ab41
CM
13434/* Map an ELF section header flag to its corresponding string. */
13435typedef struct
13436{
13437 char *flag_name;
13438 flagword flag_value;
13439} elf_flags_to_name_table;
13440
13441static elf_flags_to_name_table elf_flags_to_names [] =
13442{
13443 { "SHF_WRITE", SHF_WRITE },
13444 { "SHF_ALLOC", SHF_ALLOC },
13445 { "SHF_EXECINSTR", SHF_EXECINSTR },
13446 { "SHF_MERGE", SHF_MERGE },
13447 { "SHF_STRINGS", SHF_STRINGS },
13448 { "SHF_INFO_LINK", SHF_INFO_LINK},
13449 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13450 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13451 { "SHF_GROUP", SHF_GROUP },
13452 { "SHF_TLS", SHF_TLS },
13453 { "SHF_MASKOS", SHF_MASKOS },
13454 { "SHF_EXCLUDE", SHF_EXCLUDE },
13455};
13456
b9c361e0
JL
13457/* Returns TRUE if the section is to be included, otherwise FALSE. */
13458bfd_boolean
ae17ab41 13459bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13460 struct flag_info *flaginfo,
b9c361e0 13461 asection *section)
ae17ab41 13462{
8b127cbc 13463 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13464
8b127cbc 13465 if (!flaginfo->flags_initialized)
ae17ab41 13466 {
8b127cbc
AM
13467 bfd *obfd = info->output_bfd;
13468 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13469 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13470 int with_hex = 0;
13471 int without_hex = 0;
13472
8b127cbc 13473 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13474 {
b9c361e0 13475 unsigned i;
8b127cbc 13476 flagword (*lookup) (char *);
ae17ab41 13477
8b127cbc
AM
13478 lookup = bed->elf_backend_lookup_section_flags_hook;
13479 if (lookup != NULL)
ae17ab41 13480 {
8b127cbc 13481 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13482
13483 if (hexval != 0)
13484 {
13485 if (tf->with == with_flags)
13486 with_hex |= hexval;
13487 else if (tf->with == without_flags)
13488 without_hex |= hexval;
13489 tf->valid = TRUE;
13490 continue;
13491 }
ae17ab41 13492 }
8b127cbc 13493 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13494 {
8b127cbc 13495 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13496 {
13497 if (tf->with == with_flags)
13498 with_hex |= elf_flags_to_names[i].flag_value;
13499 else if (tf->with == without_flags)
13500 without_hex |= elf_flags_to_names[i].flag_value;
13501 tf->valid = TRUE;
13502 break;
13503 }
13504 }
8b127cbc 13505 if (!tf->valid)
b9c361e0 13506 {
68ffbac6 13507 info->callbacks->einfo
8b127cbc 13508 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13509 return FALSE;
ae17ab41
CM
13510 }
13511 }
8b127cbc
AM
13512 flaginfo->flags_initialized = TRUE;
13513 flaginfo->only_with_flags |= with_hex;
13514 flaginfo->not_with_flags |= without_hex;
ae17ab41 13515 }
ae17ab41 13516
8b127cbc 13517 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13518 return FALSE;
13519
8b127cbc 13520 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13521 return FALSE;
13522
13523 return TRUE;
ae17ab41
CM
13524}
13525
c152c796
AM
13526struct alloc_got_off_arg {
13527 bfd_vma gotoff;
10455f89 13528 struct bfd_link_info *info;
c152c796
AM
13529};
13530
13531/* We need a special top-level link routine to convert got reference counts
13532 to real got offsets. */
13533
13534static bfd_boolean
13535elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13536{
a50b1753 13537 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13538 bfd *obfd = gofarg->info->output_bfd;
13539 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13540
c152c796
AM
13541 if (h->got.refcount > 0)
13542 {
13543 h->got.offset = gofarg->gotoff;
10455f89 13544 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13545 }
13546 else
13547 h->got.offset = (bfd_vma) -1;
13548
13549 return TRUE;
13550}
13551
13552/* And an accompanying bit to work out final got entry offsets once
13553 we're done. Should be called from final_link. */
13554
13555bfd_boolean
13556bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13557 struct bfd_link_info *info)
13558{
13559 bfd *i;
13560 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13561 bfd_vma gotoff;
c152c796
AM
13562 struct alloc_got_off_arg gofarg;
13563
10455f89
HPN
13564 BFD_ASSERT (abfd == info->output_bfd);
13565
c152c796
AM
13566 if (! is_elf_hash_table (info->hash))
13567 return FALSE;
13568
13569 /* The GOT offset is relative to the .got section, but the GOT header is
13570 put into the .got.plt section, if the backend uses it. */
13571 if (bed->want_got_plt)
13572 gotoff = 0;
13573 else
13574 gotoff = bed->got_header_size;
13575
13576 /* Do the local .got entries first. */
c72f2fb2 13577 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13578 {
13579 bfd_signed_vma *local_got;
ef53be89 13580 size_t j, locsymcount;
c152c796
AM
13581 Elf_Internal_Shdr *symtab_hdr;
13582
13583 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13584 continue;
13585
13586 local_got = elf_local_got_refcounts (i);
13587 if (!local_got)
13588 continue;
13589
13590 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13591 if (elf_bad_symtab (i))
13592 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13593 else
13594 locsymcount = symtab_hdr->sh_info;
13595
13596 for (j = 0; j < locsymcount; ++j)
13597 {
13598 if (local_got[j] > 0)
13599 {
13600 local_got[j] = gotoff;
10455f89 13601 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13602 }
13603 else
13604 local_got[j] = (bfd_vma) -1;
13605 }
13606 }
13607
13608 /* Then the global .got entries. .plt refcounts are handled by
13609 adjust_dynamic_symbol */
13610 gofarg.gotoff = gotoff;
10455f89 13611 gofarg.info = info;
c152c796
AM
13612 elf_link_hash_traverse (elf_hash_table (info),
13613 elf_gc_allocate_got_offsets,
13614 &gofarg);
13615 return TRUE;
13616}
13617
13618/* Many folk need no more in the way of final link than this, once
13619 got entry reference counting is enabled. */
13620
13621bfd_boolean
13622bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13623{
13624 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13625 return FALSE;
13626
13627 /* Invoke the regular ELF backend linker to do all the work. */
13628 return bfd_elf_final_link (abfd, info);
13629}
13630
13631bfd_boolean
13632bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13633{
a50b1753 13634 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13635
13636 if (rcookie->bad_symtab)
13637 rcookie->rel = rcookie->rels;
13638
13639 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13640 {
13641 unsigned long r_symndx;
13642
13643 if (! rcookie->bad_symtab)
13644 if (rcookie->rel->r_offset > offset)
13645 return FALSE;
13646 if (rcookie->rel->r_offset != offset)
13647 continue;
13648
13649 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13650 if (r_symndx == STN_UNDEF)
c152c796
AM
13651 return TRUE;
13652
13653 if (r_symndx >= rcookie->locsymcount
13654 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13655 {
13656 struct elf_link_hash_entry *h;
13657
13658 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13659
13660 while (h->root.type == bfd_link_hash_indirect
13661 || h->root.type == bfd_link_hash_warning)
13662 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13663
13664 if ((h->root.type == bfd_link_hash_defined
13665 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13666 && (h->root.u.def.section->owner != rcookie->abfd
13667 || h->root.u.def.section->kept_section != NULL
13668 || discarded_section (h->root.u.def.section)))
c152c796 13669 return TRUE;
c152c796
AM
13670 }
13671 else
13672 {
13673 /* It's not a relocation against a global symbol,
13674 but it could be a relocation against a local
13675 symbol for a discarded section. */
13676 asection *isec;
13677 Elf_Internal_Sym *isym;
13678
13679 /* Need to: get the symbol; get the section. */
13680 isym = &rcookie->locsyms[r_symndx];
cb33740c 13681 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13682 if (isec != NULL
13683 && (isec->kept_section != NULL
13684 || discarded_section (isec)))
cb33740c 13685 return TRUE;
c152c796
AM
13686 }
13687 return FALSE;
13688 }
13689 return FALSE;
13690}
13691
13692/* Discard unneeded references to discarded sections.
75938853
AM
13693 Returns -1 on error, 1 if any section's size was changed, 0 if
13694 nothing changed. This function assumes that the relocations are in
13695 sorted order, which is true for all known assemblers. */
c152c796 13696
75938853 13697int
c152c796
AM
13698bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13699{
13700 struct elf_reloc_cookie cookie;
18cd5bce 13701 asection *o;
c152c796 13702 bfd *abfd;
75938853 13703 int changed = 0;
c152c796
AM
13704
13705 if (info->traditional_format
13706 || !is_elf_hash_table (info->hash))
75938853 13707 return 0;
c152c796 13708
18cd5bce
AM
13709 o = bfd_get_section_by_name (output_bfd, ".stab");
13710 if (o != NULL)
c152c796 13711 {
18cd5bce 13712 asection *i;
c152c796 13713
18cd5bce 13714 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13715 {
18cd5bce
AM
13716 if (i->size == 0
13717 || i->reloc_count == 0
13718 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13719 continue;
c152c796 13720
18cd5bce
AM
13721 abfd = i->owner;
13722 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13723 continue;
c152c796 13724
18cd5bce 13725 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13726 return -1;
c152c796 13727
18cd5bce
AM
13728 if (_bfd_discard_section_stabs (abfd, i,
13729 elf_section_data (i)->sec_info,
5241d853
RS
13730 bfd_elf_reloc_symbol_deleted_p,
13731 &cookie))
75938853 13732 changed = 1;
18cd5bce
AM
13733
13734 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13735 }
18cd5bce
AM
13736 }
13737
2f0c68f2
CM
13738 o = NULL;
13739 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13740 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13741 if (o != NULL)
13742 {
13743 asection *i;
c152c796 13744
18cd5bce 13745 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13746 {
18cd5bce
AM
13747 if (i->size == 0)
13748 continue;
13749
13750 abfd = i->owner;
13751 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13752 continue;
13753
13754 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13755 return -1;
18cd5bce
AM
13756
13757 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13758 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13759 bfd_elf_reloc_symbol_deleted_p,
13760 &cookie))
75938853 13761 changed = 1;
18cd5bce
AM
13762
13763 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13764 }
18cd5bce 13765 }
c152c796 13766
18cd5bce
AM
13767 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13768 {
13769 const struct elf_backend_data *bed;
c152c796 13770
18cd5bce
AM
13771 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13772 continue;
13773
13774 bed = get_elf_backend_data (abfd);
13775
13776 if (bed->elf_backend_discard_info != NULL)
13777 {
13778 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13779 return -1;
18cd5bce
AM
13780
13781 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13782 changed = 1;
18cd5bce
AM
13783
13784 fini_reloc_cookie (&cookie, abfd);
13785 }
c152c796
AM
13786 }
13787
2f0c68f2
CM
13788 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13789 _bfd_elf_end_eh_frame_parsing (info);
13790
13791 if (info->eh_frame_hdr_type
0e1862bb 13792 && !bfd_link_relocatable (info)
c152c796 13793 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13794 changed = 1;
c152c796 13795
75938853 13796 return changed;
c152c796 13797}
082b7297 13798
43e1669b 13799bfd_boolean
0c511000 13800_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13801 asection *sec,
c0f00686 13802 struct bfd_link_info *info)
082b7297
L
13803{
13804 flagword flags;
c77ec726 13805 const char *name, *key;
082b7297
L
13806 struct bfd_section_already_linked *l;
13807 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13808
c77ec726
AM
13809 if (sec->output_section == bfd_abs_section_ptr)
13810 return FALSE;
0c511000 13811
c77ec726 13812 flags = sec->flags;
0c511000 13813
c77ec726
AM
13814 /* Return if it isn't a linkonce section. A comdat group section
13815 also has SEC_LINK_ONCE set. */
13816 if ((flags & SEC_LINK_ONCE) == 0)
13817 return FALSE;
0c511000 13818
c77ec726
AM
13819 /* Don't put group member sections on our list of already linked
13820 sections. They are handled as a group via their group section. */
13821 if (elf_sec_group (sec) != NULL)
13822 return FALSE;
0c511000 13823
c77ec726
AM
13824 /* For a SHT_GROUP section, use the group signature as the key. */
13825 name = sec->name;
13826 if ((flags & SEC_GROUP) != 0
13827 && elf_next_in_group (sec) != NULL
13828 && elf_group_name (elf_next_in_group (sec)) != NULL)
13829 key = elf_group_name (elf_next_in_group (sec));
13830 else
13831 {
13832 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13833 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13834 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13835 key++;
0c511000 13836 else
c77ec726
AM
13837 /* Must be a user linkonce section that doesn't follow gcc's
13838 naming convention. In this case we won't be matching
13839 single member groups. */
13840 key = name;
0c511000 13841 }
6d2cd210 13842
c77ec726 13843 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13844
13845 for (l = already_linked_list->entry; l != NULL; l = l->next)
13846 {
c2370991 13847 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13848 sections with a signature of <key> (<key> is some string),
13849 and linkonce sections named .gnu.linkonce.<type>.<key>.
13850 Match like sections. LTO plugin sections are an exception.
13851 They are always named .gnu.linkonce.t.<key> and match either
13852 type of section. */
13853 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13854 && ((flags & SEC_GROUP) != 0
13855 || strcmp (name, l->sec->name) == 0))
13856 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13857 {
13858 /* The section has already been linked. See if we should
6d2cd210 13859 issue a warning. */
c77ec726
AM
13860 if (!_bfd_handle_already_linked (sec, l, info))
13861 return FALSE;
082b7297 13862
c77ec726 13863 if (flags & SEC_GROUP)
3d7f7666 13864 {
c77ec726
AM
13865 asection *first = elf_next_in_group (sec);
13866 asection *s = first;
3d7f7666 13867
c77ec726 13868 while (s != NULL)
3d7f7666 13869 {
c77ec726
AM
13870 s->output_section = bfd_abs_section_ptr;
13871 /* Record which group discards it. */
13872 s->kept_section = l->sec;
13873 s = elf_next_in_group (s);
13874 /* These lists are circular. */
13875 if (s == first)
13876 break;
3d7f7666
L
13877 }
13878 }
082b7297 13879
43e1669b 13880 return TRUE;
082b7297
L
13881 }
13882 }
13883
c77ec726
AM
13884 /* A single member comdat group section may be discarded by a
13885 linkonce section and vice versa. */
13886 if ((flags & SEC_GROUP) != 0)
3d7f7666 13887 {
c77ec726 13888 asection *first = elf_next_in_group (sec);
c2370991 13889
c77ec726
AM
13890 if (first != NULL && elf_next_in_group (first) == first)
13891 /* Check this single member group against linkonce sections. */
13892 for (l = already_linked_list->entry; l != NULL; l = l->next)
13893 if ((l->sec->flags & SEC_GROUP) == 0
13894 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13895 {
13896 first->output_section = bfd_abs_section_ptr;
13897 first->kept_section = l->sec;
13898 sec->output_section = bfd_abs_section_ptr;
13899 break;
13900 }
13901 }
13902 else
13903 /* Check this linkonce section against single member groups. */
13904 for (l = already_linked_list->entry; l != NULL; l = l->next)
13905 if (l->sec->flags & SEC_GROUP)
6d2cd210 13906 {
c77ec726 13907 asection *first = elf_next_in_group (l->sec);
6d2cd210 13908
c77ec726
AM
13909 if (first != NULL
13910 && elf_next_in_group (first) == first
13911 && bfd_elf_match_symbols_in_sections (first, sec, info))
13912 {
13913 sec->output_section = bfd_abs_section_ptr;
13914 sec->kept_section = first;
13915 break;
13916 }
6d2cd210 13917 }
0c511000 13918
c77ec726
AM
13919 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13920 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13921 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13922 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13923 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13924 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13925 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13926 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13927 The reverse order cannot happen as there is never a bfd with only the
13928 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13929 matter as here were are looking only for cross-bfd sections. */
13930
13931 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13932 for (l = already_linked_list->entry; l != NULL; l = l->next)
13933 if ((l->sec->flags & SEC_GROUP) == 0
13934 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13935 {
13936 if (abfd != l->sec->owner)
13937 sec->output_section = bfd_abs_section_ptr;
13938 break;
13939 }
80c29487 13940
082b7297 13941 /* This is the first section with this name. Record it. */
c77ec726 13942 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13943 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13944 return sec->output_section == bfd_abs_section_ptr;
082b7297 13945}
81e1b023 13946
a4d8e49b
L
13947bfd_boolean
13948_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13949{
13950 return sym->st_shndx == SHN_COMMON;
13951}
13952
13953unsigned int
13954_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13955{
13956 return SHN_COMMON;
13957}
13958
13959asection *
13960_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13961{
13962 return bfd_com_section_ptr;
13963}
10455f89
HPN
13964
13965bfd_vma
13966_bfd_elf_default_got_elt_size (bfd *abfd,
13967 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13968 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13969 bfd *ibfd ATTRIBUTE_UNUSED,
13970 unsigned long symndx ATTRIBUTE_UNUSED)
13971{
13972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13973 return bed->s->arch_size / 8;
13974}
83bac4b0
NC
13975
13976/* Routines to support the creation of dynamic relocs. */
13977
83bac4b0
NC
13978/* Returns the name of the dynamic reloc section associated with SEC. */
13979
13980static const char *
13981get_dynamic_reloc_section_name (bfd * abfd,
13982 asection * sec,
13983 bfd_boolean is_rela)
13984{
ddcf1fcf
BS
13985 char *name;
13986 const char *old_name = bfd_get_section_name (NULL, sec);
13987 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13988
ddcf1fcf 13989 if (old_name == NULL)
83bac4b0
NC
13990 return NULL;
13991
ddcf1fcf 13992 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13993 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13994
13995 return name;
13996}
13997
13998/* Returns the dynamic reloc section associated with SEC.
13999 If necessary compute the name of the dynamic reloc section based
14000 on SEC's name (looked up in ABFD's string table) and the setting
14001 of IS_RELA. */
14002
14003asection *
14004_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14005 asection * sec,
14006 bfd_boolean is_rela)
14007{
14008 asection * reloc_sec = elf_section_data (sec)->sreloc;
14009
14010 if (reloc_sec == NULL)
14011 {
14012 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14013
14014 if (name != NULL)
14015 {
3d4d4302 14016 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14017
14018 if (reloc_sec != NULL)
14019 elf_section_data (sec)->sreloc = reloc_sec;
14020 }
14021 }
14022
14023 return reloc_sec;
14024}
14025
14026/* Returns the dynamic reloc section associated with SEC. If the
14027 section does not exist it is created and attached to the DYNOBJ
14028 bfd and stored in the SRELOC field of SEC's elf_section_data
14029 structure.
f8076f98 14030
83bac4b0
NC
14031 ALIGNMENT is the alignment for the newly created section and
14032 IS_RELA defines whether the name should be .rela.<SEC's name>
14033 or .rel.<SEC's name>. The section name is looked up in the
14034 string table associated with ABFD. */
14035
14036asection *
ca4be51c
AM
14037_bfd_elf_make_dynamic_reloc_section (asection *sec,
14038 bfd *dynobj,
14039 unsigned int alignment,
14040 bfd *abfd,
14041 bfd_boolean is_rela)
83bac4b0
NC
14042{
14043 asection * reloc_sec = elf_section_data (sec)->sreloc;
14044
14045 if (reloc_sec == NULL)
14046 {
14047 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14048
14049 if (name == NULL)
14050 return NULL;
14051
3d4d4302 14052 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14053
14054 if (reloc_sec == NULL)
14055 {
3d4d4302
AM
14056 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14057 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14058 if ((sec->flags & SEC_ALLOC) != 0)
14059 flags |= SEC_ALLOC | SEC_LOAD;
14060
3d4d4302 14061 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14062 if (reloc_sec != NULL)
14063 {
8877b5e5
AM
14064 /* _bfd_elf_get_sec_type_attr chooses a section type by
14065 name. Override as it may be wrong, eg. for a user
14066 section named "auto" we'll get ".relauto" which is
14067 seen to be a .rela section. */
14068 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14069 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14070 reloc_sec = NULL;
14071 }
14072 }
14073
14074 elf_section_data (sec)->sreloc = reloc_sec;
14075 }
14076
14077 return reloc_sec;
14078}
1338dd10 14079
bffebb6b
AM
14080/* Copy the ELF symbol type and other attributes for a linker script
14081 assignment from HSRC to HDEST. Generally this should be treated as
14082 if we found a strong non-dynamic definition for HDEST (except that
14083 ld ignores multiple definition errors). */
1338dd10 14084void
bffebb6b
AM
14085_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14086 struct bfd_link_hash_entry *hdest,
14087 struct bfd_link_hash_entry *hsrc)
1338dd10 14088{
bffebb6b
AM
14089 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14090 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14091 Elf_Internal_Sym isym;
1338dd10
PB
14092
14093 ehdest->type = ehsrc->type;
35fc36a8 14094 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14095
14096 isym.st_other = ehsrc->other;
b8417128 14097 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14098}
351f65ca
L
14099
14100/* Append a RELA relocation REL to section S in BFD. */
14101
14102void
14103elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14104{
14105 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14106 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14107 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14108 bed->s->swap_reloca_out (abfd, rel, loc);
14109}
14110
14111/* Append a REL relocation REL to section S in BFD. */
14112
14113void
14114elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14115{
14116 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14117 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14118 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14119 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14120}
This page took 2.683192 seconds and 4 git commands to generate.