*** empty log message ***
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
64d03ab5 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
24f58f47 3 2005, 2006, 2007, 2008, 2009, 2010, 2011
9dbe8890 4 Free Software Foundation, Inc.
252b5132 5
8fdd7217 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
8fdd7217
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
8fdd7217 11 (at your option) any later version.
252b5132 12
8fdd7217
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
8fdd7217
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
252b5132 22
252b5132 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
252b5132
RH
25#include "bfdlink.h"
26#include "libbfd.h"
27#define ARCH_SIZE 0
28#include "elf-bfd.h"
4ad4eba5 29#include "safe-ctype.h"
ccf2f652 30#include "libiberty.h"
66eb6687 31#include "objalloc.h"
252b5132 32
28caa186
AM
33/* This struct is used to pass information to routines called via
34 elf_link_hash_traverse which must return failure. */
35
36struct elf_info_failed
37{
38 struct bfd_link_info *info;
39 struct bfd_elf_version_tree *verdefs;
40 bfd_boolean failed;
41};
42
43/* This structure is used to pass information to
44 _bfd_elf_link_find_version_dependencies. */
45
46struct elf_find_verdep_info
47{
48 /* General link information. */
49 struct bfd_link_info *info;
50 /* The number of dependencies. */
51 unsigned int vers;
52 /* Whether we had a failure. */
53 bfd_boolean failed;
54};
55
56static bfd_boolean _bfd_elf_fix_symbol_flags
57 (struct elf_link_hash_entry *, struct elf_info_failed *);
58
d98685ac
AM
59/* Define a symbol in a dynamic linkage section. */
60
61struct elf_link_hash_entry *
62_bfd_elf_define_linkage_sym (bfd *abfd,
63 struct bfd_link_info *info,
64 asection *sec,
65 const char *name)
66{
67 struct elf_link_hash_entry *h;
68 struct bfd_link_hash_entry *bh;
ccabcbe5 69 const struct elf_backend_data *bed;
d98685ac
AM
70
71 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
72 if (h != NULL)
73 {
74 /* Zap symbol defined in an as-needed lib that wasn't linked.
75 This is a symptom of a larger problem: Absolute symbols
76 defined in shared libraries can't be overridden, because we
77 lose the link to the bfd which is via the symbol section. */
78 h->root.type = bfd_link_hash_new;
79 }
80
81 bh = &h->root;
82 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
83 sec, 0, NULL, FALSE,
84 get_elf_backend_data (abfd)->collect,
85 &bh))
86 return NULL;
87 h = (struct elf_link_hash_entry *) bh;
88 h->def_regular = 1;
e28df02b 89 h->non_elf = 0;
d98685ac
AM
90 h->type = STT_OBJECT;
91 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
92
ccabcbe5
AM
93 bed = get_elf_backend_data (abfd);
94 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
95 return h;
96}
97
b34976b6 98bfd_boolean
268b6b39 99_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
100{
101 flagword flags;
aad5d350 102 asection *s;
252b5132 103 struct elf_link_hash_entry *h;
9c5bfbb7 104 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 105 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
106
107 /* This function may be called more than once. */
aad5d350
AM
108 s = bfd_get_section_by_name (abfd, ".got");
109 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
b34976b6 110 return TRUE;
252b5132 111
e5a52504 112 flags = bed->dynamic_sec_flags;
252b5132 113
6de2ae4a
L
114 s = bfd_make_section_with_flags (abfd,
115 (bed->rela_plts_and_copies_p
116 ? ".rela.got" : ".rel.got"),
117 (bed->dynamic_sec_flags
118 | SEC_READONLY));
119 if (s == NULL
120 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
121 return FALSE;
122 htab->srelgot = s;
252b5132 123
64e77c6d
L
124 s = bfd_make_section_with_flags (abfd, ".got", flags);
125 if (s == NULL
126 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
127 return FALSE;
128 htab->sgot = s;
129
252b5132
RH
130 if (bed->want_got_plt)
131 {
3496cb2a 132 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
252b5132 133 if (s == NULL
6de2ae4a
L
134 || !bfd_set_section_alignment (abfd, s,
135 bed->s->log_file_align))
b34976b6 136 return FALSE;
6de2ae4a 137 htab->sgotplt = s;
252b5132
RH
138 }
139
64e77c6d
L
140 /* The first bit of the global offset table is the header. */
141 s->size += bed->got_header_size;
142
2517a57f
AM
143 if (bed->want_got_sym)
144 {
145 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
146 (or .got.plt) section. We don't do this in the linker script
147 because we don't want to define the symbol if we are not creating
148 a global offset table. */
6de2ae4a
L
149 h = _bfd_elf_define_linkage_sym (abfd, info, s,
150 "_GLOBAL_OFFSET_TABLE_");
2517a57f 151 elf_hash_table (info)->hgot = h;
d98685ac
AM
152 if (h == NULL)
153 return FALSE;
2517a57f 154 }
252b5132 155
b34976b6 156 return TRUE;
252b5132
RH
157}
158\f
7e9f0867
AM
159/* Create a strtab to hold the dynamic symbol names. */
160static bfd_boolean
161_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
162{
163 struct elf_link_hash_table *hash_table;
164
165 hash_table = elf_hash_table (info);
166 if (hash_table->dynobj == NULL)
167 hash_table->dynobj = abfd;
168
169 if (hash_table->dynstr == NULL)
170 {
171 hash_table->dynstr = _bfd_elf_strtab_init ();
172 if (hash_table->dynstr == NULL)
173 return FALSE;
174 }
175 return TRUE;
176}
177
45d6a902
AM
178/* Create some sections which will be filled in with dynamic linking
179 information. ABFD is an input file which requires dynamic sections
180 to be created. The dynamic sections take up virtual memory space
181 when the final executable is run, so we need to create them before
182 addresses are assigned to the output sections. We work out the
183 actual contents and size of these sections later. */
252b5132 184
b34976b6 185bfd_boolean
268b6b39 186_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 187{
45d6a902 188 flagword flags;
91d6fa6a 189 asection *s;
9c5bfbb7 190 const struct elf_backend_data *bed;
252b5132 191
0eddce27 192 if (! is_elf_hash_table (info->hash))
45d6a902
AM
193 return FALSE;
194
195 if (elf_hash_table (info)->dynamic_sections_created)
196 return TRUE;
197
7e9f0867
AM
198 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
199 return FALSE;
45d6a902 200
7e9f0867 201 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
202 bed = get_elf_backend_data (abfd);
203
204 flags = bed->dynamic_sec_flags;
45d6a902
AM
205
206 /* A dynamically linked executable has a .interp section, but a
207 shared library does not. */
36af4a4e 208 if (info->executable)
252b5132 209 {
3496cb2a
L
210 s = bfd_make_section_with_flags (abfd, ".interp",
211 flags | SEC_READONLY);
212 if (s == NULL)
45d6a902
AM
213 return FALSE;
214 }
bb0deeff 215
45d6a902
AM
216 /* Create sections to hold version informations. These are removed
217 if they are not needed. */
3496cb2a
L
218 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
219 flags | SEC_READONLY);
45d6a902 220 if (s == NULL
45d6a902
AM
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222 return FALSE;
223
3496cb2a
L
224 s = bfd_make_section_with_flags (abfd, ".gnu.version",
225 flags | SEC_READONLY);
45d6a902 226 if (s == NULL
45d6a902
AM
227 || ! bfd_set_section_alignment (abfd, s, 1))
228 return FALSE;
229
3496cb2a
L
230 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
231 flags | SEC_READONLY);
45d6a902 232 if (s == NULL
45d6a902
AM
233 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
234 return FALSE;
235
3496cb2a
L
236 s = bfd_make_section_with_flags (abfd, ".dynsym",
237 flags | SEC_READONLY);
45d6a902 238 if (s == NULL
45d6a902
AM
239 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
240 return FALSE;
241
3496cb2a
L
242 s = bfd_make_section_with_flags (abfd, ".dynstr",
243 flags | SEC_READONLY);
244 if (s == NULL)
45d6a902
AM
245 return FALSE;
246
3496cb2a 247 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
45d6a902 248 if (s == NULL
45d6a902
AM
249 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
250 return FALSE;
251
252 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
253 .dynamic section. We could set _DYNAMIC in a linker script, but we
254 only want to define it if we are, in fact, creating a .dynamic
255 section. We don't want to define it if there is no .dynamic
256 section, since on some ELF platforms the start up code examines it
257 to decide how to initialize the process. */
d98685ac 258 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
45d6a902
AM
259 return FALSE;
260
fdc90cb4
JJ
261 if (info->emit_hash)
262 {
263 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
264 if (s == NULL
265 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
266 return FALSE;
267 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
268 }
269
270 if (info->emit_gnu_hash)
271 {
272 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
273 flags | SEC_READONLY);
274 if (s == NULL
275 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
276 return FALSE;
277 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
278 4 32-bit words followed by variable count of 64-bit words, then
279 variable count of 32-bit words. */
280 if (bed->s->arch_size == 64)
281 elf_section_data (s)->this_hdr.sh_entsize = 0;
282 else
283 elf_section_data (s)->this_hdr.sh_entsize = 4;
284 }
45d6a902
AM
285
286 /* Let the backend create the rest of the sections. This lets the
287 backend set the right flags. The backend will normally create
288 the .got and .plt sections. */
894891db
NC
289 if (bed->elf_backend_create_dynamic_sections == NULL
290 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
291 return FALSE;
292
293 elf_hash_table (info)->dynamic_sections_created = TRUE;
294
295 return TRUE;
296}
297
298/* Create dynamic sections when linking against a dynamic object. */
299
300bfd_boolean
268b6b39 301_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
302{
303 flagword flags, pltflags;
7325306f 304 struct elf_link_hash_entry *h;
45d6a902 305 asection *s;
9c5bfbb7 306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 307 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 308
252b5132
RH
309 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
310 .rel[a].bss sections. */
e5a52504 311 flags = bed->dynamic_sec_flags;
252b5132
RH
312
313 pltflags = flags;
252b5132 314 if (bed->plt_not_loaded)
6df4d94c
MM
315 /* We do not clear SEC_ALLOC here because we still want the OS to
316 allocate space for the section; it's just that there's nothing
317 to read in from the object file. */
5d1634d7 318 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
319 else
320 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
321 if (bed->plt_readonly)
322 pltflags |= SEC_READONLY;
323
3496cb2a 324 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
252b5132 325 if (s == NULL
252b5132 326 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 327 return FALSE;
6de2ae4a 328 htab->splt = s;
252b5132 329
d98685ac
AM
330 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
331 .plt section. */
7325306f
RS
332 if (bed->want_plt_sym)
333 {
334 h = _bfd_elf_define_linkage_sym (abfd, info, s,
335 "_PROCEDURE_LINKAGE_TABLE_");
336 elf_hash_table (info)->hplt = h;
337 if (h == NULL)
338 return FALSE;
339 }
252b5132 340
3496cb2a 341 s = bfd_make_section_with_flags (abfd,
d35fd659 342 (bed->rela_plts_and_copies_p
3496cb2a
L
343 ? ".rela.plt" : ".rel.plt"),
344 flags | SEC_READONLY);
252b5132 345 if (s == NULL
45d6a902 346 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 347 return FALSE;
6de2ae4a 348 htab->srelplt = s;
252b5132
RH
349
350 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 351 return FALSE;
252b5132 352
3018b441
RH
353 if (bed->want_dynbss)
354 {
355 /* The .dynbss section is a place to put symbols which are defined
356 by dynamic objects, are referenced by regular objects, and are
357 not functions. We must allocate space for them in the process
358 image and use a R_*_COPY reloc to tell the dynamic linker to
359 initialize them at run time. The linker script puts the .dynbss
360 section into the .bss section of the final image. */
3496cb2a
L
361 s = bfd_make_section_with_flags (abfd, ".dynbss",
362 (SEC_ALLOC
363 | SEC_LINKER_CREATED));
364 if (s == NULL)
b34976b6 365 return FALSE;
252b5132 366
3018b441 367 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
368 normally needed. We need to create it here, though, so that the
369 linker will map it to an output section. We can't just create it
370 only if we need it, because we will not know whether we need it
371 until we have seen all the input files, and the first time the
372 main linker code calls BFD after examining all the input files
373 (size_dynamic_sections) the input sections have already been
374 mapped to the output sections. If the section turns out not to
375 be needed, we can discard it later. We will never need this
376 section when generating a shared object, since they do not use
377 copy relocs. */
3018b441
RH
378 if (! info->shared)
379 {
3496cb2a 380 s = bfd_make_section_with_flags (abfd,
d35fd659 381 (bed->rela_plts_and_copies_p
3496cb2a
L
382 ? ".rela.bss" : ".rel.bss"),
383 flags | SEC_READONLY);
3018b441 384 if (s == NULL
45d6a902 385 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 386 return FALSE;
3018b441 387 }
252b5132
RH
388 }
389
b34976b6 390 return TRUE;
252b5132
RH
391}
392\f
252b5132
RH
393/* Record a new dynamic symbol. We record the dynamic symbols as we
394 read the input files, since we need to have a list of all of them
395 before we can determine the final sizes of the output sections.
396 Note that we may actually call this function even though we are not
397 going to output any dynamic symbols; in some cases we know that a
398 symbol should be in the dynamic symbol table, but only if there is
399 one. */
400
b34976b6 401bfd_boolean
c152c796
AM
402bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
403 struct elf_link_hash_entry *h)
252b5132
RH
404{
405 if (h->dynindx == -1)
406 {
2b0f7ef9 407 struct elf_strtab_hash *dynstr;
68b6ddd0 408 char *p;
252b5132 409 const char *name;
252b5132
RH
410 bfd_size_type indx;
411
7a13edea
NC
412 /* XXX: The ABI draft says the linker must turn hidden and
413 internal symbols into STB_LOCAL symbols when producing the
414 DSO. However, if ld.so honors st_other in the dynamic table,
415 this would not be necessary. */
416 switch (ELF_ST_VISIBILITY (h->other))
417 {
418 case STV_INTERNAL:
419 case STV_HIDDEN:
9d6eee78
L
420 if (h->root.type != bfd_link_hash_undefined
421 && h->root.type != bfd_link_hash_undefweak)
38048eb9 422 {
f5385ebf 423 h->forced_local = 1;
67687978
PB
424 if (!elf_hash_table (info)->is_relocatable_executable)
425 return TRUE;
7a13edea 426 }
0444bdd4 427
7a13edea
NC
428 default:
429 break;
430 }
431
252b5132
RH
432 h->dynindx = elf_hash_table (info)->dynsymcount;
433 ++elf_hash_table (info)->dynsymcount;
434
435 dynstr = elf_hash_table (info)->dynstr;
436 if (dynstr == NULL)
437 {
438 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 439 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 440 if (dynstr == NULL)
b34976b6 441 return FALSE;
252b5132
RH
442 }
443
444 /* We don't put any version information in the dynamic string
aad5d350 445 table. */
252b5132
RH
446 name = h->root.root.string;
447 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
448 if (p != NULL)
449 /* We know that the p points into writable memory. In fact,
450 there are only a few symbols that have read-only names, being
451 those like _GLOBAL_OFFSET_TABLE_ that are created specially
452 by the backends. Most symbols will have names pointing into
453 an ELF string table read from a file, or to objalloc memory. */
454 *p = 0;
455
456 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
457
458 if (p != NULL)
459 *p = ELF_VER_CHR;
252b5132
RH
460
461 if (indx == (bfd_size_type) -1)
b34976b6 462 return FALSE;
252b5132
RH
463 h->dynstr_index = indx;
464 }
465
b34976b6 466 return TRUE;
252b5132 467}
45d6a902 468\f
55255dae
L
469/* Mark a symbol dynamic. */
470
28caa186 471static void
55255dae 472bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
473 struct elf_link_hash_entry *h,
474 Elf_Internal_Sym *sym)
55255dae 475{
40b36307 476 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 477
40b36307
L
478 /* It may be called more than once on the same H. */
479 if(h->dynamic || info->relocatable)
55255dae
L
480 return;
481
40b36307
L
482 if ((info->dynamic_data
483 && (h->type == STT_OBJECT
484 || (sym != NULL
485 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 486 || (d != NULL
40b36307
L
487 && h->root.type == bfd_link_hash_new
488 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
489 h->dynamic = 1;
490}
491
45d6a902
AM
492/* Record an assignment to a symbol made by a linker script. We need
493 this in case some dynamic object refers to this symbol. */
494
495bfd_boolean
fe21a8fc
L
496bfd_elf_record_link_assignment (bfd *output_bfd,
497 struct bfd_link_info *info,
268b6b39 498 const char *name,
fe21a8fc
L
499 bfd_boolean provide,
500 bfd_boolean hidden)
45d6a902 501{
00cbee0a 502 struct elf_link_hash_entry *h, *hv;
4ea42fb7 503 struct elf_link_hash_table *htab;
00cbee0a 504 const struct elf_backend_data *bed;
45d6a902 505
0eddce27 506 if (!is_elf_hash_table (info->hash))
45d6a902
AM
507 return TRUE;
508
4ea42fb7
AM
509 htab = elf_hash_table (info);
510 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 511 if (h == NULL)
4ea42fb7 512 return provide;
45d6a902 513
00cbee0a 514 switch (h->root.type)
77cfaee6 515 {
00cbee0a
L
516 case bfd_link_hash_defined:
517 case bfd_link_hash_defweak:
518 case bfd_link_hash_common:
519 break;
520 case bfd_link_hash_undefweak:
521 case bfd_link_hash_undefined:
522 /* Since we're defining the symbol, don't let it seem to have not
523 been defined. record_dynamic_symbol and size_dynamic_sections
524 may depend on this. */
4ea42fb7 525 h->root.type = bfd_link_hash_new;
77cfaee6
AM
526 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
527 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
528 break;
529 case bfd_link_hash_new:
40b36307 530 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 531 h->non_elf = 0;
00cbee0a
L
532 break;
533 case bfd_link_hash_indirect:
534 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 535 the versioned symbol point to this one. */
00cbee0a
L
536 bed = get_elf_backend_data (output_bfd);
537 hv = h;
538 while (hv->root.type == bfd_link_hash_indirect
539 || hv->root.type == bfd_link_hash_warning)
540 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
541 /* We don't need to update h->root.u since linker will set them
542 later. */
543 h->root.type = bfd_link_hash_undefined;
544 hv->root.type = bfd_link_hash_indirect;
545 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
546 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
547 break;
548 case bfd_link_hash_warning:
549 abort ();
550 break;
55255dae 551 }
45d6a902
AM
552
553 /* If this symbol is being provided by the linker script, and it is
554 currently defined by a dynamic object, but not by a regular
555 object, then mark it as undefined so that the generic linker will
556 force the correct value. */
557 if (provide
f5385ebf
AM
558 && h->def_dynamic
559 && !h->def_regular)
45d6a902
AM
560 h->root.type = bfd_link_hash_undefined;
561
562 /* If this symbol is not being provided by the linker script, and it is
563 currently defined by a dynamic object, but not by a regular object,
564 then clear out any version information because the symbol will not be
565 associated with the dynamic object any more. */
566 if (!provide
f5385ebf
AM
567 && h->def_dynamic
568 && !h->def_regular)
45d6a902
AM
569 h->verinfo.verdef = NULL;
570
f5385ebf 571 h->def_regular = 1;
45d6a902 572
fe21a8fc
L
573 if (provide && hidden)
574 {
91d6fa6a 575 bed = get_elf_backend_data (output_bfd);
fe21a8fc
L
576 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
577 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
578 }
579
6fa3860b
PB
580 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
581 and executables. */
582 if (!info->relocatable
583 && h->dynindx != -1
584 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
585 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
586 h->forced_local = 1;
587
f5385ebf
AM
588 if ((h->def_dynamic
589 || h->ref_dynamic
67687978
PB
590 || info->shared
591 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
592 && h->dynindx == -1)
593 {
c152c796 594 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
595 return FALSE;
596
597 /* If this is a weak defined symbol, and we know a corresponding
598 real symbol from the same dynamic object, make sure the real
599 symbol is also made into a dynamic symbol. */
f6e332e6
AM
600 if (h->u.weakdef != NULL
601 && h->u.weakdef->dynindx == -1)
45d6a902 602 {
f6e332e6 603 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
604 return FALSE;
605 }
606 }
607
608 return TRUE;
609}
42751cf3 610
8c58d23b
AM
611/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
612 success, and 2 on a failure caused by attempting to record a symbol
613 in a discarded section, eg. a discarded link-once section symbol. */
614
615int
c152c796
AM
616bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
617 bfd *input_bfd,
618 long input_indx)
8c58d23b
AM
619{
620 bfd_size_type amt;
621 struct elf_link_local_dynamic_entry *entry;
622 struct elf_link_hash_table *eht;
623 struct elf_strtab_hash *dynstr;
624 unsigned long dynstr_index;
625 char *name;
626 Elf_External_Sym_Shndx eshndx;
627 char esym[sizeof (Elf64_External_Sym)];
628
0eddce27 629 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
630 return 0;
631
632 /* See if the entry exists already. */
633 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
634 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
635 return 1;
636
637 amt = sizeof (*entry);
a50b1753 638 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
639 if (entry == NULL)
640 return 0;
641
642 /* Go find the symbol, so that we can find it's name. */
643 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 644 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
645 {
646 bfd_release (input_bfd, entry);
647 return 0;
648 }
649
650 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 651 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
652 {
653 asection *s;
654
655 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
656 if (s == NULL || bfd_is_abs_section (s->output_section))
657 {
658 /* We can still bfd_release here as nothing has done another
659 bfd_alloc. We can't do this later in this function. */
660 bfd_release (input_bfd, entry);
661 return 2;
662 }
663 }
664
665 name = (bfd_elf_string_from_elf_section
666 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
667 entry->isym.st_name));
668
669 dynstr = elf_hash_table (info)->dynstr;
670 if (dynstr == NULL)
671 {
672 /* Create a strtab to hold the dynamic symbol names. */
673 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
674 if (dynstr == NULL)
675 return 0;
676 }
677
b34976b6 678 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
679 if (dynstr_index == (unsigned long) -1)
680 return 0;
681 entry->isym.st_name = dynstr_index;
682
683 eht = elf_hash_table (info);
684
685 entry->next = eht->dynlocal;
686 eht->dynlocal = entry;
687 entry->input_bfd = input_bfd;
688 entry->input_indx = input_indx;
689 eht->dynsymcount++;
690
691 /* Whatever binding the symbol had before, it's now local. */
692 entry->isym.st_info
693 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
694
695 /* The dynindx will be set at the end of size_dynamic_sections. */
696
697 return 1;
698}
699
30b30c21 700/* Return the dynindex of a local dynamic symbol. */
42751cf3 701
30b30c21 702long
268b6b39
AM
703_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
704 bfd *input_bfd,
705 long input_indx)
30b30c21
RH
706{
707 struct elf_link_local_dynamic_entry *e;
708
709 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
710 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
711 return e->dynindx;
712 return -1;
713}
714
715/* This function is used to renumber the dynamic symbols, if some of
716 them are removed because they are marked as local. This is called
717 via elf_link_hash_traverse. */
718
b34976b6 719static bfd_boolean
268b6b39
AM
720elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
721 void *data)
42751cf3 722{
a50b1753 723 size_t *count = (size_t *) data;
30b30c21 724
6fa3860b
PB
725 if (h->forced_local)
726 return TRUE;
727
728 if (h->dynindx != -1)
729 h->dynindx = ++(*count);
730
731 return TRUE;
732}
733
734
735/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
736 STB_LOCAL binding. */
737
738static bfd_boolean
739elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
740 void *data)
741{
a50b1753 742 size_t *count = (size_t *) data;
6fa3860b 743
6fa3860b
PB
744 if (!h->forced_local)
745 return TRUE;
746
42751cf3 747 if (h->dynindx != -1)
30b30c21
RH
748 h->dynindx = ++(*count);
749
b34976b6 750 return TRUE;
42751cf3 751}
30b30c21 752
aee6f5b4
AO
753/* Return true if the dynamic symbol for a given section should be
754 omitted when creating a shared library. */
755bfd_boolean
756_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
757 struct bfd_link_info *info,
758 asection *p)
759{
74541ad4
AM
760 struct elf_link_hash_table *htab;
761
aee6f5b4
AO
762 switch (elf_section_data (p)->this_hdr.sh_type)
763 {
764 case SHT_PROGBITS:
765 case SHT_NOBITS:
766 /* If sh_type is yet undecided, assume it could be
767 SHT_PROGBITS/SHT_NOBITS. */
768 case SHT_NULL:
74541ad4
AM
769 htab = elf_hash_table (info);
770 if (p == htab->tls_sec)
771 return FALSE;
772
773 if (htab->text_index_section != NULL)
774 return p != htab->text_index_section && p != htab->data_index_section;
775
aee6f5b4
AO
776 if (strcmp (p->name, ".got") == 0
777 || strcmp (p->name, ".got.plt") == 0
778 || strcmp (p->name, ".plt") == 0)
779 {
780 asection *ip;
aee6f5b4 781
74541ad4
AM
782 if (htab->dynobj != NULL
783 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
aee6f5b4
AO
784 && (ip->flags & SEC_LINKER_CREATED)
785 && ip->output_section == p)
786 return TRUE;
787 }
788 return FALSE;
789
790 /* There shouldn't be section relative relocations
791 against any other section. */
792 default:
793 return TRUE;
794 }
795}
796
062e2358 797/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
798 symbol for each output section, which come first. Next come symbols
799 which have been forced to local binding. Then all of the back-end
800 allocated local dynamic syms, followed by the rest of the global
801 symbols. */
30b30c21 802
554220db
AM
803static unsigned long
804_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
805 struct bfd_link_info *info,
806 unsigned long *section_sym_count)
30b30c21
RH
807{
808 unsigned long dynsymcount = 0;
809
67687978 810 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
30b30c21 811 {
aee6f5b4 812 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
813 asection *p;
814 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 815 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
816 && (p->flags & SEC_ALLOC) != 0
817 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
818 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
819 else
820 elf_section_data (p)->dynindx = 0;
30b30c21 821 }
554220db 822 *section_sym_count = dynsymcount;
30b30c21 823
6fa3860b
PB
824 elf_link_hash_traverse (elf_hash_table (info),
825 elf_link_renumber_local_hash_table_dynsyms,
826 &dynsymcount);
827
30b30c21
RH
828 if (elf_hash_table (info)->dynlocal)
829 {
830 struct elf_link_local_dynamic_entry *p;
831 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
832 p->dynindx = ++dynsymcount;
833 }
834
835 elf_link_hash_traverse (elf_hash_table (info),
836 elf_link_renumber_hash_table_dynsyms,
837 &dynsymcount);
838
839 /* There is an unused NULL entry at the head of the table which
840 we must account for in our count. Unless there weren't any
841 symbols, which means we'll have no table at all. */
842 if (dynsymcount != 0)
843 ++dynsymcount;
844
ccabcbe5
AM
845 elf_hash_table (info)->dynsymcount = dynsymcount;
846 return dynsymcount;
30b30c21 847}
252b5132 848
54ac0771
L
849/* Merge st_other field. */
850
851static void
852elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
853 Elf_Internal_Sym *isym, bfd_boolean definition,
854 bfd_boolean dynamic)
855{
856 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
857
858 /* If st_other has a processor-specific meaning, specific
859 code might be needed here. We never merge the visibility
860 attribute with the one from a dynamic object. */
861 if (bed->elf_backend_merge_symbol_attribute)
862 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
863 dynamic);
864
865 /* If this symbol has default visibility and the user has requested
866 we not re-export it, then mark it as hidden. */
867 if (definition
868 && !dynamic
869 && (abfd->no_export
870 || (abfd->my_archive && abfd->my_archive->no_export))
871 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
872 isym->st_other = (STV_HIDDEN
873 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
874
875 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
876 {
877 unsigned char hvis, symvis, other, nvis;
878
879 /* Only merge the visibility. Leave the remainder of the
880 st_other field to elf_backend_merge_symbol_attribute. */
881 other = h->other & ~ELF_ST_VISIBILITY (-1);
882
883 /* Combine visibilities, using the most constraining one. */
884 hvis = ELF_ST_VISIBILITY (h->other);
885 symvis = ELF_ST_VISIBILITY (isym->st_other);
886 if (! hvis)
887 nvis = symvis;
888 else if (! symvis)
889 nvis = hvis;
890 else
891 nvis = hvis < symvis ? hvis : symvis;
892
893 h->other = other | nvis;
894 }
895}
896
45d6a902
AM
897/* This function is called when we want to define a new symbol. It
898 handles the various cases which arise when we find a definition in
899 a dynamic object, or when there is already a definition in a
900 dynamic object. The new symbol is described by NAME, SYM, PSEC,
901 and PVALUE. We set SYM_HASH to the hash table entry. We set
902 OVERRIDE if the old symbol is overriding a new definition. We set
903 TYPE_CHANGE_OK if it is OK for the type to change. We set
904 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
905 change, we mean that we shouldn't warn if the type or size does
af44c138
L
906 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
907 object is overridden by a regular object. */
45d6a902
AM
908
909bfd_boolean
268b6b39
AM
910_bfd_elf_merge_symbol (bfd *abfd,
911 struct bfd_link_info *info,
912 const char *name,
913 Elf_Internal_Sym *sym,
914 asection **psec,
915 bfd_vma *pvalue,
af44c138 916 unsigned int *pold_alignment,
268b6b39
AM
917 struct elf_link_hash_entry **sym_hash,
918 bfd_boolean *skip,
919 bfd_boolean *override,
920 bfd_boolean *type_change_ok,
0f8a2703 921 bfd_boolean *size_change_ok)
252b5132 922{
7479dfd4 923 asection *sec, *oldsec;
45d6a902
AM
924 struct elf_link_hash_entry *h;
925 struct elf_link_hash_entry *flip;
926 int bind;
927 bfd *oldbfd;
928 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 929 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 930 const struct elf_backend_data *bed;
45d6a902
AM
931
932 *skip = FALSE;
933 *override = FALSE;
934
935 sec = *psec;
936 bind = ELF_ST_BIND (sym->st_info);
937
cd7be95b
KH
938 /* Silently discard TLS symbols from --just-syms. There's no way to
939 combine a static TLS block with a new TLS block for this executable. */
940 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
941 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
942 {
943 *skip = TRUE;
944 return TRUE;
945 }
946
45d6a902
AM
947 if (! bfd_is_und_section (sec))
948 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
949 else
950 h = ((struct elf_link_hash_entry *)
951 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
952 if (h == NULL)
953 return FALSE;
954 *sym_hash = h;
252b5132 955
88ba32a0
L
956 bed = get_elf_backend_data (abfd);
957
45d6a902
AM
958 /* This code is for coping with dynamic objects, and is only useful
959 if we are doing an ELF link. */
88ba32a0 960 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
45d6a902 961 return TRUE;
252b5132 962
45d6a902
AM
963 /* For merging, we only care about real symbols. */
964
965 while (h->root.type == bfd_link_hash_indirect
966 || h->root.type == bfd_link_hash_warning)
967 h = (struct elf_link_hash_entry *) h->root.u.i.link;
968
40b36307
L
969 /* We have to check it for every instance since the first few may be
970 refereences and not all compilers emit symbol type for undefined
971 symbols. */
972 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
973
45d6a902
AM
974 /* If we just created the symbol, mark it as being an ELF symbol.
975 Other than that, there is nothing to do--there is no merge issue
976 with a newly defined symbol--so we just return. */
977
978 if (h->root.type == bfd_link_hash_new)
252b5132 979 {
f5385ebf 980 h->non_elf = 0;
45d6a902
AM
981 return TRUE;
982 }
252b5132 983
7479dfd4
L
984 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
985 existing symbol. */
252b5132 986
45d6a902
AM
987 switch (h->root.type)
988 {
989 default:
990 oldbfd = NULL;
7479dfd4 991 oldsec = NULL;
45d6a902 992 break;
252b5132 993
45d6a902
AM
994 case bfd_link_hash_undefined:
995 case bfd_link_hash_undefweak:
996 oldbfd = h->root.u.undef.abfd;
7479dfd4 997 oldsec = NULL;
45d6a902
AM
998 break;
999
1000 case bfd_link_hash_defined:
1001 case bfd_link_hash_defweak:
1002 oldbfd = h->root.u.def.section->owner;
7479dfd4 1003 oldsec = h->root.u.def.section;
45d6a902
AM
1004 break;
1005
1006 case bfd_link_hash_common:
1007 oldbfd = h->root.u.c.p->section->owner;
7479dfd4 1008 oldsec = h->root.u.c.p->section;
45d6a902
AM
1009 break;
1010 }
1011
895fa45f
MGD
1012 /* Differentiate strong and weak symbols. */
1013 newweak = bind == STB_WEAK;
1014 oldweak = (h->root.type == bfd_link_hash_defweak
1015 || h->root.type == bfd_link_hash_undefweak);
1016
45d6a902
AM
1017 /* In cases involving weak versioned symbols, we may wind up trying
1018 to merge a symbol with itself. Catch that here, to avoid the
1019 confusion that results if we try to override a symbol with
1020 itself. The additional tests catch cases like
1021 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1022 dynamic object, which we do want to handle here. */
1023 if (abfd == oldbfd
895fa45f 1024 && (newweak || oldweak)
45d6a902 1025 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1026 || !h->def_regular))
45d6a902
AM
1027 return TRUE;
1028
1029 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1030 respectively, is from a dynamic object. */
1031
707bba77 1032 newdyn = (abfd->flags & DYNAMIC) != 0;
45d6a902 1033
707bba77 1034 olddyn = FALSE;
45d6a902
AM
1035 if (oldbfd != NULL)
1036 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1037 else if (oldsec != NULL)
45d6a902 1038 {
707bba77 1039 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1040 indices used by MIPS ELF. */
707bba77 1041 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1042 }
252b5132 1043
45d6a902
AM
1044 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1045 respectively, appear to be a definition rather than reference. */
1046
707bba77 1047 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1048
707bba77
AM
1049 olddef = (h->root.type != bfd_link_hash_undefined
1050 && h->root.type != bfd_link_hash_undefweak
1051 && h->root.type != bfd_link_hash_common);
45d6a902 1052
0a36a439
L
1053 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1054 respectively, appear to be a function. */
1055
1056 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1057 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1058
1059 oldfunc = (h->type != STT_NOTYPE
1060 && bed->is_function_type (h->type));
1061
580a2b6e
L
1062 /* When we try to create a default indirect symbol from the dynamic
1063 definition with the default version, we skip it if its type and
1064 the type of existing regular definition mismatch. We only do it
1065 if the existing regular definition won't be dynamic. */
1066 if (pold_alignment == NULL
1067 && !info->shared
1068 && !info->export_dynamic
1069 && !h->ref_dynamic
1070 && newdyn
1071 && newdef
1072 && !olddyn
1073 && (olddef || h->root.type == bfd_link_hash_common)
1074 && ELF_ST_TYPE (sym->st_info) != h->type
1075 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
fcb93ecf 1076 && h->type != STT_NOTYPE
0a36a439 1077 && !(newfunc && oldfunc))
580a2b6e
L
1078 {
1079 *skip = TRUE;
1080 return TRUE;
1081 }
1082
3a5dbfb2
AM
1083 /* Plugin symbol type isn't currently set. Stop bogus errors. */
1084 if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0)
1085 *type_change_ok = TRUE;
1086
68f49ba3
L
1087 /* Check TLS symbol. We don't check undefined symbol introduced by
1088 "ld -u". */
3a5dbfb2
AM
1089 else if (oldbfd != NULL
1090 && ELF_ST_TYPE (sym->st_info) != h->type
1091 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1092 {
1093 bfd *ntbfd, *tbfd;
1094 bfd_boolean ntdef, tdef;
1095 asection *ntsec, *tsec;
1096
1097 if (h->type == STT_TLS)
1098 {
3b36f7e6 1099 ntbfd = abfd;
7479dfd4
L
1100 ntsec = sec;
1101 ntdef = newdef;
1102 tbfd = oldbfd;
1103 tsec = oldsec;
1104 tdef = olddef;
1105 }
1106 else
1107 {
1108 ntbfd = oldbfd;
1109 ntsec = oldsec;
1110 ntdef = olddef;
1111 tbfd = abfd;
1112 tsec = sec;
1113 tdef = newdef;
1114 }
1115
1116 if (tdef && ntdef)
1117 (*_bfd_error_handler)
fc3e1e3c 1118 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1119 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1120 else if (!tdef && !ntdef)
1121 (*_bfd_error_handler)
fc3e1e3c 1122 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
7479dfd4
L
1123 tbfd, ntbfd, h->root.root.string);
1124 else if (tdef)
1125 (*_bfd_error_handler)
fc3e1e3c 1126 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
7479dfd4
L
1127 tbfd, tsec, ntbfd, h->root.root.string);
1128 else
1129 (*_bfd_error_handler)
fc3e1e3c 1130 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1131 tbfd, ntbfd, ntsec, h->root.root.string);
1132
1133 bfd_set_error (bfd_error_bad_value);
1134 return FALSE;
1135 }
1136
4cc11e76 1137 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
1138 object or is weak in all dynamic objects. Internal and hidden
1139 visibility will make it unavailable to dynamic objects. */
f5385ebf 1140 if (newdyn && !h->dynamic_def)
45d6a902
AM
1141 {
1142 if (!bfd_is_und_section (sec))
f5385ebf 1143 h->dynamic_def = 1;
45d6a902 1144 else
252b5132 1145 {
45d6a902
AM
1146 /* Check if this symbol is weak in all dynamic objects. If it
1147 is the first time we see it in a dynamic object, we mark
1148 if it is weak. Otherwise, we clear it. */
f5385ebf 1149 if (!h->ref_dynamic)
79349b09 1150 {
45d6a902 1151 if (bind == STB_WEAK)
f5385ebf 1152 h->dynamic_weak = 1;
252b5132 1153 }
45d6a902 1154 else if (bind != STB_WEAK)
f5385ebf 1155 h->dynamic_weak = 0;
252b5132 1156 }
45d6a902 1157 }
252b5132 1158
45d6a902
AM
1159 /* If the old symbol has non-default visibility, we ignore the new
1160 definition from a dynamic object. */
1161 if (newdyn
9c7a29a3 1162 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1163 && !bfd_is_und_section (sec))
1164 {
1165 *skip = TRUE;
1166 /* Make sure this symbol is dynamic. */
f5385ebf 1167 h->ref_dynamic = 1;
45d6a902
AM
1168 /* A protected symbol has external availability. Make sure it is
1169 recorded as dynamic.
1170
1171 FIXME: Should we check type and size for protected symbol? */
1172 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1173 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1174 else
1175 return TRUE;
1176 }
1177 else if (!newdyn
9c7a29a3 1178 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1179 && h->def_dynamic)
45d6a902
AM
1180 {
1181 /* If the new symbol with non-default visibility comes from a
1182 relocatable file and the old definition comes from a dynamic
1183 object, we remove the old definition. */
1184 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1185 {
1186 /* Handle the case where the old dynamic definition is
1187 default versioned. We need to copy the symbol info from
1188 the symbol with default version to the normal one if it
1189 was referenced before. */
1190 if (h->ref_regular)
1191 {
d2dee3b2 1192 struct elf_link_hash_entry *vh = *sym_hash;
91d6fa6a 1193
d2dee3b2
L
1194 vh->root.type = h->root.type;
1195 h->root.type = bfd_link_hash_indirect;
1196 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1197 /* Protected symbols will override the dynamic definition
1198 with default version. */
1199 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1200 {
1201 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1202 vh->dynamic_def = 1;
1203 vh->ref_dynamic = 1;
1204 }
1205 else
1206 {
1207 h->root.type = vh->root.type;
1208 vh->ref_dynamic = 0;
1209 /* We have to hide it here since it was made dynamic
1210 global with extra bits when the symbol info was
1211 copied from the old dynamic definition. */
1212 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1213 }
1214 h = vh;
1215 }
1216 else
1217 h = *sym_hash;
1218 }
1de1a317 1219
f6e332e6 1220 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317
L
1221 && bfd_is_und_section (sec))
1222 {
1223 /* If the new symbol is undefined and the old symbol was
1224 also undefined before, we need to make sure
1225 _bfd_generic_link_add_one_symbol doesn't mess
f6e332e6 1226 up the linker hash table undefs list. Since the old
1de1a317
L
1227 definition came from a dynamic object, it is still on the
1228 undefs list. */
1229 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1230 h->root.u.undef.abfd = abfd;
1231 }
1232 else
1233 {
1234 h->root.type = bfd_link_hash_new;
1235 h->root.u.undef.abfd = NULL;
1236 }
1237
f5385ebf 1238 if (h->def_dynamic)
252b5132 1239 {
f5385ebf
AM
1240 h->def_dynamic = 0;
1241 h->ref_dynamic = 1;
1242 h->dynamic_def = 1;
45d6a902
AM
1243 }
1244 /* FIXME: Should we check type and size for protected symbol? */
1245 h->size = 0;
1246 h->type = 0;
1247 return TRUE;
1248 }
14a793b2 1249
3e7a7d11
NC
1250 if (bind == STB_GNU_UNIQUE)
1251 h->unique_global = 1;
1252
15b43f48
AM
1253 /* If a new weak symbol definition comes from a regular file and the
1254 old symbol comes from a dynamic library, we treat the new one as
1255 strong. Similarly, an old weak symbol definition from a regular
1256 file is treated as strong when the new symbol comes from a dynamic
1257 library. Further, an old weak symbol from a dynamic library is
1258 treated as strong if the new symbol is from a dynamic library.
1259 This reflects the way glibc's ld.so works.
1260
1261 Do this before setting *type_change_ok or *size_change_ok so that
1262 we warn properly when dynamic library symbols are overridden. */
1263
1264 if (newdef && !newdyn && olddyn)
0f8a2703 1265 newweak = FALSE;
15b43f48 1266 if (olddef && newdyn)
0f8a2703
AM
1267 oldweak = FALSE;
1268
d334575b 1269 /* Allow changes between different types of function symbol. */
0a36a439 1270 if (newfunc && oldfunc)
fcb93ecf
PB
1271 *type_change_ok = TRUE;
1272
79349b09
AM
1273 /* It's OK to change the type if either the existing symbol or the
1274 new symbol is weak. A type change is also OK if the old symbol
1275 is undefined and the new symbol is defined. */
252b5132 1276
79349b09
AM
1277 if (oldweak
1278 || newweak
1279 || (newdef
1280 && h->root.type == bfd_link_hash_undefined))
1281 *type_change_ok = TRUE;
1282
1283 /* It's OK to change the size if either the existing symbol or the
1284 new symbol is weak, or if the old symbol is undefined. */
1285
1286 if (*type_change_ok
1287 || h->root.type == bfd_link_hash_undefined)
1288 *size_change_ok = TRUE;
45d6a902 1289
45d6a902
AM
1290 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1291 symbol, respectively, appears to be a common symbol in a dynamic
1292 object. If a symbol appears in an uninitialized section, and is
1293 not weak, and is not a function, then it may be a common symbol
1294 which was resolved when the dynamic object was created. We want
1295 to treat such symbols specially, because they raise special
1296 considerations when setting the symbol size: if the symbol
1297 appears as a common symbol in a regular object, and the size in
1298 the regular object is larger, we must make sure that we use the
1299 larger size. This problematic case can always be avoided in C,
1300 but it must be handled correctly when using Fortran shared
1301 libraries.
1302
1303 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1304 likewise for OLDDYNCOMMON and OLDDEF.
1305
1306 Note that this test is just a heuristic, and that it is quite
1307 possible to have an uninitialized symbol in a shared object which
1308 is really a definition, rather than a common symbol. This could
1309 lead to some minor confusion when the symbol really is a common
1310 symbol in some regular object. However, I think it will be
1311 harmless. */
1312
1313 if (newdyn
1314 && newdef
79349b09 1315 && !newweak
45d6a902
AM
1316 && (sec->flags & SEC_ALLOC) != 0
1317 && (sec->flags & SEC_LOAD) == 0
1318 && sym->st_size > 0
0a36a439 1319 && !newfunc)
45d6a902
AM
1320 newdyncommon = TRUE;
1321 else
1322 newdyncommon = FALSE;
1323
1324 if (olddyn
1325 && olddef
1326 && h->root.type == bfd_link_hash_defined
f5385ebf 1327 && h->def_dynamic
45d6a902
AM
1328 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1329 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1330 && h->size > 0
0a36a439 1331 && !oldfunc)
45d6a902
AM
1332 olddyncommon = TRUE;
1333 else
1334 olddyncommon = FALSE;
1335
a4d8e49b
L
1336 /* We now know everything about the old and new symbols. We ask the
1337 backend to check if we can merge them. */
a4d8e49b
L
1338 if (bed->merge_symbol
1339 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1340 pold_alignment, skip, override,
1341 type_change_ok, size_change_ok,
1342 &newdyn, &newdef, &newdyncommon, &newweak,
1343 abfd, &sec,
1344 &olddyn, &olddef, &olddyncommon, &oldweak,
1345 oldbfd, &oldsec))
1346 return FALSE;
1347
45d6a902
AM
1348 /* If both the old and the new symbols look like common symbols in a
1349 dynamic object, set the size of the symbol to the larger of the
1350 two. */
1351
1352 if (olddyncommon
1353 && newdyncommon
1354 && sym->st_size != h->size)
1355 {
1356 /* Since we think we have two common symbols, issue a multiple
1357 common warning if desired. Note that we only warn if the
1358 size is different. If the size is the same, we simply let
1359 the old symbol override the new one as normally happens with
1360 symbols defined in dynamic objects. */
1361
1362 if (! ((*info->callbacks->multiple_common)
24f58f47 1363 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1364 return FALSE;
252b5132 1365
45d6a902
AM
1366 if (sym->st_size > h->size)
1367 h->size = sym->st_size;
252b5132 1368
45d6a902 1369 *size_change_ok = TRUE;
252b5132
RH
1370 }
1371
45d6a902
AM
1372 /* If we are looking at a dynamic object, and we have found a
1373 definition, we need to see if the symbol was already defined by
1374 some other object. If so, we want to use the existing
1375 definition, and we do not want to report a multiple symbol
1376 definition error; we do this by clobbering *PSEC to be
1377 bfd_und_section_ptr.
1378
1379 We treat a common symbol as a definition if the symbol in the
1380 shared library is a function, since common symbols always
1381 represent variables; this can cause confusion in principle, but
1382 any such confusion would seem to indicate an erroneous program or
1383 shared library. We also permit a common symbol in a regular
79349b09 1384 object to override a weak symbol in a shared object. */
45d6a902
AM
1385
1386 if (newdyn
1387 && newdef
77cfaee6 1388 && (olddef
45d6a902 1389 || (h->root.type == bfd_link_hash_common
0a36a439 1390 && (newweak || newfunc))))
45d6a902
AM
1391 {
1392 *override = TRUE;
1393 newdef = FALSE;
1394 newdyncommon = FALSE;
252b5132 1395
45d6a902
AM
1396 *psec = sec = bfd_und_section_ptr;
1397 *size_change_ok = TRUE;
252b5132 1398
45d6a902
AM
1399 /* If we get here when the old symbol is a common symbol, then
1400 we are explicitly letting it override a weak symbol or
1401 function in a dynamic object, and we don't want to warn about
1402 a type change. If the old symbol is a defined symbol, a type
1403 change warning may still be appropriate. */
252b5132 1404
45d6a902
AM
1405 if (h->root.type == bfd_link_hash_common)
1406 *type_change_ok = TRUE;
1407 }
1408
1409 /* Handle the special case of an old common symbol merging with a
1410 new symbol which looks like a common symbol in a shared object.
1411 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1412 common symbol, and let _bfd_generic_link_add_one_symbol do the
1413 right thing. */
45d6a902
AM
1414
1415 if (newdyncommon
1416 && h->root.type == bfd_link_hash_common)
1417 {
1418 *override = TRUE;
1419 newdef = FALSE;
1420 newdyncommon = FALSE;
1421 *pvalue = sym->st_size;
a4d8e49b 1422 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1423 *size_change_ok = TRUE;
1424 }
1425
c5e2cead 1426 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1427 if (newdef && olddef && newweak)
54ac0771 1428 {
35ed3f94 1429 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1430 if (!(oldbfd != NULL
1431 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94
AM
1432 && (abfd->flags & BFD_PLUGIN) == 0))
1433 *skip = TRUE;
54ac0771
L
1434
1435 /* Merge st_other. If the symbol already has a dynamic index,
1436 but visibility says it should not be visible, turn it into a
1437 local symbol. */
1438 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1439 if (h->dynindx != -1)
1440 switch (ELF_ST_VISIBILITY (h->other))
1441 {
1442 case STV_INTERNAL:
1443 case STV_HIDDEN:
1444 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1445 break;
1446 }
1447 }
c5e2cead 1448
45d6a902
AM
1449 /* If the old symbol is from a dynamic object, and the new symbol is
1450 a definition which is not from a dynamic object, then the new
1451 symbol overrides the old symbol. Symbols from regular files
1452 always take precedence over symbols from dynamic objects, even if
1453 they are defined after the dynamic object in the link.
1454
1455 As above, we again permit a common symbol in a regular object to
1456 override a definition in a shared object if the shared object
0f8a2703 1457 symbol is a function or is weak. */
45d6a902
AM
1458
1459 flip = NULL;
77cfaee6 1460 if (!newdyn
45d6a902
AM
1461 && (newdef
1462 || (bfd_is_com_section (sec)
0a36a439 1463 && (oldweak || oldfunc)))
45d6a902
AM
1464 && olddyn
1465 && olddef
f5385ebf 1466 && h->def_dynamic)
45d6a902
AM
1467 {
1468 /* Change the hash table entry to undefined, and let
1469 _bfd_generic_link_add_one_symbol do the right thing with the
1470 new definition. */
1471
1472 h->root.type = bfd_link_hash_undefined;
1473 h->root.u.undef.abfd = h->root.u.def.section->owner;
1474 *size_change_ok = TRUE;
1475
1476 olddef = FALSE;
1477 olddyncommon = FALSE;
1478
1479 /* We again permit a type change when a common symbol may be
1480 overriding a function. */
1481
1482 if (bfd_is_com_section (sec))
0a36a439
L
1483 {
1484 if (oldfunc)
1485 {
1486 /* If a common symbol overrides a function, make sure
1487 that it isn't defined dynamically nor has type
1488 function. */
1489 h->def_dynamic = 0;
1490 h->type = STT_NOTYPE;
1491 }
1492 *type_change_ok = TRUE;
1493 }
45d6a902
AM
1494
1495 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1496 flip = *sym_hash;
1497 else
1498 /* This union may have been set to be non-NULL when this symbol
1499 was seen in a dynamic object. We must force the union to be
1500 NULL, so that it is correct for a regular symbol. */
1501 h->verinfo.vertree = NULL;
1502 }
1503
1504 /* Handle the special case of a new common symbol merging with an
1505 old symbol that looks like it might be a common symbol defined in
1506 a shared object. Note that we have already handled the case in
1507 which a new common symbol should simply override the definition
1508 in the shared library. */
1509
1510 if (! newdyn
1511 && bfd_is_com_section (sec)
1512 && olddyncommon)
1513 {
1514 /* It would be best if we could set the hash table entry to a
1515 common symbol, but we don't know what to use for the section
1516 or the alignment. */
1517 if (! ((*info->callbacks->multiple_common)
24f58f47 1518 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1519 return FALSE;
1520
4cc11e76 1521 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1522 larger, pretend that the new symbol has its size. */
1523
1524 if (h->size > *pvalue)
1525 *pvalue = h->size;
1526
af44c138
L
1527 /* We need to remember the alignment required by the symbol
1528 in the dynamic object. */
1529 BFD_ASSERT (pold_alignment);
1530 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1531
1532 olddef = FALSE;
1533 olddyncommon = FALSE;
1534
1535 h->root.type = bfd_link_hash_undefined;
1536 h->root.u.undef.abfd = h->root.u.def.section->owner;
1537
1538 *size_change_ok = TRUE;
1539 *type_change_ok = TRUE;
1540
1541 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1542 flip = *sym_hash;
1543 else
1544 h->verinfo.vertree = NULL;
1545 }
1546
1547 if (flip != NULL)
1548 {
1549 /* Handle the case where we had a versioned symbol in a dynamic
1550 library and now find a definition in a normal object. In this
1551 case, we make the versioned symbol point to the normal one. */
45d6a902 1552 flip->root.type = h->root.type;
00cbee0a 1553 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1554 h->root.type = bfd_link_hash_indirect;
1555 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1556 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1557 if (h->def_dynamic)
45d6a902 1558 {
f5385ebf
AM
1559 h->def_dynamic = 0;
1560 flip->ref_dynamic = 1;
45d6a902
AM
1561 }
1562 }
1563
45d6a902
AM
1564 return TRUE;
1565}
1566
1567/* This function is called to create an indirect symbol from the
1568 default for the symbol with the default version if needed. The
1569 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
0f8a2703 1570 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1571
28caa186 1572static bfd_boolean
268b6b39
AM
1573_bfd_elf_add_default_symbol (bfd *abfd,
1574 struct bfd_link_info *info,
1575 struct elf_link_hash_entry *h,
1576 const char *name,
1577 Elf_Internal_Sym *sym,
1578 asection **psec,
1579 bfd_vma *value,
1580 bfd_boolean *dynsym,
0f8a2703 1581 bfd_boolean override)
45d6a902
AM
1582{
1583 bfd_boolean type_change_ok;
1584 bfd_boolean size_change_ok;
1585 bfd_boolean skip;
1586 char *shortname;
1587 struct elf_link_hash_entry *hi;
1588 struct bfd_link_hash_entry *bh;
9c5bfbb7 1589 const struct elf_backend_data *bed;
45d6a902
AM
1590 bfd_boolean collect;
1591 bfd_boolean dynamic;
1592 char *p;
1593 size_t len, shortlen;
1594 asection *sec;
1595
1596 /* If this symbol has a version, and it is the default version, we
1597 create an indirect symbol from the default name to the fully
1598 decorated name. This will cause external references which do not
1599 specify a version to be bound to this version of the symbol. */
1600 p = strchr (name, ELF_VER_CHR);
1601 if (p == NULL || p[1] != ELF_VER_CHR)
1602 return TRUE;
1603
1604 if (override)
1605 {
4cc11e76 1606 /* We are overridden by an old definition. We need to check if we
45d6a902
AM
1607 need to create the indirect symbol from the default name. */
1608 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1609 FALSE, FALSE);
1610 BFD_ASSERT (hi != NULL);
1611 if (hi == h)
1612 return TRUE;
1613 while (hi->root.type == bfd_link_hash_indirect
1614 || hi->root.type == bfd_link_hash_warning)
1615 {
1616 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1617 if (hi == h)
1618 return TRUE;
1619 }
1620 }
1621
1622 bed = get_elf_backend_data (abfd);
1623 collect = bed->collect;
1624 dynamic = (abfd->flags & DYNAMIC) != 0;
1625
1626 shortlen = p - name;
a50b1753 1627 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1628 if (shortname == NULL)
1629 return FALSE;
1630 memcpy (shortname, name, shortlen);
1631 shortname[shortlen] = '\0';
1632
1633 /* We are going to create a new symbol. Merge it with any existing
1634 symbol with this name. For the purposes of the merge, act as
1635 though we were defining the symbol we just defined, although we
1636 actually going to define an indirect symbol. */
1637 type_change_ok = FALSE;
1638 size_change_ok = FALSE;
1639 sec = *psec;
1640 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1641 NULL, &hi, &skip, &override,
1642 &type_change_ok, &size_change_ok))
45d6a902
AM
1643 return FALSE;
1644
1645 if (skip)
1646 goto nondefault;
1647
1648 if (! override)
1649 {
1650 bh = &hi->root;
1651 if (! (_bfd_generic_link_add_one_symbol
1652 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1653 0, name, FALSE, collect, &bh)))
45d6a902
AM
1654 return FALSE;
1655 hi = (struct elf_link_hash_entry *) bh;
1656 }
1657 else
1658 {
1659 /* In this case the symbol named SHORTNAME is overriding the
1660 indirect symbol we want to add. We were planning on making
1661 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1662 is the name without a version. NAME is the fully versioned
1663 name, and it is the default version.
1664
1665 Overriding means that we already saw a definition for the
1666 symbol SHORTNAME in a regular object, and it is overriding
1667 the symbol defined in the dynamic object.
1668
1669 When this happens, we actually want to change NAME, the
1670 symbol we just added, to refer to SHORTNAME. This will cause
1671 references to NAME in the shared object to become references
1672 to SHORTNAME in the regular object. This is what we expect
1673 when we override a function in a shared object: that the
1674 references in the shared object will be mapped to the
1675 definition in the regular object. */
1676
1677 while (hi->root.type == bfd_link_hash_indirect
1678 || hi->root.type == bfd_link_hash_warning)
1679 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1680
1681 h->root.type = bfd_link_hash_indirect;
1682 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1683 if (h->def_dynamic)
45d6a902 1684 {
f5385ebf
AM
1685 h->def_dynamic = 0;
1686 hi->ref_dynamic = 1;
1687 if (hi->ref_regular
1688 || hi->def_regular)
45d6a902 1689 {
c152c796 1690 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1691 return FALSE;
1692 }
1693 }
1694
1695 /* Now set HI to H, so that the following code will set the
1696 other fields correctly. */
1697 hi = h;
1698 }
1699
fab4a87f
L
1700 /* Check if HI is a warning symbol. */
1701 if (hi->root.type == bfd_link_hash_warning)
1702 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1703
45d6a902
AM
1704 /* If there is a duplicate definition somewhere, then HI may not
1705 point to an indirect symbol. We will have reported an error to
1706 the user in that case. */
1707
1708 if (hi->root.type == bfd_link_hash_indirect)
1709 {
1710 struct elf_link_hash_entry *ht;
1711
45d6a902 1712 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1713 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902
AM
1714
1715 /* See if the new flags lead us to realize that the symbol must
1716 be dynamic. */
1717 if (! *dynsym)
1718 {
1719 if (! dynamic)
1720 {
ca4a656b 1721 if (! info->executable
f5385ebf 1722 || hi->ref_dynamic)
45d6a902
AM
1723 *dynsym = TRUE;
1724 }
1725 else
1726 {
f5385ebf 1727 if (hi->ref_regular)
45d6a902
AM
1728 *dynsym = TRUE;
1729 }
1730 }
1731 }
1732
1733 /* We also need to define an indirection from the nondefault version
1734 of the symbol. */
1735
1736nondefault:
1737 len = strlen (name);
a50b1753 1738 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1739 if (shortname == NULL)
1740 return FALSE;
1741 memcpy (shortname, name, shortlen);
1742 memcpy (shortname + shortlen, p + 1, len - shortlen);
1743
1744 /* Once again, merge with any existing symbol. */
1745 type_change_ok = FALSE;
1746 size_change_ok = FALSE;
1747 sec = *psec;
1748 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1749 NULL, &hi, &skip, &override,
1750 &type_change_ok, &size_change_ok))
45d6a902
AM
1751 return FALSE;
1752
1753 if (skip)
1754 return TRUE;
1755
1756 if (override)
1757 {
1758 /* Here SHORTNAME is a versioned name, so we don't expect to see
1759 the type of override we do in the case above unless it is
4cc11e76 1760 overridden by a versioned definition. */
45d6a902
AM
1761 if (hi->root.type != bfd_link_hash_defined
1762 && hi->root.type != bfd_link_hash_defweak)
1763 (*_bfd_error_handler)
d003868e
AM
1764 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1765 abfd, shortname);
45d6a902
AM
1766 }
1767 else
1768 {
1769 bh = &hi->root;
1770 if (! (_bfd_generic_link_add_one_symbol
1771 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1772 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1773 return FALSE;
1774 hi = (struct elf_link_hash_entry *) bh;
1775
1776 /* If there is a duplicate definition somewhere, then HI may not
1777 point to an indirect symbol. We will have reported an error
1778 to the user in that case. */
1779
1780 if (hi->root.type == bfd_link_hash_indirect)
1781 {
fcfa13d2 1782 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
45d6a902
AM
1783
1784 /* See if the new flags lead us to realize that the symbol
1785 must be dynamic. */
1786 if (! *dynsym)
1787 {
1788 if (! dynamic)
1789 {
ca4a656b 1790 if (! info->executable
f5385ebf 1791 || hi->ref_dynamic)
45d6a902
AM
1792 *dynsym = TRUE;
1793 }
1794 else
1795 {
f5385ebf 1796 if (hi->ref_regular)
45d6a902
AM
1797 *dynsym = TRUE;
1798 }
1799 }
1800 }
1801 }
1802
1803 return TRUE;
1804}
1805\f
1806/* This routine is used to export all defined symbols into the dynamic
1807 symbol table. It is called via elf_link_hash_traverse. */
1808
28caa186 1809static bfd_boolean
268b6b39 1810_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1811{
a50b1753 1812 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1813
1814 /* Ignore indirect symbols. These are added by the versioning code. */
1815 if (h->root.type == bfd_link_hash_indirect)
1816 return TRUE;
1817
7686d77d
AM
1818 /* Ignore this if we won't export it. */
1819 if (!eif->info->export_dynamic && !h->dynamic)
1820 return TRUE;
45d6a902
AM
1821
1822 if (h->dynindx == -1
f5385ebf
AM
1823 && (h->def_regular
1824 || h->ref_regular))
45d6a902 1825 {
1e8fa21e 1826 bfd_boolean hide;
45d6a902 1827
1e8fa21e 1828 if (eif->verdefs == NULL
09e2aba4 1829 || (bfd_find_version_for_sym (eif->verdefs, h->root.root.string, &hide)
1e8fa21e 1830 && !hide))
45d6a902 1831 {
c152c796 1832 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
1833 {
1834 eif->failed = TRUE;
1835 return FALSE;
1836 }
1837 }
1838 }
1839
1840 return TRUE;
1841}
1842\f
1843/* Look through the symbols which are defined in other shared
1844 libraries and referenced here. Update the list of version
1845 dependencies. This will be put into the .gnu.version_r section.
1846 This function is called via elf_link_hash_traverse. */
1847
28caa186 1848static bfd_boolean
268b6b39
AM
1849_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1850 void *data)
45d6a902 1851{
a50b1753 1852 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1853 Elf_Internal_Verneed *t;
1854 Elf_Internal_Vernaux *a;
1855 bfd_size_type amt;
1856
45d6a902
AM
1857 /* We only care about symbols defined in shared objects with version
1858 information. */
f5385ebf
AM
1859 if (!h->def_dynamic
1860 || h->def_regular
45d6a902
AM
1861 || h->dynindx == -1
1862 || h->verinfo.verdef == NULL)
1863 return TRUE;
1864
1865 /* See if we already know about this version. */
28caa186
AM
1866 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1867 t != NULL;
1868 t = t->vn_nextref)
45d6a902
AM
1869 {
1870 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1871 continue;
1872
1873 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1874 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1875 return TRUE;
1876
1877 break;
1878 }
1879
1880 /* This is a new version. Add it to tree we are building. */
1881
1882 if (t == NULL)
1883 {
1884 amt = sizeof *t;
a50b1753 1885 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
1886 if (t == NULL)
1887 {
1888 rinfo->failed = TRUE;
1889 return FALSE;
1890 }
1891
1892 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
1893 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1894 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
1895 }
1896
1897 amt = sizeof *a;
a50b1753 1898 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
1899 if (a == NULL)
1900 {
1901 rinfo->failed = TRUE;
1902 return FALSE;
1903 }
45d6a902
AM
1904
1905 /* Note that we are copying a string pointer here, and testing it
1906 above. If bfd_elf_string_from_elf_section is ever changed to
1907 discard the string data when low in memory, this will have to be
1908 fixed. */
1909 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1910
1911 a->vna_flags = h->verinfo.verdef->vd_flags;
1912 a->vna_nextptr = t->vn_auxptr;
1913
1914 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1915 ++rinfo->vers;
1916
1917 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1918
1919 t->vn_auxptr = a;
1920
1921 return TRUE;
1922}
1923
1924/* Figure out appropriate versions for all the symbols. We may not
1925 have the version number script until we have read all of the input
1926 files, so until that point we don't know which symbols should be
1927 local. This function is called via elf_link_hash_traverse. */
1928
28caa186 1929static bfd_boolean
268b6b39 1930_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 1931{
28caa186 1932 struct elf_info_failed *sinfo;
45d6a902 1933 struct bfd_link_info *info;
9c5bfbb7 1934 const struct elf_backend_data *bed;
45d6a902
AM
1935 struct elf_info_failed eif;
1936 char *p;
1937 bfd_size_type amt;
1938
a50b1753 1939 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
1940 info = sinfo->info;
1941
45d6a902
AM
1942 /* Fix the symbol flags. */
1943 eif.failed = FALSE;
1944 eif.info = info;
1945 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1946 {
1947 if (eif.failed)
1948 sinfo->failed = TRUE;
1949 return FALSE;
1950 }
1951
1952 /* We only need version numbers for symbols defined in regular
1953 objects. */
f5385ebf 1954 if (!h->def_regular)
45d6a902
AM
1955 return TRUE;
1956
28caa186 1957 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
1958 p = strchr (h->root.root.string, ELF_VER_CHR);
1959 if (p != NULL && h->verinfo.vertree == NULL)
1960 {
1961 struct bfd_elf_version_tree *t;
1962 bfd_boolean hidden;
1963
1964 hidden = TRUE;
1965
1966 /* There are two consecutive ELF_VER_CHR characters if this is
1967 not a hidden symbol. */
1968 ++p;
1969 if (*p == ELF_VER_CHR)
1970 {
1971 hidden = FALSE;
1972 ++p;
1973 }
1974
1975 /* If there is no version string, we can just return out. */
1976 if (*p == '\0')
1977 {
1978 if (hidden)
f5385ebf 1979 h->hidden = 1;
45d6a902
AM
1980 return TRUE;
1981 }
1982
1983 /* Look for the version. If we find it, it is no longer weak. */
1984 for (t = sinfo->verdefs; t != NULL; t = t->next)
1985 {
1986 if (strcmp (t->name, p) == 0)
1987 {
1988 size_t len;
1989 char *alc;
1990 struct bfd_elf_version_expr *d;
1991
1992 len = p - h->root.root.string;
a50b1753 1993 alc = (char *) bfd_malloc (len);
45d6a902 1994 if (alc == NULL)
14b1c01e
AM
1995 {
1996 sinfo->failed = TRUE;
1997 return FALSE;
1998 }
45d6a902
AM
1999 memcpy (alc, h->root.root.string, len - 1);
2000 alc[len - 1] = '\0';
2001 if (alc[len - 2] == ELF_VER_CHR)
2002 alc[len - 2] = '\0';
2003
2004 h->verinfo.vertree = t;
2005 t->used = TRUE;
2006 d = NULL;
2007
108ba305
JJ
2008 if (t->globals.list != NULL)
2009 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2010
2011 /* See if there is anything to force this symbol to
2012 local scope. */
108ba305 2013 if (d == NULL && t->locals.list != NULL)
45d6a902 2014 {
108ba305
JJ
2015 d = (*t->match) (&t->locals, NULL, alc);
2016 if (d != NULL
2017 && h->dynindx != -1
108ba305
JJ
2018 && ! info->export_dynamic)
2019 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2020 }
2021
2022 free (alc);
2023 break;
2024 }
2025 }
2026
2027 /* If we are building an application, we need to create a
2028 version node for this version. */
36af4a4e 2029 if (t == NULL && info->executable)
45d6a902
AM
2030 {
2031 struct bfd_elf_version_tree **pp;
2032 int version_index;
2033
2034 /* If we aren't going to export this symbol, we don't need
2035 to worry about it. */
2036 if (h->dynindx == -1)
2037 return TRUE;
2038
2039 amt = sizeof *t;
a50b1753 2040 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2041 if (t == NULL)
2042 {
2043 sinfo->failed = TRUE;
2044 return FALSE;
2045 }
2046
45d6a902 2047 t->name = p;
45d6a902
AM
2048 t->name_indx = (unsigned int) -1;
2049 t->used = TRUE;
2050
2051 version_index = 1;
2052 /* Don't count anonymous version tag. */
2053 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
2054 version_index = 0;
2055 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2056 ++version_index;
2057 t->vernum = version_index;
2058
2059 *pp = t;
2060
2061 h->verinfo.vertree = t;
2062 }
2063 else if (t == NULL)
2064 {
2065 /* We could not find the version for a symbol when
2066 generating a shared archive. Return an error. */
2067 (*_bfd_error_handler)
c55fe096 2068 (_("%B: version node not found for symbol %s"),
28caa186 2069 info->output_bfd, h->root.root.string);
45d6a902
AM
2070 bfd_set_error (bfd_error_bad_value);
2071 sinfo->failed = TRUE;
2072 return FALSE;
2073 }
2074
2075 if (hidden)
f5385ebf 2076 h->hidden = 1;
45d6a902
AM
2077 }
2078
2079 /* If we don't have a version for this symbol, see if we can find
2080 something. */
2081 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2082 {
1e8fa21e 2083 bfd_boolean hide;
ae5a3597 2084
09e2aba4 2085 h->verinfo.vertree = bfd_find_version_for_sym (sinfo->verdefs,
1e8fa21e
AM
2086 h->root.root.string, &hide);
2087 if (h->verinfo.vertree != NULL && hide)
2088 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2089 }
2090
2091 return TRUE;
2092}
2093\f
45d6a902
AM
2094/* Read and swap the relocs from the section indicated by SHDR. This
2095 may be either a REL or a RELA section. The relocations are
2096 translated into RELA relocations and stored in INTERNAL_RELOCS,
2097 which should have already been allocated to contain enough space.
2098 The EXTERNAL_RELOCS are a buffer where the external form of the
2099 relocations should be stored.
2100
2101 Returns FALSE if something goes wrong. */
2102
2103static bfd_boolean
268b6b39 2104elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2105 asection *sec,
268b6b39
AM
2106 Elf_Internal_Shdr *shdr,
2107 void *external_relocs,
2108 Elf_Internal_Rela *internal_relocs)
45d6a902 2109{
9c5bfbb7 2110 const struct elf_backend_data *bed;
268b6b39 2111 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2112 const bfd_byte *erela;
2113 const bfd_byte *erelaend;
2114 Elf_Internal_Rela *irela;
243ef1e0
L
2115 Elf_Internal_Shdr *symtab_hdr;
2116 size_t nsyms;
45d6a902 2117
45d6a902
AM
2118 /* Position ourselves at the start of the section. */
2119 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2120 return FALSE;
2121
2122 /* Read the relocations. */
2123 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2124 return FALSE;
2125
243ef1e0 2126 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2127 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2128
45d6a902
AM
2129 bed = get_elf_backend_data (abfd);
2130
2131 /* Convert the external relocations to the internal format. */
2132 if (shdr->sh_entsize == bed->s->sizeof_rel)
2133 swap_in = bed->s->swap_reloc_in;
2134 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2135 swap_in = bed->s->swap_reloca_in;
2136 else
2137 {
2138 bfd_set_error (bfd_error_wrong_format);
2139 return FALSE;
2140 }
2141
a50b1753 2142 erela = (const bfd_byte *) external_relocs;
51992aec 2143 erelaend = erela + shdr->sh_size;
45d6a902
AM
2144 irela = internal_relocs;
2145 while (erela < erelaend)
2146 {
243ef1e0
L
2147 bfd_vma r_symndx;
2148
45d6a902 2149 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2150 r_symndx = ELF32_R_SYM (irela->r_info);
2151 if (bed->s->arch_size == 64)
2152 r_symndx >>= 24;
ce98a316
NC
2153 if (nsyms > 0)
2154 {
2155 if ((size_t) r_symndx >= nsyms)
2156 {
2157 (*_bfd_error_handler)
2158 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2159 " for offset 0x%lx in section `%A'"),
2160 abfd, sec,
2161 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2162 bfd_set_error (bfd_error_bad_value);
2163 return FALSE;
2164 }
2165 }
cf35638d 2166 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2167 {
2168 (*_bfd_error_handler)
ce98a316
NC
2169 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2170 " when the object file has no symbol table"),
d003868e
AM
2171 abfd, sec,
2172 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2173 bfd_set_error (bfd_error_bad_value);
2174 return FALSE;
2175 }
45d6a902
AM
2176 irela += bed->s->int_rels_per_ext_rel;
2177 erela += shdr->sh_entsize;
2178 }
2179
2180 return TRUE;
2181}
2182
2183/* Read and swap the relocs for a section O. They may have been
2184 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2185 not NULL, they are used as buffers to read into. They are known to
2186 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2187 the return value is allocated using either malloc or bfd_alloc,
2188 according to the KEEP_MEMORY argument. If O has two relocation
2189 sections (both REL and RELA relocations), then the REL_HDR
2190 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2191 RELA_HDR relocations. */
45d6a902
AM
2192
2193Elf_Internal_Rela *
268b6b39
AM
2194_bfd_elf_link_read_relocs (bfd *abfd,
2195 asection *o,
2196 void *external_relocs,
2197 Elf_Internal_Rela *internal_relocs,
2198 bfd_boolean keep_memory)
45d6a902 2199{
268b6b39 2200 void *alloc1 = NULL;
45d6a902 2201 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2203 struct bfd_elf_section_data *esdo = elf_section_data (o);
2204 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2205
d4730f92
BS
2206 if (esdo->relocs != NULL)
2207 return esdo->relocs;
45d6a902
AM
2208
2209 if (o->reloc_count == 0)
2210 return NULL;
2211
45d6a902
AM
2212 if (internal_relocs == NULL)
2213 {
2214 bfd_size_type size;
2215
2216 size = o->reloc_count;
2217 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2218 if (keep_memory)
a50b1753 2219 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2220 else
a50b1753 2221 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2222 if (internal_relocs == NULL)
2223 goto error_return;
2224 }
2225
2226 if (external_relocs == NULL)
2227 {
d4730f92
BS
2228 bfd_size_type size = 0;
2229
2230 if (esdo->rel.hdr)
2231 size += esdo->rel.hdr->sh_size;
2232 if (esdo->rela.hdr)
2233 size += esdo->rela.hdr->sh_size;
45d6a902 2234
268b6b39 2235 alloc1 = bfd_malloc (size);
45d6a902
AM
2236 if (alloc1 == NULL)
2237 goto error_return;
2238 external_relocs = alloc1;
2239 }
2240
d4730f92
BS
2241 internal_rela_relocs = internal_relocs;
2242 if (esdo->rel.hdr)
2243 {
2244 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2245 external_relocs,
2246 internal_relocs))
2247 goto error_return;
2248 external_relocs = (((bfd_byte *) external_relocs)
2249 + esdo->rel.hdr->sh_size);
2250 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2251 * bed->s->int_rels_per_ext_rel);
2252 }
2253
2254 if (esdo->rela.hdr
2255 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2256 external_relocs,
2257 internal_rela_relocs)))
45d6a902
AM
2258 goto error_return;
2259
2260 /* Cache the results for next time, if we can. */
2261 if (keep_memory)
d4730f92 2262 esdo->relocs = internal_relocs;
45d6a902
AM
2263
2264 if (alloc1 != NULL)
2265 free (alloc1);
2266
2267 /* Don't free alloc2, since if it was allocated we are passing it
2268 back (under the name of internal_relocs). */
2269
2270 return internal_relocs;
2271
2272 error_return:
2273 if (alloc1 != NULL)
2274 free (alloc1);
2275 if (alloc2 != NULL)
4dd07732
AM
2276 {
2277 if (keep_memory)
2278 bfd_release (abfd, alloc2);
2279 else
2280 free (alloc2);
2281 }
45d6a902
AM
2282 return NULL;
2283}
2284
2285/* Compute the size of, and allocate space for, REL_HDR which is the
2286 section header for a section containing relocations for O. */
2287
28caa186 2288static bfd_boolean
268b6b39 2289_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2290 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2291{
d4730f92 2292 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2293
2294 /* That allows us to calculate the size of the section. */
d4730f92 2295 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2296
2297 /* The contents field must last into write_object_contents, so we
2298 allocate it with bfd_alloc rather than malloc. Also since we
2299 cannot be sure that the contents will actually be filled in,
2300 we zero the allocated space. */
a50b1753 2301 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2302 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2303 return FALSE;
2304
d4730f92 2305 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2306 {
2307 struct elf_link_hash_entry **p;
2308
a50b1753 2309 p = (struct elf_link_hash_entry **)
d4730f92 2310 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2311 if (p == NULL)
2312 return FALSE;
2313
d4730f92 2314 reldata->hashes = p;
45d6a902
AM
2315 }
2316
2317 return TRUE;
2318}
2319
2320/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2321 originated from the section given by INPUT_REL_HDR) to the
2322 OUTPUT_BFD. */
2323
2324bfd_boolean
268b6b39
AM
2325_bfd_elf_link_output_relocs (bfd *output_bfd,
2326 asection *input_section,
2327 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2328 Elf_Internal_Rela *internal_relocs,
2329 struct elf_link_hash_entry **rel_hash
2330 ATTRIBUTE_UNUSED)
45d6a902
AM
2331{
2332 Elf_Internal_Rela *irela;
2333 Elf_Internal_Rela *irelaend;
2334 bfd_byte *erel;
d4730f92 2335 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2336 asection *output_section;
9c5bfbb7 2337 const struct elf_backend_data *bed;
268b6b39 2338 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2339 struct bfd_elf_section_data *esdo;
45d6a902
AM
2340
2341 output_section = input_section->output_section;
45d6a902 2342
d4730f92
BS
2343 bed = get_elf_backend_data (output_bfd);
2344 esdo = elf_section_data (output_section);
2345 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2346 {
d4730f92
BS
2347 output_reldata = &esdo->rel;
2348 swap_out = bed->s->swap_reloc_out;
45d6a902 2349 }
d4730f92
BS
2350 else if (esdo->rela.hdr
2351 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2352 {
d4730f92
BS
2353 output_reldata = &esdo->rela;
2354 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2355 }
2356 else
2357 {
2358 (*_bfd_error_handler)
d003868e
AM
2359 (_("%B: relocation size mismatch in %B section %A"),
2360 output_bfd, input_section->owner, input_section);
297d8443 2361 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2362 return FALSE;
2363 }
2364
d4730f92
BS
2365 erel = output_reldata->hdr->contents;
2366 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2367 irela = internal_relocs;
2368 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2369 * bed->s->int_rels_per_ext_rel);
2370 while (irela < irelaend)
2371 {
2372 (*swap_out) (output_bfd, irela, erel);
2373 irela += bed->s->int_rels_per_ext_rel;
2374 erel += input_rel_hdr->sh_entsize;
2375 }
2376
2377 /* Bump the counter, so that we know where to add the next set of
2378 relocations. */
d4730f92 2379 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2380
2381 return TRUE;
2382}
2383\f
508c3946
L
2384/* Make weak undefined symbols in PIE dynamic. */
2385
2386bfd_boolean
2387_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2388 struct elf_link_hash_entry *h)
2389{
2390 if (info->pie
2391 && h->dynindx == -1
2392 && h->root.type == bfd_link_hash_undefweak)
2393 return bfd_elf_link_record_dynamic_symbol (info, h);
2394
2395 return TRUE;
2396}
2397
45d6a902
AM
2398/* Fix up the flags for a symbol. This handles various cases which
2399 can only be fixed after all the input files are seen. This is
2400 currently called by both adjust_dynamic_symbol and
2401 assign_sym_version, which is unnecessary but perhaps more robust in
2402 the face of future changes. */
2403
28caa186 2404static bfd_boolean
268b6b39
AM
2405_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2406 struct elf_info_failed *eif)
45d6a902 2407{
33774f08 2408 const struct elf_backend_data *bed;
508c3946 2409
45d6a902
AM
2410 /* If this symbol was mentioned in a non-ELF file, try to set
2411 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2412 permit a non-ELF file to correctly refer to a symbol defined in
2413 an ELF dynamic object. */
f5385ebf 2414 if (h->non_elf)
45d6a902
AM
2415 {
2416 while (h->root.type == bfd_link_hash_indirect)
2417 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2418
2419 if (h->root.type != bfd_link_hash_defined
2420 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2421 {
2422 h->ref_regular = 1;
2423 h->ref_regular_nonweak = 1;
2424 }
45d6a902
AM
2425 else
2426 {
2427 if (h->root.u.def.section->owner != NULL
2428 && (bfd_get_flavour (h->root.u.def.section->owner)
2429 == bfd_target_elf_flavour))
f5385ebf
AM
2430 {
2431 h->ref_regular = 1;
2432 h->ref_regular_nonweak = 1;
2433 }
45d6a902 2434 else
f5385ebf 2435 h->def_regular = 1;
45d6a902
AM
2436 }
2437
2438 if (h->dynindx == -1
f5385ebf
AM
2439 && (h->def_dynamic
2440 || h->ref_dynamic))
45d6a902 2441 {
c152c796 2442 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2443 {
2444 eif->failed = TRUE;
2445 return FALSE;
2446 }
2447 }
2448 }
2449 else
2450 {
f5385ebf 2451 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2452 was first seen in a non-ELF file. Fortunately, if the symbol
2453 was first seen in an ELF file, we're probably OK unless the
2454 symbol was defined in a non-ELF file. Catch that case here.
2455 FIXME: We're still in trouble if the symbol was first seen in
2456 a dynamic object, and then later in a non-ELF regular object. */
2457 if ((h->root.type == bfd_link_hash_defined
2458 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2459 && !h->def_regular
45d6a902
AM
2460 && (h->root.u.def.section->owner != NULL
2461 ? (bfd_get_flavour (h->root.u.def.section->owner)
2462 != bfd_target_elf_flavour)
2463 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2464 && !h->def_dynamic)))
2465 h->def_regular = 1;
45d6a902
AM
2466 }
2467
508c3946 2468 /* Backend specific symbol fixup. */
33774f08
AM
2469 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2470 if (bed->elf_backend_fixup_symbol
2471 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2472 return FALSE;
508c3946 2473
45d6a902
AM
2474 /* If this is a final link, and the symbol was defined as a common
2475 symbol in a regular object file, and there was no definition in
2476 any dynamic object, then the linker will have allocated space for
f5385ebf 2477 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2478 flag will not have been set. */
2479 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2480 && !h->def_regular
2481 && h->ref_regular
2482 && !h->def_dynamic
45d6a902 2483 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
f5385ebf 2484 h->def_regular = 1;
45d6a902
AM
2485
2486 /* If -Bsymbolic was used (which means to bind references to global
2487 symbols to the definition within the shared object), and this
2488 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2489 need a PLT entry. Likewise, if the symbol has non-default
2490 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2491 will force it local. */
f5385ebf 2492 if (h->needs_plt
45d6a902 2493 && eif->info->shared
0eddce27 2494 && is_elf_hash_table (eif->info->hash)
55255dae 2495 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2496 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2497 && h->def_regular)
45d6a902 2498 {
45d6a902
AM
2499 bfd_boolean force_local;
2500
45d6a902
AM
2501 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2502 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2503 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2504 }
2505
2506 /* If a weak undefined symbol has non-default visibility, we also
2507 hide it from the dynamic linker. */
9c7a29a3 2508 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2509 && h->root.type == bfd_link_hash_undefweak)
33774f08 2510 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2511
2512 /* If this is a weak defined symbol in a dynamic object, and we know
2513 the real definition in the dynamic object, copy interesting flags
2514 over to the real definition. */
f6e332e6 2515 if (h->u.weakdef != NULL)
45d6a902
AM
2516 {
2517 struct elf_link_hash_entry *weakdef;
2518
f6e332e6 2519 weakdef = h->u.weakdef;
ec24dc88 2520 while (h->root.type == bfd_link_hash_indirect)
45d6a902
AM
2521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2522
2523 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2524 || h->root.type == bfd_link_hash_defweak);
f5385ebf 2525 BFD_ASSERT (weakdef->def_dynamic);
45d6a902
AM
2526
2527 /* If the real definition is defined by a regular object file,
2528 don't do anything special. See the longer description in
2529 _bfd_elf_adjust_dynamic_symbol, below. */
f5385ebf 2530 if (weakdef->def_regular)
f6e332e6 2531 h->u.weakdef = NULL;
45d6a902 2532 else
a26587ba
RS
2533 {
2534 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2535 || weakdef->root.type == bfd_link_hash_defweak);
2536 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2537 }
45d6a902
AM
2538 }
2539
2540 return TRUE;
2541}
2542
2543/* Make the backend pick a good value for a dynamic symbol. This is
2544 called via elf_link_hash_traverse, and also calls itself
2545 recursively. */
2546
28caa186 2547static bfd_boolean
268b6b39 2548_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2549{
a50b1753 2550 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2551 bfd *dynobj;
9c5bfbb7 2552 const struct elf_backend_data *bed;
45d6a902 2553
0eddce27 2554 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2555 return FALSE;
2556
45d6a902
AM
2557 /* Ignore indirect symbols. These are added by the versioning code. */
2558 if (h->root.type == bfd_link_hash_indirect)
2559 return TRUE;
2560
2561 /* Fix the symbol flags. */
2562 if (! _bfd_elf_fix_symbol_flags (h, eif))
2563 return FALSE;
2564
2565 /* If this symbol does not require a PLT entry, and it is not
2566 defined by a dynamic object, or is not referenced by a regular
2567 object, ignore it. We do have to handle a weak defined symbol,
2568 even if no regular object refers to it, if we decided to add it
2569 to the dynamic symbol table. FIXME: Do we normally need to worry
2570 about symbols which are defined by one dynamic object and
2571 referenced by another one? */
f5385ebf 2572 if (!h->needs_plt
91e21fb7 2573 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2574 && (h->def_regular
2575 || !h->def_dynamic
2576 || (!h->ref_regular
f6e332e6 2577 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2578 {
a6aa5195 2579 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2580 return TRUE;
2581 }
2582
2583 /* If we've already adjusted this symbol, don't do it again. This
2584 can happen via a recursive call. */
f5385ebf 2585 if (h->dynamic_adjusted)
45d6a902
AM
2586 return TRUE;
2587
2588 /* Don't look at this symbol again. Note that we must set this
2589 after checking the above conditions, because we may look at a
2590 symbol once, decide not to do anything, and then get called
2591 recursively later after REF_REGULAR is set below. */
f5385ebf 2592 h->dynamic_adjusted = 1;
45d6a902
AM
2593
2594 /* If this is a weak definition, and we know a real definition, and
2595 the real symbol is not itself defined by a regular object file,
2596 then get a good value for the real definition. We handle the
2597 real symbol first, for the convenience of the backend routine.
2598
2599 Note that there is a confusing case here. If the real definition
2600 is defined by a regular object file, we don't get the real symbol
2601 from the dynamic object, but we do get the weak symbol. If the
2602 processor backend uses a COPY reloc, then if some routine in the
2603 dynamic object changes the real symbol, we will not see that
2604 change in the corresponding weak symbol. This is the way other
2605 ELF linkers work as well, and seems to be a result of the shared
2606 library model.
2607
2608 I will clarify this issue. Most SVR4 shared libraries define the
2609 variable _timezone and define timezone as a weak synonym. The
2610 tzset call changes _timezone. If you write
2611 extern int timezone;
2612 int _timezone = 5;
2613 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2614 you might expect that, since timezone is a synonym for _timezone,
2615 the same number will print both times. However, if the processor
2616 backend uses a COPY reloc, then actually timezone will be copied
2617 into your process image, and, since you define _timezone
2618 yourself, _timezone will not. Thus timezone and _timezone will
2619 wind up at different memory locations. The tzset call will set
2620 _timezone, leaving timezone unchanged. */
2621
f6e332e6 2622 if (h->u.weakdef != NULL)
45d6a902 2623 {
ec24dc88
AM
2624 /* If we get to this point, there is an implicit reference to
2625 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2626 h->u.weakdef->ref_regular = 1;
45d6a902 2627
ec24dc88
AM
2628 /* Ensure that the backend adjust_dynamic_symbol function sees
2629 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2630 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2631 return FALSE;
2632 }
2633
2634 /* If a symbol has no type and no size and does not require a PLT
2635 entry, then we are probably about to do the wrong thing here: we
2636 are probably going to create a COPY reloc for an empty object.
2637 This case can arise when a shared object is built with assembly
2638 code, and the assembly code fails to set the symbol type. */
2639 if (h->size == 0
2640 && h->type == STT_NOTYPE
f5385ebf 2641 && !h->needs_plt)
45d6a902
AM
2642 (*_bfd_error_handler)
2643 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2644 h->root.root.string);
2645
2646 dynobj = elf_hash_table (eif->info)->dynobj;
2647 bed = get_elf_backend_data (dynobj);
e7c33416 2648
45d6a902
AM
2649 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2650 {
2651 eif->failed = TRUE;
2652 return FALSE;
2653 }
2654
2655 return TRUE;
2656}
2657
027297b7
L
2658/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2659 DYNBSS. */
2660
2661bfd_boolean
2662_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2663 asection *dynbss)
2664{
91ac5911 2665 unsigned int power_of_two;
027297b7
L
2666 bfd_vma mask;
2667 asection *sec = h->root.u.def.section;
2668
2669 /* The section aligment of definition is the maximum alignment
91ac5911
L
2670 requirement of symbols defined in the section. Since we don't
2671 know the symbol alignment requirement, we start with the
2672 maximum alignment and check low bits of the symbol address
2673 for the minimum alignment. */
2674 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2675 mask = ((bfd_vma) 1 << power_of_two) - 1;
2676 while ((h->root.u.def.value & mask) != 0)
2677 {
2678 mask >>= 1;
2679 --power_of_two;
2680 }
027297b7 2681
91ac5911
L
2682 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2683 dynbss))
027297b7
L
2684 {
2685 /* Adjust the section alignment if needed. */
2686 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2687 power_of_two))
027297b7
L
2688 return FALSE;
2689 }
2690
91ac5911 2691 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2692 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2693
2694 /* Define the symbol as being at this point in DYNBSS. */
2695 h->root.u.def.section = dynbss;
2696 h->root.u.def.value = dynbss->size;
2697
2698 /* Increment the size of DYNBSS to make room for the symbol. */
2699 dynbss->size += h->size;
2700
2701 return TRUE;
2702}
2703
45d6a902
AM
2704/* Adjust all external symbols pointing into SEC_MERGE sections
2705 to reflect the object merging within the sections. */
2706
28caa186 2707static bfd_boolean
268b6b39 2708_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2709{
2710 asection *sec;
2711
45d6a902
AM
2712 if ((h->root.type == bfd_link_hash_defined
2713 || h->root.type == bfd_link_hash_defweak)
2714 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2715 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2716 {
a50b1753 2717 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2718
2719 h->root.u.def.value =
2720 _bfd_merged_section_offset (output_bfd,
2721 &h->root.u.def.section,
2722 elf_section_data (sec)->sec_info,
753731ee 2723 h->root.u.def.value);
45d6a902
AM
2724 }
2725
2726 return TRUE;
2727}
986a241f
RH
2728
2729/* Returns false if the symbol referred to by H should be considered
2730 to resolve local to the current module, and true if it should be
2731 considered to bind dynamically. */
2732
2733bfd_boolean
268b6b39
AM
2734_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2735 struct bfd_link_info *info,
89a2ee5a 2736 bfd_boolean not_local_protected)
986a241f
RH
2737{
2738 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2739 const struct elf_backend_data *bed;
2740 struct elf_link_hash_table *hash_table;
986a241f
RH
2741
2742 if (h == NULL)
2743 return FALSE;
2744
2745 while (h->root.type == bfd_link_hash_indirect
2746 || h->root.type == bfd_link_hash_warning)
2747 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2748
2749 /* If it was forced local, then clearly it's not dynamic. */
2750 if (h->dynindx == -1)
2751 return FALSE;
f5385ebf 2752 if (h->forced_local)
986a241f
RH
2753 return FALSE;
2754
2755 /* Identify the cases where name binding rules say that a
2756 visible symbol resolves locally. */
55255dae 2757 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
986a241f
RH
2758
2759 switch (ELF_ST_VISIBILITY (h->other))
2760 {
2761 case STV_INTERNAL:
2762 case STV_HIDDEN:
2763 return FALSE;
2764
2765 case STV_PROTECTED:
fcb93ecf
PB
2766 hash_table = elf_hash_table (info);
2767 if (!is_elf_hash_table (hash_table))
2768 return FALSE;
2769
2770 bed = get_elf_backend_data (hash_table->dynobj);
2771
986a241f
RH
2772 /* Proper resolution for function pointer equality may require
2773 that these symbols perhaps be resolved dynamically, even though
2774 we should be resolving them to the current module. */
89a2ee5a 2775 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2776 binding_stays_local_p = TRUE;
2777 break;
2778
2779 default:
986a241f
RH
2780 break;
2781 }
2782
aa37626c 2783 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2784 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2785 return TRUE;
2786
986a241f
RH
2787 /* Otherwise, the symbol is dynamic if binding rules don't tell
2788 us that it remains local. */
2789 return !binding_stays_local_p;
2790}
f6c52c13
AM
2791
2792/* Return true if the symbol referred to by H should be considered
2793 to resolve local to the current module, and false otherwise. Differs
2794 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2795 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2796 for the place where forced_local and dynindx == -1 are tested. If
2797 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2798 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2799 the symbol is local only for defined symbols.
2800 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2801 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2802 treatment of undefined weak symbols. For those that do not make
2803 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2804
2805bfd_boolean
268b6b39
AM
2806_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2807 struct bfd_link_info *info,
2808 bfd_boolean local_protected)
f6c52c13 2809{
fcb93ecf
PB
2810 const struct elf_backend_data *bed;
2811 struct elf_link_hash_table *hash_table;
2812
f6c52c13
AM
2813 /* If it's a local sym, of course we resolve locally. */
2814 if (h == NULL)
2815 return TRUE;
2816
d95edcac
L
2817 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2818 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2819 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2820 return TRUE;
2821
7e2294f9
AO
2822 /* Common symbols that become definitions don't get the DEF_REGULAR
2823 flag set, so test it first, and don't bail out. */
2824 if (ELF_COMMON_DEF_P (h))
2825 /* Do nothing. */;
f6c52c13 2826 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2827 resolve locally. The sym is either undefined or dynamic. */
2828 else if (!h->def_regular)
f6c52c13
AM
2829 return FALSE;
2830
2831 /* Forced local symbols resolve locally. */
f5385ebf 2832 if (h->forced_local)
f6c52c13
AM
2833 return TRUE;
2834
2835 /* As do non-dynamic symbols. */
2836 if (h->dynindx == -1)
2837 return TRUE;
2838
2839 /* At this point, we know the symbol is defined and dynamic. In an
2840 executable it must resolve locally, likewise when building symbolic
2841 shared libraries. */
55255dae 2842 if (info->executable || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2843 return TRUE;
2844
2845 /* Now deal with defined dynamic symbols in shared libraries. Ones
2846 with default visibility might not resolve locally. */
2847 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2848 return FALSE;
2849
fcb93ecf
PB
2850 hash_table = elf_hash_table (info);
2851 if (!is_elf_hash_table (hash_table))
2852 return TRUE;
2853
2854 bed = get_elf_backend_data (hash_table->dynobj);
2855
1c16dfa5 2856 /* STV_PROTECTED non-function symbols are local. */
fcb93ecf 2857 if (!bed->is_function_type (h->type))
1c16dfa5
L
2858 return TRUE;
2859
f6c52c13 2860 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2861 symbols be treated as dynamic symbols. If the address of a
2862 function not defined in an executable is set to that function's
2863 plt entry in the executable, then the address of the function in
2864 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2865 return local_protected;
2866}
e1918d23
AM
2867
2868/* Caches some TLS segment info, and ensures that the TLS segment vma is
2869 aligned. Returns the first TLS output section. */
2870
2871struct bfd_section *
2872_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2873{
2874 struct bfd_section *sec, *tls;
2875 unsigned int align = 0;
2876
2877 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2878 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2879 break;
2880 tls = sec;
2881
2882 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2883 if (sec->alignment_power > align)
2884 align = sec->alignment_power;
2885
2886 elf_hash_table (info)->tls_sec = tls;
2887
2888 /* Ensure the alignment of the first section is the largest alignment,
2889 so that the tls segment starts aligned. */
2890 if (tls != NULL)
2891 tls->alignment_power = align;
2892
2893 return tls;
2894}
0ad989f9
L
2895
2896/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2897static bfd_boolean
2898is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2899 Elf_Internal_Sym *sym)
2900{
a4d8e49b
L
2901 const struct elf_backend_data *bed;
2902
0ad989f9
L
2903 /* Local symbols do not count, but target specific ones might. */
2904 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2905 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2906 return FALSE;
2907
fcb93ecf 2908 bed = get_elf_backend_data (abfd);
0ad989f9 2909 /* Function symbols do not count. */
fcb93ecf 2910 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
2911 return FALSE;
2912
2913 /* If the section is undefined, then so is the symbol. */
2914 if (sym->st_shndx == SHN_UNDEF)
2915 return FALSE;
2916
2917 /* If the symbol is defined in the common section, then
2918 it is a common definition and so does not count. */
a4d8e49b 2919 if (bed->common_definition (sym))
0ad989f9
L
2920 return FALSE;
2921
2922 /* If the symbol is in a target specific section then we
2923 must rely upon the backend to tell us what it is. */
2924 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2925 /* FIXME - this function is not coded yet:
2926
2927 return _bfd_is_global_symbol_definition (abfd, sym);
2928
2929 Instead for now assume that the definition is not global,
2930 Even if this is wrong, at least the linker will behave
2931 in the same way that it used to do. */
2932 return FALSE;
2933
2934 return TRUE;
2935}
2936
2937/* Search the symbol table of the archive element of the archive ABFD
2938 whose archive map contains a mention of SYMDEF, and determine if
2939 the symbol is defined in this element. */
2940static bfd_boolean
2941elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2942{
2943 Elf_Internal_Shdr * hdr;
2944 bfd_size_type symcount;
2945 bfd_size_type extsymcount;
2946 bfd_size_type extsymoff;
2947 Elf_Internal_Sym *isymbuf;
2948 Elf_Internal_Sym *isym;
2949 Elf_Internal_Sym *isymend;
2950 bfd_boolean result;
2951
2952 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2953 if (abfd == NULL)
2954 return FALSE;
2955
2956 if (! bfd_check_format (abfd, bfd_object))
2957 return FALSE;
2958
2959 /* If we have already included the element containing this symbol in the
2960 link then we do not need to include it again. Just claim that any symbol
2961 it contains is not a definition, so that our caller will not decide to
2962 (re)include this element. */
2963 if (abfd->archive_pass)
2964 return FALSE;
2965
2966 /* Select the appropriate symbol table. */
2967 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2968 hdr = &elf_tdata (abfd)->symtab_hdr;
2969 else
2970 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2971
2972 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2973
2974 /* The sh_info field of the symtab header tells us where the
2975 external symbols start. We don't care about the local symbols. */
2976 if (elf_bad_symtab (abfd))
2977 {
2978 extsymcount = symcount;
2979 extsymoff = 0;
2980 }
2981 else
2982 {
2983 extsymcount = symcount - hdr->sh_info;
2984 extsymoff = hdr->sh_info;
2985 }
2986
2987 if (extsymcount == 0)
2988 return FALSE;
2989
2990 /* Read in the symbol table. */
2991 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2992 NULL, NULL, NULL);
2993 if (isymbuf == NULL)
2994 return FALSE;
2995
2996 /* Scan the symbol table looking for SYMDEF. */
2997 result = FALSE;
2998 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2999 {
3000 const char *name;
3001
3002 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3003 isym->st_name);
3004 if (name == NULL)
3005 break;
3006
3007 if (strcmp (name, symdef->name) == 0)
3008 {
3009 result = is_global_data_symbol_definition (abfd, isym);
3010 break;
3011 }
3012 }
3013
3014 free (isymbuf);
3015
3016 return result;
3017}
3018\f
5a580b3a
AM
3019/* Add an entry to the .dynamic table. */
3020
3021bfd_boolean
3022_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3023 bfd_vma tag,
3024 bfd_vma val)
3025{
3026 struct elf_link_hash_table *hash_table;
3027 const struct elf_backend_data *bed;
3028 asection *s;
3029 bfd_size_type newsize;
3030 bfd_byte *newcontents;
3031 Elf_Internal_Dyn dyn;
3032
3033 hash_table = elf_hash_table (info);
3034 if (! is_elf_hash_table (hash_table))
3035 return FALSE;
3036
3037 bed = get_elf_backend_data (hash_table->dynobj);
3038 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3039 BFD_ASSERT (s != NULL);
3040
eea6121a 3041 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3042 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3043 if (newcontents == NULL)
3044 return FALSE;
3045
3046 dyn.d_tag = tag;
3047 dyn.d_un.d_val = val;
eea6121a 3048 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3049
eea6121a 3050 s->size = newsize;
5a580b3a
AM
3051 s->contents = newcontents;
3052
3053 return TRUE;
3054}
3055
3056/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3057 otherwise just check whether one already exists. Returns -1 on error,
3058 1 if a DT_NEEDED tag already exists, and 0 on success. */
3059
4ad4eba5 3060static int
7e9f0867
AM
3061elf_add_dt_needed_tag (bfd *abfd,
3062 struct bfd_link_info *info,
4ad4eba5
AM
3063 const char *soname,
3064 bfd_boolean do_it)
5a580b3a
AM
3065{
3066 struct elf_link_hash_table *hash_table;
3067 bfd_size_type oldsize;
3068 bfd_size_type strindex;
3069
7e9f0867
AM
3070 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3071 return -1;
3072
5a580b3a
AM
3073 hash_table = elf_hash_table (info);
3074 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3075 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3076 if (strindex == (bfd_size_type) -1)
3077 return -1;
3078
3079 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3080 {
3081 asection *sdyn;
3082 const struct elf_backend_data *bed;
3083 bfd_byte *extdyn;
3084
3085 bed = get_elf_backend_data (hash_table->dynobj);
3086 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3087 if (sdyn != NULL)
3088 for (extdyn = sdyn->contents;
3089 extdyn < sdyn->contents + sdyn->size;
3090 extdyn += bed->s->sizeof_dyn)
3091 {
3092 Elf_Internal_Dyn dyn;
5a580b3a 3093
7e9f0867
AM
3094 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3095 if (dyn.d_tag == DT_NEEDED
3096 && dyn.d_un.d_val == strindex)
3097 {
3098 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3099 return 1;
3100 }
3101 }
5a580b3a
AM
3102 }
3103
3104 if (do_it)
3105 {
7e9f0867
AM
3106 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3107 return -1;
3108
5a580b3a
AM
3109 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3110 return -1;
3111 }
3112 else
3113 /* We were just checking for existence of the tag. */
3114 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3115
3116 return 0;
3117}
3118
010e5ae2
AM
3119static bfd_boolean
3120on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3121{
3122 for (; needed != NULL; needed = needed->next)
3123 if (strcmp (soname, needed->name) == 0)
3124 return TRUE;
3125
3126 return FALSE;
3127}
3128
5a580b3a 3129/* Sort symbol by value and section. */
4ad4eba5
AM
3130static int
3131elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3132{
3133 const struct elf_link_hash_entry *h1;
3134 const struct elf_link_hash_entry *h2;
10b7e05b 3135 bfd_signed_vma vdiff;
5a580b3a
AM
3136
3137 h1 = *(const struct elf_link_hash_entry **) arg1;
3138 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3139 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3140 if (vdiff != 0)
3141 return vdiff > 0 ? 1 : -1;
3142 else
3143 {
3144 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3145 if (sdiff != 0)
3146 return sdiff > 0 ? 1 : -1;
3147 }
5a580b3a
AM
3148 return 0;
3149}
4ad4eba5 3150
5a580b3a
AM
3151/* This function is used to adjust offsets into .dynstr for
3152 dynamic symbols. This is called via elf_link_hash_traverse. */
3153
3154static bfd_boolean
3155elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3156{
a50b1753 3157 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3158
5a580b3a
AM
3159 if (h->dynindx != -1)
3160 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3161 return TRUE;
3162}
3163
3164/* Assign string offsets in .dynstr, update all structures referencing
3165 them. */
3166
4ad4eba5
AM
3167static bfd_boolean
3168elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3169{
3170 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3171 struct elf_link_local_dynamic_entry *entry;
3172 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3173 bfd *dynobj = hash_table->dynobj;
3174 asection *sdyn;
3175 bfd_size_type size;
3176 const struct elf_backend_data *bed;
3177 bfd_byte *extdyn;
3178
3179 _bfd_elf_strtab_finalize (dynstr);
3180 size = _bfd_elf_strtab_size (dynstr);
3181
3182 bed = get_elf_backend_data (dynobj);
3183 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3184 BFD_ASSERT (sdyn != NULL);
3185
3186 /* Update all .dynamic entries referencing .dynstr strings. */
3187 for (extdyn = sdyn->contents;
eea6121a 3188 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3189 extdyn += bed->s->sizeof_dyn)
3190 {
3191 Elf_Internal_Dyn dyn;
3192
3193 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3194 switch (dyn.d_tag)
3195 {
3196 case DT_STRSZ:
3197 dyn.d_un.d_val = size;
3198 break;
3199 case DT_NEEDED:
3200 case DT_SONAME:
3201 case DT_RPATH:
3202 case DT_RUNPATH:
3203 case DT_FILTER:
3204 case DT_AUXILIARY:
7ee314fa
AM
3205 case DT_AUDIT:
3206 case DT_DEPAUDIT:
5a580b3a
AM
3207 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3208 break;
3209 default:
3210 continue;
3211 }
3212 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3213 }
3214
3215 /* Now update local dynamic symbols. */
3216 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3217 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3218 entry->isym.st_name);
3219
3220 /* And the rest of dynamic symbols. */
3221 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3222
3223 /* Adjust version definitions. */
3224 if (elf_tdata (output_bfd)->cverdefs)
3225 {
3226 asection *s;
3227 bfd_byte *p;
3228 bfd_size_type i;
3229 Elf_Internal_Verdef def;
3230 Elf_Internal_Verdaux defaux;
3231
3232 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3233 p = s->contents;
3234 do
3235 {
3236 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3237 &def);
3238 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3239 if (def.vd_aux != sizeof (Elf_External_Verdef))
3240 continue;
5a580b3a
AM
3241 for (i = 0; i < def.vd_cnt; ++i)
3242 {
3243 _bfd_elf_swap_verdaux_in (output_bfd,
3244 (Elf_External_Verdaux *) p, &defaux);
3245 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3246 defaux.vda_name);
3247 _bfd_elf_swap_verdaux_out (output_bfd,
3248 &defaux, (Elf_External_Verdaux *) p);
3249 p += sizeof (Elf_External_Verdaux);
3250 }
3251 }
3252 while (def.vd_next);
3253 }
3254
3255 /* Adjust version references. */
3256 if (elf_tdata (output_bfd)->verref)
3257 {
3258 asection *s;
3259 bfd_byte *p;
3260 bfd_size_type i;
3261 Elf_Internal_Verneed need;
3262 Elf_Internal_Vernaux needaux;
3263
3264 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3265 p = s->contents;
3266 do
3267 {
3268 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3269 &need);
3270 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3271 _bfd_elf_swap_verneed_out (output_bfd, &need,
3272 (Elf_External_Verneed *) p);
3273 p += sizeof (Elf_External_Verneed);
3274 for (i = 0; i < need.vn_cnt; ++i)
3275 {
3276 _bfd_elf_swap_vernaux_in (output_bfd,
3277 (Elf_External_Vernaux *) p, &needaux);
3278 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3279 needaux.vna_name);
3280 _bfd_elf_swap_vernaux_out (output_bfd,
3281 &needaux,
3282 (Elf_External_Vernaux *) p);
3283 p += sizeof (Elf_External_Vernaux);
3284 }
3285 }
3286 while (need.vn_next);
3287 }
3288
3289 return TRUE;
3290}
3291\f
13285a1b
AM
3292/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3293 The default is to only match when the INPUT and OUTPUT are exactly
3294 the same target. */
3295
3296bfd_boolean
3297_bfd_elf_default_relocs_compatible (const bfd_target *input,
3298 const bfd_target *output)
3299{
3300 return input == output;
3301}
3302
3303/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3304 This version is used when different targets for the same architecture
3305 are virtually identical. */
3306
3307bfd_boolean
3308_bfd_elf_relocs_compatible (const bfd_target *input,
3309 const bfd_target *output)
3310{
3311 const struct elf_backend_data *obed, *ibed;
3312
3313 if (input == output)
3314 return TRUE;
3315
3316 ibed = xvec_get_elf_backend_data (input);
3317 obed = xvec_get_elf_backend_data (output);
3318
3319 if (ibed->arch != obed->arch)
3320 return FALSE;
3321
3322 /* If both backends are using this function, deem them compatible. */
3323 return ibed->relocs_compatible == obed->relocs_compatible;
3324}
3325
4ad4eba5
AM
3326/* Add symbols from an ELF object file to the linker hash table. */
3327
3328static bfd_boolean
3329elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3330{
a0c402a5 3331 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3332 Elf_Internal_Shdr *hdr;
3333 bfd_size_type symcount;
3334 bfd_size_type extsymcount;
3335 bfd_size_type extsymoff;
3336 struct elf_link_hash_entry **sym_hash;
3337 bfd_boolean dynamic;
3338 Elf_External_Versym *extversym = NULL;
3339 Elf_External_Versym *ever;
3340 struct elf_link_hash_entry *weaks;
3341 struct elf_link_hash_entry **nondeflt_vers = NULL;
3342 bfd_size_type nondeflt_vers_cnt = 0;
3343 Elf_Internal_Sym *isymbuf = NULL;
3344 Elf_Internal_Sym *isym;
3345 Elf_Internal_Sym *isymend;
3346 const struct elf_backend_data *bed;
3347 bfd_boolean add_needed;
66eb6687 3348 struct elf_link_hash_table *htab;
4ad4eba5 3349 bfd_size_type amt;
66eb6687 3350 void *alloc_mark = NULL;
4f87808c
AM
3351 struct bfd_hash_entry **old_table = NULL;
3352 unsigned int old_size = 0;
3353 unsigned int old_count = 0;
66eb6687
AM
3354 void *old_tab = NULL;
3355 void *old_hash;
3356 void *old_ent;
3357 struct bfd_link_hash_entry *old_undefs = NULL;
3358 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3359 long old_dynsymcount = 0;
3360 size_t tabsize = 0;
3361 size_t hashsize = 0;
4ad4eba5 3362
66eb6687 3363 htab = elf_hash_table (info);
4ad4eba5 3364 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3365
3366 if ((abfd->flags & DYNAMIC) == 0)
3367 dynamic = FALSE;
3368 else
3369 {
3370 dynamic = TRUE;
3371
3372 /* You can't use -r against a dynamic object. Also, there's no
3373 hope of using a dynamic object which does not exactly match
3374 the format of the output file. */
3375 if (info->relocatable
66eb6687 3376 || !is_elf_hash_table (htab)
f13a99db 3377 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3378 {
9a0789ec
NC
3379 if (info->relocatable)
3380 bfd_set_error (bfd_error_invalid_operation);
3381 else
3382 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3383 goto error_return;
3384 }
3385 }
3386
a0c402a5
L
3387 ehdr = elf_elfheader (abfd);
3388 if (info->warn_alternate_em
3389 && bed->elf_machine_code != ehdr->e_machine
3390 && ((bed->elf_machine_alt1 != 0
3391 && ehdr->e_machine == bed->elf_machine_alt1)
3392 || (bed->elf_machine_alt2 != 0
3393 && ehdr->e_machine == bed->elf_machine_alt2)))
3394 info->callbacks->einfo
3395 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3396 ehdr->e_machine, abfd, bed->elf_machine_code);
3397
4ad4eba5
AM
3398 /* As a GNU extension, any input sections which are named
3399 .gnu.warning.SYMBOL are treated as warning symbols for the given
3400 symbol. This differs from .gnu.warning sections, which generate
3401 warnings when they are included in an output file. */
dd98f8d2
NC
3402 /* PR 12761: Also generate this warning when building shared libraries. */
3403 if (info->executable || info->shared)
4ad4eba5
AM
3404 {
3405 asection *s;
3406
3407 for (s = abfd->sections; s != NULL; s = s->next)
3408 {
3409 const char *name;
3410
3411 name = bfd_get_section_name (abfd, s);
0112cd26 3412 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5
AM
3413 {
3414 char *msg;
3415 bfd_size_type sz;
4ad4eba5
AM
3416
3417 name += sizeof ".gnu.warning." - 1;
3418
3419 /* If this is a shared object, then look up the symbol
3420 in the hash table. If it is there, and it is already
3421 been defined, then we will not be using the entry
3422 from this shared object, so we don't need to warn.
3423 FIXME: If we see the definition in a regular object
3424 later on, we will warn, but we shouldn't. The only
3425 fix is to keep track of what warnings we are supposed
3426 to emit, and then handle them all at the end of the
3427 link. */
3428 if (dynamic)
3429 {
3430 struct elf_link_hash_entry *h;
3431
66eb6687 3432 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4ad4eba5
AM
3433
3434 /* FIXME: What about bfd_link_hash_common? */
3435 if (h != NULL
3436 && (h->root.type == bfd_link_hash_defined
3437 || h->root.type == bfd_link_hash_defweak))
3438 {
3439 /* We don't want to issue this warning. Clobber
3440 the section size so that the warning does not
3441 get copied into the output file. */
eea6121a 3442 s->size = 0;
4ad4eba5
AM
3443 continue;
3444 }
3445 }
3446
eea6121a 3447 sz = s->size;
a50b1753 3448 msg = (char *) bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3449 if (msg == NULL)
3450 goto error_return;
3451
370a0e1b 3452 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3453 goto error_return;
3454
370a0e1b 3455 msg[sz] = '\0';
4ad4eba5
AM
3456
3457 if (! (_bfd_generic_link_add_one_symbol
3458 (info, abfd, name, BSF_WARNING, s, 0, msg,
66eb6687 3459 FALSE, bed->collect, NULL)))
4ad4eba5
AM
3460 goto error_return;
3461
3462 if (! info->relocatable)
3463 {
3464 /* Clobber the section size so that the warning does
3465 not get copied into the output file. */
eea6121a 3466 s->size = 0;
11d2f718
AM
3467
3468 /* Also set SEC_EXCLUDE, so that symbols defined in
3469 the warning section don't get copied to the output. */
3470 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3471 }
3472 }
3473 }
3474 }
3475
3476 add_needed = TRUE;
3477 if (! dynamic)
3478 {
3479 /* If we are creating a shared library, create all the dynamic
3480 sections immediately. We need to attach them to something,
3481 so we attach them to this BFD, provided it is the right
3482 format. FIXME: If there are no input BFD's of the same
3483 format as the output, we can't make a shared library. */
3484 if (info->shared
66eb6687 3485 && is_elf_hash_table (htab)
f13a99db 3486 && info->output_bfd->xvec == abfd->xvec
66eb6687 3487 && !htab->dynamic_sections_created)
4ad4eba5
AM
3488 {
3489 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3490 goto error_return;
3491 }
3492 }
66eb6687 3493 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3494 goto error_return;
3495 else
3496 {
3497 asection *s;
3498 const char *soname = NULL;
7ee314fa 3499 char *audit = NULL;
4ad4eba5
AM
3500 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3501 int ret;
3502
3503 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3504 ld shouldn't allow it. */
4ad4eba5
AM
3505 if ((s = abfd->sections) != NULL
3506 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
92fd189d 3507 abort ();
4ad4eba5
AM
3508
3509 /* If this dynamic lib was specified on the command line with
3510 --as-needed in effect, then we don't want to add a DT_NEEDED
3511 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3512 in by another lib's DT_NEEDED. When --no-add-needed is used
3513 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3514 any dynamic library in DT_NEEDED tags in the dynamic lib at
3515 all. */
3516 add_needed = (elf_dyn_lib_class (abfd)
3517 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3518 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3519
3520 s = bfd_get_section_by_name (abfd, ".dynamic");
3521 if (s != NULL)
3522 {
3523 bfd_byte *dynbuf;
3524 bfd_byte *extdyn;
cb33740c 3525 unsigned int elfsec;
4ad4eba5
AM
3526 unsigned long shlink;
3527
eea6121a 3528 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3529 {
3530error_free_dyn:
3531 free (dynbuf);
3532 goto error_return;
3533 }
4ad4eba5
AM
3534
3535 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3536 if (elfsec == SHN_BAD)
4ad4eba5
AM
3537 goto error_free_dyn;
3538 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3539
3540 for (extdyn = dynbuf;
eea6121a 3541 extdyn < dynbuf + s->size;
4ad4eba5
AM
3542 extdyn += bed->s->sizeof_dyn)
3543 {
3544 Elf_Internal_Dyn dyn;
3545
3546 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3547 if (dyn.d_tag == DT_SONAME)
3548 {
3549 unsigned int tagv = dyn.d_un.d_val;
3550 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3551 if (soname == NULL)
3552 goto error_free_dyn;
3553 }
3554 if (dyn.d_tag == DT_NEEDED)
3555 {
3556 struct bfd_link_needed_list *n, **pn;
3557 char *fnm, *anm;
3558 unsigned int tagv = dyn.d_un.d_val;
3559
3560 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3561 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3562 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3563 if (n == NULL || fnm == NULL)
3564 goto error_free_dyn;
3565 amt = strlen (fnm) + 1;
a50b1753 3566 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3567 if (anm == NULL)
3568 goto error_free_dyn;
3569 memcpy (anm, fnm, amt);
3570 n->name = anm;
3571 n->by = abfd;
3572 n->next = NULL;
66eb6687 3573 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3574 ;
3575 *pn = n;
3576 }
3577 if (dyn.d_tag == DT_RUNPATH)
3578 {
3579 struct bfd_link_needed_list *n, **pn;
3580 char *fnm, *anm;
3581 unsigned int tagv = dyn.d_un.d_val;
3582
3583 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3584 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3585 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3586 if (n == NULL || fnm == NULL)
3587 goto error_free_dyn;
3588 amt = strlen (fnm) + 1;
a50b1753 3589 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3590 if (anm == NULL)
3591 goto error_free_dyn;
3592 memcpy (anm, fnm, amt);
3593 n->name = anm;
3594 n->by = abfd;
3595 n->next = NULL;
3596 for (pn = & runpath;
3597 *pn != NULL;
3598 pn = &(*pn)->next)
3599 ;
3600 *pn = n;
3601 }
3602 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3603 if (!runpath && dyn.d_tag == DT_RPATH)
3604 {
3605 struct bfd_link_needed_list *n, **pn;
3606 char *fnm, *anm;
3607 unsigned int tagv = dyn.d_un.d_val;
3608
3609 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3610 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3611 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3612 if (n == NULL || fnm == NULL)
3613 goto error_free_dyn;
3614 amt = strlen (fnm) + 1;
a50b1753 3615 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3616 if (anm == NULL)
f8703194 3617 goto error_free_dyn;
4ad4eba5
AM
3618 memcpy (anm, fnm, amt);
3619 n->name = anm;
3620 n->by = abfd;
3621 n->next = NULL;
3622 for (pn = & rpath;
3623 *pn != NULL;
3624 pn = &(*pn)->next)
3625 ;
3626 *pn = n;
3627 }
7ee314fa
AM
3628 if (dyn.d_tag == DT_AUDIT)
3629 {
3630 unsigned int tagv = dyn.d_un.d_val;
3631 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3632 }
4ad4eba5
AM
3633 }
3634
3635 free (dynbuf);
3636 }
3637
3638 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3639 frees all more recently bfd_alloc'd blocks as well. */
3640 if (runpath)
3641 rpath = runpath;
3642
3643 if (rpath)
3644 {
3645 struct bfd_link_needed_list **pn;
66eb6687 3646 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3647 ;
3648 *pn = rpath;
3649 }
3650
3651 /* We do not want to include any of the sections in a dynamic
3652 object in the output file. We hack by simply clobbering the
3653 list of sections in the BFD. This could be handled more
3654 cleanly by, say, a new section flag; the existing
3655 SEC_NEVER_LOAD flag is not the one we want, because that one
3656 still implies that the section takes up space in the output
3657 file. */
3658 bfd_section_list_clear (abfd);
3659
4ad4eba5
AM
3660 /* Find the name to use in a DT_NEEDED entry that refers to this
3661 object. If the object has a DT_SONAME entry, we use it.
3662 Otherwise, if the generic linker stuck something in
3663 elf_dt_name, we use that. Otherwise, we just use the file
3664 name. */
3665 if (soname == NULL || *soname == '\0')
3666 {
3667 soname = elf_dt_name (abfd);
3668 if (soname == NULL || *soname == '\0')
3669 soname = bfd_get_filename (abfd);
3670 }
3671
3672 /* Save the SONAME because sometimes the linker emulation code
3673 will need to know it. */
3674 elf_dt_name (abfd) = soname;
3675
7e9f0867 3676 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3677 if (ret < 0)
3678 goto error_return;
3679
3680 /* If we have already included this dynamic object in the
3681 link, just ignore it. There is no reason to include a
3682 particular dynamic object more than once. */
3683 if (ret > 0)
3684 return TRUE;
7ee314fa
AM
3685
3686 /* Save the DT_AUDIT entry for the linker emulation code. */
3687 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3688 }
3689
3690 /* If this is a dynamic object, we always link against the .dynsym
3691 symbol table, not the .symtab symbol table. The dynamic linker
3692 will only see the .dynsym symbol table, so there is no reason to
3693 look at .symtab for a dynamic object. */
3694
3695 if (! dynamic || elf_dynsymtab (abfd) == 0)
3696 hdr = &elf_tdata (abfd)->symtab_hdr;
3697 else
3698 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3699
3700 symcount = hdr->sh_size / bed->s->sizeof_sym;
3701
3702 /* The sh_info field of the symtab header tells us where the
3703 external symbols start. We don't care about the local symbols at
3704 this point. */
3705 if (elf_bad_symtab (abfd))
3706 {
3707 extsymcount = symcount;
3708 extsymoff = 0;
3709 }
3710 else
3711 {
3712 extsymcount = symcount - hdr->sh_info;
3713 extsymoff = hdr->sh_info;
3714 }
3715
3716 sym_hash = NULL;
3717 if (extsymcount != 0)
3718 {
3719 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3720 NULL, NULL, NULL);
3721 if (isymbuf == NULL)
3722 goto error_return;
3723
3724 /* We store a pointer to the hash table entry for each external
3725 symbol. */
3726 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 3727 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4ad4eba5
AM
3728 if (sym_hash == NULL)
3729 goto error_free_sym;
3730 elf_sym_hashes (abfd) = sym_hash;
3731 }
3732
3733 if (dynamic)
3734 {
3735 /* Read in any version definitions. */
fc0e6df6
PB
3736 if (!_bfd_elf_slurp_version_tables (abfd,
3737 info->default_imported_symver))
4ad4eba5
AM
3738 goto error_free_sym;
3739
3740 /* Read in the symbol versions, but don't bother to convert them
3741 to internal format. */
3742 if (elf_dynversym (abfd) != 0)
3743 {
3744 Elf_Internal_Shdr *versymhdr;
3745
3746 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3747 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3748 if (extversym == NULL)
3749 goto error_free_sym;
3750 amt = versymhdr->sh_size;
3751 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3752 || bfd_bread (extversym, amt, abfd) != amt)
3753 goto error_free_vers;
3754 }
3755 }
3756
66eb6687
AM
3757 /* If we are loading an as-needed shared lib, save the symbol table
3758 state before we start adding symbols. If the lib turns out
3759 to be unneeded, restore the state. */
3760 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3761 {
3762 unsigned int i;
3763 size_t entsize;
3764
3765 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3766 {
3767 struct bfd_hash_entry *p;
2de92251 3768 struct elf_link_hash_entry *h;
66eb6687
AM
3769
3770 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3771 {
3772 h = (struct elf_link_hash_entry *) p;
3773 entsize += htab->root.table.entsize;
3774 if (h->root.type == bfd_link_hash_warning)
3775 entsize += htab->root.table.entsize;
3776 }
66eb6687
AM
3777 }
3778
3779 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3780 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3781 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3782 if (old_tab == NULL)
3783 goto error_free_vers;
3784
3785 /* Remember the current objalloc pointer, so that all mem for
3786 symbols added can later be reclaimed. */
3787 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3788 if (alloc_mark == NULL)
3789 goto error_free_vers;
3790
5061a885
AM
3791 /* Make a special call to the linker "notice" function to
3792 tell it that we are about to handle an as-needed lib. */
3793 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 3794 notice_as_needed, 0, NULL))
9af2a943 3795 goto error_free_vers;
5061a885 3796
66eb6687
AM
3797 /* Clone the symbol table and sym hashes. Remember some
3798 pointers into the symbol table, and dynamic symbol count. */
3799 old_hash = (char *) old_tab + tabsize;
3800 old_ent = (char *) old_hash + hashsize;
3801 memcpy (old_tab, htab->root.table.table, tabsize);
3802 memcpy (old_hash, sym_hash, hashsize);
3803 old_undefs = htab->root.undefs;
3804 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3805 old_table = htab->root.table.table;
3806 old_size = htab->root.table.size;
3807 old_count = htab->root.table.count;
66eb6687
AM
3808 old_dynsymcount = htab->dynsymcount;
3809
3810 for (i = 0; i < htab->root.table.size; i++)
3811 {
3812 struct bfd_hash_entry *p;
2de92251 3813 struct elf_link_hash_entry *h;
66eb6687
AM
3814
3815 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3816 {
3817 memcpy (old_ent, p, htab->root.table.entsize);
3818 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3819 h = (struct elf_link_hash_entry *) p;
3820 if (h->root.type == bfd_link_hash_warning)
3821 {
3822 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3823 old_ent = (char *) old_ent + htab->root.table.entsize;
3824 }
66eb6687
AM
3825 }
3826 }
3827 }
4ad4eba5 3828
66eb6687 3829 weaks = NULL;
4ad4eba5
AM
3830 ever = extversym != NULL ? extversym + extsymoff : NULL;
3831 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3832 isym < isymend;
3833 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3834 {
3835 int bind;
3836 bfd_vma value;
af44c138 3837 asection *sec, *new_sec;
4ad4eba5
AM
3838 flagword flags;
3839 const char *name;
3840 struct elf_link_hash_entry *h;
3841 bfd_boolean definition;
3842 bfd_boolean size_change_ok;
3843 bfd_boolean type_change_ok;
3844 bfd_boolean new_weakdef;
3845 bfd_boolean override;
a4d8e49b 3846 bfd_boolean common;
4ad4eba5
AM
3847 unsigned int old_alignment;
3848 bfd *old_bfd;
3cbc5de0 3849 bfd * undef_bfd = NULL;
4ad4eba5
AM
3850
3851 override = FALSE;
3852
3853 flags = BSF_NO_FLAGS;
3854 sec = NULL;
3855 value = isym->st_value;
3856 *sym_hash = NULL;
a4d8e49b 3857 common = bed->common_definition (isym);
4ad4eba5
AM
3858
3859 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3860 switch (bind)
4ad4eba5 3861 {
3e7a7d11 3862 case STB_LOCAL:
4ad4eba5
AM
3863 /* This should be impossible, since ELF requires that all
3864 global symbols follow all local symbols, and that sh_info
3865 point to the first global symbol. Unfortunately, Irix 5
3866 screws this up. */
3867 continue;
3e7a7d11
NC
3868
3869 case STB_GLOBAL:
a4d8e49b 3870 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3871 flags = BSF_GLOBAL;
3e7a7d11
NC
3872 break;
3873
3874 case STB_WEAK:
3875 flags = BSF_WEAK;
3876 break;
3877
3878 case STB_GNU_UNIQUE:
3879 flags = BSF_GNU_UNIQUE;
3880 break;
3881
3882 default:
4ad4eba5 3883 /* Leave it up to the processor backend. */
3e7a7d11 3884 break;
4ad4eba5
AM
3885 }
3886
3887 if (isym->st_shndx == SHN_UNDEF)
3888 sec = bfd_und_section_ptr;
cb33740c
AM
3889 else if (isym->st_shndx == SHN_ABS)
3890 sec = bfd_abs_section_ptr;
3891 else if (isym->st_shndx == SHN_COMMON)
3892 {
3893 sec = bfd_com_section_ptr;
3894 /* What ELF calls the size we call the value. What ELF
3895 calls the value we call the alignment. */
3896 value = isym->st_size;
3897 }
3898 else
4ad4eba5
AM
3899 {
3900 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3901 if (sec == NULL)
3902 sec = bfd_abs_section_ptr;
0c511000 3903 else if (elf_discarded_section (sec))
529fcb95 3904 {
e5d08002
L
3905 /* Symbols from discarded section are undefined. We keep
3906 its visibility. */
529fcb95
PB
3907 sec = bfd_und_section_ptr;
3908 isym->st_shndx = SHN_UNDEF;
3909 }
4ad4eba5
AM
3910 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3911 value -= sec->vma;
3912 }
4ad4eba5
AM
3913
3914 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3915 isym->st_name);
3916 if (name == NULL)
3917 goto error_free_vers;
3918
3919 if (isym->st_shndx == SHN_COMMON
02d00247
AM
3920 && (abfd->flags & BFD_PLUGIN) != 0)
3921 {
3922 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3923
3924 if (xc == NULL)
3925 {
3926 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3927 | SEC_EXCLUDE);
3928 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3929 if (xc == NULL)
3930 goto error_free_vers;
3931 }
3932 sec = xc;
3933 }
3934 else if (isym->st_shndx == SHN_COMMON
3935 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3936 && !info->relocatable)
4ad4eba5
AM
3937 {
3938 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3939
3940 if (tcomm == NULL)
3941 {
02d00247
AM
3942 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3943 | SEC_LINKER_CREATED);
3944 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 3945 if (tcomm == NULL)
4ad4eba5
AM
3946 goto error_free_vers;
3947 }
3948 sec = tcomm;
3949 }
66eb6687 3950 else if (bed->elf_add_symbol_hook)
4ad4eba5 3951 {
66eb6687
AM
3952 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3953 &sec, &value))
4ad4eba5
AM
3954 goto error_free_vers;
3955
3956 /* The hook function sets the name to NULL if this symbol
3957 should be skipped for some reason. */
3958 if (name == NULL)
3959 continue;
3960 }
3961
3962 /* Sanity check that all possibilities were handled. */
3963 if (sec == NULL)
3964 {
3965 bfd_set_error (bfd_error_bad_value);
3966 goto error_free_vers;
3967 }
3968
3969 if (bfd_is_und_section (sec)
3970 || bfd_is_com_section (sec))
3971 definition = FALSE;
3972 else
3973 definition = TRUE;
3974
3975 size_change_ok = FALSE;
66eb6687 3976 type_change_ok = bed->type_change_ok;
4ad4eba5
AM
3977 old_alignment = 0;
3978 old_bfd = NULL;
af44c138 3979 new_sec = sec;
4ad4eba5 3980
66eb6687 3981 if (is_elf_hash_table (htab))
4ad4eba5
AM
3982 {
3983 Elf_Internal_Versym iver;
3984 unsigned int vernum = 0;
3985 bfd_boolean skip;
3986
b918acf9
NC
3987 /* If this is a definition of a symbol which was previously
3988 referenced in a non-weak manner then make a note of the bfd
3989 that contained the reference. This is used if we need to
3990 refer to the source of the reference later on. */
3991 if (! bfd_is_und_section (sec))
3992 {
3993 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
3994
3995 if (h != NULL
3996 && h->root.type == bfd_link_hash_undefined
3997 && h->root.u.undef.abfd)
3998 undef_bfd = h->root.u.undef.abfd;
3999 }
4000
fc0e6df6 4001 if (ever == NULL)
4ad4eba5 4002 {
fc0e6df6
PB
4003 if (info->default_imported_symver)
4004 /* Use the default symbol version created earlier. */
4005 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4006 else
4007 iver.vs_vers = 0;
4008 }
4009 else
4010 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4011
4012 vernum = iver.vs_vers & VERSYM_VERSION;
4013
4014 /* If this is a hidden symbol, or if it is not version
4015 1, we append the version name to the symbol name.
cc86ff91
EB
4016 However, we do not modify a non-hidden absolute symbol
4017 if it is not a function, because it might be the version
4018 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4019 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4020 || (vernum > 1
4021 && (!bfd_is_abs_section (sec)
4022 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4023 {
4024 const char *verstr;
4025 size_t namelen, verlen, newlen;
4026 char *newname, *p;
4027
4028 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4029 {
fc0e6df6
PB
4030 if (vernum > elf_tdata (abfd)->cverdefs)
4031 verstr = NULL;
4032 else if (vernum > 1)
4033 verstr =
4034 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4035 else
4036 verstr = "";
4ad4eba5 4037
fc0e6df6 4038 if (verstr == NULL)
4ad4eba5 4039 {
fc0e6df6
PB
4040 (*_bfd_error_handler)
4041 (_("%B: %s: invalid version %u (max %d)"),
4042 abfd, name, vernum,
4043 elf_tdata (abfd)->cverdefs);
4044 bfd_set_error (bfd_error_bad_value);
4045 goto error_free_vers;
4ad4eba5 4046 }
fc0e6df6
PB
4047 }
4048 else
4049 {
4050 /* We cannot simply test for the number of
4051 entries in the VERNEED section since the
4052 numbers for the needed versions do not start
4053 at 0. */
4054 Elf_Internal_Verneed *t;
4055
4056 verstr = NULL;
4057 for (t = elf_tdata (abfd)->verref;
4058 t != NULL;
4059 t = t->vn_nextref)
4ad4eba5 4060 {
fc0e6df6 4061 Elf_Internal_Vernaux *a;
4ad4eba5 4062
fc0e6df6
PB
4063 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4064 {
4065 if (a->vna_other == vernum)
4ad4eba5 4066 {
fc0e6df6
PB
4067 verstr = a->vna_nodename;
4068 break;
4ad4eba5 4069 }
4ad4eba5 4070 }
fc0e6df6
PB
4071 if (a != NULL)
4072 break;
4073 }
4074 if (verstr == NULL)
4075 {
4076 (*_bfd_error_handler)
4077 (_("%B: %s: invalid needed version %d"),
4078 abfd, name, vernum);
4079 bfd_set_error (bfd_error_bad_value);
4080 goto error_free_vers;
4ad4eba5 4081 }
4ad4eba5 4082 }
fc0e6df6
PB
4083
4084 namelen = strlen (name);
4085 verlen = strlen (verstr);
4086 newlen = namelen + verlen + 2;
4087 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4088 && isym->st_shndx != SHN_UNDEF)
4089 ++newlen;
4090
a50b1753 4091 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4092 if (newname == NULL)
4093 goto error_free_vers;
4094 memcpy (newname, name, namelen);
4095 p = newname + namelen;
4096 *p++ = ELF_VER_CHR;
4097 /* If this is a defined non-hidden version symbol,
4098 we add another @ to the name. This indicates the
4099 default version of the symbol. */
4100 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4101 && isym->st_shndx != SHN_UNDEF)
4102 *p++ = ELF_VER_CHR;
4103 memcpy (p, verstr, verlen + 1);
4104
4105 name = newname;
4ad4eba5
AM
4106 }
4107
b918acf9
NC
4108 /* If necessary, make a second attempt to locate the bfd
4109 containing an unresolved, non-weak reference to the
4110 current symbol. */
4111 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
3cbc5de0
NC
4112 {
4113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4114
4115 if (h != NULL
b918acf9 4116 && h->root.type == bfd_link_hash_undefined
3cbc5de0
NC
4117 && h->root.u.undef.abfd)
4118 undef_bfd = h->root.u.undef.abfd;
4119 }
4120
af44c138
L
4121 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4122 &value, &old_alignment,
4ad4eba5
AM
4123 sym_hash, &skip, &override,
4124 &type_change_ok, &size_change_ok))
4125 goto error_free_vers;
4126
4127 if (skip)
4128 continue;
4129
4130 if (override)
4131 definition = FALSE;
4132
4133 h = *sym_hash;
4134 while (h->root.type == bfd_link_hash_indirect
4135 || h->root.type == bfd_link_hash_warning)
4136 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4137
4138 /* Remember the old alignment if this is a common symbol, so
4139 that we don't reduce the alignment later on. We can't
4140 check later, because _bfd_generic_link_add_one_symbol
4141 will set a default for the alignment which we want to
4142 override. We also remember the old bfd where the existing
4143 definition comes from. */
4144 switch (h->root.type)
4145 {
4146 default:
4147 break;
4148
4149 case bfd_link_hash_defined:
4150 case bfd_link_hash_defweak:
4151 old_bfd = h->root.u.def.section->owner;
4152 break;
4153
4154 case bfd_link_hash_common:
4155 old_bfd = h->root.u.c.p->section->owner;
4156 old_alignment = h->root.u.c.p->alignment_power;
4157 break;
4158 }
4159
4160 if (elf_tdata (abfd)->verdef != NULL
4161 && ! override
4162 && vernum > 1
4163 && definition)
4164 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4165 }
4166
4167 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4168 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4169 (struct bfd_link_hash_entry **) sym_hash)))
4170 goto error_free_vers;
4171
4172 h = *sym_hash;
4173 while (h->root.type == bfd_link_hash_indirect
4174 || h->root.type == bfd_link_hash_warning)
4175 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4176
4ad4eba5 4177 *sym_hash = h;
d64284fe
L
4178 if (is_elf_hash_table (htab))
4179 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4ad4eba5
AM
4180
4181 new_weakdef = FALSE;
4182 if (dynamic
4183 && definition
4184 && (flags & BSF_WEAK) != 0
fcb93ecf 4185 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4186 && is_elf_hash_table (htab)
f6e332e6 4187 && h->u.weakdef == NULL)
4ad4eba5
AM
4188 {
4189 /* Keep a list of all weak defined non function symbols from
4190 a dynamic object, using the weakdef field. Later in this
4191 function we will set the weakdef field to the correct
4192 value. We only put non-function symbols from dynamic
4193 objects on this list, because that happens to be the only
4194 time we need to know the normal symbol corresponding to a
4195 weak symbol, and the information is time consuming to
4196 figure out. If the weakdef field is not already NULL,
4197 then this symbol was already defined by some previous
4198 dynamic object, and we will be using that previous
4199 definition anyhow. */
4200
f6e332e6 4201 h->u.weakdef = weaks;
4ad4eba5
AM
4202 weaks = h;
4203 new_weakdef = TRUE;
4204 }
4205
4206 /* Set the alignment of a common symbol. */
a4d8e49b 4207 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4208 && h->root.type == bfd_link_hash_common)
4209 {
4210 unsigned int align;
4211
a4d8e49b 4212 if (common)
af44c138
L
4213 align = bfd_log2 (isym->st_value);
4214 else
4215 {
4216 /* The new symbol is a common symbol in a shared object.
4217 We need to get the alignment from the section. */
4218 align = new_sec->alignment_power;
4219 }
595213d4 4220 if (align > old_alignment)
4ad4eba5
AM
4221 h->root.u.c.p->alignment_power = align;
4222 else
4223 h->root.u.c.p->alignment_power = old_alignment;
4224 }
4225
66eb6687 4226 if (is_elf_hash_table (htab))
4ad4eba5 4227 {
4ad4eba5 4228 bfd_boolean dynsym;
4ad4eba5
AM
4229
4230 /* Check the alignment when a common symbol is involved. This
4231 can change when a common symbol is overridden by a normal
4232 definition or a common symbol is ignored due to the old
4233 normal definition. We need to make sure the maximum
4234 alignment is maintained. */
a4d8e49b 4235 if ((old_alignment || common)
4ad4eba5
AM
4236 && h->root.type != bfd_link_hash_common)
4237 {
4238 unsigned int common_align;
4239 unsigned int normal_align;
4240 unsigned int symbol_align;
4241 bfd *normal_bfd;
4242 bfd *common_bfd;
4243
4244 symbol_align = ffs (h->root.u.def.value) - 1;
4245 if (h->root.u.def.section->owner != NULL
4246 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4247 {
4248 normal_align = h->root.u.def.section->alignment_power;
4249 if (normal_align > symbol_align)
4250 normal_align = symbol_align;
4251 }
4252 else
4253 normal_align = symbol_align;
4254
4255 if (old_alignment)
4256 {
4257 common_align = old_alignment;
4258 common_bfd = old_bfd;
4259 normal_bfd = abfd;
4260 }
4261 else
4262 {
4263 common_align = bfd_log2 (isym->st_value);
4264 common_bfd = abfd;
4265 normal_bfd = old_bfd;
4266 }
4267
4268 if (normal_align < common_align)
d07676f8
NC
4269 {
4270 /* PR binutils/2735 */
4271 if (normal_bfd == NULL)
4272 (*_bfd_error_handler)
4273 (_("Warning: alignment %u of common symbol `%s' in %B"
4274 " is greater than the alignment (%u) of its section %A"),
4275 common_bfd, h->root.u.def.section,
4276 1 << common_align, name, 1 << normal_align);
4277 else
4278 (*_bfd_error_handler)
4279 (_("Warning: alignment %u of symbol `%s' in %B"
4280 " is smaller than %u in %B"),
4281 normal_bfd, common_bfd,
4282 1 << normal_align, name, 1 << common_align);
4283 }
4ad4eba5
AM
4284 }
4285
83ad0046
L
4286 /* Remember the symbol size if it isn't undefined. */
4287 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4ad4eba5
AM
4288 && (definition || h->size == 0))
4289 {
83ad0046
L
4290 if (h->size != 0
4291 && h->size != isym->st_size
4292 && ! size_change_ok)
4ad4eba5 4293 (*_bfd_error_handler)
d003868e
AM
4294 (_("Warning: size of symbol `%s' changed"
4295 " from %lu in %B to %lu in %B"),
4296 old_bfd, abfd,
4ad4eba5 4297 name, (unsigned long) h->size,
d003868e 4298 (unsigned long) isym->st_size);
4ad4eba5
AM
4299
4300 h->size = isym->st_size;
4301 }
4302
4303 /* If this is a common symbol, then we always want H->SIZE
4304 to be the size of the common symbol. The code just above
4305 won't fix the size if a common symbol becomes larger. We
4306 don't warn about a size change here, because that is
fcb93ecf
PB
4307 covered by --warn-common. Allow changed between different
4308 function types. */
4ad4eba5
AM
4309 if (h->root.type == bfd_link_hash_common)
4310 h->size = h->root.u.c.size;
4311
4312 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4313 && (definition || h->type == STT_NOTYPE))
4314 {
2955ec4c
L
4315 unsigned int type = ELF_ST_TYPE (isym->st_info);
4316
4317 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4318 symbol. */
4319 if (type == STT_GNU_IFUNC
4320 && (abfd->flags & DYNAMIC) != 0)
4321 type = STT_FUNC;
4ad4eba5 4322
2955ec4c
L
4323 if (h->type != type)
4324 {
4325 if (h->type != STT_NOTYPE && ! type_change_ok)
4326 (*_bfd_error_handler)
4327 (_("Warning: type of symbol `%s' changed"
4328 " from %d to %d in %B"),
4329 abfd, name, h->type, type);
4330
4331 h->type = type;
4332 }
4ad4eba5
AM
4333 }
4334
54ac0771
L
4335 /* Merge st_other field. */
4336 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4ad4eba5
AM
4337
4338 /* Set a flag in the hash table entry indicating the type of
4339 reference or definition we just found. Keep a count of
4340 the number of dynamic symbols we find. A dynamic symbol
4341 is one which is referenced or defined by both a regular
4342 object and a shared object. */
4ad4eba5
AM
4343 dynsym = FALSE;
4344 if (! dynamic)
4345 {
4346 if (! definition)
4347 {
f5385ebf 4348 h->ref_regular = 1;
4ad4eba5 4349 if (bind != STB_WEAK)
f5385ebf 4350 h->ref_regular_nonweak = 1;
4ad4eba5
AM
4351 }
4352 else
d8880531
L
4353 {
4354 h->def_regular = 1;
4355 if (h->def_dynamic)
4356 {
4357 h->def_dynamic = 0;
4358 h->ref_dynamic = 1;
4359 h->dynamic_def = 1;
4360 }
4361 }
4ad4eba5 4362 if (! info->executable
f5385ebf
AM
4363 || h->def_dynamic
4364 || h->ref_dynamic)
4ad4eba5
AM
4365 dynsym = TRUE;
4366 }
4367 else
4368 {
4369 if (! definition)
f5385ebf 4370 h->ref_dynamic = 1;
4ad4eba5 4371 else
f5385ebf
AM
4372 h->def_dynamic = 1;
4373 if (h->def_regular
4374 || h->ref_regular
f6e332e6 4375 || (h->u.weakdef != NULL
4ad4eba5 4376 && ! new_weakdef
f6e332e6 4377 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4378 dynsym = TRUE;
4379 }
4380
b2064611 4381 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
92b7c7b6
L
4382 {
4383 /* We don't want to make debug symbol dynamic. */
92b7c7b6
L
4384 dynsym = FALSE;
4385 }
4386
35fc36a8
RS
4387 if (definition)
4388 h->target_internal = isym->st_target_internal;
4389
4ad4eba5
AM
4390 /* Check to see if we need to add an indirect symbol for
4391 the default name. */
4392 if (definition || h->root.type == bfd_link_hash_common)
4393 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4394 &sec, &value, &dynsym,
4395 override))
4396 goto error_free_vers;
4397
4398 if (definition && !dynamic)
4399 {
4400 char *p = strchr (name, ELF_VER_CHR);
4401 if (p != NULL && p[1] != ELF_VER_CHR)
4402 {
4403 /* Queue non-default versions so that .symver x, x@FOO
4404 aliases can be checked. */
66eb6687 4405 if (!nondeflt_vers)
4ad4eba5 4406 {
66eb6687
AM
4407 amt = ((isymend - isym + 1)
4408 * sizeof (struct elf_link_hash_entry *));
a50b1753
NC
4409 nondeflt_vers =
4410 (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4411 if (!nondeflt_vers)
4412 goto error_free_vers;
4ad4eba5 4413 }
66eb6687 4414 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4415 }
4416 }
4417
4418 if (dynsym && h->dynindx == -1)
4419 {
c152c796 4420 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4421 goto error_free_vers;
f6e332e6 4422 if (h->u.weakdef != NULL
4ad4eba5 4423 && ! new_weakdef
f6e332e6 4424 && h->u.weakdef->dynindx == -1)
4ad4eba5 4425 {
66eb6687 4426 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4427 goto error_free_vers;
4428 }
4429 }
4430 else if (dynsym && h->dynindx != -1)
4431 /* If the symbol already has a dynamic index, but
4432 visibility says it should not be visible, turn it into
4433 a local symbol. */
4434 switch (ELF_ST_VISIBILITY (h->other))
4435 {
4436 case STV_INTERNAL:
4437 case STV_HIDDEN:
4438 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4439 dynsym = FALSE;
4440 break;
4441 }
4442
4443 if (!add_needed
4444 && definition
010e5ae2
AM
4445 && ((dynsym
4446 && h->ref_regular)
4447 || (h->ref_dynamic
4448 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4449 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4450 {
4451 int ret;
4452 const char *soname = elf_dt_name (abfd);
4453
4454 /* A symbol from a library loaded via DT_NEEDED of some
4455 other library is referenced by a regular object.
e56f61be 4456 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4457 --no-add-needed is used and the reference was not
4458 a weak one. */
4459 if (undef_bfd != NULL
4460 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4461 {
4462 (*_bfd_error_handler)
3cbc5de0 4463 (_("%B: undefined reference to symbol '%s'"),
b918acf9 4464 undef_bfd, name);
3cbc5de0
NC
4465 (*_bfd_error_handler)
4466 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
d003868e 4467 abfd, name);
3cbc5de0 4468 bfd_set_error (bfd_error_invalid_operation);
e56f61be
L
4469 goto error_free_vers;
4470 }
4471
a50b1753
NC
4472 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4473 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4474
4ad4eba5 4475 add_needed = TRUE;
7e9f0867 4476 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4477 if (ret < 0)
4478 goto error_free_vers;
4479
4480 BFD_ASSERT (ret == 0);
4481 }
4482 }
4483 }
4484
66eb6687
AM
4485 if (extversym != NULL)
4486 {
4487 free (extversym);
4488 extversym = NULL;
4489 }
4490
4491 if (isymbuf != NULL)
4492 {
4493 free (isymbuf);
4494 isymbuf = NULL;
4495 }
4496
4497 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4498 {
4499 unsigned int i;
4500
4501 /* Restore the symbol table. */
97fed1c9
JJ
4502 if (bed->as_needed_cleanup)
4503 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4504 old_hash = (char *) old_tab + tabsize;
4505 old_ent = (char *) old_hash + hashsize;
4506 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4507 htab->root.table.table = old_table;
4508 htab->root.table.size = old_size;
4509 htab->root.table.count = old_count;
66eb6687
AM
4510 memcpy (htab->root.table.table, old_tab, tabsize);
4511 memcpy (sym_hash, old_hash, hashsize);
4512 htab->root.undefs = old_undefs;
4513 htab->root.undefs_tail = old_undefs_tail;
4514 for (i = 0; i < htab->root.table.size; i++)
4515 {
4516 struct bfd_hash_entry *p;
4517 struct elf_link_hash_entry *h;
4518
4519 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4520 {
4521 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4522 if (h->root.type == bfd_link_hash_warning)
4523 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4524 if (h->dynindx >= old_dynsymcount)
4525 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4526
66eb6687
AM
4527 memcpy (p, old_ent, htab->root.table.entsize);
4528 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4529 h = (struct elf_link_hash_entry *) p;
4530 if (h->root.type == bfd_link_hash_warning)
4531 {
4532 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4533 old_ent = (char *) old_ent + htab->root.table.entsize;
4534 }
66eb6687
AM
4535 }
4536 }
4537
5061a885
AM
4538 /* Make a special call to the linker "notice" function to
4539 tell it that symbols added for crefs may need to be removed. */
4540 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4541 notice_not_needed, 0, NULL))
9af2a943 4542 goto error_free_vers;
5061a885 4543
66eb6687
AM
4544 free (old_tab);
4545 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4546 alloc_mark);
4547 if (nondeflt_vers != NULL)
4548 free (nondeflt_vers);
4549 return TRUE;
4550 }
2de92251 4551
66eb6687
AM
4552 if (old_tab != NULL)
4553 {
5061a885 4554 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4555 notice_needed, 0, NULL))
9af2a943 4556 goto error_free_vers;
66eb6687
AM
4557 free (old_tab);
4558 old_tab = NULL;
4559 }
4560
4ad4eba5
AM
4561 /* Now that all the symbols from this input file are created, handle
4562 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4563 if (nondeflt_vers != NULL)
4564 {
4565 bfd_size_type cnt, symidx;
4566
4567 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4568 {
4569 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4570 char *shortname, *p;
4571
4572 p = strchr (h->root.root.string, ELF_VER_CHR);
4573 if (p == NULL
4574 || (h->root.type != bfd_link_hash_defined
4575 && h->root.type != bfd_link_hash_defweak))
4576 continue;
4577
4578 amt = p - h->root.root.string;
a50b1753 4579 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4580 if (!shortname)
4581 goto error_free_vers;
4ad4eba5
AM
4582 memcpy (shortname, h->root.root.string, amt);
4583 shortname[amt] = '\0';
4584
4585 hi = (struct elf_link_hash_entry *)
66eb6687 4586 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4587 FALSE, FALSE, FALSE);
4588 if (hi != NULL
4589 && hi->root.type == h->root.type
4590 && hi->root.u.def.value == h->root.u.def.value
4591 && hi->root.u.def.section == h->root.u.def.section)
4592 {
4593 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4594 hi->root.type = bfd_link_hash_indirect;
4595 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4596 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4597 sym_hash = elf_sym_hashes (abfd);
4598 if (sym_hash)
4599 for (symidx = 0; symidx < extsymcount; ++symidx)
4600 if (sym_hash[symidx] == hi)
4601 {
4602 sym_hash[symidx] = h;
4603 break;
4604 }
4605 }
4606 free (shortname);
4607 }
4608 free (nondeflt_vers);
4609 nondeflt_vers = NULL;
4610 }
4611
4ad4eba5
AM
4612 /* Now set the weakdefs field correctly for all the weak defined
4613 symbols we found. The only way to do this is to search all the
4614 symbols. Since we only need the information for non functions in
4615 dynamic objects, that's the only time we actually put anything on
4616 the list WEAKS. We need this information so that if a regular
4617 object refers to a symbol defined weakly in a dynamic object, the
4618 real symbol in the dynamic object is also put in the dynamic
4619 symbols; we also must arrange for both symbols to point to the
4620 same memory location. We could handle the general case of symbol
4621 aliasing, but a general symbol alias can only be generated in
4622 assembler code, handling it correctly would be very time
4623 consuming, and other ELF linkers don't handle general aliasing
4624 either. */
4625 if (weaks != NULL)
4626 {
4627 struct elf_link_hash_entry **hpp;
4628 struct elf_link_hash_entry **hppend;
4629 struct elf_link_hash_entry **sorted_sym_hash;
4630 struct elf_link_hash_entry *h;
4631 size_t sym_count;
4632
4633 /* Since we have to search the whole symbol list for each weak
4634 defined symbol, search time for N weak defined symbols will be
4635 O(N^2). Binary search will cut it down to O(NlogN). */
4636 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4637 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4638 if (sorted_sym_hash == NULL)
4639 goto error_return;
4640 sym_hash = sorted_sym_hash;
4641 hpp = elf_sym_hashes (abfd);
4642 hppend = hpp + extsymcount;
4643 sym_count = 0;
4644 for (; hpp < hppend; hpp++)
4645 {
4646 h = *hpp;
4647 if (h != NULL
4648 && h->root.type == bfd_link_hash_defined
fcb93ecf 4649 && !bed->is_function_type (h->type))
4ad4eba5
AM
4650 {
4651 *sym_hash = h;
4652 sym_hash++;
4653 sym_count++;
4654 }
4655 }
4656
4657 qsort (sorted_sym_hash, sym_count,
4658 sizeof (struct elf_link_hash_entry *),
4659 elf_sort_symbol);
4660
4661 while (weaks != NULL)
4662 {
4663 struct elf_link_hash_entry *hlook;
4664 asection *slook;
4665 bfd_vma vlook;
4666 long ilook;
4667 size_t i, j, idx;
4668
4669 hlook = weaks;
f6e332e6
AM
4670 weaks = hlook->u.weakdef;
4671 hlook->u.weakdef = NULL;
4ad4eba5
AM
4672
4673 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4674 || hlook->root.type == bfd_link_hash_defweak
4675 || hlook->root.type == bfd_link_hash_common
4676 || hlook->root.type == bfd_link_hash_indirect);
4677 slook = hlook->root.u.def.section;
4678 vlook = hlook->root.u.def.value;
4679
4680 ilook = -1;
4681 i = 0;
4682 j = sym_count;
4683 while (i < j)
4684 {
4685 bfd_signed_vma vdiff;
4686 idx = (i + j) / 2;
4687 h = sorted_sym_hash [idx];
4688 vdiff = vlook - h->root.u.def.value;
4689 if (vdiff < 0)
4690 j = idx;
4691 else if (vdiff > 0)
4692 i = idx + 1;
4693 else
4694 {
a9b881be 4695 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4696 if (sdiff < 0)
4697 j = idx;
4698 else if (sdiff > 0)
4699 i = idx + 1;
4700 else
4701 {
4702 ilook = idx;
4703 break;
4704 }
4705 }
4706 }
4707
4708 /* We didn't find a value/section match. */
4709 if (ilook == -1)
4710 continue;
4711
4712 for (i = ilook; i < sym_count; i++)
4713 {
4714 h = sorted_sym_hash [i];
4715
4716 /* Stop if value or section doesn't match. */
4717 if (h->root.u.def.value != vlook
4718 || h->root.u.def.section != slook)
4719 break;
4720 else if (h != hlook)
4721 {
f6e332e6 4722 hlook->u.weakdef = h;
4ad4eba5
AM
4723
4724 /* If the weak definition is in the list of dynamic
4725 symbols, make sure the real definition is put
4726 there as well. */
4727 if (hlook->dynindx != -1 && h->dynindx == -1)
4728 {
c152c796 4729 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4730 {
4731 err_free_sym_hash:
4732 free (sorted_sym_hash);
4733 goto error_return;
4734 }
4ad4eba5
AM
4735 }
4736
4737 /* If the real definition is in the list of dynamic
4738 symbols, make sure the weak definition is put
4739 there as well. If we don't do this, then the
4740 dynamic loader might not merge the entries for the
4741 real definition and the weak definition. */
4742 if (h->dynindx != -1 && hlook->dynindx == -1)
4743 {
c152c796 4744 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4745 goto err_free_sym_hash;
4ad4eba5
AM
4746 }
4747 break;
4748 }
4749 }
4750 }
4751
4752 free (sorted_sym_hash);
4753 }
4754
33177bb1
AM
4755 if (bed->check_directives
4756 && !(*bed->check_directives) (abfd, info))
4757 return FALSE;
85fbca6a 4758
4ad4eba5
AM
4759 /* If this object is the same format as the output object, and it is
4760 not a shared library, then let the backend look through the
4761 relocs.
4762
4763 This is required to build global offset table entries and to
4764 arrange for dynamic relocs. It is not required for the
4765 particular common case of linking non PIC code, even when linking
4766 against shared libraries, but unfortunately there is no way of
4767 knowing whether an object file has been compiled PIC or not.
4768 Looking through the relocs is not particularly time consuming.
4769 The problem is that we must either (1) keep the relocs in memory,
4770 which causes the linker to require additional runtime memory or
4771 (2) read the relocs twice from the input file, which wastes time.
4772 This would be a good case for using mmap.
4773
4774 I have no idea how to handle linking PIC code into a file of a
4775 different format. It probably can't be done. */
4ad4eba5 4776 if (! dynamic
66eb6687 4777 && is_elf_hash_table (htab)
13285a1b 4778 && bed->check_relocs != NULL
39334f3a 4779 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4780 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4781 {
4782 asection *o;
4783
4784 for (o = abfd->sections; o != NULL; o = o->next)
4785 {
4786 Elf_Internal_Rela *internal_relocs;
4787 bfd_boolean ok;
4788
4789 if ((o->flags & SEC_RELOC) == 0
4790 || o->reloc_count == 0
4791 || ((info->strip == strip_all || info->strip == strip_debugger)
4792 && (o->flags & SEC_DEBUGGING) != 0)
4793 || bfd_is_abs_section (o->output_section))
4794 continue;
4795
4796 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4797 info->keep_memory);
4798 if (internal_relocs == NULL)
4799 goto error_return;
4800
66eb6687 4801 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4802
4803 if (elf_section_data (o)->relocs != internal_relocs)
4804 free (internal_relocs);
4805
4806 if (! ok)
4807 goto error_return;
4808 }
4809 }
4810
4811 /* If this is a non-traditional link, try to optimize the handling
4812 of the .stab/.stabstr sections. */
4813 if (! dynamic
4814 && ! info->traditional_format
66eb6687 4815 && is_elf_hash_table (htab)
4ad4eba5
AM
4816 && (info->strip != strip_all && info->strip != strip_debugger))
4817 {
4818 asection *stabstr;
4819
4820 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4821 if (stabstr != NULL)
4822 {
4823 bfd_size_type string_offset = 0;
4824 asection *stab;
4825
4826 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4827 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4828 && (!stab->name[5] ||
4829 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4830 && (stab->flags & SEC_MERGE) == 0
4831 && !bfd_is_abs_section (stab->output_section))
4832 {
4833 struct bfd_elf_section_data *secdata;
4834
4835 secdata = elf_section_data (stab);
66eb6687
AM
4836 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4837 stabstr, &secdata->sec_info,
4ad4eba5
AM
4838 &string_offset))
4839 goto error_return;
4840 if (secdata->sec_info)
4841 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4842 }
4843 }
4844 }
4845
66eb6687 4846 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4847 {
4848 /* Add this bfd to the loaded list. */
4849 struct elf_link_loaded_list *n;
4850
a50b1753
NC
4851 n = (struct elf_link_loaded_list *)
4852 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4ad4eba5
AM
4853 if (n == NULL)
4854 goto error_return;
4855 n->abfd = abfd;
66eb6687
AM
4856 n->next = htab->loaded;
4857 htab->loaded = n;
4ad4eba5
AM
4858 }
4859
4860 return TRUE;
4861
4862 error_free_vers:
66eb6687
AM
4863 if (old_tab != NULL)
4864 free (old_tab);
4ad4eba5
AM
4865 if (nondeflt_vers != NULL)
4866 free (nondeflt_vers);
4867 if (extversym != NULL)
4868 free (extversym);
4869 error_free_sym:
4870 if (isymbuf != NULL)
4871 free (isymbuf);
4872 error_return:
4873 return FALSE;
4874}
4875
8387904d
AM
4876/* Return the linker hash table entry of a symbol that might be
4877 satisfied by an archive symbol. Return -1 on error. */
4878
4879struct elf_link_hash_entry *
4880_bfd_elf_archive_symbol_lookup (bfd *abfd,
4881 struct bfd_link_info *info,
4882 const char *name)
4883{
4884 struct elf_link_hash_entry *h;
4885 char *p, *copy;
4886 size_t len, first;
4887
2a41f396 4888 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
4889 if (h != NULL)
4890 return h;
4891
4892 /* If this is a default version (the name contains @@), look up the
4893 symbol again with only one `@' as well as without the version.
4894 The effect is that references to the symbol with and without the
4895 version will be matched by the default symbol in the archive. */
4896
4897 p = strchr (name, ELF_VER_CHR);
4898 if (p == NULL || p[1] != ELF_VER_CHR)
4899 return h;
4900
4901 /* First check with only one `@'. */
4902 len = strlen (name);
a50b1753 4903 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
4904 if (copy == NULL)
4905 return (struct elf_link_hash_entry *) 0 - 1;
4906
4907 first = p - name + 1;
4908 memcpy (copy, name, first);
4909 memcpy (copy + first, name + first + 1, len - first);
4910
2a41f396 4911 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
4912 if (h == NULL)
4913 {
4914 /* We also need to check references to the symbol without the
4915 version. */
4916 copy[first - 1] = '\0';
4917 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 4918 FALSE, FALSE, TRUE);
8387904d
AM
4919 }
4920
4921 bfd_release (abfd, copy);
4922 return h;
4923}
4924
0ad989f9
L
4925/* Add symbols from an ELF archive file to the linker hash table. We
4926 don't use _bfd_generic_link_add_archive_symbols because of a
4927 problem which arises on UnixWare. The UnixWare libc.so is an
4928 archive which includes an entry libc.so.1 which defines a bunch of
4929 symbols. The libc.so archive also includes a number of other
4930 object files, which also define symbols, some of which are the same
4931 as those defined in libc.so.1. Correct linking requires that we
4932 consider each object file in turn, and include it if it defines any
4933 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4934 this; it looks through the list of undefined symbols, and includes
4935 any object file which defines them. When this algorithm is used on
4936 UnixWare, it winds up pulling in libc.so.1 early and defining a
4937 bunch of symbols. This means that some of the other objects in the
4938 archive are not included in the link, which is incorrect since they
4939 precede libc.so.1 in the archive.
4940
4941 Fortunately, ELF archive handling is simpler than that done by
4942 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4943 oddities. In ELF, if we find a symbol in the archive map, and the
4944 symbol is currently undefined, we know that we must pull in that
4945 object file.
4946
4947 Unfortunately, we do have to make multiple passes over the symbol
4948 table until nothing further is resolved. */
4949
4ad4eba5
AM
4950static bfd_boolean
4951elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4952{
4953 symindex c;
4954 bfd_boolean *defined = NULL;
4955 bfd_boolean *included = NULL;
4956 carsym *symdefs;
4957 bfd_boolean loop;
4958 bfd_size_type amt;
8387904d
AM
4959 const struct elf_backend_data *bed;
4960 struct elf_link_hash_entry * (*archive_symbol_lookup)
4961 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4962
4963 if (! bfd_has_map (abfd))
4964 {
4965 /* An empty archive is a special case. */
4966 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4967 return TRUE;
4968 bfd_set_error (bfd_error_no_armap);
4969 return FALSE;
4970 }
4971
4972 /* Keep track of all symbols we know to be already defined, and all
4973 files we know to be already included. This is to speed up the
4974 second and subsequent passes. */
4975 c = bfd_ardata (abfd)->symdef_count;
4976 if (c == 0)
4977 return TRUE;
4978 amt = c;
4979 amt *= sizeof (bfd_boolean);
a50b1753
NC
4980 defined = (bfd_boolean *) bfd_zmalloc (amt);
4981 included = (bfd_boolean *) bfd_zmalloc (amt);
0ad989f9
L
4982 if (defined == NULL || included == NULL)
4983 goto error_return;
4984
4985 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
4986 bed = get_elf_backend_data (abfd);
4987 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
4988
4989 do
4990 {
4991 file_ptr last;
4992 symindex i;
4993 carsym *symdef;
4994 carsym *symdefend;
4995
4996 loop = FALSE;
4997 last = -1;
4998
4999 symdef = symdefs;
5000 symdefend = symdef + c;
5001 for (i = 0; symdef < symdefend; symdef++, i++)
5002 {
5003 struct elf_link_hash_entry *h;
5004 bfd *element;
5005 struct bfd_link_hash_entry *undefs_tail;
5006 symindex mark;
5007
5008 if (defined[i] || included[i])
5009 continue;
5010 if (symdef->file_offset == last)
5011 {
5012 included[i] = TRUE;
5013 continue;
5014 }
5015
8387904d
AM
5016 h = archive_symbol_lookup (abfd, info, symdef->name);
5017 if (h == (struct elf_link_hash_entry *) 0 - 1)
5018 goto error_return;
0ad989f9
L
5019
5020 if (h == NULL)
5021 continue;
5022
5023 if (h->root.type == bfd_link_hash_common)
5024 {
5025 /* We currently have a common symbol. The archive map contains
5026 a reference to this symbol, so we may want to include it. We
5027 only want to include it however, if this archive element
5028 contains a definition of the symbol, not just another common
5029 declaration of it.
5030
5031 Unfortunately some archivers (including GNU ar) will put
5032 declarations of common symbols into their archive maps, as
5033 well as real definitions, so we cannot just go by the archive
5034 map alone. Instead we must read in the element's symbol
5035 table and check that to see what kind of symbol definition
5036 this is. */
5037 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5038 continue;
5039 }
5040 else if (h->root.type != bfd_link_hash_undefined)
5041 {
5042 if (h->root.type != bfd_link_hash_undefweak)
5043 defined[i] = TRUE;
5044 continue;
5045 }
5046
5047 /* We need to include this archive member. */
5048 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5049 if (element == NULL)
5050 goto error_return;
5051
5052 if (! bfd_check_format (element, bfd_object))
5053 goto error_return;
5054
5055 /* Doublecheck that we have not included this object
5056 already--it should be impossible, but there may be
5057 something wrong with the archive. */
5058 if (element->archive_pass != 0)
5059 {
5060 bfd_set_error (bfd_error_bad_value);
5061 goto error_return;
5062 }
5063 element->archive_pass = 1;
5064
5065 undefs_tail = info->hash->undefs_tail;
5066
0e144ba7
AM
5067 if (!(*info->callbacks
5068 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5069 goto error_return;
0e144ba7 5070 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5071 goto error_return;
5072
5073 /* If there are any new undefined symbols, we need to make
5074 another pass through the archive in order to see whether
5075 they can be defined. FIXME: This isn't perfect, because
5076 common symbols wind up on undefs_tail and because an
5077 undefined symbol which is defined later on in this pass
5078 does not require another pass. This isn't a bug, but it
5079 does make the code less efficient than it could be. */
5080 if (undefs_tail != info->hash->undefs_tail)
5081 loop = TRUE;
5082
5083 /* Look backward to mark all symbols from this object file
5084 which we have already seen in this pass. */
5085 mark = i;
5086 do
5087 {
5088 included[mark] = TRUE;
5089 if (mark == 0)
5090 break;
5091 --mark;
5092 }
5093 while (symdefs[mark].file_offset == symdef->file_offset);
5094
5095 /* We mark subsequent symbols from this object file as we go
5096 on through the loop. */
5097 last = symdef->file_offset;
5098 }
5099 }
5100 while (loop);
5101
5102 free (defined);
5103 free (included);
5104
5105 return TRUE;
5106
5107 error_return:
5108 if (defined != NULL)
5109 free (defined);
5110 if (included != NULL)
5111 free (included);
5112 return FALSE;
5113}
4ad4eba5
AM
5114
5115/* Given an ELF BFD, add symbols to the global hash table as
5116 appropriate. */
5117
5118bfd_boolean
5119bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5120{
5121 switch (bfd_get_format (abfd))
5122 {
5123 case bfd_object:
5124 return elf_link_add_object_symbols (abfd, info);
5125 case bfd_archive:
5126 return elf_link_add_archive_symbols (abfd, info);
5127 default:
5128 bfd_set_error (bfd_error_wrong_format);
5129 return FALSE;
5130 }
5131}
5a580b3a 5132\f
14b1c01e
AM
5133struct hash_codes_info
5134{
5135 unsigned long *hashcodes;
5136 bfd_boolean error;
5137};
a0c8462f 5138
5a580b3a
AM
5139/* This function will be called though elf_link_hash_traverse to store
5140 all hash value of the exported symbols in an array. */
5141
5142static bfd_boolean
5143elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5144{
a50b1753 5145 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a
AM
5146 const char *name;
5147 char *p;
5148 unsigned long ha;
5149 char *alc = NULL;
5150
5a580b3a
AM
5151 /* Ignore indirect symbols. These are added by the versioning code. */
5152 if (h->dynindx == -1)
5153 return TRUE;
5154
5155 name = h->root.root.string;
5156 p = strchr (name, ELF_VER_CHR);
5157 if (p != NULL)
5158 {
a50b1753 5159 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5160 if (alc == NULL)
5161 {
5162 inf->error = TRUE;
5163 return FALSE;
5164 }
5a580b3a
AM
5165 memcpy (alc, name, p - name);
5166 alc[p - name] = '\0';
5167 name = alc;
5168 }
5169
5170 /* Compute the hash value. */
5171 ha = bfd_elf_hash (name);
5172
5173 /* Store the found hash value in the array given as the argument. */
14b1c01e 5174 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5175
5176 /* And store it in the struct so that we can put it in the hash table
5177 later. */
f6e332e6 5178 h->u.elf_hash_value = ha;
5a580b3a
AM
5179
5180 if (alc != NULL)
5181 free (alc);
5182
5183 return TRUE;
5184}
5185
fdc90cb4
JJ
5186struct collect_gnu_hash_codes
5187{
5188 bfd *output_bfd;
5189 const struct elf_backend_data *bed;
5190 unsigned long int nsyms;
5191 unsigned long int maskbits;
5192 unsigned long int *hashcodes;
5193 unsigned long int *hashval;
5194 unsigned long int *indx;
5195 unsigned long int *counts;
5196 bfd_vma *bitmask;
5197 bfd_byte *contents;
5198 long int min_dynindx;
5199 unsigned long int bucketcount;
5200 unsigned long int symindx;
5201 long int local_indx;
5202 long int shift1, shift2;
5203 unsigned long int mask;
14b1c01e 5204 bfd_boolean error;
fdc90cb4
JJ
5205};
5206
5207/* This function will be called though elf_link_hash_traverse to store
5208 all hash value of the exported symbols in an array. */
5209
5210static bfd_boolean
5211elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5212{
a50b1753 5213 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5214 const char *name;
5215 char *p;
5216 unsigned long ha;
5217 char *alc = NULL;
5218
fdc90cb4
JJ
5219 /* Ignore indirect symbols. These are added by the versioning code. */
5220 if (h->dynindx == -1)
5221 return TRUE;
5222
5223 /* Ignore also local symbols and undefined symbols. */
5224 if (! (*s->bed->elf_hash_symbol) (h))
5225 return TRUE;
5226
5227 name = h->root.root.string;
5228 p = strchr (name, ELF_VER_CHR);
5229 if (p != NULL)
5230 {
a50b1753 5231 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5232 if (alc == NULL)
5233 {
5234 s->error = TRUE;
5235 return FALSE;
5236 }
fdc90cb4
JJ
5237 memcpy (alc, name, p - name);
5238 alc[p - name] = '\0';
5239 name = alc;
5240 }
5241
5242 /* Compute the hash value. */
5243 ha = bfd_elf_gnu_hash (name);
5244
5245 /* Store the found hash value in the array for compute_bucket_count,
5246 and also for .dynsym reordering purposes. */
5247 s->hashcodes[s->nsyms] = ha;
5248 s->hashval[h->dynindx] = ha;
5249 ++s->nsyms;
5250 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5251 s->min_dynindx = h->dynindx;
5252
5253 if (alc != NULL)
5254 free (alc);
5255
5256 return TRUE;
5257}
5258
5259/* This function will be called though elf_link_hash_traverse to do
5260 final dynaminc symbol renumbering. */
5261
5262static bfd_boolean
5263elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5264{
a50b1753 5265 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5266 unsigned long int bucket;
5267 unsigned long int val;
5268
fdc90cb4
JJ
5269 /* Ignore indirect symbols. */
5270 if (h->dynindx == -1)
5271 return TRUE;
5272
5273 /* Ignore also local symbols and undefined symbols. */
5274 if (! (*s->bed->elf_hash_symbol) (h))
5275 {
5276 if (h->dynindx >= s->min_dynindx)
5277 h->dynindx = s->local_indx++;
5278 return TRUE;
5279 }
5280
5281 bucket = s->hashval[h->dynindx] % s->bucketcount;
5282 val = (s->hashval[h->dynindx] >> s->shift1)
5283 & ((s->maskbits >> s->shift1) - 1);
5284 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5285 s->bitmask[val]
5286 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5287 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5288 if (s->counts[bucket] == 1)
5289 /* Last element terminates the chain. */
5290 val |= 1;
5291 bfd_put_32 (s->output_bfd, val,
5292 s->contents + (s->indx[bucket] - s->symindx) * 4);
5293 --s->counts[bucket];
5294 h->dynindx = s->indx[bucket]++;
5295 return TRUE;
5296}
5297
5298/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5299
5300bfd_boolean
5301_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5302{
5303 return !(h->forced_local
5304 || h->root.type == bfd_link_hash_undefined
5305 || h->root.type == bfd_link_hash_undefweak
5306 || ((h->root.type == bfd_link_hash_defined
5307 || h->root.type == bfd_link_hash_defweak)
5308 && h->root.u.def.section->output_section == NULL));
5309}
5310
5a580b3a
AM
5311/* Array used to determine the number of hash table buckets to use
5312 based on the number of symbols there are. If there are fewer than
5313 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5314 fewer than 37 we use 17 buckets, and so forth. We never use more
5315 than 32771 buckets. */
5316
5317static const size_t elf_buckets[] =
5318{
5319 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5320 16411, 32771, 0
5321};
5322
5323/* Compute bucket count for hashing table. We do not use a static set
5324 of possible tables sizes anymore. Instead we determine for all
5325 possible reasonable sizes of the table the outcome (i.e., the
5326 number of collisions etc) and choose the best solution. The
5327 weighting functions are not too simple to allow the table to grow
5328 without bounds. Instead one of the weighting factors is the size.
5329 Therefore the result is always a good payoff between few collisions
5330 (= short chain lengths) and table size. */
5331static size_t
b20dd2ce 5332compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5333 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5334 unsigned long int nsyms,
5335 int gnu_hash)
5a580b3a 5336{
5a580b3a 5337 size_t best_size = 0;
5a580b3a 5338 unsigned long int i;
5a580b3a 5339
5a580b3a
AM
5340 /* We have a problem here. The following code to optimize the table
5341 size requires an integer type with more the 32 bits. If
5342 BFD_HOST_U_64_BIT is set we know about such a type. */
5343#ifdef BFD_HOST_U_64_BIT
5344 if (info->optimize)
5345 {
5a580b3a
AM
5346 size_t minsize;
5347 size_t maxsize;
5348 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5349 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5350 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5351 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5352 unsigned long int *counts;
d40f3da9 5353 bfd_size_type amt;
0883b6e0 5354 unsigned int no_improvement_count = 0;
5a580b3a
AM
5355
5356 /* Possible optimization parameters: if we have NSYMS symbols we say
5357 that the hashing table must at least have NSYMS/4 and at most
5358 2*NSYMS buckets. */
5359 minsize = nsyms / 4;
5360 if (minsize == 0)
5361 minsize = 1;
5362 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5363 if (gnu_hash)
5364 {
5365 if (minsize < 2)
5366 minsize = 2;
5367 if ((best_size & 31) == 0)
5368 ++best_size;
5369 }
5a580b3a
AM
5370
5371 /* Create array where we count the collisions in. We must use bfd_malloc
5372 since the size could be large. */
5373 amt = maxsize;
5374 amt *= sizeof (unsigned long int);
a50b1753 5375 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5376 if (counts == NULL)
fdc90cb4 5377 return 0;
5a580b3a
AM
5378
5379 /* Compute the "optimal" size for the hash table. The criteria is a
5380 minimal chain length. The minor criteria is (of course) the size
5381 of the table. */
5382 for (i = minsize; i < maxsize; ++i)
5383 {
5384 /* Walk through the array of hashcodes and count the collisions. */
5385 BFD_HOST_U_64_BIT max;
5386 unsigned long int j;
5387 unsigned long int fact;
5388
fdc90cb4
JJ
5389 if (gnu_hash && (i & 31) == 0)
5390 continue;
5391
5a580b3a
AM
5392 memset (counts, '\0', i * sizeof (unsigned long int));
5393
5394 /* Determine how often each hash bucket is used. */
5395 for (j = 0; j < nsyms; ++j)
5396 ++counts[hashcodes[j] % i];
5397
5398 /* For the weight function we need some information about the
5399 pagesize on the target. This is information need not be 100%
5400 accurate. Since this information is not available (so far) we
5401 define it here to a reasonable default value. If it is crucial
5402 to have a better value some day simply define this value. */
5403# ifndef BFD_TARGET_PAGESIZE
5404# define BFD_TARGET_PAGESIZE (4096)
5405# endif
5406
fdc90cb4
JJ
5407 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5408 and the chains. */
5409 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5410
5411# if 1
5412 /* Variant 1: optimize for short chains. We add the squares
5413 of all the chain lengths (which favors many small chain
5414 over a few long chains). */
5415 for (j = 0; j < i; ++j)
5416 max += counts[j] * counts[j];
5417
5418 /* This adds penalties for the overall size of the table. */
fdc90cb4 5419 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5420 max *= fact * fact;
5421# else
5422 /* Variant 2: Optimize a lot more for small table. Here we
5423 also add squares of the size but we also add penalties for
5424 empty slots (the +1 term). */
5425 for (j = 0; j < i; ++j)
5426 max += (1 + counts[j]) * (1 + counts[j]);
5427
5428 /* The overall size of the table is considered, but not as
5429 strong as in variant 1, where it is squared. */
fdc90cb4 5430 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5431 max *= fact;
5432# endif
5433
5434 /* Compare with current best results. */
5435 if (max < best_chlen)
5436 {
5437 best_chlen = max;
5438 best_size = i;
0883b6e0 5439 no_improvement_count = 0;
5a580b3a 5440 }
0883b6e0
NC
5441 /* PR 11843: Avoid futile long searches for the best bucket size
5442 when there are a large number of symbols. */
5443 else if (++no_improvement_count == 100)
5444 break;
5a580b3a
AM
5445 }
5446
5447 free (counts);
5448 }
5449 else
5450#endif /* defined (BFD_HOST_U_64_BIT) */
5451 {
5452 /* This is the fallback solution if no 64bit type is available or if we
5453 are not supposed to spend much time on optimizations. We select the
5454 bucket count using a fixed set of numbers. */
5455 for (i = 0; elf_buckets[i] != 0; i++)
5456 {
5457 best_size = elf_buckets[i];
fdc90cb4 5458 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5459 break;
5460 }
fdc90cb4
JJ
5461 if (gnu_hash && best_size < 2)
5462 best_size = 2;
5a580b3a
AM
5463 }
5464
5a580b3a
AM
5465 return best_size;
5466}
5467
d0bf826b
AM
5468/* Size any SHT_GROUP section for ld -r. */
5469
5470bfd_boolean
5471_bfd_elf_size_group_sections (struct bfd_link_info *info)
5472{
5473 bfd *ibfd;
5474
5475 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5476 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5477 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5478 return FALSE;
5479 return TRUE;
5480}
5481
5a580b3a
AM
5482/* Set up the sizes and contents of the ELF dynamic sections. This is
5483 called by the ELF linker emulation before_allocation routine. We
5484 must set the sizes of the sections before the linker sets the
5485 addresses of the various sections. */
5486
5487bfd_boolean
5488bfd_elf_size_dynamic_sections (bfd *output_bfd,
5489 const char *soname,
5490 const char *rpath,
5491 const char *filter_shlib,
7ee314fa
AM
5492 const char *audit,
5493 const char *depaudit,
5a580b3a
AM
5494 const char * const *auxiliary_filters,
5495 struct bfd_link_info *info,
5496 asection **sinterpptr,
5497 struct bfd_elf_version_tree *verdefs)
5498{
5499 bfd_size_type soname_indx;
5500 bfd *dynobj;
5501 const struct elf_backend_data *bed;
28caa186 5502 struct elf_info_failed asvinfo;
5a580b3a
AM
5503
5504 *sinterpptr = NULL;
5505
5506 soname_indx = (bfd_size_type) -1;
5507
5508 if (!is_elf_hash_table (info->hash))
5509 return TRUE;
5510
6bfdb61b 5511 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5512 if (info->execstack)
5513 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5514 else if (info->noexecstack)
5515 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5516 else
5517 {
5518 bfd *inputobj;
5519 asection *notesec = NULL;
5520 int exec = 0;
5521
5522 for (inputobj = info->input_bfds;
5523 inputobj;
5524 inputobj = inputobj->link_next)
5525 {
5526 asection *s;
5527
a92c088a
L
5528 if (inputobj->flags
5529 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5530 continue;
5531 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5532 if (s)
5533 {
5534 if (s->flags & SEC_CODE)
5535 exec = PF_X;
5536 notesec = s;
5537 }
6bfdb61b 5538 else if (bed->default_execstack)
5a580b3a
AM
5539 exec = PF_X;
5540 }
5541 if (notesec)
5542 {
5543 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5544 if (exec && info->relocatable
5545 && notesec->output_section != bfd_abs_section_ptr)
5546 notesec->output_section->flags |= SEC_CODE;
5547 }
5548 }
5549
5550 /* Any syms created from now on start with -1 in
5551 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5552 elf_hash_table (info)->init_got_refcount
5553 = elf_hash_table (info)->init_got_offset;
5554 elf_hash_table (info)->init_plt_refcount
5555 = elf_hash_table (info)->init_plt_offset;
5a580b3a 5556
d0bf826b
AM
5557 if (info->relocatable
5558 && !_bfd_elf_size_group_sections (info))
5559 return FALSE;
5560
5a580b3a
AM
5561 /* The backend may have to create some sections regardless of whether
5562 we're dynamic or not. */
5a580b3a
AM
5563 if (bed->elf_backend_always_size_sections
5564 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5565 return FALSE;
5566
eb3d5f3b
JB
5567 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5568 return FALSE;
5569
5a580b3a
AM
5570 dynobj = elf_hash_table (info)->dynobj;
5571
5572 /* If there were no dynamic objects in the link, there is nothing to
5573 do here. */
5574 if (dynobj == NULL)
5575 return TRUE;
5576
5a580b3a
AM
5577 if (elf_hash_table (info)->dynamic_sections_created)
5578 {
5579 struct elf_info_failed eif;
5580 struct elf_link_hash_entry *h;
5581 asection *dynstr;
5582 struct bfd_elf_version_tree *t;
5583 struct bfd_elf_version_expr *d;
046183de 5584 asection *s;
5a580b3a
AM
5585 bfd_boolean all_defined;
5586
5587 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5588 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5589
5590 if (soname != NULL)
5591 {
5592 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5593 soname, TRUE);
5594 if (soname_indx == (bfd_size_type) -1
5595 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5596 return FALSE;
5597 }
5598
5599 if (info->symbolic)
5600 {
5601 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5602 return FALSE;
5603 info->flags |= DF_SYMBOLIC;
5604 }
5605
5606 if (rpath != NULL)
5607 {
5608 bfd_size_type indx;
5609
5610 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5611 TRUE);
5612 if (indx == (bfd_size_type) -1
5613 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5614 return FALSE;
5615
5616 if (info->new_dtags)
5617 {
5618 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5619 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5620 return FALSE;
5621 }
5622 }
5623
5624 if (filter_shlib != NULL)
5625 {
5626 bfd_size_type indx;
5627
5628 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5629 filter_shlib, TRUE);
5630 if (indx == (bfd_size_type) -1
5631 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5632 return FALSE;
5633 }
5634
5635 if (auxiliary_filters != NULL)
5636 {
5637 const char * const *p;
5638
5639 for (p = auxiliary_filters; *p != NULL; p++)
5640 {
5641 bfd_size_type indx;
5642
5643 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5644 *p, TRUE);
5645 if (indx == (bfd_size_type) -1
5646 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5647 return FALSE;
5648 }
5649 }
5650
7ee314fa
AM
5651 if (audit != NULL)
5652 {
5653 bfd_size_type indx;
5654
5655 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5656 TRUE);
5657 if (indx == (bfd_size_type) -1
5658 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5659 return FALSE;
5660 }
5661
5662 if (depaudit != NULL)
5663 {
5664 bfd_size_type indx;
5665
5666 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5667 TRUE);
5668 if (indx == (bfd_size_type) -1
5669 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5670 return FALSE;
5671 }
5672
5a580b3a
AM
5673 eif.info = info;
5674 eif.verdefs = verdefs;
5675 eif.failed = FALSE;
5676
5677 /* If we are supposed to export all symbols into the dynamic symbol
5678 table (this is not the normal case), then do so. */
55255dae
L
5679 if (info->export_dynamic
5680 || (info->executable && info->dynamic))
5a580b3a
AM
5681 {
5682 elf_link_hash_traverse (elf_hash_table (info),
5683 _bfd_elf_export_symbol,
5684 &eif);
5685 if (eif.failed)
5686 return FALSE;
5687 }
5688
5689 /* Make all global versions with definition. */
5690 for (t = verdefs; t != NULL; t = t->next)
5691 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5692 if (!d->symver && d->literal)
5a580b3a
AM
5693 {
5694 const char *verstr, *name;
5695 size_t namelen, verlen, newlen;
93252b1c 5696 char *newname, *p, leading_char;
5a580b3a
AM
5697 struct elf_link_hash_entry *newh;
5698
93252b1c 5699 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5700 name = d->pattern;
93252b1c 5701 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5702 verstr = t->name;
5703 verlen = strlen (verstr);
5704 newlen = namelen + verlen + 3;
5705
a50b1753 5706 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5707 if (newname == NULL)
5708 return FALSE;
93252b1c
MF
5709 newname[0] = leading_char;
5710 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5711
5712 /* Check the hidden versioned definition. */
5713 p = newname + namelen;
5714 *p++ = ELF_VER_CHR;
5715 memcpy (p, verstr, verlen + 1);
5716 newh = elf_link_hash_lookup (elf_hash_table (info),
5717 newname, FALSE, FALSE,
5718 FALSE);
5719 if (newh == NULL
5720 || (newh->root.type != bfd_link_hash_defined
5721 && newh->root.type != bfd_link_hash_defweak))
5722 {
5723 /* Check the default versioned definition. */
5724 *p++ = ELF_VER_CHR;
5725 memcpy (p, verstr, verlen + 1);
5726 newh = elf_link_hash_lookup (elf_hash_table (info),
5727 newname, FALSE, FALSE,
5728 FALSE);
5729 }
5730 free (newname);
5731
5732 /* Mark this version if there is a definition and it is
5733 not defined in a shared object. */
5734 if (newh != NULL
f5385ebf 5735 && !newh->def_dynamic
5a580b3a
AM
5736 && (newh->root.type == bfd_link_hash_defined
5737 || newh->root.type == bfd_link_hash_defweak))
5738 d->symver = 1;
5739 }
5740
5741 /* Attach all the symbols to their version information. */
5a580b3a
AM
5742 asvinfo.info = info;
5743 asvinfo.verdefs = verdefs;
5744 asvinfo.failed = FALSE;
5745
5746 elf_link_hash_traverse (elf_hash_table (info),
5747 _bfd_elf_link_assign_sym_version,
5748 &asvinfo);
5749 if (asvinfo.failed)
5750 return FALSE;
5751
5752 if (!info->allow_undefined_version)
5753 {
5754 /* Check if all global versions have a definition. */
5755 all_defined = TRUE;
5756 for (t = verdefs; t != NULL; t = t->next)
5757 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5758 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5759 {
5760 (*_bfd_error_handler)
5761 (_("%s: undefined version: %s"),
5762 d->pattern, t->name);
5763 all_defined = FALSE;
5764 }
5765
5766 if (!all_defined)
5767 {
5768 bfd_set_error (bfd_error_bad_value);
5769 return FALSE;
5770 }
5771 }
5772
5773 /* Find all symbols which were defined in a dynamic object and make
5774 the backend pick a reasonable value for them. */
5775 elf_link_hash_traverse (elf_hash_table (info),
5776 _bfd_elf_adjust_dynamic_symbol,
5777 &eif);
5778 if (eif.failed)
5779 return FALSE;
5780
5781 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5782 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5783 now so that we know the final size of the .dynamic section. */
5784
5785 /* If there are initialization and/or finalization functions to
5786 call then add the corresponding DT_INIT/DT_FINI entries. */
5787 h = (info->init_function
5788 ? elf_link_hash_lookup (elf_hash_table (info),
5789 info->init_function, FALSE,
5790 FALSE, FALSE)
5791 : NULL);
5792 if (h != NULL
f5385ebf
AM
5793 && (h->ref_regular
5794 || h->def_regular))
5a580b3a
AM
5795 {
5796 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5797 return FALSE;
5798 }
5799 h = (info->fini_function
5800 ? elf_link_hash_lookup (elf_hash_table (info),
5801 info->fini_function, FALSE,
5802 FALSE, FALSE)
5803 : NULL);
5804 if (h != NULL
f5385ebf
AM
5805 && (h->ref_regular
5806 || h->def_regular))
5a580b3a
AM
5807 {
5808 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5809 return FALSE;
5810 }
5811
046183de
AM
5812 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5813 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5814 {
5815 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5816 if (! info->executable)
5817 {
5818 bfd *sub;
5819 asection *o;
5820
5821 for (sub = info->input_bfds; sub != NULL;
5822 sub = sub->link_next)
3fcd97f1
JJ
5823 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5824 for (o = sub->sections; o != NULL; o = o->next)
5825 if (elf_section_data (o)->this_hdr.sh_type
5826 == SHT_PREINIT_ARRAY)
5827 {
5828 (*_bfd_error_handler)
5829 (_("%B: .preinit_array section is not allowed in DSO"),
5830 sub);
5831 break;
5832 }
5a580b3a
AM
5833
5834 bfd_set_error (bfd_error_nonrepresentable_section);
5835 return FALSE;
5836 }
5837
5838 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5839 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5840 return FALSE;
5841 }
046183de
AM
5842 s = bfd_get_section_by_name (output_bfd, ".init_array");
5843 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5844 {
5845 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5846 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5847 return FALSE;
5848 }
046183de
AM
5849 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5850 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5851 {
5852 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5853 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5854 return FALSE;
5855 }
5856
5857 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5858 /* If .dynstr is excluded from the link, we don't want any of
5859 these tags. Strictly, we should be checking each section
5860 individually; This quick check covers for the case where
5861 someone does a /DISCARD/ : { *(*) }. */
5862 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5863 {
5864 bfd_size_type strsize;
5865
5866 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5867 if ((info->emit_hash
5868 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5869 || (info->emit_gnu_hash
5870 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5871 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5872 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5873 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5874 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5875 bed->s->sizeof_sym))
5876 return FALSE;
5877 }
5878 }
5879
5880 /* The backend must work out the sizes of all the other dynamic
5881 sections. */
5882 if (bed->elf_backend_size_dynamic_sections
5883 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5884 return FALSE;
5885
5886 if (elf_hash_table (info)->dynamic_sections_created)
5887 {
554220db 5888 unsigned long section_sym_count;
5a580b3a 5889 asection *s;
5a580b3a
AM
5890
5891 /* Set up the version definition section. */
5892 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5893 BFD_ASSERT (s != NULL);
5894
5895 /* We may have created additional version definitions if we are
5896 just linking a regular application. */
5897 verdefs = asvinfo.verdefs;
5898
5899 /* Skip anonymous version tag. */
5900 if (verdefs != NULL && verdefs->vernum == 0)
5901 verdefs = verdefs->next;
5902
3e3b46e5 5903 if (verdefs == NULL && !info->create_default_symver)
8423293d 5904 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5905 else
5906 {
5907 unsigned int cdefs;
5908 bfd_size_type size;
5909 struct bfd_elf_version_tree *t;
5910 bfd_byte *p;
5911 Elf_Internal_Verdef def;
5912 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5913 struct bfd_link_hash_entry *bh;
5914 struct elf_link_hash_entry *h;
5915 const char *name;
5a580b3a
AM
5916
5917 cdefs = 0;
5918 size = 0;
5919
5920 /* Make space for the base version. */
5921 size += sizeof (Elf_External_Verdef);
5922 size += sizeof (Elf_External_Verdaux);
5923 ++cdefs;
5924
3e3b46e5
PB
5925 /* Make space for the default version. */
5926 if (info->create_default_symver)
5927 {
5928 size += sizeof (Elf_External_Verdef);
5929 ++cdefs;
5930 }
5931
5a580b3a
AM
5932 for (t = verdefs; t != NULL; t = t->next)
5933 {
5934 struct bfd_elf_version_deps *n;
5935
a6cc6b3b
RO
5936 /* Don't emit base version twice. */
5937 if (t->vernum == 0)
5938 continue;
5939
5a580b3a
AM
5940 size += sizeof (Elf_External_Verdef);
5941 size += sizeof (Elf_External_Verdaux);
5942 ++cdefs;
5943
5944 for (n = t->deps; n != NULL; n = n->next)
5945 size += sizeof (Elf_External_Verdaux);
5946 }
5947
eea6121a 5948 s->size = size;
a50b1753 5949 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 5950 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5951 return FALSE;
5952
5953 /* Fill in the version definition section. */
5954
5955 p = s->contents;
5956
5957 def.vd_version = VER_DEF_CURRENT;
5958 def.vd_flags = VER_FLG_BASE;
5959 def.vd_ndx = 1;
5960 def.vd_cnt = 1;
3e3b46e5
PB
5961 if (info->create_default_symver)
5962 {
5963 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5964 def.vd_next = sizeof (Elf_External_Verdef);
5965 }
5966 else
5967 {
5968 def.vd_aux = sizeof (Elf_External_Verdef);
5969 def.vd_next = (sizeof (Elf_External_Verdef)
5970 + sizeof (Elf_External_Verdaux));
5971 }
5a580b3a
AM
5972
5973 if (soname_indx != (bfd_size_type) -1)
5974 {
5975 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5976 soname_indx);
5977 def.vd_hash = bfd_elf_hash (soname);
5978 defaux.vda_name = soname_indx;
3e3b46e5 5979 name = soname;
5a580b3a
AM
5980 }
5981 else
5982 {
5a580b3a
AM
5983 bfd_size_type indx;
5984
06084812 5985 name = lbasename (output_bfd->filename);
5a580b3a
AM
5986 def.vd_hash = bfd_elf_hash (name);
5987 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5988 name, FALSE);
5989 if (indx == (bfd_size_type) -1)
5990 return FALSE;
5991 defaux.vda_name = indx;
5992 }
5993 defaux.vda_next = 0;
5994
5995 _bfd_elf_swap_verdef_out (output_bfd, &def,
5996 (Elf_External_Verdef *) p);
5997 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
5998 if (info->create_default_symver)
5999 {
6000 /* Add a symbol representing this version. */
6001 bh = NULL;
6002 if (! (_bfd_generic_link_add_one_symbol
6003 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6004 0, NULL, FALSE,
6005 get_elf_backend_data (dynobj)->collect, &bh)))
6006 return FALSE;
6007 h = (struct elf_link_hash_entry *) bh;
6008 h->non_elf = 0;
6009 h->def_regular = 1;
6010 h->type = STT_OBJECT;
6011 h->verinfo.vertree = NULL;
6012
6013 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6014 return FALSE;
6015
6016 /* Create a duplicate of the base version with the same
6017 aux block, but different flags. */
6018 def.vd_flags = 0;
6019 def.vd_ndx = 2;
6020 def.vd_aux = sizeof (Elf_External_Verdef);
6021 if (verdefs)
6022 def.vd_next = (sizeof (Elf_External_Verdef)
6023 + sizeof (Elf_External_Verdaux));
6024 else
6025 def.vd_next = 0;
6026 _bfd_elf_swap_verdef_out (output_bfd, &def,
6027 (Elf_External_Verdef *) p);
6028 p += sizeof (Elf_External_Verdef);
6029 }
5a580b3a
AM
6030 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6031 (Elf_External_Verdaux *) p);
6032 p += sizeof (Elf_External_Verdaux);
6033
6034 for (t = verdefs; t != NULL; t = t->next)
6035 {
6036 unsigned int cdeps;
6037 struct bfd_elf_version_deps *n;
5a580b3a 6038
a6cc6b3b
RO
6039 /* Don't emit the base version twice. */
6040 if (t->vernum == 0)
6041 continue;
6042
5a580b3a
AM
6043 cdeps = 0;
6044 for (n = t->deps; n != NULL; n = n->next)
6045 ++cdeps;
6046
6047 /* Add a symbol representing this version. */
6048 bh = NULL;
6049 if (! (_bfd_generic_link_add_one_symbol
6050 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6051 0, NULL, FALSE,
6052 get_elf_backend_data (dynobj)->collect, &bh)))
6053 return FALSE;
6054 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6055 h->non_elf = 0;
6056 h->def_regular = 1;
5a580b3a
AM
6057 h->type = STT_OBJECT;
6058 h->verinfo.vertree = t;
6059
c152c796 6060 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6061 return FALSE;
6062
6063 def.vd_version = VER_DEF_CURRENT;
6064 def.vd_flags = 0;
6065 if (t->globals.list == NULL
6066 && t->locals.list == NULL
6067 && ! t->used)
6068 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6069 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6070 def.vd_cnt = cdeps + 1;
6071 def.vd_hash = bfd_elf_hash (t->name);
6072 def.vd_aux = sizeof (Elf_External_Verdef);
6073 def.vd_next = 0;
a6cc6b3b
RO
6074
6075 /* If a basever node is next, it *must* be the last node in
6076 the chain, otherwise Verdef construction breaks. */
6077 if (t->next != NULL && t->next->vernum == 0)
6078 BFD_ASSERT (t->next->next == NULL);
6079
6080 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6081 def.vd_next = (sizeof (Elf_External_Verdef)
6082 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6083
6084 _bfd_elf_swap_verdef_out (output_bfd, &def,
6085 (Elf_External_Verdef *) p);
6086 p += sizeof (Elf_External_Verdef);
6087
6088 defaux.vda_name = h->dynstr_index;
6089 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6090 h->dynstr_index);
6091 defaux.vda_next = 0;
6092 if (t->deps != NULL)
6093 defaux.vda_next = sizeof (Elf_External_Verdaux);
6094 t->name_indx = defaux.vda_name;
6095
6096 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6097 (Elf_External_Verdaux *) p);
6098 p += sizeof (Elf_External_Verdaux);
6099
6100 for (n = t->deps; n != NULL; n = n->next)
6101 {
6102 if (n->version_needed == NULL)
6103 {
6104 /* This can happen if there was an error in the
6105 version script. */
6106 defaux.vda_name = 0;
6107 }
6108 else
6109 {
6110 defaux.vda_name = n->version_needed->name_indx;
6111 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6112 defaux.vda_name);
6113 }
6114 if (n->next == NULL)
6115 defaux.vda_next = 0;
6116 else
6117 defaux.vda_next = sizeof (Elf_External_Verdaux);
6118
6119 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6120 (Elf_External_Verdaux *) p);
6121 p += sizeof (Elf_External_Verdaux);
6122 }
6123 }
6124
6125 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6126 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6127 return FALSE;
6128
6129 elf_tdata (output_bfd)->cverdefs = cdefs;
6130 }
6131
6132 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6133 {
6134 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6135 return FALSE;
6136 }
6137 else if (info->flags & DF_BIND_NOW)
6138 {
6139 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6140 return FALSE;
6141 }
6142
6143 if (info->flags_1)
6144 {
6145 if (info->executable)
6146 info->flags_1 &= ~ (DF_1_INITFIRST
6147 | DF_1_NODELETE
6148 | DF_1_NOOPEN);
6149 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6150 return FALSE;
6151 }
6152
6153 /* Work out the size of the version reference section. */
6154
6155 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6156 BFD_ASSERT (s != NULL);
6157 {
6158 struct elf_find_verdep_info sinfo;
6159
5a580b3a
AM
6160 sinfo.info = info;
6161 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6162 if (sinfo.vers == 0)
6163 sinfo.vers = 1;
6164 sinfo.failed = FALSE;
6165
6166 elf_link_hash_traverse (elf_hash_table (info),
6167 _bfd_elf_link_find_version_dependencies,
6168 &sinfo);
14b1c01e
AM
6169 if (sinfo.failed)
6170 return FALSE;
5a580b3a
AM
6171
6172 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6173 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6174 else
6175 {
6176 Elf_Internal_Verneed *t;
6177 unsigned int size;
6178 unsigned int crefs;
6179 bfd_byte *p;
6180
a6cc6b3b 6181 /* Build the version dependency section. */
5a580b3a
AM
6182 size = 0;
6183 crefs = 0;
6184 for (t = elf_tdata (output_bfd)->verref;
6185 t != NULL;
6186 t = t->vn_nextref)
6187 {
6188 Elf_Internal_Vernaux *a;
6189
6190 size += sizeof (Elf_External_Verneed);
6191 ++crefs;
6192 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6193 size += sizeof (Elf_External_Vernaux);
6194 }
6195
eea6121a 6196 s->size = size;
a50b1753 6197 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6198 if (s->contents == NULL)
6199 return FALSE;
6200
6201 p = s->contents;
6202 for (t = elf_tdata (output_bfd)->verref;
6203 t != NULL;
6204 t = t->vn_nextref)
6205 {
6206 unsigned int caux;
6207 Elf_Internal_Vernaux *a;
6208 bfd_size_type indx;
6209
6210 caux = 0;
6211 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6212 ++caux;
6213
6214 t->vn_version = VER_NEED_CURRENT;
6215 t->vn_cnt = caux;
6216 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6217 elf_dt_name (t->vn_bfd) != NULL
6218 ? elf_dt_name (t->vn_bfd)
06084812 6219 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6220 FALSE);
6221 if (indx == (bfd_size_type) -1)
6222 return FALSE;
6223 t->vn_file = indx;
6224 t->vn_aux = sizeof (Elf_External_Verneed);
6225 if (t->vn_nextref == NULL)
6226 t->vn_next = 0;
6227 else
6228 t->vn_next = (sizeof (Elf_External_Verneed)
6229 + caux * sizeof (Elf_External_Vernaux));
6230
6231 _bfd_elf_swap_verneed_out (output_bfd, t,
6232 (Elf_External_Verneed *) p);
6233 p += sizeof (Elf_External_Verneed);
6234
6235 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6236 {
6237 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6238 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6239 a->vna_nodename, FALSE);
6240 if (indx == (bfd_size_type) -1)
6241 return FALSE;
6242 a->vna_name = indx;
6243 if (a->vna_nextptr == NULL)
6244 a->vna_next = 0;
6245 else
6246 a->vna_next = sizeof (Elf_External_Vernaux);
6247
6248 _bfd_elf_swap_vernaux_out (output_bfd, a,
6249 (Elf_External_Vernaux *) p);
6250 p += sizeof (Elf_External_Vernaux);
6251 }
6252 }
6253
6254 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6255 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6256 return FALSE;
6257
6258 elf_tdata (output_bfd)->cverrefs = crefs;
6259 }
6260 }
6261
8423293d
AM
6262 if ((elf_tdata (output_bfd)->cverrefs == 0
6263 && elf_tdata (output_bfd)->cverdefs == 0)
6264 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6265 &section_sym_count) == 0)
6266 {
6267 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6268 s->flags |= SEC_EXCLUDE;
6269 }
6270 }
6271 return TRUE;
6272}
6273
74541ad4
AM
6274/* Find the first non-excluded output section. We'll use its
6275 section symbol for some emitted relocs. */
6276void
6277_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6278{
6279 asection *s;
6280
6281 for (s = output_bfd->sections; s != NULL; s = s->next)
6282 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6283 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6284 {
6285 elf_hash_table (info)->text_index_section = s;
6286 break;
6287 }
6288}
6289
6290/* Find two non-excluded output sections, one for code, one for data.
6291 We'll use their section symbols for some emitted relocs. */
6292void
6293_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6294{
6295 asection *s;
6296
266b05cf
DJ
6297 /* Data first, since setting text_index_section changes
6298 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6299 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6300 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6301 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6302 {
266b05cf 6303 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6304 break;
6305 }
6306
6307 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6308 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6309 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6310 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6311 {
266b05cf 6312 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6313 break;
6314 }
6315
6316 if (elf_hash_table (info)->text_index_section == NULL)
6317 elf_hash_table (info)->text_index_section
6318 = elf_hash_table (info)->data_index_section;
6319}
6320
8423293d
AM
6321bfd_boolean
6322bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6323{
74541ad4
AM
6324 const struct elf_backend_data *bed;
6325
8423293d
AM
6326 if (!is_elf_hash_table (info->hash))
6327 return TRUE;
6328
74541ad4
AM
6329 bed = get_elf_backend_data (output_bfd);
6330 (*bed->elf_backend_init_index_section) (output_bfd, info);
6331
8423293d
AM
6332 if (elf_hash_table (info)->dynamic_sections_created)
6333 {
6334 bfd *dynobj;
8423293d
AM
6335 asection *s;
6336 bfd_size_type dynsymcount;
6337 unsigned long section_sym_count;
8423293d
AM
6338 unsigned int dtagcount;
6339
6340 dynobj = elf_hash_table (info)->dynobj;
6341
5a580b3a
AM
6342 /* Assign dynsym indicies. In a shared library we generate a
6343 section symbol for each output section, which come first.
6344 Next come all of the back-end allocated local dynamic syms,
6345 followed by the rest of the global symbols. */
6346
554220db
AM
6347 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6348 &section_sym_count);
5a580b3a
AM
6349
6350 /* Work out the size of the symbol version section. */
6351 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6352 BFD_ASSERT (s != NULL);
8423293d
AM
6353 if (dynsymcount != 0
6354 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6355 {
eea6121a 6356 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6357 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6358 if (s->contents == NULL)
6359 return FALSE;
6360
6361 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6362 return FALSE;
6363 }
6364
6365 /* Set the size of the .dynsym and .hash sections. We counted
6366 the number of dynamic symbols in elf_link_add_object_symbols.
6367 We will build the contents of .dynsym and .hash when we build
6368 the final symbol table, because until then we do not know the
6369 correct value to give the symbols. We built the .dynstr
6370 section as we went along in elf_link_add_object_symbols. */
6371 s = bfd_get_section_by_name (dynobj, ".dynsym");
6372 BFD_ASSERT (s != NULL);
eea6121a 6373 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6374
6375 if (dynsymcount != 0)
6376 {
a50b1753 6377 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6378 if (s->contents == NULL)
6379 return FALSE;
5a580b3a 6380
554220db
AM
6381 /* The first entry in .dynsym is a dummy symbol.
6382 Clear all the section syms, in case we don't output them all. */
6383 ++section_sym_count;
6384 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6385 }
6386
fdc90cb4
JJ
6387 elf_hash_table (info)->bucketcount = 0;
6388
5a580b3a
AM
6389 /* Compute the size of the hashing table. As a side effect this
6390 computes the hash values for all the names we export. */
fdc90cb4
JJ
6391 if (info->emit_hash)
6392 {
6393 unsigned long int *hashcodes;
14b1c01e 6394 struct hash_codes_info hashinf;
fdc90cb4
JJ
6395 bfd_size_type amt;
6396 unsigned long int nsyms;
6397 size_t bucketcount;
6398 size_t hash_entry_size;
6399
6400 /* Compute the hash values for all exported symbols. At the same
6401 time store the values in an array so that we could use them for
6402 optimizations. */
6403 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6404 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6405 if (hashcodes == NULL)
6406 return FALSE;
14b1c01e
AM
6407 hashinf.hashcodes = hashcodes;
6408 hashinf.error = FALSE;
5a580b3a 6409
fdc90cb4
JJ
6410 /* Put all hash values in HASHCODES. */
6411 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6412 elf_collect_hash_codes, &hashinf);
6413 if (hashinf.error)
4dd07732
AM
6414 {
6415 free (hashcodes);
6416 return FALSE;
6417 }
5a580b3a 6418
14b1c01e 6419 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6420 bucketcount
6421 = compute_bucket_count (info, hashcodes, nsyms, 0);
6422 free (hashcodes);
6423
6424 if (bucketcount == 0)
6425 return FALSE;
5a580b3a 6426
fdc90cb4
JJ
6427 elf_hash_table (info)->bucketcount = bucketcount;
6428
6429 s = bfd_get_section_by_name (dynobj, ".hash");
6430 BFD_ASSERT (s != NULL);
6431 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6432 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6433 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6434 if (s->contents == NULL)
6435 return FALSE;
6436
6437 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6438 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6439 s->contents + hash_entry_size);
6440 }
6441
6442 if (info->emit_gnu_hash)
6443 {
6444 size_t i, cnt;
6445 unsigned char *contents;
6446 struct collect_gnu_hash_codes cinfo;
6447 bfd_size_type amt;
6448 size_t bucketcount;
6449
6450 memset (&cinfo, 0, sizeof (cinfo));
6451
6452 /* Compute the hash values for all exported symbols. At the same
6453 time store the values in an array so that we could use them for
6454 optimizations. */
6455 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6456 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6457 if (cinfo.hashcodes == NULL)
6458 return FALSE;
6459
6460 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6461 cinfo.min_dynindx = -1;
6462 cinfo.output_bfd = output_bfd;
6463 cinfo.bed = bed;
6464
6465 /* Put all hash values in HASHCODES. */
6466 elf_link_hash_traverse (elf_hash_table (info),
6467 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6468 if (cinfo.error)
4dd07732
AM
6469 {
6470 free (cinfo.hashcodes);
6471 return FALSE;
6472 }
fdc90cb4
JJ
6473
6474 bucketcount
6475 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6476
6477 if (bucketcount == 0)
6478 {
6479 free (cinfo.hashcodes);
6480 return FALSE;
6481 }
6482
6483 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6484 BFD_ASSERT (s != NULL);
6485
6486 if (cinfo.nsyms == 0)
6487 {
6488 /* Empty .gnu.hash section is special. */
6489 BFD_ASSERT (cinfo.min_dynindx == -1);
6490 free (cinfo.hashcodes);
6491 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6492 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6493 if (contents == NULL)
6494 return FALSE;
6495 s->contents = contents;
6496 /* 1 empty bucket. */
6497 bfd_put_32 (output_bfd, 1, contents);
6498 /* SYMIDX above the special symbol 0. */
6499 bfd_put_32 (output_bfd, 1, contents + 4);
6500 /* Just one word for bitmask. */
6501 bfd_put_32 (output_bfd, 1, contents + 8);
6502 /* Only hash fn bloom filter. */
6503 bfd_put_32 (output_bfd, 0, contents + 12);
6504 /* No hashes are valid - empty bitmask. */
6505 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6506 /* No hashes in the only bucket. */
6507 bfd_put_32 (output_bfd, 0,
6508 contents + 16 + bed->s->arch_size / 8);
6509 }
6510 else
6511 {
9e6619e2 6512 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6513 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6514
9e6619e2
AM
6515 x = cinfo.nsyms;
6516 maskbitslog2 = 1;
6517 while ((x >>= 1) != 0)
6518 ++maskbitslog2;
fdc90cb4
JJ
6519 if (maskbitslog2 < 3)
6520 maskbitslog2 = 5;
6521 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6522 maskbitslog2 = maskbitslog2 + 3;
6523 else
6524 maskbitslog2 = maskbitslog2 + 2;
6525 if (bed->s->arch_size == 64)
6526 {
6527 if (maskbitslog2 == 5)
6528 maskbitslog2 = 6;
6529 cinfo.shift1 = 6;
6530 }
6531 else
6532 cinfo.shift1 = 5;
6533 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6534 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6535 cinfo.maskbits = 1 << maskbitslog2;
6536 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6537 amt = bucketcount * sizeof (unsigned long int) * 2;
6538 amt += maskwords * sizeof (bfd_vma);
a50b1753 6539 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6540 if (cinfo.bitmask == NULL)
6541 {
6542 free (cinfo.hashcodes);
6543 return FALSE;
6544 }
6545
a50b1753 6546 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6547 cinfo.indx = cinfo.counts + bucketcount;
6548 cinfo.symindx = dynsymcount - cinfo.nsyms;
6549 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6550
6551 /* Determine how often each hash bucket is used. */
6552 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6553 for (i = 0; i < cinfo.nsyms; ++i)
6554 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6555
6556 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6557 if (cinfo.counts[i] != 0)
6558 {
6559 cinfo.indx[i] = cnt;
6560 cnt += cinfo.counts[i];
6561 }
6562 BFD_ASSERT (cnt == dynsymcount);
6563 cinfo.bucketcount = bucketcount;
6564 cinfo.local_indx = cinfo.min_dynindx;
6565
6566 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6567 s->size += cinfo.maskbits / 8;
a50b1753 6568 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6569 if (contents == NULL)
6570 {
6571 free (cinfo.bitmask);
6572 free (cinfo.hashcodes);
6573 return FALSE;
6574 }
6575
6576 s->contents = contents;
6577 bfd_put_32 (output_bfd, bucketcount, contents);
6578 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6579 bfd_put_32 (output_bfd, maskwords, contents + 8);
6580 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6581 contents += 16 + cinfo.maskbits / 8;
6582
6583 for (i = 0; i < bucketcount; ++i)
6584 {
6585 if (cinfo.counts[i] == 0)
6586 bfd_put_32 (output_bfd, 0, contents);
6587 else
6588 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6589 contents += 4;
6590 }
6591
6592 cinfo.contents = contents;
6593
6594 /* Renumber dynamic symbols, populate .gnu.hash section. */
6595 elf_link_hash_traverse (elf_hash_table (info),
6596 elf_renumber_gnu_hash_syms, &cinfo);
6597
6598 contents = s->contents + 16;
6599 for (i = 0; i < maskwords; ++i)
6600 {
6601 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6602 contents);
6603 contents += bed->s->arch_size / 8;
6604 }
6605
6606 free (cinfo.bitmask);
6607 free (cinfo.hashcodes);
6608 }
6609 }
5a580b3a
AM
6610
6611 s = bfd_get_section_by_name (dynobj, ".dynstr");
6612 BFD_ASSERT (s != NULL);
6613
4ad4eba5 6614 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6615
eea6121a 6616 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6617
6618 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6619 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6620 return FALSE;
6621 }
6622
6623 return TRUE;
6624}
4d269e42
AM
6625\f
6626/* Indicate that we are only retrieving symbol values from this
6627 section. */
6628
6629void
6630_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6631{
6632 if (is_elf_hash_table (info->hash))
6633 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6634 _bfd_generic_link_just_syms (sec, info);
6635}
6636
6637/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6638
6639static void
6640merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6641 asection *sec)
6642{
6643 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6644 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6645}
6646
6647/* Finish SHF_MERGE section merging. */
6648
6649bfd_boolean
6650_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6651{
6652 bfd *ibfd;
6653 asection *sec;
6654
6655 if (!is_elf_hash_table (info->hash))
6656 return FALSE;
6657
6658 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6659 if ((ibfd->flags & DYNAMIC) == 0)
6660 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6661 if ((sec->flags & SEC_MERGE) != 0
6662 && !bfd_is_abs_section (sec->output_section))
6663 {
6664 struct bfd_elf_section_data *secdata;
6665
6666 secdata = elf_section_data (sec);
6667 if (! _bfd_add_merge_section (abfd,
6668 &elf_hash_table (info)->merge_info,
6669 sec, &secdata->sec_info))
6670 return FALSE;
6671 else if (secdata->sec_info)
6672 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6673 }
6674
6675 if (elf_hash_table (info)->merge_info != NULL)
6676 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6677 merge_sections_remove_hook);
6678 return TRUE;
6679}
6680
6681/* Create an entry in an ELF linker hash table. */
6682
6683struct bfd_hash_entry *
6684_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6685 struct bfd_hash_table *table,
6686 const char *string)
6687{
6688 /* Allocate the structure if it has not already been allocated by a
6689 subclass. */
6690 if (entry == NULL)
6691 {
a50b1753
NC
6692 entry = (struct bfd_hash_entry *)
6693 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6694 if (entry == NULL)
6695 return entry;
6696 }
6697
6698 /* Call the allocation method of the superclass. */
6699 entry = _bfd_link_hash_newfunc (entry, table, string);
6700 if (entry != NULL)
6701 {
6702 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6703 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6704
6705 /* Set local fields. */
6706 ret->indx = -1;
6707 ret->dynindx = -1;
6708 ret->got = htab->init_got_refcount;
6709 ret->plt = htab->init_plt_refcount;
6710 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6711 - offsetof (struct elf_link_hash_entry, size)));
6712 /* Assume that we have been called by a non-ELF symbol reader.
6713 This flag is then reset by the code which reads an ELF input
6714 file. This ensures that a symbol created by a non-ELF symbol
6715 reader will have the flag set correctly. */
6716 ret->non_elf = 1;
6717 }
6718
6719 return entry;
6720}
6721
6722/* Copy data from an indirect symbol to its direct symbol, hiding the
6723 old indirect symbol. Also used for copying flags to a weakdef. */
6724
6725void
6726_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6727 struct elf_link_hash_entry *dir,
6728 struct elf_link_hash_entry *ind)
6729{
6730 struct elf_link_hash_table *htab;
6731
6732 /* Copy down any references that we may have already seen to the
6733 symbol which just became indirect. */
6734
6735 dir->ref_dynamic |= ind->ref_dynamic;
6736 dir->ref_regular |= ind->ref_regular;
6737 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6738 dir->non_got_ref |= ind->non_got_ref;
6739 dir->needs_plt |= ind->needs_plt;
6740 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6741
6742 if (ind->root.type != bfd_link_hash_indirect)
6743 return;
6744
6745 /* Copy over the global and procedure linkage table refcount entries.
6746 These may have been already set up by a check_relocs routine. */
6747 htab = elf_hash_table (info);
6748 if (ind->got.refcount > htab->init_got_refcount.refcount)
6749 {
6750 if (dir->got.refcount < 0)
6751 dir->got.refcount = 0;
6752 dir->got.refcount += ind->got.refcount;
6753 ind->got.refcount = htab->init_got_refcount.refcount;
6754 }
6755
6756 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6757 {
6758 if (dir->plt.refcount < 0)
6759 dir->plt.refcount = 0;
6760 dir->plt.refcount += ind->plt.refcount;
6761 ind->plt.refcount = htab->init_plt_refcount.refcount;
6762 }
6763
6764 if (ind->dynindx != -1)
6765 {
6766 if (dir->dynindx != -1)
6767 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6768 dir->dynindx = ind->dynindx;
6769 dir->dynstr_index = ind->dynstr_index;
6770 ind->dynindx = -1;
6771 ind->dynstr_index = 0;
6772 }
6773}
6774
6775void
6776_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6777 struct elf_link_hash_entry *h,
6778 bfd_boolean force_local)
6779{
3aa14d16
L
6780 /* STT_GNU_IFUNC symbol must go through PLT. */
6781 if (h->type != STT_GNU_IFUNC)
6782 {
6783 h->plt = elf_hash_table (info)->init_plt_offset;
6784 h->needs_plt = 0;
6785 }
4d269e42
AM
6786 if (force_local)
6787 {
6788 h->forced_local = 1;
6789 if (h->dynindx != -1)
6790 {
6791 h->dynindx = -1;
6792 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6793 h->dynstr_index);
6794 }
6795 }
6796}
6797
6798/* Initialize an ELF linker hash table. */
6799
6800bfd_boolean
6801_bfd_elf_link_hash_table_init
6802 (struct elf_link_hash_table *table,
6803 bfd *abfd,
6804 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6805 struct bfd_hash_table *,
6806 const char *),
4dfe6ac6
NC
6807 unsigned int entsize,
6808 enum elf_target_id target_id)
4d269e42
AM
6809{
6810 bfd_boolean ret;
6811 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6812
6813 memset (table, 0, sizeof * table);
6814 table->init_got_refcount.refcount = can_refcount - 1;
6815 table->init_plt_refcount.refcount = can_refcount - 1;
6816 table->init_got_offset.offset = -(bfd_vma) 1;
6817 table->init_plt_offset.offset = -(bfd_vma) 1;
6818 /* The first dynamic symbol is a dummy. */
6819 table->dynsymcount = 1;
6820
6821 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 6822
4d269e42 6823 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 6824 table->hash_table_id = target_id;
4d269e42
AM
6825
6826 return ret;
6827}
6828
6829/* Create an ELF linker hash table. */
6830
6831struct bfd_link_hash_table *
6832_bfd_elf_link_hash_table_create (bfd *abfd)
6833{
6834 struct elf_link_hash_table *ret;
6835 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6836
a50b1753 6837 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
4d269e42
AM
6838 if (ret == NULL)
6839 return NULL;
6840
6841 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
6842 sizeof (struct elf_link_hash_entry),
6843 GENERIC_ELF_DATA))
4d269e42
AM
6844 {
6845 free (ret);
6846 return NULL;
6847 }
6848
6849 return &ret->root;
6850}
6851
6852/* This is a hook for the ELF emulation code in the generic linker to
6853 tell the backend linker what file name to use for the DT_NEEDED
6854 entry for a dynamic object. */
6855
6856void
6857bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6858{
6859 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6860 && bfd_get_format (abfd) == bfd_object)
6861 elf_dt_name (abfd) = name;
6862}
6863
6864int
6865bfd_elf_get_dyn_lib_class (bfd *abfd)
6866{
6867 int lib_class;
6868 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6869 && bfd_get_format (abfd) == bfd_object)
6870 lib_class = elf_dyn_lib_class (abfd);
6871 else
6872 lib_class = 0;
6873 return lib_class;
6874}
6875
6876void
6877bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6878{
6879 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6880 && bfd_get_format (abfd) == bfd_object)
6881 elf_dyn_lib_class (abfd) = lib_class;
6882}
6883
6884/* Get the list of DT_NEEDED entries for a link. This is a hook for
6885 the linker ELF emulation code. */
6886
6887struct bfd_link_needed_list *
6888bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6889 struct bfd_link_info *info)
6890{
6891 if (! is_elf_hash_table (info->hash))
6892 return NULL;
6893 return elf_hash_table (info)->needed;
6894}
6895
6896/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6897 hook for the linker ELF emulation code. */
6898
6899struct bfd_link_needed_list *
6900bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6901 struct bfd_link_info *info)
6902{
6903 if (! is_elf_hash_table (info->hash))
6904 return NULL;
6905 return elf_hash_table (info)->runpath;
6906}
6907
6908/* Get the name actually used for a dynamic object for a link. This
6909 is the SONAME entry if there is one. Otherwise, it is the string
6910 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6911
6912const char *
6913bfd_elf_get_dt_soname (bfd *abfd)
6914{
6915 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6916 && bfd_get_format (abfd) == bfd_object)
6917 return elf_dt_name (abfd);
6918 return NULL;
6919}
6920
6921/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6922 the ELF linker emulation code. */
6923
6924bfd_boolean
6925bfd_elf_get_bfd_needed_list (bfd *abfd,
6926 struct bfd_link_needed_list **pneeded)
6927{
6928 asection *s;
6929 bfd_byte *dynbuf = NULL;
cb33740c 6930 unsigned int elfsec;
4d269e42
AM
6931 unsigned long shlink;
6932 bfd_byte *extdyn, *extdynend;
6933 size_t extdynsize;
6934 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6935
6936 *pneeded = NULL;
6937
6938 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6939 || bfd_get_format (abfd) != bfd_object)
6940 return TRUE;
6941
6942 s = bfd_get_section_by_name (abfd, ".dynamic");
6943 if (s == NULL || s->size == 0)
6944 return TRUE;
6945
6946 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6947 goto error_return;
6948
6949 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 6950 if (elfsec == SHN_BAD)
4d269e42
AM
6951 goto error_return;
6952
6953 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6954
4d269e42
AM
6955 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6956 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6957
6958 extdyn = dynbuf;
6959 extdynend = extdyn + s->size;
6960 for (; extdyn < extdynend; extdyn += extdynsize)
6961 {
6962 Elf_Internal_Dyn dyn;
6963
6964 (*swap_dyn_in) (abfd, extdyn, &dyn);
6965
6966 if (dyn.d_tag == DT_NULL)
6967 break;
6968
6969 if (dyn.d_tag == DT_NEEDED)
6970 {
6971 const char *string;
6972 struct bfd_link_needed_list *l;
6973 unsigned int tagv = dyn.d_un.d_val;
6974 bfd_size_type amt;
6975
6976 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6977 if (string == NULL)
6978 goto error_return;
6979
6980 amt = sizeof *l;
a50b1753 6981 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
6982 if (l == NULL)
6983 goto error_return;
6984
6985 l->by = abfd;
6986 l->name = string;
6987 l->next = *pneeded;
6988 *pneeded = l;
6989 }
6990 }
6991
6992 free (dynbuf);
6993
6994 return TRUE;
6995
6996 error_return:
6997 if (dynbuf != NULL)
6998 free (dynbuf);
6999 return FALSE;
7000}
7001
7002struct elf_symbuf_symbol
7003{
7004 unsigned long st_name; /* Symbol name, index in string tbl */
7005 unsigned char st_info; /* Type and binding attributes */
7006 unsigned char st_other; /* Visibilty, and target specific */
7007};
7008
7009struct elf_symbuf_head
7010{
7011 struct elf_symbuf_symbol *ssym;
7012 bfd_size_type count;
7013 unsigned int st_shndx;
7014};
7015
7016struct elf_symbol
7017{
7018 union
7019 {
7020 Elf_Internal_Sym *isym;
7021 struct elf_symbuf_symbol *ssym;
7022 } u;
7023 const char *name;
7024};
7025
7026/* Sort references to symbols by ascending section number. */
7027
7028static int
7029elf_sort_elf_symbol (const void *arg1, const void *arg2)
7030{
7031 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7032 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7033
7034 return s1->st_shndx - s2->st_shndx;
7035}
7036
7037static int
7038elf_sym_name_compare (const void *arg1, const void *arg2)
7039{
7040 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7041 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7042 return strcmp (s1->name, s2->name);
7043}
7044
7045static struct elf_symbuf_head *
7046elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7047{
14b1c01e 7048 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7049 struct elf_symbuf_symbol *ssym;
7050 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7051 bfd_size_type i, shndx_count, total_size;
4d269e42 7052
a50b1753 7053 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7054 if (indbuf == NULL)
7055 return NULL;
7056
7057 for (ind = indbuf, i = 0; i < symcount; i++)
7058 if (isymbuf[i].st_shndx != SHN_UNDEF)
7059 *ind++ = &isymbuf[i];
7060 indbufend = ind;
7061
7062 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7063 elf_sort_elf_symbol);
7064
7065 shndx_count = 0;
7066 if (indbufend > indbuf)
7067 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7068 if (ind[0]->st_shndx != ind[1]->st_shndx)
7069 shndx_count++;
7070
3ae181ee
L
7071 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7072 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7073 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7074 if (ssymbuf == NULL)
7075 {
7076 free (indbuf);
7077 return NULL;
7078 }
7079
3ae181ee 7080 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7081 ssymbuf->ssym = NULL;
7082 ssymbuf->count = shndx_count;
7083 ssymbuf->st_shndx = 0;
7084 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7085 {
7086 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7087 {
7088 ssymhead++;
7089 ssymhead->ssym = ssym;
7090 ssymhead->count = 0;
7091 ssymhead->st_shndx = (*ind)->st_shndx;
7092 }
7093 ssym->st_name = (*ind)->st_name;
7094 ssym->st_info = (*ind)->st_info;
7095 ssym->st_other = (*ind)->st_other;
7096 ssymhead->count++;
7097 }
3ae181ee
L
7098 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7099 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7100 == total_size));
4d269e42
AM
7101
7102 free (indbuf);
7103 return ssymbuf;
7104}
7105
7106/* Check if 2 sections define the same set of local and global
7107 symbols. */
7108
8f317e31 7109static bfd_boolean
4d269e42
AM
7110bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7111 struct bfd_link_info *info)
7112{
7113 bfd *bfd1, *bfd2;
7114 const struct elf_backend_data *bed1, *bed2;
7115 Elf_Internal_Shdr *hdr1, *hdr2;
7116 bfd_size_type symcount1, symcount2;
7117 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7118 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7119 Elf_Internal_Sym *isym, *isymend;
7120 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7121 bfd_size_type count1, count2, i;
cb33740c 7122 unsigned int shndx1, shndx2;
4d269e42
AM
7123 bfd_boolean result;
7124
7125 bfd1 = sec1->owner;
7126 bfd2 = sec2->owner;
7127
4d269e42
AM
7128 /* Both sections have to be in ELF. */
7129 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7130 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7131 return FALSE;
7132
7133 if (elf_section_type (sec1) != elf_section_type (sec2))
7134 return FALSE;
7135
4d269e42
AM
7136 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7137 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7138 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7139 return FALSE;
7140
7141 bed1 = get_elf_backend_data (bfd1);
7142 bed2 = get_elf_backend_data (bfd2);
7143 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7144 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7145 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7146 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7147
7148 if (symcount1 == 0 || symcount2 == 0)
7149 return FALSE;
7150
7151 result = FALSE;
7152 isymbuf1 = NULL;
7153 isymbuf2 = NULL;
a50b1753
NC
7154 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7155 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7156
7157 if (ssymbuf1 == NULL)
7158 {
7159 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7160 NULL, NULL, NULL);
7161 if (isymbuf1 == NULL)
7162 goto done;
7163
7164 if (!info->reduce_memory_overheads)
7165 elf_tdata (bfd1)->symbuf = ssymbuf1
7166 = elf_create_symbuf (symcount1, isymbuf1);
7167 }
7168
7169 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7170 {
7171 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7172 NULL, NULL, NULL);
7173 if (isymbuf2 == NULL)
7174 goto done;
7175
7176 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7177 elf_tdata (bfd2)->symbuf = ssymbuf2
7178 = elf_create_symbuf (symcount2, isymbuf2);
7179 }
7180
7181 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7182 {
7183 /* Optimized faster version. */
7184 bfd_size_type lo, hi, mid;
7185 struct elf_symbol *symp;
7186 struct elf_symbuf_symbol *ssym, *ssymend;
7187
7188 lo = 0;
7189 hi = ssymbuf1->count;
7190 ssymbuf1++;
7191 count1 = 0;
7192 while (lo < hi)
7193 {
7194 mid = (lo + hi) / 2;
cb33740c 7195 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7196 hi = mid;
cb33740c 7197 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7198 lo = mid + 1;
7199 else
7200 {
7201 count1 = ssymbuf1[mid].count;
7202 ssymbuf1 += mid;
7203 break;
7204 }
7205 }
7206
7207 lo = 0;
7208 hi = ssymbuf2->count;
7209 ssymbuf2++;
7210 count2 = 0;
7211 while (lo < hi)
7212 {
7213 mid = (lo + hi) / 2;
cb33740c 7214 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7215 hi = mid;
cb33740c 7216 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7217 lo = mid + 1;
7218 else
7219 {
7220 count2 = ssymbuf2[mid].count;
7221 ssymbuf2 += mid;
7222 break;
7223 }
7224 }
7225
7226 if (count1 == 0 || count2 == 0 || count1 != count2)
7227 goto done;
7228
a50b1753
NC
7229 symtable1 = (struct elf_symbol *)
7230 bfd_malloc (count1 * sizeof (struct elf_symbol));
7231 symtable2 = (struct elf_symbol *)
7232 bfd_malloc (count2 * sizeof (struct elf_symbol));
4d269e42
AM
7233 if (symtable1 == NULL || symtable2 == NULL)
7234 goto done;
7235
7236 symp = symtable1;
7237 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7238 ssym < ssymend; ssym++, symp++)
7239 {
7240 symp->u.ssym = ssym;
7241 symp->name = bfd_elf_string_from_elf_section (bfd1,
7242 hdr1->sh_link,
7243 ssym->st_name);
7244 }
7245
7246 symp = symtable2;
7247 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7248 ssym < ssymend; ssym++, symp++)
7249 {
7250 symp->u.ssym = ssym;
7251 symp->name = bfd_elf_string_from_elf_section (bfd2,
7252 hdr2->sh_link,
7253 ssym->st_name);
7254 }
7255
7256 /* Sort symbol by name. */
7257 qsort (symtable1, count1, sizeof (struct elf_symbol),
7258 elf_sym_name_compare);
7259 qsort (symtable2, count1, sizeof (struct elf_symbol),
7260 elf_sym_name_compare);
7261
7262 for (i = 0; i < count1; i++)
7263 /* Two symbols must have the same binding, type and name. */
7264 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7265 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7266 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7267 goto done;
7268
7269 result = TRUE;
7270 goto done;
7271 }
7272
a50b1753
NC
7273 symtable1 = (struct elf_symbol *)
7274 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7275 symtable2 = (struct elf_symbol *)
7276 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7277 if (symtable1 == NULL || symtable2 == NULL)
7278 goto done;
7279
7280 /* Count definitions in the section. */
7281 count1 = 0;
7282 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7283 if (isym->st_shndx == shndx1)
4d269e42
AM
7284 symtable1[count1++].u.isym = isym;
7285
7286 count2 = 0;
7287 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7288 if (isym->st_shndx == shndx2)
4d269e42
AM
7289 symtable2[count2++].u.isym = isym;
7290
7291 if (count1 == 0 || count2 == 0 || count1 != count2)
7292 goto done;
7293
7294 for (i = 0; i < count1; i++)
7295 symtable1[i].name
7296 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7297 symtable1[i].u.isym->st_name);
7298
7299 for (i = 0; i < count2; i++)
7300 symtable2[i].name
7301 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7302 symtable2[i].u.isym->st_name);
7303
7304 /* Sort symbol by name. */
7305 qsort (symtable1, count1, sizeof (struct elf_symbol),
7306 elf_sym_name_compare);
7307 qsort (symtable2, count1, sizeof (struct elf_symbol),
7308 elf_sym_name_compare);
7309
7310 for (i = 0; i < count1; i++)
7311 /* Two symbols must have the same binding, type and name. */
7312 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7313 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7314 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7315 goto done;
7316
7317 result = TRUE;
7318
7319done:
7320 if (symtable1)
7321 free (symtable1);
7322 if (symtable2)
7323 free (symtable2);
7324 if (isymbuf1)
7325 free (isymbuf1);
7326 if (isymbuf2)
7327 free (isymbuf2);
7328
7329 return result;
7330}
7331
7332/* Return TRUE if 2 section types are compatible. */
7333
7334bfd_boolean
7335_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7336 bfd *bbfd, const asection *bsec)
7337{
7338 if (asec == NULL
7339 || bsec == NULL
7340 || abfd->xvec->flavour != bfd_target_elf_flavour
7341 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7342 return TRUE;
7343
7344 return elf_section_type (asec) == elf_section_type (bsec);
7345}
7346\f
c152c796
AM
7347/* Final phase of ELF linker. */
7348
7349/* A structure we use to avoid passing large numbers of arguments. */
7350
7351struct elf_final_link_info
7352{
7353 /* General link information. */
7354 struct bfd_link_info *info;
7355 /* Output BFD. */
7356 bfd *output_bfd;
7357 /* Symbol string table. */
7358 struct bfd_strtab_hash *symstrtab;
7359 /* .dynsym section. */
7360 asection *dynsym_sec;
7361 /* .hash section. */
7362 asection *hash_sec;
7363 /* symbol version section (.gnu.version). */
7364 asection *symver_sec;
7365 /* Buffer large enough to hold contents of any section. */
7366 bfd_byte *contents;
7367 /* Buffer large enough to hold external relocs of any section. */
7368 void *external_relocs;
7369 /* Buffer large enough to hold internal relocs of any section. */
7370 Elf_Internal_Rela *internal_relocs;
7371 /* Buffer large enough to hold external local symbols of any input
7372 BFD. */
7373 bfd_byte *external_syms;
7374 /* And a buffer for symbol section indices. */
7375 Elf_External_Sym_Shndx *locsym_shndx;
7376 /* Buffer large enough to hold internal local symbols of any input
7377 BFD. */
7378 Elf_Internal_Sym *internal_syms;
7379 /* Array large enough to hold a symbol index for each local symbol
7380 of any input BFD. */
7381 long *indices;
7382 /* Array large enough to hold a section pointer for each local
7383 symbol of any input BFD. */
7384 asection **sections;
7385 /* Buffer to hold swapped out symbols. */
7386 bfd_byte *symbuf;
7387 /* And one for symbol section indices. */
7388 Elf_External_Sym_Shndx *symshndxbuf;
7389 /* Number of swapped out symbols in buffer. */
7390 size_t symbuf_count;
7391 /* Number of symbols which fit in symbuf. */
7392 size_t symbuf_size;
7393 /* And same for symshndxbuf. */
7394 size_t shndxbuf_size;
7395};
7396
7397/* This struct is used to pass information to elf_link_output_extsym. */
7398
7399struct elf_outext_info
7400{
7401 bfd_boolean failed;
7402 bfd_boolean localsyms;
7403 struct elf_final_link_info *finfo;
7404};
7405
d9352518
DB
7406
7407/* Support for evaluating a complex relocation.
7408
7409 Complex relocations are generalized, self-describing relocations. The
7410 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7411 relocations themselves.
d9352518
DB
7412
7413 The relocations are use a reserved elf-wide relocation type code (R_RELC
7414 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7415 information (start bit, end bit, word width, etc) into the addend. This
7416 information is extracted from CGEN-generated operand tables within gas.
7417
7418 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7419 internal) representing prefix-notation expressions, including but not
7420 limited to those sorts of expressions normally encoded as addends in the
7421 addend field. The symbol mangling format is:
7422
7423 <node> := <literal>
7424 | <unary-operator> ':' <node>
7425 | <binary-operator> ':' <node> ':' <node>
7426 ;
7427
7428 <literal> := 's' <digits=N> ':' <N character symbol name>
7429 | 'S' <digits=N> ':' <N character section name>
7430 | '#' <hexdigits>
7431 ;
7432
7433 <binary-operator> := as in C
7434 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7435
7436static void
a0c8462f
AM
7437set_symbol_value (bfd *bfd_with_globals,
7438 Elf_Internal_Sym *isymbuf,
7439 size_t locsymcount,
7440 size_t symidx,
7441 bfd_vma val)
d9352518 7442{
8977835c
AM
7443 struct elf_link_hash_entry **sym_hashes;
7444 struct elf_link_hash_entry *h;
7445 size_t extsymoff = locsymcount;
d9352518 7446
8977835c 7447 if (symidx < locsymcount)
d9352518 7448 {
8977835c
AM
7449 Elf_Internal_Sym *sym;
7450
7451 sym = isymbuf + symidx;
7452 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7453 {
7454 /* It is a local symbol: move it to the
7455 "absolute" section and give it a value. */
7456 sym->st_shndx = SHN_ABS;
7457 sym->st_value = val;
7458 return;
7459 }
7460 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7461 extsymoff = 0;
d9352518 7462 }
8977835c
AM
7463
7464 /* It is a global symbol: set its link type
7465 to "defined" and give it a value. */
7466
7467 sym_hashes = elf_sym_hashes (bfd_with_globals);
7468 h = sym_hashes [symidx - extsymoff];
7469 while (h->root.type == bfd_link_hash_indirect
7470 || h->root.type == bfd_link_hash_warning)
7471 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7472 h->root.type = bfd_link_hash_defined;
7473 h->root.u.def.value = val;
7474 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7475}
7476
a0c8462f
AM
7477static bfd_boolean
7478resolve_symbol (const char *name,
7479 bfd *input_bfd,
7480 struct elf_final_link_info *finfo,
7481 bfd_vma *result,
7482 Elf_Internal_Sym *isymbuf,
7483 size_t locsymcount)
d9352518 7484{
a0c8462f
AM
7485 Elf_Internal_Sym *sym;
7486 struct bfd_link_hash_entry *global_entry;
7487 const char *candidate = NULL;
7488 Elf_Internal_Shdr *symtab_hdr;
7489 size_t i;
7490
d9352518
DB
7491 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7492
7493 for (i = 0; i < locsymcount; ++ i)
7494 {
8977835c 7495 sym = isymbuf + i;
d9352518
DB
7496
7497 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7498 continue;
7499
7500 candidate = bfd_elf_string_from_elf_section (input_bfd,
7501 symtab_hdr->sh_link,
7502 sym->st_name);
7503#ifdef DEBUG
0f02bbd9
AM
7504 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7505 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7506#endif
7507 if (candidate && strcmp (candidate, name) == 0)
7508 {
0f02bbd9 7509 asection *sec = finfo->sections [i];
d9352518 7510
0f02bbd9
AM
7511 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7512 *result += sec->output_offset + sec->output_section->vma;
d9352518 7513#ifdef DEBUG
0f02bbd9
AM
7514 printf ("Found symbol with value %8.8lx\n",
7515 (unsigned long) *result);
d9352518
DB
7516#endif
7517 return TRUE;
7518 }
7519 }
7520
7521 /* Hmm, haven't found it yet. perhaps it is a global. */
a0c8462f
AM
7522 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7523 FALSE, FALSE, TRUE);
d9352518
DB
7524 if (!global_entry)
7525 return FALSE;
a0c8462f 7526
d9352518
DB
7527 if (global_entry->type == bfd_link_hash_defined
7528 || global_entry->type == bfd_link_hash_defweak)
7529 {
a0c8462f
AM
7530 *result = (global_entry->u.def.value
7531 + global_entry->u.def.section->output_section->vma
7532 + global_entry->u.def.section->output_offset);
d9352518 7533#ifdef DEBUG
0f02bbd9
AM
7534 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7535 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7536#endif
7537 return TRUE;
a0c8462f 7538 }
d9352518 7539
d9352518
DB
7540 return FALSE;
7541}
7542
7543static bfd_boolean
a0c8462f
AM
7544resolve_section (const char *name,
7545 asection *sections,
7546 bfd_vma *result)
d9352518 7547{
a0c8462f
AM
7548 asection *curr;
7549 unsigned int len;
d9352518 7550
a0c8462f 7551 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7552 if (strcmp (curr->name, name) == 0)
7553 {
7554 *result = curr->vma;
7555 return TRUE;
7556 }
7557
7558 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7559 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7560 {
7561 len = strlen (curr->name);
a0c8462f 7562 if (len > strlen (name))
d9352518
DB
7563 continue;
7564
7565 if (strncmp (curr->name, name, len) == 0)
7566 {
7567 if (strncmp (".end", name + len, 4) == 0)
7568 {
7569 *result = curr->vma + curr->size;
7570 return TRUE;
7571 }
7572
7573 /* Insert more pseudo-section names here, if you like. */
7574 }
7575 }
a0c8462f 7576
d9352518
DB
7577 return FALSE;
7578}
7579
7580static void
a0c8462f 7581undefined_reference (const char *reftype, const char *name)
d9352518 7582{
a0c8462f
AM
7583 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7584 reftype, name);
d9352518
DB
7585}
7586
7587static bfd_boolean
a0c8462f
AM
7588eval_symbol (bfd_vma *result,
7589 const char **symp,
7590 bfd *input_bfd,
7591 struct elf_final_link_info *finfo,
7592 bfd_vma dot,
7593 Elf_Internal_Sym *isymbuf,
7594 size_t locsymcount,
7595 int signed_p)
d9352518 7596{
4b93929b
NC
7597 size_t len;
7598 size_t symlen;
a0c8462f
AM
7599 bfd_vma a;
7600 bfd_vma b;
4b93929b 7601 char symbuf[4096];
0f02bbd9 7602 const char *sym = *symp;
a0c8462f
AM
7603 const char *symend;
7604 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7605
7606 len = strlen (sym);
7607 symend = sym + len;
7608
4b93929b 7609 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7610 {
7611 bfd_set_error (bfd_error_invalid_operation);
7612 return FALSE;
7613 }
a0c8462f 7614
d9352518
DB
7615 switch (* sym)
7616 {
7617 case '.':
0f02bbd9
AM
7618 *result = dot;
7619 *symp = sym + 1;
d9352518
DB
7620 return TRUE;
7621
7622 case '#':
0f02bbd9
AM
7623 ++sym;
7624 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7625 return TRUE;
7626
7627 case 'S':
7628 symbol_is_section = TRUE;
a0c8462f 7629 case 's':
0f02bbd9
AM
7630 ++sym;
7631 symlen = strtol (sym, (char **) symp, 10);
7632 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7633
4b93929b 7634 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7635 {
7636 bfd_set_error (bfd_error_invalid_operation);
7637 return FALSE;
7638 }
7639
7640 memcpy (symbuf, sym, symlen);
a0c8462f 7641 symbuf[symlen] = '\0';
0f02bbd9 7642 *symp = sym + symlen;
a0c8462f
AM
7643
7644 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7645 the symbol as a section, or vice-versa. so we're pretty liberal in our
7646 interpretation here; section means "try section first", not "must be a
7647 section", and likewise with symbol. */
7648
a0c8462f 7649 if (symbol_is_section)
d9352518 7650 {
8977835c
AM
7651 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7652 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7653 isymbuf, locsymcount))
d9352518
DB
7654 {
7655 undefined_reference ("section", symbuf);
7656 return FALSE;
7657 }
a0c8462f
AM
7658 }
7659 else
d9352518 7660 {
8977835c
AM
7661 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7662 isymbuf, locsymcount)
7663 && !resolve_section (symbuf, finfo->output_bfd->sections,
7664 result))
d9352518
DB
7665 {
7666 undefined_reference ("symbol", symbuf);
7667 return FALSE;
7668 }
7669 }
7670
7671 return TRUE;
a0c8462f 7672
d9352518
DB
7673 /* All that remains are operators. */
7674
7675#define UNARY_OP(op) \
7676 if (strncmp (sym, #op, strlen (#op)) == 0) \
7677 { \
7678 sym += strlen (#op); \
a0c8462f
AM
7679 if (*sym == ':') \
7680 ++sym; \
0f02bbd9
AM
7681 *symp = sym; \
7682 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7683 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7684 return FALSE; \
7685 if (signed_p) \
0f02bbd9 7686 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7687 else \
7688 *result = op a; \
d9352518
DB
7689 return TRUE; \
7690 }
7691
7692#define BINARY_OP(op) \
7693 if (strncmp (sym, #op, strlen (#op)) == 0) \
7694 { \
7695 sym += strlen (#op); \
a0c8462f
AM
7696 if (*sym == ':') \
7697 ++sym; \
0f02bbd9
AM
7698 *symp = sym; \
7699 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7700 isymbuf, locsymcount, signed_p)) \
a0c8462f 7701 return FALSE; \
0f02bbd9
AM
7702 ++*symp; \
7703 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7704 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7705 return FALSE; \
7706 if (signed_p) \
0f02bbd9 7707 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7708 else \
7709 *result = a op b; \
d9352518
DB
7710 return TRUE; \
7711 }
7712
7713 default:
7714 UNARY_OP (0-);
7715 BINARY_OP (<<);
7716 BINARY_OP (>>);
7717 BINARY_OP (==);
7718 BINARY_OP (!=);
7719 BINARY_OP (<=);
7720 BINARY_OP (>=);
7721 BINARY_OP (&&);
7722 BINARY_OP (||);
7723 UNARY_OP (~);
7724 UNARY_OP (!);
7725 BINARY_OP (*);
7726 BINARY_OP (/);
7727 BINARY_OP (%);
7728 BINARY_OP (^);
7729 BINARY_OP (|);
7730 BINARY_OP (&);
7731 BINARY_OP (+);
7732 BINARY_OP (-);
7733 BINARY_OP (<);
7734 BINARY_OP (>);
7735#undef UNARY_OP
7736#undef BINARY_OP
7737 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7738 bfd_set_error (bfd_error_invalid_operation);
7739 return FALSE;
7740 }
7741}
7742
d9352518 7743static void
a0c8462f
AM
7744put_value (bfd_vma size,
7745 unsigned long chunksz,
7746 bfd *input_bfd,
7747 bfd_vma x,
7748 bfd_byte *location)
d9352518
DB
7749{
7750 location += (size - chunksz);
7751
a0c8462f 7752 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7753 {
7754 switch (chunksz)
7755 {
7756 default:
7757 case 0:
7758 abort ();
7759 case 1:
7760 bfd_put_8 (input_bfd, x, location);
7761 break;
7762 case 2:
7763 bfd_put_16 (input_bfd, x, location);
7764 break;
7765 case 4:
7766 bfd_put_32 (input_bfd, x, location);
7767 break;
7768 case 8:
7769#ifdef BFD64
7770 bfd_put_64 (input_bfd, x, location);
7771#else
7772 abort ();
7773#endif
7774 break;
7775 }
7776 }
7777}
7778
a0c8462f
AM
7779static bfd_vma
7780get_value (bfd_vma size,
7781 unsigned long chunksz,
7782 bfd *input_bfd,
7783 bfd_byte *location)
d9352518
DB
7784{
7785 bfd_vma x = 0;
7786
a0c8462f 7787 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7788 {
7789 switch (chunksz)
7790 {
7791 default:
7792 case 0:
7793 abort ();
7794 case 1:
7795 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7796 break;
7797 case 2:
7798 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7799 break;
7800 case 4:
7801 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7802 break;
7803 case 8:
7804#ifdef BFD64
7805 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7806#else
7807 abort ();
7808#endif
7809 break;
7810 }
7811 }
7812 return x;
7813}
7814
a0c8462f
AM
7815static void
7816decode_complex_addend (unsigned long *start, /* in bits */
7817 unsigned long *oplen, /* in bits */
7818 unsigned long *len, /* in bits */
7819 unsigned long *wordsz, /* in bytes */
7820 unsigned long *chunksz, /* in bytes */
7821 unsigned long *lsb0_p,
7822 unsigned long *signed_p,
7823 unsigned long *trunc_p,
7824 unsigned long encoded)
d9352518
DB
7825{
7826 * start = encoded & 0x3F;
7827 * len = (encoded >> 6) & 0x3F;
7828 * oplen = (encoded >> 12) & 0x3F;
7829 * wordsz = (encoded >> 18) & 0xF;
7830 * chunksz = (encoded >> 22) & 0xF;
7831 * lsb0_p = (encoded >> 27) & 1;
7832 * signed_p = (encoded >> 28) & 1;
7833 * trunc_p = (encoded >> 29) & 1;
7834}
7835
cdfeee4f 7836bfd_reloc_status_type
0f02bbd9 7837bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7838 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7839 bfd_byte *contents,
7840 Elf_Internal_Rela *rel,
7841 bfd_vma relocation)
d9352518 7842{
0f02bbd9
AM
7843 bfd_vma shift, x, mask;
7844 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7845 bfd_reloc_status_type r;
d9352518
DB
7846
7847 /* Perform this reloc, since it is complex.
7848 (this is not to say that it necessarily refers to a complex
7849 symbol; merely that it is a self-describing CGEN based reloc.
7850 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7851 word size, etc) encoded within it.). */
d9352518 7852
a0c8462f
AM
7853 decode_complex_addend (&start, &oplen, &len, &wordsz,
7854 &chunksz, &lsb0_p, &signed_p,
7855 &trunc_p, rel->r_addend);
d9352518
DB
7856
7857 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7858
7859 if (lsb0_p)
7860 shift = (start + 1) - len;
7861 else
7862 shift = (8 * wordsz) - (start + len);
7863
5dabe785 7864 /* FIXME: octets_per_byte. */
a0c8462f 7865 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7866
7867#ifdef DEBUG
7868 printf ("Doing complex reloc: "
7869 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7870 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7871 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7872 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
7873 oplen, (unsigned long) x, (unsigned long) mask,
7874 (unsigned long) relocation);
d9352518
DB
7875#endif
7876
cdfeee4f 7877 r = bfd_reloc_ok;
d9352518 7878 if (! trunc_p)
cdfeee4f
AM
7879 /* Now do an overflow check. */
7880 r = bfd_check_overflow ((signed_p
7881 ? complain_overflow_signed
7882 : complain_overflow_unsigned),
7883 len, 0, (8 * wordsz),
7884 relocation);
a0c8462f 7885
d9352518
DB
7886 /* Do the deed. */
7887 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7888
7889#ifdef DEBUG
7890 printf (" relocation: %8.8lx\n"
7891 " shifted mask: %8.8lx\n"
7892 " shifted/masked reloc: %8.8lx\n"
7893 " result: %8.8lx\n",
9ccb8af9
AM
7894 (unsigned long) relocation, (unsigned long) (mask << shift),
7895 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 7896#endif
5dabe785 7897 /* FIXME: octets_per_byte. */
d9352518 7898 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7899 return r;
d9352518
DB
7900}
7901
c152c796
AM
7902/* When performing a relocatable link, the input relocations are
7903 preserved. But, if they reference global symbols, the indices
d4730f92
BS
7904 referenced must be updated. Update all the relocations found in
7905 RELDATA. */
c152c796
AM
7906
7907static void
7908elf_link_adjust_relocs (bfd *abfd,
d4730f92 7909 struct bfd_elf_section_reloc_data *reldata)
c152c796
AM
7910{
7911 unsigned int i;
7912 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7913 bfd_byte *erela;
7914 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7915 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7916 bfd_vma r_type_mask;
7917 int r_sym_shift;
d4730f92
BS
7918 unsigned int count = reldata->count;
7919 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 7920
d4730f92 7921 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
7922 {
7923 swap_in = bed->s->swap_reloc_in;
7924 swap_out = bed->s->swap_reloc_out;
7925 }
d4730f92 7926 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
7927 {
7928 swap_in = bed->s->swap_reloca_in;
7929 swap_out = bed->s->swap_reloca_out;
7930 }
7931 else
7932 abort ();
7933
7934 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7935 abort ();
7936
7937 if (bed->s->arch_size == 32)
7938 {
7939 r_type_mask = 0xff;
7940 r_sym_shift = 8;
7941 }
7942 else
7943 {
7944 r_type_mask = 0xffffffff;
7945 r_sym_shift = 32;
7946 }
7947
d4730f92
BS
7948 erela = reldata->hdr->contents;
7949 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
7950 {
7951 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7952 unsigned int j;
7953
7954 if (*rel_hash == NULL)
7955 continue;
7956
7957 BFD_ASSERT ((*rel_hash)->indx >= 0);
7958
7959 (*swap_in) (abfd, erela, irela);
7960 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7961 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7962 | (irela[j].r_info & r_type_mask));
7963 (*swap_out) (abfd, irela, erela);
7964 }
7965}
7966
7967struct elf_link_sort_rela
7968{
7969 union {
7970 bfd_vma offset;
7971 bfd_vma sym_mask;
7972 } u;
7973 enum elf_reloc_type_class type;
7974 /* We use this as an array of size int_rels_per_ext_rel. */
7975 Elf_Internal_Rela rela[1];
7976};
7977
7978static int
7979elf_link_sort_cmp1 (const void *A, const void *B)
7980{
a50b1753
NC
7981 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
7982 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
7983 int relativea, relativeb;
7984
7985 relativea = a->type == reloc_class_relative;
7986 relativeb = b->type == reloc_class_relative;
7987
7988 if (relativea < relativeb)
7989 return 1;
7990 if (relativea > relativeb)
7991 return -1;
7992 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7993 return -1;
7994 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7995 return 1;
7996 if (a->rela->r_offset < b->rela->r_offset)
7997 return -1;
7998 if (a->rela->r_offset > b->rela->r_offset)
7999 return 1;
8000 return 0;
8001}
8002
8003static int
8004elf_link_sort_cmp2 (const void *A, const void *B)
8005{
a50b1753
NC
8006 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8007 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8008 int copya, copyb;
8009
8010 if (a->u.offset < b->u.offset)
8011 return -1;
8012 if (a->u.offset > b->u.offset)
8013 return 1;
8014 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8015 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8016 if (copya < copyb)
8017 return -1;
8018 if (copya > copyb)
8019 return 1;
8020 if (a->rela->r_offset < b->rela->r_offset)
8021 return -1;
8022 if (a->rela->r_offset > b->rela->r_offset)
8023 return 1;
8024 return 0;
8025}
8026
8027static size_t
8028elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8029{
3410fea8 8030 asection *dynamic_relocs;
fc66a176
L
8031 asection *rela_dyn;
8032 asection *rel_dyn;
c152c796
AM
8033 bfd_size_type count, size;
8034 size_t i, ret, sort_elt, ext_size;
8035 bfd_byte *sort, *s_non_relative, *p;
8036 struct elf_link_sort_rela *sq;
8037 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8038 int i2e = bed->s->int_rels_per_ext_rel;
8039 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8040 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8041 struct bfd_link_order *lo;
8042 bfd_vma r_sym_mask;
3410fea8 8043 bfd_boolean use_rela;
c152c796 8044
3410fea8
NC
8045 /* Find a dynamic reloc section. */
8046 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8047 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8048 if (rela_dyn != NULL && rela_dyn->size > 0
8049 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8050 {
3410fea8
NC
8051 bfd_boolean use_rela_initialised = FALSE;
8052
8053 /* This is just here to stop gcc from complaining.
8054 It's initialization checking code is not perfect. */
8055 use_rela = TRUE;
8056
8057 /* Both sections are present. Examine the sizes
8058 of the indirect sections to help us choose. */
8059 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8060 if (lo->type == bfd_indirect_link_order)
8061 {
8062 asection *o = lo->u.indirect.section;
8063
8064 if ((o->size % bed->s->sizeof_rela) == 0)
8065 {
8066 if ((o->size % bed->s->sizeof_rel) == 0)
8067 /* Section size is divisible by both rel and rela sizes.
8068 It is of no help to us. */
8069 ;
8070 else
8071 {
8072 /* Section size is only divisible by rela. */
8073 if (use_rela_initialised && (use_rela == FALSE))
8074 {
8075 _bfd_error_handler
8076 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8077 bfd_set_error (bfd_error_invalid_operation);
8078 return 0;
8079 }
8080 else
8081 {
8082 use_rela = TRUE;
8083 use_rela_initialised = TRUE;
8084 }
8085 }
8086 }
8087 else if ((o->size % bed->s->sizeof_rel) == 0)
8088 {
8089 /* Section size is only divisible by rel. */
8090 if (use_rela_initialised && (use_rela == TRUE))
8091 {
8092 _bfd_error_handler
8093 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8094 bfd_set_error (bfd_error_invalid_operation);
8095 return 0;
8096 }
8097 else
8098 {
8099 use_rela = FALSE;
8100 use_rela_initialised = TRUE;
8101 }
8102 }
8103 else
8104 {
8105 /* The section size is not divisible by either - something is wrong. */
8106 _bfd_error_handler
8107 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8108 bfd_set_error (bfd_error_invalid_operation);
8109 return 0;
8110 }
8111 }
8112
8113 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8114 if (lo->type == bfd_indirect_link_order)
8115 {
8116 asection *o = lo->u.indirect.section;
8117
8118 if ((o->size % bed->s->sizeof_rela) == 0)
8119 {
8120 if ((o->size % bed->s->sizeof_rel) == 0)
8121 /* Section size is divisible by both rel and rela sizes.
8122 It is of no help to us. */
8123 ;
8124 else
8125 {
8126 /* Section size is only divisible by rela. */
8127 if (use_rela_initialised && (use_rela == FALSE))
8128 {
8129 _bfd_error_handler
8130 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8131 bfd_set_error (bfd_error_invalid_operation);
8132 return 0;
8133 }
8134 else
8135 {
8136 use_rela = TRUE;
8137 use_rela_initialised = TRUE;
8138 }
8139 }
8140 }
8141 else if ((o->size % bed->s->sizeof_rel) == 0)
8142 {
8143 /* Section size is only divisible by rel. */
8144 if (use_rela_initialised && (use_rela == TRUE))
8145 {
8146 _bfd_error_handler
8147 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8148 bfd_set_error (bfd_error_invalid_operation);
8149 return 0;
8150 }
8151 else
8152 {
8153 use_rela = FALSE;
8154 use_rela_initialised = TRUE;
8155 }
8156 }
8157 else
8158 {
8159 /* The section size is not divisible by either - something is wrong. */
8160 _bfd_error_handler
8161 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8162 bfd_set_error (bfd_error_invalid_operation);
8163 return 0;
8164 }
8165 }
8166
8167 if (! use_rela_initialised)
8168 /* Make a guess. */
8169 use_rela = TRUE;
c152c796 8170 }
fc66a176
L
8171 else if (rela_dyn != NULL && rela_dyn->size > 0)
8172 use_rela = TRUE;
8173 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8174 use_rela = FALSE;
c152c796 8175 else
fc66a176 8176 return 0;
3410fea8
NC
8177
8178 if (use_rela)
c152c796 8179 {
3410fea8 8180 dynamic_relocs = rela_dyn;
c152c796
AM
8181 ext_size = bed->s->sizeof_rela;
8182 swap_in = bed->s->swap_reloca_in;
8183 swap_out = bed->s->swap_reloca_out;
8184 }
3410fea8
NC
8185 else
8186 {
8187 dynamic_relocs = rel_dyn;
8188 ext_size = bed->s->sizeof_rel;
8189 swap_in = bed->s->swap_reloc_in;
8190 swap_out = bed->s->swap_reloc_out;
8191 }
c152c796
AM
8192
8193 size = 0;
3410fea8 8194 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8195 if (lo->type == bfd_indirect_link_order)
3410fea8 8196 size += lo->u.indirect.section->size;
c152c796 8197
3410fea8 8198 if (size != dynamic_relocs->size)
c152c796
AM
8199 return 0;
8200
8201 sort_elt = (sizeof (struct elf_link_sort_rela)
8202 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8203
8204 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8205 if (count == 0)
8206 return 0;
a50b1753 8207 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8208
c152c796
AM
8209 if (sort == NULL)
8210 {
8211 (*info->callbacks->warning)
8212 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8213 return 0;
8214 }
8215
8216 if (bed->s->arch_size == 32)
8217 r_sym_mask = ~(bfd_vma) 0xff;
8218 else
8219 r_sym_mask = ~(bfd_vma) 0xffffffff;
8220
3410fea8 8221 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8222 if (lo->type == bfd_indirect_link_order)
8223 {
8224 bfd_byte *erel, *erelend;
8225 asection *o = lo->u.indirect.section;
8226
1da212d6
AM
8227 if (o->contents == NULL && o->size != 0)
8228 {
8229 /* This is a reloc section that is being handled as a normal
8230 section. See bfd_section_from_shdr. We can't combine
8231 relocs in this case. */
8232 free (sort);
8233 return 0;
8234 }
c152c796 8235 erel = o->contents;
eea6121a 8236 erelend = o->contents + o->size;
5dabe785 8237 /* FIXME: octets_per_byte. */
c152c796 8238 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8239
c152c796
AM
8240 while (erel < erelend)
8241 {
8242 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8243
c152c796
AM
8244 (*swap_in) (abfd, erel, s->rela);
8245 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8246 s->u.sym_mask = r_sym_mask;
8247 p += sort_elt;
8248 erel += ext_size;
8249 }
8250 }
8251
8252 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8253
8254 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8255 {
8256 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8257 if (s->type != reloc_class_relative)
8258 break;
8259 }
8260 ret = i;
8261 s_non_relative = p;
8262
8263 sq = (struct elf_link_sort_rela *) s_non_relative;
8264 for (; i < count; i++, p += sort_elt)
8265 {
8266 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8267 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8268 sq = sp;
8269 sp->u.offset = sq->rela->r_offset;
8270 }
8271
8272 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8273
3410fea8 8274 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8275 if (lo->type == bfd_indirect_link_order)
8276 {
8277 bfd_byte *erel, *erelend;
8278 asection *o = lo->u.indirect.section;
8279
8280 erel = o->contents;
eea6121a 8281 erelend = o->contents + o->size;
5dabe785 8282 /* FIXME: octets_per_byte. */
c152c796
AM
8283 p = sort + o->output_offset / ext_size * sort_elt;
8284 while (erel < erelend)
8285 {
8286 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8287 (*swap_out) (abfd, s->rela, erel);
8288 p += sort_elt;
8289 erel += ext_size;
8290 }
8291 }
8292
8293 free (sort);
3410fea8 8294 *psec = dynamic_relocs;
c152c796
AM
8295 return ret;
8296}
8297
8298/* Flush the output symbols to the file. */
8299
8300static bfd_boolean
8301elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8302 const struct elf_backend_data *bed)
8303{
8304 if (finfo->symbuf_count > 0)
8305 {
8306 Elf_Internal_Shdr *hdr;
8307 file_ptr pos;
8308 bfd_size_type amt;
8309
8310 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8311 pos = hdr->sh_offset + hdr->sh_size;
8312 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8313 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8314 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8315 return FALSE;
8316
8317 hdr->sh_size += amt;
8318 finfo->symbuf_count = 0;
8319 }
8320
8321 return TRUE;
8322}
8323
8324/* Add a symbol to the output symbol table. */
8325
6e0b88f1 8326static int
c152c796
AM
8327elf_link_output_sym (struct elf_final_link_info *finfo,
8328 const char *name,
8329 Elf_Internal_Sym *elfsym,
8330 asection *input_sec,
8331 struct elf_link_hash_entry *h)
8332{
8333 bfd_byte *dest;
8334 Elf_External_Sym_Shndx *destshndx;
6e0b88f1 8335 int (*output_symbol_hook)
c152c796
AM
8336 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8337 struct elf_link_hash_entry *);
8338 const struct elf_backend_data *bed;
8339
8340 bed = get_elf_backend_data (finfo->output_bfd);
8341 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8342 if (output_symbol_hook != NULL)
8343 {
6e0b88f1
AM
8344 int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h);
8345 if (ret != 1)
8346 return ret;
c152c796
AM
8347 }
8348
8349 if (name == NULL || *name == '\0')
8350 elfsym->st_name = 0;
8351 else if (input_sec->flags & SEC_EXCLUDE)
8352 elfsym->st_name = 0;
8353 else
8354 {
8355 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8356 name, TRUE, FALSE);
8357 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8358 return 0;
c152c796
AM
8359 }
8360
8361 if (finfo->symbuf_count >= finfo->symbuf_size)
8362 {
8363 if (! elf_link_flush_output_syms (finfo, bed))
6e0b88f1 8364 return 0;
c152c796
AM
8365 }
8366
8367 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8368 destshndx = finfo->symshndxbuf;
8369 if (destshndx != NULL)
8370 {
8371 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8372 {
8373 bfd_size_type amt;
8374
8375 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
a50b1753
NC
8376 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8377 amt * 2);
c152c796 8378 if (destshndx == NULL)
6e0b88f1 8379 return 0;
515ef31d 8380 finfo->symshndxbuf = destshndx;
c152c796
AM
8381 memset ((char *) destshndx + amt, 0, amt);
8382 finfo->shndxbuf_size *= 2;
8383 }
8384 destshndx += bfd_get_symcount (finfo->output_bfd);
8385 }
8386
8387 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8388 finfo->symbuf_count += 1;
8389 bfd_get_symcount (finfo->output_bfd) += 1;
8390
6e0b88f1 8391 return 1;
c152c796
AM
8392}
8393
c0d5a53d
L
8394/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8395
8396static bfd_boolean
8397check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8398{
4fbb74a6
AM
8399 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8400 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8401 {
8402 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8403 beyond 64k. */
c0d5a53d
L
8404 (*_bfd_error_handler)
8405 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8406 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8407 bfd_set_error (bfd_error_nonrepresentable_section);
8408 return FALSE;
8409 }
8410 return TRUE;
8411}
8412
c152c796
AM
8413/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8414 allowing an unsatisfied unversioned symbol in the DSO to match a
8415 versioned symbol that would normally require an explicit version.
8416 We also handle the case that a DSO references a hidden symbol
8417 which may be satisfied by a versioned symbol in another DSO. */
8418
8419static bfd_boolean
8420elf_link_check_versioned_symbol (struct bfd_link_info *info,
8421 const struct elf_backend_data *bed,
8422 struct elf_link_hash_entry *h)
8423{
8424 bfd *abfd;
8425 struct elf_link_loaded_list *loaded;
8426
8427 if (!is_elf_hash_table (info->hash))
8428 return FALSE;
8429
8430 switch (h->root.type)
8431 {
8432 default:
8433 abfd = NULL;
8434 break;
8435
8436 case bfd_link_hash_undefined:
8437 case bfd_link_hash_undefweak:
8438 abfd = h->root.u.undef.abfd;
8439 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8440 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8441 return FALSE;
8442 break;
8443
8444 case bfd_link_hash_defined:
8445 case bfd_link_hash_defweak:
8446 abfd = h->root.u.def.section->owner;
8447 break;
8448
8449 case bfd_link_hash_common:
8450 abfd = h->root.u.c.p->section->owner;
8451 break;
8452 }
8453 BFD_ASSERT (abfd != NULL);
8454
8455 for (loaded = elf_hash_table (info)->loaded;
8456 loaded != NULL;
8457 loaded = loaded->next)
8458 {
8459 bfd *input;
8460 Elf_Internal_Shdr *hdr;
8461 bfd_size_type symcount;
8462 bfd_size_type extsymcount;
8463 bfd_size_type extsymoff;
8464 Elf_Internal_Shdr *versymhdr;
8465 Elf_Internal_Sym *isym;
8466 Elf_Internal_Sym *isymend;
8467 Elf_Internal_Sym *isymbuf;
8468 Elf_External_Versym *ever;
8469 Elf_External_Versym *extversym;
8470
8471 input = loaded->abfd;
8472
8473 /* We check each DSO for a possible hidden versioned definition. */
8474 if (input == abfd
8475 || (input->flags & DYNAMIC) == 0
8476 || elf_dynversym (input) == 0)
8477 continue;
8478
8479 hdr = &elf_tdata (input)->dynsymtab_hdr;
8480
8481 symcount = hdr->sh_size / bed->s->sizeof_sym;
8482 if (elf_bad_symtab (input))
8483 {
8484 extsymcount = symcount;
8485 extsymoff = 0;
8486 }
8487 else
8488 {
8489 extsymcount = symcount - hdr->sh_info;
8490 extsymoff = hdr->sh_info;
8491 }
8492
8493 if (extsymcount == 0)
8494 continue;
8495
8496 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8497 NULL, NULL, NULL);
8498 if (isymbuf == NULL)
8499 return FALSE;
8500
8501 /* Read in any version definitions. */
8502 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8503 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8504 if (extversym == NULL)
8505 goto error_ret;
8506
8507 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8508 || (bfd_bread (extversym, versymhdr->sh_size, input)
8509 != versymhdr->sh_size))
8510 {
8511 free (extversym);
8512 error_ret:
8513 free (isymbuf);
8514 return FALSE;
8515 }
8516
8517 ever = extversym + extsymoff;
8518 isymend = isymbuf + extsymcount;
8519 for (isym = isymbuf; isym < isymend; isym++, ever++)
8520 {
8521 const char *name;
8522 Elf_Internal_Versym iver;
8523 unsigned short version_index;
8524
8525 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8526 || isym->st_shndx == SHN_UNDEF)
8527 continue;
8528
8529 name = bfd_elf_string_from_elf_section (input,
8530 hdr->sh_link,
8531 isym->st_name);
8532 if (strcmp (name, h->root.root.string) != 0)
8533 continue;
8534
8535 _bfd_elf_swap_versym_in (input, ever, &iver);
8536
d023c380
L
8537 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8538 && !(h->def_regular
8539 && h->forced_local))
c152c796
AM
8540 {
8541 /* If we have a non-hidden versioned sym, then it should
d023c380
L
8542 have provided a definition for the undefined sym unless
8543 it is defined in a non-shared object and forced local.
8544 */
c152c796
AM
8545 abort ();
8546 }
8547
8548 version_index = iver.vs_vers & VERSYM_VERSION;
8549 if (version_index == 1 || version_index == 2)
8550 {
8551 /* This is the base or first version. We can use it. */
8552 free (extversym);
8553 free (isymbuf);
8554 return TRUE;
8555 }
8556 }
8557
8558 free (extversym);
8559 free (isymbuf);
8560 }
8561
8562 return FALSE;
8563}
8564
8565/* Add an external symbol to the symbol table. This is called from
8566 the hash table traversal routine. When generating a shared object,
8567 we go through the symbol table twice. The first time we output
8568 anything that might have been forced to local scope in a version
8569 script. The second time we output the symbols that are still
8570 global symbols. */
8571
8572static bfd_boolean
7686d77d 8573elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 8574{
7686d77d 8575 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 8576 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
c152c796
AM
8577 struct elf_final_link_info *finfo = eoinfo->finfo;
8578 bfd_boolean strip;
8579 Elf_Internal_Sym sym;
8580 asection *input_sec;
8581 const struct elf_backend_data *bed;
6e0b88f1
AM
8582 long indx;
8583 int ret;
c152c796
AM
8584
8585 if (h->root.type == bfd_link_hash_warning)
8586 {
8587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8588 if (h->root.type == bfd_link_hash_new)
8589 return TRUE;
8590 }
8591
8592 /* Decide whether to output this symbol in this pass. */
8593 if (eoinfo->localsyms)
8594 {
f5385ebf 8595 if (!h->forced_local)
c152c796
AM
8596 return TRUE;
8597 }
8598 else
8599 {
f5385ebf 8600 if (h->forced_local)
c152c796
AM
8601 return TRUE;
8602 }
8603
8604 bed = get_elf_backend_data (finfo->output_bfd);
8605
12ac1cf5 8606 if (h->root.type == bfd_link_hash_undefined)
c152c796 8607 {
12ac1cf5
NC
8608 /* If we have an undefined symbol reference here then it must have
8609 come from a shared library that is being linked in. (Undefined
98da7939
L
8610 references in regular files have already been handled unless
8611 they are in unreferenced sections which are removed by garbage
8612 collection). */
12ac1cf5
NC
8613 bfd_boolean ignore_undef = FALSE;
8614
8615 /* Some symbols may be special in that the fact that they're
8616 undefined can be safely ignored - let backend determine that. */
8617 if (bed->elf_backend_ignore_undef_symbol)
8618 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8619
8620 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 8621 if (!ignore_undef
12ac1cf5 8622 && h->ref_dynamic
98da7939 8623 && (!h->ref_regular || finfo->info->gc_sections)
12ac1cf5
NC
8624 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8625 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
c152c796 8626 {
12ac1cf5 8627 if (! (finfo->info->callbacks->undefined_symbol
98da7939
L
8628 (finfo->info, h->root.root.string,
8629 h->ref_regular ? NULL : h->root.u.undef.abfd,
12ac1cf5
NC
8630 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8631 {
17d078c5 8632 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
8633 eoinfo->failed = TRUE;
8634 return FALSE;
8635 }
c152c796
AM
8636 }
8637 }
8638
8639 /* We should also warn if a forced local symbol is referenced from
8640 shared libraries. */
8641 if (! finfo->info->relocatable
8642 && (! finfo->info->shared)
f5385ebf
AM
8643 && h->forced_local
8644 && h->ref_dynamic
8645 && !h->dynamic_def
8646 && !h->dynamic_weak
c152c796
AM
8647 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8648 {
17d078c5
AM
8649 bfd *def_bfd;
8650 const char *msg;
8651
8652 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8653 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8654 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8655 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8656 else
8657 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8658 def_bfd = finfo->output_bfd;
8659 if (h->root.u.def.section != bfd_abs_section_ptr)
8660 def_bfd = h->root.u.def.section->owner;
8661 (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd,
8662 h->root.root.string);
8663 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8664 eoinfo->failed = TRUE;
8665 return FALSE;
8666 }
8667
8668 /* We don't want to output symbols that have never been mentioned by
8669 a regular file, or that we have been told to strip. However, if
8670 h->indx is set to -2, the symbol is used by a reloc and we must
8671 output it. */
8672 if (h->indx == -2)
8673 strip = FALSE;
f5385ebf 8674 else if ((h->def_dynamic
77cfaee6
AM
8675 || h->ref_dynamic
8676 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8677 && !h->def_regular
8678 && !h->ref_regular)
c152c796
AM
8679 strip = TRUE;
8680 else if (finfo->info->strip == strip_all)
8681 strip = TRUE;
8682 else if (finfo->info->strip == strip_some
8683 && bfd_hash_lookup (finfo->info->keep_hash,
8684 h->root.root.string, FALSE, FALSE) == NULL)
8685 strip = TRUE;
8686 else if (finfo->info->strip_discarded
8687 && (h->root.type == bfd_link_hash_defined
8688 || h->root.type == bfd_link_hash_defweak)
8689 && elf_discarded_section (h->root.u.def.section))
8690 strip = TRUE;
9e2278f5
AM
8691 else if ((h->root.type == bfd_link_hash_undefined
8692 || h->root.type == bfd_link_hash_undefweak)
8693 && h->root.u.undef.abfd != NULL
8694 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8695 strip = TRUE;
c152c796
AM
8696 else
8697 strip = FALSE;
8698
8699 /* If we're stripping it, and it's not a dynamic symbol, there's
57ca8ac7
L
8700 nothing else to do unless it is a forced local symbol or a
8701 STT_GNU_IFUNC symbol. */
c152c796
AM
8702 if (strip
8703 && h->dynindx == -1
57ca8ac7 8704 && h->type != STT_GNU_IFUNC
f5385ebf 8705 && !h->forced_local)
c152c796
AM
8706 return TRUE;
8707
8708 sym.st_value = 0;
8709 sym.st_size = h->size;
8710 sym.st_other = h->other;
f5385ebf 8711 if (h->forced_local)
935bd1e0
L
8712 {
8713 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8714 /* Turn off visibility on local symbol. */
8715 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8716 }
3e7a7d11
NC
8717 else if (h->unique_global)
8718 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
8719 else if (h->root.type == bfd_link_hash_undefweak
8720 || h->root.type == bfd_link_hash_defweak)
8721 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8722 else
8723 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 8724 sym.st_target_internal = h->target_internal;
c152c796
AM
8725
8726 switch (h->root.type)
8727 {
8728 default:
8729 case bfd_link_hash_new:
8730 case bfd_link_hash_warning:
8731 abort ();
8732 return FALSE;
8733
8734 case bfd_link_hash_undefined:
8735 case bfd_link_hash_undefweak:
8736 input_sec = bfd_und_section_ptr;
8737 sym.st_shndx = SHN_UNDEF;
8738 break;
8739
8740 case bfd_link_hash_defined:
8741 case bfd_link_hash_defweak:
8742 {
8743 input_sec = h->root.u.def.section;
8744 if (input_sec->output_section != NULL)
8745 {
8746 sym.st_shndx =
8747 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8748 input_sec->output_section);
8749 if (sym.st_shndx == SHN_BAD)
8750 {
8751 (*_bfd_error_handler)
d003868e
AM
8752 (_("%B: could not find output section %A for input section %A"),
8753 finfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 8754 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
8755 eoinfo->failed = TRUE;
8756 return FALSE;
8757 }
8758
8759 /* ELF symbols in relocatable files are section relative,
8760 but in nonrelocatable files they are virtual
8761 addresses. */
8762 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8763 if (! finfo->info->relocatable)
8764 {
8765 sym.st_value += input_sec->output_section->vma;
8766 if (h->type == STT_TLS)
8767 {
430a16a5
NC
8768 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8769 if (tls_sec != NULL)
8770 sym.st_value -= tls_sec->vma;
8771 else
8772 {
8773 /* The TLS section may have been garbage collected. */
8774 BFD_ASSERT (finfo->info->gc_sections
8775 && !input_sec->gc_mark);
8776 }
c152c796
AM
8777 }
8778 }
8779 }
8780 else
8781 {
8782 BFD_ASSERT (input_sec->owner == NULL
8783 || (input_sec->owner->flags & DYNAMIC) != 0);
8784 sym.st_shndx = SHN_UNDEF;
8785 input_sec = bfd_und_section_ptr;
8786 }
8787 }
8788 break;
8789
8790 case bfd_link_hash_common:
8791 input_sec = h->root.u.c.p->section;
a4d8e49b 8792 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8793 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8794 break;
8795
8796 case bfd_link_hash_indirect:
8797 /* These symbols are created by symbol versioning. They point
8798 to the decorated version of the name. For example, if the
8799 symbol foo@@GNU_1.2 is the default, which should be used when
8800 foo is used with no version, then we add an indirect symbol
8801 foo which points to foo@@GNU_1.2. We ignore these symbols,
8802 since the indirected symbol is already in the hash table. */
8803 return TRUE;
8804 }
8805
8806 /* Give the processor backend a chance to tweak the symbol value,
8807 and also to finish up anything that needs to be done for this
8808 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 8809 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 8810 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 8811 if ((h->type == STT_GNU_IFUNC
5f35ea9c 8812 && h->def_regular
3aa14d16
L
8813 && !finfo->info->relocatable)
8814 || ((h->dynindx != -1
8815 || h->forced_local)
8816 && ((finfo->info->shared
8817 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8818 || h->root.type != bfd_link_hash_undefweak))
8819 || !h->forced_local)
8820 && elf_hash_table (finfo->info)->dynamic_sections_created))
c152c796
AM
8821 {
8822 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8823 (finfo->output_bfd, finfo->info, h, &sym)))
8824 {
8825 eoinfo->failed = TRUE;
8826 return FALSE;
8827 }
8828 }
8829
8830 /* If we are marking the symbol as undefined, and there are no
8831 non-weak references to this symbol from a regular object, then
8832 mark the symbol as weak undefined; if there are non-weak
8833 references, mark the symbol as strong. We can't do this earlier,
8834 because it might not be marked as undefined until the
8835 finish_dynamic_symbol routine gets through with it. */
8836 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8837 && h->ref_regular
c152c796
AM
8838 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8839 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8840 {
8841 int bindtype;
2955ec4c
L
8842 unsigned int type = ELF_ST_TYPE (sym.st_info);
8843
8844 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8845 if (type == STT_GNU_IFUNC)
8846 type = STT_FUNC;
c152c796 8847
f5385ebf 8848 if (h->ref_regular_nonweak)
c152c796
AM
8849 bindtype = STB_GLOBAL;
8850 else
8851 bindtype = STB_WEAK;
2955ec4c 8852 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
8853 }
8854
bda987c2
CD
8855 /* If this is a symbol defined in a dynamic library, don't use the
8856 symbol size from the dynamic library. Relinking an executable
8857 against a new library may introduce gratuitous changes in the
8858 executable's symbols if we keep the size. */
8859 if (sym.st_shndx == SHN_UNDEF
8860 && !h->def_regular
8861 && h->def_dynamic)
8862 sym.st_size = 0;
8863
c152c796
AM
8864 /* If a non-weak symbol with non-default visibility is not defined
8865 locally, it is a fatal error. */
8866 if (! finfo->info->relocatable
8867 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8868 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8869 && h->root.type == bfd_link_hash_undefined
f5385ebf 8870 && !h->def_regular)
c152c796 8871 {
17d078c5
AM
8872 const char *msg;
8873
8874 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8875 msg = _("%B: protected symbol `%s' isn't defined");
8876 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8877 msg = _("%B: internal symbol `%s' isn't defined");
8878 else
8879 msg = _("%B: hidden symbol `%s' isn't defined");
8880 (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string);
8881 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8882 eoinfo->failed = TRUE;
8883 return FALSE;
8884 }
8885
8886 /* If this symbol should be put in the .dynsym section, then put it
8887 there now. We already know the symbol index. We also fill in
8888 the entry in the .hash section. */
8889 if (h->dynindx != -1
8890 && elf_hash_table (finfo->info)->dynamic_sections_created)
8891 {
c152c796
AM
8892 bfd_byte *esym;
8893
8894 sym.st_name = h->dynstr_index;
8895 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
c0d5a53d
L
8896 if (! check_dynsym (finfo->output_bfd, &sym))
8897 {
8898 eoinfo->failed = TRUE;
8899 return FALSE;
8900 }
c152c796
AM
8901 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8902
fdc90cb4
JJ
8903 if (finfo->hash_sec != NULL)
8904 {
8905 size_t hash_entry_size;
8906 bfd_byte *bucketpos;
8907 bfd_vma chain;
41198d0c
L
8908 size_t bucketcount;
8909 size_t bucket;
8910
8911 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8912 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8913
8914 hash_entry_size
8915 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8916 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8917 + (bucket + 2) * hash_entry_size);
8918 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8919 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8920 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8921 ((bfd_byte *) finfo->hash_sec->contents
8922 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8923 }
c152c796
AM
8924
8925 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8926 {
8927 Elf_Internal_Versym iversym;
8928 Elf_External_Versym *eversym;
8929
f5385ebf 8930 if (!h->def_regular)
c152c796
AM
8931 {
8932 if (h->verinfo.verdef == NULL)
8933 iversym.vs_vers = 0;
8934 else
8935 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8936 }
8937 else
8938 {
8939 if (h->verinfo.vertree == NULL)
8940 iversym.vs_vers = 1;
8941 else
8942 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
8943 if (finfo->info->create_default_symver)
8944 iversym.vs_vers++;
c152c796
AM
8945 }
8946
f5385ebf 8947 if (h->hidden)
c152c796
AM
8948 iversym.vs_vers |= VERSYM_HIDDEN;
8949
8950 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8951 eversym += h->dynindx;
8952 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8953 }
8954 }
8955
8956 /* If we're stripping it, then it was just a dynamic symbol, and
8957 there's nothing else to do. */
8958 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8959 return TRUE;
8960
6e0b88f1
AM
8961 indx = bfd_get_symcount (finfo->output_bfd);
8962 ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h);
8963 if (ret == 0)
c152c796
AM
8964 {
8965 eoinfo->failed = TRUE;
8966 return FALSE;
8967 }
6e0b88f1
AM
8968 else if (ret == 1)
8969 h->indx = indx;
8970 else if (h->indx == -2)
8971 abort();
c152c796
AM
8972
8973 return TRUE;
8974}
8975
cdd3575c
AM
8976/* Return TRUE if special handling is done for relocs in SEC against
8977 symbols defined in discarded sections. */
8978
c152c796
AM
8979static bfd_boolean
8980elf_section_ignore_discarded_relocs (asection *sec)
8981{
8982 const struct elf_backend_data *bed;
8983
cdd3575c
AM
8984 switch (sec->sec_info_type)
8985 {
8986 case ELF_INFO_TYPE_STABS:
8987 case ELF_INFO_TYPE_EH_FRAME:
8988 return TRUE;
8989 default:
8990 break;
8991 }
c152c796
AM
8992
8993 bed = get_elf_backend_data (sec->owner);
8994 if (bed->elf_backend_ignore_discarded_relocs != NULL
8995 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8996 return TRUE;
8997
8998 return FALSE;
8999}
9000
9e66c942
AM
9001/* Return a mask saying how ld should treat relocations in SEC against
9002 symbols defined in discarded sections. If this function returns
9003 COMPLAIN set, ld will issue a warning message. If this function
9004 returns PRETEND set, and the discarded section was link-once and the
9005 same size as the kept link-once section, ld will pretend that the
9006 symbol was actually defined in the kept section. Otherwise ld will
9007 zero the reloc (at least that is the intent, but some cooperation by
9008 the target dependent code is needed, particularly for REL targets). */
9009
8a696751
AM
9010unsigned int
9011_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9012{
9e66c942 9013 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9014 return PRETEND;
cdd3575c
AM
9015
9016 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9017 return 0;
cdd3575c
AM
9018
9019 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9020 return 0;
cdd3575c 9021
9e66c942 9022 return COMPLAIN | PRETEND;
cdd3575c
AM
9023}
9024
3d7f7666
L
9025/* Find a match between a section and a member of a section group. */
9026
9027static asection *
c0f00686
L
9028match_group_member (asection *sec, asection *group,
9029 struct bfd_link_info *info)
3d7f7666
L
9030{
9031 asection *first = elf_next_in_group (group);
9032 asection *s = first;
9033
9034 while (s != NULL)
9035 {
c0f00686 9036 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9037 return s;
9038
83180ade 9039 s = elf_next_in_group (s);
3d7f7666
L
9040 if (s == first)
9041 break;
9042 }
9043
9044 return NULL;
9045}
9046
01b3c8ab 9047/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9048 to replace it. Return the replacement if it is OK. Otherwise return
9049 NULL. */
01b3c8ab
L
9050
9051asection *
c0f00686 9052_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9053{
9054 asection *kept;
9055
9056 kept = sec->kept_section;
9057 if (kept != NULL)
9058 {
c2370991 9059 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9060 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9061 if (kept != NULL
9062 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9063 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9064 kept = NULL;
c2370991 9065 sec->kept_section = kept;
01b3c8ab
L
9066 }
9067 return kept;
9068}
9069
c152c796
AM
9070/* Link an input file into the linker output file. This function
9071 handles all the sections and relocations of the input file at once.
9072 This is so that we only have to read the local symbols once, and
9073 don't have to keep them in memory. */
9074
9075static bfd_boolean
9076elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9077{
ece5ef60 9078 int (*relocate_section)
c152c796
AM
9079 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9080 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9081 bfd *output_bfd;
9082 Elf_Internal_Shdr *symtab_hdr;
9083 size_t locsymcount;
9084 size_t extsymoff;
9085 Elf_Internal_Sym *isymbuf;
9086 Elf_Internal_Sym *isym;
9087 Elf_Internal_Sym *isymend;
9088 long *pindex;
9089 asection **ppsection;
9090 asection *o;
9091 const struct elf_backend_data *bed;
c152c796 9092 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9093 bfd_size_type address_size;
9094 bfd_vma r_type_mask;
9095 int r_sym_shift;
c152c796
AM
9096
9097 output_bfd = finfo->output_bfd;
9098 bed = get_elf_backend_data (output_bfd);
9099 relocate_section = bed->elf_backend_relocate_section;
9100
9101 /* If this is a dynamic object, we don't want to do anything here:
9102 we don't want the local symbols, and we don't want the section
9103 contents. */
9104 if ((input_bfd->flags & DYNAMIC) != 0)
9105 return TRUE;
9106
c152c796
AM
9107 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9108 if (elf_bad_symtab (input_bfd))
9109 {
9110 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9111 extsymoff = 0;
9112 }
9113 else
9114 {
9115 locsymcount = symtab_hdr->sh_info;
9116 extsymoff = symtab_hdr->sh_info;
9117 }
9118
9119 /* Read the local symbols. */
9120 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9121 if (isymbuf == NULL && locsymcount != 0)
9122 {
9123 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9124 finfo->internal_syms,
9125 finfo->external_syms,
9126 finfo->locsym_shndx);
9127 if (isymbuf == NULL)
9128 return FALSE;
9129 }
9130
9131 /* Find local symbol sections and adjust values of symbols in
9132 SEC_MERGE sections. Write out those local symbols we know are
9133 going into the output file. */
9134 isymend = isymbuf + locsymcount;
9135 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9136 isym < isymend;
9137 isym++, pindex++, ppsection++)
9138 {
9139 asection *isec;
9140 const char *name;
9141 Elf_Internal_Sym osym;
6e0b88f1
AM
9142 long indx;
9143 int ret;
c152c796
AM
9144
9145 *pindex = -1;
9146
9147 if (elf_bad_symtab (input_bfd))
9148 {
9149 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9150 {
9151 *ppsection = NULL;
9152 continue;
9153 }
9154 }
9155
9156 if (isym->st_shndx == SHN_UNDEF)
9157 isec = bfd_und_section_ptr;
c152c796
AM
9158 else if (isym->st_shndx == SHN_ABS)
9159 isec = bfd_abs_section_ptr;
9160 else if (isym->st_shndx == SHN_COMMON)
9161 isec = bfd_com_section_ptr;
9162 else
9163 {
cb33740c
AM
9164 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9165 if (isec == NULL)
9166 {
9167 /* Don't attempt to output symbols with st_shnx in the
9168 reserved range other than SHN_ABS and SHN_COMMON. */
9169 *ppsection = NULL;
9170 continue;
9171 }
9172 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9173 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9174 isym->st_value =
9175 _bfd_merged_section_offset (output_bfd, &isec,
9176 elf_section_data (isec)->sec_info,
9177 isym->st_value);
c152c796
AM
9178 }
9179
9180 *ppsection = isec;
9181
9182 /* Don't output the first, undefined, symbol. */
9183 if (ppsection == finfo->sections)
9184 continue;
9185
9186 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9187 {
9188 /* We never output section symbols. Instead, we use the
9189 section symbol of the corresponding section in the output
9190 file. */
9191 continue;
9192 }
9193
9194 /* If we are stripping all symbols, we don't want to output this
9195 one. */
9196 if (finfo->info->strip == strip_all)
9197 continue;
9198
9199 /* If we are discarding all local symbols, we don't want to
9200 output this one. If we are generating a relocatable output
9201 file, then some of the local symbols may be required by
9202 relocs; we output them below as we discover that they are
9203 needed. */
9204 if (finfo->info->discard == discard_all)
9205 continue;
9206
9207 /* If this symbol is defined in a section which we are
f02571c5
AM
9208 discarding, we don't need to keep it. */
9209 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9210 && isym->st_shndx < SHN_LORESERVE
9211 && bfd_section_removed_from_list (output_bfd,
9212 isec->output_section))
e75a280b
L
9213 continue;
9214
c152c796
AM
9215 /* Get the name of the symbol. */
9216 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9217 isym->st_name);
9218 if (name == NULL)
9219 return FALSE;
9220
9221 /* See if we are discarding symbols with this name. */
9222 if ((finfo->info->strip == strip_some
9223 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9224 == NULL))
9225 || (((finfo->info->discard == discard_sec_merge
9226 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9227 || finfo->info->discard == discard_l)
9228 && bfd_is_local_label_name (input_bfd, name)))
9229 continue;
9230
c152c796
AM
9231 osym = *isym;
9232
9233 /* Adjust the section index for the output file. */
9234 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9235 isec->output_section);
9236 if (osym.st_shndx == SHN_BAD)
9237 return FALSE;
9238
c152c796
AM
9239 /* ELF symbols in relocatable files are section relative, but
9240 in executable files they are virtual addresses. Note that
9241 this code assumes that all ELF sections have an associated
9242 BFD section with a reasonable value for output_offset; below
9243 we assume that they also have a reasonable value for
9244 output_section. Any special sections must be set up to meet
9245 these requirements. */
9246 osym.st_value += isec->output_offset;
9247 if (! finfo->info->relocatable)
9248 {
9249 osym.st_value += isec->output_section->vma;
9250 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9251 {
9252 /* STT_TLS symbols are relative to PT_TLS segment base. */
9253 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9254 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9255 }
9256 }
9257
6e0b88f1
AM
9258 indx = bfd_get_symcount (output_bfd);
9259 ret = elf_link_output_sym (finfo, name, &osym, isec, NULL);
9260 if (ret == 0)
c152c796 9261 return FALSE;
6e0b88f1
AM
9262 else if (ret == 1)
9263 *pindex = indx;
c152c796
AM
9264 }
9265
310fd250
L
9266 if (bed->s->arch_size == 32)
9267 {
9268 r_type_mask = 0xff;
9269 r_sym_shift = 8;
9270 address_size = 4;
9271 }
9272 else
9273 {
9274 r_type_mask = 0xffffffff;
9275 r_sym_shift = 32;
9276 address_size = 8;
9277 }
9278
c152c796
AM
9279 /* Relocate the contents of each section. */
9280 sym_hashes = elf_sym_hashes (input_bfd);
9281 for (o = input_bfd->sections; o != NULL; o = o->next)
9282 {
9283 bfd_byte *contents;
9284
9285 if (! o->linker_mark)
9286 {
9287 /* This section was omitted from the link. */
9288 continue;
9289 }
9290
bcacc0f5
AM
9291 if (finfo->info->relocatable
9292 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9293 {
9294 /* Deal with the group signature symbol. */
9295 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9296 unsigned long symndx = sec_data->this_hdr.sh_info;
9297 asection *osec = o->output_section;
9298
9299 if (symndx >= locsymcount
9300 || (elf_bad_symtab (input_bfd)
9301 && finfo->sections[symndx] == NULL))
9302 {
9303 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9304 while (h->root.type == bfd_link_hash_indirect
9305 || h->root.type == bfd_link_hash_warning)
9306 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9307 /* Arrange for symbol to be output. */
9308 h->indx = -2;
9309 elf_section_data (osec)->this_hdr.sh_info = -2;
9310 }
9311 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9312 {
9313 /* We'll use the output section target_index. */
9314 asection *sec = finfo->sections[symndx]->output_section;
9315 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9316 }
9317 else
9318 {
9319 if (finfo->indices[symndx] == -1)
9320 {
9321 /* Otherwise output the local symbol now. */
9322 Elf_Internal_Sym sym = isymbuf[symndx];
9323 asection *sec = finfo->sections[symndx]->output_section;
9324 const char *name;
6e0b88f1
AM
9325 long indx;
9326 int ret;
bcacc0f5
AM
9327
9328 name = bfd_elf_string_from_elf_section (input_bfd,
9329 symtab_hdr->sh_link,
9330 sym.st_name);
9331 if (name == NULL)
9332 return FALSE;
9333
9334 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9335 sec);
9336 if (sym.st_shndx == SHN_BAD)
9337 return FALSE;
9338
9339 sym.st_value += o->output_offset;
9340
6e0b88f1
AM
9341 indx = bfd_get_symcount (output_bfd);
9342 ret = elf_link_output_sym (finfo, name, &sym, o, NULL);
9343 if (ret == 0)
bcacc0f5 9344 return FALSE;
6e0b88f1
AM
9345 else if (ret == 1)
9346 finfo->indices[symndx] = indx;
9347 else
9348 abort ();
bcacc0f5
AM
9349 }
9350 elf_section_data (osec)->this_hdr.sh_info
9351 = finfo->indices[symndx];
9352 }
9353 }
9354
c152c796 9355 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9356 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9357 continue;
9358
9359 if ((o->flags & SEC_LINKER_CREATED) != 0)
9360 {
9361 /* Section was created by _bfd_elf_link_create_dynamic_sections
9362 or somesuch. */
9363 continue;
9364 }
9365
9366 /* Get the contents of the section. They have been cached by a
9367 relaxation routine. Note that o is a section in an input
9368 file, so the contents field will not have been set by any of
9369 the routines which work on output files. */
9370 if (elf_section_data (o)->this_hdr.contents != NULL)
9371 contents = elf_section_data (o)->this_hdr.contents;
9372 else
9373 {
9374 contents = finfo->contents;
4a114e3e 9375 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9376 return FALSE;
9377 }
9378
9379 if ((o->flags & SEC_RELOC) != 0)
9380 {
9381 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9382 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9383 int action_discarded;
ece5ef60 9384 int ret;
c152c796
AM
9385
9386 /* Get the swapped relocs. */
9387 internal_relocs
9388 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9389 finfo->internal_relocs, FALSE);
9390 if (internal_relocs == NULL
9391 && o->reloc_count > 0)
9392 return FALSE;
9393
310fd250
L
9394 /* We need to reverse-copy input .ctors/.dtors sections if
9395 they are placed in .init_array/.finit_array for output. */
9396 if (o->size > address_size
9397 && ((strncmp (o->name, ".ctors", 6) == 0
9398 && strcmp (o->output_section->name,
9399 ".init_array") == 0)
9400 || (strncmp (o->name, ".dtors", 6) == 0
9401 && strcmp (o->output_section->name,
9402 ".fini_array") == 0))
9403 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9404 {
310fd250
L
9405 if (o->size != o->reloc_count * address_size)
9406 {
9407 (*_bfd_error_handler)
9408 (_("error: %B: size of section %A is not "
9409 "multiple of address size"),
9410 input_bfd, o);
9411 bfd_set_error (bfd_error_on_input);
9412 return FALSE;
9413 }
9414 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9415 }
9416
0f02bbd9 9417 action_discarded = -1;
c152c796 9418 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9419 action_discarded = (*bed->action_discarded) (o);
9420
9421 /* Run through the relocs evaluating complex reloc symbols and
9422 looking for relocs against symbols from discarded sections
9423 or section symbols from removed link-once sections.
9424 Complain about relocs against discarded sections. Zero
9425 relocs against removed link-once sections. */
9426
9427 rel = internal_relocs;
9428 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9429 for ( ; rel < relend; rel++)
c152c796 9430 {
0f02bbd9
AM
9431 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9432 unsigned int s_type;
9433 asection **ps, *sec;
9434 struct elf_link_hash_entry *h = NULL;
9435 const char *sym_name;
c152c796 9436
0f02bbd9
AM
9437 if (r_symndx == STN_UNDEF)
9438 continue;
c152c796 9439
0f02bbd9
AM
9440 if (r_symndx >= locsymcount
9441 || (elf_bad_symtab (input_bfd)
9442 && finfo->sections[r_symndx] == NULL))
9443 {
9444 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9445
0f02bbd9
AM
9446 /* Badly formatted input files can contain relocs that
9447 reference non-existant symbols. Check here so that
9448 we do not seg fault. */
9449 if (h == NULL)
c152c796 9450 {
0f02bbd9 9451 char buffer [32];
dce669a1 9452
0f02bbd9
AM
9453 sprintf_vma (buffer, rel->r_info);
9454 (*_bfd_error_handler)
9455 (_("error: %B contains a reloc (0x%s) for section %A "
9456 "that references a non-existent global symbol"),
9457 input_bfd, o, buffer);
9458 bfd_set_error (bfd_error_bad_value);
9459 return FALSE;
9460 }
3b36f7e6 9461
0f02bbd9
AM
9462 while (h->root.type == bfd_link_hash_indirect
9463 || h->root.type == bfd_link_hash_warning)
9464 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9465
0f02bbd9 9466 s_type = h->type;
cdd3575c 9467
0f02bbd9
AM
9468 ps = NULL;
9469 if (h->root.type == bfd_link_hash_defined
9470 || h->root.type == bfd_link_hash_defweak)
9471 ps = &h->root.u.def.section;
9472
9473 sym_name = h->root.root.string;
9474 }
9475 else
9476 {
9477 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9478
9479 s_type = ELF_ST_TYPE (sym->st_info);
9480 ps = &finfo->sections[r_symndx];
9481 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9482 sym, *ps);
9483 }
c152c796 9484
c301e700
DD
9485 if ((s_type == STT_RELC || s_type == STT_SRELC)
9486 && !finfo->info->relocatable)
0f02bbd9
AM
9487 {
9488 bfd_vma val;
9489 bfd_vma dot = (rel->r_offset
9490 + o->output_offset + o->output_section->vma);
9491#ifdef DEBUG
9492 printf ("Encountered a complex symbol!");
9493 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
9494 input_bfd->filename, o->name,
9495 (long) (rel - internal_relocs));
0f02bbd9
AM
9496 printf (" symbol: idx %8.8lx, name %s\n",
9497 r_symndx, sym_name);
9498 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9499 (unsigned long) rel->r_info,
9500 (unsigned long) rel->r_offset);
9501#endif
9502 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9503 isymbuf, locsymcount, s_type == STT_SRELC))
9504 return FALSE;
9505
9506 /* Symbol evaluated OK. Update to absolute value. */
9507 set_symbol_value (input_bfd, isymbuf, locsymcount,
9508 r_symndx, val);
9509 continue;
9510 }
9511
9512 if (action_discarded != -1 && ps != NULL)
9513 {
cdd3575c
AM
9514 /* Complain if the definition comes from a
9515 discarded section. */
9516 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9517 {
cf35638d 9518 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 9519 if (action_discarded & COMPLAIN)
e1fffbe6
AM
9520 (*finfo->info->callbacks->einfo)
9521 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9522 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9523 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9524
87e5235d 9525 /* Try to do the best we can to support buggy old
e0ae6d6f 9526 versions of gcc. Pretend that the symbol is
87e5235d
AM
9527 really defined in the kept linkonce section.
9528 FIXME: This is quite broken. Modifying the
9529 symbol here means we will be changing all later
e0ae6d6f 9530 uses of the symbol, not just in this section. */
0f02bbd9 9531 if (action_discarded & PRETEND)
87e5235d 9532 {
01b3c8ab
L
9533 asection *kept;
9534
c0f00686
L
9535 kept = _bfd_elf_check_kept_section (sec,
9536 finfo->info);
01b3c8ab 9537 if (kept != NULL)
87e5235d
AM
9538 {
9539 *ps = kept;
9540 continue;
9541 }
9542 }
c152c796
AM
9543 }
9544 }
9545 }
9546
9547 /* Relocate the section by invoking a back end routine.
9548
9549 The back end routine is responsible for adjusting the
9550 section contents as necessary, and (if using Rela relocs
9551 and generating a relocatable output file) adjusting the
9552 reloc addend as necessary.
9553
9554 The back end routine does not have to worry about setting
9555 the reloc address or the reloc symbol index.
9556
9557 The back end routine is given a pointer to the swapped in
9558 internal symbols, and can access the hash table entries
9559 for the external symbols via elf_sym_hashes (input_bfd).
9560
9561 When generating relocatable output, the back end routine
9562 must handle STB_LOCAL/STT_SECTION symbols specially. The
9563 output symbol is going to be a section symbol
9564 corresponding to the output section, which will require
9565 the addend to be adjusted. */
9566
ece5ef60 9567 ret = (*relocate_section) (output_bfd, finfo->info,
c152c796
AM
9568 input_bfd, o, contents,
9569 internal_relocs,
9570 isymbuf,
ece5ef60
AM
9571 finfo->sections);
9572 if (!ret)
c152c796
AM
9573 return FALSE;
9574
ece5ef60
AM
9575 if (ret == 2
9576 || finfo->info->relocatable
9577 || finfo->info->emitrelocations)
c152c796
AM
9578 {
9579 Elf_Internal_Rela *irela;
d4730f92 9580 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
9581 bfd_vma last_offset;
9582 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
9583 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9584 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 9585 unsigned int next_erel;
c152c796 9586 bfd_boolean rela_normal;
d4730f92 9587 struct bfd_elf_section_data *esdi, *esdo;
c152c796 9588
d4730f92
BS
9589 esdi = elf_section_data (o);
9590 esdo = elf_section_data (o->output_section);
9591 rela_normal = FALSE;
c152c796
AM
9592
9593 /* Adjust the reloc addresses and symbol indices. */
9594
9595 irela = internal_relocs;
9596 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
9597 rel_hash = esdo->rel.hashes + esdo->rel.count;
9598 /* We start processing the REL relocs, if any. When we reach
9599 IRELAMID in the loop, we switch to the RELA relocs. */
9600 irelamid = irela;
9601 if (esdi->rel.hdr != NULL)
9602 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9603 * bed->s->int_rels_per_ext_rel);
eac338cf 9604 rel_hash_list = rel_hash;
d4730f92 9605 rela_hash_list = NULL;
c152c796
AM
9606 last_offset = o->output_offset;
9607 if (!finfo->info->relocatable)
9608 last_offset += o->output_section->vma;
9609 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9610 {
9611 unsigned long r_symndx;
9612 asection *sec;
9613 Elf_Internal_Sym sym;
9614
9615 if (next_erel == bed->s->int_rels_per_ext_rel)
9616 {
9617 rel_hash++;
9618 next_erel = 0;
9619 }
9620
d4730f92
BS
9621 if (irela == irelamid)
9622 {
9623 rel_hash = esdo->rela.hashes + esdo->rela.count;
9624 rela_hash_list = rel_hash;
9625 rela_normal = bed->rela_normal;
9626 }
9627
c152c796
AM
9628 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9629 finfo->info, o,
9630 irela->r_offset);
9631 if (irela->r_offset >= (bfd_vma) -2)
9632 {
9633 /* This is a reloc for a deleted entry or somesuch.
9634 Turn it into an R_*_NONE reloc, at the same
9635 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9636 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9637 being ordered. */
9638 irela->r_offset = last_offset;
9639 irela->r_info = 0;
9640 irela->r_addend = 0;
9641 continue;
9642 }
9643
9644 irela->r_offset += o->output_offset;
9645
9646 /* Relocs in an executable have to be virtual addresses. */
9647 if (!finfo->info->relocatable)
9648 irela->r_offset += o->output_section->vma;
9649
9650 last_offset = irela->r_offset;
9651
9652 r_symndx = irela->r_info >> r_sym_shift;
9653 if (r_symndx == STN_UNDEF)
9654 continue;
9655
9656 if (r_symndx >= locsymcount
9657 || (elf_bad_symtab (input_bfd)
9658 && finfo->sections[r_symndx] == NULL))
9659 {
9660 struct elf_link_hash_entry *rh;
9661 unsigned long indx;
9662
9663 /* This is a reloc against a global symbol. We
9664 have not yet output all the local symbols, so
9665 we do not know the symbol index of any global
9666 symbol. We set the rel_hash entry for this
9667 reloc to point to the global hash table entry
9668 for this symbol. The symbol index is then
ee75fd95 9669 set at the end of bfd_elf_final_link. */
c152c796
AM
9670 indx = r_symndx - extsymoff;
9671 rh = elf_sym_hashes (input_bfd)[indx];
9672 while (rh->root.type == bfd_link_hash_indirect
9673 || rh->root.type == bfd_link_hash_warning)
9674 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9675
9676 /* Setting the index to -2 tells
9677 elf_link_output_extsym that this symbol is
9678 used by a reloc. */
9679 BFD_ASSERT (rh->indx < 0);
9680 rh->indx = -2;
9681
9682 *rel_hash = rh;
9683
9684 continue;
9685 }
9686
9687 /* This is a reloc against a local symbol. */
9688
9689 *rel_hash = NULL;
9690 sym = isymbuf[r_symndx];
9691 sec = finfo->sections[r_symndx];
9692 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9693 {
9694 /* I suppose the backend ought to fill in the
9695 section of any STT_SECTION symbol against a
6a8d1586 9696 processor specific section. */
cf35638d 9697 r_symndx = STN_UNDEF;
6a8d1586
AM
9698 if (bfd_is_abs_section (sec))
9699 ;
c152c796
AM
9700 else if (sec == NULL || sec->owner == NULL)
9701 {
9702 bfd_set_error (bfd_error_bad_value);
9703 return FALSE;
9704 }
9705 else
9706 {
6a8d1586
AM
9707 asection *osec = sec->output_section;
9708
9709 /* If we have discarded a section, the output
9710 section will be the absolute section. In
ab96bf03
AM
9711 case of discarded SEC_MERGE sections, use
9712 the kept section. relocate_section should
9713 have already handled discarded linkonce
9714 sections. */
6a8d1586
AM
9715 if (bfd_is_abs_section (osec)
9716 && sec->kept_section != NULL
9717 && sec->kept_section->output_section != NULL)
9718 {
9719 osec = sec->kept_section->output_section;
9720 irela->r_addend -= osec->vma;
9721 }
9722
9723 if (!bfd_is_abs_section (osec))
9724 {
9725 r_symndx = osec->target_index;
cf35638d 9726 if (r_symndx == STN_UNDEF)
74541ad4
AM
9727 {
9728 struct elf_link_hash_table *htab;
9729 asection *oi;
9730
9731 htab = elf_hash_table (finfo->info);
9732 oi = htab->text_index_section;
9733 if ((osec->flags & SEC_READONLY) == 0
9734 && htab->data_index_section != NULL)
9735 oi = htab->data_index_section;
9736
9737 if (oi != NULL)
9738 {
9739 irela->r_addend += osec->vma - oi->vma;
9740 r_symndx = oi->target_index;
9741 }
9742 }
9743
cf35638d 9744 BFD_ASSERT (r_symndx != STN_UNDEF);
6a8d1586 9745 }
c152c796
AM
9746 }
9747
9748 /* Adjust the addend according to where the
9749 section winds up in the output section. */
9750 if (rela_normal)
9751 irela->r_addend += sec->output_offset;
9752 }
9753 else
9754 {
9755 if (finfo->indices[r_symndx] == -1)
9756 {
9757 unsigned long shlink;
9758 const char *name;
9759 asection *osec;
6e0b88f1 9760 long indx;
c152c796
AM
9761
9762 if (finfo->info->strip == strip_all)
9763 {
9764 /* You can't do ld -r -s. */
9765 bfd_set_error (bfd_error_invalid_operation);
9766 return FALSE;
9767 }
9768
9769 /* This symbol was skipped earlier, but
9770 since it is needed by a reloc, we
9771 must output it now. */
9772 shlink = symtab_hdr->sh_link;
9773 name = (bfd_elf_string_from_elf_section
9774 (input_bfd, shlink, sym.st_name));
9775 if (name == NULL)
9776 return FALSE;
9777
9778 osec = sec->output_section;
9779 sym.st_shndx =
9780 _bfd_elf_section_from_bfd_section (output_bfd,
9781 osec);
9782 if (sym.st_shndx == SHN_BAD)
9783 return FALSE;
9784
9785 sym.st_value += sec->output_offset;
9786 if (! finfo->info->relocatable)
9787 {
9788 sym.st_value += osec->vma;
9789 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9790 {
9791 /* STT_TLS symbols are relative to PT_TLS
9792 segment base. */
9793 BFD_ASSERT (elf_hash_table (finfo->info)
9794 ->tls_sec != NULL);
9795 sym.st_value -= (elf_hash_table (finfo->info)
9796 ->tls_sec->vma);
9797 }
9798 }
9799
6e0b88f1
AM
9800 indx = bfd_get_symcount (output_bfd);
9801 ret = elf_link_output_sym (finfo, name, &sym, sec,
9802 NULL);
9803 if (ret == 0)
c152c796 9804 return FALSE;
6e0b88f1
AM
9805 else if (ret == 1)
9806 finfo->indices[r_symndx] = indx;
9807 else
9808 abort ();
c152c796
AM
9809 }
9810
9811 r_symndx = finfo->indices[r_symndx];
9812 }
9813
9814 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9815 | (irela->r_info & r_type_mask));
9816 }
9817
9818 /* Swap out the relocs. */
d4730f92
BS
9819 input_rel_hdr = esdi->rel.hdr;
9820 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 9821 {
d4730f92
BS
9822 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9823 input_rel_hdr,
9824 internal_relocs,
9825 rel_hash_list))
9826 return FALSE;
c152c796
AM
9827 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9828 * bed->s->int_rels_per_ext_rel);
eac338cf 9829 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
9830 }
9831
9832 input_rela_hdr = esdi->rela.hdr;
9833 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9834 {
eac338cf 9835 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 9836 input_rela_hdr,
eac338cf 9837 internal_relocs,
d4730f92 9838 rela_hash_list))
c152c796
AM
9839 return FALSE;
9840 }
9841 }
9842 }
9843
9844 /* Write out the modified section contents. */
9845 if (bed->elf_backend_write_section
c7b8f16e
JB
9846 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9847 contents))
c152c796
AM
9848 {
9849 /* Section written out. */
9850 }
9851 else switch (o->sec_info_type)
9852 {
9853 case ELF_INFO_TYPE_STABS:
9854 if (! (_bfd_write_section_stabs
9855 (output_bfd,
9856 &elf_hash_table (finfo->info)->stab_info,
9857 o, &elf_section_data (o)->sec_info, contents)))
9858 return FALSE;
9859 break;
9860 case ELF_INFO_TYPE_MERGE:
9861 if (! _bfd_write_merged_section (output_bfd, o,
9862 elf_section_data (o)->sec_info))
9863 return FALSE;
9864 break;
9865 case ELF_INFO_TYPE_EH_FRAME:
9866 {
9867 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9868 o, contents))
9869 return FALSE;
9870 }
9871 break;
9872 default:
9873 {
5dabe785 9874 /* FIXME: octets_per_byte. */
310fd250
L
9875 if (! (o->flags & SEC_EXCLUDE))
9876 {
9877 file_ptr offset = (file_ptr) o->output_offset;
9878 bfd_size_type todo = o->size;
9879 if ((o->flags & SEC_ELF_REVERSE_COPY))
9880 {
9881 /* Reverse-copy input section to output. */
9882 do
9883 {
9884 todo -= address_size;
9885 if (! bfd_set_section_contents (output_bfd,
9886 o->output_section,
9887 contents + todo,
9888 offset,
9889 address_size))
9890 return FALSE;
9891 if (todo == 0)
9892 break;
9893 offset += address_size;
9894 }
9895 while (1);
9896 }
9897 else if (! bfd_set_section_contents (output_bfd,
9898 o->output_section,
9899 contents,
9900 offset, todo))
9901 return FALSE;
9902 }
c152c796
AM
9903 }
9904 break;
9905 }
9906 }
9907
9908 return TRUE;
9909}
9910
9911/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9912 requested by the linker, and does not come from any input file. This
c152c796
AM
9913 is used to build constructor and destructor tables when linking
9914 with -Ur. */
9915
9916static bfd_boolean
9917elf_reloc_link_order (bfd *output_bfd,
9918 struct bfd_link_info *info,
9919 asection *output_section,
9920 struct bfd_link_order *link_order)
9921{
9922 reloc_howto_type *howto;
9923 long indx;
9924 bfd_vma offset;
9925 bfd_vma addend;
d4730f92 9926 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
9927 struct elf_link_hash_entry **rel_hash_ptr;
9928 Elf_Internal_Shdr *rel_hdr;
9929 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9930 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9931 bfd_byte *erel;
9932 unsigned int i;
d4730f92 9933 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
9934
9935 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9936 if (howto == NULL)
9937 {
9938 bfd_set_error (bfd_error_bad_value);
9939 return FALSE;
9940 }
9941
9942 addend = link_order->u.reloc.p->addend;
9943
d4730f92
BS
9944 if (esdo->rel.hdr)
9945 reldata = &esdo->rel;
9946 else if (esdo->rela.hdr)
9947 reldata = &esdo->rela;
9948 else
9949 {
9950 reldata = NULL;
9951 BFD_ASSERT (0);
9952 }
9953
c152c796 9954 /* Figure out the symbol index. */
d4730f92 9955 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
9956 if (link_order->type == bfd_section_reloc_link_order)
9957 {
9958 indx = link_order->u.reloc.p->u.section->target_index;
9959 BFD_ASSERT (indx != 0);
9960 *rel_hash_ptr = NULL;
9961 }
9962 else
9963 {
9964 struct elf_link_hash_entry *h;
9965
9966 /* Treat a reloc against a defined symbol as though it were
9967 actually against the section. */
9968 h = ((struct elf_link_hash_entry *)
9969 bfd_wrapped_link_hash_lookup (output_bfd, info,
9970 link_order->u.reloc.p->u.name,
9971 FALSE, FALSE, TRUE));
9972 if (h != NULL
9973 && (h->root.type == bfd_link_hash_defined
9974 || h->root.type == bfd_link_hash_defweak))
9975 {
9976 asection *section;
9977
9978 section = h->root.u.def.section;
9979 indx = section->output_section->target_index;
9980 *rel_hash_ptr = NULL;
9981 /* It seems that we ought to add the symbol value to the
9982 addend here, but in practice it has already been added
9983 because it was passed to constructor_callback. */
9984 addend += section->output_section->vma + section->output_offset;
9985 }
9986 else if (h != NULL)
9987 {
9988 /* Setting the index to -2 tells elf_link_output_extsym that
9989 this symbol is used by a reloc. */
9990 h->indx = -2;
9991 *rel_hash_ptr = h;
9992 indx = 0;
9993 }
9994 else
9995 {
9996 if (! ((*info->callbacks->unattached_reloc)
9997 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9998 return FALSE;
9999 indx = 0;
10000 }
10001 }
10002
10003 /* If this is an inplace reloc, we must write the addend into the
10004 object file. */
10005 if (howto->partial_inplace && addend != 0)
10006 {
10007 bfd_size_type size;
10008 bfd_reloc_status_type rstat;
10009 bfd_byte *buf;
10010 bfd_boolean ok;
10011 const char *sym_name;
10012
a50b1753
NC
10013 size = (bfd_size_type) bfd_get_reloc_size (howto);
10014 buf = (bfd_byte *) bfd_zmalloc (size);
c152c796
AM
10015 if (buf == NULL)
10016 return FALSE;
10017 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10018 switch (rstat)
10019 {
10020 case bfd_reloc_ok:
10021 break;
10022
10023 default:
10024 case bfd_reloc_outofrange:
10025 abort ();
10026
10027 case bfd_reloc_overflow:
10028 if (link_order->type == bfd_section_reloc_link_order)
10029 sym_name = bfd_section_name (output_bfd,
10030 link_order->u.reloc.p->u.section);
10031 else
10032 sym_name = link_order->u.reloc.p->u.name;
10033 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10034 (info, NULL, sym_name, howto->name, addend, NULL,
10035 NULL, (bfd_vma) 0)))
c152c796
AM
10036 {
10037 free (buf);
10038 return FALSE;
10039 }
10040 break;
10041 }
10042 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10043 link_order->offset, size);
10044 free (buf);
10045 if (! ok)
10046 return FALSE;
10047 }
10048
10049 /* The address of a reloc is relative to the section in a
10050 relocatable file, and is a virtual address in an executable
10051 file. */
10052 offset = link_order->offset;
10053 if (! info->relocatable)
10054 offset += output_section->vma;
10055
10056 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10057 {
10058 irel[i].r_offset = offset;
10059 irel[i].r_info = 0;
10060 irel[i].r_addend = 0;
10061 }
10062 if (bed->s->arch_size == 32)
10063 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10064 else
10065 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10066
d4730f92 10067 rel_hdr = reldata->hdr;
c152c796
AM
10068 erel = rel_hdr->contents;
10069 if (rel_hdr->sh_type == SHT_REL)
10070 {
d4730f92 10071 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10072 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10073 }
10074 else
10075 {
10076 irel[0].r_addend = addend;
d4730f92 10077 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10078 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10079 }
10080
d4730f92 10081 ++reldata->count;
c152c796
AM
10082
10083 return TRUE;
10084}
10085
0b52efa6
PB
10086
10087/* Get the output vma of the section pointed to by the sh_link field. */
10088
10089static bfd_vma
10090elf_get_linked_section_vma (struct bfd_link_order *p)
10091{
10092 Elf_Internal_Shdr **elf_shdrp;
10093 asection *s;
10094 int elfsec;
10095
10096 s = p->u.indirect.section;
10097 elf_shdrp = elf_elfsections (s->owner);
10098 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10099 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10100 /* PR 290:
10101 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10102 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10103 sh_info fields. Hence we could get the situation
10104 where elfsec is 0. */
10105 if (elfsec == 0)
10106 {
10107 const struct elf_backend_data *bed
10108 = get_elf_backend_data (s->owner);
10109 if (bed->link_order_error_handler)
d003868e
AM
10110 bed->link_order_error_handler
10111 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10112 return 0;
10113 }
10114 else
10115 {
10116 s = elf_shdrp[elfsec]->bfd_section;
10117 return s->output_section->vma + s->output_offset;
10118 }
0b52efa6
PB
10119}
10120
10121
10122/* Compare two sections based on the locations of the sections they are
10123 linked to. Used by elf_fixup_link_order. */
10124
10125static int
10126compare_link_order (const void * a, const void * b)
10127{
10128 bfd_vma apos;
10129 bfd_vma bpos;
10130
10131 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10132 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10133 if (apos < bpos)
10134 return -1;
10135 return apos > bpos;
10136}
10137
10138
10139/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10140 order as their linked sections. Returns false if this could not be done
10141 because an output section includes both ordered and unordered
10142 sections. Ideally we'd do this in the linker proper. */
10143
10144static bfd_boolean
10145elf_fixup_link_order (bfd *abfd, asection *o)
10146{
10147 int seen_linkorder;
10148 int seen_other;
10149 int n;
10150 struct bfd_link_order *p;
10151 bfd *sub;
10152 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10153 unsigned elfsec;
0b52efa6 10154 struct bfd_link_order **sections;
d33cdfe3 10155 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10156 bfd_vma offset;
3b36f7e6 10157
d33cdfe3
L
10158 other_sec = NULL;
10159 linkorder_sec = NULL;
0b52efa6
PB
10160 seen_other = 0;
10161 seen_linkorder = 0;
8423293d 10162 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10163 {
d33cdfe3 10164 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10165 {
10166 s = p->u.indirect.section;
d33cdfe3
L
10167 sub = s->owner;
10168 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10169 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10170 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10171 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10172 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10173 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10174 {
10175 seen_linkorder++;
10176 linkorder_sec = s;
10177 }
0b52efa6 10178 else
d33cdfe3
L
10179 {
10180 seen_other++;
10181 other_sec = s;
10182 }
0b52efa6
PB
10183 }
10184 else
10185 seen_other++;
d33cdfe3
L
10186
10187 if (seen_other && seen_linkorder)
10188 {
10189 if (other_sec && linkorder_sec)
10190 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10191 o, linkorder_sec,
10192 linkorder_sec->owner, other_sec,
10193 other_sec->owner);
10194 else
10195 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10196 o);
10197 bfd_set_error (bfd_error_bad_value);
10198 return FALSE;
10199 }
0b52efa6
PB
10200 }
10201
10202 if (!seen_linkorder)
10203 return TRUE;
10204
0b52efa6 10205 sections = (struct bfd_link_order **)
14b1c01e
AM
10206 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10207 if (sections == NULL)
10208 return FALSE;
0b52efa6 10209 seen_linkorder = 0;
3b36f7e6 10210
8423293d 10211 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10212 {
10213 sections[seen_linkorder++] = p;
10214 }
10215 /* Sort the input sections in the order of their linked section. */
10216 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10217 compare_link_order);
10218
10219 /* Change the offsets of the sections. */
10220 offset = 0;
10221 for (n = 0; n < seen_linkorder; n++)
10222 {
10223 s = sections[n]->u.indirect.section;
461686a3 10224 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10225 s->output_offset = offset;
10226 sections[n]->offset = offset;
5dabe785 10227 /* FIXME: octets_per_byte. */
0b52efa6
PB
10228 offset += sections[n]->size;
10229 }
10230
4dd07732 10231 free (sections);
0b52efa6
PB
10232 return TRUE;
10233}
10234
10235
c152c796
AM
10236/* Do the final step of an ELF link. */
10237
10238bfd_boolean
10239bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10240{
10241 bfd_boolean dynamic;
10242 bfd_boolean emit_relocs;
10243 bfd *dynobj;
10244 struct elf_final_link_info finfo;
91d6fa6a
NC
10245 asection *o;
10246 struct bfd_link_order *p;
10247 bfd *sub;
c152c796
AM
10248 bfd_size_type max_contents_size;
10249 bfd_size_type max_external_reloc_size;
10250 bfd_size_type max_internal_reloc_count;
10251 bfd_size_type max_sym_count;
10252 bfd_size_type max_sym_shndx_count;
10253 file_ptr off;
10254 Elf_Internal_Sym elfsym;
10255 unsigned int i;
10256 Elf_Internal_Shdr *symtab_hdr;
10257 Elf_Internal_Shdr *symtab_shndx_hdr;
10258 Elf_Internal_Shdr *symstrtab_hdr;
10259 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10260 struct elf_outext_info eoinfo;
10261 bfd_boolean merged;
10262 size_t relativecount = 0;
10263 asection *reldyn = 0;
10264 bfd_size_type amt;
104d59d1
JM
10265 asection *attr_section = NULL;
10266 bfd_vma attr_size = 0;
10267 const char *std_attrs_section;
c152c796
AM
10268
10269 if (! is_elf_hash_table (info->hash))
10270 return FALSE;
10271
10272 if (info->shared)
10273 abfd->flags |= DYNAMIC;
10274
10275 dynamic = elf_hash_table (info)->dynamic_sections_created;
10276 dynobj = elf_hash_table (info)->dynobj;
10277
10278 emit_relocs = (info->relocatable
a4676736 10279 || info->emitrelocations);
c152c796
AM
10280
10281 finfo.info = info;
10282 finfo.output_bfd = abfd;
10283 finfo.symstrtab = _bfd_elf_stringtab_init ();
10284 if (finfo.symstrtab == NULL)
10285 return FALSE;
10286
10287 if (! dynamic)
10288 {
10289 finfo.dynsym_sec = NULL;
10290 finfo.hash_sec = NULL;
10291 finfo.symver_sec = NULL;
10292 }
10293 else
10294 {
10295 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10296 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
fdc90cb4 10297 BFD_ASSERT (finfo.dynsym_sec != NULL);
c152c796
AM
10298 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10299 /* Note that it is OK if symver_sec is NULL. */
10300 }
10301
10302 finfo.contents = NULL;
10303 finfo.external_relocs = NULL;
10304 finfo.internal_relocs = NULL;
10305 finfo.external_syms = NULL;
10306 finfo.locsym_shndx = NULL;
10307 finfo.internal_syms = NULL;
10308 finfo.indices = NULL;
10309 finfo.sections = NULL;
10310 finfo.symbuf = NULL;
10311 finfo.symshndxbuf = NULL;
10312 finfo.symbuf_count = 0;
10313 finfo.shndxbuf_size = 0;
10314
104d59d1
JM
10315 /* The object attributes have been merged. Remove the input
10316 sections from the link, and set the contents of the output
10317 secton. */
10318 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10319 for (o = abfd->sections; o != NULL; o = o->next)
10320 {
10321 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10322 || strcmp (o->name, ".gnu.attributes") == 0)
10323 {
10324 for (p = o->map_head.link_order; p != NULL; p = p->next)
10325 {
10326 asection *input_section;
10327
10328 if (p->type != bfd_indirect_link_order)
10329 continue;
10330 input_section = p->u.indirect.section;
10331 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10332 elf_link_input_bfd ignores this section. */
10333 input_section->flags &= ~SEC_HAS_CONTENTS;
10334 }
a0c8462f 10335
104d59d1
JM
10336 attr_size = bfd_elf_obj_attr_size (abfd);
10337 if (attr_size)
10338 {
10339 bfd_set_section_size (abfd, o, attr_size);
10340 attr_section = o;
10341 /* Skip this section later on. */
10342 o->map_head.link_order = NULL;
10343 }
10344 else
10345 o->flags |= SEC_EXCLUDE;
10346 }
10347 }
10348
c152c796
AM
10349 /* Count up the number of relocations we will output for each output
10350 section, so that we know the sizes of the reloc sections. We
10351 also figure out some maximum sizes. */
10352 max_contents_size = 0;
10353 max_external_reloc_size = 0;
10354 max_internal_reloc_count = 0;
10355 max_sym_count = 0;
10356 max_sym_shndx_count = 0;
10357 merged = FALSE;
10358 for (o = abfd->sections; o != NULL; o = o->next)
10359 {
10360 struct bfd_elf_section_data *esdo = elf_section_data (o);
10361 o->reloc_count = 0;
10362
8423293d 10363 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10364 {
10365 unsigned int reloc_count = 0;
10366 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10367
10368 if (p->type == bfd_section_reloc_link_order
10369 || p->type == bfd_symbol_reloc_link_order)
10370 reloc_count = 1;
10371 else if (p->type == bfd_indirect_link_order)
10372 {
10373 asection *sec;
10374
10375 sec = p->u.indirect.section;
10376 esdi = elf_section_data (sec);
10377
10378 /* Mark all sections which are to be included in the
10379 link. This will normally be every section. We need
10380 to do this so that we can identify any sections which
10381 the linker has decided to not include. */
10382 sec->linker_mark = TRUE;
10383
10384 if (sec->flags & SEC_MERGE)
10385 merged = TRUE;
10386
10387 if (info->relocatable || info->emitrelocations)
10388 reloc_count = sec->reloc_count;
10389 else if (bed->elf_backend_count_relocs)
58217f29 10390 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 10391
eea6121a
AM
10392 if (sec->rawsize > max_contents_size)
10393 max_contents_size = sec->rawsize;
10394 if (sec->size > max_contents_size)
10395 max_contents_size = sec->size;
c152c796
AM
10396
10397 /* We are interested in just local symbols, not all
10398 symbols. */
10399 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10400 && (sec->owner->flags & DYNAMIC) == 0)
10401 {
10402 size_t sym_count;
10403
10404 if (elf_bad_symtab (sec->owner))
10405 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10406 / bed->s->sizeof_sym);
10407 else
10408 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10409
10410 if (sym_count > max_sym_count)
10411 max_sym_count = sym_count;
10412
10413 if (sym_count > max_sym_shndx_count
10414 && elf_symtab_shndx (sec->owner) != 0)
10415 max_sym_shndx_count = sym_count;
10416
10417 if ((sec->flags & SEC_RELOC) != 0)
10418 {
d4730f92 10419 size_t ext_size = 0;
c152c796 10420
d4730f92
BS
10421 if (esdi->rel.hdr != NULL)
10422 ext_size = esdi->rel.hdr->sh_size;
10423 if (esdi->rela.hdr != NULL)
10424 ext_size += esdi->rela.hdr->sh_size;
7326c758 10425
c152c796
AM
10426 if (ext_size > max_external_reloc_size)
10427 max_external_reloc_size = ext_size;
10428 if (sec->reloc_count > max_internal_reloc_count)
10429 max_internal_reloc_count = sec->reloc_count;
10430 }
10431 }
10432 }
10433
10434 if (reloc_count == 0)
10435 continue;
10436
10437 o->reloc_count += reloc_count;
10438
d4730f92
BS
10439 if (p->type == bfd_indirect_link_order
10440 && (info->relocatable || info->emitrelocations))
c152c796 10441 {
d4730f92
BS
10442 if (esdi->rel.hdr)
10443 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10444 if (esdi->rela.hdr)
10445 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10446 }
10447 else
10448 {
10449 if (o->use_rela_p)
10450 esdo->rela.count += reloc_count;
2c2b4ed4 10451 else
d4730f92 10452 esdo->rel.count += reloc_count;
c152c796 10453 }
c152c796
AM
10454 }
10455
10456 if (o->reloc_count > 0)
10457 o->flags |= SEC_RELOC;
10458 else
10459 {
10460 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10461 set it (this is probably a bug) and if it is set
10462 assign_section_numbers will create a reloc section. */
10463 o->flags &=~ SEC_RELOC;
10464 }
10465
10466 /* If the SEC_ALLOC flag is not set, force the section VMA to
10467 zero. This is done in elf_fake_sections as well, but forcing
10468 the VMA to 0 here will ensure that relocs against these
10469 sections are handled correctly. */
10470 if ((o->flags & SEC_ALLOC) == 0
10471 && ! o->user_set_vma)
10472 o->vma = 0;
10473 }
10474
10475 if (! info->relocatable && merged)
10476 elf_link_hash_traverse (elf_hash_table (info),
10477 _bfd_elf_link_sec_merge_syms, abfd);
10478
10479 /* Figure out the file positions for everything but the symbol table
10480 and the relocs. We set symcount to force assign_section_numbers
10481 to create a symbol table. */
10482 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10483 BFD_ASSERT (! abfd->output_has_begun);
10484 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10485 goto error_return;
10486
ee75fd95 10487 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10488 for (o = abfd->sections; o != NULL; o = o->next)
10489 {
d4730f92 10490 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
10491 if ((o->flags & SEC_RELOC) != 0)
10492 {
d4730f92
BS
10493 if (esdo->rel.hdr
10494 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
10495 goto error_return;
10496
d4730f92
BS
10497 if (esdo->rela.hdr
10498 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
10499 goto error_return;
10500 }
10501
10502 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10503 to count upwards while actually outputting the relocations. */
d4730f92
BS
10504 esdo->rel.count = 0;
10505 esdo->rela.count = 0;
c152c796
AM
10506 }
10507
10508 _bfd_elf_assign_file_positions_for_relocs (abfd);
10509
10510 /* We have now assigned file positions for all the sections except
10511 .symtab and .strtab. We start the .symtab section at the current
10512 file position, and write directly to it. We build the .strtab
10513 section in memory. */
10514 bfd_get_symcount (abfd) = 0;
10515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10516 /* sh_name is set in prep_headers. */
10517 symtab_hdr->sh_type = SHT_SYMTAB;
10518 /* sh_flags, sh_addr and sh_size all start off zero. */
10519 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10520 /* sh_link is set in assign_section_numbers. */
10521 /* sh_info is set below. */
10522 /* sh_offset is set just below. */
72de5009 10523 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796
AM
10524
10525 off = elf_tdata (abfd)->next_file_pos;
10526 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10527
10528 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10529 incorrect. We do not yet know the size of the .symtab section.
10530 We correct next_file_pos below, after we do know the size. */
10531
10532 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10533 continuously seeking to the right position in the file. */
10534 if (! info->keep_memory || max_sym_count < 20)
10535 finfo.symbuf_size = 20;
10536 else
10537 finfo.symbuf_size = max_sym_count;
10538 amt = finfo.symbuf_size;
10539 amt *= bed->s->sizeof_sym;
a50b1753 10540 finfo.symbuf = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10541 if (finfo.symbuf == NULL)
10542 goto error_return;
4fbb74a6 10543 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10544 {
10545 /* Wild guess at number of output symbols. realloc'd as needed. */
10546 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10547 finfo.shndxbuf_size = amt;
10548 amt *= sizeof (Elf_External_Sym_Shndx);
a50b1753 10549 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
c152c796
AM
10550 if (finfo.symshndxbuf == NULL)
10551 goto error_return;
10552 }
10553
10554 /* Start writing out the symbol table. The first symbol is always a
10555 dummy symbol. */
10556 if (info->strip != strip_all
10557 || emit_relocs)
10558 {
10559 elfsym.st_value = 0;
10560 elfsym.st_size = 0;
10561 elfsym.st_info = 0;
10562 elfsym.st_other = 0;
10563 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 10564 elfsym.st_target_internal = 0;
6e0b88f1
AM
10565 if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10566 NULL) != 1)
c152c796
AM
10567 goto error_return;
10568 }
10569
c152c796
AM
10570 /* Output a symbol for each section. We output these even if we are
10571 discarding local symbols, since they are used for relocs. These
10572 symbols have no names. We store the index of each one in the
10573 index field of the section, so that we can find it again when
10574 outputting relocs. */
10575 if (info->strip != strip_all
10576 || emit_relocs)
10577 {
10578 elfsym.st_size = 0;
10579 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10580 elfsym.st_other = 0;
f0b5bb34 10581 elfsym.st_value = 0;
35fc36a8 10582 elfsym.st_target_internal = 0;
c152c796
AM
10583 for (i = 1; i < elf_numsections (abfd); i++)
10584 {
10585 o = bfd_section_from_elf_index (abfd, i);
10586 if (o != NULL)
f0b5bb34
AM
10587 {
10588 o->target_index = bfd_get_symcount (abfd);
10589 elfsym.st_shndx = i;
10590 if (!info->relocatable)
10591 elfsym.st_value = o->vma;
6e0b88f1 10592 if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1)
f0b5bb34
AM
10593 goto error_return;
10594 }
c152c796
AM
10595 }
10596 }
10597
10598 /* Allocate some memory to hold information read in from the input
10599 files. */
10600 if (max_contents_size != 0)
10601 {
a50b1753 10602 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
c152c796
AM
10603 if (finfo.contents == NULL)
10604 goto error_return;
10605 }
10606
10607 if (max_external_reloc_size != 0)
10608 {
10609 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10610 if (finfo.external_relocs == NULL)
10611 goto error_return;
10612 }
10613
10614 if (max_internal_reloc_count != 0)
10615 {
10616 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10617 amt *= sizeof (Elf_Internal_Rela);
a50b1753 10618 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
c152c796
AM
10619 if (finfo.internal_relocs == NULL)
10620 goto error_return;
10621 }
10622
10623 if (max_sym_count != 0)
10624 {
10625 amt = max_sym_count * bed->s->sizeof_sym;
a50b1753 10626 finfo.external_syms = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10627 if (finfo.external_syms == NULL)
10628 goto error_return;
10629
10630 amt = max_sym_count * sizeof (Elf_Internal_Sym);
a50b1753 10631 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
c152c796
AM
10632 if (finfo.internal_syms == NULL)
10633 goto error_return;
10634
10635 amt = max_sym_count * sizeof (long);
a50b1753 10636 finfo.indices = (long int *) bfd_malloc (amt);
c152c796
AM
10637 if (finfo.indices == NULL)
10638 goto error_return;
10639
10640 amt = max_sym_count * sizeof (asection *);
a50b1753 10641 finfo.sections = (asection **) bfd_malloc (amt);
c152c796
AM
10642 if (finfo.sections == NULL)
10643 goto error_return;
10644 }
10645
10646 if (max_sym_shndx_count != 0)
10647 {
10648 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
a50b1753 10649 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
c152c796
AM
10650 if (finfo.locsym_shndx == NULL)
10651 goto error_return;
10652 }
10653
10654 if (elf_hash_table (info)->tls_sec)
10655 {
10656 bfd_vma base, end = 0;
10657 asection *sec;
10658
10659 for (sec = elf_hash_table (info)->tls_sec;
10660 sec && (sec->flags & SEC_THREAD_LOCAL);
10661 sec = sec->next)
10662 {
3a800eb9 10663 bfd_size_type size = sec->size;
c152c796 10664
3a800eb9
AM
10665 if (size == 0
10666 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10667 {
91d6fa6a
NC
10668 struct bfd_link_order *ord = sec->map_tail.link_order;
10669
10670 if (ord != NULL)
10671 size = ord->offset + ord->size;
c152c796
AM
10672 }
10673 end = sec->vma + size;
10674 }
10675 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
10676 /* Only align end of TLS section if static TLS doesn't have special
10677 alignment requirements. */
10678 if (bed->static_tls_alignment == 1)
10679 end = align_power (end,
10680 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
10681 elf_hash_table (info)->tls_size = end - base;
10682 }
10683
0b52efa6
PB
10684 /* Reorder SHF_LINK_ORDER sections. */
10685 for (o = abfd->sections; o != NULL; o = o->next)
10686 {
10687 if (!elf_fixup_link_order (abfd, o))
10688 return FALSE;
10689 }
10690
c152c796
AM
10691 /* Since ELF permits relocations to be against local symbols, we
10692 must have the local symbols available when we do the relocations.
10693 Since we would rather only read the local symbols once, and we
10694 would rather not keep them in memory, we handle all the
10695 relocations for a single input file at the same time.
10696
10697 Unfortunately, there is no way to know the total number of local
10698 symbols until we have seen all of them, and the local symbol
10699 indices precede the global symbol indices. This means that when
10700 we are generating relocatable output, and we see a reloc against
10701 a global symbol, we can not know the symbol index until we have
10702 finished examining all the local symbols to see which ones we are
10703 going to output. To deal with this, we keep the relocations in
10704 memory, and don't output them until the end of the link. This is
10705 an unfortunate waste of memory, but I don't see a good way around
10706 it. Fortunately, it only happens when performing a relocatable
10707 link, which is not the common case. FIXME: If keep_memory is set
10708 we could write the relocs out and then read them again; I don't
10709 know how bad the memory loss will be. */
10710
10711 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10712 sub->output_has_begun = FALSE;
10713 for (o = abfd->sections; o != NULL; o = o->next)
10714 {
8423293d 10715 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10716 {
10717 if (p->type == bfd_indirect_link_order
10718 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10719 == bfd_target_elf_flavour)
10720 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10721 {
10722 if (! sub->output_has_begun)
10723 {
10724 if (! elf_link_input_bfd (&finfo, sub))
10725 goto error_return;
10726 sub->output_has_begun = TRUE;
10727 }
10728 }
10729 else if (p->type == bfd_section_reloc_link_order
10730 || p->type == bfd_symbol_reloc_link_order)
10731 {
10732 if (! elf_reloc_link_order (abfd, info, o, p))
10733 goto error_return;
10734 }
10735 else
10736 {
10737 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
10738 {
10739 if (p->type == bfd_indirect_link_order
10740 && (bfd_get_flavour (sub)
10741 == bfd_target_elf_flavour)
10742 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10743 != bed->s->elfclass))
10744 {
10745 const char *iclass, *oclass;
10746
10747 if (bed->s->elfclass == ELFCLASS64)
10748 {
10749 iclass = "ELFCLASS32";
10750 oclass = "ELFCLASS64";
10751 }
10752 else
10753 {
10754 iclass = "ELFCLASS64";
10755 oclass = "ELFCLASS32";
10756 }
10757
10758 bfd_set_error (bfd_error_wrong_format);
10759 (*_bfd_error_handler)
10760 (_("%B: file class %s incompatible with %s"),
10761 sub, iclass, oclass);
10762 }
10763
10764 goto error_return;
10765 }
c152c796
AM
10766 }
10767 }
10768 }
10769
c0f00686
L
10770 /* Free symbol buffer if needed. */
10771 if (!info->reduce_memory_overheads)
10772 {
10773 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10774 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10775 && elf_tdata (sub)->symbuf)
c0f00686
L
10776 {
10777 free (elf_tdata (sub)->symbuf);
10778 elf_tdata (sub)->symbuf = NULL;
10779 }
10780 }
10781
c152c796
AM
10782 /* Output any global symbols that got converted to local in a
10783 version script or due to symbol visibility. We do this in a
10784 separate step since ELF requires all local symbols to appear
10785 prior to any global symbols. FIXME: We should only do this if
10786 some global symbols were, in fact, converted to become local.
10787 FIXME: Will this work correctly with the Irix 5 linker? */
10788 eoinfo.failed = FALSE;
10789 eoinfo.finfo = &finfo;
10790 eoinfo.localsyms = TRUE;
7686d77d 10791 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
10792 if (eoinfo.failed)
10793 return FALSE;
10794
4e617b1e
PB
10795 /* If backend needs to output some local symbols not present in the hash
10796 table, do it now. */
10797 if (bed->elf_backend_output_arch_local_syms)
10798 {
6e0b88f1 10799 typedef int (*out_sym_func)
4e617b1e
PB
10800 (void *, const char *, Elf_Internal_Sym *, asection *,
10801 struct elf_link_hash_entry *);
10802
10803 if (! ((*bed->elf_backend_output_arch_local_syms)
10804 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10805 return FALSE;
10806 }
10807
c152c796
AM
10808 /* That wrote out all the local symbols. Finish up the symbol table
10809 with the global symbols. Even if we want to strip everything we
10810 can, we still need to deal with those global symbols that got
10811 converted to local in a version script. */
10812
10813 /* The sh_info field records the index of the first non local symbol. */
10814 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10815
10816 if (dynamic
10817 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10818 {
10819 Elf_Internal_Sym sym;
10820 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10821 long last_local = 0;
10822
10823 /* Write out the section symbols for the output sections. */
67687978 10824 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10825 {
10826 asection *s;
10827
10828 sym.st_size = 0;
10829 sym.st_name = 0;
10830 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10831 sym.st_other = 0;
35fc36a8 10832 sym.st_target_internal = 0;
c152c796
AM
10833
10834 for (s = abfd->sections; s != NULL; s = s->next)
10835 {
10836 int indx;
10837 bfd_byte *dest;
10838 long dynindx;
10839
c152c796 10840 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10841 if (dynindx <= 0)
10842 continue;
10843 indx = elf_section_data (s)->this_idx;
c152c796
AM
10844 BFD_ASSERT (indx > 0);
10845 sym.st_shndx = indx;
c0d5a53d
L
10846 if (! check_dynsym (abfd, &sym))
10847 return FALSE;
c152c796
AM
10848 sym.st_value = s->vma;
10849 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10850 if (last_local < dynindx)
10851 last_local = dynindx;
c152c796
AM
10852 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10853 }
c152c796
AM
10854 }
10855
10856 /* Write out the local dynsyms. */
10857 if (elf_hash_table (info)->dynlocal)
10858 {
10859 struct elf_link_local_dynamic_entry *e;
10860 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10861 {
10862 asection *s;
10863 bfd_byte *dest;
10864
935bd1e0 10865 /* Copy the internal symbol and turn off visibility.
c152c796
AM
10866 Note that we saved a word of storage and overwrote
10867 the original st_name with the dynstr_index. */
10868 sym = e->isym;
935bd1e0 10869 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 10870
cb33740c
AM
10871 s = bfd_section_from_elf_index (e->input_bfd,
10872 e->isym.st_shndx);
10873 if (s != NULL)
c152c796 10874 {
c152c796
AM
10875 sym.st_shndx =
10876 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10877 if (! check_dynsym (abfd, &sym))
10878 return FALSE;
c152c796
AM
10879 sym.st_value = (s->output_section->vma
10880 + s->output_offset
10881 + e->isym.st_value);
10882 }
10883
10884 if (last_local < e->dynindx)
10885 last_local = e->dynindx;
10886
10887 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10888 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10889 }
10890 }
10891
10892 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10893 last_local + 1;
10894 }
10895
10896 /* We get the global symbols from the hash table. */
10897 eoinfo.failed = FALSE;
10898 eoinfo.localsyms = FALSE;
10899 eoinfo.finfo = &finfo;
7686d77d 10900 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
10901 if (eoinfo.failed)
10902 return FALSE;
10903
10904 /* If backend needs to output some symbols not present in the hash
10905 table, do it now. */
10906 if (bed->elf_backend_output_arch_syms)
10907 {
6e0b88f1 10908 typedef int (*out_sym_func)
c152c796
AM
10909 (void *, const char *, Elf_Internal_Sym *, asection *,
10910 struct elf_link_hash_entry *);
10911
10912 if (! ((*bed->elf_backend_output_arch_syms)
10913 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10914 return FALSE;
10915 }
10916
10917 /* Flush all symbols to the file. */
10918 if (! elf_link_flush_output_syms (&finfo, bed))
10919 return FALSE;
10920
10921 /* Now we know the size of the symtab section. */
10922 off += symtab_hdr->sh_size;
10923
10924 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10925 if (symtab_shndx_hdr->sh_name != 0)
10926 {
10927 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10928 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10929 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10930 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10931 symtab_shndx_hdr->sh_size = amt;
10932
10933 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10934 off, TRUE);
10935
10936 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10937 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10938 return FALSE;
10939 }
10940
10941
10942 /* Finish up and write out the symbol string table (.strtab)
10943 section. */
10944 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10945 /* sh_name was set in prep_headers. */
10946 symstrtab_hdr->sh_type = SHT_STRTAB;
10947 symstrtab_hdr->sh_flags = 0;
10948 symstrtab_hdr->sh_addr = 0;
10949 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10950 symstrtab_hdr->sh_entsize = 0;
10951 symstrtab_hdr->sh_link = 0;
10952 symstrtab_hdr->sh_info = 0;
10953 /* sh_offset is set just below. */
10954 symstrtab_hdr->sh_addralign = 1;
10955
10956 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10957 elf_tdata (abfd)->next_file_pos = off;
10958
10959 if (bfd_get_symcount (abfd) > 0)
10960 {
10961 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10962 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10963 return FALSE;
10964 }
10965
10966 /* Adjust the relocs to have the correct symbol indices. */
10967 for (o = abfd->sections; o != NULL; o = o->next)
10968 {
d4730f92 10969 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
10970 if ((o->flags & SEC_RELOC) == 0)
10971 continue;
10972
d4730f92
BS
10973 if (esdo->rel.hdr != NULL)
10974 elf_link_adjust_relocs (abfd, &esdo->rel);
10975 if (esdo->rela.hdr != NULL)
10976 elf_link_adjust_relocs (abfd, &esdo->rela);
c152c796
AM
10977
10978 /* Set the reloc_count field to 0 to prevent write_relocs from
10979 trying to swap the relocs out itself. */
10980 o->reloc_count = 0;
10981 }
10982
10983 if (dynamic && info->combreloc && dynobj != NULL)
10984 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10985
10986 /* If we are linking against a dynamic object, or generating a
10987 shared library, finish up the dynamic linking information. */
10988 if (dynamic)
10989 {
10990 bfd_byte *dyncon, *dynconend;
10991
10992 /* Fix up .dynamic entries. */
10993 o = bfd_get_section_by_name (dynobj, ".dynamic");
10994 BFD_ASSERT (o != NULL);
10995
10996 dyncon = o->contents;
eea6121a 10997 dynconend = o->contents + o->size;
c152c796
AM
10998 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10999 {
11000 Elf_Internal_Dyn dyn;
11001 const char *name;
11002 unsigned int type;
11003
11004 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11005
11006 switch (dyn.d_tag)
11007 {
11008 default:
11009 continue;
11010 case DT_NULL:
11011 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11012 {
11013 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11014 {
11015 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11016 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11017 default: continue;
11018 }
11019 dyn.d_un.d_val = relativecount;
11020 relativecount = 0;
11021 break;
11022 }
11023 continue;
11024
11025 case DT_INIT:
11026 name = info->init_function;
11027 goto get_sym;
11028 case DT_FINI:
11029 name = info->fini_function;
11030 get_sym:
11031 {
11032 struct elf_link_hash_entry *h;
11033
11034 h = elf_link_hash_lookup (elf_hash_table (info), name,
11035 FALSE, FALSE, TRUE);
11036 if (h != NULL
11037 && (h->root.type == bfd_link_hash_defined
11038 || h->root.type == bfd_link_hash_defweak))
11039 {
bef26483 11040 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11041 o = h->root.u.def.section;
11042 if (o->output_section != NULL)
bef26483 11043 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11044 + o->output_offset);
11045 else
11046 {
11047 /* The symbol is imported from another shared
11048 library and does not apply to this one. */
bef26483 11049 dyn.d_un.d_ptr = 0;
c152c796
AM
11050 }
11051 break;
11052 }
11053 }
11054 continue;
11055
11056 case DT_PREINIT_ARRAYSZ:
11057 name = ".preinit_array";
11058 goto get_size;
11059 case DT_INIT_ARRAYSZ:
11060 name = ".init_array";
11061 goto get_size;
11062 case DT_FINI_ARRAYSZ:
11063 name = ".fini_array";
11064 get_size:
11065 o = bfd_get_section_by_name (abfd, name);
11066 if (o == NULL)
11067 {
11068 (*_bfd_error_handler)
d003868e 11069 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11070 goto error_return;
11071 }
eea6121a 11072 if (o->size == 0)
c152c796
AM
11073 (*_bfd_error_handler)
11074 (_("warning: %s section has zero size"), name);
eea6121a 11075 dyn.d_un.d_val = o->size;
c152c796
AM
11076 break;
11077
11078 case DT_PREINIT_ARRAY:
11079 name = ".preinit_array";
11080 goto get_vma;
11081 case DT_INIT_ARRAY:
11082 name = ".init_array";
11083 goto get_vma;
11084 case DT_FINI_ARRAY:
11085 name = ".fini_array";
11086 goto get_vma;
11087
11088 case DT_HASH:
11089 name = ".hash";
11090 goto get_vma;
fdc90cb4
JJ
11091 case DT_GNU_HASH:
11092 name = ".gnu.hash";
11093 goto get_vma;
c152c796
AM
11094 case DT_STRTAB:
11095 name = ".dynstr";
11096 goto get_vma;
11097 case DT_SYMTAB:
11098 name = ".dynsym";
11099 goto get_vma;
11100 case DT_VERDEF:
11101 name = ".gnu.version_d";
11102 goto get_vma;
11103 case DT_VERNEED:
11104 name = ".gnu.version_r";
11105 goto get_vma;
11106 case DT_VERSYM:
11107 name = ".gnu.version";
11108 get_vma:
11109 o = bfd_get_section_by_name (abfd, name);
11110 if (o == NULL)
11111 {
11112 (*_bfd_error_handler)
d003868e 11113 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11114 goto error_return;
11115 }
894891db
NC
11116 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11117 {
11118 (*_bfd_error_handler)
11119 (_("warning: section '%s' is being made into a note"), name);
11120 bfd_set_error (bfd_error_nonrepresentable_section);
11121 goto error_return;
11122 }
c152c796
AM
11123 dyn.d_un.d_ptr = o->vma;
11124 break;
11125
11126 case DT_REL:
11127 case DT_RELA:
11128 case DT_RELSZ:
11129 case DT_RELASZ:
11130 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11131 type = SHT_REL;
11132 else
11133 type = SHT_RELA;
11134 dyn.d_un.d_val = 0;
bef26483 11135 dyn.d_un.d_ptr = 0;
c152c796
AM
11136 for (i = 1; i < elf_numsections (abfd); i++)
11137 {
11138 Elf_Internal_Shdr *hdr;
11139
11140 hdr = elf_elfsections (abfd)[i];
11141 if (hdr->sh_type == type
11142 && (hdr->sh_flags & SHF_ALLOC) != 0)
11143 {
11144 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11145 dyn.d_un.d_val += hdr->sh_size;
11146 else
11147 {
bef26483
AM
11148 if (dyn.d_un.d_ptr == 0
11149 || hdr->sh_addr < dyn.d_un.d_ptr)
11150 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11151 }
11152 }
11153 }
11154 break;
11155 }
11156 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11157 }
11158 }
11159
11160 /* If we have created any dynamic sections, then output them. */
11161 if (dynobj != NULL)
11162 {
11163 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11164 goto error_return;
11165
943284cc
DJ
11166 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11167 if (info->warn_shared_textrel && info->shared)
11168 {
11169 bfd_byte *dyncon, *dynconend;
11170
11171 /* Fix up .dynamic entries. */
11172 o = bfd_get_section_by_name (dynobj, ".dynamic");
11173 BFD_ASSERT (o != NULL);
11174
11175 dyncon = o->contents;
11176 dynconend = o->contents + o->size;
11177 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11178 {
11179 Elf_Internal_Dyn dyn;
11180
11181 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11182
11183 if (dyn.d_tag == DT_TEXTREL)
11184 {
a0c8462f 11185 info->callbacks->einfo
9267588c 11186 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11187 break;
11188 }
11189 }
11190 }
11191
c152c796
AM
11192 for (o = dynobj->sections; o != NULL; o = o->next)
11193 {
11194 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11195 || o->size == 0
c152c796
AM
11196 || o->output_section == bfd_abs_section_ptr)
11197 continue;
11198 if ((o->flags & SEC_LINKER_CREATED) == 0)
11199 {
11200 /* At this point, we are only interested in sections
11201 created by _bfd_elf_link_create_dynamic_sections. */
11202 continue;
11203 }
3722b82f
AM
11204 if (elf_hash_table (info)->stab_info.stabstr == o)
11205 continue;
eea6121a
AM
11206 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11207 continue;
c152c796
AM
11208 if ((elf_section_data (o->output_section)->this_hdr.sh_type
11209 != SHT_STRTAB)
894891db 11210 && (strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0))
c152c796 11211 {
5dabe785 11212 /* FIXME: octets_per_byte. */
c152c796
AM
11213 if (! bfd_set_section_contents (abfd, o->output_section,
11214 o->contents,
11215 (file_ptr) o->output_offset,
eea6121a 11216 o->size))
c152c796
AM
11217 goto error_return;
11218 }
11219 else
11220 {
11221 /* The contents of the .dynstr section are actually in a
11222 stringtab. */
11223 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11224 if (bfd_seek (abfd, off, SEEK_SET) != 0
11225 || ! _bfd_elf_strtab_emit (abfd,
11226 elf_hash_table (info)->dynstr))
11227 goto error_return;
11228 }
11229 }
11230 }
11231
11232 if (info->relocatable)
11233 {
11234 bfd_boolean failed = FALSE;
11235
11236 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11237 if (failed)
11238 goto error_return;
11239 }
11240
11241 /* If we have optimized stabs strings, output them. */
3722b82f 11242 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11243 {
11244 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11245 goto error_return;
11246 }
11247
11248 if (info->eh_frame_hdr)
11249 {
11250 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11251 goto error_return;
11252 }
11253
11254 if (finfo.symstrtab != NULL)
11255 _bfd_stringtab_free (finfo.symstrtab);
11256 if (finfo.contents != NULL)
11257 free (finfo.contents);
11258 if (finfo.external_relocs != NULL)
11259 free (finfo.external_relocs);
11260 if (finfo.internal_relocs != NULL)
11261 free (finfo.internal_relocs);
11262 if (finfo.external_syms != NULL)
11263 free (finfo.external_syms);
11264 if (finfo.locsym_shndx != NULL)
11265 free (finfo.locsym_shndx);
11266 if (finfo.internal_syms != NULL)
11267 free (finfo.internal_syms);
11268 if (finfo.indices != NULL)
11269 free (finfo.indices);
11270 if (finfo.sections != NULL)
11271 free (finfo.sections);
11272 if (finfo.symbuf != NULL)
11273 free (finfo.symbuf);
11274 if (finfo.symshndxbuf != NULL)
11275 free (finfo.symshndxbuf);
11276 for (o = abfd->sections; o != NULL; o = o->next)
11277 {
d4730f92
BS
11278 struct bfd_elf_section_data *esdo = elf_section_data (o);
11279 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11280 free (esdo->rel.hashes);
11281 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11282 free (esdo->rela.hashes);
c152c796
AM
11283 }
11284
11285 elf_tdata (abfd)->linker = TRUE;
11286
104d59d1
JM
11287 if (attr_section)
11288 {
a50b1753 11289 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11290 if (contents == NULL)
d0f16d5e 11291 return FALSE; /* Bail out and fail. */
104d59d1
JM
11292 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11293 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11294 free (contents);
11295 }
11296
c152c796
AM
11297 return TRUE;
11298
11299 error_return:
11300 if (finfo.symstrtab != NULL)
11301 _bfd_stringtab_free (finfo.symstrtab);
11302 if (finfo.contents != NULL)
11303 free (finfo.contents);
11304 if (finfo.external_relocs != NULL)
11305 free (finfo.external_relocs);
11306 if (finfo.internal_relocs != NULL)
11307 free (finfo.internal_relocs);
11308 if (finfo.external_syms != NULL)
11309 free (finfo.external_syms);
11310 if (finfo.locsym_shndx != NULL)
11311 free (finfo.locsym_shndx);
11312 if (finfo.internal_syms != NULL)
11313 free (finfo.internal_syms);
11314 if (finfo.indices != NULL)
11315 free (finfo.indices);
11316 if (finfo.sections != NULL)
11317 free (finfo.sections);
11318 if (finfo.symbuf != NULL)
11319 free (finfo.symbuf);
11320 if (finfo.symshndxbuf != NULL)
11321 free (finfo.symshndxbuf);
11322 for (o = abfd->sections; o != NULL; o = o->next)
11323 {
d4730f92
BS
11324 struct bfd_elf_section_data *esdo = elf_section_data (o);
11325 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11326 free (esdo->rel.hashes);
11327 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11328 free (esdo->rela.hashes);
c152c796
AM
11329 }
11330
11331 return FALSE;
11332}
11333\f
5241d853
RS
11334/* Initialize COOKIE for input bfd ABFD. */
11335
11336static bfd_boolean
11337init_reloc_cookie (struct elf_reloc_cookie *cookie,
11338 struct bfd_link_info *info, bfd *abfd)
11339{
11340 Elf_Internal_Shdr *symtab_hdr;
11341 const struct elf_backend_data *bed;
11342
11343 bed = get_elf_backend_data (abfd);
11344 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11345
11346 cookie->abfd = abfd;
11347 cookie->sym_hashes = elf_sym_hashes (abfd);
11348 cookie->bad_symtab = elf_bad_symtab (abfd);
11349 if (cookie->bad_symtab)
11350 {
11351 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11352 cookie->extsymoff = 0;
11353 }
11354 else
11355 {
11356 cookie->locsymcount = symtab_hdr->sh_info;
11357 cookie->extsymoff = symtab_hdr->sh_info;
11358 }
11359
11360 if (bed->s->arch_size == 32)
11361 cookie->r_sym_shift = 8;
11362 else
11363 cookie->r_sym_shift = 32;
11364
11365 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11366 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11367 {
11368 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11369 cookie->locsymcount, 0,
11370 NULL, NULL, NULL);
11371 if (cookie->locsyms == NULL)
11372 {
11373 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11374 return FALSE;
11375 }
11376 if (info->keep_memory)
11377 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11378 }
11379 return TRUE;
11380}
11381
11382/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11383
11384static void
11385fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11386{
11387 Elf_Internal_Shdr *symtab_hdr;
11388
11389 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11390 if (cookie->locsyms != NULL
11391 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11392 free (cookie->locsyms);
11393}
11394
11395/* Initialize the relocation information in COOKIE for input section SEC
11396 of input bfd ABFD. */
11397
11398static bfd_boolean
11399init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11400 struct bfd_link_info *info, bfd *abfd,
11401 asection *sec)
11402{
11403 const struct elf_backend_data *bed;
11404
11405 if (sec->reloc_count == 0)
11406 {
11407 cookie->rels = NULL;
11408 cookie->relend = NULL;
11409 }
11410 else
11411 {
11412 bed = get_elf_backend_data (abfd);
11413
11414 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11415 info->keep_memory);
11416 if (cookie->rels == NULL)
11417 return FALSE;
11418 cookie->rel = cookie->rels;
11419 cookie->relend = (cookie->rels
11420 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11421 }
11422 cookie->rel = cookie->rels;
11423 return TRUE;
11424}
11425
11426/* Free the memory allocated by init_reloc_cookie_rels,
11427 if appropriate. */
11428
11429static void
11430fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11431 asection *sec)
11432{
11433 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11434 free (cookie->rels);
11435}
11436
11437/* Initialize the whole of COOKIE for input section SEC. */
11438
11439static bfd_boolean
11440init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11441 struct bfd_link_info *info,
11442 asection *sec)
11443{
11444 if (!init_reloc_cookie (cookie, info, sec->owner))
11445 goto error1;
11446 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11447 goto error2;
11448 return TRUE;
11449
11450 error2:
11451 fini_reloc_cookie (cookie, sec->owner);
11452 error1:
11453 return FALSE;
11454}
11455
11456/* Free the memory allocated by init_reloc_cookie_for_section,
11457 if appropriate. */
11458
11459static void
11460fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11461 asection *sec)
11462{
11463 fini_reloc_cookie_rels (cookie, sec);
11464 fini_reloc_cookie (cookie, sec->owner);
11465}
11466\f
c152c796
AM
11467/* Garbage collect unused sections. */
11468
07adf181
AM
11469/* Default gc_mark_hook. */
11470
11471asection *
11472_bfd_elf_gc_mark_hook (asection *sec,
11473 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11474 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11475 struct elf_link_hash_entry *h,
11476 Elf_Internal_Sym *sym)
11477{
bde6f3eb
L
11478 const char *sec_name;
11479
07adf181
AM
11480 if (h != NULL)
11481 {
11482 switch (h->root.type)
11483 {
11484 case bfd_link_hash_defined:
11485 case bfd_link_hash_defweak:
11486 return h->root.u.def.section;
11487
11488 case bfd_link_hash_common:
11489 return h->root.u.c.p->section;
11490
bde6f3eb
L
11491 case bfd_link_hash_undefined:
11492 case bfd_link_hash_undefweak:
11493 /* To work around a glibc bug, keep all XXX input sections
11494 when there is an as yet undefined reference to __start_XXX
11495 or __stop_XXX symbols. The linker will later define such
11496 symbols for orphan input sections that have a name
11497 representable as a C identifier. */
11498 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11499 sec_name = h->root.root.string + 8;
11500 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11501 sec_name = h->root.root.string + 7;
11502 else
11503 sec_name = NULL;
11504
11505 if (sec_name && *sec_name != '\0')
11506 {
11507 bfd *i;
11508
11509 for (i = info->input_bfds; i; i = i->link_next)
11510 {
11511 sec = bfd_get_section_by_name (i, sec_name);
11512 if (sec)
11513 sec->flags |= SEC_KEEP;
11514 }
11515 }
11516 break;
11517
07adf181
AM
11518 default:
11519 break;
11520 }
11521 }
11522 else
11523 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11524
11525 return NULL;
11526}
11527
5241d853
RS
11528/* COOKIE->rel describes a relocation against section SEC, which is
11529 a section we've decided to keep. Return the section that contains
11530 the relocation symbol, or NULL if no section contains it. */
11531
11532asection *
11533_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11534 elf_gc_mark_hook_fn gc_mark_hook,
11535 struct elf_reloc_cookie *cookie)
11536{
11537 unsigned long r_symndx;
11538 struct elf_link_hash_entry *h;
11539
11540 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 11541 if (r_symndx == STN_UNDEF)
5241d853
RS
11542 return NULL;
11543
11544 if (r_symndx >= cookie->locsymcount
11545 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11546 {
11547 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11548 while (h->root.type == bfd_link_hash_indirect
11549 || h->root.type == bfd_link_hash_warning)
11550 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11551 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11552 }
11553
11554 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11555 &cookie->locsyms[r_symndx]);
11556}
11557
11558/* COOKIE->rel describes a relocation against section SEC, which is
11559 a section we've decided to keep. Mark the section that contains
9d0a14d3 11560 the relocation symbol. */
5241d853
RS
11561
11562bfd_boolean
11563_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11564 asection *sec,
11565 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11566 struct elf_reloc_cookie *cookie)
5241d853
RS
11567{
11568 asection *rsec;
11569
11570 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11571 if (rsec && !rsec->gc_mark)
11572 {
11573 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11574 rsec->gc_mark = 1;
5241d853
RS
11575 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11576 return FALSE;
11577 }
11578 return TRUE;
11579}
11580
07adf181
AM
11581/* The mark phase of garbage collection. For a given section, mark
11582 it and any sections in this section's group, and all the sections
11583 which define symbols to which it refers. */
11584
ccfa59ea
AM
11585bfd_boolean
11586_bfd_elf_gc_mark (struct bfd_link_info *info,
11587 asection *sec,
6a5bb875 11588 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11589{
11590 bfd_boolean ret;
9d0a14d3 11591 asection *group_sec, *eh_frame;
c152c796
AM
11592
11593 sec->gc_mark = 1;
11594
11595 /* Mark all the sections in the group. */
11596 group_sec = elf_section_data (sec)->next_in_group;
11597 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11598 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11599 return FALSE;
11600
11601 /* Look through the section relocs. */
11602 ret = TRUE;
9d0a14d3
RS
11603 eh_frame = elf_eh_frame_section (sec->owner);
11604 if ((sec->flags & SEC_RELOC) != 0
11605 && sec->reloc_count > 0
11606 && sec != eh_frame)
c152c796 11607 {
5241d853 11608 struct elf_reloc_cookie cookie;
c152c796 11609
5241d853
RS
11610 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11611 ret = FALSE;
c152c796 11612 else
c152c796 11613 {
5241d853 11614 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11615 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11616 {
11617 ret = FALSE;
11618 break;
11619 }
11620 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11621 }
11622 }
9d0a14d3
RS
11623
11624 if (ret && eh_frame && elf_fde_list (sec))
11625 {
11626 struct elf_reloc_cookie cookie;
11627
11628 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11629 ret = FALSE;
11630 else
11631 {
11632 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11633 gc_mark_hook, &cookie))
11634 ret = FALSE;
11635 fini_reloc_cookie_for_section (&cookie, eh_frame);
11636 }
11637 }
11638
c152c796
AM
11639 return ret;
11640}
11641
7f6ab9f8
AM
11642/* Keep debug and special sections. */
11643
11644bfd_boolean
11645_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
11646 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
11647{
11648 bfd *ibfd;
11649
11650 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11651 {
11652 asection *isec;
11653 bfd_boolean some_kept;
11654
11655 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
11656 continue;
11657
11658 /* Ensure all linker created sections are kept, and see whether
11659 any other section is already marked. */
11660 some_kept = FALSE;
11661 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11662 {
11663 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11664 isec->gc_mark = 1;
11665 else if (isec->gc_mark)
11666 some_kept = TRUE;
11667 }
11668
11669 /* If no section in this file will be kept, then we can
11670 toss out debug sections. */
11671 if (!some_kept)
11672 continue;
11673
11674 /* Keep debug and special sections like .comment when they are
11675 not part of a group. */
11676 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11677 if (elf_next_in_group (isec) == NULL
11678 && ((isec->flags & SEC_DEBUGGING) != 0
11679 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
11680 isec->gc_mark = 1;
11681 }
11682 return TRUE;
11683}
11684
c152c796
AM
11685/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11686
c17d87de
NC
11687struct elf_gc_sweep_symbol_info
11688{
ccabcbe5
AM
11689 struct bfd_link_info *info;
11690 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11691 bfd_boolean);
11692};
11693
c152c796 11694static bfd_boolean
ccabcbe5 11695elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11696{
ccabcbe5
AM
11697 if ((h->root.type == bfd_link_hash_defined
11698 || h->root.type == bfd_link_hash_defweak)
11699 && !h->root.u.def.section->gc_mark
11700 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11701 {
a50b1753
NC
11702 struct elf_gc_sweep_symbol_info *inf =
11703 (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5
AM
11704 (*inf->hide_symbol) (inf->info, h, TRUE);
11705 }
c152c796
AM
11706
11707 return TRUE;
11708}
11709
11710/* The sweep phase of garbage collection. Remove all garbage sections. */
11711
11712typedef bfd_boolean (*gc_sweep_hook_fn)
11713 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11714
11715static bfd_boolean
ccabcbe5 11716elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11717{
11718 bfd *sub;
ccabcbe5
AM
11719 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11720 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11721 unsigned long section_sym_count;
11722 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11723
11724 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11725 {
11726 asection *o;
11727
11728 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11729 continue;
11730
11731 for (o = sub->sections; o != NULL; o = o->next)
11732 {
a33dafc3
L
11733 /* When any section in a section group is kept, we keep all
11734 sections in the section group. If the first member of
11735 the section group is excluded, we will also exclude the
11736 group section. */
11737 if (o->flags & SEC_GROUP)
11738 {
11739 asection *first = elf_next_in_group (o);
11740 o->gc_mark = first->gc_mark;
11741 }
c152c796
AM
11742
11743 if (o->gc_mark)
11744 continue;
11745
11746 /* Skip sweeping sections already excluded. */
11747 if (o->flags & SEC_EXCLUDE)
11748 continue;
11749
11750 /* Since this is early in the link process, it is simple
11751 to remove a section from the output. */
11752 o->flags |= SEC_EXCLUDE;
11753
c55fe096 11754 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11755 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11756
c152c796
AM
11757 /* But we also have to update some of the relocation
11758 info we collected before. */
11759 if (gc_sweep_hook
e8aaee2a
AM
11760 && (o->flags & SEC_RELOC) != 0
11761 && o->reloc_count > 0
11762 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11763 {
11764 Elf_Internal_Rela *internal_relocs;
11765 bfd_boolean r;
11766
11767 internal_relocs
11768 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11769 info->keep_memory);
11770 if (internal_relocs == NULL)
11771 return FALSE;
11772
11773 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11774
11775 if (elf_section_data (o)->relocs != internal_relocs)
11776 free (internal_relocs);
11777
11778 if (!r)
11779 return FALSE;
11780 }
11781 }
11782 }
11783
11784 /* Remove the symbols that were in the swept sections from the dynamic
11785 symbol table. GCFIXME: Anyone know how to get them out of the
11786 static symbol table as well? */
ccabcbe5
AM
11787 sweep_info.info = info;
11788 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11789 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11790 &sweep_info);
c152c796 11791
ccabcbe5 11792 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11793 return TRUE;
11794}
11795
11796/* Propagate collected vtable information. This is called through
11797 elf_link_hash_traverse. */
11798
11799static bfd_boolean
11800elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11801{
c152c796 11802 /* Those that are not vtables. */
f6e332e6 11803 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11804 return TRUE;
11805
11806 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11807 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11808 return TRUE;
11809
11810 /* If we've already been done, exit. */
f6e332e6 11811 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11812 return TRUE;
11813
11814 /* Make sure the parent's table is up to date. */
f6e332e6 11815 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11816
f6e332e6 11817 if (h->vtable->used == NULL)
c152c796
AM
11818 {
11819 /* None of this table's entries were referenced. Re-use the
11820 parent's table. */
f6e332e6
AM
11821 h->vtable->used = h->vtable->parent->vtable->used;
11822 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11823 }
11824 else
11825 {
11826 size_t n;
11827 bfd_boolean *cu, *pu;
11828
11829 /* Or the parent's entries into ours. */
f6e332e6 11830 cu = h->vtable->used;
c152c796 11831 cu[-1] = TRUE;
f6e332e6 11832 pu = h->vtable->parent->vtable->used;
c152c796
AM
11833 if (pu != NULL)
11834 {
11835 const struct elf_backend_data *bed;
11836 unsigned int log_file_align;
11837
11838 bed = get_elf_backend_data (h->root.u.def.section->owner);
11839 log_file_align = bed->s->log_file_align;
f6e332e6 11840 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11841 while (n--)
11842 {
11843 if (*pu)
11844 *cu = TRUE;
11845 pu++;
11846 cu++;
11847 }
11848 }
11849 }
11850
11851 return TRUE;
11852}
11853
11854static bfd_boolean
11855elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11856{
11857 asection *sec;
11858 bfd_vma hstart, hend;
11859 Elf_Internal_Rela *relstart, *relend, *rel;
11860 const struct elf_backend_data *bed;
11861 unsigned int log_file_align;
11862
c152c796
AM
11863 /* Take care of both those symbols that do not describe vtables as
11864 well as those that are not loaded. */
f6e332e6 11865 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11866 return TRUE;
11867
11868 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11869 || h->root.type == bfd_link_hash_defweak);
11870
11871 sec = h->root.u.def.section;
11872 hstart = h->root.u.def.value;
11873 hend = hstart + h->size;
11874
11875 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11876 if (!relstart)
11877 return *(bfd_boolean *) okp = FALSE;
11878 bed = get_elf_backend_data (sec->owner);
11879 log_file_align = bed->s->log_file_align;
11880
11881 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11882
11883 for (rel = relstart; rel < relend; ++rel)
11884 if (rel->r_offset >= hstart && rel->r_offset < hend)
11885 {
11886 /* If the entry is in use, do nothing. */
f6e332e6
AM
11887 if (h->vtable->used
11888 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11889 {
11890 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11891 if (h->vtable->used[entry])
c152c796
AM
11892 continue;
11893 }
11894 /* Otherwise, kill it. */
11895 rel->r_offset = rel->r_info = rel->r_addend = 0;
11896 }
11897
11898 return TRUE;
11899}
11900
87538722
AM
11901/* Mark sections containing dynamically referenced symbols. When
11902 building shared libraries, we must assume that any visible symbol is
11903 referenced. */
715df9b8 11904
64d03ab5
AM
11905bfd_boolean
11906bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 11907{
87538722
AM
11908 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11909
715df9b8
EB
11910 if ((h->root.type == bfd_link_hash_defined
11911 || h->root.type == bfd_link_hash_defweak)
87538722 11912 && (h->ref_dynamic
5adcfd8b 11913 || (!info->executable
87538722
AM
11914 && h->def_regular
11915 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11916 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
715df9b8
EB
11917 h->root.u.def.section->flags |= SEC_KEEP;
11918
11919 return TRUE;
11920}
3b36f7e6 11921
74f0fb50
AM
11922/* Keep all sections containing symbols undefined on the command-line,
11923 and the section containing the entry symbol. */
11924
11925void
11926_bfd_elf_gc_keep (struct bfd_link_info *info)
11927{
11928 struct bfd_sym_chain *sym;
11929
11930 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11931 {
11932 struct elf_link_hash_entry *h;
11933
11934 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11935 FALSE, FALSE, FALSE);
11936
11937 if (h != NULL
11938 && (h->root.type == bfd_link_hash_defined
11939 || h->root.type == bfd_link_hash_defweak)
11940 && !bfd_is_abs_section (h->root.u.def.section))
11941 h->root.u.def.section->flags |= SEC_KEEP;
11942 }
11943}
11944
c152c796
AM
11945/* Do mark and sweep of unused sections. */
11946
11947bfd_boolean
11948bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11949{
11950 bfd_boolean ok = TRUE;
11951 bfd *sub;
6a5bb875 11952 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 11953 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 11954
64d03ab5 11955 if (!bed->can_gc_sections
715df9b8 11956 || !is_elf_hash_table (info->hash))
c152c796
AM
11957 {
11958 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11959 return TRUE;
11960 }
11961
74f0fb50
AM
11962 bed->gc_keep (info);
11963
9d0a14d3
RS
11964 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11965 at the .eh_frame section if we can mark the FDEs individually. */
11966 _bfd_elf_begin_eh_frame_parsing (info);
11967 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11968 {
11969 asection *sec;
11970 struct elf_reloc_cookie cookie;
11971
11972 sec = bfd_get_section_by_name (sub, ".eh_frame");
11973 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11974 {
11975 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11976 if (elf_section_data (sec)->sec_info)
11977 elf_eh_frame_section (sub) = sec;
11978 fini_reloc_cookie_for_section (&cookie, sec);
11979 }
11980 }
11981 _bfd_elf_end_eh_frame_parsing (info);
11982
c152c796
AM
11983 /* Apply transitive closure to the vtable entry usage info. */
11984 elf_link_hash_traverse (elf_hash_table (info),
11985 elf_gc_propagate_vtable_entries_used,
11986 &ok);
11987 if (!ok)
11988 return FALSE;
11989
11990 /* Kill the vtable relocations that were not used. */
11991 elf_link_hash_traverse (elf_hash_table (info),
11992 elf_gc_smash_unused_vtentry_relocs,
11993 &ok);
11994 if (!ok)
11995 return FALSE;
11996
715df9b8
EB
11997 /* Mark dynamically referenced symbols. */
11998 if (elf_hash_table (info)->dynamic_sections_created)
11999 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 12000 bed->gc_mark_dynamic_ref,
87538722 12001 info);
c152c796 12002
715df9b8 12003 /* Grovel through relocs to find out who stays ... */
64d03ab5 12004 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
12005 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12006 {
12007 asection *o;
12008
12009 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12010 continue;
12011
7f6ab9f8
AM
12012 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12013 Also treat note sections as a root, if the section is not part
12014 of a group. */
c152c796 12015 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12016 if (!o->gc_mark
12017 && (o->flags & SEC_EXCLUDE) == 0
24007750 12018 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12019 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12020 && elf_next_in_group (o) == NULL )))
12021 {
12022 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12023 return FALSE;
12024 }
c152c796
AM
12025 }
12026
6a5bb875 12027 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12028 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12029
c152c796 12030 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12031 return elf_gc_sweep (abfd, info);
c152c796
AM
12032}
12033\f
12034/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12035
12036bfd_boolean
12037bfd_elf_gc_record_vtinherit (bfd *abfd,
12038 asection *sec,
12039 struct elf_link_hash_entry *h,
12040 bfd_vma offset)
12041{
12042 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12043 struct elf_link_hash_entry **search, *child;
12044 bfd_size_type extsymcount;
12045 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12046
12047 /* The sh_info field of the symtab header tells us where the
12048 external symbols start. We don't care about the local symbols at
12049 this point. */
12050 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12051 if (!elf_bad_symtab (abfd))
12052 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12053
12054 sym_hashes = elf_sym_hashes (abfd);
12055 sym_hashes_end = sym_hashes + extsymcount;
12056
12057 /* Hunt down the child symbol, which is in this section at the same
12058 offset as the relocation. */
12059 for (search = sym_hashes; search != sym_hashes_end; ++search)
12060 {
12061 if ((child = *search) != NULL
12062 && (child->root.type == bfd_link_hash_defined
12063 || child->root.type == bfd_link_hash_defweak)
12064 && child->root.u.def.section == sec
12065 && child->root.u.def.value == offset)
12066 goto win;
12067 }
12068
d003868e
AM
12069 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12070 abfd, sec, (unsigned long) offset);
c152c796
AM
12071 bfd_set_error (bfd_error_invalid_operation);
12072 return FALSE;
12073
12074 win:
f6e332e6
AM
12075 if (!child->vtable)
12076 {
a50b1753
NC
12077 child->vtable = (struct elf_link_virtual_table_entry *)
12078 bfd_zalloc (abfd, sizeof (*child->vtable));
f6e332e6
AM
12079 if (!child->vtable)
12080 return FALSE;
12081 }
c152c796
AM
12082 if (!h)
12083 {
12084 /* This *should* only be the absolute section. It could potentially
12085 be that someone has defined a non-global vtable though, which
12086 would be bad. It isn't worth paging in the local symbols to be
12087 sure though; that case should simply be handled by the assembler. */
12088
f6e332e6 12089 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12090 }
12091 else
f6e332e6 12092 child->vtable->parent = h;
c152c796
AM
12093
12094 return TRUE;
12095}
12096
12097/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12098
12099bfd_boolean
12100bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12101 asection *sec ATTRIBUTE_UNUSED,
12102 struct elf_link_hash_entry *h,
12103 bfd_vma addend)
12104{
12105 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12106 unsigned int log_file_align = bed->s->log_file_align;
12107
f6e332e6
AM
12108 if (!h->vtable)
12109 {
a50b1753
NC
12110 h->vtable = (struct elf_link_virtual_table_entry *)
12111 bfd_zalloc (abfd, sizeof (*h->vtable));
f6e332e6
AM
12112 if (!h->vtable)
12113 return FALSE;
12114 }
12115
12116 if (addend >= h->vtable->size)
c152c796
AM
12117 {
12118 size_t size, bytes, file_align;
f6e332e6 12119 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12120
12121 /* While the symbol is undefined, we have to be prepared to handle
12122 a zero size. */
12123 file_align = 1 << log_file_align;
12124 if (h->root.type == bfd_link_hash_undefined)
12125 size = addend + file_align;
12126 else
12127 {
12128 size = h->size;
12129 if (addend >= size)
12130 {
12131 /* Oops! We've got a reference past the defined end of
12132 the table. This is probably a bug -- shall we warn? */
12133 size = addend + file_align;
12134 }
12135 }
12136 size = (size + file_align - 1) & -file_align;
12137
12138 /* Allocate one extra entry for use as a "done" flag for the
12139 consolidation pass. */
12140 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12141
12142 if (ptr)
12143 {
a50b1753 12144 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12145
12146 if (ptr != NULL)
12147 {
12148 size_t oldbytes;
12149
f6e332e6 12150 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12151 * sizeof (bfd_boolean));
12152 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12153 }
12154 }
12155 else
a50b1753 12156 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12157
12158 if (ptr == NULL)
12159 return FALSE;
12160
12161 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12162 h->vtable->used = ptr + 1;
12163 h->vtable->size = size;
c152c796
AM
12164 }
12165
f6e332e6 12166 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12167
12168 return TRUE;
12169}
12170
ae17ab41
CM
12171/* Map an ELF section header flag to its corresponding string. */
12172typedef struct
12173{
12174 char *flag_name;
12175 flagword flag_value;
12176} elf_flags_to_name_table;
12177
12178static elf_flags_to_name_table elf_flags_to_names [] =
12179{
12180 { "SHF_WRITE", SHF_WRITE },
12181 { "SHF_ALLOC", SHF_ALLOC },
12182 { "SHF_EXECINSTR", SHF_EXECINSTR },
12183 { "SHF_MERGE", SHF_MERGE },
12184 { "SHF_STRINGS", SHF_STRINGS },
12185 { "SHF_INFO_LINK", SHF_INFO_LINK},
12186 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12187 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12188 { "SHF_GROUP", SHF_GROUP },
12189 { "SHF_TLS", SHF_TLS },
12190 { "SHF_MASKOS", SHF_MASKOS },
12191 { "SHF_EXCLUDE", SHF_EXCLUDE },
12192};
12193
12194void
12195bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12196 struct flag_info *finfo)
12197{
12198 bfd *output_bfd = info->output_bfd;
12199 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12200 struct flag_info_list *tf = finfo->flag_list;
12201 int with_hex = 0;
12202 int without_hex = 0;
12203
12204 for (tf = finfo->flag_list; tf != NULL; tf = tf->next)
12205 {
12206 int i;
12207 if (bed->elf_backend_lookup_section_flags_hook)
12208 {
12209 flagword hexval =
12210 (*bed->elf_backend_lookup_section_flags_hook) ((char *) tf->name);
12211
12212 if (hexval != 0)
12213 {
12214 if (tf->with == with_flags)
12215 with_hex |= hexval;
12216 else if (tf->with == without_flags)
12217 without_hex |= hexval;
12218 tf->valid = TRUE;
12219 continue;
12220 }
12221 }
12222 for (i = 0; i < 12; i++)
12223 {
12224 if (!strcmp (tf->name, elf_flags_to_names[i].flag_name))
12225 {
12226 if (tf->with == with_flags)
12227 with_hex |= elf_flags_to_names[i].flag_value;
12228 else if (tf->with == without_flags)
12229 without_hex |= elf_flags_to_names[i].flag_value;
12230 tf->valid = TRUE;
12231 continue;
12232 }
12233 }
12234 if (tf->valid == FALSE)
12235 {
12236 info->callbacks->einfo
12237 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12238 return;
12239 }
12240 }
12241 finfo->flags_initialized = TRUE;
12242 finfo->only_with_flags |= with_hex;
12243 finfo->not_with_flags |= without_hex;
12244
12245 return;
12246}
12247
c152c796
AM
12248struct alloc_got_off_arg {
12249 bfd_vma gotoff;
10455f89 12250 struct bfd_link_info *info;
c152c796
AM
12251};
12252
12253/* We need a special top-level link routine to convert got reference counts
12254 to real got offsets. */
12255
12256static bfd_boolean
12257elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12258{
a50b1753 12259 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
12260 bfd *obfd = gofarg->info->output_bfd;
12261 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 12262
c152c796
AM
12263 if (h->got.refcount > 0)
12264 {
12265 h->got.offset = gofarg->gotoff;
10455f89 12266 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
12267 }
12268 else
12269 h->got.offset = (bfd_vma) -1;
12270
12271 return TRUE;
12272}
12273
12274/* And an accompanying bit to work out final got entry offsets once
12275 we're done. Should be called from final_link. */
12276
12277bfd_boolean
12278bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12279 struct bfd_link_info *info)
12280{
12281 bfd *i;
12282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12283 bfd_vma gotoff;
c152c796
AM
12284 struct alloc_got_off_arg gofarg;
12285
10455f89
HPN
12286 BFD_ASSERT (abfd == info->output_bfd);
12287
c152c796
AM
12288 if (! is_elf_hash_table (info->hash))
12289 return FALSE;
12290
12291 /* The GOT offset is relative to the .got section, but the GOT header is
12292 put into the .got.plt section, if the backend uses it. */
12293 if (bed->want_got_plt)
12294 gotoff = 0;
12295 else
12296 gotoff = bed->got_header_size;
12297
12298 /* Do the local .got entries first. */
12299 for (i = info->input_bfds; i; i = i->link_next)
12300 {
12301 bfd_signed_vma *local_got;
12302 bfd_size_type j, locsymcount;
12303 Elf_Internal_Shdr *symtab_hdr;
12304
12305 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12306 continue;
12307
12308 local_got = elf_local_got_refcounts (i);
12309 if (!local_got)
12310 continue;
12311
12312 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12313 if (elf_bad_symtab (i))
12314 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12315 else
12316 locsymcount = symtab_hdr->sh_info;
12317
12318 for (j = 0; j < locsymcount; ++j)
12319 {
12320 if (local_got[j] > 0)
12321 {
12322 local_got[j] = gotoff;
10455f89 12323 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
12324 }
12325 else
12326 local_got[j] = (bfd_vma) -1;
12327 }
12328 }
12329
12330 /* Then the global .got entries. .plt refcounts are handled by
12331 adjust_dynamic_symbol */
12332 gofarg.gotoff = gotoff;
10455f89 12333 gofarg.info = info;
c152c796
AM
12334 elf_link_hash_traverse (elf_hash_table (info),
12335 elf_gc_allocate_got_offsets,
12336 &gofarg);
12337 return TRUE;
12338}
12339
12340/* Many folk need no more in the way of final link than this, once
12341 got entry reference counting is enabled. */
12342
12343bfd_boolean
12344bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12345{
12346 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12347 return FALSE;
12348
12349 /* Invoke the regular ELF backend linker to do all the work. */
12350 return bfd_elf_final_link (abfd, info);
12351}
12352
12353bfd_boolean
12354bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12355{
a50b1753 12356 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
12357
12358 if (rcookie->bad_symtab)
12359 rcookie->rel = rcookie->rels;
12360
12361 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12362 {
12363 unsigned long r_symndx;
12364
12365 if (! rcookie->bad_symtab)
12366 if (rcookie->rel->r_offset > offset)
12367 return FALSE;
12368 if (rcookie->rel->r_offset != offset)
12369 continue;
12370
12371 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 12372 if (r_symndx == STN_UNDEF)
c152c796
AM
12373 return TRUE;
12374
12375 if (r_symndx >= rcookie->locsymcount
12376 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12377 {
12378 struct elf_link_hash_entry *h;
12379
12380 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12381
12382 while (h->root.type == bfd_link_hash_indirect
12383 || h->root.type == bfd_link_hash_warning)
12384 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12385
12386 if ((h->root.type == bfd_link_hash_defined
12387 || h->root.type == bfd_link_hash_defweak)
12388 && elf_discarded_section (h->root.u.def.section))
12389 return TRUE;
12390 else
12391 return FALSE;
12392 }
12393 else
12394 {
12395 /* It's not a relocation against a global symbol,
12396 but it could be a relocation against a local
12397 symbol for a discarded section. */
12398 asection *isec;
12399 Elf_Internal_Sym *isym;
12400
12401 /* Need to: get the symbol; get the section. */
12402 isym = &rcookie->locsyms[r_symndx];
cb33740c
AM
12403 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12404 if (isec != NULL && elf_discarded_section (isec))
12405 return TRUE;
c152c796
AM
12406 }
12407 return FALSE;
12408 }
12409 return FALSE;
12410}
12411
12412/* Discard unneeded references to discarded sections.
12413 Returns TRUE if any section's size was changed. */
12414/* This function assumes that the relocations are in sorted order,
12415 which is true for all known assemblers. */
12416
12417bfd_boolean
12418bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12419{
12420 struct elf_reloc_cookie cookie;
12421 asection *stab, *eh;
c152c796
AM
12422 const struct elf_backend_data *bed;
12423 bfd *abfd;
c152c796
AM
12424 bfd_boolean ret = FALSE;
12425
12426 if (info->traditional_format
12427 || !is_elf_hash_table (info->hash))
12428 return FALSE;
12429
ca92cecb 12430 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
12431 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12432 {
12433 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12434 continue;
12435
12436 bed = get_elf_backend_data (abfd);
12437
12438 if ((abfd->flags & DYNAMIC) != 0)
12439 continue;
12440
8da3dbc5
AM
12441 eh = NULL;
12442 if (!info->relocatable)
12443 {
12444 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12445 if (eh != NULL
eea6121a 12446 && (eh->size == 0
8da3dbc5
AM
12447 || bfd_is_abs_section (eh->output_section)))
12448 eh = NULL;
12449 }
c152c796
AM
12450
12451 stab = bfd_get_section_by_name (abfd, ".stab");
12452 if (stab != NULL
eea6121a 12453 && (stab->size == 0
c152c796
AM
12454 || bfd_is_abs_section (stab->output_section)
12455 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12456 stab = NULL;
12457
12458 if (stab == NULL
12459 && eh == NULL
12460 && bed->elf_backend_discard_info == NULL)
12461 continue;
12462
5241d853
RS
12463 if (!init_reloc_cookie (&cookie, info, abfd))
12464 return FALSE;
c152c796 12465
5241d853
RS
12466 if (stab != NULL
12467 && stab->reloc_count > 0
12468 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 12469 {
5241d853
RS
12470 if (_bfd_discard_section_stabs (abfd, stab,
12471 elf_section_data (stab)->sec_info,
12472 bfd_elf_reloc_symbol_deleted_p,
12473 &cookie))
12474 ret = TRUE;
12475 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
12476 }
12477
5241d853
RS
12478 if (eh != NULL
12479 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 12480 {
ca92cecb 12481 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
12482 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12483 bfd_elf_reloc_symbol_deleted_p,
12484 &cookie))
12485 ret = TRUE;
5241d853 12486 fini_reloc_cookie_rels (&cookie, eh);
c152c796
AM
12487 }
12488
12489 if (bed->elf_backend_discard_info != NULL
12490 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12491 ret = TRUE;
12492
5241d853 12493 fini_reloc_cookie (&cookie, abfd);
c152c796 12494 }
ca92cecb 12495 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
12496
12497 if (info->eh_frame_hdr
12498 && !info->relocatable
12499 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12500 ret = TRUE;
12501
12502 return ret;
12503}
082b7297 12504
43e1669b 12505bfd_boolean
0c511000 12506_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 12507 asection *sec,
c0f00686 12508 struct bfd_link_info *info)
082b7297
L
12509{
12510 flagword flags;
c77ec726 12511 const char *name, *key;
082b7297
L
12512 struct bfd_section_already_linked *l;
12513 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 12514
c77ec726
AM
12515 if (sec->output_section == bfd_abs_section_ptr)
12516 return FALSE;
0c511000 12517
c77ec726 12518 flags = sec->flags;
0c511000 12519
c77ec726
AM
12520 /* Return if it isn't a linkonce section. A comdat group section
12521 also has SEC_LINK_ONCE set. */
12522 if ((flags & SEC_LINK_ONCE) == 0)
12523 return FALSE;
0c511000 12524
c77ec726
AM
12525 /* Don't put group member sections on our list of already linked
12526 sections. They are handled as a group via their group section. */
12527 if (elf_sec_group (sec) != NULL)
12528 return FALSE;
0c511000 12529
c77ec726
AM
12530 /* For a SHT_GROUP section, use the group signature as the key. */
12531 name = sec->name;
12532 if ((flags & SEC_GROUP) != 0
12533 && elf_next_in_group (sec) != NULL
12534 && elf_group_name (elf_next_in_group (sec)) != NULL)
12535 key = elf_group_name (elf_next_in_group (sec));
12536 else
12537 {
12538 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 12539 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
12540 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12541 key++;
0c511000 12542 else
c77ec726
AM
12543 /* Must be a user linkonce section that doesn't follow gcc's
12544 naming convention. In this case we won't be matching
12545 single member groups. */
12546 key = name;
0c511000 12547 }
6d2cd210 12548
c77ec726 12549 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
12550
12551 for (l = already_linked_list->entry; l != NULL; l = l->next)
12552 {
c2370991 12553 /* We may have 2 different types of sections on the list: group
c77ec726
AM
12554 sections with a signature of <key> (<key> is some string),
12555 and linkonce sections named .gnu.linkonce.<type>.<key>.
12556 Match like sections. LTO plugin sections are an exception.
12557 They are always named .gnu.linkonce.t.<key> and match either
12558 type of section. */
12559 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12560 && ((flags & SEC_GROUP) != 0
12561 || strcmp (name, l->sec->name) == 0))
12562 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
12563 {
12564 /* The section has already been linked. See if we should
6d2cd210 12565 issue a warning. */
c77ec726
AM
12566 if (!_bfd_handle_already_linked (sec, l, info))
12567 return FALSE;
082b7297 12568
c77ec726 12569 if (flags & SEC_GROUP)
3d7f7666 12570 {
c77ec726
AM
12571 asection *first = elf_next_in_group (sec);
12572 asection *s = first;
3d7f7666 12573
c77ec726 12574 while (s != NULL)
3d7f7666 12575 {
c77ec726
AM
12576 s->output_section = bfd_abs_section_ptr;
12577 /* Record which group discards it. */
12578 s->kept_section = l->sec;
12579 s = elf_next_in_group (s);
12580 /* These lists are circular. */
12581 if (s == first)
12582 break;
3d7f7666
L
12583 }
12584 }
082b7297 12585
43e1669b 12586 return TRUE;
082b7297
L
12587 }
12588 }
12589
c77ec726
AM
12590 /* A single member comdat group section may be discarded by a
12591 linkonce section and vice versa. */
12592 if ((flags & SEC_GROUP) != 0)
3d7f7666 12593 {
c77ec726 12594 asection *first = elf_next_in_group (sec);
c2370991 12595
c77ec726
AM
12596 if (first != NULL && elf_next_in_group (first) == first)
12597 /* Check this single member group against linkonce sections. */
12598 for (l = already_linked_list->entry; l != NULL; l = l->next)
12599 if ((l->sec->flags & SEC_GROUP) == 0
12600 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12601 {
12602 first->output_section = bfd_abs_section_ptr;
12603 first->kept_section = l->sec;
12604 sec->output_section = bfd_abs_section_ptr;
12605 break;
12606 }
12607 }
12608 else
12609 /* Check this linkonce section against single member groups. */
12610 for (l = already_linked_list->entry; l != NULL; l = l->next)
12611 if (l->sec->flags & SEC_GROUP)
6d2cd210 12612 {
c77ec726 12613 asection *first = elf_next_in_group (l->sec);
6d2cd210 12614
c77ec726
AM
12615 if (first != NULL
12616 && elf_next_in_group (first) == first
12617 && bfd_elf_match_symbols_in_sections (first, sec, info))
12618 {
12619 sec->output_section = bfd_abs_section_ptr;
12620 sec->kept_section = first;
12621 break;
12622 }
6d2cd210 12623 }
0c511000 12624
c77ec726
AM
12625 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12626 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12627 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12628 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12629 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12630 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12631 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12632 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12633 The reverse order cannot happen as there is never a bfd with only the
12634 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12635 matter as here were are looking only for cross-bfd sections. */
12636
12637 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12638 for (l = already_linked_list->entry; l != NULL; l = l->next)
12639 if ((l->sec->flags & SEC_GROUP) == 0
12640 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12641 {
12642 if (abfd != l->sec->owner)
12643 sec->output_section = bfd_abs_section_ptr;
12644 break;
12645 }
80c29487 12646
082b7297 12647 /* This is the first section with this name. Record it. */
c77ec726 12648 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 12649 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 12650 return sec->output_section == bfd_abs_section_ptr;
082b7297 12651}
81e1b023 12652
a4d8e49b
L
12653bfd_boolean
12654_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12655{
12656 return sym->st_shndx == SHN_COMMON;
12657}
12658
12659unsigned int
12660_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12661{
12662 return SHN_COMMON;
12663}
12664
12665asection *
12666_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12667{
12668 return bfd_com_section_ptr;
12669}
10455f89
HPN
12670
12671bfd_vma
12672_bfd_elf_default_got_elt_size (bfd *abfd,
12673 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12674 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12675 bfd *ibfd ATTRIBUTE_UNUSED,
12676 unsigned long symndx ATTRIBUTE_UNUSED)
12677{
12678 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12679 return bed->s->arch_size / 8;
12680}
83bac4b0
NC
12681
12682/* Routines to support the creation of dynamic relocs. */
12683
83bac4b0
NC
12684/* Returns the name of the dynamic reloc section associated with SEC. */
12685
12686static const char *
12687get_dynamic_reloc_section_name (bfd * abfd,
12688 asection * sec,
12689 bfd_boolean is_rela)
12690{
ddcf1fcf
BS
12691 char *name;
12692 const char *old_name = bfd_get_section_name (NULL, sec);
12693 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 12694
ddcf1fcf 12695 if (old_name == NULL)
83bac4b0
NC
12696 return NULL;
12697
ddcf1fcf
BS
12698 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12699 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
12700
12701 return name;
12702}
12703
12704/* Returns the dynamic reloc section associated with SEC.
12705 If necessary compute the name of the dynamic reloc section based
12706 on SEC's name (looked up in ABFD's string table) and the setting
12707 of IS_RELA. */
12708
12709asection *
12710_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12711 asection * sec,
12712 bfd_boolean is_rela)
12713{
12714 asection * reloc_sec = elf_section_data (sec)->sreloc;
12715
12716 if (reloc_sec == NULL)
12717 {
12718 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12719
12720 if (name != NULL)
12721 {
12722 reloc_sec = bfd_get_section_by_name (abfd, name);
12723
12724 if (reloc_sec != NULL)
12725 elf_section_data (sec)->sreloc = reloc_sec;
12726 }
12727 }
12728
12729 return reloc_sec;
12730}
12731
12732/* Returns the dynamic reloc section associated with SEC. If the
12733 section does not exist it is created and attached to the DYNOBJ
12734 bfd and stored in the SRELOC field of SEC's elf_section_data
12735 structure.
f8076f98 12736
83bac4b0
NC
12737 ALIGNMENT is the alignment for the newly created section and
12738 IS_RELA defines whether the name should be .rela.<SEC's name>
12739 or .rel.<SEC's name>. The section name is looked up in the
12740 string table associated with ABFD. */
12741
12742asection *
12743_bfd_elf_make_dynamic_reloc_section (asection * sec,
12744 bfd * dynobj,
12745 unsigned int alignment,
12746 bfd * abfd,
12747 bfd_boolean is_rela)
12748{
12749 asection * reloc_sec = elf_section_data (sec)->sreloc;
12750
12751 if (reloc_sec == NULL)
12752 {
12753 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12754
12755 if (name == NULL)
12756 return NULL;
12757
12758 reloc_sec = bfd_get_section_by_name (dynobj, name);
12759
12760 if (reloc_sec == NULL)
12761 {
12762 flagword flags;
12763
12764 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12765 if ((sec->flags & SEC_ALLOC) != 0)
12766 flags |= SEC_ALLOC | SEC_LOAD;
12767
12768 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12769 if (reloc_sec != NULL)
12770 {
12771 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12772 reloc_sec = NULL;
12773 }
12774 }
12775
12776 elf_section_data (sec)->sreloc = reloc_sec;
12777 }
12778
12779 return reloc_sec;
12780}
1338dd10
PB
12781
12782/* Copy the ELF symbol type associated with a linker hash entry. */
12783void
12784_bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12785 struct bfd_link_hash_entry * hdest,
12786 struct bfd_link_hash_entry * hsrc)
12787{
12788 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12789 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12790
12791 ehdest->type = ehsrc->type;
35fc36a8 12792 ehdest->target_internal = ehsrc->target_internal;
1338dd10 12793}
351f65ca
L
12794
12795/* Append a RELA relocation REL to section S in BFD. */
12796
12797void
12798elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12799{
12800 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12801 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12802 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12803 bed->s->swap_reloca_out (abfd, rel, loc);
12804}
12805
12806/* Append a REL relocation REL to section S in BFD. */
12807
12808void
12809elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12810{
12811 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12812 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12813 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12814 bed->s->swap_reloca_out (abfd, rel, loc);
12815}
This page took 2.416805 seconds and 4 git commands to generate.