Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
b90efa5b 2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
252b5132 31
28caa186
AM
32/* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35struct elf_info_failed
36{
37 struct bfd_link_info *info;
28caa186
AM
38 bfd_boolean failed;
39};
40
41/* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44struct elf_find_verdep_info
45{
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52};
53
54static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
2f0c68f2
CM
57asection *
58_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61{
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96}
97
d98685ac
AM
98/* Define a symbol in a dynamic linkage section. */
99
100struct elf_link_hash_entry *
101_bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105{
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
ccabcbe5 108 const struct elf_backend_data *bed;
d98685ac
AM
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
cf18fda4 121 bed = get_elf_backend_data (abfd);
d98685ac 122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 123 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
e28df02b 128 h->non_elf = 0;
12b2843a 129 h->root.linker_def = 1;
d98685ac 130 h->type = STT_OBJECT;
00b7642b
AM
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 133
ccabcbe5 134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
135 return h;
136}
137
b34976b6 138bfd_boolean
268b6b39 139_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
140{
141 flagword flags;
aad5d350 142 asection *s;
252b5132 143 struct elf_link_hash_entry *h;
9c5bfbb7 144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 145 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
146
147 /* This function may be called more than once. */
3d4d4302
AM
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
b34976b6 150 return TRUE;
252b5132 151
e5a52504 152 flags = bed->dynamic_sec_flags;
252b5132 153
14b2f831
AM
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
6de2ae4a
L
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
252b5132 163
14b2f831 164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
252b5132
RH
170 if (bed->want_got_plt)
171 {
14b2f831 172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 173 if (s == NULL
6de2ae4a
L
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
b34976b6 176 return FALSE;
6de2ae4a 177 htab->sgotplt = s;
252b5132
RH
178 }
179
64e77c6d
L
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
2517a57f
AM
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
6de2ae4a
L
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
2517a57f 191 elf_hash_table (info)->hgot = h;
d98685ac
AM
192 if (h == NULL)
193 return FALSE;
2517a57f 194 }
252b5132 195
b34976b6 196 return TRUE;
252b5132
RH
197}
198\f
7e9f0867
AM
199/* Create a strtab to hold the dynamic symbol names. */
200static bfd_boolean
201_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202{
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 hash_table->dynobj = abfd;
208
209 if (hash_table->dynstr == NULL)
210 {
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
213 return FALSE;
214 }
215 return TRUE;
216}
217
45d6a902
AM
218/* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
252b5132 224
b34976b6 225bfd_boolean
268b6b39 226_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 227{
45d6a902 228 flagword flags;
91d6fa6a 229 asection *s;
9c5bfbb7 230 const struct elf_backend_data *bed;
9637f6ef 231 struct elf_link_hash_entry *h;
252b5132 232
0eddce27 233 if (! is_elf_hash_table (info->hash))
45d6a902
AM
234 return FALSE;
235
236 if (elf_hash_table (info)->dynamic_sections_created)
237 return TRUE;
238
7e9f0867
AM
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240 return FALSE;
45d6a902 241
7e9f0867 242 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
243 bed = get_elf_backend_data (abfd);
244
245 flags = bed->dynamic_sec_flags;
45d6a902
AM
246
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
9b8b325a 249 if (bfd_link_executable (info) && !info->nointerp)
252b5132 250 {
14b2f831
AM
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
3496cb2a 253 if (s == NULL)
45d6a902
AM
254 return FALSE;
255 }
bb0deeff 256
45d6a902
AM
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
14b2f831
AM
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
45d6a902 261 if (s == NULL
45d6a902
AM
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263 return FALSE;
264
14b2f831
AM
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
45d6a902 267 if (s == NULL
45d6a902
AM
268 || ! bfd_set_section_alignment (abfd, s, 1))
269 return FALSE;
270
14b2f831
AM
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
45d6a902 273 if (s == NULL
45d6a902
AM
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275 return FALSE;
276
14b2f831
AM
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
45d6a902 279 if (s == NULL
45d6a902
AM
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281 return FALSE;
cae1fbbb 282 elf_hash_table (info)->dynsym = s;
45d6a902 283
14b2f831
AM
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
3496cb2a 286 if (s == NULL)
45d6a902
AM
287 return FALSE;
288
14b2f831 289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
294 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
9637f6ef
L
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
302 if (h == NULL)
45d6a902
AM
303 return FALSE;
304
fdc90cb4
JJ
305 if (info->emit_hash)
306 {
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
fdc90cb4
JJ
309 if (s == NULL
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 return FALSE;
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313 }
314
315 if (info->emit_gnu_hash)
316 {
14b2f831
AM
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
fdc90cb4
JJ
319 if (s == NULL
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 return FALSE;
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
327 else
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
329 }
45d6a902
AM
330
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
894891db
NC
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
336 return FALSE;
337
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
339
340 return TRUE;
341}
342
343/* Create dynamic sections when linking against a dynamic object. */
344
345bfd_boolean
268b6b39 346_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
347{
348 flagword flags, pltflags;
7325306f 349 struct elf_link_hash_entry *h;
45d6a902 350 asection *s;
9c5bfbb7 351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 352 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 353
252b5132
RH
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
e5a52504 356 flags = bed->dynamic_sec_flags;
252b5132
RH
357
358 pltflags = flags;
252b5132 359 if (bed->plt_not_loaded)
6df4d94c
MM
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
5d1634d7 363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
364 else
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
368
14b2f831 369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 370 if (s == NULL
252b5132 371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 372 return FALSE;
6de2ae4a 373 htab->splt = s;
252b5132 374
d98685ac
AM
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376 .plt section. */
7325306f
RS
377 if (bed->want_plt_sym)
378 {
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
382 if (h == NULL)
383 return FALSE;
384 }
252b5132 385
14b2f831
AM
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
252b5132 390 if (s == NULL
45d6a902 391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 392 return FALSE;
6de2ae4a 393 htab->srelplt = s;
252b5132
RH
394
395 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 396 return FALSE;
252b5132 397
3018b441
RH
398 if (bed->want_dynbss)
399 {
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
14b2f831
AM
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 408 if (s == NULL)
b34976b6 409 return FALSE;
252b5132 410
3018b441 411 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
421 copy relocs. */
0e1862bb 422 if (! bfd_link_pic (info))
3018b441 423 {
14b2f831
AM
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
3018b441 428 if (s == NULL
45d6a902 429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 430 return FALSE;
3018b441 431 }
252b5132
RH
432 }
433
b34976b6 434 return TRUE;
252b5132
RH
435}
436\f
252b5132
RH
437/* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
443 one. */
444
b34976b6 445bfd_boolean
c152c796
AM
446bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
252b5132
RH
448{
449 if (h->dynindx == -1)
450 {
2b0f7ef9 451 struct elf_strtab_hash *dynstr;
68b6ddd0 452 char *p;
252b5132 453 const char *name;
252b5132
RH
454 bfd_size_type indx;
455
7a13edea
NC
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
461 {
462 case STV_INTERNAL:
463 case STV_HIDDEN:
9d6eee78
L
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
38048eb9 466 {
f5385ebf 467 h->forced_local = 1;
67687978
PB
468 if (!elf_hash_table (info)->is_relocatable_executable)
469 return TRUE;
7a13edea 470 }
0444bdd4 471
7a13edea
NC
472 default:
473 break;
474 }
475
252b5132
RH
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
478
479 dynstr = elf_hash_table (info)->dynstr;
480 if (dynstr == NULL)
481 {
482 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 484 if (dynstr == NULL)
b34976b6 485 return FALSE;
252b5132
RH
486 }
487
488 /* We don't put any version information in the dynamic string
aad5d350 489 table. */
252b5132
RH
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
492 if (p != NULL)
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
498 *p = 0;
499
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501
502 if (p != NULL)
503 *p = ELF_VER_CHR;
252b5132
RH
504
505 if (indx == (bfd_size_type) -1)
b34976b6 506 return FALSE;
252b5132
RH
507 h->dynstr_index = indx;
508 }
509
b34976b6 510 return TRUE;
252b5132 511}
45d6a902 512\f
55255dae
L
513/* Mark a symbol dynamic. */
514
28caa186 515static void
55255dae 516bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
55255dae 519{
40b36307 520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 521
40b36307 522 /* It may be called more than once on the same H. */
0e1862bb 523 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
524 return;
525
40b36307
L
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
528 || (sym != NULL
529 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 530 || (d != NULL
40b36307
L
531 && h->root.type == bfd_link_hash_new
532 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
533 h->dynamic = 1;
534}
535
45d6a902
AM
536/* Record an assignment to a symbol made by a linker script. We need
537 this in case some dynamic object refers to this symbol. */
538
539bfd_boolean
fe21a8fc
L
540bfd_elf_record_link_assignment (bfd *output_bfd,
541 struct bfd_link_info *info,
268b6b39 542 const char *name,
fe21a8fc
L
543 bfd_boolean provide,
544 bfd_boolean hidden)
45d6a902 545{
00cbee0a 546 struct elf_link_hash_entry *h, *hv;
4ea42fb7 547 struct elf_link_hash_table *htab;
00cbee0a 548 const struct elf_backend_data *bed;
45d6a902 549
0eddce27 550 if (!is_elf_hash_table (info->hash))
45d6a902
AM
551 return TRUE;
552
4ea42fb7
AM
553 htab = elf_hash_table (info);
554 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 555 if (h == NULL)
4ea42fb7 556 return provide;
45d6a902 557
00cbee0a 558 switch (h->root.type)
77cfaee6 559 {
00cbee0a
L
560 case bfd_link_hash_defined:
561 case bfd_link_hash_defweak:
562 case bfd_link_hash_common:
563 break;
564 case bfd_link_hash_undefweak:
565 case bfd_link_hash_undefined:
566 /* Since we're defining the symbol, don't let it seem to have not
567 been defined. record_dynamic_symbol and size_dynamic_sections
568 may depend on this. */
4ea42fb7 569 h->root.type = bfd_link_hash_new;
77cfaee6
AM
570 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
571 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
572 break;
573 case bfd_link_hash_new:
40b36307 574 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 575 h->non_elf = 0;
00cbee0a
L
576 break;
577 case bfd_link_hash_indirect:
578 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 579 the versioned symbol point to this one. */
00cbee0a
L
580 bed = get_elf_backend_data (output_bfd);
581 hv = h;
582 while (hv->root.type == bfd_link_hash_indirect
583 || hv->root.type == bfd_link_hash_warning)
584 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
585 /* We don't need to update h->root.u since linker will set them
586 later. */
587 h->root.type = bfd_link_hash_undefined;
588 hv->root.type = bfd_link_hash_indirect;
589 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
590 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
591 break;
592 case bfd_link_hash_warning:
593 abort ();
594 break;
55255dae 595 }
45d6a902
AM
596
597 /* If this symbol is being provided by the linker script, and it is
598 currently defined by a dynamic object, but not by a regular
599 object, then mark it as undefined so that the generic linker will
600 force the correct value. */
601 if (provide
f5385ebf
AM
602 && h->def_dynamic
603 && !h->def_regular)
45d6a902
AM
604 h->root.type = bfd_link_hash_undefined;
605
606 /* If this symbol is not being provided by the linker script, and it is
607 currently defined by a dynamic object, but not by a regular object,
608 then clear out any version information because the symbol will not be
609 associated with the dynamic object any more. */
610 if (!provide
f5385ebf
AM
611 && h->def_dynamic
612 && !h->def_regular)
45d6a902
AM
613 h->verinfo.verdef = NULL;
614
f5385ebf 615 h->def_regular = 1;
45d6a902 616
eb8476a6 617 if (hidden)
fe21a8fc 618 {
91d6fa6a 619 bed = get_elf_backend_data (output_bfd);
b8297068
AM
620 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
621 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
622 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
623 }
624
6fa3860b
PB
625 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
626 and executables. */
0e1862bb 627 if (!bfd_link_relocatable (info)
6fa3860b
PB
628 && h->dynindx != -1
629 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
630 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
631 h->forced_local = 1;
632
f5385ebf
AM
633 if ((h->def_dynamic
634 || h->ref_dynamic
0e1862bb 635 || bfd_link_pic (info)
3cbc1e5e 636 || (bfd_link_pde (info)
0e1862bb 637 && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
638 && h->dynindx == -1)
639 {
c152c796 640 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
641 return FALSE;
642
643 /* If this is a weak defined symbol, and we know a corresponding
644 real symbol from the same dynamic object, make sure the real
645 symbol is also made into a dynamic symbol. */
f6e332e6
AM
646 if (h->u.weakdef != NULL
647 && h->u.weakdef->dynindx == -1)
45d6a902 648 {
f6e332e6 649 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
650 return FALSE;
651 }
652 }
653
654 return TRUE;
655}
42751cf3 656
8c58d23b
AM
657/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
658 success, and 2 on a failure caused by attempting to record a symbol
659 in a discarded section, eg. a discarded link-once section symbol. */
660
661int
c152c796
AM
662bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
663 bfd *input_bfd,
664 long input_indx)
8c58d23b
AM
665{
666 bfd_size_type amt;
667 struct elf_link_local_dynamic_entry *entry;
668 struct elf_link_hash_table *eht;
669 struct elf_strtab_hash *dynstr;
670 unsigned long dynstr_index;
671 char *name;
672 Elf_External_Sym_Shndx eshndx;
673 char esym[sizeof (Elf64_External_Sym)];
674
0eddce27 675 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
676 return 0;
677
678 /* See if the entry exists already. */
679 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
680 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
681 return 1;
682
683 amt = sizeof (*entry);
a50b1753 684 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
685 if (entry == NULL)
686 return 0;
687
688 /* Go find the symbol, so that we can find it's name. */
689 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 690 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
691 {
692 bfd_release (input_bfd, entry);
693 return 0;
694 }
695
696 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 697 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
698 {
699 asection *s;
700
701 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
702 if (s == NULL || bfd_is_abs_section (s->output_section))
703 {
704 /* We can still bfd_release here as nothing has done another
705 bfd_alloc. We can't do this later in this function. */
706 bfd_release (input_bfd, entry);
707 return 2;
708 }
709 }
710
711 name = (bfd_elf_string_from_elf_section
712 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
713 entry->isym.st_name));
714
715 dynstr = elf_hash_table (info)->dynstr;
716 if (dynstr == NULL)
717 {
718 /* Create a strtab to hold the dynamic symbol names. */
719 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
720 if (dynstr == NULL)
721 return 0;
722 }
723
b34976b6 724 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
725 if (dynstr_index == (unsigned long) -1)
726 return 0;
727 entry->isym.st_name = dynstr_index;
728
729 eht = elf_hash_table (info);
730
731 entry->next = eht->dynlocal;
732 eht->dynlocal = entry;
733 entry->input_bfd = input_bfd;
734 entry->input_indx = input_indx;
735 eht->dynsymcount++;
736
737 /* Whatever binding the symbol had before, it's now local. */
738 entry->isym.st_info
739 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
740
741 /* The dynindx will be set at the end of size_dynamic_sections. */
742
743 return 1;
744}
745
30b30c21 746/* Return the dynindex of a local dynamic symbol. */
42751cf3 747
30b30c21 748long
268b6b39
AM
749_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
750 bfd *input_bfd,
751 long input_indx)
30b30c21
RH
752{
753 struct elf_link_local_dynamic_entry *e;
754
755 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
756 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
757 return e->dynindx;
758 return -1;
759}
760
761/* This function is used to renumber the dynamic symbols, if some of
762 them are removed because they are marked as local. This is called
763 via elf_link_hash_traverse. */
764
b34976b6 765static bfd_boolean
268b6b39
AM
766elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
767 void *data)
42751cf3 768{
a50b1753 769 size_t *count = (size_t *) data;
30b30c21 770
6fa3860b
PB
771 if (h->forced_local)
772 return TRUE;
773
774 if (h->dynindx != -1)
775 h->dynindx = ++(*count);
776
777 return TRUE;
778}
779
780
781/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
782 STB_LOCAL binding. */
783
784static bfd_boolean
785elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
786 void *data)
787{
a50b1753 788 size_t *count = (size_t *) data;
6fa3860b 789
6fa3860b
PB
790 if (!h->forced_local)
791 return TRUE;
792
42751cf3 793 if (h->dynindx != -1)
30b30c21
RH
794 h->dynindx = ++(*count);
795
b34976b6 796 return TRUE;
42751cf3 797}
30b30c21 798
aee6f5b4
AO
799/* Return true if the dynamic symbol for a given section should be
800 omitted when creating a shared library. */
801bfd_boolean
802_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
803 struct bfd_link_info *info,
804 asection *p)
805{
74541ad4 806 struct elf_link_hash_table *htab;
ca55926c 807 asection *ip;
74541ad4 808
aee6f5b4
AO
809 switch (elf_section_data (p)->this_hdr.sh_type)
810 {
811 case SHT_PROGBITS:
812 case SHT_NOBITS:
813 /* If sh_type is yet undecided, assume it could be
814 SHT_PROGBITS/SHT_NOBITS. */
815 case SHT_NULL:
74541ad4
AM
816 htab = elf_hash_table (info);
817 if (p == htab->tls_sec)
818 return FALSE;
819
820 if (htab->text_index_section != NULL)
821 return p != htab->text_index_section && p != htab->data_index_section;
822
ca55926c 823 return (htab->dynobj != NULL
3d4d4302 824 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 825 && ip->output_section == p);
aee6f5b4
AO
826
827 /* There shouldn't be section relative relocations
828 against any other section. */
829 default:
830 return TRUE;
831 }
832}
833
062e2358 834/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
835 symbol for each output section, which come first. Next come symbols
836 which have been forced to local binding. Then all of the back-end
837 allocated local dynamic syms, followed by the rest of the global
838 symbols. */
30b30c21 839
554220db
AM
840static unsigned long
841_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
842 struct bfd_link_info *info,
843 unsigned long *section_sym_count)
30b30c21
RH
844{
845 unsigned long dynsymcount = 0;
846
0e1862bb
L
847 if (bfd_link_pic (info)
848 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 849 {
aee6f5b4 850 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
851 asection *p;
852 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 853 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
854 && (p->flags & SEC_ALLOC) != 0
855 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
856 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
857 else
858 elf_section_data (p)->dynindx = 0;
30b30c21 859 }
554220db 860 *section_sym_count = dynsymcount;
30b30c21 861
6fa3860b
PB
862 elf_link_hash_traverse (elf_hash_table (info),
863 elf_link_renumber_local_hash_table_dynsyms,
864 &dynsymcount);
865
30b30c21
RH
866 if (elf_hash_table (info)->dynlocal)
867 {
868 struct elf_link_local_dynamic_entry *p;
869 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
870 p->dynindx = ++dynsymcount;
871 }
872
873 elf_link_hash_traverse (elf_hash_table (info),
874 elf_link_renumber_hash_table_dynsyms,
875 &dynsymcount);
876
877 /* There is an unused NULL entry at the head of the table which
878 we must account for in our count. Unless there weren't any
879 symbols, which means we'll have no table at all. */
880 if (dynsymcount != 0)
881 ++dynsymcount;
882
ccabcbe5
AM
883 elf_hash_table (info)->dynsymcount = dynsymcount;
884 return dynsymcount;
30b30c21 885}
252b5132 886
54ac0771
L
887/* Merge st_other field. */
888
889static void
890elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 891 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 892 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
893{
894 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
895
896 /* If st_other has a processor-specific meaning, specific
cd3416da 897 code might be needed here. */
54ac0771
L
898 if (bed->elf_backend_merge_symbol_attribute)
899 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
900 dynamic);
901
cd3416da 902 if (!dynamic)
54ac0771 903 {
cd3416da
AM
904 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
905 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 906
cd3416da
AM
907 /* Keep the most constraining visibility. Leave the remainder
908 of the st_other field to elf_backend_merge_symbol_attribute. */
909 if (symvis - 1 < hvis - 1)
910 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 911 }
b8417128
AM
912 else if (definition
913 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
914 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 915 h->protected_def = 1;
54ac0771
L
916}
917
4f3fedcf
AM
918/* This function is called when we want to merge a new symbol with an
919 existing symbol. It handles the various cases which arise when we
920 find a definition in a dynamic object, or when there is already a
921 definition in a dynamic object. The new symbol is described by
922 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
923 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
924 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
925 of an old common symbol. We set OVERRIDE if the old symbol is
926 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
927 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
928 to change. By OK to change, we mean that we shouldn't warn if the
929 type or size does change. */
45d6a902 930
8a56bd02 931static bfd_boolean
268b6b39
AM
932_bfd_elf_merge_symbol (bfd *abfd,
933 struct bfd_link_info *info,
934 const char *name,
935 Elf_Internal_Sym *sym,
936 asection **psec,
937 bfd_vma *pvalue,
4f3fedcf
AM
938 struct elf_link_hash_entry **sym_hash,
939 bfd **poldbfd,
37a9e49a 940 bfd_boolean *pold_weak,
af44c138 941 unsigned int *pold_alignment,
268b6b39
AM
942 bfd_boolean *skip,
943 bfd_boolean *override,
944 bfd_boolean *type_change_ok,
6e33951e
L
945 bfd_boolean *size_change_ok,
946 bfd_boolean *matched)
252b5132 947{
7479dfd4 948 asection *sec, *oldsec;
45d6a902 949 struct elf_link_hash_entry *h;
90c984fc 950 struct elf_link_hash_entry *hi;
45d6a902
AM
951 struct elf_link_hash_entry *flip;
952 int bind;
953 bfd *oldbfd;
954 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 955 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 956 const struct elf_backend_data *bed;
6e33951e 957 char *new_version;
45d6a902
AM
958
959 *skip = FALSE;
960 *override = FALSE;
961
962 sec = *psec;
963 bind = ELF_ST_BIND (sym->st_info);
964
965 if (! bfd_is_und_section (sec))
966 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
967 else
968 h = ((struct elf_link_hash_entry *)
969 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
970 if (h == NULL)
971 return FALSE;
972 *sym_hash = h;
252b5132 973
88ba32a0
L
974 bed = get_elf_backend_data (abfd);
975
6e33951e 976 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 977 if (h->versioned != unversioned)
6e33951e 978 {
422f1182
L
979 /* Symbol version is unknown or versioned. */
980 new_version = strrchr (name, ELF_VER_CHR);
981 if (new_version)
982 {
983 if (h->versioned == unknown)
984 {
985 if (new_version > name && new_version[-1] != ELF_VER_CHR)
986 h->versioned = versioned_hidden;
987 else
988 h->versioned = versioned;
989 }
990 new_version += 1;
991 if (new_version[0] == '\0')
992 new_version = NULL;
993 }
994 else
995 h->versioned = unversioned;
6e33951e 996 }
422f1182
L
997 else
998 new_version = NULL;
6e33951e 999
90c984fc
L
1000 /* For merging, we only care about real symbols. But we need to make
1001 sure that indirect symbol dynamic flags are updated. */
1002 hi = h;
45d6a902
AM
1003 while (h->root.type == bfd_link_hash_indirect
1004 || h->root.type == bfd_link_hash_warning)
1005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1006
6e33951e
L
1007 if (!*matched)
1008 {
1009 if (hi == h || h->root.type == bfd_link_hash_new)
1010 *matched = TRUE;
1011 else
1012 {
1013 /* OLD_HIDDEN is true if the existing symbol is only visibile
1014 to the symbol with the same symbol version. NEW_HIDDEN is
1015 true if the new symbol is only visibile to the symbol with
1016 the same symbol version. */
422f1182
L
1017 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1018 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1019 if (!old_hidden && !new_hidden)
1020 /* The new symbol matches the existing symbol if both
1021 aren't hidden. */
1022 *matched = TRUE;
1023 else
1024 {
1025 /* OLD_VERSION is the symbol version of the existing
1026 symbol. */
422f1182
L
1027 char *old_version;
1028
1029 if (h->versioned >= versioned)
1030 old_version = strrchr (h->root.root.string,
1031 ELF_VER_CHR) + 1;
1032 else
1033 old_version = NULL;
6e33951e
L
1034
1035 /* The new symbol matches the existing symbol if they
1036 have the same symbol version. */
1037 *matched = (old_version == new_version
1038 || (old_version != NULL
1039 && new_version != NULL
1040 && strcmp (old_version, new_version) == 0));
1041 }
1042 }
1043 }
1044
934bce08
AM
1045 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1046 existing symbol. */
1047
1048 oldbfd = NULL;
1049 oldsec = NULL;
1050 switch (h->root.type)
1051 {
1052 default:
1053 break;
1054
1055 case bfd_link_hash_undefined:
1056 case bfd_link_hash_undefweak:
1057 oldbfd = h->root.u.undef.abfd;
1058 break;
1059
1060 case bfd_link_hash_defined:
1061 case bfd_link_hash_defweak:
1062 oldbfd = h->root.u.def.section->owner;
1063 oldsec = h->root.u.def.section;
1064 break;
1065
1066 case bfd_link_hash_common:
1067 oldbfd = h->root.u.c.p->section->owner;
1068 oldsec = h->root.u.c.p->section;
1069 if (pold_alignment)
1070 *pold_alignment = h->root.u.c.p->alignment_power;
1071 break;
1072 }
1073 if (poldbfd && *poldbfd == NULL)
1074 *poldbfd = oldbfd;
1075
1076 /* Differentiate strong and weak symbols. */
1077 newweak = bind == STB_WEAK;
1078 oldweak = (h->root.type == bfd_link_hash_defweak
1079 || h->root.type == bfd_link_hash_undefweak);
1080 if (pold_weak)
1081 *pold_weak = oldweak;
1082
1083 /* This code is for coping with dynamic objects, and is only useful
1084 if we are doing an ELF link. */
1085 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1086 return TRUE;
1087
40b36307 1088 /* We have to check it for every instance since the first few may be
ee659f1f 1089 references and not all compilers emit symbol type for undefined
40b36307
L
1090 symbols. */
1091 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1092
ee659f1f
AM
1093 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1094 respectively, is from a dynamic object. */
1095
1096 newdyn = (abfd->flags & DYNAMIC) != 0;
1097
1098 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1099 syms and defined syms in dynamic libraries respectively.
1100 ref_dynamic on the other hand can be set for a symbol defined in
1101 a dynamic library, and def_dynamic may not be set; When the
1102 definition in a dynamic lib is overridden by a definition in the
1103 executable use of the symbol in the dynamic lib becomes a
1104 reference to the executable symbol. */
1105 if (newdyn)
1106 {
1107 if (bfd_is_und_section (sec))
1108 {
1109 if (bind != STB_WEAK)
1110 {
1111 h->ref_dynamic_nonweak = 1;
1112 hi->ref_dynamic_nonweak = 1;
1113 }
1114 }
1115 else
1116 {
6e33951e
L
1117 /* Update the existing symbol only if they match. */
1118 if (*matched)
1119 h->dynamic_def = 1;
ee659f1f
AM
1120 hi->dynamic_def = 1;
1121 }
1122 }
1123
45d6a902
AM
1124 /* If we just created the symbol, mark it as being an ELF symbol.
1125 Other than that, there is nothing to do--there is no merge issue
1126 with a newly defined symbol--so we just return. */
1127
1128 if (h->root.type == bfd_link_hash_new)
252b5132 1129 {
f5385ebf 1130 h->non_elf = 0;
45d6a902
AM
1131 return TRUE;
1132 }
252b5132 1133
45d6a902
AM
1134 /* In cases involving weak versioned symbols, we may wind up trying
1135 to merge a symbol with itself. Catch that here, to avoid the
1136 confusion that results if we try to override a symbol with
1137 itself. The additional tests catch cases like
1138 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1139 dynamic object, which we do want to handle here. */
1140 if (abfd == oldbfd
895fa45f 1141 && (newweak || oldweak)
45d6a902 1142 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1143 || !h->def_regular))
45d6a902
AM
1144 return TRUE;
1145
707bba77 1146 olddyn = FALSE;
45d6a902
AM
1147 if (oldbfd != NULL)
1148 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1149 else if (oldsec != NULL)
45d6a902 1150 {
707bba77 1151 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1152 indices used by MIPS ELF. */
707bba77 1153 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1154 }
252b5132 1155
45d6a902
AM
1156 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1157 respectively, appear to be a definition rather than reference. */
1158
707bba77 1159 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1160
707bba77
AM
1161 olddef = (h->root.type != bfd_link_hash_undefined
1162 && h->root.type != bfd_link_hash_undefweak
1163 && h->root.type != bfd_link_hash_common);
45d6a902 1164
0a36a439
L
1165 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1166 respectively, appear to be a function. */
1167
1168 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1169 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1170
1171 oldfunc = (h->type != STT_NOTYPE
1172 && bed->is_function_type (h->type));
1173
580a2b6e
L
1174 /* When we try to create a default indirect symbol from the dynamic
1175 definition with the default version, we skip it if its type and
40101021 1176 the type of existing regular definition mismatch. */
580a2b6e 1177 if (pold_alignment == NULL
580a2b6e
L
1178 && newdyn
1179 && newdef
1180 && !olddyn
4584ec12
L
1181 && (((olddef || h->root.type == bfd_link_hash_common)
1182 && ELF_ST_TYPE (sym->st_info) != h->type
1183 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1184 && h->type != STT_NOTYPE
1185 && !(newfunc && oldfunc))
1186 || (olddef
1187 && ((h->type == STT_GNU_IFUNC)
1188 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1189 {
1190 *skip = TRUE;
1191 return TRUE;
1192 }
1193
4c34aff8
AM
1194 /* Check TLS symbols. We don't check undefined symbols introduced
1195 by "ld -u" which have no type (and oldbfd NULL), and we don't
1196 check symbols from plugins because they also have no type. */
1197 if (oldbfd != NULL
1198 && (oldbfd->flags & BFD_PLUGIN) == 0
1199 && (abfd->flags & BFD_PLUGIN) == 0
1200 && ELF_ST_TYPE (sym->st_info) != h->type
1201 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1202 {
1203 bfd *ntbfd, *tbfd;
1204 bfd_boolean ntdef, tdef;
1205 asection *ntsec, *tsec;
1206
1207 if (h->type == STT_TLS)
1208 {
3b36f7e6 1209 ntbfd = abfd;
7479dfd4
L
1210 ntsec = sec;
1211 ntdef = newdef;
1212 tbfd = oldbfd;
1213 tsec = oldsec;
1214 tdef = olddef;
1215 }
1216 else
1217 {
1218 ntbfd = oldbfd;
1219 ntsec = oldsec;
1220 ntdef = olddef;
1221 tbfd = abfd;
1222 tsec = sec;
1223 tdef = newdef;
1224 }
1225
1226 if (tdef && ntdef)
1227 (*_bfd_error_handler)
191c0c42
AM
1228 (_("%s: TLS definition in %B section %A "
1229 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1230 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1231 else if (!tdef && !ntdef)
1232 (*_bfd_error_handler)
191c0c42
AM
1233 (_("%s: TLS reference in %B "
1234 "mismatches non-TLS reference in %B"),
7479dfd4
L
1235 tbfd, ntbfd, h->root.root.string);
1236 else if (tdef)
1237 (*_bfd_error_handler)
191c0c42
AM
1238 (_("%s: TLS definition in %B section %A "
1239 "mismatches non-TLS reference in %B"),
7479dfd4
L
1240 tbfd, tsec, ntbfd, h->root.root.string);
1241 else
1242 (*_bfd_error_handler)
191c0c42
AM
1243 (_("%s: TLS reference in %B "
1244 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1245 tbfd, ntbfd, ntsec, h->root.root.string);
1246
1247 bfd_set_error (bfd_error_bad_value);
1248 return FALSE;
1249 }
1250
45d6a902
AM
1251 /* If the old symbol has non-default visibility, we ignore the new
1252 definition from a dynamic object. */
1253 if (newdyn
9c7a29a3 1254 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1255 && !bfd_is_und_section (sec))
1256 {
1257 *skip = TRUE;
1258 /* Make sure this symbol is dynamic. */
f5385ebf 1259 h->ref_dynamic = 1;
90c984fc 1260 hi->ref_dynamic = 1;
45d6a902
AM
1261 /* A protected symbol has external availability. Make sure it is
1262 recorded as dynamic.
1263
1264 FIXME: Should we check type and size for protected symbol? */
1265 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1266 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1267 else
1268 return TRUE;
1269 }
1270 else if (!newdyn
9c7a29a3 1271 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1272 && h->def_dynamic)
45d6a902
AM
1273 {
1274 /* If the new symbol with non-default visibility comes from a
1275 relocatable file and the old definition comes from a dynamic
1276 object, we remove the old definition. */
6c9b78e6 1277 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1278 {
1279 /* Handle the case where the old dynamic definition is
1280 default versioned. We need to copy the symbol info from
1281 the symbol with default version to the normal one if it
1282 was referenced before. */
1283 if (h->ref_regular)
1284 {
6c9b78e6 1285 hi->root.type = h->root.type;
d2dee3b2 1286 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1287 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1288
6c9b78e6 1289 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1290 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1291 {
aed81c4e
MR
1292 /* If the new symbol is hidden or internal, completely undo
1293 any dynamic link state. */
1294 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1295 h->forced_local = 0;
1296 h->ref_dynamic = 0;
d2dee3b2
L
1297 }
1298 else
aed81c4e
MR
1299 h->ref_dynamic = 1;
1300
1301 h->def_dynamic = 0;
aed81c4e
MR
1302 /* FIXME: Should we check type and size for protected symbol? */
1303 h->size = 0;
1304 h->type = 0;
1305
6c9b78e6 1306 h = hi;
d2dee3b2
L
1307 }
1308 else
6c9b78e6 1309 h = hi;
d2dee3b2 1310 }
1de1a317 1311
f5eda473
AM
1312 /* If the old symbol was undefined before, then it will still be
1313 on the undefs list. If the new symbol is undefined or
1314 common, we can't make it bfd_link_hash_new here, because new
1315 undefined or common symbols will be added to the undefs list
1316 by _bfd_generic_link_add_one_symbol. Symbols may not be
1317 added twice to the undefs list. Also, if the new symbol is
1318 undefweak then we don't want to lose the strong undef. */
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1320 {
1de1a317 1321 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1322 h->root.u.undef.abfd = abfd;
1323 }
1324 else
1325 {
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1328 }
1329
f5eda473 1330 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1331 {
f5eda473
AM
1332 /* If the new symbol is hidden or internal, completely undo
1333 any dynamic link state. */
1334 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1335 h->forced_local = 0;
1336 h->ref_dynamic = 0;
45d6a902 1337 }
f5eda473
AM
1338 else
1339 h->ref_dynamic = 1;
1340 h->def_dynamic = 0;
45d6a902
AM
1341 /* FIXME: Should we check type and size for protected symbol? */
1342 h->size = 0;
1343 h->type = 0;
1344 return TRUE;
1345 }
14a793b2 1346
15b43f48
AM
1347 /* If a new weak symbol definition comes from a regular file and the
1348 old symbol comes from a dynamic library, we treat the new one as
1349 strong. Similarly, an old weak symbol definition from a regular
1350 file is treated as strong when the new symbol comes from a dynamic
1351 library. Further, an old weak symbol from a dynamic library is
1352 treated as strong if the new symbol is from a dynamic library.
1353 This reflects the way glibc's ld.so works.
1354
1355 Do this before setting *type_change_ok or *size_change_ok so that
1356 we warn properly when dynamic library symbols are overridden. */
1357
1358 if (newdef && !newdyn && olddyn)
0f8a2703 1359 newweak = FALSE;
15b43f48 1360 if (olddef && newdyn)
0f8a2703
AM
1361 oldweak = FALSE;
1362
d334575b 1363 /* Allow changes between different types of function symbol. */
0a36a439 1364 if (newfunc && oldfunc)
fcb93ecf
PB
1365 *type_change_ok = TRUE;
1366
79349b09
AM
1367 /* It's OK to change the type if either the existing symbol or the
1368 new symbol is weak. A type change is also OK if the old symbol
1369 is undefined and the new symbol is defined. */
252b5132 1370
79349b09
AM
1371 if (oldweak
1372 || newweak
1373 || (newdef
1374 && h->root.type == bfd_link_hash_undefined))
1375 *type_change_ok = TRUE;
1376
1377 /* It's OK to change the size if either the existing symbol or the
1378 new symbol is weak, or if the old symbol is undefined. */
1379
1380 if (*type_change_ok
1381 || h->root.type == bfd_link_hash_undefined)
1382 *size_change_ok = TRUE;
45d6a902 1383
45d6a902
AM
1384 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1385 symbol, respectively, appears to be a common symbol in a dynamic
1386 object. If a symbol appears in an uninitialized section, and is
1387 not weak, and is not a function, then it may be a common symbol
1388 which was resolved when the dynamic object was created. We want
1389 to treat such symbols specially, because they raise special
1390 considerations when setting the symbol size: if the symbol
1391 appears as a common symbol in a regular object, and the size in
1392 the regular object is larger, we must make sure that we use the
1393 larger size. This problematic case can always be avoided in C,
1394 but it must be handled correctly when using Fortran shared
1395 libraries.
1396
1397 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1398 likewise for OLDDYNCOMMON and OLDDEF.
1399
1400 Note that this test is just a heuristic, and that it is quite
1401 possible to have an uninitialized symbol in a shared object which
1402 is really a definition, rather than a common symbol. This could
1403 lead to some minor confusion when the symbol really is a common
1404 symbol in some regular object. However, I think it will be
1405 harmless. */
1406
1407 if (newdyn
1408 && newdef
79349b09 1409 && !newweak
45d6a902
AM
1410 && (sec->flags & SEC_ALLOC) != 0
1411 && (sec->flags & SEC_LOAD) == 0
1412 && sym->st_size > 0
0a36a439 1413 && !newfunc)
45d6a902
AM
1414 newdyncommon = TRUE;
1415 else
1416 newdyncommon = FALSE;
1417
1418 if (olddyn
1419 && olddef
1420 && h->root.type == bfd_link_hash_defined
f5385ebf 1421 && h->def_dynamic
45d6a902
AM
1422 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1423 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1424 && h->size > 0
0a36a439 1425 && !oldfunc)
45d6a902
AM
1426 olddyncommon = TRUE;
1427 else
1428 olddyncommon = FALSE;
1429
a4d8e49b
L
1430 /* We now know everything about the old and new symbols. We ask the
1431 backend to check if we can merge them. */
5d13b3b3
AM
1432 if (bed->merge_symbol != NULL)
1433 {
1434 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1435 return FALSE;
1436 sec = *psec;
1437 }
a4d8e49b 1438
45d6a902
AM
1439 /* If both the old and the new symbols look like common symbols in a
1440 dynamic object, set the size of the symbol to the larger of the
1441 two. */
1442
1443 if (olddyncommon
1444 && newdyncommon
1445 && sym->st_size != h->size)
1446 {
1447 /* Since we think we have two common symbols, issue a multiple
1448 common warning if desired. Note that we only warn if the
1449 size is different. If the size is the same, we simply let
1450 the old symbol override the new one as normally happens with
1451 symbols defined in dynamic objects. */
1452
1453 if (! ((*info->callbacks->multiple_common)
24f58f47 1454 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1455 return FALSE;
252b5132 1456
45d6a902
AM
1457 if (sym->st_size > h->size)
1458 h->size = sym->st_size;
252b5132 1459
45d6a902 1460 *size_change_ok = TRUE;
252b5132
RH
1461 }
1462
45d6a902
AM
1463 /* If we are looking at a dynamic object, and we have found a
1464 definition, we need to see if the symbol was already defined by
1465 some other object. If so, we want to use the existing
1466 definition, and we do not want to report a multiple symbol
1467 definition error; we do this by clobbering *PSEC to be
1468 bfd_und_section_ptr.
1469
1470 We treat a common symbol as a definition if the symbol in the
1471 shared library is a function, since common symbols always
1472 represent variables; this can cause confusion in principle, but
1473 any such confusion would seem to indicate an erroneous program or
1474 shared library. We also permit a common symbol in a regular
79349b09 1475 object to override a weak symbol in a shared object. */
45d6a902
AM
1476
1477 if (newdyn
1478 && newdef
77cfaee6 1479 && (olddef
45d6a902 1480 || (h->root.type == bfd_link_hash_common
0a36a439 1481 && (newweak || newfunc))))
45d6a902
AM
1482 {
1483 *override = TRUE;
1484 newdef = FALSE;
1485 newdyncommon = FALSE;
252b5132 1486
45d6a902
AM
1487 *psec = sec = bfd_und_section_ptr;
1488 *size_change_ok = TRUE;
252b5132 1489
45d6a902
AM
1490 /* If we get here when the old symbol is a common symbol, then
1491 we are explicitly letting it override a weak symbol or
1492 function in a dynamic object, and we don't want to warn about
1493 a type change. If the old symbol is a defined symbol, a type
1494 change warning may still be appropriate. */
252b5132 1495
45d6a902
AM
1496 if (h->root.type == bfd_link_hash_common)
1497 *type_change_ok = TRUE;
1498 }
1499
1500 /* Handle the special case of an old common symbol merging with a
1501 new symbol which looks like a common symbol in a shared object.
1502 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1503 common symbol, and let _bfd_generic_link_add_one_symbol do the
1504 right thing. */
45d6a902
AM
1505
1506 if (newdyncommon
1507 && h->root.type == bfd_link_hash_common)
1508 {
1509 *override = TRUE;
1510 newdef = FALSE;
1511 newdyncommon = FALSE;
1512 *pvalue = sym->st_size;
a4d8e49b 1513 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1514 *size_change_ok = TRUE;
1515 }
1516
c5e2cead 1517 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1518 if (newdef && olddef && newweak)
54ac0771 1519 {
35ed3f94 1520 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1521 if (!(oldbfd != NULL
1522 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1523 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1524 {
1525 newdef = FALSE;
1526 *skip = TRUE;
1527 }
54ac0771
L
1528
1529 /* Merge st_other. If the symbol already has a dynamic index,
1530 but visibility says it should not be visible, turn it into a
1531 local symbol. */
b8417128 1532 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1533 if (h->dynindx != -1)
1534 switch (ELF_ST_VISIBILITY (h->other))
1535 {
1536 case STV_INTERNAL:
1537 case STV_HIDDEN:
1538 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1539 break;
1540 }
1541 }
c5e2cead 1542
45d6a902
AM
1543 /* If the old symbol is from a dynamic object, and the new symbol is
1544 a definition which is not from a dynamic object, then the new
1545 symbol overrides the old symbol. Symbols from regular files
1546 always take precedence over symbols from dynamic objects, even if
1547 they are defined after the dynamic object in the link.
1548
1549 As above, we again permit a common symbol in a regular object to
1550 override a definition in a shared object if the shared object
0f8a2703 1551 symbol is a function or is weak. */
45d6a902
AM
1552
1553 flip = NULL;
77cfaee6 1554 if (!newdyn
45d6a902
AM
1555 && (newdef
1556 || (bfd_is_com_section (sec)
0a36a439 1557 && (oldweak || oldfunc)))
45d6a902
AM
1558 && olddyn
1559 && olddef
f5385ebf 1560 && h->def_dynamic)
45d6a902
AM
1561 {
1562 /* Change the hash table entry to undefined, and let
1563 _bfd_generic_link_add_one_symbol do the right thing with the
1564 new definition. */
1565
1566 h->root.type = bfd_link_hash_undefined;
1567 h->root.u.undef.abfd = h->root.u.def.section->owner;
1568 *size_change_ok = TRUE;
1569
1570 olddef = FALSE;
1571 olddyncommon = FALSE;
1572
1573 /* We again permit a type change when a common symbol may be
1574 overriding a function. */
1575
1576 if (bfd_is_com_section (sec))
0a36a439
L
1577 {
1578 if (oldfunc)
1579 {
1580 /* If a common symbol overrides a function, make sure
1581 that it isn't defined dynamically nor has type
1582 function. */
1583 h->def_dynamic = 0;
1584 h->type = STT_NOTYPE;
1585 }
1586 *type_change_ok = TRUE;
1587 }
45d6a902 1588
6c9b78e6
AM
1589 if (hi->root.type == bfd_link_hash_indirect)
1590 flip = hi;
45d6a902
AM
1591 else
1592 /* This union may have been set to be non-NULL when this symbol
1593 was seen in a dynamic object. We must force the union to be
1594 NULL, so that it is correct for a regular symbol. */
1595 h->verinfo.vertree = NULL;
1596 }
1597
1598 /* Handle the special case of a new common symbol merging with an
1599 old symbol that looks like it might be a common symbol defined in
1600 a shared object. Note that we have already handled the case in
1601 which a new common symbol should simply override the definition
1602 in the shared library. */
1603
1604 if (! newdyn
1605 && bfd_is_com_section (sec)
1606 && olddyncommon)
1607 {
1608 /* It would be best if we could set the hash table entry to a
1609 common symbol, but we don't know what to use for the section
1610 or the alignment. */
1611 if (! ((*info->callbacks->multiple_common)
24f58f47 1612 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1613 return FALSE;
1614
4cc11e76 1615 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1616 larger, pretend that the new symbol has its size. */
1617
1618 if (h->size > *pvalue)
1619 *pvalue = h->size;
1620
af44c138
L
1621 /* We need to remember the alignment required by the symbol
1622 in the dynamic object. */
1623 BFD_ASSERT (pold_alignment);
1624 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1625
1626 olddef = FALSE;
1627 olddyncommon = FALSE;
1628
1629 h->root.type = bfd_link_hash_undefined;
1630 h->root.u.undef.abfd = h->root.u.def.section->owner;
1631
1632 *size_change_ok = TRUE;
1633 *type_change_ok = TRUE;
1634
6c9b78e6
AM
1635 if (hi->root.type == bfd_link_hash_indirect)
1636 flip = hi;
45d6a902
AM
1637 else
1638 h->verinfo.vertree = NULL;
1639 }
1640
1641 if (flip != NULL)
1642 {
1643 /* Handle the case where we had a versioned symbol in a dynamic
1644 library and now find a definition in a normal object. In this
1645 case, we make the versioned symbol point to the normal one. */
45d6a902 1646 flip->root.type = h->root.type;
00cbee0a 1647 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1648 h->root.type = bfd_link_hash_indirect;
1649 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1650 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1651 if (h->def_dynamic)
45d6a902 1652 {
f5385ebf
AM
1653 h->def_dynamic = 0;
1654 flip->ref_dynamic = 1;
45d6a902
AM
1655 }
1656 }
1657
45d6a902
AM
1658 return TRUE;
1659}
1660
1661/* This function is called to create an indirect symbol from the
1662 default for the symbol with the default version if needed. The
4f3fedcf 1663 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1664 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1665
28caa186 1666static bfd_boolean
268b6b39
AM
1667_bfd_elf_add_default_symbol (bfd *abfd,
1668 struct bfd_link_info *info,
1669 struct elf_link_hash_entry *h,
1670 const char *name,
1671 Elf_Internal_Sym *sym,
4f3fedcf
AM
1672 asection *sec,
1673 bfd_vma value,
1674 bfd **poldbfd,
e3c9d234 1675 bfd_boolean *dynsym)
45d6a902
AM
1676{
1677 bfd_boolean type_change_ok;
1678 bfd_boolean size_change_ok;
1679 bfd_boolean skip;
1680 char *shortname;
1681 struct elf_link_hash_entry *hi;
1682 struct bfd_link_hash_entry *bh;
9c5bfbb7 1683 const struct elf_backend_data *bed;
45d6a902
AM
1684 bfd_boolean collect;
1685 bfd_boolean dynamic;
e3c9d234 1686 bfd_boolean override;
45d6a902
AM
1687 char *p;
1688 size_t len, shortlen;
ffd65175 1689 asection *tmp_sec;
6e33951e 1690 bfd_boolean matched;
45d6a902 1691
422f1182
L
1692 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1693 return TRUE;
1694
45d6a902
AM
1695 /* If this symbol has a version, and it is the default version, we
1696 create an indirect symbol from the default name to the fully
1697 decorated name. This will cause external references which do not
1698 specify a version to be bound to this version of the symbol. */
1699 p = strchr (name, ELF_VER_CHR);
422f1182
L
1700 if (h->versioned == unknown)
1701 {
1702 if (p == NULL)
1703 {
1704 h->versioned = unversioned;
1705 return TRUE;
1706 }
1707 else
1708 {
1709 if (p[1] != ELF_VER_CHR)
1710 {
1711 h->versioned = versioned_hidden;
1712 return TRUE;
1713 }
1714 else
1715 h->versioned = versioned;
1716 }
1717 }
45d6a902 1718
45d6a902
AM
1719 bed = get_elf_backend_data (abfd);
1720 collect = bed->collect;
1721 dynamic = (abfd->flags & DYNAMIC) != 0;
1722
1723 shortlen = p - name;
a50b1753 1724 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1725 if (shortname == NULL)
1726 return FALSE;
1727 memcpy (shortname, name, shortlen);
1728 shortname[shortlen] = '\0';
1729
1730 /* We are going to create a new symbol. Merge it with any existing
1731 symbol with this name. For the purposes of the merge, act as
1732 though we were defining the symbol we just defined, although we
1733 actually going to define an indirect symbol. */
1734 type_change_ok = FALSE;
1735 size_change_ok = FALSE;
6e33951e 1736 matched = TRUE;
ffd65175
AM
1737 tmp_sec = sec;
1738 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1739 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1740 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1741 return FALSE;
1742
1743 if (skip)
1744 goto nondefault;
1745
1746 if (! override)
1747 {
c6e8a9a8 1748 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1749 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1750 {
1751 bh = &hi->root;
1752 if (! (_bfd_generic_link_add_one_symbol
1753 (info, abfd, shortname, BSF_INDIRECT,
1754 bfd_ind_section_ptr,
1755 0, name, FALSE, collect, &bh)))
1756 return FALSE;
1757 hi = (struct elf_link_hash_entry *) bh;
1758 }
45d6a902
AM
1759 }
1760 else
1761 {
1762 /* In this case the symbol named SHORTNAME is overriding the
1763 indirect symbol we want to add. We were planning on making
1764 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1765 is the name without a version. NAME is the fully versioned
1766 name, and it is the default version.
1767
1768 Overriding means that we already saw a definition for the
1769 symbol SHORTNAME in a regular object, and it is overriding
1770 the symbol defined in the dynamic object.
1771
1772 When this happens, we actually want to change NAME, the
1773 symbol we just added, to refer to SHORTNAME. This will cause
1774 references to NAME in the shared object to become references
1775 to SHORTNAME in the regular object. This is what we expect
1776 when we override a function in a shared object: that the
1777 references in the shared object will be mapped to the
1778 definition in the regular object. */
1779
1780 while (hi->root.type == bfd_link_hash_indirect
1781 || hi->root.type == bfd_link_hash_warning)
1782 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1783
1784 h->root.type = bfd_link_hash_indirect;
1785 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1786 if (h->def_dynamic)
45d6a902 1787 {
f5385ebf
AM
1788 h->def_dynamic = 0;
1789 hi->ref_dynamic = 1;
1790 if (hi->ref_regular
1791 || hi->def_regular)
45d6a902 1792 {
c152c796 1793 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1794 return FALSE;
1795 }
1796 }
1797
1798 /* Now set HI to H, so that the following code will set the
1799 other fields correctly. */
1800 hi = h;
1801 }
1802
fab4a87f
L
1803 /* Check if HI is a warning symbol. */
1804 if (hi->root.type == bfd_link_hash_warning)
1805 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1806
45d6a902
AM
1807 /* If there is a duplicate definition somewhere, then HI may not
1808 point to an indirect symbol. We will have reported an error to
1809 the user in that case. */
1810
1811 if (hi->root.type == bfd_link_hash_indirect)
1812 {
1813 struct elf_link_hash_entry *ht;
1814
45d6a902 1815 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1816 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1817
68c88cd4
AM
1818 /* A reference to the SHORTNAME symbol from a dynamic library
1819 will be satisfied by the versioned symbol at runtime. In
1820 effect, we have a reference to the versioned symbol. */
1821 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1822 hi->dynamic_def |= ht->dynamic_def;
1823
45d6a902
AM
1824 /* See if the new flags lead us to realize that the symbol must
1825 be dynamic. */
1826 if (! *dynsym)
1827 {
1828 if (! dynamic)
1829 {
0e1862bb 1830 if (! bfd_link_executable (info)
90c984fc 1831 || hi->def_dynamic
f5385ebf 1832 || hi->ref_dynamic)
45d6a902
AM
1833 *dynsym = TRUE;
1834 }
1835 else
1836 {
f5385ebf 1837 if (hi->ref_regular)
45d6a902
AM
1838 *dynsym = TRUE;
1839 }
1840 }
1841 }
1842
1843 /* We also need to define an indirection from the nondefault version
1844 of the symbol. */
1845
1846nondefault:
1847 len = strlen (name);
a50b1753 1848 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1849 if (shortname == NULL)
1850 return FALSE;
1851 memcpy (shortname, name, shortlen);
1852 memcpy (shortname + shortlen, p + 1, len - shortlen);
1853
1854 /* Once again, merge with any existing symbol. */
1855 type_change_ok = FALSE;
1856 size_change_ok = FALSE;
ffd65175
AM
1857 tmp_sec = sec;
1858 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1859 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1860 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1861 return FALSE;
1862
1863 if (skip)
1864 return TRUE;
1865
1866 if (override)
1867 {
1868 /* Here SHORTNAME is a versioned name, so we don't expect to see
1869 the type of override we do in the case above unless it is
4cc11e76 1870 overridden by a versioned definition. */
45d6a902
AM
1871 if (hi->root.type != bfd_link_hash_defined
1872 && hi->root.type != bfd_link_hash_defweak)
1873 (*_bfd_error_handler)
d003868e
AM
1874 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1875 abfd, shortname);
45d6a902
AM
1876 }
1877 else
1878 {
1879 bh = &hi->root;
1880 if (! (_bfd_generic_link_add_one_symbol
1881 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1882 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1883 return FALSE;
1884 hi = (struct elf_link_hash_entry *) bh;
1885
1886 /* If there is a duplicate definition somewhere, then HI may not
1887 point to an indirect symbol. We will have reported an error
1888 to the user in that case. */
1889
1890 if (hi->root.type == bfd_link_hash_indirect)
1891 {
fcfa13d2 1892 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1893 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1894 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1895
1896 /* See if the new flags lead us to realize that the symbol
1897 must be dynamic. */
1898 if (! *dynsym)
1899 {
1900 if (! dynamic)
1901 {
0e1862bb 1902 if (! bfd_link_executable (info)
f5385ebf 1903 || hi->ref_dynamic)
45d6a902
AM
1904 *dynsym = TRUE;
1905 }
1906 else
1907 {
f5385ebf 1908 if (hi->ref_regular)
45d6a902
AM
1909 *dynsym = TRUE;
1910 }
1911 }
1912 }
1913 }
1914
1915 return TRUE;
1916}
1917\f
1918/* This routine is used to export all defined symbols into the dynamic
1919 symbol table. It is called via elf_link_hash_traverse. */
1920
28caa186 1921static bfd_boolean
268b6b39 1922_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1923{
a50b1753 1924 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1925
1926 /* Ignore indirect symbols. These are added by the versioning code. */
1927 if (h->root.type == bfd_link_hash_indirect)
1928 return TRUE;
1929
7686d77d
AM
1930 /* Ignore this if we won't export it. */
1931 if (!eif->info->export_dynamic && !h->dynamic)
1932 return TRUE;
45d6a902
AM
1933
1934 if (h->dynindx == -1
fd91d419
L
1935 && (h->def_regular || h->ref_regular)
1936 && ! bfd_hide_sym_by_version (eif->info->version_info,
1937 h->root.root.string))
45d6a902 1938 {
fd91d419 1939 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1940 {
fd91d419
L
1941 eif->failed = TRUE;
1942 return FALSE;
45d6a902
AM
1943 }
1944 }
1945
1946 return TRUE;
1947}
1948\f
1949/* Look through the symbols which are defined in other shared
1950 libraries and referenced here. Update the list of version
1951 dependencies. This will be put into the .gnu.version_r section.
1952 This function is called via elf_link_hash_traverse. */
1953
28caa186 1954static bfd_boolean
268b6b39
AM
1955_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1956 void *data)
45d6a902 1957{
a50b1753 1958 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1959 Elf_Internal_Verneed *t;
1960 Elf_Internal_Vernaux *a;
1961 bfd_size_type amt;
1962
45d6a902
AM
1963 /* We only care about symbols defined in shared objects with version
1964 information. */
f5385ebf
AM
1965 if (!h->def_dynamic
1966 || h->def_regular
45d6a902 1967 || h->dynindx == -1
7b20f099
AM
1968 || h->verinfo.verdef == NULL
1969 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1970 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
1971 return TRUE;
1972
1973 /* See if we already know about this version. */
28caa186
AM
1974 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1975 t != NULL;
1976 t = t->vn_nextref)
45d6a902
AM
1977 {
1978 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1979 continue;
1980
1981 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1982 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1983 return TRUE;
1984
1985 break;
1986 }
1987
1988 /* This is a new version. Add it to tree we are building. */
1989
1990 if (t == NULL)
1991 {
1992 amt = sizeof *t;
a50b1753 1993 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
1994 if (t == NULL)
1995 {
1996 rinfo->failed = TRUE;
1997 return FALSE;
1998 }
1999
2000 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2001 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2002 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2003 }
2004
2005 amt = sizeof *a;
a50b1753 2006 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2007 if (a == NULL)
2008 {
2009 rinfo->failed = TRUE;
2010 return FALSE;
2011 }
45d6a902
AM
2012
2013 /* Note that we are copying a string pointer here, and testing it
2014 above. If bfd_elf_string_from_elf_section is ever changed to
2015 discard the string data when low in memory, this will have to be
2016 fixed. */
2017 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2018
2019 a->vna_flags = h->verinfo.verdef->vd_flags;
2020 a->vna_nextptr = t->vn_auxptr;
2021
2022 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2023 ++rinfo->vers;
2024
2025 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2026
2027 t->vn_auxptr = a;
2028
2029 return TRUE;
2030}
2031
2032/* Figure out appropriate versions for all the symbols. We may not
2033 have the version number script until we have read all of the input
2034 files, so until that point we don't know which symbols should be
2035 local. This function is called via elf_link_hash_traverse. */
2036
28caa186 2037static bfd_boolean
268b6b39 2038_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2039{
28caa186 2040 struct elf_info_failed *sinfo;
45d6a902 2041 struct bfd_link_info *info;
9c5bfbb7 2042 const struct elf_backend_data *bed;
45d6a902
AM
2043 struct elf_info_failed eif;
2044 char *p;
2045 bfd_size_type amt;
2046
a50b1753 2047 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2048 info = sinfo->info;
2049
45d6a902
AM
2050 /* Fix the symbol flags. */
2051 eif.failed = FALSE;
2052 eif.info = info;
2053 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2054 {
2055 if (eif.failed)
2056 sinfo->failed = TRUE;
2057 return FALSE;
2058 }
2059
2060 /* We only need version numbers for symbols defined in regular
2061 objects. */
f5385ebf 2062 if (!h->def_regular)
45d6a902
AM
2063 return TRUE;
2064
28caa186 2065 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2066 p = strchr (h->root.root.string, ELF_VER_CHR);
2067 if (p != NULL && h->verinfo.vertree == NULL)
2068 {
2069 struct bfd_elf_version_tree *t;
45d6a902 2070
45d6a902
AM
2071 ++p;
2072 if (*p == ELF_VER_CHR)
6e33951e 2073 ++p;
45d6a902
AM
2074
2075 /* If there is no version string, we can just return out. */
2076 if (*p == '\0')
6e33951e 2077 return TRUE;
45d6a902
AM
2078
2079 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2080 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2081 {
2082 if (strcmp (t->name, p) == 0)
2083 {
2084 size_t len;
2085 char *alc;
2086 struct bfd_elf_version_expr *d;
2087
2088 len = p - h->root.root.string;
a50b1753 2089 alc = (char *) bfd_malloc (len);
45d6a902 2090 if (alc == NULL)
14b1c01e
AM
2091 {
2092 sinfo->failed = TRUE;
2093 return FALSE;
2094 }
45d6a902
AM
2095 memcpy (alc, h->root.root.string, len - 1);
2096 alc[len - 1] = '\0';
2097 if (alc[len - 2] == ELF_VER_CHR)
2098 alc[len - 2] = '\0';
2099
2100 h->verinfo.vertree = t;
2101 t->used = TRUE;
2102 d = NULL;
2103
108ba305
JJ
2104 if (t->globals.list != NULL)
2105 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2106
2107 /* See if there is anything to force this symbol to
2108 local scope. */
108ba305 2109 if (d == NULL && t->locals.list != NULL)
45d6a902 2110 {
108ba305
JJ
2111 d = (*t->match) (&t->locals, NULL, alc);
2112 if (d != NULL
2113 && h->dynindx != -1
108ba305
JJ
2114 && ! info->export_dynamic)
2115 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2116 }
2117
2118 free (alc);
2119 break;
2120 }
2121 }
2122
2123 /* If we are building an application, we need to create a
2124 version node for this version. */
0e1862bb 2125 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2126 {
2127 struct bfd_elf_version_tree **pp;
2128 int version_index;
2129
2130 /* If we aren't going to export this symbol, we don't need
2131 to worry about it. */
2132 if (h->dynindx == -1)
2133 return TRUE;
2134
2135 amt = sizeof *t;
a50b1753 2136 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2137 if (t == NULL)
2138 {
2139 sinfo->failed = TRUE;
2140 return FALSE;
2141 }
2142
45d6a902 2143 t->name = p;
45d6a902
AM
2144 t->name_indx = (unsigned int) -1;
2145 t->used = TRUE;
2146
2147 version_index = 1;
2148 /* Don't count anonymous version tag. */
fd91d419
L
2149 if (sinfo->info->version_info != NULL
2150 && sinfo->info->version_info->vernum == 0)
45d6a902 2151 version_index = 0;
fd91d419
L
2152 for (pp = &sinfo->info->version_info;
2153 *pp != NULL;
2154 pp = &(*pp)->next)
45d6a902
AM
2155 ++version_index;
2156 t->vernum = version_index;
2157
2158 *pp = t;
2159
2160 h->verinfo.vertree = t;
2161 }
2162 else if (t == NULL)
2163 {
2164 /* We could not find the version for a symbol when
2165 generating a shared archive. Return an error. */
2166 (*_bfd_error_handler)
c55fe096 2167 (_("%B: version node not found for symbol %s"),
28caa186 2168 info->output_bfd, h->root.root.string);
45d6a902
AM
2169 bfd_set_error (bfd_error_bad_value);
2170 sinfo->failed = TRUE;
2171 return FALSE;
2172 }
45d6a902
AM
2173 }
2174
2175 /* If we don't have a version for this symbol, see if we can find
2176 something. */
fd91d419 2177 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2178 {
1e8fa21e 2179 bfd_boolean hide;
ae5a3597 2180
fd91d419
L
2181 h->verinfo.vertree
2182 = bfd_find_version_for_sym (sinfo->info->version_info,
2183 h->root.root.string, &hide);
1e8fa21e
AM
2184 if (h->verinfo.vertree != NULL && hide)
2185 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2186 }
2187
2188 return TRUE;
2189}
2190\f
45d6a902
AM
2191/* Read and swap the relocs from the section indicated by SHDR. This
2192 may be either a REL or a RELA section. The relocations are
2193 translated into RELA relocations and stored in INTERNAL_RELOCS,
2194 which should have already been allocated to contain enough space.
2195 The EXTERNAL_RELOCS are a buffer where the external form of the
2196 relocations should be stored.
2197
2198 Returns FALSE if something goes wrong. */
2199
2200static bfd_boolean
268b6b39 2201elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2202 asection *sec,
268b6b39
AM
2203 Elf_Internal_Shdr *shdr,
2204 void *external_relocs,
2205 Elf_Internal_Rela *internal_relocs)
45d6a902 2206{
9c5bfbb7 2207 const struct elf_backend_data *bed;
268b6b39 2208 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2209 const bfd_byte *erela;
2210 const bfd_byte *erelaend;
2211 Elf_Internal_Rela *irela;
243ef1e0
L
2212 Elf_Internal_Shdr *symtab_hdr;
2213 size_t nsyms;
45d6a902 2214
45d6a902
AM
2215 /* Position ourselves at the start of the section. */
2216 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2217 return FALSE;
2218
2219 /* Read the relocations. */
2220 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2221 return FALSE;
2222
243ef1e0 2223 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2224 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2225
45d6a902
AM
2226 bed = get_elf_backend_data (abfd);
2227
2228 /* Convert the external relocations to the internal format. */
2229 if (shdr->sh_entsize == bed->s->sizeof_rel)
2230 swap_in = bed->s->swap_reloc_in;
2231 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2232 swap_in = bed->s->swap_reloca_in;
2233 else
2234 {
2235 bfd_set_error (bfd_error_wrong_format);
2236 return FALSE;
2237 }
2238
a50b1753 2239 erela = (const bfd_byte *) external_relocs;
51992aec 2240 erelaend = erela + shdr->sh_size;
45d6a902
AM
2241 irela = internal_relocs;
2242 while (erela < erelaend)
2243 {
243ef1e0
L
2244 bfd_vma r_symndx;
2245
45d6a902 2246 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2247 r_symndx = ELF32_R_SYM (irela->r_info);
2248 if (bed->s->arch_size == 64)
2249 r_symndx >>= 24;
ce98a316
NC
2250 if (nsyms > 0)
2251 {
2252 if ((size_t) r_symndx >= nsyms)
2253 {
2254 (*_bfd_error_handler)
2255 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2256 " for offset 0x%lx in section `%A'"),
2257 abfd, sec,
2258 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2259 bfd_set_error (bfd_error_bad_value);
2260 return FALSE;
2261 }
2262 }
cf35638d 2263 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2264 {
2265 (*_bfd_error_handler)
ce98a316
NC
2266 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2267 " when the object file has no symbol table"),
d003868e
AM
2268 abfd, sec,
2269 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2270 bfd_set_error (bfd_error_bad_value);
2271 return FALSE;
2272 }
45d6a902
AM
2273 irela += bed->s->int_rels_per_ext_rel;
2274 erela += shdr->sh_entsize;
2275 }
2276
2277 return TRUE;
2278}
2279
2280/* Read and swap the relocs for a section O. They may have been
2281 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2282 not NULL, they are used as buffers to read into. They are known to
2283 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2284 the return value is allocated using either malloc or bfd_alloc,
2285 according to the KEEP_MEMORY argument. If O has two relocation
2286 sections (both REL and RELA relocations), then the REL_HDR
2287 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2288 RELA_HDR relocations. */
45d6a902
AM
2289
2290Elf_Internal_Rela *
268b6b39
AM
2291_bfd_elf_link_read_relocs (bfd *abfd,
2292 asection *o,
2293 void *external_relocs,
2294 Elf_Internal_Rela *internal_relocs,
2295 bfd_boolean keep_memory)
45d6a902 2296{
268b6b39 2297 void *alloc1 = NULL;
45d6a902 2298 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2299 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2300 struct bfd_elf_section_data *esdo = elf_section_data (o);
2301 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2302
d4730f92
BS
2303 if (esdo->relocs != NULL)
2304 return esdo->relocs;
45d6a902
AM
2305
2306 if (o->reloc_count == 0)
2307 return NULL;
2308
45d6a902
AM
2309 if (internal_relocs == NULL)
2310 {
2311 bfd_size_type size;
2312
2313 size = o->reloc_count;
2314 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2315 if (keep_memory)
a50b1753 2316 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2317 else
a50b1753 2318 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2319 if (internal_relocs == NULL)
2320 goto error_return;
2321 }
2322
2323 if (external_relocs == NULL)
2324 {
d4730f92
BS
2325 bfd_size_type size = 0;
2326
2327 if (esdo->rel.hdr)
2328 size += esdo->rel.hdr->sh_size;
2329 if (esdo->rela.hdr)
2330 size += esdo->rela.hdr->sh_size;
45d6a902 2331
268b6b39 2332 alloc1 = bfd_malloc (size);
45d6a902
AM
2333 if (alloc1 == NULL)
2334 goto error_return;
2335 external_relocs = alloc1;
2336 }
2337
d4730f92
BS
2338 internal_rela_relocs = internal_relocs;
2339 if (esdo->rel.hdr)
2340 {
2341 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2342 external_relocs,
2343 internal_relocs))
2344 goto error_return;
2345 external_relocs = (((bfd_byte *) external_relocs)
2346 + esdo->rel.hdr->sh_size);
2347 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2348 * bed->s->int_rels_per_ext_rel);
2349 }
2350
2351 if (esdo->rela.hdr
2352 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2353 external_relocs,
2354 internal_rela_relocs)))
45d6a902
AM
2355 goto error_return;
2356
2357 /* Cache the results for next time, if we can. */
2358 if (keep_memory)
d4730f92 2359 esdo->relocs = internal_relocs;
45d6a902
AM
2360
2361 if (alloc1 != NULL)
2362 free (alloc1);
2363
2364 /* Don't free alloc2, since if it was allocated we are passing it
2365 back (under the name of internal_relocs). */
2366
2367 return internal_relocs;
2368
2369 error_return:
2370 if (alloc1 != NULL)
2371 free (alloc1);
2372 if (alloc2 != NULL)
4dd07732
AM
2373 {
2374 if (keep_memory)
2375 bfd_release (abfd, alloc2);
2376 else
2377 free (alloc2);
2378 }
45d6a902
AM
2379 return NULL;
2380}
2381
2382/* Compute the size of, and allocate space for, REL_HDR which is the
2383 section header for a section containing relocations for O. */
2384
28caa186 2385static bfd_boolean
268b6b39 2386_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2387 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2388{
d4730f92 2389 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2390
2391 /* That allows us to calculate the size of the section. */
d4730f92 2392 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2393
2394 /* The contents field must last into write_object_contents, so we
2395 allocate it with bfd_alloc rather than malloc. Also since we
2396 cannot be sure that the contents will actually be filled in,
2397 we zero the allocated space. */
a50b1753 2398 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2399 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2400 return FALSE;
2401
d4730f92 2402 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2403 {
2404 struct elf_link_hash_entry **p;
2405
ca4be51c
AM
2406 p = ((struct elf_link_hash_entry **)
2407 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2408 if (p == NULL)
2409 return FALSE;
2410
d4730f92 2411 reldata->hashes = p;
45d6a902
AM
2412 }
2413
2414 return TRUE;
2415}
2416
2417/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2418 originated from the section given by INPUT_REL_HDR) to the
2419 OUTPUT_BFD. */
2420
2421bfd_boolean
268b6b39
AM
2422_bfd_elf_link_output_relocs (bfd *output_bfd,
2423 asection *input_section,
2424 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2425 Elf_Internal_Rela *internal_relocs,
2426 struct elf_link_hash_entry **rel_hash
2427 ATTRIBUTE_UNUSED)
45d6a902
AM
2428{
2429 Elf_Internal_Rela *irela;
2430 Elf_Internal_Rela *irelaend;
2431 bfd_byte *erel;
d4730f92 2432 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2433 asection *output_section;
9c5bfbb7 2434 const struct elf_backend_data *bed;
268b6b39 2435 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2436 struct bfd_elf_section_data *esdo;
45d6a902
AM
2437
2438 output_section = input_section->output_section;
45d6a902 2439
d4730f92
BS
2440 bed = get_elf_backend_data (output_bfd);
2441 esdo = elf_section_data (output_section);
2442 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2443 {
d4730f92
BS
2444 output_reldata = &esdo->rel;
2445 swap_out = bed->s->swap_reloc_out;
45d6a902 2446 }
d4730f92
BS
2447 else if (esdo->rela.hdr
2448 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2449 {
d4730f92
BS
2450 output_reldata = &esdo->rela;
2451 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2452 }
2453 else
2454 {
2455 (*_bfd_error_handler)
d003868e
AM
2456 (_("%B: relocation size mismatch in %B section %A"),
2457 output_bfd, input_section->owner, input_section);
297d8443 2458 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2459 return FALSE;
2460 }
2461
d4730f92
BS
2462 erel = output_reldata->hdr->contents;
2463 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2464 irela = internal_relocs;
2465 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2466 * bed->s->int_rels_per_ext_rel);
2467 while (irela < irelaend)
2468 {
2469 (*swap_out) (output_bfd, irela, erel);
2470 irela += bed->s->int_rels_per_ext_rel;
2471 erel += input_rel_hdr->sh_entsize;
2472 }
2473
2474 /* Bump the counter, so that we know where to add the next set of
2475 relocations. */
d4730f92 2476 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2477
2478 return TRUE;
2479}
2480\f
508c3946
L
2481/* Make weak undefined symbols in PIE dynamic. */
2482
2483bfd_boolean
2484_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2485 struct elf_link_hash_entry *h)
2486{
0e1862bb 2487 if (bfd_link_pie (info)
508c3946
L
2488 && h->dynindx == -1
2489 && h->root.type == bfd_link_hash_undefweak)
2490 return bfd_elf_link_record_dynamic_symbol (info, h);
2491
2492 return TRUE;
2493}
2494
45d6a902
AM
2495/* Fix up the flags for a symbol. This handles various cases which
2496 can only be fixed after all the input files are seen. This is
2497 currently called by both adjust_dynamic_symbol and
2498 assign_sym_version, which is unnecessary but perhaps more robust in
2499 the face of future changes. */
2500
28caa186 2501static bfd_boolean
268b6b39
AM
2502_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2503 struct elf_info_failed *eif)
45d6a902 2504{
33774f08 2505 const struct elf_backend_data *bed;
508c3946 2506
45d6a902
AM
2507 /* If this symbol was mentioned in a non-ELF file, try to set
2508 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2509 permit a non-ELF file to correctly refer to a symbol defined in
2510 an ELF dynamic object. */
f5385ebf 2511 if (h->non_elf)
45d6a902
AM
2512 {
2513 while (h->root.type == bfd_link_hash_indirect)
2514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2515
2516 if (h->root.type != bfd_link_hash_defined
2517 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2518 {
2519 h->ref_regular = 1;
2520 h->ref_regular_nonweak = 1;
2521 }
45d6a902
AM
2522 else
2523 {
2524 if (h->root.u.def.section->owner != NULL
2525 && (bfd_get_flavour (h->root.u.def.section->owner)
2526 == bfd_target_elf_flavour))
f5385ebf
AM
2527 {
2528 h->ref_regular = 1;
2529 h->ref_regular_nonweak = 1;
2530 }
45d6a902 2531 else
f5385ebf 2532 h->def_regular = 1;
45d6a902
AM
2533 }
2534
2535 if (h->dynindx == -1
f5385ebf
AM
2536 && (h->def_dynamic
2537 || h->ref_dynamic))
45d6a902 2538 {
c152c796 2539 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2540 {
2541 eif->failed = TRUE;
2542 return FALSE;
2543 }
2544 }
2545 }
2546 else
2547 {
f5385ebf 2548 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2549 was first seen in a non-ELF file. Fortunately, if the symbol
2550 was first seen in an ELF file, we're probably OK unless the
2551 symbol was defined in a non-ELF file. Catch that case here.
2552 FIXME: We're still in trouble if the symbol was first seen in
2553 a dynamic object, and then later in a non-ELF regular object. */
2554 if ((h->root.type == bfd_link_hash_defined
2555 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2556 && !h->def_regular
45d6a902
AM
2557 && (h->root.u.def.section->owner != NULL
2558 ? (bfd_get_flavour (h->root.u.def.section->owner)
2559 != bfd_target_elf_flavour)
2560 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2561 && !h->def_dynamic)))
2562 h->def_regular = 1;
45d6a902
AM
2563 }
2564
508c3946 2565 /* Backend specific symbol fixup. */
33774f08
AM
2566 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2567 if (bed->elf_backend_fixup_symbol
2568 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2569 return FALSE;
508c3946 2570
45d6a902
AM
2571 /* If this is a final link, and the symbol was defined as a common
2572 symbol in a regular object file, and there was no definition in
2573 any dynamic object, then the linker will have allocated space for
f5385ebf 2574 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2575 flag will not have been set. */
2576 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2577 && !h->def_regular
2578 && h->ref_regular
2579 && !h->def_dynamic
96f29d96 2580 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2581 h->def_regular = 1;
45d6a902
AM
2582
2583 /* If -Bsymbolic was used (which means to bind references to global
2584 symbols to the definition within the shared object), and this
2585 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2586 need a PLT entry. Likewise, if the symbol has non-default
2587 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2588 will force it local. */
f5385ebf 2589 if (h->needs_plt
0e1862bb 2590 && bfd_link_pic (eif->info)
0eddce27 2591 && is_elf_hash_table (eif->info->hash)
55255dae 2592 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2593 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2594 && h->def_regular)
45d6a902 2595 {
45d6a902
AM
2596 bfd_boolean force_local;
2597
45d6a902
AM
2598 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2599 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2600 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2601 }
2602
2603 /* If a weak undefined symbol has non-default visibility, we also
2604 hide it from the dynamic linker. */
9c7a29a3 2605 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2606 && h->root.type == bfd_link_hash_undefweak)
33774f08 2607 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2608
2609 /* If this is a weak defined symbol in a dynamic object, and we know
2610 the real definition in the dynamic object, copy interesting flags
2611 over to the real definition. */
f6e332e6 2612 if (h->u.weakdef != NULL)
45d6a902 2613 {
45d6a902
AM
2614 /* If the real definition is defined by a regular object file,
2615 don't do anything special. See the longer description in
2616 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2617 if (h->u.weakdef->def_regular)
f6e332e6 2618 h->u.weakdef = NULL;
45d6a902 2619 else
a26587ba 2620 {
4e6b54a6
AM
2621 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2622
2623 while (h->root.type == bfd_link_hash_indirect)
2624 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2625
2626 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2627 || h->root.type == bfd_link_hash_defweak);
2628 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2629 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2630 || weakdef->root.type == bfd_link_hash_defweak);
2631 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2632 }
45d6a902
AM
2633 }
2634
2635 return TRUE;
2636}
2637
2638/* Make the backend pick a good value for a dynamic symbol. This is
2639 called via elf_link_hash_traverse, and also calls itself
2640 recursively. */
2641
28caa186 2642static bfd_boolean
268b6b39 2643_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2644{
a50b1753 2645 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2646 bfd *dynobj;
9c5bfbb7 2647 const struct elf_backend_data *bed;
45d6a902 2648
0eddce27 2649 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2650 return FALSE;
2651
45d6a902
AM
2652 /* Ignore indirect symbols. These are added by the versioning code. */
2653 if (h->root.type == bfd_link_hash_indirect)
2654 return TRUE;
2655
2656 /* Fix the symbol flags. */
2657 if (! _bfd_elf_fix_symbol_flags (h, eif))
2658 return FALSE;
2659
2660 /* If this symbol does not require a PLT entry, and it is not
2661 defined by a dynamic object, or is not referenced by a regular
2662 object, ignore it. We do have to handle a weak defined symbol,
2663 even if no regular object refers to it, if we decided to add it
2664 to the dynamic symbol table. FIXME: Do we normally need to worry
2665 about symbols which are defined by one dynamic object and
2666 referenced by another one? */
f5385ebf 2667 if (!h->needs_plt
91e21fb7 2668 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2669 && (h->def_regular
2670 || !h->def_dynamic
2671 || (!h->ref_regular
f6e332e6 2672 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2673 {
a6aa5195 2674 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2675 return TRUE;
2676 }
2677
2678 /* If we've already adjusted this symbol, don't do it again. This
2679 can happen via a recursive call. */
f5385ebf 2680 if (h->dynamic_adjusted)
45d6a902
AM
2681 return TRUE;
2682
2683 /* Don't look at this symbol again. Note that we must set this
2684 after checking the above conditions, because we may look at a
2685 symbol once, decide not to do anything, and then get called
2686 recursively later after REF_REGULAR is set below. */
f5385ebf 2687 h->dynamic_adjusted = 1;
45d6a902
AM
2688
2689 /* If this is a weak definition, and we know a real definition, and
2690 the real symbol is not itself defined by a regular object file,
2691 then get a good value for the real definition. We handle the
2692 real symbol first, for the convenience of the backend routine.
2693
2694 Note that there is a confusing case here. If the real definition
2695 is defined by a regular object file, we don't get the real symbol
2696 from the dynamic object, but we do get the weak symbol. If the
2697 processor backend uses a COPY reloc, then if some routine in the
2698 dynamic object changes the real symbol, we will not see that
2699 change in the corresponding weak symbol. This is the way other
2700 ELF linkers work as well, and seems to be a result of the shared
2701 library model.
2702
2703 I will clarify this issue. Most SVR4 shared libraries define the
2704 variable _timezone and define timezone as a weak synonym. The
2705 tzset call changes _timezone. If you write
2706 extern int timezone;
2707 int _timezone = 5;
2708 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2709 you might expect that, since timezone is a synonym for _timezone,
2710 the same number will print both times. However, if the processor
2711 backend uses a COPY reloc, then actually timezone will be copied
2712 into your process image, and, since you define _timezone
2713 yourself, _timezone will not. Thus timezone and _timezone will
2714 wind up at different memory locations. The tzset call will set
2715 _timezone, leaving timezone unchanged. */
2716
f6e332e6 2717 if (h->u.weakdef != NULL)
45d6a902 2718 {
ec24dc88
AM
2719 /* If we get to this point, there is an implicit reference to
2720 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2721 h->u.weakdef->ref_regular = 1;
45d6a902 2722
ec24dc88
AM
2723 /* Ensure that the backend adjust_dynamic_symbol function sees
2724 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2725 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2726 return FALSE;
2727 }
2728
2729 /* If a symbol has no type and no size and does not require a PLT
2730 entry, then we are probably about to do the wrong thing here: we
2731 are probably going to create a COPY reloc for an empty object.
2732 This case can arise when a shared object is built with assembly
2733 code, and the assembly code fails to set the symbol type. */
2734 if (h->size == 0
2735 && h->type == STT_NOTYPE
f5385ebf 2736 && !h->needs_plt)
45d6a902
AM
2737 (*_bfd_error_handler)
2738 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2739 h->root.root.string);
2740
2741 dynobj = elf_hash_table (eif->info)->dynobj;
2742 bed = get_elf_backend_data (dynobj);
e7c33416 2743
45d6a902
AM
2744 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2745 {
2746 eif->failed = TRUE;
2747 return FALSE;
2748 }
2749
2750 return TRUE;
2751}
2752
027297b7
L
2753/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2754 DYNBSS. */
2755
2756bfd_boolean
6cabe1ea
AM
2757_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2758 struct elf_link_hash_entry *h,
027297b7
L
2759 asection *dynbss)
2760{
91ac5911 2761 unsigned int power_of_two;
027297b7
L
2762 bfd_vma mask;
2763 asection *sec = h->root.u.def.section;
2764
2765 /* The section aligment of definition is the maximum alignment
91ac5911
L
2766 requirement of symbols defined in the section. Since we don't
2767 know the symbol alignment requirement, we start with the
2768 maximum alignment and check low bits of the symbol address
2769 for the minimum alignment. */
2770 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2771 mask = ((bfd_vma) 1 << power_of_two) - 1;
2772 while ((h->root.u.def.value & mask) != 0)
2773 {
2774 mask >>= 1;
2775 --power_of_two;
2776 }
027297b7 2777
91ac5911
L
2778 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2779 dynbss))
027297b7
L
2780 {
2781 /* Adjust the section alignment if needed. */
2782 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2783 power_of_two))
027297b7
L
2784 return FALSE;
2785 }
2786
91ac5911 2787 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2788 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2789
2790 /* Define the symbol as being at this point in DYNBSS. */
2791 h->root.u.def.section = dynbss;
2792 h->root.u.def.value = dynbss->size;
2793
2794 /* Increment the size of DYNBSS to make room for the symbol. */
2795 dynbss->size += h->size;
2796
f7483970
L
2797 /* No error if extern_protected_data is true. */
2798 if (h->protected_def
889c2a67
L
2799 && (!info->extern_protected_data
2800 || (info->extern_protected_data < 0
2801 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2802 info->callbacks->einfo
2803 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2804 h->root.root.string);
6cabe1ea 2805
027297b7
L
2806 return TRUE;
2807}
2808
45d6a902
AM
2809/* Adjust all external symbols pointing into SEC_MERGE sections
2810 to reflect the object merging within the sections. */
2811
28caa186 2812static bfd_boolean
268b6b39 2813_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2814{
2815 asection *sec;
2816
45d6a902
AM
2817 if ((h->root.type == bfd_link_hash_defined
2818 || h->root.type == bfd_link_hash_defweak)
2819 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2820 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2821 {
a50b1753 2822 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2823
2824 h->root.u.def.value =
2825 _bfd_merged_section_offset (output_bfd,
2826 &h->root.u.def.section,
2827 elf_section_data (sec)->sec_info,
753731ee 2828 h->root.u.def.value);
45d6a902
AM
2829 }
2830
2831 return TRUE;
2832}
986a241f
RH
2833
2834/* Returns false if the symbol referred to by H should be considered
2835 to resolve local to the current module, and true if it should be
2836 considered to bind dynamically. */
2837
2838bfd_boolean
268b6b39
AM
2839_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2840 struct bfd_link_info *info,
89a2ee5a 2841 bfd_boolean not_local_protected)
986a241f
RH
2842{
2843 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2844 const struct elf_backend_data *bed;
2845 struct elf_link_hash_table *hash_table;
986a241f
RH
2846
2847 if (h == NULL)
2848 return FALSE;
2849
2850 while (h->root.type == bfd_link_hash_indirect
2851 || h->root.type == bfd_link_hash_warning)
2852 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2853
2854 /* If it was forced local, then clearly it's not dynamic. */
2855 if (h->dynindx == -1)
2856 return FALSE;
f5385ebf 2857 if (h->forced_local)
986a241f
RH
2858 return FALSE;
2859
2860 /* Identify the cases where name binding rules say that a
2861 visible symbol resolves locally. */
0e1862bb
L
2862 binding_stays_local_p = (bfd_link_executable (info)
2863 || SYMBOLIC_BIND (info, h));
986a241f
RH
2864
2865 switch (ELF_ST_VISIBILITY (h->other))
2866 {
2867 case STV_INTERNAL:
2868 case STV_HIDDEN:
2869 return FALSE;
2870
2871 case STV_PROTECTED:
fcb93ecf
PB
2872 hash_table = elf_hash_table (info);
2873 if (!is_elf_hash_table (hash_table))
2874 return FALSE;
2875
2876 bed = get_elf_backend_data (hash_table->dynobj);
2877
986a241f
RH
2878 /* Proper resolution for function pointer equality may require
2879 that these symbols perhaps be resolved dynamically, even though
2880 we should be resolving them to the current module. */
89a2ee5a 2881 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2882 binding_stays_local_p = TRUE;
2883 break;
2884
2885 default:
986a241f
RH
2886 break;
2887 }
2888
aa37626c 2889 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2890 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2891 return TRUE;
2892
986a241f
RH
2893 /* Otherwise, the symbol is dynamic if binding rules don't tell
2894 us that it remains local. */
2895 return !binding_stays_local_p;
2896}
f6c52c13
AM
2897
2898/* Return true if the symbol referred to by H should be considered
2899 to resolve local to the current module, and false otherwise. Differs
2900 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2901 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2902 for the place where forced_local and dynindx == -1 are tested. If
2903 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2904 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2905 the symbol is local only for defined symbols.
2906 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2907 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2908 treatment of undefined weak symbols. For those that do not make
2909 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2910
2911bfd_boolean
268b6b39
AM
2912_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2913 struct bfd_link_info *info,
2914 bfd_boolean local_protected)
f6c52c13 2915{
fcb93ecf
PB
2916 const struct elf_backend_data *bed;
2917 struct elf_link_hash_table *hash_table;
2918
f6c52c13
AM
2919 /* If it's a local sym, of course we resolve locally. */
2920 if (h == NULL)
2921 return TRUE;
2922
d95edcac
L
2923 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2924 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2925 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2926 return TRUE;
2927
7e2294f9
AO
2928 /* Common symbols that become definitions don't get the DEF_REGULAR
2929 flag set, so test it first, and don't bail out. */
2930 if (ELF_COMMON_DEF_P (h))
2931 /* Do nothing. */;
f6c52c13 2932 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2933 resolve locally. The sym is either undefined or dynamic. */
2934 else if (!h->def_regular)
f6c52c13
AM
2935 return FALSE;
2936
2937 /* Forced local symbols resolve locally. */
f5385ebf 2938 if (h->forced_local)
f6c52c13
AM
2939 return TRUE;
2940
2941 /* As do non-dynamic symbols. */
2942 if (h->dynindx == -1)
2943 return TRUE;
2944
2945 /* At this point, we know the symbol is defined and dynamic. In an
2946 executable it must resolve locally, likewise when building symbolic
2947 shared libraries. */
0e1862bb 2948 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2949 return TRUE;
2950
2951 /* Now deal with defined dynamic symbols in shared libraries. Ones
2952 with default visibility might not resolve locally. */
2953 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2954 return FALSE;
2955
fcb93ecf
PB
2956 hash_table = elf_hash_table (info);
2957 if (!is_elf_hash_table (hash_table))
2958 return TRUE;
2959
2960 bed = get_elf_backend_data (hash_table->dynobj);
2961
f7483970
L
2962 /* If extern_protected_data is false, STV_PROTECTED non-function
2963 symbols are local. */
889c2a67
L
2964 if ((!info->extern_protected_data
2965 || (info->extern_protected_data < 0
2966 && !bed->extern_protected_data))
2967 && !bed->is_function_type (h->type))
1c16dfa5
L
2968 return TRUE;
2969
f6c52c13 2970 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2971 symbols be treated as dynamic symbols. If the address of a
2972 function not defined in an executable is set to that function's
2973 plt entry in the executable, then the address of the function in
2974 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2975 return local_protected;
2976}
e1918d23
AM
2977
2978/* Caches some TLS segment info, and ensures that the TLS segment vma is
2979 aligned. Returns the first TLS output section. */
2980
2981struct bfd_section *
2982_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2983{
2984 struct bfd_section *sec, *tls;
2985 unsigned int align = 0;
2986
2987 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2988 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2989 break;
2990 tls = sec;
2991
2992 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2993 if (sec->alignment_power > align)
2994 align = sec->alignment_power;
2995
2996 elf_hash_table (info)->tls_sec = tls;
2997
2998 /* Ensure the alignment of the first section is the largest alignment,
2999 so that the tls segment starts aligned. */
3000 if (tls != NULL)
3001 tls->alignment_power = align;
3002
3003 return tls;
3004}
0ad989f9
L
3005
3006/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3007static bfd_boolean
3008is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3009 Elf_Internal_Sym *sym)
3010{
a4d8e49b
L
3011 const struct elf_backend_data *bed;
3012
0ad989f9
L
3013 /* Local symbols do not count, but target specific ones might. */
3014 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3015 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3016 return FALSE;
3017
fcb93ecf 3018 bed = get_elf_backend_data (abfd);
0ad989f9 3019 /* Function symbols do not count. */
fcb93ecf 3020 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3021 return FALSE;
3022
3023 /* If the section is undefined, then so is the symbol. */
3024 if (sym->st_shndx == SHN_UNDEF)
3025 return FALSE;
3026
3027 /* If the symbol is defined in the common section, then
3028 it is a common definition and so does not count. */
a4d8e49b 3029 if (bed->common_definition (sym))
0ad989f9
L
3030 return FALSE;
3031
3032 /* If the symbol is in a target specific section then we
3033 must rely upon the backend to tell us what it is. */
3034 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3035 /* FIXME - this function is not coded yet:
3036
3037 return _bfd_is_global_symbol_definition (abfd, sym);
3038
3039 Instead for now assume that the definition is not global,
3040 Even if this is wrong, at least the linker will behave
3041 in the same way that it used to do. */
3042 return FALSE;
3043
3044 return TRUE;
3045}
3046
3047/* Search the symbol table of the archive element of the archive ABFD
3048 whose archive map contains a mention of SYMDEF, and determine if
3049 the symbol is defined in this element. */
3050static bfd_boolean
3051elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3052{
3053 Elf_Internal_Shdr * hdr;
3054 bfd_size_type symcount;
3055 bfd_size_type extsymcount;
3056 bfd_size_type extsymoff;
3057 Elf_Internal_Sym *isymbuf;
3058 Elf_Internal_Sym *isym;
3059 Elf_Internal_Sym *isymend;
3060 bfd_boolean result;
3061
3062 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3063 if (abfd == NULL)
3064 return FALSE;
3065
f0bf6bfd
L
3066 /* Return FALSE if the object has been claimed by plugin. */
3067 if (abfd->plugin_format == bfd_plugin_yes)
3068 return FALSE;
3069
0ad989f9
L
3070 if (! bfd_check_format (abfd, bfd_object))
3071 return FALSE;
3072
0ad989f9
L
3073 /* Select the appropriate symbol table. */
3074 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3075 hdr = &elf_tdata (abfd)->symtab_hdr;
3076 else
3077 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3078
3079 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3080
3081 /* The sh_info field of the symtab header tells us where the
3082 external symbols start. We don't care about the local symbols. */
3083 if (elf_bad_symtab (abfd))
3084 {
3085 extsymcount = symcount;
3086 extsymoff = 0;
3087 }
3088 else
3089 {
3090 extsymcount = symcount - hdr->sh_info;
3091 extsymoff = hdr->sh_info;
3092 }
3093
3094 if (extsymcount == 0)
3095 return FALSE;
3096
3097 /* Read in the symbol table. */
3098 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3099 NULL, NULL, NULL);
3100 if (isymbuf == NULL)
3101 return FALSE;
3102
3103 /* Scan the symbol table looking for SYMDEF. */
3104 result = FALSE;
3105 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3106 {
3107 const char *name;
3108
3109 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3110 isym->st_name);
3111 if (name == NULL)
3112 break;
3113
3114 if (strcmp (name, symdef->name) == 0)
3115 {
3116 result = is_global_data_symbol_definition (abfd, isym);
3117 break;
3118 }
3119 }
3120
3121 free (isymbuf);
3122
3123 return result;
3124}
3125\f
5a580b3a
AM
3126/* Add an entry to the .dynamic table. */
3127
3128bfd_boolean
3129_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3130 bfd_vma tag,
3131 bfd_vma val)
3132{
3133 struct elf_link_hash_table *hash_table;
3134 const struct elf_backend_data *bed;
3135 asection *s;
3136 bfd_size_type newsize;
3137 bfd_byte *newcontents;
3138 Elf_Internal_Dyn dyn;
3139
3140 hash_table = elf_hash_table (info);
3141 if (! is_elf_hash_table (hash_table))
3142 return FALSE;
3143
3144 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3145 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3146 BFD_ASSERT (s != NULL);
3147
eea6121a 3148 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3149 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3150 if (newcontents == NULL)
3151 return FALSE;
3152
3153 dyn.d_tag = tag;
3154 dyn.d_un.d_val = val;
eea6121a 3155 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3156
eea6121a 3157 s->size = newsize;
5a580b3a
AM
3158 s->contents = newcontents;
3159
3160 return TRUE;
3161}
3162
3163/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3164 otherwise just check whether one already exists. Returns -1 on error,
3165 1 if a DT_NEEDED tag already exists, and 0 on success. */
3166
4ad4eba5 3167static int
7e9f0867
AM
3168elf_add_dt_needed_tag (bfd *abfd,
3169 struct bfd_link_info *info,
4ad4eba5
AM
3170 const char *soname,
3171 bfd_boolean do_it)
5a580b3a
AM
3172{
3173 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3174 bfd_size_type strindex;
3175
7e9f0867
AM
3176 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3177 return -1;
3178
5a580b3a 3179 hash_table = elf_hash_table (info);
5a580b3a
AM
3180 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3181 if (strindex == (bfd_size_type) -1)
3182 return -1;
3183
02be4619 3184 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3185 {
3186 asection *sdyn;
3187 const struct elf_backend_data *bed;
3188 bfd_byte *extdyn;
3189
3190 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3191 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3192 if (sdyn != NULL)
3193 for (extdyn = sdyn->contents;
3194 extdyn < sdyn->contents + sdyn->size;
3195 extdyn += bed->s->sizeof_dyn)
3196 {
3197 Elf_Internal_Dyn dyn;
5a580b3a 3198
7e9f0867
AM
3199 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3200 if (dyn.d_tag == DT_NEEDED
3201 && dyn.d_un.d_val == strindex)
3202 {
3203 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3204 return 1;
3205 }
3206 }
5a580b3a
AM
3207 }
3208
3209 if (do_it)
3210 {
7e9f0867
AM
3211 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3212 return -1;
3213
5a580b3a
AM
3214 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3215 return -1;
3216 }
3217 else
3218 /* We were just checking for existence of the tag. */
3219 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3220
3221 return 0;
3222}
3223
010e5ae2
AM
3224static bfd_boolean
3225on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3226{
3227 for (; needed != NULL; needed = needed->next)
1240be6b
AM
3228 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3229 && strcmp (soname, needed->name) == 0)
010e5ae2
AM
3230 return TRUE;
3231
3232 return FALSE;
3233}
3234
14160578 3235/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3236static int
3237elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3238{
3239 const struct elf_link_hash_entry *h1;
3240 const struct elf_link_hash_entry *h2;
10b7e05b 3241 bfd_signed_vma vdiff;
5a580b3a
AM
3242
3243 h1 = *(const struct elf_link_hash_entry **) arg1;
3244 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3245 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3246 if (vdiff != 0)
3247 return vdiff > 0 ? 1 : -1;
3248 else
3249 {
d3435ae8 3250 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3251 if (sdiff != 0)
3252 return sdiff > 0 ? 1 : -1;
3253 }
14160578
AM
3254 vdiff = h1->size - h2->size;
3255 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3256}
4ad4eba5 3257
5a580b3a
AM
3258/* This function is used to adjust offsets into .dynstr for
3259 dynamic symbols. This is called via elf_link_hash_traverse. */
3260
3261static bfd_boolean
3262elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3263{
a50b1753 3264 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3265
5a580b3a
AM
3266 if (h->dynindx != -1)
3267 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3268 return TRUE;
3269}
3270
3271/* Assign string offsets in .dynstr, update all structures referencing
3272 them. */
3273
4ad4eba5
AM
3274static bfd_boolean
3275elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3276{
3277 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3278 struct elf_link_local_dynamic_entry *entry;
3279 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3280 bfd *dynobj = hash_table->dynobj;
3281 asection *sdyn;
3282 bfd_size_type size;
3283 const struct elf_backend_data *bed;
3284 bfd_byte *extdyn;
3285
3286 _bfd_elf_strtab_finalize (dynstr);
3287 size = _bfd_elf_strtab_size (dynstr);
3288
3289 bed = get_elf_backend_data (dynobj);
3d4d4302 3290 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3291 BFD_ASSERT (sdyn != NULL);
3292
3293 /* Update all .dynamic entries referencing .dynstr strings. */
3294 for (extdyn = sdyn->contents;
eea6121a 3295 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3296 extdyn += bed->s->sizeof_dyn)
3297 {
3298 Elf_Internal_Dyn dyn;
3299
3300 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3301 switch (dyn.d_tag)
3302 {
3303 case DT_STRSZ:
3304 dyn.d_un.d_val = size;
3305 break;
3306 case DT_NEEDED:
3307 case DT_SONAME:
3308 case DT_RPATH:
3309 case DT_RUNPATH:
3310 case DT_FILTER:
3311 case DT_AUXILIARY:
7ee314fa
AM
3312 case DT_AUDIT:
3313 case DT_DEPAUDIT:
5a580b3a
AM
3314 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3315 break;
3316 default:
3317 continue;
3318 }
3319 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3320 }
3321
3322 /* Now update local dynamic symbols. */
3323 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3324 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3325 entry->isym.st_name);
3326
3327 /* And the rest of dynamic symbols. */
3328 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3329
3330 /* Adjust version definitions. */
3331 if (elf_tdata (output_bfd)->cverdefs)
3332 {
3333 asection *s;
3334 bfd_byte *p;
3335 bfd_size_type i;
3336 Elf_Internal_Verdef def;
3337 Elf_Internal_Verdaux defaux;
3338
3d4d4302 3339 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3340 p = s->contents;
3341 do
3342 {
3343 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3344 &def);
3345 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3346 if (def.vd_aux != sizeof (Elf_External_Verdef))
3347 continue;
5a580b3a
AM
3348 for (i = 0; i < def.vd_cnt; ++i)
3349 {
3350 _bfd_elf_swap_verdaux_in (output_bfd,
3351 (Elf_External_Verdaux *) p, &defaux);
3352 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3353 defaux.vda_name);
3354 _bfd_elf_swap_verdaux_out (output_bfd,
3355 &defaux, (Elf_External_Verdaux *) p);
3356 p += sizeof (Elf_External_Verdaux);
3357 }
3358 }
3359 while (def.vd_next);
3360 }
3361
3362 /* Adjust version references. */
3363 if (elf_tdata (output_bfd)->verref)
3364 {
3365 asection *s;
3366 bfd_byte *p;
3367 bfd_size_type i;
3368 Elf_Internal_Verneed need;
3369 Elf_Internal_Vernaux needaux;
3370
3d4d4302 3371 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3372 p = s->contents;
3373 do
3374 {
3375 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3376 &need);
3377 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3378 _bfd_elf_swap_verneed_out (output_bfd, &need,
3379 (Elf_External_Verneed *) p);
3380 p += sizeof (Elf_External_Verneed);
3381 for (i = 0; i < need.vn_cnt; ++i)
3382 {
3383 _bfd_elf_swap_vernaux_in (output_bfd,
3384 (Elf_External_Vernaux *) p, &needaux);
3385 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3386 needaux.vna_name);
3387 _bfd_elf_swap_vernaux_out (output_bfd,
3388 &needaux,
3389 (Elf_External_Vernaux *) p);
3390 p += sizeof (Elf_External_Vernaux);
3391 }
3392 }
3393 while (need.vn_next);
3394 }
3395
3396 return TRUE;
3397}
3398\f
13285a1b
AM
3399/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3400 The default is to only match when the INPUT and OUTPUT are exactly
3401 the same target. */
3402
3403bfd_boolean
3404_bfd_elf_default_relocs_compatible (const bfd_target *input,
3405 const bfd_target *output)
3406{
3407 return input == output;
3408}
3409
3410/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3411 This version is used when different targets for the same architecture
3412 are virtually identical. */
3413
3414bfd_boolean
3415_bfd_elf_relocs_compatible (const bfd_target *input,
3416 const bfd_target *output)
3417{
3418 const struct elf_backend_data *obed, *ibed;
3419
3420 if (input == output)
3421 return TRUE;
3422
3423 ibed = xvec_get_elf_backend_data (input);
3424 obed = xvec_get_elf_backend_data (output);
3425
3426 if (ibed->arch != obed->arch)
3427 return FALSE;
3428
3429 /* If both backends are using this function, deem them compatible. */
3430 return ibed->relocs_compatible == obed->relocs_compatible;
3431}
3432
e5034e59
AM
3433/* Make a special call to the linker "notice" function to tell it that
3434 we are about to handle an as-needed lib, or have finished
1b786873 3435 processing the lib. */
e5034e59
AM
3436
3437bfd_boolean
3438_bfd_elf_notice_as_needed (bfd *ibfd,
3439 struct bfd_link_info *info,
3440 enum notice_asneeded_action act)
3441{
46135103 3442 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3443}
3444
4ad4eba5
AM
3445/* Add symbols from an ELF object file to the linker hash table. */
3446
3447static bfd_boolean
3448elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3449{
a0c402a5 3450 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3451 Elf_Internal_Shdr *hdr;
3452 bfd_size_type symcount;
3453 bfd_size_type extsymcount;
3454 bfd_size_type extsymoff;
3455 struct elf_link_hash_entry **sym_hash;
3456 bfd_boolean dynamic;
3457 Elf_External_Versym *extversym = NULL;
3458 Elf_External_Versym *ever;
3459 struct elf_link_hash_entry *weaks;
3460 struct elf_link_hash_entry **nondeflt_vers = NULL;
3461 bfd_size_type nondeflt_vers_cnt = 0;
3462 Elf_Internal_Sym *isymbuf = NULL;
3463 Elf_Internal_Sym *isym;
3464 Elf_Internal_Sym *isymend;
3465 const struct elf_backend_data *bed;
3466 bfd_boolean add_needed;
66eb6687 3467 struct elf_link_hash_table *htab;
4ad4eba5 3468 bfd_size_type amt;
66eb6687 3469 void *alloc_mark = NULL;
4f87808c
AM
3470 struct bfd_hash_entry **old_table = NULL;
3471 unsigned int old_size = 0;
3472 unsigned int old_count = 0;
66eb6687 3473 void *old_tab = NULL;
66eb6687
AM
3474 void *old_ent;
3475 struct bfd_link_hash_entry *old_undefs = NULL;
3476 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3477 long old_dynsymcount = 0;
a4542f1b 3478 bfd_size_type old_dynstr_size = 0;
66eb6687 3479 size_t tabsize = 0;
db6a5d5f 3480 asection *s;
29a9f53e 3481 bfd_boolean just_syms;
4ad4eba5 3482
66eb6687 3483 htab = elf_hash_table (info);
4ad4eba5 3484 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3485
3486 if ((abfd->flags & DYNAMIC) == 0)
3487 dynamic = FALSE;
3488 else
3489 {
3490 dynamic = TRUE;
3491
3492 /* You can't use -r against a dynamic object. Also, there's no
3493 hope of using a dynamic object which does not exactly match
3494 the format of the output file. */
0e1862bb 3495 if (bfd_link_relocatable (info)
66eb6687 3496 || !is_elf_hash_table (htab)
f13a99db 3497 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3498 {
0e1862bb 3499 if (bfd_link_relocatable (info))
9a0789ec
NC
3500 bfd_set_error (bfd_error_invalid_operation);
3501 else
3502 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3503 goto error_return;
3504 }
3505 }
3506
a0c402a5
L
3507 ehdr = elf_elfheader (abfd);
3508 if (info->warn_alternate_em
3509 && bed->elf_machine_code != ehdr->e_machine
3510 && ((bed->elf_machine_alt1 != 0
3511 && ehdr->e_machine == bed->elf_machine_alt1)
3512 || (bed->elf_machine_alt2 != 0
3513 && ehdr->e_machine == bed->elf_machine_alt2)))
3514 info->callbacks->einfo
3515 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3516 ehdr->e_machine, abfd, bed->elf_machine_code);
3517
4ad4eba5
AM
3518 /* As a GNU extension, any input sections which are named
3519 .gnu.warning.SYMBOL are treated as warning symbols for the given
3520 symbol. This differs from .gnu.warning sections, which generate
3521 warnings when they are included in an output file. */
dd98f8d2 3522 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3523 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3524 {
db6a5d5f 3525 const char *name;
4ad4eba5 3526
db6a5d5f
AM
3527 name = bfd_get_section_name (abfd, s);
3528 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3529 {
db6a5d5f
AM
3530 char *msg;
3531 bfd_size_type sz;
3532
3533 name += sizeof ".gnu.warning." - 1;
3534
3535 /* If this is a shared object, then look up the symbol
3536 in the hash table. If it is there, and it is already
3537 been defined, then we will not be using the entry
3538 from this shared object, so we don't need to warn.
3539 FIXME: If we see the definition in a regular object
3540 later on, we will warn, but we shouldn't. The only
3541 fix is to keep track of what warnings we are supposed
3542 to emit, and then handle them all at the end of the
3543 link. */
3544 if (dynamic)
4ad4eba5 3545 {
db6a5d5f
AM
3546 struct elf_link_hash_entry *h;
3547
3548 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3549
3550 /* FIXME: What about bfd_link_hash_common? */
3551 if (h != NULL
3552 && (h->root.type == bfd_link_hash_defined
3553 || h->root.type == bfd_link_hash_defweak))
3554 continue;
3555 }
4ad4eba5 3556
db6a5d5f
AM
3557 sz = s->size;
3558 msg = (char *) bfd_alloc (abfd, sz + 1);
3559 if (msg == NULL)
3560 goto error_return;
4ad4eba5 3561
db6a5d5f
AM
3562 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3563 goto error_return;
4ad4eba5 3564
db6a5d5f 3565 msg[sz] = '\0';
4ad4eba5 3566
db6a5d5f
AM
3567 if (! (_bfd_generic_link_add_one_symbol
3568 (info, abfd, name, BSF_WARNING, s, 0, msg,
3569 FALSE, bed->collect, NULL)))
3570 goto error_return;
4ad4eba5 3571
0e1862bb 3572 if (bfd_link_executable (info))
db6a5d5f
AM
3573 {
3574 /* Clobber the section size so that the warning does
3575 not get copied into the output file. */
3576 s->size = 0;
11d2f718 3577
db6a5d5f
AM
3578 /* Also set SEC_EXCLUDE, so that symbols defined in
3579 the warning section don't get copied to the output. */
3580 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3581 }
3582 }
3583 }
3584
29a9f53e
L
3585 just_syms = ((s = abfd->sections) != NULL
3586 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3587
4ad4eba5
AM
3588 add_needed = TRUE;
3589 if (! dynamic)
3590 {
3591 /* If we are creating a shared library, create all the dynamic
3592 sections immediately. We need to attach them to something,
3593 so we attach them to this BFD, provided it is the right
29a9f53e
L
3594 format and is not from ld --just-symbols. FIXME: If there
3595 are no input BFD's of the same format as the output, we can't
3596 make a shared library. */
3597 if (!just_syms
0e1862bb 3598 && bfd_link_pic (info)
66eb6687 3599 && is_elf_hash_table (htab)
f13a99db 3600 && info->output_bfd->xvec == abfd->xvec
66eb6687 3601 && !htab->dynamic_sections_created)
4ad4eba5
AM
3602 {
3603 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3604 goto error_return;
3605 }
3606 }
66eb6687 3607 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3608 goto error_return;
3609 else
3610 {
4ad4eba5 3611 const char *soname = NULL;
7ee314fa 3612 char *audit = NULL;
4ad4eba5
AM
3613 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3614 int ret;
3615
3616 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3617 ld shouldn't allow it. */
29a9f53e 3618 if (just_syms)
92fd189d 3619 abort ();
4ad4eba5
AM
3620
3621 /* If this dynamic lib was specified on the command line with
3622 --as-needed in effect, then we don't want to add a DT_NEEDED
3623 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3624 in by another lib's DT_NEEDED. When --no-add-needed is used
3625 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3626 any dynamic library in DT_NEEDED tags in the dynamic lib at
3627 all. */
3628 add_needed = (elf_dyn_lib_class (abfd)
3629 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3630 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3631
3632 s = bfd_get_section_by_name (abfd, ".dynamic");
3633 if (s != NULL)
3634 {
3635 bfd_byte *dynbuf;
3636 bfd_byte *extdyn;
cb33740c 3637 unsigned int elfsec;
4ad4eba5
AM
3638 unsigned long shlink;
3639
eea6121a 3640 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3641 {
3642error_free_dyn:
3643 free (dynbuf);
3644 goto error_return;
3645 }
4ad4eba5
AM
3646
3647 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3648 if (elfsec == SHN_BAD)
4ad4eba5
AM
3649 goto error_free_dyn;
3650 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3651
3652 for (extdyn = dynbuf;
eea6121a 3653 extdyn < dynbuf + s->size;
4ad4eba5
AM
3654 extdyn += bed->s->sizeof_dyn)
3655 {
3656 Elf_Internal_Dyn dyn;
3657
3658 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3659 if (dyn.d_tag == DT_SONAME)
3660 {
3661 unsigned int tagv = dyn.d_un.d_val;
3662 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3663 if (soname == NULL)
3664 goto error_free_dyn;
3665 }
3666 if (dyn.d_tag == DT_NEEDED)
3667 {
3668 struct bfd_link_needed_list *n, **pn;
3669 char *fnm, *anm;
3670 unsigned int tagv = dyn.d_un.d_val;
3671
3672 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3673 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3674 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3675 if (n == NULL || fnm == NULL)
3676 goto error_free_dyn;
3677 amt = strlen (fnm) + 1;
a50b1753 3678 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3679 if (anm == NULL)
3680 goto error_free_dyn;
3681 memcpy (anm, fnm, amt);
3682 n->name = anm;
3683 n->by = abfd;
3684 n->next = NULL;
66eb6687 3685 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3686 ;
3687 *pn = n;
3688 }
3689 if (dyn.d_tag == DT_RUNPATH)
3690 {
3691 struct bfd_link_needed_list *n, **pn;
3692 char *fnm, *anm;
3693 unsigned int tagv = dyn.d_un.d_val;
3694
3695 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3696 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3697 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3698 if (n == NULL || fnm == NULL)
3699 goto error_free_dyn;
3700 amt = strlen (fnm) + 1;
a50b1753 3701 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3702 if (anm == NULL)
3703 goto error_free_dyn;
3704 memcpy (anm, fnm, amt);
3705 n->name = anm;
3706 n->by = abfd;
3707 n->next = NULL;
3708 for (pn = & runpath;
3709 *pn != NULL;
3710 pn = &(*pn)->next)
3711 ;
3712 *pn = n;
3713 }
3714 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3715 if (!runpath && dyn.d_tag == DT_RPATH)
3716 {
3717 struct bfd_link_needed_list *n, **pn;
3718 char *fnm, *anm;
3719 unsigned int tagv = dyn.d_un.d_val;
3720
3721 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3722 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3723 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3724 if (n == NULL || fnm == NULL)
3725 goto error_free_dyn;
3726 amt = strlen (fnm) + 1;
a50b1753 3727 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3728 if (anm == NULL)
f8703194 3729 goto error_free_dyn;
4ad4eba5
AM
3730 memcpy (anm, fnm, amt);
3731 n->name = anm;
3732 n->by = abfd;
3733 n->next = NULL;
3734 for (pn = & rpath;
3735 *pn != NULL;
3736 pn = &(*pn)->next)
3737 ;
3738 *pn = n;
3739 }
7ee314fa
AM
3740 if (dyn.d_tag == DT_AUDIT)
3741 {
3742 unsigned int tagv = dyn.d_un.d_val;
3743 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3744 }
4ad4eba5
AM
3745 }
3746
3747 free (dynbuf);
3748 }
3749
3750 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3751 frees all more recently bfd_alloc'd blocks as well. */
3752 if (runpath)
3753 rpath = runpath;
3754
3755 if (rpath)
3756 {
3757 struct bfd_link_needed_list **pn;
66eb6687 3758 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3759 ;
3760 *pn = rpath;
3761 }
3762
3763 /* We do not want to include any of the sections in a dynamic
3764 object in the output file. We hack by simply clobbering the
3765 list of sections in the BFD. This could be handled more
3766 cleanly by, say, a new section flag; the existing
3767 SEC_NEVER_LOAD flag is not the one we want, because that one
3768 still implies that the section takes up space in the output
3769 file. */
3770 bfd_section_list_clear (abfd);
3771
4ad4eba5
AM
3772 /* Find the name to use in a DT_NEEDED entry that refers to this
3773 object. If the object has a DT_SONAME entry, we use it.
3774 Otherwise, if the generic linker stuck something in
3775 elf_dt_name, we use that. Otherwise, we just use the file
3776 name. */
3777 if (soname == NULL || *soname == '\0')
3778 {
3779 soname = elf_dt_name (abfd);
3780 if (soname == NULL || *soname == '\0')
3781 soname = bfd_get_filename (abfd);
3782 }
3783
3784 /* Save the SONAME because sometimes the linker emulation code
3785 will need to know it. */
3786 elf_dt_name (abfd) = soname;
3787
7e9f0867 3788 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3789 if (ret < 0)
3790 goto error_return;
3791
3792 /* If we have already included this dynamic object in the
3793 link, just ignore it. There is no reason to include a
3794 particular dynamic object more than once. */
3795 if (ret > 0)
3796 return TRUE;
7ee314fa
AM
3797
3798 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3799 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3800 }
3801
3802 /* If this is a dynamic object, we always link against the .dynsym
3803 symbol table, not the .symtab symbol table. The dynamic linker
3804 will only see the .dynsym symbol table, so there is no reason to
3805 look at .symtab for a dynamic object. */
3806
3807 if (! dynamic || elf_dynsymtab (abfd) == 0)
3808 hdr = &elf_tdata (abfd)->symtab_hdr;
3809 else
3810 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3811
3812 symcount = hdr->sh_size / bed->s->sizeof_sym;
3813
3814 /* The sh_info field of the symtab header tells us where the
3815 external symbols start. We don't care about the local symbols at
3816 this point. */
3817 if (elf_bad_symtab (abfd))
3818 {
3819 extsymcount = symcount;
3820 extsymoff = 0;
3821 }
3822 else
3823 {
3824 extsymcount = symcount - hdr->sh_info;
3825 extsymoff = hdr->sh_info;
3826 }
3827
f45794cb 3828 sym_hash = elf_sym_hashes (abfd);
012b2306 3829 if (extsymcount != 0)
4ad4eba5
AM
3830 {
3831 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3832 NULL, NULL, NULL);
3833 if (isymbuf == NULL)
3834 goto error_return;
3835
4ad4eba5 3836 if (sym_hash == NULL)
012b2306
AM
3837 {
3838 /* We store a pointer to the hash table entry for each
3839 external symbol. */
3840 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3841 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3842 if (sym_hash == NULL)
3843 goto error_free_sym;
3844 elf_sym_hashes (abfd) = sym_hash;
3845 }
4ad4eba5
AM
3846 }
3847
3848 if (dynamic)
3849 {
3850 /* Read in any version definitions. */
fc0e6df6
PB
3851 if (!_bfd_elf_slurp_version_tables (abfd,
3852 info->default_imported_symver))
4ad4eba5
AM
3853 goto error_free_sym;
3854
3855 /* Read in the symbol versions, but don't bother to convert them
3856 to internal format. */
3857 if (elf_dynversym (abfd) != 0)
3858 {
3859 Elf_Internal_Shdr *versymhdr;
3860
3861 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3862 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3863 if (extversym == NULL)
3864 goto error_free_sym;
3865 amt = versymhdr->sh_size;
3866 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3867 || bfd_bread (extversym, amt, abfd) != amt)
3868 goto error_free_vers;
3869 }
3870 }
3871
66eb6687
AM
3872 /* If we are loading an as-needed shared lib, save the symbol table
3873 state before we start adding symbols. If the lib turns out
3874 to be unneeded, restore the state. */
3875 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3876 {
3877 unsigned int i;
3878 size_t entsize;
3879
3880 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3881 {
3882 struct bfd_hash_entry *p;
2de92251 3883 struct elf_link_hash_entry *h;
66eb6687
AM
3884
3885 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3886 {
3887 h = (struct elf_link_hash_entry *) p;
3888 entsize += htab->root.table.entsize;
3889 if (h->root.type == bfd_link_hash_warning)
3890 entsize += htab->root.table.entsize;
3891 }
66eb6687
AM
3892 }
3893
3894 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 3895 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
3896 if (old_tab == NULL)
3897 goto error_free_vers;
3898
3899 /* Remember the current objalloc pointer, so that all mem for
3900 symbols added can later be reclaimed. */
3901 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3902 if (alloc_mark == NULL)
3903 goto error_free_vers;
3904
5061a885
AM
3905 /* Make a special call to the linker "notice" function to
3906 tell it that we are about to handle an as-needed lib. */
e5034e59 3907 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 3908 goto error_free_vers;
5061a885 3909
f45794cb
AM
3910 /* Clone the symbol table. Remember some pointers into the
3911 symbol table, and dynamic symbol count. */
3912 old_ent = (char *) old_tab + tabsize;
66eb6687 3913 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
3914 old_undefs = htab->root.undefs;
3915 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3916 old_table = htab->root.table.table;
3917 old_size = htab->root.table.size;
3918 old_count = htab->root.table.count;
66eb6687 3919 old_dynsymcount = htab->dynsymcount;
a4542f1b 3920 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
3921
3922 for (i = 0; i < htab->root.table.size; i++)
3923 {
3924 struct bfd_hash_entry *p;
2de92251 3925 struct elf_link_hash_entry *h;
66eb6687
AM
3926
3927 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3928 {
3929 memcpy (old_ent, p, htab->root.table.entsize);
3930 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3931 h = (struct elf_link_hash_entry *) p;
3932 if (h->root.type == bfd_link_hash_warning)
3933 {
3934 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3935 old_ent = (char *) old_ent + htab->root.table.entsize;
3936 }
66eb6687
AM
3937 }
3938 }
3939 }
4ad4eba5 3940
66eb6687 3941 weaks = NULL;
4ad4eba5
AM
3942 ever = extversym != NULL ? extversym + extsymoff : NULL;
3943 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3944 isym < isymend;
3945 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3946 {
3947 int bind;
3948 bfd_vma value;
af44c138 3949 asection *sec, *new_sec;
4ad4eba5
AM
3950 flagword flags;
3951 const char *name;
3952 struct elf_link_hash_entry *h;
90c984fc 3953 struct elf_link_hash_entry *hi;
4ad4eba5
AM
3954 bfd_boolean definition;
3955 bfd_boolean size_change_ok;
3956 bfd_boolean type_change_ok;
3957 bfd_boolean new_weakdef;
37a9e49a
L
3958 bfd_boolean new_weak;
3959 bfd_boolean old_weak;
4ad4eba5 3960 bfd_boolean override;
a4d8e49b 3961 bfd_boolean common;
4ad4eba5
AM
3962 unsigned int old_alignment;
3963 bfd *old_bfd;
6e33951e 3964 bfd_boolean matched;
4ad4eba5
AM
3965
3966 override = FALSE;
3967
3968 flags = BSF_NO_FLAGS;
3969 sec = NULL;
3970 value = isym->st_value;
a4d8e49b 3971 common = bed->common_definition (isym);
4ad4eba5
AM
3972
3973 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3974 switch (bind)
4ad4eba5 3975 {
3e7a7d11 3976 case STB_LOCAL:
4ad4eba5
AM
3977 /* This should be impossible, since ELF requires that all
3978 global symbols follow all local symbols, and that sh_info
3979 point to the first global symbol. Unfortunately, Irix 5
3980 screws this up. */
3981 continue;
3e7a7d11
NC
3982
3983 case STB_GLOBAL:
a4d8e49b 3984 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3985 flags = BSF_GLOBAL;
3e7a7d11
NC
3986 break;
3987
3988 case STB_WEAK:
3989 flags = BSF_WEAK;
3990 break;
3991
3992 case STB_GNU_UNIQUE:
3993 flags = BSF_GNU_UNIQUE;
3994 break;
3995
3996 default:
4ad4eba5 3997 /* Leave it up to the processor backend. */
3e7a7d11 3998 break;
4ad4eba5
AM
3999 }
4000
4001 if (isym->st_shndx == SHN_UNDEF)
4002 sec = bfd_und_section_ptr;
cb33740c
AM
4003 else if (isym->st_shndx == SHN_ABS)
4004 sec = bfd_abs_section_ptr;
4005 else if (isym->st_shndx == SHN_COMMON)
4006 {
4007 sec = bfd_com_section_ptr;
4008 /* What ELF calls the size we call the value. What ELF
4009 calls the value we call the alignment. */
4010 value = isym->st_size;
4011 }
4012 else
4ad4eba5
AM
4013 {
4014 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4015 if (sec == NULL)
4016 sec = bfd_abs_section_ptr;
dbaa2011 4017 else if (discarded_section (sec))
529fcb95 4018 {
e5d08002
L
4019 /* Symbols from discarded section are undefined. We keep
4020 its visibility. */
529fcb95
PB
4021 sec = bfd_und_section_ptr;
4022 isym->st_shndx = SHN_UNDEF;
4023 }
4ad4eba5
AM
4024 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4025 value -= sec->vma;
4026 }
4ad4eba5
AM
4027
4028 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4029 isym->st_name);
4030 if (name == NULL)
4031 goto error_free_vers;
4032
4033 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4034 && (abfd->flags & BFD_PLUGIN) != 0)
4035 {
4036 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4037
4038 if (xc == NULL)
4039 {
4040 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4041 | SEC_EXCLUDE);
4042 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4043 if (xc == NULL)
4044 goto error_free_vers;
4045 }
4046 sec = xc;
4047 }
4048 else if (isym->st_shndx == SHN_COMMON
4049 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4050 && !bfd_link_relocatable (info))
4ad4eba5
AM
4051 {
4052 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4053
4054 if (tcomm == NULL)
4055 {
02d00247
AM
4056 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4057 | SEC_LINKER_CREATED);
4058 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4059 if (tcomm == NULL)
4ad4eba5
AM
4060 goto error_free_vers;
4061 }
4062 sec = tcomm;
4063 }
66eb6687 4064 else if (bed->elf_add_symbol_hook)
4ad4eba5 4065 {
66eb6687
AM
4066 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4067 &sec, &value))
4ad4eba5
AM
4068 goto error_free_vers;
4069
4070 /* The hook function sets the name to NULL if this symbol
4071 should be skipped for some reason. */
4072 if (name == NULL)
4073 continue;
4074 }
4075
4076 /* Sanity check that all possibilities were handled. */
4077 if (sec == NULL)
4078 {
4079 bfd_set_error (bfd_error_bad_value);
4080 goto error_free_vers;
4081 }
4082
191c0c42
AM
4083 /* Silently discard TLS symbols from --just-syms. There's
4084 no way to combine a static TLS block with a new TLS block
4085 for this executable. */
4086 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4087 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4088 continue;
4089
4ad4eba5
AM
4090 if (bfd_is_und_section (sec)
4091 || bfd_is_com_section (sec))
4092 definition = FALSE;
4093 else
4094 definition = TRUE;
4095
4096 size_change_ok = FALSE;
66eb6687 4097 type_change_ok = bed->type_change_ok;
37a9e49a 4098 old_weak = FALSE;
6e33951e 4099 matched = FALSE;
4ad4eba5
AM
4100 old_alignment = 0;
4101 old_bfd = NULL;
af44c138 4102 new_sec = sec;
4ad4eba5 4103
66eb6687 4104 if (is_elf_hash_table (htab))
4ad4eba5
AM
4105 {
4106 Elf_Internal_Versym iver;
4107 unsigned int vernum = 0;
4108 bfd_boolean skip;
4109
fc0e6df6 4110 if (ever == NULL)
4ad4eba5 4111 {
fc0e6df6
PB
4112 if (info->default_imported_symver)
4113 /* Use the default symbol version created earlier. */
4114 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4115 else
4116 iver.vs_vers = 0;
4117 }
4118 else
4119 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4120
4121 vernum = iver.vs_vers & VERSYM_VERSION;
4122
4123 /* If this is a hidden symbol, or if it is not version
4124 1, we append the version name to the symbol name.
cc86ff91
EB
4125 However, we do not modify a non-hidden absolute symbol
4126 if it is not a function, because it might be the version
4127 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4128 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4129 || (vernum > 1
4130 && (!bfd_is_abs_section (sec)
4131 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4132 {
4133 const char *verstr;
4134 size_t namelen, verlen, newlen;
4135 char *newname, *p;
4136
4137 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4138 {
fc0e6df6
PB
4139 if (vernum > elf_tdata (abfd)->cverdefs)
4140 verstr = NULL;
4141 else if (vernum > 1)
4142 verstr =
4143 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4144 else
4145 verstr = "";
4ad4eba5 4146
fc0e6df6 4147 if (verstr == NULL)
4ad4eba5 4148 {
fc0e6df6
PB
4149 (*_bfd_error_handler)
4150 (_("%B: %s: invalid version %u (max %d)"),
4151 abfd, name, vernum,
4152 elf_tdata (abfd)->cverdefs);
4153 bfd_set_error (bfd_error_bad_value);
4154 goto error_free_vers;
4ad4eba5 4155 }
fc0e6df6
PB
4156 }
4157 else
4158 {
4159 /* We cannot simply test for the number of
4160 entries in the VERNEED section since the
4161 numbers for the needed versions do not start
4162 at 0. */
4163 Elf_Internal_Verneed *t;
4164
4165 verstr = NULL;
4166 for (t = elf_tdata (abfd)->verref;
4167 t != NULL;
4168 t = t->vn_nextref)
4ad4eba5 4169 {
fc0e6df6 4170 Elf_Internal_Vernaux *a;
4ad4eba5 4171
fc0e6df6
PB
4172 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4173 {
4174 if (a->vna_other == vernum)
4ad4eba5 4175 {
fc0e6df6
PB
4176 verstr = a->vna_nodename;
4177 break;
4ad4eba5 4178 }
4ad4eba5 4179 }
fc0e6df6
PB
4180 if (a != NULL)
4181 break;
4182 }
4183 if (verstr == NULL)
4184 {
4185 (*_bfd_error_handler)
4186 (_("%B: %s: invalid needed version %d"),
4187 abfd, name, vernum);
4188 bfd_set_error (bfd_error_bad_value);
4189 goto error_free_vers;
4ad4eba5 4190 }
4ad4eba5 4191 }
fc0e6df6
PB
4192
4193 namelen = strlen (name);
4194 verlen = strlen (verstr);
4195 newlen = namelen + verlen + 2;
4196 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4197 && isym->st_shndx != SHN_UNDEF)
4198 ++newlen;
4199
a50b1753 4200 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4201 if (newname == NULL)
4202 goto error_free_vers;
4203 memcpy (newname, name, namelen);
4204 p = newname + namelen;
4205 *p++ = ELF_VER_CHR;
4206 /* If this is a defined non-hidden version symbol,
4207 we add another @ to the name. This indicates the
4208 default version of the symbol. */
4209 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4210 && isym->st_shndx != SHN_UNDEF)
4211 *p++ = ELF_VER_CHR;
4212 memcpy (p, verstr, verlen + 1);
4213
4214 name = newname;
4ad4eba5
AM
4215 }
4216
cd3416da
AM
4217 /* If this symbol has default visibility and the user has
4218 requested we not re-export it, then mark it as hidden. */
4219 if (definition
4220 && !dynamic
ce875075 4221 && abfd->no_export
cd3416da
AM
4222 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4223 isym->st_other = (STV_HIDDEN
4224 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4225
4f3fedcf
AM
4226 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4227 sym_hash, &old_bfd, &old_weak,
4228 &old_alignment, &skip, &override,
6e33951e
L
4229 &type_change_ok, &size_change_ok,
4230 &matched))
4ad4eba5
AM
4231 goto error_free_vers;
4232
4233 if (skip)
4234 continue;
4235
6e33951e
L
4236 /* Override a definition only if the new symbol matches the
4237 existing one. */
4238 if (override && matched)
4ad4eba5
AM
4239 definition = FALSE;
4240
4241 h = *sym_hash;
4242 while (h->root.type == bfd_link_hash_indirect
4243 || h->root.type == bfd_link_hash_warning)
4244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4245
4ad4eba5 4246 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4247 && vernum > 1
4248 && definition)
4249 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4250 }
4251
4252 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4253 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4254 (struct bfd_link_hash_entry **) sym_hash)))
4255 goto error_free_vers;
4256
4257 h = *sym_hash;
90c984fc
L
4258 /* We need to make sure that indirect symbol dynamic flags are
4259 updated. */
4260 hi = h;
4ad4eba5
AM
4261 while (h->root.type == bfd_link_hash_indirect
4262 || h->root.type == bfd_link_hash_warning)
4263 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4264
4ad4eba5
AM
4265 *sym_hash = h;
4266
37a9e49a 4267 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4268 new_weakdef = FALSE;
4269 if (dynamic
4270 && definition
37a9e49a 4271 && new_weak
fcb93ecf 4272 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4273 && is_elf_hash_table (htab)
f6e332e6 4274 && h->u.weakdef == NULL)
4ad4eba5
AM
4275 {
4276 /* Keep a list of all weak defined non function symbols from
4277 a dynamic object, using the weakdef field. Later in this
4278 function we will set the weakdef field to the correct
4279 value. We only put non-function symbols from dynamic
4280 objects on this list, because that happens to be the only
4281 time we need to know the normal symbol corresponding to a
4282 weak symbol, and the information is time consuming to
4283 figure out. If the weakdef field is not already NULL,
4284 then this symbol was already defined by some previous
4285 dynamic object, and we will be using that previous
4286 definition anyhow. */
4287
f6e332e6 4288 h->u.weakdef = weaks;
4ad4eba5
AM
4289 weaks = h;
4290 new_weakdef = TRUE;
4291 }
4292
4293 /* Set the alignment of a common symbol. */
a4d8e49b 4294 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4295 && h->root.type == bfd_link_hash_common)
4296 {
4297 unsigned int align;
4298
a4d8e49b 4299 if (common)
af44c138
L
4300 align = bfd_log2 (isym->st_value);
4301 else
4302 {
4303 /* The new symbol is a common symbol in a shared object.
4304 We need to get the alignment from the section. */
4305 align = new_sec->alignment_power;
4306 }
595213d4 4307 if (align > old_alignment)
4ad4eba5
AM
4308 h->root.u.c.p->alignment_power = align;
4309 else
4310 h->root.u.c.p->alignment_power = old_alignment;
4311 }
4312
66eb6687 4313 if (is_elf_hash_table (htab))
4ad4eba5 4314 {
4f3fedcf
AM
4315 /* Set a flag in the hash table entry indicating the type of
4316 reference or definition we just found. A dynamic symbol
4317 is one which is referenced or defined by both a regular
4318 object and a shared object. */
4319 bfd_boolean dynsym = FALSE;
4320
4321 /* Plugin symbols aren't normal. Don't set def_regular or
4322 ref_regular for them, or make them dynamic. */
4323 if ((abfd->flags & BFD_PLUGIN) != 0)
4324 ;
4325 else if (! dynamic)
4326 {
4327 if (! definition)
4328 {
4329 h->ref_regular = 1;
4330 if (bind != STB_WEAK)
4331 h->ref_regular_nonweak = 1;
4332 }
4333 else
4334 {
4335 h->def_regular = 1;
4336 if (h->def_dynamic)
4337 {
4338 h->def_dynamic = 0;
4339 h->ref_dynamic = 1;
4340 }
4341 }
4342
4343 /* If the indirect symbol has been forced local, don't
4344 make the real symbol dynamic. */
4345 if ((h == hi || !hi->forced_local)
0e1862bb 4346 && (bfd_link_dll (info)
4f3fedcf
AM
4347 || h->def_dynamic
4348 || h->ref_dynamic))
4349 dynsym = TRUE;
4350 }
4351 else
4352 {
4353 if (! definition)
4354 {
4355 h->ref_dynamic = 1;
4356 hi->ref_dynamic = 1;
4357 }
4358 else
4359 {
4360 h->def_dynamic = 1;
4361 hi->def_dynamic = 1;
4362 }
4363
4364 /* If the indirect symbol has been forced local, don't
4365 make the real symbol dynamic. */
4366 if ((h == hi || !hi->forced_local)
4367 && (h->def_regular
4368 || h->ref_regular
4369 || (h->u.weakdef != NULL
4370 && ! new_weakdef
4371 && h->u.weakdef->dynindx != -1)))
4372 dynsym = TRUE;
4373 }
4374
4375 /* Check to see if we need to add an indirect symbol for
4376 the default name. */
4377 if (definition
4378 || (!override && h->root.type == bfd_link_hash_common))
4379 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4380 sec, value, &old_bfd, &dynsym))
4381 goto error_free_vers;
4ad4eba5
AM
4382
4383 /* Check the alignment when a common symbol is involved. This
4384 can change when a common symbol is overridden by a normal
4385 definition or a common symbol is ignored due to the old
4386 normal definition. We need to make sure the maximum
4387 alignment is maintained. */
a4d8e49b 4388 if ((old_alignment || common)
4ad4eba5
AM
4389 && h->root.type != bfd_link_hash_common)
4390 {
4391 unsigned int common_align;
4392 unsigned int normal_align;
4393 unsigned int symbol_align;
4394 bfd *normal_bfd;
4395 bfd *common_bfd;
4396
3a81e825
AM
4397 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4398 || h->root.type == bfd_link_hash_defweak);
4399
4ad4eba5
AM
4400 symbol_align = ffs (h->root.u.def.value) - 1;
4401 if (h->root.u.def.section->owner != NULL
4402 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4403 {
4404 normal_align = h->root.u.def.section->alignment_power;
4405 if (normal_align > symbol_align)
4406 normal_align = symbol_align;
4407 }
4408 else
4409 normal_align = symbol_align;
4410
4411 if (old_alignment)
4412 {
4413 common_align = old_alignment;
4414 common_bfd = old_bfd;
4415 normal_bfd = abfd;
4416 }
4417 else
4418 {
4419 common_align = bfd_log2 (isym->st_value);
4420 common_bfd = abfd;
4421 normal_bfd = old_bfd;
4422 }
4423
4424 if (normal_align < common_align)
d07676f8
NC
4425 {
4426 /* PR binutils/2735 */
4427 if (normal_bfd == NULL)
4428 (*_bfd_error_handler)
4f3fedcf
AM
4429 (_("Warning: alignment %u of common symbol `%s' in %B is"
4430 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4431 common_bfd, h->root.u.def.section,
4432 1 << common_align, name, 1 << normal_align);
4433 else
4434 (*_bfd_error_handler)
4435 (_("Warning: alignment %u of symbol `%s' in %B"
4436 " is smaller than %u in %B"),
4437 normal_bfd, common_bfd,
4438 1 << normal_align, name, 1 << common_align);
4439 }
4ad4eba5
AM
4440 }
4441
83ad0046 4442 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4443 if (isym->st_size != 0
4444 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4445 && (definition || h->size == 0))
4446 {
83ad0046
L
4447 if (h->size != 0
4448 && h->size != isym->st_size
4449 && ! size_change_ok)
4ad4eba5 4450 (*_bfd_error_handler)
d003868e
AM
4451 (_("Warning: size of symbol `%s' changed"
4452 " from %lu in %B to %lu in %B"),
4453 old_bfd, abfd,
4ad4eba5 4454 name, (unsigned long) h->size,
d003868e 4455 (unsigned long) isym->st_size);
4ad4eba5
AM
4456
4457 h->size = isym->st_size;
4458 }
4459
4460 /* If this is a common symbol, then we always want H->SIZE
4461 to be the size of the common symbol. The code just above
4462 won't fix the size if a common symbol becomes larger. We
4463 don't warn about a size change here, because that is
4f3fedcf 4464 covered by --warn-common. Allow changes between different
fcb93ecf 4465 function types. */
4ad4eba5
AM
4466 if (h->root.type == bfd_link_hash_common)
4467 h->size = h->root.u.c.size;
4468
4469 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4470 && ((definition && !new_weak)
4471 || (old_weak && h->root.type == bfd_link_hash_common)
4472 || h->type == STT_NOTYPE))
4ad4eba5 4473 {
2955ec4c
L
4474 unsigned int type = ELF_ST_TYPE (isym->st_info);
4475
4476 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4477 symbol. */
4478 if (type == STT_GNU_IFUNC
4479 && (abfd->flags & DYNAMIC) != 0)
4480 type = STT_FUNC;
4ad4eba5 4481
2955ec4c
L
4482 if (h->type != type)
4483 {
4484 if (h->type != STT_NOTYPE && ! type_change_ok)
4485 (*_bfd_error_handler)
4486 (_("Warning: type of symbol `%s' changed"
4487 " from %d to %d in %B"),
4488 abfd, name, h->type, type);
4489
4490 h->type = type;
4491 }
4ad4eba5
AM
4492 }
4493
54ac0771 4494 /* Merge st_other field. */
b8417128 4495 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4496
c3df8c14 4497 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4498 if (definition
4499 && (sec->flags & SEC_DEBUGGING)
4500 && !bfd_link_relocatable (info))
c3df8c14
AM
4501 dynsym = FALSE;
4502
4f3fedcf
AM
4503 /* Nor should we make plugin symbols dynamic. */
4504 if ((abfd->flags & BFD_PLUGIN) != 0)
4505 dynsym = FALSE;
4506
35fc36a8 4507 if (definition)
35399224
L
4508 {
4509 h->target_internal = isym->st_target_internal;
4510 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4511 }
35fc36a8 4512
4ad4eba5
AM
4513 if (definition && !dynamic)
4514 {
4515 char *p = strchr (name, ELF_VER_CHR);
4516 if (p != NULL && p[1] != ELF_VER_CHR)
4517 {
4518 /* Queue non-default versions so that .symver x, x@FOO
4519 aliases can be checked. */
66eb6687 4520 if (!nondeflt_vers)
4ad4eba5 4521 {
66eb6687
AM
4522 amt = ((isymend - isym + 1)
4523 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4524 nondeflt_vers
4525 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4526 if (!nondeflt_vers)
4527 goto error_free_vers;
4ad4eba5 4528 }
66eb6687 4529 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4530 }
4531 }
4532
4533 if (dynsym && h->dynindx == -1)
4534 {
c152c796 4535 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4536 goto error_free_vers;
f6e332e6 4537 if (h->u.weakdef != NULL
4ad4eba5 4538 && ! new_weakdef
f6e332e6 4539 && h->u.weakdef->dynindx == -1)
4ad4eba5 4540 {
66eb6687 4541 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4542 goto error_free_vers;
4543 }
4544 }
4545 else if (dynsym && h->dynindx != -1)
4546 /* If the symbol already has a dynamic index, but
4547 visibility says it should not be visible, turn it into
4548 a local symbol. */
4549 switch (ELF_ST_VISIBILITY (h->other))
4550 {
4551 case STV_INTERNAL:
4552 case STV_HIDDEN:
4553 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4554 dynsym = FALSE;
4555 break;
4556 }
4557
3d5bef4c 4558 /* Don't add DT_NEEDED for references from the dummy bfd. */
4ad4eba5
AM
4559 if (!add_needed
4560 && definition
010e5ae2 4561 && ((dynsym
ffa9430d 4562 && h->ref_regular_nonweak
4f3fedcf
AM
4563 && (old_bfd == NULL
4564 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4565 || (h->ref_dynamic_nonweak
010e5ae2
AM
4566 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4567 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4568 {
4569 int ret;
4570 const char *soname = elf_dt_name (abfd);
4571
16e4ecc0
AM
4572 info->callbacks->minfo ("%!", soname, old_bfd,
4573 h->root.root.string);
4574
4ad4eba5
AM
4575 /* A symbol from a library loaded via DT_NEEDED of some
4576 other library is referenced by a regular object.
e56f61be 4577 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4578 --no-add-needed is used and the reference was not
4579 a weak one. */
4f3fedcf 4580 if (old_bfd != NULL
b918acf9 4581 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4582 {
4583 (*_bfd_error_handler)
3cbc5de0 4584 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4585 old_bfd, name);
ff5ac77b 4586 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4587 goto error_free_vers;
4588 }
4589
a50b1753 4590 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4591 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4592
4ad4eba5 4593 add_needed = TRUE;
7e9f0867 4594 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4595 if (ret < 0)
4596 goto error_free_vers;
4597
4598 BFD_ASSERT (ret == 0);
4599 }
4600 }
4601 }
4602
66eb6687
AM
4603 if (extversym != NULL)
4604 {
4605 free (extversym);
4606 extversym = NULL;
4607 }
4608
4609 if (isymbuf != NULL)
4610 {
4611 free (isymbuf);
4612 isymbuf = NULL;
4613 }
4614
4615 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4616 {
4617 unsigned int i;
4618
4619 /* Restore the symbol table. */
f45794cb
AM
4620 old_ent = (char *) old_tab + tabsize;
4621 memset (elf_sym_hashes (abfd), 0,
4622 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4623 htab->root.table.table = old_table;
4624 htab->root.table.size = old_size;
4625 htab->root.table.count = old_count;
66eb6687 4626 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4627 htab->root.undefs = old_undefs;
4628 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4629 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4630 for (i = 0; i < htab->root.table.size; i++)
4631 {
4632 struct bfd_hash_entry *p;
4633 struct elf_link_hash_entry *h;
3e0882af
L
4634 bfd_size_type size;
4635 unsigned int alignment_power;
66eb6687
AM
4636
4637 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4638 {
4639 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4640 if (h->root.type == bfd_link_hash_warning)
4641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4642 if (h->dynindx >= old_dynsymcount
4643 && h->dynstr_index < old_dynstr_size)
66eb6687 4644 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4645
3e0882af
L
4646 /* Preserve the maximum alignment and size for common
4647 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4648 since it can still be loaded at run time by another
3e0882af
L
4649 dynamic lib. */
4650 if (h->root.type == bfd_link_hash_common)
4651 {
4652 size = h->root.u.c.size;
4653 alignment_power = h->root.u.c.p->alignment_power;
4654 }
4655 else
4656 {
4657 size = 0;
4658 alignment_power = 0;
4659 }
66eb6687
AM
4660 memcpy (p, old_ent, htab->root.table.entsize);
4661 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4662 h = (struct elf_link_hash_entry *) p;
4663 if (h->root.type == bfd_link_hash_warning)
4664 {
4665 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4666 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4667 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4668 }
a4542f1b 4669 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4670 {
4671 if (size > h->root.u.c.size)
4672 h->root.u.c.size = size;
4673 if (alignment_power > h->root.u.c.p->alignment_power)
4674 h->root.u.c.p->alignment_power = alignment_power;
4675 }
66eb6687
AM
4676 }
4677 }
4678
5061a885
AM
4679 /* Make a special call to the linker "notice" function to
4680 tell it that symbols added for crefs may need to be removed. */
e5034e59 4681 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4682 goto error_free_vers;
5061a885 4683
66eb6687
AM
4684 free (old_tab);
4685 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4686 alloc_mark);
4687 if (nondeflt_vers != NULL)
4688 free (nondeflt_vers);
4689 return TRUE;
4690 }
2de92251 4691
66eb6687
AM
4692 if (old_tab != NULL)
4693 {
e5034e59 4694 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4695 goto error_free_vers;
66eb6687
AM
4696 free (old_tab);
4697 old_tab = NULL;
4698 }
4699
c6e8a9a8
L
4700 /* Now that all the symbols from this input file are created, if
4701 not performing a relocatable link, handle .symver foo, foo@BAR
4702 such that any relocs against foo become foo@BAR. */
0e1862bb 4703 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4704 {
4705 bfd_size_type cnt, symidx;
4706
4707 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4708 {
4709 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4710 char *shortname, *p;
4711
4712 p = strchr (h->root.root.string, ELF_VER_CHR);
4713 if (p == NULL
4714 || (h->root.type != bfd_link_hash_defined
4715 && h->root.type != bfd_link_hash_defweak))
4716 continue;
4717
4718 amt = p - h->root.root.string;
a50b1753 4719 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4720 if (!shortname)
4721 goto error_free_vers;
4ad4eba5
AM
4722 memcpy (shortname, h->root.root.string, amt);
4723 shortname[amt] = '\0';
4724
4725 hi = (struct elf_link_hash_entry *)
66eb6687 4726 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4727 FALSE, FALSE, FALSE);
4728 if (hi != NULL
4729 && hi->root.type == h->root.type
4730 && hi->root.u.def.value == h->root.u.def.value
4731 && hi->root.u.def.section == h->root.u.def.section)
4732 {
4733 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4734 hi->root.type = bfd_link_hash_indirect;
4735 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4736 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4737 sym_hash = elf_sym_hashes (abfd);
4738 if (sym_hash)
4739 for (symidx = 0; symidx < extsymcount; ++symidx)
4740 if (sym_hash[symidx] == hi)
4741 {
4742 sym_hash[symidx] = h;
4743 break;
4744 }
4745 }
4746 free (shortname);
4747 }
4748 free (nondeflt_vers);
4749 nondeflt_vers = NULL;
4750 }
4751
4ad4eba5
AM
4752 /* Now set the weakdefs field correctly for all the weak defined
4753 symbols we found. The only way to do this is to search all the
4754 symbols. Since we only need the information for non functions in
4755 dynamic objects, that's the only time we actually put anything on
4756 the list WEAKS. We need this information so that if a regular
4757 object refers to a symbol defined weakly in a dynamic object, the
4758 real symbol in the dynamic object is also put in the dynamic
4759 symbols; we also must arrange for both symbols to point to the
4760 same memory location. We could handle the general case of symbol
4761 aliasing, but a general symbol alias can only be generated in
4762 assembler code, handling it correctly would be very time
4763 consuming, and other ELF linkers don't handle general aliasing
4764 either. */
4765 if (weaks != NULL)
4766 {
4767 struct elf_link_hash_entry **hpp;
4768 struct elf_link_hash_entry **hppend;
4769 struct elf_link_hash_entry **sorted_sym_hash;
4770 struct elf_link_hash_entry *h;
4771 size_t sym_count;
4772
4773 /* Since we have to search the whole symbol list for each weak
4774 defined symbol, search time for N weak defined symbols will be
4775 O(N^2). Binary search will cut it down to O(NlogN). */
4776 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4777 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4778 if (sorted_sym_hash == NULL)
4779 goto error_return;
4780 sym_hash = sorted_sym_hash;
4781 hpp = elf_sym_hashes (abfd);
4782 hppend = hpp + extsymcount;
4783 sym_count = 0;
4784 for (; hpp < hppend; hpp++)
4785 {
4786 h = *hpp;
4787 if (h != NULL
4788 && h->root.type == bfd_link_hash_defined
fcb93ecf 4789 && !bed->is_function_type (h->type))
4ad4eba5
AM
4790 {
4791 *sym_hash = h;
4792 sym_hash++;
4793 sym_count++;
4794 }
4795 }
4796
4797 qsort (sorted_sym_hash, sym_count,
4798 sizeof (struct elf_link_hash_entry *),
4799 elf_sort_symbol);
4800
4801 while (weaks != NULL)
4802 {
4803 struct elf_link_hash_entry *hlook;
4804 asection *slook;
4805 bfd_vma vlook;
ed54588d 4806 size_t i, j, idx = 0;
4ad4eba5
AM
4807
4808 hlook = weaks;
f6e332e6
AM
4809 weaks = hlook->u.weakdef;
4810 hlook->u.weakdef = NULL;
4ad4eba5
AM
4811
4812 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4813 || hlook->root.type == bfd_link_hash_defweak
4814 || hlook->root.type == bfd_link_hash_common
4815 || hlook->root.type == bfd_link_hash_indirect);
4816 slook = hlook->root.u.def.section;
4817 vlook = hlook->root.u.def.value;
4818
4ad4eba5
AM
4819 i = 0;
4820 j = sym_count;
14160578 4821 while (i != j)
4ad4eba5
AM
4822 {
4823 bfd_signed_vma vdiff;
4824 idx = (i + j) / 2;
14160578 4825 h = sorted_sym_hash[idx];
4ad4eba5
AM
4826 vdiff = vlook - h->root.u.def.value;
4827 if (vdiff < 0)
4828 j = idx;
4829 else if (vdiff > 0)
4830 i = idx + 1;
4831 else
4832 {
d3435ae8 4833 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4834 if (sdiff < 0)
4835 j = idx;
4836 else if (sdiff > 0)
4837 i = idx + 1;
4838 else
14160578 4839 break;
4ad4eba5
AM
4840 }
4841 }
4842
4843 /* We didn't find a value/section match. */
14160578 4844 if (i == j)
4ad4eba5
AM
4845 continue;
4846
14160578
AM
4847 /* With multiple aliases, or when the weak symbol is already
4848 strongly defined, we have multiple matching symbols and
4849 the binary search above may land on any of them. Step
4850 one past the matching symbol(s). */
4851 while (++idx != j)
4852 {
4853 h = sorted_sym_hash[idx];
4854 if (h->root.u.def.section != slook
4855 || h->root.u.def.value != vlook)
4856 break;
4857 }
4858
4859 /* Now look back over the aliases. Since we sorted by size
4860 as well as value and section, we'll choose the one with
4861 the largest size. */
4862 while (idx-- != i)
4ad4eba5 4863 {
14160578 4864 h = sorted_sym_hash[idx];
4ad4eba5
AM
4865
4866 /* Stop if value or section doesn't match. */
14160578
AM
4867 if (h->root.u.def.section != slook
4868 || h->root.u.def.value != vlook)
4ad4eba5
AM
4869 break;
4870 else if (h != hlook)
4871 {
f6e332e6 4872 hlook->u.weakdef = h;
4ad4eba5
AM
4873
4874 /* If the weak definition is in the list of dynamic
4875 symbols, make sure the real definition is put
4876 there as well. */
4877 if (hlook->dynindx != -1 && h->dynindx == -1)
4878 {
c152c796 4879 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4880 {
4881 err_free_sym_hash:
4882 free (sorted_sym_hash);
4883 goto error_return;
4884 }
4ad4eba5
AM
4885 }
4886
4887 /* If the real definition is in the list of dynamic
4888 symbols, make sure the weak definition is put
4889 there as well. If we don't do this, then the
4890 dynamic loader might not merge the entries for the
4891 real definition and the weak definition. */
4892 if (h->dynindx != -1 && hlook->dynindx == -1)
4893 {
c152c796 4894 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4895 goto err_free_sym_hash;
4ad4eba5
AM
4896 }
4897 break;
4898 }
4899 }
4900 }
4901
4902 free (sorted_sym_hash);
4903 }
4904
33177bb1
AM
4905 if (bed->check_directives
4906 && !(*bed->check_directives) (abfd, info))
4907 return FALSE;
85fbca6a 4908
4ad4eba5
AM
4909 /* If this object is the same format as the output object, and it is
4910 not a shared library, then let the backend look through the
4911 relocs.
4912
4913 This is required to build global offset table entries and to
4914 arrange for dynamic relocs. It is not required for the
4915 particular common case of linking non PIC code, even when linking
4916 against shared libraries, but unfortunately there is no way of
4917 knowing whether an object file has been compiled PIC or not.
4918 Looking through the relocs is not particularly time consuming.
4919 The problem is that we must either (1) keep the relocs in memory,
4920 which causes the linker to require additional runtime memory or
4921 (2) read the relocs twice from the input file, which wastes time.
4922 This would be a good case for using mmap.
4923
4924 I have no idea how to handle linking PIC code into a file of a
4925 different format. It probably can't be done. */
4ad4eba5 4926 if (! dynamic
66eb6687 4927 && is_elf_hash_table (htab)
13285a1b 4928 && bed->check_relocs != NULL
39334f3a 4929 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4930 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4931 {
4932 asection *o;
4933
4934 for (o = abfd->sections; o != NULL; o = o->next)
4935 {
4936 Elf_Internal_Rela *internal_relocs;
4937 bfd_boolean ok;
4938
4939 if ((o->flags & SEC_RELOC) == 0
4940 || o->reloc_count == 0
4941 || ((info->strip == strip_all || info->strip == strip_debugger)
4942 && (o->flags & SEC_DEBUGGING) != 0)
4943 || bfd_is_abs_section (o->output_section))
4944 continue;
4945
4946 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4947 info->keep_memory);
4948 if (internal_relocs == NULL)
4949 goto error_return;
4950
66eb6687 4951 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4952
4953 if (elf_section_data (o)->relocs != internal_relocs)
4954 free (internal_relocs);
4955
4956 if (! ok)
4957 goto error_return;
4958 }
4959 }
4960
4961 /* If this is a non-traditional link, try to optimize the handling
4962 of the .stab/.stabstr sections. */
4963 if (! dynamic
4964 && ! info->traditional_format
66eb6687 4965 && is_elf_hash_table (htab)
4ad4eba5
AM
4966 && (info->strip != strip_all && info->strip != strip_debugger))
4967 {
4968 asection *stabstr;
4969
4970 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4971 if (stabstr != NULL)
4972 {
4973 bfd_size_type string_offset = 0;
4974 asection *stab;
4975
4976 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4977 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4978 && (!stab->name[5] ||
4979 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4980 && (stab->flags & SEC_MERGE) == 0
4981 && !bfd_is_abs_section (stab->output_section))
4982 {
4983 struct bfd_elf_section_data *secdata;
4984
4985 secdata = elf_section_data (stab);
66eb6687
AM
4986 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4987 stabstr, &secdata->sec_info,
4ad4eba5
AM
4988 &string_offset))
4989 goto error_return;
4990 if (secdata->sec_info)
dbaa2011 4991 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
4992 }
4993 }
4994 }
4995
66eb6687 4996 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4997 {
4998 /* Add this bfd to the loaded list. */
4999 struct elf_link_loaded_list *n;
5000
ca4be51c 5001 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5002 if (n == NULL)
5003 goto error_return;
5004 n->abfd = abfd;
66eb6687
AM
5005 n->next = htab->loaded;
5006 htab->loaded = n;
4ad4eba5
AM
5007 }
5008
5009 return TRUE;
5010
5011 error_free_vers:
66eb6687
AM
5012 if (old_tab != NULL)
5013 free (old_tab);
4ad4eba5
AM
5014 if (nondeflt_vers != NULL)
5015 free (nondeflt_vers);
5016 if (extversym != NULL)
5017 free (extversym);
5018 error_free_sym:
5019 if (isymbuf != NULL)
5020 free (isymbuf);
5021 error_return:
5022 return FALSE;
5023}
5024
8387904d
AM
5025/* Return the linker hash table entry of a symbol that might be
5026 satisfied by an archive symbol. Return -1 on error. */
5027
5028struct elf_link_hash_entry *
5029_bfd_elf_archive_symbol_lookup (bfd *abfd,
5030 struct bfd_link_info *info,
5031 const char *name)
5032{
5033 struct elf_link_hash_entry *h;
5034 char *p, *copy;
5035 size_t len, first;
5036
2a41f396 5037 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5038 if (h != NULL)
5039 return h;
5040
5041 /* If this is a default version (the name contains @@), look up the
5042 symbol again with only one `@' as well as without the version.
5043 The effect is that references to the symbol with and without the
5044 version will be matched by the default symbol in the archive. */
5045
5046 p = strchr (name, ELF_VER_CHR);
5047 if (p == NULL || p[1] != ELF_VER_CHR)
5048 return h;
5049
5050 /* First check with only one `@'. */
5051 len = strlen (name);
a50b1753 5052 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5053 if (copy == NULL)
5054 return (struct elf_link_hash_entry *) 0 - 1;
5055
5056 first = p - name + 1;
5057 memcpy (copy, name, first);
5058 memcpy (copy + first, name + first + 1, len - first);
5059
2a41f396 5060 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5061 if (h == NULL)
5062 {
5063 /* We also need to check references to the symbol without the
5064 version. */
5065 copy[first - 1] = '\0';
5066 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5067 FALSE, FALSE, TRUE);
8387904d
AM
5068 }
5069
5070 bfd_release (abfd, copy);
5071 return h;
5072}
5073
0ad989f9 5074/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5075 don't use _bfd_generic_link_add_archive_symbols because we need to
5076 handle versioned symbols.
0ad989f9
L
5077
5078 Fortunately, ELF archive handling is simpler than that done by
5079 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5080 oddities. In ELF, if we find a symbol in the archive map, and the
5081 symbol is currently undefined, we know that we must pull in that
5082 object file.
5083
5084 Unfortunately, we do have to make multiple passes over the symbol
5085 table until nothing further is resolved. */
5086
4ad4eba5
AM
5087static bfd_boolean
5088elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5089{
5090 symindex c;
13e570f8 5091 unsigned char *included = NULL;
0ad989f9
L
5092 carsym *symdefs;
5093 bfd_boolean loop;
5094 bfd_size_type amt;
8387904d
AM
5095 const struct elf_backend_data *bed;
5096 struct elf_link_hash_entry * (*archive_symbol_lookup)
5097 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5098
5099 if (! bfd_has_map (abfd))
5100 {
5101 /* An empty archive is a special case. */
5102 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5103 return TRUE;
5104 bfd_set_error (bfd_error_no_armap);
5105 return FALSE;
5106 }
5107
5108 /* Keep track of all symbols we know to be already defined, and all
5109 files we know to be already included. This is to speed up the
5110 second and subsequent passes. */
5111 c = bfd_ardata (abfd)->symdef_count;
5112 if (c == 0)
5113 return TRUE;
5114 amt = c;
13e570f8
AM
5115 amt *= sizeof (*included);
5116 included = (unsigned char *) bfd_zmalloc (amt);
5117 if (included == NULL)
5118 return FALSE;
0ad989f9
L
5119
5120 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5121 bed = get_elf_backend_data (abfd);
5122 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5123
5124 do
5125 {
5126 file_ptr last;
5127 symindex i;
5128 carsym *symdef;
5129 carsym *symdefend;
5130
5131 loop = FALSE;
5132 last = -1;
5133
5134 symdef = symdefs;
5135 symdefend = symdef + c;
5136 for (i = 0; symdef < symdefend; symdef++, i++)
5137 {
5138 struct elf_link_hash_entry *h;
5139 bfd *element;
5140 struct bfd_link_hash_entry *undefs_tail;
5141 symindex mark;
5142
13e570f8 5143 if (included[i])
0ad989f9
L
5144 continue;
5145 if (symdef->file_offset == last)
5146 {
5147 included[i] = TRUE;
5148 continue;
5149 }
5150
8387904d
AM
5151 h = archive_symbol_lookup (abfd, info, symdef->name);
5152 if (h == (struct elf_link_hash_entry *) 0 - 1)
5153 goto error_return;
0ad989f9
L
5154
5155 if (h == NULL)
5156 continue;
5157
5158 if (h->root.type == bfd_link_hash_common)
5159 {
5160 /* We currently have a common symbol. The archive map contains
5161 a reference to this symbol, so we may want to include it. We
5162 only want to include it however, if this archive element
5163 contains a definition of the symbol, not just another common
5164 declaration of it.
5165
5166 Unfortunately some archivers (including GNU ar) will put
5167 declarations of common symbols into their archive maps, as
5168 well as real definitions, so we cannot just go by the archive
5169 map alone. Instead we must read in the element's symbol
5170 table and check that to see what kind of symbol definition
5171 this is. */
5172 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5173 continue;
5174 }
5175 else if (h->root.type != bfd_link_hash_undefined)
5176 {
5177 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5178 /* Symbol must be defined. Don't check it again. */
5179 included[i] = TRUE;
0ad989f9
L
5180 continue;
5181 }
5182
5183 /* We need to include this archive member. */
5184 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5185 if (element == NULL)
5186 goto error_return;
5187
5188 if (! bfd_check_format (element, bfd_object))
5189 goto error_return;
5190
0ad989f9
L
5191 undefs_tail = info->hash->undefs_tail;
5192
0e144ba7
AM
5193 if (!(*info->callbacks
5194 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5195 goto error_return;
0e144ba7 5196 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5197 goto error_return;
5198
5199 /* If there are any new undefined symbols, we need to make
5200 another pass through the archive in order to see whether
5201 they can be defined. FIXME: This isn't perfect, because
5202 common symbols wind up on undefs_tail and because an
5203 undefined symbol which is defined later on in this pass
5204 does not require another pass. This isn't a bug, but it
5205 does make the code less efficient than it could be. */
5206 if (undefs_tail != info->hash->undefs_tail)
5207 loop = TRUE;
5208
5209 /* Look backward to mark all symbols from this object file
5210 which we have already seen in this pass. */
5211 mark = i;
5212 do
5213 {
5214 included[mark] = TRUE;
5215 if (mark == 0)
5216 break;
5217 --mark;
5218 }
5219 while (symdefs[mark].file_offset == symdef->file_offset);
5220
5221 /* We mark subsequent symbols from this object file as we go
5222 on through the loop. */
5223 last = symdef->file_offset;
5224 }
5225 }
5226 while (loop);
5227
0ad989f9
L
5228 free (included);
5229
5230 return TRUE;
5231
5232 error_return:
0ad989f9
L
5233 if (included != NULL)
5234 free (included);
5235 return FALSE;
5236}
4ad4eba5
AM
5237
5238/* Given an ELF BFD, add symbols to the global hash table as
5239 appropriate. */
5240
5241bfd_boolean
5242bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5243{
5244 switch (bfd_get_format (abfd))
5245 {
5246 case bfd_object:
5247 return elf_link_add_object_symbols (abfd, info);
5248 case bfd_archive:
5249 return elf_link_add_archive_symbols (abfd, info);
5250 default:
5251 bfd_set_error (bfd_error_wrong_format);
5252 return FALSE;
5253 }
5254}
5a580b3a 5255\f
14b1c01e
AM
5256struct hash_codes_info
5257{
5258 unsigned long *hashcodes;
5259 bfd_boolean error;
5260};
a0c8462f 5261
5a580b3a
AM
5262/* This function will be called though elf_link_hash_traverse to store
5263 all hash value of the exported symbols in an array. */
5264
5265static bfd_boolean
5266elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5267{
a50b1753 5268 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5269 const char *name;
5a580b3a
AM
5270 unsigned long ha;
5271 char *alc = NULL;
5272
5a580b3a
AM
5273 /* Ignore indirect symbols. These are added by the versioning code. */
5274 if (h->dynindx == -1)
5275 return TRUE;
5276
5277 name = h->root.root.string;
422f1182 5278 if (h->versioned >= versioned)
5a580b3a 5279 {
422f1182
L
5280 char *p = strchr (name, ELF_VER_CHR);
5281 if (p != NULL)
14b1c01e 5282 {
422f1182
L
5283 alc = (char *) bfd_malloc (p - name + 1);
5284 if (alc == NULL)
5285 {
5286 inf->error = TRUE;
5287 return FALSE;
5288 }
5289 memcpy (alc, name, p - name);
5290 alc[p - name] = '\0';
5291 name = alc;
14b1c01e 5292 }
5a580b3a
AM
5293 }
5294
5295 /* Compute the hash value. */
5296 ha = bfd_elf_hash (name);
5297
5298 /* Store the found hash value in the array given as the argument. */
14b1c01e 5299 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5300
5301 /* And store it in the struct so that we can put it in the hash table
5302 later. */
f6e332e6 5303 h->u.elf_hash_value = ha;
5a580b3a
AM
5304
5305 if (alc != NULL)
5306 free (alc);
5307
5308 return TRUE;
5309}
5310
fdc90cb4
JJ
5311struct collect_gnu_hash_codes
5312{
5313 bfd *output_bfd;
5314 const struct elf_backend_data *bed;
5315 unsigned long int nsyms;
5316 unsigned long int maskbits;
5317 unsigned long int *hashcodes;
5318 unsigned long int *hashval;
5319 unsigned long int *indx;
5320 unsigned long int *counts;
5321 bfd_vma *bitmask;
5322 bfd_byte *contents;
5323 long int min_dynindx;
5324 unsigned long int bucketcount;
5325 unsigned long int symindx;
5326 long int local_indx;
5327 long int shift1, shift2;
5328 unsigned long int mask;
14b1c01e 5329 bfd_boolean error;
fdc90cb4
JJ
5330};
5331
5332/* This function will be called though elf_link_hash_traverse to store
5333 all hash value of the exported symbols in an array. */
5334
5335static bfd_boolean
5336elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5337{
a50b1753 5338 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5339 const char *name;
fdc90cb4
JJ
5340 unsigned long ha;
5341 char *alc = NULL;
5342
fdc90cb4
JJ
5343 /* Ignore indirect symbols. These are added by the versioning code. */
5344 if (h->dynindx == -1)
5345 return TRUE;
5346
5347 /* Ignore also local symbols and undefined symbols. */
5348 if (! (*s->bed->elf_hash_symbol) (h))
5349 return TRUE;
5350
5351 name = h->root.root.string;
422f1182 5352 if (h->versioned >= versioned)
fdc90cb4 5353 {
422f1182
L
5354 char *p = strchr (name, ELF_VER_CHR);
5355 if (p != NULL)
14b1c01e 5356 {
422f1182
L
5357 alc = (char *) bfd_malloc (p - name + 1);
5358 if (alc == NULL)
5359 {
5360 s->error = TRUE;
5361 return FALSE;
5362 }
5363 memcpy (alc, name, p - name);
5364 alc[p - name] = '\0';
5365 name = alc;
14b1c01e 5366 }
fdc90cb4
JJ
5367 }
5368
5369 /* Compute the hash value. */
5370 ha = bfd_elf_gnu_hash (name);
5371
5372 /* Store the found hash value in the array for compute_bucket_count,
5373 and also for .dynsym reordering purposes. */
5374 s->hashcodes[s->nsyms] = ha;
5375 s->hashval[h->dynindx] = ha;
5376 ++s->nsyms;
5377 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5378 s->min_dynindx = h->dynindx;
5379
5380 if (alc != NULL)
5381 free (alc);
5382
5383 return TRUE;
5384}
5385
5386/* This function will be called though elf_link_hash_traverse to do
5387 final dynaminc symbol renumbering. */
5388
5389static bfd_boolean
5390elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5391{
a50b1753 5392 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5393 unsigned long int bucket;
5394 unsigned long int val;
5395
fdc90cb4
JJ
5396 /* Ignore indirect symbols. */
5397 if (h->dynindx == -1)
5398 return TRUE;
5399
5400 /* Ignore also local symbols and undefined symbols. */
5401 if (! (*s->bed->elf_hash_symbol) (h))
5402 {
5403 if (h->dynindx >= s->min_dynindx)
5404 h->dynindx = s->local_indx++;
5405 return TRUE;
5406 }
5407
5408 bucket = s->hashval[h->dynindx] % s->bucketcount;
5409 val = (s->hashval[h->dynindx] >> s->shift1)
5410 & ((s->maskbits >> s->shift1) - 1);
5411 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5412 s->bitmask[val]
5413 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5414 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5415 if (s->counts[bucket] == 1)
5416 /* Last element terminates the chain. */
5417 val |= 1;
5418 bfd_put_32 (s->output_bfd, val,
5419 s->contents + (s->indx[bucket] - s->symindx) * 4);
5420 --s->counts[bucket];
5421 h->dynindx = s->indx[bucket]++;
5422 return TRUE;
5423}
5424
5425/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5426
5427bfd_boolean
5428_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5429{
5430 return !(h->forced_local
5431 || h->root.type == bfd_link_hash_undefined
5432 || h->root.type == bfd_link_hash_undefweak
5433 || ((h->root.type == bfd_link_hash_defined
5434 || h->root.type == bfd_link_hash_defweak)
5435 && h->root.u.def.section->output_section == NULL));
5436}
5437
5a580b3a
AM
5438/* Array used to determine the number of hash table buckets to use
5439 based on the number of symbols there are. If there are fewer than
5440 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5441 fewer than 37 we use 17 buckets, and so forth. We never use more
5442 than 32771 buckets. */
5443
5444static const size_t elf_buckets[] =
5445{
5446 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5447 16411, 32771, 0
5448};
5449
5450/* Compute bucket count for hashing table. We do not use a static set
5451 of possible tables sizes anymore. Instead we determine for all
5452 possible reasonable sizes of the table the outcome (i.e., the
5453 number of collisions etc) and choose the best solution. The
5454 weighting functions are not too simple to allow the table to grow
5455 without bounds. Instead one of the weighting factors is the size.
5456 Therefore the result is always a good payoff between few collisions
5457 (= short chain lengths) and table size. */
5458static size_t
b20dd2ce 5459compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5460 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5461 unsigned long int nsyms,
5462 int gnu_hash)
5a580b3a 5463{
5a580b3a 5464 size_t best_size = 0;
5a580b3a 5465 unsigned long int i;
5a580b3a 5466
5a580b3a
AM
5467 /* We have a problem here. The following code to optimize the table
5468 size requires an integer type with more the 32 bits. If
5469 BFD_HOST_U_64_BIT is set we know about such a type. */
5470#ifdef BFD_HOST_U_64_BIT
5471 if (info->optimize)
5472 {
5a580b3a
AM
5473 size_t minsize;
5474 size_t maxsize;
5475 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5476 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5477 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5478 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5479 unsigned long int *counts;
d40f3da9 5480 bfd_size_type amt;
0883b6e0 5481 unsigned int no_improvement_count = 0;
5a580b3a
AM
5482
5483 /* Possible optimization parameters: if we have NSYMS symbols we say
5484 that the hashing table must at least have NSYMS/4 and at most
5485 2*NSYMS buckets. */
5486 minsize = nsyms / 4;
5487 if (minsize == 0)
5488 minsize = 1;
5489 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5490 if (gnu_hash)
5491 {
5492 if (minsize < 2)
5493 minsize = 2;
5494 if ((best_size & 31) == 0)
5495 ++best_size;
5496 }
5a580b3a
AM
5497
5498 /* Create array where we count the collisions in. We must use bfd_malloc
5499 since the size could be large. */
5500 amt = maxsize;
5501 amt *= sizeof (unsigned long int);
a50b1753 5502 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5503 if (counts == NULL)
fdc90cb4 5504 return 0;
5a580b3a
AM
5505
5506 /* Compute the "optimal" size for the hash table. The criteria is a
5507 minimal chain length. The minor criteria is (of course) the size
5508 of the table. */
5509 for (i = minsize; i < maxsize; ++i)
5510 {
5511 /* Walk through the array of hashcodes and count the collisions. */
5512 BFD_HOST_U_64_BIT max;
5513 unsigned long int j;
5514 unsigned long int fact;
5515
fdc90cb4
JJ
5516 if (gnu_hash && (i & 31) == 0)
5517 continue;
5518
5a580b3a
AM
5519 memset (counts, '\0', i * sizeof (unsigned long int));
5520
5521 /* Determine how often each hash bucket is used. */
5522 for (j = 0; j < nsyms; ++j)
5523 ++counts[hashcodes[j] % i];
5524
5525 /* For the weight function we need some information about the
5526 pagesize on the target. This is information need not be 100%
5527 accurate. Since this information is not available (so far) we
5528 define it here to a reasonable default value. If it is crucial
5529 to have a better value some day simply define this value. */
5530# ifndef BFD_TARGET_PAGESIZE
5531# define BFD_TARGET_PAGESIZE (4096)
5532# endif
5533
fdc90cb4
JJ
5534 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5535 and the chains. */
5536 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5537
5538# if 1
5539 /* Variant 1: optimize for short chains. We add the squares
5540 of all the chain lengths (which favors many small chain
5541 over a few long chains). */
5542 for (j = 0; j < i; ++j)
5543 max += counts[j] * counts[j];
5544
5545 /* This adds penalties for the overall size of the table. */
fdc90cb4 5546 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5547 max *= fact * fact;
5548# else
5549 /* Variant 2: Optimize a lot more for small table. Here we
5550 also add squares of the size but we also add penalties for
5551 empty slots (the +1 term). */
5552 for (j = 0; j < i; ++j)
5553 max += (1 + counts[j]) * (1 + counts[j]);
5554
5555 /* The overall size of the table is considered, but not as
5556 strong as in variant 1, where it is squared. */
fdc90cb4 5557 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5558 max *= fact;
5559# endif
5560
5561 /* Compare with current best results. */
5562 if (max < best_chlen)
5563 {
5564 best_chlen = max;
5565 best_size = i;
ca4be51c 5566 no_improvement_count = 0;
5a580b3a 5567 }
0883b6e0
NC
5568 /* PR 11843: Avoid futile long searches for the best bucket size
5569 when there are a large number of symbols. */
5570 else if (++no_improvement_count == 100)
5571 break;
5a580b3a
AM
5572 }
5573
5574 free (counts);
5575 }
5576 else
5577#endif /* defined (BFD_HOST_U_64_BIT) */
5578 {
5579 /* This is the fallback solution if no 64bit type is available or if we
5580 are not supposed to spend much time on optimizations. We select the
5581 bucket count using a fixed set of numbers. */
5582 for (i = 0; elf_buckets[i] != 0; i++)
5583 {
5584 best_size = elf_buckets[i];
fdc90cb4 5585 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5586 break;
5587 }
fdc90cb4
JJ
5588 if (gnu_hash && best_size < 2)
5589 best_size = 2;
5a580b3a
AM
5590 }
5591
5a580b3a
AM
5592 return best_size;
5593}
5594
d0bf826b
AM
5595/* Size any SHT_GROUP section for ld -r. */
5596
5597bfd_boolean
5598_bfd_elf_size_group_sections (struct bfd_link_info *info)
5599{
5600 bfd *ibfd;
5601
c72f2fb2 5602 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5603 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5604 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5605 return FALSE;
5606 return TRUE;
5607}
5608
04c3a755
NS
5609/* Set a default stack segment size. The value in INFO wins. If it
5610 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5611 undefined it is initialized. */
5612
5613bfd_boolean
5614bfd_elf_stack_segment_size (bfd *output_bfd,
5615 struct bfd_link_info *info,
5616 const char *legacy_symbol,
5617 bfd_vma default_size)
5618{
5619 struct elf_link_hash_entry *h = NULL;
5620
5621 /* Look for legacy symbol. */
5622 if (legacy_symbol)
5623 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5624 FALSE, FALSE, FALSE);
5625 if (h && (h->root.type == bfd_link_hash_defined
5626 || h->root.type == bfd_link_hash_defweak)
5627 && h->def_regular
5628 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5629 {
5630 /* The symbol has no type if specified on the command line. */
5631 h->type = STT_OBJECT;
5632 if (info->stacksize)
5633 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5634 output_bfd, legacy_symbol);
5635 else if (h->root.u.def.section != bfd_abs_section_ptr)
5636 (*_bfd_error_handler) (_("%B: %s not absolute"),
5637 output_bfd, legacy_symbol);
5638 else
5639 info->stacksize = h->root.u.def.value;
5640 }
5641
5642 if (!info->stacksize)
5643 /* If the user didn't set a size, or explicitly inhibit the
5644 size, set it now. */
5645 info->stacksize = default_size;
5646
5647 /* Provide the legacy symbol, if it is referenced. */
5648 if (h && (h->root.type == bfd_link_hash_undefined
5649 || h->root.type == bfd_link_hash_undefweak))
5650 {
5651 struct bfd_link_hash_entry *bh = NULL;
5652
5653 if (!(_bfd_generic_link_add_one_symbol
5654 (info, output_bfd, legacy_symbol,
5655 BSF_GLOBAL, bfd_abs_section_ptr,
5656 info->stacksize >= 0 ? info->stacksize : 0,
5657 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5658 return FALSE;
5659
5660 h = (struct elf_link_hash_entry *) bh;
5661 h->def_regular = 1;
5662 h->type = STT_OBJECT;
5663 }
5664
5665 return TRUE;
5666}
5667
5a580b3a
AM
5668/* Set up the sizes and contents of the ELF dynamic sections. This is
5669 called by the ELF linker emulation before_allocation routine. We
5670 must set the sizes of the sections before the linker sets the
5671 addresses of the various sections. */
5672
5673bfd_boolean
5674bfd_elf_size_dynamic_sections (bfd *output_bfd,
5675 const char *soname,
5676 const char *rpath,
5677 const char *filter_shlib,
7ee314fa
AM
5678 const char *audit,
5679 const char *depaudit,
5a580b3a
AM
5680 const char * const *auxiliary_filters,
5681 struct bfd_link_info *info,
fd91d419 5682 asection **sinterpptr)
5a580b3a
AM
5683{
5684 bfd_size_type soname_indx;
5685 bfd *dynobj;
5686 const struct elf_backend_data *bed;
28caa186 5687 struct elf_info_failed asvinfo;
5a580b3a
AM
5688
5689 *sinterpptr = NULL;
5690
5691 soname_indx = (bfd_size_type) -1;
5692
5693 if (!is_elf_hash_table (info->hash))
5694 return TRUE;
5695
6bfdb61b 5696 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5697
5698 /* Any syms created from now on start with -1 in
5699 got.refcount/offset and plt.refcount/offset. */
5700 elf_hash_table (info)->init_got_refcount
5701 = elf_hash_table (info)->init_got_offset;
5702 elf_hash_table (info)->init_plt_refcount
5703 = elf_hash_table (info)->init_plt_offset;
5704
0e1862bb 5705 if (bfd_link_relocatable (info)
04c3a755
NS
5706 && !_bfd_elf_size_group_sections (info))
5707 return FALSE;
5708
5709 /* The backend may have to create some sections regardless of whether
5710 we're dynamic or not. */
5711 if (bed->elf_backend_always_size_sections
5712 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5713 return FALSE;
5714
5715 /* Determine any GNU_STACK segment requirements, after the backend
5716 has had a chance to set a default segment size. */
5a580b3a 5717 if (info->execstack)
12bd6957 5718 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5719 else if (info->noexecstack)
12bd6957 5720 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5721 else
5722 {
5723 bfd *inputobj;
5724 asection *notesec = NULL;
5725 int exec = 0;
5726
5727 for (inputobj = info->input_bfds;
5728 inputobj;
c72f2fb2 5729 inputobj = inputobj->link.next)
5a580b3a
AM
5730 {
5731 asection *s;
5732
a92c088a
L
5733 if (inputobj->flags
5734 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5735 continue;
5736 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5737 if (s)
5738 {
5739 if (s->flags & SEC_CODE)
5740 exec = PF_X;
5741 notesec = s;
5742 }
6bfdb61b 5743 else if (bed->default_execstack)
5a580b3a
AM
5744 exec = PF_X;
5745 }
04c3a755 5746 if (notesec || info->stacksize > 0)
12bd6957 5747 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5748 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5749 && notesec->output_section != bfd_abs_section_ptr)
5750 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5751 }
5752
5a580b3a
AM
5753 dynobj = elf_hash_table (info)->dynobj;
5754
9a2a56cc 5755 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5756 {
5757 struct elf_info_failed eif;
5758 struct elf_link_hash_entry *h;
5759 asection *dynstr;
5760 struct bfd_elf_version_tree *t;
5761 struct bfd_elf_version_expr *d;
046183de 5762 asection *s;
5a580b3a
AM
5763 bfd_boolean all_defined;
5764
3d4d4302 5765 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5766 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5767
5768 if (soname != NULL)
5769 {
5770 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5771 soname, TRUE);
5772 if (soname_indx == (bfd_size_type) -1
5773 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5774 return FALSE;
5775 }
5776
5777 if (info->symbolic)
5778 {
5779 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5780 return FALSE;
5781 info->flags |= DF_SYMBOLIC;
5782 }
5783
5784 if (rpath != NULL)
5785 {
5786 bfd_size_type indx;
b1b00fcc 5787 bfd_vma tag;
5a580b3a
AM
5788
5789 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5790 TRUE);
b1b00fcc 5791 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5792 return FALSE;
5793
b1b00fcc
MF
5794 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5795 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5796 return FALSE;
5a580b3a
AM
5797 }
5798
5799 if (filter_shlib != NULL)
5800 {
5801 bfd_size_type indx;
5802
5803 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5804 filter_shlib, TRUE);
5805 if (indx == (bfd_size_type) -1
5806 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5807 return FALSE;
5808 }
5809
5810 if (auxiliary_filters != NULL)
5811 {
5812 const char * const *p;
5813
5814 for (p = auxiliary_filters; *p != NULL; p++)
5815 {
5816 bfd_size_type indx;
5817
5818 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5819 *p, TRUE);
5820 if (indx == (bfd_size_type) -1
5821 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5822 return FALSE;
5823 }
5824 }
5825
7ee314fa
AM
5826 if (audit != NULL)
5827 {
5828 bfd_size_type indx;
5829
5830 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5831 TRUE);
5832 if (indx == (bfd_size_type) -1
5833 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5834 return FALSE;
5835 }
5836
5837 if (depaudit != NULL)
5838 {
5839 bfd_size_type indx;
5840
5841 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5842 TRUE);
5843 if (indx == (bfd_size_type) -1
5844 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5845 return FALSE;
5846 }
5847
5a580b3a 5848 eif.info = info;
5a580b3a
AM
5849 eif.failed = FALSE;
5850
5851 /* If we are supposed to export all symbols into the dynamic symbol
5852 table (this is not the normal case), then do so. */
55255dae 5853 if (info->export_dynamic
0e1862bb 5854 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5855 {
5856 elf_link_hash_traverse (elf_hash_table (info),
5857 _bfd_elf_export_symbol,
5858 &eif);
5859 if (eif.failed)
5860 return FALSE;
5861 }
5862
5863 /* Make all global versions with definition. */
fd91d419 5864 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5865 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5866 if (!d->symver && d->literal)
5a580b3a
AM
5867 {
5868 const char *verstr, *name;
5869 size_t namelen, verlen, newlen;
93252b1c 5870 char *newname, *p, leading_char;
5a580b3a
AM
5871 struct elf_link_hash_entry *newh;
5872
93252b1c 5873 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5874 name = d->pattern;
93252b1c 5875 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5876 verstr = t->name;
5877 verlen = strlen (verstr);
5878 newlen = namelen + verlen + 3;
5879
a50b1753 5880 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5881 if (newname == NULL)
5882 return FALSE;
93252b1c
MF
5883 newname[0] = leading_char;
5884 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5885
5886 /* Check the hidden versioned definition. */
5887 p = newname + namelen;
5888 *p++ = ELF_VER_CHR;
5889 memcpy (p, verstr, verlen + 1);
5890 newh = elf_link_hash_lookup (elf_hash_table (info),
5891 newname, FALSE, FALSE,
5892 FALSE);
5893 if (newh == NULL
5894 || (newh->root.type != bfd_link_hash_defined
5895 && newh->root.type != bfd_link_hash_defweak))
5896 {
5897 /* Check the default versioned definition. */
5898 *p++ = ELF_VER_CHR;
5899 memcpy (p, verstr, verlen + 1);
5900 newh = elf_link_hash_lookup (elf_hash_table (info),
5901 newname, FALSE, FALSE,
5902 FALSE);
5903 }
5904 free (newname);
5905
5906 /* Mark this version if there is a definition and it is
5907 not defined in a shared object. */
5908 if (newh != NULL
f5385ebf 5909 && !newh->def_dynamic
5a580b3a
AM
5910 && (newh->root.type == bfd_link_hash_defined
5911 || newh->root.type == bfd_link_hash_defweak))
5912 d->symver = 1;
5913 }
5914
5915 /* Attach all the symbols to their version information. */
5a580b3a 5916 asvinfo.info = info;
5a580b3a
AM
5917 asvinfo.failed = FALSE;
5918
5919 elf_link_hash_traverse (elf_hash_table (info),
5920 _bfd_elf_link_assign_sym_version,
5921 &asvinfo);
5922 if (asvinfo.failed)
5923 return FALSE;
5924
5925 if (!info->allow_undefined_version)
5926 {
5927 /* Check if all global versions have a definition. */
5928 all_defined = TRUE;
fd91d419 5929 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5930 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5931 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5932 {
5933 (*_bfd_error_handler)
5934 (_("%s: undefined version: %s"),
5935 d->pattern, t->name);
5936 all_defined = FALSE;
5937 }
5938
5939 if (!all_defined)
5940 {
5941 bfd_set_error (bfd_error_bad_value);
5942 return FALSE;
5943 }
5944 }
5945
5946 /* Find all symbols which were defined in a dynamic object and make
5947 the backend pick a reasonable value for them. */
5948 elf_link_hash_traverse (elf_hash_table (info),
5949 _bfd_elf_adjust_dynamic_symbol,
5950 &eif);
5951 if (eif.failed)
5952 return FALSE;
5953
5954 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5955 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5956 now so that we know the final size of the .dynamic section. */
5957
5958 /* If there are initialization and/or finalization functions to
5959 call then add the corresponding DT_INIT/DT_FINI entries. */
5960 h = (info->init_function
5961 ? elf_link_hash_lookup (elf_hash_table (info),
5962 info->init_function, FALSE,
5963 FALSE, FALSE)
5964 : NULL);
5965 if (h != NULL
f5385ebf
AM
5966 && (h->ref_regular
5967 || h->def_regular))
5a580b3a
AM
5968 {
5969 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5970 return FALSE;
5971 }
5972 h = (info->fini_function
5973 ? elf_link_hash_lookup (elf_hash_table (info),
5974 info->fini_function, FALSE,
5975 FALSE, FALSE)
5976 : NULL);
5977 if (h != NULL
f5385ebf
AM
5978 && (h->ref_regular
5979 || h->def_regular))
5a580b3a
AM
5980 {
5981 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5982 return FALSE;
5983 }
5984
046183de
AM
5985 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5986 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5987 {
5988 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 5989 if (! bfd_link_executable (info))
5a580b3a
AM
5990 {
5991 bfd *sub;
5992 asection *o;
5993
5994 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 5995 sub = sub->link.next)
3fcd97f1
JJ
5996 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5997 for (o = sub->sections; o != NULL; o = o->next)
5998 if (elf_section_data (o)->this_hdr.sh_type
5999 == SHT_PREINIT_ARRAY)
6000 {
6001 (*_bfd_error_handler)
6002 (_("%B: .preinit_array section is not allowed in DSO"),
6003 sub);
6004 break;
6005 }
5a580b3a
AM
6006
6007 bfd_set_error (bfd_error_nonrepresentable_section);
6008 return FALSE;
6009 }
6010
6011 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6012 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6013 return FALSE;
6014 }
046183de
AM
6015 s = bfd_get_section_by_name (output_bfd, ".init_array");
6016 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6017 {
6018 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6019 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6020 return FALSE;
6021 }
046183de
AM
6022 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6023 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6024 {
6025 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6026 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6027 return FALSE;
6028 }
6029
3d4d4302 6030 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6031 /* If .dynstr is excluded from the link, we don't want any of
6032 these tags. Strictly, we should be checking each section
6033 individually; This quick check covers for the case where
6034 someone does a /DISCARD/ : { *(*) }. */
6035 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6036 {
6037 bfd_size_type strsize;
6038
6039 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6040 if ((info->emit_hash
6041 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6042 || (info->emit_gnu_hash
6043 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6044 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6045 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6046 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6047 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6048 bed->s->sizeof_sym))
6049 return FALSE;
6050 }
6051 }
6052
de231f20
CM
6053 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6054 return FALSE;
6055
5a580b3a
AM
6056 /* The backend must work out the sizes of all the other dynamic
6057 sections. */
9a2a56cc
AM
6058 if (dynobj != NULL
6059 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6060 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6061 return FALSE;
6062
9a2a56cc 6063 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6064 {
554220db 6065 unsigned long section_sym_count;
fd91d419 6066 struct bfd_elf_version_tree *verdefs;
5a580b3a 6067 asection *s;
5a580b3a
AM
6068
6069 /* Set up the version definition section. */
3d4d4302 6070 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6071 BFD_ASSERT (s != NULL);
6072
6073 /* We may have created additional version definitions if we are
6074 just linking a regular application. */
fd91d419 6075 verdefs = info->version_info;
5a580b3a
AM
6076
6077 /* Skip anonymous version tag. */
6078 if (verdefs != NULL && verdefs->vernum == 0)
6079 verdefs = verdefs->next;
6080
3e3b46e5 6081 if (verdefs == NULL && !info->create_default_symver)
8423293d 6082 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6083 else
6084 {
6085 unsigned int cdefs;
6086 bfd_size_type size;
6087 struct bfd_elf_version_tree *t;
6088 bfd_byte *p;
6089 Elf_Internal_Verdef def;
6090 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6091 struct bfd_link_hash_entry *bh;
6092 struct elf_link_hash_entry *h;
6093 const char *name;
5a580b3a
AM
6094
6095 cdefs = 0;
6096 size = 0;
6097
6098 /* Make space for the base version. */
6099 size += sizeof (Elf_External_Verdef);
6100 size += sizeof (Elf_External_Verdaux);
6101 ++cdefs;
6102
3e3b46e5
PB
6103 /* Make space for the default version. */
6104 if (info->create_default_symver)
6105 {
6106 size += sizeof (Elf_External_Verdef);
6107 ++cdefs;
6108 }
6109
5a580b3a
AM
6110 for (t = verdefs; t != NULL; t = t->next)
6111 {
6112 struct bfd_elf_version_deps *n;
6113
a6cc6b3b
RO
6114 /* Don't emit base version twice. */
6115 if (t->vernum == 0)
6116 continue;
6117
5a580b3a
AM
6118 size += sizeof (Elf_External_Verdef);
6119 size += sizeof (Elf_External_Verdaux);
6120 ++cdefs;
6121
6122 for (n = t->deps; n != NULL; n = n->next)
6123 size += sizeof (Elf_External_Verdaux);
6124 }
6125
eea6121a 6126 s->size = size;
a50b1753 6127 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6128 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6129 return FALSE;
6130
6131 /* Fill in the version definition section. */
6132
6133 p = s->contents;
6134
6135 def.vd_version = VER_DEF_CURRENT;
6136 def.vd_flags = VER_FLG_BASE;
6137 def.vd_ndx = 1;
6138 def.vd_cnt = 1;
3e3b46e5
PB
6139 if (info->create_default_symver)
6140 {
6141 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6142 def.vd_next = sizeof (Elf_External_Verdef);
6143 }
6144 else
6145 {
6146 def.vd_aux = sizeof (Elf_External_Verdef);
6147 def.vd_next = (sizeof (Elf_External_Verdef)
6148 + sizeof (Elf_External_Verdaux));
6149 }
5a580b3a
AM
6150
6151 if (soname_indx != (bfd_size_type) -1)
6152 {
6153 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6154 soname_indx);
6155 def.vd_hash = bfd_elf_hash (soname);
6156 defaux.vda_name = soname_indx;
3e3b46e5 6157 name = soname;
5a580b3a
AM
6158 }
6159 else
6160 {
5a580b3a
AM
6161 bfd_size_type indx;
6162
06084812 6163 name = lbasename (output_bfd->filename);
5a580b3a
AM
6164 def.vd_hash = bfd_elf_hash (name);
6165 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6166 name, FALSE);
6167 if (indx == (bfd_size_type) -1)
6168 return FALSE;
6169 defaux.vda_name = indx;
6170 }
6171 defaux.vda_next = 0;
6172
6173 _bfd_elf_swap_verdef_out (output_bfd, &def,
6174 (Elf_External_Verdef *) p);
6175 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6176 if (info->create_default_symver)
6177 {
6178 /* Add a symbol representing this version. */
6179 bh = NULL;
6180 if (! (_bfd_generic_link_add_one_symbol
6181 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6182 0, NULL, FALSE,
6183 get_elf_backend_data (dynobj)->collect, &bh)))
6184 return FALSE;
6185 h = (struct elf_link_hash_entry *) bh;
6186 h->non_elf = 0;
6187 h->def_regular = 1;
6188 h->type = STT_OBJECT;
6189 h->verinfo.vertree = NULL;
6190
6191 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6192 return FALSE;
6193
6194 /* Create a duplicate of the base version with the same
6195 aux block, but different flags. */
6196 def.vd_flags = 0;
6197 def.vd_ndx = 2;
6198 def.vd_aux = sizeof (Elf_External_Verdef);
6199 if (verdefs)
6200 def.vd_next = (sizeof (Elf_External_Verdef)
6201 + sizeof (Elf_External_Verdaux));
6202 else
6203 def.vd_next = 0;
6204 _bfd_elf_swap_verdef_out (output_bfd, &def,
6205 (Elf_External_Verdef *) p);
6206 p += sizeof (Elf_External_Verdef);
6207 }
5a580b3a
AM
6208 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6209 (Elf_External_Verdaux *) p);
6210 p += sizeof (Elf_External_Verdaux);
6211
6212 for (t = verdefs; t != NULL; t = t->next)
6213 {
6214 unsigned int cdeps;
6215 struct bfd_elf_version_deps *n;
5a580b3a 6216
a6cc6b3b
RO
6217 /* Don't emit the base version twice. */
6218 if (t->vernum == 0)
6219 continue;
6220
5a580b3a
AM
6221 cdeps = 0;
6222 for (n = t->deps; n != NULL; n = n->next)
6223 ++cdeps;
6224
6225 /* Add a symbol representing this version. */
6226 bh = NULL;
6227 if (! (_bfd_generic_link_add_one_symbol
6228 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6229 0, NULL, FALSE,
6230 get_elf_backend_data (dynobj)->collect, &bh)))
6231 return FALSE;
6232 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6233 h->non_elf = 0;
6234 h->def_regular = 1;
5a580b3a
AM
6235 h->type = STT_OBJECT;
6236 h->verinfo.vertree = t;
6237
c152c796 6238 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6239 return FALSE;
6240
6241 def.vd_version = VER_DEF_CURRENT;
6242 def.vd_flags = 0;
6243 if (t->globals.list == NULL
6244 && t->locals.list == NULL
6245 && ! t->used)
6246 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6247 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6248 def.vd_cnt = cdeps + 1;
6249 def.vd_hash = bfd_elf_hash (t->name);
6250 def.vd_aux = sizeof (Elf_External_Verdef);
6251 def.vd_next = 0;
a6cc6b3b
RO
6252
6253 /* If a basever node is next, it *must* be the last node in
6254 the chain, otherwise Verdef construction breaks. */
6255 if (t->next != NULL && t->next->vernum == 0)
6256 BFD_ASSERT (t->next->next == NULL);
6257
6258 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6259 def.vd_next = (sizeof (Elf_External_Verdef)
6260 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6261
6262 _bfd_elf_swap_verdef_out (output_bfd, &def,
6263 (Elf_External_Verdef *) p);
6264 p += sizeof (Elf_External_Verdef);
6265
6266 defaux.vda_name = h->dynstr_index;
6267 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6268 h->dynstr_index);
6269 defaux.vda_next = 0;
6270 if (t->deps != NULL)
6271 defaux.vda_next = sizeof (Elf_External_Verdaux);
6272 t->name_indx = defaux.vda_name;
6273
6274 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6275 (Elf_External_Verdaux *) p);
6276 p += sizeof (Elf_External_Verdaux);
6277
6278 for (n = t->deps; n != NULL; n = n->next)
6279 {
6280 if (n->version_needed == NULL)
6281 {
6282 /* This can happen if there was an error in the
6283 version script. */
6284 defaux.vda_name = 0;
6285 }
6286 else
6287 {
6288 defaux.vda_name = n->version_needed->name_indx;
6289 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6290 defaux.vda_name);
6291 }
6292 if (n->next == NULL)
6293 defaux.vda_next = 0;
6294 else
6295 defaux.vda_next = sizeof (Elf_External_Verdaux);
6296
6297 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6298 (Elf_External_Verdaux *) p);
6299 p += sizeof (Elf_External_Verdaux);
6300 }
6301 }
6302
6303 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6304 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6305 return FALSE;
6306
6307 elf_tdata (output_bfd)->cverdefs = cdefs;
6308 }
6309
6310 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6311 {
6312 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6313 return FALSE;
6314 }
6315 else if (info->flags & DF_BIND_NOW)
6316 {
6317 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6318 return FALSE;
6319 }
6320
6321 if (info->flags_1)
6322 {
0e1862bb 6323 if (bfd_link_executable (info))
5a580b3a
AM
6324 info->flags_1 &= ~ (DF_1_INITFIRST
6325 | DF_1_NODELETE
6326 | DF_1_NOOPEN);
6327 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6328 return FALSE;
6329 }
6330
6331 /* Work out the size of the version reference section. */
6332
3d4d4302 6333 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6334 BFD_ASSERT (s != NULL);
6335 {
6336 struct elf_find_verdep_info sinfo;
6337
5a580b3a
AM
6338 sinfo.info = info;
6339 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6340 if (sinfo.vers == 0)
6341 sinfo.vers = 1;
6342 sinfo.failed = FALSE;
6343
6344 elf_link_hash_traverse (elf_hash_table (info),
6345 _bfd_elf_link_find_version_dependencies,
6346 &sinfo);
14b1c01e
AM
6347 if (sinfo.failed)
6348 return FALSE;
5a580b3a
AM
6349
6350 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6351 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6352 else
6353 {
6354 Elf_Internal_Verneed *t;
6355 unsigned int size;
6356 unsigned int crefs;
6357 bfd_byte *p;
6358
a6cc6b3b 6359 /* Build the version dependency section. */
5a580b3a
AM
6360 size = 0;
6361 crefs = 0;
6362 for (t = elf_tdata (output_bfd)->verref;
6363 t != NULL;
6364 t = t->vn_nextref)
6365 {
6366 Elf_Internal_Vernaux *a;
6367
6368 size += sizeof (Elf_External_Verneed);
6369 ++crefs;
6370 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6371 size += sizeof (Elf_External_Vernaux);
6372 }
6373
eea6121a 6374 s->size = size;
a50b1753 6375 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6376 if (s->contents == NULL)
6377 return FALSE;
6378
6379 p = s->contents;
6380 for (t = elf_tdata (output_bfd)->verref;
6381 t != NULL;
6382 t = t->vn_nextref)
6383 {
6384 unsigned int caux;
6385 Elf_Internal_Vernaux *a;
6386 bfd_size_type indx;
6387
6388 caux = 0;
6389 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6390 ++caux;
6391
6392 t->vn_version = VER_NEED_CURRENT;
6393 t->vn_cnt = caux;
6394 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6395 elf_dt_name (t->vn_bfd) != NULL
6396 ? elf_dt_name (t->vn_bfd)
06084812 6397 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6398 FALSE);
6399 if (indx == (bfd_size_type) -1)
6400 return FALSE;
6401 t->vn_file = indx;
6402 t->vn_aux = sizeof (Elf_External_Verneed);
6403 if (t->vn_nextref == NULL)
6404 t->vn_next = 0;
6405 else
6406 t->vn_next = (sizeof (Elf_External_Verneed)
6407 + caux * sizeof (Elf_External_Vernaux));
6408
6409 _bfd_elf_swap_verneed_out (output_bfd, t,
6410 (Elf_External_Verneed *) p);
6411 p += sizeof (Elf_External_Verneed);
6412
6413 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6414 {
6415 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6416 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6417 a->vna_nodename, FALSE);
6418 if (indx == (bfd_size_type) -1)
6419 return FALSE;
6420 a->vna_name = indx;
6421 if (a->vna_nextptr == NULL)
6422 a->vna_next = 0;
6423 else
6424 a->vna_next = sizeof (Elf_External_Vernaux);
6425
6426 _bfd_elf_swap_vernaux_out (output_bfd, a,
6427 (Elf_External_Vernaux *) p);
6428 p += sizeof (Elf_External_Vernaux);
6429 }
6430 }
6431
6432 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6433 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6434 return FALSE;
6435
6436 elf_tdata (output_bfd)->cverrefs = crefs;
6437 }
6438 }
6439
8423293d
AM
6440 if ((elf_tdata (output_bfd)->cverrefs == 0
6441 && elf_tdata (output_bfd)->cverdefs == 0)
6442 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6443 &section_sym_count) == 0)
6444 {
3d4d4302 6445 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6446 s->flags |= SEC_EXCLUDE;
6447 }
6448 }
6449 return TRUE;
6450}
6451
74541ad4
AM
6452/* Find the first non-excluded output section. We'll use its
6453 section symbol for some emitted relocs. */
6454void
6455_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6456{
6457 asection *s;
6458
6459 for (s = output_bfd->sections; s != NULL; s = s->next)
6460 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6461 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6462 {
6463 elf_hash_table (info)->text_index_section = s;
6464 break;
6465 }
6466}
6467
6468/* Find two non-excluded output sections, one for code, one for data.
6469 We'll use their section symbols for some emitted relocs. */
6470void
6471_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6472{
6473 asection *s;
6474
266b05cf
DJ
6475 /* Data first, since setting text_index_section changes
6476 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6477 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6478 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6479 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6480 {
266b05cf 6481 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6482 break;
6483 }
6484
6485 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6486 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6487 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6488 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6489 {
266b05cf 6490 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6491 break;
6492 }
6493
6494 if (elf_hash_table (info)->text_index_section == NULL)
6495 elf_hash_table (info)->text_index_section
6496 = elf_hash_table (info)->data_index_section;
6497}
6498
8423293d
AM
6499bfd_boolean
6500bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6501{
74541ad4
AM
6502 const struct elf_backend_data *bed;
6503
8423293d
AM
6504 if (!is_elf_hash_table (info->hash))
6505 return TRUE;
6506
74541ad4
AM
6507 bed = get_elf_backend_data (output_bfd);
6508 (*bed->elf_backend_init_index_section) (output_bfd, info);
6509
8423293d
AM
6510 if (elf_hash_table (info)->dynamic_sections_created)
6511 {
6512 bfd *dynobj;
8423293d
AM
6513 asection *s;
6514 bfd_size_type dynsymcount;
6515 unsigned long section_sym_count;
8423293d
AM
6516 unsigned int dtagcount;
6517
6518 dynobj = elf_hash_table (info)->dynobj;
6519
5a580b3a
AM
6520 /* Assign dynsym indicies. In a shared library we generate a
6521 section symbol for each output section, which come first.
6522 Next come all of the back-end allocated local dynamic syms,
6523 followed by the rest of the global symbols. */
6524
554220db
AM
6525 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6526 &section_sym_count);
5a580b3a
AM
6527
6528 /* Work out the size of the symbol version section. */
3d4d4302 6529 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6530 BFD_ASSERT (s != NULL);
8423293d
AM
6531 if (dynsymcount != 0
6532 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6533 {
eea6121a 6534 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6535 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6536 if (s->contents == NULL)
6537 return FALSE;
6538
6539 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6540 return FALSE;
6541 }
6542
6543 /* Set the size of the .dynsym and .hash sections. We counted
6544 the number of dynamic symbols in elf_link_add_object_symbols.
6545 We will build the contents of .dynsym and .hash when we build
6546 the final symbol table, because until then we do not know the
6547 correct value to give the symbols. We built the .dynstr
6548 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6549 s = elf_hash_table (info)->dynsym;
5a580b3a 6550 BFD_ASSERT (s != NULL);
eea6121a 6551 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6552
6553 if (dynsymcount != 0)
6554 {
a50b1753 6555 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6556 if (s->contents == NULL)
6557 return FALSE;
5a580b3a 6558
554220db
AM
6559 /* The first entry in .dynsym is a dummy symbol.
6560 Clear all the section syms, in case we don't output them all. */
6561 ++section_sym_count;
6562 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6563 }
6564
fdc90cb4
JJ
6565 elf_hash_table (info)->bucketcount = 0;
6566
5a580b3a
AM
6567 /* Compute the size of the hashing table. As a side effect this
6568 computes the hash values for all the names we export. */
fdc90cb4
JJ
6569 if (info->emit_hash)
6570 {
6571 unsigned long int *hashcodes;
14b1c01e 6572 struct hash_codes_info hashinf;
fdc90cb4
JJ
6573 bfd_size_type amt;
6574 unsigned long int nsyms;
6575 size_t bucketcount;
6576 size_t hash_entry_size;
6577
6578 /* Compute the hash values for all exported symbols. At the same
6579 time store the values in an array so that we could use them for
6580 optimizations. */
6581 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6582 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6583 if (hashcodes == NULL)
6584 return FALSE;
14b1c01e
AM
6585 hashinf.hashcodes = hashcodes;
6586 hashinf.error = FALSE;
5a580b3a 6587
fdc90cb4
JJ
6588 /* Put all hash values in HASHCODES. */
6589 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6590 elf_collect_hash_codes, &hashinf);
6591 if (hashinf.error)
4dd07732
AM
6592 {
6593 free (hashcodes);
6594 return FALSE;
6595 }
5a580b3a 6596
14b1c01e 6597 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6598 bucketcount
6599 = compute_bucket_count (info, hashcodes, nsyms, 0);
6600 free (hashcodes);
6601
6602 if (bucketcount == 0)
6603 return FALSE;
5a580b3a 6604
fdc90cb4
JJ
6605 elf_hash_table (info)->bucketcount = bucketcount;
6606
3d4d4302 6607 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6608 BFD_ASSERT (s != NULL);
6609 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6610 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6611 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6612 if (s->contents == NULL)
6613 return FALSE;
6614
6615 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6616 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6617 s->contents + hash_entry_size);
6618 }
6619
6620 if (info->emit_gnu_hash)
6621 {
6622 size_t i, cnt;
6623 unsigned char *contents;
6624 struct collect_gnu_hash_codes cinfo;
6625 bfd_size_type amt;
6626 size_t bucketcount;
6627
6628 memset (&cinfo, 0, sizeof (cinfo));
6629
6630 /* Compute the hash values for all exported symbols. At the same
6631 time store the values in an array so that we could use them for
6632 optimizations. */
6633 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6634 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6635 if (cinfo.hashcodes == NULL)
6636 return FALSE;
6637
6638 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6639 cinfo.min_dynindx = -1;
6640 cinfo.output_bfd = output_bfd;
6641 cinfo.bed = bed;
6642
6643 /* Put all hash values in HASHCODES. */
6644 elf_link_hash_traverse (elf_hash_table (info),
6645 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6646 if (cinfo.error)
4dd07732
AM
6647 {
6648 free (cinfo.hashcodes);
6649 return FALSE;
6650 }
fdc90cb4
JJ
6651
6652 bucketcount
6653 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6654
6655 if (bucketcount == 0)
6656 {
6657 free (cinfo.hashcodes);
6658 return FALSE;
6659 }
6660
3d4d4302 6661 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6662 BFD_ASSERT (s != NULL);
6663
6664 if (cinfo.nsyms == 0)
6665 {
6666 /* Empty .gnu.hash section is special. */
6667 BFD_ASSERT (cinfo.min_dynindx == -1);
6668 free (cinfo.hashcodes);
6669 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6670 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6671 if (contents == NULL)
6672 return FALSE;
6673 s->contents = contents;
6674 /* 1 empty bucket. */
6675 bfd_put_32 (output_bfd, 1, contents);
6676 /* SYMIDX above the special symbol 0. */
6677 bfd_put_32 (output_bfd, 1, contents + 4);
6678 /* Just one word for bitmask. */
6679 bfd_put_32 (output_bfd, 1, contents + 8);
6680 /* Only hash fn bloom filter. */
6681 bfd_put_32 (output_bfd, 0, contents + 12);
6682 /* No hashes are valid - empty bitmask. */
6683 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6684 /* No hashes in the only bucket. */
6685 bfd_put_32 (output_bfd, 0,
6686 contents + 16 + bed->s->arch_size / 8);
6687 }
6688 else
6689 {
9e6619e2 6690 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6691 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6692
9e6619e2
AM
6693 x = cinfo.nsyms;
6694 maskbitslog2 = 1;
6695 while ((x >>= 1) != 0)
6696 ++maskbitslog2;
fdc90cb4
JJ
6697 if (maskbitslog2 < 3)
6698 maskbitslog2 = 5;
6699 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6700 maskbitslog2 = maskbitslog2 + 3;
6701 else
6702 maskbitslog2 = maskbitslog2 + 2;
6703 if (bed->s->arch_size == 64)
6704 {
6705 if (maskbitslog2 == 5)
6706 maskbitslog2 = 6;
6707 cinfo.shift1 = 6;
6708 }
6709 else
6710 cinfo.shift1 = 5;
6711 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6712 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6713 cinfo.maskbits = 1 << maskbitslog2;
6714 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6715 amt = bucketcount * sizeof (unsigned long int) * 2;
6716 amt += maskwords * sizeof (bfd_vma);
a50b1753 6717 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6718 if (cinfo.bitmask == NULL)
6719 {
6720 free (cinfo.hashcodes);
6721 return FALSE;
6722 }
6723
a50b1753 6724 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6725 cinfo.indx = cinfo.counts + bucketcount;
6726 cinfo.symindx = dynsymcount - cinfo.nsyms;
6727 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6728
6729 /* Determine how often each hash bucket is used. */
6730 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6731 for (i = 0; i < cinfo.nsyms; ++i)
6732 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6733
6734 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6735 if (cinfo.counts[i] != 0)
6736 {
6737 cinfo.indx[i] = cnt;
6738 cnt += cinfo.counts[i];
6739 }
6740 BFD_ASSERT (cnt == dynsymcount);
6741 cinfo.bucketcount = bucketcount;
6742 cinfo.local_indx = cinfo.min_dynindx;
6743
6744 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6745 s->size += cinfo.maskbits / 8;
a50b1753 6746 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6747 if (contents == NULL)
6748 {
6749 free (cinfo.bitmask);
6750 free (cinfo.hashcodes);
6751 return FALSE;
6752 }
6753
6754 s->contents = contents;
6755 bfd_put_32 (output_bfd, bucketcount, contents);
6756 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6757 bfd_put_32 (output_bfd, maskwords, contents + 8);
6758 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6759 contents += 16 + cinfo.maskbits / 8;
6760
6761 for (i = 0; i < bucketcount; ++i)
6762 {
6763 if (cinfo.counts[i] == 0)
6764 bfd_put_32 (output_bfd, 0, contents);
6765 else
6766 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6767 contents += 4;
6768 }
6769
6770 cinfo.contents = contents;
6771
6772 /* Renumber dynamic symbols, populate .gnu.hash section. */
6773 elf_link_hash_traverse (elf_hash_table (info),
6774 elf_renumber_gnu_hash_syms, &cinfo);
6775
6776 contents = s->contents + 16;
6777 for (i = 0; i < maskwords; ++i)
6778 {
6779 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6780 contents);
6781 contents += bed->s->arch_size / 8;
6782 }
6783
6784 free (cinfo.bitmask);
6785 free (cinfo.hashcodes);
6786 }
6787 }
5a580b3a 6788
3d4d4302 6789 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6790 BFD_ASSERT (s != NULL);
6791
4ad4eba5 6792 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6793
eea6121a 6794 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6795
6796 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6797 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6798 return FALSE;
6799 }
6800
6801 return TRUE;
6802}
4d269e42 6803\f
4d269e42
AM
6804/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6805
6806static void
6807merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6808 asection *sec)
6809{
dbaa2011
AM
6810 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6811 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6812}
6813
6814/* Finish SHF_MERGE section merging. */
6815
6816bfd_boolean
630993ec 6817_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6818{
6819 bfd *ibfd;
6820 asection *sec;
6821
6822 if (!is_elf_hash_table (info->hash))
6823 return FALSE;
6824
c72f2fb2 6825 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6826 if ((ibfd->flags & DYNAMIC) == 0
6827 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6828 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6829 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6830 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6831 if ((sec->flags & SEC_MERGE) != 0
6832 && !bfd_is_abs_section (sec->output_section))
6833 {
6834 struct bfd_elf_section_data *secdata;
6835
6836 secdata = elf_section_data (sec);
630993ec 6837 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6838 &elf_hash_table (info)->merge_info,
6839 sec, &secdata->sec_info))
6840 return FALSE;
6841 else if (secdata->sec_info)
dbaa2011 6842 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6843 }
6844
6845 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6846 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6847 merge_sections_remove_hook);
6848 return TRUE;
6849}
6850
6851/* Create an entry in an ELF linker hash table. */
6852
6853struct bfd_hash_entry *
6854_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6855 struct bfd_hash_table *table,
6856 const char *string)
6857{
6858 /* Allocate the structure if it has not already been allocated by a
6859 subclass. */
6860 if (entry == NULL)
6861 {
a50b1753 6862 entry = (struct bfd_hash_entry *)
ca4be51c 6863 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6864 if (entry == NULL)
6865 return entry;
6866 }
6867
6868 /* Call the allocation method of the superclass. */
6869 entry = _bfd_link_hash_newfunc (entry, table, string);
6870 if (entry != NULL)
6871 {
6872 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6873 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6874
6875 /* Set local fields. */
6876 ret->indx = -1;
6877 ret->dynindx = -1;
6878 ret->got = htab->init_got_refcount;
6879 ret->plt = htab->init_plt_refcount;
6880 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6881 - offsetof (struct elf_link_hash_entry, size)));
6882 /* Assume that we have been called by a non-ELF symbol reader.
6883 This flag is then reset by the code which reads an ELF input
6884 file. This ensures that a symbol created by a non-ELF symbol
6885 reader will have the flag set correctly. */
6886 ret->non_elf = 1;
6887 }
6888
6889 return entry;
6890}
6891
6892/* Copy data from an indirect symbol to its direct symbol, hiding the
6893 old indirect symbol. Also used for copying flags to a weakdef. */
6894
6895void
6896_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6897 struct elf_link_hash_entry *dir,
6898 struct elf_link_hash_entry *ind)
6899{
6900 struct elf_link_hash_table *htab;
6901
6902 /* Copy down any references that we may have already seen to the
6e33951e
L
6903 symbol which just became indirect if DIR isn't a hidden versioned
6904 symbol. */
4d269e42 6905
422f1182 6906 if (dir->versioned != versioned_hidden)
6e33951e
L
6907 {
6908 dir->ref_dynamic |= ind->ref_dynamic;
6909 dir->ref_regular |= ind->ref_regular;
6910 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6911 dir->non_got_ref |= ind->non_got_ref;
6912 dir->needs_plt |= ind->needs_plt;
6913 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6914 }
4d269e42
AM
6915
6916 if (ind->root.type != bfd_link_hash_indirect)
6917 return;
6918
6919 /* Copy over the global and procedure linkage table refcount entries.
6920 These may have been already set up by a check_relocs routine. */
6921 htab = elf_hash_table (info);
6922 if (ind->got.refcount > htab->init_got_refcount.refcount)
6923 {
6924 if (dir->got.refcount < 0)
6925 dir->got.refcount = 0;
6926 dir->got.refcount += ind->got.refcount;
6927 ind->got.refcount = htab->init_got_refcount.refcount;
6928 }
6929
6930 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6931 {
6932 if (dir->plt.refcount < 0)
6933 dir->plt.refcount = 0;
6934 dir->plt.refcount += ind->plt.refcount;
6935 ind->plt.refcount = htab->init_plt_refcount.refcount;
6936 }
6937
6938 if (ind->dynindx != -1)
6939 {
6940 if (dir->dynindx != -1)
6941 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6942 dir->dynindx = ind->dynindx;
6943 dir->dynstr_index = ind->dynstr_index;
6944 ind->dynindx = -1;
6945 ind->dynstr_index = 0;
6946 }
6947}
6948
6949void
6950_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6951 struct elf_link_hash_entry *h,
6952 bfd_boolean force_local)
6953{
3aa14d16
L
6954 /* STT_GNU_IFUNC symbol must go through PLT. */
6955 if (h->type != STT_GNU_IFUNC)
6956 {
6957 h->plt = elf_hash_table (info)->init_plt_offset;
6958 h->needs_plt = 0;
6959 }
4d269e42
AM
6960 if (force_local)
6961 {
6962 h->forced_local = 1;
6963 if (h->dynindx != -1)
6964 {
6965 h->dynindx = -1;
6966 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6967 h->dynstr_index);
6968 }
6969 }
6970}
6971
7bf52ea2
AM
6972/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6973 caller. */
4d269e42
AM
6974
6975bfd_boolean
6976_bfd_elf_link_hash_table_init
6977 (struct elf_link_hash_table *table,
6978 bfd *abfd,
6979 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6980 struct bfd_hash_table *,
6981 const char *),
4dfe6ac6
NC
6982 unsigned int entsize,
6983 enum elf_target_id target_id)
4d269e42
AM
6984{
6985 bfd_boolean ret;
6986 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6987
4d269e42
AM
6988 table->init_got_refcount.refcount = can_refcount - 1;
6989 table->init_plt_refcount.refcount = can_refcount - 1;
6990 table->init_got_offset.offset = -(bfd_vma) 1;
6991 table->init_plt_offset.offset = -(bfd_vma) 1;
6992 /* The first dynamic symbol is a dummy. */
6993 table->dynsymcount = 1;
6994
6995 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 6996
4d269e42 6997 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 6998 table->hash_table_id = target_id;
4d269e42
AM
6999
7000 return ret;
7001}
7002
7003/* Create an ELF linker hash table. */
7004
7005struct bfd_link_hash_table *
7006_bfd_elf_link_hash_table_create (bfd *abfd)
7007{
7008 struct elf_link_hash_table *ret;
7009 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7010
7bf52ea2 7011 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7012 if (ret == NULL)
7013 return NULL;
7014
7015 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7016 sizeof (struct elf_link_hash_entry),
7017 GENERIC_ELF_DATA))
4d269e42
AM
7018 {
7019 free (ret);
7020 return NULL;
7021 }
d495ab0d 7022 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7023
7024 return &ret->root;
7025}
7026
9f7c3e5e
AM
7027/* Destroy an ELF linker hash table. */
7028
7029void
d495ab0d 7030_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7031{
d495ab0d
AM
7032 struct elf_link_hash_table *htab;
7033
7034 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7035 if (htab->dynstr != NULL)
7036 _bfd_elf_strtab_free (htab->dynstr);
7037 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7038 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7039}
7040
4d269e42
AM
7041/* This is a hook for the ELF emulation code in the generic linker to
7042 tell the backend linker what file name to use for the DT_NEEDED
7043 entry for a dynamic object. */
7044
7045void
7046bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7047{
7048 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7049 && bfd_get_format (abfd) == bfd_object)
7050 elf_dt_name (abfd) = name;
7051}
7052
7053int
7054bfd_elf_get_dyn_lib_class (bfd *abfd)
7055{
7056 int lib_class;
7057 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7058 && bfd_get_format (abfd) == bfd_object)
7059 lib_class = elf_dyn_lib_class (abfd);
7060 else
7061 lib_class = 0;
7062 return lib_class;
7063}
7064
7065void
7066bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7067{
7068 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7069 && bfd_get_format (abfd) == bfd_object)
7070 elf_dyn_lib_class (abfd) = lib_class;
7071}
7072
7073/* Get the list of DT_NEEDED entries for a link. This is a hook for
7074 the linker ELF emulation code. */
7075
7076struct bfd_link_needed_list *
7077bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7078 struct bfd_link_info *info)
7079{
7080 if (! is_elf_hash_table (info->hash))
7081 return NULL;
7082 return elf_hash_table (info)->needed;
7083}
7084
7085/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7086 hook for the linker ELF emulation code. */
7087
7088struct bfd_link_needed_list *
7089bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7090 struct bfd_link_info *info)
7091{
7092 if (! is_elf_hash_table (info->hash))
7093 return NULL;
7094 return elf_hash_table (info)->runpath;
7095}
7096
7097/* Get the name actually used for a dynamic object for a link. This
7098 is the SONAME entry if there is one. Otherwise, it is the string
7099 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7100
7101const char *
7102bfd_elf_get_dt_soname (bfd *abfd)
7103{
7104 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7105 && bfd_get_format (abfd) == bfd_object)
7106 return elf_dt_name (abfd);
7107 return NULL;
7108}
7109
7110/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7111 the ELF linker emulation code. */
7112
7113bfd_boolean
7114bfd_elf_get_bfd_needed_list (bfd *abfd,
7115 struct bfd_link_needed_list **pneeded)
7116{
7117 asection *s;
7118 bfd_byte *dynbuf = NULL;
cb33740c 7119 unsigned int elfsec;
4d269e42
AM
7120 unsigned long shlink;
7121 bfd_byte *extdyn, *extdynend;
7122 size_t extdynsize;
7123 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7124
7125 *pneeded = NULL;
7126
7127 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7128 || bfd_get_format (abfd) != bfd_object)
7129 return TRUE;
7130
7131 s = bfd_get_section_by_name (abfd, ".dynamic");
7132 if (s == NULL || s->size == 0)
7133 return TRUE;
7134
7135 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7136 goto error_return;
7137
7138 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7139 if (elfsec == SHN_BAD)
4d269e42
AM
7140 goto error_return;
7141
7142 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7143
4d269e42
AM
7144 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7145 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7146
7147 extdyn = dynbuf;
7148 extdynend = extdyn + s->size;
7149 for (; extdyn < extdynend; extdyn += extdynsize)
7150 {
7151 Elf_Internal_Dyn dyn;
7152
7153 (*swap_dyn_in) (abfd, extdyn, &dyn);
7154
7155 if (dyn.d_tag == DT_NULL)
7156 break;
7157
7158 if (dyn.d_tag == DT_NEEDED)
7159 {
7160 const char *string;
7161 struct bfd_link_needed_list *l;
7162 unsigned int tagv = dyn.d_un.d_val;
7163 bfd_size_type amt;
7164
7165 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7166 if (string == NULL)
7167 goto error_return;
7168
7169 amt = sizeof *l;
a50b1753 7170 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7171 if (l == NULL)
7172 goto error_return;
7173
7174 l->by = abfd;
7175 l->name = string;
7176 l->next = *pneeded;
7177 *pneeded = l;
7178 }
7179 }
7180
7181 free (dynbuf);
7182
7183 return TRUE;
7184
7185 error_return:
7186 if (dynbuf != NULL)
7187 free (dynbuf);
7188 return FALSE;
7189}
7190
7191struct elf_symbuf_symbol
7192{
7193 unsigned long st_name; /* Symbol name, index in string tbl */
7194 unsigned char st_info; /* Type and binding attributes */
7195 unsigned char st_other; /* Visibilty, and target specific */
7196};
7197
7198struct elf_symbuf_head
7199{
7200 struct elf_symbuf_symbol *ssym;
7201 bfd_size_type count;
7202 unsigned int st_shndx;
7203};
7204
7205struct elf_symbol
7206{
7207 union
7208 {
7209 Elf_Internal_Sym *isym;
7210 struct elf_symbuf_symbol *ssym;
7211 } u;
7212 const char *name;
7213};
7214
7215/* Sort references to symbols by ascending section number. */
7216
7217static int
7218elf_sort_elf_symbol (const void *arg1, const void *arg2)
7219{
7220 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7221 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7222
7223 return s1->st_shndx - s2->st_shndx;
7224}
7225
7226static int
7227elf_sym_name_compare (const void *arg1, const void *arg2)
7228{
7229 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7230 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7231 return strcmp (s1->name, s2->name);
7232}
7233
7234static struct elf_symbuf_head *
7235elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7236{
14b1c01e 7237 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7238 struct elf_symbuf_symbol *ssym;
7239 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7240 bfd_size_type i, shndx_count, total_size;
4d269e42 7241
a50b1753 7242 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7243 if (indbuf == NULL)
7244 return NULL;
7245
7246 for (ind = indbuf, i = 0; i < symcount; i++)
7247 if (isymbuf[i].st_shndx != SHN_UNDEF)
7248 *ind++ = &isymbuf[i];
7249 indbufend = ind;
7250
7251 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7252 elf_sort_elf_symbol);
7253
7254 shndx_count = 0;
7255 if (indbufend > indbuf)
7256 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7257 if (ind[0]->st_shndx != ind[1]->st_shndx)
7258 shndx_count++;
7259
3ae181ee
L
7260 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7261 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7262 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7263 if (ssymbuf == NULL)
7264 {
7265 free (indbuf);
7266 return NULL;
7267 }
7268
3ae181ee 7269 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7270 ssymbuf->ssym = NULL;
7271 ssymbuf->count = shndx_count;
7272 ssymbuf->st_shndx = 0;
7273 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7274 {
7275 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7276 {
7277 ssymhead++;
7278 ssymhead->ssym = ssym;
7279 ssymhead->count = 0;
7280 ssymhead->st_shndx = (*ind)->st_shndx;
7281 }
7282 ssym->st_name = (*ind)->st_name;
7283 ssym->st_info = (*ind)->st_info;
7284 ssym->st_other = (*ind)->st_other;
7285 ssymhead->count++;
7286 }
3ae181ee
L
7287 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7288 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7289 == total_size));
4d269e42
AM
7290
7291 free (indbuf);
7292 return ssymbuf;
7293}
7294
7295/* Check if 2 sections define the same set of local and global
7296 symbols. */
7297
8f317e31 7298static bfd_boolean
4d269e42
AM
7299bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7300 struct bfd_link_info *info)
7301{
7302 bfd *bfd1, *bfd2;
7303 const struct elf_backend_data *bed1, *bed2;
7304 Elf_Internal_Shdr *hdr1, *hdr2;
7305 bfd_size_type symcount1, symcount2;
7306 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7307 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7308 Elf_Internal_Sym *isym, *isymend;
7309 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7310 bfd_size_type count1, count2, i;
cb33740c 7311 unsigned int shndx1, shndx2;
4d269e42
AM
7312 bfd_boolean result;
7313
7314 bfd1 = sec1->owner;
7315 bfd2 = sec2->owner;
7316
4d269e42
AM
7317 /* Both sections have to be in ELF. */
7318 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7319 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7320 return FALSE;
7321
7322 if (elf_section_type (sec1) != elf_section_type (sec2))
7323 return FALSE;
7324
4d269e42
AM
7325 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7326 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7327 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7328 return FALSE;
7329
7330 bed1 = get_elf_backend_data (bfd1);
7331 bed2 = get_elf_backend_data (bfd2);
7332 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7333 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7334 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7335 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7336
7337 if (symcount1 == 0 || symcount2 == 0)
7338 return FALSE;
7339
7340 result = FALSE;
7341 isymbuf1 = NULL;
7342 isymbuf2 = NULL;
a50b1753
NC
7343 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7344 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7345
7346 if (ssymbuf1 == NULL)
7347 {
7348 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7349 NULL, NULL, NULL);
7350 if (isymbuf1 == NULL)
7351 goto done;
7352
7353 if (!info->reduce_memory_overheads)
7354 elf_tdata (bfd1)->symbuf = ssymbuf1
7355 = elf_create_symbuf (symcount1, isymbuf1);
7356 }
7357
7358 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7359 {
7360 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7361 NULL, NULL, NULL);
7362 if (isymbuf2 == NULL)
7363 goto done;
7364
7365 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7366 elf_tdata (bfd2)->symbuf = ssymbuf2
7367 = elf_create_symbuf (symcount2, isymbuf2);
7368 }
7369
7370 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7371 {
7372 /* Optimized faster version. */
7373 bfd_size_type lo, hi, mid;
7374 struct elf_symbol *symp;
7375 struct elf_symbuf_symbol *ssym, *ssymend;
7376
7377 lo = 0;
7378 hi = ssymbuf1->count;
7379 ssymbuf1++;
7380 count1 = 0;
7381 while (lo < hi)
7382 {
7383 mid = (lo + hi) / 2;
cb33740c 7384 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7385 hi = mid;
cb33740c 7386 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7387 lo = mid + 1;
7388 else
7389 {
7390 count1 = ssymbuf1[mid].count;
7391 ssymbuf1 += mid;
7392 break;
7393 }
7394 }
7395
7396 lo = 0;
7397 hi = ssymbuf2->count;
7398 ssymbuf2++;
7399 count2 = 0;
7400 while (lo < hi)
7401 {
7402 mid = (lo + hi) / 2;
cb33740c 7403 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7404 hi = mid;
cb33740c 7405 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7406 lo = mid + 1;
7407 else
7408 {
7409 count2 = ssymbuf2[mid].count;
7410 ssymbuf2 += mid;
7411 break;
7412 }
7413 }
7414
7415 if (count1 == 0 || count2 == 0 || count1 != count2)
7416 goto done;
7417
ca4be51c
AM
7418 symtable1
7419 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7420 symtable2
7421 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7422 if (symtable1 == NULL || symtable2 == NULL)
7423 goto done;
7424
7425 symp = symtable1;
7426 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7427 ssym < ssymend; ssym++, symp++)
7428 {
7429 symp->u.ssym = ssym;
7430 symp->name = bfd_elf_string_from_elf_section (bfd1,
7431 hdr1->sh_link,
7432 ssym->st_name);
7433 }
7434
7435 symp = symtable2;
7436 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7437 ssym < ssymend; ssym++, symp++)
7438 {
7439 symp->u.ssym = ssym;
7440 symp->name = bfd_elf_string_from_elf_section (bfd2,
7441 hdr2->sh_link,
7442 ssym->st_name);
7443 }
7444
7445 /* Sort symbol by name. */
7446 qsort (symtable1, count1, sizeof (struct elf_symbol),
7447 elf_sym_name_compare);
7448 qsort (symtable2, count1, sizeof (struct elf_symbol),
7449 elf_sym_name_compare);
7450
7451 for (i = 0; i < count1; i++)
7452 /* Two symbols must have the same binding, type and name. */
7453 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7454 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7455 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7456 goto done;
7457
7458 result = TRUE;
7459 goto done;
7460 }
7461
a50b1753
NC
7462 symtable1 = (struct elf_symbol *)
7463 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7464 symtable2 = (struct elf_symbol *)
7465 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7466 if (symtable1 == NULL || symtable2 == NULL)
7467 goto done;
7468
7469 /* Count definitions in the section. */
7470 count1 = 0;
7471 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7472 if (isym->st_shndx == shndx1)
4d269e42
AM
7473 symtable1[count1++].u.isym = isym;
7474
7475 count2 = 0;
7476 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7477 if (isym->st_shndx == shndx2)
4d269e42
AM
7478 symtable2[count2++].u.isym = isym;
7479
7480 if (count1 == 0 || count2 == 0 || count1 != count2)
7481 goto done;
7482
7483 for (i = 0; i < count1; i++)
7484 symtable1[i].name
7485 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7486 symtable1[i].u.isym->st_name);
7487
7488 for (i = 0; i < count2; i++)
7489 symtable2[i].name
7490 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7491 symtable2[i].u.isym->st_name);
7492
7493 /* Sort symbol by name. */
7494 qsort (symtable1, count1, sizeof (struct elf_symbol),
7495 elf_sym_name_compare);
7496 qsort (symtable2, count1, sizeof (struct elf_symbol),
7497 elf_sym_name_compare);
7498
7499 for (i = 0; i < count1; i++)
7500 /* Two symbols must have the same binding, type and name. */
7501 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7502 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7503 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7504 goto done;
7505
7506 result = TRUE;
7507
7508done:
7509 if (symtable1)
7510 free (symtable1);
7511 if (symtable2)
7512 free (symtable2);
7513 if (isymbuf1)
7514 free (isymbuf1);
7515 if (isymbuf2)
7516 free (isymbuf2);
7517
7518 return result;
7519}
7520
7521/* Return TRUE if 2 section types are compatible. */
7522
7523bfd_boolean
7524_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7525 bfd *bbfd, const asection *bsec)
7526{
7527 if (asec == NULL
7528 || bsec == NULL
7529 || abfd->xvec->flavour != bfd_target_elf_flavour
7530 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7531 return TRUE;
7532
7533 return elf_section_type (asec) == elf_section_type (bsec);
7534}
7535\f
c152c796
AM
7536/* Final phase of ELF linker. */
7537
7538/* A structure we use to avoid passing large numbers of arguments. */
7539
7540struct elf_final_link_info
7541{
7542 /* General link information. */
7543 struct bfd_link_info *info;
7544 /* Output BFD. */
7545 bfd *output_bfd;
7546 /* Symbol string table. */
ef10c3ac 7547 struct elf_strtab_hash *symstrtab;
c152c796
AM
7548 /* .hash section. */
7549 asection *hash_sec;
7550 /* symbol version section (.gnu.version). */
7551 asection *symver_sec;
7552 /* Buffer large enough to hold contents of any section. */
7553 bfd_byte *contents;
7554 /* Buffer large enough to hold external relocs of any section. */
7555 void *external_relocs;
7556 /* Buffer large enough to hold internal relocs of any section. */
7557 Elf_Internal_Rela *internal_relocs;
7558 /* Buffer large enough to hold external local symbols of any input
7559 BFD. */
7560 bfd_byte *external_syms;
7561 /* And a buffer for symbol section indices. */
7562 Elf_External_Sym_Shndx *locsym_shndx;
7563 /* Buffer large enough to hold internal local symbols of any input
7564 BFD. */
7565 Elf_Internal_Sym *internal_syms;
7566 /* Array large enough to hold a symbol index for each local symbol
7567 of any input BFD. */
7568 long *indices;
7569 /* Array large enough to hold a section pointer for each local
7570 symbol of any input BFD. */
7571 asection **sections;
ef10c3ac 7572 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7573 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7574 /* Number of STT_FILE syms seen. */
7575 size_t filesym_count;
c152c796
AM
7576};
7577
7578/* This struct is used to pass information to elf_link_output_extsym. */
7579
7580struct elf_outext_info
7581{
7582 bfd_boolean failed;
7583 bfd_boolean localsyms;
34a79995 7584 bfd_boolean file_sym_done;
8b127cbc 7585 struct elf_final_link_info *flinfo;
c152c796
AM
7586};
7587
d9352518
DB
7588
7589/* Support for evaluating a complex relocation.
7590
7591 Complex relocations are generalized, self-describing relocations. The
7592 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7593 relocations themselves.
d9352518
DB
7594
7595 The relocations are use a reserved elf-wide relocation type code (R_RELC
7596 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7597 information (start bit, end bit, word width, etc) into the addend. This
7598 information is extracted from CGEN-generated operand tables within gas.
7599
7600 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7601 internal) representing prefix-notation expressions, including but not
7602 limited to those sorts of expressions normally encoded as addends in the
7603 addend field. The symbol mangling format is:
7604
7605 <node> := <literal>
7606 | <unary-operator> ':' <node>
7607 | <binary-operator> ':' <node> ':' <node>
7608 ;
7609
7610 <literal> := 's' <digits=N> ':' <N character symbol name>
7611 | 'S' <digits=N> ':' <N character section name>
7612 | '#' <hexdigits>
7613 ;
7614
7615 <binary-operator> := as in C
7616 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7617
7618static void
a0c8462f
AM
7619set_symbol_value (bfd *bfd_with_globals,
7620 Elf_Internal_Sym *isymbuf,
7621 size_t locsymcount,
7622 size_t symidx,
7623 bfd_vma val)
d9352518 7624{
8977835c
AM
7625 struct elf_link_hash_entry **sym_hashes;
7626 struct elf_link_hash_entry *h;
7627 size_t extsymoff = locsymcount;
d9352518 7628
8977835c 7629 if (symidx < locsymcount)
d9352518 7630 {
8977835c
AM
7631 Elf_Internal_Sym *sym;
7632
7633 sym = isymbuf + symidx;
7634 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7635 {
7636 /* It is a local symbol: move it to the
7637 "absolute" section and give it a value. */
7638 sym->st_shndx = SHN_ABS;
7639 sym->st_value = val;
7640 return;
7641 }
7642 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7643 extsymoff = 0;
d9352518 7644 }
8977835c
AM
7645
7646 /* It is a global symbol: set its link type
7647 to "defined" and give it a value. */
7648
7649 sym_hashes = elf_sym_hashes (bfd_with_globals);
7650 h = sym_hashes [symidx - extsymoff];
7651 while (h->root.type == bfd_link_hash_indirect
7652 || h->root.type == bfd_link_hash_warning)
7653 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7654 h->root.type = bfd_link_hash_defined;
7655 h->root.u.def.value = val;
7656 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7657}
7658
a0c8462f
AM
7659static bfd_boolean
7660resolve_symbol (const char *name,
7661 bfd *input_bfd,
8b127cbc 7662 struct elf_final_link_info *flinfo,
a0c8462f
AM
7663 bfd_vma *result,
7664 Elf_Internal_Sym *isymbuf,
7665 size_t locsymcount)
d9352518 7666{
a0c8462f
AM
7667 Elf_Internal_Sym *sym;
7668 struct bfd_link_hash_entry *global_entry;
7669 const char *candidate = NULL;
7670 Elf_Internal_Shdr *symtab_hdr;
7671 size_t i;
7672
d9352518
DB
7673 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7674
7675 for (i = 0; i < locsymcount; ++ i)
7676 {
8977835c 7677 sym = isymbuf + i;
d9352518
DB
7678
7679 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7680 continue;
7681
7682 candidate = bfd_elf_string_from_elf_section (input_bfd,
7683 symtab_hdr->sh_link,
7684 sym->st_name);
7685#ifdef DEBUG
0f02bbd9
AM
7686 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7687 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7688#endif
7689 if (candidate && strcmp (candidate, name) == 0)
7690 {
8b127cbc 7691 asection *sec = flinfo->sections [i];
d9352518 7692
0f02bbd9
AM
7693 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7694 *result += sec->output_offset + sec->output_section->vma;
d9352518 7695#ifdef DEBUG
0f02bbd9
AM
7696 printf ("Found symbol with value %8.8lx\n",
7697 (unsigned long) *result);
d9352518
DB
7698#endif
7699 return TRUE;
7700 }
7701 }
7702
7703 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7704 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7705 FALSE, FALSE, TRUE);
d9352518
DB
7706 if (!global_entry)
7707 return FALSE;
a0c8462f 7708
d9352518
DB
7709 if (global_entry->type == bfd_link_hash_defined
7710 || global_entry->type == bfd_link_hash_defweak)
7711 {
a0c8462f
AM
7712 *result = (global_entry->u.def.value
7713 + global_entry->u.def.section->output_section->vma
7714 + global_entry->u.def.section->output_offset);
d9352518 7715#ifdef DEBUG
0f02bbd9
AM
7716 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7717 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7718#endif
7719 return TRUE;
a0c8462f 7720 }
d9352518 7721
d9352518
DB
7722 return FALSE;
7723}
7724
7725static bfd_boolean
a0c8462f
AM
7726resolve_section (const char *name,
7727 asection *sections,
7728 bfd_vma *result)
d9352518 7729{
a0c8462f
AM
7730 asection *curr;
7731 unsigned int len;
d9352518 7732
a0c8462f 7733 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7734 if (strcmp (curr->name, name) == 0)
7735 {
7736 *result = curr->vma;
7737 return TRUE;
7738 }
7739
7740 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7741 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7742 {
7743 len = strlen (curr->name);
a0c8462f 7744 if (len > strlen (name))
d9352518
DB
7745 continue;
7746
7747 if (strncmp (curr->name, name, len) == 0)
7748 {
7749 if (strncmp (".end", name + len, 4) == 0)
7750 {
7751 *result = curr->vma + curr->size;
7752 return TRUE;
7753 }
7754
7755 /* Insert more pseudo-section names here, if you like. */
7756 }
7757 }
a0c8462f 7758
d9352518
DB
7759 return FALSE;
7760}
7761
7762static void
a0c8462f 7763undefined_reference (const char *reftype, const char *name)
d9352518 7764{
a0c8462f
AM
7765 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7766 reftype, name);
d9352518
DB
7767}
7768
7769static bfd_boolean
a0c8462f
AM
7770eval_symbol (bfd_vma *result,
7771 const char **symp,
7772 bfd *input_bfd,
8b127cbc 7773 struct elf_final_link_info *flinfo,
a0c8462f
AM
7774 bfd_vma dot,
7775 Elf_Internal_Sym *isymbuf,
7776 size_t locsymcount,
7777 int signed_p)
d9352518 7778{
4b93929b
NC
7779 size_t len;
7780 size_t symlen;
a0c8462f
AM
7781 bfd_vma a;
7782 bfd_vma b;
4b93929b 7783 char symbuf[4096];
0f02bbd9 7784 const char *sym = *symp;
a0c8462f
AM
7785 const char *symend;
7786 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7787
7788 len = strlen (sym);
7789 symend = sym + len;
7790
4b93929b 7791 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7792 {
7793 bfd_set_error (bfd_error_invalid_operation);
7794 return FALSE;
7795 }
a0c8462f 7796
d9352518
DB
7797 switch (* sym)
7798 {
7799 case '.':
0f02bbd9
AM
7800 *result = dot;
7801 *symp = sym + 1;
d9352518
DB
7802 return TRUE;
7803
7804 case '#':
0f02bbd9
AM
7805 ++sym;
7806 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7807 return TRUE;
7808
7809 case 'S':
7810 symbol_is_section = TRUE;
a0c8462f 7811 case 's':
0f02bbd9
AM
7812 ++sym;
7813 symlen = strtol (sym, (char **) symp, 10);
7814 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7815
4b93929b 7816 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7817 {
7818 bfd_set_error (bfd_error_invalid_operation);
7819 return FALSE;
7820 }
7821
7822 memcpy (symbuf, sym, symlen);
a0c8462f 7823 symbuf[symlen] = '\0';
0f02bbd9 7824 *symp = sym + symlen;
a0c8462f
AM
7825
7826 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7827 the symbol as a section, or vice-versa. so we're pretty liberal in our
7828 interpretation here; section means "try section first", not "must be a
7829 section", and likewise with symbol. */
7830
a0c8462f 7831 if (symbol_is_section)
d9352518 7832 {
8b127cbc
AM
7833 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7834 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7835 isymbuf, locsymcount))
d9352518
DB
7836 {
7837 undefined_reference ("section", symbuf);
7838 return FALSE;
7839 }
a0c8462f
AM
7840 }
7841 else
d9352518 7842 {
8b127cbc 7843 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7844 isymbuf, locsymcount)
8b127cbc 7845 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977835c 7846 result))
d9352518
DB
7847 {
7848 undefined_reference ("symbol", symbuf);
7849 return FALSE;
7850 }
7851 }
7852
7853 return TRUE;
a0c8462f 7854
d9352518
DB
7855 /* All that remains are operators. */
7856
7857#define UNARY_OP(op) \
7858 if (strncmp (sym, #op, strlen (#op)) == 0) \
7859 { \
7860 sym += strlen (#op); \
a0c8462f
AM
7861 if (*sym == ':') \
7862 ++sym; \
0f02bbd9 7863 *symp = sym; \
8b127cbc 7864 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7865 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7866 return FALSE; \
7867 if (signed_p) \
0f02bbd9 7868 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7869 else \
7870 *result = op a; \
d9352518
DB
7871 return TRUE; \
7872 }
7873
7874#define BINARY_OP(op) \
7875 if (strncmp (sym, #op, strlen (#op)) == 0) \
7876 { \
7877 sym += strlen (#op); \
a0c8462f
AM
7878 if (*sym == ':') \
7879 ++sym; \
0f02bbd9 7880 *symp = sym; \
8b127cbc 7881 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7882 isymbuf, locsymcount, signed_p)) \
a0c8462f 7883 return FALSE; \
0f02bbd9 7884 ++*symp; \
8b127cbc 7885 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7886 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7887 return FALSE; \
7888 if (signed_p) \
0f02bbd9 7889 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7890 else \
7891 *result = a op b; \
d9352518
DB
7892 return TRUE; \
7893 }
7894
7895 default:
7896 UNARY_OP (0-);
7897 BINARY_OP (<<);
7898 BINARY_OP (>>);
7899 BINARY_OP (==);
7900 BINARY_OP (!=);
7901 BINARY_OP (<=);
7902 BINARY_OP (>=);
7903 BINARY_OP (&&);
7904 BINARY_OP (||);
7905 UNARY_OP (~);
7906 UNARY_OP (!);
7907 BINARY_OP (*);
7908 BINARY_OP (/);
7909 BINARY_OP (%);
7910 BINARY_OP (^);
7911 BINARY_OP (|);
7912 BINARY_OP (&);
7913 BINARY_OP (+);
7914 BINARY_OP (-);
7915 BINARY_OP (<);
7916 BINARY_OP (>);
7917#undef UNARY_OP
7918#undef BINARY_OP
7919 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7920 bfd_set_error (bfd_error_invalid_operation);
7921 return FALSE;
7922 }
7923}
7924
d9352518 7925static void
a0c8462f
AM
7926put_value (bfd_vma size,
7927 unsigned long chunksz,
7928 bfd *input_bfd,
7929 bfd_vma x,
7930 bfd_byte *location)
d9352518
DB
7931{
7932 location += (size - chunksz);
7933
41cd1ad1 7934 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
7935 {
7936 switch (chunksz)
7937 {
d9352518
DB
7938 case 1:
7939 bfd_put_8 (input_bfd, x, location);
41cd1ad1 7940 x >>= 8;
d9352518
DB
7941 break;
7942 case 2:
7943 bfd_put_16 (input_bfd, x, location);
41cd1ad1 7944 x >>= 16;
d9352518
DB
7945 break;
7946 case 4:
7947 bfd_put_32 (input_bfd, x, location);
65164438
NC
7948 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7949 x >>= 16;
7950 x >>= 16;
d9352518 7951 break;
d9352518 7952#ifdef BFD64
41cd1ad1 7953 case 8:
d9352518 7954 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
7955 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7956 x >>= 32;
7957 x >>= 32;
7958 break;
d9352518 7959#endif
41cd1ad1
NC
7960 default:
7961 abort ();
d9352518
DB
7962 break;
7963 }
7964 }
7965}
7966
a0c8462f
AM
7967static bfd_vma
7968get_value (bfd_vma size,
7969 unsigned long chunksz,
7970 bfd *input_bfd,
7971 bfd_byte *location)
d9352518 7972{
9b239e0e 7973 int shift;
d9352518
DB
7974 bfd_vma x = 0;
7975
9b239e0e
NC
7976 /* Sanity checks. */
7977 BFD_ASSERT (chunksz <= sizeof (x)
7978 && size >= chunksz
7979 && chunksz != 0
7980 && (size % chunksz) == 0
7981 && input_bfd != NULL
7982 && location != NULL);
7983
7984 if (chunksz == sizeof (x))
7985 {
7986 BFD_ASSERT (size == chunksz);
7987
7988 /* Make sure that we do not perform an undefined shift operation.
7989 We know that size == chunksz so there will only be one iteration
7990 of the loop below. */
7991 shift = 0;
7992 }
7993 else
7994 shift = 8 * chunksz;
7995
a0c8462f 7996 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7997 {
7998 switch (chunksz)
7999 {
d9352518 8000 case 1:
9b239e0e 8001 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8002 break;
8003 case 2:
9b239e0e 8004 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8005 break;
8006 case 4:
9b239e0e 8007 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8008 break;
d9352518 8009#ifdef BFD64
9b239e0e
NC
8010 case 8:
8011 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8012 break;
9b239e0e
NC
8013#endif
8014 default:
8015 abort ();
d9352518
DB
8016 }
8017 }
8018 return x;
8019}
8020
a0c8462f
AM
8021static void
8022decode_complex_addend (unsigned long *start, /* in bits */
8023 unsigned long *oplen, /* in bits */
8024 unsigned long *len, /* in bits */
8025 unsigned long *wordsz, /* in bytes */
8026 unsigned long *chunksz, /* in bytes */
8027 unsigned long *lsb0_p,
8028 unsigned long *signed_p,
8029 unsigned long *trunc_p,
8030 unsigned long encoded)
d9352518
DB
8031{
8032 * start = encoded & 0x3F;
8033 * len = (encoded >> 6) & 0x3F;
8034 * oplen = (encoded >> 12) & 0x3F;
8035 * wordsz = (encoded >> 18) & 0xF;
8036 * chunksz = (encoded >> 22) & 0xF;
8037 * lsb0_p = (encoded >> 27) & 1;
8038 * signed_p = (encoded >> 28) & 1;
8039 * trunc_p = (encoded >> 29) & 1;
8040}
8041
cdfeee4f 8042bfd_reloc_status_type
0f02bbd9 8043bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8044 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8045 bfd_byte *contents,
8046 Elf_Internal_Rela *rel,
8047 bfd_vma relocation)
d9352518 8048{
0f02bbd9
AM
8049 bfd_vma shift, x, mask;
8050 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8051 bfd_reloc_status_type r;
d9352518
DB
8052
8053 /* Perform this reloc, since it is complex.
8054 (this is not to say that it necessarily refers to a complex
8055 symbol; merely that it is a self-describing CGEN based reloc.
8056 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8057 word size, etc) encoded within it.). */
d9352518 8058
a0c8462f
AM
8059 decode_complex_addend (&start, &oplen, &len, &wordsz,
8060 &chunksz, &lsb0_p, &signed_p,
8061 &trunc_p, rel->r_addend);
d9352518
DB
8062
8063 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8064
8065 if (lsb0_p)
8066 shift = (start + 1) - len;
8067 else
8068 shift = (8 * wordsz) - (start + len);
8069
5dabe785 8070 /* FIXME: octets_per_byte. */
a0c8462f 8071 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
8072
8073#ifdef DEBUG
8074 printf ("Doing complex reloc: "
8075 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8076 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8077 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8078 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8079 oplen, (unsigned long) x, (unsigned long) mask,
8080 (unsigned long) relocation);
d9352518
DB
8081#endif
8082
cdfeee4f 8083 r = bfd_reloc_ok;
d9352518 8084 if (! trunc_p)
cdfeee4f
AM
8085 /* Now do an overflow check. */
8086 r = bfd_check_overflow ((signed_p
8087 ? complain_overflow_signed
8088 : complain_overflow_unsigned),
8089 len, 0, (8 * wordsz),
8090 relocation);
a0c8462f 8091
d9352518
DB
8092 /* Do the deed. */
8093 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8094
8095#ifdef DEBUG
8096 printf (" relocation: %8.8lx\n"
8097 " shifted mask: %8.8lx\n"
8098 " shifted/masked reloc: %8.8lx\n"
8099 " result: %8.8lx\n",
9ccb8af9
AM
8100 (unsigned long) relocation, (unsigned long) (mask << shift),
8101 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8102#endif
5dabe785 8103 /* FIXME: octets_per_byte. */
d9352518 8104 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 8105 return r;
d9352518
DB
8106}
8107
0e287786
AM
8108/* Functions to read r_offset from external (target order) reloc
8109 entry. Faster than bfd_getl32 et al, because we let the compiler
8110 know the value is aligned. */
53df40a4 8111
0e287786
AM
8112static bfd_vma
8113ext32l_r_offset (const void *p)
53df40a4
AM
8114{
8115 union aligned32
8116 {
8117 uint32_t v;
8118 unsigned char c[4];
8119 };
8120 const union aligned32 *a
0e287786 8121 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8122
8123 uint32_t aval = ( (uint32_t) a->c[0]
8124 | (uint32_t) a->c[1] << 8
8125 | (uint32_t) a->c[2] << 16
8126 | (uint32_t) a->c[3] << 24);
0e287786 8127 return aval;
53df40a4
AM
8128}
8129
0e287786
AM
8130static bfd_vma
8131ext32b_r_offset (const void *p)
53df40a4
AM
8132{
8133 union aligned32
8134 {
8135 uint32_t v;
8136 unsigned char c[4];
8137 };
8138 const union aligned32 *a
0e287786 8139 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8140
8141 uint32_t aval = ( (uint32_t) a->c[0] << 24
8142 | (uint32_t) a->c[1] << 16
8143 | (uint32_t) a->c[2] << 8
8144 | (uint32_t) a->c[3]);
0e287786 8145 return aval;
53df40a4
AM
8146}
8147
8148#ifdef BFD_HOST_64_BIT
0e287786
AM
8149static bfd_vma
8150ext64l_r_offset (const void *p)
53df40a4
AM
8151{
8152 union aligned64
8153 {
8154 uint64_t v;
8155 unsigned char c[8];
8156 };
8157 const union aligned64 *a
0e287786 8158 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8159
8160 uint64_t aval = ( (uint64_t) a->c[0]
8161 | (uint64_t) a->c[1] << 8
8162 | (uint64_t) a->c[2] << 16
8163 | (uint64_t) a->c[3] << 24
8164 | (uint64_t) a->c[4] << 32
8165 | (uint64_t) a->c[5] << 40
8166 | (uint64_t) a->c[6] << 48
8167 | (uint64_t) a->c[7] << 56);
0e287786 8168 return aval;
53df40a4
AM
8169}
8170
0e287786
AM
8171static bfd_vma
8172ext64b_r_offset (const void *p)
53df40a4
AM
8173{
8174 union aligned64
8175 {
8176 uint64_t v;
8177 unsigned char c[8];
8178 };
8179 const union aligned64 *a
0e287786 8180 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8181
8182 uint64_t aval = ( (uint64_t) a->c[0] << 56
8183 | (uint64_t) a->c[1] << 48
8184 | (uint64_t) a->c[2] << 40
8185 | (uint64_t) a->c[3] << 32
8186 | (uint64_t) a->c[4] << 24
8187 | (uint64_t) a->c[5] << 16
8188 | (uint64_t) a->c[6] << 8
8189 | (uint64_t) a->c[7]);
0e287786 8190 return aval;
53df40a4
AM
8191}
8192#endif
8193
c152c796
AM
8194/* When performing a relocatable link, the input relocations are
8195 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8196 referenced must be updated. Update all the relocations found in
8197 RELDATA. */
c152c796 8198
bca6d0e3 8199static bfd_boolean
c152c796 8200elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8201 struct bfd_elf_section_reloc_data *reldata,
8202 bfd_boolean sort)
c152c796
AM
8203{
8204 unsigned int i;
8205 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8206 bfd_byte *erela;
8207 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8208 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8209 bfd_vma r_type_mask;
8210 int r_sym_shift;
d4730f92
BS
8211 unsigned int count = reldata->count;
8212 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8213
d4730f92 8214 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8215 {
8216 swap_in = bed->s->swap_reloc_in;
8217 swap_out = bed->s->swap_reloc_out;
8218 }
d4730f92 8219 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8220 {
8221 swap_in = bed->s->swap_reloca_in;
8222 swap_out = bed->s->swap_reloca_out;
8223 }
8224 else
8225 abort ();
8226
8227 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8228 abort ();
8229
8230 if (bed->s->arch_size == 32)
8231 {
8232 r_type_mask = 0xff;
8233 r_sym_shift = 8;
8234 }
8235 else
8236 {
8237 r_type_mask = 0xffffffff;
8238 r_sym_shift = 32;
8239 }
8240
d4730f92
BS
8241 erela = reldata->hdr->contents;
8242 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8243 {
8244 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8245 unsigned int j;
8246
8247 if (*rel_hash == NULL)
8248 continue;
8249
8250 BFD_ASSERT ((*rel_hash)->indx >= 0);
8251
8252 (*swap_in) (abfd, erela, irela);
8253 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8254 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8255 | (irela[j].r_info & r_type_mask));
8256 (*swap_out) (abfd, irela, erela);
8257 }
53df40a4 8258
0e287786 8259 if (sort && count != 0)
53df40a4 8260 {
0e287786
AM
8261 bfd_vma (*ext_r_off) (const void *);
8262 bfd_vma r_off;
8263 size_t elt_size;
8264 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8265 bfd_byte *buf = NULL;
28dbcedc
AM
8266
8267 if (bed->s->arch_size == 32)
8268 {
8269 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8270 ext_r_off = ext32l_r_offset;
28dbcedc 8271 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8272 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8273 else
8274 abort ();
8275 }
53df40a4 8276 else
28dbcedc 8277 {
53df40a4 8278#ifdef BFD_HOST_64_BIT
28dbcedc 8279 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8280 ext_r_off = ext64l_r_offset;
28dbcedc 8281 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8282 ext_r_off = ext64b_r_offset;
28dbcedc 8283 else
53df40a4 8284#endif
28dbcedc
AM
8285 abort ();
8286 }
0e287786 8287
bca6d0e3
AM
8288 /* Must use a stable sort here. A modified insertion sort,
8289 since the relocs are mostly sorted already. */
0e287786
AM
8290 elt_size = reldata->hdr->sh_entsize;
8291 base = reldata->hdr->contents;
8292 end = base + count * elt_size;
bca6d0e3 8293 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8294 abort ();
8295
8296 /* Ensure the first element is lowest. This acts as a sentinel,
8297 speeding the main loop below. */
8298 r_off = (*ext_r_off) (base);
8299 for (p = loc = base; (p += elt_size) < end; )
8300 {
8301 bfd_vma r_off2 = (*ext_r_off) (p);
8302 if (r_off > r_off2)
8303 {
8304 r_off = r_off2;
8305 loc = p;
8306 }
8307 }
8308 if (loc != base)
8309 {
8310 /* Don't just swap *base and *loc as that changes the order
8311 of the original base[0] and base[1] if they happen to
8312 have the same r_offset. */
bca6d0e3
AM
8313 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8314 memcpy (onebuf, loc, elt_size);
0e287786 8315 memmove (base + elt_size, base, loc - base);
bca6d0e3 8316 memcpy (base, onebuf, elt_size);
0e287786
AM
8317 }
8318
b29b8669 8319 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8320 {
8321 /* base to p is sorted, *p is next to insert. */
8322 r_off = (*ext_r_off) (p);
8323 /* Search the sorted region for location to insert. */
8324 loc = p - elt_size;
8325 while (r_off < (*ext_r_off) (loc))
8326 loc -= elt_size;
8327 loc += elt_size;
8328 if (loc != p)
8329 {
bca6d0e3
AM
8330 /* Chances are there is a run of relocs to insert here,
8331 from one of more input files. Files are not always
8332 linked in order due to the way elf_link_input_bfd is
8333 called. See pr17666. */
8334 size_t sortlen = p - loc;
8335 bfd_vma r_off2 = (*ext_r_off) (loc);
8336 size_t runlen = elt_size;
8337 size_t buf_size = 96 * 1024;
8338 while (p + runlen < end
8339 && (sortlen <= buf_size
8340 || runlen + elt_size <= buf_size)
8341 && r_off2 > (*ext_r_off) (p + runlen))
8342 runlen += elt_size;
8343 if (buf == NULL)
8344 {
8345 buf = bfd_malloc (buf_size);
8346 if (buf == NULL)
8347 return FALSE;
8348 }
8349 if (runlen < sortlen)
8350 {
8351 memcpy (buf, p, runlen);
8352 memmove (loc + runlen, loc, sortlen);
8353 memcpy (loc, buf, runlen);
8354 }
8355 else
8356 {
8357 memcpy (buf, loc, sortlen);
8358 memmove (loc, p, runlen);
8359 memcpy (loc + runlen, buf, sortlen);
8360 }
b29b8669 8361 p += runlen - elt_size;
0e287786
AM
8362 }
8363 }
8364 /* Hashes are no longer valid. */
28dbcedc
AM
8365 free (reldata->hashes);
8366 reldata->hashes = NULL;
bca6d0e3 8367 free (buf);
53df40a4 8368 }
bca6d0e3 8369 return TRUE;
c152c796
AM
8370}
8371
8372struct elf_link_sort_rela
8373{
8374 union {
8375 bfd_vma offset;
8376 bfd_vma sym_mask;
8377 } u;
8378 enum elf_reloc_type_class type;
8379 /* We use this as an array of size int_rels_per_ext_rel. */
8380 Elf_Internal_Rela rela[1];
8381};
8382
8383static int
8384elf_link_sort_cmp1 (const void *A, const void *B)
8385{
a50b1753
NC
8386 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8387 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8388 int relativea, relativeb;
8389
8390 relativea = a->type == reloc_class_relative;
8391 relativeb = b->type == reloc_class_relative;
8392
8393 if (relativea < relativeb)
8394 return 1;
8395 if (relativea > relativeb)
8396 return -1;
8397 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8398 return -1;
8399 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8400 return 1;
8401 if (a->rela->r_offset < b->rela->r_offset)
8402 return -1;
8403 if (a->rela->r_offset > b->rela->r_offset)
8404 return 1;
8405 return 0;
8406}
8407
8408static int
8409elf_link_sort_cmp2 (const void *A, const void *B)
8410{
a50b1753
NC
8411 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8412 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8413
7e612e98 8414 if (a->type < b->type)
c152c796 8415 return -1;
7e612e98 8416 if (a->type > b->type)
c152c796 8417 return 1;
7e612e98 8418 if (a->u.offset < b->u.offset)
c152c796 8419 return -1;
7e612e98 8420 if (a->u.offset > b->u.offset)
c152c796
AM
8421 return 1;
8422 if (a->rela->r_offset < b->rela->r_offset)
8423 return -1;
8424 if (a->rela->r_offset > b->rela->r_offset)
8425 return 1;
8426 return 0;
8427}
8428
8429static size_t
8430elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8431{
3410fea8 8432 asection *dynamic_relocs;
fc66a176
L
8433 asection *rela_dyn;
8434 asection *rel_dyn;
c152c796
AM
8435 bfd_size_type count, size;
8436 size_t i, ret, sort_elt, ext_size;
8437 bfd_byte *sort, *s_non_relative, *p;
8438 struct elf_link_sort_rela *sq;
8439 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8440 int i2e = bed->s->int_rels_per_ext_rel;
8441 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8442 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8443 struct bfd_link_order *lo;
8444 bfd_vma r_sym_mask;
3410fea8 8445 bfd_boolean use_rela;
c152c796 8446
3410fea8
NC
8447 /* Find a dynamic reloc section. */
8448 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8449 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8450 if (rela_dyn != NULL && rela_dyn->size > 0
8451 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8452 {
3410fea8
NC
8453 bfd_boolean use_rela_initialised = FALSE;
8454
8455 /* This is just here to stop gcc from complaining.
8456 It's initialization checking code is not perfect. */
8457 use_rela = TRUE;
8458
8459 /* Both sections are present. Examine the sizes
8460 of the indirect sections to help us choose. */
8461 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8462 if (lo->type == bfd_indirect_link_order)
8463 {
8464 asection *o = lo->u.indirect.section;
8465
8466 if ((o->size % bed->s->sizeof_rela) == 0)
8467 {
8468 if ((o->size % bed->s->sizeof_rel) == 0)
8469 /* Section size is divisible by both rel and rela sizes.
8470 It is of no help to us. */
8471 ;
8472 else
8473 {
8474 /* Section size is only divisible by rela. */
8475 if (use_rela_initialised && (use_rela == FALSE))
8476 {
8477 _bfd_error_handler
8478 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8479 bfd_set_error (bfd_error_invalid_operation);
8480 return 0;
8481 }
8482 else
8483 {
8484 use_rela = TRUE;
8485 use_rela_initialised = TRUE;
8486 }
8487 }
8488 }
8489 else if ((o->size % bed->s->sizeof_rel) == 0)
8490 {
8491 /* Section size is only divisible by rel. */
8492 if (use_rela_initialised && (use_rela == TRUE))
8493 {
8494 _bfd_error_handler
8495 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8496 bfd_set_error (bfd_error_invalid_operation);
8497 return 0;
8498 }
8499 else
8500 {
8501 use_rela = FALSE;
8502 use_rela_initialised = TRUE;
8503 }
8504 }
8505 else
8506 {
8507 /* The section size is not divisible by either - something is wrong. */
8508 _bfd_error_handler
8509 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8510 bfd_set_error (bfd_error_invalid_operation);
8511 return 0;
8512 }
8513 }
8514
8515 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8516 if (lo->type == bfd_indirect_link_order)
8517 {
8518 asection *o = lo->u.indirect.section;
8519
8520 if ((o->size % bed->s->sizeof_rela) == 0)
8521 {
8522 if ((o->size % bed->s->sizeof_rel) == 0)
8523 /* Section size is divisible by both rel and rela sizes.
8524 It is of no help to us. */
8525 ;
8526 else
8527 {
8528 /* Section size is only divisible by rela. */
8529 if (use_rela_initialised && (use_rela == FALSE))
8530 {
8531 _bfd_error_handler
8532 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8533 bfd_set_error (bfd_error_invalid_operation);
8534 return 0;
8535 }
8536 else
8537 {
8538 use_rela = TRUE;
8539 use_rela_initialised = TRUE;
8540 }
8541 }
8542 }
8543 else if ((o->size % bed->s->sizeof_rel) == 0)
8544 {
8545 /* Section size is only divisible by rel. */
8546 if (use_rela_initialised && (use_rela == TRUE))
8547 {
8548 _bfd_error_handler
8549 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8550 bfd_set_error (bfd_error_invalid_operation);
8551 return 0;
8552 }
8553 else
8554 {
8555 use_rela = FALSE;
8556 use_rela_initialised = TRUE;
8557 }
8558 }
8559 else
8560 {
8561 /* The section size is not divisible by either - something is wrong. */
8562 _bfd_error_handler
8563 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8564 bfd_set_error (bfd_error_invalid_operation);
8565 return 0;
8566 }
8567 }
8568
8569 if (! use_rela_initialised)
8570 /* Make a guess. */
8571 use_rela = TRUE;
c152c796 8572 }
fc66a176
L
8573 else if (rela_dyn != NULL && rela_dyn->size > 0)
8574 use_rela = TRUE;
8575 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8576 use_rela = FALSE;
c152c796 8577 else
fc66a176 8578 return 0;
3410fea8
NC
8579
8580 if (use_rela)
c152c796 8581 {
3410fea8 8582 dynamic_relocs = rela_dyn;
c152c796
AM
8583 ext_size = bed->s->sizeof_rela;
8584 swap_in = bed->s->swap_reloca_in;
8585 swap_out = bed->s->swap_reloca_out;
8586 }
3410fea8
NC
8587 else
8588 {
8589 dynamic_relocs = rel_dyn;
8590 ext_size = bed->s->sizeof_rel;
8591 swap_in = bed->s->swap_reloc_in;
8592 swap_out = bed->s->swap_reloc_out;
8593 }
c152c796
AM
8594
8595 size = 0;
3410fea8 8596 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8597 if (lo->type == bfd_indirect_link_order)
3410fea8 8598 size += lo->u.indirect.section->size;
c152c796 8599
3410fea8 8600 if (size != dynamic_relocs->size)
c152c796
AM
8601 return 0;
8602
8603 sort_elt = (sizeof (struct elf_link_sort_rela)
8604 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8605
8606 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8607 if (count == 0)
8608 return 0;
a50b1753 8609 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8610
c152c796
AM
8611 if (sort == NULL)
8612 {
8613 (*info->callbacks->warning)
8614 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8615 return 0;
8616 }
8617
8618 if (bed->s->arch_size == 32)
8619 r_sym_mask = ~(bfd_vma) 0xff;
8620 else
8621 r_sym_mask = ~(bfd_vma) 0xffffffff;
8622
3410fea8 8623 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8624 if (lo->type == bfd_indirect_link_order)
8625 {
8626 bfd_byte *erel, *erelend;
8627 asection *o = lo->u.indirect.section;
8628
1da212d6
AM
8629 if (o->contents == NULL && o->size != 0)
8630 {
8631 /* This is a reloc section that is being handled as a normal
8632 section. See bfd_section_from_shdr. We can't combine
8633 relocs in this case. */
8634 free (sort);
8635 return 0;
8636 }
c152c796 8637 erel = o->contents;
eea6121a 8638 erelend = o->contents + o->size;
5dabe785 8639 /* FIXME: octets_per_byte. */
c152c796 8640 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8641
c152c796
AM
8642 while (erel < erelend)
8643 {
8644 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8645
c152c796 8646 (*swap_in) (abfd, erel, s->rela);
7e612e98 8647 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8648 s->u.sym_mask = r_sym_mask;
8649 p += sort_elt;
8650 erel += ext_size;
8651 }
8652 }
8653
8654 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8655
8656 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8657 {
8658 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8659 if (s->type != reloc_class_relative)
8660 break;
8661 }
8662 ret = i;
8663 s_non_relative = p;
8664
8665 sq = (struct elf_link_sort_rela *) s_non_relative;
8666 for (; i < count; i++, p += sort_elt)
8667 {
8668 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8669 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8670 sq = sp;
8671 sp->u.offset = sq->rela->r_offset;
8672 }
8673
8674 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8675
3410fea8 8676 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8677 if (lo->type == bfd_indirect_link_order)
8678 {
8679 bfd_byte *erel, *erelend;
8680 asection *o = lo->u.indirect.section;
8681
8682 erel = o->contents;
eea6121a 8683 erelend = o->contents + o->size;
5dabe785 8684 /* FIXME: octets_per_byte. */
c152c796
AM
8685 p = sort + o->output_offset / ext_size * sort_elt;
8686 while (erel < erelend)
8687 {
8688 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8689 (*swap_out) (abfd, s->rela, erel);
8690 p += sort_elt;
8691 erel += ext_size;
8692 }
8693 }
8694
8695 free (sort);
3410fea8 8696 *psec = dynamic_relocs;
c152c796
AM
8697 return ret;
8698}
8699
ef10c3ac 8700/* Add a symbol to the output symbol string table. */
c152c796 8701
6e0b88f1 8702static int
ef10c3ac
L
8703elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8704 const char *name,
8705 Elf_Internal_Sym *elfsym,
8706 asection *input_sec,
8707 struct elf_link_hash_entry *h)
c152c796 8708{
6e0b88f1 8709 int (*output_symbol_hook)
c152c796
AM
8710 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8711 struct elf_link_hash_entry *);
ef10c3ac 8712 struct elf_link_hash_table *hash_table;
c152c796 8713 const struct elf_backend_data *bed;
ef10c3ac 8714 bfd_size_type strtabsize;
c152c796 8715
8539e4e8
AM
8716 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8717
8b127cbc 8718 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8719 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8720 if (output_symbol_hook != NULL)
8721 {
8b127cbc 8722 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8723 if (ret != 1)
8724 return ret;
c152c796
AM
8725 }
8726
ef10c3ac
L
8727 if (name == NULL
8728 || *name == '\0'
8729 || (input_sec->flags & SEC_EXCLUDE))
8730 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8731 else
8732 {
ef10c3ac
L
8733 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8734 to get the final offset for st_name. */
8735 elfsym->st_name
8736 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8737 name, FALSE);
c152c796 8738 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8739 return 0;
c152c796
AM
8740 }
8741
ef10c3ac
L
8742 hash_table = elf_hash_table (flinfo->info);
8743 strtabsize = hash_table->strtabsize;
8744 if (strtabsize <= hash_table->strtabcount)
c152c796 8745 {
ef10c3ac
L
8746 strtabsize += strtabsize;
8747 hash_table->strtabsize = strtabsize;
8748 strtabsize *= sizeof (*hash_table->strtab);
8749 hash_table->strtab
8750 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8751 strtabsize);
8752 if (hash_table->strtab == NULL)
6e0b88f1 8753 return 0;
c152c796 8754 }
ef10c3ac
L
8755 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8756 hash_table->strtab[hash_table->strtabcount].dest_index
8757 = hash_table->strtabcount;
8758 hash_table->strtab[hash_table->strtabcount].destshndx_index
8759 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8760
8761 bfd_get_symcount (flinfo->output_bfd) += 1;
8762 hash_table->strtabcount += 1;
8763
8764 return 1;
8765}
8766
8767/* Swap symbols out to the symbol table and flush the output symbols to
8768 the file. */
8769
8770static bfd_boolean
8771elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8772{
8773 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8774 bfd_size_type amt, i;
8775 const struct elf_backend_data *bed;
8776 bfd_byte *symbuf;
8777 Elf_Internal_Shdr *hdr;
8778 file_ptr pos;
8779 bfd_boolean ret;
8780
8781 if (!hash_table->strtabcount)
8782 return TRUE;
8783
8784 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8785
8786 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8787
ef10c3ac
L
8788 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8789 symbuf = (bfd_byte *) bfd_malloc (amt);
8790 if (symbuf == NULL)
8791 return FALSE;
1b786873 8792
ef10c3ac 8793 if (flinfo->symshndxbuf)
c152c796 8794 {
ef10c3ac
L
8795 amt = (sizeof (Elf_External_Sym_Shndx)
8796 * (bfd_get_symcount (flinfo->output_bfd)));
8797 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8798 if (flinfo->symshndxbuf == NULL)
c152c796 8799 {
ef10c3ac
L
8800 free (symbuf);
8801 return FALSE;
c152c796 8802 }
c152c796
AM
8803 }
8804
ef10c3ac
L
8805 for (i = 0; i < hash_table->strtabcount; i++)
8806 {
8807 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8808 if (elfsym->sym.st_name == (unsigned long) -1)
8809 elfsym->sym.st_name = 0;
8810 else
8811 elfsym->sym.st_name
8812 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8813 elfsym->sym.st_name);
8814 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8815 ((bfd_byte *) symbuf
8816 + (elfsym->dest_index
8817 * bed->s->sizeof_sym)),
8818 (flinfo->symshndxbuf
8819 + elfsym->destshndx_index));
8820 }
8821
8822 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8823 pos = hdr->sh_offset + hdr->sh_size;
8824 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8825 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8826 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8827 {
8828 hdr->sh_size += amt;
8829 ret = TRUE;
8830 }
8831 else
8832 ret = FALSE;
c152c796 8833
ef10c3ac
L
8834 free (symbuf);
8835
8836 free (hash_table->strtab);
8837 hash_table->strtab = NULL;
8838
8839 return ret;
c152c796
AM
8840}
8841
c0d5a53d
L
8842/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8843
8844static bfd_boolean
8845check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8846{
4fbb74a6
AM
8847 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8848 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8849 {
8850 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8851 beyond 64k. */
c0d5a53d
L
8852 (*_bfd_error_handler)
8853 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8854 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8855 bfd_set_error (bfd_error_nonrepresentable_section);
8856 return FALSE;
8857 }
8858 return TRUE;
8859}
8860
c152c796
AM
8861/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8862 allowing an unsatisfied unversioned symbol in the DSO to match a
8863 versioned symbol that would normally require an explicit version.
8864 We also handle the case that a DSO references a hidden symbol
8865 which may be satisfied by a versioned symbol in another DSO. */
8866
8867static bfd_boolean
8868elf_link_check_versioned_symbol (struct bfd_link_info *info,
8869 const struct elf_backend_data *bed,
8870 struct elf_link_hash_entry *h)
8871{
8872 bfd *abfd;
8873 struct elf_link_loaded_list *loaded;
8874
8875 if (!is_elf_hash_table (info->hash))
8876 return FALSE;
8877
90c984fc
L
8878 /* Check indirect symbol. */
8879 while (h->root.type == bfd_link_hash_indirect)
8880 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8881
c152c796
AM
8882 switch (h->root.type)
8883 {
8884 default:
8885 abfd = NULL;
8886 break;
8887
8888 case bfd_link_hash_undefined:
8889 case bfd_link_hash_undefweak:
8890 abfd = h->root.u.undef.abfd;
8891 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8892 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8893 return FALSE;
8894 break;
8895
8896 case bfd_link_hash_defined:
8897 case bfd_link_hash_defweak:
8898 abfd = h->root.u.def.section->owner;
8899 break;
8900
8901 case bfd_link_hash_common:
8902 abfd = h->root.u.c.p->section->owner;
8903 break;
8904 }
8905 BFD_ASSERT (abfd != NULL);
8906
8907 for (loaded = elf_hash_table (info)->loaded;
8908 loaded != NULL;
8909 loaded = loaded->next)
8910 {
8911 bfd *input;
8912 Elf_Internal_Shdr *hdr;
8913 bfd_size_type symcount;
8914 bfd_size_type extsymcount;
8915 bfd_size_type extsymoff;
8916 Elf_Internal_Shdr *versymhdr;
8917 Elf_Internal_Sym *isym;
8918 Elf_Internal_Sym *isymend;
8919 Elf_Internal_Sym *isymbuf;
8920 Elf_External_Versym *ever;
8921 Elf_External_Versym *extversym;
8922
8923 input = loaded->abfd;
8924
8925 /* We check each DSO for a possible hidden versioned definition. */
8926 if (input == abfd
8927 || (input->flags & DYNAMIC) == 0
8928 || elf_dynversym (input) == 0)
8929 continue;
8930
8931 hdr = &elf_tdata (input)->dynsymtab_hdr;
8932
8933 symcount = hdr->sh_size / bed->s->sizeof_sym;
8934 if (elf_bad_symtab (input))
8935 {
8936 extsymcount = symcount;
8937 extsymoff = 0;
8938 }
8939 else
8940 {
8941 extsymcount = symcount - hdr->sh_info;
8942 extsymoff = hdr->sh_info;
8943 }
8944
8945 if (extsymcount == 0)
8946 continue;
8947
8948 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8949 NULL, NULL, NULL);
8950 if (isymbuf == NULL)
8951 return FALSE;
8952
8953 /* Read in any version definitions. */
8954 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8955 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8956 if (extversym == NULL)
8957 goto error_ret;
8958
8959 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8960 || (bfd_bread (extversym, versymhdr->sh_size, input)
8961 != versymhdr->sh_size))
8962 {
8963 free (extversym);
8964 error_ret:
8965 free (isymbuf);
8966 return FALSE;
8967 }
8968
8969 ever = extversym + extsymoff;
8970 isymend = isymbuf + extsymcount;
8971 for (isym = isymbuf; isym < isymend; isym++, ever++)
8972 {
8973 const char *name;
8974 Elf_Internal_Versym iver;
8975 unsigned short version_index;
8976
8977 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8978 || isym->st_shndx == SHN_UNDEF)
8979 continue;
8980
8981 name = bfd_elf_string_from_elf_section (input,
8982 hdr->sh_link,
8983 isym->st_name);
8984 if (strcmp (name, h->root.root.string) != 0)
8985 continue;
8986
8987 _bfd_elf_swap_versym_in (input, ever, &iver);
8988
d023c380
L
8989 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8990 && !(h->def_regular
8991 && h->forced_local))
c152c796
AM
8992 {
8993 /* If we have a non-hidden versioned sym, then it should
d023c380
L
8994 have provided a definition for the undefined sym unless
8995 it is defined in a non-shared object and forced local.
8996 */
c152c796
AM
8997 abort ();
8998 }
8999
9000 version_index = iver.vs_vers & VERSYM_VERSION;
9001 if (version_index == 1 || version_index == 2)
9002 {
9003 /* This is the base or first version. We can use it. */
9004 free (extversym);
9005 free (isymbuf);
9006 return TRUE;
9007 }
9008 }
9009
9010 free (extversym);
9011 free (isymbuf);
9012 }
9013
9014 return FALSE;
9015}
9016
9017/* Add an external symbol to the symbol table. This is called from
9018 the hash table traversal routine. When generating a shared object,
9019 we go through the symbol table twice. The first time we output
9020 anything that might have been forced to local scope in a version
9021 script. The second time we output the symbols that are still
9022 global symbols. */
9023
9024static bfd_boolean
7686d77d 9025elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9026{
7686d77d 9027 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9028 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9029 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9030 bfd_boolean strip;
9031 Elf_Internal_Sym sym;
9032 asection *input_sec;
9033 const struct elf_backend_data *bed;
6e0b88f1
AM
9034 long indx;
9035 int ret;
6e33951e
L
9036 /* A symbol is bound locally if it is forced local or it is locally
9037 defined, hidden versioned, not referenced by shared library and
9038 not exported when linking executable. */
9039 bfd_boolean local_bind = (h->forced_local
0e1862bb 9040 || (bfd_link_executable (flinfo->info)
6e33951e
L
9041 && !flinfo->info->export_dynamic
9042 && !h->dynamic
9043 && !h->ref_dynamic
9044 && h->def_regular
422f1182 9045 && h->versioned == versioned_hidden));
c152c796
AM
9046
9047 if (h->root.type == bfd_link_hash_warning)
9048 {
9049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9050 if (h->root.type == bfd_link_hash_new)
9051 return TRUE;
9052 }
9053
9054 /* Decide whether to output this symbol in this pass. */
9055 if (eoinfo->localsyms)
9056 {
6e33951e 9057 if (!local_bind)
c152c796
AM
9058 return TRUE;
9059 }
9060 else
9061 {
6e33951e 9062 if (local_bind)
c152c796
AM
9063 return TRUE;
9064 }
9065
8b127cbc 9066 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9067
12ac1cf5 9068 if (h->root.type == bfd_link_hash_undefined)
c152c796 9069 {
12ac1cf5
NC
9070 /* If we have an undefined symbol reference here then it must have
9071 come from a shared library that is being linked in. (Undefined
98da7939
L
9072 references in regular files have already been handled unless
9073 they are in unreferenced sections which are removed by garbage
9074 collection). */
12ac1cf5
NC
9075 bfd_boolean ignore_undef = FALSE;
9076
9077 /* Some symbols may be special in that the fact that they're
9078 undefined can be safely ignored - let backend determine that. */
9079 if (bed->elf_backend_ignore_undef_symbol)
9080 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9081
9082 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9083 if (!ignore_undef
12ac1cf5 9084 && h->ref_dynamic
8b127cbc
AM
9085 && (!h->ref_regular || flinfo->info->gc_sections)
9086 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9087 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9088 {
9089 if (!(flinfo->info->callbacks->undefined_symbol
9090 (flinfo->info, h->root.root.string,
9091 h->ref_regular ? NULL : h->root.u.undef.abfd,
9092 NULL, 0,
9093 (flinfo->info->unresolved_syms_in_shared_libs
9094 == RM_GENERATE_ERROR))))
12ac1cf5 9095 {
17d078c5 9096 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
9097 eoinfo->failed = TRUE;
9098 return FALSE;
9099 }
c152c796
AM
9100 }
9101 }
9102
9103 /* We should also warn if a forced local symbol is referenced from
9104 shared libraries. */
0e1862bb 9105 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9106 && h->forced_local
9107 && h->ref_dynamic
371a5866 9108 && h->def_regular
f5385ebf 9109 && !h->dynamic_def
ee659f1f 9110 && h->ref_dynamic_nonweak
8b127cbc 9111 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9112 {
17d078c5
AM
9113 bfd *def_bfd;
9114 const char *msg;
90c984fc
L
9115 struct elf_link_hash_entry *hi = h;
9116
9117 /* Check indirect symbol. */
9118 while (hi->root.type == bfd_link_hash_indirect)
9119 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9120
9121 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9122 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9123 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9124 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9125 else
9126 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9127 def_bfd = flinfo->output_bfd;
90c984fc
L
9128 if (hi->root.u.def.section != bfd_abs_section_ptr)
9129 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9130 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9131 h->root.root.string);
9132 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9133 eoinfo->failed = TRUE;
9134 return FALSE;
9135 }
9136
9137 /* We don't want to output symbols that have never been mentioned by
9138 a regular file, or that we have been told to strip. However, if
9139 h->indx is set to -2, the symbol is used by a reloc and we must
9140 output it. */
d983c8c5 9141 strip = FALSE;
c152c796 9142 if (h->indx == -2)
d983c8c5 9143 ;
f5385ebf 9144 else if ((h->def_dynamic
77cfaee6
AM
9145 || h->ref_dynamic
9146 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9147 && !h->def_regular
9148 && !h->ref_regular)
c152c796 9149 strip = TRUE;
8b127cbc 9150 else if (flinfo->info->strip == strip_all)
c152c796 9151 strip = TRUE;
8b127cbc
AM
9152 else if (flinfo->info->strip == strip_some
9153 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9154 h->root.root.string, FALSE, FALSE) == NULL)
9155 strip = TRUE;
d56d55e7
AM
9156 else if ((h->root.type == bfd_link_hash_defined
9157 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9158 && ((flinfo->info->strip_discarded
dbaa2011 9159 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9160 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9161 && h->root.u.def.section->owner != NULL
d56d55e7 9162 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9163 strip = TRUE;
9e2278f5
AM
9164 else if ((h->root.type == bfd_link_hash_undefined
9165 || h->root.type == bfd_link_hash_undefweak)
9166 && h->root.u.undef.abfd != NULL
9167 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9168 strip = TRUE;
c152c796
AM
9169
9170 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9171 nothing else to do. However, if it is a forced local symbol or
9172 an ifunc symbol we need to give the backend finish_dynamic_symbol
9173 function a chance to make it dynamic. */
c152c796
AM
9174 if (strip
9175 && h->dynindx == -1
57ca8ac7 9176 && h->type != STT_GNU_IFUNC
f5385ebf 9177 && !h->forced_local)
c152c796
AM
9178 return TRUE;
9179
9180 sym.st_value = 0;
9181 sym.st_size = h->size;
9182 sym.st_other = h->other;
6e33951e 9183 if (local_bind)
935bd1e0
L
9184 {
9185 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9186 /* Turn off visibility on local symbol. */
9187 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9188 }
02acbe22
L
9189 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9190 else if (h->unique_global && h->def_regular)
3e7a7d11 9191 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
9192 else if (h->root.type == bfd_link_hash_undefweak
9193 || h->root.type == bfd_link_hash_defweak)
9194 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9195 else
9196 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 9197 sym.st_target_internal = h->target_internal;
c152c796
AM
9198
9199 switch (h->root.type)
9200 {
9201 default:
9202 case bfd_link_hash_new:
9203 case bfd_link_hash_warning:
9204 abort ();
9205 return FALSE;
9206
9207 case bfd_link_hash_undefined:
9208 case bfd_link_hash_undefweak:
9209 input_sec = bfd_und_section_ptr;
9210 sym.st_shndx = SHN_UNDEF;
9211 break;
9212
9213 case bfd_link_hash_defined:
9214 case bfd_link_hash_defweak:
9215 {
9216 input_sec = h->root.u.def.section;
9217 if (input_sec->output_section != NULL)
9218 {
9219 sym.st_shndx =
8b127cbc 9220 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9221 input_sec->output_section);
9222 if (sym.st_shndx == SHN_BAD)
9223 {
9224 (*_bfd_error_handler)
d003868e 9225 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9226 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9227 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9228 eoinfo->failed = TRUE;
9229 return FALSE;
9230 }
9231
9232 /* ELF symbols in relocatable files are section relative,
9233 but in nonrelocatable files they are virtual
9234 addresses. */
9235 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9236 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9237 {
9238 sym.st_value += input_sec->output_section->vma;
9239 if (h->type == STT_TLS)
9240 {
8b127cbc 9241 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9242 if (tls_sec != NULL)
9243 sym.st_value -= tls_sec->vma;
c152c796
AM
9244 }
9245 }
9246 }
9247 else
9248 {
9249 BFD_ASSERT (input_sec->owner == NULL
9250 || (input_sec->owner->flags & DYNAMIC) != 0);
9251 sym.st_shndx = SHN_UNDEF;
9252 input_sec = bfd_und_section_ptr;
9253 }
9254 }
9255 break;
9256
9257 case bfd_link_hash_common:
9258 input_sec = h->root.u.c.p->section;
a4d8e49b 9259 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9260 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9261 break;
9262
9263 case bfd_link_hash_indirect:
9264 /* These symbols are created by symbol versioning. They point
9265 to the decorated version of the name. For example, if the
9266 symbol foo@@GNU_1.2 is the default, which should be used when
9267 foo is used with no version, then we add an indirect symbol
9268 foo which points to foo@@GNU_1.2. We ignore these symbols,
9269 since the indirected symbol is already in the hash table. */
9270 return TRUE;
9271 }
9272
9273 /* Give the processor backend a chance to tweak the symbol value,
9274 and also to finish up anything that needs to be done for this
9275 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9276 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9277 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9278 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9279 && h->def_regular
0e1862bb 9280 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9281 || ((h->dynindx != -1
9282 || h->forced_local)
0e1862bb 9283 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9284 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9285 || h->root.type != bfd_link_hash_undefweak))
9286 || !h->forced_local)
8b127cbc 9287 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9288 {
9289 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9290 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9291 {
9292 eoinfo->failed = TRUE;
9293 return FALSE;
9294 }
9295 }
9296
9297 /* If we are marking the symbol as undefined, and there are no
9298 non-weak references to this symbol from a regular object, then
9299 mark the symbol as weak undefined; if there are non-weak
9300 references, mark the symbol as strong. We can't do this earlier,
9301 because it might not be marked as undefined until the
9302 finish_dynamic_symbol routine gets through with it. */
9303 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9304 && h->ref_regular
c152c796
AM
9305 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9306 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9307 {
9308 int bindtype;
2955ec4c
L
9309 unsigned int type = ELF_ST_TYPE (sym.st_info);
9310
9311 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9312 if (type == STT_GNU_IFUNC)
9313 type = STT_FUNC;
c152c796 9314
f5385ebf 9315 if (h->ref_regular_nonweak)
c152c796
AM
9316 bindtype = STB_GLOBAL;
9317 else
9318 bindtype = STB_WEAK;
2955ec4c 9319 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9320 }
9321
bda987c2
CD
9322 /* If this is a symbol defined in a dynamic library, don't use the
9323 symbol size from the dynamic library. Relinking an executable
9324 against a new library may introduce gratuitous changes in the
9325 executable's symbols if we keep the size. */
9326 if (sym.st_shndx == SHN_UNDEF
9327 && !h->def_regular
9328 && h->def_dynamic)
9329 sym.st_size = 0;
9330
c152c796
AM
9331 /* If a non-weak symbol with non-default visibility is not defined
9332 locally, it is a fatal error. */
0e1862bb 9333 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9334 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9335 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9336 && h->root.type == bfd_link_hash_undefined
f5385ebf 9337 && !h->def_regular)
c152c796 9338 {
17d078c5
AM
9339 const char *msg;
9340
9341 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9342 msg = _("%B: protected symbol `%s' isn't defined");
9343 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9344 msg = _("%B: internal symbol `%s' isn't defined");
9345 else
9346 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9347 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9348 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9349 eoinfo->failed = TRUE;
9350 return FALSE;
9351 }
9352
9353 /* If this symbol should be put in the .dynsym section, then put it
9354 there now. We already know the symbol index. We also fill in
9355 the entry in the .hash section. */
cae1fbbb 9356 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9357 && h->dynindx != -1
8b127cbc 9358 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9359 {
c152c796
AM
9360 bfd_byte *esym;
9361
90c984fc
L
9362 /* Since there is no version information in the dynamic string,
9363 if there is no version info in symbol version section, we will
1659f720 9364 have a run-time problem if not linking executable, referenced
6e33951e
L
9365 by shared library, not locally defined, or not bound locally.
9366 */
1659f720 9367 if (h->verinfo.verdef == NULL
6e33951e 9368 && !local_bind
0e1862bb 9369 && (!bfd_link_executable (flinfo->info)
1659f720
L
9370 || h->ref_dynamic
9371 || !h->def_regular))
90c984fc
L
9372 {
9373 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9374
9375 if (p && p [1] != '\0')
9376 {
9377 (*_bfd_error_handler)
9378 (_("%B: No symbol version section for versioned symbol `%s'"),
9379 flinfo->output_bfd, h->root.root.string);
9380 eoinfo->failed = TRUE;
9381 return FALSE;
9382 }
9383 }
9384
c152c796 9385 sym.st_name = h->dynstr_index;
cae1fbbb
L
9386 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9387 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9388 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9389 {
9390 eoinfo->failed = TRUE;
9391 return FALSE;
9392 }
8b127cbc 9393 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9394
8b127cbc 9395 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9396 {
9397 size_t hash_entry_size;
9398 bfd_byte *bucketpos;
9399 bfd_vma chain;
41198d0c
L
9400 size_t bucketcount;
9401 size_t bucket;
9402
8b127cbc 9403 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9404 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9405
9406 hash_entry_size
8b127cbc
AM
9407 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9408 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9409 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9410 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9411 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9412 bucketpos);
9413 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9414 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9415 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9416 }
c152c796 9417
8b127cbc 9418 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9419 {
9420 Elf_Internal_Versym iversym;
9421 Elf_External_Versym *eversym;
9422
f5385ebf 9423 if (!h->def_regular)
c152c796 9424 {
7b20f099
AM
9425 if (h->verinfo.verdef == NULL
9426 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9427 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9428 iversym.vs_vers = 0;
9429 else
9430 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9431 }
9432 else
9433 {
9434 if (h->verinfo.vertree == NULL)
9435 iversym.vs_vers = 1;
9436 else
9437 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9438 if (flinfo->info->create_default_symver)
3e3b46e5 9439 iversym.vs_vers++;
c152c796
AM
9440 }
9441
422f1182 9442 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9443 defined locally. */
422f1182 9444 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9445 iversym.vs_vers |= VERSYM_HIDDEN;
9446
8b127cbc 9447 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9448 eversym += h->dynindx;
8b127cbc 9449 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9450 }
9451 }
9452
d983c8c5
AM
9453 /* If the symbol is undefined, and we didn't output it to .dynsym,
9454 strip it from .symtab too. Obviously we can't do this for
9455 relocatable output or when needed for --emit-relocs. */
9456 else if (input_sec == bfd_und_section_ptr
9457 && h->indx != -2
0e1862bb 9458 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9459 return TRUE;
9460 /* Also strip others that we couldn't earlier due to dynamic symbol
9461 processing. */
9462 if (strip)
9463 return TRUE;
9464 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9465 return TRUE;
9466
2ec55de3
AM
9467 /* Output a FILE symbol so that following locals are not associated
9468 with the wrong input file. We need one for forced local symbols
9469 if we've seen more than one FILE symbol or when we have exactly
9470 one FILE symbol but global symbols are present in a file other
9471 than the one with the FILE symbol. We also need one if linker
9472 defined symbols are present. In practice these conditions are
9473 always met, so just emit the FILE symbol unconditionally. */
9474 if (eoinfo->localsyms
9475 && !eoinfo->file_sym_done
9476 && eoinfo->flinfo->filesym_count != 0)
9477 {
9478 Elf_Internal_Sym fsym;
9479
9480 memset (&fsym, 0, sizeof (fsym));
9481 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9482 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9483 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9484 bfd_und_section_ptr, NULL))
2ec55de3
AM
9485 return FALSE;
9486
9487 eoinfo->file_sym_done = TRUE;
9488 }
9489
8b127cbc 9490 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9491 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9492 input_sec, h);
6e0b88f1 9493 if (ret == 0)
c152c796
AM
9494 {
9495 eoinfo->failed = TRUE;
9496 return FALSE;
9497 }
6e0b88f1
AM
9498 else if (ret == 1)
9499 h->indx = indx;
9500 else if (h->indx == -2)
9501 abort();
c152c796
AM
9502
9503 return TRUE;
9504}
9505
cdd3575c
AM
9506/* Return TRUE if special handling is done for relocs in SEC against
9507 symbols defined in discarded sections. */
9508
c152c796
AM
9509static bfd_boolean
9510elf_section_ignore_discarded_relocs (asection *sec)
9511{
9512 const struct elf_backend_data *bed;
9513
cdd3575c
AM
9514 switch (sec->sec_info_type)
9515 {
dbaa2011
AM
9516 case SEC_INFO_TYPE_STABS:
9517 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9518 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9519 return TRUE;
9520 default:
9521 break;
9522 }
c152c796
AM
9523
9524 bed = get_elf_backend_data (sec->owner);
9525 if (bed->elf_backend_ignore_discarded_relocs != NULL
9526 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9527 return TRUE;
9528
9529 return FALSE;
9530}
9531
9e66c942
AM
9532/* Return a mask saying how ld should treat relocations in SEC against
9533 symbols defined in discarded sections. If this function returns
9534 COMPLAIN set, ld will issue a warning message. If this function
9535 returns PRETEND set, and the discarded section was link-once and the
9536 same size as the kept link-once section, ld will pretend that the
9537 symbol was actually defined in the kept section. Otherwise ld will
9538 zero the reloc (at least that is the intent, but some cooperation by
9539 the target dependent code is needed, particularly for REL targets). */
9540
8a696751
AM
9541unsigned int
9542_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9543{
9e66c942 9544 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9545 return PRETEND;
cdd3575c
AM
9546
9547 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9548 return 0;
cdd3575c
AM
9549
9550 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9551 return 0;
cdd3575c 9552
9e66c942 9553 return COMPLAIN | PRETEND;
cdd3575c
AM
9554}
9555
3d7f7666
L
9556/* Find a match between a section and a member of a section group. */
9557
9558static asection *
c0f00686
L
9559match_group_member (asection *sec, asection *group,
9560 struct bfd_link_info *info)
3d7f7666
L
9561{
9562 asection *first = elf_next_in_group (group);
9563 asection *s = first;
9564
9565 while (s != NULL)
9566 {
c0f00686 9567 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9568 return s;
9569
83180ade 9570 s = elf_next_in_group (s);
3d7f7666
L
9571 if (s == first)
9572 break;
9573 }
9574
9575 return NULL;
9576}
9577
01b3c8ab 9578/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9579 to replace it. Return the replacement if it is OK. Otherwise return
9580 NULL. */
01b3c8ab
L
9581
9582asection *
c0f00686 9583_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9584{
9585 asection *kept;
9586
9587 kept = sec->kept_section;
9588 if (kept != NULL)
9589 {
c2370991 9590 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9591 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9592 if (kept != NULL
9593 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9594 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9595 kept = NULL;
c2370991 9596 sec->kept_section = kept;
01b3c8ab
L
9597 }
9598 return kept;
9599}
9600
c152c796
AM
9601/* Link an input file into the linker output file. This function
9602 handles all the sections and relocations of the input file at once.
9603 This is so that we only have to read the local symbols once, and
9604 don't have to keep them in memory. */
9605
9606static bfd_boolean
8b127cbc 9607elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9608{
ece5ef60 9609 int (*relocate_section)
c152c796
AM
9610 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9611 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9612 bfd *output_bfd;
9613 Elf_Internal_Shdr *symtab_hdr;
9614 size_t locsymcount;
9615 size_t extsymoff;
9616 Elf_Internal_Sym *isymbuf;
9617 Elf_Internal_Sym *isym;
9618 Elf_Internal_Sym *isymend;
9619 long *pindex;
9620 asection **ppsection;
9621 asection *o;
9622 const struct elf_backend_data *bed;
c152c796 9623 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9624 bfd_size_type address_size;
9625 bfd_vma r_type_mask;
9626 int r_sym_shift;
ffbc01cc 9627 bfd_boolean have_file_sym = FALSE;
c152c796 9628
8b127cbc 9629 output_bfd = flinfo->output_bfd;
c152c796
AM
9630 bed = get_elf_backend_data (output_bfd);
9631 relocate_section = bed->elf_backend_relocate_section;
9632
9633 /* If this is a dynamic object, we don't want to do anything here:
9634 we don't want the local symbols, and we don't want the section
9635 contents. */
9636 if ((input_bfd->flags & DYNAMIC) != 0)
9637 return TRUE;
9638
c152c796
AM
9639 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9640 if (elf_bad_symtab (input_bfd))
9641 {
9642 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9643 extsymoff = 0;
9644 }
9645 else
9646 {
9647 locsymcount = symtab_hdr->sh_info;
9648 extsymoff = symtab_hdr->sh_info;
9649 }
9650
9651 /* Read the local symbols. */
9652 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9653 if (isymbuf == NULL && locsymcount != 0)
9654 {
9655 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9656 flinfo->internal_syms,
9657 flinfo->external_syms,
9658 flinfo->locsym_shndx);
c152c796
AM
9659 if (isymbuf == NULL)
9660 return FALSE;
9661 }
9662
9663 /* Find local symbol sections and adjust values of symbols in
9664 SEC_MERGE sections. Write out those local symbols we know are
9665 going into the output file. */
9666 isymend = isymbuf + locsymcount;
8b127cbc 9667 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9668 isym < isymend;
9669 isym++, pindex++, ppsection++)
9670 {
9671 asection *isec;
9672 const char *name;
9673 Elf_Internal_Sym osym;
6e0b88f1
AM
9674 long indx;
9675 int ret;
c152c796
AM
9676
9677 *pindex = -1;
9678
9679 if (elf_bad_symtab (input_bfd))
9680 {
9681 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9682 {
9683 *ppsection = NULL;
9684 continue;
9685 }
9686 }
9687
9688 if (isym->st_shndx == SHN_UNDEF)
9689 isec = bfd_und_section_ptr;
c152c796
AM
9690 else if (isym->st_shndx == SHN_ABS)
9691 isec = bfd_abs_section_ptr;
9692 else if (isym->st_shndx == SHN_COMMON)
9693 isec = bfd_com_section_ptr;
9694 else
9695 {
cb33740c
AM
9696 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9697 if (isec == NULL)
9698 {
9699 /* Don't attempt to output symbols with st_shnx in the
9700 reserved range other than SHN_ABS and SHN_COMMON. */
9701 *ppsection = NULL;
9702 continue;
9703 }
dbaa2011 9704 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9705 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9706 isym->st_value =
9707 _bfd_merged_section_offset (output_bfd, &isec,
9708 elf_section_data (isec)->sec_info,
9709 isym->st_value);
c152c796
AM
9710 }
9711
9712 *ppsection = isec;
9713
d983c8c5
AM
9714 /* Don't output the first, undefined, symbol. In fact, don't
9715 output any undefined local symbol. */
9716 if (isec == bfd_und_section_ptr)
c152c796
AM
9717 continue;
9718
9719 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9720 {
9721 /* We never output section symbols. Instead, we use the
9722 section symbol of the corresponding section in the output
9723 file. */
9724 continue;
9725 }
9726
9727 /* If we are stripping all symbols, we don't want to output this
9728 one. */
8b127cbc 9729 if (flinfo->info->strip == strip_all)
c152c796
AM
9730 continue;
9731
9732 /* If we are discarding all local symbols, we don't want to
9733 output this one. If we are generating a relocatable output
9734 file, then some of the local symbols may be required by
9735 relocs; we output them below as we discover that they are
9736 needed. */
8b127cbc 9737 if (flinfo->info->discard == discard_all)
c152c796
AM
9738 continue;
9739
9740 /* If this symbol is defined in a section which we are
f02571c5
AM
9741 discarding, we don't need to keep it. */
9742 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9743 && isym->st_shndx < SHN_LORESERVE
9744 && bfd_section_removed_from_list (output_bfd,
9745 isec->output_section))
e75a280b
L
9746 continue;
9747
c152c796
AM
9748 /* Get the name of the symbol. */
9749 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9750 isym->st_name);
9751 if (name == NULL)
9752 return FALSE;
9753
9754 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9755 if ((flinfo->info->strip == strip_some
9756 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9757 == NULL))
8b127cbc 9758 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9759 && (isec->flags & SEC_MERGE)
9760 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9761 || flinfo->info->discard == discard_l)
c152c796
AM
9762 && bfd_is_local_label_name (input_bfd, name)))
9763 continue;
9764
ffbc01cc
AM
9765 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9766 {
ce875075
AM
9767 if (input_bfd->lto_output)
9768 /* -flto puts a temp file name here. This means builds
9769 are not reproducible. Discard the symbol. */
9770 continue;
ffbc01cc
AM
9771 have_file_sym = TRUE;
9772 flinfo->filesym_count += 1;
9773 }
9774 if (!have_file_sym)
9775 {
9776 /* In the absence of debug info, bfd_find_nearest_line uses
9777 FILE symbols to determine the source file for local
9778 function symbols. Provide a FILE symbol here if input
9779 files lack such, so that their symbols won't be
9780 associated with a previous input file. It's not the
9781 source file, but the best we can do. */
9782 have_file_sym = TRUE;
9783 flinfo->filesym_count += 1;
9784 memset (&osym, 0, sizeof (osym));
9785 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9786 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9787 if (!elf_link_output_symstrtab (flinfo,
9788 (input_bfd->lto_output ? NULL
9789 : input_bfd->filename),
9790 &osym, bfd_abs_section_ptr,
9791 NULL))
ffbc01cc
AM
9792 return FALSE;
9793 }
9794
c152c796
AM
9795 osym = *isym;
9796
9797 /* Adjust the section index for the output file. */
9798 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9799 isec->output_section);
9800 if (osym.st_shndx == SHN_BAD)
9801 return FALSE;
9802
c152c796
AM
9803 /* ELF symbols in relocatable files are section relative, but
9804 in executable files they are virtual addresses. Note that
9805 this code assumes that all ELF sections have an associated
9806 BFD section with a reasonable value for output_offset; below
9807 we assume that they also have a reasonable value for
9808 output_section. Any special sections must be set up to meet
9809 these requirements. */
9810 osym.st_value += isec->output_offset;
0e1862bb 9811 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9812 {
9813 osym.st_value += isec->output_section->vma;
9814 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9815 {
9816 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9817 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9818 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9819 }
9820 }
9821
6e0b88f1 9822 indx = bfd_get_symcount (output_bfd);
ef10c3ac 9823 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 9824 if (ret == 0)
c152c796 9825 return FALSE;
6e0b88f1
AM
9826 else if (ret == 1)
9827 *pindex = indx;
c152c796
AM
9828 }
9829
310fd250
L
9830 if (bed->s->arch_size == 32)
9831 {
9832 r_type_mask = 0xff;
9833 r_sym_shift = 8;
9834 address_size = 4;
9835 }
9836 else
9837 {
9838 r_type_mask = 0xffffffff;
9839 r_sym_shift = 32;
9840 address_size = 8;
9841 }
9842
c152c796
AM
9843 /* Relocate the contents of each section. */
9844 sym_hashes = elf_sym_hashes (input_bfd);
9845 for (o = input_bfd->sections; o != NULL; o = o->next)
9846 {
9847 bfd_byte *contents;
9848
9849 if (! o->linker_mark)
9850 {
9851 /* This section was omitted from the link. */
9852 continue;
9853 }
9854
0e1862bb 9855 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
9856 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9857 {
9858 /* Deal with the group signature symbol. */
9859 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9860 unsigned long symndx = sec_data->this_hdr.sh_info;
9861 asection *osec = o->output_section;
9862
9863 if (symndx >= locsymcount
9864 || (elf_bad_symtab (input_bfd)
8b127cbc 9865 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9866 {
9867 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9868 while (h->root.type == bfd_link_hash_indirect
9869 || h->root.type == bfd_link_hash_warning)
9870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9871 /* Arrange for symbol to be output. */
9872 h->indx = -2;
9873 elf_section_data (osec)->this_hdr.sh_info = -2;
9874 }
9875 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9876 {
9877 /* We'll use the output section target_index. */
8b127cbc 9878 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9879 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9880 }
9881 else
9882 {
8b127cbc 9883 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9884 {
9885 /* Otherwise output the local symbol now. */
9886 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9887 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 9888 const char *name;
6e0b88f1
AM
9889 long indx;
9890 int ret;
bcacc0f5
AM
9891
9892 name = bfd_elf_string_from_elf_section (input_bfd,
9893 symtab_hdr->sh_link,
9894 sym.st_name);
9895 if (name == NULL)
9896 return FALSE;
9897
9898 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9899 sec);
9900 if (sym.st_shndx == SHN_BAD)
9901 return FALSE;
9902
9903 sym.st_value += o->output_offset;
9904
6e0b88f1 9905 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
9906 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9907 NULL);
6e0b88f1 9908 if (ret == 0)
bcacc0f5 9909 return FALSE;
6e0b88f1 9910 else if (ret == 1)
8b127cbc 9911 flinfo->indices[symndx] = indx;
6e0b88f1
AM
9912 else
9913 abort ();
bcacc0f5
AM
9914 }
9915 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 9916 = flinfo->indices[symndx];
bcacc0f5
AM
9917 }
9918 }
9919
c152c796 9920 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9921 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9922 continue;
9923
9924 if ((o->flags & SEC_LINKER_CREATED) != 0)
9925 {
9926 /* Section was created by _bfd_elf_link_create_dynamic_sections
9927 or somesuch. */
9928 continue;
9929 }
9930
9931 /* Get the contents of the section. They have been cached by a
9932 relaxation routine. Note that o is a section in an input
9933 file, so the contents field will not have been set by any of
9934 the routines which work on output files. */
9935 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
9936 {
9937 contents = elf_section_data (o)->this_hdr.contents;
9938 if (bed->caches_rawsize
9939 && o->rawsize != 0
9940 && o->rawsize < o->size)
9941 {
9942 memcpy (flinfo->contents, contents, o->rawsize);
9943 contents = flinfo->contents;
9944 }
9945 }
c152c796
AM
9946 else
9947 {
8b127cbc 9948 contents = flinfo->contents;
4a114e3e 9949 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9950 return FALSE;
9951 }
9952
9953 if ((o->flags & SEC_RELOC) != 0)
9954 {
9955 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9956 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9957 int action_discarded;
ece5ef60 9958 int ret;
c152c796
AM
9959
9960 /* Get the swapped relocs. */
9961 internal_relocs
8b127cbc
AM
9962 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9963 flinfo->internal_relocs, FALSE);
c152c796
AM
9964 if (internal_relocs == NULL
9965 && o->reloc_count > 0)
9966 return FALSE;
9967
310fd250
L
9968 /* We need to reverse-copy input .ctors/.dtors sections if
9969 they are placed in .init_array/.finit_array for output. */
9970 if (o->size > address_size
9971 && ((strncmp (o->name, ".ctors", 6) == 0
9972 && strcmp (o->output_section->name,
9973 ".init_array") == 0)
9974 || (strncmp (o->name, ".dtors", 6) == 0
9975 && strcmp (o->output_section->name,
9976 ".fini_array") == 0))
9977 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9978 {
310fd250
L
9979 if (o->size != o->reloc_count * address_size)
9980 {
9981 (*_bfd_error_handler)
9982 (_("error: %B: size of section %A is not "
9983 "multiple of address size"),
9984 input_bfd, o);
9985 bfd_set_error (bfd_error_on_input);
9986 return FALSE;
9987 }
9988 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9989 }
9990
0f02bbd9 9991 action_discarded = -1;
c152c796 9992 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9993 action_discarded = (*bed->action_discarded) (o);
9994
9995 /* Run through the relocs evaluating complex reloc symbols and
9996 looking for relocs against symbols from discarded sections
9997 or section symbols from removed link-once sections.
9998 Complain about relocs against discarded sections. Zero
9999 relocs against removed link-once sections. */
10000
10001 rel = internal_relocs;
10002 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10003 for ( ; rel < relend; rel++)
c152c796 10004 {
0f02bbd9
AM
10005 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10006 unsigned int s_type;
10007 asection **ps, *sec;
10008 struct elf_link_hash_entry *h = NULL;
10009 const char *sym_name;
c152c796 10010
0f02bbd9
AM
10011 if (r_symndx == STN_UNDEF)
10012 continue;
c152c796 10013
0f02bbd9
AM
10014 if (r_symndx >= locsymcount
10015 || (elf_bad_symtab (input_bfd)
8b127cbc 10016 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10017 {
10018 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10019
0f02bbd9
AM
10020 /* Badly formatted input files can contain relocs that
10021 reference non-existant symbols. Check here so that
10022 we do not seg fault. */
10023 if (h == NULL)
c152c796 10024 {
0f02bbd9 10025 char buffer [32];
dce669a1 10026
0f02bbd9
AM
10027 sprintf_vma (buffer, rel->r_info);
10028 (*_bfd_error_handler)
10029 (_("error: %B contains a reloc (0x%s) for section %A "
10030 "that references a non-existent global symbol"),
10031 input_bfd, o, buffer);
10032 bfd_set_error (bfd_error_bad_value);
10033 return FALSE;
10034 }
3b36f7e6 10035
0f02bbd9
AM
10036 while (h->root.type == bfd_link_hash_indirect
10037 || h->root.type == bfd_link_hash_warning)
10038 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10039
0f02bbd9 10040 s_type = h->type;
cdd3575c 10041
9e2dec47 10042 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10043 mark the symbol as undefined. Note that the
10044 linker may attach linker created dynamic sections
10045 to the plugin bfd. Symbols defined in linker
10046 created sections are not plugin symbols. */
9e2dec47
L
10047 if (h->root.non_ir_ref
10048 && (h->root.type == bfd_link_hash_defined
10049 || h->root.type == bfd_link_hash_defweak)
10050 && (h->root.u.def.section->flags
10051 & SEC_LINKER_CREATED) == 0
10052 && h->root.u.def.section->owner != NULL
10053 && (h->root.u.def.section->owner->flags
10054 & BFD_PLUGIN) != 0)
10055 {
10056 h->root.type = bfd_link_hash_undefined;
10057 h->root.u.undef.abfd = h->root.u.def.section->owner;
10058 }
10059
0f02bbd9
AM
10060 ps = NULL;
10061 if (h->root.type == bfd_link_hash_defined
10062 || h->root.type == bfd_link_hash_defweak)
10063 ps = &h->root.u.def.section;
10064
10065 sym_name = h->root.root.string;
10066 }
10067 else
10068 {
10069 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10070
10071 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10072 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10073 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10074 sym, *ps);
10075 }
c152c796 10076
c301e700 10077 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10078 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10079 {
10080 bfd_vma val;
10081 bfd_vma dot = (rel->r_offset
10082 + o->output_offset + o->output_section->vma);
10083#ifdef DEBUG
10084 printf ("Encountered a complex symbol!");
10085 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10086 input_bfd->filename, o->name,
10087 (long) (rel - internal_relocs));
0f02bbd9
AM
10088 printf (" symbol: idx %8.8lx, name %s\n",
10089 r_symndx, sym_name);
10090 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10091 (unsigned long) rel->r_info,
10092 (unsigned long) rel->r_offset);
10093#endif
8b127cbc 10094 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10095 isymbuf, locsymcount, s_type == STT_SRELC))
10096 return FALSE;
10097
10098 /* Symbol evaluated OK. Update to absolute value. */
10099 set_symbol_value (input_bfd, isymbuf, locsymcount,
10100 r_symndx, val);
10101 continue;
10102 }
10103
10104 if (action_discarded != -1 && ps != NULL)
10105 {
cdd3575c
AM
10106 /* Complain if the definition comes from a
10107 discarded section. */
dbaa2011 10108 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10109 {
cf35638d 10110 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10111 if (action_discarded & COMPLAIN)
8b127cbc 10112 (*flinfo->info->callbacks->einfo)
e1fffbe6 10113 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10114 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10115 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10116
87e5235d 10117 /* Try to do the best we can to support buggy old
e0ae6d6f 10118 versions of gcc. Pretend that the symbol is
87e5235d
AM
10119 really defined in the kept linkonce section.
10120 FIXME: This is quite broken. Modifying the
10121 symbol here means we will be changing all later
e0ae6d6f 10122 uses of the symbol, not just in this section. */
0f02bbd9 10123 if (action_discarded & PRETEND)
87e5235d 10124 {
01b3c8ab
L
10125 asection *kept;
10126
c0f00686 10127 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10128 flinfo->info);
01b3c8ab 10129 if (kept != NULL)
87e5235d
AM
10130 {
10131 *ps = kept;
10132 continue;
10133 }
10134 }
c152c796
AM
10135 }
10136 }
10137 }
10138
10139 /* Relocate the section by invoking a back end routine.
10140
10141 The back end routine is responsible for adjusting the
10142 section contents as necessary, and (if using Rela relocs
10143 and generating a relocatable output file) adjusting the
10144 reloc addend as necessary.
10145
10146 The back end routine does not have to worry about setting
10147 the reloc address or the reloc symbol index.
10148
10149 The back end routine is given a pointer to the swapped in
10150 internal symbols, and can access the hash table entries
10151 for the external symbols via elf_sym_hashes (input_bfd).
10152
10153 When generating relocatable output, the back end routine
10154 must handle STB_LOCAL/STT_SECTION symbols specially. The
10155 output symbol is going to be a section symbol
10156 corresponding to the output section, which will require
10157 the addend to be adjusted. */
10158
8b127cbc 10159 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10160 input_bfd, o, contents,
10161 internal_relocs,
10162 isymbuf,
8b127cbc 10163 flinfo->sections);
ece5ef60 10164 if (!ret)
c152c796
AM
10165 return FALSE;
10166
ece5ef60 10167 if (ret == 2
0e1862bb 10168 || bfd_link_relocatable (flinfo->info)
8b127cbc 10169 || flinfo->info->emitrelocations)
c152c796
AM
10170 {
10171 Elf_Internal_Rela *irela;
d4730f92 10172 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10173 bfd_vma last_offset;
10174 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10175 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10176 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10177 unsigned int next_erel;
c152c796 10178 bfd_boolean rela_normal;
d4730f92 10179 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10180
d4730f92
BS
10181 esdi = elf_section_data (o);
10182 esdo = elf_section_data (o->output_section);
10183 rela_normal = FALSE;
c152c796
AM
10184
10185 /* Adjust the reloc addresses and symbol indices. */
10186
10187 irela = internal_relocs;
10188 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10189 rel_hash = esdo->rel.hashes + esdo->rel.count;
10190 /* We start processing the REL relocs, if any. When we reach
10191 IRELAMID in the loop, we switch to the RELA relocs. */
10192 irelamid = irela;
10193 if (esdi->rel.hdr != NULL)
10194 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10195 * bed->s->int_rels_per_ext_rel);
eac338cf 10196 rel_hash_list = rel_hash;
d4730f92 10197 rela_hash_list = NULL;
c152c796 10198 last_offset = o->output_offset;
0e1862bb 10199 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10200 last_offset += o->output_section->vma;
10201 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10202 {
10203 unsigned long r_symndx;
10204 asection *sec;
10205 Elf_Internal_Sym sym;
10206
10207 if (next_erel == bed->s->int_rels_per_ext_rel)
10208 {
10209 rel_hash++;
10210 next_erel = 0;
10211 }
10212
d4730f92
BS
10213 if (irela == irelamid)
10214 {
10215 rel_hash = esdo->rela.hashes + esdo->rela.count;
10216 rela_hash_list = rel_hash;
10217 rela_normal = bed->rela_normal;
10218 }
10219
c152c796 10220 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10221 flinfo->info, o,
c152c796
AM
10222 irela->r_offset);
10223 if (irela->r_offset >= (bfd_vma) -2)
10224 {
10225 /* This is a reloc for a deleted entry or somesuch.
10226 Turn it into an R_*_NONE reloc, at the same
10227 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10228 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10229 being ordered. */
10230 irela->r_offset = last_offset;
10231 irela->r_info = 0;
10232 irela->r_addend = 0;
10233 continue;
10234 }
10235
10236 irela->r_offset += o->output_offset;
10237
10238 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10239 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10240 irela->r_offset += o->output_section->vma;
10241
10242 last_offset = irela->r_offset;
10243
10244 r_symndx = irela->r_info >> r_sym_shift;
10245 if (r_symndx == STN_UNDEF)
10246 continue;
10247
10248 if (r_symndx >= locsymcount
10249 || (elf_bad_symtab (input_bfd)
8b127cbc 10250 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10251 {
10252 struct elf_link_hash_entry *rh;
10253 unsigned long indx;
10254
10255 /* This is a reloc against a global symbol. We
10256 have not yet output all the local symbols, so
10257 we do not know the symbol index of any global
10258 symbol. We set the rel_hash entry for this
10259 reloc to point to the global hash table entry
10260 for this symbol. The symbol index is then
ee75fd95 10261 set at the end of bfd_elf_final_link. */
c152c796
AM
10262 indx = r_symndx - extsymoff;
10263 rh = elf_sym_hashes (input_bfd)[indx];
10264 while (rh->root.type == bfd_link_hash_indirect
10265 || rh->root.type == bfd_link_hash_warning)
10266 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10267
10268 /* Setting the index to -2 tells
10269 elf_link_output_extsym that this symbol is
10270 used by a reloc. */
10271 BFD_ASSERT (rh->indx < 0);
10272 rh->indx = -2;
10273
10274 *rel_hash = rh;
10275
10276 continue;
10277 }
10278
10279 /* This is a reloc against a local symbol. */
10280
10281 *rel_hash = NULL;
10282 sym = isymbuf[r_symndx];
8b127cbc 10283 sec = flinfo->sections[r_symndx];
c152c796
AM
10284 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10285 {
10286 /* I suppose the backend ought to fill in the
10287 section of any STT_SECTION symbol against a
6a8d1586 10288 processor specific section. */
cf35638d 10289 r_symndx = STN_UNDEF;
6a8d1586
AM
10290 if (bfd_is_abs_section (sec))
10291 ;
c152c796
AM
10292 else if (sec == NULL || sec->owner == NULL)
10293 {
10294 bfd_set_error (bfd_error_bad_value);
10295 return FALSE;
10296 }
10297 else
10298 {
6a8d1586
AM
10299 asection *osec = sec->output_section;
10300
10301 /* If we have discarded a section, the output
10302 section will be the absolute section. In
ab96bf03
AM
10303 case of discarded SEC_MERGE sections, use
10304 the kept section. relocate_section should
10305 have already handled discarded linkonce
10306 sections. */
6a8d1586
AM
10307 if (bfd_is_abs_section (osec)
10308 && sec->kept_section != NULL
10309 && sec->kept_section->output_section != NULL)
10310 {
10311 osec = sec->kept_section->output_section;
10312 irela->r_addend -= osec->vma;
10313 }
10314
10315 if (!bfd_is_abs_section (osec))
10316 {
10317 r_symndx = osec->target_index;
cf35638d 10318 if (r_symndx == STN_UNDEF)
74541ad4 10319 {
051d833a
AM
10320 irela->r_addend += osec->vma;
10321 osec = _bfd_nearby_section (output_bfd, osec,
10322 osec->vma);
10323 irela->r_addend -= osec->vma;
10324 r_symndx = osec->target_index;
74541ad4 10325 }
6a8d1586 10326 }
c152c796
AM
10327 }
10328
10329 /* Adjust the addend according to where the
10330 section winds up in the output section. */
10331 if (rela_normal)
10332 irela->r_addend += sec->output_offset;
10333 }
10334 else
10335 {
8b127cbc 10336 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10337 {
10338 unsigned long shlink;
10339 const char *name;
10340 asection *osec;
6e0b88f1 10341 long indx;
c152c796 10342
8b127cbc 10343 if (flinfo->info->strip == strip_all)
c152c796
AM
10344 {
10345 /* You can't do ld -r -s. */
10346 bfd_set_error (bfd_error_invalid_operation);
10347 return FALSE;
10348 }
10349
10350 /* This symbol was skipped earlier, but
10351 since it is needed by a reloc, we
10352 must output it now. */
10353 shlink = symtab_hdr->sh_link;
10354 name = (bfd_elf_string_from_elf_section
10355 (input_bfd, shlink, sym.st_name));
10356 if (name == NULL)
10357 return FALSE;
10358
10359 osec = sec->output_section;
10360 sym.st_shndx =
10361 _bfd_elf_section_from_bfd_section (output_bfd,
10362 osec);
10363 if (sym.st_shndx == SHN_BAD)
10364 return FALSE;
10365
10366 sym.st_value += sec->output_offset;
0e1862bb 10367 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10368 {
10369 sym.st_value += osec->vma;
10370 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10371 {
10372 /* STT_TLS symbols are relative to PT_TLS
10373 segment base. */
8b127cbc 10374 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10375 ->tls_sec != NULL);
8b127cbc 10376 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10377 ->tls_sec->vma);
10378 }
10379 }
10380
6e0b88f1 10381 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10382 ret = elf_link_output_symstrtab (flinfo, name,
10383 &sym, sec,
10384 NULL);
6e0b88f1 10385 if (ret == 0)
c152c796 10386 return FALSE;
6e0b88f1 10387 else if (ret == 1)
8b127cbc 10388 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10389 else
10390 abort ();
c152c796
AM
10391 }
10392
8b127cbc 10393 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10394 }
10395
10396 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10397 | (irela->r_info & r_type_mask));
10398 }
10399
10400 /* Swap out the relocs. */
d4730f92
BS
10401 input_rel_hdr = esdi->rel.hdr;
10402 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10403 {
d4730f92
BS
10404 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10405 input_rel_hdr,
10406 internal_relocs,
10407 rel_hash_list))
10408 return FALSE;
c152c796
AM
10409 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10410 * bed->s->int_rels_per_ext_rel);
eac338cf 10411 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10412 }
10413
10414 input_rela_hdr = esdi->rela.hdr;
10415 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10416 {
eac338cf 10417 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10418 input_rela_hdr,
eac338cf 10419 internal_relocs,
d4730f92 10420 rela_hash_list))
c152c796
AM
10421 return FALSE;
10422 }
10423 }
10424 }
10425
10426 /* Write out the modified section contents. */
10427 if (bed->elf_backend_write_section
8b127cbc 10428 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10429 contents))
c152c796
AM
10430 {
10431 /* Section written out. */
10432 }
10433 else switch (o->sec_info_type)
10434 {
dbaa2011 10435 case SEC_INFO_TYPE_STABS:
c152c796
AM
10436 if (! (_bfd_write_section_stabs
10437 (output_bfd,
8b127cbc 10438 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10439 o, &elf_section_data (o)->sec_info, contents)))
10440 return FALSE;
10441 break;
dbaa2011 10442 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10443 if (! _bfd_write_merged_section (output_bfd, o,
10444 elf_section_data (o)->sec_info))
10445 return FALSE;
10446 break;
dbaa2011 10447 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10448 {
8b127cbc 10449 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10450 o, contents))
10451 return FALSE;
10452 }
10453 break;
2f0c68f2
CM
10454 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10455 {
10456 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10457 flinfo->info,
10458 o, contents))
10459 return FALSE;
10460 }
10461 break;
c152c796
AM
10462 default:
10463 {
5dabe785 10464 /* FIXME: octets_per_byte. */
310fd250
L
10465 if (! (o->flags & SEC_EXCLUDE))
10466 {
10467 file_ptr offset = (file_ptr) o->output_offset;
10468 bfd_size_type todo = o->size;
10469 if ((o->flags & SEC_ELF_REVERSE_COPY))
10470 {
10471 /* Reverse-copy input section to output. */
10472 do
10473 {
10474 todo -= address_size;
10475 if (! bfd_set_section_contents (output_bfd,
10476 o->output_section,
10477 contents + todo,
10478 offset,
10479 address_size))
10480 return FALSE;
10481 if (todo == 0)
10482 break;
10483 offset += address_size;
10484 }
10485 while (1);
10486 }
10487 else if (! bfd_set_section_contents (output_bfd,
10488 o->output_section,
10489 contents,
10490 offset, todo))
10491 return FALSE;
10492 }
c152c796
AM
10493 }
10494 break;
10495 }
10496 }
10497
10498 return TRUE;
10499}
10500
10501/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10502 requested by the linker, and does not come from any input file. This
c152c796
AM
10503 is used to build constructor and destructor tables when linking
10504 with -Ur. */
10505
10506static bfd_boolean
10507elf_reloc_link_order (bfd *output_bfd,
10508 struct bfd_link_info *info,
10509 asection *output_section,
10510 struct bfd_link_order *link_order)
10511{
10512 reloc_howto_type *howto;
10513 long indx;
10514 bfd_vma offset;
10515 bfd_vma addend;
d4730f92 10516 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10517 struct elf_link_hash_entry **rel_hash_ptr;
10518 Elf_Internal_Shdr *rel_hdr;
10519 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10520 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10521 bfd_byte *erel;
10522 unsigned int i;
d4730f92 10523 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10524
10525 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10526 if (howto == NULL)
10527 {
10528 bfd_set_error (bfd_error_bad_value);
10529 return FALSE;
10530 }
10531
10532 addend = link_order->u.reloc.p->addend;
10533
d4730f92
BS
10534 if (esdo->rel.hdr)
10535 reldata = &esdo->rel;
10536 else if (esdo->rela.hdr)
10537 reldata = &esdo->rela;
10538 else
10539 {
10540 reldata = NULL;
10541 BFD_ASSERT (0);
10542 }
10543
c152c796 10544 /* Figure out the symbol index. */
d4730f92 10545 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10546 if (link_order->type == bfd_section_reloc_link_order)
10547 {
10548 indx = link_order->u.reloc.p->u.section->target_index;
10549 BFD_ASSERT (indx != 0);
10550 *rel_hash_ptr = NULL;
10551 }
10552 else
10553 {
10554 struct elf_link_hash_entry *h;
10555
10556 /* Treat a reloc against a defined symbol as though it were
10557 actually against the section. */
10558 h = ((struct elf_link_hash_entry *)
10559 bfd_wrapped_link_hash_lookup (output_bfd, info,
10560 link_order->u.reloc.p->u.name,
10561 FALSE, FALSE, TRUE));
10562 if (h != NULL
10563 && (h->root.type == bfd_link_hash_defined
10564 || h->root.type == bfd_link_hash_defweak))
10565 {
10566 asection *section;
10567
10568 section = h->root.u.def.section;
10569 indx = section->output_section->target_index;
10570 *rel_hash_ptr = NULL;
10571 /* It seems that we ought to add the symbol value to the
10572 addend here, but in practice it has already been added
10573 because it was passed to constructor_callback. */
10574 addend += section->output_section->vma + section->output_offset;
10575 }
10576 else if (h != NULL)
10577 {
10578 /* Setting the index to -2 tells elf_link_output_extsym that
10579 this symbol is used by a reloc. */
10580 h->indx = -2;
10581 *rel_hash_ptr = h;
10582 indx = 0;
10583 }
10584 else
10585 {
10586 if (! ((*info->callbacks->unattached_reloc)
10587 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10588 return FALSE;
10589 indx = 0;
10590 }
10591 }
10592
10593 /* If this is an inplace reloc, we must write the addend into the
10594 object file. */
10595 if (howto->partial_inplace && addend != 0)
10596 {
10597 bfd_size_type size;
10598 bfd_reloc_status_type rstat;
10599 bfd_byte *buf;
10600 bfd_boolean ok;
10601 const char *sym_name;
10602
a50b1753
NC
10603 size = (bfd_size_type) bfd_get_reloc_size (howto);
10604 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10605 if (buf == NULL && size != 0)
c152c796
AM
10606 return FALSE;
10607 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10608 switch (rstat)
10609 {
10610 case bfd_reloc_ok:
10611 break;
10612
10613 default:
10614 case bfd_reloc_outofrange:
10615 abort ();
10616
10617 case bfd_reloc_overflow:
10618 if (link_order->type == bfd_section_reloc_link_order)
10619 sym_name = bfd_section_name (output_bfd,
10620 link_order->u.reloc.p->u.section);
10621 else
10622 sym_name = link_order->u.reloc.p->u.name;
10623 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10624 (info, NULL, sym_name, howto->name, addend, NULL,
10625 NULL, (bfd_vma) 0)))
c152c796
AM
10626 {
10627 free (buf);
10628 return FALSE;
10629 }
10630 break;
10631 }
10632 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10633 link_order->offset, size);
10634 free (buf);
10635 if (! ok)
10636 return FALSE;
10637 }
10638
10639 /* The address of a reloc is relative to the section in a
10640 relocatable file, and is a virtual address in an executable
10641 file. */
10642 offset = link_order->offset;
0e1862bb 10643 if (! bfd_link_relocatable (info))
c152c796
AM
10644 offset += output_section->vma;
10645
10646 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10647 {
10648 irel[i].r_offset = offset;
10649 irel[i].r_info = 0;
10650 irel[i].r_addend = 0;
10651 }
10652 if (bed->s->arch_size == 32)
10653 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10654 else
10655 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10656
d4730f92 10657 rel_hdr = reldata->hdr;
c152c796
AM
10658 erel = rel_hdr->contents;
10659 if (rel_hdr->sh_type == SHT_REL)
10660 {
d4730f92 10661 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10662 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10663 }
10664 else
10665 {
10666 irel[0].r_addend = addend;
d4730f92 10667 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10668 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10669 }
10670
d4730f92 10671 ++reldata->count;
c152c796
AM
10672
10673 return TRUE;
10674}
10675
0b52efa6
PB
10676
10677/* Get the output vma of the section pointed to by the sh_link field. */
10678
10679static bfd_vma
10680elf_get_linked_section_vma (struct bfd_link_order *p)
10681{
10682 Elf_Internal_Shdr **elf_shdrp;
10683 asection *s;
10684 int elfsec;
10685
10686 s = p->u.indirect.section;
10687 elf_shdrp = elf_elfsections (s->owner);
10688 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10689 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10690 /* PR 290:
10691 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10692 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10693 sh_info fields. Hence we could get the situation
10694 where elfsec is 0. */
10695 if (elfsec == 0)
10696 {
10697 const struct elf_backend_data *bed
10698 = get_elf_backend_data (s->owner);
10699 if (bed->link_order_error_handler)
d003868e
AM
10700 bed->link_order_error_handler
10701 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10702 return 0;
10703 }
10704 else
10705 {
10706 s = elf_shdrp[elfsec]->bfd_section;
10707 return s->output_section->vma + s->output_offset;
10708 }
0b52efa6
PB
10709}
10710
10711
10712/* Compare two sections based on the locations of the sections they are
10713 linked to. Used by elf_fixup_link_order. */
10714
10715static int
10716compare_link_order (const void * a, const void * b)
10717{
10718 bfd_vma apos;
10719 bfd_vma bpos;
10720
10721 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10722 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10723 if (apos < bpos)
10724 return -1;
10725 return apos > bpos;
10726}
10727
10728
10729/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10730 order as their linked sections. Returns false if this could not be done
10731 because an output section includes both ordered and unordered
10732 sections. Ideally we'd do this in the linker proper. */
10733
10734static bfd_boolean
10735elf_fixup_link_order (bfd *abfd, asection *o)
10736{
10737 int seen_linkorder;
10738 int seen_other;
10739 int n;
10740 struct bfd_link_order *p;
10741 bfd *sub;
10742 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10743 unsigned elfsec;
0b52efa6 10744 struct bfd_link_order **sections;
d33cdfe3 10745 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10746 bfd_vma offset;
3b36f7e6 10747
d33cdfe3
L
10748 other_sec = NULL;
10749 linkorder_sec = NULL;
0b52efa6
PB
10750 seen_other = 0;
10751 seen_linkorder = 0;
8423293d 10752 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10753 {
d33cdfe3 10754 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10755 {
10756 s = p->u.indirect.section;
d33cdfe3
L
10757 sub = s->owner;
10758 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10759 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10760 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10761 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10762 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10763 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10764 {
10765 seen_linkorder++;
10766 linkorder_sec = s;
10767 }
0b52efa6 10768 else
d33cdfe3
L
10769 {
10770 seen_other++;
10771 other_sec = s;
10772 }
0b52efa6
PB
10773 }
10774 else
10775 seen_other++;
d33cdfe3
L
10776
10777 if (seen_other && seen_linkorder)
10778 {
10779 if (other_sec && linkorder_sec)
10780 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10781 o, linkorder_sec,
10782 linkorder_sec->owner, other_sec,
10783 other_sec->owner);
10784 else
10785 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10786 o);
10787 bfd_set_error (bfd_error_bad_value);
10788 return FALSE;
10789 }
0b52efa6
PB
10790 }
10791
10792 if (!seen_linkorder)
10793 return TRUE;
10794
0b52efa6 10795 sections = (struct bfd_link_order **)
14b1c01e
AM
10796 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10797 if (sections == NULL)
10798 return FALSE;
0b52efa6 10799 seen_linkorder = 0;
3b36f7e6 10800
8423293d 10801 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10802 {
10803 sections[seen_linkorder++] = p;
10804 }
10805 /* Sort the input sections in the order of their linked section. */
10806 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10807 compare_link_order);
10808
10809 /* Change the offsets of the sections. */
10810 offset = 0;
10811 for (n = 0; n < seen_linkorder; n++)
10812 {
10813 s = sections[n]->u.indirect.section;
461686a3 10814 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10815 s->output_offset = offset;
10816 sections[n]->offset = offset;
5dabe785 10817 /* FIXME: octets_per_byte. */
0b52efa6
PB
10818 offset += sections[n]->size;
10819 }
10820
4dd07732 10821 free (sections);
0b52efa6
PB
10822 return TRUE;
10823}
10824
9f7c3e5e
AM
10825static void
10826elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10827{
10828 asection *o;
10829
10830 if (flinfo->symstrtab != NULL)
ef10c3ac 10831 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
10832 if (flinfo->contents != NULL)
10833 free (flinfo->contents);
10834 if (flinfo->external_relocs != NULL)
10835 free (flinfo->external_relocs);
10836 if (flinfo->internal_relocs != NULL)
10837 free (flinfo->internal_relocs);
10838 if (flinfo->external_syms != NULL)
10839 free (flinfo->external_syms);
10840 if (flinfo->locsym_shndx != NULL)
10841 free (flinfo->locsym_shndx);
10842 if (flinfo->internal_syms != NULL)
10843 free (flinfo->internal_syms);
10844 if (flinfo->indices != NULL)
10845 free (flinfo->indices);
10846 if (flinfo->sections != NULL)
10847 free (flinfo->sections);
9f7c3e5e
AM
10848 if (flinfo->symshndxbuf != NULL)
10849 free (flinfo->symshndxbuf);
10850 for (o = obfd->sections; o != NULL; o = o->next)
10851 {
10852 struct bfd_elf_section_data *esdo = elf_section_data (o);
10853 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10854 free (esdo->rel.hashes);
10855 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10856 free (esdo->rela.hashes);
10857 }
10858}
0b52efa6 10859
c152c796
AM
10860/* Do the final step of an ELF link. */
10861
10862bfd_boolean
10863bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10864{
10865 bfd_boolean dynamic;
10866 bfd_boolean emit_relocs;
10867 bfd *dynobj;
8b127cbc 10868 struct elf_final_link_info flinfo;
91d6fa6a
NC
10869 asection *o;
10870 struct bfd_link_order *p;
10871 bfd *sub;
c152c796
AM
10872 bfd_size_type max_contents_size;
10873 bfd_size_type max_external_reloc_size;
10874 bfd_size_type max_internal_reloc_count;
10875 bfd_size_type max_sym_count;
10876 bfd_size_type max_sym_shndx_count;
c152c796
AM
10877 Elf_Internal_Sym elfsym;
10878 unsigned int i;
10879 Elf_Internal_Shdr *symtab_hdr;
10880 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
10881 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10882 struct elf_outext_info eoinfo;
10883 bfd_boolean merged;
10884 size_t relativecount = 0;
10885 asection *reldyn = 0;
10886 bfd_size_type amt;
104d59d1
JM
10887 asection *attr_section = NULL;
10888 bfd_vma attr_size = 0;
10889 const char *std_attrs_section;
c152c796
AM
10890
10891 if (! is_elf_hash_table (info->hash))
10892 return FALSE;
10893
0e1862bb 10894 if (bfd_link_pic (info))
c152c796
AM
10895 abfd->flags |= DYNAMIC;
10896
10897 dynamic = elf_hash_table (info)->dynamic_sections_created;
10898 dynobj = elf_hash_table (info)->dynobj;
10899
0e1862bb 10900 emit_relocs = (bfd_link_relocatable (info)
a4676736 10901 || info->emitrelocations);
c152c796 10902
8b127cbc
AM
10903 flinfo.info = info;
10904 flinfo.output_bfd = abfd;
ef10c3ac 10905 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 10906 if (flinfo.symstrtab == NULL)
c152c796
AM
10907 return FALSE;
10908
10909 if (! dynamic)
10910 {
8b127cbc
AM
10911 flinfo.hash_sec = NULL;
10912 flinfo.symver_sec = NULL;
c152c796
AM
10913 }
10914 else
10915 {
3d4d4302 10916 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 10917 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 10918 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
10919 /* Note that it is OK if symver_sec is NULL. */
10920 }
10921
8b127cbc
AM
10922 flinfo.contents = NULL;
10923 flinfo.external_relocs = NULL;
10924 flinfo.internal_relocs = NULL;
10925 flinfo.external_syms = NULL;
10926 flinfo.locsym_shndx = NULL;
10927 flinfo.internal_syms = NULL;
10928 flinfo.indices = NULL;
10929 flinfo.sections = NULL;
8b127cbc 10930 flinfo.symshndxbuf = NULL;
ffbc01cc 10931 flinfo.filesym_count = 0;
c152c796 10932
104d59d1
JM
10933 /* The object attributes have been merged. Remove the input
10934 sections from the link, and set the contents of the output
10935 secton. */
10936 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10937 for (o = abfd->sections; o != NULL; o = o->next)
10938 {
10939 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10940 || strcmp (o->name, ".gnu.attributes") == 0)
10941 {
10942 for (p = o->map_head.link_order; p != NULL; p = p->next)
10943 {
10944 asection *input_section;
10945
10946 if (p->type != bfd_indirect_link_order)
10947 continue;
10948 input_section = p->u.indirect.section;
10949 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10950 elf_link_input_bfd ignores this section. */
10951 input_section->flags &= ~SEC_HAS_CONTENTS;
10952 }
a0c8462f 10953
104d59d1
JM
10954 attr_size = bfd_elf_obj_attr_size (abfd);
10955 if (attr_size)
10956 {
10957 bfd_set_section_size (abfd, o, attr_size);
10958 attr_section = o;
10959 /* Skip this section later on. */
10960 o->map_head.link_order = NULL;
10961 }
10962 else
10963 o->flags |= SEC_EXCLUDE;
10964 }
10965 }
10966
c152c796
AM
10967 /* Count up the number of relocations we will output for each output
10968 section, so that we know the sizes of the reloc sections. We
10969 also figure out some maximum sizes. */
10970 max_contents_size = 0;
10971 max_external_reloc_size = 0;
10972 max_internal_reloc_count = 0;
10973 max_sym_count = 0;
10974 max_sym_shndx_count = 0;
10975 merged = FALSE;
10976 for (o = abfd->sections; o != NULL; o = o->next)
10977 {
10978 struct bfd_elf_section_data *esdo = elf_section_data (o);
10979 o->reloc_count = 0;
10980
8423293d 10981 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10982 {
10983 unsigned int reloc_count = 0;
10984 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10985
10986 if (p->type == bfd_section_reloc_link_order
10987 || p->type == bfd_symbol_reloc_link_order)
10988 reloc_count = 1;
10989 else if (p->type == bfd_indirect_link_order)
10990 {
10991 asection *sec;
10992
10993 sec = p->u.indirect.section;
10994 esdi = elf_section_data (sec);
10995
10996 /* Mark all sections which are to be included in the
10997 link. This will normally be every section. We need
10998 to do this so that we can identify any sections which
10999 the linker has decided to not include. */
11000 sec->linker_mark = TRUE;
11001
11002 if (sec->flags & SEC_MERGE)
11003 merged = TRUE;
11004
aed64b35
L
11005 if (esdo->this_hdr.sh_type == SHT_REL
11006 || esdo->this_hdr.sh_type == SHT_RELA)
11007 /* Some backends use reloc_count in relocation sections
11008 to count particular types of relocs. Of course,
11009 reloc sections themselves can't have relocations. */
11010 reloc_count = 0;
0e1862bb 11011 else if (emit_relocs)
c152c796
AM
11012 reloc_count = sec->reloc_count;
11013 else if (bed->elf_backend_count_relocs)
58217f29 11014 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11015
eea6121a
AM
11016 if (sec->rawsize > max_contents_size)
11017 max_contents_size = sec->rawsize;
11018 if (sec->size > max_contents_size)
11019 max_contents_size = sec->size;
c152c796
AM
11020
11021 /* We are interested in just local symbols, not all
11022 symbols. */
11023 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11024 && (sec->owner->flags & DYNAMIC) == 0)
11025 {
11026 size_t sym_count;
11027
11028 if (elf_bad_symtab (sec->owner))
11029 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11030 / bed->s->sizeof_sym);
11031 else
11032 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11033
11034 if (sym_count > max_sym_count)
11035 max_sym_count = sym_count;
11036
11037 if (sym_count > max_sym_shndx_count
6a40cf0c 11038 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11039 max_sym_shndx_count = sym_count;
11040
11041 if ((sec->flags & SEC_RELOC) != 0)
11042 {
d4730f92 11043 size_t ext_size = 0;
c152c796 11044
d4730f92
BS
11045 if (esdi->rel.hdr != NULL)
11046 ext_size = esdi->rel.hdr->sh_size;
11047 if (esdi->rela.hdr != NULL)
11048 ext_size += esdi->rela.hdr->sh_size;
7326c758 11049
c152c796
AM
11050 if (ext_size > max_external_reloc_size)
11051 max_external_reloc_size = ext_size;
11052 if (sec->reloc_count > max_internal_reloc_count)
11053 max_internal_reloc_count = sec->reloc_count;
11054 }
11055 }
11056 }
11057
11058 if (reloc_count == 0)
11059 continue;
11060
11061 o->reloc_count += reloc_count;
11062
0e1862bb 11063 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11064 {
d4730f92
BS
11065 if (esdi->rel.hdr)
11066 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11067 if (esdi->rela.hdr)
11068 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11069 }
11070 else
11071 {
11072 if (o->use_rela_p)
11073 esdo->rela.count += reloc_count;
2c2b4ed4 11074 else
d4730f92 11075 esdo->rel.count += reloc_count;
c152c796 11076 }
c152c796
AM
11077 }
11078
11079 if (o->reloc_count > 0)
11080 o->flags |= SEC_RELOC;
11081 else
11082 {
11083 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11084 set it (this is probably a bug) and if it is set
11085 assign_section_numbers will create a reloc section. */
11086 o->flags &=~ SEC_RELOC;
11087 }
11088
11089 /* If the SEC_ALLOC flag is not set, force the section VMA to
11090 zero. This is done in elf_fake_sections as well, but forcing
11091 the VMA to 0 here will ensure that relocs against these
11092 sections are handled correctly. */
11093 if ((o->flags & SEC_ALLOC) == 0
11094 && ! o->user_set_vma)
11095 o->vma = 0;
11096 }
11097
0e1862bb 11098 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11099 elf_link_hash_traverse (elf_hash_table (info),
11100 _bfd_elf_link_sec_merge_syms, abfd);
11101
11102 /* Figure out the file positions for everything but the symbol table
11103 and the relocs. We set symcount to force assign_section_numbers
11104 to create a symbol table. */
8539e4e8 11105 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11106 BFD_ASSERT (! abfd->output_has_begun);
11107 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11108 goto error_return;
11109
ee75fd95 11110 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11111 for (o = abfd->sections; o != NULL; o = o->next)
11112 {
d4730f92 11113 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11114 if ((o->flags & SEC_RELOC) != 0)
11115 {
d4730f92
BS
11116 if (esdo->rel.hdr
11117 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11118 goto error_return;
11119
d4730f92
BS
11120 if (esdo->rela.hdr
11121 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11122 goto error_return;
11123 }
11124
11125 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11126 to count upwards while actually outputting the relocations. */
d4730f92
BS
11127 esdo->rel.count = 0;
11128 esdo->rela.count = 0;
0ce398f1
L
11129
11130 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11131 {
11132 /* Cache the section contents so that they can be compressed
11133 later. Use bfd_malloc since it will be freed by
11134 bfd_compress_section_contents. */
11135 unsigned char *contents = esdo->this_hdr.contents;
11136 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11137 abort ();
11138 contents
11139 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11140 if (contents == NULL)
11141 goto error_return;
11142 esdo->this_hdr.contents = contents;
11143 }
c152c796
AM
11144 }
11145
c152c796 11146 /* We have now assigned file positions for all the sections except
a485e98e
AM
11147 .symtab, .strtab, and non-loaded reloc sections. We start the
11148 .symtab section at the current file position, and write directly
11149 to it. We build the .strtab section in memory. */
c152c796
AM
11150 bfd_get_symcount (abfd) = 0;
11151 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11152 /* sh_name is set in prep_headers. */
11153 symtab_hdr->sh_type = SHT_SYMTAB;
11154 /* sh_flags, sh_addr and sh_size all start off zero. */
11155 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11156 /* sh_link is set in assign_section_numbers. */
11157 /* sh_info is set below. */
11158 /* sh_offset is set just below. */
72de5009 11159 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11160
ef10c3ac
L
11161 if (max_sym_count < 20)
11162 max_sym_count = 20;
11163 elf_hash_table (info)->strtabsize = max_sym_count;
11164 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11165 elf_hash_table (info)->strtab
11166 = (struct elf_sym_strtab *) bfd_malloc (amt);
11167 if (elf_hash_table (info)->strtab == NULL)
c152c796 11168 goto error_return;
ef10c3ac
L
11169 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11170 flinfo.symshndxbuf
11171 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11172 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11173
8539e4e8 11174 if (info->strip != strip_all || emit_relocs)
c152c796 11175 {
8539e4e8
AM
11176 file_ptr off = elf_next_file_pos (abfd);
11177
11178 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11179
11180 /* Note that at this point elf_next_file_pos (abfd) is
11181 incorrect. We do not yet know the size of the .symtab section.
11182 We correct next_file_pos below, after we do know the size. */
11183
11184 /* Start writing out the symbol table. The first symbol is always a
11185 dummy symbol. */
c152c796
AM
11186 elfsym.st_value = 0;
11187 elfsym.st_size = 0;
11188 elfsym.st_info = 0;
11189 elfsym.st_other = 0;
11190 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11191 elfsym.st_target_internal = 0;
ef10c3ac
L
11192 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11193 bfd_und_section_ptr, NULL) != 1)
c152c796 11194 goto error_return;
c152c796 11195
8539e4e8
AM
11196 /* Output a symbol for each section. We output these even if we are
11197 discarding local symbols, since they are used for relocs. These
11198 symbols have no names. We store the index of each one in the
11199 index field of the section, so that we can find it again when
11200 outputting relocs. */
11201
c152c796
AM
11202 elfsym.st_size = 0;
11203 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11204 elfsym.st_other = 0;
f0b5bb34 11205 elfsym.st_value = 0;
35fc36a8 11206 elfsym.st_target_internal = 0;
c152c796
AM
11207 for (i = 1; i < elf_numsections (abfd); i++)
11208 {
11209 o = bfd_section_from_elf_index (abfd, i);
11210 if (o != NULL)
f0b5bb34
AM
11211 {
11212 o->target_index = bfd_get_symcount (abfd);
11213 elfsym.st_shndx = i;
0e1862bb 11214 if (!bfd_link_relocatable (info))
f0b5bb34 11215 elfsym.st_value = o->vma;
ef10c3ac
L
11216 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11217 NULL) != 1)
f0b5bb34
AM
11218 goto error_return;
11219 }
c152c796
AM
11220 }
11221 }
11222
11223 /* Allocate some memory to hold information read in from the input
11224 files. */
11225 if (max_contents_size != 0)
11226 {
8b127cbc
AM
11227 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11228 if (flinfo.contents == NULL)
c152c796
AM
11229 goto error_return;
11230 }
11231
11232 if (max_external_reloc_size != 0)
11233 {
8b127cbc
AM
11234 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11235 if (flinfo.external_relocs == NULL)
c152c796
AM
11236 goto error_return;
11237 }
11238
11239 if (max_internal_reloc_count != 0)
11240 {
11241 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11242 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11243 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11244 if (flinfo.internal_relocs == NULL)
c152c796
AM
11245 goto error_return;
11246 }
11247
11248 if (max_sym_count != 0)
11249 {
11250 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11251 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11252 if (flinfo.external_syms == NULL)
c152c796
AM
11253 goto error_return;
11254
11255 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11256 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11257 if (flinfo.internal_syms == NULL)
c152c796
AM
11258 goto error_return;
11259
11260 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11261 flinfo.indices = (long int *) bfd_malloc (amt);
11262 if (flinfo.indices == NULL)
c152c796
AM
11263 goto error_return;
11264
11265 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11266 flinfo.sections = (asection **) bfd_malloc (amt);
11267 if (flinfo.sections == NULL)
c152c796
AM
11268 goto error_return;
11269 }
11270
11271 if (max_sym_shndx_count != 0)
11272 {
11273 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11274 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11275 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11276 goto error_return;
11277 }
11278
11279 if (elf_hash_table (info)->tls_sec)
11280 {
11281 bfd_vma base, end = 0;
11282 asection *sec;
11283
11284 for (sec = elf_hash_table (info)->tls_sec;
11285 sec && (sec->flags & SEC_THREAD_LOCAL);
11286 sec = sec->next)
11287 {
3a800eb9 11288 bfd_size_type size = sec->size;
c152c796 11289
3a800eb9
AM
11290 if (size == 0
11291 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11292 {
91d6fa6a
NC
11293 struct bfd_link_order *ord = sec->map_tail.link_order;
11294
11295 if (ord != NULL)
11296 size = ord->offset + ord->size;
c152c796
AM
11297 }
11298 end = sec->vma + size;
11299 }
11300 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11301 /* Only align end of TLS section if static TLS doesn't have special
11302 alignment requirements. */
11303 if (bed->static_tls_alignment == 1)
11304 end = align_power (end,
11305 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11306 elf_hash_table (info)->tls_size = end - base;
11307 }
11308
0b52efa6
PB
11309 /* Reorder SHF_LINK_ORDER sections. */
11310 for (o = abfd->sections; o != NULL; o = o->next)
11311 {
11312 if (!elf_fixup_link_order (abfd, o))
11313 return FALSE;
11314 }
11315
2f0c68f2
CM
11316 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11317 return FALSE;
11318
c152c796
AM
11319 /* Since ELF permits relocations to be against local symbols, we
11320 must have the local symbols available when we do the relocations.
11321 Since we would rather only read the local symbols once, and we
11322 would rather not keep them in memory, we handle all the
11323 relocations for a single input file at the same time.
11324
11325 Unfortunately, there is no way to know the total number of local
11326 symbols until we have seen all of them, and the local symbol
11327 indices precede the global symbol indices. This means that when
11328 we are generating relocatable output, and we see a reloc against
11329 a global symbol, we can not know the symbol index until we have
11330 finished examining all the local symbols to see which ones we are
11331 going to output. To deal with this, we keep the relocations in
11332 memory, and don't output them until the end of the link. This is
11333 an unfortunate waste of memory, but I don't see a good way around
11334 it. Fortunately, it only happens when performing a relocatable
11335 link, which is not the common case. FIXME: If keep_memory is set
11336 we could write the relocs out and then read them again; I don't
11337 know how bad the memory loss will be. */
11338
c72f2fb2 11339 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11340 sub->output_has_begun = FALSE;
11341 for (o = abfd->sections; o != NULL; o = o->next)
11342 {
8423293d 11343 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11344 {
11345 if (p->type == bfd_indirect_link_order
11346 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11347 == bfd_target_elf_flavour)
11348 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11349 {
11350 if (! sub->output_has_begun)
11351 {
8b127cbc 11352 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11353 goto error_return;
11354 sub->output_has_begun = TRUE;
11355 }
11356 }
11357 else if (p->type == bfd_section_reloc_link_order
11358 || p->type == bfd_symbol_reloc_link_order)
11359 {
11360 if (! elf_reloc_link_order (abfd, info, o, p))
11361 goto error_return;
11362 }
11363 else
11364 {
11365 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11366 {
11367 if (p->type == bfd_indirect_link_order
11368 && (bfd_get_flavour (sub)
11369 == bfd_target_elf_flavour)
11370 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11371 != bed->s->elfclass))
11372 {
11373 const char *iclass, *oclass;
11374
11375 if (bed->s->elfclass == ELFCLASS64)
11376 {
11377 iclass = "ELFCLASS32";
11378 oclass = "ELFCLASS64";
11379 }
11380 else
11381 {
11382 iclass = "ELFCLASS64";
11383 oclass = "ELFCLASS32";
11384 }
11385
11386 bfd_set_error (bfd_error_wrong_format);
11387 (*_bfd_error_handler)
11388 (_("%B: file class %s incompatible with %s"),
11389 sub, iclass, oclass);
11390 }
11391
11392 goto error_return;
11393 }
c152c796
AM
11394 }
11395 }
11396 }
11397
c0f00686
L
11398 /* Free symbol buffer if needed. */
11399 if (!info->reduce_memory_overheads)
11400 {
c72f2fb2 11401 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11402 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11403 && elf_tdata (sub)->symbuf)
c0f00686
L
11404 {
11405 free (elf_tdata (sub)->symbuf);
11406 elf_tdata (sub)->symbuf = NULL;
11407 }
11408 }
11409
c152c796
AM
11410 /* Output any global symbols that got converted to local in a
11411 version script or due to symbol visibility. We do this in a
11412 separate step since ELF requires all local symbols to appear
11413 prior to any global symbols. FIXME: We should only do this if
11414 some global symbols were, in fact, converted to become local.
11415 FIXME: Will this work correctly with the Irix 5 linker? */
11416 eoinfo.failed = FALSE;
8b127cbc 11417 eoinfo.flinfo = &flinfo;
c152c796 11418 eoinfo.localsyms = TRUE;
34a79995 11419 eoinfo.file_sym_done = FALSE;
7686d77d 11420 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11421 if (eoinfo.failed)
11422 return FALSE;
11423
4e617b1e
PB
11424 /* If backend needs to output some local symbols not present in the hash
11425 table, do it now. */
8539e4e8
AM
11426 if (bed->elf_backend_output_arch_local_syms
11427 && (info->strip != strip_all || emit_relocs))
4e617b1e 11428 {
6e0b88f1 11429 typedef int (*out_sym_func)
4e617b1e
PB
11430 (void *, const char *, Elf_Internal_Sym *, asection *,
11431 struct elf_link_hash_entry *);
11432
11433 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11434 (abfd, info, &flinfo,
11435 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11436 return FALSE;
11437 }
11438
c152c796
AM
11439 /* That wrote out all the local symbols. Finish up the symbol table
11440 with the global symbols. Even if we want to strip everything we
11441 can, we still need to deal with those global symbols that got
11442 converted to local in a version script. */
11443
11444 /* The sh_info field records the index of the first non local symbol. */
11445 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11446
11447 if (dynamic
cae1fbbb
L
11448 && elf_hash_table (info)->dynsym != NULL
11449 && (elf_hash_table (info)->dynsym->output_section
11450 != bfd_abs_section_ptr))
c152c796
AM
11451 {
11452 Elf_Internal_Sym sym;
cae1fbbb 11453 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11454 long last_local = 0;
11455
11456 /* Write out the section symbols for the output sections. */
0e1862bb
L
11457 if (bfd_link_pic (info)
11458 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11459 {
11460 asection *s;
11461
11462 sym.st_size = 0;
11463 sym.st_name = 0;
11464 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11465 sym.st_other = 0;
35fc36a8 11466 sym.st_target_internal = 0;
c152c796
AM
11467
11468 for (s = abfd->sections; s != NULL; s = s->next)
11469 {
11470 int indx;
11471 bfd_byte *dest;
11472 long dynindx;
11473
c152c796 11474 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11475 if (dynindx <= 0)
11476 continue;
11477 indx = elf_section_data (s)->this_idx;
c152c796
AM
11478 BFD_ASSERT (indx > 0);
11479 sym.st_shndx = indx;
c0d5a53d
L
11480 if (! check_dynsym (abfd, &sym))
11481 return FALSE;
c152c796
AM
11482 sym.st_value = s->vma;
11483 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11484 if (last_local < dynindx)
11485 last_local = dynindx;
c152c796
AM
11486 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11487 }
c152c796
AM
11488 }
11489
11490 /* Write out the local dynsyms. */
11491 if (elf_hash_table (info)->dynlocal)
11492 {
11493 struct elf_link_local_dynamic_entry *e;
11494 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11495 {
11496 asection *s;
11497 bfd_byte *dest;
11498
935bd1e0 11499 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11500 Note that we saved a word of storage and overwrote
11501 the original st_name with the dynstr_index. */
11502 sym = e->isym;
935bd1e0 11503 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11504
cb33740c
AM
11505 s = bfd_section_from_elf_index (e->input_bfd,
11506 e->isym.st_shndx);
11507 if (s != NULL)
c152c796 11508 {
c152c796
AM
11509 sym.st_shndx =
11510 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11511 if (! check_dynsym (abfd, &sym))
11512 return FALSE;
c152c796
AM
11513 sym.st_value = (s->output_section->vma
11514 + s->output_offset
11515 + e->isym.st_value);
11516 }
11517
11518 if (last_local < e->dynindx)
11519 last_local = e->dynindx;
11520
11521 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11522 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11523 }
11524 }
11525
cae1fbbb 11526 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11527 last_local + 1;
11528 }
11529
11530 /* We get the global symbols from the hash table. */
11531 eoinfo.failed = FALSE;
11532 eoinfo.localsyms = FALSE;
8b127cbc 11533 eoinfo.flinfo = &flinfo;
7686d77d 11534 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11535 if (eoinfo.failed)
11536 return FALSE;
11537
11538 /* If backend needs to output some symbols not present in the hash
11539 table, do it now. */
8539e4e8
AM
11540 if (bed->elf_backend_output_arch_syms
11541 && (info->strip != strip_all || emit_relocs))
c152c796 11542 {
6e0b88f1 11543 typedef int (*out_sym_func)
c152c796
AM
11544 (void *, const char *, Elf_Internal_Sym *, asection *,
11545 struct elf_link_hash_entry *);
11546
11547 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11548 (abfd, info, &flinfo,
11549 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11550 return FALSE;
11551 }
11552
ef10c3ac
L
11553 /* Finalize the .strtab section. */
11554 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11555
11556 /* Swap out the .strtab section. */
11557 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11558 return FALSE;
11559
11560 /* Now we know the size of the symtab section. */
c152c796
AM
11561 if (bfd_get_symcount (abfd) > 0)
11562 {
ee3b52e9
L
11563 /* Finish up and write out the symbol string table (.strtab)
11564 section. */
11565 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11566 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11567
6a40cf0c
NC
11568 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11569 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11570 {
11571 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11572 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11573 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11574 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11575 symtab_shndx_hdr->sh_size = amt;
11576
11577 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11578 off, TRUE);
11579
11580 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11581 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11582 return FALSE;
11583 }
ee3b52e9
L
11584
11585 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11586 /* sh_name was set in prep_headers. */
11587 symstrtab_hdr->sh_type = SHT_STRTAB;
11588 symstrtab_hdr->sh_flags = 0;
11589 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11590 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11591 symstrtab_hdr->sh_entsize = 0;
11592 symstrtab_hdr->sh_link = 0;
11593 symstrtab_hdr->sh_info = 0;
11594 /* sh_offset is set just below. */
11595 symstrtab_hdr->sh_addralign = 1;
11596
11597 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11598 off, TRUE);
11599 elf_next_file_pos (abfd) = off;
11600
c152c796 11601 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11602 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11603 return FALSE;
11604 }
11605
11606 /* Adjust the relocs to have the correct symbol indices. */
11607 for (o = abfd->sections; o != NULL; o = o->next)
11608 {
d4730f92 11609 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11610 bfd_boolean sort;
c152c796
AM
11611 if ((o->flags & SEC_RELOC) == 0)
11612 continue;
11613
28dbcedc 11614 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11615 if (esdo->rel.hdr != NULL
11616 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11617 return FALSE;
11618 if (esdo->rela.hdr != NULL
11619 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11620 return FALSE;
c152c796
AM
11621
11622 /* Set the reloc_count field to 0 to prevent write_relocs from
11623 trying to swap the relocs out itself. */
11624 o->reloc_count = 0;
11625 }
11626
11627 if (dynamic && info->combreloc && dynobj != NULL)
11628 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11629
11630 /* If we are linking against a dynamic object, or generating a
11631 shared library, finish up the dynamic linking information. */
11632 if (dynamic)
11633 {
11634 bfd_byte *dyncon, *dynconend;
11635
11636 /* Fix up .dynamic entries. */
3d4d4302 11637 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11638 BFD_ASSERT (o != NULL);
11639
11640 dyncon = o->contents;
eea6121a 11641 dynconend = o->contents + o->size;
c152c796
AM
11642 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11643 {
11644 Elf_Internal_Dyn dyn;
11645 const char *name;
11646 unsigned int type;
11647
11648 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11649
11650 switch (dyn.d_tag)
11651 {
11652 default:
11653 continue;
11654 case DT_NULL:
11655 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11656 {
11657 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11658 {
11659 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11660 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11661 default: continue;
11662 }
11663 dyn.d_un.d_val = relativecount;
11664 relativecount = 0;
11665 break;
11666 }
11667 continue;
11668
11669 case DT_INIT:
11670 name = info->init_function;
11671 goto get_sym;
11672 case DT_FINI:
11673 name = info->fini_function;
11674 get_sym:
11675 {
11676 struct elf_link_hash_entry *h;
11677
11678 h = elf_link_hash_lookup (elf_hash_table (info), name,
11679 FALSE, FALSE, TRUE);
11680 if (h != NULL
11681 && (h->root.type == bfd_link_hash_defined
11682 || h->root.type == bfd_link_hash_defweak))
11683 {
bef26483 11684 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11685 o = h->root.u.def.section;
11686 if (o->output_section != NULL)
bef26483 11687 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11688 + o->output_offset);
11689 else
11690 {
11691 /* The symbol is imported from another shared
11692 library and does not apply to this one. */
bef26483 11693 dyn.d_un.d_ptr = 0;
c152c796
AM
11694 }
11695 break;
11696 }
11697 }
11698 continue;
11699
11700 case DT_PREINIT_ARRAYSZ:
11701 name = ".preinit_array";
11702 goto get_size;
11703 case DT_INIT_ARRAYSZ:
11704 name = ".init_array";
11705 goto get_size;
11706 case DT_FINI_ARRAYSZ:
11707 name = ".fini_array";
11708 get_size:
11709 o = bfd_get_section_by_name (abfd, name);
11710 if (o == NULL)
11711 {
11712 (*_bfd_error_handler)
d003868e 11713 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11714 goto error_return;
11715 }
eea6121a 11716 if (o->size == 0)
c152c796
AM
11717 (*_bfd_error_handler)
11718 (_("warning: %s section has zero size"), name);
eea6121a 11719 dyn.d_un.d_val = o->size;
c152c796
AM
11720 break;
11721
11722 case DT_PREINIT_ARRAY:
11723 name = ".preinit_array";
11724 goto get_vma;
11725 case DT_INIT_ARRAY:
11726 name = ".init_array";
11727 goto get_vma;
11728 case DT_FINI_ARRAY:
11729 name = ".fini_array";
11730 goto get_vma;
11731
11732 case DT_HASH:
11733 name = ".hash";
11734 goto get_vma;
fdc90cb4
JJ
11735 case DT_GNU_HASH:
11736 name = ".gnu.hash";
11737 goto get_vma;
c152c796
AM
11738 case DT_STRTAB:
11739 name = ".dynstr";
11740 goto get_vma;
11741 case DT_SYMTAB:
11742 name = ".dynsym";
11743 goto get_vma;
11744 case DT_VERDEF:
11745 name = ".gnu.version_d";
11746 goto get_vma;
11747 case DT_VERNEED:
11748 name = ".gnu.version_r";
11749 goto get_vma;
11750 case DT_VERSYM:
11751 name = ".gnu.version";
11752 get_vma:
11753 o = bfd_get_section_by_name (abfd, name);
11754 if (o == NULL)
11755 {
11756 (*_bfd_error_handler)
d003868e 11757 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11758 goto error_return;
11759 }
894891db
NC
11760 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11761 {
11762 (*_bfd_error_handler)
11763 (_("warning: section '%s' is being made into a note"), name);
11764 bfd_set_error (bfd_error_nonrepresentable_section);
11765 goto error_return;
11766 }
c152c796
AM
11767 dyn.d_un.d_ptr = o->vma;
11768 break;
11769
11770 case DT_REL:
11771 case DT_RELA:
11772 case DT_RELSZ:
11773 case DT_RELASZ:
11774 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11775 type = SHT_REL;
11776 else
11777 type = SHT_RELA;
11778 dyn.d_un.d_val = 0;
bef26483 11779 dyn.d_un.d_ptr = 0;
c152c796
AM
11780 for (i = 1; i < elf_numsections (abfd); i++)
11781 {
11782 Elf_Internal_Shdr *hdr;
11783
11784 hdr = elf_elfsections (abfd)[i];
11785 if (hdr->sh_type == type
11786 && (hdr->sh_flags & SHF_ALLOC) != 0)
11787 {
11788 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11789 dyn.d_un.d_val += hdr->sh_size;
11790 else
11791 {
bef26483
AM
11792 if (dyn.d_un.d_ptr == 0
11793 || hdr->sh_addr < dyn.d_un.d_ptr)
11794 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11795 }
11796 }
11797 }
11798 break;
11799 }
11800 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11801 }
11802 }
11803
11804 /* If we have created any dynamic sections, then output them. */
11805 if (dynobj != NULL)
11806 {
11807 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11808 goto error_return;
11809
943284cc 11810 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 11811 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 11812 || info->error_textrel)
3d4d4302 11813 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11814 {
11815 bfd_byte *dyncon, *dynconend;
11816
943284cc
DJ
11817 dyncon = o->contents;
11818 dynconend = o->contents + o->size;
11819 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11820 {
11821 Elf_Internal_Dyn dyn;
11822
11823 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11824
11825 if (dyn.d_tag == DT_TEXTREL)
11826 {
c192a133
AM
11827 if (info->error_textrel)
11828 info->callbacks->einfo
11829 (_("%P%X: read-only segment has dynamic relocations.\n"));
11830 else
11831 info->callbacks->einfo
11832 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11833 break;
11834 }
11835 }
11836 }
11837
c152c796
AM
11838 for (o = dynobj->sections; o != NULL; o = o->next)
11839 {
11840 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11841 || o->size == 0
c152c796
AM
11842 || o->output_section == bfd_abs_section_ptr)
11843 continue;
11844 if ((o->flags & SEC_LINKER_CREATED) == 0)
11845 {
11846 /* At this point, we are only interested in sections
11847 created by _bfd_elf_link_create_dynamic_sections. */
11848 continue;
11849 }
3722b82f
AM
11850 if (elf_hash_table (info)->stab_info.stabstr == o)
11851 continue;
eea6121a
AM
11852 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11853 continue;
3d4d4302 11854 if (strcmp (o->name, ".dynstr") != 0)
c152c796 11855 {
5dabe785 11856 /* FIXME: octets_per_byte. */
c152c796
AM
11857 if (! bfd_set_section_contents (abfd, o->output_section,
11858 o->contents,
11859 (file_ptr) o->output_offset,
eea6121a 11860 o->size))
c152c796
AM
11861 goto error_return;
11862 }
11863 else
11864 {
11865 /* The contents of the .dynstr section are actually in a
11866 stringtab. */
8539e4e8
AM
11867 file_ptr off;
11868
c152c796
AM
11869 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11870 if (bfd_seek (abfd, off, SEEK_SET) != 0
11871 || ! _bfd_elf_strtab_emit (abfd,
11872 elf_hash_table (info)->dynstr))
11873 goto error_return;
11874 }
11875 }
11876 }
11877
0e1862bb 11878 if (bfd_link_relocatable (info))
c152c796
AM
11879 {
11880 bfd_boolean failed = FALSE;
11881
11882 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11883 if (failed)
11884 goto error_return;
11885 }
11886
11887 /* If we have optimized stabs strings, output them. */
3722b82f 11888 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11889 {
11890 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11891 goto error_return;
11892 }
11893
9f7c3e5e
AM
11894 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11895 goto error_return;
c152c796 11896
9f7c3e5e 11897 elf_final_link_free (abfd, &flinfo);
c152c796 11898
12bd6957 11899 elf_linker (abfd) = TRUE;
c152c796 11900
104d59d1
JM
11901 if (attr_section)
11902 {
a50b1753 11903 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11904 if (contents == NULL)
d0f16d5e 11905 return FALSE; /* Bail out and fail. */
104d59d1
JM
11906 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11907 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11908 free (contents);
11909 }
11910
c152c796
AM
11911 return TRUE;
11912
11913 error_return:
9f7c3e5e 11914 elf_final_link_free (abfd, &flinfo);
c152c796
AM
11915 return FALSE;
11916}
11917\f
5241d853
RS
11918/* Initialize COOKIE for input bfd ABFD. */
11919
11920static bfd_boolean
11921init_reloc_cookie (struct elf_reloc_cookie *cookie,
11922 struct bfd_link_info *info, bfd *abfd)
11923{
11924 Elf_Internal_Shdr *symtab_hdr;
11925 const struct elf_backend_data *bed;
11926
11927 bed = get_elf_backend_data (abfd);
11928 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11929
11930 cookie->abfd = abfd;
11931 cookie->sym_hashes = elf_sym_hashes (abfd);
11932 cookie->bad_symtab = elf_bad_symtab (abfd);
11933 if (cookie->bad_symtab)
11934 {
11935 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11936 cookie->extsymoff = 0;
11937 }
11938 else
11939 {
11940 cookie->locsymcount = symtab_hdr->sh_info;
11941 cookie->extsymoff = symtab_hdr->sh_info;
11942 }
11943
11944 if (bed->s->arch_size == 32)
11945 cookie->r_sym_shift = 8;
11946 else
11947 cookie->r_sym_shift = 32;
11948
11949 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11950 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11951 {
11952 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11953 cookie->locsymcount, 0,
11954 NULL, NULL, NULL);
11955 if (cookie->locsyms == NULL)
11956 {
11957 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11958 return FALSE;
11959 }
11960 if (info->keep_memory)
11961 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11962 }
11963 return TRUE;
11964}
11965
11966/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11967
11968static void
11969fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11970{
11971 Elf_Internal_Shdr *symtab_hdr;
11972
11973 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11974 if (cookie->locsyms != NULL
11975 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11976 free (cookie->locsyms);
11977}
11978
11979/* Initialize the relocation information in COOKIE for input section SEC
11980 of input bfd ABFD. */
11981
11982static bfd_boolean
11983init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11984 struct bfd_link_info *info, bfd *abfd,
11985 asection *sec)
11986{
11987 const struct elf_backend_data *bed;
11988
11989 if (sec->reloc_count == 0)
11990 {
11991 cookie->rels = NULL;
11992 cookie->relend = NULL;
11993 }
11994 else
11995 {
11996 bed = get_elf_backend_data (abfd);
11997
11998 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11999 info->keep_memory);
12000 if (cookie->rels == NULL)
12001 return FALSE;
12002 cookie->rel = cookie->rels;
12003 cookie->relend = (cookie->rels
12004 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12005 }
12006 cookie->rel = cookie->rels;
12007 return TRUE;
12008}
12009
12010/* Free the memory allocated by init_reloc_cookie_rels,
12011 if appropriate. */
12012
12013static void
12014fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12015 asection *sec)
12016{
12017 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12018 free (cookie->rels);
12019}
12020
12021/* Initialize the whole of COOKIE for input section SEC. */
12022
12023static bfd_boolean
12024init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12025 struct bfd_link_info *info,
12026 asection *sec)
12027{
12028 if (!init_reloc_cookie (cookie, info, sec->owner))
12029 goto error1;
12030 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12031 goto error2;
12032 return TRUE;
12033
12034 error2:
12035 fini_reloc_cookie (cookie, sec->owner);
12036 error1:
12037 return FALSE;
12038}
12039
12040/* Free the memory allocated by init_reloc_cookie_for_section,
12041 if appropriate. */
12042
12043static void
12044fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12045 asection *sec)
12046{
12047 fini_reloc_cookie_rels (cookie, sec);
12048 fini_reloc_cookie (cookie, sec->owner);
12049}
12050\f
c152c796
AM
12051/* Garbage collect unused sections. */
12052
07adf181
AM
12053/* Default gc_mark_hook. */
12054
12055asection *
12056_bfd_elf_gc_mark_hook (asection *sec,
12057 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12058 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12059 struct elf_link_hash_entry *h,
12060 Elf_Internal_Sym *sym)
12061{
bde6f3eb
L
12062 const char *sec_name;
12063
07adf181
AM
12064 if (h != NULL)
12065 {
12066 switch (h->root.type)
12067 {
12068 case bfd_link_hash_defined:
12069 case bfd_link_hash_defweak:
12070 return h->root.u.def.section;
12071
12072 case bfd_link_hash_common:
12073 return h->root.u.c.p->section;
12074
bde6f3eb
L
12075 case bfd_link_hash_undefined:
12076 case bfd_link_hash_undefweak:
12077 /* To work around a glibc bug, keep all XXX input sections
12078 when there is an as yet undefined reference to __start_XXX
12079 or __stop_XXX symbols. The linker will later define such
12080 symbols for orphan input sections that have a name
12081 representable as a C identifier. */
12082 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12083 sec_name = h->root.root.string + 8;
12084 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12085 sec_name = h->root.root.string + 7;
12086 else
12087 sec_name = NULL;
12088
12089 if (sec_name && *sec_name != '\0')
12090 {
12091 bfd *i;
68ffbac6 12092
c72f2fb2 12093 for (i = info->input_bfds; i; i = i->link.next)
bde6f3eb
L
12094 {
12095 sec = bfd_get_section_by_name (i, sec_name);
12096 if (sec)
12097 sec->flags |= SEC_KEEP;
12098 }
12099 }
12100 break;
12101
07adf181
AM
12102 default:
12103 break;
12104 }
12105 }
12106 else
12107 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12108
12109 return NULL;
12110}
12111
5241d853
RS
12112/* COOKIE->rel describes a relocation against section SEC, which is
12113 a section we've decided to keep. Return the section that contains
12114 the relocation symbol, or NULL if no section contains it. */
12115
12116asection *
12117_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12118 elf_gc_mark_hook_fn gc_mark_hook,
12119 struct elf_reloc_cookie *cookie)
12120{
12121 unsigned long r_symndx;
12122 struct elf_link_hash_entry *h;
12123
12124 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12125 if (r_symndx == STN_UNDEF)
5241d853
RS
12126 return NULL;
12127
12128 if (r_symndx >= cookie->locsymcount
12129 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12130 {
12131 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12132 if (h == NULL)
12133 {
12134 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12135 sec->owner);
12136 return NULL;
12137 }
5241d853
RS
12138 while (h->root.type == bfd_link_hash_indirect
12139 || h->root.type == bfd_link_hash_warning)
12140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12141 h->mark = 1;
4e6b54a6
AM
12142 /* If this symbol is weak and there is a non-weak definition, we
12143 keep the non-weak definition because many backends put
12144 dynamic reloc info on the non-weak definition for code
12145 handling copy relocs. */
12146 if (h->u.weakdef != NULL)
12147 h->u.weakdef->mark = 1;
5241d853
RS
12148 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12149 }
12150
12151 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12152 &cookie->locsyms[r_symndx]);
12153}
12154
12155/* COOKIE->rel describes a relocation against section SEC, which is
12156 a section we've decided to keep. Mark the section that contains
9d0a14d3 12157 the relocation symbol. */
5241d853
RS
12158
12159bfd_boolean
12160_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12161 asection *sec,
12162 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12163 struct elf_reloc_cookie *cookie)
5241d853
RS
12164{
12165 asection *rsec;
12166
12167 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
12168 if (rsec && !rsec->gc_mark)
12169 {
a66eed7a
AM
12170 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12171 || (rsec->owner->flags & DYNAMIC) != 0)
5241d853 12172 rsec->gc_mark = 1;
5241d853
RS
12173 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12174 return FALSE;
12175 }
12176 return TRUE;
12177}
12178
07adf181
AM
12179/* The mark phase of garbage collection. For a given section, mark
12180 it and any sections in this section's group, and all the sections
12181 which define symbols to which it refers. */
12182
ccfa59ea
AM
12183bfd_boolean
12184_bfd_elf_gc_mark (struct bfd_link_info *info,
12185 asection *sec,
6a5bb875 12186 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12187{
12188 bfd_boolean ret;
9d0a14d3 12189 asection *group_sec, *eh_frame;
c152c796
AM
12190
12191 sec->gc_mark = 1;
12192
12193 /* Mark all the sections in the group. */
12194 group_sec = elf_section_data (sec)->next_in_group;
12195 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12196 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12197 return FALSE;
12198
12199 /* Look through the section relocs. */
12200 ret = TRUE;
9d0a14d3
RS
12201 eh_frame = elf_eh_frame_section (sec->owner);
12202 if ((sec->flags & SEC_RELOC) != 0
12203 && sec->reloc_count > 0
12204 && sec != eh_frame)
c152c796 12205 {
5241d853 12206 struct elf_reloc_cookie cookie;
c152c796 12207
5241d853
RS
12208 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12209 ret = FALSE;
c152c796 12210 else
c152c796 12211 {
5241d853 12212 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12213 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12214 {
12215 ret = FALSE;
12216 break;
12217 }
12218 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12219 }
12220 }
9d0a14d3
RS
12221
12222 if (ret && eh_frame && elf_fde_list (sec))
12223 {
12224 struct elf_reloc_cookie cookie;
12225
12226 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12227 ret = FALSE;
12228 else
12229 {
12230 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12231 gc_mark_hook, &cookie))
12232 ret = FALSE;
12233 fini_reloc_cookie_for_section (&cookie, eh_frame);
12234 }
12235 }
12236
2f0c68f2
CM
12237 eh_frame = elf_section_eh_frame_entry (sec);
12238 if (ret && eh_frame && !eh_frame->gc_mark)
12239 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12240 ret = FALSE;
12241
c152c796
AM
12242 return ret;
12243}
12244
3c758495
TG
12245/* Scan and mark sections in a special or debug section group. */
12246
12247static void
12248_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12249{
12250 /* Point to first section of section group. */
12251 asection *ssec;
12252 /* Used to iterate the section group. */
12253 asection *msec;
12254
12255 bfd_boolean is_special_grp = TRUE;
12256 bfd_boolean is_debug_grp = TRUE;
12257
12258 /* First scan to see if group contains any section other than debug
12259 and special section. */
12260 ssec = msec = elf_next_in_group (grp);
12261 do
12262 {
12263 if ((msec->flags & SEC_DEBUGGING) == 0)
12264 is_debug_grp = FALSE;
12265
12266 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12267 is_special_grp = FALSE;
12268
12269 msec = elf_next_in_group (msec);
12270 }
12271 while (msec != ssec);
12272
12273 /* If this is a pure debug section group or pure special section group,
12274 keep all sections in this group. */
12275 if (is_debug_grp || is_special_grp)
12276 {
12277 do
12278 {
12279 msec->gc_mark = 1;
12280 msec = elf_next_in_group (msec);
12281 }
12282 while (msec != ssec);
12283 }
12284}
12285
7f6ab9f8
AM
12286/* Keep debug and special sections. */
12287
12288bfd_boolean
12289_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12290 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12291{
12292 bfd *ibfd;
12293
c72f2fb2 12294 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12295 {
12296 asection *isec;
12297 bfd_boolean some_kept;
b40bf0a2 12298 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12299
12300 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12301 continue;
12302
b40bf0a2
NC
12303 /* Ensure all linker created sections are kept,
12304 see if any other section is already marked,
12305 and note if we have any fragmented debug sections. */
12306 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12307 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12308 {
12309 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12310 isec->gc_mark = 1;
12311 else if (isec->gc_mark)
12312 some_kept = TRUE;
b40bf0a2
NC
12313
12314 if (debug_frag_seen == FALSE
12315 && (isec->flags & SEC_DEBUGGING)
12316 && CONST_STRNEQ (isec->name, ".debug_line."))
12317 debug_frag_seen = TRUE;
7f6ab9f8
AM
12318 }
12319
12320 /* If no section in this file will be kept, then we can
b40bf0a2 12321 toss out the debug and special sections. */
7f6ab9f8
AM
12322 if (!some_kept)
12323 continue;
12324
12325 /* Keep debug and special sections like .comment when they are
3c758495
TG
12326 not part of a group. Also keep section groups that contain
12327 just debug sections or special sections. */
7f6ab9f8 12328 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12329 {
12330 if ((isec->flags & SEC_GROUP) != 0)
12331 _bfd_elf_gc_mark_debug_special_section_group (isec);
12332 else if (((isec->flags & SEC_DEBUGGING) != 0
12333 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12334 && elf_next_in_group (isec) == NULL)
12335 isec->gc_mark = 1;
12336 }
b40bf0a2
NC
12337
12338 if (! debug_frag_seen)
12339 continue;
12340
12341 /* Look for CODE sections which are going to be discarded,
12342 and find and discard any fragmented debug sections which
12343 are associated with that code section. */
12344 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12345 if ((isec->flags & SEC_CODE) != 0
12346 && isec->gc_mark == 0)
12347 {
12348 unsigned int ilen;
12349 asection *dsec;
12350
12351 ilen = strlen (isec->name);
12352
12353 /* Association is determined by the name of the debug section
12354 containing the name of the code section as a suffix. For
12355 example .debug_line.text.foo is a debug section associated
12356 with .text.foo. */
12357 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12358 {
12359 unsigned int dlen;
12360
12361 if (dsec->gc_mark == 0
12362 || (dsec->flags & SEC_DEBUGGING) == 0)
12363 continue;
12364
12365 dlen = strlen (dsec->name);
12366
12367 if (dlen > ilen
12368 && strncmp (dsec->name + (dlen - ilen),
12369 isec->name, ilen) == 0)
12370 {
12371 dsec->gc_mark = 0;
b40bf0a2
NC
12372 }
12373 }
12374 }
7f6ab9f8
AM
12375 }
12376 return TRUE;
12377}
12378
c152c796
AM
12379/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12380
c17d87de
NC
12381struct elf_gc_sweep_symbol_info
12382{
ccabcbe5
AM
12383 struct bfd_link_info *info;
12384 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12385 bfd_boolean);
12386};
12387
c152c796 12388static bfd_boolean
ccabcbe5 12389elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12390{
1d5316ab
AM
12391 if (!h->mark
12392 && (((h->root.type == bfd_link_hash_defined
12393 || h->root.type == bfd_link_hash_defweak)
c4621b33 12394 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12395 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12396 || h->root.type == bfd_link_hash_undefined
12397 || h->root.type == bfd_link_hash_undefweak))
12398 {
12399 struct elf_gc_sweep_symbol_info *inf;
12400
12401 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12402 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12403 h->def_regular = 0;
12404 h->ref_regular = 0;
12405 h->ref_regular_nonweak = 0;
ccabcbe5 12406 }
c152c796
AM
12407
12408 return TRUE;
12409}
12410
12411/* The sweep phase of garbage collection. Remove all garbage sections. */
12412
12413typedef bfd_boolean (*gc_sweep_hook_fn)
12414 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12415
12416static bfd_boolean
ccabcbe5 12417elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12418{
12419 bfd *sub;
ccabcbe5
AM
12420 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12421 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12422 unsigned long section_sym_count;
12423 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12424
c72f2fb2 12425 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12426 {
12427 asection *o;
12428
b19a8f85
L
12429 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12430 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12431 continue;
12432
12433 for (o = sub->sections; o != NULL; o = o->next)
12434 {
a33dafc3
L
12435 /* When any section in a section group is kept, we keep all
12436 sections in the section group. If the first member of
12437 the section group is excluded, we will also exclude the
12438 group section. */
12439 if (o->flags & SEC_GROUP)
12440 {
12441 asection *first = elf_next_in_group (o);
12442 o->gc_mark = first->gc_mark;
12443 }
c152c796
AM
12444
12445 if (o->gc_mark)
12446 continue;
12447
12448 /* Skip sweeping sections already excluded. */
12449 if (o->flags & SEC_EXCLUDE)
12450 continue;
12451
12452 /* Since this is early in the link process, it is simple
12453 to remove a section from the output. */
12454 o->flags |= SEC_EXCLUDE;
12455
c55fe096 12456 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12457 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12458
c152c796
AM
12459 /* But we also have to update some of the relocation
12460 info we collected before. */
12461 if (gc_sweep_hook
e8aaee2a 12462 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12463 && o->reloc_count != 0
12464 && !((info->strip == strip_all || info->strip == strip_debugger)
12465 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12466 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12467 {
12468 Elf_Internal_Rela *internal_relocs;
12469 bfd_boolean r;
12470
12471 internal_relocs
12472 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12473 info->keep_memory);
12474 if (internal_relocs == NULL)
12475 return FALSE;
12476
12477 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12478
12479 if (elf_section_data (o)->relocs != internal_relocs)
12480 free (internal_relocs);
12481
12482 if (!r)
12483 return FALSE;
12484 }
12485 }
12486 }
12487
12488 /* Remove the symbols that were in the swept sections from the dynamic
12489 symbol table. GCFIXME: Anyone know how to get them out of the
12490 static symbol table as well? */
ccabcbe5
AM
12491 sweep_info.info = info;
12492 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12493 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12494 &sweep_info);
c152c796 12495
ccabcbe5 12496 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12497 return TRUE;
12498}
12499
12500/* Propagate collected vtable information. This is called through
12501 elf_link_hash_traverse. */
12502
12503static bfd_boolean
12504elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12505{
c152c796 12506 /* Those that are not vtables. */
f6e332e6 12507 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12508 return TRUE;
12509
12510 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12511 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12512 return TRUE;
12513
12514 /* If we've already been done, exit. */
f6e332e6 12515 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12516 return TRUE;
12517
12518 /* Make sure the parent's table is up to date. */
f6e332e6 12519 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12520
f6e332e6 12521 if (h->vtable->used == NULL)
c152c796
AM
12522 {
12523 /* None of this table's entries were referenced. Re-use the
12524 parent's table. */
f6e332e6
AM
12525 h->vtable->used = h->vtable->parent->vtable->used;
12526 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12527 }
12528 else
12529 {
12530 size_t n;
12531 bfd_boolean *cu, *pu;
12532
12533 /* Or the parent's entries into ours. */
f6e332e6 12534 cu = h->vtable->used;
c152c796 12535 cu[-1] = TRUE;
f6e332e6 12536 pu = h->vtable->parent->vtable->used;
c152c796
AM
12537 if (pu != NULL)
12538 {
12539 const struct elf_backend_data *bed;
12540 unsigned int log_file_align;
12541
12542 bed = get_elf_backend_data (h->root.u.def.section->owner);
12543 log_file_align = bed->s->log_file_align;
f6e332e6 12544 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12545 while (n--)
12546 {
12547 if (*pu)
12548 *cu = TRUE;
12549 pu++;
12550 cu++;
12551 }
12552 }
12553 }
12554
12555 return TRUE;
12556}
12557
12558static bfd_boolean
12559elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12560{
12561 asection *sec;
12562 bfd_vma hstart, hend;
12563 Elf_Internal_Rela *relstart, *relend, *rel;
12564 const struct elf_backend_data *bed;
12565 unsigned int log_file_align;
12566
c152c796
AM
12567 /* Take care of both those symbols that do not describe vtables as
12568 well as those that are not loaded. */
f6e332e6 12569 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12570 return TRUE;
12571
12572 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12573 || h->root.type == bfd_link_hash_defweak);
12574
12575 sec = h->root.u.def.section;
12576 hstart = h->root.u.def.value;
12577 hend = hstart + h->size;
12578
12579 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12580 if (!relstart)
12581 return *(bfd_boolean *) okp = FALSE;
12582 bed = get_elf_backend_data (sec->owner);
12583 log_file_align = bed->s->log_file_align;
12584
12585 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12586
12587 for (rel = relstart; rel < relend; ++rel)
12588 if (rel->r_offset >= hstart && rel->r_offset < hend)
12589 {
12590 /* If the entry is in use, do nothing. */
f6e332e6
AM
12591 if (h->vtable->used
12592 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12593 {
12594 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12595 if (h->vtable->used[entry])
c152c796
AM
12596 continue;
12597 }
12598 /* Otherwise, kill it. */
12599 rel->r_offset = rel->r_info = rel->r_addend = 0;
12600 }
12601
12602 return TRUE;
12603}
12604
87538722
AM
12605/* Mark sections containing dynamically referenced symbols. When
12606 building shared libraries, we must assume that any visible symbol is
12607 referenced. */
715df9b8 12608
64d03ab5
AM
12609bfd_boolean
12610bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12611{
87538722 12612 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12613 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12614
715df9b8
EB
12615 if ((h->root.type == bfd_link_hash_defined
12616 || h->root.type == bfd_link_hash_defweak)
87538722 12617 && (h->ref_dynamic
c4621b33 12618 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12619 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12620 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12621 && (!bfd_link_executable (info)
b407645f
AM
12622 || info->export_dynamic
12623 || (h->dynamic
12624 && d != NULL
12625 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12626 && (h->versioned >= versioned
54e8959c
L
12627 || !bfd_hide_sym_by_version (info->version_info,
12628 h->root.root.string)))))
715df9b8
EB
12629 h->root.u.def.section->flags |= SEC_KEEP;
12630
12631 return TRUE;
12632}
3b36f7e6 12633
74f0fb50
AM
12634/* Keep all sections containing symbols undefined on the command-line,
12635 and the section containing the entry symbol. */
12636
12637void
12638_bfd_elf_gc_keep (struct bfd_link_info *info)
12639{
12640 struct bfd_sym_chain *sym;
12641
12642 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12643 {
12644 struct elf_link_hash_entry *h;
12645
12646 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12647 FALSE, FALSE, FALSE);
12648
12649 if (h != NULL
12650 && (h->root.type == bfd_link_hash_defined
12651 || h->root.type == bfd_link_hash_defweak)
12652 && !bfd_is_abs_section (h->root.u.def.section))
12653 h->root.u.def.section->flags |= SEC_KEEP;
12654 }
12655}
12656
2f0c68f2
CM
12657bfd_boolean
12658bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12659 struct bfd_link_info *info)
12660{
12661 bfd *ibfd = info->input_bfds;
12662
12663 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12664 {
12665 asection *sec;
12666 struct elf_reloc_cookie cookie;
12667
12668 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12669 continue;
12670
12671 if (!init_reloc_cookie (&cookie, info, ibfd))
12672 return FALSE;
12673
12674 for (sec = ibfd->sections; sec; sec = sec->next)
12675 {
12676 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12677 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12678 {
12679 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12680 fini_reloc_cookie_rels (&cookie, sec);
12681 }
12682 }
12683 }
12684 return TRUE;
12685}
12686
c152c796
AM
12687/* Do mark and sweep of unused sections. */
12688
12689bfd_boolean
12690bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12691{
12692 bfd_boolean ok = TRUE;
12693 bfd *sub;
6a5bb875 12694 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12695 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12696 struct elf_link_hash_table *htab;
c152c796 12697
64d03ab5 12698 if (!bed->can_gc_sections
715df9b8 12699 || !is_elf_hash_table (info->hash))
c152c796
AM
12700 {
12701 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12702 return TRUE;
12703 }
12704
74f0fb50 12705 bed->gc_keep (info);
da44f4e5 12706 htab = elf_hash_table (info);
74f0fb50 12707
9d0a14d3
RS
12708 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12709 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12710 for (sub = info->input_bfds;
12711 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12712 sub = sub->link.next)
9d0a14d3
RS
12713 {
12714 asection *sec;
12715 struct elf_reloc_cookie cookie;
12716
12717 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12718 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12719 {
12720 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12721 if (elf_section_data (sec)->sec_info
12722 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12723 elf_eh_frame_section (sub) = sec;
12724 fini_reloc_cookie_for_section (&cookie, sec);
9a2a56cc 12725 sec = bfd_get_next_section_by_name (sec);
9d0a14d3
RS
12726 }
12727 }
9d0a14d3 12728
c152c796 12729 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12730 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12731 if (!ok)
12732 return FALSE;
12733
12734 /* Kill the vtable relocations that were not used. */
da44f4e5 12735 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12736 if (!ok)
12737 return FALSE;
12738
715df9b8 12739 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12740 if (htab->dynamic_sections_created)
12741 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 12742
715df9b8 12743 /* Grovel through relocs to find out who stays ... */
64d03ab5 12744 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 12745 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12746 {
12747 asection *o;
12748
b19a8f85
L
12749 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12750 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12751 continue;
12752
7f6ab9f8
AM
12753 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12754 Also treat note sections as a root, if the section is not part
12755 of a group. */
c152c796 12756 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12757 if (!o->gc_mark
12758 && (o->flags & SEC_EXCLUDE) == 0
24007750 12759 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12760 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12761 && elf_next_in_group (o) == NULL )))
12762 {
12763 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12764 return FALSE;
12765 }
c152c796
AM
12766 }
12767
6a5bb875 12768 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12769 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12770
c152c796 12771 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12772 return elf_gc_sweep (abfd, info);
c152c796
AM
12773}
12774\f
12775/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12776
12777bfd_boolean
12778bfd_elf_gc_record_vtinherit (bfd *abfd,
12779 asection *sec,
12780 struct elf_link_hash_entry *h,
12781 bfd_vma offset)
12782{
12783 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12784 struct elf_link_hash_entry **search, *child;
12785 bfd_size_type extsymcount;
12786 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12787
12788 /* The sh_info field of the symtab header tells us where the
12789 external symbols start. We don't care about the local symbols at
12790 this point. */
12791 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12792 if (!elf_bad_symtab (abfd))
12793 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12794
12795 sym_hashes = elf_sym_hashes (abfd);
12796 sym_hashes_end = sym_hashes + extsymcount;
12797
12798 /* Hunt down the child symbol, which is in this section at the same
12799 offset as the relocation. */
12800 for (search = sym_hashes; search != sym_hashes_end; ++search)
12801 {
12802 if ((child = *search) != NULL
12803 && (child->root.type == bfd_link_hash_defined
12804 || child->root.type == bfd_link_hash_defweak)
12805 && child->root.u.def.section == sec
12806 && child->root.u.def.value == offset)
12807 goto win;
12808 }
12809
d003868e
AM
12810 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12811 abfd, sec, (unsigned long) offset);
c152c796
AM
12812 bfd_set_error (bfd_error_invalid_operation);
12813 return FALSE;
12814
12815 win:
f6e332e6
AM
12816 if (!child->vtable)
12817 {
ca4be51c
AM
12818 child->vtable = ((struct elf_link_virtual_table_entry *)
12819 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
12820 if (!child->vtable)
12821 return FALSE;
12822 }
c152c796
AM
12823 if (!h)
12824 {
12825 /* This *should* only be the absolute section. It could potentially
12826 be that someone has defined a non-global vtable though, which
12827 would be bad. It isn't worth paging in the local symbols to be
12828 sure though; that case should simply be handled by the assembler. */
12829
f6e332e6 12830 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12831 }
12832 else
f6e332e6 12833 child->vtable->parent = h;
c152c796
AM
12834
12835 return TRUE;
12836}
12837
12838/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12839
12840bfd_boolean
12841bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12842 asection *sec ATTRIBUTE_UNUSED,
12843 struct elf_link_hash_entry *h,
12844 bfd_vma addend)
12845{
12846 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12847 unsigned int log_file_align = bed->s->log_file_align;
12848
f6e332e6
AM
12849 if (!h->vtable)
12850 {
ca4be51c
AM
12851 h->vtable = ((struct elf_link_virtual_table_entry *)
12852 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
12853 if (!h->vtable)
12854 return FALSE;
12855 }
12856
12857 if (addend >= h->vtable->size)
c152c796
AM
12858 {
12859 size_t size, bytes, file_align;
f6e332e6 12860 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12861
12862 /* While the symbol is undefined, we have to be prepared to handle
12863 a zero size. */
12864 file_align = 1 << log_file_align;
12865 if (h->root.type == bfd_link_hash_undefined)
12866 size = addend + file_align;
12867 else
12868 {
12869 size = h->size;
12870 if (addend >= size)
12871 {
12872 /* Oops! We've got a reference past the defined end of
12873 the table. This is probably a bug -- shall we warn? */
12874 size = addend + file_align;
12875 }
12876 }
12877 size = (size + file_align - 1) & -file_align;
12878
12879 /* Allocate one extra entry for use as a "done" flag for the
12880 consolidation pass. */
12881 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12882
12883 if (ptr)
12884 {
a50b1753 12885 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12886
12887 if (ptr != NULL)
12888 {
12889 size_t oldbytes;
12890
f6e332e6 12891 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12892 * sizeof (bfd_boolean));
12893 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12894 }
12895 }
12896 else
a50b1753 12897 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12898
12899 if (ptr == NULL)
12900 return FALSE;
12901
12902 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12903 h->vtable->used = ptr + 1;
12904 h->vtable->size = size;
c152c796
AM
12905 }
12906
f6e332e6 12907 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12908
12909 return TRUE;
12910}
12911
ae17ab41
CM
12912/* Map an ELF section header flag to its corresponding string. */
12913typedef struct
12914{
12915 char *flag_name;
12916 flagword flag_value;
12917} elf_flags_to_name_table;
12918
12919static elf_flags_to_name_table elf_flags_to_names [] =
12920{
12921 { "SHF_WRITE", SHF_WRITE },
12922 { "SHF_ALLOC", SHF_ALLOC },
12923 { "SHF_EXECINSTR", SHF_EXECINSTR },
12924 { "SHF_MERGE", SHF_MERGE },
12925 { "SHF_STRINGS", SHF_STRINGS },
12926 { "SHF_INFO_LINK", SHF_INFO_LINK},
12927 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12928 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12929 { "SHF_GROUP", SHF_GROUP },
12930 { "SHF_TLS", SHF_TLS },
12931 { "SHF_MASKOS", SHF_MASKOS },
12932 { "SHF_EXCLUDE", SHF_EXCLUDE },
12933};
12934
b9c361e0
JL
12935/* Returns TRUE if the section is to be included, otherwise FALSE. */
12936bfd_boolean
ae17ab41 12937bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 12938 struct flag_info *flaginfo,
b9c361e0 12939 asection *section)
ae17ab41 12940{
8b127cbc 12941 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 12942
8b127cbc 12943 if (!flaginfo->flags_initialized)
ae17ab41 12944 {
8b127cbc
AM
12945 bfd *obfd = info->output_bfd;
12946 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12947 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
12948 int with_hex = 0;
12949 int without_hex = 0;
12950
8b127cbc 12951 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 12952 {
b9c361e0 12953 unsigned i;
8b127cbc 12954 flagword (*lookup) (char *);
ae17ab41 12955
8b127cbc
AM
12956 lookup = bed->elf_backend_lookup_section_flags_hook;
12957 if (lookup != NULL)
ae17ab41 12958 {
8b127cbc 12959 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
12960
12961 if (hexval != 0)
12962 {
12963 if (tf->with == with_flags)
12964 with_hex |= hexval;
12965 else if (tf->with == without_flags)
12966 without_hex |= hexval;
12967 tf->valid = TRUE;
12968 continue;
12969 }
ae17ab41 12970 }
8b127cbc 12971 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 12972 {
8b127cbc 12973 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
12974 {
12975 if (tf->with == with_flags)
12976 with_hex |= elf_flags_to_names[i].flag_value;
12977 else if (tf->with == without_flags)
12978 without_hex |= elf_flags_to_names[i].flag_value;
12979 tf->valid = TRUE;
12980 break;
12981 }
12982 }
8b127cbc 12983 if (!tf->valid)
b9c361e0 12984 {
68ffbac6 12985 info->callbacks->einfo
8b127cbc 12986 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 12987 return FALSE;
ae17ab41
CM
12988 }
12989 }
8b127cbc
AM
12990 flaginfo->flags_initialized = TRUE;
12991 flaginfo->only_with_flags |= with_hex;
12992 flaginfo->not_with_flags |= without_hex;
ae17ab41 12993 }
ae17ab41 12994
8b127cbc 12995 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
12996 return FALSE;
12997
8b127cbc 12998 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
12999 return FALSE;
13000
13001 return TRUE;
ae17ab41
CM
13002}
13003
c152c796
AM
13004struct alloc_got_off_arg {
13005 bfd_vma gotoff;
10455f89 13006 struct bfd_link_info *info;
c152c796
AM
13007};
13008
13009/* We need a special top-level link routine to convert got reference counts
13010 to real got offsets. */
13011
13012static bfd_boolean
13013elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13014{
a50b1753 13015 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13016 bfd *obfd = gofarg->info->output_bfd;
13017 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13018
c152c796
AM
13019 if (h->got.refcount > 0)
13020 {
13021 h->got.offset = gofarg->gotoff;
10455f89 13022 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13023 }
13024 else
13025 h->got.offset = (bfd_vma) -1;
13026
13027 return TRUE;
13028}
13029
13030/* And an accompanying bit to work out final got entry offsets once
13031 we're done. Should be called from final_link. */
13032
13033bfd_boolean
13034bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13035 struct bfd_link_info *info)
13036{
13037 bfd *i;
13038 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13039 bfd_vma gotoff;
c152c796
AM
13040 struct alloc_got_off_arg gofarg;
13041
10455f89
HPN
13042 BFD_ASSERT (abfd == info->output_bfd);
13043
c152c796
AM
13044 if (! is_elf_hash_table (info->hash))
13045 return FALSE;
13046
13047 /* The GOT offset is relative to the .got section, but the GOT header is
13048 put into the .got.plt section, if the backend uses it. */
13049 if (bed->want_got_plt)
13050 gotoff = 0;
13051 else
13052 gotoff = bed->got_header_size;
13053
13054 /* Do the local .got entries first. */
c72f2fb2 13055 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13056 {
13057 bfd_signed_vma *local_got;
13058 bfd_size_type j, locsymcount;
13059 Elf_Internal_Shdr *symtab_hdr;
13060
13061 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13062 continue;
13063
13064 local_got = elf_local_got_refcounts (i);
13065 if (!local_got)
13066 continue;
13067
13068 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13069 if (elf_bad_symtab (i))
13070 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13071 else
13072 locsymcount = symtab_hdr->sh_info;
13073
13074 for (j = 0; j < locsymcount; ++j)
13075 {
13076 if (local_got[j] > 0)
13077 {
13078 local_got[j] = gotoff;
10455f89 13079 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13080 }
13081 else
13082 local_got[j] = (bfd_vma) -1;
13083 }
13084 }
13085
13086 /* Then the global .got entries. .plt refcounts are handled by
13087 adjust_dynamic_symbol */
13088 gofarg.gotoff = gotoff;
10455f89 13089 gofarg.info = info;
c152c796
AM
13090 elf_link_hash_traverse (elf_hash_table (info),
13091 elf_gc_allocate_got_offsets,
13092 &gofarg);
13093 return TRUE;
13094}
13095
13096/* Many folk need no more in the way of final link than this, once
13097 got entry reference counting is enabled. */
13098
13099bfd_boolean
13100bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13101{
13102 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13103 return FALSE;
13104
13105 /* Invoke the regular ELF backend linker to do all the work. */
13106 return bfd_elf_final_link (abfd, info);
13107}
13108
13109bfd_boolean
13110bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13111{
a50b1753 13112 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13113
13114 if (rcookie->bad_symtab)
13115 rcookie->rel = rcookie->rels;
13116
13117 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13118 {
13119 unsigned long r_symndx;
13120
13121 if (! rcookie->bad_symtab)
13122 if (rcookie->rel->r_offset > offset)
13123 return FALSE;
13124 if (rcookie->rel->r_offset != offset)
13125 continue;
13126
13127 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13128 if (r_symndx == STN_UNDEF)
c152c796
AM
13129 return TRUE;
13130
13131 if (r_symndx >= rcookie->locsymcount
13132 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13133 {
13134 struct elf_link_hash_entry *h;
13135
13136 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13137
13138 while (h->root.type == bfd_link_hash_indirect
13139 || h->root.type == bfd_link_hash_warning)
13140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13141
13142 if ((h->root.type == bfd_link_hash_defined
13143 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13144 && (h->root.u.def.section->owner != rcookie->abfd
13145 || h->root.u.def.section->kept_section != NULL
13146 || discarded_section (h->root.u.def.section)))
c152c796 13147 return TRUE;
c152c796
AM
13148 }
13149 else
13150 {
13151 /* It's not a relocation against a global symbol,
13152 but it could be a relocation against a local
13153 symbol for a discarded section. */
13154 asection *isec;
13155 Elf_Internal_Sym *isym;
13156
13157 /* Need to: get the symbol; get the section. */
13158 isym = &rcookie->locsyms[r_symndx];
cb33740c 13159 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13160 if (isec != NULL
13161 && (isec->kept_section != NULL
13162 || discarded_section (isec)))
cb33740c 13163 return TRUE;
c152c796
AM
13164 }
13165 return FALSE;
13166 }
13167 return FALSE;
13168}
13169
13170/* Discard unneeded references to discarded sections.
75938853
AM
13171 Returns -1 on error, 1 if any section's size was changed, 0 if
13172 nothing changed. This function assumes that the relocations are in
13173 sorted order, which is true for all known assemblers. */
c152c796 13174
75938853 13175int
c152c796
AM
13176bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13177{
13178 struct elf_reloc_cookie cookie;
18cd5bce 13179 asection *o;
c152c796 13180 bfd *abfd;
75938853 13181 int changed = 0;
c152c796
AM
13182
13183 if (info->traditional_format
13184 || !is_elf_hash_table (info->hash))
75938853 13185 return 0;
c152c796 13186
18cd5bce
AM
13187 o = bfd_get_section_by_name (output_bfd, ".stab");
13188 if (o != NULL)
c152c796 13189 {
18cd5bce 13190 asection *i;
c152c796 13191
18cd5bce 13192 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13193 {
18cd5bce
AM
13194 if (i->size == 0
13195 || i->reloc_count == 0
13196 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13197 continue;
c152c796 13198
18cd5bce
AM
13199 abfd = i->owner;
13200 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13201 continue;
c152c796 13202
18cd5bce 13203 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13204 return -1;
c152c796 13205
18cd5bce
AM
13206 if (_bfd_discard_section_stabs (abfd, i,
13207 elf_section_data (i)->sec_info,
5241d853
RS
13208 bfd_elf_reloc_symbol_deleted_p,
13209 &cookie))
75938853 13210 changed = 1;
18cd5bce
AM
13211
13212 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13213 }
18cd5bce
AM
13214 }
13215
2f0c68f2
CM
13216 o = NULL;
13217 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13218 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13219 if (o != NULL)
13220 {
13221 asection *i;
c152c796 13222
18cd5bce 13223 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13224 {
18cd5bce
AM
13225 if (i->size == 0)
13226 continue;
13227
13228 abfd = i->owner;
13229 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13230 continue;
13231
13232 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13233 return -1;
18cd5bce
AM
13234
13235 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13236 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13237 bfd_elf_reloc_symbol_deleted_p,
13238 &cookie))
75938853 13239 changed = 1;
18cd5bce
AM
13240
13241 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13242 }
18cd5bce 13243 }
c152c796 13244
18cd5bce
AM
13245 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13246 {
13247 const struct elf_backend_data *bed;
c152c796 13248
18cd5bce
AM
13249 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13250 continue;
13251
13252 bed = get_elf_backend_data (abfd);
13253
13254 if (bed->elf_backend_discard_info != NULL)
13255 {
13256 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13257 return -1;
18cd5bce
AM
13258
13259 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13260 changed = 1;
18cd5bce
AM
13261
13262 fini_reloc_cookie (&cookie, abfd);
13263 }
c152c796
AM
13264 }
13265
2f0c68f2
CM
13266 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13267 _bfd_elf_end_eh_frame_parsing (info);
13268
13269 if (info->eh_frame_hdr_type
0e1862bb 13270 && !bfd_link_relocatable (info)
c152c796 13271 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13272 changed = 1;
c152c796 13273
75938853 13274 return changed;
c152c796 13275}
082b7297 13276
43e1669b 13277bfd_boolean
0c511000 13278_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13279 asection *sec,
c0f00686 13280 struct bfd_link_info *info)
082b7297
L
13281{
13282 flagword flags;
c77ec726 13283 const char *name, *key;
082b7297
L
13284 struct bfd_section_already_linked *l;
13285 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13286
c77ec726
AM
13287 if (sec->output_section == bfd_abs_section_ptr)
13288 return FALSE;
0c511000 13289
c77ec726 13290 flags = sec->flags;
0c511000 13291
c77ec726
AM
13292 /* Return if it isn't a linkonce section. A comdat group section
13293 also has SEC_LINK_ONCE set. */
13294 if ((flags & SEC_LINK_ONCE) == 0)
13295 return FALSE;
0c511000 13296
c77ec726
AM
13297 /* Don't put group member sections on our list of already linked
13298 sections. They are handled as a group via their group section. */
13299 if (elf_sec_group (sec) != NULL)
13300 return FALSE;
0c511000 13301
c77ec726
AM
13302 /* For a SHT_GROUP section, use the group signature as the key. */
13303 name = sec->name;
13304 if ((flags & SEC_GROUP) != 0
13305 && elf_next_in_group (sec) != NULL
13306 && elf_group_name (elf_next_in_group (sec)) != NULL)
13307 key = elf_group_name (elf_next_in_group (sec));
13308 else
13309 {
13310 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13311 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13312 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13313 key++;
0c511000 13314 else
c77ec726
AM
13315 /* Must be a user linkonce section that doesn't follow gcc's
13316 naming convention. In this case we won't be matching
13317 single member groups. */
13318 key = name;
0c511000 13319 }
6d2cd210 13320
c77ec726 13321 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13322
13323 for (l = already_linked_list->entry; l != NULL; l = l->next)
13324 {
c2370991 13325 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13326 sections with a signature of <key> (<key> is some string),
13327 and linkonce sections named .gnu.linkonce.<type>.<key>.
13328 Match like sections. LTO plugin sections are an exception.
13329 They are always named .gnu.linkonce.t.<key> and match either
13330 type of section. */
13331 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13332 && ((flags & SEC_GROUP) != 0
13333 || strcmp (name, l->sec->name) == 0))
13334 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13335 {
13336 /* The section has already been linked. See if we should
6d2cd210 13337 issue a warning. */
c77ec726
AM
13338 if (!_bfd_handle_already_linked (sec, l, info))
13339 return FALSE;
082b7297 13340
c77ec726 13341 if (flags & SEC_GROUP)
3d7f7666 13342 {
c77ec726
AM
13343 asection *first = elf_next_in_group (sec);
13344 asection *s = first;
3d7f7666 13345
c77ec726 13346 while (s != NULL)
3d7f7666 13347 {
c77ec726
AM
13348 s->output_section = bfd_abs_section_ptr;
13349 /* Record which group discards it. */
13350 s->kept_section = l->sec;
13351 s = elf_next_in_group (s);
13352 /* These lists are circular. */
13353 if (s == first)
13354 break;
3d7f7666
L
13355 }
13356 }
082b7297 13357
43e1669b 13358 return TRUE;
082b7297
L
13359 }
13360 }
13361
c77ec726
AM
13362 /* A single member comdat group section may be discarded by a
13363 linkonce section and vice versa. */
13364 if ((flags & SEC_GROUP) != 0)
3d7f7666 13365 {
c77ec726 13366 asection *first = elf_next_in_group (sec);
c2370991 13367
c77ec726
AM
13368 if (first != NULL && elf_next_in_group (first) == first)
13369 /* Check this single member group against linkonce sections. */
13370 for (l = already_linked_list->entry; l != NULL; l = l->next)
13371 if ((l->sec->flags & SEC_GROUP) == 0
13372 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13373 {
13374 first->output_section = bfd_abs_section_ptr;
13375 first->kept_section = l->sec;
13376 sec->output_section = bfd_abs_section_ptr;
13377 break;
13378 }
13379 }
13380 else
13381 /* Check this linkonce section against single member groups. */
13382 for (l = already_linked_list->entry; l != NULL; l = l->next)
13383 if (l->sec->flags & SEC_GROUP)
6d2cd210 13384 {
c77ec726 13385 asection *first = elf_next_in_group (l->sec);
6d2cd210 13386
c77ec726
AM
13387 if (first != NULL
13388 && elf_next_in_group (first) == first
13389 && bfd_elf_match_symbols_in_sections (first, sec, info))
13390 {
13391 sec->output_section = bfd_abs_section_ptr;
13392 sec->kept_section = first;
13393 break;
13394 }
6d2cd210 13395 }
0c511000 13396
c77ec726
AM
13397 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13398 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13399 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13400 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13401 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13402 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13403 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13404 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13405 The reverse order cannot happen as there is never a bfd with only the
13406 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13407 matter as here were are looking only for cross-bfd sections. */
13408
13409 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13410 for (l = already_linked_list->entry; l != NULL; l = l->next)
13411 if ((l->sec->flags & SEC_GROUP) == 0
13412 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13413 {
13414 if (abfd != l->sec->owner)
13415 sec->output_section = bfd_abs_section_ptr;
13416 break;
13417 }
80c29487 13418
082b7297 13419 /* This is the first section with this name. Record it. */
c77ec726 13420 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13421 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13422 return sec->output_section == bfd_abs_section_ptr;
082b7297 13423}
81e1b023 13424
a4d8e49b
L
13425bfd_boolean
13426_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13427{
13428 return sym->st_shndx == SHN_COMMON;
13429}
13430
13431unsigned int
13432_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13433{
13434 return SHN_COMMON;
13435}
13436
13437asection *
13438_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13439{
13440 return bfd_com_section_ptr;
13441}
10455f89
HPN
13442
13443bfd_vma
13444_bfd_elf_default_got_elt_size (bfd *abfd,
13445 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13446 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13447 bfd *ibfd ATTRIBUTE_UNUSED,
13448 unsigned long symndx ATTRIBUTE_UNUSED)
13449{
13450 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13451 return bed->s->arch_size / 8;
13452}
83bac4b0
NC
13453
13454/* Routines to support the creation of dynamic relocs. */
13455
83bac4b0
NC
13456/* Returns the name of the dynamic reloc section associated with SEC. */
13457
13458static const char *
13459get_dynamic_reloc_section_name (bfd * abfd,
13460 asection * sec,
13461 bfd_boolean is_rela)
13462{
ddcf1fcf
BS
13463 char *name;
13464 const char *old_name = bfd_get_section_name (NULL, sec);
13465 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13466
ddcf1fcf 13467 if (old_name == NULL)
83bac4b0
NC
13468 return NULL;
13469
ddcf1fcf 13470 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13471 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13472
13473 return name;
13474}
13475
13476/* Returns the dynamic reloc section associated with SEC.
13477 If necessary compute the name of the dynamic reloc section based
13478 on SEC's name (looked up in ABFD's string table) and the setting
13479 of IS_RELA. */
13480
13481asection *
13482_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13483 asection * sec,
13484 bfd_boolean is_rela)
13485{
13486 asection * reloc_sec = elf_section_data (sec)->sreloc;
13487
13488 if (reloc_sec == NULL)
13489 {
13490 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13491
13492 if (name != NULL)
13493 {
3d4d4302 13494 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13495
13496 if (reloc_sec != NULL)
13497 elf_section_data (sec)->sreloc = reloc_sec;
13498 }
13499 }
13500
13501 return reloc_sec;
13502}
13503
13504/* Returns the dynamic reloc section associated with SEC. If the
13505 section does not exist it is created and attached to the DYNOBJ
13506 bfd and stored in the SRELOC field of SEC's elf_section_data
13507 structure.
f8076f98 13508
83bac4b0
NC
13509 ALIGNMENT is the alignment for the newly created section and
13510 IS_RELA defines whether the name should be .rela.<SEC's name>
13511 or .rel.<SEC's name>. The section name is looked up in the
13512 string table associated with ABFD. */
13513
13514asection *
ca4be51c
AM
13515_bfd_elf_make_dynamic_reloc_section (asection *sec,
13516 bfd *dynobj,
13517 unsigned int alignment,
13518 bfd *abfd,
13519 bfd_boolean is_rela)
83bac4b0
NC
13520{
13521 asection * reloc_sec = elf_section_data (sec)->sreloc;
13522
13523 if (reloc_sec == NULL)
13524 {
13525 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13526
13527 if (name == NULL)
13528 return NULL;
13529
3d4d4302 13530 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13531
13532 if (reloc_sec == NULL)
13533 {
3d4d4302
AM
13534 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13535 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13536 if ((sec->flags & SEC_ALLOC) != 0)
13537 flags |= SEC_ALLOC | SEC_LOAD;
13538
3d4d4302 13539 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13540 if (reloc_sec != NULL)
13541 {
8877b5e5
AM
13542 /* _bfd_elf_get_sec_type_attr chooses a section type by
13543 name. Override as it may be wrong, eg. for a user
13544 section named "auto" we'll get ".relauto" which is
13545 seen to be a .rela section. */
13546 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13547 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13548 reloc_sec = NULL;
13549 }
13550 }
13551
13552 elf_section_data (sec)->sreloc = reloc_sec;
13553 }
13554
13555 return reloc_sec;
13556}
1338dd10 13557
bffebb6b
AM
13558/* Copy the ELF symbol type and other attributes for a linker script
13559 assignment from HSRC to HDEST. Generally this should be treated as
13560 if we found a strong non-dynamic definition for HDEST (except that
13561 ld ignores multiple definition errors). */
1338dd10 13562void
bffebb6b
AM
13563_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13564 struct bfd_link_hash_entry *hdest,
13565 struct bfd_link_hash_entry *hsrc)
1338dd10 13566{
bffebb6b
AM
13567 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13568 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13569 Elf_Internal_Sym isym;
1338dd10
PB
13570
13571 ehdest->type = ehsrc->type;
35fc36a8 13572 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13573
13574 isym.st_other = ehsrc->other;
b8417128 13575 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13576}
351f65ca
L
13577
13578/* Append a RELA relocation REL to section S in BFD. */
13579
13580void
13581elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13582{
13583 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13584 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13585 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13586 bed->s->swap_reloca_out (abfd, rel, loc);
13587}
13588
13589/* Append a REL relocation REL to section S in BFD. */
13590
13591void
13592elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13593{
13594 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13595 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13596 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13597 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13598}
This page took 2.809307 seconds and 4 git commands to generate.