[x86] Resolve non-PIC undefweak symbols in executable
[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
0f550b3d
L
558 if (h->versioned == unknown)
559 {
560 /* Set versioned if symbol version is unknown. */
561 char *version = strrchr (name, ELF_VER_CHR);
562 if (version)
563 {
564 if (version > name && version[-1] != ELF_VER_CHR)
565 h->versioned = versioned_hidden;
566 else
567 h->versioned = versioned;
568 }
569 }
570
00cbee0a 571 switch (h->root.type)
77cfaee6 572 {
00cbee0a
L
573 case bfd_link_hash_defined:
574 case bfd_link_hash_defweak:
575 case bfd_link_hash_common:
576 break;
577 case bfd_link_hash_undefweak:
578 case bfd_link_hash_undefined:
579 /* Since we're defining the symbol, don't let it seem to have not
580 been defined. record_dynamic_symbol and size_dynamic_sections
581 may depend on this. */
4ea42fb7 582 h->root.type = bfd_link_hash_new;
77cfaee6
AM
583 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
584 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
585 break;
586 case bfd_link_hash_new:
40b36307 587 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 588 h->non_elf = 0;
00cbee0a
L
589 break;
590 case bfd_link_hash_indirect:
591 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 592 the versioned symbol point to this one. */
00cbee0a
L
593 bed = get_elf_backend_data (output_bfd);
594 hv = h;
595 while (hv->root.type == bfd_link_hash_indirect
596 || hv->root.type == bfd_link_hash_warning)
597 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
598 /* We don't need to update h->root.u since linker will set them
599 later. */
600 h->root.type = bfd_link_hash_undefined;
601 hv->root.type = bfd_link_hash_indirect;
602 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
603 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
604 break;
605 case bfd_link_hash_warning:
606 abort ();
607 break;
55255dae 608 }
45d6a902
AM
609
610 /* If this symbol is being provided by the linker script, and it is
611 currently defined by a dynamic object, but not by a regular
612 object, then mark it as undefined so that the generic linker will
613 force the correct value. */
614 if (provide
f5385ebf
AM
615 && h->def_dynamic
616 && !h->def_regular)
45d6a902
AM
617 h->root.type = bfd_link_hash_undefined;
618
619 /* If this symbol is not being provided by the linker script, and it is
620 currently defined by a dynamic object, but not by a regular object,
621 then clear out any version information because the symbol will not be
622 associated with the dynamic object any more. */
623 if (!provide
f5385ebf
AM
624 && h->def_dynamic
625 && !h->def_regular)
45d6a902
AM
626 h->verinfo.verdef = NULL;
627
f5385ebf 628 h->def_regular = 1;
45d6a902 629
eb8476a6 630 if (hidden)
fe21a8fc 631 {
91d6fa6a 632 bed = get_elf_backend_data (output_bfd);
b8297068
AM
633 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
634 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
635 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
636 }
637
6fa3860b
PB
638 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
639 and executables. */
0e1862bb 640 if (!bfd_link_relocatable (info)
6fa3860b
PB
641 && h->dynindx != -1
642 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
643 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
644 h->forced_local = 1;
645
f5385ebf
AM
646 if ((h->def_dynamic
647 || h->ref_dynamic
6b3b0ab8
L
648 || bfd_link_dll (info)
649 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
650 && h->dynindx == -1)
651 {
c152c796 652 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
653 return FALSE;
654
655 /* If this is a weak defined symbol, and we know a corresponding
656 real symbol from the same dynamic object, make sure the real
657 symbol is also made into a dynamic symbol. */
f6e332e6
AM
658 if (h->u.weakdef != NULL
659 && h->u.weakdef->dynindx == -1)
45d6a902 660 {
f6e332e6 661 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
662 return FALSE;
663 }
664 }
665
666 return TRUE;
667}
42751cf3 668
8c58d23b
AM
669/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
670 success, and 2 on a failure caused by attempting to record a symbol
671 in a discarded section, eg. a discarded link-once section symbol. */
672
673int
c152c796
AM
674bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
675 bfd *input_bfd,
676 long input_indx)
8c58d23b
AM
677{
678 bfd_size_type amt;
679 struct elf_link_local_dynamic_entry *entry;
680 struct elf_link_hash_table *eht;
681 struct elf_strtab_hash *dynstr;
682 unsigned long dynstr_index;
683 char *name;
684 Elf_External_Sym_Shndx eshndx;
685 char esym[sizeof (Elf64_External_Sym)];
686
0eddce27 687 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
688 return 0;
689
690 /* See if the entry exists already. */
691 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
692 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
693 return 1;
694
695 amt = sizeof (*entry);
a50b1753 696 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
697 if (entry == NULL)
698 return 0;
699
700 /* Go find the symbol, so that we can find it's name. */
701 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 702 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
703 {
704 bfd_release (input_bfd, entry);
705 return 0;
706 }
707
708 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 709 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
710 {
711 asection *s;
712
713 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
714 if (s == NULL || bfd_is_abs_section (s->output_section))
715 {
716 /* We can still bfd_release here as nothing has done another
717 bfd_alloc. We can't do this later in this function. */
718 bfd_release (input_bfd, entry);
719 return 2;
720 }
721 }
722
723 name = (bfd_elf_string_from_elf_section
724 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
725 entry->isym.st_name));
726
727 dynstr = elf_hash_table (info)->dynstr;
728 if (dynstr == NULL)
729 {
730 /* Create a strtab to hold the dynamic symbol names. */
731 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
732 if (dynstr == NULL)
733 return 0;
734 }
735
b34976b6 736 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
737 if (dynstr_index == (unsigned long) -1)
738 return 0;
739 entry->isym.st_name = dynstr_index;
740
741 eht = elf_hash_table (info);
742
743 entry->next = eht->dynlocal;
744 eht->dynlocal = entry;
745 entry->input_bfd = input_bfd;
746 entry->input_indx = input_indx;
747 eht->dynsymcount++;
748
749 /* Whatever binding the symbol had before, it's now local. */
750 entry->isym.st_info
751 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
752
753 /* The dynindx will be set at the end of size_dynamic_sections. */
754
755 return 1;
756}
757
30b30c21 758/* Return the dynindex of a local dynamic symbol. */
42751cf3 759
30b30c21 760long
268b6b39
AM
761_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
762 bfd *input_bfd,
763 long input_indx)
30b30c21
RH
764{
765 struct elf_link_local_dynamic_entry *e;
766
767 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
768 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
769 return e->dynindx;
770 return -1;
771}
772
773/* This function is used to renumber the dynamic symbols, if some of
774 them are removed because they are marked as local. This is called
775 via elf_link_hash_traverse. */
776
b34976b6 777static bfd_boolean
268b6b39
AM
778elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
779 void *data)
42751cf3 780{
a50b1753 781 size_t *count = (size_t *) data;
30b30c21 782
6fa3860b
PB
783 if (h->forced_local)
784 return TRUE;
785
786 if (h->dynindx != -1)
787 h->dynindx = ++(*count);
788
789 return TRUE;
790}
791
792
793/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
794 STB_LOCAL binding. */
795
796static bfd_boolean
797elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
798 void *data)
799{
a50b1753 800 size_t *count = (size_t *) data;
6fa3860b 801
6fa3860b
PB
802 if (!h->forced_local)
803 return TRUE;
804
42751cf3 805 if (h->dynindx != -1)
30b30c21
RH
806 h->dynindx = ++(*count);
807
b34976b6 808 return TRUE;
42751cf3 809}
30b30c21 810
aee6f5b4
AO
811/* Return true if the dynamic symbol for a given section should be
812 omitted when creating a shared library. */
813bfd_boolean
814_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
815 struct bfd_link_info *info,
816 asection *p)
817{
74541ad4 818 struct elf_link_hash_table *htab;
ca55926c 819 asection *ip;
74541ad4 820
aee6f5b4
AO
821 switch (elf_section_data (p)->this_hdr.sh_type)
822 {
823 case SHT_PROGBITS:
824 case SHT_NOBITS:
825 /* If sh_type is yet undecided, assume it could be
826 SHT_PROGBITS/SHT_NOBITS. */
827 case SHT_NULL:
74541ad4
AM
828 htab = elf_hash_table (info);
829 if (p == htab->tls_sec)
830 return FALSE;
831
832 if (htab->text_index_section != NULL)
833 return p != htab->text_index_section && p != htab->data_index_section;
834
ca55926c 835 return (htab->dynobj != NULL
3d4d4302 836 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 837 && ip->output_section == p);
aee6f5b4
AO
838
839 /* There shouldn't be section relative relocations
840 against any other section. */
841 default:
842 return TRUE;
843 }
844}
845
062e2358 846/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
847 symbol for each output section, which come first. Next come symbols
848 which have been forced to local binding. Then all of the back-end
849 allocated local dynamic syms, followed by the rest of the global
850 symbols. */
30b30c21 851
554220db
AM
852static unsigned long
853_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
854 struct bfd_link_info *info,
855 unsigned long *section_sym_count)
30b30c21
RH
856{
857 unsigned long dynsymcount = 0;
858
0e1862bb
L
859 if (bfd_link_pic (info)
860 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 861 {
aee6f5b4 862 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
863 asection *p;
864 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 865 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
866 && (p->flags & SEC_ALLOC) != 0
867 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
868 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
869 else
870 elf_section_data (p)->dynindx = 0;
30b30c21 871 }
554220db 872 *section_sym_count = dynsymcount;
30b30c21 873
6fa3860b
PB
874 elf_link_hash_traverse (elf_hash_table (info),
875 elf_link_renumber_local_hash_table_dynsyms,
876 &dynsymcount);
877
30b30c21
RH
878 if (elf_hash_table (info)->dynlocal)
879 {
880 struct elf_link_local_dynamic_entry *p;
881 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
882 p->dynindx = ++dynsymcount;
883 }
884
885 elf_link_hash_traverse (elf_hash_table (info),
886 elf_link_renumber_hash_table_dynsyms,
887 &dynsymcount);
888
889 /* There is an unused NULL entry at the head of the table which
1a6e6083
L
890 we must account for in our count. We always create the dynsym
891 section, even if it is empty, with dynamic sections. */
892 if (elf_hash_table (info)->dynamic_sections_created)
30b30c21
RH
893 ++dynsymcount;
894
ccabcbe5
AM
895 elf_hash_table (info)->dynsymcount = dynsymcount;
896 return dynsymcount;
30b30c21 897}
252b5132 898
54ac0771
L
899/* Merge st_other field. */
900
901static void
902elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 903 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 904 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
905{
906 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
907
908 /* If st_other has a processor-specific meaning, specific
cd3416da 909 code might be needed here. */
54ac0771
L
910 if (bed->elf_backend_merge_symbol_attribute)
911 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
912 dynamic);
913
cd3416da 914 if (!dynamic)
54ac0771 915 {
cd3416da
AM
916 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
917 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 918
cd3416da
AM
919 /* Keep the most constraining visibility. Leave the remainder
920 of the st_other field to elf_backend_merge_symbol_attribute. */
921 if (symvis - 1 < hvis - 1)
922 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 923 }
b8417128
AM
924 else if (definition
925 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
926 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 927 h->protected_def = 1;
54ac0771
L
928}
929
4f3fedcf
AM
930/* This function is called when we want to merge a new symbol with an
931 existing symbol. It handles the various cases which arise when we
932 find a definition in a dynamic object, or when there is already a
933 definition in a dynamic object. The new symbol is described by
934 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
935 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
936 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
937 of an old common symbol. We set OVERRIDE if the old symbol is
938 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
939 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
940 to change. By OK to change, we mean that we shouldn't warn if the
941 type or size does change. */
45d6a902 942
8a56bd02 943static bfd_boolean
268b6b39
AM
944_bfd_elf_merge_symbol (bfd *abfd,
945 struct bfd_link_info *info,
946 const char *name,
947 Elf_Internal_Sym *sym,
948 asection **psec,
949 bfd_vma *pvalue,
4f3fedcf
AM
950 struct elf_link_hash_entry **sym_hash,
951 bfd **poldbfd,
37a9e49a 952 bfd_boolean *pold_weak,
af44c138 953 unsigned int *pold_alignment,
268b6b39
AM
954 bfd_boolean *skip,
955 bfd_boolean *override,
956 bfd_boolean *type_change_ok,
6e33951e
L
957 bfd_boolean *size_change_ok,
958 bfd_boolean *matched)
252b5132 959{
7479dfd4 960 asection *sec, *oldsec;
45d6a902 961 struct elf_link_hash_entry *h;
90c984fc 962 struct elf_link_hash_entry *hi;
45d6a902
AM
963 struct elf_link_hash_entry *flip;
964 int bind;
965 bfd *oldbfd;
966 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 967 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 968 const struct elf_backend_data *bed;
6e33951e 969 char *new_version;
45d6a902
AM
970
971 *skip = FALSE;
972 *override = FALSE;
973
974 sec = *psec;
975 bind = ELF_ST_BIND (sym->st_info);
976
977 if (! bfd_is_und_section (sec))
978 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
979 else
980 h = ((struct elf_link_hash_entry *)
981 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
982 if (h == NULL)
983 return FALSE;
984 *sym_hash = h;
252b5132 985
88ba32a0
L
986 bed = get_elf_backend_data (abfd);
987
6e33951e 988 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 989 if (h->versioned != unversioned)
6e33951e 990 {
422f1182
L
991 /* Symbol version is unknown or versioned. */
992 new_version = strrchr (name, ELF_VER_CHR);
993 if (new_version)
994 {
995 if (h->versioned == unknown)
996 {
997 if (new_version > name && new_version[-1] != ELF_VER_CHR)
998 h->versioned = versioned_hidden;
999 else
1000 h->versioned = versioned;
1001 }
1002 new_version += 1;
1003 if (new_version[0] == '\0')
1004 new_version = NULL;
1005 }
1006 else
1007 h->versioned = unversioned;
6e33951e 1008 }
422f1182
L
1009 else
1010 new_version = NULL;
6e33951e 1011
90c984fc
L
1012 /* For merging, we only care about real symbols. But we need to make
1013 sure that indirect symbol dynamic flags are updated. */
1014 hi = h;
45d6a902
AM
1015 while (h->root.type == bfd_link_hash_indirect
1016 || h->root.type == bfd_link_hash_warning)
1017 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1018
6e33951e
L
1019 if (!*matched)
1020 {
1021 if (hi == h || h->root.type == bfd_link_hash_new)
1022 *matched = TRUE;
1023 else
1024 {
ae7683d2 1025 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1026 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1027 true if the new symbol is only visible to the symbol with
6e33951e 1028 the same symbol version. */
422f1182
L
1029 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1030 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1031 if (!old_hidden && !new_hidden)
1032 /* The new symbol matches the existing symbol if both
1033 aren't hidden. */
1034 *matched = TRUE;
1035 else
1036 {
1037 /* OLD_VERSION is the symbol version of the existing
1038 symbol. */
422f1182
L
1039 char *old_version;
1040
1041 if (h->versioned >= versioned)
1042 old_version = strrchr (h->root.root.string,
1043 ELF_VER_CHR) + 1;
1044 else
1045 old_version = NULL;
6e33951e
L
1046
1047 /* The new symbol matches the existing symbol if they
1048 have the same symbol version. */
1049 *matched = (old_version == new_version
1050 || (old_version != NULL
1051 && new_version != NULL
1052 && strcmp (old_version, new_version) == 0));
1053 }
1054 }
1055 }
1056
934bce08
AM
1057 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1058 existing symbol. */
1059
1060 oldbfd = NULL;
1061 oldsec = NULL;
1062 switch (h->root.type)
1063 {
1064 default:
1065 break;
1066
1067 case bfd_link_hash_undefined:
1068 case bfd_link_hash_undefweak:
1069 oldbfd = h->root.u.undef.abfd;
1070 break;
1071
1072 case bfd_link_hash_defined:
1073 case bfd_link_hash_defweak:
1074 oldbfd = h->root.u.def.section->owner;
1075 oldsec = h->root.u.def.section;
1076 break;
1077
1078 case bfd_link_hash_common:
1079 oldbfd = h->root.u.c.p->section->owner;
1080 oldsec = h->root.u.c.p->section;
1081 if (pold_alignment)
1082 *pold_alignment = h->root.u.c.p->alignment_power;
1083 break;
1084 }
1085 if (poldbfd && *poldbfd == NULL)
1086 *poldbfd = oldbfd;
1087
1088 /* Differentiate strong and weak symbols. */
1089 newweak = bind == STB_WEAK;
1090 oldweak = (h->root.type == bfd_link_hash_defweak
1091 || h->root.type == bfd_link_hash_undefweak);
1092 if (pold_weak)
1093 *pold_weak = oldweak;
1094
1095 /* This code is for coping with dynamic objects, and is only useful
1096 if we are doing an ELF link. */
1097 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1098 return TRUE;
1099
40b36307 1100 /* We have to check it for every instance since the first few may be
ee659f1f 1101 references and not all compilers emit symbol type for undefined
40b36307
L
1102 symbols. */
1103 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1104
ee659f1f
AM
1105 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1106 respectively, is from a dynamic object. */
1107
1108 newdyn = (abfd->flags & DYNAMIC) != 0;
1109
1110 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1111 syms and defined syms in dynamic libraries respectively.
1112 ref_dynamic on the other hand can be set for a symbol defined in
1113 a dynamic library, and def_dynamic may not be set; When the
1114 definition in a dynamic lib is overridden by a definition in the
1115 executable use of the symbol in the dynamic lib becomes a
1116 reference to the executable symbol. */
1117 if (newdyn)
1118 {
1119 if (bfd_is_und_section (sec))
1120 {
1121 if (bind != STB_WEAK)
1122 {
1123 h->ref_dynamic_nonweak = 1;
1124 hi->ref_dynamic_nonweak = 1;
1125 }
1126 }
1127 else
1128 {
6e33951e
L
1129 /* Update the existing symbol only if they match. */
1130 if (*matched)
1131 h->dynamic_def = 1;
ee659f1f
AM
1132 hi->dynamic_def = 1;
1133 }
1134 }
1135
45d6a902
AM
1136 /* If we just created the symbol, mark it as being an ELF symbol.
1137 Other than that, there is nothing to do--there is no merge issue
1138 with a newly defined symbol--so we just return. */
1139
1140 if (h->root.type == bfd_link_hash_new)
252b5132 1141 {
f5385ebf 1142 h->non_elf = 0;
45d6a902
AM
1143 return TRUE;
1144 }
252b5132 1145
45d6a902
AM
1146 /* In cases involving weak versioned symbols, we may wind up trying
1147 to merge a symbol with itself. Catch that here, to avoid the
1148 confusion that results if we try to override a symbol with
1149 itself. The additional tests catch cases like
1150 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1151 dynamic object, which we do want to handle here. */
1152 if (abfd == oldbfd
895fa45f 1153 && (newweak || oldweak)
45d6a902 1154 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1155 || !h->def_regular))
45d6a902
AM
1156 return TRUE;
1157
707bba77 1158 olddyn = FALSE;
45d6a902
AM
1159 if (oldbfd != NULL)
1160 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1161 else if (oldsec != NULL)
45d6a902 1162 {
707bba77 1163 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1164 indices used by MIPS ELF. */
707bba77 1165 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1166 }
252b5132 1167
45d6a902
AM
1168 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1169 respectively, appear to be a definition rather than reference. */
1170
707bba77 1171 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1172
707bba77
AM
1173 olddef = (h->root.type != bfd_link_hash_undefined
1174 && h->root.type != bfd_link_hash_undefweak
1175 && h->root.type != bfd_link_hash_common);
45d6a902 1176
0a36a439
L
1177 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1178 respectively, appear to be a function. */
1179
1180 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1181 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1182
1183 oldfunc = (h->type != STT_NOTYPE
1184 && bed->is_function_type (h->type));
1185
580a2b6e
L
1186 /* When we try to create a default indirect symbol from the dynamic
1187 definition with the default version, we skip it if its type and
40101021 1188 the type of existing regular definition mismatch. */
580a2b6e 1189 if (pold_alignment == NULL
580a2b6e
L
1190 && newdyn
1191 && newdef
1192 && !olddyn
4584ec12
L
1193 && (((olddef || h->root.type == bfd_link_hash_common)
1194 && ELF_ST_TYPE (sym->st_info) != h->type
1195 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1196 && h->type != STT_NOTYPE
1197 && !(newfunc && oldfunc))
1198 || (olddef
1199 && ((h->type == STT_GNU_IFUNC)
1200 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1201 {
1202 *skip = TRUE;
1203 return TRUE;
1204 }
1205
4c34aff8
AM
1206 /* Check TLS symbols. We don't check undefined symbols introduced
1207 by "ld -u" which have no type (and oldbfd NULL), and we don't
1208 check symbols from plugins because they also have no type. */
1209 if (oldbfd != NULL
1210 && (oldbfd->flags & BFD_PLUGIN) == 0
1211 && (abfd->flags & BFD_PLUGIN) == 0
1212 && ELF_ST_TYPE (sym->st_info) != h->type
1213 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1214 {
1215 bfd *ntbfd, *tbfd;
1216 bfd_boolean ntdef, tdef;
1217 asection *ntsec, *tsec;
1218
1219 if (h->type == STT_TLS)
1220 {
3b36f7e6 1221 ntbfd = abfd;
7479dfd4
L
1222 ntsec = sec;
1223 ntdef = newdef;
1224 tbfd = oldbfd;
1225 tsec = oldsec;
1226 tdef = olddef;
1227 }
1228 else
1229 {
1230 ntbfd = oldbfd;
1231 ntsec = oldsec;
1232 ntdef = olddef;
1233 tbfd = abfd;
1234 tsec = sec;
1235 tdef = newdef;
1236 }
1237
1238 if (tdef && ntdef)
1239 (*_bfd_error_handler)
191c0c42
AM
1240 (_("%s: TLS definition in %B section %A "
1241 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1242 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1243 else if (!tdef && !ntdef)
1244 (*_bfd_error_handler)
191c0c42
AM
1245 (_("%s: TLS reference in %B "
1246 "mismatches non-TLS reference in %B"),
7479dfd4
L
1247 tbfd, ntbfd, h->root.root.string);
1248 else if (tdef)
1249 (*_bfd_error_handler)
191c0c42
AM
1250 (_("%s: TLS definition in %B section %A "
1251 "mismatches non-TLS reference in %B"),
7479dfd4
L
1252 tbfd, tsec, ntbfd, h->root.root.string);
1253 else
1254 (*_bfd_error_handler)
191c0c42
AM
1255 (_("%s: TLS reference in %B "
1256 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1257 tbfd, ntbfd, ntsec, h->root.root.string);
1258
1259 bfd_set_error (bfd_error_bad_value);
1260 return FALSE;
1261 }
1262
45d6a902
AM
1263 /* If the old symbol has non-default visibility, we ignore the new
1264 definition from a dynamic object. */
1265 if (newdyn
9c7a29a3 1266 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1267 && !bfd_is_und_section (sec))
1268 {
1269 *skip = TRUE;
1270 /* Make sure this symbol is dynamic. */
f5385ebf 1271 h->ref_dynamic = 1;
90c984fc 1272 hi->ref_dynamic = 1;
45d6a902
AM
1273 /* A protected symbol has external availability. Make sure it is
1274 recorded as dynamic.
1275
1276 FIXME: Should we check type and size for protected symbol? */
1277 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1278 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1279 else
1280 return TRUE;
1281 }
1282 else if (!newdyn
9c7a29a3 1283 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1284 && h->def_dynamic)
45d6a902
AM
1285 {
1286 /* If the new symbol with non-default visibility comes from a
1287 relocatable file and the old definition comes from a dynamic
1288 object, we remove the old definition. */
6c9b78e6 1289 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1290 {
1291 /* Handle the case where the old dynamic definition is
1292 default versioned. We need to copy the symbol info from
1293 the symbol with default version to the normal one if it
1294 was referenced before. */
1295 if (h->ref_regular)
1296 {
6c9b78e6 1297 hi->root.type = h->root.type;
d2dee3b2 1298 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1299 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1300
6c9b78e6 1301 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1302 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1303 {
aed81c4e
MR
1304 /* If the new symbol is hidden or internal, completely undo
1305 any dynamic link state. */
1306 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1307 h->forced_local = 0;
1308 h->ref_dynamic = 0;
d2dee3b2
L
1309 }
1310 else
aed81c4e
MR
1311 h->ref_dynamic = 1;
1312
1313 h->def_dynamic = 0;
aed81c4e
MR
1314 /* FIXME: Should we check type and size for protected symbol? */
1315 h->size = 0;
1316 h->type = 0;
1317
6c9b78e6 1318 h = hi;
d2dee3b2
L
1319 }
1320 else
6c9b78e6 1321 h = hi;
d2dee3b2 1322 }
1de1a317 1323
f5eda473
AM
1324 /* If the old symbol was undefined before, then it will still be
1325 on the undefs list. If the new symbol is undefined or
1326 common, we can't make it bfd_link_hash_new here, because new
1327 undefined or common symbols will be added to the undefs list
1328 by _bfd_generic_link_add_one_symbol. Symbols may not be
1329 added twice to the undefs list. Also, if the new symbol is
1330 undefweak then we don't want to lose the strong undef. */
1331 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1332 {
1de1a317 1333 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1334 h->root.u.undef.abfd = abfd;
1335 }
1336 else
1337 {
1338 h->root.type = bfd_link_hash_new;
1339 h->root.u.undef.abfd = NULL;
1340 }
1341
f5eda473 1342 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1343 {
f5eda473
AM
1344 /* If the new symbol is hidden or internal, completely undo
1345 any dynamic link state. */
1346 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1347 h->forced_local = 0;
1348 h->ref_dynamic = 0;
45d6a902 1349 }
f5eda473
AM
1350 else
1351 h->ref_dynamic = 1;
1352 h->def_dynamic = 0;
45d6a902
AM
1353 /* FIXME: Should we check type and size for protected symbol? */
1354 h->size = 0;
1355 h->type = 0;
1356 return TRUE;
1357 }
14a793b2 1358
15b43f48
AM
1359 /* If a new weak symbol definition comes from a regular file and the
1360 old symbol comes from a dynamic library, we treat the new one as
1361 strong. Similarly, an old weak symbol definition from a regular
1362 file is treated as strong when the new symbol comes from a dynamic
1363 library. Further, an old weak symbol from a dynamic library is
1364 treated as strong if the new symbol is from a dynamic library.
1365 This reflects the way glibc's ld.so works.
1366
1367 Do this before setting *type_change_ok or *size_change_ok so that
1368 we warn properly when dynamic library symbols are overridden. */
1369
1370 if (newdef && !newdyn && olddyn)
0f8a2703 1371 newweak = FALSE;
15b43f48 1372 if (olddef && newdyn)
0f8a2703
AM
1373 oldweak = FALSE;
1374
d334575b 1375 /* Allow changes between different types of function symbol. */
0a36a439 1376 if (newfunc && oldfunc)
fcb93ecf
PB
1377 *type_change_ok = TRUE;
1378
79349b09
AM
1379 /* It's OK to change the type if either the existing symbol or the
1380 new symbol is weak. A type change is also OK if the old symbol
1381 is undefined and the new symbol is defined. */
252b5132 1382
79349b09
AM
1383 if (oldweak
1384 || newweak
1385 || (newdef
1386 && h->root.type == bfd_link_hash_undefined))
1387 *type_change_ok = TRUE;
1388
1389 /* It's OK to change the size if either the existing symbol or the
1390 new symbol is weak, or if the old symbol is undefined. */
1391
1392 if (*type_change_ok
1393 || h->root.type == bfd_link_hash_undefined)
1394 *size_change_ok = TRUE;
45d6a902 1395
45d6a902
AM
1396 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1397 symbol, respectively, appears to be a common symbol in a dynamic
1398 object. If a symbol appears in an uninitialized section, and is
1399 not weak, and is not a function, then it may be a common symbol
1400 which was resolved when the dynamic object was created. We want
1401 to treat such symbols specially, because they raise special
1402 considerations when setting the symbol size: if the symbol
1403 appears as a common symbol in a regular object, and the size in
1404 the regular object is larger, we must make sure that we use the
1405 larger size. This problematic case can always be avoided in C,
1406 but it must be handled correctly when using Fortran shared
1407 libraries.
1408
1409 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1410 likewise for OLDDYNCOMMON and OLDDEF.
1411
1412 Note that this test is just a heuristic, and that it is quite
1413 possible to have an uninitialized symbol in a shared object which
1414 is really a definition, rather than a common symbol. This could
1415 lead to some minor confusion when the symbol really is a common
1416 symbol in some regular object. However, I think it will be
1417 harmless. */
1418
1419 if (newdyn
1420 && newdef
79349b09 1421 && !newweak
45d6a902
AM
1422 && (sec->flags & SEC_ALLOC) != 0
1423 && (sec->flags & SEC_LOAD) == 0
1424 && sym->st_size > 0
0a36a439 1425 && !newfunc)
45d6a902
AM
1426 newdyncommon = TRUE;
1427 else
1428 newdyncommon = FALSE;
1429
1430 if (olddyn
1431 && olddef
1432 && h->root.type == bfd_link_hash_defined
f5385ebf 1433 && h->def_dynamic
45d6a902
AM
1434 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1435 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1436 && h->size > 0
0a36a439 1437 && !oldfunc)
45d6a902
AM
1438 olddyncommon = TRUE;
1439 else
1440 olddyncommon = FALSE;
1441
a4d8e49b
L
1442 /* We now know everything about the old and new symbols. We ask the
1443 backend to check if we can merge them. */
5d13b3b3
AM
1444 if (bed->merge_symbol != NULL)
1445 {
1446 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1447 return FALSE;
1448 sec = *psec;
1449 }
a4d8e49b 1450
45d6a902
AM
1451 /* If both the old and the new symbols look like common symbols in a
1452 dynamic object, set the size of the symbol to the larger of the
1453 two. */
1454
1455 if (olddyncommon
1456 && newdyncommon
1457 && sym->st_size != h->size)
1458 {
1459 /* Since we think we have two common symbols, issue a multiple
1460 common warning if desired. Note that we only warn if the
1461 size is different. If the size is the same, we simply let
1462 the old symbol override the new one as normally happens with
1463 symbols defined in dynamic objects. */
1464
1465 if (! ((*info->callbacks->multiple_common)
24f58f47 1466 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1467 return FALSE;
252b5132 1468
45d6a902
AM
1469 if (sym->st_size > h->size)
1470 h->size = sym->st_size;
252b5132 1471
45d6a902 1472 *size_change_ok = TRUE;
252b5132
RH
1473 }
1474
45d6a902
AM
1475 /* If we are looking at a dynamic object, and we have found a
1476 definition, we need to see if the symbol was already defined by
1477 some other object. If so, we want to use the existing
1478 definition, and we do not want to report a multiple symbol
1479 definition error; we do this by clobbering *PSEC to be
1480 bfd_und_section_ptr.
1481
1482 We treat a common symbol as a definition if the symbol in the
1483 shared library is a function, since common symbols always
1484 represent variables; this can cause confusion in principle, but
1485 any such confusion would seem to indicate an erroneous program or
1486 shared library. We also permit a common symbol in a regular
79349b09 1487 object to override a weak symbol in a shared object. */
45d6a902
AM
1488
1489 if (newdyn
1490 && newdef
77cfaee6 1491 && (olddef
45d6a902 1492 || (h->root.type == bfd_link_hash_common
0a36a439 1493 && (newweak || newfunc))))
45d6a902
AM
1494 {
1495 *override = TRUE;
1496 newdef = FALSE;
1497 newdyncommon = FALSE;
252b5132 1498
45d6a902
AM
1499 *psec = sec = bfd_und_section_ptr;
1500 *size_change_ok = TRUE;
252b5132 1501
45d6a902
AM
1502 /* If we get here when the old symbol is a common symbol, then
1503 we are explicitly letting it override a weak symbol or
1504 function in a dynamic object, and we don't want to warn about
1505 a type change. If the old symbol is a defined symbol, a type
1506 change warning may still be appropriate. */
252b5132 1507
45d6a902
AM
1508 if (h->root.type == bfd_link_hash_common)
1509 *type_change_ok = TRUE;
1510 }
1511
1512 /* Handle the special case of an old common symbol merging with a
1513 new symbol which looks like a common symbol in a shared object.
1514 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1515 common symbol, and let _bfd_generic_link_add_one_symbol do the
1516 right thing. */
45d6a902
AM
1517
1518 if (newdyncommon
1519 && h->root.type == bfd_link_hash_common)
1520 {
1521 *override = TRUE;
1522 newdef = FALSE;
1523 newdyncommon = FALSE;
1524 *pvalue = sym->st_size;
a4d8e49b 1525 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1526 *size_change_ok = TRUE;
1527 }
1528
c5e2cead 1529 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1530 if (newdef && olddef && newweak)
54ac0771 1531 {
35ed3f94 1532 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1533 if (!(oldbfd != NULL
1534 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1535 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1536 {
1537 newdef = FALSE;
1538 *skip = TRUE;
1539 }
54ac0771
L
1540
1541 /* Merge st_other. If the symbol already has a dynamic index,
1542 but visibility says it should not be visible, turn it into a
1543 local symbol. */
b8417128 1544 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1545 if (h->dynindx != -1)
1546 switch (ELF_ST_VISIBILITY (h->other))
1547 {
1548 case STV_INTERNAL:
1549 case STV_HIDDEN:
1550 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1551 break;
1552 }
1553 }
c5e2cead 1554
45d6a902
AM
1555 /* If the old symbol is from a dynamic object, and the new symbol is
1556 a definition which is not from a dynamic object, then the new
1557 symbol overrides the old symbol. Symbols from regular files
1558 always take precedence over symbols from dynamic objects, even if
1559 they are defined after the dynamic object in the link.
1560
1561 As above, we again permit a common symbol in a regular object to
1562 override a definition in a shared object if the shared object
0f8a2703 1563 symbol is a function or is weak. */
45d6a902
AM
1564
1565 flip = NULL;
77cfaee6 1566 if (!newdyn
45d6a902
AM
1567 && (newdef
1568 || (bfd_is_com_section (sec)
0a36a439 1569 && (oldweak || oldfunc)))
45d6a902
AM
1570 && olddyn
1571 && olddef
f5385ebf 1572 && h->def_dynamic)
45d6a902
AM
1573 {
1574 /* Change the hash table entry to undefined, and let
1575 _bfd_generic_link_add_one_symbol do the right thing with the
1576 new definition. */
1577
1578 h->root.type = bfd_link_hash_undefined;
1579 h->root.u.undef.abfd = h->root.u.def.section->owner;
1580 *size_change_ok = TRUE;
1581
1582 olddef = FALSE;
1583 olddyncommon = FALSE;
1584
1585 /* We again permit a type change when a common symbol may be
1586 overriding a function. */
1587
1588 if (bfd_is_com_section (sec))
0a36a439
L
1589 {
1590 if (oldfunc)
1591 {
1592 /* If a common symbol overrides a function, make sure
1593 that it isn't defined dynamically nor has type
1594 function. */
1595 h->def_dynamic = 0;
1596 h->type = STT_NOTYPE;
1597 }
1598 *type_change_ok = TRUE;
1599 }
45d6a902 1600
6c9b78e6
AM
1601 if (hi->root.type == bfd_link_hash_indirect)
1602 flip = hi;
45d6a902
AM
1603 else
1604 /* This union may have been set to be non-NULL when this symbol
1605 was seen in a dynamic object. We must force the union to be
1606 NULL, so that it is correct for a regular symbol. */
1607 h->verinfo.vertree = NULL;
1608 }
1609
1610 /* Handle the special case of a new common symbol merging with an
1611 old symbol that looks like it might be a common symbol defined in
1612 a shared object. Note that we have already handled the case in
1613 which a new common symbol should simply override the definition
1614 in the shared library. */
1615
1616 if (! newdyn
1617 && bfd_is_com_section (sec)
1618 && olddyncommon)
1619 {
1620 /* It would be best if we could set the hash table entry to a
1621 common symbol, but we don't know what to use for the section
1622 or the alignment. */
1623 if (! ((*info->callbacks->multiple_common)
24f58f47 1624 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1625 return FALSE;
1626
4cc11e76 1627 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1628 larger, pretend that the new symbol has its size. */
1629
1630 if (h->size > *pvalue)
1631 *pvalue = h->size;
1632
af44c138
L
1633 /* We need to remember the alignment required by the symbol
1634 in the dynamic object. */
1635 BFD_ASSERT (pold_alignment);
1636 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1637
1638 olddef = FALSE;
1639 olddyncommon = FALSE;
1640
1641 h->root.type = bfd_link_hash_undefined;
1642 h->root.u.undef.abfd = h->root.u.def.section->owner;
1643
1644 *size_change_ok = TRUE;
1645 *type_change_ok = TRUE;
1646
6c9b78e6
AM
1647 if (hi->root.type == bfd_link_hash_indirect)
1648 flip = hi;
45d6a902
AM
1649 else
1650 h->verinfo.vertree = NULL;
1651 }
1652
1653 if (flip != NULL)
1654 {
1655 /* Handle the case where we had a versioned symbol in a dynamic
1656 library and now find a definition in a normal object. In this
1657 case, we make the versioned symbol point to the normal one. */
45d6a902 1658 flip->root.type = h->root.type;
00cbee0a 1659 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1660 h->root.type = bfd_link_hash_indirect;
1661 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1662 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1663 if (h->def_dynamic)
45d6a902 1664 {
f5385ebf
AM
1665 h->def_dynamic = 0;
1666 flip->ref_dynamic = 1;
45d6a902
AM
1667 }
1668 }
1669
45d6a902
AM
1670 return TRUE;
1671}
1672
1673/* This function is called to create an indirect symbol from the
1674 default for the symbol with the default version if needed. The
4f3fedcf 1675 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1676 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1677
28caa186 1678static bfd_boolean
268b6b39
AM
1679_bfd_elf_add_default_symbol (bfd *abfd,
1680 struct bfd_link_info *info,
1681 struct elf_link_hash_entry *h,
1682 const char *name,
1683 Elf_Internal_Sym *sym,
4f3fedcf
AM
1684 asection *sec,
1685 bfd_vma value,
1686 bfd **poldbfd,
e3c9d234 1687 bfd_boolean *dynsym)
45d6a902
AM
1688{
1689 bfd_boolean type_change_ok;
1690 bfd_boolean size_change_ok;
1691 bfd_boolean skip;
1692 char *shortname;
1693 struct elf_link_hash_entry *hi;
1694 struct bfd_link_hash_entry *bh;
9c5bfbb7 1695 const struct elf_backend_data *bed;
45d6a902
AM
1696 bfd_boolean collect;
1697 bfd_boolean dynamic;
e3c9d234 1698 bfd_boolean override;
45d6a902
AM
1699 char *p;
1700 size_t len, shortlen;
ffd65175 1701 asection *tmp_sec;
6e33951e 1702 bfd_boolean matched;
45d6a902 1703
422f1182
L
1704 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1705 return TRUE;
1706
45d6a902
AM
1707 /* If this symbol has a version, and it is the default version, we
1708 create an indirect symbol from the default name to the fully
1709 decorated name. This will cause external references which do not
1710 specify a version to be bound to this version of the symbol. */
1711 p = strchr (name, ELF_VER_CHR);
422f1182
L
1712 if (h->versioned == unknown)
1713 {
1714 if (p == NULL)
1715 {
1716 h->versioned = unversioned;
1717 return TRUE;
1718 }
1719 else
1720 {
1721 if (p[1] != ELF_VER_CHR)
1722 {
1723 h->versioned = versioned_hidden;
1724 return TRUE;
1725 }
1726 else
1727 h->versioned = versioned;
1728 }
1729 }
4373f8af
L
1730 else
1731 {
1732 /* PR ld/19073: We may see an unversioned definition after the
1733 default version. */
1734 if (p == NULL)
1735 return TRUE;
1736 }
45d6a902 1737
45d6a902
AM
1738 bed = get_elf_backend_data (abfd);
1739 collect = bed->collect;
1740 dynamic = (abfd->flags & DYNAMIC) != 0;
1741
1742 shortlen = p - name;
a50b1753 1743 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1744 if (shortname == NULL)
1745 return FALSE;
1746 memcpy (shortname, name, shortlen);
1747 shortname[shortlen] = '\0';
1748
1749 /* We are going to create a new symbol. Merge it with any existing
1750 symbol with this name. For the purposes of the merge, act as
1751 though we were defining the symbol we just defined, although we
1752 actually going to define an indirect symbol. */
1753 type_change_ok = FALSE;
1754 size_change_ok = FALSE;
6e33951e 1755 matched = TRUE;
ffd65175
AM
1756 tmp_sec = sec;
1757 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1758 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1759 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1760 return FALSE;
1761
1762 if (skip)
1763 goto nondefault;
1764
1765 if (! override)
1766 {
c6e8a9a8 1767 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1768 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1769 {
1770 bh = &hi->root;
1771 if (! (_bfd_generic_link_add_one_symbol
1772 (info, abfd, shortname, BSF_INDIRECT,
1773 bfd_ind_section_ptr,
1774 0, name, FALSE, collect, &bh)))
1775 return FALSE;
1776 hi = (struct elf_link_hash_entry *) bh;
1777 }
45d6a902
AM
1778 }
1779 else
1780 {
1781 /* In this case the symbol named SHORTNAME is overriding the
1782 indirect symbol we want to add. We were planning on making
1783 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1784 is the name without a version. NAME is the fully versioned
1785 name, and it is the default version.
1786
1787 Overriding means that we already saw a definition for the
1788 symbol SHORTNAME in a regular object, and it is overriding
1789 the symbol defined in the dynamic object.
1790
1791 When this happens, we actually want to change NAME, the
1792 symbol we just added, to refer to SHORTNAME. This will cause
1793 references to NAME in the shared object to become references
1794 to SHORTNAME in the regular object. This is what we expect
1795 when we override a function in a shared object: that the
1796 references in the shared object will be mapped to the
1797 definition in the regular object. */
1798
1799 while (hi->root.type == bfd_link_hash_indirect
1800 || hi->root.type == bfd_link_hash_warning)
1801 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1802
1803 h->root.type = bfd_link_hash_indirect;
1804 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1805 if (h->def_dynamic)
45d6a902 1806 {
f5385ebf
AM
1807 h->def_dynamic = 0;
1808 hi->ref_dynamic = 1;
1809 if (hi->ref_regular
1810 || hi->def_regular)
45d6a902 1811 {
c152c796 1812 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1813 return FALSE;
1814 }
1815 }
1816
1817 /* Now set HI to H, so that the following code will set the
1818 other fields correctly. */
1819 hi = h;
1820 }
1821
fab4a87f
L
1822 /* Check if HI is a warning symbol. */
1823 if (hi->root.type == bfd_link_hash_warning)
1824 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1825
45d6a902
AM
1826 /* If there is a duplicate definition somewhere, then HI may not
1827 point to an indirect symbol. We will have reported an error to
1828 the user in that case. */
1829
1830 if (hi->root.type == bfd_link_hash_indirect)
1831 {
1832 struct elf_link_hash_entry *ht;
1833
45d6a902 1834 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1835 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1836
68c88cd4
AM
1837 /* A reference to the SHORTNAME symbol from a dynamic library
1838 will be satisfied by the versioned symbol at runtime. In
1839 effect, we have a reference to the versioned symbol. */
1840 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1841 hi->dynamic_def |= ht->dynamic_def;
1842
45d6a902
AM
1843 /* See if the new flags lead us to realize that the symbol must
1844 be dynamic. */
1845 if (! *dynsym)
1846 {
1847 if (! dynamic)
1848 {
0e1862bb 1849 if (! bfd_link_executable (info)
90c984fc 1850 || hi->def_dynamic
f5385ebf 1851 || hi->ref_dynamic)
45d6a902
AM
1852 *dynsym = TRUE;
1853 }
1854 else
1855 {
f5385ebf 1856 if (hi->ref_regular)
45d6a902
AM
1857 *dynsym = TRUE;
1858 }
1859 }
1860 }
1861
1862 /* We also need to define an indirection from the nondefault version
1863 of the symbol. */
1864
1865nondefault:
1866 len = strlen (name);
a50b1753 1867 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1868 if (shortname == NULL)
1869 return FALSE;
1870 memcpy (shortname, name, shortlen);
1871 memcpy (shortname + shortlen, p + 1, len - shortlen);
1872
1873 /* Once again, merge with any existing symbol. */
1874 type_change_ok = FALSE;
1875 size_change_ok = FALSE;
ffd65175
AM
1876 tmp_sec = sec;
1877 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1878 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1879 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1880 return FALSE;
1881
1882 if (skip)
1883 return TRUE;
1884
1885 if (override)
1886 {
1887 /* Here SHORTNAME is a versioned name, so we don't expect to see
1888 the type of override we do in the case above unless it is
4cc11e76 1889 overridden by a versioned definition. */
45d6a902
AM
1890 if (hi->root.type != bfd_link_hash_defined
1891 && hi->root.type != bfd_link_hash_defweak)
1892 (*_bfd_error_handler)
d003868e
AM
1893 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1894 abfd, shortname);
45d6a902
AM
1895 }
1896 else
1897 {
1898 bh = &hi->root;
1899 if (! (_bfd_generic_link_add_one_symbol
1900 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1901 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1902 return FALSE;
1903 hi = (struct elf_link_hash_entry *) bh;
1904
1905 /* If there is a duplicate definition somewhere, then HI may not
1906 point to an indirect symbol. We will have reported an error
1907 to the user in that case. */
1908
1909 if (hi->root.type == bfd_link_hash_indirect)
1910 {
fcfa13d2 1911 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1912 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1913 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1914
1915 /* See if the new flags lead us to realize that the symbol
1916 must be dynamic. */
1917 if (! *dynsym)
1918 {
1919 if (! dynamic)
1920 {
0e1862bb 1921 if (! bfd_link_executable (info)
f5385ebf 1922 || hi->ref_dynamic)
45d6a902
AM
1923 *dynsym = TRUE;
1924 }
1925 else
1926 {
f5385ebf 1927 if (hi->ref_regular)
45d6a902
AM
1928 *dynsym = TRUE;
1929 }
1930 }
1931 }
1932 }
1933
1934 return TRUE;
1935}
1936\f
1937/* This routine is used to export all defined symbols into the dynamic
1938 symbol table. It is called via elf_link_hash_traverse. */
1939
28caa186 1940static bfd_boolean
268b6b39 1941_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1942{
a50b1753 1943 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1944
1945 /* Ignore indirect symbols. These are added by the versioning code. */
1946 if (h->root.type == bfd_link_hash_indirect)
1947 return TRUE;
1948
7686d77d
AM
1949 /* Ignore this if we won't export it. */
1950 if (!eif->info->export_dynamic && !h->dynamic)
1951 return TRUE;
45d6a902
AM
1952
1953 if (h->dynindx == -1
fd91d419
L
1954 && (h->def_regular || h->ref_regular)
1955 && ! bfd_hide_sym_by_version (eif->info->version_info,
1956 h->root.root.string))
45d6a902 1957 {
fd91d419 1958 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1959 {
fd91d419
L
1960 eif->failed = TRUE;
1961 return FALSE;
45d6a902
AM
1962 }
1963 }
1964
1965 return TRUE;
1966}
1967\f
1968/* Look through the symbols which are defined in other shared
1969 libraries and referenced here. Update the list of version
1970 dependencies. This will be put into the .gnu.version_r section.
1971 This function is called via elf_link_hash_traverse. */
1972
28caa186 1973static bfd_boolean
268b6b39
AM
1974_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1975 void *data)
45d6a902 1976{
a50b1753 1977 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1978 Elf_Internal_Verneed *t;
1979 Elf_Internal_Vernaux *a;
1980 bfd_size_type amt;
1981
45d6a902
AM
1982 /* We only care about symbols defined in shared objects with version
1983 information. */
f5385ebf
AM
1984 if (!h->def_dynamic
1985 || h->def_regular
45d6a902 1986 || h->dynindx == -1
7b20f099
AM
1987 || h->verinfo.verdef == NULL
1988 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1989 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
1990 return TRUE;
1991
1992 /* See if we already know about this version. */
28caa186
AM
1993 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1994 t != NULL;
1995 t = t->vn_nextref)
45d6a902
AM
1996 {
1997 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1998 continue;
1999
2000 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2001 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2002 return TRUE;
2003
2004 break;
2005 }
2006
2007 /* This is a new version. Add it to tree we are building. */
2008
2009 if (t == NULL)
2010 {
2011 amt = sizeof *t;
a50b1753 2012 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2013 if (t == NULL)
2014 {
2015 rinfo->failed = TRUE;
2016 return FALSE;
2017 }
2018
2019 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2020 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2021 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2022 }
2023
2024 amt = sizeof *a;
a50b1753 2025 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2026 if (a == NULL)
2027 {
2028 rinfo->failed = TRUE;
2029 return FALSE;
2030 }
45d6a902
AM
2031
2032 /* Note that we are copying a string pointer here, and testing it
2033 above. If bfd_elf_string_from_elf_section is ever changed to
2034 discard the string data when low in memory, this will have to be
2035 fixed. */
2036 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2037
2038 a->vna_flags = h->verinfo.verdef->vd_flags;
2039 a->vna_nextptr = t->vn_auxptr;
2040
2041 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2042 ++rinfo->vers;
2043
2044 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2045
2046 t->vn_auxptr = a;
2047
2048 return TRUE;
2049}
2050
2051/* Figure out appropriate versions for all the symbols. We may not
2052 have the version number script until we have read all of the input
2053 files, so until that point we don't know which symbols should be
2054 local. This function is called via elf_link_hash_traverse. */
2055
28caa186 2056static bfd_boolean
268b6b39 2057_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2058{
28caa186 2059 struct elf_info_failed *sinfo;
45d6a902 2060 struct bfd_link_info *info;
9c5bfbb7 2061 const struct elf_backend_data *bed;
45d6a902
AM
2062 struct elf_info_failed eif;
2063 char *p;
2064 bfd_size_type amt;
2065
a50b1753 2066 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2067 info = sinfo->info;
2068
45d6a902
AM
2069 /* Fix the symbol flags. */
2070 eif.failed = FALSE;
2071 eif.info = info;
2072 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2073 {
2074 if (eif.failed)
2075 sinfo->failed = TRUE;
2076 return FALSE;
2077 }
2078
2079 /* We only need version numbers for symbols defined in regular
2080 objects. */
f5385ebf 2081 if (!h->def_regular)
45d6a902
AM
2082 return TRUE;
2083
28caa186 2084 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2085 p = strchr (h->root.root.string, ELF_VER_CHR);
2086 if (p != NULL && h->verinfo.vertree == NULL)
2087 {
2088 struct bfd_elf_version_tree *t;
45d6a902 2089
45d6a902
AM
2090 ++p;
2091 if (*p == ELF_VER_CHR)
6e33951e 2092 ++p;
45d6a902
AM
2093
2094 /* If there is no version string, we can just return out. */
2095 if (*p == '\0')
6e33951e 2096 return TRUE;
45d6a902
AM
2097
2098 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2099 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2100 {
2101 if (strcmp (t->name, p) == 0)
2102 {
2103 size_t len;
2104 char *alc;
2105 struct bfd_elf_version_expr *d;
2106
2107 len = p - h->root.root.string;
a50b1753 2108 alc = (char *) bfd_malloc (len);
45d6a902 2109 if (alc == NULL)
14b1c01e
AM
2110 {
2111 sinfo->failed = TRUE;
2112 return FALSE;
2113 }
45d6a902
AM
2114 memcpy (alc, h->root.root.string, len - 1);
2115 alc[len - 1] = '\0';
2116 if (alc[len - 2] == ELF_VER_CHR)
2117 alc[len - 2] = '\0';
2118
2119 h->verinfo.vertree = t;
2120 t->used = TRUE;
2121 d = NULL;
2122
108ba305
JJ
2123 if (t->globals.list != NULL)
2124 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2125
2126 /* See if there is anything to force this symbol to
2127 local scope. */
108ba305 2128 if (d == NULL && t->locals.list != NULL)
45d6a902 2129 {
108ba305
JJ
2130 d = (*t->match) (&t->locals, NULL, alc);
2131 if (d != NULL
2132 && h->dynindx != -1
108ba305
JJ
2133 && ! info->export_dynamic)
2134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2135 }
2136
2137 free (alc);
2138 break;
2139 }
2140 }
2141
2142 /* If we are building an application, we need to create a
2143 version node for this version. */
0e1862bb 2144 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2145 {
2146 struct bfd_elf_version_tree **pp;
2147 int version_index;
2148
2149 /* If we aren't going to export this symbol, we don't need
2150 to worry about it. */
2151 if (h->dynindx == -1)
2152 return TRUE;
2153
2154 amt = sizeof *t;
a50b1753 2155 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2156 if (t == NULL)
2157 {
2158 sinfo->failed = TRUE;
2159 return FALSE;
2160 }
2161
45d6a902 2162 t->name = p;
45d6a902
AM
2163 t->name_indx = (unsigned int) -1;
2164 t->used = TRUE;
2165
2166 version_index = 1;
2167 /* Don't count anonymous version tag. */
fd91d419
L
2168 if (sinfo->info->version_info != NULL
2169 && sinfo->info->version_info->vernum == 0)
45d6a902 2170 version_index = 0;
fd91d419
L
2171 for (pp = &sinfo->info->version_info;
2172 *pp != NULL;
2173 pp = &(*pp)->next)
45d6a902
AM
2174 ++version_index;
2175 t->vernum = version_index;
2176
2177 *pp = t;
2178
2179 h->verinfo.vertree = t;
2180 }
2181 else if (t == NULL)
2182 {
2183 /* We could not find the version for a symbol when
2184 generating a shared archive. Return an error. */
2185 (*_bfd_error_handler)
c55fe096 2186 (_("%B: version node not found for symbol %s"),
28caa186 2187 info->output_bfd, h->root.root.string);
45d6a902
AM
2188 bfd_set_error (bfd_error_bad_value);
2189 sinfo->failed = TRUE;
2190 return FALSE;
2191 }
45d6a902
AM
2192 }
2193
2194 /* If we don't have a version for this symbol, see if we can find
2195 something. */
fd91d419 2196 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2197 {
1e8fa21e 2198 bfd_boolean hide;
ae5a3597 2199
fd91d419
L
2200 h->verinfo.vertree
2201 = bfd_find_version_for_sym (sinfo->info->version_info,
2202 h->root.root.string, &hide);
1e8fa21e
AM
2203 if (h->verinfo.vertree != NULL && hide)
2204 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2205 }
2206
2207 return TRUE;
2208}
2209\f
45d6a902
AM
2210/* Read and swap the relocs from the section indicated by SHDR. This
2211 may be either a REL or a RELA section. The relocations are
2212 translated into RELA relocations and stored in INTERNAL_RELOCS,
2213 which should have already been allocated to contain enough space.
2214 The EXTERNAL_RELOCS are a buffer where the external form of the
2215 relocations should be stored.
2216
2217 Returns FALSE if something goes wrong. */
2218
2219static bfd_boolean
268b6b39 2220elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2221 asection *sec,
268b6b39
AM
2222 Elf_Internal_Shdr *shdr,
2223 void *external_relocs,
2224 Elf_Internal_Rela *internal_relocs)
45d6a902 2225{
9c5bfbb7 2226 const struct elf_backend_data *bed;
268b6b39 2227 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2228 const bfd_byte *erela;
2229 const bfd_byte *erelaend;
2230 Elf_Internal_Rela *irela;
243ef1e0
L
2231 Elf_Internal_Shdr *symtab_hdr;
2232 size_t nsyms;
45d6a902 2233
45d6a902
AM
2234 /* Position ourselves at the start of the section. */
2235 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2236 return FALSE;
2237
2238 /* Read the relocations. */
2239 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2240 return FALSE;
2241
243ef1e0 2242 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2243 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2244
45d6a902
AM
2245 bed = get_elf_backend_data (abfd);
2246
2247 /* Convert the external relocations to the internal format. */
2248 if (shdr->sh_entsize == bed->s->sizeof_rel)
2249 swap_in = bed->s->swap_reloc_in;
2250 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2251 swap_in = bed->s->swap_reloca_in;
2252 else
2253 {
2254 bfd_set_error (bfd_error_wrong_format);
2255 return FALSE;
2256 }
2257
a50b1753 2258 erela = (const bfd_byte *) external_relocs;
51992aec 2259 erelaend = erela + shdr->sh_size;
45d6a902
AM
2260 irela = internal_relocs;
2261 while (erela < erelaend)
2262 {
243ef1e0
L
2263 bfd_vma r_symndx;
2264
45d6a902 2265 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2266 r_symndx = ELF32_R_SYM (irela->r_info);
2267 if (bed->s->arch_size == 64)
2268 r_symndx >>= 24;
ce98a316
NC
2269 if (nsyms > 0)
2270 {
2271 if ((size_t) r_symndx >= nsyms)
2272 {
2273 (*_bfd_error_handler)
2274 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2275 " for offset 0x%lx in section `%A'"),
2276 abfd, sec,
2277 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2278 bfd_set_error (bfd_error_bad_value);
2279 return FALSE;
2280 }
2281 }
cf35638d 2282 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2283 {
2284 (*_bfd_error_handler)
ce98a316
NC
2285 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2286 " when the object file has no symbol table"),
d003868e
AM
2287 abfd, sec,
2288 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2289 bfd_set_error (bfd_error_bad_value);
2290 return FALSE;
2291 }
45d6a902
AM
2292 irela += bed->s->int_rels_per_ext_rel;
2293 erela += shdr->sh_entsize;
2294 }
2295
2296 return TRUE;
2297}
2298
2299/* Read and swap the relocs for a section O. They may have been
2300 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2301 not NULL, they are used as buffers to read into. They are known to
2302 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2303 the return value is allocated using either malloc or bfd_alloc,
2304 according to the KEEP_MEMORY argument. If O has two relocation
2305 sections (both REL and RELA relocations), then the REL_HDR
2306 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2307 RELA_HDR relocations. */
45d6a902
AM
2308
2309Elf_Internal_Rela *
268b6b39
AM
2310_bfd_elf_link_read_relocs (bfd *abfd,
2311 asection *o,
2312 void *external_relocs,
2313 Elf_Internal_Rela *internal_relocs,
2314 bfd_boolean keep_memory)
45d6a902 2315{
268b6b39 2316 void *alloc1 = NULL;
45d6a902 2317 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2318 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2319 struct bfd_elf_section_data *esdo = elf_section_data (o);
2320 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2321
d4730f92
BS
2322 if (esdo->relocs != NULL)
2323 return esdo->relocs;
45d6a902
AM
2324
2325 if (o->reloc_count == 0)
2326 return NULL;
2327
45d6a902
AM
2328 if (internal_relocs == NULL)
2329 {
2330 bfd_size_type size;
2331
2332 size = o->reloc_count;
2333 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2334 if (keep_memory)
a50b1753 2335 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2336 else
a50b1753 2337 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2338 if (internal_relocs == NULL)
2339 goto error_return;
2340 }
2341
2342 if (external_relocs == NULL)
2343 {
d4730f92
BS
2344 bfd_size_type size = 0;
2345
2346 if (esdo->rel.hdr)
2347 size += esdo->rel.hdr->sh_size;
2348 if (esdo->rela.hdr)
2349 size += esdo->rela.hdr->sh_size;
45d6a902 2350
268b6b39 2351 alloc1 = bfd_malloc (size);
45d6a902
AM
2352 if (alloc1 == NULL)
2353 goto error_return;
2354 external_relocs = alloc1;
2355 }
2356
d4730f92
BS
2357 internal_rela_relocs = internal_relocs;
2358 if (esdo->rel.hdr)
2359 {
2360 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2361 external_relocs,
2362 internal_relocs))
2363 goto error_return;
2364 external_relocs = (((bfd_byte *) external_relocs)
2365 + esdo->rel.hdr->sh_size);
2366 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2367 * bed->s->int_rels_per_ext_rel);
2368 }
2369
2370 if (esdo->rela.hdr
2371 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2372 external_relocs,
2373 internal_rela_relocs)))
45d6a902
AM
2374 goto error_return;
2375
2376 /* Cache the results for next time, if we can. */
2377 if (keep_memory)
d4730f92 2378 esdo->relocs = internal_relocs;
45d6a902
AM
2379
2380 if (alloc1 != NULL)
2381 free (alloc1);
2382
2383 /* Don't free alloc2, since if it was allocated we are passing it
2384 back (under the name of internal_relocs). */
2385
2386 return internal_relocs;
2387
2388 error_return:
2389 if (alloc1 != NULL)
2390 free (alloc1);
2391 if (alloc2 != NULL)
4dd07732
AM
2392 {
2393 if (keep_memory)
2394 bfd_release (abfd, alloc2);
2395 else
2396 free (alloc2);
2397 }
45d6a902
AM
2398 return NULL;
2399}
2400
2401/* Compute the size of, and allocate space for, REL_HDR which is the
2402 section header for a section containing relocations for O. */
2403
28caa186 2404static bfd_boolean
268b6b39 2405_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2406 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2407{
d4730f92 2408 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2409
2410 /* That allows us to calculate the size of the section. */
d4730f92 2411 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2412
2413 /* The contents field must last into write_object_contents, so we
2414 allocate it with bfd_alloc rather than malloc. Also since we
2415 cannot be sure that the contents will actually be filled in,
2416 we zero the allocated space. */
a50b1753 2417 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2418 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2419 return FALSE;
2420
d4730f92 2421 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2422 {
2423 struct elf_link_hash_entry **p;
2424
ca4be51c
AM
2425 p = ((struct elf_link_hash_entry **)
2426 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2427 if (p == NULL)
2428 return FALSE;
2429
d4730f92 2430 reldata->hashes = p;
45d6a902
AM
2431 }
2432
2433 return TRUE;
2434}
2435
2436/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2437 originated from the section given by INPUT_REL_HDR) to the
2438 OUTPUT_BFD. */
2439
2440bfd_boolean
268b6b39
AM
2441_bfd_elf_link_output_relocs (bfd *output_bfd,
2442 asection *input_section,
2443 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2444 Elf_Internal_Rela *internal_relocs,
2445 struct elf_link_hash_entry **rel_hash
2446 ATTRIBUTE_UNUSED)
45d6a902
AM
2447{
2448 Elf_Internal_Rela *irela;
2449 Elf_Internal_Rela *irelaend;
2450 bfd_byte *erel;
d4730f92 2451 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2452 asection *output_section;
9c5bfbb7 2453 const struct elf_backend_data *bed;
268b6b39 2454 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2455 struct bfd_elf_section_data *esdo;
45d6a902
AM
2456
2457 output_section = input_section->output_section;
45d6a902 2458
d4730f92
BS
2459 bed = get_elf_backend_data (output_bfd);
2460 esdo = elf_section_data (output_section);
2461 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2462 {
d4730f92
BS
2463 output_reldata = &esdo->rel;
2464 swap_out = bed->s->swap_reloc_out;
45d6a902 2465 }
d4730f92
BS
2466 else if (esdo->rela.hdr
2467 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2468 {
d4730f92
BS
2469 output_reldata = &esdo->rela;
2470 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2471 }
2472 else
2473 {
2474 (*_bfd_error_handler)
d003868e
AM
2475 (_("%B: relocation size mismatch in %B section %A"),
2476 output_bfd, input_section->owner, input_section);
297d8443 2477 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2478 return FALSE;
2479 }
2480
d4730f92
BS
2481 erel = output_reldata->hdr->contents;
2482 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2483 irela = internal_relocs;
2484 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2485 * bed->s->int_rels_per_ext_rel);
2486 while (irela < irelaend)
2487 {
2488 (*swap_out) (output_bfd, irela, erel);
2489 irela += bed->s->int_rels_per_ext_rel;
2490 erel += input_rel_hdr->sh_entsize;
2491 }
2492
2493 /* Bump the counter, so that we know where to add the next set of
2494 relocations. */
d4730f92 2495 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2496
2497 return TRUE;
2498}
2499\f
508c3946
L
2500/* Make weak undefined symbols in PIE dynamic. */
2501
2502bfd_boolean
2503_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2504 struct elf_link_hash_entry *h)
2505{
0e1862bb 2506 if (bfd_link_pie (info)
508c3946
L
2507 && h->dynindx == -1
2508 && h->root.type == bfd_link_hash_undefweak)
2509 return bfd_elf_link_record_dynamic_symbol (info, h);
2510
2511 return TRUE;
2512}
2513
45d6a902
AM
2514/* Fix up the flags for a symbol. This handles various cases which
2515 can only be fixed after all the input files are seen. This is
2516 currently called by both adjust_dynamic_symbol and
2517 assign_sym_version, which is unnecessary but perhaps more robust in
2518 the face of future changes. */
2519
28caa186 2520static bfd_boolean
268b6b39
AM
2521_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2522 struct elf_info_failed *eif)
45d6a902 2523{
33774f08 2524 const struct elf_backend_data *bed;
508c3946 2525
45d6a902
AM
2526 /* If this symbol was mentioned in a non-ELF file, try to set
2527 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2528 permit a non-ELF file to correctly refer to a symbol defined in
2529 an ELF dynamic object. */
f5385ebf 2530 if (h->non_elf)
45d6a902
AM
2531 {
2532 while (h->root.type == bfd_link_hash_indirect)
2533 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2534
2535 if (h->root.type != bfd_link_hash_defined
2536 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2537 {
2538 h->ref_regular = 1;
2539 h->ref_regular_nonweak = 1;
2540 }
45d6a902
AM
2541 else
2542 {
2543 if (h->root.u.def.section->owner != NULL
2544 && (bfd_get_flavour (h->root.u.def.section->owner)
2545 == bfd_target_elf_flavour))
f5385ebf
AM
2546 {
2547 h->ref_regular = 1;
2548 h->ref_regular_nonweak = 1;
2549 }
45d6a902 2550 else
f5385ebf 2551 h->def_regular = 1;
45d6a902
AM
2552 }
2553
2554 if (h->dynindx == -1
f5385ebf
AM
2555 && (h->def_dynamic
2556 || h->ref_dynamic))
45d6a902 2557 {
c152c796 2558 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2559 {
2560 eif->failed = TRUE;
2561 return FALSE;
2562 }
2563 }
2564 }
2565 else
2566 {
f5385ebf 2567 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2568 was first seen in a non-ELF file. Fortunately, if the symbol
2569 was first seen in an ELF file, we're probably OK unless the
2570 symbol was defined in a non-ELF file. Catch that case here.
2571 FIXME: We're still in trouble if the symbol was first seen in
2572 a dynamic object, and then later in a non-ELF regular object. */
2573 if ((h->root.type == bfd_link_hash_defined
2574 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2575 && !h->def_regular
45d6a902
AM
2576 && (h->root.u.def.section->owner != NULL
2577 ? (bfd_get_flavour (h->root.u.def.section->owner)
2578 != bfd_target_elf_flavour)
2579 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2580 && !h->def_dynamic)))
2581 h->def_regular = 1;
45d6a902
AM
2582 }
2583
508c3946 2584 /* Backend specific symbol fixup. */
33774f08
AM
2585 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2586 if (bed->elf_backend_fixup_symbol
2587 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2588 return FALSE;
508c3946 2589
45d6a902
AM
2590 /* If this is a final link, and the symbol was defined as a common
2591 symbol in a regular object file, and there was no definition in
2592 any dynamic object, then the linker will have allocated space for
f5385ebf 2593 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2594 flag will not have been set. */
2595 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2596 && !h->def_regular
2597 && h->ref_regular
2598 && !h->def_dynamic
96f29d96 2599 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2600 h->def_regular = 1;
45d6a902
AM
2601
2602 /* If -Bsymbolic was used (which means to bind references to global
2603 symbols to the definition within the shared object), and this
2604 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2605 need a PLT entry. Likewise, if the symbol has non-default
2606 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2607 will force it local. */
f5385ebf 2608 if (h->needs_plt
0e1862bb 2609 && bfd_link_pic (eif->info)
0eddce27 2610 && is_elf_hash_table (eif->info->hash)
55255dae 2611 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2612 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2613 && h->def_regular)
45d6a902 2614 {
45d6a902
AM
2615 bfd_boolean force_local;
2616
45d6a902
AM
2617 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2618 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2619 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2620 }
2621
2622 /* If a weak undefined symbol has non-default visibility, we also
2623 hide it from the dynamic linker. */
9c7a29a3 2624 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2625 && h->root.type == bfd_link_hash_undefweak)
33774f08 2626 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2627
2628 /* If this is a weak defined symbol in a dynamic object, and we know
2629 the real definition in the dynamic object, copy interesting flags
2630 over to the real definition. */
f6e332e6 2631 if (h->u.weakdef != NULL)
45d6a902 2632 {
45d6a902
AM
2633 /* If the real definition is defined by a regular object file,
2634 don't do anything special. See the longer description in
2635 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2636 if (h->u.weakdef->def_regular)
f6e332e6 2637 h->u.weakdef = NULL;
45d6a902 2638 else
a26587ba 2639 {
4e6b54a6
AM
2640 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2641
2642 while (h->root.type == bfd_link_hash_indirect)
2643 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2644
2645 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2646 || h->root.type == bfd_link_hash_defweak);
2647 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2648 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2649 || weakdef->root.type == bfd_link_hash_defweak);
2650 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2651 }
45d6a902
AM
2652 }
2653
2654 return TRUE;
2655}
2656
2657/* Make the backend pick a good value for a dynamic symbol. This is
2658 called via elf_link_hash_traverse, and also calls itself
2659 recursively. */
2660
28caa186 2661static bfd_boolean
268b6b39 2662_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2663{
a50b1753 2664 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2665 bfd *dynobj;
9c5bfbb7 2666 const struct elf_backend_data *bed;
45d6a902 2667
0eddce27 2668 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2669 return FALSE;
2670
45d6a902
AM
2671 /* Ignore indirect symbols. These are added by the versioning code. */
2672 if (h->root.type == bfd_link_hash_indirect)
2673 return TRUE;
2674
2675 /* Fix the symbol flags. */
2676 if (! _bfd_elf_fix_symbol_flags (h, eif))
2677 return FALSE;
2678
2679 /* If this symbol does not require a PLT entry, and it is not
2680 defined by a dynamic object, or is not referenced by a regular
2681 object, ignore it. We do have to handle a weak defined symbol,
2682 even if no regular object refers to it, if we decided to add it
2683 to the dynamic symbol table. FIXME: Do we normally need to worry
2684 about symbols which are defined by one dynamic object and
2685 referenced by another one? */
f5385ebf 2686 if (!h->needs_plt
91e21fb7 2687 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2688 && (h->def_regular
2689 || !h->def_dynamic
2690 || (!h->ref_regular
f6e332e6 2691 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2692 {
a6aa5195 2693 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2694 return TRUE;
2695 }
2696
2697 /* If we've already adjusted this symbol, don't do it again. This
2698 can happen via a recursive call. */
f5385ebf 2699 if (h->dynamic_adjusted)
45d6a902
AM
2700 return TRUE;
2701
2702 /* Don't look at this symbol again. Note that we must set this
2703 after checking the above conditions, because we may look at a
2704 symbol once, decide not to do anything, and then get called
2705 recursively later after REF_REGULAR is set below. */
f5385ebf 2706 h->dynamic_adjusted = 1;
45d6a902
AM
2707
2708 /* If this is a weak definition, and we know a real definition, and
2709 the real symbol is not itself defined by a regular object file,
2710 then get a good value for the real definition. We handle the
2711 real symbol first, for the convenience of the backend routine.
2712
2713 Note that there is a confusing case here. If the real definition
2714 is defined by a regular object file, we don't get the real symbol
2715 from the dynamic object, but we do get the weak symbol. If the
2716 processor backend uses a COPY reloc, then if some routine in the
2717 dynamic object changes the real symbol, we will not see that
2718 change in the corresponding weak symbol. This is the way other
2719 ELF linkers work as well, and seems to be a result of the shared
2720 library model.
2721
2722 I will clarify this issue. Most SVR4 shared libraries define the
2723 variable _timezone and define timezone as a weak synonym. The
2724 tzset call changes _timezone. If you write
2725 extern int timezone;
2726 int _timezone = 5;
2727 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2728 you might expect that, since timezone is a synonym for _timezone,
2729 the same number will print both times. However, if the processor
2730 backend uses a COPY reloc, then actually timezone will be copied
2731 into your process image, and, since you define _timezone
2732 yourself, _timezone will not. Thus timezone and _timezone will
2733 wind up at different memory locations. The tzset call will set
2734 _timezone, leaving timezone unchanged. */
2735
f6e332e6 2736 if (h->u.weakdef != NULL)
45d6a902 2737 {
ec24dc88
AM
2738 /* If we get to this point, there is an implicit reference to
2739 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2740 h->u.weakdef->ref_regular = 1;
45d6a902 2741
ec24dc88
AM
2742 /* Ensure that the backend adjust_dynamic_symbol function sees
2743 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2744 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2745 return FALSE;
2746 }
2747
2748 /* If a symbol has no type and no size and does not require a PLT
2749 entry, then we are probably about to do the wrong thing here: we
2750 are probably going to create a COPY reloc for an empty object.
2751 This case can arise when a shared object is built with assembly
2752 code, and the assembly code fails to set the symbol type. */
2753 if (h->size == 0
2754 && h->type == STT_NOTYPE
f5385ebf 2755 && !h->needs_plt)
45d6a902
AM
2756 (*_bfd_error_handler)
2757 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2758 h->root.root.string);
2759
2760 dynobj = elf_hash_table (eif->info)->dynobj;
2761 bed = get_elf_backend_data (dynobj);
e7c33416 2762
45d6a902
AM
2763 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2764 {
2765 eif->failed = TRUE;
2766 return FALSE;
2767 }
2768
2769 return TRUE;
2770}
2771
027297b7
L
2772/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2773 DYNBSS. */
2774
2775bfd_boolean
6cabe1ea
AM
2776_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2777 struct elf_link_hash_entry *h,
027297b7
L
2778 asection *dynbss)
2779{
91ac5911 2780 unsigned int power_of_two;
027297b7
L
2781 bfd_vma mask;
2782 asection *sec = h->root.u.def.section;
2783
2784 /* The section aligment of definition is the maximum alignment
91ac5911
L
2785 requirement of symbols defined in the section. Since we don't
2786 know the symbol alignment requirement, we start with the
2787 maximum alignment and check low bits of the symbol address
2788 for the minimum alignment. */
2789 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2790 mask = ((bfd_vma) 1 << power_of_two) - 1;
2791 while ((h->root.u.def.value & mask) != 0)
2792 {
2793 mask >>= 1;
2794 --power_of_two;
2795 }
027297b7 2796
91ac5911
L
2797 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2798 dynbss))
027297b7
L
2799 {
2800 /* Adjust the section alignment if needed. */
2801 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2802 power_of_two))
027297b7
L
2803 return FALSE;
2804 }
2805
91ac5911 2806 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2807 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2808
2809 /* Define the symbol as being at this point in DYNBSS. */
2810 h->root.u.def.section = dynbss;
2811 h->root.u.def.value = dynbss->size;
2812
2813 /* Increment the size of DYNBSS to make room for the symbol. */
2814 dynbss->size += h->size;
2815
f7483970
L
2816 /* No error if extern_protected_data is true. */
2817 if (h->protected_def
889c2a67
L
2818 && (!info->extern_protected_data
2819 || (info->extern_protected_data < 0
2820 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2821 info->callbacks->einfo
2822 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2823 h->root.root.string);
6cabe1ea 2824
027297b7
L
2825 return TRUE;
2826}
2827
45d6a902
AM
2828/* Adjust all external symbols pointing into SEC_MERGE sections
2829 to reflect the object merging within the sections. */
2830
28caa186 2831static bfd_boolean
268b6b39 2832_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2833{
2834 asection *sec;
2835
45d6a902
AM
2836 if ((h->root.type == bfd_link_hash_defined
2837 || h->root.type == bfd_link_hash_defweak)
2838 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2839 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2840 {
a50b1753 2841 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2842
2843 h->root.u.def.value =
2844 _bfd_merged_section_offset (output_bfd,
2845 &h->root.u.def.section,
2846 elf_section_data (sec)->sec_info,
753731ee 2847 h->root.u.def.value);
45d6a902
AM
2848 }
2849
2850 return TRUE;
2851}
986a241f
RH
2852
2853/* Returns false if the symbol referred to by H should be considered
2854 to resolve local to the current module, and true if it should be
2855 considered to bind dynamically. */
2856
2857bfd_boolean
268b6b39
AM
2858_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2859 struct bfd_link_info *info,
89a2ee5a 2860 bfd_boolean not_local_protected)
986a241f
RH
2861{
2862 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2863 const struct elf_backend_data *bed;
2864 struct elf_link_hash_table *hash_table;
986a241f
RH
2865
2866 if (h == NULL)
2867 return FALSE;
2868
2869 while (h->root.type == bfd_link_hash_indirect
2870 || h->root.type == bfd_link_hash_warning)
2871 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2872
2873 /* If it was forced local, then clearly it's not dynamic. */
2874 if (h->dynindx == -1)
2875 return FALSE;
f5385ebf 2876 if (h->forced_local)
986a241f
RH
2877 return FALSE;
2878
2879 /* Identify the cases where name binding rules say that a
2880 visible symbol resolves locally. */
0e1862bb
L
2881 binding_stays_local_p = (bfd_link_executable (info)
2882 || SYMBOLIC_BIND (info, h));
986a241f
RH
2883
2884 switch (ELF_ST_VISIBILITY (h->other))
2885 {
2886 case STV_INTERNAL:
2887 case STV_HIDDEN:
2888 return FALSE;
2889
2890 case STV_PROTECTED:
fcb93ecf
PB
2891 hash_table = elf_hash_table (info);
2892 if (!is_elf_hash_table (hash_table))
2893 return FALSE;
2894
2895 bed = get_elf_backend_data (hash_table->dynobj);
2896
986a241f
RH
2897 /* Proper resolution for function pointer equality may require
2898 that these symbols perhaps be resolved dynamically, even though
2899 we should be resolving them to the current module. */
89a2ee5a 2900 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2901 binding_stays_local_p = TRUE;
2902 break;
2903
2904 default:
986a241f
RH
2905 break;
2906 }
2907
aa37626c 2908 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2909 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2910 return TRUE;
2911
986a241f
RH
2912 /* Otherwise, the symbol is dynamic if binding rules don't tell
2913 us that it remains local. */
2914 return !binding_stays_local_p;
2915}
f6c52c13
AM
2916
2917/* Return true if the symbol referred to by H should be considered
2918 to resolve local to the current module, and false otherwise. Differs
2919 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2920 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2921 for the place where forced_local and dynindx == -1 are tested. If
2922 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2923 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2924 the symbol is local only for defined symbols.
2925 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2926 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2927 treatment of undefined weak symbols. For those that do not make
2928 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2929
2930bfd_boolean
268b6b39
AM
2931_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2932 struct bfd_link_info *info,
2933 bfd_boolean local_protected)
f6c52c13 2934{
fcb93ecf
PB
2935 const struct elf_backend_data *bed;
2936 struct elf_link_hash_table *hash_table;
2937
f6c52c13
AM
2938 /* If it's a local sym, of course we resolve locally. */
2939 if (h == NULL)
2940 return TRUE;
2941
d95edcac
L
2942 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2943 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2944 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2945 return TRUE;
2946
7e2294f9
AO
2947 /* Common symbols that become definitions don't get the DEF_REGULAR
2948 flag set, so test it first, and don't bail out. */
2949 if (ELF_COMMON_DEF_P (h))
2950 /* Do nothing. */;
f6c52c13 2951 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2952 resolve locally. The sym is either undefined or dynamic. */
2953 else if (!h->def_regular)
f6c52c13
AM
2954 return FALSE;
2955
2956 /* Forced local symbols resolve locally. */
f5385ebf 2957 if (h->forced_local)
f6c52c13
AM
2958 return TRUE;
2959
2960 /* As do non-dynamic symbols. */
2961 if (h->dynindx == -1)
2962 return TRUE;
2963
2964 /* At this point, we know the symbol is defined and dynamic. In an
2965 executable it must resolve locally, likewise when building symbolic
2966 shared libraries. */
0e1862bb 2967 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2968 return TRUE;
2969
2970 /* Now deal with defined dynamic symbols in shared libraries. Ones
2971 with default visibility might not resolve locally. */
2972 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2973 return FALSE;
2974
fcb93ecf
PB
2975 hash_table = elf_hash_table (info);
2976 if (!is_elf_hash_table (hash_table))
2977 return TRUE;
2978
2979 bed = get_elf_backend_data (hash_table->dynobj);
2980
f7483970
L
2981 /* If extern_protected_data is false, STV_PROTECTED non-function
2982 symbols are local. */
889c2a67
L
2983 if ((!info->extern_protected_data
2984 || (info->extern_protected_data < 0
2985 && !bed->extern_protected_data))
2986 && !bed->is_function_type (h->type))
1c16dfa5
L
2987 return TRUE;
2988
f6c52c13 2989 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2990 symbols be treated as dynamic symbols. If the address of a
2991 function not defined in an executable is set to that function's
2992 plt entry in the executable, then the address of the function in
2993 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2994 return local_protected;
2995}
e1918d23
AM
2996
2997/* Caches some TLS segment info, and ensures that the TLS segment vma is
2998 aligned. Returns the first TLS output section. */
2999
3000struct bfd_section *
3001_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3002{
3003 struct bfd_section *sec, *tls;
3004 unsigned int align = 0;
3005
3006 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3007 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3008 break;
3009 tls = sec;
3010
3011 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3012 if (sec->alignment_power > align)
3013 align = sec->alignment_power;
3014
3015 elf_hash_table (info)->tls_sec = tls;
3016
3017 /* Ensure the alignment of the first section is the largest alignment,
3018 so that the tls segment starts aligned. */
3019 if (tls != NULL)
3020 tls->alignment_power = align;
3021
3022 return tls;
3023}
0ad989f9
L
3024
3025/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3026static bfd_boolean
3027is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3028 Elf_Internal_Sym *sym)
3029{
a4d8e49b
L
3030 const struct elf_backend_data *bed;
3031
0ad989f9
L
3032 /* Local symbols do not count, but target specific ones might. */
3033 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3034 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3035 return FALSE;
3036
fcb93ecf 3037 bed = get_elf_backend_data (abfd);
0ad989f9 3038 /* Function symbols do not count. */
fcb93ecf 3039 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3040 return FALSE;
3041
3042 /* If the section is undefined, then so is the symbol. */
3043 if (sym->st_shndx == SHN_UNDEF)
3044 return FALSE;
3045
3046 /* If the symbol is defined in the common section, then
3047 it is a common definition and so does not count. */
a4d8e49b 3048 if (bed->common_definition (sym))
0ad989f9
L
3049 return FALSE;
3050
3051 /* If the symbol is in a target specific section then we
3052 must rely upon the backend to tell us what it is. */
3053 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3054 /* FIXME - this function is not coded yet:
3055
3056 return _bfd_is_global_symbol_definition (abfd, sym);
3057
3058 Instead for now assume that the definition is not global,
3059 Even if this is wrong, at least the linker will behave
3060 in the same way that it used to do. */
3061 return FALSE;
3062
3063 return TRUE;
3064}
3065
3066/* Search the symbol table of the archive element of the archive ABFD
3067 whose archive map contains a mention of SYMDEF, and determine if
3068 the symbol is defined in this element. */
3069static bfd_boolean
3070elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3071{
3072 Elf_Internal_Shdr * hdr;
3073 bfd_size_type symcount;
3074 bfd_size_type extsymcount;
3075 bfd_size_type extsymoff;
3076 Elf_Internal_Sym *isymbuf;
3077 Elf_Internal_Sym *isym;
3078 Elf_Internal_Sym *isymend;
3079 bfd_boolean result;
3080
3081 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3082 if (abfd == NULL)
3083 return FALSE;
3084
f0bf6bfd
L
3085 /* Return FALSE if the object has been claimed by plugin. */
3086 if (abfd->plugin_format == bfd_plugin_yes)
3087 return FALSE;
3088
0ad989f9
L
3089 if (! bfd_check_format (abfd, bfd_object))
3090 return FALSE;
3091
0ad989f9
L
3092 /* Select the appropriate symbol table. */
3093 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3094 hdr = &elf_tdata (abfd)->symtab_hdr;
3095 else
3096 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3097
3098 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3099
3100 /* The sh_info field of the symtab header tells us where the
3101 external symbols start. We don't care about the local symbols. */
3102 if (elf_bad_symtab (abfd))
3103 {
3104 extsymcount = symcount;
3105 extsymoff = 0;
3106 }
3107 else
3108 {
3109 extsymcount = symcount - hdr->sh_info;
3110 extsymoff = hdr->sh_info;
3111 }
3112
3113 if (extsymcount == 0)
3114 return FALSE;
3115
3116 /* Read in the symbol table. */
3117 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3118 NULL, NULL, NULL);
3119 if (isymbuf == NULL)
3120 return FALSE;
3121
3122 /* Scan the symbol table looking for SYMDEF. */
3123 result = FALSE;
3124 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3125 {
3126 const char *name;
3127
3128 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3129 isym->st_name);
3130 if (name == NULL)
3131 break;
3132
3133 if (strcmp (name, symdef->name) == 0)
3134 {
3135 result = is_global_data_symbol_definition (abfd, isym);
3136 break;
3137 }
3138 }
3139
3140 free (isymbuf);
3141
3142 return result;
3143}
3144\f
5a580b3a
AM
3145/* Add an entry to the .dynamic table. */
3146
3147bfd_boolean
3148_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3149 bfd_vma tag,
3150 bfd_vma val)
3151{
3152 struct elf_link_hash_table *hash_table;
3153 const struct elf_backend_data *bed;
3154 asection *s;
3155 bfd_size_type newsize;
3156 bfd_byte *newcontents;
3157 Elf_Internal_Dyn dyn;
3158
3159 hash_table = elf_hash_table (info);
3160 if (! is_elf_hash_table (hash_table))
3161 return FALSE;
3162
3163 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3164 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3165 BFD_ASSERT (s != NULL);
3166
eea6121a 3167 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3168 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3169 if (newcontents == NULL)
3170 return FALSE;
3171
3172 dyn.d_tag = tag;
3173 dyn.d_un.d_val = val;
eea6121a 3174 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3175
eea6121a 3176 s->size = newsize;
5a580b3a
AM
3177 s->contents = newcontents;
3178
3179 return TRUE;
3180}
3181
3182/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3183 otherwise just check whether one already exists. Returns -1 on error,
3184 1 if a DT_NEEDED tag already exists, and 0 on success. */
3185
4ad4eba5 3186static int
7e9f0867
AM
3187elf_add_dt_needed_tag (bfd *abfd,
3188 struct bfd_link_info *info,
4ad4eba5
AM
3189 const char *soname,
3190 bfd_boolean do_it)
5a580b3a
AM
3191{
3192 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3193 bfd_size_type strindex;
3194
7e9f0867
AM
3195 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3196 return -1;
3197
5a580b3a 3198 hash_table = elf_hash_table (info);
5a580b3a
AM
3199 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3200 if (strindex == (bfd_size_type) -1)
3201 return -1;
3202
02be4619 3203 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3204 {
3205 asection *sdyn;
3206 const struct elf_backend_data *bed;
3207 bfd_byte *extdyn;
3208
3209 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3210 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3211 if (sdyn != NULL)
3212 for (extdyn = sdyn->contents;
3213 extdyn < sdyn->contents + sdyn->size;
3214 extdyn += bed->s->sizeof_dyn)
3215 {
3216 Elf_Internal_Dyn dyn;
5a580b3a 3217
7e9f0867
AM
3218 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3219 if (dyn.d_tag == DT_NEEDED
3220 && dyn.d_un.d_val == strindex)
3221 {
3222 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3223 return 1;
3224 }
3225 }
5a580b3a
AM
3226 }
3227
3228 if (do_it)
3229 {
7e9f0867
AM
3230 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3231 return -1;
3232
5a580b3a
AM
3233 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3234 return -1;
3235 }
3236 else
3237 /* We were just checking for existence of the tag. */
3238 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3239
3240 return 0;
3241}
3242
010e5ae2
AM
3243static bfd_boolean
3244on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3245{
3246 for (; needed != NULL; needed = needed->next)
1240be6b
AM
3247 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3248 && strcmp (soname, needed->name) == 0)
010e5ae2
AM
3249 return TRUE;
3250
3251 return FALSE;
3252}
3253
14160578 3254/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3255static int
3256elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3257{
3258 const struct elf_link_hash_entry *h1;
3259 const struct elf_link_hash_entry *h2;
10b7e05b 3260 bfd_signed_vma vdiff;
5a580b3a
AM
3261
3262 h1 = *(const struct elf_link_hash_entry **) arg1;
3263 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3264 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3265 if (vdiff != 0)
3266 return vdiff > 0 ? 1 : -1;
3267 else
3268 {
d3435ae8 3269 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3270 if (sdiff != 0)
3271 return sdiff > 0 ? 1 : -1;
3272 }
14160578
AM
3273 vdiff = h1->size - h2->size;
3274 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3275}
4ad4eba5 3276
5a580b3a
AM
3277/* This function is used to adjust offsets into .dynstr for
3278 dynamic symbols. This is called via elf_link_hash_traverse. */
3279
3280static bfd_boolean
3281elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3282{
a50b1753 3283 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3284
5a580b3a
AM
3285 if (h->dynindx != -1)
3286 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3287 return TRUE;
3288}
3289
3290/* Assign string offsets in .dynstr, update all structures referencing
3291 them. */
3292
4ad4eba5
AM
3293static bfd_boolean
3294elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3295{
3296 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3297 struct elf_link_local_dynamic_entry *entry;
3298 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3299 bfd *dynobj = hash_table->dynobj;
3300 asection *sdyn;
3301 bfd_size_type size;
3302 const struct elf_backend_data *bed;
3303 bfd_byte *extdyn;
3304
3305 _bfd_elf_strtab_finalize (dynstr);
3306 size = _bfd_elf_strtab_size (dynstr);
3307
3308 bed = get_elf_backend_data (dynobj);
3d4d4302 3309 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3310 BFD_ASSERT (sdyn != NULL);
3311
3312 /* Update all .dynamic entries referencing .dynstr strings. */
3313 for (extdyn = sdyn->contents;
eea6121a 3314 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3315 extdyn += bed->s->sizeof_dyn)
3316 {
3317 Elf_Internal_Dyn dyn;
3318
3319 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3320 switch (dyn.d_tag)
3321 {
3322 case DT_STRSZ:
3323 dyn.d_un.d_val = size;
3324 break;
3325 case DT_NEEDED:
3326 case DT_SONAME:
3327 case DT_RPATH:
3328 case DT_RUNPATH:
3329 case DT_FILTER:
3330 case DT_AUXILIARY:
7ee314fa
AM
3331 case DT_AUDIT:
3332 case DT_DEPAUDIT:
5a580b3a
AM
3333 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3334 break;
3335 default:
3336 continue;
3337 }
3338 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3339 }
3340
3341 /* Now update local dynamic symbols. */
3342 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3343 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3344 entry->isym.st_name);
3345
3346 /* And the rest of dynamic symbols. */
3347 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3348
3349 /* Adjust version definitions. */
3350 if (elf_tdata (output_bfd)->cverdefs)
3351 {
3352 asection *s;
3353 bfd_byte *p;
3354 bfd_size_type i;
3355 Elf_Internal_Verdef def;
3356 Elf_Internal_Verdaux defaux;
3357
3d4d4302 3358 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3359 p = s->contents;
3360 do
3361 {
3362 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3363 &def);
3364 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3365 if (def.vd_aux != sizeof (Elf_External_Verdef))
3366 continue;
5a580b3a
AM
3367 for (i = 0; i < def.vd_cnt; ++i)
3368 {
3369 _bfd_elf_swap_verdaux_in (output_bfd,
3370 (Elf_External_Verdaux *) p, &defaux);
3371 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3372 defaux.vda_name);
3373 _bfd_elf_swap_verdaux_out (output_bfd,
3374 &defaux, (Elf_External_Verdaux *) p);
3375 p += sizeof (Elf_External_Verdaux);
3376 }
3377 }
3378 while (def.vd_next);
3379 }
3380
3381 /* Adjust version references. */
3382 if (elf_tdata (output_bfd)->verref)
3383 {
3384 asection *s;
3385 bfd_byte *p;
3386 bfd_size_type i;
3387 Elf_Internal_Verneed need;
3388 Elf_Internal_Vernaux needaux;
3389
3d4d4302 3390 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3391 p = s->contents;
3392 do
3393 {
3394 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3395 &need);
3396 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3397 _bfd_elf_swap_verneed_out (output_bfd, &need,
3398 (Elf_External_Verneed *) p);
3399 p += sizeof (Elf_External_Verneed);
3400 for (i = 0; i < need.vn_cnt; ++i)
3401 {
3402 _bfd_elf_swap_vernaux_in (output_bfd,
3403 (Elf_External_Vernaux *) p, &needaux);
3404 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3405 needaux.vna_name);
3406 _bfd_elf_swap_vernaux_out (output_bfd,
3407 &needaux,
3408 (Elf_External_Vernaux *) p);
3409 p += sizeof (Elf_External_Vernaux);
3410 }
3411 }
3412 while (need.vn_next);
3413 }
3414
3415 return TRUE;
3416}
3417\f
13285a1b
AM
3418/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3419 The default is to only match when the INPUT and OUTPUT are exactly
3420 the same target. */
3421
3422bfd_boolean
3423_bfd_elf_default_relocs_compatible (const bfd_target *input,
3424 const bfd_target *output)
3425{
3426 return input == output;
3427}
3428
3429/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3430 This version is used when different targets for the same architecture
3431 are virtually identical. */
3432
3433bfd_boolean
3434_bfd_elf_relocs_compatible (const bfd_target *input,
3435 const bfd_target *output)
3436{
3437 const struct elf_backend_data *obed, *ibed;
3438
3439 if (input == output)
3440 return TRUE;
3441
3442 ibed = xvec_get_elf_backend_data (input);
3443 obed = xvec_get_elf_backend_data (output);
3444
3445 if (ibed->arch != obed->arch)
3446 return FALSE;
3447
3448 /* If both backends are using this function, deem them compatible. */
3449 return ibed->relocs_compatible == obed->relocs_compatible;
3450}
3451
e5034e59
AM
3452/* Make a special call to the linker "notice" function to tell it that
3453 we are about to handle an as-needed lib, or have finished
1b786873 3454 processing the lib. */
e5034e59
AM
3455
3456bfd_boolean
3457_bfd_elf_notice_as_needed (bfd *ibfd,
3458 struct bfd_link_info *info,
3459 enum notice_asneeded_action act)
3460{
46135103 3461 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3462}
3463
4ad4eba5
AM
3464/* Add symbols from an ELF object file to the linker hash table. */
3465
3466static bfd_boolean
3467elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3468{
a0c402a5 3469 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3470 Elf_Internal_Shdr *hdr;
3471 bfd_size_type symcount;
3472 bfd_size_type extsymcount;
3473 bfd_size_type extsymoff;
3474 struct elf_link_hash_entry **sym_hash;
3475 bfd_boolean dynamic;
3476 Elf_External_Versym *extversym = NULL;
3477 Elf_External_Versym *ever;
3478 struct elf_link_hash_entry *weaks;
3479 struct elf_link_hash_entry **nondeflt_vers = NULL;
3480 bfd_size_type nondeflt_vers_cnt = 0;
3481 Elf_Internal_Sym *isymbuf = NULL;
3482 Elf_Internal_Sym *isym;
3483 Elf_Internal_Sym *isymend;
3484 const struct elf_backend_data *bed;
3485 bfd_boolean add_needed;
66eb6687 3486 struct elf_link_hash_table *htab;
4ad4eba5 3487 bfd_size_type amt;
66eb6687 3488 void *alloc_mark = NULL;
4f87808c
AM
3489 struct bfd_hash_entry **old_table = NULL;
3490 unsigned int old_size = 0;
3491 unsigned int old_count = 0;
66eb6687 3492 void *old_tab = NULL;
66eb6687
AM
3493 void *old_ent;
3494 struct bfd_link_hash_entry *old_undefs = NULL;
3495 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3496 long old_dynsymcount = 0;
a4542f1b 3497 bfd_size_type old_dynstr_size = 0;
66eb6687 3498 size_t tabsize = 0;
db6a5d5f 3499 asection *s;
29a9f53e 3500 bfd_boolean just_syms;
4ad4eba5 3501
66eb6687 3502 htab = elf_hash_table (info);
4ad4eba5 3503 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3504
3505 if ((abfd->flags & DYNAMIC) == 0)
3506 dynamic = FALSE;
3507 else
3508 {
3509 dynamic = TRUE;
3510
3511 /* You can't use -r against a dynamic object. Also, there's no
3512 hope of using a dynamic object which does not exactly match
3513 the format of the output file. */
0e1862bb 3514 if (bfd_link_relocatable (info)
66eb6687 3515 || !is_elf_hash_table (htab)
f13a99db 3516 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3517 {
0e1862bb 3518 if (bfd_link_relocatable (info))
9a0789ec
NC
3519 bfd_set_error (bfd_error_invalid_operation);
3520 else
3521 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3522 goto error_return;
3523 }
3524 }
3525
a0c402a5
L
3526 ehdr = elf_elfheader (abfd);
3527 if (info->warn_alternate_em
3528 && bed->elf_machine_code != ehdr->e_machine
3529 && ((bed->elf_machine_alt1 != 0
3530 && ehdr->e_machine == bed->elf_machine_alt1)
3531 || (bed->elf_machine_alt2 != 0
3532 && ehdr->e_machine == bed->elf_machine_alt2)))
3533 info->callbacks->einfo
3534 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3535 ehdr->e_machine, abfd, bed->elf_machine_code);
3536
4ad4eba5
AM
3537 /* As a GNU extension, any input sections which are named
3538 .gnu.warning.SYMBOL are treated as warning symbols for the given
3539 symbol. This differs from .gnu.warning sections, which generate
3540 warnings when they are included in an output file. */
dd98f8d2 3541 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3542 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3543 {
db6a5d5f 3544 const char *name;
4ad4eba5 3545
db6a5d5f
AM
3546 name = bfd_get_section_name (abfd, s);
3547 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3548 {
db6a5d5f
AM
3549 char *msg;
3550 bfd_size_type sz;
3551
3552 name += sizeof ".gnu.warning." - 1;
3553
3554 /* If this is a shared object, then look up the symbol
3555 in the hash table. If it is there, and it is already
3556 been defined, then we will not be using the entry
3557 from this shared object, so we don't need to warn.
3558 FIXME: If we see the definition in a regular object
3559 later on, we will warn, but we shouldn't. The only
3560 fix is to keep track of what warnings we are supposed
3561 to emit, and then handle them all at the end of the
3562 link. */
3563 if (dynamic)
4ad4eba5 3564 {
db6a5d5f
AM
3565 struct elf_link_hash_entry *h;
3566
3567 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3568
3569 /* FIXME: What about bfd_link_hash_common? */
3570 if (h != NULL
3571 && (h->root.type == bfd_link_hash_defined
3572 || h->root.type == bfd_link_hash_defweak))
3573 continue;
3574 }
4ad4eba5 3575
db6a5d5f
AM
3576 sz = s->size;
3577 msg = (char *) bfd_alloc (abfd, sz + 1);
3578 if (msg == NULL)
3579 goto error_return;
4ad4eba5 3580
db6a5d5f
AM
3581 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3582 goto error_return;
4ad4eba5 3583
db6a5d5f 3584 msg[sz] = '\0';
4ad4eba5 3585
db6a5d5f
AM
3586 if (! (_bfd_generic_link_add_one_symbol
3587 (info, abfd, name, BSF_WARNING, s, 0, msg,
3588 FALSE, bed->collect, NULL)))
3589 goto error_return;
4ad4eba5 3590
0e1862bb 3591 if (bfd_link_executable (info))
db6a5d5f
AM
3592 {
3593 /* Clobber the section size so that the warning does
3594 not get copied into the output file. */
3595 s->size = 0;
11d2f718 3596
db6a5d5f
AM
3597 /* Also set SEC_EXCLUDE, so that symbols defined in
3598 the warning section don't get copied to the output. */
3599 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3600 }
3601 }
3602 }
3603
29a9f53e
L
3604 just_syms = ((s = abfd->sections) != NULL
3605 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3606
4ad4eba5
AM
3607 add_needed = TRUE;
3608 if (! dynamic)
3609 {
3610 /* If we are creating a shared library, create all the dynamic
3611 sections immediately. We need to attach them to something,
3612 so we attach them to this BFD, provided it is the right
bf89386a
L
3613 format and is not from ld --just-symbols. Always create the
3614 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3615 are no input BFD's of the same format as the output, we can't
3616 make a shared library. */
3617 if (!just_syms
bf89386a
L
3618 && (bfd_link_pic (info)
3619 || info->export_dynamic
3620 || info->dynamic)
66eb6687 3621 && is_elf_hash_table (htab)
f13a99db 3622 && info->output_bfd->xvec == abfd->xvec
66eb6687 3623 && !htab->dynamic_sections_created)
4ad4eba5
AM
3624 {
3625 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3626 goto error_return;
3627 }
3628 }
66eb6687 3629 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3630 goto error_return;
3631 else
3632 {
4ad4eba5 3633 const char *soname = NULL;
7ee314fa 3634 char *audit = NULL;
4ad4eba5
AM
3635 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3636 int ret;
3637
3638 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3639 ld shouldn't allow it. */
29a9f53e 3640 if (just_syms)
92fd189d 3641 abort ();
4ad4eba5
AM
3642
3643 /* If this dynamic lib was specified on the command line with
3644 --as-needed in effect, then we don't want to add a DT_NEEDED
3645 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3646 in by another lib's DT_NEEDED. When --no-add-needed is used
3647 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3648 any dynamic library in DT_NEEDED tags in the dynamic lib at
3649 all. */
3650 add_needed = (elf_dyn_lib_class (abfd)
3651 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3652 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3653
3654 s = bfd_get_section_by_name (abfd, ".dynamic");
3655 if (s != NULL)
3656 {
3657 bfd_byte *dynbuf;
3658 bfd_byte *extdyn;
cb33740c 3659 unsigned int elfsec;
4ad4eba5
AM
3660 unsigned long shlink;
3661
eea6121a 3662 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3663 {
3664error_free_dyn:
3665 free (dynbuf);
3666 goto error_return;
3667 }
4ad4eba5
AM
3668
3669 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3670 if (elfsec == SHN_BAD)
4ad4eba5
AM
3671 goto error_free_dyn;
3672 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3673
3674 for (extdyn = dynbuf;
eea6121a 3675 extdyn < dynbuf + s->size;
4ad4eba5
AM
3676 extdyn += bed->s->sizeof_dyn)
3677 {
3678 Elf_Internal_Dyn dyn;
3679
3680 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3681 if (dyn.d_tag == DT_SONAME)
3682 {
3683 unsigned int tagv = dyn.d_un.d_val;
3684 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3685 if (soname == NULL)
3686 goto error_free_dyn;
3687 }
3688 if (dyn.d_tag == DT_NEEDED)
3689 {
3690 struct bfd_link_needed_list *n, **pn;
3691 char *fnm, *anm;
3692 unsigned int tagv = dyn.d_un.d_val;
3693
3694 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3695 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3696 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3697 if (n == NULL || fnm == NULL)
3698 goto error_free_dyn;
3699 amt = strlen (fnm) + 1;
a50b1753 3700 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3701 if (anm == NULL)
3702 goto error_free_dyn;
3703 memcpy (anm, fnm, amt);
3704 n->name = anm;
3705 n->by = abfd;
3706 n->next = NULL;
66eb6687 3707 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3708 ;
3709 *pn = n;
3710 }
3711 if (dyn.d_tag == DT_RUNPATH)
3712 {
3713 struct bfd_link_needed_list *n, **pn;
3714 char *fnm, *anm;
3715 unsigned int tagv = dyn.d_un.d_val;
3716
3717 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3718 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3719 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3720 if (n == NULL || fnm == NULL)
3721 goto error_free_dyn;
3722 amt = strlen (fnm) + 1;
a50b1753 3723 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3724 if (anm == NULL)
3725 goto error_free_dyn;
3726 memcpy (anm, fnm, amt);
3727 n->name = anm;
3728 n->by = abfd;
3729 n->next = NULL;
3730 for (pn = & runpath;
3731 *pn != NULL;
3732 pn = &(*pn)->next)
3733 ;
3734 *pn = n;
3735 }
3736 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3737 if (!runpath && dyn.d_tag == DT_RPATH)
3738 {
3739 struct bfd_link_needed_list *n, **pn;
3740 char *fnm, *anm;
3741 unsigned int tagv = dyn.d_un.d_val;
3742
3743 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3744 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3745 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3746 if (n == NULL || fnm == NULL)
3747 goto error_free_dyn;
3748 amt = strlen (fnm) + 1;
a50b1753 3749 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3750 if (anm == NULL)
f8703194 3751 goto error_free_dyn;
4ad4eba5
AM
3752 memcpy (anm, fnm, amt);
3753 n->name = anm;
3754 n->by = abfd;
3755 n->next = NULL;
3756 for (pn = & rpath;
3757 *pn != NULL;
3758 pn = &(*pn)->next)
3759 ;
3760 *pn = n;
3761 }
7ee314fa
AM
3762 if (dyn.d_tag == DT_AUDIT)
3763 {
3764 unsigned int tagv = dyn.d_un.d_val;
3765 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3766 }
4ad4eba5
AM
3767 }
3768
3769 free (dynbuf);
3770 }
3771
3772 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3773 frees all more recently bfd_alloc'd blocks as well. */
3774 if (runpath)
3775 rpath = runpath;
3776
3777 if (rpath)
3778 {
3779 struct bfd_link_needed_list **pn;
66eb6687 3780 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3781 ;
3782 *pn = rpath;
3783 }
3784
3785 /* We do not want to include any of the sections in a dynamic
3786 object in the output file. We hack by simply clobbering the
3787 list of sections in the BFD. This could be handled more
3788 cleanly by, say, a new section flag; the existing
3789 SEC_NEVER_LOAD flag is not the one we want, because that one
3790 still implies that the section takes up space in the output
3791 file. */
3792 bfd_section_list_clear (abfd);
3793
4ad4eba5
AM
3794 /* Find the name to use in a DT_NEEDED entry that refers to this
3795 object. If the object has a DT_SONAME entry, we use it.
3796 Otherwise, if the generic linker stuck something in
3797 elf_dt_name, we use that. Otherwise, we just use the file
3798 name. */
3799 if (soname == NULL || *soname == '\0')
3800 {
3801 soname = elf_dt_name (abfd);
3802 if (soname == NULL || *soname == '\0')
3803 soname = bfd_get_filename (abfd);
3804 }
3805
3806 /* Save the SONAME because sometimes the linker emulation code
3807 will need to know it. */
3808 elf_dt_name (abfd) = soname;
3809
7e9f0867 3810 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3811 if (ret < 0)
3812 goto error_return;
3813
3814 /* If we have already included this dynamic object in the
3815 link, just ignore it. There is no reason to include a
3816 particular dynamic object more than once. */
3817 if (ret > 0)
3818 return TRUE;
7ee314fa
AM
3819
3820 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3821 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3822 }
3823
3824 /* If this is a dynamic object, we always link against the .dynsym
3825 symbol table, not the .symtab symbol table. The dynamic linker
3826 will only see the .dynsym symbol table, so there is no reason to
3827 look at .symtab for a dynamic object. */
3828
3829 if (! dynamic || elf_dynsymtab (abfd) == 0)
3830 hdr = &elf_tdata (abfd)->symtab_hdr;
3831 else
3832 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3833
3834 symcount = hdr->sh_size / bed->s->sizeof_sym;
3835
3836 /* The sh_info field of the symtab header tells us where the
3837 external symbols start. We don't care about the local symbols at
3838 this point. */
3839 if (elf_bad_symtab (abfd))
3840 {
3841 extsymcount = symcount;
3842 extsymoff = 0;
3843 }
3844 else
3845 {
3846 extsymcount = symcount - hdr->sh_info;
3847 extsymoff = hdr->sh_info;
3848 }
3849
f45794cb 3850 sym_hash = elf_sym_hashes (abfd);
012b2306 3851 if (extsymcount != 0)
4ad4eba5
AM
3852 {
3853 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3854 NULL, NULL, NULL);
3855 if (isymbuf == NULL)
3856 goto error_return;
3857
4ad4eba5 3858 if (sym_hash == NULL)
012b2306
AM
3859 {
3860 /* We store a pointer to the hash table entry for each
3861 external symbol. */
3862 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3863 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3864 if (sym_hash == NULL)
3865 goto error_free_sym;
3866 elf_sym_hashes (abfd) = sym_hash;
3867 }
4ad4eba5
AM
3868 }
3869
3870 if (dynamic)
3871 {
3872 /* Read in any version definitions. */
fc0e6df6
PB
3873 if (!_bfd_elf_slurp_version_tables (abfd,
3874 info->default_imported_symver))
4ad4eba5
AM
3875 goto error_free_sym;
3876
3877 /* Read in the symbol versions, but don't bother to convert them
3878 to internal format. */
3879 if (elf_dynversym (abfd) != 0)
3880 {
3881 Elf_Internal_Shdr *versymhdr;
3882
3883 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3884 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3885 if (extversym == NULL)
3886 goto error_free_sym;
3887 amt = versymhdr->sh_size;
3888 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3889 || bfd_bread (extversym, amt, abfd) != amt)
3890 goto error_free_vers;
3891 }
3892 }
3893
66eb6687
AM
3894 /* If we are loading an as-needed shared lib, save the symbol table
3895 state before we start adding symbols. If the lib turns out
3896 to be unneeded, restore the state. */
3897 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3898 {
3899 unsigned int i;
3900 size_t entsize;
3901
3902 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3903 {
3904 struct bfd_hash_entry *p;
2de92251 3905 struct elf_link_hash_entry *h;
66eb6687
AM
3906
3907 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3908 {
3909 h = (struct elf_link_hash_entry *) p;
3910 entsize += htab->root.table.entsize;
3911 if (h->root.type == bfd_link_hash_warning)
3912 entsize += htab->root.table.entsize;
3913 }
66eb6687
AM
3914 }
3915
3916 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 3917 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
3918 if (old_tab == NULL)
3919 goto error_free_vers;
3920
3921 /* Remember the current objalloc pointer, so that all mem for
3922 symbols added can later be reclaimed. */
3923 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3924 if (alloc_mark == NULL)
3925 goto error_free_vers;
3926
5061a885
AM
3927 /* Make a special call to the linker "notice" function to
3928 tell it that we are about to handle an as-needed lib. */
e5034e59 3929 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 3930 goto error_free_vers;
5061a885 3931
f45794cb
AM
3932 /* Clone the symbol table. Remember some pointers into the
3933 symbol table, and dynamic symbol count. */
3934 old_ent = (char *) old_tab + tabsize;
66eb6687 3935 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
3936 old_undefs = htab->root.undefs;
3937 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3938 old_table = htab->root.table.table;
3939 old_size = htab->root.table.size;
3940 old_count = htab->root.table.count;
66eb6687 3941 old_dynsymcount = htab->dynsymcount;
a4542f1b 3942 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
3943
3944 for (i = 0; i < htab->root.table.size; i++)
3945 {
3946 struct bfd_hash_entry *p;
2de92251 3947 struct elf_link_hash_entry *h;
66eb6687
AM
3948
3949 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3950 {
3951 memcpy (old_ent, p, htab->root.table.entsize);
3952 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3953 h = (struct elf_link_hash_entry *) p;
3954 if (h->root.type == bfd_link_hash_warning)
3955 {
3956 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3957 old_ent = (char *) old_ent + htab->root.table.entsize;
3958 }
66eb6687
AM
3959 }
3960 }
3961 }
4ad4eba5 3962
66eb6687 3963 weaks = NULL;
4ad4eba5
AM
3964 ever = extversym != NULL ? extversym + extsymoff : NULL;
3965 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3966 isym < isymend;
3967 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3968 {
3969 int bind;
3970 bfd_vma value;
af44c138 3971 asection *sec, *new_sec;
4ad4eba5
AM
3972 flagword flags;
3973 const char *name;
3974 struct elf_link_hash_entry *h;
90c984fc 3975 struct elf_link_hash_entry *hi;
4ad4eba5
AM
3976 bfd_boolean definition;
3977 bfd_boolean size_change_ok;
3978 bfd_boolean type_change_ok;
3979 bfd_boolean new_weakdef;
37a9e49a
L
3980 bfd_boolean new_weak;
3981 bfd_boolean old_weak;
4ad4eba5 3982 bfd_boolean override;
a4d8e49b 3983 bfd_boolean common;
4ad4eba5
AM
3984 unsigned int old_alignment;
3985 bfd *old_bfd;
6e33951e 3986 bfd_boolean matched;
4ad4eba5
AM
3987
3988 override = FALSE;
3989
3990 flags = BSF_NO_FLAGS;
3991 sec = NULL;
3992 value = isym->st_value;
a4d8e49b 3993 common = bed->common_definition (isym);
4ad4eba5
AM
3994
3995 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3996 switch (bind)
4ad4eba5 3997 {
3e7a7d11 3998 case STB_LOCAL:
4ad4eba5
AM
3999 /* This should be impossible, since ELF requires that all
4000 global symbols follow all local symbols, and that sh_info
4001 point to the first global symbol. Unfortunately, Irix 5
4002 screws this up. */
4003 continue;
3e7a7d11
NC
4004
4005 case STB_GLOBAL:
a4d8e49b 4006 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4007 flags = BSF_GLOBAL;
3e7a7d11
NC
4008 break;
4009
4010 case STB_WEAK:
4011 flags = BSF_WEAK;
4012 break;
4013
4014 case STB_GNU_UNIQUE:
4015 flags = BSF_GNU_UNIQUE;
4016 break;
4017
4018 default:
4ad4eba5 4019 /* Leave it up to the processor backend. */
3e7a7d11 4020 break;
4ad4eba5
AM
4021 }
4022
4023 if (isym->st_shndx == SHN_UNDEF)
4024 sec = bfd_und_section_ptr;
cb33740c
AM
4025 else if (isym->st_shndx == SHN_ABS)
4026 sec = bfd_abs_section_ptr;
4027 else if (isym->st_shndx == SHN_COMMON)
4028 {
4029 sec = bfd_com_section_ptr;
4030 /* What ELF calls the size we call the value. What ELF
4031 calls the value we call the alignment. */
4032 value = isym->st_size;
4033 }
4034 else
4ad4eba5
AM
4035 {
4036 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4037 if (sec == NULL)
4038 sec = bfd_abs_section_ptr;
dbaa2011 4039 else if (discarded_section (sec))
529fcb95 4040 {
e5d08002
L
4041 /* Symbols from discarded section are undefined. We keep
4042 its visibility. */
529fcb95
PB
4043 sec = bfd_und_section_ptr;
4044 isym->st_shndx = SHN_UNDEF;
4045 }
4ad4eba5
AM
4046 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4047 value -= sec->vma;
4048 }
4ad4eba5
AM
4049
4050 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4051 isym->st_name);
4052 if (name == NULL)
4053 goto error_free_vers;
4054
4055 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4056 && (abfd->flags & BFD_PLUGIN) != 0)
4057 {
4058 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4059
4060 if (xc == NULL)
4061 {
4062 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4063 | SEC_EXCLUDE);
4064 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4065 if (xc == NULL)
4066 goto error_free_vers;
4067 }
4068 sec = xc;
4069 }
4070 else if (isym->st_shndx == SHN_COMMON
4071 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4072 && !bfd_link_relocatable (info))
4ad4eba5
AM
4073 {
4074 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4075
4076 if (tcomm == NULL)
4077 {
02d00247
AM
4078 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4079 | SEC_LINKER_CREATED);
4080 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4081 if (tcomm == NULL)
4ad4eba5
AM
4082 goto error_free_vers;
4083 }
4084 sec = tcomm;
4085 }
66eb6687 4086 else if (bed->elf_add_symbol_hook)
4ad4eba5 4087 {
66eb6687
AM
4088 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4089 &sec, &value))
4ad4eba5
AM
4090 goto error_free_vers;
4091
4092 /* The hook function sets the name to NULL if this symbol
4093 should be skipped for some reason. */
4094 if (name == NULL)
4095 continue;
4096 }
4097
4098 /* Sanity check that all possibilities were handled. */
4099 if (sec == NULL)
4100 {
4101 bfd_set_error (bfd_error_bad_value);
4102 goto error_free_vers;
4103 }
4104
191c0c42
AM
4105 /* Silently discard TLS symbols from --just-syms. There's
4106 no way to combine a static TLS block with a new TLS block
4107 for this executable. */
4108 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4109 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4110 continue;
4111
4ad4eba5
AM
4112 if (bfd_is_und_section (sec)
4113 || bfd_is_com_section (sec))
4114 definition = FALSE;
4115 else
4116 definition = TRUE;
4117
4118 size_change_ok = FALSE;
66eb6687 4119 type_change_ok = bed->type_change_ok;
37a9e49a 4120 old_weak = FALSE;
6e33951e 4121 matched = FALSE;
4ad4eba5
AM
4122 old_alignment = 0;
4123 old_bfd = NULL;
af44c138 4124 new_sec = sec;
4ad4eba5 4125
66eb6687 4126 if (is_elf_hash_table (htab))
4ad4eba5
AM
4127 {
4128 Elf_Internal_Versym iver;
4129 unsigned int vernum = 0;
4130 bfd_boolean skip;
4131
fc0e6df6 4132 if (ever == NULL)
4ad4eba5 4133 {
fc0e6df6
PB
4134 if (info->default_imported_symver)
4135 /* Use the default symbol version created earlier. */
4136 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4137 else
4138 iver.vs_vers = 0;
4139 }
4140 else
4141 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4142
4143 vernum = iver.vs_vers & VERSYM_VERSION;
4144
4145 /* If this is a hidden symbol, or if it is not version
4146 1, we append the version name to the symbol name.
cc86ff91
EB
4147 However, we do not modify a non-hidden absolute symbol
4148 if it is not a function, because it might be the version
4149 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4150 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4151 || (vernum > 1
4152 && (!bfd_is_abs_section (sec)
4153 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4154 {
4155 const char *verstr;
4156 size_t namelen, verlen, newlen;
4157 char *newname, *p;
4158
4159 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4160 {
fc0e6df6
PB
4161 if (vernum > elf_tdata (abfd)->cverdefs)
4162 verstr = NULL;
4163 else if (vernum > 1)
4164 verstr =
4165 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4166 else
4167 verstr = "";
4ad4eba5 4168
fc0e6df6 4169 if (verstr == NULL)
4ad4eba5 4170 {
fc0e6df6
PB
4171 (*_bfd_error_handler)
4172 (_("%B: %s: invalid version %u (max %d)"),
4173 abfd, name, vernum,
4174 elf_tdata (abfd)->cverdefs);
4175 bfd_set_error (bfd_error_bad_value);
4176 goto error_free_vers;
4ad4eba5 4177 }
fc0e6df6
PB
4178 }
4179 else
4180 {
4181 /* We cannot simply test for the number of
4182 entries in the VERNEED section since the
4183 numbers for the needed versions do not start
4184 at 0. */
4185 Elf_Internal_Verneed *t;
4186
4187 verstr = NULL;
4188 for (t = elf_tdata (abfd)->verref;
4189 t != NULL;
4190 t = t->vn_nextref)
4ad4eba5 4191 {
fc0e6df6 4192 Elf_Internal_Vernaux *a;
4ad4eba5 4193
fc0e6df6
PB
4194 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4195 {
4196 if (a->vna_other == vernum)
4ad4eba5 4197 {
fc0e6df6
PB
4198 verstr = a->vna_nodename;
4199 break;
4ad4eba5 4200 }
4ad4eba5 4201 }
fc0e6df6
PB
4202 if (a != NULL)
4203 break;
4204 }
4205 if (verstr == NULL)
4206 {
4207 (*_bfd_error_handler)
4208 (_("%B: %s: invalid needed version %d"),
4209 abfd, name, vernum);
4210 bfd_set_error (bfd_error_bad_value);
4211 goto error_free_vers;
4ad4eba5 4212 }
4ad4eba5 4213 }
fc0e6df6
PB
4214
4215 namelen = strlen (name);
4216 verlen = strlen (verstr);
4217 newlen = namelen + verlen + 2;
4218 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4219 && isym->st_shndx != SHN_UNDEF)
4220 ++newlen;
4221
a50b1753 4222 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4223 if (newname == NULL)
4224 goto error_free_vers;
4225 memcpy (newname, name, namelen);
4226 p = newname + namelen;
4227 *p++ = ELF_VER_CHR;
4228 /* If this is a defined non-hidden version symbol,
4229 we add another @ to the name. This indicates the
4230 default version of the symbol. */
4231 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4232 && isym->st_shndx != SHN_UNDEF)
4233 *p++ = ELF_VER_CHR;
4234 memcpy (p, verstr, verlen + 1);
4235
4236 name = newname;
4ad4eba5
AM
4237 }
4238
cd3416da
AM
4239 /* If this symbol has default visibility and the user has
4240 requested we not re-export it, then mark it as hidden. */
a0d49154 4241 if (!bfd_is_und_section (sec)
cd3416da 4242 && !dynamic
ce875075 4243 && abfd->no_export
cd3416da
AM
4244 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4245 isym->st_other = (STV_HIDDEN
4246 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4247
4f3fedcf
AM
4248 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4249 sym_hash, &old_bfd, &old_weak,
4250 &old_alignment, &skip, &override,
6e33951e
L
4251 &type_change_ok, &size_change_ok,
4252 &matched))
4ad4eba5
AM
4253 goto error_free_vers;
4254
4255 if (skip)
4256 continue;
4257
6e33951e
L
4258 /* Override a definition only if the new symbol matches the
4259 existing one. */
4260 if (override && matched)
4ad4eba5
AM
4261 definition = FALSE;
4262
4263 h = *sym_hash;
4264 while (h->root.type == bfd_link_hash_indirect
4265 || h->root.type == bfd_link_hash_warning)
4266 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4267
4ad4eba5 4268 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4269 && vernum > 1
4270 && definition)
4271 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4272 }
4273
4274 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4275 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4276 (struct bfd_link_hash_entry **) sym_hash)))
4277 goto error_free_vers;
4278
4279 h = *sym_hash;
90c984fc
L
4280 /* We need to make sure that indirect symbol dynamic flags are
4281 updated. */
4282 hi = h;
4ad4eba5
AM
4283 while (h->root.type == bfd_link_hash_indirect
4284 || h->root.type == bfd_link_hash_warning)
4285 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4286
4ad4eba5
AM
4287 *sym_hash = h;
4288
37a9e49a 4289 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4290 new_weakdef = FALSE;
4291 if (dynamic
4292 && definition
37a9e49a 4293 && new_weak
fcb93ecf 4294 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4295 && is_elf_hash_table (htab)
f6e332e6 4296 && h->u.weakdef == NULL)
4ad4eba5
AM
4297 {
4298 /* Keep a list of all weak defined non function symbols from
4299 a dynamic object, using the weakdef field. Later in this
4300 function we will set the weakdef field to the correct
4301 value. We only put non-function symbols from dynamic
4302 objects on this list, because that happens to be the only
4303 time we need to know the normal symbol corresponding to a
4304 weak symbol, and the information is time consuming to
4305 figure out. If the weakdef field is not already NULL,
4306 then this symbol was already defined by some previous
4307 dynamic object, and we will be using that previous
4308 definition anyhow. */
4309
f6e332e6 4310 h->u.weakdef = weaks;
4ad4eba5
AM
4311 weaks = h;
4312 new_weakdef = TRUE;
4313 }
4314
4315 /* Set the alignment of a common symbol. */
a4d8e49b 4316 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4317 && h->root.type == bfd_link_hash_common)
4318 {
4319 unsigned int align;
4320
a4d8e49b 4321 if (common)
af44c138
L
4322 align = bfd_log2 (isym->st_value);
4323 else
4324 {
4325 /* The new symbol is a common symbol in a shared object.
4326 We need to get the alignment from the section. */
4327 align = new_sec->alignment_power;
4328 }
595213d4 4329 if (align > old_alignment)
4ad4eba5
AM
4330 h->root.u.c.p->alignment_power = align;
4331 else
4332 h->root.u.c.p->alignment_power = old_alignment;
4333 }
4334
66eb6687 4335 if (is_elf_hash_table (htab))
4ad4eba5 4336 {
4f3fedcf
AM
4337 /* Set a flag in the hash table entry indicating the type of
4338 reference or definition we just found. A dynamic symbol
4339 is one which is referenced or defined by both a regular
4340 object and a shared object. */
4341 bfd_boolean dynsym = FALSE;
4342
4343 /* Plugin symbols aren't normal. Don't set def_regular or
4344 ref_regular for them, or make them dynamic. */
4345 if ((abfd->flags & BFD_PLUGIN) != 0)
4346 ;
4347 else if (! dynamic)
4348 {
4349 if (! definition)
4350 {
4351 h->ref_regular = 1;
4352 if (bind != STB_WEAK)
4353 h->ref_regular_nonweak = 1;
4354 }
4355 else
4356 {
4357 h->def_regular = 1;
4358 if (h->def_dynamic)
4359 {
4360 h->def_dynamic = 0;
4361 h->ref_dynamic = 1;
4362 }
4363 }
4364
4365 /* If the indirect symbol has been forced local, don't
4366 make the real symbol dynamic. */
4367 if ((h == hi || !hi->forced_local)
0e1862bb 4368 && (bfd_link_dll (info)
4f3fedcf
AM
4369 || h->def_dynamic
4370 || h->ref_dynamic))
4371 dynsym = TRUE;
4372 }
4373 else
4374 {
4375 if (! definition)
4376 {
4377 h->ref_dynamic = 1;
4378 hi->ref_dynamic = 1;
4379 }
4380 else
4381 {
4382 h->def_dynamic = 1;
4383 hi->def_dynamic = 1;
4384 }
4385
4386 /* If the indirect symbol has been forced local, don't
4387 make the real symbol dynamic. */
4388 if ((h == hi || !hi->forced_local)
4389 && (h->def_regular
4390 || h->ref_regular
4391 || (h->u.weakdef != NULL
4392 && ! new_weakdef
4393 && h->u.weakdef->dynindx != -1)))
4394 dynsym = TRUE;
4395 }
4396
4397 /* Check to see if we need to add an indirect symbol for
4398 the default name. */
4399 if (definition
4400 || (!override && h->root.type == bfd_link_hash_common))
4401 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4402 sec, value, &old_bfd, &dynsym))
4403 goto error_free_vers;
4ad4eba5
AM
4404
4405 /* Check the alignment when a common symbol is involved. This
4406 can change when a common symbol is overridden by a normal
4407 definition or a common symbol is ignored due to the old
4408 normal definition. We need to make sure the maximum
4409 alignment is maintained. */
a4d8e49b 4410 if ((old_alignment || common)
4ad4eba5
AM
4411 && h->root.type != bfd_link_hash_common)
4412 {
4413 unsigned int common_align;
4414 unsigned int normal_align;
4415 unsigned int symbol_align;
4416 bfd *normal_bfd;
4417 bfd *common_bfd;
4418
3a81e825
AM
4419 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4420 || h->root.type == bfd_link_hash_defweak);
4421
4ad4eba5
AM
4422 symbol_align = ffs (h->root.u.def.value) - 1;
4423 if (h->root.u.def.section->owner != NULL
4424 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4425 {
4426 normal_align = h->root.u.def.section->alignment_power;
4427 if (normal_align > symbol_align)
4428 normal_align = symbol_align;
4429 }
4430 else
4431 normal_align = symbol_align;
4432
4433 if (old_alignment)
4434 {
4435 common_align = old_alignment;
4436 common_bfd = old_bfd;
4437 normal_bfd = abfd;
4438 }
4439 else
4440 {
4441 common_align = bfd_log2 (isym->st_value);
4442 common_bfd = abfd;
4443 normal_bfd = old_bfd;
4444 }
4445
4446 if (normal_align < common_align)
d07676f8
NC
4447 {
4448 /* PR binutils/2735 */
4449 if (normal_bfd == NULL)
4450 (*_bfd_error_handler)
4f3fedcf
AM
4451 (_("Warning: alignment %u of common symbol `%s' in %B is"
4452 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4453 common_bfd, h->root.u.def.section,
4454 1 << common_align, name, 1 << normal_align);
4455 else
4456 (*_bfd_error_handler)
4457 (_("Warning: alignment %u of symbol `%s' in %B"
4458 " is smaller than %u in %B"),
4459 normal_bfd, common_bfd,
4460 1 << normal_align, name, 1 << common_align);
4461 }
4ad4eba5
AM
4462 }
4463
83ad0046 4464 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4465 if (isym->st_size != 0
4466 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4467 && (definition || h->size == 0))
4468 {
83ad0046
L
4469 if (h->size != 0
4470 && h->size != isym->st_size
4471 && ! size_change_ok)
4ad4eba5 4472 (*_bfd_error_handler)
d003868e
AM
4473 (_("Warning: size of symbol `%s' changed"
4474 " from %lu in %B to %lu in %B"),
4475 old_bfd, abfd,
4ad4eba5 4476 name, (unsigned long) h->size,
d003868e 4477 (unsigned long) isym->st_size);
4ad4eba5
AM
4478
4479 h->size = isym->st_size;
4480 }
4481
4482 /* If this is a common symbol, then we always want H->SIZE
4483 to be the size of the common symbol. The code just above
4484 won't fix the size if a common symbol becomes larger. We
4485 don't warn about a size change here, because that is
4f3fedcf 4486 covered by --warn-common. Allow changes between different
fcb93ecf 4487 function types. */
4ad4eba5
AM
4488 if (h->root.type == bfd_link_hash_common)
4489 h->size = h->root.u.c.size;
4490
4491 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4492 && ((definition && !new_weak)
4493 || (old_weak && h->root.type == bfd_link_hash_common)
4494 || h->type == STT_NOTYPE))
4ad4eba5 4495 {
2955ec4c
L
4496 unsigned int type = ELF_ST_TYPE (isym->st_info);
4497
4498 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4499 symbol. */
4500 if (type == STT_GNU_IFUNC
4501 && (abfd->flags & DYNAMIC) != 0)
4502 type = STT_FUNC;
4ad4eba5 4503
2955ec4c
L
4504 if (h->type != type)
4505 {
4506 if (h->type != STT_NOTYPE && ! type_change_ok)
4507 (*_bfd_error_handler)
4508 (_("Warning: type of symbol `%s' changed"
4509 " from %d to %d in %B"),
4510 abfd, name, h->type, type);
4511
4512 h->type = type;
4513 }
4ad4eba5
AM
4514 }
4515
54ac0771 4516 /* Merge st_other field. */
b8417128 4517 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4518
c3df8c14 4519 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4520 if (definition
4521 && (sec->flags & SEC_DEBUGGING)
4522 && !bfd_link_relocatable (info))
c3df8c14
AM
4523 dynsym = FALSE;
4524
4f3fedcf
AM
4525 /* Nor should we make plugin symbols dynamic. */
4526 if ((abfd->flags & BFD_PLUGIN) != 0)
4527 dynsym = FALSE;
4528
35fc36a8 4529 if (definition)
35399224
L
4530 {
4531 h->target_internal = isym->st_target_internal;
4532 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4533 }
35fc36a8 4534
4ad4eba5
AM
4535 if (definition && !dynamic)
4536 {
4537 char *p = strchr (name, ELF_VER_CHR);
4538 if (p != NULL && p[1] != ELF_VER_CHR)
4539 {
4540 /* Queue non-default versions so that .symver x, x@FOO
4541 aliases can be checked. */
66eb6687 4542 if (!nondeflt_vers)
4ad4eba5 4543 {
66eb6687
AM
4544 amt = ((isymend - isym + 1)
4545 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4546 nondeflt_vers
4547 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4548 if (!nondeflt_vers)
4549 goto error_free_vers;
4ad4eba5 4550 }
66eb6687 4551 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4552 }
4553 }
4554
4555 if (dynsym && h->dynindx == -1)
4556 {
c152c796 4557 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4558 goto error_free_vers;
f6e332e6 4559 if (h->u.weakdef != NULL
4ad4eba5 4560 && ! new_weakdef
f6e332e6 4561 && h->u.weakdef->dynindx == -1)
4ad4eba5 4562 {
66eb6687 4563 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4564 goto error_free_vers;
4565 }
4566 }
4567 else if (dynsym && h->dynindx != -1)
4568 /* If the symbol already has a dynamic index, but
4569 visibility says it should not be visible, turn it into
4570 a local symbol. */
4571 switch (ELF_ST_VISIBILITY (h->other))
4572 {
4573 case STV_INTERNAL:
4574 case STV_HIDDEN:
4575 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4576 dynsym = FALSE;
4577 break;
4578 }
4579
aef28989
L
4580 /* Don't add DT_NEEDED for references from the dummy bfd nor
4581 for unmatched symbol. */
4ad4eba5 4582 if (!add_needed
aef28989 4583 && matched
4ad4eba5 4584 && definition
010e5ae2 4585 && ((dynsym
ffa9430d 4586 && h->ref_regular_nonweak
4f3fedcf
AM
4587 && (old_bfd == NULL
4588 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4589 || (h->ref_dynamic_nonweak
010e5ae2
AM
4590 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4591 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4592 {
4593 int ret;
4594 const char *soname = elf_dt_name (abfd);
4595
16e4ecc0
AM
4596 info->callbacks->minfo ("%!", soname, old_bfd,
4597 h->root.root.string);
4598
4ad4eba5
AM
4599 /* A symbol from a library loaded via DT_NEEDED of some
4600 other library is referenced by a regular object.
e56f61be 4601 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4602 --no-add-needed is used and the reference was not
4603 a weak one. */
4f3fedcf 4604 if (old_bfd != NULL
b918acf9 4605 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4606 {
4607 (*_bfd_error_handler)
3cbc5de0 4608 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4609 old_bfd, name);
ff5ac77b 4610 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4611 goto error_free_vers;
4612 }
4613
a50b1753 4614 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4615 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4616
4ad4eba5 4617 add_needed = TRUE;
7e9f0867 4618 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4619 if (ret < 0)
4620 goto error_free_vers;
4621
4622 BFD_ASSERT (ret == 0);
4623 }
4624 }
4625 }
4626
66eb6687
AM
4627 if (extversym != NULL)
4628 {
4629 free (extversym);
4630 extversym = NULL;
4631 }
4632
4633 if (isymbuf != NULL)
4634 {
4635 free (isymbuf);
4636 isymbuf = NULL;
4637 }
4638
4639 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4640 {
4641 unsigned int i;
4642
4643 /* Restore the symbol table. */
f45794cb
AM
4644 old_ent = (char *) old_tab + tabsize;
4645 memset (elf_sym_hashes (abfd), 0,
4646 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4647 htab->root.table.table = old_table;
4648 htab->root.table.size = old_size;
4649 htab->root.table.count = old_count;
66eb6687 4650 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4651 htab->root.undefs = old_undefs;
4652 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4653 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4654 for (i = 0; i < htab->root.table.size; i++)
4655 {
4656 struct bfd_hash_entry *p;
4657 struct elf_link_hash_entry *h;
3e0882af
L
4658 bfd_size_type size;
4659 unsigned int alignment_power;
66eb6687
AM
4660
4661 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4662 {
4663 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4664 if (h->root.type == bfd_link_hash_warning)
4665 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4666 if (h->dynindx >= old_dynsymcount
4667 && h->dynstr_index < old_dynstr_size)
66eb6687 4668 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4669
3e0882af
L
4670 /* Preserve the maximum alignment and size for common
4671 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4672 since it can still be loaded at run time by another
3e0882af
L
4673 dynamic lib. */
4674 if (h->root.type == bfd_link_hash_common)
4675 {
4676 size = h->root.u.c.size;
4677 alignment_power = h->root.u.c.p->alignment_power;
4678 }
4679 else
4680 {
4681 size = 0;
4682 alignment_power = 0;
4683 }
66eb6687
AM
4684 memcpy (p, old_ent, htab->root.table.entsize);
4685 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4686 h = (struct elf_link_hash_entry *) p;
4687 if (h->root.type == bfd_link_hash_warning)
4688 {
4689 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4690 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4691 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4692 }
a4542f1b 4693 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4694 {
4695 if (size > h->root.u.c.size)
4696 h->root.u.c.size = size;
4697 if (alignment_power > h->root.u.c.p->alignment_power)
4698 h->root.u.c.p->alignment_power = alignment_power;
4699 }
66eb6687
AM
4700 }
4701 }
4702
5061a885
AM
4703 /* Make a special call to the linker "notice" function to
4704 tell it that symbols added for crefs may need to be removed. */
e5034e59 4705 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4706 goto error_free_vers;
5061a885 4707
66eb6687
AM
4708 free (old_tab);
4709 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4710 alloc_mark);
4711 if (nondeflt_vers != NULL)
4712 free (nondeflt_vers);
4713 return TRUE;
4714 }
2de92251 4715
66eb6687
AM
4716 if (old_tab != NULL)
4717 {
e5034e59 4718 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4719 goto error_free_vers;
66eb6687
AM
4720 free (old_tab);
4721 old_tab = NULL;
4722 }
4723
c6e8a9a8
L
4724 /* Now that all the symbols from this input file are created, if
4725 not performing a relocatable link, handle .symver foo, foo@BAR
4726 such that any relocs against foo become foo@BAR. */
0e1862bb 4727 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4728 {
4729 bfd_size_type cnt, symidx;
4730
4731 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4732 {
4733 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4734 char *shortname, *p;
4735
4736 p = strchr (h->root.root.string, ELF_VER_CHR);
4737 if (p == NULL
4738 || (h->root.type != bfd_link_hash_defined
4739 && h->root.type != bfd_link_hash_defweak))
4740 continue;
4741
4742 amt = p - h->root.root.string;
a50b1753 4743 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4744 if (!shortname)
4745 goto error_free_vers;
4ad4eba5
AM
4746 memcpy (shortname, h->root.root.string, amt);
4747 shortname[amt] = '\0';
4748
4749 hi = (struct elf_link_hash_entry *)
66eb6687 4750 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4751 FALSE, FALSE, FALSE);
4752 if (hi != NULL
4753 && hi->root.type == h->root.type
4754 && hi->root.u.def.value == h->root.u.def.value
4755 && hi->root.u.def.section == h->root.u.def.section)
4756 {
4757 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4758 hi->root.type = bfd_link_hash_indirect;
4759 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4760 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4761 sym_hash = elf_sym_hashes (abfd);
4762 if (sym_hash)
4763 for (symidx = 0; symidx < extsymcount; ++symidx)
4764 if (sym_hash[symidx] == hi)
4765 {
4766 sym_hash[symidx] = h;
4767 break;
4768 }
4769 }
4770 free (shortname);
4771 }
4772 free (nondeflt_vers);
4773 nondeflt_vers = NULL;
4774 }
4775
4ad4eba5
AM
4776 /* Now set the weakdefs field correctly for all the weak defined
4777 symbols we found. The only way to do this is to search all the
4778 symbols. Since we only need the information for non functions in
4779 dynamic objects, that's the only time we actually put anything on
4780 the list WEAKS. We need this information so that if a regular
4781 object refers to a symbol defined weakly in a dynamic object, the
4782 real symbol in the dynamic object is also put in the dynamic
4783 symbols; we also must arrange for both symbols to point to the
4784 same memory location. We could handle the general case of symbol
4785 aliasing, but a general symbol alias can only be generated in
4786 assembler code, handling it correctly would be very time
4787 consuming, and other ELF linkers don't handle general aliasing
4788 either. */
4789 if (weaks != NULL)
4790 {
4791 struct elf_link_hash_entry **hpp;
4792 struct elf_link_hash_entry **hppend;
4793 struct elf_link_hash_entry **sorted_sym_hash;
4794 struct elf_link_hash_entry *h;
4795 size_t sym_count;
4796
4797 /* Since we have to search the whole symbol list for each weak
4798 defined symbol, search time for N weak defined symbols will be
4799 O(N^2). Binary search will cut it down to O(NlogN). */
4800 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4801 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4802 if (sorted_sym_hash == NULL)
4803 goto error_return;
4804 sym_hash = sorted_sym_hash;
4805 hpp = elf_sym_hashes (abfd);
4806 hppend = hpp + extsymcount;
4807 sym_count = 0;
4808 for (; hpp < hppend; hpp++)
4809 {
4810 h = *hpp;
4811 if (h != NULL
4812 && h->root.type == bfd_link_hash_defined
fcb93ecf 4813 && !bed->is_function_type (h->type))
4ad4eba5
AM
4814 {
4815 *sym_hash = h;
4816 sym_hash++;
4817 sym_count++;
4818 }
4819 }
4820
4821 qsort (sorted_sym_hash, sym_count,
4822 sizeof (struct elf_link_hash_entry *),
4823 elf_sort_symbol);
4824
4825 while (weaks != NULL)
4826 {
4827 struct elf_link_hash_entry *hlook;
4828 asection *slook;
4829 bfd_vma vlook;
ed54588d 4830 size_t i, j, idx = 0;
4ad4eba5
AM
4831
4832 hlook = weaks;
f6e332e6
AM
4833 weaks = hlook->u.weakdef;
4834 hlook->u.weakdef = NULL;
4ad4eba5
AM
4835
4836 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4837 || hlook->root.type == bfd_link_hash_defweak
4838 || hlook->root.type == bfd_link_hash_common
4839 || hlook->root.type == bfd_link_hash_indirect);
4840 slook = hlook->root.u.def.section;
4841 vlook = hlook->root.u.def.value;
4842
4ad4eba5
AM
4843 i = 0;
4844 j = sym_count;
14160578 4845 while (i != j)
4ad4eba5
AM
4846 {
4847 bfd_signed_vma vdiff;
4848 idx = (i + j) / 2;
14160578 4849 h = sorted_sym_hash[idx];
4ad4eba5
AM
4850 vdiff = vlook - h->root.u.def.value;
4851 if (vdiff < 0)
4852 j = idx;
4853 else if (vdiff > 0)
4854 i = idx + 1;
4855 else
4856 {
d3435ae8 4857 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4858 if (sdiff < 0)
4859 j = idx;
4860 else if (sdiff > 0)
4861 i = idx + 1;
4862 else
14160578 4863 break;
4ad4eba5
AM
4864 }
4865 }
4866
4867 /* We didn't find a value/section match. */
14160578 4868 if (i == j)
4ad4eba5
AM
4869 continue;
4870
14160578
AM
4871 /* With multiple aliases, or when the weak symbol is already
4872 strongly defined, we have multiple matching symbols and
4873 the binary search above may land on any of them. Step
4874 one past the matching symbol(s). */
4875 while (++idx != j)
4876 {
4877 h = sorted_sym_hash[idx];
4878 if (h->root.u.def.section != slook
4879 || h->root.u.def.value != vlook)
4880 break;
4881 }
4882
4883 /* Now look back over the aliases. Since we sorted by size
4884 as well as value and section, we'll choose the one with
4885 the largest size. */
4886 while (idx-- != i)
4ad4eba5 4887 {
14160578 4888 h = sorted_sym_hash[idx];
4ad4eba5
AM
4889
4890 /* Stop if value or section doesn't match. */
14160578
AM
4891 if (h->root.u.def.section != slook
4892 || h->root.u.def.value != vlook)
4ad4eba5
AM
4893 break;
4894 else if (h != hlook)
4895 {
f6e332e6 4896 hlook->u.weakdef = h;
4ad4eba5
AM
4897
4898 /* If the weak definition is in the list of dynamic
4899 symbols, make sure the real definition is put
4900 there as well. */
4901 if (hlook->dynindx != -1 && h->dynindx == -1)
4902 {
c152c796 4903 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4904 {
4905 err_free_sym_hash:
4906 free (sorted_sym_hash);
4907 goto error_return;
4908 }
4ad4eba5
AM
4909 }
4910
4911 /* If the real definition is in the list of dynamic
4912 symbols, make sure the weak definition is put
4913 there as well. If we don't do this, then the
4914 dynamic loader might not merge the entries for the
4915 real definition and the weak definition. */
4916 if (h->dynindx != -1 && hlook->dynindx == -1)
4917 {
c152c796 4918 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4919 goto err_free_sym_hash;
4ad4eba5
AM
4920 }
4921 break;
4922 }
4923 }
4924 }
4925
4926 free (sorted_sym_hash);
4927 }
4928
33177bb1
AM
4929 if (bed->check_directives
4930 && !(*bed->check_directives) (abfd, info))
4931 return FALSE;
85fbca6a 4932
4ad4eba5
AM
4933 /* If this object is the same format as the output object, and it is
4934 not a shared library, then let the backend look through the
4935 relocs.
4936
4937 This is required to build global offset table entries and to
4938 arrange for dynamic relocs. It is not required for the
4939 particular common case of linking non PIC code, even when linking
4940 against shared libraries, but unfortunately there is no way of
4941 knowing whether an object file has been compiled PIC or not.
4942 Looking through the relocs is not particularly time consuming.
4943 The problem is that we must either (1) keep the relocs in memory,
4944 which causes the linker to require additional runtime memory or
4945 (2) read the relocs twice from the input file, which wastes time.
4946 This would be a good case for using mmap.
4947
4948 I have no idea how to handle linking PIC code into a file of a
4949 different format. It probably can't be done. */
4ad4eba5 4950 if (! dynamic
66eb6687 4951 && is_elf_hash_table (htab)
13285a1b 4952 && bed->check_relocs != NULL
39334f3a 4953 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4954 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4955 {
4956 asection *o;
4957
4958 for (o = abfd->sections; o != NULL; o = o->next)
4959 {
4960 Elf_Internal_Rela *internal_relocs;
4961 bfd_boolean ok;
4962
4963 if ((o->flags & SEC_RELOC) == 0
4964 || o->reloc_count == 0
4965 || ((info->strip == strip_all || info->strip == strip_debugger)
4966 && (o->flags & SEC_DEBUGGING) != 0)
4967 || bfd_is_abs_section (o->output_section))
4968 continue;
4969
4970 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4971 info->keep_memory);
4972 if (internal_relocs == NULL)
4973 goto error_return;
4974
66eb6687 4975 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4976
4977 if (elf_section_data (o)->relocs != internal_relocs)
4978 free (internal_relocs);
4979
4980 if (! ok)
4981 goto error_return;
4982 }
4983 }
4984
4985 /* If this is a non-traditional link, try to optimize the handling
4986 of the .stab/.stabstr sections. */
4987 if (! dynamic
4988 && ! info->traditional_format
66eb6687 4989 && is_elf_hash_table (htab)
4ad4eba5
AM
4990 && (info->strip != strip_all && info->strip != strip_debugger))
4991 {
4992 asection *stabstr;
4993
4994 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4995 if (stabstr != NULL)
4996 {
4997 bfd_size_type string_offset = 0;
4998 asection *stab;
4999
5000 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5001 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5002 && (!stab->name[5] ||
5003 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5004 && (stab->flags & SEC_MERGE) == 0
5005 && !bfd_is_abs_section (stab->output_section))
5006 {
5007 struct bfd_elf_section_data *secdata;
5008
5009 secdata = elf_section_data (stab);
66eb6687
AM
5010 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5011 stabstr, &secdata->sec_info,
4ad4eba5
AM
5012 &string_offset))
5013 goto error_return;
5014 if (secdata->sec_info)
dbaa2011 5015 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5016 }
5017 }
5018 }
5019
66eb6687 5020 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5021 {
5022 /* Add this bfd to the loaded list. */
5023 struct elf_link_loaded_list *n;
5024
ca4be51c 5025 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5026 if (n == NULL)
5027 goto error_return;
5028 n->abfd = abfd;
66eb6687
AM
5029 n->next = htab->loaded;
5030 htab->loaded = n;
4ad4eba5
AM
5031 }
5032
5033 return TRUE;
5034
5035 error_free_vers:
66eb6687
AM
5036 if (old_tab != NULL)
5037 free (old_tab);
4ad4eba5
AM
5038 if (nondeflt_vers != NULL)
5039 free (nondeflt_vers);
5040 if (extversym != NULL)
5041 free (extversym);
5042 error_free_sym:
5043 if (isymbuf != NULL)
5044 free (isymbuf);
5045 error_return:
5046 return FALSE;
5047}
5048
8387904d
AM
5049/* Return the linker hash table entry of a symbol that might be
5050 satisfied by an archive symbol. Return -1 on error. */
5051
5052struct elf_link_hash_entry *
5053_bfd_elf_archive_symbol_lookup (bfd *abfd,
5054 struct bfd_link_info *info,
5055 const char *name)
5056{
5057 struct elf_link_hash_entry *h;
5058 char *p, *copy;
5059 size_t len, first;
5060
2a41f396 5061 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5062 if (h != NULL)
5063 return h;
5064
5065 /* If this is a default version (the name contains @@), look up the
5066 symbol again with only one `@' as well as without the version.
5067 The effect is that references to the symbol with and without the
5068 version will be matched by the default symbol in the archive. */
5069
5070 p = strchr (name, ELF_VER_CHR);
5071 if (p == NULL || p[1] != ELF_VER_CHR)
5072 return h;
5073
5074 /* First check with only one `@'. */
5075 len = strlen (name);
a50b1753 5076 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5077 if (copy == NULL)
5078 return (struct elf_link_hash_entry *) 0 - 1;
5079
5080 first = p - name + 1;
5081 memcpy (copy, name, first);
5082 memcpy (copy + first, name + first + 1, len - first);
5083
2a41f396 5084 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5085 if (h == NULL)
5086 {
5087 /* We also need to check references to the symbol without the
5088 version. */
5089 copy[first - 1] = '\0';
5090 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5091 FALSE, FALSE, TRUE);
8387904d
AM
5092 }
5093
5094 bfd_release (abfd, copy);
5095 return h;
5096}
5097
0ad989f9 5098/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5099 don't use _bfd_generic_link_add_archive_symbols because we need to
5100 handle versioned symbols.
0ad989f9
L
5101
5102 Fortunately, ELF archive handling is simpler than that done by
5103 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5104 oddities. In ELF, if we find a symbol in the archive map, and the
5105 symbol is currently undefined, we know that we must pull in that
5106 object file.
5107
5108 Unfortunately, we do have to make multiple passes over the symbol
5109 table until nothing further is resolved. */
5110
4ad4eba5
AM
5111static bfd_boolean
5112elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5113{
5114 symindex c;
13e570f8 5115 unsigned char *included = NULL;
0ad989f9
L
5116 carsym *symdefs;
5117 bfd_boolean loop;
5118 bfd_size_type amt;
8387904d
AM
5119 const struct elf_backend_data *bed;
5120 struct elf_link_hash_entry * (*archive_symbol_lookup)
5121 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5122
5123 if (! bfd_has_map (abfd))
5124 {
5125 /* An empty archive is a special case. */
5126 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5127 return TRUE;
5128 bfd_set_error (bfd_error_no_armap);
5129 return FALSE;
5130 }
5131
5132 /* Keep track of all symbols we know to be already defined, and all
5133 files we know to be already included. This is to speed up the
5134 second and subsequent passes. */
5135 c = bfd_ardata (abfd)->symdef_count;
5136 if (c == 0)
5137 return TRUE;
5138 amt = c;
13e570f8
AM
5139 amt *= sizeof (*included);
5140 included = (unsigned char *) bfd_zmalloc (amt);
5141 if (included == NULL)
5142 return FALSE;
0ad989f9
L
5143
5144 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5145 bed = get_elf_backend_data (abfd);
5146 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5147
5148 do
5149 {
5150 file_ptr last;
5151 symindex i;
5152 carsym *symdef;
5153 carsym *symdefend;
5154
5155 loop = FALSE;
5156 last = -1;
5157
5158 symdef = symdefs;
5159 symdefend = symdef + c;
5160 for (i = 0; symdef < symdefend; symdef++, i++)
5161 {
5162 struct elf_link_hash_entry *h;
5163 bfd *element;
5164 struct bfd_link_hash_entry *undefs_tail;
5165 symindex mark;
5166
13e570f8 5167 if (included[i])
0ad989f9
L
5168 continue;
5169 if (symdef->file_offset == last)
5170 {
5171 included[i] = TRUE;
5172 continue;
5173 }
5174
8387904d
AM
5175 h = archive_symbol_lookup (abfd, info, symdef->name);
5176 if (h == (struct elf_link_hash_entry *) 0 - 1)
5177 goto error_return;
0ad989f9
L
5178
5179 if (h == NULL)
5180 continue;
5181
5182 if (h->root.type == bfd_link_hash_common)
5183 {
5184 /* We currently have a common symbol. The archive map contains
5185 a reference to this symbol, so we may want to include it. We
5186 only want to include it however, if this archive element
5187 contains a definition of the symbol, not just another common
5188 declaration of it.
5189
5190 Unfortunately some archivers (including GNU ar) will put
5191 declarations of common symbols into their archive maps, as
5192 well as real definitions, so we cannot just go by the archive
5193 map alone. Instead we must read in the element's symbol
5194 table and check that to see what kind of symbol definition
5195 this is. */
5196 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5197 continue;
5198 }
5199 else if (h->root.type != bfd_link_hash_undefined)
5200 {
5201 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5202 /* Symbol must be defined. Don't check it again. */
5203 included[i] = TRUE;
0ad989f9
L
5204 continue;
5205 }
5206
5207 /* We need to include this archive member. */
5208 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5209 if (element == NULL)
5210 goto error_return;
5211
5212 if (! bfd_check_format (element, bfd_object))
5213 goto error_return;
5214
0ad989f9
L
5215 undefs_tail = info->hash->undefs_tail;
5216
0e144ba7
AM
5217 if (!(*info->callbacks
5218 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5219 goto error_return;
0e144ba7 5220 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5221 goto error_return;
5222
5223 /* If there are any new undefined symbols, we need to make
5224 another pass through the archive in order to see whether
5225 they can be defined. FIXME: This isn't perfect, because
5226 common symbols wind up on undefs_tail and because an
5227 undefined symbol which is defined later on in this pass
5228 does not require another pass. This isn't a bug, but it
5229 does make the code less efficient than it could be. */
5230 if (undefs_tail != info->hash->undefs_tail)
5231 loop = TRUE;
5232
5233 /* Look backward to mark all symbols from this object file
5234 which we have already seen in this pass. */
5235 mark = i;
5236 do
5237 {
5238 included[mark] = TRUE;
5239 if (mark == 0)
5240 break;
5241 --mark;
5242 }
5243 while (symdefs[mark].file_offset == symdef->file_offset);
5244
5245 /* We mark subsequent symbols from this object file as we go
5246 on through the loop. */
5247 last = symdef->file_offset;
5248 }
5249 }
5250 while (loop);
5251
0ad989f9
L
5252 free (included);
5253
5254 return TRUE;
5255
5256 error_return:
0ad989f9
L
5257 if (included != NULL)
5258 free (included);
5259 return FALSE;
5260}
4ad4eba5
AM
5261
5262/* Given an ELF BFD, add symbols to the global hash table as
5263 appropriate. */
5264
5265bfd_boolean
5266bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5267{
5268 switch (bfd_get_format (abfd))
5269 {
5270 case bfd_object:
5271 return elf_link_add_object_symbols (abfd, info);
5272 case bfd_archive:
5273 return elf_link_add_archive_symbols (abfd, info);
5274 default:
5275 bfd_set_error (bfd_error_wrong_format);
5276 return FALSE;
5277 }
5278}
5a580b3a 5279\f
14b1c01e
AM
5280struct hash_codes_info
5281{
5282 unsigned long *hashcodes;
5283 bfd_boolean error;
5284};
a0c8462f 5285
5a580b3a
AM
5286/* This function will be called though elf_link_hash_traverse to store
5287 all hash value of the exported symbols in an array. */
5288
5289static bfd_boolean
5290elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5291{
a50b1753 5292 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5293 const char *name;
5a580b3a
AM
5294 unsigned long ha;
5295 char *alc = NULL;
5296
5a580b3a
AM
5297 /* Ignore indirect symbols. These are added by the versioning code. */
5298 if (h->dynindx == -1)
5299 return TRUE;
5300
5301 name = h->root.root.string;
422f1182 5302 if (h->versioned >= versioned)
5a580b3a 5303 {
422f1182
L
5304 char *p = strchr (name, ELF_VER_CHR);
5305 if (p != NULL)
14b1c01e 5306 {
422f1182
L
5307 alc = (char *) bfd_malloc (p - name + 1);
5308 if (alc == NULL)
5309 {
5310 inf->error = TRUE;
5311 return FALSE;
5312 }
5313 memcpy (alc, name, p - name);
5314 alc[p - name] = '\0';
5315 name = alc;
14b1c01e 5316 }
5a580b3a
AM
5317 }
5318
5319 /* Compute the hash value. */
5320 ha = bfd_elf_hash (name);
5321
5322 /* Store the found hash value in the array given as the argument. */
14b1c01e 5323 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5324
5325 /* And store it in the struct so that we can put it in the hash table
5326 later. */
f6e332e6 5327 h->u.elf_hash_value = ha;
5a580b3a
AM
5328
5329 if (alc != NULL)
5330 free (alc);
5331
5332 return TRUE;
5333}
5334
fdc90cb4
JJ
5335struct collect_gnu_hash_codes
5336{
5337 bfd *output_bfd;
5338 const struct elf_backend_data *bed;
5339 unsigned long int nsyms;
5340 unsigned long int maskbits;
5341 unsigned long int *hashcodes;
5342 unsigned long int *hashval;
5343 unsigned long int *indx;
5344 unsigned long int *counts;
5345 bfd_vma *bitmask;
5346 bfd_byte *contents;
5347 long int min_dynindx;
5348 unsigned long int bucketcount;
5349 unsigned long int symindx;
5350 long int local_indx;
5351 long int shift1, shift2;
5352 unsigned long int mask;
14b1c01e 5353 bfd_boolean error;
fdc90cb4
JJ
5354};
5355
5356/* This function will be called though elf_link_hash_traverse to store
5357 all hash value of the exported symbols in an array. */
5358
5359static bfd_boolean
5360elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5361{
a50b1753 5362 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5363 const char *name;
fdc90cb4
JJ
5364 unsigned long ha;
5365 char *alc = NULL;
5366
fdc90cb4
JJ
5367 /* Ignore indirect symbols. These are added by the versioning code. */
5368 if (h->dynindx == -1)
5369 return TRUE;
5370
5371 /* Ignore also local symbols and undefined symbols. */
5372 if (! (*s->bed->elf_hash_symbol) (h))
5373 return TRUE;
5374
5375 name = h->root.root.string;
422f1182 5376 if (h->versioned >= versioned)
fdc90cb4 5377 {
422f1182
L
5378 char *p = strchr (name, ELF_VER_CHR);
5379 if (p != NULL)
14b1c01e 5380 {
422f1182
L
5381 alc = (char *) bfd_malloc (p - name + 1);
5382 if (alc == NULL)
5383 {
5384 s->error = TRUE;
5385 return FALSE;
5386 }
5387 memcpy (alc, name, p - name);
5388 alc[p - name] = '\0';
5389 name = alc;
14b1c01e 5390 }
fdc90cb4
JJ
5391 }
5392
5393 /* Compute the hash value. */
5394 ha = bfd_elf_gnu_hash (name);
5395
5396 /* Store the found hash value in the array for compute_bucket_count,
5397 and also for .dynsym reordering purposes. */
5398 s->hashcodes[s->nsyms] = ha;
5399 s->hashval[h->dynindx] = ha;
5400 ++s->nsyms;
5401 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5402 s->min_dynindx = h->dynindx;
5403
5404 if (alc != NULL)
5405 free (alc);
5406
5407 return TRUE;
5408}
5409
5410/* This function will be called though elf_link_hash_traverse to do
5411 final dynaminc symbol renumbering. */
5412
5413static bfd_boolean
5414elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5415{
a50b1753 5416 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5417 unsigned long int bucket;
5418 unsigned long int val;
5419
fdc90cb4
JJ
5420 /* Ignore indirect symbols. */
5421 if (h->dynindx == -1)
5422 return TRUE;
5423
5424 /* Ignore also local symbols and undefined symbols. */
5425 if (! (*s->bed->elf_hash_symbol) (h))
5426 {
5427 if (h->dynindx >= s->min_dynindx)
5428 h->dynindx = s->local_indx++;
5429 return TRUE;
5430 }
5431
5432 bucket = s->hashval[h->dynindx] % s->bucketcount;
5433 val = (s->hashval[h->dynindx] >> s->shift1)
5434 & ((s->maskbits >> s->shift1) - 1);
5435 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5436 s->bitmask[val]
5437 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5438 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5439 if (s->counts[bucket] == 1)
5440 /* Last element terminates the chain. */
5441 val |= 1;
5442 bfd_put_32 (s->output_bfd, val,
5443 s->contents + (s->indx[bucket] - s->symindx) * 4);
5444 --s->counts[bucket];
5445 h->dynindx = s->indx[bucket]++;
5446 return TRUE;
5447}
5448
5449/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5450
5451bfd_boolean
5452_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5453{
5454 return !(h->forced_local
5455 || h->root.type == bfd_link_hash_undefined
5456 || h->root.type == bfd_link_hash_undefweak
5457 || ((h->root.type == bfd_link_hash_defined
5458 || h->root.type == bfd_link_hash_defweak)
5459 && h->root.u.def.section->output_section == NULL));
5460}
5461
5a580b3a
AM
5462/* Array used to determine the number of hash table buckets to use
5463 based on the number of symbols there are. If there are fewer than
5464 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5465 fewer than 37 we use 17 buckets, and so forth. We never use more
5466 than 32771 buckets. */
5467
5468static const size_t elf_buckets[] =
5469{
5470 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5471 16411, 32771, 0
5472};
5473
5474/* Compute bucket count for hashing table. We do not use a static set
5475 of possible tables sizes anymore. Instead we determine for all
5476 possible reasonable sizes of the table the outcome (i.e., the
5477 number of collisions etc) and choose the best solution. The
5478 weighting functions are not too simple to allow the table to grow
5479 without bounds. Instead one of the weighting factors is the size.
5480 Therefore the result is always a good payoff between few collisions
5481 (= short chain lengths) and table size. */
5482static size_t
b20dd2ce 5483compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5484 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5485 unsigned long int nsyms,
5486 int gnu_hash)
5a580b3a 5487{
5a580b3a 5488 size_t best_size = 0;
5a580b3a 5489 unsigned long int i;
5a580b3a 5490
5a580b3a
AM
5491 /* We have a problem here. The following code to optimize the table
5492 size requires an integer type with more the 32 bits. If
5493 BFD_HOST_U_64_BIT is set we know about such a type. */
5494#ifdef BFD_HOST_U_64_BIT
5495 if (info->optimize)
5496 {
5a580b3a
AM
5497 size_t minsize;
5498 size_t maxsize;
5499 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5500 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5501 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5502 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5503 unsigned long int *counts;
d40f3da9 5504 bfd_size_type amt;
0883b6e0 5505 unsigned int no_improvement_count = 0;
5a580b3a
AM
5506
5507 /* Possible optimization parameters: if we have NSYMS symbols we say
5508 that the hashing table must at least have NSYMS/4 and at most
5509 2*NSYMS buckets. */
5510 minsize = nsyms / 4;
5511 if (minsize == 0)
5512 minsize = 1;
5513 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5514 if (gnu_hash)
5515 {
5516 if (minsize < 2)
5517 minsize = 2;
5518 if ((best_size & 31) == 0)
5519 ++best_size;
5520 }
5a580b3a
AM
5521
5522 /* Create array where we count the collisions in. We must use bfd_malloc
5523 since the size could be large. */
5524 amt = maxsize;
5525 amt *= sizeof (unsigned long int);
a50b1753 5526 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5527 if (counts == NULL)
fdc90cb4 5528 return 0;
5a580b3a
AM
5529
5530 /* Compute the "optimal" size for the hash table. The criteria is a
5531 minimal chain length. The minor criteria is (of course) the size
5532 of the table. */
5533 for (i = minsize; i < maxsize; ++i)
5534 {
5535 /* Walk through the array of hashcodes and count the collisions. */
5536 BFD_HOST_U_64_BIT max;
5537 unsigned long int j;
5538 unsigned long int fact;
5539
fdc90cb4
JJ
5540 if (gnu_hash && (i & 31) == 0)
5541 continue;
5542
5a580b3a
AM
5543 memset (counts, '\0', i * sizeof (unsigned long int));
5544
5545 /* Determine how often each hash bucket is used. */
5546 for (j = 0; j < nsyms; ++j)
5547 ++counts[hashcodes[j] % i];
5548
5549 /* For the weight function we need some information about the
5550 pagesize on the target. This is information need not be 100%
5551 accurate. Since this information is not available (so far) we
5552 define it here to a reasonable default value. If it is crucial
5553 to have a better value some day simply define this value. */
5554# ifndef BFD_TARGET_PAGESIZE
5555# define BFD_TARGET_PAGESIZE (4096)
5556# endif
5557
fdc90cb4
JJ
5558 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5559 and the chains. */
5560 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5561
5562# if 1
5563 /* Variant 1: optimize for short chains. We add the squares
5564 of all the chain lengths (which favors many small chain
5565 over a few long chains). */
5566 for (j = 0; j < i; ++j)
5567 max += counts[j] * counts[j];
5568
5569 /* This adds penalties for the overall size of the table. */
fdc90cb4 5570 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5571 max *= fact * fact;
5572# else
5573 /* Variant 2: Optimize a lot more for small table. Here we
5574 also add squares of the size but we also add penalties for
5575 empty slots (the +1 term). */
5576 for (j = 0; j < i; ++j)
5577 max += (1 + counts[j]) * (1 + counts[j]);
5578
5579 /* The overall size of the table is considered, but not as
5580 strong as in variant 1, where it is squared. */
fdc90cb4 5581 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5582 max *= fact;
5583# endif
5584
5585 /* Compare with current best results. */
5586 if (max < best_chlen)
5587 {
5588 best_chlen = max;
5589 best_size = i;
ca4be51c 5590 no_improvement_count = 0;
5a580b3a 5591 }
0883b6e0
NC
5592 /* PR 11843: Avoid futile long searches for the best bucket size
5593 when there are a large number of symbols. */
5594 else if (++no_improvement_count == 100)
5595 break;
5a580b3a
AM
5596 }
5597
5598 free (counts);
5599 }
5600 else
5601#endif /* defined (BFD_HOST_U_64_BIT) */
5602 {
5603 /* This is the fallback solution if no 64bit type is available or if we
5604 are not supposed to spend much time on optimizations. We select the
5605 bucket count using a fixed set of numbers. */
5606 for (i = 0; elf_buckets[i] != 0; i++)
5607 {
5608 best_size = elf_buckets[i];
fdc90cb4 5609 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5610 break;
5611 }
fdc90cb4
JJ
5612 if (gnu_hash && best_size < 2)
5613 best_size = 2;
5a580b3a
AM
5614 }
5615
5a580b3a
AM
5616 return best_size;
5617}
5618
d0bf826b
AM
5619/* Size any SHT_GROUP section for ld -r. */
5620
5621bfd_boolean
5622_bfd_elf_size_group_sections (struct bfd_link_info *info)
5623{
5624 bfd *ibfd;
5625
c72f2fb2 5626 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5627 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5628 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5629 return FALSE;
5630 return TRUE;
5631}
5632
04c3a755
NS
5633/* Set a default stack segment size. The value in INFO wins. If it
5634 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5635 undefined it is initialized. */
5636
5637bfd_boolean
5638bfd_elf_stack_segment_size (bfd *output_bfd,
5639 struct bfd_link_info *info,
5640 const char *legacy_symbol,
5641 bfd_vma default_size)
5642{
5643 struct elf_link_hash_entry *h = NULL;
5644
5645 /* Look for legacy symbol. */
5646 if (legacy_symbol)
5647 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5648 FALSE, FALSE, FALSE);
5649 if (h && (h->root.type == bfd_link_hash_defined
5650 || h->root.type == bfd_link_hash_defweak)
5651 && h->def_regular
5652 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5653 {
5654 /* The symbol has no type if specified on the command line. */
5655 h->type = STT_OBJECT;
5656 if (info->stacksize)
5657 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5658 output_bfd, legacy_symbol);
5659 else if (h->root.u.def.section != bfd_abs_section_ptr)
5660 (*_bfd_error_handler) (_("%B: %s not absolute"),
5661 output_bfd, legacy_symbol);
5662 else
5663 info->stacksize = h->root.u.def.value;
5664 }
5665
5666 if (!info->stacksize)
5667 /* If the user didn't set a size, or explicitly inhibit the
5668 size, set it now. */
5669 info->stacksize = default_size;
5670
5671 /* Provide the legacy symbol, if it is referenced. */
5672 if (h && (h->root.type == bfd_link_hash_undefined
5673 || h->root.type == bfd_link_hash_undefweak))
5674 {
5675 struct bfd_link_hash_entry *bh = NULL;
5676
5677 if (!(_bfd_generic_link_add_one_symbol
5678 (info, output_bfd, legacy_symbol,
5679 BSF_GLOBAL, bfd_abs_section_ptr,
5680 info->stacksize >= 0 ? info->stacksize : 0,
5681 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5682 return FALSE;
5683
5684 h = (struct elf_link_hash_entry *) bh;
5685 h->def_regular = 1;
5686 h->type = STT_OBJECT;
5687 }
5688
5689 return TRUE;
5690}
5691
5a580b3a
AM
5692/* Set up the sizes and contents of the ELF dynamic sections. This is
5693 called by the ELF linker emulation before_allocation routine. We
5694 must set the sizes of the sections before the linker sets the
5695 addresses of the various sections. */
5696
5697bfd_boolean
5698bfd_elf_size_dynamic_sections (bfd *output_bfd,
5699 const char *soname,
5700 const char *rpath,
5701 const char *filter_shlib,
7ee314fa
AM
5702 const char *audit,
5703 const char *depaudit,
5a580b3a
AM
5704 const char * const *auxiliary_filters,
5705 struct bfd_link_info *info,
fd91d419 5706 asection **sinterpptr)
5a580b3a
AM
5707{
5708 bfd_size_type soname_indx;
5709 bfd *dynobj;
5710 const struct elf_backend_data *bed;
28caa186 5711 struct elf_info_failed asvinfo;
5a580b3a
AM
5712
5713 *sinterpptr = NULL;
5714
5715 soname_indx = (bfd_size_type) -1;
5716
5717 if (!is_elf_hash_table (info->hash))
5718 return TRUE;
5719
6bfdb61b 5720 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5721
5722 /* Any syms created from now on start with -1 in
5723 got.refcount/offset and plt.refcount/offset. */
5724 elf_hash_table (info)->init_got_refcount
5725 = elf_hash_table (info)->init_got_offset;
5726 elf_hash_table (info)->init_plt_refcount
5727 = elf_hash_table (info)->init_plt_offset;
5728
0e1862bb 5729 if (bfd_link_relocatable (info)
04c3a755
NS
5730 && !_bfd_elf_size_group_sections (info))
5731 return FALSE;
5732
5733 /* The backend may have to create some sections regardless of whether
5734 we're dynamic or not. */
5735 if (bed->elf_backend_always_size_sections
5736 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5737 return FALSE;
5738
5739 /* Determine any GNU_STACK segment requirements, after the backend
5740 has had a chance to set a default segment size. */
5a580b3a 5741 if (info->execstack)
12bd6957 5742 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5743 else if (info->noexecstack)
12bd6957 5744 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5745 else
5746 {
5747 bfd *inputobj;
5748 asection *notesec = NULL;
5749 int exec = 0;
5750
5751 for (inputobj = info->input_bfds;
5752 inputobj;
c72f2fb2 5753 inputobj = inputobj->link.next)
5a580b3a
AM
5754 {
5755 asection *s;
5756
a92c088a
L
5757 if (inputobj->flags
5758 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5759 continue;
5760 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5761 if (s)
5762 {
5763 if (s->flags & SEC_CODE)
5764 exec = PF_X;
5765 notesec = s;
5766 }
6bfdb61b 5767 else if (bed->default_execstack)
5a580b3a
AM
5768 exec = PF_X;
5769 }
04c3a755 5770 if (notesec || info->stacksize > 0)
12bd6957 5771 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5772 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5773 && notesec->output_section != bfd_abs_section_ptr)
5774 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5775 }
5776
5a580b3a
AM
5777 dynobj = elf_hash_table (info)->dynobj;
5778
9a2a56cc 5779 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5780 {
5781 struct elf_info_failed eif;
5782 struct elf_link_hash_entry *h;
5783 asection *dynstr;
5784 struct bfd_elf_version_tree *t;
5785 struct bfd_elf_version_expr *d;
046183de 5786 asection *s;
5a580b3a
AM
5787 bfd_boolean all_defined;
5788
3d4d4302 5789 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5790 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5791
5792 if (soname != NULL)
5793 {
5794 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5795 soname, TRUE);
5796 if (soname_indx == (bfd_size_type) -1
5797 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5798 return FALSE;
5799 }
5800
5801 if (info->symbolic)
5802 {
5803 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5804 return FALSE;
5805 info->flags |= DF_SYMBOLIC;
5806 }
5807
5808 if (rpath != NULL)
5809 {
5810 bfd_size_type indx;
b1b00fcc 5811 bfd_vma tag;
5a580b3a
AM
5812
5813 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5814 TRUE);
b1b00fcc 5815 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5816 return FALSE;
5817
b1b00fcc
MF
5818 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5819 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5820 return FALSE;
5a580b3a
AM
5821 }
5822
5823 if (filter_shlib != NULL)
5824 {
5825 bfd_size_type indx;
5826
5827 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5828 filter_shlib, TRUE);
5829 if (indx == (bfd_size_type) -1
5830 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5831 return FALSE;
5832 }
5833
5834 if (auxiliary_filters != NULL)
5835 {
5836 const char * const *p;
5837
5838 for (p = auxiliary_filters; *p != NULL; p++)
5839 {
5840 bfd_size_type indx;
5841
5842 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5843 *p, TRUE);
5844 if (indx == (bfd_size_type) -1
5845 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5846 return FALSE;
5847 }
5848 }
5849
7ee314fa
AM
5850 if (audit != NULL)
5851 {
5852 bfd_size_type indx;
5853
5854 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5855 TRUE);
5856 if (indx == (bfd_size_type) -1
5857 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5858 return FALSE;
5859 }
5860
5861 if (depaudit != NULL)
5862 {
5863 bfd_size_type indx;
5864
5865 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5866 TRUE);
5867 if (indx == (bfd_size_type) -1
5868 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5869 return FALSE;
5870 }
5871
5a580b3a 5872 eif.info = info;
5a580b3a
AM
5873 eif.failed = FALSE;
5874
5875 /* If we are supposed to export all symbols into the dynamic symbol
5876 table (this is not the normal case), then do so. */
55255dae 5877 if (info->export_dynamic
0e1862bb 5878 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5879 {
5880 elf_link_hash_traverse (elf_hash_table (info),
5881 _bfd_elf_export_symbol,
5882 &eif);
5883 if (eif.failed)
5884 return FALSE;
5885 }
5886
5887 /* Make all global versions with definition. */
fd91d419 5888 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5889 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5890 if (!d->symver && d->literal)
5a580b3a
AM
5891 {
5892 const char *verstr, *name;
5893 size_t namelen, verlen, newlen;
93252b1c 5894 char *newname, *p, leading_char;
5a580b3a
AM
5895 struct elf_link_hash_entry *newh;
5896
93252b1c 5897 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5898 name = d->pattern;
93252b1c 5899 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5900 verstr = t->name;
5901 verlen = strlen (verstr);
5902 newlen = namelen + verlen + 3;
5903
a50b1753 5904 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5905 if (newname == NULL)
5906 return FALSE;
93252b1c
MF
5907 newname[0] = leading_char;
5908 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5909
5910 /* Check the hidden versioned definition. */
5911 p = newname + namelen;
5912 *p++ = ELF_VER_CHR;
5913 memcpy (p, verstr, verlen + 1);
5914 newh = elf_link_hash_lookup (elf_hash_table (info),
5915 newname, FALSE, FALSE,
5916 FALSE);
5917 if (newh == NULL
5918 || (newh->root.type != bfd_link_hash_defined
5919 && newh->root.type != bfd_link_hash_defweak))
5920 {
5921 /* Check the default versioned definition. */
5922 *p++ = ELF_VER_CHR;
5923 memcpy (p, verstr, verlen + 1);
5924 newh = elf_link_hash_lookup (elf_hash_table (info),
5925 newname, FALSE, FALSE,
5926 FALSE);
5927 }
5928 free (newname);
5929
5930 /* Mark this version if there is a definition and it is
5931 not defined in a shared object. */
5932 if (newh != NULL
f5385ebf 5933 && !newh->def_dynamic
5a580b3a
AM
5934 && (newh->root.type == bfd_link_hash_defined
5935 || newh->root.type == bfd_link_hash_defweak))
5936 d->symver = 1;
5937 }
5938
5939 /* Attach all the symbols to their version information. */
5a580b3a 5940 asvinfo.info = info;
5a580b3a
AM
5941 asvinfo.failed = FALSE;
5942
5943 elf_link_hash_traverse (elf_hash_table (info),
5944 _bfd_elf_link_assign_sym_version,
5945 &asvinfo);
5946 if (asvinfo.failed)
5947 return FALSE;
5948
5949 if (!info->allow_undefined_version)
5950 {
5951 /* Check if all global versions have a definition. */
5952 all_defined = TRUE;
fd91d419 5953 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5954 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5955 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5956 {
5957 (*_bfd_error_handler)
5958 (_("%s: undefined version: %s"),
5959 d->pattern, t->name);
5960 all_defined = FALSE;
5961 }
5962
5963 if (!all_defined)
5964 {
5965 bfd_set_error (bfd_error_bad_value);
5966 return FALSE;
5967 }
5968 }
5969
5970 /* Find all symbols which were defined in a dynamic object and make
5971 the backend pick a reasonable value for them. */
5972 elf_link_hash_traverse (elf_hash_table (info),
5973 _bfd_elf_adjust_dynamic_symbol,
5974 &eif);
5975 if (eif.failed)
5976 return FALSE;
5977
5978 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5979 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5980 now so that we know the final size of the .dynamic section. */
5981
5982 /* If there are initialization and/or finalization functions to
5983 call then add the corresponding DT_INIT/DT_FINI entries. */
5984 h = (info->init_function
5985 ? elf_link_hash_lookup (elf_hash_table (info),
5986 info->init_function, FALSE,
5987 FALSE, FALSE)
5988 : NULL);
5989 if (h != NULL
f5385ebf
AM
5990 && (h->ref_regular
5991 || h->def_regular))
5a580b3a
AM
5992 {
5993 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5994 return FALSE;
5995 }
5996 h = (info->fini_function
5997 ? elf_link_hash_lookup (elf_hash_table (info),
5998 info->fini_function, FALSE,
5999 FALSE, FALSE)
6000 : NULL);
6001 if (h != NULL
f5385ebf
AM
6002 && (h->ref_regular
6003 || h->def_regular))
5a580b3a
AM
6004 {
6005 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6006 return FALSE;
6007 }
6008
046183de
AM
6009 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6010 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6011 {
6012 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6013 if (! bfd_link_executable (info))
5a580b3a
AM
6014 {
6015 bfd *sub;
6016 asection *o;
6017
6018 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6019 sub = sub->link.next)
3fcd97f1
JJ
6020 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6021 for (o = sub->sections; o != NULL; o = o->next)
6022 if (elf_section_data (o)->this_hdr.sh_type
6023 == SHT_PREINIT_ARRAY)
6024 {
6025 (*_bfd_error_handler)
6026 (_("%B: .preinit_array section is not allowed in DSO"),
6027 sub);
6028 break;
6029 }
5a580b3a
AM
6030
6031 bfd_set_error (bfd_error_nonrepresentable_section);
6032 return FALSE;
6033 }
6034
6035 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6036 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6037 return FALSE;
6038 }
046183de
AM
6039 s = bfd_get_section_by_name (output_bfd, ".init_array");
6040 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6041 {
6042 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6043 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6044 return FALSE;
6045 }
046183de
AM
6046 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6047 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6048 {
6049 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6050 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6051 return FALSE;
6052 }
6053
3d4d4302 6054 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6055 /* If .dynstr is excluded from the link, we don't want any of
6056 these tags. Strictly, we should be checking each section
6057 individually; This quick check covers for the case where
6058 someone does a /DISCARD/ : { *(*) }. */
6059 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6060 {
6061 bfd_size_type strsize;
6062
6063 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6064 if ((info->emit_hash
6065 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6066 || (info->emit_gnu_hash
6067 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6068 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6069 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6070 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6071 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6072 bed->s->sizeof_sym))
6073 return FALSE;
6074 }
6075 }
6076
de231f20
CM
6077 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6078 return FALSE;
6079
5a580b3a
AM
6080 /* The backend must work out the sizes of all the other dynamic
6081 sections. */
9a2a56cc
AM
6082 if (dynobj != NULL
6083 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6084 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6085 return FALSE;
6086
9a2a56cc 6087 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6088 {
554220db 6089 unsigned long section_sym_count;
fd91d419 6090 struct bfd_elf_version_tree *verdefs;
5a580b3a 6091 asection *s;
5a580b3a
AM
6092
6093 /* Set up the version definition section. */
3d4d4302 6094 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6095 BFD_ASSERT (s != NULL);
6096
6097 /* We may have created additional version definitions if we are
6098 just linking a regular application. */
fd91d419 6099 verdefs = info->version_info;
5a580b3a
AM
6100
6101 /* Skip anonymous version tag. */
6102 if (verdefs != NULL && verdefs->vernum == 0)
6103 verdefs = verdefs->next;
6104
3e3b46e5 6105 if (verdefs == NULL && !info->create_default_symver)
8423293d 6106 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6107 else
6108 {
6109 unsigned int cdefs;
6110 bfd_size_type size;
6111 struct bfd_elf_version_tree *t;
6112 bfd_byte *p;
6113 Elf_Internal_Verdef def;
6114 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6115 struct bfd_link_hash_entry *bh;
6116 struct elf_link_hash_entry *h;
6117 const char *name;
5a580b3a
AM
6118
6119 cdefs = 0;
6120 size = 0;
6121
6122 /* Make space for the base version. */
6123 size += sizeof (Elf_External_Verdef);
6124 size += sizeof (Elf_External_Verdaux);
6125 ++cdefs;
6126
3e3b46e5
PB
6127 /* Make space for the default version. */
6128 if (info->create_default_symver)
6129 {
6130 size += sizeof (Elf_External_Verdef);
6131 ++cdefs;
6132 }
6133
5a580b3a
AM
6134 for (t = verdefs; t != NULL; t = t->next)
6135 {
6136 struct bfd_elf_version_deps *n;
6137
a6cc6b3b
RO
6138 /* Don't emit base version twice. */
6139 if (t->vernum == 0)
6140 continue;
6141
5a580b3a
AM
6142 size += sizeof (Elf_External_Verdef);
6143 size += sizeof (Elf_External_Verdaux);
6144 ++cdefs;
6145
6146 for (n = t->deps; n != NULL; n = n->next)
6147 size += sizeof (Elf_External_Verdaux);
6148 }
6149
eea6121a 6150 s->size = size;
a50b1753 6151 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6152 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6153 return FALSE;
6154
6155 /* Fill in the version definition section. */
6156
6157 p = s->contents;
6158
6159 def.vd_version = VER_DEF_CURRENT;
6160 def.vd_flags = VER_FLG_BASE;
6161 def.vd_ndx = 1;
6162 def.vd_cnt = 1;
3e3b46e5
PB
6163 if (info->create_default_symver)
6164 {
6165 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6166 def.vd_next = sizeof (Elf_External_Verdef);
6167 }
6168 else
6169 {
6170 def.vd_aux = sizeof (Elf_External_Verdef);
6171 def.vd_next = (sizeof (Elf_External_Verdef)
6172 + sizeof (Elf_External_Verdaux));
6173 }
5a580b3a
AM
6174
6175 if (soname_indx != (bfd_size_type) -1)
6176 {
6177 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6178 soname_indx);
6179 def.vd_hash = bfd_elf_hash (soname);
6180 defaux.vda_name = soname_indx;
3e3b46e5 6181 name = soname;
5a580b3a
AM
6182 }
6183 else
6184 {
5a580b3a
AM
6185 bfd_size_type indx;
6186
06084812 6187 name = lbasename (output_bfd->filename);
5a580b3a
AM
6188 def.vd_hash = bfd_elf_hash (name);
6189 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6190 name, FALSE);
6191 if (indx == (bfd_size_type) -1)
6192 return FALSE;
6193 defaux.vda_name = indx;
6194 }
6195 defaux.vda_next = 0;
6196
6197 _bfd_elf_swap_verdef_out (output_bfd, &def,
6198 (Elf_External_Verdef *) p);
6199 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6200 if (info->create_default_symver)
6201 {
6202 /* Add a symbol representing this version. */
6203 bh = NULL;
6204 if (! (_bfd_generic_link_add_one_symbol
6205 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6206 0, NULL, FALSE,
6207 get_elf_backend_data (dynobj)->collect, &bh)))
6208 return FALSE;
6209 h = (struct elf_link_hash_entry *) bh;
6210 h->non_elf = 0;
6211 h->def_regular = 1;
6212 h->type = STT_OBJECT;
6213 h->verinfo.vertree = NULL;
6214
6215 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6216 return FALSE;
6217
6218 /* Create a duplicate of the base version with the same
6219 aux block, but different flags. */
6220 def.vd_flags = 0;
6221 def.vd_ndx = 2;
6222 def.vd_aux = sizeof (Elf_External_Verdef);
6223 if (verdefs)
6224 def.vd_next = (sizeof (Elf_External_Verdef)
6225 + sizeof (Elf_External_Verdaux));
6226 else
6227 def.vd_next = 0;
6228 _bfd_elf_swap_verdef_out (output_bfd, &def,
6229 (Elf_External_Verdef *) p);
6230 p += sizeof (Elf_External_Verdef);
6231 }
5a580b3a
AM
6232 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6233 (Elf_External_Verdaux *) p);
6234 p += sizeof (Elf_External_Verdaux);
6235
6236 for (t = verdefs; t != NULL; t = t->next)
6237 {
6238 unsigned int cdeps;
6239 struct bfd_elf_version_deps *n;
5a580b3a 6240
a6cc6b3b
RO
6241 /* Don't emit the base version twice. */
6242 if (t->vernum == 0)
6243 continue;
6244
5a580b3a
AM
6245 cdeps = 0;
6246 for (n = t->deps; n != NULL; n = n->next)
6247 ++cdeps;
6248
6249 /* Add a symbol representing this version. */
6250 bh = NULL;
6251 if (! (_bfd_generic_link_add_one_symbol
6252 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6253 0, NULL, FALSE,
6254 get_elf_backend_data (dynobj)->collect, &bh)))
6255 return FALSE;
6256 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6257 h->non_elf = 0;
6258 h->def_regular = 1;
5a580b3a
AM
6259 h->type = STT_OBJECT;
6260 h->verinfo.vertree = t;
6261
c152c796 6262 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6263 return FALSE;
6264
6265 def.vd_version = VER_DEF_CURRENT;
6266 def.vd_flags = 0;
6267 if (t->globals.list == NULL
6268 && t->locals.list == NULL
6269 && ! t->used)
6270 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6271 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6272 def.vd_cnt = cdeps + 1;
6273 def.vd_hash = bfd_elf_hash (t->name);
6274 def.vd_aux = sizeof (Elf_External_Verdef);
6275 def.vd_next = 0;
a6cc6b3b
RO
6276
6277 /* If a basever node is next, it *must* be the last node in
6278 the chain, otherwise Verdef construction breaks. */
6279 if (t->next != NULL && t->next->vernum == 0)
6280 BFD_ASSERT (t->next->next == NULL);
6281
6282 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6283 def.vd_next = (sizeof (Elf_External_Verdef)
6284 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6285
6286 _bfd_elf_swap_verdef_out (output_bfd, &def,
6287 (Elf_External_Verdef *) p);
6288 p += sizeof (Elf_External_Verdef);
6289
6290 defaux.vda_name = h->dynstr_index;
6291 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6292 h->dynstr_index);
6293 defaux.vda_next = 0;
6294 if (t->deps != NULL)
6295 defaux.vda_next = sizeof (Elf_External_Verdaux);
6296 t->name_indx = defaux.vda_name;
6297
6298 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6299 (Elf_External_Verdaux *) p);
6300 p += sizeof (Elf_External_Verdaux);
6301
6302 for (n = t->deps; n != NULL; n = n->next)
6303 {
6304 if (n->version_needed == NULL)
6305 {
6306 /* This can happen if there was an error in the
6307 version script. */
6308 defaux.vda_name = 0;
6309 }
6310 else
6311 {
6312 defaux.vda_name = n->version_needed->name_indx;
6313 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6314 defaux.vda_name);
6315 }
6316 if (n->next == NULL)
6317 defaux.vda_next = 0;
6318 else
6319 defaux.vda_next = sizeof (Elf_External_Verdaux);
6320
6321 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6322 (Elf_External_Verdaux *) p);
6323 p += sizeof (Elf_External_Verdaux);
6324 }
6325 }
6326
6327 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6328 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6329 return FALSE;
6330
6331 elf_tdata (output_bfd)->cverdefs = cdefs;
6332 }
6333
6334 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6335 {
6336 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6337 return FALSE;
6338 }
6339 else if (info->flags & DF_BIND_NOW)
6340 {
6341 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6342 return FALSE;
6343 }
6344
6345 if (info->flags_1)
6346 {
0e1862bb 6347 if (bfd_link_executable (info))
5a580b3a
AM
6348 info->flags_1 &= ~ (DF_1_INITFIRST
6349 | DF_1_NODELETE
6350 | DF_1_NOOPEN);
6351 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6352 return FALSE;
6353 }
6354
6355 /* Work out the size of the version reference section. */
6356
3d4d4302 6357 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6358 BFD_ASSERT (s != NULL);
6359 {
6360 struct elf_find_verdep_info sinfo;
6361
5a580b3a
AM
6362 sinfo.info = info;
6363 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6364 if (sinfo.vers == 0)
6365 sinfo.vers = 1;
6366 sinfo.failed = FALSE;
6367
6368 elf_link_hash_traverse (elf_hash_table (info),
6369 _bfd_elf_link_find_version_dependencies,
6370 &sinfo);
14b1c01e
AM
6371 if (sinfo.failed)
6372 return FALSE;
5a580b3a
AM
6373
6374 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6375 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6376 else
6377 {
6378 Elf_Internal_Verneed *t;
6379 unsigned int size;
6380 unsigned int crefs;
6381 bfd_byte *p;
6382
a6cc6b3b 6383 /* Build the version dependency section. */
5a580b3a
AM
6384 size = 0;
6385 crefs = 0;
6386 for (t = elf_tdata (output_bfd)->verref;
6387 t != NULL;
6388 t = t->vn_nextref)
6389 {
6390 Elf_Internal_Vernaux *a;
6391
6392 size += sizeof (Elf_External_Verneed);
6393 ++crefs;
6394 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6395 size += sizeof (Elf_External_Vernaux);
6396 }
6397
eea6121a 6398 s->size = size;
a50b1753 6399 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6400 if (s->contents == NULL)
6401 return FALSE;
6402
6403 p = s->contents;
6404 for (t = elf_tdata (output_bfd)->verref;
6405 t != NULL;
6406 t = t->vn_nextref)
6407 {
6408 unsigned int caux;
6409 Elf_Internal_Vernaux *a;
6410 bfd_size_type indx;
6411
6412 caux = 0;
6413 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6414 ++caux;
6415
6416 t->vn_version = VER_NEED_CURRENT;
6417 t->vn_cnt = caux;
6418 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6419 elf_dt_name (t->vn_bfd) != NULL
6420 ? elf_dt_name (t->vn_bfd)
06084812 6421 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6422 FALSE);
6423 if (indx == (bfd_size_type) -1)
6424 return FALSE;
6425 t->vn_file = indx;
6426 t->vn_aux = sizeof (Elf_External_Verneed);
6427 if (t->vn_nextref == NULL)
6428 t->vn_next = 0;
6429 else
6430 t->vn_next = (sizeof (Elf_External_Verneed)
6431 + caux * sizeof (Elf_External_Vernaux));
6432
6433 _bfd_elf_swap_verneed_out (output_bfd, t,
6434 (Elf_External_Verneed *) p);
6435 p += sizeof (Elf_External_Verneed);
6436
6437 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6438 {
6439 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6440 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6441 a->vna_nodename, FALSE);
6442 if (indx == (bfd_size_type) -1)
6443 return FALSE;
6444 a->vna_name = indx;
6445 if (a->vna_nextptr == NULL)
6446 a->vna_next = 0;
6447 else
6448 a->vna_next = sizeof (Elf_External_Vernaux);
6449
6450 _bfd_elf_swap_vernaux_out (output_bfd, a,
6451 (Elf_External_Vernaux *) p);
6452 p += sizeof (Elf_External_Vernaux);
6453 }
6454 }
6455
6456 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6457 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6458 return FALSE;
6459
6460 elf_tdata (output_bfd)->cverrefs = crefs;
6461 }
6462 }
6463
8423293d
AM
6464 if ((elf_tdata (output_bfd)->cverrefs == 0
6465 && elf_tdata (output_bfd)->cverdefs == 0)
6466 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6467 &section_sym_count) == 0)
6468 {
3d4d4302 6469 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6470 s->flags |= SEC_EXCLUDE;
6471 }
6472 }
6473 return TRUE;
6474}
6475
74541ad4
AM
6476/* Find the first non-excluded output section. We'll use its
6477 section symbol for some emitted relocs. */
6478void
6479_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6480{
6481 asection *s;
6482
6483 for (s = output_bfd->sections; s != NULL; s = s->next)
6484 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6485 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6486 {
6487 elf_hash_table (info)->text_index_section = s;
6488 break;
6489 }
6490}
6491
6492/* Find two non-excluded output sections, one for code, one for data.
6493 We'll use their section symbols for some emitted relocs. */
6494void
6495_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6496{
6497 asection *s;
6498
266b05cf
DJ
6499 /* Data first, since setting text_index_section changes
6500 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6501 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6502 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6503 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6504 {
266b05cf 6505 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6506 break;
6507 }
6508
6509 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6510 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6511 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6512 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6513 {
266b05cf 6514 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6515 break;
6516 }
6517
6518 if (elf_hash_table (info)->text_index_section == NULL)
6519 elf_hash_table (info)->text_index_section
6520 = elf_hash_table (info)->data_index_section;
6521}
6522
8423293d
AM
6523bfd_boolean
6524bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6525{
74541ad4
AM
6526 const struct elf_backend_data *bed;
6527
8423293d
AM
6528 if (!is_elf_hash_table (info->hash))
6529 return TRUE;
6530
74541ad4
AM
6531 bed = get_elf_backend_data (output_bfd);
6532 (*bed->elf_backend_init_index_section) (output_bfd, info);
6533
8423293d
AM
6534 if (elf_hash_table (info)->dynamic_sections_created)
6535 {
6536 bfd *dynobj;
8423293d
AM
6537 asection *s;
6538 bfd_size_type dynsymcount;
6539 unsigned long section_sym_count;
8423293d
AM
6540 unsigned int dtagcount;
6541
6542 dynobj = elf_hash_table (info)->dynobj;
6543
5a580b3a
AM
6544 /* Assign dynsym indicies. In a shared library we generate a
6545 section symbol for each output section, which come first.
6546 Next come all of the back-end allocated local dynamic syms,
6547 followed by the rest of the global symbols. */
6548
554220db
AM
6549 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6550 &section_sym_count);
5a580b3a
AM
6551
6552 /* Work out the size of the symbol version section. */
3d4d4302 6553 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6554 BFD_ASSERT (s != NULL);
8423293d
AM
6555 if (dynsymcount != 0
6556 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6557 {
eea6121a 6558 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6559 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6560 if (s->contents == NULL)
6561 return FALSE;
6562
6563 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6564 return FALSE;
6565 }
6566
6567 /* Set the size of the .dynsym and .hash sections. We counted
6568 the number of dynamic symbols in elf_link_add_object_symbols.
6569 We will build the contents of .dynsym and .hash when we build
6570 the final symbol table, because until then we do not know the
6571 correct value to give the symbols. We built the .dynstr
6572 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6573 s = elf_hash_table (info)->dynsym;
5a580b3a 6574 BFD_ASSERT (s != NULL);
eea6121a 6575 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6576
6577 if (dynsymcount != 0)
6578 {
a50b1753 6579 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6580 if (s->contents == NULL)
6581 return FALSE;
5a580b3a 6582
554220db
AM
6583 /* The first entry in .dynsym is a dummy symbol.
6584 Clear all the section syms, in case we don't output them all. */
6585 ++section_sym_count;
6586 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6587 }
6588
fdc90cb4
JJ
6589 elf_hash_table (info)->bucketcount = 0;
6590
5a580b3a
AM
6591 /* Compute the size of the hashing table. As a side effect this
6592 computes the hash values for all the names we export. */
fdc90cb4
JJ
6593 if (info->emit_hash)
6594 {
6595 unsigned long int *hashcodes;
14b1c01e 6596 struct hash_codes_info hashinf;
fdc90cb4
JJ
6597 bfd_size_type amt;
6598 unsigned long int nsyms;
6599 size_t bucketcount;
6600 size_t hash_entry_size;
6601
6602 /* Compute the hash values for all exported symbols. At the same
6603 time store the values in an array so that we could use them for
6604 optimizations. */
6605 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6606 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6607 if (hashcodes == NULL)
6608 return FALSE;
14b1c01e
AM
6609 hashinf.hashcodes = hashcodes;
6610 hashinf.error = FALSE;
5a580b3a 6611
fdc90cb4
JJ
6612 /* Put all hash values in HASHCODES. */
6613 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6614 elf_collect_hash_codes, &hashinf);
6615 if (hashinf.error)
4dd07732
AM
6616 {
6617 free (hashcodes);
6618 return FALSE;
6619 }
5a580b3a 6620
14b1c01e 6621 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6622 bucketcount
6623 = compute_bucket_count (info, hashcodes, nsyms, 0);
6624 free (hashcodes);
6625
6626 if (bucketcount == 0)
6627 return FALSE;
5a580b3a 6628
fdc90cb4
JJ
6629 elf_hash_table (info)->bucketcount = bucketcount;
6630
3d4d4302 6631 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6632 BFD_ASSERT (s != NULL);
6633 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6634 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6635 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6636 if (s->contents == NULL)
6637 return FALSE;
6638
6639 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6640 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6641 s->contents + hash_entry_size);
6642 }
6643
6644 if (info->emit_gnu_hash)
6645 {
6646 size_t i, cnt;
6647 unsigned char *contents;
6648 struct collect_gnu_hash_codes cinfo;
6649 bfd_size_type amt;
6650 size_t bucketcount;
6651
6652 memset (&cinfo, 0, sizeof (cinfo));
6653
6654 /* Compute the hash values for all exported symbols. At the same
6655 time store the values in an array so that we could use them for
6656 optimizations. */
6657 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6658 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6659 if (cinfo.hashcodes == NULL)
6660 return FALSE;
6661
6662 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6663 cinfo.min_dynindx = -1;
6664 cinfo.output_bfd = output_bfd;
6665 cinfo.bed = bed;
6666
6667 /* Put all hash values in HASHCODES. */
6668 elf_link_hash_traverse (elf_hash_table (info),
6669 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6670 if (cinfo.error)
4dd07732
AM
6671 {
6672 free (cinfo.hashcodes);
6673 return FALSE;
6674 }
fdc90cb4
JJ
6675
6676 bucketcount
6677 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6678
6679 if (bucketcount == 0)
6680 {
6681 free (cinfo.hashcodes);
6682 return FALSE;
6683 }
6684
3d4d4302 6685 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6686 BFD_ASSERT (s != NULL);
6687
6688 if (cinfo.nsyms == 0)
6689 {
6690 /* Empty .gnu.hash section is special. */
6691 BFD_ASSERT (cinfo.min_dynindx == -1);
6692 free (cinfo.hashcodes);
6693 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6694 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6695 if (contents == NULL)
6696 return FALSE;
6697 s->contents = contents;
6698 /* 1 empty bucket. */
6699 bfd_put_32 (output_bfd, 1, contents);
6700 /* SYMIDX above the special symbol 0. */
6701 bfd_put_32 (output_bfd, 1, contents + 4);
6702 /* Just one word for bitmask. */
6703 bfd_put_32 (output_bfd, 1, contents + 8);
6704 /* Only hash fn bloom filter. */
6705 bfd_put_32 (output_bfd, 0, contents + 12);
6706 /* No hashes are valid - empty bitmask. */
6707 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6708 /* No hashes in the only bucket. */
6709 bfd_put_32 (output_bfd, 0,
6710 contents + 16 + bed->s->arch_size / 8);
6711 }
6712 else
6713 {
9e6619e2 6714 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6715 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6716
9e6619e2
AM
6717 x = cinfo.nsyms;
6718 maskbitslog2 = 1;
6719 while ((x >>= 1) != 0)
6720 ++maskbitslog2;
fdc90cb4
JJ
6721 if (maskbitslog2 < 3)
6722 maskbitslog2 = 5;
6723 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6724 maskbitslog2 = maskbitslog2 + 3;
6725 else
6726 maskbitslog2 = maskbitslog2 + 2;
6727 if (bed->s->arch_size == 64)
6728 {
6729 if (maskbitslog2 == 5)
6730 maskbitslog2 = 6;
6731 cinfo.shift1 = 6;
6732 }
6733 else
6734 cinfo.shift1 = 5;
6735 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6736 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6737 cinfo.maskbits = 1 << maskbitslog2;
6738 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6739 amt = bucketcount * sizeof (unsigned long int) * 2;
6740 amt += maskwords * sizeof (bfd_vma);
a50b1753 6741 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6742 if (cinfo.bitmask == NULL)
6743 {
6744 free (cinfo.hashcodes);
6745 return FALSE;
6746 }
6747
a50b1753 6748 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6749 cinfo.indx = cinfo.counts + bucketcount;
6750 cinfo.symindx = dynsymcount - cinfo.nsyms;
6751 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6752
6753 /* Determine how often each hash bucket is used. */
6754 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6755 for (i = 0; i < cinfo.nsyms; ++i)
6756 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6757
6758 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6759 if (cinfo.counts[i] != 0)
6760 {
6761 cinfo.indx[i] = cnt;
6762 cnt += cinfo.counts[i];
6763 }
6764 BFD_ASSERT (cnt == dynsymcount);
6765 cinfo.bucketcount = bucketcount;
6766 cinfo.local_indx = cinfo.min_dynindx;
6767
6768 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6769 s->size += cinfo.maskbits / 8;
a50b1753 6770 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6771 if (contents == NULL)
6772 {
6773 free (cinfo.bitmask);
6774 free (cinfo.hashcodes);
6775 return FALSE;
6776 }
6777
6778 s->contents = contents;
6779 bfd_put_32 (output_bfd, bucketcount, contents);
6780 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6781 bfd_put_32 (output_bfd, maskwords, contents + 8);
6782 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6783 contents += 16 + cinfo.maskbits / 8;
6784
6785 for (i = 0; i < bucketcount; ++i)
6786 {
6787 if (cinfo.counts[i] == 0)
6788 bfd_put_32 (output_bfd, 0, contents);
6789 else
6790 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6791 contents += 4;
6792 }
6793
6794 cinfo.contents = contents;
6795
6796 /* Renumber dynamic symbols, populate .gnu.hash section. */
6797 elf_link_hash_traverse (elf_hash_table (info),
6798 elf_renumber_gnu_hash_syms, &cinfo);
6799
6800 contents = s->contents + 16;
6801 for (i = 0; i < maskwords; ++i)
6802 {
6803 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6804 contents);
6805 contents += bed->s->arch_size / 8;
6806 }
6807
6808 free (cinfo.bitmask);
6809 free (cinfo.hashcodes);
6810 }
6811 }
5a580b3a 6812
3d4d4302 6813 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6814 BFD_ASSERT (s != NULL);
6815
4ad4eba5 6816 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6817
eea6121a 6818 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6819
6820 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6821 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6822 return FALSE;
6823 }
6824
6825 return TRUE;
6826}
4d269e42 6827\f
4d269e42
AM
6828/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6829
6830static void
6831merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6832 asection *sec)
6833{
dbaa2011
AM
6834 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6835 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6836}
6837
6838/* Finish SHF_MERGE section merging. */
6839
6840bfd_boolean
630993ec 6841_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6842{
6843 bfd *ibfd;
6844 asection *sec;
6845
6846 if (!is_elf_hash_table (info->hash))
6847 return FALSE;
6848
c72f2fb2 6849 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6850 if ((ibfd->flags & DYNAMIC) == 0
6851 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6852 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6853 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6854 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6855 if ((sec->flags & SEC_MERGE) != 0
6856 && !bfd_is_abs_section (sec->output_section))
6857 {
6858 struct bfd_elf_section_data *secdata;
6859
6860 secdata = elf_section_data (sec);
630993ec 6861 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6862 &elf_hash_table (info)->merge_info,
6863 sec, &secdata->sec_info))
6864 return FALSE;
6865 else if (secdata->sec_info)
dbaa2011 6866 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6867 }
6868
6869 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6870 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6871 merge_sections_remove_hook);
6872 return TRUE;
6873}
6874
6875/* Create an entry in an ELF linker hash table. */
6876
6877struct bfd_hash_entry *
6878_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6879 struct bfd_hash_table *table,
6880 const char *string)
6881{
6882 /* Allocate the structure if it has not already been allocated by a
6883 subclass. */
6884 if (entry == NULL)
6885 {
a50b1753 6886 entry = (struct bfd_hash_entry *)
ca4be51c 6887 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6888 if (entry == NULL)
6889 return entry;
6890 }
6891
6892 /* Call the allocation method of the superclass. */
6893 entry = _bfd_link_hash_newfunc (entry, table, string);
6894 if (entry != NULL)
6895 {
6896 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6897 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6898
6899 /* Set local fields. */
6900 ret->indx = -1;
6901 ret->dynindx = -1;
6902 ret->got = htab->init_got_refcount;
6903 ret->plt = htab->init_plt_refcount;
6904 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6905 - offsetof (struct elf_link_hash_entry, size)));
6906 /* Assume that we have been called by a non-ELF symbol reader.
6907 This flag is then reset by the code which reads an ELF input
6908 file. This ensures that a symbol created by a non-ELF symbol
6909 reader will have the flag set correctly. */
6910 ret->non_elf = 1;
6911 }
6912
6913 return entry;
6914}
6915
6916/* Copy data from an indirect symbol to its direct symbol, hiding the
6917 old indirect symbol. Also used for copying flags to a weakdef. */
6918
6919void
6920_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6921 struct elf_link_hash_entry *dir,
6922 struct elf_link_hash_entry *ind)
6923{
6924 struct elf_link_hash_table *htab;
6925
6926 /* Copy down any references that we may have already seen to the
6e33951e
L
6927 symbol which just became indirect if DIR isn't a hidden versioned
6928 symbol. */
4d269e42 6929
422f1182 6930 if (dir->versioned != versioned_hidden)
6e33951e
L
6931 {
6932 dir->ref_dynamic |= ind->ref_dynamic;
6933 dir->ref_regular |= ind->ref_regular;
6934 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6935 dir->non_got_ref |= ind->non_got_ref;
6936 dir->needs_plt |= ind->needs_plt;
6937 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6938 }
4d269e42
AM
6939
6940 if (ind->root.type != bfd_link_hash_indirect)
6941 return;
6942
6943 /* Copy over the global and procedure linkage table refcount entries.
6944 These may have been already set up by a check_relocs routine. */
6945 htab = elf_hash_table (info);
6946 if (ind->got.refcount > htab->init_got_refcount.refcount)
6947 {
6948 if (dir->got.refcount < 0)
6949 dir->got.refcount = 0;
6950 dir->got.refcount += ind->got.refcount;
6951 ind->got.refcount = htab->init_got_refcount.refcount;
6952 }
6953
6954 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6955 {
6956 if (dir->plt.refcount < 0)
6957 dir->plt.refcount = 0;
6958 dir->plt.refcount += ind->plt.refcount;
6959 ind->plt.refcount = htab->init_plt_refcount.refcount;
6960 }
6961
6962 if (ind->dynindx != -1)
6963 {
6964 if (dir->dynindx != -1)
6965 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6966 dir->dynindx = ind->dynindx;
6967 dir->dynstr_index = ind->dynstr_index;
6968 ind->dynindx = -1;
6969 ind->dynstr_index = 0;
6970 }
6971}
6972
6973void
6974_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6975 struct elf_link_hash_entry *h,
6976 bfd_boolean force_local)
6977{
3aa14d16
L
6978 /* STT_GNU_IFUNC symbol must go through PLT. */
6979 if (h->type != STT_GNU_IFUNC)
6980 {
6981 h->plt = elf_hash_table (info)->init_plt_offset;
6982 h->needs_plt = 0;
6983 }
4d269e42
AM
6984 if (force_local)
6985 {
6986 h->forced_local = 1;
6987 if (h->dynindx != -1)
6988 {
6989 h->dynindx = -1;
6990 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6991 h->dynstr_index);
6992 }
6993 }
6994}
6995
7bf52ea2
AM
6996/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6997 caller. */
4d269e42
AM
6998
6999bfd_boolean
7000_bfd_elf_link_hash_table_init
7001 (struct elf_link_hash_table *table,
7002 bfd *abfd,
7003 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7004 struct bfd_hash_table *,
7005 const char *),
4dfe6ac6
NC
7006 unsigned int entsize,
7007 enum elf_target_id target_id)
4d269e42
AM
7008{
7009 bfd_boolean ret;
7010 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7011
4d269e42
AM
7012 table->init_got_refcount.refcount = can_refcount - 1;
7013 table->init_plt_refcount.refcount = can_refcount - 1;
7014 table->init_got_offset.offset = -(bfd_vma) 1;
7015 table->init_plt_offset.offset = -(bfd_vma) 1;
7016 /* The first dynamic symbol is a dummy. */
7017 table->dynsymcount = 1;
7018
7019 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7020
4d269e42 7021 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7022 table->hash_table_id = target_id;
4d269e42
AM
7023
7024 return ret;
7025}
7026
7027/* Create an ELF linker hash table. */
7028
7029struct bfd_link_hash_table *
7030_bfd_elf_link_hash_table_create (bfd *abfd)
7031{
7032 struct elf_link_hash_table *ret;
7033 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7034
7bf52ea2 7035 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7036 if (ret == NULL)
7037 return NULL;
7038
7039 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7040 sizeof (struct elf_link_hash_entry),
7041 GENERIC_ELF_DATA))
4d269e42
AM
7042 {
7043 free (ret);
7044 return NULL;
7045 }
d495ab0d 7046 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7047
7048 return &ret->root;
7049}
7050
9f7c3e5e
AM
7051/* Destroy an ELF linker hash table. */
7052
7053void
d495ab0d 7054_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7055{
d495ab0d
AM
7056 struct elf_link_hash_table *htab;
7057
7058 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7059 if (htab->dynstr != NULL)
7060 _bfd_elf_strtab_free (htab->dynstr);
7061 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7062 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7063}
7064
4d269e42
AM
7065/* This is a hook for the ELF emulation code in the generic linker to
7066 tell the backend linker what file name to use for the DT_NEEDED
7067 entry for a dynamic object. */
7068
7069void
7070bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7071{
7072 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7073 && bfd_get_format (abfd) == bfd_object)
7074 elf_dt_name (abfd) = name;
7075}
7076
7077int
7078bfd_elf_get_dyn_lib_class (bfd *abfd)
7079{
7080 int lib_class;
7081 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7082 && bfd_get_format (abfd) == bfd_object)
7083 lib_class = elf_dyn_lib_class (abfd);
7084 else
7085 lib_class = 0;
7086 return lib_class;
7087}
7088
7089void
7090bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7091{
7092 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7093 && bfd_get_format (abfd) == bfd_object)
7094 elf_dyn_lib_class (abfd) = lib_class;
7095}
7096
7097/* Get the list of DT_NEEDED entries for a link. This is a hook for
7098 the linker ELF emulation code. */
7099
7100struct bfd_link_needed_list *
7101bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7102 struct bfd_link_info *info)
7103{
7104 if (! is_elf_hash_table (info->hash))
7105 return NULL;
7106 return elf_hash_table (info)->needed;
7107}
7108
7109/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7110 hook for the linker ELF emulation code. */
7111
7112struct bfd_link_needed_list *
7113bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7114 struct bfd_link_info *info)
7115{
7116 if (! is_elf_hash_table (info->hash))
7117 return NULL;
7118 return elf_hash_table (info)->runpath;
7119}
7120
7121/* Get the name actually used for a dynamic object for a link. This
7122 is the SONAME entry if there is one. Otherwise, it is the string
7123 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7124
7125const char *
7126bfd_elf_get_dt_soname (bfd *abfd)
7127{
7128 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7129 && bfd_get_format (abfd) == bfd_object)
7130 return elf_dt_name (abfd);
7131 return NULL;
7132}
7133
7134/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7135 the ELF linker emulation code. */
7136
7137bfd_boolean
7138bfd_elf_get_bfd_needed_list (bfd *abfd,
7139 struct bfd_link_needed_list **pneeded)
7140{
7141 asection *s;
7142 bfd_byte *dynbuf = NULL;
cb33740c 7143 unsigned int elfsec;
4d269e42
AM
7144 unsigned long shlink;
7145 bfd_byte *extdyn, *extdynend;
7146 size_t extdynsize;
7147 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7148
7149 *pneeded = NULL;
7150
7151 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7152 || bfd_get_format (abfd) != bfd_object)
7153 return TRUE;
7154
7155 s = bfd_get_section_by_name (abfd, ".dynamic");
7156 if (s == NULL || s->size == 0)
7157 return TRUE;
7158
7159 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7160 goto error_return;
7161
7162 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7163 if (elfsec == SHN_BAD)
4d269e42
AM
7164 goto error_return;
7165
7166 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7167
4d269e42
AM
7168 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7169 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7170
7171 extdyn = dynbuf;
7172 extdynend = extdyn + s->size;
7173 for (; extdyn < extdynend; extdyn += extdynsize)
7174 {
7175 Elf_Internal_Dyn dyn;
7176
7177 (*swap_dyn_in) (abfd, extdyn, &dyn);
7178
7179 if (dyn.d_tag == DT_NULL)
7180 break;
7181
7182 if (dyn.d_tag == DT_NEEDED)
7183 {
7184 const char *string;
7185 struct bfd_link_needed_list *l;
7186 unsigned int tagv = dyn.d_un.d_val;
7187 bfd_size_type amt;
7188
7189 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7190 if (string == NULL)
7191 goto error_return;
7192
7193 amt = sizeof *l;
a50b1753 7194 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7195 if (l == NULL)
7196 goto error_return;
7197
7198 l->by = abfd;
7199 l->name = string;
7200 l->next = *pneeded;
7201 *pneeded = l;
7202 }
7203 }
7204
7205 free (dynbuf);
7206
7207 return TRUE;
7208
7209 error_return:
7210 if (dynbuf != NULL)
7211 free (dynbuf);
7212 return FALSE;
7213}
7214
7215struct elf_symbuf_symbol
7216{
7217 unsigned long st_name; /* Symbol name, index in string tbl */
7218 unsigned char st_info; /* Type and binding attributes */
7219 unsigned char st_other; /* Visibilty, and target specific */
7220};
7221
7222struct elf_symbuf_head
7223{
7224 struct elf_symbuf_symbol *ssym;
7225 bfd_size_type count;
7226 unsigned int st_shndx;
7227};
7228
7229struct elf_symbol
7230{
7231 union
7232 {
7233 Elf_Internal_Sym *isym;
7234 struct elf_symbuf_symbol *ssym;
7235 } u;
7236 const char *name;
7237};
7238
7239/* Sort references to symbols by ascending section number. */
7240
7241static int
7242elf_sort_elf_symbol (const void *arg1, const void *arg2)
7243{
7244 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7245 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7246
7247 return s1->st_shndx - s2->st_shndx;
7248}
7249
7250static int
7251elf_sym_name_compare (const void *arg1, const void *arg2)
7252{
7253 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7254 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7255 return strcmp (s1->name, s2->name);
7256}
7257
7258static struct elf_symbuf_head *
7259elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7260{
14b1c01e 7261 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7262 struct elf_symbuf_symbol *ssym;
7263 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7264 bfd_size_type i, shndx_count, total_size;
4d269e42 7265
a50b1753 7266 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7267 if (indbuf == NULL)
7268 return NULL;
7269
7270 for (ind = indbuf, i = 0; i < symcount; i++)
7271 if (isymbuf[i].st_shndx != SHN_UNDEF)
7272 *ind++ = &isymbuf[i];
7273 indbufend = ind;
7274
7275 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7276 elf_sort_elf_symbol);
7277
7278 shndx_count = 0;
7279 if (indbufend > indbuf)
7280 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7281 if (ind[0]->st_shndx != ind[1]->st_shndx)
7282 shndx_count++;
7283
3ae181ee
L
7284 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7285 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7286 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7287 if (ssymbuf == NULL)
7288 {
7289 free (indbuf);
7290 return NULL;
7291 }
7292
3ae181ee 7293 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7294 ssymbuf->ssym = NULL;
7295 ssymbuf->count = shndx_count;
7296 ssymbuf->st_shndx = 0;
7297 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7298 {
7299 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7300 {
7301 ssymhead++;
7302 ssymhead->ssym = ssym;
7303 ssymhead->count = 0;
7304 ssymhead->st_shndx = (*ind)->st_shndx;
7305 }
7306 ssym->st_name = (*ind)->st_name;
7307 ssym->st_info = (*ind)->st_info;
7308 ssym->st_other = (*ind)->st_other;
7309 ssymhead->count++;
7310 }
3ae181ee
L
7311 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7312 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7313 == total_size));
4d269e42
AM
7314
7315 free (indbuf);
7316 return ssymbuf;
7317}
7318
7319/* Check if 2 sections define the same set of local and global
7320 symbols. */
7321
8f317e31 7322static bfd_boolean
4d269e42
AM
7323bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7324 struct bfd_link_info *info)
7325{
7326 bfd *bfd1, *bfd2;
7327 const struct elf_backend_data *bed1, *bed2;
7328 Elf_Internal_Shdr *hdr1, *hdr2;
7329 bfd_size_type symcount1, symcount2;
7330 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7331 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7332 Elf_Internal_Sym *isym, *isymend;
7333 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7334 bfd_size_type count1, count2, i;
cb33740c 7335 unsigned int shndx1, shndx2;
4d269e42
AM
7336 bfd_boolean result;
7337
7338 bfd1 = sec1->owner;
7339 bfd2 = sec2->owner;
7340
4d269e42
AM
7341 /* Both sections have to be in ELF. */
7342 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7343 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7344 return FALSE;
7345
7346 if (elf_section_type (sec1) != elf_section_type (sec2))
7347 return FALSE;
7348
4d269e42
AM
7349 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7350 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7351 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7352 return FALSE;
7353
7354 bed1 = get_elf_backend_data (bfd1);
7355 bed2 = get_elf_backend_data (bfd2);
7356 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7357 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7358 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7359 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7360
7361 if (symcount1 == 0 || symcount2 == 0)
7362 return FALSE;
7363
7364 result = FALSE;
7365 isymbuf1 = NULL;
7366 isymbuf2 = NULL;
a50b1753
NC
7367 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7368 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7369
7370 if (ssymbuf1 == NULL)
7371 {
7372 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7373 NULL, NULL, NULL);
7374 if (isymbuf1 == NULL)
7375 goto done;
7376
7377 if (!info->reduce_memory_overheads)
7378 elf_tdata (bfd1)->symbuf = ssymbuf1
7379 = elf_create_symbuf (symcount1, isymbuf1);
7380 }
7381
7382 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7383 {
7384 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7385 NULL, NULL, NULL);
7386 if (isymbuf2 == NULL)
7387 goto done;
7388
7389 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7390 elf_tdata (bfd2)->symbuf = ssymbuf2
7391 = elf_create_symbuf (symcount2, isymbuf2);
7392 }
7393
7394 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7395 {
7396 /* Optimized faster version. */
7397 bfd_size_type lo, hi, mid;
7398 struct elf_symbol *symp;
7399 struct elf_symbuf_symbol *ssym, *ssymend;
7400
7401 lo = 0;
7402 hi = ssymbuf1->count;
7403 ssymbuf1++;
7404 count1 = 0;
7405 while (lo < hi)
7406 {
7407 mid = (lo + hi) / 2;
cb33740c 7408 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7409 hi = mid;
cb33740c 7410 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7411 lo = mid + 1;
7412 else
7413 {
7414 count1 = ssymbuf1[mid].count;
7415 ssymbuf1 += mid;
7416 break;
7417 }
7418 }
7419
7420 lo = 0;
7421 hi = ssymbuf2->count;
7422 ssymbuf2++;
7423 count2 = 0;
7424 while (lo < hi)
7425 {
7426 mid = (lo + hi) / 2;
cb33740c 7427 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7428 hi = mid;
cb33740c 7429 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7430 lo = mid + 1;
7431 else
7432 {
7433 count2 = ssymbuf2[mid].count;
7434 ssymbuf2 += mid;
7435 break;
7436 }
7437 }
7438
7439 if (count1 == 0 || count2 == 0 || count1 != count2)
7440 goto done;
7441
ca4be51c
AM
7442 symtable1
7443 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7444 symtable2
7445 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7446 if (symtable1 == NULL || symtable2 == NULL)
7447 goto done;
7448
7449 symp = symtable1;
7450 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7451 ssym < ssymend; ssym++, symp++)
7452 {
7453 symp->u.ssym = ssym;
7454 symp->name = bfd_elf_string_from_elf_section (bfd1,
7455 hdr1->sh_link,
7456 ssym->st_name);
7457 }
7458
7459 symp = symtable2;
7460 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7461 ssym < ssymend; ssym++, symp++)
7462 {
7463 symp->u.ssym = ssym;
7464 symp->name = bfd_elf_string_from_elf_section (bfd2,
7465 hdr2->sh_link,
7466 ssym->st_name);
7467 }
7468
7469 /* Sort symbol by name. */
7470 qsort (symtable1, count1, sizeof (struct elf_symbol),
7471 elf_sym_name_compare);
7472 qsort (symtable2, count1, sizeof (struct elf_symbol),
7473 elf_sym_name_compare);
7474
7475 for (i = 0; i < count1; i++)
7476 /* Two symbols must have the same binding, type and name. */
7477 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7478 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7479 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7480 goto done;
7481
7482 result = TRUE;
7483 goto done;
7484 }
7485
a50b1753
NC
7486 symtable1 = (struct elf_symbol *)
7487 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7488 symtable2 = (struct elf_symbol *)
7489 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7490 if (symtable1 == NULL || symtable2 == NULL)
7491 goto done;
7492
7493 /* Count definitions in the section. */
7494 count1 = 0;
7495 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7496 if (isym->st_shndx == shndx1)
4d269e42
AM
7497 symtable1[count1++].u.isym = isym;
7498
7499 count2 = 0;
7500 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7501 if (isym->st_shndx == shndx2)
4d269e42
AM
7502 symtable2[count2++].u.isym = isym;
7503
7504 if (count1 == 0 || count2 == 0 || count1 != count2)
7505 goto done;
7506
7507 for (i = 0; i < count1; i++)
7508 symtable1[i].name
7509 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7510 symtable1[i].u.isym->st_name);
7511
7512 for (i = 0; i < count2; i++)
7513 symtable2[i].name
7514 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7515 symtable2[i].u.isym->st_name);
7516
7517 /* Sort symbol by name. */
7518 qsort (symtable1, count1, sizeof (struct elf_symbol),
7519 elf_sym_name_compare);
7520 qsort (symtable2, count1, sizeof (struct elf_symbol),
7521 elf_sym_name_compare);
7522
7523 for (i = 0; i < count1; i++)
7524 /* Two symbols must have the same binding, type and name. */
7525 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7526 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7527 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7528 goto done;
7529
7530 result = TRUE;
7531
7532done:
7533 if (symtable1)
7534 free (symtable1);
7535 if (symtable2)
7536 free (symtable2);
7537 if (isymbuf1)
7538 free (isymbuf1);
7539 if (isymbuf2)
7540 free (isymbuf2);
7541
7542 return result;
7543}
7544
7545/* Return TRUE if 2 section types are compatible. */
7546
7547bfd_boolean
7548_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7549 bfd *bbfd, const asection *bsec)
7550{
7551 if (asec == NULL
7552 || bsec == NULL
7553 || abfd->xvec->flavour != bfd_target_elf_flavour
7554 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7555 return TRUE;
7556
7557 return elf_section_type (asec) == elf_section_type (bsec);
7558}
7559\f
c152c796
AM
7560/* Final phase of ELF linker. */
7561
7562/* A structure we use to avoid passing large numbers of arguments. */
7563
7564struct elf_final_link_info
7565{
7566 /* General link information. */
7567 struct bfd_link_info *info;
7568 /* Output BFD. */
7569 bfd *output_bfd;
7570 /* Symbol string table. */
ef10c3ac 7571 struct elf_strtab_hash *symstrtab;
c152c796
AM
7572 /* .hash section. */
7573 asection *hash_sec;
7574 /* symbol version section (.gnu.version). */
7575 asection *symver_sec;
7576 /* Buffer large enough to hold contents of any section. */
7577 bfd_byte *contents;
7578 /* Buffer large enough to hold external relocs of any section. */
7579 void *external_relocs;
7580 /* Buffer large enough to hold internal relocs of any section. */
7581 Elf_Internal_Rela *internal_relocs;
7582 /* Buffer large enough to hold external local symbols of any input
7583 BFD. */
7584 bfd_byte *external_syms;
7585 /* And a buffer for symbol section indices. */
7586 Elf_External_Sym_Shndx *locsym_shndx;
7587 /* Buffer large enough to hold internal local symbols of any input
7588 BFD. */
7589 Elf_Internal_Sym *internal_syms;
7590 /* Array large enough to hold a symbol index for each local symbol
7591 of any input BFD. */
7592 long *indices;
7593 /* Array large enough to hold a section pointer for each local
7594 symbol of any input BFD. */
7595 asection **sections;
ef10c3ac 7596 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7597 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7598 /* Number of STT_FILE syms seen. */
7599 size_t filesym_count;
c152c796
AM
7600};
7601
7602/* This struct is used to pass information to elf_link_output_extsym. */
7603
7604struct elf_outext_info
7605{
7606 bfd_boolean failed;
7607 bfd_boolean localsyms;
34a79995 7608 bfd_boolean file_sym_done;
8b127cbc 7609 struct elf_final_link_info *flinfo;
c152c796
AM
7610};
7611
d9352518
DB
7612
7613/* Support for evaluating a complex relocation.
7614
7615 Complex relocations are generalized, self-describing relocations. The
7616 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7617 relocations themselves.
d9352518
DB
7618
7619 The relocations are use a reserved elf-wide relocation type code (R_RELC
7620 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7621 information (start bit, end bit, word width, etc) into the addend. This
7622 information is extracted from CGEN-generated operand tables within gas.
7623
7624 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7625 internal) representing prefix-notation expressions, including but not
7626 limited to those sorts of expressions normally encoded as addends in the
7627 addend field. The symbol mangling format is:
7628
7629 <node> := <literal>
7630 | <unary-operator> ':' <node>
7631 | <binary-operator> ':' <node> ':' <node>
7632 ;
7633
7634 <literal> := 's' <digits=N> ':' <N character symbol name>
7635 | 'S' <digits=N> ':' <N character section name>
7636 | '#' <hexdigits>
7637 ;
7638
7639 <binary-operator> := as in C
7640 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7641
7642static void
a0c8462f
AM
7643set_symbol_value (bfd *bfd_with_globals,
7644 Elf_Internal_Sym *isymbuf,
7645 size_t locsymcount,
7646 size_t symidx,
7647 bfd_vma val)
d9352518 7648{
8977835c
AM
7649 struct elf_link_hash_entry **sym_hashes;
7650 struct elf_link_hash_entry *h;
7651 size_t extsymoff = locsymcount;
d9352518 7652
8977835c 7653 if (symidx < locsymcount)
d9352518 7654 {
8977835c
AM
7655 Elf_Internal_Sym *sym;
7656
7657 sym = isymbuf + symidx;
7658 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7659 {
7660 /* It is a local symbol: move it to the
7661 "absolute" section and give it a value. */
7662 sym->st_shndx = SHN_ABS;
7663 sym->st_value = val;
7664 return;
7665 }
7666 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7667 extsymoff = 0;
d9352518 7668 }
8977835c
AM
7669
7670 /* It is a global symbol: set its link type
7671 to "defined" and give it a value. */
7672
7673 sym_hashes = elf_sym_hashes (bfd_with_globals);
7674 h = sym_hashes [symidx - extsymoff];
7675 while (h->root.type == bfd_link_hash_indirect
7676 || h->root.type == bfd_link_hash_warning)
7677 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7678 h->root.type = bfd_link_hash_defined;
7679 h->root.u.def.value = val;
7680 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7681}
7682
a0c8462f
AM
7683static bfd_boolean
7684resolve_symbol (const char *name,
7685 bfd *input_bfd,
8b127cbc 7686 struct elf_final_link_info *flinfo,
a0c8462f
AM
7687 bfd_vma *result,
7688 Elf_Internal_Sym *isymbuf,
7689 size_t locsymcount)
d9352518 7690{
a0c8462f
AM
7691 Elf_Internal_Sym *sym;
7692 struct bfd_link_hash_entry *global_entry;
7693 const char *candidate = NULL;
7694 Elf_Internal_Shdr *symtab_hdr;
7695 size_t i;
7696
d9352518
DB
7697 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7698
7699 for (i = 0; i < locsymcount; ++ i)
7700 {
8977835c 7701 sym = isymbuf + i;
d9352518
DB
7702
7703 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7704 continue;
7705
7706 candidate = bfd_elf_string_from_elf_section (input_bfd,
7707 symtab_hdr->sh_link,
7708 sym->st_name);
7709#ifdef DEBUG
0f02bbd9
AM
7710 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7711 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7712#endif
7713 if (candidate && strcmp (candidate, name) == 0)
7714 {
8b127cbc 7715 asection *sec = flinfo->sections [i];
d9352518 7716
0f02bbd9
AM
7717 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7718 *result += sec->output_offset + sec->output_section->vma;
d9352518 7719#ifdef DEBUG
0f02bbd9
AM
7720 printf ("Found symbol with value %8.8lx\n",
7721 (unsigned long) *result);
d9352518
DB
7722#endif
7723 return TRUE;
7724 }
7725 }
7726
7727 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7728 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7729 FALSE, FALSE, TRUE);
d9352518
DB
7730 if (!global_entry)
7731 return FALSE;
a0c8462f 7732
d9352518
DB
7733 if (global_entry->type == bfd_link_hash_defined
7734 || global_entry->type == bfd_link_hash_defweak)
7735 {
a0c8462f
AM
7736 *result = (global_entry->u.def.value
7737 + global_entry->u.def.section->output_section->vma
7738 + global_entry->u.def.section->output_offset);
d9352518 7739#ifdef DEBUG
0f02bbd9
AM
7740 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7741 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7742#endif
7743 return TRUE;
a0c8462f 7744 }
d9352518 7745
d9352518
DB
7746 return FALSE;
7747}
7748
7749static bfd_boolean
a0c8462f
AM
7750resolve_section (const char *name,
7751 asection *sections,
7752 bfd_vma *result)
d9352518 7753{
a0c8462f
AM
7754 asection *curr;
7755 unsigned int len;
d9352518 7756
a0c8462f 7757 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7758 if (strcmp (curr->name, name) == 0)
7759 {
7760 *result = curr->vma;
7761 return TRUE;
7762 }
7763
7764 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7765 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7766 {
7767 len = strlen (curr->name);
a0c8462f 7768 if (len > strlen (name))
d9352518
DB
7769 continue;
7770
7771 if (strncmp (curr->name, name, len) == 0)
7772 {
7773 if (strncmp (".end", name + len, 4) == 0)
7774 {
7775 *result = curr->vma + curr->size;
7776 return TRUE;
7777 }
7778
7779 /* Insert more pseudo-section names here, if you like. */
7780 }
7781 }
a0c8462f 7782
d9352518
DB
7783 return FALSE;
7784}
7785
7786static void
a0c8462f 7787undefined_reference (const char *reftype, const char *name)
d9352518 7788{
a0c8462f
AM
7789 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7790 reftype, name);
d9352518
DB
7791}
7792
7793static bfd_boolean
a0c8462f
AM
7794eval_symbol (bfd_vma *result,
7795 const char **symp,
7796 bfd *input_bfd,
8b127cbc 7797 struct elf_final_link_info *flinfo,
a0c8462f
AM
7798 bfd_vma dot,
7799 Elf_Internal_Sym *isymbuf,
7800 size_t locsymcount,
7801 int signed_p)
d9352518 7802{
4b93929b
NC
7803 size_t len;
7804 size_t symlen;
a0c8462f
AM
7805 bfd_vma a;
7806 bfd_vma b;
4b93929b 7807 char symbuf[4096];
0f02bbd9 7808 const char *sym = *symp;
a0c8462f
AM
7809 const char *symend;
7810 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7811
7812 len = strlen (sym);
7813 symend = sym + len;
7814
4b93929b 7815 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7816 {
7817 bfd_set_error (bfd_error_invalid_operation);
7818 return FALSE;
7819 }
a0c8462f 7820
d9352518
DB
7821 switch (* sym)
7822 {
7823 case '.':
0f02bbd9
AM
7824 *result = dot;
7825 *symp = sym + 1;
d9352518
DB
7826 return TRUE;
7827
7828 case '#':
0f02bbd9
AM
7829 ++sym;
7830 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7831 return TRUE;
7832
7833 case 'S':
7834 symbol_is_section = TRUE;
a0c8462f 7835 case 's':
0f02bbd9
AM
7836 ++sym;
7837 symlen = strtol (sym, (char **) symp, 10);
7838 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7839
4b93929b 7840 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7841 {
7842 bfd_set_error (bfd_error_invalid_operation);
7843 return FALSE;
7844 }
7845
7846 memcpy (symbuf, sym, symlen);
a0c8462f 7847 symbuf[symlen] = '\0';
0f02bbd9 7848 *symp = sym + symlen;
a0c8462f
AM
7849
7850 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7851 the symbol as a section, or vice-versa. so we're pretty liberal in our
7852 interpretation here; section means "try section first", not "must be a
7853 section", and likewise with symbol. */
7854
a0c8462f 7855 if (symbol_is_section)
d9352518 7856 {
8b127cbc
AM
7857 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7858 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7859 isymbuf, locsymcount))
d9352518
DB
7860 {
7861 undefined_reference ("section", symbuf);
7862 return FALSE;
7863 }
a0c8462f
AM
7864 }
7865 else
d9352518 7866 {
8b127cbc 7867 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7868 isymbuf, locsymcount)
8b127cbc 7869 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977835c 7870 result))
d9352518
DB
7871 {
7872 undefined_reference ("symbol", symbuf);
7873 return FALSE;
7874 }
7875 }
7876
7877 return TRUE;
a0c8462f 7878
d9352518
DB
7879 /* All that remains are operators. */
7880
7881#define UNARY_OP(op) \
7882 if (strncmp (sym, #op, strlen (#op)) == 0) \
7883 { \
7884 sym += strlen (#op); \
a0c8462f
AM
7885 if (*sym == ':') \
7886 ++sym; \
0f02bbd9 7887 *symp = sym; \
8b127cbc 7888 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7889 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7890 return FALSE; \
7891 if (signed_p) \
0f02bbd9 7892 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7893 else \
7894 *result = op a; \
d9352518
DB
7895 return TRUE; \
7896 }
7897
7898#define BINARY_OP(op) \
7899 if (strncmp (sym, #op, strlen (#op)) == 0) \
7900 { \
7901 sym += strlen (#op); \
a0c8462f
AM
7902 if (*sym == ':') \
7903 ++sym; \
0f02bbd9 7904 *symp = sym; \
8b127cbc 7905 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7906 isymbuf, locsymcount, signed_p)) \
a0c8462f 7907 return FALSE; \
0f02bbd9 7908 ++*symp; \
8b127cbc 7909 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7910 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7911 return FALSE; \
7912 if (signed_p) \
0f02bbd9 7913 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7914 else \
7915 *result = a op b; \
d9352518
DB
7916 return TRUE; \
7917 }
7918
7919 default:
7920 UNARY_OP (0-);
7921 BINARY_OP (<<);
7922 BINARY_OP (>>);
7923 BINARY_OP (==);
7924 BINARY_OP (!=);
7925 BINARY_OP (<=);
7926 BINARY_OP (>=);
7927 BINARY_OP (&&);
7928 BINARY_OP (||);
7929 UNARY_OP (~);
7930 UNARY_OP (!);
7931 BINARY_OP (*);
7932 BINARY_OP (/);
7933 BINARY_OP (%);
7934 BINARY_OP (^);
7935 BINARY_OP (|);
7936 BINARY_OP (&);
7937 BINARY_OP (+);
7938 BINARY_OP (-);
7939 BINARY_OP (<);
7940 BINARY_OP (>);
7941#undef UNARY_OP
7942#undef BINARY_OP
7943 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7944 bfd_set_error (bfd_error_invalid_operation);
7945 return FALSE;
7946 }
7947}
7948
d9352518 7949static void
a0c8462f
AM
7950put_value (bfd_vma size,
7951 unsigned long chunksz,
7952 bfd *input_bfd,
7953 bfd_vma x,
7954 bfd_byte *location)
d9352518
DB
7955{
7956 location += (size - chunksz);
7957
41cd1ad1 7958 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
7959 {
7960 switch (chunksz)
7961 {
d9352518
DB
7962 case 1:
7963 bfd_put_8 (input_bfd, x, location);
41cd1ad1 7964 x >>= 8;
d9352518
DB
7965 break;
7966 case 2:
7967 bfd_put_16 (input_bfd, x, location);
41cd1ad1 7968 x >>= 16;
d9352518
DB
7969 break;
7970 case 4:
7971 bfd_put_32 (input_bfd, x, location);
65164438
NC
7972 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7973 x >>= 16;
7974 x >>= 16;
d9352518 7975 break;
d9352518 7976#ifdef BFD64
41cd1ad1 7977 case 8:
d9352518 7978 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
7979 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7980 x >>= 32;
7981 x >>= 32;
7982 break;
d9352518 7983#endif
41cd1ad1
NC
7984 default:
7985 abort ();
d9352518
DB
7986 break;
7987 }
7988 }
7989}
7990
a0c8462f
AM
7991static bfd_vma
7992get_value (bfd_vma size,
7993 unsigned long chunksz,
7994 bfd *input_bfd,
7995 bfd_byte *location)
d9352518 7996{
9b239e0e 7997 int shift;
d9352518
DB
7998 bfd_vma x = 0;
7999
9b239e0e
NC
8000 /* Sanity checks. */
8001 BFD_ASSERT (chunksz <= sizeof (x)
8002 && size >= chunksz
8003 && chunksz != 0
8004 && (size % chunksz) == 0
8005 && input_bfd != NULL
8006 && location != NULL);
8007
8008 if (chunksz == sizeof (x))
8009 {
8010 BFD_ASSERT (size == chunksz);
8011
8012 /* Make sure that we do not perform an undefined shift operation.
8013 We know that size == chunksz so there will only be one iteration
8014 of the loop below. */
8015 shift = 0;
8016 }
8017 else
8018 shift = 8 * chunksz;
8019
a0c8462f 8020 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8021 {
8022 switch (chunksz)
8023 {
d9352518 8024 case 1:
9b239e0e 8025 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8026 break;
8027 case 2:
9b239e0e 8028 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8029 break;
8030 case 4:
9b239e0e 8031 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8032 break;
d9352518 8033#ifdef BFD64
9b239e0e
NC
8034 case 8:
8035 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8036 break;
9b239e0e
NC
8037#endif
8038 default:
8039 abort ();
d9352518
DB
8040 }
8041 }
8042 return x;
8043}
8044
a0c8462f
AM
8045static void
8046decode_complex_addend (unsigned long *start, /* in bits */
8047 unsigned long *oplen, /* in bits */
8048 unsigned long *len, /* in bits */
8049 unsigned long *wordsz, /* in bytes */
8050 unsigned long *chunksz, /* in bytes */
8051 unsigned long *lsb0_p,
8052 unsigned long *signed_p,
8053 unsigned long *trunc_p,
8054 unsigned long encoded)
d9352518
DB
8055{
8056 * start = encoded & 0x3F;
8057 * len = (encoded >> 6) & 0x3F;
8058 * oplen = (encoded >> 12) & 0x3F;
8059 * wordsz = (encoded >> 18) & 0xF;
8060 * chunksz = (encoded >> 22) & 0xF;
8061 * lsb0_p = (encoded >> 27) & 1;
8062 * signed_p = (encoded >> 28) & 1;
8063 * trunc_p = (encoded >> 29) & 1;
8064}
8065
cdfeee4f 8066bfd_reloc_status_type
0f02bbd9 8067bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8068 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8069 bfd_byte *contents,
8070 Elf_Internal_Rela *rel,
8071 bfd_vma relocation)
d9352518 8072{
0f02bbd9
AM
8073 bfd_vma shift, x, mask;
8074 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8075 bfd_reloc_status_type r;
d9352518
DB
8076
8077 /* Perform this reloc, since it is complex.
8078 (this is not to say that it necessarily refers to a complex
8079 symbol; merely that it is a self-describing CGEN based reloc.
8080 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8081 word size, etc) encoded within it.). */
d9352518 8082
a0c8462f
AM
8083 decode_complex_addend (&start, &oplen, &len, &wordsz,
8084 &chunksz, &lsb0_p, &signed_p,
8085 &trunc_p, rel->r_addend);
d9352518
DB
8086
8087 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8088
8089 if (lsb0_p)
8090 shift = (start + 1) - len;
8091 else
8092 shift = (8 * wordsz) - (start + len);
8093
5dabe785 8094 /* FIXME: octets_per_byte. */
a0c8462f 8095 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
8096
8097#ifdef DEBUG
8098 printf ("Doing complex reloc: "
8099 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8100 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8101 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8102 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8103 oplen, (unsigned long) x, (unsigned long) mask,
8104 (unsigned long) relocation);
d9352518
DB
8105#endif
8106
cdfeee4f 8107 r = bfd_reloc_ok;
d9352518 8108 if (! trunc_p)
cdfeee4f
AM
8109 /* Now do an overflow check. */
8110 r = bfd_check_overflow ((signed_p
8111 ? complain_overflow_signed
8112 : complain_overflow_unsigned),
8113 len, 0, (8 * wordsz),
8114 relocation);
a0c8462f 8115
d9352518
DB
8116 /* Do the deed. */
8117 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8118
8119#ifdef DEBUG
8120 printf (" relocation: %8.8lx\n"
8121 " shifted mask: %8.8lx\n"
8122 " shifted/masked reloc: %8.8lx\n"
8123 " result: %8.8lx\n",
9ccb8af9
AM
8124 (unsigned long) relocation, (unsigned long) (mask << shift),
8125 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8126#endif
5dabe785 8127 /* FIXME: octets_per_byte. */
d9352518 8128 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 8129 return r;
d9352518
DB
8130}
8131
0e287786
AM
8132/* Functions to read r_offset from external (target order) reloc
8133 entry. Faster than bfd_getl32 et al, because we let the compiler
8134 know the value is aligned. */
53df40a4 8135
0e287786
AM
8136static bfd_vma
8137ext32l_r_offset (const void *p)
53df40a4
AM
8138{
8139 union aligned32
8140 {
8141 uint32_t v;
8142 unsigned char c[4];
8143 };
8144 const union aligned32 *a
0e287786 8145 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8146
8147 uint32_t aval = ( (uint32_t) a->c[0]
8148 | (uint32_t) a->c[1] << 8
8149 | (uint32_t) a->c[2] << 16
8150 | (uint32_t) a->c[3] << 24);
0e287786 8151 return aval;
53df40a4
AM
8152}
8153
0e287786
AM
8154static bfd_vma
8155ext32b_r_offset (const void *p)
53df40a4
AM
8156{
8157 union aligned32
8158 {
8159 uint32_t v;
8160 unsigned char c[4];
8161 };
8162 const union aligned32 *a
0e287786 8163 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8164
8165 uint32_t aval = ( (uint32_t) a->c[0] << 24
8166 | (uint32_t) a->c[1] << 16
8167 | (uint32_t) a->c[2] << 8
8168 | (uint32_t) a->c[3]);
0e287786 8169 return aval;
53df40a4
AM
8170}
8171
8172#ifdef BFD_HOST_64_BIT
0e287786
AM
8173static bfd_vma
8174ext64l_r_offset (const void *p)
53df40a4
AM
8175{
8176 union aligned64
8177 {
8178 uint64_t v;
8179 unsigned char c[8];
8180 };
8181 const union aligned64 *a
0e287786 8182 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8183
8184 uint64_t aval = ( (uint64_t) a->c[0]
8185 | (uint64_t) a->c[1] << 8
8186 | (uint64_t) a->c[2] << 16
8187 | (uint64_t) a->c[3] << 24
8188 | (uint64_t) a->c[4] << 32
8189 | (uint64_t) a->c[5] << 40
8190 | (uint64_t) a->c[6] << 48
8191 | (uint64_t) a->c[7] << 56);
0e287786 8192 return aval;
53df40a4
AM
8193}
8194
0e287786
AM
8195static bfd_vma
8196ext64b_r_offset (const void *p)
53df40a4
AM
8197{
8198 union aligned64
8199 {
8200 uint64_t v;
8201 unsigned char c[8];
8202 };
8203 const union aligned64 *a
0e287786 8204 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8205
8206 uint64_t aval = ( (uint64_t) a->c[0] << 56
8207 | (uint64_t) a->c[1] << 48
8208 | (uint64_t) a->c[2] << 40
8209 | (uint64_t) a->c[3] << 32
8210 | (uint64_t) a->c[4] << 24
8211 | (uint64_t) a->c[5] << 16
8212 | (uint64_t) a->c[6] << 8
8213 | (uint64_t) a->c[7]);
0e287786 8214 return aval;
53df40a4
AM
8215}
8216#endif
8217
c152c796
AM
8218/* When performing a relocatable link, the input relocations are
8219 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8220 referenced must be updated. Update all the relocations found in
8221 RELDATA. */
c152c796 8222
bca6d0e3 8223static bfd_boolean
c152c796 8224elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8225 struct bfd_elf_section_reloc_data *reldata,
8226 bfd_boolean sort)
c152c796
AM
8227{
8228 unsigned int i;
8229 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8230 bfd_byte *erela;
8231 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8232 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8233 bfd_vma r_type_mask;
8234 int r_sym_shift;
d4730f92
BS
8235 unsigned int count = reldata->count;
8236 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8237
d4730f92 8238 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8239 {
8240 swap_in = bed->s->swap_reloc_in;
8241 swap_out = bed->s->swap_reloc_out;
8242 }
d4730f92 8243 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8244 {
8245 swap_in = bed->s->swap_reloca_in;
8246 swap_out = bed->s->swap_reloca_out;
8247 }
8248 else
8249 abort ();
8250
8251 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8252 abort ();
8253
8254 if (bed->s->arch_size == 32)
8255 {
8256 r_type_mask = 0xff;
8257 r_sym_shift = 8;
8258 }
8259 else
8260 {
8261 r_type_mask = 0xffffffff;
8262 r_sym_shift = 32;
8263 }
8264
d4730f92
BS
8265 erela = reldata->hdr->contents;
8266 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8267 {
8268 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8269 unsigned int j;
8270
8271 if (*rel_hash == NULL)
8272 continue;
8273
8274 BFD_ASSERT ((*rel_hash)->indx >= 0);
8275
8276 (*swap_in) (abfd, erela, irela);
8277 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8278 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8279 | (irela[j].r_info & r_type_mask));
8280 (*swap_out) (abfd, irela, erela);
8281 }
53df40a4 8282
0e287786 8283 if (sort && count != 0)
53df40a4 8284 {
0e287786
AM
8285 bfd_vma (*ext_r_off) (const void *);
8286 bfd_vma r_off;
8287 size_t elt_size;
8288 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8289 bfd_byte *buf = NULL;
28dbcedc
AM
8290
8291 if (bed->s->arch_size == 32)
8292 {
8293 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8294 ext_r_off = ext32l_r_offset;
28dbcedc 8295 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8296 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8297 else
8298 abort ();
8299 }
53df40a4 8300 else
28dbcedc 8301 {
53df40a4 8302#ifdef BFD_HOST_64_BIT
28dbcedc 8303 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8304 ext_r_off = ext64l_r_offset;
28dbcedc 8305 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8306 ext_r_off = ext64b_r_offset;
28dbcedc 8307 else
53df40a4 8308#endif
28dbcedc
AM
8309 abort ();
8310 }
0e287786 8311
bca6d0e3
AM
8312 /* Must use a stable sort here. A modified insertion sort,
8313 since the relocs are mostly sorted already. */
0e287786
AM
8314 elt_size = reldata->hdr->sh_entsize;
8315 base = reldata->hdr->contents;
8316 end = base + count * elt_size;
bca6d0e3 8317 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8318 abort ();
8319
8320 /* Ensure the first element is lowest. This acts as a sentinel,
8321 speeding the main loop below. */
8322 r_off = (*ext_r_off) (base);
8323 for (p = loc = base; (p += elt_size) < end; )
8324 {
8325 bfd_vma r_off2 = (*ext_r_off) (p);
8326 if (r_off > r_off2)
8327 {
8328 r_off = r_off2;
8329 loc = p;
8330 }
8331 }
8332 if (loc != base)
8333 {
8334 /* Don't just swap *base and *loc as that changes the order
8335 of the original base[0] and base[1] if they happen to
8336 have the same r_offset. */
bca6d0e3
AM
8337 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8338 memcpy (onebuf, loc, elt_size);
0e287786 8339 memmove (base + elt_size, base, loc - base);
bca6d0e3 8340 memcpy (base, onebuf, elt_size);
0e287786
AM
8341 }
8342
b29b8669 8343 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8344 {
8345 /* base to p is sorted, *p is next to insert. */
8346 r_off = (*ext_r_off) (p);
8347 /* Search the sorted region for location to insert. */
8348 loc = p - elt_size;
8349 while (r_off < (*ext_r_off) (loc))
8350 loc -= elt_size;
8351 loc += elt_size;
8352 if (loc != p)
8353 {
bca6d0e3
AM
8354 /* Chances are there is a run of relocs to insert here,
8355 from one of more input files. Files are not always
8356 linked in order due to the way elf_link_input_bfd is
8357 called. See pr17666. */
8358 size_t sortlen = p - loc;
8359 bfd_vma r_off2 = (*ext_r_off) (loc);
8360 size_t runlen = elt_size;
8361 size_t buf_size = 96 * 1024;
8362 while (p + runlen < end
8363 && (sortlen <= buf_size
8364 || runlen + elt_size <= buf_size)
8365 && r_off2 > (*ext_r_off) (p + runlen))
8366 runlen += elt_size;
8367 if (buf == NULL)
8368 {
8369 buf = bfd_malloc (buf_size);
8370 if (buf == NULL)
8371 return FALSE;
8372 }
8373 if (runlen < sortlen)
8374 {
8375 memcpy (buf, p, runlen);
8376 memmove (loc + runlen, loc, sortlen);
8377 memcpy (loc, buf, runlen);
8378 }
8379 else
8380 {
8381 memcpy (buf, loc, sortlen);
8382 memmove (loc, p, runlen);
8383 memcpy (loc + runlen, buf, sortlen);
8384 }
b29b8669 8385 p += runlen - elt_size;
0e287786
AM
8386 }
8387 }
8388 /* Hashes are no longer valid. */
28dbcedc
AM
8389 free (reldata->hashes);
8390 reldata->hashes = NULL;
bca6d0e3 8391 free (buf);
53df40a4 8392 }
bca6d0e3 8393 return TRUE;
c152c796
AM
8394}
8395
8396struct elf_link_sort_rela
8397{
8398 union {
8399 bfd_vma offset;
8400 bfd_vma sym_mask;
8401 } u;
8402 enum elf_reloc_type_class type;
8403 /* We use this as an array of size int_rels_per_ext_rel. */
8404 Elf_Internal_Rela rela[1];
8405};
8406
8407static int
8408elf_link_sort_cmp1 (const void *A, const void *B)
8409{
a50b1753
NC
8410 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8411 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8412 int relativea, relativeb;
8413
8414 relativea = a->type == reloc_class_relative;
8415 relativeb = b->type == reloc_class_relative;
8416
8417 if (relativea < relativeb)
8418 return 1;
8419 if (relativea > relativeb)
8420 return -1;
8421 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8422 return -1;
8423 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8424 return 1;
8425 if (a->rela->r_offset < b->rela->r_offset)
8426 return -1;
8427 if (a->rela->r_offset > b->rela->r_offset)
8428 return 1;
8429 return 0;
8430}
8431
8432static int
8433elf_link_sort_cmp2 (const void *A, const void *B)
8434{
a50b1753
NC
8435 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8436 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8437
7e612e98 8438 if (a->type < b->type)
c152c796 8439 return -1;
7e612e98 8440 if (a->type > b->type)
c152c796 8441 return 1;
7e612e98 8442 if (a->u.offset < b->u.offset)
c152c796 8443 return -1;
7e612e98 8444 if (a->u.offset > b->u.offset)
c152c796
AM
8445 return 1;
8446 if (a->rela->r_offset < b->rela->r_offset)
8447 return -1;
8448 if (a->rela->r_offset > b->rela->r_offset)
8449 return 1;
8450 return 0;
8451}
8452
8453static size_t
8454elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8455{
3410fea8 8456 asection *dynamic_relocs;
fc66a176
L
8457 asection *rela_dyn;
8458 asection *rel_dyn;
c152c796
AM
8459 bfd_size_type count, size;
8460 size_t i, ret, sort_elt, ext_size;
8461 bfd_byte *sort, *s_non_relative, *p;
8462 struct elf_link_sort_rela *sq;
8463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8464 int i2e = bed->s->int_rels_per_ext_rel;
8465 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8466 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8467 struct bfd_link_order *lo;
8468 bfd_vma r_sym_mask;
3410fea8 8469 bfd_boolean use_rela;
c152c796 8470
3410fea8
NC
8471 /* Find a dynamic reloc section. */
8472 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8473 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8474 if (rela_dyn != NULL && rela_dyn->size > 0
8475 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8476 {
3410fea8
NC
8477 bfd_boolean use_rela_initialised = FALSE;
8478
8479 /* This is just here to stop gcc from complaining.
8480 It's initialization checking code is not perfect. */
8481 use_rela = TRUE;
8482
8483 /* Both sections are present. Examine the sizes
8484 of the indirect sections to help us choose. */
8485 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8486 if (lo->type == bfd_indirect_link_order)
8487 {
8488 asection *o = lo->u.indirect.section;
8489
8490 if ((o->size % bed->s->sizeof_rela) == 0)
8491 {
8492 if ((o->size % bed->s->sizeof_rel) == 0)
8493 /* Section size is divisible by both rel and rela sizes.
8494 It is of no help to us. */
8495 ;
8496 else
8497 {
8498 /* Section size is only divisible by rela. */
8499 if (use_rela_initialised && (use_rela == FALSE))
8500 {
8501 _bfd_error_handler
8502 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8503 bfd_set_error (bfd_error_invalid_operation);
8504 return 0;
8505 }
8506 else
8507 {
8508 use_rela = TRUE;
8509 use_rela_initialised = TRUE;
8510 }
8511 }
8512 }
8513 else if ((o->size % bed->s->sizeof_rel) == 0)
8514 {
8515 /* Section size is only divisible by rel. */
8516 if (use_rela_initialised && (use_rela == TRUE))
8517 {
8518 _bfd_error_handler
8519 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8520 bfd_set_error (bfd_error_invalid_operation);
8521 return 0;
8522 }
8523 else
8524 {
8525 use_rela = FALSE;
8526 use_rela_initialised = TRUE;
8527 }
8528 }
8529 else
8530 {
8531 /* The section size is not divisible by either - something is wrong. */
8532 _bfd_error_handler
8533 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8534 bfd_set_error (bfd_error_invalid_operation);
8535 return 0;
8536 }
8537 }
8538
8539 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8540 if (lo->type == bfd_indirect_link_order)
8541 {
8542 asection *o = lo->u.indirect.section;
8543
8544 if ((o->size % bed->s->sizeof_rela) == 0)
8545 {
8546 if ((o->size % bed->s->sizeof_rel) == 0)
8547 /* Section size is divisible by both rel and rela sizes.
8548 It is of no help to us. */
8549 ;
8550 else
8551 {
8552 /* Section size is only divisible by rela. */
8553 if (use_rela_initialised && (use_rela == FALSE))
8554 {
8555 _bfd_error_handler
8556 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8557 bfd_set_error (bfd_error_invalid_operation);
8558 return 0;
8559 }
8560 else
8561 {
8562 use_rela = TRUE;
8563 use_rela_initialised = TRUE;
8564 }
8565 }
8566 }
8567 else if ((o->size % bed->s->sizeof_rel) == 0)
8568 {
8569 /* Section size is only divisible by rel. */
8570 if (use_rela_initialised && (use_rela == TRUE))
8571 {
8572 _bfd_error_handler
8573 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8574 bfd_set_error (bfd_error_invalid_operation);
8575 return 0;
8576 }
8577 else
8578 {
8579 use_rela = FALSE;
8580 use_rela_initialised = TRUE;
8581 }
8582 }
8583 else
8584 {
8585 /* The section size is not divisible by either - something is wrong. */
8586 _bfd_error_handler
8587 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8588 bfd_set_error (bfd_error_invalid_operation);
8589 return 0;
8590 }
8591 }
8592
8593 if (! use_rela_initialised)
8594 /* Make a guess. */
8595 use_rela = TRUE;
c152c796 8596 }
fc66a176
L
8597 else if (rela_dyn != NULL && rela_dyn->size > 0)
8598 use_rela = TRUE;
8599 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8600 use_rela = FALSE;
c152c796 8601 else
fc66a176 8602 return 0;
3410fea8
NC
8603
8604 if (use_rela)
c152c796 8605 {
3410fea8 8606 dynamic_relocs = rela_dyn;
c152c796
AM
8607 ext_size = bed->s->sizeof_rela;
8608 swap_in = bed->s->swap_reloca_in;
8609 swap_out = bed->s->swap_reloca_out;
8610 }
3410fea8
NC
8611 else
8612 {
8613 dynamic_relocs = rel_dyn;
8614 ext_size = bed->s->sizeof_rel;
8615 swap_in = bed->s->swap_reloc_in;
8616 swap_out = bed->s->swap_reloc_out;
8617 }
c152c796
AM
8618
8619 size = 0;
3410fea8 8620 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8621 if (lo->type == bfd_indirect_link_order)
3410fea8 8622 size += lo->u.indirect.section->size;
c152c796 8623
3410fea8 8624 if (size != dynamic_relocs->size)
c152c796
AM
8625 return 0;
8626
8627 sort_elt = (sizeof (struct elf_link_sort_rela)
8628 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8629
8630 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8631 if (count == 0)
8632 return 0;
a50b1753 8633 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8634
c152c796
AM
8635 if (sort == NULL)
8636 {
8637 (*info->callbacks->warning)
8638 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8639 return 0;
8640 }
8641
8642 if (bed->s->arch_size == 32)
8643 r_sym_mask = ~(bfd_vma) 0xff;
8644 else
8645 r_sym_mask = ~(bfd_vma) 0xffffffff;
8646
3410fea8 8647 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8648 if (lo->type == bfd_indirect_link_order)
8649 {
8650 bfd_byte *erel, *erelend;
8651 asection *o = lo->u.indirect.section;
8652
1da212d6
AM
8653 if (o->contents == NULL && o->size != 0)
8654 {
8655 /* This is a reloc section that is being handled as a normal
8656 section. See bfd_section_from_shdr. We can't combine
8657 relocs in this case. */
8658 free (sort);
8659 return 0;
8660 }
c152c796 8661 erel = o->contents;
eea6121a 8662 erelend = o->contents + o->size;
5dabe785 8663 /* FIXME: octets_per_byte. */
c152c796 8664 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8665
c152c796
AM
8666 while (erel < erelend)
8667 {
8668 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8669
c152c796 8670 (*swap_in) (abfd, erel, s->rela);
7e612e98 8671 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8672 s->u.sym_mask = r_sym_mask;
8673 p += sort_elt;
8674 erel += ext_size;
8675 }
8676 }
8677
8678 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8679
8680 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8681 {
8682 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8683 if (s->type != reloc_class_relative)
8684 break;
8685 }
8686 ret = i;
8687 s_non_relative = p;
8688
8689 sq = (struct elf_link_sort_rela *) s_non_relative;
8690 for (; i < count; i++, p += sort_elt)
8691 {
8692 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8693 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8694 sq = sp;
8695 sp->u.offset = sq->rela->r_offset;
8696 }
8697
8698 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8699
3410fea8 8700 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8701 if (lo->type == bfd_indirect_link_order)
8702 {
8703 bfd_byte *erel, *erelend;
8704 asection *o = lo->u.indirect.section;
8705
8706 erel = o->contents;
eea6121a 8707 erelend = o->contents + o->size;
5dabe785 8708 /* FIXME: octets_per_byte. */
c152c796
AM
8709 p = sort + o->output_offset / ext_size * sort_elt;
8710 while (erel < erelend)
8711 {
8712 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8713 (*swap_out) (abfd, s->rela, erel);
8714 p += sort_elt;
8715 erel += ext_size;
8716 }
8717 }
8718
8719 free (sort);
3410fea8 8720 *psec = dynamic_relocs;
c152c796
AM
8721 return ret;
8722}
8723
ef10c3ac 8724/* Add a symbol to the output symbol string table. */
c152c796 8725
6e0b88f1 8726static int
ef10c3ac
L
8727elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8728 const char *name,
8729 Elf_Internal_Sym *elfsym,
8730 asection *input_sec,
8731 struct elf_link_hash_entry *h)
c152c796 8732{
6e0b88f1 8733 int (*output_symbol_hook)
c152c796
AM
8734 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8735 struct elf_link_hash_entry *);
ef10c3ac 8736 struct elf_link_hash_table *hash_table;
c152c796 8737 const struct elf_backend_data *bed;
ef10c3ac 8738 bfd_size_type strtabsize;
c152c796 8739
8539e4e8
AM
8740 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8741
8b127cbc 8742 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8743 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8744 if (output_symbol_hook != NULL)
8745 {
8b127cbc 8746 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8747 if (ret != 1)
8748 return ret;
c152c796
AM
8749 }
8750
ef10c3ac
L
8751 if (name == NULL
8752 || *name == '\0'
8753 || (input_sec->flags & SEC_EXCLUDE))
8754 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8755 else
8756 {
ef10c3ac
L
8757 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8758 to get the final offset for st_name. */
8759 elfsym->st_name
8760 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8761 name, FALSE);
c152c796 8762 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8763 return 0;
c152c796
AM
8764 }
8765
ef10c3ac
L
8766 hash_table = elf_hash_table (flinfo->info);
8767 strtabsize = hash_table->strtabsize;
8768 if (strtabsize <= hash_table->strtabcount)
c152c796 8769 {
ef10c3ac
L
8770 strtabsize += strtabsize;
8771 hash_table->strtabsize = strtabsize;
8772 strtabsize *= sizeof (*hash_table->strtab);
8773 hash_table->strtab
8774 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8775 strtabsize);
8776 if (hash_table->strtab == NULL)
6e0b88f1 8777 return 0;
c152c796 8778 }
ef10c3ac
L
8779 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8780 hash_table->strtab[hash_table->strtabcount].dest_index
8781 = hash_table->strtabcount;
8782 hash_table->strtab[hash_table->strtabcount].destshndx_index
8783 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8784
8785 bfd_get_symcount (flinfo->output_bfd) += 1;
8786 hash_table->strtabcount += 1;
8787
8788 return 1;
8789}
8790
8791/* Swap symbols out to the symbol table and flush the output symbols to
8792 the file. */
8793
8794static bfd_boolean
8795elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8796{
8797 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8798 bfd_size_type amt, i;
8799 const struct elf_backend_data *bed;
8800 bfd_byte *symbuf;
8801 Elf_Internal_Shdr *hdr;
8802 file_ptr pos;
8803 bfd_boolean ret;
8804
8805 if (!hash_table->strtabcount)
8806 return TRUE;
8807
8808 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8809
8810 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8811
ef10c3ac
L
8812 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8813 symbuf = (bfd_byte *) bfd_malloc (amt);
8814 if (symbuf == NULL)
8815 return FALSE;
1b786873 8816
ef10c3ac 8817 if (flinfo->symshndxbuf)
c152c796 8818 {
ef10c3ac
L
8819 amt = (sizeof (Elf_External_Sym_Shndx)
8820 * (bfd_get_symcount (flinfo->output_bfd)));
8821 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8822 if (flinfo->symshndxbuf == NULL)
c152c796 8823 {
ef10c3ac
L
8824 free (symbuf);
8825 return FALSE;
c152c796 8826 }
c152c796
AM
8827 }
8828
ef10c3ac
L
8829 for (i = 0; i < hash_table->strtabcount; i++)
8830 {
8831 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8832 if (elfsym->sym.st_name == (unsigned long) -1)
8833 elfsym->sym.st_name = 0;
8834 else
8835 elfsym->sym.st_name
8836 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8837 elfsym->sym.st_name);
8838 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8839 ((bfd_byte *) symbuf
8840 + (elfsym->dest_index
8841 * bed->s->sizeof_sym)),
8842 (flinfo->symshndxbuf
8843 + elfsym->destshndx_index));
8844 }
8845
8846 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8847 pos = hdr->sh_offset + hdr->sh_size;
8848 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8849 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8850 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8851 {
8852 hdr->sh_size += amt;
8853 ret = TRUE;
8854 }
8855 else
8856 ret = FALSE;
c152c796 8857
ef10c3ac
L
8858 free (symbuf);
8859
8860 free (hash_table->strtab);
8861 hash_table->strtab = NULL;
8862
8863 return ret;
c152c796
AM
8864}
8865
c0d5a53d
L
8866/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8867
8868static bfd_boolean
8869check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8870{
4fbb74a6
AM
8871 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8872 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8873 {
8874 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8875 beyond 64k. */
c0d5a53d
L
8876 (*_bfd_error_handler)
8877 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8878 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8879 bfd_set_error (bfd_error_nonrepresentable_section);
8880 return FALSE;
8881 }
8882 return TRUE;
8883}
8884
c152c796
AM
8885/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8886 allowing an unsatisfied unversioned symbol in the DSO to match a
8887 versioned symbol that would normally require an explicit version.
8888 We also handle the case that a DSO references a hidden symbol
8889 which may be satisfied by a versioned symbol in another DSO. */
8890
8891static bfd_boolean
8892elf_link_check_versioned_symbol (struct bfd_link_info *info,
8893 const struct elf_backend_data *bed,
8894 struct elf_link_hash_entry *h)
8895{
8896 bfd *abfd;
8897 struct elf_link_loaded_list *loaded;
8898
8899 if (!is_elf_hash_table (info->hash))
8900 return FALSE;
8901
90c984fc
L
8902 /* Check indirect symbol. */
8903 while (h->root.type == bfd_link_hash_indirect)
8904 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8905
c152c796
AM
8906 switch (h->root.type)
8907 {
8908 default:
8909 abfd = NULL;
8910 break;
8911
8912 case bfd_link_hash_undefined:
8913 case bfd_link_hash_undefweak:
8914 abfd = h->root.u.undef.abfd;
8915 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8916 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8917 return FALSE;
8918 break;
8919
8920 case bfd_link_hash_defined:
8921 case bfd_link_hash_defweak:
8922 abfd = h->root.u.def.section->owner;
8923 break;
8924
8925 case bfd_link_hash_common:
8926 abfd = h->root.u.c.p->section->owner;
8927 break;
8928 }
8929 BFD_ASSERT (abfd != NULL);
8930
8931 for (loaded = elf_hash_table (info)->loaded;
8932 loaded != NULL;
8933 loaded = loaded->next)
8934 {
8935 bfd *input;
8936 Elf_Internal_Shdr *hdr;
8937 bfd_size_type symcount;
8938 bfd_size_type extsymcount;
8939 bfd_size_type extsymoff;
8940 Elf_Internal_Shdr *versymhdr;
8941 Elf_Internal_Sym *isym;
8942 Elf_Internal_Sym *isymend;
8943 Elf_Internal_Sym *isymbuf;
8944 Elf_External_Versym *ever;
8945 Elf_External_Versym *extversym;
8946
8947 input = loaded->abfd;
8948
8949 /* We check each DSO for a possible hidden versioned definition. */
8950 if (input == abfd
8951 || (input->flags & DYNAMIC) == 0
8952 || elf_dynversym (input) == 0)
8953 continue;
8954
8955 hdr = &elf_tdata (input)->dynsymtab_hdr;
8956
8957 symcount = hdr->sh_size / bed->s->sizeof_sym;
8958 if (elf_bad_symtab (input))
8959 {
8960 extsymcount = symcount;
8961 extsymoff = 0;
8962 }
8963 else
8964 {
8965 extsymcount = symcount - hdr->sh_info;
8966 extsymoff = hdr->sh_info;
8967 }
8968
8969 if (extsymcount == 0)
8970 continue;
8971
8972 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8973 NULL, NULL, NULL);
8974 if (isymbuf == NULL)
8975 return FALSE;
8976
8977 /* Read in any version definitions. */
8978 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8979 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8980 if (extversym == NULL)
8981 goto error_ret;
8982
8983 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8984 || (bfd_bread (extversym, versymhdr->sh_size, input)
8985 != versymhdr->sh_size))
8986 {
8987 free (extversym);
8988 error_ret:
8989 free (isymbuf);
8990 return FALSE;
8991 }
8992
8993 ever = extversym + extsymoff;
8994 isymend = isymbuf + extsymcount;
8995 for (isym = isymbuf; isym < isymend; isym++, ever++)
8996 {
8997 const char *name;
8998 Elf_Internal_Versym iver;
8999 unsigned short version_index;
9000
9001 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9002 || isym->st_shndx == SHN_UNDEF)
9003 continue;
9004
9005 name = bfd_elf_string_from_elf_section (input,
9006 hdr->sh_link,
9007 isym->st_name);
9008 if (strcmp (name, h->root.root.string) != 0)
9009 continue;
9010
9011 _bfd_elf_swap_versym_in (input, ever, &iver);
9012
d023c380
L
9013 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9014 && !(h->def_regular
9015 && h->forced_local))
c152c796
AM
9016 {
9017 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9018 have provided a definition for the undefined sym unless
9019 it is defined in a non-shared object and forced local.
9020 */
c152c796
AM
9021 abort ();
9022 }
9023
9024 version_index = iver.vs_vers & VERSYM_VERSION;
9025 if (version_index == 1 || version_index == 2)
9026 {
9027 /* This is the base or first version. We can use it. */
9028 free (extversym);
9029 free (isymbuf);
9030 return TRUE;
9031 }
9032 }
9033
9034 free (extversym);
9035 free (isymbuf);
9036 }
9037
9038 return FALSE;
9039}
9040
9041/* Add an external symbol to the symbol table. This is called from
9042 the hash table traversal routine. When generating a shared object,
9043 we go through the symbol table twice. The first time we output
9044 anything that might have been forced to local scope in a version
9045 script. The second time we output the symbols that are still
9046 global symbols. */
9047
9048static bfd_boolean
7686d77d 9049elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9050{
7686d77d 9051 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9052 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9053 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9054 bfd_boolean strip;
9055 Elf_Internal_Sym sym;
9056 asection *input_sec;
9057 const struct elf_backend_data *bed;
6e0b88f1
AM
9058 long indx;
9059 int ret;
6e33951e
L
9060 /* A symbol is bound locally if it is forced local or it is locally
9061 defined, hidden versioned, not referenced by shared library and
9062 not exported when linking executable. */
9063 bfd_boolean local_bind = (h->forced_local
0e1862bb 9064 || (bfd_link_executable (flinfo->info)
6e33951e
L
9065 && !flinfo->info->export_dynamic
9066 && !h->dynamic
9067 && !h->ref_dynamic
9068 && h->def_regular
422f1182 9069 && h->versioned == versioned_hidden));
c152c796
AM
9070
9071 if (h->root.type == bfd_link_hash_warning)
9072 {
9073 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9074 if (h->root.type == bfd_link_hash_new)
9075 return TRUE;
9076 }
9077
9078 /* Decide whether to output this symbol in this pass. */
9079 if (eoinfo->localsyms)
9080 {
6e33951e 9081 if (!local_bind)
c152c796
AM
9082 return TRUE;
9083 }
9084 else
9085 {
6e33951e 9086 if (local_bind)
c152c796
AM
9087 return TRUE;
9088 }
9089
8b127cbc 9090 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9091
12ac1cf5 9092 if (h->root.type == bfd_link_hash_undefined)
c152c796 9093 {
12ac1cf5
NC
9094 /* If we have an undefined symbol reference here then it must have
9095 come from a shared library that is being linked in. (Undefined
98da7939
L
9096 references in regular files have already been handled unless
9097 they are in unreferenced sections which are removed by garbage
9098 collection). */
12ac1cf5
NC
9099 bfd_boolean ignore_undef = FALSE;
9100
9101 /* Some symbols may be special in that the fact that they're
9102 undefined can be safely ignored - let backend determine that. */
9103 if (bed->elf_backend_ignore_undef_symbol)
9104 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9105
9106 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9107 if (!ignore_undef
12ac1cf5 9108 && h->ref_dynamic
8b127cbc
AM
9109 && (!h->ref_regular || flinfo->info->gc_sections)
9110 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9111 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9112 {
9113 if (!(flinfo->info->callbacks->undefined_symbol
9114 (flinfo->info, h->root.root.string,
9115 h->ref_regular ? NULL : h->root.u.undef.abfd,
9116 NULL, 0,
9117 (flinfo->info->unresolved_syms_in_shared_libs
9118 == RM_GENERATE_ERROR))))
12ac1cf5 9119 {
17d078c5 9120 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
9121 eoinfo->failed = TRUE;
9122 return FALSE;
9123 }
c152c796
AM
9124 }
9125 }
9126
9127 /* We should also warn if a forced local symbol is referenced from
9128 shared libraries. */
0e1862bb 9129 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9130 && h->forced_local
9131 && h->ref_dynamic
371a5866 9132 && h->def_regular
f5385ebf 9133 && !h->dynamic_def
ee659f1f 9134 && h->ref_dynamic_nonweak
8b127cbc 9135 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9136 {
17d078c5
AM
9137 bfd *def_bfd;
9138 const char *msg;
90c984fc
L
9139 struct elf_link_hash_entry *hi = h;
9140
9141 /* Check indirect symbol. */
9142 while (hi->root.type == bfd_link_hash_indirect)
9143 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9144
9145 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9146 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9147 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9148 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9149 else
9150 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9151 def_bfd = flinfo->output_bfd;
90c984fc
L
9152 if (hi->root.u.def.section != bfd_abs_section_ptr)
9153 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9154 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9155 h->root.root.string);
9156 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9157 eoinfo->failed = TRUE;
9158 return FALSE;
9159 }
9160
9161 /* We don't want to output symbols that have never been mentioned by
9162 a regular file, or that we have been told to strip. However, if
9163 h->indx is set to -2, the symbol is used by a reloc and we must
9164 output it. */
d983c8c5 9165 strip = FALSE;
c152c796 9166 if (h->indx == -2)
d983c8c5 9167 ;
f5385ebf 9168 else if ((h->def_dynamic
77cfaee6
AM
9169 || h->ref_dynamic
9170 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9171 && !h->def_regular
9172 && !h->ref_regular)
c152c796 9173 strip = TRUE;
8b127cbc 9174 else if (flinfo->info->strip == strip_all)
c152c796 9175 strip = TRUE;
8b127cbc
AM
9176 else if (flinfo->info->strip == strip_some
9177 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9178 h->root.root.string, FALSE, FALSE) == NULL)
9179 strip = TRUE;
d56d55e7
AM
9180 else if ((h->root.type == bfd_link_hash_defined
9181 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9182 && ((flinfo->info->strip_discarded
dbaa2011 9183 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9184 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9185 && h->root.u.def.section->owner != NULL
d56d55e7 9186 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9187 strip = TRUE;
9e2278f5
AM
9188 else if ((h->root.type == bfd_link_hash_undefined
9189 || h->root.type == bfd_link_hash_undefweak)
9190 && h->root.u.undef.abfd != NULL
9191 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9192 strip = TRUE;
c152c796
AM
9193
9194 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9195 nothing else to do. However, if it is a forced local symbol or
9196 an ifunc symbol we need to give the backend finish_dynamic_symbol
9197 function a chance to make it dynamic. */
c152c796
AM
9198 if (strip
9199 && h->dynindx == -1
57ca8ac7 9200 && h->type != STT_GNU_IFUNC
f5385ebf 9201 && !h->forced_local)
c152c796
AM
9202 return TRUE;
9203
9204 sym.st_value = 0;
9205 sym.st_size = h->size;
9206 sym.st_other = h->other;
6e33951e 9207 if (local_bind)
935bd1e0
L
9208 {
9209 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9210 /* Turn off visibility on local symbol. */
9211 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9212 }
02acbe22
L
9213 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9214 else if (h->unique_global && h->def_regular)
3e7a7d11 9215 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
9216 else if (h->root.type == bfd_link_hash_undefweak
9217 || h->root.type == bfd_link_hash_defweak)
9218 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9219 else
9220 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 9221 sym.st_target_internal = h->target_internal;
c152c796
AM
9222
9223 switch (h->root.type)
9224 {
9225 default:
9226 case bfd_link_hash_new:
9227 case bfd_link_hash_warning:
9228 abort ();
9229 return FALSE;
9230
9231 case bfd_link_hash_undefined:
9232 case bfd_link_hash_undefweak:
9233 input_sec = bfd_und_section_ptr;
9234 sym.st_shndx = SHN_UNDEF;
9235 break;
9236
9237 case bfd_link_hash_defined:
9238 case bfd_link_hash_defweak:
9239 {
9240 input_sec = h->root.u.def.section;
9241 if (input_sec->output_section != NULL)
9242 {
9243 sym.st_shndx =
8b127cbc 9244 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9245 input_sec->output_section);
9246 if (sym.st_shndx == SHN_BAD)
9247 {
9248 (*_bfd_error_handler)
d003868e 9249 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9250 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9251 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9252 eoinfo->failed = TRUE;
9253 return FALSE;
9254 }
9255
9256 /* ELF symbols in relocatable files are section relative,
9257 but in nonrelocatable files they are virtual
9258 addresses. */
9259 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9260 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9261 {
9262 sym.st_value += input_sec->output_section->vma;
9263 if (h->type == STT_TLS)
9264 {
8b127cbc 9265 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9266 if (tls_sec != NULL)
9267 sym.st_value -= tls_sec->vma;
c152c796
AM
9268 }
9269 }
9270 }
9271 else
9272 {
9273 BFD_ASSERT (input_sec->owner == NULL
9274 || (input_sec->owner->flags & DYNAMIC) != 0);
9275 sym.st_shndx = SHN_UNDEF;
9276 input_sec = bfd_und_section_ptr;
9277 }
9278 }
9279 break;
9280
9281 case bfd_link_hash_common:
9282 input_sec = h->root.u.c.p->section;
a4d8e49b 9283 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9284 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9285 break;
9286
9287 case bfd_link_hash_indirect:
9288 /* These symbols are created by symbol versioning. They point
9289 to the decorated version of the name. For example, if the
9290 symbol foo@@GNU_1.2 is the default, which should be used when
9291 foo is used with no version, then we add an indirect symbol
9292 foo which points to foo@@GNU_1.2. We ignore these symbols,
9293 since the indirected symbol is already in the hash table. */
9294 return TRUE;
9295 }
9296
9297 /* Give the processor backend a chance to tweak the symbol value,
9298 and also to finish up anything that needs to be done for this
9299 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9300 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9301 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9302 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9303 && h->def_regular
0e1862bb 9304 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9305 || ((h->dynindx != -1
9306 || h->forced_local)
0e1862bb 9307 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9308 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9309 || h->root.type != bfd_link_hash_undefweak))
9310 || !h->forced_local)
8b127cbc 9311 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9312 {
9313 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9314 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9315 {
9316 eoinfo->failed = TRUE;
9317 return FALSE;
9318 }
9319 }
9320
9321 /* If we are marking the symbol as undefined, and there are no
9322 non-weak references to this symbol from a regular object, then
9323 mark the symbol as weak undefined; if there are non-weak
9324 references, mark the symbol as strong. We can't do this earlier,
9325 because it might not be marked as undefined until the
9326 finish_dynamic_symbol routine gets through with it. */
9327 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9328 && h->ref_regular
c152c796
AM
9329 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9330 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9331 {
9332 int bindtype;
2955ec4c
L
9333 unsigned int type = ELF_ST_TYPE (sym.st_info);
9334
9335 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9336 if (type == STT_GNU_IFUNC)
9337 type = STT_FUNC;
c152c796 9338
f5385ebf 9339 if (h->ref_regular_nonweak)
c152c796
AM
9340 bindtype = STB_GLOBAL;
9341 else
9342 bindtype = STB_WEAK;
2955ec4c 9343 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9344 }
9345
bda987c2
CD
9346 /* If this is a symbol defined in a dynamic library, don't use the
9347 symbol size from the dynamic library. Relinking an executable
9348 against a new library may introduce gratuitous changes in the
9349 executable's symbols if we keep the size. */
9350 if (sym.st_shndx == SHN_UNDEF
9351 && !h->def_regular
9352 && h->def_dynamic)
9353 sym.st_size = 0;
9354
c152c796
AM
9355 /* If a non-weak symbol with non-default visibility is not defined
9356 locally, it is a fatal error. */
0e1862bb 9357 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9358 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9359 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9360 && h->root.type == bfd_link_hash_undefined
f5385ebf 9361 && !h->def_regular)
c152c796 9362 {
17d078c5
AM
9363 const char *msg;
9364
9365 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9366 msg = _("%B: protected symbol `%s' isn't defined");
9367 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9368 msg = _("%B: internal symbol `%s' isn't defined");
9369 else
9370 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9371 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9372 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9373 eoinfo->failed = TRUE;
9374 return FALSE;
9375 }
9376
9377 /* If this symbol should be put in the .dynsym section, then put it
9378 there now. We already know the symbol index. We also fill in
9379 the entry in the .hash section. */
cae1fbbb 9380 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9381 && h->dynindx != -1
8b127cbc 9382 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9383 {
c152c796
AM
9384 bfd_byte *esym;
9385
90c984fc
L
9386 /* Since there is no version information in the dynamic string,
9387 if there is no version info in symbol version section, we will
1659f720 9388 have a run-time problem if not linking executable, referenced
6e33951e
L
9389 by shared library, not locally defined, or not bound locally.
9390 */
1659f720 9391 if (h->verinfo.verdef == NULL
6e33951e 9392 && !local_bind
0e1862bb 9393 && (!bfd_link_executable (flinfo->info)
1659f720
L
9394 || h->ref_dynamic
9395 || !h->def_regular))
90c984fc
L
9396 {
9397 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9398
9399 if (p && p [1] != '\0')
9400 {
9401 (*_bfd_error_handler)
9402 (_("%B: No symbol version section for versioned symbol `%s'"),
9403 flinfo->output_bfd, h->root.root.string);
9404 eoinfo->failed = TRUE;
9405 return FALSE;
9406 }
9407 }
9408
c152c796 9409 sym.st_name = h->dynstr_index;
cae1fbbb
L
9410 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9411 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9412 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9413 {
9414 eoinfo->failed = TRUE;
9415 return FALSE;
9416 }
8b127cbc 9417 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9418
8b127cbc 9419 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9420 {
9421 size_t hash_entry_size;
9422 bfd_byte *bucketpos;
9423 bfd_vma chain;
41198d0c
L
9424 size_t bucketcount;
9425 size_t bucket;
9426
8b127cbc 9427 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9428 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9429
9430 hash_entry_size
8b127cbc
AM
9431 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9432 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9433 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9434 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9435 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9436 bucketpos);
9437 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9438 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9439 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9440 }
c152c796 9441
8b127cbc 9442 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9443 {
9444 Elf_Internal_Versym iversym;
9445 Elf_External_Versym *eversym;
9446
f5385ebf 9447 if (!h->def_regular)
c152c796 9448 {
7b20f099
AM
9449 if (h->verinfo.verdef == NULL
9450 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9451 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9452 iversym.vs_vers = 0;
9453 else
9454 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9455 }
9456 else
9457 {
9458 if (h->verinfo.vertree == NULL)
9459 iversym.vs_vers = 1;
9460 else
9461 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9462 if (flinfo->info->create_default_symver)
3e3b46e5 9463 iversym.vs_vers++;
c152c796
AM
9464 }
9465
422f1182 9466 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9467 defined locally. */
422f1182 9468 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9469 iversym.vs_vers |= VERSYM_HIDDEN;
9470
8b127cbc 9471 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9472 eversym += h->dynindx;
8b127cbc 9473 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9474 }
9475 }
9476
d983c8c5
AM
9477 /* If the symbol is undefined, and we didn't output it to .dynsym,
9478 strip it from .symtab too. Obviously we can't do this for
9479 relocatable output or when needed for --emit-relocs. */
9480 else if (input_sec == bfd_und_section_ptr
9481 && h->indx != -2
0e1862bb 9482 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9483 return TRUE;
9484 /* Also strip others that we couldn't earlier due to dynamic symbol
9485 processing. */
9486 if (strip)
9487 return TRUE;
9488 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9489 return TRUE;
9490
2ec55de3
AM
9491 /* Output a FILE symbol so that following locals are not associated
9492 with the wrong input file. We need one for forced local symbols
9493 if we've seen more than one FILE symbol or when we have exactly
9494 one FILE symbol but global symbols are present in a file other
9495 than the one with the FILE symbol. We also need one if linker
9496 defined symbols are present. In practice these conditions are
9497 always met, so just emit the FILE symbol unconditionally. */
9498 if (eoinfo->localsyms
9499 && !eoinfo->file_sym_done
9500 && eoinfo->flinfo->filesym_count != 0)
9501 {
9502 Elf_Internal_Sym fsym;
9503
9504 memset (&fsym, 0, sizeof (fsym));
9505 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9506 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9507 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9508 bfd_und_section_ptr, NULL))
2ec55de3
AM
9509 return FALSE;
9510
9511 eoinfo->file_sym_done = TRUE;
9512 }
9513
8b127cbc 9514 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9515 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9516 input_sec, h);
6e0b88f1 9517 if (ret == 0)
c152c796
AM
9518 {
9519 eoinfo->failed = TRUE;
9520 return FALSE;
9521 }
6e0b88f1
AM
9522 else if (ret == 1)
9523 h->indx = indx;
9524 else if (h->indx == -2)
9525 abort();
c152c796
AM
9526
9527 return TRUE;
9528}
9529
cdd3575c
AM
9530/* Return TRUE if special handling is done for relocs in SEC against
9531 symbols defined in discarded sections. */
9532
c152c796
AM
9533static bfd_boolean
9534elf_section_ignore_discarded_relocs (asection *sec)
9535{
9536 const struct elf_backend_data *bed;
9537
cdd3575c
AM
9538 switch (sec->sec_info_type)
9539 {
dbaa2011
AM
9540 case SEC_INFO_TYPE_STABS:
9541 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9542 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9543 return TRUE;
9544 default:
9545 break;
9546 }
c152c796
AM
9547
9548 bed = get_elf_backend_data (sec->owner);
9549 if (bed->elf_backend_ignore_discarded_relocs != NULL
9550 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9551 return TRUE;
9552
9553 return FALSE;
9554}
9555
9e66c942
AM
9556/* Return a mask saying how ld should treat relocations in SEC against
9557 symbols defined in discarded sections. If this function returns
9558 COMPLAIN set, ld will issue a warning message. If this function
9559 returns PRETEND set, and the discarded section was link-once and the
9560 same size as the kept link-once section, ld will pretend that the
9561 symbol was actually defined in the kept section. Otherwise ld will
9562 zero the reloc (at least that is the intent, but some cooperation by
9563 the target dependent code is needed, particularly for REL targets). */
9564
8a696751
AM
9565unsigned int
9566_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9567{
9e66c942 9568 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9569 return PRETEND;
cdd3575c
AM
9570
9571 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9572 return 0;
cdd3575c
AM
9573
9574 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9575 return 0;
cdd3575c 9576
9e66c942 9577 return COMPLAIN | PRETEND;
cdd3575c
AM
9578}
9579
3d7f7666
L
9580/* Find a match between a section and a member of a section group. */
9581
9582static asection *
c0f00686
L
9583match_group_member (asection *sec, asection *group,
9584 struct bfd_link_info *info)
3d7f7666
L
9585{
9586 asection *first = elf_next_in_group (group);
9587 asection *s = first;
9588
9589 while (s != NULL)
9590 {
c0f00686 9591 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9592 return s;
9593
83180ade 9594 s = elf_next_in_group (s);
3d7f7666
L
9595 if (s == first)
9596 break;
9597 }
9598
9599 return NULL;
9600}
9601
01b3c8ab 9602/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9603 to replace it. Return the replacement if it is OK. Otherwise return
9604 NULL. */
01b3c8ab
L
9605
9606asection *
c0f00686 9607_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9608{
9609 asection *kept;
9610
9611 kept = sec->kept_section;
9612 if (kept != NULL)
9613 {
c2370991 9614 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9615 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9616 if (kept != NULL
9617 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9618 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9619 kept = NULL;
c2370991 9620 sec->kept_section = kept;
01b3c8ab
L
9621 }
9622 return kept;
9623}
9624
c152c796
AM
9625/* Link an input file into the linker output file. This function
9626 handles all the sections and relocations of the input file at once.
9627 This is so that we only have to read the local symbols once, and
9628 don't have to keep them in memory. */
9629
9630static bfd_boolean
8b127cbc 9631elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9632{
ece5ef60 9633 int (*relocate_section)
c152c796
AM
9634 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9635 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9636 bfd *output_bfd;
9637 Elf_Internal_Shdr *symtab_hdr;
9638 size_t locsymcount;
9639 size_t extsymoff;
9640 Elf_Internal_Sym *isymbuf;
9641 Elf_Internal_Sym *isym;
9642 Elf_Internal_Sym *isymend;
9643 long *pindex;
9644 asection **ppsection;
9645 asection *o;
9646 const struct elf_backend_data *bed;
c152c796 9647 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9648 bfd_size_type address_size;
9649 bfd_vma r_type_mask;
9650 int r_sym_shift;
ffbc01cc 9651 bfd_boolean have_file_sym = FALSE;
c152c796 9652
8b127cbc 9653 output_bfd = flinfo->output_bfd;
c152c796
AM
9654 bed = get_elf_backend_data (output_bfd);
9655 relocate_section = bed->elf_backend_relocate_section;
9656
9657 /* If this is a dynamic object, we don't want to do anything here:
9658 we don't want the local symbols, and we don't want the section
9659 contents. */
9660 if ((input_bfd->flags & DYNAMIC) != 0)
9661 return TRUE;
9662
c152c796
AM
9663 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9664 if (elf_bad_symtab (input_bfd))
9665 {
9666 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9667 extsymoff = 0;
9668 }
9669 else
9670 {
9671 locsymcount = symtab_hdr->sh_info;
9672 extsymoff = symtab_hdr->sh_info;
9673 }
9674
9675 /* Read the local symbols. */
9676 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9677 if (isymbuf == NULL && locsymcount != 0)
9678 {
9679 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9680 flinfo->internal_syms,
9681 flinfo->external_syms,
9682 flinfo->locsym_shndx);
c152c796
AM
9683 if (isymbuf == NULL)
9684 return FALSE;
9685 }
9686
9687 /* Find local symbol sections and adjust values of symbols in
9688 SEC_MERGE sections. Write out those local symbols we know are
9689 going into the output file. */
9690 isymend = isymbuf + locsymcount;
8b127cbc 9691 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9692 isym < isymend;
9693 isym++, pindex++, ppsection++)
9694 {
9695 asection *isec;
9696 const char *name;
9697 Elf_Internal_Sym osym;
6e0b88f1
AM
9698 long indx;
9699 int ret;
c152c796
AM
9700
9701 *pindex = -1;
9702
9703 if (elf_bad_symtab (input_bfd))
9704 {
9705 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9706 {
9707 *ppsection = NULL;
9708 continue;
9709 }
9710 }
9711
9712 if (isym->st_shndx == SHN_UNDEF)
9713 isec = bfd_und_section_ptr;
c152c796
AM
9714 else if (isym->st_shndx == SHN_ABS)
9715 isec = bfd_abs_section_ptr;
9716 else if (isym->st_shndx == SHN_COMMON)
9717 isec = bfd_com_section_ptr;
9718 else
9719 {
cb33740c
AM
9720 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9721 if (isec == NULL)
9722 {
9723 /* Don't attempt to output symbols with st_shnx in the
9724 reserved range other than SHN_ABS and SHN_COMMON. */
9725 *ppsection = NULL;
9726 continue;
9727 }
dbaa2011 9728 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9729 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9730 isym->st_value =
9731 _bfd_merged_section_offset (output_bfd, &isec,
9732 elf_section_data (isec)->sec_info,
9733 isym->st_value);
c152c796
AM
9734 }
9735
9736 *ppsection = isec;
9737
d983c8c5
AM
9738 /* Don't output the first, undefined, symbol. In fact, don't
9739 output any undefined local symbol. */
9740 if (isec == bfd_und_section_ptr)
c152c796
AM
9741 continue;
9742
9743 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9744 {
9745 /* We never output section symbols. Instead, we use the
9746 section symbol of the corresponding section in the output
9747 file. */
9748 continue;
9749 }
9750
9751 /* If we are stripping all symbols, we don't want to output this
9752 one. */
8b127cbc 9753 if (flinfo->info->strip == strip_all)
c152c796
AM
9754 continue;
9755
9756 /* If we are discarding all local symbols, we don't want to
9757 output this one. If we are generating a relocatable output
9758 file, then some of the local symbols may be required by
9759 relocs; we output them below as we discover that they are
9760 needed. */
8b127cbc 9761 if (flinfo->info->discard == discard_all)
c152c796
AM
9762 continue;
9763
9764 /* If this symbol is defined in a section which we are
f02571c5
AM
9765 discarding, we don't need to keep it. */
9766 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9767 && isym->st_shndx < SHN_LORESERVE
9768 && bfd_section_removed_from_list (output_bfd,
9769 isec->output_section))
e75a280b
L
9770 continue;
9771
c152c796
AM
9772 /* Get the name of the symbol. */
9773 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9774 isym->st_name);
9775 if (name == NULL)
9776 return FALSE;
9777
9778 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9779 if ((flinfo->info->strip == strip_some
9780 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9781 == NULL))
8b127cbc 9782 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9783 && (isec->flags & SEC_MERGE)
9784 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9785 || flinfo->info->discard == discard_l)
c152c796
AM
9786 && bfd_is_local_label_name (input_bfd, name)))
9787 continue;
9788
ffbc01cc
AM
9789 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9790 {
ce875075
AM
9791 if (input_bfd->lto_output)
9792 /* -flto puts a temp file name here. This means builds
9793 are not reproducible. Discard the symbol. */
9794 continue;
ffbc01cc
AM
9795 have_file_sym = TRUE;
9796 flinfo->filesym_count += 1;
9797 }
9798 if (!have_file_sym)
9799 {
9800 /* In the absence of debug info, bfd_find_nearest_line uses
9801 FILE symbols to determine the source file for local
9802 function symbols. Provide a FILE symbol here if input
9803 files lack such, so that their symbols won't be
9804 associated with a previous input file. It's not the
9805 source file, but the best we can do. */
9806 have_file_sym = TRUE;
9807 flinfo->filesym_count += 1;
9808 memset (&osym, 0, sizeof (osym));
9809 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9810 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9811 if (!elf_link_output_symstrtab (flinfo,
9812 (input_bfd->lto_output ? NULL
9813 : input_bfd->filename),
9814 &osym, bfd_abs_section_ptr,
9815 NULL))
ffbc01cc
AM
9816 return FALSE;
9817 }
9818
c152c796
AM
9819 osym = *isym;
9820
9821 /* Adjust the section index for the output file. */
9822 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9823 isec->output_section);
9824 if (osym.st_shndx == SHN_BAD)
9825 return FALSE;
9826
c152c796
AM
9827 /* ELF symbols in relocatable files are section relative, but
9828 in executable files they are virtual addresses. Note that
9829 this code assumes that all ELF sections have an associated
9830 BFD section with a reasonable value for output_offset; below
9831 we assume that they also have a reasonable value for
9832 output_section. Any special sections must be set up to meet
9833 these requirements. */
9834 osym.st_value += isec->output_offset;
0e1862bb 9835 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9836 {
9837 osym.st_value += isec->output_section->vma;
9838 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9839 {
9840 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9841 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9842 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9843 }
9844 }
9845
6e0b88f1 9846 indx = bfd_get_symcount (output_bfd);
ef10c3ac 9847 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 9848 if (ret == 0)
c152c796 9849 return FALSE;
6e0b88f1
AM
9850 else if (ret == 1)
9851 *pindex = indx;
c152c796
AM
9852 }
9853
310fd250
L
9854 if (bed->s->arch_size == 32)
9855 {
9856 r_type_mask = 0xff;
9857 r_sym_shift = 8;
9858 address_size = 4;
9859 }
9860 else
9861 {
9862 r_type_mask = 0xffffffff;
9863 r_sym_shift = 32;
9864 address_size = 8;
9865 }
9866
c152c796
AM
9867 /* Relocate the contents of each section. */
9868 sym_hashes = elf_sym_hashes (input_bfd);
9869 for (o = input_bfd->sections; o != NULL; o = o->next)
9870 {
9871 bfd_byte *contents;
9872
9873 if (! o->linker_mark)
9874 {
9875 /* This section was omitted from the link. */
9876 continue;
9877 }
9878
0e1862bb 9879 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
9880 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9881 {
9882 /* Deal with the group signature symbol. */
9883 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9884 unsigned long symndx = sec_data->this_hdr.sh_info;
9885 asection *osec = o->output_section;
9886
9887 if (symndx >= locsymcount
9888 || (elf_bad_symtab (input_bfd)
8b127cbc 9889 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9890 {
9891 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9892 while (h->root.type == bfd_link_hash_indirect
9893 || h->root.type == bfd_link_hash_warning)
9894 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9895 /* Arrange for symbol to be output. */
9896 h->indx = -2;
9897 elf_section_data (osec)->this_hdr.sh_info = -2;
9898 }
9899 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9900 {
9901 /* We'll use the output section target_index. */
8b127cbc 9902 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9903 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9904 }
9905 else
9906 {
8b127cbc 9907 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9908 {
9909 /* Otherwise output the local symbol now. */
9910 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9911 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 9912 const char *name;
6e0b88f1
AM
9913 long indx;
9914 int ret;
bcacc0f5
AM
9915
9916 name = bfd_elf_string_from_elf_section (input_bfd,
9917 symtab_hdr->sh_link,
9918 sym.st_name);
9919 if (name == NULL)
9920 return FALSE;
9921
9922 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9923 sec);
9924 if (sym.st_shndx == SHN_BAD)
9925 return FALSE;
9926
9927 sym.st_value += o->output_offset;
9928
6e0b88f1 9929 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
9930 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9931 NULL);
6e0b88f1 9932 if (ret == 0)
bcacc0f5 9933 return FALSE;
6e0b88f1 9934 else if (ret == 1)
8b127cbc 9935 flinfo->indices[symndx] = indx;
6e0b88f1
AM
9936 else
9937 abort ();
bcacc0f5
AM
9938 }
9939 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 9940 = flinfo->indices[symndx];
bcacc0f5
AM
9941 }
9942 }
9943
c152c796 9944 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9945 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9946 continue;
9947
9948 if ((o->flags & SEC_LINKER_CREATED) != 0)
9949 {
9950 /* Section was created by _bfd_elf_link_create_dynamic_sections
9951 or somesuch. */
9952 continue;
9953 }
9954
9955 /* Get the contents of the section. They have been cached by a
9956 relaxation routine. Note that o is a section in an input
9957 file, so the contents field will not have been set by any of
9958 the routines which work on output files. */
9959 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
9960 {
9961 contents = elf_section_data (o)->this_hdr.contents;
9962 if (bed->caches_rawsize
9963 && o->rawsize != 0
9964 && o->rawsize < o->size)
9965 {
9966 memcpy (flinfo->contents, contents, o->rawsize);
9967 contents = flinfo->contents;
9968 }
9969 }
c152c796
AM
9970 else
9971 {
8b127cbc 9972 contents = flinfo->contents;
4a114e3e 9973 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9974 return FALSE;
9975 }
9976
9977 if ((o->flags & SEC_RELOC) != 0)
9978 {
9979 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9980 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9981 int action_discarded;
ece5ef60 9982 int ret;
c152c796
AM
9983
9984 /* Get the swapped relocs. */
9985 internal_relocs
8b127cbc
AM
9986 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9987 flinfo->internal_relocs, FALSE);
c152c796
AM
9988 if (internal_relocs == NULL
9989 && o->reloc_count > 0)
9990 return FALSE;
9991
310fd250
L
9992 /* We need to reverse-copy input .ctors/.dtors sections if
9993 they are placed in .init_array/.finit_array for output. */
9994 if (o->size > address_size
9995 && ((strncmp (o->name, ".ctors", 6) == 0
9996 && strcmp (o->output_section->name,
9997 ".init_array") == 0)
9998 || (strncmp (o->name, ".dtors", 6) == 0
9999 && strcmp (o->output_section->name,
10000 ".fini_array") == 0))
10001 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10002 {
310fd250
L
10003 if (o->size != o->reloc_count * address_size)
10004 {
10005 (*_bfd_error_handler)
10006 (_("error: %B: size of section %A is not "
10007 "multiple of address size"),
10008 input_bfd, o);
10009 bfd_set_error (bfd_error_on_input);
10010 return FALSE;
10011 }
10012 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10013 }
10014
0f02bbd9 10015 action_discarded = -1;
c152c796 10016 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10017 action_discarded = (*bed->action_discarded) (o);
10018
10019 /* Run through the relocs evaluating complex reloc symbols and
10020 looking for relocs against symbols from discarded sections
10021 or section symbols from removed link-once sections.
10022 Complain about relocs against discarded sections. Zero
10023 relocs against removed link-once sections. */
10024
10025 rel = internal_relocs;
10026 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10027 for ( ; rel < relend; rel++)
c152c796 10028 {
0f02bbd9
AM
10029 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10030 unsigned int s_type;
10031 asection **ps, *sec;
10032 struct elf_link_hash_entry *h = NULL;
10033 const char *sym_name;
c152c796 10034
0f02bbd9
AM
10035 if (r_symndx == STN_UNDEF)
10036 continue;
c152c796 10037
0f02bbd9
AM
10038 if (r_symndx >= locsymcount
10039 || (elf_bad_symtab (input_bfd)
8b127cbc 10040 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10041 {
10042 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10043
0f02bbd9
AM
10044 /* Badly formatted input files can contain relocs that
10045 reference non-existant symbols. Check here so that
10046 we do not seg fault. */
10047 if (h == NULL)
c152c796 10048 {
0f02bbd9 10049 char buffer [32];
dce669a1 10050
0f02bbd9
AM
10051 sprintf_vma (buffer, rel->r_info);
10052 (*_bfd_error_handler)
10053 (_("error: %B contains a reloc (0x%s) for section %A "
10054 "that references a non-existent global symbol"),
10055 input_bfd, o, buffer);
10056 bfd_set_error (bfd_error_bad_value);
10057 return FALSE;
10058 }
3b36f7e6 10059
0f02bbd9
AM
10060 while (h->root.type == bfd_link_hash_indirect
10061 || h->root.type == bfd_link_hash_warning)
10062 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10063
0f02bbd9 10064 s_type = h->type;
cdd3575c 10065
9e2dec47 10066 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10067 mark the symbol as undefined. Note that the
10068 linker may attach linker created dynamic sections
10069 to the plugin bfd. Symbols defined in linker
10070 created sections are not plugin symbols. */
9e2dec47
L
10071 if (h->root.non_ir_ref
10072 && (h->root.type == bfd_link_hash_defined
10073 || h->root.type == bfd_link_hash_defweak)
10074 && (h->root.u.def.section->flags
10075 & SEC_LINKER_CREATED) == 0
10076 && h->root.u.def.section->owner != NULL
10077 && (h->root.u.def.section->owner->flags
10078 & BFD_PLUGIN) != 0)
10079 {
10080 h->root.type = bfd_link_hash_undefined;
10081 h->root.u.undef.abfd = h->root.u.def.section->owner;
10082 }
10083
0f02bbd9
AM
10084 ps = NULL;
10085 if (h->root.type == bfd_link_hash_defined
10086 || h->root.type == bfd_link_hash_defweak)
10087 ps = &h->root.u.def.section;
10088
10089 sym_name = h->root.root.string;
10090 }
10091 else
10092 {
10093 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10094
10095 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10096 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10097 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10098 sym, *ps);
10099 }
c152c796 10100
c301e700 10101 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10102 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10103 {
10104 bfd_vma val;
10105 bfd_vma dot = (rel->r_offset
10106 + o->output_offset + o->output_section->vma);
10107#ifdef DEBUG
10108 printf ("Encountered a complex symbol!");
10109 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10110 input_bfd->filename, o->name,
10111 (long) (rel - internal_relocs));
0f02bbd9
AM
10112 printf (" symbol: idx %8.8lx, name %s\n",
10113 r_symndx, sym_name);
10114 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10115 (unsigned long) rel->r_info,
10116 (unsigned long) rel->r_offset);
10117#endif
8b127cbc 10118 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10119 isymbuf, locsymcount, s_type == STT_SRELC))
10120 return FALSE;
10121
10122 /* Symbol evaluated OK. Update to absolute value. */
10123 set_symbol_value (input_bfd, isymbuf, locsymcount,
10124 r_symndx, val);
10125 continue;
10126 }
10127
10128 if (action_discarded != -1 && ps != NULL)
10129 {
cdd3575c
AM
10130 /* Complain if the definition comes from a
10131 discarded section. */
dbaa2011 10132 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10133 {
cf35638d 10134 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10135 if (action_discarded & COMPLAIN)
8b127cbc 10136 (*flinfo->info->callbacks->einfo)
e1fffbe6 10137 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10138 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10139 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10140
87e5235d 10141 /* Try to do the best we can to support buggy old
e0ae6d6f 10142 versions of gcc. Pretend that the symbol is
87e5235d
AM
10143 really defined in the kept linkonce section.
10144 FIXME: This is quite broken. Modifying the
10145 symbol here means we will be changing all later
e0ae6d6f 10146 uses of the symbol, not just in this section. */
0f02bbd9 10147 if (action_discarded & PRETEND)
87e5235d 10148 {
01b3c8ab
L
10149 asection *kept;
10150
c0f00686 10151 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10152 flinfo->info);
01b3c8ab 10153 if (kept != NULL)
87e5235d
AM
10154 {
10155 *ps = kept;
10156 continue;
10157 }
10158 }
c152c796
AM
10159 }
10160 }
10161 }
10162
10163 /* Relocate the section by invoking a back end routine.
10164
10165 The back end routine is responsible for adjusting the
10166 section contents as necessary, and (if using Rela relocs
10167 and generating a relocatable output file) adjusting the
10168 reloc addend as necessary.
10169
10170 The back end routine does not have to worry about setting
10171 the reloc address or the reloc symbol index.
10172
10173 The back end routine is given a pointer to the swapped in
10174 internal symbols, and can access the hash table entries
10175 for the external symbols via elf_sym_hashes (input_bfd).
10176
10177 When generating relocatable output, the back end routine
10178 must handle STB_LOCAL/STT_SECTION symbols specially. The
10179 output symbol is going to be a section symbol
10180 corresponding to the output section, which will require
10181 the addend to be adjusted. */
10182
8b127cbc 10183 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10184 input_bfd, o, contents,
10185 internal_relocs,
10186 isymbuf,
8b127cbc 10187 flinfo->sections);
ece5ef60 10188 if (!ret)
c152c796
AM
10189 return FALSE;
10190
ece5ef60 10191 if (ret == 2
0e1862bb 10192 || bfd_link_relocatable (flinfo->info)
8b127cbc 10193 || flinfo->info->emitrelocations)
c152c796
AM
10194 {
10195 Elf_Internal_Rela *irela;
d4730f92 10196 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10197 bfd_vma last_offset;
10198 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10199 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10200 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10201 unsigned int next_erel;
c152c796 10202 bfd_boolean rela_normal;
d4730f92 10203 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10204
d4730f92
BS
10205 esdi = elf_section_data (o);
10206 esdo = elf_section_data (o->output_section);
10207 rela_normal = FALSE;
c152c796
AM
10208
10209 /* Adjust the reloc addresses and symbol indices. */
10210
10211 irela = internal_relocs;
10212 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10213 rel_hash = esdo->rel.hashes + esdo->rel.count;
10214 /* We start processing the REL relocs, if any. When we reach
10215 IRELAMID in the loop, we switch to the RELA relocs. */
10216 irelamid = irela;
10217 if (esdi->rel.hdr != NULL)
10218 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10219 * bed->s->int_rels_per_ext_rel);
eac338cf 10220 rel_hash_list = rel_hash;
d4730f92 10221 rela_hash_list = NULL;
c152c796 10222 last_offset = o->output_offset;
0e1862bb 10223 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10224 last_offset += o->output_section->vma;
10225 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10226 {
10227 unsigned long r_symndx;
10228 asection *sec;
10229 Elf_Internal_Sym sym;
10230
10231 if (next_erel == bed->s->int_rels_per_ext_rel)
10232 {
10233 rel_hash++;
10234 next_erel = 0;
10235 }
10236
d4730f92
BS
10237 if (irela == irelamid)
10238 {
10239 rel_hash = esdo->rela.hashes + esdo->rela.count;
10240 rela_hash_list = rel_hash;
10241 rela_normal = bed->rela_normal;
10242 }
10243
c152c796 10244 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10245 flinfo->info, o,
c152c796
AM
10246 irela->r_offset);
10247 if (irela->r_offset >= (bfd_vma) -2)
10248 {
10249 /* This is a reloc for a deleted entry or somesuch.
10250 Turn it into an R_*_NONE reloc, at the same
10251 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10252 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10253 being ordered. */
10254 irela->r_offset = last_offset;
10255 irela->r_info = 0;
10256 irela->r_addend = 0;
10257 continue;
10258 }
10259
10260 irela->r_offset += o->output_offset;
10261
10262 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10263 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10264 irela->r_offset += o->output_section->vma;
10265
10266 last_offset = irela->r_offset;
10267
10268 r_symndx = irela->r_info >> r_sym_shift;
10269 if (r_symndx == STN_UNDEF)
10270 continue;
10271
10272 if (r_symndx >= locsymcount
10273 || (elf_bad_symtab (input_bfd)
8b127cbc 10274 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10275 {
10276 struct elf_link_hash_entry *rh;
10277 unsigned long indx;
10278
10279 /* This is a reloc against a global symbol. We
10280 have not yet output all the local symbols, so
10281 we do not know the symbol index of any global
10282 symbol. We set the rel_hash entry for this
10283 reloc to point to the global hash table entry
10284 for this symbol. The symbol index is then
ee75fd95 10285 set at the end of bfd_elf_final_link. */
c152c796
AM
10286 indx = r_symndx - extsymoff;
10287 rh = elf_sym_hashes (input_bfd)[indx];
10288 while (rh->root.type == bfd_link_hash_indirect
10289 || rh->root.type == bfd_link_hash_warning)
10290 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10291
10292 /* Setting the index to -2 tells
10293 elf_link_output_extsym that this symbol is
10294 used by a reloc. */
10295 BFD_ASSERT (rh->indx < 0);
10296 rh->indx = -2;
10297
10298 *rel_hash = rh;
10299
10300 continue;
10301 }
10302
10303 /* This is a reloc against a local symbol. */
10304
10305 *rel_hash = NULL;
10306 sym = isymbuf[r_symndx];
8b127cbc 10307 sec = flinfo->sections[r_symndx];
c152c796
AM
10308 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10309 {
10310 /* I suppose the backend ought to fill in the
10311 section of any STT_SECTION symbol against a
6a8d1586 10312 processor specific section. */
cf35638d 10313 r_symndx = STN_UNDEF;
6a8d1586
AM
10314 if (bfd_is_abs_section (sec))
10315 ;
c152c796
AM
10316 else if (sec == NULL || sec->owner == NULL)
10317 {
10318 bfd_set_error (bfd_error_bad_value);
10319 return FALSE;
10320 }
10321 else
10322 {
6a8d1586
AM
10323 asection *osec = sec->output_section;
10324
10325 /* If we have discarded a section, the output
10326 section will be the absolute section. In
ab96bf03
AM
10327 case of discarded SEC_MERGE sections, use
10328 the kept section. relocate_section should
10329 have already handled discarded linkonce
10330 sections. */
6a8d1586
AM
10331 if (bfd_is_abs_section (osec)
10332 && sec->kept_section != NULL
10333 && sec->kept_section->output_section != NULL)
10334 {
10335 osec = sec->kept_section->output_section;
10336 irela->r_addend -= osec->vma;
10337 }
10338
10339 if (!bfd_is_abs_section (osec))
10340 {
10341 r_symndx = osec->target_index;
cf35638d 10342 if (r_symndx == STN_UNDEF)
74541ad4 10343 {
051d833a
AM
10344 irela->r_addend += osec->vma;
10345 osec = _bfd_nearby_section (output_bfd, osec,
10346 osec->vma);
10347 irela->r_addend -= osec->vma;
10348 r_symndx = osec->target_index;
74541ad4 10349 }
6a8d1586 10350 }
c152c796
AM
10351 }
10352
10353 /* Adjust the addend according to where the
10354 section winds up in the output section. */
10355 if (rela_normal)
10356 irela->r_addend += sec->output_offset;
10357 }
10358 else
10359 {
8b127cbc 10360 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10361 {
10362 unsigned long shlink;
10363 const char *name;
10364 asection *osec;
6e0b88f1 10365 long indx;
c152c796 10366
8b127cbc 10367 if (flinfo->info->strip == strip_all)
c152c796
AM
10368 {
10369 /* You can't do ld -r -s. */
10370 bfd_set_error (bfd_error_invalid_operation);
10371 return FALSE;
10372 }
10373
10374 /* This symbol was skipped earlier, but
10375 since it is needed by a reloc, we
10376 must output it now. */
10377 shlink = symtab_hdr->sh_link;
10378 name = (bfd_elf_string_from_elf_section
10379 (input_bfd, shlink, sym.st_name));
10380 if (name == NULL)
10381 return FALSE;
10382
10383 osec = sec->output_section;
10384 sym.st_shndx =
10385 _bfd_elf_section_from_bfd_section (output_bfd,
10386 osec);
10387 if (sym.st_shndx == SHN_BAD)
10388 return FALSE;
10389
10390 sym.st_value += sec->output_offset;
0e1862bb 10391 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10392 {
10393 sym.st_value += osec->vma;
10394 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10395 {
10396 /* STT_TLS symbols are relative to PT_TLS
10397 segment base. */
8b127cbc 10398 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10399 ->tls_sec != NULL);
8b127cbc 10400 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10401 ->tls_sec->vma);
10402 }
10403 }
10404
6e0b88f1 10405 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10406 ret = elf_link_output_symstrtab (flinfo, name,
10407 &sym, sec,
10408 NULL);
6e0b88f1 10409 if (ret == 0)
c152c796 10410 return FALSE;
6e0b88f1 10411 else if (ret == 1)
8b127cbc 10412 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10413 else
10414 abort ();
c152c796
AM
10415 }
10416
8b127cbc 10417 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10418 }
10419
10420 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10421 | (irela->r_info & r_type_mask));
10422 }
10423
10424 /* Swap out the relocs. */
d4730f92
BS
10425 input_rel_hdr = esdi->rel.hdr;
10426 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10427 {
d4730f92
BS
10428 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10429 input_rel_hdr,
10430 internal_relocs,
10431 rel_hash_list))
10432 return FALSE;
c152c796
AM
10433 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10434 * bed->s->int_rels_per_ext_rel);
eac338cf 10435 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10436 }
10437
10438 input_rela_hdr = esdi->rela.hdr;
10439 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10440 {
eac338cf 10441 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10442 input_rela_hdr,
eac338cf 10443 internal_relocs,
d4730f92 10444 rela_hash_list))
c152c796
AM
10445 return FALSE;
10446 }
10447 }
10448 }
10449
10450 /* Write out the modified section contents. */
10451 if (bed->elf_backend_write_section
8b127cbc 10452 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10453 contents))
c152c796
AM
10454 {
10455 /* Section written out. */
10456 }
10457 else switch (o->sec_info_type)
10458 {
dbaa2011 10459 case SEC_INFO_TYPE_STABS:
c152c796
AM
10460 if (! (_bfd_write_section_stabs
10461 (output_bfd,
8b127cbc 10462 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10463 o, &elf_section_data (o)->sec_info, contents)))
10464 return FALSE;
10465 break;
dbaa2011 10466 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10467 if (! _bfd_write_merged_section (output_bfd, o,
10468 elf_section_data (o)->sec_info))
10469 return FALSE;
10470 break;
dbaa2011 10471 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10472 {
8b127cbc 10473 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10474 o, contents))
10475 return FALSE;
10476 }
10477 break;
2f0c68f2
CM
10478 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10479 {
10480 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10481 flinfo->info,
10482 o, contents))
10483 return FALSE;
10484 }
10485 break;
c152c796
AM
10486 default:
10487 {
5dabe785 10488 /* FIXME: octets_per_byte. */
310fd250
L
10489 if (! (o->flags & SEC_EXCLUDE))
10490 {
10491 file_ptr offset = (file_ptr) o->output_offset;
10492 bfd_size_type todo = o->size;
10493 if ((o->flags & SEC_ELF_REVERSE_COPY))
10494 {
10495 /* Reverse-copy input section to output. */
10496 do
10497 {
10498 todo -= address_size;
10499 if (! bfd_set_section_contents (output_bfd,
10500 o->output_section,
10501 contents + todo,
10502 offset,
10503 address_size))
10504 return FALSE;
10505 if (todo == 0)
10506 break;
10507 offset += address_size;
10508 }
10509 while (1);
10510 }
10511 else if (! bfd_set_section_contents (output_bfd,
10512 o->output_section,
10513 contents,
10514 offset, todo))
10515 return FALSE;
10516 }
c152c796
AM
10517 }
10518 break;
10519 }
10520 }
10521
10522 return TRUE;
10523}
10524
10525/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10526 requested by the linker, and does not come from any input file. This
c152c796
AM
10527 is used to build constructor and destructor tables when linking
10528 with -Ur. */
10529
10530static bfd_boolean
10531elf_reloc_link_order (bfd *output_bfd,
10532 struct bfd_link_info *info,
10533 asection *output_section,
10534 struct bfd_link_order *link_order)
10535{
10536 reloc_howto_type *howto;
10537 long indx;
10538 bfd_vma offset;
10539 bfd_vma addend;
d4730f92 10540 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10541 struct elf_link_hash_entry **rel_hash_ptr;
10542 Elf_Internal_Shdr *rel_hdr;
10543 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10544 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10545 bfd_byte *erel;
10546 unsigned int i;
d4730f92 10547 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10548
10549 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10550 if (howto == NULL)
10551 {
10552 bfd_set_error (bfd_error_bad_value);
10553 return FALSE;
10554 }
10555
10556 addend = link_order->u.reloc.p->addend;
10557
d4730f92
BS
10558 if (esdo->rel.hdr)
10559 reldata = &esdo->rel;
10560 else if (esdo->rela.hdr)
10561 reldata = &esdo->rela;
10562 else
10563 {
10564 reldata = NULL;
10565 BFD_ASSERT (0);
10566 }
10567
c152c796 10568 /* Figure out the symbol index. */
d4730f92 10569 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10570 if (link_order->type == bfd_section_reloc_link_order)
10571 {
10572 indx = link_order->u.reloc.p->u.section->target_index;
10573 BFD_ASSERT (indx != 0);
10574 *rel_hash_ptr = NULL;
10575 }
10576 else
10577 {
10578 struct elf_link_hash_entry *h;
10579
10580 /* Treat a reloc against a defined symbol as though it were
10581 actually against the section. */
10582 h = ((struct elf_link_hash_entry *)
10583 bfd_wrapped_link_hash_lookup (output_bfd, info,
10584 link_order->u.reloc.p->u.name,
10585 FALSE, FALSE, TRUE));
10586 if (h != NULL
10587 && (h->root.type == bfd_link_hash_defined
10588 || h->root.type == bfd_link_hash_defweak))
10589 {
10590 asection *section;
10591
10592 section = h->root.u.def.section;
10593 indx = section->output_section->target_index;
10594 *rel_hash_ptr = NULL;
10595 /* It seems that we ought to add the symbol value to the
10596 addend here, but in practice it has already been added
10597 because it was passed to constructor_callback. */
10598 addend += section->output_section->vma + section->output_offset;
10599 }
10600 else if (h != NULL)
10601 {
10602 /* Setting the index to -2 tells elf_link_output_extsym that
10603 this symbol is used by a reloc. */
10604 h->indx = -2;
10605 *rel_hash_ptr = h;
10606 indx = 0;
10607 }
10608 else
10609 {
10610 if (! ((*info->callbacks->unattached_reloc)
10611 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10612 return FALSE;
10613 indx = 0;
10614 }
10615 }
10616
10617 /* If this is an inplace reloc, we must write the addend into the
10618 object file. */
10619 if (howto->partial_inplace && addend != 0)
10620 {
10621 bfd_size_type size;
10622 bfd_reloc_status_type rstat;
10623 bfd_byte *buf;
10624 bfd_boolean ok;
10625 const char *sym_name;
10626
a50b1753
NC
10627 size = (bfd_size_type) bfd_get_reloc_size (howto);
10628 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10629 if (buf == NULL && size != 0)
c152c796
AM
10630 return FALSE;
10631 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10632 switch (rstat)
10633 {
10634 case bfd_reloc_ok:
10635 break;
10636
10637 default:
10638 case bfd_reloc_outofrange:
10639 abort ();
10640
10641 case bfd_reloc_overflow:
10642 if (link_order->type == bfd_section_reloc_link_order)
10643 sym_name = bfd_section_name (output_bfd,
10644 link_order->u.reloc.p->u.section);
10645 else
10646 sym_name = link_order->u.reloc.p->u.name;
10647 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10648 (info, NULL, sym_name, howto->name, addend, NULL,
10649 NULL, (bfd_vma) 0)))
c152c796
AM
10650 {
10651 free (buf);
10652 return FALSE;
10653 }
10654 break;
10655 }
10656 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10657 link_order->offset, size);
10658 free (buf);
10659 if (! ok)
10660 return FALSE;
10661 }
10662
10663 /* The address of a reloc is relative to the section in a
10664 relocatable file, and is a virtual address in an executable
10665 file. */
10666 offset = link_order->offset;
0e1862bb 10667 if (! bfd_link_relocatable (info))
c152c796
AM
10668 offset += output_section->vma;
10669
10670 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10671 {
10672 irel[i].r_offset = offset;
10673 irel[i].r_info = 0;
10674 irel[i].r_addend = 0;
10675 }
10676 if (bed->s->arch_size == 32)
10677 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10678 else
10679 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10680
d4730f92 10681 rel_hdr = reldata->hdr;
c152c796
AM
10682 erel = rel_hdr->contents;
10683 if (rel_hdr->sh_type == SHT_REL)
10684 {
d4730f92 10685 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10686 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10687 }
10688 else
10689 {
10690 irel[0].r_addend = addend;
d4730f92 10691 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10692 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10693 }
10694
d4730f92 10695 ++reldata->count;
c152c796
AM
10696
10697 return TRUE;
10698}
10699
0b52efa6
PB
10700
10701/* Get the output vma of the section pointed to by the sh_link field. */
10702
10703static bfd_vma
10704elf_get_linked_section_vma (struct bfd_link_order *p)
10705{
10706 Elf_Internal_Shdr **elf_shdrp;
10707 asection *s;
10708 int elfsec;
10709
10710 s = p->u.indirect.section;
10711 elf_shdrp = elf_elfsections (s->owner);
10712 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10713 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10714 /* PR 290:
10715 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10716 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10717 sh_info fields. Hence we could get the situation
10718 where elfsec is 0. */
10719 if (elfsec == 0)
10720 {
10721 const struct elf_backend_data *bed
10722 = get_elf_backend_data (s->owner);
10723 if (bed->link_order_error_handler)
d003868e
AM
10724 bed->link_order_error_handler
10725 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10726 return 0;
10727 }
10728 else
10729 {
10730 s = elf_shdrp[elfsec]->bfd_section;
10731 return s->output_section->vma + s->output_offset;
10732 }
0b52efa6
PB
10733}
10734
10735
10736/* Compare two sections based on the locations of the sections they are
10737 linked to. Used by elf_fixup_link_order. */
10738
10739static int
10740compare_link_order (const void * a, const void * b)
10741{
10742 bfd_vma apos;
10743 bfd_vma bpos;
10744
10745 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10746 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10747 if (apos < bpos)
10748 return -1;
10749 return apos > bpos;
10750}
10751
10752
10753/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10754 order as their linked sections. Returns false if this could not be done
10755 because an output section includes both ordered and unordered
10756 sections. Ideally we'd do this in the linker proper. */
10757
10758static bfd_boolean
10759elf_fixup_link_order (bfd *abfd, asection *o)
10760{
10761 int seen_linkorder;
10762 int seen_other;
10763 int n;
10764 struct bfd_link_order *p;
10765 bfd *sub;
10766 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10767 unsigned elfsec;
0b52efa6 10768 struct bfd_link_order **sections;
d33cdfe3 10769 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10770 bfd_vma offset;
3b36f7e6 10771
d33cdfe3
L
10772 other_sec = NULL;
10773 linkorder_sec = NULL;
0b52efa6
PB
10774 seen_other = 0;
10775 seen_linkorder = 0;
8423293d 10776 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10777 {
d33cdfe3 10778 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10779 {
10780 s = p->u.indirect.section;
d33cdfe3
L
10781 sub = s->owner;
10782 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10783 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10784 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10785 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10786 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10787 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10788 {
10789 seen_linkorder++;
10790 linkorder_sec = s;
10791 }
0b52efa6 10792 else
d33cdfe3
L
10793 {
10794 seen_other++;
10795 other_sec = s;
10796 }
0b52efa6
PB
10797 }
10798 else
10799 seen_other++;
d33cdfe3
L
10800
10801 if (seen_other && seen_linkorder)
10802 {
10803 if (other_sec && linkorder_sec)
10804 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10805 o, linkorder_sec,
10806 linkorder_sec->owner, other_sec,
10807 other_sec->owner);
10808 else
10809 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10810 o);
10811 bfd_set_error (bfd_error_bad_value);
10812 return FALSE;
10813 }
0b52efa6
PB
10814 }
10815
10816 if (!seen_linkorder)
10817 return TRUE;
10818
0b52efa6 10819 sections = (struct bfd_link_order **)
14b1c01e
AM
10820 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10821 if (sections == NULL)
10822 return FALSE;
0b52efa6 10823 seen_linkorder = 0;
3b36f7e6 10824
8423293d 10825 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10826 {
10827 sections[seen_linkorder++] = p;
10828 }
10829 /* Sort the input sections in the order of their linked section. */
10830 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10831 compare_link_order);
10832
10833 /* Change the offsets of the sections. */
10834 offset = 0;
10835 for (n = 0; n < seen_linkorder; n++)
10836 {
10837 s = sections[n]->u.indirect.section;
461686a3 10838 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10839 s->output_offset = offset;
10840 sections[n]->offset = offset;
5dabe785 10841 /* FIXME: octets_per_byte. */
0b52efa6
PB
10842 offset += sections[n]->size;
10843 }
10844
4dd07732 10845 free (sections);
0b52efa6
PB
10846 return TRUE;
10847}
10848
9f7c3e5e
AM
10849static void
10850elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10851{
10852 asection *o;
10853
10854 if (flinfo->symstrtab != NULL)
ef10c3ac 10855 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
10856 if (flinfo->contents != NULL)
10857 free (flinfo->contents);
10858 if (flinfo->external_relocs != NULL)
10859 free (flinfo->external_relocs);
10860 if (flinfo->internal_relocs != NULL)
10861 free (flinfo->internal_relocs);
10862 if (flinfo->external_syms != NULL)
10863 free (flinfo->external_syms);
10864 if (flinfo->locsym_shndx != NULL)
10865 free (flinfo->locsym_shndx);
10866 if (flinfo->internal_syms != NULL)
10867 free (flinfo->internal_syms);
10868 if (flinfo->indices != NULL)
10869 free (flinfo->indices);
10870 if (flinfo->sections != NULL)
10871 free (flinfo->sections);
9f7c3e5e
AM
10872 if (flinfo->symshndxbuf != NULL)
10873 free (flinfo->symshndxbuf);
10874 for (o = obfd->sections; o != NULL; o = o->next)
10875 {
10876 struct bfd_elf_section_data *esdo = elf_section_data (o);
10877 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10878 free (esdo->rel.hashes);
10879 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10880 free (esdo->rela.hashes);
10881 }
10882}
0b52efa6 10883
c152c796
AM
10884/* Do the final step of an ELF link. */
10885
10886bfd_boolean
10887bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10888{
10889 bfd_boolean dynamic;
10890 bfd_boolean emit_relocs;
10891 bfd *dynobj;
8b127cbc 10892 struct elf_final_link_info flinfo;
91d6fa6a
NC
10893 asection *o;
10894 struct bfd_link_order *p;
10895 bfd *sub;
c152c796
AM
10896 bfd_size_type max_contents_size;
10897 bfd_size_type max_external_reloc_size;
10898 bfd_size_type max_internal_reloc_count;
10899 bfd_size_type max_sym_count;
10900 bfd_size_type max_sym_shndx_count;
c152c796
AM
10901 Elf_Internal_Sym elfsym;
10902 unsigned int i;
10903 Elf_Internal_Shdr *symtab_hdr;
10904 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
10905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10906 struct elf_outext_info eoinfo;
10907 bfd_boolean merged;
10908 size_t relativecount = 0;
10909 asection *reldyn = 0;
10910 bfd_size_type amt;
104d59d1
JM
10911 asection *attr_section = NULL;
10912 bfd_vma attr_size = 0;
10913 const char *std_attrs_section;
c152c796
AM
10914
10915 if (! is_elf_hash_table (info->hash))
10916 return FALSE;
10917
0e1862bb 10918 if (bfd_link_pic (info))
c152c796
AM
10919 abfd->flags |= DYNAMIC;
10920
10921 dynamic = elf_hash_table (info)->dynamic_sections_created;
10922 dynobj = elf_hash_table (info)->dynobj;
10923
0e1862bb 10924 emit_relocs = (bfd_link_relocatable (info)
a4676736 10925 || info->emitrelocations);
c152c796 10926
8b127cbc
AM
10927 flinfo.info = info;
10928 flinfo.output_bfd = abfd;
ef10c3ac 10929 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 10930 if (flinfo.symstrtab == NULL)
c152c796
AM
10931 return FALSE;
10932
10933 if (! dynamic)
10934 {
8b127cbc
AM
10935 flinfo.hash_sec = NULL;
10936 flinfo.symver_sec = NULL;
c152c796
AM
10937 }
10938 else
10939 {
3d4d4302 10940 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 10941 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 10942 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
10943 /* Note that it is OK if symver_sec is NULL. */
10944 }
10945
8b127cbc
AM
10946 flinfo.contents = NULL;
10947 flinfo.external_relocs = NULL;
10948 flinfo.internal_relocs = NULL;
10949 flinfo.external_syms = NULL;
10950 flinfo.locsym_shndx = NULL;
10951 flinfo.internal_syms = NULL;
10952 flinfo.indices = NULL;
10953 flinfo.sections = NULL;
8b127cbc 10954 flinfo.symshndxbuf = NULL;
ffbc01cc 10955 flinfo.filesym_count = 0;
c152c796 10956
104d59d1
JM
10957 /* The object attributes have been merged. Remove the input
10958 sections from the link, and set the contents of the output
10959 secton. */
10960 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10961 for (o = abfd->sections; o != NULL; o = o->next)
10962 {
10963 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10964 || strcmp (o->name, ".gnu.attributes") == 0)
10965 {
10966 for (p = o->map_head.link_order; p != NULL; p = p->next)
10967 {
10968 asection *input_section;
10969
10970 if (p->type != bfd_indirect_link_order)
10971 continue;
10972 input_section = p->u.indirect.section;
10973 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10974 elf_link_input_bfd ignores this section. */
10975 input_section->flags &= ~SEC_HAS_CONTENTS;
10976 }
a0c8462f 10977
104d59d1
JM
10978 attr_size = bfd_elf_obj_attr_size (abfd);
10979 if (attr_size)
10980 {
10981 bfd_set_section_size (abfd, o, attr_size);
10982 attr_section = o;
10983 /* Skip this section later on. */
10984 o->map_head.link_order = NULL;
10985 }
10986 else
10987 o->flags |= SEC_EXCLUDE;
10988 }
10989 }
10990
c152c796
AM
10991 /* Count up the number of relocations we will output for each output
10992 section, so that we know the sizes of the reloc sections. We
10993 also figure out some maximum sizes. */
10994 max_contents_size = 0;
10995 max_external_reloc_size = 0;
10996 max_internal_reloc_count = 0;
10997 max_sym_count = 0;
10998 max_sym_shndx_count = 0;
10999 merged = FALSE;
11000 for (o = abfd->sections; o != NULL; o = o->next)
11001 {
11002 struct bfd_elf_section_data *esdo = elf_section_data (o);
11003 o->reloc_count = 0;
11004
8423293d 11005 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11006 {
11007 unsigned int reloc_count = 0;
491d01d3 11008 unsigned int additional_reloc_count = 0;
c152c796 11009 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11010
11011 if (p->type == bfd_section_reloc_link_order
11012 || p->type == bfd_symbol_reloc_link_order)
11013 reloc_count = 1;
11014 else if (p->type == bfd_indirect_link_order)
11015 {
11016 asection *sec;
11017
11018 sec = p->u.indirect.section;
11019 esdi = elf_section_data (sec);
11020
11021 /* Mark all sections which are to be included in the
11022 link. This will normally be every section. We need
11023 to do this so that we can identify any sections which
11024 the linker has decided to not include. */
11025 sec->linker_mark = TRUE;
11026
11027 if (sec->flags & SEC_MERGE)
11028 merged = TRUE;
11029
aed64b35
L
11030 if (esdo->this_hdr.sh_type == SHT_REL
11031 || esdo->this_hdr.sh_type == SHT_RELA)
11032 /* Some backends use reloc_count in relocation sections
11033 to count particular types of relocs. Of course,
11034 reloc sections themselves can't have relocations. */
11035 reloc_count = 0;
0e1862bb 11036 else if (emit_relocs)
491d01d3
YU
11037 {
11038 reloc_count = sec->reloc_count;
11039 if (bed->elf_backend_count_additional_relocs)
11040 {
11041 int c;
11042 c = (*bed->elf_backend_count_additional_relocs) (sec);
11043 additional_reloc_count += c;
11044 }
11045 }
c152c796 11046 else if (bed->elf_backend_count_relocs)
58217f29 11047 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11048
eea6121a
AM
11049 if (sec->rawsize > max_contents_size)
11050 max_contents_size = sec->rawsize;
11051 if (sec->size > max_contents_size)
11052 max_contents_size = sec->size;
c152c796
AM
11053
11054 /* We are interested in just local symbols, not all
11055 symbols. */
11056 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11057 && (sec->owner->flags & DYNAMIC) == 0)
11058 {
11059 size_t sym_count;
11060
11061 if (elf_bad_symtab (sec->owner))
11062 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11063 / bed->s->sizeof_sym);
11064 else
11065 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11066
11067 if (sym_count > max_sym_count)
11068 max_sym_count = sym_count;
11069
11070 if (sym_count > max_sym_shndx_count
6a40cf0c 11071 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11072 max_sym_shndx_count = sym_count;
11073
11074 if ((sec->flags & SEC_RELOC) != 0)
11075 {
d4730f92 11076 size_t ext_size = 0;
c152c796 11077
d4730f92
BS
11078 if (esdi->rel.hdr != NULL)
11079 ext_size = esdi->rel.hdr->sh_size;
11080 if (esdi->rela.hdr != NULL)
11081 ext_size += esdi->rela.hdr->sh_size;
7326c758 11082
c152c796
AM
11083 if (ext_size > max_external_reloc_size)
11084 max_external_reloc_size = ext_size;
11085 if (sec->reloc_count > max_internal_reloc_count)
11086 max_internal_reloc_count = sec->reloc_count;
11087 }
11088 }
11089 }
11090
11091 if (reloc_count == 0)
11092 continue;
11093
491d01d3 11094 reloc_count += additional_reloc_count;
c152c796
AM
11095 o->reloc_count += reloc_count;
11096
0e1862bb 11097 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11098 {
d4730f92 11099 if (esdi->rel.hdr)
491d01d3
YU
11100 {
11101 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11102 esdo->rel.count += additional_reloc_count;
11103 }
d4730f92 11104 if (esdi->rela.hdr)
491d01d3
YU
11105 {
11106 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11107 esdo->rela.count += additional_reloc_count;
11108 }
d4730f92
BS
11109 }
11110 else
11111 {
11112 if (o->use_rela_p)
11113 esdo->rela.count += reloc_count;
2c2b4ed4 11114 else
d4730f92 11115 esdo->rel.count += reloc_count;
c152c796 11116 }
c152c796
AM
11117 }
11118
11119 if (o->reloc_count > 0)
11120 o->flags |= SEC_RELOC;
11121 else
11122 {
11123 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11124 set it (this is probably a bug) and if it is set
11125 assign_section_numbers will create a reloc section. */
11126 o->flags &=~ SEC_RELOC;
11127 }
11128
11129 /* If the SEC_ALLOC flag is not set, force the section VMA to
11130 zero. This is done in elf_fake_sections as well, but forcing
11131 the VMA to 0 here will ensure that relocs against these
11132 sections are handled correctly. */
11133 if ((o->flags & SEC_ALLOC) == 0
11134 && ! o->user_set_vma)
11135 o->vma = 0;
11136 }
11137
0e1862bb 11138 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11139 elf_link_hash_traverse (elf_hash_table (info),
11140 _bfd_elf_link_sec_merge_syms, abfd);
11141
11142 /* Figure out the file positions for everything but the symbol table
11143 and the relocs. We set symcount to force assign_section_numbers
11144 to create a symbol table. */
8539e4e8 11145 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11146 BFD_ASSERT (! abfd->output_has_begun);
11147 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11148 goto error_return;
11149
ee75fd95 11150 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11151 for (o = abfd->sections; o != NULL; o = o->next)
11152 {
d4730f92 11153 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11154 if ((o->flags & SEC_RELOC) != 0)
11155 {
d4730f92
BS
11156 if (esdo->rel.hdr
11157 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11158 goto error_return;
11159
d4730f92
BS
11160 if (esdo->rela.hdr
11161 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11162 goto error_return;
11163 }
11164
11165 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11166 to count upwards while actually outputting the relocations. */
d4730f92
BS
11167 esdo->rel.count = 0;
11168 esdo->rela.count = 0;
0ce398f1
L
11169
11170 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11171 {
11172 /* Cache the section contents so that they can be compressed
11173 later. Use bfd_malloc since it will be freed by
11174 bfd_compress_section_contents. */
11175 unsigned char *contents = esdo->this_hdr.contents;
11176 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11177 abort ();
11178 contents
11179 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11180 if (contents == NULL)
11181 goto error_return;
11182 esdo->this_hdr.contents = contents;
11183 }
c152c796
AM
11184 }
11185
c152c796 11186 /* We have now assigned file positions for all the sections except
a485e98e
AM
11187 .symtab, .strtab, and non-loaded reloc sections. We start the
11188 .symtab section at the current file position, and write directly
11189 to it. We build the .strtab section in memory. */
c152c796
AM
11190 bfd_get_symcount (abfd) = 0;
11191 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11192 /* sh_name is set in prep_headers. */
11193 symtab_hdr->sh_type = SHT_SYMTAB;
11194 /* sh_flags, sh_addr and sh_size all start off zero. */
11195 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11196 /* sh_link is set in assign_section_numbers. */
11197 /* sh_info is set below. */
11198 /* sh_offset is set just below. */
72de5009 11199 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11200
ef10c3ac
L
11201 if (max_sym_count < 20)
11202 max_sym_count = 20;
11203 elf_hash_table (info)->strtabsize = max_sym_count;
11204 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11205 elf_hash_table (info)->strtab
11206 = (struct elf_sym_strtab *) bfd_malloc (amt);
11207 if (elf_hash_table (info)->strtab == NULL)
c152c796 11208 goto error_return;
ef10c3ac
L
11209 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11210 flinfo.symshndxbuf
11211 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11212 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11213
8539e4e8 11214 if (info->strip != strip_all || emit_relocs)
c152c796 11215 {
8539e4e8
AM
11216 file_ptr off = elf_next_file_pos (abfd);
11217
11218 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11219
11220 /* Note that at this point elf_next_file_pos (abfd) is
11221 incorrect. We do not yet know the size of the .symtab section.
11222 We correct next_file_pos below, after we do know the size. */
11223
11224 /* Start writing out the symbol table. The first symbol is always a
11225 dummy symbol. */
c152c796
AM
11226 elfsym.st_value = 0;
11227 elfsym.st_size = 0;
11228 elfsym.st_info = 0;
11229 elfsym.st_other = 0;
11230 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11231 elfsym.st_target_internal = 0;
ef10c3ac
L
11232 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11233 bfd_und_section_ptr, NULL) != 1)
c152c796 11234 goto error_return;
c152c796 11235
8539e4e8
AM
11236 /* Output a symbol for each section. We output these even if we are
11237 discarding local symbols, since they are used for relocs. These
11238 symbols have no names. We store the index of each one in the
11239 index field of the section, so that we can find it again when
11240 outputting relocs. */
11241
c152c796
AM
11242 elfsym.st_size = 0;
11243 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11244 elfsym.st_other = 0;
f0b5bb34 11245 elfsym.st_value = 0;
35fc36a8 11246 elfsym.st_target_internal = 0;
c152c796
AM
11247 for (i = 1; i < elf_numsections (abfd); i++)
11248 {
11249 o = bfd_section_from_elf_index (abfd, i);
11250 if (o != NULL)
f0b5bb34
AM
11251 {
11252 o->target_index = bfd_get_symcount (abfd);
11253 elfsym.st_shndx = i;
0e1862bb 11254 if (!bfd_link_relocatable (info))
f0b5bb34 11255 elfsym.st_value = o->vma;
ef10c3ac
L
11256 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11257 NULL) != 1)
f0b5bb34
AM
11258 goto error_return;
11259 }
c152c796
AM
11260 }
11261 }
11262
11263 /* Allocate some memory to hold information read in from the input
11264 files. */
11265 if (max_contents_size != 0)
11266 {
8b127cbc
AM
11267 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11268 if (flinfo.contents == NULL)
c152c796
AM
11269 goto error_return;
11270 }
11271
11272 if (max_external_reloc_size != 0)
11273 {
8b127cbc
AM
11274 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11275 if (flinfo.external_relocs == NULL)
c152c796
AM
11276 goto error_return;
11277 }
11278
11279 if (max_internal_reloc_count != 0)
11280 {
11281 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11282 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11283 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11284 if (flinfo.internal_relocs == NULL)
c152c796
AM
11285 goto error_return;
11286 }
11287
11288 if (max_sym_count != 0)
11289 {
11290 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11291 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11292 if (flinfo.external_syms == NULL)
c152c796
AM
11293 goto error_return;
11294
11295 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11296 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11297 if (flinfo.internal_syms == NULL)
c152c796
AM
11298 goto error_return;
11299
11300 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11301 flinfo.indices = (long int *) bfd_malloc (amt);
11302 if (flinfo.indices == NULL)
c152c796
AM
11303 goto error_return;
11304
11305 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11306 flinfo.sections = (asection **) bfd_malloc (amt);
11307 if (flinfo.sections == NULL)
c152c796
AM
11308 goto error_return;
11309 }
11310
11311 if (max_sym_shndx_count != 0)
11312 {
11313 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11314 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11315 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11316 goto error_return;
11317 }
11318
11319 if (elf_hash_table (info)->tls_sec)
11320 {
11321 bfd_vma base, end = 0;
11322 asection *sec;
11323
11324 for (sec = elf_hash_table (info)->tls_sec;
11325 sec && (sec->flags & SEC_THREAD_LOCAL);
11326 sec = sec->next)
11327 {
3a800eb9 11328 bfd_size_type size = sec->size;
c152c796 11329
3a800eb9
AM
11330 if (size == 0
11331 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11332 {
91d6fa6a
NC
11333 struct bfd_link_order *ord = sec->map_tail.link_order;
11334
11335 if (ord != NULL)
11336 size = ord->offset + ord->size;
c152c796
AM
11337 }
11338 end = sec->vma + size;
11339 }
11340 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11341 /* Only align end of TLS section if static TLS doesn't have special
11342 alignment requirements. */
11343 if (bed->static_tls_alignment == 1)
11344 end = align_power (end,
11345 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11346 elf_hash_table (info)->tls_size = end - base;
11347 }
11348
0b52efa6
PB
11349 /* Reorder SHF_LINK_ORDER sections. */
11350 for (o = abfd->sections; o != NULL; o = o->next)
11351 {
11352 if (!elf_fixup_link_order (abfd, o))
11353 return FALSE;
11354 }
11355
2f0c68f2
CM
11356 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11357 return FALSE;
11358
c152c796
AM
11359 /* Since ELF permits relocations to be against local symbols, we
11360 must have the local symbols available when we do the relocations.
11361 Since we would rather only read the local symbols once, and we
11362 would rather not keep them in memory, we handle all the
11363 relocations for a single input file at the same time.
11364
11365 Unfortunately, there is no way to know the total number of local
11366 symbols until we have seen all of them, and the local symbol
11367 indices precede the global symbol indices. This means that when
11368 we are generating relocatable output, and we see a reloc against
11369 a global symbol, we can not know the symbol index until we have
11370 finished examining all the local symbols to see which ones we are
11371 going to output. To deal with this, we keep the relocations in
11372 memory, and don't output them until the end of the link. This is
11373 an unfortunate waste of memory, but I don't see a good way around
11374 it. Fortunately, it only happens when performing a relocatable
11375 link, which is not the common case. FIXME: If keep_memory is set
11376 we could write the relocs out and then read them again; I don't
11377 know how bad the memory loss will be. */
11378
c72f2fb2 11379 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11380 sub->output_has_begun = FALSE;
11381 for (o = abfd->sections; o != NULL; o = o->next)
11382 {
8423293d 11383 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11384 {
11385 if (p->type == bfd_indirect_link_order
11386 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11387 == bfd_target_elf_flavour)
11388 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11389 {
11390 if (! sub->output_has_begun)
11391 {
8b127cbc 11392 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11393 goto error_return;
11394 sub->output_has_begun = TRUE;
11395 }
11396 }
11397 else if (p->type == bfd_section_reloc_link_order
11398 || p->type == bfd_symbol_reloc_link_order)
11399 {
11400 if (! elf_reloc_link_order (abfd, info, o, p))
11401 goto error_return;
11402 }
11403 else
11404 {
11405 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11406 {
11407 if (p->type == bfd_indirect_link_order
11408 && (bfd_get_flavour (sub)
11409 == bfd_target_elf_flavour)
11410 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11411 != bed->s->elfclass))
11412 {
11413 const char *iclass, *oclass;
11414
aebf9be7 11415 switch (bed->s->elfclass)
351f65ca 11416 {
aebf9be7
NC
11417 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11418 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11419 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11420 default: abort ();
351f65ca 11421 }
aebf9be7
NC
11422
11423 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11424 {
aebf9be7
NC
11425 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11426 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11427 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11428 default: abort ();
351f65ca
L
11429 }
11430
11431 bfd_set_error (bfd_error_wrong_format);
11432 (*_bfd_error_handler)
11433 (_("%B: file class %s incompatible with %s"),
11434 sub, iclass, oclass);
11435 }
11436
11437 goto error_return;
11438 }
c152c796
AM
11439 }
11440 }
11441 }
11442
c0f00686
L
11443 /* Free symbol buffer if needed. */
11444 if (!info->reduce_memory_overheads)
11445 {
c72f2fb2 11446 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11447 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11448 && elf_tdata (sub)->symbuf)
c0f00686
L
11449 {
11450 free (elf_tdata (sub)->symbuf);
11451 elf_tdata (sub)->symbuf = NULL;
11452 }
11453 }
11454
c152c796
AM
11455 /* Output any global symbols that got converted to local in a
11456 version script or due to symbol visibility. We do this in a
11457 separate step since ELF requires all local symbols to appear
11458 prior to any global symbols. FIXME: We should only do this if
11459 some global symbols were, in fact, converted to become local.
11460 FIXME: Will this work correctly with the Irix 5 linker? */
11461 eoinfo.failed = FALSE;
8b127cbc 11462 eoinfo.flinfo = &flinfo;
c152c796 11463 eoinfo.localsyms = TRUE;
34a79995 11464 eoinfo.file_sym_done = FALSE;
7686d77d 11465 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11466 if (eoinfo.failed)
11467 return FALSE;
11468
4e617b1e
PB
11469 /* If backend needs to output some local symbols not present in the hash
11470 table, do it now. */
8539e4e8
AM
11471 if (bed->elf_backend_output_arch_local_syms
11472 && (info->strip != strip_all || emit_relocs))
4e617b1e 11473 {
6e0b88f1 11474 typedef int (*out_sym_func)
4e617b1e
PB
11475 (void *, const char *, Elf_Internal_Sym *, asection *,
11476 struct elf_link_hash_entry *);
11477
11478 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11479 (abfd, info, &flinfo,
11480 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11481 return FALSE;
11482 }
11483
c152c796
AM
11484 /* That wrote out all the local symbols. Finish up the symbol table
11485 with the global symbols. Even if we want to strip everything we
11486 can, we still need to deal with those global symbols that got
11487 converted to local in a version script. */
11488
11489 /* The sh_info field records the index of the first non local symbol. */
11490 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11491
11492 if (dynamic
cae1fbbb
L
11493 && elf_hash_table (info)->dynsym != NULL
11494 && (elf_hash_table (info)->dynsym->output_section
11495 != bfd_abs_section_ptr))
c152c796
AM
11496 {
11497 Elf_Internal_Sym sym;
cae1fbbb 11498 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11499 long last_local = 0;
11500
11501 /* Write out the section symbols for the output sections. */
0e1862bb
L
11502 if (bfd_link_pic (info)
11503 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11504 {
11505 asection *s;
11506
11507 sym.st_size = 0;
11508 sym.st_name = 0;
11509 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11510 sym.st_other = 0;
35fc36a8 11511 sym.st_target_internal = 0;
c152c796
AM
11512
11513 for (s = abfd->sections; s != NULL; s = s->next)
11514 {
11515 int indx;
11516 bfd_byte *dest;
11517 long dynindx;
11518
c152c796 11519 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11520 if (dynindx <= 0)
11521 continue;
11522 indx = elf_section_data (s)->this_idx;
c152c796
AM
11523 BFD_ASSERT (indx > 0);
11524 sym.st_shndx = indx;
c0d5a53d
L
11525 if (! check_dynsym (abfd, &sym))
11526 return FALSE;
c152c796
AM
11527 sym.st_value = s->vma;
11528 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11529 if (last_local < dynindx)
11530 last_local = dynindx;
c152c796
AM
11531 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11532 }
c152c796
AM
11533 }
11534
11535 /* Write out the local dynsyms. */
11536 if (elf_hash_table (info)->dynlocal)
11537 {
11538 struct elf_link_local_dynamic_entry *e;
11539 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11540 {
11541 asection *s;
11542 bfd_byte *dest;
11543
935bd1e0 11544 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11545 Note that we saved a word of storage and overwrote
11546 the original st_name with the dynstr_index. */
11547 sym = e->isym;
935bd1e0 11548 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11549
cb33740c
AM
11550 s = bfd_section_from_elf_index (e->input_bfd,
11551 e->isym.st_shndx);
11552 if (s != NULL)
c152c796 11553 {
c152c796
AM
11554 sym.st_shndx =
11555 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11556 if (! check_dynsym (abfd, &sym))
11557 return FALSE;
c152c796
AM
11558 sym.st_value = (s->output_section->vma
11559 + s->output_offset
11560 + e->isym.st_value);
11561 }
11562
11563 if (last_local < e->dynindx)
11564 last_local = e->dynindx;
11565
11566 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11567 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11568 }
11569 }
11570
cae1fbbb 11571 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11572 last_local + 1;
11573 }
11574
11575 /* We get the global symbols from the hash table. */
11576 eoinfo.failed = FALSE;
11577 eoinfo.localsyms = FALSE;
8b127cbc 11578 eoinfo.flinfo = &flinfo;
7686d77d 11579 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11580 if (eoinfo.failed)
11581 return FALSE;
11582
11583 /* If backend needs to output some symbols not present in the hash
11584 table, do it now. */
8539e4e8
AM
11585 if (bed->elf_backend_output_arch_syms
11586 && (info->strip != strip_all || emit_relocs))
c152c796 11587 {
6e0b88f1 11588 typedef int (*out_sym_func)
c152c796
AM
11589 (void *, const char *, Elf_Internal_Sym *, asection *,
11590 struct elf_link_hash_entry *);
11591
11592 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11593 (abfd, info, &flinfo,
11594 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11595 return FALSE;
11596 }
11597
ef10c3ac
L
11598 /* Finalize the .strtab section. */
11599 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11600
11601 /* Swap out the .strtab section. */
11602 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11603 return FALSE;
11604
11605 /* Now we know the size of the symtab section. */
c152c796
AM
11606 if (bfd_get_symcount (abfd) > 0)
11607 {
ee3b52e9
L
11608 /* Finish up and write out the symbol string table (.strtab)
11609 section. */
11610 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11611 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11612
6a40cf0c
NC
11613 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11614 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11615 {
11616 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11617 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11618 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11619 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11620 symtab_shndx_hdr->sh_size = amt;
11621
11622 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11623 off, TRUE);
11624
11625 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11626 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11627 return FALSE;
11628 }
ee3b52e9
L
11629
11630 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11631 /* sh_name was set in prep_headers. */
11632 symstrtab_hdr->sh_type = SHT_STRTAB;
11633 symstrtab_hdr->sh_flags = 0;
11634 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11635 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11636 symstrtab_hdr->sh_entsize = 0;
11637 symstrtab_hdr->sh_link = 0;
11638 symstrtab_hdr->sh_info = 0;
11639 /* sh_offset is set just below. */
11640 symstrtab_hdr->sh_addralign = 1;
11641
11642 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11643 off, TRUE);
11644 elf_next_file_pos (abfd) = off;
11645
c152c796 11646 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11647 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11648 return FALSE;
11649 }
11650
11651 /* Adjust the relocs to have the correct symbol indices. */
11652 for (o = abfd->sections; o != NULL; o = o->next)
11653 {
d4730f92 11654 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11655 bfd_boolean sort;
c152c796
AM
11656 if ((o->flags & SEC_RELOC) == 0)
11657 continue;
11658
28dbcedc 11659 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11660 if (esdo->rel.hdr != NULL
11661 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11662 return FALSE;
11663 if (esdo->rela.hdr != NULL
11664 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11665 return FALSE;
c152c796
AM
11666
11667 /* Set the reloc_count field to 0 to prevent write_relocs from
11668 trying to swap the relocs out itself. */
11669 o->reloc_count = 0;
11670 }
11671
11672 if (dynamic && info->combreloc && dynobj != NULL)
11673 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11674
11675 /* If we are linking against a dynamic object, or generating a
11676 shared library, finish up the dynamic linking information. */
11677 if (dynamic)
11678 {
11679 bfd_byte *dyncon, *dynconend;
11680
11681 /* Fix up .dynamic entries. */
3d4d4302 11682 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11683 BFD_ASSERT (o != NULL);
11684
11685 dyncon = o->contents;
eea6121a 11686 dynconend = o->contents + o->size;
c152c796
AM
11687 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11688 {
11689 Elf_Internal_Dyn dyn;
11690 const char *name;
11691 unsigned int type;
11692
11693 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11694
11695 switch (dyn.d_tag)
11696 {
11697 default:
11698 continue;
11699 case DT_NULL:
11700 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11701 {
11702 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11703 {
11704 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11705 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11706 default: continue;
11707 }
11708 dyn.d_un.d_val = relativecount;
11709 relativecount = 0;
11710 break;
11711 }
11712 continue;
11713
11714 case DT_INIT:
11715 name = info->init_function;
11716 goto get_sym;
11717 case DT_FINI:
11718 name = info->fini_function;
11719 get_sym:
11720 {
11721 struct elf_link_hash_entry *h;
11722
11723 h = elf_link_hash_lookup (elf_hash_table (info), name,
11724 FALSE, FALSE, TRUE);
11725 if (h != NULL
11726 && (h->root.type == bfd_link_hash_defined
11727 || h->root.type == bfd_link_hash_defweak))
11728 {
bef26483 11729 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11730 o = h->root.u.def.section;
11731 if (o->output_section != NULL)
bef26483 11732 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11733 + o->output_offset);
11734 else
11735 {
11736 /* The symbol is imported from another shared
11737 library and does not apply to this one. */
bef26483 11738 dyn.d_un.d_ptr = 0;
c152c796
AM
11739 }
11740 break;
11741 }
11742 }
11743 continue;
11744
11745 case DT_PREINIT_ARRAYSZ:
11746 name = ".preinit_array";
11747 goto get_size;
11748 case DT_INIT_ARRAYSZ:
11749 name = ".init_array";
11750 goto get_size;
11751 case DT_FINI_ARRAYSZ:
11752 name = ".fini_array";
11753 get_size:
11754 o = bfd_get_section_by_name (abfd, name);
11755 if (o == NULL)
11756 {
11757 (*_bfd_error_handler)
d003868e 11758 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11759 goto error_return;
11760 }
eea6121a 11761 if (o->size == 0)
c152c796
AM
11762 (*_bfd_error_handler)
11763 (_("warning: %s section has zero size"), name);
eea6121a 11764 dyn.d_un.d_val = o->size;
c152c796
AM
11765 break;
11766
11767 case DT_PREINIT_ARRAY:
11768 name = ".preinit_array";
11769 goto get_vma;
11770 case DT_INIT_ARRAY:
11771 name = ".init_array";
11772 goto get_vma;
11773 case DT_FINI_ARRAY:
11774 name = ".fini_array";
11775 goto get_vma;
11776
11777 case DT_HASH:
11778 name = ".hash";
11779 goto get_vma;
fdc90cb4
JJ
11780 case DT_GNU_HASH:
11781 name = ".gnu.hash";
11782 goto get_vma;
c152c796
AM
11783 case DT_STRTAB:
11784 name = ".dynstr";
11785 goto get_vma;
11786 case DT_SYMTAB:
11787 name = ".dynsym";
11788 goto get_vma;
11789 case DT_VERDEF:
11790 name = ".gnu.version_d";
11791 goto get_vma;
11792 case DT_VERNEED:
11793 name = ".gnu.version_r";
11794 goto get_vma;
11795 case DT_VERSYM:
11796 name = ".gnu.version";
11797 get_vma:
11798 o = bfd_get_section_by_name (abfd, name);
11799 if (o == NULL)
11800 {
11801 (*_bfd_error_handler)
d003868e 11802 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11803 goto error_return;
11804 }
894891db
NC
11805 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11806 {
11807 (*_bfd_error_handler)
11808 (_("warning: section '%s' is being made into a note"), name);
11809 bfd_set_error (bfd_error_nonrepresentable_section);
11810 goto error_return;
11811 }
c152c796
AM
11812 dyn.d_un.d_ptr = o->vma;
11813 break;
11814
11815 case DT_REL:
11816 case DT_RELA:
11817 case DT_RELSZ:
11818 case DT_RELASZ:
11819 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11820 type = SHT_REL;
11821 else
11822 type = SHT_RELA;
11823 dyn.d_un.d_val = 0;
bef26483 11824 dyn.d_un.d_ptr = 0;
c152c796
AM
11825 for (i = 1; i < elf_numsections (abfd); i++)
11826 {
11827 Elf_Internal_Shdr *hdr;
11828
11829 hdr = elf_elfsections (abfd)[i];
11830 if (hdr->sh_type == type
11831 && (hdr->sh_flags & SHF_ALLOC) != 0)
11832 {
11833 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11834 dyn.d_un.d_val += hdr->sh_size;
11835 else
11836 {
bef26483
AM
11837 if (dyn.d_un.d_ptr == 0
11838 || hdr->sh_addr < dyn.d_un.d_ptr)
11839 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11840 }
11841 }
11842 }
11843 break;
11844 }
11845 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11846 }
11847 }
11848
11849 /* If we have created any dynamic sections, then output them. */
11850 if (dynobj != NULL)
11851 {
11852 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11853 goto error_return;
11854
943284cc 11855 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 11856 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 11857 || info->error_textrel)
3d4d4302 11858 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11859 {
11860 bfd_byte *dyncon, *dynconend;
11861
943284cc
DJ
11862 dyncon = o->contents;
11863 dynconend = o->contents + o->size;
11864 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11865 {
11866 Elf_Internal_Dyn dyn;
11867
11868 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11869
11870 if (dyn.d_tag == DT_TEXTREL)
11871 {
c192a133
AM
11872 if (info->error_textrel)
11873 info->callbacks->einfo
11874 (_("%P%X: read-only segment has dynamic relocations.\n"));
11875 else
11876 info->callbacks->einfo
11877 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11878 break;
11879 }
11880 }
11881 }
11882
c152c796
AM
11883 for (o = dynobj->sections; o != NULL; o = o->next)
11884 {
11885 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11886 || o->size == 0
c152c796
AM
11887 || o->output_section == bfd_abs_section_ptr)
11888 continue;
11889 if ((o->flags & SEC_LINKER_CREATED) == 0)
11890 {
11891 /* At this point, we are only interested in sections
11892 created by _bfd_elf_link_create_dynamic_sections. */
11893 continue;
11894 }
3722b82f
AM
11895 if (elf_hash_table (info)->stab_info.stabstr == o)
11896 continue;
eea6121a
AM
11897 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11898 continue;
3d4d4302 11899 if (strcmp (o->name, ".dynstr") != 0)
c152c796 11900 {
5dabe785 11901 /* FIXME: octets_per_byte. */
c152c796
AM
11902 if (! bfd_set_section_contents (abfd, o->output_section,
11903 o->contents,
11904 (file_ptr) o->output_offset,
eea6121a 11905 o->size))
c152c796
AM
11906 goto error_return;
11907 }
11908 else
11909 {
11910 /* The contents of the .dynstr section are actually in a
11911 stringtab. */
8539e4e8
AM
11912 file_ptr off;
11913
c152c796
AM
11914 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11915 if (bfd_seek (abfd, off, SEEK_SET) != 0
11916 || ! _bfd_elf_strtab_emit (abfd,
11917 elf_hash_table (info)->dynstr))
11918 goto error_return;
11919 }
11920 }
11921 }
11922
0e1862bb 11923 if (bfd_link_relocatable (info))
c152c796
AM
11924 {
11925 bfd_boolean failed = FALSE;
11926
11927 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11928 if (failed)
11929 goto error_return;
11930 }
11931
11932 /* If we have optimized stabs strings, output them. */
3722b82f 11933 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11934 {
11935 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11936 goto error_return;
11937 }
11938
9f7c3e5e
AM
11939 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11940 goto error_return;
c152c796 11941
9f7c3e5e 11942 elf_final_link_free (abfd, &flinfo);
c152c796 11943
12bd6957 11944 elf_linker (abfd) = TRUE;
c152c796 11945
104d59d1
JM
11946 if (attr_section)
11947 {
a50b1753 11948 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11949 if (contents == NULL)
d0f16d5e 11950 return FALSE; /* Bail out and fail. */
104d59d1
JM
11951 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11952 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11953 free (contents);
11954 }
11955
c152c796
AM
11956 return TRUE;
11957
11958 error_return:
9f7c3e5e 11959 elf_final_link_free (abfd, &flinfo);
c152c796
AM
11960 return FALSE;
11961}
11962\f
5241d853
RS
11963/* Initialize COOKIE for input bfd ABFD. */
11964
11965static bfd_boolean
11966init_reloc_cookie (struct elf_reloc_cookie *cookie,
11967 struct bfd_link_info *info, bfd *abfd)
11968{
11969 Elf_Internal_Shdr *symtab_hdr;
11970 const struct elf_backend_data *bed;
11971
11972 bed = get_elf_backend_data (abfd);
11973 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11974
11975 cookie->abfd = abfd;
11976 cookie->sym_hashes = elf_sym_hashes (abfd);
11977 cookie->bad_symtab = elf_bad_symtab (abfd);
11978 if (cookie->bad_symtab)
11979 {
11980 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11981 cookie->extsymoff = 0;
11982 }
11983 else
11984 {
11985 cookie->locsymcount = symtab_hdr->sh_info;
11986 cookie->extsymoff = symtab_hdr->sh_info;
11987 }
11988
11989 if (bed->s->arch_size == 32)
11990 cookie->r_sym_shift = 8;
11991 else
11992 cookie->r_sym_shift = 32;
11993
11994 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11995 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11996 {
11997 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11998 cookie->locsymcount, 0,
11999 NULL, NULL, NULL);
12000 if (cookie->locsyms == NULL)
12001 {
12002 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12003 return FALSE;
12004 }
12005 if (info->keep_memory)
12006 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12007 }
12008 return TRUE;
12009}
12010
12011/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12012
12013static void
12014fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12015{
12016 Elf_Internal_Shdr *symtab_hdr;
12017
12018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12019 if (cookie->locsyms != NULL
12020 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12021 free (cookie->locsyms);
12022}
12023
12024/* Initialize the relocation information in COOKIE for input section SEC
12025 of input bfd ABFD. */
12026
12027static bfd_boolean
12028init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12029 struct bfd_link_info *info, bfd *abfd,
12030 asection *sec)
12031{
12032 const struct elf_backend_data *bed;
12033
12034 if (sec->reloc_count == 0)
12035 {
12036 cookie->rels = NULL;
12037 cookie->relend = NULL;
12038 }
12039 else
12040 {
12041 bed = get_elf_backend_data (abfd);
12042
12043 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12044 info->keep_memory);
12045 if (cookie->rels == NULL)
12046 return FALSE;
12047 cookie->rel = cookie->rels;
12048 cookie->relend = (cookie->rels
12049 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12050 }
12051 cookie->rel = cookie->rels;
12052 return TRUE;
12053}
12054
12055/* Free the memory allocated by init_reloc_cookie_rels,
12056 if appropriate. */
12057
12058static void
12059fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12060 asection *sec)
12061{
12062 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12063 free (cookie->rels);
12064}
12065
12066/* Initialize the whole of COOKIE for input section SEC. */
12067
12068static bfd_boolean
12069init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12070 struct bfd_link_info *info,
12071 asection *sec)
12072{
12073 if (!init_reloc_cookie (cookie, info, sec->owner))
12074 goto error1;
12075 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12076 goto error2;
12077 return TRUE;
12078
12079 error2:
12080 fini_reloc_cookie (cookie, sec->owner);
12081 error1:
12082 return FALSE;
12083}
12084
12085/* Free the memory allocated by init_reloc_cookie_for_section,
12086 if appropriate. */
12087
12088static void
12089fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12090 asection *sec)
12091{
12092 fini_reloc_cookie_rels (cookie, sec);
12093 fini_reloc_cookie (cookie, sec->owner);
12094}
12095\f
c152c796
AM
12096/* Garbage collect unused sections. */
12097
07adf181
AM
12098/* Default gc_mark_hook. */
12099
12100asection *
12101_bfd_elf_gc_mark_hook (asection *sec,
12102 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12103 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12104 struct elf_link_hash_entry *h,
12105 Elf_Internal_Sym *sym)
12106{
12107 if (h != NULL)
12108 {
12109 switch (h->root.type)
12110 {
12111 case bfd_link_hash_defined:
12112 case bfd_link_hash_defweak:
12113 return h->root.u.def.section;
12114
12115 case bfd_link_hash_common:
12116 return h->root.u.c.p->section;
12117
12118 default:
12119 break;
12120 }
12121 }
12122 else
12123 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12124
12125 return NULL;
12126}
12127
5241d853
RS
12128/* COOKIE->rel describes a relocation against section SEC, which is
12129 a section we've decided to keep. Return the section that contains
12130 the relocation symbol, or NULL if no section contains it. */
12131
12132asection *
12133_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12134 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12135 struct elf_reloc_cookie *cookie,
12136 bfd_boolean *start_stop)
5241d853
RS
12137{
12138 unsigned long r_symndx;
12139 struct elf_link_hash_entry *h;
12140
12141 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12142 if (r_symndx == STN_UNDEF)
5241d853
RS
12143 return NULL;
12144
12145 if (r_symndx >= cookie->locsymcount
12146 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12147 {
12148 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12149 if (h == NULL)
12150 {
12151 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12152 sec->owner);
12153 return NULL;
12154 }
5241d853
RS
12155 while (h->root.type == bfd_link_hash_indirect
12156 || h->root.type == bfd_link_hash_warning)
12157 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12158 h->mark = 1;
4e6b54a6
AM
12159 /* If this symbol is weak and there is a non-weak definition, we
12160 keep the non-weak definition because many backends put
12161 dynamic reloc info on the non-weak definition for code
12162 handling copy relocs. */
12163 if (h->u.weakdef != NULL)
12164 h->u.weakdef->mark = 1;
1cce69b9
AM
12165
12166 if (start_stop != NULL
12167 && (h->root.type == bfd_link_hash_undefined
12168 || h->root.type == bfd_link_hash_undefweak))
12169 {
12170 /* To work around a glibc bug, mark all XXX input sections
12171 when there is an as yet undefined reference to __start_XXX
12172 or __stop_XXX symbols. The linker will later define such
12173 symbols for orphan input sections that have a name
12174 representable as a C identifier. */
12175 const char *sec_name = NULL;
12176 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12177 sec_name = h->root.root.string + 8;
12178 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12179 sec_name = h->root.root.string + 7;
12180
12181 if (sec_name != NULL && *sec_name != '\0')
12182 {
12183 bfd *i;
12184
12185 for (i = info->input_bfds; i != NULL; i = i->link.next)
12186 {
12187 asection *s = bfd_get_section_by_name (i, sec_name);
12188 if (s != NULL && !s->gc_mark)
12189 {
12190 *start_stop = TRUE;
12191 return s;
12192 }
12193 }
12194 }
12195 }
12196
5241d853
RS
12197 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12198 }
12199
12200 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12201 &cookie->locsyms[r_symndx]);
12202}
12203
12204/* COOKIE->rel describes a relocation against section SEC, which is
12205 a section we've decided to keep. Mark the section that contains
9d0a14d3 12206 the relocation symbol. */
5241d853
RS
12207
12208bfd_boolean
12209_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12210 asection *sec,
12211 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12212 struct elf_reloc_cookie *cookie)
5241d853
RS
12213{
12214 asection *rsec;
1cce69b9 12215 bfd_boolean start_stop = FALSE;
5241d853 12216
1cce69b9
AM
12217 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12218 while (rsec != NULL)
5241d853 12219 {
1cce69b9
AM
12220 if (!rsec->gc_mark)
12221 {
12222 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12223 || (rsec->owner->flags & DYNAMIC) != 0)
12224 rsec->gc_mark = 1;
12225 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12226 return FALSE;
12227 }
12228 if (!start_stop)
12229 break;
199af150 12230 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12231 }
12232 return TRUE;
12233}
12234
07adf181
AM
12235/* The mark phase of garbage collection. For a given section, mark
12236 it and any sections in this section's group, and all the sections
12237 which define symbols to which it refers. */
12238
ccfa59ea
AM
12239bfd_boolean
12240_bfd_elf_gc_mark (struct bfd_link_info *info,
12241 asection *sec,
6a5bb875 12242 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12243{
12244 bfd_boolean ret;
9d0a14d3 12245 asection *group_sec, *eh_frame;
c152c796
AM
12246
12247 sec->gc_mark = 1;
12248
12249 /* Mark all the sections in the group. */
12250 group_sec = elf_section_data (sec)->next_in_group;
12251 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12252 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12253 return FALSE;
12254
12255 /* Look through the section relocs. */
12256 ret = TRUE;
9d0a14d3
RS
12257 eh_frame = elf_eh_frame_section (sec->owner);
12258 if ((sec->flags & SEC_RELOC) != 0
12259 && sec->reloc_count > 0
12260 && sec != eh_frame)
c152c796 12261 {
5241d853 12262 struct elf_reloc_cookie cookie;
c152c796 12263
5241d853
RS
12264 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12265 ret = FALSE;
c152c796 12266 else
c152c796 12267 {
5241d853 12268 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12269 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12270 {
12271 ret = FALSE;
12272 break;
12273 }
12274 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12275 }
12276 }
9d0a14d3
RS
12277
12278 if (ret && eh_frame && elf_fde_list (sec))
12279 {
12280 struct elf_reloc_cookie cookie;
12281
12282 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12283 ret = FALSE;
12284 else
12285 {
12286 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12287 gc_mark_hook, &cookie))
12288 ret = FALSE;
12289 fini_reloc_cookie_for_section (&cookie, eh_frame);
12290 }
12291 }
12292
2f0c68f2
CM
12293 eh_frame = elf_section_eh_frame_entry (sec);
12294 if (ret && eh_frame && !eh_frame->gc_mark)
12295 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12296 ret = FALSE;
12297
c152c796
AM
12298 return ret;
12299}
12300
3c758495
TG
12301/* Scan and mark sections in a special or debug section group. */
12302
12303static void
12304_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12305{
12306 /* Point to first section of section group. */
12307 asection *ssec;
12308 /* Used to iterate the section group. */
12309 asection *msec;
12310
12311 bfd_boolean is_special_grp = TRUE;
12312 bfd_boolean is_debug_grp = TRUE;
12313
12314 /* First scan to see if group contains any section other than debug
12315 and special section. */
12316 ssec = msec = elf_next_in_group (grp);
12317 do
12318 {
12319 if ((msec->flags & SEC_DEBUGGING) == 0)
12320 is_debug_grp = FALSE;
12321
12322 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12323 is_special_grp = FALSE;
12324
12325 msec = elf_next_in_group (msec);
12326 }
12327 while (msec != ssec);
12328
12329 /* If this is a pure debug section group or pure special section group,
12330 keep all sections in this group. */
12331 if (is_debug_grp || is_special_grp)
12332 {
12333 do
12334 {
12335 msec->gc_mark = 1;
12336 msec = elf_next_in_group (msec);
12337 }
12338 while (msec != ssec);
12339 }
12340}
12341
7f6ab9f8
AM
12342/* Keep debug and special sections. */
12343
12344bfd_boolean
12345_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12346 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12347{
12348 bfd *ibfd;
12349
c72f2fb2 12350 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12351 {
12352 asection *isec;
12353 bfd_boolean some_kept;
b40bf0a2 12354 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12355
12356 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12357 continue;
12358
b40bf0a2
NC
12359 /* Ensure all linker created sections are kept,
12360 see if any other section is already marked,
12361 and note if we have any fragmented debug sections. */
12362 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12363 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12364 {
12365 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12366 isec->gc_mark = 1;
12367 else if (isec->gc_mark)
12368 some_kept = TRUE;
b40bf0a2
NC
12369
12370 if (debug_frag_seen == FALSE
12371 && (isec->flags & SEC_DEBUGGING)
12372 && CONST_STRNEQ (isec->name, ".debug_line."))
12373 debug_frag_seen = TRUE;
7f6ab9f8
AM
12374 }
12375
12376 /* If no section in this file will be kept, then we can
b40bf0a2 12377 toss out the debug and special sections. */
7f6ab9f8
AM
12378 if (!some_kept)
12379 continue;
12380
12381 /* Keep debug and special sections like .comment when they are
3c758495
TG
12382 not part of a group. Also keep section groups that contain
12383 just debug sections or special sections. */
7f6ab9f8 12384 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12385 {
12386 if ((isec->flags & SEC_GROUP) != 0)
12387 _bfd_elf_gc_mark_debug_special_section_group (isec);
12388 else if (((isec->flags & SEC_DEBUGGING) != 0
12389 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12390 && elf_next_in_group (isec) == NULL)
12391 isec->gc_mark = 1;
12392 }
b40bf0a2
NC
12393
12394 if (! debug_frag_seen)
12395 continue;
12396
12397 /* Look for CODE sections which are going to be discarded,
12398 and find and discard any fragmented debug sections which
12399 are associated with that code section. */
12400 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12401 if ((isec->flags & SEC_CODE) != 0
12402 && isec->gc_mark == 0)
12403 {
12404 unsigned int ilen;
12405 asection *dsec;
12406
12407 ilen = strlen (isec->name);
12408
12409 /* Association is determined by the name of the debug section
12410 containing the name of the code section as a suffix. For
12411 example .debug_line.text.foo is a debug section associated
12412 with .text.foo. */
12413 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12414 {
12415 unsigned int dlen;
12416
12417 if (dsec->gc_mark == 0
12418 || (dsec->flags & SEC_DEBUGGING) == 0)
12419 continue;
12420
12421 dlen = strlen (dsec->name);
12422
12423 if (dlen > ilen
12424 && strncmp (dsec->name + (dlen - ilen),
12425 isec->name, ilen) == 0)
12426 {
12427 dsec->gc_mark = 0;
b40bf0a2
NC
12428 }
12429 }
12430 }
7f6ab9f8
AM
12431 }
12432 return TRUE;
12433}
12434
c152c796
AM
12435/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12436
c17d87de
NC
12437struct elf_gc_sweep_symbol_info
12438{
ccabcbe5
AM
12439 struct bfd_link_info *info;
12440 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12441 bfd_boolean);
12442};
12443
c152c796 12444static bfd_boolean
ccabcbe5 12445elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12446{
1d5316ab
AM
12447 if (!h->mark
12448 && (((h->root.type == bfd_link_hash_defined
12449 || h->root.type == bfd_link_hash_defweak)
c4621b33 12450 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12451 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12452 || h->root.type == bfd_link_hash_undefined
12453 || h->root.type == bfd_link_hash_undefweak))
12454 {
12455 struct elf_gc_sweep_symbol_info *inf;
12456
12457 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12458 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12459 h->def_regular = 0;
12460 h->ref_regular = 0;
12461 h->ref_regular_nonweak = 0;
ccabcbe5 12462 }
c152c796
AM
12463
12464 return TRUE;
12465}
12466
12467/* The sweep phase of garbage collection. Remove all garbage sections. */
12468
12469typedef bfd_boolean (*gc_sweep_hook_fn)
12470 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12471
12472static bfd_boolean
ccabcbe5 12473elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12474{
12475 bfd *sub;
ccabcbe5
AM
12476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12477 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12478 unsigned long section_sym_count;
12479 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12480
c72f2fb2 12481 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12482 {
12483 asection *o;
12484
b19a8f85
L
12485 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12486 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12487 continue;
12488
12489 for (o = sub->sections; o != NULL; o = o->next)
12490 {
a33dafc3
L
12491 /* When any section in a section group is kept, we keep all
12492 sections in the section group. If the first member of
12493 the section group is excluded, we will also exclude the
12494 group section. */
12495 if (o->flags & SEC_GROUP)
12496 {
12497 asection *first = elf_next_in_group (o);
12498 o->gc_mark = first->gc_mark;
12499 }
c152c796 12500
1e7eae0d 12501 if (o->gc_mark)
c152c796
AM
12502 continue;
12503
12504 /* Skip sweeping sections already excluded. */
12505 if (o->flags & SEC_EXCLUDE)
12506 continue;
12507
12508 /* Since this is early in the link process, it is simple
12509 to remove a section from the output. */
12510 o->flags |= SEC_EXCLUDE;
12511
c55fe096 12512 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12513 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12514
c152c796
AM
12515 /* But we also have to update some of the relocation
12516 info we collected before. */
12517 if (gc_sweep_hook
e8aaee2a 12518 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12519 && o->reloc_count != 0
12520 && !((info->strip == strip_all || info->strip == strip_debugger)
12521 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12522 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12523 {
12524 Elf_Internal_Rela *internal_relocs;
12525 bfd_boolean r;
12526
12527 internal_relocs
12528 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12529 info->keep_memory);
12530 if (internal_relocs == NULL)
12531 return FALSE;
12532
12533 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12534
12535 if (elf_section_data (o)->relocs != internal_relocs)
12536 free (internal_relocs);
12537
12538 if (!r)
12539 return FALSE;
12540 }
12541 }
12542 }
12543
12544 /* Remove the symbols that were in the swept sections from the dynamic
12545 symbol table. GCFIXME: Anyone know how to get them out of the
12546 static symbol table as well? */
ccabcbe5
AM
12547 sweep_info.info = info;
12548 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12549 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12550 &sweep_info);
c152c796 12551
ccabcbe5 12552 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12553 return TRUE;
12554}
12555
12556/* Propagate collected vtable information. This is called through
12557 elf_link_hash_traverse. */
12558
12559static bfd_boolean
12560elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12561{
c152c796 12562 /* Those that are not vtables. */
f6e332e6 12563 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12564 return TRUE;
12565
12566 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12567 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12568 return TRUE;
12569
12570 /* If we've already been done, exit. */
f6e332e6 12571 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12572 return TRUE;
12573
12574 /* Make sure the parent's table is up to date. */
f6e332e6 12575 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12576
f6e332e6 12577 if (h->vtable->used == NULL)
c152c796
AM
12578 {
12579 /* None of this table's entries were referenced. Re-use the
12580 parent's table. */
f6e332e6
AM
12581 h->vtable->used = h->vtable->parent->vtable->used;
12582 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12583 }
12584 else
12585 {
12586 size_t n;
12587 bfd_boolean *cu, *pu;
12588
12589 /* Or the parent's entries into ours. */
f6e332e6 12590 cu = h->vtable->used;
c152c796 12591 cu[-1] = TRUE;
f6e332e6 12592 pu = h->vtable->parent->vtable->used;
c152c796
AM
12593 if (pu != NULL)
12594 {
12595 const struct elf_backend_data *bed;
12596 unsigned int log_file_align;
12597
12598 bed = get_elf_backend_data (h->root.u.def.section->owner);
12599 log_file_align = bed->s->log_file_align;
f6e332e6 12600 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12601 while (n--)
12602 {
12603 if (*pu)
12604 *cu = TRUE;
12605 pu++;
12606 cu++;
12607 }
12608 }
12609 }
12610
12611 return TRUE;
12612}
12613
12614static bfd_boolean
12615elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12616{
12617 asection *sec;
12618 bfd_vma hstart, hend;
12619 Elf_Internal_Rela *relstart, *relend, *rel;
12620 const struct elf_backend_data *bed;
12621 unsigned int log_file_align;
12622
c152c796
AM
12623 /* Take care of both those symbols that do not describe vtables as
12624 well as those that are not loaded. */
f6e332e6 12625 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12626 return TRUE;
12627
12628 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12629 || h->root.type == bfd_link_hash_defweak);
12630
12631 sec = h->root.u.def.section;
12632 hstart = h->root.u.def.value;
12633 hend = hstart + h->size;
12634
12635 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12636 if (!relstart)
12637 return *(bfd_boolean *) okp = FALSE;
12638 bed = get_elf_backend_data (sec->owner);
12639 log_file_align = bed->s->log_file_align;
12640
12641 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12642
12643 for (rel = relstart; rel < relend; ++rel)
12644 if (rel->r_offset >= hstart && rel->r_offset < hend)
12645 {
12646 /* If the entry is in use, do nothing. */
f6e332e6
AM
12647 if (h->vtable->used
12648 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12649 {
12650 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12651 if (h->vtable->used[entry])
c152c796
AM
12652 continue;
12653 }
12654 /* Otherwise, kill it. */
12655 rel->r_offset = rel->r_info = rel->r_addend = 0;
12656 }
12657
12658 return TRUE;
12659}
12660
87538722
AM
12661/* Mark sections containing dynamically referenced symbols. When
12662 building shared libraries, we must assume that any visible symbol is
12663 referenced. */
715df9b8 12664
64d03ab5
AM
12665bfd_boolean
12666bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12667{
87538722 12668 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12669 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12670
715df9b8
EB
12671 if ((h->root.type == bfd_link_hash_defined
12672 || h->root.type == bfd_link_hash_defweak)
87538722 12673 && (h->ref_dynamic
c4621b33 12674 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12675 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12676 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12677 && (!bfd_link_executable (info)
b407645f
AM
12678 || info->export_dynamic
12679 || (h->dynamic
12680 && d != NULL
12681 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12682 && (h->versioned >= versioned
54e8959c
L
12683 || !bfd_hide_sym_by_version (info->version_info,
12684 h->root.root.string)))))
715df9b8
EB
12685 h->root.u.def.section->flags |= SEC_KEEP;
12686
12687 return TRUE;
12688}
3b36f7e6 12689
74f0fb50
AM
12690/* Keep all sections containing symbols undefined on the command-line,
12691 and the section containing the entry symbol. */
12692
12693void
12694_bfd_elf_gc_keep (struct bfd_link_info *info)
12695{
12696 struct bfd_sym_chain *sym;
12697
12698 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12699 {
12700 struct elf_link_hash_entry *h;
12701
12702 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12703 FALSE, FALSE, FALSE);
12704
12705 if (h != NULL
12706 && (h->root.type == bfd_link_hash_defined
12707 || h->root.type == bfd_link_hash_defweak)
12708 && !bfd_is_abs_section (h->root.u.def.section))
12709 h->root.u.def.section->flags |= SEC_KEEP;
12710 }
12711}
12712
2f0c68f2
CM
12713bfd_boolean
12714bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12715 struct bfd_link_info *info)
12716{
12717 bfd *ibfd = info->input_bfds;
12718
12719 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12720 {
12721 asection *sec;
12722 struct elf_reloc_cookie cookie;
12723
12724 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12725 continue;
12726
12727 if (!init_reloc_cookie (&cookie, info, ibfd))
12728 return FALSE;
12729
12730 for (sec = ibfd->sections; sec; sec = sec->next)
12731 {
12732 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12733 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12734 {
12735 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12736 fini_reloc_cookie_rels (&cookie, sec);
12737 }
12738 }
12739 }
12740 return TRUE;
12741}
12742
c152c796
AM
12743/* Do mark and sweep of unused sections. */
12744
12745bfd_boolean
12746bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12747{
12748 bfd_boolean ok = TRUE;
12749 bfd *sub;
6a5bb875 12750 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12751 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12752 struct elf_link_hash_table *htab;
c152c796 12753
64d03ab5 12754 if (!bed->can_gc_sections
715df9b8 12755 || !is_elf_hash_table (info->hash))
c152c796
AM
12756 {
12757 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12758 return TRUE;
12759 }
12760
74f0fb50 12761 bed->gc_keep (info);
da44f4e5 12762 htab = elf_hash_table (info);
74f0fb50 12763
9d0a14d3
RS
12764 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12765 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12766 for (sub = info->input_bfds;
12767 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12768 sub = sub->link.next)
9d0a14d3
RS
12769 {
12770 asection *sec;
12771 struct elf_reloc_cookie cookie;
12772
12773 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12774 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12775 {
12776 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12777 if (elf_section_data (sec)->sec_info
12778 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12779 elf_eh_frame_section (sub) = sec;
12780 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12781 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12782 }
12783 }
9d0a14d3 12784
c152c796 12785 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12786 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12787 if (!ok)
12788 return FALSE;
12789
12790 /* Kill the vtable relocations that were not used. */
da44f4e5 12791 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12792 if (!ok)
12793 return FALSE;
12794
715df9b8 12795 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12796 if (htab->dynamic_sections_created)
12797 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 12798
715df9b8 12799 /* Grovel through relocs to find out who stays ... */
64d03ab5 12800 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 12801 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12802 {
12803 asection *o;
12804
b19a8f85
L
12805 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12806 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12807 continue;
12808
7f6ab9f8
AM
12809 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12810 Also treat note sections as a root, if the section is not part
12811 of a group. */
c152c796 12812 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12813 if (!o->gc_mark
12814 && (o->flags & SEC_EXCLUDE) == 0
24007750 12815 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12816 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12817 && elf_next_in_group (o) == NULL )))
12818 {
12819 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12820 return FALSE;
12821 }
c152c796
AM
12822 }
12823
6a5bb875 12824 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12825 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12826
c152c796 12827 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12828 return elf_gc_sweep (abfd, info);
c152c796
AM
12829}
12830\f
12831/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12832
12833bfd_boolean
12834bfd_elf_gc_record_vtinherit (bfd *abfd,
12835 asection *sec,
12836 struct elf_link_hash_entry *h,
12837 bfd_vma offset)
12838{
12839 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12840 struct elf_link_hash_entry **search, *child;
12841 bfd_size_type extsymcount;
12842 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12843
12844 /* The sh_info field of the symtab header tells us where the
12845 external symbols start. We don't care about the local symbols at
12846 this point. */
12847 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12848 if (!elf_bad_symtab (abfd))
12849 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12850
12851 sym_hashes = elf_sym_hashes (abfd);
12852 sym_hashes_end = sym_hashes + extsymcount;
12853
12854 /* Hunt down the child symbol, which is in this section at the same
12855 offset as the relocation. */
12856 for (search = sym_hashes; search != sym_hashes_end; ++search)
12857 {
12858 if ((child = *search) != NULL
12859 && (child->root.type == bfd_link_hash_defined
12860 || child->root.type == bfd_link_hash_defweak)
12861 && child->root.u.def.section == sec
12862 && child->root.u.def.value == offset)
12863 goto win;
12864 }
12865
d003868e
AM
12866 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12867 abfd, sec, (unsigned long) offset);
c152c796
AM
12868 bfd_set_error (bfd_error_invalid_operation);
12869 return FALSE;
12870
12871 win:
f6e332e6
AM
12872 if (!child->vtable)
12873 {
ca4be51c
AM
12874 child->vtable = ((struct elf_link_virtual_table_entry *)
12875 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
12876 if (!child->vtable)
12877 return FALSE;
12878 }
c152c796
AM
12879 if (!h)
12880 {
12881 /* This *should* only be the absolute section. It could potentially
12882 be that someone has defined a non-global vtable though, which
12883 would be bad. It isn't worth paging in the local symbols to be
12884 sure though; that case should simply be handled by the assembler. */
12885
f6e332e6 12886 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12887 }
12888 else
f6e332e6 12889 child->vtable->parent = h;
c152c796
AM
12890
12891 return TRUE;
12892}
12893
12894/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12895
12896bfd_boolean
12897bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12898 asection *sec ATTRIBUTE_UNUSED,
12899 struct elf_link_hash_entry *h,
12900 bfd_vma addend)
12901{
12902 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12903 unsigned int log_file_align = bed->s->log_file_align;
12904
f6e332e6
AM
12905 if (!h->vtable)
12906 {
ca4be51c
AM
12907 h->vtable = ((struct elf_link_virtual_table_entry *)
12908 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
12909 if (!h->vtable)
12910 return FALSE;
12911 }
12912
12913 if (addend >= h->vtable->size)
c152c796
AM
12914 {
12915 size_t size, bytes, file_align;
f6e332e6 12916 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12917
12918 /* While the symbol is undefined, we have to be prepared to handle
12919 a zero size. */
12920 file_align = 1 << log_file_align;
12921 if (h->root.type == bfd_link_hash_undefined)
12922 size = addend + file_align;
12923 else
12924 {
12925 size = h->size;
12926 if (addend >= size)
12927 {
12928 /* Oops! We've got a reference past the defined end of
12929 the table. This is probably a bug -- shall we warn? */
12930 size = addend + file_align;
12931 }
12932 }
12933 size = (size + file_align - 1) & -file_align;
12934
12935 /* Allocate one extra entry for use as a "done" flag for the
12936 consolidation pass. */
12937 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12938
12939 if (ptr)
12940 {
a50b1753 12941 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12942
12943 if (ptr != NULL)
12944 {
12945 size_t oldbytes;
12946
f6e332e6 12947 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12948 * sizeof (bfd_boolean));
12949 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12950 }
12951 }
12952 else
a50b1753 12953 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12954
12955 if (ptr == NULL)
12956 return FALSE;
12957
12958 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12959 h->vtable->used = ptr + 1;
12960 h->vtable->size = size;
c152c796
AM
12961 }
12962
f6e332e6 12963 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12964
12965 return TRUE;
12966}
12967
ae17ab41
CM
12968/* Map an ELF section header flag to its corresponding string. */
12969typedef struct
12970{
12971 char *flag_name;
12972 flagword flag_value;
12973} elf_flags_to_name_table;
12974
12975static elf_flags_to_name_table elf_flags_to_names [] =
12976{
12977 { "SHF_WRITE", SHF_WRITE },
12978 { "SHF_ALLOC", SHF_ALLOC },
12979 { "SHF_EXECINSTR", SHF_EXECINSTR },
12980 { "SHF_MERGE", SHF_MERGE },
12981 { "SHF_STRINGS", SHF_STRINGS },
12982 { "SHF_INFO_LINK", SHF_INFO_LINK},
12983 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12984 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12985 { "SHF_GROUP", SHF_GROUP },
12986 { "SHF_TLS", SHF_TLS },
12987 { "SHF_MASKOS", SHF_MASKOS },
12988 { "SHF_EXCLUDE", SHF_EXCLUDE },
12989};
12990
b9c361e0
JL
12991/* Returns TRUE if the section is to be included, otherwise FALSE. */
12992bfd_boolean
ae17ab41 12993bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 12994 struct flag_info *flaginfo,
b9c361e0 12995 asection *section)
ae17ab41 12996{
8b127cbc 12997 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 12998
8b127cbc 12999 if (!flaginfo->flags_initialized)
ae17ab41 13000 {
8b127cbc
AM
13001 bfd *obfd = info->output_bfd;
13002 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13003 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13004 int with_hex = 0;
13005 int without_hex = 0;
13006
8b127cbc 13007 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13008 {
b9c361e0 13009 unsigned i;
8b127cbc 13010 flagword (*lookup) (char *);
ae17ab41 13011
8b127cbc
AM
13012 lookup = bed->elf_backend_lookup_section_flags_hook;
13013 if (lookup != NULL)
ae17ab41 13014 {
8b127cbc 13015 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13016
13017 if (hexval != 0)
13018 {
13019 if (tf->with == with_flags)
13020 with_hex |= hexval;
13021 else if (tf->with == without_flags)
13022 without_hex |= hexval;
13023 tf->valid = TRUE;
13024 continue;
13025 }
ae17ab41 13026 }
8b127cbc 13027 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13028 {
8b127cbc 13029 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13030 {
13031 if (tf->with == with_flags)
13032 with_hex |= elf_flags_to_names[i].flag_value;
13033 else if (tf->with == without_flags)
13034 without_hex |= elf_flags_to_names[i].flag_value;
13035 tf->valid = TRUE;
13036 break;
13037 }
13038 }
8b127cbc 13039 if (!tf->valid)
b9c361e0 13040 {
68ffbac6 13041 info->callbacks->einfo
8b127cbc 13042 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13043 return FALSE;
ae17ab41
CM
13044 }
13045 }
8b127cbc
AM
13046 flaginfo->flags_initialized = TRUE;
13047 flaginfo->only_with_flags |= with_hex;
13048 flaginfo->not_with_flags |= without_hex;
ae17ab41 13049 }
ae17ab41 13050
8b127cbc 13051 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13052 return FALSE;
13053
8b127cbc 13054 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13055 return FALSE;
13056
13057 return TRUE;
ae17ab41
CM
13058}
13059
c152c796
AM
13060struct alloc_got_off_arg {
13061 bfd_vma gotoff;
10455f89 13062 struct bfd_link_info *info;
c152c796
AM
13063};
13064
13065/* We need a special top-level link routine to convert got reference counts
13066 to real got offsets. */
13067
13068static bfd_boolean
13069elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13070{
a50b1753 13071 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13072 bfd *obfd = gofarg->info->output_bfd;
13073 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13074
c152c796
AM
13075 if (h->got.refcount > 0)
13076 {
13077 h->got.offset = gofarg->gotoff;
10455f89 13078 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13079 }
13080 else
13081 h->got.offset = (bfd_vma) -1;
13082
13083 return TRUE;
13084}
13085
13086/* And an accompanying bit to work out final got entry offsets once
13087 we're done. Should be called from final_link. */
13088
13089bfd_boolean
13090bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13091 struct bfd_link_info *info)
13092{
13093 bfd *i;
13094 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13095 bfd_vma gotoff;
c152c796
AM
13096 struct alloc_got_off_arg gofarg;
13097
10455f89
HPN
13098 BFD_ASSERT (abfd == info->output_bfd);
13099
c152c796
AM
13100 if (! is_elf_hash_table (info->hash))
13101 return FALSE;
13102
13103 /* The GOT offset is relative to the .got section, but the GOT header is
13104 put into the .got.plt section, if the backend uses it. */
13105 if (bed->want_got_plt)
13106 gotoff = 0;
13107 else
13108 gotoff = bed->got_header_size;
13109
13110 /* Do the local .got entries first. */
c72f2fb2 13111 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13112 {
13113 bfd_signed_vma *local_got;
13114 bfd_size_type j, locsymcount;
13115 Elf_Internal_Shdr *symtab_hdr;
13116
13117 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13118 continue;
13119
13120 local_got = elf_local_got_refcounts (i);
13121 if (!local_got)
13122 continue;
13123
13124 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13125 if (elf_bad_symtab (i))
13126 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13127 else
13128 locsymcount = symtab_hdr->sh_info;
13129
13130 for (j = 0; j < locsymcount; ++j)
13131 {
13132 if (local_got[j] > 0)
13133 {
13134 local_got[j] = gotoff;
10455f89 13135 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13136 }
13137 else
13138 local_got[j] = (bfd_vma) -1;
13139 }
13140 }
13141
13142 /* Then the global .got entries. .plt refcounts are handled by
13143 adjust_dynamic_symbol */
13144 gofarg.gotoff = gotoff;
10455f89 13145 gofarg.info = info;
c152c796
AM
13146 elf_link_hash_traverse (elf_hash_table (info),
13147 elf_gc_allocate_got_offsets,
13148 &gofarg);
13149 return TRUE;
13150}
13151
13152/* Many folk need no more in the way of final link than this, once
13153 got entry reference counting is enabled. */
13154
13155bfd_boolean
13156bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13157{
13158 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13159 return FALSE;
13160
13161 /* Invoke the regular ELF backend linker to do all the work. */
13162 return bfd_elf_final_link (abfd, info);
13163}
13164
13165bfd_boolean
13166bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13167{
a50b1753 13168 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13169
13170 if (rcookie->bad_symtab)
13171 rcookie->rel = rcookie->rels;
13172
13173 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13174 {
13175 unsigned long r_symndx;
13176
13177 if (! rcookie->bad_symtab)
13178 if (rcookie->rel->r_offset > offset)
13179 return FALSE;
13180 if (rcookie->rel->r_offset != offset)
13181 continue;
13182
13183 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13184 if (r_symndx == STN_UNDEF)
c152c796
AM
13185 return TRUE;
13186
13187 if (r_symndx >= rcookie->locsymcount
13188 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13189 {
13190 struct elf_link_hash_entry *h;
13191
13192 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13193
13194 while (h->root.type == bfd_link_hash_indirect
13195 || h->root.type == bfd_link_hash_warning)
13196 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13197
13198 if ((h->root.type == bfd_link_hash_defined
13199 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13200 && (h->root.u.def.section->owner != rcookie->abfd
13201 || h->root.u.def.section->kept_section != NULL
13202 || discarded_section (h->root.u.def.section)))
c152c796 13203 return TRUE;
c152c796
AM
13204 }
13205 else
13206 {
13207 /* It's not a relocation against a global symbol,
13208 but it could be a relocation against a local
13209 symbol for a discarded section. */
13210 asection *isec;
13211 Elf_Internal_Sym *isym;
13212
13213 /* Need to: get the symbol; get the section. */
13214 isym = &rcookie->locsyms[r_symndx];
cb33740c 13215 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13216 if (isec != NULL
13217 && (isec->kept_section != NULL
13218 || discarded_section (isec)))
cb33740c 13219 return TRUE;
c152c796
AM
13220 }
13221 return FALSE;
13222 }
13223 return FALSE;
13224}
13225
13226/* Discard unneeded references to discarded sections.
75938853
AM
13227 Returns -1 on error, 1 if any section's size was changed, 0 if
13228 nothing changed. This function assumes that the relocations are in
13229 sorted order, which is true for all known assemblers. */
c152c796 13230
75938853 13231int
c152c796
AM
13232bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13233{
13234 struct elf_reloc_cookie cookie;
18cd5bce 13235 asection *o;
c152c796 13236 bfd *abfd;
75938853 13237 int changed = 0;
c152c796
AM
13238
13239 if (info->traditional_format
13240 || !is_elf_hash_table (info->hash))
75938853 13241 return 0;
c152c796 13242
18cd5bce
AM
13243 o = bfd_get_section_by_name (output_bfd, ".stab");
13244 if (o != NULL)
c152c796 13245 {
18cd5bce 13246 asection *i;
c152c796 13247
18cd5bce 13248 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13249 {
18cd5bce
AM
13250 if (i->size == 0
13251 || i->reloc_count == 0
13252 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13253 continue;
c152c796 13254
18cd5bce
AM
13255 abfd = i->owner;
13256 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13257 continue;
c152c796 13258
18cd5bce 13259 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13260 return -1;
c152c796 13261
18cd5bce
AM
13262 if (_bfd_discard_section_stabs (abfd, i,
13263 elf_section_data (i)->sec_info,
5241d853
RS
13264 bfd_elf_reloc_symbol_deleted_p,
13265 &cookie))
75938853 13266 changed = 1;
18cd5bce
AM
13267
13268 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13269 }
18cd5bce
AM
13270 }
13271
2f0c68f2
CM
13272 o = NULL;
13273 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13274 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13275 if (o != NULL)
13276 {
13277 asection *i;
c152c796 13278
18cd5bce 13279 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13280 {
18cd5bce
AM
13281 if (i->size == 0)
13282 continue;
13283
13284 abfd = i->owner;
13285 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13286 continue;
13287
13288 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13289 return -1;
18cd5bce
AM
13290
13291 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13292 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13293 bfd_elf_reloc_symbol_deleted_p,
13294 &cookie))
75938853 13295 changed = 1;
18cd5bce
AM
13296
13297 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13298 }
18cd5bce 13299 }
c152c796 13300
18cd5bce
AM
13301 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13302 {
13303 const struct elf_backend_data *bed;
c152c796 13304
18cd5bce
AM
13305 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13306 continue;
13307
13308 bed = get_elf_backend_data (abfd);
13309
13310 if (bed->elf_backend_discard_info != NULL)
13311 {
13312 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13313 return -1;
18cd5bce
AM
13314
13315 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13316 changed = 1;
18cd5bce
AM
13317
13318 fini_reloc_cookie (&cookie, abfd);
13319 }
c152c796
AM
13320 }
13321
2f0c68f2
CM
13322 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13323 _bfd_elf_end_eh_frame_parsing (info);
13324
13325 if (info->eh_frame_hdr_type
0e1862bb 13326 && !bfd_link_relocatable (info)
c152c796 13327 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13328 changed = 1;
c152c796 13329
75938853 13330 return changed;
c152c796 13331}
082b7297 13332
43e1669b 13333bfd_boolean
0c511000 13334_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13335 asection *sec,
c0f00686 13336 struct bfd_link_info *info)
082b7297
L
13337{
13338 flagword flags;
c77ec726 13339 const char *name, *key;
082b7297
L
13340 struct bfd_section_already_linked *l;
13341 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13342
c77ec726
AM
13343 if (sec->output_section == bfd_abs_section_ptr)
13344 return FALSE;
0c511000 13345
c77ec726 13346 flags = sec->flags;
0c511000 13347
c77ec726
AM
13348 /* Return if it isn't a linkonce section. A comdat group section
13349 also has SEC_LINK_ONCE set. */
13350 if ((flags & SEC_LINK_ONCE) == 0)
13351 return FALSE;
0c511000 13352
c77ec726
AM
13353 /* Don't put group member sections on our list of already linked
13354 sections. They are handled as a group via their group section. */
13355 if (elf_sec_group (sec) != NULL)
13356 return FALSE;
0c511000 13357
c77ec726
AM
13358 /* For a SHT_GROUP section, use the group signature as the key. */
13359 name = sec->name;
13360 if ((flags & SEC_GROUP) != 0
13361 && elf_next_in_group (sec) != NULL
13362 && elf_group_name (elf_next_in_group (sec)) != NULL)
13363 key = elf_group_name (elf_next_in_group (sec));
13364 else
13365 {
13366 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13367 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13368 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13369 key++;
0c511000 13370 else
c77ec726
AM
13371 /* Must be a user linkonce section that doesn't follow gcc's
13372 naming convention. In this case we won't be matching
13373 single member groups. */
13374 key = name;
0c511000 13375 }
6d2cd210 13376
c77ec726 13377 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13378
13379 for (l = already_linked_list->entry; l != NULL; l = l->next)
13380 {
c2370991 13381 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13382 sections with a signature of <key> (<key> is some string),
13383 and linkonce sections named .gnu.linkonce.<type>.<key>.
13384 Match like sections. LTO plugin sections are an exception.
13385 They are always named .gnu.linkonce.t.<key> and match either
13386 type of section. */
13387 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13388 && ((flags & SEC_GROUP) != 0
13389 || strcmp (name, l->sec->name) == 0))
13390 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13391 {
13392 /* The section has already been linked. See if we should
6d2cd210 13393 issue a warning. */
c77ec726
AM
13394 if (!_bfd_handle_already_linked (sec, l, info))
13395 return FALSE;
082b7297 13396
c77ec726 13397 if (flags & SEC_GROUP)
3d7f7666 13398 {
c77ec726
AM
13399 asection *first = elf_next_in_group (sec);
13400 asection *s = first;
3d7f7666 13401
c77ec726 13402 while (s != NULL)
3d7f7666 13403 {
c77ec726
AM
13404 s->output_section = bfd_abs_section_ptr;
13405 /* Record which group discards it. */
13406 s->kept_section = l->sec;
13407 s = elf_next_in_group (s);
13408 /* These lists are circular. */
13409 if (s == first)
13410 break;
3d7f7666
L
13411 }
13412 }
082b7297 13413
43e1669b 13414 return TRUE;
082b7297
L
13415 }
13416 }
13417
c77ec726
AM
13418 /* A single member comdat group section may be discarded by a
13419 linkonce section and vice versa. */
13420 if ((flags & SEC_GROUP) != 0)
3d7f7666 13421 {
c77ec726 13422 asection *first = elf_next_in_group (sec);
c2370991 13423
c77ec726
AM
13424 if (first != NULL && elf_next_in_group (first) == first)
13425 /* Check this single member group against linkonce sections. */
13426 for (l = already_linked_list->entry; l != NULL; l = l->next)
13427 if ((l->sec->flags & SEC_GROUP) == 0
13428 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13429 {
13430 first->output_section = bfd_abs_section_ptr;
13431 first->kept_section = l->sec;
13432 sec->output_section = bfd_abs_section_ptr;
13433 break;
13434 }
13435 }
13436 else
13437 /* Check this linkonce section against single member groups. */
13438 for (l = already_linked_list->entry; l != NULL; l = l->next)
13439 if (l->sec->flags & SEC_GROUP)
6d2cd210 13440 {
c77ec726 13441 asection *first = elf_next_in_group (l->sec);
6d2cd210 13442
c77ec726
AM
13443 if (first != NULL
13444 && elf_next_in_group (first) == first
13445 && bfd_elf_match_symbols_in_sections (first, sec, info))
13446 {
13447 sec->output_section = bfd_abs_section_ptr;
13448 sec->kept_section = first;
13449 break;
13450 }
6d2cd210 13451 }
0c511000 13452
c77ec726
AM
13453 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13454 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13455 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13456 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13457 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13458 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13459 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13460 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13461 The reverse order cannot happen as there is never a bfd with only the
13462 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13463 matter as here were are looking only for cross-bfd sections. */
13464
13465 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13466 for (l = already_linked_list->entry; l != NULL; l = l->next)
13467 if ((l->sec->flags & SEC_GROUP) == 0
13468 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13469 {
13470 if (abfd != l->sec->owner)
13471 sec->output_section = bfd_abs_section_ptr;
13472 break;
13473 }
80c29487 13474
082b7297 13475 /* This is the first section with this name. Record it. */
c77ec726 13476 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13477 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13478 return sec->output_section == bfd_abs_section_ptr;
082b7297 13479}
81e1b023 13480
a4d8e49b
L
13481bfd_boolean
13482_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13483{
13484 return sym->st_shndx == SHN_COMMON;
13485}
13486
13487unsigned int
13488_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13489{
13490 return SHN_COMMON;
13491}
13492
13493asection *
13494_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13495{
13496 return bfd_com_section_ptr;
13497}
10455f89
HPN
13498
13499bfd_vma
13500_bfd_elf_default_got_elt_size (bfd *abfd,
13501 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13502 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13503 bfd *ibfd ATTRIBUTE_UNUSED,
13504 unsigned long symndx ATTRIBUTE_UNUSED)
13505{
13506 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13507 return bed->s->arch_size / 8;
13508}
83bac4b0
NC
13509
13510/* Routines to support the creation of dynamic relocs. */
13511
83bac4b0
NC
13512/* Returns the name of the dynamic reloc section associated with SEC. */
13513
13514static const char *
13515get_dynamic_reloc_section_name (bfd * abfd,
13516 asection * sec,
13517 bfd_boolean is_rela)
13518{
ddcf1fcf
BS
13519 char *name;
13520 const char *old_name = bfd_get_section_name (NULL, sec);
13521 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13522
ddcf1fcf 13523 if (old_name == NULL)
83bac4b0
NC
13524 return NULL;
13525
ddcf1fcf 13526 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13527 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13528
13529 return name;
13530}
13531
13532/* Returns the dynamic reloc section associated with SEC.
13533 If necessary compute the name of the dynamic reloc section based
13534 on SEC's name (looked up in ABFD's string table) and the setting
13535 of IS_RELA. */
13536
13537asection *
13538_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13539 asection * sec,
13540 bfd_boolean is_rela)
13541{
13542 asection * reloc_sec = elf_section_data (sec)->sreloc;
13543
13544 if (reloc_sec == NULL)
13545 {
13546 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13547
13548 if (name != NULL)
13549 {
3d4d4302 13550 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13551
13552 if (reloc_sec != NULL)
13553 elf_section_data (sec)->sreloc = reloc_sec;
13554 }
13555 }
13556
13557 return reloc_sec;
13558}
13559
13560/* Returns the dynamic reloc section associated with SEC. If the
13561 section does not exist it is created and attached to the DYNOBJ
13562 bfd and stored in the SRELOC field of SEC's elf_section_data
13563 structure.
f8076f98 13564
83bac4b0
NC
13565 ALIGNMENT is the alignment for the newly created section and
13566 IS_RELA defines whether the name should be .rela.<SEC's name>
13567 or .rel.<SEC's name>. The section name is looked up in the
13568 string table associated with ABFD. */
13569
13570asection *
ca4be51c
AM
13571_bfd_elf_make_dynamic_reloc_section (asection *sec,
13572 bfd *dynobj,
13573 unsigned int alignment,
13574 bfd *abfd,
13575 bfd_boolean is_rela)
83bac4b0
NC
13576{
13577 asection * reloc_sec = elf_section_data (sec)->sreloc;
13578
13579 if (reloc_sec == NULL)
13580 {
13581 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13582
13583 if (name == NULL)
13584 return NULL;
13585
3d4d4302 13586 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13587
13588 if (reloc_sec == NULL)
13589 {
3d4d4302
AM
13590 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13591 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13592 if ((sec->flags & SEC_ALLOC) != 0)
13593 flags |= SEC_ALLOC | SEC_LOAD;
13594
3d4d4302 13595 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13596 if (reloc_sec != NULL)
13597 {
8877b5e5
AM
13598 /* _bfd_elf_get_sec_type_attr chooses a section type by
13599 name. Override as it may be wrong, eg. for a user
13600 section named "auto" we'll get ".relauto" which is
13601 seen to be a .rela section. */
13602 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13603 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13604 reloc_sec = NULL;
13605 }
13606 }
13607
13608 elf_section_data (sec)->sreloc = reloc_sec;
13609 }
13610
13611 return reloc_sec;
13612}
1338dd10 13613
bffebb6b
AM
13614/* Copy the ELF symbol type and other attributes for a linker script
13615 assignment from HSRC to HDEST. Generally this should be treated as
13616 if we found a strong non-dynamic definition for HDEST (except that
13617 ld ignores multiple definition errors). */
1338dd10 13618void
bffebb6b
AM
13619_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13620 struct bfd_link_hash_entry *hdest,
13621 struct bfd_link_hash_entry *hsrc)
1338dd10 13622{
bffebb6b
AM
13623 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13624 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13625 Elf_Internal_Sym isym;
1338dd10
PB
13626
13627 ehdest->type = ehsrc->type;
35fc36a8 13628 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13629
13630 isym.st_other = ehsrc->other;
b8417128 13631 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13632}
351f65ca
L
13633
13634/* Append a RELA relocation REL to section S in BFD. */
13635
13636void
13637elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13638{
13639 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13640 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13641 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13642 bed->s->swap_reloca_out (abfd, rel, loc);
13643}
13644
13645/* Append a REL relocation REL to section S in BFD. */
13646
13647void
13648elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13649{
13650 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13651 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13652 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13653 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13654}
This page took 2.922596 seconds and 4 git commands to generate.