Update NEWS post GDB 8.0 branch creation.
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
222 if ((ibfd->flags
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
224 {
225 abfd = ibfd;
226 break;
227 }
228 }
229 hash_table->dynobj = abfd;
230 }
7e9f0867
AM
231
232 if (hash_table->dynstr == NULL)
233 {
234 hash_table->dynstr = _bfd_elf_strtab_init ();
235 if (hash_table->dynstr == NULL)
236 return FALSE;
237 }
238 return TRUE;
239}
240
45d6a902
AM
241/* Create some sections which will be filled in with dynamic linking
242 information. ABFD is an input file which requires dynamic sections
243 to be created. The dynamic sections take up virtual memory space
244 when the final executable is run, so we need to create them before
245 addresses are assigned to the output sections. We work out the
246 actual contents and size of these sections later. */
252b5132 247
b34976b6 248bfd_boolean
268b6b39 249_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 250{
45d6a902 251 flagword flags;
91d6fa6a 252 asection *s;
9c5bfbb7 253 const struct elf_backend_data *bed;
9637f6ef 254 struct elf_link_hash_entry *h;
252b5132 255
0eddce27 256 if (! is_elf_hash_table (info->hash))
45d6a902
AM
257 return FALSE;
258
259 if (elf_hash_table (info)->dynamic_sections_created)
260 return TRUE;
261
7e9f0867
AM
262 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
263 return FALSE;
45d6a902 264
7e9f0867 265 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
266 bed = get_elf_backend_data (abfd);
267
268 flags = bed->dynamic_sec_flags;
45d6a902
AM
269
270 /* A dynamically linked executable has a .interp section, but a
271 shared library does not. */
9b8b325a 272 if (bfd_link_executable (info) && !info->nointerp)
252b5132 273 {
14b2f831
AM
274 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
275 flags | SEC_READONLY);
3496cb2a 276 if (s == NULL)
45d6a902
AM
277 return FALSE;
278 }
bb0deeff 279
45d6a902
AM
280 /* Create sections to hold version informations. These are removed
281 if they are not needed. */
14b2f831
AM
282 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
283 flags | SEC_READONLY);
45d6a902 284 if (s == NULL
45d6a902
AM
285 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
286 return FALSE;
287
14b2f831
AM
288 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
289 flags | SEC_READONLY);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, 1))
292 return FALSE;
293
14b2f831
AM
294 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
295 flags | SEC_READONLY);
45d6a902 296 if (s == NULL
45d6a902
AM
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
298 return FALSE;
299
14b2f831
AM
300 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
301 flags | SEC_READONLY);
45d6a902 302 if (s == NULL
45d6a902
AM
303 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 return FALSE;
cae1fbbb 305 elf_hash_table (info)->dynsym = s;
45d6a902 306
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
308 flags | SEC_READONLY);
3496cb2a 309 if (s == NULL)
45d6a902
AM
310 return FALSE;
311
14b2f831 312 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 313 if (s == NULL
45d6a902
AM
314 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
315 return FALSE;
316
317 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
318 .dynamic section. We could set _DYNAMIC in a linker script, but we
319 only want to define it if we are, in fact, creating a .dynamic
320 section. We don't want to define it if there is no .dynamic
321 section, since on some ELF platforms the start up code examines it
322 to decide how to initialize the process. */
9637f6ef
L
323 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
324 elf_hash_table (info)->hdynamic = h;
325 if (h == NULL)
45d6a902
AM
326 return FALSE;
327
fdc90cb4
JJ
328 if (info->emit_hash)
329 {
14b2f831
AM
330 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
331 flags | SEC_READONLY);
fdc90cb4
JJ
332 if (s == NULL
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
335 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
336 }
337
338 if (info->emit_gnu_hash)
339 {
14b2f831
AM
340 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
341 flags | SEC_READONLY);
fdc90cb4
JJ
342 if (s == NULL
343 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
344 return FALSE;
345 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
346 4 32-bit words followed by variable count of 64-bit words, then
347 variable count of 32-bit words. */
348 if (bed->s->arch_size == 64)
349 elf_section_data (s)->this_hdr.sh_entsize = 0;
350 else
351 elf_section_data (s)->this_hdr.sh_entsize = 4;
352 }
45d6a902
AM
353
354 /* Let the backend create the rest of the sections. This lets the
355 backend set the right flags. The backend will normally create
356 the .got and .plt sections. */
894891db
NC
357 if (bed->elf_backend_create_dynamic_sections == NULL
358 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
359 return FALSE;
360
361 elf_hash_table (info)->dynamic_sections_created = TRUE;
362
363 return TRUE;
364}
365
366/* Create dynamic sections when linking against a dynamic object. */
367
368bfd_boolean
268b6b39 369_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
370{
371 flagword flags, pltflags;
7325306f 372 struct elf_link_hash_entry *h;
45d6a902 373 asection *s;
9c5bfbb7 374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 375 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 376
252b5132
RH
377 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
378 .rel[a].bss sections. */
e5a52504 379 flags = bed->dynamic_sec_flags;
252b5132
RH
380
381 pltflags = flags;
252b5132 382 if (bed->plt_not_loaded)
6df4d94c
MM
383 /* We do not clear SEC_ALLOC here because we still want the OS to
384 allocate space for the section; it's just that there's nothing
385 to read in from the object file. */
5d1634d7 386 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
387 else
388 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
389 if (bed->plt_readonly)
390 pltflags |= SEC_READONLY;
391
14b2f831 392 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 393 if (s == NULL
252b5132 394 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 395 return FALSE;
6de2ae4a 396 htab->splt = s;
252b5132 397
d98685ac
AM
398 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
399 .plt section. */
7325306f
RS
400 if (bed->want_plt_sym)
401 {
402 h = _bfd_elf_define_linkage_sym (abfd, info, s,
403 "_PROCEDURE_LINKAGE_TABLE_");
404 elf_hash_table (info)->hplt = h;
405 if (h == NULL)
406 return FALSE;
407 }
252b5132 408
14b2f831
AM
409 s = bfd_make_section_anyway_with_flags (abfd,
410 (bed->rela_plts_and_copies_p
411 ? ".rela.plt" : ".rel.plt"),
412 flags | SEC_READONLY);
252b5132 413 if (s == NULL
45d6a902 414 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 415 return FALSE;
6de2ae4a 416 htab->srelplt = s;
252b5132
RH
417
418 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 419 return FALSE;
252b5132 420
3018b441
RH
421 if (bed->want_dynbss)
422 {
423 /* The .dynbss section is a place to put symbols which are defined
424 by dynamic objects, are referenced by regular objects, and are
425 not functions. We must allocate space for them in the process
426 image and use a R_*_COPY reloc to tell the dynamic linker to
427 initialize them at run time. The linker script puts the .dynbss
428 section into the .bss section of the final image. */
14b2f831 429 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 430 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 431 if (s == NULL)
b34976b6 432 return FALSE;
9d19e4fd 433 htab->sdynbss = s;
252b5132 434
5474d94f
AM
435 if (bed->want_dynrelro)
436 {
437 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
438 sections. This section doesn't really need to have contents,
439 but make it like other .data.rel.ro sections. */
5474d94f 440 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 441 flags);
5474d94f
AM
442 if (s == NULL)
443 return FALSE;
444 htab->sdynrelro = s;
445 }
446
3018b441 447 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
448 normally needed. We need to create it here, though, so that the
449 linker will map it to an output section. We can't just create it
450 only if we need it, because we will not know whether we need it
451 until we have seen all the input files, and the first time the
452 main linker code calls BFD after examining all the input files
453 (size_dynamic_sections) the input sections have already been
454 mapped to the output sections. If the section turns out not to
455 be needed, we can discard it later. We will never need this
456 section when generating a shared object, since they do not use
457 copy relocs. */
9d19e4fd 458 if (bfd_link_executable (info))
3018b441 459 {
14b2f831
AM
460 s = bfd_make_section_anyway_with_flags (abfd,
461 (bed->rela_plts_and_copies_p
462 ? ".rela.bss" : ".rel.bss"),
463 flags | SEC_READONLY);
3018b441 464 if (s == NULL
45d6a902 465 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 466 return FALSE;
9d19e4fd 467 htab->srelbss = s;
5474d94f
AM
468
469 if (bed->want_dynrelro)
470 {
471 s = (bfd_make_section_anyway_with_flags
472 (abfd, (bed->rela_plts_and_copies_p
473 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
474 flags | SEC_READONLY));
475 if (s == NULL
476 || ! bfd_set_section_alignment (abfd, s,
477 bed->s->log_file_align))
478 return FALSE;
479 htab->sreldynrelro = s;
480 }
3018b441 481 }
252b5132
RH
482 }
483
b34976b6 484 return TRUE;
252b5132
RH
485}
486\f
252b5132
RH
487/* Record a new dynamic symbol. We record the dynamic symbols as we
488 read the input files, since we need to have a list of all of them
489 before we can determine the final sizes of the output sections.
490 Note that we may actually call this function even though we are not
491 going to output any dynamic symbols; in some cases we know that a
492 symbol should be in the dynamic symbol table, but only if there is
493 one. */
494
b34976b6 495bfd_boolean
c152c796
AM
496bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
497 struct elf_link_hash_entry *h)
252b5132
RH
498{
499 if (h->dynindx == -1)
500 {
2b0f7ef9 501 struct elf_strtab_hash *dynstr;
68b6ddd0 502 char *p;
252b5132 503 const char *name;
ef53be89 504 size_t indx;
252b5132 505
7a13edea
NC
506 /* XXX: The ABI draft says the linker must turn hidden and
507 internal symbols into STB_LOCAL symbols when producing the
508 DSO. However, if ld.so honors st_other in the dynamic table,
509 this would not be necessary. */
510 switch (ELF_ST_VISIBILITY (h->other))
511 {
512 case STV_INTERNAL:
513 case STV_HIDDEN:
9d6eee78
L
514 if (h->root.type != bfd_link_hash_undefined
515 && h->root.type != bfd_link_hash_undefweak)
38048eb9 516 {
f5385ebf 517 h->forced_local = 1;
67687978
PB
518 if (!elf_hash_table (info)->is_relocatable_executable)
519 return TRUE;
7a13edea 520 }
0444bdd4 521
7a13edea
NC
522 default:
523 break;
524 }
525
252b5132
RH
526 h->dynindx = elf_hash_table (info)->dynsymcount;
527 ++elf_hash_table (info)->dynsymcount;
528
529 dynstr = elf_hash_table (info)->dynstr;
530 if (dynstr == NULL)
531 {
532 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 533 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 534 if (dynstr == NULL)
b34976b6 535 return FALSE;
252b5132
RH
536 }
537
538 /* We don't put any version information in the dynamic string
aad5d350 539 table. */
252b5132
RH
540 name = h->root.root.string;
541 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
542 if (p != NULL)
543 /* We know that the p points into writable memory. In fact,
544 there are only a few symbols that have read-only names, being
545 those like _GLOBAL_OFFSET_TABLE_ that are created specially
546 by the backends. Most symbols will have names pointing into
547 an ELF string table read from a file, or to objalloc memory. */
548 *p = 0;
549
550 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
551
552 if (p != NULL)
553 *p = ELF_VER_CHR;
252b5132 554
ef53be89 555 if (indx == (size_t) -1)
b34976b6 556 return FALSE;
252b5132
RH
557 h->dynstr_index = indx;
558 }
559
b34976b6 560 return TRUE;
252b5132 561}
45d6a902 562\f
55255dae
L
563/* Mark a symbol dynamic. */
564
28caa186 565static void
55255dae 566bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
567 struct elf_link_hash_entry *h,
568 Elf_Internal_Sym *sym)
55255dae 569{
40b36307 570 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 571
40b36307 572 /* It may be called more than once on the same H. */
0e1862bb 573 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
574 return;
575
40b36307
L
576 if ((info->dynamic_data
577 && (h->type == STT_OBJECT
b8871f35 578 || h->type == STT_COMMON
40b36307 579 || (sym != NULL
b8871f35
L
580 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
581 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 582 || (d != NULL
40b36307
L
583 && h->root.type == bfd_link_hash_new
584 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
585 h->dynamic = 1;
586}
587
45d6a902
AM
588/* Record an assignment to a symbol made by a linker script. We need
589 this in case some dynamic object refers to this symbol. */
590
591bfd_boolean
fe21a8fc
L
592bfd_elf_record_link_assignment (bfd *output_bfd,
593 struct bfd_link_info *info,
268b6b39 594 const char *name,
fe21a8fc
L
595 bfd_boolean provide,
596 bfd_boolean hidden)
45d6a902 597{
00cbee0a 598 struct elf_link_hash_entry *h, *hv;
4ea42fb7 599 struct elf_link_hash_table *htab;
00cbee0a 600 const struct elf_backend_data *bed;
45d6a902 601
0eddce27 602 if (!is_elf_hash_table (info->hash))
45d6a902
AM
603 return TRUE;
604
4ea42fb7
AM
605 htab = elf_hash_table (info);
606 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 607 if (h == NULL)
4ea42fb7 608 return provide;
45d6a902 609
8e2a4f11
AM
610 if (h->root.type == bfd_link_hash_warning)
611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
612
0f550b3d
L
613 if (h->versioned == unknown)
614 {
615 /* Set versioned if symbol version is unknown. */
616 char *version = strrchr (name, ELF_VER_CHR);
617 if (version)
618 {
619 if (version > name && version[-1] != ELF_VER_CHR)
620 h->versioned = versioned_hidden;
621 else
622 h->versioned = versioned;
623 }
624 }
625
00cbee0a 626 switch (h->root.type)
77cfaee6 627 {
00cbee0a
L
628 case bfd_link_hash_defined:
629 case bfd_link_hash_defweak:
630 case bfd_link_hash_common:
631 break;
632 case bfd_link_hash_undefweak:
633 case bfd_link_hash_undefined:
634 /* Since we're defining the symbol, don't let it seem to have not
635 been defined. record_dynamic_symbol and size_dynamic_sections
636 may depend on this. */
4ea42fb7 637 h->root.type = bfd_link_hash_new;
77cfaee6
AM
638 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
639 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
640 break;
641 case bfd_link_hash_new:
40b36307 642 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 643 h->non_elf = 0;
00cbee0a
L
644 break;
645 case bfd_link_hash_indirect:
646 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 647 the versioned symbol point to this one. */
00cbee0a
L
648 bed = get_elf_backend_data (output_bfd);
649 hv = h;
650 while (hv->root.type == bfd_link_hash_indirect
651 || hv->root.type == bfd_link_hash_warning)
652 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
653 /* We don't need to update h->root.u since linker will set them
654 later. */
655 h->root.type = bfd_link_hash_undefined;
656 hv->root.type = bfd_link_hash_indirect;
657 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
658 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
659 break;
8e2a4f11
AM
660 default:
661 BFD_FAIL ();
c2596ca5 662 return FALSE;
55255dae 663 }
45d6a902
AM
664
665 /* If this symbol is being provided by the linker script, and it is
666 currently defined by a dynamic object, but not by a regular
667 object, then mark it as undefined so that the generic linker will
668 force the correct value. */
669 if (provide
f5385ebf
AM
670 && h->def_dynamic
671 && !h->def_regular)
45d6a902
AM
672 h->root.type = bfd_link_hash_undefined;
673
674 /* If this symbol is not being provided by the linker script, and it is
675 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
676 then clear out any version information because the symbol will not be
677 associated with the dynamic object any more. */
45d6a902 678 if (!provide
f5385ebf
AM
679 && h->def_dynamic
680 && !h->def_regular)
b531344c
MR
681 h->verinfo.verdef = NULL;
682
683 /* Make sure this symbol is not garbage collected. */
684 h->mark = 1;
45d6a902 685
f5385ebf 686 h->def_regular = 1;
45d6a902 687
eb8476a6 688 if (hidden)
fe21a8fc 689 {
91d6fa6a 690 bed = get_elf_backend_data (output_bfd);
b8297068
AM
691 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
692 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
694 }
695
6fa3860b
PB
696 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
697 and executables. */
0e1862bb 698 if (!bfd_link_relocatable (info)
6fa3860b
PB
699 && h->dynindx != -1
700 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
701 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
702 h->forced_local = 1;
703
f5385ebf
AM
704 if ((h->def_dynamic
705 || h->ref_dynamic
6b3b0ab8
L
706 || bfd_link_dll (info)
707 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
708 && h->dynindx == -1)
709 {
c152c796 710 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
711 return FALSE;
712
713 /* If this is a weak defined symbol, and we know a corresponding
714 real symbol from the same dynamic object, make sure the real
715 symbol is also made into a dynamic symbol. */
f6e332e6
AM
716 if (h->u.weakdef != NULL
717 && h->u.weakdef->dynindx == -1)
45d6a902 718 {
f6e332e6 719 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
720 return FALSE;
721 }
722 }
723
724 return TRUE;
725}
42751cf3 726
8c58d23b
AM
727/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
728 success, and 2 on a failure caused by attempting to record a symbol
729 in a discarded section, eg. a discarded link-once section symbol. */
730
731int
c152c796
AM
732bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
733 bfd *input_bfd,
734 long input_indx)
8c58d23b
AM
735{
736 bfd_size_type amt;
737 struct elf_link_local_dynamic_entry *entry;
738 struct elf_link_hash_table *eht;
739 struct elf_strtab_hash *dynstr;
ef53be89 740 size_t dynstr_index;
8c58d23b
AM
741 char *name;
742 Elf_External_Sym_Shndx eshndx;
743 char esym[sizeof (Elf64_External_Sym)];
744
0eddce27 745 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
746 return 0;
747
748 /* See if the entry exists already. */
749 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
750 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
751 return 1;
752
753 amt = sizeof (*entry);
a50b1753 754 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
755 if (entry == NULL)
756 return 0;
757
758 /* Go find the symbol, so that we can find it's name. */
759 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 760 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
761 {
762 bfd_release (input_bfd, entry);
763 return 0;
764 }
765
766 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 767 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
768 {
769 asection *s;
770
771 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
772 if (s == NULL || bfd_is_abs_section (s->output_section))
773 {
774 /* We can still bfd_release here as nothing has done another
775 bfd_alloc. We can't do this later in this function. */
776 bfd_release (input_bfd, entry);
777 return 2;
778 }
779 }
780
781 name = (bfd_elf_string_from_elf_section
782 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
783 entry->isym.st_name));
784
785 dynstr = elf_hash_table (info)->dynstr;
786 if (dynstr == NULL)
787 {
788 /* Create a strtab to hold the dynamic symbol names. */
789 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
790 if (dynstr == NULL)
791 return 0;
792 }
793
b34976b6 794 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 795 if (dynstr_index == (size_t) -1)
8c58d23b
AM
796 return 0;
797 entry->isym.st_name = dynstr_index;
798
799 eht = elf_hash_table (info);
800
801 entry->next = eht->dynlocal;
802 eht->dynlocal = entry;
803 entry->input_bfd = input_bfd;
804 entry->input_indx = input_indx;
805 eht->dynsymcount++;
806
807 /* Whatever binding the symbol had before, it's now local. */
808 entry->isym.st_info
809 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
810
811 /* The dynindx will be set at the end of size_dynamic_sections. */
812
813 return 1;
814}
815
30b30c21 816/* Return the dynindex of a local dynamic symbol. */
42751cf3 817
30b30c21 818long
268b6b39
AM
819_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
820 bfd *input_bfd,
821 long input_indx)
30b30c21
RH
822{
823 struct elf_link_local_dynamic_entry *e;
824
825 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
826 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
827 return e->dynindx;
828 return -1;
829}
830
831/* This function is used to renumber the dynamic symbols, if some of
832 them are removed because they are marked as local. This is called
833 via elf_link_hash_traverse. */
834
b34976b6 835static bfd_boolean
268b6b39
AM
836elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
837 void *data)
42751cf3 838{
a50b1753 839 size_t *count = (size_t *) data;
30b30c21 840
6fa3860b
PB
841 if (h->forced_local)
842 return TRUE;
843
844 if (h->dynindx != -1)
845 h->dynindx = ++(*count);
846
847 return TRUE;
848}
849
850
851/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
852 STB_LOCAL binding. */
853
854static bfd_boolean
855elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
856 void *data)
857{
a50b1753 858 size_t *count = (size_t *) data;
6fa3860b 859
6fa3860b
PB
860 if (!h->forced_local)
861 return TRUE;
862
42751cf3 863 if (h->dynindx != -1)
30b30c21
RH
864 h->dynindx = ++(*count);
865
b34976b6 866 return TRUE;
42751cf3 867}
30b30c21 868
aee6f5b4
AO
869/* Return true if the dynamic symbol for a given section should be
870 omitted when creating a shared library. */
871bfd_boolean
872_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
873 struct bfd_link_info *info,
874 asection *p)
875{
74541ad4 876 struct elf_link_hash_table *htab;
ca55926c 877 asection *ip;
74541ad4 878
aee6f5b4
AO
879 switch (elf_section_data (p)->this_hdr.sh_type)
880 {
881 case SHT_PROGBITS:
882 case SHT_NOBITS:
883 /* If sh_type is yet undecided, assume it could be
884 SHT_PROGBITS/SHT_NOBITS. */
885 case SHT_NULL:
74541ad4
AM
886 htab = elf_hash_table (info);
887 if (p == htab->tls_sec)
888 return FALSE;
889
890 if (htab->text_index_section != NULL)
891 return p != htab->text_index_section && p != htab->data_index_section;
892
ca55926c 893 return (htab->dynobj != NULL
3d4d4302 894 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 895 && ip->output_section == p);
aee6f5b4
AO
896
897 /* There shouldn't be section relative relocations
898 against any other section. */
899 default:
900 return TRUE;
901 }
902}
903
062e2358 904/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
905 symbol for each output section, which come first. Next come symbols
906 which have been forced to local binding. Then all of the back-end
907 allocated local dynamic syms, followed by the rest of the global
908 symbols. */
30b30c21 909
554220db
AM
910static unsigned long
911_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
912 struct bfd_link_info *info,
913 unsigned long *section_sym_count)
30b30c21
RH
914{
915 unsigned long dynsymcount = 0;
916
0e1862bb
L
917 if (bfd_link_pic (info)
918 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 919 {
aee6f5b4 920 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
921 asection *p;
922 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 923 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
924 && (p->flags & SEC_ALLOC) != 0
925 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
926 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
927 else
928 elf_section_data (p)->dynindx = 0;
30b30c21 929 }
554220db 930 *section_sym_count = dynsymcount;
30b30c21 931
6fa3860b
PB
932 elf_link_hash_traverse (elf_hash_table (info),
933 elf_link_renumber_local_hash_table_dynsyms,
934 &dynsymcount);
935
30b30c21
RH
936 if (elf_hash_table (info)->dynlocal)
937 {
938 struct elf_link_local_dynamic_entry *p;
939 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
940 p->dynindx = ++dynsymcount;
941 }
90ac2420 942 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
943
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_hash_table_dynsyms,
946 &dynsymcount);
947
d5486c43
L
948 /* There is an unused NULL entry at the head of the table which we
949 must account for in our count even if the table is empty since it
950 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
951 .dynamic section. */
952 dynsymcount++;
30b30c21 953
ccabcbe5
AM
954 elf_hash_table (info)->dynsymcount = dynsymcount;
955 return dynsymcount;
30b30c21 956}
252b5132 957
54ac0771
L
958/* Merge st_other field. */
959
960static void
961elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 962 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 963 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
964{
965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
966
967 /* If st_other has a processor-specific meaning, specific
cd3416da 968 code might be needed here. */
54ac0771
L
969 if (bed->elf_backend_merge_symbol_attribute)
970 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
971 dynamic);
972
cd3416da 973 if (!dynamic)
54ac0771 974 {
cd3416da
AM
975 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
976 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 977
cd3416da
AM
978 /* Keep the most constraining visibility. Leave the remainder
979 of the st_other field to elf_backend_merge_symbol_attribute. */
980 if (symvis - 1 < hvis - 1)
981 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 982 }
b8417128
AM
983 else if (definition
984 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
985 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 986 h->protected_def = 1;
54ac0771
L
987}
988
4f3fedcf
AM
989/* This function is called when we want to merge a new symbol with an
990 existing symbol. It handles the various cases which arise when we
991 find a definition in a dynamic object, or when there is already a
992 definition in a dynamic object. The new symbol is described by
993 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
994 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
995 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
996 of an old common symbol. We set OVERRIDE if the old symbol is
997 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
998 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
999 to change. By OK to change, we mean that we shouldn't warn if the
1000 type or size does change. */
45d6a902 1001
8a56bd02 1002static bfd_boolean
268b6b39
AM
1003_bfd_elf_merge_symbol (bfd *abfd,
1004 struct bfd_link_info *info,
1005 const char *name,
1006 Elf_Internal_Sym *sym,
1007 asection **psec,
1008 bfd_vma *pvalue,
4f3fedcf
AM
1009 struct elf_link_hash_entry **sym_hash,
1010 bfd **poldbfd,
37a9e49a 1011 bfd_boolean *pold_weak,
af44c138 1012 unsigned int *pold_alignment,
268b6b39
AM
1013 bfd_boolean *skip,
1014 bfd_boolean *override,
1015 bfd_boolean *type_change_ok,
6e33951e
L
1016 bfd_boolean *size_change_ok,
1017 bfd_boolean *matched)
252b5132 1018{
7479dfd4 1019 asection *sec, *oldsec;
45d6a902 1020 struct elf_link_hash_entry *h;
90c984fc 1021 struct elf_link_hash_entry *hi;
45d6a902
AM
1022 struct elf_link_hash_entry *flip;
1023 int bind;
1024 bfd *oldbfd;
1025 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1026 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1027 const struct elf_backend_data *bed;
6e33951e 1028 char *new_version;
45d6a902
AM
1029
1030 *skip = FALSE;
1031 *override = FALSE;
1032
1033 sec = *psec;
1034 bind = ELF_ST_BIND (sym->st_info);
1035
1036 if (! bfd_is_und_section (sec))
1037 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1038 else
1039 h = ((struct elf_link_hash_entry *)
1040 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1041 if (h == NULL)
1042 return FALSE;
1043 *sym_hash = h;
252b5132 1044
88ba32a0
L
1045 bed = get_elf_backend_data (abfd);
1046
6e33951e 1047 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1048 if (h->versioned != unversioned)
6e33951e 1049 {
422f1182
L
1050 /* Symbol version is unknown or versioned. */
1051 new_version = strrchr (name, ELF_VER_CHR);
1052 if (new_version)
1053 {
1054 if (h->versioned == unknown)
1055 {
1056 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1057 h->versioned = versioned_hidden;
1058 else
1059 h->versioned = versioned;
1060 }
1061 new_version += 1;
1062 if (new_version[0] == '\0')
1063 new_version = NULL;
1064 }
1065 else
1066 h->versioned = unversioned;
6e33951e 1067 }
422f1182
L
1068 else
1069 new_version = NULL;
6e33951e 1070
90c984fc
L
1071 /* For merging, we only care about real symbols. But we need to make
1072 sure that indirect symbol dynamic flags are updated. */
1073 hi = h;
45d6a902
AM
1074 while (h->root.type == bfd_link_hash_indirect
1075 || h->root.type == bfd_link_hash_warning)
1076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1077
6e33951e
L
1078 if (!*matched)
1079 {
1080 if (hi == h || h->root.type == bfd_link_hash_new)
1081 *matched = TRUE;
1082 else
1083 {
ae7683d2 1084 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1085 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1086 true if the new symbol is only visible to the symbol with
6e33951e 1087 the same symbol version. */
422f1182
L
1088 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1089 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1090 if (!old_hidden && !new_hidden)
1091 /* The new symbol matches the existing symbol if both
1092 aren't hidden. */
1093 *matched = TRUE;
1094 else
1095 {
1096 /* OLD_VERSION is the symbol version of the existing
1097 symbol. */
422f1182
L
1098 char *old_version;
1099
1100 if (h->versioned >= versioned)
1101 old_version = strrchr (h->root.root.string,
1102 ELF_VER_CHR) + 1;
1103 else
1104 old_version = NULL;
6e33951e
L
1105
1106 /* The new symbol matches the existing symbol if they
1107 have the same symbol version. */
1108 *matched = (old_version == new_version
1109 || (old_version != NULL
1110 && new_version != NULL
1111 && strcmp (old_version, new_version) == 0));
1112 }
1113 }
1114 }
1115
934bce08
AM
1116 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1117 existing symbol. */
1118
1119 oldbfd = NULL;
1120 oldsec = NULL;
1121 switch (h->root.type)
1122 {
1123 default:
1124 break;
1125
1126 case bfd_link_hash_undefined:
1127 case bfd_link_hash_undefweak:
1128 oldbfd = h->root.u.undef.abfd;
1129 break;
1130
1131 case bfd_link_hash_defined:
1132 case bfd_link_hash_defweak:
1133 oldbfd = h->root.u.def.section->owner;
1134 oldsec = h->root.u.def.section;
1135 break;
1136
1137 case bfd_link_hash_common:
1138 oldbfd = h->root.u.c.p->section->owner;
1139 oldsec = h->root.u.c.p->section;
1140 if (pold_alignment)
1141 *pold_alignment = h->root.u.c.p->alignment_power;
1142 break;
1143 }
1144 if (poldbfd && *poldbfd == NULL)
1145 *poldbfd = oldbfd;
1146
1147 /* Differentiate strong and weak symbols. */
1148 newweak = bind == STB_WEAK;
1149 oldweak = (h->root.type == bfd_link_hash_defweak
1150 || h->root.type == bfd_link_hash_undefweak);
1151 if (pold_weak)
1152 *pold_weak = oldweak;
1153
1154 /* This code is for coping with dynamic objects, and is only useful
1155 if we are doing an ELF link. */
1156 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1157 return TRUE;
1158
40b36307 1159 /* We have to check it for every instance since the first few may be
ee659f1f 1160 references and not all compilers emit symbol type for undefined
40b36307
L
1161 symbols. */
1162 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1163
ee659f1f
AM
1164 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1165 respectively, is from a dynamic object. */
1166
1167 newdyn = (abfd->flags & DYNAMIC) != 0;
1168
1169 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1170 syms and defined syms in dynamic libraries respectively.
1171 ref_dynamic on the other hand can be set for a symbol defined in
1172 a dynamic library, and def_dynamic may not be set; When the
1173 definition in a dynamic lib is overridden by a definition in the
1174 executable use of the symbol in the dynamic lib becomes a
1175 reference to the executable symbol. */
1176 if (newdyn)
1177 {
1178 if (bfd_is_und_section (sec))
1179 {
1180 if (bind != STB_WEAK)
1181 {
1182 h->ref_dynamic_nonweak = 1;
1183 hi->ref_dynamic_nonweak = 1;
1184 }
1185 }
1186 else
1187 {
6e33951e
L
1188 /* Update the existing symbol only if they match. */
1189 if (*matched)
1190 h->dynamic_def = 1;
ee659f1f
AM
1191 hi->dynamic_def = 1;
1192 }
1193 }
1194
45d6a902
AM
1195 /* If we just created the symbol, mark it as being an ELF symbol.
1196 Other than that, there is nothing to do--there is no merge issue
1197 with a newly defined symbol--so we just return. */
1198
1199 if (h->root.type == bfd_link_hash_new)
252b5132 1200 {
f5385ebf 1201 h->non_elf = 0;
45d6a902
AM
1202 return TRUE;
1203 }
252b5132 1204
45d6a902
AM
1205 /* In cases involving weak versioned symbols, we may wind up trying
1206 to merge a symbol with itself. Catch that here, to avoid the
1207 confusion that results if we try to override a symbol with
1208 itself. The additional tests catch cases like
1209 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1210 dynamic object, which we do want to handle here. */
1211 if (abfd == oldbfd
895fa45f 1212 && (newweak || oldweak)
45d6a902 1213 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1214 || !h->def_regular))
45d6a902
AM
1215 return TRUE;
1216
707bba77 1217 olddyn = FALSE;
45d6a902
AM
1218 if (oldbfd != NULL)
1219 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1220 else if (oldsec != NULL)
45d6a902 1221 {
707bba77 1222 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1223 indices used by MIPS ELF. */
707bba77 1224 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1225 }
252b5132 1226
45d6a902
AM
1227 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1228 respectively, appear to be a definition rather than reference. */
1229
707bba77 1230 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1231
707bba77
AM
1232 olddef = (h->root.type != bfd_link_hash_undefined
1233 && h->root.type != bfd_link_hash_undefweak
202ac193 1234 && h->root.type != bfd_link_hash_common);
45d6a902 1235
0a36a439
L
1236 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1237 respectively, appear to be a function. */
1238
1239 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1240 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1241
1242 oldfunc = (h->type != STT_NOTYPE
1243 && bed->is_function_type (h->type));
1244
c5d37467 1245 if (!(newfunc && oldfunc)
5b677558
AM
1246 && ELF_ST_TYPE (sym->st_info) != h->type
1247 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1248 && h->type != STT_NOTYPE
c5d37467
AM
1249 && (newdef || bfd_is_com_section (sec))
1250 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1251 {
c5d37467
AM
1252 /* If creating a default indirect symbol ("foo" or "foo@") from
1253 a dynamic versioned definition ("foo@@") skip doing so if
1254 there is an existing regular definition with a different
1255 type. We don't want, for example, a "time" variable in the
1256 executable overriding a "time" function in a shared library. */
1257 if (newdyn
1258 && !olddyn)
1259 {
1260 *skip = TRUE;
1261 return TRUE;
1262 }
1263
1264 /* When adding a symbol from a regular object file after we have
1265 created indirect symbols, undo the indirection and any
1266 dynamic state. */
1267 if (hi != h
1268 && !newdyn
1269 && olddyn)
1270 {
1271 h = hi;
1272 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1273 h->forced_local = 0;
1274 h->ref_dynamic = 0;
1275 h->def_dynamic = 0;
1276 h->dynamic_def = 0;
1277 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1278 {
1279 h->root.type = bfd_link_hash_undefined;
1280 h->root.u.undef.abfd = abfd;
1281 }
1282 else
1283 {
1284 h->root.type = bfd_link_hash_new;
1285 h->root.u.undef.abfd = NULL;
1286 }
1287 return TRUE;
1288 }
580a2b6e
L
1289 }
1290
4c34aff8
AM
1291 /* Check TLS symbols. We don't check undefined symbols introduced
1292 by "ld -u" which have no type (and oldbfd NULL), and we don't
1293 check symbols from plugins because they also have no type. */
1294 if (oldbfd != NULL
1295 && (oldbfd->flags & BFD_PLUGIN) == 0
1296 && (abfd->flags & BFD_PLUGIN) == 0
1297 && ELF_ST_TYPE (sym->st_info) != h->type
1298 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1299 {
1300 bfd *ntbfd, *tbfd;
1301 bfd_boolean ntdef, tdef;
1302 asection *ntsec, *tsec;
1303
1304 if (h->type == STT_TLS)
1305 {
3b36f7e6 1306 ntbfd = abfd;
7479dfd4
L
1307 ntsec = sec;
1308 ntdef = newdef;
1309 tbfd = oldbfd;
1310 tsec = oldsec;
1311 tdef = olddef;
1312 }
1313 else
1314 {
1315 ntbfd = oldbfd;
1316 ntsec = oldsec;
1317 ntdef = olddef;
1318 tbfd = abfd;
1319 tsec = sec;
1320 tdef = newdef;
1321 }
1322
1323 if (tdef && ntdef)
4eca0228 1324 _bfd_error_handler
695344c0 1325 /* xgettext:c-format */
191c0c42
AM
1326 (_("%s: TLS definition in %B section %A "
1327 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1328 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1329 else if (!tdef && !ntdef)
4eca0228 1330 _bfd_error_handler
695344c0 1331 /* xgettext:c-format */
191c0c42
AM
1332 (_("%s: TLS reference in %B "
1333 "mismatches non-TLS reference in %B"),
c08bb8dd 1334 h->root.root.string, tbfd, ntbfd);
7479dfd4 1335 else if (tdef)
4eca0228 1336 _bfd_error_handler
695344c0 1337 /* xgettext:c-format */
191c0c42
AM
1338 (_("%s: TLS definition in %B section %A "
1339 "mismatches non-TLS reference in %B"),
c08bb8dd 1340 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1341 else
4eca0228 1342 _bfd_error_handler
695344c0 1343 /* xgettext:c-format */
191c0c42
AM
1344 (_("%s: TLS reference in %B "
1345 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1346 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1347
1348 bfd_set_error (bfd_error_bad_value);
1349 return FALSE;
1350 }
1351
45d6a902
AM
1352 /* If the old symbol has non-default visibility, we ignore the new
1353 definition from a dynamic object. */
1354 if (newdyn
9c7a29a3 1355 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1356 && !bfd_is_und_section (sec))
1357 {
1358 *skip = TRUE;
1359 /* Make sure this symbol is dynamic. */
f5385ebf 1360 h->ref_dynamic = 1;
90c984fc 1361 hi->ref_dynamic = 1;
45d6a902
AM
1362 /* A protected symbol has external availability. Make sure it is
1363 recorded as dynamic.
1364
1365 FIXME: Should we check type and size for protected symbol? */
1366 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1367 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1368 else
1369 return TRUE;
1370 }
1371 else if (!newdyn
9c7a29a3 1372 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1373 && h->def_dynamic)
45d6a902
AM
1374 {
1375 /* If the new symbol with non-default visibility comes from a
1376 relocatable file and the old definition comes from a dynamic
1377 object, we remove the old definition. */
6c9b78e6 1378 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1379 {
1380 /* Handle the case where the old dynamic definition is
1381 default versioned. We need to copy the symbol info from
1382 the symbol with default version to the normal one if it
1383 was referenced before. */
1384 if (h->ref_regular)
1385 {
6c9b78e6 1386 hi->root.type = h->root.type;
d2dee3b2 1387 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1388 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1389
6c9b78e6 1390 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1391 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1392 {
aed81c4e
MR
1393 /* If the new symbol is hidden or internal, completely undo
1394 any dynamic link state. */
1395 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1396 h->forced_local = 0;
1397 h->ref_dynamic = 0;
d2dee3b2
L
1398 }
1399 else
aed81c4e
MR
1400 h->ref_dynamic = 1;
1401
1402 h->def_dynamic = 0;
aed81c4e
MR
1403 /* FIXME: Should we check type and size for protected symbol? */
1404 h->size = 0;
1405 h->type = 0;
1406
6c9b78e6 1407 h = hi;
d2dee3b2
L
1408 }
1409 else
6c9b78e6 1410 h = hi;
d2dee3b2 1411 }
1de1a317 1412
f5eda473
AM
1413 /* If the old symbol was undefined before, then it will still be
1414 on the undefs list. If the new symbol is undefined or
1415 common, we can't make it bfd_link_hash_new here, because new
1416 undefined or common symbols will be added to the undefs list
1417 by _bfd_generic_link_add_one_symbol. Symbols may not be
1418 added twice to the undefs list. Also, if the new symbol is
1419 undefweak then we don't want to lose the strong undef. */
1420 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1421 {
1de1a317 1422 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1423 h->root.u.undef.abfd = abfd;
1424 }
1425 else
1426 {
1427 h->root.type = bfd_link_hash_new;
1428 h->root.u.undef.abfd = NULL;
1429 }
1430
f5eda473 1431 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1432 {
f5eda473
AM
1433 /* If the new symbol is hidden or internal, completely undo
1434 any dynamic link state. */
1435 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1436 h->forced_local = 0;
1437 h->ref_dynamic = 0;
45d6a902 1438 }
f5eda473
AM
1439 else
1440 h->ref_dynamic = 1;
1441 h->def_dynamic = 0;
45d6a902
AM
1442 /* FIXME: Should we check type and size for protected symbol? */
1443 h->size = 0;
1444 h->type = 0;
1445 return TRUE;
1446 }
14a793b2 1447
15b43f48
AM
1448 /* If a new weak symbol definition comes from a regular file and the
1449 old symbol comes from a dynamic library, we treat the new one as
1450 strong. Similarly, an old weak symbol definition from a regular
1451 file is treated as strong when the new symbol comes from a dynamic
1452 library. Further, an old weak symbol from a dynamic library is
1453 treated as strong if the new symbol is from a dynamic library.
1454 This reflects the way glibc's ld.so works.
1455
1456 Do this before setting *type_change_ok or *size_change_ok so that
1457 we warn properly when dynamic library symbols are overridden. */
1458
1459 if (newdef && !newdyn && olddyn)
0f8a2703 1460 newweak = FALSE;
15b43f48 1461 if (olddef && newdyn)
0f8a2703
AM
1462 oldweak = FALSE;
1463
d334575b 1464 /* Allow changes between different types of function symbol. */
0a36a439 1465 if (newfunc && oldfunc)
fcb93ecf
PB
1466 *type_change_ok = TRUE;
1467
79349b09
AM
1468 /* It's OK to change the type if either the existing symbol or the
1469 new symbol is weak. A type change is also OK if the old symbol
1470 is undefined and the new symbol is defined. */
252b5132 1471
79349b09
AM
1472 if (oldweak
1473 || newweak
1474 || (newdef
1475 && h->root.type == bfd_link_hash_undefined))
1476 *type_change_ok = TRUE;
1477
1478 /* It's OK to change the size if either the existing symbol or the
1479 new symbol is weak, or if the old symbol is undefined. */
1480
1481 if (*type_change_ok
1482 || h->root.type == bfd_link_hash_undefined)
1483 *size_change_ok = TRUE;
45d6a902 1484
45d6a902
AM
1485 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1486 symbol, respectively, appears to be a common symbol in a dynamic
1487 object. If a symbol appears in an uninitialized section, and is
1488 not weak, and is not a function, then it may be a common symbol
1489 which was resolved when the dynamic object was created. We want
1490 to treat such symbols specially, because they raise special
1491 considerations when setting the symbol size: if the symbol
1492 appears as a common symbol in a regular object, and the size in
1493 the regular object is larger, we must make sure that we use the
1494 larger size. This problematic case can always be avoided in C,
1495 but it must be handled correctly when using Fortran shared
1496 libraries.
1497
1498 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1499 likewise for OLDDYNCOMMON and OLDDEF.
1500
1501 Note that this test is just a heuristic, and that it is quite
1502 possible to have an uninitialized symbol in a shared object which
1503 is really a definition, rather than a common symbol. This could
1504 lead to some minor confusion when the symbol really is a common
1505 symbol in some regular object. However, I think it will be
1506 harmless. */
1507
1508 if (newdyn
1509 && newdef
79349b09 1510 && !newweak
45d6a902
AM
1511 && (sec->flags & SEC_ALLOC) != 0
1512 && (sec->flags & SEC_LOAD) == 0
1513 && sym->st_size > 0
0a36a439 1514 && !newfunc)
45d6a902
AM
1515 newdyncommon = TRUE;
1516 else
1517 newdyncommon = FALSE;
1518
1519 if (olddyn
1520 && olddef
1521 && h->root.type == bfd_link_hash_defined
f5385ebf 1522 && h->def_dynamic
45d6a902
AM
1523 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1524 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1525 && h->size > 0
0a36a439 1526 && !oldfunc)
45d6a902
AM
1527 olddyncommon = TRUE;
1528 else
1529 olddyncommon = FALSE;
1530
a4d8e49b
L
1531 /* We now know everything about the old and new symbols. We ask the
1532 backend to check if we can merge them. */
5d13b3b3
AM
1533 if (bed->merge_symbol != NULL)
1534 {
1535 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1536 return FALSE;
1537 sec = *psec;
1538 }
a4d8e49b 1539
45d6a902
AM
1540 /* If both the old and the new symbols look like common symbols in a
1541 dynamic object, set the size of the symbol to the larger of the
1542 two. */
1543
1544 if (olddyncommon
1545 && newdyncommon
1546 && sym->st_size != h->size)
1547 {
1548 /* Since we think we have two common symbols, issue a multiple
1549 common warning if desired. Note that we only warn if the
1550 size is different. If the size is the same, we simply let
1551 the old symbol override the new one as normally happens with
1552 symbols defined in dynamic objects. */
1553
1a72702b
AM
1554 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1555 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1556 if (sym->st_size > h->size)
1557 h->size = sym->st_size;
252b5132 1558
45d6a902 1559 *size_change_ok = TRUE;
252b5132
RH
1560 }
1561
45d6a902
AM
1562 /* If we are looking at a dynamic object, and we have found a
1563 definition, we need to see if the symbol was already defined by
1564 some other object. If so, we want to use the existing
1565 definition, and we do not want to report a multiple symbol
1566 definition error; we do this by clobbering *PSEC to be
1567 bfd_und_section_ptr.
1568
1569 We treat a common symbol as a definition if the symbol in the
1570 shared library is a function, since common symbols always
1571 represent variables; this can cause confusion in principle, but
1572 any such confusion would seem to indicate an erroneous program or
1573 shared library. We also permit a common symbol in a regular
8170f769 1574 object to override a weak symbol in a shared object. */
45d6a902
AM
1575
1576 if (newdyn
1577 && newdef
77cfaee6 1578 && (olddef
45d6a902 1579 || (h->root.type == bfd_link_hash_common
8170f769 1580 && (newweak || newfunc))))
45d6a902
AM
1581 {
1582 *override = TRUE;
1583 newdef = FALSE;
1584 newdyncommon = FALSE;
252b5132 1585
45d6a902
AM
1586 *psec = sec = bfd_und_section_ptr;
1587 *size_change_ok = TRUE;
252b5132 1588
45d6a902
AM
1589 /* If we get here when the old symbol is a common symbol, then
1590 we are explicitly letting it override a weak symbol or
1591 function in a dynamic object, and we don't want to warn about
1592 a type change. If the old symbol is a defined symbol, a type
1593 change warning may still be appropriate. */
252b5132 1594
45d6a902
AM
1595 if (h->root.type == bfd_link_hash_common)
1596 *type_change_ok = TRUE;
1597 }
1598
1599 /* Handle the special case of an old common symbol merging with a
1600 new symbol which looks like a common symbol in a shared object.
1601 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1602 common symbol, and let _bfd_generic_link_add_one_symbol do the
1603 right thing. */
45d6a902
AM
1604
1605 if (newdyncommon
1606 && h->root.type == bfd_link_hash_common)
1607 {
1608 *override = TRUE;
1609 newdef = FALSE;
1610 newdyncommon = FALSE;
1611 *pvalue = sym->st_size;
a4d8e49b 1612 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1613 *size_change_ok = TRUE;
1614 }
1615
c5e2cead 1616 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1617 if (newdef && olddef && newweak)
54ac0771 1618 {
35ed3f94 1619 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1620 if (!(oldbfd != NULL
1621 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1622 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1623 {
1624 newdef = FALSE;
1625 *skip = TRUE;
1626 }
54ac0771
L
1627
1628 /* Merge st_other. If the symbol already has a dynamic index,
1629 but visibility says it should not be visible, turn it into a
1630 local symbol. */
b8417128 1631 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1632 if (h->dynindx != -1)
1633 switch (ELF_ST_VISIBILITY (h->other))
1634 {
1635 case STV_INTERNAL:
1636 case STV_HIDDEN:
1637 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1638 break;
1639 }
1640 }
c5e2cead 1641
45d6a902
AM
1642 /* If the old symbol is from a dynamic object, and the new symbol is
1643 a definition which is not from a dynamic object, then the new
1644 symbol overrides the old symbol. Symbols from regular files
1645 always take precedence over symbols from dynamic objects, even if
1646 they are defined after the dynamic object in the link.
1647
1648 As above, we again permit a common symbol in a regular object to
1649 override a definition in a shared object if the shared object
0f8a2703 1650 symbol is a function or is weak. */
45d6a902
AM
1651
1652 flip = NULL;
77cfaee6 1653 if (!newdyn
45d6a902
AM
1654 && (newdef
1655 || (bfd_is_com_section (sec)
0a36a439 1656 && (oldweak || oldfunc)))
45d6a902
AM
1657 && olddyn
1658 && olddef
f5385ebf 1659 && h->def_dynamic)
45d6a902
AM
1660 {
1661 /* Change the hash table entry to undefined, and let
1662 _bfd_generic_link_add_one_symbol do the right thing with the
1663 new definition. */
1664
1665 h->root.type = bfd_link_hash_undefined;
1666 h->root.u.undef.abfd = h->root.u.def.section->owner;
1667 *size_change_ok = TRUE;
1668
1669 olddef = FALSE;
1670 olddyncommon = FALSE;
1671
1672 /* We again permit a type change when a common symbol may be
1673 overriding a function. */
1674
1675 if (bfd_is_com_section (sec))
0a36a439
L
1676 {
1677 if (oldfunc)
1678 {
1679 /* If a common symbol overrides a function, make sure
1680 that it isn't defined dynamically nor has type
1681 function. */
1682 h->def_dynamic = 0;
1683 h->type = STT_NOTYPE;
1684 }
1685 *type_change_ok = TRUE;
1686 }
45d6a902 1687
6c9b78e6
AM
1688 if (hi->root.type == bfd_link_hash_indirect)
1689 flip = hi;
45d6a902
AM
1690 else
1691 /* This union may have been set to be non-NULL when this symbol
1692 was seen in a dynamic object. We must force the union to be
1693 NULL, so that it is correct for a regular symbol. */
1694 h->verinfo.vertree = NULL;
1695 }
1696
1697 /* Handle the special case of a new common symbol merging with an
1698 old symbol that looks like it might be a common symbol defined in
1699 a shared object. Note that we have already handled the case in
1700 which a new common symbol should simply override the definition
1701 in the shared library. */
1702
1703 if (! newdyn
1704 && bfd_is_com_section (sec)
1705 && olddyncommon)
1706 {
1707 /* It would be best if we could set the hash table entry to a
1708 common symbol, but we don't know what to use for the section
1709 or the alignment. */
1a72702b
AM
1710 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1711 bfd_link_hash_common, sym->st_size);
45d6a902 1712
4cc11e76 1713 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1714 larger, pretend that the new symbol has its size. */
1715
1716 if (h->size > *pvalue)
1717 *pvalue = h->size;
1718
af44c138
L
1719 /* We need to remember the alignment required by the symbol
1720 in the dynamic object. */
1721 BFD_ASSERT (pold_alignment);
1722 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1723
1724 olddef = FALSE;
1725 olddyncommon = FALSE;
1726
1727 h->root.type = bfd_link_hash_undefined;
1728 h->root.u.undef.abfd = h->root.u.def.section->owner;
1729
1730 *size_change_ok = TRUE;
1731 *type_change_ok = TRUE;
1732
6c9b78e6
AM
1733 if (hi->root.type == bfd_link_hash_indirect)
1734 flip = hi;
45d6a902
AM
1735 else
1736 h->verinfo.vertree = NULL;
1737 }
1738
1739 if (flip != NULL)
1740 {
1741 /* Handle the case where we had a versioned symbol in a dynamic
1742 library and now find a definition in a normal object. In this
1743 case, we make the versioned symbol point to the normal one. */
45d6a902 1744 flip->root.type = h->root.type;
00cbee0a 1745 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1746 h->root.type = bfd_link_hash_indirect;
1747 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1748 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1749 if (h->def_dynamic)
45d6a902 1750 {
f5385ebf
AM
1751 h->def_dynamic = 0;
1752 flip->ref_dynamic = 1;
45d6a902
AM
1753 }
1754 }
1755
45d6a902
AM
1756 return TRUE;
1757}
1758
1759/* This function is called to create an indirect symbol from the
1760 default for the symbol with the default version if needed. The
4f3fedcf 1761 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1762 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1763
28caa186 1764static bfd_boolean
268b6b39
AM
1765_bfd_elf_add_default_symbol (bfd *abfd,
1766 struct bfd_link_info *info,
1767 struct elf_link_hash_entry *h,
1768 const char *name,
1769 Elf_Internal_Sym *sym,
4f3fedcf
AM
1770 asection *sec,
1771 bfd_vma value,
1772 bfd **poldbfd,
e3c9d234 1773 bfd_boolean *dynsym)
45d6a902
AM
1774{
1775 bfd_boolean type_change_ok;
1776 bfd_boolean size_change_ok;
1777 bfd_boolean skip;
1778 char *shortname;
1779 struct elf_link_hash_entry *hi;
1780 struct bfd_link_hash_entry *bh;
9c5bfbb7 1781 const struct elf_backend_data *bed;
45d6a902
AM
1782 bfd_boolean collect;
1783 bfd_boolean dynamic;
e3c9d234 1784 bfd_boolean override;
45d6a902
AM
1785 char *p;
1786 size_t len, shortlen;
ffd65175 1787 asection *tmp_sec;
6e33951e 1788 bfd_boolean matched;
45d6a902 1789
422f1182
L
1790 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1791 return TRUE;
1792
45d6a902
AM
1793 /* If this symbol has a version, and it is the default version, we
1794 create an indirect symbol from the default name to the fully
1795 decorated name. This will cause external references which do not
1796 specify a version to be bound to this version of the symbol. */
1797 p = strchr (name, ELF_VER_CHR);
422f1182
L
1798 if (h->versioned == unknown)
1799 {
1800 if (p == NULL)
1801 {
1802 h->versioned = unversioned;
1803 return TRUE;
1804 }
1805 else
1806 {
1807 if (p[1] != ELF_VER_CHR)
1808 {
1809 h->versioned = versioned_hidden;
1810 return TRUE;
1811 }
1812 else
1813 h->versioned = versioned;
1814 }
1815 }
4373f8af
L
1816 else
1817 {
1818 /* PR ld/19073: We may see an unversioned definition after the
1819 default version. */
1820 if (p == NULL)
1821 return TRUE;
1822 }
45d6a902 1823
45d6a902
AM
1824 bed = get_elf_backend_data (abfd);
1825 collect = bed->collect;
1826 dynamic = (abfd->flags & DYNAMIC) != 0;
1827
1828 shortlen = p - name;
a50b1753 1829 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1830 if (shortname == NULL)
1831 return FALSE;
1832 memcpy (shortname, name, shortlen);
1833 shortname[shortlen] = '\0';
1834
1835 /* We are going to create a new symbol. Merge it with any existing
1836 symbol with this name. For the purposes of the merge, act as
1837 though we were defining the symbol we just defined, although we
1838 actually going to define an indirect symbol. */
1839 type_change_ok = FALSE;
1840 size_change_ok = FALSE;
6e33951e 1841 matched = TRUE;
ffd65175
AM
1842 tmp_sec = sec;
1843 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1844 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1845 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1846 return FALSE;
1847
1848 if (skip)
1849 goto nondefault;
1850
5b677558
AM
1851 if (hi->def_regular)
1852 {
1853 /* If the undecorated symbol will have a version added by a
1854 script different to H, then don't indirect to/from the
1855 undecorated symbol. This isn't ideal because we may not yet
1856 have seen symbol versions, if given by a script on the
1857 command line rather than via --version-script. */
1858 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1859 {
1860 bfd_boolean hide;
1861
1862 hi->verinfo.vertree
1863 = bfd_find_version_for_sym (info->version_info,
1864 hi->root.root.string, &hide);
1865 if (hi->verinfo.vertree != NULL && hide)
1866 {
1867 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1868 goto nondefault;
1869 }
1870 }
1871 if (hi->verinfo.vertree != NULL
1872 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1873 goto nondefault;
1874 }
1875
45d6a902
AM
1876 if (! override)
1877 {
c6e8a9a8 1878 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1879 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1880 {
1881 bh = &hi->root;
1882 if (! (_bfd_generic_link_add_one_symbol
1883 (info, abfd, shortname, BSF_INDIRECT,
1884 bfd_ind_section_ptr,
1885 0, name, FALSE, collect, &bh)))
1886 return FALSE;
1887 hi = (struct elf_link_hash_entry *) bh;
1888 }
45d6a902
AM
1889 }
1890 else
1891 {
1892 /* In this case the symbol named SHORTNAME is overriding the
1893 indirect symbol we want to add. We were planning on making
1894 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1895 is the name without a version. NAME is the fully versioned
1896 name, and it is the default version.
1897
1898 Overriding means that we already saw a definition for the
1899 symbol SHORTNAME in a regular object, and it is overriding
1900 the symbol defined in the dynamic object.
1901
1902 When this happens, we actually want to change NAME, the
1903 symbol we just added, to refer to SHORTNAME. This will cause
1904 references to NAME in the shared object to become references
1905 to SHORTNAME in the regular object. This is what we expect
1906 when we override a function in a shared object: that the
1907 references in the shared object will be mapped to the
1908 definition in the regular object. */
1909
1910 while (hi->root.type == bfd_link_hash_indirect
1911 || hi->root.type == bfd_link_hash_warning)
1912 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1913
1914 h->root.type = bfd_link_hash_indirect;
1915 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1916 if (h->def_dynamic)
45d6a902 1917 {
f5385ebf
AM
1918 h->def_dynamic = 0;
1919 hi->ref_dynamic = 1;
1920 if (hi->ref_regular
1921 || hi->def_regular)
45d6a902 1922 {
c152c796 1923 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1924 return FALSE;
1925 }
1926 }
1927
1928 /* Now set HI to H, so that the following code will set the
1929 other fields correctly. */
1930 hi = h;
1931 }
1932
fab4a87f
L
1933 /* Check if HI is a warning symbol. */
1934 if (hi->root.type == bfd_link_hash_warning)
1935 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1936
45d6a902
AM
1937 /* If there is a duplicate definition somewhere, then HI may not
1938 point to an indirect symbol. We will have reported an error to
1939 the user in that case. */
1940
1941 if (hi->root.type == bfd_link_hash_indirect)
1942 {
1943 struct elf_link_hash_entry *ht;
1944
45d6a902 1945 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1946 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1947
68c88cd4
AM
1948 /* A reference to the SHORTNAME symbol from a dynamic library
1949 will be satisfied by the versioned symbol at runtime. In
1950 effect, we have a reference to the versioned symbol. */
1951 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1952 hi->dynamic_def |= ht->dynamic_def;
1953
45d6a902
AM
1954 /* See if the new flags lead us to realize that the symbol must
1955 be dynamic. */
1956 if (! *dynsym)
1957 {
1958 if (! dynamic)
1959 {
0e1862bb 1960 if (! bfd_link_executable (info)
90c984fc 1961 || hi->def_dynamic
f5385ebf 1962 || hi->ref_dynamic)
45d6a902
AM
1963 *dynsym = TRUE;
1964 }
1965 else
1966 {
f5385ebf 1967 if (hi->ref_regular)
45d6a902
AM
1968 *dynsym = TRUE;
1969 }
1970 }
1971 }
1972
1973 /* We also need to define an indirection from the nondefault version
1974 of the symbol. */
1975
1976nondefault:
1977 len = strlen (name);
a50b1753 1978 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1979 if (shortname == NULL)
1980 return FALSE;
1981 memcpy (shortname, name, shortlen);
1982 memcpy (shortname + shortlen, p + 1, len - shortlen);
1983
1984 /* Once again, merge with any existing symbol. */
1985 type_change_ok = FALSE;
1986 size_change_ok = FALSE;
ffd65175
AM
1987 tmp_sec = sec;
1988 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1989 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1990 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1991 return FALSE;
1992
1993 if (skip)
1994 return TRUE;
1995
1996 if (override)
1997 {
1998 /* Here SHORTNAME is a versioned name, so we don't expect to see
1999 the type of override we do in the case above unless it is
4cc11e76 2000 overridden by a versioned definition. */
45d6a902
AM
2001 if (hi->root.type != bfd_link_hash_defined
2002 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2003 _bfd_error_handler
695344c0 2004 /* xgettext:c-format */
d003868e
AM
2005 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2006 abfd, shortname);
45d6a902
AM
2007 }
2008 else
2009 {
2010 bh = &hi->root;
2011 if (! (_bfd_generic_link_add_one_symbol
2012 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2013 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2014 return FALSE;
2015 hi = (struct elf_link_hash_entry *) bh;
2016
2017 /* If there is a duplicate definition somewhere, then HI may not
2018 point to an indirect symbol. We will have reported an error
2019 to the user in that case. */
2020
2021 if (hi->root.type == bfd_link_hash_indirect)
2022 {
fcfa13d2 2023 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2024 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2025 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2026
2027 /* See if the new flags lead us to realize that the symbol
2028 must be dynamic. */
2029 if (! *dynsym)
2030 {
2031 if (! dynamic)
2032 {
0e1862bb 2033 if (! bfd_link_executable (info)
f5385ebf 2034 || hi->ref_dynamic)
45d6a902
AM
2035 *dynsym = TRUE;
2036 }
2037 else
2038 {
f5385ebf 2039 if (hi->ref_regular)
45d6a902
AM
2040 *dynsym = TRUE;
2041 }
2042 }
2043 }
2044 }
2045
2046 return TRUE;
2047}
2048\f
2049/* This routine is used to export all defined symbols into the dynamic
2050 symbol table. It is called via elf_link_hash_traverse. */
2051
28caa186 2052static bfd_boolean
268b6b39 2053_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2054{
a50b1753 2055 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2056
2057 /* Ignore indirect symbols. These are added by the versioning code. */
2058 if (h->root.type == bfd_link_hash_indirect)
2059 return TRUE;
2060
7686d77d
AM
2061 /* Ignore this if we won't export it. */
2062 if (!eif->info->export_dynamic && !h->dynamic)
2063 return TRUE;
45d6a902
AM
2064
2065 if (h->dynindx == -1
fd91d419
L
2066 && (h->def_regular || h->ref_regular)
2067 && ! bfd_hide_sym_by_version (eif->info->version_info,
2068 h->root.root.string))
45d6a902 2069 {
fd91d419 2070 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2071 {
fd91d419
L
2072 eif->failed = TRUE;
2073 return FALSE;
45d6a902
AM
2074 }
2075 }
2076
2077 return TRUE;
2078}
2079\f
2080/* Look through the symbols which are defined in other shared
2081 libraries and referenced here. Update the list of version
2082 dependencies. This will be put into the .gnu.version_r section.
2083 This function is called via elf_link_hash_traverse. */
2084
28caa186 2085static bfd_boolean
268b6b39
AM
2086_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2087 void *data)
45d6a902 2088{
a50b1753 2089 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2090 Elf_Internal_Verneed *t;
2091 Elf_Internal_Vernaux *a;
2092 bfd_size_type amt;
2093
45d6a902
AM
2094 /* We only care about symbols defined in shared objects with version
2095 information. */
f5385ebf
AM
2096 if (!h->def_dynamic
2097 || h->def_regular
45d6a902 2098 || h->dynindx == -1
7b20f099
AM
2099 || h->verinfo.verdef == NULL
2100 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2101 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2102 return TRUE;
2103
2104 /* See if we already know about this version. */
28caa186
AM
2105 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2106 t != NULL;
2107 t = t->vn_nextref)
45d6a902
AM
2108 {
2109 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2110 continue;
2111
2112 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2113 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2114 return TRUE;
2115
2116 break;
2117 }
2118
2119 /* This is a new version. Add it to tree we are building. */
2120
2121 if (t == NULL)
2122 {
2123 amt = sizeof *t;
a50b1753 2124 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2125 if (t == NULL)
2126 {
2127 rinfo->failed = TRUE;
2128 return FALSE;
2129 }
2130
2131 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2132 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2133 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2134 }
2135
2136 amt = sizeof *a;
a50b1753 2137 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2138 if (a == NULL)
2139 {
2140 rinfo->failed = TRUE;
2141 return FALSE;
2142 }
45d6a902
AM
2143
2144 /* Note that we are copying a string pointer here, and testing it
2145 above. If bfd_elf_string_from_elf_section is ever changed to
2146 discard the string data when low in memory, this will have to be
2147 fixed. */
2148 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2149
2150 a->vna_flags = h->verinfo.verdef->vd_flags;
2151 a->vna_nextptr = t->vn_auxptr;
2152
2153 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2154 ++rinfo->vers;
2155
2156 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2157
2158 t->vn_auxptr = a;
2159
2160 return TRUE;
2161}
2162
2163/* Figure out appropriate versions for all the symbols. We may not
2164 have the version number script until we have read all of the input
2165 files, so until that point we don't know which symbols should be
2166 local. This function is called via elf_link_hash_traverse. */
2167
28caa186 2168static bfd_boolean
268b6b39 2169_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2170{
28caa186 2171 struct elf_info_failed *sinfo;
45d6a902 2172 struct bfd_link_info *info;
9c5bfbb7 2173 const struct elf_backend_data *bed;
45d6a902
AM
2174 struct elf_info_failed eif;
2175 char *p;
45d6a902 2176
a50b1753 2177 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2178 info = sinfo->info;
2179
45d6a902
AM
2180 /* Fix the symbol flags. */
2181 eif.failed = FALSE;
2182 eif.info = info;
2183 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2184 {
2185 if (eif.failed)
2186 sinfo->failed = TRUE;
2187 return FALSE;
2188 }
2189
2190 /* We only need version numbers for symbols defined in regular
2191 objects. */
f5385ebf 2192 if (!h->def_regular)
45d6a902
AM
2193 return TRUE;
2194
28caa186 2195 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2196 p = strchr (h->root.root.string, ELF_VER_CHR);
2197 if (p != NULL && h->verinfo.vertree == NULL)
2198 {
2199 struct bfd_elf_version_tree *t;
45d6a902 2200
45d6a902
AM
2201 ++p;
2202 if (*p == ELF_VER_CHR)
6e33951e 2203 ++p;
45d6a902
AM
2204
2205 /* If there is no version string, we can just return out. */
2206 if (*p == '\0')
6e33951e 2207 return TRUE;
45d6a902
AM
2208
2209 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2210 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2211 {
2212 if (strcmp (t->name, p) == 0)
2213 {
2214 size_t len;
2215 char *alc;
2216 struct bfd_elf_version_expr *d;
2217
2218 len = p - h->root.root.string;
a50b1753 2219 alc = (char *) bfd_malloc (len);
45d6a902 2220 if (alc == NULL)
14b1c01e
AM
2221 {
2222 sinfo->failed = TRUE;
2223 return FALSE;
2224 }
45d6a902
AM
2225 memcpy (alc, h->root.root.string, len - 1);
2226 alc[len - 1] = '\0';
2227 if (alc[len - 2] == ELF_VER_CHR)
2228 alc[len - 2] = '\0';
2229
2230 h->verinfo.vertree = t;
2231 t->used = TRUE;
2232 d = NULL;
2233
108ba305
JJ
2234 if (t->globals.list != NULL)
2235 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2236
2237 /* See if there is anything to force this symbol to
2238 local scope. */
108ba305 2239 if (d == NULL && t->locals.list != NULL)
45d6a902 2240 {
108ba305
JJ
2241 d = (*t->match) (&t->locals, NULL, alc);
2242 if (d != NULL
2243 && h->dynindx != -1
108ba305
JJ
2244 && ! info->export_dynamic)
2245 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2246 }
2247
2248 free (alc);
2249 break;
2250 }
2251 }
2252
2253 /* If we are building an application, we need to create a
2254 version node for this version. */
0e1862bb 2255 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2256 {
2257 struct bfd_elf_version_tree **pp;
2258 int version_index;
2259
2260 /* If we aren't going to export this symbol, we don't need
2261 to worry about it. */
2262 if (h->dynindx == -1)
2263 return TRUE;
2264
ef53be89
AM
2265 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2266 sizeof *t);
45d6a902
AM
2267 if (t == NULL)
2268 {
2269 sinfo->failed = TRUE;
2270 return FALSE;
2271 }
2272
45d6a902 2273 t->name = p;
45d6a902
AM
2274 t->name_indx = (unsigned int) -1;
2275 t->used = TRUE;
2276
2277 version_index = 1;
2278 /* Don't count anonymous version tag. */
fd91d419
L
2279 if (sinfo->info->version_info != NULL
2280 && sinfo->info->version_info->vernum == 0)
45d6a902 2281 version_index = 0;
fd91d419
L
2282 for (pp = &sinfo->info->version_info;
2283 *pp != NULL;
2284 pp = &(*pp)->next)
45d6a902
AM
2285 ++version_index;
2286 t->vernum = version_index;
2287
2288 *pp = t;
2289
2290 h->verinfo.vertree = t;
2291 }
2292 else if (t == NULL)
2293 {
2294 /* We could not find the version for a symbol when
2295 generating a shared archive. Return an error. */
4eca0228 2296 _bfd_error_handler
695344c0 2297 /* xgettext:c-format */
c55fe096 2298 (_("%B: version node not found for symbol %s"),
28caa186 2299 info->output_bfd, h->root.root.string);
45d6a902
AM
2300 bfd_set_error (bfd_error_bad_value);
2301 sinfo->failed = TRUE;
2302 return FALSE;
2303 }
45d6a902
AM
2304 }
2305
2306 /* If we don't have a version for this symbol, see if we can find
2307 something. */
fd91d419 2308 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2309 {
1e8fa21e 2310 bfd_boolean hide;
ae5a3597 2311
fd91d419
L
2312 h->verinfo.vertree
2313 = bfd_find_version_for_sym (sinfo->info->version_info,
2314 h->root.root.string, &hide);
1e8fa21e
AM
2315 if (h->verinfo.vertree != NULL && hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2317 }
2318
2319 return TRUE;
2320}
2321\f
45d6a902
AM
2322/* Read and swap the relocs from the section indicated by SHDR. This
2323 may be either a REL or a RELA section. The relocations are
2324 translated into RELA relocations and stored in INTERNAL_RELOCS,
2325 which should have already been allocated to contain enough space.
2326 The EXTERNAL_RELOCS are a buffer where the external form of the
2327 relocations should be stored.
2328
2329 Returns FALSE if something goes wrong. */
2330
2331static bfd_boolean
268b6b39 2332elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2333 asection *sec,
268b6b39
AM
2334 Elf_Internal_Shdr *shdr,
2335 void *external_relocs,
2336 Elf_Internal_Rela *internal_relocs)
45d6a902 2337{
9c5bfbb7 2338 const struct elf_backend_data *bed;
268b6b39 2339 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2340 const bfd_byte *erela;
2341 const bfd_byte *erelaend;
2342 Elf_Internal_Rela *irela;
243ef1e0
L
2343 Elf_Internal_Shdr *symtab_hdr;
2344 size_t nsyms;
45d6a902 2345
45d6a902
AM
2346 /* Position ourselves at the start of the section. */
2347 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2348 return FALSE;
2349
2350 /* Read the relocations. */
2351 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2352 return FALSE;
2353
243ef1e0 2354 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2355 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2356
45d6a902
AM
2357 bed = get_elf_backend_data (abfd);
2358
2359 /* Convert the external relocations to the internal format. */
2360 if (shdr->sh_entsize == bed->s->sizeof_rel)
2361 swap_in = bed->s->swap_reloc_in;
2362 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2363 swap_in = bed->s->swap_reloca_in;
2364 else
2365 {
2366 bfd_set_error (bfd_error_wrong_format);
2367 return FALSE;
2368 }
2369
a50b1753 2370 erela = (const bfd_byte *) external_relocs;
51992aec 2371 erelaend = erela + shdr->sh_size;
45d6a902
AM
2372 irela = internal_relocs;
2373 while (erela < erelaend)
2374 {
243ef1e0
L
2375 bfd_vma r_symndx;
2376
45d6a902 2377 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2378 r_symndx = ELF32_R_SYM (irela->r_info);
2379 if (bed->s->arch_size == 64)
2380 r_symndx >>= 24;
ce98a316
NC
2381 if (nsyms > 0)
2382 {
2383 if ((size_t) r_symndx >= nsyms)
2384 {
4eca0228 2385 _bfd_error_handler
695344c0 2386 /* xgettext:c-format */
ce98a316
NC
2387 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2388 " for offset 0x%lx in section `%A'"),
c08bb8dd
AM
2389 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2390 irela->r_offset, sec);
ce98a316
NC
2391 bfd_set_error (bfd_error_bad_value);
2392 return FALSE;
2393 }
2394 }
cf35638d 2395 else if (r_symndx != STN_UNDEF)
243ef1e0 2396 {
4eca0228 2397 _bfd_error_handler
695344c0 2398 /* xgettext:c-format */
c08bb8dd
AM
2399 (_("%B: non-zero symbol index (0x%lx)"
2400 " for offset 0x%lx in section `%A'"
ce98a316 2401 " when the object file has no symbol table"),
c08bb8dd
AM
2402 abfd, (unsigned long) r_symndx, (unsigned long) nsyms,
2403 irela->r_offset, sec);
243ef1e0
L
2404 bfd_set_error (bfd_error_bad_value);
2405 return FALSE;
2406 }
45d6a902
AM
2407 irela += bed->s->int_rels_per_ext_rel;
2408 erela += shdr->sh_entsize;
2409 }
2410
2411 return TRUE;
2412}
2413
2414/* Read and swap the relocs for a section O. They may have been
2415 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2416 not NULL, they are used as buffers to read into. They are known to
2417 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2418 the return value is allocated using either malloc or bfd_alloc,
2419 according to the KEEP_MEMORY argument. If O has two relocation
2420 sections (both REL and RELA relocations), then the REL_HDR
2421 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2422 RELA_HDR relocations. */
45d6a902
AM
2423
2424Elf_Internal_Rela *
268b6b39
AM
2425_bfd_elf_link_read_relocs (bfd *abfd,
2426 asection *o,
2427 void *external_relocs,
2428 Elf_Internal_Rela *internal_relocs,
2429 bfd_boolean keep_memory)
45d6a902 2430{
268b6b39 2431 void *alloc1 = NULL;
45d6a902 2432 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2433 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2434 struct bfd_elf_section_data *esdo = elf_section_data (o);
2435 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2436
d4730f92
BS
2437 if (esdo->relocs != NULL)
2438 return esdo->relocs;
45d6a902
AM
2439
2440 if (o->reloc_count == 0)
2441 return NULL;
2442
45d6a902
AM
2443 if (internal_relocs == NULL)
2444 {
2445 bfd_size_type size;
2446
2447 size = o->reloc_count;
2448 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2449 if (keep_memory)
a50b1753 2450 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2451 else
a50b1753 2452 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2453 if (internal_relocs == NULL)
2454 goto error_return;
2455 }
2456
2457 if (external_relocs == NULL)
2458 {
d4730f92
BS
2459 bfd_size_type size = 0;
2460
2461 if (esdo->rel.hdr)
2462 size += esdo->rel.hdr->sh_size;
2463 if (esdo->rela.hdr)
2464 size += esdo->rela.hdr->sh_size;
45d6a902 2465
268b6b39 2466 alloc1 = bfd_malloc (size);
45d6a902
AM
2467 if (alloc1 == NULL)
2468 goto error_return;
2469 external_relocs = alloc1;
2470 }
2471
d4730f92
BS
2472 internal_rela_relocs = internal_relocs;
2473 if (esdo->rel.hdr)
2474 {
2475 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2476 external_relocs,
2477 internal_relocs))
2478 goto error_return;
2479 external_relocs = (((bfd_byte *) external_relocs)
2480 + esdo->rel.hdr->sh_size);
2481 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2482 * bed->s->int_rels_per_ext_rel);
2483 }
2484
2485 if (esdo->rela.hdr
2486 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2487 external_relocs,
2488 internal_rela_relocs)))
45d6a902
AM
2489 goto error_return;
2490
2491 /* Cache the results for next time, if we can. */
2492 if (keep_memory)
d4730f92 2493 esdo->relocs = internal_relocs;
45d6a902
AM
2494
2495 if (alloc1 != NULL)
2496 free (alloc1);
2497
2498 /* Don't free alloc2, since if it was allocated we are passing it
2499 back (under the name of internal_relocs). */
2500
2501 return internal_relocs;
2502
2503 error_return:
2504 if (alloc1 != NULL)
2505 free (alloc1);
2506 if (alloc2 != NULL)
4dd07732
AM
2507 {
2508 if (keep_memory)
2509 bfd_release (abfd, alloc2);
2510 else
2511 free (alloc2);
2512 }
45d6a902
AM
2513 return NULL;
2514}
2515
2516/* Compute the size of, and allocate space for, REL_HDR which is the
2517 section header for a section containing relocations for O. */
2518
28caa186 2519static bfd_boolean
9eaff861
AO
2520_bfd_elf_link_size_reloc_section (bfd *abfd,
2521 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2522{
9eaff861 2523 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2524
2525 /* That allows us to calculate the size of the section. */
9eaff861 2526 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2527
2528 /* The contents field must last into write_object_contents, so we
2529 allocate it with bfd_alloc rather than malloc. Also since we
2530 cannot be sure that the contents will actually be filled in,
2531 we zero the allocated space. */
a50b1753 2532 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2533 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2534 return FALSE;
2535
d4730f92 2536 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2537 {
2538 struct elf_link_hash_entry **p;
2539
ca4be51c
AM
2540 p = ((struct elf_link_hash_entry **)
2541 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2542 if (p == NULL)
2543 return FALSE;
2544
d4730f92 2545 reldata->hashes = p;
45d6a902
AM
2546 }
2547
2548 return TRUE;
2549}
2550
2551/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2552 originated from the section given by INPUT_REL_HDR) to the
2553 OUTPUT_BFD. */
2554
2555bfd_boolean
268b6b39
AM
2556_bfd_elf_link_output_relocs (bfd *output_bfd,
2557 asection *input_section,
2558 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2559 Elf_Internal_Rela *internal_relocs,
2560 struct elf_link_hash_entry **rel_hash
2561 ATTRIBUTE_UNUSED)
45d6a902
AM
2562{
2563 Elf_Internal_Rela *irela;
2564 Elf_Internal_Rela *irelaend;
2565 bfd_byte *erel;
d4730f92 2566 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2567 asection *output_section;
9c5bfbb7 2568 const struct elf_backend_data *bed;
268b6b39 2569 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2570 struct bfd_elf_section_data *esdo;
45d6a902
AM
2571
2572 output_section = input_section->output_section;
45d6a902 2573
d4730f92
BS
2574 bed = get_elf_backend_data (output_bfd);
2575 esdo = elf_section_data (output_section);
2576 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2577 {
d4730f92
BS
2578 output_reldata = &esdo->rel;
2579 swap_out = bed->s->swap_reloc_out;
45d6a902 2580 }
d4730f92
BS
2581 else if (esdo->rela.hdr
2582 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2583 {
d4730f92
BS
2584 output_reldata = &esdo->rela;
2585 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2586 }
2587 else
2588 {
4eca0228 2589 _bfd_error_handler
695344c0 2590 /* xgettext:c-format */
d003868e
AM
2591 (_("%B: relocation size mismatch in %B section %A"),
2592 output_bfd, input_section->owner, input_section);
297d8443 2593 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2594 return FALSE;
2595 }
2596
d4730f92
BS
2597 erel = output_reldata->hdr->contents;
2598 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2599 irela = internal_relocs;
2600 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2601 * bed->s->int_rels_per_ext_rel);
2602 while (irela < irelaend)
2603 {
2604 (*swap_out) (output_bfd, irela, erel);
2605 irela += bed->s->int_rels_per_ext_rel;
2606 erel += input_rel_hdr->sh_entsize;
2607 }
2608
2609 /* Bump the counter, so that we know where to add the next set of
2610 relocations. */
d4730f92 2611 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2612
2613 return TRUE;
2614}
2615\f
508c3946
L
2616/* Make weak undefined symbols in PIE dynamic. */
2617
2618bfd_boolean
2619_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2620 struct elf_link_hash_entry *h)
2621{
0e1862bb 2622 if (bfd_link_pie (info)
508c3946
L
2623 && h->dynindx == -1
2624 && h->root.type == bfd_link_hash_undefweak)
2625 return bfd_elf_link_record_dynamic_symbol (info, h);
2626
2627 return TRUE;
2628}
2629
45d6a902
AM
2630/* Fix up the flags for a symbol. This handles various cases which
2631 can only be fixed after all the input files are seen. This is
2632 currently called by both adjust_dynamic_symbol and
2633 assign_sym_version, which is unnecessary but perhaps more robust in
2634 the face of future changes. */
2635
28caa186 2636static bfd_boolean
268b6b39
AM
2637_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2638 struct elf_info_failed *eif)
45d6a902 2639{
33774f08 2640 const struct elf_backend_data *bed;
508c3946 2641
45d6a902
AM
2642 /* If this symbol was mentioned in a non-ELF file, try to set
2643 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2644 permit a non-ELF file to correctly refer to a symbol defined in
2645 an ELF dynamic object. */
f5385ebf 2646 if (h->non_elf)
45d6a902
AM
2647 {
2648 while (h->root.type == bfd_link_hash_indirect)
2649 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2650
2651 if (h->root.type != bfd_link_hash_defined
2652 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2653 {
2654 h->ref_regular = 1;
2655 h->ref_regular_nonweak = 1;
2656 }
45d6a902
AM
2657 else
2658 {
2659 if (h->root.u.def.section->owner != NULL
2660 && (bfd_get_flavour (h->root.u.def.section->owner)
2661 == bfd_target_elf_flavour))
f5385ebf
AM
2662 {
2663 h->ref_regular = 1;
2664 h->ref_regular_nonweak = 1;
2665 }
45d6a902 2666 else
f5385ebf 2667 h->def_regular = 1;
45d6a902
AM
2668 }
2669
2670 if (h->dynindx == -1
f5385ebf
AM
2671 && (h->def_dynamic
2672 || h->ref_dynamic))
45d6a902 2673 {
c152c796 2674 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2675 {
2676 eif->failed = TRUE;
2677 return FALSE;
2678 }
2679 }
2680 }
2681 else
2682 {
f5385ebf 2683 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2684 was first seen in a non-ELF file. Fortunately, if the symbol
2685 was first seen in an ELF file, we're probably OK unless the
2686 symbol was defined in a non-ELF file. Catch that case here.
2687 FIXME: We're still in trouble if the symbol was first seen in
2688 a dynamic object, and then later in a non-ELF regular object. */
2689 if ((h->root.type == bfd_link_hash_defined
2690 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2691 && !h->def_regular
45d6a902
AM
2692 && (h->root.u.def.section->owner != NULL
2693 ? (bfd_get_flavour (h->root.u.def.section->owner)
2694 != bfd_target_elf_flavour)
2695 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2696 && !h->def_dynamic)))
2697 h->def_regular = 1;
45d6a902
AM
2698 }
2699
508c3946 2700 /* Backend specific symbol fixup. */
33774f08
AM
2701 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2702 if (bed->elf_backend_fixup_symbol
2703 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2704 return FALSE;
508c3946 2705
45d6a902
AM
2706 /* If this is a final link, and the symbol was defined as a common
2707 symbol in a regular object file, and there was no definition in
2708 any dynamic object, then the linker will have allocated space for
f5385ebf 2709 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2710 flag will not have been set. */
2711 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2712 && !h->def_regular
2713 && h->ref_regular
2714 && !h->def_dynamic
96f29d96 2715 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2716 h->def_regular = 1;
45d6a902 2717
4deb8f71
L
2718 /* If a weak undefined symbol has non-default visibility, we also
2719 hide it from the dynamic linker. */
2720 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2721 && h->root.type == bfd_link_hash_undefweak)
2722 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2723
2724 /* A hidden versioned symbol in executable should be forced local if
2725 it is is locally defined, not referenced by shared library and not
2726 exported. */
2727 else if (bfd_link_executable (eif->info)
2728 && h->versioned == versioned_hidden
2729 && !eif->info->export_dynamic
2730 && !h->dynamic
2731 && !h->ref_dynamic
2732 && h->def_regular)
2733 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2734
45d6a902
AM
2735 /* If -Bsymbolic was used (which means to bind references to global
2736 symbols to the definition within the shared object), and this
2737 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2738 need a PLT entry. Likewise, if the symbol has non-default
2739 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2740 will force it local. */
4deb8f71
L
2741 else if (h->needs_plt
2742 && bfd_link_pic (eif->info)
2743 && is_elf_hash_table (eif->info->hash)
2744 && (SYMBOLIC_BIND (eif->info, h)
2745 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2746 && h->def_regular)
45d6a902 2747 {
45d6a902
AM
2748 bfd_boolean force_local;
2749
45d6a902
AM
2750 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2751 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2752 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2753 }
2754
45d6a902
AM
2755 /* If this is a weak defined symbol in a dynamic object, and we know
2756 the real definition in the dynamic object, copy interesting flags
2757 over to the real definition. */
f6e332e6 2758 if (h->u.weakdef != NULL)
45d6a902 2759 {
45d6a902
AM
2760 /* If the real definition is defined by a regular object file,
2761 don't do anything special. See the longer description in
2762 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2763 if (h->u.weakdef->def_regular)
f6e332e6 2764 h->u.weakdef = NULL;
45d6a902 2765 else
a26587ba 2766 {
4e6b54a6
AM
2767 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2768
2769 while (h->root.type == bfd_link_hash_indirect)
2770 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2771
2772 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2773 || h->root.type == bfd_link_hash_defweak);
2774 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2775 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2776 || weakdef->root.type == bfd_link_hash_defweak);
2777 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2778 }
45d6a902
AM
2779 }
2780
2781 return TRUE;
2782}
2783
2784/* Make the backend pick a good value for a dynamic symbol. This is
2785 called via elf_link_hash_traverse, and also calls itself
2786 recursively. */
2787
28caa186 2788static bfd_boolean
268b6b39 2789_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2790{
a50b1753 2791 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2792 bfd *dynobj;
9c5bfbb7 2793 const struct elf_backend_data *bed;
45d6a902 2794
0eddce27 2795 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2796 return FALSE;
2797
45d6a902
AM
2798 /* Ignore indirect symbols. These are added by the versioning code. */
2799 if (h->root.type == bfd_link_hash_indirect)
2800 return TRUE;
2801
2802 /* Fix the symbol flags. */
2803 if (! _bfd_elf_fix_symbol_flags (h, eif))
2804 return FALSE;
2805
2806 /* If this symbol does not require a PLT entry, and it is not
2807 defined by a dynamic object, or is not referenced by a regular
2808 object, ignore it. We do have to handle a weak defined symbol,
2809 even if no regular object refers to it, if we decided to add it
2810 to the dynamic symbol table. FIXME: Do we normally need to worry
2811 about symbols which are defined by one dynamic object and
2812 referenced by another one? */
f5385ebf 2813 if (!h->needs_plt
91e21fb7 2814 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2815 && (h->def_regular
2816 || !h->def_dynamic
2817 || (!h->ref_regular
f6e332e6 2818 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2819 {
a6aa5195 2820 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2821 return TRUE;
2822 }
2823
2824 /* If we've already adjusted this symbol, don't do it again. This
2825 can happen via a recursive call. */
f5385ebf 2826 if (h->dynamic_adjusted)
45d6a902
AM
2827 return TRUE;
2828
2829 /* Don't look at this symbol again. Note that we must set this
2830 after checking the above conditions, because we may look at a
2831 symbol once, decide not to do anything, and then get called
2832 recursively later after REF_REGULAR is set below. */
f5385ebf 2833 h->dynamic_adjusted = 1;
45d6a902
AM
2834
2835 /* If this is a weak definition, and we know a real definition, and
2836 the real symbol is not itself defined by a regular object file,
2837 then get a good value for the real definition. We handle the
2838 real symbol first, for the convenience of the backend routine.
2839
2840 Note that there is a confusing case here. If the real definition
2841 is defined by a regular object file, we don't get the real symbol
2842 from the dynamic object, but we do get the weak symbol. If the
2843 processor backend uses a COPY reloc, then if some routine in the
2844 dynamic object changes the real symbol, we will not see that
2845 change in the corresponding weak symbol. This is the way other
2846 ELF linkers work as well, and seems to be a result of the shared
2847 library model.
2848
2849 I will clarify this issue. Most SVR4 shared libraries define the
2850 variable _timezone and define timezone as a weak synonym. The
2851 tzset call changes _timezone. If you write
2852 extern int timezone;
2853 int _timezone = 5;
2854 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2855 you might expect that, since timezone is a synonym for _timezone,
2856 the same number will print both times. However, if the processor
2857 backend uses a COPY reloc, then actually timezone will be copied
2858 into your process image, and, since you define _timezone
2859 yourself, _timezone will not. Thus timezone and _timezone will
2860 wind up at different memory locations. The tzset call will set
2861 _timezone, leaving timezone unchanged. */
2862
f6e332e6 2863 if (h->u.weakdef != NULL)
45d6a902 2864 {
ec24dc88
AM
2865 /* If we get to this point, there is an implicit reference to
2866 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2867 h->u.weakdef->ref_regular = 1;
45d6a902 2868
ec24dc88
AM
2869 /* Ensure that the backend adjust_dynamic_symbol function sees
2870 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2871 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2872 return FALSE;
2873 }
2874
2875 /* If a symbol has no type and no size and does not require a PLT
2876 entry, then we are probably about to do the wrong thing here: we
2877 are probably going to create a COPY reloc for an empty object.
2878 This case can arise when a shared object is built with assembly
2879 code, and the assembly code fails to set the symbol type. */
2880 if (h->size == 0
2881 && h->type == STT_NOTYPE
f5385ebf 2882 && !h->needs_plt)
4eca0228 2883 _bfd_error_handler
45d6a902
AM
2884 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2885 h->root.root.string);
2886
2887 dynobj = elf_hash_table (eif->info)->dynobj;
2888 bed = get_elf_backend_data (dynobj);
e7c33416 2889
45d6a902
AM
2890 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2891 {
2892 eif->failed = TRUE;
2893 return FALSE;
2894 }
2895
2896 return TRUE;
2897}
2898
027297b7
L
2899/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2900 DYNBSS. */
2901
2902bfd_boolean
6cabe1ea
AM
2903_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2904 struct elf_link_hash_entry *h,
027297b7
L
2905 asection *dynbss)
2906{
91ac5911 2907 unsigned int power_of_two;
027297b7
L
2908 bfd_vma mask;
2909 asection *sec = h->root.u.def.section;
2910
2911 /* The section aligment of definition is the maximum alignment
91ac5911
L
2912 requirement of symbols defined in the section. Since we don't
2913 know the symbol alignment requirement, we start with the
2914 maximum alignment and check low bits of the symbol address
2915 for the minimum alignment. */
2916 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2917 mask = ((bfd_vma) 1 << power_of_two) - 1;
2918 while ((h->root.u.def.value & mask) != 0)
2919 {
2920 mask >>= 1;
2921 --power_of_two;
2922 }
027297b7 2923
91ac5911
L
2924 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2925 dynbss))
027297b7
L
2926 {
2927 /* Adjust the section alignment if needed. */
2928 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2929 power_of_two))
027297b7
L
2930 return FALSE;
2931 }
2932
91ac5911 2933 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2934 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2935
2936 /* Define the symbol as being at this point in DYNBSS. */
2937 h->root.u.def.section = dynbss;
2938 h->root.u.def.value = dynbss->size;
2939
2940 /* Increment the size of DYNBSS to make room for the symbol. */
2941 dynbss->size += h->size;
2942
f7483970
L
2943 /* No error if extern_protected_data is true. */
2944 if (h->protected_def
889c2a67
L
2945 && (!info->extern_protected_data
2946 || (info->extern_protected_data < 0
2947 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2948 info->callbacks->einfo
2949 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2950 h->root.root.string);
6cabe1ea 2951
027297b7
L
2952 return TRUE;
2953}
2954
45d6a902
AM
2955/* Adjust all external symbols pointing into SEC_MERGE sections
2956 to reflect the object merging within the sections. */
2957
28caa186 2958static bfd_boolean
268b6b39 2959_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2960{
2961 asection *sec;
2962
45d6a902
AM
2963 if ((h->root.type == bfd_link_hash_defined
2964 || h->root.type == bfd_link_hash_defweak)
2965 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2966 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2967 {
a50b1753 2968 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2969
2970 h->root.u.def.value =
2971 _bfd_merged_section_offset (output_bfd,
2972 &h->root.u.def.section,
2973 elf_section_data (sec)->sec_info,
753731ee 2974 h->root.u.def.value);
45d6a902
AM
2975 }
2976
2977 return TRUE;
2978}
986a241f
RH
2979
2980/* Returns false if the symbol referred to by H should be considered
2981 to resolve local to the current module, and true if it should be
2982 considered to bind dynamically. */
2983
2984bfd_boolean
268b6b39
AM
2985_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2986 struct bfd_link_info *info,
89a2ee5a 2987 bfd_boolean not_local_protected)
986a241f
RH
2988{
2989 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2990 const struct elf_backend_data *bed;
2991 struct elf_link_hash_table *hash_table;
986a241f
RH
2992
2993 if (h == NULL)
2994 return FALSE;
2995
2996 while (h->root.type == bfd_link_hash_indirect
2997 || h->root.type == bfd_link_hash_warning)
2998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2999
3000 /* If it was forced local, then clearly it's not dynamic. */
3001 if (h->dynindx == -1)
3002 return FALSE;
f5385ebf 3003 if (h->forced_local)
986a241f
RH
3004 return FALSE;
3005
3006 /* Identify the cases where name binding rules say that a
3007 visible symbol resolves locally. */
0e1862bb
L
3008 binding_stays_local_p = (bfd_link_executable (info)
3009 || SYMBOLIC_BIND (info, h));
986a241f
RH
3010
3011 switch (ELF_ST_VISIBILITY (h->other))
3012 {
3013 case STV_INTERNAL:
3014 case STV_HIDDEN:
3015 return FALSE;
3016
3017 case STV_PROTECTED:
fcb93ecf
PB
3018 hash_table = elf_hash_table (info);
3019 if (!is_elf_hash_table (hash_table))
3020 return FALSE;
3021
3022 bed = get_elf_backend_data (hash_table->dynobj);
3023
986a241f
RH
3024 /* Proper resolution for function pointer equality may require
3025 that these symbols perhaps be resolved dynamically, even though
3026 we should be resolving them to the current module. */
89a2ee5a 3027 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3028 binding_stays_local_p = TRUE;
3029 break;
3030
3031 default:
986a241f
RH
3032 break;
3033 }
3034
aa37626c 3035 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3036 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3037 return TRUE;
3038
986a241f
RH
3039 /* Otherwise, the symbol is dynamic if binding rules don't tell
3040 us that it remains local. */
3041 return !binding_stays_local_p;
3042}
f6c52c13
AM
3043
3044/* Return true if the symbol referred to by H should be considered
3045 to resolve local to the current module, and false otherwise. Differs
3046 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3047 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
3048 for the place where forced_local and dynindx == -1 are tested. If
3049 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3050 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3051 the symbol is local only for defined symbols.
3052 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3053 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3054 treatment of undefined weak symbols. For those that do not make
3055 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3056
3057bfd_boolean
268b6b39
AM
3058_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3059 struct bfd_link_info *info,
3060 bfd_boolean local_protected)
f6c52c13 3061{
fcb93ecf
PB
3062 const struct elf_backend_data *bed;
3063 struct elf_link_hash_table *hash_table;
3064
f6c52c13
AM
3065 /* If it's a local sym, of course we resolve locally. */
3066 if (h == NULL)
3067 return TRUE;
3068
d95edcac
L
3069 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3070 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3071 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3072 return TRUE;
3073
7e2294f9
AO
3074 /* Common symbols that become definitions don't get the DEF_REGULAR
3075 flag set, so test it first, and don't bail out. */
3076 if (ELF_COMMON_DEF_P (h))
3077 /* Do nothing. */;
f6c52c13 3078 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3079 resolve locally. The sym is either undefined or dynamic. */
3080 else if (!h->def_regular)
f6c52c13
AM
3081 return FALSE;
3082
3083 /* Forced local symbols resolve locally. */
f5385ebf 3084 if (h->forced_local)
f6c52c13
AM
3085 return TRUE;
3086
3087 /* As do non-dynamic symbols. */
3088 if (h->dynindx == -1)
3089 return TRUE;
3090
3091 /* At this point, we know the symbol is defined and dynamic. In an
3092 executable it must resolve locally, likewise when building symbolic
3093 shared libraries. */
0e1862bb 3094 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3095 return TRUE;
3096
3097 /* Now deal with defined dynamic symbols in shared libraries. Ones
3098 with default visibility might not resolve locally. */
3099 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3100 return FALSE;
3101
fcb93ecf
PB
3102 hash_table = elf_hash_table (info);
3103 if (!is_elf_hash_table (hash_table))
3104 return TRUE;
3105
3106 bed = get_elf_backend_data (hash_table->dynobj);
3107
f7483970
L
3108 /* If extern_protected_data is false, STV_PROTECTED non-function
3109 symbols are local. */
889c2a67
L
3110 if ((!info->extern_protected_data
3111 || (info->extern_protected_data < 0
3112 && !bed->extern_protected_data))
3113 && !bed->is_function_type (h->type))
1c16dfa5
L
3114 return TRUE;
3115
f6c52c13 3116 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3117 symbols be treated as dynamic symbols. If the address of a
3118 function not defined in an executable is set to that function's
3119 plt entry in the executable, then the address of the function in
3120 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3121 return local_protected;
3122}
e1918d23
AM
3123
3124/* Caches some TLS segment info, and ensures that the TLS segment vma is
3125 aligned. Returns the first TLS output section. */
3126
3127struct bfd_section *
3128_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3129{
3130 struct bfd_section *sec, *tls;
3131 unsigned int align = 0;
3132
3133 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3134 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3135 break;
3136 tls = sec;
3137
3138 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3139 if (sec->alignment_power > align)
3140 align = sec->alignment_power;
3141
3142 elf_hash_table (info)->tls_sec = tls;
3143
3144 /* Ensure the alignment of the first section is the largest alignment,
3145 so that the tls segment starts aligned. */
3146 if (tls != NULL)
3147 tls->alignment_power = align;
3148
3149 return tls;
3150}
0ad989f9
L
3151
3152/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3153static bfd_boolean
3154is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3155 Elf_Internal_Sym *sym)
3156{
a4d8e49b
L
3157 const struct elf_backend_data *bed;
3158
0ad989f9
L
3159 /* Local symbols do not count, but target specific ones might. */
3160 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3161 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3162 return FALSE;
3163
fcb93ecf 3164 bed = get_elf_backend_data (abfd);
0ad989f9 3165 /* Function symbols do not count. */
fcb93ecf 3166 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3167 return FALSE;
3168
3169 /* If the section is undefined, then so is the symbol. */
3170 if (sym->st_shndx == SHN_UNDEF)
3171 return FALSE;
3172
3173 /* If the symbol is defined in the common section, then
3174 it is a common definition and so does not count. */
a4d8e49b 3175 if (bed->common_definition (sym))
0ad989f9
L
3176 return FALSE;
3177
3178 /* If the symbol is in a target specific section then we
3179 must rely upon the backend to tell us what it is. */
3180 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3181 /* FIXME - this function is not coded yet:
3182
3183 return _bfd_is_global_symbol_definition (abfd, sym);
3184
3185 Instead for now assume that the definition is not global,
3186 Even if this is wrong, at least the linker will behave
3187 in the same way that it used to do. */
3188 return FALSE;
3189
3190 return TRUE;
3191}
3192
3193/* Search the symbol table of the archive element of the archive ABFD
3194 whose archive map contains a mention of SYMDEF, and determine if
3195 the symbol is defined in this element. */
3196static bfd_boolean
3197elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3198{
3199 Elf_Internal_Shdr * hdr;
ef53be89
AM
3200 size_t symcount;
3201 size_t extsymcount;
3202 size_t extsymoff;
0ad989f9
L
3203 Elf_Internal_Sym *isymbuf;
3204 Elf_Internal_Sym *isym;
3205 Elf_Internal_Sym *isymend;
3206 bfd_boolean result;
3207
3208 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3209 if (abfd == NULL)
3210 return FALSE;
3211
3212 if (! bfd_check_format (abfd, bfd_object))
3213 return FALSE;
3214
7dc3990e
L
3215 /* Select the appropriate symbol table. If we don't know if the
3216 object file is an IR object, give linker LTO plugin a chance to
3217 get the correct symbol table. */
3218 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3219#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3220 || (abfd->plugin_format == bfd_plugin_unknown
3221 && bfd_link_plugin_object_p (abfd))
3222#endif
3223 )
3224 {
3225 /* Use the IR symbol table if the object has been claimed by
3226 plugin. */
3227 abfd = abfd->plugin_dummy_bfd;
3228 hdr = &elf_tdata (abfd)->symtab_hdr;
3229 }
3230 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3231 hdr = &elf_tdata (abfd)->symtab_hdr;
3232 else
3233 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3234
3235 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3236
3237 /* The sh_info field of the symtab header tells us where the
3238 external symbols start. We don't care about the local symbols. */
3239 if (elf_bad_symtab (abfd))
3240 {
3241 extsymcount = symcount;
3242 extsymoff = 0;
3243 }
3244 else
3245 {
3246 extsymcount = symcount - hdr->sh_info;
3247 extsymoff = hdr->sh_info;
3248 }
3249
3250 if (extsymcount == 0)
3251 return FALSE;
3252
3253 /* Read in the symbol table. */
3254 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3255 NULL, NULL, NULL);
3256 if (isymbuf == NULL)
3257 return FALSE;
3258
3259 /* Scan the symbol table looking for SYMDEF. */
3260 result = FALSE;
3261 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3262 {
3263 const char *name;
3264
3265 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3266 isym->st_name);
3267 if (name == NULL)
3268 break;
3269
3270 if (strcmp (name, symdef->name) == 0)
3271 {
3272 result = is_global_data_symbol_definition (abfd, isym);
3273 break;
3274 }
3275 }
3276
3277 free (isymbuf);
3278
3279 return result;
3280}
3281\f
5a580b3a
AM
3282/* Add an entry to the .dynamic table. */
3283
3284bfd_boolean
3285_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3286 bfd_vma tag,
3287 bfd_vma val)
3288{
3289 struct elf_link_hash_table *hash_table;
3290 const struct elf_backend_data *bed;
3291 asection *s;
3292 bfd_size_type newsize;
3293 bfd_byte *newcontents;
3294 Elf_Internal_Dyn dyn;
3295
3296 hash_table = elf_hash_table (info);
3297 if (! is_elf_hash_table (hash_table))
3298 return FALSE;
3299
3300 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3301 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3302 BFD_ASSERT (s != NULL);
3303
eea6121a 3304 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3305 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3306 if (newcontents == NULL)
3307 return FALSE;
3308
3309 dyn.d_tag = tag;
3310 dyn.d_un.d_val = val;
eea6121a 3311 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3312
eea6121a 3313 s->size = newsize;
5a580b3a
AM
3314 s->contents = newcontents;
3315
3316 return TRUE;
3317}
3318
3319/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3320 otherwise just check whether one already exists. Returns -1 on error,
3321 1 if a DT_NEEDED tag already exists, and 0 on success. */
3322
4ad4eba5 3323static int
7e9f0867
AM
3324elf_add_dt_needed_tag (bfd *abfd,
3325 struct bfd_link_info *info,
4ad4eba5
AM
3326 const char *soname,
3327 bfd_boolean do_it)
5a580b3a
AM
3328{
3329 struct elf_link_hash_table *hash_table;
ef53be89 3330 size_t strindex;
5a580b3a 3331
7e9f0867
AM
3332 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3333 return -1;
3334
5a580b3a 3335 hash_table = elf_hash_table (info);
5a580b3a 3336 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3337 if (strindex == (size_t) -1)
5a580b3a
AM
3338 return -1;
3339
02be4619 3340 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3341 {
3342 asection *sdyn;
3343 const struct elf_backend_data *bed;
3344 bfd_byte *extdyn;
3345
3346 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3347 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3348 if (sdyn != NULL)
3349 for (extdyn = sdyn->contents;
3350 extdyn < sdyn->contents + sdyn->size;
3351 extdyn += bed->s->sizeof_dyn)
3352 {
3353 Elf_Internal_Dyn dyn;
5a580b3a 3354
7e9f0867
AM
3355 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3356 if (dyn.d_tag == DT_NEEDED
3357 && dyn.d_un.d_val == strindex)
3358 {
3359 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3360 return 1;
3361 }
3362 }
5a580b3a
AM
3363 }
3364
3365 if (do_it)
3366 {
7e9f0867
AM
3367 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3368 return -1;
3369
5a580b3a
AM
3370 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3371 return -1;
3372 }
3373 else
3374 /* We were just checking for existence of the tag. */
3375 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3376
3377 return 0;
3378}
3379
7b15fa7a
AM
3380/* Return true if SONAME is on the needed list between NEEDED and STOP
3381 (or the end of list if STOP is NULL), and needed by a library that
3382 will be loaded. */
3383
010e5ae2 3384static bfd_boolean
7b15fa7a
AM
3385on_needed_list (const char *soname,
3386 struct bfd_link_needed_list *needed,
3387 struct bfd_link_needed_list *stop)
010e5ae2 3388{
7b15fa7a
AM
3389 struct bfd_link_needed_list *look;
3390 for (look = needed; look != stop; look = look->next)
3391 if (strcmp (soname, look->name) == 0
3392 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3393 /* If needed by a library that itself is not directly
3394 needed, recursively check whether that library is
3395 indirectly needed. Since we add DT_NEEDED entries to
3396 the end of the list, library dependencies appear after
3397 the library. Therefore search prior to the current
3398 LOOK, preventing possible infinite recursion. */
3399 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3400 return TRUE;
3401
3402 return FALSE;
3403}
3404
14160578 3405/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3406static int
3407elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3408{
3409 const struct elf_link_hash_entry *h1;
3410 const struct elf_link_hash_entry *h2;
10b7e05b 3411 bfd_signed_vma vdiff;
5a580b3a
AM
3412
3413 h1 = *(const struct elf_link_hash_entry **) arg1;
3414 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3415 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3416 if (vdiff != 0)
3417 return vdiff > 0 ? 1 : -1;
3418 else
3419 {
d3435ae8 3420 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3421 if (sdiff != 0)
3422 return sdiff > 0 ? 1 : -1;
3423 }
14160578
AM
3424 vdiff = h1->size - h2->size;
3425 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3426}
4ad4eba5 3427
5a580b3a
AM
3428/* This function is used to adjust offsets into .dynstr for
3429 dynamic symbols. This is called via elf_link_hash_traverse. */
3430
3431static bfd_boolean
3432elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3433{
a50b1753 3434 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3435
5a580b3a
AM
3436 if (h->dynindx != -1)
3437 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3438 return TRUE;
3439}
3440
3441/* Assign string offsets in .dynstr, update all structures referencing
3442 them. */
3443
4ad4eba5
AM
3444static bfd_boolean
3445elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3446{
3447 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3448 struct elf_link_local_dynamic_entry *entry;
3449 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3450 bfd *dynobj = hash_table->dynobj;
3451 asection *sdyn;
3452 bfd_size_type size;
3453 const struct elf_backend_data *bed;
3454 bfd_byte *extdyn;
3455
3456 _bfd_elf_strtab_finalize (dynstr);
3457 size = _bfd_elf_strtab_size (dynstr);
3458
3459 bed = get_elf_backend_data (dynobj);
3d4d4302 3460 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3461 BFD_ASSERT (sdyn != NULL);
3462
3463 /* Update all .dynamic entries referencing .dynstr strings. */
3464 for (extdyn = sdyn->contents;
eea6121a 3465 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3466 extdyn += bed->s->sizeof_dyn)
3467 {
3468 Elf_Internal_Dyn dyn;
3469
3470 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3471 switch (dyn.d_tag)
3472 {
3473 case DT_STRSZ:
3474 dyn.d_un.d_val = size;
3475 break;
3476 case DT_NEEDED:
3477 case DT_SONAME:
3478 case DT_RPATH:
3479 case DT_RUNPATH:
3480 case DT_FILTER:
3481 case DT_AUXILIARY:
7ee314fa
AM
3482 case DT_AUDIT:
3483 case DT_DEPAUDIT:
5a580b3a
AM
3484 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3485 break;
3486 default:
3487 continue;
3488 }
3489 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3490 }
3491
3492 /* Now update local dynamic symbols. */
3493 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3494 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3495 entry->isym.st_name);
3496
3497 /* And the rest of dynamic symbols. */
3498 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3499
3500 /* Adjust version definitions. */
3501 if (elf_tdata (output_bfd)->cverdefs)
3502 {
3503 asection *s;
3504 bfd_byte *p;
ef53be89 3505 size_t i;
5a580b3a
AM
3506 Elf_Internal_Verdef def;
3507 Elf_Internal_Verdaux defaux;
3508
3d4d4302 3509 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3510 p = s->contents;
3511 do
3512 {
3513 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3514 &def);
3515 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3516 if (def.vd_aux != sizeof (Elf_External_Verdef))
3517 continue;
5a580b3a
AM
3518 for (i = 0; i < def.vd_cnt; ++i)
3519 {
3520 _bfd_elf_swap_verdaux_in (output_bfd,
3521 (Elf_External_Verdaux *) p, &defaux);
3522 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3523 defaux.vda_name);
3524 _bfd_elf_swap_verdaux_out (output_bfd,
3525 &defaux, (Elf_External_Verdaux *) p);
3526 p += sizeof (Elf_External_Verdaux);
3527 }
3528 }
3529 while (def.vd_next);
3530 }
3531
3532 /* Adjust version references. */
3533 if (elf_tdata (output_bfd)->verref)
3534 {
3535 asection *s;
3536 bfd_byte *p;
ef53be89 3537 size_t i;
5a580b3a
AM
3538 Elf_Internal_Verneed need;
3539 Elf_Internal_Vernaux needaux;
3540
3d4d4302 3541 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3542 p = s->contents;
3543 do
3544 {
3545 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3546 &need);
3547 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3548 _bfd_elf_swap_verneed_out (output_bfd, &need,
3549 (Elf_External_Verneed *) p);
3550 p += sizeof (Elf_External_Verneed);
3551 for (i = 0; i < need.vn_cnt; ++i)
3552 {
3553 _bfd_elf_swap_vernaux_in (output_bfd,
3554 (Elf_External_Vernaux *) p, &needaux);
3555 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3556 needaux.vna_name);
3557 _bfd_elf_swap_vernaux_out (output_bfd,
3558 &needaux,
3559 (Elf_External_Vernaux *) p);
3560 p += sizeof (Elf_External_Vernaux);
3561 }
3562 }
3563 while (need.vn_next);
3564 }
3565
3566 return TRUE;
3567}
3568\f
13285a1b
AM
3569/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3570 The default is to only match when the INPUT and OUTPUT are exactly
3571 the same target. */
3572
3573bfd_boolean
3574_bfd_elf_default_relocs_compatible (const bfd_target *input,
3575 const bfd_target *output)
3576{
3577 return input == output;
3578}
3579
3580/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3581 This version is used when different targets for the same architecture
3582 are virtually identical. */
3583
3584bfd_boolean
3585_bfd_elf_relocs_compatible (const bfd_target *input,
3586 const bfd_target *output)
3587{
3588 const struct elf_backend_data *obed, *ibed;
3589
3590 if (input == output)
3591 return TRUE;
3592
3593 ibed = xvec_get_elf_backend_data (input);
3594 obed = xvec_get_elf_backend_data (output);
3595
3596 if (ibed->arch != obed->arch)
3597 return FALSE;
3598
3599 /* If both backends are using this function, deem them compatible. */
3600 return ibed->relocs_compatible == obed->relocs_compatible;
3601}
3602
e5034e59
AM
3603/* Make a special call to the linker "notice" function to tell it that
3604 we are about to handle an as-needed lib, or have finished
1b786873 3605 processing the lib. */
e5034e59
AM
3606
3607bfd_boolean
3608_bfd_elf_notice_as_needed (bfd *ibfd,
3609 struct bfd_link_info *info,
3610 enum notice_asneeded_action act)
3611{
46135103 3612 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3613}
3614
d9689752
L
3615/* Check relocations an ELF object file. */
3616
3617bfd_boolean
3618_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3619{
3620 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3621 struct elf_link_hash_table *htab = elf_hash_table (info);
3622
3623 /* If this object is the same format as the output object, and it is
3624 not a shared library, then let the backend look through the
3625 relocs.
3626
3627 This is required to build global offset table entries and to
3628 arrange for dynamic relocs. It is not required for the
3629 particular common case of linking non PIC code, even when linking
3630 against shared libraries, but unfortunately there is no way of
3631 knowing whether an object file has been compiled PIC or not.
3632 Looking through the relocs is not particularly time consuming.
3633 The problem is that we must either (1) keep the relocs in memory,
3634 which causes the linker to require additional runtime memory or
3635 (2) read the relocs twice from the input file, which wastes time.
3636 This would be a good case for using mmap.
3637
3638 I have no idea how to handle linking PIC code into a file of a
3639 different format. It probably can't be done. */
3640 if ((abfd->flags & DYNAMIC) == 0
3641 && is_elf_hash_table (htab)
3642 && bed->check_relocs != NULL
3643 && elf_object_id (abfd) == elf_hash_table_id (htab)
3644 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3645 {
3646 asection *o;
3647
3648 for (o = abfd->sections; o != NULL; o = o->next)
3649 {
3650 Elf_Internal_Rela *internal_relocs;
3651 bfd_boolean ok;
3652
5ce03cea 3653 /* Don't check relocations in excluded sections. */
d9689752 3654 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3655 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3656 || o->reloc_count == 0
3657 || ((info->strip == strip_all || info->strip == strip_debugger)
3658 && (o->flags & SEC_DEBUGGING) != 0)
3659 || bfd_is_abs_section (o->output_section))
3660 continue;
3661
3662 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3663 info->keep_memory);
3664 if (internal_relocs == NULL)
3665 return FALSE;
3666
3667 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3668
3669 if (elf_section_data (o)->relocs != internal_relocs)
3670 free (internal_relocs);
3671
3672 if (! ok)
3673 return FALSE;
3674 }
3675 }
3676
3677 return TRUE;
3678}
3679
4ad4eba5
AM
3680/* Add symbols from an ELF object file to the linker hash table. */
3681
3682static bfd_boolean
3683elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3684{
a0c402a5 3685 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3686 Elf_Internal_Shdr *hdr;
ef53be89
AM
3687 size_t symcount;
3688 size_t extsymcount;
3689 size_t extsymoff;
4ad4eba5
AM
3690 struct elf_link_hash_entry **sym_hash;
3691 bfd_boolean dynamic;
3692 Elf_External_Versym *extversym = NULL;
3693 Elf_External_Versym *ever;
3694 struct elf_link_hash_entry *weaks;
3695 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3696 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3697 Elf_Internal_Sym *isymbuf = NULL;
3698 Elf_Internal_Sym *isym;
3699 Elf_Internal_Sym *isymend;
3700 const struct elf_backend_data *bed;
3701 bfd_boolean add_needed;
66eb6687 3702 struct elf_link_hash_table *htab;
4ad4eba5 3703 bfd_size_type amt;
66eb6687 3704 void *alloc_mark = NULL;
4f87808c
AM
3705 struct bfd_hash_entry **old_table = NULL;
3706 unsigned int old_size = 0;
3707 unsigned int old_count = 0;
66eb6687 3708 void *old_tab = NULL;
66eb6687
AM
3709 void *old_ent;
3710 struct bfd_link_hash_entry *old_undefs = NULL;
3711 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3712 void *old_strtab = NULL;
66eb6687 3713 size_t tabsize = 0;
db6a5d5f 3714 asection *s;
29a9f53e 3715 bfd_boolean just_syms;
4ad4eba5 3716
66eb6687 3717 htab = elf_hash_table (info);
4ad4eba5 3718 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3719
3720 if ((abfd->flags & DYNAMIC) == 0)
3721 dynamic = FALSE;
3722 else
3723 {
3724 dynamic = TRUE;
3725
3726 /* You can't use -r against a dynamic object. Also, there's no
3727 hope of using a dynamic object which does not exactly match
3728 the format of the output file. */
0e1862bb 3729 if (bfd_link_relocatable (info)
66eb6687 3730 || !is_elf_hash_table (htab)
f13a99db 3731 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3732 {
0e1862bb 3733 if (bfd_link_relocatable (info))
9a0789ec
NC
3734 bfd_set_error (bfd_error_invalid_operation);
3735 else
3736 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3737 goto error_return;
3738 }
3739 }
3740
a0c402a5
L
3741 ehdr = elf_elfheader (abfd);
3742 if (info->warn_alternate_em
3743 && bed->elf_machine_code != ehdr->e_machine
3744 && ((bed->elf_machine_alt1 != 0
3745 && ehdr->e_machine == bed->elf_machine_alt1)
3746 || (bed->elf_machine_alt2 != 0
3747 && ehdr->e_machine == bed->elf_machine_alt2)))
3748 info->callbacks->einfo
695344c0 3749 /* xgettext:c-format */
a0c402a5
L
3750 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3751 ehdr->e_machine, abfd, bed->elf_machine_code);
3752
4ad4eba5
AM
3753 /* As a GNU extension, any input sections which are named
3754 .gnu.warning.SYMBOL are treated as warning symbols for the given
3755 symbol. This differs from .gnu.warning sections, which generate
3756 warnings when they are included in an output file. */
dd98f8d2 3757 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3758 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3759 {
db6a5d5f 3760 const char *name;
4ad4eba5 3761
db6a5d5f
AM
3762 name = bfd_get_section_name (abfd, s);
3763 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3764 {
db6a5d5f
AM
3765 char *msg;
3766 bfd_size_type sz;
3767
3768 name += sizeof ".gnu.warning." - 1;
3769
3770 /* If this is a shared object, then look up the symbol
3771 in the hash table. If it is there, and it is already
3772 been defined, then we will not be using the entry
3773 from this shared object, so we don't need to warn.
3774 FIXME: If we see the definition in a regular object
3775 later on, we will warn, but we shouldn't. The only
3776 fix is to keep track of what warnings we are supposed
3777 to emit, and then handle them all at the end of the
3778 link. */
3779 if (dynamic)
4ad4eba5 3780 {
db6a5d5f
AM
3781 struct elf_link_hash_entry *h;
3782
3783 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3784
3785 /* FIXME: What about bfd_link_hash_common? */
3786 if (h != NULL
3787 && (h->root.type == bfd_link_hash_defined
3788 || h->root.type == bfd_link_hash_defweak))
3789 continue;
3790 }
4ad4eba5 3791
db6a5d5f
AM
3792 sz = s->size;
3793 msg = (char *) bfd_alloc (abfd, sz + 1);
3794 if (msg == NULL)
3795 goto error_return;
4ad4eba5 3796
db6a5d5f
AM
3797 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3798 goto error_return;
4ad4eba5 3799
db6a5d5f 3800 msg[sz] = '\0';
4ad4eba5 3801
db6a5d5f
AM
3802 if (! (_bfd_generic_link_add_one_symbol
3803 (info, abfd, name, BSF_WARNING, s, 0, msg,
3804 FALSE, bed->collect, NULL)))
3805 goto error_return;
4ad4eba5 3806
0e1862bb 3807 if (bfd_link_executable (info))
db6a5d5f
AM
3808 {
3809 /* Clobber the section size so that the warning does
3810 not get copied into the output file. */
3811 s->size = 0;
11d2f718 3812
db6a5d5f
AM
3813 /* Also set SEC_EXCLUDE, so that symbols defined in
3814 the warning section don't get copied to the output. */
3815 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3816 }
3817 }
3818 }
3819
29a9f53e
L
3820 just_syms = ((s = abfd->sections) != NULL
3821 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3822
4ad4eba5
AM
3823 add_needed = TRUE;
3824 if (! dynamic)
3825 {
3826 /* If we are creating a shared library, create all the dynamic
3827 sections immediately. We need to attach them to something,
3828 so we attach them to this BFD, provided it is the right
bf89386a
L
3829 format and is not from ld --just-symbols. Always create the
3830 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3831 are no input BFD's of the same format as the output, we can't
3832 make a shared library. */
3833 if (!just_syms
bf89386a 3834 && (bfd_link_pic (info)
9c1d7a08
L
3835 || (!bfd_link_relocatable (info)
3836 && (info->export_dynamic || info->dynamic)))
66eb6687 3837 && is_elf_hash_table (htab)
f13a99db 3838 && info->output_bfd->xvec == abfd->xvec
66eb6687 3839 && !htab->dynamic_sections_created)
4ad4eba5
AM
3840 {
3841 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3842 goto error_return;
3843 }
3844 }
66eb6687 3845 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3846 goto error_return;
3847 else
3848 {
4ad4eba5 3849 const char *soname = NULL;
7ee314fa 3850 char *audit = NULL;
4ad4eba5 3851 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3852 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3853 int ret;
3854
3855 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3856 ld shouldn't allow it. */
29a9f53e 3857 if (just_syms)
92fd189d 3858 abort ();
4ad4eba5
AM
3859
3860 /* If this dynamic lib was specified on the command line with
3861 --as-needed in effect, then we don't want to add a DT_NEEDED
3862 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3863 in by another lib's DT_NEEDED. When --no-add-needed is used
3864 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3865 any dynamic library in DT_NEEDED tags in the dynamic lib at
3866 all. */
3867 add_needed = (elf_dyn_lib_class (abfd)
3868 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3869 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3870
3871 s = bfd_get_section_by_name (abfd, ".dynamic");
3872 if (s != NULL)
3873 {
3874 bfd_byte *dynbuf;
3875 bfd_byte *extdyn;
cb33740c 3876 unsigned int elfsec;
4ad4eba5
AM
3877 unsigned long shlink;
3878
eea6121a 3879 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3880 {
3881error_free_dyn:
3882 free (dynbuf);
3883 goto error_return;
3884 }
4ad4eba5
AM
3885
3886 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3887 if (elfsec == SHN_BAD)
4ad4eba5
AM
3888 goto error_free_dyn;
3889 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3890
3891 for (extdyn = dynbuf;
eea6121a 3892 extdyn < dynbuf + s->size;
4ad4eba5
AM
3893 extdyn += bed->s->sizeof_dyn)
3894 {
3895 Elf_Internal_Dyn dyn;
3896
3897 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3898 if (dyn.d_tag == DT_SONAME)
3899 {
3900 unsigned int tagv = dyn.d_un.d_val;
3901 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3902 if (soname == NULL)
3903 goto error_free_dyn;
3904 }
3905 if (dyn.d_tag == DT_NEEDED)
3906 {
3907 struct bfd_link_needed_list *n, **pn;
3908 char *fnm, *anm;
3909 unsigned int tagv = dyn.d_un.d_val;
3910
3911 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3912 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3913 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3914 if (n == NULL || fnm == NULL)
3915 goto error_free_dyn;
3916 amt = strlen (fnm) + 1;
a50b1753 3917 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3918 if (anm == NULL)
3919 goto error_free_dyn;
3920 memcpy (anm, fnm, amt);
3921 n->name = anm;
3922 n->by = abfd;
3923 n->next = NULL;
66eb6687 3924 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3925 ;
3926 *pn = n;
3927 }
3928 if (dyn.d_tag == DT_RUNPATH)
3929 {
3930 struct bfd_link_needed_list *n, **pn;
3931 char *fnm, *anm;
3932 unsigned int tagv = dyn.d_un.d_val;
3933
3934 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3935 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3936 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3937 if (n == NULL || fnm == NULL)
3938 goto error_free_dyn;
3939 amt = strlen (fnm) + 1;
a50b1753 3940 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3941 if (anm == NULL)
3942 goto error_free_dyn;
3943 memcpy (anm, fnm, amt);
3944 n->name = anm;
3945 n->by = abfd;
3946 n->next = NULL;
3947 for (pn = & runpath;
3948 *pn != NULL;
3949 pn = &(*pn)->next)
3950 ;
3951 *pn = n;
3952 }
3953 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3954 if (!runpath && dyn.d_tag == DT_RPATH)
3955 {
3956 struct bfd_link_needed_list *n, **pn;
3957 char *fnm, *anm;
3958 unsigned int tagv = dyn.d_un.d_val;
3959
3960 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3961 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3962 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3963 if (n == NULL || fnm == NULL)
3964 goto error_free_dyn;
3965 amt = strlen (fnm) + 1;
a50b1753 3966 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3967 if (anm == NULL)
f8703194 3968 goto error_free_dyn;
4ad4eba5
AM
3969 memcpy (anm, fnm, amt);
3970 n->name = anm;
3971 n->by = abfd;
3972 n->next = NULL;
3973 for (pn = & rpath;
3974 *pn != NULL;
3975 pn = &(*pn)->next)
3976 ;
3977 *pn = n;
3978 }
7ee314fa
AM
3979 if (dyn.d_tag == DT_AUDIT)
3980 {
3981 unsigned int tagv = dyn.d_un.d_val;
3982 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3983 }
4ad4eba5
AM
3984 }
3985
3986 free (dynbuf);
3987 }
3988
3989 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3990 frees all more recently bfd_alloc'd blocks as well. */
3991 if (runpath)
3992 rpath = runpath;
3993
3994 if (rpath)
3995 {
3996 struct bfd_link_needed_list **pn;
66eb6687 3997 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3998 ;
3999 *pn = rpath;
4000 }
4001
9acc85a6
AM
4002 /* If we have a PT_GNU_RELRO program header, mark as read-only
4003 all sections contained fully therein. This makes relro
4004 shared library sections appear as they will at run-time. */
4005 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4006 while (--phdr >= elf_tdata (abfd)->phdr)
4007 if (phdr->p_type == PT_GNU_RELRO)
4008 {
4009 for (s = abfd->sections; s != NULL; s = s->next)
4010 if ((s->flags & SEC_ALLOC) != 0
4011 && s->vma >= phdr->p_vaddr
4012 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4013 s->flags |= SEC_READONLY;
4014 break;
4015 }
4016
4ad4eba5
AM
4017 /* We do not want to include any of the sections in a dynamic
4018 object in the output file. We hack by simply clobbering the
4019 list of sections in the BFD. This could be handled more
4020 cleanly by, say, a new section flag; the existing
4021 SEC_NEVER_LOAD flag is not the one we want, because that one
4022 still implies that the section takes up space in the output
4023 file. */
4024 bfd_section_list_clear (abfd);
4025
4ad4eba5
AM
4026 /* Find the name to use in a DT_NEEDED entry that refers to this
4027 object. If the object has a DT_SONAME entry, we use it.
4028 Otherwise, if the generic linker stuck something in
4029 elf_dt_name, we use that. Otherwise, we just use the file
4030 name. */
4031 if (soname == NULL || *soname == '\0')
4032 {
4033 soname = elf_dt_name (abfd);
4034 if (soname == NULL || *soname == '\0')
4035 soname = bfd_get_filename (abfd);
4036 }
4037
4038 /* Save the SONAME because sometimes the linker emulation code
4039 will need to know it. */
4040 elf_dt_name (abfd) = soname;
4041
7e9f0867 4042 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4043 if (ret < 0)
4044 goto error_return;
4045
4046 /* If we have already included this dynamic object in the
4047 link, just ignore it. There is no reason to include a
4048 particular dynamic object more than once. */
4049 if (ret > 0)
4050 return TRUE;
7ee314fa
AM
4051
4052 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4053 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4054 }
4055
4056 /* If this is a dynamic object, we always link against the .dynsym
4057 symbol table, not the .symtab symbol table. The dynamic linker
4058 will only see the .dynsym symbol table, so there is no reason to
4059 look at .symtab for a dynamic object. */
4060
4061 if (! dynamic || elf_dynsymtab (abfd) == 0)
4062 hdr = &elf_tdata (abfd)->symtab_hdr;
4063 else
4064 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4065
4066 symcount = hdr->sh_size / bed->s->sizeof_sym;
4067
4068 /* The sh_info field of the symtab header tells us where the
4069 external symbols start. We don't care about the local symbols at
4070 this point. */
4071 if (elf_bad_symtab (abfd))
4072 {
4073 extsymcount = symcount;
4074 extsymoff = 0;
4075 }
4076 else
4077 {
4078 extsymcount = symcount - hdr->sh_info;
4079 extsymoff = hdr->sh_info;
4080 }
4081
f45794cb 4082 sym_hash = elf_sym_hashes (abfd);
012b2306 4083 if (extsymcount != 0)
4ad4eba5
AM
4084 {
4085 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4086 NULL, NULL, NULL);
4087 if (isymbuf == NULL)
4088 goto error_return;
4089
4ad4eba5 4090 if (sym_hash == NULL)
012b2306
AM
4091 {
4092 /* We store a pointer to the hash table entry for each
4093 external symbol. */
ef53be89
AM
4094 amt = extsymcount;
4095 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4096 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4097 if (sym_hash == NULL)
4098 goto error_free_sym;
4099 elf_sym_hashes (abfd) = sym_hash;
4100 }
4ad4eba5
AM
4101 }
4102
4103 if (dynamic)
4104 {
4105 /* Read in any version definitions. */
fc0e6df6
PB
4106 if (!_bfd_elf_slurp_version_tables (abfd,
4107 info->default_imported_symver))
4ad4eba5
AM
4108 goto error_free_sym;
4109
4110 /* Read in the symbol versions, but don't bother to convert them
4111 to internal format. */
4112 if (elf_dynversym (abfd) != 0)
4113 {
4114 Elf_Internal_Shdr *versymhdr;
4115
4116 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4117 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4118 if (extversym == NULL)
4119 goto error_free_sym;
4120 amt = versymhdr->sh_size;
4121 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4122 || bfd_bread (extversym, amt, abfd) != amt)
4123 goto error_free_vers;
4124 }
4125 }
4126
66eb6687
AM
4127 /* If we are loading an as-needed shared lib, save the symbol table
4128 state before we start adding symbols. If the lib turns out
4129 to be unneeded, restore the state. */
4130 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4131 {
4132 unsigned int i;
4133 size_t entsize;
4134
4135 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4136 {
4137 struct bfd_hash_entry *p;
2de92251 4138 struct elf_link_hash_entry *h;
66eb6687
AM
4139
4140 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4141 {
4142 h = (struct elf_link_hash_entry *) p;
4143 entsize += htab->root.table.entsize;
4144 if (h->root.type == bfd_link_hash_warning)
4145 entsize += htab->root.table.entsize;
4146 }
66eb6687
AM
4147 }
4148
4149 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4150 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4151 if (old_tab == NULL)
4152 goto error_free_vers;
4153
4154 /* Remember the current objalloc pointer, so that all mem for
4155 symbols added can later be reclaimed. */
4156 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4157 if (alloc_mark == NULL)
4158 goto error_free_vers;
4159
5061a885
AM
4160 /* Make a special call to the linker "notice" function to
4161 tell it that we are about to handle an as-needed lib. */
e5034e59 4162 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4163 goto error_free_vers;
5061a885 4164
f45794cb
AM
4165 /* Clone the symbol table. Remember some pointers into the
4166 symbol table, and dynamic symbol count. */
4167 old_ent = (char *) old_tab + tabsize;
66eb6687 4168 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4169 old_undefs = htab->root.undefs;
4170 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4171 old_table = htab->root.table.table;
4172 old_size = htab->root.table.size;
4173 old_count = htab->root.table.count;
5b677558
AM
4174 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4175 if (old_strtab == NULL)
4176 goto error_free_vers;
66eb6687
AM
4177
4178 for (i = 0; i < htab->root.table.size; i++)
4179 {
4180 struct bfd_hash_entry *p;
2de92251 4181 struct elf_link_hash_entry *h;
66eb6687
AM
4182
4183 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4184 {
4185 memcpy (old_ent, p, htab->root.table.entsize);
4186 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4187 h = (struct elf_link_hash_entry *) p;
4188 if (h->root.type == bfd_link_hash_warning)
4189 {
4190 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4191 old_ent = (char *) old_ent + htab->root.table.entsize;
4192 }
66eb6687
AM
4193 }
4194 }
4195 }
4ad4eba5 4196
66eb6687 4197 weaks = NULL;
4ad4eba5
AM
4198 ever = extversym != NULL ? extversym + extsymoff : NULL;
4199 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4200 isym < isymend;
4201 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4202 {
4203 int bind;
4204 bfd_vma value;
af44c138 4205 asection *sec, *new_sec;
4ad4eba5
AM
4206 flagword flags;
4207 const char *name;
4208 struct elf_link_hash_entry *h;
90c984fc 4209 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4210 bfd_boolean definition;
4211 bfd_boolean size_change_ok;
4212 bfd_boolean type_change_ok;
4213 bfd_boolean new_weakdef;
37a9e49a
L
4214 bfd_boolean new_weak;
4215 bfd_boolean old_weak;
4ad4eba5 4216 bfd_boolean override;
a4d8e49b 4217 bfd_boolean common;
97196564 4218 bfd_boolean discarded;
4ad4eba5
AM
4219 unsigned int old_alignment;
4220 bfd *old_bfd;
6e33951e 4221 bfd_boolean matched;
4ad4eba5
AM
4222
4223 override = FALSE;
4224
4225 flags = BSF_NO_FLAGS;
4226 sec = NULL;
4227 value = isym->st_value;
a4d8e49b 4228 common = bed->common_definition (isym);
97196564 4229 discarded = FALSE;
4ad4eba5
AM
4230
4231 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4232 switch (bind)
4ad4eba5 4233 {
3e7a7d11 4234 case STB_LOCAL:
4ad4eba5
AM
4235 /* This should be impossible, since ELF requires that all
4236 global symbols follow all local symbols, and that sh_info
4237 point to the first global symbol. Unfortunately, Irix 5
4238 screws this up. */
4239 continue;
3e7a7d11
NC
4240
4241 case STB_GLOBAL:
a4d8e49b 4242 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4243 flags = BSF_GLOBAL;
3e7a7d11
NC
4244 break;
4245
4246 case STB_WEAK:
4247 flags = BSF_WEAK;
4248 break;
4249
4250 case STB_GNU_UNIQUE:
4251 flags = BSF_GNU_UNIQUE;
4252 break;
4253
4254 default:
4ad4eba5 4255 /* Leave it up to the processor backend. */
3e7a7d11 4256 break;
4ad4eba5
AM
4257 }
4258
4259 if (isym->st_shndx == SHN_UNDEF)
4260 sec = bfd_und_section_ptr;
cb33740c
AM
4261 else if (isym->st_shndx == SHN_ABS)
4262 sec = bfd_abs_section_ptr;
4263 else if (isym->st_shndx == SHN_COMMON)
4264 {
4265 sec = bfd_com_section_ptr;
4266 /* What ELF calls the size we call the value. What ELF
4267 calls the value we call the alignment. */
4268 value = isym->st_size;
4269 }
4270 else
4ad4eba5
AM
4271 {
4272 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4273 if (sec == NULL)
4274 sec = bfd_abs_section_ptr;
dbaa2011 4275 else if (discarded_section (sec))
529fcb95 4276 {
e5d08002
L
4277 /* Symbols from discarded section are undefined. We keep
4278 its visibility. */
529fcb95 4279 sec = bfd_und_section_ptr;
97196564 4280 discarded = TRUE;
529fcb95
PB
4281 isym->st_shndx = SHN_UNDEF;
4282 }
4ad4eba5
AM
4283 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4284 value -= sec->vma;
4285 }
4ad4eba5
AM
4286
4287 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4288 isym->st_name);
4289 if (name == NULL)
4290 goto error_free_vers;
4291
4292 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4293 && (abfd->flags & BFD_PLUGIN) != 0)
4294 {
4295 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4296
4297 if (xc == NULL)
4298 {
4299 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4300 | SEC_EXCLUDE);
4301 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4302 if (xc == NULL)
4303 goto error_free_vers;
4304 }
4305 sec = xc;
4306 }
4307 else if (isym->st_shndx == SHN_COMMON
4308 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4309 && !bfd_link_relocatable (info))
4ad4eba5
AM
4310 {
4311 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4312
4313 if (tcomm == NULL)
4314 {
02d00247
AM
4315 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4316 | SEC_LINKER_CREATED);
4317 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4318 if (tcomm == NULL)
4ad4eba5
AM
4319 goto error_free_vers;
4320 }
4321 sec = tcomm;
4322 }
66eb6687 4323 else if (bed->elf_add_symbol_hook)
4ad4eba5 4324 {
66eb6687
AM
4325 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4326 &sec, &value))
4ad4eba5
AM
4327 goto error_free_vers;
4328
4329 /* The hook function sets the name to NULL if this symbol
4330 should be skipped for some reason. */
4331 if (name == NULL)
4332 continue;
4333 }
4334
4335 /* Sanity check that all possibilities were handled. */
4336 if (sec == NULL)
4337 {
4338 bfd_set_error (bfd_error_bad_value);
4339 goto error_free_vers;
4340 }
4341
191c0c42
AM
4342 /* Silently discard TLS symbols from --just-syms. There's
4343 no way to combine a static TLS block with a new TLS block
4344 for this executable. */
4345 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4346 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4347 continue;
4348
4ad4eba5
AM
4349 if (bfd_is_und_section (sec)
4350 || bfd_is_com_section (sec))
4351 definition = FALSE;
4352 else
4353 definition = TRUE;
4354
4355 size_change_ok = FALSE;
66eb6687 4356 type_change_ok = bed->type_change_ok;
37a9e49a 4357 old_weak = FALSE;
6e33951e 4358 matched = FALSE;
4ad4eba5
AM
4359 old_alignment = 0;
4360 old_bfd = NULL;
af44c138 4361 new_sec = sec;
4ad4eba5 4362
66eb6687 4363 if (is_elf_hash_table (htab))
4ad4eba5
AM
4364 {
4365 Elf_Internal_Versym iver;
4366 unsigned int vernum = 0;
4367 bfd_boolean skip;
4368
fc0e6df6 4369 if (ever == NULL)
4ad4eba5 4370 {
fc0e6df6
PB
4371 if (info->default_imported_symver)
4372 /* Use the default symbol version created earlier. */
4373 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4374 else
4375 iver.vs_vers = 0;
4376 }
4377 else
4378 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4379
4380 vernum = iver.vs_vers & VERSYM_VERSION;
4381
4382 /* If this is a hidden symbol, or if it is not version
4383 1, we append the version name to the symbol name.
cc86ff91
EB
4384 However, we do not modify a non-hidden absolute symbol
4385 if it is not a function, because it might be the version
4386 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4387 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4388 || (vernum > 1
4389 && (!bfd_is_abs_section (sec)
4390 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4391 {
4392 const char *verstr;
4393 size_t namelen, verlen, newlen;
4394 char *newname, *p;
4395
4396 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4397 {
fc0e6df6
PB
4398 if (vernum > elf_tdata (abfd)->cverdefs)
4399 verstr = NULL;
4400 else if (vernum > 1)
4401 verstr =
4402 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4403 else
4404 verstr = "";
4ad4eba5 4405
fc0e6df6 4406 if (verstr == NULL)
4ad4eba5 4407 {
4eca0228 4408 _bfd_error_handler
695344c0 4409 /* xgettext:c-format */
fc0e6df6
PB
4410 (_("%B: %s: invalid version %u (max %d)"),
4411 abfd, name, vernum,
4412 elf_tdata (abfd)->cverdefs);
4413 bfd_set_error (bfd_error_bad_value);
4414 goto error_free_vers;
4ad4eba5 4415 }
fc0e6df6
PB
4416 }
4417 else
4418 {
4419 /* We cannot simply test for the number of
4420 entries in the VERNEED section since the
4421 numbers for the needed versions do not start
4422 at 0. */
4423 Elf_Internal_Verneed *t;
4424
4425 verstr = NULL;
4426 for (t = elf_tdata (abfd)->verref;
4427 t != NULL;
4428 t = t->vn_nextref)
4ad4eba5 4429 {
fc0e6df6 4430 Elf_Internal_Vernaux *a;
4ad4eba5 4431
fc0e6df6
PB
4432 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4433 {
4434 if (a->vna_other == vernum)
4ad4eba5 4435 {
fc0e6df6
PB
4436 verstr = a->vna_nodename;
4437 break;
4ad4eba5 4438 }
4ad4eba5 4439 }
fc0e6df6
PB
4440 if (a != NULL)
4441 break;
4442 }
4443 if (verstr == NULL)
4444 {
4eca0228 4445 _bfd_error_handler
695344c0 4446 /* xgettext:c-format */
fc0e6df6
PB
4447 (_("%B: %s: invalid needed version %d"),
4448 abfd, name, vernum);
4449 bfd_set_error (bfd_error_bad_value);
4450 goto error_free_vers;
4ad4eba5 4451 }
4ad4eba5 4452 }
fc0e6df6
PB
4453
4454 namelen = strlen (name);
4455 verlen = strlen (verstr);
4456 newlen = namelen + verlen + 2;
4457 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4458 && isym->st_shndx != SHN_UNDEF)
4459 ++newlen;
4460
a50b1753 4461 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4462 if (newname == NULL)
4463 goto error_free_vers;
4464 memcpy (newname, name, namelen);
4465 p = newname + namelen;
4466 *p++ = ELF_VER_CHR;
4467 /* If this is a defined non-hidden version symbol,
4468 we add another @ to the name. This indicates the
4469 default version of the symbol. */
4470 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4471 && isym->st_shndx != SHN_UNDEF)
4472 *p++ = ELF_VER_CHR;
4473 memcpy (p, verstr, verlen + 1);
4474
4475 name = newname;
4ad4eba5
AM
4476 }
4477
cd3416da
AM
4478 /* If this symbol has default visibility and the user has
4479 requested we not re-export it, then mark it as hidden. */
a0d49154 4480 if (!bfd_is_und_section (sec)
cd3416da 4481 && !dynamic
ce875075 4482 && abfd->no_export
cd3416da
AM
4483 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4484 isym->st_other = (STV_HIDDEN
4485 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4486
4f3fedcf
AM
4487 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4488 sym_hash, &old_bfd, &old_weak,
4489 &old_alignment, &skip, &override,
6e33951e
L
4490 &type_change_ok, &size_change_ok,
4491 &matched))
4ad4eba5
AM
4492 goto error_free_vers;
4493
4494 if (skip)
4495 continue;
4496
6e33951e
L
4497 /* Override a definition only if the new symbol matches the
4498 existing one. */
4499 if (override && matched)
4ad4eba5
AM
4500 definition = FALSE;
4501
4502 h = *sym_hash;
4503 while (h->root.type == bfd_link_hash_indirect
4504 || h->root.type == bfd_link_hash_warning)
4505 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4506
4ad4eba5 4507 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4508 && vernum > 1
4509 && definition)
4510 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4511 }
4512
4513 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4514 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4515 (struct bfd_link_hash_entry **) sym_hash)))
4516 goto error_free_vers;
4517
a43942db
MR
4518 if ((flags & BSF_GNU_UNIQUE)
4519 && (abfd->flags & DYNAMIC) == 0
4520 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4521 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4522
4ad4eba5 4523 h = *sym_hash;
90c984fc
L
4524 /* We need to make sure that indirect symbol dynamic flags are
4525 updated. */
4526 hi = h;
4ad4eba5
AM
4527 while (h->root.type == bfd_link_hash_indirect
4528 || h->root.type == bfd_link_hash_warning)
4529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4530
97196564
L
4531 /* Setting the index to -3 tells elf_link_output_extsym that
4532 this symbol is defined in a discarded section. */
4533 if (discarded)
4534 h->indx = -3;
4535
4ad4eba5
AM
4536 *sym_hash = h;
4537
37a9e49a 4538 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4539 new_weakdef = FALSE;
4540 if (dynamic
4541 && definition
37a9e49a 4542 && new_weak
fcb93ecf 4543 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4544 && is_elf_hash_table (htab)
f6e332e6 4545 && h->u.weakdef == NULL)
4ad4eba5
AM
4546 {
4547 /* Keep a list of all weak defined non function symbols from
4548 a dynamic object, using the weakdef field. Later in this
4549 function we will set the weakdef field to the correct
4550 value. We only put non-function symbols from dynamic
4551 objects on this list, because that happens to be the only
4552 time we need to know the normal symbol corresponding to a
4553 weak symbol, and the information is time consuming to
4554 figure out. If the weakdef field is not already NULL,
4555 then this symbol was already defined by some previous
4556 dynamic object, and we will be using that previous
4557 definition anyhow. */
4558
f6e332e6 4559 h->u.weakdef = weaks;
4ad4eba5
AM
4560 weaks = h;
4561 new_weakdef = TRUE;
4562 }
4563
4564 /* Set the alignment of a common symbol. */
a4d8e49b 4565 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4566 && h->root.type == bfd_link_hash_common)
4567 {
4568 unsigned int align;
4569
a4d8e49b 4570 if (common)
af44c138
L
4571 align = bfd_log2 (isym->st_value);
4572 else
4573 {
4574 /* The new symbol is a common symbol in a shared object.
4575 We need to get the alignment from the section. */
4576 align = new_sec->alignment_power;
4577 }
595213d4 4578 if (align > old_alignment)
4ad4eba5
AM
4579 h->root.u.c.p->alignment_power = align;
4580 else
4581 h->root.u.c.p->alignment_power = old_alignment;
4582 }
4583
66eb6687 4584 if (is_elf_hash_table (htab))
4ad4eba5 4585 {
4f3fedcf
AM
4586 /* Set a flag in the hash table entry indicating the type of
4587 reference or definition we just found. A dynamic symbol
4588 is one which is referenced or defined by both a regular
4589 object and a shared object. */
4590 bfd_boolean dynsym = FALSE;
4591
4592 /* Plugin symbols aren't normal. Don't set def_regular or
4593 ref_regular for them, or make them dynamic. */
4594 if ((abfd->flags & BFD_PLUGIN) != 0)
4595 ;
4596 else if (! dynamic)
4597 {
4598 if (! definition)
4599 {
4600 h->ref_regular = 1;
4601 if (bind != STB_WEAK)
4602 h->ref_regular_nonweak = 1;
4603 }
4604 else
4605 {
4606 h->def_regular = 1;
4607 if (h->def_dynamic)
4608 {
4609 h->def_dynamic = 0;
4610 h->ref_dynamic = 1;
4611 }
4612 }
4613
4614 /* If the indirect symbol has been forced local, don't
4615 make the real symbol dynamic. */
4616 if ((h == hi || !hi->forced_local)
0e1862bb 4617 && (bfd_link_dll (info)
4f3fedcf
AM
4618 || h->def_dynamic
4619 || h->ref_dynamic))
4620 dynsym = TRUE;
4621 }
4622 else
4623 {
4624 if (! definition)
4625 {
4626 h->ref_dynamic = 1;
4627 hi->ref_dynamic = 1;
4628 }
4629 else
4630 {
4631 h->def_dynamic = 1;
4632 hi->def_dynamic = 1;
4633 }
4634
4635 /* If the indirect symbol has been forced local, don't
4636 make the real symbol dynamic. */
4637 if ((h == hi || !hi->forced_local)
4638 && (h->def_regular
4639 || h->ref_regular
4640 || (h->u.weakdef != NULL
4641 && ! new_weakdef
4642 && h->u.weakdef->dynindx != -1)))
4643 dynsym = TRUE;
4644 }
4645
4646 /* Check to see if we need to add an indirect symbol for
4647 the default name. */
4648 if (definition
4649 || (!override && h->root.type == bfd_link_hash_common))
4650 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4651 sec, value, &old_bfd, &dynsym))
4652 goto error_free_vers;
4ad4eba5
AM
4653
4654 /* Check the alignment when a common symbol is involved. This
4655 can change when a common symbol is overridden by a normal
4656 definition or a common symbol is ignored due to the old
4657 normal definition. We need to make sure the maximum
4658 alignment is maintained. */
a4d8e49b 4659 if ((old_alignment || common)
4ad4eba5
AM
4660 && h->root.type != bfd_link_hash_common)
4661 {
4662 unsigned int common_align;
4663 unsigned int normal_align;
4664 unsigned int symbol_align;
4665 bfd *normal_bfd;
4666 bfd *common_bfd;
4667
3a81e825
AM
4668 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4669 || h->root.type == bfd_link_hash_defweak);
4670
4ad4eba5
AM
4671 symbol_align = ffs (h->root.u.def.value) - 1;
4672 if (h->root.u.def.section->owner != NULL
0616a280
AM
4673 && (h->root.u.def.section->owner->flags
4674 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4675 {
4676 normal_align = h->root.u.def.section->alignment_power;
4677 if (normal_align > symbol_align)
4678 normal_align = symbol_align;
4679 }
4680 else
4681 normal_align = symbol_align;
4682
4683 if (old_alignment)
4684 {
4685 common_align = old_alignment;
4686 common_bfd = old_bfd;
4687 normal_bfd = abfd;
4688 }
4689 else
4690 {
4691 common_align = bfd_log2 (isym->st_value);
4692 common_bfd = abfd;
4693 normal_bfd = old_bfd;
4694 }
4695
4696 if (normal_align < common_align)
d07676f8
NC
4697 {
4698 /* PR binutils/2735 */
4699 if (normal_bfd == NULL)
4eca0228 4700 _bfd_error_handler
695344c0 4701 /* xgettext:c-format */
4f3fedcf
AM
4702 (_("Warning: alignment %u of common symbol `%s' in %B is"
4703 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4704 1 << common_align, name, common_bfd,
4705 1 << normal_align, h->root.u.def.section);
d07676f8 4706 else
4eca0228 4707 _bfd_error_handler
695344c0 4708 /* xgettext:c-format */
d07676f8
NC
4709 (_("Warning: alignment %u of symbol `%s' in %B"
4710 " is smaller than %u in %B"),
c08bb8dd
AM
4711 1 << normal_align, name, normal_bfd,
4712 1 << common_align, common_bfd);
d07676f8 4713 }
4ad4eba5
AM
4714 }
4715
83ad0046 4716 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4717 if (isym->st_size != 0
4718 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4719 && (definition || h->size == 0))
4720 {
83ad0046
L
4721 if (h->size != 0
4722 && h->size != isym->st_size
4723 && ! size_change_ok)
4eca0228 4724 _bfd_error_handler
695344c0 4725 /* xgettext:c-format */
d003868e
AM
4726 (_("Warning: size of symbol `%s' changed"
4727 " from %lu in %B to %lu in %B"),
c08bb8dd
AM
4728 name, (unsigned long) h->size, old_bfd,
4729 (unsigned long) isym->st_size, abfd);
4ad4eba5
AM
4730
4731 h->size = isym->st_size;
4732 }
4733
4734 /* If this is a common symbol, then we always want H->SIZE
4735 to be the size of the common symbol. The code just above
4736 won't fix the size if a common symbol becomes larger. We
4737 don't warn about a size change here, because that is
4f3fedcf 4738 covered by --warn-common. Allow changes between different
fcb93ecf 4739 function types. */
4ad4eba5
AM
4740 if (h->root.type == bfd_link_hash_common)
4741 h->size = h->root.u.c.size;
4742
4743 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4744 && ((definition && !new_weak)
4745 || (old_weak && h->root.type == bfd_link_hash_common)
4746 || h->type == STT_NOTYPE))
4ad4eba5 4747 {
2955ec4c
L
4748 unsigned int type = ELF_ST_TYPE (isym->st_info);
4749
4750 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4751 symbol. */
4752 if (type == STT_GNU_IFUNC
4753 && (abfd->flags & DYNAMIC) != 0)
4754 type = STT_FUNC;
4ad4eba5 4755
2955ec4c
L
4756 if (h->type != type)
4757 {
4758 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4759 /* xgettext:c-format */
4eca0228 4760 _bfd_error_handler
2955ec4c
L
4761 (_("Warning: type of symbol `%s' changed"
4762 " from %d to %d in %B"),
c08bb8dd 4763 name, h->type, type, abfd);
2955ec4c
L
4764
4765 h->type = type;
4766 }
4ad4eba5
AM
4767 }
4768
54ac0771 4769 /* Merge st_other field. */
b8417128 4770 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4771
c3df8c14 4772 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4773 if (definition
4774 && (sec->flags & SEC_DEBUGGING)
4775 && !bfd_link_relocatable (info))
c3df8c14
AM
4776 dynsym = FALSE;
4777
4f3fedcf
AM
4778 /* Nor should we make plugin symbols dynamic. */
4779 if ((abfd->flags & BFD_PLUGIN) != 0)
4780 dynsym = FALSE;
4781
35fc36a8 4782 if (definition)
35399224
L
4783 {
4784 h->target_internal = isym->st_target_internal;
4785 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4786 }
35fc36a8 4787
4ad4eba5
AM
4788 if (definition && !dynamic)
4789 {
4790 char *p = strchr (name, ELF_VER_CHR);
4791 if (p != NULL && p[1] != ELF_VER_CHR)
4792 {
4793 /* Queue non-default versions so that .symver x, x@FOO
4794 aliases can be checked. */
66eb6687 4795 if (!nondeflt_vers)
4ad4eba5 4796 {
66eb6687
AM
4797 amt = ((isymend - isym + 1)
4798 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4799 nondeflt_vers
4800 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4801 if (!nondeflt_vers)
4802 goto error_free_vers;
4ad4eba5 4803 }
66eb6687 4804 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4805 }
4806 }
4807
4808 if (dynsym && h->dynindx == -1)
4809 {
c152c796 4810 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4811 goto error_free_vers;
f6e332e6 4812 if (h->u.weakdef != NULL
4ad4eba5 4813 && ! new_weakdef
f6e332e6 4814 && h->u.weakdef->dynindx == -1)
4ad4eba5 4815 {
66eb6687 4816 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4817 goto error_free_vers;
4818 }
4819 }
1f599d0e 4820 else if (h->dynindx != -1)
4ad4eba5
AM
4821 /* If the symbol already has a dynamic index, but
4822 visibility says it should not be visible, turn it into
4823 a local symbol. */
4824 switch (ELF_ST_VISIBILITY (h->other))
4825 {
4826 case STV_INTERNAL:
4827 case STV_HIDDEN:
4828 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4829 dynsym = FALSE;
4830 break;
4831 }
4832
aef28989
L
4833 /* Don't add DT_NEEDED for references from the dummy bfd nor
4834 for unmatched symbol. */
4ad4eba5 4835 if (!add_needed
aef28989 4836 && matched
4ad4eba5 4837 && definition
010e5ae2 4838 && ((dynsym
ffa9430d 4839 && h->ref_regular_nonweak
4f3fedcf
AM
4840 && (old_bfd == NULL
4841 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4842 || (h->ref_dynamic_nonweak
010e5ae2 4843 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4844 && !on_needed_list (elf_dt_name (abfd),
4845 htab->needed, NULL))))
4ad4eba5
AM
4846 {
4847 int ret;
4848 const char *soname = elf_dt_name (abfd);
4849
16e4ecc0
AM
4850 info->callbacks->minfo ("%!", soname, old_bfd,
4851 h->root.root.string);
4852
4ad4eba5
AM
4853 /* A symbol from a library loaded via DT_NEEDED of some
4854 other library is referenced by a regular object.
e56f61be 4855 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4856 --no-add-needed is used and the reference was not
4857 a weak one. */
4f3fedcf 4858 if (old_bfd != NULL
b918acf9 4859 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4860 {
4eca0228 4861 _bfd_error_handler
695344c0 4862 /* xgettext:c-format */
3cbc5de0 4863 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4864 old_bfd, name);
ff5ac77b 4865 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4866 goto error_free_vers;
4867 }
4868
a50b1753 4869 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4870 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4871
4ad4eba5 4872 add_needed = TRUE;
7e9f0867 4873 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4874 if (ret < 0)
4875 goto error_free_vers;
4876
4877 BFD_ASSERT (ret == 0);
4878 }
4879 }
4880 }
4881
66eb6687
AM
4882 if (extversym != NULL)
4883 {
4884 free (extversym);
4885 extversym = NULL;
4886 }
4887
4888 if (isymbuf != NULL)
4889 {
4890 free (isymbuf);
4891 isymbuf = NULL;
4892 }
4893
4894 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4895 {
4896 unsigned int i;
4897
4898 /* Restore the symbol table. */
f45794cb
AM
4899 old_ent = (char *) old_tab + tabsize;
4900 memset (elf_sym_hashes (abfd), 0,
4901 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4902 htab->root.table.table = old_table;
4903 htab->root.table.size = old_size;
4904 htab->root.table.count = old_count;
66eb6687 4905 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4906 htab->root.undefs = old_undefs;
4907 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4908 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4909 free (old_strtab);
4910 old_strtab = NULL;
66eb6687
AM
4911 for (i = 0; i < htab->root.table.size; i++)
4912 {
4913 struct bfd_hash_entry *p;
4914 struct elf_link_hash_entry *h;
3e0882af
L
4915 bfd_size_type size;
4916 unsigned int alignment_power;
66eb6687
AM
4917
4918 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4919 {
4920 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4921 if (h->root.type == bfd_link_hash_warning)
4922 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4923
3e0882af
L
4924 /* Preserve the maximum alignment and size for common
4925 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4926 since it can still be loaded at run time by another
3e0882af
L
4927 dynamic lib. */
4928 if (h->root.type == bfd_link_hash_common)
4929 {
4930 size = h->root.u.c.size;
4931 alignment_power = h->root.u.c.p->alignment_power;
4932 }
4933 else
4934 {
4935 size = 0;
4936 alignment_power = 0;
4937 }
66eb6687
AM
4938 memcpy (p, old_ent, htab->root.table.entsize);
4939 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4940 h = (struct elf_link_hash_entry *) p;
4941 if (h->root.type == bfd_link_hash_warning)
4942 {
4943 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4944 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4946 }
a4542f1b 4947 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4948 {
4949 if (size > h->root.u.c.size)
4950 h->root.u.c.size = size;
4951 if (alignment_power > h->root.u.c.p->alignment_power)
4952 h->root.u.c.p->alignment_power = alignment_power;
4953 }
66eb6687
AM
4954 }
4955 }
4956
5061a885
AM
4957 /* Make a special call to the linker "notice" function to
4958 tell it that symbols added for crefs may need to be removed. */
e5034e59 4959 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4960 goto error_free_vers;
5061a885 4961
66eb6687
AM
4962 free (old_tab);
4963 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4964 alloc_mark);
4965 if (nondeflt_vers != NULL)
4966 free (nondeflt_vers);
4967 return TRUE;
4968 }
2de92251 4969
66eb6687
AM
4970 if (old_tab != NULL)
4971 {
e5034e59 4972 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4973 goto error_free_vers;
66eb6687
AM
4974 free (old_tab);
4975 old_tab = NULL;
4976 }
4977
c6e8a9a8
L
4978 /* Now that all the symbols from this input file are created, if
4979 not performing a relocatable link, handle .symver foo, foo@BAR
4980 such that any relocs against foo become foo@BAR. */
0e1862bb 4981 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4982 {
ef53be89 4983 size_t cnt, symidx;
4ad4eba5
AM
4984
4985 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4986 {
4987 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4988 char *shortname, *p;
4989
4990 p = strchr (h->root.root.string, ELF_VER_CHR);
4991 if (p == NULL
4992 || (h->root.type != bfd_link_hash_defined
4993 && h->root.type != bfd_link_hash_defweak))
4994 continue;
4995
4996 amt = p - h->root.root.string;
a50b1753 4997 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4998 if (!shortname)
4999 goto error_free_vers;
4ad4eba5
AM
5000 memcpy (shortname, h->root.root.string, amt);
5001 shortname[amt] = '\0';
5002
5003 hi = (struct elf_link_hash_entry *)
66eb6687 5004 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5005 FALSE, FALSE, FALSE);
5006 if (hi != NULL
5007 && hi->root.type == h->root.type
5008 && hi->root.u.def.value == h->root.u.def.value
5009 && hi->root.u.def.section == h->root.u.def.section)
5010 {
5011 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5012 hi->root.type = bfd_link_hash_indirect;
5013 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5014 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5015 sym_hash = elf_sym_hashes (abfd);
5016 if (sym_hash)
5017 for (symidx = 0; symidx < extsymcount; ++symidx)
5018 if (sym_hash[symidx] == hi)
5019 {
5020 sym_hash[symidx] = h;
5021 break;
5022 }
5023 }
5024 free (shortname);
5025 }
5026 free (nondeflt_vers);
5027 nondeflt_vers = NULL;
5028 }
5029
4ad4eba5
AM
5030 /* Now set the weakdefs field correctly for all the weak defined
5031 symbols we found. The only way to do this is to search all the
5032 symbols. Since we only need the information for non functions in
5033 dynamic objects, that's the only time we actually put anything on
5034 the list WEAKS. We need this information so that if a regular
5035 object refers to a symbol defined weakly in a dynamic object, the
5036 real symbol in the dynamic object is also put in the dynamic
5037 symbols; we also must arrange for both symbols to point to the
5038 same memory location. We could handle the general case of symbol
5039 aliasing, but a general symbol alias can only be generated in
5040 assembler code, handling it correctly would be very time
5041 consuming, and other ELF linkers don't handle general aliasing
5042 either. */
5043 if (weaks != NULL)
5044 {
5045 struct elf_link_hash_entry **hpp;
5046 struct elf_link_hash_entry **hppend;
5047 struct elf_link_hash_entry **sorted_sym_hash;
5048 struct elf_link_hash_entry *h;
5049 size_t sym_count;
5050
5051 /* Since we have to search the whole symbol list for each weak
5052 defined symbol, search time for N weak defined symbols will be
5053 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5054 amt = extsymcount;
5055 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5056 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5057 if (sorted_sym_hash == NULL)
5058 goto error_return;
5059 sym_hash = sorted_sym_hash;
5060 hpp = elf_sym_hashes (abfd);
5061 hppend = hpp + extsymcount;
5062 sym_count = 0;
5063 for (; hpp < hppend; hpp++)
5064 {
5065 h = *hpp;
5066 if (h != NULL
5067 && h->root.type == bfd_link_hash_defined
fcb93ecf 5068 && !bed->is_function_type (h->type))
4ad4eba5
AM
5069 {
5070 *sym_hash = h;
5071 sym_hash++;
5072 sym_count++;
5073 }
5074 }
5075
5076 qsort (sorted_sym_hash, sym_count,
5077 sizeof (struct elf_link_hash_entry *),
5078 elf_sort_symbol);
5079
5080 while (weaks != NULL)
5081 {
5082 struct elf_link_hash_entry *hlook;
5083 asection *slook;
5084 bfd_vma vlook;
ed54588d 5085 size_t i, j, idx = 0;
4ad4eba5
AM
5086
5087 hlook = weaks;
f6e332e6
AM
5088 weaks = hlook->u.weakdef;
5089 hlook->u.weakdef = NULL;
4ad4eba5
AM
5090
5091 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5092 || hlook->root.type == bfd_link_hash_defweak
5093 || hlook->root.type == bfd_link_hash_common
5094 || hlook->root.type == bfd_link_hash_indirect);
5095 slook = hlook->root.u.def.section;
5096 vlook = hlook->root.u.def.value;
5097
4ad4eba5
AM
5098 i = 0;
5099 j = sym_count;
14160578 5100 while (i != j)
4ad4eba5
AM
5101 {
5102 bfd_signed_vma vdiff;
5103 idx = (i + j) / 2;
14160578 5104 h = sorted_sym_hash[idx];
4ad4eba5
AM
5105 vdiff = vlook - h->root.u.def.value;
5106 if (vdiff < 0)
5107 j = idx;
5108 else if (vdiff > 0)
5109 i = idx + 1;
5110 else
5111 {
d3435ae8 5112 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5113 if (sdiff < 0)
5114 j = idx;
5115 else if (sdiff > 0)
5116 i = idx + 1;
5117 else
14160578 5118 break;
4ad4eba5
AM
5119 }
5120 }
5121
5122 /* We didn't find a value/section match. */
14160578 5123 if (i == j)
4ad4eba5
AM
5124 continue;
5125
14160578
AM
5126 /* With multiple aliases, or when the weak symbol is already
5127 strongly defined, we have multiple matching symbols and
5128 the binary search above may land on any of them. Step
5129 one past the matching symbol(s). */
5130 while (++idx != j)
5131 {
5132 h = sorted_sym_hash[idx];
5133 if (h->root.u.def.section != slook
5134 || h->root.u.def.value != vlook)
5135 break;
5136 }
5137
5138 /* Now look back over the aliases. Since we sorted by size
5139 as well as value and section, we'll choose the one with
5140 the largest size. */
5141 while (idx-- != i)
4ad4eba5 5142 {
14160578 5143 h = sorted_sym_hash[idx];
4ad4eba5
AM
5144
5145 /* Stop if value or section doesn't match. */
14160578
AM
5146 if (h->root.u.def.section != slook
5147 || h->root.u.def.value != vlook)
4ad4eba5
AM
5148 break;
5149 else if (h != hlook)
5150 {
f6e332e6 5151 hlook->u.weakdef = h;
4ad4eba5
AM
5152
5153 /* If the weak definition is in the list of dynamic
5154 symbols, make sure the real definition is put
5155 there as well. */
5156 if (hlook->dynindx != -1 && h->dynindx == -1)
5157 {
c152c796 5158 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5159 {
5160 err_free_sym_hash:
5161 free (sorted_sym_hash);
5162 goto error_return;
5163 }
4ad4eba5
AM
5164 }
5165
5166 /* If the real definition is in the list of dynamic
5167 symbols, make sure the weak definition is put
5168 there as well. If we don't do this, then the
5169 dynamic loader might not merge the entries for the
5170 real definition and the weak definition. */
5171 if (h->dynindx != -1 && hlook->dynindx == -1)
5172 {
c152c796 5173 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5174 goto err_free_sym_hash;
4ad4eba5
AM
5175 }
5176 break;
5177 }
5178 }
5179 }
5180
5181 free (sorted_sym_hash);
5182 }
5183
33177bb1
AM
5184 if (bed->check_directives
5185 && !(*bed->check_directives) (abfd, info))
5186 return FALSE;
85fbca6a 5187
d9689752
L
5188 if (!info->check_relocs_after_open_input
5189 && !_bfd_elf_link_check_relocs (abfd, info))
5190 return FALSE;
4ad4eba5
AM
5191
5192 /* If this is a non-traditional link, try to optimize the handling
5193 of the .stab/.stabstr sections. */
5194 if (! dynamic
5195 && ! info->traditional_format
66eb6687 5196 && is_elf_hash_table (htab)
4ad4eba5
AM
5197 && (info->strip != strip_all && info->strip != strip_debugger))
5198 {
5199 asection *stabstr;
5200
5201 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5202 if (stabstr != NULL)
5203 {
5204 bfd_size_type string_offset = 0;
5205 asection *stab;
5206
5207 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5208 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5209 && (!stab->name[5] ||
5210 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5211 && (stab->flags & SEC_MERGE) == 0
5212 && !bfd_is_abs_section (stab->output_section))
5213 {
5214 struct bfd_elf_section_data *secdata;
5215
5216 secdata = elf_section_data (stab);
66eb6687
AM
5217 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5218 stabstr, &secdata->sec_info,
4ad4eba5
AM
5219 &string_offset))
5220 goto error_return;
5221 if (secdata->sec_info)
dbaa2011 5222 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5223 }
5224 }
5225 }
5226
66eb6687 5227 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5228 {
5229 /* Add this bfd to the loaded list. */
5230 struct elf_link_loaded_list *n;
5231
ca4be51c 5232 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5233 if (n == NULL)
5234 goto error_return;
5235 n->abfd = abfd;
66eb6687
AM
5236 n->next = htab->loaded;
5237 htab->loaded = n;
4ad4eba5
AM
5238 }
5239
5240 return TRUE;
5241
5242 error_free_vers:
66eb6687
AM
5243 if (old_tab != NULL)
5244 free (old_tab);
5b677558
AM
5245 if (old_strtab != NULL)
5246 free (old_strtab);
4ad4eba5
AM
5247 if (nondeflt_vers != NULL)
5248 free (nondeflt_vers);
5249 if (extversym != NULL)
5250 free (extversym);
5251 error_free_sym:
5252 if (isymbuf != NULL)
5253 free (isymbuf);
5254 error_return:
5255 return FALSE;
5256}
5257
8387904d
AM
5258/* Return the linker hash table entry of a symbol that might be
5259 satisfied by an archive symbol. Return -1 on error. */
5260
5261struct elf_link_hash_entry *
5262_bfd_elf_archive_symbol_lookup (bfd *abfd,
5263 struct bfd_link_info *info,
5264 const char *name)
5265{
5266 struct elf_link_hash_entry *h;
5267 char *p, *copy;
5268 size_t len, first;
5269
2a41f396 5270 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5271 if (h != NULL)
5272 return h;
5273
5274 /* If this is a default version (the name contains @@), look up the
5275 symbol again with only one `@' as well as without the version.
5276 The effect is that references to the symbol with and without the
5277 version will be matched by the default symbol in the archive. */
5278
5279 p = strchr (name, ELF_VER_CHR);
5280 if (p == NULL || p[1] != ELF_VER_CHR)
5281 return h;
5282
5283 /* First check with only one `@'. */
5284 len = strlen (name);
a50b1753 5285 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5286 if (copy == NULL)
5287 return (struct elf_link_hash_entry *) 0 - 1;
5288
5289 first = p - name + 1;
5290 memcpy (copy, name, first);
5291 memcpy (copy + first, name + first + 1, len - first);
5292
2a41f396 5293 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5294 if (h == NULL)
5295 {
5296 /* We also need to check references to the symbol without the
5297 version. */
5298 copy[first - 1] = '\0';
5299 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5300 FALSE, FALSE, TRUE);
8387904d
AM
5301 }
5302
5303 bfd_release (abfd, copy);
5304 return h;
5305}
5306
0ad989f9 5307/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5308 don't use _bfd_generic_link_add_archive_symbols because we need to
5309 handle versioned symbols.
0ad989f9
L
5310
5311 Fortunately, ELF archive handling is simpler than that done by
5312 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5313 oddities. In ELF, if we find a symbol in the archive map, and the
5314 symbol is currently undefined, we know that we must pull in that
5315 object file.
5316
5317 Unfortunately, we do have to make multiple passes over the symbol
5318 table until nothing further is resolved. */
5319
4ad4eba5
AM
5320static bfd_boolean
5321elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5322{
5323 symindex c;
13e570f8 5324 unsigned char *included = NULL;
0ad989f9
L
5325 carsym *symdefs;
5326 bfd_boolean loop;
5327 bfd_size_type amt;
8387904d
AM
5328 const struct elf_backend_data *bed;
5329 struct elf_link_hash_entry * (*archive_symbol_lookup)
5330 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5331
5332 if (! bfd_has_map (abfd))
5333 {
5334 /* An empty archive is a special case. */
5335 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5336 return TRUE;
5337 bfd_set_error (bfd_error_no_armap);
5338 return FALSE;
5339 }
5340
5341 /* Keep track of all symbols we know to be already defined, and all
5342 files we know to be already included. This is to speed up the
5343 second and subsequent passes. */
5344 c = bfd_ardata (abfd)->symdef_count;
5345 if (c == 0)
5346 return TRUE;
5347 amt = c;
13e570f8
AM
5348 amt *= sizeof (*included);
5349 included = (unsigned char *) bfd_zmalloc (amt);
5350 if (included == NULL)
5351 return FALSE;
0ad989f9
L
5352
5353 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5354 bed = get_elf_backend_data (abfd);
5355 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5356
5357 do
5358 {
5359 file_ptr last;
5360 symindex i;
5361 carsym *symdef;
5362 carsym *symdefend;
5363
5364 loop = FALSE;
5365 last = -1;
5366
5367 symdef = symdefs;
5368 symdefend = symdef + c;
5369 for (i = 0; symdef < symdefend; symdef++, i++)
5370 {
5371 struct elf_link_hash_entry *h;
5372 bfd *element;
5373 struct bfd_link_hash_entry *undefs_tail;
5374 symindex mark;
5375
13e570f8 5376 if (included[i])
0ad989f9
L
5377 continue;
5378 if (symdef->file_offset == last)
5379 {
5380 included[i] = TRUE;
5381 continue;
5382 }
5383
8387904d
AM
5384 h = archive_symbol_lookup (abfd, info, symdef->name);
5385 if (h == (struct elf_link_hash_entry *) 0 - 1)
5386 goto error_return;
0ad989f9
L
5387
5388 if (h == NULL)
5389 continue;
5390
5391 if (h->root.type == bfd_link_hash_common)
5392 {
5393 /* We currently have a common symbol. The archive map contains
5394 a reference to this symbol, so we may want to include it. We
5395 only want to include it however, if this archive element
5396 contains a definition of the symbol, not just another common
5397 declaration of it.
5398
5399 Unfortunately some archivers (including GNU ar) will put
5400 declarations of common symbols into their archive maps, as
5401 well as real definitions, so we cannot just go by the archive
5402 map alone. Instead we must read in the element's symbol
5403 table and check that to see what kind of symbol definition
5404 this is. */
5405 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5406 continue;
5407 }
5408 else if (h->root.type != bfd_link_hash_undefined)
5409 {
5410 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5411 /* Symbol must be defined. Don't check it again. */
5412 included[i] = TRUE;
0ad989f9
L
5413 continue;
5414 }
5415
5416 /* We need to include this archive member. */
5417 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5418 if (element == NULL)
5419 goto error_return;
5420
5421 if (! bfd_check_format (element, bfd_object))
5422 goto error_return;
5423
0ad989f9
L
5424 undefs_tail = info->hash->undefs_tail;
5425
0e144ba7
AM
5426 if (!(*info->callbacks
5427 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5428 continue;
0e144ba7 5429 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5430 goto error_return;
5431
5432 /* If there are any new undefined symbols, we need to make
5433 another pass through the archive in order to see whether
5434 they can be defined. FIXME: This isn't perfect, because
5435 common symbols wind up on undefs_tail and because an
5436 undefined symbol which is defined later on in this pass
5437 does not require another pass. This isn't a bug, but it
5438 does make the code less efficient than it could be. */
5439 if (undefs_tail != info->hash->undefs_tail)
5440 loop = TRUE;
5441
5442 /* Look backward to mark all symbols from this object file
5443 which we have already seen in this pass. */
5444 mark = i;
5445 do
5446 {
5447 included[mark] = TRUE;
5448 if (mark == 0)
5449 break;
5450 --mark;
5451 }
5452 while (symdefs[mark].file_offset == symdef->file_offset);
5453
5454 /* We mark subsequent symbols from this object file as we go
5455 on through the loop. */
5456 last = symdef->file_offset;
5457 }
5458 }
5459 while (loop);
5460
0ad989f9
L
5461 free (included);
5462
5463 return TRUE;
5464
5465 error_return:
0ad989f9
L
5466 if (included != NULL)
5467 free (included);
5468 return FALSE;
5469}
4ad4eba5
AM
5470
5471/* Given an ELF BFD, add symbols to the global hash table as
5472 appropriate. */
5473
5474bfd_boolean
5475bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5476{
5477 switch (bfd_get_format (abfd))
5478 {
5479 case bfd_object:
5480 return elf_link_add_object_symbols (abfd, info);
5481 case bfd_archive:
5482 return elf_link_add_archive_symbols (abfd, info);
5483 default:
5484 bfd_set_error (bfd_error_wrong_format);
5485 return FALSE;
5486 }
5487}
5a580b3a 5488\f
14b1c01e
AM
5489struct hash_codes_info
5490{
5491 unsigned long *hashcodes;
5492 bfd_boolean error;
5493};
a0c8462f 5494
5a580b3a
AM
5495/* This function will be called though elf_link_hash_traverse to store
5496 all hash value of the exported symbols in an array. */
5497
5498static bfd_boolean
5499elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5500{
a50b1753 5501 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5502 const char *name;
5a580b3a
AM
5503 unsigned long ha;
5504 char *alc = NULL;
5505
5a580b3a
AM
5506 /* Ignore indirect symbols. These are added by the versioning code. */
5507 if (h->dynindx == -1)
5508 return TRUE;
5509
5510 name = h->root.root.string;
422f1182 5511 if (h->versioned >= versioned)
5a580b3a 5512 {
422f1182
L
5513 char *p = strchr (name, ELF_VER_CHR);
5514 if (p != NULL)
14b1c01e 5515 {
422f1182
L
5516 alc = (char *) bfd_malloc (p - name + 1);
5517 if (alc == NULL)
5518 {
5519 inf->error = TRUE;
5520 return FALSE;
5521 }
5522 memcpy (alc, name, p - name);
5523 alc[p - name] = '\0';
5524 name = alc;
14b1c01e 5525 }
5a580b3a
AM
5526 }
5527
5528 /* Compute the hash value. */
5529 ha = bfd_elf_hash (name);
5530
5531 /* Store the found hash value in the array given as the argument. */
14b1c01e 5532 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5533
5534 /* And store it in the struct so that we can put it in the hash table
5535 later. */
f6e332e6 5536 h->u.elf_hash_value = ha;
5a580b3a
AM
5537
5538 if (alc != NULL)
5539 free (alc);
5540
5541 return TRUE;
5542}
5543
fdc90cb4
JJ
5544struct collect_gnu_hash_codes
5545{
5546 bfd *output_bfd;
5547 const struct elf_backend_data *bed;
5548 unsigned long int nsyms;
5549 unsigned long int maskbits;
5550 unsigned long int *hashcodes;
5551 unsigned long int *hashval;
5552 unsigned long int *indx;
5553 unsigned long int *counts;
5554 bfd_vma *bitmask;
5555 bfd_byte *contents;
5556 long int min_dynindx;
5557 unsigned long int bucketcount;
5558 unsigned long int symindx;
5559 long int local_indx;
5560 long int shift1, shift2;
5561 unsigned long int mask;
14b1c01e 5562 bfd_boolean error;
fdc90cb4
JJ
5563};
5564
5565/* This function will be called though elf_link_hash_traverse to store
5566 all hash value of the exported symbols in an array. */
5567
5568static bfd_boolean
5569elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5570{
a50b1753 5571 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5572 const char *name;
fdc90cb4
JJ
5573 unsigned long ha;
5574 char *alc = NULL;
5575
fdc90cb4
JJ
5576 /* Ignore indirect symbols. These are added by the versioning code. */
5577 if (h->dynindx == -1)
5578 return TRUE;
5579
5580 /* Ignore also local symbols and undefined symbols. */
5581 if (! (*s->bed->elf_hash_symbol) (h))
5582 return TRUE;
5583
5584 name = h->root.root.string;
422f1182 5585 if (h->versioned >= versioned)
fdc90cb4 5586 {
422f1182
L
5587 char *p = strchr (name, ELF_VER_CHR);
5588 if (p != NULL)
14b1c01e 5589 {
422f1182
L
5590 alc = (char *) bfd_malloc (p - name + 1);
5591 if (alc == NULL)
5592 {
5593 s->error = TRUE;
5594 return FALSE;
5595 }
5596 memcpy (alc, name, p - name);
5597 alc[p - name] = '\0';
5598 name = alc;
14b1c01e 5599 }
fdc90cb4
JJ
5600 }
5601
5602 /* Compute the hash value. */
5603 ha = bfd_elf_gnu_hash (name);
5604
5605 /* Store the found hash value in the array for compute_bucket_count,
5606 and also for .dynsym reordering purposes. */
5607 s->hashcodes[s->nsyms] = ha;
5608 s->hashval[h->dynindx] = ha;
5609 ++s->nsyms;
5610 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5611 s->min_dynindx = h->dynindx;
5612
5613 if (alc != NULL)
5614 free (alc);
5615
5616 return TRUE;
5617}
5618
5619/* This function will be called though elf_link_hash_traverse to do
5620 final dynaminc symbol renumbering. */
5621
5622static bfd_boolean
5623elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5624{
a50b1753 5625 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5626 unsigned long int bucket;
5627 unsigned long int val;
5628
fdc90cb4
JJ
5629 /* Ignore indirect symbols. */
5630 if (h->dynindx == -1)
5631 return TRUE;
5632
5633 /* Ignore also local symbols and undefined symbols. */
5634 if (! (*s->bed->elf_hash_symbol) (h))
5635 {
5636 if (h->dynindx >= s->min_dynindx)
5637 h->dynindx = s->local_indx++;
5638 return TRUE;
5639 }
5640
5641 bucket = s->hashval[h->dynindx] % s->bucketcount;
5642 val = (s->hashval[h->dynindx] >> s->shift1)
5643 & ((s->maskbits >> s->shift1) - 1);
5644 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5645 s->bitmask[val]
5646 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5647 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5648 if (s->counts[bucket] == 1)
5649 /* Last element terminates the chain. */
5650 val |= 1;
5651 bfd_put_32 (s->output_bfd, val,
5652 s->contents + (s->indx[bucket] - s->symindx) * 4);
5653 --s->counts[bucket];
5654 h->dynindx = s->indx[bucket]++;
5655 return TRUE;
5656}
5657
5658/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5659
5660bfd_boolean
5661_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5662{
5663 return !(h->forced_local
5664 || h->root.type == bfd_link_hash_undefined
5665 || h->root.type == bfd_link_hash_undefweak
5666 || ((h->root.type == bfd_link_hash_defined
5667 || h->root.type == bfd_link_hash_defweak)
5668 && h->root.u.def.section->output_section == NULL));
5669}
5670
5a580b3a
AM
5671/* Array used to determine the number of hash table buckets to use
5672 based on the number of symbols there are. If there are fewer than
5673 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5674 fewer than 37 we use 17 buckets, and so forth. We never use more
5675 than 32771 buckets. */
5676
5677static const size_t elf_buckets[] =
5678{
5679 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5680 16411, 32771, 0
5681};
5682
5683/* Compute bucket count for hashing table. We do not use a static set
5684 of possible tables sizes anymore. Instead we determine for all
5685 possible reasonable sizes of the table the outcome (i.e., the
5686 number of collisions etc) and choose the best solution. The
5687 weighting functions are not too simple to allow the table to grow
5688 without bounds. Instead one of the weighting factors is the size.
5689 Therefore the result is always a good payoff between few collisions
5690 (= short chain lengths) and table size. */
5691static size_t
b20dd2ce 5692compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5693 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5694 unsigned long int nsyms,
5695 int gnu_hash)
5a580b3a 5696{
5a580b3a 5697 size_t best_size = 0;
5a580b3a 5698 unsigned long int i;
5a580b3a 5699
5a580b3a
AM
5700 /* We have a problem here. The following code to optimize the table
5701 size requires an integer type with more the 32 bits. If
5702 BFD_HOST_U_64_BIT is set we know about such a type. */
5703#ifdef BFD_HOST_U_64_BIT
5704 if (info->optimize)
5705 {
5a580b3a
AM
5706 size_t minsize;
5707 size_t maxsize;
5708 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5709 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5710 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5711 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5712 unsigned long int *counts;
d40f3da9 5713 bfd_size_type amt;
0883b6e0 5714 unsigned int no_improvement_count = 0;
5a580b3a
AM
5715
5716 /* Possible optimization parameters: if we have NSYMS symbols we say
5717 that the hashing table must at least have NSYMS/4 and at most
5718 2*NSYMS buckets. */
5719 minsize = nsyms / 4;
5720 if (minsize == 0)
5721 minsize = 1;
5722 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5723 if (gnu_hash)
5724 {
5725 if (minsize < 2)
5726 minsize = 2;
5727 if ((best_size & 31) == 0)
5728 ++best_size;
5729 }
5a580b3a
AM
5730
5731 /* Create array where we count the collisions in. We must use bfd_malloc
5732 since the size could be large. */
5733 amt = maxsize;
5734 amt *= sizeof (unsigned long int);
a50b1753 5735 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5736 if (counts == NULL)
fdc90cb4 5737 return 0;
5a580b3a
AM
5738
5739 /* Compute the "optimal" size for the hash table. The criteria is a
5740 minimal chain length. The minor criteria is (of course) the size
5741 of the table. */
5742 for (i = minsize; i < maxsize; ++i)
5743 {
5744 /* Walk through the array of hashcodes and count the collisions. */
5745 BFD_HOST_U_64_BIT max;
5746 unsigned long int j;
5747 unsigned long int fact;
5748
fdc90cb4
JJ
5749 if (gnu_hash && (i & 31) == 0)
5750 continue;
5751
5a580b3a
AM
5752 memset (counts, '\0', i * sizeof (unsigned long int));
5753
5754 /* Determine how often each hash bucket is used. */
5755 for (j = 0; j < nsyms; ++j)
5756 ++counts[hashcodes[j] % i];
5757
5758 /* For the weight function we need some information about the
5759 pagesize on the target. This is information need not be 100%
5760 accurate. Since this information is not available (so far) we
5761 define it here to a reasonable default value. If it is crucial
5762 to have a better value some day simply define this value. */
5763# ifndef BFD_TARGET_PAGESIZE
5764# define BFD_TARGET_PAGESIZE (4096)
5765# endif
5766
fdc90cb4
JJ
5767 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5768 and the chains. */
5769 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5770
5771# if 1
5772 /* Variant 1: optimize for short chains. We add the squares
5773 of all the chain lengths (which favors many small chain
5774 over a few long chains). */
5775 for (j = 0; j < i; ++j)
5776 max += counts[j] * counts[j];
5777
5778 /* This adds penalties for the overall size of the table. */
fdc90cb4 5779 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5780 max *= fact * fact;
5781# else
5782 /* Variant 2: Optimize a lot more for small table. Here we
5783 also add squares of the size but we also add penalties for
5784 empty slots (the +1 term). */
5785 for (j = 0; j < i; ++j)
5786 max += (1 + counts[j]) * (1 + counts[j]);
5787
5788 /* The overall size of the table is considered, but not as
5789 strong as in variant 1, where it is squared. */
fdc90cb4 5790 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5791 max *= fact;
5792# endif
5793
5794 /* Compare with current best results. */
5795 if (max < best_chlen)
5796 {
5797 best_chlen = max;
5798 best_size = i;
ca4be51c 5799 no_improvement_count = 0;
5a580b3a 5800 }
0883b6e0
NC
5801 /* PR 11843: Avoid futile long searches for the best bucket size
5802 when there are a large number of symbols. */
5803 else if (++no_improvement_count == 100)
5804 break;
5a580b3a
AM
5805 }
5806
5807 free (counts);
5808 }
5809 else
5810#endif /* defined (BFD_HOST_U_64_BIT) */
5811 {
5812 /* This is the fallback solution if no 64bit type is available or if we
5813 are not supposed to spend much time on optimizations. We select the
5814 bucket count using a fixed set of numbers. */
5815 for (i = 0; elf_buckets[i] != 0; i++)
5816 {
5817 best_size = elf_buckets[i];
fdc90cb4 5818 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5819 break;
5820 }
fdc90cb4
JJ
5821 if (gnu_hash && best_size < 2)
5822 best_size = 2;
5a580b3a
AM
5823 }
5824
5a580b3a
AM
5825 return best_size;
5826}
5827
d0bf826b
AM
5828/* Size any SHT_GROUP section for ld -r. */
5829
5830bfd_boolean
5831_bfd_elf_size_group_sections (struct bfd_link_info *info)
5832{
5833 bfd *ibfd;
5834
c72f2fb2 5835 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5836 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5837 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5838 return FALSE;
5839 return TRUE;
5840}
5841
04c3a755
NS
5842/* Set a default stack segment size. The value in INFO wins. If it
5843 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5844 undefined it is initialized. */
5845
5846bfd_boolean
5847bfd_elf_stack_segment_size (bfd *output_bfd,
5848 struct bfd_link_info *info,
5849 const char *legacy_symbol,
5850 bfd_vma default_size)
5851{
5852 struct elf_link_hash_entry *h = NULL;
5853
5854 /* Look for legacy symbol. */
5855 if (legacy_symbol)
5856 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5857 FALSE, FALSE, FALSE);
5858 if (h && (h->root.type == bfd_link_hash_defined
5859 || h->root.type == bfd_link_hash_defweak)
5860 && h->def_regular
5861 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5862 {
5863 /* The symbol has no type if specified on the command line. */
5864 h->type = STT_OBJECT;
5865 if (info->stacksize)
695344c0 5866 /* xgettext:c-format */
4eca0228
AM
5867 _bfd_error_handler (_("%B: stack size specified and %s set"),
5868 output_bfd, legacy_symbol);
04c3a755 5869 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5870 /* xgettext:c-format */
4eca0228
AM
5871 _bfd_error_handler (_("%B: %s not absolute"),
5872 output_bfd, legacy_symbol);
04c3a755
NS
5873 else
5874 info->stacksize = h->root.u.def.value;
5875 }
5876
5877 if (!info->stacksize)
5878 /* If the user didn't set a size, or explicitly inhibit the
5879 size, set it now. */
5880 info->stacksize = default_size;
5881
5882 /* Provide the legacy symbol, if it is referenced. */
5883 if (h && (h->root.type == bfd_link_hash_undefined
5884 || h->root.type == bfd_link_hash_undefweak))
5885 {
5886 struct bfd_link_hash_entry *bh = NULL;
5887
5888 if (!(_bfd_generic_link_add_one_symbol
5889 (info, output_bfd, legacy_symbol,
5890 BSF_GLOBAL, bfd_abs_section_ptr,
5891 info->stacksize >= 0 ? info->stacksize : 0,
5892 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5893 return FALSE;
5894
5895 h = (struct elf_link_hash_entry *) bh;
5896 h->def_regular = 1;
5897 h->type = STT_OBJECT;
5898 }
5899
5900 return TRUE;
5901}
5902
b531344c
MR
5903/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5904
5905struct elf_gc_sweep_symbol_info
5906{
5907 struct bfd_link_info *info;
5908 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5909 bfd_boolean);
5910};
5911
5912static bfd_boolean
5913elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5914{
5915 if (!h->mark
5916 && (((h->root.type == bfd_link_hash_defined
5917 || h->root.type == bfd_link_hash_defweak)
5918 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5919 && h->root.u.def.section->gc_mark))
5920 || h->root.type == bfd_link_hash_undefined
5921 || h->root.type == bfd_link_hash_undefweak))
5922 {
5923 struct elf_gc_sweep_symbol_info *inf;
5924
5925 inf = (struct elf_gc_sweep_symbol_info *) data;
5926 (*inf->hide_symbol) (inf->info, h, TRUE);
5927 h->def_regular = 0;
5928 h->ref_regular = 0;
5929 h->ref_regular_nonweak = 0;
5930 }
5931
5932 return TRUE;
5933}
5934
5a580b3a
AM
5935/* Set up the sizes and contents of the ELF dynamic sections. This is
5936 called by the ELF linker emulation before_allocation routine. We
5937 must set the sizes of the sections before the linker sets the
5938 addresses of the various sections. */
5939
5940bfd_boolean
5941bfd_elf_size_dynamic_sections (bfd *output_bfd,
5942 const char *soname,
5943 const char *rpath,
5944 const char *filter_shlib,
7ee314fa
AM
5945 const char *audit,
5946 const char *depaudit,
5a580b3a
AM
5947 const char * const *auxiliary_filters,
5948 struct bfd_link_info *info,
fd91d419 5949 asection **sinterpptr)
5a580b3a 5950{
ef53be89 5951 size_t soname_indx;
5a580b3a
AM
5952 bfd *dynobj;
5953 const struct elf_backend_data *bed;
5a580b3a
AM
5954
5955 *sinterpptr = NULL;
5956
ef53be89 5957 soname_indx = (size_t) -1;
5a580b3a
AM
5958
5959 if (!is_elf_hash_table (info->hash))
5960 return TRUE;
5961
5a580b3a
AM
5962 dynobj = elf_hash_table (info)->dynobj;
5963
9a2a56cc 5964 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 5965 {
902e9fc7
MR
5966 struct bfd_elf_version_tree *verdefs;
5967 struct elf_info_failed asvinfo;
5a580b3a
AM
5968 struct bfd_elf_version_tree *t;
5969 struct bfd_elf_version_expr *d;
902e9fc7 5970 struct elf_info_failed eif;
5a580b3a 5971 bfd_boolean all_defined;
902e9fc7 5972 asection *s;
7ee314fa 5973
5a580b3a 5974 eif.info = info;
5a580b3a
AM
5975 eif.failed = FALSE;
5976
5977 /* If we are supposed to export all symbols into the dynamic symbol
5978 table (this is not the normal case), then do so. */
55255dae 5979 if (info->export_dynamic
0e1862bb 5980 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5981 {
5982 elf_link_hash_traverse (elf_hash_table (info),
5983 _bfd_elf_export_symbol,
5984 &eif);
5985 if (eif.failed)
5986 return FALSE;
5987 }
5988
5989 /* Make all global versions with definition. */
fd91d419 5990 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5991 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5992 if (!d->symver && d->literal)
5a580b3a
AM
5993 {
5994 const char *verstr, *name;
5995 size_t namelen, verlen, newlen;
93252b1c 5996 char *newname, *p, leading_char;
5a580b3a
AM
5997 struct elf_link_hash_entry *newh;
5998
93252b1c 5999 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6000 name = d->pattern;
93252b1c 6001 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6002 verstr = t->name;
6003 verlen = strlen (verstr);
6004 newlen = namelen + verlen + 3;
6005
a50b1753 6006 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6007 if (newname == NULL)
6008 return FALSE;
93252b1c
MF
6009 newname[0] = leading_char;
6010 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6011
6012 /* Check the hidden versioned definition. */
6013 p = newname + namelen;
6014 *p++ = ELF_VER_CHR;
6015 memcpy (p, verstr, verlen + 1);
6016 newh = elf_link_hash_lookup (elf_hash_table (info),
6017 newname, FALSE, FALSE,
6018 FALSE);
6019 if (newh == NULL
6020 || (newh->root.type != bfd_link_hash_defined
6021 && newh->root.type != bfd_link_hash_defweak))
6022 {
6023 /* Check the default versioned definition. */
6024 *p++ = ELF_VER_CHR;
6025 memcpy (p, verstr, verlen + 1);
6026 newh = elf_link_hash_lookup (elf_hash_table (info),
6027 newname, FALSE, FALSE,
6028 FALSE);
6029 }
6030 free (newname);
6031
6032 /* Mark this version if there is a definition and it is
6033 not defined in a shared object. */
6034 if (newh != NULL
f5385ebf 6035 && !newh->def_dynamic
5a580b3a
AM
6036 && (newh->root.type == bfd_link_hash_defined
6037 || newh->root.type == bfd_link_hash_defweak))
6038 d->symver = 1;
6039 }
6040
6041 /* Attach all the symbols to their version information. */
5a580b3a 6042 asvinfo.info = info;
5a580b3a
AM
6043 asvinfo.failed = FALSE;
6044
6045 elf_link_hash_traverse (elf_hash_table (info),
6046 _bfd_elf_link_assign_sym_version,
6047 &asvinfo);
6048 if (asvinfo.failed)
6049 return FALSE;
6050
6051 if (!info->allow_undefined_version)
6052 {
6053 /* Check if all global versions have a definition. */
6054 all_defined = TRUE;
fd91d419 6055 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6056 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6057 if (d->literal && !d->symver && !d->script)
5a580b3a 6058 {
4eca0228 6059 _bfd_error_handler
5a580b3a
AM
6060 (_("%s: undefined version: %s"),
6061 d->pattern, t->name);
6062 all_defined = FALSE;
6063 }
6064
6065 if (!all_defined)
6066 {
6067 bfd_set_error (bfd_error_bad_value);
6068 return FALSE;
6069 }
6070 }
6071
902e9fc7
MR
6072 /* Set up the version definition section. */
6073 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6074 BFD_ASSERT (s != NULL);
5a580b3a 6075
902e9fc7
MR
6076 /* We may have created additional version definitions if we are
6077 just linking a regular application. */
6078 verdefs = info->version_info;
5a580b3a 6079
902e9fc7
MR
6080 /* Skip anonymous version tag. */
6081 if (verdefs != NULL && verdefs->vernum == 0)
6082 verdefs = verdefs->next;
5a580b3a 6083
902e9fc7
MR
6084 if (verdefs == NULL && !info->create_default_symver)
6085 s->flags |= SEC_EXCLUDE;
6086 else
5a580b3a 6087 {
902e9fc7
MR
6088 unsigned int cdefs;
6089 bfd_size_type size;
6090 bfd_byte *p;
6091 Elf_Internal_Verdef def;
6092 Elf_Internal_Verdaux defaux;
6093 struct bfd_link_hash_entry *bh;
6094 struct elf_link_hash_entry *h;
6095 const char *name;
5a580b3a 6096
902e9fc7
MR
6097 cdefs = 0;
6098 size = 0;
5a580b3a 6099
902e9fc7
MR
6100 /* Make space for the base version. */
6101 size += sizeof (Elf_External_Verdef);
6102 size += sizeof (Elf_External_Verdaux);
6103 ++cdefs;
6104
6105 /* Make space for the default version. */
6106 if (info->create_default_symver)
6107 {
6108 size += sizeof (Elf_External_Verdef);
6109 ++cdefs;
3e3b46e5
PB
6110 }
6111
5a580b3a
AM
6112 for (t = verdefs; t != NULL; t = t->next)
6113 {
6114 struct bfd_elf_version_deps *n;
6115
a6cc6b3b
RO
6116 /* Don't emit base version twice. */
6117 if (t->vernum == 0)
6118 continue;
6119
5a580b3a
AM
6120 size += sizeof (Elf_External_Verdef);
6121 size += sizeof (Elf_External_Verdaux);
6122 ++cdefs;
6123
6124 for (n = t->deps; n != NULL; n = n->next)
6125 size += sizeof (Elf_External_Verdaux);
6126 }
6127
eea6121a 6128 s->size = size;
a50b1753 6129 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6130 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6131 return FALSE;
6132
6133 /* Fill in the version definition section. */
6134
6135 p = s->contents;
6136
6137 def.vd_version = VER_DEF_CURRENT;
6138 def.vd_flags = VER_FLG_BASE;
6139 def.vd_ndx = 1;
6140 def.vd_cnt = 1;
3e3b46e5
PB
6141 if (info->create_default_symver)
6142 {
6143 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6144 def.vd_next = sizeof (Elf_External_Verdef);
6145 }
6146 else
6147 {
6148 def.vd_aux = sizeof (Elf_External_Verdef);
6149 def.vd_next = (sizeof (Elf_External_Verdef)
6150 + sizeof (Elf_External_Verdaux));
6151 }
5a580b3a 6152
ef53be89 6153 if (soname_indx != (size_t) -1)
5a580b3a
AM
6154 {
6155 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6156 soname_indx);
6157 def.vd_hash = bfd_elf_hash (soname);
6158 defaux.vda_name = soname_indx;
3e3b46e5 6159 name = soname;
5a580b3a
AM
6160 }
6161 else
6162 {
ef53be89 6163 size_t indx;
5a580b3a 6164
06084812 6165 name = lbasename (output_bfd->filename);
5a580b3a
AM
6166 def.vd_hash = bfd_elf_hash (name);
6167 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6168 name, FALSE);
ef53be89 6169 if (indx == (size_t) -1)
5a580b3a
AM
6170 return FALSE;
6171 defaux.vda_name = indx;
6172 }
6173 defaux.vda_next = 0;
6174
6175 _bfd_elf_swap_verdef_out (output_bfd, &def,
6176 (Elf_External_Verdef *) p);
6177 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6178 if (info->create_default_symver)
6179 {
6180 /* Add a symbol representing this version. */
6181 bh = NULL;
6182 if (! (_bfd_generic_link_add_one_symbol
6183 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6184 0, NULL, FALSE,
6185 get_elf_backend_data (dynobj)->collect, &bh)))
6186 return FALSE;
6187 h = (struct elf_link_hash_entry *) bh;
6188 h->non_elf = 0;
6189 h->def_regular = 1;
6190 h->type = STT_OBJECT;
6191 h->verinfo.vertree = NULL;
6192
6193 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6194 return FALSE;
6195
6196 /* Create a duplicate of the base version with the same
6197 aux block, but different flags. */
6198 def.vd_flags = 0;
6199 def.vd_ndx = 2;
6200 def.vd_aux = sizeof (Elf_External_Verdef);
6201 if (verdefs)
6202 def.vd_next = (sizeof (Elf_External_Verdef)
6203 + sizeof (Elf_External_Verdaux));
6204 else
6205 def.vd_next = 0;
6206 _bfd_elf_swap_verdef_out (output_bfd, &def,
6207 (Elf_External_Verdef *) p);
6208 p += sizeof (Elf_External_Verdef);
6209 }
5a580b3a
AM
6210 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6211 (Elf_External_Verdaux *) p);
6212 p += sizeof (Elf_External_Verdaux);
6213
6214 for (t = verdefs; t != NULL; t = t->next)
6215 {
6216 unsigned int cdeps;
6217 struct bfd_elf_version_deps *n;
5a580b3a 6218
a6cc6b3b
RO
6219 /* Don't emit the base version twice. */
6220 if (t->vernum == 0)
6221 continue;
6222
5a580b3a
AM
6223 cdeps = 0;
6224 for (n = t->deps; n != NULL; n = n->next)
6225 ++cdeps;
6226
6227 /* Add a symbol representing this version. */
6228 bh = NULL;
6229 if (! (_bfd_generic_link_add_one_symbol
6230 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6231 0, NULL, FALSE,
6232 get_elf_backend_data (dynobj)->collect, &bh)))
6233 return FALSE;
6234 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6235 h->non_elf = 0;
6236 h->def_regular = 1;
5a580b3a
AM
6237 h->type = STT_OBJECT;
6238 h->verinfo.vertree = t;
6239
c152c796 6240 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6241 return FALSE;
6242
6243 def.vd_version = VER_DEF_CURRENT;
6244 def.vd_flags = 0;
6245 if (t->globals.list == NULL
6246 && t->locals.list == NULL
6247 && ! t->used)
6248 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6249 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6250 def.vd_cnt = cdeps + 1;
6251 def.vd_hash = bfd_elf_hash (t->name);
6252 def.vd_aux = sizeof (Elf_External_Verdef);
6253 def.vd_next = 0;
a6cc6b3b
RO
6254
6255 /* If a basever node is next, it *must* be the last node in
6256 the chain, otherwise Verdef construction breaks. */
6257 if (t->next != NULL && t->next->vernum == 0)
6258 BFD_ASSERT (t->next->next == NULL);
6259
6260 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6261 def.vd_next = (sizeof (Elf_External_Verdef)
6262 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6263
6264 _bfd_elf_swap_verdef_out (output_bfd, &def,
6265 (Elf_External_Verdef *) p);
6266 p += sizeof (Elf_External_Verdef);
6267
6268 defaux.vda_name = h->dynstr_index;
6269 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6270 h->dynstr_index);
6271 defaux.vda_next = 0;
6272 if (t->deps != NULL)
6273 defaux.vda_next = sizeof (Elf_External_Verdaux);
6274 t->name_indx = defaux.vda_name;
6275
6276 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6277 (Elf_External_Verdaux *) p);
6278 p += sizeof (Elf_External_Verdaux);
6279
6280 for (n = t->deps; n != NULL; n = n->next)
6281 {
6282 if (n->version_needed == NULL)
6283 {
6284 /* This can happen if there was an error in the
6285 version script. */
6286 defaux.vda_name = 0;
6287 }
6288 else
6289 {
6290 defaux.vda_name = n->version_needed->name_indx;
6291 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6292 defaux.vda_name);
6293 }
6294 if (n->next == NULL)
6295 defaux.vda_next = 0;
6296 else
6297 defaux.vda_next = sizeof (Elf_External_Verdaux);
6298
6299 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6300 (Elf_External_Verdaux *) p);
6301 p += sizeof (Elf_External_Verdaux);
6302 }
6303 }
6304
5a580b3a
AM
6305 elf_tdata (output_bfd)->cverdefs = cdefs;
6306 }
6307
5a580b3a
AM
6308 /* Work out the size of the version reference section. */
6309
3d4d4302 6310 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6311 BFD_ASSERT (s != NULL);
6312 {
6313 struct elf_find_verdep_info sinfo;
6314
5a580b3a
AM
6315 sinfo.info = info;
6316 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6317 if (sinfo.vers == 0)
6318 sinfo.vers = 1;
6319 sinfo.failed = FALSE;
6320
6321 elf_link_hash_traverse (elf_hash_table (info),
6322 _bfd_elf_link_find_version_dependencies,
6323 &sinfo);
14b1c01e
AM
6324 if (sinfo.failed)
6325 return FALSE;
5a580b3a
AM
6326
6327 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6328 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6329 else
6330 {
902e9fc7 6331 Elf_Internal_Verneed *vn;
5a580b3a
AM
6332 unsigned int size;
6333 unsigned int crefs;
6334 bfd_byte *p;
6335
a6cc6b3b 6336 /* Build the version dependency section. */
5a580b3a
AM
6337 size = 0;
6338 crefs = 0;
902e9fc7
MR
6339 for (vn = elf_tdata (output_bfd)->verref;
6340 vn != NULL;
6341 vn = vn->vn_nextref)
5a580b3a
AM
6342 {
6343 Elf_Internal_Vernaux *a;
6344
6345 size += sizeof (Elf_External_Verneed);
6346 ++crefs;
902e9fc7 6347 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6348 size += sizeof (Elf_External_Vernaux);
6349 }
6350
eea6121a 6351 s->size = size;
a50b1753 6352 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6353 if (s->contents == NULL)
6354 return FALSE;
6355
6356 p = s->contents;
902e9fc7
MR
6357 for (vn = elf_tdata (output_bfd)->verref;
6358 vn != NULL;
6359 vn = vn->vn_nextref)
5a580b3a
AM
6360 {
6361 unsigned int caux;
6362 Elf_Internal_Vernaux *a;
ef53be89 6363 size_t indx;
5a580b3a
AM
6364
6365 caux = 0;
902e9fc7 6366 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6367 ++caux;
6368
902e9fc7
MR
6369 vn->vn_version = VER_NEED_CURRENT;
6370 vn->vn_cnt = caux;
5a580b3a 6371 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
902e9fc7
MR
6372 elf_dt_name (vn->vn_bfd) != NULL
6373 ? elf_dt_name (vn->vn_bfd)
6374 : lbasename (vn->vn_bfd->filename),
5a580b3a 6375 FALSE);
ef53be89 6376 if (indx == (size_t) -1)
5a580b3a 6377 return FALSE;
902e9fc7
MR
6378 vn->vn_file = indx;
6379 vn->vn_aux = sizeof (Elf_External_Verneed);
6380 if (vn->vn_nextref == NULL)
6381 vn->vn_next = 0;
5a580b3a 6382 else
902e9fc7 6383 vn->vn_next = (sizeof (Elf_External_Verneed)
5a580b3a
AM
6384 + caux * sizeof (Elf_External_Vernaux));
6385
902e9fc7 6386 _bfd_elf_swap_verneed_out (output_bfd, vn,
5a580b3a
AM
6387 (Elf_External_Verneed *) p);
6388 p += sizeof (Elf_External_Verneed);
6389
902e9fc7 6390 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
5a580b3a
AM
6391 {
6392 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6393 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6394 a->vna_nodename, FALSE);
ef53be89 6395 if (indx == (size_t) -1)
5a580b3a
AM
6396 return FALSE;
6397 a->vna_name = indx;
6398 if (a->vna_nextptr == NULL)
6399 a->vna_next = 0;
6400 else
6401 a->vna_next = sizeof (Elf_External_Vernaux);
6402
6403 _bfd_elf_swap_vernaux_out (output_bfd, a,
6404 (Elf_External_Vernaux *) p);
6405 p += sizeof (Elf_External_Vernaux);
6406 }
6407 }
6408
5a580b3a
AM
6409 elf_tdata (output_bfd)->cverrefs = crefs;
6410 }
6411 }
902e9fc7
MR
6412 }
6413
6414 bed = get_elf_backend_data (output_bfd);
6415
6416 if (info->gc_sections && bed->can_gc_sections)
6417 {
6418 struct elf_gc_sweep_symbol_info sweep_info;
6419 unsigned long section_sym_count;
6420
6421 /* Remove the symbols that were in the swept sections from the
6422 dynamic symbol table. GCFIXME: Anyone know how to get them
6423 out of the static symbol table as well? */
6424 sweep_info.info = info;
6425 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6426 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6427 &sweep_info);
6428
6429 _bfd_elf_link_renumber_dynsyms (output_bfd, info, &section_sym_count);
6430 }
6431
6432 /* Any syms created from now on start with -1 in
6433 got.refcount/offset and plt.refcount/offset. */
6434 elf_hash_table (info)->init_got_refcount
6435 = elf_hash_table (info)->init_got_offset;
6436 elf_hash_table (info)->init_plt_refcount
6437 = elf_hash_table (info)->init_plt_offset;
6438
6439 if (bfd_link_relocatable (info)
6440 && !_bfd_elf_size_group_sections (info))
6441 return FALSE;
6442
6443 /* The backend may have to create some sections regardless of whether
6444 we're dynamic or not. */
6445 if (bed->elf_backend_always_size_sections
6446 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6447 return FALSE;
6448
6449 /* Determine any GNU_STACK segment requirements, after the backend
6450 has had a chance to set a default segment size. */
6451 if (info->execstack)
6452 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6453 else if (info->noexecstack)
6454 elf_stack_flags (output_bfd) = PF_R | PF_W;
6455 else
6456 {
6457 bfd *inputobj;
6458 asection *notesec = NULL;
6459 int exec = 0;
6460
6461 for (inputobj = info->input_bfds;
6462 inputobj;
6463 inputobj = inputobj->link.next)
6464 {
6465 asection *s;
6466
6467 if (inputobj->flags
6468 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6469 continue;
6470 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6471 if (s)
6472 {
6473 if (s->flags & SEC_CODE)
6474 exec = PF_X;
6475 notesec = s;
6476 }
6477 else if (bed->default_execstack)
6478 exec = PF_X;
6479 }
6480 if (notesec || info->stacksize > 0)
6481 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6482 if (notesec && exec && bfd_link_relocatable (info)
6483 && notesec->output_section != bfd_abs_section_ptr)
6484 notesec->output_section->flags |= SEC_CODE;
6485 }
6486
6487 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6488 {
6489 struct elf_info_failed eif;
6490 struct elf_link_hash_entry *h;
6491 asection *dynstr;
6492 asection *s;
6493
6494 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6495 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6496
6497 if (soname != NULL)
6498 {
6499 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6500 soname, TRUE);
6501 if (soname_indx == (size_t) -1
6502 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6503 return FALSE;
6504 }
6505
6506 if (info->symbolic)
6507 {
6508 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6509 return FALSE;
6510 info->flags |= DF_SYMBOLIC;
6511 }
6512
6513 if (rpath != NULL)
6514 {
6515 size_t indx;
6516 bfd_vma tag;
6517
6518 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6519 TRUE);
6520 if (indx == (size_t) -1)
6521 return FALSE;
6522
6523 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6524 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6525 return FALSE;
6526 }
6527
6528 if (filter_shlib != NULL)
6529 {
6530 size_t indx;
6531
6532 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6533 filter_shlib, TRUE);
6534 if (indx == (size_t) -1
6535 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6536 return FALSE;
6537 }
6538
6539 if (auxiliary_filters != NULL)
6540 {
6541 const char * const *p;
6542
6543 for (p = auxiliary_filters; *p != NULL; p++)
6544 {
6545 size_t indx;
6546
6547 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6548 *p, TRUE);
6549 if (indx == (size_t) -1
6550 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6551 return FALSE;
6552 }
6553 }
6554
6555 if (audit != NULL)
6556 {
6557 size_t indx;
6558
6559 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6560 TRUE);
6561 if (indx == (size_t) -1
6562 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6563 return FALSE;
6564 }
6565
6566 if (depaudit != NULL)
6567 {
6568 size_t indx;
6569
6570 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6571 TRUE);
6572 if (indx == (size_t) -1
6573 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6574 return FALSE;
6575 }
6576
6577 eif.info = info;
6578 eif.failed = FALSE;
6579
6580 /* Find all symbols which were defined in a dynamic object and make
6581 the backend pick a reasonable value for them. */
6582 elf_link_hash_traverse (elf_hash_table (info),
6583 _bfd_elf_adjust_dynamic_symbol,
6584 &eif);
6585 if (eif.failed)
6586 return FALSE;
6587
6588 /* Add some entries to the .dynamic section. We fill in some of the
6589 values later, in bfd_elf_final_link, but we must add the entries
6590 now so that we know the final size of the .dynamic section. */
6591
6592 /* If there are initialization and/or finalization functions to
6593 call then add the corresponding DT_INIT/DT_FINI entries. */
6594 h = (info->init_function
6595 ? elf_link_hash_lookup (elf_hash_table (info),
6596 info->init_function, FALSE,
6597 FALSE, FALSE)
6598 : NULL);
6599 if (h != NULL
6600 && (h->ref_regular
6601 || h->def_regular))
6602 {
6603 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6604 return FALSE;
6605 }
6606 h = (info->fini_function
6607 ? elf_link_hash_lookup (elf_hash_table (info),
6608 info->fini_function, FALSE,
6609 FALSE, FALSE)
6610 : NULL);
6611 if (h != NULL
6612 && (h->ref_regular
6613 || h->def_regular))
6614 {
6615 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6616 return FALSE;
6617 }
6618
6619 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6620 if (s != NULL && s->linker_has_input)
6621 {
6622 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6623 if (! bfd_link_executable (info))
6624 {
6625 bfd *sub;
6626 asection *o;
6627
6628 for (sub = info->input_bfds; sub != NULL;
6629 sub = sub->link.next)
6630 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6631 for (o = sub->sections; o != NULL; o = o->next)
6632 if (elf_section_data (o)->this_hdr.sh_type
6633 == SHT_PREINIT_ARRAY)
6634 {
6635 _bfd_error_handler
6636 (_("%B: .preinit_array section is not allowed in DSO"),
6637 sub);
6638 break;
6639 }
6640
6641 bfd_set_error (bfd_error_nonrepresentable_section);
6642 return FALSE;
6643 }
6644
6645 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6646 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6647 return FALSE;
6648 }
6649 s = bfd_get_section_by_name (output_bfd, ".init_array");
6650 if (s != NULL && s->linker_has_input)
6651 {
6652 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6653 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6654 return FALSE;
6655 }
6656 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6657 if (s != NULL && s->linker_has_input)
6658 {
6659 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6660 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6661 return FALSE;
6662 }
6663
6664 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6665 /* If .dynstr is excluded from the link, we don't want any of
6666 these tags. Strictly, we should be checking each section
6667 individually; This quick check covers for the case where
6668 someone does a /DISCARD/ : { *(*) }. */
6669 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6670 {
6671 bfd_size_type strsize;
6672
6673 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6674 if ((info->emit_hash
6675 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6676 || (info->emit_gnu_hash
6677 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6678 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6679 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6680 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6681 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6682 bed->s->sizeof_sym))
6683 return FALSE;
6684 }
6685 }
6686
6687 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6688 return FALSE;
6689
6690 /* The backend must work out the sizes of all the other dynamic
6691 sections. */
6692 if (dynobj != NULL
6693 && bed->elf_backend_size_dynamic_sections != NULL
6694 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6695 return FALSE;
6696
6697 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6698 {
6699 unsigned long section_sym_count;
6700
6701 if (elf_tdata (output_bfd)->cverdefs)
6702 {
6703 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6704
6705 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6706 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6707 return FALSE;
6708 }
6709
6710 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6711 {
6712 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6713 return FALSE;
6714 }
6715 else if (info->flags & DF_BIND_NOW)
6716 {
6717 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6718 return FALSE;
6719 }
6720
6721 if (info->flags_1)
6722 {
6723 if (bfd_link_executable (info))
6724 info->flags_1 &= ~ (DF_1_INITFIRST
6725 | DF_1_NODELETE
6726 | DF_1_NOOPEN);
6727 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6728 return FALSE;
6729 }
6730
6731 if (elf_tdata (output_bfd)->cverrefs)
6732 {
6733 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6734
6735 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6736 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6737 return FALSE;
6738 }
5a580b3a 6739
8423293d
AM
6740 if ((elf_tdata (output_bfd)->cverrefs == 0
6741 && elf_tdata (output_bfd)->cverdefs == 0)
6742 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6743 &section_sym_count) == 0)
6744 {
902e9fc7
MR
6745 asection *s;
6746
3d4d4302 6747 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6748 s->flags |= SEC_EXCLUDE;
6749 }
6750 }
6751 return TRUE;
6752}
6753
74541ad4
AM
6754/* Find the first non-excluded output section. We'll use its
6755 section symbol for some emitted relocs. */
6756void
6757_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6758{
6759 asection *s;
6760
6761 for (s = output_bfd->sections; s != NULL; s = s->next)
6762 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6763 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6764 {
6765 elf_hash_table (info)->text_index_section = s;
6766 break;
6767 }
6768}
6769
6770/* Find two non-excluded output sections, one for code, one for data.
6771 We'll use their section symbols for some emitted relocs. */
6772void
6773_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6774{
6775 asection *s;
6776
266b05cf
DJ
6777 /* Data first, since setting text_index_section changes
6778 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6779 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6780 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6781 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6782 {
266b05cf 6783 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6784 break;
6785 }
6786
6787 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6788 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6789 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6790 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6791 {
266b05cf 6792 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6793 break;
6794 }
6795
6796 if (elf_hash_table (info)->text_index_section == NULL)
6797 elf_hash_table (info)->text_index_section
6798 = elf_hash_table (info)->data_index_section;
6799}
6800
8423293d
AM
6801bfd_boolean
6802bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6803{
74541ad4
AM
6804 const struct elf_backend_data *bed;
6805
8423293d
AM
6806 if (!is_elf_hash_table (info->hash))
6807 return TRUE;
6808
74541ad4
AM
6809 bed = get_elf_backend_data (output_bfd);
6810 (*bed->elf_backend_init_index_section) (output_bfd, info);
6811
8423293d
AM
6812 if (elf_hash_table (info)->dynamic_sections_created)
6813 {
6814 bfd *dynobj;
8423293d
AM
6815 asection *s;
6816 bfd_size_type dynsymcount;
6817 unsigned long section_sym_count;
8423293d
AM
6818 unsigned int dtagcount;
6819
6820 dynobj = elf_hash_table (info)->dynobj;
6821
5a580b3a
AM
6822 /* Assign dynsym indicies. In a shared library we generate a
6823 section symbol for each output section, which come first.
6824 Next come all of the back-end allocated local dynamic syms,
6825 followed by the rest of the global symbols. */
6826
554220db
AM
6827 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6828 &section_sym_count);
5a580b3a
AM
6829
6830 /* Work out the size of the symbol version section. */
3d4d4302 6831 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6832 BFD_ASSERT (s != NULL);
d5486c43 6833 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6834 {
eea6121a 6835 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6836 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6837 if (s->contents == NULL)
6838 return FALSE;
6839
6840 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6841 return FALSE;
6842 }
6843
6844 /* Set the size of the .dynsym and .hash sections. We counted
6845 the number of dynamic symbols in elf_link_add_object_symbols.
6846 We will build the contents of .dynsym and .hash when we build
6847 the final symbol table, because until then we do not know the
6848 correct value to give the symbols. We built the .dynstr
6849 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6850 s = elf_hash_table (info)->dynsym;
5a580b3a 6851 BFD_ASSERT (s != NULL);
eea6121a 6852 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6853
d5486c43
L
6854 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6855 if (s->contents == NULL)
6856 return FALSE;
5a580b3a 6857
d5486c43
L
6858 /* The first entry in .dynsym is a dummy symbol. Clear all the
6859 section syms, in case we don't output them all. */
6860 ++section_sym_count;
6861 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6862
fdc90cb4
JJ
6863 elf_hash_table (info)->bucketcount = 0;
6864
5a580b3a
AM
6865 /* Compute the size of the hashing table. As a side effect this
6866 computes the hash values for all the names we export. */
fdc90cb4
JJ
6867 if (info->emit_hash)
6868 {
6869 unsigned long int *hashcodes;
14b1c01e 6870 struct hash_codes_info hashinf;
fdc90cb4
JJ
6871 bfd_size_type amt;
6872 unsigned long int nsyms;
6873 size_t bucketcount;
6874 size_t hash_entry_size;
6875
6876 /* Compute the hash values for all exported symbols. At the same
6877 time store the values in an array so that we could use them for
6878 optimizations. */
6879 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6880 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6881 if (hashcodes == NULL)
6882 return FALSE;
14b1c01e
AM
6883 hashinf.hashcodes = hashcodes;
6884 hashinf.error = FALSE;
5a580b3a 6885
fdc90cb4
JJ
6886 /* Put all hash values in HASHCODES. */
6887 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6888 elf_collect_hash_codes, &hashinf);
6889 if (hashinf.error)
4dd07732
AM
6890 {
6891 free (hashcodes);
6892 return FALSE;
6893 }
5a580b3a 6894
14b1c01e 6895 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6896 bucketcount
6897 = compute_bucket_count (info, hashcodes, nsyms, 0);
6898 free (hashcodes);
6899
6900 if (bucketcount == 0)
6901 return FALSE;
5a580b3a 6902
fdc90cb4
JJ
6903 elf_hash_table (info)->bucketcount = bucketcount;
6904
3d4d4302 6905 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6906 BFD_ASSERT (s != NULL);
6907 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6908 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6909 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6910 if (s->contents == NULL)
6911 return FALSE;
6912
6913 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6914 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6915 s->contents + hash_entry_size);
6916 }
6917
6918 if (info->emit_gnu_hash)
6919 {
6920 size_t i, cnt;
6921 unsigned char *contents;
6922 struct collect_gnu_hash_codes cinfo;
6923 bfd_size_type amt;
6924 size_t bucketcount;
6925
6926 memset (&cinfo, 0, sizeof (cinfo));
6927
6928 /* Compute the hash values for all exported symbols. At the same
6929 time store the values in an array so that we could use them for
6930 optimizations. */
6931 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6932 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6933 if (cinfo.hashcodes == NULL)
6934 return FALSE;
6935
6936 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6937 cinfo.min_dynindx = -1;
6938 cinfo.output_bfd = output_bfd;
6939 cinfo.bed = bed;
6940
6941 /* Put all hash values in HASHCODES. */
6942 elf_link_hash_traverse (elf_hash_table (info),
6943 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6944 if (cinfo.error)
4dd07732
AM
6945 {
6946 free (cinfo.hashcodes);
6947 return FALSE;
6948 }
fdc90cb4
JJ
6949
6950 bucketcount
6951 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6952
6953 if (bucketcount == 0)
6954 {
6955 free (cinfo.hashcodes);
6956 return FALSE;
6957 }
6958
3d4d4302 6959 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6960 BFD_ASSERT (s != NULL);
6961
6962 if (cinfo.nsyms == 0)
6963 {
6964 /* Empty .gnu.hash section is special. */
6965 BFD_ASSERT (cinfo.min_dynindx == -1);
6966 free (cinfo.hashcodes);
6967 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6968 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6969 if (contents == NULL)
6970 return FALSE;
6971 s->contents = contents;
6972 /* 1 empty bucket. */
6973 bfd_put_32 (output_bfd, 1, contents);
6974 /* SYMIDX above the special symbol 0. */
6975 bfd_put_32 (output_bfd, 1, contents + 4);
6976 /* Just one word for bitmask. */
6977 bfd_put_32 (output_bfd, 1, contents + 8);
6978 /* Only hash fn bloom filter. */
6979 bfd_put_32 (output_bfd, 0, contents + 12);
6980 /* No hashes are valid - empty bitmask. */
6981 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6982 /* No hashes in the only bucket. */
6983 bfd_put_32 (output_bfd, 0,
6984 contents + 16 + bed->s->arch_size / 8);
6985 }
6986 else
6987 {
9e6619e2 6988 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6989 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6990
9e6619e2
AM
6991 x = cinfo.nsyms;
6992 maskbitslog2 = 1;
6993 while ((x >>= 1) != 0)
6994 ++maskbitslog2;
fdc90cb4
JJ
6995 if (maskbitslog2 < 3)
6996 maskbitslog2 = 5;
6997 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6998 maskbitslog2 = maskbitslog2 + 3;
6999 else
7000 maskbitslog2 = maskbitslog2 + 2;
7001 if (bed->s->arch_size == 64)
7002 {
7003 if (maskbitslog2 == 5)
7004 maskbitslog2 = 6;
7005 cinfo.shift1 = 6;
7006 }
7007 else
7008 cinfo.shift1 = 5;
7009 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7010 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7011 cinfo.maskbits = 1 << maskbitslog2;
7012 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7013 amt = bucketcount * sizeof (unsigned long int) * 2;
7014 amt += maskwords * sizeof (bfd_vma);
a50b1753 7015 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7016 if (cinfo.bitmask == NULL)
7017 {
7018 free (cinfo.hashcodes);
7019 return FALSE;
7020 }
7021
a50b1753 7022 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7023 cinfo.indx = cinfo.counts + bucketcount;
7024 cinfo.symindx = dynsymcount - cinfo.nsyms;
7025 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7026
7027 /* Determine how often each hash bucket is used. */
7028 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7029 for (i = 0; i < cinfo.nsyms; ++i)
7030 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7031
7032 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7033 if (cinfo.counts[i] != 0)
7034 {
7035 cinfo.indx[i] = cnt;
7036 cnt += cinfo.counts[i];
7037 }
7038 BFD_ASSERT (cnt == dynsymcount);
7039 cinfo.bucketcount = bucketcount;
7040 cinfo.local_indx = cinfo.min_dynindx;
7041
7042 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7043 s->size += cinfo.maskbits / 8;
a50b1753 7044 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7045 if (contents == NULL)
7046 {
7047 free (cinfo.bitmask);
7048 free (cinfo.hashcodes);
7049 return FALSE;
7050 }
7051
7052 s->contents = contents;
7053 bfd_put_32 (output_bfd, bucketcount, contents);
7054 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7055 bfd_put_32 (output_bfd, maskwords, contents + 8);
7056 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7057 contents += 16 + cinfo.maskbits / 8;
7058
7059 for (i = 0; i < bucketcount; ++i)
7060 {
7061 if (cinfo.counts[i] == 0)
7062 bfd_put_32 (output_bfd, 0, contents);
7063 else
7064 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7065 contents += 4;
7066 }
7067
7068 cinfo.contents = contents;
7069
7070 /* Renumber dynamic symbols, populate .gnu.hash section. */
7071 elf_link_hash_traverse (elf_hash_table (info),
7072 elf_renumber_gnu_hash_syms, &cinfo);
7073
7074 contents = s->contents + 16;
7075 for (i = 0; i < maskwords; ++i)
7076 {
7077 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7078 contents);
7079 contents += bed->s->arch_size / 8;
7080 }
7081
7082 free (cinfo.bitmask);
7083 free (cinfo.hashcodes);
7084 }
7085 }
5a580b3a 7086
3d4d4302 7087 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7088 BFD_ASSERT (s != NULL);
7089
4ad4eba5 7090 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7091
eea6121a 7092 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7093
7094 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7095 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7096 return FALSE;
7097 }
7098
7099 return TRUE;
7100}
4d269e42 7101\f
4d269e42
AM
7102/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7103
7104static void
7105merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7106 asection *sec)
7107{
dbaa2011
AM
7108 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7109 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7110}
7111
7112/* Finish SHF_MERGE section merging. */
7113
7114bfd_boolean
630993ec 7115_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7116{
7117 bfd *ibfd;
7118 asection *sec;
7119
7120 if (!is_elf_hash_table (info->hash))
7121 return FALSE;
7122
c72f2fb2 7123 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7124 if ((ibfd->flags & DYNAMIC) == 0
7125 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7126 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7127 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7128 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7129 if ((sec->flags & SEC_MERGE) != 0
7130 && !bfd_is_abs_section (sec->output_section))
7131 {
7132 struct bfd_elf_section_data *secdata;
7133
7134 secdata = elf_section_data (sec);
630993ec 7135 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7136 &elf_hash_table (info)->merge_info,
7137 sec, &secdata->sec_info))
7138 return FALSE;
7139 else if (secdata->sec_info)
dbaa2011 7140 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7141 }
7142
7143 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7144 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7145 merge_sections_remove_hook);
7146 return TRUE;
7147}
7148
7149/* Create an entry in an ELF linker hash table. */
7150
7151struct bfd_hash_entry *
7152_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7153 struct bfd_hash_table *table,
7154 const char *string)
7155{
7156 /* Allocate the structure if it has not already been allocated by a
7157 subclass. */
7158 if (entry == NULL)
7159 {
a50b1753 7160 entry = (struct bfd_hash_entry *)
ca4be51c 7161 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7162 if (entry == NULL)
7163 return entry;
7164 }
7165
7166 /* Call the allocation method of the superclass. */
7167 entry = _bfd_link_hash_newfunc (entry, table, string);
7168 if (entry != NULL)
7169 {
7170 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7171 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7172
7173 /* Set local fields. */
7174 ret->indx = -1;
7175 ret->dynindx = -1;
7176 ret->got = htab->init_got_refcount;
7177 ret->plt = htab->init_plt_refcount;
7178 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7179 - offsetof (struct elf_link_hash_entry, size)));
7180 /* Assume that we have been called by a non-ELF symbol reader.
7181 This flag is then reset by the code which reads an ELF input
7182 file. This ensures that a symbol created by a non-ELF symbol
7183 reader will have the flag set correctly. */
7184 ret->non_elf = 1;
7185 }
7186
7187 return entry;
7188}
7189
7190/* Copy data from an indirect symbol to its direct symbol, hiding the
7191 old indirect symbol. Also used for copying flags to a weakdef. */
7192
7193void
7194_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7195 struct elf_link_hash_entry *dir,
7196 struct elf_link_hash_entry *ind)
7197{
7198 struct elf_link_hash_table *htab;
7199
7200 /* Copy down any references that we may have already seen to the
e81830c5 7201 symbol which just became indirect. */
4d269e42 7202
422f1182 7203 if (dir->versioned != versioned_hidden)
e81830c5
AM
7204 dir->ref_dynamic |= ind->ref_dynamic;
7205 dir->ref_regular |= ind->ref_regular;
7206 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7207 dir->non_got_ref |= ind->non_got_ref;
7208 dir->needs_plt |= ind->needs_plt;
7209 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7210
7211 if (ind->root.type != bfd_link_hash_indirect)
7212 return;
7213
7214 /* Copy over the global and procedure linkage table refcount entries.
7215 These may have been already set up by a check_relocs routine. */
7216 htab = elf_hash_table (info);
7217 if (ind->got.refcount > htab->init_got_refcount.refcount)
7218 {
7219 if (dir->got.refcount < 0)
7220 dir->got.refcount = 0;
7221 dir->got.refcount += ind->got.refcount;
7222 ind->got.refcount = htab->init_got_refcount.refcount;
7223 }
7224
7225 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7226 {
7227 if (dir->plt.refcount < 0)
7228 dir->plt.refcount = 0;
7229 dir->plt.refcount += ind->plt.refcount;
7230 ind->plt.refcount = htab->init_plt_refcount.refcount;
7231 }
7232
7233 if (ind->dynindx != -1)
7234 {
7235 if (dir->dynindx != -1)
7236 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7237 dir->dynindx = ind->dynindx;
7238 dir->dynstr_index = ind->dynstr_index;
7239 ind->dynindx = -1;
7240 ind->dynstr_index = 0;
7241 }
7242}
7243
7244void
7245_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7246 struct elf_link_hash_entry *h,
7247 bfd_boolean force_local)
7248{
3aa14d16
L
7249 /* STT_GNU_IFUNC symbol must go through PLT. */
7250 if (h->type != STT_GNU_IFUNC)
7251 {
7252 h->plt = elf_hash_table (info)->init_plt_offset;
7253 h->needs_plt = 0;
7254 }
4d269e42
AM
7255 if (force_local)
7256 {
7257 h->forced_local = 1;
7258 if (h->dynindx != -1)
7259 {
7260 h->dynindx = -1;
7261 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7262 h->dynstr_index);
7263 }
7264 }
7265}
7266
7bf52ea2
AM
7267/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7268 caller. */
4d269e42
AM
7269
7270bfd_boolean
7271_bfd_elf_link_hash_table_init
7272 (struct elf_link_hash_table *table,
7273 bfd *abfd,
7274 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7275 struct bfd_hash_table *,
7276 const char *),
4dfe6ac6
NC
7277 unsigned int entsize,
7278 enum elf_target_id target_id)
4d269e42
AM
7279{
7280 bfd_boolean ret;
7281 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7282
4d269e42
AM
7283 table->init_got_refcount.refcount = can_refcount - 1;
7284 table->init_plt_refcount.refcount = can_refcount - 1;
7285 table->init_got_offset.offset = -(bfd_vma) 1;
7286 table->init_plt_offset.offset = -(bfd_vma) 1;
7287 /* The first dynamic symbol is a dummy. */
7288 table->dynsymcount = 1;
7289
7290 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7291
4d269e42 7292 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7293 table->hash_table_id = target_id;
4d269e42
AM
7294
7295 return ret;
7296}
7297
7298/* Create an ELF linker hash table. */
7299
7300struct bfd_link_hash_table *
7301_bfd_elf_link_hash_table_create (bfd *abfd)
7302{
7303 struct elf_link_hash_table *ret;
7304 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7305
7bf52ea2 7306 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7307 if (ret == NULL)
7308 return NULL;
7309
7310 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7311 sizeof (struct elf_link_hash_entry),
7312 GENERIC_ELF_DATA))
4d269e42
AM
7313 {
7314 free (ret);
7315 return NULL;
7316 }
d495ab0d 7317 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7318
7319 return &ret->root;
7320}
7321
9f7c3e5e
AM
7322/* Destroy an ELF linker hash table. */
7323
7324void
d495ab0d 7325_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7326{
d495ab0d
AM
7327 struct elf_link_hash_table *htab;
7328
7329 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7330 if (htab->dynstr != NULL)
7331 _bfd_elf_strtab_free (htab->dynstr);
7332 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7333 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7334}
7335
4d269e42
AM
7336/* This is a hook for the ELF emulation code in the generic linker to
7337 tell the backend linker what file name to use for the DT_NEEDED
7338 entry for a dynamic object. */
7339
7340void
7341bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7342{
7343 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7344 && bfd_get_format (abfd) == bfd_object)
7345 elf_dt_name (abfd) = name;
7346}
7347
7348int
7349bfd_elf_get_dyn_lib_class (bfd *abfd)
7350{
7351 int lib_class;
7352 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7353 && bfd_get_format (abfd) == bfd_object)
7354 lib_class = elf_dyn_lib_class (abfd);
7355 else
7356 lib_class = 0;
7357 return lib_class;
7358}
7359
7360void
7361bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7362{
7363 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7364 && bfd_get_format (abfd) == bfd_object)
7365 elf_dyn_lib_class (abfd) = lib_class;
7366}
7367
7368/* Get the list of DT_NEEDED entries for a link. This is a hook for
7369 the linker ELF emulation code. */
7370
7371struct bfd_link_needed_list *
7372bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7373 struct bfd_link_info *info)
7374{
7375 if (! is_elf_hash_table (info->hash))
7376 return NULL;
7377 return elf_hash_table (info)->needed;
7378}
7379
7380/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7381 hook for the linker ELF emulation code. */
7382
7383struct bfd_link_needed_list *
7384bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7385 struct bfd_link_info *info)
7386{
7387 if (! is_elf_hash_table (info->hash))
7388 return NULL;
7389 return elf_hash_table (info)->runpath;
7390}
7391
7392/* Get the name actually used for a dynamic object for a link. This
7393 is the SONAME entry if there is one. Otherwise, it is the string
7394 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7395
7396const char *
7397bfd_elf_get_dt_soname (bfd *abfd)
7398{
7399 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7400 && bfd_get_format (abfd) == bfd_object)
7401 return elf_dt_name (abfd);
7402 return NULL;
7403}
7404
7405/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7406 the ELF linker emulation code. */
7407
7408bfd_boolean
7409bfd_elf_get_bfd_needed_list (bfd *abfd,
7410 struct bfd_link_needed_list **pneeded)
7411{
7412 asection *s;
7413 bfd_byte *dynbuf = NULL;
cb33740c 7414 unsigned int elfsec;
4d269e42
AM
7415 unsigned long shlink;
7416 bfd_byte *extdyn, *extdynend;
7417 size_t extdynsize;
7418 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7419
7420 *pneeded = NULL;
7421
7422 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7423 || bfd_get_format (abfd) != bfd_object)
7424 return TRUE;
7425
7426 s = bfd_get_section_by_name (abfd, ".dynamic");
7427 if (s == NULL || s->size == 0)
7428 return TRUE;
7429
7430 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7431 goto error_return;
7432
7433 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7434 if (elfsec == SHN_BAD)
4d269e42
AM
7435 goto error_return;
7436
7437 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7438
4d269e42
AM
7439 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7440 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7441
7442 extdyn = dynbuf;
7443 extdynend = extdyn + s->size;
7444 for (; extdyn < extdynend; extdyn += extdynsize)
7445 {
7446 Elf_Internal_Dyn dyn;
7447
7448 (*swap_dyn_in) (abfd, extdyn, &dyn);
7449
7450 if (dyn.d_tag == DT_NULL)
7451 break;
7452
7453 if (dyn.d_tag == DT_NEEDED)
7454 {
7455 const char *string;
7456 struct bfd_link_needed_list *l;
7457 unsigned int tagv = dyn.d_un.d_val;
7458 bfd_size_type amt;
7459
7460 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7461 if (string == NULL)
7462 goto error_return;
7463
7464 amt = sizeof *l;
a50b1753 7465 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7466 if (l == NULL)
7467 goto error_return;
7468
7469 l->by = abfd;
7470 l->name = string;
7471 l->next = *pneeded;
7472 *pneeded = l;
7473 }
7474 }
7475
7476 free (dynbuf);
7477
7478 return TRUE;
7479
7480 error_return:
7481 if (dynbuf != NULL)
7482 free (dynbuf);
7483 return FALSE;
7484}
7485
7486struct elf_symbuf_symbol
7487{
7488 unsigned long st_name; /* Symbol name, index in string tbl */
7489 unsigned char st_info; /* Type and binding attributes */
7490 unsigned char st_other; /* Visibilty, and target specific */
7491};
7492
7493struct elf_symbuf_head
7494{
7495 struct elf_symbuf_symbol *ssym;
ef53be89 7496 size_t count;
4d269e42
AM
7497 unsigned int st_shndx;
7498};
7499
7500struct elf_symbol
7501{
7502 union
7503 {
7504 Elf_Internal_Sym *isym;
7505 struct elf_symbuf_symbol *ssym;
7506 } u;
7507 const char *name;
7508};
7509
7510/* Sort references to symbols by ascending section number. */
7511
7512static int
7513elf_sort_elf_symbol (const void *arg1, const void *arg2)
7514{
7515 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7516 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7517
7518 return s1->st_shndx - s2->st_shndx;
7519}
7520
7521static int
7522elf_sym_name_compare (const void *arg1, const void *arg2)
7523{
7524 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7525 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7526 return strcmp (s1->name, s2->name);
7527}
7528
7529static struct elf_symbuf_head *
ef53be89 7530elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7531{
14b1c01e 7532 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7533 struct elf_symbuf_symbol *ssym;
7534 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7535 size_t i, shndx_count, total_size;
4d269e42 7536
a50b1753 7537 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7538 if (indbuf == NULL)
7539 return NULL;
7540
7541 for (ind = indbuf, i = 0; i < symcount; i++)
7542 if (isymbuf[i].st_shndx != SHN_UNDEF)
7543 *ind++ = &isymbuf[i];
7544 indbufend = ind;
7545
7546 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7547 elf_sort_elf_symbol);
7548
7549 shndx_count = 0;
7550 if (indbufend > indbuf)
7551 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7552 if (ind[0]->st_shndx != ind[1]->st_shndx)
7553 shndx_count++;
7554
3ae181ee
L
7555 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7556 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7557 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7558 if (ssymbuf == NULL)
7559 {
7560 free (indbuf);
7561 return NULL;
7562 }
7563
3ae181ee 7564 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7565 ssymbuf->ssym = NULL;
7566 ssymbuf->count = shndx_count;
7567 ssymbuf->st_shndx = 0;
7568 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7569 {
7570 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7571 {
7572 ssymhead++;
7573 ssymhead->ssym = ssym;
7574 ssymhead->count = 0;
7575 ssymhead->st_shndx = (*ind)->st_shndx;
7576 }
7577 ssym->st_name = (*ind)->st_name;
7578 ssym->st_info = (*ind)->st_info;
7579 ssym->st_other = (*ind)->st_other;
7580 ssymhead->count++;
7581 }
ef53be89 7582 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7583 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7584 == total_size));
4d269e42
AM
7585
7586 free (indbuf);
7587 return ssymbuf;
7588}
7589
7590/* Check if 2 sections define the same set of local and global
7591 symbols. */
7592
8f317e31 7593static bfd_boolean
4d269e42
AM
7594bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7595 struct bfd_link_info *info)
7596{
7597 bfd *bfd1, *bfd2;
7598 const struct elf_backend_data *bed1, *bed2;
7599 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7600 size_t symcount1, symcount2;
4d269e42
AM
7601 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7602 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7603 Elf_Internal_Sym *isym, *isymend;
7604 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7605 size_t count1, count2, i;
cb33740c 7606 unsigned int shndx1, shndx2;
4d269e42
AM
7607 bfd_boolean result;
7608
7609 bfd1 = sec1->owner;
7610 bfd2 = sec2->owner;
7611
4d269e42
AM
7612 /* Both sections have to be in ELF. */
7613 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7614 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7615 return FALSE;
7616
7617 if (elf_section_type (sec1) != elf_section_type (sec2))
7618 return FALSE;
7619
4d269e42
AM
7620 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7621 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7622 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7623 return FALSE;
7624
7625 bed1 = get_elf_backend_data (bfd1);
7626 bed2 = get_elf_backend_data (bfd2);
7627 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7628 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7629 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7630 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7631
7632 if (symcount1 == 0 || symcount2 == 0)
7633 return FALSE;
7634
7635 result = FALSE;
7636 isymbuf1 = NULL;
7637 isymbuf2 = NULL;
a50b1753
NC
7638 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7639 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7640
7641 if (ssymbuf1 == NULL)
7642 {
7643 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7644 NULL, NULL, NULL);
7645 if (isymbuf1 == NULL)
7646 goto done;
7647
7648 if (!info->reduce_memory_overheads)
7649 elf_tdata (bfd1)->symbuf = ssymbuf1
7650 = elf_create_symbuf (symcount1, isymbuf1);
7651 }
7652
7653 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7654 {
7655 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7656 NULL, NULL, NULL);
7657 if (isymbuf2 == NULL)
7658 goto done;
7659
7660 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7661 elf_tdata (bfd2)->symbuf = ssymbuf2
7662 = elf_create_symbuf (symcount2, isymbuf2);
7663 }
7664
7665 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7666 {
7667 /* Optimized faster version. */
ef53be89 7668 size_t lo, hi, mid;
4d269e42
AM
7669 struct elf_symbol *symp;
7670 struct elf_symbuf_symbol *ssym, *ssymend;
7671
7672 lo = 0;
7673 hi = ssymbuf1->count;
7674 ssymbuf1++;
7675 count1 = 0;
7676 while (lo < hi)
7677 {
7678 mid = (lo + hi) / 2;
cb33740c 7679 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7680 hi = mid;
cb33740c 7681 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7682 lo = mid + 1;
7683 else
7684 {
7685 count1 = ssymbuf1[mid].count;
7686 ssymbuf1 += mid;
7687 break;
7688 }
7689 }
7690
7691 lo = 0;
7692 hi = ssymbuf2->count;
7693 ssymbuf2++;
7694 count2 = 0;
7695 while (lo < hi)
7696 {
7697 mid = (lo + hi) / 2;
cb33740c 7698 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7699 hi = mid;
cb33740c 7700 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7701 lo = mid + 1;
7702 else
7703 {
7704 count2 = ssymbuf2[mid].count;
7705 ssymbuf2 += mid;
7706 break;
7707 }
7708 }
7709
7710 if (count1 == 0 || count2 == 0 || count1 != count2)
7711 goto done;
7712
ca4be51c
AM
7713 symtable1
7714 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7715 symtable2
7716 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7717 if (symtable1 == NULL || symtable2 == NULL)
7718 goto done;
7719
7720 symp = symtable1;
7721 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7722 ssym < ssymend; ssym++, symp++)
7723 {
7724 symp->u.ssym = ssym;
7725 symp->name = bfd_elf_string_from_elf_section (bfd1,
7726 hdr1->sh_link,
7727 ssym->st_name);
7728 }
7729
7730 symp = symtable2;
7731 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7732 ssym < ssymend; ssym++, symp++)
7733 {
7734 symp->u.ssym = ssym;
7735 symp->name = bfd_elf_string_from_elf_section (bfd2,
7736 hdr2->sh_link,
7737 ssym->st_name);
7738 }
7739
7740 /* Sort symbol by name. */
7741 qsort (symtable1, count1, sizeof (struct elf_symbol),
7742 elf_sym_name_compare);
7743 qsort (symtable2, count1, sizeof (struct elf_symbol),
7744 elf_sym_name_compare);
7745
7746 for (i = 0; i < count1; i++)
7747 /* Two symbols must have the same binding, type and name. */
7748 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7749 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7750 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7751 goto done;
7752
7753 result = TRUE;
7754 goto done;
7755 }
7756
a50b1753
NC
7757 symtable1 = (struct elf_symbol *)
7758 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7759 symtable2 = (struct elf_symbol *)
7760 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7761 if (symtable1 == NULL || symtable2 == NULL)
7762 goto done;
7763
7764 /* Count definitions in the section. */
7765 count1 = 0;
7766 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7767 if (isym->st_shndx == shndx1)
4d269e42
AM
7768 symtable1[count1++].u.isym = isym;
7769
7770 count2 = 0;
7771 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7772 if (isym->st_shndx == shndx2)
4d269e42
AM
7773 symtable2[count2++].u.isym = isym;
7774
7775 if (count1 == 0 || count2 == 0 || count1 != count2)
7776 goto done;
7777
7778 for (i = 0; i < count1; i++)
7779 symtable1[i].name
7780 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7781 symtable1[i].u.isym->st_name);
7782
7783 for (i = 0; i < count2; i++)
7784 symtable2[i].name
7785 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7786 symtable2[i].u.isym->st_name);
7787
7788 /* Sort symbol by name. */
7789 qsort (symtable1, count1, sizeof (struct elf_symbol),
7790 elf_sym_name_compare);
7791 qsort (symtable2, count1, sizeof (struct elf_symbol),
7792 elf_sym_name_compare);
7793
7794 for (i = 0; i < count1; i++)
7795 /* Two symbols must have the same binding, type and name. */
7796 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7797 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7798 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7799 goto done;
7800
7801 result = TRUE;
7802
7803done:
7804 if (symtable1)
7805 free (symtable1);
7806 if (symtable2)
7807 free (symtable2);
7808 if (isymbuf1)
7809 free (isymbuf1);
7810 if (isymbuf2)
7811 free (isymbuf2);
7812
7813 return result;
7814}
7815
7816/* Return TRUE if 2 section types are compatible. */
7817
7818bfd_boolean
7819_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7820 bfd *bbfd, const asection *bsec)
7821{
7822 if (asec == NULL
7823 || bsec == NULL
7824 || abfd->xvec->flavour != bfd_target_elf_flavour
7825 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7826 return TRUE;
7827
7828 return elf_section_type (asec) == elf_section_type (bsec);
7829}
7830\f
c152c796
AM
7831/* Final phase of ELF linker. */
7832
7833/* A structure we use to avoid passing large numbers of arguments. */
7834
7835struct elf_final_link_info
7836{
7837 /* General link information. */
7838 struct bfd_link_info *info;
7839 /* Output BFD. */
7840 bfd *output_bfd;
7841 /* Symbol string table. */
ef10c3ac 7842 struct elf_strtab_hash *symstrtab;
c152c796
AM
7843 /* .hash section. */
7844 asection *hash_sec;
7845 /* symbol version section (.gnu.version). */
7846 asection *symver_sec;
7847 /* Buffer large enough to hold contents of any section. */
7848 bfd_byte *contents;
7849 /* Buffer large enough to hold external relocs of any section. */
7850 void *external_relocs;
7851 /* Buffer large enough to hold internal relocs of any section. */
7852 Elf_Internal_Rela *internal_relocs;
7853 /* Buffer large enough to hold external local symbols of any input
7854 BFD. */
7855 bfd_byte *external_syms;
7856 /* And a buffer for symbol section indices. */
7857 Elf_External_Sym_Shndx *locsym_shndx;
7858 /* Buffer large enough to hold internal local symbols of any input
7859 BFD. */
7860 Elf_Internal_Sym *internal_syms;
7861 /* Array large enough to hold a symbol index for each local symbol
7862 of any input BFD. */
7863 long *indices;
7864 /* Array large enough to hold a section pointer for each local
7865 symbol of any input BFD. */
7866 asection **sections;
ef10c3ac 7867 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7868 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7869 /* Number of STT_FILE syms seen. */
7870 size_t filesym_count;
c152c796
AM
7871};
7872
7873/* This struct is used to pass information to elf_link_output_extsym. */
7874
7875struct elf_outext_info
7876{
7877 bfd_boolean failed;
7878 bfd_boolean localsyms;
34a79995 7879 bfd_boolean file_sym_done;
8b127cbc 7880 struct elf_final_link_info *flinfo;
c152c796
AM
7881};
7882
d9352518
DB
7883
7884/* Support for evaluating a complex relocation.
7885
7886 Complex relocations are generalized, self-describing relocations. The
7887 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7888 relocations themselves.
d9352518
DB
7889
7890 The relocations are use a reserved elf-wide relocation type code (R_RELC
7891 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7892 information (start bit, end bit, word width, etc) into the addend. This
7893 information is extracted from CGEN-generated operand tables within gas.
7894
7895 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7896 internal) representing prefix-notation expressions, including but not
7897 limited to those sorts of expressions normally encoded as addends in the
7898 addend field. The symbol mangling format is:
7899
7900 <node> := <literal>
7901 | <unary-operator> ':' <node>
7902 | <binary-operator> ':' <node> ':' <node>
7903 ;
7904
7905 <literal> := 's' <digits=N> ':' <N character symbol name>
7906 | 'S' <digits=N> ':' <N character section name>
7907 | '#' <hexdigits>
7908 ;
7909
7910 <binary-operator> := as in C
7911 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7912
7913static void
a0c8462f
AM
7914set_symbol_value (bfd *bfd_with_globals,
7915 Elf_Internal_Sym *isymbuf,
7916 size_t locsymcount,
7917 size_t symidx,
7918 bfd_vma val)
d9352518 7919{
8977835c
AM
7920 struct elf_link_hash_entry **sym_hashes;
7921 struct elf_link_hash_entry *h;
7922 size_t extsymoff = locsymcount;
d9352518 7923
8977835c 7924 if (symidx < locsymcount)
d9352518 7925 {
8977835c
AM
7926 Elf_Internal_Sym *sym;
7927
7928 sym = isymbuf + symidx;
7929 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7930 {
7931 /* It is a local symbol: move it to the
7932 "absolute" section and give it a value. */
7933 sym->st_shndx = SHN_ABS;
7934 sym->st_value = val;
7935 return;
7936 }
7937 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7938 extsymoff = 0;
d9352518 7939 }
8977835c
AM
7940
7941 /* It is a global symbol: set its link type
7942 to "defined" and give it a value. */
7943
7944 sym_hashes = elf_sym_hashes (bfd_with_globals);
7945 h = sym_hashes [symidx - extsymoff];
7946 while (h->root.type == bfd_link_hash_indirect
7947 || h->root.type == bfd_link_hash_warning)
7948 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7949 h->root.type = bfd_link_hash_defined;
7950 h->root.u.def.value = val;
7951 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7952}
7953
a0c8462f
AM
7954static bfd_boolean
7955resolve_symbol (const char *name,
7956 bfd *input_bfd,
8b127cbc 7957 struct elf_final_link_info *flinfo,
a0c8462f
AM
7958 bfd_vma *result,
7959 Elf_Internal_Sym *isymbuf,
7960 size_t locsymcount)
d9352518 7961{
a0c8462f
AM
7962 Elf_Internal_Sym *sym;
7963 struct bfd_link_hash_entry *global_entry;
7964 const char *candidate = NULL;
7965 Elf_Internal_Shdr *symtab_hdr;
7966 size_t i;
7967
d9352518
DB
7968 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7969
7970 for (i = 0; i < locsymcount; ++ i)
7971 {
8977835c 7972 sym = isymbuf + i;
d9352518
DB
7973
7974 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7975 continue;
7976
7977 candidate = bfd_elf_string_from_elf_section (input_bfd,
7978 symtab_hdr->sh_link,
7979 sym->st_name);
7980#ifdef DEBUG
0f02bbd9
AM
7981 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7982 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7983#endif
7984 if (candidate && strcmp (candidate, name) == 0)
7985 {
8b127cbc 7986 asection *sec = flinfo->sections [i];
d9352518 7987
0f02bbd9
AM
7988 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7989 *result += sec->output_offset + sec->output_section->vma;
d9352518 7990#ifdef DEBUG
0f02bbd9
AM
7991 printf ("Found symbol with value %8.8lx\n",
7992 (unsigned long) *result);
d9352518
DB
7993#endif
7994 return TRUE;
7995 }
7996 }
7997
7998 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7999 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8000 FALSE, FALSE, TRUE);
d9352518
DB
8001 if (!global_entry)
8002 return FALSE;
a0c8462f 8003
d9352518
DB
8004 if (global_entry->type == bfd_link_hash_defined
8005 || global_entry->type == bfd_link_hash_defweak)
8006 {
a0c8462f
AM
8007 *result = (global_entry->u.def.value
8008 + global_entry->u.def.section->output_section->vma
8009 + global_entry->u.def.section->output_offset);
d9352518 8010#ifdef DEBUG
0f02bbd9
AM
8011 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8012 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8013#endif
8014 return TRUE;
a0c8462f 8015 }
d9352518 8016
d9352518
DB
8017 return FALSE;
8018}
8019
37b01f6a
DG
8020/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8021 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8022 names like "foo.end" which is the end address of section "foo". */
8023
d9352518 8024static bfd_boolean
a0c8462f
AM
8025resolve_section (const char *name,
8026 asection *sections,
37b01f6a
DG
8027 bfd_vma *result,
8028 bfd * abfd)
d9352518 8029{
a0c8462f
AM
8030 asection *curr;
8031 unsigned int len;
d9352518 8032
a0c8462f 8033 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8034 if (strcmp (curr->name, name) == 0)
8035 {
8036 *result = curr->vma;
8037 return TRUE;
8038 }
8039
8040 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8041 /* FIXME: This could be coded more efficiently... */
a0c8462f 8042 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8043 {
8044 len = strlen (curr->name);
a0c8462f 8045 if (len > strlen (name))
d9352518
DB
8046 continue;
8047
8048 if (strncmp (curr->name, name, len) == 0)
8049 {
8050 if (strncmp (".end", name + len, 4) == 0)
8051 {
37b01f6a 8052 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8053 return TRUE;
8054 }
8055
8056 /* Insert more pseudo-section names here, if you like. */
8057 }
8058 }
a0c8462f 8059
d9352518
DB
8060 return FALSE;
8061}
8062
8063static void
a0c8462f 8064undefined_reference (const char *reftype, const char *name)
d9352518 8065{
695344c0 8066 /* xgettext:c-format */
a0c8462f
AM
8067 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8068 reftype, name);
d9352518
DB
8069}
8070
8071static bfd_boolean
a0c8462f
AM
8072eval_symbol (bfd_vma *result,
8073 const char **symp,
8074 bfd *input_bfd,
8b127cbc 8075 struct elf_final_link_info *flinfo,
a0c8462f
AM
8076 bfd_vma dot,
8077 Elf_Internal_Sym *isymbuf,
8078 size_t locsymcount,
8079 int signed_p)
d9352518 8080{
4b93929b
NC
8081 size_t len;
8082 size_t symlen;
a0c8462f
AM
8083 bfd_vma a;
8084 bfd_vma b;
4b93929b 8085 char symbuf[4096];
0f02bbd9 8086 const char *sym = *symp;
a0c8462f
AM
8087 const char *symend;
8088 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8089
8090 len = strlen (sym);
8091 symend = sym + len;
8092
4b93929b 8093 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8094 {
8095 bfd_set_error (bfd_error_invalid_operation);
8096 return FALSE;
8097 }
a0c8462f 8098
d9352518
DB
8099 switch (* sym)
8100 {
8101 case '.':
0f02bbd9
AM
8102 *result = dot;
8103 *symp = sym + 1;
d9352518
DB
8104 return TRUE;
8105
8106 case '#':
0f02bbd9
AM
8107 ++sym;
8108 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8109 return TRUE;
8110
8111 case 'S':
8112 symbol_is_section = TRUE;
1a0670f3 8113 /* Fall through. */
a0c8462f 8114 case 's':
0f02bbd9
AM
8115 ++sym;
8116 symlen = strtol (sym, (char **) symp, 10);
8117 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8118
4b93929b 8119 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8120 {
8121 bfd_set_error (bfd_error_invalid_operation);
8122 return FALSE;
8123 }
8124
8125 memcpy (symbuf, sym, symlen);
a0c8462f 8126 symbuf[symlen] = '\0';
0f02bbd9 8127 *symp = sym + symlen;
a0c8462f
AM
8128
8129 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8130 the symbol as a section, or vice-versa. so we're pretty liberal in our
8131 interpretation here; section means "try section first", not "must be a
8132 section", and likewise with symbol. */
8133
a0c8462f 8134 if (symbol_is_section)
d9352518 8135 {
37b01f6a 8136 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8137 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8138 isymbuf, locsymcount))
d9352518
DB
8139 {
8140 undefined_reference ("section", symbuf);
8141 return FALSE;
8142 }
a0c8462f
AM
8143 }
8144 else
d9352518 8145 {
8b127cbc 8146 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8147 isymbuf, locsymcount)
8b127cbc 8148 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8149 result, input_bfd))
d9352518
DB
8150 {
8151 undefined_reference ("symbol", symbuf);
8152 return FALSE;
8153 }
8154 }
8155
8156 return TRUE;
a0c8462f 8157
d9352518
DB
8158 /* All that remains are operators. */
8159
8160#define UNARY_OP(op) \
8161 if (strncmp (sym, #op, strlen (#op)) == 0) \
8162 { \
8163 sym += strlen (#op); \
a0c8462f
AM
8164 if (*sym == ':') \
8165 ++sym; \
0f02bbd9 8166 *symp = sym; \
8b127cbc 8167 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8168 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8169 return FALSE; \
8170 if (signed_p) \
0f02bbd9 8171 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8172 else \
8173 *result = op a; \
d9352518
DB
8174 return TRUE; \
8175 }
8176
8177#define BINARY_OP(op) \
8178 if (strncmp (sym, #op, strlen (#op)) == 0) \
8179 { \
8180 sym += strlen (#op); \
a0c8462f
AM
8181 if (*sym == ':') \
8182 ++sym; \
0f02bbd9 8183 *symp = sym; \
8b127cbc 8184 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8185 isymbuf, locsymcount, signed_p)) \
a0c8462f 8186 return FALSE; \
0f02bbd9 8187 ++*symp; \
8b127cbc 8188 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8189 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8190 return FALSE; \
8191 if (signed_p) \
0f02bbd9 8192 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8193 else \
8194 *result = a op b; \
d9352518
DB
8195 return TRUE; \
8196 }
8197
8198 default:
8199 UNARY_OP (0-);
8200 BINARY_OP (<<);
8201 BINARY_OP (>>);
8202 BINARY_OP (==);
8203 BINARY_OP (!=);
8204 BINARY_OP (<=);
8205 BINARY_OP (>=);
8206 BINARY_OP (&&);
8207 BINARY_OP (||);
8208 UNARY_OP (~);
8209 UNARY_OP (!);
8210 BINARY_OP (*);
8211 BINARY_OP (/);
8212 BINARY_OP (%);
8213 BINARY_OP (^);
8214 BINARY_OP (|);
8215 BINARY_OP (&);
8216 BINARY_OP (+);
8217 BINARY_OP (-);
8218 BINARY_OP (<);
8219 BINARY_OP (>);
8220#undef UNARY_OP
8221#undef BINARY_OP
8222 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8223 bfd_set_error (bfd_error_invalid_operation);
8224 return FALSE;
8225 }
8226}
8227
d9352518 8228static void
a0c8462f
AM
8229put_value (bfd_vma size,
8230 unsigned long chunksz,
8231 bfd *input_bfd,
8232 bfd_vma x,
8233 bfd_byte *location)
d9352518
DB
8234{
8235 location += (size - chunksz);
8236
41cd1ad1 8237 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8238 {
8239 switch (chunksz)
8240 {
d9352518
DB
8241 case 1:
8242 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8243 x >>= 8;
d9352518
DB
8244 break;
8245 case 2:
8246 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8247 x >>= 16;
d9352518
DB
8248 break;
8249 case 4:
8250 bfd_put_32 (input_bfd, x, location);
65164438
NC
8251 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8252 x >>= 16;
8253 x >>= 16;
d9352518 8254 break;
d9352518 8255#ifdef BFD64
41cd1ad1 8256 case 8:
d9352518 8257 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8258 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8259 x >>= 32;
8260 x >>= 32;
8261 break;
d9352518 8262#endif
41cd1ad1
NC
8263 default:
8264 abort ();
d9352518
DB
8265 break;
8266 }
8267 }
8268}
8269
a0c8462f
AM
8270static bfd_vma
8271get_value (bfd_vma size,
8272 unsigned long chunksz,
8273 bfd *input_bfd,
8274 bfd_byte *location)
d9352518 8275{
9b239e0e 8276 int shift;
d9352518
DB
8277 bfd_vma x = 0;
8278
9b239e0e
NC
8279 /* Sanity checks. */
8280 BFD_ASSERT (chunksz <= sizeof (x)
8281 && size >= chunksz
8282 && chunksz != 0
8283 && (size % chunksz) == 0
8284 && input_bfd != NULL
8285 && location != NULL);
8286
8287 if (chunksz == sizeof (x))
8288 {
8289 BFD_ASSERT (size == chunksz);
8290
8291 /* Make sure that we do not perform an undefined shift operation.
8292 We know that size == chunksz so there will only be one iteration
8293 of the loop below. */
8294 shift = 0;
8295 }
8296 else
8297 shift = 8 * chunksz;
8298
a0c8462f 8299 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8300 {
8301 switch (chunksz)
8302 {
d9352518 8303 case 1:
9b239e0e 8304 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8305 break;
8306 case 2:
9b239e0e 8307 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8308 break;
8309 case 4:
9b239e0e 8310 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8311 break;
d9352518 8312#ifdef BFD64
9b239e0e
NC
8313 case 8:
8314 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8315 break;
9b239e0e
NC
8316#endif
8317 default:
8318 abort ();
d9352518
DB
8319 }
8320 }
8321 return x;
8322}
8323
a0c8462f
AM
8324static void
8325decode_complex_addend (unsigned long *start, /* in bits */
8326 unsigned long *oplen, /* in bits */
8327 unsigned long *len, /* in bits */
8328 unsigned long *wordsz, /* in bytes */
8329 unsigned long *chunksz, /* in bytes */
8330 unsigned long *lsb0_p,
8331 unsigned long *signed_p,
8332 unsigned long *trunc_p,
8333 unsigned long encoded)
d9352518
DB
8334{
8335 * start = encoded & 0x3F;
8336 * len = (encoded >> 6) & 0x3F;
8337 * oplen = (encoded >> 12) & 0x3F;
8338 * wordsz = (encoded >> 18) & 0xF;
8339 * chunksz = (encoded >> 22) & 0xF;
8340 * lsb0_p = (encoded >> 27) & 1;
8341 * signed_p = (encoded >> 28) & 1;
8342 * trunc_p = (encoded >> 29) & 1;
8343}
8344
cdfeee4f 8345bfd_reloc_status_type
0f02bbd9 8346bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8347 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8348 bfd_byte *contents,
8349 Elf_Internal_Rela *rel,
8350 bfd_vma relocation)
d9352518 8351{
0f02bbd9
AM
8352 bfd_vma shift, x, mask;
8353 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8354 bfd_reloc_status_type r;
d9352518
DB
8355
8356 /* Perform this reloc, since it is complex.
8357 (this is not to say that it necessarily refers to a complex
8358 symbol; merely that it is a self-describing CGEN based reloc.
8359 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8360 word size, etc) encoded within it.). */
d9352518 8361
a0c8462f
AM
8362 decode_complex_addend (&start, &oplen, &len, &wordsz,
8363 &chunksz, &lsb0_p, &signed_p,
8364 &trunc_p, rel->r_addend);
d9352518
DB
8365
8366 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8367
8368 if (lsb0_p)
8369 shift = (start + 1) - len;
8370 else
8371 shift = (8 * wordsz) - (start + len);
8372
37b01f6a
DG
8373 x = get_value (wordsz, chunksz, input_bfd,
8374 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8375
8376#ifdef DEBUG
8377 printf ("Doing complex reloc: "
8378 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8379 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8380 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8381 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8382 oplen, (unsigned long) x, (unsigned long) mask,
8383 (unsigned long) relocation);
d9352518
DB
8384#endif
8385
cdfeee4f 8386 r = bfd_reloc_ok;
d9352518 8387 if (! trunc_p)
cdfeee4f
AM
8388 /* Now do an overflow check. */
8389 r = bfd_check_overflow ((signed_p
8390 ? complain_overflow_signed
8391 : complain_overflow_unsigned),
8392 len, 0, (8 * wordsz),
8393 relocation);
a0c8462f 8394
d9352518
DB
8395 /* Do the deed. */
8396 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8397
8398#ifdef DEBUG
8399 printf (" relocation: %8.8lx\n"
8400 " shifted mask: %8.8lx\n"
8401 " shifted/masked reloc: %8.8lx\n"
8402 " result: %8.8lx\n",
9ccb8af9
AM
8403 (unsigned long) relocation, (unsigned long) (mask << shift),
8404 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8405#endif
37b01f6a
DG
8406 put_value (wordsz, chunksz, input_bfd, x,
8407 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8408 return r;
d9352518
DB
8409}
8410
0e287786
AM
8411/* Functions to read r_offset from external (target order) reloc
8412 entry. Faster than bfd_getl32 et al, because we let the compiler
8413 know the value is aligned. */
53df40a4 8414
0e287786
AM
8415static bfd_vma
8416ext32l_r_offset (const void *p)
53df40a4
AM
8417{
8418 union aligned32
8419 {
8420 uint32_t v;
8421 unsigned char c[4];
8422 };
8423 const union aligned32 *a
0e287786 8424 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8425
8426 uint32_t aval = ( (uint32_t) a->c[0]
8427 | (uint32_t) a->c[1] << 8
8428 | (uint32_t) a->c[2] << 16
8429 | (uint32_t) a->c[3] << 24);
0e287786 8430 return aval;
53df40a4
AM
8431}
8432
0e287786
AM
8433static bfd_vma
8434ext32b_r_offset (const void *p)
53df40a4
AM
8435{
8436 union aligned32
8437 {
8438 uint32_t v;
8439 unsigned char c[4];
8440 };
8441 const union aligned32 *a
0e287786 8442 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8443
8444 uint32_t aval = ( (uint32_t) a->c[0] << 24
8445 | (uint32_t) a->c[1] << 16
8446 | (uint32_t) a->c[2] << 8
8447 | (uint32_t) a->c[3]);
0e287786 8448 return aval;
53df40a4
AM
8449}
8450
8451#ifdef BFD_HOST_64_BIT
0e287786
AM
8452static bfd_vma
8453ext64l_r_offset (const void *p)
53df40a4
AM
8454{
8455 union aligned64
8456 {
8457 uint64_t v;
8458 unsigned char c[8];
8459 };
8460 const union aligned64 *a
0e287786 8461 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8462
8463 uint64_t aval = ( (uint64_t) a->c[0]
8464 | (uint64_t) a->c[1] << 8
8465 | (uint64_t) a->c[2] << 16
8466 | (uint64_t) a->c[3] << 24
8467 | (uint64_t) a->c[4] << 32
8468 | (uint64_t) a->c[5] << 40
8469 | (uint64_t) a->c[6] << 48
8470 | (uint64_t) a->c[7] << 56);
0e287786 8471 return aval;
53df40a4
AM
8472}
8473
0e287786
AM
8474static bfd_vma
8475ext64b_r_offset (const void *p)
53df40a4
AM
8476{
8477 union aligned64
8478 {
8479 uint64_t v;
8480 unsigned char c[8];
8481 };
8482 const union aligned64 *a
0e287786 8483 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8484
8485 uint64_t aval = ( (uint64_t) a->c[0] << 56
8486 | (uint64_t) a->c[1] << 48
8487 | (uint64_t) a->c[2] << 40
8488 | (uint64_t) a->c[3] << 32
8489 | (uint64_t) a->c[4] << 24
8490 | (uint64_t) a->c[5] << 16
8491 | (uint64_t) a->c[6] << 8
8492 | (uint64_t) a->c[7]);
0e287786 8493 return aval;
53df40a4
AM
8494}
8495#endif
8496
c152c796
AM
8497/* When performing a relocatable link, the input relocations are
8498 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8499 referenced must be updated. Update all the relocations found in
8500 RELDATA. */
c152c796 8501
bca6d0e3 8502static bfd_boolean
c152c796 8503elf_link_adjust_relocs (bfd *abfd,
9eaff861 8504 asection *sec,
28dbcedc
AM
8505 struct bfd_elf_section_reloc_data *reldata,
8506 bfd_boolean sort)
c152c796
AM
8507{
8508 unsigned int i;
8509 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8510 bfd_byte *erela;
8511 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8512 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8513 bfd_vma r_type_mask;
8514 int r_sym_shift;
d4730f92
BS
8515 unsigned int count = reldata->count;
8516 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8517
d4730f92 8518 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8519 {
8520 swap_in = bed->s->swap_reloc_in;
8521 swap_out = bed->s->swap_reloc_out;
8522 }
d4730f92 8523 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8524 {
8525 swap_in = bed->s->swap_reloca_in;
8526 swap_out = bed->s->swap_reloca_out;
8527 }
8528 else
8529 abort ();
8530
8531 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8532 abort ();
8533
8534 if (bed->s->arch_size == 32)
8535 {
8536 r_type_mask = 0xff;
8537 r_sym_shift = 8;
8538 }
8539 else
8540 {
8541 r_type_mask = 0xffffffff;
8542 r_sym_shift = 32;
8543 }
8544
d4730f92
BS
8545 erela = reldata->hdr->contents;
8546 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8547 {
8548 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8549 unsigned int j;
8550
8551 if (*rel_hash == NULL)
8552 continue;
8553
8554 BFD_ASSERT ((*rel_hash)->indx >= 0);
8555
8556 (*swap_in) (abfd, erela, irela);
8557 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8558 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8559 | (irela[j].r_info & r_type_mask));
8560 (*swap_out) (abfd, irela, erela);
8561 }
53df40a4 8562
9eaff861
AO
8563 if (bed->elf_backend_update_relocs)
8564 (*bed->elf_backend_update_relocs) (sec, reldata);
8565
0e287786 8566 if (sort && count != 0)
53df40a4 8567 {
0e287786
AM
8568 bfd_vma (*ext_r_off) (const void *);
8569 bfd_vma r_off;
8570 size_t elt_size;
8571 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8572 bfd_byte *buf = NULL;
28dbcedc
AM
8573
8574 if (bed->s->arch_size == 32)
8575 {
8576 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8577 ext_r_off = ext32l_r_offset;
28dbcedc 8578 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8579 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8580 else
8581 abort ();
8582 }
53df40a4 8583 else
28dbcedc 8584 {
53df40a4 8585#ifdef BFD_HOST_64_BIT
28dbcedc 8586 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8587 ext_r_off = ext64l_r_offset;
28dbcedc 8588 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8589 ext_r_off = ext64b_r_offset;
28dbcedc 8590 else
53df40a4 8591#endif
28dbcedc
AM
8592 abort ();
8593 }
0e287786 8594
bca6d0e3
AM
8595 /* Must use a stable sort here. A modified insertion sort,
8596 since the relocs are mostly sorted already. */
0e287786
AM
8597 elt_size = reldata->hdr->sh_entsize;
8598 base = reldata->hdr->contents;
8599 end = base + count * elt_size;
bca6d0e3 8600 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8601 abort ();
8602
8603 /* Ensure the first element is lowest. This acts as a sentinel,
8604 speeding the main loop below. */
8605 r_off = (*ext_r_off) (base);
8606 for (p = loc = base; (p += elt_size) < end; )
8607 {
8608 bfd_vma r_off2 = (*ext_r_off) (p);
8609 if (r_off > r_off2)
8610 {
8611 r_off = r_off2;
8612 loc = p;
8613 }
8614 }
8615 if (loc != base)
8616 {
8617 /* Don't just swap *base and *loc as that changes the order
8618 of the original base[0] and base[1] if they happen to
8619 have the same r_offset. */
bca6d0e3
AM
8620 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8621 memcpy (onebuf, loc, elt_size);
0e287786 8622 memmove (base + elt_size, base, loc - base);
bca6d0e3 8623 memcpy (base, onebuf, elt_size);
0e287786
AM
8624 }
8625
b29b8669 8626 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8627 {
8628 /* base to p is sorted, *p is next to insert. */
8629 r_off = (*ext_r_off) (p);
8630 /* Search the sorted region for location to insert. */
8631 loc = p - elt_size;
8632 while (r_off < (*ext_r_off) (loc))
8633 loc -= elt_size;
8634 loc += elt_size;
8635 if (loc != p)
8636 {
bca6d0e3
AM
8637 /* Chances are there is a run of relocs to insert here,
8638 from one of more input files. Files are not always
8639 linked in order due to the way elf_link_input_bfd is
8640 called. See pr17666. */
8641 size_t sortlen = p - loc;
8642 bfd_vma r_off2 = (*ext_r_off) (loc);
8643 size_t runlen = elt_size;
8644 size_t buf_size = 96 * 1024;
8645 while (p + runlen < end
8646 && (sortlen <= buf_size
8647 || runlen + elt_size <= buf_size)
8648 && r_off2 > (*ext_r_off) (p + runlen))
8649 runlen += elt_size;
8650 if (buf == NULL)
8651 {
8652 buf = bfd_malloc (buf_size);
8653 if (buf == NULL)
8654 return FALSE;
8655 }
8656 if (runlen < sortlen)
8657 {
8658 memcpy (buf, p, runlen);
8659 memmove (loc + runlen, loc, sortlen);
8660 memcpy (loc, buf, runlen);
8661 }
8662 else
8663 {
8664 memcpy (buf, loc, sortlen);
8665 memmove (loc, p, runlen);
8666 memcpy (loc + runlen, buf, sortlen);
8667 }
b29b8669 8668 p += runlen - elt_size;
0e287786
AM
8669 }
8670 }
8671 /* Hashes are no longer valid. */
28dbcedc
AM
8672 free (reldata->hashes);
8673 reldata->hashes = NULL;
bca6d0e3 8674 free (buf);
53df40a4 8675 }
bca6d0e3 8676 return TRUE;
c152c796
AM
8677}
8678
8679struct elf_link_sort_rela
8680{
8681 union {
8682 bfd_vma offset;
8683 bfd_vma sym_mask;
8684 } u;
8685 enum elf_reloc_type_class type;
8686 /* We use this as an array of size int_rels_per_ext_rel. */
8687 Elf_Internal_Rela rela[1];
8688};
8689
8690static int
8691elf_link_sort_cmp1 (const void *A, const void *B)
8692{
a50b1753
NC
8693 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8694 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8695 int relativea, relativeb;
8696
8697 relativea = a->type == reloc_class_relative;
8698 relativeb = b->type == reloc_class_relative;
8699
8700 if (relativea < relativeb)
8701 return 1;
8702 if (relativea > relativeb)
8703 return -1;
8704 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8705 return -1;
8706 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8707 return 1;
8708 if (a->rela->r_offset < b->rela->r_offset)
8709 return -1;
8710 if (a->rela->r_offset > b->rela->r_offset)
8711 return 1;
8712 return 0;
8713}
8714
8715static int
8716elf_link_sort_cmp2 (const void *A, const void *B)
8717{
a50b1753
NC
8718 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8719 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8720
7e612e98 8721 if (a->type < b->type)
c152c796 8722 return -1;
7e612e98 8723 if (a->type > b->type)
c152c796 8724 return 1;
7e612e98 8725 if (a->u.offset < b->u.offset)
c152c796 8726 return -1;
7e612e98 8727 if (a->u.offset > b->u.offset)
c152c796
AM
8728 return 1;
8729 if (a->rela->r_offset < b->rela->r_offset)
8730 return -1;
8731 if (a->rela->r_offset > b->rela->r_offset)
8732 return 1;
8733 return 0;
8734}
8735
8736static size_t
8737elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8738{
3410fea8 8739 asection *dynamic_relocs;
fc66a176
L
8740 asection *rela_dyn;
8741 asection *rel_dyn;
c152c796
AM
8742 bfd_size_type count, size;
8743 size_t i, ret, sort_elt, ext_size;
8744 bfd_byte *sort, *s_non_relative, *p;
8745 struct elf_link_sort_rela *sq;
8746 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8747 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8748 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8749 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8750 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8751 struct bfd_link_order *lo;
8752 bfd_vma r_sym_mask;
3410fea8 8753 bfd_boolean use_rela;
c152c796 8754
3410fea8
NC
8755 /* Find a dynamic reloc section. */
8756 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8757 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8758 if (rela_dyn != NULL && rela_dyn->size > 0
8759 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8760 {
3410fea8
NC
8761 bfd_boolean use_rela_initialised = FALSE;
8762
8763 /* This is just here to stop gcc from complaining.
c8e44c6d 8764 Its initialization checking code is not perfect. */
3410fea8
NC
8765 use_rela = TRUE;
8766
8767 /* Both sections are present. Examine the sizes
8768 of the indirect sections to help us choose. */
8769 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8770 if (lo->type == bfd_indirect_link_order)
8771 {
8772 asection *o = lo->u.indirect.section;
8773
8774 if ((o->size % bed->s->sizeof_rela) == 0)
8775 {
8776 if ((o->size % bed->s->sizeof_rel) == 0)
8777 /* Section size is divisible by both rel and rela sizes.
8778 It is of no help to us. */
8779 ;
8780 else
8781 {
8782 /* Section size is only divisible by rela. */
8783 if (use_rela_initialised && (use_rela == FALSE))
8784 {
c8e44c6d
AM
8785 _bfd_error_handler (_("%B: Unable to sort relocs - "
8786 "they are in more than one size"),
8787 abfd);
3410fea8
NC
8788 bfd_set_error (bfd_error_invalid_operation);
8789 return 0;
8790 }
8791 else
8792 {
8793 use_rela = TRUE;
8794 use_rela_initialised = TRUE;
8795 }
8796 }
8797 }
8798 else if ((o->size % bed->s->sizeof_rel) == 0)
8799 {
8800 /* Section size is only divisible by rel. */
8801 if (use_rela_initialised && (use_rela == TRUE))
8802 {
c8e44c6d
AM
8803 _bfd_error_handler (_("%B: Unable to sort relocs - "
8804 "they are in more than one size"),
8805 abfd);
3410fea8
NC
8806 bfd_set_error (bfd_error_invalid_operation);
8807 return 0;
8808 }
8809 else
8810 {
8811 use_rela = FALSE;
8812 use_rela_initialised = TRUE;
8813 }
8814 }
8815 else
8816 {
c8e44c6d
AM
8817 /* The section size is not divisible by either -
8818 something is wrong. */
8819 _bfd_error_handler (_("%B: Unable to sort relocs - "
8820 "they are of an unknown size"), abfd);
3410fea8
NC
8821 bfd_set_error (bfd_error_invalid_operation);
8822 return 0;
8823 }
8824 }
8825
8826 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8827 if (lo->type == bfd_indirect_link_order)
8828 {
8829 asection *o = lo->u.indirect.section;
8830
8831 if ((o->size % bed->s->sizeof_rela) == 0)
8832 {
8833 if ((o->size % bed->s->sizeof_rel) == 0)
8834 /* Section size is divisible by both rel and rela sizes.
8835 It is of no help to us. */
8836 ;
8837 else
8838 {
8839 /* Section size is only divisible by rela. */
8840 if (use_rela_initialised && (use_rela == FALSE))
8841 {
c8e44c6d
AM
8842 _bfd_error_handler (_("%B: Unable to sort relocs - "
8843 "they are in more than one size"),
8844 abfd);
3410fea8
NC
8845 bfd_set_error (bfd_error_invalid_operation);
8846 return 0;
8847 }
8848 else
8849 {
8850 use_rela = TRUE;
8851 use_rela_initialised = TRUE;
8852 }
8853 }
8854 }
8855 else if ((o->size % bed->s->sizeof_rel) == 0)
8856 {
8857 /* Section size is only divisible by rel. */
8858 if (use_rela_initialised && (use_rela == TRUE))
8859 {
c8e44c6d
AM
8860 _bfd_error_handler (_("%B: Unable to sort relocs - "
8861 "they are in more than one size"),
8862 abfd);
3410fea8
NC
8863 bfd_set_error (bfd_error_invalid_operation);
8864 return 0;
8865 }
8866 else
8867 {
8868 use_rela = FALSE;
8869 use_rela_initialised = TRUE;
8870 }
8871 }
8872 else
8873 {
c8e44c6d
AM
8874 /* The section size is not divisible by either -
8875 something is wrong. */
8876 _bfd_error_handler (_("%B: Unable to sort relocs - "
8877 "they are of an unknown size"), abfd);
3410fea8
NC
8878 bfd_set_error (bfd_error_invalid_operation);
8879 return 0;
8880 }
8881 }
8882
8883 if (! use_rela_initialised)
8884 /* Make a guess. */
8885 use_rela = TRUE;
c152c796 8886 }
fc66a176
L
8887 else if (rela_dyn != NULL && rela_dyn->size > 0)
8888 use_rela = TRUE;
8889 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8890 use_rela = FALSE;
c152c796 8891 else
fc66a176 8892 return 0;
3410fea8
NC
8893
8894 if (use_rela)
c152c796 8895 {
3410fea8 8896 dynamic_relocs = rela_dyn;
c152c796
AM
8897 ext_size = bed->s->sizeof_rela;
8898 swap_in = bed->s->swap_reloca_in;
8899 swap_out = bed->s->swap_reloca_out;
8900 }
3410fea8
NC
8901 else
8902 {
8903 dynamic_relocs = rel_dyn;
8904 ext_size = bed->s->sizeof_rel;
8905 swap_in = bed->s->swap_reloc_in;
8906 swap_out = bed->s->swap_reloc_out;
8907 }
c152c796
AM
8908
8909 size = 0;
3410fea8 8910 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8911 if (lo->type == bfd_indirect_link_order)
3410fea8 8912 size += lo->u.indirect.section->size;
c152c796 8913
3410fea8 8914 if (size != dynamic_relocs->size)
c152c796
AM
8915 return 0;
8916
8917 sort_elt = (sizeof (struct elf_link_sort_rela)
8918 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8919
8920 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8921 if (count == 0)
8922 return 0;
a50b1753 8923 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8924
c152c796
AM
8925 if (sort == NULL)
8926 {
8927 (*info->callbacks->warning)
8928 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8929 return 0;
8930 }
8931
8932 if (bed->s->arch_size == 32)
8933 r_sym_mask = ~(bfd_vma) 0xff;
8934 else
8935 r_sym_mask = ~(bfd_vma) 0xffffffff;
8936
3410fea8 8937 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8938 if (lo->type == bfd_indirect_link_order)
8939 {
8940 bfd_byte *erel, *erelend;
8941 asection *o = lo->u.indirect.section;
8942
1da212d6
AM
8943 if (o->contents == NULL && o->size != 0)
8944 {
8945 /* This is a reloc section that is being handled as a normal
8946 section. See bfd_section_from_shdr. We can't combine
8947 relocs in this case. */
8948 free (sort);
8949 return 0;
8950 }
c152c796 8951 erel = o->contents;
eea6121a 8952 erelend = o->contents + o->size;
c8e44c6d 8953 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8954
c152c796
AM
8955 while (erel < erelend)
8956 {
8957 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8958
c152c796 8959 (*swap_in) (abfd, erel, s->rela);
7e612e98 8960 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8961 s->u.sym_mask = r_sym_mask;
8962 p += sort_elt;
8963 erel += ext_size;
8964 }
8965 }
8966
8967 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8968
8969 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8970 {
8971 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8972 if (s->type != reloc_class_relative)
8973 break;
8974 }
8975 ret = i;
8976 s_non_relative = p;
8977
8978 sq = (struct elf_link_sort_rela *) s_non_relative;
8979 for (; i < count; i++, p += sort_elt)
8980 {
8981 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8982 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8983 sq = sp;
8984 sp->u.offset = sq->rela->r_offset;
8985 }
8986
8987 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8988
c8e44c6d
AM
8989 struct elf_link_hash_table *htab = elf_hash_table (info);
8990 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8991 {
8992 /* We have plt relocs in .rela.dyn. */
8993 sq = (struct elf_link_sort_rela *) sort;
8994 for (i = 0; i < count; i++)
8995 if (sq[count - i - 1].type != reloc_class_plt)
8996 break;
8997 if (i != 0 && htab->srelplt->size == i * ext_size)
8998 {
8999 struct bfd_link_order **plo;
9000 /* Put srelplt link_order last. This is so the output_offset
9001 set in the next loop is correct for DT_JMPREL. */
9002 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9003 if ((*plo)->type == bfd_indirect_link_order
9004 && (*plo)->u.indirect.section == htab->srelplt)
9005 {
9006 lo = *plo;
9007 *plo = lo->next;
9008 }
9009 else
9010 plo = &(*plo)->next;
9011 *plo = lo;
9012 lo->next = NULL;
9013 dynamic_relocs->map_tail.link_order = lo;
9014 }
9015 }
9016
9017 p = sort;
3410fea8 9018 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9019 if (lo->type == bfd_indirect_link_order)
9020 {
9021 bfd_byte *erel, *erelend;
9022 asection *o = lo->u.indirect.section;
9023
9024 erel = o->contents;
eea6121a 9025 erelend = o->contents + o->size;
c8e44c6d 9026 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9027 while (erel < erelend)
9028 {
9029 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9030 (*swap_out) (abfd, s->rela, erel);
9031 p += sort_elt;
9032 erel += ext_size;
9033 }
9034 }
9035
9036 free (sort);
3410fea8 9037 *psec = dynamic_relocs;
c152c796
AM
9038 return ret;
9039}
9040
ef10c3ac 9041/* Add a symbol to the output symbol string table. */
c152c796 9042
6e0b88f1 9043static int
ef10c3ac
L
9044elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9045 const char *name,
9046 Elf_Internal_Sym *elfsym,
9047 asection *input_sec,
9048 struct elf_link_hash_entry *h)
c152c796 9049{
6e0b88f1 9050 int (*output_symbol_hook)
c152c796
AM
9051 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9052 struct elf_link_hash_entry *);
ef10c3ac 9053 struct elf_link_hash_table *hash_table;
c152c796 9054 const struct elf_backend_data *bed;
ef10c3ac 9055 bfd_size_type strtabsize;
c152c796 9056
8539e4e8
AM
9057 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9058
8b127cbc 9059 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9060 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9061 if (output_symbol_hook != NULL)
9062 {
8b127cbc 9063 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9064 if (ret != 1)
9065 return ret;
c152c796
AM
9066 }
9067
ef10c3ac
L
9068 if (name == NULL
9069 || *name == '\0'
9070 || (input_sec->flags & SEC_EXCLUDE))
9071 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9072 else
9073 {
ef10c3ac
L
9074 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9075 to get the final offset for st_name. */
9076 elfsym->st_name
9077 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9078 name, FALSE);
c152c796 9079 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9080 return 0;
c152c796
AM
9081 }
9082
ef10c3ac
L
9083 hash_table = elf_hash_table (flinfo->info);
9084 strtabsize = hash_table->strtabsize;
9085 if (strtabsize <= hash_table->strtabcount)
c152c796 9086 {
ef10c3ac
L
9087 strtabsize += strtabsize;
9088 hash_table->strtabsize = strtabsize;
9089 strtabsize *= sizeof (*hash_table->strtab);
9090 hash_table->strtab
9091 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9092 strtabsize);
9093 if (hash_table->strtab == NULL)
6e0b88f1 9094 return 0;
c152c796 9095 }
ef10c3ac
L
9096 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9097 hash_table->strtab[hash_table->strtabcount].dest_index
9098 = hash_table->strtabcount;
9099 hash_table->strtab[hash_table->strtabcount].destshndx_index
9100 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9101
9102 bfd_get_symcount (flinfo->output_bfd) += 1;
9103 hash_table->strtabcount += 1;
9104
9105 return 1;
9106}
9107
9108/* Swap symbols out to the symbol table and flush the output symbols to
9109 the file. */
9110
9111static bfd_boolean
9112elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9113{
9114 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9115 bfd_size_type amt;
9116 size_t i;
ef10c3ac
L
9117 const struct elf_backend_data *bed;
9118 bfd_byte *symbuf;
9119 Elf_Internal_Shdr *hdr;
9120 file_ptr pos;
9121 bfd_boolean ret;
9122
9123 if (!hash_table->strtabcount)
9124 return TRUE;
9125
9126 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9127
9128 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9129
ef10c3ac
L
9130 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9131 symbuf = (bfd_byte *) bfd_malloc (amt);
9132 if (symbuf == NULL)
9133 return FALSE;
1b786873 9134
ef10c3ac 9135 if (flinfo->symshndxbuf)
c152c796 9136 {
ef53be89
AM
9137 amt = sizeof (Elf_External_Sym_Shndx);
9138 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9139 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9140 if (flinfo->symshndxbuf == NULL)
c152c796 9141 {
ef10c3ac
L
9142 free (symbuf);
9143 return FALSE;
c152c796 9144 }
c152c796
AM
9145 }
9146
ef10c3ac
L
9147 for (i = 0; i < hash_table->strtabcount; i++)
9148 {
9149 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9150 if (elfsym->sym.st_name == (unsigned long) -1)
9151 elfsym->sym.st_name = 0;
9152 else
9153 elfsym->sym.st_name
9154 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9155 elfsym->sym.st_name);
9156 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9157 ((bfd_byte *) symbuf
9158 + (elfsym->dest_index
9159 * bed->s->sizeof_sym)),
9160 (flinfo->symshndxbuf
9161 + elfsym->destshndx_index));
9162 }
9163
9164 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9165 pos = hdr->sh_offset + hdr->sh_size;
9166 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9167 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9168 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9169 {
9170 hdr->sh_size += amt;
9171 ret = TRUE;
9172 }
9173 else
9174 ret = FALSE;
c152c796 9175
ef10c3ac
L
9176 free (symbuf);
9177
9178 free (hash_table->strtab);
9179 hash_table->strtab = NULL;
9180
9181 return ret;
c152c796
AM
9182}
9183
c0d5a53d
L
9184/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9185
9186static bfd_boolean
9187check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9188{
4fbb74a6
AM
9189 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9190 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9191 {
9192 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9193 beyond 64k. */
4eca0228 9194 _bfd_error_handler
695344c0 9195 /* xgettext:c-format */
c0d5a53d 9196 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9197 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9198 bfd_set_error (bfd_error_nonrepresentable_section);
9199 return FALSE;
9200 }
9201 return TRUE;
9202}
9203
c152c796
AM
9204/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9205 allowing an unsatisfied unversioned symbol in the DSO to match a
9206 versioned symbol that would normally require an explicit version.
9207 We also handle the case that a DSO references a hidden symbol
9208 which may be satisfied by a versioned symbol in another DSO. */
9209
9210static bfd_boolean
9211elf_link_check_versioned_symbol (struct bfd_link_info *info,
9212 const struct elf_backend_data *bed,
9213 struct elf_link_hash_entry *h)
9214{
9215 bfd *abfd;
9216 struct elf_link_loaded_list *loaded;
9217
9218 if (!is_elf_hash_table (info->hash))
9219 return FALSE;
9220
90c984fc
L
9221 /* Check indirect symbol. */
9222 while (h->root.type == bfd_link_hash_indirect)
9223 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9224
c152c796
AM
9225 switch (h->root.type)
9226 {
9227 default:
9228 abfd = NULL;
9229 break;
9230
9231 case bfd_link_hash_undefined:
9232 case bfd_link_hash_undefweak:
9233 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9234 if (abfd == NULL
9235 || (abfd->flags & DYNAMIC) == 0
e56f61be 9236 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9237 return FALSE;
9238 break;
9239
9240 case bfd_link_hash_defined:
9241 case bfd_link_hash_defweak:
9242 abfd = h->root.u.def.section->owner;
9243 break;
9244
9245 case bfd_link_hash_common:
9246 abfd = h->root.u.c.p->section->owner;
9247 break;
9248 }
9249 BFD_ASSERT (abfd != NULL);
9250
9251 for (loaded = elf_hash_table (info)->loaded;
9252 loaded != NULL;
9253 loaded = loaded->next)
9254 {
9255 bfd *input;
9256 Elf_Internal_Shdr *hdr;
ef53be89
AM
9257 size_t symcount;
9258 size_t extsymcount;
9259 size_t extsymoff;
c152c796
AM
9260 Elf_Internal_Shdr *versymhdr;
9261 Elf_Internal_Sym *isym;
9262 Elf_Internal_Sym *isymend;
9263 Elf_Internal_Sym *isymbuf;
9264 Elf_External_Versym *ever;
9265 Elf_External_Versym *extversym;
9266
9267 input = loaded->abfd;
9268
9269 /* We check each DSO for a possible hidden versioned definition. */
9270 if (input == abfd
9271 || (input->flags & DYNAMIC) == 0
9272 || elf_dynversym (input) == 0)
9273 continue;
9274
9275 hdr = &elf_tdata (input)->dynsymtab_hdr;
9276
9277 symcount = hdr->sh_size / bed->s->sizeof_sym;
9278 if (elf_bad_symtab (input))
9279 {
9280 extsymcount = symcount;
9281 extsymoff = 0;
9282 }
9283 else
9284 {
9285 extsymcount = symcount - hdr->sh_info;
9286 extsymoff = hdr->sh_info;
9287 }
9288
9289 if (extsymcount == 0)
9290 continue;
9291
9292 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9293 NULL, NULL, NULL);
9294 if (isymbuf == NULL)
9295 return FALSE;
9296
9297 /* Read in any version definitions. */
9298 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9299 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9300 if (extversym == NULL)
9301 goto error_ret;
9302
9303 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9304 || (bfd_bread (extversym, versymhdr->sh_size, input)
9305 != versymhdr->sh_size))
9306 {
9307 free (extversym);
9308 error_ret:
9309 free (isymbuf);
9310 return FALSE;
9311 }
9312
9313 ever = extversym + extsymoff;
9314 isymend = isymbuf + extsymcount;
9315 for (isym = isymbuf; isym < isymend; isym++, ever++)
9316 {
9317 const char *name;
9318 Elf_Internal_Versym iver;
9319 unsigned short version_index;
9320
9321 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9322 || isym->st_shndx == SHN_UNDEF)
9323 continue;
9324
9325 name = bfd_elf_string_from_elf_section (input,
9326 hdr->sh_link,
9327 isym->st_name);
9328 if (strcmp (name, h->root.root.string) != 0)
9329 continue;
9330
9331 _bfd_elf_swap_versym_in (input, ever, &iver);
9332
d023c380
L
9333 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9334 && !(h->def_regular
9335 && h->forced_local))
c152c796
AM
9336 {
9337 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9338 have provided a definition for the undefined sym unless
9339 it is defined in a non-shared object and forced local.
9340 */
c152c796
AM
9341 abort ();
9342 }
9343
9344 version_index = iver.vs_vers & VERSYM_VERSION;
9345 if (version_index == 1 || version_index == 2)
9346 {
9347 /* This is the base or first version. We can use it. */
9348 free (extversym);
9349 free (isymbuf);
9350 return TRUE;
9351 }
9352 }
9353
9354 free (extversym);
9355 free (isymbuf);
9356 }
9357
9358 return FALSE;
9359}
9360
b8871f35
L
9361/* Convert ELF common symbol TYPE. */
9362
9363static int
9364elf_link_convert_common_type (struct bfd_link_info *info, int type)
9365{
9366 /* Commom symbol can only appear in relocatable link. */
9367 if (!bfd_link_relocatable (info))
9368 abort ();
9369 switch (info->elf_stt_common)
9370 {
9371 case unchanged:
9372 break;
9373 case elf_stt_common:
9374 type = STT_COMMON;
9375 break;
9376 case no_elf_stt_common:
9377 type = STT_OBJECT;
9378 break;
9379 }
9380 return type;
9381}
9382
c152c796
AM
9383/* Add an external symbol to the symbol table. This is called from
9384 the hash table traversal routine. When generating a shared object,
9385 we go through the symbol table twice. The first time we output
9386 anything that might have been forced to local scope in a version
9387 script. The second time we output the symbols that are still
9388 global symbols. */
9389
9390static bfd_boolean
7686d77d 9391elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9392{
7686d77d 9393 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9394 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9395 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9396 bfd_boolean strip;
9397 Elf_Internal_Sym sym;
9398 asection *input_sec;
9399 const struct elf_backend_data *bed;
6e0b88f1
AM
9400 long indx;
9401 int ret;
b8871f35 9402 unsigned int type;
c152c796
AM
9403
9404 if (h->root.type == bfd_link_hash_warning)
9405 {
9406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9407 if (h->root.type == bfd_link_hash_new)
9408 return TRUE;
9409 }
9410
9411 /* Decide whether to output this symbol in this pass. */
9412 if (eoinfo->localsyms)
9413 {
4deb8f71 9414 if (!h->forced_local)
c152c796
AM
9415 return TRUE;
9416 }
9417 else
9418 {
4deb8f71 9419 if (h->forced_local)
c152c796
AM
9420 return TRUE;
9421 }
9422
8b127cbc 9423 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9424
12ac1cf5 9425 if (h->root.type == bfd_link_hash_undefined)
c152c796 9426 {
12ac1cf5
NC
9427 /* If we have an undefined symbol reference here then it must have
9428 come from a shared library that is being linked in. (Undefined
98da7939
L
9429 references in regular files have already been handled unless
9430 they are in unreferenced sections which are removed by garbage
9431 collection). */
12ac1cf5
NC
9432 bfd_boolean ignore_undef = FALSE;
9433
9434 /* Some symbols may be special in that the fact that they're
9435 undefined can be safely ignored - let backend determine that. */
9436 if (bed->elf_backend_ignore_undef_symbol)
9437 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9438
9439 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9440 if (!ignore_undef
12ac1cf5 9441 && h->ref_dynamic
8b127cbc
AM
9442 && (!h->ref_regular || flinfo->info->gc_sections)
9443 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9444 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9445 (*flinfo->info->callbacks->undefined_symbol)
9446 (flinfo->info, h->root.root.string,
9447 h->ref_regular ? NULL : h->root.u.undef.abfd,
9448 NULL, 0,
9449 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9450
9451 /* Strip a global symbol defined in a discarded section. */
9452 if (h->indx == -3)
9453 return TRUE;
c152c796
AM
9454 }
9455
9456 /* We should also warn if a forced local symbol is referenced from
9457 shared libraries. */
0e1862bb 9458 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9459 && h->forced_local
9460 && h->ref_dynamic
371a5866 9461 && h->def_regular
f5385ebf 9462 && !h->dynamic_def
ee659f1f 9463 && h->ref_dynamic_nonweak
8b127cbc 9464 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9465 {
17d078c5
AM
9466 bfd *def_bfd;
9467 const char *msg;
90c984fc
L
9468 struct elf_link_hash_entry *hi = h;
9469
9470 /* Check indirect symbol. */
9471 while (hi->root.type == bfd_link_hash_indirect)
9472 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9473
9474 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9475 /* xgettext:c-format */
17d078c5
AM
9476 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9477 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9478 /* xgettext:c-format */
17d078c5
AM
9479 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9480 else
695344c0 9481 /* xgettext:c-format */
17d078c5 9482 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9483 def_bfd = flinfo->output_bfd;
90c984fc
L
9484 if (hi->root.u.def.section != bfd_abs_section_ptr)
9485 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9486 _bfd_error_handler (msg, flinfo->output_bfd,
9487 h->root.root.string, def_bfd);
17d078c5 9488 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9489 eoinfo->failed = TRUE;
9490 return FALSE;
9491 }
9492
9493 /* We don't want to output symbols that have never been mentioned by
9494 a regular file, or that we have been told to strip. However, if
9495 h->indx is set to -2, the symbol is used by a reloc and we must
9496 output it. */
d983c8c5 9497 strip = FALSE;
c152c796 9498 if (h->indx == -2)
d983c8c5 9499 ;
f5385ebf 9500 else if ((h->def_dynamic
77cfaee6
AM
9501 || h->ref_dynamic
9502 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9503 && !h->def_regular
9504 && !h->ref_regular)
c152c796 9505 strip = TRUE;
8b127cbc 9506 else if (flinfo->info->strip == strip_all)
c152c796 9507 strip = TRUE;
8b127cbc
AM
9508 else if (flinfo->info->strip == strip_some
9509 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9510 h->root.root.string, FALSE, FALSE) == NULL)
9511 strip = TRUE;
d56d55e7
AM
9512 else if ((h->root.type == bfd_link_hash_defined
9513 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9514 && ((flinfo->info->strip_discarded
dbaa2011 9515 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9516 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9517 && h->root.u.def.section->owner != NULL
d56d55e7 9518 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9519 strip = TRUE;
9e2278f5
AM
9520 else if ((h->root.type == bfd_link_hash_undefined
9521 || h->root.type == bfd_link_hash_undefweak)
9522 && h->root.u.undef.abfd != NULL
9523 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9524 strip = TRUE;
c152c796 9525
b8871f35
L
9526 type = h->type;
9527
c152c796 9528 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9529 nothing else to do. However, if it is a forced local symbol or
9530 an ifunc symbol we need to give the backend finish_dynamic_symbol
9531 function a chance to make it dynamic. */
c152c796
AM
9532 if (strip
9533 && h->dynindx == -1
b8871f35 9534 && type != STT_GNU_IFUNC
f5385ebf 9535 && !h->forced_local)
c152c796
AM
9536 return TRUE;
9537
9538 sym.st_value = 0;
9539 sym.st_size = h->size;
9540 sym.st_other = h->other;
c152c796
AM
9541 switch (h->root.type)
9542 {
9543 default:
9544 case bfd_link_hash_new:
9545 case bfd_link_hash_warning:
9546 abort ();
9547 return FALSE;
9548
9549 case bfd_link_hash_undefined:
9550 case bfd_link_hash_undefweak:
9551 input_sec = bfd_und_section_ptr;
9552 sym.st_shndx = SHN_UNDEF;
9553 break;
9554
9555 case bfd_link_hash_defined:
9556 case bfd_link_hash_defweak:
9557 {
9558 input_sec = h->root.u.def.section;
9559 if (input_sec->output_section != NULL)
9560 {
9561 sym.st_shndx =
8b127cbc 9562 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9563 input_sec->output_section);
9564 if (sym.st_shndx == SHN_BAD)
9565 {
4eca0228 9566 _bfd_error_handler
695344c0 9567 /* xgettext:c-format */
d003868e 9568 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9569 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9570 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9571 eoinfo->failed = TRUE;
9572 return FALSE;
9573 }
9574
9575 /* ELF symbols in relocatable files are section relative,
9576 but in nonrelocatable files they are virtual
9577 addresses. */
9578 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9579 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9580 {
9581 sym.st_value += input_sec->output_section->vma;
9582 if (h->type == STT_TLS)
9583 {
8b127cbc 9584 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9585 if (tls_sec != NULL)
9586 sym.st_value -= tls_sec->vma;
c152c796
AM
9587 }
9588 }
9589 }
9590 else
9591 {
9592 BFD_ASSERT (input_sec->owner == NULL
9593 || (input_sec->owner->flags & DYNAMIC) != 0);
9594 sym.st_shndx = SHN_UNDEF;
9595 input_sec = bfd_und_section_ptr;
9596 }
9597 }
9598 break;
9599
9600 case bfd_link_hash_common:
9601 input_sec = h->root.u.c.p->section;
a4d8e49b 9602 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9603 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9604 break;
9605
9606 case bfd_link_hash_indirect:
9607 /* These symbols are created by symbol versioning. They point
9608 to the decorated version of the name. For example, if the
9609 symbol foo@@GNU_1.2 is the default, which should be used when
9610 foo is used with no version, then we add an indirect symbol
9611 foo which points to foo@@GNU_1.2. We ignore these symbols,
9612 since the indirected symbol is already in the hash table. */
9613 return TRUE;
9614 }
9615
b8871f35
L
9616 if (type == STT_COMMON || type == STT_OBJECT)
9617 switch (h->root.type)
9618 {
9619 case bfd_link_hash_common:
9620 type = elf_link_convert_common_type (flinfo->info, type);
9621 break;
9622 case bfd_link_hash_defined:
9623 case bfd_link_hash_defweak:
9624 if (bed->common_definition (&sym))
9625 type = elf_link_convert_common_type (flinfo->info, type);
9626 else
9627 type = STT_OBJECT;
9628 break;
9629 case bfd_link_hash_undefined:
9630 case bfd_link_hash_undefweak:
9631 break;
9632 default:
9633 abort ();
9634 }
9635
4deb8f71 9636 if (h->forced_local)
b8871f35
L
9637 {
9638 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9639 /* Turn off visibility on local symbol. */
9640 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9641 }
9642 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9643 else if (h->unique_global && h->def_regular)
9644 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9645 else if (h->root.type == bfd_link_hash_undefweak
9646 || h->root.type == bfd_link_hash_defweak)
9647 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9648 else
9649 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9650 sym.st_target_internal = h->target_internal;
9651
c152c796
AM
9652 /* Give the processor backend a chance to tweak the symbol value,
9653 and also to finish up anything that needs to be done for this
9654 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9655 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9656 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9657 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9658 && h->def_regular
0e1862bb 9659 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9660 || ((h->dynindx != -1
9661 || h->forced_local)
0e1862bb 9662 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9663 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9664 || h->root.type != bfd_link_hash_undefweak))
9665 || !h->forced_local)
8b127cbc 9666 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9667 {
9668 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9669 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9670 {
9671 eoinfo->failed = TRUE;
9672 return FALSE;
9673 }
9674 }
9675
9676 /* If we are marking the symbol as undefined, and there are no
9677 non-weak references to this symbol from a regular object, then
9678 mark the symbol as weak undefined; if there are non-weak
9679 references, mark the symbol as strong. We can't do this earlier,
9680 because it might not be marked as undefined until the
9681 finish_dynamic_symbol routine gets through with it. */
9682 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9683 && h->ref_regular
c152c796
AM
9684 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9685 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9686 {
9687 int bindtype;
b8871f35 9688 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9689
9690 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9691 if (type == STT_GNU_IFUNC)
9692 type = STT_FUNC;
c152c796 9693
f5385ebf 9694 if (h->ref_regular_nonweak)
c152c796
AM
9695 bindtype = STB_GLOBAL;
9696 else
9697 bindtype = STB_WEAK;
2955ec4c 9698 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9699 }
9700
bda987c2
CD
9701 /* If this is a symbol defined in a dynamic library, don't use the
9702 symbol size from the dynamic library. Relinking an executable
9703 against a new library may introduce gratuitous changes in the
9704 executable's symbols if we keep the size. */
9705 if (sym.st_shndx == SHN_UNDEF
9706 && !h->def_regular
9707 && h->def_dynamic)
9708 sym.st_size = 0;
9709
c152c796
AM
9710 /* If a non-weak symbol with non-default visibility is not defined
9711 locally, it is a fatal error. */
0e1862bb 9712 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9713 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9714 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9715 && h->root.type == bfd_link_hash_undefined
f5385ebf 9716 && !h->def_regular)
c152c796 9717 {
17d078c5
AM
9718 const char *msg;
9719
9720 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9721 /* xgettext:c-format */
17d078c5
AM
9722 msg = _("%B: protected symbol `%s' isn't defined");
9723 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9724 /* xgettext:c-format */
17d078c5
AM
9725 msg = _("%B: internal symbol `%s' isn't defined");
9726 else
695344c0 9727 /* xgettext:c-format */
17d078c5 9728 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9729 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9730 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9731 eoinfo->failed = TRUE;
9732 return FALSE;
9733 }
9734
9735 /* If this symbol should be put in the .dynsym section, then put it
9736 there now. We already know the symbol index. We also fill in
9737 the entry in the .hash section. */
cae1fbbb 9738 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9739 && h->dynindx != -1
8b127cbc 9740 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9741 {
c152c796
AM
9742 bfd_byte *esym;
9743
90c984fc
L
9744 /* Since there is no version information in the dynamic string,
9745 if there is no version info in symbol version section, we will
1659f720 9746 have a run-time problem if not linking executable, referenced
4deb8f71 9747 by shared library, or not bound locally. */
1659f720 9748 if (h->verinfo.verdef == NULL
0e1862bb 9749 && (!bfd_link_executable (flinfo->info)
1659f720
L
9750 || h->ref_dynamic
9751 || !h->def_regular))
90c984fc
L
9752 {
9753 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9754
9755 if (p && p [1] != '\0')
9756 {
4eca0228 9757 _bfd_error_handler
695344c0 9758 /* xgettext:c-format */
90c984fc
L
9759 (_("%B: No symbol version section for versioned symbol `%s'"),
9760 flinfo->output_bfd, h->root.root.string);
9761 eoinfo->failed = TRUE;
9762 return FALSE;
9763 }
9764 }
9765
c152c796 9766 sym.st_name = h->dynstr_index;
cae1fbbb
L
9767 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9768 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9769 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9770 {
9771 eoinfo->failed = TRUE;
9772 return FALSE;
9773 }
8b127cbc 9774 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9775
8b127cbc 9776 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9777 {
9778 size_t hash_entry_size;
9779 bfd_byte *bucketpos;
9780 bfd_vma chain;
41198d0c
L
9781 size_t bucketcount;
9782 size_t bucket;
9783
8b127cbc 9784 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9785 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9786
9787 hash_entry_size
8b127cbc
AM
9788 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9789 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9790 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9791 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9792 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9793 bucketpos);
9794 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9795 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9796 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9797 }
c152c796 9798
8b127cbc 9799 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9800 {
9801 Elf_Internal_Versym iversym;
9802 Elf_External_Versym *eversym;
9803
f5385ebf 9804 if (!h->def_regular)
c152c796 9805 {
7b20f099
AM
9806 if (h->verinfo.verdef == NULL
9807 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9808 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9809 iversym.vs_vers = 0;
9810 else
9811 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9812 }
9813 else
9814 {
9815 if (h->verinfo.vertree == NULL)
9816 iversym.vs_vers = 1;
9817 else
9818 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9819 if (flinfo->info->create_default_symver)
3e3b46e5 9820 iversym.vs_vers++;
c152c796
AM
9821 }
9822
422f1182 9823 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9824 defined locally. */
422f1182 9825 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9826 iversym.vs_vers |= VERSYM_HIDDEN;
9827
8b127cbc 9828 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9829 eversym += h->dynindx;
8b127cbc 9830 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9831 }
9832 }
9833
d983c8c5
AM
9834 /* If the symbol is undefined, and we didn't output it to .dynsym,
9835 strip it from .symtab too. Obviously we can't do this for
9836 relocatable output or when needed for --emit-relocs. */
9837 else if (input_sec == bfd_und_section_ptr
9838 && h->indx != -2
0e1862bb 9839 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9840 return TRUE;
9841 /* Also strip others that we couldn't earlier due to dynamic symbol
9842 processing. */
9843 if (strip)
9844 return TRUE;
9845 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9846 return TRUE;
9847
2ec55de3
AM
9848 /* Output a FILE symbol so that following locals are not associated
9849 with the wrong input file. We need one for forced local symbols
9850 if we've seen more than one FILE symbol or when we have exactly
9851 one FILE symbol but global symbols are present in a file other
9852 than the one with the FILE symbol. We also need one if linker
9853 defined symbols are present. In practice these conditions are
9854 always met, so just emit the FILE symbol unconditionally. */
9855 if (eoinfo->localsyms
9856 && !eoinfo->file_sym_done
9857 && eoinfo->flinfo->filesym_count != 0)
9858 {
9859 Elf_Internal_Sym fsym;
9860
9861 memset (&fsym, 0, sizeof (fsym));
9862 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9863 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9864 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9865 bfd_und_section_ptr, NULL))
2ec55de3
AM
9866 return FALSE;
9867
9868 eoinfo->file_sym_done = TRUE;
9869 }
9870
8b127cbc 9871 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9872 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9873 input_sec, h);
6e0b88f1 9874 if (ret == 0)
c152c796
AM
9875 {
9876 eoinfo->failed = TRUE;
9877 return FALSE;
9878 }
6e0b88f1
AM
9879 else if (ret == 1)
9880 h->indx = indx;
9881 else if (h->indx == -2)
9882 abort();
c152c796
AM
9883
9884 return TRUE;
9885}
9886
cdd3575c
AM
9887/* Return TRUE if special handling is done for relocs in SEC against
9888 symbols defined in discarded sections. */
9889
c152c796
AM
9890static bfd_boolean
9891elf_section_ignore_discarded_relocs (asection *sec)
9892{
9893 const struct elf_backend_data *bed;
9894
cdd3575c
AM
9895 switch (sec->sec_info_type)
9896 {
dbaa2011
AM
9897 case SEC_INFO_TYPE_STABS:
9898 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9899 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9900 return TRUE;
9901 default:
9902 break;
9903 }
c152c796
AM
9904
9905 bed = get_elf_backend_data (sec->owner);
9906 if (bed->elf_backend_ignore_discarded_relocs != NULL
9907 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9908 return TRUE;
9909
9910 return FALSE;
9911}
9912
9e66c942
AM
9913/* Return a mask saying how ld should treat relocations in SEC against
9914 symbols defined in discarded sections. If this function returns
9915 COMPLAIN set, ld will issue a warning message. If this function
9916 returns PRETEND set, and the discarded section was link-once and the
9917 same size as the kept link-once section, ld will pretend that the
9918 symbol was actually defined in the kept section. Otherwise ld will
9919 zero the reloc (at least that is the intent, but some cooperation by
9920 the target dependent code is needed, particularly for REL targets). */
9921
8a696751
AM
9922unsigned int
9923_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9924{
9e66c942 9925 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9926 return PRETEND;
cdd3575c
AM
9927
9928 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9929 return 0;
cdd3575c
AM
9930
9931 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9932 return 0;
cdd3575c 9933
9e66c942 9934 return COMPLAIN | PRETEND;
cdd3575c
AM
9935}
9936
3d7f7666
L
9937/* Find a match between a section and a member of a section group. */
9938
9939static asection *
c0f00686
L
9940match_group_member (asection *sec, asection *group,
9941 struct bfd_link_info *info)
3d7f7666
L
9942{
9943 asection *first = elf_next_in_group (group);
9944 asection *s = first;
9945
9946 while (s != NULL)
9947 {
c0f00686 9948 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9949 return s;
9950
83180ade 9951 s = elf_next_in_group (s);
3d7f7666
L
9952 if (s == first)
9953 break;
9954 }
9955
9956 return NULL;
9957}
9958
01b3c8ab 9959/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9960 to replace it. Return the replacement if it is OK. Otherwise return
9961 NULL. */
01b3c8ab
L
9962
9963asection *
c0f00686 9964_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9965{
9966 asection *kept;
9967
9968 kept = sec->kept_section;
9969 if (kept != NULL)
9970 {
c2370991 9971 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9972 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9973 if (kept != NULL
9974 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9975 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9976 kept = NULL;
c2370991 9977 sec->kept_section = kept;
01b3c8ab
L
9978 }
9979 return kept;
9980}
9981
c152c796
AM
9982/* Link an input file into the linker output file. This function
9983 handles all the sections and relocations of the input file at once.
9984 This is so that we only have to read the local symbols once, and
9985 don't have to keep them in memory. */
9986
9987static bfd_boolean
8b127cbc 9988elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9989{
ece5ef60 9990 int (*relocate_section)
c152c796
AM
9991 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9992 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9993 bfd *output_bfd;
9994 Elf_Internal_Shdr *symtab_hdr;
9995 size_t locsymcount;
9996 size_t extsymoff;
9997 Elf_Internal_Sym *isymbuf;
9998 Elf_Internal_Sym *isym;
9999 Elf_Internal_Sym *isymend;
10000 long *pindex;
10001 asection **ppsection;
10002 asection *o;
10003 const struct elf_backend_data *bed;
c152c796 10004 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10005 bfd_size_type address_size;
10006 bfd_vma r_type_mask;
10007 int r_sym_shift;
ffbc01cc 10008 bfd_boolean have_file_sym = FALSE;
c152c796 10009
8b127cbc 10010 output_bfd = flinfo->output_bfd;
c152c796
AM
10011 bed = get_elf_backend_data (output_bfd);
10012 relocate_section = bed->elf_backend_relocate_section;
10013
10014 /* If this is a dynamic object, we don't want to do anything here:
10015 we don't want the local symbols, and we don't want the section
10016 contents. */
10017 if ((input_bfd->flags & DYNAMIC) != 0)
10018 return TRUE;
10019
c152c796
AM
10020 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10021 if (elf_bad_symtab (input_bfd))
10022 {
10023 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10024 extsymoff = 0;
10025 }
10026 else
10027 {
10028 locsymcount = symtab_hdr->sh_info;
10029 extsymoff = symtab_hdr->sh_info;
10030 }
10031
10032 /* Read the local symbols. */
10033 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10034 if (isymbuf == NULL && locsymcount != 0)
10035 {
10036 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10037 flinfo->internal_syms,
10038 flinfo->external_syms,
10039 flinfo->locsym_shndx);
c152c796
AM
10040 if (isymbuf == NULL)
10041 return FALSE;
10042 }
10043
10044 /* Find local symbol sections and adjust values of symbols in
10045 SEC_MERGE sections. Write out those local symbols we know are
10046 going into the output file. */
10047 isymend = isymbuf + locsymcount;
8b127cbc 10048 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10049 isym < isymend;
10050 isym++, pindex++, ppsection++)
10051 {
10052 asection *isec;
10053 const char *name;
10054 Elf_Internal_Sym osym;
6e0b88f1
AM
10055 long indx;
10056 int ret;
c152c796
AM
10057
10058 *pindex = -1;
10059
10060 if (elf_bad_symtab (input_bfd))
10061 {
10062 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10063 {
10064 *ppsection = NULL;
10065 continue;
10066 }
10067 }
10068
10069 if (isym->st_shndx == SHN_UNDEF)
10070 isec = bfd_und_section_ptr;
c152c796
AM
10071 else if (isym->st_shndx == SHN_ABS)
10072 isec = bfd_abs_section_ptr;
10073 else if (isym->st_shndx == SHN_COMMON)
10074 isec = bfd_com_section_ptr;
10075 else
10076 {
cb33740c
AM
10077 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10078 if (isec == NULL)
10079 {
10080 /* Don't attempt to output symbols with st_shnx in the
10081 reserved range other than SHN_ABS and SHN_COMMON. */
10082 *ppsection = NULL;
10083 continue;
10084 }
dbaa2011 10085 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10086 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10087 isym->st_value =
10088 _bfd_merged_section_offset (output_bfd, &isec,
10089 elf_section_data (isec)->sec_info,
10090 isym->st_value);
c152c796
AM
10091 }
10092
10093 *ppsection = isec;
10094
d983c8c5
AM
10095 /* Don't output the first, undefined, symbol. In fact, don't
10096 output any undefined local symbol. */
10097 if (isec == bfd_und_section_ptr)
c152c796
AM
10098 continue;
10099
10100 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10101 {
10102 /* We never output section symbols. Instead, we use the
10103 section symbol of the corresponding section in the output
10104 file. */
10105 continue;
10106 }
10107
10108 /* If we are stripping all symbols, we don't want to output this
10109 one. */
8b127cbc 10110 if (flinfo->info->strip == strip_all)
c152c796
AM
10111 continue;
10112
10113 /* If we are discarding all local symbols, we don't want to
10114 output this one. If we are generating a relocatable output
10115 file, then some of the local symbols may be required by
10116 relocs; we output them below as we discover that they are
10117 needed. */
8b127cbc 10118 if (flinfo->info->discard == discard_all)
c152c796
AM
10119 continue;
10120
10121 /* If this symbol is defined in a section which we are
f02571c5
AM
10122 discarding, we don't need to keep it. */
10123 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10124 && isym->st_shndx < SHN_LORESERVE
10125 && bfd_section_removed_from_list (output_bfd,
10126 isec->output_section))
e75a280b
L
10127 continue;
10128
c152c796
AM
10129 /* Get the name of the symbol. */
10130 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10131 isym->st_name);
10132 if (name == NULL)
10133 return FALSE;
10134
10135 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10136 if ((flinfo->info->strip == strip_some
10137 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10138 == NULL))
8b127cbc 10139 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10140 && (isec->flags & SEC_MERGE)
10141 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10142 || flinfo->info->discard == discard_l)
c152c796
AM
10143 && bfd_is_local_label_name (input_bfd, name)))
10144 continue;
10145
ffbc01cc
AM
10146 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10147 {
ce875075
AM
10148 if (input_bfd->lto_output)
10149 /* -flto puts a temp file name here. This means builds
10150 are not reproducible. Discard the symbol. */
10151 continue;
ffbc01cc
AM
10152 have_file_sym = TRUE;
10153 flinfo->filesym_count += 1;
10154 }
10155 if (!have_file_sym)
10156 {
10157 /* In the absence of debug info, bfd_find_nearest_line uses
10158 FILE symbols to determine the source file for local
10159 function symbols. Provide a FILE symbol here if input
10160 files lack such, so that their symbols won't be
10161 associated with a previous input file. It's not the
10162 source file, but the best we can do. */
10163 have_file_sym = TRUE;
10164 flinfo->filesym_count += 1;
10165 memset (&osym, 0, sizeof (osym));
10166 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10167 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10168 if (!elf_link_output_symstrtab (flinfo,
10169 (input_bfd->lto_output ? NULL
10170 : input_bfd->filename),
10171 &osym, bfd_abs_section_ptr,
10172 NULL))
ffbc01cc
AM
10173 return FALSE;
10174 }
10175
c152c796
AM
10176 osym = *isym;
10177
10178 /* Adjust the section index for the output file. */
10179 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10180 isec->output_section);
10181 if (osym.st_shndx == SHN_BAD)
10182 return FALSE;
10183
c152c796
AM
10184 /* ELF symbols in relocatable files are section relative, but
10185 in executable files they are virtual addresses. Note that
10186 this code assumes that all ELF sections have an associated
10187 BFD section with a reasonable value for output_offset; below
10188 we assume that they also have a reasonable value for
10189 output_section. Any special sections must be set up to meet
10190 these requirements. */
10191 osym.st_value += isec->output_offset;
0e1862bb 10192 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10193 {
10194 osym.st_value += isec->output_section->vma;
10195 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10196 {
10197 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10198 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10199 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10200 }
10201 }
10202
6e0b88f1 10203 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10204 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10205 if (ret == 0)
c152c796 10206 return FALSE;
6e0b88f1
AM
10207 else if (ret == 1)
10208 *pindex = indx;
c152c796
AM
10209 }
10210
310fd250
L
10211 if (bed->s->arch_size == 32)
10212 {
10213 r_type_mask = 0xff;
10214 r_sym_shift = 8;
10215 address_size = 4;
10216 }
10217 else
10218 {
10219 r_type_mask = 0xffffffff;
10220 r_sym_shift = 32;
10221 address_size = 8;
10222 }
10223
c152c796
AM
10224 /* Relocate the contents of each section. */
10225 sym_hashes = elf_sym_hashes (input_bfd);
10226 for (o = input_bfd->sections; o != NULL; o = o->next)
10227 {
10228 bfd_byte *contents;
10229
10230 if (! o->linker_mark)
10231 {
10232 /* This section was omitted from the link. */
10233 continue;
10234 }
10235
0e1862bb 10236 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10237 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10238 {
10239 /* Deal with the group signature symbol. */
10240 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10241 unsigned long symndx = sec_data->this_hdr.sh_info;
10242 asection *osec = o->output_section;
10243
10244 if (symndx >= locsymcount
10245 || (elf_bad_symtab (input_bfd)
8b127cbc 10246 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10247 {
10248 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10249 while (h->root.type == bfd_link_hash_indirect
10250 || h->root.type == bfd_link_hash_warning)
10251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10252 /* Arrange for symbol to be output. */
10253 h->indx = -2;
10254 elf_section_data (osec)->this_hdr.sh_info = -2;
10255 }
10256 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10257 {
10258 /* We'll use the output section target_index. */
8b127cbc 10259 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10260 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10261 }
10262 else
10263 {
8b127cbc 10264 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10265 {
10266 /* Otherwise output the local symbol now. */
10267 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10268 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10269 const char *name;
6e0b88f1
AM
10270 long indx;
10271 int ret;
bcacc0f5
AM
10272
10273 name = bfd_elf_string_from_elf_section (input_bfd,
10274 symtab_hdr->sh_link,
10275 sym.st_name);
10276 if (name == NULL)
10277 return FALSE;
10278
10279 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10280 sec);
10281 if (sym.st_shndx == SHN_BAD)
10282 return FALSE;
10283
10284 sym.st_value += o->output_offset;
10285
6e0b88f1 10286 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10287 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10288 NULL);
6e0b88f1 10289 if (ret == 0)
bcacc0f5 10290 return FALSE;
6e0b88f1 10291 else if (ret == 1)
8b127cbc 10292 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10293 else
10294 abort ();
bcacc0f5
AM
10295 }
10296 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10297 = flinfo->indices[symndx];
bcacc0f5
AM
10298 }
10299 }
10300
c152c796 10301 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10302 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10303 continue;
10304
10305 if ((o->flags & SEC_LINKER_CREATED) != 0)
10306 {
10307 /* Section was created by _bfd_elf_link_create_dynamic_sections
10308 or somesuch. */
10309 continue;
10310 }
10311
10312 /* Get the contents of the section. They have been cached by a
10313 relaxation routine. Note that o is a section in an input
10314 file, so the contents field will not have been set by any of
10315 the routines which work on output files. */
10316 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10317 {
10318 contents = elf_section_data (o)->this_hdr.contents;
10319 if (bed->caches_rawsize
10320 && o->rawsize != 0
10321 && o->rawsize < o->size)
10322 {
10323 memcpy (flinfo->contents, contents, o->rawsize);
10324 contents = flinfo->contents;
10325 }
10326 }
c152c796
AM
10327 else
10328 {
8b127cbc 10329 contents = flinfo->contents;
4a114e3e 10330 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10331 return FALSE;
10332 }
10333
10334 if ((o->flags & SEC_RELOC) != 0)
10335 {
10336 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10337 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10338 int action_discarded;
ece5ef60 10339 int ret;
c152c796
AM
10340
10341 /* Get the swapped relocs. */
10342 internal_relocs
8b127cbc
AM
10343 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10344 flinfo->internal_relocs, FALSE);
c152c796
AM
10345 if (internal_relocs == NULL
10346 && o->reloc_count > 0)
10347 return FALSE;
10348
310fd250
L
10349 /* We need to reverse-copy input .ctors/.dtors sections if
10350 they are placed in .init_array/.finit_array for output. */
10351 if (o->size > address_size
10352 && ((strncmp (o->name, ".ctors", 6) == 0
10353 && strcmp (o->output_section->name,
10354 ".init_array") == 0)
10355 || (strncmp (o->name, ".dtors", 6) == 0
10356 && strcmp (o->output_section->name,
10357 ".fini_array") == 0))
10358 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10359 {
310fd250
L
10360 if (o->size != o->reloc_count * address_size)
10361 {
4eca0228 10362 _bfd_error_handler
695344c0 10363 /* xgettext:c-format */
310fd250
L
10364 (_("error: %B: size of section %A is not "
10365 "multiple of address size"),
10366 input_bfd, o);
10367 bfd_set_error (bfd_error_on_input);
10368 return FALSE;
10369 }
10370 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10371 }
10372
0f02bbd9 10373 action_discarded = -1;
c152c796 10374 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10375 action_discarded = (*bed->action_discarded) (o);
10376
10377 /* Run through the relocs evaluating complex reloc symbols and
10378 looking for relocs against symbols from discarded sections
10379 or section symbols from removed link-once sections.
10380 Complain about relocs against discarded sections. Zero
10381 relocs against removed link-once sections. */
10382
10383 rel = internal_relocs;
10384 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10385 for ( ; rel < relend; rel++)
c152c796 10386 {
0f02bbd9
AM
10387 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10388 unsigned int s_type;
10389 asection **ps, *sec;
10390 struct elf_link_hash_entry *h = NULL;
10391 const char *sym_name;
c152c796 10392
0f02bbd9
AM
10393 if (r_symndx == STN_UNDEF)
10394 continue;
c152c796 10395
0f02bbd9
AM
10396 if (r_symndx >= locsymcount
10397 || (elf_bad_symtab (input_bfd)
8b127cbc 10398 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10399 {
10400 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10401
0f02bbd9
AM
10402 /* Badly formatted input files can contain relocs that
10403 reference non-existant symbols. Check here so that
10404 we do not seg fault. */
10405 if (h == NULL)
c152c796 10406 {
0f02bbd9 10407 char buffer [32];
dce669a1 10408
0f02bbd9 10409 sprintf_vma (buffer, rel->r_info);
4eca0228 10410 _bfd_error_handler
695344c0 10411 /* xgettext:c-format */
0f02bbd9
AM
10412 (_("error: %B contains a reloc (0x%s) for section %A "
10413 "that references a non-existent global symbol"),
c08bb8dd 10414 input_bfd, buffer, o);
0f02bbd9
AM
10415 bfd_set_error (bfd_error_bad_value);
10416 return FALSE;
10417 }
3b36f7e6 10418
0f02bbd9
AM
10419 while (h->root.type == bfd_link_hash_indirect
10420 || h->root.type == bfd_link_hash_warning)
10421 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10422
0f02bbd9 10423 s_type = h->type;
cdd3575c 10424
9e2dec47 10425 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10426 mark the symbol as undefined. Note that the
10427 linker may attach linker created dynamic sections
10428 to the plugin bfd. Symbols defined in linker
10429 created sections are not plugin symbols. */
9e2dec47
L
10430 if (h->root.non_ir_ref
10431 && (h->root.type == bfd_link_hash_defined
10432 || h->root.type == bfd_link_hash_defweak)
10433 && (h->root.u.def.section->flags
10434 & SEC_LINKER_CREATED) == 0
10435 && h->root.u.def.section->owner != NULL
10436 && (h->root.u.def.section->owner->flags
10437 & BFD_PLUGIN) != 0)
10438 {
10439 h->root.type = bfd_link_hash_undefined;
10440 h->root.u.undef.abfd = h->root.u.def.section->owner;
10441 }
10442
0f02bbd9
AM
10443 ps = NULL;
10444 if (h->root.type == bfd_link_hash_defined
10445 || h->root.type == bfd_link_hash_defweak)
10446 ps = &h->root.u.def.section;
10447
10448 sym_name = h->root.root.string;
10449 }
10450 else
10451 {
10452 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10453
10454 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10455 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10456 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10457 sym, *ps);
10458 }
c152c796 10459
c301e700 10460 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10461 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10462 {
10463 bfd_vma val;
10464 bfd_vma dot = (rel->r_offset
10465 + o->output_offset + o->output_section->vma);
10466#ifdef DEBUG
10467 printf ("Encountered a complex symbol!");
10468 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10469 input_bfd->filename, o->name,
10470 (long) (rel - internal_relocs));
0f02bbd9
AM
10471 printf (" symbol: idx %8.8lx, name %s\n",
10472 r_symndx, sym_name);
10473 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10474 (unsigned long) rel->r_info,
10475 (unsigned long) rel->r_offset);
10476#endif
8b127cbc 10477 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10478 isymbuf, locsymcount, s_type == STT_SRELC))
10479 return FALSE;
10480
10481 /* Symbol evaluated OK. Update to absolute value. */
10482 set_symbol_value (input_bfd, isymbuf, locsymcount,
10483 r_symndx, val);
10484 continue;
10485 }
10486
10487 if (action_discarded != -1 && ps != NULL)
10488 {
cdd3575c
AM
10489 /* Complain if the definition comes from a
10490 discarded section. */
dbaa2011 10491 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10492 {
cf35638d 10493 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10494 if (action_discarded & COMPLAIN)
8b127cbc 10495 (*flinfo->info->callbacks->einfo)
695344c0 10496 /* xgettext:c-format */
e1fffbe6 10497 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10498 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10499 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10500
87e5235d 10501 /* Try to do the best we can to support buggy old
e0ae6d6f 10502 versions of gcc. Pretend that the symbol is
87e5235d
AM
10503 really defined in the kept linkonce section.
10504 FIXME: This is quite broken. Modifying the
10505 symbol here means we will be changing all later
e0ae6d6f 10506 uses of the symbol, not just in this section. */
0f02bbd9 10507 if (action_discarded & PRETEND)
87e5235d 10508 {
01b3c8ab
L
10509 asection *kept;
10510
c0f00686 10511 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10512 flinfo->info);
01b3c8ab 10513 if (kept != NULL)
87e5235d
AM
10514 {
10515 *ps = kept;
10516 continue;
10517 }
10518 }
c152c796
AM
10519 }
10520 }
10521 }
10522
10523 /* Relocate the section by invoking a back end routine.
10524
10525 The back end routine is responsible for adjusting the
10526 section contents as necessary, and (if using Rela relocs
10527 and generating a relocatable output file) adjusting the
10528 reloc addend as necessary.
10529
10530 The back end routine does not have to worry about setting
10531 the reloc address or the reloc symbol index.
10532
10533 The back end routine is given a pointer to the swapped in
10534 internal symbols, and can access the hash table entries
10535 for the external symbols via elf_sym_hashes (input_bfd).
10536
10537 When generating relocatable output, the back end routine
10538 must handle STB_LOCAL/STT_SECTION symbols specially. The
10539 output symbol is going to be a section symbol
10540 corresponding to the output section, which will require
10541 the addend to be adjusted. */
10542
8b127cbc 10543 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10544 input_bfd, o, contents,
10545 internal_relocs,
10546 isymbuf,
8b127cbc 10547 flinfo->sections);
ece5ef60 10548 if (!ret)
c152c796
AM
10549 return FALSE;
10550
ece5ef60 10551 if (ret == 2
0e1862bb 10552 || bfd_link_relocatable (flinfo->info)
8b127cbc 10553 || flinfo->info->emitrelocations)
c152c796
AM
10554 {
10555 Elf_Internal_Rela *irela;
d4730f92 10556 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10557 bfd_vma last_offset;
10558 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10559 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10560 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10561 unsigned int next_erel;
c152c796 10562 bfd_boolean rela_normal;
d4730f92 10563 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10564
d4730f92
BS
10565 esdi = elf_section_data (o);
10566 esdo = elf_section_data (o->output_section);
10567 rela_normal = FALSE;
c152c796
AM
10568
10569 /* Adjust the reloc addresses and symbol indices. */
10570
10571 irela = internal_relocs;
10572 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10573 rel_hash = esdo->rel.hashes + esdo->rel.count;
10574 /* We start processing the REL relocs, if any. When we reach
10575 IRELAMID in the loop, we switch to the RELA relocs. */
10576 irelamid = irela;
10577 if (esdi->rel.hdr != NULL)
10578 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10579 * bed->s->int_rels_per_ext_rel);
eac338cf 10580 rel_hash_list = rel_hash;
d4730f92 10581 rela_hash_list = NULL;
c152c796 10582 last_offset = o->output_offset;
0e1862bb 10583 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10584 last_offset += o->output_section->vma;
10585 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10586 {
10587 unsigned long r_symndx;
10588 asection *sec;
10589 Elf_Internal_Sym sym;
10590
10591 if (next_erel == bed->s->int_rels_per_ext_rel)
10592 {
10593 rel_hash++;
10594 next_erel = 0;
10595 }
10596
d4730f92
BS
10597 if (irela == irelamid)
10598 {
10599 rel_hash = esdo->rela.hashes + esdo->rela.count;
10600 rela_hash_list = rel_hash;
10601 rela_normal = bed->rela_normal;
10602 }
10603
c152c796 10604 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10605 flinfo->info, o,
c152c796
AM
10606 irela->r_offset);
10607 if (irela->r_offset >= (bfd_vma) -2)
10608 {
10609 /* This is a reloc for a deleted entry or somesuch.
10610 Turn it into an R_*_NONE reloc, at the same
10611 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10612 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10613 being ordered. */
10614 irela->r_offset = last_offset;
10615 irela->r_info = 0;
10616 irela->r_addend = 0;
10617 continue;
10618 }
10619
10620 irela->r_offset += o->output_offset;
10621
10622 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10623 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10624 irela->r_offset += o->output_section->vma;
10625
10626 last_offset = irela->r_offset;
10627
10628 r_symndx = irela->r_info >> r_sym_shift;
10629 if (r_symndx == STN_UNDEF)
10630 continue;
10631
10632 if (r_symndx >= locsymcount
10633 || (elf_bad_symtab (input_bfd)
8b127cbc 10634 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10635 {
10636 struct elf_link_hash_entry *rh;
10637 unsigned long indx;
10638
10639 /* This is a reloc against a global symbol. We
10640 have not yet output all the local symbols, so
10641 we do not know the symbol index of any global
10642 symbol. We set the rel_hash entry for this
10643 reloc to point to the global hash table entry
10644 for this symbol. The symbol index is then
ee75fd95 10645 set at the end of bfd_elf_final_link. */
c152c796
AM
10646 indx = r_symndx - extsymoff;
10647 rh = elf_sym_hashes (input_bfd)[indx];
10648 while (rh->root.type == bfd_link_hash_indirect
10649 || rh->root.type == bfd_link_hash_warning)
10650 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10651
10652 /* Setting the index to -2 tells
10653 elf_link_output_extsym that this symbol is
10654 used by a reloc. */
10655 BFD_ASSERT (rh->indx < 0);
10656 rh->indx = -2;
10657
10658 *rel_hash = rh;
10659
10660 continue;
10661 }
10662
10663 /* This is a reloc against a local symbol. */
10664
10665 *rel_hash = NULL;
10666 sym = isymbuf[r_symndx];
8b127cbc 10667 sec = flinfo->sections[r_symndx];
c152c796
AM
10668 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10669 {
10670 /* I suppose the backend ought to fill in the
10671 section of any STT_SECTION symbol against a
6a8d1586 10672 processor specific section. */
cf35638d 10673 r_symndx = STN_UNDEF;
6a8d1586
AM
10674 if (bfd_is_abs_section (sec))
10675 ;
c152c796
AM
10676 else if (sec == NULL || sec->owner == NULL)
10677 {
10678 bfd_set_error (bfd_error_bad_value);
10679 return FALSE;
10680 }
10681 else
10682 {
6a8d1586
AM
10683 asection *osec = sec->output_section;
10684
10685 /* If we have discarded a section, the output
10686 section will be the absolute section. In
ab96bf03
AM
10687 case of discarded SEC_MERGE sections, use
10688 the kept section. relocate_section should
10689 have already handled discarded linkonce
10690 sections. */
6a8d1586
AM
10691 if (bfd_is_abs_section (osec)
10692 && sec->kept_section != NULL
10693 && sec->kept_section->output_section != NULL)
10694 {
10695 osec = sec->kept_section->output_section;
10696 irela->r_addend -= osec->vma;
10697 }
10698
10699 if (!bfd_is_abs_section (osec))
10700 {
10701 r_symndx = osec->target_index;
cf35638d 10702 if (r_symndx == STN_UNDEF)
74541ad4 10703 {
051d833a
AM
10704 irela->r_addend += osec->vma;
10705 osec = _bfd_nearby_section (output_bfd, osec,
10706 osec->vma);
10707 irela->r_addend -= osec->vma;
10708 r_symndx = osec->target_index;
74541ad4 10709 }
6a8d1586 10710 }
c152c796
AM
10711 }
10712
10713 /* Adjust the addend according to where the
10714 section winds up in the output section. */
10715 if (rela_normal)
10716 irela->r_addend += sec->output_offset;
10717 }
10718 else
10719 {
8b127cbc 10720 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10721 {
10722 unsigned long shlink;
10723 const char *name;
10724 asection *osec;
6e0b88f1 10725 long indx;
c152c796 10726
8b127cbc 10727 if (flinfo->info->strip == strip_all)
c152c796
AM
10728 {
10729 /* You can't do ld -r -s. */
10730 bfd_set_error (bfd_error_invalid_operation);
10731 return FALSE;
10732 }
10733
10734 /* This symbol was skipped earlier, but
10735 since it is needed by a reloc, we
10736 must output it now. */
10737 shlink = symtab_hdr->sh_link;
10738 name = (bfd_elf_string_from_elf_section
10739 (input_bfd, shlink, sym.st_name));
10740 if (name == NULL)
10741 return FALSE;
10742
10743 osec = sec->output_section;
10744 sym.st_shndx =
10745 _bfd_elf_section_from_bfd_section (output_bfd,
10746 osec);
10747 if (sym.st_shndx == SHN_BAD)
10748 return FALSE;
10749
10750 sym.st_value += sec->output_offset;
0e1862bb 10751 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10752 {
10753 sym.st_value += osec->vma;
10754 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10755 {
10756 /* STT_TLS symbols are relative to PT_TLS
10757 segment base. */
8b127cbc 10758 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10759 ->tls_sec != NULL);
8b127cbc 10760 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10761 ->tls_sec->vma);
10762 }
10763 }
10764
6e0b88f1 10765 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10766 ret = elf_link_output_symstrtab (flinfo, name,
10767 &sym, sec,
10768 NULL);
6e0b88f1 10769 if (ret == 0)
c152c796 10770 return FALSE;
6e0b88f1 10771 else if (ret == 1)
8b127cbc 10772 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10773 else
10774 abort ();
c152c796
AM
10775 }
10776
8b127cbc 10777 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10778 }
10779
10780 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10781 | (irela->r_info & r_type_mask));
10782 }
10783
10784 /* Swap out the relocs. */
d4730f92
BS
10785 input_rel_hdr = esdi->rel.hdr;
10786 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10787 {
d4730f92
BS
10788 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10789 input_rel_hdr,
10790 internal_relocs,
10791 rel_hash_list))
10792 return FALSE;
c152c796
AM
10793 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10794 * bed->s->int_rels_per_ext_rel);
eac338cf 10795 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10796 }
10797
10798 input_rela_hdr = esdi->rela.hdr;
10799 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10800 {
eac338cf 10801 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10802 input_rela_hdr,
eac338cf 10803 internal_relocs,
d4730f92 10804 rela_hash_list))
c152c796
AM
10805 return FALSE;
10806 }
10807 }
10808 }
10809
10810 /* Write out the modified section contents. */
10811 if (bed->elf_backend_write_section
8b127cbc 10812 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10813 contents))
c152c796
AM
10814 {
10815 /* Section written out. */
10816 }
10817 else switch (o->sec_info_type)
10818 {
dbaa2011 10819 case SEC_INFO_TYPE_STABS:
c152c796
AM
10820 if (! (_bfd_write_section_stabs
10821 (output_bfd,
8b127cbc 10822 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10823 o, &elf_section_data (o)->sec_info, contents)))
10824 return FALSE;
10825 break;
dbaa2011 10826 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10827 if (! _bfd_write_merged_section (output_bfd, o,
10828 elf_section_data (o)->sec_info))
10829 return FALSE;
10830 break;
dbaa2011 10831 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10832 {
8b127cbc 10833 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10834 o, contents))
10835 return FALSE;
10836 }
10837 break;
2f0c68f2
CM
10838 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10839 {
10840 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10841 flinfo->info,
10842 o, contents))
10843 return FALSE;
10844 }
10845 break;
c152c796
AM
10846 default:
10847 {
310fd250
L
10848 if (! (o->flags & SEC_EXCLUDE))
10849 {
10850 file_ptr offset = (file_ptr) o->output_offset;
10851 bfd_size_type todo = o->size;
37b01f6a
DG
10852
10853 offset *= bfd_octets_per_byte (output_bfd);
10854
310fd250
L
10855 if ((o->flags & SEC_ELF_REVERSE_COPY))
10856 {
10857 /* Reverse-copy input section to output. */
10858 do
10859 {
10860 todo -= address_size;
10861 if (! bfd_set_section_contents (output_bfd,
10862 o->output_section,
10863 contents + todo,
10864 offset,
10865 address_size))
10866 return FALSE;
10867 if (todo == 0)
10868 break;
10869 offset += address_size;
10870 }
10871 while (1);
10872 }
10873 else if (! bfd_set_section_contents (output_bfd,
10874 o->output_section,
10875 contents,
10876 offset, todo))
10877 return FALSE;
10878 }
c152c796
AM
10879 }
10880 break;
10881 }
10882 }
10883
10884 return TRUE;
10885}
10886
10887/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10888 requested by the linker, and does not come from any input file. This
c152c796
AM
10889 is used to build constructor and destructor tables when linking
10890 with -Ur. */
10891
10892static bfd_boolean
10893elf_reloc_link_order (bfd *output_bfd,
10894 struct bfd_link_info *info,
10895 asection *output_section,
10896 struct bfd_link_order *link_order)
10897{
10898 reloc_howto_type *howto;
10899 long indx;
10900 bfd_vma offset;
10901 bfd_vma addend;
d4730f92 10902 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10903 struct elf_link_hash_entry **rel_hash_ptr;
10904 Elf_Internal_Shdr *rel_hdr;
10905 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10906 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10907 bfd_byte *erel;
10908 unsigned int i;
d4730f92 10909 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10910
10911 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10912 if (howto == NULL)
10913 {
10914 bfd_set_error (bfd_error_bad_value);
10915 return FALSE;
10916 }
10917
10918 addend = link_order->u.reloc.p->addend;
10919
d4730f92
BS
10920 if (esdo->rel.hdr)
10921 reldata = &esdo->rel;
10922 else if (esdo->rela.hdr)
10923 reldata = &esdo->rela;
10924 else
10925 {
10926 reldata = NULL;
10927 BFD_ASSERT (0);
10928 }
10929
c152c796 10930 /* Figure out the symbol index. */
d4730f92 10931 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10932 if (link_order->type == bfd_section_reloc_link_order)
10933 {
10934 indx = link_order->u.reloc.p->u.section->target_index;
10935 BFD_ASSERT (indx != 0);
10936 *rel_hash_ptr = NULL;
10937 }
10938 else
10939 {
10940 struct elf_link_hash_entry *h;
10941
10942 /* Treat a reloc against a defined symbol as though it were
10943 actually against the section. */
10944 h = ((struct elf_link_hash_entry *)
10945 bfd_wrapped_link_hash_lookup (output_bfd, info,
10946 link_order->u.reloc.p->u.name,
10947 FALSE, FALSE, TRUE));
10948 if (h != NULL
10949 && (h->root.type == bfd_link_hash_defined
10950 || h->root.type == bfd_link_hash_defweak))
10951 {
10952 asection *section;
10953
10954 section = h->root.u.def.section;
10955 indx = section->output_section->target_index;
10956 *rel_hash_ptr = NULL;
10957 /* It seems that we ought to add the symbol value to the
10958 addend here, but in practice it has already been added
10959 because it was passed to constructor_callback. */
10960 addend += section->output_section->vma + section->output_offset;
10961 }
10962 else if (h != NULL)
10963 {
10964 /* Setting the index to -2 tells elf_link_output_extsym that
10965 this symbol is used by a reloc. */
10966 h->indx = -2;
10967 *rel_hash_ptr = h;
10968 indx = 0;
10969 }
10970 else
10971 {
1a72702b
AM
10972 (*info->callbacks->unattached_reloc)
10973 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10974 indx = 0;
10975 }
10976 }
10977
10978 /* If this is an inplace reloc, we must write the addend into the
10979 object file. */
10980 if (howto->partial_inplace && addend != 0)
10981 {
10982 bfd_size_type size;
10983 bfd_reloc_status_type rstat;
10984 bfd_byte *buf;
10985 bfd_boolean ok;
10986 const char *sym_name;
10987
a50b1753
NC
10988 size = (bfd_size_type) bfd_get_reloc_size (howto);
10989 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10990 if (buf == NULL && size != 0)
c152c796
AM
10991 return FALSE;
10992 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10993 switch (rstat)
10994 {
10995 case bfd_reloc_ok:
10996 break;
10997
10998 default:
10999 case bfd_reloc_outofrange:
11000 abort ();
11001
11002 case bfd_reloc_overflow:
11003 if (link_order->type == bfd_section_reloc_link_order)
11004 sym_name = bfd_section_name (output_bfd,
11005 link_order->u.reloc.p->u.section);
11006 else
11007 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11008 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11009 howto->name, addend, NULL, NULL,
11010 (bfd_vma) 0);
c152c796
AM
11011 break;
11012 }
37b01f6a 11013
c152c796 11014 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11015 link_order->offset
11016 * bfd_octets_per_byte (output_bfd),
11017 size);
c152c796
AM
11018 free (buf);
11019 if (! ok)
11020 return FALSE;
11021 }
11022
11023 /* The address of a reloc is relative to the section in a
11024 relocatable file, and is a virtual address in an executable
11025 file. */
11026 offset = link_order->offset;
0e1862bb 11027 if (! bfd_link_relocatable (info))
c152c796
AM
11028 offset += output_section->vma;
11029
11030 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11031 {
11032 irel[i].r_offset = offset;
11033 irel[i].r_info = 0;
11034 irel[i].r_addend = 0;
11035 }
11036 if (bed->s->arch_size == 32)
11037 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11038 else
11039 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11040
d4730f92 11041 rel_hdr = reldata->hdr;
c152c796
AM
11042 erel = rel_hdr->contents;
11043 if (rel_hdr->sh_type == SHT_REL)
11044 {
d4730f92 11045 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11046 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11047 }
11048 else
11049 {
11050 irel[0].r_addend = addend;
d4730f92 11051 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11052 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11053 }
11054
d4730f92 11055 ++reldata->count;
c152c796
AM
11056
11057 return TRUE;
11058}
11059
0b52efa6
PB
11060
11061/* Get the output vma of the section pointed to by the sh_link field. */
11062
11063static bfd_vma
11064elf_get_linked_section_vma (struct bfd_link_order *p)
11065{
11066 Elf_Internal_Shdr **elf_shdrp;
11067 asection *s;
11068 int elfsec;
11069
11070 s = p->u.indirect.section;
11071 elf_shdrp = elf_elfsections (s->owner);
11072 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11073 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11074 /* PR 290:
11075 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11076 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11077 sh_info fields. Hence we could get the situation
11078 where elfsec is 0. */
11079 if (elfsec == 0)
11080 {
11081 const struct elf_backend_data *bed
11082 = get_elf_backend_data (s->owner);
11083 if (bed->link_order_error_handler)
d003868e 11084 bed->link_order_error_handler
695344c0 11085 /* xgettext:c-format */
d003868e 11086 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11087 return 0;
11088 }
11089 else
11090 {
11091 s = elf_shdrp[elfsec]->bfd_section;
11092 return s->output_section->vma + s->output_offset;
11093 }
0b52efa6
PB
11094}
11095
11096
11097/* Compare two sections based on the locations of the sections they are
11098 linked to. Used by elf_fixup_link_order. */
11099
11100static int
11101compare_link_order (const void * a, const void * b)
11102{
11103 bfd_vma apos;
11104 bfd_vma bpos;
11105
11106 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11107 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11108 if (apos < bpos)
11109 return -1;
11110 return apos > bpos;
11111}
11112
11113
11114/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11115 order as their linked sections. Returns false if this could not be done
11116 because an output section includes both ordered and unordered
11117 sections. Ideally we'd do this in the linker proper. */
11118
11119static bfd_boolean
11120elf_fixup_link_order (bfd *abfd, asection *o)
11121{
11122 int seen_linkorder;
11123 int seen_other;
11124 int n;
11125 struct bfd_link_order *p;
11126 bfd *sub;
11127 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11128 unsigned elfsec;
0b52efa6 11129 struct bfd_link_order **sections;
d33cdfe3 11130 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11131 bfd_vma offset;
3b36f7e6 11132
d33cdfe3
L
11133 other_sec = NULL;
11134 linkorder_sec = NULL;
0b52efa6
PB
11135 seen_other = 0;
11136 seen_linkorder = 0;
8423293d 11137 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11138 {
d33cdfe3 11139 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11140 {
11141 s = p->u.indirect.section;
d33cdfe3
L
11142 sub = s->owner;
11143 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11144 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11145 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11146 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11147 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11148 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11149 {
11150 seen_linkorder++;
11151 linkorder_sec = s;
11152 }
0b52efa6 11153 else
d33cdfe3
L
11154 {
11155 seen_other++;
11156 other_sec = s;
11157 }
0b52efa6
PB
11158 }
11159 else
11160 seen_other++;
d33cdfe3
L
11161
11162 if (seen_other && seen_linkorder)
11163 {
11164 if (other_sec && linkorder_sec)
4eca0228 11165 _bfd_error_handler
695344c0 11166 /* xgettext:c-format */
4eca0228
AM
11167 (_("%A has both ordered [`%A' in %B] "
11168 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11169 o, linkorder_sec, linkorder_sec->owner,
11170 other_sec, other_sec->owner);
d33cdfe3 11171 else
4eca0228
AM
11172 _bfd_error_handler
11173 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11174 bfd_set_error (bfd_error_bad_value);
11175 return FALSE;
11176 }
0b52efa6
PB
11177 }
11178
11179 if (!seen_linkorder)
11180 return TRUE;
11181
0b52efa6 11182 sections = (struct bfd_link_order **)
14b1c01e
AM
11183 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11184 if (sections == NULL)
11185 return FALSE;
0b52efa6 11186 seen_linkorder = 0;
3b36f7e6 11187
8423293d 11188 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11189 {
11190 sections[seen_linkorder++] = p;
11191 }
11192 /* Sort the input sections in the order of their linked section. */
11193 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11194 compare_link_order);
11195
11196 /* Change the offsets of the sections. */
11197 offset = 0;
11198 for (n = 0; n < seen_linkorder; n++)
11199 {
11200 s = sections[n]->u.indirect.section;
461686a3 11201 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11202 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11203 sections[n]->offset = offset;
11204 offset += sections[n]->size;
11205 }
11206
4dd07732 11207 free (sections);
0b52efa6
PB
11208 return TRUE;
11209}
11210
76359541
TP
11211/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11212 Returns TRUE upon success, FALSE otherwise. */
11213
11214static bfd_boolean
11215elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11216{
11217 bfd_boolean ret = FALSE;
11218 bfd *implib_bfd;
11219 const struct elf_backend_data *bed;
11220 flagword flags;
11221 enum bfd_architecture arch;
11222 unsigned int mach;
11223 asymbol **sympp = NULL;
11224 long symsize;
11225 long symcount;
11226 long src_count;
11227 elf_symbol_type *osymbuf;
11228
11229 implib_bfd = info->out_implib_bfd;
11230 bed = get_elf_backend_data (abfd);
11231
11232 if (!bfd_set_format (implib_bfd, bfd_object))
11233 return FALSE;
11234
11235 flags = bfd_get_file_flags (abfd);
11236 flags &= ~HAS_RELOC;
11237 if (!bfd_set_start_address (implib_bfd, 0)
11238 || !bfd_set_file_flags (implib_bfd, flags))
11239 return FALSE;
11240
11241 /* Copy architecture of output file to import library file. */
11242 arch = bfd_get_arch (abfd);
11243 mach = bfd_get_mach (abfd);
11244 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11245 && (abfd->target_defaulted
11246 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11247 return FALSE;
11248
11249 /* Get symbol table size. */
11250 symsize = bfd_get_symtab_upper_bound (abfd);
11251 if (symsize < 0)
11252 return FALSE;
11253
11254 /* Read in the symbol table. */
11255 sympp = (asymbol **) xmalloc (symsize);
11256 symcount = bfd_canonicalize_symtab (abfd, sympp);
11257 if (symcount < 0)
11258 goto free_sym_buf;
11259
11260 /* Allow the BFD backend to copy any private header data it
11261 understands from the output BFD to the import library BFD. */
11262 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11263 goto free_sym_buf;
11264
11265 /* Filter symbols to appear in the import library. */
11266 if (bed->elf_backend_filter_implib_symbols)
11267 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11268 symcount);
11269 else
11270 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11271 if (symcount == 0)
11272 {
5df1bc57 11273 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11274 _bfd_error_handler (_("%B: no symbol found for import library"),
11275 implib_bfd);
76359541
TP
11276 goto free_sym_buf;
11277 }
11278
11279
11280 /* Make symbols absolute. */
11281 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11282 sizeof (*osymbuf));
11283 for (src_count = 0; src_count < symcount; src_count++)
11284 {
11285 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11286 sizeof (*osymbuf));
11287 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11288 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11289 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11290 osymbuf[src_count].internal_elf_sym.st_value =
11291 osymbuf[src_count].symbol.value;
11292 sympp[src_count] = &osymbuf[src_count].symbol;
11293 }
11294
11295 bfd_set_symtab (implib_bfd, sympp, symcount);
11296
11297 /* Allow the BFD backend to copy any private data it understands
11298 from the output BFD to the import library BFD. This is done last
11299 to permit the routine to look at the filtered symbol table. */
11300 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11301 goto free_sym_buf;
11302
11303 if (!bfd_close (implib_bfd))
11304 goto free_sym_buf;
11305
11306 ret = TRUE;
11307
11308free_sym_buf:
11309 free (sympp);
11310 return ret;
11311}
11312
9f7c3e5e
AM
11313static void
11314elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11315{
11316 asection *o;
11317
11318 if (flinfo->symstrtab != NULL)
ef10c3ac 11319 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11320 if (flinfo->contents != NULL)
11321 free (flinfo->contents);
11322 if (flinfo->external_relocs != NULL)
11323 free (flinfo->external_relocs);
11324 if (flinfo->internal_relocs != NULL)
11325 free (flinfo->internal_relocs);
11326 if (flinfo->external_syms != NULL)
11327 free (flinfo->external_syms);
11328 if (flinfo->locsym_shndx != NULL)
11329 free (flinfo->locsym_shndx);
11330 if (flinfo->internal_syms != NULL)
11331 free (flinfo->internal_syms);
11332 if (flinfo->indices != NULL)
11333 free (flinfo->indices);
11334 if (flinfo->sections != NULL)
11335 free (flinfo->sections);
9f7c3e5e
AM
11336 if (flinfo->symshndxbuf != NULL)
11337 free (flinfo->symshndxbuf);
11338 for (o = obfd->sections; o != NULL; o = o->next)
11339 {
11340 struct bfd_elf_section_data *esdo = elf_section_data (o);
11341 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11342 free (esdo->rel.hashes);
11343 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11344 free (esdo->rela.hashes);
11345 }
11346}
0b52efa6 11347
c152c796
AM
11348/* Do the final step of an ELF link. */
11349
11350bfd_boolean
11351bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11352{
11353 bfd_boolean dynamic;
11354 bfd_boolean emit_relocs;
11355 bfd *dynobj;
8b127cbc 11356 struct elf_final_link_info flinfo;
91d6fa6a
NC
11357 asection *o;
11358 struct bfd_link_order *p;
11359 bfd *sub;
c152c796
AM
11360 bfd_size_type max_contents_size;
11361 bfd_size_type max_external_reloc_size;
11362 bfd_size_type max_internal_reloc_count;
11363 bfd_size_type max_sym_count;
11364 bfd_size_type max_sym_shndx_count;
c152c796
AM
11365 Elf_Internal_Sym elfsym;
11366 unsigned int i;
11367 Elf_Internal_Shdr *symtab_hdr;
11368 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11369 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11370 struct elf_outext_info eoinfo;
11371 bfd_boolean merged;
11372 size_t relativecount = 0;
11373 asection *reldyn = 0;
11374 bfd_size_type amt;
104d59d1
JM
11375 asection *attr_section = NULL;
11376 bfd_vma attr_size = 0;
11377 const char *std_attrs_section;
64f52338 11378 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11379
64f52338 11380 if (!is_elf_hash_table (htab))
c152c796
AM
11381 return FALSE;
11382
0e1862bb 11383 if (bfd_link_pic (info))
c152c796
AM
11384 abfd->flags |= DYNAMIC;
11385
64f52338
AM
11386 dynamic = htab->dynamic_sections_created;
11387 dynobj = htab->dynobj;
c152c796 11388
0e1862bb 11389 emit_relocs = (bfd_link_relocatable (info)
a4676736 11390 || info->emitrelocations);
c152c796 11391
8b127cbc
AM
11392 flinfo.info = info;
11393 flinfo.output_bfd = abfd;
ef10c3ac 11394 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11395 if (flinfo.symstrtab == NULL)
c152c796
AM
11396 return FALSE;
11397
11398 if (! dynamic)
11399 {
8b127cbc
AM
11400 flinfo.hash_sec = NULL;
11401 flinfo.symver_sec = NULL;
c152c796
AM
11402 }
11403 else
11404 {
3d4d4302 11405 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11406 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11407 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11408 /* Note that it is OK if symver_sec is NULL. */
11409 }
11410
8b127cbc
AM
11411 flinfo.contents = NULL;
11412 flinfo.external_relocs = NULL;
11413 flinfo.internal_relocs = NULL;
11414 flinfo.external_syms = NULL;
11415 flinfo.locsym_shndx = NULL;
11416 flinfo.internal_syms = NULL;
11417 flinfo.indices = NULL;
11418 flinfo.sections = NULL;
8b127cbc 11419 flinfo.symshndxbuf = NULL;
ffbc01cc 11420 flinfo.filesym_count = 0;
c152c796 11421
104d59d1
JM
11422 /* The object attributes have been merged. Remove the input
11423 sections from the link, and set the contents of the output
11424 secton. */
11425 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11426 for (o = abfd->sections; o != NULL; o = o->next)
11427 {
11428 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11429 || strcmp (o->name, ".gnu.attributes") == 0)
11430 {
11431 for (p = o->map_head.link_order; p != NULL; p = p->next)
11432 {
11433 asection *input_section;
11434
11435 if (p->type != bfd_indirect_link_order)
11436 continue;
11437 input_section = p->u.indirect.section;
11438 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11439 elf_link_input_bfd ignores this section. */
11440 input_section->flags &= ~SEC_HAS_CONTENTS;
11441 }
a0c8462f 11442
104d59d1
JM
11443 attr_size = bfd_elf_obj_attr_size (abfd);
11444 if (attr_size)
11445 {
11446 bfd_set_section_size (abfd, o, attr_size);
11447 attr_section = o;
11448 /* Skip this section later on. */
11449 o->map_head.link_order = NULL;
11450 }
11451 else
11452 o->flags |= SEC_EXCLUDE;
11453 }
11454 }
11455
c152c796
AM
11456 /* Count up the number of relocations we will output for each output
11457 section, so that we know the sizes of the reloc sections. We
11458 also figure out some maximum sizes. */
11459 max_contents_size = 0;
11460 max_external_reloc_size = 0;
11461 max_internal_reloc_count = 0;
11462 max_sym_count = 0;
11463 max_sym_shndx_count = 0;
11464 merged = FALSE;
11465 for (o = abfd->sections; o != NULL; o = o->next)
11466 {
11467 struct bfd_elf_section_data *esdo = elf_section_data (o);
11468 o->reloc_count = 0;
11469
8423293d 11470 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11471 {
11472 unsigned int reloc_count = 0;
9eaff861 11473 unsigned int additional_reloc_count = 0;
c152c796 11474 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11475
11476 if (p->type == bfd_section_reloc_link_order
11477 || p->type == bfd_symbol_reloc_link_order)
11478 reloc_count = 1;
11479 else if (p->type == bfd_indirect_link_order)
11480 {
11481 asection *sec;
11482
11483 sec = p->u.indirect.section;
c152c796
AM
11484
11485 /* Mark all sections which are to be included in the
11486 link. This will normally be every section. We need
11487 to do this so that we can identify any sections which
11488 the linker has decided to not include. */
11489 sec->linker_mark = TRUE;
11490
11491 if (sec->flags & SEC_MERGE)
11492 merged = TRUE;
11493
eea6121a
AM
11494 if (sec->rawsize > max_contents_size)
11495 max_contents_size = sec->rawsize;
11496 if (sec->size > max_contents_size)
11497 max_contents_size = sec->size;
c152c796 11498
c152c796
AM
11499 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11500 && (sec->owner->flags & DYNAMIC) == 0)
11501 {
11502 size_t sym_count;
11503
a961cdd5
AM
11504 /* We are interested in just local symbols, not all
11505 symbols. */
c152c796
AM
11506 if (elf_bad_symtab (sec->owner))
11507 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11508 / bed->s->sizeof_sym);
11509 else
11510 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11511
11512 if (sym_count > max_sym_count)
11513 max_sym_count = sym_count;
11514
11515 if (sym_count > max_sym_shndx_count
6a40cf0c 11516 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11517 max_sym_shndx_count = sym_count;
11518
a961cdd5
AM
11519 if (esdo->this_hdr.sh_type == SHT_REL
11520 || esdo->this_hdr.sh_type == SHT_RELA)
11521 /* Some backends use reloc_count in relocation sections
11522 to count particular types of relocs. Of course,
11523 reloc sections themselves can't have relocations. */
11524 ;
11525 else if (emit_relocs)
11526 {
11527 reloc_count = sec->reloc_count;
11528 if (bed->elf_backend_count_additional_relocs)
11529 {
11530 int c;
11531 c = (*bed->elf_backend_count_additional_relocs) (sec);
11532 additional_reloc_count += c;
11533 }
11534 }
11535 else if (bed->elf_backend_count_relocs)
11536 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11537
11538 esdi = elf_section_data (sec);
11539
c152c796
AM
11540 if ((sec->flags & SEC_RELOC) != 0)
11541 {
d4730f92 11542 size_t ext_size = 0;
c152c796 11543
d4730f92
BS
11544 if (esdi->rel.hdr != NULL)
11545 ext_size = esdi->rel.hdr->sh_size;
11546 if (esdi->rela.hdr != NULL)
11547 ext_size += esdi->rela.hdr->sh_size;
7326c758 11548
c152c796
AM
11549 if (ext_size > max_external_reloc_size)
11550 max_external_reloc_size = ext_size;
11551 if (sec->reloc_count > max_internal_reloc_count)
11552 max_internal_reloc_count = sec->reloc_count;
11553 }
11554 }
11555 }
11556
11557 if (reloc_count == 0)
11558 continue;
11559
9eaff861 11560 reloc_count += additional_reloc_count;
c152c796
AM
11561 o->reloc_count += reloc_count;
11562
0e1862bb 11563 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11564 {
d4730f92 11565 if (esdi->rel.hdr)
9eaff861 11566 {
491d01d3 11567 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11568 esdo->rel.count += additional_reloc_count;
11569 }
d4730f92 11570 if (esdi->rela.hdr)
9eaff861 11571 {
491d01d3 11572 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11573 esdo->rela.count += additional_reloc_count;
11574 }
d4730f92
BS
11575 }
11576 else
11577 {
11578 if (o->use_rela_p)
11579 esdo->rela.count += reloc_count;
2c2b4ed4 11580 else
d4730f92 11581 esdo->rel.count += reloc_count;
c152c796 11582 }
c152c796
AM
11583 }
11584
9eaff861 11585 if (o->reloc_count > 0)
c152c796
AM
11586 o->flags |= SEC_RELOC;
11587 else
11588 {
11589 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11590 set it (this is probably a bug) and if it is set
11591 assign_section_numbers will create a reloc section. */
11592 o->flags &=~ SEC_RELOC;
11593 }
11594
11595 /* If the SEC_ALLOC flag is not set, force the section VMA to
11596 zero. This is done in elf_fake_sections as well, but forcing
11597 the VMA to 0 here will ensure that relocs against these
11598 sections are handled correctly. */
11599 if ((o->flags & SEC_ALLOC) == 0
11600 && ! o->user_set_vma)
11601 o->vma = 0;
11602 }
11603
0e1862bb 11604 if (! bfd_link_relocatable (info) && merged)
64f52338 11605 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11606
11607 /* Figure out the file positions for everything but the symbol table
11608 and the relocs. We set symcount to force assign_section_numbers
11609 to create a symbol table. */
8539e4e8 11610 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11611 BFD_ASSERT (! abfd->output_has_begun);
11612 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11613 goto error_return;
11614
ee75fd95 11615 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11616 for (o = abfd->sections; o != NULL; o = o->next)
11617 {
d4730f92 11618 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11619 if ((o->flags & SEC_RELOC) != 0)
11620 {
d4730f92 11621 if (esdo->rel.hdr
9eaff861 11622 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11623 goto error_return;
11624
d4730f92 11625 if (esdo->rela.hdr
9eaff861 11626 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11627 goto error_return;
11628 }
11629
11630 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11631 to count upwards while actually outputting the relocations. */
d4730f92
BS
11632 esdo->rel.count = 0;
11633 esdo->rela.count = 0;
0ce398f1
L
11634
11635 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11636 {
11637 /* Cache the section contents so that they can be compressed
11638 later. Use bfd_malloc since it will be freed by
11639 bfd_compress_section_contents. */
11640 unsigned char *contents = esdo->this_hdr.contents;
11641 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11642 abort ();
11643 contents
11644 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11645 if (contents == NULL)
11646 goto error_return;
11647 esdo->this_hdr.contents = contents;
11648 }
c152c796
AM
11649 }
11650
c152c796 11651 /* We have now assigned file positions for all the sections except
a485e98e
AM
11652 .symtab, .strtab, and non-loaded reloc sections. We start the
11653 .symtab section at the current file position, and write directly
11654 to it. We build the .strtab section in memory. */
c152c796
AM
11655 bfd_get_symcount (abfd) = 0;
11656 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11657 /* sh_name is set in prep_headers. */
11658 symtab_hdr->sh_type = SHT_SYMTAB;
11659 /* sh_flags, sh_addr and sh_size all start off zero. */
11660 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11661 /* sh_link is set in assign_section_numbers. */
11662 /* sh_info is set below. */
11663 /* sh_offset is set just below. */
72de5009 11664 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11665
ef10c3ac
L
11666 if (max_sym_count < 20)
11667 max_sym_count = 20;
64f52338 11668 htab->strtabsize = max_sym_count;
ef10c3ac 11669 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11670 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11671 if (htab->strtab == NULL)
c152c796 11672 goto error_return;
ef10c3ac
L
11673 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11674 flinfo.symshndxbuf
11675 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11676 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11677
8539e4e8 11678 if (info->strip != strip_all || emit_relocs)
c152c796 11679 {
8539e4e8
AM
11680 file_ptr off = elf_next_file_pos (abfd);
11681
11682 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11683
11684 /* Note that at this point elf_next_file_pos (abfd) is
11685 incorrect. We do not yet know the size of the .symtab section.
11686 We correct next_file_pos below, after we do know the size. */
11687
11688 /* Start writing out the symbol table. The first symbol is always a
11689 dummy symbol. */
c152c796
AM
11690 elfsym.st_value = 0;
11691 elfsym.st_size = 0;
11692 elfsym.st_info = 0;
11693 elfsym.st_other = 0;
11694 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11695 elfsym.st_target_internal = 0;
ef10c3ac
L
11696 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11697 bfd_und_section_ptr, NULL) != 1)
c152c796 11698 goto error_return;
c152c796 11699
8539e4e8
AM
11700 /* Output a symbol for each section. We output these even if we are
11701 discarding local symbols, since they are used for relocs. These
11702 symbols have no names. We store the index of each one in the
11703 index field of the section, so that we can find it again when
11704 outputting relocs. */
11705
c152c796
AM
11706 elfsym.st_size = 0;
11707 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11708 elfsym.st_other = 0;
f0b5bb34 11709 elfsym.st_value = 0;
35fc36a8 11710 elfsym.st_target_internal = 0;
c152c796
AM
11711 for (i = 1; i < elf_numsections (abfd); i++)
11712 {
11713 o = bfd_section_from_elf_index (abfd, i);
11714 if (o != NULL)
f0b5bb34
AM
11715 {
11716 o->target_index = bfd_get_symcount (abfd);
11717 elfsym.st_shndx = i;
0e1862bb 11718 if (!bfd_link_relocatable (info))
f0b5bb34 11719 elfsym.st_value = o->vma;
ef10c3ac
L
11720 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11721 NULL) != 1)
f0b5bb34
AM
11722 goto error_return;
11723 }
c152c796
AM
11724 }
11725 }
11726
11727 /* Allocate some memory to hold information read in from the input
11728 files. */
11729 if (max_contents_size != 0)
11730 {
8b127cbc
AM
11731 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11732 if (flinfo.contents == NULL)
c152c796
AM
11733 goto error_return;
11734 }
11735
11736 if (max_external_reloc_size != 0)
11737 {
8b127cbc
AM
11738 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11739 if (flinfo.external_relocs == NULL)
c152c796
AM
11740 goto error_return;
11741 }
11742
11743 if (max_internal_reloc_count != 0)
11744 {
11745 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11746 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11747 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11748 if (flinfo.internal_relocs == NULL)
c152c796
AM
11749 goto error_return;
11750 }
11751
11752 if (max_sym_count != 0)
11753 {
11754 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11755 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11756 if (flinfo.external_syms == NULL)
c152c796
AM
11757 goto error_return;
11758
11759 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11760 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11761 if (flinfo.internal_syms == NULL)
c152c796
AM
11762 goto error_return;
11763
11764 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11765 flinfo.indices = (long int *) bfd_malloc (amt);
11766 if (flinfo.indices == NULL)
c152c796
AM
11767 goto error_return;
11768
11769 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11770 flinfo.sections = (asection **) bfd_malloc (amt);
11771 if (flinfo.sections == NULL)
c152c796
AM
11772 goto error_return;
11773 }
11774
11775 if (max_sym_shndx_count != 0)
11776 {
11777 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11778 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11779 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11780 goto error_return;
11781 }
11782
64f52338 11783 if (htab->tls_sec)
c152c796
AM
11784 {
11785 bfd_vma base, end = 0;
11786 asection *sec;
11787
64f52338 11788 for (sec = htab->tls_sec;
c152c796
AM
11789 sec && (sec->flags & SEC_THREAD_LOCAL);
11790 sec = sec->next)
11791 {
3a800eb9 11792 bfd_size_type size = sec->size;
c152c796 11793
3a800eb9
AM
11794 if (size == 0
11795 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11796 {
91d6fa6a
NC
11797 struct bfd_link_order *ord = sec->map_tail.link_order;
11798
11799 if (ord != NULL)
11800 size = ord->offset + ord->size;
c152c796
AM
11801 }
11802 end = sec->vma + size;
11803 }
64f52338 11804 base = htab->tls_sec->vma;
7dc98aea
RO
11805 /* Only align end of TLS section if static TLS doesn't have special
11806 alignment requirements. */
11807 if (bed->static_tls_alignment == 1)
64f52338
AM
11808 end = align_power (end, htab->tls_sec->alignment_power);
11809 htab->tls_size = end - base;
c152c796
AM
11810 }
11811
0b52efa6
PB
11812 /* Reorder SHF_LINK_ORDER sections. */
11813 for (o = abfd->sections; o != NULL; o = o->next)
11814 {
11815 if (!elf_fixup_link_order (abfd, o))
11816 return FALSE;
11817 }
11818
2f0c68f2
CM
11819 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11820 return FALSE;
11821
c152c796
AM
11822 /* Since ELF permits relocations to be against local symbols, we
11823 must have the local symbols available when we do the relocations.
11824 Since we would rather only read the local symbols once, and we
11825 would rather not keep them in memory, we handle all the
11826 relocations for a single input file at the same time.
11827
11828 Unfortunately, there is no way to know the total number of local
11829 symbols until we have seen all of them, and the local symbol
11830 indices precede the global symbol indices. This means that when
11831 we are generating relocatable output, and we see a reloc against
11832 a global symbol, we can not know the symbol index until we have
11833 finished examining all the local symbols to see which ones we are
11834 going to output. To deal with this, we keep the relocations in
11835 memory, and don't output them until the end of the link. This is
11836 an unfortunate waste of memory, but I don't see a good way around
11837 it. Fortunately, it only happens when performing a relocatable
11838 link, which is not the common case. FIXME: If keep_memory is set
11839 we could write the relocs out and then read them again; I don't
11840 know how bad the memory loss will be. */
11841
c72f2fb2 11842 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11843 sub->output_has_begun = FALSE;
11844 for (o = abfd->sections; o != NULL; o = o->next)
11845 {
8423293d 11846 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11847 {
11848 if (p->type == bfd_indirect_link_order
11849 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11850 == bfd_target_elf_flavour)
11851 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11852 {
11853 if (! sub->output_has_begun)
11854 {
8b127cbc 11855 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11856 goto error_return;
11857 sub->output_has_begun = TRUE;
11858 }
11859 }
11860 else if (p->type == bfd_section_reloc_link_order
11861 || p->type == bfd_symbol_reloc_link_order)
11862 {
11863 if (! elf_reloc_link_order (abfd, info, o, p))
11864 goto error_return;
11865 }
11866 else
11867 {
11868 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11869 {
11870 if (p->type == bfd_indirect_link_order
11871 && (bfd_get_flavour (sub)
11872 == bfd_target_elf_flavour)
11873 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11874 != bed->s->elfclass))
11875 {
11876 const char *iclass, *oclass;
11877
aebf9be7 11878 switch (bed->s->elfclass)
351f65ca 11879 {
aebf9be7
NC
11880 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11881 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11882 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11883 default: abort ();
351f65ca 11884 }
aebf9be7
NC
11885
11886 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11887 {
aebf9be7
NC
11888 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11889 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11890 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11891 default: abort ();
351f65ca
L
11892 }
11893
11894 bfd_set_error (bfd_error_wrong_format);
4eca0228 11895 _bfd_error_handler
695344c0 11896 /* xgettext:c-format */
351f65ca
L
11897 (_("%B: file class %s incompatible with %s"),
11898 sub, iclass, oclass);
11899 }
11900
11901 goto error_return;
11902 }
c152c796
AM
11903 }
11904 }
11905 }
11906
c0f00686
L
11907 /* Free symbol buffer if needed. */
11908 if (!info->reduce_memory_overheads)
11909 {
c72f2fb2 11910 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11911 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11912 && elf_tdata (sub)->symbuf)
c0f00686
L
11913 {
11914 free (elf_tdata (sub)->symbuf);
11915 elf_tdata (sub)->symbuf = NULL;
11916 }
11917 }
11918
c152c796
AM
11919 /* Output any global symbols that got converted to local in a
11920 version script or due to symbol visibility. We do this in a
11921 separate step since ELF requires all local symbols to appear
11922 prior to any global symbols. FIXME: We should only do this if
11923 some global symbols were, in fact, converted to become local.
11924 FIXME: Will this work correctly with the Irix 5 linker? */
11925 eoinfo.failed = FALSE;
8b127cbc 11926 eoinfo.flinfo = &flinfo;
c152c796 11927 eoinfo.localsyms = TRUE;
34a79995 11928 eoinfo.file_sym_done = FALSE;
7686d77d 11929 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11930 if (eoinfo.failed)
11931 return FALSE;
11932
4e617b1e
PB
11933 /* If backend needs to output some local symbols not present in the hash
11934 table, do it now. */
8539e4e8
AM
11935 if (bed->elf_backend_output_arch_local_syms
11936 && (info->strip != strip_all || emit_relocs))
4e617b1e 11937 {
6e0b88f1 11938 typedef int (*out_sym_func)
4e617b1e
PB
11939 (void *, const char *, Elf_Internal_Sym *, asection *,
11940 struct elf_link_hash_entry *);
11941
11942 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11943 (abfd, info, &flinfo,
11944 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11945 return FALSE;
11946 }
11947
c152c796
AM
11948 /* That wrote out all the local symbols. Finish up the symbol table
11949 with the global symbols. Even if we want to strip everything we
11950 can, we still need to deal with those global symbols that got
11951 converted to local in a version script. */
11952
11953 /* The sh_info field records the index of the first non local symbol. */
11954 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11955
11956 if (dynamic
64f52338
AM
11957 && htab->dynsym != NULL
11958 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
11959 {
11960 Elf_Internal_Sym sym;
64f52338 11961 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 11962
64f52338
AM
11963 o = htab->dynsym->output_section;
11964 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
11965
11966 /* Write out the section symbols for the output sections. */
0e1862bb 11967 if (bfd_link_pic (info)
64f52338 11968 || htab->is_relocatable_executable)
c152c796
AM
11969 {
11970 asection *s;
11971
11972 sym.st_size = 0;
11973 sym.st_name = 0;
11974 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11975 sym.st_other = 0;
35fc36a8 11976 sym.st_target_internal = 0;
c152c796
AM
11977
11978 for (s = abfd->sections; s != NULL; s = s->next)
11979 {
11980 int indx;
11981 bfd_byte *dest;
11982 long dynindx;
11983
c152c796 11984 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11985 if (dynindx <= 0)
11986 continue;
11987 indx = elf_section_data (s)->this_idx;
c152c796
AM
11988 BFD_ASSERT (indx > 0);
11989 sym.st_shndx = indx;
c0d5a53d
L
11990 if (! check_dynsym (abfd, &sym))
11991 return FALSE;
c152c796
AM
11992 sym.st_value = s->vma;
11993 dest = dynsym + dynindx * bed->s->sizeof_sym;
11994 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11995 }
c152c796
AM
11996 }
11997
11998 /* Write out the local dynsyms. */
64f52338 11999 if (htab->dynlocal)
c152c796
AM
12000 {
12001 struct elf_link_local_dynamic_entry *e;
64f52338 12002 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12003 {
12004 asection *s;
12005 bfd_byte *dest;
12006
935bd1e0 12007 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12008 Note that we saved a word of storage and overwrote
12009 the original st_name with the dynstr_index. */
12010 sym = e->isym;
935bd1e0 12011 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12012
cb33740c
AM
12013 s = bfd_section_from_elf_index (e->input_bfd,
12014 e->isym.st_shndx);
12015 if (s != NULL)
c152c796 12016 {
c152c796
AM
12017 sym.st_shndx =
12018 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12019 if (! check_dynsym (abfd, &sym))
12020 return FALSE;
c152c796
AM
12021 sym.st_value = (s->output_section->vma
12022 + s->output_offset
12023 + e->isym.st_value);
12024 }
12025
c152c796
AM
12026 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12027 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12028 }
12029 }
c152c796
AM
12030 }
12031
12032 /* We get the global symbols from the hash table. */
12033 eoinfo.failed = FALSE;
12034 eoinfo.localsyms = FALSE;
8b127cbc 12035 eoinfo.flinfo = &flinfo;
7686d77d 12036 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12037 if (eoinfo.failed)
12038 return FALSE;
12039
12040 /* If backend needs to output some symbols not present in the hash
12041 table, do it now. */
8539e4e8
AM
12042 if (bed->elf_backend_output_arch_syms
12043 && (info->strip != strip_all || emit_relocs))
c152c796 12044 {
6e0b88f1 12045 typedef int (*out_sym_func)
c152c796
AM
12046 (void *, const char *, Elf_Internal_Sym *, asection *,
12047 struct elf_link_hash_entry *);
12048
12049 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12050 (abfd, info, &flinfo,
12051 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12052 return FALSE;
12053 }
12054
ef10c3ac
L
12055 /* Finalize the .strtab section. */
12056 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12057
12058 /* Swap out the .strtab section. */
12059 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12060 return FALSE;
12061
12062 /* Now we know the size of the symtab section. */
c152c796
AM
12063 if (bfd_get_symcount (abfd) > 0)
12064 {
ee3b52e9
L
12065 /* Finish up and write out the symbol string table (.strtab)
12066 section. */
ad32986f 12067 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12068 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12069
ad32986f 12070 if (elf_symtab_shndx_list (abfd))
8539e4e8 12071 {
ad32986f 12072 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12073
ad32986f
NC
12074 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12075 {
12076 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12077 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12078 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12079 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12080 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12081
ad32986f
NC
12082 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12083 off, TRUE);
12084
12085 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12086 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12087 return FALSE;
12088 }
8539e4e8 12089 }
ee3b52e9
L
12090
12091 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12092 /* sh_name was set in prep_headers. */
12093 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12094 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12095 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12096 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12097 symstrtab_hdr->sh_entsize = 0;
12098 symstrtab_hdr->sh_link = 0;
12099 symstrtab_hdr->sh_info = 0;
12100 /* sh_offset is set just below. */
12101 symstrtab_hdr->sh_addralign = 1;
12102
12103 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12104 off, TRUE);
12105 elf_next_file_pos (abfd) = off;
12106
c152c796 12107 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12108 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12109 return FALSE;
12110 }
12111
76359541
TP
12112 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12113 {
4eca0228
AM
12114 _bfd_error_handler (_("%B: failed to generate import library"),
12115 info->out_implib_bfd);
76359541
TP
12116 return FALSE;
12117 }
12118
c152c796
AM
12119 /* Adjust the relocs to have the correct symbol indices. */
12120 for (o = abfd->sections; o != NULL; o = o->next)
12121 {
d4730f92 12122 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12123 bfd_boolean sort;
c152c796
AM
12124 if ((o->flags & SEC_RELOC) == 0)
12125 continue;
12126
28dbcedc 12127 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12128 if (esdo->rel.hdr != NULL
9eaff861 12129 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
12130 return FALSE;
12131 if (esdo->rela.hdr != NULL
9eaff861 12132 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 12133 return FALSE;
c152c796
AM
12134
12135 /* Set the reloc_count field to 0 to prevent write_relocs from
12136 trying to swap the relocs out itself. */
12137 o->reloc_count = 0;
12138 }
12139
12140 if (dynamic && info->combreloc && dynobj != NULL)
12141 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12142
12143 /* If we are linking against a dynamic object, or generating a
12144 shared library, finish up the dynamic linking information. */
12145 if (dynamic)
12146 {
12147 bfd_byte *dyncon, *dynconend;
12148
12149 /* Fix up .dynamic entries. */
3d4d4302 12150 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12151 BFD_ASSERT (o != NULL);
12152
12153 dyncon = o->contents;
eea6121a 12154 dynconend = o->contents + o->size;
c152c796
AM
12155 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12156 {
12157 Elf_Internal_Dyn dyn;
12158 const char *name;
12159 unsigned int type;
64487780
AM
12160 bfd_size_type sh_size;
12161 bfd_vma sh_addr;
c152c796
AM
12162
12163 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12164
12165 switch (dyn.d_tag)
12166 {
12167 default:
12168 continue;
12169 case DT_NULL:
12170 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12171 {
12172 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12173 {
12174 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12175 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12176 default: continue;
12177 }
12178 dyn.d_un.d_val = relativecount;
12179 relativecount = 0;
12180 break;
12181 }
12182 continue;
12183
12184 case DT_INIT:
12185 name = info->init_function;
12186 goto get_sym;
12187 case DT_FINI:
12188 name = info->fini_function;
12189 get_sym:
12190 {
12191 struct elf_link_hash_entry *h;
12192
64f52338 12193 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12194 if (h != NULL
12195 && (h->root.type == bfd_link_hash_defined
12196 || h->root.type == bfd_link_hash_defweak))
12197 {
bef26483 12198 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12199 o = h->root.u.def.section;
12200 if (o->output_section != NULL)
bef26483 12201 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12202 + o->output_offset);
12203 else
12204 {
12205 /* The symbol is imported from another shared
12206 library and does not apply to this one. */
bef26483 12207 dyn.d_un.d_ptr = 0;
c152c796
AM
12208 }
12209 break;
12210 }
12211 }
12212 continue;
12213
12214 case DT_PREINIT_ARRAYSZ:
12215 name = ".preinit_array";
4ade44b7 12216 goto get_out_size;
c152c796
AM
12217 case DT_INIT_ARRAYSZ:
12218 name = ".init_array";
4ade44b7 12219 goto get_out_size;
c152c796
AM
12220 case DT_FINI_ARRAYSZ:
12221 name = ".fini_array";
4ade44b7 12222 get_out_size:
c152c796
AM
12223 o = bfd_get_section_by_name (abfd, name);
12224 if (o == NULL)
12225 {
4eca0228 12226 _bfd_error_handler
4ade44b7 12227 (_("could not find section %s"), name);
c152c796
AM
12228 goto error_return;
12229 }
eea6121a 12230 if (o->size == 0)
4eca0228 12231 _bfd_error_handler
c152c796 12232 (_("warning: %s section has zero size"), name);
eea6121a 12233 dyn.d_un.d_val = o->size;
c152c796
AM
12234 break;
12235
12236 case DT_PREINIT_ARRAY:
12237 name = ".preinit_array";
4ade44b7 12238 goto get_out_vma;
c152c796
AM
12239 case DT_INIT_ARRAY:
12240 name = ".init_array";
4ade44b7 12241 goto get_out_vma;
c152c796
AM
12242 case DT_FINI_ARRAY:
12243 name = ".fini_array";
4ade44b7
AM
12244 get_out_vma:
12245 o = bfd_get_section_by_name (abfd, name);
12246 goto do_vma;
c152c796
AM
12247
12248 case DT_HASH:
12249 name = ".hash";
12250 goto get_vma;
fdc90cb4
JJ
12251 case DT_GNU_HASH:
12252 name = ".gnu.hash";
12253 goto get_vma;
c152c796
AM
12254 case DT_STRTAB:
12255 name = ".dynstr";
12256 goto get_vma;
12257 case DT_SYMTAB:
12258 name = ".dynsym";
12259 goto get_vma;
12260 case DT_VERDEF:
12261 name = ".gnu.version_d";
12262 goto get_vma;
12263 case DT_VERNEED:
12264 name = ".gnu.version_r";
12265 goto get_vma;
12266 case DT_VERSYM:
12267 name = ".gnu.version";
12268 get_vma:
4ade44b7
AM
12269 o = bfd_get_linker_section (dynobj, name);
12270 do_vma:
c152c796
AM
12271 if (o == NULL)
12272 {
4eca0228 12273 _bfd_error_handler
4ade44b7 12274 (_("could not find section %s"), name);
c152c796
AM
12275 goto error_return;
12276 }
894891db
NC
12277 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12278 {
4eca0228 12279 _bfd_error_handler
894891db
NC
12280 (_("warning: section '%s' is being made into a note"), name);
12281 bfd_set_error (bfd_error_nonrepresentable_section);
12282 goto error_return;
12283 }
4ade44b7 12284 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12285 break;
12286
12287 case DT_REL:
12288 case DT_RELA:
12289 case DT_RELSZ:
12290 case DT_RELASZ:
12291 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12292 type = SHT_REL;
12293 else
12294 type = SHT_RELA;
64487780
AM
12295 sh_size = 0;
12296 sh_addr = 0;
c152c796
AM
12297 for (i = 1; i < elf_numsections (abfd); i++)
12298 {
12299 Elf_Internal_Shdr *hdr;
12300
12301 hdr = elf_elfsections (abfd)[i];
12302 if (hdr->sh_type == type
12303 && (hdr->sh_flags & SHF_ALLOC) != 0)
12304 {
64487780
AM
12305 sh_size += hdr->sh_size;
12306 if (sh_addr == 0
12307 || sh_addr > hdr->sh_addr)
12308 sh_addr = hdr->sh_addr;
c152c796
AM
12309 }
12310 }
64487780 12311
64f52338
AM
12312 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12313 {
12314 /* Don't count procedure linkage table relocs in the
12315 overall reloc count. */
64487780
AM
12316 sh_size -= htab->srelplt->size;
12317 if (sh_size == 0)
12318 /* If the size is zero, make the address zero too.
12319 This is to avoid a glibc bug. If the backend
12320 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12321 zero, then we'll put DT_RELA at the end of
12322 DT_JMPREL. glibc will interpret the end of
12323 DT_RELA matching the end of DT_JMPREL as the
12324 case where DT_RELA includes DT_JMPREL, and for
12325 LD_BIND_NOW will decide that processing DT_RELA
12326 will process the PLT relocs too. Net result:
12327 No PLT relocs applied. */
12328 sh_addr = 0;
12329
64f52338
AM
12330 /* If .rela.plt is the first .rela section, exclude
12331 it from DT_RELA. */
64487780
AM
12332 else if (sh_addr == (htab->srelplt->output_section->vma
12333 + htab->srelplt->output_offset))
12334 sh_addr += htab->srelplt->size;
64f52338 12335 }
64487780
AM
12336
12337 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12338 dyn.d_un.d_val = sh_size;
12339 else
12340 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12341 break;
12342 }
12343 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12344 }
12345 }
12346
12347 /* If we have created any dynamic sections, then output them. */
12348 if (dynobj != NULL)
12349 {
12350 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12351 goto error_return;
12352
943284cc 12353 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12354 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12355 || info->error_textrel)
3d4d4302 12356 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12357 {
12358 bfd_byte *dyncon, *dynconend;
12359
943284cc
DJ
12360 dyncon = o->contents;
12361 dynconend = o->contents + o->size;
12362 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12363 {
12364 Elf_Internal_Dyn dyn;
12365
12366 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12367
12368 if (dyn.d_tag == DT_TEXTREL)
12369 {
c192a133
AM
12370 if (info->error_textrel)
12371 info->callbacks->einfo
12372 (_("%P%X: read-only segment has dynamic relocations.\n"));
12373 else
12374 info->callbacks->einfo
12375 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12376 break;
12377 }
12378 }
12379 }
12380
c152c796
AM
12381 for (o = dynobj->sections; o != NULL; o = o->next)
12382 {
12383 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12384 || o->size == 0
c152c796
AM
12385 || o->output_section == bfd_abs_section_ptr)
12386 continue;
12387 if ((o->flags & SEC_LINKER_CREATED) == 0)
12388 {
12389 /* At this point, we are only interested in sections
12390 created by _bfd_elf_link_create_dynamic_sections. */
12391 continue;
12392 }
64f52338 12393 if (htab->stab_info.stabstr == o)
3722b82f 12394 continue;
64f52338 12395 if (htab->eh_info.hdr_sec == o)
eea6121a 12396 continue;
3d4d4302 12397 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12398 {
12399 if (! bfd_set_section_contents (abfd, o->output_section,
12400 o->contents,
37b01f6a
DG
12401 (file_ptr) o->output_offset
12402 * bfd_octets_per_byte (abfd),
eea6121a 12403 o->size))
c152c796
AM
12404 goto error_return;
12405 }
12406 else
12407 {
12408 /* The contents of the .dynstr section are actually in a
12409 stringtab. */
8539e4e8
AM
12410 file_ptr off;
12411
c152c796
AM
12412 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12413 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12414 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12415 goto error_return;
12416 }
12417 }
12418 }
12419
0e1862bb 12420 if (bfd_link_relocatable (info))
c152c796
AM
12421 {
12422 bfd_boolean failed = FALSE;
12423
12424 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12425 if (failed)
12426 goto error_return;
12427 }
12428
12429 /* If we have optimized stabs strings, output them. */
64f52338 12430 if (htab->stab_info.stabstr != NULL)
c152c796 12431 {
64f52338 12432 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12433 goto error_return;
12434 }
12435
9f7c3e5e
AM
12436 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12437 goto error_return;
c152c796 12438
9f7c3e5e 12439 elf_final_link_free (abfd, &flinfo);
c152c796 12440
12bd6957 12441 elf_linker (abfd) = TRUE;
c152c796 12442
104d59d1
JM
12443 if (attr_section)
12444 {
a50b1753 12445 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12446 if (contents == NULL)
d0f16d5e 12447 return FALSE; /* Bail out and fail. */
104d59d1
JM
12448 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12449 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12450 free (contents);
12451 }
12452
c152c796
AM
12453 return TRUE;
12454
12455 error_return:
9f7c3e5e 12456 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12457 return FALSE;
12458}
12459\f
5241d853
RS
12460/* Initialize COOKIE for input bfd ABFD. */
12461
12462static bfd_boolean
12463init_reloc_cookie (struct elf_reloc_cookie *cookie,
12464 struct bfd_link_info *info, bfd *abfd)
12465{
12466 Elf_Internal_Shdr *symtab_hdr;
12467 const struct elf_backend_data *bed;
12468
12469 bed = get_elf_backend_data (abfd);
12470 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12471
12472 cookie->abfd = abfd;
12473 cookie->sym_hashes = elf_sym_hashes (abfd);
12474 cookie->bad_symtab = elf_bad_symtab (abfd);
12475 if (cookie->bad_symtab)
12476 {
12477 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12478 cookie->extsymoff = 0;
12479 }
12480 else
12481 {
12482 cookie->locsymcount = symtab_hdr->sh_info;
12483 cookie->extsymoff = symtab_hdr->sh_info;
12484 }
12485
12486 if (bed->s->arch_size == 32)
12487 cookie->r_sym_shift = 8;
12488 else
12489 cookie->r_sym_shift = 32;
12490
12491 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12492 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12493 {
12494 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12495 cookie->locsymcount, 0,
12496 NULL, NULL, NULL);
12497 if (cookie->locsyms == NULL)
12498 {
12499 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12500 return FALSE;
12501 }
12502 if (info->keep_memory)
12503 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12504 }
12505 return TRUE;
12506}
12507
12508/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12509
12510static void
12511fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12512{
12513 Elf_Internal_Shdr *symtab_hdr;
12514
12515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12516 if (cookie->locsyms != NULL
12517 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12518 free (cookie->locsyms);
12519}
12520
12521/* Initialize the relocation information in COOKIE for input section SEC
12522 of input bfd ABFD. */
12523
12524static bfd_boolean
12525init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12526 struct bfd_link_info *info, bfd *abfd,
12527 asection *sec)
12528{
12529 const struct elf_backend_data *bed;
12530
12531 if (sec->reloc_count == 0)
12532 {
12533 cookie->rels = NULL;
12534 cookie->relend = NULL;
12535 }
12536 else
12537 {
12538 bed = get_elf_backend_data (abfd);
12539
12540 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12541 info->keep_memory);
12542 if (cookie->rels == NULL)
12543 return FALSE;
12544 cookie->rel = cookie->rels;
12545 cookie->relend = (cookie->rels
12546 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12547 }
12548 cookie->rel = cookie->rels;
12549 return TRUE;
12550}
12551
12552/* Free the memory allocated by init_reloc_cookie_rels,
12553 if appropriate. */
12554
12555static void
12556fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12557 asection *sec)
12558{
12559 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12560 free (cookie->rels);
12561}
12562
12563/* Initialize the whole of COOKIE for input section SEC. */
12564
12565static bfd_boolean
12566init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12567 struct bfd_link_info *info,
12568 asection *sec)
12569{
12570 if (!init_reloc_cookie (cookie, info, sec->owner))
12571 goto error1;
12572 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12573 goto error2;
12574 return TRUE;
12575
12576 error2:
12577 fini_reloc_cookie (cookie, sec->owner);
12578 error1:
12579 return FALSE;
12580}
12581
12582/* Free the memory allocated by init_reloc_cookie_for_section,
12583 if appropriate. */
12584
12585static void
12586fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12587 asection *sec)
12588{
12589 fini_reloc_cookie_rels (cookie, sec);
12590 fini_reloc_cookie (cookie, sec->owner);
12591}
12592\f
c152c796
AM
12593/* Garbage collect unused sections. */
12594
07adf181
AM
12595/* Default gc_mark_hook. */
12596
12597asection *
12598_bfd_elf_gc_mark_hook (asection *sec,
12599 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12600 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12601 struct elf_link_hash_entry *h,
12602 Elf_Internal_Sym *sym)
12603{
12604 if (h != NULL)
12605 {
12606 switch (h->root.type)
12607 {
12608 case bfd_link_hash_defined:
12609 case bfd_link_hash_defweak:
12610 return h->root.u.def.section;
12611
12612 case bfd_link_hash_common:
12613 return h->root.u.c.p->section;
12614
12615 default:
12616 break;
12617 }
12618 }
12619 else
12620 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12621
12622 return NULL;
12623}
12624
a6a4679f
AM
12625/* For undefined __start_<name> and __stop_<name> symbols, return the
12626 first input section matching <name>. Return NULL otherwise. */
12627
12628asection *
12629_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12630 struct elf_link_hash_entry *h)
12631{
12632 asection *s;
12633 const char *sec_name;
12634
12635 if (h->root.type != bfd_link_hash_undefined
12636 && h->root.type != bfd_link_hash_undefweak)
12637 return NULL;
12638
12639 s = h->root.u.undef.section;
12640 if (s != NULL)
12641 {
12642 if (s == (asection *) 0 - 1)
12643 return NULL;
12644 return s;
12645 }
12646
12647 sec_name = NULL;
12648 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12649 sec_name = h->root.root.string + 8;
12650 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12651 sec_name = h->root.root.string + 7;
12652
12653 if (sec_name != NULL && *sec_name != '\0')
12654 {
12655 bfd *i;
12656
12657 for (i = info->input_bfds; i != NULL; i = i->link.next)
12658 {
12659 s = bfd_get_section_by_name (i, sec_name);
12660 if (s != NULL)
12661 {
12662 h->root.u.undef.section = s;
12663 break;
12664 }
12665 }
12666 }
12667
12668 if (s == NULL)
12669 h->root.u.undef.section = (asection *) 0 - 1;
12670
12671 return s;
12672}
12673
5241d853
RS
12674/* COOKIE->rel describes a relocation against section SEC, which is
12675 a section we've decided to keep. Return the section that contains
12676 the relocation symbol, or NULL if no section contains it. */
12677
12678asection *
12679_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12680 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12681 struct elf_reloc_cookie *cookie,
12682 bfd_boolean *start_stop)
5241d853
RS
12683{
12684 unsigned long r_symndx;
12685 struct elf_link_hash_entry *h;
12686
12687 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12688 if (r_symndx == STN_UNDEF)
5241d853
RS
12689 return NULL;
12690
12691 if (r_symndx >= cookie->locsymcount
12692 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12693 {
12694 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12695 if (h == NULL)
12696 {
12697 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12698 sec->owner);
12699 return NULL;
12700 }
5241d853
RS
12701 while (h->root.type == bfd_link_hash_indirect
12702 || h->root.type == bfd_link_hash_warning)
12703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12704 h->mark = 1;
4e6b54a6
AM
12705 /* If this symbol is weak and there is a non-weak definition, we
12706 keep the non-weak definition because many backends put
12707 dynamic reloc info on the non-weak definition for code
12708 handling copy relocs. */
12709 if (h->u.weakdef != NULL)
12710 h->u.weakdef->mark = 1;
1cce69b9 12711
a6a4679f 12712 if (start_stop != NULL)
1cce69b9
AM
12713 {
12714 /* To work around a glibc bug, mark all XXX input sections
12715 when there is an as yet undefined reference to __start_XXX
12716 or __stop_XXX symbols. The linker will later define such
12717 symbols for orphan input sections that have a name
12718 representable as a C identifier. */
a6a4679f 12719 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12720
a6a4679f 12721 if (s != NULL)
1cce69b9 12722 {
a6a4679f
AM
12723 *start_stop = !s->gc_mark;
12724 return s;
1cce69b9
AM
12725 }
12726 }
12727
5241d853
RS
12728 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12729 }
12730
12731 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12732 &cookie->locsyms[r_symndx]);
12733}
12734
12735/* COOKIE->rel describes a relocation against section SEC, which is
12736 a section we've decided to keep. Mark the section that contains
9d0a14d3 12737 the relocation symbol. */
5241d853
RS
12738
12739bfd_boolean
12740_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12741 asection *sec,
12742 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12743 struct elf_reloc_cookie *cookie)
5241d853
RS
12744{
12745 asection *rsec;
1cce69b9 12746 bfd_boolean start_stop = FALSE;
5241d853 12747
1cce69b9
AM
12748 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12749 while (rsec != NULL)
5241d853 12750 {
1cce69b9
AM
12751 if (!rsec->gc_mark)
12752 {
12753 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12754 || (rsec->owner->flags & DYNAMIC) != 0)
12755 rsec->gc_mark = 1;
12756 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12757 return FALSE;
12758 }
12759 if (!start_stop)
12760 break;
199af150 12761 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12762 }
12763 return TRUE;
12764}
12765
07adf181
AM
12766/* The mark phase of garbage collection. For a given section, mark
12767 it and any sections in this section's group, and all the sections
12768 which define symbols to which it refers. */
12769
ccfa59ea
AM
12770bfd_boolean
12771_bfd_elf_gc_mark (struct bfd_link_info *info,
12772 asection *sec,
6a5bb875 12773 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12774{
12775 bfd_boolean ret;
9d0a14d3 12776 asection *group_sec, *eh_frame;
c152c796
AM
12777
12778 sec->gc_mark = 1;
12779
12780 /* Mark all the sections in the group. */
12781 group_sec = elf_section_data (sec)->next_in_group;
12782 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12783 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12784 return FALSE;
12785
12786 /* Look through the section relocs. */
12787 ret = TRUE;
9d0a14d3
RS
12788 eh_frame = elf_eh_frame_section (sec->owner);
12789 if ((sec->flags & SEC_RELOC) != 0
12790 && sec->reloc_count > 0
12791 && sec != eh_frame)
c152c796 12792 {
5241d853 12793 struct elf_reloc_cookie cookie;
c152c796 12794
5241d853
RS
12795 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12796 ret = FALSE;
c152c796 12797 else
c152c796 12798 {
5241d853 12799 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12800 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12801 {
12802 ret = FALSE;
12803 break;
12804 }
12805 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12806 }
12807 }
9d0a14d3
RS
12808
12809 if (ret && eh_frame && elf_fde_list (sec))
12810 {
12811 struct elf_reloc_cookie cookie;
12812
12813 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12814 ret = FALSE;
12815 else
12816 {
12817 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12818 gc_mark_hook, &cookie))
12819 ret = FALSE;
12820 fini_reloc_cookie_for_section (&cookie, eh_frame);
12821 }
12822 }
12823
2f0c68f2
CM
12824 eh_frame = elf_section_eh_frame_entry (sec);
12825 if (ret && eh_frame && !eh_frame->gc_mark)
12826 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12827 ret = FALSE;
12828
c152c796
AM
12829 return ret;
12830}
12831
3c758495
TG
12832/* Scan and mark sections in a special or debug section group. */
12833
12834static void
12835_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12836{
12837 /* Point to first section of section group. */
12838 asection *ssec;
12839 /* Used to iterate the section group. */
12840 asection *msec;
12841
12842 bfd_boolean is_special_grp = TRUE;
12843 bfd_boolean is_debug_grp = TRUE;
12844
12845 /* First scan to see if group contains any section other than debug
12846 and special section. */
12847 ssec = msec = elf_next_in_group (grp);
12848 do
12849 {
12850 if ((msec->flags & SEC_DEBUGGING) == 0)
12851 is_debug_grp = FALSE;
12852
12853 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12854 is_special_grp = FALSE;
12855
12856 msec = elf_next_in_group (msec);
12857 }
12858 while (msec != ssec);
12859
12860 /* If this is a pure debug section group or pure special section group,
12861 keep all sections in this group. */
12862 if (is_debug_grp || is_special_grp)
12863 {
12864 do
12865 {
12866 msec->gc_mark = 1;
12867 msec = elf_next_in_group (msec);
12868 }
12869 while (msec != ssec);
12870 }
12871}
12872
7f6ab9f8
AM
12873/* Keep debug and special sections. */
12874
12875bfd_boolean
12876_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12877 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12878{
12879 bfd *ibfd;
12880
c72f2fb2 12881 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12882 {
12883 asection *isec;
12884 bfd_boolean some_kept;
b40bf0a2 12885 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12886
12887 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12888 continue;
12889
b40bf0a2
NC
12890 /* Ensure all linker created sections are kept,
12891 see if any other section is already marked,
12892 and note if we have any fragmented debug sections. */
12893 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12894 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12895 {
12896 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12897 isec->gc_mark = 1;
12898 else if (isec->gc_mark)
12899 some_kept = TRUE;
b40bf0a2
NC
12900
12901 if (debug_frag_seen == FALSE
12902 && (isec->flags & SEC_DEBUGGING)
12903 && CONST_STRNEQ (isec->name, ".debug_line."))
12904 debug_frag_seen = TRUE;
7f6ab9f8
AM
12905 }
12906
12907 /* If no section in this file will be kept, then we can
b40bf0a2 12908 toss out the debug and special sections. */
7f6ab9f8
AM
12909 if (!some_kept)
12910 continue;
12911
12912 /* Keep debug and special sections like .comment when they are
3c758495
TG
12913 not part of a group. Also keep section groups that contain
12914 just debug sections or special sections. */
7f6ab9f8 12915 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12916 {
12917 if ((isec->flags & SEC_GROUP) != 0)
12918 _bfd_elf_gc_mark_debug_special_section_group (isec);
12919 else if (((isec->flags & SEC_DEBUGGING) != 0
12920 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12921 && elf_next_in_group (isec) == NULL)
12922 isec->gc_mark = 1;
12923 }
b40bf0a2
NC
12924
12925 if (! debug_frag_seen)
12926 continue;
12927
12928 /* Look for CODE sections which are going to be discarded,
12929 and find and discard any fragmented debug sections which
12930 are associated with that code section. */
12931 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12932 if ((isec->flags & SEC_CODE) != 0
12933 && isec->gc_mark == 0)
12934 {
12935 unsigned int ilen;
12936 asection *dsec;
12937
12938 ilen = strlen (isec->name);
12939
12940 /* Association is determined by the name of the debug section
12941 containing the name of the code section as a suffix. For
12942 example .debug_line.text.foo is a debug section associated
12943 with .text.foo. */
12944 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12945 {
12946 unsigned int dlen;
12947
12948 if (dsec->gc_mark == 0
12949 || (dsec->flags & SEC_DEBUGGING) == 0)
12950 continue;
12951
12952 dlen = strlen (dsec->name);
12953
12954 if (dlen > ilen
12955 && strncmp (dsec->name + (dlen - ilen),
12956 isec->name, ilen) == 0)
12957 {
12958 dsec->gc_mark = 0;
b40bf0a2
NC
12959 }
12960 }
12961 }
7f6ab9f8
AM
12962 }
12963 return TRUE;
12964}
12965
c152c796
AM
12966/* The sweep phase of garbage collection. Remove all garbage sections. */
12967
12968typedef bfd_boolean (*gc_sweep_hook_fn)
12969 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12970
12971static bfd_boolean
ccabcbe5 12972elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12973{
12974 bfd *sub;
ccabcbe5
AM
12975 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12976 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 12977
c72f2fb2 12978 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12979 {
12980 asection *o;
12981
b19a8f85
L
12982 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12983 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12984 continue;
12985
12986 for (o = sub->sections; o != NULL; o = o->next)
12987 {
a33dafc3
L
12988 /* When any section in a section group is kept, we keep all
12989 sections in the section group. If the first member of
12990 the section group is excluded, we will also exclude the
12991 group section. */
12992 if (o->flags & SEC_GROUP)
12993 {
12994 asection *first = elf_next_in_group (o);
12995 o->gc_mark = first->gc_mark;
12996 }
c152c796 12997
1e7eae0d 12998 if (o->gc_mark)
c152c796
AM
12999 continue;
13000
13001 /* Skip sweeping sections already excluded. */
13002 if (o->flags & SEC_EXCLUDE)
13003 continue;
13004
13005 /* Since this is early in the link process, it is simple
13006 to remove a section from the output. */
13007 o->flags |= SEC_EXCLUDE;
13008
c55fe096 13009 if (info->print_gc_sections && o->size != 0)
695344c0 13010 /* xgettext:c-format */
c08bb8dd
AM
13011 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13012 o, sub);
c17d87de 13013
c152c796
AM
13014 /* But we also have to update some of the relocation
13015 info we collected before. */
13016 if (gc_sweep_hook
e8aaee2a 13017 && (o->flags & SEC_RELOC) != 0
9850436d
AM
13018 && o->reloc_count != 0
13019 && !((info->strip == strip_all || info->strip == strip_debugger)
13020 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 13021 && !bfd_is_abs_section (o->output_section))
c152c796
AM
13022 {
13023 Elf_Internal_Rela *internal_relocs;
13024 bfd_boolean r;
13025
13026 internal_relocs
13027 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13028 info->keep_memory);
13029 if (internal_relocs == NULL)
13030 return FALSE;
13031
13032 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13033
13034 if (elf_section_data (o)->relocs != internal_relocs)
13035 free (internal_relocs);
13036
13037 if (!r)
13038 return FALSE;
13039 }
13040 }
13041 }
13042
c152c796
AM
13043 return TRUE;
13044}
13045
13046/* Propagate collected vtable information. This is called through
13047 elf_link_hash_traverse. */
13048
13049static bfd_boolean
13050elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13051{
c152c796 13052 /* Those that are not vtables. */
f6e332e6 13053 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13054 return TRUE;
13055
13056 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 13057 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13058 return TRUE;
13059
13060 /* If we've already been done, exit. */
f6e332e6 13061 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
13062 return TRUE;
13063
13064 /* Make sure the parent's table is up to date. */
f6e332e6 13065 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 13066
f6e332e6 13067 if (h->vtable->used == NULL)
c152c796
AM
13068 {
13069 /* None of this table's entries were referenced. Re-use the
13070 parent's table. */
f6e332e6
AM
13071 h->vtable->used = h->vtable->parent->vtable->used;
13072 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
13073 }
13074 else
13075 {
13076 size_t n;
13077 bfd_boolean *cu, *pu;
13078
13079 /* Or the parent's entries into ours. */
f6e332e6 13080 cu = h->vtable->used;
c152c796 13081 cu[-1] = TRUE;
f6e332e6 13082 pu = h->vtable->parent->vtable->used;
c152c796
AM
13083 if (pu != NULL)
13084 {
13085 const struct elf_backend_data *bed;
13086 unsigned int log_file_align;
13087
13088 bed = get_elf_backend_data (h->root.u.def.section->owner);
13089 log_file_align = bed->s->log_file_align;
f6e332e6 13090 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
13091 while (n--)
13092 {
13093 if (*pu)
13094 *cu = TRUE;
13095 pu++;
13096 cu++;
13097 }
13098 }
13099 }
13100
13101 return TRUE;
13102}
13103
13104static bfd_boolean
13105elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13106{
13107 asection *sec;
13108 bfd_vma hstart, hend;
13109 Elf_Internal_Rela *relstart, *relend, *rel;
13110 const struct elf_backend_data *bed;
13111 unsigned int log_file_align;
13112
c152c796
AM
13113 /* Take care of both those symbols that do not describe vtables as
13114 well as those that are not loaded. */
f6e332e6 13115 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13116 return TRUE;
13117
13118 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13119 || h->root.type == bfd_link_hash_defweak);
13120
13121 sec = h->root.u.def.section;
13122 hstart = h->root.u.def.value;
13123 hend = hstart + h->size;
13124
13125 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13126 if (!relstart)
13127 return *(bfd_boolean *) okp = FALSE;
13128 bed = get_elf_backend_data (sec->owner);
13129 log_file_align = bed->s->log_file_align;
13130
13131 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13132
13133 for (rel = relstart; rel < relend; ++rel)
13134 if (rel->r_offset >= hstart && rel->r_offset < hend)
13135 {
13136 /* If the entry is in use, do nothing. */
f6e332e6
AM
13137 if (h->vtable->used
13138 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13139 {
13140 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13141 if (h->vtable->used[entry])
c152c796
AM
13142 continue;
13143 }
13144 /* Otherwise, kill it. */
13145 rel->r_offset = rel->r_info = rel->r_addend = 0;
13146 }
13147
13148 return TRUE;
13149}
13150
87538722
AM
13151/* Mark sections containing dynamically referenced symbols. When
13152 building shared libraries, we must assume that any visible symbol is
13153 referenced. */
715df9b8 13154
64d03ab5
AM
13155bfd_boolean
13156bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13157{
87538722 13158 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13159 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13160
715df9b8
EB
13161 if ((h->root.type == bfd_link_hash_defined
13162 || h->root.type == bfd_link_hash_defweak)
87538722 13163 && (h->ref_dynamic
c4621b33 13164 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13165 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13166 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13167 && (!bfd_link_executable (info)
22185505 13168 || info->gc_keep_exported
b407645f
AM
13169 || info->export_dynamic
13170 || (h->dynamic
13171 && d != NULL
13172 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13173 && (h->versioned >= versioned
54e8959c
L
13174 || !bfd_hide_sym_by_version (info->version_info,
13175 h->root.root.string)))))
715df9b8
EB
13176 h->root.u.def.section->flags |= SEC_KEEP;
13177
13178 return TRUE;
13179}
3b36f7e6 13180
74f0fb50
AM
13181/* Keep all sections containing symbols undefined on the command-line,
13182 and the section containing the entry symbol. */
13183
13184void
13185_bfd_elf_gc_keep (struct bfd_link_info *info)
13186{
13187 struct bfd_sym_chain *sym;
13188
13189 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13190 {
13191 struct elf_link_hash_entry *h;
13192
13193 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13194 FALSE, FALSE, FALSE);
13195
13196 if (h != NULL
13197 && (h->root.type == bfd_link_hash_defined
13198 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13199 && !bfd_is_abs_section (h->root.u.def.section)
13200 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13201 h->root.u.def.section->flags |= SEC_KEEP;
13202 }
13203}
13204
2f0c68f2
CM
13205bfd_boolean
13206bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13207 struct bfd_link_info *info)
13208{
13209 bfd *ibfd = info->input_bfds;
13210
13211 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13212 {
13213 asection *sec;
13214 struct elf_reloc_cookie cookie;
13215
13216 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13217 continue;
13218
13219 if (!init_reloc_cookie (&cookie, info, ibfd))
13220 return FALSE;
13221
13222 for (sec = ibfd->sections; sec; sec = sec->next)
13223 {
13224 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13225 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13226 {
13227 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13228 fini_reloc_cookie_rels (&cookie, sec);
13229 }
13230 }
13231 }
13232 return TRUE;
13233}
13234
c152c796
AM
13235/* Do mark and sweep of unused sections. */
13236
13237bfd_boolean
13238bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13239{
13240 bfd_boolean ok = TRUE;
13241 bfd *sub;
6a5bb875 13242 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13243 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13244 struct elf_link_hash_table *htab;
c152c796 13245
64d03ab5 13246 if (!bed->can_gc_sections
715df9b8 13247 || !is_elf_hash_table (info->hash))
c152c796 13248 {
4eca0228 13249 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13250 return TRUE;
13251 }
13252
74f0fb50 13253 bed->gc_keep (info);
da44f4e5 13254 htab = elf_hash_table (info);
74f0fb50 13255
9d0a14d3
RS
13256 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13257 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13258 for (sub = info->input_bfds;
13259 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13260 sub = sub->link.next)
9d0a14d3
RS
13261 {
13262 asection *sec;
13263 struct elf_reloc_cookie cookie;
13264
13265 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13266 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13267 {
13268 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13269 if (elf_section_data (sec)->sec_info
13270 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13271 elf_eh_frame_section (sub) = sec;
13272 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13273 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13274 }
13275 }
9d0a14d3 13276
c152c796 13277 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13278 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13279 if (!ok)
13280 return FALSE;
13281
13282 /* Kill the vtable relocations that were not used. */
da44f4e5 13283 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13284 if (!ok)
13285 return FALSE;
13286
715df9b8 13287 /* Mark dynamically referenced symbols. */
22185505 13288 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13289 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13290
715df9b8 13291 /* Grovel through relocs to find out who stays ... */
64d03ab5 13292 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13293 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13294 {
13295 asection *o;
13296
b19a8f85
L
13297 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13298 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13299 continue;
13300
7f6ab9f8
AM
13301 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13302 Also treat note sections as a root, if the section is not part
13303 of a group. */
c152c796 13304 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13305 if (!o->gc_mark
13306 && (o->flags & SEC_EXCLUDE) == 0
24007750 13307 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13308 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13309 && elf_next_in_group (o) == NULL )))
13310 {
13311 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13312 return FALSE;
13313 }
c152c796
AM
13314 }
13315
6a5bb875 13316 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13317 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13318
c152c796 13319 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13320 return elf_gc_sweep (abfd, info);
c152c796
AM
13321}
13322\f
13323/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13324
13325bfd_boolean
13326bfd_elf_gc_record_vtinherit (bfd *abfd,
13327 asection *sec,
13328 struct elf_link_hash_entry *h,
13329 bfd_vma offset)
13330{
13331 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13332 struct elf_link_hash_entry **search, *child;
ef53be89 13333 size_t extsymcount;
c152c796
AM
13334 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13335
13336 /* The sh_info field of the symtab header tells us where the
13337 external symbols start. We don't care about the local symbols at
13338 this point. */
13339 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13340 if (!elf_bad_symtab (abfd))
13341 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13342
13343 sym_hashes = elf_sym_hashes (abfd);
13344 sym_hashes_end = sym_hashes + extsymcount;
13345
13346 /* Hunt down the child symbol, which is in this section at the same
13347 offset as the relocation. */
13348 for (search = sym_hashes; search != sym_hashes_end; ++search)
13349 {
13350 if ((child = *search) != NULL
13351 && (child->root.type == bfd_link_hash_defined
13352 || child->root.type == bfd_link_hash_defweak)
13353 && child->root.u.def.section == sec
13354 && child->root.u.def.value == offset)
13355 goto win;
13356 }
13357
695344c0
NC
13358 /* xgettext:c-format */
13359 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13360 abfd, sec, (unsigned long) offset);
c152c796
AM
13361 bfd_set_error (bfd_error_invalid_operation);
13362 return FALSE;
13363
13364 win:
f6e332e6
AM
13365 if (!child->vtable)
13366 {
ca4be51c
AM
13367 child->vtable = ((struct elf_link_virtual_table_entry *)
13368 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13369 if (!child->vtable)
13370 return FALSE;
13371 }
c152c796
AM
13372 if (!h)
13373 {
13374 /* This *should* only be the absolute section. It could potentially
13375 be that someone has defined a non-global vtable though, which
13376 would be bad. It isn't worth paging in the local symbols to be
13377 sure though; that case should simply be handled by the assembler. */
13378
f6e332e6 13379 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13380 }
13381 else
f6e332e6 13382 child->vtable->parent = h;
c152c796
AM
13383
13384 return TRUE;
13385}
13386
13387/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13388
13389bfd_boolean
13390bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13391 asection *sec ATTRIBUTE_UNUSED,
13392 struct elf_link_hash_entry *h,
13393 bfd_vma addend)
13394{
13395 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13396 unsigned int log_file_align = bed->s->log_file_align;
13397
f6e332e6
AM
13398 if (!h->vtable)
13399 {
ca4be51c
AM
13400 h->vtable = ((struct elf_link_virtual_table_entry *)
13401 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13402 if (!h->vtable)
13403 return FALSE;
13404 }
13405
13406 if (addend >= h->vtable->size)
c152c796
AM
13407 {
13408 size_t size, bytes, file_align;
f6e332e6 13409 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13410
13411 /* While the symbol is undefined, we have to be prepared to handle
13412 a zero size. */
13413 file_align = 1 << log_file_align;
13414 if (h->root.type == bfd_link_hash_undefined)
13415 size = addend + file_align;
13416 else
13417 {
13418 size = h->size;
13419 if (addend >= size)
13420 {
13421 /* Oops! We've got a reference past the defined end of
13422 the table. This is probably a bug -- shall we warn? */
13423 size = addend + file_align;
13424 }
13425 }
13426 size = (size + file_align - 1) & -file_align;
13427
13428 /* Allocate one extra entry for use as a "done" flag for the
13429 consolidation pass. */
13430 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13431
13432 if (ptr)
13433 {
a50b1753 13434 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13435
13436 if (ptr != NULL)
13437 {
13438 size_t oldbytes;
13439
f6e332e6 13440 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13441 * sizeof (bfd_boolean));
13442 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13443 }
13444 }
13445 else
a50b1753 13446 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13447
13448 if (ptr == NULL)
13449 return FALSE;
13450
13451 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13452 h->vtable->used = ptr + 1;
13453 h->vtable->size = size;
c152c796
AM
13454 }
13455
f6e332e6 13456 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13457
13458 return TRUE;
13459}
13460
ae17ab41
CM
13461/* Map an ELF section header flag to its corresponding string. */
13462typedef struct
13463{
13464 char *flag_name;
13465 flagword flag_value;
13466} elf_flags_to_name_table;
13467
13468static elf_flags_to_name_table elf_flags_to_names [] =
13469{
13470 { "SHF_WRITE", SHF_WRITE },
13471 { "SHF_ALLOC", SHF_ALLOC },
13472 { "SHF_EXECINSTR", SHF_EXECINSTR },
13473 { "SHF_MERGE", SHF_MERGE },
13474 { "SHF_STRINGS", SHF_STRINGS },
13475 { "SHF_INFO_LINK", SHF_INFO_LINK},
13476 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13477 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13478 { "SHF_GROUP", SHF_GROUP },
13479 { "SHF_TLS", SHF_TLS },
13480 { "SHF_MASKOS", SHF_MASKOS },
13481 { "SHF_EXCLUDE", SHF_EXCLUDE },
13482};
13483
b9c361e0
JL
13484/* Returns TRUE if the section is to be included, otherwise FALSE. */
13485bfd_boolean
ae17ab41 13486bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13487 struct flag_info *flaginfo,
b9c361e0 13488 asection *section)
ae17ab41 13489{
8b127cbc 13490 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13491
8b127cbc 13492 if (!flaginfo->flags_initialized)
ae17ab41 13493 {
8b127cbc
AM
13494 bfd *obfd = info->output_bfd;
13495 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13496 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13497 int with_hex = 0;
13498 int without_hex = 0;
13499
8b127cbc 13500 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13501 {
b9c361e0 13502 unsigned i;
8b127cbc 13503 flagword (*lookup) (char *);
ae17ab41 13504
8b127cbc
AM
13505 lookup = bed->elf_backend_lookup_section_flags_hook;
13506 if (lookup != NULL)
ae17ab41 13507 {
8b127cbc 13508 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13509
13510 if (hexval != 0)
13511 {
13512 if (tf->with == with_flags)
13513 with_hex |= hexval;
13514 else if (tf->with == without_flags)
13515 without_hex |= hexval;
13516 tf->valid = TRUE;
13517 continue;
13518 }
ae17ab41 13519 }
8b127cbc 13520 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13521 {
8b127cbc 13522 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13523 {
13524 if (tf->with == with_flags)
13525 with_hex |= elf_flags_to_names[i].flag_value;
13526 else if (tf->with == without_flags)
13527 without_hex |= elf_flags_to_names[i].flag_value;
13528 tf->valid = TRUE;
13529 break;
13530 }
13531 }
8b127cbc 13532 if (!tf->valid)
b9c361e0 13533 {
68ffbac6 13534 info->callbacks->einfo
8b127cbc 13535 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13536 return FALSE;
ae17ab41
CM
13537 }
13538 }
8b127cbc
AM
13539 flaginfo->flags_initialized = TRUE;
13540 flaginfo->only_with_flags |= with_hex;
13541 flaginfo->not_with_flags |= without_hex;
ae17ab41 13542 }
ae17ab41 13543
8b127cbc 13544 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13545 return FALSE;
13546
8b127cbc 13547 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13548 return FALSE;
13549
13550 return TRUE;
ae17ab41
CM
13551}
13552
c152c796
AM
13553struct alloc_got_off_arg {
13554 bfd_vma gotoff;
10455f89 13555 struct bfd_link_info *info;
c152c796
AM
13556};
13557
13558/* We need a special top-level link routine to convert got reference counts
13559 to real got offsets. */
13560
13561static bfd_boolean
13562elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13563{
a50b1753 13564 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13565 bfd *obfd = gofarg->info->output_bfd;
13566 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13567
c152c796
AM
13568 if (h->got.refcount > 0)
13569 {
13570 h->got.offset = gofarg->gotoff;
10455f89 13571 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13572 }
13573 else
13574 h->got.offset = (bfd_vma) -1;
13575
13576 return TRUE;
13577}
13578
13579/* And an accompanying bit to work out final got entry offsets once
13580 we're done. Should be called from final_link. */
13581
13582bfd_boolean
13583bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13584 struct bfd_link_info *info)
13585{
13586 bfd *i;
13587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13588 bfd_vma gotoff;
c152c796
AM
13589 struct alloc_got_off_arg gofarg;
13590
10455f89
HPN
13591 BFD_ASSERT (abfd == info->output_bfd);
13592
c152c796
AM
13593 if (! is_elf_hash_table (info->hash))
13594 return FALSE;
13595
13596 /* The GOT offset is relative to the .got section, but the GOT header is
13597 put into the .got.plt section, if the backend uses it. */
13598 if (bed->want_got_plt)
13599 gotoff = 0;
13600 else
13601 gotoff = bed->got_header_size;
13602
13603 /* Do the local .got entries first. */
c72f2fb2 13604 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13605 {
13606 bfd_signed_vma *local_got;
ef53be89 13607 size_t j, locsymcount;
c152c796
AM
13608 Elf_Internal_Shdr *symtab_hdr;
13609
13610 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13611 continue;
13612
13613 local_got = elf_local_got_refcounts (i);
13614 if (!local_got)
13615 continue;
13616
13617 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13618 if (elf_bad_symtab (i))
13619 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13620 else
13621 locsymcount = symtab_hdr->sh_info;
13622
13623 for (j = 0; j < locsymcount; ++j)
13624 {
13625 if (local_got[j] > 0)
13626 {
13627 local_got[j] = gotoff;
10455f89 13628 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13629 }
13630 else
13631 local_got[j] = (bfd_vma) -1;
13632 }
13633 }
13634
13635 /* Then the global .got entries. .plt refcounts are handled by
13636 adjust_dynamic_symbol */
13637 gofarg.gotoff = gotoff;
10455f89 13638 gofarg.info = info;
c152c796
AM
13639 elf_link_hash_traverse (elf_hash_table (info),
13640 elf_gc_allocate_got_offsets,
13641 &gofarg);
13642 return TRUE;
13643}
13644
13645/* Many folk need no more in the way of final link than this, once
13646 got entry reference counting is enabled. */
13647
13648bfd_boolean
13649bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13650{
13651 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13652 return FALSE;
13653
13654 /* Invoke the regular ELF backend linker to do all the work. */
13655 return bfd_elf_final_link (abfd, info);
13656}
13657
13658bfd_boolean
13659bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13660{
a50b1753 13661 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13662
13663 if (rcookie->bad_symtab)
13664 rcookie->rel = rcookie->rels;
13665
13666 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13667 {
13668 unsigned long r_symndx;
13669
13670 if (! rcookie->bad_symtab)
13671 if (rcookie->rel->r_offset > offset)
13672 return FALSE;
13673 if (rcookie->rel->r_offset != offset)
13674 continue;
13675
13676 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13677 if (r_symndx == STN_UNDEF)
c152c796
AM
13678 return TRUE;
13679
13680 if (r_symndx >= rcookie->locsymcount
13681 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13682 {
13683 struct elf_link_hash_entry *h;
13684
13685 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13686
13687 while (h->root.type == bfd_link_hash_indirect
13688 || h->root.type == bfd_link_hash_warning)
13689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13690
13691 if ((h->root.type == bfd_link_hash_defined
13692 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13693 && (h->root.u.def.section->owner != rcookie->abfd
13694 || h->root.u.def.section->kept_section != NULL
13695 || discarded_section (h->root.u.def.section)))
c152c796 13696 return TRUE;
c152c796
AM
13697 }
13698 else
13699 {
13700 /* It's not a relocation against a global symbol,
13701 but it could be a relocation against a local
13702 symbol for a discarded section. */
13703 asection *isec;
13704 Elf_Internal_Sym *isym;
13705
13706 /* Need to: get the symbol; get the section. */
13707 isym = &rcookie->locsyms[r_symndx];
cb33740c 13708 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13709 if (isec != NULL
13710 && (isec->kept_section != NULL
13711 || discarded_section (isec)))
cb33740c 13712 return TRUE;
c152c796
AM
13713 }
13714 return FALSE;
13715 }
13716 return FALSE;
13717}
13718
13719/* Discard unneeded references to discarded sections.
75938853
AM
13720 Returns -1 on error, 1 if any section's size was changed, 0 if
13721 nothing changed. This function assumes that the relocations are in
13722 sorted order, which is true for all known assemblers. */
c152c796 13723
75938853 13724int
c152c796
AM
13725bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13726{
13727 struct elf_reloc_cookie cookie;
18cd5bce 13728 asection *o;
c152c796 13729 bfd *abfd;
75938853 13730 int changed = 0;
c152c796
AM
13731
13732 if (info->traditional_format
13733 || !is_elf_hash_table (info->hash))
75938853 13734 return 0;
c152c796 13735
18cd5bce
AM
13736 o = bfd_get_section_by_name (output_bfd, ".stab");
13737 if (o != NULL)
c152c796 13738 {
18cd5bce 13739 asection *i;
c152c796 13740
18cd5bce 13741 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13742 {
18cd5bce
AM
13743 if (i->size == 0
13744 || i->reloc_count == 0
13745 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13746 continue;
c152c796 13747
18cd5bce
AM
13748 abfd = i->owner;
13749 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13750 continue;
c152c796 13751
18cd5bce 13752 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13753 return -1;
c152c796 13754
18cd5bce
AM
13755 if (_bfd_discard_section_stabs (abfd, i,
13756 elf_section_data (i)->sec_info,
5241d853
RS
13757 bfd_elf_reloc_symbol_deleted_p,
13758 &cookie))
75938853 13759 changed = 1;
18cd5bce
AM
13760
13761 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13762 }
18cd5bce
AM
13763 }
13764
2f0c68f2
CM
13765 o = NULL;
13766 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13767 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13768 if (o != NULL)
13769 {
13770 asection *i;
c152c796 13771
18cd5bce 13772 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13773 {
18cd5bce
AM
13774 if (i->size == 0)
13775 continue;
13776
13777 abfd = i->owner;
13778 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13779 continue;
13780
13781 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13782 return -1;
18cd5bce
AM
13783
13784 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13785 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13786 bfd_elf_reloc_symbol_deleted_p,
13787 &cookie))
75938853 13788 changed = 1;
18cd5bce
AM
13789
13790 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13791 }
18cd5bce 13792 }
c152c796 13793
18cd5bce
AM
13794 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13795 {
13796 const struct elf_backend_data *bed;
c152c796 13797
18cd5bce
AM
13798 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13799 continue;
13800
13801 bed = get_elf_backend_data (abfd);
13802
13803 if (bed->elf_backend_discard_info != NULL)
13804 {
13805 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13806 return -1;
18cd5bce
AM
13807
13808 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13809 changed = 1;
18cd5bce
AM
13810
13811 fini_reloc_cookie (&cookie, abfd);
13812 }
c152c796
AM
13813 }
13814
2f0c68f2
CM
13815 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13816 _bfd_elf_end_eh_frame_parsing (info);
13817
13818 if (info->eh_frame_hdr_type
0e1862bb 13819 && !bfd_link_relocatable (info)
c152c796 13820 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13821 changed = 1;
c152c796 13822
75938853 13823 return changed;
c152c796 13824}
082b7297 13825
43e1669b 13826bfd_boolean
0c511000 13827_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13828 asection *sec,
c0f00686 13829 struct bfd_link_info *info)
082b7297
L
13830{
13831 flagword flags;
c77ec726 13832 const char *name, *key;
082b7297
L
13833 struct bfd_section_already_linked *l;
13834 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13835
c77ec726
AM
13836 if (sec->output_section == bfd_abs_section_ptr)
13837 return FALSE;
0c511000 13838
c77ec726 13839 flags = sec->flags;
0c511000 13840
c77ec726
AM
13841 /* Return if it isn't a linkonce section. A comdat group section
13842 also has SEC_LINK_ONCE set. */
13843 if ((flags & SEC_LINK_ONCE) == 0)
13844 return FALSE;
0c511000 13845
c77ec726
AM
13846 /* Don't put group member sections on our list of already linked
13847 sections. They are handled as a group via their group section. */
13848 if (elf_sec_group (sec) != NULL)
13849 return FALSE;
0c511000 13850
c77ec726
AM
13851 /* For a SHT_GROUP section, use the group signature as the key. */
13852 name = sec->name;
13853 if ((flags & SEC_GROUP) != 0
13854 && elf_next_in_group (sec) != NULL
13855 && elf_group_name (elf_next_in_group (sec)) != NULL)
13856 key = elf_group_name (elf_next_in_group (sec));
13857 else
13858 {
13859 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13860 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13861 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13862 key++;
0c511000 13863 else
c77ec726
AM
13864 /* Must be a user linkonce section that doesn't follow gcc's
13865 naming convention. In this case we won't be matching
13866 single member groups. */
13867 key = name;
0c511000 13868 }
6d2cd210 13869
c77ec726 13870 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13871
13872 for (l = already_linked_list->entry; l != NULL; l = l->next)
13873 {
c2370991 13874 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13875 sections with a signature of <key> (<key> is some string),
13876 and linkonce sections named .gnu.linkonce.<type>.<key>.
13877 Match like sections. LTO plugin sections are an exception.
13878 They are always named .gnu.linkonce.t.<key> and match either
13879 type of section. */
13880 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13881 && ((flags & SEC_GROUP) != 0
13882 || strcmp (name, l->sec->name) == 0))
13883 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13884 {
13885 /* The section has already been linked. See if we should
6d2cd210 13886 issue a warning. */
c77ec726
AM
13887 if (!_bfd_handle_already_linked (sec, l, info))
13888 return FALSE;
082b7297 13889
c77ec726 13890 if (flags & SEC_GROUP)
3d7f7666 13891 {
c77ec726
AM
13892 asection *first = elf_next_in_group (sec);
13893 asection *s = first;
3d7f7666 13894
c77ec726 13895 while (s != NULL)
3d7f7666 13896 {
c77ec726
AM
13897 s->output_section = bfd_abs_section_ptr;
13898 /* Record which group discards it. */
13899 s->kept_section = l->sec;
13900 s = elf_next_in_group (s);
13901 /* These lists are circular. */
13902 if (s == first)
13903 break;
3d7f7666
L
13904 }
13905 }
082b7297 13906
43e1669b 13907 return TRUE;
082b7297
L
13908 }
13909 }
13910
c77ec726
AM
13911 /* A single member comdat group section may be discarded by a
13912 linkonce section and vice versa. */
13913 if ((flags & SEC_GROUP) != 0)
3d7f7666 13914 {
c77ec726 13915 asection *first = elf_next_in_group (sec);
c2370991 13916
c77ec726
AM
13917 if (first != NULL && elf_next_in_group (first) == first)
13918 /* Check this single member group against linkonce sections. */
13919 for (l = already_linked_list->entry; l != NULL; l = l->next)
13920 if ((l->sec->flags & SEC_GROUP) == 0
13921 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13922 {
13923 first->output_section = bfd_abs_section_ptr;
13924 first->kept_section = l->sec;
13925 sec->output_section = bfd_abs_section_ptr;
13926 break;
13927 }
13928 }
13929 else
13930 /* Check this linkonce section against single member groups. */
13931 for (l = already_linked_list->entry; l != NULL; l = l->next)
13932 if (l->sec->flags & SEC_GROUP)
6d2cd210 13933 {
c77ec726 13934 asection *first = elf_next_in_group (l->sec);
6d2cd210 13935
c77ec726
AM
13936 if (first != NULL
13937 && elf_next_in_group (first) == first
13938 && bfd_elf_match_symbols_in_sections (first, sec, info))
13939 {
13940 sec->output_section = bfd_abs_section_ptr;
13941 sec->kept_section = first;
13942 break;
13943 }
6d2cd210 13944 }
0c511000 13945
c77ec726
AM
13946 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13947 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13948 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13949 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13950 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13951 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13952 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13953 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13954 The reverse order cannot happen as there is never a bfd with only the
13955 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13956 matter as here were are looking only for cross-bfd sections. */
13957
13958 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13959 for (l = already_linked_list->entry; l != NULL; l = l->next)
13960 if ((l->sec->flags & SEC_GROUP) == 0
13961 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13962 {
13963 if (abfd != l->sec->owner)
13964 sec->output_section = bfd_abs_section_ptr;
13965 break;
13966 }
80c29487 13967
082b7297 13968 /* This is the first section with this name. Record it. */
c77ec726 13969 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13970 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13971 return sec->output_section == bfd_abs_section_ptr;
082b7297 13972}
81e1b023 13973
a4d8e49b
L
13974bfd_boolean
13975_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13976{
13977 return sym->st_shndx == SHN_COMMON;
13978}
13979
13980unsigned int
13981_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13982{
13983 return SHN_COMMON;
13984}
13985
13986asection *
13987_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13988{
13989 return bfd_com_section_ptr;
13990}
10455f89
HPN
13991
13992bfd_vma
13993_bfd_elf_default_got_elt_size (bfd *abfd,
13994 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13995 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13996 bfd *ibfd ATTRIBUTE_UNUSED,
13997 unsigned long symndx ATTRIBUTE_UNUSED)
13998{
13999 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14000 return bed->s->arch_size / 8;
14001}
83bac4b0
NC
14002
14003/* Routines to support the creation of dynamic relocs. */
14004
83bac4b0
NC
14005/* Returns the name of the dynamic reloc section associated with SEC. */
14006
14007static const char *
14008get_dynamic_reloc_section_name (bfd * abfd,
14009 asection * sec,
14010 bfd_boolean is_rela)
14011{
ddcf1fcf
BS
14012 char *name;
14013 const char *old_name = bfd_get_section_name (NULL, sec);
14014 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14015
ddcf1fcf 14016 if (old_name == NULL)
83bac4b0
NC
14017 return NULL;
14018
ddcf1fcf 14019 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14020 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14021
14022 return name;
14023}
14024
14025/* Returns the dynamic reloc section associated with SEC.
14026 If necessary compute the name of the dynamic reloc section based
14027 on SEC's name (looked up in ABFD's string table) and the setting
14028 of IS_RELA. */
14029
14030asection *
14031_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14032 asection * sec,
14033 bfd_boolean is_rela)
14034{
14035 asection * reloc_sec = elf_section_data (sec)->sreloc;
14036
14037 if (reloc_sec == NULL)
14038 {
14039 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14040
14041 if (name != NULL)
14042 {
3d4d4302 14043 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14044
14045 if (reloc_sec != NULL)
14046 elf_section_data (sec)->sreloc = reloc_sec;
14047 }
14048 }
14049
14050 return reloc_sec;
14051}
14052
14053/* Returns the dynamic reloc section associated with SEC. If the
14054 section does not exist it is created and attached to the DYNOBJ
14055 bfd and stored in the SRELOC field of SEC's elf_section_data
14056 structure.
f8076f98 14057
83bac4b0
NC
14058 ALIGNMENT is the alignment for the newly created section and
14059 IS_RELA defines whether the name should be .rela.<SEC's name>
14060 or .rel.<SEC's name>. The section name is looked up in the
14061 string table associated with ABFD. */
14062
14063asection *
ca4be51c
AM
14064_bfd_elf_make_dynamic_reloc_section (asection *sec,
14065 bfd *dynobj,
14066 unsigned int alignment,
14067 bfd *abfd,
14068 bfd_boolean is_rela)
83bac4b0
NC
14069{
14070 asection * reloc_sec = elf_section_data (sec)->sreloc;
14071
14072 if (reloc_sec == NULL)
14073 {
14074 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14075
14076 if (name == NULL)
14077 return NULL;
14078
3d4d4302 14079 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14080
14081 if (reloc_sec == NULL)
14082 {
3d4d4302
AM
14083 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14084 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14085 if ((sec->flags & SEC_ALLOC) != 0)
14086 flags |= SEC_ALLOC | SEC_LOAD;
14087
3d4d4302 14088 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14089 if (reloc_sec != NULL)
14090 {
8877b5e5
AM
14091 /* _bfd_elf_get_sec_type_attr chooses a section type by
14092 name. Override as it may be wrong, eg. for a user
14093 section named "auto" we'll get ".relauto" which is
14094 seen to be a .rela section. */
14095 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14096 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14097 reloc_sec = NULL;
14098 }
14099 }
14100
14101 elf_section_data (sec)->sreloc = reloc_sec;
14102 }
14103
14104 return reloc_sec;
14105}
1338dd10 14106
bffebb6b
AM
14107/* Copy the ELF symbol type and other attributes for a linker script
14108 assignment from HSRC to HDEST. Generally this should be treated as
14109 if we found a strong non-dynamic definition for HDEST (except that
14110 ld ignores multiple definition errors). */
1338dd10 14111void
bffebb6b
AM
14112_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14113 struct bfd_link_hash_entry *hdest,
14114 struct bfd_link_hash_entry *hsrc)
1338dd10 14115{
bffebb6b
AM
14116 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14117 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14118 Elf_Internal_Sym isym;
1338dd10
PB
14119
14120 ehdest->type = ehsrc->type;
35fc36a8 14121 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14122
14123 isym.st_other = ehsrc->other;
b8417128 14124 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14125}
351f65ca
L
14126
14127/* Append a RELA relocation REL to section S in BFD. */
14128
14129void
14130elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14131{
14132 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14133 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14134 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14135 bed->s->swap_reloca_out (abfd, rel, loc);
14136}
14137
14138/* Append a REL relocation REL to section S in BFD. */
14139
14140void
14141elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14142{
14143 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14144 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14145 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14146 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14147}
This page took 2.489482 seconds and 4 git commands to generate.