]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
Properly implement STT_COMMON
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
6f2750fe 2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
252b5132 31
28caa186
AM
32/* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35struct elf_info_failed
36{
37 struct bfd_link_info *info;
28caa186
AM
38 bfd_boolean failed;
39};
40
41/* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44struct elf_find_verdep_info
45{
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52};
53
54static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
2f0c68f2
CM
57asection *
58_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61{
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96}
97
d98685ac
AM
98/* Define a symbol in a dynamic linkage section. */
99
100struct elf_link_hash_entry *
101_bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105{
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
ccabcbe5 108 const struct elf_backend_data *bed;
d98685ac
AM
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
cf18fda4 121 bed = get_elf_backend_data (abfd);
d98685ac 122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 123 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
e28df02b 128 h->non_elf = 0;
12b2843a 129 h->root.linker_def = 1;
d98685ac 130 h->type = STT_OBJECT;
00b7642b
AM
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 133
ccabcbe5 134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
135 return h;
136}
137
b34976b6 138bfd_boolean
268b6b39 139_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
140{
141 flagword flags;
aad5d350 142 asection *s;
252b5132 143 struct elf_link_hash_entry *h;
9c5bfbb7 144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 145 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
146
147 /* This function may be called more than once. */
3d4d4302
AM
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
b34976b6 150 return TRUE;
252b5132 151
e5a52504 152 flags = bed->dynamic_sec_flags;
252b5132 153
14b2f831
AM
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
6de2ae4a
L
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
252b5132 163
14b2f831 164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
252b5132
RH
170 if (bed->want_got_plt)
171 {
14b2f831 172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 173 if (s == NULL
6de2ae4a
L
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
b34976b6 176 return FALSE;
6de2ae4a 177 htab->sgotplt = s;
252b5132
RH
178 }
179
64e77c6d
L
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
2517a57f
AM
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
6de2ae4a
L
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
2517a57f 191 elf_hash_table (info)->hgot = h;
d98685ac
AM
192 if (h == NULL)
193 return FALSE;
2517a57f 194 }
252b5132 195
b34976b6 196 return TRUE;
252b5132
RH
197}
198\f
7e9f0867
AM
199/* Create a strtab to hold the dynamic symbol names. */
200static bfd_boolean
201_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202{
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 hash_table->dynobj = abfd;
208
209 if (hash_table->dynstr == NULL)
210 {
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
213 return FALSE;
214 }
215 return TRUE;
216}
217
45d6a902
AM
218/* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
252b5132 224
b34976b6 225bfd_boolean
268b6b39 226_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 227{
45d6a902 228 flagword flags;
91d6fa6a 229 asection *s;
9c5bfbb7 230 const struct elf_backend_data *bed;
9637f6ef 231 struct elf_link_hash_entry *h;
252b5132 232
0eddce27 233 if (! is_elf_hash_table (info->hash))
45d6a902
AM
234 return FALSE;
235
236 if (elf_hash_table (info)->dynamic_sections_created)
237 return TRUE;
238
7e9f0867
AM
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240 return FALSE;
45d6a902 241
7e9f0867 242 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
243 bed = get_elf_backend_data (abfd);
244
245 flags = bed->dynamic_sec_flags;
45d6a902
AM
246
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
9b8b325a 249 if (bfd_link_executable (info) && !info->nointerp)
252b5132 250 {
14b2f831
AM
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
3496cb2a 253 if (s == NULL)
45d6a902
AM
254 return FALSE;
255 }
bb0deeff 256
45d6a902
AM
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
14b2f831
AM
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
45d6a902 261 if (s == NULL
45d6a902
AM
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263 return FALSE;
264
14b2f831
AM
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
45d6a902 267 if (s == NULL
45d6a902
AM
268 || ! bfd_set_section_alignment (abfd, s, 1))
269 return FALSE;
270
14b2f831
AM
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
45d6a902 273 if (s == NULL
45d6a902
AM
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275 return FALSE;
276
14b2f831
AM
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
45d6a902 279 if (s == NULL
45d6a902
AM
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281 return FALSE;
cae1fbbb 282 elf_hash_table (info)->dynsym = s;
45d6a902 283
14b2f831
AM
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
3496cb2a 286 if (s == NULL)
45d6a902
AM
287 return FALSE;
288
14b2f831 289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
294 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
9637f6ef
L
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
302 if (h == NULL)
45d6a902
AM
303 return FALSE;
304
fdc90cb4
JJ
305 if (info->emit_hash)
306 {
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
fdc90cb4
JJ
309 if (s == NULL
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 return FALSE;
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313 }
314
315 if (info->emit_gnu_hash)
316 {
14b2f831
AM
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
fdc90cb4
JJ
319 if (s == NULL
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 return FALSE;
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
327 else
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
329 }
45d6a902
AM
330
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
894891db
NC
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
336 return FALSE;
337
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
339
340 return TRUE;
341}
342
343/* Create dynamic sections when linking against a dynamic object. */
344
345bfd_boolean
268b6b39 346_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
347{
348 flagword flags, pltflags;
7325306f 349 struct elf_link_hash_entry *h;
45d6a902 350 asection *s;
9c5bfbb7 351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 352 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 353
252b5132
RH
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
e5a52504 356 flags = bed->dynamic_sec_flags;
252b5132
RH
357
358 pltflags = flags;
252b5132 359 if (bed->plt_not_loaded)
6df4d94c
MM
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
5d1634d7 363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
364 else
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
368
14b2f831 369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 370 if (s == NULL
252b5132 371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 372 return FALSE;
6de2ae4a 373 htab->splt = s;
252b5132 374
d98685ac
AM
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376 .plt section. */
7325306f
RS
377 if (bed->want_plt_sym)
378 {
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
382 if (h == NULL)
383 return FALSE;
384 }
252b5132 385
14b2f831
AM
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
252b5132 390 if (s == NULL
45d6a902 391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 392 return FALSE;
6de2ae4a 393 htab->srelplt = s;
252b5132
RH
394
395 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 396 return FALSE;
252b5132 397
3018b441
RH
398 if (bed->want_dynbss)
399 {
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
14b2f831
AM
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 408 if (s == NULL)
b34976b6 409 return FALSE;
252b5132 410
3018b441 411 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
421 copy relocs. */
0e1862bb 422 if (! bfd_link_pic (info))
3018b441 423 {
14b2f831
AM
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
3018b441 428 if (s == NULL
45d6a902 429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 430 return FALSE;
3018b441 431 }
252b5132
RH
432 }
433
b34976b6 434 return TRUE;
252b5132
RH
435}
436\f
252b5132
RH
437/* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
443 one. */
444
b34976b6 445bfd_boolean
c152c796
AM
446bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
252b5132
RH
448{
449 if (h->dynindx == -1)
450 {
2b0f7ef9 451 struct elf_strtab_hash *dynstr;
68b6ddd0 452 char *p;
252b5132 453 const char *name;
252b5132
RH
454 bfd_size_type indx;
455
7a13edea
NC
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
461 {
462 case STV_INTERNAL:
463 case STV_HIDDEN:
9d6eee78
L
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
38048eb9 466 {
f5385ebf 467 h->forced_local = 1;
67687978
PB
468 if (!elf_hash_table (info)->is_relocatable_executable)
469 return TRUE;
7a13edea 470 }
0444bdd4 471
7a13edea
NC
472 default:
473 break;
474 }
475
252b5132
RH
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
478
479 dynstr = elf_hash_table (info)->dynstr;
480 if (dynstr == NULL)
481 {
482 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 484 if (dynstr == NULL)
b34976b6 485 return FALSE;
252b5132
RH
486 }
487
488 /* We don't put any version information in the dynamic string
aad5d350 489 table. */
252b5132
RH
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
492 if (p != NULL)
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
498 *p = 0;
499
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501
502 if (p != NULL)
503 *p = ELF_VER_CHR;
252b5132
RH
504
505 if (indx == (bfd_size_type) -1)
b34976b6 506 return FALSE;
252b5132
RH
507 h->dynstr_index = indx;
508 }
509
b34976b6 510 return TRUE;
252b5132 511}
45d6a902 512\f
55255dae
L
513/* Mark a symbol dynamic. */
514
28caa186 515static void
55255dae 516bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
55255dae 519{
40b36307 520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 521
40b36307 522 /* It may be called more than once on the same H. */
0e1862bb 523 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
524 return;
525
40b36307
L
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
b8871f35 528 || h->type == STT_COMMON
40b36307 529 || (sym != NULL
b8871f35
L
530 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
531 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 532 || (d != NULL
40b36307
L
533 && h->root.type == bfd_link_hash_new
534 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
535 h->dynamic = 1;
536}
537
45d6a902
AM
538/* Record an assignment to a symbol made by a linker script. We need
539 this in case some dynamic object refers to this symbol. */
540
541bfd_boolean
fe21a8fc
L
542bfd_elf_record_link_assignment (bfd *output_bfd,
543 struct bfd_link_info *info,
268b6b39 544 const char *name,
fe21a8fc
L
545 bfd_boolean provide,
546 bfd_boolean hidden)
45d6a902 547{
00cbee0a 548 struct elf_link_hash_entry *h, *hv;
4ea42fb7 549 struct elf_link_hash_table *htab;
00cbee0a 550 const struct elf_backend_data *bed;
45d6a902 551
0eddce27 552 if (!is_elf_hash_table (info->hash))
45d6a902
AM
553 return TRUE;
554
4ea42fb7
AM
555 htab = elf_hash_table (info);
556 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 557 if (h == NULL)
4ea42fb7 558 return provide;
45d6a902 559
0f550b3d
L
560 if (h->versioned == unknown)
561 {
562 /* Set versioned if symbol version is unknown. */
563 char *version = strrchr (name, ELF_VER_CHR);
564 if (version)
565 {
566 if (version > name && version[-1] != ELF_VER_CHR)
567 h->versioned = versioned_hidden;
568 else
569 h->versioned = versioned;
570 }
571 }
572
00cbee0a 573 switch (h->root.type)
77cfaee6 574 {
00cbee0a
L
575 case bfd_link_hash_defined:
576 case bfd_link_hash_defweak:
577 case bfd_link_hash_common:
578 break;
579 case bfd_link_hash_undefweak:
580 case bfd_link_hash_undefined:
581 /* Since we're defining the symbol, don't let it seem to have not
582 been defined. record_dynamic_symbol and size_dynamic_sections
583 may depend on this. */
4ea42fb7 584 h->root.type = bfd_link_hash_new;
77cfaee6
AM
585 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
586 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
587 break;
588 case bfd_link_hash_new:
40b36307 589 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 590 h->non_elf = 0;
00cbee0a
L
591 break;
592 case bfd_link_hash_indirect:
593 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 594 the versioned symbol point to this one. */
00cbee0a
L
595 bed = get_elf_backend_data (output_bfd);
596 hv = h;
597 while (hv->root.type == bfd_link_hash_indirect
598 || hv->root.type == bfd_link_hash_warning)
599 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
600 /* We don't need to update h->root.u since linker will set them
601 later. */
602 h->root.type = bfd_link_hash_undefined;
603 hv->root.type = bfd_link_hash_indirect;
604 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
605 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
606 break;
607 case bfd_link_hash_warning:
608 abort ();
609 break;
55255dae 610 }
45d6a902
AM
611
612 /* If this symbol is being provided by the linker script, and it is
613 currently defined by a dynamic object, but not by a regular
614 object, then mark it as undefined so that the generic linker will
615 force the correct value. */
616 if (provide
f5385ebf
AM
617 && h->def_dynamic
618 && !h->def_regular)
45d6a902
AM
619 h->root.type = bfd_link_hash_undefined;
620
621 /* If this symbol is not being provided by the linker script, and it is
622 currently defined by a dynamic object, but not by a regular object,
623 then clear out any version information because the symbol will not be
624 associated with the dynamic object any more. */
625 if (!provide
f5385ebf
AM
626 && h->def_dynamic
627 && !h->def_regular)
45d6a902
AM
628 h->verinfo.verdef = NULL;
629
f5385ebf 630 h->def_regular = 1;
45d6a902 631
eb8476a6 632 if (hidden)
fe21a8fc 633 {
91d6fa6a 634 bed = get_elf_backend_data (output_bfd);
b8297068
AM
635 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
636 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
637 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
638 }
639
6fa3860b
PB
640 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
641 and executables. */
0e1862bb 642 if (!bfd_link_relocatable (info)
6fa3860b
PB
643 && h->dynindx != -1
644 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
645 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
646 h->forced_local = 1;
647
f5385ebf
AM
648 if ((h->def_dynamic
649 || h->ref_dynamic
6b3b0ab8
L
650 || bfd_link_dll (info)
651 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
652 && h->dynindx == -1)
653 {
c152c796 654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
655 return FALSE;
656
657 /* If this is a weak defined symbol, and we know a corresponding
658 real symbol from the same dynamic object, make sure the real
659 symbol is also made into a dynamic symbol. */
f6e332e6
AM
660 if (h->u.weakdef != NULL
661 && h->u.weakdef->dynindx == -1)
45d6a902 662 {
f6e332e6 663 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
664 return FALSE;
665 }
666 }
667
668 return TRUE;
669}
42751cf3 670
8c58d23b
AM
671/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
672 success, and 2 on a failure caused by attempting to record a symbol
673 in a discarded section, eg. a discarded link-once section symbol. */
674
675int
c152c796
AM
676bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
677 bfd *input_bfd,
678 long input_indx)
8c58d23b
AM
679{
680 bfd_size_type amt;
681 struct elf_link_local_dynamic_entry *entry;
682 struct elf_link_hash_table *eht;
683 struct elf_strtab_hash *dynstr;
684 unsigned long dynstr_index;
685 char *name;
686 Elf_External_Sym_Shndx eshndx;
687 char esym[sizeof (Elf64_External_Sym)];
688
0eddce27 689 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
690 return 0;
691
692 /* See if the entry exists already. */
693 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
694 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
695 return 1;
696
697 amt = sizeof (*entry);
a50b1753 698 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
699 if (entry == NULL)
700 return 0;
701
702 /* Go find the symbol, so that we can find it's name. */
703 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 704 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
705 {
706 bfd_release (input_bfd, entry);
707 return 0;
708 }
709
710 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 711 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
712 {
713 asection *s;
714
715 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
716 if (s == NULL || bfd_is_abs_section (s->output_section))
717 {
718 /* We can still bfd_release here as nothing has done another
719 bfd_alloc. We can't do this later in this function. */
720 bfd_release (input_bfd, entry);
721 return 2;
722 }
723 }
724
725 name = (bfd_elf_string_from_elf_section
726 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
727 entry->isym.st_name));
728
729 dynstr = elf_hash_table (info)->dynstr;
730 if (dynstr == NULL)
731 {
732 /* Create a strtab to hold the dynamic symbol names. */
733 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
734 if (dynstr == NULL)
735 return 0;
736 }
737
b34976b6 738 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
739 if (dynstr_index == (unsigned long) -1)
740 return 0;
741 entry->isym.st_name = dynstr_index;
742
743 eht = elf_hash_table (info);
744
745 entry->next = eht->dynlocal;
746 eht->dynlocal = entry;
747 entry->input_bfd = input_bfd;
748 entry->input_indx = input_indx;
749 eht->dynsymcount++;
750
751 /* Whatever binding the symbol had before, it's now local. */
752 entry->isym.st_info
753 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
754
755 /* The dynindx will be set at the end of size_dynamic_sections. */
756
757 return 1;
758}
759
30b30c21 760/* Return the dynindex of a local dynamic symbol. */
42751cf3 761
30b30c21 762long
268b6b39
AM
763_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
30b30c21
RH
766{
767 struct elf_link_local_dynamic_entry *e;
768
769 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
770 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
771 return e->dynindx;
772 return -1;
773}
774
775/* This function is used to renumber the dynamic symbols, if some of
776 them are removed because they are marked as local. This is called
777 via elf_link_hash_traverse. */
778
b34976b6 779static bfd_boolean
268b6b39
AM
780elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
781 void *data)
42751cf3 782{
a50b1753 783 size_t *count = (size_t *) data;
30b30c21 784
6fa3860b
PB
785 if (h->forced_local)
786 return TRUE;
787
788 if (h->dynindx != -1)
789 h->dynindx = ++(*count);
790
791 return TRUE;
792}
793
794
795/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
796 STB_LOCAL binding. */
797
798static bfd_boolean
799elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
800 void *data)
801{
a50b1753 802 size_t *count = (size_t *) data;
6fa3860b 803
6fa3860b
PB
804 if (!h->forced_local)
805 return TRUE;
806
42751cf3 807 if (h->dynindx != -1)
30b30c21
RH
808 h->dynindx = ++(*count);
809
b34976b6 810 return TRUE;
42751cf3 811}
30b30c21 812
aee6f5b4
AO
813/* Return true if the dynamic symbol for a given section should be
814 omitted when creating a shared library. */
815bfd_boolean
816_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
817 struct bfd_link_info *info,
818 asection *p)
819{
74541ad4 820 struct elf_link_hash_table *htab;
ca55926c 821 asection *ip;
74541ad4 822
aee6f5b4
AO
823 switch (elf_section_data (p)->this_hdr.sh_type)
824 {
825 case SHT_PROGBITS:
826 case SHT_NOBITS:
827 /* If sh_type is yet undecided, assume it could be
828 SHT_PROGBITS/SHT_NOBITS. */
829 case SHT_NULL:
74541ad4
AM
830 htab = elf_hash_table (info);
831 if (p == htab->tls_sec)
832 return FALSE;
833
834 if (htab->text_index_section != NULL)
835 return p != htab->text_index_section && p != htab->data_index_section;
836
ca55926c 837 return (htab->dynobj != NULL
3d4d4302 838 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 839 && ip->output_section == p);
aee6f5b4
AO
840
841 /* There shouldn't be section relative relocations
842 against any other section. */
843 default:
844 return TRUE;
845 }
846}
847
062e2358 848/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
849 symbol for each output section, which come first. Next come symbols
850 which have been forced to local binding. Then all of the back-end
851 allocated local dynamic syms, followed by the rest of the global
852 symbols. */
30b30c21 853
554220db
AM
854static unsigned long
855_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
856 struct bfd_link_info *info,
857 unsigned long *section_sym_count)
30b30c21
RH
858{
859 unsigned long dynsymcount = 0;
860
0e1862bb
L
861 if (bfd_link_pic (info)
862 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 863 {
aee6f5b4 864 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
865 asection *p;
866 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 867 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
868 && (p->flags & SEC_ALLOC) != 0
869 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
870 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
871 else
872 elf_section_data (p)->dynindx = 0;
30b30c21 873 }
554220db 874 *section_sym_count = dynsymcount;
30b30c21 875
6fa3860b
PB
876 elf_link_hash_traverse (elf_hash_table (info),
877 elf_link_renumber_local_hash_table_dynsyms,
878 &dynsymcount);
879
30b30c21
RH
880 if (elf_hash_table (info)->dynlocal)
881 {
882 struct elf_link_local_dynamic_entry *p;
883 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
884 p->dynindx = ++dynsymcount;
885 }
886
887 elf_link_hash_traverse (elf_hash_table (info),
888 elf_link_renumber_hash_table_dynsyms,
889 &dynsymcount);
890
891 /* There is an unused NULL entry at the head of the table which
1a6e6083
L
892 we must account for in our count. We always create the dynsym
893 section, even if it is empty, with dynamic sections. */
894 if (elf_hash_table (info)->dynamic_sections_created)
30b30c21
RH
895 ++dynsymcount;
896
ccabcbe5
AM
897 elf_hash_table (info)->dynsymcount = dynsymcount;
898 return dynsymcount;
30b30c21 899}
252b5132 900
54ac0771
L
901/* Merge st_other field. */
902
903static void
904elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 905 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 906 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
907{
908 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
909
910 /* If st_other has a processor-specific meaning, specific
cd3416da 911 code might be needed here. */
54ac0771
L
912 if (bed->elf_backend_merge_symbol_attribute)
913 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
914 dynamic);
915
cd3416da 916 if (!dynamic)
54ac0771 917 {
cd3416da
AM
918 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
919 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 920
cd3416da
AM
921 /* Keep the most constraining visibility. Leave the remainder
922 of the st_other field to elf_backend_merge_symbol_attribute. */
923 if (symvis - 1 < hvis - 1)
924 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 925 }
b8417128
AM
926 else if (definition
927 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
928 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 929 h->protected_def = 1;
54ac0771
L
930}
931
4f3fedcf
AM
932/* This function is called when we want to merge a new symbol with an
933 existing symbol. It handles the various cases which arise when we
934 find a definition in a dynamic object, or when there is already a
935 definition in a dynamic object. The new symbol is described by
936 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
937 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
938 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
939 of an old common symbol. We set OVERRIDE if the old symbol is
940 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
941 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
942 to change. By OK to change, we mean that we shouldn't warn if the
943 type or size does change. */
45d6a902 944
8a56bd02 945static bfd_boolean
268b6b39
AM
946_bfd_elf_merge_symbol (bfd *abfd,
947 struct bfd_link_info *info,
948 const char *name,
949 Elf_Internal_Sym *sym,
950 asection **psec,
951 bfd_vma *pvalue,
4f3fedcf
AM
952 struct elf_link_hash_entry **sym_hash,
953 bfd **poldbfd,
37a9e49a 954 bfd_boolean *pold_weak,
af44c138 955 unsigned int *pold_alignment,
268b6b39
AM
956 bfd_boolean *skip,
957 bfd_boolean *override,
958 bfd_boolean *type_change_ok,
6e33951e
L
959 bfd_boolean *size_change_ok,
960 bfd_boolean *matched)
252b5132 961{
7479dfd4 962 asection *sec, *oldsec;
45d6a902 963 struct elf_link_hash_entry *h;
90c984fc 964 struct elf_link_hash_entry *hi;
45d6a902
AM
965 struct elf_link_hash_entry *flip;
966 int bind;
967 bfd *oldbfd;
968 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 969 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 970 const struct elf_backend_data *bed;
6e33951e 971 char *new_version;
45d6a902
AM
972
973 *skip = FALSE;
974 *override = FALSE;
975
976 sec = *psec;
977 bind = ELF_ST_BIND (sym->st_info);
978
979 if (! bfd_is_und_section (sec))
980 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
981 else
982 h = ((struct elf_link_hash_entry *)
983 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
984 if (h == NULL)
985 return FALSE;
986 *sym_hash = h;
252b5132 987
88ba32a0
L
988 bed = get_elf_backend_data (abfd);
989
6e33951e 990 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 991 if (h->versioned != unversioned)
6e33951e 992 {
422f1182
L
993 /* Symbol version is unknown or versioned. */
994 new_version = strrchr (name, ELF_VER_CHR);
995 if (new_version)
996 {
997 if (h->versioned == unknown)
998 {
999 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1000 h->versioned = versioned_hidden;
1001 else
1002 h->versioned = versioned;
1003 }
1004 new_version += 1;
1005 if (new_version[0] == '\0')
1006 new_version = NULL;
1007 }
1008 else
1009 h->versioned = unversioned;
6e33951e 1010 }
422f1182
L
1011 else
1012 new_version = NULL;
6e33951e 1013
90c984fc
L
1014 /* For merging, we only care about real symbols. But we need to make
1015 sure that indirect symbol dynamic flags are updated. */
1016 hi = h;
45d6a902
AM
1017 while (h->root.type == bfd_link_hash_indirect
1018 || h->root.type == bfd_link_hash_warning)
1019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1020
6e33951e
L
1021 if (!*matched)
1022 {
1023 if (hi == h || h->root.type == bfd_link_hash_new)
1024 *matched = TRUE;
1025 else
1026 {
ae7683d2 1027 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1028 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1029 true if the new symbol is only visible to the symbol with
6e33951e 1030 the same symbol version. */
422f1182
L
1031 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1032 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1033 if (!old_hidden && !new_hidden)
1034 /* The new symbol matches the existing symbol if both
1035 aren't hidden. */
1036 *matched = TRUE;
1037 else
1038 {
1039 /* OLD_VERSION is the symbol version of the existing
1040 symbol. */
422f1182
L
1041 char *old_version;
1042
1043 if (h->versioned >= versioned)
1044 old_version = strrchr (h->root.root.string,
1045 ELF_VER_CHR) + 1;
1046 else
1047 old_version = NULL;
6e33951e
L
1048
1049 /* The new symbol matches the existing symbol if they
1050 have the same symbol version. */
1051 *matched = (old_version == new_version
1052 || (old_version != NULL
1053 && new_version != NULL
1054 && strcmp (old_version, new_version) == 0));
1055 }
1056 }
1057 }
1058
934bce08
AM
1059 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1060 existing symbol. */
1061
1062 oldbfd = NULL;
1063 oldsec = NULL;
1064 switch (h->root.type)
1065 {
1066 default:
1067 break;
1068
1069 case bfd_link_hash_undefined:
1070 case bfd_link_hash_undefweak:
1071 oldbfd = h->root.u.undef.abfd;
1072 break;
1073
1074 case bfd_link_hash_defined:
1075 case bfd_link_hash_defweak:
1076 oldbfd = h->root.u.def.section->owner;
1077 oldsec = h->root.u.def.section;
1078 break;
1079
1080 case bfd_link_hash_common:
1081 oldbfd = h->root.u.c.p->section->owner;
1082 oldsec = h->root.u.c.p->section;
1083 if (pold_alignment)
1084 *pold_alignment = h->root.u.c.p->alignment_power;
1085 break;
1086 }
1087 if (poldbfd && *poldbfd == NULL)
1088 *poldbfd = oldbfd;
1089
1090 /* Differentiate strong and weak symbols. */
1091 newweak = bind == STB_WEAK;
1092 oldweak = (h->root.type == bfd_link_hash_defweak
1093 || h->root.type == bfd_link_hash_undefweak);
1094 if (pold_weak)
1095 *pold_weak = oldweak;
1096
1097 /* This code is for coping with dynamic objects, and is only useful
1098 if we are doing an ELF link. */
1099 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1100 return TRUE;
1101
40b36307 1102 /* We have to check it for every instance since the first few may be
ee659f1f 1103 references and not all compilers emit symbol type for undefined
40b36307
L
1104 symbols. */
1105 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1106
ee659f1f
AM
1107 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1108 respectively, is from a dynamic object. */
1109
1110 newdyn = (abfd->flags & DYNAMIC) != 0;
1111
1112 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1113 syms and defined syms in dynamic libraries respectively.
1114 ref_dynamic on the other hand can be set for a symbol defined in
1115 a dynamic library, and def_dynamic may not be set; When the
1116 definition in a dynamic lib is overridden by a definition in the
1117 executable use of the symbol in the dynamic lib becomes a
1118 reference to the executable symbol. */
1119 if (newdyn)
1120 {
1121 if (bfd_is_und_section (sec))
1122 {
1123 if (bind != STB_WEAK)
1124 {
1125 h->ref_dynamic_nonweak = 1;
1126 hi->ref_dynamic_nonweak = 1;
1127 }
1128 }
1129 else
1130 {
6e33951e
L
1131 /* Update the existing symbol only if they match. */
1132 if (*matched)
1133 h->dynamic_def = 1;
ee659f1f
AM
1134 hi->dynamic_def = 1;
1135 }
1136 }
1137
45d6a902
AM
1138 /* If we just created the symbol, mark it as being an ELF symbol.
1139 Other than that, there is nothing to do--there is no merge issue
1140 with a newly defined symbol--so we just return. */
1141
1142 if (h->root.type == bfd_link_hash_new)
252b5132 1143 {
f5385ebf 1144 h->non_elf = 0;
45d6a902
AM
1145 return TRUE;
1146 }
252b5132 1147
45d6a902
AM
1148 /* In cases involving weak versioned symbols, we may wind up trying
1149 to merge a symbol with itself. Catch that here, to avoid the
1150 confusion that results if we try to override a symbol with
1151 itself. The additional tests catch cases like
1152 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1153 dynamic object, which we do want to handle here. */
1154 if (abfd == oldbfd
895fa45f 1155 && (newweak || oldweak)
45d6a902 1156 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1157 || !h->def_regular))
45d6a902
AM
1158 return TRUE;
1159
707bba77 1160 olddyn = FALSE;
45d6a902
AM
1161 if (oldbfd != NULL)
1162 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1163 else if (oldsec != NULL)
45d6a902 1164 {
707bba77 1165 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1166 indices used by MIPS ELF. */
707bba77 1167 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1168 }
252b5132 1169
45d6a902
AM
1170 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1171 respectively, appear to be a definition rather than reference. */
1172
707bba77 1173 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1174
707bba77
AM
1175 olddef = (h->root.type != bfd_link_hash_undefined
1176 && h->root.type != bfd_link_hash_undefweak
1177 && h->root.type != bfd_link_hash_common);
45d6a902 1178
0a36a439
L
1179 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1180 respectively, appear to be a function. */
1181
1182 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1183 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1184
1185 oldfunc = (h->type != STT_NOTYPE
1186 && bed->is_function_type (h->type));
1187
580a2b6e
L
1188 /* When we try to create a default indirect symbol from the dynamic
1189 definition with the default version, we skip it if its type and
40101021 1190 the type of existing regular definition mismatch. */
580a2b6e 1191 if (pold_alignment == NULL
580a2b6e
L
1192 && newdyn
1193 && newdef
1194 && !olddyn
4584ec12
L
1195 && (((olddef || h->root.type == bfd_link_hash_common)
1196 && ELF_ST_TYPE (sym->st_info) != h->type
1197 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1198 && h->type != STT_NOTYPE
1199 && !(newfunc && oldfunc))
1200 || (olddef
1201 && ((h->type == STT_GNU_IFUNC)
1202 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1203 {
1204 *skip = TRUE;
1205 return TRUE;
1206 }
1207
4c34aff8
AM
1208 /* Check TLS symbols. We don't check undefined symbols introduced
1209 by "ld -u" which have no type (and oldbfd NULL), and we don't
1210 check symbols from plugins because they also have no type. */
1211 if (oldbfd != NULL
1212 && (oldbfd->flags & BFD_PLUGIN) == 0
1213 && (abfd->flags & BFD_PLUGIN) == 0
1214 && ELF_ST_TYPE (sym->st_info) != h->type
1215 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1216 {
1217 bfd *ntbfd, *tbfd;
1218 bfd_boolean ntdef, tdef;
1219 asection *ntsec, *tsec;
1220
1221 if (h->type == STT_TLS)
1222 {
3b36f7e6 1223 ntbfd = abfd;
7479dfd4
L
1224 ntsec = sec;
1225 ntdef = newdef;
1226 tbfd = oldbfd;
1227 tsec = oldsec;
1228 tdef = olddef;
1229 }
1230 else
1231 {
1232 ntbfd = oldbfd;
1233 ntsec = oldsec;
1234 ntdef = olddef;
1235 tbfd = abfd;
1236 tsec = sec;
1237 tdef = newdef;
1238 }
1239
1240 if (tdef && ntdef)
1241 (*_bfd_error_handler)
191c0c42
AM
1242 (_("%s: TLS definition in %B section %A "
1243 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1244 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1245 else if (!tdef && !ntdef)
1246 (*_bfd_error_handler)
191c0c42
AM
1247 (_("%s: TLS reference in %B "
1248 "mismatches non-TLS reference in %B"),
7479dfd4
L
1249 tbfd, ntbfd, h->root.root.string);
1250 else if (tdef)
1251 (*_bfd_error_handler)
191c0c42
AM
1252 (_("%s: TLS definition in %B section %A "
1253 "mismatches non-TLS reference in %B"),
7479dfd4
L
1254 tbfd, tsec, ntbfd, h->root.root.string);
1255 else
1256 (*_bfd_error_handler)
191c0c42
AM
1257 (_("%s: TLS reference in %B "
1258 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1259 tbfd, ntbfd, ntsec, h->root.root.string);
1260
1261 bfd_set_error (bfd_error_bad_value);
1262 return FALSE;
1263 }
1264
45d6a902
AM
1265 /* If the old symbol has non-default visibility, we ignore the new
1266 definition from a dynamic object. */
1267 if (newdyn
9c7a29a3 1268 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1269 && !bfd_is_und_section (sec))
1270 {
1271 *skip = TRUE;
1272 /* Make sure this symbol is dynamic. */
f5385ebf 1273 h->ref_dynamic = 1;
90c984fc 1274 hi->ref_dynamic = 1;
45d6a902
AM
1275 /* A protected symbol has external availability. Make sure it is
1276 recorded as dynamic.
1277
1278 FIXME: Should we check type and size for protected symbol? */
1279 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1280 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1281 else
1282 return TRUE;
1283 }
1284 else if (!newdyn
9c7a29a3 1285 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1286 && h->def_dynamic)
45d6a902
AM
1287 {
1288 /* If the new symbol with non-default visibility comes from a
1289 relocatable file and the old definition comes from a dynamic
1290 object, we remove the old definition. */
6c9b78e6 1291 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1292 {
1293 /* Handle the case where the old dynamic definition is
1294 default versioned. We need to copy the symbol info from
1295 the symbol with default version to the normal one if it
1296 was referenced before. */
1297 if (h->ref_regular)
1298 {
6c9b78e6 1299 hi->root.type = h->root.type;
d2dee3b2 1300 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1301 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1302
6c9b78e6 1303 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1304 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1305 {
aed81c4e
MR
1306 /* If the new symbol is hidden or internal, completely undo
1307 any dynamic link state. */
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
d2dee3b2
L
1311 }
1312 else
aed81c4e
MR
1313 h->ref_dynamic = 1;
1314
1315 h->def_dynamic = 0;
aed81c4e
MR
1316 /* FIXME: Should we check type and size for protected symbol? */
1317 h->size = 0;
1318 h->type = 0;
1319
6c9b78e6 1320 h = hi;
d2dee3b2
L
1321 }
1322 else
6c9b78e6 1323 h = hi;
d2dee3b2 1324 }
1de1a317 1325
f5eda473
AM
1326 /* If the old symbol was undefined before, then it will still be
1327 on the undefs list. If the new symbol is undefined or
1328 common, we can't make it bfd_link_hash_new here, because new
1329 undefined or common symbols will be added to the undefs list
1330 by _bfd_generic_link_add_one_symbol. Symbols may not be
1331 added twice to the undefs list. Also, if the new symbol is
1332 undefweak then we don't want to lose the strong undef. */
1333 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1334 {
1de1a317 1335 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1336 h->root.u.undef.abfd = abfd;
1337 }
1338 else
1339 {
1340 h->root.type = bfd_link_hash_new;
1341 h->root.u.undef.abfd = NULL;
1342 }
1343
f5eda473 1344 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1345 {
f5eda473
AM
1346 /* If the new symbol is hidden or internal, completely undo
1347 any dynamic link state. */
1348 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1349 h->forced_local = 0;
1350 h->ref_dynamic = 0;
45d6a902 1351 }
f5eda473
AM
1352 else
1353 h->ref_dynamic = 1;
1354 h->def_dynamic = 0;
45d6a902
AM
1355 /* FIXME: Should we check type and size for protected symbol? */
1356 h->size = 0;
1357 h->type = 0;
1358 return TRUE;
1359 }
14a793b2 1360
15b43f48
AM
1361 /* If a new weak symbol definition comes from a regular file and the
1362 old symbol comes from a dynamic library, we treat the new one as
1363 strong. Similarly, an old weak symbol definition from a regular
1364 file is treated as strong when the new symbol comes from a dynamic
1365 library. Further, an old weak symbol from a dynamic library is
1366 treated as strong if the new symbol is from a dynamic library.
1367 This reflects the way glibc's ld.so works.
1368
1369 Do this before setting *type_change_ok or *size_change_ok so that
1370 we warn properly when dynamic library symbols are overridden. */
1371
1372 if (newdef && !newdyn && olddyn)
0f8a2703 1373 newweak = FALSE;
15b43f48 1374 if (olddef && newdyn)
0f8a2703
AM
1375 oldweak = FALSE;
1376
d334575b 1377 /* Allow changes between different types of function symbol. */
0a36a439 1378 if (newfunc && oldfunc)
fcb93ecf
PB
1379 *type_change_ok = TRUE;
1380
79349b09
AM
1381 /* It's OK to change the type if either the existing symbol or the
1382 new symbol is weak. A type change is also OK if the old symbol
1383 is undefined and the new symbol is defined. */
252b5132 1384
79349b09
AM
1385 if (oldweak
1386 || newweak
1387 || (newdef
1388 && h->root.type == bfd_link_hash_undefined))
1389 *type_change_ok = TRUE;
1390
1391 /* It's OK to change the size if either the existing symbol or the
1392 new symbol is weak, or if the old symbol is undefined. */
1393
1394 if (*type_change_ok
1395 || h->root.type == bfd_link_hash_undefined)
1396 *size_change_ok = TRUE;
45d6a902 1397
45d6a902
AM
1398 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1399 symbol, respectively, appears to be a common symbol in a dynamic
1400 object. If a symbol appears in an uninitialized section, and is
1401 not weak, and is not a function, then it may be a common symbol
1402 which was resolved when the dynamic object was created. We want
1403 to treat such symbols specially, because they raise special
1404 considerations when setting the symbol size: if the symbol
1405 appears as a common symbol in a regular object, and the size in
1406 the regular object is larger, we must make sure that we use the
1407 larger size. This problematic case can always be avoided in C,
1408 but it must be handled correctly when using Fortran shared
1409 libraries.
1410
1411 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1412 likewise for OLDDYNCOMMON and OLDDEF.
1413
1414 Note that this test is just a heuristic, and that it is quite
1415 possible to have an uninitialized symbol in a shared object which
1416 is really a definition, rather than a common symbol. This could
1417 lead to some minor confusion when the symbol really is a common
1418 symbol in some regular object. However, I think it will be
1419 harmless. */
1420
1421 if (newdyn
1422 && newdef
79349b09 1423 && !newweak
45d6a902
AM
1424 && (sec->flags & SEC_ALLOC) != 0
1425 && (sec->flags & SEC_LOAD) == 0
1426 && sym->st_size > 0
0a36a439 1427 && !newfunc)
45d6a902
AM
1428 newdyncommon = TRUE;
1429 else
1430 newdyncommon = FALSE;
1431
1432 if (olddyn
1433 && olddef
1434 && h->root.type == bfd_link_hash_defined
f5385ebf 1435 && h->def_dynamic
45d6a902
AM
1436 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1437 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1438 && h->size > 0
0a36a439 1439 && !oldfunc)
45d6a902
AM
1440 olddyncommon = TRUE;
1441 else
1442 olddyncommon = FALSE;
1443
a4d8e49b
L
1444 /* We now know everything about the old and new symbols. We ask the
1445 backend to check if we can merge them. */
5d13b3b3
AM
1446 if (bed->merge_symbol != NULL)
1447 {
1448 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1449 return FALSE;
1450 sec = *psec;
1451 }
a4d8e49b 1452
45d6a902
AM
1453 /* If both the old and the new symbols look like common symbols in a
1454 dynamic object, set the size of the symbol to the larger of the
1455 two. */
1456
1457 if (olddyncommon
1458 && newdyncommon
1459 && sym->st_size != h->size)
1460 {
1461 /* Since we think we have two common symbols, issue a multiple
1462 common warning if desired. Note that we only warn if the
1463 size is different. If the size is the same, we simply let
1464 the old symbol override the new one as normally happens with
1465 symbols defined in dynamic objects. */
1466
1467 if (! ((*info->callbacks->multiple_common)
24f58f47 1468 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1469 return FALSE;
252b5132 1470
45d6a902
AM
1471 if (sym->st_size > h->size)
1472 h->size = sym->st_size;
252b5132 1473
45d6a902 1474 *size_change_ok = TRUE;
252b5132
RH
1475 }
1476
45d6a902
AM
1477 /* If we are looking at a dynamic object, and we have found a
1478 definition, we need to see if the symbol was already defined by
1479 some other object. If so, we want to use the existing
1480 definition, and we do not want to report a multiple symbol
1481 definition error; we do this by clobbering *PSEC to be
1482 bfd_und_section_ptr.
1483
1484 We treat a common symbol as a definition if the symbol in the
1485 shared library is a function, since common symbols always
1486 represent variables; this can cause confusion in principle, but
1487 any such confusion would seem to indicate an erroneous program or
1488 shared library. We also permit a common symbol in a regular
79349b09 1489 object to override a weak symbol in a shared object. */
45d6a902
AM
1490
1491 if (newdyn
1492 && newdef
77cfaee6 1493 && (olddef
45d6a902 1494 || (h->root.type == bfd_link_hash_common
0a36a439 1495 && (newweak || newfunc))))
45d6a902
AM
1496 {
1497 *override = TRUE;
1498 newdef = FALSE;
1499 newdyncommon = FALSE;
252b5132 1500
45d6a902
AM
1501 *psec = sec = bfd_und_section_ptr;
1502 *size_change_ok = TRUE;
252b5132 1503
45d6a902
AM
1504 /* If we get here when the old symbol is a common symbol, then
1505 we are explicitly letting it override a weak symbol or
1506 function in a dynamic object, and we don't want to warn about
1507 a type change. If the old symbol is a defined symbol, a type
1508 change warning may still be appropriate. */
252b5132 1509
45d6a902
AM
1510 if (h->root.type == bfd_link_hash_common)
1511 *type_change_ok = TRUE;
1512 }
1513
1514 /* Handle the special case of an old common symbol merging with a
1515 new symbol which looks like a common symbol in a shared object.
1516 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1517 common symbol, and let _bfd_generic_link_add_one_symbol do the
1518 right thing. */
45d6a902
AM
1519
1520 if (newdyncommon
1521 && h->root.type == bfd_link_hash_common)
1522 {
1523 *override = TRUE;
1524 newdef = FALSE;
1525 newdyncommon = FALSE;
1526 *pvalue = sym->st_size;
a4d8e49b 1527 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1528 *size_change_ok = TRUE;
1529 }
1530
c5e2cead 1531 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1532 if (newdef && olddef && newweak)
54ac0771 1533 {
35ed3f94 1534 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1535 if (!(oldbfd != NULL
1536 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1537 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1538 {
1539 newdef = FALSE;
1540 *skip = TRUE;
1541 }
54ac0771
L
1542
1543 /* Merge st_other. If the symbol already has a dynamic index,
1544 but visibility says it should not be visible, turn it into a
1545 local symbol. */
b8417128 1546 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1547 if (h->dynindx != -1)
1548 switch (ELF_ST_VISIBILITY (h->other))
1549 {
1550 case STV_INTERNAL:
1551 case STV_HIDDEN:
1552 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1553 break;
1554 }
1555 }
c5e2cead 1556
45d6a902
AM
1557 /* If the old symbol is from a dynamic object, and the new symbol is
1558 a definition which is not from a dynamic object, then the new
1559 symbol overrides the old symbol. Symbols from regular files
1560 always take precedence over symbols from dynamic objects, even if
1561 they are defined after the dynamic object in the link.
1562
1563 As above, we again permit a common symbol in a regular object to
1564 override a definition in a shared object if the shared object
0f8a2703 1565 symbol is a function or is weak. */
45d6a902
AM
1566
1567 flip = NULL;
77cfaee6 1568 if (!newdyn
45d6a902
AM
1569 && (newdef
1570 || (bfd_is_com_section (sec)
0a36a439 1571 && (oldweak || oldfunc)))
45d6a902
AM
1572 && olddyn
1573 && olddef
f5385ebf 1574 && h->def_dynamic)
45d6a902
AM
1575 {
1576 /* Change the hash table entry to undefined, and let
1577 _bfd_generic_link_add_one_symbol do the right thing with the
1578 new definition. */
1579
1580 h->root.type = bfd_link_hash_undefined;
1581 h->root.u.undef.abfd = h->root.u.def.section->owner;
1582 *size_change_ok = TRUE;
1583
1584 olddef = FALSE;
1585 olddyncommon = FALSE;
1586
1587 /* We again permit a type change when a common symbol may be
1588 overriding a function. */
1589
1590 if (bfd_is_com_section (sec))
0a36a439
L
1591 {
1592 if (oldfunc)
1593 {
1594 /* If a common symbol overrides a function, make sure
1595 that it isn't defined dynamically nor has type
1596 function. */
1597 h->def_dynamic = 0;
1598 h->type = STT_NOTYPE;
1599 }
1600 *type_change_ok = TRUE;
1601 }
45d6a902 1602
6c9b78e6
AM
1603 if (hi->root.type == bfd_link_hash_indirect)
1604 flip = hi;
45d6a902
AM
1605 else
1606 /* This union may have been set to be non-NULL when this symbol
1607 was seen in a dynamic object. We must force the union to be
1608 NULL, so that it is correct for a regular symbol. */
1609 h->verinfo.vertree = NULL;
1610 }
1611
1612 /* Handle the special case of a new common symbol merging with an
1613 old symbol that looks like it might be a common symbol defined in
1614 a shared object. Note that we have already handled the case in
1615 which a new common symbol should simply override the definition
1616 in the shared library. */
1617
1618 if (! newdyn
1619 && bfd_is_com_section (sec)
1620 && olddyncommon)
1621 {
1622 /* It would be best if we could set the hash table entry to a
1623 common symbol, but we don't know what to use for the section
1624 or the alignment. */
1625 if (! ((*info->callbacks->multiple_common)
24f58f47 1626 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1627 return FALSE;
1628
4cc11e76 1629 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1630 larger, pretend that the new symbol has its size. */
1631
1632 if (h->size > *pvalue)
1633 *pvalue = h->size;
1634
af44c138
L
1635 /* We need to remember the alignment required by the symbol
1636 in the dynamic object. */
1637 BFD_ASSERT (pold_alignment);
1638 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1639
1640 olddef = FALSE;
1641 olddyncommon = FALSE;
1642
1643 h->root.type = bfd_link_hash_undefined;
1644 h->root.u.undef.abfd = h->root.u.def.section->owner;
1645
1646 *size_change_ok = TRUE;
1647 *type_change_ok = TRUE;
1648
6c9b78e6
AM
1649 if (hi->root.type == bfd_link_hash_indirect)
1650 flip = hi;
45d6a902
AM
1651 else
1652 h->verinfo.vertree = NULL;
1653 }
1654
1655 if (flip != NULL)
1656 {
1657 /* Handle the case where we had a versioned symbol in a dynamic
1658 library and now find a definition in a normal object. In this
1659 case, we make the versioned symbol point to the normal one. */
45d6a902 1660 flip->root.type = h->root.type;
00cbee0a 1661 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1662 h->root.type = bfd_link_hash_indirect;
1663 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1664 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1665 if (h->def_dynamic)
45d6a902 1666 {
f5385ebf
AM
1667 h->def_dynamic = 0;
1668 flip->ref_dynamic = 1;
45d6a902
AM
1669 }
1670 }
1671
45d6a902
AM
1672 return TRUE;
1673}
1674
1675/* This function is called to create an indirect symbol from the
1676 default for the symbol with the default version if needed. The
4f3fedcf 1677 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1678 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1679
28caa186 1680static bfd_boolean
268b6b39
AM
1681_bfd_elf_add_default_symbol (bfd *abfd,
1682 struct bfd_link_info *info,
1683 struct elf_link_hash_entry *h,
1684 const char *name,
1685 Elf_Internal_Sym *sym,
4f3fedcf
AM
1686 asection *sec,
1687 bfd_vma value,
1688 bfd **poldbfd,
e3c9d234 1689 bfd_boolean *dynsym)
45d6a902
AM
1690{
1691 bfd_boolean type_change_ok;
1692 bfd_boolean size_change_ok;
1693 bfd_boolean skip;
1694 char *shortname;
1695 struct elf_link_hash_entry *hi;
1696 struct bfd_link_hash_entry *bh;
9c5bfbb7 1697 const struct elf_backend_data *bed;
45d6a902
AM
1698 bfd_boolean collect;
1699 bfd_boolean dynamic;
e3c9d234 1700 bfd_boolean override;
45d6a902
AM
1701 char *p;
1702 size_t len, shortlen;
ffd65175 1703 asection *tmp_sec;
6e33951e 1704 bfd_boolean matched;
45d6a902 1705
422f1182
L
1706 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1707 return TRUE;
1708
45d6a902
AM
1709 /* If this symbol has a version, and it is the default version, we
1710 create an indirect symbol from the default name to the fully
1711 decorated name. This will cause external references which do not
1712 specify a version to be bound to this version of the symbol. */
1713 p = strchr (name, ELF_VER_CHR);
422f1182
L
1714 if (h->versioned == unknown)
1715 {
1716 if (p == NULL)
1717 {
1718 h->versioned = unversioned;
1719 return TRUE;
1720 }
1721 else
1722 {
1723 if (p[1] != ELF_VER_CHR)
1724 {
1725 h->versioned = versioned_hidden;
1726 return TRUE;
1727 }
1728 else
1729 h->versioned = versioned;
1730 }
1731 }
4373f8af
L
1732 else
1733 {
1734 /* PR ld/19073: We may see an unversioned definition after the
1735 default version. */
1736 if (p == NULL)
1737 return TRUE;
1738 }
45d6a902 1739
45d6a902
AM
1740 bed = get_elf_backend_data (abfd);
1741 collect = bed->collect;
1742 dynamic = (abfd->flags & DYNAMIC) != 0;
1743
1744 shortlen = p - name;
a50b1753 1745 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1746 if (shortname == NULL)
1747 return FALSE;
1748 memcpy (shortname, name, shortlen);
1749 shortname[shortlen] = '\0';
1750
1751 /* We are going to create a new symbol. Merge it with any existing
1752 symbol with this name. For the purposes of the merge, act as
1753 though we were defining the symbol we just defined, although we
1754 actually going to define an indirect symbol. */
1755 type_change_ok = FALSE;
1756 size_change_ok = FALSE;
6e33951e 1757 matched = TRUE;
ffd65175
AM
1758 tmp_sec = sec;
1759 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1760 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1761 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1762 return FALSE;
1763
1764 if (skip)
1765 goto nondefault;
1766
1767 if (! override)
1768 {
c6e8a9a8 1769 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1770 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1771 {
1772 bh = &hi->root;
1773 if (! (_bfd_generic_link_add_one_symbol
1774 (info, abfd, shortname, BSF_INDIRECT,
1775 bfd_ind_section_ptr,
1776 0, name, FALSE, collect, &bh)))
1777 return FALSE;
1778 hi = (struct elf_link_hash_entry *) bh;
1779 }
45d6a902
AM
1780 }
1781 else
1782 {
1783 /* In this case the symbol named SHORTNAME is overriding the
1784 indirect symbol we want to add. We were planning on making
1785 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1786 is the name without a version. NAME is the fully versioned
1787 name, and it is the default version.
1788
1789 Overriding means that we already saw a definition for the
1790 symbol SHORTNAME in a regular object, and it is overriding
1791 the symbol defined in the dynamic object.
1792
1793 When this happens, we actually want to change NAME, the
1794 symbol we just added, to refer to SHORTNAME. This will cause
1795 references to NAME in the shared object to become references
1796 to SHORTNAME in the regular object. This is what we expect
1797 when we override a function in a shared object: that the
1798 references in the shared object will be mapped to the
1799 definition in the regular object. */
1800
1801 while (hi->root.type == bfd_link_hash_indirect
1802 || hi->root.type == bfd_link_hash_warning)
1803 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1804
1805 h->root.type = bfd_link_hash_indirect;
1806 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1807 if (h->def_dynamic)
45d6a902 1808 {
f5385ebf
AM
1809 h->def_dynamic = 0;
1810 hi->ref_dynamic = 1;
1811 if (hi->ref_regular
1812 || hi->def_regular)
45d6a902 1813 {
c152c796 1814 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1815 return FALSE;
1816 }
1817 }
1818
1819 /* Now set HI to H, so that the following code will set the
1820 other fields correctly. */
1821 hi = h;
1822 }
1823
fab4a87f
L
1824 /* Check if HI is a warning symbol. */
1825 if (hi->root.type == bfd_link_hash_warning)
1826 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1827
45d6a902
AM
1828 /* If there is a duplicate definition somewhere, then HI may not
1829 point to an indirect symbol. We will have reported an error to
1830 the user in that case. */
1831
1832 if (hi->root.type == bfd_link_hash_indirect)
1833 {
1834 struct elf_link_hash_entry *ht;
1835
45d6a902 1836 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1837 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1838
68c88cd4
AM
1839 /* A reference to the SHORTNAME symbol from a dynamic library
1840 will be satisfied by the versioned symbol at runtime. In
1841 effect, we have a reference to the versioned symbol. */
1842 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1843 hi->dynamic_def |= ht->dynamic_def;
1844
45d6a902
AM
1845 /* See if the new flags lead us to realize that the symbol must
1846 be dynamic. */
1847 if (! *dynsym)
1848 {
1849 if (! dynamic)
1850 {
0e1862bb 1851 if (! bfd_link_executable (info)
90c984fc 1852 || hi->def_dynamic
f5385ebf 1853 || hi->ref_dynamic)
45d6a902
AM
1854 *dynsym = TRUE;
1855 }
1856 else
1857 {
f5385ebf 1858 if (hi->ref_regular)
45d6a902
AM
1859 *dynsym = TRUE;
1860 }
1861 }
1862 }
1863
1864 /* We also need to define an indirection from the nondefault version
1865 of the symbol. */
1866
1867nondefault:
1868 len = strlen (name);
a50b1753 1869 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1870 if (shortname == NULL)
1871 return FALSE;
1872 memcpy (shortname, name, shortlen);
1873 memcpy (shortname + shortlen, p + 1, len - shortlen);
1874
1875 /* Once again, merge with any existing symbol. */
1876 type_change_ok = FALSE;
1877 size_change_ok = FALSE;
ffd65175
AM
1878 tmp_sec = sec;
1879 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1880 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1881 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1882 return FALSE;
1883
1884 if (skip)
1885 return TRUE;
1886
1887 if (override)
1888 {
1889 /* Here SHORTNAME is a versioned name, so we don't expect to see
1890 the type of override we do in the case above unless it is
4cc11e76 1891 overridden by a versioned definition. */
45d6a902
AM
1892 if (hi->root.type != bfd_link_hash_defined
1893 && hi->root.type != bfd_link_hash_defweak)
1894 (*_bfd_error_handler)
d003868e
AM
1895 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1896 abfd, shortname);
45d6a902
AM
1897 }
1898 else
1899 {
1900 bh = &hi->root;
1901 if (! (_bfd_generic_link_add_one_symbol
1902 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1903 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1904 return FALSE;
1905 hi = (struct elf_link_hash_entry *) bh;
1906
1907 /* If there is a duplicate definition somewhere, then HI may not
1908 point to an indirect symbol. We will have reported an error
1909 to the user in that case. */
1910
1911 if (hi->root.type == bfd_link_hash_indirect)
1912 {
fcfa13d2 1913 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1914 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1915 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1916
1917 /* See if the new flags lead us to realize that the symbol
1918 must be dynamic. */
1919 if (! *dynsym)
1920 {
1921 if (! dynamic)
1922 {
0e1862bb 1923 if (! bfd_link_executable (info)
f5385ebf 1924 || hi->ref_dynamic)
45d6a902
AM
1925 *dynsym = TRUE;
1926 }
1927 else
1928 {
f5385ebf 1929 if (hi->ref_regular)
45d6a902
AM
1930 *dynsym = TRUE;
1931 }
1932 }
1933 }
1934 }
1935
1936 return TRUE;
1937}
1938\f
1939/* This routine is used to export all defined symbols into the dynamic
1940 symbol table. It is called via elf_link_hash_traverse. */
1941
28caa186 1942static bfd_boolean
268b6b39 1943_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1944{
a50b1753 1945 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1946
1947 /* Ignore indirect symbols. These are added by the versioning code. */
1948 if (h->root.type == bfd_link_hash_indirect)
1949 return TRUE;
1950
7686d77d
AM
1951 /* Ignore this if we won't export it. */
1952 if (!eif->info->export_dynamic && !h->dynamic)
1953 return TRUE;
45d6a902
AM
1954
1955 if (h->dynindx == -1
fd91d419
L
1956 && (h->def_regular || h->ref_regular)
1957 && ! bfd_hide_sym_by_version (eif->info->version_info,
1958 h->root.root.string))
45d6a902 1959 {
fd91d419 1960 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1961 {
fd91d419
L
1962 eif->failed = TRUE;
1963 return FALSE;
45d6a902
AM
1964 }
1965 }
1966
1967 return TRUE;
1968}
1969\f
1970/* Look through the symbols which are defined in other shared
1971 libraries and referenced here. Update the list of version
1972 dependencies. This will be put into the .gnu.version_r section.
1973 This function is called via elf_link_hash_traverse. */
1974
28caa186 1975static bfd_boolean
268b6b39
AM
1976_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1977 void *data)
45d6a902 1978{
a50b1753 1979 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1980 Elf_Internal_Verneed *t;
1981 Elf_Internal_Vernaux *a;
1982 bfd_size_type amt;
1983
45d6a902
AM
1984 /* We only care about symbols defined in shared objects with version
1985 information. */
f5385ebf
AM
1986 if (!h->def_dynamic
1987 || h->def_regular
45d6a902 1988 || h->dynindx == -1
7b20f099
AM
1989 || h->verinfo.verdef == NULL
1990 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1991 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
1992 return TRUE;
1993
1994 /* See if we already know about this version. */
28caa186
AM
1995 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1996 t != NULL;
1997 t = t->vn_nextref)
45d6a902
AM
1998 {
1999 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2000 continue;
2001
2002 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2003 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2004 return TRUE;
2005
2006 break;
2007 }
2008
2009 /* This is a new version. Add it to tree we are building. */
2010
2011 if (t == NULL)
2012 {
2013 amt = sizeof *t;
a50b1753 2014 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2015 if (t == NULL)
2016 {
2017 rinfo->failed = TRUE;
2018 return FALSE;
2019 }
2020
2021 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2022 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2023 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2024 }
2025
2026 amt = sizeof *a;
a50b1753 2027 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2028 if (a == NULL)
2029 {
2030 rinfo->failed = TRUE;
2031 return FALSE;
2032 }
45d6a902
AM
2033
2034 /* Note that we are copying a string pointer here, and testing it
2035 above. If bfd_elf_string_from_elf_section is ever changed to
2036 discard the string data when low in memory, this will have to be
2037 fixed. */
2038 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2039
2040 a->vna_flags = h->verinfo.verdef->vd_flags;
2041 a->vna_nextptr = t->vn_auxptr;
2042
2043 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2044 ++rinfo->vers;
2045
2046 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2047
2048 t->vn_auxptr = a;
2049
2050 return TRUE;
2051}
2052
2053/* Figure out appropriate versions for all the symbols. We may not
2054 have the version number script until we have read all of the input
2055 files, so until that point we don't know which symbols should be
2056 local. This function is called via elf_link_hash_traverse. */
2057
28caa186 2058static bfd_boolean
268b6b39 2059_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2060{
28caa186 2061 struct elf_info_failed *sinfo;
45d6a902 2062 struct bfd_link_info *info;
9c5bfbb7 2063 const struct elf_backend_data *bed;
45d6a902
AM
2064 struct elf_info_failed eif;
2065 char *p;
2066 bfd_size_type amt;
2067
a50b1753 2068 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2069 info = sinfo->info;
2070
45d6a902
AM
2071 /* Fix the symbol flags. */
2072 eif.failed = FALSE;
2073 eif.info = info;
2074 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2075 {
2076 if (eif.failed)
2077 sinfo->failed = TRUE;
2078 return FALSE;
2079 }
2080
2081 /* We only need version numbers for symbols defined in regular
2082 objects. */
f5385ebf 2083 if (!h->def_regular)
45d6a902
AM
2084 return TRUE;
2085
28caa186 2086 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2087 p = strchr (h->root.root.string, ELF_VER_CHR);
2088 if (p != NULL && h->verinfo.vertree == NULL)
2089 {
2090 struct bfd_elf_version_tree *t;
45d6a902 2091
45d6a902
AM
2092 ++p;
2093 if (*p == ELF_VER_CHR)
6e33951e 2094 ++p;
45d6a902
AM
2095
2096 /* If there is no version string, we can just return out. */
2097 if (*p == '\0')
6e33951e 2098 return TRUE;
45d6a902
AM
2099
2100 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2101 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2102 {
2103 if (strcmp (t->name, p) == 0)
2104 {
2105 size_t len;
2106 char *alc;
2107 struct bfd_elf_version_expr *d;
2108
2109 len = p - h->root.root.string;
a50b1753 2110 alc = (char *) bfd_malloc (len);
45d6a902 2111 if (alc == NULL)
14b1c01e
AM
2112 {
2113 sinfo->failed = TRUE;
2114 return FALSE;
2115 }
45d6a902
AM
2116 memcpy (alc, h->root.root.string, len - 1);
2117 alc[len - 1] = '\0';
2118 if (alc[len - 2] == ELF_VER_CHR)
2119 alc[len - 2] = '\0';
2120
2121 h->verinfo.vertree = t;
2122 t->used = TRUE;
2123 d = NULL;
2124
108ba305
JJ
2125 if (t->globals.list != NULL)
2126 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2127
2128 /* See if there is anything to force this symbol to
2129 local scope. */
108ba305 2130 if (d == NULL && t->locals.list != NULL)
45d6a902 2131 {
108ba305
JJ
2132 d = (*t->match) (&t->locals, NULL, alc);
2133 if (d != NULL
2134 && h->dynindx != -1
108ba305
JJ
2135 && ! info->export_dynamic)
2136 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2137 }
2138
2139 free (alc);
2140 break;
2141 }
2142 }
2143
2144 /* If we are building an application, we need to create a
2145 version node for this version. */
0e1862bb 2146 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2147 {
2148 struct bfd_elf_version_tree **pp;
2149 int version_index;
2150
2151 /* If we aren't going to export this symbol, we don't need
2152 to worry about it. */
2153 if (h->dynindx == -1)
2154 return TRUE;
2155
2156 amt = sizeof *t;
a50b1753 2157 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2158 if (t == NULL)
2159 {
2160 sinfo->failed = TRUE;
2161 return FALSE;
2162 }
2163
45d6a902 2164 t->name = p;
45d6a902
AM
2165 t->name_indx = (unsigned int) -1;
2166 t->used = TRUE;
2167
2168 version_index = 1;
2169 /* Don't count anonymous version tag. */
fd91d419
L
2170 if (sinfo->info->version_info != NULL
2171 && sinfo->info->version_info->vernum == 0)
45d6a902 2172 version_index = 0;
fd91d419
L
2173 for (pp = &sinfo->info->version_info;
2174 *pp != NULL;
2175 pp = &(*pp)->next)
45d6a902
AM
2176 ++version_index;
2177 t->vernum = version_index;
2178
2179 *pp = t;
2180
2181 h->verinfo.vertree = t;
2182 }
2183 else if (t == NULL)
2184 {
2185 /* We could not find the version for a symbol when
2186 generating a shared archive. Return an error. */
2187 (*_bfd_error_handler)
c55fe096 2188 (_("%B: version node not found for symbol %s"),
28caa186 2189 info->output_bfd, h->root.root.string);
45d6a902
AM
2190 bfd_set_error (bfd_error_bad_value);
2191 sinfo->failed = TRUE;
2192 return FALSE;
2193 }
45d6a902
AM
2194 }
2195
2196 /* If we don't have a version for this symbol, see if we can find
2197 something. */
fd91d419 2198 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2199 {
1e8fa21e 2200 bfd_boolean hide;
ae5a3597 2201
fd91d419
L
2202 h->verinfo.vertree
2203 = bfd_find_version_for_sym (sinfo->info->version_info,
2204 h->root.root.string, &hide);
1e8fa21e
AM
2205 if (h->verinfo.vertree != NULL && hide)
2206 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2207 }
2208
2209 return TRUE;
2210}
2211\f
45d6a902
AM
2212/* Read and swap the relocs from the section indicated by SHDR. This
2213 may be either a REL or a RELA section. The relocations are
2214 translated into RELA relocations and stored in INTERNAL_RELOCS,
2215 which should have already been allocated to contain enough space.
2216 The EXTERNAL_RELOCS are a buffer where the external form of the
2217 relocations should be stored.
2218
2219 Returns FALSE if something goes wrong. */
2220
2221static bfd_boolean
268b6b39 2222elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2223 asection *sec,
268b6b39
AM
2224 Elf_Internal_Shdr *shdr,
2225 void *external_relocs,
2226 Elf_Internal_Rela *internal_relocs)
45d6a902 2227{
9c5bfbb7 2228 const struct elf_backend_data *bed;
268b6b39 2229 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2230 const bfd_byte *erela;
2231 const bfd_byte *erelaend;
2232 Elf_Internal_Rela *irela;
243ef1e0
L
2233 Elf_Internal_Shdr *symtab_hdr;
2234 size_t nsyms;
45d6a902 2235
45d6a902
AM
2236 /* Position ourselves at the start of the section. */
2237 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2238 return FALSE;
2239
2240 /* Read the relocations. */
2241 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2242 return FALSE;
2243
243ef1e0 2244 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2245 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2246
45d6a902
AM
2247 bed = get_elf_backend_data (abfd);
2248
2249 /* Convert the external relocations to the internal format. */
2250 if (shdr->sh_entsize == bed->s->sizeof_rel)
2251 swap_in = bed->s->swap_reloc_in;
2252 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2253 swap_in = bed->s->swap_reloca_in;
2254 else
2255 {
2256 bfd_set_error (bfd_error_wrong_format);
2257 return FALSE;
2258 }
2259
a50b1753 2260 erela = (const bfd_byte *) external_relocs;
51992aec 2261 erelaend = erela + shdr->sh_size;
45d6a902
AM
2262 irela = internal_relocs;
2263 while (erela < erelaend)
2264 {
243ef1e0
L
2265 bfd_vma r_symndx;
2266
45d6a902 2267 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2268 r_symndx = ELF32_R_SYM (irela->r_info);
2269 if (bed->s->arch_size == 64)
2270 r_symndx >>= 24;
ce98a316
NC
2271 if (nsyms > 0)
2272 {
2273 if ((size_t) r_symndx >= nsyms)
2274 {
2275 (*_bfd_error_handler)
2276 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2277 " for offset 0x%lx in section `%A'"),
2278 abfd, sec,
2279 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2280 bfd_set_error (bfd_error_bad_value);
2281 return FALSE;
2282 }
2283 }
cf35638d 2284 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2285 {
2286 (*_bfd_error_handler)
ce98a316
NC
2287 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2288 " when the object file has no symbol table"),
d003868e
AM
2289 abfd, sec,
2290 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2291 bfd_set_error (bfd_error_bad_value);
2292 return FALSE;
2293 }
45d6a902
AM
2294 irela += bed->s->int_rels_per_ext_rel;
2295 erela += shdr->sh_entsize;
2296 }
2297
2298 return TRUE;
2299}
2300
2301/* Read and swap the relocs for a section O. They may have been
2302 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2303 not NULL, they are used as buffers to read into. They are known to
2304 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2305 the return value is allocated using either malloc or bfd_alloc,
2306 according to the KEEP_MEMORY argument. If O has two relocation
2307 sections (both REL and RELA relocations), then the REL_HDR
2308 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2309 RELA_HDR relocations. */
45d6a902
AM
2310
2311Elf_Internal_Rela *
268b6b39
AM
2312_bfd_elf_link_read_relocs (bfd *abfd,
2313 asection *o,
2314 void *external_relocs,
2315 Elf_Internal_Rela *internal_relocs,
2316 bfd_boolean keep_memory)
45d6a902 2317{
268b6b39 2318 void *alloc1 = NULL;
45d6a902 2319 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2320 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2321 struct bfd_elf_section_data *esdo = elf_section_data (o);
2322 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2323
d4730f92
BS
2324 if (esdo->relocs != NULL)
2325 return esdo->relocs;
45d6a902
AM
2326
2327 if (o->reloc_count == 0)
2328 return NULL;
2329
45d6a902
AM
2330 if (internal_relocs == NULL)
2331 {
2332 bfd_size_type size;
2333
2334 size = o->reloc_count;
2335 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2336 if (keep_memory)
a50b1753 2337 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2338 else
a50b1753 2339 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2340 if (internal_relocs == NULL)
2341 goto error_return;
2342 }
2343
2344 if (external_relocs == NULL)
2345 {
d4730f92
BS
2346 bfd_size_type size = 0;
2347
2348 if (esdo->rel.hdr)
2349 size += esdo->rel.hdr->sh_size;
2350 if (esdo->rela.hdr)
2351 size += esdo->rela.hdr->sh_size;
45d6a902 2352
268b6b39 2353 alloc1 = bfd_malloc (size);
45d6a902
AM
2354 if (alloc1 == NULL)
2355 goto error_return;
2356 external_relocs = alloc1;
2357 }
2358
d4730f92
BS
2359 internal_rela_relocs = internal_relocs;
2360 if (esdo->rel.hdr)
2361 {
2362 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2363 external_relocs,
2364 internal_relocs))
2365 goto error_return;
2366 external_relocs = (((bfd_byte *) external_relocs)
2367 + esdo->rel.hdr->sh_size);
2368 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2369 * bed->s->int_rels_per_ext_rel);
2370 }
2371
2372 if (esdo->rela.hdr
2373 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2374 external_relocs,
2375 internal_rela_relocs)))
45d6a902
AM
2376 goto error_return;
2377
2378 /* Cache the results for next time, if we can. */
2379 if (keep_memory)
d4730f92 2380 esdo->relocs = internal_relocs;
45d6a902
AM
2381
2382 if (alloc1 != NULL)
2383 free (alloc1);
2384
2385 /* Don't free alloc2, since if it was allocated we are passing it
2386 back (under the name of internal_relocs). */
2387
2388 return internal_relocs;
2389
2390 error_return:
2391 if (alloc1 != NULL)
2392 free (alloc1);
2393 if (alloc2 != NULL)
4dd07732
AM
2394 {
2395 if (keep_memory)
2396 bfd_release (abfd, alloc2);
2397 else
2398 free (alloc2);
2399 }
45d6a902
AM
2400 return NULL;
2401}
2402
2403/* Compute the size of, and allocate space for, REL_HDR which is the
2404 section header for a section containing relocations for O. */
2405
28caa186 2406static bfd_boolean
268b6b39 2407_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2408 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2409{
d4730f92 2410 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2411
2412 /* That allows us to calculate the size of the section. */
d4730f92 2413 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2414
2415 /* The contents field must last into write_object_contents, so we
2416 allocate it with bfd_alloc rather than malloc. Also since we
2417 cannot be sure that the contents will actually be filled in,
2418 we zero the allocated space. */
a50b1753 2419 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2420 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2421 return FALSE;
2422
d4730f92 2423 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2424 {
2425 struct elf_link_hash_entry **p;
2426
ca4be51c
AM
2427 p = ((struct elf_link_hash_entry **)
2428 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2429 if (p == NULL)
2430 return FALSE;
2431
d4730f92 2432 reldata->hashes = p;
45d6a902
AM
2433 }
2434
2435 return TRUE;
2436}
2437
2438/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2439 originated from the section given by INPUT_REL_HDR) to the
2440 OUTPUT_BFD. */
2441
2442bfd_boolean
268b6b39
AM
2443_bfd_elf_link_output_relocs (bfd *output_bfd,
2444 asection *input_section,
2445 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2446 Elf_Internal_Rela *internal_relocs,
2447 struct elf_link_hash_entry **rel_hash
2448 ATTRIBUTE_UNUSED)
45d6a902
AM
2449{
2450 Elf_Internal_Rela *irela;
2451 Elf_Internal_Rela *irelaend;
2452 bfd_byte *erel;
d4730f92 2453 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2454 asection *output_section;
9c5bfbb7 2455 const struct elf_backend_data *bed;
268b6b39 2456 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2457 struct bfd_elf_section_data *esdo;
45d6a902
AM
2458
2459 output_section = input_section->output_section;
45d6a902 2460
d4730f92
BS
2461 bed = get_elf_backend_data (output_bfd);
2462 esdo = elf_section_data (output_section);
2463 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2464 {
d4730f92
BS
2465 output_reldata = &esdo->rel;
2466 swap_out = bed->s->swap_reloc_out;
45d6a902 2467 }
d4730f92
BS
2468 else if (esdo->rela.hdr
2469 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2470 {
d4730f92
BS
2471 output_reldata = &esdo->rela;
2472 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2473 }
2474 else
2475 {
2476 (*_bfd_error_handler)
d003868e
AM
2477 (_("%B: relocation size mismatch in %B section %A"),
2478 output_bfd, input_section->owner, input_section);
297d8443 2479 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2480 return FALSE;
2481 }
2482
d4730f92
BS
2483 erel = output_reldata->hdr->contents;
2484 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2485 irela = internal_relocs;
2486 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2487 * bed->s->int_rels_per_ext_rel);
2488 while (irela < irelaend)
2489 {
2490 (*swap_out) (output_bfd, irela, erel);
2491 irela += bed->s->int_rels_per_ext_rel;
2492 erel += input_rel_hdr->sh_entsize;
2493 }
2494
2495 /* Bump the counter, so that we know where to add the next set of
2496 relocations. */
d4730f92 2497 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2498
2499 return TRUE;
2500}
2501\f
508c3946
L
2502/* Make weak undefined symbols in PIE dynamic. */
2503
2504bfd_boolean
2505_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2506 struct elf_link_hash_entry *h)
2507{
0e1862bb 2508 if (bfd_link_pie (info)
508c3946
L
2509 && h->dynindx == -1
2510 && h->root.type == bfd_link_hash_undefweak)
2511 return bfd_elf_link_record_dynamic_symbol (info, h);
2512
2513 return TRUE;
2514}
2515
45d6a902
AM
2516/* Fix up the flags for a symbol. This handles various cases which
2517 can only be fixed after all the input files are seen. This is
2518 currently called by both adjust_dynamic_symbol and
2519 assign_sym_version, which is unnecessary but perhaps more robust in
2520 the face of future changes. */
2521
28caa186 2522static bfd_boolean
268b6b39
AM
2523_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2524 struct elf_info_failed *eif)
45d6a902 2525{
33774f08 2526 const struct elf_backend_data *bed;
508c3946 2527
45d6a902
AM
2528 /* If this symbol was mentioned in a non-ELF file, try to set
2529 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2530 permit a non-ELF file to correctly refer to a symbol defined in
2531 an ELF dynamic object. */
f5385ebf 2532 if (h->non_elf)
45d6a902
AM
2533 {
2534 while (h->root.type == bfd_link_hash_indirect)
2535 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2536
2537 if (h->root.type != bfd_link_hash_defined
2538 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2539 {
2540 h->ref_regular = 1;
2541 h->ref_regular_nonweak = 1;
2542 }
45d6a902
AM
2543 else
2544 {
2545 if (h->root.u.def.section->owner != NULL
2546 && (bfd_get_flavour (h->root.u.def.section->owner)
2547 == bfd_target_elf_flavour))
f5385ebf
AM
2548 {
2549 h->ref_regular = 1;
2550 h->ref_regular_nonweak = 1;
2551 }
45d6a902 2552 else
f5385ebf 2553 h->def_regular = 1;
45d6a902
AM
2554 }
2555
2556 if (h->dynindx == -1
f5385ebf
AM
2557 && (h->def_dynamic
2558 || h->ref_dynamic))
45d6a902 2559 {
c152c796 2560 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2561 {
2562 eif->failed = TRUE;
2563 return FALSE;
2564 }
2565 }
2566 }
2567 else
2568 {
f5385ebf 2569 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2570 was first seen in a non-ELF file. Fortunately, if the symbol
2571 was first seen in an ELF file, we're probably OK unless the
2572 symbol was defined in a non-ELF file. Catch that case here.
2573 FIXME: We're still in trouble if the symbol was first seen in
2574 a dynamic object, and then later in a non-ELF regular object. */
2575 if ((h->root.type == bfd_link_hash_defined
2576 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2577 && !h->def_regular
45d6a902
AM
2578 && (h->root.u.def.section->owner != NULL
2579 ? (bfd_get_flavour (h->root.u.def.section->owner)
2580 != bfd_target_elf_flavour)
2581 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2582 && !h->def_dynamic)))
2583 h->def_regular = 1;
45d6a902
AM
2584 }
2585
508c3946 2586 /* Backend specific symbol fixup. */
33774f08
AM
2587 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2588 if (bed->elf_backend_fixup_symbol
2589 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2590 return FALSE;
508c3946 2591
45d6a902
AM
2592 /* If this is a final link, and the symbol was defined as a common
2593 symbol in a regular object file, and there was no definition in
2594 any dynamic object, then the linker will have allocated space for
f5385ebf 2595 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2596 flag will not have been set. */
2597 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2598 && !h->def_regular
2599 && h->ref_regular
2600 && !h->def_dynamic
96f29d96 2601 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2602 h->def_regular = 1;
45d6a902
AM
2603
2604 /* If -Bsymbolic was used (which means to bind references to global
2605 symbols to the definition within the shared object), and this
2606 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2607 need a PLT entry. Likewise, if the symbol has non-default
2608 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2609 will force it local. */
f5385ebf 2610 if (h->needs_plt
0e1862bb 2611 && bfd_link_pic (eif->info)
0eddce27 2612 && is_elf_hash_table (eif->info->hash)
55255dae 2613 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2614 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2615 && h->def_regular)
45d6a902 2616 {
45d6a902
AM
2617 bfd_boolean force_local;
2618
45d6a902
AM
2619 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2620 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2621 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2622 }
2623
2624 /* If a weak undefined symbol has non-default visibility, we also
2625 hide it from the dynamic linker. */
9c7a29a3 2626 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2627 && h->root.type == bfd_link_hash_undefweak)
33774f08 2628 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2629
2630 /* If this is a weak defined symbol in a dynamic object, and we know
2631 the real definition in the dynamic object, copy interesting flags
2632 over to the real definition. */
f6e332e6 2633 if (h->u.weakdef != NULL)
45d6a902 2634 {
45d6a902
AM
2635 /* If the real definition is defined by a regular object file,
2636 don't do anything special. See the longer description in
2637 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2638 if (h->u.weakdef->def_regular)
f6e332e6 2639 h->u.weakdef = NULL;
45d6a902 2640 else
a26587ba 2641 {
4e6b54a6
AM
2642 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2643
2644 while (h->root.type == bfd_link_hash_indirect)
2645 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2646
2647 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2648 || h->root.type == bfd_link_hash_defweak);
2649 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2650 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2651 || weakdef->root.type == bfd_link_hash_defweak);
2652 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2653 }
45d6a902
AM
2654 }
2655
2656 return TRUE;
2657}
2658
2659/* Make the backend pick a good value for a dynamic symbol. This is
2660 called via elf_link_hash_traverse, and also calls itself
2661 recursively. */
2662
28caa186 2663static bfd_boolean
268b6b39 2664_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2665{
a50b1753 2666 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2667 bfd *dynobj;
9c5bfbb7 2668 const struct elf_backend_data *bed;
45d6a902 2669
0eddce27 2670 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2671 return FALSE;
2672
45d6a902
AM
2673 /* Ignore indirect symbols. These are added by the versioning code. */
2674 if (h->root.type == bfd_link_hash_indirect)
2675 return TRUE;
2676
2677 /* Fix the symbol flags. */
2678 if (! _bfd_elf_fix_symbol_flags (h, eif))
2679 return FALSE;
2680
2681 /* If this symbol does not require a PLT entry, and it is not
2682 defined by a dynamic object, or is not referenced by a regular
2683 object, ignore it. We do have to handle a weak defined symbol,
2684 even if no regular object refers to it, if we decided to add it
2685 to the dynamic symbol table. FIXME: Do we normally need to worry
2686 about symbols which are defined by one dynamic object and
2687 referenced by another one? */
f5385ebf 2688 if (!h->needs_plt
91e21fb7 2689 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2690 && (h->def_regular
2691 || !h->def_dynamic
2692 || (!h->ref_regular
f6e332e6 2693 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2694 {
a6aa5195 2695 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2696 return TRUE;
2697 }
2698
2699 /* If we've already adjusted this symbol, don't do it again. This
2700 can happen via a recursive call. */
f5385ebf 2701 if (h->dynamic_adjusted)
45d6a902
AM
2702 return TRUE;
2703
2704 /* Don't look at this symbol again. Note that we must set this
2705 after checking the above conditions, because we may look at a
2706 symbol once, decide not to do anything, and then get called
2707 recursively later after REF_REGULAR is set below. */
f5385ebf 2708 h->dynamic_adjusted = 1;
45d6a902
AM
2709
2710 /* If this is a weak definition, and we know a real definition, and
2711 the real symbol is not itself defined by a regular object file,
2712 then get a good value for the real definition. We handle the
2713 real symbol first, for the convenience of the backend routine.
2714
2715 Note that there is a confusing case here. If the real definition
2716 is defined by a regular object file, we don't get the real symbol
2717 from the dynamic object, but we do get the weak symbol. If the
2718 processor backend uses a COPY reloc, then if some routine in the
2719 dynamic object changes the real symbol, we will not see that
2720 change in the corresponding weak symbol. This is the way other
2721 ELF linkers work as well, and seems to be a result of the shared
2722 library model.
2723
2724 I will clarify this issue. Most SVR4 shared libraries define the
2725 variable _timezone and define timezone as a weak synonym. The
2726 tzset call changes _timezone. If you write
2727 extern int timezone;
2728 int _timezone = 5;
2729 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2730 you might expect that, since timezone is a synonym for _timezone,
2731 the same number will print both times. However, if the processor
2732 backend uses a COPY reloc, then actually timezone will be copied
2733 into your process image, and, since you define _timezone
2734 yourself, _timezone will not. Thus timezone and _timezone will
2735 wind up at different memory locations. The tzset call will set
2736 _timezone, leaving timezone unchanged. */
2737
f6e332e6 2738 if (h->u.weakdef != NULL)
45d6a902 2739 {
ec24dc88
AM
2740 /* If we get to this point, there is an implicit reference to
2741 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2742 h->u.weakdef->ref_regular = 1;
45d6a902 2743
ec24dc88
AM
2744 /* Ensure that the backend adjust_dynamic_symbol function sees
2745 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2746 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2747 return FALSE;
2748 }
2749
2750 /* If a symbol has no type and no size and does not require a PLT
2751 entry, then we are probably about to do the wrong thing here: we
2752 are probably going to create a COPY reloc for an empty object.
2753 This case can arise when a shared object is built with assembly
2754 code, and the assembly code fails to set the symbol type. */
2755 if (h->size == 0
2756 && h->type == STT_NOTYPE
f5385ebf 2757 && !h->needs_plt)
45d6a902
AM
2758 (*_bfd_error_handler)
2759 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2760 h->root.root.string);
2761
2762 dynobj = elf_hash_table (eif->info)->dynobj;
2763 bed = get_elf_backend_data (dynobj);
e7c33416 2764
45d6a902
AM
2765 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2766 {
2767 eif->failed = TRUE;
2768 return FALSE;
2769 }
2770
2771 return TRUE;
2772}
2773
027297b7
L
2774/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2775 DYNBSS. */
2776
2777bfd_boolean
6cabe1ea
AM
2778_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2779 struct elf_link_hash_entry *h,
027297b7
L
2780 asection *dynbss)
2781{
91ac5911 2782 unsigned int power_of_two;
027297b7
L
2783 bfd_vma mask;
2784 asection *sec = h->root.u.def.section;
2785
2786 /* The section aligment of definition is the maximum alignment
91ac5911
L
2787 requirement of symbols defined in the section. Since we don't
2788 know the symbol alignment requirement, we start with the
2789 maximum alignment and check low bits of the symbol address
2790 for the minimum alignment. */
2791 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2792 mask = ((bfd_vma) 1 << power_of_two) - 1;
2793 while ((h->root.u.def.value & mask) != 0)
2794 {
2795 mask >>= 1;
2796 --power_of_two;
2797 }
027297b7 2798
91ac5911
L
2799 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2800 dynbss))
027297b7
L
2801 {
2802 /* Adjust the section alignment if needed. */
2803 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2804 power_of_two))
027297b7
L
2805 return FALSE;
2806 }
2807
91ac5911 2808 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2809 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2810
2811 /* Define the symbol as being at this point in DYNBSS. */
2812 h->root.u.def.section = dynbss;
2813 h->root.u.def.value = dynbss->size;
2814
2815 /* Increment the size of DYNBSS to make room for the symbol. */
2816 dynbss->size += h->size;
2817
f7483970
L
2818 /* No error if extern_protected_data is true. */
2819 if (h->protected_def
889c2a67
L
2820 && (!info->extern_protected_data
2821 || (info->extern_protected_data < 0
2822 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2823 info->callbacks->einfo
2824 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2825 h->root.root.string);
6cabe1ea 2826
027297b7
L
2827 return TRUE;
2828}
2829
45d6a902
AM
2830/* Adjust all external symbols pointing into SEC_MERGE sections
2831 to reflect the object merging within the sections. */
2832
28caa186 2833static bfd_boolean
268b6b39 2834_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2835{
2836 asection *sec;
2837
45d6a902
AM
2838 if ((h->root.type == bfd_link_hash_defined
2839 || h->root.type == bfd_link_hash_defweak)
2840 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2841 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2842 {
a50b1753 2843 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2844
2845 h->root.u.def.value =
2846 _bfd_merged_section_offset (output_bfd,
2847 &h->root.u.def.section,
2848 elf_section_data (sec)->sec_info,
753731ee 2849 h->root.u.def.value);
45d6a902
AM
2850 }
2851
2852 return TRUE;
2853}
986a241f
RH
2854
2855/* Returns false if the symbol referred to by H should be considered
2856 to resolve local to the current module, and true if it should be
2857 considered to bind dynamically. */
2858
2859bfd_boolean
268b6b39
AM
2860_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2861 struct bfd_link_info *info,
89a2ee5a 2862 bfd_boolean not_local_protected)
986a241f
RH
2863{
2864 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2865 const struct elf_backend_data *bed;
2866 struct elf_link_hash_table *hash_table;
986a241f
RH
2867
2868 if (h == NULL)
2869 return FALSE;
2870
2871 while (h->root.type == bfd_link_hash_indirect
2872 || h->root.type == bfd_link_hash_warning)
2873 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2874
2875 /* If it was forced local, then clearly it's not dynamic. */
2876 if (h->dynindx == -1)
2877 return FALSE;
f5385ebf 2878 if (h->forced_local)
986a241f
RH
2879 return FALSE;
2880
2881 /* Identify the cases where name binding rules say that a
2882 visible symbol resolves locally. */
0e1862bb
L
2883 binding_stays_local_p = (bfd_link_executable (info)
2884 || SYMBOLIC_BIND (info, h));
986a241f
RH
2885
2886 switch (ELF_ST_VISIBILITY (h->other))
2887 {
2888 case STV_INTERNAL:
2889 case STV_HIDDEN:
2890 return FALSE;
2891
2892 case STV_PROTECTED:
fcb93ecf
PB
2893 hash_table = elf_hash_table (info);
2894 if (!is_elf_hash_table (hash_table))
2895 return FALSE;
2896
2897 bed = get_elf_backend_data (hash_table->dynobj);
2898
986a241f
RH
2899 /* Proper resolution for function pointer equality may require
2900 that these symbols perhaps be resolved dynamically, even though
2901 we should be resolving them to the current module. */
89a2ee5a 2902 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2903 binding_stays_local_p = TRUE;
2904 break;
2905
2906 default:
986a241f
RH
2907 break;
2908 }
2909
aa37626c 2910 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2911 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2912 return TRUE;
2913
986a241f
RH
2914 /* Otherwise, the symbol is dynamic if binding rules don't tell
2915 us that it remains local. */
2916 return !binding_stays_local_p;
2917}
f6c52c13
AM
2918
2919/* Return true if the symbol referred to by H should be considered
2920 to resolve local to the current module, and false otherwise. Differs
2921 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2922 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2923 for the place where forced_local and dynindx == -1 are tested. If
2924 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2925 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2926 the symbol is local only for defined symbols.
2927 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2928 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2929 treatment of undefined weak symbols. For those that do not make
2930 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2931
2932bfd_boolean
268b6b39
AM
2933_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2934 struct bfd_link_info *info,
2935 bfd_boolean local_protected)
f6c52c13 2936{
fcb93ecf
PB
2937 const struct elf_backend_data *bed;
2938 struct elf_link_hash_table *hash_table;
2939
f6c52c13
AM
2940 /* If it's a local sym, of course we resolve locally. */
2941 if (h == NULL)
2942 return TRUE;
2943
d95edcac
L
2944 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2945 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2946 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2947 return TRUE;
2948
7e2294f9
AO
2949 /* Common symbols that become definitions don't get the DEF_REGULAR
2950 flag set, so test it first, and don't bail out. */
2951 if (ELF_COMMON_DEF_P (h))
2952 /* Do nothing. */;
f6c52c13 2953 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2954 resolve locally. The sym is either undefined or dynamic. */
2955 else if (!h->def_regular)
f6c52c13
AM
2956 return FALSE;
2957
2958 /* Forced local symbols resolve locally. */
f5385ebf 2959 if (h->forced_local)
f6c52c13
AM
2960 return TRUE;
2961
2962 /* As do non-dynamic symbols. */
2963 if (h->dynindx == -1)
2964 return TRUE;
2965
2966 /* At this point, we know the symbol is defined and dynamic. In an
2967 executable it must resolve locally, likewise when building symbolic
2968 shared libraries. */
0e1862bb 2969 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2970 return TRUE;
2971
2972 /* Now deal with defined dynamic symbols in shared libraries. Ones
2973 with default visibility might not resolve locally. */
2974 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2975 return FALSE;
2976
fcb93ecf
PB
2977 hash_table = elf_hash_table (info);
2978 if (!is_elf_hash_table (hash_table))
2979 return TRUE;
2980
2981 bed = get_elf_backend_data (hash_table->dynobj);
2982
f7483970
L
2983 /* If extern_protected_data is false, STV_PROTECTED non-function
2984 symbols are local. */
889c2a67
L
2985 if ((!info->extern_protected_data
2986 || (info->extern_protected_data < 0
2987 && !bed->extern_protected_data))
2988 && !bed->is_function_type (h->type))
1c16dfa5
L
2989 return TRUE;
2990
f6c52c13 2991 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2992 symbols be treated as dynamic symbols. If the address of a
2993 function not defined in an executable is set to that function's
2994 plt entry in the executable, then the address of the function in
2995 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2996 return local_protected;
2997}
e1918d23
AM
2998
2999/* Caches some TLS segment info, and ensures that the TLS segment vma is
3000 aligned. Returns the first TLS output section. */
3001
3002struct bfd_section *
3003_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3004{
3005 struct bfd_section *sec, *tls;
3006 unsigned int align = 0;
3007
3008 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3009 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3010 break;
3011 tls = sec;
3012
3013 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3014 if (sec->alignment_power > align)
3015 align = sec->alignment_power;
3016
3017 elf_hash_table (info)->tls_sec = tls;
3018
3019 /* Ensure the alignment of the first section is the largest alignment,
3020 so that the tls segment starts aligned. */
3021 if (tls != NULL)
3022 tls->alignment_power = align;
3023
3024 return tls;
3025}
0ad989f9
L
3026
3027/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3028static bfd_boolean
3029is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3030 Elf_Internal_Sym *sym)
3031{
a4d8e49b
L
3032 const struct elf_backend_data *bed;
3033
0ad989f9
L
3034 /* Local symbols do not count, but target specific ones might. */
3035 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3036 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3037 return FALSE;
3038
fcb93ecf 3039 bed = get_elf_backend_data (abfd);
0ad989f9 3040 /* Function symbols do not count. */
fcb93ecf 3041 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3042 return FALSE;
3043
3044 /* If the section is undefined, then so is the symbol. */
3045 if (sym->st_shndx == SHN_UNDEF)
3046 return FALSE;
3047
3048 /* If the symbol is defined in the common section, then
3049 it is a common definition and so does not count. */
a4d8e49b 3050 if (bed->common_definition (sym))
0ad989f9
L
3051 return FALSE;
3052
3053 /* If the symbol is in a target specific section then we
3054 must rely upon the backend to tell us what it is. */
3055 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3056 /* FIXME - this function is not coded yet:
3057
3058 return _bfd_is_global_symbol_definition (abfd, sym);
3059
3060 Instead for now assume that the definition is not global,
3061 Even if this is wrong, at least the linker will behave
3062 in the same way that it used to do. */
3063 return FALSE;
3064
3065 return TRUE;
3066}
3067
3068/* Search the symbol table of the archive element of the archive ABFD
3069 whose archive map contains a mention of SYMDEF, and determine if
3070 the symbol is defined in this element. */
3071static bfd_boolean
3072elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3073{
3074 Elf_Internal_Shdr * hdr;
3075 bfd_size_type symcount;
3076 bfd_size_type extsymcount;
3077 bfd_size_type extsymoff;
3078 Elf_Internal_Sym *isymbuf;
3079 Elf_Internal_Sym *isym;
3080 Elf_Internal_Sym *isymend;
3081 bfd_boolean result;
3082
3083 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3084 if (abfd == NULL)
3085 return FALSE;
3086
f0bf6bfd
L
3087 /* Return FALSE if the object has been claimed by plugin. */
3088 if (abfd->plugin_format == bfd_plugin_yes)
3089 return FALSE;
3090
0ad989f9
L
3091 if (! bfd_check_format (abfd, bfd_object))
3092 return FALSE;
3093
0ad989f9
L
3094 /* Select the appropriate symbol table. */
3095 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3096 hdr = &elf_tdata (abfd)->symtab_hdr;
3097 else
3098 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3099
3100 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3101
3102 /* The sh_info field of the symtab header tells us where the
3103 external symbols start. We don't care about the local symbols. */
3104 if (elf_bad_symtab (abfd))
3105 {
3106 extsymcount = symcount;
3107 extsymoff = 0;
3108 }
3109 else
3110 {
3111 extsymcount = symcount - hdr->sh_info;
3112 extsymoff = hdr->sh_info;
3113 }
3114
3115 if (extsymcount == 0)
3116 return FALSE;
3117
3118 /* Read in the symbol table. */
3119 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3120 NULL, NULL, NULL);
3121 if (isymbuf == NULL)
3122 return FALSE;
3123
3124 /* Scan the symbol table looking for SYMDEF. */
3125 result = FALSE;
3126 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3127 {
3128 const char *name;
3129
3130 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3131 isym->st_name);
3132 if (name == NULL)
3133 break;
3134
3135 if (strcmp (name, symdef->name) == 0)
3136 {
3137 result = is_global_data_symbol_definition (abfd, isym);
3138 break;
3139 }
3140 }
3141
3142 free (isymbuf);
3143
3144 return result;
3145}
3146\f
5a580b3a
AM
3147/* Add an entry to the .dynamic table. */
3148
3149bfd_boolean
3150_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3151 bfd_vma tag,
3152 bfd_vma val)
3153{
3154 struct elf_link_hash_table *hash_table;
3155 const struct elf_backend_data *bed;
3156 asection *s;
3157 bfd_size_type newsize;
3158 bfd_byte *newcontents;
3159 Elf_Internal_Dyn dyn;
3160
3161 hash_table = elf_hash_table (info);
3162 if (! is_elf_hash_table (hash_table))
3163 return FALSE;
3164
3165 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3166 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3167 BFD_ASSERT (s != NULL);
3168
eea6121a 3169 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3170 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3171 if (newcontents == NULL)
3172 return FALSE;
3173
3174 dyn.d_tag = tag;
3175 dyn.d_un.d_val = val;
eea6121a 3176 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3177
eea6121a 3178 s->size = newsize;
5a580b3a
AM
3179 s->contents = newcontents;
3180
3181 return TRUE;
3182}
3183
3184/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3185 otherwise just check whether one already exists. Returns -1 on error,
3186 1 if a DT_NEEDED tag already exists, and 0 on success. */
3187
4ad4eba5 3188static int
7e9f0867
AM
3189elf_add_dt_needed_tag (bfd *abfd,
3190 struct bfd_link_info *info,
4ad4eba5
AM
3191 const char *soname,
3192 bfd_boolean do_it)
5a580b3a
AM
3193{
3194 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3195 bfd_size_type strindex;
3196
7e9f0867
AM
3197 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3198 return -1;
3199
5a580b3a 3200 hash_table = elf_hash_table (info);
5a580b3a
AM
3201 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3202 if (strindex == (bfd_size_type) -1)
3203 return -1;
3204
02be4619 3205 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3206 {
3207 asection *sdyn;
3208 const struct elf_backend_data *bed;
3209 bfd_byte *extdyn;
3210
3211 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3212 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3213 if (sdyn != NULL)
3214 for (extdyn = sdyn->contents;
3215 extdyn < sdyn->contents + sdyn->size;
3216 extdyn += bed->s->sizeof_dyn)
3217 {
3218 Elf_Internal_Dyn dyn;
5a580b3a 3219
7e9f0867
AM
3220 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3221 if (dyn.d_tag == DT_NEEDED
3222 && dyn.d_un.d_val == strindex)
3223 {
3224 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3225 return 1;
3226 }
3227 }
5a580b3a
AM
3228 }
3229
3230 if (do_it)
3231 {
7e9f0867
AM
3232 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3233 return -1;
3234
5a580b3a
AM
3235 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3236 return -1;
3237 }
3238 else
3239 /* We were just checking for existence of the tag. */
3240 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3241
3242 return 0;
3243}
3244
010e5ae2
AM
3245static bfd_boolean
3246on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3247{
3248 for (; needed != NULL; needed = needed->next)
1240be6b
AM
3249 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3250 && strcmp (soname, needed->name) == 0)
010e5ae2
AM
3251 return TRUE;
3252
3253 return FALSE;
3254}
3255
14160578 3256/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3257static int
3258elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3259{
3260 const struct elf_link_hash_entry *h1;
3261 const struct elf_link_hash_entry *h2;
10b7e05b 3262 bfd_signed_vma vdiff;
5a580b3a
AM
3263
3264 h1 = *(const struct elf_link_hash_entry **) arg1;
3265 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3266 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3267 if (vdiff != 0)
3268 return vdiff > 0 ? 1 : -1;
3269 else
3270 {
d3435ae8 3271 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3272 if (sdiff != 0)
3273 return sdiff > 0 ? 1 : -1;
3274 }
14160578
AM
3275 vdiff = h1->size - h2->size;
3276 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3277}
4ad4eba5 3278
5a580b3a
AM
3279/* This function is used to adjust offsets into .dynstr for
3280 dynamic symbols. This is called via elf_link_hash_traverse. */
3281
3282static bfd_boolean
3283elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3284{
a50b1753 3285 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3286
5a580b3a
AM
3287 if (h->dynindx != -1)
3288 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3289 return TRUE;
3290}
3291
3292/* Assign string offsets in .dynstr, update all structures referencing
3293 them. */
3294
4ad4eba5
AM
3295static bfd_boolean
3296elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3297{
3298 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3299 struct elf_link_local_dynamic_entry *entry;
3300 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3301 bfd *dynobj = hash_table->dynobj;
3302 asection *sdyn;
3303 bfd_size_type size;
3304 const struct elf_backend_data *bed;
3305 bfd_byte *extdyn;
3306
3307 _bfd_elf_strtab_finalize (dynstr);
3308 size = _bfd_elf_strtab_size (dynstr);
3309
3310 bed = get_elf_backend_data (dynobj);
3d4d4302 3311 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3312 BFD_ASSERT (sdyn != NULL);
3313
3314 /* Update all .dynamic entries referencing .dynstr strings. */
3315 for (extdyn = sdyn->contents;
eea6121a 3316 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3317 extdyn += bed->s->sizeof_dyn)
3318 {
3319 Elf_Internal_Dyn dyn;
3320
3321 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3322 switch (dyn.d_tag)
3323 {
3324 case DT_STRSZ:
3325 dyn.d_un.d_val = size;
3326 break;
3327 case DT_NEEDED:
3328 case DT_SONAME:
3329 case DT_RPATH:
3330 case DT_RUNPATH:
3331 case DT_FILTER:
3332 case DT_AUXILIARY:
7ee314fa
AM
3333 case DT_AUDIT:
3334 case DT_DEPAUDIT:
5a580b3a
AM
3335 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3336 break;
3337 default:
3338 continue;
3339 }
3340 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3341 }
3342
3343 /* Now update local dynamic symbols. */
3344 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3345 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3346 entry->isym.st_name);
3347
3348 /* And the rest of dynamic symbols. */
3349 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3350
3351 /* Adjust version definitions. */
3352 if (elf_tdata (output_bfd)->cverdefs)
3353 {
3354 asection *s;
3355 bfd_byte *p;
3356 bfd_size_type i;
3357 Elf_Internal_Verdef def;
3358 Elf_Internal_Verdaux defaux;
3359
3d4d4302 3360 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3361 p = s->contents;
3362 do
3363 {
3364 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3365 &def);
3366 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3367 if (def.vd_aux != sizeof (Elf_External_Verdef))
3368 continue;
5a580b3a
AM
3369 for (i = 0; i < def.vd_cnt; ++i)
3370 {
3371 _bfd_elf_swap_verdaux_in (output_bfd,
3372 (Elf_External_Verdaux *) p, &defaux);
3373 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3374 defaux.vda_name);
3375 _bfd_elf_swap_verdaux_out (output_bfd,
3376 &defaux, (Elf_External_Verdaux *) p);
3377 p += sizeof (Elf_External_Verdaux);
3378 }
3379 }
3380 while (def.vd_next);
3381 }
3382
3383 /* Adjust version references. */
3384 if (elf_tdata (output_bfd)->verref)
3385 {
3386 asection *s;
3387 bfd_byte *p;
3388 bfd_size_type i;
3389 Elf_Internal_Verneed need;
3390 Elf_Internal_Vernaux needaux;
3391
3d4d4302 3392 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3393 p = s->contents;
3394 do
3395 {
3396 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3397 &need);
3398 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3399 _bfd_elf_swap_verneed_out (output_bfd, &need,
3400 (Elf_External_Verneed *) p);
3401 p += sizeof (Elf_External_Verneed);
3402 for (i = 0; i < need.vn_cnt; ++i)
3403 {
3404 _bfd_elf_swap_vernaux_in (output_bfd,
3405 (Elf_External_Vernaux *) p, &needaux);
3406 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3407 needaux.vna_name);
3408 _bfd_elf_swap_vernaux_out (output_bfd,
3409 &needaux,
3410 (Elf_External_Vernaux *) p);
3411 p += sizeof (Elf_External_Vernaux);
3412 }
3413 }
3414 while (need.vn_next);
3415 }
3416
3417 return TRUE;
3418}
3419\f
13285a1b
AM
3420/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3421 The default is to only match when the INPUT and OUTPUT are exactly
3422 the same target. */
3423
3424bfd_boolean
3425_bfd_elf_default_relocs_compatible (const bfd_target *input,
3426 const bfd_target *output)
3427{
3428 return input == output;
3429}
3430
3431/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3432 This version is used when different targets for the same architecture
3433 are virtually identical. */
3434
3435bfd_boolean
3436_bfd_elf_relocs_compatible (const bfd_target *input,
3437 const bfd_target *output)
3438{
3439 const struct elf_backend_data *obed, *ibed;
3440
3441 if (input == output)
3442 return TRUE;
3443
3444 ibed = xvec_get_elf_backend_data (input);
3445 obed = xvec_get_elf_backend_data (output);
3446
3447 if (ibed->arch != obed->arch)
3448 return FALSE;
3449
3450 /* If both backends are using this function, deem them compatible. */
3451 return ibed->relocs_compatible == obed->relocs_compatible;
3452}
3453
e5034e59
AM
3454/* Make a special call to the linker "notice" function to tell it that
3455 we are about to handle an as-needed lib, or have finished
1b786873 3456 processing the lib. */
e5034e59
AM
3457
3458bfd_boolean
3459_bfd_elf_notice_as_needed (bfd *ibfd,
3460 struct bfd_link_info *info,
3461 enum notice_asneeded_action act)
3462{
46135103 3463 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3464}
3465
4ad4eba5
AM
3466/* Add symbols from an ELF object file to the linker hash table. */
3467
3468static bfd_boolean
3469elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3470{
a0c402a5 3471 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3472 Elf_Internal_Shdr *hdr;
3473 bfd_size_type symcount;
3474 bfd_size_type extsymcount;
3475 bfd_size_type extsymoff;
3476 struct elf_link_hash_entry **sym_hash;
3477 bfd_boolean dynamic;
3478 Elf_External_Versym *extversym = NULL;
3479 Elf_External_Versym *ever;
3480 struct elf_link_hash_entry *weaks;
3481 struct elf_link_hash_entry **nondeflt_vers = NULL;
3482 bfd_size_type nondeflt_vers_cnt = 0;
3483 Elf_Internal_Sym *isymbuf = NULL;
3484 Elf_Internal_Sym *isym;
3485 Elf_Internal_Sym *isymend;
3486 const struct elf_backend_data *bed;
3487 bfd_boolean add_needed;
66eb6687 3488 struct elf_link_hash_table *htab;
4ad4eba5 3489 bfd_size_type amt;
66eb6687 3490 void *alloc_mark = NULL;
4f87808c
AM
3491 struct bfd_hash_entry **old_table = NULL;
3492 unsigned int old_size = 0;
3493 unsigned int old_count = 0;
66eb6687 3494 void *old_tab = NULL;
66eb6687
AM
3495 void *old_ent;
3496 struct bfd_link_hash_entry *old_undefs = NULL;
3497 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3498 long old_dynsymcount = 0;
a4542f1b 3499 bfd_size_type old_dynstr_size = 0;
66eb6687 3500 size_t tabsize = 0;
db6a5d5f 3501 asection *s;
29a9f53e 3502 bfd_boolean just_syms;
4ad4eba5 3503
66eb6687 3504 htab = elf_hash_table (info);
4ad4eba5 3505 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3506
3507 if ((abfd->flags & DYNAMIC) == 0)
3508 dynamic = FALSE;
3509 else
3510 {
3511 dynamic = TRUE;
3512
3513 /* You can't use -r against a dynamic object. Also, there's no
3514 hope of using a dynamic object which does not exactly match
3515 the format of the output file. */
0e1862bb 3516 if (bfd_link_relocatable (info)
66eb6687 3517 || !is_elf_hash_table (htab)
f13a99db 3518 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3519 {
0e1862bb 3520 if (bfd_link_relocatable (info))
9a0789ec
NC
3521 bfd_set_error (bfd_error_invalid_operation);
3522 else
3523 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3524 goto error_return;
3525 }
3526 }
3527
a0c402a5
L
3528 ehdr = elf_elfheader (abfd);
3529 if (info->warn_alternate_em
3530 && bed->elf_machine_code != ehdr->e_machine
3531 && ((bed->elf_machine_alt1 != 0
3532 && ehdr->e_machine == bed->elf_machine_alt1)
3533 || (bed->elf_machine_alt2 != 0
3534 && ehdr->e_machine == bed->elf_machine_alt2)))
3535 info->callbacks->einfo
3536 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3537 ehdr->e_machine, abfd, bed->elf_machine_code);
3538
4ad4eba5
AM
3539 /* As a GNU extension, any input sections which are named
3540 .gnu.warning.SYMBOL are treated as warning symbols for the given
3541 symbol. This differs from .gnu.warning sections, which generate
3542 warnings when they are included in an output file. */
dd98f8d2 3543 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3544 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3545 {
db6a5d5f 3546 const char *name;
4ad4eba5 3547
db6a5d5f
AM
3548 name = bfd_get_section_name (abfd, s);
3549 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3550 {
db6a5d5f
AM
3551 char *msg;
3552 bfd_size_type sz;
3553
3554 name += sizeof ".gnu.warning." - 1;
3555
3556 /* If this is a shared object, then look up the symbol
3557 in the hash table. If it is there, and it is already
3558 been defined, then we will not be using the entry
3559 from this shared object, so we don't need to warn.
3560 FIXME: If we see the definition in a regular object
3561 later on, we will warn, but we shouldn't. The only
3562 fix is to keep track of what warnings we are supposed
3563 to emit, and then handle them all at the end of the
3564 link. */
3565 if (dynamic)
4ad4eba5 3566 {
db6a5d5f
AM
3567 struct elf_link_hash_entry *h;
3568
3569 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3570
3571 /* FIXME: What about bfd_link_hash_common? */
3572 if (h != NULL
3573 && (h->root.type == bfd_link_hash_defined
3574 || h->root.type == bfd_link_hash_defweak))
3575 continue;
3576 }
4ad4eba5 3577
db6a5d5f
AM
3578 sz = s->size;
3579 msg = (char *) bfd_alloc (abfd, sz + 1);
3580 if (msg == NULL)
3581 goto error_return;
4ad4eba5 3582
db6a5d5f
AM
3583 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3584 goto error_return;
4ad4eba5 3585
db6a5d5f 3586 msg[sz] = '\0';
4ad4eba5 3587
db6a5d5f
AM
3588 if (! (_bfd_generic_link_add_one_symbol
3589 (info, abfd, name, BSF_WARNING, s, 0, msg,
3590 FALSE, bed->collect, NULL)))
3591 goto error_return;
4ad4eba5 3592
0e1862bb 3593 if (bfd_link_executable (info))
db6a5d5f
AM
3594 {
3595 /* Clobber the section size so that the warning does
3596 not get copied into the output file. */
3597 s->size = 0;
11d2f718 3598
db6a5d5f
AM
3599 /* Also set SEC_EXCLUDE, so that symbols defined in
3600 the warning section don't get copied to the output. */
3601 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3602 }
3603 }
3604 }
3605
29a9f53e
L
3606 just_syms = ((s = abfd->sections) != NULL
3607 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3608
4ad4eba5
AM
3609 add_needed = TRUE;
3610 if (! dynamic)
3611 {
3612 /* If we are creating a shared library, create all the dynamic
3613 sections immediately. We need to attach them to something,
3614 so we attach them to this BFD, provided it is the right
bf89386a
L
3615 format and is not from ld --just-symbols. Always create the
3616 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3617 are no input BFD's of the same format as the output, we can't
3618 make a shared library. */
3619 if (!just_syms
bf89386a
L
3620 && (bfd_link_pic (info)
3621 || info->export_dynamic
3622 || info->dynamic)
66eb6687 3623 && is_elf_hash_table (htab)
f13a99db 3624 && info->output_bfd->xvec == abfd->xvec
66eb6687 3625 && !htab->dynamic_sections_created)
4ad4eba5
AM
3626 {
3627 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3628 goto error_return;
3629 }
3630 }
66eb6687 3631 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3632 goto error_return;
3633 else
3634 {
4ad4eba5 3635 const char *soname = NULL;
7ee314fa 3636 char *audit = NULL;
4ad4eba5
AM
3637 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3638 int ret;
3639
3640 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3641 ld shouldn't allow it. */
29a9f53e 3642 if (just_syms)
92fd189d 3643 abort ();
4ad4eba5
AM
3644
3645 /* If this dynamic lib was specified on the command line with
3646 --as-needed in effect, then we don't want to add a DT_NEEDED
3647 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3648 in by another lib's DT_NEEDED. When --no-add-needed is used
3649 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3650 any dynamic library in DT_NEEDED tags in the dynamic lib at
3651 all. */
3652 add_needed = (elf_dyn_lib_class (abfd)
3653 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3654 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3655
3656 s = bfd_get_section_by_name (abfd, ".dynamic");
3657 if (s != NULL)
3658 {
3659 bfd_byte *dynbuf;
3660 bfd_byte *extdyn;
cb33740c 3661 unsigned int elfsec;
4ad4eba5
AM
3662 unsigned long shlink;
3663
eea6121a 3664 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3665 {
3666error_free_dyn:
3667 free (dynbuf);
3668 goto error_return;
3669 }
4ad4eba5
AM
3670
3671 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3672 if (elfsec == SHN_BAD)
4ad4eba5
AM
3673 goto error_free_dyn;
3674 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3675
3676 for (extdyn = dynbuf;
eea6121a 3677 extdyn < dynbuf + s->size;
4ad4eba5
AM
3678 extdyn += bed->s->sizeof_dyn)
3679 {
3680 Elf_Internal_Dyn dyn;
3681
3682 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3683 if (dyn.d_tag == DT_SONAME)
3684 {
3685 unsigned int tagv = dyn.d_un.d_val;
3686 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3687 if (soname == NULL)
3688 goto error_free_dyn;
3689 }
3690 if (dyn.d_tag == DT_NEEDED)
3691 {
3692 struct bfd_link_needed_list *n, **pn;
3693 char *fnm, *anm;
3694 unsigned int tagv = dyn.d_un.d_val;
3695
3696 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3697 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3698 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3699 if (n == NULL || fnm == NULL)
3700 goto error_free_dyn;
3701 amt = strlen (fnm) + 1;
a50b1753 3702 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3703 if (anm == NULL)
3704 goto error_free_dyn;
3705 memcpy (anm, fnm, amt);
3706 n->name = anm;
3707 n->by = abfd;
3708 n->next = NULL;
66eb6687 3709 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3710 ;
3711 *pn = n;
3712 }
3713 if (dyn.d_tag == DT_RUNPATH)
3714 {
3715 struct bfd_link_needed_list *n, **pn;
3716 char *fnm, *anm;
3717 unsigned int tagv = dyn.d_un.d_val;
3718
3719 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3720 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3721 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3722 if (n == NULL || fnm == NULL)
3723 goto error_free_dyn;
3724 amt = strlen (fnm) + 1;
a50b1753 3725 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3726 if (anm == NULL)
3727 goto error_free_dyn;
3728 memcpy (anm, fnm, amt);
3729 n->name = anm;
3730 n->by = abfd;
3731 n->next = NULL;
3732 for (pn = & runpath;
3733 *pn != NULL;
3734 pn = &(*pn)->next)
3735 ;
3736 *pn = n;
3737 }
3738 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3739 if (!runpath && dyn.d_tag == DT_RPATH)
3740 {
3741 struct bfd_link_needed_list *n, **pn;
3742 char *fnm, *anm;
3743 unsigned int tagv = dyn.d_un.d_val;
3744
3745 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3746 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3747 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3748 if (n == NULL || fnm == NULL)
3749 goto error_free_dyn;
3750 amt = strlen (fnm) + 1;
a50b1753 3751 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3752 if (anm == NULL)
f8703194 3753 goto error_free_dyn;
4ad4eba5
AM
3754 memcpy (anm, fnm, amt);
3755 n->name = anm;
3756 n->by = abfd;
3757 n->next = NULL;
3758 for (pn = & rpath;
3759 *pn != NULL;
3760 pn = &(*pn)->next)
3761 ;
3762 *pn = n;
3763 }
7ee314fa
AM
3764 if (dyn.d_tag == DT_AUDIT)
3765 {
3766 unsigned int tagv = dyn.d_un.d_val;
3767 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3768 }
4ad4eba5
AM
3769 }
3770
3771 free (dynbuf);
3772 }
3773
3774 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3775 frees all more recently bfd_alloc'd blocks as well. */
3776 if (runpath)
3777 rpath = runpath;
3778
3779 if (rpath)
3780 {
3781 struct bfd_link_needed_list **pn;
66eb6687 3782 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3783 ;
3784 *pn = rpath;
3785 }
3786
3787 /* We do not want to include any of the sections in a dynamic
3788 object in the output file. We hack by simply clobbering the
3789 list of sections in the BFD. This could be handled more
3790 cleanly by, say, a new section flag; the existing
3791 SEC_NEVER_LOAD flag is not the one we want, because that one
3792 still implies that the section takes up space in the output
3793 file. */
3794 bfd_section_list_clear (abfd);
3795
4ad4eba5
AM
3796 /* Find the name to use in a DT_NEEDED entry that refers to this
3797 object. If the object has a DT_SONAME entry, we use it.
3798 Otherwise, if the generic linker stuck something in
3799 elf_dt_name, we use that. Otherwise, we just use the file
3800 name. */
3801 if (soname == NULL || *soname == '\0')
3802 {
3803 soname = elf_dt_name (abfd);
3804 if (soname == NULL || *soname == '\0')
3805 soname = bfd_get_filename (abfd);
3806 }
3807
3808 /* Save the SONAME because sometimes the linker emulation code
3809 will need to know it. */
3810 elf_dt_name (abfd) = soname;
3811
7e9f0867 3812 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3813 if (ret < 0)
3814 goto error_return;
3815
3816 /* If we have already included this dynamic object in the
3817 link, just ignore it. There is no reason to include a
3818 particular dynamic object more than once. */
3819 if (ret > 0)
3820 return TRUE;
7ee314fa
AM
3821
3822 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3823 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3824 }
3825
3826 /* If this is a dynamic object, we always link against the .dynsym
3827 symbol table, not the .symtab symbol table. The dynamic linker
3828 will only see the .dynsym symbol table, so there is no reason to
3829 look at .symtab for a dynamic object. */
3830
3831 if (! dynamic || elf_dynsymtab (abfd) == 0)
3832 hdr = &elf_tdata (abfd)->symtab_hdr;
3833 else
3834 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3835
3836 symcount = hdr->sh_size / bed->s->sizeof_sym;
3837
3838 /* The sh_info field of the symtab header tells us where the
3839 external symbols start. We don't care about the local symbols at
3840 this point. */
3841 if (elf_bad_symtab (abfd))
3842 {
3843 extsymcount = symcount;
3844 extsymoff = 0;
3845 }
3846 else
3847 {
3848 extsymcount = symcount - hdr->sh_info;
3849 extsymoff = hdr->sh_info;
3850 }
3851
f45794cb 3852 sym_hash = elf_sym_hashes (abfd);
012b2306 3853 if (extsymcount != 0)
4ad4eba5
AM
3854 {
3855 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3856 NULL, NULL, NULL);
3857 if (isymbuf == NULL)
3858 goto error_return;
3859
4ad4eba5 3860 if (sym_hash == NULL)
012b2306
AM
3861 {
3862 /* We store a pointer to the hash table entry for each
3863 external symbol. */
3864 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3865 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3866 if (sym_hash == NULL)
3867 goto error_free_sym;
3868 elf_sym_hashes (abfd) = sym_hash;
3869 }
4ad4eba5
AM
3870 }
3871
3872 if (dynamic)
3873 {
3874 /* Read in any version definitions. */
fc0e6df6
PB
3875 if (!_bfd_elf_slurp_version_tables (abfd,
3876 info->default_imported_symver))
4ad4eba5
AM
3877 goto error_free_sym;
3878
3879 /* Read in the symbol versions, but don't bother to convert them
3880 to internal format. */
3881 if (elf_dynversym (abfd) != 0)
3882 {
3883 Elf_Internal_Shdr *versymhdr;
3884
3885 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3886 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3887 if (extversym == NULL)
3888 goto error_free_sym;
3889 amt = versymhdr->sh_size;
3890 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3891 || bfd_bread (extversym, amt, abfd) != amt)
3892 goto error_free_vers;
3893 }
3894 }
3895
66eb6687
AM
3896 /* If we are loading an as-needed shared lib, save the symbol table
3897 state before we start adding symbols. If the lib turns out
3898 to be unneeded, restore the state. */
3899 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3900 {
3901 unsigned int i;
3902 size_t entsize;
3903
3904 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3905 {
3906 struct bfd_hash_entry *p;
2de92251 3907 struct elf_link_hash_entry *h;
66eb6687
AM
3908
3909 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3910 {
3911 h = (struct elf_link_hash_entry *) p;
3912 entsize += htab->root.table.entsize;
3913 if (h->root.type == bfd_link_hash_warning)
3914 entsize += htab->root.table.entsize;
3915 }
66eb6687
AM
3916 }
3917
3918 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 3919 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
3920 if (old_tab == NULL)
3921 goto error_free_vers;
3922
3923 /* Remember the current objalloc pointer, so that all mem for
3924 symbols added can later be reclaimed. */
3925 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3926 if (alloc_mark == NULL)
3927 goto error_free_vers;
3928
5061a885
AM
3929 /* Make a special call to the linker "notice" function to
3930 tell it that we are about to handle an as-needed lib. */
e5034e59 3931 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 3932 goto error_free_vers;
5061a885 3933
f45794cb
AM
3934 /* Clone the symbol table. Remember some pointers into the
3935 symbol table, and dynamic symbol count. */
3936 old_ent = (char *) old_tab + tabsize;
66eb6687 3937 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
3938 old_undefs = htab->root.undefs;
3939 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3940 old_table = htab->root.table.table;
3941 old_size = htab->root.table.size;
3942 old_count = htab->root.table.count;
66eb6687 3943 old_dynsymcount = htab->dynsymcount;
a4542f1b 3944 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
3945
3946 for (i = 0; i < htab->root.table.size; i++)
3947 {
3948 struct bfd_hash_entry *p;
2de92251 3949 struct elf_link_hash_entry *h;
66eb6687
AM
3950
3951 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3952 {
3953 memcpy (old_ent, p, htab->root.table.entsize);
3954 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3955 h = (struct elf_link_hash_entry *) p;
3956 if (h->root.type == bfd_link_hash_warning)
3957 {
3958 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3959 old_ent = (char *) old_ent + htab->root.table.entsize;
3960 }
66eb6687
AM
3961 }
3962 }
3963 }
4ad4eba5 3964
66eb6687 3965 weaks = NULL;
4ad4eba5
AM
3966 ever = extversym != NULL ? extversym + extsymoff : NULL;
3967 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3968 isym < isymend;
3969 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3970 {
3971 int bind;
3972 bfd_vma value;
af44c138 3973 asection *sec, *new_sec;
4ad4eba5
AM
3974 flagword flags;
3975 const char *name;
3976 struct elf_link_hash_entry *h;
90c984fc 3977 struct elf_link_hash_entry *hi;
4ad4eba5
AM
3978 bfd_boolean definition;
3979 bfd_boolean size_change_ok;
3980 bfd_boolean type_change_ok;
3981 bfd_boolean new_weakdef;
37a9e49a
L
3982 bfd_boolean new_weak;
3983 bfd_boolean old_weak;
4ad4eba5 3984 bfd_boolean override;
a4d8e49b 3985 bfd_boolean common;
4ad4eba5
AM
3986 unsigned int old_alignment;
3987 bfd *old_bfd;
6e33951e 3988 bfd_boolean matched;
4ad4eba5
AM
3989
3990 override = FALSE;
3991
3992 flags = BSF_NO_FLAGS;
3993 sec = NULL;
3994 value = isym->st_value;
a4d8e49b 3995 common = bed->common_definition (isym);
4ad4eba5
AM
3996
3997 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3998 switch (bind)
4ad4eba5 3999 {
3e7a7d11 4000 case STB_LOCAL:
4ad4eba5
AM
4001 /* This should be impossible, since ELF requires that all
4002 global symbols follow all local symbols, and that sh_info
4003 point to the first global symbol. Unfortunately, Irix 5
4004 screws this up. */
4005 continue;
3e7a7d11
NC
4006
4007 case STB_GLOBAL:
a4d8e49b 4008 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4009 flags = BSF_GLOBAL;
3e7a7d11
NC
4010 break;
4011
4012 case STB_WEAK:
4013 flags = BSF_WEAK;
4014 break;
4015
4016 case STB_GNU_UNIQUE:
4017 flags = BSF_GNU_UNIQUE;
4018 break;
4019
4020 default:
4ad4eba5 4021 /* Leave it up to the processor backend. */
3e7a7d11 4022 break;
4ad4eba5
AM
4023 }
4024
4025 if (isym->st_shndx == SHN_UNDEF)
4026 sec = bfd_und_section_ptr;
cb33740c
AM
4027 else if (isym->st_shndx == SHN_ABS)
4028 sec = bfd_abs_section_ptr;
4029 else if (isym->st_shndx == SHN_COMMON)
4030 {
4031 sec = bfd_com_section_ptr;
4032 /* What ELF calls the size we call the value. What ELF
4033 calls the value we call the alignment. */
4034 value = isym->st_size;
4035 }
4036 else
4ad4eba5
AM
4037 {
4038 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4039 if (sec == NULL)
4040 sec = bfd_abs_section_ptr;
dbaa2011 4041 else if (discarded_section (sec))
529fcb95 4042 {
e5d08002
L
4043 /* Symbols from discarded section are undefined. We keep
4044 its visibility. */
529fcb95
PB
4045 sec = bfd_und_section_ptr;
4046 isym->st_shndx = SHN_UNDEF;
4047 }
4ad4eba5
AM
4048 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4049 value -= sec->vma;
4050 }
4ad4eba5
AM
4051
4052 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4053 isym->st_name);
4054 if (name == NULL)
4055 goto error_free_vers;
4056
4057 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4058 && (abfd->flags & BFD_PLUGIN) != 0)
4059 {
4060 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4061
4062 if (xc == NULL)
4063 {
4064 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4065 | SEC_EXCLUDE);
4066 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4067 if (xc == NULL)
4068 goto error_free_vers;
4069 }
4070 sec = xc;
4071 }
4072 else if (isym->st_shndx == SHN_COMMON
4073 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4074 && !bfd_link_relocatable (info))
4ad4eba5
AM
4075 {
4076 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4077
4078 if (tcomm == NULL)
4079 {
02d00247
AM
4080 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4081 | SEC_LINKER_CREATED);
4082 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4083 if (tcomm == NULL)
4ad4eba5
AM
4084 goto error_free_vers;
4085 }
4086 sec = tcomm;
4087 }
66eb6687 4088 else if (bed->elf_add_symbol_hook)
4ad4eba5 4089 {
66eb6687
AM
4090 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4091 &sec, &value))
4ad4eba5
AM
4092 goto error_free_vers;
4093
4094 /* The hook function sets the name to NULL if this symbol
4095 should be skipped for some reason. */
4096 if (name == NULL)
4097 continue;
4098 }
4099
4100 /* Sanity check that all possibilities were handled. */
4101 if (sec == NULL)
4102 {
4103 bfd_set_error (bfd_error_bad_value);
4104 goto error_free_vers;
4105 }
4106
191c0c42
AM
4107 /* Silently discard TLS symbols from --just-syms. There's
4108 no way to combine a static TLS block with a new TLS block
4109 for this executable. */
4110 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4111 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4112 continue;
4113
4ad4eba5
AM
4114 if (bfd_is_und_section (sec)
4115 || bfd_is_com_section (sec))
4116 definition = FALSE;
4117 else
4118 definition = TRUE;
4119
4120 size_change_ok = FALSE;
66eb6687 4121 type_change_ok = bed->type_change_ok;
37a9e49a 4122 old_weak = FALSE;
6e33951e 4123 matched = FALSE;
4ad4eba5
AM
4124 old_alignment = 0;
4125 old_bfd = NULL;
af44c138 4126 new_sec = sec;
4ad4eba5 4127
66eb6687 4128 if (is_elf_hash_table (htab))
4ad4eba5
AM
4129 {
4130 Elf_Internal_Versym iver;
4131 unsigned int vernum = 0;
4132 bfd_boolean skip;
4133
fc0e6df6 4134 if (ever == NULL)
4ad4eba5 4135 {
fc0e6df6
PB
4136 if (info->default_imported_symver)
4137 /* Use the default symbol version created earlier. */
4138 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4139 else
4140 iver.vs_vers = 0;
4141 }
4142 else
4143 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4144
4145 vernum = iver.vs_vers & VERSYM_VERSION;
4146
4147 /* If this is a hidden symbol, or if it is not version
4148 1, we append the version name to the symbol name.
cc86ff91
EB
4149 However, we do not modify a non-hidden absolute symbol
4150 if it is not a function, because it might be the version
4151 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4152 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4153 || (vernum > 1
4154 && (!bfd_is_abs_section (sec)
4155 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4156 {
4157 const char *verstr;
4158 size_t namelen, verlen, newlen;
4159 char *newname, *p;
4160
4161 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4162 {
fc0e6df6
PB
4163 if (vernum > elf_tdata (abfd)->cverdefs)
4164 verstr = NULL;
4165 else if (vernum > 1)
4166 verstr =
4167 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4168 else
4169 verstr = "";
4ad4eba5 4170
fc0e6df6 4171 if (verstr == NULL)
4ad4eba5 4172 {
fc0e6df6
PB
4173 (*_bfd_error_handler)
4174 (_("%B: %s: invalid version %u (max %d)"),
4175 abfd, name, vernum,
4176 elf_tdata (abfd)->cverdefs);
4177 bfd_set_error (bfd_error_bad_value);
4178 goto error_free_vers;
4ad4eba5 4179 }
fc0e6df6
PB
4180 }
4181 else
4182 {
4183 /* We cannot simply test for the number of
4184 entries in the VERNEED section since the
4185 numbers for the needed versions do not start
4186 at 0. */
4187 Elf_Internal_Verneed *t;
4188
4189 verstr = NULL;
4190 for (t = elf_tdata (abfd)->verref;
4191 t != NULL;
4192 t = t->vn_nextref)
4ad4eba5 4193 {
fc0e6df6 4194 Elf_Internal_Vernaux *a;
4ad4eba5 4195
fc0e6df6
PB
4196 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4197 {
4198 if (a->vna_other == vernum)
4ad4eba5 4199 {
fc0e6df6
PB
4200 verstr = a->vna_nodename;
4201 break;
4ad4eba5 4202 }
4ad4eba5 4203 }
fc0e6df6
PB
4204 if (a != NULL)
4205 break;
4206 }
4207 if (verstr == NULL)
4208 {
4209 (*_bfd_error_handler)
4210 (_("%B: %s: invalid needed version %d"),
4211 abfd, name, vernum);
4212 bfd_set_error (bfd_error_bad_value);
4213 goto error_free_vers;
4ad4eba5 4214 }
4ad4eba5 4215 }
fc0e6df6
PB
4216
4217 namelen = strlen (name);
4218 verlen = strlen (verstr);
4219 newlen = namelen + verlen + 2;
4220 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4221 && isym->st_shndx != SHN_UNDEF)
4222 ++newlen;
4223
a50b1753 4224 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4225 if (newname == NULL)
4226 goto error_free_vers;
4227 memcpy (newname, name, namelen);
4228 p = newname + namelen;
4229 *p++ = ELF_VER_CHR;
4230 /* If this is a defined non-hidden version symbol,
4231 we add another @ to the name. This indicates the
4232 default version of the symbol. */
4233 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4234 && isym->st_shndx != SHN_UNDEF)
4235 *p++ = ELF_VER_CHR;
4236 memcpy (p, verstr, verlen + 1);
4237
4238 name = newname;
4ad4eba5
AM
4239 }
4240
cd3416da
AM
4241 /* If this symbol has default visibility and the user has
4242 requested we not re-export it, then mark it as hidden. */
a0d49154 4243 if (!bfd_is_und_section (sec)
cd3416da 4244 && !dynamic
ce875075 4245 && abfd->no_export
cd3416da
AM
4246 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4247 isym->st_other = (STV_HIDDEN
4248 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4249
4f3fedcf
AM
4250 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4251 sym_hash, &old_bfd, &old_weak,
4252 &old_alignment, &skip, &override,
6e33951e
L
4253 &type_change_ok, &size_change_ok,
4254 &matched))
4ad4eba5
AM
4255 goto error_free_vers;
4256
4257 if (skip)
4258 continue;
4259
6e33951e
L
4260 /* Override a definition only if the new symbol matches the
4261 existing one. */
4262 if (override && matched)
4ad4eba5
AM
4263 definition = FALSE;
4264
4265 h = *sym_hash;
4266 while (h->root.type == bfd_link_hash_indirect
4267 || h->root.type == bfd_link_hash_warning)
4268 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4269
4ad4eba5 4270 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4271 && vernum > 1
4272 && definition)
4273 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4274 }
4275
4276 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4277 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4278 (struct bfd_link_hash_entry **) sym_hash)))
4279 goto error_free_vers;
4280
4281 h = *sym_hash;
90c984fc
L
4282 /* We need to make sure that indirect symbol dynamic flags are
4283 updated. */
4284 hi = h;
4ad4eba5
AM
4285 while (h->root.type == bfd_link_hash_indirect
4286 || h->root.type == bfd_link_hash_warning)
4287 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4288
4ad4eba5
AM
4289 *sym_hash = h;
4290
37a9e49a 4291 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4292 new_weakdef = FALSE;
4293 if (dynamic
4294 && definition
37a9e49a 4295 && new_weak
fcb93ecf 4296 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4297 && is_elf_hash_table (htab)
f6e332e6 4298 && h->u.weakdef == NULL)
4ad4eba5
AM
4299 {
4300 /* Keep a list of all weak defined non function symbols from
4301 a dynamic object, using the weakdef field. Later in this
4302 function we will set the weakdef field to the correct
4303 value. We only put non-function symbols from dynamic
4304 objects on this list, because that happens to be the only
4305 time we need to know the normal symbol corresponding to a
4306 weak symbol, and the information is time consuming to
4307 figure out. If the weakdef field is not already NULL,
4308 then this symbol was already defined by some previous
4309 dynamic object, and we will be using that previous
4310 definition anyhow. */
4311
f6e332e6 4312 h->u.weakdef = weaks;
4ad4eba5
AM
4313 weaks = h;
4314 new_weakdef = TRUE;
4315 }
4316
4317 /* Set the alignment of a common symbol. */
a4d8e49b 4318 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4319 && h->root.type == bfd_link_hash_common)
4320 {
4321 unsigned int align;
4322
a4d8e49b 4323 if (common)
af44c138
L
4324 align = bfd_log2 (isym->st_value);
4325 else
4326 {
4327 /* The new symbol is a common symbol in a shared object.
4328 We need to get the alignment from the section. */
4329 align = new_sec->alignment_power;
4330 }
595213d4 4331 if (align > old_alignment)
4ad4eba5
AM
4332 h->root.u.c.p->alignment_power = align;
4333 else
4334 h->root.u.c.p->alignment_power = old_alignment;
4335 }
4336
66eb6687 4337 if (is_elf_hash_table (htab))
4ad4eba5 4338 {
4f3fedcf
AM
4339 /* Set a flag in the hash table entry indicating the type of
4340 reference or definition we just found. A dynamic symbol
4341 is one which is referenced or defined by both a regular
4342 object and a shared object. */
4343 bfd_boolean dynsym = FALSE;
4344
4345 /* Plugin symbols aren't normal. Don't set def_regular or
4346 ref_regular for them, or make them dynamic. */
4347 if ((abfd->flags & BFD_PLUGIN) != 0)
4348 ;
4349 else if (! dynamic)
4350 {
4351 if (! definition)
4352 {
4353 h->ref_regular = 1;
4354 if (bind != STB_WEAK)
4355 h->ref_regular_nonweak = 1;
4356 }
4357 else
4358 {
4359 h->def_regular = 1;
4360 if (h->def_dynamic)
4361 {
4362 h->def_dynamic = 0;
4363 h->ref_dynamic = 1;
4364 }
4365 }
4366
4367 /* If the indirect symbol has been forced local, don't
4368 make the real symbol dynamic. */
4369 if ((h == hi || !hi->forced_local)
0e1862bb 4370 && (bfd_link_dll (info)
4f3fedcf
AM
4371 || h->def_dynamic
4372 || h->ref_dynamic))
4373 dynsym = TRUE;
4374 }
4375 else
4376 {
4377 if (! definition)
4378 {
4379 h->ref_dynamic = 1;
4380 hi->ref_dynamic = 1;
4381 }
4382 else
4383 {
4384 h->def_dynamic = 1;
4385 hi->def_dynamic = 1;
4386 }
4387
4388 /* If the indirect symbol has been forced local, don't
4389 make the real symbol dynamic. */
4390 if ((h == hi || !hi->forced_local)
4391 && (h->def_regular
4392 || h->ref_regular
4393 || (h->u.weakdef != NULL
4394 && ! new_weakdef
4395 && h->u.weakdef->dynindx != -1)))
4396 dynsym = TRUE;
4397 }
4398
4399 /* Check to see if we need to add an indirect symbol for
4400 the default name. */
4401 if (definition
4402 || (!override && h->root.type == bfd_link_hash_common))
4403 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4404 sec, value, &old_bfd, &dynsym))
4405 goto error_free_vers;
4ad4eba5
AM
4406
4407 /* Check the alignment when a common symbol is involved. This
4408 can change when a common symbol is overridden by a normal
4409 definition or a common symbol is ignored due to the old
4410 normal definition. We need to make sure the maximum
4411 alignment is maintained. */
a4d8e49b 4412 if ((old_alignment || common)
4ad4eba5
AM
4413 && h->root.type != bfd_link_hash_common)
4414 {
4415 unsigned int common_align;
4416 unsigned int normal_align;
4417 unsigned int symbol_align;
4418 bfd *normal_bfd;
4419 bfd *common_bfd;
4420
3a81e825
AM
4421 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4422 || h->root.type == bfd_link_hash_defweak);
4423
4ad4eba5
AM
4424 symbol_align = ffs (h->root.u.def.value) - 1;
4425 if (h->root.u.def.section->owner != NULL
4426 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4427 {
4428 normal_align = h->root.u.def.section->alignment_power;
4429 if (normal_align > symbol_align)
4430 normal_align = symbol_align;
4431 }
4432 else
4433 normal_align = symbol_align;
4434
4435 if (old_alignment)
4436 {
4437 common_align = old_alignment;
4438 common_bfd = old_bfd;
4439 normal_bfd = abfd;
4440 }
4441 else
4442 {
4443 common_align = bfd_log2 (isym->st_value);
4444 common_bfd = abfd;
4445 normal_bfd = old_bfd;
4446 }
4447
4448 if (normal_align < common_align)
d07676f8
NC
4449 {
4450 /* PR binutils/2735 */
4451 if (normal_bfd == NULL)
4452 (*_bfd_error_handler)
4f3fedcf
AM
4453 (_("Warning: alignment %u of common symbol `%s' in %B is"
4454 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4455 common_bfd, h->root.u.def.section,
4456 1 << common_align, name, 1 << normal_align);
4457 else
4458 (*_bfd_error_handler)
4459 (_("Warning: alignment %u of symbol `%s' in %B"
4460 " is smaller than %u in %B"),
4461 normal_bfd, common_bfd,
4462 1 << normal_align, name, 1 << common_align);
4463 }
4ad4eba5
AM
4464 }
4465
83ad0046 4466 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4467 if (isym->st_size != 0
4468 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4469 && (definition || h->size == 0))
4470 {
83ad0046
L
4471 if (h->size != 0
4472 && h->size != isym->st_size
4473 && ! size_change_ok)
4ad4eba5 4474 (*_bfd_error_handler)
d003868e
AM
4475 (_("Warning: size of symbol `%s' changed"
4476 " from %lu in %B to %lu in %B"),
4477 old_bfd, abfd,
4ad4eba5 4478 name, (unsigned long) h->size,
d003868e 4479 (unsigned long) isym->st_size);
4ad4eba5
AM
4480
4481 h->size = isym->st_size;
4482 }
4483
4484 /* If this is a common symbol, then we always want H->SIZE
4485 to be the size of the common symbol. The code just above
4486 won't fix the size if a common symbol becomes larger. We
4487 don't warn about a size change here, because that is
4f3fedcf 4488 covered by --warn-common. Allow changes between different
fcb93ecf 4489 function types. */
4ad4eba5
AM
4490 if (h->root.type == bfd_link_hash_common)
4491 h->size = h->root.u.c.size;
4492
4493 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4494 && ((definition && !new_weak)
4495 || (old_weak && h->root.type == bfd_link_hash_common)
4496 || h->type == STT_NOTYPE))
4ad4eba5 4497 {
2955ec4c
L
4498 unsigned int type = ELF_ST_TYPE (isym->st_info);
4499
4500 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4501 symbol. */
4502 if (type == STT_GNU_IFUNC
4503 && (abfd->flags & DYNAMIC) != 0)
4504 type = STT_FUNC;
4ad4eba5 4505
2955ec4c
L
4506 if (h->type != type)
4507 {
4508 if (h->type != STT_NOTYPE && ! type_change_ok)
4509 (*_bfd_error_handler)
4510 (_("Warning: type of symbol `%s' changed"
4511 " from %d to %d in %B"),
4512 abfd, name, h->type, type);
4513
4514 h->type = type;
4515 }
4ad4eba5
AM
4516 }
4517
54ac0771 4518 /* Merge st_other field. */
b8417128 4519 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4520
c3df8c14 4521 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4522 if (definition
4523 && (sec->flags & SEC_DEBUGGING)
4524 && !bfd_link_relocatable (info))
c3df8c14
AM
4525 dynsym = FALSE;
4526
4f3fedcf
AM
4527 /* Nor should we make plugin symbols dynamic. */
4528 if ((abfd->flags & BFD_PLUGIN) != 0)
4529 dynsym = FALSE;
4530
35fc36a8 4531 if (definition)
35399224
L
4532 {
4533 h->target_internal = isym->st_target_internal;
4534 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4535 }
35fc36a8 4536
4ad4eba5
AM
4537 if (definition && !dynamic)
4538 {
4539 char *p = strchr (name, ELF_VER_CHR);
4540 if (p != NULL && p[1] != ELF_VER_CHR)
4541 {
4542 /* Queue non-default versions so that .symver x, x@FOO
4543 aliases can be checked. */
66eb6687 4544 if (!nondeflt_vers)
4ad4eba5 4545 {
66eb6687
AM
4546 amt = ((isymend - isym + 1)
4547 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4548 nondeflt_vers
4549 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4550 if (!nondeflt_vers)
4551 goto error_free_vers;
4ad4eba5 4552 }
66eb6687 4553 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4554 }
4555 }
4556
4557 if (dynsym && h->dynindx == -1)
4558 {
c152c796 4559 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4560 goto error_free_vers;
f6e332e6 4561 if (h->u.weakdef != NULL
4ad4eba5 4562 && ! new_weakdef
f6e332e6 4563 && h->u.weakdef->dynindx == -1)
4ad4eba5 4564 {
66eb6687 4565 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4566 goto error_free_vers;
4567 }
4568 }
4569 else if (dynsym && h->dynindx != -1)
4570 /* If the symbol already has a dynamic index, but
4571 visibility says it should not be visible, turn it into
4572 a local symbol. */
4573 switch (ELF_ST_VISIBILITY (h->other))
4574 {
4575 case STV_INTERNAL:
4576 case STV_HIDDEN:
4577 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4578 dynsym = FALSE;
4579 break;
4580 }
4581
aef28989
L
4582 /* Don't add DT_NEEDED for references from the dummy bfd nor
4583 for unmatched symbol. */
4ad4eba5 4584 if (!add_needed
aef28989 4585 && matched
4ad4eba5 4586 && definition
010e5ae2 4587 && ((dynsym
ffa9430d 4588 && h->ref_regular_nonweak
4f3fedcf
AM
4589 && (old_bfd == NULL
4590 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4591 || (h->ref_dynamic_nonweak
010e5ae2
AM
4592 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4593 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4594 {
4595 int ret;
4596 const char *soname = elf_dt_name (abfd);
4597
16e4ecc0
AM
4598 info->callbacks->minfo ("%!", soname, old_bfd,
4599 h->root.root.string);
4600
4ad4eba5
AM
4601 /* A symbol from a library loaded via DT_NEEDED of some
4602 other library is referenced by a regular object.
e56f61be 4603 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4604 --no-add-needed is used and the reference was not
4605 a weak one. */
4f3fedcf 4606 if (old_bfd != NULL
b918acf9 4607 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4608 {
4609 (*_bfd_error_handler)
3cbc5de0 4610 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4611 old_bfd, name);
ff5ac77b 4612 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4613 goto error_free_vers;
4614 }
4615
a50b1753 4616 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4617 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4618
4ad4eba5 4619 add_needed = TRUE;
7e9f0867 4620 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4621 if (ret < 0)
4622 goto error_free_vers;
4623
4624 BFD_ASSERT (ret == 0);
4625 }
4626 }
4627 }
4628
66eb6687
AM
4629 if (extversym != NULL)
4630 {
4631 free (extversym);
4632 extversym = NULL;
4633 }
4634
4635 if (isymbuf != NULL)
4636 {
4637 free (isymbuf);
4638 isymbuf = NULL;
4639 }
4640
4641 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4642 {
4643 unsigned int i;
4644
4645 /* Restore the symbol table. */
f45794cb
AM
4646 old_ent = (char *) old_tab + tabsize;
4647 memset (elf_sym_hashes (abfd), 0,
4648 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4649 htab->root.table.table = old_table;
4650 htab->root.table.size = old_size;
4651 htab->root.table.count = old_count;
66eb6687 4652 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4653 htab->root.undefs = old_undefs;
4654 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4655 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4656 for (i = 0; i < htab->root.table.size; i++)
4657 {
4658 struct bfd_hash_entry *p;
4659 struct elf_link_hash_entry *h;
3e0882af
L
4660 bfd_size_type size;
4661 unsigned int alignment_power;
66eb6687
AM
4662
4663 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4664 {
4665 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4666 if (h->root.type == bfd_link_hash_warning)
4667 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4668 if (h->dynindx >= old_dynsymcount
4669 && h->dynstr_index < old_dynstr_size)
66eb6687 4670 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4671
3e0882af
L
4672 /* Preserve the maximum alignment and size for common
4673 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4674 since it can still be loaded at run time by another
3e0882af
L
4675 dynamic lib. */
4676 if (h->root.type == bfd_link_hash_common)
4677 {
4678 size = h->root.u.c.size;
4679 alignment_power = h->root.u.c.p->alignment_power;
4680 }
4681 else
4682 {
4683 size = 0;
4684 alignment_power = 0;
4685 }
66eb6687
AM
4686 memcpy (p, old_ent, htab->root.table.entsize);
4687 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4688 h = (struct elf_link_hash_entry *) p;
4689 if (h->root.type == bfd_link_hash_warning)
4690 {
4691 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4692 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4694 }
a4542f1b 4695 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4696 {
4697 if (size > h->root.u.c.size)
4698 h->root.u.c.size = size;
4699 if (alignment_power > h->root.u.c.p->alignment_power)
4700 h->root.u.c.p->alignment_power = alignment_power;
4701 }
66eb6687
AM
4702 }
4703 }
4704
5061a885
AM
4705 /* Make a special call to the linker "notice" function to
4706 tell it that symbols added for crefs may need to be removed. */
e5034e59 4707 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4708 goto error_free_vers;
5061a885 4709
66eb6687
AM
4710 free (old_tab);
4711 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4712 alloc_mark);
4713 if (nondeflt_vers != NULL)
4714 free (nondeflt_vers);
4715 return TRUE;
4716 }
2de92251 4717
66eb6687
AM
4718 if (old_tab != NULL)
4719 {
e5034e59 4720 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4721 goto error_free_vers;
66eb6687
AM
4722 free (old_tab);
4723 old_tab = NULL;
4724 }
4725
c6e8a9a8
L
4726 /* Now that all the symbols from this input file are created, if
4727 not performing a relocatable link, handle .symver foo, foo@BAR
4728 such that any relocs against foo become foo@BAR. */
0e1862bb 4729 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4730 {
4731 bfd_size_type cnt, symidx;
4732
4733 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4734 {
4735 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4736 char *shortname, *p;
4737
4738 p = strchr (h->root.root.string, ELF_VER_CHR);
4739 if (p == NULL
4740 || (h->root.type != bfd_link_hash_defined
4741 && h->root.type != bfd_link_hash_defweak))
4742 continue;
4743
4744 amt = p - h->root.root.string;
a50b1753 4745 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4746 if (!shortname)
4747 goto error_free_vers;
4ad4eba5
AM
4748 memcpy (shortname, h->root.root.string, amt);
4749 shortname[amt] = '\0';
4750
4751 hi = (struct elf_link_hash_entry *)
66eb6687 4752 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4753 FALSE, FALSE, FALSE);
4754 if (hi != NULL
4755 && hi->root.type == h->root.type
4756 && hi->root.u.def.value == h->root.u.def.value
4757 && hi->root.u.def.section == h->root.u.def.section)
4758 {
4759 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4760 hi->root.type = bfd_link_hash_indirect;
4761 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4762 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4763 sym_hash = elf_sym_hashes (abfd);
4764 if (sym_hash)
4765 for (symidx = 0; symidx < extsymcount; ++symidx)
4766 if (sym_hash[symidx] == hi)
4767 {
4768 sym_hash[symidx] = h;
4769 break;
4770 }
4771 }
4772 free (shortname);
4773 }
4774 free (nondeflt_vers);
4775 nondeflt_vers = NULL;
4776 }
4777
4ad4eba5
AM
4778 /* Now set the weakdefs field correctly for all the weak defined
4779 symbols we found. The only way to do this is to search all the
4780 symbols. Since we only need the information for non functions in
4781 dynamic objects, that's the only time we actually put anything on
4782 the list WEAKS. We need this information so that if a regular
4783 object refers to a symbol defined weakly in a dynamic object, the
4784 real symbol in the dynamic object is also put in the dynamic
4785 symbols; we also must arrange for both symbols to point to the
4786 same memory location. We could handle the general case of symbol
4787 aliasing, but a general symbol alias can only be generated in
4788 assembler code, handling it correctly would be very time
4789 consuming, and other ELF linkers don't handle general aliasing
4790 either. */
4791 if (weaks != NULL)
4792 {
4793 struct elf_link_hash_entry **hpp;
4794 struct elf_link_hash_entry **hppend;
4795 struct elf_link_hash_entry **sorted_sym_hash;
4796 struct elf_link_hash_entry *h;
4797 size_t sym_count;
4798
4799 /* Since we have to search the whole symbol list for each weak
4800 defined symbol, search time for N weak defined symbols will be
4801 O(N^2). Binary search will cut it down to O(NlogN). */
4802 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4803 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4804 if (sorted_sym_hash == NULL)
4805 goto error_return;
4806 sym_hash = sorted_sym_hash;
4807 hpp = elf_sym_hashes (abfd);
4808 hppend = hpp + extsymcount;
4809 sym_count = 0;
4810 for (; hpp < hppend; hpp++)
4811 {
4812 h = *hpp;
4813 if (h != NULL
4814 && h->root.type == bfd_link_hash_defined
fcb93ecf 4815 && !bed->is_function_type (h->type))
4ad4eba5
AM
4816 {
4817 *sym_hash = h;
4818 sym_hash++;
4819 sym_count++;
4820 }
4821 }
4822
4823 qsort (sorted_sym_hash, sym_count,
4824 sizeof (struct elf_link_hash_entry *),
4825 elf_sort_symbol);
4826
4827 while (weaks != NULL)
4828 {
4829 struct elf_link_hash_entry *hlook;
4830 asection *slook;
4831 bfd_vma vlook;
ed54588d 4832 size_t i, j, idx = 0;
4ad4eba5
AM
4833
4834 hlook = weaks;
f6e332e6
AM
4835 weaks = hlook->u.weakdef;
4836 hlook->u.weakdef = NULL;
4ad4eba5
AM
4837
4838 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4839 || hlook->root.type == bfd_link_hash_defweak
4840 || hlook->root.type == bfd_link_hash_common
4841 || hlook->root.type == bfd_link_hash_indirect);
4842 slook = hlook->root.u.def.section;
4843 vlook = hlook->root.u.def.value;
4844
4ad4eba5
AM
4845 i = 0;
4846 j = sym_count;
14160578 4847 while (i != j)
4ad4eba5
AM
4848 {
4849 bfd_signed_vma vdiff;
4850 idx = (i + j) / 2;
14160578 4851 h = sorted_sym_hash[idx];
4ad4eba5
AM
4852 vdiff = vlook - h->root.u.def.value;
4853 if (vdiff < 0)
4854 j = idx;
4855 else if (vdiff > 0)
4856 i = idx + 1;
4857 else
4858 {
d3435ae8 4859 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4860 if (sdiff < 0)
4861 j = idx;
4862 else if (sdiff > 0)
4863 i = idx + 1;
4864 else
14160578 4865 break;
4ad4eba5
AM
4866 }
4867 }
4868
4869 /* We didn't find a value/section match. */
14160578 4870 if (i == j)
4ad4eba5
AM
4871 continue;
4872
14160578
AM
4873 /* With multiple aliases, or when the weak symbol is already
4874 strongly defined, we have multiple matching symbols and
4875 the binary search above may land on any of them. Step
4876 one past the matching symbol(s). */
4877 while (++idx != j)
4878 {
4879 h = sorted_sym_hash[idx];
4880 if (h->root.u.def.section != slook
4881 || h->root.u.def.value != vlook)
4882 break;
4883 }
4884
4885 /* Now look back over the aliases. Since we sorted by size
4886 as well as value and section, we'll choose the one with
4887 the largest size. */
4888 while (idx-- != i)
4ad4eba5 4889 {
14160578 4890 h = sorted_sym_hash[idx];
4ad4eba5
AM
4891
4892 /* Stop if value or section doesn't match. */
14160578
AM
4893 if (h->root.u.def.section != slook
4894 || h->root.u.def.value != vlook)
4ad4eba5
AM
4895 break;
4896 else if (h != hlook)
4897 {
f6e332e6 4898 hlook->u.weakdef = h;
4ad4eba5
AM
4899
4900 /* If the weak definition is in the list of dynamic
4901 symbols, make sure the real definition is put
4902 there as well. */
4903 if (hlook->dynindx != -1 && h->dynindx == -1)
4904 {
c152c796 4905 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4906 {
4907 err_free_sym_hash:
4908 free (sorted_sym_hash);
4909 goto error_return;
4910 }
4ad4eba5
AM
4911 }
4912
4913 /* If the real definition is in the list of dynamic
4914 symbols, make sure the weak definition is put
4915 there as well. If we don't do this, then the
4916 dynamic loader might not merge the entries for the
4917 real definition and the weak definition. */
4918 if (h->dynindx != -1 && hlook->dynindx == -1)
4919 {
c152c796 4920 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4921 goto err_free_sym_hash;
4ad4eba5
AM
4922 }
4923 break;
4924 }
4925 }
4926 }
4927
4928 free (sorted_sym_hash);
4929 }
4930
33177bb1
AM
4931 if (bed->check_directives
4932 && !(*bed->check_directives) (abfd, info))
4933 return FALSE;
85fbca6a 4934
4ad4eba5
AM
4935 /* If this object is the same format as the output object, and it is
4936 not a shared library, then let the backend look through the
4937 relocs.
4938
4939 This is required to build global offset table entries and to
4940 arrange for dynamic relocs. It is not required for the
4941 particular common case of linking non PIC code, even when linking
4942 against shared libraries, but unfortunately there is no way of
4943 knowing whether an object file has been compiled PIC or not.
4944 Looking through the relocs is not particularly time consuming.
4945 The problem is that we must either (1) keep the relocs in memory,
4946 which causes the linker to require additional runtime memory or
4947 (2) read the relocs twice from the input file, which wastes time.
4948 This would be a good case for using mmap.
4949
4950 I have no idea how to handle linking PIC code into a file of a
4951 different format. It probably can't be done. */
4ad4eba5 4952 if (! dynamic
66eb6687 4953 && is_elf_hash_table (htab)
13285a1b 4954 && bed->check_relocs != NULL
39334f3a 4955 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4956 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4957 {
4958 asection *o;
4959
4960 for (o = abfd->sections; o != NULL; o = o->next)
4961 {
4962 Elf_Internal_Rela *internal_relocs;
4963 bfd_boolean ok;
4964
4965 if ((o->flags & SEC_RELOC) == 0
4966 || o->reloc_count == 0
4967 || ((info->strip == strip_all || info->strip == strip_debugger)
4968 && (o->flags & SEC_DEBUGGING) != 0)
4969 || bfd_is_abs_section (o->output_section))
4970 continue;
4971
4972 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4973 info->keep_memory);
4974 if (internal_relocs == NULL)
4975 goto error_return;
4976
66eb6687 4977 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4978
4979 if (elf_section_data (o)->relocs != internal_relocs)
4980 free (internal_relocs);
4981
4982 if (! ok)
4983 goto error_return;
4984 }
4985 }
4986
4987 /* If this is a non-traditional link, try to optimize the handling
4988 of the .stab/.stabstr sections. */
4989 if (! dynamic
4990 && ! info->traditional_format
66eb6687 4991 && is_elf_hash_table (htab)
4ad4eba5
AM
4992 && (info->strip != strip_all && info->strip != strip_debugger))
4993 {
4994 asection *stabstr;
4995
4996 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4997 if (stabstr != NULL)
4998 {
4999 bfd_size_type string_offset = 0;
5000 asection *stab;
5001
5002 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5003 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5004 && (!stab->name[5] ||
5005 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5006 && (stab->flags & SEC_MERGE) == 0
5007 && !bfd_is_abs_section (stab->output_section))
5008 {
5009 struct bfd_elf_section_data *secdata;
5010
5011 secdata = elf_section_data (stab);
66eb6687
AM
5012 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5013 stabstr, &secdata->sec_info,
4ad4eba5
AM
5014 &string_offset))
5015 goto error_return;
5016 if (secdata->sec_info)
dbaa2011 5017 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5018 }
5019 }
5020 }
5021
66eb6687 5022 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5023 {
5024 /* Add this bfd to the loaded list. */
5025 struct elf_link_loaded_list *n;
5026
ca4be51c 5027 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5028 if (n == NULL)
5029 goto error_return;
5030 n->abfd = abfd;
66eb6687
AM
5031 n->next = htab->loaded;
5032 htab->loaded = n;
4ad4eba5
AM
5033 }
5034
5035 return TRUE;
5036
5037 error_free_vers:
66eb6687
AM
5038 if (old_tab != NULL)
5039 free (old_tab);
4ad4eba5
AM
5040 if (nondeflt_vers != NULL)
5041 free (nondeflt_vers);
5042 if (extversym != NULL)
5043 free (extversym);
5044 error_free_sym:
5045 if (isymbuf != NULL)
5046 free (isymbuf);
5047 error_return:
5048 return FALSE;
5049}
5050
8387904d
AM
5051/* Return the linker hash table entry of a symbol that might be
5052 satisfied by an archive symbol. Return -1 on error. */
5053
5054struct elf_link_hash_entry *
5055_bfd_elf_archive_symbol_lookup (bfd *abfd,
5056 struct bfd_link_info *info,
5057 const char *name)
5058{
5059 struct elf_link_hash_entry *h;
5060 char *p, *copy;
5061 size_t len, first;
5062
2a41f396 5063 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5064 if (h != NULL)
5065 return h;
5066
5067 /* If this is a default version (the name contains @@), look up the
5068 symbol again with only one `@' as well as without the version.
5069 The effect is that references to the symbol with and without the
5070 version will be matched by the default symbol in the archive. */
5071
5072 p = strchr (name, ELF_VER_CHR);
5073 if (p == NULL || p[1] != ELF_VER_CHR)
5074 return h;
5075
5076 /* First check with only one `@'. */
5077 len = strlen (name);
a50b1753 5078 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5079 if (copy == NULL)
5080 return (struct elf_link_hash_entry *) 0 - 1;
5081
5082 first = p - name + 1;
5083 memcpy (copy, name, first);
5084 memcpy (copy + first, name + first + 1, len - first);
5085
2a41f396 5086 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5087 if (h == NULL)
5088 {
5089 /* We also need to check references to the symbol without the
5090 version. */
5091 copy[first - 1] = '\0';
5092 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5093 FALSE, FALSE, TRUE);
8387904d
AM
5094 }
5095
5096 bfd_release (abfd, copy);
5097 return h;
5098}
5099
0ad989f9 5100/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5101 don't use _bfd_generic_link_add_archive_symbols because we need to
5102 handle versioned symbols.
0ad989f9
L
5103
5104 Fortunately, ELF archive handling is simpler than that done by
5105 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5106 oddities. In ELF, if we find a symbol in the archive map, and the
5107 symbol is currently undefined, we know that we must pull in that
5108 object file.
5109
5110 Unfortunately, we do have to make multiple passes over the symbol
5111 table until nothing further is resolved. */
5112
4ad4eba5
AM
5113static bfd_boolean
5114elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5115{
5116 symindex c;
13e570f8 5117 unsigned char *included = NULL;
0ad989f9
L
5118 carsym *symdefs;
5119 bfd_boolean loop;
5120 bfd_size_type amt;
8387904d
AM
5121 const struct elf_backend_data *bed;
5122 struct elf_link_hash_entry * (*archive_symbol_lookup)
5123 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5124
5125 if (! bfd_has_map (abfd))
5126 {
5127 /* An empty archive is a special case. */
5128 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5129 return TRUE;
5130 bfd_set_error (bfd_error_no_armap);
5131 return FALSE;
5132 }
5133
5134 /* Keep track of all symbols we know to be already defined, and all
5135 files we know to be already included. This is to speed up the
5136 second and subsequent passes. */
5137 c = bfd_ardata (abfd)->symdef_count;
5138 if (c == 0)
5139 return TRUE;
5140 amt = c;
13e570f8
AM
5141 amt *= sizeof (*included);
5142 included = (unsigned char *) bfd_zmalloc (amt);
5143 if (included == NULL)
5144 return FALSE;
0ad989f9
L
5145
5146 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5147 bed = get_elf_backend_data (abfd);
5148 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5149
5150 do
5151 {
5152 file_ptr last;
5153 symindex i;
5154 carsym *symdef;
5155 carsym *symdefend;
5156
5157 loop = FALSE;
5158 last = -1;
5159
5160 symdef = symdefs;
5161 symdefend = symdef + c;
5162 for (i = 0; symdef < symdefend; symdef++, i++)
5163 {
5164 struct elf_link_hash_entry *h;
5165 bfd *element;
5166 struct bfd_link_hash_entry *undefs_tail;
5167 symindex mark;
5168
13e570f8 5169 if (included[i])
0ad989f9
L
5170 continue;
5171 if (symdef->file_offset == last)
5172 {
5173 included[i] = TRUE;
5174 continue;
5175 }
5176
8387904d
AM
5177 h = archive_symbol_lookup (abfd, info, symdef->name);
5178 if (h == (struct elf_link_hash_entry *) 0 - 1)
5179 goto error_return;
0ad989f9
L
5180
5181 if (h == NULL)
5182 continue;
5183
5184 if (h->root.type == bfd_link_hash_common)
5185 {
5186 /* We currently have a common symbol. The archive map contains
5187 a reference to this symbol, so we may want to include it. We
5188 only want to include it however, if this archive element
5189 contains a definition of the symbol, not just another common
5190 declaration of it.
5191
5192 Unfortunately some archivers (including GNU ar) will put
5193 declarations of common symbols into their archive maps, as
5194 well as real definitions, so we cannot just go by the archive
5195 map alone. Instead we must read in the element's symbol
5196 table and check that to see what kind of symbol definition
5197 this is. */
5198 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5199 continue;
5200 }
5201 else if (h->root.type != bfd_link_hash_undefined)
5202 {
5203 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5204 /* Symbol must be defined. Don't check it again. */
5205 included[i] = TRUE;
0ad989f9
L
5206 continue;
5207 }
5208
5209 /* We need to include this archive member. */
5210 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5211 if (element == NULL)
5212 goto error_return;
5213
5214 if (! bfd_check_format (element, bfd_object))
5215 goto error_return;
5216
0ad989f9
L
5217 undefs_tail = info->hash->undefs_tail;
5218
0e144ba7
AM
5219 if (!(*info->callbacks
5220 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5221 goto error_return;
0e144ba7 5222 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5223 goto error_return;
5224
5225 /* If there are any new undefined symbols, we need to make
5226 another pass through the archive in order to see whether
5227 they can be defined. FIXME: This isn't perfect, because
5228 common symbols wind up on undefs_tail and because an
5229 undefined symbol which is defined later on in this pass
5230 does not require another pass. This isn't a bug, but it
5231 does make the code less efficient than it could be. */
5232 if (undefs_tail != info->hash->undefs_tail)
5233 loop = TRUE;
5234
5235 /* Look backward to mark all symbols from this object file
5236 which we have already seen in this pass. */
5237 mark = i;
5238 do
5239 {
5240 included[mark] = TRUE;
5241 if (mark == 0)
5242 break;
5243 --mark;
5244 }
5245 while (symdefs[mark].file_offset == symdef->file_offset);
5246
5247 /* We mark subsequent symbols from this object file as we go
5248 on through the loop. */
5249 last = symdef->file_offset;
5250 }
5251 }
5252 while (loop);
5253
0ad989f9
L
5254 free (included);
5255
5256 return TRUE;
5257
5258 error_return:
0ad989f9
L
5259 if (included != NULL)
5260 free (included);
5261 return FALSE;
5262}
4ad4eba5
AM
5263
5264/* Given an ELF BFD, add symbols to the global hash table as
5265 appropriate. */
5266
5267bfd_boolean
5268bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5269{
5270 switch (bfd_get_format (abfd))
5271 {
5272 case bfd_object:
5273 return elf_link_add_object_symbols (abfd, info);
5274 case bfd_archive:
5275 return elf_link_add_archive_symbols (abfd, info);
5276 default:
5277 bfd_set_error (bfd_error_wrong_format);
5278 return FALSE;
5279 }
5280}
5a580b3a 5281\f
14b1c01e
AM
5282struct hash_codes_info
5283{
5284 unsigned long *hashcodes;
5285 bfd_boolean error;
5286};
a0c8462f 5287
5a580b3a
AM
5288/* This function will be called though elf_link_hash_traverse to store
5289 all hash value of the exported symbols in an array. */
5290
5291static bfd_boolean
5292elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5293{
a50b1753 5294 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5295 const char *name;
5a580b3a
AM
5296 unsigned long ha;
5297 char *alc = NULL;
5298
5a580b3a
AM
5299 /* Ignore indirect symbols. These are added by the versioning code. */
5300 if (h->dynindx == -1)
5301 return TRUE;
5302
5303 name = h->root.root.string;
422f1182 5304 if (h->versioned >= versioned)
5a580b3a 5305 {
422f1182
L
5306 char *p = strchr (name, ELF_VER_CHR);
5307 if (p != NULL)
14b1c01e 5308 {
422f1182
L
5309 alc = (char *) bfd_malloc (p - name + 1);
5310 if (alc == NULL)
5311 {
5312 inf->error = TRUE;
5313 return FALSE;
5314 }
5315 memcpy (alc, name, p - name);
5316 alc[p - name] = '\0';
5317 name = alc;
14b1c01e 5318 }
5a580b3a
AM
5319 }
5320
5321 /* Compute the hash value. */
5322 ha = bfd_elf_hash (name);
5323
5324 /* Store the found hash value in the array given as the argument. */
14b1c01e 5325 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5326
5327 /* And store it in the struct so that we can put it in the hash table
5328 later. */
f6e332e6 5329 h->u.elf_hash_value = ha;
5a580b3a
AM
5330
5331 if (alc != NULL)
5332 free (alc);
5333
5334 return TRUE;
5335}
5336
fdc90cb4
JJ
5337struct collect_gnu_hash_codes
5338{
5339 bfd *output_bfd;
5340 const struct elf_backend_data *bed;
5341 unsigned long int nsyms;
5342 unsigned long int maskbits;
5343 unsigned long int *hashcodes;
5344 unsigned long int *hashval;
5345 unsigned long int *indx;
5346 unsigned long int *counts;
5347 bfd_vma *bitmask;
5348 bfd_byte *contents;
5349 long int min_dynindx;
5350 unsigned long int bucketcount;
5351 unsigned long int symindx;
5352 long int local_indx;
5353 long int shift1, shift2;
5354 unsigned long int mask;
14b1c01e 5355 bfd_boolean error;
fdc90cb4
JJ
5356};
5357
5358/* This function will be called though elf_link_hash_traverse to store
5359 all hash value of the exported symbols in an array. */
5360
5361static bfd_boolean
5362elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5363{
a50b1753 5364 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5365 const char *name;
fdc90cb4
JJ
5366 unsigned long ha;
5367 char *alc = NULL;
5368
fdc90cb4
JJ
5369 /* Ignore indirect symbols. These are added by the versioning code. */
5370 if (h->dynindx == -1)
5371 return TRUE;
5372
5373 /* Ignore also local symbols and undefined symbols. */
5374 if (! (*s->bed->elf_hash_symbol) (h))
5375 return TRUE;
5376
5377 name = h->root.root.string;
422f1182 5378 if (h->versioned >= versioned)
fdc90cb4 5379 {
422f1182
L
5380 char *p = strchr (name, ELF_VER_CHR);
5381 if (p != NULL)
14b1c01e 5382 {
422f1182
L
5383 alc = (char *) bfd_malloc (p - name + 1);
5384 if (alc == NULL)
5385 {
5386 s->error = TRUE;
5387 return FALSE;
5388 }
5389 memcpy (alc, name, p - name);
5390 alc[p - name] = '\0';
5391 name = alc;
14b1c01e 5392 }
fdc90cb4
JJ
5393 }
5394
5395 /* Compute the hash value. */
5396 ha = bfd_elf_gnu_hash (name);
5397
5398 /* Store the found hash value in the array for compute_bucket_count,
5399 and also for .dynsym reordering purposes. */
5400 s->hashcodes[s->nsyms] = ha;
5401 s->hashval[h->dynindx] = ha;
5402 ++s->nsyms;
5403 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5404 s->min_dynindx = h->dynindx;
5405
5406 if (alc != NULL)
5407 free (alc);
5408
5409 return TRUE;
5410}
5411
5412/* This function will be called though elf_link_hash_traverse to do
5413 final dynaminc symbol renumbering. */
5414
5415static bfd_boolean
5416elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5417{
a50b1753 5418 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5419 unsigned long int bucket;
5420 unsigned long int val;
5421
fdc90cb4
JJ
5422 /* Ignore indirect symbols. */
5423 if (h->dynindx == -1)
5424 return TRUE;
5425
5426 /* Ignore also local symbols and undefined symbols. */
5427 if (! (*s->bed->elf_hash_symbol) (h))
5428 {
5429 if (h->dynindx >= s->min_dynindx)
5430 h->dynindx = s->local_indx++;
5431 return TRUE;
5432 }
5433
5434 bucket = s->hashval[h->dynindx] % s->bucketcount;
5435 val = (s->hashval[h->dynindx] >> s->shift1)
5436 & ((s->maskbits >> s->shift1) - 1);
5437 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5438 s->bitmask[val]
5439 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5440 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5441 if (s->counts[bucket] == 1)
5442 /* Last element terminates the chain. */
5443 val |= 1;
5444 bfd_put_32 (s->output_bfd, val,
5445 s->contents + (s->indx[bucket] - s->symindx) * 4);
5446 --s->counts[bucket];
5447 h->dynindx = s->indx[bucket]++;
5448 return TRUE;
5449}
5450
5451/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5452
5453bfd_boolean
5454_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5455{
5456 return !(h->forced_local
5457 || h->root.type == bfd_link_hash_undefined
5458 || h->root.type == bfd_link_hash_undefweak
5459 || ((h->root.type == bfd_link_hash_defined
5460 || h->root.type == bfd_link_hash_defweak)
5461 && h->root.u.def.section->output_section == NULL));
5462}
5463
5a580b3a
AM
5464/* Array used to determine the number of hash table buckets to use
5465 based on the number of symbols there are. If there are fewer than
5466 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5467 fewer than 37 we use 17 buckets, and so forth. We never use more
5468 than 32771 buckets. */
5469
5470static const size_t elf_buckets[] =
5471{
5472 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5473 16411, 32771, 0
5474};
5475
5476/* Compute bucket count for hashing table. We do not use a static set
5477 of possible tables sizes anymore. Instead we determine for all
5478 possible reasonable sizes of the table the outcome (i.e., the
5479 number of collisions etc) and choose the best solution. The
5480 weighting functions are not too simple to allow the table to grow
5481 without bounds. Instead one of the weighting factors is the size.
5482 Therefore the result is always a good payoff between few collisions
5483 (= short chain lengths) and table size. */
5484static size_t
b20dd2ce 5485compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5486 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5487 unsigned long int nsyms,
5488 int gnu_hash)
5a580b3a 5489{
5a580b3a 5490 size_t best_size = 0;
5a580b3a 5491 unsigned long int i;
5a580b3a 5492
5a580b3a
AM
5493 /* We have a problem here. The following code to optimize the table
5494 size requires an integer type with more the 32 bits. If
5495 BFD_HOST_U_64_BIT is set we know about such a type. */
5496#ifdef BFD_HOST_U_64_BIT
5497 if (info->optimize)
5498 {
5a580b3a
AM
5499 size_t minsize;
5500 size_t maxsize;
5501 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5502 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5503 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5504 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5505 unsigned long int *counts;
d40f3da9 5506 bfd_size_type amt;
0883b6e0 5507 unsigned int no_improvement_count = 0;
5a580b3a
AM
5508
5509 /* Possible optimization parameters: if we have NSYMS symbols we say
5510 that the hashing table must at least have NSYMS/4 and at most
5511 2*NSYMS buckets. */
5512 minsize = nsyms / 4;
5513 if (minsize == 0)
5514 minsize = 1;
5515 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5516 if (gnu_hash)
5517 {
5518 if (minsize < 2)
5519 minsize = 2;
5520 if ((best_size & 31) == 0)
5521 ++best_size;
5522 }
5a580b3a
AM
5523
5524 /* Create array where we count the collisions in. We must use bfd_malloc
5525 since the size could be large. */
5526 amt = maxsize;
5527 amt *= sizeof (unsigned long int);
a50b1753 5528 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5529 if (counts == NULL)
fdc90cb4 5530 return 0;
5a580b3a
AM
5531
5532 /* Compute the "optimal" size for the hash table. The criteria is a
5533 minimal chain length. The minor criteria is (of course) the size
5534 of the table. */
5535 for (i = minsize; i < maxsize; ++i)
5536 {
5537 /* Walk through the array of hashcodes and count the collisions. */
5538 BFD_HOST_U_64_BIT max;
5539 unsigned long int j;
5540 unsigned long int fact;
5541
fdc90cb4
JJ
5542 if (gnu_hash && (i & 31) == 0)
5543 continue;
5544
5a580b3a
AM
5545 memset (counts, '\0', i * sizeof (unsigned long int));
5546
5547 /* Determine how often each hash bucket is used. */
5548 for (j = 0; j < nsyms; ++j)
5549 ++counts[hashcodes[j] % i];
5550
5551 /* For the weight function we need some information about the
5552 pagesize on the target. This is information need not be 100%
5553 accurate. Since this information is not available (so far) we
5554 define it here to a reasonable default value. If it is crucial
5555 to have a better value some day simply define this value. */
5556# ifndef BFD_TARGET_PAGESIZE
5557# define BFD_TARGET_PAGESIZE (4096)
5558# endif
5559
fdc90cb4
JJ
5560 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5561 and the chains. */
5562 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5563
5564# if 1
5565 /* Variant 1: optimize for short chains. We add the squares
5566 of all the chain lengths (which favors many small chain
5567 over a few long chains). */
5568 for (j = 0; j < i; ++j)
5569 max += counts[j] * counts[j];
5570
5571 /* This adds penalties for the overall size of the table. */
fdc90cb4 5572 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5573 max *= fact * fact;
5574# else
5575 /* Variant 2: Optimize a lot more for small table. Here we
5576 also add squares of the size but we also add penalties for
5577 empty slots (the +1 term). */
5578 for (j = 0; j < i; ++j)
5579 max += (1 + counts[j]) * (1 + counts[j]);
5580
5581 /* The overall size of the table is considered, but not as
5582 strong as in variant 1, where it is squared. */
fdc90cb4 5583 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5584 max *= fact;
5585# endif
5586
5587 /* Compare with current best results. */
5588 if (max < best_chlen)
5589 {
5590 best_chlen = max;
5591 best_size = i;
ca4be51c 5592 no_improvement_count = 0;
5a580b3a 5593 }
0883b6e0
NC
5594 /* PR 11843: Avoid futile long searches for the best bucket size
5595 when there are a large number of symbols. */
5596 else if (++no_improvement_count == 100)
5597 break;
5a580b3a
AM
5598 }
5599
5600 free (counts);
5601 }
5602 else
5603#endif /* defined (BFD_HOST_U_64_BIT) */
5604 {
5605 /* This is the fallback solution if no 64bit type is available or if we
5606 are not supposed to spend much time on optimizations. We select the
5607 bucket count using a fixed set of numbers. */
5608 for (i = 0; elf_buckets[i] != 0; i++)
5609 {
5610 best_size = elf_buckets[i];
fdc90cb4 5611 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5612 break;
5613 }
fdc90cb4
JJ
5614 if (gnu_hash && best_size < 2)
5615 best_size = 2;
5a580b3a
AM
5616 }
5617
5a580b3a
AM
5618 return best_size;
5619}
5620
d0bf826b
AM
5621/* Size any SHT_GROUP section for ld -r. */
5622
5623bfd_boolean
5624_bfd_elf_size_group_sections (struct bfd_link_info *info)
5625{
5626 bfd *ibfd;
5627
c72f2fb2 5628 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5629 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5630 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5631 return FALSE;
5632 return TRUE;
5633}
5634
04c3a755
NS
5635/* Set a default stack segment size. The value in INFO wins. If it
5636 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5637 undefined it is initialized. */
5638
5639bfd_boolean
5640bfd_elf_stack_segment_size (bfd *output_bfd,
5641 struct bfd_link_info *info,
5642 const char *legacy_symbol,
5643 bfd_vma default_size)
5644{
5645 struct elf_link_hash_entry *h = NULL;
5646
5647 /* Look for legacy symbol. */
5648 if (legacy_symbol)
5649 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5650 FALSE, FALSE, FALSE);
5651 if (h && (h->root.type == bfd_link_hash_defined
5652 || h->root.type == bfd_link_hash_defweak)
5653 && h->def_regular
5654 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5655 {
5656 /* The symbol has no type if specified on the command line. */
5657 h->type = STT_OBJECT;
5658 if (info->stacksize)
5659 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5660 output_bfd, legacy_symbol);
5661 else if (h->root.u.def.section != bfd_abs_section_ptr)
5662 (*_bfd_error_handler) (_("%B: %s not absolute"),
5663 output_bfd, legacy_symbol);
5664 else
5665 info->stacksize = h->root.u.def.value;
5666 }
5667
5668 if (!info->stacksize)
5669 /* If the user didn't set a size, or explicitly inhibit the
5670 size, set it now. */
5671 info->stacksize = default_size;
5672
5673 /* Provide the legacy symbol, if it is referenced. */
5674 if (h && (h->root.type == bfd_link_hash_undefined
5675 || h->root.type == bfd_link_hash_undefweak))
5676 {
5677 struct bfd_link_hash_entry *bh = NULL;
5678
5679 if (!(_bfd_generic_link_add_one_symbol
5680 (info, output_bfd, legacy_symbol,
5681 BSF_GLOBAL, bfd_abs_section_ptr,
5682 info->stacksize >= 0 ? info->stacksize : 0,
5683 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5684 return FALSE;
5685
5686 h = (struct elf_link_hash_entry *) bh;
5687 h->def_regular = 1;
5688 h->type = STT_OBJECT;
5689 }
5690
5691 return TRUE;
5692}
5693
5a580b3a
AM
5694/* Set up the sizes and contents of the ELF dynamic sections. This is
5695 called by the ELF linker emulation before_allocation routine. We
5696 must set the sizes of the sections before the linker sets the
5697 addresses of the various sections. */
5698
5699bfd_boolean
5700bfd_elf_size_dynamic_sections (bfd *output_bfd,
5701 const char *soname,
5702 const char *rpath,
5703 const char *filter_shlib,
7ee314fa
AM
5704 const char *audit,
5705 const char *depaudit,
5a580b3a
AM
5706 const char * const *auxiliary_filters,
5707 struct bfd_link_info *info,
fd91d419 5708 asection **sinterpptr)
5a580b3a
AM
5709{
5710 bfd_size_type soname_indx;
5711 bfd *dynobj;
5712 const struct elf_backend_data *bed;
28caa186 5713 struct elf_info_failed asvinfo;
5a580b3a
AM
5714
5715 *sinterpptr = NULL;
5716
5717 soname_indx = (bfd_size_type) -1;
5718
5719 if (!is_elf_hash_table (info->hash))
5720 return TRUE;
5721
6bfdb61b 5722 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5723
5724 /* Any syms created from now on start with -1 in
5725 got.refcount/offset and plt.refcount/offset. */
5726 elf_hash_table (info)->init_got_refcount
5727 = elf_hash_table (info)->init_got_offset;
5728 elf_hash_table (info)->init_plt_refcount
5729 = elf_hash_table (info)->init_plt_offset;
5730
0e1862bb 5731 if (bfd_link_relocatable (info)
04c3a755
NS
5732 && !_bfd_elf_size_group_sections (info))
5733 return FALSE;
5734
5735 /* The backend may have to create some sections regardless of whether
5736 we're dynamic or not. */
5737 if (bed->elf_backend_always_size_sections
5738 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5739 return FALSE;
5740
5741 /* Determine any GNU_STACK segment requirements, after the backend
5742 has had a chance to set a default segment size. */
5a580b3a 5743 if (info->execstack)
12bd6957 5744 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5745 else if (info->noexecstack)
12bd6957 5746 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5747 else
5748 {
5749 bfd *inputobj;
5750 asection *notesec = NULL;
5751 int exec = 0;
5752
5753 for (inputobj = info->input_bfds;
5754 inputobj;
c72f2fb2 5755 inputobj = inputobj->link.next)
5a580b3a
AM
5756 {
5757 asection *s;
5758
a92c088a
L
5759 if (inputobj->flags
5760 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5761 continue;
5762 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5763 if (s)
5764 {
5765 if (s->flags & SEC_CODE)
5766 exec = PF_X;
5767 notesec = s;
5768 }
6bfdb61b 5769 else if (bed->default_execstack)
5a580b3a
AM
5770 exec = PF_X;
5771 }
04c3a755 5772 if (notesec || info->stacksize > 0)
12bd6957 5773 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5774 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5775 && notesec->output_section != bfd_abs_section_ptr)
5776 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5777 }
5778
5a580b3a
AM
5779 dynobj = elf_hash_table (info)->dynobj;
5780
9a2a56cc 5781 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5782 {
5783 struct elf_info_failed eif;
5784 struct elf_link_hash_entry *h;
5785 asection *dynstr;
5786 struct bfd_elf_version_tree *t;
5787 struct bfd_elf_version_expr *d;
046183de 5788 asection *s;
5a580b3a
AM
5789 bfd_boolean all_defined;
5790
3d4d4302 5791 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5792 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5793
5794 if (soname != NULL)
5795 {
5796 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5797 soname, TRUE);
5798 if (soname_indx == (bfd_size_type) -1
5799 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5800 return FALSE;
5801 }
5802
5803 if (info->symbolic)
5804 {
5805 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5806 return FALSE;
5807 info->flags |= DF_SYMBOLIC;
5808 }
5809
5810 if (rpath != NULL)
5811 {
5812 bfd_size_type indx;
b1b00fcc 5813 bfd_vma tag;
5a580b3a
AM
5814
5815 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5816 TRUE);
b1b00fcc 5817 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5818 return FALSE;
5819
b1b00fcc
MF
5820 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5821 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5822 return FALSE;
5a580b3a
AM
5823 }
5824
5825 if (filter_shlib != NULL)
5826 {
5827 bfd_size_type indx;
5828
5829 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5830 filter_shlib, TRUE);
5831 if (indx == (bfd_size_type) -1
5832 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5833 return FALSE;
5834 }
5835
5836 if (auxiliary_filters != NULL)
5837 {
5838 const char * const *p;
5839
5840 for (p = auxiliary_filters; *p != NULL; p++)
5841 {
5842 bfd_size_type indx;
5843
5844 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5845 *p, TRUE);
5846 if (indx == (bfd_size_type) -1
5847 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5848 return FALSE;
5849 }
5850 }
5851
7ee314fa
AM
5852 if (audit != NULL)
5853 {
5854 bfd_size_type indx;
5855
5856 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5857 TRUE);
5858 if (indx == (bfd_size_type) -1
5859 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5860 return FALSE;
5861 }
5862
5863 if (depaudit != NULL)
5864 {
5865 bfd_size_type indx;
5866
5867 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5868 TRUE);
5869 if (indx == (bfd_size_type) -1
5870 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5871 return FALSE;
5872 }
5873
5a580b3a 5874 eif.info = info;
5a580b3a
AM
5875 eif.failed = FALSE;
5876
5877 /* If we are supposed to export all symbols into the dynamic symbol
5878 table (this is not the normal case), then do so. */
55255dae 5879 if (info->export_dynamic
0e1862bb 5880 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5881 {
5882 elf_link_hash_traverse (elf_hash_table (info),
5883 _bfd_elf_export_symbol,
5884 &eif);
5885 if (eif.failed)
5886 return FALSE;
5887 }
5888
5889 /* Make all global versions with definition. */
fd91d419 5890 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5891 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5892 if (!d->symver && d->literal)
5a580b3a
AM
5893 {
5894 const char *verstr, *name;
5895 size_t namelen, verlen, newlen;
93252b1c 5896 char *newname, *p, leading_char;
5a580b3a
AM
5897 struct elf_link_hash_entry *newh;
5898
93252b1c 5899 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5900 name = d->pattern;
93252b1c 5901 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5902 verstr = t->name;
5903 verlen = strlen (verstr);
5904 newlen = namelen + verlen + 3;
5905
a50b1753 5906 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5907 if (newname == NULL)
5908 return FALSE;
93252b1c
MF
5909 newname[0] = leading_char;
5910 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5911
5912 /* Check the hidden versioned definition. */
5913 p = newname + namelen;
5914 *p++ = ELF_VER_CHR;
5915 memcpy (p, verstr, verlen + 1);
5916 newh = elf_link_hash_lookup (elf_hash_table (info),
5917 newname, FALSE, FALSE,
5918 FALSE);
5919 if (newh == NULL
5920 || (newh->root.type != bfd_link_hash_defined
5921 && newh->root.type != bfd_link_hash_defweak))
5922 {
5923 /* Check the default versioned definition. */
5924 *p++ = ELF_VER_CHR;
5925 memcpy (p, verstr, verlen + 1);
5926 newh = elf_link_hash_lookup (elf_hash_table (info),
5927 newname, FALSE, FALSE,
5928 FALSE);
5929 }
5930 free (newname);
5931
5932 /* Mark this version if there is a definition and it is
5933 not defined in a shared object. */
5934 if (newh != NULL
f5385ebf 5935 && !newh->def_dynamic
5a580b3a
AM
5936 && (newh->root.type == bfd_link_hash_defined
5937 || newh->root.type == bfd_link_hash_defweak))
5938 d->symver = 1;
5939 }
5940
5941 /* Attach all the symbols to their version information. */
5a580b3a 5942 asvinfo.info = info;
5a580b3a
AM
5943 asvinfo.failed = FALSE;
5944
5945 elf_link_hash_traverse (elf_hash_table (info),
5946 _bfd_elf_link_assign_sym_version,
5947 &asvinfo);
5948 if (asvinfo.failed)
5949 return FALSE;
5950
5951 if (!info->allow_undefined_version)
5952 {
5953 /* Check if all global versions have a definition. */
5954 all_defined = TRUE;
fd91d419 5955 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5956 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5957 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5958 {
5959 (*_bfd_error_handler)
5960 (_("%s: undefined version: %s"),
5961 d->pattern, t->name);
5962 all_defined = FALSE;
5963 }
5964
5965 if (!all_defined)
5966 {
5967 bfd_set_error (bfd_error_bad_value);
5968 return FALSE;
5969 }
5970 }
5971
5972 /* Find all symbols which were defined in a dynamic object and make
5973 the backend pick a reasonable value for them. */
5974 elf_link_hash_traverse (elf_hash_table (info),
5975 _bfd_elf_adjust_dynamic_symbol,
5976 &eif);
5977 if (eif.failed)
5978 return FALSE;
5979
5980 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5981 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5982 now so that we know the final size of the .dynamic section. */
5983
5984 /* If there are initialization and/or finalization functions to
5985 call then add the corresponding DT_INIT/DT_FINI entries. */
5986 h = (info->init_function
5987 ? elf_link_hash_lookup (elf_hash_table (info),
5988 info->init_function, FALSE,
5989 FALSE, FALSE)
5990 : NULL);
5991 if (h != NULL
f5385ebf
AM
5992 && (h->ref_regular
5993 || h->def_regular))
5a580b3a
AM
5994 {
5995 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5996 return FALSE;
5997 }
5998 h = (info->fini_function
5999 ? elf_link_hash_lookup (elf_hash_table (info),
6000 info->fini_function, FALSE,
6001 FALSE, FALSE)
6002 : NULL);
6003 if (h != NULL
f5385ebf
AM
6004 && (h->ref_regular
6005 || h->def_regular))
5a580b3a
AM
6006 {
6007 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6008 return FALSE;
6009 }
6010
046183de
AM
6011 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6012 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6013 {
6014 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6015 if (! bfd_link_executable (info))
5a580b3a
AM
6016 {
6017 bfd *sub;
6018 asection *o;
6019
6020 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6021 sub = sub->link.next)
3fcd97f1
JJ
6022 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6023 for (o = sub->sections; o != NULL; o = o->next)
6024 if (elf_section_data (o)->this_hdr.sh_type
6025 == SHT_PREINIT_ARRAY)
6026 {
6027 (*_bfd_error_handler)
6028 (_("%B: .preinit_array section is not allowed in DSO"),
6029 sub);
6030 break;
6031 }
5a580b3a
AM
6032
6033 bfd_set_error (bfd_error_nonrepresentable_section);
6034 return FALSE;
6035 }
6036
6037 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6038 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6039 return FALSE;
6040 }
046183de
AM
6041 s = bfd_get_section_by_name (output_bfd, ".init_array");
6042 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6043 {
6044 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6045 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6046 return FALSE;
6047 }
046183de
AM
6048 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6049 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6050 {
6051 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6052 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6053 return FALSE;
6054 }
6055
3d4d4302 6056 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6057 /* If .dynstr is excluded from the link, we don't want any of
6058 these tags. Strictly, we should be checking each section
6059 individually; This quick check covers for the case where
6060 someone does a /DISCARD/ : { *(*) }. */
6061 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6062 {
6063 bfd_size_type strsize;
6064
6065 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6066 if ((info->emit_hash
6067 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6068 || (info->emit_gnu_hash
6069 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6070 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6071 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6072 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6073 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6074 bed->s->sizeof_sym))
6075 return FALSE;
6076 }
6077 }
6078
de231f20
CM
6079 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6080 return FALSE;
6081
5a580b3a
AM
6082 /* The backend must work out the sizes of all the other dynamic
6083 sections. */
9a2a56cc
AM
6084 if (dynobj != NULL
6085 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6086 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6087 return FALSE;
6088
9a2a56cc 6089 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6090 {
554220db 6091 unsigned long section_sym_count;
fd91d419 6092 struct bfd_elf_version_tree *verdefs;
5a580b3a 6093 asection *s;
5a580b3a
AM
6094
6095 /* Set up the version definition section. */
3d4d4302 6096 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6097 BFD_ASSERT (s != NULL);
6098
6099 /* We may have created additional version definitions if we are
6100 just linking a regular application. */
fd91d419 6101 verdefs = info->version_info;
5a580b3a
AM
6102
6103 /* Skip anonymous version tag. */
6104 if (verdefs != NULL && verdefs->vernum == 0)
6105 verdefs = verdefs->next;
6106
3e3b46e5 6107 if (verdefs == NULL && !info->create_default_symver)
8423293d 6108 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6109 else
6110 {
6111 unsigned int cdefs;
6112 bfd_size_type size;
6113 struct bfd_elf_version_tree *t;
6114 bfd_byte *p;
6115 Elf_Internal_Verdef def;
6116 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6117 struct bfd_link_hash_entry *bh;
6118 struct elf_link_hash_entry *h;
6119 const char *name;
5a580b3a
AM
6120
6121 cdefs = 0;
6122 size = 0;
6123
6124 /* Make space for the base version. */
6125 size += sizeof (Elf_External_Verdef);
6126 size += sizeof (Elf_External_Verdaux);
6127 ++cdefs;
6128
3e3b46e5
PB
6129 /* Make space for the default version. */
6130 if (info->create_default_symver)
6131 {
6132 size += sizeof (Elf_External_Verdef);
6133 ++cdefs;
6134 }
6135
5a580b3a
AM
6136 for (t = verdefs; t != NULL; t = t->next)
6137 {
6138 struct bfd_elf_version_deps *n;
6139
a6cc6b3b
RO
6140 /* Don't emit base version twice. */
6141 if (t->vernum == 0)
6142 continue;
6143
5a580b3a
AM
6144 size += sizeof (Elf_External_Verdef);
6145 size += sizeof (Elf_External_Verdaux);
6146 ++cdefs;
6147
6148 for (n = t->deps; n != NULL; n = n->next)
6149 size += sizeof (Elf_External_Verdaux);
6150 }
6151
eea6121a 6152 s->size = size;
a50b1753 6153 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6154 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6155 return FALSE;
6156
6157 /* Fill in the version definition section. */
6158
6159 p = s->contents;
6160
6161 def.vd_version = VER_DEF_CURRENT;
6162 def.vd_flags = VER_FLG_BASE;
6163 def.vd_ndx = 1;
6164 def.vd_cnt = 1;
3e3b46e5
PB
6165 if (info->create_default_symver)
6166 {
6167 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6168 def.vd_next = sizeof (Elf_External_Verdef);
6169 }
6170 else
6171 {
6172 def.vd_aux = sizeof (Elf_External_Verdef);
6173 def.vd_next = (sizeof (Elf_External_Verdef)
6174 + sizeof (Elf_External_Verdaux));
6175 }
5a580b3a
AM
6176
6177 if (soname_indx != (bfd_size_type) -1)
6178 {
6179 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6180 soname_indx);
6181 def.vd_hash = bfd_elf_hash (soname);
6182 defaux.vda_name = soname_indx;
3e3b46e5 6183 name = soname;
5a580b3a
AM
6184 }
6185 else
6186 {
5a580b3a
AM
6187 bfd_size_type indx;
6188
06084812 6189 name = lbasename (output_bfd->filename);
5a580b3a
AM
6190 def.vd_hash = bfd_elf_hash (name);
6191 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6192 name, FALSE);
6193 if (indx == (bfd_size_type) -1)
6194 return FALSE;
6195 defaux.vda_name = indx;
6196 }
6197 defaux.vda_next = 0;
6198
6199 _bfd_elf_swap_verdef_out (output_bfd, &def,
6200 (Elf_External_Verdef *) p);
6201 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6202 if (info->create_default_symver)
6203 {
6204 /* Add a symbol representing this version. */
6205 bh = NULL;
6206 if (! (_bfd_generic_link_add_one_symbol
6207 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6208 0, NULL, FALSE,
6209 get_elf_backend_data (dynobj)->collect, &bh)))
6210 return FALSE;
6211 h = (struct elf_link_hash_entry *) bh;
6212 h->non_elf = 0;
6213 h->def_regular = 1;
6214 h->type = STT_OBJECT;
6215 h->verinfo.vertree = NULL;
6216
6217 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6218 return FALSE;
6219
6220 /* Create a duplicate of the base version with the same
6221 aux block, but different flags. */
6222 def.vd_flags = 0;
6223 def.vd_ndx = 2;
6224 def.vd_aux = sizeof (Elf_External_Verdef);
6225 if (verdefs)
6226 def.vd_next = (sizeof (Elf_External_Verdef)
6227 + sizeof (Elf_External_Verdaux));
6228 else
6229 def.vd_next = 0;
6230 _bfd_elf_swap_verdef_out (output_bfd, &def,
6231 (Elf_External_Verdef *) p);
6232 p += sizeof (Elf_External_Verdef);
6233 }
5a580b3a
AM
6234 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6235 (Elf_External_Verdaux *) p);
6236 p += sizeof (Elf_External_Verdaux);
6237
6238 for (t = verdefs; t != NULL; t = t->next)
6239 {
6240 unsigned int cdeps;
6241 struct bfd_elf_version_deps *n;
5a580b3a 6242
a6cc6b3b
RO
6243 /* Don't emit the base version twice. */
6244 if (t->vernum == 0)
6245 continue;
6246
5a580b3a
AM
6247 cdeps = 0;
6248 for (n = t->deps; n != NULL; n = n->next)
6249 ++cdeps;
6250
6251 /* Add a symbol representing this version. */
6252 bh = NULL;
6253 if (! (_bfd_generic_link_add_one_symbol
6254 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6255 0, NULL, FALSE,
6256 get_elf_backend_data (dynobj)->collect, &bh)))
6257 return FALSE;
6258 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6259 h->non_elf = 0;
6260 h->def_regular = 1;
5a580b3a
AM
6261 h->type = STT_OBJECT;
6262 h->verinfo.vertree = t;
6263
c152c796 6264 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6265 return FALSE;
6266
6267 def.vd_version = VER_DEF_CURRENT;
6268 def.vd_flags = 0;
6269 if (t->globals.list == NULL
6270 && t->locals.list == NULL
6271 && ! t->used)
6272 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6273 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6274 def.vd_cnt = cdeps + 1;
6275 def.vd_hash = bfd_elf_hash (t->name);
6276 def.vd_aux = sizeof (Elf_External_Verdef);
6277 def.vd_next = 0;
a6cc6b3b
RO
6278
6279 /* If a basever node is next, it *must* be the last node in
6280 the chain, otherwise Verdef construction breaks. */
6281 if (t->next != NULL && t->next->vernum == 0)
6282 BFD_ASSERT (t->next->next == NULL);
6283
6284 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6285 def.vd_next = (sizeof (Elf_External_Verdef)
6286 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6287
6288 _bfd_elf_swap_verdef_out (output_bfd, &def,
6289 (Elf_External_Verdef *) p);
6290 p += sizeof (Elf_External_Verdef);
6291
6292 defaux.vda_name = h->dynstr_index;
6293 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6294 h->dynstr_index);
6295 defaux.vda_next = 0;
6296 if (t->deps != NULL)
6297 defaux.vda_next = sizeof (Elf_External_Verdaux);
6298 t->name_indx = defaux.vda_name;
6299
6300 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6301 (Elf_External_Verdaux *) p);
6302 p += sizeof (Elf_External_Verdaux);
6303
6304 for (n = t->deps; n != NULL; n = n->next)
6305 {
6306 if (n->version_needed == NULL)
6307 {
6308 /* This can happen if there was an error in the
6309 version script. */
6310 defaux.vda_name = 0;
6311 }
6312 else
6313 {
6314 defaux.vda_name = n->version_needed->name_indx;
6315 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6316 defaux.vda_name);
6317 }
6318 if (n->next == NULL)
6319 defaux.vda_next = 0;
6320 else
6321 defaux.vda_next = sizeof (Elf_External_Verdaux);
6322
6323 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6324 (Elf_External_Verdaux *) p);
6325 p += sizeof (Elf_External_Verdaux);
6326 }
6327 }
6328
6329 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6330 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6331 return FALSE;
6332
6333 elf_tdata (output_bfd)->cverdefs = cdefs;
6334 }
6335
6336 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6337 {
6338 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6339 return FALSE;
6340 }
6341 else if (info->flags & DF_BIND_NOW)
6342 {
6343 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6344 return FALSE;
6345 }
6346
6347 if (info->flags_1)
6348 {
0e1862bb 6349 if (bfd_link_executable (info))
5a580b3a
AM
6350 info->flags_1 &= ~ (DF_1_INITFIRST
6351 | DF_1_NODELETE
6352 | DF_1_NOOPEN);
6353 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6354 return FALSE;
6355 }
6356
6357 /* Work out the size of the version reference section. */
6358
3d4d4302 6359 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6360 BFD_ASSERT (s != NULL);
6361 {
6362 struct elf_find_verdep_info sinfo;
6363
5a580b3a
AM
6364 sinfo.info = info;
6365 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6366 if (sinfo.vers == 0)
6367 sinfo.vers = 1;
6368 sinfo.failed = FALSE;
6369
6370 elf_link_hash_traverse (elf_hash_table (info),
6371 _bfd_elf_link_find_version_dependencies,
6372 &sinfo);
14b1c01e
AM
6373 if (sinfo.failed)
6374 return FALSE;
5a580b3a
AM
6375
6376 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6377 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6378 else
6379 {
6380 Elf_Internal_Verneed *t;
6381 unsigned int size;
6382 unsigned int crefs;
6383 bfd_byte *p;
6384
a6cc6b3b 6385 /* Build the version dependency section. */
5a580b3a
AM
6386 size = 0;
6387 crefs = 0;
6388 for (t = elf_tdata (output_bfd)->verref;
6389 t != NULL;
6390 t = t->vn_nextref)
6391 {
6392 Elf_Internal_Vernaux *a;
6393
6394 size += sizeof (Elf_External_Verneed);
6395 ++crefs;
6396 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6397 size += sizeof (Elf_External_Vernaux);
6398 }
6399
eea6121a 6400 s->size = size;
a50b1753 6401 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6402 if (s->contents == NULL)
6403 return FALSE;
6404
6405 p = s->contents;
6406 for (t = elf_tdata (output_bfd)->verref;
6407 t != NULL;
6408 t = t->vn_nextref)
6409 {
6410 unsigned int caux;
6411 Elf_Internal_Vernaux *a;
6412 bfd_size_type indx;
6413
6414 caux = 0;
6415 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6416 ++caux;
6417
6418 t->vn_version = VER_NEED_CURRENT;
6419 t->vn_cnt = caux;
6420 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6421 elf_dt_name (t->vn_bfd) != NULL
6422 ? elf_dt_name (t->vn_bfd)
06084812 6423 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6424 FALSE);
6425 if (indx == (bfd_size_type) -1)
6426 return FALSE;
6427 t->vn_file = indx;
6428 t->vn_aux = sizeof (Elf_External_Verneed);
6429 if (t->vn_nextref == NULL)
6430 t->vn_next = 0;
6431 else
6432 t->vn_next = (sizeof (Elf_External_Verneed)
6433 + caux * sizeof (Elf_External_Vernaux));
6434
6435 _bfd_elf_swap_verneed_out (output_bfd, t,
6436 (Elf_External_Verneed *) p);
6437 p += sizeof (Elf_External_Verneed);
6438
6439 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6440 {
6441 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6442 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6443 a->vna_nodename, FALSE);
6444 if (indx == (bfd_size_type) -1)
6445 return FALSE;
6446 a->vna_name = indx;
6447 if (a->vna_nextptr == NULL)
6448 a->vna_next = 0;
6449 else
6450 a->vna_next = sizeof (Elf_External_Vernaux);
6451
6452 _bfd_elf_swap_vernaux_out (output_bfd, a,
6453 (Elf_External_Vernaux *) p);
6454 p += sizeof (Elf_External_Vernaux);
6455 }
6456 }
6457
6458 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6459 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6460 return FALSE;
6461
6462 elf_tdata (output_bfd)->cverrefs = crefs;
6463 }
6464 }
6465
8423293d
AM
6466 if ((elf_tdata (output_bfd)->cverrefs == 0
6467 && elf_tdata (output_bfd)->cverdefs == 0)
6468 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6469 &section_sym_count) == 0)
6470 {
3d4d4302 6471 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6472 s->flags |= SEC_EXCLUDE;
6473 }
6474 }
6475 return TRUE;
6476}
6477
74541ad4
AM
6478/* Find the first non-excluded output section. We'll use its
6479 section symbol for some emitted relocs. */
6480void
6481_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6482{
6483 asection *s;
6484
6485 for (s = output_bfd->sections; s != NULL; s = s->next)
6486 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6487 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6488 {
6489 elf_hash_table (info)->text_index_section = s;
6490 break;
6491 }
6492}
6493
6494/* Find two non-excluded output sections, one for code, one for data.
6495 We'll use their section symbols for some emitted relocs. */
6496void
6497_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6498{
6499 asection *s;
6500
266b05cf
DJ
6501 /* Data first, since setting text_index_section changes
6502 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6503 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6504 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6505 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6506 {
266b05cf 6507 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6508 break;
6509 }
6510
6511 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6512 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6513 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6514 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6515 {
266b05cf 6516 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6517 break;
6518 }
6519
6520 if (elf_hash_table (info)->text_index_section == NULL)
6521 elf_hash_table (info)->text_index_section
6522 = elf_hash_table (info)->data_index_section;
6523}
6524
8423293d
AM
6525bfd_boolean
6526bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6527{
74541ad4
AM
6528 const struct elf_backend_data *bed;
6529
8423293d
AM
6530 if (!is_elf_hash_table (info->hash))
6531 return TRUE;
6532
74541ad4
AM
6533 bed = get_elf_backend_data (output_bfd);
6534 (*bed->elf_backend_init_index_section) (output_bfd, info);
6535
8423293d
AM
6536 if (elf_hash_table (info)->dynamic_sections_created)
6537 {
6538 bfd *dynobj;
8423293d
AM
6539 asection *s;
6540 bfd_size_type dynsymcount;
6541 unsigned long section_sym_count;
8423293d
AM
6542 unsigned int dtagcount;
6543
6544 dynobj = elf_hash_table (info)->dynobj;
6545
5a580b3a
AM
6546 /* Assign dynsym indicies. In a shared library we generate a
6547 section symbol for each output section, which come first.
6548 Next come all of the back-end allocated local dynamic syms,
6549 followed by the rest of the global symbols. */
6550
554220db
AM
6551 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6552 &section_sym_count);
5a580b3a
AM
6553
6554 /* Work out the size of the symbol version section. */
3d4d4302 6555 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6556 BFD_ASSERT (s != NULL);
8423293d
AM
6557 if (dynsymcount != 0
6558 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6559 {
eea6121a 6560 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6561 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6562 if (s->contents == NULL)
6563 return FALSE;
6564
6565 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6566 return FALSE;
6567 }
6568
6569 /* Set the size of the .dynsym and .hash sections. We counted
6570 the number of dynamic symbols in elf_link_add_object_symbols.
6571 We will build the contents of .dynsym and .hash when we build
6572 the final symbol table, because until then we do not know the
6573 correct value to give the symbols. We built the .dynstr
6574 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6575 s = elf_hash_table (info)->dynsym;
5a580b3a 6576 BFD_ASSERT (s != NULL);
eea6121a 6577 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6578
6579 if (dynsymcount != 0)
6580 {
a50b1753 6581 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6582 if (s->contents == NULL)
6583 return FALSE;
5a580b3a 6584
554220db
AM
6585 /* The first entry in .dynsym is a dummy symbol.
6586 Clear all the section syms, in case we don't output them all. */
6587 ++section_sym_count;
6588 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6589 }
6590
fdc90cb4
JJ
6591 elf_hash_table (info)->bucketcount = 0;
6592
5a580b3a
AM
6593 /* Compute the size of the hashing table. As a side effect this
6594 computes the hash values for all the names we export. */
fdc90cb4
JJ
6595 if (info->emit_hash)
6596 {
6597 unsigned long int *hashcodes;
14b1c01e 6598 struct hash_codes_info hashinf;
fdc90cb4
JJ
6599 bfd_size_type amt;
6600 unsigned long int nsyms;
6601 size_t bucketcount;
6602 size_t hash_entry_size;
6603
6604 /* Compute the hash values for all exported symbols. At the same
6605 time store the values in an array so that we could use them for
6606 optimizations. */
6607 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6608 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6609 if (hashcodes == NULL)
6610 return FALSE;
14b1c01e
AM
6611 hashinf.hashcodes = hashcodes;
6612 hashinf.error = FALSE;
5a580b3a 6613
fdc90cb4
JJ
6614 /* Put all hash values in HASHCODES. */
6615 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6616 elf_collect_hash_codes, &hashinf);
6617 if (hashinf.error)
4dd07732
AM
6618 {
6619 free (hashcodes);
6620 return FALSE;
6621 }
5a580b3a 6622
14b1c01e 6623 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6624 bucketcount
6625 = compute_bucket_count (info, hashcodes, nsyms, 0);
6626 free (hashcodes);
6627
6628 if (bucketcount == 0)
6629 return FALSE;
5a580b3a 6630
fdc90cb4
JJ
6631 elf_hash_table (info)->bucketcount = bucketcount;
6632
3d4d4302 6633 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6634 BFD_ASSERT (s != NULL);
6635 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6636 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6637 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6638 if (s->contents == NULL)
6639 return FALSE;
6640
6641 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6642 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6643 s->contents + hash_entry_size);
6644 }
6645
6646 if (info->emit_gnu_hash)
6647 {
6648 size_t i, cnt;
6649 unsigned char *contents;
6650 struct collect_gnu_hash_codes cinfo;
6651 bfd_size_type amt;
6652 size_t bucketcount;
6653
6654 memset (&cinfo, 0, sizeof (cinfo));
6655
6656 /* Compute the hash values for all exported symbols. At the same
6657 time store the values in an array so that we could use them for
6658 optimizations. */
6659 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6660 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6661 if (cinfo.hashcodes == NULL)
6662 return FALSE;
6663
6664 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6665 cinfo.min_dynindx = -1;
6666 cinfo.output_bfd = output_bfd;
6667 cinfo.bed = bed;
6668
6669 /* Put all hash values in HASHCODES. */
6670 elf_link_hash_traverse (elf_hash_table (info),
6671 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6672 if (cinfo.error)
4dd07732
AM
6673 {
6674 free (cinfo.hashcodes);
6675 return FALSE;
6676 }
fdc90cb4
JJ
6677
6678 bucketcount
6679 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6680
6681 if (bucketcount == 0)
6682 {
6683 free (cinfo.hashcodes);
6684 return FALSE;
6685 }
6686
3d4d4302 6687 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6688 BFD_ASSERT (s != NULL);
6689
6690 if (cinfo.nsyms == 0)
6691 {
6692 /* Empty .gnu.hash section is special. */
6693 BFD_ASSERT (cinfo.min_dynindx == -1);
6694 free (cinfo.hashcodes);
6695 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6696 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6697 if (contents == NULL)
6698 return FALSE;
6699 s->contents = contents;
6700 /* 1 empty bucket. */
6701 bfd_put_32 (output_bfd, 1, contents);
6702 /* SYMIDX above the special symbol 0. */
6703 bfd_put_32 (output_bfd, 1, contents + 4);
6704 /* Just one word for bitmask. */
6705 bfd_put_32 (output_bfd, 1, contents + 8);
6706 /* Only hash fn bloom filter. */
6707 bfd_put_32 (output_bfd, 0, contents + 12);
6708 /* No hashes are valid - empty bitmask. */
6709 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6710 /* No hashes in the only bucket. */
6711 bfd_put_32 (output_bfd, 0,
6712 contents + 16 + bed->s->arch_size / 8);
6713 }
6714 else
6715 {
9e6619e2 6716 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6717 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6718
9e6619e2
AM
6719 x = cinfo.nsyms;
6720 maskbitslog2 = 1;
6721 while ((x >>= 1) != 0)
6722 ++maskbitslog2;
fdc90cb4
JJ
6723 if (maskbitslog2 < 3)
6724 maskbitslog2 = 5;
6725 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6726 maskbitslog2 = maskbitslog2 + 3;
6727 else
6728 maskbitslog2 = maskbitslog2 + 2;
6729 if (bed->s->arch_size == 64)
6730 {
6731 if (maskbitslog2 == 5)
6732 maskbitslog2 = 6;
6733 cinfo.shift1 = 6;
6734 }
6735 else
6736 cinfo.shift1 = 5;
6737 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6738 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6739 cinfo.maskbits = 1 << maskbitslog2;
6740 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6741 amt = bucketcount * sizeof (unsigned long int) * 2;
6742 amt += maskwords * sizeof (bfd_vma);
a50b1753 6743 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6744 if (cinfo.bitmask == NULL)
6745 {
6746 free (cinfo.hashcodes);
6747 return FALSE;
6748 }
6749
a50b1753 6750 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6751 cinfo.indx = cinfo.counts + bucketcount;
6752 cinfo.symindx = dynsymcount - cinfo.nsyms;
6753 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6754
6755 /* Determine how often each hash bucket is used. */
6756 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6757 for (i = 0; i < cinfo.nsyms; ++i)
6758 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6759
6760 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6761 if (cinfo.counts[i] != 0)
6762 {
6763 cinfo.indx[i] = cnt;
6764 cnt += cinfo.counts[i];
6765 }
6766 BFD_ASSERT (cnt == dynsymcount);
6767 cinfo.bucketcount = bucketcount;
6768 cinfo.local_indx = cinfo.min_dynindx;
6769
6770 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6771 s->size += cinfo.maskbits / 8;
a50b1753 6772 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6773 if (contents == NULL)
6774 {
6775 free (cinfo.bitmask);
6776 free (cinfo.hashcodes);
6777 return FALSE;
6778 }
6779
6780 s->contents = contents;
6781 bfd_put_32 (output_bfd, bucketcount, contents);
6782 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6783 bfd_put_32 (output_bfd, maskwords, contents + 8);
6784 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6785 contents += 16 + cinfo.maskbits / 8;
6786
6787 for (i = 0; i < bucketcount; ++i)
6788 {
6789 if (cinfo.counts[i] == 0)
6790 bfd_put_32 (output_bfd, 0, contents);
6791 else
6792 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6793 contents += 4;
6794 }
6795
6796 cinfo.contents = contents;
6797
6798 /* Renumber dynamic symbols, populate .gnu.hash section. */
6799 elf_link_hash_traverse (elf_hash_table (info),
6800 elf_renumber_gnu_hash_syms, &cinfo);
6801
6802 contents = s->contents + 16;
6803 for (i = 0; i < maskwords; ++i)
6804 {
6805 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6806 contents);
6807 contents += bed->s->arch_size / 8;
6808 }
6809
6810 free (cinfo.bitmask);
6811 free (cinfo.hashcodes);
6812 }
6813 }
5a580b3a 6814
3d4d4302 6815 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6816 BFD_ASSERT (s != NULL);
6817
4ad4eba5 6818 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6819
eea6121a 6820 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6821
6822 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6823 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6824 return FALSE;
6825 }
6826
6827 return TRUE;
6828}
4d269e42 6829\f
4d269e42
AM
6830/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6831
6832static void
6833merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6834 asection *sec)
6835{
dbaa2011
AM
6836 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6837 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6838}
6839
6840/* Finish SHF_MERGE section merging. */
6841
6842bfd_boolean
630993ec 6843_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6844{
6845 bfd *ibfd;
6846 asection *sec;
6847
6848 if (!is_elf_hash_table (info->hash))
6849 return FALSE;
6850
c72f2fb2 6851 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6852 if ((ibfd->flags & DYNAMIC) == 0
6853 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6854 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6855 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6856 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6857 if ((sec->flags & SEC_MERGE) != 0
6858 && !bfd_is_abs_section (sec->output_section))
6859 {
6860 struct bfd_elf_section_data *secdata;
6861
6862 secdata = elf_section_data (sec);
630993ec 6863 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6864 &elf_hash_table (info)->merge_info,
6865 sec, &secdata->sec_info))
6866 return FALSE;
6867 else if (secdata->sec_info)
dbaa2011 6868 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6869 }
6870
6871 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6872 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6873 merge_sections_remove_hook);
6874 return TRUE;
6875}
6876
6877/* Create an entry in an ELF linker hash table. */
6878
6879struct bfd_hash_entry *
6880_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6881 struct bfd_hash_table *table,
6882 const char *string)
6883{
6884 /* Allocate the structure if it has not already been allocated by a
6885 subclass. */
6886 if (entry == NULL)
6887 {
a50b1753 6888 entry = (struct bfd_hash_entry *)
ca4be51c 6889 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6890 if (entry == NULL)
6891 return entry;
6892 }
6893
6894 /* Call the allocation method of the superclass. */
6895 entry = _bfd_link_hash_newfunc (entry, table, string);
6896 if (entry != NULL)
6897 {
6898 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6899 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6900
6901 /* Set local fields. */
6902 ret->indx = -1;
6903 ret->dynindx = -1;
6904 ret->got = htab->init_got_refcount;
6905 ret->plt = htab->init_plt_refcount;
6906 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6907 - offsetof (struct elf_link_hash_entry, size)));
6908 /* Assume that we have been called by a non-ELF symbol reader.
6909 This flag is then reset by the code which reads an ELF input
6910 file. This ensures that a symbol created by a non-ELF symbol
6911 reader will have the flag set correctly. */
6912 ret->non_elf = 1;
6913 }
6914
6915 return entry;
6916}
6917
6918/* Copy data from an indirect symbol to its direct symbol, hiding the
6919 old indirect symbol. Also used for copying flags to a weakdef. */
6920
6921void
6922_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6923 struct elf_link_hash_entry *dir,
6924 struct elf_link_hash_entry *ind)
6925{
6926 struct elf_link_hash_table *htab;
6927
6928 /* Copy down any references that we may have already seen to the
6e33951e
L
6929 symbol which just became indirect if DIR isn't a hidden versioned
6930 symbol. */
4d269e42 6931
422f1182 6932 if (dir->versioned != versioned_hidden)
6e33951e
L
6933 {
6934 dir->ref_dynamic |= ind->ref_dynamic;
6935 dir->ref_regular |= ind->ref_regular;
6936 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6937 dir->non_got_ref |= ind->non_got_ref;
6938 dir->needs_plt |= ind->needs_plt;
6939 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6940 }
4d269e42
AM
6941
6942 if (ind->root.type != bfd_link_hash_indirect)
6943 return;
6944
6945 /* Copy over the global and procedure linkage table refcount entries.
6946 These may have been already set up by a check_relocs routine. */
6947 htab = elf_hash_table (info);
6948 if (ind->got.refcount > htab->init_got_refcount.refcount)
6949 {
6950 if (dir->got.refcount < 0)
6951 dir->got.refcount = 0;
6952 dir->got.refcount += ind->got.refcount;
6953 ind->got.refcount = htab->init_got_refcount.refcount;
6954 }
6955
6956 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6957 {
6958 if (dir->plt.refcount < 0)
6959 dir->plt.refcount = 0;
6960 dir->plt.refcount += ind->plt.refcount;
6961 ind->plt.refcount = htab->init_plt_refcount.refcount;
6962 }
6963
6964 if (ind->dynindx != -1)
6965 {
6966 if (dir->dynindx != -1)
6967 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6968 dir->dynindx = ind->dynindx;
6969 dir->dynstr_index = ind->dynstr_index;
6970 ind->dynindx = -1;
6971 ind->dynstr_index = 0;
6972 }
6973}
6974
6975void
6976_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6977 struct elf_link_hash_entry *h,
6978 bfd_boolean force_local)
6979{
3aa14d16
L
6980 /* STT_GNU_IFUNC symbol must go through PLT. */
6981 if (h->type != STT_GNU_IFUNC)
6982 {
6983 h->plt = elf_hash_table (info)->init_plt_offset;
6984 h->needs_plt = 0;
6985 }
4d269e42
AM
6986 if (force_local)
6987 {
6988 h->forced_local = 1;
6989 if (h->dynindx != -1)
6990 {
6991 h->dynindx = -1;
6992 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6993 h->dynstr_index);
6994 }
6995 }
6996}
6997
7bf52ea2
AM
6998/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6999 caller. */
4d269e42
AM
7000
7001bfd_boolean
7002_bfd_elf_link_hash_table_init
7003 (struct elf_link_hash_table *table,
7004 bfd *abfd,
7005 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7006 struct bfd_hash_table *,
7007 const char *),
4dfe6ac6
NC
7008 unsigned int entsize,
7009 enum elf_target_id target_id)
4d269e42
AM
7010{
7011 bfd_boolean ret;
7012 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7013
4d269e42
AM
7014 table->init_got_refcount.refcount = can_refcount - 1;
7015 table->init_plt_refcount.refcount = can_refcount - 1;
7016 table->init_got_offset.offset = -(bfd_vma) 1;
7017 table->init_plt_offset.offset = -(bfd_vma) 1;
7018 /* The first dynamic symbol is a dummy. */
7019 table->dynsymcount = 1;
7020
7021 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7022
4d269e42 7023 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7024 table->hash_table_id = target_id;
4d269e42
AM
7025
7026 return ret;
7027}
7028
7029/* Create an ELF linker hash table. */
7030
7031struct bfd_link_hash_table *
7032_bfd_elf_link_hash_table_create (bfd *abfd)
7033{
7034 struct elf_link_hash_table *ret;
7035 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7036
7bf52ea2 7037 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7038 if (ret == NULL)
7039 return NULL;
7040
7041 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7042 sizeof (struct elf_link_hash_entry),
7043 GENERIC_ELF_DATA))
4d269e42
AM
7044 {
7045 free (ret);
7046 return NULL;
7047 }
d495ab0d 7048 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7049
7050 return &ret->root;
7051}
7052
9f7c3e5e
AM
7053/* Destroy an ELF linker hash table. */
7054
7055void
d495ab0d 7056_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7057{
d495ab0d
AM
7058 struct elf_link_hash_table *htab;
7059
7060 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7061 if (htab->dynstr != NULL)
7062 _bfd_elf_strtab_free (htab->dynstr);
7063 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7064 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7065}
7066
4d269e42
AM
7067/* This is a hook for the ELF emulation code in the generic linker to
7068 tell the backend linker what file name to use for the DT_NEEDED
7069 entry for a dynamic object. */
7070
7071void
7072bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7073{
7074 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7075 && bfd_get_format (abfd) == bfd_object)
7076 elf_dt_name (abfd) = name;
7077}
7078
7079int
7080bfd_elf_get_dyn_lib_class (bfd *abfd)
7081{
7082 int lib_class;
7083 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7084 && bfd_get_format (abfd) == bfd_object)
7085 lib_class = elf_dyn_lib_class (abfd);
7086 else
7087 lib_class = 0;
7088 return lib_class;
7089}
7090
7091void
7092bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7093{
7094 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7095 && bfd_get_format (abfd) == bfd_object)
7096 elf_dyn_lib_class (abfd) = lib_class;
7097}
7098
7099/* Get the list of DT_NEEDED entries for a link. This is a hook for
7100 the linker ELF emulation code. */
7101
7102struct bfd_link_needed_list *
7103bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7104 struct bfd_link_info *info)
7105{
7106 if (! is_elf_hash_table (info->hash))
7107 return NULL;
7108 return elf_hash_table (info)->needed;
7109}
7110
7111/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7112 hook for the linker ELF emulation code. */
7113
7114struct bfd_link_needed_list *
7115bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7116 struct bfd_link_info *info)
7117{
7118 if (! is_elf_hash_table (info->hash))
7119 return NULL;
7120 return elf_hash_table (info)->runpath;
7121}
7122
7123/* Get the name actually used for a dynamic object for a link. This
7124 is the SONAME entry if there is one. Otherwise, it is the string
7125 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7126
7127const char *
7128bfd_elf_get_dt_soname (bfd *abfd)
7129{
7130 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7131 && bfd_get_format (abfd) == bfd_object)
7132 return elf_dt_name (abfd);
7133 return NULL;
7134}
7135
7136/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7137 the ELF linker emulation code. */
7138
7139bfd_boolean
7140bfd_elf_get_bfd_needed_list (bfd *abfd,
7141 struct bfd_link_needed_list **pneeded)
7142{
7143 asection *s;
7144 bfd_byte *dynbuf = NULL;
cb33740c 7145 unsigned int elfsec;
4d269e42
AM
7146 unsigned long shlink;
7147 bfd_byte *extdyn, *extdynend;
7148 size_t extdynsize;
7149 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7150
7151 *pneeded = NULL;
7152
7153 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7154 || bfd_get_format (abfd) != bfd_object)
7155 return TRUE;
7156
7157 s = bfd_get_section_by_name (abfd, ".dynamic");
7158 if (s == NULL || s->size == 0)
7159 return TRUE;
7160
7161 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7162 goto error_return;
7163
7164 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7165 if (elfsec == SHN_BAD)
4d269e42
AM
7166 goto error_return;
7167
7168 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7169
4d269e42
AM
7170 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7171 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7172
7173 extdyn = dynbuf;
7174 extdynend = extdyn + s->size;
7175 for (; extdyn < extdynend; extdyn += extdynsize)
7176 {
7177 Elf_Internal_Dyn dyn;
7178
7179 (*swap_dyn_in) (abfd, extdyn, &dyn);
7180
7181 if (dyn.d_tag == DT_NULL)
7182 break;
7183
7184 if (dyn.d_tag == DT_NEEDED)
7185 {
7186 const char *string;
7187 struct bfd_link_needed_list *l;
7188 unsigned int tagv = dyn.d_un.d_val;
7189 bfd_size_type amt;
7190
7191 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7192 if (string == NULL)
7193 goto error_return;
7194
7195 amt = sizeof *l;
a50b1753 7196 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7197 if (l == NULL)
7198 goto error_return;
7199
7200 l->by = abfd;
7201 l->name = string;
7202 l->next = *pneeded;
7203 *pneeded = l;
7204 }
7205 }
7206
7207 free (dynbuf);
7208
7209 return TRUE;
7210
7211 error_return:
7212 if (dynbuf != NULL)
7213 free (dynbuf);
7214 return FALSE;
7215}
7216
7217struct elf_symbuf_symbol
7218{
7219 unsigned long st_name; /* Symbol name, index in string tbl */
7220 unsigned char st_info; /* Type and binding attributes */
7221 unsigned char st_other; /* Visibilty, and target specific */
7222};
7223
7224struct elf_symbuf_head
7225{
7226 struct elf_symbuf_symbol *ssym;
7227 bfd_size_type count;
7228 unsigned int st_shndx;
7229};
7230
7231struct elf_symbol
7232{
7233 union
7234 {
7235 Elf_Internal_Sym *isym;
7236 struct elf_symbuf_symbol *ssym;
7237 } u;
7238 const char *name;
7239};
7240
7241/* Sort references to symbols by ascending section number. */
7242
7243static int
7244elf_sort_elf_symbol (const void *arg1, const void *arg2)
7245{
7246 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7247 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7248
7249 return s1->st_shndx - s2->st_shndx;
7250}
7251
7252static int
7253elf_sym_name_compare (const void *arg1, const void *arg2)
7254{
7255 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7256 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7257 return strcmp (s1->name, s2->name);
7258}
7259
7260static struct elf_symbuf_head *
7261elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7262{
14b1c01e 7263 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7264 struct elf_symbuf_symbol *ssym;
7265 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7266 bfd_size_type i, shndx_count, total_size;
4d269e42 7267
a50b1753 7268 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7269 if (indbuf == NULL)
7270 return NULL;
7271
7272 for (ind = indbuf, i = 0; i < symcount; i++)
7273 if (isymbuf[i].st_shndx != SHN_UNDEF)
7274 *ind++ = &isymbuf[i];
7275 indbufend = ind;
7276
7277 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7278 elf_sort_elf_symbol);
7279
7280 shndx_count = 0;
7281 if (indbufend > indbuf)
7282 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7283 if (ind[0]->st_shndx != ind[1]->st_shndx)
7284 shndx_count++;
7285
3ae181ee
L
7286 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7287 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7288 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7289 if (ssymbuf == NULL)
7290 {
7291 free (indbuf);
7292 return NULL;
7293 }
7294
3ae181ee 7295 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7296 ssymbuf->ssym = NULL;
7297 ssymbuf->count = shndx_count;
7298 ssymbuf->st_shndx = 0;
7299 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7300 {
7301 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7302 {
7303 ssymhead++;
7304 ssymhead->ssym = ssym;
7305 ssymhead->count = 0;
7306 ssymhead->st_shndx = (*ind)->st_shndx;
7307 }
7308 ssym->st_name = (*ind)->st_name;
7309 ssym->st_info = (*ind)->st_info;
7310 ssym->st_other = (*ind)->st_other;
7311 ssymhead->count++;
7312 }
3ae181ee
L
7313 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7314 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7315 == total_size));
4d269e42
AM
7316
7317 free (indbuf);
7318 return ssymbuf;
7319}
7320
7321/* Check if 2 sections define the same set of local and global
7322 symbols. */
7323
8f317e31 7324static bfd_boolean
4d269e42
AM
7325bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7326 struct bfd_link_info *info)
7327{
7328 bfd *bfd1, *bfd2;
7329 const struct elf_backend_data *bed1, *bed2;
7330 Elf_Internal_Shdr *hdr1, *hdr2;
7331 bfd_size_type symcount1, symcount2;
7332 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7333 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7334 Elf_Internal_Sym *isym, *isymend;
7335 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7336 bfd_size_type count1, count2, i;
cb33740c 7337 unsigned int shndx1, shndx2;
4d269e42
AM
7338 bfd_boolean result;
7339
7340 bfd1 = sec1->owner;
7341 bfd2 = sec2->owner;
7342
4d269e42
AM
7343 /* Both sections have to be in ELF. */
7344 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7345 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7346 return FALSE;
7347
7348 if (elf_section_type (sec1) != elf_section_type (sec2))
7349 return FALSE;
7350
4d269e42
AM
7351 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7352 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7353 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7354 return FALSE;
7355
7356 bed1 = get_elf_backend_data (bfd1);
7357 bed2 = get_elf_backend_data (bfd2);
7358 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7359 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7360 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7361 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7362
7363 if (symcount1 == 0 || symcount2 == 0)
7364 return FALSE;
7365
7366 result = FALSE;
7367 isymbuf1 = NULL;
7368 isymbuf2 = NULL;
a50b1753
NC
7369 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7370 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7371
7372 if (ssymbuf1 == NULL)
7373 {
7374 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7375 NULL, NULL, NULL);
7376 if (isymbuf1 == NULL)
7377 goto done;
7378
7379 if (!info->reduce_memory_overheads)
7380 elf_tdata (bfd1)->symbuf = ssymbuf1
7381 = elf_create_symbuf (symcount1, isymbuf1);
7382 }
7383
7384 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7385 {
7386 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7387 NULL, NULL, NULL);
7388 if (isymbuf2 == NULL)
7389 goto done;
7390
7391 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7392 elf_tdata (bfd2)->symbuf = ssymbuf2
7393 = elf_create_symbuf (symcount2, isymbuf2);
7394 }
7395
7396 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7397 {
7398 /* Optimized faster version. */
7399 bfd_size_type lo, hi, mid;
7400 struct elf_symbol *symp;
7401 struct elf_symbuf_symbol *ssym, *ssymend;
7402
7403 lo = 0;
7404 hi = ssymbuf1->count;
7405 ssymbuf1++;
7406 count1 = 0;
7407 while (lo < hi)
7408 {
7409 mid = (lo + hi) / 2;
cb33740c 7410 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7411 hi = mid;
cb33740c 7412 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7413 lo = mid + 1;
7414 else
7415 {
7416 count1 = ssymbuf1[mid].count;
7417 ssymbuf1 += mid;
7418 break;
7419 }
7420 }
7421
7422 lo = 0;
7423 hi = ssymbuf2->count;
7424 ssymbuf2++;
7425 count2 = 0;
7426 while (lo < hi)
7427 {
7428 mid = (lo + hi) / 2;
cb33740c 7429 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7430 hi = mid;
cb33740c 7431 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7432 lo = mid + 1;
7433 else
7434 {
7435 count2 = ssymbuf2[mid].count;
7436 ssymbuf2 += mid;
7437 break;
7438 }
7439 }
7440
7441 if (count1 == 0 || count2 == 0 || count1 != count2)
7442 goto done;
7443
ca4be51c
AM
7444 symtable1
7445 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7446 symtable2
7447 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7448 if (symtable1 == NULL || symtable2 == NULL)
7449 goto done;
7450
7451 symp = symtable1;
7452 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7453 ssym < ssymend; ssym++, symp++)
7454 {
7455 symp->u.ssym = ssym;
7456 symp->name = bfd_elf_string_from_elf_section (bfd1,
7457 hdr1->sh_link,
7458 ssym->st_name);
7459 }
7460
7461 symp = symtable2;
7462 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7463 ssym < ssymend; ssym++, symp++)
7464 {
7465 symp->u.ssym = ssym;
7466 symp->name = bfd_elf_string_from_elf_section (bfd2,
7467 hdr2->sh_link,
7468 ssym->st_name);
7469 }
7470
7471 /* Sort symbol by name. */
7472 qsort (symtable1, count1, sizeof (struct elf_symbol),
7473 elf_sym_name_compare);
7474 qsort (symtable2, count1, sizeof (struct elf_symbol),
7475 elf_sym_name_compare);
7476
7477 for (i = 0; i < count1; i++)
7478 /* Two symbols must have the same binding, type and name. */
7479 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7480 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7481 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7482 goto done;
7483
7484 result = TRUE;
7485 goto done;
7486 }
7487
a50b1753
NC
7488 symtable1 = (struct elf_symbol *)
7489 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7490 symtable2 = (struct elf_symbol *)
7491 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7492 if (symtable1 == NULL || symtable2 == NULL)
7493 goto done;
7494
7495 /* Count definitions in the section. */
7496 count1 = 0;
7497 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7498 if (isym->st_shndx == shndx1)
4d269e42
AM
7499 symtable1[count1++].u.isym = isym;
7500
7501 count2 = 0;
7502 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7503 if (isym->st_shndx == shndx2)
4d269e42
AM
7504 symtable2[count2++].u.isym = isym;
7505
7506 if (count1 == 0 || count2 == 0 || count1 != count2)
7507 goto done;
7508
7509 for (i = 0; i < count1; i++)
7510 symtable1[i].name
7511 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7512 symtable1[i].u.isym->st_name);
7513
7514 for (i = 0; i < count2; i++)
7515 symtable2[i].name
7516 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7517 symtable2[i].u.isym->st_name);
7518
7519 /* Sort symbol by name. */
7520 qsort (symtable1, count1, sizeof (struct elf_symbol),
7521 elf_sym_name_compare);
7522 qsort (symtable2, count1, sizeof (struct elf_symbol),
7523 elf_sym_name_compare);
7524
7525 for (i = 0; i < count1; i++)
7526 /* Two symbols must have the same binding, type and name. */
7527 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7528 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7529 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7530 goto done;
7531
7532 result = TRUE;
7533
7534done:
7535 if (symtable1)
7536 free (symtable1);
7537 if (symtable2)
7538 free (symtable2);
7539 if (isymbuf1)
7540 free (isymbuf1);
7541 if (isymbuf2)
7542 free (isymbuf2);
7543
7544 return result;
7545}
7546
7547/* Return TRUE if 2 section types are compatible. */
7548
7549bfd_boolean
7550_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7551 bfd *bbfd, const asection *bsec)
7552{
7553 if (asec == NULL
7554 || bsec == NULL
7555 || abfd->xvec->flavour != bfd_target_elf_flavour
7556 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7557 return TRUE;
7558
7559 return elf_section_type (asec) == elf_section_type (bsec);
7560}
7561\f
c152c796
AM
7562/* Final phase of ELF linker. */
7563
7564/* A structure we use to avoid passing large numbers of arguments. */
7565
7566struct elf_final_link_info
7567{
7568 /* General link information. */
7569 struct bfd_link_info *info;
7570 /* Output BFD. */
7571 bfd *output_bfd;
7572 /* Symbol string table. */
ef10c3ac 7573 struct elf_strtab_hash *symstrtab;
c152c796
AM
7574 /* .hash section. */
7575 asection *hash_sec;
7576 /* symbol version section (.gnu.version). */
7577 asection *symver_sec;
7578 /* Buffer large enough to hold contents of any section. */
7579 bfd_byte *contents;
7580 /* Buffer large enough to hold external relocs of any section. */
7581 void *external_relocs;
7582 /* Buffer large enough to hold internal relocs of any section. */
7583 Elf_Internal_Rela *internal_relocs;
7584 /* Buffer large enough to hold external local symbols of any input
7585 BFD. */
7586 bfd_byte *external_syms;
7587 /* And a buffer for symbol section indices. */
7588 Elf_External_Sym_Shndx *locsym_shndx;
7589 /* Buffer large enough to hold internal local symbols of any input
7590 BFD. */
7591 Elf_Internal_Sym *internal_syms;
7592 /* Array large enough to hold a symbol index for each local symbol
7593 of any input BFD. */
7594 long *indices;
7595 /* Array large enough to hold a section pointer for each local
7596 symbol of any input BFD. */
7597 asection **sections;
ef10c3ac 7598 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7599 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7600 /* Number of STT_FILE syms seen. */
7601 size_t filesym_count;
c152c796
AM
7602};
7603
7604/* This struct is used to pass information to elf_link_output_extsym. */
7605
7606struct elf_outext_info
7607{
7608 bfd_boolean failed;
7609 bfd_boolean localsyms;
34a79995 7610 bfd_boolean file_sym_done;
8b127cbc 7611 struct elf_final_link_info *flinfo;
c152c796
AM
7612};
7613
d9352518
DB
7614
7615/* Support for evaluating a complex relocation.
7616
7617 Complex relocations are generalized, self-describing relocations. The
7618 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7619 relocations themselves.
d9352518
DB
7620
7621 The relocations are use a reserved elf-wide relocation type code (R_RELC
7622 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7623 information (start bit, end bit, word width, etc) into the addend. This
7624 information is extracted from CGEN-generated operand tables within gas.
7625
7626 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7627 internal) representing prefix-notation expressions, including but not
7628 limited to those sorts of expressions normally encoded as addends in the
7629 addend field. The symbol mangling format is:
7630
7631 <node> := <literal>
7632 | <unary-operator> ':' <node>
7633 | <binary-operator> ':' <node> ':' <node>
7634 ;
7635
7636 <literal> := 's' <digits=N> ':' <N character symbol name>
7637 | 'S' <digits=N> ':' <N character section name>
7638 | '#' <hexdigits>
7639 ;
7640
7641 <binary-operator> := as in C
7642 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7643
7644static void
a0c8462f
AM
7645set_symbol_value (bfd *bfd_with_globals,
7646 Elf_Internal_Sym *isymbuf,
7647 size_t locsymcount,
7648 size_t symidx,
7649 bfd_vma val)
d9352518 7650{
8977835c
AM
7651 struct elf_link_hash_entry **sym_hashes;
7652 struct elf_link_hash_entry *h;
7653 size_t extsymoff = locsymcount;
d9352518 7654
8977835c 7655 if (symidx < locsymcount)
d9352518 7656 {
8977835c
AM
7657 Elf_Internal_Sym *sym;
7658
7659 sym = isymbuf + symidx;
7660 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7661 {
7662 /* It is a local symbol: move it to the
7663 "absolute" section and give it a value. */
7664 sym->st_shndx = SHN_ABS;
7665 sym->st_value = val;
7666 return;
7667 }
7668 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7669 extsymoff = 0;
d9352518 7670 }
8977835c
AM
7671
7672 /* It is a global symbol: set its link type
7673 to "defined" and give it a value. */
7674
7675 sym_hashes = elf_sym_hashes (bfd_with_globals);
7676 h = sym_hashes [symidx - extsymoff];
7677 while (h->root.type == bfd_link_hash_indirect
7678 || h->root.type == bfd_link_hash_warning)
7679 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7680 h->root.type = bfd_link_hash_defined;
7681 h->root.u.def.value = val;
7682 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7683}
7684
a0c8462f
AM
7685static bfd_boolean
7686resolve_symbol (const char *name,
7687 bfd *input_bfd,
8b127cbc 7688 struct elf_final_link_info *flinfo,
a0c8462f
AM
7689 bfd_vma *result,
7690 Elf_Internal_Sym *isymbuf,
7691 size_t locsymcount)
d9352518 7692{
a0c8462f
AM
7693 Elf_Internal_Sym *sym;
7694 struct bfd_link_hash_entry *global_entry;
7695 const char *candidate = NULL;
7696 Elf_Internal_Shdr *symtab_hdr;
7697 size_t i;
7698
d9352518
DB
7699 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7700
7701 for (i = 0; i < locsymcount; ++ i)
7702 {
8977835c 7703 sym = isymbuf + i;
d9352518
DB
7704
7705 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7706 continue;
7707
7708 candidate = bfd_elf_string_from_elf_section (input_bfd,
7709 symtab_hdr->sh_link,
7710 sym->st_name);
7711#ifdef DEBUG
0f02bbd9
AM
7712 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7713 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7714#endif
7715 if (candidate && strcmp (candidate, name) == 0)
7716 {
8b127cbc 7717 asection *sec = flinfo->sections [i];
d9352518 7718
0f02bbd9
AM
7719 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7720 *result += sec->output_offset + sec->output_section->vma;
d9352518 7721#ifdef DEBUG
0f02bbd9
AM
7722 printf ("Found symbol with value %8.8lx\n",
7723 (unsigned long) *result);
d9352518
DB
7724#endif
7725 return TRUE;
7726 }
7727 }
7728
7729 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7730 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7731 FALSE, FALSE, TRUE);
d9352518
DB
7732 if (!global_entry)
7733 return FALSE;
a0c8462f 7734
d9352518
DB
7735 if (global_entry->type == bfd_link_hash_defined
7736 || global_entry->type == bfd_link_hash_defweak)
7737 {
a0c8462f
AM
7738 *result = (global_entry->u.def.value
7739 + global_entry->u.def.section->output_section->vma
7740 + global_entry->u.def.section->output_offset);
d9352518 7741#ifdef DEBUG
0f02bbd9
AM
7742 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7743 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7744#endif
7745 return TRUE;
a0c8462f 7746 }
d9352518 7747
d9352518
DB
7748 return FALSE;
7749}
7750
7751static bfd_boolean
a0c8462f
AM
7752resolve_section (const char *name,
7753 asection *sections,
7754 bfd_vma *result)
d9352518 7755{
a0c8462f
AM
7756 asection *curr;
7757 unsigned int len;
d9352518 7758
a0c8462f 7759 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7760 if (strcmp (curr->name, name) == 0)
7761 {
7762 *result = curr->vma;
7763 return TRUE;
7764 }
7765
7766 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7767 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7768 {
7769 len = strlen (curr->name);
a0c8462f 7770 if (len > strlen (name))
d9352518
DB
7771 continue;
7772
7773 if (strncmp (curr->name, name, len) == 0)
7774 {
7775 if (strncmp (".end", name + len, 4) == 0)
7776 {
7777 *result = curr->vma + curr->size;
7778 return TRUE;
7779 }
7780
7781 /* Insert more pseudo-section names here, if you like. */
7782 }
7783 }
a0c8462f 7784
d9352518
DB
7785 return FALSE;
7786}
7787
7788static void
a0c8462f 7789undefined_reference (const char *reftype, const char *name)
d9352518 7790{
a0c8462f
AM
7791 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7792 reftype, name);
d9352518
DB
7793}
7794
7795static bfd_boolean
a0c8462f
AM
7796eval_symbol (bfd_vma *result,
7797 const char **symp,
7798 bfd *input_bfd,
8b127cbc 7799 struct elf_final_link_info *flinfo,
a0c8462f
AM
7800 bfd_vma dot,
7801 Elf_Internal_Sym *isymbuf,
7802 size_t locsymcount,
7803 int signed_p)
d9352518 7804{
4b93929b
NC
7805 size_t len;
7806 size_t symlen;
a0c8462f
AM
7807 bfd_vma a;
7808 bfd_vma b;
4b93929b 7809 char symbuf[4096];
0f02bbd9 7810 const char *sym = *symp;
a0c8462f
AM
7811 const char *symend;
7812 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7813
7814 len = strlen (sym);
7815 symend = sym + len;
7816
4b93929b 7817 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7818 {
7819 bfd_set_error (bfd_error_invalid_operation);
7820 return FALSE;
7821 }
a0c8462f 7822
d9352518
DB
7823 switch (* sym)
7824 {
7825 case '.':
0f02bbd9
AM
7826 *result = dot;
7827 *symp = sym + 1;
d9352518
DB
7828 return TRUE;
7829
7830 case '#':
0f02bbd9
AM
7831 ++sym;
7832 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7833 return TRUE;
7834
7835 case 'S':
7836 symbol_is_section = TRUE;
a0c8462f 7837 case 's':
0f02bbd9
AM
7838 ++sym;
7839 symlen = strtol (sym, (char **) symp, 10);
7840 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7841
4b93929b 7842 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7843 {
7844 bfd_set_error (bfd_error_invalid_operation);
7845 return FALSE;
7846 }
7847
7848 memcpy (symbuf, sym, symlen);
a0c8462f 7849 symbuf[symlen] = '\0';
0f02bbd9 7850 *symp = sym + symlen;
a0c8462f
AM
7851
7852 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7853 the symbol as a section, or vice-versa. so we're pretty liberal in our
7854 interpretation here; section means "try section first", not "must be a
7855 section", and likewise with symbol. */
7856
a0c8462f 7857 if (symbol_is_section)
d9352518 7858 {
8b127cbc
AM
7859 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7860 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7861 isymbuf, locsymcount))
d9352518
DB
7862 {
7863 undefined_reference ("section", symbuf);
7864 return FALSE;
7865 }
a0c8462f
AM
7866 }
7867 else
d9352518 7868 {
8b127cbc 7869 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7870 isymbuf, locsymcount)
8b127cbc 7871 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977835c 7872 result))
d9352518
DB
7873 {
7874 undefined_reference ("symbol", symbuf);
7875 return FALSE;
7876 }
7877 }
7878
7879 return TRUE;
a0c8462f 7880
d9352518
DB
7881 /* All that remains are operators. */
7882
7883#define UNARY_OP(op) \
7884 if (strncmp (sym, #op, strlen (#op)) == 0) \
7885 { \
7886 sym += strlen (#op); \
a0c8462f
AM
7887 if (*sym == ':') \
7888 ++sym; \
0f02bbd9 7889 *symp = sym; \
8b127cbc 7890 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7891 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7892 return FALSE; \
7893 if (signed_p) \
0f02bbd9 7894 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7895 else \
7896 *result = op a; \
d9352518
DB
7897 return TRUE; \
7898 }
7899
7900#define BINARY_OP(op) \
7901 if (strncmp (sym, #op, strlen (#op)) == 0) \
7902 { \
7903 sym += strlen (#op); \
a0c8462f
AM
7904 if (*sym == ':') \
7905 ++sym; \
0f02bbd9 7906 *symp = sym; \
8b127cbc 7907 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7908 isymbuf, locsymcount, signed_p)) \
a0c8462f 7909 return FALSE; \
0f02bbd9 7910 ++*symp; \
8b127cbc 7911 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7912 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7913 return FALSE; \
7914 if (signed_p) \
0f02bbd9 7915 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7916 else \
7917 *result = a op b; \
d9352518
DB
7918 return TRUE; \
7919 }
7920
7921 default:
7922 UNARY_OP (0-);
7923 BINARY_OP (<<);
7924 BINARY_OP (>>);
7925 BINARY_OP (==);
7926 BINARY_OP (!=);
7927 BINARY_OP (<=);
7928 BINARY_OP (>=);
7929 BINARY_OP (&&);
7930 BINARY_OP (||);
7931 UNARY_OP (~);
7932 UNARY_OP (!);
7933 BINARY_OP (*);
7934 BINARY_OP (/);
7935 BINARY_OP (%);
7936 BINARY_OP (^);
7937 BINARY_OP (|);
7938 BINARY_OP (&);
7939 BINARY_OP (+);
7940 BINARY_OP (-);
7941 BINARY_OP (<);
7942 BINARY_OP (>);
7943#undef UNARY_OP
7944#undef BINARY_OP
7945 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7946 bfd_set_error (bfd_error_invalid_operation);
7947 return FALSE;
7948 }
7949}
7950
d9352518 7951static void
a0c8462f
AM
7952put_value (bfd_vma size,
7953 unsigned long chunksz,
7954 bfd *input_bfd,
7955 bfd_vma x,
7956 bfd_byte *location)
d9352518
DB
7957{
7958 location += (size - chunksz);
7959
41cd1ad1 7960 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
7961 {
7962 switch (chunksz)
7963 {
d9352518
DB
7964 case 1:
7965 bfd_put_8 (input_bfd, x, location);
41cd1ad1 7966 x >>= 8;
d9352518
DB
7967 break;
7968 case 2:
7969 bfd_put_16 (input_bfd, x, location);
41cd1ad1 7970 x >>= 16;
d9352518
DB
7971 break;
7972 case 4:
7973 bfd_put_32 (input_bfd, x, location);
65164438
NC
7974 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7975 x >>= 16;
7976 x >>= 16;
d9352518 7977 break;
d9352518 7978#ifdef BFD64
41cd1ad1 7979 case 8:
d9352518 7980 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
7981 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7982 x >>= 32;
7983 x >>= 32;
7984 break;
d9352518 7985#endif
41cd1ad1
NC
7986 default:
7987 abort ();
d9352518
DB
7988 break;
7989 }
7990 }
7991}
7992
a0c8462f
AM
7993static bfd_vma
7994get_value (bfd_vma size,
7995 unsigned long chunksz,
7996 bfd *input_bfd,
7997 bfd_byte *location)
d9352518 7998{
9b239e0e 7999 int shift;
d9352518
DB
8000 bfd_vma x = 0;
8001
9b239e0e
NC
8002 /* Sanity checks. */
8003 BFD_ASSERT (chunksz <= sizeof (x)
8004 && size >= chunksz
8005 && chunksz != 0
8006 && (size % chunksz) == 0
8007 && input_bfd != NULL
8008 && location != NULL);
8009
8010 if (chunksz == sizeof (x))
8011 {
8012 BFD_ASSERT (size == chunksz);
8013
8014 /* Make sure that we do not perform an undefined shift operation.
8015 We know that size == chunksz so there will only be one iteration
8016 of the loop below. */
8017 shift = 0;
8018 }
8019 else
8020 shift = 8 * chunksz;
8021
a0c8462f 8022 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8023 {
8024 switch (chunksz)
8025 {
d9352518 8026 case 1:
9b239e0e 8027 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8028 break;
8029 case 2:
9b239e0e 8030 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8031 break;
8032 case 4:
9b239e0e 8033 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8034 break;
d9352518 8035#ifdef BFD64
9b239e0e
NC
8036 case 8:
8037 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8038 break;
9b239e0e
NC
8039#endif
8040 default:
8041 abort ();
d9352518
DB
8042 }
8043 }
8044 return x;
8045}
8046
a0c8462f
AM
8047static void
8048decode_complex_addend (unsigned long *start, /* in bits */
8049 unsigned long *oplen, /* in bits */
8050 unsigned long *len, /* in bits */
8051 unsigned long *wordsz, /* in bytes */
8052 unsigned long *chunksz, /* in bytes */
8053 unsigned long *lsb0_p,
8054 unsigned long *signed_p,
8055 unsigned long *trunc_p,
8056 unsigned long encoded)
d9352518
DB
8057{
8058 * start = encoded & 0x3F;
8059 * len = (encoded >> 6) & 0x3F;
8060 * oplen = (encoded >> 12) & 0x3F;
8061 * wordsz = (encoded >> 18) & 0xF;
8062 * chunksz = (encoded >> 22) & 0xF;
8063 * lsb0_p = (encoded >> 27) & 1;
8064 * signed_p = (encoded >> 28) & 1;
8065 * trunc_p = (encoded >> 29) & 1;
8066}
8067
cdfeee4f 8068bfd_reloc_status_type
0f02bbd9 8069bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8070 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8071 bfd_byte *contents,
8072 Elf_Internal_Rela *rel,
8073 bfd_vma relocation)
d9352518 8074{
0f02bbd9
AM
8075 bfd_vma shift, x, mask;
8076 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8077 bfd_reloc_status_type r;
d9352518
DB
8078
8079 /* Perform this reloc, since it is complex.
8080 (this is not to say that it necessarily refers to a complex
8081 symbol; merely that it is a self-describing CGEN based reloc.
8082 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8083 word size, etc) encoded within it.). */
d9352518 8084
a0c8462f
AM
8085 decode_complex_addend (&start, &oplen, &len, &wordsz,
8086 &chunksz, &lsb0_p, &signed_p,
8087 &trunc_p, rel->r_addend);
d9352518
DB
8088
8089 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8090
8091 if (lsb0_p)
8092 shift = (start + 1) - len;
8093 else
8094 shift = (8 * wordsz) - (start + len);
8095
5dabe785 8096 /* FIXME: octets_per_byte. */
a0c8462f 8097 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
8098
8099#ifdef DEBUG
8100 printf ("Doing complex reloc: "
8101 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8102 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8103 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8104 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8105 oplen, (unsigned long) x, (unsigned long) mask,
8106 (unsigned long) relocation);
d9352518
DB
8107#endif
8108
cdfeee4f 8109 r = bfd_reloc_ok;
d9352518 8110 if (! trunc_p)
cdfeee4f
AM
8111 /* Now do an overflow check. */
8112 r = bfd_check_overflow ((signed_p
8113 ? complain_overflow_signed
8114 : complain_overflow_unsigned),
8115 len, 0, (8 * wordsz),
8116 relocation);
a0c8462f 8117
d9352518
DB
8118 /* Do the deed. */
8119 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8120
8121#ifdef DEBUG
8122 printf (" relocation: %8.8lx\n"
8123 " shifted mask: %8.8lx\n"
8124 " shifted/masked reloc: %8.8lx\n"
8125 " result: %8.8lx\n",
9ccb8af9
AM
8126 (unsigned long) relocation, (unsigned long) (mask << shift),
8127 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8128#endif
5dabe785 8129 /* FIXME: octets_per_byte. */
d9352518 8130 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 8131 return r;
d9352518
DB
8132}
8133
0e287786
AM
8134/* Functions to read r_offset from external (target order) reloc
8135 entry. Faster than bfd_getl32 et al, because we let the compiler
8136 know the value is aligned. */
53df40a4 8137
0e287786
AM
8138static bfd_vma
8139ext32l_r_offset (const void *p)
53df40a4
AM
8140{
8141 union aligned32
8142 {
8143 uint32_t v;
8144 unsigned char c[4];
8145 };
8146 const union aligned32 *a
0e287786 8147 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8148
8149 uint32_t aval = ( (uint32_t) a->c[0]
8150 | (uint32_t) a->c[1] << 8
8151 | (uint32_t) a->c[2] << 16
8152 | (uint32_t) a->c[3] << 24);
0e287786 8153 return aval;
53df40a4
AM
8154}
8155
0e287786
AM
8156static bfd_vma
8157ext32b_r_offset (const void *p)
53df40a4
AM
8158{
8159 union aligned32
8160 {
8161 uint32_t v;
8162 unsigned char c[4];
8163 };
8164 const union aligned32 *a
0e287786 8165 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8166
8167 uint32_t aval = ( (uint32_t) a->c[0] << 24
8168 | (uint32_t) a->c[1] << 16
8169 | (uint32_t) a->c[2] << 8
8170 | (uint32_t) a->c[3]);
0e287786 8171 return aval;
53df40a4
AM
8172}
8173
8174#ifdef BFD_HOST_64_BIT
0e287786
AM
8175static bfd_vma
8176ext64l_r_offset (const void *p)
53df40a4
AM
8177{
8178 union aligned64
8179 {
8180 uint64_t v;
8181 unsigned char c[8];
8182 };
8183 const union aligned64 *a
0e287786 8184 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8185
8186 uint64_t aval = ( (uint64_t) a->c[0]
8187 | (uint64_t) a->c[1] << 8
8188 | (uint64_t) a->c[2] << 16
8189 | (uint64_t) a->c[3] << 24
8190 | (uint64_t) a->c[4] << 32
8191 | (uint64_t) a->c[5] << 40
8192 | (uint64_t) a->c[6] << 48
8193 | (uint64_t) a->c[7] << 56);
0e287786 8194 return aval;
53df40a4
AM
8195}
8196
0e287786
AM
8197static bfd_vma
8198ext64b_r_offset (const void *p)
53df40a4
AM
8199{
8200 union aligned64
8201 {
8202 uint64_t v;
8203 unsigned char c[8];
8204 };
8205 const union aligned64 *a
0e287786 8206 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8207
8208 uint64_t aval = ( (uint64_t) a->c[0] << 56
8209 | (uint64_t) a->c[1] << 48
8210 | (uint64_t) a->c[2] << 40
8211 | (uint64_t) a->c[3] << 32
8212 | (uint64_t) a->c[4] << 24
8213 | (uint64_t) a->c[5] << 16
8214 | (uint64_t) a->c[6] << 8
8215 | (uint64_t) a->c[7]);
0e287786 8216 return aval;
53df40a4
AM
8217}
8218#endif
8219
c152c796
AM
8220/* When performing a relocatable link, the input relocations are
8221 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8222 referenced must be updated. Update all the relocations found in
8223 RELDATA. */
c152c796 8224
bca6d0e3 8225static bfd_boolean
c152c796 8226elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8227 struct bfd_elf_section_reloc_data *reldata,
8228 bfd_boolean sort)
c152c796
AM
8229{
8230 unsigned int i;
8231 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8232 bfd_byte *erela;
8233 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8234 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8235 bfd_vma r_type_mask;
8236 int r_sym_shift;
d4730f92
BS
8237 unsigned int count = reldata->count;
8238 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8239
d4730f92 8240 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8241 {
8242 swap_in = bed->s->swap_reloc_in;
8243 swap_out = bed->s->swap_reloc_out;
8244 }
d4730f92 8245 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8246 {
8247 swap_in = bed->s->swap_reloca_in;
8248 swap_out = bed->s->swap_reloca_out;
8249 }
8250 else
8251 abort ();
8252
8253 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8254 abort ();
8255
8256 if (bed->s->arch_size == 32)
8257 {
8258 r_type_mask = 0xff;
8259 r_sym_shift = 8;
8260 }
8261 else
8262 {
8263 r_type_mask = 0xffffffff;
8264 r_sym_shift = 32;
8265 }
8266
d4730f92
BS
8267 erela = reldata->hdr->contents;
8268 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8269 {
8270 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8271 unsigned int j;
8272
8273 if (*rel_hash == NULL)
8274 continue;
8275
8276 BFD_ASSERT ((*rel_hash)->indx >= 0);
8277
8278 (*swap_in) (abfd, erela, irela);
8279 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8280 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8281 | (irela[j].r_info & r_type_mask));
8282 (*swap_out) (abfd, irela, erela);
8283 }
53df40a4 8284
0e287786 8285 if (sort && count != 0)
53df40a4 8286 {
0e287786
AM
8287 bfd_vma (*ext_r_off) (const void *);
8288 bfd_vma r_off;
8289 size_t elt_size;
8290 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8291 bfd_byte *buf = NULL;
28dbcedc
AM
8292
8293 if (bed->s->arch_size == 32)
8294 {
8295 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8296 ext_r_off = ext32l_r_offset;
28dbcedc 8297 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8298 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8299 else
8300 abort ();
8301 }
53df40a4 8302 else
28dbcedc 8303 {
53df40a4 8304#ifdef BFD_HOST_64_BIT
28dbcedc 8305 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8306 ext_r_off = ext64l_r_offset;
28dbcedc 8307 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8308 ext_r_off = ext64b_r_offset;
28dbcedc 8309 else
53df40a4 8310#endif
28dbcedc
AM
8311 abort ();
8312 }
0e287786 8313
bca6d0e3
AM
8314 /* Must use a stable sort here. A modified insertion sort,
8315 since the relocs are mostly sorted already. */
0e287786
AM
8316 elt_size = reldata->hdr->sh_entsize;
8317 base = reldata->hdr->contents;
8318 end = base + count * elt_size;
bca6d0e3 8319 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8320 abort ();
8321
8322 /* Ensure the first element is lowest. This acts as a sentinel,
8323 speeding the main loop below. */
8324 r_off = (*ext_r_off) (base);
8325 for (p = loc = base; (p += elt_size) < end; )
8326 {
8327 bfd_vma r_off2 = (*ext_r_off) (p);
8328 if (r_off > r_off2)
8329 {
8330 r_off = r_off2;
8331 loc = p;
8332 }
8333 }
8334 if (loc != base)
8335 {
8336 /* Don't just swap *base and *loc as that changes the order
8337 of the original base[0] and base[1] if they happen to
8338 have the same r_offset. */
bca6d0e3
AM
8339 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8340 memcpy (onebuf, loc, elt_size);
0e287786 8341 memmove (base + elt_size, base, loc - base);
bca6d0e3 8342 memcpy (base, onebuf, elt_size);
0e287786
AM
8343 }
8344
b29b8669 8345 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8346 {
8347 /* base to p is sorted, *p is next to insert. */
8348 r_off = (*ext_r_off) (p);
8349 /* Search the sorted region for location to insert. */
8350 loc = p - elt_size;
8351 while (r_off < (*ext_r_off) (loc))
8352 loc -= elt_size;
8353 loc += elt_size;
8354 if (loc != p)
8355 {
bca6d0e3
AM
8356 /* Chances are there is a run of relocs to insert here,
8357 from one of more input files. Files are not always
8358 linked in order due to the way elf_link_input_bfd is
8359 called. See pr17666. */
8360 size_t sortlen = p - loc;
8361 bfd_vma r_off2 = (*ext_r_off) (loc);
8362 size_t runlen = elt_size;
8363 size_t buf_size = 96 * 1024;
8364 while (p + runlen < end
8365 && (sortlen <= buf_size
8366 || runlen + elt_size <= buf_size)
8367 && r_off2 > (*ext_r_off) (p + runlen))
8368 runlen += elt_size;
8369 if (buf == NULL)
8370 {
8371 buf = bfd_malloc (buf_size);
8372 if (buf == NULL)
8373 return FALSE;
8374 }
8375 if (runlen < sortlen)
8376 {
8377 memcpy (buf, p, runlen);
8378 memmove (loc + runlen, loc, sortlen);
8379 memcpy (loc, buf, runlen);
8380 }
8381 else
8382 {
8383 memcpy (buf, loc, sortlen);
8384 memmove (loc, p, runlen);
8385 memcpy (loc + runlen, buf, sortlen);
8386 }
b29b8669 8387 p += runlen - elt_size;
0e287786
AM
8388 }
8389 }
8390 /* Hashes are no longer valid. */
28dbcedc
AM
8391 free (reldata->hashes);
8392 reldata->hashes = NULL;
bca6d0e3 8393 free (buf);
53df40a4 8394 }
bca6d0e3 8395 return TRUE;
c152c796
AM
8396}
8397
8398struct elf_link_sort_rela
8399{
8400 union {
8401 bfd_vma offset;
8402 bfd_vma sym_mask;
8403 } u;
8404 enum elf_reloc_type_class type;
8405 /* We use this as an array of size int_rels_per_ext_rel. */
8406 Elf_Internal_Rela rela[1];
8407};
8408
8409static int
8410elf_link_sort_cmp1 (const void *A, const void *B)
8411{
a50b1753
NC
8412 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8413 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8414 int relativea, relativeb;
8415
8416 relativea = a->type == reloc_class_relative;
8417 relativeb = b->type == reloc_class_relative;
8418
8419 if (relativea < relativeb)
8420 return 1;
8421 if (relativea > relativeb)
8422 return -1;
8423 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8424 return -1;
8425 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8426 return 1;
8427 if (a->rela->r_offset < b->rela->r_offset)
8428 return -1;
8429 if (a->rela->r_offset > b->rela->r_offset)
8430 return 1;
8431 return 0;
8432}
8433
8434static int
8435elf_link_sort_cmp2 (const void *A, const void *B)
8436{
a50b1753
NC
8437 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8438 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8439
7e612e98 8440 if (a->type < b->type)
c152c796 8441 return -1;
7e612e98 8442 if (a->type > b->type)
c152c796 8443 return 1;
7e612e98 8444 if (a->u.offset < b->u.offset)
c152c796 8445 return -1;
7e612e98 8446 if (a->u.offset > b->u.offset)
c152c796
AM
8447 return 1;
8448 if (a->rela->r_offset < b->rela->r_offset)
8449 return -1;
8450 if (a->rela->r_offset > b->rela->r_offset)
8451 return 1;
8452 return 0;
8453}
8454
8455static size_t
8456elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8457{
3410fea8 8458 asection *dynamic_relocs;
fc66a176
L
8459 asection *rela_dyn;
8460 asection *rel_dyn;
c152c796
AM
8461 bfd_size_type count, size;
8462 size_t i, ret, sort_elt, ext_size;
8463 bfd_byte *sort, *s_non_relative, *p;
8464 struct elf_link_sort_rela *sq;
8465 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8466 int i2e = bed->s->int_rels_per_ext_rel;
8467 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8468 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8469 struct bfd_link_order *lo;
8470 bfd_vma r_sym_mask;
3410fea8 8471 bfd_boolean use_rela;
c152c796 8472
3410fea8
NC
8473 /* Find a dynamic reloc section. */
8474 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8475 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8476 if (rela_dyn != NULL && rela_dyn->size > 0
8477 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8478 {
3410fea8
NC
8479 bfd_boolean use_rela_initialised = FALSE;
8480
8481 /* This is just here to stop gcc from complaining.
8482 It's initialization checking code is not perfect. */
8483 use_rela = TRUE;
8484
8485 /* Both sections are present. Examine the sizes
8486 of the indirect sections to help us choose. */
8487 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8488 if (lo->type == bfd_indirect_link_order)
8489 {
8490 asection *o = lo->u.indirect.section;
8491
8492 if ((o->size % bed->s->sizeof_rela) == 0)
8493 {
8494 if ((o->size % bed->s->sizeof_rel) == 0)
8495 /* Section size is divisible by both rel and rela sizes.
8496 It is of no help to us. */
8497 ;
8498 else
8499 {
8500 /* Section size is only divisible by rela. */
8501 if (use_rela_initialised && (use_rela == FALSE))
8502 {
8503 _bfd_error_handler
8504 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8505 bfd_set_error (bfd_error_invalid_operation);
8506 return 0;
8507 }
8508 else
8509 {
8510 use_rela = TRUE;
8511 use_rela_initialised = TRUE;
8512 }
8513 }
8514 }
8515 else if ((o->size % bed->s->sizeof_rel) == 0)
8516 {
8517 /* Section size is only divisible by rel. */
8518 if (use_rela_initialised && (use_rela == TRUE))
8519 {
8520 _bfd_error_handler
8521 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8522 bfd_set_error (bfd_error_invalid_operation);
8523 return 0;
8524 }
8525 else
8526 {
8527 use_rela = FALSE;
8528 use_rela_initialised = TRUE;
8529 }
8530 }
8531 else
8532 {
8533 /* The section size is not divisible by either - something is wrong. */
8534 _bfd_error_handler
8535 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8536 bfd_set_error (bfd_error_invalid_operation);
8537 return 0;
8538 }
8539 }
8540
8541 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8542 if (lo->type == bfd_indirect_link_order)
8543 {
8544 asection *o = lo->u.indirect.section;
8545
8546 if ((o->size % bed->s->sizeof_rela) == 0)
8547 {
8548 if ((o->size % bed->s->sizeof_rel) == 0)
8549 /* Section size is divisible by both rel and rela sizes.
8550 It is of no help to us. */
8551 ;
8552 else
8553 {
8554 /* Section size is only divisible by rela. */
8555 if (use_rela_initialised && (use_rela == FALSE))
8556 {
8557 _bfd_error_handler
8558 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8559 bfd_set_error (bfd_error_invalid_operation);
8560 return 0;
8561 }
8562 else
8563 {
8564 use_rela = TRUE;
8565 use_rela_initialised = TRUE;
8566 }
8567 }
8568 }
8569 else if ((o->size % bed->s->sizeof_rel) == 0)
8570 {
8571 /* Section size is only divisible by rel. */
8572 if (use_rela_initialised && (use_rela == TRUE))
8573 {
8574 _bfd_error_handler
8575 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8576 bfd_set_error (bfd_error_invalid_operation);
8577 return 0;
8578 }
8579 else
8580 {
8581 use_rela = FALSE;
8582 use_rela_initialised = TRUE;
8583 }
8584 }
8585 else
8586 {
8587 /* The section size is not divisible by either - something is wrong. */
8588 _bfd_error_handler
8589 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8590 bfd_set_error (bfd_error_invalid_operation);
8591 return 0;
8592 }
8593 }
8594
8595 if (! use_rela_initialised)
8596 /* Make a guess. */
8597 use_rela = TRUE;
c152c796 8598 }
fc66a176
L
8599 else if (rela_dyn != NULL && rela_dyn->size > 0)
8600 use_rela = TRUE;
8601 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8602 use_rela = FALSE;
c152c796 8603 else
fc66a176 8604 return 0;
3410fea8
NC
8605
8606 if (use_rela)
c152c796 8607 {
3410fea8 8608 dynamic_relocs = rela_dyn;
c152c796
AM
8609 ext_size = bed->s->sizeof_rela;
8610 swap_in = bed->s->swap_reloca_in;
8611 swap_out = bed->s->swap_reloca_out;
8612 }
3410fea8
NC
8613 else
8614 {
8615 dynamic_relocs = rel_dyn;
8616 ext_size = bed->s->sizeof_rel;
8617 swap_in = bed->s->swap_reloc_in;
8618 swap_out = bed->s->swap_reloc_out;
8619 }
c152c796
AM
8620
8621 size = 0;
3410fea8 8622 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8623 if (lo->type == bfd_indirect_link_order)
3410fea8 8624 size += lo->u.indirect.section->size;
c152c796 8625
3410fea8 8626 if (size != dynamic_relocs->size)
c152c796
AM
8627 return 0;
8628
8629 sort_elt = (sizeof (struct elf_link_sort_rela)
8630 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8631
8632 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8633 if (count == 0)
8634 return 0;
a50b1753 8635 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8636
c152c796
AM
8637 if (sort == NULL)
8638 {
8639 (*info->callbacks->warning)
8640 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8641 return 0;
8642 }
8643
8644 if (bed->s->arch_size == 32)
8645 r_sym_mask = ~(bfd_vma) 0xff;
8646 else
8647 r_sym_mask = ~(bfd_vma) 0xffffffff;
8648
3410fea8 8649 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8650 if (lo->type == bfd_indirect_link_order)
8651 {
8652 bfd_byte *erel, *erelend;
8653 asection *o = lo->u.indirect.section;
8654
1da212d6
AM
8655 if (o->contents == NULL && o->size != 0)
8656 {
8657 /* This is a reloc section that is being handled as a normal
8658 section. See bfd_section_from_shdr. We can't combine
8659 relocs in this case. */
8660 free (sort);
8661 return 0;
8662 }
c152c796 8663 erel = o->contents;
eea6121a 8664 erelend = o->contents + o->size;
5dabe785 8665 /* FIXME: octets_per_byte. */
c152c796 8666 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8667
c152c796
AM
8668 while (erel < erelend)
8669 {
8670 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8671
c152c796 8672 (*swap_in) (abfd, erel, s->rela);
7e612e98 8673 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8674 s->u.sym_mask = r_sym_mask;
8675 p += sort_elt;
8676 erel += ext_size;
8677 }
8678 }
8679
8680 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8681
8682 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8683 {
8684 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8685 if (s->type != reloc_class_relative)
8686 break;
8687 }
8688 ret = i;
8689 s_non_relative = p;
8690
8691 sq = (struct elf_link_sort_rela *) s_non_relative;
8692 for (; i < count; i++, p += sort_elt)
8693 {
8694 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8695 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8696 sq = sp;
8697 sp->u.offset = sq->rela->r_offset;
8698 }
8699
8700 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8701
3410fea8 8702 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8703 if (lo->type == bfd_indirect_link_order)
8704 {
8705 bfd_byte *erel, *erelend;
8706 asection *o = lo->u.indirect.section;
8707
8708 erel = o->contents;
eea6121a 8709 erelend = o->contents + o->size;
5dabe785 8710 /* FIXME: octets_per_byte. */
c152c796
AM
8711 p = sort + o->output_offset / ext_size * sort_elt;
8712 while (erel < erelend)
8713 {
8714 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8715 (*swap_out) (abfd, s->rela, erel);
8716 p += sort_elt;
8717 erel += ext_size;
8718 }
8719 }
8720
8721 free (sort);
3410fea8 8722 *psec = dynamic_relocs;
c152c796
AM
8723 return ret;
8724}
8725
ef10c3ac 8726/* Add a symbol to the output symbol string table. */
c152c796 8727
6e0b88f1 8728static int
ef10c3ac
L
8729elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8730 const char *name,
8731 Elf_Internal_Sym *elfsym,
8732 asection *input_sec,
8733 struct elf_link_hash_entry *h)
c152c796 8734{
6e0b88f1 8735 int (*output_symbol_hook)
c152c796
AM
8736 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8737 struct elf_link_hash_entry *);
ef10c3ac 8738 struct elf_link_hash_table *hash_table;
c152c796 8739 const struct elf_backend_data *bed;
ef10c3ac 8740 bfd_size_type strtabsize;
c152c796 8741
8539e4e8
AM
8742 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8743
8b127cbc 8744 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8745 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8746 if (output_symbol_hook != NULL)
8747 {
8b127cbc 8748 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8749 if (ret != 1)
8750 return ret;
c152c796
AM
8751 }
8752
ef10c3ac
L
8753 if (name == NULL
8754 || *name == '\0'
8755 || (input_sec->flags & SEC_EXCLUDE))
8756 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8757 else
8758 {
ef10c3ac
L
8759 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8760 to get the final offset for st_name. */
8761 elfsym->st_name
8762 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8763 name, FALSE);
c152c796 8764 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8765 return 0;
c152c796
AM
8766 }
8767
ef10c3ac
L
8768 hash_table = elf_hash_table (flinfo->info);
8769 strtabsize = hash_table->strtabsize;
8770 if (strtabsize <= hash_table->strtabcount)
c152c796 8771 {
ef10c3ac
L
8772 strtabsize += strtabsize;
8773 hash_table->strtabsize = strtabsize;
8774 strtabsize *= sizeof (*hash_table->strtab);
8775 hash_table->strtab
8776 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8777 strtabsize);
8778 if (hash_table->strtab == NULL)
6e0b88f1 8779 return 0;
c152c796 8780 }
ef10c3ac
L
8781 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8782 hash_table->strtab[hash_table->strtabcount].dest_index
8783 = hash_table->strtabcount;
8784 hash_table->strtab[hash_table->strtabcount].destshndx_index
8785 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8786
8787 bfd_get_symcount (flinfo->output_bfd) += 1;
8788 hash_table->strtabcount += 1;
8789
8790 return 1;
8791}
8792
8793/* Swap symbols out to the symbol table and flush the output symbols to
8794 the file. */
8795
8796static bfd_boolean
8797elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8798{
8799 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8800 bfd_size_type amt, i;
8801 const struct elf_backend_data *bed;
8802 bfd_byte *symbuf;
8803 Elf_Internal_Shdr *hdr;
8804 file_ptr pos;
8805 bfd_boolean ret;
8806
8807 if (!hash_table->strtabcount)
8808 return TRUE;
8809
8810 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8811
8812 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8813
ef10c3ac
L
8814 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8815 symbuf = (bfd_byte *) bfd_malloc (amt);
8816 if (symbuf == NULL)
8817 return FALSE;
1b786873 8818
ef10c3ac 8819 if (flinfo->symshndxbuf)
c152c796 8820 {
ef10c3ac
L
8821 amt = (sizeof (Elf_External_Sym_Shndx)
8822 * (bfd_get_symcount (flinfo->output_bfd)));
8823 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8824 if (flinfo->symshndxbuf == NULL)
c152c796 8825 {
ef10c3ac
L
8826 free (symbuf);
8827 return FALSE;
c152c796 8828 }
c152c796
AM
8829 }
8830
ef10c3ac
L
8831 for (i = 0; i < hash_table->strtabcount; i++)
8832 {
8833 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8834 if (elfsym->sym.st_name == (unsigned long) -1)
8835 elfsym->sym.st_name = 0;
8836 else
8837 elfsym->sym.st_name
8838 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8839 elfsym->sym.st_name);
8840 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8841 ((bfd_byte *) symbuf
8842 + (elfsym->dest_index
8843 * bed->s->sizeof_sym)),
8844 (flinfo->symshndxbuf
8845 + elfsym->destshndx_index));
8846 }
8847
8848 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8849 pos = hdr->sh_offset + hdr->sh_size;
8850 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8851 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8852 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8853 {
8854 hdr->sh_size += amt;
8855 ret = TRUE;
8856 }
8857 else
8858 ret = FALSE;
c152c796 8859
ef10c3ac
L
8860 free (symbuf);
8861
8862 free (hash_table->strtab);
8863 hash_table->strtab = NULL;
8864
8865 return ret;
c152c796
AM
8866}
8867
c0d5a53d
L
8868/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8869
8870static bfd_boolean
8871check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8872{
4fbb74a6
AM
8873 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8874 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8875 {
8876 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8877 beyond 64k. */
c0d5a53d
L
8878 (*_bfd_error_handler)
8879 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8880 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8881 bfd_set_error (bfd_error_nonrepresentable_section);
8882 return FALSE;
8883 }
8884 return TRUE;
8885}
8886
c152c796
AM
8887/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8888 allowing an unsatisfied unversioned symbol in the DSO to match a
8889 versioned symbol that would normally require an explicit version.
8890 We also handle the case that a DSO references a hidden symbol
8891 which may be satisfied by a versioned symbol in another DSO. */
8892
8893static bfd_boolean
8894elf_link_check_versioned_symbol (struct bfd_link_info *info,
8895 const struct elf_backend_data *bed,
8896 struct elf_link_hash_entry *h)
8897{
8898 bfd *abfd;
8899 struct elf_link_loaded_list *loaded;
8900
8901 if (!is_elf_hash_table (info->hash))
8902 return FALSE;
8903
90c984fc
L
8904 /* Check indirect symbol. */
8905 while (h->root.type == bfd_link_hash_indirect)
8906 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8907
c152c796
AM
8908 switch (h->root.type)
8909 {
8910 default:
8911 abfd = NULL;
8912 break;
8913
8914 case bfd_link_hash_undefined:
8915 case bfd_link_hash_undefweak:
8916 abfd = h->root.u.undef.abfd;
8917 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8918 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8919 return FALSE;
8920 break;
8921
8922 case bfd_link_hash_defined:
8923 case bfd_link_hash_defweak:
8924 abfd = h->root.u.def.section->owner;
8925 break;
8926
8927 case bfd_link_hash_common:
8928 abfd = h->root.u.c.p->section->owner;
8929 break;
8930 }
8931 BFD_ASSERT (abfd != NULL);
8932
8933 for (loaded = elf_hash_table (info)->loaded;
8934 loaded != NULL;
8935 loaded = loaded->next)
8936 {
8937 bfd *input;
8938 Elf_Internal_Shdr *hdr;
8939 bfd_size_type symcount;
8940 bfd_size_type extsymcount;
8941 bfd_size_type extsymoff;
8942 Elf_Internal_Shdr *versymhdr;
8943 Elf_Internal_Sym *isym;
8944 Elf_Internal_Sym *isymend;
8945 Elf_Internal_Sym *isymbuf;
8946 Elf_External_Versym *ever;
8947 Elf_External_Versym *extversym;
8948
8949 input = loaded->abfd;
8950
8951 /* We check each DSO for a possible hidden versioned definition. */
8952 if (input == abfd
8953 || (input->flags & DYNAMIC) == 0
8954 || elf_dynversym (input) == 0)
8955 continue;
8956
8957 hdr = &elf_tdata (input)->dynsymtab_hdr;
8958
8959 symcount = hdr->sh_size / bed->s->sizeof_sym;
8960 if (elf_bad_symtab (input))
8961 {
8962 extsymcount = symcount;
8963 extsymoff = 0;
8964 }
8965 else
8966 {
8967 extsymcount = symcount - hdr->sh_info;
8968 extsymoff = hdr->sh_info;
8969 }
8970
8971 if (extsymcount == 0)
8972 continue;
8973
8974 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8975 NULL, NULL, NULL);
8976 if (isymbuf == NULL)
8977 return FALSE;
8978
8979 /* Read in any version definitions. */
8980 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8981 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8982 if (extversym == NULL)
8983 goto error_ret;
8984
8985 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8986 || (bfd_bread (extversym, versymhdr->sh_size, input)
8987 != versymhdr->sh_size))
8988 {
8989 free (extversym);
8990 error_ret:
8991 free (isymbuf);
8992 return FALSE;
8993 }
8994
8995 ever = extversym + extsymoff;
8996 isymend = isymbuf + extsymcount;
8997 for (isym = isymbuf; isym < isymend; isym++, ever++)
8998 {
8999 const char *name;
9000 Elf_Internal_Versym iver;
9001 unsigned short version_index;
9002
9003 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9004 || isym->st_shndx == SHN_UNDEF)
9005 continue;
9006
9007 name = bfd_elf_string_from_elf_section (input,
9008 hdr->sh_link,
9009 isym->st_name);
9010 if (strcmp (name, h->root.root.string) != 0)
9011 continue;
9012
9013 _bfd_elf_swap_versym_in (input, ever, &iver);
9014
d023c380
L
9015 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9016 && !(h->def_regular
9017 && h->forced_local))
c152c796
AM
9018 {
9019 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9020 have provided a definition for the undefined sym unless
9021 it is defined in a non-shared object and forced local.
9022 */
c152c796
AM
9023 abort ();
9024 }
9025
9026 version_index = iver.vs_vers & VERSYM_VERSION;
9027 if (version_index == 1 || version_index == 2)
9028 {
9029 /* This is the base or first version. We can use it. */
9030 free (extversym);
9031 free (isymbuf);
9032 return TRUE;
9033 }
9034 }
9035
9036 free (extversym);
9037 free (isymbuf);
9038 }
9039
9040 return FALSE;
9041}
9042
b8871f35
L
9043/* Convert ELF common symbol TYPE. */
9044
9045static int
9046elf_link_convert_common_type (struct bfd_link_info *info, int type)
9047{
9048 /* Commom symbol can only appear in relocatable link. */
9049 if (!bfd_link_relocatable (info))
9050 abort ();
9051 switch (info->elf_stt_common)
9052 {
9053 case unchanged:
9054 break;
9055 case elf_stt_common:
9056 type = STT_COMMON;
9057 break;
9058 case no_elf_stt_common:
9059 type = STT_OBJECT;
9060 break;
9061 }
9062 return type;
9063}
9064
c152c796
AM
9065/* Add an external symbol to the symbol table. This is called from
9066 the hash table traversal routine. When generating a shared object,
9067 we go through the symbol table twice. The first time we output
9068 anything that might have been forced to local scope in a version
9069 script. The second time we output the symbols that are still
9070 global symbols. */
9071
9072static bfd_boolean
7686d77d 9073elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9074{
7686d77d 9075 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9076 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9077 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9078 bfd_boolean strip;
9079 Elf_Internal_Sym sym;
9080 asection *input_sec;
9081 const struct elf_backend_data *bed;
6e0b88f1
AM
9082 long indx;
9083 int ret;
b8871f35 9084 unsigned int type;
6e33951e
L
9085 /* A symbol is bound locally if it is forced local or it is locally
9086 defined, hidden versioned, not referenced by shared library and
9087 not exported when linking executable. */
9088 bfd_boolean local_bind = (h->forced_local
0e1862bb 9089 || (bfd_link_executable (flinfo->info)
6e33951e
L
9090 && !flinfo->info->export_dynamic
9091 && !h->dynamic
9092 && !h->ref_dynamic
9093 && h->def_regular
422f1182 9094 && h->versioned == versioned_hidden));
c152c796
AM
9095
9096 if (h->root.type == bfd_link_hash_warning)
9097 {
9098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9099 if (h->root.type == bfd_link_hash_new)
9100 return TRUE;
9101 }
9102
9103 /* Decide whether to output this symbol in this pass. */
9104 if (eoinfo->localsyms)
9105 {
6e33951e 9106 if (!local_bind)
c152c796
AM
9107 return TRUE;
9108 }
9109 else
9110 {
6e33951e 9111 if (local_bind)
c152c796
AM
9112 return TRUE;
9113 }
9114
8b127cbc 9115 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9116
12ac1cf5 9117 if (h->root.type == bfd_link_hash_undefined)
c152c796 9118 {
12ac1cf5
NC
9119 /* If we have an undefined symbol reference here then it must have
9120 come from a shared library that is being linked in. (Undefined
98da7939
L
9121 references in regular files have already been handled unless
9122 they are in unreferenced sections which are removed by garbage
9123 collection). */
12ac1cf5
NC
9124 bfd_boolean ignore_undef = FALSE;
9125
9126 /* Some symbols may be special in that the fact that they're
9127 undefined can be safely ignored - let backend determine that. */
9128 if (bed->elf_backend_ignore_undef_symbol)
9129 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9130
9131 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9132 if (!ignore_undef
12ac1cf5 9133 && h->ref_dynamic
8b127cbc
AM
9134 && (!h->ref_regular || flinfo->info->gc_sections)
9135 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9136 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9137 {
9138 if (!(flinfo->info->callbacks->undefined_symbol
9139 (flinfo->info, h->root.root.string,
9140 h->ref_regular ? NULL : h->root.u.undef.abfd,
9141 NULL, 0,
9142 (flinfo->info->unresolved_syms_in_shared_libs
9143 == RM_GENERATE_ERROR))))
12ac1cf5 9144 {
17d078c5 9145 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
9146 eoinfo->failed = TRUE;
9147 return FALSE;
9148 }
c152c796
AM
9149 }
9150 }
9151
9152 /* We should also warn if a forced local symbol is referenced from
9153 shared libraries. */
0e1862bb 9154 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9155 && h->forced_local
9156 && h->ref_dynamic
371a5866 9157 && h->def_regular
f5385ebf 9158 && !h->dynamic_def
ee659f1f 9159 && h->ref_dynamic_nonweak
8b127cbc 9160 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9161 {
17d078c5
AM
9162 bfd *def_bfd;
9163 const char *msg;
90c984fc
L
9164 struct elf_link_hash_entry *hi = h;
9165
9166 /* Check indirect symbol. */
9167 while (hi->root.type == bfd_link_hash_indirect)
9168 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9169
9170 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9171 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9172 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9173 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9174 else
9175 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9176 def_bfd = flinfo->output_bfd;
90c984fc
L
9177 if (hi->root.u.def.section != bfd_abs_section_ptr)
9178 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9179 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9180 h->root.root.string);
9181 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9182 eoinfo->failed = TRUE;
9183 return FALSE;
9184 }
9185
9186 /* We don't want to output symbols that have never been mentioned by
9187 a regular file, or that we have been told to strip. However, if
9188 h->indx is set to -2, the symbol is used by a reloc and we must
9189 output it. */
d983c8c5 9190 strip = FALSE;
c152c796 9191 if (h->indx == -2)
d983c8c5 9192 ;
f5385ebf 9193 else if ((h->def_dynamic
77cfaee6
AM
9194 || h->ref_dynamic
9195 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9196 && !h->def_regular
9197 && !h->ref_regular)
c152c796 9198 strip = TRUE;
8b127cbc 9199 else if (flinfo->info->strip == strip_all)
c152c796 9200 strip = TRUE;
8b127cbc
AM
9201 else if (flinfo->info->strip == strip_some
9202 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9203 h->root.root.string, FALSE, FALSE) == NULL)
9204 strip = TRUE;
d56d55e7
AM
9205 else if ((h->root.type == bfd_link_hash_defined
9206 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9207 && ((flinfo->info->strip_discarded
dbaa2011 9208 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9209 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9210 && h->root.u.def.section->owner != NULL
d56d55e7 9211 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9212 strip = TRUE;
9e2278f5
AM
9213 else if ((h->root.type == bfd_link_hash_undefined
9214 || h->root.type == bfd_link_hash_undefweak)
9215 && h->root.u.undef.abfd != NULL
9216 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9217 strip = TRUE;
c152c796 9218
b8871f35
L
9219 type = h->type;
9220
c152c796 9221 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9222 nothing else to do. However, if it is a forced local symbol or
9223 an ifunc symbol we need to give the backend finish_dynamic_symbol
9224 function a chance to make it dynamic. */
c152c796
AM
9225 if (strip
9226 && h->dynindx == -1
b8871f35 9227 && type != STT_GNU_IFUNC
f5385ebf 9228 && !h->forced_local)
c152c796
AM
9229 return TRUE;
9230
9231 sym.st_value = 0;
9232 sym.st_size = h->size;
9233 sym.st_other = h->other;
c152c796
AM
9234 switch (h->root.type)
9235 {
9236 default:
9237 case bfd_link_hash_new:
9238 case bfd_link_hash_warning:
9239 abort ();
9240 return FALSE;
9241
9242 case bfd_link_hash_undefined:
9243 case bfd_link_hash_undefweak:
9244 input_sec = bfd_und_section_ptr;
9245 sym.st_shndx = SHN_UNDEF;
9246 break;
9247
9248 case bfd_link_hash_defined:
9249 case bfd_link_hash_defweak:
9250 {
9251 input_sec = h->root.u.def.section;
9252 if (input_sec->output_section != NULL)
9253 {
9254 sym.st_shndx =
8b127cbc 9255 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9256 input_sec->output_section);
9257 if (sym.st_shndx == SHN_BAD)
9258 {
9259 (*_bfd_error_handler)
d003868e 9260 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9261 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9262 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9263 eoinfo->failed = TRUE;
9264 return FALSE;
9265 }
9266
9267 /* ELF symbols in relocatable files are section relative,
9268 but in nonrelocatable files they are virtual
9269 addresses. */
9270 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9271 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9272 {
9273 sym.st_value += input_sec->output_section->vma;
9274 if (h->type == STT_TLS)
9275 {
8b127cbc 9276 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9277 if (tls_sec != NULL)
9278 sym.st_value -= tls_sec->vma;
c152c796
AM
9279 }
9280 }
9281 }
9282 else
9283 {
9284 BFD_ASSERT (input_sec->owner == NULL
9285 || (input_sec->owner->flags & DYNAMIC) != 0);
9286 sym.st_shndx = SHN_UNDEF;
9287 input_sec = bfd_und_section_ptr;
9288 }
9289 }
9290 break;
9291
9292 case bfd_link_hash_common:
9293 input_sec = h->root.u.c.p->section;
a4d8e49b 9294 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9295 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9296 break;
9297
9298 case bfd_link_hash_indirect:
9299 /* These symbols are created by symbol versioning. They point
9300 to the decorated version of the name. For example, if the
9301 symbol foo@@GNU_1.2 is the default, which should be used when
9302 foo is used with no version, then we add an indirect symbol
9303 foo which points to foo@@GNU_1.2. We ignore these symbols,
9304 since the indirected symbol is already in the hash table. */
9305 return TRUE;
9306 }
9307
b8871f35
L
9308 if (type == STT_COMMON || type == STT_OBJECT)
9309 switch (h->root.type)
9310 {
9311 case bfd_link_hash_common:
9312 type = elf_link_convert_common_type (flinfo->info, type);
9313 break;
9314 case bfd_link_hash_defined:
9315 case bfd_link_hash_defweak:
9316 if (bed->common_definition (&sym))
9317 type = elf_link_convert_common_type (flinfo->info, type);
9318 else
9319 type = STT_OBJECT;
9320 break;
9321 case bfd_link_hash_undefined:
9322 case bfd_link_hash_undefweak:
9323 break;
9324 default:
9325 abort ();
9326 }
9327
9328 if (local_bind)
9329 {
9330 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9331 /* Turn off visibility on local symbol. */
9332 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9333 }
9334 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9335 else if (h->unique_global && h->def_regular)
9336 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9337 else if (h->root.type == bfd_link_hash_undefweak
9338 || h->root.type == bfd_link_hash_defweak)
9339 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9340 else
9341 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9342 sym.st_target_internal = h->target_internal;
9343
c152c796
AM
9344 /* Give the processor backend a chance to tweak the symbol value,
9345 and also to finish up anything that needs to be done for this
9346 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9347 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9348 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9349 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9350 && h->def_regular
0e1862bb 9351 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9352 || ((h->dynindx != -1
9353 || h->forced_local)
0e1862bb 9354 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9355 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9356 || h->root.type != bfd_link_hash_undefweak))
9357 || !h->forced_local)
8b127cbc 9358 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9359 {
9360 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9361 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9362 {
9363 eoinfo->failed = TRUE;
9364 return FALSE;
9365 }
9366 }
9367
9368 /* If we are marking the symbol as undefined, and there are no
9369 non-weak references to this symbol from a regular object, then
9370 mark the symbol as weak undefined; if there are non-weak
9371 references, mark the symbol as strong. We can't do this earlier,
9372 because it might not be marked as undefined until the
9373 finish_dynamic_symbol routine gets through with it. */
9374 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9375 && h->ref_regular
c152c796
AM
9376 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9377 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9378 {
9379 int bindtype;
b8871f35 9380 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9381
9382 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9383 if (type == STT_GNU_IFUNC)
9384 type = STT_FUNC;
c152c796 9385
f5385ebf 9386 if (h->ref_regular_nonweak)
c152c796
AM
9387 bindtype = STB_GLOBAL;
9388 else
9389 bindtype = STB_WEAK;
2955ec4c 9390 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9391 }
9392
bda987c2
CD
9393 /* If this is a symbol defined in a dynamic library, don't use the
9394 symbol size from the dynamic library. Relinking an executable
9395 against a new library may introduce gratuitous changes in the
9396 executable's symbols if we keep the size. */
9397 if (sym.st_shndx == SHN_UNDEF
9398 && !h->def_regular
9399 && h->def_dynamic)
9400 sym.st_size = 0;
9401
c152c796
AM
9402 /* If a non-weak symbol with non-default visibility is not defined
9403 locally, it is a fatal error. */
0e1862bb 9404 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9405 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9406 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9407 && h->root.type == bfd_link_hash_undefined
f5385ebf 9408 && !h->def_regular)
c152c796 9409 {
17d078c5
AM
9410 const char *msg;
9411
9412 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9413 msg = _("%B: protected symbol `%s' isn't defined");
9414 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9415 msg = _("%B: internal symbol `%s' isn't defined");
9416 else
9417 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9418 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9419 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9420 eoinfo->failed = TRUE;
9421 return FALSE;
9422 }
9423
9424 /* If this symbol should be put in the .dynsym section, then put it
9425 there now. We already know the symbol index. We also fill in
9426 the entry in the .hash section. */
cae1fbbb 9427 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9428 && h->dynindx != -1
8b127cbc 9429 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9430 {
c152c796
AM
9431 bfd_byte *esym;
9432
90c984fc
L
9433 /* Since there is no version information in the dynamic string,
9434 if there is no version info in symbol version section, we will
1659f720 9435 have a run-time problem if not linking executable, referenced
6e33951e
L
9436 by shared library, not locally defined, or not bound locally.
9437 */
1659f720 9438 if (h->verinfo.verdef == NULL
6e33951e 9439 && !local_bind
0e1862bb 9440 && (!bfd_link_executable (flinfo->info)
1659f720
L
9441 || h->ref_dynamic
9442 || !h->def_regular))
90c984fc
L
9443 {
9444 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9445
9446 if (p && p [1] != '\0')
9447 {
9448 (*_bfd_error_handler)
9449 (_("%B: No symbol version section for versioned symbol `%s'"),
9450 flinfo->output_bfd, h->root.root.string);
9451 eoinfo->failed = TRUE;
9452 return FALSE;
9453 }
9454 }
9455
c152c796 9456 sym.st_name = h->dynstr_index;
cae1fbbb
L
9457 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9458 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9459 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9460 {
9461 eoinfo->failed = TRUE;
9462 return FALSE;
9463 }
8b127cbc 9464 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9465
8b127cbc 9466 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9467 {
9468 size_t hash_entry_size;
9469 bfd_byte *bucketpos;
9470 bfd_vma chain;
41198d0c
L
9471 size_t bucketcount;
9472 size_t bucket;
9473
8b127cbc 9474 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9475 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9476
9477 hash_entry_size
8b127cbc
AM
9478 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9479 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9480 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9481 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9482 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9483 bucketpos);
9484 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9485 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9486 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9487 }
c152c796 9488
8b127cbc 9489 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9490 {
9491 Elf_Internal_Versym iversym;
9492 Elf_External_Versym *eversym;
9493
f5385ebf 9494 if (!h->def_regular)
c152c796 9495 {
7b20f099
AM
9496 if (h->verinfo.verdef == NULL
9497 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9498 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9499 iversym.vs_vers = 0;
9500 else
9501 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9502 }
9503 else
9504 {
9505 if (h->verinfo.vertree == NULL)
9506 iversym.vs_vers = 1;
9507 else
9508 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9509 if (flinfo->info->create_default_symver)
3e3b46e5 9510 iversym.vs_vers++;
c152c796
AM
9511 }
9512
422f1182 9513 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9514 defined locally. */
422f1182 9515 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9516 iversym.vs_vers |= VERSYM_HIDDEN;
9517
8b127cbc 9518 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9519 eversym += h->dynindx;
8b127cbc 9520 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9521 }
9522 }
9523
d983c8c5
AM
9524 /* If the symbol is undefined, and we didn't output it to .dynsym,
9525 strip it from .symtab too. Obviously we can't do this for
9526 relocatable output or when needed for --emit-relocs. */
9527 else if (input_sec == bfd_und_section_ptr
9528 && h->indx != -2
0e1862bb 9529 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9530 return TRUE;
9531 /* Also strip others that we couldn't earlier due to dynamic symbol
9532 processing. */
9533 if (strip)
9534 return TRUE;
9535 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9536 return TRUE;
9537
2ec55de3
AM
9538 /* Output a FILE symbol so that following locals are not associated
9539 with the wrong input file. We need one for forced local symbols
9540 if we've seen more than one FILE symbol or when we have exactly
9541 one FILE symbol but global symbols are present in a file other
9542 than the one with the FILE symbol. We also need one if linker
9543 defined symbols are present. In practice these conditions are
9544 always met, so just emit the FILE symbol unconditionally. */
9545 if (eoinfo->localsyms
9546 && !eoinfo->file_sym_done
9547 && eoinfo->flinfo->filesym_count != 0)
9548 {
9549 Elf_Internal_Sym fsym;
9550
9551 memset (&fsym, 0, sizeof (fsym));
9552 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9553 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9554 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9555 bfd_und_section_ptr, NULL))
2ec55de3
AM
9556 return FALSE;
9557
9558 eoinfo->file_sym_done = TRUE;
9559 }
9560
8b127cbc 9561 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9562 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9563 input_sec, h);
6e0b88f1 9564 if (ret == 0)
c152c796
AM
9565 {
9566 eoinfo->failed = TRUE;
9567 return FALSE;
9568 }
6e0b88f1
AM
9569 else if (ret == 1)
9570 h->indx = indx;
9571 else if (h->indx == -2)
9572 abort();
c152c796
AM
9573
9574 return TRUE;
9575}
9576
cdd3575c
AM
9577/* Return TRUE if special handling is done for relocs in SEC against
9578 symbols defined in discarded sections. */
9579
c152c796
AM
9580static bfd_boolean
9581elf_section_ignore_discarded_relocs (asection *sec)
9582{
9583 const struct elf_backend_data *bed;
9584
cdd3575c
AM
9585 switch (sec->sec_info_type)
9586 {
dbaa2011
AM
9587 case SEC_INFO_TYPE_STABS:
9588 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9589 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9590 return TRUE;
9591 default:
9592 break;
9593 }
c152c796
AM
9594
9595 bed = get_elf_backend_data (sec->owner);
9596 if (bed->elf_backend_ignore_discarded_relocs != NULL
9597 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9598 return TRUE;
9599
9600 return FALSE;
9601}
9602
9e66c942
AM
9603/* Return a mask saying how ld should treat relocations in SEC against
9604 symbols defined in discarded sections. If this function returns
9605 COMPLAIN set, ld will issue a warning message. If this function
9606 returns PRETEND set, and the discarded section was link-once and the
9607 same size as the kept link-once section, ld will pretend that the
9608 symbol was actually defined in the kept section. Otherwise ld will
9609 zero the reloc (at least that is the intent, but some cooperation by
9610 the target dependent code is needed, particularly for REL targets). */
9611
8a696751
AM
9612unsigned int
9613_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9614{
9e66c942 9615 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9616 return PRETEND;
cdd3575c
AM
9617
9618 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9619 return 0;
cdd3575c
AM
9620
9621 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9622 return 0;
cdd3575c 9623
9e66c942 9624 return COMPLAIN | PRETEND;
cdd3575c
AM
9625}
9626
3d7f7666
L
9627/* Find a match between a section and a member of a section group. */
9628
9629static asection *
c0f00686
L
9630match_group_member (asection *sec, asection *group,
9631 struct bfd_link_info *info)
3d7f7666
L
9632{
9633 asection *first = elf_next_in_group (group);
9634 asection *s = first;
9635
9636 while (s != NULL)
9637 {
c0f00686 9638 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9639 return s;
9640
83180ade 9641 s = elf_next_in_group (s);
3d7f7666
L
9642 if (s == first)
9643 break;
9644 }
9645
9646 return NULL;
9647}
9648
01b3c8ab 9649/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9650 to replace it. Return the replacement if it is OK. Otherwise return
9651 NULL. */
01b3c8ab
L
9652
9653asection *
c0f00686 9654_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9655{
9656 asection *kept;
9657
9658 kept = sec->kept_section;
9659 if (kept != NULL)
9660 {
c2370991 9661 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9662 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9663 if (kept != NULL
9664 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9665 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9666 kept = NULL;
c2370991 9667 sec->kept_section = kept;
01b3c8ab
L
9668 }
9669 return kept;
9670}
9671
c152c796
AM
9672/* Link an input file into the linker output file. This function
9673 handles all the sections and relocations of the input file at once.
9674 This is so that we only have to read the local symbols once, and
9675 don't have to keep them in memory. */
9676
9677static bfd_boolean
8b127cbc 9678elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9679{
ece5ef60 9680 int (*relocate_section)
c152c796
AM
9681 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9682 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9683 bfd *output_bfd;
9684 Elf_Internal_Shdr *symtab_hdr;
9685 size_t locsymcount;
9686 size_t extsymoff;
9687 Elf_Internal_Sym *isymbuf;
9688 Elf_Internal_Sym *isym;
9689 Elf_Internal_Sym *isymend;
9690 long *pindex;
9691 asection **ppsection;
9692 asection *o;
9693 const struct elf_backend_data *bed;
c152c796 9694 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9695 bfd_size_type address_size;
9696 bfd_vma r_type_mask;
9697 int r_sym_shift;
ffbc01cc 9698 bfd_boolean have_file_sym = FALSE;
c152c796 9699
8b127cbc 9700 output_bfd = flinfo->output_bfd;
c152c796
AM
9701 bed = get_elf_backend_data (output_bfd);
9702 relocate_section = bed->elf_backend_relocate_section;
9703
9704 /* If this is a dynamic object, we don't want to do anything here:
9705 we don't want the local symbols, and we don't want the section
9706 contents. */
9707 if ((input_bfd->flags & DYNAMIC) != 0)
9708 return TRUE;
9709
c152c796
AM
9710 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9711 if (elf_bad_symtab (input_bfd))
9712 {
9713 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9714 extsymoff = 0;
9715 }
9716 else
9717 {
9718 locsymcount = symtab_hdr->sh_info;
9719 extsymoff = symtab_hdr->sh_info;
9720 }
9721
9722 /* Read the local symbols. */
9723 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9724 if (isymbuf == NULL && locsymcount != 0)
9725 {
9726 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9727 flinfo->internal_syms,
9728 flinfo->external_syms,
9729 flinfo->locsym_shndx);
c152c796
AM
9730 if (isymbuf == NULL)
9731 return FALSE;
9732 }
9733
9734 /* Find local symbol sections and adjust values of symbols in
9735 SEC_MERGE sections. Write out those local symbols we know are
9736 going into the output file. */
9737 isymend = isymbuf + locsymcount;
8b127cbc 9738 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9739 isym < isymend;
9740 isym++, pindex++, ppsection++)
9741 {
9742 asection *isec;
9743 const char *name;
9744 Elf_Internal_Sym osym;
6e0b88f1
AM
9745 long indx;
9746 int ret;
c152c796
AM
9747
9748 *pindex = -1;
9749
9750 if (elf_bad_symtab (input_bfd))
9751 {
9752 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9753 {
9754 *ppsection = NULL;
9755 continue;
9756 }
9757 }
9758
9759 if (isym->st_shndx == SHN_UNDEF)
9760 isec = bfd_und_section_ptr;
c152c796
AM
9761 else if (isym->st_shndx == SHN_ABS)
9762 isec = bfd_abs_section_ptr;
9763 else if (isym->st_shndx == SHN_COMMON)
9764 isec = bfd_com_section_ptr;
9765 else
9766 {
cb33740c
AM
9767 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9768 if (isec == NULL)
9769 {
9770 /* Don't attempt to output symbols with st_shnx in the
9771 reserved range other than SHN_ABS and SHN_COMMON. */
9772 *ppsection = NULL;
9773 continue;
9774 }
dbaa2011 9775 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9776 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9777 isym->st_value =
9778 _bfd_merged_section_offset (output_bfd, &isec,
9779 elf_section_data (isec)->sec_info,
9780 isym->st_value);
c152c796
AM
9781 }
9782
9783 *ppsection = isec;
9784
d983c8c5
AM
9785 /* Don't output the first, undefined, symbol. In fact, don't
9786 output any undefined local symbol. */
9787 if (isec == bfd_und_section_ptr)
c152c796
AM
9788 continue;
9789
9790 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9791 {
9792 /* We never output section symbols. Instead, we use the
9793 section symbol of the corresponding section in the output
9794 file. */
9795 continue;
9796 }
9797
9798 /* If we are stripping all symbols, we don't want to output this
9799 one. */
8b127cbc 9800 if (flinfo->info->strip == strip_all)
c152c796
AM
9801 continue;
9802
9803 /* If we are discarding all local symbols, we don't want to
9804 output this one. If we are generating a relocatable output
9805 file, then some of the local symbols may be required by
9806 relocs; we output them below as we discover that they are
9807 needed. */
8b127cbc 9808 if (flinfo->info->discard == discard_all)
c152c796
AM
9809 continue;
9810
9811 /* If this symbol is defined in a section which we are
f02571c5
AM
9812 discarding, we don't need to keep it. */
9813 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9814 && isym->st_shndx < SHN_LORESERVE
9815 && bfd_section_removed_from_list (output_bfd,
9816 isec->output_section))
e75a280b
L
9817 continue;
9818
c152c796
AM
9819 /* Get the name of the symbol. */
9820 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9821 isym->st_name);
9822 if (name == NULL)
9823 return FALSE;
9824
9825 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9826 if ((flinfo->info->strip == strip_some
9827 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9828 == NULL))
8b127cbc 9829 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9830 && (isec->flags & SEC_MERGE)
9831 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9832 || flinfo->info->discard == discard_l)
c152c796
AM
9833 && bfd_is_local_label_name (input_bfd, name)))
9834 continue;
9835
ffbc01cc
AM
9836 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9837 {
ce875075
AM
9838 if (input_bfd->lto_output)
9839 /* -flto puts a temp file name here. This means builds
9840 are not reproducible. Discard the symbol. */
9841 continue;
ffbc01cc
AM
9842 have_file_sym = TRUE;
9843 flinfo->filesym_count += 1;
9844 }
9845 if (!have_file_sym)
9846 {
9847 /* In the absence of debug info, bfd_find_nearest_line uses
9848 FILE symbols to determine the source file for local
9849 function symbols. Provide a FILE symbol here if input
9850 files lack such, so that their symbols won't be
9851 associated with a previous input file. It's not the
9852 source file, but the best we can do. */
9853 have_file_sym = TRUE;
9854 flinfo->filesym_count += 1;
9855 memset (&osym, 0, sizeof (osym));
9856 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9857 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9858 if (!elf_link_output_symstrtab (flinfo,
9859 (input_bfd->lto_output ? NULL
9860 : input_bfd->filename),
9861 &osym, bfd_abs_section_ptr,
9862 NULL))
ffbc01cc
AM
9863 return FALSE;
9864 }
9865
c152c796
AM
9866 osym = *isym;
9867
9868 /* Adjust the section index for the output file. */
9869 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9870 isec->output_section);
9871 if (osym.st_shndx == SHN_BAD)
9872 return FALSE;
9873
c152c796
AM
9874 /* ELF symbols in relocatable files are section relative, but
9875 in executable files they are virtual addresses. Note that
9876 this code assumes that all ELF sections have an associated
9877 BFD section with a reasonable value for output_offset; below
9878 we assume that they also have a reasonable value for
9879 output_section. Any special sections must be set up to meet
9880 these requirements. */
9881 osym.st_value += isec->output_offset;
0e1862bb 9882 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9883 {
9884 osym.st_value += isec->output_section->vma;
9885 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9886 {
9887 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9888 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9889 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9890 }
9891 }
9892
6e0b88f1 9893 indx = bfd_get_symcount (output_bfd);
ef10c3ac 9894 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 9895 if (ret == 0)
c152c796 9896 return FALSE;
6e0b88f1
AM
9897 else if (ret == 1)
9898 *pindex = indx;
c152c796
AM
9899 }
9900
310fd250
L
9901 if (bed->s->arch_size == 32)
9902 {
9903 r_type_mask = 0xff;
9904 r_sym_shift = 8;
9905 address_size = 4;
9906 }
9907 else
9908 {
9909 r_type_mask = 0xffffffff;
9910 r_sym_shift = 32;
9911 address_size = 8;
9912 }
9913
c152c796
AM
9914 /* Relocate the contents of each section. */
9915 sym_hashes = elf_sym_hashes (input_bfd);
9916 for (o = input_bfd->sections; o != NULL; o = o->next)
9917 {
9918 bfd_byte *contents;
9919
9920 if (! o->linker_mark)
9921 {
9922 /* This section was omitted from the link. */
9923 continue;
9924 }
9925
0e1862bb 9926 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
9927 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9928 {
9929 /* Deal with the group signature symbol. */
9930 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9931 unsigned long symndx = sec_data->this_hdr.sh_info;
9932 asection *osec = o->output_section;
9933
9934 if (symndx >= locsymcount
9935 || (elf_bad_symtab (input_bfd)
8b127cbc 9936 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9937 {
9938 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9939 while (h->root.type == bfd_link_hash_indirect
9940 || h->root.type == bfd_link_hash_warning)
9941 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9942 /* Arrange for symbol to be output. */
9943 h->indx = -2;
9944 elf_section_data (osec)->this_hdr.sh_info = -2;
9945 }
9946 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9947 {
9948 /* We'll use the output section target_index. */
8b127cbc 9949 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9950 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9951 }
9952 else
9953 {
8b127cbc 9954 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9955 {
9956 /* Otherwise output the local symbol now. */
9957 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9958 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 9959 const char *name;
6e0b88f1
AM
9960 long indx;
9961 int ret;
bcacc0f5
AM
9962
9963 name = bfd_elf_string_from_elf_section (input_bfd,
9964 symtab_hdr->sh_link,
9965 sym.st_name);
9966 if (name == NULL)
9967 return FALSE;
9968
9969 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9970 sec);
9971 if (sym.st_shndx == SHN_BAD)
9972 return FALSE;
9973
9974 sym.st_value += o->output_offset;
9975
6e0b88f1 9976 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
9977 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9978 NULL);
6e0b88f1 9979 if (ret == 0)
bcacc0f5 9980 return FALSE;
6e0b88f1 9981 else if (ret == 1)
8b127cbc 9982 flinfo->indices[symndx] = indx;
6e0b88f1
AM
9983 else
9984 abort ();
bcacc0f5
AM
9985 }
9986 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 9987 = flinfo->indices[symndx];
bcacc0f5
AM
9988 }
9989 }
9990
c152c796 9991 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9992 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9993 continue;
9994
9995 if ((o->flags & SEC_LINKER_CREATED) != 0)
9996 {
9997 /* Section was created by _bfd_elf_link_create_dynamic_sections
9998 or somesuch. */
9999 continue;
10000 }
10001
10002 /* Get the contents of the section. They have been cached by a
10003 relaxation routine. Note that o is a section in an input
10004 file, so the contents field will not have been set by any of
10005 the routines which work on output files. */
10006 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10007 {
10008 contents = elf_section_data (o)->this_hdr.contents;
10009 if (bed->caches_rawsize
10010 && o->rawsize != 0
10011 && o->rawsize < o->size)
10012 {
10013 memcpy (flinfo->contents, contents, o->rawsize);
10014 contents = flinfo->contents;
10015 }
10016 }
c152c796
AM
10017 else
10018 {
8b127cbc 10019 contents = flinfo->contents;
4a114e3e 10020 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10021 return FALSE;
10022 }
10023
10024 if ((o->flags & SEC_RELOC) != 0)
10025 {
10026 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10027 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10028 int action_discarded;
ece5ef60 10029 int ret;
c152c796
AM
10030
10031 /* Get the swapped relocs. */
10032 internal_relocs
8b127cbc
AM
10033 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10034 flinfo->internal_relocs, FALSE);
c152c796
AM
10035 if (internal_relocs == NULL
10036 && o->reloc_count > 0)
10037 return FALSE;
10038
310fd250
L
10039 /* We need to reverse-copy input .ctors/.dtors sections if
10040 they are placed in .init_array/.finit_array for output. */
10041 if (o->size > address_size
10042 && ((strncmp (o->name, ".ctors", 6) == 0
10043 && strcmp (o->output_section->name,
10044 ".init_array") == 0)
10045 || (strncmp (o->name, ".dtors", 6) == 0
10046 && strcmp (o->output_section->name,
10047 ".fini_array") == 0))
10048 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10049 {
310fd250
L
10050 if (o->size != o->reloc_count * address_size)
10051 {
10052 (*_bfd_error_handler)
10053 (_("error: %B: size of section %A is not "
10054 "multiple of address size"),
10055 input_bfd, o);
10056 bfd_set_error (bfd_error_on_input);
10057 return FALSE;
10058 }
10059 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10060 }
10061
0f02bbd9 10062 action_discarded = -1;
c152c796 10063 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10064 action_discarded = (*bed->action_discarded) (o);
10065
10066 /* Run through the relocs evaluating complex reloc symbols and
10067 looking for relocs against symbols from discarded sections
10068 or section symbols from removed link-once sections.
10069 Complain about relocs against discarded sections. Zero
10070 relocs against removed link-once sections. */
10071
10072 rel = internal_relocs;
10073 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10074 for ( ; rel < relend; rel++)
c152c796 10075 {
0f02bbd9
AM
10076 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10077 unsigned int s_type;
10078 asection **ps, *sec;
10079 struct elf_link_hash_entry *h = NULL;
10080 const char *sym_name;
c152c796 10081
0f02bbd9
AM
10082 if (r_symndx == STN_UNDEF)
10083 continue;
c152c796 10084
0f02bbd9
AM
10085 if (r_symndx >= locsymcount
10086 || (elf_bad_symtab (input_bfd)
8b127cbc 10087 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10088 {
10089 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10090
0f02bbd9
AM
10091 /* Badly formatted input files can contain relocs that
10092 reference non-existant symbols. Check here so that
10093 we do not seg fault. */
10094 if (h == NULL)
c152c796 10095 {
0f02bbd9 10096 char buffer [32];
dce669a1 10097
0f02bbd9
AM
10098 sprintf_vma (buffer, rel->r_info);
10099 (*_bfd_error_handler)
10100 (_("error: %B contains a reloc (0x%s) for section %A "
10101 "that references a non-existent global symbol"),
10102 input_bfd, o, buffer);
10103 bfd_set_error (bfd_error_bad_value);
10104 return FALSE;
10105 }
3b36f7e6 10106
0f02bbd9
AM
10107 while (h->root.type == bfd_link_hash_indirect
10108 || h->root.type == bfd_link_hash_warning)
10109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10110
0f02bbd9 10111 s_type = h->type;
cdd3575c 10112
9e2dec47 10113 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10114 mark the symbol as undefined. Note that the
10115 linker may attach linker created dynamic sections
10116 to the plugin bfd. Symbols defined in linker
10117 created sections are not plugin symbols. */
9e2dec47
L
10118 if (h->root.non_ir_ref
10119 && (h->root.type == bfd_link_hash_defined
10120 || h->root.type == bfd_link_hash_defweak)
10121 && (h->root.u.def.section->flags
10122 & SEC_LINKER_CREATED) == 0
10123 && h->root.u.def.section->owner != NULL
10124 && (h->root.u.def.section->owner->flags
10125 & BFD_PLUGIN) != 0)
10126 {
10127 h->root.type = bfd_link_hash_undefined;
10128 h->root.u.undef.abfd = h->root.u.def.section->owner;
10129 }
10130
0f02bbd9
AM
10131 ps = NULL;
10132 if (h->root.type == bfd_link_hash_defined
10133 || h->root.type == bfd_link_hash_defweak)
10134 ps = &h->root.u.def.section;
10135
10136 sym_name = h->root.root.string;
10137 }
10138 else
10139 {
10140 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10141
10142 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10143 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10144 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10145 sym, *ps);
10146 }
c152c796 10147
c301e700 10148 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10149 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10150 {
10151 bfd_vma val;
10152 bfd_vma dot = (rel->r_offset
10153 + o->output_offset + o->output_section->vma);
10154#ifdef DEBUG
10155 printf ("Encountered a complex symbol!");
10156 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10157 input_bfd->filename, o->name,
10158 (long) (rel - internal_relocs));
0f02bbd9
AM
10159 printf (" symbol: idx %8.8lx, name %s\n",
10160 r_symndx, sym_name);
10161 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10162 (unsigned long) rel->r_info,
10163 (unsigned long) rel->r_offset);
10164#endif
8b127cbc 10165 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10166 isymbuf, locsymcount, s_type == STT_SRELC))
10167 return FALSE;
10168
10169 /* Symbol evaluated OK. Update to absolute value. */
10170 set_symbol_value (input_bfd, isymbuf, locsymcount,
10171 r_symndx, val);
10172 continue;
10173 }
10174
10175 if (action_discarded != -1 && ps != NULL)
10176 {
cdd3575c
AM
10177 /* Complain if the definition comes from a
10178 discarded section. */
dbaa2011 10179 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10180 {
cf35638d 10181 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10182 if (action_discarded & COMPLAIN)
8b127cbc 10183 (*flinfo->info->callbacks->einfo)
e1fffbe6 10184 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10185 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10186 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10187
87e5235d 10188 /* Try to do the best we can to support buggy old
e0ae6d6f 10189 versions of gcc. Pretend that the symbol is
87e5235d
AM
10190 really defined in the kept linkonce section.
10191 FIXME: This is quite broken. Modifying the
10192 symbol here means we will be changing all later
e0ae6d6f 10193 uses of the symbol, not just in this section. */
0f02bbd9 10194 if (action_discarded & PRETEND)
87e5235d 10195 {
01b3c8ab
L
10196 asection *kept;
10197
c0f00686 10198 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10199 flinfo->info);
01b3c8ab 10200 if (kept != NULL)
87e5235d
AM
10201 {
10202 *ps = kept;
10203 continue;
10204 }
10205 }
c152c796
AM
10206 }
10207 }
10208 }
10209
10210 /* Relocate the section by invoking a back end routine.
10211
10212 The back end routine is responsible for adjusting the
10213 section contents as necessary, and (if using Rela relocs
10214 and generating a relocatable output file) adjusting the
10215 reloc addend as necessary.
10216
10217 The back end routine does not have to worry about setting
10218 the reloc address or the reloc symbol index.
10219
10220 The back end routine is given a pointer to the swapped in
10221 internal symbols, and can access the hash table entries
10222 for the external symbols via elf_sym_hashes (input_bfd).
10223
10224 When generating relocatable output, the back end routine
10225 must handle STB_LOCAL/STT_SECTION symbols specially. The
10226 output symbol is going to be a section symbol
10227 corresponding to the output section, which will require
10228 the addend to be adjusted. */
10229
8b127cbc 10230 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10231 input_bfd, o, contents,
10232 internal_relocs,
10233 isymbuf,
8b127cbc 10234 flinfo->sections);
ece5ef60 10235 if (!ret)
c152c796
AM
10236 return FALSE;
10237
ece5ef60 10238 if (ret == 2
0e1862bb 10239 || bfd_link_relocatable (flinfo->info)
8b127cbc 10240 || flinfo->info->emitrelocations)
c152c796
AM
10241 {
10242 Elf_Internal_Rela *irela;
d4730f92 10243 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10244 bfd_vma last_offset;
10245 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10246 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10247 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10248 unsigned int next_erel;
c152c796 10249 bfd_boolean rela_normal;
d4730f92 10250 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10251
d4730f92
BS
10252 esdi = elf_section_data (o);
10253 esdo = elf_section_data (o->output_section);
10254 rela_normal = FALSE;
c152c796
AM
10255
10256 /* Adjust the reloc addresses and symbol indices. */
10257
10258 irela = internal_relocs;
10259 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10260 rel_hash = esdo->rel.hashes + esdo->rel.count;
10261 /* We start processing the REL relocs, if any. When we reach
10262 IRELAMID in the loop, we switch to the RELA relocs. */
10263 irelamid = irela;
10264 if (esdi->rel.hdr != NULL)
10265 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10266 * bed->s->int_rels_per_ext_rel);
eac338cf 10267 rel_hash_list = rel_hash;
d4730f92 10268 rela_hash_list = NULL;
c152c796 10269 last_offset = o->output_offset;
0e1862bb 10270 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10271 last_offset += o->output_section->vma;
10272 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10273 {
10274 unsigned long r_symndx;
10275 asection *sec;
10276 Elf_Internal_Sym sym;
10277
10278 if (next_erel == bed->s->int_rels_per_ext_rel)
10279 {
10280 rel_hash++;
10281 next_erel = 0;
10282 }
10283
d4730f92
BS
10284 if (irela == irelamid)
10285 {
10286 rel_hash = esdo->rela.hashes + esdo->rela.count;
10287 rela_hash_list = rel_hash;
10288 rela_normal = bed->rela_normal;
10289 }
10290
c152c796 10291 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10292 flinfo->info, o,
c152c796
AM
10293 irela->r_offset);
10294 if (irela->r_offset >= (bfd_vma) -2)
10295 {
10296 /* This is a reloc for a deleted entry or somesuch.
10297 Turn it into an R_*_NONE reloc, at the same
10298 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10299 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10300 being ordered. */
10301 irela->r_offset = last_offset;
10302 irela->r_info = 0;
10303 irela->r_addend = 0;
10304 continue;
10305 }
10306
10307 irela->r_offset += o->output_offset;
10308
10309 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10310 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10311 irela->r_offset += o->output_section->vma;
10312
10313 last_offset = irela->r_offset;
10314
10315 r_symndx = irela->r_info >> r_sym_shift;
10316 if (r_symndx == STN_UNDEF)
10317 continue;
10318
10319 if (r_symndx >= locsymcount
10320 || (elf_bad_symtab (input_bfd)
8b127cbc 10321 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10322 {
10323 struct elf_link_hash_entry *rh;
10324 unsigned long indx;
10325
10326 /* This is a reloc against a global symbol. We
10327 have not yet output all the local symbols, so
10328 we do not know the symbol index of any global
10329 symbol. We set the rel_hash entry for this
10330 reloc to point to the global hash table entry
10331 for this symbol. The symbol index is then
ee75fd95 10332 set at the end of bfd_elf_final_link. */
c152c796
AM
10333 indx = r_symndx - extsymoff;
10334 rh = elf_sym_hashes (input_bfd)[indx];
10335 while (rh->root.type == bfd_link_hash_indirect
10336 || rh->root.type == bfd_link_hash_warning)
10337 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10338
10339 /* Setting the index to -2 tells
10340 elf_link_output_extsym that this symbol is
10341 used by a reloc. */
10342 BFD_ASSERT (rh->indx < 0);
10343 rh->indx = -2;
10344
10345 *rel_hash = rh;
10346
10347 continue;
10348 }
10349
10350 /* This is a reloc against a local symbol. */
10351
10352 *rel_hash = NULL;
10353 sym = isymbuf[r_symndx];
8b127cbc 10354 sec = flinfo->sections[r_symndx];
c152c796
AM
10355 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10356 {
10357 /* I suppose the backend ought to fill in the
10358 section of any STT_SECTION symbol against a
6a8d1586 10359 processor specific section. */
cf35638d 10360 r_symndx = STN_UNDEF;
6a8d1586
AM
10361 if (bfd_is_abs_section (sec))
10362 ;
c152c796
AM
10363 else if (sec == NULL || sec->owner == NULL)
10364 {
10365 bfd_set_error (bfd_error_bad_value);
10366 return FALSE;
10367 }
10368 else
10369 {
6a8d1586
AM
10370 asection *osec = sec->output_section;
10371
10372 /* If we have discarded a section, the output
10373 section will be the absolute section. In
ab96bf03
AM
10374 case of discarded SEC_MERGE sections, use
10375 the kept section. relocate_section should
10376 have already handled discarded linkonce
10377 sections. */
6a8d1586
AM
10378 if (bfd_is_abs_section (osec)
10379 && sec->kept_section != NULL
10380 && sec->kept_section->output_section != NULL)
10381 {
10382 osec = sec->kept_section->output_section;
10383 irela->r_addend -= osec->vma;
10384 }
10385
10386 if (!bfd_is_abs_section (osec))
10387 {
10388 r_symndx = osec->target_index;
cf35638d 10389 if (r_symndx == STN_UNDEF)
74541ad4 10390 {
051d833a
AM
10391 irela->r_addend += osec->vma;
10392 osec = _bfd_nearby_section (output_bfd, osec,
10393 osec->vma);
10394 irela->r_addend -= osec->vma;
10395 r_symndx = osec->target_index;
74541ad4 10396 }
6a8d1586 10397 }
c152c796
AM
10398 }
10399
10400 /* Adjust the addend according to where the
10401 section winds up in the output section. */
10402 if (rela_normal)
10403 irela->r_addend += sec->output_offset;
10404 }
10405 else
10406 {
8b127cbc 10407 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10408 {
10409 unsigned long shlink;
10410 const char *name;
10411 asection *osec;
6e0b88f1 10412 long indx;
c152c796 10413
8b127cbc 10414 if (flinfo->info->strip == strip_all)
c152c796
AM
10415 {
10416 /* You can't do ld -r -s. */
10417 bfd_set_error (bfd_error_invalid_operation);
10418 return FALSE;
10419 }
10420
10421 /* This symbol was skipped earlier, but
10422 since it is needed by a reloc, we
10423 must output it now. */
10424 shlink = symtab_hdr->sh_link;
10425 name = (bfd_elf_string_from_elf_section
10426 (input_bfd, shlink, sym.st_name));
10427 if (name == NULL)
10428 return FALSE;
10429
10430 osec = sec->output_section;
10431 sym.st_shndx =
10432 _bfd_elf_section_from_bfd_section (output_bfd,
10433 osec);
10434 if (sym.st_shndx == SHN_BAD)
10435 return FALSE;
10436
10437 sym.st_value += sec->output_offset;
0e1862bb 10438 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10439 {
10440 sym.st_value += osec->vma;
10441 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10442 {
10443 /* STT_TLS symbols are relative to PT_TLS
10444 segment base. */
8b127cbc 10445 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10446 ->tls_sec != NULL);
8b127cbc 10447 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10448 ->tls_sec->vma);
10449 }
10450 }
10451
6e0b88f1 10452 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10453 ret = elf_link_output_symstrtab (flinfo, name,
10454 &sym, sec,
10455 NULL);
6e0b88f1 10456 if (ret == 0)
c152c796 10457 return FALSE;
6e0b88f1 10458 else if (ret == 1)
8b127cbc 10459 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10460 else
10461 abort ();
c152c796
AM
10462 }
10463
8b127cbc 10464 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10465 }
10466
10467 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10468 | (irela->r_info & r_type_mask));
10469 }
10470
10471 /* Swap out the relocs. */
d4730f92
BS
10472 input_rel_hdr = esdi->rel.hdr;
10473 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10474 {
d4730f92
BS
10475 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10476 input_rel_hdr,
10477 internal_relocs,
10478 rel_hash_list))
10479 return FALSE;
c152c796
AM
10480 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10481 * bed->s->int_rels_per_ext_rel);
eac338cf 10482 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10483 }
10484
10485 input_rela_hdr = esdi->rela.hdr;
10486 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10487 {
eac338cf 10488 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10489 input_rela_hdr,
eac338cf 10490 internal_relocs,
d4730f92 10491 rela_hash_list))
c152c796
AM
10492 return FALSE;
10493 }
10494 }
10495 }
10496
10497 /* Write out the modified section contents. */
10498 if (bed->elf_backend_write_section
8b127cbc 10499 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10500 contents))
c152c796
AM
10501 {
10502 /* Section written out. */
10503 }
10504 else switch (o->sec_info_type)
10505 {
dbaa2011 10506 case SEC_INFO_TYPE_STABS:
c152c796
AM
10507 if (! (_bfd_write_section_stabs
10508 (output_bfd,
8b127cbc 10509 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10510 o, &elf_section_data (o)->sec_info, contents)))
10511 return FALSE;
10512 break;
dbaa2011 10513 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10514 if (! _bfd_write_merged_section (output_bfd, o,
10515 elf_section_data (o)->sec_info))
10516 return FALSE;
10517 break;
dbaa2011 10518 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10519 {
8b127cbc 10520 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10521 o, contents))
10522 return FALSE;
10523 }
10524 break;
2f0c68f2
CM
10525 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10526 {
10527 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10528 flinfo->info,
10529 o, contents))
10530 return FALSE;
10531 }
10532 break;
c152c796
AM
10533 default:
10534 {
5dabe785 10535 /* FIXME: octets_per_byte. */
310fd250
L
10536 if (! (o->flags & SEC_EXCLUDE))
10537 {
10538 file_ptr offset = (file_ptr) o->output_offset;
10539 bfd_size_type todo = o->size;
10540 if ((o->flags & SEC_ELF_REVERSE_COPY))
10541 {
10542 /* Reverse-copy input section to output. */
10543 do
10544 {
10545 todo -= address_size;
10546 if (! bfd_set_section_contents (output_bfd,
10547 o->output_section,
10548 contents + todo,
10549 offset,
10550 address_size))
10551 return FALSE;
10552 if (todo == 0)
10553 break;
10554 offset += address_size;
10555 }
10556 while (1);
10557 }
10558 else if (! bfd_set_section_contents (output_bfd,
10559 o->output_section,
10560 contents,
10561 offset, todo))
10562 return FALSE;
10563 }
c152c796
AM
10564 }
10565 break;
10566 }
10567 }
10568
10569 return TRUE;
10570}
10571
10572/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10573 requested by the linker, and does not come from any input file. This
c152c796
AM
10574 is used to build constructor and destructor tables when linking
10575 with -Ur. */
10576
10577static bfd_boolean
10578elf_reloc_link_order (bfd *output_bfd,
10579 struct bfd_link_info *info,
10580 asection *output_section,
10581 struct bfd_link_order *link_order)
10582{
10583 reloc_howto_type *howto;
10584 long indx;
10585 bfd_vma offset;
10586 bfd_vma addend;
d4730f92 10587 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10588 struct elf_link_hash_entry **rel_hash_ptr;
10589 Elf_Internal_Shdr *rel_hdr;
10590 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10591 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10592 bfd_byte *erel;
10593 unsigned int i;
d4730f92 10594 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10595
10596 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10597 if (howto == NULL)
10598 {
10599 bfd_set_error (bfd_error_bad_value);
10600 return FALSE;
10601 }
10602
10603 addend = link_order->u.reloc.p->addend;
10604
d4730f92
BS
10605 if (esdo->rel.hdr)
10606 reldata = &esdo->rel;
10607 else if (esdo->rela.hdr)
10608 reldata = &esdo->rela;
10609 else
10610 {
10611 reldata = NULL;
10612 BFD_ASSERT (0);
10613 }
10614
c152c796 10615 /* Figure out the symbol index. */
d4730f92 10616 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10617 if (link_order->type == bfd_section_reloc_link_order)
10618 {
10619 indx = link_order->u.reloc.p->u.section->target_index;
10620 BFD_ASSERT (indx != 0);
10621 *rel_hash_ptr = NULL;
10622 }
10623 else
10624 {
10625 struct elf_link_hash_entry *h;
10626
10627 /* Treat a reloc against a defined symbol as though it were
10628 actually against the section. */
10629 h = ((struct elf_link_hash_entry *)
10630 bfd_wrapped_link_hash_lookup (output_bfd, info,
10631 link_order->u.reloc.p->u.name,
10632 FALSE, FALSE, TRUE));
10633 if (h != NULL
10634 && (h->root.type == bfd_link_hash_defined
10635 || h->root.type == bfd_link_hash_defweak))
10636 {
10637 asection *section;
10638
10639 section = h->root.u.def.section;
10640 indx = section->output_section->target_index;
10641 *rel_hash_ptr = NULL;
10642 /* It seems that we ought to add the symbol value to the
10643 addend here, but in practice it has already been added
10644 because it was passed to constructor_callback. */
10645 addend += section->output_section->vma + section->output_offset;
10646 }
10647 else if (h != NULL)
10648 {
10649 /* Setting the index to -2 tells elf_link_output_extsym that
10650 this symbol is used by a reloc. */
10651 h->indx = -2;
10652 *rel_hash_ptr = h;
10653 indx = 0;
10654 }
10655 else
10656 {
10657 if (! ((*info->callbacks->unattached_reloc)
10658 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10659 return FALSE;
10660 indx = 0;
10661 }
10662 }
10663
10664 /* If this is an inplace reloc, we must write the addend into the
10665 object file. */
10666 if (howto->partial_inplace && addend != 0)
10667 {
10668 bfd_size_type size;
10669 bfd_reloc_status_type rstat;
10670 bfd_byte *buf;
10671 bfd_boolean ok;
10672 const char *sym_name;
10673
a50b1753
NC
10674 size = (bfd_size_type) bfd_get_reloc_size (howto);
10675 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10676 if (buf == NULL && size != 0)
c152c796
AM
10677 return FALSE;
10678 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10679 switch (rstat)
10680 {
10681 case bfd_reloc_ok:
10682 break;
10683
10684 default:
10685 case bfd_reloc_outofrange:
10686 abort ();
10687
10688 case bfd_reloc_overflow:
10689 if (link_order->type == bfd_section_reloc_link_order)
10690 sym_name = bfd_section_name (output_bfd,
10691 link_order->u.reloc.p->u.section);
10692 else
10693 sym_name = link_order->u.reloc.p->u.name;
10694 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10695 (info, NULL, sym_name, howto->name, addend, NULL,
10696 NULL, (bfd_vma) 0)))
c152c796
AM
10697 {
10698 free (buf);
10699 return FALSE;
10700 }
10701 break;
10702 }
10703 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10704 link_order->offset, size);
10705 free (buf);
10706 if (! ok)
10707 return FALSE;
10708 }
10709
10710 /* The address of a reloc is relative to the section in a
10711 relocatable file, and is a virtual address in an executable
10712 file. */
10713 offset = link_order->offset;
0e1862bb 10714 if (! bfd_link_relocatable (info))
c152c796
AM
10715 offset += output_section->vma;
10716
10717 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10718 {
10719 irel[i].r_offset = offset;
10720 irel[i].r_info = 0;
10721 irel[i].r_addend = 0;
10722 }
10723 if (bed->s->arch_size == 32)
10724 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10725 else
10726 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10727
d4730f92 10728 rel_hdr = reldata->hdr;
c152c796
AM
10729 erel = rel_hdr->contents;
10730 if (rel_hdr->sh_type == SHT_REL)
10731 {
d4730f92 10732 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10733 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10734 }
10735 else
10736 {
10737 irel[0].r_addend = addend;
d4730f92 10738 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10739 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10740 }
10741
d4730f92 10742 ++reldata->count;
c152c796
AM
10743
10744 return TRUE;
10745}
10746
0b52efa6
PB
10747
10748/* Get the output vma of the section pointed to by the sh_link field. */
10749
10750static bfd_vma
10751elf_get_linked_section_vma (struct bfd_link_order *p)
10752{
10753 Elf_Internal_Shdr **elf_shdrp;
10754 asection *s;
10755 int elfsec;
10756
10757 s = p->u.indirect.section;
10758 elf_shdrp = elf_elfsections (s->owner);
10759 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10760 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10761 /* PR 290:
10762 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10763 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10764 sh_info fields. Hence we could get the situation
10765 where elfsec is 0. */
10766 if (elfsec == 0)
10767 {
10768 const struct elf_backend_data *bed
10769 = get_elf_backend_data (s->owner);
10770 if (bed->link_order_error_handler)
d003868e
AM
10771 bed->link_order_error_handler
10772 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10773 return 0;
10774 }
10775 else
10776 {
10777 s = elf_shdrp[elfsec]->bfd_section;
10778 return s->output_section->vma + s->output_offset;
10779 }
0b52efa6
PB
10780}
10781
10782
10783/* Compare two sections based on the locations of the sections they are
10784 linked to. Used by elf_fixup_link_order. */
10785
10786static int
10787compare_link_order (const void * a, const void * b)
10788{
10789 bfd_vma apos;
10790 bfd_vma bpos;
10791
10792 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10793 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10794 if (apos < bpos)
10795 return -1;
10796 return apos > bpos;
10797}
10798
10799
10800/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10801 order as their linked sections. Returns false if this could not be done
10802 because an output section includes both ordered and unordered
10803 sections. Ideally we'd do this in the linker proper. */
10804
10805static bfd_boolean
10806elf_fixup_link_order (bfd *abfd, asection *o)
10807{
10808 int seen_linkorder;
10809 int seen_other;
10810 int n;
10811 struct bfd_link_order *p;
10812 bfd *sub;
10813 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10814 unsigned elfsec;
0b52efa6 10815 struct bfd_link_order **sections;
d33cdfe3 10816 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10817 bfd_vma offset;
3b36f7e6 10818
d33cdfe3
L
10819 other_sec = NULL;
10820 linkorder_sec = NULL;
0b52efa6
PB
10821 seen_other = 0;
10822 seen_linkorder = 0;
8423293d 10823 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10824 {
d33cdfe3 10825 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10826 {
10827 s = p->u.indirect.section;
d33cdfe3
L
10828 sub = s->owner;
10829 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10830 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10831 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10832 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10833 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10834 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10835 {
10836 seen_linkorder++;
10837 linkorder_sec = s;
10838 }
0b52efa6 10839 else
d33cdfe3
L
10840 {
10841 seen_other++;
10842 other_sec = s;
10843 }
0b52efa6
PB
10844 }
10845 else
10846 seen_other++;
d33cdfe3
L
10847
10848 if (seen_other && seen_linkorder)
10849 {
10850 if (other_sec && linkorder_sec)
10851 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10852 o, linkorder_sec,
10853 linkorder_sec->owner, other_sec,
10854 other_sec->owner);
10855 else
10856 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10857 o);
10858 bfd_set_error (bfd_error_bad_value);
10859 return FALSE;
10860 }
0b52efa6
PB
10861 }
10862
10863 if (!seen_linkorder)
10864 return TRUE;
10865
0b52efa6 10866 sections = (struct bfd_link_order **)
14b1c01e
AM
10867 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10868 if (sections == NULL)
10869 return FALSE;
0b52efa6 10870 seen_linkorder = 0;
3b36f7e6 10871
8423293d 10872 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10873 {
10874 sections[seen_linkorder++] = p;
10875 }
10876 /* Sort the input sections in the order of their linked section. */
10877 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10878 compare_link_order);
10879
10880 /* Change the offsets of the sections. */
10881 offset = 0;
10882 for (n = 0; n < seen_linkorder; n++)
10883 {
10884 s = sections[n]->u.indirect.section;
461686a3 10885 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10886 s->output_offset = offset;
10887 sections[n]->offset = offset;
5dabe785 10888 /* FIXME: octets_per_byte. */
0b52efa6
PB
10889 offset += sections[n]->size;
10890 }
10891
4dd07732 10892 free (sections);
0b52efa6
PB
10893 return TRUE;
10894}
10895
9f7c3e5e
AM
10896static void
10897elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10898{
10899 asection *o;
10900
10901 if (flinfo->symstrtab != NULL)
ef10c3ac 10902 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
10903 if (flinfo->contents != NULL)
10904 free (flinfo->contents);
10905 if (flinfo->external_relocs != NULL)
10906 free (flinfo->external_relocs);
10907 if (flinfo->internal_relocs != NULL)
10908 free (flinfo->internal_relocs);
10909 if (flinfo->external_syms != NULL)
10910 free (flinfo->external_syms);
10911 if (flinfo->locsym_shndx != NULL)
10912 free (flinfo->locsym_shndx);
10913 if (flinfo->internal_syms != NULL)
10914 free (flinfo->internal_syms);
10915 if (flinfo->indices != NULL)
10916 free (flinfo->indices);
10917 if (flinfo->sections != NULL)
10918 free (flinfo->sections);
9f7c3e5e
AM
10919 if (flinfo->symshndxbuf != NULL)
10920 free (flinfo->symshndxbuf);
10921 for (o = obfd->sections; o != NULL; o = o->next)
10922 {
10923 struct bfd_elf_section_data *esdo = elf_section_data (o);
10924 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10925 free (esdo->rel.hashes);
10926 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10927 free (esdo->rela.hashes);
10928 }
10929}
0b52efa6 10930
c152c796
AM
10931/* Do the final step of an ELF link. */
10932
10933bfd_boolean
10934bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10935{
10936 bfd_boolean dynamic;
10937 bfd_boolean emit_relocs;
10938 bfd *dynobj;
8b127cbc 10939 struct elf_final_link_info flinfo;
91d6fa6a
NC
10940 asection *o;
10941 struct bfd_link_order *p;
10942 bfd *sub;
c152c796
AM
10943 bfd_size_type max_contents_size;
10944 bfd_size_type max_external_reloc_size;
10945 bfd_size_type max_internal_reloc_count;
10946 bfd_size_type max_sym_count;
10947 bfd_size_type max_sym_shndx_count;
c152c796
AM
10948 Elf_Internal_Sym elfsym;
10949 unsigned int i;
10950 Elf_Internal_Shdr *symtab_hdr;
10951 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
10952 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10953 struct elf_outext_info eoinfo;
10954 bfd_boolean merged;
10955 size_t relativecount = 0;
10956 asection *reldyn = 0;
10957 bfd_size_type amt;
104d59d1
JM
10958 asection *attr_section = NULL;
10959 bfd_vma attr_size = 0;
10960 const char *std_attrs_section;
c152c796
AM
10961
10962 if (! is_elf_hash_table (info->hash))
10963 return FALSE;
10964
0e1862bb 10965 if (bfd_link_pic (info))
c152c796
AM
10966 abfd->flags |= DYNAMIC;
10967
10968 dynamic = elf_hash_table (info)->dynamic_sections_created;
10969 dynobj = elf_hash_table (info)->dynobj;
10970
0e1862bb 10971 emit_relocs = (bfd_link_relocatable (info)
a4676736 10972 || info->emitrelocations);
c152c796 10973
8b127cbc
AM
10974 flinfo.info = info;
10975 flinfo.output_bfd = abfd;
ef10c3ac 10976 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 10977 if (flinfo.symstrtab == NULL)
c152c796
AM
10978 return FALSE;
10979
10980 if (! dynamic)
10981 {
8b127cbc
AM
10982 flinfo.hash_sec = NULL;
10983 flinfo.symver_sec = NULL;
c152c796
AM
10984 }
10985 else
10986 {
3d4d4302 10987 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 10988 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 10989 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
10990 /* Note that it is OK if symver_sec is NULL. */
10991 }
10992
8b127cbc
AM
10993 flinfo.contents = NULL;
10994 flinfo.external_relocs = NULL;
10995 flinfo.internal_relocs = NULL;
10996 flinfo.external_syms = NULL;
10997 flinfo.locsym_shndx = NULL;
10998 flinfo.internal_syms = NULL;
10999 flinfo.indices = NULL;
11000 flinfo.sections = NULL;
8b127cbc 11001 flinfo.symshndxbuf = NULL;
ffbc01cc 11002 flinfo.filesym_count = 0;
c152c796 11003
104d59d1
JM
11004 /* The object attributes have been merged. Remove the input
11005 sections from the link, and set the contents of the output
11006 secton. */
11007 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11008 for (o = abfd->sections; o != NULL; o = o->next)
11009 {
11010 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11011 || strcmp (o->name, ".gnu.attributes") == 0)
11012 {
11013 for (p = o->map_head.link_order; p != NULL; p = p->next)
11014 {
11015 asection *input_section;
11016
11017 if (p->type != bfd_indirect_link_order)
11018 continue;
11019 input_section = p->u.indirect.section;
11020 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11021 elf_link_input_bfd ignores this section. */
11022 input_section->flags &= ~SEC_HAS_CONTENTS;
11023 }
a0c8462f 11024
104d59d1
JM
11025 attr_size = bfd_elf_obj_attr_size (abfd);
11026 if (attr_size)
11027 {
11028 bfd_set_section_size (abfd, o, attr_size);
11029 attr_section = o;
11030 /* Skip this section later on. */
11031 o->map_head.link_order = NULL;
11032 }
11033 else
11034 o->flags |= SEC_EXCLUDE;
11035 }
11036 }
11037
c152c796
AM
11038 /* Count up the number of relocations we will output for each output
11039 section, so that we know the sizes of the reloc sections. We
11040 also figure out some maximum sizes. */
11041 max_contents_size = 0;
11042 max_external_reloc_size = 0;
11043 max_internal_reloc_count = 0;
11044 max_sym_count = 0;
11045 max_sym_shndx_count = 0;
11046 merged = FALSE;
11047 for (o = abfd->sections; o != NULL; o = o->next)
11048 {
11049 struct bfd_elf_section_data *esdo = elf_section_data (o);
11050 o->reloc_count = 0;
11051
8423293d 11052 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11053 {
11054 unsigned int reloc_count = 0;
491d01d3 11055 unsigned int additional_reloc_count = 0;
c152c796 11056 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11057
11058 if (p->type == bfd_section_reloc_link_order
11059 || p->type == bfd_symbol_reloc_link_order)
11060 reloc_count = 1;
11061 else if (p->type == bfd_indirect_link_order)
11062 {
11063 asection *sec;
11064
11065 sec = p->u.indirect.section;
11066 esdi = elf_section_data (sec);
11067
11068 /* Mark all sections which are to be included in the
11069 link. This will normally be every section. We need
11070 to do this so that we can identify any sections which
11071 the linker has decided to not include. */
11072 sec->linker_mark = TRUE;
11073
11074 if (sec->flags & SEC_MERGE)
11075 merged = TRUE;
11076
aed64b35
L
11077 if (esdo->this_hdr.sh_type == SHT_REL
11078 || esdo->this_hdr.sh_type == SHT_RELA)
11079 /* Some backends use reloc_count in relocation sections
11080 to count particular types of relocs. Of course,
11081 reloc sections themselves can't have relocations. */
11082 reloc_count = 0;
0e1862bb 11083 else if (emit_relocs)
491d01d3
YU
11084 {
11085 reloc_count = sec->reloc_count;
11086 if (bed->elf_backend_count_additional_relocs)
11087 {
11088 int c;
11089 c = (*bed->elf_backend_count_additional_relocs) (sec);
11090 additional_reloc_count += c;
11091 }
11092 }
c152c796 11093 else if (bed->elf_backend_count_relocs)
58217f29 11094 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11095
eea6121a
AM
11096 if (sec->rawsize > max_contents_size)
11097 max_contents_size = sec->rawsize;
11098 if (sec->size > max_contents_size)
11099 max_contents_size = sec->size;
c152c796
AM
11100
11101 /* We are interested in just local symbols, not all
11102 symbols. */
11103 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11104 && (sec->owner->flags & DYNAMIC) == 0)
11105 {
11106 size_t sym_count;
11107
11108 if (elf_bad_symtab (sec->owner))
11109 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11110 / bed->s->sizeof_sym);
11111 else
11112 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11113
11114 if (sym_count > max_sym_count)
11115 max_sym_count = sym_count;
11116
11117 if (sym_count > max_sym_shndx_count
6a40cf0c 11118 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11119 max_sym_shndx_count = sym_count;
11120
11121 if ((sec->flags & SEC_RELOC) != 0)
11122 {
d4730f92 11123 size_t ext_size = 0;
c152c796 11124
d4730f92
BS
11125 if (esdi->rel.hdr != NULL)
11126 ext_size = esdi->rel.hdr->sh_size;
11127 if (esdi->rela.hdr != NULL)
11128 ext_size += esdi->rela.hdr->sh_size;
7326c758 11129
c152c796
AM
11130 if (ext_size > max_external_reloc_size)
11131 max_external_reloc_size = ext_size;
11132 if (sec->reloc_count > max_internal_reloc_count)
11133 max_internal_reloc_count = sec->reloc_count;
11134 }
11135 }
11136 }
11137
11138 if (reloc_count == 0)
11139 continue;
11140
491d01d3 11141 reloc_count += additional_reloc_count;
c152c796
AM
11142 o->reloc_count += reloc_count;
11143
0e1862bb 11144 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11145 {
d4730f92 11146 if (esdi->rel.hdr)
491d01d3
YU
11147 {
11148 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11149 esdo->rel.count += additional_reloc_count;
11150 }
d4730f92 11151 if (esdi->rela.hdr)
491d01d3
YU
11152 {
11153 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11154 esdo->rela.count += additional_reloc_count;
11155 }
d4730f92
BS
11156 }
11157 else
11158 {
11159 if (o->use_rela_p)
11160 esdo->rela.count += reloc_count;
2c2b4ed4 11161 else
d4730f92 11162 esdo->rel.count += reloc_count;
c152c796 11163 }
c152c796
AM
11164 }
11165
11166 if (o->reloc_count > 0)
11167 o->flags |= SEC_RELOC;
11168 else
11169 {
11170 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11171 set it (this is probably a bug) and if it is set
11172 assign_section_numbers will create a reloc section. */
11173 o->flags &=~ SEC_RELOC;
11174 }
11175
11176 /* If the SEC_ALLOC flag is not set, force the section VMA to
11177 zero. This is done in elf_fake_sections as well, but forcing
11178 the VMA to 0 here will ensure that relocs against these
11179 sections are handled correctly. */
11180 if ((o->flags & SEC_ALLOC) == 0
11181 && ! o->user_set_vma)
11182 o->vma = 0;
11183 }
11184
0e1862bb 11185 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11186 elf_link_hash_traverse (elf_hash_table (info),
11187 _bfd_elf_link_sec_merge_syms, abfd);
11188
11189 /* Figure out the file positions for everything but the symbol table
11190 and the relocs. We set symcount to force assign_section_numbers
11191 to create a symbol table. */
8539e4e8 11192 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11193 BFD_ASSERT (! abfd->output_has_begun);
11194 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11195 goto error_return;
11196
ee75fd95 11197 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11198 for (o = abfd->sections; o != NULL; o = o->next)
11199 {
d4730f92 11200 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11201 if ((o->flags & SEC_RELOC) != 0)
11202 {
d4730f92
BS
11203 if (esdo->rel.hdr
11204 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11205 goto error_return;
11206
d4730f92
BS
11207 if (esdo->rela.hdr
11208 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11209 goto error_return;
11210 }
11211
11212 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11213 to count upwards while actually outputting the relocations. */
d4730f92
BS
11214 esdo->rel.count = 0;
11215 esdo->rela.count = 0;
0ce398f1
L
11216
11217 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11218 {
11219 /* Cache the section contents so that they can be compressed
11220 later. Use bfd_malloc since it will be freed by
11221 bfd_compress_section_contents. */
11222 unsigned char *contents = esdo->this_hdr.contents;
11223 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11224 abort ();
11225 contents
11226 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11227 if (contents == NULL)
11228 goto error_return;
11229 esdo->this_hdr.contents = contents;
11230 }
c152c796
AM
11231 }
11232
c152c796 11233 /* We have now assigned file positions for all the sections except
a485e98e
AM
11234 .symtab, .strtab, and non-loaded reloc sections. We start the
11235 .symtab section at the current file position, and write directly
11236 to it. We build the .strtab section in memory. */
c152c796
AM
11237 bfd_get_symcount (abfd) = 0;
11238 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11239 /* sh_name is set in prep_headers. */
11240 symtab_hdr->sh_type = SHT_SYMTAB;
11241 /* sh_flags, sh_addr and sh_size all start off zero. */
11242 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11243 /* sh_link is set in assign_section_numbers. */
11244 /* sh_info is set below. */
11245 /* sh_offset is set just below. */
72de5009 11246 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11247
ef10c3ac
L
11248 if (max_sym_count < 20)
11249 max_sym_count = 20;
11250 elf_hash_table (info)->strtabsize = max_sym_count;
11251 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11252 elf_hash_table (info)->strtab
11253 = (struct elf_sym_strtab *) bfd_malloc (amt);
11254 if (elf_hash_table (info)->strtab == NULL)
c152c796 11255 goto error_return;
ef10c3ac
L
11256 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11257 flinfo.symshndxbuf
11258 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11259 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11260
8539e4e8 11261 if (info->strip != strip_all || emit_relocs)
c152c796 11262 {
8539e4e8
AM
11263 file_ptr off = elf_next_file_pos (abfd);
11264
11265 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11266
11267 /* Note that at this point elf_next_file_pos (abfd) is
11268 incorrect. We do not yet know the size of the .symtab section.
11269 We correct next_file_pos below, after we do know the size. */
11270
11271 /* Start writing out the symbol table. The first symbol is always a
11272 dummy symbol. */
c152c796
AM
11273 elfsym.st_value = 0;
11274 elfsym.st_size = 0;
11275 elfsym.st_info = 0;
11276 elfsym.st_other = 0;
11277 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11278 elfsym.st_target_internal = 0;
ef10c3ac
L
11279 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11280 bfd_und_section_ptr, NULL) != 1)
c152c796 11281 goto error_return;
c152c796 11282
8539e4e8
AM
11283 /* Output a symbol for each section. We output these even if we are
11284 discarding local symbols, since they are used for relocs. These
11285 symbols have no names. We store the index of each one in the
11286 index field of the section, so that we can find it again when
11287 outputting relocs. */
11288
c152c796
AM
11289 elfsym.st_size = 0;
11290 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11291 elfsym.st_other = 0;
f0b5bb34 11292 elfsym.st_value = 0;
35fc36a8 11293 elfsym.st_target_internal = 0;
c152c796
AM
11294 for (i = 1; i < elf_numsections (abfd); i++)
11295 {
11296 o = bfd_section_from_elf_index (abfd, i);
11297 if (o != NULL)
f0b5bb34
AM
11298 {
11299 o->target_index = bfd_get_symcount (abfd);
11300 elfsym.st_shndx = i;
0e1862bb 11301 if (!bfd_link_relocatable (info))
f0b5bb34 11302 elfsym.st_value = o->vma;
ef10c3ac
L
11303 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11304 NULL) != 1)
f0b5bb34
AM
11305 goto error_return;
11306 }
c152c796
AM
11307 }
11308 }
11309
11310 /* Allocate some memory to hold information read in from the input
11311 files. */
11312 if (max_contents_size != 0)
11313 {
8b127cbc
AM
11314 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11315 if (flinfo.contents == NULL)
c152c796
AM
11316 goto error_return;
11317 }
11318
11319 if (max_external_reloc_size != 0)
11320 {
8b127cbc
AM
11321 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11322 if (flinfo.external_relocs == NULL)
c152c796
AM
11323 goto error_return;
11324 }
11325
11326 if (max_internal_reloc_count != 0)
11327 {
11328 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11329 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11330 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11331 if (flinfo.internal_relocs == NULL)
c152c796
AM
11332 goto error_return;
11333 }
11334
11335 if (max_sym_count != 0)
11336 {
11337 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11338 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11339 if (flinfo.external_syms == NULL)
c152c796
AM
11340 goto error_return;
11341
11342 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11343 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11344 if (flinfo.internal_syms == NULL)
c152c796
AM
11345 goto error_return;
11346
11347 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11348 flinfo.indices = (long int *) bfd_malloc (amt);
11349 if (flinfo.indices == NULL)
c152c796
AM
11350 goto error_return;
11351
11352 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11353 flinfo.sections = (asection **) bfd_malloc (amt);
11354 if (flinfo.sections == NULL)
c152c796
AM
11355 goto error_return;
11356 }
11357
11358 if (max_sym_shndx_count != 0)
11359 {
11360 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11361 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11362 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11363 goto error_return;
11364 }
11365
11366 if (elf_hash_table (info)->tls_sec)
11367 {
11368 bfd_vma base, end = 0;
11369 asection *sec;
11370
11371 for (sec = elf_hash_table (info)->tls_sec;
11372 sec && (sec->flags & SEC_THREAD_LOCAL);
11373 sec = sec->next)
11374 {
3a800eb9 11375 bfd_size_type size = sec->size;
c152c796 11376
3a800eb9
AM
11377 if (size == 0
11378 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11379 {
91d6fa6a
NC
11380 struct bfd_link_order *ord = sec->map_tail.link_order;
11381
11382 if (ord != NULL)
11383 size = ord->offset + ord->size;
c152c796
AM
11384 }
11385 end = sec->vma + size;
11386 }
11387 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11388 /* Only align end of TLS section if static TLS doesn't have special
11389 alignment requirements. */
11390 if (bed->static_tls_alignment == 1)
11391 end = align_power (end,
11392 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11393 elf_hash_table (info)->tls_size = end - base;
11394 }
11395
0b52efa6
PB
11396 /* Reorder SHF_LINK_ORDER sections. */
11397 for (o = abfd->sections; o != NULL; o = o->next)
11398 {
11399 if (!elf_fixup_link_order (abfd, o))
11400 return FALSE;
11401 }
11402
2f0c68f2
CM
11403 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11404 return FALSE;
11405
c152c796
AM
11406 /* Since ELF permits relocations to be against local symbols, we
11407 must have the local symbols available when we do the relocations.
11408 Since we would rather only read the local symbols once, and we
11409 would rather not keep them in memory, we handle all the
11410 relocations for a single input file at the same time.
11411
11412 Unfortunately, there is no way to know the total number of local
11413 symbols until we have seen all of them, and the local symbol
11414 indices precede the global symbol indices. This means that when
11415 we are generating relocatable output, and we see a reloc against
11416 a global symbol, we can not know the symbol index until we have
11417 finished examining all the local symbols to see which ones we are
11418 going to output. To deal with this, we keep the relocations in
11419 memory, and don't output them until the end of the link. This is
11420 an unfortunate waste of memory, but I don't see a good way around
11421 it. Fortunately, it only happens when performing a relocatable
11422 link, which is not the common case. FIXME: If keep_memory is set
11423 we could write the relocs out and then read them again; I don't
11424 know how bad the memory loss will be. */
11425
c72f2fb2 11426 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11427 sub->output_has_begun = FALSE;
11428 for (o = abfd->sections; o != NULL; o = o->next)
11429 {
8423293d 11430 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11431 {
11432 if (p->type == bfd_indirect_link_order
11433 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11434 == bfd_target_elf_flavour)
11435 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11436 {
11437 if (! sub->output_has_begun)
11438 {
8b127cbc 11439 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11440 goto error_return;
11441 sub->output_has_begun = TRUE;
11442 }
11443 }
11444 else if (p->type == bfd_section_reloc_link_order
11445 || p->type == bfd_symbol_reloc_link_order)
11446 {
11447 if (! elf_reloc_link_order (abfd, info, o, p))
11448 goto error_return;
11449 }
11450 else
11451 {
11452 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11453 {
11454 if (p->type == bfd_indirect_link_order
11455 && (bfd_get_flavour (sub)
11456 == bfd_target_elf_flavour)
11457 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11458 != bed->s->elfclass))
11459 {
11460 const char *iclass, *oclass;
11461
aebf9be7 11462 switch (bed->s->elfclass)
351f65ca 11463 {
aebf9be7
NC
11464 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11465 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11466 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11467 default: abort ();
351f65ca 11468 }
aebf9be7
NC
11469
11470 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11471 {
aebf9be7
NC
11472 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11473 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11474 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11475 default: abort ();
351f65ca
L
11476 }
11477
11478 bfd_set_error (bfd_error_wrong_format);
11479 (*_bfd_error_handler)
11480 (_("%B: file class %s incompatible with %s"),
11481 sub, iclass, oclass);
11482 }
11483
11484 goto error_return;
11485 }
c152c796
AM
11486 }
11487 }
11488 }
11489
c0f00686
L
11490 /* Free symbol buffer if needed. */
11491 if (!info->reduce_memory_overheads)
11492 {
c72f2fb2 11493 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11494 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11495 && elf_tdata (sub)->symbuf)
c0f00686
L
11496 {
11497 free (elf_tdata (sub)->symbuf);
11498 elf_tdata (sub)->symbuf = NULL;
11499 }
11500 }
11501
c152c796
AM
11502 /* Output any global symbols that got converted to local in a
11503 version script or due to symbol visibility. We do this in a
11504 separate step since ELF requires all local symbols to appear
11505 prior to any global symbols. FIXME: We should only do this if
11506 some global symbols were, in fact, converted to become local.
11507 FIXME: Will this work correctly with the Irix 5 linker? */
11508 eoinfo.failed = FALSE;
8b127cbc 11509 eoinfo.flinfo = &flinfo;
c152c796 11510 eoinfo.localsyms = TRUE;
34a79995 11511 eoinfo.file_sym_done = FALSE;
7686d77d 11512 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11513 if (eoinfo.failed)
11514 return FALSE;
11515
4e617b1e
PB
11516 /* If backend needs to output some local symbols not present in the hash
11517 table, do it now. */
8539e4e8
AM
11518 if (bed->elf_backend_output_arch_local_syms
11519 && (info->strip != strip_all || emit_relocs))
4e617b1e 11520 {
6e0b88f1 11521 typedef int (*out_sym_func)
4e617b1e
PB
11522 (void *, const char *, Elf_Internal_Sym *, asection *,
11523 struct elf_link_hash_entry *);
11524
11525 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11526 (abfd, info, &flinfo,
11527 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11528 return FALSE;
11529 }
11530
c152c796
AM
11531 /* That wrote out all the local symbols. Finish up the symbol table
11532 with the global symbols. Even if we want to strip everything we
11533 can, we still need to deal with those global symbols that got
11534 converted to local in a version script. */
11535
11536 /* The sh_info field records the index of the first non local symbol. */
11537 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11538
11539 if (dynamic
cae1fbbb
L
11540 && elf_hash_table (info)->dynsym != NULL
11541 && (elf_hash_table (info)->dynsym->output_section
11542 != bfd_abs_section_ptr))
c152c796
AM
11543 {
11544 Elf_Internal_Sym sym;
cae1fbbb 11545 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11546 long last_local = 0;
11547
11548 /* Write out the section symbols for the output sections. */
0e1862bb
L
11549 if (bfd_link_pic (info)
11550 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11551 {
11552 asection *s;
11553
11554 sym.st_size = 0;
11555 sym.st_name = 0;
11556 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11557 sym.st_other = 0;
35fc36a8 11558 sym.st_target_internal = 0;
c152c796
AM
11559
11560 for (s = abfd->sections; s != NULL; s = s->next)
11561 {
11562 int indx;
11563 bfd_byte *dest;
11564 long dynindx;
11565
c152c796 11566 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11567 if (dynindx <= 0)
11568 continue;
11569 indx = elf_section_data (s)->this_idx;
c152c796
AM
11570 BFD_ASSERT (indx > 0);
11571 sym.st_shndx = indx;
c0d5a53d
L
11572 if (! check_dynsym (abfd, &sym))
11573 return FALSE;
c152c796
AM
11574 sym.st_value = s->vma;
11575 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11576 if (last_local < dynindx)
11577 last_local = dynindx;
c152c796
AM
11578 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11579 }
c152c796
AM
11580 }
11581
11582 /* Write out the local dynsyms. */
11583 if (elf_hash_table (info)->dynlocal)
11584 {
11585 struct elf_link_local_dynamic_entry *e;
11586 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11587 {
11588 asection *s;
11589 bfd_byte *dest;
11590
935bd1e0 11591 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11592 Note that we saved a word of storage and overwrote
11593 the original st_name with the dynstr_index. */
11594 sym = e->isym;
935bd1e0 11595 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11596
cb33740c
AM
11597 s = bfd_section_from_elf_index (e->input_bfd,
11598 e->isym.st_shndx);
11599 if (s != NULL)
c152c796 11600 {
c152c796
AM
11601 sym.st_shndx =
11602 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11603 if (! check_dynsym (abfd, &sym))
11604 return FALSE;
c152c796
AM
11605 sym.st_value = (s->output_section->vma
11606 + s->output_offset
11607 + e->isym.st_value);
11608 }
11609
11610 if (last_local < e->dynindx)
11611 last_local = e->dynindx;
11612
11613 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11614 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11615 }
11616 }
11617
cae1fbbb 11618 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11619 last_local + 1;
11620 }
11621
11622 /* We get the global symbols from the hash table. */
11623 eoinfo.failed = FALSE;
11624 eoinfo.localsyms = FALSE;
8b127cbc 11625 eoinfo.flinfo = &flinfo;
7686d77d 11626 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11627 if (eoinfo.failed)
11628 return FALSE;
11629
11630 /* If backend needs to output some symbols not present in the hash
11631 table, do it now. */
8539e4e8
AM
11632 if (bed->elf_backend_output_arch_syms
11633 && (info->strip != strip_all || emit_relocs))
c152c796 11634 {
6e0b88f1 11635 typedef int (*out_sym_func)
c152c796
AM
11636 (void *, const char *, Elf_Internal_Sym *, asection *,
11637 struct elf_link_hash_entry *);
11638
11639 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11640 (abfd, info, &flinfo,
11641 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11642 return FALSE;
11643 }
11644
ef10c3ac
L
11645 /* Finalize the .strtab section. */
11646 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11647
11648 /* Swap out the .strtab section. */
11649 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11650 return FALSE;
11651
11652 /* Now we know the size of the symtab section. */
c152c796
AM
11653 if (bfd_get_symcount (abfd) > 0)
11654 {
ee3b52e9
L
11655 /* Finish up and write out the symbol string table (.strtab)
11656 section. */
11657 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11658 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11659
6a40cf0c
NC
11660 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11661 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11662 {
11663 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11664 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11665 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11666 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11667 symtab_shndx_hdr->sh_size = amt;
11668
11669 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11670 off, TRUE);
11671
11672 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11673 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11674 return FALSE;
11675 }
ee3b52e9
L
11676
11677 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11678 /* sh_name was set in prep_headers. */
11679 symstrtab_hdr->sh_type = SHT_STRTAB;
11680 symstrtab_hdr->sh_flags = 0;
11681 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11682 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11683 symstrtab_hdr->sh_entsize = 0;
11684 symstrtab_hdr->sh_link = 0;
11685 symstrtab_hdr->sh_info = 0;
11686 /* sh_offset is set just below. */
11687 symstrtab_hdr->sh_addralign = 1;
11688
11689 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11690 off, TRUE);
11691 elf_next_file_pos (abfd) = off;
11692
c152c796 11693 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11694 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11695 return FALSE;
11696 }
11697
11698 /* Adjust the relocs to have the correct symbol indices. */
11699 for (o = abfd->sections; o != NULL; o = o->next)
11700 {
d4730f92 11701 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11702 bfd_boolean sort;
c152c796
AM
11703 if ((o->flags & SEC_RELOC) == 0)
11704 continue;
11705
28dbcedc 11706 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11707 if (esdo->rel.hdr != NULL
11708 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11709 return FALSE;
11710 if (esdo->rela.hdr != NULL
11711 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11712 return FALSE;
c152c796
AM
11713
11714 /* Set the reloc_count field to 0 to prevent write_relocs from
11715 trying to swap the relocs out itself. */
11716 o->reloc_count = 0;
11717 }
11718
11719 if (dynamic && info->combreloc && dynobj != NULL)
11720 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11721
11722 /* If we are linking against a dynamic object, or generating a
11723 shared library, finish up the dynamic linking information. */
11724 if (dynamic)
11725 {
11726 bfd_byte *dyncon, *dynconend;
11727
11728 /* Fix up .dynamic entries. */
3d4d4302 11729 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11730 BFD_ASSERT (o != NULL);
11731
11732 dyncon = o->contents;
eea6121a 11733 dynconend = o->contents + o->size;
c152c796
AM
11734 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11735 {
11736 Elf_Internal_Dyn dyn;
11737 const char *name;
11738 unsigned int type;
11739
11740 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11741
11742 switch (dyn.d_tag)
11743 {
11744 default:
11745 continue;
11746 case DT_NULL:
11747 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11748 {
11749 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11750 {
11751 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11752 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11753 default: continue;
11754 }
11755 dyn.d_un.d_val = relativecount;
11756 relativecount = 0;
11757 break;
11758 }
11759 continue;
11760
11761 case DT_INIT:
11762 name = info->init_function;
11763 goto get_sym;
11764 case DT_FINI:
11765 name = info->fini_function;
11766 get_sym:
11767 {
11768 struct elf_link_hash_entry *h;
11769
11770 h = elf_link_hash_lookup (elf_hash_table (info), name,
11771 FALSE, FALSE, TRUE);
11772 if (h != NULL
11773 && (h->root.type == bfd_link_hash_defined
11774 || h->root.type == bfd_link_hash_defweak))
11775 {
bef26483 11776 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11777 o = h->root.u.def.section;
11778 if (o->output_section != NULL)
bef26483 11779 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11780 + o->output_offset);
11781 else
11782 {
11783 /* The symbol is imported from another shared
11784 library and does not apply to this one. */
bef26483 11785 dyn.d_un.d_ptr = 0;
c152c796
AM
11786 }
11787 break;
11788 }
11789 }
11790 continue;
11791
11792 case DT_PREINIT_ARRAYSZ:
11793 name = ".preinit_array";
11794 goto get_size;
11795 case DT_INIT_ARRAYSZ:
11796 name = ".init_array";
11797 goto get_size;
11798 case DT_FINI_ARRAYSZ:
11799 name = ".fini_array";
11800 get_size:
11801 o = bfd_get_section_by_name (abfd, name);
11802 if (o == NULL)
11803 {
11804 (*_bfd_error_handler)
d003868e 11805 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11806 goto error_return;
11807 }
eea6121a 11808 if (o->size == 0)
c152c796
AM
11809 (*_bfd_error_handler)
11810 (_("warning: %s section has zero size"), name);
eea6121a 11811 dyn.d_un.d_val = o->size;
c152c796
AM
11812 break;
11813
11814 case DT_PREINIT_ARRAY:
11815 name = ".preinit_array";
11816 goto get_vma;
11817 case DT_INIT_ARRAY:
11818 name = ".init_array";
11819 goto get_vma;
11820 case DT_FINI_ARRAY:
11821 name = ".fini_array";
11822 goto get_vma;
11823
11824 case DT_HASH:
11825 name = ".hash";
11826 goto get_vma;
fdc90cb4
JJ
11827 case DT_GNU_HASH:
11828 name = ".gnu.hash";
11829 goto get_vma;
c152c796
AM
11830 case DT_STRTAB:
11831 name = ".dynstr";
11832 goto get_vma;
11833 case DT_SYMTAB:
11834 name = ".dynsym";
11835 goto get_vma;
11836 case DT_VERDEF:
11837 name = ".gnu.version_d";
11838 goto get_vma;
11839 case DT_VERNEED:
11840 name = ".gnu.version_r";
11841 goto get_vma;
11842 case DT_VERSYM:
11843 name = ".gnu.version";
11844 get_vma:
11845 o = bfd_get_section_by_name (abfd, name);
11846 if (o == NULL)
11847 {
11848 (*_bfd_error_handler)
d003868e 11849 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11850 goto error_return;
11851 }
894891db
NC
11852 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11853 {
11854 (*_bfd_error_handler)
11855 (_("warning: section '%s' is being made into a note"), name);
11856 bfd_set_error (bfd_error_nonrepresentable_section);
11857 goto error_return;
11858 }
c152c796
AM
11859 dyn.d_un.d_ptr = o->vma;
11860 break;
11861
11862 case DT_REL:
11863 case DT_RELA:
11864 case DT_RELSZ:
11865 case DT_RELASZ:
11866 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11867 type = SHT_REL;
11868 else
11869 type = SHT_RELA;
11870 dyn.d_un.d_val = 0;
bef26483 11871 dyn.d_un.d_ptr = 0;
c152c796
AM
11872 for (i = 1; i < elf_numsections (abfd); i++)
11873 {
11874 Elf_Internal_Shdr *hdr;
11875
11876 hdr = elf_elfsections (abfd)[i];
11877 if (hdr->sh_type == type
11878 && (hdr->sh_flags & SHF_ALLOC) != 0)
11879 {
11880 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11881 dyn.d_un.d_val += hdr->sh_size;
11882 else
11883 {
bef26483
AM
11884 if (dyn.d_un.d_ptr == 0
11885 || hdr->sh_addr < dyn.d_un.d_ptr)
11886 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11887 }
11888 }
11889 }
11890 break;
11891 }
11892 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11893 }
11894 }
11895
11896 /* If we have created any dynamic sections, then output them. */
11897 if (dynobj != NULL)
11898 {
11899 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11900 goto error_return;
11901
943284cc 11902 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 11903 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 11904 || info->error_textrel)
3d4d4302 11905 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11906 {
11907 bfd_byte *dyncon, *dynconend;
11908
943284cc
DJ
11909 dyncon = o->contents;
11910 dynconend = o->contents + o->size;
11911 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11912 {
11913 Elf_Internal_Dyn dyn;
11914
11915 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11916
11917 if (dyn.d_tag == DT_TEXTREL)
11918 {
c192a133
AM
11919 if (info->error_textrel)
11920 info->callbacks->einfo
11921 (_("%P%X: read-only segment has dynamic relocations.\n"));
11922 else
11923 info->callbacks->einfo
11924 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11925 break;
11926 }
11927 }
11928 }
11929
c152c796
AM
11930 for (o = dynobj->sections; o != NULL; o = o->next)
11931 {
11932 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11933 || o->size == 0
c152c796
AM
11934 || o->output_section == bfd_abs_section_ptr)
11935 continue;
11936 if ((o->flags & SEC_LINKER_CREATED) == 0)
11937 {
11938 /* At this point, we are only interested in sections
11939 created by _bfd_elf_link_create_dynamic_sections. */
11940 continue;
11941 }
3722b82f
AM
11942 if (elf_hash_table (info)->stab_info.stabstr == o)
11943 continue;
eea6121a
AM
11944 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11945 continue;
3d4d4302 11946 if (strcmp (o->name, ".dynstr") != 0)
c152c796 11947 {
5dabe785 11948 /* FIXME: octets_per_byte. */
c152c796
AM
11949 if (! bfd_set_section_contents (abfd, o->output_section,
11950 o->contents,
11951 (file_ptr) o->output_offset,
eea6121a 11952 o->size))
c152c796
AM
11953 goto error_return;
11954 }
11955 else
11956 {
11957 /* The contents of the .dynstr section are actually in a
11958 stringtab. */
8539e4e8
AM
11959 file_ptr off;
11960
c152c796
AM
11961 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11962 if (bfd_seek (abfd, off, SEEK_SET) != 0
11963 || ! _bfd_elf_strtab_emit (abfd,
11964 elf_hash_table (info)->dynstr))
11965 goto error_return;
11966 }
11967 }
11968 }
11969
0e1862bb 11970 if (bfd_link_relocatable (info))
c152c796
AM
11971 {
11972 bfd_boolean failed = FALSE;
11973
11974 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11975 if (failed)
11976 goto error_return;
11977 }
11978
11979 /* If we have optimized stabs strings, output them. */
3722b82f 11980 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11981 {
11982 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11983 goto error_return;
11984 }
11985
9f7c3e5e
AM
11986 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11987 goto error_return;
c152c796 11988
9f7c3e5e 11989 elf_final_link_free (abfd, &flinfo);
c152c796 11990
12bd6957 11991 elf_linker (abfd) = TRUE;
c152c796 11992
104d59d1
JM
11993 if (attr_section)
11994 {
a50b1753 11995 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11996 if (contents == NULL)
d0f16d5e 11997 return FALSE; /* Bail out and fail. */
104d59d1
JM
11998 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11999 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12000 free (contents);
12001 }
12002
c152c796
AM
12003 return TRUE;
12004
12005 error_return:
9f7c3e5e 12006 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12007 return FALSE;
12008}
12009\f
5241d853
RS
12010/* Initialize COOKIE for input bfd ABFD. */
12011
12012static bfd_boolean
12013init_reloc_cookie (struct elf_reloc_cookie *cookie,
12014 struct bfd_link_info *info, bfd *abfd)
12015{
12016 Elf_Internal_Shdr *symtab_hdr;
12017 const struct elf_backend_data *bed;
12018
12019 bed = get_elf_backend_data (abfd);
12020 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12021
12022 cookie->abfd = abfd;
12023 cookie->sym_hashes = elf_sym_hashes (abfd);
12024 cookie->bad_symtab = elf_bad_symtab (abfd);
12025 if (cookie->bad_symtab)
12026 {
12027 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12028 cookie->extsymoff = 0;
12029 }
12030 else
12031 {
12032 cookie->locsymcount = symtab_hdr->sh_info;
12033 cookie->extsymoff = symtab_hdr->sh_info;
12034 }
12035
12036 if (bed->s->arch_size == 32)
12037 cookie->r_sym_shift = 8;
12038 else
12039 cookie->r_sym_shift = 32;
12040
12041 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12042 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12043 {
12044 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12045 cookie->locsymcount, 0,
12046 NULL, NULL, NULL);
12047 if (cookie->locsyms == NULL)
12048 {
12049 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12050 return FALSE;
12051 }
12052 if (info->keep_memory)
12053 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12054 }
12055 return TRUE;
12056}
12057
12058/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12059
12060static void
12061fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12062{
12063 Elf_Internal_Shdr *symtab_hdr;
12064
12065 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12066 if (cookie->locsyms != NULL
12067 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12068 free (cookie->locsyms);
12069}
12070
12071/* Initialize the relocation information in COOKIE for input section SEC
12072 of input bfd ABFD. */
12073
12074static bfd_boolean
12075init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12076 struct bfd_link_info *info, bfd *abfd,
12077 asection *sec)
12078{
12079 const struct elf_backend_data *bed;
12080
12081 if (sec->reloc_count == 0)
12082 {
12083 cookie->rels = NULL;
12084 cookie->relend = NULL;
12085 }
12086 else
12087 {
12088 bed = get_elf_backend_data (abfd);
12089
12090 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12091 info->keep_memory);
12092 if (cookie->rels == NULL)
12093 return FALSE;
12094 cookie->rel = cookie->rels;
12095 cookie->relend = (cookie->rels
12096 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12097 }
12098 cookie->rel = cookie->rels;
12099 return TRUE;
12100}
12101
12102/* Free the memory allocated by init_reloc_cookie_rels,
12103 if appropriate. */
12104
12105static void
12106fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12107 asection *sec)
12108{
12109 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12110 free (cookie->rels);
12111}
12112
12113/* Initialize the whole of COOKIE for input section SEC. */
12114
12115static bfd_boolean
12116init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12117 struct bfd_link_info *info,
12118 asection *sec)
12119{
12120 if (!init_reloc_cookie (cookie, info, sec->owner))
12121 goto error1;
12122 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12123 goto error2;
12124 return TRUE;
12125
12126 error2:
12127 fini_reloc_cookie (cookie, sec->owner);
12128 error1:
12129 return FALSE;
12130}
12131
12132/* Free the memory allocated by init_reloc_cookie_for_section,
12133 if appropriate. */
12134
12135static void
12136fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12137 asection *sec)
12138{
12139 fini_reloc_cookie_rels (cookie, sec);
12140 fini_reloc_cookie (cookie, sec->owner);
12141}
12142\f
c152c796
AM
12143/* Garbage collect unused sections. */
12144
07adf181
AM
12145/* Default gc_mark_hook. */
12146
12147asection *
12148_bfd_elf_gc_mark_hook (asection *sec,
12149 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12150 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12151 struct elf_link_hash_entry *h,
12152 Elf_Internal_Sym *sym)
12153{
12154 if (h != NULL)
12155 {
12156 switch (h->root.type)
12157 {
12158 case bfd_link_hash_defined:
12159 case bfd_link_hash_defweak:
12160 return h->root.u.def.section;
12161
12162 case bfd_link_hash_common:
12163 return h->root.u.c.p->section;
12164
12165 default:
12166 break;
12167 }
12168 }
12169 else
12170 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12171
12172 return NULL;
12173}
12174
5241d853
RS
12175/* COOKIE->rel describes a relocation against section SEC, which is
12176 a section we've decided to keep. Return the section that contains
12177 the relocation symbol, or NULL if no section contains it. */
12178
12179asection *
12180_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12181 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12182 struct elf_reloc_cookie *cookie,
12183 bfd_boolean *start_stop)
5241d853
RS
12184{
12185 unsigned long r_symndx;
12186 struct elf_link_hash_entry *h;
12187
12188 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12189 if (r_symndx == STN_UNDEF)
5241d853
RS
12190 return NULL;
12191
12192 if (r_symndx >= cookie->locsymcount
12193 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12194 {
12195 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12196 if (h == NULL)
12197 {
12198 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12199 sec->owner);
12200 return NULL;
12201 }
5241d853
RS
12202 while (h->root.type == bfd_link_hash_indirect
12203 || h->root.type == bfd_link_hash_warning)
12204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12205 h->mark = 1;
4e6b54a6
AM
12206 /* If this symbol is weak and there is a non-weak definition, we
12207 keep the non-weak definition because many backends put
12208 dynamic reloc info on the non-weak definition for code
12209 handling copy relocs. */
12210 if (h->u.weakdef != NULL)
12211 h->u.weakdef->mark = 1;
1cce69b9
AM
12212
12213 if (start_stop != NULL
12214 && (h->root.type == bfd_link_hash_undefined
12215 || h->root.type == bfd_link_hash_undefweak))
12216 {
12217 /* To work around a glibc bug, mark all XXX input sections
12218 when there is an as yet undefined reference to __start_XXX
12219 or __stop_XXX symbols. The linker will later define such
12220 symbols for orphan input sections that have a name
12221 representable as a C identifier. */
12222 const char *sec_name = NULL;
12223 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12224 sec_name = h->root.root.string + 8;
12225 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12226 sec_name = h->root.root.string + 7;
12227
12228 if (sec_name != NULL && *sec_name != '\0')
12229 {
12230 bfd *i;
12231
12232 for (i = info->input_bfds; i != NULL; i = i->link.next)
12233 {
12234 asection *s = bfd_get_section_by_name (i, sec_name);
12235 if (s != NULL && !s->gc_mark)
12236 {
12237 *start_stop = TRUE;
12238 return s;
12239 }
12240 }
12241 }
12242 }
12243
5241d853
RS
12244 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12245 }
12246
12247 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12248 &cookie->locsyms[r_symndx]);
12249}
12250
12251/* COOKIE->rel describes a relocation against section SEC, which is
12252 a section we've decided to keep. Mark the section that contains
9d0a14d3 12253 the relocation symbol. */
5241d853
RS
12254
12255bfd_boolean
12256_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12257 asection *sec,
12258 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12259 struct elf_reloc_cookie *cookie)
5241d853
RS
12260{
12261 asection *rsec;
1cce69b9 12262 bfd_boolean start_stop = FALSE;
5241d853 12263
1cce69b9
AM
12264 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12265 while (rsec != NULL)
5241d853 12266 {
1cce69b9
AM
12267 if (!rsec->gc_mark)
12268 {
12269 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12270 || (rsec->owner->flags & DYNAMIC) != 0)
12271 rsec->gc_mark = 1;
12272 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12273 return FALSE;
12274 }
12275 if (!start_stop)
12276 break;
199af150 12277 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12278 }
12279 return TRUE;
12280}
12281
07adf181
AM
12282/* The mark phase of garbage collection. For a given section, mark
12283 it and any sections in this section's group, and all the sections
12284 which define symbols to which it refers. */
12285
ccfa59ea
AM
12286bfd_boolean
12287_bfd_elf_gc_mark (struct bfd_link_info *info,
12288 asection *sec,
6a5bb875 12289 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12290{
12291 bfd_boolean ret;
9d0a14d3 12292 asection *group_sec, *eh_frame;
c152c796
AM
12293
12294 sec->gc_mark = 1;
12295
12296 /* Mark all the sections in the group. */
12297 group_sec = elf_section_data (sec)->next_in_group;
12298 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12299 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12300 return FALSE;
12301
12302 /* Look through the section relocs. */
12303 ret = TRUE;
9d0a14d3
RS
12304 eh_frame = elf_eh_frame_section (sec->owner);
12305 if ((sec->flags & SEC_RELOC) != 0
12306 && sec->reloc_count > 0
12307 && sec != eh_frame)
c152c796 12308 {
5241d853 12309 struct elf_reloc_cookie cookie;
c152c796 12310
5241d853
RS
12311 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12312 ret = FALSE;
c152c796 12313 else
c152c796 12314 {
5241d853 12315 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12316 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12317 {
12318 ret = FALSE;
12319 break;
12320 }
12321 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12322 }
12323 }
9d0a14d3
RS
12324
12325 if (ret && eh_frame && elf_fde_list (sec))
12326 {
12327 struct elf_reloc_cookie cookie;
12328
12329 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12330 ret = FALSE;
12331 else
12332 {
12333 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12334 gc_mark_hook, &cookie))
12335 ret = FALSE;
12336 fini_reloc_cookie_for_section (&cookie, eh_frame);
12337 }
12338 }
12339
2f0c68f2
CM
12340 eh_frame = elf_section_eh_frame_entry (sec);
12341 if (ret && eh_frame && !eh_frame->gc_mark)
12342 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12343 ret = FALSE;
12344
c152c796
AM
12345 return ret;
12346}
12347
3c758495
TG
12348/* Scan and mark sections in a special or debug section group. */
12349
12350static void
12351_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12352{
12353 /* Point to first section of section group. */
12354 asection *ssec;
12355 /* Used to iterate the section group. */
12356 asection *msec;
12357
12358 bfd_boolean is_special_grp = TRUE;
12359 bfd_boolean is_debug_grp = TRUE;
12360
12361 /* First scan to see if group contains any section other than debug
12362 and special section. */
12363 ssec = msec = elf_next_in_group (grp);
12364 do
12365 {
12366 if ((msec->flags & SEC_DEBUGGING) == 0)
12367 is_debug_grp = FALSE;
12368
12369 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12370 is_special_grp = FALSE;
12371
12372 msec = elf_next_in_group (msec);
12373 }
12374 while (msec != ssec);
12375
12376 /* If this is a pure debug section group or pure special section group,
12377 keep all sections in this group. */
12378 if (is_debug_grp || is_special_grp)
12379 {
12380 do
12381 {
12382 msec->gc_mark = 1;
12383 msec = elf_next_in_group (msec);
12384 }
12385 while (msec != ssec);
12386 }
12387}
12388
7f6ab9f8
AM
12389/* Keep debug and special sections. */
12390
12391bfd_boolean
12392_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12393 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12394{
12395 bfd *ibfd;
12396
c72f2fb2 12397 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12398 {
12399 asection *isec;
12400 bfd_boolean some_kept;
b40bf0a2 12401 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12402
12403 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12404 continue;
12405
b40bf0a2
NC
12406 /* Ensure all linker created sections are kept,
12407 see if any other section is already marked,
12408 and note if we have any fragmented debug sections. */
12409 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12410 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12411 {
12412 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12413 isec->gc_mark = 1;
12414 else if (isec->gc_mark)
12415 some_kept = TRUE;
b40bf0a2
NC
12416
12417 if (debug_frag_seen == FALSE
12418 && (isec->flags & SEC_DEBUGGING)
12419 && CONST_STRNEQ (isec->name, ".debug_line."))
12420 debug_frag_seen = TRUE;
7f6ab9f8
AM
12421 }
12422
12423 /* If no section in this file will be kept, then we can
b40bf0a2 12424 toss out the debug and special sections. */
7f6ab9f8
AM
12425 if (!some_kept)
12426 continue;
12427
12428 /* Keep debug and special sections like .comment when they are
3c758495
TG
12429 not part of a group. Also keep section groups that contain
12430 just debug sections or special sections. */
7f6ab9f8 12431 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12432 {
12433 if ((isec->flags & SEC_GROUP) != 0)
12434 _bfd_elf_gc_mark_debug_special_section_group (isec);
12435 else if (((isec->flags & SEC_DEBUGGING) != 0
12436 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12437 && elf_next_in_group (isec) == NULL)
12438 isec->gc_mark = 1;
12439 }
b40bf0a2
NC
12440
12441 if (! debug_frag_seen)
12442 continue;
12443
12444 /* Look for CODE sections which are going to be discarded,
12445 and find and discard any fragmented debug sections which
12446 are associated with that code section. */
12447 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12448 if ((isec->flags & SEC_CODE) != 0
12449 && isec->gc_mark == 0)
12450 {
12451 unsigned int ilen;
12452 asection *dsec;
12453
12454 ilen = strlen (isec->name);
12455
12456 /* Association is determined by the name of the debug section
12457 containing the name of the code section as a suffix. For
12458 example .debug_line.text.foo is a debug section associated
12459 with .text.foo. */
12460 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12461 {
12462 unsigned int dlen;
12463
12464 if (dsec->gc_mark == 0
12465 || (dsec->flags & SEC_DEBUGGING) == 0)
12466 continue;
12467
12468 dlen = strlen (dsec->name);
12469
12470 if (dlen > ilen
12471 && strncmp (dsec->name + (dlen - ilen),
12472 isec->name, ilen) == 0)
12473 {
12474 dsec->gc_mark = 0;
b40bf0a2
NC
12475 }
12476 }
12477 }
7f6ab9f8
AM
12478 }
12479 return TRUE;
12480}
12481
c152c796
AM
12482/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12483
c17d87de
NC
12484struct elf_gc_sweep_symbol_info
12485{
ccabcbe5
AM
12486 struct bfd_link_info *info;
12487 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12488 bfd_boolean);
12489};
12490
c152c796 12491static bfd_boolean
ccabcbe5 12492elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12493{
1d5316ab
AM
12494 if (!h->mark
12495 && (((h->root.type == bfd_link_hash_defined
12496 || h->root.type == bfd_link_hash_defweak)
c4621b33 12497 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12498 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12499 || h->root.type == bfd_link_hash_undefined
12500 || h->root.type == bfd_link_hash_undefweak))
12501 {
12502 struct elf_gc_sweep_symbol_info *inf;
12503
12504 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12505 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12506 h->def_regular = 0;
12507 h->ref_regular = 0;
12508 h->ref_regular_nonweak = 0;
ccabcbe5 12509 }
c152c796
AM
12510
12511 return TRUE;
12512}
12513
12514/* The sweep phase of garbage collection. Remove all garbage sections. */
12515
12516typedef bfd_boolean (*gc_sweep_hook_fn)
12517 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12518
12519static bfd_boolean
ccabcbe5 12520elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12521{
12522 bfd *sub;
ccabcbe5
AM
12523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12524 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12525 unsigned long section_sym_count;
12526 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12527
c72f2fb2 12528 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12529 {
12530 asection *o;
12531
b19a8f85
L
12532 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12533 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12534 continue;
12535
12536 for (o = sub->sections; o != NULL; o = o->next)
12537 {
a33dafc3
L
12538 /* When any section in a section group is kept, we keep all
12539 sections in the section group. If the first member of
12540 the section group is excluded, we will also exclude the
12541 group section. */
12542 if (o->flags & SEC_GROUP)
12543 {
12544 asection *first = elf_next_in_group (o);
12545 o->gc_mark = first->gc_mark;
12546 }
c152c796 12547
1e7eae0d 12548 if (o->gc_mark)
c152c796
AM
12549 continue;
12550
12551 /* Skip sweeping sections already excluded. */
12552 if (o->flags & SEC_EXCLUDE)
12553 continue;
12554
12555 /* Since this is early in the link process, it is simple
12556 to remove a section from the output. */
12557 o->flags |= SEC_EXCLUDE;
12558
c55fe096 12559 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12560 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12561
c152c796
AM
12562 /* But we also have to update some of the relocation
12563 info we collected before. */
12564 if (gc_sweep_hook
e8aaee2a 12565 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12566 && o->reloc_count != 0
12567 && !((info->strip == strip_all || info->strip == strip_debugger)
12568 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12569 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12570 {
12571 Elf_Internal_Rela *internal_relocs;
12572 bfd_boolean r;
12573
12574 internal_relocs
12575 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12576 info->keep_memory);
12577 if (internal_relocs == NULL)
12578 return FALSE;
12579
12580 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12581
12582 if (elf_section_data (o)->relocs != internal_relocs)
12583 free (internal_relocs);
12584
12585 if (!r)
12586 return FALSE;
12587 }
12588 }
12589 }
12590
12591 /* Remove the symbols that were in the swept sections from the dynamic
12592 symbol table. GCFIXME: Anyone know how to get them out of the
12593 static symbol table as well? */
ccabcbe5
AM
12594 sweep_info.info = info;
12595 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12596 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12597 &sweep_info);
c152c796 12598
ccabcbe5 12599 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12600 return TRUE;
12601}
12602
12603/* Propagate collected vtable information. This is called through
12604 elf_link_hash_traverse. */
12605
12606static bfd_boolean
12607elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12608{
c152c796 12609 /* Those that are not vtables. */
f6e332e6 12610 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12611 return TRUE;
12612
12613 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12614 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12615 return TRUE;
12616
12617 /* If we've already been done, exit. */
f6e332e6 12618 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12619 return TRUE;
12620
12621 /* Make sure the parent's table is up to date. */
f6e332e6 12622 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12623
f6e332e6 12624 if (h->vtable->used == NULL)
c152c796
AM
12625 {
12626 /* None of this table's entries were referenced. Re-use the
12627 parent's table. */
f6e332e6
AM
12628 h->vtable->used = h->vtable->parent->vtable->used;
12629 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12630 }
12631 else
12632 {
12633 size_t n;
12634 bfd_boolean *cu, *pu;
12635
12636 /* Or the parent's entries into ours. */
f6e332e6 12637 cu = h->vtable->used;
c152c796 12638 cu[-1] = TRUE;
f6e332e6 12639 pu = h->vtable->parent->vtable->used;
c152c796
AM
12640 if (pu != NULL)
12641 {
12642 const struct elf_backend_data *bed;
12643 unsigned int log_file_align;
12644
12645 bed = get_elf_backend_data (h->root.u.def.section->owner);
12646 log_file_align = bed->s->log_file_align;
f6e332e6 12647 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12648 while (n--)
12649 {
12650 if (*pu)
12651 *cu = TRUE;
12652 pu++;
12653 cu++;
12654 }
12655 }
12656 }
12657
12658 return TRUE;
12659}
12660
12661static bfd_boolean
12662elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12663{
12664 asection *sec;
12665 bfd_vma hstart, hend;
12666 Elf_Internal_Rela *relstart, *relend, *rel;
12667 const struct elf_backend_data *bed;
12668 unsigned int log_file_align;
12669
c152c796
AM
12670 /* Take care of both those symbols that do not describe vtables as
12671 well as those that are not loaded. */
f6e332e6 12672 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12673 return TRUE;
12674
12675 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12676 || h->root.type == bfd_link_hash_defweak);
12677
12678 sec = h->root.u.def.section;
12679 hstart = h->root.u.def.value;
12680 hend = hstart + h->size;
12681
12682 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12683 if (!relstart)
12684 return *(bfd_boolean *) okp = FALSE;
12685 bed = get_elf_backend_data (sec->owner);
12686 log_file_align = bed->s->log_file_align;
12687
12688 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12689
12690 for (rel = relstart; rel < relend; ++rel)
12691 if (rel->r_offset >= hstart && rel->r_offset < hend)
12692 {
12693 /* If the entry is in use, do nothing. */
f6e332e6
AM
12694 if (h->vtable->used
12695 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12696 {
12697 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12698 if (h->vtable->used[entry])
c152c796
AM
12699 continue;
12700 }
12701 /* Otherwise, kill it. */
12702 rel->r_offset = rel->r_info = rel->r_addend = 0;
12703 }
12704
12705 return TRUE;
12706}
12707
87538722
AM
12708/* Mark sections containing dynamically referenced symbols. When
12709 building shared libraries, we must assume that any visible symbol is
12710 referenced. */
715df9b8 12711
64d03ab5
AM
12712bfd_boolean
12713bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12714{
87538722 12715 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12716 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12717
715df9b8
EB
12718 if ((h->root.type == bfd_link_hash_defined
12719 || h->root.type == bfd_link_hash_defweak)
87538722 12720 && (h->ref_dynamic
c4621b33 12721 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12722 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12723 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12724 && (!bfd_link_executable (info)
b407645f
AM
12725 || info->export_dynamic
12726 || (h->dynamic
12727 && d != NULL
12728 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12729 && (h->versioned >= versioned
54e8959c
L
12730 || !bfd_hide_sym_by_version (info->version_info,
12731 h->root.root.string)))))
715df9b8
EB
12732 h->root.u.def.section->flags |= SEC_KEEP;
12733
12734 return TRUE;
12735}
3b36f7e6 12736
74f0fb50
AM
12737/* Keep all sections containing symbols undefined on the command-line,
12738 and the section containing the entry symbol. */
12739
12740void
12741_bfd_elf_gc_keep (struct bfd_link_info *info)
12742{
12743 struct bfd_sym_chain *sym;
12744
12745 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12746 {
12747 struct elf_link_hash_entry *h;
12748
12749 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12750 FALSE, FALSE, FALSE);
12751
12752 if (h != NULL
12753 && (h->root.type == bfd_link_hash_defined
12754 || h->root.type == bfd_link_hash_defweak)
12755 && !bfd_is_abs_section (h->root.u.def.section))
12756 h->root.u.def.section->flags |= SEC_KEEP;
12757 }
12758}
12759
2f0c68f2
CM
12760bfd_boolean
12761bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12762 struct bfd_link_info *info)
12763{
12764 bfd *ibfd = info->input_bfds;
12765
12766 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12767 {
12768 asection *sec;
12769 struct elf_reloc_cookie cookie;
12770
12771 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12772 continue;
12773
12774 if (!init_reloc_cookie (&cookie, info, ibfd))
12775 return FALSE;
12776
12777 for (sec = ibfd->sections; sec; sec = sec->next)
12778 {
12779 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12780 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12781 {
12782 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12783 fini_reloc_cookie_rels (&cookie, sec);
12784 }
12785 }
12786 }
12787 return TRUE;
12788}
12789
c152c796
AM
12790/* Do mark and sweep of unused sections. */
12791
12792bfd_boolean
12793bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12794{
12795 bfd_boolean ok = TRUE;
12796 bfd *sub;
6a5bb875 12797 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12798 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12799 struct elf_link_hash_table *htab;
c152c796 12800
64d03ab5 12801 if (!bed->can_gc_sections
715df9b8 12802 || !is_elf_hash_table (info->hash))
c152c796
AM
12803 {
12804 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12805 return TRUE;
12806 }
12807
74f0fb50 12808 bed->gc_keep (info);
da44f4e5 12809 htab = elf_hash_table (info);
74f0fb50 12810
9d0a14d3
RS
12811 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12812 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12813 for (sub = info->input_bfds;
12814 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12815 sub = sub->link.next)
9d0a14d3
RS
12816 {
12817 asection *sec;
12818 struct elf_reloc_cookie cookie;
12819
12820 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12821 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12822 {
12823 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12824 if (elf_section_data (sec)->sec_info
12825 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12826 elf_eh_frame_section (sub) = sec;
12827 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12828 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12829 }
12830 }
9d0a14d3 12831
c152c796 12832 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12833 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12834 if (!ok)
12835 return FALSE;
12836
12837 /* Kill the vtable relocations that were not used. */
da44f4e5 12838 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12839 if (!ok)
12840 return FALSE;
12841
715df9b8 12842 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12843 if (htab->dynamic_sections_created)
12844 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 12845
715df9b8 12846 /* Grovel through relocs to find out who stays ... */
64d03ab5 12847 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 12848 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12849 {
12850 asection *o;
12851
b19a8f85
L
12852 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12853 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12854 continue;
12855
7f6ab9f8
AM
12856 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12857 Also treat note sections as a root, if the section is not part
12858 of a group. */
c152c796 12859 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12860 if (!o->gc_mark
12861 && (o->flags & SEC_EXCLUDE) == 0
24007750 12862 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12863 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12864 && elf_next_in_group (o) == NULL )))
12865 {
12866 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12867 return FALSE;
12868 }
c152c796
AM
12869 }
12870
6a5bb875 12871 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12872 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12873
c152c796 12874 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12875 return elf_gc_sweep (abfd, info);
c152c796
AM
12876}
12877\f
12878/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12879
12880bfd_boolean
12881bfd_elf_gc_record_vtinherit (bfd *abfd,
12882 asection *sec,
12883 struct elf_link_hash_entry *h,
12884 bfd_vma offset)
12885{
12886 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12887 struct elf_link_hash_entry **search, *child;
12888 bfd_size_type extsymcount;
12889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12890
12891 /* The sh_info field of the symtab header tells us where the
12892 external symbols start. We don't care about the local symbols at
12893 this point. */
12894 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12895 if (!elf_bad_symtab (abfd))
12896 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12897
12898 sym_hashes = elf_sym_hashes (abfd);
12899 sym_hashes_end = sym_hashes + extsymcount;
12900
12901 /* Hunt down the child symbol, which is in this section at the same
12902 offset as the relocation. */
12903 for (search = sym_hashes; search != sym_hashes_end; ++search)
12904 {
12905 if ((child = *search) != NULL
12906 && (child->root.type == bfd_link_hash_defined
12907 || child->root.type == bfd_link_hash_defweak)
12908 && child->root.u.def.section == sec
12909 && child->root.u.def.value == offset)
12910 goto win;
12911 }
12912
d003868e
AM
12913 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12914 abfd, sec, (unsigned long) offset);
c152c796
AM
12915 bfd_set_error (bfd_error_invalid_operation);
12916 return FALSE;
12917
12918 win:
f6e332e6
AM
12919 if (!child->vtable)
12920 {
ca4be51c
AM
12921 child->vtable = ((struct elf_link_virtual_table_entry *)
12922 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
12923 if (!child->vtable)
12924 return FALSE;
12925 }
c152c796
AM
12926 if (!h)
12927 {
12928 /* This *should* only be the absolute section. It could potentially
12929 be that someone has defined a non-global vtable though, which
12930 would be bad. It isn't worth paging in the local symbols to be
12931 sure though; that case should simply be handled by the assembler. */
12932
f6e332e6 12933 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12934 }
12935 else
f6e332e6 12936 child->vtable->parent = h;
c152c796
AM
12937
12938 return TRUE;
12939}
12940
12941/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12942
12943bfd_boolean
12944bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12945 asection *sec ATTRIBUTE_UNUSED,
12946 struct elf_link_hash_entry *h,
12947 bfd_vma addend)
12948{
12949 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12950 unsigned int log_file_align = bed->s->log_file_align;
12951
f6e332e6
AM
12952 if (!h->vtable)
12953 {
ca4be51c
AM
12954 h->vtable = ((struct elf_link_virtual_table_entry *)
12955 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
12956 if (!h->vtable)
12957 return FALSE;
12958 }
12959
12960 if (addend >= h->vtable->size)
c152c796
AM
12961 {
12962 size_t size, bytes, file_align;
f6e332e6 12963 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12964
12965 /* While the symbol is undefined, we have to be prepared to handle
12966 a zero size. */
12967 file_align = 1 << log_file_align;
12968 if (h->root.type == bfd_link_hash_undefined)
12969 size = addend + file_align;
12970 else
12971 {
12972 size = h->size;
12973 if (addend >= size)
12974 {
12975 /* Oops! We've got a reference past the defined end of
12976 the table. This is probably a bug -- shall we warn? */
12977 size = addend + file_align;
12978 }
12979 }
12980 size = (size + file_align - 1) & -file_align;
12981
12982 /* Allocate one extra entry for use as a "done" flag for the
12983 consolidation pass. */
12984 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12985
12986 if (ptr)
12987 {
a50b1753 12988 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12989
12990 if (ptr != NULL)
12991 {
12992 size_t oldbytes;
12993
f6e332e6 12994 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12995 * sizeof (bfd_boolean));
12996 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12997 }
12998 }
12999 else
a50b1753 13000 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13001
13002 if (ptr == NULL)
13003 return FALSE;
13004
13005 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13006 h->vtable->used = ptr + 1;
13007 h->vtable->size = size;
c152c796
AM
13008 }
13009
f6e332e6 13010 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13011
13012 return TRUE;
13013}
13014
ae17ab41
CM
13015/* Map an ELF section header flag to its corresponding string. */
13016typedef struct
13017{
13018 char *flag_name;
13019 flagword flag_value;
13020} elf_flags_to_name_table;
13021
13022static elf_flags_to_name_table elf_flags_to_names [] =
13023{
13024 { "SHF_WRITE", SHF_WRITE },
13025 { "SHF_ALLOC", SHF_ALLOC },
13026 { "SHF_EXECINSTR", SHF_EXECINSTR },
13027 { "SHF_MERGE", SHF_MERGE },
13028 { "SHF_STRINGS", SHF_STRINGS },
13029 { "SHF_INFO_LINK", SHF_INFO_LINK},
13030 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13031 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13032 { "SHF_GROUP", SHF_GROUP },
13033 { "SHF_TLS", SHF_TLS },
13034 { "SHF_MASKOS", SHF_MASKOS },
13035 { "SHF_EXCLUDE", SHF_EXCLUDE },
13036};
13037
b9c361e0
JL
13038/* Returns TRUE if the section is to be included, otherwise FALSE. */
13039bfd_boolean
ae17ab41 13040bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13041 struct flag_info *flaginfo,
b9c361e0 13042 asection *section)
ae17ab41 13043{
8b127cbc 13044 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13045
8b127cbc 13046 if (!flaginfo->flags_initialized)
ae17ab41 13047 {
8b127cbc
AM
13048 bfd *obfd = info->output_bfd;
13049 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13050 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13051 int with_hex = 0;
13052 int without_hex = 0;
13053
8b127cbc 13054 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13055 {
b9c361e0 13056 unsigned i;
8b127cbc 13057 flagword (*lookup) (char *);
ae17ab41 13058
8b127cbc
AM
13059 lookup = bed->elf_backend_lookup_section_flags_hook;
13060 if (lookup != NULL)
ae17ab41 13061 {
8b127cbc 13062 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13063
13064 if (hexval != 0)
13065 {
13066 if (tf->with == with_flags)
13067 with_hex |= hexval;
13068 else if (tf->with == without_flags)
13069 without_hex |= hexval;
13070 tf->valid = TRUE;
13071 continue;
13072 }
ae17ab41 13073 }
8b127cbc 13074 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13075 {
8b127cbc 13076 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13077 {
13078 if (tf->with == with_flags)
13079 with_hex |= elf_flags_to_names[i].flag_value;
13080 else if (tf->with == without_flags)
13081 without_hex |= elf_flags_to_names[i].flag_value;
13082 tf->valid = TRUE;
13083 break;
13084 }
13085 }
8b127cbc 13086 if (!tf->valid)
b9c361e0 13087 {
68ffbac6 13088 info->callbacks->einfo
8b127cbc 13089 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13090 return FALSE;
ae17ab41
CM
13091 }
13092 }
8b127cbc
AM
13093 flaginfo->flags_initialized = TRUE;
13094 flaginfo->only_with_flags |= with_hex;
13095 flaginfo->not_with_flags |= without_hex;
ae17ab41 13096 }
ae17ab41 13097
8b127cbc 13098 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13099 return FALSE;
13100
8b127cbc 13101 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13102 return FALSE;
13103
13104 return TRUE;
ae17ab41
CM
13105}
13106
c152c796
AM
13107struct alloc_got_off_arg {
13108 bfd_vma gotoff;
10455f89 13109 struct bfd_link_info *info;
c152c796
AM
13110};
13111
13112/* We need a special top-level link routine to convert got reference counts
13113 to real got offsets. */
13114
13115static bfd_boolean
13116elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13117{
a50b1753 13118 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13119 bfd *obfd = gofarg->info->output_bfd;
13120 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13121
c152c796
AM
13122 if (h->got.refcount > 0)
13123 {
13124 h->got.offset = gofarg->gotoff;
10455f89 13125 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13126 }
13127 else
13128 h->got.offset = (bfd_vma) -1;
13129
13130 return TRUE;
13131}
13132
13133/* And an accompanying bit to work out final got entry offsets once
13134 we're done. Should be called from final_link. */
13135
13136bfd_boolean
13137bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13138 struct bfd_link_info *info)
13139{
13140 bfd *i;
13141 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13142 bfd_vma gotoff;
c152c796
AM
13143 struct alloc_got_off_arg gofarg;
13144
10455f89
HPN
13145 BFD_ASSERT (abfd == info->output_bfd);
13146
c152c796
AM
13147 if (! is_elf_hash_table (info->hash))
13148 return FALSE;
13149
13150 /* The GOT offset is relative to the .got section, but the GOT header is
13151 put into the .got.plt section, if the backend uses it. */
13152 if (bed->want_got_plt)
13153 gotoff = 0;
13154 else
13155 gotoff = bed->got_header_size;
13156
13157 /* Do the local .got entries first. */
c72f2fb2 13158 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13159 {
13160 bfd_signed_vma *local_got;
13161 bfd_size_type j, locsymcount;
13162 Elf_Internal_Shdr *symtab_hdr;
13163
13164 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13165 continue;
13166
13167 local_got = elf_local_got_refcounts (i);
13168 if (!local_got)
13169 continue;
13170
13171 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13172 if (elf_bad_symtab (i))
13173 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13174 else
13175 locsymcount = symtab_hdr->sh_info;
13176
13177 for (j = 0; j < locsymcount; ++j)
13178 {
13179 if (local_got[j] > 0)
13180 {
13181 local_got[j] = gotoff;
10455f89 13182 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13183 }
13184 else
13185 local_got[j] = (bfd_vma) -1;
13186 }
13187 }
13188
13189 /* Then the global .got entries. .plt refcounts are handled by
13190 adjust_dynamic_symbol */
13191 gofarg.gotoff = gotoff;
10455f89 13192 gofarg.info = info;
c152c796
AM
13193 elf_link_hash_traverse (elf_hash_table (info),
13194 elf_gc_allocate_got_offsets,
13195 &gofarg);
13196 return TRUE;
13197}
13198
13199/* Many folk need no more in the way of final link than this, once
13200 got entry reference counting is enabled. */
13201
13202bfd_boolean
13203bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13204{
13205 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13206 return FALSE;
13207
13208 /* Invoke the regular ELF backend linker to do all the work. */
13209 return bfd_elf_final_link (abfd, info);
13210}
13211
13212bfd_boolean
13213bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13214{
a50b1753 13215 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13216
13217 if (rcookie->bad_symtab)
13218 rcookie->rel = rcookie->rels;
13219
13220 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13221 {
13222 unsigned long r_symndx;
13223
13224 if (! rcookie->bad_symtab)
13225 if (rcookie->rel->r_offset > offset)
13226 return FALSE;
13227 if (rcookie->rel->r_offset != offset)
13228 continue;
13229
13230 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13231 if (r_symndx == STN_UNDEF)
c152c796
AM
13232 return TRUE;
13233
13234 if (r_symndx >= rcookie->locsymcount
13235 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13236 {
13237 struct elf_link_hash_entry *h;
13238
13239 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13240
13241 while (h->root.type == bfd_link_hash_indirect
13242 || h->root.type == bfd_link_hash_warning)
13243 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13244
13245 if ((h->root.type == bfd_link_hash_defined
13246 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13247 && (h->root.u.def.section->owner != rcookie->abfd
13248 || h->root.u.def.section->kept_section != NULL
13249 || discarded_section (h->root.u.def.section)))
c152c796 13250 return TRUE;
c152c796
AM
13251 }
13252 else
13253 {
13254 /* It's not a relocation against a global symbol,
13255 but it could be a relocation against a local
13256 symbol for a discarded section. */
13257 asection *isec;
13258 Elf_Internal_Sym *isym;
13259
13260 /* Need to: get the symbol; get the section. */
13261 isym = &rcookie->locsyms[r_symndx];
cb33740c 13262 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13263 if (isec != NULL
13264 && (isec->kept_section != NULL
13265 || discarded_section (isec)))
cb33740c 13266 return TRUE;
c152c796
AM
13267 }
13268 return FALSE;
13269 }
13270 return FALSE;
13271}
13272
13273/* Discard unneeded references to discarded sections.
75938853
AM
13274 Returns -1 on error, 1 if any section's size was changed, 0 if
13275 nothing changed. This function assumes that the relocations are in
13276 sorted order, which is true for all known assemblers. */
c152c796 13277
75938853 13278int
c152c796
AM
13279bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13280{
13281 struct elf_reloc_cookie cookie;
18cd5bce 13282 asection *o;
c152c796 13283 bfd *abfd;
75938853 13284 int changed = 0;
c152c796
AM
13285
13286 if (info->traditional_format
13287 || !is_elf_hash_table (info->hash))
75938853 13288 return 0;
c152c796 13289
18cd5bce
AM
13290 o = bfd_get_section_by_name (output_bfd, ".stab");
13291 if (o != NULL)
c152c796 13292 {
18cd5bce 13293 asection *i;
c152c796 13294
18cd5bce 13295 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13296 {
18cd5bce
AM
13297 if (i->size == 0
13298 || i->reloc_count == 0
13299 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13300 continue;
c152c796 13301
18cd5bce
AM
13302 abfd = i->owner;
13303 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13304 continue;
c152c796 13305
18cd5bce 13306 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13307 return -1;
c152c796 13308
18cd5bce
AM
13309 if (_bfd_discard_section_stabs (abfd, i,
13310 elf_section_data (i)->sec_info,
5241d853
RS
13311 bfd_elf_reloc_symbol_deleted_p,
13312 &cookie))
75938853 13313 changed = 1;
18cd5bce
AM
13314
13315 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13316 }
18cd5bce
AM
13317 }
13318
2f0c68f2
CM
13319 o = NULL;
13320 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13321 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13322 if (o != NULL)
13323 {
13324 asection *i;
c152c796 13325
18cd5bce 13326 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13327 {
18cd5bce
AM
13328 if (i->size == 0)
13329 continue;
13330
13331 abfd = i->owner;
13332 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13333 continue;
13334
13335 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13336 return -1;
18cd5bce
AM
13337
13338 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13339 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13340 bfd_elf_reloc_symbol_deleted_p,
13341 &cookie))
75938853 13342 changed = 1;
18cd5bce
AM
13343
13344 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13345 }
18cd5bce 13346 }
c152c796 13347
18cd5bce
AM
13348 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13349 {
13350 const struct elf_backend_data *bed;
c152c796 13351
18cd5bce
AM
13352 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13353 continue;
13354
13355 bed = get_elf_backend_data (abfd);
13356
13357 if (bed->elf_backend_discard_info != NULL)
13358 {
13359 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13360 return -1;
18cd5bce
AM
13361
13362 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13363 changed = 1;
18cd5bce
AM
13364
13365 fini_reloc_cookie (&cookie, abfd);
13366 }
c152c796
AM
13367 }
13368
2f0c68f2
CM
13369 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13370 _bfd_elf_end_eh_frame_parsing (info);
13371
13372 if (info->eh_frame_hdr_type
0e1862bb 13373 && !bfd_link_relocatable (info)
c152c796 13374 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13375 changed = 1;
c152c796 13376
75938853 13377 return changed;
c152c796 13378}
082b7297 13379
43e1669b 13380bfd_boolean
0c511000 13381_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13382 asection *sec,
c0f00686 13383 struct bfd_link_info *info)
082b7297
L
13384{
13385 flagword flags;
c77ec726 13386 const char *name, *key;
082b7297
L
13387 struct bfd_section_already_linked *l;
13388 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13389
c77ec726
AM
13390 if (sec->output_section == bfd_abs_section_ptr)
13391 return FALSE;
0c511000 13392
c77ec726 13393 flags = sec->flags;
0c511000 13394
c77ec726
AM
13395 /* Return if it isn't a linkonce section. A comdat group section
13396 also has SEC_LINK_ONCE set. */
13397 if ((flags & SEC_LINK_ONCE) == 0)
13398 return FALSE;
0c511000 13399
c77ec726
AM
13400 /* Don't put group member sections on our list of already linked
13401 sections. They are handled as a group via their group section. */
13402 if (elf_sec_group (sec) != NULL)
13403 return FALSE;
0c511000 13404
c77ec726
AM
13405 /* For a SHT_GROUP section, use the group signature as the key. */
13406 name = sec->name;
13407 if ((flags & SEC_GROUP) != 0
13408 && elf_next_in_group (sec) != NULL
13409 && elf_group_name (elf_next_in_group (sec)) != NULL)
13410 key = elf_group_name (elf_next_in_group (sec));
13411 else
13412 {
13413 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13414 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13415 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13416 key++;
0c511000 13417 else
c77ec726
AM
13418 /* Must be a user linkonce section that doesn't follow gcc's
13419 naming convention. In this case we won't be matching
13420 single member groups. */
13421 key = name;
0c511000 13422 }
6d2cd210 13423
c77ec726 13424 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13425
13426 for (l = already_linked_list->entry; l != NULL; l = l->next)
13427 {
c2370991 13428 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13429 sections with a signature of <key> (<key> is some string),
13430 and linkonce sections named .gnu.linkonce.<type>.<key>.
13431 Match like sections. LTO plugin sections are an exception.
13432 They are always named .gnu.linkonce.t.<key> and match either
13433 type of section. */
13434 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13435 && ((flags & SEC_GROUP) != 0
13436 || strcmp (name, l->sec->name) == 0))
13437 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13438 {
13439 /* The section has already been linked. See if we should
6d2cd210 13440 issue a warning. */
c77ec726
AM
13441 if (!_bfd_handle_already_linked (sec, l, info))
13442 return FALSE;
082b7297 13443
c77ec726 13444 if (flags & SEC_GROUP)
3d7f7666 13445 {
c77ec726
AM
13446 asection *first = elf_next_in_group (sec);
13447 asection *s = first;
3d7f7666 13448
c77ec726 13449 while (s != NULL)
3d7f7666 13450 {
c77ec726
AM
13451 s->output_section = bfd_abs_section_ptr;
13452 /* Record which group discards it. */
13453 s->kept_section = l->sec;
13454 s = elf_next_in_group (s);
13455 /* These lists are circular. */
13456 if (s == first)
13457 break;
3d7f7666
L
13458 }
13459 }
082b7297 13460
43e1669b 13461 return TRUE;
082b7297
L
13462 }
13463 }
13464
c77ec726
AM
13465 /* A single member comdat group section may be discarded by a
13466 linkonce section and vice versa. */
13467 if ((flags & SEC_GROUP) != 0)
3d7f7666 13468 {
c77ec726 13469 asection *first = elf_next_in_group (sec);
c2370991 13470
c77ec726
AM
13471 if (first != NULL && elf_next_in_group (first) == first)
13472 /* Check this single member group against linkonce sections. */
13473 for (l = already_linked_list->entry; l != NULL; l = l->next)
13474 if ((l->sec->flags & SEC_GROUP) == 0
13475 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13476 {
13477 first->output_section = bfd_abs_section_ptr;
13478 first->kept_section = l->sec;
13479 sec->output_section = bfd_abs_section_ptr;
13480 break;
13481 }
13482 }
13483 else
13484 /* Check this linkonce section against single member groups. */
13485 for (l = already_linked_list->entry; l != NULL; l = l->next)
13486 if (l->sec->flags & SEC_GROUP)
6d2cd210 13487 {
c77ec726 13488 asection *first = elf_next_in_group (l->sec);
6d2cd210 13489
c77ec726
AM
13490 if (first != NULL
13491 && elf_next_in_group (first) == first
13492 && bfd_elf_match_symbols_in_sections (first, sec, info))
13493 {
13494 sec->output_section = bfd_abs_section_ptr;
13495 sec->kept_section = first;
13496 break;
13497 }
6d2cd210 13498 }
0c511000 13499
c77ec726
AM
13500 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13501 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13502 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13503 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13504 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13505 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13506 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13507 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13508 The reverse order cannot happen as there is never a bfd with only the
13509 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13510 matter as here were are looking only for cross-bfd sections. */
13511
13512 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13513 for (l = already_linked_list->entry; l != NULL; l = l->next)
13514 if ((l->sec->flags & SEC_GROUP) == 0
13515 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13516 {
13517 if (abfd != l->sec->owner)
13518 sec->output_section = bfd_abs_section_ptr;
13519 break;
13520 }
80c29487 13521
082b7297 13522 /* This is the first section with this name. Record it. */
c77ec726 13523 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13524 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13525 return sec->output_section == bfd_abs_section_ptr;
082b7297 13526}
81e1b023 13527
a4d8e49b
L
13528bfd_boolean
13529_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13530{
13531 return sym->st_shndx == SHN_COMMON;
13532}
13533
13534unsigned int
13535_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13536{
13537 return SHN_COMMON;
13538}
13539
13540asection *
13541_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13542{
13543 return bfd_com_section_ptr;
13544}
10455f89
HPN
13545
13546bfd_vma
13547_bfd_elf_default_got_elt_size (bfd *abfd,
13548 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13549 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13550 bfd *ibfd ATTRIBUTE_UNUSED,
13551 unsigned long symndx ATTRIBUTE_UNUSED)
13552{
13553 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13554 return bed->s->arch_size / 8;
13555}
83bac4b0
NC
13556
13557/* Routines to support the creation of dynamic relocs. */
13558
83bac4b0
NC
13559/* Returns the name of the dynamic reloc section associated with SEC. */
13560
13561static const char *
13562get_dynamic_reloc_section_name (bfd * abfd,
13563 asection * sec,
13564 bfd_boolean is_rela)
13565{
ddcf1fcf
BS
13566 char *name;
13567 const char *old_name = bfd_get_section_name (NULL, sec);
13568 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13569
ddcf1fcf 13570 if (old_name == NULL)
83bac4b0
NC
13571 return NULL;
13572
ddcf1fcf 13573 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13574 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13575
13576 return name;
13577}
13578
13579/* Returns the dynamic reloc section associated with SEC.
13580 If necessary compute the name of the dynamic reloc section based
13581 on SEC's name (looked up in ABFD's string table) and the setting
13582 of IS_RELA. */
13583
13584asection *
13585_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13586 asection * sec,
13587 bfd_boolean is_rela)
13588{
13589 asection * reloc_sec = elf_section_data (sec)->sreloc;
13590
13591 if (reloc_sec == NULL)
13592 {
13593 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13594
13595 if (name != NULL)
13596 {
3d4d4302 13597 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13598
13599 if (reloc_sec != NULL)
13600 elf_section_data (sec)->sreloc = reloc_sec;
13601 }
13602 }
13603
13604 return reloc_sec;
13605}
13606
13607/* Returns the dynamic reloc section associated with SEC. If the
13608 section does not exist it is created and attached to the DYNOBJ
13609 bfd and stored in the SRELOC field of SEC's elf_section_data
13610 structure.
f8076f98 13611
83bac4b0
NC
13612 ALIGNMENT is the alignment for the newly created section and
13613 IS_RELA defines whether the name should be .rela.<SEC's name>
13614 or .rel.<SEC's name>. The section name is looked up in the
13615 string table associated with ABFD. */
13616
13617asection *
ca4be51c
AM
13618_bfd_elf_make_dynamic_reloc_section (asection *sec,
13619 bfd *dynobj,
13620 unsigned int alignment,
13621 bfd *abfd,
13622 bfd_boolean is_rela)
83bac4b0
NC
13623{
13624 asection * reloc_sec = elf_section_data (sec)->sreloc;
13625
13626 if (reloc_sec == NULL)
13627 {
13628 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13629
13630 if (name == NULL)
13631 return NULL;
13632
3d4d4302 13633 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13634
13635 if (reloc_sec == NULL)
13636 {
3d4d4302
AM
13637 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13638 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13639 if ((sec->flags & SEC_ALLOC) != 0)
13640 flags |= SEC_ALLOC | SEC_LOAD;
13641
3d4d4302 13642 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13643 if (reloc_sec != NULL)
13644 {
8877b5e5
AM
13645 /* _bfd_elf_get_sec_type_attr chooses a section type by
13646 name. Override as it may be wrong, eg. for a user
13647 section named "auto" we'll get ".relauto" which is
13648 seen to be a .rela section. */
13649 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13650 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13651 reloc_sec = NULL;
13652 }
13653 }
13654
13655 elf_section_data (sec)->sreloc = reloc_sec;
13656 }
13657
13658 return reloc_sec;
13659}
1338dd10 13660
bffebb6b
AM
13661/* Copy the ELF symbol type and other attributes for a linker script
13662 assignment from HSRC to HDEST. Generally this should be treated as
13663 if we found a strong non-dynamic definition for HDEST (except that
13664 ld ignores multiple definition errors). */
1338dd10 13665void
bffebb6b
AM
13666_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13667 struct bfd_link_hash_entry *hdest,
13668 struct bfd_link_hash_entry *hsrc)
1338dd10 13669{
bffebb6b
AM
13670 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13671 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13672 Elf_Internal_Sym isym;
1338dd10
PB
13673
13674 ehdest->type = ehsrc->type;
35fc36a8 13675 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13676
13677 isym.st_other = ehsrc->other;
b8417128 13678 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13679}
351f65ca
L
13680
13681/* Append a RELA relocation REL to section S in BFD. */
13682
13683void
13684elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13685{
13686 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13687 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13688 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13689 bed->s->swap_reloca_out (abfd, rel, loc);
13690}
13691
13692/* Append a REL relocation REL to section S in BFD. */
13693
13694void
13695elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13696{
13697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13698 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13699 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13700 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13701}