Place copy relocations in .dynbss when target is read-only.
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
6f2750fe 2 Copyright (C) 1995-2016 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;
122 }
123
124 bh = &h->root;
cf18fda4 125 bed = get_elf_backend_data (abfd);
d98685ac 126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 127 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
e28df02b 132 h->non_elf = 0;
12b2843a 133 h->root.linker_def = 1;
d98685ac 134 h->type = STT_OBJECT;
00b7642b
AM
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 137
ccabcbe5 138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
139 return h;
140}
141
b34976b6 142bfd_boolean
268b6b39 143_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
144{
145 flagword flags;
aad5d350 146 asection *s;
252b5132 147 struct elf_link_hash_entry *h;
9c5bfbb7 148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 149 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
150
151 /* This function may be called more than once. */
ce558b89 152 if (htab->sgot != NULL)
b34976b6 153 return TRUE;
252b5132 154
e5a52504 155 flags = bed->dynamic_sec_flags;
252b5132 156
14b2f831
AM
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
6de2ae4a
L
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
252b5132 166
14b2f831 167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
252b5132
RH
173 if (bed->want_got_plt)
174 {
14b2f831 175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 176 if (s == NULL
6de2ae4a
L
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
b34976b6 179 return FALSE;
6de2ae4a 180 htab->sgotplt = s;
252b5132
RH
181 }
182
64e77c6d
L
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
2517a57f
AM
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
6de2ae4a
L
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
2517a57f 194 elf_hash_table (info)->hgot = h;
d98685ac
AM
195 if (h == NULL)
196 return FALSE;
2517a57f 197 }
252b5132 198
b34976b6 199 return TRUE;
252b5132
RH
200}
201\f
7e9f0867
AM
202/* Create a strtab to hold the dynamic symbol names. */
203static bfd_boolean
204_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205{
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
6cd255ca
L
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
7e9f0867
AM
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236}
237
45d6a902
AM
238/* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
252b5132 244
b34976b6 245bfd_boolean
268b6b39 246_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 247{
45d6a902 248 flagword flags;
91d6fa6a 249 asection *s;
9c5bfbb7 250 const struct elf_backend_data *bed;
9637f6ef 251 struct elf_link_hash_entry *h;
252b5132 252
0eddce27 253 if (! is_elf_hash_table (info->hash))
45d6a902
AM
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
7e9f0867
AM
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
45d6a902 261
7e9f0867 262 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
45d6a902
AM
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
9b8b325a 269 if (bfd_link_executable (info) && !info->nointerp)
252b5132 270 {
14b2f831
AM
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
3496cb2a 273 if (s == NULL)
45d6a902
AM
274 return FALSE;
275 }
bb0deeff 276
45d6a902
AM
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
14b2f831
AM
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
45d6a902 281 if (s == NULL
45d6a902
AM
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
14b2f831
AM
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
45d6a902 287 if (s == NULL
45d6a902
AM
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
14b2f831
AM
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
45d6a902 293 if (s == NULL
45d6a902
AM
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
14b2f831
AM
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
45d6a902 299 if (s == NULL
45d6a902
AM
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
cae1fbbb 302 elf_hash_table (info)->dynsym = s;
45d6a902 303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
3496cb2a 306 if (s == NULL)
45d6a902
AM
307 return FALSE;
308
14b2f831 309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 310 if (s == NULL
45d6a902
AM
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
9637f6ef
L
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
45d6a902
AM
323 return FALSE;
324
fdc90cb4
JJ
325 if (info->emit_hash)
326 {
14b2f831
AM
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
fdc90cb4
JJ
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
14b2f831
AM
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
fdc90cb4
JJ
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
45d6a902
AM
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
894891db
NC
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361}
362
363/* Create dynamic sections when linking against a dynamic object. */
364
365bfd_boolean
268b6b39 366_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
367{
368 flagword flags, pltflags;
7325306f 369 struct elf_link_hash_entry *h;
45d6a902 370 asection *s;
9c5bfbb7 371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 372 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 373
252b5132
RH
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
e5a52504 376 flags = bed->dynamic_sec_flags;
252b5132
RH
377
378 pltflags = flags;
252b5132 379 if (bed->plt_not_loaded)
6df4d94c
MM
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
5d1634d7 383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
14b2f831 389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 390 if (s == NULL
252b5132 391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 392 return FALSE;
6de2ae4a 393 htab->splt = s;
252b5132 394
d98685ac
AM
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
7325306f
RS
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
252b5132 405
14b2f831
AM
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
252b5132 410 if (s == NULL
45d6a902 411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 412 return FALSE;
6de2ae4a 413 htab->srelplt = s;
252b5132
RH
414
415 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 416 return FALSE;
252b5132 417
3018b441
RH
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
14b2f831
AM
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 428 if (s == NULL)
b34976b6 429 return FALSE;
9d19e4fd 430 htab->sdynbss = s;
252b5132 431
5474d94f
AM
432 if (bed->want_dynrelro)
433 {
434 /* Similarly, but for symbols that were originally in read-only
435 sections. */
436 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
437 (SEC_ALLOC | SEC_READONLY
438 | SEC_HAS_CONTENTS
439 | SEC_LINKER_CREATED));
440 if (s == NULL)
441 return FALSE;
442 htab->sdynrelro = s;
443 }
444
3018b441 445 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
446 normally needed. We need to create it here, though, so that the
447 linker will map it to an output section. We can't just create it
448 only if we need it, because we will not know whether we need it
449 until we have seen all the input files, and the first time the
450 main linker code calls BFD after examining all the input files
451 (size_dynamic_sections) the input sections have already been
452 mapped to the output sections. If the section turns out not to
453 be needed, we can discard it later. We will never need this
454 section when generating a shared object, since they do not use
455 copy relocs. */
9d19e4fd 456 if (bfd_link_executable (info))
3018b441 457 {
14b2f831
AM
458 s = bfd_make_section_anyway_with_flags (abfd,
459 (bed->rela_plts_and_copies_p
460 ? ".rela.bss" : ".rel.bss"),
461 flags | SEC_READONLY);
3018b441 462 if (s == NULL
45d6a902 463 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 464 return FALSE;
9d19e4fd 465 htab->srelbss = s;
5474d94f
AM
466
467 if (bed->want_dynrelro)
468 {
469 s = (bfd_make_section_anyway_with_flags
470 (abfd, (bed->rela_plts_and_copies_p
471 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
472 flags | SEC_READONLY));
473 if (s == NULL
474 || ! bfd_set_section_alignment (abfd, s,
475 bed->s->log_file_align))
476 return FALSE;
477 htab->sreldynrelro = s;
478 }
3018b441 479 }
252b5132
RH
480 }
481
b34976b6 482 return TRUE;
252b5132
RH
483}
484\f
252b5132
RH
485/* Record a new dynamic symbol. We record the dynamic symbols as we
486 read the input files, since we need to have a list of all of them
487 before we can determine the final sizes of the output sections.
488 Note that we may actually call this function even though we are not
489 going to output any dynamic symbols; in some cases we know that a
490 symbol should be in the dynamic symbol table, but only if there is
491 one. */
492
b34976b6 493bfd_boolean
c152c796
AM
494bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
495 struct elf_link_hash_entry *h)
252b5132
RH
496{
497 if (h->dynindx == -1)
498 {
2b0f7ef9 499 struct elf_strtab_hash *dynstr;
68b6ddd0 500 char *p;
252b5132 501 const char *name;
ef53be89 502 size_t indx;
252b5132 503
7a13edea
NC
504 /* XXX: The ABI draft says the linker must turn hidden and
505 internal symbols into STB_LOCAL symbols when producing the
506 DSO. However, if ld.so honors st_other in the dynamic table,
507 this would not be necessary. */
508 switch (ELF_ST_VISIBILITY (h->other))
509 {
510 case STV_INTERNAL:
511 case STV_HIDDEN:
9d6eee78
L
512 if (h->root.type != bfd_link_hash_undefined
513 && h->root.type != bfd_link_hash_undefweak)
38048eb9 514 {
f5385ebf 515 h->forced_local = 1;
67687978
PB
516 if (!elf_hash_table (info)->is_relocatable_executable)
517 return TRUE;
7a13edea 518 }
0444bdd4 519
7a13edea
NC
520 default:
521 break;
522 }
523
252b5132
RH
524 h->dynindx = elf_hash_table (info)->dynsymcount;
525 ++elf_hash_table (info)->dynsymcount;
526
527 dynstr = elf_hash_table (info)->dynstr;
528 if (dynstr == NULL)
529 {
530 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 531 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 532 if (dynstr == NULL)
b34976b6 533 return FALSE;
252b5132
RH
534 }
535
536 /* We don't put any version information in the dynamic string
aad5d350 537 table. */
252b5132
RH
538 name = h->root.root.string;
539 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
540 if (p != NULL)
541 /* We know that the p points into writable memory. In fact,
542 there are only a few symbols that have read-only names, being
543 those like _GLOBAL_OFFSET_TABLE_ that are created specially
544 by the backends. Most symbols will have names pointing into
545 an ELF string table read from a file, or to objalloc memory. */
546 *p = 0;
547
548 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
549
550 if (p != NULL)
551 *p = ELF_VER_CHR;
252b5132 552
ef53be89 553 if (indx == (size_t) -1)
b34976b6 554 return FALSE;
252b5132
RH
555 h->dynstr_index = indx;
556 }
557
b34976b6 558 return TRUE;
252b5132 559}
45d6a902 560\f
55255dae
L
561/* Mark a symbol dynamic. */
562
28caa186 563static void
55255dae 564bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
565 struct elf_link_hash_entry *h,
566 Elf_Internal_Sym *sym)
55255dae 567{
40b36307 568 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 569
40b36307 570 /* It may be called more than once on the same H. */
0e1862bb 571 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
572 return;
573
40b36307
L
574 if ((info->dynamic_data
575 && (h->type == STT_OBJECT
b8871f35 576 || h->type == STT_COMMON
40b36307 577 || (sym != NULL
b8871f35
L
578 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
579 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 580 || (d != NULL
40b36307
L
581 && h->root.type == bfd_link_hash_new
582 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
583 h->dynamic = 1;
584}
585
45d6a902
AM
586/* Record an assignment to a symbol made by a linker script. We need
587 this in case some dynamic object refers to this symbol. */
588
589bfd_boolean
fe21a8fc
L
590bfd_elf_record_link_assignment (bfd *output_bfd,
591 struct bfd_link_info *info,
268b6b39 592 const char *name,
fe21a8fc
L
593 bfd_boolean provide,
594 bfd_boolean hidden)
45d6a902 595{
00cbee0a 596 struct elf_link_hash_entry *h, *hv;
4ea42fb7 597 struct elf_link_hash_table *htab;
00cbee0a 598 const struct elf_backend_data *bed;
45d6a902 599
0eddce27 600 if (!is_elf_hash_table (info->hash))
45d6a902
AM
601 return TRUE;
602
4ea42fb7
AM
603 htab = elf_hash_table (info);
604 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 605 if (h == NULL)
4ea42fb7 606 return provide;
45d6a902 607
8e2a4f11
AM
608 if (h->root.type == bfd_link_hash_warning)
609 h = (struct elf_link_hash_entry *) h->root.u.i.link;
610
0f550b3d
L
611 if (h->versioned == unknown)
612 {
613 /* Set versioned if symbol version is unknown. */
614 char *version = strrchr (name, ELF_VER_CHR);
615 if (version)
616 {
617 if (version > name && version[-1] != ELF_VER_CHR)
618 h->versioned = versioned_hidden;
619 else
620 h->versioned = versioned;
621 }
622 }
623
00cbee0a 624 switch (h->root.type)
77cfaee6 625 {
00cbee0a
L
626 case bfd_link_hash_defined:
627 case bfd_link_hash_defweak:
628 case bfd_link_hash_common:
629 break;
630 case bfd_link_hash_undefweak:
631 case bfd_link_hash_undefined:
632 /* Since we're defining the symbol, don't let it seem to have not
633 been defined. record_dynamic_symbol and size_dynamic_sections
634 may depend on this. */
4ea42fb7 635 h->root.type = bfd_link_hash_new;
77cfaee6
AM
636 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
637 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
638 break;
639 case bfd_link_hash_new:
40b36307 640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 641 h->non_elf = 0;
00cbee0a
L
642 break;
643 case bfd_link_hash_indirect:
644 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 645 the versioned symbol point to this one. */
00cbee0a
L
646 bed = get_elf_backend_data (output_bfd);
647 hv = h;
648 while (hv->root.type == bfd_link_hash_indirect
649 || hv->root.type == bfd_link_hash_warning)
650 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
651 /* We don't need to update h->root.u since linker will set them
652 later. */
653 h->root.type = bfd_link_hash_undefined;
654 hv->root.type = bfd_link_hash_indirect;
655 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
656 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
657 break;
8e2a4f11
AM
658 default:
659 BFD_FAIL ();
c2596ca5 660 return FALSE;
55255dae 661 }
45d6a902
AM
662
663 /* If this symbol is being provided by the linker script, and it is
664 currently defined by a dynamic object, but not by a regular
665 object, then mark it as undefined so that the generic linker will
666 force the correct value. */
667 if (provide
f5385ebf
AM
668 && h->def_dynamic
669 && !h->def_regular)
45d6a902
AM
670 h->root.type = bfd_link_hash_undefined;
671
672 /* If this symbol is not being provided by the linker script, and it is
673 currently defined by a dynamic object, but not by a regular object,
674 then clear out any version information because the symbol will not be
675 associated with the dynamic object any more. */
676 if (!provide
f5385ebf
AM
677 && h->def_dynamic
678 && !h->def_regular)
45d6a902
AM
679 h->verinfo.verdef = NULL;
680
f5385ebf 681 h->def_regular = 1;
45d6a902 682
eb8476a6 683 if (hidden)
fe21a8fc 684 {
91d6fa6a 685 bed = get_elf_backend_data (output_bfd);
b8297068
AM
686 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
687 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
688 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
689 }
690
6fa3860b
PB
691 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
692 and executables. */
0e1862bb 693 if (!bfd_link_relocatable (info)
6fa3860b
PB
694 && h->dynindx != -1
695 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
696 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
697 h->forced_local = 1;
698
f5385ebf
AM
699 if ((h->def_dynamic
700 || h->ref_dynamic
6b3b0ab8
L
701 || bfd_link_dll (info)
702 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
703 && h->dynindx == -1)
704 {
c152c796 705 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
706 return FALSE;
707
708 /* If this is a weak defined symbol, and we know a corresponding
709 real symbol from the same dynamic object, make sure the real
710 symbol is also made into a dynamic symbol. */
f6e332e6
AM
711 if (h->u.weakdef != NULL
712 && h->u.weakdef->dynindx == -1)
45d6a902 713 {
f6e332e6 714 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
715 return FALSE;
716 }
717 }
718
719 return TRUE;
720}
42751cf3 721
8c58d23b
AM
722/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
723 success, and 2 on a failure caused by attempting to record a symbol
724 in a discarded section, eg. a discarded link-once section symbol. */
725
726int
c152c796
AM
727bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
728 bfd *input_bfd,
729 long input_indx)
8c58d23b
AM
730{
731 bfd_size_type amt;
732 struct elf_link_local_dynamic_entry *entry;
733 struct elf_link_hash_table *eht;
734 struct elf_strtab_hash *dynstr;
ef53be89 735 size_t dynstr_index;
8c58d23b
AM
736 char *name;
737 Elf_External_Sym_Shndx eshndx;
738 char esym[sizeof (Elf64_External_Sym)];
739
0eddce27 740 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
741 return 0;
742
743 /* See if the entry exists already. */
744 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
745 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
746 return 1;
747
748 amt = sizeof (*entry);
a50b1753 749 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
750 if (entry == NULL)
751 return 0;
752
753 /* Go find the symbol, so that we can find it's name. */
754 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 755 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
756 {
757 bfd_release (input_bfd, entry);
758 return 0;
759 }
760
761 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 762 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
763 {
764 asection *s;
765
766 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
767 if (s == NULL || bfd_is_abs_section (s->output_section))
768 {
769 /* We can still bfd_release here as nothing has done another
770 bfd_alloc. We can't do this later in this function. */
771 bfd_release (input_bfd, entry);
772 return 2;
773 }
774 }
775
776 name = (bfd_elf_string_from_elf_section
777 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
778 entry->isym.st_name));
779
780 dynstr = elf_hash_table (info)->dynstr;
781 if (dynstr == NULL)
782 {
783 /* Create a strtab to hold the dynamic symbol names. */
784 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
785 if (dynstr == NULL)
786 return 0;
787 }
788
b34976b6 789 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 790 if (dynstr_index == (size_t) -1)
8c58d23b
AM
791 return 0;
792 entry->isym.st_name = dynstr_index;
793
794 eht = elf_hash_table (info);
795
796 entry->next = eht->dynlocal;
797 eht->dynlocal = entry;
798 entry->input_bfd = input_bfd;
799 entry->input_indx = input_indx;
800 eht->dynsymcount++;
801
802 /* Whatever binding the symbol had before, it's now local. */
803 entry->isym.st_info
804 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
805
806 /* The dynindx will be set at the end of size_dynamic_sections. */
807
808 return 1;
809}
810
30b30c21 811/* Return the dynindex of a local dynamic symbol. */
42751cf3 812
30b30c21 813long
268b6b39
AM
814_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
815 bfd *input_bfd,
816 long input_indx)
30b30c21
RH
817{
818 struct elf_link_local_dynamic_entry *e;
819
820 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
821 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
822 return e->dynindx;
823 return -1;
824}
825
826/* This function is used to renumber the dynamic symbols, if some of
827 them are removed because they are marked as local. This is called
828 via elf_link_hash_traverse. */
829
b34976b6 830static bfd_boolean
268b6b39
AM
831elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
832 void *data)
42751cf3 833{
a50b1753 834 size_t *count = (size_t *) data;
30b30c21 835
6fa3860b
PB
836 if (h->forced_local)
837 return TRUE;
838
839 if (h->dynindx != -1)
840 h->dynindx = ++(*count);
841
842 return TRUE;
843}
844
845
846/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
847 STB_LOCAL binding. */
848
849static bfd_boolean
850elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 void *data)
852{
a50b1753 853 size_t *count = (size_t *) data;
6fa3860b 854
6fa3860b
PB
855 if (!h->forced_local)
856 return TRUE;
857
42751cf3 858 if (h->dynindx != -1)
30b30c21
RH
859 h->dynindx = ++(*count);
860
b34976b6 861 return TRUE;
42751cf3 862}
30b30c21 863
aee6f5b4
AO
864/* Return true if the dynamic symbol for a given section should be
865 omitted when creating a shared library. */
866bfd_boolean
867_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
868 struct bfd_link_info *info,
869 asection *p)
870{
74541ad4 871 struct elf_link_hash_table *htab;
ca55926c 872 asection *ip;
74541ad4 873
aee6f5b4
AO
874 switch (elf_section_data (p)->this_hdr.sh_type)
875 {
876 case SHT_PROGBITS:
877 case SHT_NOBITS:
878 /* If sh_type is yet undecided, assume it could be
879 SHT_PROGBITS/SHT_NOBITS. */
880 case SHT_NULL:
74541ad4
AM
881 htab = elf_hash_table (info);
882 if (p == htab->tls_sec)
883 return FALSE;
884
885 if (htab->text_index_section != NULL)
886 return p != htab->text_index_section && p != htab->data_index_section;
887
ca55926c 888 return (htab->dynobj != NULL
3d4d4302 889 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 890 && ip->output_section == p);
aee6f5b4
AO
891
892 /* There shouldn't be section relative relocations
893 against any other section. */
894 default:
895 return TRUE;
896 }
897}
898
062e2358 899/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
900 symbol for each output section, which come first. Next come symbols
901 which have been forced to local binding. Then all of the back-end
902 allocated local dynamic syms, followed by the rest of the global
903 symbols. */
30b30c21 904
554220db
AM
905static unsigned long
906_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
907 struct bfd_link_info *info,
908 unsigned long *section_sym_count)
30b30c21
RH
909{
910 unsigned long dynsymcount = 0;
911
0e1862bb
L
912 if (bfd_link_pic (info)
913 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 914 {
aee6f5b4 915 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
916 asection *p;
917 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 918 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
919 && (p->flags & SEC_ALLOC) != 0
920 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
921 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
922 else
923 elf_section_data (p)->dynindx = 0;
30b30c21 924 }
554220db 925 *section_sym_count = dynsymcount;
30b30c21 926
6fa3860b
PB
927 elf_link_hash_traverse (elf_hash_table (info),
928 elf_link_renumber_local_hash_table_dynsyms,
929 &dynsymcount);
930
30b30c21
RH
931 if (elf_hash_table (info)->dynlocal)
932 {
933 struct elf_link_local_dynamic_entry *p;
934 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
935 p->dynindx = ++dynsymcount;
936 }
90ac2420 937 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
938
939 elf_link_hash_traverse (elf_hash_table (info),
940 elf_link_renumber_hash_table_dynsyms,
941 &dynsymcount);
942
d5486c43
L
943 /* There is an unused NULL entry at the head of the table which we
944 must account for in our count even if the table is empty since it
945 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
946 .dynamic section. */
947 dynsymcount++;
30b30c21 948
ccabcbe5
AM
949 elf_hash_table (info)->dynsymcount = dynsymcount;
950 return dynsymcount;
30b30c21 951}
252b5132 952
54ac0771
L
953/* Merge st_other field. */
954
955static void
956elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 957 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 958 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
959{
960 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
961
962 /* If st_other has a processor-specific meaning, specific
cd3416da 963 code might be needed here. */
54ac0771
L
964 if (bed->elf_backend_merge_symbol_attribute)
965 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
966 dynamic);
967
cd3416da 968 if (!dynamic)
54ac0771 969 {
cd3416da
AM
970 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
971 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 972
cd3416da
AM
973 /* Keep the most constraining visibility. Leave the remainder
974 of the st_other field to elf_backend_merge_symbol_attribute. */
975 if (symvis - 1 < hvis - 1)
976 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 977 }
b8417128
AM
978 else if (definition
979 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
980 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 981 h->protected_def = 1;
54ac0771
L
982}
983
4f3fedcf
AM
984/* This function is called when we want to merge a new symbol with an
985 existing symbol. It handles the various cases which arise when we
986 find a definition in a dynamic object, or when there is already a
987 definition in a dynamic object. The new symbol is described by
988 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
989 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
990 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
991 of an old common symbol. We set OVERRIDE if the old symbol is
992 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
993 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
994 to change. By OK to change, we mean that we shouldn't warn if the
995 type or size does change. */
45d6a902 996
8a56bd02 997static bfd_boolean
268b6b39
AM
998_bfd_elf_merge_symbol (bfd *abfd,
999 struct bfd_link_info *info,
1000 const char *name,
1001 Elf_Internal_Sym *sym,
1002 asection **psec,
1003 bfd_vma *pvalue,
4f3fedcf
AM
1004 struct elf_link_hash_entry **sym_hash,
1005 bfd **poldbfd,
37a9e49a 1006 bfd_boolean *pold_weak,
af44c138 1007 unsigned int *pold_alignment,
268b6b39
AM
1008 bfd_boolean *skip,
1009 bfd_boolean *override,
1010 bfd_boolean *type_change_ok,
6e33951e
L
1011 bfd_boolean *size_change_ok,
1012 bfd_boolean *matched)
252b5132 1013{
7479dfd4 1014 asection *sec, *oldsec;
45d6a902 1015 struct elf_link_hash_entry *h;
90c984fc 1016 struct elf_link_hash_entry *hi;
45d6a902
AM
1017 struct elf_link_hash_entry *flip;
1018 int bind;
1019 bfd *oldbfd;
1020 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1021 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1022 const struct elf_backend_data *bed;
6e33951e 1023 char *new_version;
45d6a902
AM
1024
1025 *skip = FALSE;
1026 *override = FALSE;
1027
1028 sec = *psec;
1029 bind = ELF_ST_BIND (sym->st_info);
1030
1031 if (! bfd_is_und_section (sec))
1032 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1033 else
1034 h = ((struct elf_link_hash_entry *)
1035 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1036 if (h == NULL)
1037 return FALSE;
1038 *sym_hash = h;
252b5132 1039
88ba32a0
L
1040 bed = get_elf_backend_data (abfd);
1041
6e33951e 1042 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1043 if (h->versioned != unversioned)
6e33951e 1044 {
422f1182
L
1045 /* Symbol version is unknown or versioned. */
1046 new_version = strrchr (name, ELF_VER_CHR);
1047 if (new_version)
1048 {
1049 if (h->versioned == unknown)
1050 {
1051 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1052 h->versioned = versioned_hidden;
1053 else
1054 h->versioned = versioned;
1055 }
1056 new_version += 1;
1057 if (new_version[0] == '\0')
1058 new_version = NULL;
1059 }
1060 else
1061 h->versioned = unversioned;
6e33951e 1062 }
422f1182
L
1063 else
1064 new_version = NULL;
6e33951e 1065
90c984fc
L
1066 /* For merging, we only care about real symbols. But we need to make
1067 sure that indirect symbol dynamic flags are updated. */
1068 hi = h;
45d6a902
AM
1069 while (h->root.type == bfd_link_hash_indirect
1070 || h->root.type == bfd_link_hash_warning)
1071 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1072
6e33951e
L
1073 if (!*matched)
1074 {
1075 if (hi == h || h->root.type == bfd_link_hash_new)
1076 *matched = TRUE;
1077 else
1078 {
ae7683d2 1079 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1080 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1081 true if the new symbol is only visible to the symbol with
6e33951e 1082 the same symbol version. */
422f1182
L
1083 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1084 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1085 if (!old_hidden && !new_hidden)
1086 /* The new symbol matches the existing symbol if both
1087 aren't hidden. */
1088 *matched = TRUE;
1089 else
1090 {
1091 /* OLD_VERSION is the symbol version of the existing
1092 symbol. */
422f1182
L
1093 char *old_version;
1094
1095 if (h->versioned >= versioned)
1096 old_version = strrchr (h->root.root.string,
1097 ELF_VER_CHR) + 1;
1098 else
1099 old_version = NULL;
6e33951e
L
1100
1101 /* The new symbol matches the existing symbol if they
1102 have the same symbol version. */
1103 *matched = (old_version == new_version
1104 || (old_version != NULL
1105 && new_version != NULL
1106 && strcmp (old_version, new_version) == 0));
1107 }
1108 }
1109 }
1110
934bce08
AM
1111 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1112 existing symbol. */
1113
1114 oldbfd = NULL;
1115 oldsec = NULL;
1116 switch (h->root.type)
1117 {
1118 default:
1119 break;
1120
1121 case bfd_link_hash_undefined:
1122 case bfd_link_hash_undefweak:
1123 oldbfd = h->root.u.undef.abfd;
1124 break;
1125
1126 case bfd_link_hash_defined:
1127 case bfd_link_hash_defweak:
1128 oldbfd = h->root.u.def.section->owner;
1129 oldsec = h->root.u.def.section;
1130 break;
1131
1132 case bfd_link_hash_common:
1133 oldbfd = h->root.u.c.p->section->owner;
1134 oldsec = h->root.u.c.p->section;
1135 if (pold_alignment)
1136 *pold_alignment = h->root.u.c.p->alignment_power;
1137 break;
1138 }
1139 if (poldbfd && *poldbfd == NULL)
1140 *poldbfd = oldbfd;
1141
1142 /* Differentiate strong and weak symbols. */
1143 newweak = bind == STB_WEAK;
1144 oldweak = (h->root.type == bfd_link_hash_defweak
1145 || h->root.type == bfd_link_hash_undefweak);
1146 if (pold_weak)
1147 *pold_weak = oldweak;
1148
1149 /* This code is for coping with dynamic objects, and is only useful
1150 if we are doing an ELF link. */
1151 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1152 return TRUE;
1153
40b36307 1154 /* We have to check it for every instance since the first few may be
ee659f1f 1155 references and not all compilers emit symbol type for undefined
40b36307
L
1156 symbols. */
1157 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1158
ee659f1f
AM
1159 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1160 respectively, is from a dynamic object. */
1161
1162 newdyn = (abfd->flags & DYNAMIC) != 0;
1163
1164 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1165 syms and defined syms in dynamic libraries respectively.
1166 ref_dynamic on the other hand can be set for a symbol defined in
1167 a dynamic library, and def_dynamic may not be set; When the
1168 definition in a dynamic lib is overridden by a definition in the
1169 executable use of the symbol in the dynamic lib becomes a
1170 reference to the executable symbol. */
1171 if (newdyn)
1172 {
1173 if (bfd_is_und_section (sec))
1174 {
1175 if (bind != STB_WEAK)
1176 {
1177 h->ref_dynamic_nonweak = 1;
1178 hi->ref_dynamic_nonweak = 1;
1179 }
1180 }
1181 else
1182 {
6e33951e
L
1183 /* Update the existing symbol only if they match. */
1184 if (*matched)
1185 h->dynamic_def = 1;
ee659f1f
AM
1186 hi->dynamic_def = 1;
1187 }
1188 }
1189
45d6a902
AM
1190 /* If we just created the symbol, mark it as being an ELF symbol.
1191 Other than that, there is nothing to do--there is no merge issue
1192 with a newly defined symbol--so we just return. */
1193
1194 if (h->root.type == bfd_link_hash_new)
252b5132 1195 {
f5385ebf 1196 h->non_elf = 0;
45d6a902
AM
1197 return TRUE;
1198 }
252b5132 1199
45d6a902
AM
1200 /* In cases involving weak versioned symbols, we may wind up trying
1201 to merge a symbol with itself. Catch that here, to avoid the
1202 confusion that results if we try to override a symbol with
1203 itself. The additional tests catch cases like
1204 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1205 dynamic object, which we do want to handle here. */
1206 if (abfd == oldbfd
895fa45f 1207 && (newweak || oldweak)
45d6a902 1208 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1209 || !h->def_regular))
45d6a902
AM
1210 return TRUE;
1211
707bba77 1212 olddyn = FALSE;
45d6a902
AM
1213 if (oldbfd != NULL)
1214 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1215 else if (oldsec != NULL)
45d6a902 1216 {
707bba77 1217 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1218 indices used by MIPS ELF. */
707bba77 1219 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1220 }
252b5132 1221
45d6a902
AM
1222 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1223 respectively, appear to be a definition rather than reference. */
1224
707bba77 1225 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1226
707bba77
AM
1227 olddef = (h->root.type != bfd_link_hash_undefined
1228 && h->root.type != bfd_link_hash_undefweak
202ac193 1229 && h->root.type != bfd_link_hash_common);
45d6a902 1230
0a36a439
L
1231 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1232 respectively, appear to be a function. */
1233
1234 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1235 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1236
1237 oldfunc = (h->type != STT_NOTYPE
1238 && bed->is_function_type (h->type));
1239
5b677558
AM
1240 /* If creating a default indirect symbol ("foo" or "foo@") from a
1241 dynamic versioned definition ("foo@@") skip doing so if there is
1242 an existing regular definition with a different type. We don't
1243 want, for example, a "time" variable in the executable overriding
1244 a "time" function in a shared library. */
580a2b6e 1245 if (pold_alignment == NULL
580a2b6e
L
1246 && newdyn
1247 && newdef
1248 && !olddyn
5b677558
AM
1249 && (olddef || h->root.type == bfd_link_hash_common)
1250 && ELF_ST_TYPE (sym->st_info) != h->type
1251 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1252 && h->type != STT_NOTYPE
1253 && !(newfunc && oldfunc))
580a2b6e
L
1254 {
1255 *skip = TRUE;
1256 return TRUE;
1257 }
1258
4c34aff8
AM
1259 /* Check TLS symbols. We don't check undefined symbols introduced
1260 by "ld -u" which have no type (and oldbfd NULL), and we don't
1261 check symbols from plugins because they also have no type. */
1262 if (oldbfd != NULL
1263 && (oldbfd->flags & BFD_PLUGIN) == 0
1264 && (abfd->flags & BFD_PLUGIN) == 0
1265 && ELF_ST_TYPE (sym->st_info) != h->type
1266 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1267 {
1268 bfd *ntbfd, *tbfd;
1269 bfd_boolean ntdef, tdef;
1270 asection *ntsec, *tsec;
1271
1272 if (h->type == STT_TLS)
1273 {
3b36f7e6 1274 ntbfd = abfd;
7479dfd4
L
1275 ntsec = sec;
1276 ntdef = newdef;
1277 tbfd = oldbfd;
1278 tsec = oldsec;
1279 tdef = olddef;
1280 }
1281 else
1282 {
1283 ntbfd = oldbfd;
1284 ntsec = oldsec;
1285 ntdef = olddef;
1286 tbfd = abfd;
1287 tsec = sec;
1288 tdef = newdef;
1289 }
1290
1291 if (tdef && ntdef)
4eca0228 1292 _bfd_error_handler
695344c0 1293 /* xgettext:c-format */
191c0c42
AM
1294 (_("%s: TLS definition in %B section %A "
1295 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1296 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1297 else if (!tdef && !ntdef)
4eca0228 1298 _bfd_error_handler
695344c0 1299 /* xgettext:c-format */
191c0c42
AM
1300 (_("%s: TLS reference in %B "
1301 "mismatches non-TLS reference in %B"),
7479dfd4
L
1302 tbfd, ntbfd, h->root.root.string);
1303 else if (tdef)
4eca0228 1304 _bfd_error_handler
695344c0 1305 /* xgettext:c-format */
191c0c42
AM
1306 (_("%s: TLS definition in %B section %A "
1307 "mismatches non-TLS reference in %B"),
7479dfd4
L
1308 tbfd, tsec, ntbfd, h->root.root.string);
1309 else
4eca0228 1310 _bfd_error_handler
695344c0 1311 /* xgettext:c-format */
191c0c42
AM
1312 (_("%s: TLS reference in %B "
1313 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1314 tbfd, ntbfd, ntsec, h->root.root.string);
1315
1316 bfd_set_error (bfd_error_bad_value);
1317 return FALSE;
1318 }
1319
45d6a902
AM
1320 /* If the old symbol has non-default visibility, we ignore the new
1321 definition from a dynamic object. */
1322 if (newdyn
9c7a29a3 1323 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1324 && !bfd_is_und_section (sec))
1325 {
1326 *skip = TRUE;
1327 /* Make sure this symbol is dynamic. */
f5385ebf 1328 h->ref_dynamic = 1;
90c984fc 1329 hi->ref_dynamic = 1;
45d6a902
AM
1330 /* A protected symbol has external availability. Make sure it is
1331 recorded as dynamic.
1332
1333 FIXME: Should we check type and size for protected symbol? */
1334 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1335 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1336 else
1337 return TRUE;
1338 }
1339 else if (!newdyn
9c7a29a3 1340 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1341 && h->def_dynamic)
45d6a902
AM
1342 {
1343 /* If the new symbol with non-default visibility comes from a
1344 relocatable file and the old definition comes from a dynamic
1345 object, we remove the old definition. */
6c9b78e6 1346 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1347 {
1348 /* Handle the case where the old dynamic definition is
1349 default versioned. We need to copy the symbol info from
1350 the symbol with default version to the normal one if it
1351 was referenced before. */
1352 if (h->ref_regular)
1353 {
6c9b78e6 1354 hi->root.type = h->root.type;
d2dee3b2 1355 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1356 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1357
6c9b78e6 1358 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1359 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1360 {
aed81c4e
MR
1361 /* If the new symbol is hidden or internal, completely undo
1362 any dynamic link state. */
1363 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1364 h->forced_local = 0;
1365 h->ref_dynamic = 0;
d2dee3b2
L
1366 }
1367 else
aed81c4e
MR
1368 h->ref_dynamic = 1;
1369
1370 h->def_dynamic = 0;
aed81c4e
MR
1371 /* FIXME: Should we check type and size for protected symbol? */
1372 h->size = 0;
1373 h->type = 0;
1374
6c9b78e6 1375 h = hi;
d2dee3b2
L
1376 }
1377 else
6c9b78e6 1378 h = hi;
d2dee3b2 1379 }
1de1a317 1380
f5eda473
AM
1381 /* If the old symbol was undefined before, then it will still be
1382 on the undefs list. If the new symbol is undefined or
1383 common, we can't make it bfd_link_hash_new here, because new
1384 undefined or common symbols will be added to the undefs list
1385 by _bfd_generic_link_add_one_symbol. Symbols may not be
1386 added twice to the undefs list. Also, if the new symbol is
1387 undefweak then we don't want to lose the strong undef. */
1388 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1389 {
1de1a317 1390 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1391 h->root.u.undef.abfd = abfd;
1392 }
1393 else
1394 {
1395 h->root.type = bfd_link_hash_new;
1396 h->root.u.undef.abfd = NULL;
1397 }
1398
f5eda473 1399 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1400 {
f5eda473
AM
1401 /* If the new symbol is hidden or internal, completely undo
1402 any dynamic link state. */
1403 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1404 h->forced_local = 0;
1405 h->ref_dynamic = 0;
45d6a902 1406 }
f5eda473
AM
1407 else
1408 h->ref_dynamic = 1;
1409 h->def_dynamic = 0;
45d6a902
AM
1410 /* FIXME: Should we check type and size for protected symbol? */
1411 h->size = 0;
1412 h->type = 0;
1413 return TRUE;
1414 }
14a793b2 1415
15b43f48
AM
1416 /* If a new weak symbol definition comes from a regular file and the
1417 old symbol comes from a dynamic library, we treat the new one as
1418 strong. Similarly, an old weak symbol definition from a regular
1419 file is treated as strong when the new symbol comes from a dynamic
1420 library. Further, an old weak symbol from a dynamic library is
1421 treated as strong if the new symbol is from a dynamic library.
1422 This reflects the way glibc's ld.so works.
1423
1424 Do this before setting *type_change_ok or *size_change_ok so that
1425 we warn properly when dynamic library symbols are overridden. */
1426
1427 if (newdef && !newdyn && olddyn)
0f8a2703 1428 newweak = FALSE;
15b43f48 1429 if (olddef && newdyn)
0f8a2703
AM
1430 oldweak = FALSE;
1431
d334575b 1432 /* Allow changes between different types of function symbol. */
0a36a439 1433 if (newfunc && oldfunc)
fcb93ecf
PB
1434 *type_change_ok = TRUE;
1435
79349b09
AM
1436 /* It's OK to change the type if either the existing symbol or the
1437 new symbol is weak. A type change is also OK if the old symbol
1438 is undefined and the new symbol is defined. */
252b5132 1439
79349b09
AM
1440 if (oldweak
1441 || newweak
1442 || (newdef
1443 && h->root.type == bfd_link_hash_undefined))
1444 *type_change_ok = TRUE;
1445
1446 /* It's OK to change the size if either the existing symbol or the
1447 new symbol is weak, or if the old symbol is undefined. */
1448
1449 if (*type_change_ok
1450 || h->root.type == bfd_link_hash_undefined)
1451 *size_change_ok = TRUE;
45d6a902 1452
45d6a902
AM
1453 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1454 symbol, respectively, appears to be a common symbol in a dynamic
1455 object. If a symbol appears in an uninitialized section, and is
1456 not weak, and is not a function, then it may be a common symbol
1457 which was resolved when the dynamic object was created. We want
1458 to treat such symbols specially, because they raise special
1459 considerations when setting the symbol size: if the symbol
1460 appears as a common symbol in a regular object, and the size in
1461 the regular object is larger, we must make sure that we use the
1462 larger size. This problematic case can always be avoided in C,
1463 but it must be handled correctly when using Fortran shared
1464 libraries.
1465
1466 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1467 likewise for OLDDYNCOMMON and OLDDEF.
1468
1469 Note that this test is just a heuristic, and that it is quite
1470 possible to have an uninitialized symbol in a shared object which
1471 is really a definition, rather than a common symbol. This could
1472 lead to some minor confusion when the symbol really is a common
1473 symbol in some regular object. However, I think it will be
1474 harmless. */
1475
1476 if (newdyn
1477 && newdef
79349b09 1478 && !newweak
45d6a902
AM
1479 && (sec->flags & SEC_ALLOC) != 0
1480 && (sec->flags & SEC_LOAD) == 0
1481 && sym->st_size > 0
0a36a439 1482 && !newfunc)
45d6a902
AM
1483 newdyncommon = TRUE;
1484 else
1485 newdyncommon = FALSE;
1486
1487 if (olddyn
1488 && olddef
1489 && h->root.type == bfd_link_hash_defined
f5385ebf 1490 && h->def_dynamic
45d6a902
AM
1491 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1492 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1493 && h->size > 0
0a36a439 1494 && !oldfunc)
45d6a902
AM
1495 olddyncommon = TRUE;
1496 else
1497 olddyncommon = FALSE;
1498
a4d8e49b
L
1499 /* We now know everything about the old and new symbols. We ask the
1500 backend to check if we can merge them. */
5d13b3b3
AM
1501 if (bed->merge_symbol != NULL)
1502 {
1503 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1504 return FALSE;
1505 sec = *psec;
1506 }
a4d8e49b 1507
45d6a902
AM
1508 /* If both the old and the new symbols look like common symbols in a
1509 dynamic object, set the size of the symbol to the larger of the
1510 two. */
1511
1512 if (olddyncommon
1513 && newdyncommon
1514 && sym->st_size != h->size)
1515 {
1516 /* Since we think we have two common symbols, issue a multiple
1517 common warning if desired. Note that we only warn if the
1518 size is different. If the size is the same, we simply let
1519 the old symbol override the new one as normally happens with
1520 symbols defined in dynamic objects. */
1521
1a72702b
AM
1522 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1523 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1524 if (sym->st_size > h->size)
1525 h->size = sym->st_size;
252b5132 1526
45d6a902 1527 *size_change_ok = TRUE;
252b5132
RH
1528 }
1529
45d6a902
AM
1530 /* If we are looking at a dynamic object, and we have found a
1531 definition, we need to see if the symbol was already defined by
1532 some other object. If so, we want to use the existing
1533 definition, and we do not want to report a multiple symbol
1534 definition error; we do this by clobbering *PSEC to be
1535 bfd_und_section_ptr.
1536
1537 We treat a common symbol as a definition if the symbol in the
1538 shared library is a function, since common symbols always
1539 represent variables; this can cause confusion in principle, but
1540 any such confusion would seem to indicate an erroneous program or
1541 shared library. We also permit a common symbol in a regular
202ac193
L
1542 object to override a weak symbol in a shared object. A common
1543 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1544
1545 if (newdyn
1546 && newdef
77cfaee6 1547 && (olddef
45d6a902 1548 || (h->root.type == bfd_link_hash_common
202ac193
L
1549 && (newweak
1550 || newfunc
1551 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1552 {
1553 *override = TRUE;
1554 newdef = FALSE;
1555 newdyncommon = FALSE;
252b5132 1556
45d6a902
AM
1557 *psec = sec = bfd_und_section_ptr;
1558 *size_change_ok = TRUE;
252b5132 1559
45d6a902
AM
1560 /* If we get here when the old symbol is a common symbol, then
1561 we are explicitly letting it override a weak symbol or
1562 function in a dynamic object, and we don't want to warn about
1563 a type change. If the old symbol is a defined symbol, a type
1564 change warning may still be appropriate. */
252b5132 1565
45d6a902
AM
1566 if (h->root.type == bfd_link_hash_common)
1567 *type_change_ok = TRUE;
1568 }
1569
1570 /* Handle the special case of an old common symbol merging with a
1571 new symbol which looks like a common symbol in a shared object.
1572 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1573 common symbol, and let _bfd_generic_link_add_one_symbol do the
1574 right thing. */
45d6a902
AM
1575
1576 if (newdyncommon
1577 && h->root.type == bfd_link_hash_common)
1578 {
1579 *override = TRUE;
1580 newdef = FALSE;
1581 newdyncommon = FALSE;
1582 *pvalue = sym->st_size;
a4d8e49b 1583 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1584 *size_change_ok = TRUE;
1585 }
1586
c5e2cead 1587 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1588 if (newdef && olddef && newweak)
54ac0771 1589 {
35ed3f94 1590 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1591 if (!(oldbfd != NULL
1592 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1593 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1594 {
1595 newdef = FALSE;
1596 *skip = TRUE;
1597 }
54ac0771
L
1598
1599 /* Merge st_other. If the symbol already has a dynamic index,
1600 but visibility says it should not be visible, turn it into a
1601 local symbol. */
b8417128 1602 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1603 if (h->dynindx != -1)
1604 switch (ELF_ST_VISIBILITY (h->other))
1605 {
1606 case STV_INTERNAL:
1607 case STV_HIDDEN:
1608 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1609 break;
1610 }
1611 }
c5e2cead 1612
45d6a902
AM
1613 /* If the old symbol is from a dynamic object, and the new symbol is
1614 a definition which is not from a dynamic object, then the new
1615 symbol overrides the old symbol. Symbols from regular files
1616 always take precedence over symbols from dynamic objects, even if
1617 they are defined after the dynamic object in the link.
1618
1619 As above, we again permit a common symbol in a regular object to
1620 override a definition in a shared object if the shared object
0f8a2703 1621 symbol is a function or is weak. */
45d6a902
AM
1622
1623 flip = NULL;
77cfaee6 1624 if (!newdyn
45d6a902
AM
1625 && (newdef
1626 || (bfd_is_com_section (sec)
0a36a439 1627 && (oldweak || oldfunc)))
45d6a902
AM
1628 && olddyn
1629 && olddef
f5385ebf 1630 && h->def_dynamic)
45d6a902
AM
1631 {
1632 /* Change the hash table entry to undefined, and let
1633 _bfd_generic_link_add_one_symbol do the right thing with the
1634 new definition. */
1635
1636 h->root.type = bfd_link_hash_undefined;
1637 h->root.u.undef.abfd = h->root.u.def.section->owner;
1638 *size_change_ok = TRUE;
1639
1640 olddef = FALSE;
1641 olddyncommon = FALSE;
1642
1643 /* We again permit a type change when a common symbol may be
1644 overriding a function. */
1645
1646 if (bfd_is_com_section (sec))
0a36a439
L
1647 {
1648 if (oldfunc)
1649 {
1650 /* If a common symbol overrides a function, make sure
1651 that it isn't defined dynamically nor has type
1652 function. */
1653 h->def_dynamic = 0;
1654 h->type = STT_NOTYPE;
1655 }
1656 *type_change_ok = TRUE;
1657 }
45d6a902 1658
6c9b78e6
AM
1659 if (hi->root.type == bfd_link_hash_indirect)
1660 flip = hi;
45d6a902
AM
1661 else
1662 /* This union may have been set to be non-NULL when this symbol
1663 was seen in a dynamic object. We must force the union to be
1664 NULL, so that it is correct for a regular symbol. */
1665 h->verinfo.vertree = NULL;
1666 }
1667
1668 /* Handle the special case of a new common symbol merging with an
1669 old symbol that looks like it might be a common symbol defined in
1670 a shared object. Note that we have already handled the case in
1671 which a new common symbol should simply override the definition
1672 in the shared library. */
1673
1674 if (! newdyn
1675 && bfd_is_com_section (sec)
1676 && olddyncommon)
1677 {
1678 /* It would be best if we could set the hash table entry to a
1679 common symbol, but we don't know what to use for the section
1680 or the alignment. */
1a72702b
AM
1681 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1682 bfd_link_hash_common, sym->st_size);
45d6a902 1683
4cc11e76 1684 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1685 larger, pretend that the new symbol has its size. */
1686
1687 if (h->size > *pvalue)
1688 *pvalue = h->size;
1689
af44c138
L
1690 /* We need to remember the alignment required by the symbol
1691 in the dynamic object. */
1692 BFD_ASSERT (pold_alignment);
1693 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1694
1695 olddef = FALSE;
1696 olddyncommon = FALSE;
1697
1698 h->root.type = bfd_link_hash_undefined;
1699 h->root.u.undef.abfd = h->root.u.def.section->owner;
1700
1701 *size_change_ok = TRUE;
1702 *type_change_ok = TRUE;
1703
6c9b78e6
AM
1704 if (hi->root.type == bfd_link_hash_indirect)
1705 flip = hi;
45d6a902
AM
1706 else
1707 h->verinfo.vertree = NULL;
1708 }
1709
1710 if (flip != NULL)
1711 {
1712 /* Handle the case where we had a versioned symbol in a dynamic
1713 library and now find a definition in a normal object. In this
1714 case, we make the versioned symbol point to the normal one. */
45d6a902 1715 flip->root.type = h->root.type;
00cbee0a 1716 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1717 h->root.type = bfd_link_hash_indirect;
1718 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1719 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1720 if (h->def_dynamic)
45d6a902 1721 {
f5385ebf
AM
1722 h->def_dynamic = 0;
1723 flip->ref_dynamic = 1;
45d6a902
AM
1724 }
1725 }
1726
45d6a902
AM
1727 return TRUE;
1728}
1729
1730/* This function is called to create an indirect symbol from the
1731 default for the symbol with the default version if needed. The
4f3fedcf 1732 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1733 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1734
28caa186 1735static bfd_boolean
268b6b39
AM
1736_bfd_elf_add_default_symbol (bfd *abfd,
1737 struct bfd_link_info *info,
1738 struct elf_link_hash_entry *h,
1739 const char *name,
1740 Elf_Internal_Sym *sym,
4f3fedcf
AM
1741 asection *sec,
1742 bfd_vma value,
1743 bfd **poldbfd,
e3c9d234 1744 bfd_boolean *dynsym)
45d6a902
AM
1745{
1746 bfd_boolean type_change_ok;
1747 bfd_boolean size_change_ok;
1748 bfd_boolean skip;
1749 char *shortname;
1750 struct elf_link_hash_entry *hi;
1751 struct bfd_link_hash_entry *bh;
9c5bfbb7 1752 const struct elf_backend_data *bed;
45d6a902
AM
1753 bfd_boolean collect;
1754 bfd_boolean dynamic;
e3c9d234 1755 bfd_boolean override;
45d6a902
AM
1756 char *p;
1757 size_t len, shortlen;
ffd65175 1758 asection *tmp_sec;
6e33951e 1759 bfd_boolean matched;
45d6a902 1760
422f1182
L
1761 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1762 return TRUE;
1763
45d6a902
AM
1764 /* If this symbol has a version, and it is the default version, we
1765 create an indirect symbol from the default name to the fully
1766 decorated name. This will cause external references which do not
1767 specify a version to be bound to this version of the symbol. */
1768 p = strchr (name, ELF_VER_CHR);
422f1182
L
1769 if (h->versioned == unknown)
1770 {
1771 if (p == NULL)
1772 {
1773 h->versioned = unversioned;
1774 return TRUE;
1775 }
1776 else
1777 {
1778 if (p[1] != ELF_VER_CHR)
1779 {
1780 h->versioned = versioned_hidden;
1781 return TRUE;
1782 }
1783 else
1784 h->versioned = versioned;
1785 }
1786 }
4373f8af
L
1787 else
1788 {
1789 /* PR ld/19073: We may see an unversioned definition after the
1790 default version. */
1791 if (p == NULL)
1792 return TRUE;
1793 }
45d6a902 1794
45d6a902
AM
1795 bed = get_elf_backend_data (abfd);
1796 collect = bed->collect;
1797 dynamic = (abfd->flags & DYNAMIC) != 0;
1798
1799 shortlen = p - name;
a50b1753 1800 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1801 if (shortname == NULL)
1802 return FALSE;
1803 memcpy (shortname, name, shortlen);
1804 shortname[shortlen] = '\0';
1805
1806 /* We are going to create a new symbol. Merge it with any existing
1807 symbol with this name. For the purposes of the merge, act as
1808 though we were defining the symbol we just defined, although we
1809 actually going to define an indirect symbol. */
1810 type_change_ok = FALSE;
1811 size_change_ok = FALSE;
6e33951e 1812 matched = TRUE;
ffd65175
AM
1813 tmp_sec = sec;
1814 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1815 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1816 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1817 return FALSE;
1818
1819 if (skip)
1820 goto nondefault;
1821
5b677558
AM
1822 if (hi->def_regular)
1823 {
1824 /* If the undecorated symbol will have a version added by a
1825 script different to H, then don't indirect to/from the
1826 undecorated symbol. This isn't ideal because we may not yet
1827 have seen symbol versions, if given by a script on the
1828 command line rather than via --version-script. */
1829 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1830 {
1831 bfd_boolean hide;
1832
1833 hi->verinfo.vertree
1834 = bfd_find_version_for_sym (info->version_info,
1835 hi->root.root.string, &hide);
1836 if (hi->verinfo.vertree != NULL && hide)
1837 {
1838 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1839 goto nondefault;
1840 }
1841 }
1842 if (hi->verinfo.vertree != NULL
1843 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1844 goto nondefault;
1845 }
1846
45d6a902
AM
1847 if (! override)
1848 {
c6e8a9a8 1849 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1850 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1851 {
1852 bh = &hi->root;
1853 if (! (_bfd_generic_link_add_one_symbol
1854 (info, abfd, shortname, BSF_INDIRECT,
1855 bfd_ind_section_ptr,
1856 0, name, FALSE, collect, &bh)))
1857 return FALSE;
1858 hi = (struct elf_link_hash_entry *) bh;
1859 }
45d6a902
AM
1860 }
1861 else
1862 {
1863 /* In this case the symbol named SHORTNAME is overriding the
1864 indirect symbol we want to add. We were planning on making
1865 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1866 is the name without a version. NAME is the fully versioned
1867 name, and it is the default version.
1868
1869 Overriding means that we already saw a definition for the
1870 symbol SHORTNAME in a regular object, and it is overriding
1871 the symbol defined in the dynamic object.
1872
1873 When this happens, we actually want to change NAME, the
1874 symbol we just added, to refer to SHORTNAME. This will cause
1875 references to NAME in the shared object to become references
1876 to SHORTNAME in the regular object. This is what we expect
1877 when we override a function in a shared object: that the
1878 references in the shared object will be mapped to the
1879 definition in the regular object. */
1880
1881 while (hi->root.type == bfd_link_hash_indirect
1882 || hi->root.type == bfd_link_hash_warning)
1883 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1884
1885 h->root.type = bfd_link_hash_indirect;
1886 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1887 if (h->def_dynamic)
45d6a902 1888 {
f5385ebf
AM
1889 h->def_dynamic = 0;
1890 hi->ref_dynamic = 1;
1891 if (hi->ref_regular
1892 || hi->def_regular)
45d6a902 1893 {
c152c796 1894 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1895 return FALSE;
1896 }
1897 }
1898
1899 /* Now set HI to H, so that the following code will set the
1900 other fields correctly. */
1901 hi = h;
1902 }
1903
fab4a87f
L
1904 /* Check if HI is a warning symbol. */
1905 if (hi->root.type == bfd_link_hash_warning)
1906 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1907
45d6a902
AM
1908 /* If there is a duplicate definition somewhere, then HI may not
1909 point to an indirect symbol. We will have reported an error to
1910 the user in that case. */
1911
1912 if (hi->root.type == bfd_link_hash_indirect)
1913 {
1914 struct elf_link_hash_entry *ht;
1915
45d6a902 1916 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1917 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1918
68c88cd4
AM
1919 /* A reference to the SHORTNAME symbol from a dynamic library
1920 will be satisfied by the versioned symbol at runtime. In
1921 effect, we have a reference to the versioned symbol. */
1922 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1923 hi->dynamic_def |= ht->dynamic_def;
1924
45d6a902
AM
1925 /* See if the new flags lead us to realize that the symbol must
1926 be dynamic. */
1927 if (! *dynsym)
1928 {
1929 if (! dynamic)
1930 {
0e1862bb 1931 if (! bfd_link_executable (info)
90c984fc 1932 || hi->def_dynamic
f5385ebf 1933 || hi->ref_dynamic)
45d6a902
AM
1934 *dynsym = TRUE;
1935 }
1936 else
1937 {
f5385ebf 1938 if (hi->ref_regular)
45d6a902
AM
1939 *dynsym = TRUE;
1940 }
1941 }
1942 }
1943
1944 /* We also need to define an indirection from the nondefault version
1945 of the symbol. */
1946
1947nondefault:
1948 len = strlen (name);
a50b1753 1949 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1950 if (shortname == NULL)
1951 return FALSE;
1952 memcpy (shortname, name, shortlen);
1953 memcpy (shortname + shortlen, p + 1, len - shortlen);
1954
1955 /* Once again, merge with any existing symbol. */
1956 type_change_ok = FALSE;
1957 size_change_ok = FALSE;
ffd65175
AM
1958 tmp_sec = sec;
1959 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1960 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1961 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1962 return FALSE;
1963
1964 if (skip)
1965 return TRUE;
1966
1967 if (override)
1968 {
1969 /* Here SHORTNAME is a versioned name, so we don't expect to see
1970 the type of override we do in the case above unless it is
4cc11e76 1971 overridden by a versioned definition. */
45d6a902
AM
1972 if (hi->root.type != bfd_link_hash_defined
1973 && hi->root.type != bfd_link_hash_defweak)
4eca0228 1974 _bfd_error_handler
695344c0 1975 /* xgettext:c-format */
d003868e
AM
1976 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1977 abfd, shortname);
45d6a902
AM
1978 }
1979 else
1980 {
1981 bh = &hi->root;
1982 if (! (_bfd_generic_link_add_one_symbol
1983 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1984 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1985 return FALSE;
1986 hi = (struct elf_link_hash_entry *) bh;
1987
1988 /* If there is a duplicate definition somewhere, then HI may not
1989 point to an indirect symbol. We will have reported an error
1990 to the user in that case. */
1991
1992 if (hi->root.type == bfd_link_hash_indirect)
1993 {
fcfa13d2 1994 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1995 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1996 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1997
1998 /* See if the new flags lead us to realize that the symbol
1999 must be dynamic. */
2000 if (! *dynsym)
2001 {
2002 if (! dynamic)
2003 {
0e1862bb 2004 if (! bfd_link_executable (info)
f5385ebf 2005 || hi->ref_dynamic)
45d6a902
AM
2006 *dynsym = TRUE;
2007 }
2008 else
2009 {
f5385ebf 2010 if (hi->ref_regular)
45d6a902
AM
2011 *dynsym = TRUE;
2012 }
2013 }
2014 }
2015 }
2016
2017 return TRUE;
2018}
2019\f
2020/* This routine is used to export all defined symbols into the dynamic
2021 symbol table. It is called via elf_link_hash_traverse. */
2022
28caa186 2023static bfd_boolean
268b6b39 2024_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2025{
a50b1753 2026 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2027
2028 /* Ignore indirect symbols. These are added by the versioning code. */
2029 if (h->root.type == bfd_link_hash_indirect)
2030 return TRUE;
2031
7686d77d
AM
2032 /* Ignore this if we won't export it. */
2033 if (!eif->info->export_dynamic && !h->dynamic)
2034 return TRUE;
45d6a902
AM
2035
2036 if (h->dynindx == -1
fd91d419
L
2037 && (h->def_regular || h->ref_regular)
2038 && ! bfd_hide_sym_by_version (eif->info->version_info,
2039 h->root.root.string))
45d6a902 2040 {
fd91d419 2041 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2042 {
fd91d419
L
2043 eif->failed = TRUE;
2044 return FALSE;
45d6a902
AM
2045 }
2046 }
2047
2048 return TRUE;
2049}
2050\f
2051/* Look through the symbols which are defined in other shared
2052 libraries and referenced here. Update the list of version
2053 dependencies. This will be put into the .gnu.version_r section.
2054 This function is called via elf_link_hash_traverse. */
2055
28caa186 2056static bfd_boolean
268b6b39
AM
2057_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2058 void *data)
45d6a902 2059{
a50b1753 2060 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2061 Elf_Internal_Verneed *t;
2062 Elf_Internal_Vernaux *a;
2063 bfd_size_type amt;
2064
45d6a902
AM
2065 /* We only care about symbols defined in shared objects with version
2066 information. */
f5385ebf
AM
2067 if (!h->def_dynamic
2068 || h->def_regular
45d6a902 2069 || h->dynindx == -1
7b20f099
AM
2070 || h->verinfo.verdef == NULL
2071 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2072 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2073 return TRUE;
2074
2075 /* See if we already know about this version. */
28caa186
AM
2076 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2077 t != NULL;
2078 t = t->vn_nextref)
45d6a902
AM
2079 {
2080 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2081 continue;
2082
2083 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2084 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2085 return TRUE;
2086
2087 break;
2088 }
2089
2090 /* This is a new version. Add it to tree we are building. */
2091
2092 if (t == NULL)
2093 {
2094 amt = sizeof *t;
a50b1753 2095 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2096 if (t == NULL)
2097 {
2098 rinfo->failed = TRUE;
2099 return FALSE;
2100 }
2101
2102 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2103 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2104 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2105 }
2106
2107 amt = sizeof *a;
a50b1753 2108 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2109 if (a == NULL)
2110 {
2111 rinfo->failed = TRUE;
2112 return FALSE;
2113 }
45d6a902
AM
2114
2115 /* Note that we are copying a string pointer here, and testing it
2116 above. If bfd_elf_string_from_elf_section is ever changed to
2117 discard the string data when low in memory, this will have to be
2118 fixed. */
2119 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2120
2121 a->vna_flags = h->verinfo.verdef->vd_flags;
2122 a->vna_nextptr = t->vn_auxptr;
2123
2124 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2125 ++rinfo->vers;
2126
2127 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2128
2129 t->vn_auxptr = a;
2130
2131 return TRUE;
2132}
2133
2134/* Figure out appropriate versions for all the symbols. We may not
2135 have the version number script until we have read all of the input
2136 files, so until that point we don't know which symbols should be
2137 local. This function is called via elf_link_hash_traverse. */
2138
28caa186 2139static bfd_boolean
268b6b39 2140_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2141{
28caa186 2142 struct elf_info_failed *sinfo;
45d6a902 2143 struct bfd_link_info *info;
9c5bfbb7 2144 const struct elf_backend_data *bed;
45d6a902
AM
2145 struct elf_info_failed eif;
2146 char *p;
45d6a902 2147
a50b1753 2148 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2149 info = sinfo->info;
2150
45d6a902
AM
2151 /* Fix the symbol flags. */
2152 eif.failed = FALSE;
2153 eif.info = info;
2154 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2155 {
2156 if (eif.failed)
2157 sinfo->failed = TRUE;
2158 return FALSE;
2159 }
2160
2161 /* We only need version numbers for symbols defined in regular
2162 objects. */
f5385ebf 2163 if (!h->def_regular)
45d6a902
AM
2164 return TRUE;
2165
28caa186 2166 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2167 p = strchr (h->root.root.string, ELF_VER_CHR);
2168 if (p != NULL && h->verinfo.vertree == NULL)
2169 {
2170 struct bfd_elf_version_tree *t;
45d6a902 2171
45d6a902
AM
2172 ++p;
2173 if (*p == ELF_VER_CHR)
6e33951e 2174 ++p;
45d6a902
AM
2175
2176 /* If there is no version string, we can just return out. */
2177 if (*p == '\0')
6e33951e 2178 return TRUE;
45d6a902
AM
2179
2180 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2181 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2182 {
2183 if (strcmp (t->name, p) == 0)
2184 {
2185 size_t len;
2186 char *alc;
2187 struct bfd_elf_version_expr *d;
2188
2189 len = p - h->root.root.string;
a50b1753 2190 alc = (char *) bfd_malloc (len);
45d6a902 2191 if (alc == NULL)
14b1c01e
AM
2192 {
2193 sinfo->failed = TRUE;
2194 return FALSE;
2195 }
45d6a902
AM
2196 memcpy (alc, h->root.root.string, len - 1);
2197 alc[len - 1] = '\0';
2198 if (alc[len - 2] == ELF_VER_CHR)
2199 alc[len - 2] = '\0';
2200
2201 h->verinfo.vertree = t;
2202 t->used = TRUE;
2203 d = NULL;
2204
108ba305
JJ
2205 if (t->globals.list != NULL)
2206 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2207
2208 /* See if there is anything to force this symbol to
2209 local scope. */
108ba305 2210 if (d == NULL && t->locals.list != NULL)
45d6a902 2211 {
108ba305
JJ
2212 d = (*t->match) (&t->locals, NULL, alc);
2213 if (d != NULL
2214 && h->dynindx != -1
108ba305
JJ
2215 && ! info->export_dynamic)
2216 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2217 }
2218
2219 free (alc);
2220 break;
2221 }
2222 }
2223
2224 /* If we are building an application, we need to create a
2225 version node for this version. */
0e1862bb 2226 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2227 {
2228 struct bfd_elf_version_tree **pp;
2229 int version_index;
2230
2231 /* If we aren't going to export this symbol, we don't need
2232 to worry about it. */
2233 if (h->dynindx == -1)
2234 return TRUE;
2235
ef53be89
AM
2236 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2237 sizeof *t);
45d6a902
AM
2238 if (t == NULL)
2239 {
2240 sinfo->failed = TRUE;
2241 return FALSE;
2242 }
2243
45d6a902 2244 t->name = p;
45d6a902
AM
2245 t->name_indx = (unsigned int) -1;
2246 t->used = TRUE;
2247
2248 version_index = 1;
2249 /* Don't count anonymous version tag. */
fd91d419
L
2250 if (sinfo->info->version_info != NULL
2251 && sinfo->info->version_info->vernum == 0)
45d6a902 2252 version_index = 0;
fd91d419
L
2253 for (pp = &sinfo->info->version_info;
2254 *pp != NULL;
2255 pp = &(*pp)->next)
45d6a902
AM
2256 ++version_index;
2257 t->vernum = version_index;
2258
2259 *pp = t;
2260
2261 h->verinfo.vertree = t;
2262 }
2263 else if (t == NULL)
2264 {
2265 /* We could not find the version for a symbol when
2266 generating a shared archive. Return an error. */
4eca0228 2267 _bfd_error_handler
695344c0 2268 /* xgettext:c-format */
c55fe096 2269 (_("%B: version node not found for symbol %s"),
28caa186 2270 info->output_bfd, h->root.root.string);
45d6a902
AM
2271 bfd_set_error (bfd_error_bad_value);
2272 sinfo->failed = TRUE;
2273 return FALSE;
2274 }
45d6a902
AM
2275 }
2276
2277 /* If we don't have a version for this symbol, see if we can find
2278 something. */
fd91d419 2279 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2280 {
1e8fa21e 2281 bfd_boolean hide;
ae5a3597 2282
fd91d419
L
2283 h->verinfo.vertree
2284 = bfd_find_version_for_sym (sinfo->info->version_info,
2285 h->root.root.string, &hide);
1e8fa21e
AM
2286 if (h->verinfo.vertree != NULL && hide)
2287 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2288 }
2289
2290 return TRUE;
2291}
2292\f
45d6a902
AM
2293/* Read and swap the relocs from the section indicated by SHDR. This
2294 may be either a REL or a RELA section. The relocations are
2295 translated into RELA relocations and stored in INTERNAL_RELOCS,
2296 which should have already been allocated to contain enough space.
2297 The EXTERNAL_RELOCS are a buffer where the external form of the
2298 relocations should be stored.
2299
2300 Returns FALSE if something goes wrong. */
2301
2302static bfd_boolean
268b6b39 2303elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2304 asection *sec,
268b6b39
AM
2305 Elf_Internal_Shdr *shdr,
2306 void *external_relocs,
2307 Elf_Internal_Rela *internal_relocs)
45d6a902 2308{
9c5bfbb7 2309 const struct elf_backend_data *bed;
268b6b39 2310 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2311 const bfd_byte *erela;
2312 const bfd_byte *erelaend;
2313 Elf_Internal_Rela *irela;
243ef1e0
L
2314 Elf_Internal_Shdr *symtab_hdr;
2315 size_t nsyms;
45d6a902 2316
45d6a902
AM
2317 /* Position ourselves at the start of the section. */
2318 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2319 return FALSE;
2320
2321 /* Read the relocations. */
2322 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2323 return FALSE;
2324
243ef1e0 2325 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2326 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2327
45d6a902
AM
2328 bed = get_elf_backend_data (abfd);
2329
2330 /* Convert the external relocations to the internal format. */
2331 if (shdr->sh_entsize == bed->s->sizeof_rel)
2332 swap_in = bed->s->swap_reloc_in;
2333 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2334 swap_in = bed->s->swap_reloca_in;
2335 else
2336 {
2337 bfd_set_error (bfd_error_wrong_format);
2338 return FALSE;
2339 }
2340
a50b1753 2341 erela = (const bfd_byte *) external_relocs;
51992aec 2342 erelaend = erela + shdr->sh_size;
45d6a902
AM
2343 irela = internal_relocs;
2344 while (erela < erelaend)
2345 {
243ef1e0
L
2346 bfd_vma r_symndx;
2347
45d6a902 2348 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2349 r_symndx = ELF32_R_SYM (irela->r_info);
2350 if (bed->s->arch_size == 64)
2351 r_symndx >>= 24;
ce98a316
NC
2352 if (nsyms > 0)
2353 {
2354 if ((size_t) r_symndx >= nsyms)
2355 {
4eca0228 2356 _bfd_error_handler
695344c0 2357 /* xgettext:c-format */
ce98a316
NC
2358 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2359 " for offset 0x%lx in section `%A'"),
2360 abfd, sec,
2361 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2362 bfd_set_error (bfd_error_bad_value);
2363 return FALSE;
2364 }
2365 }
cf35638d 2366 else if (r_symndx != STN_UNDEF)
243ef1e0 2367 {
4eca0228 2368 _bfd_error_handler
695344c0 2369 /* xgettext:c-format */
ce98a316
NC
2370 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2371 " when the object file has no symbol table"),
d003868e
AM
2372 abfd, sec,
2373 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2374 bfd_set_error (bfd_error_bad_value);
2375 return FALSE;
2376 }
45d6a902
AM
2377 irela += bed->s->int_rels_per_ext_rel;
2378 erela += shdr->sh_entsize;
2379 }
2380
2381 return TRUE;
2382}
2383
2384/* Read and swap the relocs for a section O. They may have been
2385 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2386 not NULL, they are used as buffers to read into. They are known to
2387 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2388 the return value is allocated using either malloc or bfd_alloc,
2389 according to the KEEP_MEMORY argument. If O has two relocation
2390 sections (both REL and RELA relocations), then the REL_HDR
2391 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2392 RELA_HDR relocations. */
45d6a902
AM
2393
2394Elf_Internal_Rela *
268b6b39
AM
2395_bfd_elf_link_read_relocs (bfd *abfd,
2396 asection *o,
2397 void *external_relocs,
2398 Elf_Internal_Rela *internal_relocs,
2399 bfd_boolean keep_memory)
45d6a902 2400{
268b6b39 2401 void *alloc1 = NULL;
45d6a902 2402 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2403 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2404 struct bfd_elf_section_data *esdo = elf_section_data (o);
2405 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2406
d4730f92
BS
2407 if (esdo->relocs != NULL)
2408 return esdo->relocs;
45d6a902
AM
2409
2410 if (o->reloc_count == 0)
2411 return NULL;
2412
45d6a902
AM
2413 if (internal_relocs == NULL)
2414 {
2415 bfd_size_type size;
2416
2417 size = o->reloc_count;
2418 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2419 if (keep_memory)
a50b1753 2420 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2421 else
a50b1753 2422 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2423 if (internal_relocs == NULL)
2424 goto error_return;
2425 }
2426
2427 if (external_relocs == NULL)
2428 {
d4730f92
BS
2429 bfd_size_type size = 0;
2430
2431 if (esdo->rel.hdr)
2432 size += esdo->rel.hdr->sh_size;
2433 if (esdo->rela.hdr)
2434 size += esdo->rela.hdr->sh_size;
45d6a902 2435
268b6b39 2436 alloc1 = bfd_malloc (size);
45d6a902
AM
2437 if (alloc1 == NULL)
2438 goto error_return;
2439 external_relocs = alloc1;
2440 }
2441
d4730f92
BS
2442 internal_rela_relocs = internal_relocs;
2443 if (esdo->rel.hdr)
2444 {
2445 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2446 external_relocs,
2447 internal_relocs))
2448 goto error_return;
2449 external_relocs = (((bfd_byte *) external_relocs)
2450 + esdo->rel.hdr->sh_size);
2451 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2452 * bed->s->int_rels_per_ext_rel);
2453 }
2454
2455 if (esdo->rela.hdr
2456 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2457 external_relocs,
2458 internal_rela_relocs)))
45d6a902
AM
2459 goto error_return;
2460
2461 /* Cache the results for next time, if we can. */
2462 if (keep_memory)
d4730f92 2463 esdo->relocs = internal_relocs;
45d6a902
AM
2464
2465 if (alloc1 != NULL)
2466 free (alloc1);
2467
2468 /* Don't free alloc2, since if it was allocated we are passing it
2469 back (under the name of internal_relocs). */
2470
2471 return internal_relocs;
2472
2473 error_return:
2474 if (alloc1 != NULL)
2475 free (alloc1);
2476 if (alloc2 != NULL)
4dd07732
AM
2477 {
2478 if (keep_memory)
2479 bfd_release (abfd, alloc2);
2480 else
2481 free (alloc2);
2482 }
45d6a902
AM
2483 return NULL;
2484}
2485
2486/* Compute the size of, and allocate space for, REL_HDR which is the
2487 section header for a section containing relocations for O. */
2488
28caa186 2489static bfd_boolean
9eaff861
AO
2490_bfd_elf_link_size_reloc_section (bfd *abfd,
2491 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2492{
9eaff861 2493 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2494
2495 /* That allows us to calculate the size of the section. */
9eaff861 2496 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2497
2498 /* The contents field must last into write_object_contents, so we
2499 allocate it with bfd_alloc rather than malloc. Also since we
2500 cannot be sure that the contents will actually be filled in,
2501 we zero the allocated space. */
a50b1753 2502 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2503 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2504 return FALSE;
2505
d4730f92 2506 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2507 {
2508 struct elf_link_hash_entry **p;
2509
ca4be51c
AM
2510 p = ((struct elf_link_hash_entry **)
2511 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2512 if (p == NULL)
2513 return FALSE;
2514
d4730f92 2515 reldata->hashes = p;
45d6a902
AM
2516 }
2517
2518 return TRUE;
2519}
2520
2521/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2522 originated from the section given by INPUT_REL_HDR) to the
2523 OUTPUT_BFD. */
2524
2525bfd_boolean
268b6b39
AM
2526_bfd_elf_link_output_relocs (bfd *output_bfd,
2527 asection *input_section,
2528 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2529 Elf_Internal_Rela *internal_relocs,
2530 struct elf_link_hash_entry **rel_hash
2531 ATTRIBUTE_UNUSED)
45d6a902
AM
2532{
2533 Elf_Internal_Rela *irela;
2534 Elf_Internal_Rela *irelaend;
2535 bfd_byte *erel;
d4730f92 2536 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2537 asection *output_section;
9c5bfbb7 2538 const struct elf_backend_data *bed;
268b6b39 2539 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2540 struct bfd_elf_section_data *esdo;
45d6a902
AM
2541
2542 output_section = input_section->output_section;
45d6a902 2543
d4730f92
BS
2544 bed = get_elf_backend_data (output_bfd);
2545 esdo = elf_section_data (output_section);
2546 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2547 {
d4730f92
BS
2548 output_reldata = &esdo->rel;
2549 swap_out = bed->s->swap_reloc_out;
45d6a902 2550 }
d4730f92
BS
2551 else if (esdo->rela.hdr
2552 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2553 {
d4730f92
BS
2554 output_reldata = &esdo->rela;
2555 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2556 }
2557 else
2558 {
4eca0228 2559 _bfd_error_handler
695344c0 2560 /* xgettext:c-format */
d003868e
AM
2561 (_("%B: relocation size mismatch in %B section %A"),
2562 output_bfd, input_section->owner, input_section);
297d8443 2563 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2564 return FALSE;
2565 }
2566
d4730f92
BS
2567 erel = output_reldata->hdr->contents;
2568 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2569 irela = internal_relocs;
2570 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2571 * bed->s->int_rels_per_ext_rel);
2572 while (irela < irelaend)
2573 {
2574 (*swap_out) (output_bfd, irela, erel);
2575 irela += bed->s->int_rels_per_ext_rel;
2576 erel += input_rel_hdr->sh_entsize;
2577 }
2578
2579 /* Bump the counter, so that we know where to add the next set of
2580 relocations. */
d4730f92 2581 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2582
2583 return TRUE;
2584}
2585\f
508c3946
L
2586/* Make weak undefined symbols in PIE dynamic. */
2587
2588bfd_boolean
2589_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2590 struct elf_link_hash_entry *h)
2591{
0e1862bb 2592 if (bfd_link_pie (info)
508c3946
L
2593 && h->dynindx == -1
2594 && h->root.type == bfd_link_hash_undefweak)
2595 return bfd_elf_link_record_dynamic_symbol (info, h);
2596
2597 return TRUE;
2598}
2599
45d6a902
AM
2600/* Fix up the flags for a symbol. This handles various cases which
2601 can only be fixed after all the input files are seen. This is
2602 currently called by both adjust_dynamic_symbol and
2603 assign_sym_version, which is unnecessary but perhaps more robust in
2604 the face of future changes. */
2605
28caa186 2606static bfd_boolean
268b6b39
AM
2607_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2608 struct elf_info_failed *eif)
45d6a902 2609{
33774f08 2610 const struct elf_backend_data *bed;
508c3946 2611
45d6a902
AM
2612 /* If this symbol was mentioned in a non-ELF file, try to set
2613 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2614 permit a non-ELF file to correctly refer to a symbol defined in
2615 an ELF dynamic object. */
f5385ebf 2616 if (h->non_elf)
45d6a902
AM
2617 {
2618 while (h->root.type == bfd_link_hash_indirect)
2619 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2620
2621 if (h->root.type != bfd_link_hash_defined
2622 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2623 {
2624 h->ref_regular = 1;
2625 h->ref_regular_nonweak = 1;
2626 }
45d6a902
AM
2627 else
2628 {
2629 if (h->root.u.def.section->owner != NULL
2630 && (bfd_get_flavour (h->root.u.def.section->owner)
2631 == bfd_target_elf_flavour))
f5385ebf
AM
2632 {
2633 h->ref_regular = 1;
2634 h->ref_regular_nonweak = 1;
2635 }
45d6a902 2636 else
f5385ebf 2637 h->def_regular = 1;
45d6a902
AM
2638 }
2639
2640 if (h->dynindx == -1
f5385ebf
AM
2641 && (h->def_dynamic
2642 || h->ref_dynamic))
45d6a902 2643 {
c152c796 2644 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2645 {
2646 eif->failed = TRUE;
2647 return FALSE;
2648 }
2649 }
2650 }
2651 else
2652 {
f5385ebf 2653 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2654 was first seen in a non-ELF file. Fortunately, if the symbol
2655 was first seen in an ELF file, we're probably OK unless the
2656 symbol was defined in a non-ELF file. Catch that case here.
2657 FIXME: We're still in trouble if the symbol was first seen in
2658 a dynamic object, and then later in a non-ELF regular object. */
2659 if ((h->root.type == bfd_link_hash_defined
2660 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2661 && !h->def_regular
45d6a902
AM
2662 && (h->root.u.def.section->owner != NULL
2663 ? (bfd_get_flavour (h->root.u.def.section->owner)
2664 != bfd_target_elf_flavour)
2665 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2666 && !h->def_dynamic)))
2667 h->def_regular = 1;
45d6a902
AM
2668 }
2669
508c3946 2670 /* Backend specific symbol fixup. */
33774f08
AM
2671 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2672 if (bed->elf_backend_fixup_symbol
2673 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2674 return FALSE;
508c3946 2675
45d6a902
AM
2676 /* If this is a final link, and the symbol was defined as a common
2677 symbol in a regular object file, and there was no definition in
2678 any dynamic object, then the linker will have allocated space for
f5385ebf 2679 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2680 flag will not have been set. */
2681 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2682 && !h->def_regular
2683 && h->ref_regular
2684 && !h->def_dynamic
96f29d96 2685 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2686 h->def_regular = 1;
45d6a902 2687
4deb8f71
L
2688 /* If a weak undefined symbol has non-default visibility, we also
2689 hide it from the dynamic linker. */
2690 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2691 && h->root.type == bfd_link_hash_undefweak)
2692 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2693
2694 /* A hidden versioned symbol in executable should be forced local if
2695 it is is locally defined, not referenced by shared library and not
2696 exported. */
2697 else if (bfd_link_executable (eif->info)
2698 && h->versioned == versioned_hidden
2699 && !eif->info->export_dynamic
2700 && !h->dynamic
2701 && !h->ref_dynamic
2702 && h->def_regular)
2703 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2704
45d6a902
AM
2705 /* If -Bsymbolic was used (which means to bind references to global
2706 symbols to the definition within the shared object), and this
2707 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2708 need a PLT entry. Likewise, if the symbol has non-default
2709 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2710 will force it local. */
4deb8f71
L
2711 else if (h->needs_plt
2712 && bfd_link_pic (eif->info)
2713 && is_elf_hash_table (eif->info->hash)
2714 && (SYMBOLIC_BIND (eif->info, h)
2715 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2716 && h->def_regular)
45d6a902 2717 {
45d6a902
AM
2718 bfd_boolean force_local;
2719
45d6a902
AM
2720 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2721 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2722 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2723 }
2724
45d6a902
AM
2725 /* If this is a weak defined symbol in a dynamic object, and we know
2726 the real definition in the dynamic object, copy interesting flags
2727 over to the real definition. */
f6e332e6 2728 if (h->u.weakdef != NULL)
45d6a902 2729 {
45d6a902
AM
2730 /* If the real definition is defined by a regular object file,
2731 don't do anything special. See the longer description in
2732 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2733 if (h->u.weakdef->def_regular)
f6e332e6 2734 h->u.weakdef = NULL;
45d6a902 2735 else
a26587ba 2736 {
4e6b54a6
AM
2737 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2738
2739 while (h->root.type == bfd_link_hash_indirect)
2740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2741
2742 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2743 || h->root.type == bfd_link_hash_defweak);
2744 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2745 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2746 || weakdef->root.type == bfd_link_hash_defweak);
2747 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2748 }
45d6a902
AM
2749 }
2750
2751 return TRUE;
2752}
2753
2754/* Make the backend pick a good value for a dynamic symbol. This is
2755 called via elf_link_hash_traverse, and also calls itself
2756 recursively. */
2757
28caa186 2758static bfd_boolean
268b6b39 2759_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2760{
a50b1753 2761 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2762 bfd *dynobj;
9c5bfbb7 2763 const struct elf_backend_data *bed;
45d6a902 2764
0eddce27 2765 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2766 return FALSE;
2767
45d6a902
AM
2768 /* Ignore indirect symbols. These are added by the versioning code. */
2769 if (h->root.type == bfd_link_hash_indirect)
2770 return TRUE;
2771
2772 /* Fix the symbol flags. */
2773 if (! _bfd_elf_fix_symbol_flags (h, eif))
2774 return FALSE;
2775
2776 /* If this symbol does not require a PLT entry, and it is not
2777 defined by a dynamic object, or is not referenced by a regular
2778 object, ignore it. We do have to handle a weak defined symbol,
2779 even if no regular object refers to it, if we decided to add it
2780 to the dynamic symbol table. FIXME: Do we normally need to worry
2781 about symbols which are defined by one dynamic object and
2782 referenced by another one? */
f5385ebf 2783 if (!h->needs_plt
91e21fb7 2784 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2785 && (h->def_regular
2786 || !h->def_dynamic
2787 || (!h->ref_regular
f6e332e6 2788 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2789 {
a6aa5195 2790 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2791 return TRUE;
2792 }
2793
2794 /* If we've already adjusted this symbol, don't do it again. This
2795 can happen via a recursive call. */
f5385ebf 2796 if (h->dynamic_adjusted)
45d6a902
AM
2797 return TRUE;
2798
2799 /* Don't look at this symbol again. Note that we must set this
2800 after checking the above conditions, because we may look at a
2801 symbol once, decide not to do anything, and then get called
2802 recursively later after REF_REGULAR is set below. */
f5385ebf 2803 h->dynamic_adjusted = 1;
45d6a902
AM
2804
2805 /* If this is a weak definition, and we know a real definition, and
2806 the real symbol is not itself defined by a regular object file,
2807 then get a good value for the real definition. We handle the
2808 real symbol first, for the convenience of the backend routine.
2809
2810 Note that there is a confusing case here. If the real definition
2811 is defined by a regular object file, we don't get the real symbol
2812 from the dynamic object, but we do get the weak symbol. If the
2813 processor backend uses a COPY reloc, then if some routine in the
2814 dynamic object changes the real symbol, we will not see that
2815 change in the corresponding weak symbol. This is the way other
2816 ELF linkers work as well, and seems to be a result of the shared
2817 library model.
2818
2819 I will clarify this issue. Most SVR4 shared libraries define the
2820 variable _timezone and define timezone as a weak synonym. The
2821 tzset call changes _timezone. If you write
2822 extern int timezone;
2823 int _timezone = 5;
2824 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2825 you might expect that, since timezone is a synonym for _timezone,
2826 the same number will print both times. However, if the processor
2827 backend uses a COPY reloc, then actually timezone will be copied
2828 into your process image, and, since you define _timezone
2829 yourself, _timezone will not. Thus timezone and _timezone will
2830 wind up at different memory locations. The tzset call will set
2831 _timezone, leaving timezone unchanged. */
2832
f6e332e6 2833 if (h->u.weakdef != NULL)
45d6a902 2834 {
ec24dc88
AM
2835 /* If we get to this point, there is an implicit reference to
2836 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2837 h->u.weakdef->ref_regular = 1;
45d6a902 2838
ec24dc88
AM
2839 /* Ensure that the backend adjust_dynamic_symbol function sees
2840 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2841 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2842 return FALSE;
2843 }
2844
2845 /* If a symbol has no type and no size and does not require a PLT
2846 entry, then we are probably about to do the wrong thing here: we
2847 are probably going to create a COPY reloc for an empty object.
2848 This case can arise when a shared object is built with assembly
2849 code, and the assembly code fails to set the symbol type. */
2850 if (h->size == 0
2851 && h->type == STT_NOTYPE
f5385ebf 2852 && !h->needs_plt)
4eca0228 2853 _bfd_error_handler
45d6a902
AM
2854 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2855 h->root.root.string);
2856
2857 dynobj = elf_hash_table (eif->info)->dynobj;
2858 bed = get_elf_backend_data (dynobj);
e7c33416 2859
45d6a902
AM
2860 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2861 {
2862 eif->failed = TRUE;
2863 return FALSE;
2864 }
2865
2866 return TRUE;
2867}
2868
027297b7
L
2869/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2870 DYNBSS. */
2871
2872bfd_boolean
6cabe1ea
AM
2873_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2874 struct elf_link_hash_entry *h,
027297b7
L
2875 asection *dynbss)
2876{
91ac5911 2877 unsigned int power_of_two;
027297b7
L
2878 bfd_vma mask;
2879 asection *sec = h->root.u.def.section;
2880
2881 /* The section aligment of definition is the maximum alignment
91ac5911
L
2882 requirement of symbols defined in the section. Since we don't
2883 know the symbol alignment requirement, we start with the
2884 maximum alignment and check low bits of the symbol address
2885 for the minimum alignment. */
2886 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2887 mask = ((bfd_vma) 1 << power_of_two) - 1;
2888 while ((h->root.u.def.value & mask) != 0)
2889 {
2890 mask >>= 1;
2891 --power_of_two;
2892 }
027297b7 2893
91ac5911
L
2894 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2895 dynbss))
027297b7
L
2896 {
2897 /* Adjust the section alignment if needed. */
2898 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2899 power_of_two))
027297b7
L
2900 return FALSE;
2901 }
2902
91ac5911 2903 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2904 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2905
2906 /* Define the symbol as being at this point in DYNBSS. */
2907 h->root.u.def.section = dynbss;
2908 h->root.u.def.value = dynbss->size;
2909
2910 /* Increment the size of DYNBSS to make room for the symbol. */
2911 dynbss->size += h->size;
2912
f7483970
L
2913 /* No error if extern_protected_data is true. */
2914 if (h->protected_def
889c2a67
L
2915 && (!info->extern_protected_data
2916 || (info->extern_protected_data < 0
2917 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2918 info->callbacks->einfo
2919 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2920 h->root.root.string);
6cabe1ea 2921
027297b7
L
2922 return TRUE;
2923}
2924
45d6a902
AM
2925/* Adjust all external symbols pointing into SEC_MERGE sections
2926 to reflect the object merging within the sections. */
2927
28caa186 2928static bfd_boolean
268b6b39 2929_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2930{
2931 asection *sec;
2932
45d6a902
AM
2933 if ((h->root.type == bfd_link_hash_defined
2934 || h->root.type == bfd_link_hash_defweak)
2935 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2936 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2937 {
a50b1753 2938 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2939
2940 h->root.u.def.value =
2941 _bfd_merged_section_offset (output_bfd,
2942 &h->root.u.def.section,
2943 elf_section_data (sec)->sec_info,
753731ee 2944 h->root.u.def.value);
45d6a902
AM
2945 }
2946
2947 return TRUE;
2948}
986a241f
RH
2949
2950/* Returns false if the symbol referred to by H should be considered
2951 to resolve local to the current module, and true if it should be
2952 considered to bind dynamically. */
2953
2954bfd_boolean
268b6b39
AM
2955_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2956 struct bfd_link_info *info,
89a2ee5a 2957 bfd_boolean not_local_protected)
986a241f
RH
2958{
2959 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2960 const struct elf_backend_data *bed;
2961 struct elf_link_hash_table *hash_table;
986a241f
RH
2962
2963 if (h == NULL)
2964 return FALSE;
2965
2966 while (h->root.type == bfd_link_hash_indirect
2967 || h->root.type == bfd_link_hash_warning)
2968 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2969
2970 /* If it was forced local, then clearly it's not dynamic. */
2971 if (h->dynindx == -1)
2972 return FALSE;
f5385ebf 2973 if (h->forced_local)
986a241f
RH
2974 return FALSE;
2975
2976 /* Identify the cases where name binding rules say that a
2977 visible symbol resolves locally. */
0e1862bb
L
2978 binding_stays_local_p = (bfd_link_executable (info)
2979 || SYMBOLIC_BIND (info, h));
986a241f
RH
2980
2981 switch (ELF_ST_VISIBILITY (h->other))
2982 {
2983 case STV_INTERNAL:
2984 case STV_HIDDEN:
2985 return FALSE;
2986
2987 case STV_PROTECTED:
fcb93ecf
PB
2988 hash_table = elf_hash_table (info);
2989 if (!is_elf_hash_table (hash_table))
2990 return FALSE;
2991
2992 bed = get_elf_backend_data (hash_table->dynobj);
2993
986a241f
RH
2994 /* Proper resolution for function pointer equality may require
2995 that these symbols perhaps be resolved dynamically, even though
2996 we should be resolving them to the current module. */
89a2ee5a 2997 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2998 binding_stays_local_p = TRUE;
2999 break;
3000
3001 default:
986a241f
RH
3002 break;
3003 }
3004
aa37626c 3005 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3006 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3007 return TRUE;
3008
986a241f
RH
3009 /* Otherwise, the symbol is dynamic if binding rules don't tell
3010 us that it remains local. */
3011 return !binding_stays_local_p;
3012}
f6c52c13
AM
3013
3014/* Return true if the symbol referred to by H should be considered
3015 to resolve local to the current module, and false otherwise. Differs
3016 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3017 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
3018 for the place where forced_local and dynindx == -1 are tested. If
3019 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3020 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3021 the symbol is local only for defined symbols.
3022 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3023 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3024 treatment of undefined weak symbols. For those that do not make
3025 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3026
3027bfd_boolean
268b6b39
AM
3028_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3029 struct bfd_link_info *info,
3030 bfd_boolean local_protected)
f6c52c13 3031{
fcb93ecf
PB
3032 const struct elf_backend_data *bed;
3033 struct elf_link_hash_table *hash_table;
3034
f6c52c13
AM
3035 /* If it's a local sym, of course we resolve locally. */
3036 if (h == NULL)
3037 return TRUE;
3038
d95edcac
L
3039 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3040 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3041 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3042 return TRUE;
3043
7e2294f9
AO
3044 /* Common symbols that become definitions don't get the DEF_REGULAR
3045 flag set, so test it first, and don't bail out. */
3046 if (ELF_COMMON_DEF_P (h))
3047 /* Do nothing. */;
f6c52c13 3048 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3049 resolve locally. The sym is either undefined or dynamic. */
3050 else if (!h->def_regular)
f6c52c13
AM
3051 return FALSE;
3052
3053 /* Forced local symbols resolve locally. */
f5385ebf 3054 if (h->forced_local)
f6c52c13
AM
3055 return TRUE;
3056
3057 /* As do non-dynamic symbols. */
3058 if (h->dynindx == -1)
3059 return TRUE;
3060
3061 /* At this point, we know the symbol is defined and dynamic. In an
3062 executable it must resolve locally, likewise when building symbolic
3063 shared libraries. */
0e1862bb 3064 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3065 return TRUE;
3066
3067 /* Now deal with defined dynamic symbols in shared libraries. Ones
3068 with default visibility might not resolve locally. */
3069 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3070 return FALSE;
3071
fcb93ecf
PB
3072 hash_table = elf_hash_table (info);
3073 if (!is_elf_hash_table (hash_table))
3074 return TRUE;
3075
3076 bed = get_elf_backend_data (hash_table->dynobj);
3077
f7483970
L
3078 /* If extern_protected_data is false, STV_PROTECTED non-function
3079 symbols are local. */
889c2a67
L
3080 if ((!info->extern_protected_data
3081 || (info->extern_protected_data < 0
3082 && !bed->extern_protected_data))
3083 && !bed->is_function_type (h->type))
1c16dfa5
L
3084 return TRUE;
3085
f6c52c13 3086 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3087 symbols be treated as dynamic symbols. If the address of a
3088 function not defined in an executable is set to that function's
3089 plt entry in the executable, then the address of the function in
3090 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3091 return local_protected;
3092}
e1918d23
AM
3093
3094/* Caches some TLS segment info, and ensures that the TLS segment vma is
3095 aligned. Returns the first TLS output section. */
3096
3097struct bfd_section *
3098_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3099{
3100 struct bfd_section *sec, *tls;
3101 unsigned int align = 0;
3102
3103 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3104 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3105 break;
3106 tls = sec;
3107
3108 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3109 if (sec->alignment_power > align)
3110 align = sec->alignment_power;
3111
3112 elf_hash_table (info)->tls_sec = tls;
3113
3114 /* Ensure the alignment of the first section is the largest alignment,
3115 so that the tls segment starts aligned. */
3116 if (tls != NULL)
3117 tls->alignment_power = align;
3118
3119 return tls;
3120}
0ad989f9
L
3121
3122/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3123static bfd_boolean
3124is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3125 Elf_Internal_Sym *sym)
3126{
a4d8e49b
L
3127 const struct elf_backend_data *bed;
3128
0ad989f9
L
3129 /* Local symbols do not count, but target specific ones might. */
3130 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3131 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3132 return FALSE;
3133
fcb93ecf 3134 bed = get_elf_backend_data (abfd);
0ad989f9 3135 /* Function symbols do not count. */
fcb93ecf 3136 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3137 return FALSE;
3138
3139 /* If the section is undefined, then so is the symbol. */
3140 if (sym->st_shndx == SHN_UNDEF)
3141 return FALSE;
3142
3143 /* If the symbol is defined in the common section, then
3144 it is a common definition and so does not count. */
a4d8e49b 3145 if (bed->common_definition (sym))
0ad989f9
L
3146 return FALSE;
3147
3148 /* If the symbol is in a target specific section then we
3149 must rely upon the backend to tell us what it is. */
3150 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3151 /* FIXME - this function is not coded yet:
3152
3153 return _bfd_is_global_symbol_definition (abfd, sym);
3154
3155 Instead for now assume that the definition is not global,
3156 Even if this is wrong, at least the linker will behave
3157 in the same way that it used to do. */
3158 return FALSE;
3159
3160 return TRUE;
3161}
3162
3163/* Search the symbol table of the archive element of the archive ABFD
3164 whose archive map contains a mention of SYMDEF, and determine if
3165 the symbol is defined in this element. */
3166static bfd_boolean
3167elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3168{
3169 Elf_Internal_Shdr * hdr;
ef53be89
AM
3170 size_t symcount;
3171 size_t extsymcount;
3172 size_t extsymoff;
0ad989f9
L
3173 Elf_Internal_Sym *isymbuf;
3174 Elf_Internal_Sym *isym;
3175 Elf_Internal_Sym *isymend;
3176 bfd_boolean result;
3177
3178 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3179 if (abfd == NULL)
3180 return FALSE;
3181
3182 if (! bfd_check_format (abfd, bfd_object))
3183 return FALSE;
3184
7dc3990e
L
3185 /* Select the appropriate symbol table. If we don't know if the
3186 object file is an IR object, give linker LTO plugin a chance to
3187 get the correct symbol table. */
3188 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3189#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3190 || (abfd->plugin_format == bfd_plugin_unknown
3191 && bfd_link_plugin_object_p (abfd))
3192#endif
3193 )
3194 {
3195 /* Use the IR symbol table if the object has been claimed by
3196 plugin. */
3197 abfd = abfd->plugin_dummy_bfd;
3198 hdr = &elf_tdata (abfd)->symtab_hdr;
3199 }
3200 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3201 hdr = &elf_tdata (abfd)->symtab_hdr;
3202 else
3203 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3204
3205 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3206
3207 /* The sh_info field of the symtab header tells us where the
3208 external symbols start. We don't care about the local symbols. */
3209 if (elf_bad_symtab (abfd))
3210 {
3211 extsymcount = symcount;
3212 extsymoff = 0;
3213 }
3214 else
3215 {
3216 extsymcount = symcount - hdr->sh_info;
3217 extsymoff = hdr->sh_info;
3218 }
3219
3220 if (extsymcount == 0)
3221 return FALSE;
3222
3223 /* Read in the symbol table. */
3224 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3225 NULL, NULL, NULL);
3226 if (isymbuf == NULL)
3227 return FALSE;
3228
3229 /* Scan the symbol table looking for SYMDEF. */
3230 result = FALSE;
3231 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3232 {
3233 const char *name;
3234
3235 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3236 isym->st_name);
3237 if (name == NULL)
3238 break;
3239
3240 if (strcmp (name, symdef->name) == 0)
3241 {
3242 result = is_global_data_symbol_definition (abfd, isym);
3243 break;
3244 }
3245 }
3246
3247 free (isymbuf);
3248
3249 return result;
3250}
3251\f
5a580b3a
AM
3252/* Add an entry to the .dynamic table. */
3253
3254bfd_boolean
3255_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3256 bfd_vma tag,
3257 bfd_vma val)
3258{
3259 struct elf_link_hash_table *hash_table;
3260 const struct elf_backend_data *bed;
3261 asection *s;
3262 bfd_size_type newsize;
3263 bfd_byte *newcontents;
3264 Elf_Internal_Dyn dyn;
3265
3266 hash_table = elf_hash_table (info);
3267 if (! is_elf_hash_table (hash_table))
3268 return FALSE;
3269
3270 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3271 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3272 BFD_ASSERT (s != NULL);
3273
eea6121a 3274 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3275 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3276 if (newcontents == NULL)
3277 return FALSE;
3278
3279 dyn.d_tag = tag;
3280 dyn.d_un.d_val = val;
eea6121a 3281 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3282
eea6121a 3283 s->size = newsize;
5a580b3a
AM
3284 s->contents = newcontents;
3285
3286 return TRUE;
3287}
3288
3289/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3290 otherwise just check whether one already exists. Returns -1 on error,
3291 1 if a DT_NEEDED tag already exists, and 0 on success. */
3292
4ad4eba5 3293static int
7e9f0867
AM
3294elf_add_dt_needed_tag (bfd *abfd,
3295 struct bfd_link_info *info,
4ad4eba5
AM
3296 const char *soname,
3297 bfd_boolean do_it)
5a580b3a
AM
3298{
3299 struct elf_link_hash_table *hash_table;
ef53be89 3300 size_t strindex;
5a580b3a 3301
7e9f0867
AM
3302 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3303 return -1;
3304
5a580b3a 3305 hash_table = elf_hash_table (info);
5a580b3a 3306 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3307 if (strindex == (size_t) -1)
5a580b3a
AM
3308 return -1;
3309
02be4619 3310 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3311 {
3312 asection *sdyn;
3313 const struct elf_backend_data *bed;
3314 bfd_byte *extdyn;
3315
3316 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3317 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3318 if (sdyn != NULL)
3319 for (extdyn = sdyn->contents;
3320 extdyn < sdyn->contents + sdyn->size;
3321 extdyn += bed->s->sizeof_dyn)
3322 {
3323 Elf_Internal_Dyn dyn;
5a580b3a 3324
7e9f0867
AM
3325 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3326 if (dyn.d_tag == DT_NEEDED
3327 && dyn.d_un.d_val == strindex)
3328 {
3329 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3330 return 1;
3331 }
3332 }
5a580b3a
AM
3333 }
3334
3335 if (do_it)
3336 {
7e9f0867
AM
3337 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3338 return -1;
3339
5a580b3a
AM
3340 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3341 return -1;
3342 }
3343 else
3344 /* We were just checking for existence of the tag. */
3345 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3346
3347 return 0;
3348}
3349
7b15fa7a
AM
3350/* Return true if SONAME is on the needed list between NEEDED and STOP
3351 (or the end of list if STOP is NULL), and needed by a library that
3352 will be loaded. */
3353
010e5ae2 3354static bfd_boolean
7b15fa7a
AM
3355on_needed_list (const char *soname,
3356 struct bfd_link_needed_list *needed,
3357 struct bfd_link_needed_list *stop)
010e5ae2 3358{
7b15fa7a
AM
3359 struct bfd_link_needed_list *look;
3360 for (look = needed; look != stop; look = look->next)
3361 if (strcmp (soname, look->name) == 0
3362 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3363 /* If needed by a library that itself is not directly
3364 needed, recursively check whether that library is
3365 indirectly needed. Since we add DT_NEEDED entries to
3366 the end of the list, library dependencies appear after
3367 the library. Therefore search prior to the current
3368 LOOK, preventing possible infinite recursion. */
3369 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3370 return TRUE;
3371
3372 return FALSE;
3373}
3374
14160578 3375/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3376static int
3377elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3378{
3379 const struct elf_link_hash_entry *h1;
3380 const struct elf_link_hash_entry *h2;
10b7e05b 3381 bfd_signed_vma vdiff;
5a580b3a
AM
3382
3383 h1 = *(const struct elf_link_hash_entry **) arg1;
3384 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3385 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3386 if (vdiff != 0)
3387 return vdiff > 0 ? 1 : -1;
3388 else
3389 {
d3435ae8 3390 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3391 if (sdiff != 0)
3392 return sdiff > 0 ? 1 : -1;
3393 }
14160578
AM
3394 vdiff = h1->size - h2->size;
3395 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3396}
4ad4eba5 3397
5a580b3a
AM
3398/* This function is used to adjust offsets into .dynstr for
3399 dynamic symbols. This is called via elf_link_hash_traverse. */
3400
3401static bfd_boolean
3402elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3403{
a50b1753 3404 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3405
5a580b3a
AM
3406 if (h->dynindx != -1)
3407 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3408 return TRUE;
3409}
3410
3411/* Assign string offsets in .dynstr, update all structures referencing
3412 them. */
3413
4ad4eba5
AM
3414static bfd_boolean
3415elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3416{
3417 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3418 struct elf_link_local_dynamic_entry *entry;
3419 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3420 bfd *dynobj = hash_table->dynobj;
3421 asection *sdyn;
3422 bfd_size_type size;
3423 const struct elf_backend_data *bed;
3424 bfd_byte *extdyn;
3425
3426 _bfd_elf_strtab_finalize (dynstr);
3427 size = _bfd_elf_strtab_size (dynstr);
3428
3429 bed = get_elf_backend_data (dynobj);
3d4d4302 3430 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3431 BFD_ASSERT (sdyn != NULL);
3432
3433 /* Update all .dynamic entries referencing .dynstr strings. */
3434 for (extdyn = sdyn->contents;
eea6121a 3435 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3436 extdyn += bed->s->sizeof_dyn)
3437 {
3438 Elf_Internal_Dyn dyn;
3439
3440 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3441 switch (dyn.d_tag)
3442 {
3443 case DT_STRSZ:
3444 dyn.d_un.d_val = size;
3445 break;
3446 case DT_NEEDED:
3447 case DT_SONAME:
3448 case DT_RPATH:
3449 case DT_RUNPATH:
3450 case DT_FILTER:
3451 case DT_AUXILIARY:
7ee314fa
AM
3452 case DT_AUDIT:
3453 case DT_DEPAUDIT:
5a580b3a
AM
3454 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3455 break;
3456 default:
3457 continue;
3458 }
3459 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3460 }
3461
3462 /* Now update local dynamic symbols. */
3463 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3464 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3465 entry->isym.st_name);
3466
3467 /* And the rest of dynamic symbols. */
3468 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3469
3470 /* Adjust version definitions. */
3471 if (elf_tdata (output_bfd)->cverdefs)
3472 {
3473 asection *s;
3474 bfd_byte *p;
ef53be89 3475 size_t i;
5a580b3a
AM
3476 Elf_Internal_Verdef def;
3477 Elf_Internal_Verdaux defaux;
3478
3d4d4302 3479 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3480 p = s->contents;
3481 do
3482 {
3483 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3484 &def);
3485 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3486 if (def.vd_aux != sizeof (Elf_External_Verdef))
3487 continue;
5a580b3a
AM
3488 for (i = 0; i < def.vd_cnt; ++i)
3489 {
3490 _bfd_elf_swap_verdaux_in (output_bfd,
3491 (Elf_External_Verdaux *) p, &defaux);
3492 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3493 defaux.vda_name);
3494 _bfd_elf_swap_verdaux_out (output_bfd,
3495 &defaux, (Elf_External_Verdaux *) p);
3496 p += sizeof (Elf_External_Verdaux);
3497 }
3498 }
3499 while (def.vd_next);
3500 }
3501
3502 /* Adjust version references. */
3503 if (elf_tdata (output_bfd)->verref)
3504 {
3505 asection *s;
3506 bfd_byte *p;
ef53be89 3507 size_t i;
5a580b3a
AM
3508 Elf_Internal_Verneed need;
3509 Elf_Internal_Vernaux needaux;
3510
3d4d4302 3511 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3512 p = s->contents;
3513 do
3514 {
3515 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3516 &need);
3517 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3518 _bfd_elf_swap_verneed_out (output_bfd, &need,
3519 (Elf_External_Verneed *) p);
3520 p += sizeof (Elf_External_Verneed);
3521 for (i = 0; i < need.vn_cnt; ++i)
3522 {
3523 _bfd_elf_swap_vernaux_in (output_bfd,
3524 (Elf_External_Vernaux *) p, &needaux);
3525 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3526 needaux.vna_name);
3527 _bfd_elf_swap_vernaux_out (output_bfd,
3528 &needaux,
3529 (Elf_External_Vernaux *) p);
3530 p += sizeof (Elf_External_Vernaux);
3531 }
3532 }
3533 while (need.vn_next);
3534 }
3535
3536 return TRUE;
3537}
3538\f
13285a1b
AM
3539/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3540 The default is to only match when the INPUT and OUTPUT are exactly
3541 the same target. */
3542
3543bfd_boolean
3544_bfd_elf_default_relocs_compatible (const bfd_target *input,
3545 const bfd_target *output)
3546{
3547 return input == output;
3548}
3549
3550/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3551 This version is used when different targets for the same architecture
3552 are virtually identical. */
3553
3554bfd_boolean
3555_bfd_elf_relocs_compatible (const bfd_target *input,
3556 const bfd_target *output)
3557{
3558 const struct elf_backend_data *obed, *ibed;
3559
3560 if (input == output)
3561 return TRUE;
3562
3563 ibed = xvec_get_elf_backend_data (input);
3564 obed = xvec_get_elf_backend_data (output);
3565
3566 if (ibed->arch != obed->arch)
3567 return FALSE;
3568
3569 /* If both backends are using this function, deem them compatible. */
3570 return ibed->relocs_compatible == obed->relocs_compatible;
3571}
3572
e5034e59
AM
3573/* Make a special call to the linker "notice" function to tell it that
3574 we are about to handle an as-needed lib, or have finished
1b786873 3575 processing the lib. */
e5034e59
AM
3576
3577bfd_boolean
3578_bfd_elf_notice_as_needed (bfd *ibfd,
3579 struct bfd_link_info *info,
3580 enum notice_asneeded_action act)
3581{
46135103 3582 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3583}
3584
d9689752
L
3585/* Check relocations an ELF object file. */
3586
3587bfd_boolean
3588_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3589{
3590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3591 struct elf_link_hash_table *htab = elf_hash_table (info);
3592
3593 /* If this object is the same format as the output object, and it is
3594 not a shared library, then let the backend look through the
3595 relocs.
3596
3597 This is required to build global offset table entries and to
3598 arrange for dynamic relocs. It is not required for the
3599 particular common case of linking non PIC code, even when linking
3600 against shared libraries, but unfortunately there is no way of
3601 knowing whether an object file has been compiled PIC or not.
3602 Looking through the relocs is not particularly time consuming.
3603 The problem is that we must either (1) keep the relocs in memory,
3604 which causes the linker to require additional runtime memory or
3605 (2) read the relocs twice from the input file, which wastes time.
3606 This would be a good case for using mmap.
3607
3608 I have no idea how to handle linking PIC code into a file of a
3609 different format. It probably can't be done. */
3610 if ((abfd->flags & DYNAMIC) == 0
3611 && is_elf_hash_table (htab)
3612 && bed->check_relocs != NULL
3613 && elf_object_id (abfd) == elf_hash_table_id (htab)
3614 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3615 {
3616 asection *o;
3617
3618 for (o = abfd->sections; o != NULL; o = o->next)
3619 {
3620 Elf_Internal_Rela *internal_relocs;
3621 bfd_boolean ok;
3622
5ce03cea 3623 /* Don't check relocations in excluded sections. */
d9689752 3624 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3625 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3626 || o->reloc_count == 0
3627 || ((info->strip == strip_all || info->strip == strip_debugger)
3628 && (o->flags & SEC_DEBUGGING) != 0)
3629 || bfd_is_abs_section (o->output_section))
3630 continue;
3631
3632 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3633 info->keep_memory);
3634 if (internal_relocs == NULL)
3635 return FALSE;
3636
3637 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3638
3639 if (elf_section_data (o)->relocs != internal_relocs)
3640 free (internal_relocs);
3641
3642 if (! ok)
3643 return FALSE;
3644 }
3645 }
3646
3647 return TRUE;
3648}
3649
4ad4eba5
AM
3650/* Add symbols from an ELF object file to the linker hash table. */
3651
3652static bfd_boolean
3653elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3654{
a0c402a5 3655 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3656 Elf_Internal_Shdr *hdr;
ef53be89
AM
3657 size_t symcount;
3658 size_t extsymcount;
3659 size_t extsymoff;
4ad4eba5
AM
3660 struct elf_link_hash_entry **sym_hash;
3661 bfd_boolean dynamic;
3662 Elf_External_Versym *extversym = NULL;
3663 Elf_External_Versym *ever;
3664 struct elf_link_hash_entry *weaks;
3665 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3666 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3667 Elf_Internal_Sym *isymbuf = NULL;
3668 Elf_Internal_Sym *isym;
3669 Elf_Internal_Sym *isymend;
3670 const struct elf_backend_data *bed;
3671 bfd_boolean add_needed;
66eb6687 3672 struct elf_link_hash_table *htab;
4ad4eba5 3673 bfd_size_type amt;
66eb6687 3674 void *alloc_mark = NULL;
4f87808c
AM
3675 struct bfd_hash_entry **old_table = NULL;
3676 unsigned int old_size = 0;
3677 unsigned int old_count = 0;
66eb6687 3678 void *old_tab = NULL;
66eb6687
AM
3679 void *old_ent;
3680 struct bfd_link_hash_entry *old_undefs = NULL;
3681 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3682 void *old_strtab = NULL;
66eb6687 3683 size_t tabsize = 0;
db6a5d5f 3684 asection *s;
29a9f53e 3685 bfd_boolean just_syms;
4ad4eba5 3686
66eb6687 3687 htab = elf_hash_table (info);
4ad4eba5 3688 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3689
3690 if ((abfd->flags & DYNAMIC) == 0)
3691 dynamic = FALSE;
3692 else
3693 {
3694 dynamic = TRUE;
3695
3696 /* You can't use -r against a dynamic object. Also, there's no
3697 hope of using a dynamic object which does not exactly match
3698 the format of the output file. */
0e1862bb 3699 if (bfd_link_relocatable (info)
66eb6687 3700 || !is_elf_hash_table (htab)
f13a99db 3701 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3702 {
0e1862bb 3703 if (bfd_link_relocatable (info))
9a0789ec
NC
3704 bfd_set_error (bfd_error_invalid_operation);
3705 else
3706 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3707 goto error_return;
3708 }
3709 }
3710
a0c402a5
L
3711 ehdr = elf_elfheader (abfd);
3712 if (info->warn_alternate_em
3713 && bed->elf_machine_code != ehdr->e_machine
3714 && ((bed->elf_machine_alt1 != 0
3715 && ehdr->e_machine == bed->elf_machine_alt1)
3716 || (bed->elf_machine_alt2 != 0
3717 && ehdr->e_machine == bed->elf_machine_alt2)))
3718 info->callbacks->einfo
695344c0 3719 /* xgettext:c-format */
a0c402a5
L
3720 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3721 ehdr->e_machine, abfd, bed->elf_machine_code);
3722
4ad4eba5
AM
3723 /* As a GNU extension, any input sections which are named
3724 .gnu.warning.SYMBOL are treated as warning symbols for the given
3725 symbol. This differs from .gnu.warning sections, which generate
3726 warnings when they are included in an output file. */
dd98f8d2 3727 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3728 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3729 {
db6a5d5f 3730 const char *name;
4ad4eba5 3731
db6a5d5f
AM
3732 name = bfd_get_section_name (abfd, s);
3733 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3734 {
db6a5d5f
AM
3735 char *msg;
3736 bfd_size_type sz;
3737
3738 name += sizeof ".gnu.warning." - 1;
3739
3740 /* If this is a shared object, then look up the symbol
3741 in the hash table. If it is there, and it is already
3742 been defined, then we will not be using the entry
3743 from this shared object, so we don't need to warn.
3744 FIXME: If we see the definition in a regular object
3745 later on, we will warn, but we shouldn't. The only
3746 fix is to keep track of what warnings we are supposed
3747 to emit, and then handle them all at the end of the
3748 link. */
3749 if (dynamic)
4ad4eba5 3750 {
db6a5d5f
AM
3751 struct elf_link_hash_entry *h;
3752
3753 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3754
3755 /* FIXME: What about bfd_link_hash_common? */
3756 if (h != NULL
3757 && (h->root.type == bfd_link_hash_defined
3758 || h->root.type == bfd_link_hash_defweak))
3759 continue;
3760 }
4ad4eba5 3761
db6a5d5f
AM
3762 sz = s->size;
3763 msg = (char *) bfd_alloc (abfd, sz + 1);
3764 if (msg == NULL)
3765 goto error_return;
4ad4eba5 3766
db6a5d5f
AM
3767 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3768 goto error_return;
4ad4eba5 3769
db6a5d5f 3770 msg[sz] = '\0';
4ad4eba5 3771
db6a5d5f
AM
3772 if (! (_bfd_generic_link_add_one_symbol
3773 (info, abfd, name, BSF_WARNING, s, 0, msg,
3774 FALSE, bed->collect, NULL)))
3775 goto error_return;
4ad4eba5 3776
0e1862bb 3777 if (bfd_link_executable (info))
db6a5d5f
AM
3778 {
3779 /* Clobber the section size so that the warning does
3780 not get copied into the output file. */
3781 s->size = 0;
11d2f718 3782
db6a5d5f
AM
3783 /* Also set SEC_EXCLUDE, so that symbols defined in
3784 the warning section don't get copied to the output. */
3785 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3786 }
3787 }
3788 }
3789
29a9f53e
L
3790 just_syms = ((s = abfd->sections) != NULL
3791 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3792
4ad4eba5
AM
3793 add_needed = TRUE;
3794 if (! dynamic)
3795 {
3796 /* If we are creating a shared library, create all the dynamic
3797 sections immediately. We need to attach them to something,
3798 so we attach them to this BFD, provided it is the right
bf89386a
L
3799 format and is not from ld --just-symbols. Always create the
3800 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3801 are no input BFD's of the same format as the output, we can't
3802 make a shared library. */
3803 if (!just_syms
bf89386a 3804 && (bfd_link_pic (info)
9c1d7a08
L
3805 || (!bfd_link_relocatable (info)
3806 && (info->export_dynamic || info->dynamic)))
66eb6687 3807 && is_elf_hash_table (htab)
f13a99db 3808 && info->output_bfd->xvec == abfd->xvec
66eb6687 3809 && !htab->dynamic_sections_created)
4ad4eba5
AM
3810 {
3811 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3812 goto error_return;
3813 }
3814 }
66eb6687 3815 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3816 goto error_return;
3817 else
3818 {
4ad4eba5 3819 const char *soname = NULL;
7ee314fa 3820 char *audit = NULL;
4ad4eba5
AM
3821 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3822 int ret;
3823
3824 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3825 ld shouldn't allow it. */
29a9f53e 3826 if (just_syms)
92fd189d 3827 abort ();
4ad4eba5
AM
3828
3829 /* If this dynamic lib was specified on the command line with
3830 --as-needed in effect, then we don't want to add a DT_NEEDED
3831 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3832 in by another lib's DT_NEEDED. When --no-add-needed is used
3833 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3834 any dynamic library in DT_NEEDED tags in the dynamic lib at
3835 all. */
3836 add_needed = (elf_dyn_lib_class (abfd)
3837 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3838 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3839
3840 s = bfd_get_section_by_name (abfd, ".dynamic");
3841 if (s != NULL)
3842 {
3843 bfd_byte *dynbuf;
3844 bfd_byte *extdyn;
cb33740c 3845 unsigned int elfsec;
4ad4eba5
AM
3846 unsigned long shlink;
3847
eea6121a 3848 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3849 {
3850error_free_dyn:
3851 free (dynbuf);
3852 goto error_return;
3853 }
4ad4eba5
AM
3854
3855 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3856 if (elfsec == SHN_BAD)
4ad4eba5
AM
3857 goto error_free_dyn;
3858 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3859
3860 for (extdyn = dynbuf;
eea6121a 3861 extdyn < dynbuf + s->size;
4ad4eba5
AM
3862 extdyn += bed->s->sizeof_dyn)
3863 {
3864 Elf_Internal_Dyn dyn;
3865
3866 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3867 if (dyn.d_tag == DT_SONAME)
3868 {
3869 unsigned int tagv = dyn.d_un.d_val;
3870 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3871 if (soname == NULL)
3872 goto error_free_dyn;
3873 }
3874 if (dyn.d_tag == DT_NEEDED)
3875 {
3876 struct bfd_link_needed_list *n, **pn;
3877 char *fnm, *anm;
3878 unsigned int tagv = dyn.d_un.d_val;
3879
3880 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3881 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3882 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3883 if (n == NULL || fnm == NULL)
3884 goto error_free_dyn;
3885 amt = strlen (fnm) + 1;
a50b1753 3886 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3887 if (anm == NULL)
3888 goto error_free_dyn;
3889 memcpy (anm, fnm, amt);
3890 n->name = anm;
3891 n->by = abfd;
3892 n->next = NULL;
66eb6687 3893 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3894 ;
3895 *pn = n;
3896 }
3897 if (dyn.d_tag == DT_RUNPATH)
3898 {
3899 struct bfd_link_needed_list *n, **pn;
3900 char *fnm, *anm;
3901 unsigned int tagv = dyn.d_un.d_val;
3902
3903 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3904 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3905 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3906 if (n == NULL || fnm == NULL)
3907 goto error_free_dyn;
3908 amt = strlen (fnm) + 1;
a50b1753 3909 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3910 if (anm == NULL)
3911 goto error_free_dyn;
3912 memcpy (anm, fnm, amt);
3913 n->name = anm;
3914 n->by = abfd;
3915 n->next = NULL;
3916 for (pn = & runpath;
3917 *pn != NULL;
3918 pn = &(*pn)->next)
3919 ;
3920 *pn = n;
3921 }
3922 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3923 if (!runpath && dyn.d_tag == DT_RPATH)
3924 {
3925 struct bfd_link_needed_list *n, **pn;
3926 char *fnm, *anm;
3927 unsigned int tagv = dyn.d_un.d_val;
3928
3929 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3930 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3931 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3932 if (n == NULL || fnm == NULL)
3933 goto error_free_dyn;
3934 amt = strlen (fnm) + 1;
a50b1753 3935 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3936 if (anm == NULL)
f8703194 3937 goto error_free_dyn;
4ad4eba5
AM
3938 memcpy (anm, fnm, amt);
3939 n->name = anm;
3940 n->by = abfd;
3941 n->next = NULL;
3942 for (pn = & rpath;
3943 *pn != NULL;
3944 pn = &(*pn)->next)
3945 ;
3946 *pn = n;
3947 }
7ee314fa
AM
3948 if (dyn.d_tag == DT_AUDIT)
3949 {
3950 unsigned int tagv = dyn.d_un.d_val;
3951 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3952 }
4ad4eba5
AM
3953 }
3954
3955 free (dynbuf);
3956 }
3957
3958 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3959 frees all more recently bfd_alloc'd blocks as well. */
3960 if (runpath)
3961 rpath = runpath;
3962
3963 if (rpath)
3964 {
3965 struct bfd_link_needed_list **pn;
66eb6687 3966 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3967 ;
3968 *pn = rpath;
3969 }
3970
3971 /* We do not want to include any of the sections in a dynamic
3972 object in the output file. We hack by simply clobbering the
3973 list of sections in the BFD. This could be handled more
3974 cleanly by, say, a new section flag; the existing
3975 SEC_NEVER_LOAD flag is not the one we want, because that one
3976 still implies that the section takes up space in the output
3977 file. */
3978 bfd_section_list_clear (abfd);
3979
4ad4eba5
AM
3980 /* Find the name to use in a DT_NEEDED entry that refers to this
3981 object. If the object has a DT_SONAME entry, we use it.
3982 Otherwise, if the generic linker stuck something in
3983 elf_dt_name, we use that. Otherwise, we just use the file
3984 name. */
3985 if (soname == NULL || *soname == '\0')
3986 {
3987 soname = elf_dt_name (abfd);
3988 if (soname == NULL || *soname == '\0')
3989 soname = bfd_get_filename (abfd);
3990 }
3991
3992 /* Save the SONAME because sometimes the linker emulation code
3993 will need to know it. */
3994 elf_dt_name (abfd) = soname;
3995
7e9f0867 3996 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3997 if (ret < 0)
3998 goto error_return;
3999
4000 /* If we have already included this dynamic object in the
4001 link, just ignore it. There is no reason to include a
4002 particular dynamic object more than once. */
4003 if (ret > 0)
4004 return TRUE;
7ee314fa
AM
4005
4006 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4007 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4008 }
4009
4010 /* If this is a dynamic object, we always link against the .dynsym
4011 symbol table, not the .symtab symbol table. The dynamic linker
4012 will only see the .dynsym symbol table, so there is no reason to
4013 look at .symtab for a dynamic object. */
4014
4015 if (! dynamic || elf_dynsymtab (abfd) == 0)
4016 hdr = &elf_tdata (abfd)->symtab_hdr;
4017 else
4018 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4019
4020 symcount = hdr->sh_size / bed->s->sizeof_sym;
4021
4022 /* The sh_info field of the symtab header tells us where the
4023 external symbols start. We don't care about the local symbols at
4024 this point. */
4025 if (elf_bad_symtab (abfd))
4026 {
4027 extsymcount = symcount;
4028 extsymoff = 0;
4029 }
4030 else
4031 {
4032 extsymcount = symcount - hdr->sh_info;
4033 extsymoff = hdr->sh_info;
4034 }
4035
f45794cb 4036 sym_hash = elf_sym_hashes (abfd);
012b2306 4037 if (extsymcount != 0)
4ad4eba5
AM
4038 {
4039 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4040 NULL, NULL, NULL);
4041 if (isymbuf == NULL)
4042 goto error_return;
4043
4ad4eba5 4044 if (sym_hash == NULL)
012b2306
AM
4045 {
4046 /* We store a pointer to the hash table entry for each
4047 external symbol. */
ef53be89
AM
4048 amt = extsymcount;
4049 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4050 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4051 if (sym_hash == NULL)
4052 goto error_free_sym;
4053 elf_sym_hashes (abfd) = sym_hash;
4054 }
4ad4eba5
AM
4055 }
4056
4057 if (dynamic)
4058 {
4059 /* Read in any version definitions. */
fc0e6df6
PB
4060 if (!_bfd_elf_slurp_version_tables (abfd,
4061 info->default_imported_symver))
4ad4eba5
AM
4062 goto error_free_sym;
4063
4064 /* Read in the symbol versions, but don't bother to convert them
4065 to internal format. */
4066 if (elf_dynversym (abfd) != 0)
4067 {
4068 Elf_Internal_Shdr *versymhdr;
4069
4070 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4071 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4072 if (extversym == NULL)
4073 goto error_free_sym;
4074 amt = versymhdr->sh_size;
4075 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4076 || bfd_bread (extversym, amt, abfd) != amt)
4077 goto error_free_vers;
4078 }
4079 }
4080
66eb6687
AM
4081 /* If we are loading an as-needed shared lib, save the symbol table
4082 state before we start adding symbols. If the lib turns out
4083 to be unneeded, restore the state. */
4084 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4085 {
4086 unsigned int i;
4087 size_t entsize;
4088
4089 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4090 {
4091 struct bfd_hash_entry *p;
2de92251 4092 struct elf_link_hash_entry *h;
66eb6687
AM
4093
4094 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4095 {
4096 h = (struct elf_link_hash_entry *) p;
4097 entsize += htab->root.table.entsize;
4098 if (h->root.type == bfd_link_hash_warning)
4099 entsize += htab->root.table.entsize;
4100 }
66eb6687
AM
4101 }
4102
4103 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4104 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4105 if (old_tab == NULL)
4106 goto error_free_vers;
4107
4108 /* Remember the current objalloc pointer, so that all mem for
4109 symbols added can later be reclaimed. */
4110 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4111 if (alloc_mark == NULL)
4112 goto error_free_vers;
4113
5061a885
AM
4114 /* Make a special call to the linker "notice" function to
4115 tell it that we are about to handle an as-needed lib. */
e5034e59 4116 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4117 goto error_free_vers;
5061a885 4118
f45794cb
AM
4119 /* Clone the symbol table. Remember some pointers into the
4120 symbol table, and dynamic symbol count. */
4121 old_ent = (char *) old_tab + tabsize;
66eb6687 4122 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4123 old_undefs = htab->root.undefs;
4124 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4125 old_table = htab->root.table.table;
4126 old_size = htab->root.table.size;
4127 old_count = htab->root.table.count;
5b677558
AM
4128 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4129 if (old_strtab == NULL)
4130 goto error_free_vers;
66eb6687
AM
4131
4132 for (i = 0; i < htab->root.table.size; i++)
4133 {
4134 struct bfd_hash_entry *p;
2de92251 4135 struct elf_link_hash_entry *h;
66eb6687
AM
4136
4137 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4138 {
4139 memcpy (old_ent, p, htab->root.table.entsize);
4140 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4141 h = (struct elf_link_hash_entry *) p;
4142 if (h->root.type == bfd_link_hash_warning)
4143 {
4144 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4145 old_ent = (char *) old_ent + htab->root.table.entsize;
4146 }
66eb6687
AM
4147 }
4148 }
4149 }
4ad4eba5 4150
66eb6687 4151 weaks = NULL;
4ad4eba5
AM
4152 ever = extversym != NULL ? extversym + extsymoff : NULL;
4153 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4154 isym < isymend;
4155 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4156 {
4157 int bind;
4158 bfd_vma value;
af44c138 4159 asection *sec, *new_sec;
4ad4eba5
AM
4160 flagword flags;
4161 const char *name;
4162 struct elf_link_hash_entry *h;
90c984fc 4163 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4164 bfd_boolean definition;
4165 bfd_boolean size_change_ok;
4166 bfd_boolean type_change_ok;
4167 bfd_boolean new_weakdef;
37a9e49a
L
4168 bfd_boolean new_weak;
4169 bfd_boolean old_weak;
4ad4eba5 4170 bfd_boolean override;
a4d8e49b 4171 bfd_boolean common;
97196564 4172 bfd_boolean discarded;
4ad4eba5
AM
4173 unsigned int old_alignment;
4174 bfd *old_bfd;
6e33951e 4175 bfd_boolean matched;
4ad4eba5
AM
4176
4177 override = FALSE;
4178
4179 flags = BSF_NO_FLAGS;
4180 sec = NULL;
4181 value = isym->st_value;
a4d8e49b 4182 common = bed->common_definition (isym);
97196564 4183 discarded = FALSE;
4ad4eba5
AM
4184
4185 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4186 switch (bind)
4ad4eba5 4187 {
3e7a7d11 4188 case STB_LOCAL:
4ad4eba5
AM
4189 /* This should be impossible, since ELF requires that all
4190 global symbols follow all local symbols, and that sh_info
4191 point to the first global symbol. Unfortunately, Irix 5
4192 screws this up. */
4193 continue;
3e7a7d11
NC
4194
4195 case STB_GLOBAL:
a4d8e49b 4196 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4197 flags = BSF_GLOBAL;
3e7a7d11
NC
4198 break;
4199
4200 case STB_WEAK:
4201 flags = BSF_WEAK;
4202 break;
4203
4204 case STB_GNU_UNIQUE:
4205 flags = BSF_GNU_UNIQUE;
4206 break;
4207
4208 default:
4ad4eba5 4209 /* Leave it up to the processor backend. */
3e7a7d11 4210 break;
4ad4eba5
AM
4211 }
4212
4213 if (isym->st_shndx == SHN_UNDEF)
4214 sec = bfd_und_section_ptr;
cb33740c
AM
4215 else if (isym->st_shndx == SHN_ABS)
4216 sec = bfd_abs_section_ptr;
4217 else if (isym->st_shndx == SHN_COMMON)
4218 {
4219 sec = bfd_com_section_ptr;
4220 /* What ELF calls the size we call the value. What ELF
4221 calls the value we call the alignment. */
4222 value = isym->st_size;
4223 }
4224 else
4ad4eba5
AM
4225 {
4226 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4227 if (sec == NULL)
4228 sec = bfd_abs_section_ptr;
dbaa2011 4229 else if (discarded_section (sec))
529fcb95 4230 {
e5d08002
L
4231 /* Symbols from discarded section are undefined. We keep
4232 its visibility. */
529fcb95 4233 sec = bfd_und_section_ptr;
97196564 4234 discarded = TRUE;
529fcb95
PB
4235 isym->st_shndx = SHN_UNDEF;
4236 }
4ad4eba5
AM
4237 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4238 value -= sec->vma;
4239 }
4ad4eba5
AM
4240
4241 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4242 isym->st_name);
4243 if (name == NULL)
4244 goto error_free_vers;
4245
4246 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4247 && (abfd->flags & BFD_PLUGIN) != 0)
4248 {
4249 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4250
4251 if (xc == NULL)
4252 {
4253 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4254 | SEC_EXCLUDE);
4255 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4256 if (xc == NULL)
4257 goto error_free_vers;
4258 }
4259 sec = xc;
4260 }
4261 else if (isym->st_shndx == SHN_COMMON
4262 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4263 && !bfd_link_relocatable (info))
4ad4eba5
AM
4264 {
4265 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4266
4267 if (tcomm == NULL)
4268 {
02d00247
AM
4269 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4270 | SEC_LINKER_CREATED);
4271 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4272 if (tcomm == NULL)
4ad4eba5
AM
4273 goto error_free_vers;
4274 }
4275 sec = tcomm;
4276 }
66eb6687 4277 else if (bed->elf_add_symbol_hook)
4ad4eba5 4278 {
66eb6687
AM
4279 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4280 &sec, &value))
4ad4eba5
AM
4281 goto error_free_vers;
4282
4283 /* The hook function sets the name to NULL if this symbol
4284 should be skipped for some reason. */
4285 if (name == NULL)
4286 continue;
4287 }
4288
4289 /* Sanity check that all possibilities were handled. */
4290 if (sec == NULL)
4291 {
4292 bfd_set_error (bfd_error_bad_value);
4293 goto error_free_vers;
4294 }
4295
191c0c42
AM
4296 /* Silently discard TLS symbols from --just-syms. There's
4297 no way to combine a static TLS block with a new TLS block
4298 for this executable. */
4299 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4300 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4301 continue;
4302
4ad4eba5
AM
4303 if (bfd_is_und_section (sec)
4304 || bfd_is_com_section (sec))
4305 definition = FALSE;
4306 else
4307 definition = TRUE;
4308
4309 size_change_ok = FALSE;
66eb6687 4310 type_change_ok = bed->type_change_ok;
37a9e49a 4311 old_weak = FALSE;
6e33951e 4312 matched = FALSE;
4ad4eba5
AM
4313 old_alignment = 0;
4314 old_bfd = NULL;
af44c138 4315 new_sec = sec;
4ad4eba5 4316
66eb6687 4317 if (is_elf_hash_table (htab))
4ad4eba5
AM
4318 {
4319 Elf_Internal_Versym iver;
4320 unsigned int vernum = 0;
4321 bfd_boolean skip;
4322
fc0e6df6 4323 if (ever == NULL)
4ad4eba5 4324 {
fc0e6df6
PB
4325 if (info->default_imported_symver)
4326 /* Use the default symbol version created earlier. */
4327 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4328 else
4329 iver.vs_vers = 0;
4330 }
4331 else
4332 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4333
4334 vernum = iver.vs_vers & VERSYM_VERSION;
4335
4336 /* If this is a hidden symbol, or if it is not version
4337 1, we append the version name to the symbol name.
cc86ff91
EB
4338 However, we do not modify a non-hidden absolute symbol
4339 if it is not a function, because it might be the version
4340 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4341 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4342 || (vernum > 1
4343 && (!bfd_is_abs_section (sec)
4344 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4345 {
4346 const char *verstr;
4347 size_t namelen, verlen, newlen;
4348 char *newname, *p;
4349
4350 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4351 {
fc0e6df6
PB
4352 if (vernum > elf_tdata (abfd)->cverdefs)
4353 verstr = NULL;
4354 else if (vernum > 1)
4355 verstr =
4356 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4357 else
4358 verstr = "";
4ad4eba5 4359
fc0e6df6 4360 if (verstr == NULL)
4ad4eba5 4361 {
4eca0228 4362 _bfd_error_handler
695344c0 4363 /* xgettext:c-format */
fc0e6df6
PB
4364 (_("%B: %s: invalid version %u (max %d)"),
4365 abfd, name, vernum,
4366 elf_tdata (abfd)->cverdefs);
4367 bfd_set_error (bfd_error_bad_value);
4368 goto error_free_vers;
4ad4eba5 4369 }
fc0e6df6
PB
4370 }
4371 else
4372 {
4373 /* We cannot simply test for the number of
4374 entries in the VERNEED section since the
4375 numbers for the needed versions do not start
4376 at 0. */
4377 Elf_Internal_Verneed *t;
4378
4379 verstr = NULL;
4380 for (t = elf_tdata (abfd)->verref;
4381 t != NULL;
4382 t = t->vn_nextref)
4ad4eba5 4383 {
fc0e6df6 4384 Elf_Internal_Vernaux *a;
4ad4eba5 4385
fc0e6df6
PB
4386 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4387 {
4388 if (a->vna_other == vernum)
4ad4eba5 4389 {
fc0e6df6
PB
4390 verstr = a->vna_nodename;
4391 break;
4ad4eba5 4392 }
4ad4eba5 4393 }
fc0e6df6
PB
4394 if (a != NULL)
4395 break;
4396 }
4397 if (verstr == NULL)
4398 {
4eca0228 4399 _bfd_error_handler
695344c0 4400 /* xgettext:c-format */
fc0e6df6
PB
4401 (_("%B: %s: invalid needed version %d"),
4402 abfd, name, vernum);
4403 bfd_set_error (bfd_error_bad_value);
4404 goto error_free_vers;
4ad4eba5 4405 }
4ad4eba5 4406 }
fc0e6df6
PB
4407
4408 namelen = strlen (name);
4409 verlen = strlen (verstr);
4410 newlen = namelen + verlen + 2;
4411 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4412 && isym->st_shndx != SHN_UNDEF)
4413 ++newlen;
4414
a50b1753 4415 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4416 if (newname == NULL)
4417 goto error_free_vers;
4418 memcpy (newname, name, namelen);
4419 p = newname + namelen;
4420 *p++ = ELF_VER_CHR;
4421 /* If this is a defined non-hidden version symbol,
4422 we add another @ to the name. This indicates the
4423 default version of the symbol. */
4424 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4425 && isym->st_shndx != SHN_UNDEF)
4426 *p++ = ELF_VER_CHR;
4427 memcpy (p, verstr, verlen + 1);
4428
4429 name = newname;
4ad4eba5
AM
4430 }
4431
cd3416da
AM
4432 /* If this symbol has default visibility and the user has
4433 requested we not re-export it, then mark it as hidden. */
a0d49154 4434 if (!bfd_is_und_section (sec)
cd3416da 4435 && !dynamic
ce875075 4436 && abfd->no_export
cd3416da
AM
4437 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4438 isym->st_other = (STV_HIDDEN
4439 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4440
4f3fedcf
AM
4441 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4442 sym_hash, &old_bfd, &old_weak,
4443 &old_alignment, &skip, &override,
6e33951e
L
4444 &type_change_ok, &size_change_ok,
4445 &matched))
4ad4eba5
AM
4446 goto error_free_vers;
4447
4448 if (skip)
4449 continue;
4450
6e33951e
L
4451 /* Override a definition only if the new symbol matches the
4452 existing one. */
4453 if (override && matched)
4ad4eba5
AM
4454 definition = FALSE;
4455
4456 h = *sym_hash;
4457 while (h->root.type == bfd_link_hash_indirect
4458 || h->root.type == bfd_link_hash_warning)
4459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4460
4ad4eba5 4461 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4462 && vernum > 1
4463 && definition)
4464 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4465 }
4466
4467 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4468 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4469 (struct bfd_link_hash_entry **) sym_hash)))
4470 goto error_free_vers;
4471
a43942db
MR
4472 if ((flags & BSF_GNU_UNIQUE)
4473 && (abfd->flags & DYNAMIC) == 0
4474 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4475 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4476
4ad4eba5 4477 h = *sym_hash;
90c984fc
L
4478 /* We need to make sure that indirect symbol dynamic flags are
4479 updated. */
4480 hi = h;
4ad4eba5
AM
4481 while (h->root.type == bfd_link_hash_indirect
4482 || h->root.type == bfd_link_hash_warning)
4483 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4484
97196564
L
4485 /* Setting the index to -3 tells elf_link_output_extsym that
4486 this symbol is defined in a discarded section. */
4487 if (discarded)
4488 h->indx = -3;
4489
4ad4eba5
AM
4490 *sym_hash = h;
4491
37a9e49a 4492 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4493 new_weakdef = FALSE;
4494 if (dynamic
4495 && definition
37a9e49a 4496 && new_weak
fcb93ecf 4497 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4498 && is_elf_hash_table (htab)
f6e332e6 4499 && h->u.weakdef == NULL)
4ad4eba5
AM
4500 {
4501 /* Keep a list of all weak defined non function symbols from
4502 a dynamic object, using the weakdef field. Later in this
4503 function we will set the weakdef field to the correct
4504 value. We only put non-function symbols from dynamic
4505 objects on this list, because that happens to be the only
4506 time we need to know the normal symbol corresponding to a
4507 weak symbol, and the information is time consuming to
4508 figure out. If the weakdef field is not already NULL,
4509 then this symbol was already defined by some previous
4510 dynamic object, and we will be using that previous
4511 definition anyhow. */
4512
f6e332e6 4513 h->u.weakdef = weaks;
4ad4eba5
AM
4514 weaks = h;
4515 new_weakdef = TRUE;
4516 }
4517
4518 /* Set the alignment of a common symbol. */
a4d8e49b 4519 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4520 && h->root.type == bfd_link_hash_common)
4521 {
4522 unsigned int align;
4523
a4d8e49b 4524 if (common)
af44c138
L
4525 align = bfd_log2 (isym->st_value);
4526 else
4527 {
4528 /* The new symbol is a common symbol in a shared object.
4529 We need to get the alignment from the section. */
4530 align = new_sec->alignment_power;
4531 }
595213d4 4532 if (align > old_alignment)
4ad4eba5
AM
4533 h->root.u.c.p->alignment_power = align;
4534 else
4535 h->root.u.c.p->alignment_power = old_alignment;
4536 }
4537
66eb6687 4538 if (is_elf_hash_table (htab))
4ad4eba5 4539 {
4f3fedcf
AM
4540 /* Set a flag in the hash table entry indicating the type of
4541 reference or definition we just found. A dynamic symbol
4542 is one which is referenced or defined by both a regular
4543 object and a shared object. */
4544 bfd_boolean dynsym = FALSE;
4545
4546 /* Plugin symbols aren't normal. Don't set def_regular or
4547 ref_regular for them, or make them dynamic. */
4548 if ((abfd->flags & BFD_PLUGIN) != 0)
4549 ;
4550 else if (! dynamic)
4551 {
4552 if (! definition)
4553 {
4554 h->ref_regular = 1;
4555 if (bind != STB_WEAK)
4556 h->ref_regular_nonweak = 1;
4557 }
4558 else
4559 {
4560 h->def_regular = 1;
4561 if (h->def_dynamic)
4562 {
4563 h->def_dynamic = 0;
4564 h->ref_dynamic = 1;
4565 }
4566 }
4567
4568 /* If the indirect symbol has been forced local, don't
4569 make the real symbol dynamic. */
4570 if ((h == hi || !hi->forced_local)
0e1862bb 4571 && (bfd_link_dll (info)
4f3fedcf
AM
4572 || h->def_dynamic
4573 || h->ref_dynamic))
4574 dynsym = TRUE;
4575 }
4576 else
4577 {
4578 if (! definition)
4579 {
4580 h->ref_dynamic = 1;
4581 hi->ref_dynamic = 1;
4582 }
4583 else
4584 {
4585 h->def_dynamic = 1;
4586 hi->def_dynamic = 1;
4587 }
4588
4589 /* If the indirect symbol has been forced local, don't
4590 make the real symbol dynamic. */
4591 if ((h == hi || !hi->forced_local)
4592 && (h->def_regular
4593 || h->ref_regular
4594 || (h->u.weakdef != NULL
4595 && ! new_weakdef
4596 && h->u.weakdef->dynindx != -1)))
4597 dynsym = TRUE;
4598 }
4599
4600 /* Check to see if we need to add an indirect symbol for
4601 the default name. */
4602 if (definition
4603 || (!override && h->root.type == bfd_link_hash_common))
4604 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4605 sec, value, &old_bfd, &dynsym))
4606 goto error_free_vers;
4ad4eba5
AM
4607
4608 /* Check the alignment when a common symbol is involved. This
4609 can change when a common symbol is overridden by a normal
4610 definition or a common symbol is ignored due to the old
4611 normal definition. We need to make sure the maximum
4612 alignment is maintained. */
a4d8e49b 4613 if ((old_alignment || common)
4ad4eba5
AM
4614 && h->root.type != bfd_link_hash_common)
4615 {
4616 unsigned int common_align;
4617 unsigned int normal_align;
4618 unsigned int symbol_align;
4619 bfd *normal_bfd;
4620 bfd *common_bfd;
4621
3a81e825
AM
4622 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4623 || h->root.type == bfd_link_hash_defweak);
4624
4ad4eba5
AM
4625 symbol_align = ffs (h->root.u.def.value) - 1;
4626 if (h->root.u.def.section->owner != NULL
0616a280
AM
4627 && (h->root.u.def.section->owner->flags
4628 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4629 {
4630 normal_align = h->root.u.def.section->alignment_power;
4631 if (normal_align > symbol_align)
4632 normal_align = symbol_align;
4633 }
4634 else
4635 normal_align = symbol_align;
4636
4637 if (old_alignment)
4638 {
4639 common_align = old_alignment;
4640 common_bfd = old_bfd;
4641 normal_bfd = abfd;
4642 }
4643 else
4644 {
4645 common_align = bfd_log2 (isym->st_value);
4646 common_bfd = abfd;
4647 normal_bfd = old_bfd;
4648 }
4649
4650 if (normal_align < common_align)
d07676f8
NC
4651 {
4652 /* PR binutils/2735 */
4653 if (normal_bfd == NULL)
4eca0228 4654 _bfd_error_handler
695344c0 4655 /* xgettext:c-format */
4f3fedcf
AM
4656 (_("Warning: alignment %u of common symbol `%s' in %B is"
4657 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4658 common_bfd, h->root.u.def.section,
4659 1 << common_align, name, 1 << normal_align);
4660 else
4eca0228 4661 _bfd_error_handler
695344c0 4662 /* xgettext:c-format */
d07676f8
NC
4663 (_("Warning: alignment %u of symbol `%s' in %B"
4664 " is smaller than %u in %B"),
4665 normal_bfd, common_bfd,
4666 1 << normal_align, name, 1 << common_align);
4667 }
4ad4eba5
AM
4668 }
4669
83ad0046 4670 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4671 if (isym->st_size != 0
4672 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4673 && (definition || h->size == 0))
4674 {
83ad0046
L
4675 if (h->size != 0
4676 && h->size != isym->st_size
4677 && ! size_change_ok)
4eca0228 4678 _bfd_error_handler
695344c0 4679 /* xgettext:c-format */
d003868e
AM
4680 (_("Warning: size of symbol `%s' changed"
4681 " from %lu in %B to %lu in %B"),
4682 old_bfd, abfd,
4ad4eba5 4683 name, (unsigned long) h->size,
d003868e 4684 (unsigned long) isym->st_size);
4ad4eba5
AM
4685
4686 h->size = isym->st_size;
4687 }
4688
4689 /* If this is a common symbol, then we always want H->SIZE
4690 to be the size of the common symbol. The code just above
4691 won't fix the size if a common symbol becomes larger. We
4692 don't warn about a size change here, because that is
4f3fedcf 4693 covered by --warn-common. Allow changes between different
fcb93ecf 4694 function types. */
4ad4eba5
AM
4695 if (h->root.type == bfd_link_hash_common)
4696 h->size = h->root.u.c.size;
4697
4698 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4699 && ((definition && !new_weak)
4700 || (old_weak && h->root.type == bfd_link_hash_common)
4701 || h->type == STT_NOTYPE))
4ad4eba5 4702 {
2955ec4c
L
4703 unsigned int type = ELF_ST_TYPE (isym->st_info);
4704
4705 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4706 symbol. */
4707 if (type == STT_GNU_IFUNC
4708 && (abfd->flags & DYNAMIC) != 0)
4709 type = STT_FUNC;
4ad4eba5 4710
2955ec4c
L
4711 if (h->type != type)
4712 {
4713 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4714 /* xgettext:c-format */
4eca0228 4715 _bfd_error_handler
2955ec4c
L
4716 (_("Warning: type of symbol `%s' changed"
4717 " from %d to %d in %B"),
4718 abfd, name, h->type, type);
4719
4720 h->type = type;
4721 }
4ad4eba5
AM
4722 }
4723
54ac0771 4724 /* Merge st_other field. */
b8417128 4725 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4726
c3df8c14 4727 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4728 if (definition
4729 && (sec->flags & SEC_DEBUGGING)
4730 && !bfd_link_relocatable (info))
c3df8c14
AM
4731 dynsym = FALSE;
4732
4f3fedcf
AM
4733 /* Nor should we make plugin symbols dynamic. */
4734 if ((abfd->flags & BFD_PLUGIN) != 0)
4735 dynsym = FALSE;
4736
35fc36a8 4737 if (definition)
35399224
L
4738 {
4739 h->target_internal = isym->st_target_internal;
4740 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4741 }
35fc36a8 4742
4ad4eba5
AM
4743 if (definition && !dynamic)
4744 {
4745 char *p = strchr (name, ELF_VER_CHR);
4746 if (p != NULL && p[1] != ELF_VER_CHR)
4747 {
4748 /* Queue non-default versions so that .symver x, x@FOO
4749 aliases can be checked. */
66eb6687 4750 if (!nondeflt_vers)
4ad4eba5 4751 {
66eb6687
AM
4752 amt = ((isymend - isym + 1)
4753 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4754 nondeflt_vers
4755 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4756 if (!nondeflt_vers)
4757 goto error_free_vers;
4ad4eba5 4758 }
66eb6687 4759 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4760 }
4761 }
4762
4763 if (dynsym && h->dynindx == -1)
4764 {
c152c796 4765 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4766 goto error_free_vers;
f6e332e6 4767 if (h->u.weakdef != NULL
4ad4eba5 4768 && ! new_weakdef
f6e332e6 4769 && h->u.weakdef->dynindx == -1)
4ad4eba5 4770 {
66eb6687 4771 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4772 goto error_free_vers;
4773 }
4774 }
1f599d0e 4775 else if (h->dynindx != -1)
4ad4eba5
AM
4776 /* If the symbol already has a dynamic index, but
4777 visibility says it should not be visible, turn it into
4778 a local symbol. */
4779 switch (ELF_ST_VISIBILITY (h->other))
4780 {
4781 case STV_INTERNAL:
4782 case STV_HIDDEN:
4783 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4784 dynsym = FALSE;
4785 break;
4786 }
4787
aef28989
L
4788 /* Don't add DT_NEEDED for references from the dummy bfd nor
4789 for unmatched symbol. */
4ad4eba5 4790 if (!add_needed
aef28989 4791 && matched
4ad4eba5 4792 && definition
010e5ae2 4793 && ((dynsym
ffa9430d 4794 && h->ref_regular_nonweak
4f3fedcf
AM
4795 && (old_bfd == NULL
4796 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4797 || (h->ref_dynamic_nonweak
010e5ae2 4798 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4799 && !on_needed_list (elf_dt_name (abfd),
4800 htab->needed, NULL))))
4ad4eba5
AM
4801 {
4802 int ret;
4803 const char *soname = elf_dt_name (abfd);
4804
16e4ecc0
AM
4805 info->callbacks->minfo ("%!", soname, old_bfd,
4806 h->root.root.string);
4807
4ad4eba5
AM
4808 /* A symbol from a library loaded via DT_NEEDED of some
4809 other library is referenced by a regular object.
e56f61be 4810 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4811 --no-add-needed is used and the reference was not
4812 a weak one. */
4f3fedcf 4813 if (old_bfd != NULL
b918acf9 4814 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4815 {
4eca0228 4816 _bfd_error_handler
695344c0 4817 /* xgettext:c-format */
3cbc5de0 4818 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4819 old_bfd, name);
ff5ac77b 4820 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4821 goto error_free_vers;
4822 }
4823
a50b1753 4824 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4825 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4826
4ad4eba5 4827 add_needed = TRUE;
7e9f0867 4828 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4829 if (ret < 0)
4830 goto error_free_vers;
4831
4832 BFD_ASSERT (ret == 0);
4833 }
4834 }
4835 }
4836
66eb6687
AM
4837 if (extversym != NULL)
4838 {
4839 free (extversym);
4840 extversym = NULL;
4841 }
4842
4843 if (isymbuf != NULL)
4844 {
4845 free (isymbuf);
4846 isymbuf = NULL;
4847 }
4848
4849 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4850 {
4851 unsigned int i;
4852
4853 /* Restore the symbol table. */
f45794cb
AM
4854 old_ent = (char *) old_tab + tabsize;
4855 memset (elf_sym_hashes (abfd), 0,
4856 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4857 htab->root.table.table = old_table;
4858 htab->root.table.size = old_size;
4859 htab->root.table.count = old_count;
66eb6687 4860 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4861 htab->root.undefs = old_undefs;
4862 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4863 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4864 free (old_strtab);
4865 old_strtab = NULL;
66eb6687
AM
4866 for (i = 0; i < htab->root.table.size; i++)
4867 {
4868 struct bfd_hash_entry *p;
4869 struct elf_link_hash_entry *h;
3e0882af
L
4870 bfd_size_type size;
4871 unsigned int alignment_power;
66eb6687
AM
4872
4873 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4874 {
4875 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4876 if (h->root.type == bfd_link_hash_warning)
4877 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4878
3e0882af
L
4879 /* Preserve the maximum alignment and size for common
4880 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4881 since it can still be loaded at run time by another
3e0882af
L
4882 dynamic lib. */
4883 if (h->root.type == bfd_link_hash_common)
4884 {
4885 size = h->root.u.c.size;
4886 alignment_power = h->root.u.c.p->alignment_power;
4887 }
4888 else
4889 {
4890 size = 0;
4891 alignment_power = 0;
4892 }
66eb6687
AM
4893 memcpy (p, old_ent, htab->root.table.entsize);
4894 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4895 h = (struct elf_link_hash_entry *) p;
4896 if (h->root.type == bfd_link_hash_warning)
4897 {
4898 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4899 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4900 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4901 }
a4542f1b 4902 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4903 {
4904 if (size > h->root.u.c.size)
4905 h->root.u.c.size = size;
4906 if (alignment_power > h->root.u.c.p->alignment_power)
4907 h->root.u.c.p->alignment_power = alignment_power;
4908 }
66eb6687
AM
4909 }
4910 }
4911
5061a885
AM
4912 /* Make a special call to the linker "notice" function to
4913 tell it that symbols added for crefs may need to be removed. */
e5034e59 4914 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4915 goto error_free_vers;
5061a885 4916
66eb6687
AM
4917 free (old_tab);
4918 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4919 alloc_mark);
4920 if (nondeflt_vers != NULL)
4921 free (nondeflt_vers);
4922 return TRUE;
4923 }
2de92251 4924
66eb6687
AM
4925 if (old_tab != NULL)
4926 {
e5034e59 4927 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4928 goto error_free_vers;
66eb6687
AM
4929 free (old_tab);
4930 old_tab = NULL;
4931 }
4932
c6e8a9a8
L
4933 /* Now that all the symbols from this input file are created, if
4934 not performing a relocatable link, handle .symver foo, foo@BAR
4935 such that any relocs against foo become foo@BAR. */
0e1862bb 4936 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4937 {
ef53be89 4938 size_t cnt, symidx;
4ad4eba5
AM
4939
4940 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4941 {
4942 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4943 char *shortname, *p;
4944
4945 p = strchr (h->root.root.string, ELF_VER_CHR);
4946 if (p == NULL
4947 || (h->root.type != bfd_link_hash_defined
4948 && h->root.type != bfd_link_hash_defweak))
4949 continue;
4950
4951 amt = p - h->root.root.string;
a50b1753 4952 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4953 if (!shortname)
4954 goto error_free_vers;
4ad4eba5
AM
4955 memcpy (shortname, h->root.root.string, amt);
4956 shortname[amt] = '\0';
4957
4958 hi = (struct elf_link_hash_entry *)
66eb6687 4959 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4960 FALSE, FALSE, FALSE);
4961 if (hi != NULL
4962 && hi->root.type == h->root.type
4963 && hi->root.u.def.value == h->root.u.def.value
4964 && hi->root.u.def.section == h->root.u.def.section)
4965 {
4966 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4967 hi->root.type = bfd_link_hash_indirect;
4968 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4969 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4970 sym_hash = elf_sym_hashes (abfd);
4971 if (sym_hash)
4972 for (symidx = 0; symidx < extsymcount; ++symidx)
4973 if (sym_hash[symidx] == hi)
4974 {
4975 sym_hash[symidx] = h;
4976 break;
4977 }
4978 }
4979 free (shortname);
4980 }
4981 free (nondeflt_vers);
4982 nondeflt_vers = NULL;
4983 }
4984
4ad4eba5
AM
4985 /* Now set the weakdefs field correctly for all the weak defined
4986 symbols we found. The only way to do this is to search all the
4987 symbols. Since we only need the information for non functions in
4988 dynamic objects, that's the only time we actually put anything on
4989 the list WEAKS. We need this information so that if a regular
4990 object refers to a symbol defined weakly in a dynamic object, the
4991 real symbol in the dynamic object is also put in the dynamic
4992 symbols; we also must arrange for both symbols to point to the
4993 same memory location. We could handle the general case of symbol
4994 aliasing, but a general symbol alias can only be generated in
4995 assembler code, handling it correctly would be very time
4996 consuming, and other ELF linkers don't handle general aliasing
4997 either. */
4998 if (weaks != NULL)
4999 {
5000 struct elf_link_hash_entry **hpp;
5001 struct elf_link_hash_entry **hppend;
5002 struct elf_link_hash_entry **sorted_sym_hash;
5003 struct elf_link_hash_entry *h;
5004 size_t sym_count;
5005
5006 /* Since we have to search the whole symbol list for each weak
5007 defined symbol, search time for N weak defined symbols will be
5008 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5009 amt = extsymcount;
5010 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5011 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5012 if (sorted_sym_hash == NULL)
5013 goto error_return;
5014 sym_hash = sorted_sym_hash;
5015 hpp = elf_sym_hashes (abfd);
5016 hppend = hpp + extsymcount;
5017 sym_count = 0;
5018 for (; hpp < hppend; hpp++)
5019 {
5020 h = *hpp;
5021 if (h != NULL
5022 && h->root.type == bfd_link_hash_defined
fcb93ecf 5023 && !bed->is_function_type (h->type))
4ad4eba5
AM
5024 {
5025 *sym_hash = h;
5026 sym_hash++;
5027 sym_count++;
5028 }
5029 }
5030
5031 qsort (sorted_sym_hash, sym_count,
5032 sizeof (struct elf_link_hash_entry *),
5033 elf_sort_symbol);
5034
5035 while (weaks != NULL)
5036 {
5037 struct elf_link_hash_entry *hlook;
5038 asection *slook;
5039 bfd_vma vlook;
ed54588d 5040 size_t i, j, idx = 0;
4ad4eba5
AM
5041
5042 hlook = weaks;
f6e332e6
AM
5043 weaks = hlook->u.weakdef;
5044 hlook->u.weakdef = NULL;
4ad4eba5
AM
5045
5046 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5047 || hlook->root.type == bfd_link_hash_defweak
5048 || hlook->root.type == bfd_link_hash_common
5049 || hlook->root.type == bfd_link_hash_indirect);
5050 slook = hlook->root.u.def.section;
5051 vlook = hlook->root.u.def.value;
5052
4ad4eba5
AM
5053 i = 0;
5054 j = sym_count;
14160578 5055 while (i != j)
4ad4eba5
AM
5056 {
5057 bfd_signed_vma vdiff;
5058 idx = (i + j) / 2;
14160578 5059 h = sorted_sym_hash[idx];
4ad4eba5
AM
5060 vdiff = vlook - h->root.u.def.value;
5061 if (vdiff < 0)
5062 j = idx;
5063 else if (vdiff > 0)
5064 i = idx + 1;
5065 else
5066 {
d3435ae8 5067 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5068 if (sdiff < 0)
5069 j = idx;
5070 else if (sdiff > 0)
5071 i = idx + 1;
5072 else
14160578 5073 break;
4ad4eba5
AM
5074 }
5075 }
5076
5077 /* We didn't find a value/section match. */
14160578 5078 if (i == j)
4ad4eba5
AM
5079 continue;
5080
14160578
AM
5081 /* With multiple aliases, or when the weak symbol is already
5082 strongly defined, we have multiple matching symbols and
5083 the binary search above may land on any of them. Step
5084 one past the matching symbol(s). */
5085 while (++idx != j)
5086 {
5087 h = sorted_sym_hash[idx];
5088 if (h->root.u.def.section != slook
5089 || h->root.u.def.value != vlook)
5090 break;
5091 }
5092
5093 /* Now look back over the aliases. Since we sorted by size
5094 as well as value and section, we'll choose the one with
5095 the largest size. */
5096 while (idx-- != i)
4ad4eba5 5097 {
14160578 5098 h = sorted_sym_hash[idx];
4ad4eba5
AM
5099
5100 /* Stop if value or section doesn't match. */
14160578
AM
5101 if (h->root.u.def.section != slook
5102 || h->root.u.def.value != vlook)
4ad4eba5
AM
5103 break;
5104 else if (h != hlook)
5105 {
f6e332e6 5106 hlook->u.weakdef = h;
4ad4eba5
AM
5107
5108 /* If the weak definition is in the list of dynamic
5109 symbols, make sure the real definition is put
5110 there as well. */
5111 if (hlook->dynindx != -1 && h->dynindx == -1)
5112 {
c152c796 5113 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5114 {
5115 err_free_sym_hash:
5116 free (sorted_sym_hash);
5117 goto error_return;
5118 }
4ad4eba5
AM
5119 }
5120
5121 /* If the real definition is in the list of dynamic
5122 symbols, make sure the weak definition is put
5123 there as well. If we don't do this, then the
5124 dynamic loader might not merge the entries for the
5125 real definition and the weak definition. */
5126 if (h->dynindx != -1 && hlook->dynindx == -1)
5127 {
c152c796 5128 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5129 goto err_free_sym_hash;
4ad4eba5
AM
5130 }
5131 break;
5132 }
5133 }
5134 }
5135
5136 free (sorted_sym_hash);
5137 }
5138
33177bb1
AM
5139 if (bed->check_directives
5140 && !(*bed->check_directives) (abfd, info))
5141 return FALSE;
85fbca6a 5142
d9689752
L
5143 if (!info->check_relocs_after_open_input
5144 && !_bfd_elf_link_check_relocs (abfd, info))
5145 return FALSE;
4ad4eba5
AM
5146
5147 /* If this is a non-traditional link, try to optimize the handling
5148 of the .stab/.stabstr sections. */
5149 if (! dynamic
5150 && ! info->traditional_format
66eb6687 5151 && is_elf_hash_table (htab)
4ad4eba5
AM
5152 && (info->strip != strip_all && info->strip != strip_debugger))
5153 {
5154 asection *stabstr;
5155
5156 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5157 if (stabstr != NULL)
5158 {
5159 bfd_size_type string_offset = 0;
5160 asection *stab;
5161
5162 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5163 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5164 && (!stab->name[5] ||
5165 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5166 && (stab->flags & SEC_MERGE) == 0
5167 && !bfd_is_abs_section (stab->output_section))
5168 {
5169 struct bfd_elf_section_data *secdata;
5170
5171 secdata = elf_section_data (stab);
66eb6687
AM
5172 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5173 stabstr, &secdata->sec_info,
4ad4eba5
AM
5174 &string_offset))
5175 goto error_return;
5176 if (secdata->sec_info)
dbaa2011 5177 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5178 }
5179 }
5180 }
5181
66eb6687 5182 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5183 {
5184 /* Add this bfd to the loaded list. */
5185 struct elf_link_loaded_list *n;
5186
ca4be51c 5187 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5188 if (n == NULL)
5189 goto error_return;
5190 n->abfd = abfd;
66eb6687
AM
5191 n->next = htab->loaded;
5192 htab->loaded = n;
4ad4eba5
AM
5193 }
5194
5195 return TRUE;
5196
5197 error_free_vers:
66eb6687
AM
5198 if (old_tab != NULL)
5199 free (old_tab);
5b677558
AM
5200 if (old_strtab != NULL)
5201 free (old_strtab);
4ad4eba5
AM
5202 if (nondeflt_vers != NULL)
5203 free (nondeflt_vers);
5204 if (extversym != NULL)
5205 free (extversym);
5206 error_free_sym:
5207 if (isymbuf != NULL)
5208 free (isymbuf);
5209 error_return:
5210 return FALSE;
5211}
5212
8387904d
AM
5213/* Return the linker hash table entry of a symbol that might be
5214 satisfied by an archive symbol. Return -1 on error. */
5215
5216struct elf_link_hash_entry *
5217_bfd_elf_archive_symbol_lookup (bfd *abfd,
5218 struct bfd_link_info *info,
5219 const char *name)
5220{
5221 struct elf_link_hash_entry *h;
5222 char *p, *copy;
5223 size_t len, first;
5224
2a41f396 5225 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5226 if (h != NULL)
5227 return h;
5228
5229 /* If this is a default version (the name contains @@), look up the
5230 symbol again with only one `@' as well as without the version.
5231 The effect is that references to the symbol with and without the
5232 version will be matched by the default symbol in the archive. */
5233
5234 p = strchr (name, ELF_VER_CHR);
5235 if (p == NULL || p[1] != ELF_VER_CHR)
5236 return h;
5237
5238 /* First check with only one `@'. */
5239 len = strlen (name);
a50b1753 5240 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5241 if (copy == NULL)
5242 return (struct elf_link_hash_entry *) 0 - 1;
5243
5244 first = p - name + 1;
5245 memcpy (copy, name, first);
5246 memcpy (copy + first, name + first + 1, len - first);
5247
2a41f396 5248 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5249 if (h == NULL)
5250 {
5251 /* We also need to check references to the symbol without the
5252 version. */
5253 copy[first - 1] = '\0';
5254 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5255 FALSE, FALSE, TRUE);
8387904d
AM
5256 }
5257
5258 bfd_release (abfd, copy);
5259 return h;
5260}
5261
0ad989f9 5262/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5263 don't use _bfd_generic_link_add_archive_symbols because we need to
5264 handle versioned symbols.
0ad989f9
L
5265
5266 Fortunately, ELF archive handling is simpler than that done by
5267 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5268 oddities. In ELF, if we find a symbol in the archive map, and the
5269 symbol is currently undefined, we know that we must pull in that
5270 object file.
5271
5272 Unfortunately, we do have to make multiple passes over the symbol
5273 table until nothing further is resolved. */
5274
4ad4eba5
AM
5275static bfd_boolean
5276elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5277{
5278 symindex c;
13e570f8 5279 unsigned char *included = NULL;
0ad989f9
L
5280 carsym *symdefs;
5281 bfd_boolean loop;
5282 bfd_size_type amt;
8387904d
AM
5283 const struct elf_backend_data *bed;
5284 struct elf_link_hash_entry * (*archive_symbol_lookup)
5285 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5286
5287 if (! bfd_has_map (abfd))
5288 {
5289 /* An empty archive is a special case. */
5290 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5291 return TRUE;
5292 bfd_set_error (bfd_error_no_armap);
5293 return FALSE;
5294 }
5295
5296 /* Keep track of all symbols we know to be already defined, and all
5297 files we know to be already included. This is to speed up the
5298 second and subsequent passes. */
5299 c = bfd_ardata (abfd)->symdef_count;
5300 if (c == 0)
5301 return TRUE;
5302 amt = c;
13e570f8
AM
5303 amt *= sizeof (*included);
5304 included = (unsigned char *) bfd_zmalloc (amt);
5305 if (included == NULL)
5306 return FALSE;
0ad989f9
L
5307
5308 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5309 bed = get_elf_backend_data (abfd);
5310 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5311
5312 do
5313 {
5314 file_ptr last;
5315 symindex i;
5316 carsym *symdef;
5317 carsym *symdefend;
5318
5319 loop = FALSE;
5320 last = -1;
5321
5322 symdef = symdefs;
5323 symdefend = symdef + c;
5324 for (i = 0; symdef < symdefend; symdef++, i++)
5325 {
5326 struct elf_link_hash_entry *h;
5327 bfd *element;
5328 struct bfd_link_hash_entry *undefs_tail;
5329 symindex mark;
5330
13e570f8 5331 if (included[i])
0ad989f9
L
5332 continue;
5333 if (symdef->file_offset == last)
5334 {
5335 included[i] = TRUE;
5336 continue;
5337 }
5338
8387904d
AM
5339 h = archive_symbol_lookup (abfd, info, symdef->name);
5340 if (h == (struct elf_link_hash_entry *) 0 - 1)
5341 goto error_return;
0ad989f9
L
5342
5343 if (h == NULL)
5344 continue;
5345
5346 if (h->root.type == bfd_link_hash_common)
5347 {
5348 /* We currently have a common symbol. The archive map contains
5349 a reference to this symbol, so we may want to include it. We
5350 only want to include it however, if this archive element
5351 contains a definition of the symbol, not just another common
5352 declaration of it.
5353
5354 Unfortunately some archivers (including GNU ar) will put
5355 declarations of common symbols into their archive maps, as
5356 well as real definitions, so we cannot just go by the archive
5357 map alone. Instead we must read in the element's symbol
5358 table and check that to see what kind of symbol definition
5359 this is. */
5360 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5361 continue;
5362 }
5363 else if (h->root.type != bfd_link_hash_undefined)
5364 {
5365 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5366 /* Symbol must be defined. Don't check it again. */
5367 included[i] = TRUE;
0ad989f9
L
5368 continue;
5369 }
5370
5371 /* We need to include this archive member. */
5372 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5373 if (element == NULL)
5374 goto error_return;
5375
5376 if (! bfd_check_format (element, bfd_object))
5377 goto error_return;
5378
0ad989f9
L
5379 undefs_tail = info->hash->undefs_tail;
5380
0e144ba7
AM
5381 if (!(*info->callbacks
5382 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5383 continue;
0e144ba7 5384 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5385 goto error_return;
5386
5387 /* If there are any new undefined symbols, we need to make
5388 another pass through the archive in order to see whether
5389 they can be defined. FIXME: This isn't perfect, because
5390 common symbols wind up on undefs_tail and because an
5391 undefined symbol which is defined later on in this pass
5392 does not require another pass. This isn't a bug, but it
5393 does make the code less efficient than it could be. */
5394 if (undefs_tail != info->hash->undefs_tail)
5395 loop = TRUE;
5396
5397 /* Look backward to mark all symbols from this object file
5398 which we have already seen in this pass. */
5399 mark = i;
5400 do
5401 {
5402 included[mark] = TRUE;
5403 if (mark == 0)
5404 break;
5405 --mark;
5406 }
5407 while (symdefs[mark].file_offset == symdef->file_offset);
5408
5409 /* We mark subsequent symbols from this object file as we go
5410 on through the loop. */
5411 last = symdef->file_offset;
5412 }
5413 }
5414 while (loop);
5415
0ad989f9
L
5416 free (included);
5417
5418 return TRUE;
5419
5420 error_return:
0ad989f9
L
5421 if (included != NULL)
5422 free (included);
5423 return FALSE;
5424}
4ad4eba5
AM
5425
5426/* Given an ELF BFD, add symbols to the global hash table as
5427 appropriate. */
5428
5429bfd_boolean
5430bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5431{
5432 switch (bfd_get_format (abfd))
5433 {
5434 case bfd_object:
5435 return elf_link_add_object_symbols (abfd, info);
5436 case bfd_archive:
5437 return elf_link_add_archive_symbols (abfd, info);
5438 default:
5439 bfd_set_error (bfd_error_wrong_format);
5440 return FALSE;
5441 }
5442}
5a580b3a 5443\f
14b1c01e
AM
5444struct hash_codes_info
5445{
5446 unsigned long *hashcodes;
5447 bfd_boolean error;
5448};
a0c8462f 5449
5a580b3a
AM
5450/* This function will be called though elf_link_hash_traverse to store
5451 all hash value of the exported symbols in an array. */
5452
5453static bfd_boolean
5454elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5455{
a50b1753 5456 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5457 const char *name;
5a580b3a
AM
5458 unsigned long ha;
5459 char *alc = NULL;
5460
5a580b3a
AM
5461 /* Ignore indirect symbols. These are added by the versioning code. */
5462 if (h->dynindx == -1)
5463 return TRUE;
5464
5465 name = h->root.root.string;
422f1182 5466 if (h->versioned >= versioned)
5a580b3a 5467 {
422f1182
L
5468 char *p = strchr (name, ELF_VER_CHR);
5469 if (p != NULL)
14b1c01e 5470 {
422f1182
L
5471 alc = (char *) bfd_malloc (p - name + 1);
5472 if (alc == NULL)
5473 {
5474 inf->error = TRUE;
5475 return FALSE;
5476 }
5477 memcpy (alc, name, p - name);
5478 alc[p - name] = '\0';
5479 name = alc;
14b1c01e 5480 }
5a580b3a
AM
5481 }
5482
5483 /* Compute the hash value. */
5484 ha = bfd_elf_hash (name);
5485
5486 /* Store the found hash value in the array given as the argument. */
14b1c01e 5487 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5488
5489 /* And store it in the struct so that we can put it in the hash table
5490 later. */
f6e332e6 5491 h->u.elf_hash_value = ha;
5a580b3a
AM
5492
5493 if (alc != NULL)
5494 free (alc);
5495
5496 return TRUE;
5497}
5498
fdc90cb4
JJ
5499struct collect_gnu_hash_codes
5500{
5501 bfd *output_bfd;
5502 const struct elf_backend_data *bed;
5503 unsigned long int nsyms;
5504 unsigned long int maskbits;
5505 unsigned long int *hashcodes;
5506 unsigned long int *hashval;
5507 unsigned long int *indx;
5508 unsigned long int *counts;
5509 bfd_vma *bitmask;
5510 bfd_byte *contents;
5511 long int min_dynindx;
5512 unsigned long int bucketcount;
5513 unsigned long int symindx;
5514 long int local_indx;
5515 long int shift1, shift2;
5516 unsigned long int mask;
14b1c01e 5517 bfd_boolean error;
fdc90cb4
JJ
5518};
5519
5520/* This function will be called though elf_link_hash_traverse to store
5521 all hash value of the exported symbols in an array. */
5522
5523static bfd_boolean
5524elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5525{
a50b1753 5526 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5527 const char *name;
fdc90cb4
JJ
5528 unsigned long ha;
5529 char *alc = NULL;
5530
fdc90cb4
JJ
5531 /* Ignore indirect symbols. These are added by the versioning code. */
5532 if (h->dynindx == -1)
5533 return TRUE;
5534
5535 /* Ignore also local symbols and undefined symbols. */
5536 if (! (*s->bed->elf_hash_symbol) (h))
5537 return TRUE;
5538
5539 name = h->root.root.string;
422f1182 5540 if (h->versioned >= versioned)
fdc90cb4 5541 {
422f1182
L
5542 char *p = strchr (name, ELF_VER_CHR);
5543 if (p != NULL)
14b1c01e 5544 {
422f1182
L
5545 alc = (char *) bfd_malloc (p - name + 1);
5546 if (alc == NULL)
5547 {
5548 s->error = TRUE;
5549 return FALSE;
5550 }
5551 memcpy (alc, name, p - name);
5552 alc[p - name] = '\0';
5553 name = alc;
14b1c01e 5554 }
fdc90cb4
JJ
5555 }
5556
5557 /* Compute the hash value. */
5558 ha = bfd_elf_gnu_hash (name);
5559
5560 /* Store the found hash value in the array for compute_bucket_count,
5561 and also for .dynsym reordering purposes. */
5562 s->hashcodes[s->nsyms] = ha;
5563 s->hashval[h->dynindx] = ha;
5564 ++s->nsyms;
5565 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5566 s->min_dynindx = h->dynindx;
5567
5568 if (alc != NULL)
5569 free (alc);
5570
5571 return TRUE;
5572}
5573
5574/* This function will be called though elf_link_hash_traverse to do
5575 final dynaminc symbol renumbering. */
5576
5577static bfd_boolean
5578elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5579{
a50b1753 5580 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5581 unsigned long int bucket;
5582 unsigned long int val;
5583
fdc90cb4
JJ
5584 /* Ignore indirect symbols. */
5585 if (h->dynindx == -1)
5586 return TRUE;
5587
5588 /* Ignore also local symbols and undefined symbols. */
5589 if (! (*s->bed->elf_hash_symbol) (h))
5590 {
5591 if (h->dynindx >= s->min_dynindx)
5592 h->dynindx = s->local_indx++;
5593 return TRUE;
5594 }
5595
5596 bucket = s->hashval[h->dynindx] % s->bucketcount;
5597 val = (s->hashval[h->dynindx] >> s->shift1)
5598 & ((s->maskbits >> s->shift1) - 1);
5599 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5600 s->bitmask[val]
5601 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5602 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5603 if (s->counts[bucket] == 1)
5604 /* Last element terminates the chain. */
5605 val |= 1;
5606 bfd_put_32 (s->output_bfd, val,
5607 s->contents + (s->indx[bucket] - s->symindx) * 4);
5608 --s->counts[bucket];
5609 h->dynindx = s->indx[bucket]++;
5610 return TRUE;
5611}
5612
5613/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5614
5615bfd_boolean
5616_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5617{
5618 return !(h->forced_local
5619 || h->root.type == bfd_link_hash_undefined
5620 || h->root.type == bfd_link_hash_undefweak
5621 || ((h->root.type == bfd_link_hash_defined
5622 || h->root.type == bfd_link_hash_defweak)
5623 && h->root.u.def.section->output_section == NULL));
5624}
5625
5a580b3a
AM
5626/* Array used to determine the number of hash table buckets to use
5627 based on the number of symbols there are. If there are fewer than
5628 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5629 fewer than 37 we use 17 buckets, and so forth. We never use more
5630 than 32771 buckets. */
5631
5632static const size_t elf_buckets[] =
5633{
5634 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5635 16411, 32771, 0
5636};
5637
5638/* Compute bucket count for hashing table. We do not use a static set
5639 of possible tables sizes anymore. Instead we determine for all
5640 possible reasonable sizes of the table the outcome (i.e., the
5641 number of collisions etc) and choose the best solution. The
5642 weighting functions are not too simple to allow the table to grow
5643 without bounds. Instead one of the weighting factors is the size.
5644 Therefore the result is always a good payoff between few collisions
5645 (= short chain lengths) and table size. */
5646static size_t
b20dd2ce 5647compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5648 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5649 unsigned long int nsyms,
5650 int gnu_hash)
5a580b3a 5651{
5a580b3a 5652 size_t best_size = 0;
5a580b3a 5653 unsigned long int i;
5a580b3a 5654
5a580b3a
AM
5655 /* We have a problem here. The following code to optimize the table
5656 size requires an integer type with more the 32 bits. If
5657 BFD_HOST_U_64_BIT is set we know about such a type. */
5658#ifdef BFD_HOST_U_64_BIT
5659 if (info->optimize)
5660 {
5a580b3a
AM
5661 size_t minsize;
5662 size_t maxsize;
5663 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5664 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5665 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5666 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5667 unsigned long int *counts;
d40f3da9 5668 bfd_size_type amt;
0883b6e0 5669 unsigned int no_improvement_count = 0;
5a580b3a
AM
5670
5671 /* Possible optimization parameters: if we have NSYMS symbols we say
5672 that the hashing table must at least have NSYMS/4 and at most
5673 2*NSYMS buckets. */
5674 minsize = nsyms / 4;
5675 if (minsize == 0)
5676 minsize = 1;
5677 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5678 if (gnu_hash)
5679 {
5680 if (minsize < 2)
5681 minsize = 2;
5682 if ((best_size & 31) == 0)
5683 ++best_size;
5684 }
5a580b3a
AM
5685
5686 /* Create array where we count the collisions in. We must use bfd_malloc
5687 since the size could be large. */
5688 amt = maxsize;
5689 amt *= sizeof (unsigned long int);
a50b1753 5690 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5691 if (counts == NULL)
fdc90cb4 5692 return 0;
5a580b3a
AM
5693
5694 /* Compute the "optimal" size for the hash table. The criteria is a
5695 minimal chain length. The minor criteria is (of course) the size
5696 of the table. */
5697 for (i = minsize; i < maxsize; ++i)
5698 {
5699 /* Walk through the array of hashcodes and count the collisions. */
5700 BFD_HOST_U_64_BIT max;
5701 unsigned long int j;
5702 unsigned long int fact;
5703
fdc90cb4
JJ
5704 if (gnu_hash && (i & 31) == 0)
5705 continue;
5706
5a580b3a
AM
5707 memset (counts, '\0', i * sizeof (unsigned long int));
5708
5709 /* Determine how often each hash bucket is used. */
5710 for (j = 0; j < nsyms; ++j)
5711 ++counts[hashcodes[j] % i];
5712
5713 /* For the weight function we need some information about the
5714 pagesize on the target. This is information need not be 100%
5715 accurate. Since this information is not available (so far) we
5716 define it here to a reasonable default value. If it is crucial
5717 to have a better value some day simply define this value. */
5718# ifndef BFD_TARGET_PAGESIZE
5719# define BFD_TARGET_PAGESIZE (4096)
5720# endif
5721
fdc90cb4
JJ
5722 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5723 and the chains. */
5724 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5725
5726# if 1
5727 /* Variant 1: optimize for short chains. We add the squares
5728 of all the chain lengths (which favors many small chain
5729 over a few long chains). */
5730 for (j = 0; j < i; ++j)
5731 max += counts[j] * counts[j];
5732
5733 /* This adds penalties for the overall size of the table. */
fdc90cb4 5734 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5735 max *= fact * fact;
5736# else
5737 /* Variant 2: Optimize a lot more for small table. Here we
5738 also add squares of the size but we also add penalties for
5739 empty slots (the +1 term). */
5740 for (j = 0; j < i; ++j)
5741 max += (1 + counts[j]) * (1 + counts[j]);
5742
5743 /* The overall size of the table is considered, but not as
5744 strong as in variant 1, where it is squared. */
fdc90cb4 5745 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5746 max *= fact;
5747# endif
5748
5749 /* Compare with current best results. */
5750 if (max < best_chlen)
5751 {
5752 best_chlen = max;
5753 best_size = i;
ca4be51c 5754 no_improvement_count = 0;
5a580b3a 5755 }
0883b6e0
NC
5756 /* PR 11843: Avoid futile long searches for the best bucket size
5757 when there are a large number of symbols. */
5758 else if (++no_improvement_count == 100)
5759 break;
5a580b3a
AM
5760 }
5761
5762 free (counts);
5763 }
5764 else
5765#endif /* defined (BFD_HOST_U_64_BIT) */
5766 {
5767 /* This is the fallback solution if no 64bit type is available or if we
5768 are not supposed to spend much time on optimizations. We select the
5769 bucket count using a fixed set of numbers. */
5770 for (i = 0; elf_buckets[i] != 0; i++)
5771 {
5772 best_size = elf_buckets[i];
fdc90cb4 5773 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5774 break;
5775 }
fdc90cb4
JJ
5776 if (gnu_hash && best_size < 2)
5777 best_size = 2;
5a580b3a
AM
5778 }
5779
5a580b3a
AM
5780 return best_size;
5781}
5782
d0bf826b
AM
5783/* Size any SHT_GROUP section for ld -r. */
5784
5785bfd_boolean
5786_bfd_elf_size_group_sections (struct bfd_link_info *info)
5787{
5788 bfd *ibfd;
5789
c72f2fb2 5790 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5791 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5792 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5793 return FALSE;
5794 return TRUE;
5795}
5796
04c3a755
NS
5797/* Set a default stack segment size. The value in INFO wins. If it
5798 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5799 undefined it is initialized. */
5800
5801bfd_boolean
5802bfd_elf_stack_segment_size (bfd *output_bfd,
5803 struct bfd_link_info *info,
5804 const char *legacy_symbol,
5805 bfd_vma default_size)
5806{
5807 struct elf_link_hash_entry *h = NULL;
5808
5809 /* Look for legacy symbol. */
5810 if (legacy_symbol)
5811 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5812 FALSE, FALSE, FALSE);
5813 if (h && (h->root.type == bfd_link_hash_defined
5814 || h->root.type == bfd_link_hash_defweak)
5815 && h->def_regular
5816 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5817 {
5818 /* The symbol has no type if specified on the command line. */
5819 h->type = STT_OBJECT;
5820 if (info->stacksize)
695344c0 5821 /* xgettext:c-format */
4eca0228
AM
5822 _bfd_error_handler (_("%B: stack size specified and %s set"),
5823 output_bfd, legacy_symbol);
04c3a755 5824 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5825 /* xgettext:c-format */
4eca0228
AM
5826 _bfd_error_handler (_("%B: %s not absolute"),
5827 output_bfd, legacy_symbol);
04c3a755
NS
5828 else
5829 info->stacksize = h->root.u.def.value;
5830 }
5831
5832 if (!info->stacksize)
5833 /* If the user didn't set a size, or explicitly inhibit the
5834 size, set it now. */
5835 info->stacksize = default_size;
5836
5837 /* Provide the legacy symbol, if it is referenced. */
5838 if (h && (h->root.type == bfd_link_hash_undefined
5839 || h->root.type == bfd_link_hash_undefweak))
5840 {
5841 struct bfd_link_hash_entry *bh = NULL;
5842
5843 if (!(_bfd_generic_link_add_one_symbol
5844 (info, output_bfd, legacy_symbol,
5845 BSF_GLOBAL, bfd_abs_section_ptr,
5846 info->stacksize >= 0 ? info->stacksize : 0,
5847 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5848 return FALSE;
5849
5850 h = (struct elf_link_hash_entry *) bh;
5851 h->def_regular = 1;
5852 h->type = STT_OBJECT;
5853 }
5854
5855 return TRUE;
5856}
5857
5a580b3a
AM
5858/* Set up the sizes and contents of the ELF dynamic sections. This is
5859 called by the ELF linker emulation before_allocation routine. We
5860 must set the sizes of the sections before the linker sets the
5861 addresses of the various sections. */
5862
5863bfd_boolean
5864bfd_elf_size_dynamic_sections (bfd *output_bfd,
5865 const char *soname,
5866 const char *rpath,
5867 const char *filter_shlib,
7ee314fa
AM
5868 const char *audit,
5869 const char *depaudit,
5a580b3a
AM
5870 const char * const *auxiliary_filters,
5871 struct bfd_link_info *info,
fd91d419 5872 asection **sinterpptr)
5a580b3a 5873{
ef53be89 5874 size_t soname_indx;
5a580b3a
AM
5875 bfd *dynobj;
5876 const struct elf_backend_data *bed;
28caa186 5877 struct elf_info_failed asvinfo;
5a580b3a
AM
5878
5879 *sinterpptr = NULL;
5880
ef53be89 5881 soname_indx = (size_t) -1;
5a580b3a
AM
5882
5883 if (!is_elf_hash_table (info->hash))
5884 return TRUE;
5885
6bfdb61b 5886 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5887
5888 /* Any syms created from now on start with -1 in
5889 got.refcount/offset and plt.refcount/offset. */
5890 elf_hash_table (info)->init_got_refcount
5891 = elf_hash_table (info)->init_got_offset;
5892 elf_hash_table (info)->init_plt_refcount
5893 = elf_hash_table (info)->init_plt_offset;
5894
0e1862bb 5895 if (bfd_link_relocatable (info)
04c3a755
NS
5896 && !_bfd_elf_size_group_sections (info))
5897 return FALSE;
5898
5899 /* The backend may have to create some sections regardless of whether
5900 we're dynamic or not. */
5901 if (bed->elf_backend_always_size_sections
5902 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5903 return FALSE;
5904
5905 /* Determine any GNU_STACK segment requirements, after the backend
5906 has had a chance to set a default segment size. */
5a580b3a 5907 if (info->execstack)
12bd6957 5908 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5909 else if (info->noexecstack)
12bd6957 5910 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5911 else
5912 {
5913 bfd *inputobj;
5914 asection *notesec = NULL;
5915 int exec = 0;
5916
5917 for (inputobj = info->input_bfds;
5918 inputobj;
c72f2fb2 5919 inputobj = inputobj->link.next)
5a580b3a
AM
5920 {
5921 asection *s;
5922
a92c088a
L
5923 if (inputobj->flags
5924 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5925 continue;
5926 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5927 if (s)
5928 {
5929 if (s->flags & SEC_CODE)
5930 exec = PF_X;
5931 notesec = s;
5932 }
6bfdb61b 5933 else if (bed->default_execstack)
5a580b3a
AM
5934 exec = PF_X;
5935 }
04c3a755 5936 if (notesec || info->stacksize > 0)
12bd6957 5937 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5938 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5939 && notesec->output_section != bfd_abs_section_ptr)
5940 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5941 }
5942
5a580b3a
AM
5943 dynobj = elf_hash_table (info)->dynobj;
5944
9a2a56cc 5945 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5946 {
5947 struct elf_info_failed eif;
5948 struct elf_link_hash_entry *h;
5949 asection *dynstr;
5950 struct bfd_elf_version_tree *t;
5951 struct bfd_elf_version_expr *d;
046183de 5952 asection *s;
5a580b3a
AM
5953 bfd_boolean all_defined;
5954
3d4d4302 5955 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5956 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5957
5958 if (soname != NULL)
5959 {
5960 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5961 soname, TRUE);
ef53be89 5962 if (soname_indx == (size_t) -1
5a580b3a
AM
5963 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5964 return FALSE;
5965 }
5966
5967 if (info->symbolic)
5968 {
5969 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5970 return FALSE;
5971 info->flags |= DF_SYMBOLIC;
5972 }
5973
5974 if (rpath != NULL)
5975 {
ef53be89 5976 size_t indx;
b1b00fcc 5977 bfd_vma tag;
5a580b3a
AM
5978
5979 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5980 TRUE);
ef53be89 5981 if (indx == (size_t) -1)
5a580b3a
AM
5982 return FALSE;
5983
b1b00fcc
MF
5984 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5985 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5986 return FALSE;
5a580b3a
AM
5987 }
5988
5989 if (filter_shlib != NULL)
5990 {
ef53be89 5991 size_t indx;
5a580b3a
AM
5992
5993 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5994 filter_shlib, TRUE);
ef53be89 5995 if (indx == (size_t) -1
5a580b3a
AM
5996 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5997 return FALSE;
5998 }
5999
6000 if (auxiliary_filters != NULL)
6001 {
6002 const char * const *p;
6003
6004 for (p = auxiliary_filters; *p != NULL; p++)
6005 {
ef53be89 6006 size_t indx;
5a580b3a
AM
6007
6008 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6009 *p, TRUE);
ef53be89 6010 if (indx == (size_t) -1
5a580b3a
AM
6011 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6012 return FALSE;
6013 }
6014 }
6015
7ee314fa
AM
6016 if (audit != NULL)
6017 {
ef53be89 6018 size_t indx;
7ee314fa
AM
6019
6020 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6021 TRUE);
ef53be89 6022 if (indx == (size_t) -1
7ee314fa
AM
6023 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6024 return FALSE;
6025 }
6026
6027 if (depaudit != NULL)
6028 {
ef53be89 6029 size_t indx;
7ee314fa
AM
6030
6031 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6032 TRUE);
ef53be89 6033 if (indx == (size_t) -1
7ee314fa
AM
6034 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6035 return FALSE;
6036 }
6037
5a580b3a 6038 eif.info = info;
5a580b3a
AM
6039 eif.failed = FALSE;
6040
6041 /* If we are supposed to export all symbols into the dynamic symbol
6042 table (this is not the normal case), then do so. */
55255dae 6043 if (info->export_dynamic
0e1862bb 6044 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6045 {
6046 elf_link_hash_traverse (elf_hash_table (info),
6047 _bfd_elf_export_symbol,
6048 &eif);
6049 if (eif.failed)
6050 return FALSE;
6051 }
6052
6053 /* Make all global versions with definition. */
fd91d419 6054 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6055 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6056 if (!d->symver && d->literal)
5a580b3a
AM
6057 {
6058 const char *verstr, *name;
6059 size_t namelen, verlen, newlen;
93252b1c 6060 char *newname, *p, leading_char;
5a580b3a
AM
6061 struct elf_link_hash_entry *newh;
6062
93252b1c 6063 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6064 name = d->pattern;
93252b1c 6065 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6066 verstr = t->name;
6067 verlen = strlen (verstr);
6068 newlen = namelen + verlen + 3;
6069
a50b1753 6070 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6071 if (newname == NULL)
6072 return FALSE;
93252b1c
MF
6073 newname[0] = leading_char;
6074 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6075
6076 /* Check the hidden versioned definition. */
6077 p = newname + namelen;
6078 *p++ = ELF_VER_CHR;
6079 memcpy (p, verstr, verlen + 1);
6080 newh = elf_link_hash_lookup (elf_hash_table (info),
6081 newname, FALSE, FALSE,
6082 FALSE);
6083 if (newh == NULL
6084 || (newh->root.type != bfd_link_hash_defined
6085 && newh->root.type != bfd_link_hash_defweak))
6086 {
6087 /* Check the default versioned definition. */
6088 *p++ = ELF_VER_CHR;
6089 memcpy (p, verstr, verlen + 1);
6090 newh = elf_link_hash_lookup (elf_hash_table (info),
6091 newname, FALSE, FALSE,
6092 FALSE);
6093 }
6094 free (newname);
6095
6096 /* Mark this version if there is a definition and it is
6097 not defined in a shared object. */
6098 if (newh != NULL
f5385ebf 6099 && !newh->def_dynamic
5a580b3a
AM
6100 && (newh->root.type == bfd_link_hash_defined
6101 || newh->root.type == bfd_link_hash_defweak))
6102 d->symver = 1;
6103 }
6104
6105 /* Attach all the symbols to their version information. */
5a580b3a 6106 asvinfo.info = info;
5a580b3a
AM
6107 asvinfo.failed = FALSE;
6108
6109 elf_link_hash_traverse (elf_hash_table (info),
6110 _bfd_elf_link_assign_sym_version,
6111 &asvinfo);
6112 if (asvinfo.failed)
6113 return FALSE;
6114
6115 if (!info->allow_undefined_version)
6116 {
6117 /* Check if all global versions have a definition. */
6118 all_defined = TRUE;
fd91d419 6119 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6120 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6121 if (d->literal && !d->symver && !d->script)
5a580b3a 6122 {
4eca0228 6123 _bfd_error_handler
5a580b3a
AM
6124 (_("%s: undefined version: %s"),
6125 d->pattern, t->name);
6126 all_defined = FALSE;
6127 }
6128
6129 if (!all_defined)
6130 {
6131 bfd_set_error (bfd_error_bad_value);
6132 return FALSE;
6133 }
6134 }
6135
6136 /* Find all symbols which were defined in a dynamic object and make
6137 the backend pick a reasonable value for them. */
6138 elf_link_hash_traverse (elf_hash_table (info),
6139 _bfd_elf_adjust_dynamic_symbol,
6140 &eif);
6141 if (eif.failed)
6142 return FALSE;
6143
6144 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6145 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6146 now so that we know the final size of the .dynamic section. */
6147
6148 /* If there are initialization and/or finalization functions to
6149 call then add the corresponding DT_INIT/DT_FINI entries. */
6150 h = (info->init_function
6151 ? elf_link_hash_lookup (elf_hash_table (info),
6152 info->init_function, FALSE,
6153 FALSE, FALSE)
6154 : NULL);
6155 if (h != NULL
f5385ebf
AM
6156 && (h->ref_regular
6157 || h->def_regular))
5a580b3a
AM
6158 {
6159 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6160 return FALSE;
6161 }
6162 h = (info->fini_function
6163 ? elf_link_hash_lookup (elf_hash_table (info),
6164 info->fini_function, FALSE,
6165 FALSE, FALSE)
6166 : NULL);
6167 if (h != NULL
f5385ebf
AM
6168 && (h->ref_regular
6169 || h->def_regular))
5a580b3a
AM
6170 {
6171 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6172 return FALSE;
6173 }
6174
046183de
AM
6175 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6176 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6177 {
6178 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6179 if (! bfd_link_executable (info))
5a580b3a
AM
6180 {
6181 bfd *sub;
6182 asection *o;
6183
6184 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6185 sub = sub->link.next)
3fcd97f1
JJ
6186 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6187 for (o = sub->sections; o != NULL; o = o->next)
6188 if (elf_section_data (o)->this_hdr.sh_type
6189 == SHT_PREINIT_ARRAY)
6190 {
4eca0228 6191 _bfd_error_handler
3fcd97f1
JJ
6192 (_("%B: .preinit_array section is not allowed in DSO"),
6193 sub);
6194 break;
6195 }
5a580b3a
AM
6196
6197 bfd_set_error (bfd_error_nonrepresentable_section);
6198 return FALSE;
6199 }
6200
6201 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6202 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6203 return FALSE;
6204 }
046183de
AM
6205 s = bfd_get_section_by_name (output_bfd, ".init_array");
6206 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6207 {
6208 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6209 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6210 return FALSE;
6211 }
046183de
AM
6212 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6213 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6214 {
6215 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6216 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6217 return FALSE;
6218 }
6219
3d4d4302 6220 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6221 /* If .dynstr is excluded from the link, we don't want any of
6222 these tags. Strictly, we should be checking each section
6223 individually; This quick check covers for the case where
6224 someone does a /DISCARD/ : { *(*) }. */
6225 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6226 {
6227 bfd_size_type strsize;
6228
6229 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6230 if ((info->emit_hash
6231 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6232 || (info->emit_gnu_hash
6233 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6234 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6235 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6236 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6237 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6238 bed->s->sizeof_sym))
6239 return FALSE;
6240 }
6241 }
6242
de231f20
CM
6243 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6244 return FALSE;
6245
5a580b3a
AM
6246 /* The backend must work out the sizes of all the other dynamic
6247 sections. */
9a2a56cc
AM
6248 if (dynobj != NULL
6249 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6250 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6251 return FALSE;
6252
9a2a56cc 6253 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6254 {
554220db 6255 unsigned long section_sym_count;
fd91d419 6256 struct bfd_elf_version_tree *verdefs;
5a580b3a 6257 asection *s;
5a580b3a
AM
6258
6259 /* Set up the version definition section. */
3d4d4302 6260 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6261 BFD_ASSERT (s != NULL);
6262
6263 /* We may have created additional version definitions if we are
6264 just linking a regular application. */
fd91d419 6265 verdefs = info->version_info;
5a580b3a
AM
6266
6267 /* Skip anonymous version tag. */
6268 if (verdefs != NULL && verdefs->vernum == 0)
6269 verdefs = verdefs->next;
6270
3e3b46e5 6271 if (verdefs == NULL && !info->create_default_symver)
8423293d 6272 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6273 else
6274 {
6275 unsigned int cdefs;
6276 bfd_size_type size;
6277 struct bfd_elf_version_tree *t;
6278 bfd_byte *p;
6279 Elf_Internal_Verdef def;
6280 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6281 struct bfd_link_hash_entry *bh;
6282 struct elf_link_hash_entry *h;
6283 const char *name;
5a580b3a
AM
6284
6285 cdefs = 0;
6286 size = 0;
6287
6288 /* Make space for the base version. */
6289 size += sizeof (Elf_External_Verdef);
6290 size += sizeof (Elf_External_Verdaux);
6291 ++cdefs;
6292
3e3b46e5
PB
6293 /* Make space for the default version. */
6294 if (info->create_default_symver)
6295 {
6296 size += sizeof (Elf_External_Verdef);
6297 ++cdefs;
6298 }
6299
5a580b3a
AM
6300 for (t = verdefs; t != NULL; t = t->next)
6301 {
6302 struct bfd_elf_version_deps *n;
6303
a6cc6b3b
RO
6304 /* Don't emit base version twice. */
6305 if (t->vernum == 0)
6306 continue;
6307
5a580b3a
AM
6308 size += sizeof (Elf_External_Verdef);
6309 size += sizeof (Elf_External_Verdaux);
6310 ++cdefs;
6311
6312 for (n = t->deps; n != NULL; n = n->next)
6313 size += sizeof (Elf_External_Verdaux);
6314 }
6315
eea6121a 6316 s->size = size;
a50b1753 6317 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6318 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6319 return FALSE;
6320
6321 /* Fill in the version definition section. */
6322
6323 p = s->contents;
6324
6325 def.vd_version = VER_DEF_CURRENT;
6326 def.vd_flags = VER_FLG_BASE;
6327 def.vd_ndx = 1;
6328 def.vd_cnt = 1;
3e3b46e5
PB
6329 if (info->create_default_symver)
6330 {
6331 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6332 def.vd_next = sizeof (Elf_External_Verdef);
6333 }
6334 else
6335 {
6336 def.vd_aux = sizeof (Elf_External_Verdef);
6337 def.vd_next = (sizeof (Elf_External_Verdef)
6338 + sizeof (Elf_External_Verdaux));
6339 }
5a580b3a 6340
ef53be89 6341 if (soname_indx != (size_t) -1)
5a580b3a
AM
6342 {
6343 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6344 soname_indx);
6345 def.vd_hash = bfd_elf_hash (soname);
6346 defaux.vda_name = soname_indx;
3e3b46e5 6347 name = soname;
5a580b3a
AM
6348 }
6349 else
6350 {
ef53be89 6351 size_t indx;
5a580b3a 6352
06084812 6353 name = lbasename (output_bfd->filename);
5a580b3a
AM
6354 def.vd_hash = bfd_elf_hash (name);
6355 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6356 name, FALSE);
ef53be89 6357 if (indx == (size_t) -1)
5a580b3a
AM
6358 return FALSE;
6359 defaux.vda_name = indx;
6360 }
6361 defaux.vda_next = 0;
6362
6363 _bfd_elf_swap_verdef_out (output_bfd, &def,
6364 (Elf_External_Verdef *) p);
6365 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6366 if (info->create_default_symver)
6367 {
6368 /* Add a symbol representing this version. */
6369 bh = NULL;
6370 if (! (_bfd_generic_link_add_one_symbol
6371 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6372 0, NULL, FALSE,
6373 get_elf_backend_data (dynobj)->collect, &bh)))
6374 return FALSE;
6375 h = (struct elf_link_hash_entry *) bh;
6376 h->non_elf = 0;
6377 h->def_regular = 1;
6378 h->type = STT_OBJECT;
6379 h->verinfo.vertree = NULL;
6380
6381 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6382 return FALSE;
6383
6384 /* Create a duplicate of the base version with the same
6385 aux block, but different flags. */
6386 def.vd_flags = 0;
6387 def.vd_ndx = 2;
6388 def.vd_aux = sizeof (Elf_External_Verdef);
6389 if (verdefs)
6390 def.vd_next = (sizeof (Elf_External_Verdef)
6391 + sizeof (Elf_External_Verdaux));
6392 else
6393 def.vd_next = 0;
6394 _bfd_elf_swap_verdef_out (output_bfd, &def,
6395 (Elf_External_Verdef *) p);
6396 p += sizeof (Elf_External_Verdef);
6397 }
5a580b3a
AM
6398 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6399 (Elf_External_Verdaux *) p);
6400 p += sizeof (Elf_External_Verdaux);
6401
6402 for (t = verdefs; t != NULL; t = t->next)
6403 {
6404 unsigned int cdeps;
6405 struct bfd_elf_version_deps *n;
5a580b3a 6406
a6cc6b3b
RO
6407 /* Don't emit the base version twice. */
6408 if (t->vernum == 0)
6409 continue;
6410
5a580b3a
AM
6411 cdeps = 0;
6412 for (n = t->deps; n != NULL; n = n->next)
6413 ++cdeps;
6414
6415 /* Add a symbol representing this version. */
6416 bh = NULL;
6417 if (! (_bfd_generic_link_add_one_symbol
6418 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6419 0, NULL, FALSE,
6420 get_elf_backend_data (dynobj)->collect, &bh)))
6421 return FALSE;
6422 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6423 h->non_elf = 0;
6424 h->def_regular = 1;
5a580b3a
AM
6425 h->type = STT_OBJECT;
6426 h->verinfo.vertree = t;
6427
c152c796 6428 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6429 return FALSE;
6430
6431 def.vd_version = VER_DEF_CURRENT;
6432 def.vd_flags = 0;
6433 if (t->globals.list == NULL
6434 && t->locals.list == NULL
6435 && ! t->used)
6436 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6437 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6438 def.vd_cnt = cdeps + 1;
6439 def.vd_hash = bfd_elf_hash (t->name);
6440 def.vd_aux = sizeof (Elf_External_Verdef);
6441 def.vd_next = 0;
a6cc6b3b
RO
6442
6443 /* If a basever node is next, it *must* be the last node in
6444 the chain, otherwise Verdef construction breaks. */
6445 if (t->next != NULL && t->next->vernum == 0)
6446 BFD_ASSERT (t->next->next == NULL);
6447
6448 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6449 def.vd_next = (sizeof (Elf_External_Verdef)
6450 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6451
6452 _bfd_elf_swap_verdef_out (output_bfd, &def,
6453 (Elf_External_Verdef *) p);
6454 p += sizeof (Elf_External_Verdef);
6455
6456 defaux.vda_name = h->dynstr_index;
6457 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6458 h->dynstr_index);
6459 defaux.vda_next = 0;
6460 if (t->deps != NULL)
6461 defaux.vda_next = sizeof (Elf_External_Verdaux);
6462 t->name_indx = defaux.vda_name;
6463
6464 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6465 (Elf_External_Verdaux *) p);
6466 p += sizeof (Elf_External_Verdaux);
6467
6468 for (n = t->deps; n != NULL; n = n->next)
6469 {
6470 if (n->version_needed == NULL)
6471 {
6472 /* This can happen if there was an error in the
6473 version script. */
6474 defaux.vda_name = 0;
6475 }
6476 else
6477 {
6478 defaux.vda_name = n->version_needed->name_indx;
6479 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6480 defaux.vda_name);
6481 }
6482 if (n->next == NULL)
6483 defaux.vda_next = 0;
6484 else
6485 defaux.vda_next = sizeof (Elf_External_Verdaux);
6486
6487 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6488 (Elf_External_Verdaux *) p);
6489 p += sizeof (Elf_External_Verdaux);
6490 }
6491 }
6492
6493 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6494 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6495 return FALSE;
6496
6497 elf_tdata (output_bfd)->cverdefs = cdefs;
6498 }
6499
6500 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6501 {
6502 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6503 return FALSE;
6504 }
6505 else if (info->flags & DF_BIND_NOW)
6506 {
6507 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6508 return FALSE;
6509 }
6510
6511 if (info->flags_1)
6512 {
0e1862bb 6513 if (bfd_link_executable (info))
5a580b3a
AM
6514 info->flags_1 &= ~ (DF_1_INITFIRST
6515 | DF_1_NODELETE
6516 | DF_1_NOOPEN);
6517 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6518 return FALSE;
6519 }
6520
6521 /* Work out the size of the version reference section. */
6522
3d4d4302 6523 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6524 BFD_ASSERT (s != NULL);
6525 {
6526 struct elf_find_verdep_info sinfo;
6527
5a580b3a
AM
6528 sinfo.info = info;
6529 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6530 if (sinfo.vers == 0)
6531 sinfo.vers = 1;
6532 sinfo.failed = FALSE;
6533
6534 elf_link_hash_traverse (elf_hash_table (info),
6535 _bfd_elf_link_find_version_dependencies,
6536 &sinfo);
14b1c01e
AM
6537 if (sinfo.failed)
6538 return FALSE;
5a580b3a
AM
6539
6540 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6541 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6542 else
6543 {
6544 Elf_Internal_Verneed *t;
6545 unsigned int size;
6546 unsigned int crefs;
6547 bfd_byte *p;
6548
a6cc6b3b 6549 /* Build the version dependency section. */
5a580b3a
AM
6550 size = 0;
6551 crefs = 0;
6552 for (t = elf_tdata (output_bfd)->verref;
6553 t != NULL;
6554 t = t->vn_nextref)
6555 {
6556 Elf_Internal_Vernaux *a;
6557
6558 size += sizeof (Elf_External_Verneed);
6559 ++crefs;
6560 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6561 size += sizeof (Elf_External_Vernaux);
6562 }
6563
eea6121a 6564 s->size = size;
a50b1753 6565 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6566 if (s->contents == NULL)
6567 return FALSE;
6568
6569 p = s->contents;
6570 for (t = elf_tdata (output_bfd)->verref;
6571 t != NULL;
6572 t = t->vn_nextref)
6573 {
6574 unsigned int caux;
6575 Elf_Internal_Vernaux *a;
ef53be89 6576 size_t indx;
5a580b3a
AM
6577
6578 caux = 0;
6579 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6580 ++caux;
6581
6582 t->vn_version = VER_NEED_CURRENT;
6583 t->vn_cnt = caux;
6584 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6585 elf_dt_name (t->vn_bfd) != NULL
6586 ? elf_dt_name (t->vn_bfd)
06084812 6587 : lbasename (t->vn_bfd->filename),
5a580b3a 6588 FALSE);
ef53be89 6589 if (indx == (size_t) -1)
5a580b3a
AM
6590 return FALSE;
6591 t->vn_file = indx;
6592 t->vn_aux = sizeof (Elf_External_Verneed);
6593 if (t->vn_nextref == NULL)
6594 t->vn_next = 0;
6595 else
6596 t->vn_next = (sizeof (Elf_External_Verneed)
6597 + caux * sizeof (Elf_External_Vernaux));
6598
6599 _bfd_elf_swap_verneed_out (output_bfd, t,
6600 (Elf_External_Verneed *) p);
6601 p += sizeof (Elf_External_Verneed);
6602
6603 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6604 {
6605 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6606 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6607 a->vna_nodename, FALSE);
ef53be89 6608 if (indx == (size_t) -1)
5a580b3a
AM
6609 return FALSE;
6610 a->vna_name = indx;
6611 if (a->vna_nextptr == NULL)
6612 a->vna_next = 0;
6613 else
6614 a->vna_next = sizeof (Elf_External_Vernaux);
6615
6616 _bfd_elf_swap_vernaux_out (output_bfd, a,
6617 (Elf_External_Vernaux *) p);
6618 p += sizeof (Elf_External_Vernaux);
6619 }
6620 }
6621
6622 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6623 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6624 return FALSE;
6625
6626 elf_tdata (output_bfd)->cverrefs = crefs;
6627 }
6628 }
6629
8423293d
AM
6630 if ((elf_tdata (output_bfd)->cverrefs == 0
6631 && elf_tdata (output_bfd)->cverdefs == 0)
6632 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6633 &section_sym_count) == 0)
6634 {
3d4d4302 6635 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6636 s->flags |= SEC_EXCLUDE;
6637 }
6638 }
6639 return TRUE;
6640}
6641
74541ad4
AM
6642/* Find the first non-excluded output section. We'll use its
6643 section symbol for some emitted relocs. */
6644void
6645_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6646{
6647 asection *s;
6648
6649 for (s = output_bfd->sections; s != NULL; s = s->next)
6650 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6651 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6652 {
6653 elf_hash_table (info)->text_index_section = s;
6654 break;
6655 }
6656}
6657
6658/* Find two non-excluded output sections, one for code, one for data.
6659 We'll use their section symbols for some emitted relocs. */
6660void
6661_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6662{
6663 asection *s;
6664
266b05cf
DJ
6665 /* Data first, since setting text_index_section changes
6666 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6667 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6668 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6669 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6670 {
266b05cf 6671 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6672 break;
6673 }
6674
6675 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6676 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6677 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6678 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6679 {
266b05cf 6680 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6681 break;
6682 }
6683
6684 if (elf_hash_table (info)->text_index_section == NULL)
6685 elf_hash_table (info)->text_index_section
6686 = elf_hash_table (info)->data_index_section;
6687}
6688
8423293d
AM
6689bfd_boolean
6690bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6691{
74541ad4
AM
6692 const struct elf_backend_data *bed;
6693
8423293d
AM
6694 if (!is_elf_hash_table (info->hash))
6695 return TRUE;
6696
74541ad4
AM
6697 bed = get_elf_backend_data (output_bfd);
6698 (*bed->elf_backend_init_index_section) (output_bfd, info);
6699
8423293d
AM
6700 if (elf_hash_table (info)->dynamic_sections_created)
6701 {
6702 bfd *dynobj;
8423293d
AM
6703 asection *s;
6704 bfd_size_type dynsymcount;
6705 unsigned long section_sym_count;
8423293d
AM
6706 unsigned int dtagcount;
6707
6708 dynobj = elf_hash_table (info)->dynobj;
6709
5a580b3a
AM
6710 /* Assign dynsym indicies. In a shared library we generate a
6711 section symbol for each output section, which come first.
6712 Next come all of the back-end allocated local dynamic syms,
6713 followed by the rest of the global symbols. */
6714
554220db
AM
6715 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6716 &section_sym_count);
5a580b3a
AM
6717
6718 /* Work out the size of the symbol version section. */
3d4d4302 6719 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6720 BFD_ASSERT (s != NULL);
d5486c43 6721 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6722 {
eea6121a 6723 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6724 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6725 if (s->contents == NULL)
6726 return FALSE;
6727
6728 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6729 return FALSE;
6730 }
6731
6732 /* Set the size of the .dynsym and .hash sections. We counted
6733 the number of dynamic symbols in elf_link_add_object_symbols.
6734 We will build the contents of .dynsym and .hash when we build
6735 the final symbol table, because until then we do not know the
6736 correct value to give the symbols. We built the .dynstr
6737 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6738 s = elf_hash_table (info)->dynsym;
5a580b3a 6739 BFD_ASSERT (s != NULL);
eea6121a 6740 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6741
d5486c43
L
6742 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6743 if (s->contents == NULL)
6744 return FALSE;
5a580b3a 6745
d5486c43
L
6746 /* The first entry in .dynsym is a dummy symbol. Clear all the
6747 section syms, in case we don't output them all. */
6748 ++section_sym_count;
6749 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6750
fdc90cb4
JJ
6751 elf_hash_table (info)->bucketcount = 0;
6752
5a580b3a
AM
6753 /* Compute the size of the hashing table. As a side effect this
6754 computes the hash values for all the names we export. */
fdc90cb4
JJ
6755 if (info->emit_hash)
6756 {
6757 unsigned long int *hashcodes;
14b1c01e 6758 struct hash_codes_info hashinf;
fdc90cb4
JJ
6759 bfd_size_type amt;
6760 unsigned long int nsyms;
6761 size_t bucketcount;
6762 size_t hash_entry_size;
6763
6764 /* Compute the hash values for all exported symbols. At the same
6765 time store the values in an array so that we could use them for
6766 optimizations. */
6767 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6768 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6769 if (hashcodes == NULL)
6770 return FALSE;
14b1c01e
AM
6771 hashinf.hashcodes = hashcodes;
6772 hashinf.error = FALSE;
5a580b3a 6773
fdc90cb4
JJ
6774 /* Put all hash values in HASHCODES. */
6775 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6776 elf_collect_hash_codes, &hashinf);
6777 if (hashinf.error)
4dd07732
AM
6778 {
6779 free (hashcodes);
6780 return FALSE;
6781 }
5a580b3a 6782
14b1c01e 6783 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6784 bucketcount
6785 = compute_bucket_count (info, hashcodes, nsyms, 0);
6786 free (hashcodes);
6787
6788 if (bucketcount == 0)
6789 return FALSE;
5a580b3a 6790
fdc90cb4
JJ
6791 elf_hash_table (info)->bucketcount = bucketcount;
6792
3d4d4302 6793 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6794 BFD_ASSERT (s != NULL);
6795 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6796 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6797 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6798 if (s->contents == NULL)
6799 return FALSE;
6800
6801 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6802 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6803 s->contents + hash_entry_size);
6804 }
6805
6806 if (info->emit_gnu_hash)
6807 {
6808 size_t i, cnt;
6809 unsigned char *contents;
6810 struct collect_gnu_hash_codes cinfo;
6811 bfd_size_type amt;
6812 size_t bucketcount;
6813
6814 memset (&cinfo, 0, sizeof (cinfo));
6815
6816 /* Compute the hash values for all exported symbols. At the same
6817 time store the values in an array so that we could use them for
6818 optimizations. */
6819 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6820 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6821 if (cinfo.hashcodes == NULL)
6822 return FALSE;
6823
6824 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6825 cinfo.min_dynindx = -1;
6826 cinfo.output_bfd = output_bfd;
6827 cinfo.bed = bed;
6828
6829 /* Put all hash values in HASHCODES. */
6830 elf_link_hash_traverse (elf_hash_table (info),
6831 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6832 if (cinfo.error)
4dd07732
AM
6833 {
6834 free (cinfo.hashcodes);
6835 return FALSE;
6836 }
fdc90cb4
JJ
6837
6838 bucketcount
6839 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6840
6841 if (bucketcount == 0)
6842 {
6843 free (cinfo.hashcodes);
6844 return FALSE;
6845 }
6846
3d4d4302 6847 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6848 BFD_ASSERT (s != NULL);
6849
6850 if (cinfo.nsyms == 0)
6851 {
6852 /* Empty .gnu.hash section is special. */
6853 BFD_ASSERT (cinfo.min_dynindx == -1);
6854 free (cinfo.hashcodes);
6855 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6856 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6857 if (contents == NULL)
6858 return FALSE;
6859 s->contents = contents;
6860 /* 1 empty bucket. */
6861 bfd_put_32 (output_bfd, 1, contents);
6862 /* SYMIDX above the special symbol 0. */
6863 bfd_put_32 (output_bfd, 1, contents + 4);
6864 /* Just one word for bitmask. */
6865 bfd_put_32 (output_bfd, 1, contents + 8);
6866 /* Only hash fn bloom filter. */
6867 bfd_put_32 (output_bfd, 0, contents + 12);
6868 /* No hashes are valid - empty bitmask. */
6869 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6870 /* No hashes in the only bucket. */
6871 bfd_put_32 (output_bfd, 0,
6872 contents + 16 + bed->s->arch_size / 8);
6873 }
6874 else
6875 {
9e6619e2 6876 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6877 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6878
9e6619e2
AM
6879 x = cinfo.nsyms;
6880 maskbitslog2 = 1;
6881 while ((x >>= 1) != 0)
6882 ++maskbitslog2;
fdc90cb4
JJ
6883 if (maskbitslog2 < 3)
6884 maskbitslog2 = 5;
6885 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6886 maskbitslog2 = maskbitslog2 + 3;
6887 else
6888 maskbitslog2 = maskbitslog2 + 2;
6889 if (bed->s->arch_size == 64)
6890 {
6891 if (maskbitslog2 == 5)
6892 maskbitslog2 = 6;
6893 cinfo.shift1 = 6;
6894 }
6895 else
6896 cinfo.shift1 = 5;
6897 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6898 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6899 cinfo.maskbits = 1 << maskbitslog2;
6900 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6901 amt = bucketcount * sizeof (unsigned long int) * 2;
6902 amt += maskwords * sizeof (bfd_vma);
a50b1753 6903 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6904 if (cinfo.bitmask == NULL)
6905 {
6906 free (cinfo.hashcodes);
6907 return FALSE;
6908 }
6909
a50b1753 6910 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6911 cinfo.indx = cinfo.counts + bucketcount;
6912 cinfo.symindx = dynsymcount - cinfo.nsyms;
6913 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6914
6915 /* Determine how often each hash bucket is used. */
6916 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6917 for (i = 0; i < cinfo.nsyms; ++i)
6918 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6919
6920 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6921 if (cinfo.counts[i] != 0)
6922 {
6923 cinfo.indx[i] = cnt;
6924 cnt += cinfo.counts[i];
6925 }
6926 BFD_ASSERT (cnt == dynsymcount);
6927 cinfo.bucketcount = bucketcount;
6928 cinfo.local_indx = cinfo.min_dynindx;
6929
6930 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6931 s->size += cinfo.maskbits / 8;
a50b1753 6932 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6933 if (contents == NULL)
6934 {
6935 free (cinfo.bitmask);
6936 free (cinfo.hashcodes);
6937 return FALSE;
6938 }
6939
6940 s->contents = contents;
6941 bfd_put_32 (output_bfd, bucketcount, contents);
6942 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6943 bfd_put_32 (output_bfd, maskwords, contents + 8);
6944 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6945 contents += 16 + cinfo.maskbits / 8;
6946
6947 for (i = 0; i < bucketcount; ++i)
6948 {
6949 if (cinfo.counts[i] == 0)
6950 bfd_put_32 (output_bfd, 0, contents);
6951 else
6952 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6953 contents += 4;
6954 }
6955
6956 cinfo.contents = contents;
6957
6958 /* Renumber dynamic symbols, populate .gnu.hash section. */
6959 elf_link_hash_traverse (elf_hash_table (info),
6960 elf_renumber_gnu_hash_syms, &cinfo);
6961
6962 contents = s->contents + 16;
6963 for (i = 0; i < maskwords; ++i)
6964 {
6965 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6966 contents);
6967 contents += bed->s->arch_size / 8;
6968 }
6969
6970 free (cinfo.bitmask);
6971 free (cinfo.hashcodes);
6972 }
6973 }
5a580b3a 6974
3d4d4302 6975 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6976 BFD_ASSERT (s != NULL);
6977
4ad4eba5 6978 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6979
eea6121a 6980 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6981
6982 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6983 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6984 return FALSE;
6985 }
6986
6987 return TRUE;
6988}
4d269e42 6989\f
4d269e42
AM
6990/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6991
6992static void
6993merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6994 asection *sec)
6995{
dbaa2011
AM
6996 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6997 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6998}
6999
7000/* Finish SHF_MERGE section merging. */
7001
7002bfd_boolean
630993ec 7003_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7004{
7005 bfd *ibfd;
7006 asection *sec;
7007
7008 if (!is_elf_hash_table (info->hash))
7009 return FALSE;
7010
c72f2fb2 7011 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7012 if ((ibfd->flags & DYNAMIC) == 0
7013 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7014 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7015 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7016 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7017 if ((sec->flags & SEC_MERGE) != 0
7018 && !bfd_is_abs_section (sec->output_section))
7019 {
7020 struct bfd_elf_section_data *secdata;
7021
7022 secdata = elf_section_data (sec);
630993ec 7023 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7024 &elf_hash_table (info)->merge_info,
7025 sec, &secdata->sec_info))
7026 return FALSE;
7027 else if (secdata->sec_info)
dbaa2011 7028 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7029 }
7030
7031 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7032 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7033 merge_sections_remove_hook);
7034 return TRUE;
7035}
7036
7037/* Create an entry in an ELF linker hash table. */
7038
7039struct bfd_hash_entry *
7040_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7041 struct bfd_hash_table *table,
7042 const char *string)
7043{
7044 /* Allocate the structure if it has not already been allocated by a
7045 subclass. */
7046 if (entry == NULL)
7047 {
a50b1753 7048 entry = (struct bfd_hash_entry *)
ca4be51c 7049 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7050 if (entry == NULL)
7051 return entry;
7052 }
7053
7054 /* Call the allocation method of the superclass. */
7055 entry = _bfd_link_hash_newfunc (entry, table, string);
7056 if (entry != NULL)
7057 {
7058 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7059 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7060
7061 /* Set local fields. */
7062 ret->indx = -1;
7063 ret->dynindx = -1;
7064 ret->got = htab->init_got_refcount;
7065 ret->plt = htab->init_plt_refcount;
7066 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7067 - offsetof (struct elf_link_hash_entry, size)));
7068 /* Assume that we have been called by a non-ELF symbol reader.
7069 This flag is then reset by the code which reads an ELF input
7070 file. This ensures that a symbol created by a non-ELF symbol
7071 reader will have the flag set correctly. */
7072 ret->non_elf = 1;
7073 }
7074
7075 return entry;
7076}
7077
7078/* Copy data from an indirect symbol to its direct symbol, hiding the
7079 old indirect symbol. Also used for copying flags to a weakdef. */
7080
7081void
7082_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7083 struct elf_link_hash_entry *dir,
7084 struct elf_link_hash_entry *ind)
7085{
7086 struct elf_link_hash_table *htab;
7087
7088 /* Copy down any references that we may have already seen to the
6e33951e
L
7089 symbol which just became indirect if DIR isn't a hidden versioned
7090 symbol. */
4d269e42 7091
422f1182 7092 if (dir->versioned != versioned_hidden)
6e33951e
L
7093 {
7094 dir->ref_dynamic |= ind->ref_dynamic;
7095 dir->ref_regular |= ind->ref_regular;
7096 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7097 dir->non_got_ref |= ind->non_got_ref;
7098 dir->needs_plt |= ind->needs_plt;
7099 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7100 }
4d269e42
AM
7101
7102 if (ind->root.type != bfd_link_hash_indirect)
7103 return;
7104
7105 /* Copy over the global and procedure linkage table refcount entries.
7106 These may have been already set up by a check_relocs routine. */
7107 htab = elf_hash_table (info);
7108 if (ind->got.refcount > htab->init_got_refcount.refcount)
7109 {
7110 if (dir->got.refcount < 0)
7111 dir->got.refcount = 0;
7112 dir->got.refcount += ind->got.refcount;
7113 ind->got.refcount = htab->init_got_refcount.refcount;
7114 }
7115
7116 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7117 {
7118 if (dir->plt.refcount < 0)
7119 dir->plt.refcount = 0;
7120 dir->plt.refcount += ind->plt.refcount;
7121 ind->plt.refcount = htab->init_plt_refcount.refcount;
7122 }
7123
7124 if (ind->dynindx != -1)
7125 {
7126 if (dir->dynindx != -1)
7127 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7128 dir->dynindx = ind->dynindx;
7129 dir->dynstr_index = ind->dynstr_index;
7130 ind->dynindx = -1;
7131 ind->dynstr_index = 0;
7132 }
7133}
7134
7135void
7136_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7137 struct elf_link_hash_entry *h,
7138 bfd_boolean force_local)
7139{
3aa14d16
L
7140 /* STT_GNU_IFUNC symbol must go through PLT. */
7141 if (h->type != STT_GNU_IFUNC)
7142 {
7143 h->plt = elf_hash_table (info)->init_plt_offset;
7144 h->needs_plt = 0;
7145 }
4d269e42
AM
7146 if (force_local)
7147 {
7148 h->forced_local = 1;
7149 if (h->dynindx != -1)
7150 {
7151 h->dynindx = -1;
7152 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7153 h->dynstr_index);
7154 }
7155 }
7156}
7157
7bf52ea2
AM
7158/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7159 caller. */
4d269e42
AM
7160
7161bfd_boolean
7162_bfd_elf_link_hash_table_init
7163 (struct elf_link_hash_table *table,
7164 bfd *abfd,
7165 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7166 struct bfd_hash_table *,
7167 const char *),
4dfe6ac6
NC
7168 unsigned int entsize,
7169 enum elf_target_id target_id)
4d269e42
AM
7170{
7171 bfd_boolean ret;
7172 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7173
4d269e42
AM
7174 table->init_got_refcount.refcount = can_refcount - 1;
7175 table->init_plt_refcount.refcount = can_refcount - 1;
7176 table->init_got_offset.offset = -(bfd_vma) 1;
7177 table->init_plt_offset.offset = -(bfd_vma) 1;
7178 /* The first dynamic symbol is a dummy. */
7179 table->dynsymcount = 1;
7180
7181 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7182
4d269e42 7183 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7184 table->hash_table_id = target_id;
4d269e42
AM
7185
7186 return ret;
7187}
7188
7189/* Create an ELF linker hash table. */
7190
7191struct bfd_link_hash_table *
7192_bfd_elf_link_hash_table_create (bfd *abfd)
7193{
7194 struct elf_link_hash_table *ret;
7195 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7196
7bf52ea2 7197 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7198 if (ret == NULL)
7199 return NULL;
7200
7201 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7202 sizeof (struct elf_link_hash_entry),
7203 GENERIC_ELF_DATA))
4d269e42
AM
7204 {
7205 free (ret);
7206 return NULL;
7207 }
d495ab0d 7208 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7209
7210 return &ret->root;
7211}
7212
9f7c3e5e
AM
7213/* Destroy an ELF linker hash table. */
7214
7215void
d495ab0d 7216_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7217{
d495ab0d
AM
7218 struct elf_link_hash_table *htab;
7219
7220 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7221 if (htab->dynstr != NULL)
7222 _bfd_elf_strtab_free (htab->dynstr);
7223 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7224 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7225}
7226
4d269e42
AM
7227/* This is a hook for the ELF emulation code in the generic linker to
7228 tell the backend linker what file name to use for the DT_NEEDED
7229 entry for a dynamic object. */
7230
7231void
7232bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7233{
7234 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7235 && bfd_get_format (abfd) == bfd_object)
7236 elf_dt_name (abfd) = name;
7237}
7238
7239int
7240bfd_elf_get_dyn_lib_class (bfd *abfd)
7241{
7242 int lib_class;
7243 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7244 && bfd_get_format (abfd) == bfd_object)
7245 lib_class = elf_dyn_lib_class (abfd);
7246 else
7247 lib_class = 0;
7248 return lib_class;
7249}
7250
7251void
7252bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7253{
7254 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7255 && bfd_get_format (abfd) == bfd_object)
7256 elf_dyn_lib_class (abfd) = lib_class;
7257}
7258
7259/* Get the list of DT_NEEDED entries for a link. This is a hook for
7260 the linker ELF emulation code. */
7261
7262struct bfd_link_needed_list *
7263bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7264 struct bfd_link_info *info)
7265{
7266 if (! is_elf_hash_table (info->hash))
7267 return NULL;
7268 return elf_hash_table (info)->needed;
7269}
7270
7271/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7272 hook for the linker ELF emulation code. */
7273
7274struct bfd_link_needed_list *
7275bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7276 struct bfd_link_info *info)
7277{
7278 if (! is_elf_hash_table (info->hash))
7279 return NULL;
7280 return elf_hash_table (info)->runpath;
7281}
7282
7283/* Get the name actually used for a dynamic object for a link. This
7284 is the SONAME entry if there is one. Otherwise, it is the string
7285 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7286
7287const char *
7288bfd_elf_get_dt_soname (bfd *abfd)
7289{
7290 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7291 && bfd_get_format (abfd) == bfd_object)
7292 return elf_dt_name (abfd);
7293 return NULL;
7294}
7295
7296/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7297 the ELF linker emulation code. */
7298
7299bfd_boolean
7300bfd_elf_get_bfd_needed_list (bfd *abfd,
7301 struct bfd_link_needed_list **pneeded)
7302{
7303 asection *s;
7304 bfd_byte *dynbuf = NULL;
cb33740c 7305 unsigned int elfsec;
4d269e42
AM
7306 unsigned long shlink;
7307 bfd_byte *extdyn, *extdynend;
7308 size_t extdynsize;
7309 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7310
7311 *pneeded = NULL;
7312
7313 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7314 || bfd_get_format (abfd) != bfd_object)
7315 return TRUE;
7316
7317 s = bfd_get_section_by_name (abfd, ".dynamic");
7318 if (s == NULL || s->size == 0)
7319 return TRUE;
7320
7321 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7322 goto error_return;
7323
7324 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7325 if (elfsec == SHN_BAD)
4d269e42
AM
7326 goto error_return;
7327
7328 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7329
4d269e42
AM
7330 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7331 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7332
7333 extdyn = dynbuf;
7334 extdynend = extdyn + s->size;
7335 for (; extdyn < extdynend; extdyn += extdynsize)
7336 {
7337 Elf_Internal_Dyn dyn;
7338
7339 (*swap_dyn_in) (abfd, extdyn, &dyn);
7340
7341 if (dyn.d_tag == DT_NULL)
7342 break;
7343
7344 if (dyn.d_tag == DT_NEEDED)
7345 {
7346 const char *string;
7347 struct bfd_link_needed_list *l;
7348 unsigned int tagv = dyn.d_un.d_val;
7349 bfd_size_type amt;
7350
7351 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7352 if (string == NULL)
7353 goto error_return;
7354
7355 amt = sizeof *l;
a50b1753 7356 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7357 if (l == NULL)
7358 goto error_return;
7359
7360 l->by = abfd;
7361 l->name = string;
7362 l->next = *pneeded;
7363 *pneeded = l;
7364 }
7365 }
7366
7367 free (dynbuf);
7368
7369 return TRUE;
7370
7371 error_return:
7372 if (dynbuf != NULL)
7373 free (dynbuf);
7374 return FALSE;
7375}
7376
7377struct elf_symbuf_symbol
7378{
7379 unsigned long st_name; /* Symbol name, index in string tbl */
7380 unsigned char st_info; /* Type and binding attributes */
7381 unsigned char st_other; /* Visibilty, and target specific */
7382};
7383
7384struct elf_symbuf_head
7385{
7386 struct elf_symbuf_symbol *ssym;
ef53be89 7387 size_t count;
4d269e42
AM
7388 unsigned int st_shndx;
7389};
7390
7391struct elf_symbol
7392{
7393 union
7394 {
7395 Elf_Internal_Sym *isym;
7396 struct elf_symbuf_symbol *ssym;
7397 } u;
7398 const char *name;
7399};
7400
7401/* Sort references to symbols by ascending section number. */
7402
7403static int
7404elf_sort_elf_symbol (const void *arg1, const void *arg2)
7405{
7406 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7407 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7408
7409 return s1->st_shndx - s2->st_shndx;
7410}
7411
7412static int
7413elf_sym_name_compare (const void *arg1, const void *arg2)
7414{
7415 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7416 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7417 return strcmp (s1->name, s2->name);
7418}
7419
7420static struct elf_symbuf_head *
ef53be89 7421elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7422{
14b1c01e 7423 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7424 struct elf_symbuf_symbol *ssym;
7425 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7426 size_t i, shndx_count, total_size;
4d269e42 7427
a50b1753 7428 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7429 if (indbuf == NULL)
7430 return NULL;
7431
7432 for (ind = indbuf, i = 0; i < symcount; i++)
7433 if (isymbuf[i].st_shndx != SHN_UNDEF)
7434 *ind++ = &isymbuf[i];
7435 indbufend = ind;
7436
7437 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7438 elf_sort_elf_symbol);
7439
7440 shndx_count = 0;
7441 if (indbufend > indbuf)
7442 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7443 if (ind[0]->st_shndx != ind[1]->st_shndx)
7444 shndx_count++;
7445
3ae181ee
L
7446 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7447 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7448 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7449 if (ssymbuf == NULL)
7450 {
7451 free (indbuf);
7452 return NULL;
7453 }
7454
3ae181ee 7455 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7456 ssymbuf->ssym = NULL;
7457 ssymbuf->count = shndx_count;
7458 ssymbuf->st_shndx = 0;
7459 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7460 {
7461 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7462 {
7463 ssymhead++;
7464 ssymhead->ssym = ssym;
7465 ssymhead->count = 0;
7466 ssymhead->st_shndx = (*ind)->st_shndx;
7467 }
7468 ssym->st_name = (*ind)->st_name;
7469 ssym->st_info = (*ind)->st_info;
7470 ssym->st_other = (*ind)->st_other;
7471 ssymhead->count++;
7472 }
ef53be89 7473 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7474 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7475 == total_size));
4d269e42
AM
7476
7477 free (indbuf);
7478 return ssymbuf;
7479}
7480
7481/* Check if 2 sections define the same set of local and global
7482 symbols. */
7483
8f317e31 7484static bfd_boolean
4d269e42
AM
7485bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7486 struct bfd_link_info *info)
7487{
7488 bfd *bfd1, *bfd2;
7489 const struct elf_backend_data *bed1, *bed2;
7490 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7491 size_t symcount1, symcount2;
4d269e42
AM
7492 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7493 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7494 Elf_Internal_Sym *isym, *isymend;
7495 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7496 size_t count1, count2, i;
cb33740c 7497 unsigned int shndx1, shndx2;
4d269e42
AM
7498 bfd_boolean result;
7499
7500 bfd1 = sec1->owner;
7501 bfd2 = sec2->owner;
7502
4d269e42
AM
7503 /* Both sections have to be in ELF. */
7504 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7505 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7506 return FALSE;
7507
7508 if (elf_section_type (sec1) != elf_section_type (sec2))
7509 return FALSE;
7510
4d269e42
AM
7511 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7512 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7513 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7514 return FALSE;
7515
7516 bed1 = get_elf_backend_data (bfd1);
7517 bed2 = get_elf_backend_data (bfd2);
7518 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7519 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7520 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7521 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7522
7523 if (symcount1 == 0 || symcount2 == 0)
7524 return FALSE;
7525
7526 result = FALSE;
7527 isymbuf1 = NULL;
7528 isymbuf2 = NULL;
a50b1753
NC
7529 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7530 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7531
7532 if (ssymbuf1 == NULL)
7533 {
7534 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7535 NULL, NULL, NULL);
7536 if (isymbuf1 == NULL)
7537 goto done;
7538
7539 if (!info->reduce_memory_overheads)
7540 elf_tdata (bfd1)->symbuf = ssymbuf1
7541 = elf_create_symbuf (symcount1, isymbuf1);
7542 }
7543
7544 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7545 {
7546 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7547 NULL, NULL, NULL);
7548 if (isymbuf2 == NULL)
7549 goto done;
7550
7551 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7552 elf_tdata (bfd2)->symbuf = ssymbuf2
7553 = elf_create_symbuf (symcount2, isymbuf2);
7554 }
7555
7556 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7557 {
7558 /* Optimized faster version. */
ef53be89 7559 size_t lo, hi, mid;
4d269e42
AM
7560 struct elf_symbol *symp;
7561 struct elf_symbuf_symbol *ssym, *ssymend;
7562
7563 lo = 0;
7564 hi = ssymbuf1->count;
7565 ssymbuf1++;
7566 count1 = 0;
7567 while (lo < hi)
7568 {
7569 mid = (lo + hi) / 2;
cb33740c 7570 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7571 hi = mid;
cb33740c 7572 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7573 lo = mid + 1;
7574 else
7575 {
7576 count1 = ssymbuf1[mid].count;
7577 ssymbuf1 += mid;
7578 break;
7579 }
7580 }
7581
7582 lo = 0;
7583 hi = ssymbuf2->count;
7584 ssymbuf2++;
7585 count2 = 0;
7586 while (lo < hi)
7587 {
7588 mid = (lo + hi) / 2;
cb33740c 7589 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7590 hi = mid;
cb33740c 7591 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7592 lo = mid + 1;
7593 else
7594 {
7595 count2 = ssymbuf2[mid].count;
7596 ssymbuf2 += mid;
7597 break;
7598 }
7599 }
7600
7601 if (count1 == 0 || count2 == 0 || count1 != count2)
7602 goto done;
7603
ca4be51c
AM
7604 symtable1
7605 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7606 symtable2
7607 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7608 if (symtable1 == NULL || symtable2 == NULL)
7609 goto done;
7610
7611 symp = symtable1;
7612 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7613 ssym < ssymend; ssym++, symp++)
7614 {
7615 symp->u.ssym = ssym;
7616 symp->name = bfd_elf_string_from_elf_section (bfd1,
7617 hdr1->sh_link,
7618 ssym->st_name);
7619 }
7620
7621 symp = symtable2;
7622 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7623 ssym < ssymend; ssym++, symp++)
7624 {
7625 symp->u.ssym = ssym;
7626 symp->name = bfd_elf_string_from_elf_section (bfd2,
7627 hdr2->sh_link,
7628 ssym->st_name);
7629 }
7630
7631 /* Sort symbol by name. */
7632 qsort (symtable1, count1, sizeof (struct elf_symbol),
7633 elf_sym_name_compare);
7634 qsort (symtable2, count1, sizeof (struct elf_symbol),
7635 elf_sym_name_compare);
7636
7637 for (i = 0; i < count1; i++)
7638 /* Two symbols must have the same binding, type and name. */
7639 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7640 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7641 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7642 goto done;
7643
7644 result = TRUE;
7645 goto done;
7646 }
7647
a50b1753
NC
7648 symtable1 = (struct elf_symbol *)
7649 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7650 symtable2 = (struct elf_symbol *)
7651 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7652 if (symtable1 == NULL || symtable2 == NULL)
7653 goto done;
7654
7655 /* Count definitions in the section. */
7656 count1 = 0;
7657 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7658 if (isym->st_shndx == shndx1)
4d269e42
AM
7659 symtable1[count1++].u.isym = isym;
7660
7661 count2 = 0;
7662 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7663 if (isym->st_shndx == shndx2)
4d269e42
AM
7664 symtable2[count2++].u.isym = isym;
7665
7666 if (count1 == 0 || count2 == 0 || count1 != count2)
7667 goto done;
7668
7669 for (i = 0; i < count1; i++)
7670 symtable1[i].name
7671 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7672 symtable1[i].u.isym->st_name);
7673
7674 for (i = 0; i < count2; i++)
7675 symtable2[i].name
7676 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7677 symtable2[i].u.isym->st_name);
7678
7679 /* Sort symbol by name. */
7680 qsort (symtable1, count1, sizeof (struct elf_symbol),
7681 elf_sym_name_compare);
7682 qsort (symtable2, count1, sizeof (struct elf_symbol),
7683 elf_sym_name_compare);
7684
7685 for (i = 0; i < count1; i++)
7686 /* Two symbols must have the same binding, type and name. */
7687 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7688 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7689 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7690 goto done;
7691
7692 result = TRUE;
7693
7694done:
7695 if (symtable1)
7696 free (symtable1);
7697 if (symtable2)
7698 free (symtable2);
7699 if (isymbuf1)
7700 free (isymbuf1);
7701 if (isymbuf2)
7702 free (isymbuf2);
7703
7704 return result;
7705}
7706
7707/* Return TRUE if 2 section types are compatible. */
7708
7709bfd_boolean
7710_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7711 bfd *bbfd, const asection *bsec)
7712{
7713 if (asec == NULL
7714 || bsec == NULL
7715 || abfd->xvec->flavour != bfd_target_elf_flavour
7716 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7717 return TRUE;
7718
7719 return elf_section_type (asec) == elf_section_type (bsec);
7720}
7721\f
c152c796
AM
7722/* Final phase of ELF linker. */
7723
7724/* A structure we use to avoid passing large numbers of arguments. */
7725
7726struct elf_final_link_info
7727{
7728 /* General link information. */
7729 struct bfd_link_info *info;
7730 /* Output BFD. */
7731 bfd *output_bfd;
7732 /* Symbol string table. */
ef10c3ac 7733 struct elf_strtab_hash *symstrtab;
c152c796
AM
7734 /* .hash section. */
7735 asection *hash_sec;
7736 /* symbol version section (.gnu.version). */
7737 asection *symver_sec;
7738 /* Buffer large enough to hold contents of any section. */
7739 bfd_byte *contents;
7740 /* Buffer large enough to hold external relocs of any section. */
7741 void *external_relocs;
7742 /* Buffer large enough to hold internal relocs of any section. */
7743 Elf_Internal_Rela *internal_relocs;
7744 /* Buffer large enough to hold external local symbols of any input
7745 BFD. */
7746 bfd_byte *external_syms;
7747 /* And a buffer for symbol section indices. */
7748 Elf_External_Sym_Shndx *locsym_shndx;
7749 /* Buffer large enough to hold internal local symbols of any input
7750 BFD. */
7751 Elf_Internal_Sym *internal_syms;
7752 /* Array large enough to hold a symbol index for each local symbol
7753 of any input BFD. */
7754 long *indices;
7755 /* Array large enough to hold a section pointer for each local
7756 symbol of any input BFD. */
7757 asection **sections;
ef10c3ac 7758 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7759 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7760 /* Number of STT_FILE syms seen. */
7761 size_t filesym_count;
c152c796
AM
7762};
7763
7764/* This struct is used to pass information to elf_link_output_extsym. */
7765
7766struct elf_outext_info
7767{
7768 bfd_boolean failed;
7769 bfd_boolean localsyms;
34a79995 7770 bfd_boolean file_sym_done;
8b127cbc 7771 struct elf_final_link_info *flinfo;
c152c796
AM
7772};
7773
d9352518
DB
7774
7775/* Support for evaluating a complex relocation.
7776
7777 Complex relocations are generalized, self-describing relocations. The
7778 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7779 relocations themselves.
d9352518
DB
7780
7781 The relocations are use a reserved elf-wide relocation type code (R_RELC
7782 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7783 information (start bit, end bit, word width, etc) into the addend. This
7784 information is extracted from CGEN-generated operand tables within gas.
7785
7786 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7787 internal) representing prefix-notation expressions, including but not
7788 limited to those sorts of expressions normally encoded as addends in the
7789 addend field. The symbol mangling format is:
7790
7791 <node> := <literal>
7792 | <unary-operator> ':' <node>
7793 | <binary-operator> ':' <node> ':' <node>
7794 ;
7795
7796 <literal> := 's' <digits=N> ':' <N character symbol name>
7797 | 'S' <digits=N> ':' <N character section name>
7798 | '#' <hexdigits>
7799 ;
7800
7801 <binary-operator> := as in C
7802 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7803
7804static void
a0c8462f
AM
7805set_symbol_value (bfd *bfd_with_globals,
7806 Elf_Internal_Sym *isymbuf,
7807 size_t locsymcount,
7808 size_t symidx,
7809 bfd_vma val)
d9352518 7810{
8977835c
AM
7811 struct elf_link_hash_entry **sym_hashes;
7812 struct elf_link_hash_entry *h;
7813 size_t extsymoff = locsymcount;
d9352518 7814
8977835c 7815 if (symidx < locsymcount)
d9352518 7816 {
8977835c
AM
7817 Elf_Internal_Sym *sym;
7818
7819 sym = isymbuf + symidx;
7820 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7821 {
7822 /* It is a local symbol: move it to the
7823 "absolute" section and give it a value. */
7824 sym->st_shndx = SHN_ABS;
7825 sym->st_value = val;
7826 return;
7827 }
7828 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7829 extsymoff = 0;
d9352518 7830 }
8977835c
AM
7831
7832 /* It is a global symbol: set its link type
7833 to "defined" and give it a value. */
7834
7835 sym_hashes = elf_sym_hashes (bfd_with_globals);
7836 h = sym_hashes [symidx - extsymoff];
7837 while (h->root.type == bfd_link_hash_indirect
7838 || h->root.type == bfd_link_hash_warning)
7839 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7840 h->root.type = bfd_link_hash_defined;
7841 h->root.u.def.value = val;
7842 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7843}
7844
a0c8462f
AM
7845static bfd_boolean
7846resolve_symbol (const char *name,
7847 bfd *input_bfd,
8b127cbc 7848 struct elf_final_link_info *flinfo,
a0c8462f
AM
7849 bfd_vma *result,
7850 Elf_Internal_Sym *isymbuf,
7851 size_t locsymcount)
d9352518 7852{
a0c8462f
AM
7853 Elf_Internal_Sym *sym;
7854 struct bfd_link_hash_entry *global_entry;
7855 const char *candidate = NULL;
7856 Elf_Internal_Shdr *symtab_hdr;
7857 size_t i;
7858
d9352518
DB
7859 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7860
7861 for (i = 0; i < locsymcount; ++ i)
7862 {
8977835c 7863 sym = isymbuf + i;
d9352518
DB
7864
7865 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7866 continue;
7867
7868 candidate = bfd_elf_string_from_elf_section (input_bfd,
7869 symtab_hdr->sh_link,
7870 sym->st_name);
7871#ifdef DEBUG
0f02bbd9
AM
7872 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7873 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7874#endif
7875 if (candidate && strcmp (candidate, name) == 0)
7876 {
8b127cbc 7877 asection *sec = flinfo->sections [i];
d9352518 7878
0f02bbd9
AM
7879 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7880 *result += sec->output_offset + sec->output_section->vma;
d9352518 7881#ifdef DEBUG
0f02bbd9
AM
7882 printf ("Found symbol with value %8.8lx\n",
7883 (unsigned long) *result);
d9352518
DB
7884#endif
7885 return TRUE;
7886 }
7887 }
7888
7889 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7890 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7891 FALSE, FALSE, TRUE);
d9352518
DB
7892 if (!global_entry)
7893 return FALSE;
a0c8462f 7894
d9352518
DB
7895 if (global_entry->type == bfd_link_hash_defined
7896 || global_entry->type == bfd_link_hash_defweak)
7897 {
a0c8462f
AM
7898 *result = (global_entry->u.def.value
7899 + global_entry->u.def.section->output_section->vma
7900 + global_entry->u.def.section->output_offset);
d9352518 7901#ifdef DEBUG
0f02bbd9
AM
7902 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7903 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7904#endif
7905 return TRUE;
a0c8462f 7906 }
d9352518 7907
d9352518
DB
7908 return FALSE;
7909}
7910
37b01f6a
DG
7911/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7912 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7913 names like "foo.end" which is the end address of section "foo". */
7914
d9352518 7915static bfd_boolean
a0c8462f
AM
7916resolve_section (const char *name,
7917 asection *sections,
37b01f6a
DG
7918 bfd_vma *result,
7919 bfd * abfd)
d9352518 7920{
a0c8462f
AM
7921 asection *curr;
7922 unsigned int len;
d9352518 7923
a0c8462f 7924 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7925 if (strcmp (curr->name, name) == 0)
7926 {
7927 *result = curr->vma;
7928 return TRUE;
7929 }
7930
7931 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7932 /* FIXME: This could be coded more efficiently... */
a0c8462f 7933 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7934 {
7935 len = strlen (curr->name);
a0c8462f 7936 if (len > strlen (name))
d9352518
DB
7937 continue;
7938
7939 if (strncmp (curr->name, name, len) == 0)
7940 {
7941 if (strncmp (".end", name + len, 4) == 0)
7942 {
37b01f6a 7943 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7944 return TRUE;
7945 }
7946
7947 /* Insert more pseudo-section names here, if you like. */
7948 }
7949 }
a0c8462f 7950
d9352518
DB
7951 return FALSE;
7952}
7953
7954static void
a0c8462f 7955undefined_reference (const char *reftype, const char *name)
d9352518 7956{
695344c0 7957 /* xgettext:c-format */
a0c8462f
AM
7958 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7959 reftype, name);
d9352518
DB
7960}
7961
7962static bfd_boolean
a0c8462f
AM
7963eval_symbol (bfd_vma *result,
7964 const char **symp,
7965 bfd *input_bfd,
8b127cbc 7966 struct elf_final_link_info *flinfo,
a0c8462f
AM
7967 bfd_vma dot,
7968 Elf_Internal_Sym *isymbuf,
7969 size_t locsymcount,
7970 int signed_p)
d9352518 7971{
4b93929b
NC
7972 size_t len;
7973 size_t symlen;
a0c8462f
AM
7974 bfd_vma a;
7975 bfd_vma b;
4b93929b 7976 char symbuf[4096];
0f02bbd9 7977 const char *sym = *symp;
a0c8462f
AM
7978 const char *symend;
7979 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7980
7981 len = strlen (sym);
7982 symend = sym + len;
7983
4b93929b 7984 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7985 {
7986 bfd_set_error (bfd_error_invalid_operation);
7987 return FALSE;
7988 }
a0c8462f 7989
d9352518
DB
7990 switch (* sym)
7991 {
7992 case '.':
0f02bbd9
AM
7993 *result = dot;
7994 *symp = sym + 1;
d9352518
DB
7995 return TRUE;
7996
7997 case '#':
0f02bbd9
AM
7998 ++sym;
7999 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8000 return TRUE;
8001
8002 case 'S':
8003 symbol_is_section = TRUE;
1a0670f3 8004 /* Fall through. */
a0c8462f 8005 case 's':
0f02bbd9
AM
8006 ++sym;
8007 symlen = strtol (sym, (char **) symp, 10);
8008 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8009
4b93929b 8010 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8011 {
8012 bfd_set_error (bfd_error_invalid_operation);
8013 return FALSE;
8014 }
8015
8016 memcpy (symbuf, sym, symlen);
a0c8462f 8017 symbuf[symlen] = '\0';
0f02bbd9 8018 *symp = sym + symlen;
a0c8462f
AM
8019
8020 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8021 the symbol as a section, or vice-versa. so we're pretty liberal in our
8022 interpretation here; section means "try section first", not "must be a
8023 section", and likewise with symbol. */
8024
a0c8462f 8025 if (symbol_is_section)
d9352518 8026 {
37b01f6a 8027 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8028 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8029 isymbuf, locsymcount))
d9352518
DB
8030 {
8031 undefined_reference ("section", symbuf);
8032 return FALSE;
8033 }
a0c8462f
AM
8034 }
8035 else
d9352518 8036 {
8b127cbc 8037 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8038 isymbuf, locsymcount)
8b127cbc 8039 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8040 result, input_bfd))
d9352518
DB
8041 {
8042 undefined_reference ("symbol", symbuf);
8043 return FALSE;
8044 }
8045 }
8046
8047 return TRUE;
a0c8462f 8048
d9352518
DB
8049 /* All that remains are operators. */
8050
8051#define UNARY_OP(op) \
8052 if (strncmp (sym, #op, strlen (#op)) == 0) \
8053 { \
8054 sym += strlen (#op); \
a0c8462f
AM
8055 if (*sym == ':') \
8056 ++sym; \
0f02bbd9 8057 *symp = sym; \
8b127cbc 8058 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8059 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8060 return FALSE; \
8061 if (signed_p) \
0f02bbd9 8062 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8063 else \
8064 *result = op a; \
d9352518
DB
8065 return TRUE; \
8066 }
8067
8068#define BINARY_OP(op) \
8069 if (strncmp (sym, #op, strlen (#op)) == 0) \
8070 { \
8071 sym += strlen (#op); \
a0c8462f
AM
8072 if (*sym == ':') \
8073 ++sym; \
0f02bbd9 8074 *symp = sym; \
8b127cbc 8075 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8076 isymbuf, locsymcount, signed_p)) \
a0c8462f 8077 return FALSE; \
0f02bbd9 8078 ++*symp; \
8b127cbc 8079 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8080 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8081 return FALSE; \
8082 if (signed_p) \
0f02bbd9 8083 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8084 else \
8085 *result = a op b; \
d9352518
DB
8086 return TRUE; \
8087 }
8088
8089 default:
8090 UNARY_OP (0-);
8091 BINARY_OP (<<);
8092 BINARY_OP (>>);
8093 BINARY_OP (==);
8094 BINARY_OP (!=);
8095 BINARY_OP (<=);
8096 BINARY_OP (>=);
8097 BINARY_OP (&&);
8098 BINARY_OP (||);
8099 UNARY_OP (~);
8100 UNARY_OP (!);
8101 BINARY_OP (*);
8102 BINARY_OP (/);
8103 BINARY_OP (%);
8104 BINARY_OP (^);
8105 BINARY_OP (|);
8106 BINARY_OP (&);
8107 BINARY_OP (+);
8108 BINARY_OP (-);
8109 BINARY_OP (<);
8110 BINARY_OP (>);
8111#undef UNARY_OP
8112#undef BINARY_OP
8113 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8114 bfd_set_error (bfd_error_invalid_operation);
8115 return FALSE;
8116 }
8117}
8118
d9352518 8119static void
a0c8462f
AM
8120put_value (bfd_vma size,
8121 unsigned long chunksz,
8122 bfd *input_bfd,
8123 bfd_vma x,
8124 bfd_byte *location)
d9352518
DB
8125{
8126 location += (size - chunksz);
8127
41cd1ad1 8128 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8129 {
8130 switch (chunksz)
8131 {
d9352518
DB
8132 case 1:
8133 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8134 x >>= 8;
d9352518
DB
8135 break;
8136 case 2:
8137 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8138 x >>= 16;
d9352518
DB
8139 break;
8140 case 4:
8141 bfd_put_32 (input_bfd, x, location);
65164438
NC
8142 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8143 x >>= 16;
8144 x >>= 16;
d9352518 8145 break;
d9352518 8146#ifdef BFD64
41cd1ad1 8147 case 8:
d9352518 8148 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8149 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8150 x >>= 32;
8151 x >>= 32;
8152 break;
d9352518 8153#endif
41cd1ad1
NC
8154 default:
8155 abort ();
d9352518
DB
8156 break;
8157 }
8158 }
8159}
8160
a0c8462f
AM
8161static bfd_vma
8162get_value (bfd_vma size,
8163 unsigned long chunksz,
8164 bfd *input_bfd,
8165 bfd_byte *location)
d9352518 8166{
9b239e0e 8167 int shift;
d9352518
DB
8168 bfd_vma x = 0;
8169
9b239e0e
NC
8170 /* Sanity checks. */
8171 BFD_ASSERT (chunksz <= sizeof (x)
8172 && size >= chunksz
8173 && chunksz != 0
8174 && (size % chunksz) == 0
8175 && input_bfd != NULL
8176 && location != NULL);
8177
8178 if (chunksz == sizeof (x))
8179 {
8180 BFD_ASSERT (size == chunksz);
8181
8182 /* Make sure that we do not perform an undefined shift operation.
8183 We know that size == chunksz so there will only be one iteration
8184 of the loop below. */
8185 shift = 0;
8186 }
8187 else
8188 shift = 8 * chunksz;
8189
a0c8462f 8190 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8191 {
8192 switch (chunksz)
8193 {
d9352518 8194 case 1:
9b239e0e 8195 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8196 break;
8197 case 2:
9b239e0e 8198 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8199 break;
8200 case 4:
9b239e0e 8201 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8202 break;
d9352518 8203#ifdef BFD64
9b239e0e
NC
8204 case 8:
8205 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8206 break;
9b239e0e
NC
8207#endif
8208 default:
8209 abort ();
d9352518
DB
8210 }
8211 }
8212 return x;
8213}
8214
a0c8462f
AM
8215static void
8216decode_complex_addend (unsigned long *start, /* in bits */
8217 unsigned long *oplen, /* in bits */
8218 unsigned long *len, /* in bits */
8219 unsigned long *wordsz, /* in bytes */
8220 unsigned long *chunksz, /* in bytes */
8221 unsigned long *lsb0_p,
8222 unsigned long *signed_p,
8223 unsigned long *trunc_p,
8224 unsigned long encoded)
d9352518
DB
8225{
8226 * start = encoded & 0x3F;
8227 * len = (encoded >> 6) & 0x3F;
8228 * oplen = (encoded >> 12) & 0x3F;
8229 * wordsz = (encoded >> 18) & 0xF;
8230 * chunksz = (encoded >> 22) & 0xF;
8231 * lsb0_p = (encoded >> 27) & 1;
8232 * signed_p = (encoded >> 28) & 1;
8233 * trunc_p = (encoded >> 29) & 1;
8234}
8235
cdfeee4f 8236bfd_reloc_status_type
0f02bbd9 8237bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8238 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8239 bfd_byte *contents,
8240 Elf_Internal_Rela *rel,
8241 bfd_vma relocation)
d9352518 8242{
0f02bbd9
AM
8243 bfd_vma shift, x, mask;
8244 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8245 bfd_reloc_status_type r;
d9352518
DB
8246
8247 /* Perform this reloc, since it is complex.
8248 (this is not to say that it necessarily refers to a complex
8249 symbol; merely that it is a self-describing CGEN based reloc.
8250 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8251 word size, etc) encoded within it.). */
d9352518 8252
a0c8462f
AM
8253 decode_complex_addend (&start, &oplen, &len, &wordsz,
8254 &chunksz, &lsb0_p, &signed_p,
8255 &trunc_p, rel->r_addend);
d9352518
DB
8256
8257 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8258
8259 if (lsb0_p)
8260 shift = (start + 1) - len;
8261 else
8262 shift = (8 * wordsz) - (start + len);
8263
37b01f6a
DG
8264 x = get_value (wordsz, chunksz, input_bfd,
8265 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8266
8267#ifdef DEBUG
8268 printf ("Doing complex reloc: "
8269 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8270 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8271 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8272 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8273 oplen, (unsigned long) x, (unsigned long) mask,
8274 (unsigned long) relocation);
d9352518
DB
8275#endif
8276
cdfeee4f 8277 r = bfd_reloc_ok;
d9352518 8278 if (! trunc_p)
cdfeee4f
AM
8279 /* Now do an overflow check. */
8280 r = bfd_check_overflow ((signed_p
8281 ? complain_overflow_signed
8282 : complain_overflow_unsigned),
8283 len, 0, (8 * wordsz),
8284 relocation);
a0c8462f 8285
d9352518
DB
8286 /* Do the deed. */
8287 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8288
8289#ifdef DEBUG
8290 printf (" relocation: %8.8lx\n"
8291 " shifted mask: %8.8lx\n"
8292 " shifted/masked reloc: %8.8lx\n"
8293 " result: %8.8lx\n",
9ccb8af9
AM
8294 (unsigned long) relocation, (unsigned long) (mask << shift),
8295 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8296#endif
37b01f6a
DG
8297 put_value (wordsz, chunksz, input_bfd, x,
8298 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8299 return r;
d9352518
DB
8300}
8301
0e287786
AM
8302/* Functions to read r_offset from external (target order) reloc
8303 entry. Faster than bfd_getl32 et al, because we let the compiler
8304 know the value is aligned. */
53df40a4 8305
0e287786
AM
8306static bfd_vma
8307ext32l_r_offset (const void *p)
53df40a4
AM
8308{
8309 union aligned32
8310 {
8311 uint32_t v;
8312 unsigned char c[4];
8313 };
8314 const union aligned32 *a
0e287786 8315 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8316
8317 uint32_t aval = ( (uint32_t) a->c[0]
8318 | (uint32_t) a->c[1] << 8
8319 | (uint32_t) a->c[2] << 16
8320 | (uint32_t) a->c[3] << 24);
0e287786 8321 return aval;
53df40a4
AM
8322}
8323
0e287786
AM
8324static bfd_vma
8325ext32b_r_offset (const void *p)
53df40a4
AM
8326{
8327 union aligned32
8328 {
8329 uint32_t v;
8330 unsigned char c[4];
8331 };
8332 const union aligned32 *a
0e287786 8333 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8334
8335 uint32_t aval = ( (uint32_t) a->c[0] << 24
8336 | (uint32_t) a->c[1] << 16
8337 | (uint32_t) a->c[2] << 8
8338 | (uint32_t) a->c[3]);
0e287786 8339 return aval;
53df40a4
AM
8340}
8341
8342#ifdef BFD_HOST_64_BIT
0e287786
AM
8343static bfd_vma
8344ext64l_r_offset (const void *p)
53df40a4
AM
8345{
8346 union aligned64
8347 {
8348 uint64_t v;
8349 unsigned char c[8];
8350 };
8351 const union aligned64 *a
0e287786 8352 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8353
8354 uint64_t aval = ( (uint64_t) a->c[0]
8355 | (uint64_t) a->c[1] << 8
8356 | (uint64_t) a->c[2] << 16
8357 | (uint64_t) a->c[3] << 24
8358 | (uint64_t) a->c[4] << 32
8359 | (uint64_t) a->c[5] << 40
8360 | (uint64_t) a->c[6] << 48
8361 | (uint64_t) a->c[7] << 56);
0e287786 8362 return aval;
53df40a4
AM
8363}
8364
0e287786
AM
8365static bfd_vma
8366ext64b_r_offset (const void *p)
53df40a4
AM
8367{
8368 union aligned64
8369 {
8370 uint64_t v;
8371 unsigned char c[8];
8372 };
8373 const union aligned64 *a
0e287786 8374 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8375
8376 uint64_t aval = ( (uint64_t) a->c[0] << 56
8377 | (uint64_t) a->c[1] << 48
8378 | (uint64_t) a->c[2] << 40
8379 | (uint64_t) a->c[3] << 32
8380 | (uint64_t) a->c[4] << 24
8381 | (uint64_t) a->c[5] << 16
8382 | (uint64_t) a->c[6] << 8
8383 | (uint64_t) a->c[7]);
0e287786 8384 return aval;
53df40a4
AM
8385}
8386#endif
8387
c152c796
AM
8388/* When performing a relocatable link, the input relocations are
8389 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8390 referenced must be updated. Update all the relocations found in
8391 RELDATA. */
c152c796 8392
bca6d0e3 8393static bfd_boolean
c152c796 8394elf_link_adjust_relocs (bfd *abfd,
9eaff861 8395 asection *sec,
28dbcedc
AM
8396 struct bfd_elf_section_reloc_data *reldata,
8397 bfd_boolean sort)
c152c796
AM
8398{
8399 unsigned int i;
8400 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8401 bfd_byte *erela;
8402 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8403 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8404 bfd_vma r_type_mask;
8405 int r_sym_shift;
d4730f92
BS
8406 unsigned int count = reldata->count;
8407 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8408
d4730f92 8409 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8410 {
8411 swap_in = bed->s->swap_reloc_in;
8412 swap_out = bed->s->swap_reloc_out;
8413 }
d4730f92 8414 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8415 {
8416 swap_in = bed->s->swap_reloca_in;
8417 swap_out = bed->s->swap_reloca_out;
8418 }
8419 else
8420 abort ();
8421
8422 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8423 abort ();
8424
8425 if (bed->s->arch_size == 32)
8426 {
8427 r_type_mask = 0xff;
8428 r_sym_shift = 8;
8429 }
8430 else
8431 {
8432 r_type_mask = 0xffffffff;
8433 r_sym_shift = 32;
8434 }
8435
d4730f92
BS
8436 erela = reldata->hdr->contents;
8437 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8438 {
8439 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8440 unsigned int j;
8441
8442 if (*rel_hash == NULL)
8443 continue;
8444
8445 BFD_ASSERT ((*rel_hash)->indx >= 0);
8446
8447 (*swap_in) (abfd, erela, irela);
8448 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8449 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8450 | (irela[j].r_info & r_type_mask));
8451 (*swap_out) (abfd, irela, erela);
8452 }
53df40a4 8453
9eaff861
AO
8454 if (bed->elf_backend_update_relocs)
8455 (*bed->elf_backend_update_relocs) (sec, reldata);
8456
0e287786 8457 if (sort && count != 0)
53df40a4 8458 {
0e287786
AM
8459 bfd_vma (*ext_r_off) (const void *);
8460 bfd_vma r_off;
8461 size_t elt_size;
8462 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8463 bfd_byte *buf = NULL;
28dbcedc
AM
8464
8465 if (bed->s->arch_size == 32)
8466 {
8467 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8468 ext_r_off = ext32l_r_offset;
28dbcedc 8469 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8470 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8471 else
8472 abort ();
8473 }
53df40a4 8474 else
28dbcedc 8475 {
53df40a4 8476#ifdef BFD_HOST_64_BIT
28dbcedc 8477 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8478 ext_r_off = ext64l_r_offset;
28dbcedc 8479 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8480 ext_r_off = ext64b_r_offset;
28dbcedc 8481 else
53df40a4 8482#endif
28dbcedc
AM
8483 abort ();
8484 }
0e287786 8485
bca6d0e3
AM
8486 /* Must use a stable sort here. A modified insertion sort,
8487 since the relocs are mostly sorted already. */
0e287786
AM
8488 elt_size = reldata->hdr->sh_entsize;
8489 base = reldata->hdr->contents;
8490 end = base + count * elt_size;
bca6d0e3 8491 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8492 abort ();
8493
8494 /* Ensure the first element is lowest. This acts as a sentinel,
8495 speeding the main loop below. */
8496 r_off = (*ext_r_off) (base);
8497 for (p = loc = base; (p += elt_size) < end; )
8498 {
8499 bfd_vma r_off2 = (*ext_r_off) (p);
8500 if (r_off > r_off2)
8501 {
8502 r_off = r_off2;
8503 loc = p;
8504 }
8505 }
8506 if (loc != base)
8507 {
8508 /* Don't just swap *base and *loc as that changes the order
8509 of the original base[0] and base[1] if they happen to
8510 have the same r_offset. */
bca6d0e3
AM
8511 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8512 memcpy (onebuf, loc, elt_size);
0e287786 8513 memmove (base + elt_size, base, loc - base);
bca6d0e3 8514 memcpy (base, onebuf, elt_size);
0e287786
AM
8515 }
8516
b29b8669 8517 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8518 {
8519 /* base to p is sorted, *p is next to insert. */
8520 r_off = (*ext_r_off) (p);
8521 /* Search the sorted region for location to insert. */
8522 loc = p - elt_size;
8523 while (r_off < (*ext_r_off) (loc))
8524 loc -= elt_size;
8525 loc += elt_size;
8526 if (loc != p)
8527 {
bca6d0e3
AM
8528 /* Chances are there is a run of relocs to insert here,
8529 from one of more input files. Files are not always
8530 linked in order due to the way elf_link_input_bfd is
8531 called. See pr17666. */
8532 size_t sortlen = p - loc;
8533 bfd_vma r_off2 = (*ext_r_off) (loc);
8534 size_t runlen = elt_size;
8535 size_t buf_size = 96 * 1024;
8536 while (p + runlen < end
8537 && (sortlen <= buf_size
8538 || runlen + elt_size <= buf_size)
8539 && r_off2 > (*ext_r_off) (p + runlen))
8540 runlen += elt_size;
8541 if (buf == NULL)
8542 {
8543 buf = bfd_malloc (buf_size);
8544 if (buf == NULL)
8545 return FALSE;
8546 }
8547 if (runlen < sortlen)
8548 {
8549 memcpy (buf, p, runlen);
8550 memmove (loc + runlen, loc, sortlen);
8551 memcpy (loc, buf, runlen);
8552 }
8553 else
8554 {
8555 memcpy (buf, loc, sortlen);
8556 memmove (loc, p, runlen);
8557 memcpy (loc + runlen, buf, sortlen);
8558 }
b29b8669 8559 p += runlen - elt_size;
0e287786
AM
8560 }
8561 }
8562 /* Hashes are no longer valid. */
28dbcedc
AM
8563 free (reldata->hashes);
8564 reldata->hashes = NULL;
bca6d0e3 8565 free (buf);
53df40a4 8566 }
bca6d0e3 8567 return TRUE;
c152c796
AM
8568}
8569
8570struct elf_link_sort_rela
8571{
8572 union {
8573 bfd_vma offset;
8574 bfd_vma sym_mask;
8575 } u;
8576 enum elf_reloc_type_class type;
8577 /* We use this as an array of size int_rels_per_ext_rel. */
8578 Elf_Internal_Rela rela[1];
8579};
8580
8581static int
8582elf_link_sort_cmp1 (const void *A, const void *B)
8583{
a50b1753
NC
8584 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8585 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8586 int relativea, relativeb;
8587
8588 relativea = a->type == reloc_class_relative;
8589 relativeb = b->type == reloc_class_relative;
8590
8591 if (relativea < relativeb)
8592 return 1;
8593 if (relativea > relativeb)
8594 return -1;
8595 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8596 return -1;
8597 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8598 return 1;
8599 if (a->rela->r_offset < b->rela->r_offset)
8600 return -1;
8601 if (a->rela->r_offset > b->rela->r_offset)
8602 return 1;
8603 return 0;
8604}
8605
8606static int
8607elf_link_sort_cmp2 (const void *A, const void *B)
8608{
a50b1753
NC
8609 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8610 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8611
7e612e98 8612 if (a->type < b->type)
c152c796 8613 return -1;
7e612e98 8614 if (a->type > b->type)
c152c796 8615 return 1;
7e612e98 8616 if (a->u.offset < b->u.offset)
c152c796 8617 return -1;
7e612e98 8618 if (a->u.offset > b->u.offset)
c152c796
AM
8619 return 1;
8620 if (a->rela->r_offset < b->rela->r_offset)
8621 return -1;
8622 if (a->rela->r_offset > b->rela->r_offset)
8623 return 1;
8624 return 0;
8625}
8626
8627static size_t
8628elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8629{
3410fea8 8630 asection *dynamic_relocs;
fc66a176
L
8631 asection *rela_dyn;
8632 asection *rel_dyn;
c152c796
AM
8633 bfd_size_type count, size;
8634 size_t i, ret, sort_elt, ext_size;
8635 bfd_byte *sort, *s_non_relative, *p;
8636 struct elf_link_sort_rela *sq;
8637 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8638 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8639 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8640 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8641 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8642 struct bfd_link_order *lo;
8643 bfd_vma r_sym_mask;
3410fea8 8644 bfd_boolean use_rela;
c152c796 8645
3410fea8
NC
8646 /* Find a dynamic reloc section. */
8647 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8648 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8649 if (rela_dyn != NULL && rela_dyn->size > 0
8650 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8651 {
3410fea8
NC
8652 bfd_boolean use_rela_initialised = FALSE;
8653
8654 /* This is just here to stop gcc from complaining.
c8e44c6d 8655 Its initialization checking code is not perfect. */
3410fea8
NC
8656 use_rela = TRUE;
8657
8658 /* Both sections are present. Examine the sizes
8659 of the indirect sections to help us choose. */
8660 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8661 if (lo->type == bfd_indirect_link_order)
8662 {
8663 asection *o = lo->u.indirect.section;
8664
8665 if ((o->size % bed->s->sizeof_rela) == 0)
8666 {
8667 if ((o->size % bed->s->sizeof_rel) == 0)
8668 /* Section size is divisible by both rel and rela sizes.
8669 It is of no help to us. */
8670 ;
8671 else
8672 {
8673 /* Section size is only divisible by rela. */
8674 if (use_rela_initialised && (use_rela == FALSE))
8675 {
c8e44c6d
AM
8676 _bfd_error_handler (_("%B: Unable to sort relocs - "
8677 "they are in more than one size"),
8678 abfd);
3410fea8
NC
8679 bfd_set_error (bfd_error_invalid_operation);
8680 return 0;
8681 }
8682 else
8683 {
8684 use_rela = TRUE;
8685 use_rela_initialised = TRUE;
8686 }
8687 }
8688 }
8689 else if ((o->size % bed->s->sizeof_rel) == 0)
8690 {
8691 /* Section size is only divisible by rel. */
8692 if (use_rela_initialised && (use_rela == TRUE))
8693 {
c8e44c6d
AM
8694 _bfd_error_handler (_("%B: Unable to sort relocs - "
8695 "they are in more than one size"),
8696 abfd);
3410fea8
NC
8697 bfd_set_error (bfd_error_invalid_operation);
8698 return 0;
8699 }
8700 else
8701 {
8702 use_rela = FALSE;
8703 use_rela_initialised = TRUE;
8704 }
8705 }
8706 else
8707 {
c8e44c6d
AM
8708 /* The section size is not divisible by either -
8709 something is wrong. */
8710 _bfd_error_handler (_("%B: Unable to sort relocs - "
8711 "they are of an unknown size"), abfd);
3410fea8
NC
8712 bfd_set_error (bfd_error_invalid_operation);
8713 return 0;
8714 }
8715 }
8716
8717 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8718 if (lo->type == bfd_indirect_link_order)
8719 {
8720 asection *o = lo->u.indirect.section;
8721
8722 if ((o->size % bed->s->sizeof_rela) == 0)
8723 {
8724 if ((o->size % bed->s->sizeof_rel) == 0)
8725 /* Section size is divisible by both rel and rela sizes.
8726 It is of no help to us. */
8727 ;
8728 else
8729 {
8730 /* Section size is only divisible by rela. */
8731 if (use_rela_initialised && (use_rela == FALSE))
8732 {
c8e44c6d
AM
8733 _bfd_error_handler (_("%B: Unable to sort relocs - "
8734 "they are in more than one size"),
8735 abfd);
3410fea8
NC
8736 bfd_set_error (bfd_error_invalid_operation);
8737 return 0;
8738 }
8739 else
8740 {
8741 use_rela = TRUE;
8742 use_rela_initialised = TRUE;
8743 }
8744 }
8745 }
8746 else if ((o->size % bed->s->sizeof_rel) == 0)
8747 {
8748 /* Section size is only divisible by rel. */
8749 if (use_rela_initialised && (use_rela == TRUE))
8750 {
c8e44c6d
AM
8751 _bfd_error_handler (_("%B: Unable to sort relocs - "
8752 "they are in more than one size"),
8753 abfd);
3410fea8
NC
8754 bfd_set_error (bfd_error_invalid_operation);
8755 return 0;
8756 }
8757 else
8758 {
8759 use_rela = FALSE;
8760 use_rela_initialised = TRUE;
8761 }
8762 }
8763 else
8764 {
c8e44c6d
AM
8765 /* The section size is not divisible by either -
8766 something is wrong. */
8767 _bfd_error_handler (_("%B: Unable to sort relocs - "
8768 "they are of an unknown size"), abfd);
3410fea8
NC
8769 bfd_set_error (bfd_error_invalid_operation);
8770 return 0;
8771 }
8772 }
8773
8774 if (! use_rela_initialised)
8775 /* Make a guess. */
8776 use_rela = TRUE;
c152c796 8777 }
fc66a176
L
8778 else if (rela_dyn != NULL && rela_dyn->size > 0)
8779 use_rela = TRUE;
8780 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8781 use_rela = FALSE;
c152c796 8782 else
fc66a176 8783 return 0;
3410fea8
NC
8784
8785 if (use_rela)
c152c796 8786 {
3410fea8 8787 dynamic_relocs = rela_dyn;
c152c796
AM
8788 ext_size = bed->s->sizeof_rela;
8789 swap_in = bed->s->swap_reloca_in;
8790 swap_out = bed->s->swap_reloca_out;
8791 }
3410fea8
NC
8792 else
8793 {
8794 dynamic_relocs = rel_dyn;
8795 ext_size = bed->s->sizeof_rel;
8796 swap_in = bed->s->swap_reloc_in;
8797 swap_out = bed->s->swap_reloc_out;
8798 }
c152c796
AM
8799
8800 size = 0;
3410fea8 8801 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8802 if (lo->type == bfd_indirect_link_order)
3410fea8 8803 size += lo->u.indirect.section->size;
c152c796 8804
3410fea8 8805 if (size != dynamic_relocs->size)
c152c796
AM
8806 return 0;
8807
8808 sort_elt = (sizeof (struct elf_link_sort_rela)
8809 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8810
8811 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8812 if (count == 0)
8813 return 0;
a50b1753 8814 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8815
c152c796
AM
8816 if (sort == NULL)
8817 {
8818 (*info->callbacks->warning)
8819 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8820 return 0;
8821 }
8822
8823 if (bed->s->arch_size == 32)
8824 r_sym_mask = ~(bfd_vma) 0xff;
8825 else
8826 r_sym_mask = ~(bfd_vma) 0xffffffff;
8827
3410fea8 8828 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8829 if (lo->type == bfd_indirect_link_order)
8830 {
8831 bfd_byte *erel, *erelend;
8832 asection *o = lo->u.indirect.section;
8833
1da212d6
AM
8834 if (o->contents == NULL && o->size != 0)
8835 {
8836 /* This is a reloc section that is being handled as a normal
8837 section. See bfd_section_from_shdr. We can't combine
8838 relocs in this case. */
8839 free (sort);
8840 return 0;
8841 }
c152c796 8842 erel = o->contents;
eea6121a 8843 erelend = o->contents + o->size;
c8e44c6d 8844 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8845
c152c796
AM
8846 while (erel < erelend)
8847 {
8848 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8849
c152c796 8850 (*swap_in) (abfd, erel, s->rela);
7e612e98 8851 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8852 s->u.sym_mask = r_sym_mask;
8853 p += sort_elt;
8854 erel += ext_size;
8855 }
8856 }
8857
8858 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8859
8860 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8861 {
8862 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8863 if (s->type != reloc_class_relative)
8864 break;
8865 }
8866 ret = i;
8867 s_non_relative = p;
8868
8869 sq = (struct elf_link_sort_rela *) s_non_relative;
8870 for (; i < count; i++, p += sort_elt)
8871 {
8872 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8873 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8874 sq = sp;
8875 sp->u.offset = sq->rela->r_offset;
8876 }
8877
8878 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8879
c8e44c6d
AM
8880 struct elf_link_hash_table *htab = elf_hash_table (info);
8881 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8882 {
8883 /* We have plt relocs in .rela.dyn. */
8884 sq = (struct elf_link_sort_rela *) sort;
8885 for (i = 0; i < count; i++)
8886 if (sq[count - i - 1].type != reloc_class_plt)
8887 break;
8888 if (i != 0 && htab->srelplt->size == i * ext_size)
8889 {
8890 struct bfd_link_order **plo;
8891 /* Put srelplt link_order last. This is so the output_offset
8892 set in the next loop is correct for DT_JMPREL. */
8893 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8894 if ((*plo)->type == bfd_indirect_link_order
8895 && (*plo)->u.indirect.section == htab->srelplt)
8896 {
8897 lo = *plo;
8898 *plo = lo->next;
8899 }
8900 else
8901 plo = &(*plo)->next;
8902 *plo = lo;
8903 lo->next = NULL;
8904 dynamic_relocs->map_tail.link_order = lo;
8905 }
8906 }
8907
8908 p = sort;
3410fea8 8909 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8910 if (lo->type == bfd_indirect_link_order)
8911 {
8912 bfd_byte *erel, *erelend;
8913 asection *o = lo->u.indirect.section;
8914
8915 erel = o->contents;
eea6121a 8916 erelend = o->contents + o->size;
c8e44c6d 8917 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8918 while (erel < erelend)
8919 {
8920 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8921 (*swap_out) (abfd, s->rela, erel);
8922 p += sort_elt;
8923 erel += ext_size;
8924 }
8925 }
8926
8927 free (sort);
3410fea8 8928 *psec = dynamic_relocs;
c152c796
AM
8929 return ret;
8930}
8931
ef10c3ac 8932/* Add a symbol to the output symbol string table. */
c152c796 8933
6e0b88f1 8934static int
ef10c3ac
L
8935elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8936 const char *name,
8937 Elf_Internal_Sym *elfsym,
8938 asection *input_sec,
8939 struct elf_link_hash_entry *h)
c152c796 8940{
6e0b88f1 8941 int (*output_symbol_hook)
c152c796
AM
8942 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8943 struct elf_link_hash_entry *);
ef10c3ac 8944 struct elf_link_hash_table *hash_table;
c152c796 8945 const struct elf_backend_data *bed;
ef10c3ac 8946 bfd_size_type strtabsize;
c152c796 8947
8539e4e8
AM
8948 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8949
8b127cbc 8950 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8951 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8952 if (output_symbol_hook != NULL)
8953 {
8b127cbc 8954 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8955 if (ret != 1)
8956 return ret;
c152c796
AM
8957 }
8958
ef10c3ac
L
8959 if (name == NULL
8960 || *name == '\0'
8961 || (input_sec->flags & SEC_EXCLUDE))
8962 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8963 else
8964 {
ef10c3ac
L
8965 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8966 to get the final offset for st_name. */
8967 elfsym->st_name
8968 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8969 name, FALSE);
c152c796 8970 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8971 return 0;
c152c796
AM
8972 }
8973
ef10c3ac
L
8974 hash_table = elf_hash_table (flinfo->info);
8975 strtabsize = hash_table->strtabsize;
8976 if (strtabsize <= hash_table->strtabcount)
c152c796 8977 {
ef10c3ac
L
8978 strtabsize += strtabsize;
8979 hash_table->strtabsize = strtabsize;
8980 strtabsize *= sizeof (*hash_table->strtab);
8981 hash_table->strtab
8982 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8983 strtabsize);
8984 if (hash_table->strtab == NULL)
6e0b88f1 8985 return 0;
c152c796 8986 }
ef10c3ac
L
8987 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8988 hash_table->strtab[hash_table->strtabcount].dest_index
8989 = hash_table->strtabcount;
8990 hash_table->strtab[hash_table->strtabcount].destshndx_index
8991 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8992
8993 bfd_get_symcount (flinfo->output_bfd) += 1;
8994 hash_table->strtabcount += 1;
8995
8996 return 1;
8997}
8998
8999/* Swap symbols out to the symbol table and flush the output symbols to
9000 the file. */
9001
9002static bfd_boolean
9003elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9004{
9005 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9006 bfd_size_type amt;
9007 size_t i;
ef10c3ac
L
9008 const struct elf_backend_data *bed;
9009 bfd_byte *symbuf;
9010 Elf_Internal_Shdr *hdr;
9011 file_ptr pos;
9012 bfd_boolean ret;
9013
9014 if (!hash_table->strtabcount)
9015 return TRUE;
9016
9017 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9018
9019 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9020
ef10c3ac
L
9021 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9022 symbuf = (bfd_byte *) bfd_malloc (amt);
9023 if (symbuf == NULL)
9024 return FALSE;
1b786873 9025
ef10c3ac 9026 if (flinfo->symshndxbuf)
c152c796 9027 {
ef53be89
AM
9028 amt = sizeof (Elf_External_Sym_Shndx);
9029 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9030 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9031 if (flinfo->symshndxbuf == NULL)
c152c796 9032 {
ef10c3ac
L
9033 free (symbuf);
9034 return FALSE;
c152c796 9035 }
c152c796
AM
9036 }
9037
ef10c3ac
L
9038 for (i = 0; i < hash_table->strtabcount; i++)
9039 {
9040 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9041 if (elfsym->sym.st_name == (unsigned long) -1)
9042 elfsym->sym.st_name = 0;
9043 else
9044 elfsym->sym.st_name
9045 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9046 elfsym->sym.st_name);
9047 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9048 ((bfd_byte *) symbuf
9049 + (elfsym->dest_index
9050 * bed->s->sizeof_sym)),
9051 (flinfo->symshndxbuf
9052 + elfsym->destshndx_index));
9053 }
9054
9055 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9056 pos = hdr->sh_offset + hdr->sh_size;
9057 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9058 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9059 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9060 {
9061 hdr->sh_size += amt;
9062 ret = TRUE;
9063 }
9064 else
9065 ret = FALSE;
c152c796 9066
ef10c3ac
L
9067 free (symbuf);
9068
9069 free (hash_table->strtab);
9070 hash_table->strtab = NULL;
9071
9072 return ret;
c152c796
AM
9073}
9074
c0d5a53d
L
9075/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9076
9077static bfd_boolean
9078check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9079{
4fbb74a6
AM
9080 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9081 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9082 {
9083 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9084 beyond 64k. */
4eca0228 9085 _bfd_error_handler
695344c0 9086 /* xgettext:c-format */
c0d5a53d 9087 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9088 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9089 bfd_set_error (bfd_error_nonrepresentable_section);
9090 return FALSE;
9091 }
9092 return TRUE;
9093}
9094
c152c796
AM
9095/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9096 allowing an unsatisfied unversioned symbol in the DSO to match a
9097 versioned symbol that would normally require an explicit version.
9098 We also handle the case that a DSO references a hidden symbol
9099 which may be satisfied by a versioned symbol in another DSO. */
9100
9101static bfd_boolean
9102elf_link_check_versioned_symbol (struct bfd_link_info *info,
9103 const struct elf_backend_data *bed,
9104 struct elf_link_hash_entry *h)
9105{
9106 bfd *abfd;
9107 struct elf_link_loaded_list *loaded;
9108
9109 if (!is_elf_hash_table (info->hash))
9110 return FALSE;
9111
90c984fc
L
9112 /* Check indirect symbol. */
9113 while (h->root.type == bfd_link_hash_indirect)
9114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9115
c152c796
AM
9116 switch (h->root.type)
9117 {
9118 default:
9119 abfd = NULL;
9120 break;
9121
9122 case bfd_link_hash_undefined:
9123 case bfd_link_hash_undefweak:
9124 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9125 if (abfd == NULL
9126 || (abfd->flags & DYNAMIC) == 0
e56f61be 9127 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9128 return FALSE;
9129 break;
9130
9131 case bfd_link_hash_defined:
9132 case bfd_link_hash_defweak:
9133 abfd = h->root.u.def.section->owner;
9134 break;
9135
9136 case bfd_link_hash_common:
9137 abfd = h->root.u.c.p->section->owner;
9138 break;
9139 }
9140 BFD_ASSERT (abfd != NULL);
9141
9142 for (loaded = elf_hash_table (info)->loaded;
9143 loaded != NULL;
9144 loaded = loaded->next)
9145 {
9146 bfd *input;
9147 Elf_Internal_Shdr *hdr;
ef53be89
AM
9148 size_t symcount;
9149 size_t extsymcount;
9150 size_t extsymoff;
c152c796
AM
9151 Elf_Internal_Shdr *versymhdr;
9152 Elf_Internal_Sym *isym;
9153 Elf_Internal_Sym *isymend;
9154 Elf_Internal_Sym *isymbuf;
9155 Elf_External_Versym *ever;
9156 Elf_External_Versym *extversym;
9157
9158 input = loaded->abfd;
9159
9160 /* We check each DSO for a possible hidden versioned definition. */
9161 if (input == abfd
9162 || (input->flags & DYNAMIC) == 0
9163 || elf_dynversym (input) == 0)
9164 continue;
9165
9166 hdr = &elf_tdata (input)->dynsymtab_hdr;
9167
9168 symcount = hdr->sh_size / bed->s->sizeof_sym;
9169 if (elf_bad_symtab (input))
9170 {
9171 extsymcount = symcount;
9172 extsymoff = 0;
9173 }
9174 else
9175 {
9176 extsymcount = symcount - hdr->sh_info;
9177 extsymoff = hdr->sh_info;
9178 }
9179
9180 if (extsymcount == 0)
9181 continue;
9182
9183 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9184 NULL, NULL, NULL);
9185 if (isymbuf == NULL)
9186 return FALSE;
9187
9188 /* Read in any version definitions. */
9189 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9190 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9191 if (extversym == NULL)
9192 goto error_ret;
9193
9194 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9195 || (bfd_bread (extversym, versymhdr->sh_size, input)
9196 != versymhdr->sh_size))
9197 {
9198 free (extversym);
9199 error_ret:
9200 free (isymbuf);
9201 return FALSE;
9202 }
9203
9204 ever = extversym + extsymoff;
9205 isymend = isymbuf + extsymcount;
9206 for (isym = isymbuf; isym < isymend; isym++, ever++)
9207 {
9208 const char *name;
9209 Elf_Internal_Versym iver;
9210 unsigned short version_index;
9211
9212 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9213 || isym->st_shndx == SHN_UNDEF)
9214 continue;
9215
9216 name = bfd_elf_string_from_elf_section (input,
9217 hdr->sh_link,
9218 isym->st_name);
9219 if (strcmp (name, h->root.root.string) != 0)
9220 continue;
9221
9222 _bfd_elf_swap_versym_in (input, ever, &iver);
9223
d023c380
L
9224 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9225 && !(h->def_regular
9226 && h->forced_local))
c152c796
AM
9227 {
9228 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9229 have provided a definition for the undefined sym unless
9230 it is defined in a non-shared object and forced local.
9231 */
c152c796
AM
9232 abort ();
9233 }
9234
9235 version_index = iver.vs_vers & VERSYM_VERSION;
9236 if (version_index == 1 || version_index == 2)
9237 {
9238 /* This is the base or first version. We can use it. */
9239 free (extversym);
9240 free (isymbuf);
9241 return TRUE;
9242 }
9243 }
9244
9245 free (extversym);
9246 free (isymbuf);
9247 }
9248
9249 return FALSE;
9250}
9251
b8871f35
L
9252/* Convert ELF common symbol TYPE. */
9253
9254static int
9255elf_link_convert_common_type (struct bfd_link_info *info, int type)
9256{
9257 /* Commom symbol can only appear in relocatable link. */
9258 if (!bfd_link_relocatable (info))
9259 abort ();
9260 switch (info->elf_stt_common)
9261 {
9262 case unchanged:
9263 break;
9264 case elf_stt_common:
9265 type = STT_COMMON;
9266 break;
9267 case no_elf_stt_common:
9268 type = STT_OBJECT;
9269 break;
9270 }
9271 return type;
9272}
9273
c152c796
AM
9274/* Add an external symbol to the symbol table. This is called from
9275 the hash table traversal routine. When generating a shared object,
9276 we go through the symbol table twice. The first time we output
9277 anything that might have been forced to local scope in a version
9278 script. The second time we output the symbols that are still
9279 global symbols. */
9280
9281static bfd_boolean
7686d77d 9282elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9283{
7686d77d 9284 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9285 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9286 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9287 bfd_boolean strip;
9288 Elf_Internal_Sym sym;
9289 asection *input_sec;
9290 const struct elf_backend_data *bed;
6e0b88f1
AM
9291 long indx;
9292 int ret;
b8871f35 9293 unsigned int type;
c152c796
AM
9294
9295 if (h->root.type == bfd_link_hash_warning)
9296 {
9297 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9298 if (h->root.type == bfd_link_hash_new)
9299 return TRUE;
9300 }
9301
9302 /* Decide whether to output this symbol in this pass. */
9303 if (eoinfo->localsyms)
9304 {
4deb8f71 9305 if (!h->forced_local)
c152c796
AM
9306 return TRUE;
9307 }
9308 else
9309 {
4deb8f71 9310 if (h->forced_local)
c152c796
AM
9311 return TRUE;
9312 }
9313
8b127cbc 9314 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9315
12ac1cf5 9316 if (h->root.type == bfd_link_hash_undefined)
c152c796 9317 {
12ac1cf5
NC
9318 /* If we have an undefined symbol reference here then it must have
9319 come from a shared library that is being linked in. (Undefined
98da7939
L
9320 references in regular files have already been handled unless
9321 they are in unreferenced sections which are removed by garbage
9322 collection). */
12ac1cf5
NC
9323 bfd_boolean ignore_undef = FALSE;
9324
9325 /* Some symbols may be special in that the fact that they're
9326 undefined can be safely ignored - let backend determine that. */
9327 if (bed->elf_backend_ignore_undef_symbol)
9328 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9329
9330 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9331 if (!ignore_undef
12ac1cf5 9332 && h->ref_dynamic
8b127cbc
AM
9333 && (!h->ref_regular || flinfo->info->gc_sections)
9334 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9335 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9336 (*flinfo->info->callbacks->undefined_symbol)
9337 (flinfo->info, h->root.root.string,
9338 h->ref_regular ? NULL : h->root.u.undef.abfd,
9339 NULL, 0,
9340 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9341
9342 /* Strip a global symbol defined in a discarded section. */
9343 if (h->indx == -3)
9344 return TRUE;
c152c796
AM
9345 }
9346
9347 /* We should also warn if a forced local symbol is referenced from
9348 shared libraries. */
0e1862bb 9349 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9350 && h->forced_local
9351 && h->ref_dynamic
371a5866 9352 && h->def_regular
f5385ebf 9353 && !h->dynamic_def
ee659f1f 9354 && h->ref_dynamic_nonweak
8b127cbc 9355 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9356 {
17d078c5
AM
9357 bfd *def_bfd;
9358 const char *msg;
90c984fc
L
9359 struct elf_link_hash_entry *hi = h;
9360
9361 /* Check indirect symbol. */
9362 while (hi->root.type == bfd_link_hash_indirect)
9363 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9364
9365 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9366 /* xgettext:c-format */
17d078c5
AM
9367 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9368 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9369 /* xgettext:c-format */
17d078c5
AM
9370 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9371 else
695344c0 9372 /* xgettext:c-format */
17d078c5 9373 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9374 def_bfd = flinfo->output_bfd;
90c984fc
L
9375 if (hi->root.u.def.section != bfd_abs_section_ptr)
9376 def_bfd = hi->root.u.def.section->owner;
4eca0228
AM
9377 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9378 h->root.root.string);
17d078c5 9379 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9380 eoinfo->failed = TRUE;
9381 return FALSE;
9382 }
9383
9384 /* We don't want to output symbols that have never been mentioned by
9385 a regular file, or that we have been told to strip. However, if
9386 h->indx is set to -2, the symbol is used by a reloc and we must
9387 output it. */
d983c8c5 9388 strip = FALSE;
c152c796 9389 if (h->indx == -2)
d983c8c5 9390 ;
f5385ebf 9391 else if ((h->def_dynamic
77cfaee6
AM
9392 || h->ref_dynamic
9393 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9394 && !h->def_regular
9395 && !h->ref_regular)
c152c796 9396 strip = TRUE;
8b127cbc 9397 else if (flinfo->info->strip == strip_all)
c152c796 9398 strip = TRUE;
8b127cbc
AM
9399 else if (flinfo->info->strip == strip_some
9400 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9401 h->root.root.string, FALSE, FALSE) == NULL)
9402 strip = TRUE;
d56d55e7
AM
9403 else if ((h->root.type == bfd_link_hash_defined
9404 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9405 && ((flinfo->info->strip_discarded
dbaa2011 9406 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9407 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9408 && h->root.u.def.section->owner != NULL
d56d55e7 9409 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9410 strip = TRUE;
9e2278f5
AM
9411 else if ((h->root.type == bfd_link_hash_undefined
9412 || h->root.type == bfd_link_hash_undefweak)
9413 && h->root.u.undef.abfd != NULL
9414 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9415 strip = TRUE;
c152c796 9416
b8871f35
L
9417 type = h->type;
9418
c152c796 9419 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9420 nothing else to do. However, if it is a forced local symbol or
9421 an ifunc symbol we need to give the backend finish_dynamic_symbol
9422 function a chance to make it dynamic. */
c152c796
AM
9423 if (strip
9424 && h->dynindx == -1
b8871f35 9425 && type != STT_GNU_IFUNC
f5385ebf 9426 && !h->forced_local)
c152c796
AM
9427 return TRUE;
9428
9429 sym.st_value = 0;
9430 sym.st_size = h->size;
9431 sym.st_other = h->other;
c152c796
AM
9432 switch (h->root.type)
9433 {
9434 default:
9435 case bfd_link_hash_new:
9436 case bfd_link_hash_warning:
9437 abort ();
9438 return FALSE;
9439
9440 case bfd_link_hash_undefined:
9441 case bfd_link_hash_undefweak:
9442 input_sec = bfd_und_section_ptr;
9443 sym.st_shndx = SHN_UNDEF;
9444 break;
9445
9446 case bfd_link_hash_defined:
9447 case bfd_link_hash_defweak:
9448 {
9449 input_sec = h->root.u.def.section;
9450 if (input_sec->output_section != NULL)
9451 {
9452 sym.st_shndx =
8b127cbc 9453 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9454 input_sec->output_section);
9455 if (sym.st_shndx == SHN_BAD)
9456 {
4eca0228 9457 _bfd_error_handler
695344c0 9458 /* xgettext:c-format */
d003868e 9459 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9460 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9461 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9462 eoinfo->failed = TRUE;
9463 return FALSE;
9464 }
9465
9466 /* ELF symbols in relocatable files are section relative,
9467 but in nonrelocatable files they are virtual
9468 addresses. */
9469 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9470 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9471 {
9472 sym.st_value += input_sec->output_section->vma;
9473 if (h->type == STT_TLS)
9474 {
8b127cbc 9475 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9476 if (tls_sec != NULL)
9477 sym.st_value -= tls_sec->vma;
c152c796
AM
9478 }
9479 }
9480 }
9481 else
9482 {
9483 BFD_ASSERT (input_sec->owner == NULL
9484 || (input_sec->owner->flags & DYNAMIC) != 0);
9485 sym.st_shndx = SHN_UNDEF;
9486 input_sec = bfd_und_section_ptr;
9487 }
9488 }
9489 break;
9490
9491 case bfd_link_hash_common:
9492 input_sec = h->root.u.c.p->section;
a4d8e49b 9493 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9494 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9495 break;
9496
9497 case bfd_link_hash_indirect:
9498 /* These symbols are created by symbol versioning. They point
9499 to the decorated version of the name. For example, if the
9500 symbol foo@@GNU_1.2 is the default, which should be used when
9501 foo is used with no version, then we add an indirect symbol
9502 foo which points to foo@@GNU_1.2. We ignore these symbols,
9503 since the indirected symbol is already in the hash table. */
9504 return TRUE;
9505 }
9506
b8871f35
L
9507 if (type == STT_COMMON || type == STT_OBJECT)
9508 switch (h->root.type)
9509 {
9510 case bfd_link_hash_common:
9511 type = elf_link_convert_common_type (flinfo->info, type);
9512 break;
9513 case bfd_link_hash_defined:
9514 case bfd_link_hash_defweak:
9515 if (bed->common_definition (&sym))
9516 type = elf_link_convert_common_type (flinfo->info, type);
9517 else
9518 type = STT_OBJECT;
9519 break;
9520 case bfd_link_hash_undefined:
9521 case bfd_link_hash_undefweak:
9522 break;
9523 default:
9524 abort ();
9525 }
9526
4deb8f71 9527 if (h->forced_local)
b8871f35
L
9528 {
9529 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9530 /* Turn off visibility on local symbol. */
9531 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9532 }
9533 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9534 else if (h->unique_global && h->def_regular)
9535 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9536 else if (h->root.type == bfd_link_hash_undefweak
9537 || h->root.type == bfd_link_hash_defweak)
9538 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9539 else
9540 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9541 sym.st_target_internal = h->target_internal;
9542
c152c796
AM
9543 /* Give the processor backend a chance to tweak the symbol value,
9544 and also to finish up anything that needs to be done for this
9545 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9546 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9547 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9548 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9549 && h->def_regular
0e1862bb 9550 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9551 || ((h->dynindx != -1
9552 || h->forced_local)
0e1862bb 9553 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9554 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9555 || h->root.type != bfd_link_hash_undefweak))
9556 || !h->forced_local)
8b127cbc 9557 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9558 {
9559 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9560 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9561 {
9562 eoinfo->failed = TRUE;
9563 return FALSE;
9564 }
9565 }
9566
9567 /* If we are marking the symbol as undefined, and there are no
9568 non-weak references to this symbol from a regular object, then
9569 mark the symbol as weak undefined; if there are non-weak
9570 references, mark the symbol as strong. We can't do this earlier,
9571 because it might not be marked as undefined until the
9572 finish_dynamic_symbol routine gets through with it. */
9573 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9574 && h->ref_regular
c152c796
AM
9575 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9576 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9577 {
9578 int bindtype;
b8871f35 9579 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9580
9581 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9582 if (type == STT_GNU_IFUNC)
9583 type = STT_FUNC;
c152c796 9584
f5385ebf 9585 if (h->ref_regular_nonweak)
c152c796
AM
9586 bindtype = STB_GLOBAL;
9587 else
9588 bindtype = STB_WEAK;
2955ec4c 9589 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9590 }
9591
bda987c2
CD
9592 /* If this is a symbol defined in a dynamic library, don't use the
9593 symbol size from the dynamic library. Relinking an executable
9594 against a new library may introduce gratuitous changes in the
9595 executable's symbols if we keep the size. */
9596 if (sym.st_shndx == SHN_UNDEF
9597 && !h->def_regular
9598 && h->def_dynamic)
9599 sym.st_size = 0;
9600
c152c796
AM
9601 /* If a non-weak symbol with non-default visibility is not defined
9602 locally, it is a fatal error. */
0e1862bb 9603 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9604 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9605 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9606 && h->root.type == bfd_link_hash_undefined
f5385ebf 9607 && !h->def_regular)
c152c796 9608 {
17d078c5
AM
9609 const char *msg;
9610
9611 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9612 /* xgettext:c-format */
17d078c5
AM
9613 msg = _("%B: protected symbol `%s' isn't defined");
9614 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9615 /* xgettext:c-format */
17d078c5
AM
9616 msg = _("%B: internal symbol `%s' isn't defined");
9617 else
695344c0 9618 /* xgettext:c-format */
17d078c5 9619 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9620 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9621 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9622 eoinfo->failed = TRUE;
9623 return FALSE;
9624 }
9625
9626 /* If this symbol should be put in the .dynsym section, then put it
9627 there now. We already know the symbol index. We also fill in
9628 the entry in the .hash section. */
cae1fbbb 9629 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9630 && h->dynindx != -1
8b127cbc 9631 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9632 {
c152c796
AM
9633 bfd_byte *esym;
9634
90c984fc
L
9635 /* Since there is no version information in the dynamic string,
9636 if there is no version info in symbol version section, we will
1659f720 9637 have a run-time problem if not linking executable, referenced
4deb8f71 9638 by shared library, or not bound locally. */
1659f720 9639 if (h->verinfo.verdef == NULL
0e1862bb 9640 && (!bfd_link_executable (flinfo->info)
1659f720
L
9641 || h->ref_dynamic
9642 || !h->def_regular))
90c984fc
L
9643 {
9644 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9645
9646 if (p && p [1] != '\0')
9647 {
4eca0228 9648 _bfd_error_handler
695344c0 9649 /* xgettext:c-format */
90c984fc
L
9650 (_("%B: No symbol version section for versioned symbol `%s'"),
9651 flinfo->output_bfd, h->root.root.string);
9652 eoinfo->failed = TRUE;
9653 return FALSE;
9654 }
9655 }
9656
c152c796 9657 sym.st_name = h->dynstr_index;
cae1fbbb
L
9658 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9659 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9660 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9661 {
9662 eoinfo->failed = TRUE;
9663 return FALSE;
9664 }
8b127cbc 9665 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9666
8b127cbc 9667 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9668 {
9669 size_t hash_entry_size;
9670 bfd_byte *bucketpos;
9671 bfd_vma chain;
41198d0c
L
9672 size_t bucketcount;
9673 size_t bucket;
9674
8b127cbc 9675 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9676 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9677
9678 hash_entry_size
8b127cbc
AM
9679 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9680 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9681 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9682 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9683 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9684 bucketpos);
9685 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9686 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9687 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9688 }
c152c796 9689
8b127cbc 9690 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9691 {
9692 Elf_Internal_Versym iversym;
9693 Elf_External_Versym *eversym;
9694
f5385ebf 9695 if (!h->def_regular)
c152c796 9696 {
7b20f099
AM
9697 if (h->verinfo.verdef == NULL
9698 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9699 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9700 iversym.vs_vers = 0;
9701 else
9702 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9703 }
9704 else
9705 {
9706 if (h->verinfo.vertree == NULL)
9707 iversym.vs_vers = 1;
9708 else
9709 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9710 if (flinfo->info->create_default_symver)
3e3b46e5 9711 iversym.vs_vers++;
c152c796
AM
9712 }
9713
422f1182 9714 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9715 defined locally. */
422f1182 9716 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9717 iversym.vs_vers |= VERSYM_HIDDEN;
9718
8b127cbc 9719 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9720 eversym += h->dynindx;
8b127cbc 9721 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9722 }
9723 }
9724
d983c8c5
AM
9725 /* If the symbol is undefined, and we didn't output it to .dynsym,
9726 strip it from .symtab too. Obviously we can't do this for
9727 relocatable output or when needed for --emit-relocs. */
9728 else if (input_sec == bfd_und_section_ptr
9729 && h->indx != -2
0e1862bb 9730 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9731 return TRUE;
9732 /* Also strip others that we couldn't earlier due to dynamic symbol
9733 processing. */
9734 if (strip)
9735 return TRUE;
9736 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9737 return TRUE;
9738
2ec55de3
AM
9739 /* Output a FILE symbol so that following locals are not associated
9740 with the wrong input file. We need one for forced local symbols
9741 if we've seen more than one FILE symbol or when we have exactly
9742 one FILE symbol but global symbols are present in a file other
9743 than the one with the FILE symbol. We also need one if linker
9744 defined symbols are present. In practice these conditions are
9745 always met, so just emit the FILE symbol unconditionally. */
9746 if (eoinfo->localsyms
9747 && !eoinfo->file_sym_done
9748 && eoinfo->flinfo->filesym_count != 0)
9749 {
9750 Elf_Internal_Sym fsym;
9751
9752 memset (&fsym, 0, sizeof (fsym));
9753 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9754 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9755 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9756 bfd_und_section_ptr, NULL))
2ec55de3
AM
9757 return FALSE;
9758
9759 eoinfo->file_sym_done = TRUE;
9760 }
9761
8b127cbc 9762 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9763 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9764 input_sec, h);
6e0b88f1 9765 if (ret == 0)
c152c796
AM
9766 {
9767 eoinfo->failed = TRUE;
9768 return FALSE;
9769 }
6e0b88f1
AM
9770 else if (ret == 1)
9771 h->indx = indx;
9772 else if (h->indx == -2)
9773 abort();
c152c796
AM
9774
9775 return TRUE;
9776}
9777
cdd3575c
AM
9778/* Return TRUE if special handling is done for relocs in SEC against
9779 symbols defined in discarded sections. */
9780
c152c796
AM
9781static bfd_boolean
9782elf_section_ignore_discarded_relocs (asection *sec)
9783{
9784 const struct elf_backend_data *bed;
9785
cdd3575c
AM
9786 switch (sec->sec_info_type)
9787 {
dbaa2011
AM
9788 case SEC_INFO_TYPE_STABS:
9789 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9790 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9791 return TRUE;
9792 default:
9793 break;
9794 }
c152c796
AM
9795
9796 bed = get_elf_backend_data (sec->owner);
9797 if (bed->elf_backend_ignore_discarded_relocs != NULL
9798 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9799 return TRUE;
9800
9801 return FALSE;
9802}
9803
9e66c942
AM
9804/* Return a mask saying how ld should treat relocations in SEC against
9805 symbols defined in discarded sections. If this function returns
9806 COMPLAIN set, ld will issue a warning message. If this function
9807 returns PRETEND set, and the discarded section was link-once and the
9808 same size as the kept link-once section, ld will pretend that the
9809 symbol was actually defined in the kept section. Otherwise ld will
9810 zero the reloc (at least that is the intent, but some cooperation by
9811 the target dependent code is needed, particularly for REL targets). */
9812
8a696751
AM
9813unsigned int
9814_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9815{
9e66c942 9816 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9817 return PRETEND;
cdd3575c
AM
9818
9819 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9820 return 0;
cdd3575c
AM
9821
9822 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9823 return 0;
cdd3575c 9824
9e66c942 9825 return COMPLAIN | PRETEND;
cdd3575c
AM
9826}
9827
3d7f7666
L
9828/* Find a match between a section and a member of a section group. */
9829
9830static asection *
c0f00686
L
9831match_group_member (asection *sec, asection *group,
9832 struct bfd_link_info *info)
3d7f7666
L
9833{
9834 asection *first = elf_next_in_group (group);
9835 asection *s = first;
9836
9837 while (s != NULL)
9838 {
c0f00686 9839 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9840 return s;
9841
83180ade 9842 s = elf_next_in_group (s);
3d7f7666
L
9843 if (s == first)
9844 break;
9845 }
9846
9847 return NULL;
9848}
9849
01b3c8ab 9850/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9851 to replace it. Return the replacement if it is OK. Otherwise return
9852 NULL. */
01b3c8ab
L
9853
9854asection *
c0f00686 9855_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9856{
9857 asection *kept;
9858
9859 kept = sec->kept_section;
9860 if (kept != NULL)
9861 {
c2370991 9862 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9863 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9864 if (kept != NULL
9865 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9866 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9867 kept = NULL;
c2370991 9868 sec->kept_section = kept;
01b3c8ab
L
9869 }
9870 return kept;
9871}
9872
c152c796
AM
9873/* Link an input file into the linker output file. This function
9874 handles all the sections and relocations of the input file at once.
9875 This is so that we only have to read the local symbols once, and
9876 don't have to keep them in memory. */
9877
9878static bfd_boolean
8b127cbc 9879elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9880{
ece5ef60 9881 int (*relocate_section)
c152c796
AM
9882 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9883 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9884 bfd *output_bfd;
9885 Elf_Internal_Shdr *symtab_hdr;
9886 size_t locsymcount;
9887 size_t extsymoff;
9888 Elf_Internal_Sym *isymbuf;
9889 Elf_Internal_Sym *isym;
9890 Elf_Internal_Sym *isymend;
9891 long *pindex;
9892 asection **ppsection;
9893 asection *o;
9894 const struct elf_backend_data *bed;
c152c796 9895 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9896 bfd_size_type address_size;
9897 bfd_vma r_type_mask;
9898 int r_sym_shift;
ffbc01cc 9899 bfd_boolean have_file_sym = FALSE;
c152c796 9900
8b127cbc 9901 output_bfd = flinfo->output_bfd;
c152c796
AM
9902 bed = get_elf_backend_data (output_bfd);
9903 relocate_section = bed->elf_backend_relocate_section;
9904
9905 /* If this is a dynamic object, we don't want to do anything here:
9906 we don't want the local symbols, and we don't want the section
9907 contents. */
9908 if ((input_bfd->flags & DYNAMIC) != 0)
9909 return TRUE;
9910
c152c796
AM
9911 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9912 if (elf_bad_symtab (input_bfd))
9913 {
9914 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9915 extsymoff = 0;
9916 }
9917 else
9918 {
9919 locsymcount = symtab_hdr->sh_info;
9920 extsymoff = symtab_hdr->sh_info;
9921 }
9922
9923 /* Read the local symbols. */
9924 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9925 if (isymbuf == NULL && locsymcount != 0)
9926 {
9927 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9928 flinfo->internal_syms,
9929 flinfo->external_syms,
9930 flinfo->locsym_shndx);
c152c796
AM
9931 if (isymbuf == NULL)
9932 return FALSE;
9933 }
9934
9935 /* Find local symbol sections and adjust values of symbols in
9936 SEC_MERGE sections. Write out those local symbols we know are
9937 going into the output file. */
9938 isymend = isymbuf + locsymcount;
8b127cbc 9939 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9940 isym < isymend;
9941 isym++, pindex++, ppsection++)
9942 {
9943 asection *isec;
9944 const char *name;
9945 Elf_Internal_Sym osym;
6e0b88f1
AM
9946 long indx;
9947 int ret;
c152c796
AM
9948
9949 *pindex = -1;
9950
9951 if (elf_bad_symtab (input_bfd))
9952 {
9953 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9954 {
9955 *ppsection = NULL;
9956 continue;
9957 }
9958 }
9959
9960 if (isym->st_shndx == SHN_UNDEF)
9961 isec = bfd_und_section_ptr;
c152c796
AM
9962 else if (isym->st_shndx == SHN_ABS)
9963 isec = bfd_abs_section_ptr;
9964 else if (isym->st_shndx == SHN_COMMON)
9965 isec = bfd_com_section_ptr;
9966 else
9967 {
cb33740c
AM
9968 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9969 if (isec == NULL)
9970 {
9971 /* Don't attempt to output symbols with st_shnx in the
9972 reserved range other than SHN_ABS and SHN_COMMON. */
9973 *ppsection = NULL;
9974 continue;
9975 }
dbaa2011 9976 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9977 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9978 isym->st_value =
9979 _bfd_merged_section_offset (output_bfd, &isec,
9980 elf_section_data (isec)->sec_info,
9981 isym->st_value);
c152c796
AM
9982 }
9983
9984 *ppsection = isec;
9985
d983c8c5
AM
9986 /* Don't output the first, undefined, symbol. In fact, don't
9987 output any undefined local symbol. */
9988 if (isec == bfd_und_section_ptr)
c152c796
AM
9989 continue;
9990
9991 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9992 {
9993 /* We never output section symbols. Instead, we use the
9994 section symbol of the corresponding section in the output
9995 file. */
9996 continue;
9997 }
9998
9999 /* If we are stripping all symbols, we don't want to output this
10000 one. */
8b127cbc 10001 if (flinfo->info->strip == strip_all)
c152c796
AM
10002 continue;
10003
10004 /* If we are discarding all local symbols, we don't want to
10005 output this one. If we are generating a relocatable output
10006 file, then some of the local symbols may be required by
10007 relocs; we output them below as we discover that they are
10008 needed. */
8b127cbc 10009 if (flinfo->info->discard == discard_all)
c152c796
AM
10010 continue;
10011
10012 /* If this symbol is defined in a section which we are
f02571c5
AM
10013 discarding, we don't need to keep it. */
10014 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10015 && isym->st_shndx < SHN_LORESERVE
10016 && bfd_section_removed_from_list (output_bfd,
10017 isec->output_section))
e75a280b
L
10018 continue;
10019
c152c796
AM
10020 /* Get the name of the symbol. */
10021 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10022 isym->st_name);
10023 if (name == NULL)
10024 return FALSE;
10025
10026 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10027 if ((flinfo->info->strip == strip_some
10028 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10029 == NULL))
8b127cbc 10030 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10031 && (isec->flags & SEC_MERGE)
10032 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10033 || flinfo->info->discard == discard_l)
c152c796
AM
10034 && bfd_is_local_label_name (input_bfd, name)))
10035 continue;
10036
ffbc01cc
AM
10037 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10038 {
ce875075
AM
10039 if (input_bfd->lto_output)
10040 /* -flto puts a temp file name here. This means builds
10041 are not reproducible. Discard the symbol. */
10042 continue;
ffbc01cc
AM
10043 have_file_sym = TRUE;
10044 flinfo->filesym_count += 1;
10045 }
10046 if (!have_file_sym)
10047 {
10048 /* In the absence of debug info, bfd_find_nearest_line uses
10049 FILE symbols to determine the source file for local
10050 function symbols. Provide a FILE symbol here if input
10051 files lack such, so that their symbols won't be
10052 associated with a previous input file. It's not the
10053 source file, but the best we can do. */
10054 have_file_sym = TRUE;
10055 flinfo->filesym_count += 1;
10056 memset (&osym, 0, sizeof (osym));
10057 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10058 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10059 if (!elf_link_output_symstrtab (flinfo,
10060 (input_bfd->lto_output ? NULL
10061 : input_bfd->filename),
10062 &osym, bfd_abs_section_ptr,
10063 NULL))
ffbc01cc
AM
10064 return FALSE;
10065 }
10066
c152c796
AM
10067 osym = *isym;
10068
10069 /* Adjust the section index for the output file. */
10070 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10071 isec->output_section);
10072 if (osym.st_shndx == SHN_BAD)
10073 return FALSE;
10074
c152c796
AM
10075 /* ELF symbols in relocatable files are section relative, but
10076 in executable files they are virtual addresses. Note that
10077 this code assumes that all ELF sections have an associated
10078 BFD section with a reasonable value for output_offset; below
10079 we assume that they also have a reasonable value for
10080 output_section. Any special sections must be set up to meet
10081 these requirements. */
10082 osym.st_value += isec->output_offset;
0e1862bb 10083 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10084 {
10085 osym.st_value += isec->output_section->vma;
10086 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10087 {
10088 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10089 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10090 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10091 }
10092 }
10093
6e0b88f1 10094 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10095 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10096 if (ret == 0)
c152c796 10097 return FALSE;
6e0b88f1
AM
10098 else if (ret == 1)
10099 *pindex = indx;
c152c796
AM
10100 }
10101
310fd250
L
10102 if (bed->s->arch_size == 32)
10103 {
10104 r_type_mask = 0xff;
10105 r_sym_shift = 8;
10106 address_size = 4;
10107 }
10108 else
10109 {
10110 r_type_mask = 0xffffffff;
10111 r_sym_shift = 32;
10112 address_size = 8;
10113 }
10114
c152c796
AM
10115 /* Relocate the contents of each section. */
10116 sym_hashes = elf_sym_hashes (input_bfd);
10117 for (o = input_bfd->sections; o != NULL; o = o->next)
10118 {
10119 bfd_byte *contents;
10120
10121 if (! o->linker_mark)
10122 {
10123 /* This section was omitted from the link. */
10124 continue;
10125 }
10126
0e1862bb 10127 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10128 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10129 {
10130 /* Deal with the group signature symbol. */
10131 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10132 unsigned long symndx = sec_data->this_hdr.sh_info;
10133 asection *osec = o->output_section;
10134
10135 if (symndx >= locsymcount
10136 || (elf_bad_symtab (input_bfd)
8b127cbc 10137 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10138 {
10139 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10140 while (h->root.type == bfd_link_hash_indirect
10141 || h->root.type == bfd_link_hash_warning)
10142 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10143 /* Arrange for symbol to be output. */
10144 h->indx = -2;
10145 elf_section_data (osec)->this_hdr.sh_info = -2;
10146 }
10147 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10148 {
10149 /* We'll use the output section target_index. */
8b127cbc 10150 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10151 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10152 }
10153 else
10154 {
8b127cbc 10155 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10156 {
10157 /* Otherwise output the local symbol now. */
10158 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10159 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10160 const char *name;
6e0b88f1
AM
10161 long indx;
10162 int ret;
bcacc0f5
AM
10163
10164 name = bfd_elf_string_from_elf_section (input_bfd,
10165 symtab_hdr->sh_link,
10166 sym.st_name);
10167 if (name == NULL)
10168 return FALSE;
10169
10170 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10171 sec);
10172 if (sym.st_shndx == SHN_BAD)
10173 return FALSE;
10174
10175 sym.st_value += o->output_offset;
10176
6e0b88f1 10177 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10178 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10179 NULL);
6e0b88f1 10180 if (ret == 0)
bcacc0f5 10181 return FALSE;
6e0b88f1 10182 else if (ret == 1)
8b127cbc 10183 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10184 else
10185 abort ();
bcacc0f5
AM
10186 }
10187 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10188 = flinfo->indices[symndx];
bcacc0f5
AM
10189 }
10190 }
10191
c152c796 10192 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10193 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10194 continue;
10195
10196 if ((o->flags & SEC_LINKER_CREATED) != 0)
10197 {
10198 /* Section was created by _bfd_elf_link_create_dynamic_sections
10199 or somesuch. */
10200 continue;
10201 }
10202
10203 /* Get the contents of the section. They have been cached by a
10204 relaxation routine. Note that o is a section in an input
10205 file, so the contents field will not have been set by any of
10206 the routines which work on output files. */
10207 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10208 {
10209 contents = elf_section_data (o)->this_hdr.contents;
10210 if (bed->caches_rawsize
10211 && o->rawsize != 0
10212 && o->rawsize < o->size)
10213 {
10214 memcpy (flinfo->contents, contents, o->rawsize);
10215 contents = flinfo->contents;
10216 }
10217 }
c152c796
AM
10218 else
10219 {
8b127cbc 10220 contents = flinfo->contents;
4a114e3e 10221 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10222 return FALSE;
10223 }
10224
10225 if ((o->flags & SEC_RELOC) != 0)
10226 {
10227 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10228 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10229 int action_discarded;
ece5ef60 10230 int ret;
c152c796
AM
10231
10232 /* Get the swapped relocs. */
10233 internal_relocs
8b127cbc
AM
10234 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10235 flinfo->internal_relocs, FALSE);
c152c796
AM
10236 if (internal_relocs == NULL
10237 && o->reloc_count > 0)
10238 return FALSE;
10239
310fd250
L
10240 /* We need to reverse-copy input .ctors/.dtors sections if
10241 they are placed in .init_array/.finit_array for output. */
10242 if (o->size > address_size
10243 && ((strncmp (o->name, ".ctors", 6) == 0
10244 && strcmp (o->output_section->name,
10245 ".init_array") == 0)
10246 || (strncmp (o->name, ".dtors", 6) == 0
10247 && strcmp (o->output_section->name,
10248 ".fini_array") == 0))
10249 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10250 {
310fd250
L
10251 if (o->size != o->reloc_count * address_size)
10252 {
4eca0228 10253 _bfd_error_handler
695344c0 10254 /* xgettext:c-format */
310fd250
L
10255 (_("error: %B: size of section %A is not "
10256 "multiple of address size"),
10257 input_bfd, o);
10258 bfd_set_error (bfd_error_on_input);
10259 return FALSE;
10260 }
10261 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10262 }
10263
0f02bbd9 10264 action_discarded = -1;
c152c796 10265 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10266 action_discarded = (*bed->action_discarded) (o);
10267
10268 /* Run through the relocs evaluating complex reloc symbols and
10269 looking for relocs against symbols from discarded sections
10270 or section symbols from removed link-once sections.
10271 Complain about relocs against discarded sections. Zero
10272 relocs against removed link-once sections. */
10273
10274 rel = internal_relocs;
10275 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10276 for ( ; rel < relend; rel++)
c152c796 10277 {
0f02bbd9
AM
10278 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10279 unsigned int s_type;
10280 asection **ps, *sec;
10281 struct elf_link_hash_entry *h = NULL;
10282 const char *sym_name;
c152c796 10283
0f02bbd9
AM
10284 if (r_symndx == STN_UNDEF)
10285 continue;
c152c796 10286
0f02bbd9
AM
10287 if (r_symndx >= locsymcount
10288 || (elf_bad_symtab (input_bfd)
8b127cbc 10289 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10290 {
10291 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10292
0f02bbd9
AM
10293 /* Badly formatted input files can contain relocs that
10294 reference non-existant symbols. Check here so that
10295 we do not seg fault. */
10296 if (h == NULL)
c152c796 10297 {
0f02bbd9 10298 char buffer [32];
dce669a1 10299
0f02bbd9 10300 sprintf_vma (buffer, rel->r_info);
4eca0228 10301 _bfd_error_handler
695344c0 10302 /* xgettext:c-format */
0f02bbd9
AM
10303 (_("error: %B contains a reloc (0x%s) for section %A "
10304 "that references a non-existent global symbol"),
10305 input_bfd, o, buffer);
10306 bfd_set_error (bfd_error_bad_value);
10307 return FALSE;
10308 }
3b36f7e6 10309
0f02bbd9
AM
10310 while (h->root.type == bfd_link_hash_indirect
10311 || h->root.type == bfd_link_hash_warning)
10312 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10313
0f02bbd9 10314 s_type = h->type;
cdd3575c 10315
9e2dec47 10316 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10317 mark the symbol as undefined. Note that the
10318 linker may attach linker created dynamic sections
10319 to the plugin bfd. Symbols defined in linker
10320 created sections are not plugin symbols. */
9e2dec47
L
10321 if (h->root.non_ir_ref
10322 && (h->root.type == bfd_link_hash_defined
10323 || h->root.type == bfd_link_hash_defweak)
10324 && (h->root.u.def.section->flags
10325 & SEC_LINKER_CREATED) == 0
10326 && h->root.u.def.section->owner != NULL
10327 && (h->root.u.def.section->owner->flags
10328 & BFD_PLUGIN) != 0)
10329 {
10330 h->root.type = bfd_link_hash_undefined;
10331 h->root.u.undef.abfd = h->root.u.def.section->owner;
10332 }
10333
0f02bbd9
AM
10334 ps = NULL;
10335 if (h->root.type == bfd_link_hash_defined
10336 || h->root.type == bfd_link_hash_defweak)
10337 ps = &h->root.u.def.section;
10338
10339 sym_name = h->root.root.string;
10340 }
10341 else
10342 {
10343 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10344
10345 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10346 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10347 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10348 sym, *ps);
10349 }
c152c796 10350
c301e700 10351 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10352 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10353 {
10354 bfd_vma val;
10355 bfd_vma dot = (rel->r_offset
10356 + o->output_offset + o->output_section->vma);
10357#ifdef DEBUG
10358 printf ("Encountered a complex symbol!");
10359 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10360 input_bfd->filename, o->name,
10361 (long) (rel - internal_relocs));
0f02bbd9
AM
10362 printf (" symbol: idx %8.8lx, name %s\n",
10363 r_symndx, sym_name);
10364 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10365 (unsigned long) rel->r_info,
10366 (unsigned long) rel->r_offset);
10367#endif
8b127cbc 10368 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10369 isymbuf, locsymcount, s_type == STT_SRELC))
10370 return FALSE;
10371
10372 /* Symbol evaluated OK. Update to absolute value. */
10373 set_symbol_value (input_bfd, isymbuf, locsymcount,
10374 r_symndx, val);
10375 continue;
10376 }
10377
10378 if (action_discarded != -1 && ps != NULL)
10379 {
cdd3575c
AM
10380 /* Complain if the definition comes from a
10381 discarded section. */
dbaa2011 10382 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10383 {
cf35638d 10384 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10385 if (action_discarded & COMPLAIN)
8b127cbc 10386 (*flinfo->info->callbacks->einfo)
695344c0 10387 /* xgettext:c-format */
e1fffbe6 10388 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10389 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10390 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10391
87e5235d 10392 /* Try to do the best we can to support buggy old
e0ae6d6f 10393 versions of gcc. Pretend that the symbol is
87e5235d
AM
10394 really defined in the kept linkonce section.
10395 FIXME: This is quite broken. Modifying the
10396 symbol here means we will be changing all later
e0ae6d6f 10397 uses of the symbol, not just in this section. */
0f02bbd9 10398 if (action_discarded & PRETEND)
87e5235d 10399 {
01b3c8ab
L
10400 asection *kept;
10401
c0f00686 10402 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10403 flinfo->info);
01b3c8ab 10404 if (kept != NULL)
87e5235d
AM
10405 {
10406 *ps = kept;
10407 continue;
10408 }
10409 }
c152c796
AM
10410 }
10411 }
10412 }
10413
10414 /* Relocate the section by invoking a back end routine.
10415
10416 The back end routine is responsible for adjusting the
10417 section contents as necessary, and (if using Rela relocs
10418 and generating a relocatable output file) adjusting the
10419 reloc addend as necessary.
10420
10421 The back end routine does not have to worry about setting
10422 the reloc address or the reloc symbol index.
10423
10424 The back end routine is given a pointer to the swapped in
10425 internal symbols, and can access the hash table entries
10426 for the external symbols via elf_sym_hashes (input_bfd).
10427
10428 When generating relocatable output, the back end routine
10429 must handle STB_LOCAL/STT_SECTION symbols specially. The
10430 output symbol is going to be a section symbol
10431 corresponding to the output section, which will require
10432 the addend to be adjusted. */
10433
8b127cbc 10434 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10435 input_bfd, o, contents,
10436 internal_relocs,
10437 isymbuf,
8b127cbc 10438 flinfo->sections);
ece5ef60 10439 if (!ret)
c152c796
AM
10440 return FALSE;
10441
ece5ef60 10442 if (ret == 2
0e1862bb 10443 || bfd_link_relocatable (flinfo->info)
8b127cbc 10444 || flinfo->info->emitrelocations)
c152c796
AM
10445 {
10446 Elf_Internal_Rela *irela;
d4730f92 10447 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10448 bfd_vma last_offset;
10449 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10450 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10451 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10452 unsigned int next_erel;
c152c796 10453 bfd_boolean rela_normal;
d4730f92 10454 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10455
d4730f92
BS
10456 esdi = elf_section_data (o);
10457 esdo = elf_section_data (o->output_section);
10458 rela_normal = FALSE;
c152c796
AM
10459
10460 /* Adjust the reloc addresses and symbol indices. */
10461
10462 irela = internal_relocs;
10463 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10464 rel_hash = esdo->rel.hashes + esdo->rel.count;
10465 /* We start processing the REL relocs, if any. When we reach
10466 IRELAMID in the loop, we switch to the RELA relocs. */
10467 irelamid = irela;
10468 if (esdi->rel.hdr != NULL)
10469 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10470 * bed->s->int_rels_per_ext_rel);
eac338cf 10471 rel_hash_list = rel_hash;
d4730f92 10472 rela_hash_list = NULL;
c152c796 10473 last_offset = o->output_offset;
0e1862bb 10474 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10475 last_offset += o->output_section->vma;
10476 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10477 {
10478 unsigned long r_symndx;
10479 asection *sec;
10480 Elf_Internal_Sym sym;
10481
10482 if (next_erel == bed->s->int_rels_per_ext_rel)
10483 {
10484 rel_hash++;
10485 next_erel = 0;
10486 }
10487
d4730f92
BS
10488 if (irela == irelamid)
10489 {
10490 rel_hash = esdo->rela.hashes + esdo->rela.count;
10491 rela_hash_list = rel_hash;
10492 rela_normal = bed->rela_normal;
10493 }
10494
c152c796 10495 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10496 flinfo->info, o,
c152c796
AM
10497 irela->r_offset);
10498 if (irela->r_offset >= (bfd_vma) -2)
10499 {
10500 /* This is a reloc for a deleted entry or somesuch.
10501 Turn it into an R_*_NONE reloc, at the same
10502 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10503 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10504 being ordered. */
10505 irela->r_offset = last_offset;
10506 irela->r_info = 0;
10507 irela->r_addend = 0;
10508 continue;
10509 }
10510
10511 irela->r_offset += o->output_offset;
10512
10513 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10514 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10515 irela->r_offset += o->output_section->vma;
10516
10517 last_offset = irela->r_offset;
10518
10519 r_symndx = irela->r_info >> r_sym_shift;
10520 if (r_symndx == STN_UNDEF)
10521 continue;
10522
10523 if (r_symndx >= locsymcount
10524 || (elf_bad_symtab (input_bfd)
8b127cbc 10525 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10526 {
10527 struct elf_link_hash_entry *rh;
10528 unsigned long indx;
10529
10530 /* This is a reloc against a global symbol. We
10531 have not yet output all the local symbols, so
10532 we do not know the symbol index of any global
10533 symbol. We set the rel_hash entry for this
10534 reloc to point to the global hash table entry
10535 for this symbol. The symbol index is then
ee75fd95 10536 set at the end of bfd_elf_final_link. */
c152c796
AM
10537 indx = r_symndx - extsymoff;
10538 rh = elf_sym_hashes (input_bfd)[indx];
10539 while (rh->root.type == bfd_link_hash_indirect
10540 || rh->root.type == bfd_link_hash_warning)
10541 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10542
10543 /* Setting the index to -2 tells
10544 elf_link_output_extsym that this symbol is
10545 used by a reloc. */
10546 BFD_ASSERT (rh->indx < 0);
10547 rh->indx = -2;
10548
10549 *rel_hash = rh;
10550
10551 continue;
10552 }
10553
10554 /* This is a reloc against a local symbol. */
10555
10556 *rel_hash = NULL;
10557 sym = isymbuf[r_symndx];
8b127cbc 10558 sec = flinfo->sections[r_symndx];
c152c796
AM
10559 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10560 {
10561 /* I suppose the backend ought to fill in the
10562 section of any STT_SECTION symbol against a
6a8d1586 10563 processor specific section. */
cf35638d 10564 r_symndx = STN_UNDEF;
6a8d1586
AM
10565 if (bfd_is_abs_section (sec))
10566 ;
c152c796
AM
10567 else if (sec == NULL || sec->owner == NULL)
10568 {
10569 bfd_set_error (bfd_error_bad_value);
10570 return FALSE;
10571 }
10572 else
10573 {
6a8d1586
AM
10574 asection *osec = sec->output_section;
10575
10576 /* If we have discarded a section, the output
10577 section will be the absolute section. In
ab96bf03
AM
10578 case of discarded SEC_MERGE sections, use
10579 the kept section. relocate_section should
10580 have already handled discarded linkonce
10581 sections. */
6a8d1586
AM
10582 if (bfd_is_abs_section (osec)
10583 && sec->kept_section != NULL
10584 && sec->kept_section->output_section != NULL)
10585 {
10586 osec = sec->kept_section->output_section;
10587 irela->r_addend -= osec->vma;
10588 }
10589
10590 if (!bfd_is_abs_section (osec))
10591 {
10592 r_symndx = osec->target_index;
cf35638d 10593 if (r_symndx == STN_UNDEF)
74541ad4 10594 {
051d833a
AM
10595 irela->r_addend += osec->vma;
10596 osec = _bfd_nearby_section (output_bfd, osec,
10597 osec->vma);
10598 irela->r_addend -= osec->vma;
10599 r_symndx = osec->target_index;
74541ad4 10600 }
6a8d1586 10601 }
c152c796
AM
10602 }
10603
10604 /* Adjust the addend according to where the
10605 section winds up in the output section. */
10606 if (rela_normal)
10607 irela->r_addend += sec->output_offset;
10608 }
10609 else
10610 {
8b127cbc 10611 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10612 {
10613 unsigned long shlink;
10614 const char *name;
10615 asection *osec;
6e0b88f1 10616 long indx;
c152c796 10617
8b127cbc 10618 if (flinfo->info->strip == strip_all)
c152c796
AM
10619 {
10620 /* You can't do ld -r -s. */
10621 bfd_set_error (bfd_error_invalid_operation);
10622 return FALSE;
10623 }
10624
10625 /* This symbol was skipped earlier, but
10626 since it is needed by a reloc, we
10627 must output it now. */
10628 shlink = symtab_hdr->sh_link;
10629 name = (bfd_elf_string_from_elf_section
10630 (input_bfd, shlink, sym.st_name));
10631 if (name == NULL)
10632 return FALSE;
10633
10634 osec = sec->output_section;
10635 sym.st_shndx =
10636 _bfd_elf_section_from_bfd_section (output_bfd,
10637 osec);
10638 if (sym.st_shndx == SHN_BAD)
10639 return FALSE;
10640
10641 sym.st_value += sec->output_offset;
0e1862bb 10642 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10643 {
10644 sym.st_value += osec->vma;
10645 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10646 {
10647 /* STT_TLS symbols are relative to PT_TLS
10648 segment base. */
8b127cbc 10649 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10650 ->tls_sec != NULL);
8b127cbc 10651 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10652 ->tls_sec->vma);
10653 }
10654 }
10655
6e0b88f1 10656 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10657 ret = elf_link_output_symstrtab (flinfo, name,
10658 &sym, sec,
10659 NULL);
6e0b88f1 10660 if (ret == 0)
c152c796 10661 return FALSE;
6e0b88f1 10662 else if (ret == 1)
8b127cbc 10663 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10664 else
10665 abort ();
c152c796
AM
10666 }
10667
8b127cbc 10668 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10669 }
10670
10671 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10672 | (irela->r_info & r_type_mask));
10673 }
10674
10675 /* Swap out the relocs. */
d4730f92
BS
10676 input_rel_hdr = esdi->rel.hdr;
10677 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10678 {
d4730f92
BS
10679 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10680 input_rel_hdr,
10681 internal_relocs,
10682 rel_hash_list))
10683 return FALSE;
c152c796
AM
10684 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10685 * bed->s->int_rels_per_ext_rel);
eac338cf 10686 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10687 }
10688
10689 input_rela_hdr = esdi->rela.hdr;
10690 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10691 {
eac338cf 10692 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10693 input_rela_hdr,
eac338cf 10694 internal_relocs,
d4730f92 10695 rela_hash_list))
c152c796
AM
10696 return FALSE;
10697 }
10698 }
10699 }
10700
10701 /* Write out the modified section contents. */
10702 if (bed->elf_backend_write_section
8b127cbc 10703 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10704 contents))
c152c796
AM
10705 {
10706 /* Section written out. */
10707 }
10708 else switch (o->sec_info_type)
10709 {
dbaa2011 10710 case SEC_INFO_TYPE_STABS:
c152c796
AM
10711 if (! (_bfd_write_section_stabs
10712 (output_bfd,
8b127cbc 10713 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10714 o, &elf_section_data (o)->sec_info, contents)))
10715 return FALSE;
10716 break;
dbaa2011 10717 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10718 if (! _bfd_write_merged_section (output_bfd, o,
10719 elf_section_data (o)->sec_info))
10720 return FALSE;
10721 break;
dbaa2011 10722 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10723 {
8b127cbc 10724 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10725 o, contents))
10726 return FALSE;
10727 }
10728 break;
2f0c68f2
CM
10729 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10730 {
10731 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10732 flinfo->info,
10733 o, contents))
10734 return FALSE;
10735 }
10736 break;
c152c796
AM
10737 default:
10738 {
310fd250
L
10739 if (! (o->flags & SEC_EXCLUDE))
10740 {
10741 file_ptr offset = (file_ptr) o->output_offset;
10742 bfd_size_type todo = o->size;
37b01f6a
DG
10743
10744 offset *= bfd_octets_per_byte (output_bfd);
10745
310fd250
L
10746 if ((o->flags & SEC_ELF_REVERSE_COPY))
10747 {
10748 /* Reverse-copy input section to output. */
10749 do
10750 {
10751 todo -= address_size;
10752 if (! bfd_set_section_contents (output_bfd,
10753 o->output_section,
10754 contents + todo,
10755 offset,
10756 address_size))
10757 return FALSE;
10758 if (todo == 0)
10759 break;
10760 offset += address_size;
10761 }
10762 while (1);
10763 }
10764 else if (! bfd_set_section_contents (output_bfd,
10765 o->output_section,
10766 contents,
10767 offset, todo))
10768 return FALSE;
10769 }
c152c796
AM
10770 }
10771 break;
10772 }
10773 }
10774
10775 return TRUE;
10776}
10777
10778/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10779 requested by the linker, and does not come from any input file. This
c152c796
AM
10780 is used to build constructor and destructor tables when linking
10781 with -Ur. */
10782
10783static bfd_boolean
10784elf_reloc_link_order (bfd *output_bfd,
10785 struct bfd_link_info *info,
10786 asection *output_section,
10787 struct bfd_link_order *link_order)
10788{
10789 reloc_howto_type *howto;
10790 long indx;
10791 bfd_vma offset;
10792 bfd_vma addend;
d4730f92 10793 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10794 struct elf_link_hash_entry **rel_hash_ptr;
10795 Elf_Internal_Shdr *rel_hdr;
10796 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10797 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10798 bfd_byte *erel;
10799 unsigned int i;
d4730f92 10800 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10801
10802 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10803 if (howto == NULL)
10804 {
10805 bfd_set_error (bfd_error_bad_value);
10806 return FALSE;
10807 }
10808
10809 addend = link_order->u.reloc.p->addend;
10810
d4730f92
BS
10811 if (esdo->rel.hdr)
10812 reldata = &esdo->rel;
10813 else if (esdo->rela.hdr)
10814 reldata = &esdo->rela;
10815 else
10816 {
10817 reldata = NULL;
10818 BFD_ASSERT (0);
10819 }
10820
c152c796 10821 /* Figure out the symbol index. */
d4730f92 10822 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10823 if (link_order->type == bfd_section_reloc_link_order)
10824 {
10825 indx = link_order->u.reloc.p->u.section->target_index;
10826 BFD_ASSERT (indx != 0);
10827 *rel_hash_ptr = NULL;
10828 }
10829 else
10830 {
10831 struct elf_link_hash_entry *h;
10832
10833 /* Treat a reloc against a defined symbol as though it were
10834 actually against the section. */
10835 h = ((struct elf_link_hash_entry *)
10836 bfd_wrapped_link_hash_lookup (output_bfd, info,
10837 link_order->u.reloc.p->u.name,
10838 FALSE, FALSE, TRUE));
10839 if (h != NULL
10840 && (h->root.type == bfd_link_hash_defined
10841 || h->root.type == bfd_link_hash_defweak))
10842 {
10843 asection *section;
10844
10845 section = h->root.u.def.section;
10846 indx = section->output_section->target_index;
10847 *rel_hash_ptr = NULL;
10848 /* It seems that we ought to add the symbol value to the
10849 addend here, but in practice it has already been added
10850 because it was passed to constructor_callback. */
10851 addend += section->output_section->vma + section->output_offset;
10852 }
10853 else if (h != NULL)
10854 {
10855 /* Setting the index to -2 tells elf_link_output_extsym that
10856 this symbol is used by a reloc. */
10857 h->indx = -2;
10858 *rel_hash_ptr = h;
10859 indx = 0;
10860 }
10861 else
10862 {
1a72702b
AM
10863 (*info->callbacks->unattached_reloc)
10864 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10865 indx = 0;
10866 }
10867 }
10868
10869 /* If this is an inplace reloc, we must write the addend into the
10870 object file. */
10871 if (howto->partial_inplace && addend != 0)
10872 {
10873 bfd_size_type size;
10874 bfd_reloc_status_type rstat;
10875 bfd_byte *buf;
10876 bfd_boolean ok;
10877 const char *sym_name;
10878
a50b1753
NC
10879 size = (bfd_size_type) bfd_get_reloc_size (howto);
10880 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10881 if (buf == NULL && size != 0)
c152c796
AM
10882 return FALSE;
10883 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10884 switch (rstat)
10885 {
10886 case bfd_reloc_ok:
10887 break;
10888
10889 default:
10890 case bfd_reloc_outofrange:
10891 abort ();
10892
10893 case bfd_reloc_overflow:
10894 if (link_order->type == bfd_section_reloc_link_order)
10895 sym_name = bfd_section_name (output_bfd,
10896 link_order->u.reloc.p->u.section);
10897 else
10898 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10899 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10900 howto->name, addend, NULL, NULL,
10901 (bfd_vma) 0);
c152c796
AM
10902 break;
10903 }
37b01f6a 10904
c152c796 10905 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10906 link_order->offset
10907 * bfd_octets_per_byte (output_bfd),
10908 size);
c152c796
AM
10909 free (buf);
10910 if (! ok)
10911 return FALSE;
10912 }
10913
10914 /* The address of a reloc is relative to the section in a
10915 relocatable file, and is a virtual address in an executable
10916 file. */
10917 offset = link_order->offset;
0e1862bb 10918 if (! bfd_link_relocatable (info))
c152c796
AM
10919 offset += output_section->vma;
10920
10921 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10922 {
10923 irel[i].r_offset = offset;
10924 irel[i].r_info = 0;
10925 irel[i].r_addend = 0;
10926 }
10927 if (bed->s->arch_size == 32)
10928 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10929 else
10930 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10931
d4730f92 10932 rel_hdr = reldata->hdr;
c152c796
AM
10933 erel = rel_hdr->contents;
10934 if (rel_hdr->sh_type == SHT_REL)
10935 {
d4730f92 10936 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10937 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10938 }
10939 else
10940 {
10941 irel[0].r_addend = addend;
d4730f92 10942 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10943 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10944 }
10945
d4730f92 10946 ++reldata->count;
c152c796
AM
10947
10948 return TRUE;
10949}
10950
0b52efa6
PB
10951
10952/* Get the output vma of the section pointed to by the sh_link field. */
10953
10954static bfd_vma
10955elf_get_linked_section_vma (struct bfd_link_order *p)
10956{
10957 Elf_Internal_Shdr **elf_shdrp;
10958 asection *s;
10959 int elfsec;
10960
10961 s = p->u.indirect.section;
10962 elf_shdrp = elf_elfsections (s->owner);
10963 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10964 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10965 /* PR 290:
10966 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10967 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10968 sh_info fields. Hence we could get the situation
10969 where elfsec is 0. */
10970 if (elfsec == 0)
10971 {
10972 const struct elf_backend_data *bed
10973 = get_elf_backend_data (s->owner);
10974 if (bed->link_order_error_handler)
d003868e 10975 bed->link_order_error_handler
695344c0 10976 /* xgettext:c-format */
d003868e 10977 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10978 return 0;
10979 }
10980 else
10981 {
10982 s = elf_shdrp[elfsec]->bfd_section;
10983 return s->output_section->vma + s->output_offset;
10984 }
0b52efa6
PB
10985}
10986
10987
10988/* Compare two sections based on the locations of the sections they are
10989 linked to. Used by elf_fixup_link_order. */
10990
10991static int
10992compare_link_order (const void * a, const void * b)
10993{
10994 bfd_vma apos;
10995 bfd_vma bpos;
10996
10997 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10998 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10999 if (apos < bpos)
11000 return -1;
11001 return apos > bpos;
11002}
11003
11004
11005/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11006 order as their linked sections. Returns false if this could not be done
11007 because an output section includes both ordered and unordered
11008 sections. Ideally we'd do this in the linker proper. */
11009
11010static bfd_boolean
11011elf_fixup_link_order (bfd *abfd, asection *o)
11012{
11013 int seen_linkorder;
11014 int seen_other;
11015 int n;
11016 struct bfd_link_order *p;
11017 bfd *sub;
11018 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11019 unsigned elfsec;
0b52efa6 11020 struct bfd_link_order **sections;
d33cdfe3 11021 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11022 bfd_vma offset;
3b36f7e6 11023
d33cdfe3
L
11024 other_sec = NULL;
11025 linkorder_sec = NULL;
0b52efa6
PB
11026 seen_other = 0;
11027 seen_linkorder = 0;
8423293d 11028 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11029 {
d33cdfe3 11030 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11031 {
11032 s = p->u.indirect.section;
d33cdfe3
L
11033 sub = s->owner;
11034 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11035 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11036 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11037 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11038 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11039 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11040 {
11041 seen_linkorder++;
11042 linkorder_sec = s;
11043 }
0b52efa6 11044 else
d33cdfe3
L
11045 {
11046 seen_other++;
11047 other_sec = s;
11048 }
0b52efa6
PB
11049 }
11050 else
11051 seen_other++;
d33cdfe3
L
11052
11053 if (seen_other && seen_linkorder)
11054 {
11055 if (other_sec && linkorder_sec)
4eca0228 11056 _bfd_error_handler
695344c0 11057 /* xgettext:c-format */
4eca0228
AM
11058 (_("%A has both ordered [`%A' in %B] "
11059 "and unordered [`%A' in %B] sections"),
11060 o, linkorder_sec,
11061 linkorder_sec->owner, other_sec,
11062 other_sec->owner);
d33cdfe3 11063 else
4eca0228
AM
11064 _bfd_error_handler
11065 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11066 bfd_set_error (bfd_error_bad_value);
11067 return FALSE;
11068 }
0b52efa6
PB
11069 }
11070
11071 if (!seen_linkorder)
11072 return TRUE;
11073
0b52efa6 11074 sections = (struct bfd_link_order **)
14b1c01e
AM
11075 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11076 if (sections == NULL)
11077 return FALSE;
0b52efa6 11078 seen_linkorder = 0;
3b36f7e6 11079
8423293d 11080 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11081 {
11082 sections[seen_linkorder++] = p;
11083 }
11084 /* Sort the input sections in the order of their linked section. */
11085 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11086 compare_link_order);
11087
11088 /* Change the offsets of the sections. */
11089 offset = 0;
11090 for (n = 0; n < seen_linkorder; n++)
11091 {
11092 s = sections[n]->u.indirect.section;
461686a3 11093 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11094 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11095 sections[n]->offset = offset;
11096 offset += sections[n]->size;
11097 }
11098
4dd07732 11099 free (sections);
0b52efa6
PB
11100 return TRUE;
11101}
11102
76359541
TP
11103/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11104 Returns TRUE upon success, FALSE otherwise. */
11105
11106static bfd_boolean
11107elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11108{
11109 bfd_boolean ret = FALSE;
11110 bfd *implib_bfd;
11111 const struct elf_backend_data *bed;
11112 flagword flags;
11113 enum bfd_architecture arch;
11114 unsigned int mach;
11115 asymbol **sympp = NULL;
11116 long symsize;
11117 long symcount;
11118 long src_count;
11119 elf_symbol_type *osymbuf;
11120
11121 implib_bfd = info->out_implib_bfd;
11122 bed = get_elf_backend_data (abfd);
11123
11124 if (!bfd_set_format (implib_bfd, bfd_object))
11125 return FALSE;
11126
11127 flags = bfd_get_file_flags (abfd);
11128 flags &= ~HAS_RELOC;
11129 if (!bfd_set_start_address (implib_bfd, 0)
11130 || !bfd_set_file_flags (implib_bfd, flags))
11131 return FALSE;
11132
11133 /* Copy architecture of output file to import library file. */
11134 arch = bfd_get_arch (abfd);
11135 mach = bfd_get_mach (abfd);
11136 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11137 && (abfd->target_defaulted
11138 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11139 return FALSE;
11140
11141 /* Get symbol table size. */
11142 symsize = bfd_get_symtab_upper_bound (abfd);
11143 if (symsize < 0)
11144 return FALSE;
11145
11146 /* Read in the symbol table. */
11147 sympp = (asymbol **) xmalloc (symsize);
11148 symcount = bfd_canonicalize_symtab (abfd, sympp);
11149 if (symcount < 0)
11150 goto free_sym_buf;
11151
11152 /* Allow the BFD backend to copy any private header data it
11153 understands from the output BFD to the import library BFD. */
11154 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11155 goto free_sym_buf;
11156
11157 /* Filter symbols to appear in the import library. */
11158 if (bed->elf_backend_filter_implib_symbols)
11159 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11160 symcount);
11161 else
11162 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11163 if (symcount == 0)
11164 {
5df1bc57 11165 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11166 _bfd_error_handler (_("%B: no symbol found for import library"),
11167 implib_bfd);
76359541
TP
11168 goto free_sym_buf;
11169 }
11170
11171
11172 /* Make symbols absolute. */
11173 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11174 sizeof (*osymbuf));
11175 for (src_count = 0; src_count < symcount; src_count++)
11176 {
11177 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11178 sizeof (*osymbuf));
11179 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11180 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11181 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11182 osymbuf[src_count].internal_elf_sym.st_value =
11183 osymbuf[src_count].symbol.value;
11184 sympp[src_count] = &osymbuf[src_count].symbol;
11185 }
11186
11187 bfd_set_symtab (implib_bfd, sympp, symcount);
11188
11189 /* Allow the BFD backend to copy any private data it understands
11190 from the output BFD to the import library BFD. This is done last
11191 to permit the routine to look at the filtered symbol table. */
11192 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11193 goto free_sym_buf;
11194
11195 if (!bfd_close (implib_bfd))
11196 goto free_sym_buf;
11197
11198 ret = TRUE;
11199
11200free_sym_buf:
11201 free (sympp);
11202 return ret;
11203}
11204
9f7c3e5e
AM
11205static void
11206elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11207{
11208 asection *o;
11209
11210 if (flinfo->symstrtab != NULL)
ef10c3ac 11211 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11212 if (flinfo->contents != NULL)
11213 free (flinfo->contents);
11214 if (flinfo->external_relocs != NULL)
11215 free (flinfo->external_relocs);
11216 if (flinfo->internal_relocs != NULL)
11217 free (flinfo->internal_relocs);
11218 if (flinfo->external_syms != NULL)
11219 free (flinfo->external_syms);
11220 if (flinfo->locsym_shndx != NULL)
11221 free (flinfo->locsym_shndx);
11222 if (flinfo->internal_syms != NULL)
11223 free (flinfo->internal_syms);
11224 if (flinfo->indices != NULL)
11225 free (flinfo->indices);
11226 if (flinfo->sections != NULL)
11227 free (flinfo->sections);
9f7c3e5e
AM
11228 if (flinfo->symshndxbuf != NULL)
11229 free (flinfo->symshndxbuf);
11230 for (o = obfd->sections; o != NULL; o = o->next)
11231 {
11232 struct bfd_elf_section_data *esdo = elf_section_data (o);
11233 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11234 free (esdo->rel.hashes);
11235 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11236 free (esdo->rela.hashes);
11237 }
11238}
0b52efa6 11239
c152c796
AM
11240/* Do the final step of an ELF link. */
11241
11242bfd_boolean
11243bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11244{
11245 bfd_boolean dynamic;
11246 bfd_boolean emit_relocs;
11247 bfd *dynobj;
8b127cbc 11248 struct elf_final_link_info flinfo;
91d6fa6a
NC
11249 asection *o;
11250 struct bfd_link_order *p;
11251 bfd *sub;
c152c796
AM
11252 bfd_size_type max_contents_size;
11253 bfd_size_type max_external_reloc_size;
11254 bfd_size_type max_internal_reloc_count;
11255 bfd_size_type max_sym_count;
11256 bfd_size_type max_sym_shndx_count;
c152c796
AM
11257 Elf_Internal_Sym elfsym;
11258 unsigned int i;
11259 Elf_Internal_Shdr *symtab_hdr;
11260 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11262 struct elf_outext_info eoinfo;
11263 bfd_boolean merged;
11264 size_t relativecount = 0;
11265 asection *reldyn = 0;
11266 bfd_size_type amt;
104d59d1
JM
11267 asection *attr_section = NULL;
11268 bfd_vma attr_size = 0;
11269 const char *std_attrs_section;
64f52338 11270 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11271
64f52338 11272 if (!is_elf_hash_table (htab))
c152c796
AM
11273 return FALSE;
11274
0e1862bb 11275 if (bfd_link_pic (info))
c152c796
AM
11276 abfd->flags |= DYNAMIC;
11277
64f52338
AM
11278 dynamic = htab->dynamic_sections_created;
11279 dynobj = htab->dynobj;
c152c796 11280
0e1862bb 11281 emit_relocs = (bfd_link_relocatable (info)
a4676736 11282 || info->emitrelocations);
c152c796 11283
8b127cbc
AM
11284 flinfo.info = info;
11285 flinfo.output_bfd = abfd;
ef10c3ac 11286 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11287 if (flinfo.symstrtab == NULL)
c152c796
AM
11288 return FALSE;
11289
11290 if (! dynamic)
11291 {
8b127cbc
AM
11292 flinfo.hash_sec = NULL;
11293 flinfo.symver_sec = NULL;
c152c796
AM
11294 }
11295 else
11296 {
3d4d4302 11297 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11298 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11299 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11300 /* Note that it is OK if symver_sec is NULL. */
11301 }
11302
8b127cbc
AM
11303 flinfo.contents = NULL;
11304 flinfo.external_relocs = NULL;
11305 flinfo.internal_relocs = NULL;
11306 flinfo.external_syms = NULL;
11307 flinfo.locsym_shndx = NULL;
11308 flinfo.internal_syms = NULL;
11309 flinfo.indices = NULL;
11310 flinfo.sections = NULL;
8b127cbc 11311 flinfo.symshndxbuf = NULL;
ffbc01cc 11312 flinfo.filesym_count = 0;
c152c796 11313
104d59d1
JM
11314 /* The object attributes have been merged. Remove the input
11315 sections from the link, and set the contents of the output
11316 secton. */
11317 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11318 for (o = abfd->sections; o != NULL; o = o->next)
11319 {
11320 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11321 || strcmp (o->name, ".gnu.attributes") == 0)
11322 {
11323 for (p = o->map_head.link_order; p != NULL; p = p->next)
11324 {
11325 asection *input_section;
11326
11327 if (p->type != bfd_indirect_link_order)
11328 continue;
11329 input_section = p->u.indirect.section;
11330 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11331 elf_link_input_bfd ignores this section. */
11332 input_section->flags &= ~SEC_HAS_CONTENTS;
11333 }
a0c8462f 11334
104d59d1
JM
11335 attr_size = bfd_elf_obj_attr_size (abfd);
11336 if (attr_size)
11337 {
11338 bfd_set_section_size (abfd, o, attr_size);
11339 attr_section = o;
11340 /* Skip this section later on. */
11341 o->map_head.link_order = NULL;
11342 }
11343 else
11344 o->flags |= SEC_EXCLUDE;
11345 }
11346 }
11347
c152c796
AM
11348 /* Count up the number of relocations we will output for each output
11349 section, so that we know the sizes of the reloc sections. We
11350 also figure out some maximum sizes. */
11351 max_contents_size = 0;
11352 max_external_reloc_size = 0;
11353 max_internal_reloc_count = 0;
11354 max_sym_count = 0;
11355 max_sym_shndx_count = 0;
11356 merged = FALSE;
11357 for (o = abfd->sections; o != NULL; o = o->next)
11358 {
11359 struct bfd_elf_section_data *esdo = elf_section_data (o);
11360 o->reloc_count = 0;
11361
8423293d 11362 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11363 {
11364 unsigned int reloc_count = 0;
9eaff861 11365 unsigned int additional_reloc_count = 0;
c152c796 11366 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11367
11368 if (p->type == bfd_section_reloc_link_order
11369 || p->type == bfd_symbol_reloc_link_order)
11370 reloc_count = 1;
11371 else if (p->type == bfd_indirect_link_order)
11372 {
11373 asection *sec;
11374
11375 sec = p->u.indirect.section;
c152c796
AM
11376
11377 /* Mark all sections which are to be included in the
11378 link. This will normally be every section. We need
11379 to do this so that we can identify any sections which
11380 the linker has decided to not include. */
11381 sec->linker_mark = TRUE;
11382
11383 if (sec->flags & SEC_MERGE)
11384 merged = TRUE;
11385
eea6121a
AM
11386 if (sec->rawsize > max_contents_size)
11387 max_contents_size = sec->rawsize;
11388 if (sec->size > max_contents_size)
11389 max_contents_size = sec->size;
c152c796 11390
c152c796
AM
11391 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11392 && (sec->owner->flags & DYNAMIC) == 0)
11393 {
11394 size_t sym_count;
11395
a961cdd5
AM
11396 /* We are interested in just local symbols, not all
11397 symbols. */
c152c796
AM
11398 if (elf_bad_symtab (sec->owner))
11399 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11400 / bed->s->sizeof_sym);
11401 else
11402 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11403
11404 if (sym_count > max_sym_count)
11405 max_sym_count = sym_count;
11406
11407 if (sym_count > max_sym_shndx_count
6a40cf0c 11408 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11409 max_sym_shndx_count = sym_count;
11410
a961cdd5
AM
11411 if (esdo->this_hdr.sh_type == SHT_REL
11412 || esdo->this_hdr.sh_type == SHT_RELA)
11413 /* Some backends use reloc_count in relocation sections
11414 to count particular types of relocs. Of course,
11415 reloc sections themselves can't have relocations. */
11416 ;
11417 else if (emit_relocs)
11418 {
11419 reloc_count = sec->reloc_count;
11420 if (bed->elf_backend_count_additional_relocs)
11421 {
11422 int c;
11423 c = (*bed->elf_backend_count_additional_relocs) (sec);
11424 additional_reloc_count += c;
11425 }
11426 }
11427 else if (bed->elf_backend_count_relocs)
11428 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11429
11430 esdi = elf_section_data (sec);
11431
c152c796
AM
11432 if ((sec->flags & SEC_RELOC) != 0)
11433 {
d4730f92 11434 size_t ext_size = 0;
c152c796 11435
d4730f92
BS
11436 if (esdi->rel.hdr != NULL)
11437 ext_size = esdi->rel.hdr->sh_size;
11438 if (esdi->rela.hdr != NULL)
11439 ext_size += esdi->rela.hdr->sh_size;
7326c758 11440
c152c796
AM
11441 if (ext_size > max_external_reloc_size)
11442 max_external_reloc_size = ext_size;
11443 if (sec->reloc_count > max_internal_reloc_count)
11444 max_internal_reloc_count = sec->reloc_count;
11445 }
11446 }
11447 }
11448
11449 if (reloc_count == 0)
11450 continue;
11451
9eaff861 11452 reloc_count += additional_reloc_count;
c152c796
AM
11453 o->reloc_count += reloc_count;
11454
0e1862bb 11455 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11456 {
d4730f92 11457 if (esdi->rel.hdr)
9eaff861 11458 {
491d01d3 11459 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11460 esdo->rel.count += additional_reloc_count;
11461 }
d4730f92 11462 if (esdi->rela.hdr)
9eaff861 11463 {
491d01d3 11464 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11465 esdo->rela.count += additional_reloc_count;
11466 }
d4730f92
BS
11467 }
11468 else
11469 {
11470 if (o->use_rela_p)
11471 esdo->rela.count += reloc_count;
2c2b4ed4 11472 else
d4730f92 11473 esdo->rel.count += reloc_count;
c152c796 11474 }
c152c796
AM
11475 }
11476
9eaff861 11477 if (o->reloc_count > 0)
c152c796
AM
11478 o->flags |= SEC_RELOC;
11479 else
11480 {
11481 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11482 set it (this is probably a bug) and if it is set
11483 assign_section_numbers will create a reloc section. */
11484 o->flags &=~ SEC_RELOC;
11485 }
11486
11487 /* If the SEC_ALLOC flag is not set, force the section VMA to
11488 zero. This is done in elf_fake_sections as well, but forcing
11489 the VMA to 0 here will ensure that relocs against these
11490 sections are handled correctly. */
11491 if ((o->flags & SEC_ALLOC) == 0
11492 && ! o->user_set_vma)
11493 o->vma = 0;
11494 }
11495
0e1862bb 11496 if (! bfd_link_relocatable (info) && merged)
64f52338 11497 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11498
11499 /* Figure out the file positions for everything but the symbol table
11500 and the relocs. We set symcount to force assign_section_numbers
11501 to create a symbol table. */
8539e4e8 11502 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11503 BFD_ASSERT (! abfd->output_has_begun);
11504 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11505 goto error_return;
11506
ee75fd95 11507 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11508 for (o = abfd->sections; o != NULL; o = o->next)
11509 {
d4730f92 11510 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11511 if ((o->flags & SEC_RELOC) != 0)
11512 {
d4730f92 11513 if (esdo->rel.hdr
9eaff861 11514 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11515 goto error_return;
11516
d4730f92 11517 if (esdo->rela.hdr
9eaff861 11518 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11519 goto error_return;
11520 }
11521
11522 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11523 to count upwards while actually outputting the relocations. */
d4730f92
BS
11524 esdo->rel.count = 0;
11525 esdo->rela.count = 0;
0ce398f1
L
11526
11527 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11528 {
11529 /* Cache the section contents so that they can be compressed
11530 later. Use bfd_malloc since it will be freed by
11531 bfd_compress_section_contents. */
11532 unsigned char *contents = esdo->this_hdr.contents;
11533 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11534 abort ();
11535 contents
11536 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11537 if (contents == NULL)
11538 goto error_return;
11539 esdo->this_hdr.contents = contents;
11540 }
c152c796
AM
11541 }
11542
c152c796 11543 /* We have now assigned file positions for all the sections except
a485e98e
AM
11544 .symtab, .strtab, and non-loaded reloc sections. We start the
11545 .symtab section at the current file position, and write directly
11546 to it. We build the .strtab section in memory. */
c152c796
AM
11547 bfd_get_symcount (abfd) = 0;
11548 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11549 /* sh_name is set in prep_headers. */
11550 symtab_hdr->sh_type = SHT_SYMTAB;
11551 /* sh_flags, sh_addr and sh_size all start off zero. */
11552 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11553 /* sh_link is set in assign_section_numbers. */
11554 /* sh_info is set below. */
11555 /* sh_offset is set just below. */
72de5009 11556 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11557
ef10c3ac
L
11558 if (max_sym_count < 20)
11559 max_sym_count = 20;
64f52338 11560 htab->strtabsize = max_sym_count;
ef10c3ac 11561 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11562 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11563 if (htab->strtab == NULL)
c152c796 11564 goto error_return;
ef10c3ac
L
11565 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11566 flinfo.symshndxbuf
11567 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11568 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11569
8539e4e8 11570 if (info->strip != strip_all || emit_relocs)
c152c796 11571 {
8539e4e8
AM
11572 file_ptr off = elf_next_file_pos (abfd);
11573
11574 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11575
11576 /* Note that at this point elf_next_file_pos (abfd) is
11577 incorrect. We do not yet know the size of the .symtab section.
11578 We correct next_file_pos below, after we do know the size. */
11579
11580 /* Start writing out the symbol table. The first symbol is always a
11581 dummy symbol. */
c152c796
AM
11582 elfsym.st_value = 0;
11583 elfsym.st_size = 0;
11584 elfsym.st_info = 0;
11585 elfsym.st_other = 0;
11586 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11587 elfsym.st_target_internal = 0;
ef10c3ac
L
11588 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11589 bfd_und_section_ptr, NULL) != 1)
c152c796 11590 goto error_return;
c152c796 11591
8539e4e8
AM
11592 /* Output a symbol for each section. We output these even if we are
11593 discarding local symbols, since they are used for relocs. These
11594 symbols have no names. We store the index of each one in the
11595 index field of the section, so that we can find it again when
11596 outputting relocs. */
11597
c152c796
AM
11598 elfsym.st_size = 0;
11599 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11600 elfsym.st_other = 0;
f0b5bb34 11601 elfsym.st_value = 0;
35fc36a8 11602 elfsym.st_target_internal = 0;
c152c796
AM
11603 for (i = 1; i < elf_numsections (abfd); i++)
11604 {
11605 o = bfd_section_from_elf_index (abfd, i);
11606 if (o != NULL)
f0b5bb34
AM
11607 {
11608 o->target_index = bfd_get_symcount (abfd);
11609 elfsym.st_shndx = i;
0e1862bb 11610 if (!bfd_link_relocatable (info))
f0b5bb34 11611 elfsym.st_value = o->vma;
ef10c3ac
L
11612 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11613 NULL) != 1)
f0b5bb34
AM
11614 goto error_return;
11615 }
c152c796
AM
11616 }
11617 }
11618
11619 /* Allocate some memory to hold information read in from the input
11620 files. */
11621 if (max_contents_size != 0)
11622 {
8b127cbc
AM
11623 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11624 if (flinfo.contents == NULL)
c152c796
AM
11625 goto error_return;
11626 }
11627
11628 if (max_external_reloc_size != 0)
11629 {
8b127cbc
AM
11630 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11631 if (flinfo.external_relocs == NULL)
c152c796
AM
11632 goto error_return;
11633 }
11634
11635 if (max_internal_reloc_count != 0)
11636 {
11637 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11638 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11639 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11640 if (flinfo.internal_relocs == NULL)
c152c796
AM
11641 goto error_return;
11642 }
11643
11644 if (max_sym_count != 0)
11645 {
11646 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11647 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11648 if (flinfo.external_syms == NULL)
c152c796
AM
11649 goto error_return;
11650
11651 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11652 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11653 if (flinfo.internal_syms == NULL)
c152c796
AM
11654 goto error_return;
11655
11656 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11657 flinfo.indices = (long int *) bfd_malloc (amt);
11658 if (flinfo.indices == NULL)
c152c796
AM
11659 goto error_return;
11660
11661 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11662 flinfo.sections = (asection **) bfd_malloc (amt);
11663 if (flinfo.sections == NULL)
c152c796
AM
11664 goto error_return;
11665 }
11666
11667 if (max_sym_shndx_count != 0)
11668 {
11669 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11670 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11671 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11672 goto error_return;
11673 }
11674
64f52338 11675 if (htab->tls_sec)
c152c796
AM
11676 {
11677 bfd_vma base, end = 0;
11678 asection *sec;
11679
64f52338 11680 for (sec = htab->tls_sec;
c152c796
AM
11681 sec && (sec->flags & SEC_THREAD_LOCAL);
11682 sec = sec->next)
11683 {
3a800eb9 11684 bfd_size_type size = sec->size;
c152c796 11685
3a800eb9
AM
11686 if (size == 0
11687 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11688 {
91d6fa6a
NC
11689 struct bfd_link_order *ord = sec->map_tail.link_order;
11690
11691 if (ord != NULL)
11692 size = ord->offset + ord->size;
c152c796
AM
11693 }
11694 end = sec->vma + size;
11695 }
64f52338 11696 base = htab->tls_sec->vma;
7dc98aea
RO
11697 /* Only align end of TLS section if static TLS doesn't have special
11698 alignment requirements. */
11699 if (bed->static_tls_alignment == 1)
64f52338
AM
11700 end = align_power (end, htab->tls_sec->alignment_power);
11701 htab->tls_size = end - base;
c152c796
AM
11702 }
11703
0b52efa6
PB
11704 /* Reorder SHF_LINK_ORDER sections. */
11705 for (o = abfd->sections; o != NULL; o = o->next)
11706 {
11707 if (!elf_fixup_link_order (abfd, o))
11708 return FALSE;
11709 }
11710
2f0c68f2
CM
11711 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11712 return FALSE;
11713
c152c796
AM
11714 /* Since ELF permits relocations to be against local symbols, we
11715 must have the local symbols available when we do the relocations.
11716 Since we would rather only read the local symbols once, and we
11717 would rather not keep them in memory, we handle all the
11718 relocations for a single input file at the same time.
11719
11720 Unfortunately, there is no way to know the total number of local
11721 symbols until we have seen all of them, and the local symbol
11722 indices precede the global symbol indices. This means that when
11723 we are generating relocatable output, and we see a reloc against
11724 a global symbol, we can not know the symbol index until we have
11725 finished examining all the local symbols to see which ones we are
11726 going to output. To deal with this, we keep the relocations in
11727 memory, and don't output them until the end of the link. This is
11728 an unfortunate waste of memory, but I don't see a good way around
11729 it. Fortunately, it only happens when performing a relocatable
11730 link, which is not the common case. FIXME: If keep_memory is set
11731 we could write the relocs out and then read them again; I don't
11732 know how bad the memory loss will be. */
11733
c72f2fb2 11734 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11735 sub->output_has_begun = FALSE;
11736 for (o = abfd->sections; o != NULL; o = o->next)
11737 {
8423293d 11738 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11739 {
11740 if (p->type == bfd_indirect_link_order
11741 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11742 == bfd_target_elf_flavour)
11743 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11744 {
11745 if (! sub->output_has_begun)
11746 {
8b127cbc 11747 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11748 goto error_return;
11749 sub->output_has_begun = TRUE;
11750 }
11751 }
11752 else if (p->type == bfd_section_reloc_link_order
11753 || p->type == bfd_symbol_reloc_link_order)
11754 {
11755 if (! elf_reloc_link_order (abfd, info, o, p))
11756 goto error_return;
11757 }
11758 else
11759 {
11760 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11761 {
11762 if (p->type == bfd_indirect_link_order
11763 && (bfd_get_flavour (sub)
11764 == bfd_target_elf_flavour)
11765 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11766 != bed->s->elfclass))
11767 {
11768 const char *iclass, *oclass;
11769
aebf9be7 11770 switch (bed->s->elfclass)
351f65ca 11771 {
aebf9be7
NC
11772 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11773 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11774 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11775 default: abort ();
351f65ca 11776 }
aebf9be7
NC
11777
11778 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11779 {
aebf9be7
NC
11780 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11781 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11782 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11783 default: abort ();
351f65ca
L
11784 }
11785
11786 bfd_set_error (bfd_error_wrong_format);
4eca0228 11787 _bfd_error_handler
695344c0 11788 /* xgettext:c-format */
351f65ca
L
11789 (_("%B: file class %s incompatible with %s"),
11790 sub, iclass, oclass);
11791 }
11792
11793 goto error_return;
11794 }
c152c796
AM
11795 }
11796 }
11797 }
11798
c0f00686
L
11799 /* Free symbol buffer if needed. */
11800 if (!info->reduce_memory_overheads)
11801 {
c72f2fb2 11802 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11803 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11804 && elf_tdata (sub)->symbuf)
c0f00686
L
11805 {
11806 free (elf_tdata (sub)->symbuf);
11807 elf_tdata (sub)->symbuf = NULL;
11808 }
11809 }
11810
c152c796
AM
11811 /* Output any global symbols that got converted to local in a
11812 version script or due to symbol visibility. We do this in a
11813 separate step since ELF requires all local symbols to appear
11814 prior to any global symbols. FIXME: We should only do this if
11815 some global symbols were, in fact, converted to become local.
11816 FIXME: Will this work correctly with the Irix 5 linker? */
11817 eoinfo.failed = FALSE;
8b127cbc 11818 eoinfo.flinfo = &flinfo;
c152c796 11819 eoinfo.localsyms = TRUE;
34a79995 11820 eoinfo.file_sym_done = FALSE;
7686d77d 11821 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11822 if (eoinfo.failed)
11823 return FALSE;
11824
4e617b1e
PB
11825 /* If backend needs to output some local symbols not present in the hash
11826 table, do it now. */
8539e4e8
AM
11827 if (bed->elf_backend_output_arch_local_syms
11828 && (info->strip != strip_all || emit_relocs))
4e617b1e 11829 {
6e0b88f1 11830 typedef int (*out_sym_func)
4e617b1e
PB
11831 (void *, const char *, Elf_Internal_Sym *, asection *,
11832 struct elf_link_hash_entry *);
11833
11834 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11835 (abfd, info, &flinfo,
11836 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11837 return FALSE;
11838 }
11839
c152c796
AM
11840 /* That wrote out all the local symbols. Finish up the symbol table
11841 with the global symbols. Even if we want to strip everything we
11842 can, we still need to deal with those global symbols that got
11843 converted to local in a version script. */
11844
11845 /* The sh_info field records the index of the first non local symbol. */
11846 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11847
11848 if (dynamic
64f52338
AM
11849 && htab->dynsym != NULL
11850 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
11851 {
11852 Elf_Internal_Sym sym;
64f52338 11853 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 11854
64f52338
AM
11855 o = htab->dynsym->output_section;
11856 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
11857
11858 /* Write out the section symbols for the output sections. */
0e1862bb 11859 if (bfd_link_pic (info)
64f52338 11860 || htab->is_relocatable_executable)
c152c796
AM
11861 {
11862 asection *s;
11863
11864 sym.st_size = 0;
11865 sym.st_name = 0;
11866 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11867 sym.st_other = 0;
35fc36a8 11868 sym.st_target_internal = 0;
c152c796
AM
11869
11870 for (s = abfd->sections; s != NULL; s = s->next)
11871 {
11872 int indx;
11873 bfd_byte *dest;
11874 long dynindx;
11875
c152c796 11876 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11877 if (dynindx <= 0)
11878 continue;
11879 indx = elf_section_data (s)->this_idx;
c152c796
AM
11880 BFD_ASSERT (indx > 0);
11881 sym.st_shndx = indx;
c0d5a53d
L
11882 if (! check_dynsym (abfd, &sym))
11883 return FALSE;
c152c796
AM
11884 sym.st_value = s->vma;
11885 dest = dynsym + dynindx * bed->s->sizeof_sym;
11886 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11887 }
c152c796
AM
11888 }
11889
11890 /* Write out the local dynsyms. */
64f52338 11891 if (htab->dynlocal)
c152c796
AM
11892 {
11893 struct elf_link_local_dynamic_entry *e;
64f52338 11894 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
11895 {
11896 asection *s;
11897 bfd_byte *dest;
11898
935bd1e0 11899 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11900 Note that we saved a word of storage and overwrote
11901 the original st_name with the dynstr_index. */
11902 sym = e->isym;
935bd1e0 11903 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11904
cb33740c
AM
11905 s = bfd_section_from_elf_index (e->input_bfd,
11906 e->isym.st_shndx);
11907 if (s != NULL)
c152c796 11908 {
c152c796
AM
11909 sym.st_shndx =
11910 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11911 if (! check_dynsym (abfd, &sym))
11912 return FALSE;
c152c796
AM
11913 sym.st_value = (s->output_section->vma
11914 + s->output_offset
11915 + e->isym.st_value);
11916 }
11917
c152c796
AM
11918 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11919 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11920 }
11921 }
c152c796
AM
11922 }
11923
11924 /* We get the global symbols from the hash table. */
11925 eoinfo.failed = FALSE;
11926 eoinfo.localsyms = FALSE;
8b127cbc 11927 eoinfo.flinfo = &flinfo;
7686d77d 11928 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11929 if (eoinfo.failed)
11930 return FALSE;
11931
11932 /* If backend needs to output some symbols not present in the hash
11933 table, do it now. */
8539e4e8
AM
11934 if (bed->elf_backend_output_arch_syms
11935 && (info->strip != strip_all || emit_relocs))
c152c796 11936 {
6e0b88f1 11937 typedef int (*out_sym_func)
c152c796
AM
11938 (void *, const char *, Elf_Internal_Sym *, asection *,
11939 struct elf_link_hash_entry *);
11940
11941 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11942 (abfd, info, &flinfo,
11943 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11944 return FALSE;
11945 }
11946
ef10c3ac
L
11947 /* Finalize the .strtab section. */
11948 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11949
11950 /* Swap out the .strtab section. */
11951 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11952 return FALSE;
11953
11954 /* Now we know the size of the symtab section. */
c152c796
AM
11955 if (bfd_get_symcount (abfd) > 0)
11956 {
ee3b52e9
L
11957 /* Finish up and write out the symbol string table (.strtab)
11958 section. */
11959 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11960 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11961
6a40cf0c
NC
11962 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11963 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11964 {
11965 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11966 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11967 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11968 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11969 symtab_shndx_hdr->sh_size = amt;
11970
11971 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11972 off, TRUE);
11973
11974 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11975 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11976 return FALSE;
11977 }
ee3b52e9
L
11978
11979 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11980 /* sh_name was set in prep_headers. */
11981 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11982 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11983 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11984 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11985 symstrtab_hdr->sh_entsize = 0;
11986 symstrtab_hdr->sh_link = 0;
11987 symstrtab_hdr->sh_info = 0;
11988 /* sh_offset is set just below. */
11989 symstrtab_hdr->sh_addralign = 1;
11990
11991 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11992 off, TRUE);
11993 elf_next_file_pos (abfd) = off;
11994
c152c796 11995 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11996 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11997 return FALSE;
11998 }
11999
76359541
TP
12000 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12001 {
4eca0228
AM
12002 _bfd_error_handler (_("%B: failed to generate import library"),
12003 info->out_implib_bfd);
76359541
TP
12004 return FALSE;
12005 }
12006
c152c796
AM
12007 /* Adjust the relocs to have the correct symbol indices. */
12008 for (o = abfd->sections; o != NULL; o = o->next)
12009 {
d4730f92 12010 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12011 bfd_boolean sort;
c152c796
AM
12012 if ((o->flags & SEC_RELOC) == 0)
12013 continue;
12014
28dbcedc 12015 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12016 if (esdo->rel.hdr != NULL
9eaff861 12017 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
12018 return FALSE;
12019 if (esdo->rela.hdr != NULL
9eaff861 12020 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 12021 return FALSE;
c152c796
AM
12022
12023 /* Set the reloc_count field to 0 to prevent write_relocs from
12024 trying to swap the relocs out itself. */
12025 o->reloc_count = 0;
12026 }
12027
12028 if (dynamic && info->combreloc && dynobj != NULL)
12029 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12030
12031 /* If we are linking against a dynamic object, or generating a
12032 shared library, finish up the dynamic linking information. */
12033 if (dynamic)
12034 {
12035 bfd_byte *dyncon, *dynconend;
12036
12037 /* Fix up .dynamic entries. */
3d4d4302 12038 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12039 BFD_ASSERT (o != NULL);
12040
12041 dyncon = o->contents;
eea6121a 12042 dynconend = o->contents + o->size;
c152c796
AM
12043 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12044 {
12045 Elf_Internal_Dyn dyn;
12046 const char *name;
12047 unsigned int type;
12048
12049 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12050
12051 switch (dyn.d_tag)
12052 {
12053 default:
12054 continue;
12055 case DT_NULL:
12056 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12057 {
12058 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12059 {
12060 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12061 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12062 default: continue;
12063 }
12064 dyn.d_un.d_val = relativecount;
12065 relativecount = 0;
12066 break;
12067 }
12068 continue;
12069
12070 case DT_INIT:
12071 name = info->init_function;
12072 goto get_sym;
12073 case DT_FINI:
12074 name = info->fini_function;
12075 get_sym:
12076 {
12077 struct elf_link_hash_entry *h;
12078
64f52338 12079 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12080 if (h != NULL
12081 && (h->root.type == bfd_link_hash_defined
12082 || h->root.type == bfd_link_hash_defweak))
12083 {
bef26483 12084 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12085 o = h->root.u.def.section;
12086 if (o->output_section != NULL)
bef26483 12087 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12088 + o->output_offset);
12089 else
12090 {
12091 /* The symbol is imported from another shared
12092 library and does not apply to this one. */
bef26483 12093 dyn.d_un.d_ptr = 0;
c152c796
AM
12094 }
12095 break;
12096 }
12097 }
12098 continue;
12099
12100 case DT_PREINIT_ARRAYSZ:
12101 name = ".preinit_array";
4ade44b7 12102 goto get_out_size;
c152c796
AM
12103 case DT_INIT_ARRAYSZ:
12104 name = ".init_array";
4ade44b7 12105 goto get_out_size;
c152c796
AM
12106 case DT_FINI_ARRAYSZ:
12107 name = ".fini_array";
4ade44b7 12108 get_out_size:
c152c796
AM
12109 o = bfd_get_section_by_name (abfd, name);
12110 if (o == NULL)
12111 {
4eca0228 12112 _bfd_error_handler
4ade44b7 12113 (_("could not find section %s"), name);
c152c796
AM
12114 goto error_return;
12115 }
eea6121a 12116 if (o->size == 0)
4eca0228 12117 _bfd_error_handler
c152c796 12118 (_("warning: %s section has zero size"), name);
eea6121a 12119 dyn.d_un.d_val = o->size;
c152c796
AM
12120 break;
12121
12122 case DT_PREINIT_ARRAY:
12123 name = ".preinit_array";
4ade44b7 12124 goto get_out_vma;
c152c796
AM
12125 case DT_INIT_ARRAY:
12126 name = ".init_array";
4ade44b7 12127 goto get_out_vma;
c152c796
AM
12128 case DT_FINI_ARRAY:
12129 name = ".fini_array";
4ade44b7
AM
12130 get_out_vma:
12131 o = bfd_get_section_by_name (abfd, name);
12132 goto do_vma;
c152c796
AM
12133
12134 case DT_HASH:
12135 name = ".hash";
12136 goto get_vma;
fdc90cb4
JJ
12137 case DT_GNU_HASH:
12138 name = ".gnu.hash";
12139 goto get_vma;
c152c796
AM
12140 case DT_STRTAB:
12141 name = ".dynstr";
12142 goto get_vma;
12143 case DT_SYMTAB:
12144 name = ".dynsym";
12145 goto get_vma;
12146 case DT_VERDEF:
12147 name = ".gnu.version_d";
12148 goto get_vma;
12149 case DT_VERNEED:
12150 name = ".gnu.version_r";
12151 goto get_vma;
12152 case DT_VERSYM:
12153 name = ".gnu.version";
12154 get_vma:
4ade44b7
AM
12155 o = bfd_get_linker_section (dynobj, name);
12156 do_vma:
c152c796
AM
12157 if (o == NULL)
12158 {
4eca0228 12159 _bfd_error_handler
4ade44b7 12160 (_("could not find section %s"), name);
c152c796
AM
12161 goto error_return;
12162 }
894891db
NC
12163 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12164 {
4eca0228 12165 _bfd_error_handler
894891db
NC
12166 (_("warning: section '%s' is being made into a note"), name);
12167 bfd_set_error (bfd_error_nonrepresentable_section);
12168 goto error_return;
12169 }
4ade44b7 12170 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12171 break;
12172
12173 case DT_REL:
12174 case DT_RELA:
12175 case DT_RELSZ:
12176 case DT_RELASZ:
12177 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12178 type = SHT_REL;
12179 else
12180 type = SHT_RELA;
12181 dyn.d_un.d_val = 0;
bef26483 12182 dyn.d_un.d_ptr = 0;
c152c796
AM
12183 for (i = 1; i < elf_numsections (abfd); i++)
12184 {
12185 Elf_Internal_Shdr *hdr;
12186
12187 hdr = elf_elfsections (abfd)[i];
12188 if (hdr->sh_type == type
12189 && (hdr->sh_flags & SHF_ALLOC) != 0)
12190 {
12191 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12192 dyn.d_un.d_val += hdr->sh_size;
12193 else
12194 {
bef26483
AM
12195 if (dyn.d_un.d_ptr == 0
12196 || hdr->sh_addr < dyn.d_un.d_ptr)
12197 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12198 }
12199 }
12200 }
64f52338
AM
12201 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12202 {
12203 /* Don't count procedure linkage table relocs in the
12204 overall reloc count. */
12205 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12206 dyn.d_un.d_val -= htab->srelplt->size;
12207 /* If .rela.plt is the first .rela section, exclude
12208 it from DT_RELA. */
12209 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12210 + htab->srelplt->output_offset))
12211 dyn.d_un.d_ptr += htab->srelplt->size;
12212 }
c152c796
AM
12213 break;
12214 }
12215 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12216 }
12217 }
12218
12219 /* If we have created any dynamic sections, then output them. */
12220 if (dynobj != NULL)
12221 {
12222 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12223 goto error_return;
12224
943284cc 12225 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12226 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12227 || info->error_textrel)
3d4d4302 12228 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12229 {
12230 bfd_byte *dyncon, *dynconend;
12231
943284cc
DJ
12232 dyncon = o->contents;
12233 dynconend = o->contents + o->size;
12234 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12235 {
12236 Elf_Internal_Dyn dyn;
12237
12238 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12239
12240 if (dyn.d_tag == DT_TEXTREL)
12241 {
c192a133
AM
12242 if (info->error_textrel)
12243 info->callbacks->einfo
12244 (_("%P%X: read-only segment has dynamic relocations.\n"));
12245 else
12246 info->callbacks->einfo
12247 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12248 break;
12249 }
12250 }
12251 }
12252
c152c796
AM
12253 for (o = dynobj->sections; o != NULL; o = o->next)
12254 {
12255 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12256 || o->size == 0
c152c796
AM
12257 || o->output_section == bfd_abs_section_ptr)
12258 continue;
12259 if ((o->flags & SEC_LINKER_CREATED) == 0)
12260 {
12261 /* At this point, we are only interested in sections
12262 created by _bfd_elf_link_create_dynamic_sections. */
12263 continue;
12264 }
64f52338 12265 if (htab->stab_info.stabstr == o)
3722b82f 12266 continue;
64f52338 12267 if (htab->eh_info.hdr_sec == o)
eea6121a 12268 continue;
3d4d4302 12269 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12270 {
12271 if (! bfd_set_section_contents (abfd, o->output_section,
12272 o->contents,
37b01f6a
DG
12273 (file_ptr) o->output_offset
12274 * bfd_octets_per_byte (abfd),
eea6121a 12275 o->size))
c152c796
AM
12276 goto error_return;
12277 }
12278 else
12279 {
12280 /* The contents of the .dynstr section are actually in a
12281 stringtab. */
8539e4e8
AM
12282 file_ptr off;
12283
c152c796
AM
12284 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12285 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12286 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12287 goto error_return;
12288 }
12289 }
12290 }
12291
0e1862bb 12292 if (bfd_link_relocatable (info))
c152c796
AM
12293 {
12294 bfd_boolean failed = FALSE;
12295
12296 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12297 if (failed)
12298 goto error_return;
12299 }
12300
12301 /* If we have optimized stabs strings, output them. */
64f52338 12302 if (htab->stab_info.stabstr != NULL)
c152c796 12303 {
64f52338 12304 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12305 goto error_return;
12306 }
12307
9f7c3e5e
AM
12308 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12309 goto error_return;
c152c796 12310
9f7c3e5e 12311 elf_final_link_free (abfd, &flinfo);
c152c796 12312
12bd6957 12313 elf_linker (abfd) = TRUE;
c152c796 12314
104d59d1
JM
12315 if (attr_section)
12316 {
a50b1753 12317 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12318 if (contents == NULL)
d0f16d5e 12319 return FALSE; /* Bail out and fail. */
104d59d1
JM
12320 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12321 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12322 free (contents);
12323 }
12324
c152c796
AM
12325 return TRUE;
12326
12327 error_return:
9f7c3e5e 12328 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12329 return FALSE;
12330}
12331\f
5241d853
RS
12332/* Initialize COOKIE for input bfd ABFD. */
12333
12334static bfd_boolean
12335init_reloc_cookie (struct elf_reloc_cookie *cookie,
12336 struct bfd_link_info *info, bfd *abfd)
12337{
12338 Elf_Internal_Shdr *symtab_hdr;
12339 const struct elf_backend_data *bed;
12340
12341 bed = get_elf_backend_data (abfd);
12342 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12343
12344 cookie->abfd = abfd;
12345 cookie->sym_hashes = elf_sym_hashes (abfd);
12346 cookie->bad_symtab = elf_bad_symtab (abfd);
12347 if (cookie->bad_symtab)
12348 {
12349 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12350 cookie->extsymoff = 0;
12351 }
12352 else
12353 {
12354 cookie->locsymcount = symtab_hdr->sh_info;
12355 cookie->extsymoff = symtab_hdr->sh_info;
12356 }
12357
12358 if (bed->s->arch_size == 32)
12359 cookie->r_sym_shift = 8;
12360 else
12361 cookie->r_sym_shift = 32;
12362
12363 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12364 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12365 {
12366 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12367 cookie->locsymcount, 0,
12368 NULL, NULL, NULL);
12369 if (cookie->locsyms == NULL)
12370 {
12371 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12372 return FALSE;
12373 }
12374 if (info->keep_memory)
12375 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12376 }
12377 return TRUE;
12378}
12379
12380/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12381
12382static void
12383fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12384{
12385 Elf_Internal_Shdr *symtab_hdr;
12386
12387 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12388 if (cookie->locsyms != NULL
12389 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12390 free (cookie->locsyms);
12391}
12392
12393/* Initialize the relocation information in COOKIE for input section SEC
12394 of input bfd ABFD. */
12395
12396static bfd_boolean
12397init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12398 struct bfd_link_info *info, bfd *abfd,
12399 asection *sec)
12400{
12401 const struct elf_backend_data *bed;
12402
12403 if (sec->reloc_count == 0)
12404 {
12405 cookie->rels = NULL;
12406 cookie->relend = NULL;
12407 }
12408 else
12409 {
12410 bed = get_elf_backend_data (abfd);
12411
12412 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12413 info->keep_memory);
12414 if (cookie->rels == NULL)
12415 return FALSE;
12416 cookie->rel = cookie->rels;
12417 cookie->relend = (cookie->rels
12418 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12419 }
12420 cookie->rel = cookie->rels;
12421 return TRUE;
12422}
12423
12424/* Free the memory allocated by init_reloc_cookie_rels,
12425 if appropriate. */
12426
12427static void
12428fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12429 asection *sec)
12430{
12431 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12432 free (cookie->rels);
12433}
12434
12435/* Initialize the whole of COOKIE for input section SEC. */
12436
12437static bfd_boolean
12438init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12439 struct bfd_link_info *info,
12440 asection *sec)
12441{
12442 if (!init_reloc_cookie (cookie, info, sec->owner))
12443 goto error1;
12444 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12445 goto error2;
12446 return TRUE;
12447
12448 error2:
12449 fini_reloc_cookie (cookie, sec->owner);
12450 error1:
12451 return FALSE;
12452}
12453
12454/* Free the memory allocated by init_reloc_cookie_for_section,
12455 if appropriate. */
12456
12457static void
12458fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12459 asection *sec)
12460{
12461 fini_reloc_cookie_rels (cookie, sec);
12462 fini_reloc_cookie (cookie, sec->owner);
12463}
12464\f
c152c796
AM
12465/* Garbage collect unused sections. */
12466
07adf181
AM
12467/* Default gc_mark_hook. */
12468
12469asection *
12470_bfd_elf_gc_mark_hook (asection *sec,
12471 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12472 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12473 struct elf_link_hash_entry *h,
12474 Elf_Internal_Sym *sym)
12475{
12476 if (h != NULL)
12477 {
12478 switch (h->root.type)
12479 {
12480 case bfd_link_hash_defined:
12481 case bfd_link_hash_defweak:
12482 return h->root.u.def.section;
12483
12484 case bfd_link_hash_common:
12485 return h->root.u.c.p->section;
12486
12487 default:
12488 break;
12489 }
12490 }
12491 else
12492 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12493
12494 return NULL;
12495}
12496
a6a4679f
AM
12497/* For undefined __start_<name> and __stop_<name> symbols, return the
12498 first input section matching <name>. Return NULL otherwise. */
12499
12500asection *
12501_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12502 struct elf_link_hash_entry *h)
12503{
12504 asection *s;
12505 const char *sec_name;
12506
12507 if (h->root.type != bfd_link_hash_undefined
12508 && h->root.type != bfd_link_hash_undefweak)
12509 return NULL;
12510
12511 s = h->root.u.undef.section;
12512 if (s != NULL)
12513 {
12514 if (s == (asection *) 0 - 1)
12515 return NULL;
12516 return s;
12517 }
12518
12519 sec_name = NULL;
12520 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12521 sec_name = h->root.root.string + 8;
12522 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12523 sec_name = h->root.root.string + 7;
12524
12525 if (sec_name != NULL && *sec_name != '\0')
12526 {
12527 bfd *i;
12528
12529 for (i = info->input_bfds; i != NULL; i = i->link.next)
12530 {
12531 s = bfd_get_section_by_name (i, sec_name);
12532 if (s != NULL)
12533 {
12534 h->root.u.undef.section = s;
12535 break;
12536 }
12537 }
12538 }
12539
12540 if (s == NULL)
12541 h->root.u.undef.section = (asection *) 0 - 1;
12542
12543 return s;
12544}
12545
5241d853
RS
12546/* COOKIE->rel describes a relocation against section SEC, which is
12547 a section we've decided to keep. Return the section that contains
12548 the relocation symbol, or NULL if no section contains it. */
12549
12550asection *
12551_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12552 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12553 struct elf_reloc_cookie *cookie,
12554 bfd_boolean *start_stop)
5241d853
RS
12555{
12556 unsigned long r_symndx;
12557 struct elf_link_hash_entry *h;
12558
12559 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12560 if (r_symndx == STN_UNDEF)
5241d853
RS
12561 return NULL;
12562
12563 if (r_symndx >= cookie->locsymcount
12564 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12565 {
12566 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12567 if (h == NULL)
12568 {
12569 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12570 sec->owner);
12571 return NULL;
12572 }
5241d853
RS
12573 while (h->root.type == bfd_link_hash_indirect
12574 || h->root.type == bfd_link_hash_warning)
12575 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12576 h->mark = 1;
4e6b54a6
AM
12577 /* If this symbol is weak and there is a non-weak definition, we
12578 keep the non-weak definition because many backends put
12579 dynamic reloc info on the non-weak definition for code
12580 handling copy relocs. */
12581 if (h->u.weakdef != NULL)
12582 h->u.weakdef->mark = 1;
1cce69b9 12583
a6a4679f 12584 if (start_stop != NULL)
1cce69b9
AM
12585 {
12586 /* To work around a glibc bug, mark all XXX input sections
12587 when there is an as yet undefined reference to __start_XXX
12588 or __stop_XXX symbols. The linker will later define such
12589 symbols for orphan input sections that have a name
12590 representable as a C identifier. */
a6a4679f 12591 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12592
a6a4679f 12593 if (s != NULL)
1cce69b9 12594 {
a6a4679f
AM
12595 *start_stop = !s->gc_mark;
12596 return s;
1cce69b9
AM
12597 }
12598 }
12599
5241d853
RS
12600 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12601 }
12602
12603 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12604 &cookie->locsyms[r_symndx]);
12605}
12606
12607/* COOKIE->rel describes a relocation against section SEC, which is
12608 a section we've decided to keep. Mark the section that contains
9d0a14d3 12609 the relocation symbol. */
5241d853
RS
12610
12611bfd_boolean
12612_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12613 asection *sec,
12614 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12615 struct elf_reloc_cookie *cookie)
5241d853
RS
12616{
12617 asection *rsec;
1cce69b9 12618 bfd_boolean start_stop = FALSE;
5241d853 12619
1cce69b9
AM
12620 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12621 while (rsec != NULL)
5241d853 12622 {
1cce69b9
AM
12623 if (!rsec->gc_mark)
12624 {
12625 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12626 || (rsec->owner->flags & DYNAMIC) != 0)
12627 rsec->gc_mark = 1;
12628 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12629 return FALSE;
12630 }
12631 if (!start_stop)
12632 break;
199af150 12633 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12634 }
12635 return TRUE;
12636}
12637
07adf181
AM
12638/* The mark phase of garbage collection. For a given section, mark
12639 it and any sections in this section's group, and all the sections
12640 which define symbols to which it refers. */
12641
ccfa59ea
AM
12642bfd_boolean
12643_bfd_elf_gc_mark (struct bfd_link_info *info,
12644 asection *sec,
6a5bb875 12645 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12646{
12647 bfd_boolean ret;
9d0a14d3 12648 asection *group_sec, *eh_frame;
c152c796
AM
12649
12650 sec->gc_mark = 1;
12651
12652 /* Mark all the sections in the group. */
12653 group_sec = elf_section_data (sec)->next_in_group;
12654 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12655 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12656 return FALSE;
12657
12658 /* Look through the section relocs. */
12659 ret = TRUE;
9d0a14d3
RS
12660 eh_frame = elf_eh_frame_section (sec->owner);
12661 if ((sec->flags & SEC_RELOC) != 0
12662 && sec->reloc_count > 0
12663 && sec != eh_frame)
c152c796 12664 {
5241d853 12665 struct elf_reloc_cookie cookie;
c152c796 12666
5241d853
RS
12667 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12668 ret = FALSE;
c152c796 12669 else
c152c796 12670 {
5241d853 12671 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12672 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12673 {
12674 ret = FALSE;
12675 break;
12676 }
12677 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12678 }
12679 }
9d0a14d3
RS
12680
12681 if (ret && eh_frame && elf_fde_list (sec))
12682 {
12683 struct elf_reloc_cookie cookie;
12684
12685 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12686 ret = FALSE;
12687 else
12688 {
12689 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12690 gc_mark_hook, &cookie))
12691 ret = FALSE;
12692 fini_reloc_cookie_for_section (&cookie, eh_frame);
12693 }
12694 }
12695
2f0c68f2
CM
12696 eh_frame = elf_section_eh_frame_entry (sec);
12697 if (ret && eh_frame && !eh_frame->gc_mark)
12698 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12699 ret = FALSE;
12700
c152c796
AM
12701 return ret;
12702}
12703
3c758495
TG
12704/* Scan and mark sections in a special or debug section group. */
12705
12706static void
12707_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12708{
12709 /* Point to first section of section group. */
12710 asection *ssec;
12711 /* Used to iterate the section group. */
12712 asection *msec;
12713
12714 bfd_boolean is_special_grp = TRUE;
12715 bfd_boolean is_debug_grp = TRUE;
12716
12717 /* First scan to see if group contains any section other than debug
12718 and special section. */
12719 ssec = msec = elf_next_in_group (grp);
12720 do
12721 {
12722 if ((msec->flags & SEC_DEBUGGING) == 0)
12723 is_debug_grp = FALSE;
12724
12725 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12726 is_special_grp = FALSE;
12727
12728 msec = elf_next_in_group (msec);
12729 }
12730 while (msec != ssec);
12731
12732 /* If this is a pure debug section group or pure special section group,
12733 keep all sections in this group. */
12734 if (is_debug_grp || is_special_grp)
12735 {
12736 do
12737 {
12738 msec->gc_mark = 1;
12739 msec = elf_next_in_group (msec);
12740 }
12741 while (msec != ssec);
12742 }
12743}
12744
7f6ab9f8
AM
12745/* Keep debug and special sections. */
12746
12747bfd_boolean
12748_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12749 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12750{
12751 bfd *ibfd;
12752
c72f2fb2 12753 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12754 {
12755 asection *isec;
12756 bfd_boolean some_kept;
b40bf0a2 12757 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12758
12759 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12760 continue;
12761
b40bf0a2
NC
12762 /* Ensure all linker created sections are kept,
12763 see if any other section is already marked,
12764 and note if we have any fragmented debug sections. */
12765 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12766 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12767 {
12768 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12769 isec->gc_mark = 1;
12770 else if (isec->gc_mark)
12771 some_kept = TRUE;
b40bf0a2
NC
12772
12773 if (debug_frag_seen == FALSE
12774 && (isec->flags & SEC_DEBUGGING)
12775 && CONST_STRNEQ (isec->name, ".debug_line."))
12776 debug_frag_seen = TRUE;
7f6ab9f8
AM
12777 }
12778
12779 /* If no section in this file will be kept, then we can
b40bf0a2 12780 toss out the debug and special sections. */
7f6ab9f8
AM
12781 if (!some_kept)
12782 continue;
12783
12784 /* Keep debug and special sections like .comment when they are
3c758495
TG
12785 not part of a group. Also keep section groups that contain
12786 just debug sections or special sections. */
7f6ab9f8 12787 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12788 {
12789 if ((isec->flags & SEC_GROUP) != 0)
12790 _bfd_elf_gc_mark_debug_special_section_group (isec);
12791 else if (((isec->flags & SEC_DEBUGGING) != 0
12792 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12793 && elf_next_in_group (isec) == NULL)
12794 isec->gc_mark = 1;
12795 }
b40bf0a2
NC
12796
12797 if (! debug_frag_seen)
12798 continue;
12799
12800 /* Look for CODE sections which are going to be discarded,
12801 and find and discard any fragmented debug sections which
12802 are associated with that code section. */
12803 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12804 if ((isec->flags & SEC_CODE) != 0
12805 && isec->gc_mark == 0)
12806 {
12807 unsigned int ilen;
12808 asection *dsec;
12809
12810 ilen = strlen (isec->name);
12811
12812 /* Association is determined by the name of the debug section
12813 containing the name of the code section as a suffix. For
12814 example .debug_line.text.foo is a debug section associated
12815 with .text.foo. */
12816 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12817 {
12818 unsigned int dlen;
12819
12820 if (dsec->gc_mark == 0
12821 || (dsec->flags & SEC_DEBUGGING) == 0)
12822 continue;
12823
12824 dlen = strlen (dsec->name);
12825
12826 if (dlen > ilen
12827 && strncmp (dsec->name + (dlen - ilen),
12828 isec->name, ilen) == 0)
12829 {
12830 dsec->gc_mark = 0;
b40bf0a2
NC
12831 }
12832 }
12833 }
7f6ab9f8
AM
12834 }
12835 return TRUE;
12836}
12837
c152c796
AM
12838/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12839
c17d87de
NC
12840struct elf_gc_sweep_symbol_info
12841{
ccabcbe5
AM
12842 struct bfd_link_info *info;
12843 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12844 bfd_boolean);
12845};
12846
c152c796 12847static bfd_boolean
ccabcbe5 12848elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12849{
1d5316ab
AM
12850 if (!h->mark
12851 && (((h->root.type == bfd_link_hash_defined
12852 || h->root.type == bfd_link_hash_defweak)
c4621b33 12853 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12854 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12855 || h->root.type == bfd_link_hash_undefined
12856 || h->root.type == bfd_link_hash_undefweak))
12857 {
12858 struct elf_gc_sweep_symbol_info *inf;
12859
12860 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12861 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12862 h->def_regular = 0;
12863 h->ref_regular = 0;
12864 h->ref_regular_nonweak = 0;
ccabcbe5 12865 }
c152c796
AM
12866
12867 return TRUE;
12868}
12869
12870/* The sweep phase of garbage collection. Remove all garbage sections. */
12871
12872typedef bfd_boolean (*gc_sweep_hook_fn)
12873 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12874
12875static bfd_boolean
ccabcbe5 12876elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12877{
12878 bfd *sub;
ccabcbe5
AM
12879 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12880 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12881 unsigned long section_sym_count;
12882 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12883
c72f2fb2 12884 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12885 {
12886 asection *o;
12887
b19a8f85
L
12888 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12889 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12890 continue;
12891
12892 for (o = sub->sections; o != NULL; o = o->next)
12893 {
a33dafc3
L
12894 /* When any section in a section group is kept, we keep all
12895 sections in the section group. If the first member of
12896 the section group is excluded, we will also exclude the
12897 group section. */
12898 if (o->flags & SEC_GROUP)
12899 {
12900 asection *first = elf_next_in_group (o);
12901 o->gc_mark = first->gc_mark;
12902 }
c152c796 12903
1e7eae0d 12904 if (o->gc_mark)
c152c796
AM
12905 continue;
12906
12907 /* Skip sweeping sections already excluded. */
12908 if (o->flags & SEC_EXCLUDE)
12909 continue;
12910
12911 /* Since this is early in the link process, it is simple
12912 to remove a section from the output. */
12913 o->flags |= SEC_EXCLUDE;
12914
c55fe096 12915 if (info->print_gc_sections && o->size != 0)
695344c0 12916 /* xgettext:c-format */
c17d87de
NC
12917 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12918
c152c796
AM
12919 /* But we also have to update some of the relocation
12920 info we collected before. */
12921 if (gc_sweep_hook
e8aaee2a 12922 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12923 && o->reloc_count != 0
12924 && !((info->strip == strip_all || info->strip == strip_debugger)
12925 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12926 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12927 {
12928 Elf_Internal_Rela *internal_relocs;
12929 bfd_boolean r;
12930
12931 internal_relocs
12932 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12933 info->keep_memory);
12934 if (internal_relocs == NULL)
12935 return FALSE;
12936
12937 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12938
12939 if (elf_section_data (o)->relocs != internal_relocs)
12940 free (internal_relocs);
12941
12942 if (!r)
12943 return FALSE;
12944 }
12945 }
12946 }
12947
12948 /* Remove the symbols that were in the swept sections from the dynamic
12949 symbol table. GCFIXME: Anyone know how to get them out of the
12950 static symbol table as well? */
ccabcbe5
AM
12951 sweep_info.info = info;
12952 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12953 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12954 &sweep_info);
c152c796 12955
ccabcbe5 12956 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12957 return TRUE;
12958}
12959
12960/* Propagate collected vtable information. This is called through
12961 elf_link_hash_traverse. */
12962
12963static bfd_boolean
12964elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12965{
c152c796 12966 /* Those that are not vtables. */
f6e332e6 12967 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12968 return TRUE;
12969
12970 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12971 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12972 return TRUE;
12973
12974 /* If we've already been done, exit. */
f6e332e6 12975 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12976 return TRUE;
12977
12978 /* Make sure the parent's table is up to date. */
f6e332e6 12979 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12980
f6e332e6 12981 if (h->vtable->used == NULL)
c152c796
AM
12982 {
12983 /* None of this table's entries were referenced. Re-use the
12984 parent's table. */
f6e332e6
AM
12985 h->vtable->used = h->vtable->parent->vtable->used;
12986 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12987 }
12988 else
12989 {
12990 size_t n;
12991 bfd_boolean *cu, *pu;
12992
12993 /* Or the parent's entries into ours. */
f6e332e6 12994 cu = h->vtable->used;
c152c796 12995 cu[-1] = TRUE;
f6e332e6 12996 pu = h->vtable->parent->vtable->used;
c152c796
AM
12997 if (pu != NULL)
12998 {
12999 const struct elf_backend_data *bed;
13000 unsigned int log_file_align;
13001
13002 bed = get_elf_backend_data (h->root.u.def.section->owner);
13003 log_file_align = bed->s->log_file_align;
f6e332e6 13004 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
13005 while (n--)
13006 {
13007 if (*pu)
13008 *cu = TRUE;
13009 pu++;
13010 cu++;
13011 }
13012 }
13013 }
13014
13015 return TRUE;
13016}
13017
13018static bfd_boolean
13019elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13020{
13021 asection *sec;
13022 bfd_vma hstart, hend;
13023 Elf_Internal_Rela *relstart, *relend, *rel;
13024 const struct elf_backend_data *bed;
13025 unsigned int log_file_align;
13026
c152c796
AM
13027 /* Take care of both those symbols that do not describe vtables as
13028 well as those that are not loaded. */
f6e332e6 13029 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13030 return TRUE;
13031
13032 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13033 || h->root.type == bfd_link_hash_defweak);
13034
13035 sec = h->root.u.def.section;
13036 hstart = h->root.u.def.value;
13037 hend = hstart + h->size;
13038
13039 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13040 if (!relstart)
13041 return *(bfd_boolean *) okp = FALSE;
13042 bed = get_elf_backend_data (sec->owner);
13043 log_file_align = bed->s->log_file_align;
13044
13045 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13046
13047 for (rel = relstart; rel < relend; ++rel)
13048 if (rel->r_offset >= hstart && rel->r_offset < hend)
13049 {
13050 /* If the entry is in use, do nothing. */
f6e332e6
AM
13051 if (h->vtable->used
13052 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13053 {
13054 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13055 if (h->vtable->used[entry])
c152c796
AM
13056 continue;
13057 }
13058 /* Otherwise, kill it. */
13059 rel->r_offset = rel->r_info = rel->r_addend = 0;
13060 }
13061
13062 return TRUE;
13063}
13064
87538722
AM
13065/* Mark sections containing dynamically referenced symbols. When
13066 building shared libraries, we must assume that any visible symbol is
13067 referenced. */
715df9b8 13068
64d03ab5
AM
13069bfd_boolean
13070bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13071{
87538722 13072 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13073 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13074
715df9b8
EB
13075 if ((h->root.type == bfd_link_hash_defined
13076 || h->root.type == bfd_link_hash_defweak)
87538722 13077 && (h->ref_dynamic
c4621b33 13078 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13079 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13080 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13081 && (!bfd_link_executable (info)
22185505 13082 || info->gc_keep_exported
b407645f
AM
13083 || info->export_dynamic
13084 || (h->dynamic
13085 && d != NULL
13086 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13087 && (h->versioned >= versioned
54e8959c
L
13088 || !bfd_hide_sym_by_version (info->version_info,
13089 h->root.root.string)))))
715df9b8
EB
13090 h->root.u.def.section->flags |= SEC_KEEP;
13091
13092 return TRUE;
13093}
3b36f7e6 13094
74f0fb50
AM
13095/* Keep all sections containing symbols undefined on the command-line,
13096 and the section containing the entry symbol. */
13097
13098void
13099_bfd_elf_gc_keep (struct bfd_link_info *info)
13100{
13101 struct bfd_sym_chain *sym;
13102
13103 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13104 {
13105 struct elf_link_hash_entry *h;
13106
13107 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13108 FALSE, FALSE, FALSE);
13109
13110 if (h != NULL
13111 && (h->root.type == bfd_link_hash_defined
13112 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13113 && !bfd_is_abs_section (h->root.u.def.section)
13114 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13115 h->root.u.def.section->flags |= SEC_KEEP;
13116 }
13117}
13118
2f0c68f2
CM
13119bfd_boolean
13120bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13121 struct bfd_link_info *info)
13122{
13123 bfd *ibfd = info->input_bfds;
13124
13125 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13126 {
13127 asection *sec;
13128 struct elf_reloc_cookie cookie;
13129
13130 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13131 continue;
13132
13133 if (!init_reloc_cookie (&cookie, info, ibfd))
13134 return FALSE;
13135
13136 for (sec = ibfd->sections; sec; sec = sec->next)
13137 {
13138 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13139 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13140 {
13141 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13142 fini_reloc_cookie_rels (&cookie, sec);
13143 }
13144 }
13145 }
13146 return TRUE;
13147}
13148
c152c796
AM
13149/* Do mark and sweep of unused sections. */
13150
13151bfd_boolean
13152bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13153{
13154 bfd_boolean ok = TRUE;
13155 bfd *sub;
6a5bb875 13156 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13158 struct elf_link_hash_table *htab;
c152c796 13159
64d03ab5 13160 if (!bed->can_gc_sections
715df9b8 13161 || !is_elf_hash_table (info->hash))
c152c796 13162 {
4eca0228 13163 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13164 return TRUE;
13165 }
13166
74f0fb50 13167 bed->gc_keep (info);
da44f4e5 13168 htab = elf_hash_table (info);
74f0fb50 13169
9d0a14d3
RS
13170 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13171 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13172 for (sub = info->input_bfds;
13173 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13174 sub = sub->link.next)
9d0a14d3
RS
13175 {
13176 asection *sec;
13177 struct elf_reloc_cookie cookie;
13178
13179 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13180 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13181 {
13182 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13183 if (elf_section_data (sec)->sec_info
13184 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13185 elf_eh_frame_section (sub) = sec;
13186 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13187 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13188 }
13189 }
9d0a14d3 13190
c152c796 13191 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13192 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13193 if (!ok)
13194 return FALSE;
13195
13196 /* Kill the vtable relocations that were not used. */
da44f4e5 13197 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13198 if (!ok)
13199 return FALSE;
13200
715df9b8 13201 /* Mark dynamically referenced symbols. */
22185505 13202 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13203 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13204
715df9b8 13205 /* Grovel through relocs to find out who stays ... */
64d03ab5 13206 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13207 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13208 {
13209 asection *o;
13210
b19a8f85
L
13211 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13212 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13213 continue;
13214
7f6ab9f8
AM
13215 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13216 Also treat note sections as a root, if the section is not part
13217 of a group. */
c152c796 13218 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13219 if (!o->gc_mark
13220 && (o->flags & SEC_EXCLUDE) == 0
24007750 13221 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13222 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13223 && elf_next_in_group (o) == NULL )))
13224 {
13225 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13226 return FALSE;
13227 }
c152c796
AM
13228 }
13229
6a5bb875 13230 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13231 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13232
c152c796 13233 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13234 return elf_gc_sweep (abfd, info);
c152c796
AM
13235}
13236\f
13237/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13238
13239bfd_boolean
13240bfd_elf_gc_record_vtinherit (bfd *abfd,
13241 asection *sec,
13242 struct elf_link_hash_entry *h,
13243 bfd_vma offset)
13244{
13245 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13246 struct elf_link_hash_entry **search, *child;
ef53be89 13247 size_t extsymcount;
c152c796
AM
13248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13249
13250 /* The sh_info field of the symtab header tells us where the
13251 external symbols start. We don't care about the local symbols at
13252 this point. */
13253 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13254 if (!elf_bad_symtab (abfd))
13255 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13256
13257 sym_hashes = elf_sym_hashes (abfd);
13258 sym_hashes_end = sym_hashes + extsymcount;
13259
13260 /* Hunt down the child symbol, which is in this section at the same
13261 offset as the relocation. */
13262 for (search = sym_hashes; search != sym_hashes_end; ++search)
13263 {
13264 if ((child = *search) != NULL
13265 && (child->root.type == bfd_link_hash_defined
13266 || child->root.type == bfd_link_hash_defweak)
13267 && child->root.u.def.section == sec
13268 && child->root.u.def.value == offset)
13269 goto win;
13270 }
13271
695344c0
NC
13272 /* xgettext:c-format */
13273 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13274 abfd, sec, (unsigned long) offset);
c152c796
AM
13275 bfd_set_error (bfd_error_invalid_operation);
13276 return FALSE;
13277
13278 win:
f6e332e6
AM
13279 if (!child->vtable)
13280 {
ca4be51c
AM
13281 child->vtable = ((struct elf_link_virtual_table_entry *)
13282 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13283 if (!child->vtable)
13284 return FALSE;
13285 }
c152c796
AM
13286 if (!h)
13287 {
13288 /* This *should* only be the absolute section. It could potentially
13289 be that someone has defined a non-global vtable though, which
13290 would be bad. It isn't worth paging in the local symbols to be
13291 sure though; that case should simply be handled by the assembler. */
13292
f6e332e6 13293 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13294 }
13295 else
f6e332e6 13296 child->vtable->parent = h;
c152c796
AM
13297
13298 return TRUE;
13299}
13300
13301/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13302
13303bfd_boolean
13304bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13305 asection *sec ATTRIBUTE_UNUSED,
13306 struct elf_link_hash_entry *h,
13307 bfd_vma addend)
13308{
13309 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13310 unsigned int log_file_align = bed->s->log_file_align;
13311
f6e332e6
AM
13312 if (!h->vtable)
13313 {
ca4be51c
AM
13314 h->vtable = ((struct elf_link_virtual_table_entry *)
13315 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13316 if (!h->vtable)
13317 return FALSE;
13318 }
13319
13320 if (addend >= h->vtable->size)
c152c796
AM
13321 {
13322 size_t size, bytes, file_align;
f6e332e6 13323 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13324
13325 /* While the symbol is undefined, we have to be prepared to handle
13326 a zero size. */
13327 file_align = 1 << log_file_align;
13328 if (h->root.type == bfd_link_hash_undefined)
13329 size = addend + file_align;
13330 else
13331 {
13332 size = h->size;
13333 if (addend >= size)
13334 {
13335 /* Oops! We've got a reference past the defined end of
13336 the table. This is probably a bug -- shall we warn? */
13337 size = addend + file_align;
13338 }
13339 }
13340 size = (size + file_align - 1) & -file_align;
13341
13342 /* Allocate one extra entry for use as a "done" flag for the
13343 consolidation pass. */
13344 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13345
13346 if (ptr)
13347 {
a50b1753 13348 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13349
13350 if (ptr != NULL)
13351 {
13352 size_t oldbytes;
13353
f6e332e6 13354 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13355 * sizeof (bfd_boolean));
13356 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13357 }
13358 }
13359 else
a50b1753 13360 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13361
13362 if (ptr == NULL)
13363 return FALSE;
13364
13365 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13366 h->vtable->used = ptr + 1;
13367 h->vtable->size = size;
c152c796
AM
13368 }
13369
f6e332e6 13370 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13371
13372 return TRUE;
13373}
13374
ae17ab41
CM
13375/* Map an ELF section header flag to its corresponding string. */
13376typedef struct
13377{
13378 char *flag_name;
13379 flagword flag_value;
13380} elf_flags_to_name_table;
13381
13382static elf_flags_to_name_table elf_flags_to_names [] =
13383{
13384 { "SHF_WRITE", SHF_WRITE },
13385 { "SHF_ALLOC", SHF_ALLOC },
13386 { "SHF_EXECINSTR", SHF_EXECINSTR },
13387 { "SHF_MERGE", SHF_MERGE },
13388 { "SHF_STRINGS", SHF_STRINGS },
13389 { "SHF_INFO_LINK", SHF_INFO_LINK},
13390 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13391 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13392 { "SHF_GROUP", SHF_GROUP },
13393 { "SHF_TLS", SHF_TLS },
13394 { "SHF_MASKOS", SHF_MASKOS },
13395 { "SHF_EXCLUDE", SHF_EXCLUDE },
13396};
13397
b9c361e0
JL
13398/* Returns TRUE if the section is to be included, otherwise FALSE. */
13399bfd_boolean
ae17ab41 13400bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13401 struct flag_info *flaginfo,
b9c361e0 13402 asection *section)
ae17ab41 13403{
8b127cbc 13404 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13405
8b127cbc 13406 if (!flaginfo->flags_initialized)
ae17ab41 13407 {
8b127cbc
AM
13408 bfd *obfd = info->output_bfd;
13409 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13410 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13411 int with_hex = 0;
13412 int without_hex = 0;
13413
8b127cbc 13414 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13415 {
b9c361e0 13416 unsigned i;
8b127cbc 13417 flagword (*lookup) (char *);
ae17ab41 13418
8b127cbc
AM
13419 lookup = bed->elf_backend_lookup_section_flags_hook;
13420 if (lookup != NULL)
ae17ab41 13421 {
8b127cbc 13422 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13423
13424 if (hexval != 0)
13425 {
13426 if (tf->with == with_flags)
13427 with_hex |= hexval;
13428 else if (tf->with == without_flags)
13429 without_hex |= hexval;
13430 tf->valid = TRUE;
13431 continue;
13432 }
ae17ab41 13433 }
8b127cbc 13434 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13435 {
8b127cbc 13436 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13437 {
13438 if (tf->with == with_flags)
13439 with_hex |= elf_flags_to_names[i].flag_value;
13440 else if (tf->with == without_flags)
13441 without_hex |= elf_flags_to_names[i].flag_value;
13442 tf->valid = TRUE;
13443 break;
13444 }
13445 }
8b127cbc 13446 if (!tf->valid)
b9c361e0 13447 {
68ffbac6 13448 info->callbacks->einfo
8b127cbc 13449 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13450 return FALSE;
ae17ab41
CM
13451 }
13452 }
8b127cbc
AM
13453 flaginfo->flags_initialized = TRUE;
13454 flaginfo->only_with_flags |= with_hex;
13455 flaginfo->not_with_flags |= without_hex;
ae17ab41 13456 }
ae17ab41 13457
8b127cbc 13458 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13459 return FALSE;
13460
8b127cbc 13461 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13462 return FALSE;
13463
13464 return TRUE;
ae17ab41
CM
13465}
13466
c152c796
AM
13467struct alloc_got_off_arg {
13468 bfd_vma gotoff;
10455f89 13469 struct bfd_link_info *info;
c152c796
AM
13470};
13471
13472/* We need a special top-level link routine to convert got reference counts
13473 to real got offsets. */
13474
13475static bfd_boolean
13476elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13477{
a50b1753 13478 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13479 bfd *obfd = gofarg->info->output_bfd;
13480 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13481
c152c796
AM
13482 if (h->got.refcount > 0)
13483 {
13484 h->got.offset = gofarg->gotoff;
10455f89 13485 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13486 }
13487 else
13488 h->got.offset = (bfd_vma) -1;
13489
13490 return TRUE;
13491}
13492
13493/* And an accompanying bit to work out final got entry offsets once
13494 we're done. Should be called from final_link. */
13495
13496bfd_boolean
13497bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13498 struct bfd_link_info *info)
13499{
13500 bfd *i;
13501 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13502 bfd_vma gotoff;
c152c796
AM
13503 struct alloc_got_off_arg gofarg;
13504
10455f89
HPN
13505 BFD_ASSERT (abfd == info->output_bfd);
13506
c152c796
AM
13507 if (! is_elf_hash_table (info->hash))
13508 return FALSE;
13509
13510 /* The GOT offset is relative to the .got section, but the GOT header is
13511 put into the .got.plt section, if the backend uses it. */
13512 if (bed->want_got_plt)
13513 gotoff = 0;
13514 else
13515 gotoff = bed->got_header_size;
13516
13517 /* Do the local .got entries first. */
c72f2fb2 13518 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13519 {
13520 bfd_signed_vma *local_got;
ef53be89 13521 size_t j, locsymcount;
c152c796
AM
13522 Elf_Internal_Shdr *symtab_hdr;
13523
13524 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13525 continue;
13526
13527 local_got = elf_local_got_refcounts (i);
13528 if (!local_got)
13529 continue;
13530
13531 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13532 if (elf_bad_symtab (i))
13533 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13534 else
13535 locsymcount = symtab_hdr->sh_info;
13536
13537 for (j = 0; j < locsymcount; ++j)
13538 {
13539 if (local_got[j] > 0)
13540 {
13541 local_got[j] = gotoff;
10455f89 13542 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13543 }
13544 else
13545 local_got[j] = (bfd_vma) -1;
13546 }
13547 }
13548
13549 /* Then the global .got entries. .plt refcounts are handled by
13550 adjust_dynamic_symbol */
13551 gofarg.gotoff = gotoff;
10455f89 13552 gofarg.info = info;
c152c796
AM
13553 elf_link_hash_traverse (elf_hash_table (info),
13554 elf_gc_allocate_got_offsets,
13555 &gofarg);
13556 return TRUE;
13557}
13558
13559/* Many folk need no more in the way of final link than this, once
13560 got entry reference counting is enabled. */
13561
13562bfd_boolean
13563bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13564{
13565 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13566 return FALSE;
13567
13568 /* Invoke the regular ELF backend linker to do all the work. */
13569 return bfd_elf_final_link (abfd, info);
13570}
13571
13572bfd_boolean
13573bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13574{
a50b1753 13575 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13576
13577 if (rcookie->bad_symtab)
13578 rcookie->rel = rcookie->rels;
13579
13580 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13581 {
13582 unsigned long r_symndx;
13583
13584 if (! rcookie->bad_symtab)
13585 if (rcookie->rel->r_offset > offset)
13586 return FALSE;
13587 if (rcookie->rel->r_offset != offset)
13588 continue;
13589
13590 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13591 if (r_symndx == STN_UNDEF)
c152c796
AM
13592 return TRUE;
13593
13594 if (r_symndx >= rcookie->locsymcount
13595 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13596 {
13597 struct elf_link_hash_entry *h;
13598
13599 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13600
13601 while (h->root.type == bfd_link_hash_indirect
13602 || h->root.type == bfd_link_hash_warning)
13603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13604
13605 if ((h->root.type == bfd_link_hash_defined
13606 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13607 && (h->root.u.def.section->owner != rcookie->abfd
13608 || h->root.u.def.section->kept_section != NULL
13609 || discarded_section (h->root.u.def.section)))
c152c796 13610 return TRUE;
c152c796
AM
13611 }
13612 else
13613 {
13614 /* It's not a relocation against a global symbol,
13615 but it could be a relocation against a local
13616 symbol for a discarded section. */
13617 asection *isec;
13618 Elf_Internal_Sym *isym;
13619
13620 /* Need to: get the symbol; get the section. */
13621 isym = &rcookie->locsyms[r_symndx];
cb33740c 13622 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13623 if (isec != NULL
13624 && (isec->kept_section != NULL
13625 || discarded_section (isec)))
cb33740c 13626 return TRUE;
c152c796
AM
13627 }
13628 return FALSE;
13629 }
13630 return FALSE;
13631}
13632
13633/* Discard unneeded references to discarded sections.
75938853
AM
13634 Returns -1 on error, 1 if any section's size was changed, 0 if
13635 nothing changed. This function assumes that the relocations are in
13636 sorted order, which is true for all known assemblers. */
c152c796 13637
75938853 13638int
c152c796
AM
13639bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13640{
13641 struct elf_reloc_cookie cookie;
18cd5bce 13642 asection *o;
c152c796 13643 bfd *abfd;
75938853 13644 int changed = 0;
c152c796
AM
13645
13646 if (info->traditional_format
13647 || !is_elf_hash_table (info->hash))
75938853 13648 return 0;
c152c796 13649
18cd5bce
AM
13650 o = bfd_get_section_by_name (output_bfd, ".stab");
13651 if (o != NULL)
c152c796 13652 {
18cd5bce 13653 asection *i;
c152c796 13654
18cd5bce 13655 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13656 {
18cd5bce
AM
13657 if (i->size == 0
13658 || i->reloc_count == 0
13659 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13660 continue;
c152c796 13661
18cd5bce
AM
13662 abfd = i->owner;
13663 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13664 continue;
c152c796 13665
18cd5bce 13666 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13667 return -1;
c152c796 13668
18cd5bce
AM
13669 if (_bfd_discard_section_stabs (abfd, i,
13670 elf_section_data (i)->sec_info,
5241d853
RS
13671 bfd_elf_reloc_symbol_deleted_p,
13672 &cookie))
75938853 13673 changed = 1;
18cd5bce
AM
13674
13675 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13676 }
18cd5bce
AM
13677 }
13678
2f0c68f2
CM
13679 o = NULL;
13680 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13681 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13682 if (o != NULL)
13683 {
13684 asection *i;
c152c796 13685
18cd5bce 13686 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13687 {
18cd5bce
AM
13688 if (i->size == 0)
13689 continue;
13690
13691 abfd = i->owner;
13692 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13693 continue;
13694
13695 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13696 return -1;
18cd5bce
AM
13697
13698 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13699 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13700 bfd_elf_reloc_symbol_deleted_p,
13701 &cookie))
75938853 13702 changed = 1;
18cd5bce
AM
13703
13704 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13705 }
18cd5bce 13706 }
c152c796 13707
18cd5bce
AM
13708 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13709 {
13710 const struct elf_backend_data *bed;
c152c796 13711
18cd5bce
AM
13712 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13713 continue;
13714
13715 bed = get_elf_backend_data (abfd);
13716
13717 if (bed->elf_backend_discard_info != NULL)
13718 {
13719 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13720 return -1;
18cd5bce
AM
13721
13722 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13723 changed = 1;
18cd5bce
AM
13724
13725 fini_reloc_cookie (&cookie, abfd);
13726 }
c152c796
AM
13727 }
13728
2f0c68f2
CM
13729 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13730 _bfd_elf_end_eh_frame_parsing (info);
13731
13732 if (info->eh_frame_hdr_type
0e1862bb 13733 && !bfd_link_relocatable (info)
c152c796 13734 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13735 changed = 1;
c152c796 13736
75938853 13737 return changed;
c152c796 13738}
082b7297 13739
43e1669b 13740bfd_boolean
0c511000 13741_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13742 asection *sec,
c0f00686 13743 struct bfd_link_info *info)
082b7297
L
13744{
13745 flagword flags;
c77ec726 13746 const char *name, *key;
082b7297
L
13747 struct bfd_section_already_linked *l;
13748 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13749
c77ec726
AM
13750 if (sec->output_section == bfd_abs_section_ptr)
13751 return FALSE;
0c511000 13752
c77ec726 13753 flags = sec->flags;
0c511000 13754
c77ec726
AM
13755 /* Return if it isn't a linkonce section. A comdat group section
13756 also has SEC_LINK_ONCE set. */
13757 if ((flags & SEC_LINK_ONCE) == 0)
13758 return FALSE;
0c511000 13759
c77ec726
AM
13760 /* Don't put group member sections on our list of already linked
13761 sections. They are handled as a group via their group section. */
13762 if (elf_sec_group (sec) != NULL)
13763 return FALSE;
0c511000 13764
c77ec726
AM
13765 /* For a SHT_GROUP section, use the group signature as the key. */
13766 name = sec->name;
13767 if ((flags & SEC_GROUP) != 0
13768 && elf_next_in_group (sec) != NULL
13769 && elf_group_name (elf_next_in_group (sec)) != NULL)
13770 key = elf_group_name (elf_next_in_group (sec));
13771 else
13772 {
13773 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13774 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13775 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13776 key++;
0c511000 13777 else
c77ec726
AM
13778 /* Must be a user linkonce section that doesn't follow gcc's
13779 naming convention. In this case we won't be matching
13780 single member groups. */
13781 key = name;
0c511000 13782 }
6d2cd210 13783
c77ec726 13784 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13785
13786 for (l = already_linked_list->entry; l != NULL; l = l->next)
13787 {
c2370991 13788 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13789 sections with a signature of <key> (<key> is some string),
13790 and linkonce sections named .gnu.linkonce.<type>.<key>.
13791 Match like sections. LTO plugin sections are an exception.
13792 They are always named .gnu.linkonce.t.<key> and match either
13793 type of section. */
13794 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13795 && ((flags & SEC_GROUP) != 0
13796 || strcmp (name, l->sec->name) == 0))
13797 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13798 {
13799 /* The section has already been linked. See if we should
6d2cd210 13800 issue a warning. */
c77ec726
AM
13801 if (!_bfd_handle_already_linked (sec, l, info))
13802 return FALSE;
082b7297 13803
c77ec726 13804 if (flags & SEC_GROUP)
3d7f7666 13805 {
c77ec726
AM
13806 asection *first = elf_next_in_group (sec);
13807 asection *s = first;
3d7f7666 13808
c77ec726 13809 while (s != NULL)
3d7f7666 13810 {
c77ec726
AM
13811 s->output_section = bfd_abs_section_ptr;
13812 /* Record which group discards it. */
13813 s->kept_section = l->sec;
13814 s = elf_next_in_group (s);
13815 /* These lists are circular. */
13816 if (s == first)
13817 break;
3d7f7666
L
13818 }
13819 }
082b7297 13820
43e1669b 13821 return TRUE;
082b7297
L
13822 }
13823 }
13824
c77ec726
AM
13825 /* A single member comdat group section may be discarded by a
13826 linkonce section and vice versa. */
13827 if ((flags & SEC_GROUP) != 0)
3d7f7666 13828 {
c77ec726 13829 asection *first = elf_next_in_group (sec);
c2370991 13830
c77ec726
AM
13831 if (first != NULL && elf_next_in_group (first) == first)
13832 /* Check this single member group against linkonce sections. */
13833 for (l = already_linked_list->entry; l != NULL; l = l->next)
13834 if ((l->sec->flags & SEC_GROUP) == 0
13835 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13836 {
13837 first->output_section = bfd_abs_section_ptr;
13838 first->kept_section = l->sec;
13839 sec->output_section = bfd_abs_section_ptr;
13840 break;
13841 }
13842 }
13843 else
13844 /* Check this linkonce section against single member groups. */
13845 for (l = already_linked_list->entry; l != NULL; l = l->next)
13846 if (l->sec->flags & SEC_GROUP)
6d2cd210 13847 {
c77ec726 13848 asection *first = elf_next_in_group (l->sec);
6d2cd210 13849
c77ec726
AM
13850 if (first != NULL
13851 && elf_next_in_group (first) == first
13852 && bfd_elf_match_symbols_in_sections (first, sec, info))
13853 {
13854 sec->output_section = bfd_abs_section_ptr;
13855 sec->kept_section = first;
13856 break;
13857 }
6d2cd210 13858 }
0c511000 13859
c77ec726
AM
13860 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13861 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13862 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13863 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13864 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13865 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13866 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13867 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13868 The reverse order cannot happen as there is never a bfd with only the
13869 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13870 matter as here were are looking only for cross-bfd sections. */
13871
13872 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13873 for (l = already_linked_list->entry; l != NULL; l = l->next)
13874 if ((l->sec->flags & SEC_GROUP) == 0
13875 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13876 {
13877 if (abfd != l->sec->owner)
13878 sec->output_section = bfd_abs_section_ptr;
13879 break;
13880 }
80c29487 13881
082b7297 13882 /* This is the first section with this name. Record it. */
c77ec726 13883 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13884 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13885 return sec->output_section == bfd_abs_section_ptr;
082b7297 13886}
81e1b023 13887
a4d8e49b
L
13888bfd_boolean
13889_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13890{
13891 return sym->st_shndx == SHN_COMMON;
13892}
13893
13894unsigned int
13895_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13896{
13897 return SHN_COMMON;
13898}
13899
13900asection *
13901_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13902{
13903 return bfd_com_section_ptr;
13904}
10455f89
HPN
13905
13906bfd_vma
13907_bfd_elf_default_got_elt_size (bfd *abfd,
13908 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13909 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13910 bfd *ibfd ATTRIBUTE_UNUSED,
13911 unsigned long symndx ATTRIBUTE_UNUSED)
13912{
13913 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13914 return bed->s->arch_size / 8;
13915}
83bac4b0
NC
13916
13917/* Routines to support the creation of dynamic relocs. */
13918
83bac4b0
NC
13919/* Returns the name of the dynamic reloc section associated with SEC. */
13920
13921static const char *
13922get_dynamic_reloc_section_name (bfd * abfd,
13923 asection * sec,
13924 bfd_boolean is_rela)
13925{
ddcf1fcf
BS
13926 char *name;
13927 const char *old_name = bfd_get_section_name (NULL, sec);
13928 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13929
ddcf1fcf 13930 if (old_name == NULL)
83bac4b0
NC
13931 return NULL;
13932
ddcf1fcf 13933 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13934 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13935
13936 return name;
13937}
13938
13939/* Returns the dynamic reloc section associated with SEC.
13940 If necessary compute the name of the dynamic reloc section based
13941 on SEC's name (looked up in ABFD's string table) and the setting
13942 of IS_RELA. */
13943
13944asection *
13945_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13946 asection * sec,
13947 bfd_boolean is_rela)
13948{
13949 asection * reloc_sec = elf_section_data (sec)->sreloc;
13950
13951 if (reloc_sec == NULL)
13952 {
13953 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13954
13955 if (name != NULL)
13956 {
3d4d4302 13957 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13958
13959 if (reloc_sec != NULL)
13960 elf_section_data (sec)->sreloc = reloc_sec;
13961 }
13962 }
13963
13964 return reloc_sec;
13965}
13966
13967/* Returns the dynamic reloc section associated with SEC. If the
13968 section does not exist it is created and attached to the DYNOBJ
13969 bfd and stored in the SRELOC field of SEC's elf_section_data
13970 structure.
f8076f98 13971
83bac4b0
NC
13972 ALIGNMENT is the alignment for the newly created section and
13973 IS_RELA defines whether the name should be .rela.<SEC's name>
13974 or .rel.<SEC's name>. The section name is looked up in the
13975 string table associated with ABFD. */
13976
13977asection *
ca4be51c
AM
13978_bfd_elf_make_dynamic_reloc_section (asection *sec,
13979 bfd *dynobj,
13980 unsigned int alignment,
13981 bfd *abfd,
13982 bfd_boolean is_rela)
83bac4b0
NC
13983{
13984 asection * reloc_sec = elf_section_data (sec)->sreloc;
13985
13986 if (reloc_sec == NULL)
13987 {
13988 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13989
13990 if (name == NULL)
13991 return NULL;
13992
3d4d4302 13993 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13994
13995 if (reloc_sec == NULL)
13996 {
3d4d4302
AM
13997 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13998 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13999 if ((sec->flags & SEC_ALLOC) != 0)
14000 flags |= SEC_ALLOC | SEC_LOAD;
14001
3d4d4302 14002 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14003 if (reloc_sec != NULL)
14004 {
8877b5e5
AM
14005 /* _bfd_elf_get_sec_type_attr chooses a section type by
14006 name. Override as it may be wrong, eg. for a user
14007 section named "auto" we'll get ".relauto" which is
14008 seen to be a .rela section. */
14009 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14010 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14011 reloc_sec = NULL;
14012 }
14013 }
14014
14015 elf_section_data (sec)->sreloc = reloc_sec;
14016 }
14017
14018 return reloc_sec;
14019}
1338dd10 14020
bffebb6b
AM
14021/* Copy the ELF symbol type and other attributes for a linker script
14022 assignment from HSRC to HDEST. Generally this should be treated as
14023 if we found a strong non-dynamic definition for HDEST (except that
14024 ld ignores multiple definition errors). */
1338dd10 14025void
bffebb6b
AM
14026_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14027 struct bfd_link_hash_entry *hdest,
14028 struct bfd_link_hash_entry *hsrc)
1338dd10 14029{
bffebb6b
AM
14030 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14031 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14032 Elf_Internal_Sym isym;
1338dd10
PB
14033
14034 ehdest->type = ehsrc->type;
35fc36a8 14035 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14036
14037 isym.st_other = ehsrc->other;
b8417128 14038 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14039}
351f65ca
L
14040
14041/* Append a RELA relocation REL to section S in BFD. */
14042
14043void
14044elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14045{
14046 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14047 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14048 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14049 bed->s->swap_reloca_out (abfd, rel, loc);
14050}
14051
14052/* Append a REL relocation REL to section S in BFD. */
14053
14054void
14055elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14056{
14057 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14058 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14059 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14060 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14061}
This page took 2.651859 seconds and 4 git commands to generate.