Don't add DT_NEEDED for unmatched symbol
[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"
252b5132 31
28caa186
AM
32/* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35struct elf_info_failed
36{
37 struct bfd_link_info *info;
28caa186
AM
38 bfd_boolean failed;
39};
40
41/* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44struct elf_find_verdep_info
45{
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52};
53
54static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
2f0c68f2
CM
57asection *
58_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61{
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96}
97
d98685ac
AM
98/* Define a symbol in a dynamic linkage section. */
99
100struct elf_link_hash_entry *
101_bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105{
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
ccabcbe5 108 const struct elf_backend_data *bed;
d98685ac
AM
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
cf18fda4 121 bed = get_elf_backend_data (abfd);
d98685ac 122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 123 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
e28df02b 128 h->non_elf = 0;
12b2843a 129 h->root.linker_def = 1;
d98685ac 130 h->type = STT_OBJECT;
00b7642b
AM
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 133
ccabcbe5 134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
135 return h;
136}
137
b34976b6 138bfd_boolean
268b6b39 139_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
140{
141 flagword flags;
aad5d350 142 asection *s;
252b5132 143 struct elf_link_hash_entry *h;
9c5bfbb7 144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 145 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
146
147 /* This function may be called more than once. */
3d4d4302
AM
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
b34976b6 150 return TRUE;
252b5132 151
e5a52504 152 flags = bed->dynamic_sec_flags;
252b5132 153
14b2f831
AM
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
6de2ae4a
L
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
252b5132 163
14b2f831 164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
252b5132
RH
170 if (bed->want_got_plt)
171 {
14b2f831 172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 173 if (s == NULL
6de2ae4a
L
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
b34976b6 176 return FALSE;
6de2ae4a 177 htab->sgotplt = s;
252b5132
RH
178 }
179
64e77c6d
L
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
2517a57f
AM
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
6de2ae4a
L
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
2517a57f 191 elf_hash_table (info)->hgot = h;
d98685ac
AM
192 if (h == NULL)
193 return FALSE;
2517a57f 194 }
252b5132 195
b34976b6 196 return TRUE;
252b5132
RH
197}
198\f
7e9f0867
AM
199/* Create a strtab to hold the dynamic symbol names. */
200static bfd_boolean
201_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202{
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 hash_table->dynobj = abfd;
208
209 if (hash_table->dynstr == NULL)
210 {
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
213 return FALSE;
214 }
215 return TRUE;
216}
217
45d6a902
AM
218/* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
252b5132 224
b34976b6 225bfd_boolean
268b6b39 226_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 227{
45d6a902 228 flagword flags;
91d6fa6a 229 asection *s;
9c5bfbb7 230 const struct elf_backend_data *bed;
9637f6ef 231 struct elf_link_hash_entry *h;
252b5132 232
0eddce27 233 if (! is_elf_hash_table (info->hash))
45d6a902
AM
234 return FALSE;
235
236 if (elf_hash_table (info)->dynamic_sections_created)
237 return TRUE;
238
7e9f0867
AM
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240 return FALSE;
45d6a902 241
7e9f0867 242 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
243 bed = get_elf_backend_data (abfd);
244
245 flags = bed->dynamic_sec_flags;
45d6a902
AM
246
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
9b8b325a 249 if (bfd_link_executable (info) && !info->nointerp)
252b5132 250 {
14b2f831
AM
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
3496cb2a 253 if (s == NULL)
45d6a902
AM
254 return FALSE;
255 }
bb0deeff 256
45d6a902
AM
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
14b2f831
AM
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
45d6a902 261 if (s == NULL
45d6a902
AM
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263 return FALSE;
264
14b2f831
AM
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
45d6a902 267 if (s == NULL
45d6a902
AM
268 || ! bfd_set_section_alignment (abfd, s, 1))
269 return FALSE;
270
14b2f831
AM
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
45d6a902 273 if (s == NULL
45d6a902
AM
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275 return FALSE;
276
14b2f831
AM
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
45d6a902 279 if (s == NULL
45d6a902
AM
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281 return FALSE;
cae1fbbb 282 elf_hash_table (info)->dynsym = s;
45d6a902 283
14b2f831
AM
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
3496cb2a 286 if (s == NULL)
45d6a902
AM
287 return FALSE;
288
14b2f831 289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
294 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
9637f6ef
L
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
302 if (h == NULL)
45d6a902
AM
303 return FALSE;
304
fdc90cb4
JJ
305 if (info->emit_hash)
306 {
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
fdc90cb4
JJ
309 if (s == NULL
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 return FALSE;
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313 }
314
315 if (info->emit_gnu_hash)
316 {
14b2f831
AM
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
fdc90cb4
JJ
319 if (s == NULL
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 return FALSE;
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
327 else
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
329 }
45d6a902
AM
330
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
894891db
NC
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
336 return FALSE;
337
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
339
340 return TRUE;
341}
342
343/* Create dynamic sections when linking against a dynamic object. */
344
345bfd_boolean
268b6b39 346_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
347{
348 flagword flags, pltflags;
7325306f 349 struct elf_link_hash_entry *h;
45d6a902 350 asection *s;
9c5bfbb7 351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 352 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 353
252b5132
RH
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
e5a52504 356 flags = bed->dynamic_sec_flags;
252b5132
RH
357
358 pltflags = flags;
252b5132 359 if (bed->plt_not_loaded)
6df4d94c
MM
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
5d1634d7 363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
364 else
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
368
14b2f831 369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 370 if (s == NULL
252b5132 371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 372 return FALSE;
6de2ae4a 373 htab->splt = s;
252b5132 374
d98685ac
AM
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376 .plt section. */
7325306f
RS
377 if (bed->want_plt_sym)
378 {
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
382 if (h == NULL)
383 return FALSE;
384 }
252b5132 385
14b2f831
AM
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
252b5132 390 if (s == NULL
45d6a902 391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 392 return FALSE;
6de2ae4a 393 htab->srelplt = s;
252b5132
RH
394
395 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 396 return FALSE;
252b5132 397
3018b441
RH
398 if (bed->want_dynbss)
399 {
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
14b2f831
AM
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 408 if (s == NULL)
b34976b6 409 return FALSE;
252b5132 410
3018b441 411 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
421 copy relocs. */
0e1862bb 422 if (! bfd_link_pic (info))
3018b441 423 {
14b2f831
AM
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
3018b441 428 if (s == NULL
45d6a902 429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 430 return FALSE;
3018b441 431 }
252b5132
RH
432 }
433
b34976b6 434 return TRUE;
252b5132
RH
435}
436\f
252b5132
RH
437/* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
443 one. */
444
b34976b6 445bfd_boolean
c152c796
AM
446bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
252b5132
RH
448{
449 if (h->dynindx == -1)
450 {
2b0f7ef9 451 struct elf_strtab_hash *dynstr;
68b6ddd0 452 char *p;
252b5132 453 const char *name;
252b5132
RH
454 bfd_size_type indx;
455
7a13edea
NC
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
461 {
462 case STV_INTERNAL:
463 case STV_HIDDEN:
9d6eee78
L
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
38048eb9 466 {
f5385ebf 467 h->forced_local = 1;
67687978
PB
468 if (!elf_hash_table (info)->is_relocatable_executable)
469 return TRUE;
7a13edea 470 }
0444bdd4 471
7a13edea
NC
472 default:
473 break;
474 }
475
252b5132
RH
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
478
479 dynstr = elf_hash_table (info)->dynstr;
480 if (dynstr == NULL)
481 {
482 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 484 if (dynstr == NULL)
b34976b6 485 return FALSE;
252b5132
RH
486 }
487
488 /* We don't put any version information in the dynamic string
aad5d350 489 table. */
252b5132
RH
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
492 if (p != NULL)
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
498 *p = 0;
499
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501
502 if (p != NULL)
503 *p = ELF_VER_CHR;
252b5132
RH
504
505 if (indx == (bfd_size_type) -1)
b34976b6 506 return FALSE;
252b5132
RH
507 h->dynstr_index = indx;
508 }
509
b34976b6 510 return TRUE;
252b5132 511}
45d6a902 512\f
55255dae
L
513/* Mark a symbol dynamic. */
514
28caa186 515static void
55255dae 516bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
55255dae 519{
40b36307 520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 521
40b36307 522 /* It may be called more than once on the same H. */
0e1862bb 523 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
524 return;
525
40b36307
L
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
528 || (sym != NULL
529 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 530 || (d != NULL
40b36307
L
531 && h->root.type == bfd_link_hash_new
532 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
533 h->dynamic = 1;
534}
535
45d6a902
AM
536/* Record an assignment to a symbol made by a linker script. We need
537 this in case some dynamic object refers to this symbol. */
538
539bfd_boolean
fe21a8fc
L
540bfd_elf_record_link_assignment (bfd *output_bfd,
541 struct bfd_link_info *info,
268b6b39 542 const char *name,
fe21a8fc
L
543 bfd_boolean provide,
544 bfd_boolean hidden)
45d6a902 545{
00cbee0a 546 struct elf_link_hash_entry *h, *hv;
4ea42fb7 547 struct elf_link_hash_table *htab;
00cbee0a 548 const struct elf_backend_data *bed;
45d6a902 549
0eddce27 550 if (!is_elf_hash_table (info->hash))
45d6a902
AM
551 return TRUE;
552
4ea42fb7
AM
553 htab = elf_hash_table (info);
554 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 555 if (h == NULL)
4ea42fb7 556 return provide;
45d6a902 557
00cbee0a 558 switch (h->root.type)
77cfaee6 559 {
00cbee0a
L
560 case bfd_link_hash_defined:
561 case bfd_link_hash_defweak:
562 case bfd_link_hash_common:
563 break;
564 case bfd_link_hash_undefweak:
565 case bfd_link_hash_undefined:
566 /* Since we're defining the symbol, don't let it seem to have not
567 been defined. record_dynamic_symbol and size_dynamic_sections
568 may depend on this. */
4ea42fb7 569 h->root.type = bfd_link_hash_new;
77cfaee6
AM
570 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
571 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
572 break;
573 case bfd_link_hash_new:
40b36307 574 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 575 h->non_elf = 0;
00cbee0a
L
576 break;
577 case bfd_link_hash_indirect:
578 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 579 the versioned symbol point to this one. */
00cbee0a
L
580 bed = get_elf_backend_data (output_bfd);
581 hv = h;
582 while (hv->root.type == bfd_link_hash_indirect
583 || hv->root.type == bfd_link_hash_warning)
584 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
585 /* We don't need to update h->root.u since linker will set them
586 later. */
587 h->root.type = bfd_link_hash_undefined;
588 hv->root.type = bfd_link_hash_indirect;
589 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
590 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
591 break;
592 case bfd_link_hash_warning:
593 abort ();
594 break;
55255dae 595 }
45d6a902
AM
596
597 /* If this symbol is being provided by the linker script, and it is
598 currently defined by a dynamic object, but not by a regular
599 object, then mark it as undefined so that the generic linker will
600 force the correct value. */
601 if (provide
f5385ebf
AM
602 && h->def_dynamic
603 && !h->def_regular)
45d6a902
AM
604 h->root.type = bfd_link_hash_undefined;
605
606 /* If this symbol is not being provided by the linker script, and it is
607 currently defined by a dynamic object, but not by a regular object,
608 then clear out any version information because the symbol will not be
609 associated with the dynamic object any more. */
610 if (!provide
f5385ebf
AM
611 && h->def_dynamic
612 && !h->def_regular)
45d6a902
AM
613 h->verinfo.verdef = NULL;
614
f5385ebf 615 h->def_regular = 1;
45d6a902 616
eb8476a6 617 if (hidden)
fe21a8fc 618 {
91d6fa6a 619 bed = get_elf_backend_data (output_bfd);
b8297068
AM
620 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
621 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
622 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
623 }
624
6fa3860b
PB
625 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
626 and executables. */
0e1862bb 627 if (!bfd_link_relocatable (info)
6fa3860b
PB
628 && h->dynindx != -1
629 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
630 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
631 h->forced_local = 1;
632
f5385ebf
AM
633 if ((h->def_dynamic
634 || h->ref_dynamic
0e1862bb 635 || bfd_link_pic (info)
3cbc1e5e 636 || (bfd_link_pde (info)
0e1862bb 637 && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
638 && h->dynindx == -1)
639 {
c152c796 640 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
641 return FALSE;
642
643 /* If this is a weak defined symbol, and we know a corresponding
644 real symbol from the same dynamic object, make sure the real
645 symbol is also made into a dynamic symbol. */
f6e332e6
AM
646 if (h->u.weakdef != NULL
647 && h->u.weakdef->dynindx == -1)
45d6a902 648 {
f6e332e6 649 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
650 return FALSE;
651 }
652 }
653
654 return TRUE;
655}
42751cf3 656
8c58d23b
AM
657/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
658 success, and 2 on a failure caused by attempting to record a symbol
659 in a discarded section, eg. a discarded link-once section symbol. */
660
661int
c152c796
AM
662bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
663 bfd *input_bfd,
664 long input_indx)
8c58d23b
AM
665{
666 bfd_size_type amt;
667 struct elf_link_local_dynamic_entry *entry;
668 struct elf_link_hash_table *eht;
669 struct elf_strtab_hash *dynstr;
670 unsigned long dynstr_index;
671 char *name;
672 Elf_External_Sym_Shndx eshndx;
673 char esym[sizeof (Elf64_External_Sym)];
674
0eddce27 675 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
676 return 0;
677
678 /* See if the entry exists already. */
679 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
680 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
681 return 1;
682
683 amt = sizeof (*entry);
a50b1753 684 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
685 if (entry == NULL)
686 return 0;
687
688 /* Go find the symbol, so that we can find it's name. */
689 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 690 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
691 {
692 bfd_release (input_bfd, entry);
693 return 0;
694 }
695
696 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 697 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
698 {
699 asection *s;
700
701 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
702 if (s == NULL || bfd_is_abs_section (s->output_section))
703 {
704 /* We can still bfd_release here as nothing has done another
705 bfd_alloc. We can't do this later in this function. */
706 bfd_release (input_bfd, entry);
707 return 2;
708 }
709 }
710
711 name = (bfd_elf_string_from_elf_section
712 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
713 entry->isym.st_name));
714
715 dynstr = elf_hash_table (info)->dynstr;
716 if (dynstr == NULL)
717 {
718 /* Create a strtab to hold the dynamic symbol names. */
719 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
720 if (dynstr == NULL)
721 return 0;
722 }
723
b34976b6 724 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
725 if (dynstr_index == (unsigned long) -1)
726 return 0;
727 entry->isym.st_name = dynstr_index;
728
729 eht = elf_hash_table (info);
730
731 entry->next = eht->dynlocal;
732 eht->dynlocal = entry;
733 entry->input_bfd = input_bfd;
734 entry->input_indx = input_indx;
735 eht->dynsymcount++;
736
737 /* Whatever binding the symbol had before, it's now local. */
738 entry->isym.st_info
739 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
740
741 /* The dynindx will be set at the end of size_dynamic_sections. */
742
743 return 1;
744}
745
30b30c21 746/* Return the dynindex of a local dynamic symbol. */
42751cf3 747
30b30c21 748long
268b6b39
AM
749_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
750 bfd *input_bfd,
751 long input_indx)
30b30c21
RH
752{
753 struct elf_link_local_dynamic_entry *e;
754
755 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
756 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
757 return e->dynindx;
758 return -1;
759}
760
761/* This function is used to renumber the dynamic symbols, if some of
762 them are removed because they are marked as local. This is called
763 via elf_link_hash_traverse. */
764
b34976b6 765static bfd_boolean
268b6b39
AM
766elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
767 void *data)
42751cf3 768{
a50b1753 769 size_t *count = (size_t *) data;
30b30c21 770
6fa3860b
PB
771 if (h->forced_local)
772 return TRUE;
773
774 if (h->dynindx != -1)
775 h->dynindx = ++(*count);
776
777 return TRUE;
778}
779
780
781/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
782 STB_LOCAL binding. */
783
784static bfd_boolean
785elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
786 void *data)
787{
a50b1753 788 size_t *count = (size_t *) data;
6fa3860b 789
6fa3860b
PB
790 if (!h->forced_local)
791 return TRUE;
792
42751cf3 793 if (h->dynindx != -1)
30b30c21
RH
794 h->dynindx = ++(*count);
795
b34976b6 796 return TRUE;
42751cf3 797}
30b30c21 798
aee6f5b4
AO
799/* Return true if the dynamic symbol for a given section should be
800 omitted when creating a shared library. */
801bfd_boolean
802_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
803 struct bfd_link_info *info,
804 asection *p)
805{
74541ad4 806 struct elf_link_hash_table *htab;
ca55926c 807 asection *ip;
74541ad4 808
aee6f5b4
AO
809 switch (elf_section_data (p)->this_hdr.sh_type)
810 {
811 case SHT_PROGBITS:
812 case SHT_NOBITS:
813 /* If sh_type is yet undecided, assume it could be
814 SHT_PROGBITS/SHT_NOBITS. */
815 case SHT_NULL:
74541ad4
AM
816 htab = elf_hash_table (info);
817 if (p == htab->tls_sec)
818 return FALSE;
819
820 if (htab->text_index_section != NULL)
821 return p != htab->text_index_section && p != htab->data_index_section;
822
ca55926c 823 return (htab->dynobj != NULL
3d4d4302 824 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 825 && ip->output_section == p);
aee6f5b4
AO
826
827 /* There shouldn't be section relative relocations
828 against any other section. */
829 default:
830 return TRUE;
831 }
832}
833
062e2358 834/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
835 symbol for each output section, which come first. Next come symbols
836 which have been forced to local binding. Then all of the back-end
837 allocated local dynamic syms, followed by the rest of the global
838 symbols. */
30b30c21 839
554220db
AM
840static unsigned long
841_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
842 struct bfd_link_info *info,
843 unsigned long *section_sym_count)
30b30c21
RH
844{
845 unsigned long dynsymcount = 0;
846
0e1862bb
L
847 if (bfd_link_pic (info)
848 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 849 {
aee6f5b4 850 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
851 asection *p;
852 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 853 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
854 && (p->flags & SEC_ALLOC) != 0
855 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
856 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
857 else
858 elf_section_data (p)->dynindx = 0;
30b30c21 859 }
554220db 860 *section_sym_count = dynsymcount;
30b30c21 861
6fa3860b
PB
862 elf_link_hash_traverse (elf_hash_table (info),
863 elf_link_renumber_local_hash_table_dynsyms,
864 &dynsymcount);
865
30b30c21
RH
866 if (elf_hash_table (info)->dynlocal)
867 {
868 struct elf_link_local_dynamic_entry *p;
869 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
870 p->dynindx = ++dynsymcount;
871 }
872
873 elf_link_hash_traverse (elf_hash_table (info),
874 elf_link_renumber_hash_table_dynsyms,
875 &dynsymcount);
876
877 /* There is an unused NULL entry at the head of the table which
878 we must account for in our count. Unless there weren't any
879 symbols, which means we'll have no table at all. */
880 if (dynsymcount != 0)
881 ++dynsymcount;
882
ccabcbe5
AM
883 elf_hash_table (info)->dynsymcount = dynsymcount;
884 return dynsymcount;
30b30c21 885}
252b5132 886
54ac0771
L
887/* Merge st_other field. */
888
889static void
890elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 891 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 892 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
893{
894 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
895
896 /* If st_other has a processor-specific meaning, specific
cd3416da 897 code might be needed here. */
54ac0771
L
898 if (bed->elf_backend_merge_symbol_attribute)
899 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
900 dynamic);
901
cd3416da 902 if (!dynamic)
54ac0771 903 {
cd3416da
AM
904 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
905 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 906
cd3416da
AM
907 /* Keep the most constraining visibility. Leave the remainder
908 of the st_other field to elf_backend_merge_symbol_attribute. */
909 if (symvis - 1 < hvis - 1)
910 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 911 }
b8417128
AM
912 else if (definition
913 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
914 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 915 h->protected_def = 1;
54ac0771
L
916}
917
4f3fedcf
AM
918/* This function is called when we want to merge a new symbol with an
919 existing symbol. It handles the various cases which arise when we
920 find a definition in a dynamic object, or when there is already a
921 definition in a dynamic object. The new symbol is described by
922 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
923 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
924 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
925 of an old common symbol. We set OVERRIDE if the old symbol is
926 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
927 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
928 to change. By OK to change, we mean that we shouldn't warn if the
929 type or size does change. */
45d6a902 930
8a56bd02 931static bfd_boolean
268b6b39
AM
932_bfd_elf_merge_symbol (bfd *abfd,
933 struct bfd_link_info *info,
934 const char *name,
935 Elf_Internal_Sym *sym,
936 asection **psec,
937 bfd_vma *pvalue,
4f3fedcf
AM
938 struct elf_link_hash_entry **sym_hash,
939 bfd **poldbfd,
37a9e49a 940 bfd_boolean *pold_weak,
af44c138 941 unsigned int *pold_alignment,
268b6b39
AM
942 bfd_boolean *skip,
943 bfd_boolean *override,
944 bfd_boolean *type_change_ok,
6e33951e
L
945 bfd_boolean *size_change_ok,
946 bfd_boolean *matched)
252b5132 947{
7479dfd4 948 asection *sec, *oldsec;
45d6a902 949 struct elf_link_hash_entry *h;
90c984fc 950 struct elf_link_hash_entry *hi;
45d6a902
AM
951 struct elf_link_hash_entry *flip;
952 int bind;
953 bfd *oldbfd;
954 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 955 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 956 const struct elf_backend_data *bed;
6e33951e 957 char *new_version;
45d6a902
AM
958
959 *skip = FALSE;
960 *override = FALSE;
961
962 sec = *psec;
963 bind = ELF_ST_BIND (sym->st_info);
964
965 if (! bfd_is_und_section (sec))
966 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
967 else
968 h = ((struct elf_link_hash_entry *)
969 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
970 if (h == NULL)
971 return FALSE;
972 *sym_hash = h;
252b5132 973
88ba32a0
L
974 bed = get_elf_backend_data (abfd);
975
6e33951e 976 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 977 if (h->versioned != unversioned)
6e33951e 978 {
422f1182
L
979 /* Symbol version is unknown or versioned. */
980 new_version = strrchr (name, ELF_VER_CHR);
981 if (new_version)
982 {
983 if (h->versioned == unknown)
984 {
985 if (new_version > name && new_version[-1] != ELF_VER_CHR)
986 h->versioned = versioned_hidden;
987 else
988 h->versioned = versioned;
989 }
990 new_version += 1;
991 if (new_version[0] == '\0')
992 new_version = NULL;
993 }
994 else
995 h->versioned = unversioned;
6e33951e 996 }
422f1182
L
997 else
998 new_version = NULL;
6e33951e 999
90c984fc
L
1000 /* For merging, we only care about real symbols. But we need to make
1001 sure that indirect symbol dynamic flags are updated. */
1002 hi = h;
45d6a902
AM
1003 while (h->root.type == bfd_link_hash_indirect
1004 || h->root.type == bfd_link_hash_warning)
1005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1006
6e33951e
L
1007 if (!*matched)
1008 {
1009 if (hi == h || h->root.type == bfd_link_hash_new)
1010 *matched = TRUE;
1011 else
1012 {
ae7683d2 1013 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1014 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1015 true if the new symbol is only visible to the symbol with
6e33951e 1016 the same symbol version. */
422f1182
L
1017 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1018 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1019 if (!old_hidden && !new_hidden)
1020 /* The new symbol matches the existing symbol if both
1021 aren't hidden. */
1022 *matched = TRUE;
1023 else
1024 {
1025 /* OLD_VERSION is the symbol version of the existing
1026 symbol. */
422f1182
L
1027 char *old_version;
1028
1029 if (h->versioned >= versioned)
1030 old_version = strrchr (h->root.root.string,
1031 ELF_VER_CHR) + 1;
1032 else
1033 old_version = NULL;
6e33951e
L
1034
1035 /* The new symbol matches the existing symbol if they
1036 have the same symbol version. */
1037 *matched = (old_version == new_version
1038 || (old_version != NULL
1039 && new_version != NULL
1040 && strcmp (old_version, new_version) == 0));
1041 }
1042 }
1043 }
1044
934bce08
AM
1045 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1046 existing symbol. */
1047
1048 oldbfd = NULL;
1049 oldsec = NULL;
1050 switch (h->root.type)
1051 {
1052 default:
1053 break;
1054
1055 case bfd_link_hash_undefined:
1056 case bfd_link_hash_undefweak:
1057 oldbfd = h->root.u.undef.abfd;
1058 break;
1059
1060 case bfd_link_hash_defined:
1061 case bfd_link_hash_defweak:
1062 oldbfd = h->root.u.def.section->owner;
1063 oldsec = h->root.u.def.section;
1064 break;
1065
1066 case bfd_link_hash_common:
1067 oldbfd = h->root.u.c.p->section->owner;
1068 oldsec = h->root.u.c.p->section;
1069 if (pold_alignment)
1070 *pold_alignment = h->root.u.c.p->alignment_power;
1071 break;
1072 }
1073 if (poldbfd && *poldbfd == NULL)
1074 *poldbfd = oldbfd;
1075
1076 /* Differentiate strong and weak symbols. */
1077 newweak = bind == STB_WEAK;
1078 oldweak = (h->root.type == bfd_link_hash_defweak
1079 || h->root.type == bfd_link_hash_undefweak);
1080 if (pold_weak)
1081 *pold_weak = oldweak;
1082
1083 /* This code is for coping with dynamic objects, and is only useful
1084 if we are doing an ELF link. */
1085 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1086 return TRUE;
1087
40b36307 1088 /* We have to check it for every instance since the first few may be
ee659f1f 1089 references and not all compilers emit symbol type for undefined
40b36307
L
1090 symbols. */
1091 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1092
ee659f1f
AM
1093 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1094 respectively, is from a dynamic object. */
1095
1096 newdyn = (abfd->flags & DYNAMIC) != 0;
1097
1098 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1099 syms and defined syms in dynamic libraries respectively.
1100 ref_dynamic on the other hand can be set for a symbol defined in
1101 a dynamic library, and def_dynamic may not be set; When the
1102 definition in a dynamic lib is overridden by a definition in the
1103 executable use of the symbol in the dynamic lib becomes a
1104 reference to the executable symbol. */
1105 if (newdyn)
1106 {
1107 if (bfd_is_und_section (sec))
1108 {
1109 if (bind != STB_WEAK)
1110 {
1111 h->ref_dynamic_nonweak = 1;
1112 hi->ref_dynamic_nonweak = 1;
1113 }
1114 }
1115 else
1116 {
6e33951e
L
1117 /* Update the existing symbol only if they match. */
1118 if (*matched)
1119 h->dynamic_def = 1;
ee659f1f
AM
1120 hi->dynamic_def = 1;
1121 }
1122 }
1123
45d6a902
AM
1124 /* If we just created the symbol, mark it as being an ELF symbol.
1125 Other than that, there is nothing to do--there is no merge issue
1126 with a newly defined symbol--so we just return. */
1127
1128 if (h->root.type == bfd_link_hash_new)
252b5132 1129 {
f5385ebf 1130 h->non_elf = 0;
45d6a902
AM
1131 return TRUE;
1132 }
252b5132 1133
45d6a902
AM
1134 /* In cases involving weak versioned symbols, we may wind up trying
1135 to merge a symbol with itself. Catch that here, to avoid the
1136 confusion that results if we try to override a symbol with
1137 itself. The additional tests catch cases like
1138 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1139 dynamic object, which we do want to handle here. */
1140 if (abfd == oldbfd
895fa45f 1141 && (newweak || oldweak)
45d6a902 1142 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1143 || !h->def_regular))
45d6a902
AM
1144 return TRUE;
1145
707bba77 1146 olddyn = FALSE;
45d6a902
AM
1147 if (oldbfd != NULL)
1148 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1149 else if (oldsec != NULL)
45d6a902 1150 {
707bba77 1151 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1152 indices used by MIPS ELF. */
707bba77 1153 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1154 }
252b5132 1155
45d6a902
AM
1156 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1157 respectively, appear to be a definition rather than reference. */
1158
707bba77 1159 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1160
707bba77
AM
1161 olddef = (h->root.type != bfd_link_hash_undefined
1162 && h->root.type != bfd_link_hash_undefweak
1163 && h->root.type != bfd_link_hash_common);
45d6a902 1164
0a36a439
L
1165 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1166 respectively, appear to be a function. */
1167
1168 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1169 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1170
1171 oldfunc = (h->type != STT_NOTYPE
1172 && bed->is_function_type (h->type));
1173
580a2b6e
L
1174 /* When we try to create a default indirect symbol from the dynamic
1175 definition with the default version, we skip it if its type and
40101021 1176 the type of existing regular definition mismatch. */
580a2b6e 1177 if (pold_alignment == NULL
580a2b6e
L
1178 && newdyn
1179 && newdef
1180 && !olddyn
4584ec12
L
1181 && (((olddef || h->root.type == bfd_link_hash_common)
1182 && ELF_ST_TYPE (sym->st_info) != h->type
1183 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1184 && h->type != STT_NOTYPE
1185 && !(newfunc && oldfunc))
1186 || (olddef
1187 && ((h->type == STT_GNU_IFUNC)
1188 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1189 {
1190 *skip = TRUE;
1191 return TRUE;
1192 }
1193
4c34aff8
AM
1194 /* Check TLS symbols. We don't check undefined symbols introduced
1195 by "ld -u" which have no type (and oldbfd NULL), and we don't
1196 check symbols from plugins because they also have no type. */
1197 if (oldbfd != NULL
1198 && (oldbfd->flags & BFD_PLUGIN) == 0
1199 && (abfd->flags & BFD_PLUGIN) == 0
1200 && ELF_ST_TYPE (sym->st_info) != h->type
1201 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1202 {
1203 bfd *ntbfd, *tbfd;
1204 bfd_boolean ntdef, tdef;
1205 asection *ntsec, *tsec;
1206
1207 if (h->type == STT_TLS)
1208 {
3b36f7e6 1209 ntbfd = abfd;
7479dfd4
L
1210 ntsec = sec;
1211 ntdef = newdef;
1212 tbfd = oldbfd;
1213 tsec = oldsec;
1214 tdef = olddef;
1215 }
1216 else
1217 {
1218 ntbfd = oldbfd;
1219 ntsec = oldsec;
1220 ntdef = olddef;
1221 tbfd = abfd;
1222 tsec = sec;
1223 tdef = newdef;
1224 }
1225
1226 if (tdef && ntdef)
1227 (*_bfd_error_handler)
191c0c42
AM
1228 (_("%s: TLS definition in %B section %A "
1229 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1230 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1231 else if (!tdef && !ntdef)
1232 (*_bfd_error_handler)
191c0c42
AM
1233 (_("%s: TLS reference in %B "
1234 "mismatches non-TLS reference in %B"),
7479dfd4
L
1235 tbfd, ntbfd, h->root.root.string);
1236 else if (tdef)
1237 (*_bfd_error_handler)
191c0c42
AM
1238 (_("%s: TLS definition in %B section %A "
1239 "mismatches non-TLS reference in %B"),
7479dfd4
L
1240 tbfd, tsec, ntbfd, h->root.root.string);
1241 else
1242 (*_bfd_error_handler)
191c0c42
AM
1243 (_("%s: TLS reference in %B "
1244 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1245 tbfd, ntbfd, ntsec, h->root.root.string);
1246
1247 bfd_set_error (bfd_error_bad_value);
1248 return FALSE;
1249 }
1250
45d6a902
AM
1251 /* If the old symbol has non-default visibility, we ignore the new
1252 definition from a dynamic object. */
1253 if (newdyn
9c7a29a3 1254 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1255 && !bfd_is_und_section (sec))
1256 {
1257 *skip = TRUE;
1258 /* Make sure this symbol is dynamic. */
f5385ebf 1259 h->ref_dynamic = 1;
90c984fc 1260 hi->ref_dynamic = 1;
45d6a902
AM
1261 /* A protected symbol has external availability. Make sure it is
1262 recorded as dynamic.
1263
1264 FIXME: Should we check type and size for protected symbol? */
1265 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1266 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1267 else
1268 return TRUE;
1269 }
1270 else if (!newdyn
9c7a29a3 1271 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1272 && h->def_dynamic)
45d6a902
AM
1273 {
1274 /* If the new symbol with non-default visibility comes from a
1275 relocatable file and the old definition comes from a dynamic
1276 object, we remove the old definition. */
6c9b78e6 1277 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1278 {
1279 /* Handle the case where the old dynamic definition is
1280 default versioned. We need to copy the symbol info from
1281 the symbol with default version to the normal one if it
1282 was referenced before. */
1283 if (h->ref_regular)
1284 {
6c9b78e6 1285 hi->root.type = h->root.type;
d2dee3b2 1286 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1287 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1288
6c9b78e6 1289 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1290 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1291 {
aed81c4e
MR
1292 /* If the new symbol is hidden or internal, completely undo
1293 any dynamic link state. */
1294 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1295 h->forced_local = 0;
1296 h->ref_dynamic = 0;
d2dee3b2
L
1297 }
1298 else
aed81c4e
MR
1299 h->ref_dynamic = 1;
1300
1301 h->def_dynamic = 0;
aed81c4e
MR
1302 /* FIXME: Should we check type and size for protected symbol? */
1303 h->size = 0;
1304 h->type = 0;
1305
6c9b78e6 1306 h = hi;
d2dee3b2
L
1307 }
1308 else
6c9b78e6 1309 h = hi;
d2dee3b2 1310 }
1de1a317 1311
f5eda473
AM
1312 /* If the old symbol was undefined before, then it will still be
1313 on the undefs list. If the new symbol is undefined or
1314 common, we can't make it bfd_link_hash_new here, because new
1315 undefined or common symbols will be added to the undefs list
1316 by _bfd_generic_link_add_one_symbol. Symbols may not be
1317 added twice to the undefs list. Also, if the new symbol is
1318 undefweak then we don't want to lose the strong undef. */
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1320 {
1de1a317 1321 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1322 h->root.u.undef.abfd = abfd;
1323 }
1324 else
1325 {
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1328 }
1329
f5eda473 1330 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1331 {
f5eda473
AM
1332 /* If the new symbol is hidden or internal, completely undo
1333 any dynamic link state. */
1334 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1335 h->forced_local = 0;
1336 h->ref_dynamic = 0;
45d6a902 1337 }
f5eda473
AM
1338 else
1339 h->ref_dynamic = 1;
1340 h->def_dynamic = 0;
45d6a902
AM
1341 /* FIXME: Should we check type and size for protected symbol? */
1342 h->size = 0;
1343 h->type = 0;
1344 return TRUE;
1345 }
14a793b2 1346
15b43f48
AM
1347 /* If a new weak symbol definition comes from a regular file and the
1348 old symbol comes from a dynamic library, we treat the new one as
1349 strong. Similarly, an old weak symbol definition from a regular
1350 file is treated as strong when the new symbol comes from a dynamic
1351 library. Further, an old weak symbol from a dynamic library is
1352 treated as strong if the new symbol is from a dynamic library.
1353 This reflects the way glibc's ld.so works.
1354
1355 Do this before setting *type_change_ok or *size_change_ok so that
1356 we warn properly when dynamic library symbols are overridden. */
1357
1358 if (newdef && !newdyn && olddyn)
0f8a2703 1359 newweak = FALSE;
15b43f48 1360 if (olddef && newdyn)
0f8a2703
AM
1361 oldweak = FALSE;
1362
d334575b 1363 /* Allow changes between different types of function symbol. */
0a36a439 1364 if (newfunc && oldfunc)
fcb93ecf
PB
1365 *type_change_ok = TRUE;
1366
79349b09
AM
1367 /* It's OK to change the type if either the existing symbol or the
1368 new symbol is weak. A type change is also OK if the old symbol
1369 is undefined and the new symbol is defined. */
252b5132 1370
79349b09
AM
1371 if (oldweak
1372 || newweak
1373 || (newdef
1374 && h->root.type == bfd_link_hash_undefined))
1375 *type_change_ok = TRUE;
1376
1377 /* It's OK to change the size if either the existing symbol or the
1378 new symbol is weak, or if the old symbol is undefined. */
1379
1380 if (*type_change_ok
1381 || h->root.type == bfd_link_hash_undefined)
1382 *size_change_ok = TRUE;
45d6a902 1383
45d6a902
AM
1384 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1385 symbol, respectively, appears to be a common symbol in a dynamic
1386 object. If a symbol appears in an uninitialized section, and is
1387 not weak, and is not a function, then it may be a common symbol
1388 which was resolved when the dynamic object was created. We want
1389 to treat such symbols specially, because they raise special
1390 considerations when setting the symbol size: if the symbol
1391 appears as a common symbol in a regular object, and the size in
1392 the regular object is larger, we must make sure that we use the
1393 larger size. This problematic case can always be avoided in C,
1394 but it must be handled correctly when using Fortran shared
1395 libraries.
1396
1397 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1398 likewise for OLDDYNCOMMON and OLDDEF.
1399
1400 Note that this test is just a heuristic, and that it is quite
1401 possible to have an uninitialized symbol in a shared object which
1402 is really a definition, rather than a common symbol. This could
1403 lead to some minor confusion when the symbol really is a common
1404 symbol in some regular object. However, I think it will be
1405 harmless. */
1406
1407 if (newdyn
1408 && newdef
79349b09 1409 && !newweak
45d6a902
AM
1410 && (sec->flags & SEC_ALLOC) != 0
1411 && (sec->flags & SEC_LOAD) == 0
1412 && sym->st_size > 0
0a36a439 1413 && !newfunc)
45d6a902
AM
1414 newdyncommon = TRUE;
1415 else
1416 newdyncommon = FALSE;
1417
1418 if (olddyn
1419 && olddef
1420 && h->root.type == bfd_link_hash_defined
f5385ebf 1421 && h->def_dynamic
45d6a902
AM
1422 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1423 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1424 && h->size > 0
0a36a439 1425 && !oldfunc)
45d6a902
AM
1426 olddyncommon = TRUE;
1427 else
1428 olddyncommon = FALSE;
1429
a4d8e49b
L
1430 /* We now know everything about the old and new symbols. We ask the
1431 backend to check if we can merge them. */
5d13b3b3
AM
1432 if (bed->merge_symbol != NULL)
1433 {
1434 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1435 return FALSE;
1436 sec = *psec;
1437 }
a4d8e49b 1438
45d6a902
AM
1439 /* If both the old and the new symbols look like common symbols in a
1440 dynamic object, set the size of the symbol to the larger of the
1441 two. */
1442
1443 if (olddyncommon
1444 && newdyncommon
1445 && sym->st_size != h->size)
1446 {
1447 /* Since we think we have two common symbols, issue a multiple
1448 common warning if desired. Note that we only warn if the
1449 size is different. If the size is the same, we simply let
1450 the old symbol override the new one as normally happens with
1451 symbols defined in dynamic objects. */
1452
1453 if (! ((*info->callbacks->multiple_common)
24f58f47 1454 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1455 return FALSE;
252b5132 1456
45d6a902
AM
1457 if (sym->st_size > h->size)
1458 h->size = sym->st_size;
252b5132 1459
45d6a902 1460 *size_change_ok = TRUE;
252b5132
RH
1461 }
1462
45d6a902
AM
1463 /* If we are looking at a dynamic object, and we have found a
1464 definition, we need to see if the symbol was already defined by
1465 some other object. If so, we want to use the existing
1466 definition, and we do not want to report a multiple symbol
1467 definition error; we do this by clobbering *PSEC to be
1468 bfd_und_section_ptr.
1469
1470 We treat a common symbol as a definition if the symbol in the
1471 shared library is a function, since common symbols always
1472 represent variables; this can cause confusion in principle, but
1473 any such confusion would seem to indicate an erroneous program or
1474 shared library. We also permit a common symbol in a regular
79349b09 1475 object to override a weak symbol in a shared object. */
45d6a902
AM
1476
1477 if (newdyn
1478 && newdef
77cfaee6 1479 && (olddef
45d6a902 1480 || (h->root.type == bfd_link_hash_common
0a36a439 1481 && (newweak || newfunc))))
45d6a902
AM
1482 {
1483 *override = TRUE;
1484 newdef = FALSE;
1485 newdyncommon = FALSE;
252b5132 1486
45d6a902
AM
1487 *psec = sec = bfd_und_section_ptr;
1488 *size_change_ok = TRUE;
252b5132 1489
45d6a902
AM
1490 /* If we get here when the old symbol is a common symbol, then
1491 we are explicitly letting it override a weak symbol or
1492 function in a dynamic object, and we don't want to warn about
1493 a type change. If the old symbol is a defined symbol, a type
1494 change warning may still be appropriate. */
252b5132 1495
45d6a902
AM
1496 if (h->root.type == bfd_link_hash_common)
1497 *type_change_ok = TRUE;
1498 }
1499
1500 /* Handle the special case of an old common symbol merging with a
1501 new symbol which looks like a common symbol in a shared object.
1502 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1503 common symbol, and let _bfd_generic_link_add_one_symbol do the
1504 right thing. */
45d6a902
AM
1505
1506 if (newdyncommon
1507 && h->root.type == bfd_link_hash_common)
1508 {
1509 *override = TRUE;
1510 newdef = FALSE;
1511 newdyncommon = FALSE;
1512 *pvalue = sym->st_size;
a4d8e49b 1513 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1514 *size_change_ok = TRUE;
1515 }
1516
c5e2cead 1517 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1518 if (newdef && olddef && newweak)
54ac0771 1519 {
35ed3f94 1520 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1521 if (!(oldbfd != NULL
1522 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1523 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1524 {
1525 newdef = FALSE;
1526 *skip = TRUE;
1527 }
54ac0771
L
1528
1529 /* Merge st_other. If the symbol already has a dynamic index,
1530 but visibility says it should not be visible, turn it into a
1531 local symbol. */
b8417128 1532 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1533 if (h->dynindx != -1)
1534 switch (ELF_ST_VISIBILITY (h->other))
1535 {
1536 case STV_INTERNAL:
1537 case STV_HIDDEN:
1538 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1539 break;
1540 }
1541 }
c5e2cead 1542
45d6a902
AM
1543 /* If the old symbol is from a dynamic object, and the new symbol is
1544 a definition which is not from a dynamic object, then the new
1545 symbol overrides the old symbol. Symbols from regular files
1546 always take precedence over symbols from dynamic objects, even if
1547 they are defined after the dynamic object in the link.
1548
1549 As above, we again permit a common symbol in a regular object to
1550 override a definition in a shared object if the shared object
0f8a2703 1551 symbol is a function or is weak. */
45d6a902
AM
1552
1553 flip = NULL;
77cfaee6 1554 if (!newdyn
45d6a902
AM
1555 && (newdef
1556 || (bfd_is_com_section (sec)
0a36a439 1557 && (oldweak || oldfunc)))
45d6a902
AM
1558 && olddyn
1559 && olddef
f5385ebf 1560 && h->def_dynamic)
45d6a902
AM
1561 {
1562 /* Change the hash table entry to undefined, and let
1563 _bfd_generic_link_add_one_symbol do the right thing with the
1564 new definition. */
1565
1566 h->root.type = bfd_link_hash_undefined;
1567 h->root.u.undef.abfd = h->root.u.def.section->owner;
1568 *size_change_ok = TRUE;
1569
1570 olddef = FALSE;
1571 olddyncommon = FALSE;
1572
1573 /* We again permit a type change when a common symbol may be
1574 overriding a function. */
1575
1576 if (bfd_is_com_section (sec))
0a36a439
L
1577 {
1578 if (oldfunc)
1579 {
1580 /* If a common symbol overrides a function, make sure
1581 that it isn't defined dynamically nor has type
1582 function. */
1583 h->def_dynamic = 0;
1584 h->type = STT_NOTYPE;
1585 }
1586 *type_change_ok = TRUE;
1587 }
45d6a902 1588
6c9b78e6
AM
1589 if (hi->root.type == bfd_link_hash_indirect)
1590 flip = hi;
45d6a902
AM
1591 else
1592 /* This union may have been set to be non-NULL when this symbol
1593 was seen in a dynamic object. We must force the union to be
1594 NULL, so that it is correct for a regular symbol. */
1595 h->verinfo.vertree = NULL;
1596 }
1597
1598 /* Handle the special case of a new common symbol merging with an
1599 old symbol that looks like it might be a common symbol defined in
1600 a shared object. Note that we have already handled the case in
1601 which a new common symbol should simply override the definition
1602 in the shared library. */
1603
1604 if (! newdyn
1605 && bfd_is_com_section (sec)
1606 && olddyncommon)
1607 {
1608 /* It would be best if we could set the hash table entry to a
1609 common symbol, but we don't know what to use for the section
1610 or the alignment. */
1611 if (! ((*info->callbacks->multiple_common)
24f58f47 1612 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1613 return FALSE;
1614
4cc11e76 1615 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1616 larger, pretend that the new symbol has its size. */
1617
1618 if (h->size > *pvalue)
1619 *pvalue = h->size;
1620
af44c138
L
1621 /* We need to remember the alignment required by the symbol
1622 in the dynamic object. */
1623 BFD_ASSERT (pold_alignment);
1624 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1625
1626 olddef = FALSE;
1627 olddyncommon = FALSE;
1628
1629 h->root.type = bfd_link_hash_undefined;
1630 h->root.u.undef.abfd = h->root.u.def.section->owner;
1631
1632 *size_change_ok = TRUE;
1633 *type_change_ok = TRUE;
1634
6c9b78e6
AM
1635 if (hi->root.type == bfd_link_hash_indirect)
1636 flip = hi;
45d6a902
AM
1637 else
1638 h->verinfo.vertree = NULL;
1639 }
1640
1641 if (flip != NULL)
1642 {
1643 /* Handle the case where we had a versioned symbol in a dynamic
1644 library and now find a definition in a normal object. In this
1645 case, we make the versioned symbol point to the normal one. */
45d6a902 1646 flip->root.type = h->root.type;
00cbee0a 1647 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1648 h->root.type = bfd_link_hash_indirect;
1649 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1650 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1651 if (h->def_dynamic)
45d6a902 1652 {
f5385ebf
AM
1653 h->def_dynamic = 0;
1654 flip->ref_dynamic = 1;
45d6a902
AM
1655 }
1656 }
1657
45d6a902
AM
1658 return TRUE;
1659}
1660
1661/* This function is called to create an indirect symbol from the
1662 default for the symbol with the default version if needed. The
4f3fedcf 1663 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1664 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1665
28caa186 1666static bfd_boolean
268b6b39
AM
1667_bfd_elf_add_default_symbol (bfd *abfd,
1668 struct bfd_link_info *info,
1669 struct elf_link_hash_entry *h,
1670 const char *name,
1671 Elf_Internal_Sym *sym,
4f3fedcf
AM
1672 asection *sec,
1673 bfd_vma value,
1674 bfd **poldbfd,
e3c9d234 1675 bfd_boolean *dynsym)
45d6a902
AM
1676{
1677 bfd_boolean type_change_ok;
1678 bfd_boolean size_change_ok;
1679 bfd_boolean skip;
1680 char *shortname;
1681 struct elf_link_hash_entry *hi;
1682 struct bfd_link_hash_entry *bh;
9c5bfbb7 1683 const struct elf_backend_data *bed;
45d6a902
AM
1684 bfd_boolean collect;
1685 bfd_boolean dynamic;
e3c9d234 1686 bfd_boolean override;
45d6a902
AM
1687 char *p;
1688 size_t len, shortlen;
ffd65175 1689 asection *tmp_sec;
6e33951e 1690 bfd_boolean matched;
45d6a902 1691
422f1182
L
1692 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1693 return TRUE;
1694
45d6a902
AM
1695 /* If this symbol has a version, and it is the default version, we
1696 create an indirect symbol from the default name to the fully
1697 decorated name. This will cause external references which do not
1698 specify a version to be bound to this version of the symbol. */
1699 p = strchr (name, ELF_VER_CHR);
422f1182
L
1700 if (h->versioned == unknown)
1701 {
1702 if (p == NULL)
1703 {
1704 h->versioned = unversioned;
1705 return TRUE;
1706 }
1707 else
1708 {
1709 if (p[1] != ELF_VER_CHR)
1710 {
1711 h->versioned = versioned_hidden;
1712 return TRUE;
1713 }
1714 else
1715 h->versioned = versioned;
1716 }
1717 }
4373f8af
L
1718 else
1719 {
1720 /* PR ld/19073: We may see an unversioned definition after the
1721 default version. */
1722 if (p == NULL)
1723 return TRUE;
1724 }
45d6a902 1725
45d6a902
AM
1726 bed = get_elf_backend_data (abfd);
1727 collect = bed->collect;
1728 dynamic = (abfd->flags & DYNAMIC) != 0;
1729
1730 shortlen = p - name;
a50b1753 1731 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1732 if (shortname == NULL)
1733 return FALSE;
1734 memcpy (shortname, name, shortlen);
1735 shortname[shortlen] = '\0';
1736
1737 /* We are going to create a new symbol. Merge it with any existing
1738 symbol with this name. For the purposes of the merge, act as
1739 though we were defining the symbol we just defined, although we
1740 actually going to define an indirect symbol. */
1741 type_change_ok = FALSE;
1742 size_change_ok = FALSE;
6e33951e 1743 matched = TRUE;
ffd65175
AM
1744 tmp_sec = sec;
1745 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1746 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1747 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1748 return FALSE;
1749
1750 if (skip)
1751 goto nondefault;
1752
1753 if (! override)
1754 {
c6e8a9a8 1755 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1756 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1757 {
1758 bh = &hi->root;
1759 if (! (_bfd_generic_link_add_one_symbol
1760 (info, abfd, shortname, BSF_INDIRECT,
1761 bfd_ind_section_ptr,
1762 0, name, FALSE, collect, &bh)))
1763 return FALSE;
1764 hi = (struct elf_link_hash_entry *) bh;
1765 }
45d6a902
AM
1766 }
1767 else
1768 {
1769 /* In this case the symbol named SHORTNAME is overriding the
1770 indirect symbol we want to add. We were planning on making
1771 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1772 is the name without a version. NAME is the fully versioned
1773 name, and it is the default version.
1774
1775 Overriding means that we already saw a definition for the
1776 symbol SHORTNAME in a regular object, and it is overriding
1777 the symbol defined in the dynamic object.
1778
1779 When this happens, we actually want to change NAME, the
1780 symbol we just added, to refer to SHORTNAME. This will cause
1781 references to NAME in the shared object to become references
1782 to SHORTNAME in the regular object. This is what we expect
1783 when we override a function in a shared object: that the
1784 references in the shared object will be mapped to the
1785 definition in the regular object. */
1786
1787 while (hi->root.type == bfd_link_hash_indirect
1788 || hi->root.type == bfd_link_hash_warning)
1789 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1790
1791 h->root.type = bfd_link_hash_indirect;
1792 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1793 if (h->def_dynamic)
45d6a902 1794 {
f5385ebf
AM
1795 h->def_dynamic = 0;
1796 hi->ref_dynamic = 1;
1797 if (hi->ref_regular
1798 || hi->def_regular)
45d6a902 1799 {
c152c796 1800 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1801 return FALSE;
1802 }
1803 }
1804
1805 /* Now set HI to H, so that the following code will set the
1806 other fields correctly. */
1807 hi = h;
1808 }
1809
fab4a87f
L
1810 /* Check if HI is a warning symbol. */
1811 if (hi->root.type == bfd_link_hash_warning)
1812 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1813
45d6a902
AM
1814 /* If there is a duplicate definition somewhere, then HI may not
1815 point to an indirect symbol. We will have reported an error to
1816 the user in that case. */
1817
1818 if (hi->root.type == bfd_link_hash_indirect)
1819 {
1820 struct elf_link_hash_entry *ht;
1821
45d6a902 1822 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1823 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1824
68c88cd4
AM
1825 /* A reference to the SHORTNAME symbol from a dynamic library
1826 will be satisfied by the versioned symbol at runtime. In
1827 effect, we have a reference to the versioned symbol. */
1828 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1829 hi->dynamic_def |= ht->dynamic_def;
1830
45d6a902
AM
1831 /* See if the new flags lead us to realize that the symbol must
1832 be dynamic. */
1833 if (! *dynsym)
1834 {
1835 if (! dynamic)
1836 {
0e1862bb 1837 if (! bfd_link_executable (info)
90c984fc 1838 || hi->def_dynamic
f5385ebf 1839 || hi->ref_dynamic)
45d6a902
AM
1840 *dynsym = TRUE;
1841 }
1842 else
1843 {
f5385ebf 1844 if (hi->ref_regular)
45d6a902
AM
1845 *dynsym = TRUE;
1846 }
1847 }
1848 }
1849
1850 /* We also need to define an indirection from the nondefault version
1851 of the symbol. */
1852
1853nondefault:
1854 len = strlen (name);
a50b1753 1855 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1856 if (shortname == NULL)
1857 return FALSE;
1858 memcpy (shortname, name, shortlen);
1859 memcpy (shortname + shortlen, p + 1, len - shortlen);
1860
1861 /* Once again, merge with any existing symbol. */
1862 type_change_ok = FALSE;
1863 size_change_ok = FALSE;
ffd65175
AM
1864 tmp_sec = sec;
1865 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1866 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1867 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1868 return FALSE;
1869
1870 if (skip)
1871 return TRUE;
1872
1873 if (override)
1874 {
1875 /* Here SHORTNAME is a versioned name, so we don't expect to see
1876 the type of override we do in the case above unless it is
4cc11e76 1877 overridden by a versioned definition. */
45d6a902
AM
1878 if (hi->root.type != bfd_link_hash_defined
1879 && hi->root.type != bfd_link_hash_defweak)
1880 (*_bfd_error_handler)
d003868e
AM
1881 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1882 abfd, shortname);
45d6a902
AM
1883 }
1884 else
1885 {
1886 bh = &hi->root;
1887 if (! (_bfd_generic_link_add_one_symbol
1888 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1889 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1890 return FALSE;
1891 hi = (struct elf_link_hash_entry *) bh;
1892
1893 /* If there is a duplicate definition somewhere, then HI may not
1894 point to an indirect symbol. We will have reported an error
1895 to the user in that case. */
1896
1897 if (hi->root.type == bfd_link_hash_indirect)
1898 {
fcfa13d2 1899 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1900 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1901 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1902
1903 /* See if the new flags lead us to realize that the symbol
1904 must be dynamic. */
1905 if (! *dynsym)
1906 {
1907 if (! dynamic)
1908 {
0e1862bb 1909 if (! bfd_link_executable (info)
f5385ebf 1910 || hi->ref_dynamic)
45d6a902
AM
1911 *dynsym = TRUE;
1912 }
1913 else
1914 {
f5385ebf 1915 if (hi->ref_regular)
45d6a902
AM
1916 *dynsym = TRUE;
1917 }
1918 }
1919 }
1920 }
1921
1922 return TRUE;
1923}
1924\f
1925/* This routine is used to export all defined symbols into the dynamic
1926 symbol table. It is called via elf_link_hash_traverse. */
1927
28caa186 1928static bfd_boolean
268b6b39 1929_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1930{
a50b1753 1931 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1932
1933 /* Ignore indirect symbols. These are added by the versioning code. */
1934 if (h->root.type == bfd_link_hash_indirect)
1935 return TRUE;
1936
7686d77d
AM
1937 /* Ignore this if we won't export it. */
1938 if (!eif->info->export_dynamic && !h->dynamic)
1939 return TRUE;
45d6a902
AM
1940
1941 if (h->dynindx == -1
fd91d419
L
1942 && (h->def_regular || h->ref_regular)
1943 && ! bfd_hide_sym_by_version (eif->info->version_info,
1944 h->root.root.string))
45d6a902 1945 {
fd91d419 1946 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1947 {
fd91d419
L
1948 eif->failed = TRUE;
1949 return FALSE;
45d6a902
AM
1950 }
1951 }
1952
1953 return TRUE;
1954}
1955\f
1956/* Look through the symbols which are defined in other shared
1957 libraries and referenced here. Update the list of version
1958 dependencies. This will be put into the .gnu.version_r section.
1959 This function is called via elf_link_hash_traverse. */
1960
28caa186 1961static bfd_boolean
268b6b39
AM
1962_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1963 void *data)
45d6a902 1964{
a50b1753 1965 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1966 Elf_Internal_Verneed *t;
1967 Elf_Internal_Vernaux *a;
1968 bfd_size_type amt;
1969
45d6a902
AM
1970 /* We only care about symbols defined in shared objects with version
1971 information. */
f5385ebf
AM
1972 if (!h->def_dynamic
1973 || h->def_regular
45d6a902 1974 || h->dynindx == -1
7b20f099
AM
1975 || h->verinfo.verdef == NULL
1976 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1977 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
1978 return TRUE;
1979
1980 /* See if we already know about this version. */
28caa186
AM
1981 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1982 t != NULL;
1983 t = t->vn_nextref)
45d6a902
AM
1984 {
1985 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1986 continue;
1987
1988 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1989 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1990 return TRUE;
1991
1992 break;
1993 }
1994
1995 /* This is a new version. Add it to tree we are building. */
1996
1997 if (t == NULL)
1998 {
1999 amt = sizeof *t;
a50b1753 2000 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2001 if (t == NULL)
2002 {
2003 rinfo->failed = TRUE;
2004 return FALSE;
2005 }
2006
2007 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2008 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2009 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2010 }
2011
2012 amt = sizeof *a;
a50b1753 2013 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2014 if (a == NULL)
2015 {
2016 rinfo->failed = TRUE;
2017 return FALSE;
2018 }
45d6a902
AM
2019
2020 /* Note that we are copying a string pointer here, and testing it
2021 above. If bfd_elf_string_from_elf_section is ever changed to
2022 discard the string data when low in memory, this will have to be
2023 fixed. */
2024 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2025
2026 a->vna_flags = h->verinfo.verdef->vd_flags;
2027 a->vna_nextptr = t->vn_auxptr;
2028
2029 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2030 ++rinfo->vers;
2031
2032 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2033
2034 t->vn_auxptr = a;
2035
2036 return TRUE;
2037}
2038
2039/* Figure out appropriate versions for all the symbols. We may not
2040 have the version number script until we have read all of the input
2041 files, so until that point we don't know which symbols should be
2042 local. This function is called via elf_link_hash_traverse. */
2043
28caa186 2044static bfd_boolean
268b6b39 2045_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2046{
28caa186 2047 struct elf_info_failed *sinfo;
45d6a902 2048 struct bfd_link_info *info;
9c5bfbb7 2049 const struct elf_backend_data *bed;
45d6a902
AM
2050 struct elf_info_failed eif;
2051 char *p;
2052 bfd_size_type amt;
2053
a50b1753 2054 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2055 info = sinfo->info;
2056
45d6a902
AM
2057 /* Fix the symbol flags. */
2058 eif.failed = FALSE;
2059 eif.info = info;
2060 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2061 {
2062 if (eif.failed)
2063 sinfo->failed = TRUE;
2064 return FALSE;
2065 }
2066
2067 /* We only need version numbers for symbols defined in regular
2068 objects. */
f5385ebf 2069 if (!h->def_regular)
45d6a902
AM
2070 return TRUE;
2071
28caa186 2072 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2073 p = strchr (h->root.root.string, ELF_VER_CHR);
2074 if (p != NULL && h->verinfo.vertree == NULL)
2075 {
2076 struct bfd_elf_version_tree *t;
45d6a902 2077
45d6a902
AM
2078 ++p;
2079 if (*p == ELF_VER_CHR)
6e33951e 2080 ++p;
45d6a902
AM
2081
2082 /* If there is no version string, we can just return out. */
2083 if (*p == '\0')
6e33951e 2084 return TRUE;
45d6a902
AM
2085
2086 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2087 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2088 {
2089 if (strcmp (t->name, p) == 0)
2090 {
2091 size_t len;
2092 char *alc;
2093 struct bfd_elf_version_expr *d;
2094
2095 len = p - h->root.root.string;
a50b1753 2096 alc = (char *) bfd_malloc (len);
45d6a902 2097 if (alc == NULL)
14b1c01e
AM
2098 {
2099 sinfo->failed = TRUE;
2100 return FALSE;
2101 }
45d6a902
AM
2102 memcpy (alc, h->root.root.string, len - 1);
2103 alc[len - 1] = '\0';
2104 if (alc[len - 2] == ELF_VER_CHR)
2105 alc[len - 2] = '\0';
2106
2107 h->verinfo.vertree = t;
2108 t->used = TRUE;
2109 d = NULL;
2110
108ba305
JJ
2111 if (t->globals.list != NULL)
2112 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2113
2114 /* See if there is anything to force this symbol to
2115 local scope. */
108ba305 2116 if (d == NULL && t->locals.list != NULL)
45d6a902 2117 {
108ba305
JJ
2118 d = (*t->match) (&t->locals, NULL, alc);
2119 if (d != NULL
2120 && h->dynindx != -1
108ba305
JJ
2121 && ! info->export_dynamic)
2122 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2123 }
2124
2125 free (alc);
2126 break;
2127 }
2128 }
2129
2130 /* If we are building an application, we need to create a
2131 version node for this version. */
0e1862bb 2132 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2133 {
2134 struct bfd_elf_version_tree **pp;
2135 int version_index;
2136
2137 /* If we aren't going to export this symbol, we don't need
2138 to worry about it. */
2139 if (h->dynindx == -1)
2140 return TRUE;
2141
2142 amt = sizeof *t;
a50b1753 2143 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2144 if (t == NULL)
2145 {
2146 sinfo->failed = TRUE;
2147 return FALSE;
2148 }
2149
45d6a902 2150 t->name = p;
45d6a902
AM
2151 t->name_indx = (unsigned int) -1;
2152 t->used = TRUE;
2153
2154 version_index = 1;
2155 /* Don't count anonymous version tag. */
fd91d419
L
2156 if (sinfo->info->version_info != NULL
2157 && sinfo->info->version_info->vernum == 0)
45d6a902 2158 version_index = 0;
fd91d419
L
2159 for (pp = &sinfo->info->version_info;
2160 *pp != NULL;
2161 pp = &(*pp)->next)
45d6a902
AM
2162 ++version_index;
2163 t->vernum = version_index;
2164
2165 *pp = t;
2166
2167 h->verinfo.vertree = t;
2168 }
2169 else if (t == NULL)
2170 {
2171 /* We could not find the version for a symbol when
2172 generating a shared archive. Return an error. */
2173 (*_bfd_error_handler)
c55fe096 2174 (_("%B: version node not found for symbol %s"),
28caa186 2175 info->output_bfd, h->root.root.string);
45d6a902
AM
2176 bfd_set_error (bfd_error_bad_value);
2177 sinfo->failed = TRUE;
2178 return FALSE;
2179 }
45d6a902
AM
2180 }
2181
2182 /* If we don't have a version for this symbol, see if we can find
2183 something. */
fd91d419 2184 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2185 {
1e8fa21e 2186 bfd_boolean hide;
ae5a3597 2187
fd91d419
L
2188 h->verinfo.vertree
2189 = bfd_find_version_for_sym (sinfo->info->version_info,
2190 h->root.root.string, &hide);
1e8fa21e
AM
2191 if (h->verinfo.vertree != NULL && hide)
2192 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2193 }
2194
2195 return TRUE;
2196}
2197\f
45d6a902
AM
2198/* Read and swap the relocs from the section indicated by SHDR. This
2199 may be either a REL or a RELA section. The relocations are
2200 translated into RELA relocations and stored in INTERNAL_RELOCS,
2201 which should have already been allocated to contain enough space.
2202 The EXTERNAL_RELOCS are a buffer where the external form of the
2203 relocations should be stored.
2204
2205 Returns FALSE if something goes wrong. */
2206
2207static bfd_boolean
268b6b39 2208elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2209 asection *sec,
268b6b39
AM
2210 Elf_Internal_Shdr *shdr,
2211 void *external_relocs,
2212 Elf_Internal_Rela *internal_relocs)
45d6a902 2213{
9c5bfbb7 2214 const struct elf_backend_data *bed;
268b6b39 2215 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2216 const bfd_byte *erela;
2217 const bfd_byte *erelaend;
2218 Elf_Internal_Rela *irela;
243ef1e0
L
2219 Elf_Internal_Shdr *symtab_hdr;
2220 size_t nsyms;
45d6a902 2221
45d6a902
AM
2222 /* Position ourselves at the start of the section. */
2223 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2224 return FALSE;
2225
2226 /* Read the relocations. */
2227 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2228 return FALSE;
2229
243ef1e0 2230 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2231 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2232
45d6a902
AM
2233 bed = get_elf_backend_data (abfd);
2234
2235 /* Convert the external relocations to the internal format. */
2236 if (shdr->sh_entsize == bed->s->sizeof_rel)
2237 swap_in = bed->s->swap_reloc_in;
2238 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2239 swap_in = bed->s->swap_reloca_in;
2240 else
2241 {
2242 bfd_set_error (bfd_error_wrong_format);
2243 return FALSE;
2244 }
2245
a50b1753 2246 erela = (const bfd_byte *) external_relocs;
51992aec 2247 erelaend = erela + shdr->sh_size;
45d6a902
AM
2248 irela = internal_relocs;
2249 while (erela < erelaend)
2250 {
243ef1e0
L
2251 bfd_vma r_symndx;
2252
45d6a902 2253 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2254 r_symndx = ELF32_R_SYM (irela->r_info);
2255 if (bed->s->arch_size == 64)
2256 r_symndx >>= 24;
ce98a316
NC
2257 if (nsyms > 0)
2258 {
2259 if ((size_t) r_symndx >= nsyms)
2260 {
2261 (*_bfd_error_handler)
2262 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2263 " for offset 0x%lx in section `%A'"),
2264 abfd, sec,
2265 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2266 bfd_set_error (bfd_error_bad_value);
2267 return FALSE;
2268 }
2269 }
cf35638d 2270 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2271 {
2272 (*_bfd_error_handler)
ce98a316
NC
2273 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2274 " when the object file has no symbol table"),
d003868e
AM
2275 abfd, sec,
2276 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2277 bfd_set_error (bfd_error_bad_value);
2278 return FALSE;
2279 }
45d6a902
AM
2280 irela += bed->s->int_rels_per_ext_rel;
2281 erela += shdr->sh_entsize;
2282 }
2283
2284 return TRUE;
2285}
2286
2287/* Read and swap the relocs for a section O. They may have been
2288 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2289 not NULL, they are used as buffers to read into. They are known to
2290 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2291 the return value is allocated using either malloc or bfd_alloc,
2292 according to the KEEP_MEMORY argument. If O has two relocation
2293 sections (both REL and RELA relocations), then the REL_HDR
2294 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2295 RELA_HDR relocations. */
45d6a902
AM
2296
2297Elf_Internal_Rela *
268b6b39
AM
2298_bfd_elf_link_read_relocs (bfd *abfd,
2299 asection *o,
2300 void *external_relocs,
2301 Elf_Internal_Rela *internal_relocs,
2302 bfd_boolean keep_memory)
45d6a902 2303{
268b6b39 2304 void *alloc1 = NULL;
45d6a902 2305 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2307 struct bfd_elf_section_data *esdo = elf_section_data (o);
2308 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2309
d4730f92
BS
2310 if (esdo->relocs != NULL)
2311 return esdo->relocs;
45d6a902
AM
2312
2313 if (o->reloc_count == 0)
2314 return NULL;
2315
45d6a902
AM
2316 if (internal_relocs == NULL)
2317 {
2318 bfd_size_type size;
2319
2320 size = o->reloc_count;
2321 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2322 if (keep_memory)
a50b1753 2323 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2324 else
a50b1753 2325 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2326 if (internal_relocs == NULL)
2327 goto error_return;
2328 }
2329
2330 if (external_relocs == NULL)
2331 {
d4730f92
BS
2332 bfd_size_type size = 0;
2333
2334 if (esdo->rel.hdr)
2335 size += esdo->rel.hdr->sh_size;
2336 if (esdo->rela.hdr)
2337 size += esdo->rela.hdr->sh_size;
45d6a902 2338
268b6b39 2339 alloc1 = bfd_malloc (size);
45d6a902
AM
2340 if (alloc1 == NULL)
2341 goto error_return;
2342 external_relocs = alloc1;
2343 }
2344
d4730f92
BS
2345 internal_rela_relocs = internal_relocs;
2346 if (esdo->rel.hdr)
2347 {
2348 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2349 external_relocs,
2350 internal_relocs))
2351 goto error_return;
2352 external_relocs = (((bfd_byte *) external_relocs)
2353 + esdo->rel.hdr->sh_size);
2354 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2355 * bed->s->int_rels_per_ext_rel);
2356 }
2357
2358 if (esdo->rela.hdr
2359 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2360 external_relocs,
2361 internal_rela_relocs)))
45d6a902
AM
2362 goto error_return;
2363
2364 /* Cache the results for next time, if we can. */
2365 if (keep_memory)
d4730f92 2366 esdo->relocs = internal_relocs;
45d6a902
AM
2367
2368 if (alloc1 != NULL)
2369 free (alloc1);
2370
2371 /* Don't free alloc2, since if it was allocated we are passing it
2372 back (under the name of internal_relocs). */
2373
2374 return internal_relocs;
2375
2376 error_return:
2377 if (alloc1 != NULL)
2378 free (alloc1);
2379 if (alloc2 != NULL)
4dd07732
AM
2380 {
2381 if (keep_memory)
2382 bfd_release (abfd, alloc2);
2383 else
2384 free (alloc2);
2385 }
45d6a902
AM
2386 return NULL;
2387}
2388
2389/* Compute the size of, and allocate space for, REL_HDR which is the
2390 section header for a section containing relocations for O. */
2391
28caa186 2392static bfd_boolean
268b6b39 2393_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2394 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2395{
d4730f92 2396 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2397
2398 /* That allows us to calculate the size of the section. */
d4730f92 2399 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2400
2401 /* The contents field must last into write_object_contents, so we
2402 allocate it with bfd_alloc rather than malloc. Also since we
2403 cannot be sure that the contents will actually be filled in,
2404 we zero the allocated space. */
a50b1753 2405 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2406 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2407 return FALSE;
2408
d4730f92 2409 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2410 {
2411 struct elf_link_hash_entry **p;
2412
ca4be51c
AM
2413 p = ((struct elf_link_hash_entry **)
2414 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2415 if (p == NULL)
2416 return FALSE;
2417
d4730f92 2418 reldata->hashes = p;
45d6a902
AM
2419 }
2420
2421 return TRUE;
2422}
2423
2424/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2425 originated from the section given by INPUT_REL_HDR) to the
2426 OUTPUT_BFD. */
2427
2428bfd_boolean
268b6b39
AM
2429_bfd_elf_link_output_relocs (bfd *output_bfd,
2430 asection *input_section,
2431 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2432 Elf_Internal_Rela *internal_relocs,
2433 struct elf_link_hash_entry **rel_hash
2434 ATTRIBUTE_UNUSED)
45d6a902
AM
2435{
2436 Elf_Internal_Rela *irela;
2437 Elf_Internal_Rela *irelaend;
2438 bfd_byte *erel;
d4730f92 2439 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2440 asection *output_section;
9c5bfbb7 2441 const struct elf_backend_data *bed;
268b6b39 2442 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2443 struct bfd_elf_section_data *esdo;
45d6a902
AM
2444
2445 output_section = input_section->output_section;
45d6a902 2446
d4730f92
BS
2447 bed = get_elf_backend_data (output_bfd);
2448 esdo = elf_section_data (output_section);
2449 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2450 {
d4730f92
BS
2451 output_reldata = &esdo->rel;
2452 swap_out = bed->s->swap_reloc_out;
45d6a902 2453 }
d4730f92
BS
2454 else if (esdo->rela.hdr
2455 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2456 {
d4730f92
BS
2457 output_reldata = &esdo->rela;
2458 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2459 }
2460 else
2461 {
2462 (*_bfd_error_handler)
d003868e
AM
2463 (_("%B: relocation size mismatch in %B section %A"),
2464 output_bfd, input_section->owner, input_section);
297d8443 2465 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2466 return FALSE;
2467 }
2468
d4730f92
BS
2469 erel = output_reldata->hdr->contents;
2470 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2471 irela = internal_relocs;
2472 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2473 * bed->s->int_rels_per_ext_rel);
2474 while (irela < irelaend)
2475 {
2476 (*swap_out) (output_bfd, irela, erel);
2477 irela += bed->s->int_rels_per_ext_rel;
2478 erel += input_rel_hdr->sh_entsize;
2479 }
2480
2481 /* Bump the counter, so that we know where to add the next set of
2482 relocations. */
d4730f92 2483 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2484
2485 return TRUE;
2486}
2487\f
508c3946
L
2488/* Make weak undefined symbols in PIE dynamic. */
2489
2490bfd_boolean
2491_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2492 struct elf_link_hash_entry *h)
2493{
0e1862bb 2494 if (bfd_link_pie (info)
508c3946
L
2495 && h->dynindx == -1
2496 && h->root.type == bfd_link_hash_undefweak)
2497 return bfd_elf_link_record_dynamic_symbol (info, h);
2498
2499 return TRUE;
2500}
2501
45d6a902
AM
2502/* Fix up the flags for a symbol. This handles various cases which
2503 can only be fixed after all the input files are seen. This is
2504 currently called by both adjust_dynamic_symbol and
2505 assign_sym_version, which is unnecessary but perhaps more robust in
2506 the face of future changes. */
2507
28caa186 2508static bfd_boolean
268b6b39
AM
2509_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2510 struct elf_info_failed *eif)
45d6a902 2511{
33774f08 2512 const struct elf_backend_data *bed;
508c3946 2513
45d6a902
AM
2514 /* If this symbol was mentioned in a non-ELF file, try to set
2515 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2516 permit a non-ELF file to correctly refer to a symbol defined in
2517 an ELF dynamic object. */
f5385ebf 2518 if (h->non_elf)
45d6a902
AM
2519 {
2520 while (h->root.type == bfd_link_hash_indirect)
2521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2522
2523 if (h->root.type != bfd_link_hash_defined
2524 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2525 {
2526 h->ref_regular = 1;
2527 h->ref_regular_nonweak = 1;
2528 }
45d6a902
AM
2529 else
2530 {
2531 if (h->root.u.def.section->owner != NULL
2532 && (bfd_get_flavour (h->root.u.def.section->owner)
2533 == bfd_target_elf_flavour))
f5385ebf
AM
2534 {
2535 h->ref_regular = 1;
2536 h->ref_regular_nonweak = 1;
2537 }
45d6a902 2538 else
f5385ebf 2539 h->def_regular = 1;
45d6a902
AM
2540 }
2541
2542 if (h->dynindx == -1
f5385ebf
AM
2543 && (h->def_dynamic
2544 || h->ref_dynamic))
45d6a902 2545 {
c152c796 2546 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2547 {
2548 eif->failed = TRUE;
2549 return FALSE;
2550 }
2551 }
2552 }
2553 else
2554 {
f5385ebf 2555 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2556 was first seen in a non-ELF file. Fortunately, if the symbol
2557 was first seen in an ELF file, we're probably OK unless the
2558 symbol was defined in a non-ELF file. Catch that case here.
2559 FIXME: We're still in trouble if the symbol was first seen in
2560 a dynamic object, and then later in a non-ELF regular object. */
2561 if ((h->root.type == bfd_link_hash_defined
2562 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2563 && !h->def_regular
45d6a902
AM
2564 && (h->root.u.def.section->owner != NULL
2565 ? (bfd_get_flavour (h->root.u.def.section->owner)
2566 != bfd_target_elf_flavour)
2567 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2568 && !h->def_dynamic)))
2569 h->def_regular = 1;
45d6a902
AM
2570 }
2571
508c3946 2572 /* Backend specific symbol fixup. */
33774f08
AM
2573 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2574 if (bed->elf_backend_fixup_symbol
2575 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2576 return FALSE;
508c3946 2577
45d6a902
AM
2578 /* If this is a final link, and the symbol was defined as a common
2579 symbol in a regular object file, and there was no definition in
2580 any dynamic object, then the linker will have allocated space for
f5385ebf 2581 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2582 flag will not have been set. */
2583 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2584 && !h->def_regular
2585 && h->ref_regular
2586 && !h->def_dynamic
96f29d96 2587 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2588 h->def_regular = 1;
45d6a902
AM
2589
2590 /* If -Bsymbolic was used (which means to bind references to global
2591 symbols to the definition within the shared object), and this
2592 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2593 need a PLT entry. Likewise, if the symbol has non-default
2594 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2595 will force it local. */
f5385ebf 2596 if (h->needs_plt
0e1862bb 2597 && bfd_link_pic (eif->info)
0eddce27 2598 && is_elf_hash_table (eif->info->hash)
55255dae 2599 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2600 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2601 && h->def_regular)
45d6a902 2602 {
45d6a902
AM
2603 bfd_boolean force_local;
2604
45d6a902
AM
2605 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2606 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2607 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2608 }
2609
2610 /* If a weak undefined symbol has non-default visibility, we also
2611 hide it from the dynamic linker. */
9c7a29a3 2612 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2613 && h->root.type == bfd_link_hash_undefweak)
33774f08 2614 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2615
2616 /* If this is a weak defined symbol in a dynamic object, and we know
2617 the real definition in the dynamic object, copy interesting flags
2618 over to the real definition. */
f6e332e6 2619 if (h->u.weakdef != NULL)
45d6a902 2620 {
45d6a902
AM
2621 /* If the real definition is defined by a regular object file,
2622 don't do anything special. See the longer description in
2623 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2624 if (h->u.weakdef->def_regular)
f6e332e6 2625 h->u.weakdef = NULL;
45d6a902 2626 else
a26587ba 2627 {
4e6b54a6
AM
2628 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2629
2630 while (h->root.type == bfd_link_hash_indirect)
2631 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2632
2633 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2634 || h->root.type == bfd_link_hash_defweak);
2635 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2636 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2637 || weakdef->root.type == bfd_link_hash_defweak);
2638 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2639 }
45d6a902
AM
2640 }
2641
2642 return TRUE;
2643}
2644
2645/* Make the backend pick a good value for a dynamic symbol. This is
2646 called via elf_link_hash_traverse, and also calls itself
2647 recursively. */
2648
28caa186 2649static bfd_boolean
268b6b39 2650_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2651{
a50b1753 2652 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2653 bfd *dynobj;
9c5bfbb7 2654 const struct elf_backend_data *bed;
45d6a902 2655
0eddce27 2656 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2657 return FALSE;
2658
45d6a902
AM
2659 /* Ignore indirect symbols. These are added by the versioning code. */
2660 if (h->root.type == bfd_link_hash_indirect)
2661 return TRUE;
2662
2663 /* Fix the symbol flags. */
2664 if (! _bfd_elf_fix_symbol_flags (h, eif))
2665 return FALSE;
2666
2667 /* If this symbol does not require a PLT entry, and it is not
2668 defined by a dynamic object, or is not referenced by a regular
2669 object, ignore it. We do have to handle a weak defined symbol,
2670 even if no regular object refers to it, if we decided to add it
2671 to the dynamic symbol table. FIXME: Do we normally need to worry
2672 about symbols which are defined by one dynamic object and
2673 referenced by another one? */
f5385ebf 2674 if (!h->needs_plt
91e21fb7 2675 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2676 && (h->def_regular
2677 || !h->def_dynamic
2678 || (!h->ref_regular
f6e332e6 2679 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2680 {
a6aa5195 2681 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2682 return TRUE;
2683 }
2684
2685 /* If we've already adjusted this symbol, don't do it again. This
2686 can happen via a recursive call. */
f5385ebf 2687 if (h->dynamic_adjusted)
45d6a902
AM
2688 return TRUE;
2689
2690 /* Don't look at this symbol again. Note that we must set this
2691 after checking the above conditions, because we may look at a
2692 symbol once, decide not to do anything, and then get called
2693 recursively later after REF_REGULAR is set below. */
f5385ebf 2694 h->dynamic_adjusted = 1;
45d6a902
AM
2695
2696 /* If this is a weak definition, and we know a real definition, and
2697 the real symbol is not itself defined by a regular object file,
2698 then get a good value for the real definition. We handle the
2699 real symbol first, for the convenience of the backend routine.
2700
2701 Note that there is a confusing case here. If the real definition
2702 is defined by a regular object file, we don't get the real symbol
2703 from the dynamic object, but we do get the weak symbol. If the
2704 processor backend uses a COPY reloc, then if some routine in the
2705 dynamic object changes the real symbol, we will not see that
2706 change in the corresponding weak symbol. This is the way other
2707 ELF linkers work as well, and seems to be a result of the shared
2708 library model.
2709
2710 I will clarify this issue. Most SVR4 shared libraries define the
2711 variable _timezone and define timezone as a weak synonym. The
2712 tzset call changes _timezone. If you write
2713 extern int timezone;
2714 int _timezone = 5;
2715 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2716 you might expect that, since timezone is a synonym for _timezone,
2717 the same number will print both times. However, if the processor
2718 backend uses a COPY reloc, then actually timezone will be copied
2719 into your process image, and, since you define _timezone
2720 yourself, _timezone will not. Thus timezone and _timezone will
2721 wind up at different memory locations. The tzset call will set
2722 _timezone, leaving timezone unchanged. */
2723
f6e332e6 2724 if (h->u.weakdef != NULL)
45d6a902 2725 {
ec24dc88
AM
2726 /* If we get to this point, there is an implicit reference to
2727 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2728 h->u.weakdef->ref_regular = 1;
45d6a902 2729
ec24dc88
AM
2730 /* Ensure that the backend adjust_dynamic_symbol function sees
2731 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2732 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2733 return FALSE;
2734 }
2735
2736 /* If a symbol has no type and no size and does not require a PLT
2737 entry, then we are probably about to do the wrong thing here: we
2738 are probably going to create a COPY reloc for an empty object.
2739 This case can arise when a shared object is built with assembly
2740 code, and the assembly code fails to set the symbol type. */
2741 if (h->size == 0
2742 && h->type == STT_NOTYPE
f5385ebf 2743 && !h->needs_plt)
45d6a902
AM
2744 (*_bfd_error_handler)
2745 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2746 h->root.root.string);
2747
2748 dynobj = elf_hash_table (eif->info)->dynobj;
2749 bed = get_elf_backend_data (dynobj);
e7c33416 2750
45d6a902
AM
2751 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2752 {
2753 eif->failed = TRUE;
2754 return FALSE;
2755 }
2756
2757 return TRUE;
2758}
2759
027297b7
L
2760/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2761 DYNBSS. */
2762
2763bfd_boolean
6cabe1ea
AM
2764_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2765 struct elf_link_hash_entry *h,
027297b7
L
2766 asection *dynbss)
2767{
91ac5911 2768 unsigned int power_of_two;
027297b7
L
2769 bfd_vma mask;
2770 asection *sec = h->root.u.def.section;
2771
2772 /* The section aligment of definition is the maximum alignment
91ac5911
L
2773 requirement of symbols defined in the section. Since we don't
2774 know the symbol alignment requirement, we start with the
2775 maximum alignment and check low bits of the symbol address
2776 for the minimum alignment. */
2777 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2778 mask = ((bfd_vma) 1 << power_of_two) - 1;
2779 while ((h->root.u.def.value & mask) != 0)
2780 {
2781 mask >>= 1;
2782 --power_of_two;
2783 }
027297b7 2784
91ac5911
L
2785 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2786 dynbss))
027297b7
L
2787 {
2788 /* Adjust the section alignment if needed. */
2789 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2790 power_of_two))
027297b7
L
2791 return FALSE;
2792 }
2793
91ac5911 2794 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2795 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2796
2797 /* Define the symbol as being at this point in DYNBSS. */
2798 h->root.u.def.section = dynbss;
2799 h->root.u.def.value = dynbss->size;
2800
2801 /* Increment the size of DYNBSS to make room for the symbol. */
2802 dynbss->size += h->size;
2803
f7483970
L
2804 /* No error if extern_protected_data is true. */
2805 if (h->protected_def
889c2a67
L
2806 && (!info->extern_protected_data
2807 || (info->extern_protected_data < 0
2808 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2809 info->callbacks->einfo
2810 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2811 h->root.root.string);
6cabe1ea 2812
027297b7
L
2813 return TRUE;
2814}
2815
45d6a902
AM
2816/* Adjust all external symbols pointing into SEC_MERGE sections
2817 to reflect the object merging within the sections. */
2818
28caa186 2819static bfd_boolean
268b6b39 2820_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2821{
2822 asection *sec;
2823
45d6a902
AM
2824 if ((h->root.type == bfd_link_hash_defined
2825 || h->root.type == bfd_link_hash_defweak)
2826 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2827 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2828 {
a50b1753 2829 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2830
2831 h->root.u.def.value =
2832 _bfd_merged_section_offset (output_bfd,
2833 &h->root.u.def.section,
2834 elf_section_data (sec)->sec_info,
753731ee 2835 h->root.u.def.value);
45d6a902
AM
2836 }
2837
2838 return TRUE;
2839}
986a241f
RH
2840
2841/* Returns false if the symbol referred to by H should be considered
2842 to resolve local to the current module, and true if it should be
2843 considered to bind dynamically. */
2844
2845bfd_boolean
268b6b39
AM
2846_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2847 struct bfd_link_info *info,
89a2ee5a 2848 bfd_boolean not_local_protected)
986a241f
RH
2849{
2850 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2851 const struct elf_backend_data *bed;
2852 struct elf_link_hash_table *hash_table;
986a241f
RH
2853
2854 if (h == NULL)
2855 return FALSE;
2856
2857 while (h->root.type == bfd_link_hash_indirect
2858 || h->root.type == bfd_link_hash_warning)
2859 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2860
2861 /* If it was forced local, then clearly it's not dynamic. */
2862 if (h->dynindx == -1)
2863 return FALSE;
f5385ebf 2864 if (h->forced_local)
986a241f
RH
2865 return FALSE;
2866
2867 /* Identify the cases where name binding rules say that a
2868 visible symbol resolves locally. */
0e1862bb
L
2869 binding_stays_local_p = (bfd_link_executable (info)
2870 || SYMBOLIC_BIND (info, h));
986a241f
RH
2871
2872 switch (ELF_ST_VISIBILITY (h->other))
2873 {
2874 case STV_INTERNAL:
2875 case STV_HIDDEN:
2876 return FALSE;
2877
2878 case STV_PROTECTED:
fcb93ecf
PB
2879 hash_table = elf_hash_table (info);
2880 if (!is_elf_hash_table (hash_table))
2881 return FALSE;
2882
2883 bed = get_elf_backend_data (hash_table->dynobj);
2884
986a241f
RH
2885 /* Proper resolution for function pointer equality may require
2886 that these symbols perhaps be resolved dynamically, even though
2887 we should be resolving them to the current module. */
89a2ee5a 2888 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2889 binding_stays_local_p = TRUE;
2890 break;
2891
2892 default:
986a241f
RH
2893 break;
2894 }
2895
aa37626c 2896 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2897 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2898 return TRUE;
2899
986a241f
RH
2900 /* Otherwise, the symbol is dynamic if binding rules don't tell
2901 us that it remains local. */
2902 return !binding_stays_local_p;
2903}
f6c52c13
AM
2904
2905/* Return true if the symbol referred to by H should be considered
2906 to resolve local to the current module, and false otherwise. Differs
2907 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2908 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2909 for the place where forced_local and dynindx == -1 are tested. If
2910 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2911 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2912 the symbol is local only for defined symbols.
2913 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2914 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2915 treatment of undefined weak symbols. For those that do not make
2916 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2917
2918bfd_boolean
268b6b39
AM
2919_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2920 struct bfd_link_info *info,
2921 bfd_boolean local_protected)
f6c52c13 2922{
fcb93ecf
PB
2923 const struct elf_backend_data *bed;
2924 struct elf_link_hash_table *hash_table;
2925
f6c52c13
AM
2926 /* If it's a local sym, of course we resolve locally. */
2927 if (h == NULL)
2928 return TRUE;
2929
d95edcac
L
2930 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2931 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2932 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2933 return TRUE;
2934
7e2294f9
AO
2935 /* Common symbols that become definitions don't get the DEF_REGULAR
2936 flag set, so test it first, and don't bail out. */
2937 if (ELF_COMMON_DEF_P (h))
2938 /* Do nothing. */;
f6c52c13 2939 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2940 resolve locally. The sym is either undefined or dynamic. */
2941 else if (!h->def_regular)
f6c52c13
AM
2942 return FALSE;
2943
2944 /* Forced local symbols resolve locally. */
f5385ebf 2945 if (h->forced_local)
f6c52c13
AM
2946 return TRUE;
2947
2948 /* As do non-dynamic symbols. */
2949 if (h->dynindx == -1)
2950 return TRUE;
2951
2952 /* At this point, we know the symbol is defined and dynamic. In an
2953 executable it must resolve locally, likewise when building symbolic
2954 shared libraries. */
0e1862bb 2955 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2956 return TRUE;
2957
2958 /* Now deal with defined dynamic symbols in shared libraries. Ones
2959 with default visibility might not resolve locally. */
2960 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2961 return FALSE;
2962
fcb93ecf
PB
2963 hash_table = elf_hash_table (info);
2964 if (!is_elf_hash_table (hash_table))
2965 return TRUE;
2966
2967 bed = get_elf_backend_data (hash_table->dynobj);
2968
f7483970
L
2969 /* If extern_protected_data is false, STV_PROTECTED non-function
2970 symbols are local. */
889c2a67
L
2971 if ((!info->extern_protected_data
2972 || (info->extern_protected_data < 0
2973 && !bed->extern_protected_data))
2974 && !bed->is_function_type (h->type))
1c16dfa5
L
2975 return TRUE;
2976
f6c52c13 2977 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2978 symbols be treated as dynamic symbols. If the address of a
2979 function not defined in an executable is set to that function's
2980 plt entry in the executable, then the address of the function in
2981 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2982 return local_protected;
2983}
e1918d23
AM
2984
2985/* Caches some TLS segment info, and ensures that the TLS segment vma is
2986 aligned. Returns the first TLS output section. */
2987
2988struct bfd_section *
2989_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2990{
2991 struct bfd_section *sec, *tls;
2992 unsigned int align = 0;
2993
2994 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2995 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2996 break;
2997 tls = sec;
2998
2999 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3000 if (sec->alignment_power > align)
3001 align = sec->alignment_power;
3002
3003 elf_hash_table (info)->tls_sec = tls;
3004
3005 /* Ensure the alignment of the first section is the largest alignment,
3006 so that the tls segment starts aligned. */
3007 if (tls != NULL)
3008 tls->alignment_power = align;
3009
3010 return tls;
3011}
0ad989f9
L
3012
3013/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3014static bfd_boolean
3015is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3016 Elf_Internal_Sym *sym)
3017{
a4d8e49b
L
3018 const struct elf_backend_data *bed;
3019
0ad989f9
L
3020 /* Local symbols do not count, but target specific ones might. */
3021 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3022 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3023 return FALSE;
3024
fcb93ecf 3025 bed = get_elf_backend_data (abfd);
0ad989f9 3026 /* Function symbols do not count. */
fcb93ecf 3027 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3028 return FALSE;
3029
3030 /* If the section is undefined, then so is the symbol. */
3031 if (sym->st_shndx == SHN_UNDEF)
3032 return FALSE;
3033
3034 /* If the symbol is defined in the common section, then
3035 it is a common definition and so does not count. */
a4d8e49b 3036 if (bed->common_definition (sym))
0ad989f9
L
3037 return FALSE;
3038
3039 /* If the symbol is in a target specific section then we
3040 must rely upon the backend to tell us what it is. */
3041 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3042 /* FIXME - this function is not coded yet:
3043
3044 return _bfd_is_global_symbol_definition (abfd, sym);
3045
3046 Instead for now assume that the definition is not global,
3047 Even if this is wrong, at least the linker will behave
3048 in the same way that it used to do. */
3049 return FALSE;
3050
3051 return TRUE;
3052}
3053
3054/* Search the symbol table of the archive element of the archive ABFD
3055 whose archive map contains a mention of SYMDEF, and determine if
3056 the symbol is defined in this element. */
3057static bfd_boolean
3058elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3059{
3060 Elf_Internal_Shdr * hdr;
3061 bfd_size_type symcount;
3062 bfd_size_type extsymcount;
3063 bfd_size_type extsymoff;
3064 Elf_Internal_Sym *isymbuf;
3065 Elf_Internal_Sym *isym;
3066 Elf_Internal_Sym *isymend;
3067 bfd_boolean result;
3068
3069 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3070 if (abfd == NULL)
3071 return FALSE;
3072
f0bf6bfd
L
3073 /* Return FALSE if the object has been claimed by plugin. */
3074 if (abfd->plugin_format == bfd_plugin_yes)
3075 return FALSE;
3076
0ad989f9
L
3077 if (! bfd_check_format (abfd, bfd_object))
3078 return FALSE;
3079
0ad989f9
L
3080 /* Select the appropriate symbol table. */
3081 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3082 hdr = &elf_tdata (abfd)->symtab_hdr;
3083 else
3084 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3085
3086 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3087
3088 /* The sh_info field of the symtab header tells us where the
3089 external symbols start. We don't care about the local symbols. */
3090 if (elf_bad_symtab (abfd))
3091 {
3092 extsymcount = symcount;
3093 extsymoff = 0;
3094 }
3095 else
3096 {
3097 extsymcount = symcount - hdr->sh_info;
3098 extsymoff = hdr->sh_info;
3099 }
3100
3101 if (extsymcount == 0)
3102 return FALSE;
3103
3104 /* Read in the symbol table. */
3105 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3106 NULL, NULL, NULL);
3107 if (isymbuf == NULL)
3108 return FALSE;
3109
3110 /* Scan the symbol table looking for SYMDEF. */
3111 result = FALSE;
3112 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3113 {
3114 const char *name;
3115
3116 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3117 isym->st_name);
3118 if (name == NULL)
3119 break;
3120
3121 if (strcmp (name, symdef->name) == 0)
3122 {
3123 result = is_global_data_symbol_definition (abfd, isym);
3124 break;
3125 }
3126 }
3127
3128 free (isymbuf);
3129
3130 return result;
3131}
3132\f
5a580b3a
AM
3133/* Add an entry to the .dynamic table. */
3134
3135bfd_boolean
3136_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3137 bfd_vma tag,
3138 bfd_vma val)
3139{
3140 struct elf_link_hash_table *hash_table;
3141 const struct elf_backend_data *bed;
3142 asection *s;
3143 bfd_size_type newsize;
3144 bfd_byte *newcontents;
3145 Elf_Internal_Dyn dyn;
3146
3147 hash_table = elf_hash_table (info);
3148 if (! is_elf_hash_table (hash_table))
3149 return FALSE;
3150
3151 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3152 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3153 BFD_ASSERT (s != NULL);
3154
eea6121a 3155 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3156 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3157 if (newcontents == NULL)
3158 return FALSE;
3159
3160 dyn.d_tag = tag;
3161 dyn.d_un.d_val = val;
eea6121a 3162 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3163
eea6121a 3164 s->size = newsize;
5a580b3a
AM
3165 s->contents = newcontents;
3166
3167 return TRUE;
3168}
3169
3170/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3171 otherwise just check whether one already exists. Returns -1 on error,
3172 1 if a DT_NEEDED tag already exists, and 0 on success. */
3173
4ad4eba5 3174static int
7e9f0867
AM
3175elf_add_dt_needed_tag (bfd *abfd,
3176 struct bfd_link_info *info,
4ad4eba5
AM
3177 const char *soname,
3178 bfd_boolean do_it)
5a580b3a
AM
3179{
3180 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3181 bfd_size_type strindex;
3182
7e9f0867
AM
3183 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3184 return -1;
3185
5a580b3a 3186 hash_table = elf_hash_table (info);
5a580b3a
AM
3187 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3188 if (strindex == (bfd_size_type) -1)
3189 return -1;
3190
02be4619 3191 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3192 {
3193 asection *sdyn;
3194 const struct elf_backend_data *bed;
3195 bfd_byte *extdyn;
3196
3197 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3198 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3199 if (sdyn != NULL)
3200 for (extdyn = sdyn->contents;
3201 extdyn < sdyn->contents + sdyn->size;
3202 extdyn += bed->s->sizeof_dyn)
3203 {
3204 Elf_Internal_Dyn dyn;
5a580b3a 3205
7e9f0867
AM
3206 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3207 if (dyn.d_tag == DT_NEEDED
3208 && dyn.d_un.d_val == strindex)
3209 {
3210 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3211 return 1;
3212 }
3213 }
5a580b3a
AM
3214 }
3215
3216 if (do_it)
3217 {
7e9f0867
AM
3218 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3219 return -1;
3220
5a580b3a
AM
3221 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3222 return -1;
3223 }
3224 else
3225 /* We were just checking for existence of the tag. */
3226 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3227
3228 return 0;
3229}
3230
010e5ae2
AM
3231static bfd_boolean
3232on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3233{
3234 for (; needed != NULL; needed = needed->next)
1240be6b
AM
3235 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3236 && strcmp (soname, needed->name) == 0)
010e5ae2
AM
3237 return TRUE;
3238
3239 return FALSE;
3240}
3241
14160578 3242/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3243static int
3244elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3245{
3246 const struct elf_link_hash_entry *h1;
3247 const struct elf_link_hash_entry *h2;
10b7e05b 3248 bfd_signed_vma vdiff;
5a580b3a
AM
3249
3250 h1 = *(const struct elf_link_hash_entry **) arg1;
3251 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3252 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3253 if (vdiff != 0)
3254 return vdiff > 0 ? 1 : -1;
3255 else
3256 {
d3435ae8 3257 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3258 if (sdiff != 0)
3259 return sdiff > 0 ? 1 : -1;
3260 }
14160578
AM
3261 vdiff = h1->size - h2->size;
3262 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3263}
4ad4eba5 3264
5a580b3a
AM
3265/* This function is used to adjust offsets into .dynstr for
3266 dynamic symbols. This is called via elf_link_hash_traverse. */
3267
3268static bfd_boolean
3269elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3270{
a50b1753 3271 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3272
5a580b3a
AM
3273 if (h->dynindx != -1)
3274 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3275 return TRUE;
3276}
3277
3278/* Assign string offsets in .dynstr, update all structures referencing
3279 them. */
3280
4ad4eba5
AM
3281static bfd_boolean
3282elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3283{
3284 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3285 struct elf_link_local_dynamic_entry *entry;
3286 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3287 bfd *dynobj = hash_table->dynobj;
3288 asection *sdyn;
3289 bfd_size_type size;
3290 const struct elf_backend_data *bed;
3291 bfd_byte *extdyn;
3292
3293 _bfd_elf_strtab_finalize (dynstr);
3294 size = _bfd_elf_strtab_size (dynstr);
3295
3296 bed = get_elf_backend_data (dynobj);
3d4d4302 3297 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3298 BFD_ASSERT (sdyn != NULL);
3299
3300 /* Update all .dynamic entries referencing .dynstr strings. */
3301 for (extdyn = sdyn->contents;
eea6121a 3302 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3303 extdyn += bed->s->sizeof_dyn)
3304 {
3305 Elf_Internal_Dyn dyn;
3306
3307 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3308 switch (dyn.d_tag)
3309 {
3310 case DT_STRSZ:
3311 dyn.d_un.d_val = size;
3312 break;
3313 case DT_NEEDED:
3314 case DT_SONAME:
3315 case DT_RPATH:
3316 case DT_RUNPATH:
3317 case DT_FILTER:
3318 case DT_AUXILIARY:
7ee314fa
AM
3319 case DT_AUDIT:
3320 case DT_DEPAUDIT:
5a580b3a
AM
3321 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3322 break;
3323 default:
3324 continue;
3325 }
3326 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3327 }
3328
3329 /* Now update local dynamic symbols. */
3330 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3331 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3332 entry->isym.st_name);
3333
3334 /* And the rest of dynamic symbols. */
3335 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3336
3337 /* Adjust version definitions. */
3338 if (elf_tdata (output_bfd)->cverdefs)
3339 {
3340 asection *s;
3341 bfd_byte *p;
3342 bfd_size_type i;
3343 Elf_Internal_Verdef def;
3344 Elf_Internal_Verdaux defaux;
3345
3d4d4302 3346 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3347 p = s->contents;
3348 do
3349 {
3350 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3351 &def);
3352 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3353 if (def.vd_aux != sizeof (Elf_External_Verdef))
3354 continue;
5a580b3a
AM
3355 for (i = 0; i < def.vd_cnt; ++i)
3356 {
3357 _bfd_elf_swap_verdaux_in (output_bfd,
3358 (Elf_External_Verdaux *) p, &defaux);
3359 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3360 defaux.vda_name);
3361 _bfd_elf_swap_verdaux_out (output_bfd,
3362 &defaux, (Elf_External_Verdaux *) p);
3363 p += sizeof (Elf_External_Verdaux);
3364 }
3365 }
3366 while (def.vd_next);
3367 }
3368
3369 /* Adjust version references. */
3370 if (elf_tdata (output_bfd)->verref)
3371 {
3372 asection *s;
3373 bfd_byte *p;
3374 bfd_size_type i;
3375 Elf_Internal_Verneed need;
3376 Elf_Internal_Vernaux needaux;
3377
3d4d4302 3378 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3379 p = s->contents;
3380 do
3381 {
3382 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3383 &need);
3384 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3385 _bfd_elf_swap_verneed_out (output_bfd, &need,
3386 (Elf_External_Verneed *) p);
3387 p += sizeof (Elf_External_Verneed);
3388 for (i = 0; i < need.vn_cnt; ++i)
3389 {
3390 _bfd_elf_swap_vernaux_in (output_bfd,
3391 (Elf_External_Vernaux *) p, &needaux);
3392 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3393 needaux.vna_name);
3394 _bfd_elf_swap_vernaux_out (output_bfd,
3395 &needaux,
3396 (Elf_External_Vernaux *) p);
3397 p += sizeof (Elf_External_Vernaux);
3398 }
3399 }
3400 while (need.vn_next);
3401 }
3402
3403 return TRUE;
3404}
3405\f
13285a1b
AM
3406/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3407 The default is to only match when the INPUT and OUTPUT are exactly
3408 the same target. */
3409
3410bfd_boolean
3411_bfd_elf_default_relocs_compatible (const bfd_target *input,
3412 const bfd_target *output)
3413{
3414 return input == output;
3415}
3416
3417/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3418 This version is used when different targets for the same architecture
3419 are virtually identical. */
3420
3421bfd_boolean
3422_bfd_elf_relocs_compatible (const bfd_target *input,
3423 const bfd_target *output)
3424{
3425 const struct elf_backend_data *obed, *ibed;
3426
3427 if (input == output)
3428 return TRUE;
3429
3430 ibed = xvec_get_elf_backend_data (input);
3431 obed = xvec_get_elf_backend_data (output);
3432
3433 if (ibed->arch != obed->arch)
3434 return FALSE;
3435
3436 /* If both backends are using this function, deem them compatible. */
3437 return ibed->relocs_compatible == obed->relocs_compatible;
3438}
3439
e5034e59
AM
3440/* Make a special call to the linker "notice" function to tell it that
3441 we are about to handle an as-needed lib, or have finished
1b786873 3442 processing the lib. */
e5034e59
AM
3443
3444bfd_boolean
3445_bfd_elf_notice_as_needed (bfd *ibfd,
3446 struct bfd_link_info *info,
3447 enum notice_asneeded_action act)
3448{
46135103 3449 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3450}
3451
4ad4eba5
AM
3452/* Add symbols from an ELF object file to the linker hash table. */
3453
3454static bfd_boolean
3455elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3456{
a0c402a5 3457 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3458 Elf_Internal_Shdr *hdr;
3459 bfd_size_type symcount;
3460 bfd_size_type extsymcount;
3461 bfd_size_type extsymoff;
3462 struct elf_link_hash_entry **sym_hash;
3463 bfd_boolean dynamic;
3464 Elf_External_Versym *extversym = NULL;
3465 Elf_External_Versym *ever;
3466 struct elf_link_hash_entry *weaks;
3467 struct elf_link_hash_entry **nondeflt_vers = NULL;
3468 bfd_size_type nondeflt_vers_cnt = 0;
3469 Elf_Internal_Sym *isymbuf = NULL;
3470 Elf_Internal_Sym *isym;
3471 Elf_Internal_Sym *isymend;
3472 const struct elf_backend_data *bed;
3473 bfd_boolean add_needed;
66eb6687 3474 struct elf_link_hash_table *htab;
4ad4eba5 3475 bfd_size_type amt;
66eb6687 3476 void *alloc_mark = NULL;
4f87808c
AM
3477 struct bfd_hash_entry **old_table = NULL;
3478 unsigned int old_size = 0;
3479 unsigned int old_count = 0;
66eb6687 3480 void *old_tab = NULL;
66eb6687
AM
3481 void *old_ent;
3482 struct bfd_link_hash_entry *old_undefs = NULL;
3483 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3484 long old_dynsymcount = 0;
a4542f1b 3485 bfd_size_type old_dynstr_size = 0;
66eb6687 3486 size_t tabsize = 0;
db6a5d5f 3487 asection *s;
29a9f53e 3488 bfd_boolean just_syms;
4ad4eba5 3489
66eb6687 3490 htab = elf_hash_table (info);
4ad4eba5 3491 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3492
3493 if ((abfd->flags & DYNAMIC) == 0)
3494 dynamic = FALSE;
3495 else
3496 {
3497 dynamic = TRUE;
3498
3499 /* You can't use -r against a dynamic object. Also, there's no
3500 hope of using a dynamic object which does not exactly match
3501 the format of the output file. */
0e1862bb 3502 if (bfd_link_relocatable (info)
66eb6687 3503 || !is_elf_hash_table (htab)
f13a99db 3504 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3505 {
0e1862bb 3506 if (bfd_link_relocatable (info))
9a0789ec
NC
3507 bfd_set_error (bfd_error_invalid_operation);
3508 else
3509 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3510 goto error_return;
3511 }
3512 }
3513
a0c402a5
L
3514 ehdr = elf_elfheader (abfd);
3515 if (info->warn_alternate_em
3516 && bed->elf_machine_code != ehdr->e_machine
3517 && ((bed->elf_machine_alt1 != 0
3518 && ehdr->e_machine == bed->elf_machine_alt1)
3519 || (bed->elf_machine_alt2 != 0
3520 && ehdr->e_machine == bed->elf_machine_alt2)))
3521 info->callbacks->einfo
3522 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3523 ehdr->e_machine, abfd, bed->elf_machine_code);
3524
4ad4eba5
AM
3525 /* As a GNU extension, any input sections which are named
3526 .gnu.warning.SYMBOL are treated as warning symbols for the given
3527 symbol. This differs from .gnu.warning sections, which generate
3528 warnings when they are included in an output file. */
dd98f8d2 3529 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3530 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3531 {
db6a5d5f 3532 const char *name;
4ad4eba5 3533
db6a5d5f
AM
3534 name = bfd_get_section_name (abfd, s);
3535 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3536 {
db6a5d5f
AM
3537 char *msg;
3538 bfd_size_type sz;
3539
3540 name += sizeof ".gnu.warning." - 1;
3541
3542 /* If this is a shared object, then look up the symbol
3543 in the hash table. If it is there, and it is already
3544 been defined, then we will not be using the entry
3545 from this shared object, so we don't need to warn.
3546 FIXME: If we see the definition in a regular object
3547 later on, we will warn, but we shouldn't. The only
3548 fix is to keep track of what warnings we are supposed
3549 to emit, and then handle them all at the end of the
3550 link. */
3551 if (dynamic)
4ad4eba5 3552 {
db6a5d5f
AM
3553 struct elf_link_hash_entry *h;
3554
3555 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3556
3557 /* FIXME: What about bfd_link_hash_common? */
3558 if (h != NULL
3559 && (h->root.type == bfd_link_hash_defined
3560 || h->root.type == bfd_link_hash_defweak))
3561 continue;
3562 }
4ad4eba5 3563
db6a5d5f
AM
3564 sz = s->size;
3565 msg = (char *) bfd_alloc (abfd, sz + 1);
3566 if (msg == NULL)
3567 goto error_return;
4ad4eba5 3568
db6a5d5f
AM
3569 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3570 goto error_return;
4ad4eba5 3571
db6a5d5f 3572 msg[sz] = '\0';
4ad4eba5 3573
db6a5d5f
AM
3574 if (! (_bfd_generic_link_add_one_symbol
3575 (info, abfd, name, BSF_WARNING, s, 0, msg,
3576 FALSE, bed->collect, NULL)))
3577 goto error_return;
4ad4eba5 3578
0e1862bb 3579 if (bfd_link_executable (info))
db6a5d5f
AM
3580 {
3581 /* Clobber the section size so that the warning does
3582 not get copied into the output file. */
3583 s->size = 0;
11d2f718 3584
db6a5d5f
AM
3585 /* Also set SEC_EXCLUDE, so that symbols defined in
3586 the warning section don't get copied to the output. */
3587 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3588 }
3589 }
3590 }
3591
29a9f53e
L
3592 just_syms = ((s = abfd->sections) != NULL
3593 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3594
4ad4eba5
AM
3595 add_needed = TRUE;
3596 if (! dynamic)
3597 {
3598 /* If we are creating a shared library, create all the dynamic
3599 sections immediately. We need to attach them to something,
3600 so we attach them to this BFD, provided it is the right
29a9f53e
L
3601 format and is not from ld --just-symbols. FIXME: If there
3602 are no input BFD's of the same format as the output, we can't
3603 make a shared library. */
3604 if (!just_syms
0e1862bb 3605 && bfd_link_pic (info)
66eb6687 3606 && is_elf_hash_table (htab)
f13a99db 3607 && info->output_bfd->xvec == abfd->xvec
66eb6687 3608 && !htab->dynamic_sections_created)
4ad4eba5
AM
3609 {
3610 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3611 goto error_return;
3612 }
3613 }
66eb6687 3614 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3615 goto error_return;
3616 else
3617 {
4ad4eba5 3618 const char *soname = NULL;
7ee314fa 3619 char *audit = NULL;
4ad4eba5
AM
3620 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3621 int ret;
3622
3623 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3624 ld shouldn't allow it. */
29a9f53e 3625 if (just_syms)
92fd189d 3626 abort ();
4ad4eba5
AM
3627
3628 /* If this dynamic lib was specified on the command line with
3629 --as-needed in effect, then we don't want to add a DT_NEEDED
3630 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3631 in by another lib's DT_NEEDED. When --no-add-needed is used
3632 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3633 any dynamic library in DT_NEEDED tags in the dynamic lib at
3634 all. */
3635 add_needed = (elf_dyn_lib_class (abfd)
3636 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3637 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3638
3639 s = bfd_get_section_by_name (abfd, ".dynamic");
3640 if (s != NULL)
3641 {
3642 bfd_byte *dynbuf;
3643 bfd_byte *extdyn;
cb33740c 3644 unsigned int elfsec;
4ad4eba5
AM
3645 unsigned long shlink;
3646
eea6121a 3647 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3648 {
3649error_free_dyn:
3650 free (dynbuf);
3651 goto error_return;
3652 }
4ad4eba5
AM
3653
3654 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3655 if (elfsec == SHN_BAD)
4ad4eba5
AM
3656 goto error_free_dyn;
3657 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3658
3659 for (extdyn = dynbuf;
eea6121a 3660 extdyn < dynbuf + s->size;
4ad4eba5
AM
3661 extdyn += bed->s->sizeof_dyn)
3662 {
3663 Elf_Internal_Dyn dyn;
3664
3665 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3666 if (dyn.d_tag == DT_SONAME)
3667 {
3668 unsigned int tagv = dyn.d_un.d_val;
3669 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3670 if (soname == NULL)
3671 goto error_free_dyn;
3672 }
3673 if (dyn.d_tag == DT_NEEDED)
3674 {
3675 struct bfd_link_needed_list *n, **pn;
3676 char *fnm, *anm;
3677 unsigned int tagv = dyn.d_un.d_val;
3678
3679 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3680 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3681 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3682 if (n == NULL || fnm == NULL)
3683 goto error_free_dyn;
3684 amt = strlen (fnm) + 1;
a50b1753 3685 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3686 if (anm == NULL)
3687 goto error_free_dyn;
3688 memcpy (anm, fnm, amt);
3689 n->name = anm;
3690 n->by = abfd;
3691 n->next = NULL;
66eb6687 3692 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3693 ;
3694 *pn = n;
3695 }
3696 if (dyn.d_tag == DT_RUNPATH)
3697 {
3698 struct bfd_link_needed_list *n, **pn;
3699 char *fnm, *anm;
3700 unsigned int tagv = dyn.d_un.d_val;
3701
3702 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3703 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3704 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3705 if (n == NULL || fnm == NULL)
3706 goto error_free_dyn;
3707 amt = strlen (fnm) + 1;
a50b1753 3708 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3709 if (anm == NULL)
3710 goto error_free_dyn;
3711 memcpy (anm, fnm, amt);
3712 n->name = anm;
3713 n->by = abfd;
3714 n->next = NULL;
3715 for (pn = & runpath;
3716 *pn != NULL;
3717 pn = &(*pn)->next)
3718 ;
3719 *pn = n;
3720 }
3721 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3722 if (!runpath && dyn.d_tag == DT_RPATH)
3723 {
3724 struct bfd_link_needed_list *n, **pn;
3725 char *fnm, *anm;
3726 unsigned int tagv = dyn.d_un.d_val;
3727
3728 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3729 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3730 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3731 if (n == NULL || fnm == NULL)
3732 goto error_free_dyn;
3733 amt = strlen (fnm) + 1;
a50b1753 3734 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3735 if (anm == NULL)
f8703194 3736 goto error_free_dyn;
4ad4eba5
AM
3737 memcpy (anm, fnm, amt);
3738 n->name = anm;
3739 n->by = abfd;
3740 n->next = NULL;
3741 for (pn = & rpath;
3742 *pn != NULL;
3743 pn = &(*pn)->next)
3744 ;
3745 *pn = n;
3746 }
7ee314fa
AM
3747 if (dyn.d_tag == DT_AUDIT)
3748 {
3749 unsigned int tagv = dyn.d_un.d_val;
3750 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3751 }
4ad4eba5
AM
3752 }
3753
3754 free (dynbuf);
3755 }
3756
3757 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3758 frees all more recently bfd_alloc'd blocks as well. */
3759 if (runpath)
3760 rpath = runpath;
3761
3762 if (rpath)
3763 {
3764 struct bfd_link_needed_list **pn;
66eb6687 3765 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3766 ;
3767 *pn = rpath;
3768 }
3769
3770 /* We do not want to include any of the sections in a dynamic
3771 object in the output file. We hack by simply clobbering the
3772 list of sections in the BFD. This could be handled more
3773 cleanly by, say, a new section flag; the existing
3774 SEC_NEVER_LOAD flag is not the one we want, because that one
3775 still implies that the section takes up space in the output
3776 file. */
3777 bfd_section_list_clear (abfd);
3778
4ad4eba5
AM
3779 /* Find the name to use in a DT_NEEDED entry that refers to this
3780 object. If the object has a DT_SONAME entry, we use it.
3781 Otherwise, if the generic linker stuck something in
3782 elf_dt_name, we use that. Otherwise, we just use the file
3783 name. */
3784 if (soname == NULL || *soname == '\0')
3785 {
3786 soname = elf_dt_name (abfd);
3787 if (soname == NULL || *soname == '\0')
3788 soname = bfd_get_filename (abfd);
3789 }
3790
3791 /* Save the SONAME because sometimes the linker emulation code
3792 will need to know it. */
3793 elf_dt_name (abfd) = soname;
3794
7e9f0867 3795 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3796 if (ret < 0)
3797 goto error_return;
3798
3799 /* If we have already included this dynamic object in the
3800 link, just ignore it. There is no reason to include a
3801 particular dynamic object more than once. */
3802 if (ret > 0)
3803 return TRUE;
7ee314fa
AM
3804
3805 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3806 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3807 }
3808
3809 /* If this is a dynamic object, we always link against the .dynsym
3810 symbol table, not the .symtab symbol table. The dynamic linker
3811 will only see the .dynsym symbol table, so there is no reason to
3812 look at .symtab for a dynamic object. */
3813
3814 if (! dynamic || elf_dynsymtab (abfd) == 0)
3815 hdr = &elf_tdata (abfd)->symtab_hdr;
3816 else
3817 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3818
3819 symcount = hdr->sh_size / bed->s->sizeof_sym;
3820
3821 /* The sh_info field of the symtab header tells us where the
3822 external symbols start. We don't care about the local symbols at
3823 this point. */
3824 if (elf_bad_symtab (abfd))
3825 {
3826 extsymcount = symcount;
3827 extsymoff = 0;
3828 }
3829 else
3830 {
3831 extsymcount = symcount - hdr->sh_info;
3832 extsymoff = hdr->sh_info;
3833 }
3834
f45794cb 3835 sym_hash = elf_sym_hashes (abfd);
012b2306 3836 if (extsymcount != 0)
4ad4eba5
AM
3837 {
3838 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3839 NULL, NULL, NULL);
3840 if (isymbuf == NULL)
3841 goto error_return;
3842
4ad4eba5 3843 if (sym_hash == NULL)
012b2306
AM
3844 {
3845 /* We store a pointer to the hash table entry for each
3846 external symbol. */
3847 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3848 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3849 if (sym_hash == NULL)
3850 goto error_free_sym;
3851 elf_sym_hashes (abfd) = sym_hash;
3852 }
4ad4eba5
AM
3853 }
3854
3855 if (dynamic)
3856 {
3857 /* Read in any version definitions. */
fc0e6df6
PB
3858 if (!_bfd_elf_slurp_version_tables (abfd,
3859 info->default_imported_symver))
4ad4eba5
AM
3860 goto error_free_sym;
3861
3862 /* Read in the symbol versions, but don't bother to convert them
3863 to internal format. */
3864 if (elf_dynversym (abfd) != 0)
3865 {
3866 Elf_Internal_Shdr *versymhdr;
3867
3868 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3869 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3870 if (extversym == NULL)
3871 goto error_free_sym;
3872 amt = versymhdr->sh_size;
3873 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3874 || bfd_bread (extversym, amt, abfd) != amt)
3875 goto error_free_vers;
3876 }
3877 }
3878
66eb6687
AM
3879 /* If we are loading an as-needed shared lib, save the symbol table
3880 state before we start adding symbols. If the lib turns out
3881 to be unneeded, restore the state. */
3882 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3883 {
3884 unsigned int i;
3885 size_t entsize;
3886
3887 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3888 {
3889 struct bfd_hash_entry *p;
2de92251 3890 struct elf_link_hash_entry *h;
66eb6687
AM
3891
3892 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3893 {
3894 h = (struct elf_link_hash_entry *) p;
3895 entsize += htab->root.table.entsize;
3896 if (h->root.type == bfd_link_hash_warning)
3897 entsize += htab->root.table.entsize;
3898 }
66eb6687
AM
3899 }
3900
3901 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 3902 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
3903 if (old_tab == NULL)
3904 goto error_free_vers;
3905
3906 /* Remember the current objalloc pointer, so that all mem for
3907 symbols added can later be reclaimed. */
3908 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3909 if (alloc_mark == NULL)
3910 goto error_free_vers;
3911
5061a885
AM
3912 /* Make a special call to the linker "notice" function to
3913 tell it that we are about to handle an as-needed lib. */
e5034e59 3914 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 3915 goto error_free_vers;
5061a885 3916
f45794cb
AM
3917 /* Clone the symbol table. Remember some pointers into the
3918 symbol table, and dynamic symbol count. */
3919 old_ent = (char *) old_tab + tabsize;
66eb6687 3920 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
3921 old_undefs = htab->root.undefs;
3922 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3923 old_table = htab->root.table.table;
3924 old_size = htab->root.table.size;
3925 old_count = htab->root.table.count;
66eb6687 3926 old_dynsymcount = htab->dynsymcount;
a4542f1b 3927 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
3928
3929 for (i = 0; i < htab->root.table.size; i++)
3930 {
3931 struct bfd_hash_entry *p;
2de92251 3932 struct elf_link_hash_entry *h;
66eb6687
AM
3933
3934 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3935 {
3936 memcpy (old_ent, p, htab->root.table.entsize);
3937 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3938 h = (struct elf_link_hash_entry *) p;
3939 if (h->root.type == bfd_link_hash_warning)
3940 {
3941 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3942 old_ent = (char *) old_ent + htab->root.table.entsize;
3943 }
66eb6687
AM
3944 }
3945 }
3946 }
4ad4eba5 3947
66eb6687 3948 weaks = NULL;
4ad4eba5
AM
3949 ever = extversym != NULL ? extversym + extsymoff : NULL;
3950 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3951 isym < isymend;
3952 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3953 {
3954 int bind;
3955 bfd_vma value;
af44c138 3956 asection *sec, *new_sec;
4ad4eba5
AM
3957 flagword flags;
3958 const char *name;
3959 struct elf_link_hash_entry *h;
90c984fc 3960 struct elf_link_hash_entry *hi;
4ad4eba5
AM
3961 bfd_boolean definition;
3962 bfd_boolean size_change_ok;
3963 bfd_boolean type_change_ok;
3964 bfd_boolean new_weakdef;
37a9e49a
L
3965 bfd_boolean new_weak;
3966 bfd_boolean old_weak;
4ad4eba5 3967 bfd_boolean override;
a4d8e49b 3968 bfd_boolean common;
4ad4eba5
AM
3969 unsigned int old_alignment;
3970 bfd *old_bfd;
6e33951e 3971 bfd_boolean matched;
4ad4eba5
AM
3972
3973 override = FALSE;
3974
3975 flags = BSF_NO_FLAGS;
3976 sec = NULL;
3977 value = isym->st_value;
a4d8e49b 3978 common = bed->common_definition (isym);
4ad4eba5
AM
3979
3980 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3981 switch (bind)
4ad4eba5 3982 {
3e7a7d11 3983 case STB_LOCAL:
4ad4eba5
AM
3984 /* This should be impossible, since ELF requires that all
3985 global symbols follow all local symbols, and that sh_info
3986 point to the first global symbol. Unfortunately, Irix 5
3987 screws this up. */
3988 continue;
3e7a7d11
NC
3989
3990 case STB_GLOBAL:
a4d8e49b 3991 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3992 flags = BSF_GLOBAL;
3e7a7d11
NC
3993 break;
3994
3995 case STB_WEAK:
3996 flags = BSF_WEAK;
3997 break;
3998
3999 case STB_GNU_UNIQUE:
4000 flags = BSF_GNU_UNIQUE;
4001 break;
4002
4003 default:
4ad4eba5 4004 /* Leave it up to the processor backend. */
3e7a7d11 4005 break;
4ad4eba5
AM
4006 }
4007
4008 if (isym->st_shndx == SHN_UNDEF)
4009 sec = bfd_und_section_ptr;
cb33740c
AM
4010 else if (isym->st_shndx == SHN_ABS)
4011 sec = bfd_abs_section_ptr;
4012 else if (isym->st_shndx == SHN_COMMON)
4013 {
4014 sec = bfd_com_section_ptr;
4015 /* What ELF calls the size we call the value. What ELF
4016 calls the value we call the alignment. */
4017 value = isym->st_size;
4018 }
4019 else
4ad4eba5
AM
4020 {
4021 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4022 if (sec == NULL)
4023 sec = bfd_abs_section_ptr;
dbaa2011 4024 else if (discarded_section (sec))
529fcb95 4025 {
e5d08002
L
4026 /* Symbols from discarded section are undefined. We keep
4027 its visibility. */
529fcb95
PB
4028 sec = bfd_und_section_ptr;
4029 isym->st_shndx = SHN_UNDEF;
4030 }
4ad4eba5
AM
4031 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4032 value -= sec->vma;
4033 }
4ad4eba5
AM
4034
4035 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4036 isym->st_name);
4037 if (name == NULL)
4038 goto error_free_vers;
4039
4040 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4041 && (abfd->flags & BFD_PLUGIN) != 0)
4042 {
4043 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4044
4045 if (xc == NULL)
4046 {
4047 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4048 | SEC_EXCLUDE);
4049 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4050 if (xc == NULL)
4051 goto error_free_vers;
4052 }
4053 sec = xc;
4054 }
4055 else if (isym->st_shndx == SHN_COMMON
4056 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4057 && !bfd_link_relocatable (info))
4ad4eba5
AM
4058 {
4059 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4060
4061 if (tcomm == NULL)
4062 {
02d00247
AM
4063 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4064 | SEC_LINKER_CREATED);
4065 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4066 if (tcomm == NULL)
4ad4eba5
AM
4067 goto error_free_vers;
4068 }
4069 sec = tcomm;
4070 }
66eb6687 4071 else if (bed->elf_add_symbol_hook)
4ad4eba5 4072 {
66eb6687
AM
4073 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4074 &sec, &value))
4ad4eba5
AM
4075 goto error_free_vers;
4076
4077 /* The hook function sets the name to NULL if this symbol
4078 should be skipped for some reason. */
4079 if (name == NULL)
4080 continue;
4081 }
4082
4083 /* Sanity check that all possibilities were handled. */
4084 if (sec == NULL)
4085 {
4086 bfd_set_error (bfd_error_bad_value);
4087 goto error_free_vers;
4088 }
4089
191c0c42
AM
4090 /* Silently discard TLS symbols from --just-syms. There's
4091 no way to combine a static TLS block with a new TLS block
4092 for this executable. */
4093 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4094 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4095 continue;
4096
4ad4eba5
AM
4097 if (bfd_is_und_section (sec)
4098 || bfd_is_com_section (sec))
4099 definition = FALSE;
4100 else
4101 definition = TRUE;
4102
4103 size_change_ok = FALSE;
66eb6687 4104 type_change_ok = bed->type_change_ok;
37a9e49a 4105 old_weak = FALSE;
6e33951e 4106 matched = FALSE;
4ad4eba5
AM
4107 old_alignment = 0;
4108 old_bfd = NULL;
af44c138 4109 new_sec = sec;
4ad4eba5 4110
66eb6687 4111 if (is_elf_hash_table (htab))
4ad4eba5
AM
4112 {
4113 Elf_Internal_Versym iver;
4114 unsigned int vernum = 0;
4115 bfd_boolean skip;
4116
fc0e6df6 4117 if (ever == NULL)
4ad4eba5 4118 {
fc0e6df6
PB
4119 if (info->default_imported_symver)
4120 /* Use the default symbol version created earlier. */
4121 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4122 else
4123 iver.vs_vers = 0;
4124 }
4125 else
4126 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4127
4128 vernum = iver.vs_vers & VERSYM_VERSION;
4129
4130 /* If this is a hidden symbol, or if it is not version
4131 1, we append the version name to the symbol name.
cc86ff91
EB
4132 However, we do not modify a non-hidden absolute symbol
4133 if it is not a function, because it might be the version
4134 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4135 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4136 || (vernum > 1
4137 && (!bfd_is_abs_section (sec)
4138 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4139 {
4140 const char *verstr;
4141 size_t namelen, verlen, newlen;
4142 char *newname, *p;
4143
4144 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4145 {
fc0e6df6
PB
4146 if (vernum > elf_tdata (abfd)->cverdefs)
4147 verstr = NULL;
4148 else if (vernum > 1)
4149 verstr =
4150 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4151 else
4152 verstr = "";
4ad4eba5 4153
fc0e6df6 4154 if (verstr == NULL)
4ad4eba5 4155 {
fc0e6df6
PB
4156 (*_bfd_error_handler)
4157 (_("%B: %s: invalid version %u (max %d)"),
4158 abfd, name, vernum,
4159 elf_tdata (abfd)->cverdefs);
4160 bfd_set_error (bfd_error_bad_value);
4161 goto error_free_vers;
4ad4eba5 4162 }
fc0e6df6
PB
4163 }
4164 else
4165 {
4166 /* We cannot simply test for the number of
4167 entries in the VERNEED section since the
4168 numbers for the needed versions do not start
4169 at 0. */
4170 Elf_Internal_Verneed *t;
4171
4172 verstr = NULL;
4173 for (t = elf_tdata (abfd)->verref;
4174 t != NULL;
4175 t = t->vn_nextref)
4ad4eba5 4176 {
fc0e6df6 4177 Elf_Internal_Vernaux *a;
4ad4eba5 4178
fc0e6df6
PB
4179 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4180 {
4181 if (a->vna_other == vernum)
4ad4eba5 4182 {
fc0e6df6
PB
4183 verstr = a->vna_nodename;
4184 break;
4ad4eba5 4185 }
4ad4eba5 4186 }
fc0e6df6
PB
4187 if (a != NULL)
4188 break;
4189 }
4190 if (verstr == NULL)
4191 {
4192 (*_bfd_error_handler)
4193 (_("%B: %s: invalid needed version %d"),
4194 abfd, name, vernum);
4195 bfd_set_error (bfd_error_bad_value);
4196 goto error_free_vers;
4ad4eba5 4197 }
4ad4eba5 4198 }
fc0e6df6
PB
4199
4200 namelen = strlen (name);
4201 verlen = strlen (verstr);
4202 newlen = namelen + verlen + 2;
4203 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4204 && isym->st_shndx != SHN_UNDEF)
4205 ++newlen;
4206
a50b1753 4207 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4208 if (newname == NULL)
4209 goto error_free_vers;
4210 memcpy (newname, name, namelen);
4211 p = newname + namelen;
4212 *p++ = ELF_VER_CHR;
4213 /* If this is a defined non-hidden version symbol,
4214 we add another @ to the name. This indicates the
4215 default version of the symbol. */
4216 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4217 && isym->st_shndx != SHN_UNDEF)
4218 *p++ = ELF_VER_CHR;
4219 memcpy (p, verstr, verlen + 1);
4220
4221 name = newname;
4ad4eba5
AM
4222 }
4223
cd3416da
AM
4224 /* If this symbol has default visibility and the user has
4225 requested we not re-export it, then mark it as hidden. */
a0d49154 4226 if (!bfd_is_und_section (sec)
cd3416da 4227 && !dynamic
ce875075 4228 && abfd->no_export
cd3416da
AM
4229 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4230 isym->st_other = (STV_HIDDEN
4231 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4232
4f3fedcf
AM
4233 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4234 sym_hash, &old_bfd, &old_weak,
4235 &old_alignment, &skip, &override,
6e33951e
L
4236 &type_change_ok, &size_change_ok,
4237 &matched))
4ad4eba5
AM
4238 goto error_free_vers;
4239
4240 if (skip)
4241 continue;
4242
6e33951e
L
4243 /* Override a definition only if the new symbol matches the
4244 existing one. */
4245 if (override && matched)
4ad4eba5
AM
4246 definition = FALSE;
4247
4248 h = *sym_hash;
4249 while (h->root.type == bfd_link_hash_indirect
4250 || h->root.type == bfd_link_hash_warning)
4251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4252
4ad4eba5 4253 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4254 && vernum > 1
4255 && definition)
4256 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4257 }
4258
4259 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4260 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4261 (struct bfd_link_hash_entry **) sym_hash)))
4262 goto error_free_vers;
4263
4264 h = *sym_hash;
90c984fc
L
4265 /* We need to make sure that indirect symbol dynamic flags are
4266 updated. */
4267 hi = h;
4ad4eba5
AM
4268 while (h->root.type == bfd_link_hash_indirect
4269 || h->root.type == bfd_link_hash_warning)
4270 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4271
4ad4eba5
AM
4272 *sym_hash = h;
4273
37a9e49a 4274 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4275 new_weakdef = FALSE;
4276 if (dynamic
4277 && definition
37a9e49a 4278 && new_weak
fcb93ecf 4279 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4280 && is_elf_hash_table (htab)
f6e332e6 4281 && h->u.weakdef == NULL)
4ad4eba5
AM
4282 {
4283 /* Keep a list of all weak defined non function symbols from
4284 a dynamic object, using the weakdef field. Later in this
4285 function we will set the weakdef field to the correct
4286 value. We only put non-function symbols from dynamic
4287 objects on this list, because that happens to be the only
4288 time we need to know the normal symbol corresponding to a
4289 weak symbol, and the information is time consuming to
4290 figure out. If the weakdef field is not already NULL,
4291 then this symbol was already defined by some previous
4292 dynamic object, and we will be using that previous
4293 definition anyhow. */
4294
f6e332e6 4295 h->u.weakdef = weaks;
4ad4eba5
AM
4296 weaks = h;
4297 new_weakdef = TRUE;
4298 }
4299
4300 /* Set the alignment of a common symbol. */
a4d8e49b 4301 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4302 && h->root.type == bfd_link_hash_common)
4303 {
4304 unsigned int align;
4305
a4d8e49b 4306 if (common)
af44c138
L
4307 align = bfd_log2 (isym->st_value);
4308 else
4309 {
4310 /* The new symbol is a common symbol in a shared object.
4311 We need to get the alignment from the section. */
4312 align = new_sec->alignment_power;
4313 }
595213d4 4314 if (align > old_alignment)
4ad4eba5
AM
4315 h->root.u.c.p->alignment_power = align;
4316 else
4317 h->root.u.c.p->alignment_power = old_alignment;
4318 }
4319
66eb6687 4320 if (is_elf_hash_table (htab))
4ad4eba5 4321 {
4f3fedcf
AM
4322 /* Set a flag in the hash table entry indicating the type of
4323 reference or definition we just found. A dynamic symbol
4324 is one which is referenced or defined by both a regular
4325 object and a shared object. */
4326 bfd_boolean dynsym = FALSE;
4327
4328 /* Plugin symbols aren't normal. Don't set def_regular or
4329 ref_regular for them, or make them dynamic. */
4330 if ((abfd->flags & BFD_PLUGIN) != 0)
4331 ;
4332 else if (! dynamic)
4333 {
4334 if (! definition)
4335 {
4336 h->ref_regular = 1;
4337 if (bind != STB_WEAK)
4338 h->ref_regular_nonweak = 1;
4339 }
4340 else
4341 {
4342 h->def_regular = 1;
4343 if (h->def_dynamic)
4344 {
4345 h->def_dynamic = 0;
4346 h->ref_dynamic = 1;
4347 }
4348 }
4349
4350 /* If the indirect symbol has been forced local, don't
4351 make the real symbol dynamic. */
4352 if ((h == hi || !hi->forced_local)
0e1862bb 4353 && (bfd_link_dll (info)
4f3fedcf
AM
4354 || h->def_dynamic
4355 || h->ref_dynamic))
4356 dynsym = TRUE;
4357 }
4358 else
4359 {
4360 if (! definition)
4361 {
4362 h->ref_dynamic = 1;
4363 hi->ref_dynamic = 1;
4364 }
4365 else
4366 {
4367 h->def_dynamic = 1;
4368 hi->def_dynamic = 1;
4369 }
4370
4371 /* If the indirect symbol has been forced local, don't
4372 make the real symbol dynamic. */
4373 if ((h == hi || !hi->forced_local)
4374 && (h->def_regular
4375 || h->ref_regular
4376 || (h->u.weakdef != NULL
4377 && ! new_weakdef
4378 && h->u.weakdef->dynindx != -1)))
4379 dynsym = TRUE;
4380 }
4381
4382 /* Check to see if we need to add an indirect symbol for
4383 the default name. */
4384 if (definition
4385 || (!override && h->root.type == bfd_link_hash_common))
4386 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4387 sec, value, &old_bfd, &dynsym))
4388 goto error_free_vers;
4ad4eba5
AM
4389
4390 /* Check the alignment when a common symbol is involved. This
4391 can change when a common symbol is overridden by a normal
4392 definition or a common symbol is ignored due to the old
4393 normal definition. We need to make sure the maximum
4394 alignment is maintained. */
a4d8e49b 4395 if ((old_alignment || common)
4ad4eba5
AM
4396 && h->root.type != bfd_link_hash_common)
4397 {
4398 unsigned int common_align;
4399 unsigned int normal_align;
4400 unsigned int symbol_align;
4401 bfd *normal_bfd;
4402 bfd *common_bfd;
4403
3a81e825
AM
4404 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4405 || h->root.type == bfd_link_hash_defweak);
4406
4ad4eba5
AM
4407 symbol_align = ffs (h->root.u.def.value) - 1;
4408 if (h->root.u.def.section->owner != NULL
4409 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4410 {
4411 normal_align = h->root.u.def.section->alignment_power;
4412 if (normal_align > symbol_align)
4413 normal_align = symbol_align;
4414 }
4415 else
4416 normal_align = symbol_align;
4417
4418 if (old_alignment)
4419 {
4420 common_align = old_alignment;
4421 common_bfd = old_bfd;
4422 normal_bfd = abfd;
4423 }
4424 else
4425 {
4426 common_align = bfd_log2 (isym->st_value);
4427 common_bfd = abfd;
4428 normal_bfd = old_bfd;
4429 }
4430
4431 if (normal_align < common_align)
d07676f8
NC
4432 {
4433 /* PR binutils/2735 */
4434 if (normal_bfd == NULL)
4435 (*_bfd_error_handler)
4f3fedcf
AM
4436 (_("Warning: alignment %u of common symbol `%s' in %B is"
4437 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4438 common_bfd, h->root.u.def.section,
4439 1 << common_align, name, 1 << normal_align);
4440 else
4441 (*_bfd_error_handler)
4442 (_("Warning: alignment %u of symbol `%s' in %B"
4443 " is smaller than %u in %B"),
4444 normal_bfd, common_bfd,
4445 1 << normal_align, name, 1 << common_align);
4446 }
4ad4eba5
AM
4447 }
4448
83ad0046 4449 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4450 if (isym->st_size != 0
4451 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4452 && (definition || h->size == 0))
4453 {
83ad0046
L
4454 if (h->size != 0
4455 && h->size != isym->st_size
4456 && ! size_change_ok)
4ad4eba5 4457 (*_bfd_error_handler)
d003868e
AM
4458 (_("Warning: size of symbol `%s' changed"
4459 " from %lu in %B to %lu in %B"),
4460 old_bfd, abfd,
4ad4eba5 4461 name, (unsigned long) h->size,
d003868e 4462 (unsigned long) isym->st_size);
4ad4eba5
AM
4463
4464 h->size = isym->st_size;
4465 }
4466
4467 /* If this is a common symbol, then we always want H->SIZE
4468 to be the size of the common symbol. The code just above
4469 won't fix the size if a common symbol becomes larger. We
4470 don't warn about a size change here, because that is
4f3fedcf 4471 covered by --warn-common. Allow changes between different
fcb93ecf 4472 function types. */
4ad4eba5
AM
4473 if (h->root.type == bfd_link_hash_common)
4474 h->size = h->root.u.c.size;
4475
4476 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4477 && ((definition && !new_weak)
4478 || (old_weak && h->root.type == bfd_link_hash_common)
4479 || h->type == STT_NOTYPE))
4ad4eba5 4480 {
2955ec4c
L
4481 unsigned int type = ELF_ST_TYPE (isym->st_info);
4482
4483 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4484 symbol. */
4485 if (type == STT_GNU_IFUNC
4486 && (abfd->flags & DYNAMIC) != 0)
4487 type = STT_FUNC;
4ad4eba5 4488
2955ec4c
L
4489 if (h->type != type)
4490 {
4491 if (h->type != STT_NOTYPE && ! type_change_ok)
4492 (*_bfd_error_handler)
4493 (_("Warning: type of symbol `%s' changed"
4494 " from %d to %d in %B"),
4495 abfd, name, h->type, type);
4496
4497 h->type = type;
4498 }
4ad4eba5
AM
4499 }
4500
54ac0771 4501 /* Merge st_other field. */
b8417128 4502 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4503
c3df8c14 4504 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4505 if (definition
4506 && (sec->flags & SEC_DEBUGGING)
4507 && !bfd_link_relocatable (info))
c3df8c14
AM
4508 dynsym = FALSE;
4509
4f3fedcf
AM
4510 /* Nor should we make plugin symbols dynamic. */
4511 if ((abfd->flags & BFD_PLUGIN) != 0)
4512 dynsym = FALSE;
4513
35fc36a8 4514 if (definition)
35399224
L
4515 {
4516 h->target_internal = isym->st_target_internal;
4517 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4518 }
35fc36a8 4519
4ad4eba5
AM
4520 if (definition && !dynamic)
4521 {
4522 char *p = strchr (name, ELF_VER_CHR);
4523 if (p != NULL && p[1] != ELF_VER_CHR)
4524 {
4525 /* Queue non-default versions so that .symver x, x@FOO
4526 aliases can be checked. */
66eb6687 4527 if (!nondeflt_vers)
4ad4eba5 4528 {
66eb6687
AM
4529 amt = ((isymend - isym + 1)
4530 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4531 nondeflt_vers
4532 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4533 if (!nondeflt_vers)
4534 goto error_free_vers;
4ad4eba5 4535 }
66eb6687 4536 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4537 }
4538 }
4539
4540 if (dynsym && h->dynindx == -1)
4541 {
c152c796 4542 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4543 goto error_free_vers;
f6e332e6 4544 if (h->u.weakdef != NULL
4ad4eba5 4545 && ! new_weakdef
f6e332e6 4546 && h->u.weakdef->dynindx == -1)
4ad4eba5 4547 {
66eb6687 4548 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4549 goto error_free_vers;
4550 }
4551 }
4552 else if (dynsym && h->dynindx != -1)
4553 /* If the symbol already has a dynamic index, but
4554 visibility says it should not be visible, turn it into
4555 a local symbol. */
4556 switch (ELF_ST_VISIBILITY (h->other))
4557 {
4558 case STV_INTERNAL:
4559 case STV_HIDDEN:
4560 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4561 dynsym = FALSE;
4562 break;
4563 }
4564
aef28989
L
4565 /* Don't add DT_NEEDED for references from the dummy bfd nor
4566 for unmatched symbol. */
4ad4eba5 4567 if (!add_needed
aef28989 4568 && matched
4ad4eba5 4569 && definition
010e5ae2 4570 && ((dynsym
ffa9430d 4571 && h->ref_regular_nonweak
4f3fedcf
AM
4572 && (old_bfd == NULL
4573 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4574 || (h->ref_dynamic_nonweak
010e5ae2
AM
4575 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4576 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4577 {
4578 int ret;
4579 const char *soname = elf_dt_name (abfd);
4580
16e4ecc0
AM
4581 info->callbacks->minfo ("%!", soname, old_bfd,
4582 h->root.root.string);
4583
4ad4eba5
AM
4584 /* A symbol from a library loaded via DT_NEEDED of some
4585 other library is referenced by a regular object.
e56f61be 4586 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4587 --no-add-needed is used and the reference was not
4588 a weak one. */
4f3fedcf 4589 if (old_bfd != NULL
b918acf9 4590 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4591 {
4592 (*_bfd_error_handler)
3cbc5de0 4593 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4594 old_bfd, name);
ff5ac77b 4595 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4596 goto error_free_vers;
4597 }
4598
a50b1753 4599 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4600 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4601
4ad4eba5 4602 add_needed = TRUE;
7e9f0867 4603 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4604 if (ret < 0)
4605 goto error_free_vers;
4606
4607 BFD_ASSERT (ret == 0);
4608 }
4609 }
4610 }
4611
66eb6687
AM
4612 if (extversym != NULL)
4613 {
4614 free (extversym);
4615 extversym = NULL;
4616 }
4617
4618 if (isymbuf != NULL)
4619 {
4620 free (isymbuf);
4621 isymbuf = NULL;
4622 }
4623
4624 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4625 {
4626 unsigned int i;
4627
4628 /* Restore the symbol table. */
f45794cb
AM
4629 old_ent = (char *) old_tab + tabsize;
4630 memset (elf_sym_hashes (abfd), 0,
4631 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4632 htab->root.table.table = old_table;
4633 htab->root.table.size = old_size;
4634 htab->root.table.count = old_count;
66eb6687 4635 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4636 htab->root.undefs = old_undefs;
4637 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4638 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4639 for (i = 0; i < htab->root.table.size; i++)
4640 {
4641 struct bfd_hash_entry *p;
4642 struct elf_link_hash_entry *h;
3e0882af
L
4643 bfd_size_type size;
4644 unsigned int alignment_power;
66eb6687
AM
4645
4646 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4647 {
4648 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4649 if (h->root.type == bfd_link_hash_warning)
4650 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4651 if (h->dynindx >= old_dynsymcount
4652 && h->dynstr_index < old_dynstr_size)
66eb6687 4653 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4654
3e0882af
L
4655 /* Preserve the maximum alignment and size for common
4656 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4657 since it can still be loaded at run time by another
3e0882af
L
4658 dynamic lib. */
4659 if (h->root.type == bfd_link_hash_common)
4660 {
4661 size = h->root.u.c.size;
4662 alignment_power = h->root.u.c.p->alignment_power;
4663 }
4664 else
4665 {
4666 size = 0;
4667 alignment_power = 0;
4668 }
66eb6687
AM
4669 memcpy (p, old_ent, htab->root.table.entsize);
4670 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4671 h = (struct elf_link_hash_entry *) p;
4672 if (h->root.type == bfd_link_hash_warning)
4673 {
4674 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4675 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4677 }
a4542f1b 4678 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4679 {
4680 if (size > h->root.u.c.size)
4681 h->root.u.c.size = size;
4682 if (alignment_power > h->root.u.c.p->alignment_power)
4683 h->root.u.c.p->alignment_power = alignment_power;
4684 }
66eb6687
AM
4685 }
4686 }
4687
5061a885
AM
4688 /* Make a special call to the linker "notice" function to
4689 tell it that symbols added for crefs may need to be removed. */
e5034e59 4690 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4691 goto error_free_vers;
5061a885 4692
66eb6687
AM
4693 free (old_tab);
4694 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4695 alloc_mark);
4696 if (nondeflt_vers != NULL)
4697 free (nondeflt_vers);
4698 return TRUE;
4699 }
2de92251 4700
66eb6687
AM
4701 if (old_tab != NULL)
4702 {
e5034e59 4703 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4704 goto error_free_vers;
66eb6687
AM
4705 free (old_tab);
4706 old_tab = NULL;
4707 }
4708
c6e8a9a8
L
4709 /* Now that all the symbols from this input file are created, if
4710 not performing a relocatable link, handle .symver foo, foo@BAR
4711 such that any relocs against foo become foo@BAR. */
0e1862bb 4712 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4713 {
4714 bfd_size_type cnt, symidx;
4715
4716 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4717 {
4718 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4719 char *shortname, *p;
4720
4721 p = strchr (h->root.root.string, ELF_VER_CHR);
4722 if (p == NULL
4723 || (h->root.type != bfd_link_hash_defined
4724 && h->root.type != bfd_link_hash_defweak))
4725 continue;
4726
4727 amt = p - h->root.root.string;
a50b1753 4728 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4729 if (!shortname)
4730 goto error_free_vers;
4ad4eba5
AM
4731 memcpy (shortname, h->root.root.string, amt);
4732 shortname[amt] = '\0';
4733
4734 hi = (struct elf_link_hash_entry *)
66eb6687 4735 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4736 FALSE, FALSE, FALSE);
4737 if (hi != NULL
4738 && hi->root.type == h->root.type
4739 && hi->root.u.def.value == h->root.u.def.value
4740 && hi->root.u.def.section == h->root.u.def.section)
4741 {
4742 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4743 hi->root.type = bfd_link_hash_indirect;
4744 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4745 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4746 sym_hash = elf_sym_hashes (abfd);
4747 if (sym_hash)
4748 for (symidx = 0; symidx < extsymcount; ++symidx)
4749 if (sym_hash[symidx] == hi)
4750 {
4751 sym_hash[symidx] = h;
4752 break;
4753 }
4754 }
4755 free (shortname);
4756 }
4757 free (nondeflt_vers);
4758 nondeflt_vers = NULL;
4759 }
4760
4ad4eba5
AM
4761 /* Now set the weakdefs field correctly for all the weak defined
4762 symbols we found. The only way to do this is to search all the
4763 symbols. Since we only need the information for non functions in
4764 dynamic objects, that's the only time we actually put anything on
4765 the list WEAKS. We need this information so that if a regular
4766 object refers to a symbol defined weakly in a dynamic object, the
4767 real symbol in the dynamic object is also put in the dynamic
4768 symbols; we also must arrange for both symbols to point to the
4769 same memory location. We could handle the general case of symbol
4770 aliasing, but a general symbol alias can only be generated in
4771 assembler code, handling it correctly would be very time
4772 consuming, and other ELF linkers don't handle general aliasing
4773 either. */
4774 if (weaks != NULL)
4775 {
4776 struct elf_link_hash_entry **hpp;
4777 struct elf_link_hash_entry **hppend;
4778 struct elf_link_hash_entry **sorted_sym_hash;
4779 struct elf_link_hash_entry *h;
4780 size_t sym_count;
4781
4782 /* Since we have to search the whole symbol list for each weak
4783 defined symbol, search time for N weak defined symbols will be
4784 O(N^2). Binary search will cut it down to O(NlogN). */
4785 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4786 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4787 if (sorted_sym_hash == NULL)
4788 goto error_return;
4789 sym_hash = sorted_sym_hash;
4790 hpp = elf_sym_hashes (abfd);
4791 hppend = hpp + extsymcount;
4792 sym_count = 0;
4793 for (; hpp < hppend; hpp++)
4794 {
4795 h = *hpp;
4796 if (h != NULL
4797 && h->root.type == bfd_link_hash_defined
fcb93ecf 4798 && !bed->is_function_type (h->type))
4ad4eba5
AM
4799 {
4800 *sym_hash = h;
4801 sym_hash++;
4802 sym_count++;
4803 }
4804 }
4805
4806 qsort (sorted_sym_hash, sym_count,
4807 sizeof (struct elf_link_hash_entry *),
4808 elf_sort_symbol);
4809
4810 while (weaks != NULL)
4811 {
4812 struct elf_link_hash_entry *hlook;
4813 asection *slook;
4814 bfd_vma vlook;
ed54588d 4815 size_t i, j, idx = 0;
4ad4eba5
AM
4816
4817 hlook = weaks;
f6e332e6
AM
4818 weaks = hlook->u.weakdef;
4819 hlook->u.weakdef = NULL;
4ad4eba5
AM
4820
4821 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4822 || hlook->root.type == bfd_link_hash_defweak
4823 || hlook->root.type == bfd_link_hash_common
4824 || hlook->root.type == bfd_link_hash_indirect);
4825 slook = hlook->root.u.def.section;
4826 vlook = hlook->root.u.def.value;
4827
4ad4eba5
AM
4828 i = 0;
4829 j = sym_count;
14160578 4830 while (i != j)
4ad4eba5
AM
4831 {
4832 bfd_signed_vma vdiff;
4833 idx = (i + j) / 2;
14160578 4834 h = sorted_sym_hash[idx];
4ad4eba5
AM
4835 vdiff = vlook - h->root.u.def.value;
4836 if (vdiff < 0)
4837 j = idx;
4838 else if (vdiff > 0)
4839 i = idx + 1;
4840 else
4841 {
d3435ae8 4842 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4843 if (sdiff < 0)
4844 j = idx;
4845 else if (sdiff > 0)
4846 i = idx + 1;
4847 else
14160578 4848 break;
4ad4eba5
AM
4849 }
4850 }
4851
4852 /* We didn't find a value/section match. */
14160578 4853 if (i == j)
4ad4eba5
AM
4854 continue;
4855
14160578
AM
4856 /* With multiple aliases, or when the weak symbol is already
4857 strongly defined, we have multiple matching symbols and
4858 the binary search above may land on any of them. Step
4859 one past the matching symbol(s). */
4860 while (++idx != j)
4861 {
4862 h = sorted_sym_hash[idx];
4863 if (h->root.u.def.section != slook
4864 || h->root.u.def.value != vlook)
4865 break;
4866 }
4867
4868 /* Now look back over the aliases. Since we sorted by size
4869 as well as value and section, we'll choose the one with
4870 the largest size. */
4871 while (idx-- != i)
4ad4eba5 4872 {
14160578 4873 h = sorted_sym_hash[idx];
4ad4eba5
AM
4874
4875 /* Stop if value or section doesn't match. */
14160578
AM
4876 if (h->root.u.def.section != slook
4877 || h->root.u.def.value != vlook)
4ad4eba5
AM
4878 break;
4879 else if (h != hlook)
4880 {
f6e332e6 4881 hlook->u.weakdef = h;
4ad4eba5
AM
4882
4883 /* If the weak definition is in the list of dynamic
4884 symbols, make sure the real definition is put
4885 there as well. */
4886 if (hlook->dynindx != -1 && h->dynindx == -1)
4887 {
c152c796 4888 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4889 {
4890 err_free_sym_hash:
4891 free (sorted_sym_hash);
4892 goto error_return;
4893 }
4ad4eba5
AM
4894 }
4895
4896 /* If the real definition is in the list of dynamic
4897 symbols, make sure the weak definition is put
4898 there as well. If we don't do this, then the
4899 dynamic loader might not merge the entries for the
4900 real definition and the weak definition. */
4901 if (h->dynindx != -1 && hlook->dynindx == -1)
4902 {
c152c796 4903 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4904 goto err_free_sym_hash;
4ad4eba5
AM
4905 }
4906 break;
4907 }
4908 }
4909 }
4910
4911 free (sorted_sym_hash);
4912 }
4913
33177bb1
AM
4914 if (bed->check_directives
4915 && !(*bed->check_directives) (abfd, info))
4916 return FALSE;
85fbca6a 4917
4ad4eba5
AM
4918 /* If this object is the same format as the output object, and it is
4919 not a shared library, then let the backend look through the
4920 relocs.
4921
4922 This is required to build global offset table entries and to
4923 arrange for dynamic relocs. It is not required for the
4924 particular common case of linking non PIC code, even when linking
4925 against shared libraries, but unfortunately there is no way of
4926 knowing whether an object file has been compiled PIC or not.
4927 Looking through the relocs is not particularly time consuming.
4928 The problem is that we must either (1) keep the relocs in memory,
4929 which causes the linker to require additional runtime memory or
4930 (2) read the relocs twice from the input file, which wastes time.
4931 This would be a good case for using mmap.
4932
4933 I have no idea how to handle linking PIC code into a file of a
4934 different format. It probably can't be done. */
4ad4eba5 4935 if (! dynamic
66eb6687 4936 && is_elf_hash_table (htab)
13285a1b 4937 && bed->check_relocs != NULL
39334f3a 4938 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4939 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4940 {
4941 asection *o;
4942
4943 for (o = abfd->sections; o != NULL; o = o->next)
4944 {
4945 Elf_Internal_Rela *internal_relocs;
4946 bfd_boolean ok;
4947
4948 if ((o->flags & SEC_RELOC) == 0
4949 || o->reloc_count == 0
4950 || ((info->strip == strip_all || info->strip == strip_debugger)
4951 && (o->flags & SEC_DEBUGGING) != 0)
4952 || bfd_is_abs_section (o->output_section))
4953 continue;
4954
4955 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4956 info->keep_memory);
4957 if (internal_relocs == NULL)
4958 goto error_return;
4959
66eb6687 4960 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4961
4962 if (elf_section_data (o)->relocs != internal_relocs)
4963 free (internal_relocs);
4964
4965 if (! ok)
4966 goto error_return;
4967 }
4968 }
4969
4970 /* If this is a non-traditional link, try to optimize the handling
4971 of the .stab/.stabstr sections. */
4972 if (! dynamic
4973 && ! info->traditional_format
66eb6687 4974 && is_elf_hash_table (htab)
4ad4eba5
AM
4975 && (info->strip != strip_all && info->strip != strip_debugger))
4976 {
4977 asection *stabstr;
4978
4979 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4980 if (stabstr != NULL)
4981 {
4982 bfd_size_type string_offset = 0;
4983 asection *stab;
4984
4985 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4986 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4987 && (!stab->name[5] ||
4988 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4989 && (stab->flags & SEC_MERGE) == 0
4990 && !bfd_is_abs_section (stab->output_section))
4991 {
4992 struct bfd_elf_section_data *secdata;
4993
4994 secdata = elf_section_data (stab);
66eb6687
AM
4995 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4996 stabstr, &secdata->sec_info,
4ad4eba5
AM
4997 &string_offset))
4998 goto error_return;
4999 if (secdata->sec_info)
dbaa2011 5000 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5001 }
5002 }
5003 }
5004
66eb6687 5005 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5006 {
5007 /* Add this bfd to the loaded list. */
5008 struct elf_link_loaded_list *n;
5009
ca4be51c 5010 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5011 if (n == NULL)
5012 goto error_return;
5013 n->abfd = abfd;
66eb6687
AM
5014 n->next = htab->loaded;
5015 htab->loaded = n;
4ad4eba5
AM
5016 }
5017
5018 return TRUE;
5019
5020 error_free_vers:
66eb6687
AM
5021 if (old_tab != NULL)
5022 free (old_tab);
4ad4eba5
AM
5023 if (nondeflt_vers != NULL)
5024 free (nondeflt_vers);
5025 if (extversym != NULL)
5026 free (extversym);
5027 error_free_sym:
5028 if (isymbuf != NULL)
5029 free (isymbuf);
5030 error_return:
5031 return FALSE;
5032}
5033
8387904d
AM
5034/* Return the linker hash table entry of a symbol that might be
5035 satisfied by an archive symbol. Return -1 on error. */
5036
5037struct elf_link_hash_entry *
5038_bfd_elf_archive_symbol_lookup (bfd *abfd,
5039 struct bfd_link_info *info,
5040 const char *name)
5041{
5042 struct elf_link_hash_entry *h;
5043 char *p, *copy;
5044 size_t len, first;
5045
2a41f396 5046 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5047 if (h != NULL)
5048 return h;
5049
5050 /* If this is a default version (the name contains @@), look up the
5051 symbol again with only one `@' as well as without the version.
5052 The effect is that references to the symbol with and without the
5053 version will be matched by the default symbol in the archive. */
5054
5055 p = strchr (name, ELF_VER_CHR);
5056 if (p == NULL || p[1] != ELF_VER_CHR)
5057 return h;
5058
5059 /* First check with only one `@'. */
5060 len = strlen (name);
a50b1753 5061 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5062 if (copy == NULL)
5063 return (struct elf_link_hash_entry *) 0 - 1;
5064
5065 first = p - name + 1;
5066 memcpy (copy, name, first);
5067 memcpy (copy + first, name + first + 1, len - first);
5068
2a41f396 5069 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5070 if (h == NULL)
5071 {
5072 /* We also need to check references to the symbol without the
5073 version. */
5074 copy[first - 1] = '\0';
5075 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5076 FALSE, FALSE, TRUE);
8387904d
AM
5077 }
5078
5079 bfd_release (abfd, copy);
5080 return h;
5081}
5082
0ad989f9 5083/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5084 don't use _bfd_generic_link_add_archive_symbols because we need to
5085 handle versioned symbols.
0ad989f9
L
5086
5087 Fortunately, ELF archive handling is simpler than that done by
5088 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5089 oddities. In ELF, if we find a symbol in the archive map, and the
5090 symbol is currently undefined, we know that we must pull in that
5091 object file.
5092
5093 Unfortunately, we do have to make multiple passes over the symbol
5094 table until nothing further is resolved. */
5095
4ad4eba5
AM
5096static bfd_boolean
5097elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5098{
5099 symindex c;
13e570f8 5100 unsigned char *included = NULL;
0ad989f9
L
5101 carsym *symdefs;
5102 bfd_boolean loop;
5103 bfd_size_type amt;
8387904d
AM
5104 const struct elf_backend_data *bed;
5105 struct elf_link_hash_entry * (*archive_symbol_lookup)
5106 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5107
5108 if (! bfd_has_map (abfd))
5109 {
5110 /* An empty archive is a special case. */
5111 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5112 return TRUE;
5113 bfd_set_error (bfd_error_no_armap);
5114 return FALSE;
5115 }
5116
5117 /* Keep track of all symbols we know to be already defined, and all
5118 files we know to be already included. This is to speed up the
5119 second and subsequent passes. */
5120 c = bfd_ardata (abfd)->symdef_count;
5121 if (c == 0)
5122 return TRUE;
5123 amt = c;
13e570f8
AM
5124 amt *= sizeof (*included);
5125 included = (unsigned char *) bfd_zmalloc (amt);
5126 if (included == NULL)
5127 return FALSE;
0ad989f9
L
5128
5129 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5130 bed = get_elf_backend_data (abfd);
5131 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5132
5133 do
5134 {
5135 file_ptr last;
5136 symindex i;
5137 carsym *symdef;
5138 carsym *symdefend;
5139
5140 loop = FALSE;
5141 last = -1;
5142
5143 symdef = symdefs;
5144 symdefend = symdef + c;
5145 for (i = 0; symdef < symdefend; symdef++, i++)
5146 {
5147 struct elf_link_hash_entry *h;
5148 bfd *element;
5149 struct bfd_link_hash_entry *undefs_tail;
5150 symindex mark;
5151
13e570f8 5152 if (included[i])
0ad989f9
L
5153 continue;
5154 if (symdef->file_offset == last)
5155 {
5156 included[i] = TRUE;
5157 continue;
5158 }
5159
8387904d
AM
5160 h = archive_symbol_lookup (abfd, info, symdef->name);
5161 if (h == (struct elf_link_hash_entry *) 0 - 1)
5162 goto error_return;
0ad989f9
L
5163
5164 if (h == NULL)
5165 continue;
5166
5167 if (h->root.type == bfd_link_hash_common)
5168 {
5169 /* We currently have a common symbol. The archive map contains
5170 a reference to this symbol, so we may want to include it. We
5171 only want to include it however, if this archive element
5172 contains a definition of the symbol, not just another common
5173 declaration of it.
5174
5175 Unfortunately some archivers (including GNU ar) will put
5176 declarations of common symbols into their archive maps, as
5177 well as real definitions, so we cannot just go by the archive
5178 map alone. Instead we must read in the element's symbol
5179 table and check that to see what kind of symbol definition
5180 this is. */
5181 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5182 continue;
5183 }
5184 else if (h->root.type != bfd_link_hash_undefined)
5185 {
5186 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5187 /* Symbol must be defined. Don't check it again. */
5188 included[i] = TRUE;
0ad989f9
L
5189 continue;
5190 }
5191
5192 /* We need to include this archive member. */
5193 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5194 if (element == NULL)
5195 goto error_return;
5196
5197 if (! bfd_check_format (element, bfd_object))
5198 goto error_return;
5199
0ad989f9
L
5200 undefs_tail = info->hash->undefs_tail;
5201
0e144ba7
AM
5202 if (!(*info->callbacks
5203 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5204 goto error_return;
0e144ba7 5205 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5206 goto error_return;
5207
5208 /* If there are any new undefined symbols, we need to make
5209 another pass through the archive in order to see whether
5210 they can be defined. FIXME: This isn't perfect, because
5211 common symbols wind up on undefs_tail and because an
5212 undefined symbol which is defined later on in this pass
5213 does not require another pass. This isn't a bug, but it
5214 does make the code less efficient than it could be. */
5215 if (undefs_tail != info->hash->undefs_tail)
5216 loop = TRUE;
5217
5218 /* Look backward to mark all symbols from this object file
5219 which we have already seen in this pass. */
5220 mark = i;
5221 do
5222 {
5223 included[mark] = TRUE;
5224 if (mark == 0)
5225 break;
5226 --mark;
5227 }
5228 while (symdefs[mark].file_offset == symdef->file_offset);
5229
5230 /* We mark subsequent symbols from this object file as we go
5231 on through the loop. */
5232 last = symdef->file_offset;
5233 }
5234 }
5235 while (loop);
5236
0ad989f9
L
5237 free (included);
5238
5239 return TRUE;
5240
5241 error_return:
0ad989f9
L
5242 if (included != NULL)
5243 free (included);
5244 return FALSE;
5245}
4ad4eba5
AM
5246
5247/* Given an ELF BFD, add symbols to the global hash table as
5248 appropriate. */
5249
5250bfd_boolean
5251bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5252{
5253 switch (bfd_get_format (abfd))
5254 {
5255 case bfd_object:
5256 return elf_link_add_object_symbols (abfd, info);
5257 case bfd_archive:
5258 return elf_link_add_archive_symbols (abfd, info);
5259 default:
5260 bfd_set_error (bfd_error_wrong_format);
5261 return FALSE;
5262 }
5263}
5a580b3a 5264\f
14b1c01e
AM
5265struct hash_codes_info
5266{
5267 unsigned long *hashcodes;
5268 bfd_boolean error;
5269};
a0c8462f 5270
5a580b3a
AM
5271/* This function will be called though elf_link_hash_traverse to store
5272 all hash value of the exported symbols in an array. */
5273
5274static bfd_boolean
5275elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5276{
a50b1753 5277 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5278 const char *name;
5a580b3a
AM
5279 unsigned long ha;
5280 char *alc = NULL;
5281
5a580b3a
AM
5282 /* Ignore indirect symbols. These are added by the versioning code. */
5283 if (h->dynindx == -1)
5284 return TRUE;
5285
5286 name = h->root.root.string;
422f1182 5287 if (h->versioned >= versioned)
5a580b3a 5288 {
422f1182
L
5289 char *p = strchr (name, ELF_VER_CHR);
5290 if (p != NULL)
14b1c01e 5291 {
422f1182
L
5292 alc = (char *) bfd_malloc (p - name + 1);
5293 if (alc == NULL)
5294 {
5295 inf->error = TRUE;
5296 return FALSE;
5297 }
5298 memcpy (alc, name, p - name);
5299 alc[p - name] = '\0';
5300 name = alc;
14b1c01e 5301 }
5a580b3a
AM
5302 }
5303
5304 /* Compute the hash value. */
5305 ha = bfd_elf_hash (name);
5306
5307 /* Store the found hash value in the array given as the argument. */
14b1c01e 5308 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5309
5310 /* And store it in the struct so that we can put it in the hash table
5311 later. */
f6e332e6 5312 h->u.elf_hash_value = ha;
5a580b3a
AM
5313
5314 if (alc != NULL)
5315 free (alc);
5316
5317 return TRUE;
5318}
5319
fdc90cb4
JJ
5320struct collect_gnu_hash_codes
5321{
5322 bfd *output_bfd;
5323 const struct elf_backend_data *bed;
5324 unsigned long int nsyms;
5325 unsigned long int maskbits;
5326 unsigned long int *hashcodes;
5327 unsigned long int *hashval;
5328 unsigned long int *indx;
5329 unsigned long int *counts;
5330 bfd_vma *bitmask;
5331 bfd_byte *contents;
5332 long int min_dynindx;
5333 unsigned long int bucketcount;
5334 unsigned long int symindx;
5335 long int local_indx;
5336 long int shift1, shift2;
5337 unsigned long int mask;
14b1c01e 5338 bfd_boolean error;
fdc90cb4
JJ
5339};
5340
5341/* This function will be called though elf_link_hash_traverse to store
5342 all hash value of the exported symbols in an array. */
5343
5344static bfd_boolean
5345elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5346{
a50b1753 5347 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5348 const char *name;
fdc90cb4
JJ
5349 unsigned long ha;
5350 char *alc = NULL;
5351
fdc90cb4
JJ
5352 /* Ignore indirect symbols. These are added by the versioning code. */
5353 if (h->dynindx == -1)
5354 return TRUE;
5355
5356 /* Ignore also local symbols and undefined symbols. */
5357 if (! (*s->bed->elf_hash_symbol) (h))
5358 return TRUE;
5359
5360 name = h->root.root.string;
422f1182 5361 if (h->versioned >= versioned)
fdc90cb4 5362 {
422f1182
L
5363 char *p = strchr (name, ELF_VER_CHR);
5364 if (p != NULL)
14b1c01e 5365 {
422f1182
L
5366 alc = (char *) bfd_malloc (p - name + 1);
5367 if (alc == NULL)
5368 {
5369 s->error = TRUE;
5370 return FALSE;
5371 }
5372 memcpy (alc, name, p - name);
5373 alc[p - name] = '\0';
5374 name = alc;
14b1c01e 5375 }
fdc90cb4
JJ
5376 }
5377
5378 /* Compute the hash value. */
5379 ha = bfd_elf_gnu_hash (name);
5380
5381 /* Store the found hash value in the array for compute_bucket_count,
5382 and also for .dynsym reordering purposes. */
5383 s->hashcodes[s->nsyms] = ha;
5384 s->hashval[h->dynindx] = ha;
5385 ++s->nsyms;
5386 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5387 s->min_dynindx = h->dynindx;
5388
5389 if (alc != NULL)
5390 free (alc);
5391
5392 return TRUE;
5393}
5394
5395/* This function will be called though elf_link_hash_traverse to do
5396 final dynaminc symbol renumbering. */
5397
5398static bfd_boolean
5399elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5400{
a50b1753 5401 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5402 unsigned long int bucket;
5403 unsigned long int val;
5404
fdc90cb4
JJ
5405 /* Ignore indirect symbols. */
5406 if (h->dynindx == -1)
5407 return TRUE;
5408
5409 /* Ignore also local symbols and undefined symbols. */
5410 if (! (*s->bed->elf_hash_symbol) (h))
5411 {
5412 if (h->dynindx >= s->min_dynindx)
5413 h->dynindx = s->local_indx++;
5414 return TRUE;
5415 }
5416
5417 bucket = s->hashval[h->dynindx] % s->bucketcount;
5418 val = (s->hashval[h->dynindx] >> s->shift1)
5419 & ((s->maskbits >> s->shift1) - 1);
5420 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5421 s->bitmask[val]
5422 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5423 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5424 if (s->counts[bucket] == 1)
5425 /* Last element terminates the chain. */
5426 val |= 1;
5427 bfd_put_32 (s->output_bfd, val,
5428 s->contents + (s->indx[bucket] - s->symindx) * 4);
5429 --s->counts[bucket];
5430 h->dynindx = s->indx[bucket]++;
5431 return TRUE;
5432}
5433
5434/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5435
5436bfd_boolean
5437_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5438{
5439 return !(h->forced_local
5440 || h->root.type == bfd_link_hash_undefined
5441 || h->root.type == bfd_link_hash_undefweak
5442 || ((h->root.type == bfd_link_hash_defined
5443 || h->root.type == bfd_link_hash_defweak)
5444 && h->root.u.def.section->output_section == NULL));
5445}
5446
5a580b3a
AM
5447/* Array used to determine the number of hash table buckets to use
5448 based on the number of symbols there are. If there are fewer than
5449 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5450 fewer than 37 we use 17 buckets, and so forth. We never use more
5451 than 32771 buckets. */
5452
5453static const size_t elf_buckets[] =
5454{
5455 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5456 16411, 32771, 0
5457};
5458
5459/* Compute bucket count for hashing table. We do not use a static set
5460 of possible tables sizes anymore. Instead we determine for all
5461 possible reasonable sizes of the table the outcome (i.e., the
5462 number of collisions etc) and choose the best solution. The
5463 weighting functions are not too simple to allow the table to grow
5464 without bounds. Instead one of the weighting factors is the size.
5465 Therefore the result is always a good payoff between few collisions
5466 (= short chain lengths) and table size. */
5467static size_t
b20dd2ce 5468compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5469 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5470 unsigned long int nsyms,
5471 int gnu_hash)
5a580b3a 5472{
5a580b3a 5473 size_t best_size = 0;
5a580b3a 5474 unsigned long int i;
5a580b3a 5475
5a580b3a
AM
5476 /* We have a problem here. The following code to optimize the table
5477 size requires an integer type with more the 32 bits. If
5478 BFD_HOST_U_64_BIT is set we know about such a type. */
5479#ifdef BFD_HOST_U_64_BIT
5480 if (info->optimize)
5481 {
5a580b3a
AM
5482 size_t minsize;
5483 size_t maxsize;
5484 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5485 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5486 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5487 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5488 unsigned long int *counts;
d40f3da9 5489 bfd_size_type amt;
0883b6e0 5490 unsigned int no_improvement_count = 0;
5a580b3a
AM
5491
5492 /* Possible optimization parameters: if we have NSYMS symbols we say
5493 that the hashing table must at least have NSYMS/4 and at most
5494 2*NSYMS buckets. */
5495 minsize = nsyms / 4;
5496 if (minsize == 0)
5497 minsize = 1;
5498 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5499 if (gnu_hash)
5500 {
5501 if (minsize < 2)
5502 minsize = 2;
5503 if ((best_size & 31) == 0)
5504 ++best_size;
5505 }
5a580b3a
AM
5506
5507 /* Create array where we count the collisions in. We must use bfd_malloc
5508 since the size could be large. */
5509 amt = maxsize;
5510 amt *= sizeof (unsigned long int);
a50b1753 5511 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5512 if (counts == NULL)
fdc90cb4 5513 return 0;
5a580b3a
AM
5514
5515 /* Compute the "optimal" size for the hash table. The criteria is a
5516 minimal chain length. The minor criteria is (of course) the size
5517 of the table. */
5518 for (i = minsize; i < maxsize; ++i)
5519 {
5520 /* Walk through the array of hashcodes and count the collisions. */
5521 BFD_HOST_U_64_BIT max;
5522 unsigned long int j;
5523 unsigned long int fact;
5524
fdc90cb4
JJ
5525 if (gnu_hash && (i & 31) == 0)
5526 continue;
5527
5a580b3a
AM
5528 memset (counts, '\0', i * sizeof (unsigned long int));
5529
5530 /* Determine how often each hash bucket is used. */
5531 for (j = 0; j < nsyms; ++j)
5532 ++counts[hashcodes[j] % i];
5533
5534 /* For the weight function we need some information about the
5535 pagesize on the target. This is information need not be 100%
5536 accurate. Since this information is not available (so far) we
5537 define it here to a reasonable default value. If it is crucial
5538 to have a better value some day simply define this value. */
5539# ifndef BFD_TARGET_PAGESIZE
5540# define BFD_TARGET_PAGESIZE (4096)
5541# endif
5542
fdc90cb4
JJ
5543 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5544 and the chains. */
5545 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5546
5547# if 1
5548 /* Variant 1: optimize for short chains. We add the squares
5549 of all the chain lengths (which favors many small chain
5550 over a few long chains). */
5551 for (j = 0; j < i; ++j)
5552 max += counts[j] * counts[j];
5553
5554 /* This adds penalties for the overall size of the table. */
fdc90cb4 5555 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5556 max *= fact * fact;
5557# else
5558 /* Variant 2: Optimize a lot more for small table. Here we
5559 also add squares of the size but we also add penalties for
5560 empty slots (the +1 term). */
5561 for (j = 0; j < i; ++j)
5562 max += (1 + counts[j]) * (1 + counts[j]);
5563
5564 /* The overall size of the table is considered, but not as
5565 strong as in variant 1, where it is squared. */
fdc90cb4 5566 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5567 max *= fact;
5568# endif
5569
5570 /* Compare with current best results. */
5571 if (max < best_chlen)
5572 {
5573 best_chlen = max;
5574 best_size = i;
ca4be51c 5575 no_improvement_count = 0;
5a580b3a 5576 }
0883b6e0
NC
5577 /* PR 11843: Avoid futile long searches for the best bucket size
5578 when there are a large number of symbols. */
5579 else if (++no_improvement_count == 100)
5580 break;
5a580b3a
AM
5581 }
5582
5583 free (counts);
5584 }
5585 else
5586#endif /* defined (BFD_HOST_U_64_BIT) */
5587 {
5588 /* This is the fallback solution if no 64bit type is available or if we
5589 are not supposed to spend much time on optimizations. We select the
5590 bucket count using a fixed set of numbers. */
5591 for (i = 0; elf_buckets[i] != 0; i++)
5592 {
5593 best_size = elf_buckets[i];
fdc90cb4 5594 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5595 break;
5596 }
fdc90cb4
JJ
5597 if (gnu_hash && best_size < 2)
5598 best_size = 2;
5a580b3a
AM
5599 }
5600
5a580b3a
AM
5601 return best_size;
5602}
5603
d0bf826b
AM
5604/* Size any SHT_GROUP section for ld -r. */
5605
5606bfd_boolean
5607_bfd_elf_size_group_sections (struct bfd_link_info *info)
5608{
5609 bfd *ibfd;
5610
c72f2fb2 5611 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5612 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5613 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5614 return FALSE;
5615 return TRUE;
5616}
5617
04c3a755
NS
5618/* Set a default stack segment size. The value in INFO wins. If it
5619 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5620 undefined it is initialized. */
5621
5622bfd_boolean
5623bfd_elf_stack_segment_size (bfd *output_bfd,
5624 struct bfd_link_info *info,
5625 const char *legacy_symbol,
5626 bfd_vma default_size)
5627{
5628 struct elf_link_hash_entry *h = NULL;
5629
5630 /* Look for legacy symbol. */
5631 if (legacy_symbol)
5632 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5633 FALSE, FALSE, FALSE);
5634 if (h && (h->root.type == bfd_link_hash_defined
5635 || h->root.type == bfd_link_hash_defweak)
5636 && h->def_regular
5637 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5638 {
5639 /* The symbol has no type if specified on the command line. */
5640 h->type = STT_OBJECT;
5641 if (info->stacksize)
5642 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5643 output_bfd, legacy_symbol);
5644 else if (h->root.u.def.section != bfd_abs_section_ptr)
5645 (*_bfd_error_handler) (_("%B: %s not absolute"),
5646 output_bfd, legacy_symbol);
5647 else
5648 info->stacksize = h->root.u.def.value;
5649 }
5650
5651 if (!info->stacksize)
5652 /* If the user didn't set a size, or explicitly inhibit the
5653 size, set it now. */
5654 info->stacksize = default_size;
5655
5656 /* Provide the legacy symbol, if it is referenced. */
5657 if (h && (h->root.type == bfd_link_hash_undefined
5658 || h->root.type == bfd_link_hash_undefweak))
5659 {
5660 struct bfd_link_hash_entry *bh = NULL;
5661
5662 if (!(_bfd_generic_link_add_one_symbol
5663 (info, output_bfd, legacy_symbol,
5664 BSF_GLOBAL, bfd_abs_section_ptr,
5665 info->stacksize >= 0 ? info->stacksize : 0,
5666 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5667 return FALSE;
5668
5669 h = (struct elf_link_hash_entry *) bh;
5670 h->def_regular = 1;
5671 h->type = STT_OBJECT;
5672 }
5673
5674 return TRUE;
5675}
5676
5a580b3a
AM
5677/* Set up the sizes and contents of the ELF dynamic sections. This is
5678 called by the ELF linker emulation before_allocation routine. We
5679 must set the sizes of the sections before the linker sets the
5680 addresses of the various sections. */
5681
5682bfd_boolean
5683bfd_elf_size_dynamic_sections (bfd *output_bfd,
5684 const char *soname,
5685 const char *rpath,
5686 const char *filter_shlib,
7ee314fa
AM
5687 const char *audit,
5688 const char *depaudit,
5a580b3a
AM
5689 const char * const *auxiliary_filters,
5690 struct bfd_link_info *info,
fd91d419 5691 asection **sinterpptr)
5a580b3a
AM
5692{
5693 bfd_size_type soname_indx;
5694 bfd *dynobj;
5695 const struct elf_backend_data *bed;
28caa186 5696 struct elf_info_failed asvinfo;
5a580b3a
AM
5697
5698 *sinterpptr = NULL;
5699
5700 soname_indx = (bfd_size_type) -1;
5701
5702 if (!is_elf_hash_table (info->hash))
5703 return TRUE;
5704
6bfdb61b 5705 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5706
5707 /* Any syms created from now on start with -1 in
5708 got.refcount/offset and plt.refcount/offset. */
5709 elf_hash_table (info)->init_got_refcount
5710 = elf_hash_table (info)->init_got_offset;
5711 elf_hash_table (info)->init_plt_refcount
5712 = elf_hash_table (info)->init_plt_offset;
5713
0e1862bb 5714 if (bfd_link_relocatable (info)
04c3a755
NS
5715 && !_bfd_elf_size_group_sections (info))
5716 return FALSE;
5717
5718 /* The backend may have to create some sections regardless of whether
5719 we're dynamic or not. */
5720 if (bed->elf_backend_always_size_sections
5721 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5722 return FALSE;
5723
5724 /* Determine any GNU_STACK segment requirements, after the backend
5725 has had a chance to set a default segment size. */
5a580b3a 5726 if (info->execstack)
12bd6957 5727 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5728 else if (info->noexecstack)
12bd6957 5729 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5730 else
5731 {
5732 bfd *inputobj;
5733 asection *notesec = NULL;
5734 int exec = 0;
5735
5736 for (inputobj = info->input_bfds;
5737 inputobj;
c72f2fb2 5738 inputobj = inputobj->link.next)
5a580b3a
AM
5739 {
5740 asection *s;
5741
a92c088a
L
5742 if (inputobj->flags
5743 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5744 continue;
5745 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5746 if (s)
5747 {
5748 if (s->flags & SEC_CODE)
5749 exec = PF_X;
5750 notesec = s;
5751 }
6bfdb61b 5752 else if (bed->default_execstack)
5a580b3a
AM
5753 exec = PF_X;
5754 }
04c3a755 5755 if (notesec || info->stacksize > 0)
12bd6957 5756 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5757 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5758 && notesec->output_section != bfd_abs_section_ptr)
5759 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5760 }
5761
5a580b3a
AM
5762 dynobj = elf_hash_table (info)->dynobj;
5763
9a2a56cc 5764 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5765 {
5766 struct elf_info_failed eif;
5767 struct elf_link_hash_entry *h;
5768 asection *dynstr;
5769 struct bfd_elf_version_tree *t;
5770 struct bfd_elf_version_expr *d;
046183de 5771 asection *s;
5a580b3a
AM
5772 bfd_boolean all_defined;
5773
3d4d4302 5774 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5775 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5776
5777 if (soname != NULL)
5778 {
5779 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5780 soname, TRUE);
5781 if (soname_indx == (bfd_size_type) -1
5782 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5783 return FALSE;
5784 }
5785
5786 if (info->symbolic)
5787 {
5788 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5789 return FALSE;
5790 info->flags |= DF_SYMBOLIC;
5791 }
5792
5793 if (rpath != NULL)
5794 {
5795 bfd_size_type indx;
b1b00fcc 5796 bfd_vma tag;
5a580b3a
AM
5797
5798 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5799 TRUE);
b1b00fcc 5800 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5801 return FALSE;
5802
b1b00fcc
MF
5803 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5804 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5805 return FALSE;
5a580b3a
AM
5806 }
5807
5808 if (filter_shlib != NULL)
5809 {
5810 bfd_size_type indx;
5811
5812 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5813 filter_shlib, TRUE);
5814 if (indx == (bfd_size_type) -1
5815 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5816 return FALSE;
5817 }
5818
5819 if (auxiliary_filters != NULL)
5820 {
5821 const char * const *p;
5822
5823 for (p = auxiliary_filters; *p != NULL; p++)
5824 {
5825 bfd_size_type indx;
5826
5827 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5828 *p, TRUE);
5829 if (indx == (bfd_size_type) -1
5830 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5831 return FALSE;
5832 }
5833 }
5834
7ee314fa
AM
5835 if (audit != NULL)
5836 {
5837 bfd_size_type indx;
5838
5839 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5840 TRUE);
5841 if (indx == (bfd_size_type) -1
5842 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5843 return FALSE;
5844 }
5845
5846 if (depaudit != NULL)
5847 {
5848 bfd_size_type indx;
5849
5850 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5851 TRUE);
5852 if (indx == (bfd_size_type) -1
5853 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5854 return FALSE;
5855 }
5856
5a580b3a 5857 eif.info = info;
5a580b3a
AM
5858 eif.failed = FALSE;
5859
5860 /* If we are supposed to export all symbols into the dynamic symbol
5861 table (this is not the normal case), then do so. */
55255dae 5862 if (info->export_dynamic
0e1862bb 5863 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5864 {
5865 elf_link_hash_traverse (elf_hash_table (info),
5866 _bfd_elf_export_symbol,
5867 &eif);
5868 if (eif.failed)
5869 return FALSE;
5870 }
5871
5872 /* Make all global versions with definition. */
fd91d419 5873 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5874 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5875 if (!d->symver && d->literal)
5a580b3a
AM
5876 {
5877 const char *verstr, *name;
5878 size_t namelen, verlen, newlen;
93252b1c 5879 char *newname, *p, leading_char;
5a580b3a
AM
5880 struct elf_link_hash_entry *newh;
5881
93252b1c 5882 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5883 name = d->pattern;
93252b1c 5884 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5885 verstr = t->name;
5886 verlen = strlen (verstr);
5887 newlen = namelen + verlen + 3;
5888
a50b1753 5889 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5890 if (newname == NULL)
5891 return FALSE;
93252b1c
MF
5892 newname[0] = leading_char;
5893 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5894
5895 /* Check the hidden versioned definition. */
5896 p = newname + namelen;
5897 *p++ = ELF_VER_CHR;
5898 memcpy (p, verstr, verlen + 1);
5899 newh = elf_link_hash_lookup (elf_hash_table (info),
5900 newname, FALSE, FALSE,
5901 FALSE);
5902 if (newh == NULL
5903 || (newh->root.type != bfd_link_hash_defined
5904 && newh->root.type != bfd_link_hash_defweak))
5905 {
5906 /* Check the default versioned definition. */
5907 *p++ = ELF_VER_CHR;
5908 memcpy (p, verstr, verlen + 1);
5909 newh = elf_link_hash_lookup (elf_hash_table (info),
5910 newname, FALSE, FALSE,
5911 FALSE);
5912 }
5913 free (newname);
5914
5915 /* Mark this version if there is a definition and it is
5916 not defined in a shared object. */
5917 if (newh != NULL
f5385ebf 5918 && !newh->def_dynamic
5a580b3a
AM
5919 && (newh->root.type == bfd_link_hash_defined
5920 || newh->root.type == bfd_link_hash_defweak))
5921 d->symver = 1;
5922 }
5923
5924 /* Attach all the symbols to their version information. */
5a580b3a 5925 asvinfo.info = info;
5a580b3a
AM
5926 asvinfo.failed = FALSE;
5927
5928 elf_link_hash_traverse (elf_hash_table (info),
5929 _bfd_elf_link_assign_sym_version,
5930 &asvinfo);
5931 if (asvinfo.failed)
5932 return FALSE;
5933
5934 if (!info->allow_undefined_version)
5935 {
5936 /* Check if all global versions have a definition. */
5937 all_defined = TRUE;
fd91d419 5938 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5939 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5940 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5941 {
5942 (*_bfd_error_handler)
5943 (_("%s: undefined version: %s"),
5944 d->pattern, t->name);
5945 all_defined = FALSE;
5946 }
5947
5948 if (!all_defined)
5949 {
5950 bfd_set_error (bfd_error_bad_value);
5951 return FALSE;
5952 }
5953 }
5954
5955 /* Find all symbols which were defined in a dynamic object and make
5956 the backend pick a reasonable value for them. */
5957 elf_link_hash_traverse (elf_hash_table (info),
5958 _bfd_elf_adjust_dynamic_symbol,
5959 &eif);
5960 if (eif.failed)
5961 return FALSE;
5962
5963 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5964 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5965 now so that we know the final size of the .dynamic section. */
5966
5967 /* If there are initialization and/or finalization functions to
5968 call then add the corresponding DT_INIT/DT_FINI entries. */
5969 h = (info->init_function
5970 ? elf_link_hash_lookup (elf_hash_table (info),
5971 info->init_function, FALSE,
5972 FALSE, FALSE)
5973 : NULL);
5974 if (h != NULL
f5385ebf
AM
5975 && (h->ref_regular
5976 || h->def_regular))
5a580b3a
AM
5977 {
5978 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5979 return FALSE;
5980 }
5981 h = (info->fini_function
5982 ? elf_link_hash_lookup (elf_hash_table (info),
5983 info->fini_function, FALSE,
5984 FALSE, FALSE)
5985 : NULL);
5986 if (h != NULL
f5385ebf
AM
5987 && (h->ref_regular
5988 || h->def_regular))
5a580b3a
AM
5989 {
5990 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5991 return FALSE;
5992 }
5993
046183de
AM
5994 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5995 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5996 {
5997 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 5998 if (! bfd_link_executable (info))
5a580b3a
AM
5999 {
6000 bfd *sub;
6001 asection *o;
6002
6003 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6004 sub = sub->link.next)
3fcd97f1
JJ
6005 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6006 for (o = sub->sections; o != NULL; o = o->next)
6007 if (elf_section_data (o)->this_hdr.sh_type
6008 == SHT_PREINIT_ARRAY)
6009 {
6010 (*_bfd_error_handler)
6011 (_("%B: .preinit_array section is not allowed in DSO"),
6012 sub);
6013 break;
6014 }
5a580b3a
AM
6015
6016 bfd_set_error (bfd_error_nonrepresentable_section);
6017 return FALSE;
6018 }
6019
6020 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6021 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6022 return FALSE;
6023 }
046183de
AM
6024 s = bfd_get_section_by_name (output_bfd, ".init_array");
6025 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6026 {
6027 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6028 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6029 return FALSE;
6030 }
046183de
AM
6031 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6032 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6033 {
6034 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6035 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6036 return FALSE;
6037 }
6038
3d4d4302 6039 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6040 /* If .dynstr is excluded from the link, we don't want any of
6041 these tags. Strictly, we should be checking each section
6042 individually; This quick check covers for the case where
6043 someone does a /DISCARD/ : { *(*) }. */
6044 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6045 {
6046 bfd_size_type strsize;
6047
6048 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6049 if ((info->emit_hash
6050 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6051 || (info->emit_gnu_hash
6052 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6053 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6054 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6055 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6056 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6057 bed->s->sizeof_sym))
6058 return FALSE;
6059 }
6060 }
6061
de231f20
CM
6062 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6063 return FALSE;
6064
5a580b3a
AM
6065 /* The backend must work out the sizes of all the other dynamic
6066 sections. */
9a2a56cc
AM
6067 if (dynobj != NULL
6068 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6069 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6070 return FALSE;
6071
9a2a56cc 6072 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6073 {
554220db 6074 unsigned long section_sym_count;
fd91d419 6075 struct bfd_elf_version_tree *verdefs;
5a580b3a 6076 asection *s;
5a580b3a
AM
6077
6078 /* Set up the version definition section. */
3d4d4302 6079 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6080 BFD_ASSERT (s != NULL);
6081
6082 /* We may have created additional version definitions if we are
6083 just linking a regular application. */
fd91d419 6084 verdefs = info->version_info;
5a580b3a
AM
6085
6086 /* Skip anonymous version tag. */
6087 if (verdefs != NULL && verdefs->vernum == 0)
6088 verdefs = verdefs->next;
6089
3e3b46e5 6090 if (verdefs == NULL && !info->create_default_symver)
8423293d 6091 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6092 else
6093 {
6094 unsigned int cdefs;
6095 bfd_size_type size;
6096 struct bfd_elf_version_tree *t;
6097 bfd_byte *p;
6098 Elf_Internal_Verdef def;
6099 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6100 struct bfd_link_hash_entry *bh;
6101 struct elf_link_hash_entry *h;
6102 const char *name;
5a580b3a
AM
6103
6104 cdefs = 0;
6105 size = 0;
6106
6107 /* Make space for the base version. */
6108 size += sizeof (Elf_External_Verdef);
6109 size += sizeof (Elf_External_Verdaux);
6110 ++cdefs;
6111
3e3b46e5
PB
6112 /* Make space for the default version. */
6113 if (info->create_default_symver)
6114 {
6115 size += sizeof (Elf_External_Verdef);
6116 ++cdefs;
6117 }
6118
5a580b3a
AM
6119 for (t = verdefs; t != NULL; t = t->next)
6120 {
6121 struct bfd_elf_version_deps *n;
6122
a6cc6b3b
RO
6123 /* Don't emit base version twice. */
6124 if (t->vernum == 0)
6125 continue;
6126
5a580b3a
AM
6127 size += sizeof (Elf_External_Verdef);
6128 size += sizeof (Elf_External_Verdaux);
6129 ++cdefs;
6130
6131 for (n = t->deps; n != NULL; n = n->next)
6132 size += sizeof (Elf_External_Verdaux);
6133 }
6134
eea6121a 6135 s->size = size;
a50b1753 6136 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6137 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6138 return FALSE;
6139
6140 /* Fill in the version definition section. */
6141
6142 p = s->contents;
6143
6144 def.vd_version = VER_DEF_CURRENT;
6145 def.vd_flags = VER_FLG_BASE;
6146 def.vd_ndx = 1;
6147 def.vd_cnt = 1;
3e3b46e5
PB
6148 if (info->create_default_symver)
6149 {
6150 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6151 def.vd_next = sizeof (Elf_External_Verdef);
6152 }
6153 else
6154 {
6155 def.vd_aux = sizeof (Elf_External_Verdef);
6156 def.vd_next = (sizeof (Elf_External_Verdef)
6157 + sizeof (Elf_External_Verdaux));
6158 }
5a580b3a
AM
6159
6160 if (soname_indx != (bfd_size_type) -1)
6161 {
6162 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6163 soname_indx);
6164 def.vd_hash = bfd_elf_hash (soname);
6165 defaux.vda_name = soname_indx;
3e3b46e5 6166 name = soname;
5a580b3a
AM
6167 }
6168 else
6169 {
5a580b3a
AM
6170 bfd_size_type indx;
6171
06084812 6172 name = lbasename (output_bfd->filename);
5a580b3a
AM
6173 def.vd_hash = bfd_elf_hash (name);
6174 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6175 name, FALSE);
6176 if (indx == (bfd_size_type) -1)
6177 return FALSE;
6178 defaux.vda_name = indx;
6179 }
6180 defaux.vda_next = 0;
6181
6182 _bfd_elf_swap_verdef_out (output_bfd, &def,
6183 (Elf_External_Verdef *) p);
6184 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6185 if (info->create_default_symver)
6186 {
6187 /* Add a symbol representing this version. */
6188 bh = NULL;
6189 if (! (_bfd_generic_link_add_one_symbol
6190 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6191 0, NULL, FALSE,
6192 get_elf_backend_data (dynobj)->collect, &bh)))
6193 return FALSE;
6194 h = (struct elf_link_hash_entry *) bh;
6195 h->non_elf = 0;
6196 h->def_regular = 1;
6197 h->type = STT_OBJECT;
6198 h->verinfo.vertree = NULL;
6199
6200 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6201 return FALSE;
6202
6203 /* Create a duplicate of the base version with the same
6204 aux block, but different flags. */
6205 def.vd_flags = 0;
6206 def.vd_ndx = 2;
6207 def.vd_aux = sizeof (Elf_External_Verdef);
6208 if (verdefs)
6209 def.vd_next = (sizeof (Elf_External_Verdef)
6210 + sizeof (Elf_External_Verdaux));
6211 else
6212 def.vd_next = 0;
6213 _bfd_elf_swap_verdef_out (output_bfd, &def,
6214 (Elf_External_Verdef *) p);
6215 p += sizeof (Elf_External_Verdef);
6216 }
5a580b3a
AM
6217 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6218 (Elf_External_Verdaux *) p);
6219 p += sizeof (Elf_External_Verdaux);
6220
6221 for (t = verdefs; t != NULL; t = t->next)
6222 {
6223 unsigned int cdeps;
6224 struct bfd_elf_version_deps *n;
5a580b3a 6225
a6cc6b3b
RO
6226 /* Don't emit the base version twice. */
6227 if (t->vernum == 0)
6228 continue;
6229
5a580b3a
AM
6230 cdeps = 0;
6231 for (n = t->deps; n != NULL; n = n->next)
6232 ++cdeps;
6233
6234 /* Add a symbol representing this version. */
6235 bh = NULL;
6236 if (! (_bfd_generic_link_add_one_symbol
6237 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6238 0, NULL, FALSE,
6239 get_elf_backend_data (dynobj)->collect, &bh)))
6240 return FALSE;
6241 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6242 h->non_elf = 0;
6243 h->def_regular = 1;
5a580b3a
AM
6244 h->type = STT_OBJECT;
6245 h->verinfo.vertree = t;
6246
c152c796 6247 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6248 return FALSE;
6249
6250 def.vd_version = VER_DEF_CURRENT;
6251 def.vd_flags = 0;
6252 if (t->globals.list == NULL
6253 && t->locals.list == NULL
6254 && ! t->used)
6255 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6256 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6257 def.vd_cnt = cdeps + 1;
6258 def.vd_hash = bfd_elf_hash (t->name);
6259 def.vd_aux = sizeof (Elf_External_Verdef);
6260 def.vd_next = 0;
a6cc6b3b
RO
6261
6262 /* If a basever node is next, it *must* be the last node in
6263 the chain, otherwise Verdef construction breaks. */
6264 if (t->next != NULL && t->next->vernum == 0)
6265 BFD_ASSERT (t->next->next == NULL);
6266
6267 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6268 def.vd_next = (sizeof (Elf_External_Verdef)
6269 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6270
6271 _bfd_elf_swap_verdef_out (output_bfd, &def,
6272 (Elf_External_Verdef *) p);
6273 p += sizeof (Elf_External_Verdef);
6274
6275 defaux.vda_name = h->dynstr_index;
6276 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6277 h->dynstr_index);
6278 defaux.vda_next = 0;
6279 if (t->deps != NULL)
6280 defaux.vda_next = sizeof (Elf_External_Verdaux);
6281 t->name_indx = defaux.vda_name;
6282
6283 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6284 (Elf_External_Verdaux *) p);
6285 p += sizeof (Elf_External_Verdaux);
6286
6287 for (n = t->deps; n != NULL; n = n->next)
6288 {
6289 if (n->version_needed == NULL)
6290 {
6291 /* This can happen if there was an error in the
6292 version script. */
6293 defaux.vda_name = 0;
6294 }
6295 else
6296 {
6297 defaux.vda_name = n->version_needed->name_indx;
6298 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6299 defaux.vda_name);
6300 }
6301 if (n->next == NULL)
6302 defaux.vda_next = 0;
6303 else
6304 defaux.vda_next = sizeof (Elf_External_Verdaux);
6305
6306 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6307 (Elf_External_Verdaux *) p);
6308 p += sizeof (Elf_External_Verdaux);
6309 }
6310 }
6311
6312 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6313 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6314 return FALSE;
6315
6316 elf_tdata (output_bfd)->cverdefs = cdefs;
6317 }
6318
6319 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6320 {
6321 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6322 return FALSE;
6323 }
6324 else if (info->flags & DF_BIND_NOW)
6325 {
6326 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6327 return FALSE;
6328 }
6329
6330 if (info->flags_1)
6331 {
0e1862bb 6332 if (bfd_link_executable (info))
5a580b3a
AM
6333 info->flags_1 &= ~ (DF_1_INITFIRST
6334 | DF_1_NODELETE
6335 | DF_1_NOOPEN);
6336 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6337 return FALSE;
6338 }
6339
6340 /* Work out the size of the version reference section. */
6341
3d4d4302 6342 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6343 BFD_ASSERT (s != NULL);
6344 {
6345 struct elf_find_verdep_info sinfo;
6346
5a580b3a
AM
6347 sinfo.info = info;
6348 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6349 if (sinfo.vers == 0)
6350 sinfo.vers = 1;
6351 sinfo.failed = FALSE;
6352
6353 elf_link_hash_traverse (elf_hash_table (info),
6354 _bfd_elf_link_find_version_dependencies,
6355 &sinfo);
14b1c01e
AM
6356 if (sinfo.failed)
6357 return FALSE;
5a580b3a
AM
6358
6359 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6360 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6361 else
6362 {
6363 Elf_Internal_Verneed *t;
6364 unsigned int size;
6365 unsigned int crefs;
6366 bfd_byte *p;
6367
a6cc6b3b 6368 /* Build the version dependency section. */
5a580b3a
AM
6369 size = 0;
6370 crefs = 0;
6371 for (t = elf_tdata (output_bfd)->verref;
6372 t != NULL;
6373 t = t->vn_nextref)
6374 {
6375 Elf_Internal_Vernaux *a;
6376
6377 size += sizeof (Elf_External_Verneed);
6378 ++crefs;
6379 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6380 size += sizeof (Elf_External_Vernaux);
6381 }
6382
eea6121a 6383 s->size = size;
a50b1753 6384 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6385 if (s->contents == NULL)
6386 return FALSE;
6387
6388 p = s->contents;
6389 for (t = elf_tdata (output_bfd)->verref;
6390 t != NULL;
6391 t = t->vn_nextref)
6392 {
6393 unsigned int caux;
6394 Elf_Internal_Vernaux *a;
6395 bfd_size_type indx;
6396
6397 caux = 0;
6398 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6399 ++caux;
6400
6401 t->vn_version = VER_NEED_CURRENT;
6402 t->vn_cnt = caux;
6403 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6404 elf_dt_name (t->vn_bfd) != NULL
6405 ? elf_dt_name (t->vn_bfd)
06084812 6406 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6407 FALSE);
6408 if (indx == (bfd_size_type) -1)
6409 return FALSE;
6410 t->vn_file = indx;
6411 t->vn_aux = sizeof (Elf_External_Verneed);
6412 if (t->vn_nextref == NULL)
6413 t->vn_next = 0;
6414 else
6415 t->vn_next = (sizeof (Elf_External_Verneed)
6416 + caux * sizeof (Elf_External_Vernaux));
6417
6418 _bfd_elf_swap_verneed_out (output_bfd, t,
6419 (Elf_External_Verneed *) p);
6420 p += sizeof (Elf_External_Verneed);
6421
6422 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6423 {
6424 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6425 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6426 a->vna_nodename, FALSE);
6427 if (indx == (bfd_size_type) -1)
6428 return FALSE;
6429 a->vna_name = indx;
6430 if (a->vna_nextptr == NULL)
6431 a->vna_next = 0;
6432 else
6433 a->vna_next = sizeof (Elf_External_Vernaux);
6434
6435 _bfd_elf_swap_vernaux_out (output_bfd, a,
6436 (Elf_External_Vernaux *) p);
6437 p += sizeof (Elf_External_Vernaux);
6438 }
6439 }
6440
6441 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6442 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6443 return FALSE;
6444
6445 elf_tdata (output_bfd)->cverrefs = crefs;
6446 }
6447 }
6448
8423293d
AM
6449 if ((elf_tdata (output_bfd)->cverrefs == 0
6450 && elf_tdata (output_bfd)->cverdefs == 0)
6451 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6452 &section_sym_count) == 0)
6453 {
3d4d4302 6454 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6455 s->flags |= SEC_EXCLUDE;
6456 }
6457 }
6458 return TRUE;
6459}
6460
74541ad4
AM
6461/* Find the first non-excluded output section. We'll use its
6462 section symbol for some emitted relocs. */
6463void
6464_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6465{
6466 asection *s;
6467
6468 for (s = output_bfd->sections; s != NULL; s = s->next)
6469 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6470 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6471 {
6472 elf_hash_table (info)->text_index_section = s;
6473 break;
6474 }
6475}
6476
6477/* Find two non-excluded output sections, one for code, one for data.
6478 We'll use their section symbols for some emitted relocs. */
6479void
6480_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6481{
6482 asection *s;
6483
266b05cf
DJ
6484 /* Data first, since setting text_index_section changes
6485 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6486 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6487 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6488 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6489 {
266b05cf 6490 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6491 break;
6492 }
6493
6494 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6495 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6496 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6497 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6498 {
266b05cf 6499 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6500 break;
6501 }
6502
6503 if (elf_hash_table (info)->text_index_section == NULL)
6504 elf_hash_table (info)->text_index_section
6505 = elf_hash_table (info)->data_index_section;
6506}
6507
8423293d
AM
6508bfd_boolean
6509bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6510{
74541ad4
AM
6511 const struct elf_backend_data *bed;
6512
8423293d
AM
6513 if (!is_elf_hash_table (info->hash))
6514 return TRUE;
6515
74541ad4
AM
6516 bed = get_elf_backend_data (output_bfd);
6517 (*bed->elf_backend_init_index_section) (output_bfd, info);
6518
8423293d
AM
6519 if (elf_hash_table (info)->dynamic_sections_created)
6520 {
6521 bfd *dynobj;
8423293d
AM
6522 asection *s;
6523 bfd_size_type dynsymcount;
6524 unsigned long section_sym_count;
8423293d
AM
6525 unsigned int dtagcount;
6526
6527 dynobj = elf_hash_table (info)->dynobj;
6528
5a580b3a
AM
6529 /* Assign dynsym indicies. In a shared library we generate a
6530 section symbol for each output section, which come first.
6531 Next come all of the back-end allocated local dynamic syms,
6532 followed by the rest of the global symbols. */
6533
554220db
AM
6534 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6535 &section_sym_count);
5a580b3a
AM
6536
6537 /* Work out the size of the symbol version section. */
3d4d4302 6538 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6539 BFD_ASSERT (s != NULL);
8423293d
AM
6540 if (dynsymcount != 0
6541 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6542 {
eea6121a 6543 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6544 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6545 if (s->contents == NULL)
6546 return FALSE;
6547
6548 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6549 return FALSE;
6550 }
6551
6552 /* Set the size of the .dynsym and .hash sections. We counted
6553 the number of dynamic symbols in elf_link_add_object_symbols.
6554 We will build the contents of .dynsym and .hash when we build
6555 the final symbol table, because until then we do not know the
6556 correct value to give the symbols. We built the .dynstr
6557 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6558 s = elf_hash_table (info)->dynsym;
5a580b3a 6559 BFD_ASSERT (s != NULL);
eea6121a 6560 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6561
6562 if (dynsymcount != 0)
6563 {
a50b1753 6564 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6565 if (s->contents == NULL)
6566 return FALSE;
5a580b3a 6567
554220db
AM
6568 /* The first entry in .dynsym is a dummy symbol.
6569 Clear all the section syms, in case we don't output them all. */
6570 ++section_sym_count;
6571 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6572 }
6573
fdc90cb4
JJ
6574 elf_hash_table (info)->bucketcount = 0;
6575
5a580b3a
AM
6576 /* Compute the size of the hashing table. As a side effect this
6577 computes the hash values for all the names we export. */
fdc90cb4
JJ
6578 if (info->emit_hash)
6579 {
6580 unsigned long int *hashcodes;
14b1c01e 6581 struct hash_codes_info hashinf;
fdc90cb4
JJ
6582 bfd_size_type amt;
6583 unsigned long int nsyms;
6584 size_t bucketcount;
6585 size_t hash_entry_size;
6586
6587 /* Compute the hash values for all exported symbols. At the same
6588 time store the values in an array so that we could use them for
6589 optimizations. */
6590 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6591 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6592 if (hashcodes == NULL)
6593 return FALSE;
14b1c01e
AM
6594 hashinf.hashcodes = hashcodes;
6595 hashinf.error = FALSE;
5a580b3a 6596
fdc90cb4
JJ
6597 /* Put all hash values in HASHCODES. */
6598 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6599 elf_collect_hash_codes, &hashinf);
6600 if (hashinf.error)
4dd07732
AM
6601 {
6602 free (hashcodes);
6603 return FALSE;
6604 }
5a580b3a 6605
14b1c01e 6606 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6607 bucketcount
6608 = compute_bucket_count (info, hashcodes, nsyms, 0);
6609 free (hashcodes);
6610
6611 if (bucketcount == 0)
6612 return FALSE;
5a580b3a 6613
fdc90cb4
JJ
6614 elf_hash_table (info)->bucketcount = bucketcount;
6615
3d4d4302 6616 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6617 BFD_ASSERT (s != NULL);
6618 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6619 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6620 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6621 if (s->contents == NULL)
6622 return FALSE;
6623
6624 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6625 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6626 s->contents + hash_entry_size);
6627 }
6628
6629 if (info->emit_gnu_hash)
6630 {
6631 size_t i, cnt;
6632 unsigned char *contents;
6633 struct collect_gnu_hash_codes cinfo;
6634 bfd_size_type amt;
6635 size_t bucketcount;
6636
6637 memset (&cinfo, 0, sizeof (cinfo));
6638
6639 /* Compute the hash values for all exported symbols. At the same
6640 time store the values in an array so that we could use them for
6641 optimizations. */
6642 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6643 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6644 if (cinfo.hashcodes == NULL)
6645 return FALSE;
6646
6647 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6648 cinfo.min_dynindx = -1;
6649 cinfo.output_bfd = output_bfd;
6650 cinfo.bed = bed;
6651
6652 /* Put all hash values in HASHCODES. */
6653 elf_link_hash_traverse (elf_hash_table (info),
6654 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6655 if (cinfo.error)
4dd07732
AM
6656 {
6657 free (cinfo.hashcodes);
6658 return FALSE;
6659 }
fdc90cb4
JJ
6660
6661 bucketcount
6662 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6663
6664 if (bucketcount == 0)
6665 {
6666 free (cinfo.hashcodes);
6667 return FALSE;
6668 }
6669
3d4d4302 6670 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6671 BFD_ASSERT (s != NULL);
6672
6673 if (cinfo.nsyms == 0)
6674 {
6675 /* Empty .gnu.hash section is special. */
6676 BFD_ASSERT (cinfo.min_dynindx == -1);
6677 free (cinfo.hashcodes);
6678 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6679 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6680 if (contents == NULL)
6681 return FALSE;
6682 s->contents = contents;
6683 /* 1 empty bucket. */
6684 bfd_put_32 (output_bfd, 1, contents);
6685 /* SYMIDX above the special symbol 0. */
6686 bfd_put_32 (output_bfd, 1, contents + 4);
6687 /* Just one word for bitmask. */
6688 bfd_put_32 (output_bfd, 1, contents + 8);
6689 /* Only hash fn bloom filter. */
6690 bfd_put_32 (output_bfd, 0, contents + 12);
6691 /* No hashes are valid - empty bitmask. */
6692 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6693 /* No hashes in the only bucket. */
6694 bfd_put_32 (output_bfd, 0,
6695 contents + 16 + bed->s->arch_size / 8);
6696 }
6697 else
6698 {
9e6619e2 6699 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6700 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6701
9e6619e2
AM
6702 x = cinfo.nsyms;
6703 maskbitslog2 = 1;
6704 while ((x >>= 1) != 0)
6705 ++maskbitslog2;
fdc90cb4
JJ
6706 if (maskbitslog2 < 3)
6707 maskbitslog2 = 5;
6708 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6709 maskbitslog2 = maskbitslog2 + 3;
6710 else
6711 maskbitslog2 = maskbitslog2 + 2;
6712 if (bed->s->arch_size == 64)
6713 {
6714 if (maskbitslog2 == 5)
6715 maskbitslog2 = 6;
6716 cinfo.shift1 = 6;
6717 }
6718 else
6719 cinfo.shift1 = 5;
6720 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6721 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6722 cinfo.maskbits = 1 << maskbitslog2;
6723 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6724 amt = bucketcount * sizeof (unsigned long int) * 2;
6725 amt += maskwords * sizeof (bfd_vma);
a50b1753 6726 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6727 if (cinfo.bitmask == NULL)
6728 {
6729 free (cinfo.hashcodes);
6730 return FALSE;
6731 }
6732
a50b1753 6733 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6734 cinfo.indx = cinfo.counts + bucketcount;
6735 cinfo.symindx = dynsymcount - cinfo.nsyms;
6736 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6737
6738 /* Determine how often each hash bucket is used. */
6739 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6740 for (i = 0; i < cinfo.nsyms; ++i)
6741 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6742
6743 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6744 if (cinfo.counts[i] != 0)
6745 {
6746 cinfo.indx[i] = cnt;
6747 cnt += cinfo.counts[i];
6748 }
6749 BFD_ASSERT (cnt == dynsymcount);
6750 cinfo.bucketcount = bucketcount;
6751 cinfo.local_indx = cinfo.min_dynindx;
6752
6753 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6754 s->size += cinfo.maskbits / 8;
a50b1753 6755 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6756 if (contents == NULL)
6757 {
6758 free (cinfo.bitmask);
6759 free (cinfo.hashcodes);
6760 return FALSE;
6761 }
6762
6763 s->contents = contents;
6764 bfd_put_32 (output_bfd, bucketcount, contents);
6765 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6766 bfd_put_32 (output_bfd, maskwords, contents + 8);
6767 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6768 contents += 16 + cinfo.maskbits / 8;
6769
6770 for (i = 0; i < bucketcount; ++i)
6771 {
6772 if (cinfo.counts[i] == 0)
6773 bfd_put_32 (output_bfd, 0, contents);
6774 else
6775 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6776 contents += 4;
6777 }
6778
6779 cinfo.contents = contents;
6780
6781 /* Renumber dynamic symbols, populate .gnu.hash section. */
6782 elf_link_hash_traverse (elf_hash_table (info),
6783 elf_renumber_gnu_hash_syms, &cinfo);
6784
6785 contents = s->contents + 16;
6786 for (i = 0; i < maskwords; ++i)
6787 {
6788 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6789 contents);
6790 contents += bed->s->arch_size / 8;
6791 }
6792
6793 free (cinfo.bitmask);
6794 free (cinfo.hashcodes);
6795 }
6796 }
5a580b3a 6797
3d4d4302 6798 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6799 BFD_ASSERT (s != NULL);
6800
4ad4eba5 6801 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6802
eea6121a 6803 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6804
6805 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6806 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6807 return FALSE;
6808 }
6809
6810 return TRUE;
6811}
4d269e42 6812\f
4d269e42
AM
6813/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6814
6815static void
6816merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6817 asection *sec)
6818{
dbaa2011
AM
6819 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6820 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6821}
6822
6823/* Finish SHF_MERGE section merging. */
6824
6825bfd_boolean
630993ec 6826_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6827{
6828 bfd *ibfd;
6829 asection *sec;
6830
6831 if (!is_elf_hash_table (info->hash))
6832 return FALSE;
6833
c72f2fb2 6834 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6835 if ((ibfd->flags & DYNAMIC) == 0
6836 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6837 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6838 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6839 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6840 if ((sec->flags & SEC_MERGE) != 0
6841 && !bfd_is_abs_section (sec->output_section))
6842 {
6843 struct bfd_elf_section_data *secdata;
6844
6845 secdata = elf_section_data (sec);
630993ec 6846 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6847 &elf_hash_table (info)->merge_info,
6848 sec, &secdata->sec_info))
6849 return FALSE;
6850 else if (secdata->sec_info)
dbaa2011 6851 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6852 }
6853
6854 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6855 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6856 merge_sections_remove_hook);
6857 return TRUE;
6858}
6859
6860/* Create an entry in an ELF linker hash table. */
6861
6862struct bfd_hash_entry *
6863_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6864 struct bfd_hash_table *table,
6865 const char *string)
6866{
6867 /* Allocate the structure if it has not already been allocated by a
6868 subclass. */
6869 if (entry == NULL)
6870 {
a50b1753 6871 entry = (struct bfd_hash_entry *)
ca4be51c 6872 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6873 if (entry == NULL)
6874 return entry;
6875 }
6876
6877 /* Call the allocation method of the superclass. */
6878 entry = _bfd_link_hash_newfunc (entry, table, string);
6879 if (entry != NULL)
6880 {
6881 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6882 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6883
6884 /* Set local fields. */
6885 ret->indx = -1;
6886 ret->dynindx = -1;
6887 ret->got = htab->init_got_refcount;
6888 ret->plt = htab->init_plt_refcount;
6889 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6890 - offsetof (struct elf_link_hash_entry, size)));
6891 /* Assume that we have been called by a non-ELF symbol reader.
6892 This flag is then reset by the code which reads an ELF input
6893 file. This ensures that a symbol created by a non-ELF symbol
6894 reader will have the flag set correctly. */
6895 ret->non_elf = 1;
6896 }
6897
6898 return entry;
6899}
6900
6901/* Copy data from an indirect symbol to its direct symbol, hiding the
6902 old indirect symbol. Also used for copying flags to a weakdef. */
6903
6904void
6905_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6906 struct elf_link_hash_entry *dir,
6907 struct elf_link_hash_entry *ind)
6908{
6909 struct elf_link_hash_table *htab;
6910
6911 /* Copy down any references that we may have already seen to the
6e33951e
L
6912 symbol which just became indirect if DIR isn't a hidden versioned
6913 symbol. */
4d269e42 6914
422f1182 6915 if (dir->versioned != versioned_hidden)
6e33951e
L
6916 {
6917 dir->ref_dynamic |= ind->ref_dynamic;
6918 dir->ref_regular |= ind->ref_regular;
6919 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6920 dir->non_got_ref |= ind->non_got_ref;
6921 dir->needs_plt |= ind->needs_plt;
6922 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6923 }
4d269e42
AM
6924
6925 if (ind->root.type != bfd_link_hash_indirect)
6926 return;
6927
6928 /* Copy over the global and procedure linkage table refcount entries.
6929 These may have been already set up by a check_relocs routine. */
6930 htab = elf_hash_table (info);
6931 if (ind->got.refcount > htab->init_got_refcount.refcount)
6932 {
6933 if (dir->got.refcount < 0)
6934 dir->got.refcount = 0;
6935 dir->got.refcount += ind->got.refcount;
6936 ind->got.refcount = htab->init_got_refcount.refcount;
6937 }
6938
6939 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6940 {
6941 if (dir->plt.refcount < 0)
6942 dir->plt.refcount = 0;
6943 dir->plt.refcount += ind->plt.refcount;
6944 ind->plt.refcount = htab->init_plt_refcount.refcount;
6945 }
6946
6947 if (ind->dynindx != -1)
6948 {
6949 if (dir->dynindx != -1)
6950 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6951 dir->dynindx = ind->dynindx;
6952 dir->dynstr_index = ind->dynstr_index;
6953 ind->dynindx = -1;
6954 ind->dynstr_index = 0;
6955 }
6956}
6957
6958void
6959_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6960 struct elf_link_hash_entry *h,
6961 bfd_boolean force_local)
6962{
3aa14d16
L
6963 /* STT_GNU_IFUNC symbol must go through PLT. */
6964 if (h->type != STT_GNU_IFUNC)
6965 {
6966 h->plt = elf_hash_table (info)->init_plt_offset;
6967 h->needs_plt = 0;
6968 }
4d269e42
AM
6969 if (force_local)
6970 {
6971 h->forced_local = 1;
6972 if (h->dynindx != -1)
6973 {
6974 h->dynindx = -1;
6975 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6976 h->dynstr_index);
6977 }
6978 }
6979}
6980
7bf52ea2
AM
6981/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6982 caller. */
4d269e42
AM
6983
6984bfd_boolean
6985_bfd_elf_link_hash_table_init
6986 (struct elf_link_hash_table *table,
6987 bfd *abfd,
6988 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6989 struct bfd_hash_table *,
6990 const char *),
4dfe6ac6
NC
6991 unsigned int entsize,
6992 enum elf_target_id target_id)
4d269e42
AM
6993{
6994 bfd_boolean ret;
6995 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6996
4d269e42
AM
6997 table->init_got_refcount.refcount = can_refcount - 1;
6998 table->init_plt_refcount.refcount = can_refcount - 1;
6999 table->init_got_offset.offset = -(bfd_vma) 1;
7000 table->init_plt_offset.offset = -(bfd_vma) 1;
7001 /* The first dynamic symbol is a dummy. */
7002 table->dynsymcount = 1;
7003
7004 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7005
4d269e42 7006 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7007 table->hash_table_id = target_id;
4d269e42
AM
7008
7009 return ret;
7010}
7011
7012/* Create an ELF linker hash table. */
7013
7014struct bfd_link_hash_table *
7015_bfd_elf_link_hash_table_create (bfd *abfd)
7016{
7017 struct elf_link_hash_table *ret;
7018 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7019
7bf52ea2 7020 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7021 if (ret == NULL)
7022 return NULL;
7023
7024 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7025 sizeof (struct elf_link_hash_entry),
7026 GENERIC_ELF_DATA))
4d269e42
AM
7027 {
7028 free (ret);
7029 return NULL;
7030 }
d495ab0d 7031 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7032
7033 return &ret->root;
7034}
7035
9f7c3e5e
AM
7036/* Destroy an ELF linker hash table. */
7037
7038void
d495ab0d 7039_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7040{
d495ab0d
AM
7041 struct elf_link_hash_table *htab;
7042
7043 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7044 if (htab->dynstr != NULL)
7045 _bfd_elf_strtab_free (htab->dynstr);
7046 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7047 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7048}
7049
4d269e42
AM
7050/* This is a hook for the ELF emulation code in the generic linker to
7051 tell the backend linker what file name to use for the DT_NEEDED
7052 entry for a dynamic object. */
7053
7054void
7055bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7056{
7057 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7058 && bfd_get_format (abfd) == bfd_object)
7059 elf_dt_name (abfd) = name;
7060}
7061
7062int
7063bfd_elf_get_dyn_lib_class (bfd *abfd)
7064{
7065 int lib_class;
7066 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7067 && bfd_get_format (abfd) == bfd_object)
7068 lib_class = elf_dyn_lib_class (abfd);
7069 else
7070 lib_class = 0;
7071 return lib_class;
7072}
7073
7074void
7075bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7076{
7077 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7078 && bfd_get_format (abfd) == bfd_object)
7079 elf_dyn_lib_class (abfd) = lib_class;
7080}
7081
7082/* Get the list of DT_NEEDED entries for a link. This is a hook for
7083 the linker ELF emulation code. */
7084
7085struct bfd_link_needed_list *
7086bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7087 struct bfd_link_info *info)
7088{
7089 if (! is_elf_hash_table (info->hash))
7090 return NULL;
7091 return elf_hash_table (info)->needed;
7092}
7093
7094/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7095 hook for the linker ELF emulation code. */
7096
7097struct bfd_link_needed_list *
7098bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7099 struct bfd_link_info *info)
7100{
7101 if (! is_elf_hash_table (info->hash))
7102 return NULL;
7103 return elf_hash_table (info)->runpath;
7104}
7105
7106/* Get the name actually used for a dynamic object for a link. This
7107 is the SONAME entry if there is one. Otherwise, it is the string
7108 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7109
7110const char *
7111bfd_elf_get_dt_soname (bfd *abfd)
7112{
7113 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7114 && bfd_get_format (abfd) == bfd_object)
7115 return elf_dt_name (abfd);
7116 return NULL;
7117}
7118
7119/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7120 the ELF linker emulation code. */
7121
7122bfd_boolean
7123bfd_elf_get_bfd_needed_list (bfd *abfd,
7124 struct bfd_link_needed_list **pneeded)
7125{
7126 asection *s;
7127 bfd_byte *dynbuf = NULL;
cb33740c 7128 unsigned int elfsec;
4d269e42
AM
7129 unsigned long shlink;
7130 bfd_byte *extdyn, *extdynend;
7131 size_t extdynsize;
7132 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7133
7134 *pneeded = NULL;
7135
7136 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7137 || bfd_get_format (abfd) != bfd_object)
7138 return TRUE;
7139
7140 s = bfd_get_section_by_name (abfd, ".dynamic");
7141 if (s == NULL || s->size == 0)
7142 return TRUE;
7143
7144 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7145 goto error_return;
7146
7147 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7148 if (elfsec == SHN_BAD)
4d269e42
AM
7149 goto error_return;
7150
7151 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7152
4d269e42
AM
7153 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7154 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7155
7156 extdyn = dynbuf;
7157 extdynend = extdyn + s->size;
7158 for (; extdyn < extdynend; extdyn += extdynsize)
7159 {
7160 Elf_Internal_Dyn dyn;
7161
7162 (*swap_dyn_in) (abfd, extdyn, &dyn);
7163
7164 if (dyn.d_tag == DT_NULL)
7165 break;
7166
7167 if (dyn.d_tag == DT_NEEDED)
7168 {
7169 const char *string;
7170 struct bfd_link_needed_list *l;
7171 unsigned int tagv = dyn.d_un.d_val;
7172 bfd_size_type amt;
7173
7174 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7175 if (string == NULL)
7176 goto error_return;
7177
7178 amt = sizeof *l;
a50b1753 7179 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7180 if (l == NULL)
7181 goto error_return;
7182
7183 l->by = abfd;
7184 l->name = string;
7185 l->next = *pneeded;
7186 *pneeded = l;
7187 }
7188 }
7189
7190 free (dynbuf);
7191
7192 return TRUE;
7193
7194 error_return:
7195 if (dynbuf != NULL)
7196 free (dynbuf);
7197 return FALSE;
7198}
7199
7200struct elf_symbuf_symbol
7201{
7202 unsigned long st_name; /* Symbol name, index in string tbl */
7203 unsigned char st_info; /* Type and binding attributes */
7204 unsigned char st_other; /* Visibilty, and target specific */
7205};
7206
7207struct elf_symbuf_head
7208{
7209 struct elf_symbuf_symbol *ssym;
7210 bfd_size_type count;
7211 unsigned int st_shndx;
7212};
7213
7214struct elf_symbol
7215{
7216 union
7217 {
7218 Elf_Internal_Sym *isym;
7219 struct elf_symbuf_symbol *ssym;
7220 } u;
7221 const char *name;
7222};
7223
7224/* Sort references to symbols by ascending section number. */
7225
7226static int
7227elf_sort_elf_symbol (const void *arg1, const void *arg2)
7228{
7229 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7230 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7231
7232 return s1->st_shndx - s2->st_shndx;
7233}
7234
7235static int
7236elf_sym_name_compare (const void *arg1, const void *arg2)
7237{
7238 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7239 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7240 return strcmp (s1->name, s2->name);
7241}
7242
7243static struct elf_symbuf_head *
7244elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7245{
14b1c01e 7246 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7247 struct elf_symbuf_symbol *ssym;
7248 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7249 bfd_size_type i, shndx_count, total_size;
4d269e42 7250
a50b1753 7251 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7252 if (indbuf == NULL)
7253 return NULL;
7254
7255 for (ind = indbuf, i = 0; i < symcount; i++)
7256 if (isymbuf[i].st_shndx != SHN_UNDEF)
7257 *ind++ = &isymbuf[i];
7258 indbufend = ind;
7259
7260 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7261 elf_sort_elf_symbol);
7262
7263 shndx_count = 0;
7264 if (indbufend > indbuf)
7265 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7266 if (ind[0]->st_shndx != ind[1]->st_shndx)
7267 shndx_count++;
7268
3ae181ee
L
7269 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7270 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7271 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7272 if (ssymbuf == NULL)
7273 {
7274 free (indbuf);
7275 return NULL;
7276 }
7277
3ae181ee 7278 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7279 ssymbuf->ssym = NULL;
7280 ssymbuf->count = shndx_count;
7281 ssymbuf->st_shndx = 0;
7282 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7283 {
7284 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7285 {
7286 ssymhead++;
7287 ssymhead->ssym = ssym;
7288 ssymhead->count = 0;
7289 ssymhead->st_shndx = (*ind)->st_shndx;
7290 }
7291 ssym->st_name = (*ind)->st_name;
7292 ssym->st_info = (*ind)->st_info;
7293 ssym->st_other = (*ind)->st_other;
7294 ssymhead->count++;
7295 }
3ae181ee
L
7296 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7297 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7298 == total_size));
4d269e42
AM
7299
7300 free (indbuf);
7301 return ssymbuf;
7302}
7303
7304/* Check if 2 sections define the same set of local and global
7305 symbols. */
7306
8f317e31 7307static bfd_boolean
4d269e42
AM
7308bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7309 struct bfd_link_info *info)
7310{
7311 bfd *bfd1, *bfd2;
7312 const struct elf_backend_data *bed1, *bed2;
7313 Elf_Internal_Shdr *hdr1, *hdr2;
7314 bfd_size_type symcount1, symcount2;
7315 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7316 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7317 Elf_Internal_Sym *isym, *isymend;
7318 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7319 bfd_size_type count1, count2, i;
cb33740c 7320 unsigned int shndx1, shndx2;
4d269e42
AM
7321 bfd_boolean result;
7322
7323 bfd1 = sec1->owner;
7324 bfd2 = sec2->owner;
7325
4d269e42
AM
7326 /* Both sections have to be in ELF. */
7327 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7328 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7329 return FALSE;
7330
7331 if (elf_section_type (sec1) != elf_section_type (sec2))
7332 return FALSE;
7333
4d269e42
AM
7334 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7335 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7336 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7337 return FALSE;
7338
7339 bed1 = get_elf_backend_data (bfd1);
7340 bed2 = get_elf_backend_data (bfd2);
7341 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7342 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7343 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7344 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7345
7346 if (symcount1 == 0 || symcount2 == 0)
7347 return FALSE;
7348
7349 result = FALSE;
7350 isymbuf1 = NULL;
7351 isymbuf2 = NULL;
a50b1753
NC
7352 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7353 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7354
7355 if (ssymbuf1 == NULL)
7356 {
7357 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7358 NULL, NULL, NULL);
7359 if (isymbuf1 == NULL)
7360 goto done;
7361
7362 if (!info->reduce_memory_overheads)
7363 elf_tdata (bfd1)->symbuf = ssymbuf1
7364 = elf_create_symbuf (symcount1, isymbuf1);
7365 }
7366
7367 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7368 {
7369 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7370 NULL, NULL, NULL);
7371 if (isymbuf2 == NULL)
7372 goto done;
7373
7374 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7375 elf_tdata (bfd2)->symbuf = ssymbuf2
7376 = elf_create_symbuf (symcount2, isymbuf2);
7377 }
7378
7379 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7380 {
7381 /* Optimized faster version. */
7382 bfd_size_type lo, hi, mid;
7383 struct elf_symbol *symp;
7384 struct elf_symbuf_symbol *ssym, *ssymend;
7385
7386 lo = 0;
7387 hi = ssymbuf1->count;
7388 ssymbuf1++;
7389 count1 = 0;
7390 while (lo < hi)
7391 {
7392 mid = (lo + hi) / 2;
cb33740c 7393 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7394 hi = mid;
cb33740c 7395 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7396 lo = mid + 1;
7397 else
7398 {
7399 count1 = ssymbuf1[mid].count;
7400 ssymbuf1 += mid;
7401 break;
7402 }
7403 }
7404
7405 lo = 0;
7406 hi = ssymbuf2->count;
7407 ssymbuf2++;
7408 count2 = 0;
7409 while (lo < hi)
7410 {
7411 mid = (lo + hi) / 2;
cb33740c 7412 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7413 hi = mid;
cb33740c 7414 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7415 lo = mid + 1;
7416 else
7417 {
7418 count2 = ssymbuf2[mid].count;
7419 ssymbuf2 += mid;
7420 break;
7421 }
7422 }
7423
7424 if (count1 == 0 || count2 == 0 || count1 != count2)
7425 goto done;
7426
ca4be51c
AM
7427 symtable1
7428 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7429 symtable2
7430 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7431 if (symtable1 == NULL || symtable2 == NULL)
7432 goto done;
7433
7434 symp = symtable1;
7435 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7436 ssym < ssymend; ssym++, symp++)
7437 {
7438 symp->u.ssym = ssym;
7439 symp->name = bfd_elf_string_from_elf_section (bfd1,
7440 hdr1->sh_link,
7441 ssym->st_name);
7442 }
7443
7444 symp = symtable2;
7445 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7446 ssym < ssymend; ssym++, symp++)
7447 {
7448 symp->u.ssym = ssym;
7449 symp->name = bfd_elf_string_from_elf_section (bfd2,
7450 hdr2->sh_link,
7451 ssym->st_name);
7452 }
7453
7454 /* Sort symbol by name. */
7455 qsort (symtable1, count1, sizeof (struct elf_symbol),
7456 elf_sym_name_compare);
7457 qsort (symtable2, count1, sizeof (struct elf_symbol),
7458 elf_sym_name_compare);
7459
7460 for (i = 0; i < count1; i++)
7461 /* Two symbols must have the same binding, type and name. */
7462 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7463 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7464 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7465 goto done;
7466
7467 result = TRUE;
7468 goto done;
7469 }
7470
a50b1753
NC
7471 symtable1 = (struct elf_symbol *)
7472 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7473 symtable2 = (struct elf_symbol *)
7474 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7475 if (symtable1 == NULL || symtable2 == NULL)
7476 goto done;
7477
7478 /* Count definitions in the section. */
7479 count1 = 0;
7480 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7481 if (isym->st_shndx == shndx1)
4d269e42
AM
7482 symtable1[count1++].u.isym = isym;
7483
7484 count2 = 0;
7485 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7486 if (isym->st_shndx == shndx2)
4d269e42
AM
7487 symtable2[count2++].u.isym = isym;
7488
7489 if (count1 == 0 || count2 == 0 || count1 != count2)
7490 goto done;
7491
7492 for (i = 0; i < count1; i++)
7493 symtable1[i].name
7494 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7495 symtable1[i].u.isym->st_name);
7496
7497 for (i = 0; i < count2; i++)
7498 symtable2[i].name
7499 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7500 symtable2[i].u.isym->st_name);
7501
7502 /* Sort symbol by name. */
7503 qsort (symtable1, count1, sizeof (struct elf_symbol),
7504 elf_sym_name_compare);
7505 qsort (symtable2, count1, sizeof (struct elf_symbol),
7506 elf_sym_name_compare);
7507
7508 for (i = 0; i < count1; i++)
7509 /* Two symbols must have the same binding, type and name. */
7510 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7511 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7512 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7513 goto done;
7514
7515 result = TRUE;
7516
7517done:
7518 if (symtable1)
7519 free (symtable1);
7520 if (symtable2)
7521 free (symtable2);
7522 if (isymbuf1)
7523 free (isymbuf1);
7524 if (isymbuf2)
7525 free (isymbuf2);
7526
7527 return result;
7528}
7529
7530/* Return TRUE if 2 section types are compatible. */
7531
7532bfd_boolean
7533_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7534 bfd *bbfd, const asection *bsec)
7535{
7536 if (asec == NULL
7537 || bsec == NULL
7538 || abfd->xvec->flavour != bfd_target_elf_flavour
7539 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7540 return TRUE;
7541
7542 return elf_section_type (asec) == elf_section_type (bsec);
7543}
7544\f
c152c796
AM
7545/* Final phase of ELF linker. */
7546
7547/* A structure we use to avoid passing large numbers of arguments. */
7548
7549struct elf_final_link_info
7550{
7551 /* General link information. */
7552 struct bfd_link_info *info;
7553 /* Output BFD. */
7554 bfd *output_bfd;
7555 /* Symbol string table. */
ef10c3ac 7556 struct elf_strtab_hash *symstrtab;
c152c796
AM
7557 /* .hash section. */
7558 asection *hash_sec;
7559 /* symbol version section (.gnu.version). */
7560 asection *symver_sec;
7561 /* Buffer large enough to hold contents of any section. */
7562 bfd_byte *contents;
7563 /* Buffer large enough to hold external relocs of any section. */
7564 void *external_relocs;
7565 /* Buffer large enough to hold internal relocs of any section. */
7566 Elf_Internal_Rela *internal_relocs;
7567 /* Buffer large enough to hold external local symbols of any input
7568 BFD. */
7569 bfd_byte *external_syms;
7570 /* And a buffer for symbol section indices. */
7571 Elf_External_Sym_Shndx *locsym_shndx;
7572 /* Buffer large enough to hold internal local symbols of any input
7573 BFD. */
7574 Elf_Internal_Sym *internal_syms;
7575 /* Array large enough to hold a symbol index for each local symbol
7576 of any input BFD. */
7577 long *indices;
7578 /* Array large enough to hold a section pointer for each local
7579 symbol of any input BFD. */
7580 asection **sections;
ef10c3ac 7581 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7582 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7583 /* Number of STT_FILE syms seen. */
7584 size_t filesym_count;
c152c796
AM
7585};
7586
7587/* This struct is used to pass information to elf_link_output_extsym. */
7588
7589struct elf_outext_info
7590{
7591 bfd_boolean failed;
7592 bfd_boolean localsyms;
34a79995 7593 bfd_boolean file_sym_done;
8b127cbc 7594 struct elf_final_link_info *flinfo;
c152c796
AM
7595};
7596
d9352518
DB
7597
7598/* Support for evaluating a complex relocation.
7599
7600 Complex relocations are generalized, self-describing relocations. The
7601 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7602 relocations themselves.
d9352518
DB
7603
7604 The relocations are use a reserved elf-wide relocation type code (R_RELC
7605 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7606 information (start bit, end bit, word width, etc) into the addend. This
7607 information is extracted from CGEN-generated operand tables within gas.
7608
7609 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7610 internal) representing prefix-notation expressions, including but not
7611 limited to those sorts of expressions normally encoded as addends in the
7612 addend field. The symbol mangling format is:
7613
7614 <node> := <literal>
7615 | <unary-operator> ':' <node>
7616 | <binary-operator> ':' <node> ':' <node>
7617 ;
7618
7619 <literal> := 's' <digits=N> ':' <N character symbol name>
7620 | 'S' <digits=N> ':' <N character section name>
7621 | '#' <hexdigits>
7622 ;
7623
7624 <binary-operator> := as in C
7625 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7626
7627static void
a0c8462f
AM
7628set_symbol_value (bfd *bfd_with_globals,
7629 Elf_Internal_Sym *isymbuf,
7630 size_t locsymcount,
7631 size_t symidx,
7632 bfd_vma val)
d9352518 7633{
8977835c
AM
7634 struct elf_link_hash_entry **sym_hashes;
7635 struct elf_link_hash_entry *h;
7636 size_t extsymoff = locsymcount;
d9352518 7637
8977835c 7638 if (symidx < locsymcount)
d9352518 7639 {
8977835c
AM
7640 Elf_Internal_Sym *sym;
7641
7642 sym = isymbuf + symidx;
7643 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7644 {
7645 /* It is a local symbol: move it to the
7646 "absolute" section and give it a value. */
7647 sym->st_shndx = SHN_ABS;
7648 sym->st_value = val;
7649 return;
7650 }
7651 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7652 extsymoff = 0;
d9352518 7653 }
8977835c
AM
7654
7655 /* It is a global symbol: set its link type
7656 to "defined" and give it a value. */
7657
7658 sym_hashes = elf_sym_hashes (bfd_with_globals);
7659 h = sym_hashes [symidx - extsymoff];
7660 while (h->root.type == bfd_link_hash_indirect
7661 || h->root.type == bfd_link_hash_warning)
7662 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7663 h->root.type = bfd_link_hash_defined;
7664 h->root.u.def.value = val;
7665 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7666}
7667
a0c8462f
AM
7668static bfd_boolean
7669resolve_symbol (const char *name,
7670 bfd *input_bfd,
8b127cbc 7671 struct elf_final_link_info *flinfo,
a0c8462f
AM
7672 bfd_vma *result,
7673 Elf_Internal_Sym *isymbuf,
7674 size_t locsymcount)
d9352518 7675{
a0c8462f
AM
7676 Elf_Internal_Sym *sym;
7677 struct bfd_link_hash_entry *global_entry;
7678 const char *candidate = NULL;
7679 Elf_Internal_Shdr *symtab_hdr;
7680 size_t i;
7681
d9352518
DB
7682 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7683
7684 for (i = 0; i < locsymcount; ++ i)
7685 {
8977835c 7686 sym = isymbuf + i;
d9352518
DB
7687
7688 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7689 continue;
7690
7691 candidate = bfd_elf_string_from_elf_section (input_bfd,
7692 symtab_hdr->sh_link,
7693 sym->st_name);
7694#ifdef DEBUG
0f02bbd9
AM
7695 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7696 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7697#endif
7698 if (candidate && strcmp (candidate, name) == 0)
7699 {
8b127cbc 7700 asection *sec = flinfo->sections [i];
d9352518 7701
0f02bbd9
AM
7702 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7703 *result += sec->output_offset + sec->output_section->vma;
d9352518 7704#ifdef DEBUG
0f02bbd9
AM
7705 printf ("Found symbol with value %8.8lx\n",
7706 (unsigned long) *result);
d9352518
DB
7707#endif
7708 return TRUE;
7709 }
7710 }
7711
7712 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7713 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7714 FALSE, FALSE, TRUE);
d9352518
DB
7715 if (!global_entry)
7716 return FALSE;
a0c8462f 7717
d9352518
DB
7718 if (global_entry->type == bfd_link_hash_defined
7719 || global_entry->type == bfd_link_hash_defweak)
7720 {
a0c8462f
AM
7721 *result = (global_entry->u.def.value
7722 + global_entry->u.def.section->output_section->vma
7723 + global_entry->u.def.section->output_offset);
d9352518 7724#ifdef DEBUG
0f02bbd9
AM
7725 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7726 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7727#endif
7728 return TRUE;
a0c8462f 7729 }
d9352518 7730
d9352518
DB
7731 return FALSE;
7732}
7733
7734static bfd_boolean
a0c8462f
AM
7735resolve_section (const char *name,
7736 asection *sections,
7737 bfd_vma *result)
d9352518 7738{
a0c8462f
AM
7739 asection *curr;
7740 unsigned int len;
d9352518 7741
a0c8462f 7742 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7743 if (strcmp (curr->name, name) == 0)
7744 {
7745 *result = curr->vma;
7746 return TRUE;
7747 }
7748
7749 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7750 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7751 {
7752 len = strlen (curr->name);
a0c8462f 7753 if (len > strlen (name))
d9352518
DB
7754 continue;
7755
7756 if (strncmp (curr->name, name, len) == 0)
7757 {
7758 if (strncmp (".end", name + len, 4) == 0)
7759 {
7760 *result = curr->vma + curr->size;
7761 return TRUE;
7762 }
7763
7764 /* Insert more pseudo-section names here, if you like. */
7765 }
7766 }
a0c8462f 7767
d9352518
DB
7768 return FALSE;
7769}
7770
7771static void
a0c8462f 7772undefined_reference (const char *reftype, const char *name)
d9352518 7773{
a0c8462f
AM
7774 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7775 reftype, name);
d9352518
DB
7776}
7777
7778static bfd_boolean
a0c8462f
AM
7779eval_symbol (bfd_vma *result,
7780 const char **symp,
7781 bfd *input_bfd,
8b127cbc 7782 struct elf_final_link_info *flinfo,
a0c8462f
AM
7783 bfd_vma dot,
7784 Elf_Internal_Sym *isymbuf,
7785 size_t locsymcount,
7786 int signed_p)
d9352518 7787{
4b93929b
NC
7788 size_t len;
7789 size_t symlen;
a0c8462f
AM
7790 bfd_vma a;
7791 bfd_vma b;
4b93929b 7792 char symbuf[4096];
0f02bbd9 7793 const char *sym = *symp;
a0c8462f
AM
7794 const char *symend;
7795 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7796
7797 len = strlen (sym);
7798 symend = sym + len;
7799
4b93929b 7800 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7801 {
7802 bfd_set_error (bfd_error_invalid_operation);
7803 return FALSE;
7804 }
a0c8462f 7805
d9352518
DB
7806 switch (* sym)
7807 {
7808 case '.':
0f02bbd9
AM
7809 *result = dot;
7810 *symp = sym + 1;
d9352518
DB
7811 return TRUE;
7812
7813 case '#':
0f02bbd9
AM
7814 ++sym;
7815 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7816 return TRUE;
7817
7818 case 'S':
7819 symbol_is_section = TRUE;
a0c8462f 7820 case 's':
0f02bbd9
AM
7821 ++sym;
7822 symlen = strtol (sym, (char **) symp, 10);
7823 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7824
4b93929b 7825 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7826 {
7827 bfd_set_error (bfd_error_invalid_operation);
7828 return FALSE;
7829 }
7830
7831 memcpy (symbuf, sym, symlen);
a0c8462f 7832 symbuf[symlen] = '\0';
0f02bbd9 7833 *symp = sym + symlen;
a0c8462f
AM
7834
7835 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7836 the symbol as a section, or vice-versa. so we're pretty liberal in our
7837 interpretation here; section means "try section first", not "must be a
7838 section", and likewise with symbol. */
7839
a0c8462f 7840 if (symbol_is_section)
d9352518 7841 {
8b127cbc
AM
7842 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7843 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7844 isymbuf, locsymcount))
d9352518
DB
7845 {
7846 undefined_reference ("section", symbuf);
7847 return FALSE;
7848 }
a0c8462f
AM
7849 }
7850 else
d9352518 7851 {
8b127cbc 7852 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7853 isymbuf, locsymcount)
8b127cbc 7854 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977835c 7855 result))
d9352518
DB
7856 {
7857 undefined_reference ("symbol", symbuf);
7858 return FALSE;
7859 }
7860 }
7861
7862 return TRUE;
a0c8462f 7863
d9352518
DB
7864 /* All that remains are operators. */
7865
7866#define UNARY_OP(op) \
7867 if (strncmp (sym, #op, strlen (#op)) == 0) \
7868 { \
7869 sym += strlen (#op); \
a0c8462f
AM
7870 if (*sym == ':') \
7871 ++sym; \
0f02bbd9 7872 *symp = sym; \
8b127cbc 7873 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7874 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7875 return FALSE; \
7876 if (signed_p) \
0f02bbd9 7877 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7878 else \
7879 *result = op a; \
d9352518
DB
7880 return TRUE; \
7881 }
7882
7883#define BINARY_OP(op) \
7884 if (strncmp (sym, #op, strlen (#op)) == 0) \
7885 { \
7886 sym += strlen (#op); \
a0c8462f
AM
7887 if (*sym == ':') \
7888 ++sym; \
0f02bbd9 7889 *symp = sym; \
8b127cbc 7890 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7891 isymbuf, locsymcount, signed_p)) \
a0c8462f 7892 return FALSE; \
0f02bbd9 7893 ++*symp; \
8b127cbc 7894 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7895 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7896 return FALSE; \
7897 if (signed_p) \
0f02bbd9 7898 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7899 else \
7900 *result = a op b; \
d9352518
DB
7901 return TRUE; \
7902 }
7903
7904 default:
7905 UNARY_OP (0-);
7906 BINARY_OP (<<);
7907 BINARY_OP (>>);
7908 BINARY_OP (==);
7909 BINARY_OP (!=);
7910 BINARY_OP (<=);
7911 BINARY_OP (>=);
7912 BINARY_OP (&&);
7913 BINARY_OP (||);
7914 UNARY_OP (~);
7915 UNARY_OP (!);
7916 BINARY_OP (*);
7917 BINARY_OP (/);
7918 BINARY_OP (%);
7919 BINARY_OP (^);
7920 BINARY_OP (|);
7921 BINARY_OP (&);
7922 BINARY_OP (+);
7923 BINARY_OP (-);
7924 BINARY_OP (<);
7925 BINARY_OP (>);
7926#undef UNARY_OP
7927#undef BINARY_OP
7928 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7929 bfd_set_error (bfd_error_invalid_operation);
7930 return FALSE;
7931 }
7932}
7933
d9352518 7934static void
a0c8462f
AM
7935put_value (bfd_vma size,
7936 unsigned long chunksz,
7937 bfd *input_bfd,
7938 bfd_vma x,
7939 bfd_byte *location)
d9352518
DB
7940{
7941 location += (size - chunksz);
7942
41cd1ad1 7943 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
7944 {
7945 switch (chunksz)
7946 {
d9352518
DB
7947 case 1:
7948 bfd_put_8 (input_bfd, x, location);
41cd1ad1 7949 x >>= 8;
d9352518
DB
7950 break;
7951 case 2:
7952 bfd_put_16 (input_bfd, x, location);
41cd1ad1 7953 x >>= 16;
d9352518
DB
7954 break;
7955 case 4:
7956 bfd_put_32 (input_bfd, x, location);
65164438
NC
7957 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7958 x >>= 16;
7959 x >>= 16;
d9352518 7960 break;
d9352518 7961#ifdef BFD64
41cd1ad1 7962 case 8:
d9352518 7963 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
7964 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7965 x >>= 32;
7966 x >>= 32;
7967 break;
d9352518 7968#endif
41cd1ad1
NC
7969 default:
7970 abort ();
d9352518
DB
7971 break;
7972 }
7973 }
7974}
7975
a0c8462f
AM
7976static bfd_vma
7977get_value (bfd_vma size,
7978 unsigned long chunksz,
7979 bfd *input_bfd,
7980 bfd_byte *location)
d9352518 7981{
9b239e0e 7982 int shift;
d9352518
DB
7983 bfd_vma x = 0;
7984
9b239e0e
NC
7985 /* Sanity checks. */
7986 BFD_ASSERT (chunksz <= sizeof (x)
7987 && size >= chunksz
7988 && chunksz != 0
7989 && (size % chunksz) == 0
7990 && input_bfd != NULL
7991 && location != NULL);
7992
7993 if (chunksz == sizeof (x))
7994 {
7995 BFD_ASSERT (size == chunksz);
7996
7997 /* Make sure that we do not perform an undefined shift operation.
7998 We know that size == chunksz so there will only be one iteration
7999 of the loop below. */
8000 shift = 0;
8001 }
8002 else
8003 shift = 8 * chunksz;
8004
a0c8462f 8005 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8006 {
8007 switch (chunksz)
8008 {
d9352518 8009 case 1:
9b239e0e 8010 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8011 break;
8012 case 2:
9b239e0e 8013 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8014 break;
8015 case 4:
9b239e0e 8016 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8017 break;
d9352518 8018#ifdef BFD64
9b239e0e
NC
8019 case 8:
8020 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8021 break;
9b239e0e
NC
8022#endif
8023 default:
8024 abort ();
d9352518
DB
8025 }
8026 }
8027 return x;
8028}
8029
a0c8462f
AM
8030static void
8031decode_complex_addend (unsigned long *start, /* in bits */
8032 unsigned long *oplen, /* in bits */
8033 unsigned long *len, /* in bits */
8034 unsigned long *wordsz, /* in bytes */
8035 unsigned long *chunksz, /* in bytes */
8036 unsigned long *lsb0_p,
8037 unsigned long *signed_p,
8038 unsigned long *trunc_p,
8039 unsigned long encoded)
d9352518
DB
8040{
8041 * start = encoded & 0x3F;
8042 * len = (encoded >> 6) & 0x3F;
8043 * oplen = (encoded >> 12) & 0x3F;
8044 * wordsz = (encoded >> 18) & 0xF;
8045 * chunksz = (encoded >> 22) & 0xF;
8046 * lsb0_p = (encoded >> 27) & 1;
8047 * signed_p = (encoded >> 28) & 1;
8048 * trunc_p = (encoded >> 29) & 1;
8049}
8050
cdfeee4f 8051bfd_reloc_status_type
0f02bbd9 8052bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8053 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8054 bfd_byte *contents,
8055 Elf_Internal_Rela *rel,
8056 bfd_vma relocation)
d9352518 8057{
0f02bbd9
AM
8058 bfd_vma shift, x, mask;
8059 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8060 bfd_reloc_status_type r;
d9352518
DB
8061
8062 /* Perform this reloc, since it is complex.
8063 (this is not to say that it necessarily refers to a complex
8064 symbol; merely that it is a self-describing CGEN based reloc.
8065 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8066 word size, etc) encoded within it.). */
d9352518 8067
a0c8462f
AM
8068 decode_complex_addend (&start, &oplen, &len, &wordsz,
8069 &chunksz, &lsb0_p, &signed_p,
8070 &trunc_p, rel->r_addend);
d9352518
DB
8071
8072 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8073
8074 if (lsb0_p)
8075 shift = (start + 1) - len;
8076 else
8077 shift = (8 * wordsz) - (start + len);
8078
5dabe785 8079 /* FIXME: octets_per_byte. */
a0c8462f 8080 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
8081
8082#ifdef DEBUG
8083 printf ("Doing complex reloc: "
8084 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8085 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8086 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8087 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8088 oplen, (unsigned long) x, (unsigned long) mask,
8089 (unsigned long) relocation);
d9352518
DB
8090#endif
8091
cdfeee4f 8092 r = bfd_reloc_ok;
d9352518 8093 if (! trunc_p)
cdfeee4f
AM
8094 /* Now do an overflow check. */
8095 r = bfd_check_overflow ((signed_p
8096 ? complain_overflow_signed
8097 : complain_overflow_unsigned),
8098 len, 0, (8 * wordsz),
8099 relocation);
a0c8462f 8100
d9352518
DB
8101 /* Do the deed. */
8102 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8103
8104#ifdef DEBUG
8105 printf (" relocation: %8.8lx\n"
8106 " shifted mask: %8.8lx\n"
8107 " shifted/masked reloc: %8.8lx\n"
8108 " result: %8.8lx\n",
9ccb8af9
AM
8109 (unsigned long) relocation, (unsigned long) (mask << shift),
8110 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8111#endif
5dabe785 8112 /* FIXME: octets_per_byte. */
d9352518 8113 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 8114 return r;
d9352518
DB
8115}
8116
0e287786
AM
8117/* Functions to read r_offset from external (target order) reloc
8118 entry. Faster than bfd_getl32 et al, because we let the compiler
8119 know the value is aligned. */
53df40a4 8120
0e287786
AM
8121static bfd_vma
8122ext32l_r_offset (const void *p)
53df40a4
AM
8123{
8124 union aligned32
8125 {
8126 uint32_t v;
8127 unsigned char c[4];
8128 };
8129 const union aligned32 *a
0e287786 8130 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8131
8132 uint32_t aval = ( (uint32_t) a->c[0]
8133 | (uint32_t) a->c[1] << 8
8134 | (uint32_t) a->c[2] << 16
8135 | (uint32_t) a->c[3] << 24);
0e287786 8136 return aval;
53df40a4
AM
8137}
8138
0e287786
AM
8139static bfd_vma
8140ext32b_r_offset (const void *p)
53df40a4
AM
8141{
8142 union aligned32
8143 {
8144 uint32_t v;
8145 unsigned char c[4];
8146 };
8147 const union aligned32 *a
0e287786 8148 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8149
8150 uint32_t aval = ( (uint32_t) a->c[0] << 24
8151 | (uint32_t) a->c[1] << 16
8152 | (uint32_t) a->c[2] << 8
8153 | (uint32_t) a->c[3]);
0e287786 8154 return aval;
53df40a4
AM
8155}
8156
8157#ifdef BFD_HOST_64_BIT
0e287786
AM
8158static bfd_vma
8159ext64l_r_offset (const void *p)
53df40a4
AM
8160{
8161 union aligned64
8162 {
8163 uint64_t v;
8164 unsigned char c[8];
8165 };
8166 const union aligned64 *a
0e287786 8167 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8168
8169 uint64_t aval = ( (uint64_t) a->c[0]
8170 | (uint64_t) a->c[1] << 8
8171 | (uint64_t) a->c[2] << 16
8172 | (uint64_t) a->c[3] << 24
8173 | (uint64_t) a->c[4] << 32
8174 | (uint64_t) a->c[5] << 40
8175 | (uint64_t) a->c[6] << 48
8176 | (uint64_t) a->c[7] << 56);
0e287786 8177 return aval;
53df40a4
AM
8178}
8179
0e287786
AM
8180static bfd_vma
8181ext64b_r_offset (const void *p)
53df40a4
AM
8182{
8183 union aligned64
8184 {
8185 uint64_t v;
8186 unsigned char c[8];
8187 };
8188 const union aligned64 *a
0e287786 8189 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8190
8191 uint64_t aval = ( (uint64_t) a->c[0] << 56
8192 | (uint64_t) a->c[1] << 48
8193 | (uint64_t) a->c[2] << 40
8194 | (uint64_t) a->c[3] << 32
8195 | (uint64_t) a->c[4] << 24
8196 | (uint64_t) a->c[5] << 16
8197 | (uint64_t) a->c[6] << 8
8198 | (uint64_t) a->c[7]);
0e287786 8199 return aval;
53df40a4
AM
8200}
8201#endif
8202
c152c796
AM
8203/* When performing a relocatable link, the input relocations are
8204 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8205 referenced must be updated. Update all the relocations found in
8206 RELDATA. */
c152c796 8207
bca6d0e3 8208static bfd_boolean
c152c796 8209elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8210 struct bfd_elf_section_reloc_data *reldata,
8211 bfd_boolean sort)
c152c796
AM
8212{
8213 unsigned int i;
8214 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8215 bfd_byte *erela;
8216 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8217 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8218 bfd_vma r_type_mask;
8219 int r_sym_shift;
d4730f92
BS
8220 unsigned int count = reldata->count;
8221 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8222
d4730f92 8223 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8224 {
8225 swap_in = bed->s->swap_reloc_in;
8226 swap_out = bed->s->swap_reloc_out;
8227 }
d4730f92 8228 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8229 {
8230 swap_in = bed->s->swap_reloca_in;
8231 swap_out = bed->s->swap_reloca_out;
8232 }
8233 else
8234 abort ();
8235
8236 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8237 abort ();
8238
8239 if (bed->s->arch_size == 32)
8240 {
8241 r_type_mask = 0xff;
8242 r_sym_shift = 8;
8243 }
8244 else
8245 {
8246 r_type_mask = 0xffffffff;
8247 r_sym_shift = 32;
8248 }
8249
d4730f92
BS
8250 erela = reldata->hdr->contents;
8251 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8252 {
8253 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8254 unsigned int j;
8255
8256 if (*rel_hash == NULL)
8257 continue;
8258
8259 BFD_ASSERT ((*rel_hash)->indx >= 0);
8260
8261 (*swap_in) (abfd, erela, irela);
8262 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8263 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8264 | (irela[j].r_info & r_type_mask));
8265 (*swap_out) (abfd, irela, erela);
8266 }
53df40a4 8267
0e287786 8268 if (sort && count != 0)
53df40a4 8269 {
0e287786
AM
8270 bfd_vma (*ext_r_off) (const void *);
8271 bfd_vma r_off;
8272 size_t elt_size;
8273 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8274 bfd_byte *buf = NULL;
28dbcedc
AM
8275
8276 if (bed->s->arch_size == 32)
8277 {
8278 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8279 ext_r_off = ext32l_r_offset;
28dbcedc 8280 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8281 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8282 else
8283 abort ();
8284 }
53df40a4 8285 else
28dbcedc 8286 {
53df40a4 8287#ifdef BFD_HOST_64_BIT
28dbcedc 8288 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8289 ext_r_off = ext64l_r_offset;
28dbcedc 8290 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8291 ext_r_off = ext64b_r_offset;
28dbcedc 8292 else
53df40a4 8293#endif
28dbcedc
AM
8294 abort ();
8295 }
0e287786 8296
bca6d0e3
AM
8297 /* Must use a stable sort here. A modified insertion sort,
8298 since the relocs are mostly sorted already. */
0e287786
AM
8299 elt_size = reldata->hdr->sh_entsize;
8300 base = reldata->hdr->contents;
8301 end = base + count * elt_size;
bca6d0e3 8302 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8303 abort ();
8304
8305 /* Ensure the first element is lowest. This acts as a sentinel,
8306 speeding the main loop below. */
8307 r_off = (*ext_r_off) (base);
8308 for (p = loc = base; (p += elt_size) < end; )
8309 {
8310 bfd_vma r_off2 = (*ext_r_off) (p);
8311 if (r_off > r_off2)
8312 {
8313 r_off = r_off2;
8314 loc = p;
8315 }
8316 }
8317 if (loc != base)
8318 {
8319 /* Don't just swap *base and *loc as that changes the order
8320 of the original base[0] and base[1] if they happen to
8321 have the same r_offset. */
bca6d0e3
AM
8322 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8323 memcpy (onebuf, loc, elt_size);
0e287786 8324 memmove (base + elt_size, base, loc - base);
bca6d0e3 8325 memcpy (base, onebuf, elt_size);
0e287786
AM
8326 }
8327
b29b8669 8328 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8329 {
8330 /* base to p is sorted, *p is next to insert. */
8331 r_off = (*ext_r_off) (p);
8332 /* Search the sorted region for location to insert. */
8333 loc = p - elt_size;
8334 while (r_off < (*ext_r_off) (loc))
8335 loc -= elt_size;
8336 loc += elt_size;
8337 if (loc != p)
8338 {
bca6d0e3
AM
8339 /* Chances are there is a run of relocs to insert here,
8340 from one of more input files. Files are not always
8341 linked in order due to the way elf_link_input_bfd is
8342 called. See pr17666. */
8343 size_t sortlen = p - loc;
8344 bfd_vma r_off2 = (*ext_r_off) (loc);
8345 size_t runlen = elt_size;
8346 size_t buf_size = 96 * 1024;
8347 while (p + runlen < end
8348 && (sortlen <= buf_size
8349 || runlen + elt_size <= buf_size)
8350 && r_off2 > (*ext_r_off) (p + runlen))
8351 runlen += elt_size;
8352 if (buf == NULL)
8353 {
8354 buf = bfd_malloc (buf_size);
8355 if (buf == NULL)
8356 return FALSE;
8357 }
8358 if (runlen < sortlen)
8359 {
8360 memcpy (buf, p, runlen);
8361 memmove (loc + runlen, loc, sortlen);
8362 memcpy (loc, buf, runlen);
8363 }
8364 else
8365 {
8366 memcpy (buf, loc, sortlen);
8367 memmove (loc, p, runlen);
8368 memcpy (loc + runlen, buf, sortlen);
8369 }
b29b8669 8370 p += runlen - elt_size;
0e287786
AM
8371 }
8372 }
8373 /* Hashes are no longer valid. */
28dbcedc
AM
8374 free (reldata->hashes);
8375 reldata->hashes = NULL;
bca6d0e3 8376 free (buf);
53df40a4 8377 }
bca6d0e3 8378 return TRUE;
c152c796
AM
8379}
8380
8381struct elf_link_sort_rela
8382{
8383 union {
8384 bfd_vma offset;
8385 bfd_vma sym_mask;
8386 } u;
8387 enum elf_reloc_type_class type;
8388 /* We use this as an array of size int_rels_per_ext_rel. */
8389 Elf_Internal_Rela rela[1];
8390};
8391
8392static int
8393elf_link_sort_cmp1 (const void *A, const void *B)
8394{
a50b1753
NC
8395 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8396 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8397 int relativea, relativeb;
8398
8399 relativea = a->type == reloc_class_relative;
8400 relativeb = b->type == reloc_class_relative;
8401
8402 if (relativea < relativeb)
8403 return 1;
8404 if (relativea > relativeb)
8405 return -1;
8406 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8407 return -1;
8408 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8409 return 1;
8410 if (a->rela->r_offset < b->rela->r_offset)
8411 return -1;
8412 if (a->rela->r_offset > b->rela->r_offset)
8413 return 1;
8414 return 0;
8415}
8416
8417static int
8418elf_link_sort_cmp2 (const void *A, const void *B)
8419{
a50b1753
NC
8420 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8421 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8422
7e612e98 8423 if (a->type < b->type)
c152c796 8424 return -1;
7e612e98 8425 if (a->type > b->type)
c152c796 8426 return 1;
7e612e98 8427 if (a->u.offset < b->u.offset)
c152c796 8428 return -1;
7e612e98 8429 if (a->u.offset > b->u.offset)
c152c796
AM
8430 return 1;
8431 if (a->rela->r_offset < b->rela->r_offset)
8432 return -1;
8433 if (a->rela->r_offset > b->rela->r_offset)
8434 return 1;
8435 return 0;
8436}
8437
8438static size_t
8439elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8440{
3410fea8 8441 asection *dynamic_relocs;
fc66a176
L
8442 asection *rela_dyn;
8443 asection *rel_dyn;
c152c796
AM
8444 bfd_size_type count, size;
8445 size_t i, ret, sort_elt, ext_size;
8446 bfd_byte *sort, *s_non_relative, *p;
8447 struct elf_link_sort_rela *sq;
8448 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8449 int i2e = bed->s->int_rels_per_ext_rel;
8450 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8451 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8452 struct bfd_link_order *lo;
8453 bfd_vma r_sym_mask;
3410fea8 8454 bfd_boolean use_rela;
c152c796 8455
3410fea8
NC
8456 /* Find a dynamic reloc section. */
8457 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8458 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8459 if (rela_dyn != NULL && rela_dyn->size > 0
8460 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8461 {
3410fea8
NC
8462 bfd_boolean use_rela_initialised = FALSE;
8463
8464 /* This is just here to stop gcc from complaining.
8465 It's initialization checking code is not perfect. */
8466 use_rela = TRUE;
8467
8468 /* Both sections are present. Examine the sizes
8469 of the indirect sections to help us choose. */
8470 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8471 if (lo->type == bfd_indirect_link_order)
8472 {
8473 asection *o = lo->u.indirect.section;
8474
8475 if ((o->size % bed->s->sizeof_rela) == 0)
8476 {
8477 if ((o->size % bed->s->sizeof_rel) == 0)
8478 /* Section size is divisible by both rel and rela sizes.
8479 It is of no help to us. */
8480 ;
8481 else
8482 {
8483 /* Section size is only divisible by rela. */
8484 if (use_rela_initialised && (use_rela == FALSE))
8485 {
8486 _bfd_error_handler
8487 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8488 bfd_set_error (bfd_error_invalid_operation);
8489 return 0;
8490 }
8491 else
8492 {
8493 use_rela = TRUE;
8494 use_rela_initialised = TRUE;
8495 }
8496 }
8497 }
8498 else if ((o->size % bed->s->sizeof_rel) == 0)
8499 {
8500 /* Section size is only divisible by rel. */
8501 if (use_rela_initialised && (use_rela == TRUE))
8502 {
8503 _bfd_error_handler
8504 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8505 bfd_set_error (bfd_error_invalid_operation);
8506 return 0;
8507 }
8508 else
8509 {
8510 use_rela = FALSE;
8511 use_rela_initialised = TRUE;
8512 }
8513 }
8514 else
8515 {
8516 /* The section size is not divisible by either - something is wrong. */
8517 _bfd_error_handler
8518 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8519 bfd_set_error (bfd_error_invalid_operation);
8520 return 0;
8521 }
8522 }
8523
8524 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8525 if (lo->type == bfd_indirect_link_order)
8526 {
8527 asection *o = lo->u.indirect.section;
8528
8529 if ((o->size % bed->s->sizeof_rela) == 0)
8530 {
8531 if ((o->size % bed->s->sizeof_rel) == 0)
8532 /* Section size is divisible by both rel and rela sizes.
8533 It is of no help to us. */
8534 ;
8535 else
8536 {
8537 /* Section size is only divisible by rela. */
8538 if (use_rela_initialised && (use_rela == FALSE))
8539 {
8540 _bfd_error_handler
8541 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8542 bfd_set_error (bfd_error_invalid_operation);
8543 return 0;
8544 }
8545 else
8546 {
8547 use_rela = TRUE;
8548 use_rela_initialised = TRUE;
8549 }
8550 }
8551 }
8552 else if ((o->size % bed->s->sizeof_rel) == 0)
8553 {
8554 /* Section size is only divisible by rel. */
8555 if (use_rela_initialised && (use_rela == TRUE))
8556 {
8557 _bfd_error_handler
8558 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8559 bfd_set_error (bfd_error_invalid_operation);
8560 return 0;
8561 }
8562 else
8563 {
8564 use_rela = FALSE;
8565 use_rela_initialised = TRUE;
8566 }
8567 }
8568 else
8569 {
8570 /* The section size is not divisible by either - something is wrong. */
8571 _bfd_error_handler
8572 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8573 bfd_set_error (bfd_error_invalid_operation);
8574 return 0;
8575 }
8576 }
8577
8578 if (! use_rela_initialised)
8579 /* Make a guess. */
8580 use_rela = TRUE;
c152c796 8581 }
fc66a176
L
8582 else if (rela_dyn != NULL && rela_dyn->size > 0)
8583 use_rela = TRUE;
8584 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8585 use_rela = FALSE;
c152c796 8586 else
fc66a176 8587 return 0;
3410fea8
NC
8588
8589 if (use_rela)
c152c796 8590 {
3410fea8 8591 dynamic_relocs = rela_dyn;
c152c796
AM
8592 ext_size = bed->s->sizeof_rela;
8593 swap_in = bed->s->swap_reloca_in;
8594 swap_out = bed->s->swap_reloca_out;
8595 }
3410fea8
NC
8596 else
8597 {
8598 dynamic_relocs = rel_dyn;
8599 ext_size = bed->s->sizeof_rel;
8600 swap_in = bed->s->swap_reloc_in;
8601 swap_out = bed->s->swap_reloc_out;
8602 }
c152c796
AM
8603
8604 size = 0;
3410fea8 8605 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8606 if (lo->type == bfd_indirect_link_order)
3410fea8 8607 size += lo->u.indirect.section->size;
c152c796 8608
3410fea8 8609 if (size != dynamic_relocs->size)
c152c796
AM
8610 return 0;
8611
8612 sort_elt = (sizeof (struct elf_link_sort_rela)
8613 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8614
8615 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8616 if (count == 0)
8617 return 0;
a50b1753 8618 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8619
c152c796
AM
8620 if (sort == NULL)
8621 {
8622 (*info->callbacks->warning)
8623 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8624 return 0;
8625 }
8626
8627 if (bed->s->arch_size == 32)
8628 r_sym_mask = ~(bfd_vma) 0xff;
8629 else
8630 r_sym_mask = ~(bfd_vma) 0xffffffff;
8631
3410fea8 8632 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8633 if (lo->type == bfd_indirect_link_order)
8634 {
8635 bfd_byte *erel, *erelend;
8636 asection *o = lo->u.indirect.section;
8637
1da212d6
AM
8638 if (o->contents == NULL && o->size != 0)
8639 {
8640 /* This is a reloc section that is being handled as a normal
8641 section. See bfd_section_from_shdr. We can't combine
8642 relocs in this case. */
8643 free (sort);
8644 return 0;
8645 }
c152c796 8646 erel = o->contents;
eea6121a 8647 erelend = o->contents + o->size;
5dabe785 8648 /* FIXME: octets_per_byte. */
c152c796 8649 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8650
c152c796
AM
8651 while (erel < erelend)
8652 {
8653 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8654
c152c796 8655 (*swap_in) (abfd, erel, s->rela);
7e612e98 8656 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8657 s->u.sym_mask = r_sym_mask;
8658 p += sort_elt;
8659 erel += ext_size;
8660 }
8661 }
8662
8663 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8664
8665 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8666 {
8667 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8668 if (s->type != reloc_class_relative)
8669 break;
8670 }
8671 ret = i;
8672 s_non_relative = p;
8673
8674 sq = (struct elf_link_sort_rela *) s_non_relative;
8675 for (; i < count; i++, p += sort_elt)
8676 {
8677 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8678 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8679 sq = sp;
8680 sp->u.offset = sq->rela->r_offset;
8681 }
8682
8683 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8684
3410fea8 8685 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8686 if (lo->type == bfd_indirect_link_order)
8687 {
8688 bfd_byte *erel, *erelend;
8689 asection *o = lo->u.indirect.section;
8690
8691 erel = o->contents;
eea6121a 8692 erelend = o->contents + o->size;
5dabe785 8693 /* FIXME: octets_per_byte. */
c152c796
AM
8694 p = sort + o->output_offset / ext_size * sort_elt;
8695 while (erel < erelend)
8696 {
8697 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8698 (*swap_out) (abfd, s->rela, erel);
8699 p += sort_elt;
8700 erel += ext_size;
8701 }
8702 }
8703
8704 free (sort);
3410fea8 8705 *psec = dynamic_relocs;
c152c796
AM
8706 return ret;
8707}
8708
ef10c3ac 8709/* Add a symbol to the output symbol string table. */
c152c796 8710
6e0b88f1 8711static int
ef10c3ac
L
8712elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8713 const char *name,
8714 Elf_Internal_Sym *elfsym,
8715 asection *input_sec,
8716 struct elf_link_hash_entry *h)
c152c796 8717{
6e0b88f1 8718 int (*output_symbol_hook)
c152c796
AM
8719 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8720 struct elf_link_hash_entry *);
ef10c3ac 8721 struct elf_link_hash_table *hash_table;
c152c796 8722 const struct elf_backend_data *bed;
ef10c3ac 8723 bfd_size_type strtabsize;
c152c796 8724
8539e4e8
AM
8725 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8726
8b127cbc 8727 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8728 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8729 if (output_symbol_hook != NULL)
8730 {
8b127cbc 8731 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8732 if (ret != 1)
8733 return ret;
c152c796
AM
8734 }
8735
ef10c3ac
L
8736 if (name == NULL
8737 || *name == '\0'
8738 || (input_sec->flags & SEC_EXCLUDE))
8739 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8740 else
8741 {
ef10c3ac
L
8742 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8743 to get the final offset for st_name. */
8744 elfsym->st_name
8745 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8746 name, FALSE);
c152c796 8747 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8748 return 0;
c152c796
AM
8749 }
8750
ef10c3ac
L
8751 hash_table = elf_hash_table (flinfo->info);
8752 strtabsize = hash_table->strtabsize;
8753 if (strtabsize <= hash_table->strtabcount)
c152c796 8754 {
ef10c3ac
L
8755 strtabsize += strtabsize;
8756 hash_table->strtabsize = strtabsize;
8757 strtabsize *= sizeof (*hash_table->strtab);
8758 hash_table->strtab
8759 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8760 strtabsize);
8761 if (hash_table->strtab == NULL)
6e0b88f1 8762 return 0;
c152c796 8763 }
ef10c3ac
L
8764 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8765 hash_table->strtab[hash_table->strtabcount].dest_index
8766 = hash_table->strtabcount;
8767 hash_table->strtab[hash_table->strtabcount].destshndx_index
8768 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8769
8770 bfd_get_symcount (flinfo->output_bfd) += 1;
8771 hash_table->strtabcount += 1;
8772
8773 return 1;
8774}
8775
8776/* Swap symbols out to the symbol table and flush the output symbols to
8777 the file. */
8778
8779static bfd_boolean
8780elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8781{
8782 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8783 bfd_size_type amt, i;
8784 const struct elf_backend_data *bed;
8785 bfd_byte *symbuf;
8786 Elf_Internal_Shdr *hdr;
8787 file_ptr pos;
8788 bfd_boolean ret;
8789
8790 if (!hash_table->strtabcount)
8791 return TRUE;
8792
8793 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8794
8795 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8796
ef10c3ac
L
8797 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8798 symbuf = (bfd_byte *) bfd_malloc (amt);
8799 if (symbuf == NULL)
8800 return FALSE;
1b786873 8801
ef10c3ac 8802 if (flinfo->symshndxbuf)
c152c796 8803 {
ef10c3ac
L
8804 amt = (sizeof (Elf_External_Sym_Shndx)
8805 * (bfd_get_symcount (flinfo->output_bfd)));
8806 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8807 if (flinfo->symshndxbuf == NULL)
c152c796 8808 {
ef10c3ac
L
8809 free (symbuf);
8810 return FALSE;
c152c796 8811 }
c152c796
AM
8812 }
8813
ef10c3ac
L
8814 for (i = 0; i < hash_table->strtabcount; i++)
8815 {
8816 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8817 if (elfsym->sym.st_name == (unsigned long) -1)
8818 elfsym->sym.st_name = 0;
8819 else
8820 elfsym->sym.st_name
8821 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8822 elfsym->sym.st_name);
8823 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8824 ((bfd_byte *) symbuf
8825 + (elfsym->dest_index
8826 * bed->s->sizeof_sym)),
8827 (flinfo->symshndxbuf
8828 + elfsym->destshndx_index));
8829 }
8830
8831 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8832 pos = hdr->sh_offset + hdr->sh_size;
8833 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8834 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8835 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8836 {
8837 hdr->sh_size += amt;
8838 ret = TRUE;
8839 }
8840 else
8841 ret = FALSE;
c152c796 8842
ef10c3ac
L
8843 free (symbuf);
8844
8845 free (hash_table->strtab);
8846 hash_table->strtab = NULL;
8847
8848 return ret;
c152c796
AM
8849}
8850
c0d5a53d
L
8851/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8852
8853static bfd_boolean
8854check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8855{
4fbb74a6
AM
8856 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8857 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8858 {
8859 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8860 beyond 64k. */
c0d5a53d
L
8861 (*_bfd_error_handler)
8862 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8863 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8864 bfd_set_error (bfd_error_nonrepresentable_section);
8865 return FALSE;
8866 }
8867 return TRUE;
8868}
8869
c152c796
AM
8870/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8871 allowing an unsatisfied unversioned symbol in the DSO to match a
8872 versioned symbol that would normally require an explicit version.
8873 We also handle the case that a DSO references a hidden symbol
8874 which may be satisfied by a versioned symbol in another DSO. */
8875
8876static bfd_boolean
8877elf_link_check_versioned_symbol (struct bfd_link_info *info,
8878 const struct elf_backend_data *bed,
8879 struct elf_link_hash_entry *h)
8880{
8881 bfd *abfd;
8882 struct elf_link_loaded_list *loaded;
8883
8884 if (!is_elf_hash_table (info->hash))
8885 return FALSE;
8886
90c984fc
L
8887 /* Check indirect symbol. */
8888 while (h->root.type == bfd_link_hash_indirect)
8889 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8890
c152c796
AM
8891 switch (h->root.type)
8892 {
8893 default:
8894 abfd = NULL;
8895 break;
8896
8897 case bfd_link_hash_undefined:
8898 case bfd_link_hash_undefweak:
8899 abfd = h->root.u.undef.abfd;
8900 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8901 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8902 return FALSE;
8903 break;
8904
8905 case bfd_link_hash_defined:
8906 case bfd_link_hash_defweak:
8907 abfd = h->root.u.def.section->owner;
8908 break;
8909
8910 case bfd_link_hash_common:
8911 abfd = h->root.u.c.p->section->owner;
8912 break;
8913 }
8914 BFD_ASSERT (abfd != NULL);
8915
8916 for (loaded = elf_hash_table (info)->loaded;
8917 loaded != NULL;
8918 loaded = loaded->next)
8919 {
8920 bfd *input;
8921 Elf_Internal_Shdr *hdr;
8922 bfd_size_type symcount;
8923 bfd_size_type extsymcount;
8924 bfd_size_type extsymoff;
8925 Elf_Internal_Shdr *versymhdr;
8926 Elf_Internal_Sym *isym;
8927 Elf_Internal_Sym *isymend;
8928 Elf_Internal_Sym *isymbuf;
8929 Elf_External_Versym *ever;
8930 Elf_External_Versym *extversym;
8931
8932 input = loaded->abfd;
8933
8934 /* We check each DSO for a possible hidden versioned definition. */
8935 if (input == abfd
8936 || (input->flags & DYNAMIC) == 0
8937 || elf_dynversym (input) == 0)
8938 continue;
8939
8940 hdr = &elf_tdata (input)->dynsymtab_hdr;
8941
8942 symcount = hdr->sh_size / bed->s->sizeof_sym;
8943 if (elf_bad_symtab (input))
8944 {
8945 extsymcount = symcount;
8946 extsymoff = 0;
8947 }
8948 else
8949 {
8950 extsymcount = symcount - hdr->sh_info;
8951 extsymoff = hdr->sh_info;
8952 }
8953
8954 if (extsymcount == 0)
8955 continue;
8956
8957 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8958 NULL, NULL, NULL);
8959 if (isymbuf == NULL)
8960 return FALSE;
8961
8962 /* Read in any version definitions. */
8963 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8964 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8965 if (extversym == NULL)
8966 goto error_ret;
8967
8968 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8969 || (bfd_bread (extversym, versymhdr->sh_size, input)
8970 != versymhdr->sh_size))
8971 {
8972 free (extversym);
8973 error_ret:
8974 free (isymbuf);
8975 return FALSE;
8976 }
8977
8978 ever = extversym + extsymoff;
8979 isymend = isymbuf + extsymcount;
8980 for (isym = isymbuf; isym < isymend; isym++, ever++)
8981 {
8982 const char *name;
8983 Elf_Internal_Versym iver;
8984 unsigned short version_index;
8985
8986 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8987 || isym->st_shndx == SHN_UNDEF)
8988 continue;
8989
8990 name = bfd_elf_string_from_elf_section (input,
8991 hdr->sh_link,
8992 isym->st_name);
8993 if (strcmp (name, h->root.root.string) != 0)
8994 continue;
8995
8996 _bfd_elf_swap_versym_in (input, ever, &iver);
8997
d023c380
L
8998 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8999 && !(h->def_regular
9000 && h->forced_local))
c152c796
AM
9001 {
9002 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9003 have provided a definition for the undefined sym unless
9004 it is defined in a non-shared object and forced local.
9005 */
c152c796
AM
9006 abort ();
9007 }
9008
9009 version_index = iver.vs_vers & VERSYM_VERSION;
9010 if (version_index == 1 || version_index == 2)
9011 {
9012 /* This is the base or first version. We can use it. */
9013 free (extversym);
9014 free (isymbuf);
9015 return TRUE;
9016 }
9017 }
9018
9019 free (extversym);
9020 free (isymbuf);
9021 }
9022
9023 return FALSE;
9024}
9025
9026/* Add an external symbol to the symbol table. This is called from
9027 the hash table traversal routine. When generating a shared object,
9028 we go through the symbol table twice. The first time we output
9029 anything that might have been forced to local scope in a version
9030 script. The second time we output the symbols that are still
9031 global symbols. */
9032
9033static bfd_boolean
7686d77d 9034elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9035{
7686d77d 9036 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9037 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9038 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9039 bfd_boolean strip;
9040 Elf_Internal_Sym sym;
9041 asection *input_sec;
9042 const struct elf_backend_data *bed;
6e0b88f1
AM
9043 long indx;
9044 int ret;
6e33951e
L
9045 /* A symbol is bound locally if it is forced local or it is locally
9046 defined, hidden versioned, not referenced by shared library and
9047 not exported when linking executable. */
9048 bfd_boolean local_bind = (h->forced_local
0e1862bb 9049 || (bfd_link_executable (flinfo->info)
6e33951e
L
9050 && !flinfo->info->export_dynamic
9051 && !h->dynamic
9052 && !h->ref_dynamic
9053 && h->def_regular
422f1182 9054 && h->versioned == versioned_hidden));
c152c796
AM
9055
9056 if (h->root.type == bfd_link_hash_warning)
9057 {
9058 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9059 if (h->root.type == bfd_link_hash_new)
9060 return TRUE;
9061 }
9062
9063 /* Decide whether to output this symbol in this pass. */
9064 if (eoinfo->localsyms)
9065 {
6e33951e 9066 if (!local_bind)
c152c796
AM
9067 return TRUE;
9068 }
9069 else
9070 {
6e33951e 9071 if (local_bind)
c152c796
AM
9072 return TRUE;
9073 }
9074
8b127cbc 9075 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9076
12ac1cf5 9077 if (h->root.type == bfd_link_hash_undefined)
c152c796 9078 {
12ac1cf5
NC
9079 /* If we have an undefined symbol reference here then it must have
9080 come from a shared library that is being linked in. (Undefined
98da7939
L
9081 references in regular files have already been handled unless
9082 they are in unreferenced sections which are removed by garbage
9083 collection). */
12ac1cf5
NC
9084 bfd_boolean ignore_undef = FALSE;
9085
9086 /* Some symbols may be special in that the fact that they're
9087 undefined can be safely ignored - let backend determine that. */
9088 if (bed->elf_backend_ignore_undef_symbol)
9089 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9090
9091 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9092 if (!ignore_undef
12ac1cf5 9093 && h->ref_dynamic
8b127cbc
AM
9094 && (!h->ref_regular || flinfo->info->gc_sections)
9095 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9096 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9097 {
9098 if (!(flinfo->info->callbacks->undefined_symbol
9099 (flinfo->info, h->root.root.string,
9100 h->ref_regular ? NULL : h->root.u.undef.abfd,
9101 NULL, 0,
9102 (flinfo->info->unresolved_syms_in_shared_libs
9103 == RM_GENERATE_ERROR))))
12ac1cf5 9104 {
17d078c5 9105 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
9106 eoinfo->failed = TRUE;
9107 return FALSE;
9108 }
c152c796
AM
9109 }
9110 }
9111
9112 /* We should also warn if a forced local symbol is referenced from
9113 shared libraries. */
0e1862bb 9114 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9115 && h->forced_local
9116 && h->ref_dynamic
371a5866 9117 && h->def_regular
f5385ebf 9118 && !h->dynamic_def
ee659f1f 9119 && h->ref_dynamic_nonweak
8b127cbc 9120 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9121 {
17d078c5
AM
9122 bfd *def_bfd;
9123 const char *msg;
90c984fc
L
9124 struct elf_link_hash_entry *hi = h;
9125
9126 /* Check indirect symbol. */
9127 while (hi->root.type == bfd_link_hash_indirect)
9128 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9129
9130 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9131 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9132 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9133 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9134 else
9135 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9136 def_bfd = flinfo->output_bfd;
90c984fc
L
9137 if (hi->root.u.def.section != bfd_abs_section_ptr)
9138 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9139 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9140 h->root.root.string);
9141 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9142 eoinfo->failed = TRUE;
9143 return FALSE;
9144 }
9145
9146 /* We don't want to output symbols that have never been mentioned by
9147 a regular file, or that we have been told to strip. However, if
9148 h->indx is set to -2, the symbol is used by a reloc and we must
9149 output it. */
d983c8c5 9150 strip = FALSE;
c152c796 9151 if (h->indx == -2)
d983c8c5 9152 ;
f5385ebf 9153 else if ((h->def_dynamic
77cfaee6
AM
9154 || h->ref_dynamic
9155 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9156 && !h->def_regular
9157 && !h->ref_regular)
c152c796 9158 strip = TRUE;
8b127cbc 9159 else if (flinfo->info->strip == strip_all)
c152c796 9160 strip = TRUE;
8b127cbc
AM
9161 else if (flinfo->info->strip == strip_some
9162 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9163 h->root.root.string, FALSE, FALSE) == NULL)
9164 strip = TRUE;
d56d55e7
AM
9165 else if ((h->root.type == bfd_link_hash_defined
9166 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9167 && ((flinfo->info->strip_discarded
dbaa2011 9168 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9169 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9170 && h->root.u.def.section->owner != NULL
d56d55e7 9171 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9172 strip = TRUE;
9e2278f5
AM
9173 else if ((h->root.type == bfd_link_hash_undefined
9174 || h->root.type == bfd_link_hash_undefweak)
9175 && h->root.u.undef.abfd != NULL
9176 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9177 strip = TRUE;
c152c796
AM
9178
9179 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9180 nothing else to do. However, if it is a forced local symbol or
9181 an ifunc symbol we need to give the backend finish_dynamic_symbol
9182 function a chance to make it dynamic. */
c152c796
AM
9183 if (strip
9184 && h->dynindx == -1
57ca8ac7 9185 && h->type != STT_GNU_IFUNC
f5385ebf 9186 && !h->forced_local)
c152c796
AM
9187 return TRUE;
9188
9189 sym.st_value = 0;
9190 sym.st_size = h->size;
9191 sym.st_other = h->other;
6e33951e 9192 if (local_bind)
935bd1e0
L
9193 {
9194 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9195 /* Turn off visibility on local symbol. */
9196 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9197 }
02acbe22
L
9198 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9199 else if (h->unique_global && h->def_regular)
3e7a7d11 9200 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
9201 else if (h->root.type == bfd_link_hash_undefweak
9202 || h->root.type == bfd_link_hash_defweak)
9203 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9204 else
9205 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 9206 sym.st_target_internal = h->target_internal;
c152c796
AM
9207
9208 switch (h->root.type)
9209 {
9210 default:
9211 case bfd_link_hash_new:
9212 case bfd_link_hash_warning:
9213 abort ();
9214 return FALSE;
9215
9216 case bfd_link_hash_undefined:
9217 case bfd_link_hash_undefweak:
9218 input_sec = bfd_und_section_ptr;
9219 sym.st_shndx = SHN_UNDEF;
9220 break;
9221
9222 case bfd_link_hash_defined:
9223 case bfd_link_hash_defweak:
9224 {
9225 input_sec = h->root.u.def.section;
9226 if (input_sec->output_section != NULL)
9227 {
9228 sym.st_shndx =
8b127cbc 9229 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9230 input_sec->output_section);
9231 if (sym.st_shndx == SHN_BAD)
9232 {
9233 (*_bfd_error_handler)
d003868e 9234 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9235 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9236 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9237 eoinfo->failed = TRUE;
9238 return FALSE;
9239 }
9240
9241 /* ELF symbols in relocatable files are section relative,
9242 but in nonrelocatable files they are virtual
9243 addresses. */
9244 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9245 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9246 {
9247 sym.st_value += input_sec->output_section->vma;
9248 if (h->type == STT_TLS)
9249 {
8b127cbc 9250 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9251 if (tls_sec != NULL)
9252 sym.st_value -= tls_sec->vma;
c152c796
AM
9253 }
9254 }
9255 }
9256 else
9257 {
9258 BFD_ASSERT (input_sec->owner == NULL
9259 || (input_sec->owner->flags & DYNAMIC) != 0);
9260 sym.st_shndx = SHN_UNDEF;
9261 input_sec = bfd_und_section_ptr;
9262 }
9263 }
9264 break;
9265
9266 case bfd_link_hash_common:
9267 input_sec = h->root.u.c.p->section;
a4d8e49b 9268 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9269 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9270 break;
9271
9272 case bfd_link_hash_indirect:
9273 /* These symbols are created by symbol versioning. They point
9274 to the decorated version of the name. For example, if the
9275 symbol foo@@GNU_1.2 is the default, which should be used when
9276 foo is used with no version, then we add an indirect symbol
9277 foo which points to foo@@GNU_1.2. We ignore these symbols,
9278 since the indirected symbol is already in the hash table. */
9279 return TRUE;
9280 }
9281
9282 /* Give the processor backend a chance to tweak the symbol value,
9283 and also to finish up anything that needs to be done for this
9284 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9285 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9286 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9287 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9288 && h->def_regular
0e1862bb 9289 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9290 || ((h->dynindx != -1
9291 || h->forced_local)
0e1862bb 9292 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9293 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9294 || h->root.type != bfd_link_hash_undefweak))
9295 || !h->forced_local)
8b127cbc 9296 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9297 {
9298 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9299 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9300 {
9301 eoinfo->failed = TRUE;
9302 return FALSE;
9303 }
9304 }
9305
9306 /* If we are marking the symbol as undefined, and there are no
9307 non-weak references to this symbol from a regular object, then
9308 mark the symbol as weak undefined; if there are non-weak
9309 references, mark the symbol as strong. We can't do this earlier,
9310 because it might not be marked as undefined until the
9311 finish_dynamic_symbol routine gets through with it. */
9312 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9313 && h->ref_regular
c152c796
AM
9314 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9315 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9316 {
9317 int bindtype;
2955ec4c
L
9318 unsigned int type = ELF_ST_TYPE (sym.st_info);
9319
9320 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9321 if (type == STT_GNU_IFUNC)
9322 type = STT_FUNC;
c152c796 9323
f5385ebf 9324 if (h->ref_regular_nonweak)
c152c796
AM
9325 bindtype = STB_GLOBAL;
9326 else
9327 bindtype = STB_WEAK;
2955ec4c 9328 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9329 }
9330
bda987c2
CD
9331 /* If this is a symbol defined in a dynamic library, don't use the
9332 symbol size from the dynamic library. Relinking an executable
9333 against a new library may introduce gratuitous changes in the
9334 executable's symbols if we keep the size. */
9335 if (sym.st_shndx == SHN_UNDEF
9336 && !h->def_regular
9337 && h->def_dynamic)
9338 sym.st_size = 0;
9339
c152c796
AM
9340 /* If a non-weak symbol with non-default visibility is not defined
9341 locally, it is a fatal error. */
0e1862bb 9342 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9343 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9344 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9345 && h->root.type == bfd_link_hash_undefined
f5385ebf 9346 && !h->def_regular)
c152c796 9347 {
17d078c5
AM
9348 const char *msg;
9349
9350 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9351 msg = _("%B: protected symbol `%s' isn't defined");
9352 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9353 msg = _("%B: internal symbol `%s' isn't defined");
9354 else
9355 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9356 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9357 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9358 eoinfo->failed = TRUE;
9359 return FALSE;
9360 }
9361
9362 /* If this symbol should be put in the .dynsym section, then put it
9363 there now. We already know the symbol index. We also fill in
9364 the entry in the .hash section. */
cae1fbbb 9365 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9366 && h->dynindx != -1
8b127cbc 9367 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9368 {
c152c796
AM
9369 bfd_byte *esym;
9370
90c984fc
L
9371 /* Since there is no version information in the dynamic string,
9372 if there is no version info in symbol version section, we will
1659f720 9373 have a run-time problem if not linking executable, referenced
6e33951e
L
9374 by shared library, not locally defined, or not bound locally.
9375 */
1659f720 9376 if (h->verinfo.verdef == NULL
6e33951e 9377 && !local_bind
0e1862bb 9378 && (!bfd_link_executable (flinfo->info)
1659f720
L
9379 || h->ref_dynamic
9380 || !h->def_regular))
90c984fc
L
9381 {
9382 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9383
9384 if (p && p [1] != '\0')
9385 {
9386 (*_bfd_error_handler)
9387 (_("%B: No symbol version section for versioned symbol `%s'"),
9388 flinfo->output_bfd, h->root.root.string);
9389 eoinfo->failed = TRUE;
9390 return FALSE;
9391 }
9392 }
9393
c152c796 9394 sym.st_name = h->dynstr_index;
cae1fbbb
L
9395 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9396 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9397 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9398 {
9399 eoinfo->failed = TRUE;
9400 return FALSE;
9401 }
8b127cbc 9402 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9403
8b127cbc 9404 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9405 {
9406 size_t hash_entry_size;
9407 bfd_byte *bucketpos;
9408 bfd_vma chain;
41198d0c
L
9409 size_t bucketcount;
9410 size_t bucket;
9411
8b127cbc 9412 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9413 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9414
9415 hash_entry_size
8b127cbc
AM
9416 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9417 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9418 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9419 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9420 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9421 bucketpos);
9422 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9423 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9424 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9425 }
c152c796 9426
8b127cbc 9427 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9428 {
9429 Elf_Internal_Versym iversym;
9430 Elf_External_Versym *eversym;
9431
f5385ebf 9432 if (!h->def_regular)
c152c796 9433 {
7b20f099
AM
9434 if (h->verinfo.verdef == NULL
9435 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9436 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9437 iversym.vs_vers = 0;
9438 else
9439 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9440 }
9441 else
9442 {
9443 if (h->verinfo.vertree == NULL)
9444 iversym.vs_vers = 1;
9445 else
9446 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9447 if (flinfo->info->create_default_symver)
3e3b46e5 9448 iversym.vs_vers++;
c152c796
AM
9449 }
9450
422f1182 9451 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9452 defined locally. */
422f1182 9453 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9454 iversym.vs_vers |= VERSYM_HIDDEN;
9455
8b127cbc 9456 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9457 eversym += h->dynindx;
8b127cbc 9458 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9459 }
9460 }
9461
d983c8c5
AM
9462 /* If the symbol is undefined, and we didn't output it to .dynsym,
9463 strip it from .symtab too. Obviously we can't do this for
9464 relocatable output or when needed for --emit-relocs. */
9465 else if (input_sec == bfd_und_section_ptr
9466 && h->indx != -2
0e1862bb 9467 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9468 return TRUE;
9469 /* Also strip others that we couldn't earlier due to dynamic symbol
9470 processing. */
9471 if (strip)
9472 return TRUE;
9473 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9474 return TRUE;
9475
2ec55de3
AM
9476 /* Output a FILE symbol so that following locals are not associated
9477 with the wrong input file. We need one for forced local symbols
9478 if we've seen more than one FILE symbol or when we have exactly
9479 one FILE symbol but global symbols are present in a file other
9480 than the one with the FILE symbol. We also need one if linker
9481 defined symbols are present. In practice these conditions are
9482 always met, so just emit the FILE symbol unconditionally. */
9483 if (eoinfo->localsyms
9484 && !eoinfo->file_sym_done
9485 && eoinfo->flinfo->filesym_count != 0)
9486 {
9487 Elf_Internal_Sym fsym;
9488
9489 memset (&fsym, 0, sizeof (fsym));
9490 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9491 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9492 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9493 bfd_und_section_ptr, NULL))
2ec55de3
AM
9494 return FALSE;
9495
9496 eoinfo->file_sym_done = TRUE;
9497 }
9498
8b127cbc 9499 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9500 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9501 input_sec, h);
6e0b88f1 9502 if (ret == 0)
c152c796
AM
9503 {
9504 eoinfo->failed = TRUE;
9505 return FALSE;
9506 }
6e0b88f1
AM
9507 else if (ret == 1)
9508 h->indx = indx;
9509 else if (h->indx == -2)
9510 abort();
c152c796
AM
9511
9512 return TRUE;
9513}
9514
cdd3575c
AM
9515/* Return TRUE if special handling is done for relocs in SEC against
9516 symbols defined in discarded sections. */
9517
c152c796
AM
9518static bfd_boolean
9519elf_section_ignore_discarded_relocs (asection *sec)
9520{
9521 const struct elf_backend_data *bed;
9522
cdd3575c
AM
9523 switch (sec->sec_info_type)
9524 {
dbaa2011
AM
9525 case SEC_INFO_TYPE_STABS:
9526 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9527 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9528 return TRUE;
9529 default:
9530 break;
9531 }
c152c796
AM
9532
9533 bed = get_elf_backend_data (sec->owner);
9534 if (bed->elf_backend_ignore_discarded_relocs != NULL
9535 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9536 return TRUE;
9537
9538 return FALSE;
9539}
9540
9e66c942
AM
9541/* Return a mask saying how ld should treat relocations in SEC against
9542 symbols defined in discarded sections. If this function returns
9543 COMPLAIN set, ld will issue a warning message. If this function
9544 returns PRETEND set, and the discarded section was link-once and the
9545 same size as the kept link-once section, ld will pretend that the
9546 symbol was actually defined in the kept section. Otherwise ld will
9547 zero the reloc (at least that is the intent, but some cooperation by
9548 the target dependent code is needed, particularly for REL targets). */
9549
8a696751
AM
9550unsigned int
9551_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9552{
9e66c942 9553 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9554 return PRETEND;
cdd3575c
AM
9555
9556 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9557 return 0;
cdd3575c
AM
9558
9559 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9560 return 0;
cdd3575c 9561
9e66c942 9562 return COMPLAIN | PRETEND;
cdd3575c
AM
9563}
9564
3d7f7666
L
9565/* Find a match between a section and a member of a section group. */
9566
9567static asection *
c0f00686
L
9568match_group_member (asection *sec, asection *group,
9569 struct bfd_link_info *info)
3d7f7666
L
9570{
9571 asection *first = elf_next_in_group (group);
9572 asection *s = first;
9573
9574 while (s != NULL)
9575 {
c0f00686 9576 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9577 return s;
9578
83180ade 9579 s = elf_next_in_group (s);
3d7f7666
L
9580 if (s == first)
9581 break;
9582 }
9583
9584 return NULL;
9585}
9586
01b3c8ab 9587/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9588 to replace it. Return the replacement if it is OK. Otherwise return
9589 NULL. */
01b3c8ab
L
9590
9591asection *
c0f00686 9592_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9593{
9594 asection *kept;
9595
9596 kept = sec->kept_section;
9597 if (kept != NULL)
9598 {
c2370991 9599 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9600 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9601 if (kept != NULL
9602 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9603 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9604 kept = NULL;
c2370991 9605 sec->kept_section = kept;
01b3c8ab
L
9606 }
9607 return kept;
9608}
9609
c152c796
AM
9610/* Link an input file into the linker output file. This function
9611 handles all the sections and relocations of the input file at once.
9612 This is so that we only have to read the local symbols once, and
9613 don't have to keep them in memory. */
9614
9615static bfd_boolean
8b127cbc 9616elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9617{
ece5ef60 9618 int (*relocate_section)
c152c796
AM
9619 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9620 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9621 bfd *output_bfd;
9622 Elf_Internal_Shdr *symtab_hdr;
9623 size_t locsymcount;
9624 size_t extsymoff;
9625 Elf_Internal_Sym *isymbuf;
9626 Elf_Internal_Sym *isym;
9627 Elf_Internal_Sym *isymend;
9628 long *pindex;
9629 asection **ppsection;
9630 asection *o;
9631 const struct elf_backend_data *bed;
c152c796 9632 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9633 bfd_size_type address_size;
9634 bfd_vma r_type_mask;
9635 int r_sym_shift;
ffbc01cc 9636 bfd_boolean have_file_sym = FALSE;
c152c796 9637
8b127cbc 9638 output_bfd = flinfo->output_bfd;
c152c796
AM
9639 bed = get_elf_backend_data (output_bfd);
9640 relocate_section = bed->elf_backend_relocate_section;
9641
9642 /* If this is a dynamic object, we don't want to do anything here:
9643 we don't want the local symbols, and we don't want the section
9644 contents. */
9645 if ((input_bfd->flags & DYNAMIC) != 0)
9646 return TRUE;
9647
c152c796
AM
9648 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9649 if (elf_bad_symtab (input_bfd))
9650 {
9651 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9652 extsymoff = 0;
9653 }
9654 else
9655 {
9656 locsymcount = symtab_hdr->sh_info;
9657 extsymoff = symtab_hdr->sh_info;
9658 }
9659
9660 /* Read the local symbols. */
9661 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9662 if (isymbuf == NULL && locsymcount != 0)
9663 {
9664 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9665 flinfo->internal_syms,
9666 flinfo->external_syms,
9667 flinfo->locsym_shndx);
c152c796
AM
9668 if (isymbuf == NULL)
9669 return FALSE;
9670 }
9671
9672 /* Find local symbol sections and adjust values of symbols in
9673 SEC_MERGE sections. Write out those local symbols we know are
9674 going into the output file. */
9675 isymend = isymbuf + locsymcount;
8b127cbc 9676 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9677 isym < isymend;
9678 isym++, pindex++, ppsection++)
9679 {
9680 asection *isec;
9681 const char *name;
9682 Elf_Internal_Sym osym;
6e0b88f1
AM
9683 long indx;
9684 int ret;
c152c796
AM
9685
9686 *pindex = -1;
9687
9688 if (elf_bad_symtab (input_bfd))
9689 {
9690 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9691 {
9692 *ppsection = NULL;
9693 continue;
9694 }
9695 }
9696
9697 if (isym->st_shndx == SHN_UNDEF)
9698 isec = bfd_und_section_ptr;
c152c796
AM
9699 else if (isym->st_shndx == SHN_ABS)
9700 isec = bfd_abs_section_ptr;
9701 else if (isym->st_shndx == SHN_COMMON)
9702 isec = bfd_com_section_ptr;
9703 else
9704 {
cb33740c
AM
9705 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9706 if (isec == NULL)
9707 {
9708 /* Don't attempt to output symbols with st_shnx in the
9709 reserved range other than SHN_ABS and SHN_COMMON. */
9710 *ppsection = NULL;
9711 continue;
9712 }
dbaa2011 9713 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9714 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9715 isym->st_value =
9716 _bfd_merged_section_offset (output_bfd, &isec,
9717 elf_section_data (isec)->sec_info,
9718 isym->st_value);
c152c796
AM
9719 }
9720
9721 *ppsection = isec;
9722
d983c8c5
AM
9723 /* Don't output the first, undefined, symbol. In fact, don't
9724 output any undefined local symbol. */
9725 if (isec == bfd_und_section_ptr)
c152c796
AM
9726 continue;
9727
9728 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9729 {
9730 /* We never output section symbols. Instead, we use the
9731 section symbol of the corresponding section in the output
9732 file. */
9733 continue;
9734 }
9735
9736 /* If we are stripping all symbols, we don't want to output this
9737 one. */
8b127cbc 9738 if (flinfo->info->strip == strip_all)
c152c796
AM
9739 continue;
9740
9741 /* If we are discarding all local symbols, we don't want to
9742 output this one. If we are generating a relocatable output
9743 file, then some of the local symbols may be required by
9744 relocs; we output them below as we discover that they are
9745 needed. */
8b127cbc 9746 if (flinfo->info->discard == discard_all)
c152c796
AM
9747 continue;
9748
9749 /* If this symbol is defined in a section which we are
f02571c5
AM
9750 discarding, we don't need to keep it. */
9751 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9752 && isym->st_shndx < SHN_LORESERVE
9753 && bfd_section_removed_from_list (output_bfd,
9754 isec->output_section))
e75a280b
L
9755 continue;
9756
c152c796
AM
9757 /* Get the name of the symbol. */
9758 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9759 isym->st_name);
9760 if (name == NULL)
9761 return FALSE;
9762
9763 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9764 if ((flinfo->info->strip == strip_some
9765 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9766 == NULL))
8b127cbc 9767 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9768 && (isec->flags & SEC_MERGE)
9769 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9770 || flinfo->info->discard == discard_l)
c152c796
AM
9771 && bfd_is_local_label_name (input_bfd, name)))
9772 continue;
9773
ffbc01cc
AM
9774 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9775 {
ce875075
AM
9776 if (input_bfd->lto_output)
9777 /* -flto puts a temp file name here. This means builds
9778 are not reproducible. Discard the symbol. */
9779 continue;
ffbc01cc
AM
9780 have_file_sym = TRUE;
9781 flinfo->filesym_count += 1;
9782 }
9783 if (!have_file_sym)
9784 {
9785 /* In the absence of debug info, bfd_find_nearest_line uses
9786 FILE symbols to determine the source file for local
9787 function symbols. Provide a FILE symbol here if input
9788 files lack such, so that their symbols won't be
9789 associated with a previous input file. It's not the
9790 source file, but the best we can do. */
9791 have_file_sym = TRUE;
9792 flinfo->filesym_count += 1;
9793 memset (&osym, 0, sizeof (osym));
9794 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9795 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9796 if (!elf_link_output_symstrtab (flinfo,
9797 (input_bfd->lto_output ? NULL
9798 : input_bfd->filename),
9799 &osym, bfd_abs_section_ptr,
9800 NULL))
ffbc01cc
AM
9801 return FALSE;
9802 }
9803
c152c796
AM
9804 osym = *isym;
9805
9806 /* Adjust the section index for the output file. */
9807 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9808 isec->output_section);
9809 if (osym.st_shndx == SHN_BAD)
9810 return FALSE;
9811
c152c796
AM
9812 /* ELF symbols in relocatable files are section relative, but
9813 in executable files they are virtual addresses. Note that
9814 this code assumes that all ELF sections have an associated
9815 BFD section with a reasonable value for output_offset; below
9816 we assume that they also have a reasonable value for
9817 output_section. Any special sections must be set up to meet
9818 these requirements. */
9819 osym.st_value += isec->output_offset;
0e1862bb 9820 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9821 {
9822 osym.st_value += isec->output_section->vma;
9823 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9824 {
9825 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9826 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9827 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9828 }
9829 }
9830
6e0b88f1 9831 indx = bfd_get_symcount (output_bfd);
ef10c3ac 9832 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 9833 if (ret == 0)
c152c796 9834 return FALSE;
6e0b88f1
AM
9835 else if (ret == 1)
9836 *pindex = indx;
c152c796
AM
9837 }
9838
310fd250
L
9839 if (bed->s->arch_size == 32)
9840 {
9841 r_type_mask = 0xff;
9842 r_sym_shift = 8;
9843 address_size = 4;
9844 }
9845 else
9846 {
9847 r_type_mask = 0xffffffff;
9848 r_sym_shift = 32;
9849 address_size = 8;
9850 }
9851
c152c796
AM
9852 /* Relocate the contents of each section. */
9853 sym_hashes = elf_sym_hashes (input_bfd);
9854 for (o = input_bfd->sections; o != NULL; o = o->next)
9855 {
9856 bfd_byte *contents;
9857
9858 if (! o->linker_mark)
9859 {
9860 /* This section was omitted from the link. */
9861 continue;
9862 }
9863
0e1862bb 9864 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
9865 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9866 {
9867 /* Deal with the group signature symbol. */
9868 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9869 unsigned long symndx = sec_data->this_hdr.sh_info;
9870 asection *osec = o->output_section;
9871
9872 if (symndx >= locsymcount
9873 || (elf_bad_symtab (input_bfd)
8b127cbc 9874 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9875 {
9876 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9877 while (h->root.type == bfd_link_hash_indirect
9878 || h->root.type == bfd_link_hash_warning)
9879 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9880 /* Arrange for symbol to be output. */
9881 h->indx = -2;
9882 elf_section_data (osec)->this_hdr.sh_info = -2;
9883 }
9884 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9885 {
9886 /* We'll use the output section target_index. */
8b127cbc 9887 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9888 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9889 }
9890 else
9891 {
8b127cbc 9892 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9893 {
9894 /* Otherwise output the local symbol now. */
9895 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9896 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 9897 const char *name;
6e0b88f1
AM
9898 long indx;
9899 int ret;
bcacc0f5
AM
9900
9901 name = bfd_elf_string_from_elf_section (input_bfd,
9902 symtab_hdr->sh_link,
9903 sym.st_name);
9904 if (name == NULL)
9905 return FALSE;
9906
9907 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9908 sec);
9909 if (sym.st_shndx == SHN_BAD)
9910 return FALSE;
9911
9912 sym.st_value += o->output_offset;
9913
6e0b88f1 9914 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
9915 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9916 NULL);
6e0b88f1 9917 if (ret == 0)
bcacc0f5 9918 return FALSE;
6e0b88f1 9919 else if (ret == 1)
8b127cbc 9920 flinfo->indices[symndx] = indx;
6e0b88f1
AM
9921 else
9922 abort ();
bcacc0f5
AM
9923 }
9924 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 9925 = flinfo->indices[symndx];
bcacc0f5
AM
9926 }
9927 }
9928
c152c796 9929 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9930 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9931 continue;
9932
9933 if ((o->flags & SEC_LINKER_CREATED) != 0)
9934 {
9935 /* Section was created by _bfd_elf_link_create_dynamic_sections
9936 or somesuch. */
9937 continue;
9938 }
9939
9940 /* Get the contents of the section. They have been cached by a
9941 relaxation routine. Note that o is a section in an input
9942 file, so the contents field will not have been set by any of
9943 the routines which work on output files. */
9944 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
9945 {
9946 contents = elf_section_data (o)->this_hdr.contents;
9947 if (bed->caches_rawsize
9948 && o->rawsize != 0
9949 && o->rawsize < o->size)
9950 {
9951 memcpy (flinfo->contents, contents, o->rawsize);
9952 contents = flinfo->contents;
9953 }
9954 }
c152c796
AM
9955 else
9956 {
8b127cbc 9957 contents = flinfo->contents;
4a114e3e 9958 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9959 return FALSE;
9960 }
9961
9962 if ((o->flags & SEC_RELOC) != 0)
9963 {
9964 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9965 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9966 int action_discarded;
ece5ef60 9967 int ret;
c152c796
AM
9968
9969 /* Get the swapped relocs. */
9970 internal_relocs
8b127cbc
AM
9971 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9972 flinfo->internal_relocs, FALSE);
c152c796
AM
9973 if (internal_relocs == NULL
9974 && o->reloc_count > 0)
9975 return FALSE;
9976
310fd250
L
9977 /* We need to reverse-copy input .ctors/.dtors sections if
9978 they are placed in .init_array/.finit_array for output. */
9979 if (o->size > address_size
9980 && ((strncmp (o->name, ".ctors", 6) == 0
9981 && strcmp (o->output_section->name,
9982 ".init_array") == 0)
9983 || (strncmp (o->name, ".dtors", 6) == 0
9984 && strcmp (o->output_section->name,
9985 ".fini_array") == 0))
9986 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9987 {
310fd250
L
9988 if (o->size != o->reloc_count * address_size)
9989 {
9990 (*_bfd_error_handler)
9991 (_("error: %B: size of section %A is not "
9992 "multiple of address size"),
9993 input_bfd, o);
9994 bfd_set_error (bfd_error_on_input);
9995 return FALSE;
9996 }
9997 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9998 }
9999
0f02bbd9 10000 action_discarded = -1;
c152c796 10001 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10002 action_discarded = (*bed->action_discarded) (o);
10003
10004 /* Run through the relocs evaluating complex reloc symbols and
10005 looking for relocs against symbols from discarded sections
10006 or section symbols from removed link-once sections.
10007 Complain about relocs against discarded sections. Zero
10008 relocs against removed link-once sections. */
10009
10010 rel = internal_relocs;
10011 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10012 for ( ; rel < relend; rel++)
c152c796 10013 {
0f02bbd9
AM
10014 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10015 unsigned int s_type;
10016 asection **ps, *sec;
10017 struct elf_link_hash_entry *h = NULL;
10018 const char *sym_name;
c152c796 10019
0f02bbd9
AM
10020 if (r_symndx == STN_UNDEF)
10021 continue;
c152c796 10022
0f02bbd9
AM
10023 if (r_symndx >= locsymcount
10024 || (elf_bad_symtab (input_bfd)
8b127cbc 10025 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10026 {
10027 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10028
0f02bbd9
AM
10029 /* Badly formatted input files can contain relocs that
10030 reference non-existant symbols. Check here so that
10031 we do not seg fault. */
10032 if (h == NULL)
c152c796 10033 {
0f02bbd9 10034 char buffer [32];
dce669a1 10035
0f02bbd9
AM
10036 sprintf_vma (buffer, rel->r_info);
10037 (*_bfd_error_handler)
10038 (_("error: %B contains a reloc (0x%s) for section %A "
10039 "that references a non-existent global symbol"),
10040 input_bfd, o, buffer);
10041 bfd_set_error (bfd_error_bad_value);
10042 return FALSE;
10043 }
3b36f7e6 10044
0f02bbd9
AM
10045 while (h->root.type == bfd_link_hash_indirect
10046 || h->root.type == bfd_link_hash_warning)
10047 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10048
0f02bbd9 10049 s_type = h->type;
cdd3575c 10050
9e2dec47 10051 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10052 mark the symbol as undefined. Note that the
10053 linker may attach linker created dynamic sections
10054 to the plugin bfd. Symbols defined in linker
10055 created sections are not plugin symbols. */
9e2dec47
L
10056 if (h->root.non_ir_ref
10057 && (h->root.type == bfd_link_hash_defined
10058 || h->root.type == bfd_link_hash_defweak)
10059 && (h->root.u.def.section->flags
10060 & SEC_LINKER_CREATED) == 0
10061 && h->root.u.def.section->owner != NULL
10062 && (h->root.u.def.section->owner->flags
10063 & BFD_PLUGIN) != 0)
10064 {
10065 h->root.type = bfd_link_hash_undefined;
10066 h->root.u.undef.abfd = h->root.u.def.section->owner;
10067 }
10068
0f02bbd9
AM
10069 ps = NULL;
10070 if (h->root.type == bfd_link_hash_defined
10071 || h->root.type == bfd_link_hash_defweak)
10072 ps = &h->root.u.def.section;
10073
10074 sym_name = h->root.root.string;
10075 }
10076 else
10077 {
10078 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10079
10080 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10081 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10082 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10083 sym, *ps);
10084 }
c152c796 10085
c301e700 10086 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10087 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10088 {
10089 bfd_vma val;
10090 bfd_vma dot = (rel->r_offset
10091 + o->output_offset + o->output_section->vma);
10092#ifdef DEBUG
10093 printf ("Encountered a complex symbol!");
10094 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10095 input_bfd->filename, o->name,
10096 (long) (rel - internal_relocs));
0f02bbd9
AM
10097 printf (" symbol: idx %8.8lx, name %s\n",
10098 r_symndx, sym_name);
10099 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10100 (unsigned long) rel->r_info,
10101 (unsigned long) rel->r_offset);
10102#endif
8b127cbc 10103 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10104 isymbuf, locsymcount, s_type == STT_SRELC))
10105 return FALSE;
10106
10107 /* Symbol evaluated OK. Update to absolute value. */
10108 set_symbol_value (input_bfd, isymbuf, locsymcount,
10109 r_symndx, val);
10110 continue;
10111 }
10112
10113 if (action_discarded != -1 && ps != NULL)
10114 {
cdd3575c
AM
10115 /* Complain if the definition comes from a
10116 discarded section. */
dbaa2011 10117 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10118 {
cf35638d 10119 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10120 if (action_discarded & COMPLAIN)
8b127cbc 10121 (*flinfo->info->callbacks->einfo)
e1fffbe6 10122 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10123 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10124 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10125
87e5235d 10126 /* Try to do the best we can to support buggy old
e0ae6d6f 10127 versions of gcc. Pretend that the symbol is
87e5235d
AM
10128 really defined in the kept linkonce section.
10129 FIXME: This is quite broken. Modifying the
10130 symbol here means we will be changing all later
e0ae6d6f 10131 uses of the symbol, not just in this section. */
0f02bbd9 10132 if (action_discarded & PRETEND)
87e5235d 10133 {
01b3c8ab
L
10134 asection *kept;
10135
c0f00686 10136 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10137 flinfo->info);
01b3c8ab 10138 if (kept != NULL)
87e5235d
AM
10139 {
10140 *ps = kept;
10141 continue;
10142 }
10143 }
c152c796
AM
10144 }
10145 }
10146 }
10147
10148 /* Relocate the section by invoking a back end routine.
10149
10150 The back end routine is responsible for adjusting the
10151 section contents as necessary, and (if using Rela relocs
10152 and generating a relocatable output file) adjusting the
10153 reloc addend as necessary.
10154
10155 The back end routine does not have to worry about setting
10156 the reloc address or the reloc symbol index.
10157
10158 The back end routine is given a pointer to the swapped in
10159 internal symbols, and can access the hash table entries
10160 for the external symbols via elf_sym_hashes (input_bfd).
10161
10162 When generating relocatable output, the back end routine
10163 must handle STB_LOCAL/STT_SECTION symbols specially. The
10164 output symbol is going to be a section symbol
10165 corresponding to the output section, which will require
10166 the addend to be adjusted. */
10167
8b127cbc 10168 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10169 input_bfd, o, contents,
10170 internal_relocs,
10171 isymbuf,
8b127cbc 10172 flinfo->sections);
ece5ef60 10173 if (!ret)
c152c796
AM
10174 return FALSE;
10175
ece5ef60 10176 if (ret == 2
0e1862bb 10177 || bfd_link_relocatable (flinfo->info)
8b127cbc 10178 || flinfo->info->emitrelocations)
c152c796
AM
10179 {
10180 Elf_Internal_Rela *irela;
d4730f92 10181 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10182 bfd_vma last_offset;
10183 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10184 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10185 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10186 unsigned int next_erel;
c152c796 10187 bfd_boolean rela_normal;
d4730f92 10188 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10189
d4730f92
BS
10190 esdi = elf_section_data (o);
10191 esdo = elf_section_data (o->output_section);
10192 rela_normal = FALSE;
c152c796
AM
10193
10194 /* Adjust the reloc addresses and symbol indices. */
10195
10196 irela = internal_relocs;
10197 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10198 rel_hash = esdo->rel.hashes + esdo->rel.count;
10199 /* We start processing the REL relocs, if any. When we reach
10200 IRELAMID in the loop, we switch to the RELA relocs. */
10201 irelamid = irela;
10202 if (esdi->rel.hdr != NULL)
10203 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10204 * bed->s->int_rels_per_ext_rel);
eac338cf 10205 rel_hash_list = rel_hash;
d4730f92 10206 rela_hash_list = NULL;
c152c796 10207 last_offset = o->output_offset;
0e1862bb 10208 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10209 last_offset += o->output_section->vma;
10210 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10211 {
10212 unsigned long r_symndx;
10213 asection *sec;
10214 Elf_Internal_Sym sym;
10215
10216 if (next_erel == bed->s->int_rels_per_ext_rel)
10217 {
10218 rel_hash++;
10219 next_erel = 0;
10220 }
10221
d4730f92
BS
10222 if (irela == irelamid)
10223 {
10224 rel_hash = esdo->rela.hashes + esdo->rela.count;
10225 rela_hash_list = rel_hash;
10226 rela_normal = bed->rela_normal;
10227 }
10228
c152c796 10229 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10230 flinfo->info, o,
c152c796
AM
10231 irela->r_offset);
10232 if (irela->r_offset >= (bfd_vma) -2)
10233 {
10234 /* This is a reloc for a deleted entry or somesuch.
10235 Turn it into an R_*_NONE reloc, at the same
10236 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10237 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10238 being ordered. */
10239 irela->r_offset = last_offset;
10240 irela->r_info = 0;
10241 irela->r_addend = 0;
10242 continue;
10243 }
10244
10245 irela->r_offset += o->output_offset;
10246
10247 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10248 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10249 irela->r_offset += o->output_section->vma;
10250
10251 last_offset = irela->r_offset;
10252
10253 r_symndx = irela->r_info >> r_sym_shift;
10254 if (r_symndx == STN_UNDEF)
10255 continue;
10256
10257 if (r_symndx >= locsymcount
10258 || (elf_bad_symtab (input_bfd)
8b127cbc 10259 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10260 {
10261 struct elf_link_hash_entry *rh;
10262 unsigned long indx;
10263
10264 /* This is a reloc against a global symbol. We
10265 have not yet output all the local symbols, so
10266 we do not know the symbol index of any global
10267 symbol. We set the rel_hash entry for this
10268 reloc to point to the global hash table entry
10269 for this symbol. The symbol index is then
ee75fd95 10270 set at the end of bfd_elf_final_link. */
c152c796
AM
10271 indx = r_symndx - extsymoff;
10272 rh = elf_sym_hashes (input_bfd)[indx];
10273 while (rh->root.type == bfd_link_hash_indirect
10274 || rh->root.type == bfd_link_hash_warning)
10275 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10276
10277 /* Setting the index to -2 tells
10278 elf_link_output_extsym that this symbol is
10279 used by a reloc. */
10280 BFD_ASSERT (rh->indx < 0);
10281 rh->indx = -2;
10282
10283 *rel_hash = rh;
10284
10285 continue;
10286 }
10287
10288 /* This is a reloc against a local symbol. */
10289
10290 *rel_hash = NULL;
10291 sym = isymbuf[r_symndx];
8b127cbc 10292 sec = flinfo->sections[r_symndx];
c152c796
AM
10293 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10294 {
10295 /* I suppose the backend ought to fill in the
10296 section of any STT_SECTION symbol against a
6a8d1586 10297 processor specific section. */
cf35638d 10298 r_symndx = STN_UNDEF;
6a8d1586
AM
10299 if (bfd_is_abs_section (sec))
10300 ;
c152c796
AM
10301 else if (sec == NULL || sec->owner == NULL)
10302 {
10303 bfd_set_error (bfd_error_bad_value);
10304 return FALSE;
10305 }
10306 else
10307 {
6a8d1586
AM
10308 asection *osec = sec->output_section;
10309
10310 /* If we have discarded a section, the output
10311 section will be the absolute section. In
ab96bf03
AM
10312 case of discarded SEC_MERGE sections, use
10313 the kept section. relocate_section should
10314 have already handled discarded linkonce
10315 sections. */
6a8d1586
AM
10316 if (bfd_is_abs_section (osec)
10317 && sec->kept_section != NULL
10318 && sec->kept_section->output_section != NULL)
10319 {
10320 osec = sec->kept_section->output_section;
10321 irela->r_addend -= osec->vma;
10322 }
10323
10324 if (!bfd_is_abs_section (osec))
10325 {
10326 r_symndx = osec->target_index;
cf35638d 10327 if (r_symndx == STN_UNDEF)
74541ad4 10328 {
051d833a
AM
10329 irela->r_addend += osec->vma;
10330 osec = _bfd_nearby_section (output_bfd, osec,
10331 osec->vma);
10332 irela->r_addend -= osec->vma;
10333 r_symndx = osec->target_index;
74541ad4 10334 }
6a8d1586 10335 }
c152c796
AM
10336 }
10337
10338 /* Adjust the addend according to where the
10339 section winds up in the output section. */
10340 if (rela_normal)
10341 irela->r_addend += sec->output_offset;
10342 }
10343 else
10344 {
8b127cbc 10345 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10346 {
10347 unsigned long shlink;
10348 const char *name;
10349 asection *osec;
6e0b88f1 10350 long indx;
c152c796 10351
8b127cbc 10352 if (flinfo->info->strip == strip_all)
c152c796
AM
10353 {
10354 /* You can't do ld -r -s. */
10355 bfd_set_error (bfd_error_invalid_operation);
10356 return FALSE;
10357 }
10358
10359 /* This symbol was skipped earlier, but
10360 since it is needed by a reloc, we
10361 must output it now. */
10362 shlink = symtab_hdr->sh_link;
10363 name = (bfd_elf_string_from_elf_section
10364 (input_bfd, shlink, sym.st_name));
10365 if (name == NULL)
10366 return FALSE;
10367
10368 osec = sec->output_section;
10369 sym.st_shndx =
10370 _bfd_elf_section_from_bfd_section (output_bfd,
10371 osec);
10372 if (sym.st_shndx == SHN_BAD)
10373 return FALSE;
10374
10375 sym.st_value += sec->output_offset;
0e1862bb 10376 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10377 {
10378 sym.st_value += osec->vma;
10379 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10380 {
10381 /* STT_TLS symbols are relative to PT_TLS
10382 segment base. */
8b127cbc 10383 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10384 ->tls_sec != NULL);
8b127cbc 10385 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10386 ->tls_sec->vma);
10387 }
10388 }
10389
6e0b88f1 10390 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10391 ret = elf_link_output_symstrtab (flinfo, name,
10392 &sym, sec,
10393 NULL);
6e0b88f1 10394 if (ret == 0)
c152c796 10395 return FALSE;
6e0b88f1 10396 else if (ret == 1)
8b127cbc 10397 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10398 else
10399 abort ();
c152c796
AM
10400 }
10401
8b127cbc 10402 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10403 }
10404
10405 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10406 | (irela->r_info & r_type_mask));
10407 }
10408
10409 /* Swap out the relocs. */
d4730f92
BS
10410 input_rel_hdr = esdi->rel.hdr;
10411 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10412 {
d4730f92
BS
10413 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10414 input_rel_hdr,
10415 internal_relocs,
10416 rel_hash_list))
10417 return FALSE;
c152c796
AM
10418 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10419 * bed->s->int_rels_per_ext_rel);
eac338cf 10420 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10421 }
10422
10423 input_rela_hdr = esdi->rela.hdr;
10424 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10425 {
eac338cf 10426 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10427 input_rela_hdr,
eac338cf 10428 internal_relocs,
d4730f92 10429 rela_hash_list))
c152c796
AM
10430 return FALSE;
10431 }
10432 }
10433 }
10434
10435 /* Write out the modified section contents. */
10436 if (bed->elf_backend_write_section
8b127cbc 10437 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10438 contents))
c152c796
AM
10439 {
10440 /* Section written out. */
10441 }
10442 else switch (o->sec_info_type)
10443 {
dbaa2011 10444 case SEC_INFO_TYPE_STABS:
c152c796
AM
10445 if (! (_bfd_write_section_stabs
10446 (output_bfd,
8b127cbc 10447 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10448 o, &elf_section_data (o)->sec_info, contents)))
10449 return FALSE;
10450 break;
dbaa2011 10451 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10452 if (! _bfd_write_merged_section (output_bfd, o,
10453 elf_section_data (o)->sec_info))
10454 return FALSE;
10455 break;
dbaa2011 10456 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10457 {
8b127cbc 10458 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10459 o, contents))
10460 return FALSE;
10461 }
10462 break;
2f0c68f2
CM
10463 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10464 {
10465 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10466 flinfo->info,
10467 o, contents))
10468 return FALSE;
10469 }
10470 break;
c152c796
AM
10471 default:
10472 {
5dabe785 10473 /* FIXME: octets_per_byte. */
310fd250
L
10474 if (! (o->flags & SEC_EXCLUDE))
10475 {
10476 file_ptr offset = (file_ptr) o->output_offset;
10477 bfd_size_type todo = o->size;
10478 if ((o->flags & SEC_ELF_REVERSE_COPY))
10479 {
10480 /* Reverse-copy input section to output. */
10481 do
10482 {
10483 todo -= address_size;
10484 if (! bfd_set_section_contents (output_bfd,
10485 o->output_section,
10486 contents + todo,
10487 offset,
10488 address_size))
10489 return FALSE;
10490 if (todo == 0)
10491 break;
10492 offset += address_size;
10493 }
10494 while (1);
10495 }
10496 else if (! bfd_set_section_contents (output_bfd,
10497 o->output_section,
10498 contents,
10499 offset, todo))
10500 return FALSE;
10501 }
c152c796
AM
10502 }
10503 break;
10504 }
10505 }
10506
10507 return TRUE;
10508}
10509
10510/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10511 requested by the linker, and does not come from any input file. This
c152c796
AM
10512 is used to build constructor and destructor tables when linking
10513 with -Ur. */
10514
10515static bfd_boolean
10516elf_reloc_link_order (bfd *output_bfd,
10517 struct bfd_link_info *info,
10518 asection *output_section,
10519 struct bfd_link_order *link_order)
10520{
10521 reloc_howto_type *howto;
10522 long indx;
10523 bfd_vma offset;
10524 bfd_vma addend;
d4730f92 10525 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10526 struct elf_link_hash_entry **rel_hash_ptr;
10527 Elf_Internal_Shdr *rel_hdr;
10528 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10529 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10530 bfd_byte *erel;
10531 unsigned int i;
d4730f92 10532 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10533
10534 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10535 if (howto == NULL)
10536 {
10537 bfd_set_error (bfd_error_bad_value);
10538 return FALSE;
10539 }
10540
10541 addend = link_order->u.reloc.p->addend;
10542
d4730f92
BS
10543 if (esdo->rel.hdr)
10544 reldata = &esdo->rel;
10545 else if (esdo->rela.hdr)
10546 reldata = &esdo->rela;
10547 else
10548 {
10549 reldata = NULL;
10550 BFD_ASSERT (0);
10551 }
10552
c152c796 10553 /* Figure out the symbol index. */
d4730f92 10554 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10555 if (link_order->type == bfd_section_reloc_link_order)
10556 {
10557 indx = link_order->u.reloc.p->u.section->target_index;
10558 BFD_ASSERT (indx != 0);
10559 *rel_hash_ptr = NULL;
10560 }
10561 else
10562 {
10563 struct elf_link_hash_entry *h;
10564
10565 /* Treat a reloc against a defined symbol as though it were
10566 actually against the section. */
10567 h = ((struct elf_link_hash_entry *)
10568 bfd_wrapped_link_hash_lookup (output_bfd, info,
10569 link_order->u.reloc.p->u.name,
10570 FALSE, FALSE, TRUE));
10571 if (h != NULL
10572 && (h->root.type == bfd_link_hash_defined
10573 || h->root.type == bfd_link_hash_defweak))
10574 {
10575 asection *section;
10576
10577 section = h->root.u.def.section;
10578 indx = section->output_section->target_index;
10579 *rel_hash_ptr = NULL;
10580 /* It seems that we ought to add the symbol value to the
10581 addend here, but in practice it has already been added
10582 because it was passed to constructor_callback. */
10583 addend += section->output_section->vma + section->output_offset;
10584 }
10585 else if (h != NULL)
10586 {
10587 /* Setting the index to -2 tells elf_link_output_extsym that
10588 this symbol is used by a reloc. */
10589 h->indx = -2;
10590 *rel_hash_ptr = h;
10591 indx = 0;
10592 }
10593 else
10594 {
10595 if (! ((*info->callbacks->unattached_reloc)
10596 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10597 return FALSE;
10598 indx = 0;
10599 }
10600 }
10601
10602 /* If this is an inplace reloc, we must write the addend into the
10603 object file. */
10604 if (howto->partial_inplace && addend != 0)
10605 {
10606 bfd_size_type size;
10607 bfd_reloc_status_type rstat;
10608 bfd_byte *buf;
10609 bfd_boolean ok;
10610 const char *sym_name;
10611
a50b1753
NC
10612 size = (bfd_size_type) bfd_get_reloc_size (howto);
10613 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10614 if (buf == NULL && size != 0)
c152c796
AM
10615 return FALSE;
10616 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10617 switch (rstat)
10618 {
10619 case bfd_reloc_ok:
10620 break;
10621
10622 default:
10623 case bfd_reloc_outofrange:
10624 abort ();
10625
10626 case bfd_reloc_overflow:
10627 if (link_order->type == bfd_section_reloc_link_order)
10628 sym_name = bfd_section_name (output_bfd,
10629 link_order->u.reloc.p->u.section);
10630 else
10631 sym_name = link_order->u.reloc.p->u.name;
10632 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10633 (info, NULL, sym_name, howto->name, addend, NULL,
10634 NULL, (bfd_vma) 0)))
c152c796
AM
10635 {
10636 free (buf);
10637 return FALSE;
10638 }
10639 break;
10640 }
10641 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10642 link_order->offset, size);
10643 free (buf);
10644 if (! ok)
10645 return FALSE;
10646 }
10647
10648 /* The address of a reloc is relative to the section in a
10649 relocatable file, and is a virtual address in an executable
10650 file. */
10651 offset = link_order->offset;
0e1862bb 10652 if (! bfd_link_relocatable (info))
c152c796
AM
10653 offset += output_section->vma;
10654
10655 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10656 {
10657 irel[i].r_offset = offset;
10658 irel[i].r_info = 0;
10659 irel[i].r_addend = 0;
10660 }
10661 if (bed->s->arch_size == 32)
10662 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10663 else
10664 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10665
d4730f92 10666 rel_hdr = reldata->hdr;
c152c796
AM
10667 erel = rel_hdr->contents;
10668 if (rel_hdr->sh_type == SHT_REL)
10669 {
d4730f92 10670 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10671 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10672 }
10673 else
10674 {
10675 irel[0].r_addend = addend;
d4730f92 10676 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10677 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10678 }
10679
d4730f92 10680 ++reldata->count;
c152c796
AM
10681
10682 return TRUE;
10683}
10684
0b52efa6
PB
10685
10686/* Get the output vma of the section pointed to by the sh_link field. */
10687
10688static bfd_vma
10689elf_get_linked_section_vma (struct bfd_link_order *p)
10690{
10691 Elf_Internal_Shdr **elf_shdrp;
10692 asection *s;
10693 int elfsec;
10694
10695 s = p->u.indirect.section;
10696 elf_shdrp = elf_elfsections (s->owner);
10697 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10698 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10699 /* PR 290:
10700 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10701 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10702 sh_info fields. Hence we could get the situation
10703 where elfsec is 0. */
10704 if (elfsec == 0)
10705 {
10706 const struct elf_backend_data *bed
10707 = get_elf_backend_data (s->owner);
10708 if (bed->link_order_error_handler)
d003868e
AM
10709 bed->link_order_error_handler
10710 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10711 return 0;
10712 }
10713 else
10714 {
10715 s = elf_shdrp[elfsec]->bfd_section;
10716 return s->output_section->vma + s->output_offset;
10717 }
0b52efa6
PB
10718}
10719
10720
10721/* Compare two sections based on the locations of the sections they are
10722 linked to. Used by elf_fixup_link_order. */
10723
10724static int
10725compare_link_order (const void * a, const void * b)
10726{
10727 bfd_vma apos;
10728 bfd_vma bpos;
10729
10730 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10731 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10732 if (apos < bpos)
10733 return -1;
10734 return apos > bpos;
10735}
10736
10737
10738/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10739 order as their linked sections. Returns false if this could not be done
10740 because an output section includes both ordered and unordered
10741 sections. Ideally we'd do this in the linker proper. */
10742
10743static bfd_boolean
10744elf_fixup_link_order (bfd *abfd, asection *o)
10745{
10746 int seen_linkorder;
10747 int seen_other;
10748 int n;
10749 struct bfd_link_order *p;
10750 bfd *sub;
10751 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10752 unsigned elfsec;
0b52efa6 10753 struct bfd_link_order **sections;
d33cdfe3 10754 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10755 bfd_vma offset;
3b36f7e6 10756
d33cdfe3
L
10757 other_sec = NULL;
10758 linkorder_sec = NULL;
0b52efa6
PB
10759 seen_other = 0;
10760 seen_linkorder = 0;
8423293d 10761 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10762 {
d33cdfe3 10763 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10764 {
10765 s = p->u.indirect.section;
d33cdfe3
L
10766 sub = s->owner;
10767 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10768 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10769 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10770 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10771 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10772 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10773 {
10774 seen_linkorder++;
10775 linkorder_sec = s;
10776 }
0b52efa6 10777 else
d33cdfe3
L
10778 {
10779 seen_other++;
10780 other_sec = s;
10781 }
0b52efa6
PB
10782 }
10783 else
10784 seen_other++;
d33cdfe3
L
10785
10786 if (seen_other && seen_linkorder)
10787 {
10788 if (other_sec && linkorder_sec)
10789 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10790 o, linkorder_sec,
10791 linkorder_sec->owner, other_sec,
10792 other_sec->owner);
10793 else
10794 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10795 o);
10796 bfd_set_error (bfd_error_bad_value);
10797 return FALSE;
10798 }
0b52efa6
PB
10799 }
10800
10801 if (!seen_linkorder)
10802 return TRUE;
10803
0b52efa6 10804 sections = (struct bfd_link_order **)
14b1c01e
AM
10805 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10806 if (sections == NULL)
10807 return FALSE;
0b52efa6 10808 seen_linkorder = 0;
3b36f7e6 10809
8423293d 10810 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10811 {
10812 sections[seen_linkorder++] = p;
10813 }
10814 /* Sort the input sections in the order of their linked section. */
10815 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10816 compare_link_order);
10817
10818 /* Change the offsets of the sections. */
10819 offset = 0;
10820 for (n = 0; n < seen_linkorder; n++)
10821 {
10822 s = sections[n]->u.indirect.section;
461686a3 10823 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10824 s->output_offset = offset;
10825 sections[n]->offset = offset;
5dabe785 10826 /* FIXME: octets_per_byte. */
0b52efa6
PB
10827 offset += sections[n]->size;
10828 }
10829
4dd07732 10830 free (sections);
0b52efa6
PB
10831 return TRUE;
10832}
10833
9f7c3e5e
AM
10834static void
10835elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10836{
10837 asection *o;
10838
10839 if (flinfo->symstrtab != NULL)
ef10c3ac 10840 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
10841 if (flinfo->contents != NULL)
10842 free (flinfo->contents);
10843 if (flinfo->external_relocs != NULL)
10844 free (flinfo->external_relocs);
10845 if (flinfo->internal_relocs != NULL)
10846 free (flinfo->internal_relocs);
10847 if (flinfo->external_syms != NULL)
10848 free (flinfo->external_syms);
10849 if (flinfo->locsym_shndx != NULL)
10850 free (flinfo->locsym_shndx);
10851 if (flinfo->internal_syms != NULL)
10852 free (flinfo->internal_syms);
10853 if (flinfo->indices != NULL)
10854 free (flinfo->indices);
10855 if (flinfo->sections != NULL)
10856 free (flinfo->sections);
9f7c3e5e
AM
10857 if (flinfo->symshndxbuf != NULL)
10858 free (flinfo->symshndxbuf);
10859 for (o = obfd->sections; o != NULL; o = o->next)
10860 {
10861 struct bfd_elf_section_data *esdo = elf_section_data (o);
10862 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10863 free (esdo->rel.hashes);
10864 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10865 free (esdo->rela.hashes);
10866 }
10867}
0b52efa6 10868
c152c796
AM
10869/* Do the final step of an ELF link. */
10870
10871bfd_boolean
10872bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10873{
10874 bfd_boolean dynamic;
10875 bfd_boolean emit_relocs;
10876 bfd *dynobj;
8b127cbc 10877 struct elf_final_link_info flinfo;
91d6fa6a
NC
10878 asection *o;
10879 struct bfd_link_order *p;
10880 bfd *sub;
c152c796
AM
10881 bfd_size_type max_contents_size;
10882 bfd_size_type max_external_reloc_size;
10883 bfd_size_type max_internal_reloc_count;
10884 bfd_size_type max_sym_count;
10885 bfd_size_type max_sym_shndx_count;
c152c796
AM
10886 Elf_Internal_Sym elfsym;
10887 unsigned int i;
10888 Elf_Internal_Shdr *symtab_hdr;
10889 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
10890 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10891 struct elf_outext_info eoinfo;
10892 bfd_boolean merged;
10893 size_t relativecount = 0;
10894 asection *reldyn = 0;
10895 bfd_size_type amt;
104d59d1
JM
10896 asection *attr_section = NULL;
10897 bfd_vma attr_size = 0;
10898 const char *std_attrs_section;
c152c796
AM
10899
10900 if (! is_elf_hash_table (info->hash))
10901 return FALSE;
10902
0e1862bb 10903 if (bfd_link_pic (info))
c152c796
AM
10904 abfd->flags |= DYNAMIC;
10905
10906 dynamic = elf_hash_table (info)->dynamic_sections_created;
10907 dynobj = elf_hash_table (info)->dynobj;
10908
0e1862bb 10909 emit_relocs = (bfd_link_relocatable (info)
a4676736 10910 || info->emitrelocations);
c152c796 10911
8b127cbc
AM
10912 flinfo.info = info;
10913 flinfo.output_bfd = abfd;
ef10c3ac 10914 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 10915 if (flinfo.symstrtab == NULL)
c152c796
AM
10916 return FALSE;
10917
10918 if (! dynamic)
10919 {
8b127cbc
AM
10920 flinfo.hash_sec = NULL;
10921 flinfo.symver_sec = NULL;
c152c796
AM
10922 }
10923 else
10924 {
3d4d4302 10925 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 10926 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 10927 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
10928 /* Note that it is OK if symver_sec is NULL. */
10929 }
10930
8b127cbc
AM
10931 flinfo.contents = NULL;
10932 flinfo.external_relocs = NULL;
10933 flinfo.internal_relocs = NULL;
10934 flinfo.external_syms = NULL;
10935 flinfo.locsym_shndx = NULL;
10936 flinfo.internal_syms = NULL;
10937 flinfo.indices = NULL;
10938 flinfo.sections = NULL;
8b127cbc 10939 flinfo.symshndxbuf = NULL;
ffbc01cc 10940 flinfo.filesym_count = 0;
c152c796 10941
104d59d1
JM
10942 /* The object attributes have been merged. Remove the input
10943 sections from the link, and set the contents of the output
10944 secton. */
10945 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10946 for (o = abfd->sections; o != NULL; o = o->next)
10947 {
10948 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10949 || strcmp (o->name, ".gnu.attributes") == 0)
10950 {
10951 for (p = o->map_head.link_order; p != NULL; p = p->next)
10952 {
10953 asection *input_section;
10954
10955 if (p->type != bfd_indirect_link_order)
10956 continue;
10957 input_section = p->u.indirect.section;
10958 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10959 elf_link_input_bfd ignores this section. */
10960 input_section->flags &= ~SEC_HAS_CONTENTS;
10961 }
a0c8462f 10962
104d59d1
JM
10963 attr_size = bfd_elf_obj_attr_size (abfd);
10964 if (attr_size)
10965 {
10966 bfd_set_section_size (abfd, o, attr_size);
10967 attr_section = o;
10968 /* Skip this section later on. */
10969 o->map_head.link_order = NULL;
10970 }
10971 else
10972 o->flags |= SEC_EXCLUDE;
10973 }
10974 }
10975
c152c796
AM
10976 /* Count up the number of relocations we will output for each output
10977 section, so that we know the sizes of the reloc sections. We
10978 also figure out some maximum sizes. */
10979 max_contents_size = 0;
10980 max_external_reloc_size = 0;
10981 max_internal_reloc_count = 0;
10982 max_sym_count = 0;
10983 max_sym_shndx_count = 0;
10984 merged = FALSE;
10985 for (o = abfd->sections; o != NULL; o = o->next)
10986 {
10987 struct bfd_elf_section_data *esdo = elf_section_data (o);
10988 o->reloc_count = 0;
10989
8423293d 10990 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10991 {
10992 unsigned int reloc_count = 0;
491d01d3 10993 unsigned int additional_reloc_count = 0;
c152c796 10994 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10995
10996 if (p->type == bfd_section_reloc_link_order
10997 || p->type == bfd_symbol_reloc_link_order)
10998 reloc_count = 1;
10999 else if (p->type == bfd_indirect_link_order)
11000 {
11001 asection *sec;
11002
11003 sec = p->u.indirect.section;
11004 esdi = elf_section_data (sec);
11005
11006 /* Mark all sections which are to be included in the
11007 link. This will normally be every section. We need
11008 to do this so that we can identify any sections which
11009 the linker has decided to not include. */
11010 sec->linker_mark = TRUE;
11011
11012 if (sec->flags & SEC_MERGE)
11013 merged = TRUE;
11014
aed64b35
L
11015 if (esdo->this_hdr.sh_type == SHT_REL
11016 || esdo->this_hdr.sh_type == SHT_RELA)
11017 /* Some backends use reloc_count in relocation sections
11018 to count particular types of relocs. Of course,
11019 reloc sections themselves can't have relocations. */
11020 reloc_count = 0;
0e1862bb 11021 else if (emit_relocs)
491d01d3
YU
11022 {
11023 reloc_count = sec->reloc_count;
11024 if (bed->elf_backend_count_additional_relocs)
11025 {
11026 int c;
11027 c = (*bed->elf_backend_count_additional_relocs) (sec);
11028 additional_reloc_count += c;
11029 }
11030 }
c152c796 11031 else if (bed->elf_backend_count_relocs)
58217f29 11032 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11033
eea6121a
AM
11034 if (sec->rawsize > max_contents_size)
11035 max_contents_size = sec->rawsize;
11036 if (sec->size > max_contents_size)
11037 max_contents_size = sec->size;
c152c796
AM
11038
11039 /* We are interested in just local symbols, not all
11040 symbols. */
11041 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11042 && (sec->owner->flags & DYNAMIC) == 0)
11043 {
11044 size_t sym_count;
11045
11046 if (elf_bad_symtab (sec->owner))
11047 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11048 / bed->s->sizeof_sym);
11049 else
11050 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11051
11052 if (sym_count > max_sym_count)
11053 max_sym_count = sym_count;
11054
11055 if (sym_count > max_sym_shndx_count
6a40cf0c 11056 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11057 max_sym_shndx_count = sym_count;
11058
11059 if ((sec->flags & SEC_RELOC) != 0)
11060 {
d4730f92 11061 size_t ext_size = 0;
c152c796 11062
d4730f92
BS
11063 if (esdi->rel.hdr != NULL)
11064 ext_size = esdi->rel.hdr->sh_size;
11065 if (esdi->rela.hdr != NULL)
11066 ext_size += esdi->rela.hdr->sh_size;
7326c758 11067
c152c796
AM
11068 if (ext_size > max_external_reloc_size)
11069 max_external_reloc_size = ext_size;
11070 if (sec->reloc_count > max_internal_reloc_count)
11071 max_internal_reloc_count = sec->reloc_count;
11072 }
11073 }
11074 }
11075
11076 if (reloc_count == 0)
11077 continue;
11078
491d01d3 11079 reloc_count += additional_reloc_count;
c152c796
AM
11080 o->reloc_count += reloc_count;
11081
0e1862bb 11082 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11083 {
d4730f92 11084 if (esdi->rel.hdr)
491d01d3
YU
11085 {
11086 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11087 esdo->rel.count += additional_reloc_count;
11088 }
d4730f92 11089 if (esdi->rela.hdr)
491d01d3
YU
11090 {
11091 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11092 esdo->rela.count += additional_reloc_count;
11093 }
d4730f92
BS
11094 }
11095 else
11096 {
11097 if (o->use_rela_p)
11098 esdo->rela.count += reloc_count;
2c2b4ed4 11099 else
d4730f92 11100 esdo->rel.count += reloc_count;
c152c796 11101 }
c152c796
AM
11102 }
11103
11104 if (o->reloc_count > 0)
11105 o->flags |= SEC_RELOC;
11106 else
11107 {
11108 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11109 set it (this is probably a bug) and if it is set
11110 assign_section_numbers will create a reloc section. */
11111 o->flags &=~ SEC_RELOC;
11112 }
11113
11114 /* If the SEC_ALLOC flag is not set, force the section VMA to
11115 zero. This is done in elf_fake_sections as well, but forcing
11116 the VMA to 0 here will ensure that relocs against these
11117 sections are handled correctly. */
11118 if ((o->flags & SEC_ALLOC) == 0
11119 && ! o->user_set_vma)
11120 o->vma = 0;
11121 }
11122
0e1862bb 11123 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11124 elf_link_hash_traverse (elf_hash_table (info),
11125 _bfd_elf_link_sec_merge_syms, abfd);
11126
11127 /* Figure out the file positions for everything but the symbol table
11128 and the relocs. We set symcount to force assign_section_numbers
11129 to create a symbol table. */
8539e4e8 11130 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11131 BFD_ASSERT (! abfd->output_has_begun);
11132 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11133 goto error_return;
11134
ee75fd95 11135 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11136 for (o = abfd->sections; o != NULL; o = o->next)
11137 {
d4730f92 11138 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11139 if ((o->flags & SEC_RELOC) != 0)
11140 {
d4730f92
BS
11141 if (esdo->rel.hdr
11142 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11143 goto error_return;
11144
d4730f92
BS
11145 if (esdo->rela.hdr
11146 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11147 goto error_return;
11148 }
11149
11150 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11151 to count upwards while actually outputting the relocations. */
d4730f92
BS
11152 esdo->rel.count = 0;
11153 esdo->rela.count = 0;
0ce398f1
L
11154
11155 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11156 {
11157 /* Cache the section contents so that they can be compressed
11158 later. Use bfd_malloc since it will be freed by
11159 bfd_compress_section_contents. */
11160 unsigned char *contents = esdo->this_hdr.contents;
11161 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11162 abort ();
11163 contents
11164 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11165 if (contents == NULL)
11166 goto error_return;
11167 esdo->this_hdr.contents = contents;
11168 }
c152c796
AM
11169 }
11170
c152c796 11171 /* We have now assigned file positions for all the sections except
a485e98e
AM
11172 .symtab, .strtab, and non-loaded reloc sections. We start the
11173 .symtab section at the current file position, and write directly
11174 to it. We build the .strtab section in memory. */
c152c796
AM
11175 bfd_get_symcount (abfd) = 0;
11176 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11177 /* sh_name is set in prep_headers. */
11178 symtab_hdr->sh_type = SHT_SYMTAB;
11179 /* sh_flags, sh_addr and sh_size all start off zero. */
11180 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11181 /* sh_link is set in assign_section_numbers. */
11182 /* sh_info is set below. */
11183 /* sh_offset is set just below. */
72de5009 11184 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11185
ef10c3ac
L
11186 if (max_sym_count < 20)
11187 max_sym_count = 20;
11188 elf_hash_table (info)->strtabsize = max_sym_count;
11189 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11190 elf_hash_table (info)->strtab
11191 = (struct elf_sym_strtab *) bfd_malloc (amt);
11192 if (elf_hash_table (info)->strtab == NULL)
c152c796 11193 goto error_return;
ef10c3ac
L
11194 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11195 flinfo.symshndxbuf
11196 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11197 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11198
8539e4e8 11199 if (info->strip != strip_all || emit_relocs)
c152c796 11200 {
8539e4e8
AM
11201 file_ptr off = elf_next_file_pos (abfd);
11202
11203 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11204
11205 /* Note that at this point elf_next_file_pos (abfd) is
11206 incorrect. We do not yet know the size of the .symtab section.
11207 We correct next_file_pos below, after we do know the size. */
11208
11209 /* Start writing out the symbol table. The first symbol is always a
11210 dummy symbol. */
c152c796
AM
11211 elfsym.st_value = 0;
11212 elfsym.st_size = 0;
11213 elfsym.st_info = 0;
11214 elfsym.st_other = 0;
11215 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11216 elfsym.st_target_internal = 0;
ef10c3ac
L
11217 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11218 bfd_und_section_ptr, NULL) != 1)
c152c796 11219 goto error_return;
c152c796 11220
8539e4e8
AM
11221 /* Output a symbol for each section. We output these even if we are
11222 discarding local symbols, since they are used for relocs. These
11223 symbols have no names. We store the index of each one in the
11224 index field of the section, so that we can find it again when
11225 outputting relocs. */
11226
c152c796
AM
11227 elfsym.st_size = 0;
11228 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11229 elfsym.st_other = 0;
f0b5bb34 11230 elfsym.st_value = 0;
35fc36a8 11231 elfsym.st_target_internal = 0;
c152c796
AM
11232 for (i = 1; i < elf_numsections (abfd); i++)
11233 {
11234 o = bfd_section_from_elf_index (abfd, i);
11235 if (o != NULL)
f0b5bb34
AM
11236 {
11237 o->target_index = bfd_get_symcount (abfd);
11238 elfsym.st_shndx = i;
0e1862bb 11239 if (!bfd_link_relocatable (info))
f0b5bb34 11240 elfsym.st_value = o->vma;
ef10c3ac
L
11241 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11242 NULL) != 1)
f0b5bb34
AM
11243 goto error_return;
11244 }
c152c796
AM
11245 }
11246 }
11247
11248 /* Allocate some memory to hold information read in from the input
11249 files. */
11250 if (max_contents_size != 0)
11251 {
8b127cbc
AM
11252 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11253 if (flinfo.contents == NULL)
c152c796
AM
11254 goto error_return;
11255 }
11256
11257 if (max_external_reloc_size != 0)
11258 {
8b127cbc
AM
11259 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11260 if (flinfo.external_relocs == NULL)
c152c796
AM
11261 goto error_return;
11262 }
11263
11264 if (max_internal_reloc_count != 0)
11265 {
11266 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11267 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11268 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11269 if (flinfo.internal_relocs == NULL)
c152c796
AM
11270 goto error_return;
11271 }
11272
11273 if (max_sym_count != 0)
11274 {
11275 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11276 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11277 if (flinfo.external_syms == NULL)
c152c796
AM
11278 goto error_return;
11279
11280 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11281 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11282 if (flinfo.internal_syms == NULL)
c152c796
AM
11283 goto error_return;
11284
11285 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11286 flinfo.indices = (long int *) bfd_malloc (amt);
11287 if (flinfo.indices == NULL)
c152c796
AM
11288 goto error_return;
11289
11290 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11291 flinfo.sections = (asection **) bfd_malloc (amt);
11292 if (flinfo.sections == NULL)
c152c796
AM
11293 goto error_return;
11294 }
11295
11296 if (max_sym_shndx_count != 0)
11297 {
11298 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11299 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11300 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11301 goto error_return;
11302 }
11303
11304 if (elf_hash_table (info)->tls_sec)
11305 {
11306 bfd_vma base, end = 0;
11307 asection *sec;
11308
11309 for (sec = elf_hash_table (info)->tls_sec;
11310 sec && (sec->flags & SEC_THREAD_LOCAL);
11311 sec = sec->next)
11312 {
3a800eb9 11313 bfd_size_type size = sec->size;
c152c796 11314
3a800eb9
AM
11315 if (size == 0
11316 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11317 {
91d6fa6a
NC
11318 struct bfd_link_order *ord = sec->map_tail.link_order;
11319
11320 if (ord != NULL)
11321 size = ord->offset + ord->size;
c152c796
AM
11322 }
11323 end = sec->vma + size;
11324 }
11325 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11326 /* Only align end of TLS section if static TLS doesn't have special
11327 alignment requirements. */
11328 if (bed->static_tls_alignment == 1)
11329 end = align_power (end,
11330 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11331 elf_hash_table (info)->tls_size = end - base;
11332 }
11333
0b52efa6
PB
11334 /* Reorder SHF_LINK_ORDER sections. */
11335 for (o = abfd->sections; o != NULL; o = o->next)
11336 {
11337 if (!elf_fixup_link_order (abfd, o))
11338 return FALSE;
11339 }
11340
2f0c68f2
CM
11341 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11342 return FALSE;
11343
c152c796
AM
11344 /* Since ELF permits relocations to be against local symbols, we
11345 must have the local symbols available when we do the relocations.
11346 Since we would rather only read the local symbols once, and we
11347 would rather not keep them in memory, we handle all the
11348 relocations for a single input file at the same time.
11349
11350 Unfortunately, there is no way to know the total number of local
11351 symbols until we have seen all of them, and the local symbol
11352 indices precede the global symbol indices. This means that when
11353 we are generating relocatable output, and we see a reloc against
11354 a global symbol, we can not know the symbol index until we have
11355 finished examining all the local symbols to see which ones we are
11356 going to output. To deal with this, we keep the relocations in
11357 memory, and don't output them until the end of the link. This is
11358 an unfortunate waste of memory, but I don't see a good way around
11359 it. Fortunately, it only happens when performing a relocatable
11360 link, which is not the common case. FIXME: If keep_memory is set
11361 we could write the relocs out and then read them again; I don't
11362 know how bad the memory loss will be. */
11363
c72f2fb2 11364 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11365 sub->output_has_begun = FALSE;
11366 for (o = abfd->sections; o != NULL; o = o->next)
11367 {
8423293d 11368 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11369 {
11370 if (p->type == bfd_indirect_link_order
11371 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11372 == bfd_target_elf_flavour)
11373 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11374 {
11375 if (! sub->output_has_begun)
11376 {
8b127cbc 11377 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11378 goto error_return;
11379 sub->output_has_begun = TRUE;
11380 }
11381 }
11382 else if (p->type == bfd_section_reloc_link_order
11383 || p->type == bfd_symbol_reloc_link_order)
11384 {
11385 if (! elf_reloc_link_order (abfd, info, o, p))
11386 goto error_return;
11387 }
11388 else
11389 {
11390 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11391 {
11392 if (p->type == bfd_indirect_link_order
11393 && (bfd_get_flavour (sub)
11394 == bfd_target_elf_flavour)
11395 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11396 != bed->s->elfclass))
11397 {
11398 const char *iclass, *oclass;
11399
aebf9be7 11400 switch (bed->s->elfclass)
351f65ca 11401 {
aebf9be7
NC
11402 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11403 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11404 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11405 default: abort ();
351f65ca 11406 }
aebf9be7
NC
11407
11408 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11409 {
aebf9be7
NC
11410 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11411 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11412 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11413 default: abort ();
351f65ca
L
11414 }
11415
11416 bfd_set_error (bfd_error_wrong_format);
11417 (*_bfd_error_handler)
11418 (_("%B: file class %s incompatible with %s"),
11419 sub, iclass, oclass);
11420 }
11421
11422 goto error_return;
11423 }
c152c796
AM
11424 }
11425 }
11426 }
11427
c0f00686
L
11428 /* Free symbol buffer if needed. */
11429 if (!info->reduce_memory_overheads)
11430 {
c72f2fb2 11431 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11432 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11433 && elf_tdata (sub)->symbuf)
c0f00686
L
11434 {
11435 free (elf_tdata (sub)->symbuf);
11436 elf_tdata (sub)->symbuf = NULL;
11437 }
11438 }
11439
c152c796
AM
11440 /* Output any global symbols that got converted to local in a
11441 version script or due to symbol visibility. We do this in a
11442 separate step since ELF requires all local symbols to appear
11443 prior to any global symbols. FIXME: We should only do this if
11444 some global symbols were, in fact, converted to become local.
11445 FIXME: Will this work correctly with the Irix 5 linker? */
11446 eoinfo.failed = FALSE;
8b127cbc 11447 eoinfo.flinfo = &flinfo;
c152c796 11448 eoinfo.localsyms = TRUE;
34a79995 11449 eoinfo.file_sym_done = FALSE;
7686d77d 11450 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11451 if (eoinfo.failed)
11452 return FALSE;
11453
4e617b1e
PB
11454 /* If backend needs to output some local symbols not present in the hash
11455 table, do it now. */
8539e4e8
AM
11456 if (bed->elf_backend_output_arch_local_syms
11457 && (info->strip != strip_all || emit_relocs))
4e617b1e 11458 {
6e0b88f1 11459 typedef int (*out_sym_func)
4e617b1e
PB
11460 (void *, const char *, Elf_Internal_Sym *, asection *,
11461 struct elf_link_hash_entry *);
11462
11463 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11464 (abfd, info, &flinfo,
11465 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11466 return FALSE;
11467 }
11468
c152c796
AM
11469 /* That wrote out all the local symbols. Finish up the symbol table
11470 with the global symbols. Even if we want to strip everything we
11471 can, we still need to deal with those global symbols that got
11472 converted to local in a version script. */
11473
11474 /* The sh_info field records the index of the first non local symbol. */
11475 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11476
11477 if (dynamic
cae1fbbb
L
11478 && elf_hash_table (info)->dynsym != NULL
11479 && (elf_hash_table (info)->dynsym->output_section
11480 != bfd_abs_section_ptr))
c152c796
AM
11481 {
11482 Elf_Internal_Sym sym;
cae1fbbb 11483 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11484 long last_local = 0;
11485
11486 /* Write out the section symbols for the output sections. */
0e1862bb
L
11487 if (bfd_link_pic (info)
11488 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11489 {
11490 asection *s;
11491
11492 sym.st_size = 0;
11493 sym.st_name = 0;
11494 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11495 sym.st_other = 0;
35fc36a8 11496 sym.st_target_internal = 0;
c152c796
AM
11497
11498 for (s = abfd->sections; s != NULL; s = s->next)
11499 {
11500 int indx;
11501 bfd_byte *dest;
11502 long dynindx;
11503
c152c796 11504 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11505 if (dynindx <= 0)
11506 continue;
11507 indx = elf_section_data (s)->this_idx;
c152c796
AM
11508 BFD_ASSERT (indx > 0);
11509 sym.st_shndx = indx;
c0d5a53d
L
11510 if (! check_dynsym (abfd, &sym))
11511 return FALSE;
c152c796
AM
11512 sym.st_value = s->vma;
11513 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11514 if (last_local < dynindx)
11515 last_local = dynindx;
c152c796
AM
11516 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11517 }
c152c796
AM
11518 }
11519
11520 /* Write out the local dynsyms. */
11521 if (elf_hash_table (info)->dynlocal)
11522 {
11523 struct elf_link_local_dynamic_entry *e;
11524 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11525 {
11526 asection *s;
11527 bfd_byte *dest;
11528
935bd1e0 11529 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11530 Note that we saved a word of storage and overwrote
11531 the original st_name with the dynstr_index. */
11532 sym = e->isym;
935bd1e0 11533 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11534
cb33740c
AM
11535 s = bfd_section_from_elf_index (e->input_bfd,
11536 e->isym.st_shndx);
11537 if (s != NULL)
c152c796 11538 {
c152c796
AM
11539 sym.st_shndx =
11540 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11541 if (! check_dynsym (abfd, &sym))
11542 return FALSE;
c152c796
AM
11543 sym.st_value = (s->output_section->vma
11544 + s->output_offset
11545 + e->isym.st_value);
11546 }
11547
11548 if (last_local < e->dynindx)
11549 last_local = e->dynindx;
11550
11551 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11552 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11553 }
11554 }
11555
cae1fbbb 11556 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11557 last_local + 1;
11558 }
11559
11560 /* We get the global symbols from the hash table. */
11561 eoinfo.failed = FALSE;
11562 eoinfo.localsyms = FALSE;
8b127cbc 11563 eoinfo.flinfo = &flinfo;
7686d77d 11564 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11565 if (eoinfo.failed)
11566 return FALSE;
11567
11568 /* If backend needs to output some symbols not present in the hash
11569 table, do it now. */
8539e4e8
AM
11570 if (bed->elf_backend_output_arch_syms
11571 && (info->strip != strip_all || emit_relocs))
c152c796 11572 {
6e0b88f1 11573 typedef int (*out_sym_func)
c152c796
AM
11574 (void *, const char *, Elf_Internal_Sym *, asection *,
11575 struct elf_link_hash_entry *);
11576
11577 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11578 (abfd, info, &flinfo,
11579 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11580 return FALSE;
11581 }
11582
ef10c3ac
L
11583 /* Finalize the .strtab section. */
11584 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11585
11586 /* Swap out the .strtab section. */
11587 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11588 return FALSE;
11589
11590 /* Now we know the size of the symtab section. */
c152c796
AM
11591 if (bfd_get_symcount (abfd) > 0)
11592 {
ee3b52e9
L
11593 /* Finish up and write out the symbol string table (.strtab)
11594 section. */
11595 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11596 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11597
6a40cf0c
NC
11598 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11599 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11600 {
11601 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11602 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11603 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11604 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11605 symtab_shndx_hdr->sh_size = amt;
11606
11607 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11608 off, TRUE);
11609
11610 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11611 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11612 return FALSE;
11613 }
ee3b52e9
L
11614
11615 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11616 /* sh_name was set in prep_headers. */
11617 symstrtab_hdr->sh_type = SHT_STRTAB;
11618 symstrtab_hdr->sh_flags = 0;
11619 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11620 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11621 symstrtab_hdr->sh_entsize = 0;
11622 symstrtab_hdr->sh_link = 0;
11623 symstrtab_hdr->sh_info = 0;
11624 /* sh_offset is set just below. */
11625 symstrtab_hdr->sh_addralign = 1;
11626
11627 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11628 off, TRUE);
11629 elf_next_file_pos (abfd) = off;
11630
c152c796 11631 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11632 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11633 return FALSE;
11634 }
11635
11636 /* Adjust the relocs to have the correct symbol indices. */
11637 for (o = abfd->sections; o != NULL; o = o->next)
11638 {
d4730f92 11639 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11640 bfd_boolean sort;
c152c796
AM
11641 if ((o->flags & SEC_RELOC) == 0)
11642 continue;
11643
28dbcedc 11644 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11645 if (esdo->rel.hdr != NULL
11646 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11647 return FALSE;
11648 if (esdo->rela.hdr != NULL
11649 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11650 return FALSE;
c152c796
AM
11651
11652 /* Set the reloc_count field to 0 to prevent write_relocs from
11653 trying to swap the relocs out itself. */
11654 o->reloc_count = 0;
11655 }
11656
11657 if (dynamic && info->combreloc && dynobj != NULL)
11658 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11659
11660 /* If we are linking against a dynamic object, or generating a
11661 shared library, finish up the dynamic linking information. */
11662 if (dynamic)
11663 {
11664 bfd_byte *dyncon, *dynconend;
11665
11666 /* Fix up .dynamic entries. */
3d4d4302 11667 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11668 BFD_ASSERT (o != NULL);
11669
11670 dyncon = o->contents;
eea6121a 11671 dynconend = o->contents + o->size;
c152c796
AM
11672 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11673 {
11674 Elf_Internal_Dyn dyn;
11675 const char *name;
11676 unsigned int type;
11677
11678 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11679
11680 switch (dyn.d_tag)
11681 {
11682 default:
11683 continue;
11684 case DT_NULL:
11685 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11686 {
11687 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11688 {
11689 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11690 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11691 default: continue;
11692 }
11693 dyn.d_un.d_val = relativecount;
11694 relativecount = 0;
11695 break;
11696 }
11697 continue;
11698
11699 case DT_INIT:
11700 name = info->init_function;
11701 goto get_sym;
11702 case DT_FINI:
11703 name = info->fini_function;
11704 get_sym:
11705 {
11706 struct elf_link_hash_entry *h;
11707
11708 h = elf_link_hash_lookup (elf_hash_table (info), name,
11709 FALSE, FALSE, TRUE);
11710 if (h != NULL
11711 && (h->root.type == bfd_link_hash_defined
11712 || h->root.type == bfd_link_hash_defweak))
11713 {
bef26483 11714 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11715 o = h->root.u.def.section;
11716 if (o->output_section != NULL)
bef26483 11717 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11718 + o->output_offset);
11719 else
11720 {
11721 /* The symbol is imported from another shared
11722 library and does not apply to this one. */
bef26483 11723 dyn.d_un.d_ptr = 0;
c152c796
AM
11724 }
11725 break;
11726 }
11727 }
11728 continue;
11729
11730 case DT_PREINIT_ARRAYSZ:
11731 name = ".preinit_array";
11732 goto get_size;
11733 case DT_INIT_ARRAYSZ:
11734 name = ".init_array";
11735 goto get_size;
11736 case DT_FINI_ARRAYSZ:
11737 name = ".fini_array";
11738 get_size:
11739 o = bfd_get_section_by_name (abfd, name);
11740 if (o == NULL)
11741 {
11742 (*_bfd_error_handler)
d003868e 11743 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11744 goto error_return;
11745 }
eea6121a 11746 if (o->size == 0)
c152c796
AM
11747 (*_bfd_error_handler)
11748 (_("warning: %s section has zero size"), name);
eea6121a 11749 dyn.d_un.d_val = o->size;
c152c796
AM
11750 break;
11751
11752 case DT_PREINIT_ARRAY:
11753 name = ".preinit_array";
11754 goto get_vma;
11755 case DT_INIT_ARRAY:
11756 name = ".init_array";
11757 goto get_vma;
11758 case DT_FINI_ARRAY:
11759 name = ".fini_array";
11760 goto get_vma;
11761
11762 case DT_HASH:
11763 name = ".hash";
11764 goto get_vma;
fdc90cb4
JJ
11765 case DT_GNU_HASH:
11766 name = ".gnu.hash";
11767 goto get_vma;
c152c796
AM
11768 case DT_STRTAB:
11769 name = ".dynstr";
11770 goto get_vma;
11771 case DT_SYMTAB:
11772 name = ".dynsym";
11773 goto get_vma;
11774 case DT_VERDEF:
11775 name = ".gnu.version_d";
11776 goto get_vma;
11777 case DT_VERNEED:
11778 name = ".gnu.version_r";
11779 goto get_vma;
11780 case DT_VERSYM:
11781 name = ".gnu.version";
11782 get_vma:
11783 o = bfd_get_section_by_name (abfd, name);
11784 if (o == NULL)
11785 {
11786 (*_bfd_error_handler)
d003868e 11787 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11788 goto error_return;
11789 }
894891db
NC
11790 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11791 {
11792 (*_bfd_error_handler)
11793 (_("warning: section '%s' is being made into a note"), name);
11794 bfd_set_error (bfd_error_nonrepresentable_section);
11795 goto error_return;
11796 }
c152c796
AM
11797 dyn.d_un.d_ptr = o->vma;
11798 break;
11799
11800 case DT_REL:
11801 case DT_RELA:
11802 case DT_RELSZ:
11803 case DT_RELASZ:
11804 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11805 type = SHT_REL;
11806 else
11807 type = SHT_RELA;
11808 dyn.d_un.d_val = 0;
bef26483 11809 dyn.d_un.d_ptr = 0;
c152c796
AM
11810 for (i = 1; i < elf_numsections (abfd); i++)
11811 {
11812 Elf_Internal_Shdr *hdr;
11813
11814 hdr = elf_elfsections (abfd)[i];
11815 if (hdr->sh_type == type
11816 && (hdr->sh_flags & SHF_ALLOC) != 0)
11817 {
11818 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11819 dyn.d_un.d_val += hdr->sh_size;
11820 else
11821 {
bef26483
AM
11822 if (dyn.d_un.d_ptr == 0
11823 || hdr->sh_addr < dyn.d_un.d_ptr)
11824 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11825 }
11826 }
11827 }
11828 break;
11829 }
11830 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11831 }
11832 }
11833
11834 /* If we have created any dynamic sections, then output them. */
11835 if (dynobj != NULL)
11836 {
11837 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11838 goto error_return;
11839
943284cc 11840 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 11841 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 11842 || info->error_textrel)
3d4d4302 11843 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11844 {
11845 bfd_byte *dyncon, *dynconend;
11846
943284cc
DJ
11847 dyncon = o->contents;
11848 dynconend = o->contents + o->size;
11849 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11850 {
11851 Elf_Internal_Dyn dyn;
11852
11853 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11854
11855 if (dyn.d_tag == DT_TEXTREL)
11856 {
c192a133
AM
11857 if (info->error_textrel)
11858 info->callbacks->einfo
11859 (_("%P%X: read-only segment has dynamic relocations.\n"));
11860 else
11861 info->callbacks->einfo
11862 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11863 break;
11864 }
11865 }
11866 }
11867
c152c796
AM
11868 for (o = dynobj->sections; o != NULL; o = o->next)
11869 {
11870 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11871 || o->size == 0
c152c796
AM
11872 || o->output_section == bfd_abs_section_ptr)
11873 continue;
11874 if ((o->flags & SEC_LINKER_CREATED) == 0)
11875 {
11876 /* At this point, we are only interested in sections
11877 created by _bfd_elf_link_create_dynamic_sections. */
11878 continue;
11879 }
3722b82f
AM
11880 if (elf_hash_table (info)->stab_info.stabstr == o)
11881 continue;
eea6121a
AM
11882 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11883 continue;
3d4d4302 11884 if (strcmp (o->name, ".dynstr") != 0)
c152c796 11885 {
5dabe785 11886 /* FIXME: octets_per_byte. */
c152c796
AM
11887 if (! bfd_set_section_contents (abfd, o->output_section,
11888 o->contents,
11889 (file_ptr) o->output_offset,
eea6121a 11890 o->size))
c152c796
AM
11891 goto error_return;
11892 }
11893 else
11894 {
11895 /* The contents of the .dynstr section are actually in a
11896 stringtab. */
8539e4e8
AM
11897 file_ptr off;
11898
c152c796
AM
11899 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11900 if (bfd_seek (abfd, off, SEEK_SET) != 0
11901 || ! _bfd_elf_strtab_emit (abfd,
11902 elf_hash_table (info)->dynstr))
11903 goto error_return;
11904 }
11905 }
11906 }
11907
0e1862bb 11908 if (bfd_link_relocatable (info))
c152c796
AM
11909 {
11910 bfd_boolean failed = FALSE;
11911
11912 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11913 if (failed)
11914 goto error_return;
11915 }
11916
11917 /* If we have optimized stabs strings, output them. */
3722b82f 11918 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11919 {
11920 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11921 goto error_return;
11922 }
11923
9f7c3e5e
AM
11924 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11925 goto error_return;
c152c796 11926
9f7c3e5e 11927 elf_final_link_free (abfd, &flinfo);
c152c796 11928
12bd6957 11929 elf_linker (abfd) = TRUE;
c152c796 11930
104d59d1
JM
11931 if (attr_section)
11932 {
a50b1753 11933 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11934 if (contents == NULL)
d0f16d5e 11935 return FALSE; /* Bail out and fail. */
104d59d1
JM
11936 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11937 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11938 free (contents);
11939 }
11940
c152c796
AM
11941 return TRUE;
11942
11943 error_return:
9f7c3e5e 11944 elf_final_link_free (abfd, &flinfo);
c152c796
AM
11945 return FALSE;
11946}
11947\f
5241d853
RS
11948/* Initialize COOKIE for input bfd ABFD. */
11949
11950static bfd_boolean
11951init_reloc_cookie (struct elf_reloc_cookie *cookie,
11952 struct bfd_link_info *info, bfd *abfd)
11953{
11954 Elf_Internal_Shdr *symtab_hdr;
11955 const struct elf_backend_data *bed;
11956
11957 bed = get_elf_backend_data (abfd);
11958 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11959
11960 cookie->abfd = abfd;
11961 cookie->sym_hashes = elf_sym_hashes (abfd);
11962 cookie->bad_symtab = elf_bad_symtab (abfd);
11963 if (cookie->bad_symtab)
11964 {
11965 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11966 cookie->extsymoff = 0;
11967 }
11968 else
11969 {
11970 cookie->locsymcount = symtab_hdr->sh_info;
11971 cookie->extsymoff = symtab_hdr->sh_info;
11972 }
11973
11974 if (bed->s->arch_size == 32)
11975 cookie->r_sym_shift = 8;
11976 else
11977 cookie->r_sym_shift = 32;
11978
11979 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11980 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11981 {
11982 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11983 cookie->locsymcount, 0,
11984 NULL, NULL, NULL);
11985 if (cookie->locsyms == NULL)
11986 {
11987 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11988 return FALSE;
11989 }
11990 if (info->keep_memory)
11991 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11992 }
11993 return TRUE;
11994}
11995
11996/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11997
11998static void
11999fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12000{
12001 Elf_Internal_Shdr *symtab_hdr;
12002
12003 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12004 if (cookie->locsyms != NULL
12005 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12006 free (cookie->locsyms);
12007}
12008
12009/* Initialize the relocation information in COOKIE for input section SEC
12010 of input bfd ABFD. */
12011
12012static bfd_boolean
12013init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12014 struct bfd_link_info *info, bfd *abfd,
12015 asection *sec)
12016{
12017 const struct elf_backend_data *bed;
12018
12019 if (sec->reloc_count == 0)
12020 {
12021 cookie->rels = NULL;
12022 cookie->relend = NULL;
12023 }
12024 else
12025 {
12026 bed = get_elf_backend_data (abfd);
12027
12028 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12029 info->keep_memory);
12030 if (cookie->rels == NULL)
12031 return FALSE;
12032 cookie->rel = cookie->rels;
12033 cookie->relend = (cookie->rels
12034 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12035 }
12036 cookie->rel = cookie->rels;
12037 return TRUE;
12038}
12039
12040/* Free the memory allocated by init_reloc_cookie_rels,
12041 if appropriate. */
12042
12043static void
12044fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12045 asection *sec)
12046{
12047 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12048 free (cookie->rels);
12049}
12050
12051/* Initialize the whole of COOKIE for input section SEC. */
12052
12053static bfd_boolean
12054init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12055 struct bfd_link_info *info,
12056 asection *sec)
12057{
12058 if (!init_reloc_cookie (cookie, info, sec->owner))
12059 goto error1;
12060 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12061 goto error2;
12062 return TRUE;
12063
12064 error2:
12065 fini_reloc_cookie (cookie, sec->owner);
12066 error1:
12067 return FALSE;
12068}
12069
12070/* Free the memory allocated by init_reloc_cookie_for_section,
12071 if appropriate. */
12072
12073static void
12074fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12075 asection *sec)
12076{
12077 fini_reloc_cookie_rels (cookie, sec);
12078 fini_reloc_cookie (cookie, sec->owner);
12079}
12080\f
c152c796
AM
12081/* Garbage collect unused sections. */
12082
07adf181
AM
12083/* Default gc_mark_hook. */
12084
12085asection *
12086_bfd_elf_gc_mark_hook (asection *sec,
12087 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12088 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12089 struct elf_link_hash_entry *h,
12090 Elf_Internal_Sym *sym)
12091{
12092 if (h != NULL)
12093 {
12094 switch (h->root.type)
12095 {
12096 case bfd_link_hash_defined:
12097 case bfd_link_hash_defweak:
12098 return h->root.u.def.section;
12099
12100 case bfd_link_hash_common:
12101 return h->root.u.c.p->section;
12102
12103 default:
12104 break;
12105 }
12106 }
12107 else
12108 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12109
12110 return NULL;
12111}
12112
5241d853
RS
12113/* COOKIE->rel describes a relocation against section SEC, which is
12114 a section we've decided to keep. Return the section that contains
12115 the relocation symbol, or NULL if no section contains it. */
12116
12117asection *
12118_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12119 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12120 struct elf_reloc_cookie *cookie,
12121 bfd_boolean *start_stop)
5241d853
RS
12122{
12123 unsigned long r_symndx;
12124 struct elf_link_hash_entry *h;
12125
12126 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12127 if (r_symndx == STN_UNDEF)
5241d853
RS
12128 return NULL;
12129
12130 if (r_symndx >= cookie->locsymcount
12131 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12132 {
12133 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12134 if (h == NULL)
12135 {
12136 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12137 sec->owner);
12138 return NULL;
12139 }
5241d853
RS
12140 while (h->root.type == bfd_link_hash_indirect
12141 || h->root.type == bfd_link_hash_warning)
12142 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12143 h->mark = 1;
4e6b54a6
AM
12144 /* If this symbol is weak and there is a non-weak definition, we
12145 keep the non-weak definition because many backends put
12146 dynamic reloc info on the non-weak definition for code
12147 handling copy relocs. */
12148 if (h->u.weakdef != NULL)
12149 h->u.weakdef->mark = 1;
1cce69b9
AM
12150
12151 if (start_stop != NULL
12152 && (h->root.type == bfd_link_hash_undefined
12153 || h->root.type == bfd_link_hash_undefweak))
12154 {
12155 /* To work around a glibc bug, mark all XXX input sections
12156 when there is an as yet undefined reference to __start_XXX
12157 or __stop_XXX symbols. The linker will later define such
12158 symbols for orphan input sections that have a name
12159 representable as a C identifier. */
12160 const char *sec_name = NULL;
12161 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12162 sec_name = h->root.root.string + 8;
12163 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12164 sec_name = h->root.root.string + 7;
12165
12166 if (sec_name != NULL && *sec_name != '\0')
12167 {
12168 bfd *i;
12169
12170 for (i = info->input_bfds; i != NULL; i = i->link.next)
12171 {
12172 asection *s = bfd_get_section_by_name (i, sec_name);
12173 if (s != NULL && !s->gc_mark)
12174 {
12175 *start_stop = TRUE;
12176 return s;
12177 }
12178 }
12179 }
12180 }
12181
5241d853
RS
12182 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12183 }
12184
12185 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12186 &cookie->locsyms[r_symndx]);
12187}
12188
12189/* COOKIE->rel describes a relocation against section SEC, which is
12190 a section we've decided to keep. Mark the section that contains
9d0a14d3 12191 the relocation symbol. */
5241d853
RS
12192
12193bfd_boolean
12194_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12195 asection *sec,
12196 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12197 struct elf_reloc_cookie *cookie)
5241d853
RS
12198{
12199 asection *rsec;
1cce69b9 12200 bfd_boolean start_stop = FALSE;
5241d853 12201
1cce69b9
AM
12202 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12203 while (rsec != NULL)
5241d853 12204 {
1cce69b9
AM
12205 if (!rsec->gc_mark)
12206 {
12207 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12208 || (rsec->owner->flags & DYNAMIC) != 0)
12209 rsec->gc_mark = 1;
12210 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12211 return FALSE;
12212 }
12213 if (!start_stop)
12214 break;
199af150 12215 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12216 }
12217 return TRUE;
12218}
12219
07adf181
AM
12220/* The mark phase of garbage collection. For a given section, mark
12221 it and any sections in this section's group, and all the sections
12222 which define symbols to which it refers. */
12223
ccfa59ea
AM
12224bfd_boolean
12225_bfd_elf_gc_mark (struct bfd_link_info *info,
12226 asection *sec,
6a5bb875 12227 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12228{
12229 bfd_boolean ret;
9d0a14d3 12230 asection *group_sec, *eh_frame;
c152c796
AM
12231
12232 sec->gc_mark = 1;
12233
12234 /* Mark all the sections in the group. */
12235 group_sec = elf_section_data (sec)->next_in_group;
12236 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12237 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12238 return FALSE;
12239
12240 /* Look through the section relocs. */
12241 ret = TRUE;
9d0a14d3
RS
12242 eh_frame = elf_eh_frame_section (sec->owner);
12243 if ((sec->flags & SEC_RELOC) != 0
12244 && sec->reloc_count > 0
12245 && sec != eh_frame)
c152c796 12246 {
5241d853 12247 struct elf_reloc_cookie cookie;
c152c796 12248
5241d853
RS
12249 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12250 ret = FALSE;
c152c796 12251 else
c152c796 12252 {
5241d853 12253 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12254 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12255 {
12256 ret = FALSE;
12257 break;
12258 }
12259 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12260 }
12261 }
9d0a14d3
RS
12262
12263 if (ret && eh_frame && elf_fde_list (sec))
12264 {
12265 struct elf_reloc_cookie cookie;
12266
12267 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12268 ret = FALSE;
12269 else
12270 {
12271 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12272 gc_mark_hook, &cookie))
12273 ret = FALSE;
12274 fini_reloc_cookie_for_section (&cookie, eh_frame);
12275 }
12276 }
12277
2f0c68f2
CM
12278 eh_frame = elf_section_eh_frame_entry (sec);
12279 if (ret && eh_frame && !eh_frame->gc_mark)
12280 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12281 ret = FALSE;
12282
c152c796
AM
12283 return ret;
12284}
12285
3c758495
TG
12286/* Scan and mark sections in a special or debug section group. */
12287
12288static void
12289_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12290{
12291 /* Point to first section of section group. */
12292 asection *ssec;
12293 /* Used to iterate the section group. */
12294 asection *msec;
12295
12296 bfd_boolean is_special_grp = TRUE;
12297 bfd_boolean is_debug_grp = TRUE;
12298
12299 /* First scan to see if group contains any section other than debug
12300 and special section. */
12301 ssec = msec = elf_next_in_group (grp);
12302 do
12303 {
12304 if ((msec->flags & SEC_DEBUGGING) == 0)
12305 is_debug_grp = FALSE;
12306
12307 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12308 is_special_grp = FALSE;
12309
12310 msec = elf_next_in_group (msec);
12311 }
12312 while (msec != ssec);
12313
12314 /* If this is a pure debug section group or pure special section group,
12315 keep all sections in this group. */
12316 if (is_debug_grp || is_special_grp)
12317 {
12318 do
12319 {
12320 msec->gc_mark = 1;
12321 msec = elf_next_in_group (msec);
12322 }
12323 while (msec != ssec);
12324 }
12325}
12326
7f6ab9f8
AM
12327/* Keep debug and special sections. */
12328
12329bfd_boolean
12330_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12331 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12332{
12333 bfd *ibfd;
12334
c72f2fb2 12335 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12336 {
12337 asection *isec;
12338 bfd_boolean some_kept;
b40bf0a2 12339 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12340
12341 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12342 continue;
12343
b40bf0a2
NC
12344 /* Ensure all linker created sections are kept,
12345 see if any other section is already marked,
12346 and note if we have any fragmented debug sections. */
12347 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12348 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12349 {
12350 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12351 isec->gc_mark = 1;
12352 else if (isec->gc_mark)
12353 some_kept = TRUE;
b40bf0a2
NC
12354
12355 if (debug_frag_seen == FALSE
12356 && (isec->flags & SEC_DEBUGGING)
12357 && CONST_STRNEQ (isec->name, ".debug_line."))
12358 debug_frag_seen = TRUE;
7f6ab9f8
AM
12359 }
12360
12361 /* If no section in this file will be kept, then we can
b40bf0a2 12362 toss out the debug and special sections. */
7f6ab9f8
AM
12363 if (!some_kept)
12364 continue;
12365
12366 /* Keep debug and special sections like .comment when they are
3c758495
TG
12367 not part of a group. Also keep section groups that contain
12368 just debug sections or special sections. */
7f6ab9f8 12369 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12370 {
12371 if ((isec->flags & SEC_GROUP) != 0)
12372 _bfd_elf_gc_mark_debug_special_section_group (isec);
12373 else if (((isec->flags & SEC_DEBUGGING) != 0
12374 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12375 && elf_next_in_group (isec) == NULL)
12376 isec->gc_mark = 1;
12377 }
b40bf0a2
NC
12378
12379 if (! debug_frag_seen)
12380 continue;
12381
12382 /* Look for CODE sections which are going to be discarded,
12383 and find and discard any fragmented debug sections which
12384 are associated with that code section. */
12385 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12386 if ((isec->flags & SEC_CODE) != 0
12387 && isec->gc_mark == 0)
12388 {
12389 unsigned int ilen;
12390 asection *dsec;
12391
12392 ilen = strlen (isec->name);
12393
12394 /* Association is determined by the name of the debug section
12395 containing the name of the code section as a suffix. For
12396 example .debug_line.text.foo is a debug section associated
12397 with .text.foo. */
12398 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12399 {
12400 unsigned int dlen;
12401
12402 if (dsec->gc_mark == 0
12403 || (dsec->flags & SEC_DEBUGGING) == 0)
12404 continue;
12405
12406 dlen = strlen (dsec->name);
12407
12408 if (dlen > ilen
12409 && strncmp (dsec->name + (dlen - ilen),
12410 isec->name, ilen) == 0)
12411 {
12412 dsec->gc_mark = 0;
b40bf0a2
NC
12413 }
12414 }
12415 }
7f6ab9f8
AM
12416 }
12417 return TRUE;
12418}
12419
c152c796
AM
12420/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12421
c17d87de
NC
12422struct elf_gc_sweep_symbol_info
12423{
ccabcbe5
AM
12424 struct bfd_link_info *info;
12425 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12426 bfd_boolean);
12427};
12428
c152c796 12429static bfd_boolean
ccabcbe5 12430elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12431{
1d5316ab
AM
12432 if (!h->mark
12433 && (((h->root.type == bfd_link_hash_defined
12434 || h->root.type == bfd_link_hash_defweak)
c4621b33 12435 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12436 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12437 || h->root.type == bfd_link_hash_undefined
12438 || h->root.type == bfd_link_hash_undefweak))
12439 {
12440 struct elf_gc_sweep_symbol_info *inf;
12441
12442 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12443 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12444 h->def_regular = 0;
12445 h->ref_regular = 0;
12446 h->ref_regular_nonweak = 0;
ccabcbe5 12447 }
c152c796
AM
12448
12449 return TRUE;
12450}
12451
12452/* The sweep phase of garbage collection. Remove all garbage sections. */
12453
12454typedef bfd_boolean (*gc_sweep_hook_fn)
12455 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12456
12457static bfd_boolean
ccabcbe5 12458elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12459{
12460 bfd *sub;
ccabcbe5
AM
12461 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12462 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12463 unsigned long section_sym_count;
12464 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12465
c72f2fb2 12466 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12467 {
12468 asection *o;
12469
b19a8f85
L
12470 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12471 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12472 continue;
12473
12474 for (o = sub->sections; o != NULL; o = o->next)
12475 {
a33dafc3
L
12476 /* When any section in a section group is kept, we keep all
12477 sections in the section group. If the first member of
12478 the section group is excluded, we will also exclude the
12479 group section. */
12480 if (o->flags & SEC_GROUP)
12481 {
12482 asection *first = elf_next_in_group (o);
12483 o->gc_mark = first->gc_mark;
12484 }
c152c796 12485
1e7eae0d 12486 if (o->gc_mark)
c152c796
AM
12487 continue;
12488
12489 /* Skip sweeping sections already excluded. */
12490 if (o->flags & SEC_EXCLUDE)
12491 continue;
12492
12493 /* Since this is early in the link process, it is simple
12494 to remove a section from the output. */
12495 o->flags |= SEC_EXCLUDE;
12496
c55fe096 12497 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12498 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12499
c152c796
AM
12500 /* But we also have to update some of the relocation
12501 info we collected before. */
12502 if (gc_sweep_hook
e8aaee2a 12503 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12504 && o->reloc_count != 0
12505 && !((info->strip == strip_all || info->strip == strip_debugger)
12506 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12507 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12508 {
12509 Elf_Internal_Rela *internal_relocs;
12510 bfd_boolean r;
12511
12512 internal_relocs
12513 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12514 info->keep_memory);
12515 if (internal_relocs == NULL)
12516 return FALSE;
12517
12518 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12519
12520 if (elf_section_data (o)->relocs != internal_relocs)
12521 free (internal_relocs);
12522
12523 if (!r)
12524 return FALSE;
12525 }
12526 }
12527 }
12528
12529 /* Remove the symbols that were in the swept sections from the dynamic
12530 symbol table. GCFIXME: Anyone know how to get them out of the
12531 static symbol table as well? */
ccabcbe5
AM
12532 sweep_info.info = info;
12533 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12534 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12535 &sweep_info);
c152c796 12536
ccabcbe5 12537 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12538 return TRUE;
12539}
12540
12541/* Propagate collected vtable information. This is called through
12542 elf_link_hash_traverse. */
12543
12544static bfd_boolean
12545elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12546{
c152c796 12547 /* Those that are not vtables. */
f6e332e6 12548 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12549 return TRUE;
12550
12551 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12552 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12553 return TRUE;
12554
12555 /* If we've already been done, exit. */
f6e332e6 12556 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12557 return TRUE;
12558
12559 /* Make sure the parent's table is up to date. */
f6e332e6 12560 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12561
f6e332e6 12562 if (h->vtable->used == NULL)
c152c796
AM
12563 {
12564 /* None of this table's entries were referenced. Re-use the
12565 parent's table. */
f6e332e6
AM
12566 h->vtable->used = h->vtable->parent->vtable->used;
12567 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12568 }
12569 else
12570 {
12571 size_t n;
12572 bfd_boolean *cu, *pu;
12573
12574 /* Or the parent's entries into ours. */
f6e332e6 12575 cu = h->vtable->used;
c152c796 12576 cu[-1] = TRUE;
f6e332e6 12577 pu = h->vtable->parent->vtable->used;
c152c796
AM
12578 if (pu != NULL)
12579 {
12580 const struct elf_backend_data *bed;
12581 unsigned int log_file_align;
12582
12583 bed = get_elf_backend_data (h->root.u.def.section->owner);
12584 log_file_align = bed->s->log_file_align;
f6e332e6 12585 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12586 while (n--)
12587 {
12588 if (*pu)
12589 *cu = TRUE;
12590 pu++;
12591 cu++;
12592 }
12593 }
12594 }
12595
12596 return TRUE;
12597}
12598
12599static bfd_boolean
12600elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12601{
12602 asection *sec;
12603 bfd_vma hstart, hend;
12604 Elf_Internal_Rela *relstart, *relend, *rel;
12605 const struct elf_backend_data *bed;
12606 unsigned int log_file_align;
12607
c152c796
AM
12608 /* Take care of both those symbols that do not describe vtables as
12609 well as those that are not loaded. */
f6e332e6 12610 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12611 return TRUE;
12612
12613 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12614 || h->root.type == bfd_link_hash_defweak);
12615
12616 sec = h->root.u.def.section;
12617 hstart = h->root.u.def.value;
12618 hend = hstart + h->size;
12619
12620 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12621 if (!relstart)
12622 return *(bfd_boolean *) okp = FALSE;
12623 bed = get_elf_backend_data (sec->owner);
12624 log_file_align = bed->s->log_file_align;
12625
12626 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12627
12628 for (rel = relstart; rel < relend; ++rel)
12629 if (rel->r_offset >= hstart && rel->r_offset < hend)
12630 {
12631 /* If the entry is in use, do nothing. */
f6e332e6
AM
12632 if (h->vtable->used
12633 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12634 {
12635 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12636 if (h->vtable->used[entry])
c152c796
AM
12637 continue;
12638 }
12639 /* Otherwise, kill it. */
12640 rel->r_offset = rel->r_info = rel->r_addend = 0;
12641 }
12642
12643 return TRUE;
12644}
12645
87538722
AM
12646/* Mark sections containing dynamically referenced symbols. When
12647 building shared libraries, we must assume that any visible symbol is
12648 referenced. */
715df9b8 12649
64d03ab5
AM
12650bfd_boolean
12651bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12652{
87538722 12653 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12654 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12655
715df9b8
EB
12656 if ((h->root.type == bfd_link_hash_defined
12657 || h->root.type == bfd_link_hash_defweak)
87538722 12658 && (h->ref_dynamic
c4621b33 12659 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12660 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12661 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12662 && (!bfd_link_executable (info)
b407645f
AM
12663 || info->export_dynamic
12664 || (h->dynamic
12665 && d != NULL
12666 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12667 && (h->versioned >= versioned
54e8959c
L
12668 || !bfd_hide_sym_by_version (info->version_info,
12669 h->root.root.string)))))
715df9b8
EB
12670 h->root.u.def.section->flags |= SEC_KEEP;
12671
12672 return TRUE;
12673}
3b36f7e6 12674
74f0fb50
AM
12675/* Keep all sections containing symbols undefined on the command-line,
12676 and the section containing the entry symbol. */
12677
12678void
12679_bfd_elf_gc_keep (struct bfd_link_info *info)
12680{
12681 struct bfd_sym_chain *sym;
12682
12683 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12684 {
12685 struct elf_link_hash_entry *h;
12686
12687 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12688 FALSE, FALSE, FALSE);
12689
12690 if (h != NULL
12691 && (h->root.type == bfd_link_hash_defined
12692 || h->root.type == bfd_link_hash_defweak)
12693 && !bfd_is_abs_section (h->root.u.def.section))
12694 h->root.u.def.section->flags |= SEC_KEEP;
12695 }
12696}
12697
2f0c68f2
CM
12698bfd_boolean
12699bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12700 struct bfd_link_info *info)
12701{
12702 bfd *ibfd = info->input_bfds;
12703
12704 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12705 {
12706 asection *sec;
12707 struct elf_reloc_cookie cookie;
12708
12709 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12710 continue;
12711
12712 if (!init_reloc_cookie (&cookie, info, ibfd))
12713 return FALSE;
12714
12715 for (sec = ibfd->sections; sec; sec = sec->next)
12716 {
12717 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12718 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12719 {
12720 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12721 fini_reloc_cookie_rels (&cookie, sec);
12722 }
12723 }
12724 }
12725 return TRUE;
12726}
12727
c152c796
AM
12728/* Do mark and sweep of unused sections. */
12729
12730bfd_boolean
12731bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12732{
12733 bfd_boolean ok = TRUE;
12734 bfd *sub;
6a5bb875 12735 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12736 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12737 struct elf_link_hash_table *htab;
c152c796 12738
64d03ab5 12739 if (!bed->can_gc_sections
715df9b8 12740 || !is_elf_hash_table (info->hash))
c152c796
AM
12741 {
12742 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12743 return TRUE;
12744 }
12745
74f0fb50 12746 bed->gc_keep (info);
da44f4e5 12747 htab = elf_hash_table (info);
74f0fb50 12748
9d0a14d3
RS
12749 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12750 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12751 for (sub = info->input_bfds;
12752 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12753 sub = sub->link.next)
9d0a14d3
RS
12754 {
12755 asection *sec;
12756 struct elf_reloc_cookie cookie;
12757
12758 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12759 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12760 {
12761 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12762 if (elf_section_data (sec)->sec_info
12763 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12764 elf_eh_frame_section (sub) = sec;
12765 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12766 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12767 }
12768 }
9d0a14d3 12769
c152c796 12770 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12771 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12772 if (!ok)
12773 return FALSE;
12774
12775 /* Kill the vtable relocations that were not used. */
da44f4e5 12776 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12777 if (!ok)
12778 return FALSE;
12779
715df9b8 12780 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12781 if (htab->dynamic_sections_created)
12782 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 12783
715df9b8 12784 /* Grovel through relocs to find out who stays ... */
64d03ab5 12785 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 12786 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12787 {
12788 asection *o;
12789
b19a8f85
L
12790 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12791 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12792 continue;
12793
7f6ab9f8
AM
12794 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12795 Also treat note sections as a root, if the section is not part
12796 of a group. */
c152c796 12797 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12798 if (!o->gc_mark
12799 && (o->flags & SEC_EXCLUDE) == 0
24007750 12800 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12801 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12802 && elf_next_in_group (o) == NULL )))
12803 {
12804 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12805 return FALSE;
12806 }
c152c796
AM
12807 }
12808
6a5bb875 12809 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12810 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12811
c152c796 12812 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12813 return elf_gc_sweep (abfd, info);
c152c796
AM
12814}
12815\f
12816/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12817
12818bfd_boolean
12819bfd_elf_gc_record_vtinherit (bfd *abfd,
12820 asection *sec,
12821 struct elf_link_hash_entry *h,
12822 bfd_vma offset)
12823{
12824 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12825 struct elf_link_hash_entry **search, *child;
12826 bfd_size_type extsymcount;
12827 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12828
12829 /* The sh_info field of the symtab header tells us where the
12830 external symbols start. We don't care about the local symbols at
12831 this point. */
12832 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12833 if (!elf_bad_symtab (abfd))
12834 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12835
12836 sym_hashes = elf_sym_hashes (abfd);
12837 sym_hashes_end = sym_hashes + extsymcount;
12838
12839 /* Hunt down the child symbol, which is in this section at the same
12840 offset as the relocation. */
12841 for (search = sym_hashes; search != sym_hashes_end; ++search)
12842 {
12843 if ((child = *search) != NULL
12844 && (child->root.type == bfd_link_hash_defined
12845 || child->root.type == bfd_link_hash_defweak)
12846 && child->root.u.def.section == sec
12847 && child->root.u.def.value == offset)
12848 goto win;
12849 }
12850
d003868e
AM
12851 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12852 abfd, sec, (unsigned long) offset);
c152c796
AM
12853 bfd_set_error (bfd_error_invalid_operation);
12854 return FALSE;
12855
12856 win:
f6e332e6
AM
12857 if (!child->vtable)
12858 {
ca4be51c
AM
12859 child->vtable = ((struct elf_link_virtual_table_entry *)
12860 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
12861 if (!child->vtable)
12862 return FALSE;
12863 }
c152c796
AM
12864 if (!h)
12865 {
12866 /* This *should* only be the absolute section. It could potentially
12867 be that someone has defined a non-global vtable though, which
12868 would be bad. It isn't worth paging in the local symbols to be
12869 sure though; that case should simply be handled by the assembler. */
12870
f6e332e6 12871 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12872 }
12873 else
f6e332e6 12874 child->vtable->parent = h;
c152c796
AM
12875
12876 return TRUE;
12877}
12878
12879/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12880
12881bfd_boolean
12882bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12883 asection *sec ATTRIBUTE_UNUSED,
12884 struct elf_link_hash_entry *h,
12885 bfd_vma addend)
12886{
12887 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12888 unsigned int log_file_align = bed->s->log_file_align;
12889
f6e332e6
AM
12890 if (!h->vtable)
12891 {
ca4be51c
AM
12892 h->vtable = ((struct elf_link_virtual_table_entry *)
12893 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
12894 if (!h->vtable)
12895 return FALSE;
12896 }
12897
12898 if (addend >= h->vtable->size)
c152c796
AM
12899 {
12900 size_t size, bytes, file_align;
f6e332e6 12901 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12902
12903 /* While the symbol is undefined, we have to be prepared to handle
12904 a zero size. */
12905 file_align = 1 << log_file_align;
12906 if (h->root.type == bfd_link_hash_undefined)
12907 size = addend + file_align;
12908 else
12909 {
12910 size = h->size;
12911 if (addend >= size)
12912 {
12913 /* Oops! We've got a reference past the defined end of
12914 the table. This is probably a bug -- shall we warn? */
12915 size = addend + file_align;
12916 }
12917 }
12918 size = (size + file_align - 1) & -file_align;
12919
12920 /* Allocate one extra entry for use as a "done" flag for the
12921 consolidation pass. */
12922 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12923
12924 if (ptr)
12925 {
a50b1753 12926 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12927
12928 if (ptr != NULL)
12929 {
12930 size_t oldbytes;
12931
f6e332e6 12932 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12933 * sizeof (bfd_boolean));
12934 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12935 }
12936 }
12937 else
a50b1753 12938 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12939
12940 if (ptr == NULL)
12941 return FALSE;
12942
12943 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12944 h->vtable->used = ptr + 1;
12945 h->vtable->size = size;
c152c796
AM
12946 }
12947
f6e332e6 12948 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12949
12950 return TRUE;
12951}
12952
ae17ab41
CM
12953/* Map an ELF section header flag to its corresponding string. */
12954typedef struct
12955{
12956 char *flag_name;
12957 flagword flag_value;
12958} elf_flags_to_name_table;
12959
12960static elf_flags_to_name_table elf_flags_to_names [] =
12961{
12962 { "SHF_WRITE", SHF_WRITE },
12963 { "SHF_ALLOC", SHF_ALLOC },
12964 { "SHF_EXECINSTR", SHF_EXECINSTR },
12965 { "SHF_MERGE", SHF_MERGE },
12966 { "SHF_STRINGS", SHF_STRINGS },
12967 { "SHF_INFO_LINK", SHF_INFO_LINK},
12968 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12969 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12970 { "SHF_GROUP", SHF_GROUP },
12971 { "SHF_TLS", SHF_TLS },
12972 { "SHF_MASKOS", SHF_MASKOS },
12973 { "SHF_EXCLUDE", SHF_EXCLUDE },
12974};
12975
b9c361e0
JL
12976/* Returns TRUE if the section is to be included, otherwise FALSE. */
12977bfd_boolean
ae17ab41 12978bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 12979 struct flag_info *flaginfo,
b9c361e0 12980 asection *section)
ae17ab41 12981{
8b127cbc 12982 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 12983
8b127cbc 12984 if (!flaginfo->flags_initialized)
ae17ab41 12985 {
8b127cbc
AM
12986 bfd *obfd = info->output_bfd;
12987 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12988 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
12989 int with_hex = 0;
12990 int without_hex = 0;
12991
8b127cbc 12992 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 12993 {
b9c361e0 12994 unsigned i;
8b127cbc 12995 flagword (*lookup) (char *);
ae17ab41 12996
8b127cbc
AM
12997 lookup = bed->elf_backend_lookup_section_flags_hook;
12998 if (lookup != NULL)
ae17ab41 12999 {
8b127cbc 13000 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13001
13002 if (hexval != 0)
13003 {
13004 if (tf->with == with_flags)
13005 with_hex |= hexval;
13006 else if (tf->with == without_flags)
13007 without_hex |= hexval;
13008 tf->valid = TRUE;
13009 continue;
13010 }
ae17ab41 13011 }
8b127cbc 13012 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13013 {
8b127cbc 13014 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13015 {
13016 if (tf->with == with_flags)
13017 with_hex |= elf_flags_to_names[i].flag_value;
13018 else if (tf->with == without_flags)
13019 without_hex |= elf_flags_to_names[i].flag_value;
13020 tf->valid = TRUE;
13021 break;
13022 }
13023 }
8b127cbc 13024 if (!tf->valid)
b9c361e0 13025 {
68ffbac6 13026 info->callbacks->einfo
8b127cbc 13027 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13028 return FALSE;
ae17ab41
CM
13029 }
13030 }
8b127cbc
AM
13031 flaginfo->flags_initialized = TRUE;
13032 flaginfo->only_with_flags |= with_hex;
13033 flaginfo->not_with_flags |= without_hex;
ae17ab41 13034 }
ae17ab41 13035
8b127cbc 13036 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13037 return FALSE;
13038
8b127cbc 13039 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13040 return FALSE;
13041
13042 return TRUE;
ae17ab41
CM
13043}
13044
c152c796
AM
13045struct alloc_got_off_arg {
13046 bfd_vma gotoff;
10455f89 13047 struct bfd_link_info *info;
c152c796
AM
13048};
13049
13050/* We need a special top-level link routine to convert got reference counts
13051 to real got offsets. */
13052
13053static bfd_boolean
13054elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13055{
a50b1753 13056 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13057 bfd *obfd = gofarg->info->output_bfd;
13058 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13059
c152c796
AM
13060 if (h->got.refcount > 0)
13061 {
13062 h->got.offset = gofarg->gotoff;
10455f89 13063 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13064 }
13065 else
13066 h->got.offset = (bfd_vma) -1;
13067
13068 return TRUE;
13069}
13070
13071/* And an accompanying bit to work out final got entry offsets once
13072 we're done. Should be called from final_link. */
13073
13074bfd_boolean
13075bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13076 struct bfd_link_info *info)
13077{
13078 bfd *i;
13079 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13080 bfd_vma gotoff;
c152c796
AM
13081 struct alloc_got_off_arg gofarg;
13082
10455f89
HPN
13083 BFD_ASSERT (abfd == info->output_bfd);
13084
c152c796
AM
13085 if (! is_elf_hash_table (info->hash))
13086 return FALSE;
13087
13088 /* The GOT offset is relative to the .got section, but the GOT header is
13089 put into the .got.plt section, if the backend uses it. */
13090 if (bed->want_got_plt)
13091 gotoff = 0;
13092 else
13093 gotoff = bed->got_header_size;
13094
13095 /* Do the local .got entries first. */
c72f2fb2 13096 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13097 {
13098 bfd_signed_vma *local_got;
13099 bfd_size_type j, locsymcount;
13100 Elf_Internal_Shdr *symtab_hdr;
13101
13102 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13103 continue;
13104
13105 local_got = elf_local_got_refcounts (i);
13106 if (!local_got)
13107 continue;
13108
13109 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13110 if (elf_bad_symtab (i))
13111 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13112 else
13113 locsymcount = symtab_hdr->sh_info;
13114
13115 for (j = 0; j < locsymcount; ++j)
13116 {
13117 if (local_got[j] > 0)
13118 {
13119 local_got[j] = gotoff;
10455f89 13120 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13121 }
13122 else
13123 local_got[j] = (bfd_vma) -1;
13124 }
13125 }
13126
13127 /* Then the global .got entries. .plt refcounts are handled by
13128 adjust_dynamic_symbol */
13129 gofarg.gotoff = gotoff;
10455f89 13130 gofarg.info = info;
c152c796
AM
13131 elf_link_hash_traverse (elf_hash_table (info),
13132 elf_gc_allocate_got_offsets,
13133 &gofarg);
13134 return TRUE;
13135}
13136
13137/* Many folk need no more in the way of final link than this, once
13138 got entry reference counting is enabled. */
13139
13140bfd_boolean
13141bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13142{
13143 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13144 return FALSE;
13145
13146 /* Invoke the regular ELF backend linker to do all the work. */
13147 return bfd_elf_final_link (abfd, info);
13148}
13149
13150bfd_boolean
13151bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13152{
a50b1753 13153 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13154
13155 if (rcookie->bad_symtab)
13156 rcookie->rel = rcookie->rels;
13157
13158 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13159 {
13160 unsigned long r_symndx;
13161
13162 if (! rcookie->bad_symtab)
13163 if (rcookie->rel->r_offset > offset)
13164 return FALSE;
13165 if (rcookie->rel->r_offset != offset)
13166 continue;
13167
13168 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13169 if (r_symndx == STN_UNDEF)
c152c796
AM
13170 return TRUE;
13171
13172 if (r_symndx >= rcookie->locsymcount
13173 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13174 {
13175 struct elf_link_hash_entry *h;
13176
13177 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13178
13179 while (h->root.type == bfd_link_hash_indirect
13180 || h->root.type == bfd_link_hash_warning)
13181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13182
13183 if ((h->root.type == bfd_link_hash_defined
13184 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13185 && (h->root.u.def.section->owner != rcookie->abfd
13186 || h->root.u.def.section->kept_section != NULL
13187 || discarded_section (h->root.u.def.section)))
c152c796 13188 return TRUE;
c152c796
AM
13189 }
13190 else
13191 {
13192 /* It's not a relocation against a global symbol,
13193 but it could be a relocation against a local
13194 symbol for a discarded section. */
13195 asection *isec;
13196 Elf_Internal_Sym *isym;
13197
13198 /* Need to: get the symbol; get the section. */
13199 isym = &rcookie->locsyms[r_symndx];
cb33740c 13200 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13201 if (isec != NULL
13202 && (isec->kept_section != NULL
13203 || discarded_section (isec)))
cb33740c 13204 return TRUE;
c152c796
AM
13205 }
13206 return FALSE;
13207 }
13208 return FALSE;
13209}
13210
13211/* Discard unneeded references to discarded sections.
75938853
AM
13212 Returns -1 on error, 1 if any section's size was changed, 0 if
13213 nothing changed. This function assumes that the relocations are in
13214 sorted order, which is true for all known assemblers. */
c152c796 13215
75938853 13216int
c152c796
AM
13217bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13218{
13219 struct elf_reloc_cookie cookie;
18cd5bce 13220 asection *o;
c152c796 13221 bfd *abfd;
75938853 13222 int changed = 0;
c152c796
AM
13223
13224 if (info->traditional_format
13225 || !is_elf_hash_table (info->hash))
75938853 13226 return 0;
c152c796 13227
18cd5bce
AM
13228 o = bfd_get_section_by_name (output_bfd, ".stab");
13229 if (o != NULL)
c152c796 13230 {
18cd5bce 13231 asection *i;
c152c796 13232
18cd5bce 13233 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13234 {
18cd5bce
AM
13235 if (i->size == 0
13236 || i->reloc_count == 0
13237 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13238 continue;
c152c796 13239
18cd5bce
AM
13240 abfd = i->owner;
13241 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13242 continue;
c152c796 13243
18cd5bce 13244 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13245 return -1;
c152c796 13246
18cd5bce
AM
13247 if (_bfd_discard_section_stabs (abfd, i,
13248 elf_section_data (i)->sec_info,
5241d853
RS
13249 bfd_elf_reloc_symbol_deleted_p,
13250 &cookie))
75938853 13251 changed = 1;
18cd5bce
AM
13252
13253 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13254 }
18cd5bce
AM
13255 }
13256
2f0c68f2
CM
13257 o = NULL;
13258 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13259 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13260 if (o != NULL)
13261 {
13262 asection *i;
c152c796 13263
18cd5bce 13264 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13265 {
18cd5bce
AM
13266 if (i->size == 0)
13267 continue;
13268
13269 abfd = i->owner;
13270 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13271 continue;
13272
13273 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13274 return -1;
18cd5bce
AM
13275
13276 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13277 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13278 bfd_elf_reloc_symbol_deleted_p,
13279 &cookie))
75938853 13280 changed = 1;
18cd5bce
AM
13281
13282 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13283 }
18cd5bce 13284 }
c152c796 13285
18cd5bce
AM
13286 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13287 {
13288 const struct elf_backend_data *bed;
c152c796 13289
18cd5bce
AM
13290 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13291 continue;
13292
13293 bed = get_elf_backend_data (abfd);
13294
13295 if (bed->elf_backend_discard_info != NULL)
13296 {
13297 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13298 return -1;
18cd5bce
AM
13299
13300 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13301 changed = 1;
18cd5bce
AM
13302
13303 fini_reloc_cookie (&cookie, abfd);
13304 }
c152c796
AM
13305 }
13306
2f0c68f2
CM
13307 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13308 _bfd_elf_end_eh_frame_parsing (info);
13309
13310 if (info->eh_frame_hdr_type
0e1862bb 13311 && !bfd_link_relocatable (info)
c152c796 13312 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13313 changed = 1;
c152c796 13314
75938853 13315 return changed;
c152c796 13316}
082b7297 13317
43e1669b 13318bfd_boolean
0c511000 13319_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13320 asection *sec,
c0f00686 13321 struct bfd_link_info *info)
082b7297
L
13322{
13323 flagword flags;
c77ec726 13324 const char *name, *key;
082b7297
L
13325 struct bfd_section_already_linked *l;
13326 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13327
c77ec726
AM
13328 if (sec->output_section == bfd_abs_section_ptr)
13329 return FALSE;
0c511000 13330
c77ec726 13331 flags = sec->flags;
0c511000 13332
c77ec726
AM
13333 /* Return if it isn't a linkonce section. A comdat group section
13334 also has SEC_LINK_ONCE set. */
13335 if ((flags & SEC_LINK_ONCE) == 0)
13336 return FALSE;
0c511000 13337
c77ec726
AM
13338 /* Don't put group member sections on our list of already linked
13339 sections. They are handled as a group via their group section. */
13340 if (elf_sec_group (sec) != NULL)
13341 return FALSE;
0c511000 13342
c77ec726
AM
13343 /* For a SHT_GROUP section, use the group signature as the key. */
13344 name = sec->name;
13345 if ((flags & SEC_GROUP) != 0
13346 && elf_next_in_group (sec) != NULL
13347 && elf_group_name (elf_next_in_group (sec)) != NULL)
13348 key = elf_group_name (elf_next_in_group (sec));
13349 else
13350 {
13351 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13352 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13353 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13354 key++;
0c511000 13355 else
c77ec726
AM
13356 /* Must be a user linkonce section that doesn't follow gcc's
13357 naming convention. In this case we won't be matching
13358 single member groups. */
13359 key = name;
0c511000 13360 }
6d2cd210 13361
c77ec726 13362 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13363
13364 for (l = already_linked_list->entry; l != NULL; l = l->next)
13365 {
c2370991 13366 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13367 sections with a signature of <key> (<key> is some string),
13368 and linkonce sections named .gnu.linkonce.<type>.<key>.
13369 Match like sections. LTO plugin sections are an exception.
13370 They are always named .gnu.linkonce.t.<key> and match either
13371 type of section. */
13372 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13373 && ((flags & SEC_GROUP) != 0
13374 || strcmp (name, l->sec->name) == 0))
13375 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13376 {
13377 /* The section has already been linked. See if we should
6d2cd210 13378 issue a warning. */
c77ec726
AM
13379 if (!_bfd_handle_already_linked (sec, l, info))
13380 return FALSE;
082b7297 13381
c77ec726 13382 if (flags & SEC_GROUP)
3d7f7666 13383 {
c77ec726
AM
13384 asection *first = elf_next_in_group (sec);
13385 asection *s = first;
3d7f7666 13386
c77ec726 13387 while (s != NULL)
3d7f7666 13388 {
c77ec726
AM
13389 s->output_section = bfd_abs_section_ptr;
13390 /* Record which group discards it. */
13391 s->kept_section = l->sec;
13392 s = elf_next_in_group (s);
13393 /* These lists are circular. */
13394 if (s == first)
13395 break;
3d7f7666
L
13396 }
13397 }
082b7297 13398
43e1669b 13399 return TRUE;
082b7297
L
13400 }
13401 }
13402
c77ec726
AM
13403 /* A single member comdat group section may be discarded by a
13404 linkonce section and vice versa. */
13405 if ((flags & SEC_GROUP) != 0)
3d7f7666 13406 {
c77ec726 13407 asection *first = elf_next_in_group (sec);
c2370991 13408
c77ec726
AM
13409 if (first != NULL && elf_next_in_group (first) == first)
13410 /* Check this single member group against linkonce sections. */
13411 for (l = already_linked_list->entry; l != NULL; l = l->next)
13412 if ((l->sec->flags & SEC_GROUP) == 0
13413 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13414 {
13415 first->output_section = bfd_abs_section_ptr;
13416 first->kept_section = l->sec;
13417 sec->output_section = bfd_abs_section_ptr;
13418 break;
13419 }
13420 }
13421 else
13422 /* Check this linkonce section against single member groups. */
13423 for (l = already_linked_list->entry; l != NULL; l = l->next)
13424 if (l->sec->flags & SEC_GROUP)
6d2cd210 13425 {
c77ec726 13426 asection *first = elf_next_in_group (l->sec);
6d2cd210 13427
c77ec726
AM
13428 if (first != NULL
13429 && elf_next_in_group (first) == first
13430 && bfd_elf_match_symbols_in_sections (first, sec, info))
13431 {
13432 sec->output_section = bfd_abs_section_ptr;
13433 sec->kept_section = first;
13434 break;
13435 }
6d2cd210 13436 }
0c511000 13437
c77ec726
AM
13438 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13439 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13440 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13441 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13442 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13443 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13444 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13445 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13446 The reverse order cannot happen as there is never a bfd with only the
13447 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13448 matter as here were are looking only for cross-bfd sections. */
13449
13450 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13451 for (l = already_linked_list->entry; l != NULL; l = l->next)
13452 if ((l->sec->flags & SEC_GROUP) == 0
13453 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13454 {
13455 if (abfd != l->sec->owner)
13456 sec->output_section = bfd_abs_section_ptr;
13457 break;
13458 }
80c29487 13459
082b7297 13460 /* This is the first section with this name. Record it. */
c77ec726 13461 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13462 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13463 return sec->output_section == bfd_abs_section_ptr;
082b7297 13464}
81e1b023 13465
a4d8e49b
L
13466bfd_boolean
13467_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13468{
13469 return sym->st_shndx == SHN_COMMON;
13470}
13471
13472unsigned int
13473_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13474{
13475 return SHN_COMMON;
13476}
13477
13478asection *
13479_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13480{
13481 return bfd_com_section_ptr;
13482}
10455f89
HPN
13483
13484bfd_vma
13485_bfd_elf_default_got_elt_size (bfd *abfd,
13486 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13487 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13488 bfd *ibfd ATTRIBUTE_UNUSED,
13489 unsigned long symndx ATTRIBUTE_UNUSED)
13490{
13491 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13492 return bed->s->arch_size / 8;
13493}
83bac4b0
NC
13494
13495/* Routines to support the creation of dynamic relocs. */
13496
83bac4b0
NC
13497/* Returns the name of the dynamic reloc section associated with SEC. */
13498
13499static const char *
13500get_dynamic_reloc_section_name (bfd * abfd,
13501 asection * sec,
13502 bfd_boolean is_rela)
13503{
ddcf1fcf
BS
13504 char *name;
13505 const char *old_name = bfd_get_section_name (NULL, sec);
13506 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13507
ddcf1fcf 13508 if (old_name == NULL)
83bac4b0
NC
13509 return NULL;
13510
ddcf1fcf 13511 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13512 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13513
13514 return name;
13515}
13516
13517/* Returns the dynamic reloc section associated with SEC.
13518 If necessary compute the name of the dynamic reloc section based
13519 on SEC's name (looked up in ABFD's string table) and the setting
13520 of IS_RELA. */
13521
13522asection *
13523_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13524 asection * sec,
13525 bfd_boolean is_rela)
13526{
13527 asection * reloc_sec = elf_section_data (sec)->sreloc;
13528
13529 if (reloc_sec == NULL)
13530 {
13531 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13532
13533 if (name != NULL)
13534 {
3d4d4302 13535 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13536
13537 if (reloc_sec != NULL)
13538 elf_section_data (sec)->sreloc = reloc_sec;
13539 }
13540 }
13541
13542 return reloc_sec;
13543}
13544
13545/* Returns the dynamic reloc section associated with SEC. If the
13546 section does not exist it is created and attached to the DYNOBJ
13547 bfd and stored in the SRELOC field of SEC's elf_section_data
13548 structure.
f8076f98 13549
83bac4b0
NC
13550 ALIGNMENT is the alignment for the newly created section and
13551 IS_RELA defines whether the name should be .rela.<SEC's name>
13552 or .rel.<SEC's name>. The section name is looked up in the
13553 string table associated with ABFD. */
13554
13555asection *
ca4be51c
AM
13556_bfd_elf_make_dynamic_reloc_section (asection *sec,
13557 bfd *dynobj,
13558 unsigned int alignment,
13559 bfd *abfd,
13560 bfd_boolean is_rela)
83bac4b0
NC
13561{
13562 asection * reloc_sec = elf_section_data (sec)->sreloc;
13563
13564 if (reloc_sec == NULL)
13565 {
13566 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13567
13568 if (name == NULL)
13569 return NULL;
13570
3d4d4302 13571 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13572
13573 if (reloc_sec == NULL)
13574 {
3d4d4302
AM
13575 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13576 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13577 if ((sec->flags & SEC_ALLOC) != 0)
13578 flags |= SEC_ALLOC | SEC_LOAD;
13579
3d4d4302 13580 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13581 if (reloc_sec != NULL)
13582 {
8877b5e5
AM
13583 /* _bfd_elf_get_sec_type_attr chooses a section type by
13584 name. Override as it may be wrong, eg. for a user
13585 section named "auto" we'll get ".relauto" which is
13586 seen to be a .rela section. */
13587 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13588 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13589 reloc_sec = NULL;
13590 }
13591 }
13592
13593 elf_section_data (sec)->sreloc = reloc_sec;
13594 }
13595
13596 return reloc_sec;
13597}
1338dd10 13598
bffebb6b
AM
13599/* Copy the ELF symbol type and other attributes for a linker script
13600 assignment from HSRC to HDEST. Generally this should be treated as
13601 if we found a strong non-dynamic definition for HDEST (except that
13602 ld ignores multiple definition errors). */
1338dd10 13603void
bffebb6b
AM
13604_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13605 struct bfd_link_hash_entry *hdest,
13606 struct bfd_link_hash_entry *hsrc)
1338dd10 13607{
bffebb6b
AM
13608 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13609 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13610 Elf_Internal_Sym isym;
1338dd10
PB
13611
13612 ehdest->type = ehsrc->type;
35fc36a8 13613 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13614
13615 isym.st_other = ehsrc->other;
b8417128 13616 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13617}
351f65ca
L
13618
13619/* Append a RELA relocation REL to section S in BFD. */
13620
13621void
13622elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13623{
13624 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13625 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13626 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13627 bed->s->swap_reloca_out (abfd, rel, loc);
13628}
13629
13630/* Append a REL relocation REL to section S in BFD. */
13631
13632void
13633elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13634{
13635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13636 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13637 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13638 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13639}
This page took 2.280915 seconds and 4 git commands to generate.