PR gas/12754
[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. */
289 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
290 return FALSE;
291
292 elf_hash_table (info)->dynamic_sections_created = TRUE;
293
294 return TRUE;
295}
296
297/* Create dynamic sections when linking against a dynamic object. */
298
299bfd_boolean
268b6b39 300_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
301{
302 flagword flags, pltflags;
7325306f 303 struct elf_link_hash_entry *h;
45d6a902 304 asection *s;
9c5bfbb7 305 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 306 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 307
252b5132
RH
308 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
309 .rel[a].bss sections. */
e5a52504 310 flags = bed->dynamic_sec_flags;
252b5132
RH
311
312 pltflags = flags;
252b5132 313 if (bed->plt_not_loaded)
6df4d94c
MM
314 /* We do not clear SEC_ALLOC here because we still want the OS to
315 allocate space for the section; it's just that there's nothing
316 to read in from the object file. */
5d1634d7 317 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
318 else
319 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
320 if (bed->plt_readonly)
321 pltflags |= SEC_READONLY;
322
3496cb2a 323 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
252b5132 324 if (s == NULL
252b5132 325 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 326 return FALSE;
6de2ae4a 327 htab->splt = s;
252b5132 328
d98685ac
AM
329 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
330 .plt section. */
7325306f
RS
331 if (bed->want_plt_sym)
332 {
333 h = _bfd_elf_define_linkage_sym (abfd, info, s,
334 "_PROCEDURE_LINKAGE_TABLE_");
335 elf_hash_table (info)->hplt = h;
336 if (h == NULL)
337 return FALSE;
338 }
252b5132 339
3496cb2a 340 s = bfd_make_section_with_flags (abfd,
d35fd659 341 (bed->rela_plts_and_copies_p
3496cb2a
L
342 ? ".rela.plt" : ".rel.plt"),
343 flags | SEC_READONLY);
252b5132 344 if (s == NULL
45d6a902 345 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 346 return FALSE;
6de2ae4a 347 htab->srelplt = s;
252b5132
RH
348
349 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 350 return FALSE;
252b5132 351
3018b441
RH
352 if (bed->want_dynbss)
353 {
354 /* The .dynbss section is a place to put symbols which are defined
355 by dynamic objects, are referenced by regular objects, and are
356 not functions. We must allocate space for them in the process
357 image and use a R_*_COPY reloc to tell the dynamic linker to
358 initialize them at run time. The linker script puts the .dynbss
359 section into the .bss section of the final image. */
3496cb2a
L
360 s = bfd_make_section_with_flags (abfd, ".dynbss",
361 (SEC_ALLOC
362 | SEC_LINKER_CREATED));
363 if (s == NULL)
b34976b6 364 return FALSE;
252b5132 365
3018b441 366 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
367 normally needed. We need to create it here, though, so that the
368 linker will map it to an output section. We can't just create it
369 only if we need it, because we will not know whether we need it
370 until we have seen all the input files, and the first time the
371 main linker code calls BFD after examining all the input files
372 (size_dynamic_sections) the input sections have already been
373 mapped to the output sections. If the section turns out not to
374 be needed, we can discard it later. We will never need this
375 section when generating a shared object, since they do not use
376 copy relocs. */
3018b441
RH
377 if (! info->shared)
378 {
3496cb2a 379 s = bfd_make_section_with_flags (abfd,
d35fd659 380 (bed->rela_plts_and_copies_p
3496cb2a
L
381 ? ".rela.bss" : ".rel.bss"),
382 flags | SEC_READONLY);
3018b441 383 if (s == NULL
45d6a902 384 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 385 return FALSE;
3018b441 386 }
252b5132
RH
387 }
388
b34976b6 389 return TRUE;
252b5132
RH
390}
391\f
252b5132
RH
392/* Record a new dynamic symbol. We record the dynamic symbols as we
393 read the input files, since we need to have a list of all of them
394 before we can determine the final sizes of the output sections.
395 Note that we may actually call this function even though we are not
396 going to output any dynamic symbols; in some cases we know that a
397 symbol should be in the dynamic symbol table, but only if there is
398 one. */
399
b34976b6 400bfd_boolean
c152c796
AM
401bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
402 struct elf_link_hash_entry *h)
252b5132
RH
403{
404 if (h->dynindx == -1)
405 {
2b0f7ef9 406 struct elf_strtab_hash *dynstr;
68b6ddd0 407 char *p;
252b5132 408 const char *name;
252b5132
RH
409 bfd_size_type indx;
410
7a13edea
NC
411 /* XXX: The ABI draft says the linker must turn hidden and
412 internal symbols into STB_LOCAL symbols when producing the
413 DSO. However, if ld.so honors st_other in the dynamic table,
414 this would not be necessary. */
415 switch (ELF_ST_VISIBILITY (h->other))
416 {
417 case STV_INTERNAL:
418 case STV_HIDDEN:
9d6eee78
L
419 if (h->root.type != bfd_link_hash_undefined
420 && h->root.type != bfd_link_hash_undefweak)
38048eb9 421 {
f5385ebf 422 h->forced_local = 1;
67687978
PB
423 if (!elf_hash_table (info)->is_relocatable_executable)
424 return TRUE;
7a13edea 425 }
0444bdd4 426
7a13edea
NC
427 default:
428 break;
429 }
430
252b5132
RH
431 h->dynindx = elf_hash_table (info)->dynsymcount;
432 ++elf_hash_table (info)->dynsymcount;
433
434 dynstr = elf_hash_table (info)->dynstr;
435 if (dynstr == NULL)
436 {
437 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 438 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 439 if (dynstr == NULL)
b34976b6 440 return FALSE;
252b5132
RH
441 }
442
443 /* We don't put any version information in the dynamic string
aad5d350 444 table. */
252b5132
RH
445 name = h->root.root.string;
446 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
447 if (p != NULL)
448 /* We know that the p points into writable memory. In fact,
449 there are only a few symbols that have read-only names, being
450 those like _GLOBAL_OFFSET_TABLE_ that are created specially
451 by the backends. Most symbols will have names pointing into
452 an ELF string table read from a file, or to objalloc memory. */
453 *p = 0;
454
455 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
456
457 if (p != NULL)
458 *p = ELF_VER_CHR;
252b5132
RH
459
460 if (indx == (bfd_size_type) -1)
b34976b6 461 return FALSE;
252b5132
RH
462 h->dynstr_index = indx;
463 }
464
b34976b6 465 return TRUE;
252b5132 466}
45d6a902 467\f
55255dae
L
468/* Mark a symbol dynamic. */
469
28caa186 470static void
55255dae 471bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
472 struct elf_link_hash_entry *h,
473 Elf_Internal_Sym *sym)
55255dae 474{
40b36307 475 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 476
40b36307
L
477 /* It may be called more than once on the same H. */
478 if(h->dynamic || info->relocatable)
55255dae
L
479 return;
480
40b36307
L
481 if ((info->dynamic_data
482 && (h->type == STT_OBJECT
483 || (sym != NULL
484 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 485 || (d != NULL
40b36307
L
486 && h->root.type == bfd_link_hash_new
487 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
488 h->dynamic = 1;
489}
490
45d6a902
AM
491/* Record an assignment to a symbol made by a linker script. We need
492 this in case some dynamic object refers to this symbol. */
493
494bfd_boolean
fe21a8fc
L
495bfd_elf_record_link_assignment (bfd *output_bfd,
496 struct bfd_link_info *info,
268b6b39 497 const char *name,
fe21a8fc
L
498 bfd_boolean provide,
499 bfd_boolean hidden)
45d6a902 500{
00cbee0a 501 struct elf_link_hash_entry *h, *hv;
4ea42fb7 502 struct elf_link_hash_table *htab;
00cbee0a 503 const struct elf_backend_data *bed;
45d6a902 504
0eddce27 505 if (!is_elf_hash_table (info->hash))
45d6a902
AM
506 return TRUE;
507
4ea42fb7
AM
508 htab = elf_hash_table (info);
509 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 510 if (h == NULL)
4ea42fb7 511 return provide;
45d6a902 512
00cbee0a 513 switch (h->root.type)
77cfaee6 514 {
00cbee0a
L
515 case bfd_link_hash_defined:
516 case bfd_link_hash_defweak:
517 case bfd_link_hash_common:
518 break;
519 case bfd_link_hash_undefweak:
520 case bfd_link_hash_undefined:
521 /* Since we're defining the symbol, don't let it seem to have not
522 been defined. record_dynamic_symbol and size_dynamic_sections
523 may depend on this. */
4ea42fb7 524 h->root.type = bfd_link_hash_new;
77cfaee6
AM
525 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
526 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
527 break;
528 case bfd_link_hash_new:
40b36307 529 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 530 h->non_elf = 0;
00cbee0a
L
531 break;
532 case bfd_link_hash_indirect:
533 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 534 the versioned symbol point to this one. */
00cbee0a
L
535 bed = get_elf_backend_data (output_bfd);
536 hv = h;
537 while (hv->root.type == bfd_link_hash_indirect
538 || hv->root.type == bfd_link_hash_warning)
539 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
540 /* We don't need to update h->root.u since linker will set them
541 later. */
542 h->root.type = bfd_link_hash_undefined;
543 hv->root.type = bfd_link_hash_indirect;
544 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
546 break;
547 case bfd_link_hash_warning:
548 abort ();
549 break;
55255dae 550 }
45d6a902
AM
551
552 /* If this symbol is being provided by the linker script, and it is
553 currently defined by a dynamic object, but not by a regular
554 object, then mark it as undefined so that the generic linker will
555 force the correct value. */
556 if (provide
f5385ebf
AM
557 && h->def_dynamic
558 && !h->def_regular)
45d6a902
AM
559 h->root.type = bfd_link_hash_undefined;
560
561 /* If this symbol is not being provided by the linker script, and it is
562 currently defined by a dynamic object, but not by a regular object,
563 then clear out any version information because the symbol will not be
564 associated with the dynamic object any more. */
565 if (!provide
f5385ebf
AM
566 && h->def_dynamic
567 && !h->def_regular)
45d6a902
AM
568 h->verinfo.verdef = NULL;
569
f5385ebf 570 h->def_regular = 1;
45d6a902 571
fe21a8fc
L
572 if (provide && hidden)
573 {
91d6fa6a 574 bed = get_elf_backend_data (output_bfd);
fe21a8fc
L
575 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
576 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
577 }
578
6fa3860b
PB
579 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
580 and executables. */
581 if (!info->relocatable
582 && h->dynindx != -1
583 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
584 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
585 h->forced_local = 1;
586
f5385ebf
AM
587 if ((h->def_dynamic
588 || h->ref_dynamic
67687978
PB
589 || info->shared
590 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
591 && h->dynindx == -1)
592 {
c152c796 593 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
594 return FALSE;
595
596 /* If this is a weak defined symbol, and we know a corresponding
597 real symbol from the same dynamic object, make sure the real
598 symbol is also made into a dynamic symbol. */
f6e332e6
AM
599 if (h->u.weakdef != NULL
600 && h->u.weakdef->dynindx == -1)
45d6a902 601 {
f6e332e6 602 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
603 return FALSE;
604 }
605 }
606
607 return TRUE;
608}
42751cf3 609
8c58d23b
AM
610/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
611 success, and 2 on a failure caused by attempting to record a symbol
612 in a discarded section, eg. a discarded link-once section symbol. */
613
614int
c152c796
AM
615bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
616 bfd *input_bfd,
617 long input_indx)
8c58d23b
AM
618{
619 bfd_size_type amt;
620 struct elf_link_local_dynamic_entry *entry;
621 struct elf_link_hash_table *eht;
622 struct elf_strtab_hash *dynstr;
623 unsigned long dynstr_index;
624 char *name;
625 Elf_External_Sym_Shndx eshndx;
626 char esym[sizeof (Elf64_External_Sym)];
627
0eddce27 628 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
629 return 0;
630
631 /* See if the entry exists already. */
632 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
633 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
634 return 1;
635
636 amt = sizeof (*entry);
a50b1753 637 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
638 if (entry == NULL)
639 return 0;
640
641 /* Go find the symbol, so that we can find it's name. */
642 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 643 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
644 {
645 bfd_release (input_bfd, entry);
646 return 0;
647 }
648
649 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 650 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
651 {
652 asection *s;
653
654 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
655 if (s == NULL || bfd_is_abs_section (s->output_section))
656 {
657 /* We can still bfd_release here as nothing has done another
658 bfd_alloc. We can't do this later in this function. */
659 bfd_release (input_bfd, entry);
660 return 2;
661 }
662 }
663
664 name = (bfd_elf_string_from_elf_section
665 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
666 entry->isym.st_name));
667
668 dynstr = elf_hash_table (info)->dynstr;
669 if (dynstr == NULL)
670 {
671 /* Create a strtab to hold the dynamic symbol names. */
672 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
673 if (dynstr == NULL)
674 return 0;
675 }
676
b34976b6 677 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
678 if (dynstr_index == (unsigned long) -1)
679 return 0;
680 entry->isym.st_name = dynstr_index;
681
682 eht = elf_hash_table (info);
683
684 entry->next = eht->dynlocal;
685 eht->dynlocal = entry;
686 entry->input_bfd = input_bfd;
687 entry->input_indx = input_indx;
688 eht->dynsymcount++;
689
690 /* Whatever binding the symbol had before, it's now local. */
691 entry->isym.st_info
692 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
693
694 /* The dynindx will be set at the end of size_dynamic_sections. */
695
696 return 1;
697}
698
30b30c21 699/* Return the dynindex of a local dynamic symbol. */
42751cf3 700
30b30c21 701long
268b6b39
AM
702_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
703 bfd *input_bfd,
704 long input_indx)
30b30c21
RH
705{
706 struct elf_link_local_dynamic_entry *e;
707
708 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
709 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
710 return e->dynindx;
711 return -1;
712}
713
714/* This function is used to renumber the dynamic symbols, if some of
715 them are removed because they are marked as local. This is called
716 via elf_link_hash_traverse. */
717
b34976b6 718static bfd_boolean
268b6b39
AM
719elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
720 void *data)
42751cf3 721{
a50b1753 722 size_t *count = (size_t *) data;
30b30c21 723
e92d460e
AM
724 if (h->root.type == bfd_link_hash_warning)
725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
726
6fa3860b
PB
727 if (h->forced_local)
728 return TRUE;
729
730 if (h->dynindx != -1)
731 h->dynindx = ++(*count);
732
733 return TRUE;
734}
735
736
737/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
738 STB_LOCAL binding. */
739
740static bfd_boolean
741elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
742 void *data)
743{
a50b1753 744 size_t *count = (size_t *) data;
6fa3860b
PB
745
746 if (h->root.type == bfd_link_hash_warning)
747 h = (struct elf_link_hash_entry *) h->root.u.i.link;
748
749 if (!h->forced_local)
750 return TRUE;
751
42751cf3 752 if (h->dynindx != -1)
30b30c21
RH
753 h->dynindx = ++(*count);
754
b34976b6 755 return TRUE;
42751cf3 756}
30b30c21 757
aee6f5b4
AO
758/* Return true if the dynamic symbol for a given section should be
759 omitted when creating a shared library. */
760bfd_boolean
761_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
762 struct bfd_link_info *info,
763 asection *p)
764{
74541ad4
AM
765 struct elf_link_hash_table *htab;
766
aee6f5b4
AO
767 switch (elf_section_data (p)->this_hdr.sh_type)
768 {
769 case SHT_PROGBITS:
770 case SHT_NOBITS:
771 /* If sh_type is yet undecided, assume it could be
772 SHT_PROGBITS/SHT_NOBITS. */
773 case SHT_NULL:
74541ad4
AM
774 htab = elf_hash_table (info);
775 if (p == htab->tls_sec)
776 return FALSE;
777
778 if (htab->text_index_section != NULL)
779 return p != htab->text_index_section && p != htab->data_index_section;
780
aee6f5b4
AO
781 if (strcmp (p->name, ".got") == 0
782 || strcmp (p->name, ".got.plt") == 0
783 || strcmp (p->name, ".plt") == 0)
784 {
785 asection *ip;
aee6f5b4 786
74541ad4
AM
787 if (htab->dynobj != NULL
788 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
aee6f5b4
AO
789 && (ip->flags & SEC_LINKER_CREATED)
790 && ip->output_section == p)
791 return TRUE;
792 }
793 return FALSE;
794
795 /* There shouldn't be section relative relocations
796 against any other section. */
797 default:
798 return TRUE;
799 }
800}
801
062e2358 802/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
803 symbol for each output section, which come first. Next come symbols
804 which have been forced to local binding. Then all of the back-end
805 allocated local dynamic syms, followed by the rest of the global
806 symbols. */
30b30c21 807
554220db
AM
808static unsigned long
809_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
810 struct bfd_link_info *info,
811 unsigned long *section_sym_count)
30b30c21
RH
812{
813 unsigned long dynsymcount = 0;
814
67687978 815 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
30b30c21 816 {
aee6f5b4 817 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
818 asection *p;
819 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 820 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
821 && (p->flags & SEC_ALLOC) != 0
822 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
823 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
824 else
825 elf_section_data (p)->dynindx = 0;
30b30c21 826 }
554220db 827 *section_sym_count = dynsymcount;
30b30c21 828
6fa3860b
PB
829 elf_link_hash_traverse (elf_hash_table (info),
830 elf_link_renumber_local_hash_table_dynsyms,
831 &dynsymcount);
832
30b30c21
RH
833 if (elf_hash_table (info)->dynlocal)
834 {
835 struct elf_link_local_dynamic_entry *p;
836 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
837 p->dynindx = ++dynsymcount;
838 }
839
840 elf_link_hash_traverse (elf_hash_table (info),
841 elf_link_renumber_hash_table_dynsyms,
842 &dynsymcount);
843
844 /* There is an unused NULL entry at the head of the table which
845 we must account for in our count. Unless there weren't any
846 symbols, which means we'll have no table at all. */
847 if (dynsymcount != 0)
848 ++dynsymcount;
849
ccabcbe5
AM
850 elf_hash_table (info)->dynsymcount = dynsymcount;
851 return dynsymcount;
30b30c21 852}
252b5132 853
54ac0771
L
854/* Merge st_other field. */
855
856static void
857elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
858 Elf_Internal_Sym *isym, bfd_boolean definition,
859 bfd_boolean dynamic)
860{
861 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
862
863 /* If st_other has a processor-specific meaning, specific
864 code might be needed here. We never merge the visibility
865 attribute with the one from a dynamic object. */
866 if (bed->elf_backend_merge_symbol_attribute)
867 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
868 dynamic);
869
870 /* If this symbol has default visibility and the user has requested
871 we not re-export it, then mark it as hidden. */
872 if (definition
873 && !dynamic
874 && (abfd->no_export
875 || (abfd->my_archive && abfd->my_archive->no_export))
876 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
877 isym->st_other = (STV_HIDDEN
878 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
879
880 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
881 {
882 unsigned char hvis, symvis, other, nvis;
883
884 /* Only merge the visibility. Leave the remainder of the
885 st_other field to elf_backend_merge_symbol_attribute. */
886 other = h->other & ~ELF_ST_VISIBILITY (-1);
887
888 /* Combine visibilities, using the most constraining one. */
889 hvis = ELF_ST_VISIBILITY (h->other);
890 symvis = ELF_ST_VISIBILITY (isym->st_other);
891 if (! hvis)
892 nvis = symvis;
893 else if (! symvis)
894 nvis = hvis;
895 else
896 nvis = hvis < symvis ? hvis : symvis;
897
898 h->other = other | nvis;
899 }
900}
901
45d6a902
AM
902/* This function is called when we want to define a new symbol. It
903 handles the various cases which arise when we find a definition in
904 a dynamic object, or when there is already a definition in a
905 dynamic object. The new symbol is described by NAME, SYM, PSEC,
906 and PVALUE. We set SYM_HASH to the hash table entry. We set
907 OVERRIDE if the old symbol is overriding a new definition. We set
908 TYPE_CHANGE_OK if it is OK for the type to change. We set
909 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
910 change, we mean that we shouldn't warn if the type or size does
af44c138
L
911 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
912 object is overridden by a regular object. */
45d6a902
AM
913
914bfd_boolean
268b6b39
AM
915_bfd_elf_merge_symbol (bfd *abfd,
916 struct bfd_link_info *info,
917 const char *name,
918 Elf_Internal_Sym *sym,
919 asection **psec,
920 bfd_vma *pvalue,
af44c138 921 unsigned int *pold_alignment,
268b6b39
AM
922 struct elf_link_hash_entry **sym_hash,
923 bfd_boolean *skip,
924 bfd_boolean *override,
925 bfd_boolean *type_change_ok,
0f8a2703 926 bfd_boolean *size_change_ok)
252b5132 927{
7479dfd4 928 asection *sec, *oldsec;
45d6a902
AM
929 struct elf_link_hash_entry *h;
930 struct elf_link_hash_entry *flip;
931 int bind;
932 bfd *oldbfd;
933 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 934 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 935 const struct elf_backend_data *bed;
45d6a902
AM
936
937 *skip = FALSE;
938 *override = FALSE;
939
940 sec = *psec;
941 bind = ELF_ST_BIND (sym->st_info);
942
cd7be95b
KH
943 /* Silently discard TLS symbols from --just-syms. There's no way to
944 combine a static TLS block with a new TLS block for this executable. */
945 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
946 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
947 {
948 *skip = TRUE;
949 return TRUE;
950 }
951
45d6a902
AM
952 if (! bfd_is_und_section (sec))
953 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
954 else
955 h = ((struct elf_link_hash_entry *)
956 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
957 if (h == NULL)
958 return FALSE;
959 *sym_hash = h;
252b5132 960
88ba32a0
L
961 bed = get_elf_backend_data (abfd);
962
45d6a902
AM
963 /* This code is for coping with dynamic objects, and is only useful
964 if we are doing an ELF link. */
88ba32a0 965 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
45d6a902 966 return TRUE;
252b5132 967
45d6a902
AM
968 /* For merging, we only care about real symbols. */
969
970 while (h->root.type == bfd_link_hash_indirect
971 || h->root.type == bfd_link_hash_warning)
972 h = (struct elf_link_hash_entry *) h->root.u.i.link;
973
40b36307
L
974 /* We have to check it for every instance since the first few may be
975 refereences and not all compilers emit symbol type for undefined
976 symbols. */
977 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
978
45d6a902
AM
979 /* If we just created the symbol, mark it as being an ELF symbol.
980 Other than that, there is nothing to do--there is no merge issue
981 with a newly defined symbol--so we just return. */
982
983 if (h->root.type == bfd_link_hash_new)
252b5132 984 {
f5385ebf 985 h->non_elf = 0;
45d6a902
AM
986 return TRUE;
987 }
252b5132 988
7479dfd4
L
989 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
990 existing symbol. */
252b5132 991
45d6a902
AM
992 switch (h->root.type)
993 {
994 default:
995 oldbfd = NULL;
7479dfd4 996 oldsec = NULL;
45d6a902 997 break;
252b5132 998
45d6a902
AM
999 case bfd_link_hash_undefined:
1000 case bfd_link_hash_undefweak:
1001 oldbfd = h->root.u.undef.abfd;
7479dfd4 1002 oldsec = NULL;
45d6a902
AM
1003 break;
1004
1005 case bfd_link_hash_defined:
1006 case bfd_link_hash_defweak:
1007 oldbfd = h->root.u.def.section->owner;
7479dfd4 1008 oldsec = h->root.u.def.section;
45d6a902
AM
1009 break;
1010
1011 case bfd_link_hash_common:
1012 oldbfd = h->root.u.c.p->section->owner;
7479dfd4 1013 oldsec = h->root.u.c.p->section;
45d6a902
AM
1014 break;
1015 }
1016
895fa45f
MGD
1017 /* Differentiate strong and weak symbols. */
1018 newweak = bind == STB_WEAK;
1019 oldweak = (h->root.type == bfd_link_hash_defweak
1020 || h->root.type == bfd_link_hash_undefweak);
1021
45d6a902
AM
1022 /* In cases involving weak versioned symbols, we may wind up trying
1023 to merge a symbol with itself. Catch that here, to avoid the
1024 confusion that results if we try to override a symbol with
1025 itself. The additional tests catch cases like
1026 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1027 dynamic object, which we do want to handle here. */
1028 if (abfd == oldbfd
895fa45f 1029 && (newweak || oldweak)
45d6a902 1030 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1031 || !h->def_regular))
45d6a902
AM
1032 return TRUE;
1033
1034 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1035 respectively, is from a dynamic object. */
1036
707bba77 1037 newdyn = (abfd->flags & DYNAMIC) != 0;
45d6a902 1038
707bba77 1039 olddyn = FALSE;
45d6a902
AM
1040 if (oldbfd != NULL)
1041 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1042 else if (oldsec != NULL)
45d6a902 1043 {
707bba77 1044 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1045 indices used by MIPS ELF. */
707bba77 1046 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1047 }
252b5132 1048
45d6a902
AM
1049 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1050 respectively, appear to be a definition rather than reference. */
1051
707bba77 1052 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1053
707bba77
AM
1054 olddef = (h->root.type != bfd_link_hash_undefined
1055 && h->root.type != bfd_link_hash_undefweak
1056 && h->root.type != bfd_link_hash_common);
45d6a902 1057
0a36a439
L
1058 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1059 respectively, appear to be a function. */
1060
1061 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1062 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1063
1064 oldfunc = (h->type != STT_NOTYPE
1065 && bed->is_function_type (h->type));
1066
580a2b6e
L
1067 /* When we try to create a default indirect symbol from the dynamic
1068 definition with the default version, we skip it if its type and
1069 the type of existing regular definition mismatch. We only do it
1070 if the existing regular definition won't be dynamic. */
1071 if (pold_alignment == NULL
1072 && !info->shared
1073 && !info->export_dynamic
1074 && !h->ref_dynamic
1075 && newdyn
1076 && newdef
1077 && !olddyn
1078 && (olddef || h->root.type == bfd_link_hash_common)
1079 && ELF_ST_TYPE (sym->st_info) != h->type
1080 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
fcb93ecf 1081 && h->type != STT_NOTYPE
0a36a439 1082 && !(newfunc && oldfunc))
580a2b6e
L
1083 {
1084 *skip = TRUE;
1085 return TRUE;
1086 }
1087
68f49ba3
L
1088 /* Check TLS symbol. We don't check undefined symbol introduced by
1089 "ld -u". */
7479dfd4 1090 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
68f49ba3
L
1091 && ELF_ST_TYPE (sym->st_info) != h->type
1092 && oldbfd != NULL)
7479dfd4
L
1093 {
1094 bfd *ntbfd, *tbfd;
1095 bfd_boolean ntdef, tdef;
1096 asection *ntsec, *tsec;
1097
1098 if (h->type == STT_TLS)
1099 {
3b36f7e6 1100 ntbfd = abfd;
7479dfd4
L
1101 ntsec = sec;
1102 ntdef = newdef;
1103 tbfd = oldbfd;
1104 tsec = oldsec;
1105 tdef = olddef;
1106 }
1107 else
1108 {
1109 ntbfd = oldbfd;
1110 ntsec = oldsec;
1111 ntdef = olddef;
1112 tbfd = abfd;
1113 tsec = sec;
1114 tdef = newdef;
1115 }
1116
1117 if (tdef && ntdef)
1118 (*_bfd_error_handler)
fc3e1e3c 1119 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1120 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1121 else if (!tdef && !ntdef)
1122 (*_bfd_error_handler)
fc3e1e3c 1123 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
7479dfd4
L
1124 tbfd, ntbfd, h->root.root.string);
1125 else if (tdef)
1126 (*_bfd_error_handler)
fc3e1e3c 1127 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
7479dfd4
L
1128 tbfd, tsec, ntbfd, h->root.root.string);
1129 else
1130 (*_bfd_error_handler)
fc3e1e3c 1131 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1132 tbfd, ntbfd, ntsec, h->root.root.string);
1133
1134 bfd_set_error (bfd_error_bad_value);
1135 return FALSE;
1136 }
1137
4cc11e76 1138 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
1139 object or is weak in all dynamic objects. Internal and hidden
1140 visibility will make it unavailable to dynamic objects. */
f5385ebf 1141 if (newdyn && !h->dynamic_def)
45d6a902
AM
1142 {
1143 if (!bfd_is_und_section (sec))
f5385ebf 1144 h->dynamic_def = 1;
45d6a902 1145 else
252b5132 1146 {
45d6a902
AM
1147 /* Check if this symbol is weak in all dynamic objects. If it
1148 is the first time we see it in a dynamic object, we mark
1149 if it is weak. Otherwise, we clear it. */
f5385ebf 1150 if (!h->ref_dynamic)
79349b09 1151 {
45d6a902 1152 if (bind == STB_WEAK)
f5385ebf 1153 h->dynamic_weak = 1;
252b5132 1154 }
45d6a902 1155 else if (bind != STB_WEAK)
f5385ebf 1156 h->dynamic_weak = 0;
252b5132 1157 }
45d6a902 1158 }
252b5132 1159
45d6a902
AM
1160 /* If the old symbol has non-default visibility, we ignore the new
1161 definition from a dynamic object. */
1162 if (newdyn
9c7a29a3 1163 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1164 && !bfd_is_und_section (sec))
1165 {
1166 *skip = TRUE;
1167 /* Make sure this symbol is dynamic. */
f5385ebf 1168 h->ref_dynamic = 1;
45d6a902
AM
1169 /* A protected symbol has external availability. Make sure it is
1170 recorded as dynamic.
1171
1172 FIXME: Should we check type and size for protected symbol? */
1173 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1174 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1175 else
1176 return TRUE;
1177 }
1178 else if (!newdyn
9c7a29a3 1179 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1180 && h->def_dynamic)
45d6a902
AM
1181 {
1182 /* If the new symbol with non-default visibility comes from a
1183 relocatable file and the old definition comes from a dynamic
1184 object, we remove the old definition. */
1185 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1186 {
1187 /* Handle the case where the old dynamic definition is
1188 default versioned. We need to copy the symbol info from
1189 the symbol with default version to the normal one if it
1190 was referenced before. */
1191 if (h->ref_regular)
1192 {
d2dee3b2 1193 struct elf_link_hash_entry *vh = *sym_hash;
91d6fa6a 1194
d2dee3b2
L
1195 vh->root.type = h->root.type;
1196 h->root.type = bfd_link_hash_indirect;
1197 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1198 /* Protected symbols will override the dynamic definition
1199 with default version. */
1200 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1201 {
1202 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1203 vh->dynamic_def = 1;
1204 vh->ref_dynamic = 1;
1205 }
1206 else
1207 {
1208 h->root.type = vh->root.type;
1209 vh->ref_dynamic = 0;
1210 /* We have to hide it here since it was made dynamic
1211 global with extra bits when the symbol info was
1212 copied from the old dynamic definition. */
1213 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1214 }
1215 h = vh;
1216 }
1217 else
1218 h = *sym_hash;
1219 }
1de1a317 1220
f6e332e6 1221 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317
L
1222 && bfd_is_und_section (sec))
1223 {
1224 /* If the new symbol is undefined and the old symbol was
1225 also undefined before, we need to make sure
1226 _bfd_generic_link_add_one_symbol doesn't mess
f6e332e6 1227 up the linker hash table undefs list. Since the old
1de1a317
L
1228 definition came from a dynamic object, it is still on the
1229 undefs list. */
1230 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1231 h->root.u.undef.abfd = abfd;
1232 }
1233 else
1234 {
1235 h->root.type = bfd_link_hash_new;
1236 h->root.u.undef.abfd = NULL;
1237 }
1238
f5385ebf 1239 if (h->def_dynamic)
252b5132 1240 {
f5385ebf
AM
1241 h->def_dynamic = 0;
1242 h->ref_dynamic = 1;
1243 h->dynamic_def = 1;
45d6a902
AM
1244 }
1245 /* FIXME: Should we check type and size for protected symbol? */
1246 h->size = 0;
1247 h->type = 0;
1248 return TRUE;
1249 }
14a793b2 1250
3e7a7d11
NC
1251 if (bind == STB_GNU_UNIQUE)
1252 h->unique_global = 1;
1253
15b43f48
AM
1254 /* If a new weak symbol definition comes from a regular file and the
1255 old symbol comes from a dynamic library, we treat the new one as
1256 strong. Similarly, an old weak symbol definition from a regular
1257 file is treated as strong when the new symbol comes from a dynamic
1258 library. Further, an old weak symbol from a dynamic library is
1259 treated as strong if the new symbol is from a dynamic library.
1260 This reflects the way glibc's ld.so works.
1261
1262 Do this before setting *type_change_ok or *size_change_ok so that
1263 we warn properly when dynamic library symbols are overridden. */
1264
1265 if (newdef && !newdyn && olddyn)
0f8a2703 1266 newweak = FALSE;
15b43f48 1267 if (olddef && newdyn)
0f8a2703
AM
1268 oldweak = FALSE;
1269
d334575b 1270 /* Allow changes between different types of function symbol. */
0a36a439 1271 if (newfunc && oldfunc)
fcb93ecf
PB
1272 *type_change_ok = TRUE;
1273
79349b09
AM
1274 /* It's OK to change the type if either the existing symbol or the
1275 new symbol is weak. A type change is also OK if the old symbol
1276 is undefined and the new symbol is defined. */
252b5132 1277
79349b09
AM
1278 if (oldweak
1279 || newweak
1280 || (newdef
1281 && h->root.type == bfd_link_hash_undefined))
1282 *type_change_ok = TRUE;
1283
1284 /* It's OK to change the size if either the existing symbol or the
1285 new symbol is weak, or if the old symbol is undefined. */
1286
1287 if (*type_change_ok
1288 || h->root.type == bfd_link_hash_undefined)
1289 *size_change_ok = TRUE;
45d6a902 1290
45d6a902
AM
1291 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1292 symbol, respectively, appears to be a common symbol in a dynamic
1293 object. If a symbol appears in an uninitialized section, and is
1294 not weak, and is not a function, then it may be a common symbol
1295 which was resolved when the dynamic object was created. We want
1296 to treat such symbols specially, because they raise special
1297 considerations when setting the symbol size: if the symbol
1298 appears as a common symbol in a regular object, and the size in
1299 the regular object is larger, we must make sure that we use the
1300 larger size. This problematic case can always be avoided in C,
1301 but it must be handled correctly when using Fortran shared
1302 libraries.
1303
1304 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1305 likewise for OLDDYNCOMMON and OLDDEF.
1306
1307 Note that this test is just a heuristic, and that it is quite
1308 possible to have an uninitialized symbol in a shared object which
1309 is really a definition, rather than a common symbol. This could
1310 lead to some minor confusion when the symbol really is a common
1311 symbol in some regular object. However, I think it will be
1312 harmless. */
1313
1314 if (newdyn
1315 && newdef
79349b09 1316 && !newweak
45d6a902
AM
1317 && (sec->flags & SEC_ALLOC) != 0
1318 && (sec->flags & SEC_LOAD) == 0
1319 && sym->st_size > 0
0a36a439 1320 && !newfunc)
45d6a902
AM
1321 newdyncommon = TRUE;
1322 else
1323 newdyncommon = FALSE;
1324
1325 if (olddyn
1326 && olddef
1327 && h->root.type == bfd_link_hash_defined
f5385ebf 1328 && h->def_dynamic
45d6a902
AM
1329 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1330 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1331 && h->size > 0
0a36a439 1332 && !oldfunc)
45d6a902
AM
1333 olddyncommon = TRUE;
1334 else
1335 olddyncommon = FALSE;
1336
a4d8e49b
L
1337 /* We now know everything about the old and new symbols. We ask the
1338 backend to check if we can merge them. */
a4d8e49b
L
1339 if (bed->merge_symbol
1340 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1341 pold_alignment, skip, override,
1342 type_change_ok, size_change_ok,
1343 &newdyn, &newdef, &newdyncommon, &newweak,
1344 abfd, &sec,
1345 &olddyn, &olddef, &olddyncommon, &oldweak,
1346 oldbfd, &oldsec))
1347 return FALSE;
1348
45d6a902
AM
1349 /* If both the old and the new symbols look like common symbols in a
1350 dynamic object, set the size of the symbol to the larger of the
1351 two. */
1352
1353 if (olddyncommon
1354 && newdyncommon
1355 && sym->st_size != h->size)
1356 {
1357 /* Since we think we have two common symbols, issue a multiple
1358 common warning if desired. Note that we only warn if the
1359 size is different. If the size is the same, we simply let
1360 the old symbol override the new one as normally happens with
1361 symbols defined in dynamic objects. */
1362
1363 if (! ((*info->callbacks->multiple_common)
24f58f47 1364 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1365 return FALSE;
252b5132 1366
45d6a902
AM
1367 if (sym->st_size > h->size)
1368 h->size = sym->st_size;
252b5132 1369
45d6a902 1370 *size_change_ok = TRUE;
252b5132
RH
1371 }
1372
45d6a902
AM
1373 /* If we are looking at a dynamic object, and we have found a
1374 definition, we need to see if the symbol was already defined by
1375 some other object. If so, we want to use the existing
1376 definition, and we do not want to report a multiple symbol
1377 definition error; we do this by clobbering *PSEC to be
1378 bfd_und_section_ptr.
1379
1380 We treat a common symbol as a definition if the symbol in the
1381 shared library is a function, since common symbols always
1382 represent variables; this can cause confusion in principle, but
1383 any such confusion would seem to indicate an erroneous program or
1384 shared library. We also permit a common symbol in a regular
79349b09 1385 object to override a weak symbol in a shared object. */
45d6a902
AM
1386
1387 if (newdyn
1388 && newdef
77cfaee6 1389 && (olddef
45d6a902 1390 || (h->root.type == bfd_link_hash_common
0a36a439 1391 && (newweak || newfunc))))
45d6a902
AM
1392 {
1393 *override = TRUE;
1394 newdef = FALSE;
1395 newdyncommon = FALSE;
252b5132 1396
45d6a902
AM
1397 *psec = sec = bfd_und_section_ptr;
1398 *size_change_ok = TRUE;
252b5132 1399
45d6a902
AM
1400 /* If we get here when the old symbol is a common symbol, then
1401 we are explicitly letting it override a weak symbol or
1402 function in a dynamic object, and we don't want to warn about
1403 a type change. If the old symbol is a defined symbol, a type
1404 change warning may still be appropriate. */
252b5132 1405
45d6a902
AM
1406 if (h->root.type == bfd_link_hash_common)
1407 *type_change_ok = TRUE;
1408 }
1409
1410 /* Handle the special case of an old common symbol merging with a
1411 new symbol which looks like a common symbol in a shared object.
1412 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1413 common symbol, and let _bfd_generic_link_add_one_symbol do the
1414 right thing. */
45d6a902
AM
1415
1416 if (newdyncommon
1417 && h->root.type == bfd_link_hash_common)
1418 {
1419 *override = TRUE;
1420 newdef = FALSE;
1421 newdyncommon = FALSE;
1422 *pvalue = sym->st_size;
a4d8e49b 1423 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1424 *size_change_ok = TRUE;
1425 }
1426
c5e2cead 1427 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1428 if (newdef && olddef && newweak)
54ac0771 1429 {
35ed3f94
AM
1430 /* Don't skip new non-IR weak syms. */
1431 if (!((oldbfd->flags & BFD_PLUGIN) != 0
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 1813
55255dae
L
1814 /* Ignore this if we won't export it. */
1815 if (!eif->info->export_dynamic && !h->dynamic)
1816 return TRUE;
1817
45d6a902
AM
1818 /* Ignore indirect symbols. These are added by the versioning code. */
1819 if (h->root.type == bfd_link_hash_indirect)
1820 return TRUE;
1821
1822 if (h->root.type == bfd_link_hash_warning)
1823 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1824
1825 if (h->dynindx == -1
f5385ebf
AM
1826 && (h->def_regular
1827 || h->ref_regular))
45d6a902 1828 {
1e8fa21e 1829 bfd_boolean hide;
45d6a902 1830
1e8fa21e 1831 if (eif->verdefs == NULL
09e2aba4 1832 || (bfd_find_version_for_sym (eif->verdefs, h->root.root.string, &hide)
1e8fa21e 1833 && !hide))
45d6a902 1834 {
c152c796 1835 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
1836 {
1837 eif->failed = TRUE;
1838 return FALSE;
1839 }
1840 }
1841 }
1842
1843 return TRUE;
1844}
1845\f
1846/* Look through the symbols which are defined in other shared
1847 libraries and referenced here. Update the list of version
1848 dependencies. This will be put into the .gnu.version_r section.
1849 This function is called via elf_link_hash_traverse. */
1850
28caa186 1851static bfd_boolean
268b6b39
AM
1852_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1853 void *data)
45d6a902 1854{
a50b1753 1855 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1856 Elf_Internal_Verneed *t;
1857 Elf_Internal_Vernaux *a;
1858 bfd_size_type amt;
1859
1860 if (h->root.type == bfd_link_hash_warning)
1861 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1862
1863 /* We only care about symbols defined in shared objects with version
1864 information. */
f5385ebf
AM
1865 if (!h->def_dynamic
1866 || h->def_regular
45d6a902
AM
1867 || h->dynindx == -1
1868 || h->verinfo.verdef == NULL)
1869 return TRUE;
1870
1871 /* See if we already know about this version. */
28caa186
AM
1872 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1873 t != NULL;
1874 t = t->vn_nextref)
45d6a902
AM
1875 {
1876 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1877 continue;
1878
1879 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1880 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1881 return TRUE;
1882
1883 break;
1884 }
1885
1886 /* This is a new version. Add it to tree we are building. */
1887
1888 if (t == NULL)
1889 {
1890 amt = sizeof *t;
a50b1753 1891 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
1892 if (t == NULL)
1893 {
1894 rinfo->failed = TRUE;
1895 return FALSE;
1896 }
1897
1898 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
1899 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1900 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
1901 }
1902
1903 amt = sizeof *a;
a50b1753 1904 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
1905 if (a == NULL)
1906 {
1907 rinfo->failed = TRUE;
1908 return FALSE;
1909 }
45d6a902
AM
1910
1911 /* Note that we are copying a string pointer here, and testing it
1912 above. If bfd_elf_string_from_elf_section is ever changed to
1913 discard the string data when low in memory, this will have to be
1914 fixed. */
1915 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1916
1917 a->vna_flags = h->verinfo.verdef->vd_flags;
1918 a->vna_nextptr = t->vn_auxptr;
1919
1920 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1921 ++rinfo->vers;
1922
1923 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1924
1925 t->vn_auxptr = a;
1926
1927 return TRUE;
1928}
1929
1930/* Figure out appropriate versions for all the symbols. We may not
1931 have the version number script until we have read all of the input
1932 files, so until that point we don't know which symbols should be
1933 local. This function is called via elf_link_hash_traverse. */
1934
28caa186 1935static bfd_boolean
268b6b39 1936_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 1937{
28caa186 1938 struct elf_info_failed *sinfo;
45d6a902 1939 struct bfd_link_info *info;
9c5bfbb7 1940 const struct elf_backend_data *bed;
45d6a902
AM
1941 struct elf_info_failed eif;
1942 char *p;
1943 bfd_size_type amt;
1944
a50b1753 1945 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
1946 info = sinfo->info;
1947
1948 if (h->root.type == bfd_link_hash_warning)
1949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1950
1951 /* Fix the symbol flags. */
1952 eif.failed = FALSE;
1953 eif.info = info;
1954 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1955 {
1956 if (eif.failed)
1957 sinfo->failed = TRUE;
1958 return FALSE;
1959 }
1960
1961 /* We only need version numbers for symbols defined in regular
1962 objects. */
f5385ebf 1963 if (!h->def_regular)
45d6a902
AM
1964 return TRUE;
1965
28caa186 1966 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
1967 p = strchr (h->root.root.string, ELF_VER_CHR);
1968 if (p != NULL && h->verinfo.vertree == NULL)
1969 {
1970 struct bfd_elf_version_tree *t;
1971 bfd_boolean hidden;
1972
1973 hidden = TRUE;
1974
1975 /* There are two consecutive ELF_VER_CHR characters if this is
1976 not a hidden symbol. */
1977 ++p;
1978 if (*p == ELF_VER_CHR)
1979 {
1980 hidden = FALSE;
1981 ++p;
1982 }
1983
1984 /* If there is no version string, we can just return out. */
1985 if (*p == '\0')
1986 {
1987 if (hidden)
f5385ebf 1988 h->hidden = 1;
45d6a902
AM
1989 return TRUE;
1990 }
1991
1992 /* Look for the version. If we find it, it is no longer weak. */
1993 for (t = sinfo->verdefs; t != NULL; t = t->next)
1994 {
1995 if (strcmp (t->name, p) == 0)
1996 {
1997 size_t len;
1998 char *alc;
1999 struct bfd_elf_version_expr *d;
2000
2001 len = p - h->root.root.string;
a50b1753 2002 alc = (char *) bfd_malloc (len);
45d6a902 2003 if (alc == NULL)
14b1c01e
AM
2004 {
2005 sinfo->failed = TRUE;
2006 return FALSE;
2007 }
45d6a902
AM
2008 memcpy (alc, h->root.root.string, len - 1);
2009 alc[len - 1] = '\0';
2010 if (alc[len - 2] == ELF_VER_CHR)
2011 alc[len - 2] = '\0';
2012
2013 h->verinfo.vertree = t;
2014 t->used = TRUE;
2015 d = NULL;
2016
108ba305
JJ
2017 if (t->globals.list != NULL)
2018 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2019
2020 /* See if there is anything to force this symbol to
2021 local scope. */
108ba305 2022 if (d == NULL && t->locals.list != NULL)
45d6a902 2023 {
108ba305
JJ
2024 d = (*t->match) (&t->locals, NULL, alc);
2025 if (d != NULL
2026 && h->dynindx != -1
108ba305
JJ
2027 && ! info->export_dynamic)
2028 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2029 }
2030
2031 free (alc);
2032 break;
2033 }
2034 }
2035
2036 /* If we are building an application, we need to create a
2037 version node for this version. */
36af4a4e 2038 if (t == NULL && info->executable)
45d6a902
AM
2039 {
2040 struct bfd_elf_version_tree **pp;
2041 int version_index;
2042
2043 /* If we aren't going to export this symbol, we don't need
2044 to worry about it. */
2045 if (h->dynindx == -1)
2046 return TRUE;
2047
2048 amt = sizeof *t;
a50b1753 2049 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2050 if (t == NULL)
2051 {
2052 sinfo->failed = TRUE;
2053 return FALSE;
2054 }
2055
45d6a902 2056 t->name = p;
45d6a902
AM
2057 t->name_indx = (unsigned int) -1;
2058 t->used = TRUE;
2059
2060 version_index = 1;
2061 /* Don't count anonymous version tag. */
2062 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
2063 version_index = 0;
2064 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2065 ++version_index;
2066 t->vernum = version_index;
2067
2068 *pp = t;
2069
2070 h->verinfo.vertree = t;
2071 }
2072 else if (t == NULL)
2073 {
2074 /* We could not find the version for a symbol when
2075 generating a shared archive. Return an error. */
2076 (*_bfd_error_handler)
c55fe096 2077 (_("%B: version node not found for symbol %s"),
28caa186 2078 info->output_bfd, h->root.root.string);
45d6a902
AM
2079 bfd_set_error (bfd_error_bad_value);
2080 sinfo->failed = TRUE;
2081 return FALSE;
2082 }
2083
2084 if (hidden)
f5385ebf 2085 h->hidden = 1;
45d6a902
AM
2086 }
2087
2088 /* If we don't have a version for this symbol, see if we can find
2089 something. */
2090 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2091 {
1e8fa21e 2092 bfd_boolean hide;
ae5a3597 2093
09e2aba4 2094 h->verinfo.vertree = bfd_find_version_for_sym (sinfo->verdefs,
1e8fa21e
AM
2095 h->root.root.string, &hide);
2096 if (h->verinfo.vertree != NULL && hide)
2097 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2098 }
2099
2100 return TRUE;
2101}
2102\f
45d6a902
AM
2103/* Read and swap the relocs from the section indicated by SHDR. This
2104 may be either a REL or a RELA section. The relocations are
2105 translated into RELA relocations and stored in INTERNAL_RELOCS,
2106 which should have already been allocated to contain enough space.
2107 The EXTERNAL_RELOCS are a buffer where the external form of the
2108 relocations should be stored.
2109
2110 Returns FALSE if something goes wrong. */
2111
2112static bfd_boolean
268b6b39 2113elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2114 asection *sec,
268b6b39
AM
2115 Elf_Internal_Shdr *shdr,
2116 void *external_relocs,
2117 Elf_Internal_Rela *internal_relocs)
45d6a902 2118{
9c5bfbb7 2119 const struct elf_backend_data *bed;
268b6b39 2120 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2121 const bfd_byte *erela;
2122 const bfd_byte *erelaend;
2123 Elf_Internal_Rela *irela;
243ef1e0
L
2124 Elf_Internal_Shdr *symtab_hdr;
2125 size_t nsyms;
45d6a902 2126
45d6a902
AM
2127 /* Position ourselves at the start of the section. */
2128 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2129 return FALSE;
2130
2131 /* Read the relocations. */
2132 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2133 return FALSE;
2134
243ef1e0 2135 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2136 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2137
45d6a902
AM
2138 bed = get_elf_backend_data (abfd);
2139
2140 /* Convert the external relocations to the internal format. */
2141 if (shdr->sh_entsize == bed->s->sizeof_rel)
2142 swap_in = bed->s->swap_reloc_in;
2143 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2144 swap_in = bed->s->swap_reloca_in;
2145 else
2146 {
2147 bfd_set_error (bfd_error_wrong_format);
2148 return FALSE;
2149 }
2150
a50b1753 2151 erela = (const bfd_byte *) external_relocs;
51992aec 2152 erelaend = erela + shdr->sh_size;
45d6a902
AM
2153 irela = internal_relocs;
2154 while (erela < erelaend)
2155 {
243ef1e0
L
2156 bfd_vma r_symndx;
2157
45d6a902 2158 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2159 r_symndx = ELF32_R_SYM (irela->r_info);
2160 if (bed->s->arch_size == 64)
2161 r_symndx >>= 24;
ce98a316
NC
2162 if (nsyms > 0)
2163 {
2164 if ((size_t) r_symndx >= nsyms)
2165 {
2166 (*_bfd_error_handler)
2167 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2168 " for offset 0x%lx in section `%A'"),
2169 abfd, sec,
2170 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2171 bfd_set_error (bfd_error_bad_value);
2172 return FALSE;
2173 }
2174 }
cf35638d 2175 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2176 {
2177 (*_bfd_error_handler)
ce98a316
NC
2178 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2179 " when the object file has no symbol table"),
d003868e
AM
2180 abfd, sec,
2181 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2182 bfd_set_error (bfd_error_bad_value);
2183 return FALSE;
2184 }
45d6a902
AM
2185 irela += bed->s->int_rels_per_ext_rel;
2186 erela += shdr->sh_entsize;
2187 }
2188
2189 return TRUE;
2190}
2191
2192/* Read and swap the relocs for a section O. They may have been
2193 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2194 not NULL, they are used as buffers to read into. They are known to
2195 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2196 the return value is allocated using either malloc or bfd_alloc,
2197 according to the KEEP_MEMORY argument. If O has two relocation
2198 sections (both REL and RELA relocations), then the REL_HDR
2199 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2200 RELA_HDR relocations. */
45d6a902
AM
2201
2202Elf_Internal_Rela *
268b6b39
AM
2203_bfd_elf_link_read_relocs (bfd *abfd,
2204 asection *o,
2205 void *external_relocs,
2206 Elf_Internal_Rela *internal_relocs,
2207 bfd_boolean keep_memory)
45d6a902 2208{
268b6b39 2209 void *alloc1 = NULL;
45d6a902 2210 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2212 struct bfd_elf_section_data *esdo = elf_section_data (o);
2213 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2214
d4730f92
BS
2215 if (esdo->relocs != NULL)
2216 return esdo->relocs;
45d6a902
AM
2217
2218 if (o->reloc_count == 0)
2219 return NULL;
2220
45d6a902
AM
2221 if (internal_relocs == NULL)
2222 {
2223 bfd_size_type size;
2224
2225 size = o->reloc_count;
2226 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2227 if (keep_memory)
a50b1753 2228 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2229 else
a50b1753 2230 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2231 if (internal_relocs == NULL)
2232 goto error_return;
2233 }
2234
2235 if (external_relocs == NULL)
2236 {
d4730f92
BS
2237 bfd_size_type size = 0;
2238
2239 if (esdo->rel.hdr)
2240 size += esdo->rel.hdr->sh_size;
2241 if (esdo->rela.hdr)
2242 size += esdo->rela.hdr->sh_size;
45d6a902 2243
268b6b39 2244 alloc1 = bfd_malloc (size);
45d6a902
AM
2245 if (alloc1 == NULL)
2246 goto error_return;
2247 external_relocs = alloc1;
2248 }
2249
d4730f92
BS
2250 internal_rela_relocs = internal_relocs;
2251 if (esdo->rel.hdr)
2252 {
2253 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2254 external_relocs,
2255 internal_relocs))
2256 goto error_return;
2257 external_relocs = (((bfd_byte *) external_relocs)
2258 + esdo->rel.hdr->sh_size);
2259 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2260 * bed->s->int_rels_per_ext_rel);
2261 }
2262
2263 if (esdo->rela.hdr
2264 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2265 external_relocs,
2266 internal_rela_relocs)))
45d6a902
AM
2267 goto error_return;
2268
2269 /* Cache the results for next time, if we can. */
2270 if (keep_memory)
d4730f92 2271 esdo->relocs = internal_relocs;
45d6a902
AM
2272
2273 if (alloc1 != NULL)
2274 free (alloc1);
2275
2276 /* Don't free alloc2, since if it was allocated we are passing it
2277 back (under the name of internal_relocs). */
2278
2279 return internal_relocs;
2280
2281 error_return:
2282 if (alloc1 != NULL)
2283 free (alloc1);
2284 if (alloc2 != NULL)
4dd07732
AM
2285 {
2286 if (keep_memory)
2287 bfd_release (abfd, alloc2);
2288 else
2289 free (alloc2);
2290 }
45d6a902
AM
2291 return NULL;
2292}
2293
2294/* Compute the size of, and allocate space for, REL_HDR which is the
2295 section header for a section containing relocations for O. */
2296
28caa186 2297static bfd_boolean
268b6b39 2298_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2299 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2300{
d4730f92 2301 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2302
2303 /* That allows us to calculate the size of the section. */
d4730f92 2304 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2305
2306 /* The contents field must last into write_object_contents, so we
2307 allocate it with bfd_alloc rather than malloc. Also since we
2308 cannot be sure that the contents will actually be filled in,
2309 we zero the allocated space. */
a50b1753 2310 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2311 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2312 return FALSE;
2313
d4730f92 2314 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2315 {
2316 struct elf_link_hash_entry **p;
2317
a50b1753 2318 p = (struct elf_link_hash_entry **)
d4730f92 2319 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2320 if (p == NULL)
2321 return FALSE;
2322
d4730f92 2323 reldata->hashes = p;
45d6a902
AM
2324 }
2325
2326 return TRUE;
2327}
2328
2329/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2330 originated from the section given by INPUT_REL_HDR) to the
2331 OUTPUT_BFD. */
2332
2333bfd_boolean
268b6b39
AM
2334_bfd_elf_link_output_relocs (bfd *output_bfd,
2335 asection *input_section,
2336 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2337 Elf_Internal_Rela *internal_relocs,
2338 struct elf_link_hash_entry **rel_hash
2339 ATTRIBUTE_UNUSED)
45d6a902
AM
2340{
2341 Elf_Internal_Rela *irela;
2342 Elf_Internal_Rela *irelaend;
2343 bfd_byte *erel;
d4730f92 2344 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2345 asection *output_section;
9c5bfbb7 2346 const struct elf_backend_data *bed;
268b6b39 2347 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2348 struct bfd_elf_section_data *esdo;
45d6a902
AM
2349
2350 output_section = input_section->output_section;
45d6a902 2351
d4730f92
BS
2352 bed = get_elf_backend_data (output_bfd);
2353 esdo = elf_section_data (output_section);
2354 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2355 {
d4730f92
BS
2356 output_reldata = &esdo->rel;
2357 swap_out = bed->s->swap_reloc_out;
45d6a902 2358 }
d4730f92
BS
2359 else if (esdo->rela.hdr
2360 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2361 {
d4730f92
BS
2362 output_reldata = &esdo->rela;
2363 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2364 }
2365 else
2366 {
2367 (*_bfd_error_handler)
d003868e
AM
2368 (_("%B: relocation size mismatch in %B section %A"),
2369 output_bfd, input_section->owner, input_section);
297d8443 2370 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2371 return FALSE;
2372 }
2373
d4730f92
BS
2374 erel = output_reldata->hdr->contents;
2375 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2376 irela = internal_relocs;
2377 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2378 * bed->s->int_rels_per_ext_rel);
2379 while (irela < irelaend)
2380 {
2381 (*swap_out) (output_bfd, irela, erel);
2382 irela += bed->s->int_rels_per_ext_rel;
2383 erel += input_rel_hdr->sh_entsize;
2384 }
2385
2386 /* Bump the counter, so that we know where to add the next set of
2387 relocations. */
d4730f92 2388 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2389
2390 return TRUE;
2391}
2392\f
508c3946
L
2393/* Make weak undefined symbols in PIE dynamic. */
2394
2395bfd_boolean
2396_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2397 struct elf_link_hash_entry *h)
2398{
2399 if (info->pie
2400 && h->dynindx == -1
2401 && h->root.type == bfd_link_hash_undefweak)
2402 return bfd_elf_link_record_dynamic_symbol (info, h);
2403
2404 return TRUE;
2405}
2406
45d6a902
AM
2407/* Fix up the flags for a symbol. This handles various cases which
2408 can only be fixed after all the input files are seen. This is
2409 currently called by both adjust_dynamic_symbol and
2410 assign_sym_version, which is unnecessary but perhaps more robust in
2411 the face of future changes. */
2412
28caa186 2413static bfd_boolean
268b6b39
AM
2414_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2415 struct elf_info_failed *eif)
45d6a902 2416{
33774f08 2417 const struct elf_backend_data *bed;
508c3946 2418
45d6a902
AM
2419 /* If this symbol was mentioned in a non-ELF file, try to set
2420 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2421 permit a non-ELF file to correctly refer to a symbol defined in
2422 an ELF dynamic object. */
f5385ebf 2423 if (h->non_elf)
45d6a902
AM
2424 {
2425 while (h->root.type == bfd_link_hash_indirect)
2426 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2427
2428 if (h->root.type != bfd_link_hash_defined
2429 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2430 {
2431 h->ref_regular = 1;
2432 h->ref_regular_nonweak = 1;
2433 }
45d6a902
AM
2434 else
2435 {
2436 if (h->root.u.def.section->owner != NULL
2437 && (bfd_get_flavour (h->root.u.def.section->owner)
2438 == bfd_target_elf_flavour))
f5385ebf
AM
2439 {
2440 h->ref_regular = 1;
2441 h->ref_regular_nonweak = 1;
2442 }
45d6a902 2443 else
f5385ebf 2444 h->def_regular = 1;
45d6a902
AM
2445 }
2446
2447 if (h->dynindx == -1
f5385ebf
AM
2448 && (h->def_dynamic
2449 || h->ref_dynamic))
45d6a902 2450 {
c152c796 2451 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2452 {
2453 eif->failed = TRUE;
2454 return FALSE;
2455 }
2456 }
2457 }
2458 else
2459 {
f5385ebf 2460 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2461 was first seen in a non-ELF file. Fortunately, if the symbol
2462 was first seen in an ELF file, we're probably OK unless the
2463 symbol was defined in a non-ELF file. Catch that case here.
2464 FIXME: We're still in trouble if the symbol was first seen in
2465 a dynamic object, and then later in a non-ELF regular object. */
2466 if ((h->root.type == bfd_link_hash_defined
2467 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2468 && !h->def_regular
45d6a902
AM
2469 && (h->root.u.def.section->owner != NULL
2470 ? (bfd_get_flavour (h->root.u.def.section->owner)
2471 != bfd_target_elf_flavour)
2472 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2473 && !h->def_dynamic)))
2474 h->def_regular = 1;
45d6a902
AM
2475 }
2476
508c3946 2477 /* Backend specific symbol fixup. */
33774f08
AM
2478 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2479 if (bed->elf_backend_fixup_symbol
2480 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2481 return FALSE;
508c3946 2482
45d6a902
AM
2483 /* If this is a final link, and the symbol was defined as a common
2484 symbol in a regular object file, and there was no definition in
2485 any dynamic object, then the linker will have allocated space for
f5385ebf 2486 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2487 flag will not have been set. */
2488 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2489 && !h->def_regular
2490 && h->ref_regular
2491 && !h->def_dynamic
45d6a902 2492 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
f5385ebf 2493 h->def_regular = 1;
45d6a902
AM
2494
2495 /* If -Bsymbolic was used (which means to bind references to global
2496 symbols to the definition within the shared object), and this
2497 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2498 need a PLT entry. Likewise, if the symbol has non-default
2499 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2500 will force it local. */
f5385ebf 2501 if (h->needs_plt
45d6a902 2502 && eif->info->shared
0eddce27 2503 && is_elf_hash_table (eif->info->hash)
55255dae 2504 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2505 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2506 && h->def_regular)
45d6a902 2507 {
45d6a902
AM
2508 bfd_boolean force_local;
2509
45d6a902
AM
2510 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2511 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2512 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2513 }
2514
2515 /* If a weak undefined symbol has non-default visibility, we also
2516 hide it from the dynamic linker. */
9c7a29a3 2517 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2518 && h->root.type == bfd_link_hash_undefweak)
33774f08 2519 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2520
2521 /* If this is a weak defined symbol in a dynamic object, and we know
2522 the real definition in the dynamic object, copy interesting flags
2523 over to the real definition. */
f6e332e6 2524 if (h->u.weakdef != NULL)
45d6a902
AM
2525 {
2526 struct elf_link_hash_entry *weakdef;
2527
f6e332e6 2528 weakdef = h->u.weakdef;
45d6a902
AM
2529 if (h->root.type == bfd_link_hash_indirect)
2530 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2531
2532 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2533 || h->root.type == bfd_link_hash_defweak);
f5385ebf 2534 BFD_ASSERT (weakdef->def_dynamic);
45d6a902
AM
2535
2536 /* If the real definition is defined by a regular object file,
2537 don't do anything special. See the longer description in
2538 _bfd_elf_adjust_dynamic_symbol, below. */
f5385ebf 2539 if (weakdef->def_regular)
f6e332e6 2540 h->u.weakdef = NULL;
45d6a902 2541 else
a26587ba
RS
2542 {
2543 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2544 || weakdef->root.type == bfd_link_hash_defweak);
2545 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2546 }
45d6a902
AM
2547 }
2548
2549 return TRUE;
2550}
2551
2552/* Make the backend pick a good value for a dynamic symbol. This is
2553 called via elf_link_hash_traverse, and also calls itself
2554 recursively. */
2555
28caa186 2556static bfd_boolean
268b6b39 2557_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2558{
a50b1753 2559 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2560 bfd *dynobj;
9c5bfbb7 2561 const struct elf_backend_data *bed;
45d6a902 2562
0eddce27 2563 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2564 return FALSE;
2565
2566 if (h->root.type == bfd_link_hash_warning)
2567 {
a6aa5195
AM
2568 h->got = elf_hash_table (eif->info)->init_got_offset;
2569 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2570
2571 /* When warning symbols are created, they **replace** the "real"
2572 entry in the hash table, thus we never get to see the real
2573 symbol in a hash traversal. So look at it now. */
2574 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2575 }
2576
2577 /* Ignore indirect symbols. These are added by the versioning code. */
2578 if (h->root.type == bfd_link_hash_indirect)
2579 return TRUE;
2580
2581 /* Fix the symbol flags. */
2582 if (! _bfd_elf_fix_symbol_flags (h, eif))
2583 return FALSE;
2584
2585 /* If this symbol does not require a PLT entry, and it is not
2586 defined by a dynamic object, or is not referenced by a regular
2587 object, ignore it. We do have to handle a weak defined symbol,
2588 even if no regular object refers to it, if we decided to add it
2589 to the dynamic symbol table. FIXME: Do we normally need to worry
2590 about symbols which are defined by one dynamic object and
2591 referenced by another one? */
f5385ebf 2592 if (!h->needs_plt
91e21fb7 2593 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2594 && (h->def_regular
2595 || !h->def_dynamic
2596 || (!h->ref_regular
f6e332e6 2597 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2598 {
a6aa5195 2599 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2600 return TRUE;
2601 }
2602
2603 /* If we've already adjusted this symbol, don't do it again. This
2604 can happen via a recursive call. */
f5385ebf 2605 if (h->dynamic_adjusted)
45d6a902
AM
2606 return TRUE;
2607
2608 /* Don't look at this symbol again. Note that we must set this
2609 after checking the above conditions, because we may look at a
2610 symbol once, decide not to do anything, and then get called
2611 recursively later after REF_REGULAR is set below. */
f5385ebf 2612 h->dynamic_adjusted = 1;
45d6a902
AM
2613
2614 /* If this is a weak definition, and we know a real definition, and
2615 the real symbol is not itself defined by a regular object file,
2616 then get a good value for the real definition. We handle the
2617 real symbol first, for the convenience of the backend routine.
2618
2619 Note that there is a confusing case here. If the real definition
2620 is defined by a regular object file, we don't get the real symbol
2621 from the dynamic object, but we do get the weak symbol. If the
2622 processor backend uses a COPY reloc, then if some routine in the
2623 dynamic object changes the real symbol, we will not see that
2624 change in the corresponding weak symbol. This is the way other
2625 ELF linkers work as well, and seems to be a result of the shared
2626 library model.
2627
2628 I will clarify this issue. Most SVR4 shared libraries define the
2629 variable _timezone and define timezone as a weak synonym. The
2630 tzset call changes _timezone. If you write
2631 extern int timezone;
2632 int _timezone = 5;
2633 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2634 you might expect that, since timezone is a synonym for _timezone,
2635 the same number will print both times. However, if the processor
2636 backend uses a COPY reloc, then actually timezone will be copied
2637 into your process image, and, since you define _timezone
2638 yourself, _timezone will not. Thus timezone and _timezone will
2639 wind up at different memory locations. The tzset call will set
2640 _timezone, leaving timezone unchanged. */
2641
f6e332e6 2642 if (h->u.weakdef != NULL)
45d6a902
AM
2643 {
2644 /* If we get to this point, we know there is an implicit
2645 reference by a regular object file via the weak symbol H.
2646 FIXME: Is this really true? What if the traversal finds
f6e332e6
AM
2647 H->U.WEAKDEF before it finds H? */
2648 h->u.weakdef->ref_regular = 1;
45d6a902 2649
f6e332e6 2650 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2651 return FALSE;
2652 }
2653
2654 /* If a symbol has no type and no size and does not require a PLT
2655 entry, then we are probably about to do the wrong thing here: we
2656 are probably going to create a COPY reloc for an empty object.
2657 This case can arise when a shared object is built with assembly
2658 code, and the assembly code fails to set the symbol type. */
2659 if (h->size == 0
2660 && h->type == STT_NOTYPE
f5385ebf 2661 && !h->needs_plt)
45d6a902
AM
2662 (*_bfd_error_handler)
2663 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2664 h->root.root.string);
2665
2666 dynobj = elf_hash_table (eif->info)->dynobj;
2667 bed = get_elf_backend_data (dynobj);
e7c33416 2668
45d6a902
AM
2669 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2670 {
2671 eif->failed = TRUE;
2672 return FALSE;
2673 }
2674
2675 return TRUE;
2676}
2677
027297b7
L
2678/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2679 DYNBSS. */
2680
2681bfd_boolean
2682_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2683 asection *dynbss)
2684{
91ac5911 2685 unsigned int power_of_two;
027297b7
L
2686 bfd_vma mask;
2687 asection *sec = h->root.u.def.section;
2688
2689 /* The section aligment of definition is the maximum alignment
91ac5911
L
2690 requirement of symbols defined in the section. Since we don't
2691 know the symbol alignment requirement, we start with the
2692 maximum alignment and check low bits of the symbol address
2693 for the minimum alignment. */
2694 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2695 mask = ((bfd_vma) 1 << power_of_two) - 1;
2696 while ((h->root.u.def.value & mask) != 0)
2697 {
2698 mask >>= 1;
2699 --power_of_two;
2700 }
027297b7 2701
91ac5911
L
2702 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2703 dynbss))
027297b7
L
2704 {
2705 /* Adjust the section alignment if needed. */
2706 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2707 power_of_two))
027297b7
L
2708 return FALSE;
2709 }
2710
91ac5911 2711 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2712 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2713
2714 /* Define the symbol as being at this point in DYNBSS. */
2715 h->root.u.def.section = dynbss;
2716 h->root.u.def.value = dynbss->size;
2717
2718 /* Increment the size of DYNBSS to make room for the symbol. */
2719 dynbss->size += h->size;
2720
2721 return TRUE;
2722}
2723
45d6a902
AM
2724/* Adjust all external symbols pointing into SEC_MERGE sections
2725 to reflect the object merging within the sections. */
2726
28caa186 2727static bfd_boolean
268b6b39 2728_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2729{
2730 asection *sec;
2731
2732 if (h->root.type == bfd_link_hash_warning)
2733 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2734
2735 if ((h->root.type == bfd_link_hash_defined
2736 || h->root.type == bfd_link_hash_defweak)
2737 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2738 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2739 {
a50b1753 2740 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2741
2742 h->root.u.def.value =
2743 _bfd_merged_section_offset (output_bfd,
2744 &h->root.u.def.section,
2745 elf_section_data (sec)->sec_info,
753731ee 2746 h->root.u.def.value);
45d6a902
AM
2747 }
2748
2749 return TRUE;
2750}
986a241f
RH
2751
2752/* Returns false if the symbol referred to by H should be considered
2753 to resolve local to the current module, and true if it should be
2754 considered to bind dynamically. */
2755
2756bfd_boolean
268b6b39
AM
2757_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2758 struct bfd_link_info *info,
89a2ee5a 2759 bfd_boolean not_local_protected)
986a241f
RH
2760{
2761 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2762 const struct elf_backend_data *bed;
2763 struct elf_link_hash_table *hash_table;
986a241f
RH
2764
2765 if (h == NULL)
2766 return FALSE;
2767
2768 while (h->root.type == bfd_link_hash_indirect
2769 || h->root.type == bfd_link_hash_warning)
2770 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2771
2772 /* If it was forced local, then clearly it's not dynamic. */
2773 if (h->dynindx == -1)
2774 return FALSE;
f5385ebf 2775 if (h->forced_local)
986a241f
RH
2776 return FALSE;
2777
2778 /* Identify the cases where name binding rules say that a
2779 visible symbol resolves locally. */
55255dae 2780 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
986a241f
RH
2781
2782 switch (ELF_ST_VISIBILITY (h->other))
2783 {
2784 case STV_INTERNAL:
2785 case STV_HIDDEN:
2786 return FALSE;
2787
2788 case STV_PROTECTED:
fcb93ecf
PB
2789 hash_table = elf_hash_table (info);
2790 if (!is_elf_hash_table (hash_table))
2791 return FALSE;
2792
2793 bed = get_elf_backend_data (hash_table->dynobj);
2794
986a241f
RH
2795 /* Proper resolution for function pointer equality may require
2796 that these symbols perhaps be resolved dynamically, even though
2797 we should be resolving them to the current module. */
89a2ee5a 2798 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2799 binding_stays_local_p = TRUE;
2800 break;
2801
2802 default:
986a241f
RH
2803 break;
2804 }
2805
aa37626c 2806 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2807 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2808 return TRUE;
2809
986a241f
RH
2810 /* Otherwise, the symbol is dynamic if binding rules don't tell
2811 us that it remains local. */
2812 return !binding_stays_local_p;
2813}
f6c52c13
AM
2814
2815/* Return true if the symbol referred to by H should be considered
2816 to resolve local to the current module, and false otherwise. Differs
2817 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2818 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2819 for the place where forced_local and dynindx == -1 are tested. If
2820 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2821 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2822 the symbol is local only for defined symbols.
2823 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2824 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2825 treatment of undefined weak symbols. For those that do not make
2826 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2827
2828bfd_boolean
268b6b39
AM
2829_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2830 struct bfd_link_info *info,
2831 bfd_boolean local_protected)
f6c52c13 2832{
fcb93ecf
PB
2833 const struct elf_backend_data *bed;
2834 struct elf_link_hash_table *hash_table;
2835
f6c52c13
AM
2836 /* If it's a local sym, of course we resolve locally. */
2837 if (h == NULL)
2838 return TRUE;
2839
d95edcac
L
2840 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2841 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2842 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2843 return TRUE;
2844
7e2294f9
AO
2845 /* Common symbols that become definitions don't get the DEF_REGULAR
2846 flag set, so test it first, and don't bail out. */
2847 if (ELF_COMMON_DEF_P (h))
2848 /* Do nothing. */;
f6c52c13 2849 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2850 resolve locally. The sym is either undefined or dynamic. */
2851 else if (!h->def_regular)
f6c52c13
AM
2852 return FALSE;
2853
2854 /* Forced local symbols resolve locally. */
f5385ebf 2855 if (h->forced_local)
f6c52c13
AM
2856 return TRUE;
2857
2858 /* As do non-dynamic symbols. */
2859 if (h->dynindx == -1)
2860 return TRUE;
2861
2862 /* At this point, we know the symbol is defined and dynamic. In an
2863 executable it must resolve locally, likewise when building symbolic
2864 shared libraries. */
55255dae 2865 if (info->executable || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2866 return TRUE;
2867
2868 /* Now deal with defined dynamic symbols in shared libraries. Ones
2869 with default visibility might not resolve locally. */
2870 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2871 return FALSE;
2872
fcb93ecf
PB
2873 hash_table = elf_hash_table (info);
2874 if (!is_elf_hash_table (hash_table))
2875 return TRUE;
2876
2877 bed = get_elf_backend_data (hash_table->dynobj);
2878
1c16dfa5 2879 /* STV_PROTECTED non-function symbols are local. */
fcb93ecf 2880 if (!bed->is_function_type (h->type))
1c16dfa5
L
2881 return TRUE;
2882
f6c52c13
AM
2883 /* Function pointer equality tests may require that STV_PROTECTED
2884 symbols be treated as dynamic symbols, even when we know that the
2885 dynamic linker will resolve them locally. */
2886 return local_protected;
2887}
e1918d23
AM
2888
2889/* Caches some TLS segment info, and ensures that the TLS segment vma is
2890 aligned. Returns the first TLS output section. */
2891
2892struct bfd_section *
2893_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2894{
2895 struct bfd_section *sec, *tls;
2896 unsigned int align = 0;
2897
2898 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2899 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2900 break;
2901 tls = sec;
2902
2903 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2904 if (sec->alignment_power > align)
2905 align = sec->alignment_power;
2906
2907 elf_hash_table (info)->tls_sec = tls;
2908
2909 /* Ensure the alignment of the first section is the largest alignment,
2910 so that the tls segment starts aligned. */
2911 if (tls != NULL)
2912 tls->alignment_power = align;
2913
2914 return tls;
2915}
0ad989f9
L
2916
2917/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2918static bfd_boolean
2919is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2920 Elf_Internal_Sym *sym)
2921{
a4d8e49b
L
2922 const struct elf_backend_data *bed;
2923
0ad989f9
L
2924 /* Local symbols do not count, but target specific ones might. */
2925 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2926 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2927 return FALSE;
2928
fcb93ecf 2929 bed = get_elf_backend_data (abfd);
0ad989f9 2930 /* Function symbols do not count. */
fcb93ecf 2931 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
2932 return FALSE;
2933
2934 /* If the section is undefined, then so is the symbol. */
2935 if (sym->st_shndx == SHN_UNDEF)
2936 return FALSE;
2937
2938 /* If the symbol is defined in the common section, then
2939 it is a common definition and so does not count. */
a4d8e49b 2940 if (bed->common_definition (sym))
0ad989f9
L
2941 return FALSE;
2942
2943 /* If the symbol is in a target specific section then we
2944 must rely upon the backend to tell us what it is. */
2945 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2946 /* FIXME - this function is not coded yet:
2947
2948 return _bfd_is_global_symbol_definition (abfd, sym);
2949
2950 Instead for now assume that the definition is not global,
2951 Even if this is wrong, at least the linker will behave
2952 in the same way that it used to do. */
2953 return FALSE;
2954
2955 return TRUE;
2956}
2957
2958/* Search the symbol table of the archive element of the archive ABFD
2959 whose archive map contains a mention of SYMDEF, and determine if
2960 the symbol is defined in this element. */
2961static bfd_boolean
2962elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2963{
2964 Elf_Internal_Shdr * hdr;
2965 bfd_size_type symcount;
2966 bfd_size_type extsymcount;
2967 bfd_size_type extsymoff;
2968 Elf_Internal_Sym *isymbuf;
2969 Elf_Internal_Sym *isym;
2970 Elf_Internal_Sym *isymend;
2971 bfd_boolean result;
2972
2973 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2974 if (abfd == NULL)
2975 return FALSE;
2976
2977 if (! bfd_check_format (abfd, bfd_object))
2978 return FALSE;
2979
2980 /* If we have already included the element containing this symbol in the
2981 link then we do not need to include it again. Just claim that any symbol
2982 it contains is not a definition, so that our caller will not decide to
2983 (re)include this element. */
2984 if (abfd->archive_pass)
2985 return FALSE;
2986
2987 /* Select the appropriate symbol table. */
2988 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2989 hdr = &elf_tdata (abfd)->symtab_hdr;
2990 else
2991 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2992
2993 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2994
2995 /* The sh_info field of the symtab header tells us where the
2996 external symbols start. We don't care about the local symbols. */
2997 if (elf_bad_symtab (abfd))
2998 {
2999 extsymcount = symcount;
3000 extsymoff = 0;
3001 }
3002 else
3003 {
3004 extsymcount = symcount - hdr->sh_info;
3005 extsymoff = hdr->sh_info;
3006 }
3007
3008 if (extsymcount == 0)
3009 return FALSE;
3010
3011 /* Read in the symbol table. */
3012 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3013 NULL, NULL, NULL);
3014 if (isymbuf == NULL)
3015 return FALSE;
3016
3017 /* Scan the symbol table looking for SYMDEF. */
3018 result = FALSE;
3019 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3020 {
3021 const char *name;
3022
3023 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3024 isym->st_name);
3025 if (name == NULL)
3026 break;
3027
3028 if (strcmp (name, symdef->name) == 0)
3029 {
3030 result = is_global_data_symbol_definition (abfd, isym);
3031 break;
3032 }
3033 }
3034
3035 free (isymbuf);
3036
3037 return result;
3038}
3039\f
5a580b3a
AM
3040/* Add an entry to the .dynamic table. */
3041
3042bfd_boolean
3043_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3044 bfd_vma tag,
3045 bfd_vma val)
3046{
3047 struct elf_link_hash_table *hash_table;
3048 const struct elf_backend_data *bed;
3049 asection *s;
3050 bfd_size_type newsize;
3051 bfd_byte *newcontents;
3052 Elf_Internal_Dyn dyn;
3053
3054 hash_table = elf_hash_table (info);
3055 if (! is_elf_hash_table (hash_table))
3056 return FALSE;
3057
3058 bed = get_elf_backend_data (hash_table->dynobj);
3059 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3060 BFD_ASSERT (s != NULL);
3061
eea6121a 3062 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3063 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3064 if (newcontents == NULL)
3065 return FALSE;
3066
3067 dyn.d_tag = tag;
3068 dyn.d_un.d_val = val;
eea6121a 3069 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3070
eea6121a 3071 s->size = newsize;
5a580b3a
AM
3072 s->contents = newcontents;
3073
3074 return TRUE;
3075}
3076
3077/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3078 otherwise just check whether one already exists. Returns -1 on error,
3079 1 if a DT_NEEDED tag already exists, and 0 on success. */
3080
4ad4eba5 3081static int
7e9f0867
AM
3082elf_add_dt_needed_tag (bfd *abfd,
3083 struct bfd_link_info *info,
4ad4eba5
AM
3084 const char *soname,
3085 bfd_boolean do_it)
5a580b3a
AM
3086{
3087 struct elf_link_hash_table *hash_table;
3088 bfd_size_type oldsize;
3089 bfd_size_type strindex;
3090
7e9f0867
AM
3091 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3092 return -1;
3093
5a580b3a
AM
3094 hash_table = elf_hash_table (info);
3095 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3096 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3097 if (strindex == (bfd_size_type) -1)
3098 return -1;
3099
3100 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3101 {
3102 asection *sdyn;
3103 const struct elf_backend_data *bed;
3104 bfd_byte *extdyn;
3105
3106 bed = get_elf_backend_data (hash_table->dynobj);
3107 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3108 if (sdyn != NULL)
3109 for (extdyn = sdyn->contents;
3110 extdyn < sdyn->contents + sdyn->size;
3111 extdyn += bed->s->sizeof_dyn)
3112 {
3113 Elf_Internal_Dyn dyn;
5a580b3a 3114
7e9f0867
AM
3115 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3116 if (dyn.d_tag == DT_NEEDED
3117 && dyn.d_un.d_val == strindex)
3118 {
3119 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3120 return 1;
3121 }
3122 }
5a580b3a
AM
3123 }
3124
3125 if (do_it)
3126 {
7e9f0867
AM
3127 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3128 return -1;
3129
5a580b3a
AM
3130 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3131 return -1;
3132 }
3133 else
3134 /* We were just checking for existence of the tag. */
3135 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3136
3137 return 0;
3138}
3139
010e5ae2
AM
3140static bfd_boolean
3141on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3142{
3143 for (; needed != NULL; needed = needed->next)
3144 if (strcmp (soname, needed->name) == 0)
3145 return TRUE;
3146
3147 return FALSE;
3148}
3149
5a580b3a 3150/* Sort symbol by value and section. */
4ad4eba5
AM
3151static int
3152elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3153{
3154 const struct elf_link_hash_entry *h1;
3155 const struct elf_link_hash_entry *h2;
10b7e05b 3156 bfd_signed_vma vdiff;
5a580b3a
AM
3157
3158 h1 = *(const struct elf_link_hash_entry **) arg1;
3159 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3160 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3161 if (vdiff != 0)
3162 return vdiff > 0 ? 1 : -1;
3163 else
3164 {
3165 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3166 if (sdiff != 0)
3167 return sdiff > 0 ? 1 : -1;
3168 }
5a580b3a
AM
3169 return 0;
3170}
4ad4eba5 3171
5a580b3a
AM
3172/* This function is used to adjust offsets into .dynstr for
3173 dynamic symbols. This is called via elf_link_hash_traverse. */
3174
3175static bfd_boolean
3176elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3177{
a50b1753 3178 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a
AM
3179
3180 if (h->root.type == bfd_link_hash_warning)
3181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3182
3183 if (h->dynindx != -1)
3184 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3185 return TRUE;
3186}
3187
3188/* Assign string offsets in .dynstr, update all structures referencing
3189 them. */
3190
4ad4eba5
AM
3191static bfd_boolean
3192elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3193{
3194 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3195 struct elf_link_local_dynamic_entry *entry;
3196 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3197 bfd *dynobj = hash_table->dynobj;
3198 asection *sdyn;
3199 bfd_size_type size;
3200 const struct elf_backend_data *bed;
3201 bfd_byte *extdyn;
3202
3203 _bfd_elf_strtab_finalize (dynstr);
3204 size = _bfd_elf_strtab_size (dynstr);
3205
3206 bed = get_elf_backend_data (dynobj);
3207 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3208 BFD_ASSERT (sdyn != NULL);
3209
3210 /* Update all .dynamic entries referencing .dynstr strings. */
3211 for (extdyn = sdyn->contents;
eea6121a 3212 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3213 extdyn += bed->s->sizeof_dyn)
3214 {
3215 Elf_Internal_Dyn dyn;
3216
3217 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3218 switch (dyn.d_tag)
3219 {
3220 case DT_STRSZ:
3221 dyn.d_un.d_val = size;
3222 break;
3223 case DT_NEEDED:
3224 case DT_SONAME:
3225 case DT_RPATH:
3226 case DT_RUNPATH:
3227 case DT_FILTER:
3228 case DT_AUXILIARY:
7ee314fa
AM
3229 case DT_AUDIT:
3230 case DT_DEPAUDIT:
5a580b3a
AM
3231 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3232 break;
3233 default:
3234 continue;
3235 }
3236 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3237 }
3238
3239 /* Now update local dynamic symbols. */
3240 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3241 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3242 entry->isym.st_name);
3243
3244 /* And the rest of dynamic symbols. */
3245 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3246
3247 /* Adjust version definitions. */
3248 if (elf_tdata (output_bfd)->cverdefs)
3249 {
3250 asection *s;
3251 bfd_byte *p;
3252 bfd_size_type i;
3253 Elf_Internal_Verdef def;
3254 Elf_Internal_Verdaux defaux;
3255
3256 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3257 p = s->contents;
3258 do
3259 {
3260 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3261 &def);
3262 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3263 if (def.vd_aux != sizeof (Elf_External_Verdef))
3264 continue;
5a580b3a
AM
3265 for (i = 0; i < def.vd_cnt; ++i)
3266 {
3267 _bfd_elf_swap_verdaux_in (output_bfd,
3268 (Elf_External_Verdaux *) p, &defaux);
3269 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3270 defaux.vda_name);
3271 _bfd_elf_swap_verdaux_out (output_bfd,
3272 &defaux, (Elf_External_Verdaux *) p);
3273 p += sizeof (Elf_External_Verdaux);
3274 }
3275 }
3276 while (def.vd_next);
3277 }
3278
3279 /* Adjust version references. */
3280 if (elf_tdata (output_bfd)->verref)
3281 {
3282 asection *s;
3283 bfd_byte *p;
3284 bfd_size_type i;
3285 Elf_Internal_Verneed need;
3286 Elf_Internal_Vernaux needaux;
3287
3288 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3289 p = s->contents;
3290 do
3291 {
3292 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3293 &need);
3294 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3295 _bfd_elf_swap_verneed_out (output_bfd, &need,
3296 (Elf_External_Verneed *) p);
3297 p += sizeof (Elf_External_Verneed);
3298 for (i = 0; i < need.vn_cnt; ++i)
3299 {
3300 _bfd_elf_swap_vernaux_in (output_bfd,
3301 (Elf_External_Vernaux *) p, &needaux);
3302 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3303 needaux.vna_name);
3304 _bfd_elf_swap_vernaux_out (output_bfd,
3305 &needaux,
3306 (Elf_External_Vernaux *) p);
3307 p += sizeof (Elf_External_Vernaux);
3308 }
3309 }
3310 while (need.vn_next);
3311 }
3312
3313 return TRUE;
3314}
3315\f
13285a1b
AM
3316/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3317 The default is to only match when the INPUT and OUTPUT are exactly
3318 the same target. */
3319
3320bfd_boolean
3321_bfd_elf_default_relocs_compatible (const bfd_target *input,
3322 const bfd_target *output)
3323{
3324 return input == output;
3325}
3326
3327/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3328 This version is used when different targets for the same architecture
3329 are virtually identical. */
3330
3331bfd_boolean
3332_bfd_elf_relocs_compatible (const bfd_target *input,
3333 const bfd_target *output)
3334{
3335 const struct elf_backend_data *obed, *ibed;
3336
3337 if (input == output)
3338 return TRUE;
3339
3340 ibed = xvec_get_elf_backend_data (input);
3341 obed = xvec_get_elf_backend_data (output);
3342
3343 if (ibed->arch != obed->arch)
3344 return FALSE;
3345
3346 /* If both backends are using this function, deem them compatible. */
3347 return ibed->relocs_compatible == obed->relocs_compatible;
3348}
3349
4ad4eba5
AM
3350/* Add symbols from an ELF object file to the linker hash table. */
3351
3352static bfd_boolean
3353elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3354{
a0c402a5 3355 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3356 Elf_Internal_Shdr *hdr;
3357 bfd_size_type symcount;
3358 bfd_size_type extsymcount;
3359 bfd_size_type extsymoff;
3360 struct elf_link_hash_entry **sym_hash;
3361 bfd_boolean dynamic;
3362 Elf_External_Versym *extversym = NULL;
3363 Elf_External_Versym *ever;
3364 struct elf_link_hash_entry *weaks;
3365 struct elf_link_hash_entry **nondeflt_vers = NULL;
3366 bfd_size_type nondeflt_vers_cnt = 0;
3367 Elf_Internal_Sym *isymbuf = NULL;
3368 Elf_Internal_Sym *isym;
3369 Elf_Internal_Sym *isymend;
3370 const struct elf_backend_data *bed;
3371 bfd_boolean add_needed;
66eb6687 3372 struct elf_link_hash_table *htab;
4ad4eba5 3373 bfd_size_type amt;
66eb6687 3374 void *alloc_mark = NULL;
4f87808c
AM
3375 struct bfd_hash_entry **old_table = NULL;
3376 unsigned int old_size = 0;
3377 unsigned int old_count = 0;
66eb6687
AM
3378 void *old_tab = NULL;
3379 void *old_hash;
3380 void *old_ent;
3381 struct bfd_link_hash_entry *old_undefs = NULL;
3382 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3383 long old_dynsymcount = 0;
3384 size_t tabsize = 0;
3385 size_t hashsize = 0;
4ad4eba5 3386
66eb6687 3387 htab = elf_hash_table (info);
4ad4eba5 3388 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3389
3390 if ((abfd->flags & DYNAMIC) == 0)
3391 dynamic = FALSE;
3392 else
3393 {
3394 dynamic = TRUE;
3395
3396 /* You can't use -r against a dynamic object. Also, there's no
3397 hope of using a dynamic object which does not exactly match
3398 the format of the output file. */
3399 if (info->relocatable
66eb6687 3400 || !is_elf_hash_table (htab)
f13a99db 3401 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3402 {
9a0789ec
NC
3403 if (info->relocatable)
3404 bfd_set_error (bfd_error_invalid_operation);
3405 else
3406 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3407 goto error_return;
3408 }
3409 }
3410
a0c402a5
L
3411 ehdr = elf_elfheader (abfd);
3412 if (info->warn_alternate_em
3413 && bed->elf_machine_code != ehdr->e_machine
3414 && ((bed->elf_machine_alt1 != 0
3415 && ehdr->e_machine == bed->elf_machine_alt1)
3416 || (bed->elf_machine_alt2 != 0
3417 && ehdr->e_machine == bed->elf_machine_alt2)))
3418 info->callbacks->einfo
3419 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3420 ehdr->e_machine, abfd, bed->elf_machine_code);
3421
4ad4eba5
AM
3422 /* As a GNU extension, any input sections which are named
3423 .gnu.warning.SYMBOL are treated as warning symbols for the given
3424 symbol. This differs from .gnu.warning sections, which generate
3425 warnings when they are included in an output file. */
3426 if (info->executable)
3427 {
3428 asection *s;
3429
3430 for (s = abfd->sections; s != NULL; s = s->next)
3431 {
3432 const char *name;
3433
3434 name = bfd_get_section_name (abfd, s);
0112cd26 3435 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5
AM
3436 {
3437 char *msg;
3438 bfd_size_type sz;
4ad4eba5
AM
3439
3440 name += sizeof ".gnu.warning." - 1;
3441
3442 /* If this is a shared object, then look up the symbol
3443 in the hash table. If it is there, and it is already
3444 been defined, then we will not be using the entry
3445 from this shared object, so we don't need to warn.
3446 FIXME: If we see the definition in a regular object
3447 later on, we will warn, but we shouldn't. The only
3448 fix is to keep track of what warnings we are supposed
3449 to emit, and then handle them all at the end of the
3450 link. */
3451 if (dynamic)
3452 {
3453 struct elf_link_hash_entry *h;
3454
66eb6687 3455 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4ad4eba5
AM
3456
3457 /* FIXME: What about bfd_link_hash_common? */
3458 if (h != NULL
3459 && (h->root.type == bfd_link_hash_defined
3460 || h->root.type == bfd_link_hash_defweak))
3461 {
3462 /* We don't want to issue this warning. Clobber
3463 the section size so that the warning does not
3464 get copied into the output file. */
eea6121a 3465 s->size = 0;
4ad4eba5
AM
3466 continue;
3467 }
3468 }
3469
eea6121a 3470 sz = s->size;
a50b1753 3471 msg = (char *) bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3472 if (msg == NULL)
3473 goto error_return;
3474
370a0e1b 3475 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3476 goto error_return;
3477
370a0e1b 3478 msg[sz] = '\0';
4ad4eba5
AM
3479
3480 if (! (_bfd_generic_link_add_one_symbol
3481 (info, abfd, name, BSF_WARNING, s, 0, msg,
66eb6687 3482 FALSE, bed->collect, NULL)))
4ad4eba5
AM
3483 goto error_return;
3484
3485 if (! info->relocatable)
3486 {
3487 /* Clobber the section size so that the warning does
3488 not get copied into the output file. */
eea6121a 3489 s->size = 0;
11d2f718
AM
3490
3491 /* Also set SEC_EXCLUDE, so that symbols defined in
3492 the warning section don't get copied to the output. */
3493 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3494 }
3495 }
3496 }
3497 }
3498
3499 add_needed = TRUE;
3500 if (! dynamic)
3501 {
3502 /* If we are creating a shared library, create all the dynamic
3503 sections immediately. We need to attach them to something,
3504 so we attach them to this BFD, provided it is the right
3505 format. FIXME: If there are no input BFD's of the same
3506 format as the output, we can't make a shared library. */
3507 if (info->shared
66eb6687 3508 && is_elf_hash_table (htab)
f13a99db 3509 && info->output_bfd->xvec == abfd->xvec
66eb6687 3510 && !htab->dynamic_sections_created)
4ad4eba5
AM
3511 {
3512 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3513 goto error_return;
3514 }
3515 }
66eb6687 3516 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3517 goto error_return;
3518 else
3519 {
3520 asection *s;
3521 const char *soname = NULL;
7ee314fa 3522 char *audit = NULL;
4ad4eba5
AM
3523 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3524 int ret;
3525
3526 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3527 ld shouldn't allow it. */
4ad4eba5
AM
3528 if ((s = abfd->sections) != NULL
3529 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
92fd189d 3530 abort ();
4ad4eba5
AM
3531
3532 /* If this dynamic lib was specified on the command line with
3533 --as-needed in effect, then we don't want to add a DT_NEEDED
3534 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3535 in by another lib's DT_NEEDED. When --no-add-needed is used
3536 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3537 any dynamic library in DT_NEEDED tags in the dynamic lib at
3538 all. */
3539 add_needed = (elf_dyn_lib_class (abfd)
3540 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3541 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3542
3543 s = bfd_get_section_by_name (abfd, ".dynamic");
3544 if (s != NULL)
3545 {
3546 bfd_byte *dynbuf;
3547 bfd_byte *extdyn;
cb33740c 3548 unsigned int elfsec;
4ad4eba5
AM
3549 unsigned long shlink;
3550
eea6121a 3551 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3552 {
3553error_free_dyn:
3554 free (dynbuf);
3555 goto error_return;
3556 }
4ad4eba5
AM
3557
3558 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3559 if (elfsec == SHN_BAD)
4ad4eba5
AM
3560 goto error_free_dyn;
3561 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3562
3563 for (extdyn = dynbuf;
eea6121a 3564 extdyn < dynbuf + s->size;
4ad4eba5
AM
3565 extdyn += bed->s->sizeof_dyn)
3566 {
3567 Elf_Internal_Dyn dyn;
3568
3569 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3570 if (dyn.d_tag == DT_SONAME)
3571 {
3572 unsigned int tagv = dyn.d_un.d_val;
3573 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3574 if (soname == NULL)
3575 goto error_free_dyn;
3576 }
3577 if (dyn.d_tag == DT_NEEDED)
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;
66eb6687 3596 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3597 ;
3598 *pn = n;
3599 }
3600 if (dyn.d_tag == DT_RUNPATH)
3601 {
3602 struct bfd_link_needed_list *n, **pn;
3603 char *fnm, *anm;
3604 unsigned int tagv = dyn.d_un.d_val;
3605
3606 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3607 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3608 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3609 if (n == NULL || fnm == NULL)
3610 goto error_free_dyn;
3611 amt = strlen (fnm) + 1;
a50b1753 3612 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3613 if (anm == NULL)
3614 goto error_free_dyn;
3615 memcpy (anm, fnm, amt);
3616 n->name = anm;
3617 n->by = abfd;
3618 n->next = NULL;
3619 for (pn = & runpath;
3620 *pn != NULL;
3621 pn = &(*pn)->next)
3622 ;
3623 *pn = n;
3624 }
3625 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3626 if (!runpath && dyn.d_tag == DT_RPATH)
3627 {
3628 struct bfd_link_needed_list *n, **pn;
3629 char *fnm, *anm;
3630 unsigned int tagv = dyn.d_un.d_val;
3631
3632 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3633 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3634 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3635 if (n == NULL || fnm == NULL)
3636 goto error_free_dyn;
3637 amt = strlen (fnm) + 1;
a50b1753 3638 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3639 if (anm == NULL)
f8703194 3640 goto error_free_dyn;
4ad4eba5
AM
3641 memcpy (anm, fnm, amt);
3642 n->name = anm;
3643 n->by = abfd;
3644 n->next = NULL;
3645 for (pn = & rpath;
3646 *pn != NULL;
3647 pn = &(*pn)->next)
3648 ;
3649 *pn = n;
3650 }
7ee314fa
AM
3651 if (dyn.d_tag == DT_AUDIT)
3652 {
3653 unsigned int tagv = dyn.d_un.d_val;
3654 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3655 }
4ad4eba5
AM
3656 }
3657
3658 free (dynbuf);
3659 }
3660
3661 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3662 frees all more recently bfd_alloc'd blocks as well. */
3663 if (runpath)
3664 rpath = runpath;
3665
3666 if (rpath)
3667 {
3668 struct bfd_link_needed_list **pn;
66eb6687 3669 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3670 ;
3671 *pn = rpath;
3672 }
3673
3674 /* We do not want to include any of the sections in a dynamic
3675 object in the output file. We hack by simply clobbering the
3676 list of sections in the BFD. This could be handled more
3677 cleanly by, say, a new section flag; the existing
3678 SEC_NEVER_LOAD flag is not the one we want, because that one
3679 still implies that the section takes up space in the output
3680 file. */
3681 bfd_section_list_clear (abfd);
3682
4ad4eba5
AM
3683 /* Find the name to use in a DT_NEEDED entry that refers to this
3684 object. If the object has a DT_SONAME entry, we use it.
3685 Otherwise, if the generic linker stuck something in
3686 elf_dt_name, we use that. Otherwise, we just use the file
3687 name. */
3688 if (soname == NULL || *soname == '\0')
3689 {
3690 soname = elf_dt_name (abfd);
3691 if (soname == NULL || *soname == '\0')
3692 soname = bfd_get_filename (abfd);
3693 }
3694
3695 /* Save the SONAME because sometimes the linker emulation code
3696 will need to know it. */
3697 elf_dt_name (abfd) = soname;
3698
7e9f0867 3699 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3700 if (ret < 0)
3701 goto error_return;
3702
3703 /* If we have already included this dynamic object in the
3704 link, just ignore it. There is no reason to include a
3705 particular dynamic object more than once. */
3706 if (ret > 0)
3707 return TRUE;
7ee314fa
AM
3708
3709 /* Save the DT_AUDIT entry for the linker emulation code. */
3710 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3711 }
3712
3713 /* If this is a dynamic object, we always link against the .dynsym
3714 symbol table, not the .symtab symbol table. The dynamic linker
3715 will only see the .dynsym symbol table, so there is no reason to
3716 look at .symtab for a dynamic object. */
3717
3718 if (! dynamic || elf_dynsymtab (abfd) == 0)
3719 hdr = &elf_tdata (abfd)->symtab_hdr;
3720 else
3721 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3722
3723 symcount = hdr->sh_size / bed->s->sizeof_sym;
3724
3725 /* The sh_info field of the symtab header tells us where the
3726 external symbols start. We don't care about the local symbols at
3727 this point. */
3728 if (elf_bad_symtab (abfd))
3729 {
3730 extsymcount = symcount;
3731 extsymoff = 0;
3732 }
3733 else
3734 {
3735 extsymcount = symcount - hdr->sh_info;
3736 extsymoff = hdr->sh_info;
3737 }
3738
3739 sym_hash = NULL;
3740 if (extsymcount != 0)
3741 {
3742 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3743 NULL, NULL, NULL);
3744 if (isymbuf == NULL)
3745 goto error_return;
3746
3747 /* We store a pointer to the hash table entry for each external
3748 symbol. */
3749 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 3750 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4ad4eba5
AM
3751 if (sym_hash == NULL)
3752 goto error_free_sym;
3753 elf_sym_hashes (abfd) = sym_hash;
3754 }
3755
3756 if (dynamic)
3757 {
3758 /* Read in any version definitions. */
fc0e6df6
PB
3759 if (!_bfd_elf_slurp_version_tables (abfd,
3760 info->default_imported_symver))
4ad4eba5
AM
3761 goto error_free_sym;
3762
3763 /* Read in the symbol versions, but don't bother to convert them
3764 to internal format. */
3765 if (elf_dynversym (abfd) != 0)
3766 {
3767 Elf_Internal_Shdr *versymhdr;
3768
3769 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3770 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3771 if (extversym == NULL)
3772 goto error_free_sym;
3773 amt = versymhdr->sh_size;
3774 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3775 || bfd_bread (extversym, amt, abfd) != amt)
3776 goto error_free_vers;
3777 }
3778 }
3779
66eb6687
AM
3780 /* If we are loading an as-needed shared lib, save the symbol table
3781 state before we start adding symbols. If the lib turns out
3782 to be unneeded, restore the state. */
3783 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3784 {
3785 unsigned int i;
3786 size_t entsize;
3787
3788 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3789 {
3790 struct bfd_hash_entry *p;
2de92251 3791 struct elf_link_hash_entry *h;
66eb6687
AM
3792
3793 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3794 {
3795 h = (struct elf_link_hash_entry *) p;
3796 entsize += htab->root.table.entsize;
3797 if (h->root.type == bfd_link_hash_warning)
3798 entsize += htab->root.table.entsize;
3799 }
66eb6687
AM
3800 }
3801
3802 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3803 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3804 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3805 if (old_tab == NULL)
3806 goto error_free_vers;
3807
3808 /* Remember the current objalloc pointer, so that all mem for
3809 symbols added can later be reclaimed. */
3810 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3811 if (alloc_mark == NULL)
3812 goto error_free_vers;
3813
5061a885
AM
3814 /* Make a special call to the linker "notice" function to
3815 tell it that we are about to handle an as-needed lib. */
3816 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 3817 notice_as_needed, 0, NULL))
9af2a943 3818 goto error_free_vers;
5061a885 3819
66eb6687
AM
3820 /* Clone the symbol table and sym hashes. Remember some
3821 pointers into the symbol table, and dynamic symbol count. */
3822 old_hash = (char *) old_tab + tabsize;
3823 old_ent = (char *) old_hash + hashsize;
3824 memcpy (old_tab, htab->root.table.table, tabsize);
3825 memcpy (old_hash, sym_hash, hashsize);
3826 old_undefs = htab->root.undefs;
3827 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3828 old_table = htab->root.table.table;
3829 old_size = htab->root.table.size;
3830 old_count = htab->root.table.count;
66eb6687
AM
3831 old_dynsymcount = htab->dynsymcount;
3832
3833 for (i = 0; i < htab->root.table.size; i++)
3834 {
3835 struct bfd_hash_entry *p;
2de92251 3836 struct elf_link_hash_entry *h;
66eb6687
AM
3837
3838 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3839 {
3840 memcpy (old_ent, p, htab->root.table.entsize);
3841 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3842 h = (struct elf_link_hash_entry *) p;
3843 if (h->root.type == bfd_link_hash_warning)
3844 {
3845 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3846 old_ent = (char *) old_ent + htab->root.table.entsize;
3847 }
66eb6687
AM
3848 }
3849 }
3850 }
4ad4eba5 3851
66eb6687 3852 weaks = NULL;
4ad4eba5
AM
3853 ever = extversym != NULL ? extversym + extsymoff : NULL;
3854 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3855 isym < isymend;
3856 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3857 {
3858 int bind;
3859 bfd_vma value;
af44c138 3860 asection *sec, *new_sec;
4ad4eba5
AM
3861 flagword flags;
3862 const char *name;
3863 struct elf_link_hash_entry *h;
3864 bfd_boolean definition;
3865 bfd_boolean size_change_ok;
3866 bfd_boolean type_change_ok;
3867 bfd_boolean new_weakdef;
3868 bfd_boolean override;
a4d8e49b 3869 bfd_boolean common;
4ad4eba5
AM
3870 unsigned int old_alignment;
3871 bfd *old_bfd;
3cbc5de0 3872 bfd * undef_bfd = NULL;
4ad4eba5
AM
3873
3874 override = FALSE;
3875
3876 flags = BSF_NO_FLAGS;
3877 sec = NULL;
3878 value = isym->st_value;
3879 *sym_hash = NULL;
a4d8e49b 3880 common = bed->common_definition (isym);
4ad4eba5
AM
3881
3882 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3883 switch (bind)
4ad4eba5 3884 {
3e7a7d11 3885 case STB_LOCAL:
4ad4eba5
AM
3886 /* This should be impossible, since ELF requires that all
3887 global symbols follow all local symbols, and that sh_info
3888 point to the first global symbol. Unfortunately, Irix 5
3889 screws this up. */
3890 continue;
3e7a7d11
NC
3891
3892 case STB_GLOBAL:
a4d8e49b 3893 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3894 flags = BSF_GLOBAL;
3e7a7d11
NC
3895 break;
3896
3897 case STB_WEAK:
3898 flags = BSF_WEAK;
3899 break;
3900
3901 case STB_GNU_UNIQUE:
3902 flags = BSF_GNU_UNIQUE;
3903 break;
3904
3905 default:
4ad4eba5 3906 /* Leave it up to the processor backend. */
3e7a7d11 3907 break;
4ad4eba5
AM
3908 }
3909
3910 if (isym->st_shndx == SHN_UNDEF)
3911 sec = bfd_und_section_ptr;
cb33740c
AM
3912 else if (isym->st_shndx == SHN_ABS)
3913 sec = bfd_abs_section_ptr;
3914 else if (isym->st_shndx == SHN_COMMON)
3915 {
3916 sec = bfd_com_section_ptr;
3917 /* What ELF calls the size we call the value. What ELF
3918 calls the value we call the alignment. */
3919 value = isym->st_size;
3920 }
3921 else
4ad4eba5
AM
3922 {
3923 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3924 if (sec == NULL)
3925 sec = bfd_abs_section_ptr;
529fcb95
PB
3926 else if (sec->kept_section)
3927 {
e5d08002
L
3928 /* Symbols from discarded section are undefined. We keep
3929 its visibility. */
529fcb95
PB
3930 sec = bfd_und_section_ptr;
3931 isym->st_shndx = SHN_UNDEF;
3932 }
4ad4eba5
AM
3933 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3934 value -= sec->vma;
3935 }
4ad4eba5
AM
3936
3937 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3938 isym->st_name);
3939 if (name == NULL)
3940 goto error_free_vers;
3941
3942 if (isym->st_shndx == SHN_COMMON
02d00247
AM
3943 && (abfd->flags & BFD_PLUGIN) != 0)
3944 {
3945 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3946
3947 if (xc == NULL)
3948 {
3949 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3950 | SEC_EXCLUDE);
3951 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3952 if (xc == NULL)
3953 goto error_free_vers;
3954 }
3955 sec = xc;
3956 }
3957 else if (isym->st_shndx == SHN_COMMON
3958 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3959 && !info->relocatable)
4ad4eba5
AM
3960 {
3961 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3962
3963 if (tcomm == NULL)
3964 {
02d00247
AM
3965 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3966 | SEC_LINKER_CREATED);
3967 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 3968 if (tcomm == NULL)
4ad4eba5
AM
3969 goto error_free_vers;
3970 }
3971 sec = tcomm;
3972 }
66eb6687 3973 else if (bed->elf_add_symbol_hook)
4ad4eba5 3974 {
66eb6687
AM
3975 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3976 &sec, &value))
4ad4eba5
AM
3977 goto error_free_vers;
3978
3979 /* The hook function sets the name to NULL if this symbol
3980 should be skipped for some reason. */
3981 if (name == NULL)
3982 continue;
3983 }
3984
3985 /* Sanity check that all possibilities were handled. */
3986 if (sec == NULL)
3987 {
3988 bfd_set_error (bfd_error_bad_value);
3989 goto error_free_vers;
3990 }
3991
3992 if (bfd_is_und_section (sec)
3993 || bfd_is_com_section (sec))
3994 definition = FALSE;
3995 else
3996 definition = TRUE;
3997
3998 size_change_ok = FALSE;
66eb6687 3999 type_change_ok = bed->type_change_ok;
4ad4eba5
AM
4000 old_alignment = 0;
4001 old_bfd = NULL;
af44c138 4002 new_sec = sec;
4ad4eba5 4003
66eb6687 4004 if (is_elf_hash_table (htab))
4ad4eba5
AM
4005 {
4006 Elf_Internal_Versym iver;
4007 unsigned int vernum = 0;
4008 bfd_boolean skip;
4009
b918acf9
NC
4010 /* If this is a definition of a symbol which was previously
4011 referenced in a non-weak manner then make a note of the bfd
4012 that contained the reference. This is used if we need to
4013 refer to the source of the reference later on. */
4014 if (! bfd_is_und_section (sec))
4015 {
4016 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4017
4018 if (h != NULL
4019 && h->root.type == bfd_link_hash_undefined
4020 && h->root.u.undef.abfd)
4021 undef_bfd = h->root.u.undef.abfd;
4022 }
4023
fc0e6df6 4024 if (ever == NULL)
4ad4eba5 4025 {
fc0e6df6
PB
4026 if (info->default_imported_symver)
4027 /* Use the default symbol version created earlier. */
4028 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4029 else
4030 iver.vs_vers = 0;
4031 }
4032 else
4033 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4034
4035 vernum = iver.vs_vers & VERSYM_VERSION;
4036
4037 /* If this is a hidden symbol, or if it is not version
4038 1, we append the version name to the symbol name.
cc86ff91
EB
4039 However, we do not modify a non-hidden absolute symbol
4040 if it is not a function, because it might be the version
4041 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4042 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4043 || (vernum > 1
4044 && (!bfd_is_abs_section (sec)
4045 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4046 {
4047 const char *verstr;
4048 size_t namelen, verlen, newlen;
4049 char *newname, *p;
4050
4051 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4052 {
fc0e6df6
PB
4053 if (vernum > elf_tdata (abfd)->cverdefs)
4054 verstr = NULL;
4055 else if (vernum > 1)
4056 verstr =
4057 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4058 else
4059 verstr = "";
4ad4eba5 4060
fc0e6df6 4061 if (verstr == NULL)
4ad4eba5 4062 {
fc0e6df6
PB
4063 (*_bfd_error_handler)
4064 (_("%B: %s: invalid version %u (max %d)"),
4065 abfd, name, vernum,
4066 elf_tdata (abfd)->cverdefs);
4067 bfd_set_error (bfd_error_bad_value);
4068 goto error_free_vers;
4ad4eba5 4069 }
fc0e6df6
PB
4070 }
4071 else
4072 {
4073 /* We cannot simply test for the number of
4074 entries in the VERNEED section since the
4075 numbers for the needed versions do not start
4076 at 0. */
4077 Elf_Internal_Verneed *t;
4078
4079 verstr = NULL;
4080 for (t = elf_tdata (abfd)->verref;
4081 t != NULL;
4082 t = t->vn_nextref)
4ad4eba5 4083 {
fc0e6df6 4084 Elf_Internal_Vernaux *a;
4ad4eba5 4085
fc0e6df6
PB
4086 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4087 {
4088 if (a->vna_other == vernum)
4ad4eba5 4089 {
fc0e6df6
PB
4090 verstr = a->vna_nodename;
4091 break;
4ad4eba5 4092 }
4ad4eba5 4093 }
fc0e6df6
PB
4094 if (a != NULL)
4095 break;
4096 }
4097 if (verstr == NULL)
4098 {
4099 (*_bfd_error_handler)
4100 (_("%B: %s: invalid needed version %d"),
4101 abfd, name, vernum);
4102 bfd_set_error (bfd_error_bad_value);
4103 goto error_free_vers;
4ad4eba5 4104 }
4ad4eba5 4105 }
fc0e6df6
PB
4106
4107 namelen = strlen (name);
4108 verlen = strlen (verstr);
4109 newlen = namelen + verlen + 2;
4110 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4111 && isym->st_shndx != SHN_UNDEF)
4112 ++newlen;
4113
a50b1753 4114 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4115 if (newname == NULL)
4116 goto error_free_vers;
4117 memcpy (newname, name, namelen);
4118 p = newname + namelen;
4119 *p++ = ELF_VER_CHR;
4120 /* If this is a defined non-hidden version symbol,
4121 we add another @ to the name. This indicates the
4122 default version of the symbol. */
4123 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4124 && isym->st_shndx != SHN_UNDEF)
4125 *p++ = ELF_VER_CHR;
4126 memcpy (p, verstr, verlen + 1);
4127
4128 name = newname;
4ad4eba5
AM
4129 }
4130
b918acf9
NC
4131 /* If necessary, make a second attempt to locate the bfd
4132 containing an unresolved, non-weak reference to the
4133 current symbol. */
4134 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
3cbc5de0
NC
4135 {
4136 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4137
4138 if (h != NULL
b918acf9 4139 && h->root.type == bfd_link_hash_undefined
3cbc5de0
NC
4140 && h->root.u.undef.abfd)
4141 undef_bfd = h->root.u.undef.abfd;
4142 }
4143
af44c138
L
4144 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4145 &value, &old_alignment,
4ad4eba5
AM
4146 sym_hash, &skip, &override,
4147 &type_change_ok, &size_change_ok))
4148 goto error_free_vers;
4149
4150 if (skip)
4151 continue;
4152
4153 if (override)
4154 definition = FALSE;
4155
4156 h = *sym_hash;
4157 while (h->root.type == bfd_link_hash_indirect
4158 || h->root.type == bfd_link_hash_warning)
4159 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4160
4161 /* Remember the old alignment if this is a common symbol, so
4162 that we don't reduce the alignment later on. We can't
4163 check later, because _bfd_generic_link_add_one_symbol
4164 will set a default for the alignment which we want to
4165 override. We also remember the old bfd where the existing
4166 definition comes from. */
4167 switch (h->root.type)
4168 {
4169 default:
4170 break;
4171
4172 case bfd_link_hash_defined:
4173 case bfd_link_hash_defweak:
4174 old_bfd = h->root.u.def.section->owner;
4175 break;
4176
4177 case bfd_link_hash_common:
4178 old_bfd = h->root.u.c.p->section->owner;
4179 old_alignment = h->root.u.c.p->alignment_power;
4180 break;
4181 }
4182
4183 if (elf_tdata (abfd)->verdef != NULL
4184 && ! override
4185 && vernum > 1
4186 && definition)
4187 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4188 }
4189
4190 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4191 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4192 (struct bfd_link_hash_entry **) sym_hash)))
4193 goto error_free_vers;
4194
4195 h = *sym_hash;
4196 while (h->root.type == bfd_link_hash_indirect
4197 || h->root.type == bfd_link_hash_warning)
4198 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4199
4ad4eba5 4200 *sym_hash = h;
d64284fe
L
4201 if (is_elf_hash_table (htab))
4202 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4ad4eba5
AM
4203
4204 new_weakdef = FALSE;
4205 if (dynamic
4206 && definition
4207 && (flags & BSF_WEAK) != 0
fcb93ecf 4208 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4209 && is_elf_hash_table (htab)
f6e332e6 4210 && h->u.weakdef == NULL)
4ad4eba5
AM
4211 {
4212 /* Keep a list of all weak defined non function symbols from
4213 a dynamic object, using the weakdef field. Later in this
4214 function we will set the weakdef field to the correct
4215 value. We only put non-function symbols from dynamic
4216 objects on this list, because that happens to be the only
4217 time we need to know the normal symbol corresponding to a
4218 weak symbol, and the information is time consuming to
4219 figure out. If the weakdef field is not already NULL,
4220 then this symbol was already defined by some previous
4221 dynamic object, and we will be using that previous
4222 definition anyhow. */
4223
f6e332e6 4224 h->u.weakdef = weaks;
4ad4eba5
AM
4225 weaks = h;
4226 new_weakdef = TRUE;
4227 }
4228
4229 /* Set the alignment of a common symbol. */
a4d8e49b 4230 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4231 && h->root.type == bfd_link_hash_common)
4232 {
4233 unsigned int align;
4234
a4d8e49b 4235 if (common)
af44c138
L
4236 align = bfd_log2 (isym->st_value);
4237 else
4238 {
4239 /* The new symbol is a common symbol in a shared object.
4240 We need to get the alignment from the section. */
4241 align = new_sec->alignment_power;
4242 }
595213d4 4243 if (align > old_alignment)
4ad4eba5
AM
4244 h->root.u.c.p->alignment_power = align;
4245 else
4246 h->root.u.c.p->alignment_power = old_alignment;
4247 }
4248
66eb6687 4249 if (is_elf_hash_table (htab))
4ad4eba5 4250 {
4ad4eba5 4251 bfd_boolean dynsym;
4ad4eba5
AM
4252
4253 /* Check the alignment when a common symbol is involved. This
4254 can change when a common symbol is overridden by a normal
4255 definition or a common symbol is ignored due to the old
4256 normal definition. We need to make sure the maximum
4257 alignment is maintained. */
a4d8e49b 4258 if ((old_alignment || common)
4ad4eba5
AM
4259 && h->root.type != bfd_link_hash_common)
4260 {
4261 unsigned int common_align;
4262 unsigned int normal_align;
4263 unsigned int symbol_align;
4264 bfd *normal_bfd;
4265 bfd *common_bfd;
4266
4267 symbol_align = ffs (h->root.u.def.value) - 1;
4268 if (h->root.u.def.section->owner != NULL
4269 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4270 {
4271 normal_align = h->root.u.def.section->alignment_power;
4272 if (normal_align > symbol_align)
4273 normal_align = symbol_align;
4274 }
4275 else
4276 normal_align = symbol_align;
4277
4278 if (old_alignment)
4279 {
4280 common_align = old_alignment;
4281 common_bfd = old_bfd;
4282 normal_bfd = abfd;
4283 }
4284 else
4285 {
4286 common_align = bfd_log2 (isym->st_value);
4287 common_bfd = abfd;
4288 normal_bfd = old_bfd;
4289 }
4290
4291 if (normal_align < common_align)
d07676f8
NC
4292 {
4293 /* PR binutils/2735 */
4294 if (normal_bfd == NULL)
4295 (*_bfd_error_handler)
4296 (_("Warning: alignment %u of common symbol `%s' in %B"
4297 " is greater than the alignment (%u) of its section %A"),
4298 common_bfd, h->root.u.def.section,
4299 1 << common_align, name, 1 << normal_align);
4300 else
4301 (*_bfd_error_handler)
4302 (_("Warning: alignment %u of symbol `%s' in %B"
4303 " is smaller than %u in %B"),
4304 normal_bfd, common_bfd,
4305 1 << normal_align, name, 1 << common_align);
4306 }
4ad4eba5
AM
4307 }
4308
83ad0046
L
4309 /* Remember the symbol size if it isn't undefined. */
4310 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4ad4eba5
AM
4311 && (definition || h->size == 0))
4312 {
83ad0046
L
4313 if (h->size != 0
4314 && h->size != isym->st_size
4315 && ! size_change_ok)
4ad4eba5 4316 (*_bfd_error_handler)
d003868e
AM
4317 (_("Warning: size of symbol `%s' changed"
4318 " from %lu in %B to %lu in %B"),
4319 old_bfd, abfd,
4ad4eba5 4320 name, (unsigned long) h->size,
d003868e 4321 (unsigned long) isym->st_size);
4ad4eba5
AM
4322
4323 h->size = isym->st_size;
4324 }
4325
4326 /* If this is a common symbol, then we always want H->SIZE
4327 to be the size of the common symbol. The code just above
4328 won't fix the size if a common symbol becomes larger. We
4329 don't warn about a size change here, because that is
fcb93ecf
PB
4330 covered by --warn-common. Allow changed between different
4331 function types. */
4ad4eba5
AM
4332 if (h->root.type == bfd_link_hash_common)
4333 h->size = h->root.u.c.size;
4334
4335 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4336 && (definition || h->type == STT_NOTYPE))
4337 {
2955ec4c
L
4338 unsigned int type = ELF_ST_TYPE (isym->st_info);
4339
4340 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4341 symbol. */
4342 if (type == STT_GNU_IFUNC
4343 && (abfd->flags & DYNAMIC) != 0)
4344 type = STT_FUNC;
4ad4eba5 4345
2955ec4c
L
4346 if (h->type != type)
4347 {
4348 if (h->type != STT_NOTYPE && ! type_change_ok)
4349 (*_bfd_error_handler)
4350 (_("Warning: type of symbol `%s' changed"
4351 " from %d to %d in %B"),
4352 abfd, name, h->type, type);
4353
4354 h->type = type;
4355 }
4ad4eba5
AM
4356 }
4357
54ac0771
L
4358 /* Merge st_other field. */
4359 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4ad4eba5
AM
4360
4361 /* Set a flag in the hash table entry indicating the type of
4362 reference or definition we just found. Keep a count of
4363 the number of dynamic symbols we find. A dynamic symbol
4364 is one which is referenced or defined by both a regular
4365 object and a shared object. */
4ad4eba5
AM
4366 dynsym = FALSE;
4367 if (! dynamic)
4368 {
4369 if (! definition)
4370 {
f5385ebf 4371 h->ref_regular = 1;
4ad4eba5 4372 if (bind != STB_WEAK)
f5385ebf 4373 h->ref_regular_nonweak = 1;
4ad4eba5
AM
4374 }
4375 else
d8880531
L
4376 {
4377 h->def_regular = 1;
4378 if (h->def_dynamic)
4379 {
4380 h->def_dynamic = 0;
4381 h->ref_dynamic = 1;
4382 h->dynamic_def = 1;
4383 }
4384 }
4ad4eba5 4385 if (! info->executable
f5385ebf
AM
4386 || h->def_dynamic
4387 || h->ref_dynamic)
4ad4eba5
AM
4388 dynsym = TRUE;
4389 }
4390 else
4391 {
4392 if (! definition)
f5385ebf 4393 h->ref_dynamic = 1;
4ad4eba5 4394 else
f5385ebf
AM
4395 h->def_dynamic = 1;
4396 if (h->def_regular
4397 || h->ref_regular
f6e332e6 4398 || (h->u.weakdef != NULL
4ad4eba5 4399 && ! new_weakdef
f6e332e6 4400 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4401 dynsym = TRUE;
4402 }
4403
b2064611 4404 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
92b7c7b6
L
4405 {
4406 /* We don't want to make debug symbol dynamic. */
92b7c7b6
L
4407 dynsym = FALSE;
4408 }
4409
35fc36a8
RS
4410 if (definition)
4411 h->target_internal = isym->st_target_internal;
4412
4ad4eba5
AM
4413 /* Check to see if we need to add an indirect symbol for
4414 the default name. */
4415 if (definition || h->root.type == bfd_link_hash_common)
4416 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4417 &sec, &value, &dynsym,
4418 override))
4419 goto error_free_vers;
4420
4421 if (definition && !dynamic)
4422 {
4423 char *p = strchr (name, ELF_VER_CHR);
4424 if (p != NULL && p[1] != ELF_VER_CHR)
4425 {
4426 /* Queue non-default versions so that .symver x, x@FOO
4427 aliases can be checked. */
66eb6687 4428 if (!nondeflt_vers)
4ad4eba5 4429 {
66eb6687
AM
4430 amt = ((isymend - isym + 1)
4431 * sizeof (struct elf_link_hash_entry *));
a50b1753
NC
4432 nondeflt_vers =
4433 (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4434 if (!nondeflt_vers)
4435 goto error_free_vers;
4ad4eba5 4436 }
66eb6687 4437 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4438 }
4439 }
4440
4441 if (dynsym && h->dynindx == -1)
4442 {
c152c796 4443 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4444 goto error_free_vers;
f6e332e6 4445 if (h->u.weakdef != NULL
4ad4eba5 4446 && ! new_weakdef
f6e332e6 4447 && h->u.weakdef->dynindx == -1)
4ad4eba5 4448 {
66eb6687 4449 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4450 goto error_free_vers;
4451 }
4452 }
4453 else if (dynsym && h->dynindx != -1)
4454 /* If the symbol already has a dynamic index, but
4455 visibility says it should not be visible, turn it into
4456 a local symbol. */
4457 switch (ELF_ST_VISIBILITY (h->other))
4458 {
4459 case STV_INTERNAL:
4460 case STV_HIDDEN:
4461 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4462 dynsym = FALSE;
4463 break;
4464 }
4465
4466 if (!add_needed
4467 && definition
010e5ae2
AM
4468 && ((dynsym
4469 && h->ref_regular)
4470 || (h->ref_dynamic
4471 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4472 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4473 {
4474 int ret;
4475 const char *soname = elf_dt_name (abfd);
4476
4477 /* A symbol from a library loaded via DT_NEEDED of some
4478 other library is referenced by a regular object.
e56f61be 4479 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4480 --no-add-needed is used and the reference was not
4481 a weak one. */
4482 if (undef_bfd != NULL
4483 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4484 {
4485 (*_bfd_error_handler)
3cbc5de0 4486 (_("%B: undefined reference to symbol '%s'"),
b918acf9 4487 undef_bfd, name);
3cbc5de0
NC
4488 (*_bfd_error_handler)
4489 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
d003868e 4490 abfd, name);
3cbc5de0 4491 bfd_set_error (bfd_error_invalid_operation);
e56f61be
L
4492 goto error_free_vers;
4493 }
4494
a50b1753
NC
4495 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4496 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4497
4ad4eba5 4498 add_needed = TRUE;
7e9f0867 4499 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4500 if (ret < 0)
4501 goto error_free_vers;
4502
4503 BFD_ASSERT (ret == 0);
4504 }
4505 }
4506 }
4507
66eb6687
AM
4508 if (extversym != NULL)
4509 {
4510 free (extversym);
4511 extversym = NULL;
4512 }
4513
4514 if (isymbuf != NULL)
4515 {
4516 free (isymbuf);
4517 isymbuf = NULL;
4518 }
4519
4520 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4521 {
4522 unsigned int i;
4523
4524 /* Restore the symbol table. */
97fed1c9
JJ
4525 if (bed->as_needed_cleanup)
4526 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4527 old_hash = (char *) old_tab + tabsize;
4528 old_ent = (char *) old_hash + hashsize;
4529 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4530 htab->root.table.table = old_table;
4531 htab->root.table.size = old_size;
4532 htab->root.table.count = old_count;
66eb6687
AM
4533 memcpy (htab->root.table.table, old_tab, tabsize);
4534 memcpy (sym_hash, old_hash, hashsize);
4535 htab->root.undefs = old_undefs;
4536 htab->root.undefs_tail = old_undefs_tail;
4537 for (i = 0; i < htab->root.table.size; i++)
4538 {
4539 struct bfd_hash_entry *p;
4540 struct elf_link_hash_entry *h;
4541
4542 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4543 {
4544 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4545 if (h->root.type == bfd_link_hash_warning)
4546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4547 if (h->dynindx >= old_dynsymcount)
4548 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4549
66eb6687
AM
4550 memcpy (p, old_ent, htab->root.table.entsize);
4551 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4552 h = (struct elf_link_hash_entry *) p;
4553 if (h->root.type == bfd_link_hash_warning)
4554 {
4555 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4556 old_ent = (char *) old_ent + htab->root.table.entsize;
4557 }
66eb6687
AM
4558 }
4559 }
4560
5061a885
AM
4561 /* Make a special call to the linker "notice" function to
4562 tell it that symbols added for crefs may need to be removed. */
4563 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4564 notice_not_needed, 0, NULL))
9af2a943 4565 goto error_free_vers;
5061a885 4566
66eb6687
AM
4567 free (old_tab);
4568 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4569 alloc_mark);
4570 if (nondeflt_vers != NULL)
4571 free (nondeflt_vers);
4572 return TRUE;
4573 }
2de92251 4574
66eb6687
AM
4575 if (old_tab != NULL)
4576 {
5061a885 4577 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4578 notice_needed, 0, NULL))
9af2a943 4579 goto error_free_vers;
66eb6687
AM
4580 free (old_tab);
4581 old_tab = NULL;
4582 }
4583
4ad4eba5
AM
4584 /* Now that all the symbols from this input file are created, handle
4585 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4586 if (nondeflt_vers != NULL)
4587 {
4588 bfd_size_type cnt, symidx;
4589
4590 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4591 {
4592 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4593 char *shortname, *p;
4594
4595 p = strchr (h->root.root.string, ELF_VER_CHR);
4596 if (p == NULL
4597 || (h->root.type != bfd_link_hash_defined
4598 && h->root.type != bfd_link_hash_defweak))
4599 continue;
4600
4601 amt = p - h->root.root.string;
a50b1753 4602 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4603 if (!shortname)
4604 goto error_free_vers;
4ad4eba5
AM
4605 memcpy (shortname, h->root.root.string, amt);
4606 shortname[amt] = '\0';
4607
4608 hi = (struct elf_link_hash_entry *)
66eb6687 4609 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4610 FALSE, FALSE, FALSE);
4611 if (hi != NULL
4612 && hi->root.type == h->root.type
4613 && hi->root.u.def.value == h->root.u.def.value
4614 && hi->root.u.def.section == h->root.u.def.section)
4615 {
4616 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4617 hi->root.type = bfd_link_hash_indirect;
4618 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4619 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4620 sym_hash = elf_sym_hashes (abfd);
4621 if (sym_hash)
4622 for (symidx = 0; symidx < extsymcount; ++symidx)
4623 if (sym_hash[symidx] == hi)
4624 {
4625 sym_hash[symidx] = h;
4626 break;
4627 }
4628 }
4629 free (shortname);
4630 }
4631 free (nondeflt_vers);
4632 nondeflt_vers = NULL;
4633 }
4634
4ad4eba5
AM
4635 /* Now set the weakdefs field correctly for all the weak defined
4636 symbols we found. The only way to do this is to search all the
4637 symbols. Since we only need the information for non functions in
4638 dynamic objects, that's the only time we actually put anything on
4639 the list WEAKS. We need this information so that if a regular
4640 object refers to a symbol defined weakly in a dynamic object, the
4641 real symbol in the dynamic object is also put in the dynamic
4642 symbols; we also must arrange for both symbols to point to the
4643 same memory location. We could handle the general case of symbol
4644 aliasing, but a general symbol alias can only be generated in
4645 assembler code, handling it correctly would be very time
4646 consuming, and other ELF linkers don't handle general aliasing
4647 either. */
4648 if (weaks != NULL)
4649 {
4650 struct elf_link_hash_entry **hpp;
4651 struct elf_link_hash_entry **hppend;
4652 struct elf_link_hash_entry **sorted_sym_hash;
4653 struct elf_link_hash_entry *h;
4654 size_t sym_count;
4655
4656 /* Since we have to search the whole symbol list for each weak
4657 defined symbol, search time for N weak defined symbols will be
4658 O(N^2). Binary search will cut it down to O(NlogN). */
4659 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4660 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4661 if (sorted_sym_hash == NULL)
4662 goto error_return;
4663 sym_hash = sorted_sym_hash;
4664 hpp = elf_sym_hashes (abfd);
4665 hppend = hpp + extsymcount;
4666 sym_count = 0;
4667 for (; hpp < hppend; hpp++)
4668 {
4669 h = *hpp;
4670 if (h != NULL
4671 && h->root.type == bfd_link_hash_defined
fcb93ecf 4672 && !bed->is_function_type (h->type))
4ad4eba5
AM
4673 {
4674 *sym_hash = h;
4675 sym_hash++;
4676 sym_count++;
4677 }
4678 }
4679
4680 qsort (sorted_sym_hash, sym_count,
4681 sizeof (struct elf_link_hash_entry *),
4682 elf_sort_symbol);
4683
4684 while (weaks != NULL)
4685 {
4686 struct elf_link_hash_entry *hlook;
4687 asection *slook;
4688 bfd_vma vlook;
4689 long ilook;
4690 size_t i, j, idx;
4691
4692 hlook = weaks;
f6e332e6
AM
4693 weaks = hlook->u.weakdef;
4694 hlook->u.weakdef = NULL;
4ad4eba5
AM
4695
4696 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4697 || hlook->root.type == bfd_link_hash_defweak
4698 || hlook->root.type == bfd_link_hash_common
4699 || hlook->root.type == bfd_link_hash_indirect);
4700 slook = hlook->root.u.def.section;
4701 vlook = hlook->root.u.def.value;
4702
4703 ilook = -1;
4704 i = 0;
4705 j = sym_count;
4706 while (i < j)
4707 {
4708 bfd_signed_vma vdiff;
4709 idx = (i + j) / 2;
4710 h = sorted_sym_hash [idx];
4711 vdiff = vlook - h->root.u.def.value;
4712 if (vdiff < 0)
4713 j = idx;
4714 else if (vdiff > 0)
4715 i = idx + 1;
4716 else
4717 {
a9b881be 4718 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4719 if (sdiff < 0)
4720 j = idx;
4721 else if (sdiff > 0)
4722 i = idx + 1;
4723 else
4724 {
4725 ilook = idx;
4726 break;
4727 }
4728 }
4729 }
4730
4731 /* We didn't find a value/section match. */
4732 if (ilook == -1)
4733 continue;
4734
4735 for (i = ilook; i < sym_count; i++)
4736 {
4737 h = sorted_sym_hash [i];
4738
4739 /* Stop if value or section doesn't match. */
4740 if (h->root.u.def.value != vlook
4741 || h->root.u.def.section != slook)
4742 break;
4743 else if (h != hlook)
4744 {
f6e332e6 4745 hlook->u.weakdef = h;
4ad4eba5
AM
4746
4747 /* If the weak definition is in the list of dynamic
4748 symbols, make sure the real definition is put
4749 there as well. */
4750 if (hlook->dynindx != -1 && h->dynindx == -1)
4751 {
c152c796 4752 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4753 {
4754 err_free_sym_hash:
4755 free (sorted_sym_hash);
4756 goto error_return;
4757 }
4ad4eba5
AM
4758 }
4759
4760 /* If the real definition is in the list of dynamic
4761 symbols, make sure the weak definition is put
4762 there as well. If we don't do this, then the
4763 dynamic loader might not merge the entries for the
4764 real definition and the weak definition. */
4765 if (h->dynindx != -1 && hlook->dynindx == -1)
4766 {
c152c796 4767 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4768 goto err_free_sym_hash;
4ad4eba5
AM
4769 }
4770 break;
4771 }
4772 }
4773 }
4774
4775 free (sorted_sym_hash);
4776 }
4777
33177bb1
AM
4778 if (bed->check_directives
4779 && !(*bed->check_directives) (abfd, info))
4780 return FALSE;
85fbca6a 4781
4ad4eba5
AM
4782 /* If this object is the same format as the output object, and it is
4783 not a shared library, then let the backend look through the
4784 relocs.
4785
4786 This is required to build global offset table entries and to
4787 arrange for dynamic relocs. It is not required for the
4788 particular common case of linking non PIC code, even when linking
4789 against shared libraries, but unfortunately there is no way of
4790 knowing whether an object file has been compiled PIC or not.
4791 Looking through the relocs is not particularly time consuming.
4792 The problem is that we must either (1) keep the relocs in memory,
4793 which causes the linker to require additional runtime memory or
4794 (2) read the relocs twice from the input file, which wastes time.
4795 This would be a good case for using mmap.
4796
4797 I have no idea how to handle linking PIC code into a file of a
4798 different format. It probably can't be done. */
4ad4eba5 4799 if (! dynamic
66eb6687 4800 && is_elf_hash_table (htab)
13285a1b 4801 && bed->check_relocs != NULL
39334f3a 4802 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4803 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4804 {
4805 asection *o;
4806
4807 for (o = abfd->sections; o != NULL; o = o->next)
4808 {
4809 Elf_Internal_Rela *internal_relocs;
4810 bfd_boolean ok;
4811
4812 if ((o->flags & SEC_RELOC) == 0
4813 || o->reloc_count == 0
4814 || ((info->strip == strip_all || info->strip == strip_debugger)
4815 && (o->flags & SEC_DEBUGGING) != 0)
4816 || bfd_is_abs_section (o->output_section))
4817 continue;
4818
4819 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4820 info->keep_memory);
4821 if (internal_relocs == NULL)
4822 goto error_return;
4823
66eb6687 4824 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4825
4826 if (elf_section_data (o)->relocs != internal_relocs)
4827 free (internal_relocs);
4828
4829 if (! ok)
4830 goto error_return;
4831 }
4832 }
4833
4834 /* If this is a non-traditional link, try to optimize the handling
4835 of the .stab/.stabstr sections. */
4836 if (! dynamic
4837 && ! info->traditional_format
66eb6687 4838 && is_elf_hash_table (htab)
4ad4eba5
AM
4839 && (info->strip != strip_all && info->strip != strip_debugger))
4840 {
4841 asection *stabstr;
4842
4843 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4844 if (stabstr != NULL)
4845 {
4846 bfd_size_type string_offset = 0;
4847 asection *stab;
4848
4849 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4850 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4851 && (!stab->name[5] ||
4852 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4853 && (stab->flags & SEC_MERGE) == 0
4854 && !bfd_is_abs_section (stab->output_section))
4855 {
4856 struct bfd_elf_section_data *secdata;
4857
4858 secdata = elf_section_data (stab);
66eb6687
AM
4859 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4860 stabstr, &secdata->sec_info,
4ad4eba5
AM
4861 &string_offset))
4862 goto error_return;
4863 if (secdata->sec_info)
4864 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4865 }
4866 }
4867 }
4868
66eb6687 4869 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4870 {
4871 /* Add this bfd to the loaded list. */
4872 struct elf_link_loaded_list *n;
4873
a50b1753
NC
4874 n = (struct elf_link_loaded_list *)
4875 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4ad4eba5
AM
4876 if (n == NULL)
4877 goto error_return;
4878 n->abfd = abfd;
66eb6687
AM
4879 n->next = htab->loaded;
4880 htab->loaded = n;
4ad4eba5
AM
4881 }
4882
4883 return TRUE;
4884
4885 error_free_vers:
66eb6687
AM
4886 if (old_tab != NULL)
4887 free (old_tab);
4ad4eba5
AM
4888 if (nondeflt_vers != NULL)
4889 free (nondeflt_vers);
4890 if (extversym != NULL)
4891 free (extversym);
4892 error_free_sym:
4893 if (isymbuf != NULL)
4894 free (isymbuf);
4895 error_return:
4896 return FALSE;
4897}
4898
8387904d
AM
4899/* Return the linker hash table entry of a symbol that might be
4900 satisfied by an archive symbol. Return -1 on error. */
4901
4902struct elf_link_hash_entry *
4903_bfd_elf_archive_symbol_lookup (bfd *abfd,
4904 struct bfd_link_info *info,
4905 const char *name)
4906{
4907 struct elf_link_hash_entry *h;
4908 char *p, *copy;
4909 size_t len, first;
4910
4911 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4912 if (h != NULL)
4913 return h;
4914
4915 /* If this is a default version (the name contains @@), look up the
4916 symbol again with only one `@' as well as without the version.
4917 The effect is that references to the symbol with and without the
4918 version will be matched by the default symbol in the archive. */
4919
4920 p = strchr (name, ELF_VER_CHR);
4921 if (p == NULL || p[1] != ELF_VER_CHR)
4922 return h;
4923
4924 /* First check with only one `@'. */
4925 len = strlen (name);
a50b1753 4926 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
4927 if (copy == NULL)
4928 return (struct elf_link_hash_entry *) 0 - 1;
4929
4930 first = p - name + 1;
4931 memcpy (copy, name, first);
4932 memcpy (copy + first, name + first + 1, len - first);
4933
4934 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4935 if (h == NULL)
4936 {
4937 /* We also need to check references to the symbol without the
4938 version. */
4939 copy[first - 1] = '\0';
4940 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4941 FALSE, FALSE, FALSE);
4942 }
4943
4944 bfd_release (abfd, copy);
4945 return h;
4946}
4947
0ad989f9
L
4948/* Add symbols from an ELF archive file to the linker hash table. We
4949 don't use _bfd_generic_link_add_archive_symbols because of a
4950 problem which arises on UnixWare. The UnixWare libc.so is an
4951 archive which includes an entry libc.so.1 which defines a bunch of
4952 symbols. The libc.so archive also includes a number of other
4953 object files, which also define symbols, some of which are the same
4954 as those defined in libc.so.1. Correct linking requires that we
4955 consider each object file in turn, and include it if it defines any
4956 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4957 this; it looks through the list of undefined symbols, and includes
4958 any object file which defines them. When this algorithm is used on
4959 UnixWare, it winds up pulling in libc.so.1 early and defining a
4960 bunch of symbols. This means that some of the other objects in the
4961 archive are not included in the link, which is incorrect since they
4962 precede libc.so.1 in the archive.
4963
4964 Fortunately, ELF archive handling is simpler than that done by
4965 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4966 oddities. In ELF, if we find a symbol in the archive map, and the
4967 symbol is currently undefined, we know that we must pull in that
4968 object file.
4969
4970 Unfortunately, we do have to make multiple passes over the symbol
4971 table until nothing further is resolved. */
4972
4ad4eba5
AM
4973static bfd_boolean
4974elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4975{
4976 symindex c;
4977 bfd_boolean *defined = NULL;
4978 bfd_boolean *included = NULL;
4979 carsym *symdefs;
4980 bfd_boolean loop;
4981 bfd_size_type amt;
8387904d
AM
4982 const struct elf_backend_data *bed;
4983 struct elf_link_hash_entry * (*archive_symbol_lookup)
4984 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4985
4986 if (! bfd_has_map (abfd))
4987 {
4988 /* An empty archive is a special case. */
4989 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4990 return TRUE;
4991 bfd_set_error (bfd_error_no_armap);
4992 return FALSE;
4993 }
4994
4995 /* Keep track of all symbols we know to be already defined, and all
4996 files we know to be already included. This is to speed up the
4997 second and subsequent passes. */
4998 c = bfd_ardata (abfd)->symdef_count;
4999 if (c == 0)
5000 return TRUE;
5001 amt = c;
5002 amt *= sizeof (bfd_boolean);
a50b1753
NC
5003 defined = (bfd_boolean *) bfd_zmalloc (amt);
5004 included = (bfd_boolean *) bfd_zmalloc (amt);
0ad989f9
L
5005 if (defined == NULL || included == NULL)
5006 goto error_return;
5007
5008 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5009 bed = get_elf_backend_data (abfd);
5010 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5011
5012 do
5013 {
5014 file_ptr last;
5015 symindex i;
5016 carsym *symdef;
5017 carsym *symdefend;
5018
5019 loop = FALSE;
5020 last = -1;
5021
5022 symdef = symdefs;
5023 symdefend = symdef + c;
5024 for (i = 0; symdef < symdefend; symdef++, i++)
5025 {
5026 struct elf_link_hash_entry *h;
5027 bfd *element;
5028 struct bfd_link_hash_entry *undefs_tail;
5029 symindex mark;
5030
5031 if (defined[i] || included[i])
5032 continue;
5033 if (symdef->file_offset == last)
5034 {
5035 included[i] = TRUE;
5036 continue;
5037 }
5038
8387904d
AM
5039 h = archive_symbol_lookup (abfd, info, symdef->name);
5040 if (h == (struct elf_link_hash_entry *) 0 - 1)
5041 goto error_return;
0ad989f9
L
5042
5043 if (h == NULL)
5044 continue;
5045
5046 if (h->root.type == bfd_link_hash_common)
5047 {
5048 /* We currently have a common symbol. The archive map contains
5049 a reference to this symbol, so we may want to include it. We
5050 only want to include it however, if this archive element
5051 contains a definition of the symbol, not just another common
5052 declaration of it.
5053
5054 Unfortunately some archivers (including GNU ar) will put
5055 declarations of common symbols into their archive maps, as
5056 well as real definitions, so we cannot just go by the archive
5057 map alone. Instead we must read in the element's symbol
5058 table and check that to see what kind of symbol definition
5059 this is. */
5060 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5061 continue;
5062 }
5063 else if (h->root.type != bfd_link_hash_undefined)
5064 {
5065 if (h->root.type != bfd_link_hash_undefweak)
5066 defined[i] = TRUE;
5067 continue;
5068 }
5069
5070 /* We need to include this archive member. */
5071 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5072 if (element == NULL)
5073 goto error_return;
5074
5075 if (! bfd_check_format (element, bfd_object))
5076 goto error_return;
5077
5078 /* Doublecheck that we have not included this object
5079 already--it should be impossible, but there may be
5080 something wrong with the archive. */
5081 if (element->archive_pass != 0)
5082 {
5083 bfd_set_error (bfd_error_bad_value);
5084 goto error_return;
5085 }
5086 element->archive_pass = 1;
5087
5088 undefs_tail = info->hash->undefs_tail;
5089
0e144ba7
AM
5090 if (!(*info->callbacks
5091 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5092 goto error_return;
0e144ba7 5093 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5094 goto error_return;
5095
5096 /* If there are any new undefined symbols, we need to make
5097 another pass through the archive in order to see whether
5098 they can be defined. FIXME: This isn't perfect, because
5099 common symbols wind up on undefs_tail and because an
5100 undefined symbol which is defined later on in this pass
5101 does not require another pass. This isn't a bug, but it
5102 does make the code less efficient than it could be. */
5103 if (undefs_tail != info->hash->undefs_tail)
5104 loop = TRUE;
5105
5106 /* Look backward to mark all symbols from this object file
5107 which we have already seen in this pass. */
5108 mark = i;
5109 do
5110 {
5111 included[mark] = TRUE;
5112 if (mark == 0)
5113 break;
5114 --mark;
5115 }
5116 while (symdefs[mark].file_offset == symdef->file_offset);
5117
5118 /* We mark subsequent symbols from this object file as we go
5119 on through the loop. */
5120 last = symdef->file_offset;
5121 }
5122 }
5123 while (loop);
5124
5125 free (defined);
5126 free (included);
5127
5128 return TRUE;
5129
5130 error_return:
5131 if (defined != NULL)
5132 free (defined);
5133 if (included != NULL)
5134 free (included);
5135 return FALSE;
5136}
4ad4eba5
AM
5137
5138/* Given an ELF BFD, add symbols to the global hash table as
5139 appropriate. */
5140
5141bfd_boolean
5142bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5143{
5144 switch (bfd_get_format (abfd))
5145 {
5146 case bfd_object:
5147 return elf_link_add_object_symbols (abfd, info);
5148 case bfd_archive:
5149 return elf_link_add_archive_symbols (abfd, info);
5150 default:
5151 bfd_set_error (bfd_error_wrong_format);
5152 return FALSE;
5153 }
5154}
5a580b3a 5155\f
14b1c01e
AM
5156struct hash_codes_info
5157{
5158 unsigned long *hashcodes;
5159 bfd_boolean error;
5160};
a0c8462f 5161
5a580b3a
AM
5162/* This function will be called though elf_link_hash_traverse to store
5163 all hash value of the exported symbols in an array. */
5164
5165static bfd_boolean
5166elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5167{
a50b1753 5168 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a
AM
5169 const char *name;
5170 char *p;
5171 unsigned long ha;
5172 char *alc = NULL;
5173
5174 if (h->root.type == bfd_link_hash_warning)
5175 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5176
5177 /* Ignore indirect symbols. These are added by the versioning code. */
5178 if (h->dynindx == -1)
5179 return TRUE;
5180
5181 name = h->root.root.string;
5182 p = strchr (name, ELF_VER_CHR);
5183 if (p != NULL)
5184 {
a50b1753 5185 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5186 if (alc == NULL)
5187 {
5188 inf->error = TRUE;
5189 return FALSE;
5190 }
5a580b3a
AM
5191 memcpy (alc, name, p - name);
5192 alc[p - name] = '\0';
5193 name = alc;
5194 }
5195
5196 /* Compute the hash value. */
5197 ha = bfd_elf_hash (name);
5198
5199 /* Store the found hash value in the array given as the argument. */
14b1c01e 5200 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5201
5202 /* And store it in the struct so that we can put it in the hash table
5203 later. */
f6e332e6 5204 h->u.elf_hash_value = ha;
5a580b3a
AM
5205
5206 if (alc != NULL)
5207 free (alc);
5208
5209 return TRUE;
5210}
5211
fdc90cb4
JJ
5212struct collect_gnu_hash_codes
5213{
5214 bfd *output_bfd;
5215 const struct elf_backend_data *bed;
5216 unsigned long int nsyms;
5217 unsigned long int maskbits;
5218 unsigned long int *hashcodes;
5219 unsigned long int *hashval;
5220 unsigned long int *indx;
5221 unsigned long int *counts;
5222 bfd_vma *bitmask;
5223 bfd_byte *contents;
5224 long int min_dynindx;
5225 unsigned long int bucketcount;
5226 unsigned long int symindx;
5227 long int local_indx;
5228 long int shift1, shift2;
5229 unsigned long int mask;
14b1c01e 5230 bfd_boolean error;
fdc90cb4
JJ
5231};
5232
5233/* This function will be called though elf_link_hash_traverse to store
5234 all hash value of the exported symbols in an array. */
5235
5236static bfd_boolean
5237elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5238{
a50b1753 5239 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5240 const char *name;
5241 char *p;
5242 unsigned long ha;
5243 char *alc = NULL;
5244
5245 if (h->root.type == bfd_link_hash_warning)
5246 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5247
5248 /* Ignore indirect symbols. These are added by the versioning code. */
5249 if (h->dynindx == -1)
5250 return TRUE;
5251
5252 /* Ignore also local symbols and undefined symbols. */
5253 if (! (*s->bed->elf_hash_symbol) (h))
5254 return TRUE;
5255
5256 name = h->root.root.string;
5257 p = strchr (name, ELF_VER_CHR);
5258 if (p != NULL)
5259 {
a50b1753 5260 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5261 if (alc == NULL)
5262 {
5263 s->error = TRUE;
5264 return FALSE;
5265 }
fdc90cb4
JJ
5266 memcpy (alc, name, p - name);
5267 alc[p - name] = '\0';
5268 name = alc;
5269 }
5270
5271 /* Compute the hash value. */
5272 ha = bfd_elf_gnu_hash (name);
5273
5274 /* Store the found hash value in the array for compute_bucket_count,
5275 and also for .dynsym reordering purposes. */
5276 s->hashcodes[s->nsyms] = ha;
5277 s->hashval[h->dynindx] = ha;
5278 ++s->nsyms;
5279 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5280 s->min_dynindx = h->dynindx;
5281
5282 if (alc != NULL)
5283 free (alc);
5284
5285 return TRUE;
5286}
5287
5288/* This function will be called though elf_link_hash_traverse to do
5289 final dynaminc symbol renumbering. */
5290
5291static bfd_boolean
5292elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5293{
a50b1753 5294 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5295 unsigned long int bucket;
5296 unsigned long int val;
5297
5298 if (h->root.type == bfd_link_hash_warning)
5299 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5300
5301 /* Ignore indirect symbols. */
5302 if (h->dynindx == -1)
5303 return TRUE;
5304
5305 /* Ignore also local symbols and undefined symbols. */
5306 if (! (*s->bed->elf_hash_symbol) (h))
5307 {
5308 if (h->dynindx >= s->min_dynindx)
5309 h->dynindx = s->local_indx++;
5310 return TRUE;
5311 }
5312
5313 bucket = s->hashval[h->dynindx] % s->bucketcount;
5314 val = (s->hashval[h->dynindx] >> s->shift1)
5315 & ((s->maskbits >> s->shift1) - 1);
5316 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5317 s->bitmask[val]
5318 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5319 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5320 if (s->counts[bucket] == 1)
5321 /* Last element terminates the chain. */
5322 val |= 1;
5323 bfd_put_32 (s->output_bfd, val,
5324 s->contents + (s->indx[bucket] - s->symindx) * 4);
5325 --s->counts[bucket];
5326 h->dynindx = s->indx[bucket]++;
5327 return TRUE;
5328}
5329
5330/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5331
5332bfd_boolean
5333_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5334{
5335 return !(h->forced_local
5336 || h->root.type == bfd_link_hash_undefined
5337 || h->root.type == bfd_link_hash_undefweak
5338 || ((h->root.type == bfd_link_hash_defined
5339 || h->root.type == bfd_link_hash_defweak)
5340 && h->root.u.def.section->output_section == NULL));
5341}
5342
5a580b3a
AM
5343/* Array used to determine the number of hash table buckets to use
5344 based on the number of symbols there are. If there are fewer than
5345 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5346 fewer than 37 we use 17 buckets, and so forth. We never use more
5347 than 32771 buckets. */
5348
5349static const size_t elf_buckets[] =
5350{
5351 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5352 16411, 32771, 0
5353};
5354
5355/* Compute bucket count for hashing table. We do not use a static set
5356 of possible tables sizes anymore. Instead we determine for all
5357 possible reasonable sizes of the table the outcome (i.e., the
5358 number of collisions etc) and choose the best solution. The
5359 weighting functions are not too simple to allow the table to grow
5360 without bounds. Instead one of the weighting factors is the size.
5361 Therefore the result is always a good payoff between few collisions
5362 (= short chain lengths) and table size. */
5363static size_t
b20dd2ce 5364compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5365 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5366 unsigned long int nsyms,
5367 int gnu_hash)
5a580b3a 5368{
5a580b3a 5369 size_t best_size = 0;
5a580b3a 5370 unsigned long int i;
5a580b3a 5371
5a580b3a
AM
5372 /* We have a problem here. The following code to optimize the table
5373 size requires an integer type with more the 32 bits. If
5374 BFD_HOST_U_64_BIT is set we know about such a type. */
5375#ifdef BFD_HOST_U_64_BIT
5376 if (info->optimize)
5377 {
5a580b3a
AM
5378 size_t minsize;
5379 size_t maxsize;
5380 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5381 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5382 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5383 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5384 unsigned long int *counts;
d40f3da9 5385 bfd_size_type amt;
0883b6e0 5386 unsigned int no_improvement_count = 0;
5a580b3a
AM
5387
5388 /* Possible optimization parameters: if we have NSYMS symbols we say
5389 that the hashing table must at least have NSYMS/4 and at most
5390 2*NSYMS buckets. */
5391 minsize = nsyms / 4;
5392 if (minsize == 0)
5393 minsize = 1;
5394 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5395 if (gnu_hash)
5396 {
5397 if (minsize < 2)
5398 minsize = 2;
5399 if ((best_size & 31) == 0)
5400 ++best_size;
5401 }
5a580b3a
AM
5402
5403 /* Create array where we count the collisions in. We must use bfd_malloc
5404 since the size could be large. */
5405 amt = maxsize;
5406 amt *= sizeof (unsigned long int);
a50b1753 5407 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5408 if (counts == NULL)
fdc90cb4 5409 return 0;
5a580b3a
AM
5410
5411 /* Compute the "optimal" size for the hash table. The criteria is a
5412 minimal chain length. The minor criteria is (of course) the size
5413 of the table. */
5414 for (i = minsize; i < maxsize; ++i)
5415 {
5416 /* Walk through the array of hashcodes and count the collisions. */
5417 BFD_HOST_U_64_BIT max;
5418 unsigned long int j;
5419 unsigned long int fact;
5420
fdc90cb4
JJ
5421 if (gnu_hash && (i & 31) == 0)
5422 continue;
5423
5a580b3a
AM
5424 memset (counts, '\0', i * sizeof (unsigned long int));
5425
5426 /* Determine how often each hash bucket is used. */
5427 for (j = 0; j < nsyms; ++j)
5428 ++counts[hashcodes[j] % i];
5429
5430 /* For the weight function we need some information about the
5431 pagesize on the target. This is information need not be 100%
5432 accurate. Since this information is not available (so far) we
5433 define it here to a reasonable default value. If it is crucial
5434 to have a better value some day simply define this value. */
5435# ifndef BFD_TARGET_PAGESIZE
5436# define BFD_TARGET_PAGESIZE (4096)
5437# endif
5438
fdc90cb4
JJ
5439 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5440 and the chains. */
5441 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5442
5443# if 1
5444 /* Variant 1: optimize for short chains. We add the squares
5445 of all the chain lengths (which favors many small chain
5446 over a few long chains). */
5447 for (j = 0; j < i; ++j)
5448 max += counts[j] * counts[j];
5449
5450 /* This adds penalties for the overall size of the table. */
fdc90cb4 5451 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5452 max *= fact * fact;
5453# else
5454 /* Variant 2: Optimize a lot more for small table. Here we
5455 also add squares of the size but we also add penalties for
5456 empty slots (the +1 term). */
5457 for (j = 0; j < i; ++j)
5458 max += (1 + counts[j]) * (1 + counts[j]);
5459
5460 /* The overall size of the table is considered, but not as
5461 strong as in variant 1, where it is squared. */
fdc90cb4 5462 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5463 max *= fact;
5464# endif
5465
5466 /* Compare with current best results. */
5467 if (max < best_chlen)
5468 {
5469 best_chlen = max;
5470 best_size = i;
0883b6e0 5471 no_improvement_count = 0;
5a580b3a 5472 }
0883b6e0
NC
5473 /* PR 11843: Avoid futile long searches for the best bucket size
5474 when there are a large number of symbols. */
5475 else if (++no_improvement_count == 100)
5476 break;
5a580b3a
AM
5477 }
5478
5479 free (counts);
5480 }
5481 else
5482#endif /* defined (BFD_HOST_U_64_BIT) */
5483 {
5484 /* This is the fallback solution if no 64bit type is available or if we
5485 are not supposed to spend much time on optimizations. We select the
5486 bucket count using a fixed set of numbers. */
5487 for (i = 0; elf_buckets[i] != 0; i++)
5488 {
5489 best_size = elf_buckets[i];
fdc90cb4 5490 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5491 break;
5492 }
fdc90cb4
JJ
5493 if (gnu_hash && best_size < 2)
5494 best_size = 2;
5a580b3a
AM
5495 }
5496
5a580b3a
AM
5497 return best_size;
5498}
5499
d0bf826b
AM
5500/* Size any SHT_GROUP section for ld -r. */
5501
5502bfd_boolean
5503_bfd_elf_size_group_sections (struct bfd_link_info *info)
5504{
5505 bfd *ibfd;
5506
5507 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5508 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5509 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5510 return FALSE;
5511 return TRUE;
5512}
5513
5a580b3a
AM
5514/* Set up the sizes and contents of the ELF dynamic sections. This is
5515 called by the ELF linker emulation before_allocation routine. We
5516 must set the sizes of the sections before the linker sets the
5517 addresses of the various sections. */
5518
5519bfd_boolean
5520bfd_elf_size_dynamic_sections (bfd *output_bfd,
5521 const char *soname,
5522 const char *rpath,
5523 const char *filter_shlib,
7ee314fa
AM
5524 const char *audit,
5525 const char *depaudit,
5a580b3a
AM
5526 const char * const *auxiliary_filters,
5527 struct bfd_link_info *info,
5528 asection **sinterpptr,
5529 struct bfd_elf_version_tree *verdefs)
5530{
5531 bfd_size_type soname_indx;
5532 bfd *dynobj;
5533 const struct elf_backend_data *bed;
28caa186 5534 struct elf_info_failed asvinfo;
5a580b3a
AM
5535
5536 *sinterpptr = NULL;
5537
5538 soname_indx = (bfd_size_type) -1;
5539
5540 if (!is_elf_hash_table (info->hash))
5541 return TRUE;
5542
6bfdb61b 5543 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5544 if (info->execstack)
5545 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5546 else if (info->noexecstack)
5547 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5548 else
5549 {
5550 bfd *inputobj;
5551 asection *notesec = NULL;
5552 int exec = 0;
5553
5554 for (inputobj = info->input_bfds;
5555 inputobj;
5556 inputobj = inputobj->link_next)
5557 {
5558 asection *s;
5559
a94b9d2d 5560 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5a580b3a
AM
5561 continue;
5562 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5563 if (s)
5564 {
5565 if (s->flags & SEC_CODE)
5566 exec = PF_X;
5567 notesec = s;
5568 }
6bfdb61b 5569 else if (bed->default_execstack)
5a580b3a
AM
5570 exec = PF_X;
5571 }
5572 if (notesec)
5573 {
5574 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5575 if (exec && info->relocatable
5576 && notesec->output_section != bfd_abs_section_ptr)
5577 notesec->output_section->flags |= SEC_CODE;
5578 }
5579 }
5580
5581 /* Any syms created from now on start with -1 in
5582 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5583 elf_hash_table (info)->init_got_refcount
5584 = elf_hash_table (info)->init_got_offset;
5585 elf_hash_table (info)->init_plt_refcount
5586 = elf_hash_table (info)->init_plt_offset;
5a580b3a 5587
d0bf826b
AM
5588 if (info->relocatable
5589 && !_bfd_elf_size_group_sections (info))
5590 return FALSE;
5591
5a580b3a
AM
5592 /* The backend may have to create some sections regardless of whether
5593 we're dynamic or not. */
5a580b3a
AM
5594 if (bed->elf_backend_always_size_sections
5595 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5596 return FALSE;
5597
eb3d5f3b
JB
5598 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5599 return FALSE;
5600
5a580b3a
AM
5601 dynobj = elf_hash_table (info)->dynobj;
5602
5603 /* If there were no dynamic objects in the link, there is nothing to
5604 do here. */
5605 if (dynobj == NULL)
5606 return TRUE;
5607
5a580b3a
AM
5608 if (elf_hash_table (info)->dynamic_sections_created)
5609 {
5610 struct elf_info_failed eif;
5611 struct elf_link_hash_entry *h;
5612 asection *dynstr;
5613 struct bfd_elf_version_tree *t;
5614 struct bfd_elf_version_expr *d;
046183de 5615 asection *s;
5a580b3a
AM
5616 bfd_boolean all_defined;
5617
5618 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5619 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5620
5621 if (soname != NULL)
5622 {
5623 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5624 soname, TRUE);
5625 if (soname_indx == (bfd_size_type) -1
5626 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5627 return FALSE;
5628 }
5629
5630 if (info->symbolic)
5631 {
5632 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5633 return FALSE;
5634 info->flags |= DF_SYMBOLIC;
5635 }
5636
5637 if (rpath != NULL)
5638 {
5639 bfd_size_type indx;
5640
5641 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5642 TRUE);
5643 if (indx == (bfd_size_type) -1
5644 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5645 return FALSE;
5646
5647 if (info->new_dtags)
5648 {
5649 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5650 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5651 return FALSE;
5652 }
5653 }
5654
5655 if (filter_shlib != NULL)
5656 {
5657 bfd_size_type indx;
5658
5659 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5660 filter_shlib, TRUE);
5661 if (indx == (bfd_size_type) -1
5662 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5663 return FALSE;
5664 }
5665
5666 if (auxiliary_filters != NULL)
5667 {
5668 const char * const *p;
5669
5670 for (p = auxiliary_filters; *p != NULL; p++)
5671 {
5672 bfd_size_type indx;
5673
5674 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5675 *p, TRUE);
5676 if (indx == (bfd_size_type) -1
5677 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5678 return FALSE;
5679 }
5680 }
5681
7ee314fa
AM
5682 if (audit != NULL)
5683 {
5684 bfd_size_type indx;
5685
5686 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5687 TRUE);
5688 if (indx == (bfd_size_type) -1
5689 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5690 return FALSE;
5691 }
5692
5693 if (depaudit != NULL)
5694 {
5695 bfd_size_type indx;
5696
5697 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5698 TRUE);
5699 if (indx == (bfd_size_type) -1
5700 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5701 return FALSE;
5702 }
5703
5a580b3a
AM
5704 eif.info = info;
5705 eif.verdefs = verdefs;
5706 eif.failed = FALSE;
5707
5708 /* If we are supposed to export all symbols into the dynamic symbol
5709 table (this is not the normal case), then do so. */
55255dae
L
5710 if (info->export_dynamic
5711 || (info->executable && info->dynamic))
5a580b3a
AM
5712 {
5713 elf_link_hash_traverse (elf_hash_table (info),
5714 _bfd_elf_export_symbol,
5715 &eif);
5716 if (eif.failed)
5717 return FALSE;
5718 }
5719
5720 /* Make all global versions with definition. */
5721 for (t = verdefs; t != NULL; t = t->next)
5722 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5723 if (!d->symver && d->literal)
5a580b3a
AM
5724 {
5725 const char *verstr, *name;
5726 size_t namelen, verlen, newlen;
93252b1c 5727 char *newname, *p, leading_char;
5a580b3a
AM
5728 struct elf_link_hash_entry *newh;
5729
93252b1c 5730 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5731 name = d->pattern;
93252b1c 5732 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5733 verstr = t->name;
5734 verlen = strlen (verstr);
5735 newlen = namelen + verlen + 3;
5736
a50b1753 5737 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5738 if (newname == NULL)
5739 return FALSE;
93252b1c
MF
5740 newname[0] = leading_char;
5741 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5742
5743 /* Check the hidden versioned definition. */
5744 p = newname + namelen;
5745 *p++ = ELF_VER_CHR;
5746 memcpy (p, verstr, verlen + 1);
5747 newh = elf_link_hash_lookup (elf_hash_table (info),
5748 newname, FALSE, FALSE,
5749 FALSE);
5750 if (newh == NULL
5751 || (newh->root.type != bfd_link_hash_defined
5752 && newh->root.type != bfd_link_hash_defweak))
5753 {
5754 /* Check the default versioned definition. */
5755 *p++ = ELF_VER_CHR;
5756 memcpy (p, verstr, verlen + 1);
5757 newh = elf_link_hash_lookup (elf_hash_table (info),
5758 newname, FALSE, FALSE,
5759 FALSE);
5760 }
5761 free (newname);
5762
5763 /* Mark this version if there is a definition and it is
5764 not defined in a shared object. */
5765 if (newh != NULL
f5385ebf 5766 && !newh->def_dynamic
5a580b3a
AM
5767 && (newh->root.type == bfd_link_hash_defined
5768 || newh->root.type == bfd_link_hash_defweak))
5769 d->symver = 1;
5770 }
5771
5772 /* Attach all the symbols to their version information. */
5a580b3a
AM
5773 asvinfo.info = info;
5774 asvinfo.verdefs = verdefs;
5775 asvinfo.failed = FALSE;
5776
5777 elf_link_hash_traverse (elf_hash_table (info),
5778 _bfd_elf_link_assign_sym_version,
5779 &asvinfo);
5780 if (asvinfo.failed)
5781 return FALSE;
5782
5783 if (!info->allow_undefined_version)
5784 {
5785 /* Check if all global versions have a definition. */
5786 all_defined = TRUE;
5787 for (t = verdefs; t != NULL; t = t->next)
5788 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5789 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5790 {
5791 (*_bfd_error_handler)
5792 (_("%s: undefined version: %s"),
5793 d->pattern, t->name);
5794 all_defined = FALSE;
5795 }
5796
5797 if (!all_defined)
5798 {
5799 bfd_set_error (bfd_error_bad_value);
5800 return FALSE;
5801 }
5802 }
5803
5804 /* Find all symbols which were defined in a dynamic object and make
5805 the backend pick a reasonable value for them. */
5806 elf_link_hash_traverse (elf_hash_table (info),
5807 _bfd_elf_adjust_dynamic_symbol,
5808 &eif);
5809 if (eif.failed)
5810 return FALSE;
5811
5812 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5813 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5814 now so that we know the final size of the .dynamic section. */
5815
5816 /* If there are initialization and/or finalization functions to
5817 call then add the corresponding DT_INIT/DT_FINI entries. */
5818 h = (info->init_function
5819 ? elf_link_hash_lookup (elf_hash_table (info),
5820 info->init_function, FALSE,
5821 FALSE, FALSE)
5822 : NULL);
5823 if (h != NULL
f5385ebf
AM
5824 && (h->ref_regular
5825 || h->def_regular))
5a580b3a
AM
5826 {
5827 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5828 return FALSE;
5829 }
5830 h = (info->fini_function
5831 ? elf_link_hash_lookup (elf_hash_table (info),
5832 info->fini_function, FALSE,
5833 FALSE, FALSE)
5834 : NULL);
5835 if (h != NULL
f5385ebf
AM
5836 && (h->ref_regular
5837 || h->def_regular))
5a580b3a
AM
5838 {
5839 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5840 return FALSE;
5841 }
5842
046183de
AM
5843 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5844 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5845 {
5846 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5847 if (! info->executable)
5848 {
5849 bfd *sub;
5850 asection *o;
5851
5852 for (sub = info->input_bfds; sub != NULL;
5853 sub = sub->link_next)
3fcd97f1
JJ
5854 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5855 for (o = sub->sections; o != NULL; o = o->next)
5856 if (elf_section_data (o)->this_hdr.sh_type
5857 == SHT_PREINIT_ARRAY)
5858 {
5859 (*_bfd_error_handler)
5860 (_("%B: .preinit_array section is not allowed in DSO"),
5861 sub);
5862 break;
5863 }
5a580b3a
AM
5864
5865 bfd_set_error (bfd_error_nonrepresentable_section);
5866 return FALSE;
5867 }
5868
5869 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5870 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5871 return FALSE;
5872 }
046183de
AM
5873 s = bfd_get_section_by_name (output_bfd, ".init_array");
5874 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5875 {
5876 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5877 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5878 return FALSE;
5879 }
046183de
AM
5880 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5881 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5882 {
5883 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5884 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5885 return FALSE;
5886 }
5887
5888 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5889 /* If .dynstr is excluded from the link, we don't want any of
5890 these tags. Strictly, we should be checking each section
5891 individually; This quick check covers for the case where
5892 someone does a /DISCARD/ : { *(*) }. */
5893 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5894 {
5895 bfd_size_type strsize;
5896
5897 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5898 if ((info->emit_hash
5899 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5900 || (info->emit_gnu_hash
5901 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5902 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5903 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5904 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5905 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5906 bed->s->sizeof_sym))
5907 return FALSE;
5908 }
5909 }
5910
5911 /* The backend must work out the sizes of all the other dynamic
5912 sections. */
5913 if (bed->elf_backend_size_dynamic_sections
5914 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5915 return FALSE;
5916
5917 if (elf_hash_table (info)->dynamic_sections_created)
5918 {
554220db 5919 unsigned long section_sym_count;
5a580b3a 5920 asection *s;
5a580b3a
AM
5921
5922 /* Set up the version definition section. */
5923 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5924 BFD_ASSERT (s != NULL);
5925
5926 /* We may have created additional version definitions if we are
5927 just linking a regular application. */
5928 verdefs = asvinfo.verdefs;
5929
5930 /* Skip anonymous version tag. */
5931 if (verdefs != NULL && verdefs->vernum == 0)
5932 verdefs = verdefs->next;
5933
3e3b46e5 5934 if (verdefs == NULL && !info->create_default_symver)
8423293d 5935 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5936 else
5937 {
5938 unsigned int cdefs;
5939 bfd_size_type size;
5940 struct bfd_elf_version_tree *t;
5941 bfd_byte *p;
5942 Elf_Internal_Verdef def;
5943 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5944 struct bfd_link_hash_entry *bh;
5945 struct elf_link_hash_entry *h;
5946 const char *name;
5a580b3a
AM
5947
5948 cdefs = 0;
5949 size = 0;
5950
5951 /* Make space for the base version. */
5952 size += sizeof (Elf_External_Verdef);
5953 size += sizeof (Elf_External_Verdaux);
5954 ++cdefs;
5955
3e3b46e5
PB
5956 /* Make space for the default version. */
5957 if (info->create_default_symver)
5958 {
5959 size += sizeof (Elf_External_Verdef);
5960 ++cdefs;
5961 }
5962
5a580b3a
AM
5963 for (t = verdefs; t != NULL; t = t->next)
5964 {
5965 struct bfd_elf_version_deps *n;
5966
a6cc6b3b
RO
5967 /* Don't emit base version twice. */
5968 if (t->vernum == 0)
5969 continue;
5970
5a580b3a
AM
5971 size += sizeof (Elf_External_Verdef);
5972 size += sizeof (Elf_External_Verdaux);
5973 ++cdefs;
5974
5975 for (n = t->deps; n != NULL; n = n->next)
5976 size += sizeof (Elf_External_Verdaux);
5977 }
5978
eea6121a 5979 s->size = size;
a50b1753 5980 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 5981 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5982 return FALSE;
5983
5984 /* Fill in the version definition section. */
5985
5986 p = s->contents;
5987
5988 def.vd_version = VER_DEF_CURRENT;
5989 def.vd_flags = VER_FLG_BASE;
5990 def.vd_ndx = 1;
5991 def.vd_cnt = 1;
3e3b46e5
PB
5992 if (info->create_default_symver)
5993 {
5994 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5995 def.vd_next = sizeof (Elf_External_Verdef);
5996 }
5997 else
5998 {
5999 def.vd_aux = sizeof (Elf_External_Verdef);
6000 def.vd_next = (sizeof (Elf_External_Verdef)
6001 + sizeof (Elf_External_Verdaux));
6002 }
5a580b3a
AM
6003
6004 if (soname_indx != (bfd_size_type) -1)
6005 {
6006 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6007 soname_indx);
6008 def.vd_hash = bfd_elf_hash (soname);
6009 defaux.vda_name = soname_indx;
3e3b46e5 6010 name = soname;
5a580b3a
AM
6011 }
6012 else
6013 {
5a580b3a
AM
6014 bfd_size_type indx;
6015
06084812 6016 name = lbasename (output_bfd->filename);
5a580b3a
AM
6017 def.vd_hash = bfd_elf_hash (name);
6018 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6019 name, FALSE);
6020 if (indx == (bfd_size_type) -1)
6021 return FALSE;
6022 defaux.vda_name = indx;
6023 }
6024 defaux.vda_next = 0;
6025
6026 _bfd_elf_swap_verdef_out (output_bfd, &def,
6027 (Elf_External_Verdef *) p);
6028 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6029 if (info->create_default_symver)
6030 {
6031 /* Add a symbol representing this version. */
6032 bh = NULL;
6033 if (! (_bfd_generic_link_add_one_symbol
6034 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6035 0, NULL, FALSE,
6036 get_elf_backend_data (dynobj)->collect, &bh)))
6037 return FALSE;
6038 h = (struct elf_link_hash_entry *) bh;
6039 h->non_elf = 0;
6040 h->def_regular = 1;
6041 h->type = STT_OBJECT;
6042 h->verinfo.vertree = NULL;
6043
6044 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6045 return FALSE;
6046
6047 /* Create a duplicate of the base version with the same
6048 aux block, but different flags. */
6049 def.vd_flags = 0;
6050 def.vd_ndx = 2;
6051 def.vd_aux = sizeof (Elf_External_Verdef);
6052 if (verdefs)
6053 def.vd_next = (sizeof (Elf_External_Verdef)
6054 + sizeof (Elf_External_Verdaux));
6055 else
6056 def.vd_next = 0;
6057 _bfd_elf_swap_verdef_out (output_bfd, &def,
6058 (Elf_External_Verdef *) p);
6059 p += sizeof (Elf_External_Verdef);
6060 }
5a580b3a
AM
6061 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6062 (Elf_External_Verdaux *) p);
6063 p += sizeof (Elf_External_Verdaux);
6064
6065 for (t = verdefs; t != NULL; t = t->next)
6066 {
6067 unsigned int cdeps;
6068 struct bfd_elf_version_deps *n;
5a580b3a 6069
a6cc6b3b
RO
6070 /* Don't emit the base version twice. */
6071 if (t->vernum == 0)
6072 continue;
6073
5a580b3a
AM
6074 cdeps = 0;
6075 for (n = t->deps; n != NULL; n = n->next)
6076 ++cdeps;
6077
6078 /* Add a symbol representing this version. */
6079 bh = NULL;
6080 if (! (_bfd_generic_link_add_one_symbol
6081 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6082 0, NULL, FALSE,
6083 get_elf_backend_data (dynobj)->collect, &bh)))
6084 return FALSE;
6085 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6086 h->non_elf = 0;
6087 h->def_regular = 1;
5a580b3a
AM
6088 h->type = STT_OBJECT;
6089 h->verinfo.vertree = t;
6090
c152c796 6091 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6092 return FALSE;
6093
6094 def.vd_version = VER_DEF_CURRENT;
6095 def.vd_flags = 0;
6096 if (t->globals.list == NULL
6097 && t->locals.list == NULL
6098 && ! t->used)
6099 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6100 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6101 def.vd_cnt = cdeps + 1;
6102 def.vd_hash = bfd_elf_hash (t->name);
6103 def.vd_aux = sizeof (Elf_External_Verdef);
6104 def.vd_next = 0;
a6cc6b3b
RO
6105
6106 /* If a basever node is next, it *must* be the last node in
6107 the chain, otherwise Verdef construction breaks. */
6108 if (t->next != NULL && t->next->vernum == 0)
6109 BFD_ASSERT (t->next->next == NULL);
6110
6111 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6112 def.vd_next = (sizeof (Elf_External_Verdef)
6113 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6114
6115 _bfd_elf_swap_verdef_out (output_bfd, &def,
6116 (Elf_External_Verdef *) p);
6117 p += sizeof (Elf_External_Verdef);
6118
6119 defaux.vda_name = h->dynstr_index;
6120 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6121 h->dynstr_index);
6122 defaux.vda_next = 0;
6123 if (t->deps != NULL)
6124 defaux.vda_next = sizeof (Elf_External_Verdaux);
6125 t->name_indx = defaux.vda_name;
6126
6127 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6128 (Elf_External_Verdaux *) p);
6129 p += sizeof (Elf_External_Verdaux);
6130
6131 for (n = t->deps; n != NULL; n = n->next)
6132 {
6133 if (n->version_needed == NULL)
6134 {
6135 /* This can happen if there was an error in the
6136 version script. */
6137 defaux.vda_name = 0;
6138 }
6139 else
6140 {
6141 defaux.vda_name = n->version_needed->name_indx;
6142 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6143 defaux.vda_name);
6144 }
6145 if (n->next == NULL)
6146 defaux.vda_next = 0;
6147 else
6148 defaux.vda_next = sizeof (Elf_External_Verdaux);
6149
6150 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6151 (Elf_External_Verdaux *) p);
6152 p += sizeof (Elf_External_Verdaux);
6153 }
6154 }
6155
6156 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6157 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6158 return FALSE;
6159
6160 elf_tdata (output_bfd)->cverdefs = cdefs;
6161 }
6162
6163 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6164 {
6165 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6166 return FALSE;
6167 }
6168 else if (info->flags & DF_BIND_NOW)
6169 {
6170 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6171 return FALSE;
6172 }
6173
6174 if (info->flags_1)
6175 {
6176 if (info->executable)
6177 info->flags_1 &= ~ (DF_1_INITFIRST
6178 | DF_1_NODELETE
6179 | DF_1_NOOPEN);
6180 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6181 return FALSE;
6182 }
6183
6184 /* Work out the size of the version reference section. */
6185
6186 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6187 BFD_ASSERT (s != NULL);
6188 {
6189 struct elf_find_verdep_info sinfo;
6190
5a580b3a
AM
6191 sinfo.info = info;
6192 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6193 if (sinfo.vers == 0)
6194 sinfo.vers = 1;
6195 sinfo.failed = FALSE;
6196
6197 elf_link_hash_traverse (elf_hash_table (info),
6198 _bfd_elf_link_find_version_dependencies,
6199 &sinfo);
14b1c01e
AM
6200 if (sinfo.failed)
6201 return FALSE;
5a580b3a
AM
6202
6203 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6204 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6205 else
6206 {
6207 Elf_Internal_Verneed *t;
6208 unsigned int size;
6209 unsigned int crefs;
6210 bfd_byte *p;
6211
a6cc6b3b 6212 /* Build the version dependency section. */
5a580b3a
AM
6213 size = 0;
6214 crefs = 0;
6215 for (t = elf_tdata (output_bfd)->verref;
6216 t != NULL;
6217 t = t->vn_nextref)
6218 {
6219 Elf_Internal_Vernaux *a;
6220
6221 size += sizeof (Elf_External_Verneed);
6222 ++crefs;
6223 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6224 size += sizeof (Elf_External_Vernaux);
6225 }
6226
eea6121a 6227 s->size = size;
a50b1753 6228 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6229 if (s->contents == NULL)
6230 return FALSE;
6231
6232 p = s->contents;
6233 for (t = elf_tdata (output_bfd)->verref;
6234 t != NULL;
6235 t = t->vn_nextref)
6236 {
6237 unsigned int caux;
6238 Elf_Internal_Vernaux *a;
6239 bfd_size_type indx;
6240
6241 caux = 0;
6242 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6243 ++caux;
6244
6245 t->vn_version = VER_NEED_CURRENT;
6246 t->vn_cnt = caux;
6247 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6248 elf_dt_name (t->vn_bfd) != NULL
6249 ? elf_dt_name (t->vn_bfd)
06084812 6250 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6251 FALSE);
6252 if (indx == (bfd_size_type) -1)
6253 return FALSE;
6254 t->vn_file = indx;
6255 t->vn_aux = sizeof (Elf_External_Verneed);
6256 if (t->vn_nextref == NULL)
6257 t->vn_next = 0;
6258 else
6259 t->vn_next = (sizeof (Elf_External_Verneed)
6260 + caux * sizeof (Elf_External_Vernaux));
6261
6262 _bfd_elf_swap_verneed_out (output_bfd, t,
6263 (Elf_External_Verneed *) p);
6264 p += sizeof (Elf_External_Verneed);
6265
6266 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6267 {
6268 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6269 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6270 a->vna_nodename, FALSE);
6271 if (indx == (bfd_size_type) -1)
6272 return FALSE;
6273 a->vna_name = indx;
6274 if (a->vna_nextptr == NULL)
6275 a->vna_next = 0;
6276 else
6277 a->vna_next = sizeof (Elf_External_Vernaux);
6278
6279 _bfd_elf_swap_vernaux_out (output_bfd, a,
6280 (Elf_External_Vernaux *) p);
6281 p += sizeof (Elf_External_Vernaux);
6282 }
6283 }
6284
6285 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6286 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6287 return FALSE;
6288
6289 elf_tdata (output_bfd)->cverrefs = crefs;
6290 }
6291 }
6292
8423293d
AM
6293 if ((elf_tdata (output_bfd)->cverrefs == 0
6294 && elf_tdata (output_bfd)->cverdefs == 0)
6295 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6296 &section_sym_count) == 0)
6297 {
6298 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6299 s->flags |= SEC_EXCLUDE;
6300 }
6301 }
6302 return TRUE;
6303}
6304
74541ad4
AM
6305/* Find the first non-excluded output section. We'll use its
6306 section symbol for some emitted relocs. */
6307void
6308_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6309{
6310 asection *s;
6311
6312 for (s = output_bfd->sections; s != NULL; s = s->next)
6313 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6314 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6315 {
6316 elf_hash_table (info)->text_index_section = s;
6317 break;
6318 }
6319}
6320
6321/* Find two non-excluded output sections, one for code, one for data.
6322 We'll use their section symbols for some emitted relocs. */
6323void
6324_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6325{
6326 asection *s;
6327
266b05cf
DJ
6328 /* Data first, since setting text_index_section changes
6329 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6330 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6331 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6332 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6333 {
266b05cf 6334 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6335 break;
6336 }
6337
6338 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6339 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6340 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6341 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6342 {
266b05cf 6343 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6344 break;
6345 }
6346
6347 if (elf_hash_table (info)->text_index_section == NULL)
6348 elf_hash_table (info)->text_index_section
6349 = elf_hash_table (info)->data_index_section;
6350}
6351
8423293d
AM
6352bfd_boolean
6353bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6354{
74541ad4
AM
6355 const struct elf_backend_data *bed;
6356
8423293d
AM
6357 if (!is_elf_hash_table (info->hash))
6358 return TRUE;
6359
74541ad4
AM
6360 bed = get_elf_backend_data (output_bfd);
6361 (*bed->elf_backend_init_index_section) (output_bfd, info);
6362
8423293d
AM
6363 if (elf_hash_table (info)->dynamic_sections_created)
6364 {
6365 bfd *dynobj;
8423293d
AM
6366 asection *s;
6367 bfd_size_type dynsymcount;
6368 unsigned long section_sym_count;
8423293d
AM
6369 unsigned int dtagcount;
6370
6371 dynobj = elf_hash_table (info)->dynobj;
6372
5a580b3a
AM
6373 /* Assign dynsym indicies. In a shared library we generate a
6374 section symbol for each output section, which come first.
6375 Next come all of the back-end allocated local dynamic syms,
6376 followed by the rest of the global symbols. */
6377
554220db
AM
6378 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6379 &section_sym_count);
5a580b3a
AM
6380
6381 /* Work out the size of the symbol version section. */
6382 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6383 BFD_ASSERT (s != NULL);
8423293d
AM
6384 if (dynsymcount != 0
6385 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6386 {
eea6121a 6387 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6388 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6389 if (s->contents == NULL)
6390 return FALSE;
6391
6392 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6393 return FALSE;
6394 }
6395
6396 /* Set the size of the .dynsym and .hash sections. We counted
6397 the number of dynamic symbols in elf_link_add_object_symbols.
6398 We will build the contents of .dynsym and .hash when we build
6399 the final symbol table, because until then we do not know the
6400 correct value to give the symbols. We built the .dynstr
6401 section as we went along in elf_link_add_object_symbols. */
6402 s = bfd_get_section_by_name (dynobj, ".dynsym");
6403 BFD_ASSERT (s != NULL);
eea6121a 6404 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6405
6406 if (dynsymcount != 0)
6407 {
a50b1753 6408 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6409 if (s->contents == NULL)
6410 return FALSE;
5a580b3a 6411
554220db
AM
6412 /* The first entry in .dynsym is a dummy symbol.
6413 Clear all the section syms, in case we don't output them all. */
6414 ++section_sym_count;
6415 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6416 }
6417
fdc90cb4
JJ
6418 elf_hash_table (info)->bucketcount = 0;
6419
5a580b3a
AM
6420 /* Compute the size of the hashing table. As a side effect this
6421 computes the hash values for all the names we export. */
fdc90cb4
JJ
6422 if (info->emit_hash)
6423 {
6424 unsigned long int *hashcodes;
14b1c01e 6425 struct hash_codes_info hashinf;
fdc90cb4
JJ
6426 bfd_size_type amt;
6427 unsigned long int nsyms;
6428 size_t bucketcount;
6429 size_t hash_entry_size;
6430
6431 /* Compute the hash values for all exported symbols. At the same
6432 time store the values in an array so that we could use them for
6433 optimizations. */
6434 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6435 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6436 if (hashcodes == NULL)
6437 return FALSE;
14b1c01e
AM
6438 hashinf.hashcodes = hashcodes;
6439 hashinf.error = FALSE;
5a580b3a 6440
fdc90cb4
JJ
6441 /* Put all hash values in HASHCODES. */
6442 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6443 elf_collect_hash_codes, &hashinf);
6444 if (hashinf.error)
4dd07732
AM
6445 {
6446 free (hashcodes);
6447 return FALSE;
6448 }
5a580b3a 6449
14b1c01e 6450 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6451 bucketcount
6452 = compute_bucket_count (info, hashcodes, nsyms, 0);
6453 free (hashcodes);
6454
6455 if (bucketcount == 0)
6456 return FALSE;
5a580b3a 6457
fdc90cb4
JJ
6458 elf_hash_table (info)->bucketcount = bucketcount;
6459
6460 s = bfd_get_section_by_name (dynobj, ".hash");
6461 BFD_ASSERT (s != NULL);
6462 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6463 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6464 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6465 if (s->contents == NULL)
6466 return FALSE;
6467
6468 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6469 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6470 s->contents + hash_entry_size);
6471 }
6472
6473 if (info->emit_gnu_hash)
6474 {
6475 size_t i, cnt;
6476 unsigned char *contents;
6477 struct collect_gnu_hash_codes cinfo;
6478 bfd_size_type amt;
6479 size_t bucketcount;
6480
6481 memset (&cinfo, 0, sizeof (cinfo));
6482
6483 /* Compute the hash values for all exported symbols. At the same
6484 time store the values in an array so that we could use them for
6485 optimizations. */
6486 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6487 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6488 if (cinfo.hashcodes == NULL)
6489 return FALSE;
6490
6491 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6492 cinfo.min_dynindx = -1;
6493 cinfo.output_bfd = output_bfd;
6494 cinfo.bed = bed;
6495
6496 /* Put all hash values in HASHCODES. */
6497 elf_link_hash_traverse (elf_hash_table (info),
6498 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6499 if (cinfo.error)
4dd07732
AM
6500 {
6501 free (cinfo.hashcodes);
6502 return FALSE;
6503 }
fdc90cb4
JJ
6504
6505 bucketcount
6506 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6507
6508 if (bucketcount == 0)
6509 {
6510 free (cinfo.hashcodes);
6511 return FALSE;
6512 }
6513
6514 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6515 BFD_ASSERT (s != NULL);
6516
6517 if (cinfo.nsyms == 0)
6518 {
6519 /* Empty .gnu.hash section is special. */
6520 BFD_ASSERT (cinfo.min_dynindx == -1);
6521 free (cinfo.hashcodes);
6522 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6523 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6524 if (contents == NULL)
6525 return FALSE;
6526 s->contents = contents;
6527 /* 1 empty bucket. */
6528 bfd_put_32 (output_bfd, 1, contents);
6529 /* SYMIDX above the special symbol 0. */
6530 bfd_put_32 (output_bfd, 1, contents + 4);
6531 /* Just one word for bitmask. */
6532 bfd_put_32 (output_bfd, 1, contents + 8);
6533 /* Only hash fn bloom filter. */
6534 bfd_put_32 (output_bfd, 0, contents + 12);
6535 /* No hashes are valid - empty bitmask. */
6536 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6537 /* No hashes in the only bucket. */
6538 bfd_put_32 (output_bfd, 0,
6539 contents + 16 + bed->s->arch_size / 8);
6540 }
6541 else
6542 {
9e6619e2 6543 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6544 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6545
9e6619e2
AM
6546 x = cinfo.nsyms;
6547 maskbitslog2 = 1;
6548 while ((x >>= 1) != 0)
6549 ++maskbitslog2;
fdc90cb4
JJ
6550 if (maskbitslog2 < 3)
6551 maskbitslog2 = 5;
6552 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6553 maskbitslog2 = maskbitslog2 + 3;
6554 else
6555 maskbitslog2 = maskbitslog2 + 2;
6556 if (bed->s->arch_size == 64)
6557 {
6558 if (maskbitslog2 == 5)
6559 maskbitslog2 = 6;
6560 cinfo.shift1 = 6;
6561 }
6562 else
6563 cinfo.shift1 = 5;
6564 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6565 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6566 cinfo.maskbits = 1 << maskbitslog2;
6567 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6568 amt = bucketcount * sizeof (unsigned long int) * 2;
6569 amt += maskwords * sizeof (bfd_vma);
a50b1753 6570 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6571 if (cinfo.bitmask == NULL)
6572 {
6573 free (cinfo.hashcodes);
6574 return FALSE;
6575 }
6576
a50b1753 6577 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6578 cinfo.indx = cinfo.counts + bucketcount;
6579 cinfo.symindx = dynsymcount - cinfo.nsyms;
6580 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6581
6582 /* Determine how often each hash bucket is used. */
6583 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6584 for (i = 0; i < cinfo.nsyms; ++i)
6585 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6586
6587 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6588 if (cinfo.counts[i] != 0)
6589 {
6590 cinfo.indx[i] = cnt;
6591 cnt += cinfo.counts[i];
6592 }
6593 BFD_ASSERT (cnt == dynsymcount);
6594 cinfo.bucketcount = bucketcount;
6595 cinfo.local_indx = cinfo.min_dynindx;
6596
6597 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6598 s->size += cinfo.maskbits / 8;
a50b1753 6599 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6600 if (contents == NULL)
6601 {
6602 free (cinfo.bitmask);
6603 free (cinfo.hashcodes);
6604 return FALSE;
6605 }
6606
6607 s->contents = contents;
6608 bfd_put_32 (output_bfd, bucketcount, contents);
6609 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6610 bfd_put_32 (output_bfd, maskwords, contents + 8);
6611 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6612 contents += 16 + cinfo.maskbits / 8;
6613
6614 for (i = 0; i < bucketcount; ++i)
6615 {
6616 if (cinfo.counts[i] == 0)
6617 bfd_put_32 (output_bfd, 0, contents);
6618 else
6619 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6620 contents += 4;
6621 }
6622
6623 cinfo.contents = contents;
6624
6625 /* Renumber dynamic symbols, populate .gnu.hash section. */
6626 elf_link_hash_traverse (elf_hash_table (info),
6627 elf_renumber_gnu_hash_syms, &cinfo);
6628
6629 contents = s->contents + 16;
6630 for (i = 0; i < maskwords; ++i)
6631 {
6632 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6633 contents);
6634 contents += bed->s->arch_size / 8;
6635 }
6636
6637 free (cinfo.bitmask);
6638 free (cinfo.hashcodes);
6639 }
6640 }
5a580b3a
AM
6641
6642 s = bfd_get_section_by_name (dynobj, ".dynstr");
6643 BFD_ASSERT (s != NULL);
6644
4ad4eba5 6645 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6646
eea6121a 6647 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6648
6649 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6650 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6651 return FALSE;
6652 }
6653
6654 return TRUE;
6655}
4d269e42
AM
6656\f
6657/* Indicate that we are only retrieving symbol values from this
6658 section. */
6659
6660void
6661_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6662{
6663 if (is_elf_hash_table (info->hash))
6664 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6665 _bfd_generic_link_just_syms (sec, info);
6666}
6667
6668/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6669
6670static void
6671merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6672 asection *sec)
6673{
6674 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6675 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6676}
6677
6678/* Finish SHF_MERGE section merging. */
6679
6680bfd_boolean
6681_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6682{
6683 bfd *ibfd;
6684 asection *sec;
6685
6686 if (!is_elf_hash_table (info->hash))
6687 return FALSE;
6688
6689 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6690 if ((ibfd->flags & DYNAMIC) == 0)
6691 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6692 if ((sec->flags & SEC_MERGE) != 0
6693 && !bfd_is_abs_section (sec->output_section))
6694 {
6695 struct bfd_elf_section_data *secdata;
6696
6697 secdata = elf_section_data (sec);
6698 if (! _bfd_add_merge_section (abfd,
6699 &elf_hash_table (info)->merge_info,
6700 sec, &secdata->sec_info))
6701 return FALSE;
6702 else if (secdata->sec_info)
6703 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6704 }
6705
6706 if (elf_hash_table (info)->merge_info != NULL)
6707 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6708 merge_sections_remove_hook);
6709 return TRUE;
6710}
6711
6712/* Create an entry in an ELF linker hash table. */
6713
6714struct bfd_hash_entry *
6715_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6716 struct bfd_hash_table *table,
6717 const char *string)
6718{
6719 /* Allocate the structure if it has not already been allocated by a
6720 subclass. */
6721 if (entry == NULL)
6722 {
a50b1753
NC
6723 entry = (struct bfd_hash_entry *)
6724 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6725 if (entry == NULL)
6726 return entry;
6727 }
6728
6729 /* Call the allocation method of the superclass. */
6730 entry = _bfd_link_hash_newfunc (entry, table, string);
6731 if (entry != NULL)
6732 {
6733 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6734 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6735
6736 /* Set local fields. */
6737 ret->indx = -1;
6738 ret->dynindx = -1;
6739 ret->got = htab->init_got_refcount;
6740 ret->plt = htab->init_plt_refcount;
6741 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6742 - offsetof (struct elf_link_hash_entry, size)));
6743 /* Assume that we have been called by a non-ELF symbol reader.
6744 This flag is then reset by the code which reads an ELF input
6745 file. This ensures that a symbol created by a non-ELF symbol
6746 reader will have the flag set correctly. */
6747 ret->non_elf = 1;
6748 }
6749
6750 return entry;
6751}
6752
6753/* Copy data from an indirect symbol to its direct symbol, hiding the
6754 old indirect symbol. Also used for copying flags to a weakdef. */
6755
6756void
6757_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6758 struct elf_link_hash_entry *dir,
6759 struct elf_link_hash_entry *ind)
6760{
6761 struct elf_link_hash_table *htab;
6762
6763 /* Copy down any references that we may have already seen to the
6764 symbol which just became indirect. */
6765
6766 dir->ref_dynamic |= ind->ref_dynamic;
6767 dir->ref_regular |= ind->ref_regular;
6768 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6769 dir->non_got_ref |= ind->non_got_ref;
6770 dir->needs_plt |= ind->needs_plt;
6771 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6772
6773 if (ind->root.type != bfd_link_hash_indirect)
6774 return;
6775
6776 /* Copy over the global and procedure linkage table refcount entries.
6777 These may have been already set up by a check_relocs routine. */
6778 htab = elf_hash_table (info);
6779 if (ind->got.refcount > htab->init_got_refcount.refcount)
6780 {
6781 if (dir->got.refcount < 0)
6782 dir->got.refcount = 0;
6783 dir->got.refcount += ind->got.refcount;
6784 ind->got.refcount = htab->init_got_refcount.refcount;
6785 }
6786
6787 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6788 {
6789 if (dir->plt.refcount < 0)
6790 dir->plt.refcount = 0;
6791 dir->plt.refcount += ind->plt.refcount;
6792 ind->plt.refcount = htab->init_plt_refcount.refcount;
6793 }
6794
6795 if (ind->dynindx != -1)
6796 {
6797 if (dir->dynindx != -1)
6798 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6799 dir->dynindx = ind->dynindx;
6800 dir->dynstr_index = ind->dynstr_index;
6801 ind->dynindx = -1;
6802 ind->dynstr_index = 0;
6803 }
6804}
6805
6806void
6807_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6808 struct elf_link_hash_entry *h,
6809 bfd_boolean force_local)
6810{
3aa14d16
L
6811 /* STT_GNU_IFUNC symbol must go through PLT. */
6812 if (h->type != STT_GNU_IFUNC)
6813 {
6814 h->plt = elf_hash_table (info)->init_plt_offset;
6815 h->needs_plt = 0;
6816 }
4d269e42
AM
6817 if (force_local)
6818 {
6819 h->forced_local = 1;
6820 if (h->dynindx != -1)
6821 {
6822 h->dynindx = -1;
6823 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6824 h->dynstr_index);
6825 }
6826 }
6827}
6828
6829/* Initialize an ELF linker hash table. */
6830
6831bfd_boolean
6832_bfd_elf_link_hash_table_init
6833 (struct elf_link_hash_table *table,
6834 bfd *abfd,
6835 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6836 struct bfd_hash_table *,
6837 const char *),
4dfe6ac6
NC
6838 unsigned int entsize,
6839 enum elf_target_id target_id)
4d269e42
AM
6840{
6841 bfd_boolean ret;
6842 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6843
6844 memset (table, 0, sizeof * table);
6845 table->init_got_refcount.refcount = can_refcount - 1;
6846 table->init_plt_refcount.refcount = can_refcount - 1;
6847 table->init_got_offset.offset = -(bfd_vma) 1;
6848 table->init_plt_offset.offset = -(bfd_vma) 1;
6849 /* The first dynamic symbol is a dummy. */
6850 table->dynsymcount = 1;
6851
6852 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 6853
4d269e42 6854 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 6855 table->hash_table_id = target_id;
4d269e42
AM
6856
6857 return ret;
6858}
6859
6860/* Create an ELF linker hash table. */
6861
6862struct bfd_link_hash_table *
6863_bfd_elf_link_hash_table_create (bfd *abfd)
6864{
6865 struct elf_link_hash_table *ret;
6866 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6867
a50b1753 6868 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
4d269e42
AM
6869 if (ret == NULL)
6870 return NULL;
6871
6872 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
6873 sizeof (struct elf_link_hash_entry),
6874 GENERIC_ELF_DATA))
4d269e42
AM
6875 {
6876 free (ret);
6877 return NULL;
6878 }
6879
6880 return &ret->root;
6881}
6882
6883/* This is a hook for the ELF emulation code in the generic linker to
6884 tell the backend linker what file name to use for the DT_NEEDED
6885 entry for a dynamic object. */
6886
6887void
6888bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6889{
6890 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6891 && bfd_get_format (abfd) == bfd_object)
6892 elf_dt_name (abfd) = name;
6893}
6894
6895int
6896bfd_elf_get_dyn_lib_class (bfd *abfd)
6897{
6898 int lib_class;
6899 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6900 && bfd_get_format (abfd) == bfd_object)
6901 lib_class = elf_dyn_lib_class (abfd);
6902 else
6903 lib_class = 0;
6904 return lib_class;
6905}
6906
6907void
6908bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6909{
6910 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6911 && bfd_get_format (abfd) == bfd_object)
6912 elf_dyn_lib_class (abfd) = lib_class;
6913}
6914
6915/* Get the list of DT_NEEDED entries for a link. This is a hook for
6916 the linker ELF emulation code. */
6917
6918struct bfd_link_needed_list *
6919bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6920 struct bfd_link_info *info)
6921{
6922 if (! is_elf_hash_table (info->hash))
6923 return NULL;
6924 return elf_hash_table (info)->needed;
6925}
6926
6927/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6928 hook for the linker ELF emulation code. */
6929
6930struct bfd_link_needed_list *
6931bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6932 struct bfd_link_info *info)
6933{
6934 if (! is_elf_hash_table (info->hash))
6935 return NULL;
6936 return elf_hash_table (info)->runpath;
6937}
6938
6939/* Get the name actually used for a dynamic object for a link. This
6940 is the SONAME entry if there is one. Otherwise, it is the string
6941 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6942
6943const char *
6944bfd_elf_get_dt_soname (bfd *abfd)
6945{
6946 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6947 && bfd_get_format (abfd) == bfd_object)
6948 return elf_dt_name (abfd);
6949 return NULL;
6950}
6951
6952/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6953 the ELF linker emulation code. */
6954
6955bfd_boolean
6956bfd_elf_get_bfd_needed_list (bfd *abfd,
6957 struct bfd_link_needed_list **pneeded)
6958{
6959 asection *s;
6960 bfd_byte *dynbuf = NULL;
cb33740c 6961 unsigned int elfsec;
4d269e42
AM
6962 unsigned long shlink;
6963 bfd_byte *extdyn, *extdynend;
6964 size_t extdynsize;
6965 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6966
6967 *pneeded = NULL;
6968
6969 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6970 || bfd_get_format (abfd) != bfd_object)
6971 return TRUE;
6972
6973 s = bfd_get_section_by_name (abfd, ".dynamic");
6974 if (s == NULL || s->size == 0)
6975 return TRUE;
6976
6977 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6978 goto error_return;
6979
6980 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 6981 if (elfsec == SHN_BAD)
4d269e42
AM
6982 goto error_return;
6983
6984 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6985
4d269e42
AM
6986 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6987 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6988
6989 extdyn = dynbuf;
6990 extdynend = extdyn + s->size;
6991 for (; extdyn < extdynend; extdyn += extdynsize)
6992 {
6993 Elf_Internal_Dyn dyn;
6994
6995 (*swap_dyn_in) (abfd, extdyn, &dyn);
6996
6997 if (dyn.d_tag == DT_NULL)
6998 break;
6999
7000 if (dyn.d_tag == DT_NEEDED)
7001 {
7002 const char *string;
7003 struct bfd_link_needed_list *l;
7004 unsigned int tagv = dyn.d_un.d_val;
7005 bfd_size_type amt;
7006
7007 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7008 if (string == NULL)
7009 goto error_return;
7010
7011 amt = sizeof *l;
a50b1753 7012 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7013 if (l == NULL)
7014 goto error_return;
7015
7016 l->by = abfd;
7017 l->name = string;
7018 l->next = *pneeded;
7019 *pneeded = l;
7020 }
7021 }
7022
7023 free (dynbuf);
7024
7025 return TRUE;
7026
7027 error_return:
7028 if (dynbuf != NULL)
7029 free (dynbuf);
7030 return FALSE;
7031}
7032
7033struct elf_symbuf_symbol
7034{
7035 unsigned long st_name; /* Symbol name, index in string tbl */
7036 unsigned char st_info; /* Type and binding attributes */
7037 unsigned char st_other; /* Visibilty, and target specific */
7038};
7039
7040struct elf_symbuf_head
7041{
7042 struct elf_symbuf_symbol *ssym;
7043 bfd_size_type count;
7044 unsigned int st_shndx;
7045};
7046
7047struct elf_symbol
7048{
7049 union
7050 {
7051 Elf_Internal_Sym *isym;
7052 struct elf_symbuf_symbol *ssym;
7053 } u;
7054 const char *name;
7055};
7056
7057/* Sort references to symbols by ascending section number. */
7058
7059static int
7060elf_sort_elf_symbol (const void *arg1, const void *arg2)
7061{
7062 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7063 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7064
7065 return s1->st_shndx - s2->st_shndx;
7066}
7067
7068static int
7069elf_sym_name_compare (const void *arg1, const void *arg2)
7070{
7071 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7072 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7073 return strcmp (s1->name, s2->name);
7074}
7075
7076static struct elf_symbuf_head *
7077elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7078{
14b1c01e 7079 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7080 struct elf_symbuf_symbol *ssym;
7081 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7082 bfd_size_type i, shndx_count, total_size;
4d269e42 7083
a50b1753 7084 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7085 if (indbuf == NULL)
7086 return NULL;
7087
7088 for (ind = indbuf, i = 0; i < symcount; i++)
7089 if (isymbuf[i].st_shndx != SHN_UNDEF)
7090 *ind++ = &isymbuf[i];
7091 indbufend = ind;
7092
7093 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7094 elf_sort_elf_symbol);
7095
7096 shndx_count = 0;
7097 if (indbufend > indbuf)
7098 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7099 if (ind[0]->st_shndx != ind[1]->st_shndx)
7100 shndx_count++;
7101
3ae181ee
L
7102 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7103 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7104 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7105 if (ssymbuf == NULL)
7106 {
7107 free (indbuf);
7108 return NULL;
7109 }
7110
3ae181ee 7111 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7112 ssymbuf->ssym = NULL;
7113 ssymbuf->count = shndx_count;
7114 ssymbuf->st_shndx = 0;
7115 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7116 {
7117 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7118 {
7119 ssymhead++;
7120 ssymhead->ssym = ssym;
7121 ssymhead->count = 0;
7122 ssymhead->st_shndx = (*ind)->st_shndx;
7123 }
7124 ssym->st_name = (*ind)->st_name;
7125 ssym->st_info = (*ind)->st_info;
7126 ssym->st_other = (*ind)->st_other;
7127 ssymhead->count++;
7128 }
3ae181ee
L
7129 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7130 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7131 == total_size));
4d269e42
AM
7132
7133 free (indbuf);
7134 return ssymbuf;
7135}
7136
7137/* Check if 2 sections define the same set of local and global
7138 symbols. */
7139
8f317e31 7140static bfd_boolean
4d269e42
AM
7141bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7142 struct bfd_link_info *info)
7143{
7144 bfd *bfd1, *bfd2;
7145 const struct elf_backend_data *bed1, *bed2;
7146 Elf_Internal_Shdr *hdr1, *hdr2;
7147 bfd_size_type symcount1, symcount2;
7148 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7149 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7150 Elf_Internal_Sym *isym, *isymend;
7151 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7152 bfd_size_type count1, count2, i;
cb33740c 7153 unsigned int shndx1, shndx2;
4d269e42
AM
7154 bfd_boolean result;
7155
7156 bfd1 = sec1->owner;
7157 bfd2 = sec2->owner;
7158
4d269e42
AM
7159 /* Both sections have to be in ELF. */
7160 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7161 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7162 return FALSE;
7163
7164 if (elf_section_type (sec1) != elf_section_type (sec2))
7165 return FALSE;
7166
4d269e42
AM
7167 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7168 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7169 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7170 return FALSE;
7171
7172 bed1 = get_elf_backend_data (bfd1);
7173 bed2 = get_elf_backend_data (bfd2);
7174 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7175 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7176 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7177 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7178
7179 if (symcount1 == 0 || symcount2 == 0)
7180 return FALSE;
7181
7182 result = FALSE;
7183 isymbuf1 = NULL;
7184 isymbuf2 = NULL;
a50b1753
NC
7185 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7186 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7187
7188 if (ssymbuf1 == NULL)
7189 {
7190 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7191 NULL, NULL, NULL);
7192 if (isymbuf1 == NULL)
7193 goto done;
7194
7195 if (!info->reduce_memory_overheads)
7196 elf_tdata (bfd1)->symbuf = ssymbuf1
7197 = elf_create_symbuf (symcount1, isymbuf1);
7198 }
7199
7200 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7201 {
7202 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7203 NULL, NULL, NULL);
7204 if (isymbuf2 == NULL)
7205 goto done;
7206
7207 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7208 elf_tdata (bfd2)->symbuf = ssymbuf2
7209 = elf_create_symbuf (symcount2, isymbuf2);
7210 }
7211
7212 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7213 {
7214 /* Optimized faster version. */
7215 bfd_size_type lo, hi, mid;
7216 struct elf_symbol *symp;
7217 struct elf_symbuf_symbol *ssym, *ssymend;
7218
7219 lo = 0;
7220 hi = ssymbuf1->count;
7221 ssymbuf1++;
7222 count1 = 0;
7223 while (lo < hi)
7224 {
7225 mid = (lo + hi) / 2;
cb33740c 7226 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7227 hi = mid;
cb33740c 7228 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7229 lo = mid + 1;
7230 else
7231 {
7232 count1 = ssymbuf1[mid].count;
7233 ssymbuf1 += mid;
7234 break;
7235 }
7236 }
7237
7238 lo = 0;
7239 hi = ssymbuf2->count;
7240 ssymbuf2++;
7241 count2 = 0;
7242 while (lo < hi)
7243 {
7244 mid = (lo + hi) / 2;
cb33740c 7245 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7246 hi = mid;
cb33740c 7247 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7248 lo = mid + 1;
7249 else
7250 {
7251 count2 = ssymbuf2[mid].count;
7252 ssymbuf2 += mid;
7253 break;
7254 }
7255 }
7256
7257 if (count1 == 0 || count2 == 0 || count1 != count2)
7258 goto done;
7259
a50b1753
NC
7260 symtable1 = (struct elf_symbol *)
7261 bfd_malloc (count1 * sizeof (struct elf_symbol));
7262 symtable2 = (struct elf_symbol *)
7263 bfd_malloc (count2 * sizeof (struct elf_symbol));
4d269e42
AM
7264 if (symtable1 == NULL || symtable2 == NULL)
7265 goto done;
7266
7267 symp = symtable1;
7268 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7269 ssym < ssymend; ssym++, symp++)
7270 {
7271 symp->u.ssym = ssym;
7272 symp->name = bfd_elf_string_from_elf_section (bfd1,
7273 hdr1->sh_link,
7274 ssym->st_name);
7275 }
7276
7277 symp = symtable2;
7278 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7279 ssym < ssymend; ssym++, symp++)
7280 {
7281 symp->u.ssym = ssym;
7282 symp->name = bfd_elf_string_from_elf_section (bfd2,
7283 hdr2->sh_link,
7284 ssym->st_name);
7285 }
7286
7287 /* Sort symbol by name. */
7288 qsort (symtable1, count1, sizeof (struct elf_symbol),
7289 elf_sym_name_compare);
7290 qsort (symtable2, count1, sizeof (struct elf_symbol),
7291 elf_sym_name_compare);
7292
7293 for (i = 0; i < count1; i++)
7294 /* Two symbols must have the same binding, type and name. */
7295 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7296 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7297 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7298 goto done;
7299
7300 result = TRUE;
7301 goto done;
7302 }
7303
a50b1753
NC
7304 symtable1 = (struct elf_symbol *)
7305 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7306 symtable2 = (struct elf_symbol *)
7307 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7308 if (symtable1 == NULL || symtable2 == NULL)
7309 goto done;
7310
7311 /* Count definitions in the section. */
7312 count1 = 0;
7313 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7314 if (isym->st_shndx == shndx1)
4d269e42
AM
7315 symtable1[count1++].u.isym = isym;
7316
7317 count2 = 0;
7318 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7319 if (isym->st_shndx == shndx2)
4d269e42
AM
7320 symtable2[count2++].u.isym = isym;
7321
7322 if (count1 == 0 || count2 == 0 || count1 != count2)
7323 goto done;
7324
7325 for (i = 0; i < count1; i++)
7326 symtable1[i].name
7327 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7328 symtable1[i].u.isym->st_name);
7329
7330 for (i = 0; i < count2; i++)
7331 symtable2[i].name
7332 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7333 symtable2[i].u.isym->st_name);
7334
7335 /* Sort symbol by name. */
7336 qsort (symtable1, count1, sizeof (struct elf_symbol),
7337 elf_sym_name_compare);
7338 qsort (symtable2, count1, sizeof (struct elf_symbol),
7339 elf_sym_name_compare);
7340
7341 for (i = 0; i < count1; i++)
7342 /* Two symbols must have the same binding, type and name. */
7343 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7344 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7345 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7346 goto done;
7347
7348 result = TRUE;
7349
7350done:
7351 if (symtable1)
7352 free (symtable1);
7353 if (symtable2)
7354 free (symtable2);
7355 if (isymbuf1)
7356 free (isymbuf1);
7357 if (isymbuf2)
7358 free (isymbuf2);
7359
7360 return result;
7361}
7362
7363/* Return TRUE if 2 section types are compatible. */
7364
7365bfd_boolean
7366_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7367 bfd *bbfd, const asection *bsec)
7368{
7369 if (asec == NULL
7370 || bsec == NULL
7371 || abfd->xvec->flavour != bfd_target_elf_flavour
7372 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7373 return TRUE;
7374
7375 return elf_section_type (asec) == elf_section_type (bsec);
7376}
7377\f
c152c796
AM
7378/* Final phase of ELF linker. */
7379
7380/* A structure we use to avoid passing large numbers of arguments. */
7381
7382struct elf_final_link_info
7383{
7384 /* General link information. */
7385 struct bfd_link_info *info;
7386 /* Output BFD. */
7387 bfd *output_bfd;
7388 /* Symbol string table. */
7389 struct bfd_strtab_hash *symstrtab;
7390 /* .dynsym section. */
7391 asection *dynsym_sec;
7392 /* .hash section. */
7393 asection *hash_sec;
7394 /* symbol version section (.gnu.version). */
7395 asection *symver_sec;
7396 /* Buffer large enough to hold contents of any section. */
7397 bfd_byte *contents;
7398 /* Buffer large enough to hold external relocs of any section. */
7399 void *external_relocs;
7400 /* Buffer large enough to hold internal relocs of any section. */
7401 Elf_Internal_Rela *internal_relocs;
7402 /* Buffer large enough to hold external local symbols of any input
7403 BFD. */
7404 bfd_byte *external_syms;
7405 /* And a buffer for symbol section indices. */
7406 Elf_External_Sym_Shndx *locsym_shndx;
7407 /* Buffer large enough to hold internal local symbols of any input
7408 BFD. */
7409 Elf_Internal_Sym *internal_syms;
7410 /* Array large enough to hold a symbol index for each local symbol
7411 of any input BFD. */
7412 long *indices;
7413 /* Array large enough to hold a section pointer for each local
7414 symbol of any input BFD. */
7415 asection **sections;
7416 /* Buffer to hold swapped out symbols. */
7417 bfd_byte *symbuf;
7418 /* And one for symbol section indices. */
7419 Elf_External_Sym_Shndx *symshndxbuf;
7420 /* Number of swapped out symbols in buffer. */
7421 size_t symbuf_count;
7422 /* Number of symbols which fit in symbuf. */
7423 size_t symbuf_size;
7424 /* And same for symshndxbuf. */
7425 size_t shndxbuf_size;
7426};
7427
7428/* This struct is used to pass information to elf_link_output_extsym. */
7429
7430struct elf_outext_info
7431{
7432 bfd_boolean failed;
7433 bfd_boolean localsyms;
7434 struct elf_final_link_info *finfo;
7435};
7436
d9352518
DB
7437
7438/* Support for evaluating a complex relocation.
7439
7440 Complex relocations are generalized, self-describing relocations. The
7441 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7442 relocations themselves.
d9352518
DB
7443
7444 The relocations are use a reserved elf-wide relocation type code (R_RELC
7445 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7446 information (start bit, end bit, word width, etc) into the addend. This
7447 information is extracted from CGEN-generated operand tables within gas.
7448
7449 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7450 internal) representing prefix-notation expressions, including but not
7451 limited to those sorts of expressions normally encoded as addends in the
7452 addend field. The symbol mangling format is:
7453
7454 <node> := <literal>
7455 | <unary-operator> ':' <node>
7456 | <binary-operator> ':' <node> ':' <node>
7457 ;
7458
7459 <literal> := 's' <digits=N> ':' <N character symbol name>
7460 | 'S' <digits=N> ':' <N character section name>
7461 | '#' <hexdigits>
7462 ;
7463
7464 <binary-operator> := as in C
7465 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7466
7467static void
a0c8462f
AM
7468set_symbol_value (bfd *bfd_with_globals,
7469 Elf_Internal_Sym *isymbuf,
7470 size_t locsymcount,
7471 size_t symidx,
7472 bfd_vma val)
d9352518 7473{
8977835c
AM
7474 struct elf_link_hash_entry **sym_hashes;
7475 struct elf_link_hash_entry *h;
7476 size_t extsymoff = locsymcount;
d9352518 7477
8977835c 7478 if (symidx < locsymcount)
d9352518 7479 {
8977835c
AM
7480 Elf_Internal_Sym *sym;
7481
7482 sym = isymbuf + symidx;
7483 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7484 {
7485 /* It is a local symbol: move it to the
7486 "absolute" section and give it a value. */
7487 sym->st_shndx = SHN_ABS;
7488 sym->st_value = val;
7489 return;
7490 }
7491 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7492 extsymoff = 0;
d9352518 7493 }
8977835c
AM
7494
7495 /* It is a global symbol: set its link type
7496 to "defined" and give it a value. */
7497
7498 sym_hashes = elf_sym_hashes (bfd_with_globals);
7499 h = sym_hashes [symidx - extsymoff];
7500 while (h->root.type == bfd_link_hash_indirect
7501 || h->root.type == bfd_link_hash_warning)
7502 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7503 h->root.type = bfd_link_hash_defined;
7504 h->root.u.def.value = val;
7505 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7506}
7507
a0c8462f
AM
7508static bfd_boolean
7509resolve_symbol (const char *name,
7510 bfd *input_bfd,
7511 struct elf_final_link_info *finfo,
7512 bfd_vma *result,
7513 Elf_Internal_Sym *isymbuf,
7514 size_t locsymcount)
d9352518 7515{
a0c8462f
AM
7516 Elf_Internal_Sym *sym;
7517 struct bfd_link_hash_entry *global_entry;
7518 const char *candidate = NULL;
7519 Elf_Internal_Shdr *symtab_hdr;
7520 size_t i;
7521
d9352518
DB
7522 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7523
7524 for (i = 0; i < locsymcount; ++ i)
7525 {
8977835c 7526 sym = isymbuf + i;
d9352518
DB
7527
7528 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7529 continue;
7530
7531 candidate = bfd_elf_string_from_elf_section (input_bfd,
7532 symtab_hdr->sh_link,
7533 sym->st_name);
7534#ifdef DEBUG
0f02bbd9
AM
7535 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7536 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7537#endif
7538 if (candidate && strcmp (candidate, name) == 0)
7539 {
0f02bbd9 7540 asection *sec = finfo->sections [i];
d9352518 7541
0f02bbd9
AM
7542 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7543 *result += sec->output_offset + sec->output_section->vma;
d9352518 7544#ifdef DEBUG
0f02bbd9
AM
7545 printf ("Found symbol with value %8.8lx\n",
7546 (unsigned long) *result);
d9352518
DB
7547#endif
7548 return TRUE;
7549 }
7550 }
7551
7552 /* Hmm, haven't found it yet. perhaps it is a global. */
a0c8462f
AM
7553 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7554 FALSE, FALSE, TRUE);
d9352518
DB
7555 if (!global_entry)
7556 return FALSE;
a0c8462f 7557
d9352518
DB
7558 if (global_entry->type == bfd_link_hash_defined
7559 || global_entry->type == bfd_link_hash_defweak)
7560 {
a0c8462f
AM
7561 *result = (global_entry->u.def.value
7562 + global_entry->u.def.section->output_section->vma
7563 + global_entry->u.def.section->output_offset);
d9352518 7564#ifdef DEBUG
0f02bbd9
AM
7565 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7566 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7567#endif
7568 return TRUE;
a0c8462f 7569 }
d9352518 7570
d9352518
DB
7571 return FALSE;
7572}
7573
7574static bfd_boolean
a0c8462f
AM
7575resolve_section (const char *name,
7576 asection *sections,
7577 bfd_vma *result)
d9352518 7578{
a0c8462f
AM
7579 asection *curr;
7580 unsigned int len;
d9352518 7581
a0c8462f 7582 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7583 if (strcmp (curr->name, name) == 0)
7584 {
7585 *result = curr->vma;
7586 return TRUE;
7587 }
7588
7589 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7590 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7591 {
7592 len = strlen (curr->name);
a0c8462f 7593 if (len > strlen (name))
d9352518
DB
7594 continue;
7595
7596 if (strncmp (curr->name, name, len) == 0)
7597 {
7598 if (strncmp (".end", name + len, 4) == 0)
7599 {
7600 *result = curr->vma + curr->size;
7601 return TRUE;
7602 }
7603
7604 /* Insert more pseudo-section names here, if you like. */
7605 }
7606 }
a0c8462f 7607
d9352518
DB
7608 return FALSE;
7609}
7610
7611static void
a0c8462f 7612undefined_reference (const char *reftype, const char *name)
d9352518 7613{
a0c8462f
AM
7614 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7615 reftype, name);
d9352518
DB
7616}
7617
7618static bfd_boolean
a0c8462f
AM
7619eval_symbol (bfd_vma *result,
7620 const char **symp,
7621 bfd *input_bfd,
7622 struct elf_final_link_info *finfo,
7623 bfd_vma dot,
7624 Elf_Internal_Sym *isymbuf,
7625 size_t locsymcount,
7626 int signed_p)
d9352518 7627{
4b93929b
NC
7628 size_t len;
7629 size_t symlen;
a0c8462f
AM
7630 bfd_vma a;
7631 bfd_vma b;
4b93929b 7632 char symbuf[4096];
0f02bbd9 7633 const char *sym = *symp;
a0c8462f
AM
7634 const char *symend;
7635 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7636
7637 len = strlen (sym);
7638 symend = sym + len;
7639
4b93929b 7640 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7641 {
7642 bfd_set_error (bfd_error_invalid_operation);
7643 return FALSE;
7644 }
a0c8462f 7645
d9352518
DB
7646 switch (* sym)
7647 {
7648 case '.':
0f02bbd9
AM
7649 *result = dot;
7650 *symp = sym + 1;
d9352518
DB
7651 return TRUE;
7652
7653 case '#':
0f02bbd9
AM
7654 ++sym;
7655 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7656 return TRUE;
7657
7658 case 'S':
7659 symbol_is_section = TRUE;
a0c8462f 7660 case 's':
0f02bbd9
AM
7661 ++sym;
7662 symlen = strtol (sym, (char **) symp, 10);
7663 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7664
4b93929b 7665 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7666 {
7667 bfd_set_error (bfd_error_invalid_operation);
7668 return FALSE;
7669 }
7670
7671 memcpy (symbuf, sym, symlen);
a0c8462f 7672 symbuf[symlen] = '\0';
0f02bbd9 7673 *symp = sym + symlen;
a0c8462f
AM
7674
7675 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7676 the symbol as a section, or vice-versa. so we're pretty liberal in our
7677 interpretation here; section means "try section first", not "must be a
7678 section", and likewise with symbol. */
7679
a0c8462f 7680 if (symbol_is_section)
d9352518 7681 {
8977835c
AM
7682 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7683 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7684 isymbuf, locsymcount))
d9352518
DB
7685 {
7686 undefined_reference ("section", symbuf);
7687 return FALSE;
7688 }
a0c8462f
AM
7689 }
7690 else
d9352518 7691 {
8977835c
AM
7692 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7693 isymbuf, locsymcount)
7694 && !resolve_section (symbuf, finfo->output_bfd->sections,
7695 result))
d9352518
DB
7696 {
7697 undefined_reference ("symbol", symbuf);
7698 return FALSE;
7699 }
7700 }
7701
7702 return TRUE;
a0c8462f 7703
d9352518
DB
7704 /* All that remains are operators. */
7705
7706#define UNARY_OP(op) \
7707 if (strncmp (sym, #op, strlen (#op)) == 0) \
7708 { \
7709 sym += strlen (#op); \
a0c8462f
AM
7710 if (*sym == ':') \
7711 ++sym; \
0f02bbd9
AM
7712 *symp = sym; \
7713 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7714 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7715 return FALSE; \
7716 if (signed_p) \
0f02bbd9 7717 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7718 else \
7719 *result = op a; \
d9352518
DB
7720 return TRUE; \
7721 }
7722
7723#define BINARY_OP(op) \
7724 if (strncmp (sym, #op, strlen (#op)) == 0) \
7725 { \
7726 sym += strlen (#op); \
a0c8462f
AM
7727 if (*sym == ':') \
7728 ++sym; \
0f02bbd9
AM
7729 *symp = sym; \
7730 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7731 isymbuf, locsymcount, signed_p)) \
a0c8462f 7732 return FALSE; \
0f02bbd9
AM
7733 ++*symp; \
7734 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7735 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7736 return FALSE; \
7737 if (signed_p) \
0f02bbd9 7738 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7739 else \
7740 *result = a op b; \
d9352518
DB
7741 return TRUE; \
7742 }
7743
7744 default:
7745 UNARY_OP (0-);
7746 BINARY_OP (<<);
7747 BINARY_OP (>>);
7748 BINARY_OP (==);
7749 BINARY_OP (!=);
7750 BINARY_OP (<=);
7751 BINARY_OP (>=);
7752 BINARY_OP (&&);
7753 BINARY_OP (||);
7754 UNARY_OP (~);
7755 UNARY_OP (!);
7756 BINARY_OP (*);
7757 BINARY_OP (/);
7758 BINARY_OP (%);
7759 BINARY_OP (^);
7760 BINARY_OP (|);
7761 BINARY_OP (&);
7762 BINARY_OP (+);
7763 BINARY_OP (-);
7764 BINARY_OP (<);
7765 BINARY_OP (>);
7766#undef UNARY_OP
7767#undef BINARY_OP
7768 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7769 bfd_set_error (bfd_error_invalid_operation);
7770 return FALSE;
7771 }
7772}
7773
d9352518 7774static void
a0c8462f
AM
7775put_value (bfd_vma size,
7776 unsigned long chunksz,
7777 bfd *input_bfd,
7778 bfd_vma x,
7779 bfd_byte *location)
d9352518
DB
7780{
7781 location += (size - chunksz);
7782
a0c8462f 7783 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7784 {
7785 switch (chunksz)
7786 {
7787 default:
7788 case 0:
7789 abort ();
7790 case 1:
7791 bfd_put_8 (input_bfd, x, location);
7792 break;
7793 case 2:
7794 bfd_put_16 (input_bfd, x, location);
7795 break;
7796 case 4:
7797 bfd_put_32 (input_bfd, x, location);
7798 break;
7799 case 8:
7800#ifdef BFD64
7801 bfd_put_64 (input_bfd, x, location);
7802#else
7803 abort ();
7804#endif
7805 break;
7806 }
7807 }
7808}
7809
a0c8462f
AM
7810static bfd_vma
7811get_value (bfd_vma size,
7812 unsigned long chunksz,
7813 bfd *input_bfd,
7814 bfd_byte *location)
d9352518
DB
7815{
7816 bfd_vma x = 0;
7817
a0c8462f 7818 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7819 {
7820 switch (chunksz)
7821 {
7822 default:
7823 case 0:
7824 abort ();
7825 case 1:
7826 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7827 break;
7828 case 2:
7829 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7830 break;
7831 case 4:
7832 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7833 break;
7834 case 8:
7835#ifdef BFD64
7836 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7837#else
7838 abort ();
7839#endif
7840 break;
7841 }
7842 }
7843 return x;
7844}
7845
a0c8462f
AM
7846static void
7847decode_complex_addend (unsigned long *start, /* in bits */
7848 unsigned long *oplen, /* in bits */
7849 unsigned long *len, /* in bits */
7850 unsigned long *wordsz, /* in bytes */
7851 unsigned long *chunksz, /* in bytes */
7852 unsigned long *lsb0_p,
7853 unsigned long *signed_p,
7854 unsigned long *trunc_p,
7855 unsigned long encoded)
d9352518
DB
7856{
7857 * start = encoded & 0x3F;
7858 * len = (encoded >> 6) & 0x3F;
7859 * oplen = (encoded >> 12) & 0x3F;
7860 * wordsz = (encoded >> 18) & 0xF;
7861 * chunksz = (encoded >> 22) & 0xF;
7862 * lsb0_p = (encoded >> 27) & 1;
7863 * signed_p = (encoded >> 28) & 1;
7864 * trunc_p = (encoded >> 29) & 1;
7865}
7866
cdfeee4f 7867bfd_reloc_status_type
0f02bbd9 7868bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7869 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7870 bfd_byte *contents,
7871 Elf_Internal_Rela *rel,
7872 bfd_vma relocation)
d9352518 7873{
0f02bbd9
AM
7874 bfd_vma shift, x, mask;
7875 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7876 bfd_reloc_status_type r;
d9352518
DB
7877
7878 /* Perform this reloc, since it is complex.
7879 (this is not to say that it necessarily refers to a complex
7880 symbol; merely that it is a self-describing CGEN based reloc.
7881 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7882 word size, etc) encoded within it.). */
d9352518 7883
a0c8462f
AM
7884 decode_complex_addend (&start, &oplen, &len, &wordsz,
7885 &chunksz, &lsb0_p, &signed_p,
7886 &trunc_p, rel->r_addend);
d9352518
DB
7887
7888 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7889
7890 if (lsb0_p)
7891 shift = (start + 1) - len;
7892 else
7893 shift = (8 * wordsz) - (start + len);
7894
5dabe785 7895 /* FIXME: octets_per_byte. */
a0c8462f 7896 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7897
7898#ifdef DEBUG
7899 printf ("Doing complex reloc: "
7900 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7901 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7902 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7903 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
7904 oplen, (unsigned long) x, (unsigned long) mask,
7905 (unsigned long) relocation);
d9352518
DB
7906#endif
7907
cdfeee4f 7908 r = bfd_reloc_ok;
d9352518 7909 if (! trunc_p)
cdfeee4f
AM
7910 /* Now do an overflow check. */
7911 r = bfd_check_overflow ((signed_p
7912 ? complain_overflow_signed
7913 : complain_overflow_unsigned),
7914 len, 0, (8 * wordsz),
7915 relocation);
a0c8462f 7916
d9352518
DB
7917 /* Do the deed. */
7918 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7919
7920#ifdef DEBUG
7921 printf (" relocation: %8.8lx\n"
7922 " shifted mask: %8.8lx\n"
7923 " shifted/masked reloc: %8.8lx\n"
7924 " result: %8.8lx\n",
9ccb8af9
AM
7925 (unsigned long) relocation, (unsigned long) (mask << shift),
7926 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 7927#endif
5dabe785 7928 /* FIXME: octets_per_byte. */
d9352518 7929 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7930 return r;
d9352518
DB
7931}
7932
c152c796
AM
7933/* When performing a relocatable link, the input relocations are
7934 preserved. But, if they reference global symbols, the indices
d4730f92
BS
7935 referenced must be updated. Update all the relocations found in
7936 RELDATA. */
c152c796
AM
7937
7938static void
7939elf_link_adjust_relocs (bfd *abfd,
d4730f92 7940 struct bfd_elf_section_reloc_data *reldata)
c152c796
AM
7941{
7942 unsigned int i;
7943 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7944 bfd_byte *erela;
7945 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7946 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7947 bfd_vma r_type_mask;
7948 int r_sym_shift;
d4730f92
BS
7949 unsigned int count = reldata->count;
7950 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 7951
d4730f92 7952 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
7953 {
7954 swap_in = bed->s->swap_reloc_in;
7955 swap_out = bed->s->swap_reloc_out;
7956 }
d4730f92 7957 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
7958 {
7959 swap_in = bed->s->swap_reloca_in;
7960 swap_out = bed->s->swap_reloca_out;
7961 }
7962 else
7963 abort ();
7964
7965 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7966 abort ();
7967
7968 if (bed->s->arch_size == 32)
7969 {
7970 r_type_mask = 0xff;
7971 r_sym_shift = 8;
7972 }
7973 else
7974 {
7975 r_type_mask = 0xffffffff;
7976 r_sym_shift = 32;
7977 }
7978
d4730f92
BS
7979 erela = reldata->hdr->contents;
7980 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
7981 {
7982 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7983 unsigned int j;
7984
7985 if (*rel_hash == NULL)
7986 continue;
7987
7988 BFD_ASSERT ((*rel_hash)->indx >= 0);
7989
7990 (*swap_in) (abfd, erela, irela);
7991 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7992 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7993 | (irela[j].r_info & r_type_mask));
7994 (*swap_out) (abfd, irela, erela);
7995 }
7996}
7997
7998struct elf_link_sort_rela
7999{
8000 union {
8001 bfd_vma offset;
8002 bfd_vma sym_mask;
8003 } u;
8004 enum elf_reloc_type_class type;
8005 /* We use this as an array of size int_rels_per_ext_rel. */
8006 Elf_Internal_Rela rela[1];
8007};
8008
8009static int
8010elf_link_sort_cmp1 (const void *A, const void *B)
8011{
a50b1753
NC
8012 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8013 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8014 int relativea, relativeb;
8015
8016 relativea = a->type == reloc_class_relative;
8017 relativeb = b->type == reloc_class_relative;
8018
8019 if (relativea < relativeb)
8020 return 1;
8021 if (relativea > relativeb)
8022 return -1;
8023 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8024 return -1;
8025 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8026 return 1;
8027 if (a->rela->r_offset < b->rela->r_offset)
8028 return -1;
8029 if (a->rela->r_offset > b->rela->r_offset)
8030 return 1;
8031 return 0;
8032}
8033
8034static int
8035elf_link_sort_cmp2 (const void *A, const void *B)
8036{
a50b1753
NC
8037 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8038 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8039 int copya, copyb;
8040
8041 if (a->u.offset < b->u.offset)
8042 return -1;
8043 if (a->u.offset > b->u.offset)
8044 return 1;
8045 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8046 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8047 if (copya < copyb)
8048 return -1;
8049 if (copya > copyb)
8050 return 1;
8051 if (a->rela->r_offset < b->rela->r_offset)
8052 return -1;
8053 if (a->rela->r_offset > b->rela->r_offset)
8054 return 1;
8055 return 0;
8056}
8057
8058static size_t
8059elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8060{
3410fea8 8061 asection *dynamic_relocs;
fc66a176
L
8062 asection *rela_dyn;
8063 asection *rel_dyn;
c152c796
AM
8064 bfd_size_type count, size;
8065 size_t i, ret, sort_elt, ext_size;
8066 bfd_byte *sort, *s_non_relative, *p;
8067 struct elf_link_sort_rela *sq;
8068 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8069 int i2e = bed->s->int_rels_per_ext_rel;
8070 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8071 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8072 struct bfd_link_order *lo;
8073 bfd_vma r_sym_mask;
3410fea8 8074 bfd_boolean use_rela;
c152c796 8075
3410fea8
NC
8076 /* Find a dynamic reloc section. */
8077 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8078 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8079 if (rela_dyn != NULL && rela_dyn->size > 0
8080 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8081 {
3410fea8
NC
8082 bfd_boolean use_rela_initialised = FALSE;
8083
8084 /* This is just here to stop gcc from complaining.
8085 It's initialization checking code is not perfect. */
8086 use_rela = TRUE;
8087
8088 /* Both sections are present. Examine the sizes
8089 of the indirect sections to help us choose. */
8090 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8091 if (lo->type == bfd_indirect_link_order)
8092 {
8093 asection *o = lo->u.indirect.section;
8094
8095 if ((o->size % bed->s->sizeof_rela) == 0)
8096 {
8097 if ((o->size % bed->s->sizeof_rel) == 0)
8098 /* Section size is divisible by both rel and rela sizes.
8099 It is of no help to us. */
8100 ;
8101 else
8102 {
8103 /* Section size is only divisible by rela. */
8104 if (use_rela_initialised && (use_rela == FALSE))
8105 {
8106 _bfd_error_handler
8107 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8108 bfd_set_error (bfd_error_invalid_operation);
8109 return 0;
8110 }
8111 else
8112 {
8113 use_rela = TRUE;
8114 use_rela_initialised = TRUE;
8115 }
8116 }
8117 }
8118 else if ((o->size % bed->s->sizeof_rel) == 0)
8119 {
8120 /* Section size is only divisible by rel. */
8121 if (use_rela_initialised && (use_rela == TRUE))
8122 {
8123 _bfd_error_handler
8124 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8125 bfd_set_error (bfd_error_invalid_operation);
8126 return 0;
8127 }
8128 else
8129 {
8130 use_rela = FALSE;
8131 use_rela_initialised = TRUE;
8132 }
8133 }
8134 else
8135 {
8136 /* The section size is not divisible by either - something is wrong. */
8137 _bfd_error_handler
8138 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8139 bfd_set_error (bfd_error_invalid_operation);
8140 return 0;
8141 }
8142 }
8143
8144 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8145 if (lo->type == bfd_indirect_link_order)
8146 {
8147 asection *o = lo->u.indirect.section;
8148
8149 if ((o->size % bed->s->sizeof_rela) == 0)
8150 {
8151 if ((o->size % bed->s->sizeof_rel) == 0)
8152 /* Section size is divisible by both rel and rela sizes.
8153 It is of no help to us. */
8154 ;
8155 else
8156 {
8157 /* Section size is only divisible by rela. */
8158 if (use_rela_initialised && (use_rela == FALSE))
8159 {
8160 _bfd_error_handler
8161 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8162 bfd_set_error (bfd_error_invalid_operation);
8163 return 0;
8164 }
8165 else
8166 {
8167 use_rela = TRUE;
8168 use_rela_initialised = TRUE;
8169 }
8170 }
8171 }
8172 else if ((o->size % bed->s->sizeof_rel) == 0)
8173 {
8174 /* Section size is only divisible by rel. */
8175 if (use_rela_initialised && (use_rela == TRUE))
8176 {
8177 _bfd_error_handler
8178 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8179 bfd_set_error (bfd_error_invalid_operation);
8180 return 0;
8181 }
8182 else
8183 {
8184 use_rela = FALSE;
8185 use_rela_initialised = TRUE;
8186 }
8187 }
8188 else
8189 {
8190 /* The section size is not divisible by either - something is wrong. */
8191 _bfd_error_handler
8192 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8193 bfd_set_error (bfd_error_invalid_operation);
8194 return 0;
8195 }
8196 }
8197
8198 if (! use_rela_initialised)
8199 /* Make a guess. */
8200 use_rela = TRUE;
c152c796 8201 }
fc66a176
L
8202 else if (rela_dyn != NULL && rela_dyn->size > 0)
8203 use_rela = TRUE;
8204 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8205 use_rela = FALSE;
c152c796 8206 else
fc66a176 8207 return 0;
3410fea8
NC
8208
8209 if (use_rela)
c152c796 8210 {
3410fea8 8211 dynamic_relocs = rela_dyn;
c152c796
AM
8212 ext_size = bed->s->sizeof_rela;
8213 swap_in = bed->s->swap_reloca_in;
8214 swap_out = bed->s->swap_reloca_out;
8215 }
3410fea8
NC
8216 else
8217 {
8218 dynamic_relocs = rel_dyn;
8219 ext_size = bed->s->sizeof_rel;
8220 swap_in = bed->s->swap_reloc_in;
8221 swap_out = bed->s->swap_reloc_out;
8222 }
c152c796
AM
8223
8224 size = 0;
3410fea8 8225 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8226 if (lo->type == bfd_indirect_link_order)
3410fea8 8227 size += lo->u.indirect.section->size;
c152c796 8228
3410fea8 8229 if (size != dynamic_relocs->size)
c152c796
AM
8230 return 0;
8231
8232 sort_elt = (sizeof (struct elf_link_sort_rela)
8233 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8234
8235 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8236 if (count == 0)
8237 return 0;
a50b1753 8238 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8239
c152c796
AM
8240 if (sort == NULL)
8241 {
8242 (*info->callbacks->warning)
8243 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8244 return 0;
8245 }
8246
8247 if (bed->s->arch_size == 32)
8248 r_sym_mask = ~(bfd_vma) 0xff;
8249 else
8250 r_sym_mask = ~(bfd_vma) 0xffffffff;
8251
3410fea8 8252 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8253 if (lo->type == bfd_indirect_link_order)
8254 {
8255 bfd_byte *erel, *erelend;
8256 asection *o = lo->u.indirect.section;
8257
1da212d6
AM
8258 if (o->contents == NULL && o->size != 0)
8259 {
8260 /* This is a reloc section that is being handled as a normal
8261 section. See bfd_section_from_shdr. We can't combine
8262 relocs in this case. */
8263 free (sort);
8264 return 0;
8265 }
c152c796 8266 erel = o->contents;
eea6121a 8267 erelend = o->contents + o->size;
5dabe785 8268 /* FIXME: octets_per_byte. */
c152c796 8269 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8270
c152c796
AM
8271 while (erel < erelend)
8272 {
8273 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8274
c152c796
AM
8275 (*swap_in) (abfd, erel, s->rela);
8276 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8277 s->u.sym_mask = r_sym_mask;
8278 p += sort_elt;
8279 erel += ext_size;
8280 }
8281 }
8282
8283 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8284
8285 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8286 {
8287 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8288 if (s->type != reloc_class_relative)
8289 break;
8290 }
8291 ret = i;
8292 s_non_relative = p;
8293
8294 sq = (struct elf_link_sort_rela *) s_non_relative;
8295 for (; i < count; i++, p += sort_elt)
8296 {
8297 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8298 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8299 sq = sp;
8300 sp->u.offset = sq->rela->r_offset;
8301 }
8302
8303 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8304
3410fea8 8305 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8306 if (lo->type == bfd_indirect_link_order)
8307 {
8308 bfd_byte *erel, *erelend;
8309 asection *o = lo->u.indirect.section;
8310
8311 erel = o->contents;
eea6121a 8312 erelend = o->contents + o->size;
5dabe785 8313 /* FIXME: octets_per_byte. */
c152c796
AM
8314 p = sort + o->output_offset / ext_size * sort_elt;
8315 while (erel < erelend)
8316 {
8317 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8318 (*swap_out) (abfd, s->rela, erel);
8319 p += sort_elt;
8320 erel += ext_size;
8321 }
8322 }
8323
8324 free (sort);
3410fea8 8325 *psec = dynamic_relocs;
c152c796
AM
8326 return ret;
8327}
8328
8329/* Flush the output symbols to the file. */
8330
8331static bfd_boolean
8332elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8333 const struct elf_backend_data *bed)
8334{
8335 if (finfo->symbuf_count > 0)
8336 {
8337 Elf_Internal_Shdr *hdr;
8338 file_ptr pos;
8339 bfd_size_type amt;
8340
8341 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8342 pos = hdr->sh_offset + hdr->sh_size;
8343 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8344 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8345 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8346 return FALSE;
8347
8348 hdr->sh_size += amt;
8349 finfo->symbuf_count = 0;
8350 }
8351
8352 return TRUE;
8353}
8354
8355/* Add a symbol to the output symbol table. */
8356
6e0b88f1 8357static int
c152c796
AM
8358elf_link_output_sym (struct elf_final_link_info *finfo,
8359 const char *name,
8360 Elf_Internal_Sym *elfsym,
8361 asection *input_sec,
8362 struct elf_link_hash_entry *h)
8363{
8364 bfd_byte *dest;
8365 Elf_External_Sym_Shndx *destshndx;
6e0b88f1 8366 int (*output_symbol_hook)
c152c796
AM
8367 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8368 struct elf_link_hash_entry *);
8369 const struct elf_backend_data *bed;
8370
8371 bed = get_elf_backend_data (finfo->output_bfd);
8372 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8373 if (output_symbol_hook != NULL)
8374 {
6e0b88f1
AM
8375 int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h);
8376 if (ret != 1)
8377 return ret;
c152c796
AM
8378 }
8379
8380 if (name == NULL || *name == '\0')
8381 elfsym->st_name = 0;
8382 else if (input_sec->flags & SEC_EXCLUDE)
8383 elfsym->st_name = 0;
8384 else
8385 {
8386 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8387 name, TRUE, FALSE);
8388 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8389 return 0;
c152c796
AM
8390 }
8391
8392 if (finfo->symbuf_count >= finfo->symbuf_size)
8393 {
8394 if (! elf_link_flush_output_syms (finfo, bed))
6e0b88f1 8395 return 0;
c152c796
AM
8396 }
8397
8398 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8399 destshndx = finfo->symshndxbuf;
8400 if (destshndx != NULL)
8401 {
8402 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8403 {
8404 bfd_size_type amt;
8405
8406 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
a50b1753
NC
8407 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8408 amt * 2);
c152c796 8409 if (destshndx == NULL)
6e0b88f1 8410 return 0;
515ef31d 8411 finfo->symshndxbuf = destshndx;
c152c796
AM
8412 memset ((char *) destshndx + amt, 0, amt);
8413 finfo->shndxbuf_size *= 2;
8414 }
8415 destshndx += bfd_get_symcount (finfo->output_bfd);
8416 }
8417
8418 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8419 finfo->symbuf_count += 1;
8420 bfd_get_symcount (finfo->output_bfd) += 1;
8421
6e0b88f1 8422 return 1;
c152c796
AM
8423}
8424
c0d5a53d
L
8425/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8426
8427static bfd_boolean
8428check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8429{
4fbb74a6
AM
8430 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8431 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8432 {
8433 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8434 beyond 64k. */
c0d5a53d
L
8435 (*_bfd_error_handler)
8436 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8437 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8438 bfd_set_error (bfd_error_nonrepresentable_section);
8439 return FALSE;
8440 }
8441 return TRUE;
8442}
8443
c152c796
AM
8444/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8445 allowing an unsatisfied unversioned symbol in the DSO to match a
8446 versioned symbol that would normally require an explicit version.
8447 We also handle the case that a DSO references a hidden symbol
8448 which may be satisfied by a versioned symbol in another DSO. */
8449
8450static bfd_boolean
8451elf_link_check_versioned_symbol (struct bfd_link_info *info,
8452 const struct elf_backend_data *bed,
8453 struct elf_link_hash_entry *h)
8454{
8455 bfd *abfd;
8456 struct elf_link_loaded_list *loaded;
8457
8458 if (!is_elf_hash_table (info->hash))
8459 return FALSE;
8460
8461 switch (h->root.type)
8462 {
8463 default:
8464 abfd = NULL;
8465 break;
8466
8467 case bfd_link_hash_undefined:
8468 case bfd_link_hash_undefweak:
8469 abfd = h->root.u.undef.abfd;
8470 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8471 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8472 return FALSE;
8473 break;
8474
8475 case bfd_link_hash_defined:
8476 case bfd_link_hash_defweak:
8477 abfd = h->root.u.def.section->owner;
8478 break;
8479
8480 case bfd_link_hash_common:
8481 abfd = h->root.u.c.p->section->owner;
8482 break;
8483 }
8484 BFD_ASSERT (abfd != NULL);
8485
8486 for (loaded = elf_hash_table (info)->loaded;
8487 loaded != NULL;
8488 loaded = loaded->next)
8489 {
8490 bfd *input;
8491 Elf_Internal_Shdr *hdr;
8492 bfd_size_type symcount;
8493 bfd_size_type extsymcount;
8494 bfd_size_type extsymoff;
8495 Elf_Internal_Shdr *versymhdr;
8496 Elf_Internal_Sym *isym;
8497 Elf_Internal_Sym *isymend;
8498 Elf_Internal_Sym *isymbuf;
8499 Elf_External_Versym *ever;
8500 Elf_External_Versym *extversym;
8501
8502 input = loaded->abfd;
8503
8504 /* We check each DSO for a possible hidden versioned definition. */
8505 if (input == abfd
8506 || (input->flags & DYNAMIC) == 0
8507 || elf_dynversym (input) == 0)
8508 continue;
8509
8510 hdr = &elf_tdata (input)->dynsymtab_hdr;
8511
8512 symcount = hdr->sh_size / bed->s->sizeof_sym;
8513 if (elf_bad_symtab (input))
8514 {
8515 extsymcount = symcount;
8516 extsymoff = 0;
8517 }
8518 else
8519 {
8520 extsymcount = symcount - hdr->sh_info;
8521 extsymoff = hdr->sh_info;
8522 }
8523
8524 if (extsymcount == 0)
8525 continue;
8526
8527 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8528 NULL, NULL, NULL);
8529 if (isymbuf == NULL)
8530 return FALSE;
8531
8532 /* Read in any version definitions. */
8533 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8534 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8535 if (extversym == NULL)
8536 goto error_ret;
8537
8538 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8539 || (bfd_bread (extversym, versymhdr->sh_size, input)
8540 != versymhdr->sh_size))
8541 {
8542 free (extversym);
8543 error_ret:
8544 free (isymbuf);
8545 return FALSE;
8546 }
8547
8548 ever = extversym + extsymoff;
8549 isymend = isymbuf + extsymcount;
8550 for (isym = isymbuf; isym < isymend; isym++, ever++)
8551 {
8552 const char *name;
8553 Elf_Internal_Versym iver;
8554 unsigned short version_index;
8555
8556 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8557 || isym->st_shndx == SHN_UNDEF)
8558 continue;
8559
8560 name = bfd_elf_string_from_elf_section (input,
8561 hdr->sh_link,
8562 isym->st_name);
8563 if (strcmp (name, h->root.root.string) != 0)
8564 continue;
8565
8566 _bfd_elf_swap_versym_in (input, ever, &iver);
8567
d023c380
L
8568 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8569 && !(h->def_regular
8570 && h->forced_local))
c152c796
AM
8571 {
8572 /* If we have a non-hidden versioned sym, then it should
d023c380
L
8573 have provided a definition for the undefined sym unless
8574 it is defined in a non-shared object and forced local.
8575 */
c152c796
AM
8576 abort ();
8577 }
8578
8579 version_index = iver.vs_vers & VERSYM_VERSION;
8580 if (version_index == 1 || version_index == 2)
8581 {
8582 /* This is the base or first version. We can use it. */
8583 free (extversym);
8584 free (isymbuf);
8585 return TRUE;
8586 }
8587 }
8588
8589 free (extversym);
8590 free (isymbuf);
8591 }
8592
8593 return FALSE;
8594}
8595
8596/* Add an external symbol to the symbol table. This is called from
8597 the hash table traversal routine. When generating a shared object,
8598 we go through the symbol table twice. The first time we output
8599 anything that might have been forced to local scope in a version
8600 script. The second time we output the symbols that are still
8601 global symbols. */
8602
8603static bfd_boolean
8604elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8605{
a50b1753 8606 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
c152c796
AM
8607 struct elf_final_link_info *finfo = eoinfo->finfo;
8608 bfd_boolean strip;
8609 Elf_Internal_Sym sym;
8610 asection *input_sec;
8611 const struct elf_backend_data *bed;
6e0b88f1
AM
8612 long indx;
8613 int ret;
c152c796
AM
8614
8615 if (h->root.type == bfd_link_hash_warning)
8616 {
8617 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8618 if (h->root.type == bfd_link_hash_new)
8619 return TRUE;
8620 }
8621
8622 /* Decide whether to output this symbol in this pass. */
8623 if (eoinfo->localsyms)
8624 {
f5385ebf 8625 if (!h->forced_local)
c152c796
AM
8626 return TRUE;
8627 }
8628 else
8629 {
f5385ebf 8630 if (h->forced_local)
c152c796
AM
8631 return TRUE;
8632 }
8633
8634 bed = get_elf_backend_data (finfo->output_bfd);
8635
12ac1cf5 8636 if (h->root.type == bfd_link_hash_undefined)
c152c796 8637 {
12ac1cf5
NC
8638 /* If we have an undefined symbol reference here then it must have
8639 come from a shared library that is being linked in. (Undefined
98da7939
L
8640 references in regular files have already been handled unless
8641 they are in unreferenced sections which are removed by garbage
8642 collection). */
12ac1cf5
NC
8643 bfd_boolean ignore_undef = FALSE;
8644
8645 /* Some symbols may be special in that the fact that they're
8646 undefined can be safely ignored - let backend determine that. */
8647 if (bed->elf_backend_ignore_undef_symbol)
8648 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8649
8650 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 8651 if (!ignore_undef
12ac1cf5 8652 && h->ref_dynamic
98da7939 8653 && (!h->ref_regular || finfo->info->gc_sections)
12ac1cf5
NC
8654 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8655 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
c152c796 8656 {
12ac1cf5 8657 if (! (finfo->info->callbacks->undefined_symbol
98da7939
L
8658 (finfo->info, h->root.root.string,
8659 h->ref_regular ? NULL : h->root.u.undef.abfd,
12ac1cf5
NC
8660 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8661 {
17d078c5 8662 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
8663 eoinfo->failed = TRUE;
8664 return FALSE;
8665 }
c152c796
AM
8666 }
8667 }
8668
8669 /* We should also warn if a forced local symbol is referenced from
8670 shared libraries. */
8671 if (! finfo->info->relocatable
8672 && (! finfo->info->shared)
f5385ebf
AM
8673 && h->forced_local
8674 && h->ref_dynamic
8675 && !h->dynamic_def
8676 && !h->dynamic_weak
c152c796
AM
8677 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8678 {
17d078c5
AM
8679 bfd *def_bfd;
8680 const char *msg;
8681
8682 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8683 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8684 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8685 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8686 else
8687 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8688 def_bfd = finfo->output_bfd;
8689 if (h->root.u.def.section != bfd_abs_section_ptr)
8690 def_bfd = h->root.u.def.section->owner;
8691 (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd,
8692 h->root.root.string);
8693 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8694 eoinfo->failed = TRUE;
8695 return FALSE;
8696 }
8697
8698 /* We don't want to output symbols that have never been mentioned by
8699 a regular file, or that we have been told to strip. However, if
8700 h->indx is set to -2, the symbol is used by a reloc and we must
8701 output it. */
8702 if (h->indx == -2)
8703 strip = FALSE;
f5385ebf 8704 else if ((h->def_dynamic
77cfaee6
AM
8705 || h->ref_dynamic
8706 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8707 && !h->def_regular
8708 && !h->ref_regular)
c152c796
AM
8709 strip = TRUE;
8710 else if (finfo->info->strip == strip_all)
8711 strip = TRUE;
8712 else if (finfo->info->strip == strip_some
8713 && bfd_hash_lookup (finfo->info->keep_hash,
8714 h->root.root.string, FALSE, FALSE) == NULL)
8715 strip = TRUE;
8716 else if (finfo->info->strip_discarded
8717 && (h->root.type == bfd_link_hash_defined
8718 || h->root.type == bfd_link_hash_defweak)
8719 && elf_discarded_section (h->root.u.def.section))
8720 strip = TRUE;
9e2278f5
AM
8721 else if ((h->root.type == bfd_link_hash_undefined
8722 || h->root.type == bfd_link_hash_undefweak)
8723 && h->root.u.undef.abfd != NULL
8724 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8725 strip = TRUE;
c152c796
AM
8726 else
8727 strip = FALSE;
8728
8729 /* If we're stripping it, and it's not a dynamic symbol, there's
57ca8ac7
L
8730 nothing else to do unless it is a forced local symbol or a
8731 STT_GNU_IFUNC symbol. */
c152c796
AM
8732 if (strip
8733 && h->dynindx == -1
57ca8ac7 8734 && h->type != STT_GNU_IFUNC
f5385ebf 8735 && !h->forced_local)
c152c796
AM
8736 return TRUE;
8737
8738 sym.st_value = 0;
8739 sym.st_size = h->size;
8740 sym.st_other = h->other;
f5385ebf 8741 if (h->forced_local)
935bd1e0
L
8742 {
8743 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8744 /* Turn off visibility on local symbol. */
8745 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8746 }
3e7a7d11
NC
8747 else if (h->unique_global)
8748 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
8749 else if (h->root.type == bfd_link_hash_undefweak
8750 || h->root.type == bfd_link_hash_defweak)
8751 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8752 else
8753 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 8754 sym.st_target_internal = h->target_internal;
c152c796
AM
8755
8756 switch (h->root.type)
8757 {
8758 default:
8759 case bfd_link_hash_new:
8760 case bfd_link_hash_warning:
8761 abort ();
8762 return FALSE;
8763
8764 case bfd_link_hash_undefined:
8765 case bfd_link_hash_undefweak:
8766 input_sec = bfd_und_section_ptr;
8767 sym.st_shndx = SHN_UNDEF;
8768 break;
8769
8770 case bfd_link_hash_defined:
8771 case bfd_link_hash_defweak:
8772 {
8773 input_sec = h->root.u.def.section;
8774 if (input_sec->output_section != NULL)
8775 {
8776 sym.st_shndx =
8777 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8778 input_sec->output_section);
8779 if (sym.st_shndx == SHN_BAD)
8780 {
8781 (*_bfd_error_handler)
d003868e
AM
8782 (_("%B: could not find output section %A for input section %A"),
8783 finfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 8784 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
8785 eoinfo->failed = TRUE;
8786 return FALSE;
8787 }
8788
8789 /* ELF symbols in relocatable files are section relative,
8790 but in nonrelocatable files they are virtual
8791 addresses. */
8792 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8793 if (! finfo->info->relocatable)
8794 {
8795 sym.st_value += input_sec->output_section->vma;
8796 if (h->type == STT_TLS)
8797 {
430a16a5
NC
8798 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8799 if (tls_sec != NULL)
8800 sym.st_value -= tls_sec->vma;
8801 else
8802 {
8803 /* The TLS section may have been garbage collected. */
8804 BFD_ASSERT (finfo->info->gc_sections
8805 && !input_sec->gc_mark);
8806 }
c152c796
AM
8807 }
8808 }
8809 }
8810 else
8811 {
8812 BFD_ASSERT (input_sec->owner == NULL
8813 || (input_sec->owner->flags & DYNAMIC) != 0);
8814 sym.st_shndx = SHN_UNDEF;
8815 input_sec = bfd_und_section_ptr;
8816 }
8817 }
8818 break;
8819
8820 case bfd_link_hash_common:
8821 input_sec = h->root.u.c.p->section;
a4d8e49b 8822 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8823 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8824 break;
8825
8826 case bfd_link_hash_indirect:
8827 /* These symbols are created by symbol versioning. They point
8828 to the decorated version of the name. For example, if the
8829 symbol foo@@GNU_1.2 is the default, which should be used when
8830 foo is used with no version, then we add an indirect symbol
8831 foo which points to foo@@GNU_1.2. We ignore these symbols,
8832 since the indirected symbol is already in the hash table. */
8833 return TRUE;
8834 }
8835
8836 /* Give the processor backend a chance to tweak the symbol value,
8837 and also to finish up anything that needs to be done for this
8838 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 8839 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 8840 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 8841 if ((h->type == STT_GNU_IFUNC
5f35ea9c 8842 && h->def_regular
3aa14d16
L
8843 && !finfo->info->relocatable)
8844 || ((h->dynindx != -1
8845 || h->forced_local)
8846 && ((finfo->info->shared
8847 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8848 || h->root.type != bfd_link_hash_undefweak))
8849 || !h->forced_local)
8850 && elf_hash_table (finfo->info)->dynamic_sections_created))
c152c796
AM
8851 {
8852 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8853 (finfo->output_bfd, finfo->info, h, &sym)))
8854 {
8855 eoinfo->failed = TRUE;
8856 return FALSE;
8857 }
8858 }
8859
8860 /* If we are marking the symbol as undefined, and there are no
8861 non-weak references to this symbol from a regular object, then
8862 mark the symbol as weak undefined; if there are non-weak
8863 references, mark the symbol as strong. We can't do this earlier,
8864 because it might not be marked as undefined until the
8865 finish_dynamic_symbol routine gets through with it. */
8866 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8867 && h->ref_regular
c152c796
AM
8868 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8869 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8870 {
8871 int bindtype;
2955ec4c
L
8872 unsigned int type = ELF_ST_TYPE (sym.st_info);
8873
8874 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8875 if (type == STT_GNU_IFUNC)
8876 type = STT_FUNC;
c152c796 8877
f5385ebf 8878 if (h->ref_regular_nonweak)
c152c796
AM
8879 bindtype = STB_GLOBAL;
8880 else
8881 bindtype = STB_WEAK;
2955ec4c 8882 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
8883 }
8884
bda987c2
CD
8885 /* If this is a symbol defined in a dynamic library, don't use the
8886 symbol size from the dynamic library. Relinking an executable
8887 against a new library may introduce gratuitous changes in the
8888 executable's symbols if we keep the size. */
8889 if (sym.st_shndx == SHN_UNDEF
8890 && !h->def_regular
8891 && h->def_dynamic)
8892 sym.st_size = 0;
8893
c152c796
AM
8894 /* If a non-weak symbol with non-default visibility is not defined
8895 locally, it is a fatal error. */
8896 if (! finfo->info->relocatable
8897 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8898 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8899 && h->root.type == bfd_link_hash_undefined
f5385ebf 8900 && !h->def_regular)
c152c796 8901 {
17d078c5
AM
8902 const char *msg;
8903
8904 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8905 msg = _("%B: protected symbol `%s' isn't defined");
8906 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8907 msg = _("%B: internal symbol `%s' isn't defined");
8908 else
8909 msg = _("%B: hidden symbol `%s' isn't defined");
8910 (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string);
8911 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8912 eoinfo->failed = TRUE;
8913 return FALSE;
8914 }
8915
8916 /* If this symbol should be put in the .dynsym section, then put it
8917 there now. We already know the symbol index. We also fill in
8918 the entry in the .hash section. */
8919 if (h->dynindx != -1
8920 && elf_hash_table (finfo->info)->dynamic_sections_created)
8921 {
c152c796
AM
8922 bfd_byte *esym;
8923
8924 sym.st_name = h->dynstr_index;
8925 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
c0d5a53d
L
8926 if (! check_dynsym (finfo->output_bfd, &sym))
8927 {
8928 eoinfo->failed = TRUE;
8929 return FALSE;
8930 }
c152c796
AM
8931 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8932
fdc90cb4
JJ
8933 if (finfo->hash_sec != NULL)
8934 {
8935 size_t hash_entry_size;
8936 bfd_byte *bucketpos;
8937 bfd_vma chain;
41198d0c
L
8938 size_t bucketcount;
8939 size_t bucket;
8940
8941 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8942 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8943
8944 hash_entry_size
8945 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8946 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8947 + (bucket + 2) * hash_entry_size);
8948 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8949 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8950 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8951 ((bfd_byte *) finfo->hash_sec->contents
8952 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8953 }
c152c796
AM
8954
8955 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8956 {
8957 Elf_Internal_Versym iversym;
8958 Elf_External_Versym *eversym;
8959
f5385ebf 8960 if (!h->def_regular)
c152c796
AM
8961 {
8962 if (h->verinfo.verdef == NULL)
8963 iversym.vs_vers = 0;
8964 else
8965 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8966 }
8967 else
8968 {
8969 if (h->verinfo.vertree == NULL)
8970 iversym.vs_vers = 1;
8971 else
8972 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
8973 if (finfo->info->create_default_symver)
8974 iversym.vs_vers++;
c152c796
AM
8975 }
8976
f5385ebf 8977 if (h->hidden)
c152c796
AM
8978 iversym.vs_vers |= VERSYM_HIDDEN;
8979
8980 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8981 eversym += h->dynindx;
8982 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8983 }
8984 }
8985
8986 /* If we're stripping it, then it was just a dynamic symbol, and
8987 there's nothing else to do. */
8988 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8989 return TRUE;
8990
6e0b88f1
AM
8991 indx = bfd_get_symcount (finfo->output_bfd);
8992 ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h);
8993 if (ret == 0)
c152c796
AM
8994 {
8995 eoinfo->failed = TRUE;
8996 return FALSE;
8997 }
6e0b88f1
AM
8998 else if (ret == 1)
8999 h->indx = indx;
9000 else if (h->indx == -2)
9001 abort();
c152c796
AM
9002
9003 return TRUE;
9004}
9005
cdd3575c
AM
9006/* Return TRUE if special handling is done for relocs in SEC against
9007 symbols defined in discarded sections. */
9008
c152c796
AM
9009static bfd_boolean
9010elf_section_ignore_discarded_relocs (asection *sec)
9011{
9012 const struct elf_backend_data *bed;
9013
cdd3575c
AM
9014 switch (sec->sec_info_type)
9015 {
9016 case ELF_INFO_TYPE_STABS:
9017 case ELF_INFO_TYPE_EH_FRAME:
9018 return TRUE;
9019 default:
9020 break;
9021 }
c152c796
AM
9022
9023 bed = get_elf_backend_data (sec->owner);
9024 if (bed->elf_backend_ignore_discarded_relocs != NULL
9025 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9026 return TRUE;
9027
9028 return FALSE;
9029}
9030
9e66c942
AM
9031/* Return a mask saying how ld should treat relocations in SEC against
9032 symbols defined in discarded sections. If this function returns
9033 COMPLAIN set, ld will issue a warning message. If this function
9034 returns PRETEND set, and the discarded section was link-once and the
9035 same size as the kept link-once section, ld will pretend that the
9036 symbol was actually defined in the kept section. Otherwise ld will
9037 zero the reloc (at least that is the intent, but some cooperation by
9038 the target dependent code is needed, particularly for REL targets). */
9039
8a696751
AM
9040unsigned int
9041_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9042{
9e66c942 9043 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9044 return PRETEND;
cdd3575c
AM
9045
9046 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9047 return 0;
cdd3575c
AM
9048
9049 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9050 return 0;
cdd3575c 9051
9e66c942 9052 return COMPLAIN | PRETEND;
cdd3575c
AM
9053}
9054
3d7f7666
L
9055/* Find a match between a section and a member of a section group. */
9056
9057static asection *
c0f00686
L
9058match_group_member (asection *sec, asection *group,
9059 struct bfd_link_info *info)
3d7f7666
L
9060{
9061 asection *first = elf_next_in_group (group);
9062 asection *s = first;
9063
9064 while (s != NULL)
9065 {
c0f00686 9066 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9067 return s;
9068
83180ade 9069 s = elf_next_in_group (s);
3d7f7666
L
9070 if (s == first)
9071 break;
9072 }
9073
9074 return NULL;
9075}
9076
01b3c8ab 9077/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9078 to replace it. Return the replacement if it is OK. Otherwise return
9079 NULL. */
01b3c8ab
L
9080
9081asection *
c0f00686 9082_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9083{
9084 asection *kept;
9085
9086 kept = sec->kept_section;
9087 if (kept != NULL)
9088 {
c2370991 9089 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9090 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9091 if (kept != NULL
9092 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9093 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9094 kept = NULL;
c2370991 9095 sec->kept_section = kept;
01b3c8ab
L
9096 }
9097 return kept;
9098}
9099
c152c796
AM
9100/* Link an input file into the linker output file. This function
9101 handles all the sections and relocations of the input file at once.
9102 This is so that we only have to read the local symbols once, and
9103 don't have to keep them in memory. */
9104
9105static bfd_boolean
9106elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9107{
ece5ef60 9108 int (*relocate_section)
c152c796
AM
9109 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9110 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9111 bfd *output_bfd;
9112 Elf_Internal_Shdr *symtab_hdr;
9113 size_t locsymcount;
9114 size_t extsymoff;
9115 Elf_Internal_Sym *isymbuf;
9116 Elf_Internal_Sym *isym;
9117 Elf_Internal_Sym *isymend;
9118 long *pindex;
9119 asection **ppsection;
9120 asection *o;
9121 const struct elf_backend_data *bed;
c152c796 9122 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9123 bfd_size_type address_size;
9124 bfd_vma r_type_mask;
9125 int r_sym_shift;
c152c796
AM
9126
9127 output_bfd = finfo->output_bfd;
9128 bed = get_elf_backend_data (output_bfd);
9129 relocate_section = bed->elf_backend_relocate_section;
9130
9131 /* If this is a dynamic object, we don't want to do anything here:
9132 we don't want the local symbols, and we don't want the section
9133 contents. */
9134 if ((input_bfd->flags & DYNAMIC) != 0)
9135 return TRUE;
9136
c152c796
AM
9137 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9138 if (elf_bad_symtab (input_bfd))
9139 {
9140 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9141 extsymoff = 0;
9142 }
9143 else
9144 {
9145 locsymcount = symtab_hdr->sh_info;
9146 extsymoff = symtab_hdr->sh_info;
9147 }
9148
9149 /* Read the local symbols. */
9150 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9151 if (isymbuf == NULL && locsymcount != 0)
9152 {
9153 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9154 finfo->internal_syms,
9155 finfo->external_syms,
9156 finfo->locsym_shndx);
9157 if (isymbuf == NULL)
9158 return FALSE;
9159 }
9160
9161 /* Find local symbol sections and adjust values of symbols in
9162 SEC_MERGE sections. Write out those local symbols we know are
9163 going into the output file. */
9164 isymend = isymbuf + locsymcount;
9165 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9166 isym < isymend;
9167 isym++, pindex++, ppsection++)
9168 {
9169 asection *isec;
9170 const char *name;
9171 Elf_Internal_Sym osym;
6e0b88f1
AM
9172 long indx;
9173 int ret;
c152c796
AM
9174
9175 *pindex = -1;
9176
9177 if (elf_bad_symtab (input_bfd))
9178 {
9179 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9180 {
9181 *ppsection = NULL;
9182 continue;
9183 }
9184 }
9185
9186 if (isym->st_shndx == SHN_UNDEF)
9187 isec = bfd_und_section_ptr;
c152c796
AM
9188 else if (isym->st_shndx == SHN_ABS)
9189 isec = bfd_abs_section_ptr;
9190 else if (isym->st_shndx == SHN_COMMON)
9191 isec = bfd_com_section_ptr;
9192 else
9193 {
cb33740c
AM
9194 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9195 if (isec == NULL)
9196 {
9197 /* Don't attempt to output symbols with st_shnx in the
9198 reserved range other than SHN_ABS and SHN_COMMON. */
9199 *ppsection = NULL;
9200 continue;
9201 }
9202 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9203 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9204 isym->st_value =
9205 _bfd_merged_section_offset (output_bfd, &isec,
9206 elf_section_data (isec)->sec_info,
9207 isym->st_value);
c152c796
AM
9208 }
9209
9210 *ppsection = isec;
9211
9212 /* Don't output the first, undefined, symbol. */
9213 if (ppsection == finfo->sections)
9214 continue;
9215
9216 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9217 {
9218 /* We never output section symbols. Instead, we use the
9219 section symbol of the corresponding section in the output
9220 file. */
9221 continue;
9222 }
9223
9224 /* If we are stripping all symbols, we don't want to output this
9225 one. */
9226 if (finfo->info->strip == strip_all)
9227 continue;
9228
9229 /* If we are discarding all local symbols, we don't want to
9230 output this one. If we are generating a relocatable output
9231 file, then some of the local symbols may be required by
9232 relocs; we output them below as we discover that they are
9233 needed. */
9234 if (finfo->info->discard == discard_all)
9235 continue;
9236
9237 /* If this symbol is defined in a section which we are
f02571c5
AM
9238 discarding, we don't need to keep it. */
9239 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9240 && isym->st_shndx < SHN_LORESERVE
9241 && bfd_section_removed_from_list (output_bfd,
9242 isec->output_section))
e75a280b
L
9243 continue;
9244
c152c796
AM
9245 /* Get the name of the symbol. */
9246 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9247 isym->st_name);
9248 if (name == NULL)
9249 return FALSE;
9250
9251 /* See if we are discarding symbols with this name. */
9252 if ((finfo->info->strip == strip_some
9253 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9254 == NULL))
9255 || (((finfo->info->discard == discard_sec_merge
9256 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9257 || finfo->info->discard == discard_l)
9258 && bfd_is_local_label_name (input_bfd, name)))
9259 continue;
9260
c152c796
AM
9261 osym = *isym;
9262
9263 /* Adjust the section index for the output file. */
9264 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9265 isec->output_section);
9266 if (osym.st_shndx == SHN_BAD)
9267 return FALSE;
9268
c152c796
AM
9269 /* ELF symbols in relocatable files are section relative, but
9270 in executable files they are virtual addresses. Note that
9271 this code assumes that all ELF sections have an associated
9272 BFD section with a reasonable value for output_offset; below
9273 we assume that they also have a reasonable value for
9274 output_section. Any special sections must be set up to meet
9275 these requirements. */
9276 osym.st_value += isec->output_offset;
9277 if (! finfo->info->relocatable)
9278 {
9279 osym.st_value += isec->output_section->vma;
9280 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9281 {
9282 /* STT_TLS symbols are relative to PT_TLS segment base. */
9283 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9284 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9285 }
9286 }
9287
6e0b88f1
AM
9288 indx = bfd_get_symcount (output_bfd);
9289 ret = elf_link_output_sym (finfo, name, &osym, isec, NULL);
9290 if (ret == 0)
c152c796 9291 return FALSE;
6e0b88f1
AM
9292 else if (ret == 1)
9293 *pindex = indx;
c152c796
AM
9294 }
9295
310fd250
L
9296 if (bed->s->arch_size == 32)
9297 {
9298 r_type_mask = 0xff;
9299 r_sym_shift = 8;
9300 address_size = 4;
9301 }
9302 else
9303 {
9304 r_type_mask = 0xffffffff;
9305 r_sym_shift = 32;
9306 address_size = 8;
9307 }
9308
c152c796
AM
9309 /* Relocate the contents of each section. */
9310 sym_hashes = elf_sym_hashes (input_bfd);
9311 for (o = input_bfd->sections; o != NULL; o = o->next)
9312 {
9313 bfd_byte *contents;
9314
9315 if (! o->linker_mark)
9316 {
9317 /* This section was omitted from the link. */
9318 continue;
9319 }
9320
bcacc0f5
AM
9321 if (finfo->info->relocatable
9322 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9323 {
9324 /* Deal with the group signature symbol. */
9325 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9326 unsigned long symndx = sec_data->this_hdr.sh_info;
9327 asection *osec = o->output_section;
9328
9329 if (symndx >= locsymcount
9330 || (elf_bad_symtab (input_bfd)
9331 && finfo->sections[symndx] == NULL))
9332 {
9333 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9334 while (h->root.type == bfd_link_hash_indirect
9335 || h->root.type == bfd_link_hash_warning)
9336 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9337 /* Arrange for symbol to be output. */
9338 h->indx = -2;
9339 elf_section_data (osec)->this_hdr.sh_info = -2;
9340 }
9341 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9342 {
9343 /* We'll use the output section target_index. */
9344 asection *sec = finfo->sections[symndx]->output_section;
9345 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9346 }
9347 else
9348 {
9349 if (finfo->indices[symndx] == -1)
9350 {
9351 /* Otherwise output the local symbol now. */
9352 Elf_Internal_Sym sym = isymbuf[symndx];
9353 asection *sec = finfo->sections[symndx]->output_section;
9354 const char *name;
6e0b88f1
AM
9355 long indx;
9356 int ret;
bcacc0f5
AM
9357
9358 name = bfd_elf_string_from_elf_section (input_bfd,
9359 symtab_hdr->sh_link,
9360 sym.st_name);
9361 if (name == NULL)
9362 return FALSE;
9363
9364 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9365 sec);
9366 if (sym.st_shndx == SHN_BAD)
9367 return FALSE;
9368
9369 sym.st_value += o->output_offset;
9370
6e0b88f1
AM
9371 indx = bfd_get_symcount (output_bfd);
9372 ret = elf_link_output_sym (finfo, name, &sym, o, NULL);
9373 if (ret == 0)
bcacc0f5 9374 return FALSE;
6e0b88f1
AM
9375 else if (ret == 1)
9376 finfo->indices[symndx] = indx;
9377 else
9378 abort ();
bcacc0f5
AM
9379 }
9380 elf_section_data (osec)->this_hdr.sh_info
9381 = finfo->indices[symndx];
9382 }
9383 }
9384
c152c796 9385 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9386 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9387 continue;
9388
9389 if ((o->flags & SEC_LINKER_CREATED) != 0)
9390 {
9391 /* Section was created by _bfd_elf_link_create_dynamic_sections
9392 or somesuch. */
9393 continue;
9394 }
9395
9396 /* Get the contents of the section. They have been cached by a
9397 relaxation routine. Note that o is a section in an input
9398 file, so the contents field will not have been set by any of
9399 the routines which work on output files. */
9400 if (elf_section_data (o)->this_hdr.contents != NULL)
9401 contents = elf_section_data (o)->this_hdr.contents;
9402 else
9403 {
9404 contents = finfo->contents;
4a114e3e 9405 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9406 return FALSE;
9407 }
9408
9409 if ((o->flags & SEC_RELOC) != 0)
9410 {
9411 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9412 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9413 int action_discarded;
ece5ef60 9414 int ret;
c152c796
AM
9415
9416 /* Get the swapped relocs. */
9417 internal_relocs
9418 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9419 finfo->internal_relocs, FALSE);
9420 if (internal_relocs == NULL
9421 && o->reloc_count > 0)
9422 return FALSE;
9423
310fd250
L
9424 /* We need to reverse-copy input .ctors/.dtors sections if
9425 they are placed in .init_array/.finit_array for output. */
9426 if (o->size > address_size
9427 && ((strncmp (o->name, ".ctors", 6) == 0
9428 && strcmp (o->output_section->name,
9429 ".init_array") == 0)
9430 || (strncmp (o->name, ".dtors", 6) == 0
9431 && strcmp (o->output_section->name,
9432 ".fini_array") == 0))
9433 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9434 {
310fd250
L
9435 if (o->size != o->reloc_count * address_size)
9436 {
9437 (*_bfd_error_handler)
9438 (_("error: %B: size of section %A is not "
9439 "multiple of address size"),
9440 input_bfd, o);
9441 bfd_set_error (bfd_error_on_input);
9442 return FALSE;
9443 }
9444 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9445 }
9446
0f02bbd9 9447 action_discarded = -1;
c152c796 9448 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9449 action_discarded = (*bed->action_discarded) (o);
9450
9451 /* Run through the relocs evaluating complex reloc symbols and
9452 looking for relocs against symbols from discarded sections
9453 or section symbols from removed link-once sections.
9454 Complain about relocs against discarded sections. Zero
9455 relocs against removed link-once sections. */
9456
9457 rel = internal_relocs;
9458 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9459 for ( ; rel < relend; rel++)
c152c796 9460 {
0f02bbd9
AM
9461 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9462 unsigned int s_type;
9463 asection **ps, *sec;
9464 struct elf_link_hash_entry *h = NULL;
9465 const char *sym_name;
c152c796 9466
0f02bbd9
AM
9467 if (r_symndx == STN_UNDEF)
9468 continue;
c152c796 9469
0f02bbd9
AM
9470 if (r_symndx >= locsymcount
9471 || (elf_bad_symtab (input_bfd)
9472 && finfo->sections[r_symndx] == NULL))
9473 {
9474 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9475
0f02bbd9
AM
9476 /* Badly formatted input files can contain relocs that
9477 reference non-existant symbols. Check here so that
9478 we do not seg fault. */
9479 if (h == NULL)
c152c796 9480 {
0f02bbd9 9481 char buffer [32];
dce669a1 9482
0f02bbd9
AM
9483 sprintf_vma (buffer, rel->r_info);
9484 (*_bfd_error_handler)
9485 (_("error: %B contains a reloc (0x%s) for section %A "
9486 "that references a non-existent global symbol"),
9487 input_bfd, o, buffer);
9488 bfd_set_error (bfd_error_bad_value);
9489 return FALSE;
9490 }
3b36f7e6 9491
0f02bbd9
AM
9492 while (h->root.type == bfd_link_hash_indirect
9493 || h->root.type == bfd_link_hash_warning)
9494 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9495
0f02bbd9 9496 s_type = h->type;
cdd3575c 9497
0f02bbd9
AM
9498 ps = NULL;
9499 if (h->root.type == bfd_link_hash_defined
9500 || h->root.type == bfd_link_hash_defweak)
9501 ps = &h->root.u.def.section;
9502
9503 sym_name = h->root.root.string;
9504 }
9505 else
9506 {
9507 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9508
9509 s_type = ELF_ST_TYPE (sym->st_info);
9510 ps = &finfo->sections[r_symndx];
9511 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9512 sym, *ps);
9513 }
c152c796 9514
c301e700
DD
9515 if ((s_type == STT_RELC || s_type == STT_SRELC)
9516 && !finfo->info->relocatable)
0f02bbd9
AM
9517 {
9518 bfd_vma val;
9519 bfd_vma dot = (rel->r_offset
9520 + o->output_offset + o->output_section->vma);
9521#ifdef DEBUG
9522 printf ("Encountered a complex symbol!");
9523 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
9524 input_bfd->filename, o->name,
9525 (long) (rel - internal_relocs));
0f02bbd9
AM
9526 printf (" symbol: idx %8.8lx, name %s\n",
9527 r_symndx, sym_name);
9528 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9529 (unsigned long) rel->r_info,
9530 (unsigned long) rel->r_offset);
9531#endif
9532 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9533 isymbuf, locsymcount, s_type == STT_SRELC))
9534 return FALSE;
9535
9536 /* Symbol evaluated OK. Update to absolute value. */
9537 set_symbol_value (input_bfd, isymbuf, locsymcount,
9538 r_symndx, val);
9539 continue;
9540 }
9541
9542 if (action_discarded != -1 && ps != NULL)
9543 {
cdd3575c
AM
9544 /* Complain if the definition comes from a
9545 discarded section. */
9546 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9547 {
cf35638d 9548 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 9549 if (action_discarded & COMPLAIN)
e1fffbe6
AM
9550 (*finfo->info->callbacks->einfo)
9551 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9552 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9553 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9554
87e5235d 9555 /* Try to do the best we can to support buggy old
e0ae6d6f 9556 versions of gcc. Pretend that the symbol is
87e5235d
AM
9557 really defined in the kept linkonce section.
9558 FIXME: This is quite broken. Modifying the
9559 symbol here means we will be changing all later
e0ae6d6f 9560 uses of the symbol, not just in this section. */
0f02bbd9 9561 if (action_discarded & PRETEND)
87e5235d 9562 {
01b3c8ab
L
9563 asection *kept;
9564
c0f00686
L
9565 kept = _bfd_elf_check_kept_section (sec,
9566 finfo->info);
01b3c8ab 9567 if (kept != NULL)
87e5235d
AM
9568 {
9569 *ps = kept;
9570 continue;
9571 }
9572 }
c152c796
AM
9573 }
9574 }
9575 }
9576
9577 /* Relocate the section by invoking a back end routine.
9578
9579 The back end routine is responsible for adjusting the
9580 section contents as necessary, and (if using Rela relocs
9581 and generating a relocatable output file) adjusting the
9582 reloc addend as necessary.
9583
9584 The back end routine does not have to worry about setting
9585 the reloc address or the reloc symbol index.
9586
9587 The back end routine is given a pointer to the swapped in
9588 internal symbols, and can access the hash table entries
9589 for the external symbols via elf_sym_hashes (input_bfd).
9590
9591 When generating relocatable output, the back end routine
9592 must handle STB_LOCAL/STT_SECTION symbols specially. The
9593 output symbol is going to be a section symbol
9594 corresponding to the output section, which will require
9595 the addend to be adjusted. */
9596
ece5ef60 9597 ret = (*relocate_section) (output_bfd, finfo->info,
c152c796
AM
9598 input_bfd, o, contents,
9599 internal_relocs,
9600 isymbuf,
ece5ef60
AM
9601 finfo->sections);
9602 if (!ret)
c152c796
AM
9603 return FALSE;
9604
ece5ef60
AM
9605 if (ret == 2
9606 || finfo->info->relocatable
9607 || finfo->info->emitrelocations)
c152c796
AM
9608 {
9609 Elf_Internal_Rela *irela;
d4730f92 9610 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
9611 bfd_vma last_offset;
9612 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
9613 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9614 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 9615 unsigned int next_erel;
c152c796 9616 bfd_boolean rela_normal;
d4730f92 9617 struct bfd_elf_section_data *esdi, *esdo;
c152c796 9618
d4730f92
BS
9619 esdi = elf_section_data (o);
9620 esdo = elf_section_data (o->output_section);
9621 rela_normal = FALSE;
c152c796
AM
9622
9623 /* Adjust the reloc addresses and symbol indices. */
9624
9625 irela = internal_relocs;
9626 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
9627 rel_hash = esdo->rel.hashes + esdo->rel.count;
9628 /* We start processing the REL relocs, if any. When we reach
9629 IRELAMID in the loop, we switch to the RELA relocs. */
9630 irelamid = irela;
9631 if (esdi->rel.hdr != NULL)
9632 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9633 * bed->s->int_rels_per_ext_rel);
eac338cf 9634 rel_hash_list = rel_hash;
d4730f92 9635 rela_hash_list = NULL;
c152c796
AM
9636 last_offset = o->output_offset;
9637 if (!finfo->info->relocatable)
9638 last_offset += o->output_section->vma;
9639 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9640 {
9641 unsigned long r_symndx;
9642 asection *sec;
9643 Elf_Internal_Sym sym;
9644
9645 if (next_erel == bed->s->int_rels_per_ext_rel)
9646 {
9647 rel_hash++;
9648 next_erel = 0;
9649 }
9650
d4730f92
BS
9651 if (irela == irelamid)
9652 {
9653 rel_hash = esdo->rela.hashes + esdo->rela.count;
9654 rela_hash_list = rel_hash;
9655 rela_normal = bed->rela_normal;
9656 }
9657
c152c796
AM
9658 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9659 finfo->info, o,
9660 irela->r_offset);
9661 if (irela->r_offset >= (bfd_vma) -2)
9662 {
9663 /* This is a reloc for a deleted entry or somesuch.
9664 Turn it into an R_*_NONE reloc, at the same
9665 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9666 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9667 being ordered. */
9668 irela->r_offset = last_offset;
9669 irela->r_info = 0;
9670 irela->r_addend = 0;
9671 continue;
9672 }
9673
9674 irela->r_offset += o->output_offset;
9675
9676 /* Relocs in an executable have to be virtual addresses. */
9677 if (!finfo->info->relocatable)
9678 irela->r_offset += o->output_section->vma;
9679
9680 last_offset = irela->r_offset;
9681
9682 r_symndx = irela->r_info >> r_sym_shift;
9683 if (r_symndx == STN_UNDEF)
9684 continue;
9685
9686 if (r_symndx >= locsymcount
9687 || (elf_bad_symtab (input_bfd)
9688 && finfo->sections[r_symndx] == NULL))
9689 {
9690 struct elf_link_hash_entry *rh;
9691 unsigned long indx;
9692
9693 /* This is a reloc against a global symbol. We
9694 have not yet output all the local symbols, so
9695 we do not know the symbol index of any global
9696 symbol. We set the rel_hash entry for this
9697 reloc to point to the global hash table entry
9698 for this symbol. The symbol index is then
ee75fd95 9699 set at the end of bfd_elf_final_link. */
c152c796
AM
9700 indx = r_symndx - extsymoff;
9701 rh = elf_sym_hashes (input_bfd)[indx];
9702 while (rh->root.type == bfd_link_hash_indirect
9703 || rh->root.type == bfd_link_hash_warning)
9704 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9705
9706 /* Setting the index to -2 tells
9707 elf_link_output_extsym that this symbol is
9708 used by a reloc. */
9709 BFD_ASSERT (rh->indx < 0);
9710 rh->indx = -2;
9711
9712 *rel_hash = rh;
9713
9714 continue;
9715 }
9716
9717 /* This is a reloc against a local symbol. */
9718
9719 *rel_hash = NULL;
9720 sym = isymbuf[r_symndx];
9721 sec = finfo->sections[r_symndx];
9722 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9723 {
9724 /* I suppose the backend ought to fill in the
9725 section of any STT_SECTION symbol against a
6a8d1586 9726 processor specific section. */
cf35638d 9727 r_symndx = STN_UNDEF;
6a8d1586
AM
9728 if (bfd_is_abs_section (sec))
9729 ;
c152c796
AM
9730 else if (sec == NULL || sec->owner == NULL)
9731 {
9732 bfd_set_error (bfd_error_bad_value);
9733 return FALSE;
9734 }
9735 else
9736 {
6a8d1586
AM
9737 asection *osec = sec->output_section;
9738
9739 /* If we have discarded a section, the output
9740 section will be the absolute section. In
ab96bf03
AM
9741 case of discarded SEC_MERGE sections, use
9742 the kept section. relocate_section should
9743 have already handled discarded linkonce
9744 sections. */
6a8d1586
AM
9745 if (bfd_is_abs_section (osec)
9746 && sec->kept_section != NULL
9747 && sec->kept_section->output_section != NULL)
9748 {
9749 osec = sec->kept_section->output_section;
9750 irela->r_addend -= osec->vma;
9751 }
9752
9753 if (!bfd_is_abs_section (osec))
9754 {
9755 r_symndx = osec->target_index;
cf35638d 9756 if (r_symndx == STN_UNDEF)
74541ad4
AM
9757 {
9758 struct elf_link_hash_table *htab;
9759 asection *oi;
9760
9761 htab = elf_hash_table (finfo->info);
9762 oi = htab->text_index_section;
9763 if ((osec->flags & SEC_READONLY) == 0
9764 && htab->data_index_section != NULL)
9765 oi = htab->data_index_section;
9766
9767 if (oi != NULL)
9768 {
9769 irela->r_addend += osec->vma - oi->vma;
9770 r_symndx = oi->target_index;
9771 }
9772 }
9773
cf35638d 9774 BFD_ASSERT (r_symndx != STN_UNDEF);
6a8d1586 9775 }
c152c796
AM
9776 }
9777
9778 /* Adjust the addend according to where the
9779 section winds up in the output section. */
9780 if (rela_normal)
9781 irela->r_addend += sec->output_offset;
9782 }
9783 else
9784 {
9785 if (finfo->indices[r_symndx] == -1)
9786 {
9787 unsigned long shlink;
9788 const char *name;
9789 asection *osec;
6e0b88f1 9790 long indx;
c152c796
AM
9791
9792 if (finfo->info->strip == strip_all)
9793 {
9794 /* You can't do ld -r -s. */
9795 bfd_set_error (bfd_error_invalid_operation);
9796 return FALSE;
9797 }
9798
9799 /* This symbol was skipped earlier, but
9800 since it is needed by a reloc, we
9801 must output it now. */
9802 shlink = symtab_hdr->sh_link;
9803 name = (bfd_elf_string_from_elf_section
9804 (input_bfd, shlink, sym.st_name));
9805 if (name == NULL)
9806 return FALSE;
9807
9808 osec = sec->output_section;
9809 sym.st_shndx =
9810 _bfd_elf_section_from_bfd_section (output_bfd,
9811 osec);
9812 if (sym.st_shndx == SHN_BAD)
9813 return FALSE;
9814
9815 sym.st_value += sec->output_offset;
9816 if (! finfo->info->relocatable)
9817 {
9818 sym.st_value += osec->vma;
9819 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9820 {
9821 /* STT_TLS symbols are relative to PT_TLS
9822 segment base. */
9823 BFD_ASSERT (elf_hash_table (finfo->info)
9824 ->tls_sec != NULL);
9825 sym.st_value -= (elf_hash_table (finfo->info)
9826 ->tls_sec->vma);
9827 }
9828 }
9829
6e0b88f1
AM
9830 indx = bfd_get_symcount (output_bfd);
9831 ret = elf_link_output_sym (finfo, name, &sym, sec,
9832 NULL);
9833 if (ret == 0)
c152c796 9834 return FALSE;
6e0b88f1
AM
9835 else if (ret == 1)
9836 finfo->indices[r_symndx] = indx;
9837 else
9838 abort ();
c152c796
AM
9839 }
9840
9841 r_symndx = finfo->indices[r_symndx];
9842 }
9843
9844 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9845 | (irela->r_info & r_type_mask));
9846 }
9847
9848 /* Swap out the relocs. */
d4730f92
BS
9849 input_rel_hdr = esdi->rel.hdr;
9850 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 9851 {
d4730f92
BS
9852 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9853 input_rel_hdr,
9854 internal_relocs,
9855 rel_hash_list))
9856 return FALSE;
c152c796
AM
9857 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9858 * bed->s->int_rels_per_ext_rel);
eac338cf 9859 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
9860 }
9861
9862 input_rela_hdr = esdi->rela.hdr;
9863 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9864 {
eac338cf 9865 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 9866 input_rela_hdr,
eac338cf 9867 internal_relocs,
d4730f92 9868 rela_hash_list))
c152c796
AM
9869 return FALSE;
9870 }
9871 }
9872 }
9873
9874 /* Write out the modified section contents. */
9875 if (bed->elf_backend_write_section
c7b8f16e
JB
9876 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9877 contents))
c152c796
AM
9878 {
9879 /* Section written out. */
9880 }
9881 else switch (o->sec_info_type)
9882 {
9883 case ELF_INFO_TYPE_STABS:
9884 if (! (_bfd_write_section_stabs
9885 (output_bfd,
9886 &elf_hash_table (finfo->info)->stab_info,
9887 o, &elf_section_data (o)->sec_info, contents)))
9888 return FALSE;
9889 break;
9890 case ELF_INFO_TYPE_MERGE:
9891 if (! _bfd_write_merged_section (output_bfd, o,
9892 elf_section_data (o)->sec_info))
9893 return FALSE;
9894 break;
9895 case ELF_INFO_TYPE_EH_FRAME:
9896 {
9897 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9898 o, contents))
9899 return FALSE;
9900 }
9901 break;
9902 default:
9903 {
5dabe785 9904 /* FIXME: octets_per_byte. */
310fd250
L
9905 if (! (o->flags & SEC_EXCLUDE))
9906 {
9907 file_ptr offset = (file_ptr) o->output_offset;
9908 bfd_size_type todo = o->size;
9909 if ((o->flags & SEC_ELF_REVERSE_COPY))
9910 {
9911 /* Reverse-copy input section to output. */
9912 do
9913 {
9914 todo -= address_size;
9915 if (! bfd_set_section_contents (output_bfd,
9916 o->output_section,
9917 contents + todo,
9918 offset,
9919 address_size))
9920 return FALSE;
9921 if (todo == 0)
9922 break;
9923 offset += address_size;
9924 }
9925 while (1);
9926 }
9927 else if (! bfd_set_section_contents (output_bfd,
9928 o->output_section,
9929 contents,
9930 offset, todo))
9931 return FALSE;
9932 }
c152c796
AM
9933 }
9934 break;
9935 }
9936 }
9937
9938 return TRUE;
9939}
9940
9941/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9942 requested by the linker, and does not come from any input file. This
c152c796
AM
9943 is used to build constructor and destructor tables when linking
9944 with -Ur. */
9945
9946static bfd_boolean
9947elf_reloc_link_order (bfd *output_bfd,
9948 struct bfd_link_info *info,
9949 asection *output_section,
9950 struct bfd_link_order *link_order)
9951{
9952 reloc_howto_type *howto;
9953 long indx;
9954 bfd_vma offset;
9955 bfd_vma addend;
d4730f92 9956 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
9957 struct elf_link_hash_entry **rel_hash_ptr;
9958 Elf_Internal_Shdr *rel_hdr;
9959 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9960 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9961 bfd_byte *erel;
9962 unsigned int i;
d4730f92 9963 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
9964
9965 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9966 if (howto == NULL)
9967 {
9968 bfd_set_error (bfd_error_bad_value);
9969 return FALSE;
9970 }
9971
9972 addend = link_order->u.reloc.p->addend;
9973
d4730f92
BS
9974 if (esdo->rel.hdr)
9975 reldata = &esdo->rel;
9976 else if (esdo->rela.hdr)
9977 reldata = &esdo->rela;
9978 else
9979 {
9980 reldata = NULL;
9981 BFD_ASSERT (0);
9982 }
9983
c152c796 9984 /* Figure out the symbol index. */
d4730f92 9985 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
9986 if (link_order->type == bfd_section_reloc_link_order)
9987 {
9988 indx = link_order->u.reloc.p->u.section->target_index;
9989 BFD_ASSERT (indx != 0);
9990 *rel_hash_ptr = NULL;
9991 }
9992 else
9993 {
9994 struct elf_link_hash_entry *h;
9995
9996 /* Treat a reloc against a defined symbol as though it were
9997 actually against the section. */
9998 h = ((struct elf_link_hash_entry *)
9999 bfd_wrapped_link_hash_lookup (output_bfd, info,
10000 link_order->u.reloc.p->u.name,
10001 FALSE, FALSE, TRUE));
10002 if (h != NULL
10003 && (h->root.type == bfd_link_hash_defined
10004 || h->root.type == bfd_link_hash_defweak))
10005 {
10006 asection *section;
10007
10008 section = h->root.u.def.section;
10009 indx = section->output_section->target_index;
10010 *rel_hash_ptr = NULL;
10011 /* It seems that we ought to add the symbol value to the
10012 addend here, but in practice it has already been added
10013 because it was passed to constructor_callback. */
10014 addend += section->output_section->vma + section->output_offset;
10015 }
10016 else if (h != NULL)
10017 {
10018 /* Setting the index to -2 tells elf_link_output_extsym that
10019 this symbol is used by a reloc. */
10020 h->indx = -2;
10021 *rel_hash_ptr = h;
10022 indx = 0;
10023 }
10024 else
10025 {
10026 if (! ((*info->callbacks->unattached_reloc)
10027 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10028 return FALSE;
10029 indx = 0;
10030 }
10031 }
10032
10033 /* If this is an inplace reloc, we must write the addend into the
10034 object file. */
10035 if (howto->partial_inplace && addend != 0)
10036 {
10037 bfd_size_type size;
10038 bfd_reloc_status_type rstat;
10039 bfd_byte *buf;
10040 bfd_boolean ok;
10041 const char *sym_name;
10042
a50b1753
NC
10043 size = (bfd_size_type) bfd_get_reloc_size (howto);
10044 buf = (bfd_byte *) bfd_zmalloc (size);
c152c796
AM
10045 if (buf == NULL)
10046 return FALSE;
10047 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10048 switch (rstat)
10049 {
10050 case bfd_reloc_ok:
10051 break;
10052
10053 default:
10054 case bfd_reloc_outofrange:
10055 abort ();
10056
10057 case bfd_reloc_overflow:
10058 if (link_order->type == bfd_section_reloc_link_order)
10059 sym_name = bfd_section_name (output_bfd,
10060 link_order->u.reloc.p->u.section);
10061 else
10062 sym_name = link_order->u.reloc.p->u.name;
10063 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10064 (info, NULL, sym_name, howto->name, addend, NULL,
10065 NULL, (bfd_vma) 0)))
c152c796
AM
10066 {
10067 free (buf);
10068 return FALSE;
10069 }
10070 break;
10071 }
10072 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10073 link_order->offset, size);
10074 free (buf);
10075 if (! ok)
10076 return FALSE;
10077 }
10078
10079 /* The address of a reloc is relative to the section in a
10080 relocatable file, and is a virtual address in an executable
10081 file. */
10082 offset = link_order->offset;
10083 if (! info->relocatable)
10084 offset += output_section->vma;
10085
10086 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10087 {
10088 irel[i].r_offset = offset;
10089 irel[i].r_info = 0;
10090 irel[i].r_addend = 0;
10091 }
10092 if (bed->s->arch_size == 32)
10093 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10094 else
10095 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10096
d4730f92 10097 rel_hdr = reldata->hdr;
c152c796
AM
10098 erel = rel_hdr->contents;
10099 if (rel_hdr->sh_type == SHT_REL)
10100 {
d4730f92 10101 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10102 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10103 }
10104 else
10105 {
10106 irel[0].r_addend = addend;
d4730f92 10107 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10108 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10109 }
10110
d4730f92 10111 ++reldata->count;
c152c796
AM
10112
10113 return TRUE;
10114}
10115
0b52efa6
PB
10116
10117/* Get the output vma of the section pointed to by the sh_link field. */
10118
10119static bfd_vma
10120elf_get_linked_section_vma (struct bfd_link_order *p)
10121{
10122 Elf_Internal_Shdr **elf_shdrp;
10123 asection *s;
10124 int elfsec;
10125
10126 s = p->u.indirect.section;
10127 elf_shdrp = elf_elfsections (s->owner);
10128 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10129 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10130 /* PR 290:
10131 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10132 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10133 sh_info fields. Hence we could get the situation
10134 where elfsec is 0. */
10135 if (elfsec == 0)
10136 {
10137 const struct elf_backend_data *bed
10138 = get_elf_backend_data (s->owner);
10139 if (bed->link_order_error_handler)
d003868e
AM
10140 bed->link_order_error_handler
10141 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10142 return 0;
10143 }
10144 else
10145 {
10146 s = elf_shdrp[elfsec]->bfd_section;
10147 return s->output_section->vma + s->output_offset;
10148 }
0b52efa6
PB
10149}
10150
10151
10152/* Compare two sections based on the locations of the sections they are
10153 linked to. Used by elf_fixup_link_order. */
10154
10155static int
10156compare_link_order (const void * a, const void * b)
10157{
10158 bfd_vma apos;
10159 bfd_vma bpos;
10160
10161 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10162 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10163 if (apos < bpos)
10164 return -1;
10165 return apos > bpos;
10166}
10167
10168
10169/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10170 order as their linked sections. Returns false if this could not be done
10171 because an output section includes both ordered and unordered
10172 sections. Ideally we'd do this in the linker proper. */
10173
10174static bfd_boolean
10175elf_fixup_link_order (bfd *abfd, asection *o)
10176{
10177 int seen_linkorder;
10178 int seen_other;
10179 int n;
10180 struct bfd_link_order *p;
10181 bfd *sub;
10182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10183 unsigned elfsec;
0b52efa6 10184 struct bfd_link_order **sections;
d33cdfe3 10185 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10186 bfd_vma offset;
3b36f7e6 10187
d33cdfe3
L
10188 other_sec = NULL;
10189 linkorder_sec = NULL;
0b52efa6
PB
10190 seen_other = 0;
10191 seen_linkorder = 0;
8423293d 10192 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10193 {
d33cdfe3 10194 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10195 {
10196 s = p->u.indirect.section;
d33cdfe3
L
10197 sub = s->owner;
10198 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10199 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10200 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10201 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10202 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10203 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10204 {
10205 seen_linkorder++;
10206 linkorder_sec = s;
10207 }
0b52efa6 10208 else
d33cdfe3
L
10209 {
10210 seen_other++;
10211 other_sec = s;
10212 }
0b52efa6
PB
10213 }
10214 else
10215 seen_other++;
d33cdfe3
L
10216
10217 if (seen_other && seen_linkorder)
10218 {
10219 if (other_sec && linkorder_sec)
10220 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10221 o, linkorder_sec,
10222 linkorder_sec->owner, other_sec,
10223 other_sec->owner);
10224 else
10225 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10226 o);
10227 bfd_set_error (bfd_error_bad_value);
10228 return FALSE;
10229 }
0b52efa6
PB
10230 }
10231
10232 if (!seen_linkorder)
10233 return TRUE;
10234
0b52efa6 10235 sections = (struct bfd_link_order **)
14b1c01e
AM
10236 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10237 if (sections == NULL)
10238 return FALSE;
0b52efa6 10239 seen_linkorder = 0;
3b36f7e6 10240
8423293d 10241 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10242 {
10243 sections[seen_linkorder++] = p;
10244 }
10245 /* Sort the input sections in the order of their linked section. */
10246 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10247 compare_link_order);
10248
10249 /* Change the offsets of the sections. */
10250 offset = 0;
10251 for (n = 0; n < seen_linkorder; n++)
10252 {
10253 s = sections[n]->u.indirect.section;
461686a3 10254 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10255 s->output_offset = offset;
10256 sections[n]->offset = offset;
5dabe785 10257 /* FIXME: octets_per_byte. */
0b52efa6
PB
10258 offset += sections[n]->size;
10259 }
10260
4dd07732 10261 free (sections);
0b52efa6
PB
10262 return TRUE;
10263}
10264
10265
c152c796
AM
10266/* Do the final step of an ELF link. */
10267
10268bfd_boolean
10269bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10270{
10271 bfd_boolean dynamic;
10272 bfd_boolean emit_relocs;
10273 bfd *dynobj;
10274 struct elf_final_link_info finfo;
91d6fa6a
NC
10275 asection *o;
10276 struct bfd_link_order *p;
10277 bfd *sub;
c152c796
AM
10278 bfd_size_type max_contents_size;
10279 bfd_size_type max_external_reloc_size;
10280 bfd_size_type max_internal_reloc_count;
10281 bfd_size_type max_sym_count;
10282 bfd_size_type max_sym_shndx_count;
10283 file_ptr off;
10284 Elf_Internal_Sym elfsym;
10285 unsigned int i;
10286 Elf_Internal_Shdr *symtab_hdr;
10287 Elf_Internal_Shdr *symtab_shndx_hdr;
10288 Elf_Internal_Shdr *symstrtab_hdr;
10289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10290 struct elf_outext_info eoinfo;
10291 bfd_boolean merged;
10292 size_t relativecount = 0;
10293 asection *reldyn = 0;
10294 bfd_size_type amt;
104d59d1
JM
10295 asection *attr_section = NULL;
10296 bfd_vma attr_size = 0;
10297 const char *std_attrs_section;
c152c796
AM
10298
10299 if (! is_elf_hash_table (info->hash))
10300 return FALSE;
10301
10302 if (info->shared)
10303 abfd->flags |= DYNAMIC;
10304
10305 dynamic = elf_hash_table (info)->dynamic_sections_created;
10306 dynobj = elf_hash_table (info)->dynobj;
10307
10308 emit_relocs = (info->relocatable
a4676736 10309 || info->emitrelocations);
c152c796
AM
10310
10311 finfo.info = info;
10312 finfo.output_bfd = abfd;
10313 finfo.symstrtab = _bfd_elf_stringtab_init ();
10314 if (finfo.symstrtab == NULL)
10315 return FALSE;
10316
10317 if (! dynamic)
10318 {
10319 finfo.dynsym_sec = NULL;
10320 finfo.hash_sec = NULL;
10321 finfo.symver_sec = NULL;
10322 }
10323 else
10324 {
10325 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10326 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
fdc90cb4 10327 BFD_ASSERT (finfo.dynsym_sec != NULL);
c152c796
AM
10328 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10329 /* Note that it is OK if symver_sec is NULL. */
10330 }
10331
10332 finfo.contents = NULL;
10333 finfo.external_relocs = NULL;
10334 finfo.internal_relocs = NULL;
10335 finfo.external_syms = NULL;
10336 finfo.locsym_shndx = NULL;
10337 finfo.internal_syms = NULL;
10338 finfo.indices = NULL;
10339 finfo.sections = NULL;
10340 finfo.symbuf = NULL;
10341 finfo.symshndxbuf = NULL;
10342 finfo.symbuf_count = 0;
10343 finfo.shndxbuf_size = 0;
10344
104d59d1
JM
10345 /* The object attributes have been merged. Remove the input
10346 sections from the link, and set the contents of the output
10347 secton. */
10348 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10349 for (o = abfd->sections; o != NULL; o = o->next)
10350 {
10351 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10352 || strcmp (o->name, ".gnu.attributes") == 0)
10353 {
10354 for (p = o->map_head.link_order; p != NULL; p = p->next)
10355 {
10356 asection *input_section;
10357
10358 if (p->type != bfd_indirect_link_order)
10359 continue;
10360 input_section = p->u.indirect.section;
10361 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10362 elf_link_input_bfd ignores this section. */
10363 input_section->flags &= ~SEC_HAS_CONTENTS;
10364 }
a0c8462f 10365
104d59d1
JM
10366 attr_size = bfd_elf_obj_attr_size (abfd);
10367 if (attr_size)
10368 {
10369 bfd_set_section_size (abfd, o, attr_size);
10370 attr_section = o;
10371 /* Skip this section later on. */
10372 o->map_head.link_order = NULL;
10373 }
10374 else
10375 o->flags |= SEC_EXCLUDE;
10376 }
10377 }
10378
c152c796
AM
10379 /* Count up the number of relocations we will output for each output
10380 section, so that we know the sizes of the reloc sections. We
10381 also figure out some maximum sizes. */
10382 max_contents_size = 0;
10383 max_external_reloc_size = 0;
10384 max_internal_reloc_count = 0;
10385 max_sym_count = 0;
10386 max_sym_shndx_count = 0;
10387 merged = FALSE;
10388 for (o = abfd->sections; o != NULL; o = o->next)
10389 {
10390 struct bfd_elf_section_data *esdo = elf_section_data (o);
10391 o->reloc_count = 0;
10392
8423293d 10393 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10394 {
10395 unsigned int reloc_count = 0;
10396 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10397
10398 if (p->type == bfd_section_reloc_link_order
10399 || p->type == bfd_symbol_reloc_link_order)
10400 reloc_count = 1;
10401 else if (p->type == bfd_indirect_link_order)
10402 {
10403 asection *sec;
10404
10405 sec = p->u.indirect.section;
10406 esdi = elf_section_data (sec);
10407
10408 /* Mark all sections which are to be included in the
10409 link. This will normally be every section. We need
10410 to do this so that we can identify any sections which
10411 the linker has decided to not include. */
10412 sec->linker_mark = TRUE;
10413
10414 if (sec->flags & SEC_MERGE)
10415 merged = TRUE;
10416
10417 if (info->relocatable || info->emitrelocations)
10418 reloc_count = sec->reloc_count;
10419 else if (bed->elf_backend_count_relocs)
58217f29 10420 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 10421
eea6121a
AM
10422 if (sec->rawsize > max_contents_size)
10423 max_contents_size = sec->rawsize;
10424 if (sec->size > max_contents_size)
10425 max_contents_size = sec->size;
c152c796
AM
10426
10427 /* We are interested in just local symbols, not all
10428 symbols. */
10429 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10430 && (sec->owner->flags & DYNAMIC) == 0)
10431 {
10432 size_t sym_count;
10433
10434 if (elf_bad_symtab (sec->owner))
10435 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10436 / bed->s->sizeof_sym);
10437 else
10438 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10439
10440 if (sym_count > max_sym_count)
10441 max_sym_count = sym_count;
10442
10443 if (sym_count > max_sym_shndx_count
10444 && elf_symtab_shndx (sec->owner) != 0)
10445 max_sym_shndx_count = sym_count;
10446
10447 if ((sec->flags & SEC_RELOC) != 0)
10448 {
d4730f92 10449 size_t ext_size = 0;
c152c796 10450
d4730f92
BS
10451 if (esdi->rel.hdr != NULL)
10452 ext_size = esdi->rel.hdr->sh_size;
10453 if (esdi->rela.hdr != NULL)
10454 ext_size += esdi->rela.hdr->sh_size;
7326c758 10455
c152c796
AM
10456 if (ext_size > max_external_reloc_size)
10457 max_external_reloc_size = ext_size;
10458 if (sec->reloc_count > max_internal_reloc_count)
10459 max_internal_reloc_count = sec->reloc_count;
10460 }
10461 }
10462 }
10463
10464 if (reloc_count == 0)
10465 continue;
10466
10467 o->reloc_count += reloc_count;
10468
d4730f92
BS
10469 if (p->type == bfd_indirect_link_order
10470 && (info->relocatable || info->emitrelocations))
c152c796 10471 {
d4730f92
BS
10472 if (esdi->rel.hdr)
10473 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10474 if (esdi->rela.hdr)
10475 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10476 }
10477 else
10478 {
10479 if (o->use_rela_p)
10480 esdo->rela.count += reloc_count;
2c2b4ed4 10481 else
d4730f92 10482 esdo->rel.count += reloc_count;
c152c796 10483 }
c152c796
AM
10484 }
10485
10486 if (o->reloc_count > 0)
10487 o->flags |= SEC_RELOC;
10488 else
10489 {
10490 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10491 set it (this is probably a bug) and if it is set
10492 assign_section_numbers will create a reloc section. */
10493 o->flags &=~ SEC_RELOC;
10494 }
10495
10496 /* If the SEC_ALLOC flag is not set, force the section VMA to
10497 zero. This is done in elf_fake_sections as well, but forcing
10498 the VMA to 0 here will ensure that relocs against these
10499 sections are handled correctly. */
10500 if ((o->flags & SEC_ALLOC) == 0
10501 && ! o->user_set_vma)
10502 o->vma = 0;
10503 }
10504
10505 if (! info->relocatable && merged)
10506 elf_link_hash_traverse (elf_hash_table (info),
10507 _bfd_elf_link_sec_merge_syms, abfd);
10508
10509 /* Figure out the file positions for everything but the symbol table
10510 and the relocs. We set symcount to force assign_section_numbers
10511 to create a symbol table. */
10512 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10513 BFD_ASSERT (! abfd->output_has_begun);
10514 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10515 goto error_return;
10516
ee75fd95 10517 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10518 for (o = abfd->sections; o != NULL; o = o->next)
10519 {
d4730f92 10520 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
10521 if ((o->flags & SEC_RELOC) != 0)
10522 {
d4730f92
BS
10523 if (esdo->rel.hdr
10524 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
10525 goto error_return;
10526
d4730f92
BS
10527 if (esdo->rela.hdr
10528 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
10529 goto error_return;
10530 }
10531
10532 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10533 to count upwards while actually outputting the relocations. */
d4730f92
BS
10534 esdo->rel.count = 0;
10535 esdo->rela.count = 0;
c152c796
AM
10536 }
10537
10538 _bfd_elf_assign_file_positions_for_relocs (abfd);
10539
10540 /* We have now assigned file positions for all the sections except
10541 .symtab and .strtab. We start the .symtab section at the current
10542 file position, and write directly to it. We build the .strtab
10543 section in memory. */
10544 bfd_get_symcount (abfd) = 0;
10545 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10546 /* sh_name is set in prep_headers. */
10547 symtab_hdr->sh_type = SHT_SYMTAB;
10548 /* sh_flags, sh_addr and sh_size all start off zero. */
10549 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10550 /* sh_link is set in assign_section_numbers. */
10551 /* sh_info is set below. */
10552 /* sh_offset is set just below. */
72de5009 10553 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796
AM
10554
10555 off = elf_tdata (abfd)->next_file_pos;
10556 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10557
10558 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10559 incorrect. We do not yet know the size of the .symtab section.
10560 We correct next_file_pos below, after we do know the size. */
10561
10562 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10563 continuously seeking to the right position in the file. */
10564 if (! info->keep_memory || max_sym_count < 20)
10565 finfo.symbuf_size = 20;
10566 else
10567 finfo.symbuf_size = max_sym_count;
10568 amt = finfo.symbuf_size;
10569 amt *= bed->s->sizeof_sym;
a50b1753 10570 finfo.symbuf = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10571 if (finfo.symbuf == NULL)
10572 goto error_return;
4fbb74a6 10573 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10574 {
10575 /* Wild guess at number of output symbols. realloc'd as needed. */
10576 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10577 finfo.shndxbuf_size = amt;
10578 amt *= sizeof (Elf_External_Sym_Shndx);
a50b1753 10579 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
c152c796
AM
10580 if (finfo.symshndxbuf == NULL)
10581 goto error_return;
10582 }
10583
10584 /* Start writing out the symbol table. The first symbol is always a
10585 dummy symbol. */
10586 if (info->strip != strip_all
10587 || emit_relocs)
10588 {
10589 elfsym.st_value = 0;
10590 elfsym.st_size = 0;
10591 elfsym.st_info = 0;
10592 elfsym.st_other = 0;
10593 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 10594 elfsym.st_target_internal = 0;
6e0b88f1
AM
10595 if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10596 NULL) != 1)
c152c796
AM
10597 goto error_return;
10598 }
10599
c152c796
AM
10600 /* Output a symbol for each section. We output these even if we are
10601 discarding local symbols, since they are used for relocs. These
10602 symbols have no names. We store the index of each one in the
10603 index field of the section, so that we can find it again when
10604 outputting relocs. */
10605 if (info->strip != strip_all
10606 || emit_relocs)
10607 {
10608 elfsym.st_size = 0;
10609 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10610 elfsym.st_other = 0;
f0b5bb34 10611 elfsym.st_value = 0;
35fc36a8 10612 elfsym.st_target_internal = 0;
c152c796
AM
10613 for (i = 1; i < elf_numsections (abfd); i++)
10614 {
10615 o = bfd_section_from_elf_index (abfd, i);
10616 if (o != NULL)
f0b5bb34
AM
10617 {
10618 o->target_index = bfd_get_symcount (abfd);
10619 elfsym.st_shndx = i;
10620 if (!info->relocatable)
10621 elfsym.st_value = o->vma;
6e0b88f1 10622 if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1)
f0b5bb34
AM
10623 goto error_return;
10624 }
c152c796
AM
10625 }
10626 }
10627
10628 /* Allocate some memory to hold information read in from the input
10629 files. */
10630 if (max_contents_size != 0)
10631 {
a50b1753 10632 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
c152c796
AM
10633 if (finfo.contents == NULL)
10634 goto error_return;
10635 }
10636
10637 if (max_external_reloc_size != 0)
10638 {
10639 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10640 if (finfo.external_relocs == NULL)
10641 goto error_return;
10642 }
10643
10644 if (max_internal_reloc_count != 0)
10645 {
10646 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10647 amt *= sizeof (Elf_Internal_Rela);
a50b1753 10648 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
c152c796
AM
10649 if (finfo.internal_relocs == NULL)
10650 goto error_return;
10651 }
10652
10653 if (max_sym_count != 0)
10654 {
10655 amt = max_sym_count * bed->s->sizeof_sym;
a50b1753 10656 finfo.external_syms = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10657 if (finfo.external_syms == NULL)
10658 goto error_return;
10659
10660 amt = max_sym_count * sizeof (Elf_Internal_Sym);
a50b1753 10661 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
c152c796
AM
10662 if (finfo.internal_syms == NULL)
10663 goto error_return;
10664
10665 amt = max_sym_count * sizeof (long);
a50b1753 10666 finfo.indices = (long int *) bfd_malloc (amt);
c152c796
AM
10667 if (finfo.indices == NULL)
10668 goto error_return;
10669
10670 amt = max_sym_count * sizeof (asection *);
a50b1753 10671 finfo.sections = (asection **) bfd_malloc (amt);
c152c796
AM
10672 if (finfo.sections == NULL)
10673 goto error_return;
10674 }
10675
10676 if (max_sym_shndx_count != 0)
10677 {
10678 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
a50b1753 10679 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
c152c796
AM
10680 if (finfo.locsym_shndx == NULL)
10681 goto error_return;
10682 }
10683
10684 if (elf_hash_table (info)->tls_sec)
10685 {
10686 bfd_vma base, end = 0;
10687 asection *sec;
10688
10689 for (sec = elf_hash_table (info)->tls_sec;
10690 sec && (sec->flags & SEC_THREAD_LOCAL);
10691 sec = sec->next)
10692 {
3a800eb9 10693 bfd_size_type size = sec->size;
c152c796 10694
3a800eb9
AM
10695 if (size == 0
10696 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10697 {
91d6fa6a
NC
10698 struct bfd_link_order *ord = sec->map_tail.link_order;
10699
10700 if (ord != NULL)
10701 size = ord->offset + ord->size;
c152c796
AM
10702 }
10703 end = sec->vma + size;
10704 }
10705 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
10706 /* Only align end of TLS section if static TLS doesn't have special
10707 alignment requirements. */
10708 if (bed->static_tls_alignment == 1)
10709 end = align_power (end,
10710 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
10711 elf_hash_table (info)->tls_size = end - base;
10712 }
10713
0b52efa6
PB
10714 /* Reorder SHF_LINK_ORDER sections. */
10715 for (o = abfd->sections; o != NULL; o = o->next)
10716 {
10717 if (!elf_fixup_link_order (abfd, o))
10718 return FALSE;
10719 }
10720
c152c796
AM
10721 /* Since ELF permits relocations to be against local symbols, we
10722 must have the local symbols available when we do the relocations.
10723 Since we would rather only read the local symbols once, and we
10724 would rather not keep them in memory, we handle all the
10725 relocations for a single input file at the same time.
10726
10727 Unfortunately, there is no way to know the total number of local
10728 symbols until we have seen all of them, and the local symbol
10729 indices precede the global symbol indices. This means that when
10730 we are generating relocatable output, and we see a reloc against
10731 a global symbol, we can not know the symbol index until we have
10732 finished examining all the local symbols to see which ones we are
10733 going to output. To deal with this, we keep the relocations in
10734 memory, and don't output them until the end of the link. This is
10735 an unfortunate waste of memory, but I don't see a good way around
10736 it. Fortunately, it only happens when performing a relocatable
10737 link, which is not the common case. FIXME: If keep_memory is set
10738 we could write the relocs out and then read them again; I don't
10739 know how bad the memory loss will be. */
10740
10741 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10742 sub->output_has_begun = FALSE;
10743 for (o = abfd->sections; o != NULL; o = o->next)
10744 {
8423293d 10745 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10746 {
10747 if (p->type == bfd_indirect_link_order
10748 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10749 == bfd_target_elf_flavour)
10750 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10751 {
10752 if (! sub->output_has_begun)
10753 {
10754 if (! elf_link_input_bfd (&finfo, sub))
10755 goto error_return;
10756 sub->output_has_begun = TRUE;
10757 }
10758 }
10759 else if (p->type == bfd_section_reloc_link_order
10760 || p->type == bfd_symbol_reloc_link_order)
10761 {
10762 if (! elf_reloc_link_order (abfd, info, o, p))
10763 goto error_return;
10764 }
10765 else
10766 {
10767 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
10768 {
10769 if (p->type == bfd_indirect_link_order
10770 && (bfd_get_flavour (sub)
10771 == bfd_target_elf_flavour)
10772 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10773 != bed->s->elfclass))
10774 {
10775 const char *iclass, *oclass;
10776
10777 if (bed->s->elfclass == ELFCLASS64)
10778 {
10779 iclass = "ELFCLASS32";
10780 oclass = "ELFCLASS64";
10781 }
10782 else
10783 {
10784 iclass = "ELFCLASS64";
10785 oclass = "ELFCLASS32";
10786 }
10787
10788 bfd_set_error (bfd_error_wrong_format);
10789 (*_bfd_error_handler)
10790 (_("%B: file class %s incompatible with %s"),
10791 sub, iclass, oclass);
10792 }
10793
10794 goto error_return;
10795 }
c152c796
AM
10796 }
10797 }
10798 }
10799
c0f00686
L
10800 /* Free symbol buffer if needed. */
10801 if (!info->reduce_memory_overheads)
10802 {
10803 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10804 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10805 && elf_tdata (sub)->symbuf)
c0f00686
L
10806 {
10807 free (elf_tdata (sub)->symbuf);
10808 elf_tdata (sub)->symbuf = NULL;
10809 }
10810 }
10811
c152c796
AM
10812 /* Output any global symbols that got converted to local in a
10813 version script or due to symbol visibility. We do this in a
10814 separate step since ELF requires all local symbols to appear
10815 prior to any global symbols. FIXME: We should only do this if
10816 some global symbols were, in fact, converted to become local.
10817 FIXME: Will this work correctly with the Irix 5 linker? */
10818 eoinfo.failed = FALSE;
10819 eoinfo.finfo = &finfo;
10820 eoinfo.localsyms = TRUE;
10821 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10822 &eoinfo);
10823 if (eoinfo.failed)
10824 return FALSE;
10825
4e617b1e
PB
10826 /* If backend needs to output some local symbols not present in the hash
10827 table, do it now. */
10828 if (bed->elf_backend_output_arch_local_syms)
10829 {
6e0b88f1 10830 typedef int (*out_sym_func)
4e617b1e
PB
10831 (void *, const char *, Elf_Internal_Sym *, asection *,
10832 struct elf_link_hash_entry *);
10833
10834 if (! ((*bed->elf_backend_output_arch_local_syms)
10835 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10836 return FALSE;
10837 }
10838
c152c796
AM
10839 /* That wrote out all the local symbols. Finish up the symbol table
10840 with the global symbols. Even if we want to strip everything we
10841 can, we still need to deal with those global symbols that got
10842 converted to local in a version script. */
10843
10844 /* The sh_info field records the index of the first non local symbol. */
10845 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10846
10847 if (dynamic
10848 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10849 {
10850 Elf_Internal_Sym sym;
10851 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10852 long last_local = 0;
10853
10854 /* Write out the section symbols for the output sections. */
67687978 10855 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10856 {
10857 asection *s;
10858
10859 sym.st_size = 0;
10860 sym.st_name = 0;
10861 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10862 sym.st_other = 0;
35fc36a8 10863 sym.st_target_internal = 0;
c152c796
AM
10864
10865 for (s = abfd->sections; s != NULL; s = s->next)
10866 {
10867 int indx;
10868 bfd_byte *dest;
10869 long dynindx;
10870
c152c796 10871 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10872 if (dynindx <= 0)
10873 continue;
10874 indx = elf_section_data (s)->this_idx;
c152c796
AM
10875 BFD_ASSERT (indx > 0);
10876 sym.st_shndx = indx;
c0d5a53d
L
10877 if (! check_dynsym (abfd, &sym))
10878 return FALSE;
c152c796
AM
10879 sym.st_value = s->vma;
10880 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10881 if (last_local < dynindx)
10882 last_local = dynindx;
c152c796
AM
10883 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10884 }
c152c796
AM
10885 }
10886
10887 /* Write out the local dynsyms. */
10888 if (elf_hash_table (info)->dynlocal)
10889 {
10890 struct elf_link_local_dynamic_entry *e;
10891 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10892 {
10893 asection *s;
10894 bfd_byte *dest;
10895
935bd1e0 10896 /* Copy the internal symbol and turn off visibility.
c152c796
AM
10897 Note that we saved a word of storage and overwrote
10898 the original st_name with the dynstr_index. */
10899 sym = e->isym;
935bd1e0 10900 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 10901
cb33740c
AM
10902 s = bfd_section_from_elf_index (e->input_bfd,
10903 e->isym.st_shndx);
10904 if (s != NULL)
c152c796 10905 {
c152c796
AM
10906 sym.st_shndx =
10907 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10908 if (! check_dynsym (abfd, &sym))
10909 return FALSE;
c152c796
AM
10910 sym.st_value = (s->output_section->vma
10911 + s->output_offset
10912 + e->isym.st_value);
10913 }
10914
10915 if (last_local < e->dynindx)
10916 last_local = e->dynindx;
10917
10918 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10919 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10920 }
10921 }
10922
10923 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10924 last_local + 1;
10925 }
10926
10927 /* We get the global symbols from the hash table. */
10928 eoinfo.failed = FALSE;
10929 eoinfo.localsyms = FALSE;
10930 eoinfo.finfo = &finfo;
10931 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10932 &eoinfo);
10933 if (eoinfo.failed)
10934 return FALSE;
10935
10936 /* If backend needs to output some symbols not present in the hash
10937 table, do it now. */
10938 if (bed->elf_backend_output_arch_syms)
10939 {
6e0b88f1 10940 typedef int (*out_sym_func)
c152c796
AM
10941 (void *, const char *, Elf_Internal_Sym *, asection *,
10942 struct elf_link_hash_entry *);
10943
10944 if (! ((*bed->elf_backend_output_arch_syms)
10945 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10946 return FALSE;
10947 }
10948
10949 /* Flush all symbols to the file. */
10950 if (! elf_link_flush_output_syms (&finfo, bed))
10951 return FALSE;
10952
10953 /* Now we know the size of the symtab section. */
10954 off += symtab_hdr->sh_size;
10955
10956 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10957 if (symtab_shndx_hdr->sh_name != 0)
10958 {
10959 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10960 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10961 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10962 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10963 symtab_shndx_hdr->sh_size = amt;
10964
10965 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10966 off, TRUE);
10967
10968 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10969 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10970 return FALSE;
10971 }
10972
10973
10974 /* Finish up and write out the symbol string table (.strtab)
10975 section. */
10976 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10977 /* sh_name was set in prep_headers. */
10978 symstrtab_hdr->sh_type = SHT_STRTAB;
10979 symstrtab_hdr->sh_flags = 0;
10980 symstrtab_hdr->sh_addr = 0;
10981 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10982 symstrtab_hdr->sh_entsize = 0;
10983 symstrtab_hdr->sh_link = 0;
10984 symstrtab_hdr->sh_info = 0;
10985 /* sh_offset is set just below. */
10986 symstrtab_hdr->sh_addralign = 1;
10987
10988 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10989 elf_tdata (abfd)->next_file_pos = off;
10990
10991 if (bfd_get_symcount (abfd) > 0)
10992 {
10993 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10994 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10995 return FALSE;
10996 }
10997
10998 /* Adjust the relocs to have the correct symbol indices. */
10999 for (o = abfd->sections; o != NULL; o = o->next)
11000 {
d4730f92 11001 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11002 if ((o->flags & SEC_RELOC) == 0)
11003 continue;
11004
d4730f92
BS
11005 if (esdo->rel.hdr != NULL)
11006 elf_link_adjust_relocs (abfd, &esdo->rel);
11007 if (esdo->rela.hdr != NULL)
11008 elf_link_adjust_relocs (abfd, &esdo->rela);
c152c796
AM
11009
11010 /* Set the reloc_count field to 0 to prevent write_relocs from
11011 trying to swap the relocs out itself. */
11012 o->reloc_count = 0;
11013 }
11014
11015 if (dynamic && info->combreloc && dynobj != NULL)
11016 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11017
11018 /* If we are linking against a dynamic object, or generating a
11019 shared library, finish up the dynamic linking information. */
11020 if (dynamic)
11021 {
11022 bfd_byte *dyncon, *dynconend;
11023
11024 /* Fix up .dynamic entries. */
11025 o = bfd_get_section_by_name (dynobj, ".dynamic");
11026 BFD_ASSERT (o != NULL);
11027
11028 dyncon = o->contents;
eea6121a 11029 dynconend = o->contents + o->size;
c152c796
AM
11030 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11031 {
11032 Elf_Internal_Dyn dyn;
11033 const char *name;
11034 unsigned int type;
11035
11036 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11037
11038 switch (dyn.d_tag)
11039 {
11040 default:
11041 continue;
11042 case DT_NULL:
11043 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11044 {
11045 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11046 {
11047 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11048 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11049 default: continue;
11050 }
11051 dyn.d_un.d_val = relativecount;
11052 relativecount = 0;
11053 break;
11054 }
11055 continue;
11056
11057 case DT_INIT:
11058 name = info->init_function;
11059 goto get_sym;
11060 case DT_FINI:
11061 name = info->fini_function;
11062 get_sym:
11063 {
11064 struct elf_link_hash_entry *h;
11065
11066 h = elf_link_hash_lookup (elf_hash_table (info), name,
11067 FALSE, FALSE, TRUE);
11068 if (h != NULL
11069 && (h->root.type == bfd_link_hash_defined
11070 || h->root.type == bfd_link_hash_defweak))
11071 {
bef26483 11072 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11073 o = h->root.u.def.section;
11074 if (o->output_section != NULL)
bef26483 11075 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11076 + o->output_offset);
11077 else
11078 {
11079 /* The symbol is imported from another shared
11080 library and does not apply to this one. */
bef26483 11081 dyn.d_un.d_ptr = 0;
c152c796
AM
11082 }
11083 break;
11084 }
11085 }
11086 continue;
11087
11088 case DT_PREINIT_ARRAYSZ:
11089 name = ".preinit_array";
11090 goto get_size;
11091 case DT_INIT_ARRAYSZ:
11092 name = ".init_array";
11093 goto get_size;
11094 case DT_FINI_ARRAYSZ:
11095 name = ".fini_array";
11096 get_size:
11097 o = bfd_get_section_by_name (abfd, name);
11098 if (o == NULL)
11099 {
11100 (*_bfd_error_handler)
d003868e 11101 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11102 goto error_return;
11103 }
eea6121a 11104 if (o->size == 0)
c152c796
AM
11105 (*_bfd_error_handler)
11106 (_("warning: %s section has zero size"), name);
eea6121a 11107 dyn.d_un.d_val = o->size;
c152c796
AM
11108 break;
11109
11110 case DT_PREINIT_ARRAY:
11111 name = ".preinit_array";
11112 goto get_vma;
11113 case DT_INIT_ARRAY:
11114 name = ".init_array";
11115 goto get_vma;
11116 case DT_FINI_ARRAY:
11117 name = ".fini_array";
11118 goto get_vma;
11119
11120 case DT_HASH:
11121 name = ".hash";
11122 goto get_vma;
fdc90cb4
JJ
11123 case DT_GNU_HASH:
11124 name = ".gnu.hash";
11125 goto get_vma;
c152c796
AM
11126 case DT_STRTAB:
11127 name = ".dynstr";
11128 goto get_vma;
11129 case DT_SYMTAB:
11130 name = ".dynsym";
11131 goto get_vma;
11132 case DT_VERDEF:
11133 name = ".gnu.version_d";
11134 goto get_vma;
11135 case DT_VERNEED:
11136 name = ".gnu.version_r";
11137 goto get_vma;
11138 case DT_VERSYM:
11139 name = ".gnu.version";
11140 get_vma:
11141 o = bfd_get_section_by_name (abfd, name);
11142 if (o == NULL)
11143 {
11144 (*_bfd_error_handler)
d003868e 11145 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11146 goto error_return;
11147 }
11148 dyn.d_un.d_ptr = o->vma;
11149 break;
11150
11151 case DT_REL:
11152 case DT_RELA:
11153 case DT_RELSZ:
11154 case DT_RELASZ:
11155 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11156 type = SHT_REL;
11157 else
11158 type = SHT_RELA;
11159 dyn.d_un.d_val = 0;
bef26483 11160 dyn.d_un.d_ptr = 0;
c152c796
AM
11161 for (i = 1; i < elf_numsections (abfd); i++)
11162 {
11163 Elf_Internal_Shdr *hdr;
11164
11165 hdr = elf_elfsections (abfd)[i];
11166 if (hdr->sh_type == type
11167 && (hdr->sh_flags & SHF_ALLOC) != 0)
11168 {
11169 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11170 dyn.d_un.d_val += hdr->sh_size;
11171 else
11172 {
bef26483
AM
11173 if (dyn.d_un.d_ptr == 0
11174 || hdr->sh_addr < dyn.d_un.d_ptr)
11175 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11176 }
11177 }
11178 }
11179 break;
11180 }
11181 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11182 }
11183 }
11184
11185 /* If we have created any dynamic sections, then output them. */
11186 if (dynobj != NULL)
11187 {
11188 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11189 goto error_return;
11190
943284cc
DJ
11191 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11192 if (info->warn_shared_textrel && info->shared)
11193 {
11194 bfd_byte *dyncon, *dynconend;
11195
11196 /* Fix up .dynamic entries. */
11197 o = bfd_get_section_by_name (dynobj, ".dynamic");
11198 BFD_ASSERT (o != NULL);
11199
11200 dyncon = o->contents;
11201 dynconend = o->contents + o->size;
11202 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11203 {
11204 Elf_Internal_Dyn dyn;
11205
11206 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11207
11208 if (dyn.d_tag == DT_TEXTREL)
11209 {
a0c8462f 11210 info->callbacks->einfo
9267588c 11211 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11212 break;
11213 }
11214 }
11215 }
11216
c152c796
AM
11217 for (o = dynobj->sections; o != NULL; o = o->next)
11218 {
11219 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11220 || o->size == 0
c152c796
AM
11221 || o->output_section == bfd_abs_section_ptr)
11222 continue;
11223 if ((o->flags & SEC_LINKER_CREATED) == 0)
11224 {
11225 /* At this point, we are only interested in sections
11226 created by _bfd_elf_link_create_dynamic_sections. */
11227 continue;
11228 }
3722b82f
AM
11229 if (elf_hash_table (info)->stab_info.stabstr == o)
11230 continue;
eea6121a
AM
11231 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11232 continue;
c152c796
AM
11233 if ((elf_section_data (o->output_section)->this_hdr.sh_type
11234 != SHT_STRTAB)
11235 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
11236 {
5dabe785 11237 /* FIXME: octets_per_byte. */
c152c796
AM
11238 if (! bfd_set_section_contents (abfd, o->output_section,
11239 o->contents,
11240 (file_ptr) o->output_offset,
eea6121a 11241 o->size))
c152c796
AM
11242 goto error_return;
11243 }
11244 else
11245 {
11246 /* The contents of the .dynstr section are actually in a
11247 stringtab. */
11248 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11249 if (bfd_seek (abfd, off, SEEK_SET) != 0
11250 || ! _bfd_elf_strtab_emit (abfd,
11251 elf_hash_table (info)->dynstr))
11252 goto error_return;
11253 }
11254 }
11255 }
11256
11257 if (info->relocatable)
11258 {
11259 bfd_boolean failed = FALSE;
11260
11261 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11262 if (failed)
11263 goto error_return;
11264 }
11265
11266 /* If we have optimized stabs strings, output them. */
3722b82f 11267 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11268 {
11269 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11270 goto error_return;
11271 }
11272
11273 if (info->eh_frame_hdr)
11274 {
11275 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11276 goto error_return;
11277 }
11278
11279 if (finfo.symstrtab != NULL)
11280 _bfd_stringtab_free (finfo.symstrtab);
11281 if (finfo.contents != NULL)
11282 free (finfo.contents);
11283 if (finfo.external_relocs != NULL)
11284 free (finfo.external_relocs);
11285 if (finfo.internal_relocs != NULL)
11286 free (finfo.internal_relocs);
11287 if (finfo.external_syms != NULL)
11288 free (finfo.external_syms);
11289 if (finfo.locsym_shndx != NULL)
11290 free (finfo.locsym_shndx);
11291 if (finfo.internal_syms != NULL)
11292 free (finfo.internal_syms);
11293 if (finfo.indices != NULL)
11294 free (finfo.indices);
11295 if (finfo.sections != NULL)
11296 free (finfo.sections);
11297 if (finfo.symbuf != NULL)
11298 free (finfo.symbuf);
11299 if (finfo.symshndxbuf != NULL)
11300 free (finfo.symshndxbuf);
11301 for (o = abfd->sections; o != NULL; o = o->next)
11302 {
d4730f92
BS
11303 struct bfd_elf_section_data *esdo = elf_section_data (o);
11304 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11305 free (esdo->rel.hashes);
11306 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11307 free (esdo->rela.hashes);
c152c796
AM
11308 }
11309
11310 elf_tdata (abfd)->linker = TRUE;
11311
104d59d1
JM
11312 if (attr_section)
11313 {
a50b1753 11314 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11315 if (contents == NULL)
d0f16d5e 11316 return FALSE; /* Bail out and fail. */
104d59d1
JM
11317 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11318 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11319 free (contents);
11320 }
11321
c152c796
AM
11322 return TRUE;
11323
11324 error_return:
11325 if (finfo.symstrtab != NULL)
11326 _bfd_stringtab_free (finfo.symstrtab);
11327 if (finfo.contents != NULL)
11328 free (finfo.contents);
11329 if (finfo.external_relocs != NULL)
11330 free (finfo.external_relocs);
11331 if (finfo.internal_relocs != NULL)
11332 free (finfo.internal_relocs);
11333 if (finfo.external_syms != NULL)
11334 free (finfo.external_syms);
11335 if (finfo.locsym_shndx != NULL)
11336 free (finfo.locsym_shndx);
11337 if (finfo.internal_syms != NULL)
11338 free (finfo.internal_syms);
11339 if (finfo.indices != NULL)
11340 free (finfo.indices);
11341 if (finfo.sections != NULL)
11342 free (finfo.sections);
11343 if (finfo.symbuf != NULL)
11344 free (finfo.symbuf);
11345 if (finfo.symshndxbuf != NULL)
11346 free (finfo.symshndxbuf);
11347 for (o = abfd->sections; o != NULL; o = o->next)
11348 {
d4730f92
BS
11349 struct bfd_elf_section_data *esdo = elf_section_data (o);
11350 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11351 free (esdo->rel.hashes);
11352 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11353 free (esdo->rela.hashes);
c152c796
AM
11354 }
11355
11356 return FALSE;
11357}
11358\f
5241d853
RS
11359/* Initialize COOKIE for input bfd ABFD. */
11360
11361static bfd_boolean
11362init_reloc_cookie (struct elf_reloc_cookie *cookie,
11363 struct bfd_link_info *info, bfd *abfd)
11364{
11365 Elf_Internal_Shdr *symtab_hdr;
11366 const struct elf_backend_data *bed;
11367
11368 bed = get_elf_backend_data (abfd);
11369 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11370
11371 cookie->abfd = abfd;
11372 cookie->sym_hashes = elf_sym_hashes (abfd);
11373 cookie->bad_symtab = elf_bad_symtab (abfd);
11374 if (cookie->bad_symtab)
11375 {
11376 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11377 cookie->extsymoff = 0;
11378 }
11379 else
11380 {
11381 cookie->locsymcount = symtab_hdr->sh_info;
11382 cookie->extsymoff = symtab_hdr->sh_info;
11383 }
11384
11385 if (bed->s->arch_size == 32)
11386 cookie->r_sym_shift = 8;
11387 else
11388 cookie->r_sym_shift = 32;
11389
11390 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11391 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11392 {
11393 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11394 cookie->locsymcount, 0,
11395 NULL, NULL, NULL);
11396 if (cookie->locsyms == NULL)
11397 {
11398 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11399 return FALSE;
11400 }
11401 if (info->keep_memory)
11402 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11403 }
11404 return TRUE;
11405}
11406
11407/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11408
11409static void
11410fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11411{
11412 Elf_Internal_Shdr *symtab_hdr;
11413
11414 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11415 if (cookie->locsyms != NULL
11416 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11417 free (cookie->locsyms);
11418}
11419
11420/* Initialize the relocation information in COOKIE for input section SEC
11421 of input bfd ABFD. */
11422
11423static bfd_boolean
11424init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11425 struct bfd_link_info *info, bfd *abfd,
11426 asection *sec)
11427{
11428 const struct elf_backend_data *bed;
11429
11430 if (sec->reloc_count == 0)
11431 {
11432 cookie->rels = NULL;
11433 cookie->relend = NULL;
11434 }
11435 else
11436 {
11437 bed = get_elf_backend_data (abfd);
11438
11439 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11440 info->keep_memory);
11441 if (cookie->rels == NULL)
11442 return FALSE;
11443 cookie->rel = cookie->rels;
11444 cookie->relend = (cookie->rels
11445 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11446 }
11447 cookie->rel = cookie->rels;
11448 return TRUE;
11449}
11450
11451/* Free the memory allocated by init_reloc_cookie_rels,
11452 if appropriate. */
11453
11454static void
11455fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11456 asection *sec)
11457{
11458 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11459 free (cookie->rels);
11460}
11461
11462/* Initialize the whole of COOKIE for input section SEC. */
11463
11464static bfd_boolean
11465init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11466 struct bfd_link_info *info,
11467 asection *sec)
11468{
11469 if (!init_reloc_cookie (cookie, info, sec->owner))
11470 goto error1;
11471 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11472 goto error2;
11473 return TRUE;
11474
11475 error2:
11476 fini_reloc_cookie (cookie, sec->owner);
11477 error1:
11478 return FALSE;
11479}
11480
11481/* Free the memory allocated by init_reloc_cookie_for_section,
11482 if appropriate. */
11483
11484static void
11485fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11486 asection *sec)
11487{
11488 fini_reloc_cookie_rels (cookie, sec);
11489 fini_reloc_cookie (cookie, sec->owner);
11490}
11491\f
c152c796
AM
11492/* Garbage collect unused sections. */
11493
07adf181
AM
11494/* Default gc_mark_hook. */
11495
11496asection *
11497_bfd_elf_gc_mark_hook (asection *sec,
11498 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11499 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11500 struct elf_link_hash_entry *h,
11501 Elf_Internal_Sym *sym)
11502{
bde6f3eb
L
11503 const char *sec_name;
11504
07adf181
AM
11505 if (h != NULL)
11506 {
11507 switch (h->root.type)
11508 {
11509 case bfd_link_hash_defined:
11510 case bfd_link_hash_defweak:
11511 return h->root.u.def.section;
11512
11513 case bfd_link_hash_common:
11514 return h->root.u.c.p->section;
11515
bde6f3eb
L
11516 case bfd_link_hash_undefined:
11517 case bfd_link_hash_undefweak:
11518 /* To work around a glibc bug, keep all XXX input sections
11519 when there is an as yet undefined reference to __start_XXX
11520 or __stop_XXX symbols. The linker will later define such
11521 symbols for orphan input sections that have a name
11522 representable as a C identifier. */
11523 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11524 sec_name = h->root.root.string + 8;
11525 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11526 sec_name = h->root.root.string + 7;
11527 else
11528 sec_name = NULL;
11529
11530 if (sec_name && *sec_name != '\0')
11531 {
11532 bfd *i;
11533
11534 for (i = info->input_bfds; i; i = i->link_next)
11535 {
11536 sec = bfd_get_section_by_name (i, sec_name);
11537 if (sec)
11538 sec->flags |= SEC_KEEP;
11539 }
11540 }
11541 break;
11542
07adf181
AM
11543 default:
11544 break;
11545 }
11546 }
11547 else
11548 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11549
11550 return NULL;
11551}
11552
5241d853
RS
11553/* COOKIE->rel describes a relocation against section SEC, which is
11554 a section we've decided to keep. Return the section that contains
11555 the relocation symbol, or NULL if no section contains it. */
11556
11557asection *
11558_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11559 elf_gc_mark_hook_fn gc_mark_hook,
11560 struct elf_reloc_cookie *cookie)
11561{
11562 unsigned long r_symndx;
11563 struct elf_link_hash_entry *h;
11564
11565 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 11566 if (r_symndx == STN_UNDEF)
5241d853
RS
11567 return NULL;
11568
11569 if (r_symndx >= cookie->locsymcount
11570 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11571 {
11572 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11573 while (h->root.type == bfd_link_hash_indirect
11574 || h->root.type == bfd_link_hash_warning)
11575 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11576 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11577 }
11578
11579 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11580 &cookie->locsyms[r_symndx]);
11581}
11582
11583/* COOKIE->rel describes a relocation against section SEC, which is
11584 a section we've decided to keep. Mark the section that contains
9d0a14d3 11585 the relocation symbol. */
5241d853
RS
11586
11587bfd_boolean
11588_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11589 asection *sec,
11590 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11591 struct elf_reloc_cookie *cookie)
5241d853
RS
11592{
11593 asection *rsec;
11594
11595 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11596 if (rsec && !rsec->gc_mark)
11597 {
11598 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11599 rsec->gc_mark = 1;
5241d853
RS
11600 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11601 return FALSE;
11602 }
11603 return TRUE;
11604}
11605
07adf181
AM
11606/* The mark phase of garbage collection. For a given section, mark
11607 it and any sections in this section's group, and all the sections
11608 which define symbols to which it refers. */
11609
ccfa59ea
AM
11610bfd_boolean
11611_bfd_elf_gc_mark (struct bfd_link_info *info,
11612 asection *sec,
6a5bb875 11613 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11614{
11615 bfd_boolean ret;
9d0a14d3 11616 asection *group_sec, *eh_frame;
c152c796
AM
11617
11618 sec->gc_mark = 1;
11619
11620 /* Mark all the sections in the group. */
11621 group_sec = elf_section_data (sec)->next_in_group;
11622 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11623 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11624 return FALSE;
11625
11626 /* Look through the section relocs. */
11627 ret = TRUE;
9d0a14d3
RS
11628 eh_frame = elf_eh_frame_section (sec->owner);
11629 if ((sec->flags & SEC_RELOC) != 0
11630 && sec->reloc_count > 0
11631 && sec != eh_frame)
c152c796 11632 {
5241d853 11633 struct elf_reloc_cookie cookie;
c152c796 11634
5241d853
RS
11635 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11636 ret = FALSE;
c152c796 11637 else
c152c796 11638 {
5241d853 11639 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11640 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11641 {
11642 ret = FALSE;
11643 break;
11644 }
11645 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11646 }
11647 }
9d0a14d3
RS
11648
11649 if (ret && eh_frame && elf_fde_list (sec))
11650 {
11651 struct elf_reloc_cookie cookie;
11652
11653 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11654 ret = FALSE;
11655 else
11656 {
11657 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11658 gc_mark_hook, &cookie))
11659 ret = FALSE;
11660 fini_reloc_cookie_for_section (&cookie, eh_frame);
11661 }
11662 }
11663
c152c796
AM
11664 return ret;
11665}
11666
11667/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11668
c17d87de
NC
11669struct elf_gc_sweep_symbol_info
11670{
ccabcbe5
AM
11671 struct bfd_link_info *info;
11672 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11673 bfd_boolean);
11674};
11675
c152c796 11676static bfd_boolean
ccabcbe5 11677elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11678{
c152c796
AM
11679 if (h->root.type == bfd_link_hash_warning)
11680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11681
ccabcbe5
AM
11682 if ((h->root.type == bfd_link_hash_defined
11683 || h->root.type == bfd_link_hash_defweak)
11684 && !h->root.u.def.section->gc_mark
11685 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11686 {
a50b1753
NC
11687 struct elf_gc_sweep_symbol_info *inf =
11688 (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5
AM
11689 (*inf->hide_symbol) (inf->info, h, TRUE);
11690 }
c152c796
AM
11691
11692 return TRUE;
11693}
11694
11695/* The sweep phase of garbage collection. Remove all garbage sections. */
11696
11697typedef bfd_boolean (*gc_sweep_hook_fn)
11698 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11699
11700static bfd_boolean
ccabcbe5 11701elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11702{
11703 bfd *sub;
ccabcbe5
AM
11704 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11705 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11706 unsigned long section_sym_count;
11707 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11708
11709 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11710 {
11711 asection *o;
11712
11713 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11714 continue;
11715
11716 for (o = sub->sections; o != NULL; o = o->next)
11717 {
a33dafc3
L
11718 /* When any section in a section group is kept, we keep all
11719 sections in the section group. If the first member of
11720 the section group is excluded, we will also exclude the
11721 group section. */
11722 if (o->flags & SEC_GROUP)
11723 {
11724 asection *first = elf_next_in_group (o);
11725 o->gc_mark = first->gc_mark;
11726 }
11727 else if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
16583161
L
11728 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0
11729 || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE)
a33dafc3 11730 {
16583161 11731 /* Keep debug, special and SHT_NOTE sections. */
a33dafc3
L
11732 o->gc_mark = 1;
11733 }
c152c796
AM
11734
11735 if (o->gc_mark)
11736 continue;
11737
11738 /* Skip sweeping sections already excluded. */
11739 if (o->flags & SEC_EXCLUDE)
11740 continue;
11741
11742 /* Since this is early in the link process, it is simple
11743 to remove a section from the output. */
11744 o->flags |= SEC_EXCLUDE;
11745
c55fe096 11746 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11747 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11748
c152c796
AM
11749 /* But we also have to update some of the relocation
11750 info we collected before. */
11751 if (gc_sweep_hook
e8aaee2a
AM
11752 && (o->flags & SEC_RELOC) != 0
11753 && o->reloc_count > 0
11754 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11755 {
11756 Elf_Internal_Rela *internal_relocs;
11757 bfd_boolean r;
11758
11759 internal_relocs
11760 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11761 info->keep_memory);
11762 if (internal_relocs == NULL)
11763 return FALSE;
11764
11765 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11766
11767 if (elf_section_data (o)->relocs != internal_relocs)
11768 free (internal_relocs);
11769
11770 if (!r)
11771 return FALSE;
11772 }
11773 }
11774 }
11775
11776 /* Remove the symbols that were in the swept sections from the dynamic
11777 symbol table. GCFIXME: Anyone know how to get them out of the
11778 static symbol table as well? */
ccabcbe5
AM
11779 sweep_info.info = info;
11780 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11781 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11782 &sweep_info);
c152c796 11783
ccabcbe5 11784 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11785 return TRUE;
11786}
11787
11788/* Propagate collected vtable information. This is called through
11789 elf_link_hash_traverse. */
11790
11791static bfd_boolean
11792elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11793{
11794 if (h->root.type == bfd_link_hash_warning)
11795 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11796
11797 /* Those that are not vtables. */
f6e332e6 11798 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11799 return TRUE;
11800
11801 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11802 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11803 return TRUE;
11804
11805 /* If we've already been done, exit. */
f6e332e6 11806 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11807 return TRUE;
11808
11809 /* Make sure the parent's table is up to date. */
f6e332e6 11810 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11811
f6e332e6 11812 if (h->vtable->used == NULL)
c152c796
AM
11813 {
11814 /* None of this table's entries were referenced. Re-use the
11815 parent's table. */
f6e332e6
AM
11816 h->vtable->used = h->vtable->parent->vtable->used;
11817 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11818 }
11819 else
11820 {
11821 size_t n;
11822 bfd_boolean *cu, *pu;
11823
11824 /* Or the parent's entries into ours. */
f6e332e6 11825 cu = h->vtable->used;
c152c796 11826 cu[-1] = TRUE;
f6e332e6 11827 pu = h->vtable->parent->vtable->used;
c152c796
AM
11828 if (pu != NULL)
11829 {
11830 const struct elf_backend_data *bed;
11831 unsigned int log_file_align;
11832
11833 bed = get_elf_backend_data (h->root.u.def.section->owner);
11834 log_file_align = bed->s->log_file_align;
f6e332e6 11835 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11836 while (n--)
11837 {
11838 if (*pu)
11839 *cu = TRUE;
11840 pu++;
11841 cu++;
11842 }
11843 }
11844 }
11845
11846 return TRUE;
11847}
11848
11849static bfd_boolean
11850elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11851{
11852 asection *sec;
11853 bfd_vma hstart, hend;
11854 Elf_Internal_Rela *relstart, *relend, *rel;
11855 const struct elf_backend_data *bed;
11856 unsigned int log_file_align;
11857
11858 if (h->root.type == bfd_link_hash_warning)
11859 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11860
11861 /* Take care of both those symbols that do not describe vtables as
11862 well as those that are not loaded. */
f6e332e6 11863 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11864 return TRUE;
11865
11866 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11867 || h->root.type == bfd_link_hash_defweak);
11868
11869 sec = h->root.u.def.section;
11870 hstart = h->root.u.def.value;
11871 hend = hstart + h->size;
11872
11873 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11874 if (!relstart)
11875 return *(bfd_boolean *) okp = FALSE;
11876 bed = get_elf_backend_data (sec->owner);
11877 log_file_align = bed->s->log_file_align;
11878
11879 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11880
11881 for (rel = relstart; rel < relend; ++rel)
11882 if (rel->r_offset >= hstart && rel->r_offset < hend)
11883 {
11884 /* If the entry is in use, do nothing. */
f6e332e6
AM
11885 if (h->vtable->used
11886 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11887 {
11888 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11889 if (h->vtable->used[entry])
c152c796
AM
11890 continue;
11891 }
11892 /* Otherwise, kill it. */
11893 rel->r_offset = rel->r_info = rel->r_addend = 0;
11894 }
11895
11896 return TRUE;
11897}
11898
87538722
AM
11899/* Mark sections containing dynamically referenced symbols. When
11900 building shared libraries, we must assume that any visible symbol is
11901 referenced. */
715df9b8 11902
64d03ab5
AM
11903bfd_boolean
11904bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 11905{
87538722
AM
11906 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11907
715df9b8
EB
11908 if (h->root.type == bfd_link_hash_warning)
11909 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11910
11911 if ((h->root.type == bfd_link_hash_defined
11912 || h->root.type == bfd_link_hash_defweak)
87538722 11913 && (h->ref_dynamic
5adcfd8b 11914 || (!info->executable
87538722
AM
11915 && h->def_regular
11916 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11917 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
715df9b8
EB
11918 h->root.u.def.section->flags |= SEC_KEEP;
11919
11920 return TRUE;
11921}
3b36f7e6 11922
74f0fb50
AM
11923/* Keep all sections containing symbols undefined on the command-line,
11924 and the section containing the entry symbol. */
11925
11926void
11927_bfd_elf_gc_keep (struct bfd_link_info *info)
11928{
11929 struct bfd_sym_chain *sym;
11930
11931 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11932 {
11933 struct elf_link_hash_entry *h;
11934
11935 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11936 FALSE, FALSE, FALSE);
11937
11938 if (h != NULL
11939 && (h->root.type == bfd_link_hash_defined
11940 || h->root.type == bfd_link_hash_defweak)
11941 && !bfd_is_abs_section (h->root.u.def.section))
11942 h->root.u.def.section->flags |= SEC_KEEP;
11943 }
11944}
11945
c152c796
AM
11946/* Do mark and sweep of unused sections. */
11947
11948bfd_boolean
11949bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11950{
11951 bfd_boolean ok = TRUE;
11952 bfd *sub;
6a5bb875 11953 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 11954 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 11955
64d03ab5 11956 if (!bed->can_gc_sections
715df9b8 11957 || !is_elf_hash_table (info->hash))
c152c796
AM
11958 {
11959 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11960 return TRUE;
11961 }
11962
74f0fb50
AM
11963 bed->gc_keep (info);
11964
9d0a14d3
RS
11965 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11966 at the .eh_frame section if we can mark the FDEs individually. */
11967 _bfd_elf_begin_eh_frame_parsing (info);
11968 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11969 {
11970 asection *sec;
11971 struct elf_reloc_cookie cookie;
11972
11973 sec = bfd_get_section_by_name (sub, ".eh_frame");
11974 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11975 {
11976 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11977 if (elf_section_data (sec)->sec_info)
11978 elf_eh_frame_section (sub) = sec;
11979 fini_reloc_cookie_for_section (&cookie, sec);
11980 }
11981 }
11982 _bfd_elf_end_eh_frame_parsing (info);
11983
c152c796
AM
11984 /* Apply transitive closure to the vtable entry usage info. */
11985 elf_link_hash_traverse (elf_hash_table (info),
11986 elf_gc_propagate_vtable_entries_used,
11987 &ok);
11988 if (!ok)
11989 return FALSE;
11990
11991 /* Kill the vtable relocations that were not used. */
11992 elf_link_hash_traverse (elf_hash_table (info),
11993 elf_gc_smash_unused_vtentry_relocs,
11994 &ok);
11995 if (!ok)
11996 return FALSE;
11997
715df9b8
EB
11998 /* Mark dynamically referenced symbols. */
11999 if (elf_hash_table (info)->dynamic_sections_created)
12000 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 12001 bed->gc_mark_dynamic_ref,
87538722 12002 info);
c152c796 12003
715df9b8 12004 /* Grovel through relocs to find out who stays ... */
64d03ab5 12005 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
12006 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12007 {
12008 asection *o;
12009
12010 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12011 continue;
12012
12013 for (o = sub->sections; o != NULL; o = o->next)
a14a5de3 12014 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
39c2f51b
AM
12015 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12016 return FALSE;
c152c796
AM
12017 }
12018
6a5bb875
PB
12019 /* Allow the backend to mark additional target specific sections. */
12020 if (bed->gc_mark_extra_sections)
74f0fb50 12021 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12022
c152c796 12023 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12024 return elf_gc_sweep (abfd, info);
c152c796
AM
12025}
12026\f
12027/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12028
12029bfd_boolean
12030bfd_elf_gc_record_vtinherit (bfd *abfd,
12031 asection *sec,
12032 struct elf_link_hash_entry *h,
12033 bfd_vma offset)
12034{
12035 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12036 struct elf_link_hash_entry **search, *child;
12037 bfd_size_type extsymcount;
12038 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12039
12040 /* The sh_info field of the symtab header tells us where the
12041 external symbols start. We don't care about the local symbols at
12042 this point. */
12043 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12044 if (!elf_bad_symtab (abfd))
12045 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12046
12047 sym_hashes = elf_sym_hashes (abfd);
12048 sym_hashes_end = sym_hashes + extsymcount;
12049
12050 /* Hunt down the child symbol, which is in this section at the same
12051 offset as the relocation. */
12052 for (search = sym_hashes; search != sym_hashes_end; ++search)
12053 {
12054 if ((child = *search) != NULL
12055 && (child->root.type == bfd_link_hash_defined
12056 || child->root.type == bfd_link_hash_defweak)
12057 && child->root.u.def.section == sec
12058 && child->root.u.def.value == offset)
12059 goto win;
12060 }
12061
d003868e
AM
12062 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12063 abfd, sec, (unsigned long) offset);
c152c796
AM
12064 bfd_set_error (bfd_error_invalid_operation);
12065 return FALSE;
12066
12067 win:
f6e332e6
AM
12068 if (!child->vtable)
12069 {
a50b1753
NC
12070 child->vtable = (struct elf_link_virtual_table_entry *)
12071 bfd_zalloc (abfd, sizeof (*child->vtable));
f6e332e6
AM
12072 if (!child->vtable)
12073 return FALSE;
12074 }
c152c796
AM
12075 if (!h)
12076 {
12077 /* This *should* only be the absolute section. It could potentially
12078 be that someone has defined a non-global vtable though, which
12079 would be bad. It isn't worth paging in the local symbols to be
12080 sure though; that case should simply be handled by the assembler. */
12081
f6e332e6 12082 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12083 }
12084 else
f6e332e6 12085 child->vtable->parent = h;
c152c796
AM
12086
12087 return TRUE;
12088}
12089
12090/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12091
12092bfd_boolean
12093bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12094 asection *sec ATTRIBUTE_UNUSED,
12095 struct elf_link_hash_entry *h,
12096 bfd_vma addend)
12097{
12098 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12099 unsigned int log_file_align = bed->s->log_file_align;
12100
f6e332e6
AM
12101 if (!h->vtable)
12102 {
a50b1753
NC
12103 h->vtable = (struct elf_link_virtual_table_entry *)
12104 bfd_zalloc (abfd, sizeof (*h->vtable));
f6e332e6
AM
12105 if (!h->vtable)
12106 return FALSE;
12107 }
12108
12109 if (addend >= h->vtable->size)
c152c796
AM
12110 {
12111 size_t size, bytes, file_align;
f6e332e6 12112 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12113
12114 /* While the symbol is undefined, we have to be prepared to handle
12115 a zero size. */
12116 file_align = 1 << log_file_align;
12117 if (h->root.type == bfd_link_hash_undefined)
12118 size = addend + file_align;
12119 else
12120 {
12121 size = h->size;
12122 if (addend >= size)
12123 {
12124 /* Oops! We've got a reference past the defined end of
12125 the table. This is probably a bug -- shall we warn? */
12126 size = addend + file_align;
12127 }
12128 }
12129 size = (size + file_align - 1) & -file_align;
12130
12131 /* Allocate one extra entry for use as a "done" flag for the
12132 consolidation pass. */
12133 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12134
12135 if (ptr)
12136 {
a50b1753 12137 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12138
12139 if (ptr != NULL)
12140 {
12141 size_t oldbytes;
12142
f6e332e6 12143 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12144 * sizeof (bfd_boolean));
12145 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12146 }
12147 }
12148 else
a50b1753 12149 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12150
12151 if (ptr == NULL)
12152 return FALSE;
12153
12154 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12155 h->vtable->used = ptr + 1;
12156 h->vtable->size = size;
c152c796
AM
12157 }
12158
f6e332e6 12159 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12160
12161 return TRUE;
12162}
12163
12164struct alloc_got_off_arg {
12165 bfd_vma gotoff;
10455f89 12166 struct bfd_link_info *info;
c152c796
AM
12167};
12168
12169/* We need a special top-level link routine to convert got reference counts
12170 to real got offsets. */
12171
12172static bfd_boolean
12173elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12174{
a50b1753 12175 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
12176 bfd *obfd = gofarg->info->output_bfd;
12177 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796
AM
12178
12179 if (h->root.type == bfd_link_hash_warning)
12180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12181
12182 if (h->got.refcount > 0)
12183 {
12184 h->got.offset = gofarg->gotoff;
10455f89 12185 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
12186 }
12187 else
12188 h->got.offset = (bfd_vma) -1;
12189
12190 return TRUE;
12191}
12192
12193/* And an accompanying bit to work out final got entry offsets once
12194 we're done. Should be called from final_link. */
12195
12196bfd_boolean
12197bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12198 struct bfd_link_info *info)
12199{
12200 bfd *i;
12201 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12202 bfd_vma gotoff;
c152c796
AM
12203 struct alloc_got_off_arg gofarg;
12204
10455f89
HPN
12205 BFD_ASSERT (abfd == info->output_bfd);
12206
c152c796
AM
12207 if (! is_elf_hash_table (info->hash))
12208 return FALSE;
12209
12210 /* The GOT offset is relative to the .got section, but the GOT header is
12211 put into the .got.plt section, if the backend uses it. */
12212 if (bed->want_got_plt)
12213 gotoff = 0;
12214 else
12215 gotoff = bed->got_header_size;
12216
12217 /* Do the local .got entries first. */
12218 for (i = info->input_bfds; i; i = i->link_next)
12219 {
12220 bfd_signed_vma *local_got;
12221 bfd_size_type j, locsymcount;
12222 Elf_Internal_Shdr *symtab_hdr;
12223
12224 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12225 continue;
12226
12227 local_got = elf_local_got_refcounts (i);
12228 if (!local_got)
12229 continue;
12230
12231 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12232 if (elf_bad_symtab (i))
12233 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12234 else
12235 locsymcount = symtab_hdr->sh_info;
12236
12237 for (j = 0; j < locsymcount; ++j)
12238 {
12239 if (local_got[j] > 0)
12240 {
12241 local_got[j] = gotoff;
10455f89 12242 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
12243 }
12244 else
12245 local_got[j] = (bfd_vma) -1;
12246 }
12247 }
12248
12249 /* Then the global .got entries. .plt refcounts are handled by
12250 adjust_dynamic_symbol */
12251 gofarg.gotoff = gotoff;
10455f89 12252 gofarg.info = info;
c152c796
AM
12253 elf_link_hash_traverse (elf_hash_table (info),
12254 elf_gc_allocate_got_offsets,
12255 &gofarg);
12256 return TRUE;
12257}
12258
12259/* Many folk need no more in the way of final link than this, once
12260 got entry reference counting is enabled. */
12261
12262bfd_boolean
12263bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12264{
12265 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12266 return FALSE;
12267
12268 /* Invoke the regular ELF backend linker to do all the work. */
12269 return bfd_elf_final_link (abfd, info);
12270}
12271
12272bfd_boolean
12273bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12274{
a50b1753 12275 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
12276
12277 if (rcookie->bad_symtab)
12278 rcookie->rel = rcookie->rels;
12279
12280 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12281 {
12282 unsigned long r_symndx;
12283
12284 if (! rcookie->bad_symtab)
12285 if (rcookie->rel->r_offset > offset)
12286 return FALSE;
12287 if (rcookie->rel->r_offset != offset)
12288 continue;
12289
12290 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 12291 if (r_symndx == STN_UNDEF)
c152c796
AM
12292 return TRUE;
12293
12294 if (r_symndx >= rcookie->locsymcount
12295 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12296 {
12297 struct elf_link_hash_entry *h;
12298
12299 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12300
12301 while (h->root.type == bfd_link_hash_indirect
12302 || h->root.type == bfd_link_hash_warning)
12303 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12304
12305 if ((h->root.type == bfd_link_hash_defined
12306 || h->root.type == bfd_link_hash_defweak)
12307 && elf_discarded_section (h->root.u.def.section))
12308 return TRUE;
12309 else
12310 return FALSE;
12311 }
12312 else
12313 {
12314 /* It's not a relocation against a global symbol,
12315 but it could be a relocation against a local
12316 symbol for a discarded section. */
12317 asection *isec;
12318 Elf_Internal_Sym *isym;
12319
12320 /* Need to: get the symbol; get the section. */
12321 isym = &rcookie->locsyms[r_symndx];
cb33740c
AM
12322 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12323 if (isec != NULL && elf_discarded_section (isec))
12324 return TRUE;
c152c796
AM
12325 }
12326 return FALSE;
12327 }
12328 return FALSE;
12329}
12330
12331/* Discard unneeded references to discarded sections.
12332 Returns TRUE if any section's size was changed. */
12333/* This function assumes that the relocations are in sorted order,
12334 which is true for all known assemblers. */
12335
12336bfd_boolean
12337bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12338{
12339 struct elf_reloc_cookie cookie;
12340 asection *stab, *eh;
c152c796
AM
12341 const struct elf_backend_data *bed;
12342 bfd *abfd;
c152c796
AM
12343 bfd_boolean ret = FALSE;
12344
12345 if (info->traditional_format
12346 || !is_elf_hash_table (info->hash))
12347 return FALSE;
12348
ca92cecb 12349 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
12350 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12351 {
12352 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12353 continue;
12354
12355 bed = get_elf_backend_data (abfd);
12356
12357 if ((abfd->flags & DYNAMIC) != 0)
12358 continue;
12359
8da3dbc5
AM
12360 eh = NULL;
12361 if (!info->relocatable)
12362 {
12363 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12364 if (eh != NULL
eea6121a 12365 && (eh->size == 0
8da3dbc5
AM
12366 || bfd_is_abs_section (eh->output_section)))
12367 eh = NULL;
12368 }
c152c796
AM
12369
12370 stab = bfd_get_section_by_name (abfd, ".stab");
12371 if (stab != NULL
eea6121a 12372 && (stab->size == 0
c152c796
AM
12373 || bfd_is_abs_section (stab->output_section)
12374 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12375 stab = NULL;
12376
12377 if (stab == NULL
12378 && eh == NULL
12379 && bed->elf_backend_discard_info == NULL)
12380 continue;
12381
5241d853
RS
12382 if (!init_reloc_cookie (&cookie, info, abfd))
12383 return FALSE;
c152c796 12384
5241d853
RS
12385 if (stab != NULL
12386 && stab->reloc_count > 0
12387 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 12388 {
5241d853
RS
12389 if (_bfd_discard_section_stabs (abfd, stab,
12390 elf_section_data (stab)->sec_info,
12391 bfd_elf_reloc_symbol_deleted_p,
12392 &cookie))
12393 ret = TRUE;
12394 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
12395 }
12396
5241d853
RS
12397 if (eh != NULL
12398 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 12399 {
ca92cecb 12400 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
12401 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12402 bfd_elf_reloc_symbol_deleted_p,
12403 &cookie))
12404 ret = TRUE;
5241d853 12405 fini_reloc_cookie_rels (&cookie, eh);
c152c796
AM
12406 }
12407
12408 if (bed->elf_backend_discard_info != NULL
12409 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12410 ret = TRUE;
12411
5241d853 12412 fini_reloc_cookie (&cookie, abfd);
c152c796 12413 }
ca92cecb 12414 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
12415
12416 if (info->eh_frame_hdr
12417 && !info->relocatable
12418 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12419 ret = TRUE;
12420
12421 return ret;
12422}
082b7297 12423
9659de1c
AM
12424/* For a SHT_GROUP section, return the group signature. For other
12425 sections, return the normal section name. */
12426
12427static const char *
12428section_signature (asection *sec)
12429{
12430 if ((sec->flags & SEC_GROUP) != 0
12431 && elf_next_in_group (sec) != NULL
12432 && elf_group_name (elf_next_in_group (sec)) != NULL)
12433 return elf_group_name (elf_next_in_group (sec));
12434 return sec->name;
12435}
12436
082b7297 12437void
9659de1c 12438_bfd_elf_section_already_linked (bfd *abfd, asection *sec,
c0f00686 12439 struct bfd_link_info *info)
082b7297
L
12440{
12441 flagword flags;
6d2cd210 12442 const char *name, *p;
082b7297
L
12443 struct bfd_section_already_linked *l;
12444 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666 12445
3d7f7666
L
12446 if (sec->output_section == bfd_abs_section_ptr)
12447 return;
082b7297
L
12448
12449 flags = sec->flags;
3d7f7666 12450
c2370991
AM
12451 /* Return if it isn't a linkonce section. A comdat group section
12452 also has SEC_LINK_ONCE set. */
12453 if ((flags & SEC_LINK_ONCE) == 0)
082b7297
L
12454 return;
12455
c2370991
AM
12456 /* Don't put group member sections on our list of already linked
12457 sections. They are handled as a group via their group section. */
12458 if (elf_sec_group (sec) != NULL)
12459 return;
3d7f7666 12460
082b7297
L
12461 /* FIXME: When doing a relocatable link, we may have trouble
12462 copying relocations in other sections that refer to local symbols
12463 in the section being discarded. Those relocations will have to
12464 be converted somehow; as of this writing I'm not sure that any of
12465 the backends handle that correctly.
12466
12467 It is tempting to instead not discard link once sections when
12468 doing a relocatable link (technically, they should be discarded
12469 whenever we are building constructors). However, that fails,
12470 because the linker winds up combining all the link once sections
12471 into a single large link once section, which defeats the purpose
12472 of having link once sections in the first place.
12473
12474 Also, not merging link once sections in a relocatable link
12475 causes trouble for MIPS ELF, which relies on link once semantics
12476 to handle the .reginfo section correctly. */
12477
9659de1c 12478 name = section_signature (sec);
082b7297 12479
0112cd26 12480 if (CONST_STRNEQ (name, ".gnu.linkonce.")
6d2cd210
JJ
12481 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12482 p++;
12483 else
12484 p = name;
12485
12486 already_linked_list = bfd_section_already_linked_table_lookup (p);
082b7297
L
12487
12488 for (l = already_linked_list->entry; l != NULL; l = l->next)
12489 {
c2370991
AM
12490 /* We may have 2 different types of sections on the list: group
12491 sections and linkonce sections. Match like sections. */
3d7f7666 12492 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9659de1c 12493 && strcmp (name, section_signature (l->sec)) == 0
082b7297
L
12494 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12495 {
12496 /* The section has already been linked. See if we should
6d2cd210 12497 issue a warning. */
082b7297
L
12498 switch (flags & SEC_LINK_DUPLICATES)
12499 {
12500 default:
12501 abort ();
12502
12503 case SEC_LINK_DUPLICATES_DISCARD:
12504 break;
12505
12506 case SEC_LINK_DUPLICATES_ONE_ONLY:
12507 (*_bfd_error_handler)
c93625e2 12508 (_("%B: ignoring duplicate section `%A'"),
d003868e 12509 abfd, sec);
082b7297
L
12510 break;
12511
12512 case SEC_LINK_DUPLICATES_SAME_SIZE:
12513 if (sec->size != l->sec->size)
12514 (*_bfd_error_handler)
c93625e2 12515 (_("%B: duplicate section `%A' has different size"),
d003868e 12516 abfd, sec);
082b7297 12517 break;
ea5158d8
DJ
12518
12519 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12520 if (sec->size != l->sec->size)
12521 (*_bfd_error_handler)
c93625e2 12522 (_("%B: duplicate section `%A' has different size"),
ea5158d8
DJ
12523 abfd, sec);
12524 else if (sec->size != 0)
12525 {
12526 bfd_byte *sec_contents, *l_sec_contents;
12527
12528 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12529 (*_bfd_error_handler)
c93625e2 12530 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12531 abfd, sec);
12532 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12533 &l_sec_contents))
12534 (*_bfd_error_handler)
c93625e2 12535 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12536 l->sec->owner, l->sec);
12537 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12538 (*_bfd_error_handler)
c93625e2 12539 (_("%B: warning: duplicate section `%A' has different contents"),
ea5158d8
DJ
12540 abfd, sec);
12541
12542 if (sec_contents)
12543 free (sec_contents);
12544 if (l_sec_contents)
12545 free (l_sec_contents);
12546 }
12547 break;
082b7297
L
12548 }
12549
12550 /* Set the output_section field so that lang_add_section
12551 does not create a lang_input_section structure for this
12552 section. Since there might be a symbol in the section
12553 being discarded, we must retain a pointer to the section
12554 which we are really going to use. */
12555 sec->output_section = bfd_abs_section_ptr;
12556 sec->kept_section = l->sec;
3b36f7e6 12557
082b7297 12558 if (flags & SEC_GROUP)
3d7f7666
L
12559 {
12560 asection *first = elf_next_in_group (sec);
12561 asection *s = first;
12562
12563 while (s != NULL)
12564 {
12565 s->output_section = bfd_abs_section_ptr;
12566 /* Record which group discards it. */
12567 s->kept_section = l->sec;
12568 s = elf_next_in_group (s);
12569 /* These lists are circular. */
12570 if (s == first)
12571 break;
12572 }
12573 }
082b7297
L
12574
12575 return;
12576 }
12577 }
12578
c2370991
AM
12579 /* A single member comdat group section may be discarded by a
12580 linkonce section and vice versa. */
12581
12582 if ((flags & SEC_GROUP) != 0)
3d7f7666 12583 {
c2370991
AM
12584 asection *first = elf_next_in_group (sec);
12585
12586 if (first != NULL && elf_next_in_group (first) == first)
12587 /* Check this single member group against linkonce sections. */
12588 for (l = already_linked_list->entry; l != NULL; l = l->next)
12589 if ((l->sec->flags & SEC_GROUP) == 0
12590 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12591 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12592 {
12593 first->output_section = bfd_abs_section_ptr;
12594 first->kept_section = l->sec;
12595 sec->output_section = bfd_abs_section_ptr;
12596 break;
12597 }
3d7f7666
L
12598 }
12599 else
c2370991 12600 /* Check this linkonce section against single member groups. */
6d2cd210
JJ
12601 for (l = already_linked_list->entry; l != NULL; l = l->next)
12602 if (l->sec->flags & SEC_GROUP)
12603 {
12604 asection *first = elf_next_in_group (l->sec);
12605
12606 if (first != NULL
12607 && elf_next_in_group (first) == first
c0f00686 12608 && bfd_elf_match_symbols_in_sections (first, sec, info))
6d2cd210
JJ
12609 {
12610 sec->output_section = bfd_abs_section_ptr;
c2370991 12611 sec->kept_section = first;
6d2cd210
JJ
12612 break;
12613 }
12614 }
12615
80c29487
JK
12616 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12617 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12618 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12619 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12620 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12621 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12622 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12623 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12624 The reverse order cannot happen as there is never a bfd with only the
12625 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12626 matter as here were are looking only for cross-bfd sections. */
12627
12628 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12629 for (l = already_linked_list->entry; l != NULL; l = l->next)
12630 if ((l->sec->flags & SEC_GROUP) == 0
12631 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12632 {
12633 if (abfd != l->sec->owner)
12634 sec->output_section = bfd_abs_section_ptr;
12635 break;
12636 }
12637
082b7297 12638 /* This is the first section with this name. Record it. */
a6626e8c 12639 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 12640 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
082b7297 12641}
81e1b023 12642
a4d8e49b
L
12643bfd_boolean
12644_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12645{
12646 return sym->st_shndx == SHN_COMMON;
12647}
12648
12649unsigned int
12650_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12651{
12652 return SHN_COMMON;
12653}
12654
12655asection *
12656_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12657{
12658 return bfd_com_section_ptr;
12659}
10455f89
HPN
12660
12661bfd_vma
12662_bfd_elf_default_got_elt_size (bfd *abfd,
12663 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12664 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12665 bfd *ibfd ATTRIBUTE_UNUSED,
12666 unsigned long symndx ATTRIBUTE_UNUSED)
12667{
12668 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12669 return bed->s->arch_size / 8;
12670}
83bac4b0
NC
12671
12672/* Routines to support the creation of dynamic relocs. */
12673
83bac4b0
NC
12674/* Returns the name of the dynamic reloc section associated with SEC. */
12675
12676static const char *
12677get_dynamic_reloc_section_name (bfd * abfd,
12678 asection * sec,
12679 bfd_boolean is_rela)
12680{
ddcf1fcf
BS
12681 char *name;
12682 const char *old_name = bfd_get_section_name (NULL, sec);
12683 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 12684
ddcf1fcf 12685 if (old_name == NULL)
83bac4b0
NC
12686 return NULL;
12687
ddcf1fcf
BS
12688 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12689 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
12690
12691 return name;
12692}
12693
12694/* Returns the dynamic reloc section associated with SEC.
12695 If necessary compute the name of the dynamic reloc section based
12696 on SEC's name (looked up in ABFD's string table) and the setting
12697 of IS_RELA. */
12698
12699asection *
12700_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12701 asection * sec,
12702 bfd_boolean is_rela)
12703{
12704 asection * reloc_sec = elf_section_data (sec)->sreloc;
12705
12706 if (reloc_sec == NULL)
12707 {
12708 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12709
12710 if (name != NULL)
12711 {
12712 reloc_sec = bfd_get_section_by_name (abfd, name);
12713
12714 if (reloc_sec != NULL)
12715 elf_section_data (sec)->sreloc = reloc_sec;
12716 }
12717 }
12718
12719 return reloc_sec;
12720}
12721
12722/* Returns the dynamic reloc section associated with SEC. If the
12723 section does not exist it is created and attached to the DYNOBJ
12724 bfd and stored in the SRELOC field of SEC's elf_section_data
12725 structure.
f8076f98 12726
83bac4b0
NC
12727 ALIGNMENT is the alignment for the newly created section and
12728 IS_RELA defines whether the name should be .rela.<SEC's name>
12729 or .rel.<SEC's name>. The section name is looked up in the
12730 string table associated with ABFD. */
12731
12732asection *
12733_bfd_elf_make_dynamic_reloc_section (asection * sec,
12734 bfd * dynobj,
12735 unsigned int alignment,
12736 bfd * abfd,
12737 bfd_boolean is_rela)
12738{
12739 asection * reloc_sec = elf_section_data (sec)->sreloc;
12740
12741 if (reloc_sec == NULL)
12742 {
12743 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12744
12745 if (name == NULL)
12746 return NULL;
12747
12748 reloc_sec = bfd_get_section_by_name (dynobj, name);
12749
12750 if (reloc_sec == NULL)
12751 {
12752 flagword flags;
12753
12754 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12755 if ((sec->flags & SEC_ALLOC) != 0)
12756 flags |= SEC_ALLOC | SEC_LOAD;
12757
12758 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12759 if (reloc_sec != NULL)
12760 {
12761 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12762 reloc_sec = NULL;
12763 }
12764 }
12765
12766 elf_section_data (sec)->sreloc = reloc_sec;
12767 }
12768
12769 return reloc_sec;
12770}
1338dd10
PB
12771
12772/* Copy the ELF symbol type associated with a linker hash entry. */
12773void
12774_bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12775 struct bfd_link_hash_entry * hdest,
12776 struct bfd_link_hash_entry * hsrc)
12777{
12778 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12779 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12780
12781 ehdest->type = ehsrc->type;
35fc36a8 12782 ehdest->target_internal = ehsrc->target_internal;
1338dd10 12783}
351f65ca
L
12784
12785/* Append a RELA relocation REL to section S in BFD. */
12786
12787void
12788elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12789{
12790 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12791 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12792 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12793 bed->s->swap_reloca_out (abfd, rel, loc);
12794}
12795
12796/* Append a REL relocation REL to section S in BFD. */
12797
12798void
12799elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12800{
12801 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12802 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12803 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12804 bed->s->swap_reloca_out (abfd, rel, loc);
12805}
This page took 1.483012 seconds and 4 git commands to generate.