]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
sim: bfin: switch to new syscall trace level
[thirdparty/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. */
dd98f8d2
NC
3426 /* PR 12761: Also generate this warning when building shared libraries. */
3427 if (info->executable || info->shared)
4ad4eba5
AM
3428 {
3429 asection *s;
3430
3431 for (s = abfd->sections; s != NULL; s = s->next)
3432 {
3433 const char *name;
3434
3435 name = bfd_get_section_name (abfd, s);
0112cd26 3436 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5
AM
3437 {
3438 char *msg;
3439 bfd_size_type sz;
4ad4eba5
AM
3440
3441 name += sizeof ".gnu.warning." - 1;
3442
3443 /* If this is a shared object, then look up the symbol
3444 in the hash table. If it is there, and it is already
3445 been defined, then we will not be using the entry
3446 from this shared object, so we don't need to warn.
3447 FIXME: If we see the definition in a regular object
3448 later on, we will warn, but we shouldn't. The only
3449 fix is to keep track of what warnings we are supposed
3450 to emit, and then handle them all at the end of the
3451 link. */
3452 if (dynamic)
3453 {
3454 struct elf_link_hash_entry *h;
3455
66eb6687 3456 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4ad4eba5
AM
3457
3458 /* FIXME: What about bfd_link_hash_common? */
3459 if (h != NULL
3460 && (h->root.type == bfd_link_hash_defined
3461 || h->root.type == bfd_link_hash_defweak))
3462 {
3463 /* We don't want to issue this warning. Clobber
3464 the section size so that the warning does not
3465 get copied into the output file. */
eea6121a 3466 s->size = 0;
4ad4eba5
AM
3467 continue;
3468 }
3469 }
3470
eea6121a 3471 sz = s->size;
a50b1753 3472 msg = (char *) bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3473 if (msg == NULL)
3474 goto error_return;
3475
370a0e1b 3476 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3477 goto error_return;
3478
370a0e1b 3479 msg[sz] = '\0';
4ad4eba5
AM
3480
3481 if (! (_bfd_generic_link_add_one_symbol
3482 (info, abfd, name, BSF_WARNING, s, 0, msg,
66eb6687 3483 FALSE, bed->collect, NULL)))
4ad4eba5
AM
3484 goto error_return;
3485
3486 if (! info->relocatable)
3487 {
3488 /* Clobber the section size so that the warning does
3489 not get copied into the output file. */
eea6121a 3490 s->size = 0;
11d2f718
AM
3491
3492 /* Also set SEC_EXCLUDE, so that symbols defined in
3493 the warning section don't get copied to the output. */
3494 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3495 }
3496 }
3497 }
3498 }
3499
3500 add_needed = TRUE;
3501 if (! dynamic)
3502 {
3503 /* If we are creating a shared library, create all the dynamic
3504 sections immediately. We need to attach them to something,
3505 so we attach them to this BFD, provided it is the right
3506 format. FIXME: If there are no input BFD's of the same
3507 format as the output, we can't make a shared library. */
3508 if (info->shared
66eb6687 3509 && is_elf_hash_table (htab)
f13a99db 3510 && info->output_bfd->xvec == abfd->xvec
66eb6687 3511 && !htab->dynamic_sections_created)
4ad4eba5
AM
3512 {
3513 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3514 goto error_return;
3515 }
3516 }
66eb6687 3517 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3518 goto error_return;
3519 else
3520 {
3521 asection *s;
3522 const char *soname = NULL;
7ee314fa 3523 char *audit = NULL;
4ad4eba5
AM
3524 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3525 int ret;
3526
3527 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3528 ld shouldn't allow it. */
4ad4eba5
AM
3529 if ((s = abfd->sections) != NULL
3530 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
92fd189d 3531 abort ();
4ad4eba5
AM
3532
3533 /* If this dynamic lib was specified on the command line with
3534 --as-needed in effect, then we don't want to add a DT_NEEDED
3535 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3536 in by another lib's DT_NEEDED. When --no-add-needed is used
3537 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3538 any dynamic library in DT_NEEDED tags in the dynamic lib at
3539 all. */
3540 add_needed = (elf_dyn_lib_class (abfd)
3541 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3542 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3543
3544 s = bfd_get_section_by_name (abfd, ".dynamic");
3545 if (s != NULL)
3546 {
3547 bfd_byte *dynbuf;
3548 bfd_byte *extdyn;
cb33740c 3549 unsigned int elfsec;
4ad4eba5
AM
3550 unsigned long shlink;
3551
eea6121a 3552 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3553 {
3554error_free_dyn:
3555 free (dynbuf);
3556 goto error_return;
3557 }
4ad4eba5
AM
3558
3559 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3560 if (elfsec == SHN_BAD)
4ad4eba5
AM
3561 goto error_free_dyn;
3562 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3563
3564 for (extdyn = dynbuf;
eea6121a 3565 extdyn < dynbuf + s->size;
4ad4eba5
AM
3566 extdyn += bed->s->sizeof_dyn)
3567 {
3568 Elf_Internal_Dyn dyn;
3569
3570 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3571 if (dyn.d_tag == DT_SONAME)
3572 {
3573 unsigned int tagv = dyn.d_un.d_val;
3574 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3575 if (soname == NULL)
3576 goto error_free_dyn;
3577 }
3578 if (dyn.d_tag == DT_NEEDED)
3579 {
3580 struct bfd_link_needed_list *n, **pn;
3581 char *fnm, *anm;
3582 unsigned int tagv = dyn.d_un.d_val;
3583
3584 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3585 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3586 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3587 if (n == NULL || fnm == NULL)
3588 goto error_free_dyn;
3589 amt = strlen (fnm) + 1;
a50b1753 3590 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3591 if (anm == NULL)
3592 goto error_free_dyn;
3593 memcpy (anm, fnm, amt);
3594 n->name = anm;
3595 n->by = abfd;
3596 n->next = NULL;
66eb6687 3597 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3598 ;
3599 *pn = n;
3600 }
3601 if (dyn.d_tag == DT_RUNPATH)
3602 {
3603 struct bfd_link_needed_list *n, **pn;
3604 char *fnm, *anm;
3605 unsigned int tagv = dyn.d_un.d_val;
3606
3607 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3608 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3609 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3610 if (n == NULL || fnm == NULL)
3611 goto error_free_dyn;
3612 amt = strlen (fnm) + 1;
a50b1753 3613 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3614 if (anm == NULL)
3615 goto error_free_dyn;
3616 memcpy (anm, fnm, amt);
3617 n->name = anm;
3618 n->by = abfd;
3619 n->next = NULL;
3620 for (pn = & runpath;
3621 *pn != NULL;
3622 pn = &(*pn)->next)
3623 ;
3624 *pn = n;
3625 }
3626 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3627 if (!runpath && dyn.d_tag == DT_RPATH)
3628 {
3629 struct bfd_link_needed_list *n, **pn;
3630 char *fnm, *anm;
3631 unsigned int tagv = dyn.d_un.d_val;
3632
3633 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3634 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3635 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3636 if (n == NULL || fnm == NULL)
3637 goto error_free_dyn;
3638 amt = strlen (fnm) + 1;
a50b1753 3639 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3640 if (anm == NULL)
f8703194 3641 goto error_free_dyn;
4ad4eba5
AM
3642 memcpy (anm, fnm, amt);
3643 n->name = anm;
3644 n->by = abfd;
3645 n->next = NULL;
3646 for (pn = & rpath;
3647 *pn != NULL;
3648 pn = &(*pn)->next)
3649 ;
3650 *pn = n;
3651 }
7ee314fa
AM
3652 if (dyn.d_tag == DT_AUDIT)
3653 {
3654 unsigned int tagv = dyn.d_un.d_val;
3655 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3656 }
4ad4eba5
AM
3657 }
3658
3659 free (dynbuf);
3660 }
3661
3662 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3663 frees all more recently bfd_alloc'd blocks as well. */
3664 if (runpath)
3665 rpath = runpath;
3666
3667 if (rpath)
3668 {
3669 struct bfd_link_needed_list **pn;
66eb6687 3670 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3671 ;
3672 *pn = rpath;
3673 }
3674
3675 /* We do not want to include any of the sections in a dynamic
3676 object in the output file. We hack by simply clobbering the
3677 list of sections in the BFD. This could be handled more
3678 cleanly by, say, a new section flag; the existing
3679 SEC_NEVER_LOAD flag is not the one we want, because that one
3680 still implies that the section takes up space in the output
3681 file. */
3682 bfd_section_list_clear (abfd);
3683
4ad4eba5
AM
3684 /* Find the name to use in a DT_NEEDED entry that refers to this
3685 object. If the object has a DT_SONAME entry, we use it.
3686 Otherwise, if the generic linker stuck something in
3687 elf_dt_name, we use that. Otherwise, we just use the file
3688 name. */
3689 if (soname == NULL || *soname == '\0')
3690 {
3691 soname = elf_dt_name (abfd);
3692 if (soname == NULL || *soname == '\0')
3693 soname = bfd_get_filename (abfd);
3694 }
3695
3696 /* Save the SONAME because sometimes the linker emulation code
3697 will need to know it. */
3698 elf_dt_name (abfd) = soname;
3699
7e9f0867 3700 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3701 if (ret < 0)
3702 goto error_return;
3703
3704 /* If we have already included this dynamic object in the
3705 link, just ignore it. There is no reason to include a
3706 particular dynamic object more than once. */
3707 if (ret > 0)
3708 return TRUE;
7ee314fa
AM
3709
3710 /* Save the DT_AUDIT entry for the linker emulation code. */
3711 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3712 }
3713
3714 /* If this is a dynamic object, we always link against the .dynsym
3715 symbol table, not the .symtab symbol table. The dynamic linker
3716 will only see the .dynsym symbol table, so there is no reason to
3717 look at .symtab for a dynamic object. */
3718
3719 if (! dynamic || elf_dynsymtab (abfd) == 0)
3720 hdr = &elf_tdata (abfd)->symtab_hdr;
3721 else
3722 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3723
3724 symcount = hdr->sh_size / bed->s->sizeof_sym;
3725
3726 /* The sh_info field of the symtab header tells us where the
3727 external symbols start. We don't care about the local symbols at
3728 this point. */
3729 if (elf_bad_symtab (abfd))
3730 {
3731 extsymcount = symcount;
3732 extsymoff = 0;
3733 }
3734 else
3735 {
3736 extsymcount = symcount - hdr->sh_info;
3737 extsymoff = hdr->sh_info;
3738 }
3739
3740 sym_hash = NULL;
3741 if (extsymcount != 0)
3742 {
3743 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3744 NULL, NULL, NULL);
3745 if (isymbuf == NULL)
3746 goto error_return;
3747
3748 /* We store a pointer to the hash table entry for each external
3749 symbol. */
3750 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 3751 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4ad4eba5
AM
3752 if (sym_hash == NULL)
3753 goto error_free_sym;
3754 elf_sym_hashes (abfd) = sym_hash;
3755 }
3756
3757 if (dynamic)
3758 {
3759 /* Read in any version definitions. */
fc0e6df6
PB
3760 if (!_bfd_elf_slurp_version_tables (abfd,
3761 info->default_imported_symver))
4ad4eba5
AM
3762 goto error_free_sym;
3763
3764 /* Read in the symbol versions, but don't bother to convert them
3765 to internal format. */
3766 if (elf_dynversym (abfd) != 0)
3767 {
3768 Elf_Internal_Shdr *versymhdr;
3769
3770 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3771 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3772 if (extversym == NULL)
3773 goto error_free_sym;
3774 amt = versymhdr->sh_size;
3775 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3776 || bfd_bread (extversym, amt, abfd) != amt)
3777 goto error_free_vers;
3778 }
3779 }
3780
66eb6687
AM
3781 /* If we are loading an as-needed shared lib, save the symbol table
3782 state before we start adding symbols. If the lib turns out
3783 to be unneeded, restore the state. */
3784 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3785 {
3786 unsigned int i;
3787 size_t entsize;
3788
3789 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3790 {
3791 struct bfd_hash_entry *p;
2de92251 3792 struct elf_link_hash_entry *h;
66eb6687
AM
3793
3794 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3795 {
3796 h = (struct elf_link_hash_entry *) p;
3797 entsize += htab->root.table.entsize;
3798 if (h->root.type == bfd_link_hash_warning)
3799 entsize += htab->root.table.entsize;
3800 }
66eb6687
AM
3801 }
3802
3803 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3804 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3805 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3806 if (old_tab == NULL)
3807 goto error_free_vers;
3808
3809 /* Remember the current objalloc pointer, so that all mem for
3810 symbols added can later be reclaimed. */
3811 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3812 if (alloc_mark == NULL)
3813 goto error_free_vers;
3814
5061a885
AM
3815 /* Make a special call to the linker "notice" function to
3816 tell it that we are about to handle an as-needed lib. */
3817 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 3818 notice_as_needed, 0, NULL))
9af2a943 3819 goto error_free_vers;
5061a885 3820
66eb6687
AM
3821 /* Clone the symbol table and sym hashes. Remember some
3822 pointers into the symbol table, and dynamic symbol count. */
3823 old_hash = (char *) old_tab + tabsize;
3824 old_ent = (char *) old_hash + hashsize;
3825 memcpy (old_tab, htab->root.table.table, tabsize);
3826 memcpy (old_hash, sym_hash, hashsize);
3827 old_undefs = htab->root.undefs;
3828 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3829 old_table = htab->root.table.table;
3830 old_size = htab->root.table.size;
3831 old_count = htab->root.table.count;
66eb6687
AM
3832 old_dynsymcount = htab->dynsymcount;
3833
3834 for (i = 0; i < htab->root.table.size; i++)
3835 {
3836 struct bfd_hash_entry *p;
2de92251 3837 struct elf_link_hash_entry *h;
66eb6687
AM
3838
3839 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3840 {
3841 memcpy (old_ent, p, htab->root.table.entsize);
3842 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3843 h = (struct elf_link_hash_entry *) p;
3844 if (h->root.type == bfd_link_hash_warning)
3845 {
3846 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3847 old_ent = (char *) old_ent + htab->root.table.entsize;
3848 }
66eb6687
AM
3849 }
3850 }
3851 }
4ad4eba5 3852
66eb6687 3853 weaks = NULL;
4ad4eba5
AM
3854 ever = extversym != NULL ? extversym + extsymoff : NULL;
3855 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3856 isym < isymend;
3857 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3858 {
3859 int bind;
3860 bfd_vma value;
af44c138 3861 asection *sec, *new_sec;
4ad4eba5
AM
3862 flagword flags;
3863 const char *name;
3864 struct elf_link_hash_entry *h;
3865 bfd_boolean definition;
3866 bfd_boolean size_change_ok;
3867 bfd_boolean type_change_ok;
3868 bfd_boolean new_weakdef;
3869 bfd_boolean override;
a4d8e49b 3870 bfd_boolean common;
4ad4eba5
AM
3871 unsigned int old_alignment;
3872 bfd *old_bfd;
3cbc5de0 3873 bfd * undef_bfd = NULL;
4ad4eba5
AM
3874
3875 override = FALSE;
3876
3877 flags = BSF_NO_FLAGS;
3878 sec = NULL;
3879 value = isym->st_value;
3880 *sym_hash = NULL;
a4d8e49b 3881 common = bed->common_definition (isym);
4ad4eba5
AM
3882
3883 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3884 switch (bind)
4ad4eba5 3885 {
3e7a7d11 3886 case STB_LOCAL:
4ad4eba5
AM
3887 /* This should be impossible, since ELF requires that all
3888 global symbols follow all local symbols, and that sh_info
3889 point to the first global symbol. Unfortunately, Irix 5
3890 screws this up. */
3891 continue;
3e7a7d11
NC
3892
3893 case STB_GLOBAL:
a4d8e49b 3894 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3895 flags = BSF_GLOBAL;
3e7a7d11
NC
3896 break;
3897
3898 case STB_WEAK:
3899 flags = BSF_WEAK;
3900 break;
3901
3902 case STB_GNU_UNIQUE:
3903 flags = BSF_GNU_UNIQUE;
3904 break;
3905
3906 default:
4ad4eba5 3907 /* Leave it up to the processor backend. */
3e7a7d11 3908 break;
4ad4eba5
AM
3909 }
3910
3911 if (isym->st_shndx == SHN_UNDEF)
3912 sec = bfd_und_section_ptr;
cb33740c
AM
3913 else if (isym->st_shndx == SHN_ABS)
3914 sec = bfd_abs_section_ptr;
3915 else if (isym->st_shndx == SHN_COMMON)
3916 {
3917 sec = bfd_com_section_ptr;
3918 /* What ELF calls the size we call the value. What ELF
3919 calls the value we call the alignment. */
3920 value = isym->st_size;
3921 }
3922 else
4ad4eba5
AM
3923 {
3924 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3925 if (sec == NULL)
3926 sec = bfd_abs_section_ptr;
529fcb95
PB
3927 else if (sec->kept_section)
3928 {
e5d08002
L
3929 /* Symbols from discarded section are undefined. We keep
3930 its visibility. */
529fcb95
PB
3931 sec = bfd_und_section_ptr;
3932 isym->st_shndx = SHN_UNDEF;
3933 }
4ad4eba5
AM
3934 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3935 value -= sec->vma;
3936 }
4ad4eba5
AM
3937
3938 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3939 isym->st_name);
3940 if (name == NULL)
3941 goto error_free_vers;
3942
3943 if (isym->st_shndx == SHN_COMMON
02d00247
AM
3944 && (abfd->flags & BFD_PLUGIN) != 0)
3945 {
3946 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3947
3948 if (xc == NULL)
3949 {
3950 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3951 | SEC_EXCLUDE);
3952 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3953 if (xc == NULL)
3954 goto error_free_vers;
3955 }
3956 sec = xc;
3957 }
3958 else if (isym->st_shndx == SHN_COMMON
3959 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3960 && !info->relocatable)
4ad4eba5
AM
3961 {
3962 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3963
3964 if (tcomm == NULL)
3965 {
02d00247
AM
3966 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3967 | SEC_LINKER_CREATED);
3968 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 3969 if (tcomm == NULL)
4ad4eba5
AM
3970 goto error_free_vers;
3971 }
3972 sec = tcomm;
3973 }
66eb6687 3974 else if (bed->elf_add_symbol_hook)
4ad4eba5 3975 {
66eb6687
AM
3976 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3977 &sec, &value))
4ad4eba5
AM
3978 goto error_free_vers;
3979
3980 /* The hook function sets the name to NULL if this symbol
3981 should be skipped for some reason. */
3982 if (name == NULL)
3983 continue;
3984 }
3985
3986 /* Sanity check that all possibilities were handled. */
3987 if (sec == NULL)
3988 {
3989 bfd_set_error (bfd_error_bad_value);
3990 goto error_free_vers;
3991 }
3992
3993 if (bfd_is_und_section (sec)
3994 || bfd_is_com_section (sec))
3995 definition = FALSE;
3996 else
3997 definition = TRUE;
3998
3999 size_change_ok = FALSE;
66eb6687 4000 type_change_ok = bed->type_change_ok;
4ad4eba5
AM
4001 old_alignment = 0;
4002 old_bfd = NULL;
af44c138 4003 new_sec = sec;
4ad4eba5 4004
66eb6687 4005 if (is_elf_hash_table (htab))
4ad4eba5
AM
4006 {
4007 Elf_Internal_Versym iver;
4008 unsigned int vernum = 0;
4009 bfd_boolean skip;
4010
b918acf9
NC
4011 /* If this is a definition of a symbol which was previously
4012 referenced in a non-weak manner then make a note of the bfd
4013 that contained the reference. This is used if we need to
4014 refer to the source of the reference later on. */
4015 if (! bfd_is_und_section (sec))
4016 {
4017 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4018
4019 if (h != NULL
4020 && h->root.type == bfd_link_hash_undefined
4021 && h->root.u.undef.abfd)
4022 undef_bfd = h->root.u.undef.abfd;
4023 }
4024
fc0e6df6 4025 if (ever == NULL)
4ad4eba5 4026 {
fc0e6df6
PB
4027 if (info->default_imported_symver)
4028 /* Use the default symbol version created earlier. */
4029 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4030 else
4031 iver.vs_vers = 0;
4032 }
4033 else
4034 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4035
4036 vernum = iver.vs_vers & VERSYM_VERSION;
4037
4038 /* If this is a hidden symbol, or if it is not version
4039 1, we append the version name to the symbol name.
cc86ff91
EB
4040 However, we do not modify a non-hidden absolute symbol
4041 if it is not a function, because it might be the version
4042 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4043 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4044 || (vernum > 1
4045 && (!bfd_is_abs_section (sec)
4046 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4047 {
4048 const char *verstr;
4049 size_t namelen, verlen, newlen;
4050 char *newname, *p;
4051
4052 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4053 {
fc0e6df6
PB
4054 if (vernum > elf_tdata (abfd)->cverdefs)
4055 verstr = NULL;
4056 else if (vernum > 1)
4057 verstr =
4058 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4059 else
4060 verstr = "";
4ad4eba5 4061
fc0e6df6 4062 if (verstr == NULL)
4ad4eba5 4063 {
fc0e6df6
PB
4064 (*_bfd_error_handler)
4065 (_("%B: %s: invalid version %u (max %d)"),
4066 abfd, name, vernum,
4067 elf_tdata (abfd)->cverdefs);
4068 bfd_set_error (bfd_error_bad_value);
4069 goto error_free_vers;
4ad4eba5 4070 }
fc0e6df6
PB
4071 }
4072 else
4073 {
4074 /* We cannot simply test for the number of
4075 entries in the VERNEED section since the
4076 numbers for the needed versions do not start
4077 at 0. */
4078 Elf_Internal_Verneed *t;
4079
4080 verstr = NULL;
4081 for (t = elf_tdata (abfd)->verref;
4082 t != NULL;
4083 t = t->vn_nextref)
4ad4eba5 4084 {
fc0e6df6 4085 Elf_Internal_Vernaux *a;
4ad4eba5 4086
fc0e6df6
PB
4087 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4088 {
4089 if (a->vna_other == vernum)
4ad4eba5 4090 {
fc0e6df6
PB
4091 verstr = a->vna_nodename;
4092 break;
4ad4eba5 4093 }
4ad4eba5 4094 }
fc0e6df6
PB
4095 if (a != NULL)
4096 break;
4097 }
4098 if (verstr == NULL)
4099 {
4100 (*_bfd_error_handler)
4101 (_("%B: %s: invalid needed version %d"),
4102 abfd, name, vernum);
4103 bfd_set_error (bfd_error_bad_value);
4104 goto error_free_vers;
4ad4eba5 4105 }
4ad4eba5 4106 }
fc0e6df6
PB
4107
4108 namelen = strlen (name);
4109 verlen = strlen (verstr);
4110 newlen = namelen + verlen + 2;
4111 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4112 && isym->st_shndx != SHN_UNDEF)
4113 ++newlen;
4114
a50b1753 4115 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4116 if (newname == NULL)
4117 goto error_free_vers;
4118 memcpy (newname, name, namelen);
4119 p = newname + namelen;
4120 *p++ = ELF_VER_CHR;
4121 /* If this is a defined non-hidden version symbol,
4122 we add another @ to the name. This indicates the
4123 default version of the symbol. */
4124 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4125 && isym->st_shndx != SHN_UNDEF)
4126 *p++ = ELF_VER_CHR;
4127 memcpy (p, verstr, verlen + 1);
4128
4129 name = newname;
4ad4eba5
AM
4130 }
4131
b918acf9
NC
4132 /* If necessary, make a second attempt to locate the bfd
4133 containing an unresolved, non-weak reference to the
4134 current symbol. */
4135 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
3cbc5de0
NC
4136 {
4137 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4138
4139 if (h != NULL
b918acf9 4140 && h->root.type == bfd_link_hash_undefined
3cbc5de0
NC
4141 && h->root.u.undef.abfd)
4142 undef_bfd = h->root.u.undef.abfd;
4143 }
4144
af44c138
L
4145 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4146 &value, &old_alignment,
4ad4eba5
AM
4147 sym_hash, &skip, &override,
4148 &type_change_ok, &size_change_ok))
4149 goto error_free_vers;
4150
4151 if (skip)
4152 continue;
4153
4154 if (override)
4155 definition = FALSE;
4156
4157 h = *sym_hash;
4158 while (h->root.type == bfd_link_hash_indirect
4159 || h->root.type == bfd_link_hash_warning)
4160 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4161
4162 /* Remember the old alignment if this is a common symbol, so
4163 that we don't reduce the alignment later on. We can't
4164 check later, because _bfd_generic_link_add_one_symbol
4165 will set a default for the alignment which we want to
4166 override. We also remember the old bfd where the existing
4167 definition comes from. */
4168 switch (h->root.type)
4169 {
4170 default:
4171 break;
4172
4173 case bfd_link_hash_defined:
4174 case bfd_link_hash_defweak:
4175 old_bfd = h->root.u.def.section->owner;
4176 break;
4177
4178 case bfd_link_hash_common:
4179 old_bfd = h->root.u.c.p->section->owner;
4180 old_alignment = h->root.u.c.p->alignment_power;
4181 break;
4182 }
4183
4184 if (elf_tdata (abfd)->verdef != NULL
4185 && ! override
4186 && vernum > 1
4187 && definition)
4188 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4189 }
4190
4191 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4192 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4193 (struct bfd_link_hash_entry **) sym_hash)))
4194 goto error_free_vers;
4195
4196 h = *sym_hash;
4197 while (h->root.type == bfd_link_hash_indirect
4198 || h->root.type == bfd_link_hash_warning)
4199 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4200
4ad4eba5 4201 *sym_hash = h;
d64284fe
L
4202 if (is_elf_hash_table (htab))
4203 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4ad4eba5
AM
4204
4205 new_weakdef = FALSE;
4206 if (dynamic
4207 && definition
4208 && (flags & BSF_WEAK) != 0
fcb93ecf 4209 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4210 && is_elf_hash_table (htab)
f6e332e6 4211 && h->u.weakdef == NULL)
4ad4eba5
AM
4212 {
4213 /* Keep a list of all weak defined non function symbols from
4214 a dynamic object, using the weakdef field. Later in this
4215 function we will set the weakdef field to the correct
4216 value. We only put non-function symbols from dynamic
4217 objects on this list, because that happens to be the only
4218 time we need to know the normal symbol corresponding to a
4219 weak symbol, and the information is time consuming to
4220 figure out. If the weakdef field is not already NULL,
4221 then this symbol was already defined by some previous
4222 dynamic object, and we will be using that previous
4223 definition anyhow. */
4224
f6e332e6 4225 h->u.weakdef = weaks;
4ad4eba5
AM
4226 weaks = h;
4227 new_weakdef = TRUE;
4228 }
4229
4230 /* Set the alignment of a common symbol. */
a4d8e49b 4231 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4232 && h->root.type == bfd_link_hash_common)
4233 {
4234 unsigned int align;
4235
a4d8e49b 4236 if (common)
af44c138
L
4237 align = bfd_log2 (isym->st_value);
4238 else
4239 {
4240 /* The new symbol is a common symbol in a shared object.
4241 We need to get the alignment from the section. */
4242 align = new_sec->alignment_power;
4243 }
595213d4 4244 if (align > old_alignment)
4ad4eba5
AM
4245 h->root.u.c.p->alignment_power = align;
4246 else
4247 h->root.u.c.p->alignment_power = old_alignment;
4248 }
4249
66eb6687 4250 if (is_elf_hash_table (htab))
4ad4eba5 4251 {
4ad4eba5 4252 bfd_boolean dynsym;
4ad4eba5
AM
4253
4254 /* Check the alignment when a common symbol is involved. This
4255 can change when a common symbol is overridden by a normal
4256 definition or a common symbol is ignored due to the old
4257 normal definition. We need to make sure the maximum
4258 alignment is maintained. */
a4d8e49b 4259 if ((old_alignment || common)
4ad4eba5
AM
4260 && h->root.type != bfd_link_hash_common)
4261 {
4262 unsigned int common_align;
4263 unsigned int normal_align;
4264 unsigned int symbol_align;
4265 bfd *normal_bfd;
4266 bfd *common_bfd;
4267
4268 symbol_align = ffs (h->root.u.def.value) - 1;
4269 if (h->root.u.def.section->owner != NULL
4270 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4271 {
4272 normal_align = h->root.u.def.section->alignment_power;
4273 if (normal_align > symbol_align)
4274 normal_align = symbol_align;
4275 }
4276 else
4277 normal_align = symbol_align;
4278
4279 if (old_alignment)
4280 {
4281 common_align = old_alignment;
4282 common_bfd = old_bfd;
4283 normal_bfd = abfd;
4284 }
4285 else
4286 {
4287 common_align = bfd_log2 (isym->st_value);
4288 common_bfd = abfd;
4289 normal_bfd = old_bfd;
4290 }
4291
4292 if (normal_align < common_align)
d07676f8
NC
4293 {
4294 /* PR binutils/2735 */
4295 if (normal_bfd == NULL)
4296 (*_bfd_error_handler)
4297 (_("Warning: alignment %u of common symbol `%s' in %B"
4298 " is greater than the alignment (%u) of its section %A"),
4299 common_bfd, h->root.u.def.section,
4300 1 << common_align, name, 1 << normal_align);
4301 else
4302 (*_bfd_error_handler)
4303 (_("Warning: alignment %u of symbol `%s' in %B"
4304 " is smaller than %u in %B"),
4305 normal_bfd, common_bfd,
4306 1 << normal_align, name, 1 << common_align);
4307 }
4ad4eba5
AM
4308 }
4309
83ad0046
L
4310 /* Remember the symbol size if it isn't undefined. */
4311 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4ad4eba5
AM
4312 && (definition || h->size == 0))
4313 {
83ad0046
L
4314 if (h->size != 0
4315 && h->size != isym->st_size
4316 && ! size_change_ok)
4ad4eba5 4317 (*_bfd_error_handler)
d003868e
AM
4318 (_("Warning: size of symbol `%s' changed"
4319 " from %lu in %B to %lu in %B"),
4320 old_bfd, abfd,
4ad4eba5 4321 name, (unsigned long) h->size,
d003868e 4322 (unsigned long) isym->st_size);
4ad4eba5
AM
4323
4324 h->size = isym->st_size;
4325 }
4326
4327 /* If this is a common symbol, then we always want H->SIZE
4328 to be the size of the common symbol. The code just above
4329 won't fix the size if a common symbol becomes larger. We
4330 don't warn about a size change here, because that is
fcb93ecf
PB
4331 covered by --warn-common. Allow changed between different
4332 function types. */
4ad4eba5
AM
4333 if (h->root.type == bfd_link_hash_common)
4334 h->size = h->root.u.c.size;
4335
4336 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4337 && (definition || h->type == STT_NOTYPE))
4338 {
2955ec4c
L
4339 unsigned int type = ELF_ST_TYPE (isym->st_info);
4340
4341 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4342 symbol. */
4343 if (type == STT_GNU_IFUNC
4344 && (abfd->flags & DYNAMIC) != 0)
4345 type = STT_FUNC;
4ad4eba5 4346
2955ec4c
L
4347 if (h->type != type)
4348 {
4349 if (h->type != STT_NOTYPE && ! type_change_ok)
4350 (*_bfd_error_handler)
4351 (_("Warning: type of symbol `%s' changed"
4352 " from %d to %d in %B"),
4353 abfd, name, h->type, type);
4354
4355 h->type = type;
4356 }
4ad4eba5
AM
4357 }
4358
54ac0771
L
4359 /* Merge st_other field. */
4360 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4ad4eba5
AM
4361
4362 /* Set a flag in the hash table entry indicating the type of
4363 reference or definition we just found. Keep a count of
4364 the number of dynamic symbols we find. A dynamic symbol
4365 is one which is referenced or defined by both a regular
4366 object and a shared object. */
4ad4eba5
AM
4367 dynsym = FALSE;
4368 if (! dynamic)
4369 {
4370 if (! definition)
4371 {
f5385ebf 4372 h->ref_regular = 1;
4ad4eba5 4373 if (bind != STB_WEAK)
f5385ebf 4374 h->ref_regular_nonweak = 1;
4ad4eba5
AM
4375 }
4376 else
d8880531
L
4377 {
4378 h->def_regular = 1;
4379 if (h->def_dynamic)
4380 {
4381 h->def_dynamic = 0;
4382 h->ref_dynamic = 1;
4383 h->dynamic_def = 1;
4384 }
4385 }
4ad4eba5 4386 if (! info->executable
f5385ebf
AM
4387 || h->def_dynamic
4388 || h->ref_dynamic)
4ad4eba5
AM
4389 dynsym = TRUE;
4390 }
4391 else
4392 {
4393 if (! definition)
f5385ebf 4394 h->ref_dynamic = 1;
4ad4eba5 4395 else
f5385ebf
AM
4396 h->def_dynamic = 1;
4397 if (h->def_regular
4398 || h->ref_regular
f6e332e6 4399 || (h->u.weakdef != NULL
4ad4eba5 4400 && ! new_weakdef
f6e332e6 4401 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4402 dynsym = TRUE;
4403 }
4404
b2064611 4405 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
92b7c7b6
L
4406 {
4407 /* We don't want to make debug symbol dynamic. */
92b7c7b6
L
4408 dynsym = FALSE;
4409 }
4410
35fc36a8
RS
4411 if (definition)
4412 h->target_internal = isym->st_target_internal;
4413
4ad4eba5
AM
4414 /* Check to see if we need to add an indirect symbol for
4415 the default name. */
4416 if (definition || h->root.type == bfd_link_hash_common)
4417 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4418 &sec, &value, &dynsym,
4419 override))
4420 goto error_free_vers;
4421
4422 if (definition && !dynamic)
4423 {
4424 char *p = strchr (name, ELF_VER_CHR);
4425 if (p != NULL && p[1] != ELF_VER_CHR)
4426 {
4427 /* Queue non-default versions so that .symver x, x@FOO
4428 aliases can be checked. */
66eb6687 4429 if (!nondeflt_vers)
4ad4eba5 4430 {
66eb6687
AM
4431 amt = ((isymend - isym + 1)
4432 * sizeof (struct elf_link_hash_entry *));
a50b1753
NC
4433 nondeflt_vers =
4434 (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4435 if (!nondeflt_vers)
4436 goto error_free_vers;
4ad4eba5 4437 }
66eb6687 4438 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4439 }
4440 }
4441
4442 if (dynsym && h->dynindx == -1)
4443 {
c152c796 4444 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4445 goto error_free_vers;
f6e332e6 4446 if (h->u.weakdef != NULL
4ad4eba5 4447 && ! new_weakdef
f6e332e6 4448 && h->u.weakdef->dynindx == -1)
4ad4eba5 4449 {
66eb6687 4450 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4451 goto error_free_vers;
4452 }
4453 }
4454 else if (dynsym && h->dynindx != -1)
4455 /* If the symbol already has a dynamic index, but
4456 visibility says it should not be visible, turn it into
4457 a local symbol. */
4458 switch (ELF_ST_VISIBILITY (h->other))
4459 {
4460 case STV_INTERNAL:
4461 case STV_HIDDEN:
4462 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4463 dynsym = FALSE;
4464 break;
4465 }
4466
4467 if (!add_needed
4468 && definition
010e5ae2
AM
4469 && ((dynsym
4470 && h->ref_regular)
4471 || (h->ref_dynamic
4472 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4473 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4474 {
4475 int ret;
4476 const char *soname = elf_dt_name (abfd);
4477
4478 /* A symbol from a library loaded via DT_NEEDED of some
4479 other library is referenced by a regular object.
e56f61be 4480 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4481 --no-add-needed is used and the reference was not
4482 a weak one. */
4483 if (undef_bfd != NULL
4484 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4485 {
4486 (*_bfd_error_handler)
3cbc5de0 4487 (_("%B: undefined reference to symbol '%s'"),
b918acf9 4488 undef_bfd, name);
3cbc5de0
NC
4489 (*_bfd_error_handler)
4490 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
d003868e 4491 abfd, name);
3cbc5de0 4492 bfd_set_error (bfd_error_invalid_operation);
e56f61be
L
4493 goto error_free_vers;
4494 }
4495
a50b1753
NC
4496 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4497 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4498
4ad4eba5 4499 add_needed = TRUE;
7e9f0867 4500 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4501 if (ret < 0)
4502 goto error_free_vers;
4503
4504 BFD_ASSERT (ret == 0);
4505 }
4506 }
4507 }
4508
66eb6687
AM
4509 if (extversym != NULL)
4510 {
4511 free (extversym);
4512 extversym = NULL;
4513 }
4514
4515 if (isymbuf != NULL)
4516 {
4517 free (isymbuf);
4518 isymbuf = NULL;
4519 }
4520
4521 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4522 {
4523 unsigned int i;
4524
4525 /* Restore the symbol table. */
97fed1c9
JJ
4526 if (bed->as_needed_cleanup)
4527 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4528 old_hash = (char *) old_tab + tabsize;
4529 old_ent = (char *) old_hash + hashsize;
4530 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4531 htab->root.table.table = old_table;
4532 htab->root.table.size = old_size;
4533 htab->root.table.count = old_count;
66eb6687
AM
4534 memcpy (htab->root.table.table, old_tab, tabsize);
4535 memcpy (sym_hash, old_hash, hashsize);
4536 htab->root.undefs = old_undefs;
4537 htab->root.undefs_tail = old_undefs_tail;
4538 for (i = 0; i < htab->root.table.size; i++)
4539 {
4540 struct bfd_hash_entry *p;
4541 struct elf_link_hash_entry *h;
4542
4543 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4544 {
4545 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4546 if (h->root.type == bfd_link_hash_warning)
4547 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4548 if (h->dynindx >= old_dynsymcount)
4549 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4550
66eb6687
AM
4551 memcpy (p, old_ent, htab->root.table.entsize);
4552 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4553 h = (struct elf_link_hash_entry *) p;
4554 if (h->root.type == bfd_link_hash_warning)
4555 {
4556 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4557 old_ent = (char *) old_ent + htab->root.table.entsize;
4558 }
66eb6687
AM
4559 }
4560 }
4561
5061a885
AM
4562 /* Make a special call to the linker "notice" function to
4563 tell it that symbols added for crefs may need to be removed. */
4564 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4565 notice_not_needed, 0, NULL))
9af2a943 4566 goto error_free_vers;
5061a885 4567
66eb6687
AM
4568 free (old_tab);
4569 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4570 alloc_mark);
4571 if (nondeflt_vers != NULL)
4572 free (nondeflt_vers);
4573 return TRUE;
4574 }
2de92251 4575
66eb6687
AM
4576 if (old_tab != NULL)
4577 {
5061a885 4578 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
16d96b5b 4579 notice_needed, 0, NULL))
9af2a943 4580 goto error_free_vers;
66eb6687
AM
4581 free (old_tab);
4582 old_tab = NULL;
4583 }
4584
4ad4eba5
AM
4585 /* Now that all the symbols from this input file are created, handle
4586 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4587 if (nondeflt_vers != NULL)
4588 {
4589 bfd_size_type cnt, symidx;
4590
4591 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4592 {
4593 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4594 char *shortname, *p;
4595
4596 p = strchr (h->root.root.string, ELF_VER_CHR);
4597 if (p == NULL
4598 || (h->root.type != bfd_link_hash_defined
4599 && h->root.type != bfd_link_hash_defweak))
4600 continue;
4601
4602 amt = p - h->root.root.string;
a50b1753 4603 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4604 if (!shortname)
4605 goto error_free_vers;
4ad4eba5
AM
4606 memcpy (shortname, h->root.root.string, amt);
4607 shortname[amt] = '\0';
4608
4609 hi = (struct elf_link_hash_entry *)
66eb6687 4610 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4611 FALSE, FALSE, FALSE);
4612 if (hi != NULL
4613 && hi->root.type == h->root.type
4614 && hi->root.u.def.value == h->root.u.def.value
4615 && hi->root.u.def.section == h->root.u.def.section)
4616 {
4617 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4618 hi->root.type = bfd_link_hash_indirect;
4619 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4620 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4621 sym_hash = elf_sym_hashes (abfd);
4622 if (sym_hash)
4623 for (symidx = 0; symidx < extsymcount; ++symidx)
4624 if (sym_hash[symidx] == hi)
4625 {
4626 sym_hash[symidx] = h;
4627 break;
4628 }
4629 }
4630 free (shortname);
4631 }
4632 free (nondeflt_vers);
4633 nondeflt_vers = NULL;
4634 }
4635
4ad4eba5
AM
4636 /* Now set the weakdefs field correctly for all the weak defined
4637 symbols we found. The only way to do this is to search all the
4638 symbols. Since we only need the information for non functions in
4639 dynamic objects, that's the only time we actually put anything on
4640 the list WEAKS. We need this information so that if a regular
4641 object refers to a symbol defined weakly in a dynamic object, the
4642 real symbol in the dynamic object is also put in the dynamic
4643 symbols; we also must arrange for both symbols to point to the
4644 same memory location. We could handle the general case of symbol
4645 aliasing, but a general symbol alias can only be generated in
4646 assembler code, handling it correctly would be very time
4647 consuming, and other ELF linkers don't handle general aliasing
4648 either. */
4649 if (weaks != NULL)
4650 {
4651 struct elf_link_hash_entry **hpp;
4652 struct elf_link_hash_entry **hppend;
4653 struct elf_link_hash_entry **sorted_sym_hash;
4654 struct elf_link_hash_entry *h;
4655 size_t sym_count;
4656
4657 /* Since we have to search the whole symbol list for each weak
4658 defined symbol, search time for N weak defined symbols will be
4659 O(N^2). Binary search will cut it down to O(NlogN). */
4660 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4661 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4662 if (sorted_sym_hash == NULL)
4663 goto error_return;
4664 sym_hash = sorted_sym_hash;
4665 hpp = elf_sym_hashes (abfd);
4666 hppend = hpp + extsymcount;
4667 sym_count = 0;
4668 for (; hpp < hppend; hpp++)
4669 {
4670 h = *hpp;
4671 if (h != NULL
4672 && h->root.type == bfd_link_hash_defined
fcb93ecf 4673 && !bed->is_function_type (h->type))
4ad4eba5
AM
4674 {
4675 *sym_hash = h;
4676 sym_hash++;
4677 sym_count++;
4678 }
4679 }
4680
4681 qsort (sorted_sym_hash, sym_count,
4682 sizeof (struct elf_link_hash_entry *),
4683 elf_sort_symbol);
4684
4685 while (weaks != NULL)
4686 {
4687 struct elf_link_hash_entry *hlook;
4688 asection *slook;
4689 bfd_vma vlook;
4690 long ilook;
4691 size_t i, j, idx;
4692
4693 hlook = weaks;
f6e332e6
AM
4694 weaks = hlook->u.weakdef;
4695 hlook->u.weakdef = NULL;
4ad4eba5
AM
4696
4697 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4698 || hlook->root.type == bfd_link_hash_defweak
4699 || hlook->root.type == bfd_link_hash_common
4700 || hlook->root.type == bfd_link_hash_indirect);
4701 slook = hlook->root.u.def.section;
4702 vlook = hlook->root.u.def.value;
4703
4704 ilook = -1;
4705 i = 0;
4706 j = sym_count;
4707 while (i < j)
4708 {
4709 bfd_signed_vma vdiff;
4710 idx = (i + j) / 2;
4711 h = sorted_sym_hash [idx];
4712 vdiff = vlook - h->root.u.def.value;
4713 if (vdiff < 0)
4714 j = idx;
4715 else if (vdiff > 0)
4716 i = idx + 1;
4717 else
4718 {
a9b881be 4719 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4720 if (sdiff < 0)
4721 j = idx;
4722 else if (sdiff > 0)
4723 i = idx + 1;
4724 else
4725 {
4726 ilook = idx;
4727 break;
4728 }
4729 }
4730 }
4731
4732 /* We didn't find a value/section match. */
4733 if (ilook == -1)
4734 continue;
4735
4736 for (i = ilook; i < sym_count; i++)
4737 {
4738 h = sorted_sym_hash [i];
4739
4740 /* Stop if value or section doesn't match. */
4741 if (h->root.u.def.value != vlook
4742 || h->root.u.def.section != slook)
4743 break;
4744 else if (h != hlook)
4745 {
f6e332e6 4746 hlook->u.weakdef = h;
4ad4eba5
AM
4747
4748 /* If the weak definition is in the list of dynamic
4749 symbols, make sure the real definition is put
4750 there as well. */
4751 if (hlook->dynindx != -1 && h->dynindx == -1)
4752 {
c152c796 4753 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4754 {
4755 err_free_sym_hash:
4756 free (sorted_sym_hash);
4757 goto error_return;
4758 }
4ad4eba5
AM
4759 }
4760
4761 /* If the real definition is in the list of dynamic
4762 symbols, make sure the weak definition is put
4763 there as well. If we don't do this, then the
4764 dynamic loader might not merge the entries for the
4765 real definition and the weak definition. */
4766 if (h->dynindx != -1 && hlook->dynindx == -1)
4767 {
c152c796 4768 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4769 goto err_free_sym_hash;
4ad4eba5
AM
4770 }
4771 break;
4772 }
4773 }
4774 }
4775
4776 free (sorted_sym_hash);
4777 }
4778
33177bb1
AM
4779 if (bed->check_directives
4780 && !(*bed->check_directives) (abfd, info))
4781 return FALSE;
85fbca6a 4782
4ad4eba5
AM
4783 /* If this object is the same format as the output object, and it is
4784 not a shared library, then let the backend look through the
4785 relocs.
4786
4787 This is required to build global offset table entries and to
4788 arrange for dynamic relocs. It is not required for the
4789 particular common case of linking non PIC code, even when linking
4790 against shared libraries, but unfortunately there is no way of
4791 knowing whether an object file has been compiled PIC or not.
4792 Looking through the relocs is not particularly time consuming.
4793 The problem is that we must either (1) keep the relocs in memory,
4794 which causes the linker to require additional runtime memory or
4795 (2) read the relocs twice from the input file, which wastes time.
4796 This would be a good case for using mmap.
4797
4798 I have no idea how to handle linking PIC code into a file of a
4799 different format. It probably can't be done. */
4ad4eba5 4800 if (! dynamic
66eb6687 4801 && is_elf_hash_table (htab)
13285a1b 4802 && bed->check_relocs != NULL
39334f3a 4803 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4804 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4805 {
4806 asection *o;
4807
4808 for (o = abfd->sections; o != NULL; o = o->next)
4809 {
4810 Elf_Internal_Rela *internal_relocs;
4811 bfd_boolean ok;
4812
4813 if ((o->flags & SEC_RELOC) == 0
4814 || o->reloc_count == 0
4815 || ((info->strip == strip_all || info->strip == strip_debugger)
4816 && (o->flags & SEC_DEBUGGING) != 0)
4817 || bfd_is_abs_section (o->output_section))
4818 continue;
4819
4820 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4821 info->keep_memory);
4822 if (internal_relocs == NULL)
4823 goto error_return;
4824
66eb6687 4825 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4826
4827 if (elf_section_data (o)->relocs != internal_relocs)
4828 free (internal_relocs);
4829
4830 if (! ok)
4831 goto error_return;
4832 }
4833 }
4834
4835 /* If this is a non-traditional link, try to optimize the handling
4836 of the .stab/.stabstr sections. */
4837 if (! dynamic
4838 && ! info->traditional_format
66eb6687 4839 && is_elf_hash_table (htab)
4ad4eba5
AM
4840 && (info->strip != strip_all && info->strip != strip_debugger))
4841 {
4842 asection *stabstr;
4843
4844 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4845 if (stabstr != NULL)
4846 {
4847 bfd_size_type string_offset = 0;
4848 asection *stab;
4849
4850 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4851 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4852 && (!stab->name[5] ||
4853 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4854 && (stab->flags & SEC_MERGE) == 0
4855 && !bfd_is_abs_section (stab->output_section))
4856 {
4857 struct bfd_elf_section_data *secdata;
4858
4859 secdata = elf_section_data (stab);
66eb6687
AM
4860 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4861 stabstr, &secdata->sec_info,
4ad4eba5
AM
4862 &string_offset))
4863 goto error_return;
4864 if (secdata->sec_info)
4865 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4866 }
4867 }
4868 }
4869
66eb6687 4870 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4871 {
4872 /* Add this bfd to the loaded list. */
4873 struct elf_link_loaded_list *n;
4874
a50b1753
NC
4875 n = (struct elf_link_loaded_list *)
4876 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4ad4eba5
AM
4877 if (n == NULL)
4878 goto error_return;
4879 n->abfd = abfd;
66eb6687
AM
4880 n->next = htab->loaded;
4881 htab->loaded = n;
4ad4eba5
AM
4882 }
4883
4884 return TRUE;
4885
4886 error_free_vers:
66eb6687
AM
4887 if (old_tab != NULL)
4888 free (old_tab);
4ad4eba5
AM
4889 if (nondeflt_vers != NULL)
4890 free (nondeflt_vers);
4891 if (extversym != NULL)
4892 free (extversym);
4893 error_free_sym:
4894 if (isymbuf != NULL)
4895 free (isymbuf);
4896 error_return:
4897 return FALSE;
4898}
4899
8387904d
AM
4900/* Return the linker hash table entry of a symbol that might be
4901 satisfied by an archive symbol. Return -1 on error. */
4902
4903struct elf_link_hash_entry *
4904_bfd_elf_archive_symbol_lookup (bfd *abfd,
4905 struct bfd_link_info *info,
4906 const char *name)
4907{
4908 struct elf_link_hash_entry *h;
4909 char *p, *copy;
4910 size_t len, first;
4911
4912 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4913 if (h != NULL)
4914 return h;
4915
4916 /* If this is a default version (the name contains @@), look up the
4917 symbol again with only one `@' as well as without the version.
4918 The effect is that references to the symbol with and without the
4919 version will be matched by the default symbol in the archive. */
4920
4921 p = strchr (name, ELF_VER_CHR);
4922 if (p == NULL || p[1] != ELF_VER_CHR)
4923 return h;
4924
4925 /* First check with only one `@'. */
4926 len = strlen (name);
a50b1753 4927 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
4928 if (copy == NULL)
4929 return (struct elf_link_hash_entry *) 0 - 1;
4930
4931 first = p - name + 1;
4932 memcpy (copy, name, first);
4933 memcpy (copy + first, name + first + 1, len - first);
4934
4935 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4936 if (h == NULL)
4937 {
4938 /* We also need to check references to the symbol without the
4939 version. */
4940 copy[first - 1] = '\0';
4941 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4942 FALSE, FALSE, FALSE);
4943 }
4944
4945 bfd_release (abfd, copy);
4946 return h;
4947}
4948
0ad989f9
L
4949/* Add symbols from an ELF archive file to the linker hash table. We
4950 don't use _bfd_generic_link_add_archive_symbols because of a
4951 problem which arises on UnixWare. The UnixWare libc.so is an
4952 archive which includes an entry libc.so.1 which defines a bunch of
4953 symbols. The libc.so archive also includes a number of other
4954 object files, which also define symbols, some of which are the same
4955 as those defined in libc.so.1. Correct linking requires that we
4956 consider each object file in turn, and include it if it defines any
4957 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4958 this; it looks through the list of undefined symbols, and includes
4959 any object file which defines them. When this algorithm is used on
4960 UnixWare, it winds up pulling in libc.so.1 early and defining a
4961 bunch of symbols. This means that some of the other objects in the
4962 archive are not included in the link, which is incorrect since they
4963 precede libc.so.1 in the archive.
4964
4965 Fortunately, ELF archive handling is simpler than that done by
4966 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4967 oddities. In ELF, if we find a symbol in the archive map, and the
4968 symbol is currently undefined, we know that we must pull in that
4969 object file.
4970
4971 Unfortunately, we do have to make multiple passes over the symbol
4972 table until nothing further is resolved. */
4973
4ad4eba5
AM
4974static bfd_boolean
4975elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4976{
4977 symindex c;
4978 bfd_boolean *defined = NULL;
4979 bfd_boolean *included = NULL;
4980 carsym *symdefs;
4981 bfd_boolean loop;
4982 bfd_size_type amt;
8387904d
AM
4983 const struct elf_backend_data *bed;
4984 struct elf_link_hash_entry * (*archive_symbol_lookup)
4985 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4986
4987 if (! bfd_has_map (abfd))
4988 {
4989 /* An empty archive is a special case. */
4990 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4991 return TRUE;
4992 bfd_set_error (bfd_error_no_armap);
4993 return FALSE;
4994 }
4995
4996 /* Keep track of all symbols we know to be already defined, and all
4997 files we know to be already included. This is to speed up the
4998 second and subsequent passes. */
4999 c = bfd_ardata (abfd)->symdef_count;
5000 if (c == 0)
5001 return TRUE;
5002 amt = c;
5003 amt *= sizeof (bfd_boolean);
a50b1753
NC
5004 defined = (bfd_boolean *) bfd_zmalloc (amt);
5005 included = (bfd_boolean *) bfd_zmalloc (amt);
0ad989f9
L
5006 if (defined == NULL || included == NULL)
5007 goto error_return;
5008
5009 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5010 bed = get_elf_backend_data (abfd);
5011 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5012
5013 do
5014 {
5015 file_ptr last;
5016 symindex i;
5017 carsym *symdef;
5018 carsym *symdefend;
5019
5020 loop = FALSE;
5021 last = -1;
5022
5023 symdef = symdefs;
5024 symdefend = symdef + c;
5025 for (i = 0; symdef < symdefend; symdef++, i++)
5026 {
5027 struct elf_link_hash_entry *h;
5028 bfd *element;
5029 struct bfd_link_hash_entry *undefs_tail;
5030 symindex mark;
5031
5032 if (defined[i] || included[i])
5033 continue;
5034 if (symdef->file_offset == last)
5035 {
5036 included[i] = TRUE;
5037 continue;
5038 }
5039
8387904d
AM
5040 h = archive_symbol_lookup (abfd, info, symdef->name);
5041 if (h == (struct elf_link_hash_entry *) 0 - 1)
5042 goto error_return;
0ad989f9
L
5043
5044 if (h == NULL)
5045 continue;
5046
5047 if (h->root.type == bfd_link_hash_common)
5048 {
5049 /* We currently have a common symbol. The archive map contains
5050 a reference to this symbol, so we may want to include it. We
5051 only want to include it however, if this archive element
5052 contains a definition of the symbol, not just another common
5053 declaration of it.
5054
5055 Unfortunately some archivers (including GNU ar) will put
5056 declarations of common symbols into their archive maps, as
5057 well as real definitions, so we cannot just go by the archive
5058 map alone. Instead we must read in the element's symbol
5059 table and check that to see what kind of symbol definition
5060 this is. */
5061 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5062 continue;
5063 }
5064 else if (h->root.type != bfd_link_hash_undefined)
5065 {
5066 if (h->root.type != bfd_link_hash_undefweak)
5067 defined[i] = TRUE;
5068 continue;
5069 }
5070
5071 /* We need to include this archive member. */
5072 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5073 if (element == NULL)
5074 goto error_return;
5075
5076 if (! bfd_check_format (element, bfd_object))
5077 goto error_return;
5078
5079 /* Doublecheck that we have not included this object
5080 already--it should be impossible, but there may be
5081 something wrong with the archive. */
5082 if (element->archive_pass != 0)
5083 {
5084 bfd_set_error (bfd_error_bad_value);
5085 goto error_return;
5086 }
5087 element->archive_pass = 1;
5088
5089 undefs_tail = info->hash->undefs_tail;
5090
0e144ba7
AM
5091 if (!(*info->callbacks
5092 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5093 goto error_return;
0e144ba7 5094 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5095 goto error_return;
5096
5097 /* If there are any new undefined symbols, we need to make
5098 another pass through the archive in order to see whether
5099 they can be defined. FIXME: This isn't perfect, because
5100 common symbols wind up on undefs_tail and because an
5101 undefined symbol which is defined later on in this pass
5102 does not require another pass. This isn't a bug, but it
5103 does make the code less efficient than it could be. */
5104 if (undefs_tail != info->hash->undefs_tail)
5105 loop = TRUE;
5106
5107 /* Look backward to mark all symbols from this object file
5108 which we have already seen in this pass. */
5109 mark = i;
5110 do
5111 {
5112 included[mark] = TRUE;
5113 if (mark == 0)
5114 break;
5115 --mark;
5116 }
5117 while (symdefs[mark].file_offset == symdef->file_offset);
5118
5119 /* We mark subsequent symbols from this object file as we go
5120 on through the loop. */
5121 last = symdef->file_offset;
5122 }
5123 }
5124 while (loop);
5125
5126 free (defined);
5127 free (included);
5128
5129 return TRUE;
5130
5131 error_return:
5132 if (defined != NULL)
5133 free (defined);
5134 if (included != NULL)
5135 free (included);
5136 return FALSE;
5137}
4ad4eba5
AM
5138
5139/* Given an ELF BFD, add symbols to the global hash table as
5140 appropriate. */
5141
5142bfd_boolean
5143bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5144{
5145 switch (bfd_get_format (abfd))
5146 {
5147 case bfd_object:
5148 return elf_link_add_object_symbols (abfd, info);
5149 case bfd_archive:
5150 return elf_link_add_archive_symbols (abfd, info);
5151 default:
5152 bfd_set_error (bfd_error_wrong_format);
5153 return FALSE;
5154 }
5155}
5a580b3a 5156\f
14b1c01e
AM
5157struct hash_codes_info
5158{
5159 unsigned long *hashcodes;
5160 bfd_boolean error;
5161};
a0c8462f 5162
5a580b3a
AM
5163/* This function will be called though elf_link_hash_traverse to store
5164 all hash value of the exported symbols in an array. */
5165
5166static bfd_boolean
5167elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5168{
a50b1753 5169 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a
AM
5170 const char *name;
5171 char *p;
5172 unsigned long ha;
5173 char *alc = NULL;
5174
5175 if (h->root.type == bfd_link_hash_warning)
5176 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5177
5178 /* Ignore indirect symbols. These are added by the versioning code. */
5179 if (h->dynindx == -1)
5180 return TRUE;
5181
5182 name = h->root.root.string;
5183 p = strchr (name, ELF_VER_CHR);
5184 if (p != NULL)
5185 {
a50b1753 5186 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5187 if (alc == NULL)
5188 {
5189 inf->error = TRUE;
5190 return FALSE;
5191 }
5a580b3a
AM
5192 memcpy (alc, name, p - name);
5193 alc[p - name] = '\0';
5194 name = alc;
5195 }
5196
5197 /* Compute the hash value. */
5198 ha = bfd_elf_hash (name);
5199
5200 /* Store the found hash value in the array given as the argument. */
14b1c01e 5201 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5202
5203 /* And store it in the struct so that we can put it in the hash table
5204 later. */
f6e332e6 5205 h->u.elf_hash_value = ha;
5a580b3a
AM
5206
5207 if (alc != NULL)
5208 free (alc);
5209
5210 return TRUE;
5211}
5212
fdc90cb4
JJ
5213struct collect_gnu_hash_codes
5214{
5215 bfd *output_bfd;
5216 const struct elf_backend_data *bed;
5217 unsigned long int nsyms;
5218 unsigned long int maskbits;
5219 unsigned long int *hashcodes;
5220 unsigned long int *hashval;
5221 unsigned long int *indx;
5222 unsigned long int *counts;
5223 bfd_vma *bitmask;
5224 bfd_byte *contents;
5225 long int min_dynindx;
5226 unsigned long int bucketcount;
5227 unsigned long int symindx;
5228 long int local_indx;
5229 long int shift1, shift2;
5230 unsigned long int mask;
14b1c01e 5231 bfd_boolean error;
fdc90cb4
JJ
5232};
5233
5234/* This function will be called though elf_link_hash_traverse to store
5235 all hash value of the exported symbols in an array. */
5236
5237static bfd_boolean
5238elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5239{
a50b1753 5240 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5241 const char *name;
5242 char *p;
5243 unsigned long ha;
5244 char *alc = NULL;
5245
5246 if (h->root.type == bfd_link_hash_warning)
5247 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5248
5249 /* Ignore indirect symbols. These are added by the versioning code. */
5250 if (h->dynindx == -1)
5251 return TRUE;
5252
5253 /* Ignore also local symbols and undefined symbols. */
5254 if (! (*s->bed->elf_hash_symbol) (h))
5255 return TRUE;
5256
5257 name = h->root.root.string;
5258 p = strchr (name, ELF_VER_CHR);
5259 if (p != NULL)
5260 {
a50b1753 5261 alc = (char *) bfd_malloc (p - name + 1);
14b1c01e
AM
5262 if (alc == NULL)
5263 {
5264 s->error = TRUE;
5265 return FALSE;
5266 }
fdc90cb4
JJ
5267 memcpy (alc, name, p - name);
5268 alc[p - name] = '\0';
5269 name = alc;
5270 }
5271
5272 /* Compute the hash value. */
5273 ha = bfd_elf_gnu_hash (name);
5274
5275 /* Store the found hash value in the array for compute_bucket_count,
5276 and also for .dynsym reordering purposes. */
5277 s->hashcodes[s->nsyms] = ha;
5278 s->hashval[h->dynindx] = ha;
5279 ++s->nsyms;
5280 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5281 s->min_dynindx = h->dynindx;
5282
5283 if (alc != NULL)
5284 free (alc);
5285
5286 return TRUE;
5287}
5288
5289/* This function will be called though elf_link_hash_traverse to do
5290 final dynaminc symbol renumbering. */
5291
5292static bfd_boolean
5293elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5294{
a50b1753 5295 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5296 unsigned long int bucket;
5297 unsigned long int val;
5298
5299 if (h->root.type == bfd_link_hash_warning)
5300 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5301
5302 /* Ignore indirect symbols. */
5303 if (h->dynindx == -1)
5304 return TRUE;
5305
5306 /* Ignore also local symbols and undefined symbols. */
5307 if (! (*s->bed->elf_hash_symbol) (h))
5308 {
5309 if (h->dynindx >= s->min_dynindx)
5310 h->dynindx = s->local_indx++;
5311 return TRUE;
5312 }
5313
5314 bucket = s->hashval[h->dynindx] % s->bucketcount;
5315 val = (s->hashval[h->dynindx] >> s->shift1)
5316 & ((s->maskbits >> s->shift1) - 1);
5317 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5318 s->bitmask[val]
5319 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5320 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5321 if (s->counts[bucket] == 1)
5322 /* Last element terminates the chain. */
5323 val |= 1;
5324 bfd_put_32 (s->output_bfd, val,
5325 s->contents + (s->indx[bucket] - s->symindx) * 4);
5326 --s->counts[bucket];
5327 h->dynindx = s->indx[bucket]++;
5328 return TRUE;
5329}
5330
5331/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5332
5333bfd_boolean
5334_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5335{
5336 return !(h->forced_local
5337 || h->root.type == bfd_link_hash_undefined
5338 || h->root.type == bfd_link_hash_undefweak
5339 || ((h->root.type == bfd_link_hash_defined
5340 || h->root.type == bfd_link_hash_defweak)
5341 && h->root.u.def.section->output_section == NULL));
5342}
5343
5a580b3a
AM
5344/* Array used to determine the number of hash table buckets to use
5345 based on the number of symbols there are. If there are fewer than
5346 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5347 fewer than 37 we use 17 buckets, and so forth. We never use more
5348 than 32771 buckets. */
5349
5350static const size_t elf_buckets[] =
5351{
5352 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5353 16411, 32771, 0
5354};
5355
5356/* Compute bucket count for hashing table. We do not use a static set
5357 of possible tables sizes anymore. Instead we determine for all
5358 possible reasonable sizes of the table the outcome (i.e., the
5359 number of collisions etc) and choose the best solution. The
5360 weighting functions are not too simple to allow the table to grow
5361 without bounds. Instead one of the weighting factors is the size.
5362 Therefore the result is always a good payoff between few collisions
5363 (= short chain lengths) and table size. */
5364static size_t
b20dd2ce 5365compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5366 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5367 unsigned long int nsyms,
5368 int gnu_hash)
5a580b3a 5369{
5a580b3a 5370 size_t best_size = 0;
5a580b3a 5371 unsigned long int i;
5a580b3a 5372
5a580b3a
AM
5373 /* We have a problem here. The following code to optimize the table
5374 size requires an integer type with more the 32 bits. If
5375 BFD_HOST_U_64_BIT is set we know about such a type. */
5376#ifdef BFD_HOST_U_64_BIT
5377 if (info->optimize)
5378 {
5a580b3a
AM
5379 size_t minsize;
5380 size_t maxsize;
5381 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5382 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5383 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5384 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5385 unsigned long int *counts;
d40f3da9 5386 bfd_size_type amt;
0883b6e0 5387 unsigned int no_improvement_count = 0;
5a580b3a
AM
5388
5389 /* Possible optimization parameters: if we have NSYMS symbols we say
5390 that the hashing table must at least have NSYMS/4 and at most
5391 2*NSYMS buckets. */
5392 minsize = nsyms / 4;
5393 if (minsize == 0)
5394 minsize = 1;
5395 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5396 if (gnu_hash)
5397 {
5398 if (minsize < 2)
5399 minsize = 2;
5400 if ((best_size & 31) == 0)
5401 ++best_size;
5402 }
5a580b3a
AM
5403
5404 /* Create array where we count the collisions in. We must use bfd_malloc
5405 since the size could be large. */
5406 amt = maxsize;
5407 amt *= sizeof (unsigned long int);
a50b1753 5408 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5409 if (counts == NULL)
fdc90cb4 5410 return 0;
5a580b3a
AM
5411
5412 /* Compute the "optimal" size for the hash table. The criteria is a
5413 minimal chain length. The minor criteria is (of course) the size
5414 of the table. */
5415 for (i = minsize; i < maxsize; ++i)
5416 {
5417 /* Walk through the array of hashcodes and count the collisions. */
5418 BFD_HOST_U_64_BIT max;
5419 unsigned long int j;
5420 unsigned long int fact;
5421
fdc90cb4
JJ
5422 if (gnu_hash && (i & 31) == 0)
5423 continue;
5424
5a580b3a
AM
5425 memset (counts, '\0', i * sizeof (unsigned long int));
5426
5427 /* Determine how often each hash bucket is used. */
5428 for (j = 0; j < nsyms; ++j)
5429 ++counts[hashcodes[j] % i];
5430
5431 /* For the weight function we need some information about the
5432 pagesize on the target. This is information need not be 100%
5433 accurate. Since this information is not available (so far) we
5434 define it here to a reasonable default value. If it is crucial
5435 to have a better value some day simply define this value. */
5436# ifndef BFD_TARGET_PAGESIZE
5437# define BFD_TARGET_PAGESIZE (4096)
5438# endif
5439
fdc90cb4
JJ
5440 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5441 and the chains. */
5442 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5443
5444# if 1
5445 /* Variant 1: optimize for short chains. We add the squares
5446 of all the chain lengths (which favors many small chain
5447 over a few long chains). */
5448 for (j = 0; j < i; ++j)
5449 max += counts[j] * counts[j];
5450
5451 /* This adds penalties for the overall size of the table. */
fdc90cb4 5452 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5453 max *= fact * fact;
5454# else
5455 /* Variant 2: Optimize a lot more for small table. Here we
5456 also add squares of the size but we also add penalties for
5457 empty slots (the +1 term). */
5458 for (j = 0; j < i; ++j)
5459 max += (1 + counts[j]) * (1 + counts[j]);
5460
5461 /* The overall size of the table is considered, but not as
5462 strong as in variant 1, where it is squared. */
fdc90cb4 5463 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5464 max *= fact;
5465# endif
5466
5467 /* Compare with current best results. */
5468 if (max < best_chlen)
5469 {
5470 best_chlen = max;
5471 best_size = i;
0883b6e0 5472 no_improvement_count = 0;
5a580b3a 5473 }
0883b6e0
NC
5474 /* PR 11843: Avoid futile long searches for the best bucket size
5475 when there are a large number of symbols. */
5476 else if (++no_improvement_count == 100)
5477 break;
5a580b3a
AM
5478 }
5479
5480 free (counts);
5481 }
5482 else
5483#endif /* defined (BFD_HOST_U_64_BIT) */
5484 {
5485 /* This is the fallback solution if no 64bit type is available or if we
5486 are not supposed to spend much time on optimizations. We select the
5487 bucket count using a fixed set of numbers. */
5488 for (i = 0; elf_buckets[i] != 0; i++)
5489 {
5490 best_size = elf_buckets[i];
fdc90cb4 5491 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5492 break;
5493 }
fdc90cb4
JJ
5494 if (gnu_hash && best_size < 2)
5495 best_size = 2;
5a580b3a
AM
5496 }
5497
5a580b3a
AM
5498 return best_size;
5499}
5500
d0bf826b
AM
5501/* Size any SHT_GROUP section for ld -r. */
5502
5503bfd_boolean
5504_bfd_elf_size_group_sections (struct bfd_link_info *info)
5505{
5506 bfd *ibfd;
5507
5508 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5509 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5510 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5511 return FALSE;
5512 return TRUE;
5513}
5514
5a580b3a
AM
5515/* Set up the sizes and contents of the ELF dynamic sections. This is
5516 called by the ELF linker emulation before_allocation routine. We
5517 must set the sizes of the sections before the linker sets the
5518 addresses of the various sections. */
5519
5520bfd_boolean
5521bfd_elf_size_dynamic_sections (bfd *output_bfd,
5522 const char *soname,
5523 const char *rpath,
5524 const char *filter_shlib,
7ee314fa
AM
5525 const char *audit,
5526 const char *depaudit,
5a580b3a
AM
5527 const char * const *auxiliary_filters,
5528 struct bfd_link_info *info,
5529 asection **sinterpptr,
5530 struct bfd_elf_version_tree *verdefs)
5531{
5532 bfd_size_type soname_indx;
5533 bfd *dynobj;
5534 const struct elf_backend_data *bed;
28caa186 5535 struct elf_info_failed asvinfo;
5a580b3a
AM
5536
5537 *sinterpptr = NULL;
5538
5539 soname_indx = (bfd_size_type) -1;
5540
5541 if (!is_elf_hash_table (info->hash))
5542 return TRUE;
5543
6bfdb61b 5544 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5545 if (info->execstack)
5546 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5547 else if (info->noexecstack)
5548 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5549 else
5550 {
5551 bfd *inputobj;
5552 asection *notesec = NULL;
5553 int exec = 0;
5554
5555 for (inputobj = info->input_bfds;
5556 inputobj;
5557 inputobj = inputobj->link_next)
5558 {
5559 asection *s;
5560
a94b9d2d 5561 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5a580b3a
AM
5562 continue;
5563 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5564 if (s)
5565 {
5566 if (s->flags & SEC_CODE)
5567 exec = PF_X;
5568 notesec = s;
5569 }
6bfdb61b 5570 else if (bed->default_execstack)
5a580b3a
AM
5571 exec = PF_X;
5572 }
5573 if (notesec)
5574 {
5575 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5576 if (exec && info->relocatable
5577 && notesec->output_section != bfd_abs_section_ptr)
5578 notesec->output_section->flags |= SEC_CODE;
5579 }
5580 }
5581
5582 /* Any syms created from now on start with -1 in
5583 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5584 elf_hash_table (info)->init_got_refcount
5585 = elf_hash_table (info)->init_got_offset;
5586 elf_hash_table (info)->init_plt_refcount
5587 = elf_hash_table (info)->init_plt_offset;
5a580b3a 5588
d0bf826b
AM
5589 if (info->relocatable
5590 && !_bfd_elf_size_group_sections (info))
5591 return FALSE;
5592
5a580b3a
AM
5593 /* The backend may have to create some sections regardless of whether
5594 we're dynamic or not. */
5a580b3a
AM
5595 if (bed->elf_backend_always_size_sections
5596 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5597 return FALSE;
5598
eb3d5f3b
JB
5599 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5600 return FALSE;
5601
5a580b3a
AM
5602 dynobj = elf_hash_table (info)->dynobj;
5603
5604 /* If there were no dynamic objects in the link, there is nothing to
5605 do here. */
5606 if (dynobj == NULL)
5607 return TRUE;
5608
5a580b3a
AM
5609 if (elf_hash_table (info)->dynamic_sections_created)
5610 {
5611 struct elf_info_failed eif;
5612 struct elf_link_hash_entry *h;
5613 asection *dynstr;
5614 struct bfd_elf_version_tree *t;
5615 struct bfd_elf_version_expr *d;
046183de 5616 asection *s;
5a580b3a
AM
5617 bfd_boolean all_defined;
5618
5619 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5620 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5621
5622 if (soname != NULL)
5623 {
5624 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5625 soname, TRUE);
5626 if (soname_indx == (bfd_size_type) -1
5627 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5628 return FALSE;
5629 }
5630
5631 if (info->symbolic)
5632 {
5633 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5634 return FALSE;
5635 info->flags |= DF_SYMBOLIC;
5636 }
5637
5638 if (rpath != NULL)
5639 {
5640 bfd_size_type indx;
5641
5642 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5643 TRUE);
5644 if (indx == (bfd_size_type) -1
5645 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5646 return FALSE;
5647
5648 if (info->new_dtags)
5649 {
5650 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5651 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5652 return FALSE;
5653 }
5654 }
5655
5656 if (filter_shlib != NULL)
5657 {
5658 bfd_size_type indx;
5659
5660 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5661 filter_shlib, TRUE);
5662 if (indx == (bfd_size_type) -1
5663 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5664 return FALSE;
5665 }
5666
5667 if (auxiliary_filters != NULL)
5668 {
5669 const char * const *p;
5670
5671 for (p = auxiliary_filters; *p != NULL; p++)
5672 {
5673 bfd_size_type indx;
5674
5675 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5676 *p, TRUE);
5677 if (indx == (bfd_size_type) -1
5678 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5679 return FALSE;
5680 }
5681 }
5682
7ee314fa
AM
5683 if (audit != NULL)
5684 {
5685 bfd_size_type indx;
5686
5687 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5688 TRUE);
5689 if (indx == (bfd_size_type) -1
5690 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5691 return FALSE;
5692 }
5693
5694 if (depaudit != NULL)
5695 {
5696 bfd_size_type indx;
5697
5698 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5699 TRUE);
5700 if (indx == (bfd_size_type) -1
5701 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5702 return FALSE;
5703 }
5704
5a580b3a
AM
5705 eif.info = info;
5706 eif.verdefs = verdefs;
5707 eif.failed = FALSE;
5708
5709 /* If we are supposed to export all symbols into the dynamic symbol
5710 table (this is not the normal case), then do so. */
55255dae
L
5711 if (info->export_dynamic
5712 || (info->executable && info->dynamic))
5a580b3a
AM
5713 {
5714 elf_link_hash_traverse (elf_hash_table (info),
5715 _bfd_elf_export_symbol,
5716 &eif);
5717 if (eif.failed)
5718 return FALSE;
5719 }
5720
5721 /* Make all global versions with definition. */
5722 for (t = verdefs; t != NULL; t = t->next)
5723 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5724 if (!d->symver && d->literal)
5a580b3a
AM
5725 {
5726 const char *verstr, *name;
5727 size_t namelen, verlen, newlen;
93252b1c 5728 char *newname, *p, leading_char;
5a580b3a
AM
5729 struct elf_link_hash_entry *newh;
5730
93252b1c 5731 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5732 name = d->pattern;
93252b1c 5733 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5734 verstr = t->name;
5735 verlen = strlen (verstr);
5736 newlen = namelen + verlen + 3;
5737
a50b1753 5738 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5739 if (newname == NULL)
5740 return FALSE;
93252b1c
MF
5741 newname[0] = leading_char;
5742 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5743
5744 /* Check the hidden versioned definition. */
5745 p = newname + namelen;
5746 *p++ = ELF_VER_CHR;
5747 memcpy (p, verstr, verlen + 1);
5748 newh = elf_link_hash_lookup (elf_hash_table (info),
5749 newname, FALSE, FALSE,
5750 FALSE);
5751 if (newh == NULL
5752 || (newh->root.type != bfd_link_hash_defined
5753 && newh->root.type != bfd_link_hash_defweak))
5754 {
5755 /* Check the default versioned definition. */
5756 *p++ = ELF_VER_CHR;
5757 memcpy (p, verstr, verlen + 1);
5758 newh = elf_link_hash_lookup (elf_hash_table (info),
5759 newname, FALSE, FALSE,
5760 FALSE);
5761 }
5762 free (newname);
5763
5764 /* Mark this version if there is a definition and it is
5765 not defined in a shared object. */
5766 if (newh != NULL
f5385ebf 5767 && !newh->def_dynamic
5a580b3a
AM
5768 && (newh->root.type == bfd_link_hash_defined
5769 || newh->root.type == bfd_link_hash_defweak))
5770 d->symver = 1;
5771 }
5772
5773 /* Attach all the symbols to their version information. */
5a580b3a
AM
5774 asvinfo.info = info;
5775 asvinfo.verdefs = verdefs;
5776 asvinfo.failed = FALSE;
5777
5778 elf_link_hash_traverse (elf_hash_table (info),
5779 _bfd_elf_link_assign_sym_version,
5780 &asvinfo);
5781 if (asvinfo.failed)
5782 return FALSE;
5783
5784 if (!info->allow_undefined_version)
5785 {
5786 /* Check if all global versions have a definition. */
5787 all_defined = TRUE;
5788 for (t = verdefs; t != NULL; t = t->next)
5789 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5790 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5791 {
5792 (*_bfd_error_handler)
5793 (_("%s: undefined version: %s"),
5794 d->pattern, t->name);
5795 all_defined = FALSE;
5796 }
5797
5798 if (!all_defined)
5799 {
5800 bfd_set_error (bfd_error_bad_value);
5801 return FALSE;
5802 }
5803 }
5804
5805 /* Find all symbols which were defined in a dynamic object and make
5806 the backend pick a reasonable value for them. */
5807 elf_link_hash_traverse (elf_hash_table (info),
5808 _bfd_elf_adjust_dynamic_symbol,
5809 &eif);
5810 if (eif.failed)
5811 return FALSE;
5812
5813 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5814 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5815 now so that we know the final size of the .dynamic section. */
5816
5817 /* If there are initialization and/or finalization functions to
5818 call then add the corresponding DT_INIT/DT_FINI entries. */
5819 h = (info->init_function
5820 ? elf_link_hash_lookup (elf_hash_table (info),
5821 info->init_function, FALSE,
5822 FALSE, FALSE)
5823 : NULL);
5824 if (h != NULL
f5385ebf
AM
5825 && (h->ref_regular
5826 || h->def_regular))
5a580b3a
AM
5827 {
5828 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5829 return FALSE;
5830 }
5831 h = (info->fini_function
5832 ? elf_link_hash_lookup (elf_hash_table (info),
5833 info->fini_function, FALSE,
5834 FALSE, FALSE)
5835 : NULL);
5836 if (h != NULL
f5385ebf
AM
5837 && (h->ref_regular
5838 || h->def_regular))
5a580b3a
AM
5839 {
5840 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5841 return FALSE;
5842 }
5843
046183de
AM
5844 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5845 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5846 {
5847 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5848 if (! info->executable)
5849 {
5850 bfd *sub;
5851 asection *o;
5852
5853 for (sub = info->input_bfds; sub != NULL;
5854 sub = sub->link_next)
3fcd97f1
JJ
5855 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5856 for (o = sub->sections; o != NULL; o = o->next)
5857 if (elf_section_data (o)->this_hdr.sh_type
5858 == SHT_PREINIT_ARRAY)
5859 {
5860 (*_bfd_error_handler)
5861 (_("%B: .preinit_array section is not allowed in DSO"),
5862 sub);
5863 break;
5864 }
5a580b3a
AM
5865
5866 bfd_set_error (bfd_error_nonrepresentable_section);
5867 return FALSE;
5868 }
5869
5870 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5871 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5872 return FALSE;
5873 }
046183de
AM
5874 s = bfd_get_section_by_name (output_bfd, ".init_array");
5875 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5876 {
5877 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5878 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5879 return FALSE;
5880 }
046183de
AM
5881 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5882 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5883 {
5884 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5885 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5886 return FALSE;
5887 }
5888
5889 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5890 /* If .dynstr is excluded from the link, we don't want any of
5891 these tags. Strictly, we should be checking each section
5892 individually; This quick check covers for the case where
5893 someone does a /DISCARD/ : { *(*) }. */
5894 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5895 {
5896 bfd_size_type strsize;
5897
5898 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5899 if ((info->emit_hash
5900 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5901 || (info->emit_gnu_hash
5902 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5903 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5904 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5905 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5906 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5907 bed->s->sizeof_sym))
5908 return FALSE;
5909 }
5910 }
5911
5912 /* The backend must work out the sizes of all the other dynamic
5913 sections. */
5914 if (bed->elf_backend_size_dynamic_sections
5915 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5916 return FALSE;
5917
5918 if (elf_hash_table (info)->dynamic_sections_created)
5919 {
554220db 5920 unsigned long section_sym_count;
5a580b3a 5921 asection *s;
5a580b3a
AM
5922
5923 /* Set up the version definition section. */
5924 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5925 BFD_ASSERT (s != NULL);
5926
5927 /* We may have created additional version definitions if we are
5928 just linking a regular application. */
5929 verdefs = asvinfo.verdefs;
5930
5931 /* Skip anonymous version tag. */
5932 if (verdefs != NULL && verdefs->vernum == 0)
5933 verdefs = verdefs->next;
5934
3e3b46e5 5935 if (verdefs == NULL && !info->create_default_symver)
8423293d 5936 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5937 else
5938 {
5939 unsigned int cdefs;
5940 bfd_size_type size;
5941 struct bfd_elf_version_tree *t;
5942 bfd_byte *p;
5943 Elf_Internal_Verdef def;
5944 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5945 struct bfd_link_hash_entry *bh;
5946 struct elf_link_hash_entry *h;
5947 const char *name;
5a580b3a
AM
5948
5949 cdefs = 0;
5950 size = 0;
5951
5952 /* Make space for the base version. */
5953 size += sizeof (Elf_External_Verdef);
5954 size += sizeof (Elf_External_Verdaux);
5955 ++cdefs;
5956
3e3b46e5
PB
5957 /* Make space for the default version. */
5958 if (info->create_default_symver)
5959 {
5960 size += sizeof (Elf_External_Verdef);
5961 ++cdefs;
5962 }
5963
5a580b3a
AM
5964 for (t = verdefs; t != NULL; t = t->next)
5965 {
5966 struct bfd_elf_version_deps *n;
5967
a6cc6b3b
RO
5968 /* Don't emit base version twice. */
5969 if (t->vernum == 0)
5970 continue;
5971
5a580b3a
AM
5972 size += sizeof (Elf_External_Verdef);
5973 size += sizeof (Elf_External_Verdaux);
5974 ++cdefs;
5975
5976 for (n = t->deps; n != NULL; n = n->next)
5977 size += sizeof (Elf_External_Verdaux);
5978 }
5979
eea6121a 5980 s->size = size;
a50b1753 5981 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 5982 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5983 return FALSE;
5984
5985 /* Fill in the version definition section. */
5986
5987 p = s->contents;
5988
5989 def.vd_version = VER_DEF_CURRENT;
5990 def.vd_flags = VER_FLG_BASE;
5991 def.vd_ndx = 1;
5992 def.vd_cnt = 1;
3e3b46e5
PB
5993 if (info->create_default_symver)
5994 {
5995 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5996 def.vd_next = sizeof (Elf_External_Verdef);
5997 }
5998 else
5999 {
6000 def.vd_aux = sizeof (Elf_External_Verdef);
6001 def.vd_next = (sizeof (Elf_External_Verdef)
6002 + sizeof (Elf_External_Verdaux));
6003 }
5a580b3a
AM
6004
6005 if (soname_indx != (bfd_size_type) -1)
6006 {
6007 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6008 soname_indx);
6009 def.vd_hash = bfd_elf_hash (soname);
6010 defaux.vda_name = soname_indx;
3e3b46e5 6011 name = soname;
5a580b3a
AM
6012 }
6013 else
6014 {
5a580b3a
AM
6015 bfd_size_type indx;
6016
06084812 6017 name = lbasename (output_bfd->filename);
5a580b3a
AM
6018 def.vd_hash = bfd_elf_hash (name);
6019 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6020 name, FALSE);
6021 if (indx == (bfd_size_type) -1)
6022 return FALSE;
6023 defaux.vda_name = indx;
6024 }
6025 defaux.vda_next = 0;
6026
6027 _bfd_elf_swap_verdef_out (output_bfd, &def,
6028 (Elf_External_Verdef *) p);
6029 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6030 if (info->create_default_symver)
6031 {
6032 /* Add a symbol representing this version. */
6033 bh = NULL;
6034 if (! (_bfd_generic_link_add_one_symbol
6035 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6036 0, NULL, FALSE,
6037 get_elf_backend_data (dynobj)->collect, &bh)))
6038 return FALSE;
6039 h = (struct elf_link_hash_entry *) bh;
6040 h->non_elf = 0;
6041 h->def_regular = 1;
6042 h->type = STT_OBJECT;
6043 h->verinfo.vertree = NULL;
6044
6045 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6046 return FALSE;
6047
6048 /* Create a duplicate of the base version with the same
6049 aux block, but different flags. */
6050 def.vd_flags = 0;
6051 def.vd_ndx = 2;
6052 def.vd_aux = sizeof (Elf_External_Verdef);
6053 if (verdefs)
6054 def.vd_next = (sizeof (Elf_External_Verdef)
6055 + sizeof (Elf_External_Verdaux));
6056 else
6057 def.vd_next = 0;
6058 _bfd_elf_swap_verdef_out (output_bfd, &def,
6059 (Elf_External_Verdef *) p);
6060 p += sizeof (Elf_External_Verdef);
6061 }
5a580b3a
AM
6062 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6063 (Elf_External_Verdaux *) p);
6064 p += sizeof (Elf_External_Verdaux);
6065
6066 for (t = verdefs; t != NULL; t = t->next)
6067 {
6068 unsigned int cdeps;
6069 struct bfd_elf_version_deps *n;
5a580b3a 6070
a6cc6b3b
RO
6071 /* Don't emit the base version twice. */
6072 if (t->vernum == 0)
6073 continue;
6074
5a580b3a
AM
6075 cdeps = 0;
6076 for (n = t->deps; n != NULL; n = n->next)
6077 ++cdeps;
6078
6079 /* Add a symbol representing this version. */
6080 bh = NULL;
6081 if (! (_bfd_generic_link_add_one_symbol
6082 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6083 0, NULL, FALSE,
6084 get_elf_backend_data (dynobj)->collect, &bh)))
6085 return FALSE;
6086 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6087 h->non_elf = 0;
6088 h->def_regular = 1;
5a580b3a
AM
6089 h->type = STT_OBJECT;
6090 h->verinfo.vertree = t;
6091
c152c796 6092 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6093 return FALSE;
6094
6095 def.vd_version = VER_DEF_CURRENT;
6096 def.vd_flags = 0;
6097 if (t->globals.list == NULL
6098 && t->locals.list == NULL
6099 && ! t->used)
6100 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6101 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6102 def.vd_cnt = cdeps + 1;
6103 def.vd_hash = bfd_elf_hash (t->name);
6104 def.vd_aux = sizeof (Elf_External_Verdef);
6105 def.vd_next = 0;
a6cc6b3b
RO
6106
6107 /* If a basever node is next, it *must* be the last node in
6108 the chain, otherwise Verdef construction breaks. */
6109 if (t->next != NULL && t->next->vernum == 0)
6110 BFD_ASSERT (t->next->next == NULL);
6111
6112 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6113 def.vd_next = (sizeof (Elf_External_Verdef)
6114 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6115
6116 _bfd_elf_swap_verdef_out (output_bfd, &def,
6117 (Elf_External_Verdef *) p);
6118 p += sizeof (Elf_External_Verdef);
6119
6120 defaux.vda_name = h->dynstr_index;
6121 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6122 h->dynstr_index);
6123 defaux.vda_next = 0;
6124 if (t->deps != NULL)
6125 defaux.vda_next = sizeof (Elf_External_Verdaux);
6126 t->name_indx = defaux.vda_name;
6127
6128 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6129 (Elf_External_Verdaux *) p);
6130 p += sizeof (Elf_External_Verdaux);
6131
6132 for (n = t->deps; n != NULL; n = n->next)
6133 {
6134 if (n->version_needed == NULL)
6135 {
6136 /* This can happen if there was an error in the
6137 version script. */
6138 defaux.vda_name = 0;
6139 }
6140 else
6141 {
6142 defaux.vda_name = n->version_needed->name_indx;
6143 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6144 defaux.vda_name);
6145 }
6146 if (n->next == NULL)
6147 defaux.vda_next = 0;
6148 else
6149 defaux.vda_next = sizeof (Elf_External_Verdaux);
6150
6151 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6152 (Elf_External_Verdaux *) p);
6153 p += sizeof (Elf_External_Verdaux);
6154 }
6155 }
6156
6157 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6158 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6159 return FALSE;
6160
6161 elf_tdata (output_bfd)->cverdefs = cdefs;
6162 }
6163
6164 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6165 {
6166 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6167 return FALSE;
6168 }
6169 else if (info->flags & DF_BIND_NOW)
6170 {
6171 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6172 return FALSE;
6173 }
6174
6175 if (info->flags_1)
6176 {
6177 if (info->executable)
6178 info->flags_1 &= ~ (DF_1_INITFIRST
6179 | DF_1_NODELETE
6180 | DF_1_NOOPEN);
6181 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6182 return FALSE;
6183 }
6184
6185 /* Work out the size of the version reference section. */
6186
6187 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6188 BFD_ASSERT (s != NULL);
6189 {
6190 struct elf_find_verdep_info sinfo;
6191
5a580b3a
AM
6192 sinfo.info = info;
6193 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6194 if (sinfo.vers == 0)
6195 sinfo.vers = 1;
6196 sinfo.failed = FALSE;
6197
6198 elf_link_hash_traverse (elf_hash_table (info),
6199 _bfd_elf_link_find_version_dependencies,
6200 &sinfo);
14b1c01e
AM
6201 if (sinfo.failed)
6202 return FALSE;
5a580b3a
AM
6203
6204 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6205 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6206 else
6207 {
6208 Elf_Internal_Verneed *t;
6209 unsigned int size;
6210 unsigned int crefs;
6211 bfd_byte *p;
6212
a6cc6b3b 6213 /* Build the version dependency section. */
5a580b3a
AM
6214 size = 0;
6215 crefs = 0;
6216 for (t = elf_tdata (output_bfd)->verref;
6217 t != NULL;
6218 t = t->vn_nextref)
6219 {
6220 Elf_Internal_Vernaux *a;
6221
6222 size += sizeof (Elf_External_Verneed);
6223 ++crefs;
6224 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6225 size += sizeof (Elf_External_Vernaux);
6226 }
6227
eea6121a 6228 s->size = size;
a50b1753 6229 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6230 if (s->contents == NULL)
6231 return FALSE;
6232
6233 p = s->contents;
6234 for (t = elf_tdata (output_bfd)->verref;
6235 t != NULL;
6236 t = t->vn_nextref)
6237 {
6238 unsigned int caux;
6239 Elf_Internal_Vernaux *a;
6240 bfd_size_type indx;
6241
6242 caux = 0;
6243 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6244 ++caux;
6245
6246 t->vn_version = VER_NEED_CURRENT;
6247 t->vn_cnt = caux;
6248 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6249 elf_dt_name (t->vn_bfd) != NULL
6250 ? elf_dt_name (t->vn_bfd)
06084812 6251 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6252 FALSE);
6253 if (indx == (bfd_size_type) -1)
6254 return FALSE;
6255 t->vn_file = indx;
6256 t->vn_aux = sizeof (Elf_External_Verneed);
6257 if (t->vn_nextref == NULL)
6258 t->vn_next = 0;
6259 else
6260 t->vn_next = (sizeof (Elf_External_Verneed)
6261 + caux * sizeof (Elf_External_Vernaux));
6262
6263 _bfd_elf_swap_verneed_out (output_bfd, t,
6264 (Elf_External_Verneed *) p);
6265 p += sizeof (Elf_External_Verneed);
6266
6267 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6268 {
6269 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6270 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6271 a->vna_nodename, FALSE);
6272 if (indx == (bfd_size_type) -1)
6273 return FALSE;
6274 a->vna_name = indx;
6275 if (a->vna_nextptr == NULL)
6276 a->vna_next = 0;
6277 else
6278 a->vna_next = sizeof (Elf_External_Vernaux);
6279
6280 _bfd_elf_swap_vernaux_out (output_bfd, a,
6281 (Elf_External_Vernaux *) p);
6282 p += sizeof (Elf_External_Vernaux);
6283 }
6284 }
6285
6286 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6287 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6288 return FALSE;
6289
6290 elf_tdata (output_bfd)->cverrefs = crefs;
6291 }
6292 }
6293
8423293d
AM
6294 if ((elf_tdata (output_bfd)->cverrefs == 0
6295 && elf_tdata (output_bfd)->cverdefs == 0)
6296 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6297 &section_sym_count) == 0)
6298 {
6299 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6300 s->flags |= SEC_EXCLUDE;
6301 }
6302 }
6303 return TRUE;
6304}
6305
74541ad4
AM
6306/* Find the first non-excluded output section. We'll use its
6307 section symbol for some emitted relocs. */
6308void
6309_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6310{
6311 asection *s;
6312
6313 for (s = output_bfd->sections; s != NULL; s = s->next)
6314 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6315 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6316 {
6317 elf_hash_table (info)->text_index_section = s;
6318 break;
6319 }
6320}
6321
6322/* Find two non-excluded output sections, one for code, one for data.
6323 We'll use their section symbols for some emitted relocs. */
6324void
6325_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6326{
6327 asection *s;
6328
266b05cf
DJ
6329 /* Data first, since setting text_index_section changes
6330 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6331 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6332 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6333 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6334 {
266b05cf 6335 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6336 break;
6337 }
6338
6339 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6340 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6341 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6342 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6343 {
266b05cf 6344 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6345 break;
6346 }
6347
6348 if (elf_hash_table (info)->text_index_section == NULL)
6349 elf_hash_table (info)->text_index_section
6350 = elf_hash_table (info)->data_index_section;
6351}
6352
8423293d
AM
6353bfd_boolean
6354bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6355{
74541ad4
AM
6356 const struct elf_backend_data *bed;
6357
8423293d
AM
6358 if (!is_elf_hash_table (info->hash))
6359 return TRUE;
6360
74541ad4
AM
6361 bed = get_elf_backend_data (output_bfd);
6362 (*bed->elf_backend_init_index_section) (output_bfd, info);
6363
8423293d
AM
6364 if (elf_hash_table (info)->dynamic_sections_created)
6365 {
6366 bfd *dynobj;
8423293d
AM
6367 asection *s;
6368 bfd_size_type dynsymcount;
6369 unsigned long section_sym_count;
8423293d
AM
6370 unsigned int dtagcount;
6371
6372 dynobj = elf_hash_table (info)->dynobj;
6373
5a580b3a
AM
6374 /* Assign dynsym indicies. In a shared library we generate a
6375 section symbol for each output section, which come first.
6376 Next come all of the back-end allocated local dynamic syms,
6377 followed by the rest of the global symbols. */
6378
554220db
AM
6379 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6380 &section_sym_count);
5a580b3a
AM
6381
6382 /* Work out the size of the symbol version section. */
6383 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6384 BFD_ASSERT (s != NULL);
8423293d
AM
6385 if (dynsymcount != 0
6386 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6387 {
eea6121a 6388 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6389 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6390 if (s->contents == NULL)
6391 return FALSE;
6392
6393 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6394 return FALSE;
6395 }
6396
6397 /* Set the size of the .dynsym and .hash sections. We counted
6398 the number of dynamic symbols in elf_link_add_object_symbols.
6399 We will build the contents of .dynsym and .hash when we build
6400 the final symbol table, because until then we do not know the
6401 correct value to give the symbols. We built the .dynstr
6402 section as we went along in elf_link_add_object_symbols. */
6403 s = bfd_get_section_by_name (dynobj, ".dynsym");
6404 BFD_ASSERT (s != NULL);
eea6121a 6405 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6406
6407 if (dynsymcount != 0)
6408 {
a50b1753 6409 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6410 if (s->contents == NULL)
6411 return FALSE;
5a580b3a 6412
554220db
AM
6413 /* The first entry in .dynsym is a dummy symbol.
6414 Clear all the section syms, in case we don't output them all. */
6415 ++section_sym_count;
6416 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6417 }
6418
fdc90cb4
JJ
6419 elf_hash_table (info)->bucketcount = 0;
6420
5a580b3a
AM
6421 /* Compute the size of the hashing table. As a side effect this
6422 computes the hash values for all the names we export. */
fdc90cb4
JJ
6423 if (info->emit_hash)
6424 {
6425 unsigned long int *hashcodes;
14b1c01e 6426 struct hash_codes_info hashinf;
fdc90cb4
JJ
6427 bfd_size_type amt;
6428 unsigned long int nsyms;
6429 size_t bucketcount;
6430 size_t hash_entry_size;
6431
6432 /* Compute the hash values for all exported symbols. At the same
6433 time store the values in an array so that we could use them for
6434 optimizations. */
6435 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6436 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6437 if (hashcodes == NULL)
6438 return FALSE;
14b1c01e
AM
6439 hashinf.hashcodes = hashcodes;
6440 hashinf.error = FALSE;
5a580b3a 6441
fdc90cb4
JJ
6442 /* Put all hash values in HASHCODES. */
6443 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6444 elf_collect_hash_codes, &hashinf);
6445 if (hashinf.error)
4dd07732
AM
6446 {
6447 free (hashcodes);
6448 return FALSE;
6449 }
5a580b3a 6450
14b1c01e 6451 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6452 bucketcount
6453 = compute_bucket_count (info, hashcodes, nsyms, 0);
6454 free (hashcodes);
6455
6456 if (bucketcount == 0)
6457 return FALSE;
5a580b3a 6458
fdc90cb4
JJ
6459 elf_hash_table (info)->bucketcount = bucketcount;
6460
6461 s = bfd_get_section_by_name (dynobj, ".hash");
6462 BFD_ASSERT (s != NULL);
6463 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6464 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6465 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6466 if (s->contents == NULL)
6467 return FALSE;
6468
6469 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6470 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6471 s->contents + hash_entry_size);
6472 }
6473
6474 if (info->emit_gnu_hash)
6475 {
6476 size_t i, cnt;
6477 unsigned char *contents;
6478 struct collect_gnu_hash_codes cinfo;
6479 bfd_size_type amt;
6480 size_t bucketcount;
6481
6482 memset (&cinfo, 0, sizeof (cinfo));
6483
6484 /* Compute the hash values for all exported symbols. At the same
6485 time store the values in an array so that we could use them for
6486 optimizations. */
6487 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6488 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6489 if (cinfo.hashcodes == NULL)
6490 return FALSE;
6491
6492 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6493 cinfo.min_dynindx = -1;
6494 cinfo.output_bfd = output_bfd;
6495 cinfo.bed = bed;
6496
6497 /* Put all hash values in HASHCODES. */
6498 elf_link_hash_traverse (elf_hash_table (info),
6499 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6500 if (cinfo.error)
4dd07732
AM
6501 {
6502 free (cinfo.hashcodes);
6503 return FALSE;
6504 }
fdc90cb4
JJ
6505
6506 bucketcount
6507 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6508
6509 if (bucketcount == 0)
6510 {
6511 free (cinfo.hashcodes);
6512 return FALSE;
6513 }
6514
6515 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6516 BFD_ASSERT (s != NULL);
6517
6518 if (cinfo.nsyms == 0)
6519 {
6520 /* Empty .gnu.hash section is special. */
6521 BFD_ASSERT (cinfo.min_dynindx == -1);
6522 free (cinfo.hashcodes);
6523 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6524 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6525 if (contents == NULL)
6526 return FALSE;
6527 s->contents = contents;
6528 /* 1 empty bucket. */
6529 bfd_put_32 (output_bfd, 1, contents);
6530 /* SYMIDX above the special symbol 0. */
6531 bfd_put_32 (output_bfd, 1, contents + 4);
6532 /* Just one word for bitmask. */
6533 bfd_put_32 (output_bfd, 1, contents + 8);
6534 /* Only hash fn bloom filter. */
6535 bfd_put_32 (output_bfd, 0, contents + 12);
6536 /* No hashes are valid - empty bitmask. */
6537 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6538 /* No hashes in the only bucket. */
6539 bfd_put_32 (output_bfd, 0,
6540 contents + 16 + bed->s->arch_size / 8);
6541 }
6542 else
6543 {
9e6619e2 6544 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6545 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6546
9e6619e2
AM
6547 x = cinfo.nsyms;
6548 maskbitslog2 = 1;
6549 while ((x >>= 1) != 0)
6550 ++maskbitslog2;
fdc90cb4
JJ
6551 if (maskbitslog2 < 3)
6552 maskbitslog2 = 5;
6553 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6554 maskbitslog2 = maskbitslog2 + 3;
6555 else
6556 maskbitslog2 = maskbitslog2 + 2;
6557 if (bed->s->arch_size == 64)
6558 {
6559 if (maskbitslog2 == 5)
6560 maskbitslog2 = 6;
6561 cinfo.shift1 = 6;
6562 }
6563 else
6564 cinfo.shift1 = 5;
6565 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6566 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6567 cinfo.maskbits = 1 << maskbitslog2;
6568 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6569 amt = bucketcount * sizeof (unsigned long int) * 2;
6570 amt += maskwords * sizeof (bfd_vma);
a50b1753 6571 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6572 if (cinfo.bitmask == NULL)
6573 {
6574 free (cinfo.hashcodes);
6575 return FALSE;
6576 }
6577
a50b1753 6578 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6579 cinfo.indx = cinfo.counts + bucketcount;
6580 cinfo.symindx = dynsymcount - cinfo.nsyms;
6581 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6582
6583 /* Determine how often each hash bucket is used. */
6584 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6585 for (i = 0; i < cinfo.nsyms; ++i)
6586 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6587
6588 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6589 if (cinfo.counts[i] != 0)
6590 {
6591 cinfo.indx[i] = cnt;
6592 cnt += cinfo.counts[i];
6593 }
6594 BFD_ASSERT (cnt == dynsymcount);
6595 cinfo.bucketcount = bucketcount;
6596 cinfo.local_indx = cinfo.min_dynindx;
6597
6598 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6599 s->size += cinfo.maskbits / 8;
a50b1753 6600 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6601 if (contents == NULL)
6602 {
6603 free (cinfo.bitmask);
6604 free (cinfo.hashcodes);
6605 return FALSE;
6606 }
6607
6608 s->contents = contents;
6609 bfd_put_32 (output_bfd, bucketcount, contents);
6610 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6611 bfd_put_32 (output_bfd, maskwords, contents + 8);
6612 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6613 contents += 16 + cinfo.maskbits / 8;
6614
6615 for (i = 0; i < bucketcount; ++i)
6616 {
6617 if (cinfo.counts[i] == 0)
6618 bfd_put_32 (output_bfd, 0, contents);
6619 else
6620 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6621 contents += 4;
6622 }
6623
6624 cinfo.contents = contents;
6625
6626 /* Renumber dynamic symbols, populate .gnu.hash section. */
6627 elf_link_hash_traverse (elf_hash_table (info),
6628 elf_renumber_gnu_hash_syms, &cinfo);
6629
6630 contents = s->contents + 16;
6631 for (i = 0; i < maskwords; ++i)
6632 {
6633 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6634 contents);
6635 contents += bed->s->arch_size / 8;
6636 }
6637
6638 free (cinfo.bitmask);
6639 free (cinfo.hashcodes);
6640 }
6641 }
5a580b3a
AM
6642
6643 s = bfd_get_section_by_name (dynobj, ".dynstr");
6644 BFD_ASSERT (s != NULL);
6645
4ad4eba5 6646 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6647
eea6121a 6648 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6649
6650 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6651 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6652 return FALSE;
6653 }
6654
6655 return TRUE;
6656}
4d269e42
AM
6657\f
6658/* Indicate that we are only retrieving symbol values from this
6659 section. */
6660
6661void
6662_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6663{
6664 if (is_elf_hash_table (info->hash))
6665 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6666 _bfd_generic_link_just_syms (sec, info);
6667}
6668
6669/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6670
6671static void
6672merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6673 asection *sec)
6674{
6675 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6676 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6677}
6678
6679/* Finish SHF_MERGE section merging. */
6680
6681bfd_boolean
6682_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6683{
6684 bfd *ibfd;
6685 asection *sec;
6686
6687 if (!is_elf_hash_table (info->hash))
6688 return FALSE;
6689
6690 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6691 if ((ibfd->flags & DYNAMIC) == 0)
6692 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6693 if ((sec->flags & SEC_MERGE) != 0
6694 && !bfd_is_abs_section (sec->output_section))
6695 {
6696 struct bfd_elf_section_data *secdata;
6697
6698 secdata = elf_section_data (sec);
6699 if (! _bfd_add_merge_section (abfd,
6700 &elf_hash_table (info)->merge_info,
6701 sec, &secdata->sec_info))
6702 return FALSE;
6703 else if (secdata->sec_info)
6704 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6705 }
6706
6707 if (elf_hash_table (info)->merge_info != NULL)
6708 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6709 merge_sections_remove_hook);
6710 return TRUE;
6711}
6712
6713/* Create an entry in an ELF linker hash table. */
6714
6715struct bfd_hash_entry *
6716_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6717 struct bfd_hash_table *table,
6718 const char *string)
6719{
6720 /* Allocate the structure if it has not already been allocated by a
6721 subclass. */
6722 if (entry == NULL)
6723 {
a50b1753
NC
6724 entry = (struct bfd_hash_entry *)
6725 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6726 if (entry == NULL)
6727 return entry;
6728 }
6729
6730 /* Call the allocation method of the superclass. */
6731 entry = _bfd_link_hash_newfunc (entry, table, string);
6732 if (entry != NULL)
6733 {
6734 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6735 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6736
6737 /* Set local fields. */
6738 ret->indx = -1;
6739 ret->dynindx = -1;
6740 ret->got = htab->init_got_refcount;
6741 ret->plt = htab->init_plt_refcount;
6742 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6743 - offsetof (struct elf_link_hash_entry, size)));
6744 /* Assume that we have been called by a non-ELF symbol reader.
6745 This flag is then reset by the code which reads an ELF input
6746 file. This ensures that a symbol created by a non-ELF symbol
6747 reader will have the flag set correctly. */
6748 ret->non_elf = 1;
6749 }
6750
6751 return entry;
6752}
6753
6754/* Copy data from an indirect symbol to its direct symbol, hiding the
6755 old indirect symbol. Also used for copying flags to a weakdef. */
6756
6757void
6758_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6759 struct elf_link_hash_entry *dir,
6760 struct elf_link_hash_entry *ind)
6761{
6762 struct elf_link_hash_table *htab;
6763
6764 /* Copy down any references that we may have already seen to the
6765 symbol which just became indirect. */
6766
6767 dir->ref_dynamic |= ind->ref_dynamic;
6768 dir->ref_regular |= ind->ref_regular;
6769 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6770 dir->non_got_ref |= ind->non_got_ref;
6771 dir->needs_plt |= ind->needs_plt;
6772 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6773
6774 if (ind->root.type != bfd_link_hash_indirect)
6775 return;
6776
6777 /* Copy over the global and procedure linkage table refcount entries.
6778 These may have been already set up by a check_relocs routine. */
6779 htab = elf_hash_table (info);
6780 if (ind->got.refcount > htab->init_got_refcount.refcount)
6781 {
6782 if (dir->got.refcount < 0)
6783 dir->got.refcount = 0;
6784 dir->got.refcount += ind->got.refcount;
6785 ind->got.refcount = htab->init_got_refcount.refcount;
6786 }
6787
6788 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6789 {
6790 if (dir->plt.refcount < 0)
6791 dir->plt.refcount = 0;
6792 dir->plt.refcount += ind->plt.refcount;
6793 ind->plt.refcount = htab->init_plt_refcount.refcount;
6794 }
6795
6796 if (ind->dynindx != -1)
6797 {
6798 if (dir->dynindx != -1)
6799 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6800 dir->dynindx = ind->dynindx;
6801 dir->dynstr_index = ind->dynstr_index;
6802 ind->dynindx = -1;
6803 ind->dynstr_index = 0;
6804 }
6805}
6806
6807void
6808_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6809 struct elf_link_hash_entry *h,
6810 bfd_boolean force_local)
6811{
3aa14d16
L
6812 /* STT_GNU_IFUNC symbol must go through PLT. */
6813 if (h->type != STT_GNU_IFUNC)
6814 {
6815 h->plt = elf_hash_table (info)->init_plt_offset;
6816 h->needs_plt = 0;
6817 }
4d269e42
AM
6818 if (force_local)
6819 {
6820 h->forced_local = 1;
6821 if (h->dynindx != -1)
6822 {
6823 h->dynindx = -1;
6824 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6825 h->dynstr_index);
6826 }
6827 }
6828}
6829
6830/* Initialize an ELF linker hash table. */
6831
6832bfd_boolean
6833_bfd_elf_link_hash_table_init
6834 (struct elf_link_hash_table *table,
6835 bfd *abfd,
6836 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6837 struct bfd_hash_table *,
6838 const char *),
4dfe6ac6
NC
6839 unsigned int entsize,
6840 enum elf_target_id target_id)
4d269e42
AM
6841{
6842 bfd_boolean ret;
6843 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6844
6845 memset (table, 0, sizeof * table);
6846 table->init_got_refcount.refcount = can_refcount - 1;
6847 table->init_plt_refcount.refcount = can_refcount - 1;
6848 table->init_got_offset.offset = -(bfd_vma) 1;
6849 table->init_plt_offset.offset = -(bfd_vma) 1;
6850 /* The first dynamic symbol is a dummy. */
6851 table->dynsymcount = 1;
6852
6853 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 6854
4d269e42 6855 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 6856 table->hash_table_id = target_id;
4d269e42
AM
6857
6858 return ret;
6859}
6860
6861/* Create an ELF linker hash table. */
6862
6863struct bfd_link_hash_table *
6864_bfd_elf_link_hash_table_create (bfd *abfd)
6865{
6866 struct elf_link_hash_table *ret;
6867 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6868
a50b1753 6869 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
4d269e42
AM
6870 if (ret == NULL)
6871 return NULL;
6872
6873 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
6874 sizeof (struct elf_link_hash_entry),
6875 GENERIC_ELF_DATA))
4d269e42
AM
6876 {
6877 free (ret);
6878 return NULL;
6879 }
6880
6881 return &ret->root;
6882}
6883
6884/* This is a hook for the ELF emulation code in the generic linker to
6885 tell the backend linker what file name to use for the DT_NEEDED
6886 entry for a dynamic object. */
6887
6888void
6889bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6890{
6891 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6892 && bfd_get_format (abfd) == bfd_object)
6893 elf_dt_name (abfd) = name;
6894}
6895
6896int
6897bfd_elf_get_dyn_lib_class (bfd *abfd)
6898{
6899 int lib_class;
6900 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6901 && bfd_get_format (abfd) == bfd_object)
6902 lib_class = elf_dyn_lib_class (abfd);
6903 else
6904 lib_class = 0;
6905 return lib_class;
6906}
6907
6908void
6909bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6910{
6911 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6912 && bfd_get_format (abfd) == bfd_object)
6913 elf_dyn_lib_class (abfd) = lib_class;
6914}
6915
6916/* Get the list of DT_NEEDED entries for a link. This is a hook for
6917 the linker ELF emulation code. */
6918
6919struct bfd_link_needed_list *
6920bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6921 struct bfd_link_info *info)
6922{
6923 if (! is_elf_hash_table (info->hash))
6924 return NULL;
6925 return elf_hash_table (info)->needed;
6926}
6927
6928/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6929 hook for the linker ELF emulation code. */
6930
6931struct bfd_link_needed_list *
6932bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6933 struct bfd_link_info *info)
6934{
6935 if (! is_elf_hash_table (info->hash))
6936 return NULL;
6937 return elf_hash_table (info)->runpath;
6938}
6939
6940/* Get the name actually used for a dynamic object for a link. This
6941 is the SONAME entry if there is one. Otherwise, it is the string
6942 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6943
6944const char *
6945bfd_elf_get_dt_soname (bfd *abfd)
6946{
6947 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6948 && bfd_get_format (abfd) == bfd_object)
6949 return elf_dt_name (abfd);
6950 return NULL;
6951}
6952
6953/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6954 the ELF linker emulation code. */
6955
6956bfd_boolean
6957bfd_elf_get_bfd_needed_list (bfd *abfd,
6958 struct bfd_link_needed_list **pneeded)
6959{
6960 asection *s;
6961 bfd_byte *dynbuf = NULL;
cb33740c 6962 unsigned int elfsec;
4d269e42
AM
6963 unsigned long shlink;
6964 bfd_byte *extdyn, *extdynend;
6965 size_t extdynsize;
6966 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6967
6968 *pneeded = NULL;
6969
6970 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6971 || bfd_get_format (abfd) != bfd_object)
6972 return TRUE;
6973
6974 s = bfd_get_section_by_name (abfd, ".dynamic");
6975 if (s == NULL || s->size == 0)
6976 return TRUE;
6977
6978 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6979 goto error_return;
6980
6981 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 6982 if (elfsec == SHN_BAD)
4d269e42
AM
6983 goto error_return;
6984
6985 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6986
4d269e42
AM
6987 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6988 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6989
6990 extdyn = dynbuf;
6991 extdynend = extdyn + s->size;
6992 for (; extdyn < extdynend; extdyn += extdynsize)
6993 {
6994 Elf_Internal_Dyn dyn;
6995
6996 (*swap_dyn_in) (abfd, extdyn, &dyn);
6997
6998 if (dyn.d_tag == DT_NULL)
6999 break;
7000
7001 if (dyn.d_tag == DT_NEEDED)
7002 {
7003 const char *string;
7004 struct bfd_link_needed_list *l;
7005 unsigned int tagv = dyn.d_un.d_val;
7006 bfd_size_type amt;
7007
7008 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7009 if (string == NULL)
7010 goto error_return;
7011
7012 amt = sizeof *l;
a50b1753 7013 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7014 if (l == NULL)
7015 goto error_return;
7016
7017 l->by = abfd;
7018 l->name = string;
7019 l->next = *pneeded;
7020 *pneeded = l;
7021 }
7022 }
7023
7024 free (dynbuf);
7025
7026 return TRUE;
7027
7028 error_return:
7029 if (dynbuf != NULL)
7030 free (dynbuf);
7031 return FALSE;
7032}
7033
7034struct elf_symbuf_symbol
7035{
7036 unsigned long st_name; /* Symbol name, index in string tbl */
7037 unsigned char st_info; /* Type and binding attributes */
7038 unsigned char st_other; /* Visibilty, and target specific */
7039};
7040
7041struct elf_symbuf_head
7042{
7043 struct elf_symbuf_symbol *ssym;
7044 bfd_size_type count;
7045 unsigned int st_shndx;
7046};
7047
7048struct elf_symbol
7049{
7050 union
7051 {
7052 Elf_Internal_Sym *isym;
7053 struct elf_symbuf_symbol *ssym;
7054 } u;
7055 const char *name;
7056};
7057
7058/* Sort references to symbols by ascending section number. */
7059
7060static int
7061elf_sort_elf_symbol (const void *arg1, const void *arg2)
7062{
7063 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7064 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7065
7066 return s1->st_shndx - s2->st_shndx;
7067}
7068
7069static int
7070elf_sym_name_compare (const void *arg1, const void *arg2)
7071{
7072 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7073 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7074 return strcmp (s1->name, s2->name);
7075}
7076
7077static struct elf_symbuf_head *
7078elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7079{
14b1c01e 7080 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7081 struct elf_symbuf_symbol *ssym;
7082 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7083 bfd_size_type i, shndx_count, total_size;
4d269e42 7084
a50b1753 7085 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7086 if (indbuf == NULL)
7087 return NULL;
7088
7089 for (ind = indbuf, i = 0; i < symcount; i++)
7090 if (isymbuf[i].st_shndx != SHN_UNDEF)
7091 *ind++ = &isymbuf[i];
7092 indbufend = ind;
7093
7094 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7095 elf_sort_elf_symbol);
7096
7097 shndx_count = 0;
7098 if (indbufend > indbuf)
7099 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7100 if (ind[0]->st_shndx != ind[1]->st_shndx)
7101 shndx_count++;
7102
3ae181ee
L
7103 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7104 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7105 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7106 if (ssymbuf == NULL)
7107 {
7108 free (indbuf);
7109 return NULL;
7110 }
7111
3ae181ee 7112 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7113 ssymbuf->ssym = NULL;
7114 ssymbuf->count = shndx_count;
7115 ssymbuf->st_shndx = 0;
7116 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7117 {
7118 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7119 {
7120 ssymhead++;
7121 ssymhead->ssym = ssym;
7122 ssymhead->count = 0;
7123 ssymhead->st_shndx = (*ind)->st_shndx;
7124 }
7125 ssym->st_name = (*ind)->st_name;
7126 ssym->st_info = (*ind)->st_info;
7127 ssym->st_other = (*ind)->st_other;
7128 ssymhead->count++;
7129 }
3ae181ee
L
7130 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7131 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7132 == total_size));
4d269e42
AM
7133
7134 free (indbuf);
7135 return ssymbuf;
7136}
7137
7138/* Check if 2 sections define the same set of local and global
7139 symbols. */
7140
8f317e31 7141static bfd_boolean
4d269e42
AM
7142bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7143 struct bfd_link_info *info)
7144{
7145 bfd *bfd1, *bfd2;
7146 const struct elf_backend_data *bed1, *bed2;
7147 Elf_Internal_Shdr *hdr1, *hdr2;
7148 bfd_size_type symcount1, symcount2;
7149 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7150 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7151 Elf_Internal_Sym *isym, *isymend;
7152 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7153 bfd_size_type count1, count2, i;
cb33740c 7154 unsigned int shndx1, shndx2;
4d269e42
AM
7155 bfd_boolean result;
7156
7157 bfd1 = sec1->owner;
7158 bfd2 = sec2->owner;
7159
4d269e42
AM
7160 /* Both sections have to be in ELF. */
7161 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7162 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7163 return FALSE;
7164
7165 if (elf_section_type (sec1) != elf_section_type (sec2))
7166 return FALSE;
7167
4d269e42
AM
7168 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7169 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7170 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7171 return FALSE;
7172
7173 bed1 = get_elf_backend_data (bfd1);
7174 bed2 = get_elf_backend_data (bfd2);
7175 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7176 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7177 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7178 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7179
7180 if (symcount1 == 0 || symcount2 == 0)
7181 return FALSE;
7182
7183 result = FALSE;
7184 isymbuf1 = NULL;
7185 isymbuf2 = NULL;
a50b1753
NC
7186 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7187 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7188
7189 if (ssymbuf1 == NULL)
7190 {
7191 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7192 NULL, NULL, NULL);
7193 if (isymbuf1 == NULL)
7194 goto done;
7195
7196 if (!info->reduce_memory_overheads)
7197 elf_tdata (bfd1)->symbuf = ssymbuf1
7198 = elf_create_symbuf (symcount1, isymbuf1);
7199 }
7200
7201 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7202 {
7203 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7204 NULL, NULL, NULL);
7205 if (isymbuf2 == NULL)
7206 goto done;
7207
7208 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7209 elf_tdata (bfd2)->symbuf = ssymbuf2
7210 = elf_create_symbuf (symcount2, isymbuf2);
7211 }
7212
7213 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7214 {
7215 /* Optimized faster version. */
7216 bfd_size_type lo, hi, mid;
7217 struct elf_symbol *symp;
7218 struct elf_symbuf_symbol *ssym, *ssymend;
7219
7220 lo = 0;
7221 hi = ssymbuf1->count;
7222 ssymbuf1++;
7223 count1 = 0;
7224 while (lo < hi)
7225 {
7226 mid = (lo + hi) / 2;
cb33740c 7227 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7228 hi = mid;
cb33740c 7229 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7230 lo = mid + 1;
7231 else
7232 {
7233 count1 = ssymbuf1[mid].count;
7234 ssymbuf1 += mid;
7235 break;
7236 }
7237 }
7238
7239 lo = 0;
7240 hi = ssymbuf2->count;
7241 ssymbuf2++;
7242 count2 = 0;
7243 while (lo < hi)
7244 {
7245 mid = (lo + hi) / 2;
cb33740c 7246 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7247 hi = mid;
cb33740c 7248 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7249 lo = mid + 1;
7250 else
7251 {
7252 count2 = ssymbuf2[mid].count;
7253 ssymbuf2 += mid;
7254 break;
7255 }
7256 }
7257
7258 if (count1 == 0 || count2 == 0 || count1 != count2)
7259 goto done;
7260
a50b1753
NC
7261 symtable1 = (struct elf_symbol *)
7262 bfd_malloc (count1 * sizeof (struct elf_symbol));
7263 symtable2 = (struct elf_symbol *)
7264 bfd_malloc (count2 * sizeof (struct elf_symbol));
4d269e42
AM
7265 if (symtable1 == NULL || symtable2 == NULL)
7266 goto done;
7267
7268 symp = symtable1;
7269 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7270 ssym < ssymend; ssym++, symp++)
7271 {
7272 symp->u.ssym = ssym;
7273 symp->name = bfd_elf_string_from_elf_section (bfd1,
7274 hdr1->sh_link,
7275 ssym->st_name);
7276 }
7277
7278 symp = symtable2;
7279 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7280 ssym < ssymend; ssym++, symp++)
7281 {
7282 symp->u.ssym = ssym;
7283 symp->name = bfd_elf_string_from_elf_section (bfd2,
7284 hdr2->sh_link,
7285 ssym->st_name);
7286 }
7287
7288 /* Sort symbol by name. */
7289 qsort (symtable1, count1, sizeof (struct elf_symbol),
7290 elf_sym_name_compare);
7291 qsort (symtable2, count1, sizeof (struct elf_symbol),
7292 elf_sym_name_compare);
7293
7294 for (i = 0; i < count1; i++)
7295 /* Two symbols must have the same binding, type and name. */
7296 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7297 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7298 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7299 goto done;
7300
7301 result = TRUE;
7302 goto done;
7303 }
7304
a50b1753
NC
7305 symtable1 = (struct elf_symbol *)
7306 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7307 symtable2 = (struct elf_symbol *)
7308 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7309 if (symtable1 == NULL || symtable2 == NULL)
7310 goto done;
7311
7312 /* Count definitions in the section. */
7313 count1 = 0;
7314 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7315 if (isym->st_shndx == shndx1)
4d269e42
AM
7316 symtable1[count1++].u.isym = isym;
7317
7318 count2 = 0;
7319 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7320 if (isym->st_shndx == shndx2)
4d269e42
AM
7321 symtable2[count2++].u.isym = isym;
7322
7323 if (count1 == 0 || count2 == 0 || count1 != count2)
7324 goto done;
7325
7326 for (i = 0; i < count1; i++)
7327 symtable1[i].name
7328 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7329 symtable1[i].u.isym->st_name);
7330
7331 for (i = 0; i < count2; i++)
7332 symtable2[i].name
7333 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7334 symtable2[i].u.isym->st_name);
7335
7336 /* Sort symbol by name. */
7337 qsort (symtable1, count1, sizeof (struct elf_symbol),
7338 elf_sym_name_compare);
7339 qsort (symtable2, count1, sizeof (struct elf_symbol),
7340 elf_sym_name_compare);
7341
7342 for (i = 0; i < count1; i++)
7343 /* Two symbols must have the same binding, type and name. */
7344 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7345 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7346 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7347 goto done;
7348
7349 result = TRUE;
7350
7351done:
7352 if (symtable1)
7353 free (symtable1);
7354 if (symtable2)
7355 free (symtable2);
7356 if (isymbuf1)
7357 free (isymbuf1);
7358 if (isymbuf2)
7359 free (isymbuf2);
7360
7361 return result;
7362}
7363
7364/* Return TRUE if 2 section types are compatible. */
7365
7366bfd_boolean
7367_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7368 bfd *bbfd, const asection *bsec)
7369{
7370 if (asec == NULL
7371 || bsec == NULL
7372 || abfd->xvec->flavour != bfd_target_elf_flavour
7373 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7374 return TRUE;
7375
7376 return elf_section_type (asec) == elf_section_type (bsec);
7377}
7378\f
c152c796
AM
7379/* Final phase of ELF linker. */
7380
7381/* A structure we use to avoid passing large numbers of arguments. */
7382
7383struct elf_final_link_info
7384{
7385 /* General link information. */
7386 struct bfd_link_info *info;
7387 /* Output BFD. */
7388 bfd *output_bfd;
7389 /* Symbol string table. */
7390 struct bfd_strtab_hash *symstrtab;
7391 /* .dynsym section. */
7392 asection *dynsym_sec;
7393 /* .hash section. */
7394 asection *hash_sec;
7395 /* symbol version section (.gnu.version). */
7396 asection *symver_sec;
7397 /* Buffer large enough to hold contents of any section. */
7398 bfd_byte *contents;
7399 /* Buffer large enough to hold external relocs of any section. */
7400 void *external_relocs;
7401 /* Buffer large enough to hold internal relocs of any section. */
7402 Elf_Internal_Rela *internal_relocs;
7403 /* Buffer large enough to hold external local symbols of any input
7404 BFD. */
7405 bfd_byte *external_syms;
7406 /* And a buffer for symbol section indices. */
7407 Elf_External_Sym_Shndx *locsym_shndx;
7408 /* Buffer large enough to hold internal local symbols of any input
7409 BFD. */
7410 Elf_Internal_Sym *internal_syms;
7411 /* Array large enough to hold a symbol index for each local symbol
7412 of any input BFD. */
7413 long *indices;
7414 /* Array large enough to hold a section pointer for each local
7415 symbol of any input BFD. */
7416 asection **sections;
7417 /* Buffer to hold swapped out symbols. */
7418 bfd_byte *symbuf;
7419 /* And one for symbol section indices. */
7420 Elf_External_Sym_Shndx *symshndxbuf;
7421 /* Number of swapped out symbols in buffer. */
7422 size_t symbuf_count;
7423 /* Number of symbols which fit in symbuf. */
7424 size_t symbuf_size;
7425 /* And same for symshndxbuf. */
7426 size_t shndxbuf_size;
7427};
7428
7429/* This struct is used to pass information to elf_link_output_extsym. */
7430
7431struct elf_outext_info
7432{
7433 bfd_boolean failed;
7434 bfd_boolean localsyms;
7435 struct elf_final_link_info *finfo;
7436};
7437
d9352518
DB
7438
7439/* Support for evaluating a complex relocation.
7440
7441 Complex relocations are generalized, self-describing relocations. The
7442 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7443 relocations themselves.
d9352518
DB
7444
7445 The relocations are use a reserved elf-wide relocation type code (R_RELC
7446 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7447 information (start bit, end bit, word width, etc) into the addend. This
7448 information is extracted from CGEN-generated operand tables within gas.
7449
7450 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7451 internal) representing prefix-notation expressions, including but not
7452 limited to those sorts of expressions normally encoded as addends in the
7453 addend field. The symbol mangling format is:
7454
7455 <node> := <literal>
7456 | <unary-operator> ':' <node>
7457 | <binary-operator> ':' <node> ':' <node>
7458 ;
7459
7460 <literal> := 's' <digits=N> ':' <N character symbol name>
7461 | 'S' <digits=N> ':' <N character section name>
7462 | '#' <hexdigits>
7463 ;
7464
7465 <binary-operator> := as in C
7466 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7467
7468static void
a0c8462f
AM
7469set_symbol_value (bfd *bfd_with_globals,
7470 Elf_Internal_Sym *isymbuf,
7471 size_t locsymcount,
7472 size_t symidx,
7473 bfd_vma val)
d9352518 7474{
8977835c
AM
7475 struct elf_link_hash_entry **sym_hashes;
7476 struct elf_link_hash_entry *h;
7477 size_t extsymoff = locsymcount;
d9352518 7478
8977835c 7479 if (symidx < locsymcount)
d9352518 7480 {
8977835c
AM
7481 Elf_Internal_Sym *sym;
7482
7483 sym = isymbuf + symidx;
7484 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7485 {
7486 /* It is a local symbol: move it to the
7487 "absolute" section and give it a value. */
7488 sym->st_shndx = SHN_ABS;
7489 sym->st_value = val;
7490 return;
7491 }
7492 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7493 extsymoff = 0;
d9352518 7494 }
8977835c
AM
7495
7496 /* It is a global symbol: set its link type
7497 to "defined" and give it a value. */
7498
7499 sym_hashes = elf_sym_hashes (bfd_with_globals);
7500 h = sym_hashes [symidx - extsymoff];
7501 while (h->root.type == bfd_link_hash_indirect
7502 || h->root.type == bfd_link_hash_warning)
7503 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7504 h->root.type = bfd_link_hash_defined;
7505 h->root.u.def.value = val;
7506 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7507}
7508
a0c8462f
AM
7509static bfd_boolean
7510resolve_symbol (const char *name,
7511 bfd *input_bfd,
7512 struct elf_final_link_info *finfo,
7513 bfd_vma *result,
7514 Elf_Internal_Sym *isymbuf,
7515 size_t locsymcount)
d9352518 7516{
a0c8462f
AM
7517 Elf_Internal_Sym *sym;
7518 struct bfd_link_hash_entry *global_entry;
7519 const char *candidate = NULL;
7520 Elf_Internal_Shdr *symtab_hdr;
7521 size_t i;
7522
d9352518
DB
7523 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7524
7525 for (i = 0; i < locsymcount; ++ i)
7526 {
8977835c 7527 sym = isymbuf + i;
d9352518
DB
7528
7529 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7530 continue;
7531
7532 candidate = bfd_elf_string_from_elf_section (input_bfd,
7533 symtab_hdr->sh_link,
7534 sym->st_name);
7535#ifdef DEBUG
0f02bbd9
AM
7536 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7537 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7538#endif
7539 if (candidate && strcmp (candidate, name) == 0)
7540 {
0f02bbd9 7541 asection *sec = finfo->sections [i];
d9352518 7542
0f02bbd9
AM
7543 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7544 *result += sec->output_offset + sec->output_section->vma;
d9352518 7545#ifdef DEBUG
0f02bbd9
AM
7546 printf ("Found symbol with value %8.8lx\n",
7547 (unsigned long) *result);
d9352518
DB
7548#endif
7549 return TRUE;
7550 }
7551 }
7552
7553 /* Hmm, haven't found it yet. perhaps it is a global. */
a0c8462f
AM
7554 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7555 FALSE, FALSE, TRUE);
d9352518
DB
7556 if (!global_entry)
7557 return FALSE;
a0c8462f 7558
d9352518
DB
7559 if (global_entry->type == bfd_link_hash_defined
7560 || global_entry->type == bfd_link_hash_defweak)
7561 {
a0c8462f
AM
7562 *result = (global_entry->u.def.value
7563 + global_entry->u.def.section->output_section->vma
7564 + global_entry->u.def.section->output_offset);
d9352518 7565#ifdef DEBUG
0f02bbd9
AM
7566 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7567 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7568#endif
7569 return TRUE;
a0c8462f 7570 }
d9352518 7571
d9352518
DB
7572 return FALSE;
7573}
7574
7575static bfd_boolean
a0c8462f
AM
7576resolve_section (const char *name,
7577 asection *sections,
7578 bfd_vma *result)
d9352518 7579{
a0c8462f
AM
7580 asection *curr;
7581 unsigned int len;
d9352518 7582
a0c8462f 7583 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7584 if (strcmp (curr->name, name) == 0)
7585 {
7586 *result = curr->vma;
7587 return TRUE;
7588 }
7589
7590 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7591 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7592 {
7593 len = strlen (curr->name);
a0c8462f 7594 if (len > strlen (name))
d9352518
DB
7595 continue;
7596
7597 if (strncmp (curr->name, name, len) == 0)
7598 {
7599 if (strncmp (".end", name + len, 4) == 0)
7600 {
7601 *result = curr->vma + curr->size;
7602 return TRUE;
7603 }
7604
7605 /* Insert more pseudo-section names here, if you like. */
7606 }
7607 }
a0c8462f 7608
d9352518
DB
7609 return FALSE;
7610}
7611
7612static void
a0c8462f 7613undefined_reference (const char *reftype, const char *name)
d9352518 7614{
a0c8462f
AM
7615 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7616 reftype, name);
d9352518
DB
7617}
7618
7619static bfd_boolean
a0c8462f
AM
7620eval_symbol (bfd_vma *result,
7621 const char **symp,
7622 bfd *input_bfd,
7623 struct elf_final_link_info *finfo,
7624 bfd_vma dot,
7625 Elf_Internal_Sym *isymbuf,
7626 size_t locsymcount,
7627 int signed_p)
d9352518 7628{
4b93929b
NC
7629 size_t len;
7630 size_t symlen;
a0c8462f
AM
7631 bfd_vma a;
7632 bfd_vma b;
4b93929b 7633 char symbuf[4096];
0f02bbd9 7634 const char *sym = *symp;
a0c8462f
AM
7635 const char *symend;
7636 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7637
7638 len = strlen (sym);
7639 symend = sym + len;
7640
4b93929b 7641 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7642 {
7643 bfd_set_error (bfd_error_invalid_operation);
7644 return FALSE;
7645 }
a0c8462f 7646
d9352518
DB
7647 switch (* sym)
7648 {
7649 case '.':
0f02bbd9
AM
7650 *result = dot;
7651 *symp = sym + 1;
d9352518
DB
7652 return TRUE;
7653
7654 case '#':
0f02bbd9
AM
7655 ++sym;
7656 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7657 return TRUE;
7658
7659 case 'S':
7660 symbol_is_section = TRUE;
a0c8462f 7661 case 's':
0f02bbd9
AM
7662 ++sym;
7663 symlen = strtol (sym, (char **) symp, 10);
7664 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7665
4b93929b 7666 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7667 {
7668 bfd_set_error (bfd_error_invalid_operation);
7669 return FALSE;
7670 }
7671
7672 memcpy (symbuf, sym, symlen);
a0c8462f 7673 symbuf[symlen] = '\0';
0f02bbd9 7674 *symp = sym + symlen;
a0c8462f
AM
7675
7676 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7677 the symbol as a section, or vice-versa. so we're pretty liberal in our
7678 interpretation here; section means "try section first", not "must be a
7679 section", and likewise with symbol. */
7680
a0c8462f 7681 if (symbol_is_section)
d9352518 7682 {
8977835c
AM
7683 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7684 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7685 isymbuf, locsymcount))
d9352518
DB
7686 {
7687 undefined_reference ("section", symbuf);
7688 return FALSE;
7689 }
a0c8462f
AM
7690 }
7691 else
d9352518 7692 {
8977835c
AM
7693 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7694 isymbuf, locsymcount)
7695 && !resolve_section (symbuf, finfo->output_bfd->sections,
7696 result))
d9352518
DB
7697 {
7698 undefined_reference ("symbol", symbuf);
7699 return FALSE;
7700 }
7701 }
7702
7703 return TRUE;
a0c8462f 7704
d9352518
DB
7705 /* All that remains are operators. */
7706
7707#define UNARY_OP(op) \
7708 if (strncmp (sym, #op, strlen (#op)) == 0) \
7709 { \
7710 sym += strlen (#op); \
a0c8462f
AM
7711 if (*sym == ':') \
7712 ++sym; \
0f02bbd9
AM
7713 *symp = sym; \
7714 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7715 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7716 return FALSE; \
7717 if (signed_p) \
0f02bbd9 7718 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7719 else \
7720 *result = op a; \
d9352518
DB
7721 return TRUE; \
7722 }
7723
7724#define BINARY_OP(op) \
7725 if (strncmp (sym, #op, strlen (#op)) == 0) \
7726 { \
7727 sym += strlen (#op); \
a0c8462f
AM
7728 if (*sym == ':') \
7729 ++sym; \
0f02bbd9
AM
7730 *symp = sym; \
7731 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7732 isymbuf, locsymcount, signed_p)) \
a0c8462f 7733 return FALSE; \
0f02bbd9
AM
7734 ++*symp; \
7735 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7736 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7737 return FALSE; \
7738 if (signed_p) \
0f02bbd9 7739 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7740 else \
7741 *result = a op b; \
d9352518
DB
7742 return TRUE; \
7743 }
7744
7745 default:
7746 UNARY_OP (0-);
7747 BINARY_OP (<<);
7748 BINARY_OP (>>);
7749 BINARY_OP (==);
7750 BINARY_OP (!=);
7751 BINARY_OP (<=);
7752 BINARY_OP (>=);
7753 BINARY_OP (&&);
7754 BINARY_OP (||);
7755 UNARY_OP (~);
7756 UNARY_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 BINARY_OP (>);
7767#undef UNARY_OP
7768#undef BINARY_OP
7769 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7770 bfd_set_error (bfd_error_invalid_operation);
7771 return FALSE;
7772 }
7773}
7774
d9352518 7775static void
a0c8462f
AM
7776put_value (bfd_vma size,
7777 unsigned long chunksz,
7778 bfd *input_bfd,
7779 bfd_vma x,
7780 bfd_byte *location)
d9352518
DB
7781{
7782 location += (size - chunksz);
7783
a0c8462f 7784 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7785 {
7786 switch (chunksz)
7787 {
7788 default:
7789 case 0:
7790 abort ();
7791 case 1:
7792 bfd_put_8 (input_bfd, x, location);
7793 break;
7794 case 2:
7795 bfd_put_16 (input_bfd, x, location);
7796 break;
7797 case 4:
7798 bfd_put_32 (input_bfd, x, location);
7799 break;
7800 case 8:
7801#ifdef BFD64
7802 bfd_put_64 (input_bfd, x, location);
7803#else
7804 abort ();
7805#endif
7806 break;
7807 }
7808 }
7809}
7810
a0c8462f
AM
7811static bfd_vma
7812get_value (bfd_vma size,
7813 unsigned long chunksz,
7814 bfd *input_bfd,
7815 bfd_byte *location)
d9352518
DB
7816{
7817 bfd_vma x = 0;
7818
a0c8462f 7819 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7820 {
7821 switch (chunksz)
7822 {
7823 default:
7824 case 0:
7825 abort ();
7826 case 1:
7827 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7828 break;
7829 case 2:
7830 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7831 break;
7832 case 4:
7833 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7834 break;
7835 case 8:
7836#ifdef BFD64
7837 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7838#else
7839 abort ();
7840#endif
7841 break;
7842 }
7843 }
7844 return x;
7845}
7846
a0c8462f
AM
7847static void
7848decode_complex_addend (unsigned long *start, /* in bits */
7849 unsigned long *oplen, /* in bits */
7850 unsigned long *len, /* in bits */
7851 unsigned long *wordsz, /* in bytes */
7852 unsigned long *chunksz, /* in bytes */
7853 unsigned long *lsb0_p,
7854 unsigned long *signed_p,
7855 unsigned long *trunc_p,
7856 unsigned long encoded)
d9352518
DB
7857{
7858 * start = encoded & 0x3F;
7859 * len = (encoded >> 6) & 0x3F;
7860 * oplen = (encoded >> 12) & 0x3F;
7861 * wordsz = (encoded >> 18) & 0xF;
7862 * chunksz = (encoded >> 22) & 0xF;
7863 * lsb0_p = (encoded >> 27) & 1;
7864 * signed_p = (encoded >> 28) & 1;
7865 * trunc_p = (encoded >> 29) & 1;
7866}
7867
cdfeee4f 7868bfd_reloc_status_type
0f02bbd9 7869bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7870 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7871 bfd_byte *contents,
7872 Elf_Internal_Rela *rel,
7873 bfd_vma relocation)
d9352518 7874{
0f02bbd9
AM
7875 bfd_vma shift, x, mask;
7876 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7877 bfd_reloc_status_type r;
d9352518
DB
7878
7879 /* Perform this reloc, since it is complex.
7880 (this is not to say that it necessarily refers to a complex
7881 symbol; merely that it is a self-describing CGEN based reloc.
7882 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7883 word size, etc) encoded within it.). */
d9352518 7884
a0c8462f
AM
7885 decode_complex_addend (&start, &oplen, &len, &wordsz,
7886 &chunksz, &lsb0_p, &signed_p,
7887 &trunc_p, rel->r_addend);
d9352518
DB
7888
7889 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7890
7891 if (lsb0_p)
7892 shift = (start + 1) - len;
7893 else
7894 shift = (8 * wordsz) - (start + len);
7895
5dabe785 7896 /* FIXME: octets_per_byte. */
a0c8462f 7897 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7898
7899#ifdef DEBUG
7900 printf ("Doing complex reloc: "
7901 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7902 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7903 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7904 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
7905 oplen, (unsigned long) x, (unsigned long) mask,
7906 (unsigned long) relocation);
d9352518
DB
7907#endif
7908
cdfeee4f 7909 r = bfd_reloc_ok;
d9352518 7910 if (! trunc_p)
cdfeee4f
AM
7911 /* Now do an overflow check. */
7912 r = bfd_check_overflow ((signed_p
7913 ? complain_overflow_signed
7914 : complain_overflow_unsigned),
7915 len, 0, (8 * wordsz),
7916 relocation);
a0c8462f 7917
d9352518
DB
7918 /* Do the deed. */
7919 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7920
7921#ifdef DEBUG
7922 printf (" relocation: %8.8lx\n"
7923 " shifted mask: %8.8lx\n"
7924 " shifted/masked reloc: %8.8lx\n"
7925 " result: %8.8lx\n",
9ccb8af9
AM
7926 (unsigned long) relocation, (unsigned long) (mask << shift),
7927 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 7928#endif
5dabe785 7929 /* FIXME: octets_per_byte. */
d9352518 7930 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7931 return r;
d9352518
DB
7932}
7933
c152c796
AM
7934/* When performing a relocatable link, the input relocations are
7935 preserved. But, if they reference global symbols, the indices
d4730f92
BS
7936 referenced must be updated. Update all the relocations found in
7937 RELDATA. */
c152c796
AM
7938
7939static void
7940elf_link_adjust_relocs (bfd *abfd,
d4730f92 7941 struct bfd_elf_section_reloc_data *reldata)
c152c796
AM
7942{
7943 unsigned int i;
7944 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7945 bfd_byte *erela;
7946 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7947 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7948 bfd_vma r_type_mask;
7949 int r_sym_shift;
d4730f92
BS
7950 unsigned int count = reldata->count;
7951 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 7952
d4730f92 7953 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
7954 {
7955 swap_in = bed->s->swap_reloc_in;
7956 swap_out = bed->s->swap_reloc_out;
7957 }
d4730f92 7958 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
7959 {
7960 swap_in = bed->s->swap_reloca_in;
7961 swap_out = bed->s->swap_reloca_out;
7962 }
7963 else
7964 abort ();
7965
7966 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7967 abort ();
7968
7969 if (bed->s->arch_size == 32)
7970 {
7971 r_type_mask = 0xff;
7972 r_sym_shift = 8;
7973 }
7974 else
7975 {
7976 r_type_mask = 0xffffffff;
7977 r_sym_shift = 32;
7978 }
7979
d4730f92
BS
7980 erela = reldata->hdr->contents;
7981 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
7982 {
7983 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7984 unsigned int j;
7985
7986 if (*rel_hash == NULL)
7987 continue;
7988
7989 BFD_ASSERT ((*rel_hash)->indx >= 0);
7990
7991 (*swap_in) (abfd, erela, irela);
7992 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7993 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7994 | (irela[j].r_info & r_type_mask));
7995 (*swap_out) (abfd, irela, erela);
7996 }
7997}
7998
7999struct elf_link_sort_rela
8000{
8001 union {
8002 bfd_vma offset;
8003 bfd_vma sym_mask;
8004 } u;
8005 enum elf_reloc_type_class type;
8006 /* We use this as an array of size int_rels_per_ext_rel. */
8007 Elf_Internal_Rela rela[1];
8008};
8009
8010static int
8011elf_link_sort_cmp1 (const void *A, const void *B)
8012{
a50b1753
NC
8013 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8014 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8015 int relativea, relativeb;
8016
8017 relativea = a->type == reloc_class_relative;
8018 relativeb = b->type == reloc_class_relative;
8019
8020 if (relativea < relativeb)
8021 return 1;
8022 if (relativea > relativeb)
8023 return -1;
8024 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8025 return -1;
8026 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8027 return 1;
8028 if (a->rela->r_offset < b->rela->r_offset)
8029 return -1;
8030 if (a->rela->r_offset > b->rela->r_offset)
8031 return 1;
8032 return 0;
8033}
8034
8035static int
8036elf_link_sort_cmp2 (const void *A, const void *B)
8037{
a50b1753
NC
8038 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8039 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8040 int copya, copyb;
8041
8042 if (a->u.offset < b->u.offset)
8043 return -1;
8044 if (a->u.offset > b->u.offset)
8045 return 1;
8046 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8047 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8048 if (copya < copyb)
8049 return -1;
8050 if (copya > copyb)
8051 return 1;
8052 if (a->rela->r_offset < b->rela->r_offset)
8053 return -1;
8054 if (a->rela->r_offset > b->rela->r_offset)
8055 return 1;
8056 return 0;
8057}
8058
8059static size_t
8060elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8061{
3410fea8 8062 asection *dynamic_relocs;
fc66a176
L
8063 asection *rela_dyn;
8064 asection *rel_dyn;
c152c796
AM
8065 bfd_size_type count, size;
8066 size_t i, ret, sort_elt, ext_size;
8067 bfd_byte *sort, *s_non_relative, *p;
8068 struct elf_link_sort_rela *sq;
8069 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8070 int i2e = bed->s->int_rels_per_ext_rel;
8071 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8072 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8073 struct bfd_link_order *lo;
8074 bfd_vma r_sym_mask;
3410fea8 8075 bfd_boolean use_rela;
c152c796 8076
3410fea8
NC
8077 /* Find a dynamic reloc section. */
8078 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8079 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8080 if (rela_dyn != NULL && rela_dyn->size > 0
8081 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8082 {
3410fea8
NC
8083 bfd_boolean use_rela_initialised = FALSE;
8084
8085 /* This is just here to stop gcc from complaining.
8086 It's initialization checking code is not perfect. */
8087 use_rela = TRUE;
8088
8089 /* Both sections are present. Examine the sizes
8090 of the indirect sections to help us choose. */
8091 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8092 if (lo->type == bfd_indirect_link_order)
8093 {
8094 asection *o = lo->u.indirect.section;
8095
8096 if ((o->size % bed->s->sizeof_rela) == 0)
8097 {
8098 if ((o->size % bed->s->sizeof_rel) == 0)
8099 /* Section size is divisible by both rel and rela sizes.
8100 It is of no help to us. */
8101 ;
8102 else
8103 {
8104 /* Section size is only divisible by rela. */
8105 if (use_rela_initialised && (use_rela == FALSE))
8106 {
8107 _bfd_error_handler
8108 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8109 bfd_set_error (bfd_error_invalid_operation);
8110 return 0;
8111 }
8112 else
8113 {
8114 use_rela = TRUE;
8115 use_rela_initialised = TRUE;
8116 }
8117 }
8118 }
8119 else if ((o->size % bed->s->sizeof_rel) == 0)
8120 {
8121 /* Section size is only divisible by rel. */
8122 if (use_rela_initialised && (use_rela == TRUE))
8123 {
8124 _bfd_error_handler
8125 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8126 bfd_set_error (bfd_error_invalid_operation);
8127 return 0;
8128 }
8129 else
8130 {
8131 use_rela = FALSE;
8132 use_rela_initialised = TRUE;
8133 }
8134 }
8135 else
8136 {
8137 /* The section size is not divisible by either - something is wrong. */
8138 _bfd_error_handler
8139 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8140 bfd_set_error (bfd_error_invalid_operation);
8141 return 0;
8142 }
8143 }
8144
8145 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8146 if (lo->type == bfd_indirect_link_order)
8147 {
8148 asection *o = lo->u.indirect.section;
8149
8150 if ((o->size % bed->s->sizeof_rela) == 0)
8151 {
8152 if ((o->size % bed->s->sizeof_rel) == 0)
8153 /* Section size is divisible by both rel and rela sizes.
8154 It is of no help to us. */
8155 ;
8156 else
8157 {
8158 /* Section size is only divisible by rela. */
8159 if (use_rela_initialised && (use_rela == FALSE))
8160 {
8161 _bfd_error_handler
8162 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8163 bfd_set_error (bfd_error_invalid_operation);
8164 return 0;
8165 }
8166 else
8167 {
8168 use_rela = TRUE;
8169 use_rela_initialised = TRUE;
8170 }
8171 }
8172 }
8173 else if ((o->size % bed->s->sizeof_rel) == 0)
8174 {
8175 /* Section size is only divisible by rel. */
8176 if (use_rela_initialised && (use_rela == TRUE))
8177 {
8178 _bfd_error_handler
8179 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8180 bfd_set_error (bfd_error_invalid_operation);
8181 return 0;
8182 }
8183 else
8184 {
8185 use_rela = FALSE;
8186 use_rela_initialised = TRUE;
8187 }
8188 }
8189 else
8190 {
8191 /* The section size is not divisible by either - something is wrong. */
8192 _bfd_error_handler
8193 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8194 bfd_set_error (bfd_error_invalid_operation);
8195 return 0;
8196 }
8197 }
8198
8199 if (! use_rela_initialised)
8200 /* Make a guess. */
8201 use_rela = TRUE;
c152c796 8202 }
fc66a176
L
8203 else if (rela_dyn != NULL && rela_dyn->size > 0)
8204 use_rela = TRUE;
8205 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8206 use_rela = FALSE;
c152c796 8207 else
fc66a176 8208 return 0;
3410fea8
NC
8209
8210 if (use_rela)
c152c796 8211 {
3410fea8 8212 dynamic_relocs = rela_dyn;
c152c796
AM
8213 ext_size = bed->s->sizeof_rela;
8214 swap_in = bed->s->swap_reloca_in;
8215 swap_out = bed->s->swap_reloca_out;
8216 }
3410fea8
NC
8217 else
8218 {
8219 dynamic_relocs = rel_dyn;
8220 ext_size = bed->s->sizeof_rel;
8221 swap_in = bed->s->swap_reloc_in;
8222 swap_out = bed->s->swap_reloc_out;
8223 }
c152c796
AM
8224
8225 size = 0;
3410fea8 8226 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8227 if (lo->type == bfd_indirect_link_order)
3410fea8 8228 size += lo->u.indirect.section->size;
c152c796 8229
3410fea8 8230 if (size != dynamic_relocs->size)
c152c796
AM
8231 return 0;
8232
8233 sort_elt = (sizeof (struct elf_link_sort_rela)
8234 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8235
8236 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8237 if (count == 0)
8238 return 0;
a50b1753 8239 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8240
c152c796
AM
8241 if (sort == NULL)
8242 {
8243 (*info->callbacks->warning)
8244 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8245 return 0;
8246 }
8247
8248 if (bed->s->arch_size == 32)
8249 r_sym_mask = ~(bfd_vma) 0xff;
8250 else
8251 r_sym_mask = ~(bfd_vma) 0xffffffff;
8252
3410fea8 8253 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8254 if (lo->type == bfd_indirect_link_order)
8255 {
8256 bfd_byte *erel, *erelend;
8257 asection *o = lo->u.indirect.section;
8258
1da212d6
AM
8259 if (o->contents == NULL && o->size != 0)
8260 {
8261 /* This is a reloc section that is being handled as a normal
8262 section. See bfd_section_from_shdr. We can't combine
8263 relocs in this case. */
8264 free (sort);
8265 return 0;
8266 }
c152c796 8267 erel = o->contents;
eea6121a 8268 erelend = o->contents + o->size;
5dabe785 8269 /* FIXME: octets_per_byte. */
c152c796 8270 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8271
c152c796
AM
8272 while (erel < erelend)
8273 {
8274 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8275
c152c796
AM
8276 (*swap_in) (abfd, erel, s->rela);
8277 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8278 s->u.sym_mask = r_sym_mask;
8279 p += sort_elt;
8280 erel += ext_size;
8281 }
8282 }
8283
8284 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8285
8286 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8287 {
8288 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8289 if (s->type != reloc_class_relative)
8290 break;
8291 }
8292 ret = i;
8293 s_non_relative = p;
8294
8295 sq = (struct elf_link_sort_rela *) s_non_relative;
8296 for (; i < count; i++, p += sort_elt)
8297 {
8298 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8299 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8300 sq = sp;
8301 sp->u.offset = sq->rela->r_offset;
8302 }
8303
8304 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8305
3410fea8 8306 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8307 if (lo->type == bfd_indirect_link_order)
8308 {
8309 bfd_byte *erel, *erelend;
8310 asection *o = lo->u.indirect.section;
8311
8312 erel = o->contents;
eea6121a 8313 erelend = o->contents + o->size;
5dabe785 8314 /* FIXME: octets_per_byte. */
c152c796
AM
8315 p = sort + o->output_offset / ext_size * sort_elt;
8316 while (erel < erelend)
8317 {
8318 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8319 (*swap_out) (abfd, s->rela, erel);
8320 p += sort_elt;
8321 erel += ext_size;
8322 }
8323 }
8324
8325 free (sort);
3410fea8 8326 *psec = dynamic_relocs;
c152c796
AM
8327 return ret;
8328}
8329
8330/* Flush the output symbols to the file. */
8331
8332static bfd_boolean
8333elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8334 const struct elf_backend_data *bed)
8335{
8336 if (finfo->symbuf_count > 0)
8337 {
8338 Elf_Internal_Shdr *hdr;
8339 file_ptr pos;
8340 bfd_size_type amt;
8341
8342 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8343 pos = hdr->sh_offset + hdr->sh_size;
8344 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8345 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8346 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8347 return FALSE;
8348
8349 hdr->sh_size += amt;
8350 finfo->symbuf_count = 0;
8351 }
8352
8353 return TRUE;
8354}
8355
8356/* Add a symbol to the output symbol table. */
8357
6e0b88f1 8358static int
c152c796
AM
8359elf_link_output_sym (struct elf_final_link_info *finfo,
8360 const char *name,
8361 Elf_Internal_Sym *elfsym,
8362 asection *input_sec,
8363 struct elf_link_hash_entry *h)
8364{
8365 bfd_byte *dest;
8366 Elf_External_Sym_Shndx *destshndx;
6e0b88f1 8367 int (*output_symbol_hook)
c152c796
AM
8368 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8369 struct elf_link_hash_entry *);
8370 const struct elf_backend_data *bed;
8371
8372 bed = get_elf_backend_data (finfo->output_bfd);
8373 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8374 if (output_symbol_hook != NULL)
8375 {
6e0b88f1
AM
8376 int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h);
8377 if (ret != 1)
8378 return ret;
c152c796
AM
8379 }
8380
8381 if (name == NULL || *name == '\0')
8382 elfsym->st_name = 0;
8383 else if (input_sec->flags & SEC_EXCLUDE)
8384 elfsym->st_name = 0;
8385 else
8386 {
8387 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8388 name, TRUE, FALSE);
8389 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8390 return 0;
c152c796
AM
8391 }
8392
8393 if (finfo->symbuf_count >= finfo->symbuf_size)
8394 {
8395 if (! elf_link_flush_output_syms (finfo, bed))
6e0b88f1 8396 return 0;
c152c796
AM
8397 }
8398
8399 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8400 destshndx = finfo->symshndxbuf;
8401 if (destshndx != NULL)
8402 {
8403 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8404 {
8405 bfd_size_type amt;
8406
8407 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
a50b1753
NC
8408 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8409 amt * 2);
c152c796 8410 if (destshndx == NULL)
6e0b88f1 8411 return 0;
515ef31d 8412 finfo->symshndxbuf = destshndx;
c152c796
AM
8413 memset ((char *) destshndx + amt, 0, amt);
8414 finfo->shndxbuf_size *= 2;
8415 }
8416 destshndx += bfd_get_symcount (finfo->output_bfd);
8417 }
8418
8419 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8420 finfo->symbuf_count += 1;
8421 bfd_get_symcount (finfo->output_bfd) += 1;
8422
6e0b88f1 8423 return 1;
c152c796
AM
8424}
8425
c0d5a53d
L
8426/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8427
8428static bfd_boolean
8429check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8430{
4fbb74a6
AM
8431 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8432 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8433 {
8434 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8435 beyond 64k. */
c0d5a53d
L
8436 (*_bfd_error_handler)
8437 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8438 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8439 bfd_set_error (bfd_error_nonrepresentable_section);
8440 return FALSE;
8441 }
8442 return TRUE;
8443}
8444
c152c796
AM
8445/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8446 allowing an unsatisfied unversioned symbol in the DSO to match a
8447 versioned symbol that would normally require an explicit version.
8448 We also handle the case that a DSO references a hidden symbol
8449 which may be satisfied by a versioned symbol in another DSO. */
8450
8451static bfd_boolean
8452elf_link_check_versioned_symbol (struct bfd_link_info *info,
8453 const struct elf_backend_data *bed,
8454 struct elf_link_hash_entry *h)
8455{
8456 bfd *abfd;
8457 struct elf_link_loaded_list *loaded;
8458
8459 if (!is_elf_hash_table (info->hash))
8460 return FALSE;
8461
8462 switch (h->root.type)
8463 {
8464 default:
8465 abfd = NULL;
8466 break;
8467
8468 case bfd_link_hash_undefined:
8469 case bfd_link_hash_undefweak:
8470 abfd = h->root.u.undef.abfd;
8471 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8472 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8473 return FALSE;
8474 break;
8475
8476 case bfd_link_hash_defined:
8477 case bfd_link_hash_defweak:
8478 abfd = h->root.u.def.section->owner;
8479 break;
8480
8481 case bfd_link_hash_common:
8482 abfd = h->root.u.c.p->section->owner;
8483 break;
8484 }
8485 BFD_ASSERT (abfd != NULL);
8486
8487 for (loaded = elf_hash_table (info)->loaded;
8488 loaded != NULL;
8489 loaded = loaded->next)
8490 {
8491 bfd *input;
8492 Elf_Internal_Shdr *hdr;
8493 bfd_size_type symcount;
8494 bfd_size_type extsymcount;
8495 bfd_size_type extsymoff;
8496 Elf_Internal_Shdr *versymhdr;
8497 Elf_Internal_Sym *isym;
8498 Elf_Internal_Sym *isymend;
8499 Elf_Internal_Sym *isymbuf;
8500 Elf_External_Versym *ever;
8501 Elf_External_Versym *extversym;
8502
8503 input = loaded->abfd;
8504
8505 /* We check each DSO for a possible hidden versioned definition. */
8506 if (input == abfd
8507 || (input->flags & DYNAMIC) == 0
8508 || elf_dynversym (input) == 0)
8509 continue;
8510
8511 hdr = &elf_tdata (input)->dynsymtab_hdr;
8512
8513 symcount = hdr->sh_size / bed->s->sizeof_sym;
8514 if (elf_bad_symtab (input))
8515 {
8516 extsymcount = symcount;
8517 extsymoff = 0;
8518 }
8519 else
8520 {
8521 extsymcount = symcount - hdr->sh_info;
8522 extsymoff = hdr->sh_info;
8523 }
8524
8525 if (extsymcount == 0)
8526 continue;
8527
8528 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8529 NULL, NULL, NULL);
8530 if (isymbuf == NULL)
8531 return FALSE;
8532
8533 /* Read in any version definitions. */
8534 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8535 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8536 if (extversym == NULL)
8537 goto error_ret;
8538
8539 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8540 || (bfd_bread (extversym, versymhdr->sh_size, input)
8541 != versymhdr->sh_size))
8542 {
8543 free (extversym);
8544 error_ret:
8545 free (isymbuf);
8546 return FALSE;
8547 }
8548
8549 ever = extversym + extsymoff;
8550 isymend = isymbuf + extsymcount;
8551 for (isym = isymbuf; isym < isymend; isym++, ever++)
8552 {
8553 const char *name;
8554 Elf_Internal_Versym iver;
8555 unsigned short version_index;
8556
8557 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8558 || isym->st_shndx == SHN_UNDEF)
8559 continue;
8560
8561 name = bfd_elf_string_from_elf_section (input,
8562 hdr->sh_link,
8563 isym->st_name);
8564 if (strcmp (name, h->root.root.string) != 0)
8565 continue;
8566
8567 _bfd_elf_swap_versym_in (input, ever, &iver);
8568
d023c380
L
8569 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8570 && !(h->def_regular
8571 && h->forced_local))
c152c796
AM
8572 {
8573 /* If we have a non-hidden versioned sym, then it should
d023c380
L
8574 have provided a definition for the undefined sym unless
8575 it is defined in a non-shared object and forced local.
8576 */
c152c796
AM
8577 abort ();
8578 }
8579
8580 version_index = iver.vs_vers & VERSYM_VERSION;
8581 if (version_index == 1 || version_index == 2)
8582 {
8583 /* This is the base or first version. We can use it. */
8584 free (extversym);
8585 free (isymbuf);
8586 return TRUE;
8587 }
8588 }
8589
8590 free (extversym);
8591 free (isymbuf);
8592 }
8593
8594 return FALSE;
8595}
8596
8597/* Add an external symbol to the symbol table. This is called from
8598 the hash table traversal routine. When generating a shared object,
8599 we go through the symbol table twice. The first time we output
8600 anything that might have been forced to local scope in a version
8601 script. The second time we output the symbols that are still
8602 global symbols. */
8603
8604static bfd_boolean
8605elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8606{
a50b1753 8607 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
c152c796
AM
8608 struct elf_final_link_info *finfo = eoinfo->finfo;
8609 bfd_boolean strip;
8610 Elf_Internal_Sym sym;
8611 asection *input_sec;
8612 const struct elf_backend_data *bed;
6e0b88f1
AM
8613 long indx;
8614 int ret;
c152c796
AM
8615
8616 if (h->root.type == bfd_link_hash_warning)
8617 {
8618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8619 if (h->root.type == bfd_link_hash_new)
8620 return TRUE;
8621 }
8622
8623 /* Decide whether to output this symbol in this pass. */
8624 if (eoinfo->localsyms)
8625 {
f5385ebf 8626 if (!h->forced_local)
c152c796
AM
8627 return TRUE;
8628 }
8629 else
8630 {
f5385ebf 8631 if (h->forced_local)
c152c796
AM
8632 return TRUE;
8633 }
8634
8635 bed = get_elf_backend_data (finfo->output_bfd);
8636
12ac1cf5 8637 if (h->root.type == bfd_link_hash_undefined)
c152c796 8638 {
12ac1cf5
NC
8639 /* If we have an undefined symbol reference here then it must have
8640 come from a shared library that is being linked in. (Undefined
98da7939
L
8641 references in regular files have already been handled unless
8642 they are in unreferenced sections which are removed by garbage
8643 collection). */
12ac1cf5
NC
8644 bfd_boolean ignore_undef = FALSE;
8645
8646 /* Some symbols may be special in that the fact that they're
8647 undefined can be safely ignored - let backend determine that. */
8648 if (bed->elf_backend_ignore_undef_symbol)
8649 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8650
8651 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 8652 if (!ignore_undef
12ac1cf5 8653 && h->ref_dynamic
98da7939 8654 && (!h->ref_regular || finfo->info->gc_sections)
12ac1cf5
NC
8655 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8656 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
c152c796 8657 {
12ac1cf5 8658 if (! (finfo->info->callbacks->undefined_symbol
98da7939
L
8659 (finfo->info, h->root.root.string,
8660 h->ref_regular ? NULL : h->root.u.undef.abfd,
12ac1cf5
NC
8661 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8662 {
17d078c5 8663 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
8664 eoinfo->failed = TRUE;
8665 return FALSE;
8666 }
c152c796
AM
8667 }
8668 }
8669
8670 /* We should also warn if a forced local symbol is referenced from
8671 shared libraries. */
8672 if (! finfo->info->relocatable
8673 && (! finfo->info->shared)
f5385ebf
AM
8674 && h->forced_local
8675 && h->ref_dynamic
8676 && !h->dynamic_def
8677 && !h->dynamic_weak
c152c796
AM
8678 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8679 {
17d078c5
AM
8680 bfd *def_bfd;
8681 const char *msg;
8682
8683 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8684 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8685 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8686 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8687 else
8688 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8689 def_bfd = finfo->output_bfd;
8690 if (h->root.u.def.section != bfd_abs_section_ptr)
8691 def_bfd = h->root.u.def.section->owner;
8692 (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd,
8693 h->root.root.string);
8694 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8695 eoinfo->failed = TRUE;
8696 return FALSE;
8697 }
8698
8699 /* We don't want to output symbols that have never been mentioned by
8700 a regular file, or that we have been told to strip. However, if
8701 h->indx is set to -2, the symbol is used by a reloc and we must
8702 output it. */
8703 if (h->indx == -2)
8704 strip = FALSE;
f5385ebf 8705 else if ((h->def_dynamic
77cfaee6
AM
8706 || h->ref_dynamic
8707 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8708 && !h->def_regular
8709 && !h->ref_regular)
c152c796
AM
8710 strip = TRUE;
8711 else if (finfo->info->strip == strip_all)
8712 strip = TRUE;
8713 else if (finfo->info->strip == strip_some
8714 && bfd_hash_lookup (finfo->info->keep_hash,
8715 h->root.root.string, FALSE, FALSE) == NULL)
8716 strip = TRUE;
8717 else if (finfo->info->strip_discarded
8718 && (h->root.type == bfd_link_hash_defined
8719 || h->root.type == bfd_link_hash_defweak)
8720 && elf_discarded_section (h->root.u.def.section))
8721 strip = TRUE;
9e2278f5
AM
8722 else if ((h->root.type == bfd_link_hash_undefined
8723 || h->root.type == bfd_link_hash_undefweak)
8724 && h->root.u.undef.abfd != NULL
8725 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8726 strip = TRUE;
c152c796
AM
8727 else
8728 strip = FALSE;
8729
8730 /* If we're stripping it, and it's not a dynamic symbol, there's
57ca8ac7
L
8731 nothing else to do unless it is a forced local symbol or a
8732 STT_GNU_IFUNC symbol. */
c152c796
AM
8733 if (strip
8734 && h->dynindx == -1
57ca8ac7 8735 && h->type != STT_GNU_IFUNC
f5385ebf 8736 && !h->forced_local)
c152c796
AM
8737 return TRUE;
8738
8739 sym.st_value = 0;
8740 sym.st_size = h->size;
8741 sym.st_other = h->other;
f5385ebf 8742 if (h->forced_local)
935bd1e0
L
8743 {
8744 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8745 /* Turn off visibility on local symbol. */
8746 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8747 }
3e7a7d11
NC
8748 else if (h->unique_global)
8749 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
8750 else if (h->root.type == bfd_link_hash_undefweak
8751 || h->root.type == bfd_link_hash_defweak)
8752 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8753 else
8754 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 8755 sym.st_target_internal = h->target_internal;
c152c796
AM
8756
8757 switch (h->root.type)
8758 {
8759 default:
8760 case bfd_link_hash_new:
8761 case bfd_link_hash_warning:
8762 abort ();
8763 return FALSE;
8764
8765 case bfd_link_hash_undefined:
8766 case bfd_link_hash_undefweak:
8767 input_sec = bfd_und_section_ptr;
8768 sym.st_shndx = SHN_UNDEF;
8769 break;
8770
8771 case bfd_link_hash_defined:
8772 case bfd_link_hash_defweak:
8773 {
8774 input_sec = h->root.u.def.section;
8775 if (input_sec->output_section != NULL)
8776 {
8777 sym.st_shndx =
8778 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8779 input_sec->output_section);
8780 if (sym.st_shndx == SHN_BAD)
8781 {
8782 (*_bfd_error_handler)
d003868e
AM
8783 (_("%B: could not find output section %A for input section %A"),
8784 finfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 8785 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
8786 eoinfo->failed = TRUE;
8787 return FALSE;
8788 }
8789
8790 /* ELF symbols in relocatable files are section relative,
8791 but in nonrelocatable files they are virtual
8792 addresses. */
8793 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8794 if (! finfo->info->relocatable)
8795 {
8796 sym.st_value += input_sec->output_section->vma;
8797 if (h->type == STT_TLS)
8798 {
430a16a5
NC
8799 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8800 if (tls_sec != NULL)
8801 sym.st_value -= tls_sec->vma;
8802 else
8803 {
8804 /* The TLS section may have been garbage collected. */
8805 BFD_ASSERT (finfo->info->gc_sections
8806 && !input_sec->gc_mark);
8807 }
c152c796
AM
8808 }
8809 }
8810 }
8811 else
8812 {
8813 BFD_ASSERT (input_sec->owner == NULL
8814 || (input_sec->owner->flags & DYNAMIC) != 0);
8815 sym.st_shndx = SHN_UNDEF;
8816 input_sec = bfd_und_section_ptr;
8817 }
8818 }
8819 break;
8820
8821 case bfd_link_hash_common:
8822 input_sec = h->root.u.c.p->section;
a4d8e49b 8823 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8824 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8825 break;
8826
8827 case bfd_link_hash_indirect:
8828 /* These symbols are created by symbol versioning. They point
8829 to the decorated version of the name. For example, if the
8830 symbol foo@@GNU_1.2 is the default, which should be used when
8831 foo is used with no version, then we add an indirect symbol
8832 foo which points to foo@@GNU_1.2. We ignore these symbols,
8833 since the indirected symbol is already in the hash table. */
8834 return TRUE;
8835 }
8836
8837 /* Give the processor backend a chance to tweak the symbol value,
8838 and also to finish up anything that needs to be done for this
8839 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 8840 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 8841 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 8842 if ((h->type == STT_GNU_IFUNC
5f35ea9c 8843 && h->def_regular
3aa14d16
L
8844 && !finfo->info->relocatable)
8845 || ((h->dynindx != -1
8846 || h->forced_local)
8847 && ((finfo->info->shared
8848 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8849 || h->root.type != bfd_link_hash_undefweak))
8850 || !h->forced_local)
8851 && elf_hash_table (finfo->info)->dynamic_sections_created))
c152c796
AM
8852 {
8853 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8854 (finfo->output_bfd, finfo->info, h, &sym)))
8855 {
8856 eoinfo->failed = TRUE;
8857 return FALSE;
8858 }
8859 }
8860
8861 /* If we are marking the symbol as undefined, and there are no
8862 non-weak references to this symbol from a regular object, then
8863 mark the symbol as weak undefined; if there are non-weak
8864 references, mark the symbol as strong. We can't do this earlier,
8865 because it might not be marked as undefined until the
8866 finish_dynamic_symbol routine gets through with it. */
8867 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8868 && h->ref_regular
c152c796
AM
8869 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8870 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8871 {
8872 int bindtype;
2955ec4c
L
8873 unsigned int type = ELF_ST_TYPE (sym.st_info);
8874
8875 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8876 if (type == STT_GNU_IFUNC)
8877 type = STT_FUNC;
c152c796 8878
f5385ebf 8879 if (h->ref_regular_nonweak)
c152c796
AM
8880 bindtype = STB_GLOBAL;
8881 else
8882 bindtype = STB_WEAK;
2955ec4c 8883 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
8884 }
8885
bda987c2
CD
8886 /* If this is a symbol defined in a dynamic library, don't use the
8887 symbol size from the dynamic library. Relinking an executable
8888 against a new library may introduce gratuitous changes in the
8889 executable's symbols if we keep the size. */
8890 if (sym.st_shndx == SHN_UNDEF
8891 && !h->def_regular
8892 && h->def_dynamic)
8893 sym.st_size = 0;
8894
c152c796
AM
8895 /* If a non-weak symbol with non-default visibility is not defined
8896 locally, it is a fatal error. */
8897 if (! finfo->info->relocatable
8898 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8899 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8900 && h->root.type == bfd_link_hash_undefined
f5385ebf 8901 && !h->def_regular)
c152c796 8902 {
17d078c5
AM
8903 const char *msg;
8904
8905 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8906 msg = _("%B: protected symbol `%s' isn't defined");
8907 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8908 msg = _("%B: internal symbol `%s' isn't defined");
8909 else
8910 msg = _("%B: hidden symbol `%s' isn't defined");
8911 (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string);
8912 bfd_set_error (bfd_error_bad_value);
c152c796
AM
8913 eoinfo->failed = TRUE;
8914 return FALSE;
8915 }
8916
8917 /* If this symbol should be put in the .dynsym section, then put it
8918 there now. We already know the symbol index. We also fill in
8919 the entry in the .hash section. */
8920 if (h->dynindx != -1
8921 && elf_hash_table (finfo->info)->dynamic_sections_created)
8922 {
c152c796
AM
8923 bfd_byte *esym;
8924
8925 sym.st_name = h->dynstr_index;
8926 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
c0d5a53d
L
8927 if (! check_dynsym (finfo->output_bfd, &sym))
8928 {
8929 eoinfo->failed = TRUE;
8930 return FALSE;
8931 }
c152c796
AM
8932 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8933
fdc90cb4
JJ
8934 if (finfo->hash_sec != NULL)
8935 {
8936 size_t hash_entry_size;
8937 bfd_byte *bucketpos;
8938 bfd_vma chain;
41198d0c
L
8939 size_t bucketcount;
8940 size_t bucket;
8941
8942 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8943 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8944
8945 hash_entry_size
8946 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8947 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8948 + (bucket + 2) * hash_entry_size);
8949 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8950 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8951 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8952 ((bfd_byte *) finfo->hash_sec->contents
8953 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8954 }
c152c796
AM
8955
8956 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8957 {
8958 Elf_Internal_Versym iversym;
8959 Elf_External_Versym *eversym;
8960
f5385ebf 8961 if (!h->def_regular)
c152c796
AM
8962 {
8963 if (h->verinfo.verdef == NULL)
8964 iversym.vs_vers = 0;
8965 else
8966 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8967 }
8968 else
8969 {
8970 if (h->verinfo.vertree == NULL)
8971 iversym.vs_vers = 1;
8972 else
8973 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
8974 if (finfo->info->create_default_symver)
8975 iversym.vs_vers++;
c152c796
AM
8976 }
8977
f5385ebf 8978 if (h->hidden)
c152c796
AM
8979 iversym.vs_vers |= VERSYM_HIDDEN;
8980
8981 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8982 eversym += h->dynindx;
8983 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8984 }
8985 }
8986
8987 /* If we're stripping it, then it was just a dynamic symbol, and
8988 there's nothing else to do. */
8989 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8990 return TRUE;
8991
6e0b88f1
AM
8992 indx = bfd_get_symcount (finfo->output_bfd);
8993 ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h);
8994 if (ret == 0)
c152c796
AM
8995 {
8996 eoinfo->failed = TRUE;
8997 return FALSE;
8998 }
6e0b88f1
AM
8999 else if (ret == 1)
9000 h->indx = indx;
9001 else if (h->indx == -2)
9002 abort();
c152c796
AM
9003
9004 return TRUE;
9005}
9006
cdd3575c
AM
9007/* Return TRUE if special handling is done for relocs in SEC against
9008 symbols defined in discarded sections. */
9009
c152c796
AM
9010static bfd_boolean
9011elf_section_ignore_discarded_relocs (asection *sec)
9012{
9013 const struct elf_backend_data *bed;
9014
cdd3575c
AM
9015 switch (sec->sec_info_type)
9016 {
9017 case ELF_INFO_TYPE_STABS:
9018 case ELF_INFO_TYPE_EH_FRAME:
9019 return TRUE;
9020 default:
9021 break;
9022 }
c152c796
AM
9023
9024 bed = get_elf_backend_data (sec->owner);
9025 if (bed->elf_backend_ignore_discarded_relocs != NULL
9026 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9027 return TRUE;
9028
9029 return FALSE;
9030}
9031
9e66c942
AM
9032/* Return a mask saying how ld should treat relocations in SEC against
9033 symbols defined in discarded sections. If this function returns
9034 COMPLAIN set, ld will issue a warning message. If this function
9035 returns PRETEND set, and the discarded section was link-once and the
9036 same size as the kept link-once section, ld will pretend that the
9037 symbol was actually defined in the kept section. Otherwise ld will
9038 zero the reloc (at least that is the intent, but some cooperation by
9039 the target dependent code is needed, particularly for REL targets). */
9040
8a696751
AM
9041unsigned int
9042_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9043{
9e66c942 9044 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9045 return PRETEND;
cdd3575c
AM
9046
9047 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9048 return 0;
cdd3575c
AM
9049
9050 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9051 return 0;
cdd3575c 9052
9e66c942 9053 return COMPLAIN | PRETEND;
cdd3575c
AM
9054}
9055
3d7f7666
L
9056/* Find a match between a section and a member of a section group. */
9057
9058static asection *
c0f00686
L
9059match_group_member (asection *sec, asection *group,
9060 struct bfd_link_info *info)
3d7f7666
L
9061{
9062 asection *first = elf_next_in_group (group);
9063 asection *s = first;
9064
9065 while (s != NULL)
9066 {
c0f00686 9067 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9068 return s;
9069
83180ade 9070 s = elf_next_in_group (s);
3d7f7666
L
9071 if (s == first)
9072 break;
9073 }
9074
9075 return NULL;
9076}
9077
01b3c8ab 9078/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9079 to replace it. Return the replacement if it is OK. Otherwise return
9080 NULL. */
01b3c8ab
L
9081
9082asection *
c0f00686 9083_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9084{
9085 asection *kept;
9086
9087 kept = sec->kept_section;
9088 if (kept != NULL)
9089 {
c2370991 9090 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9091 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9092 if (kept != NULL
9093 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9094 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9095 kept = NULL;
c2370991 9096 sec->kept_section = kept;
01b3c8ab
L
9097 }
9098 return kept;
9099}
9100
c152c796
AM
9101/* Link an input file into the linker output file. This function
9102 handles all the sections and relocations of the input file at once.
9103 This is so that we only have to read the local symbols once, and
9104 don't have to keep them in memory. */
9105
9106static bfd_boolean
9107elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9108{
ece5ef60 9109 int (*relocate_section)
c152c796
AM
9110 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9111 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9112 bfd *output_bfd;
9113 Elf_Internal_Shdr *symtab_hdr;
9114 size_t locsymcount;
9115 size_t extsymoff;
9116 Elf_Internal_Sym *isymbuf;
9117 Elf_Internal_Sym *isym;
9118 Elf_Internal_Sym *isymend;
9119 long *pindex;
9120 asection **ppsection;
9121 asection *o;
9122 const struct elf_backend_data *bed;
c152c796 9123 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9124 bfd_size_type address_size;
9125 bfd_vma r_type_mask;
9126 int r_sym_shift;
c152c796
AM
9127
9128 output_bfd = finfo->output_bfd;
9129 bed = get_elf_backend_data (output_bfd);
9130 relocate_section = bed->elf_backend_relocate_section;
9131
9132 /* If this is a dynamic object, we don't want to do anything here:
9133 we don't want the local symbols, and we don't want the section
9134 contents. */
9135 if ((input_bfd->flags & DYNAMIC) != 0)
9136 return TRUE;
9137
c152c796
AM
9138 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9139 if (elf_bad_symtab (input_bfd))
9140 {
9141 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9142 extsymoff = 0;
9143 }
9144 else
9145 {
9146 locsymcount = symtab_hdr->sh_info;
9147 extsymoff = symtab_hdr->sh_info;
9148 }
9149
9150 /* Read the local symbols. */
9151 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9152 if (isymbuf == NULL && locsymcount != 0)
9153 {
9154 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9155 finfo->internal_syms,
9156 finfo->external_syms,
9157 finfo->locsym_shndx);
9158 if (isymbuf == NULL)
9159 return FALSE;
9160 }
9161
9162 /* Find local symbol sections and adjust values of symbols in
9163 SEC_MERGE sections. Write out those local symbols we know are
9164 going into the output file. */
9165 isymend = isymbuf + locsymcount;
9166 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9167 isym < isymend;
9168 isym++, pindex++, ppsection++)
9169 {
9170 asection *isec;
9171 const char *name;
9172 Elf_Internal_Sym osym;
6e0b88f1
AM
9173 long indx;
9174 int ret;
c152c796
AM
9175
9176 *pindex = -1;
9177
9178 if (elf_bad_symtab (input_bfd))
9179 {
9180 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9181 {
9182 *ppsection = NULL;
9183 continue;
9184 }
9185 }
9186
9187 if (isym->st_shndx == SHN_UNDEF)
9188 isec = bfd_und_section_ptr;
c152c796
AM
9189 else if (isym->st_shndx == SHN_ABS)
9190 isec = bfd_abs_section_ptr;
9191 else if (isym->st_shndx == SHN_COMMON)
9192 isec = bfd_com_section_ptr;
9193 else
9194 {
cb33740c
AM
9195 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9196 if (isec == NULL)
9197 {
9198 /* Don't attempt to output symbols with st_shnx in the
9199 reserved range other than SHN_ABS and SHN_COMMON. */
9200 *ppsection = NULL;
9201 continue;
9202 }
9203 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9204 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9205 isym->st_value =
9206 _bfd_merged_section_offset (output_bfd, &isec,
9207 elf_section_data (isec)->sec_info,
9208 isym->st_value);
c152c796
AM
9209 }
9210
9211 *ppsection = isec;
9212
9213 /* Don't output the first, undefined, symbol. */
9214 if (ppsection == finfo->sections)
9215 continue;
9216
9217 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9218 {
9219 /* We never output section symbols. Instead, we use the
9220 section symbol of the corresponding section in the output
9221 file. */
9222 continue;
9223 }
9224
9225 /* If we are stripping all symbols, we don't want to output this
9226 one. */
9227 if (finfo->info->strip == strip_all)
9228 continue;
9229
9230 /* If we are discarding all local symbols, we don't want to
9231 output this one. If we are generating a relocatable output
9232 file, then some of the local symbols may be required by
9233 relocs; we output them below as we discover that they are
9234 needed. */
9235 if (finfo->info->discard == discard_all)
9236 continue;
9237
9238 /* If this symbol is defined in a section which we are
f02571c5
AM
9239 discarding, we don't need to keep it. */
9240 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9241 && isym->st_shndx < SHN_LORESERVE
9242 && bfd_section_removed_from_list (output_bfd,
9243 isec->output_section))
e75a280b
L
9244 continue;
9245
c152c796
AM
9246 /* Get the name of the symbol. */
9247 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9248 isym->st_name);
9249 if (name == NULL)
9250 return FALSE;
9251
9252 /* See if we are discarding symbols with this name. */
9253 if ((finfo->info->strip == strip_some
9254 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9255 == NULL))
9256 || (((finfo->info->discard == discard_sec_merge
9257 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9258 || finfo->info->discard == discard_l)
9259 && bfd_is_local_label_name (input_bfd, name)))
9260 continue;
9261
c152c796
AM
9262 osym = *isym;
9263
9264 /* Adjust the section index for the output file. */
9265 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9266 isec->output_section);
9267 if (osym.st_shndx == SHN_BAD)
9268 return FALSE;
9269
c152c796
AM
9270 /* ELF symbols in relocatable files are section relative, but
9271 in executable files they are virtual addresses. Note that
9272 this code assumes that all ELF sections have an associated
9273 BFD section with a reasonable value for output_offset; below
9274 we assume that they also have a reasonable value for
9275 output_section. Any special sections must be set up to meet
9276 these requirements. */
9277 osym.st_value += isec->output_offset;
9278 if (! finfo->info->relocatable)
9279 {
9280 osym.st_value += isec->output_section->vma;
9281 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9282 {
9283 /* STT_TLS symbols are relative to PT_TLS segment base. */
9284 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9285 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9286 }
9287 }
9288
6e0b88f1
AM
9289 indx = bfd_get_symcount (output_bfd);
9290 ret = elf_link_output_sym (finfo, name, &osym, isec, NULL);
9291 if (ret == 0)
c152c796 9292 return FALSE;
6e0b88f1
AM
9293 else if (ret == 1)
9294 *pindex = indx;
c152c796
AM
9295 }
9296
310fd250
L
9297 if (bed->s->arch_size == 32)
9298 {
9299 r_type_mask = 0xff;
9300 r_sym_shift = 8;
9301 address_size = 4;
9302 }
9303 else
9304 {
9305 r_type_mask = 0xffffffff;
9306 r_sym_shift = 32;
9307 address_size = 8;
9308 }
9309
c152c796
AM
9310 /* Relocate the contents of each section. */
9311 sym_hashes = elf_sym_hashes (input_bfd);
9312 for (o = input_bfd->sections; o != NULL; o = o->next)
9313 {
9314 bfd_byte *contents;
9315
9316 if (! o->linker_mark)
9317 {
9318 /* This section was omitted from the link. */
9319 continue;
9320 }
9321
bcacc0f5
AM
9322 if (finfo->info->relocatable
9323 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9324 {
9325 /* Deal with the group signature symbol. */
9326 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9327 unsigned long symndx = sec_data->this_hdr.sh_info;
9328 asection *osec = o->output_section;
9329
9330 if (symndx >= locsymcount
9331 || (elf_bad_symtab (input_bfd)
9332 && finfo->sections[symndx] == NULL))
9333 {
9334 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9335 while (h->root.type == bfd_link_hash_indirect
9336 || h->root.type == bfd_link_hash_warning)
9337 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9338 /* Arrange for symbol to be output. */
9339 h->indx = -2;
9340 elf_section_data (osec)->this_hdr.sh_info = -2;
9341 }
9342 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9343 {
9344 /* We'll use the output section target_index. */
9345 asection *sec = finfo->sections[symndx]->output_section;
9346 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9347 }
9348 else
9349 {
9350 if (finfo->indices[symndx] == -1)
9351 {
9352 /* Otherwise output the local symbol now. */
9353 Elf_Internal_Sym sym = isymbuf[symndx];
9354 asection *sec = finfo->sections[symndx]->output_section;
9355 const char *name;
6e0b88f1
AM
9356 long indx;
9357 int ret;
bcacc0f5
AM
9358
9359 name = bfd_elf_string_from_elf_section (input_bfd,
9360 symtab_hdr->sh_link,
9361 sym.st_name);
9362 if (name == NULL)
9363 return FALSE;
9364
9365 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9366 sec);
9367 if (sym.st_shndx == SHN_BAD)
9368 return FALSE;
9369
9370 sym.st_value += o->output_offset;
9371
6e0b88f1
AM
9372 indx = bfd_get_symcount (output_bfd);
9373 ret = elf_link_output_sym (finfo, name, &sym, o, NULL);
9374 if (ret == 0)
bcacc0f5 9375 return FALSE;
6e0b88f1
AM
9376 else if (ret == 1)
9377 finfo->indices[symndx] = indx;
9378 else
9379 abort ();
bcacc0f5
AM
9380 }
9381 elf_section_data (osec)->this_hdr.sh_info
9382 = finfo->indices[symndx];
9383 }
9384 }
9385
c152c796 9386 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9387 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9388 continue;
9389
9390 if ((o->flags & SEC_LINKER_CREATED) != 0)
9391 {
9392 /* Section was created by _bfd_elf_link_create_dynamic_sections
9393 or somesuch. */
9394 continue;
9395 }
9396
9397 /* Get the contents of the section. They have been cached by a
9398 relaxation routine. Note that o is a section in an input
9399 file, so the contents field will not have been set by any of
9400 the routines which work on output files. */
9401 if (elf_section_data (o)->this_hdr.contents != NULL)
9402 contents = elf_section_data (o)->this_hdr.contents;
9403 else
9404 {
9405 contents = finfo->contents;
4a114e3e 9406 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9407 return FALSE;
9408 }
9409
9410 if ((o->flags & SEC_RELOC) != 0)
9411 {
9412 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9413 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9414 int action_discarded;
ece5ef60 9415 int ret;
c152c796
AM
9416
9417 /* Get the swapped relocs. */
9418 internal_relocs
9419 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9420 finfo->internal_relocs, FALSE);
9421 if (internal_relocs == NULL
9422 && o->reloc_count > 0)
9423 return FALSE;
9424
310fd250
L
9425 /* We need to reverse-copy input .ctors/.dtors sections if
9426 they are placed in .init_array/.finit_array for output. */
9427 if (o->size > address_size
9428 && ((strncmp (o->name, ".ctors", 6) == 0
9429 && strcmp (o->output_section->name,
9430 ".init_array") == 0)
9431 || (strncmp (o->name, ".dtors", 6) == 0
9432 && strcmp (o->output_section->name,
9433 ".fini_array") == 0))
9434 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9435 {
310fd250
L
9436 if (o->size != o->reloc_count * address_size)
9437 {
9438 (*_bfd_error_handler)
9439 (_("error: %B: size of section %A is not "
9440 "multiple of address size"),
9441 input_bfd, o);
9442 bfd_set_error (bfd_error_on_input);
9443 return FALSE;
9444 }
9445 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
9446 }
9447
0f02bbd9 9448 action_discarded = -1;
c152c796 9449 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9450 action_discarded = (*bed->action_discarded) (o);
9451
9452 /* Run through the relocs evaluating complex reloc symbols and
9453 looking for relocs against symbols from discarded sections
9454 or section symbols from removed link-once sections.
9455 Complain about relocs against discarded sections. Zero
9456 relocs against removed link-once sections. */
9457
9458 rel = internal_relocs;
9459 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9460 for ( ; rel < relend; rel++)
c152c796 9461 {
0f02bbd9
AM
9462 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9463 unsigned int s_type;
9464 asection **ps, *sec;
9465 struct elf_link_hash_entry *h = NULL;
9466 const char *sym_name;
c152c796 9467
0f02bbd9
AM
9468 if (r_symndx == STN_UNDEF)
9469 continue;
c152c796 9470
0f02bbd9
AM
9471 if (r_symndx >= locsymcount
9472 || (elf_bad_symtab (input_bfd)
9473 && finfo->sections[r_symndx] == NULL))
9474 {
9475 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9476
0f02bbd9
AM
9477 /* Badly formatted input files can contain relocs that
9478 reference non-existant symbols. Check here so that
9479 we do not seg fault. */
9480 if (h == NULL)
c152c796 9481 {
0f02bbd9 9482 char buffer [32];
dce669a1 9483
0f02bbd9
AM
9484 sprintf_vma (buffer, rel->r_info);
9485 (*_bfd_error_handler)
9486 (_("error: %B contains a reloc (0x%s) for section %A "
9487 "that references a non-existent global symbol"),
9488 input_bfd, o, buffer);
9489 bfd_set_error (bfd_error_bad_value);
9490 return FALSE;
9491 }
3b36f7e6 9492
0f02bbd9
AM
9493 while (h->root.type == bfd_link_hash_indirect
9494 || h->root.type == bfd_link_hash_warning)
9495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9496
0f02bbd9 9497 s_type = h->type;
cdd3575c 9498
0f02bbd9
AM
9499 ps = NULL;
9500 if (h->root.type == bfd_link_hash_defined
9501 || h->root.type == bfd_link_hash_defweak)
9502 ps = &h->root.u.def.section;
9503
9504 sym_name = h->root.root.string;
9505 }
9506 else
9507 {
9508 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9509
9510 s_type = ELF_ST_TYPE (sym->st_info);
9511 ps = &finfo->sections[r_symndx];
9512 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9513 sym, *ps);
9514 }
c152c796 9515
c301e700
DD
9516 if ((s_type == STT_RELC || s_type == STT_SRELC)
9517 && !finfo->info->relocatable)
0f02bbd9
AM
9518 {
9519 bfd_vma val;
9520 bfd_vma dot = (rel->r_offset
9521 + o->output_offset + o->output_section->vma);
9522#ifdef DEBUG
9523 printf ("Encountered a complex symbol!");
9524 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
9525 input_bfd->filename, o->name,
9526 (long) (rel - internal_relocs));
0f02bbd9
AM
9527 printf (" symbol: idx %8.8lx, name %s\n",
9528 r_symndx, sym_name);
9529 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9530 (unsigned long) rel->r_info,
9531 (unsigned long) rel->r_offset);
9532#endif
9533 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9534 isymbuf, locsymcount, s_type == STT_SRELC))
9535 return FALSE;
9536
9537 /* Symbol evaluated OK. Update to absolute value. */
9538 set_symbol_value (input_bfd, isymbuf, locsymcount,
9539 r_symndx, val);
9540 continue;
9541 }
9542
9543 if (action_discarded != -1 && ps != NULL)
9544 {
cdd3575c
AM
9545 /* Complain if the definition comes from a
9546 discarded section. */
9547 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9548 {
cf35638d 9549 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 9550 if (action_discarded & COMPLAIN)
e1fffbe6
AM
9551 (*finfo->info->callbacks->einfo)
9552 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9553 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9554 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9555
87e5235d 9556 /* Try to do the best we can to support buggy old
e0ae6d6f 9557 versions of gcc. Pretend that the symbol is
87e5235d
AM
9558 really defined in the kept linkonce section.
9559 FIXME: This is quite broken. Modifying the
9560 symbol here means we will be changing all later
e0ae6d6f 9561 uses of the symbol, not just in this section. */
0f02bbd9 9562 if (action_discarded & PRETEND)
87e5235d 9563 {
01b3c8ab
L
9564 asection *kept;
9565
c0f00686
L
9566 kept = _bfd_elf_check_kept_section (sec,
9567 finfo->info);
01b3c8ab 9568 if (kept != NULL)
87e5235d
AM
9569 {
9570 *ps = kept;
9571 continue;
9572 }
9573 }
c152c796
AM
9574 }
9575 }
9576 }
9577
9578 /* Relocate the section by invoking a back end routine.
9579
9580 The back end routine is responsible for adjusting the
9581 section contents as necessary, and (if using Rela relocs
9582 and generating a relocatable output file) adjusting the
9583 reloc addend as necessary.
9584
9585 The back end routine does not have to worry about setting
9586 the reloc address or the reloc symbol index.
9587
9588 The back end routine is given a pointer to the swapped in
9589 internal symbols, and can access the hash table entries
9590 for the external symbols via elf_sym_hashes (input_bfd).
9591
9592 When generating relocatable output, the back end routine
9593 must handle STB_LOCAL/STT_SECTION symbols specially. The
9594 output symbol is going to be a section symbol
9595 corresponding to the output section, which will require
9596 the addend to be adjusted. */
9597
ece5ef60 9598 ret = (*relocate_section) (output_bfd, finfo->info,
c152c796
AM
9599 input_bfd, o, contents,
9600 internal_relocs,
9601 isymbuf,
ece5ef60
AM
9602 finfo->sections);
9603 if (!ret)
c152c796
AM
9604 return FALSE;
9605
ece5ef60
AM
9606 if (ret == 2
9607 || finfo->info->relocatable
9608 || finfo->info->emitrelocations)
c152c796
AM
9609 {
9610 Elf_Internal_Rela *irela;
d4730f92 9611 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
9612 bfd_vma last_offset;
9613 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
9614 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9615 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 9616 unsigned int next_erel;
c152c796 9617 bfd_boolean rela_normal;
d4730f92 9618 struct bfd_elf_section_data *esdi, *esdo;
c152c796 9619
d4730f92
BS
9620 esdi = elf_section_data (o);
9621 esdo = elf_section_data (o->output_section);
9622 rela_normal = FALSE;
c152c796
AM
9623
9624 /* Adjust the reloc addresses and symbol indices. */
9625
9626 irela = internal_relocs;
9627 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
9628 rel_hash = esdo->rel.hashes + esdo->rel.count;
9629 /* We start processing the REL relocs, if any. When we reach
9630 IRELAMID in the loop, we switch to the RELA relocs. */
9631 irelamid = irela;
9632 if (esdi->rel.hdr != NULL)
9633 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9634 * bed->s->int_rels_per_ext_rel);
eac338cf 9635 rel_hash_list = rel_hash;
d4730f92 9636 rela_hash_list = NULL;
c152c796
AM
9637 last_offset = o->output_offset;
9638 if (!finfo->info->relocatable)
9639 last_offset += o->output_section->vma;
9640 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9641 {
9642 unsigned long r_symndx;
9643 asection *sec;
9644 Elf_Internal_Sym sym;
9645
9646 if (next_erel == bed->s->int_rels_per_ext_rel)
9647 {
9648 rel_hash++;
9649 next_erel = 0;
9650 }
9651
d4730f92
BS
9652 if (irela == irelamid)
9653 {
9654 rel_hash = esdo->rela.hashes + esdo->rela.count;
9655 rela_hash_list = rel_hash;
9656 rela_normal = bed->rela_normal;
9657 }
9658
c152c796
AM
9659 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9660 finfo->info, o,
9661 irela->r_offset);
9662 if (irela->r_offset >= (bfd_vma) -2)
9663 {
9664 /* This is a reloc for a deleted entry or somesuch.
9665 Turn it into an R_*_NONE reloc, at the same
9666 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9667 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9668 being ordered. */
9669 irela->r_offset = last_offset;
9670 irela->r_info = 0;
9671 irela->r_addend = 0;
9672 continue;
9673 }
9674
9675 irela->r_offset += o->output_offset;
9676
9677 /* Relocs in an executable have to be virtual addresses. */
9678 if (!finfo->info->relocatable)
9679 irela->r_offset += o->output_section->vma;
9680
9681 last_offset = irela->r_offset;
9682
9683 r_symndx = irela->r_info >> r_sym_shift;
9684 if (r_symndx == STN_UNDEF)
9685 continue;
9686
9687 if (r_symndx >= locsymcount
9688 || (elf_bad_symtab (input_bfd)
9689 && finfo->sections[r_symndx] == NULL))
9690 {
9691 struct elf_link_hash_entry *rh;
9692 unsigned long indx;
9693
9694 /* This is a reloc against a global symbol. We
9695 have not yet output all the local symbols, so
9696 we do not know the symbol index of any global
9697 symbol. We set the rel_hash entry for this
9698 reloc to point to the global hash table entry
9699 for this symbol. The symbol index is then
ee75fd95 9700 set at the end of bfd_elf_final_link. */
c152c796
AM
9701 indx = r_symndx - extsymoff;
9702 rh = elf_sym_hashes (input_bfd)[indx];
9703 while (rh->root.type == bfd_link_hash_indirect
9704 || rh->root.type == bfd_link_hash_warning)
9705 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9706
9707 /* Setting the index to -2 tells
9708 elf_link_output_extsym that this symbol is
9709 used by a reloc. */
9710 BFD_ASSERT (rh->indx < 0);
9711 rh->indx = -2;
9712
9713 *rel_hash = rh;
9714
9715 continue;
9716 }
9717
9718 /* This is a reloc against a local symbol. */
9719
9720 *rel_hash = NULL;
9721 sym = isymbuf[r_symndx];
9722 sec = finfo->sections[r_symndx];
9723 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9724 {
9725 /* I suppose the backend ought to fill in the
9726 section of any STT_SECTION symbol against a
6a8d1586 9727 processor specific section. */
cf35638d 9728 r_symndx = STN_UNDEF;
6a8d1586
AM
9729 if (bfd_is_abs_section (sec))
9730 ;
c152c796
AM
9731 else if (sec == NULL || sec->owner == NULL)
9732 {
9733 bfd_set_error (bfd_error_bad_value);
9734 return FALSE;
9735 }
9736 else
9737 {
6a8d1586
AM
9738 asection *osec = sec->output_section;
9739
9740 /* If we have discarded a section, the output
9741 section will be the absolute section. In
ab96bf03
AM
9742 case of discarded SEC_MERGE sections, use
9743 the kept section. relocate_section should
9744 have already handled discarded linkonce
9745 sections. */
6a8d1586
AM
9746 if (bfd_is_abs_section (osec)
9747 && sec->kept_section != NULL
9748 && sec->kept_section->output_section != NULL)
9749 {
9750 osec = sec->kept_section->output_section;
9751 irela->r_addend -= osec->vma;
9752 }
9753
9754 if (!bfd_is_abs_section (osec))
9755 {
9756 r_symndx = osec->target_index;
cf35638d 9757 if (r_symndx == STN_UNDEF)
74541ad4
AM
9758 {
9759 struct elf_link_hash_table *htab;
9760 asection *oi;
9761
9762 htab = elf_hash_table (finfo->info);
9763 oi = htab->text_index_section;
9764 if ((osec->flags & SEC_READONLY) == 0
9765 && htab->data_index_section != NULL)
9766 oi = htab->data_index_section;
9767
9768 if (oi != NULL)
9769 {
9770 irela->r_addend += osec->vma - oi->vma;
9771 r_symndx = oi->target_index;
9772 }
9773 }
9774
cf35638d 9775 BFD_ASSERT (r_symndx != STN_UNDEF);
6a8d1586 9776 }
c152c796
AM
9777 }
9778
9779 /* Adjust the addend according to where the
9780 section winds up in the output section. */
9781 if (rela_normal)
9782 irela->r_addend += sec->output_offset;
9783 }
9784 else
9785 {
9786 if (finfo->indices[r_symndx] == -1)
9787 {
9788 unsigned long shlink;
9789 const char *name;
9790 asection *osec;
6e0b88f1 9791 long indx;
c152c796
AM
9792
9793 if (finfo->info->strip == strip_all)
9794 {
9795 /* You can't do ld -r -s. */
9796 bfd_set_error (bfd_error_invalid_operation);
9797 return FALSE;
9798 }
9799
9800 /* This symbol was skipped earlier, but
9801 since it is needed by a reloc, we
9802 must output it now. */
9803 shlink = symtab_hdr->sh_link;
9804 name = (bfd_elf_string_from_elf_section
9805 (input_bfd, shlink, sym.st_name));
9806 if (name == NULL)
9807 return FALSE;
9808
9809 osec = sec->output_section;
9810 sym.st_shndx =
9811 _bfd_elf_section_from_bfd_section (output_bfd,
9812 osec);
9813 if (sym.st_shndx == SHN_BAD)
9814 return FALSE;
9815
9816 sym.st_value += sec->output_offset;
9817 if (! finfo->info->relocatable)
9818 {
9819 sym.st_value += osec->vma;
9820 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9821 {
9822 /* STT_TLS symbols are relative to PT_TLS
9823 segment base. */
9824 BFD_ASSERT (elf_hash_table (finfo->info)
9825 ->tls_sec != NULL);
9826 sym.st_value -= (elf_hash_table (finfo->info)
9827 ->tls_sec->vma);
9828 }
9829 }
9830
6e0b88f1
AM
9831 indx = bfd_get_symcount (output_bfd);
9832 ret = elf_link_output_sym (finfo, name, &sym, sec,
9833 NULL);
9834 if (ret == 0)
c152c796 9835 return FALSE;
6e0b88f1
AM
9836 else if (ret == 1)
9837 finfo->indices[r_symndx] = indx;
9838 else
9839 abort ();
c152c796
AM
9840 }
9841
9842 r_symndx = finfo->indices[r_symndx];
9843 }
9844
9845 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9846 | (irela->r_info & r_type_mask));
9847 }
9848
9849 /* Swap out the relocs. */
d4730f92
BS
9850 input_rel_hdr = esdi->rel.hdr;
9851 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 9852 {
d4730f92
BS
9853 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9854 input_rel_hdr,
9855 internal_relocs,
9856 rel_hash_list))
9857 return FALSE;
c152c796
AM
9858 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9859 * bed->s->int_rels_per_ext_rel);
eac338cf 9860 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
9861 }
9862
9863 input_rela_hdr = esdi->rela.hdr;
9864 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9865 {
eac338cf 9866 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 9867 input_rela_hdr,
eac338cf 9868 internal_relocs,
d4730f92 9869 rela_hash_list))
c152c796
AM
9870 return FALSE;
9871 }
9872 }
9873 }
9874
9875 /* Write out the modified section contents. */
9876 if (bed->elf_backend_write_section
c7b8f16e
JB
9877 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9878 contents))
c152c796
AM
9879 {
9880 /* Section written out. */
9881 }
9882 else switch (o->sec_info_type)
9883 {
9884 case ELF_INFO_TYPE_STABS:
9885 if (! (_bfd_write_section_stabs
9886 (output_bfd,
9887 &elf_hash_table (finfo->info)->stab_info,
9888 o, &elf_section_data (o)->sec_info, contents)))
9889 return FALSE;
9890 break;
9891 case ELF_INFO_TYPE_MERGE:
9892 if (! _bfd_write_merged_section (output_bfd, o,
9893 elf_section_data (o)->sec_info))
9894 return FALSE;
9895 break;
9896 case ELF_INFO_TYPE_EH_FRAME:
9897 {
9898 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9899 o, contents))
9900 return FALSE;
9901 }
9902 break;
9903 default:
9904 {
5dabe785 9905 /* FIXME: octets_per_byte. */
310fd250
L
9906 if (! (o->flags & SEC_EXCLUDE))
9907 {
9908 file_ptr offset = (file_ptr) o->output_offset;
9909 bfd_size_type todo = o->size;
9910 if ((o->flags & SEC_ELF_REVERSE_COPY))
9911 {
9912 /* Reverse-copy input section to output. */
9913 do
9914 {
9915 todo -= address_size;
9916 if (! bfd_set_section_contents (output_bfd,
9917 o->output_section,
9918 contents + todo,
9919 offset,
9920 address_size))
9921 return FALSE;
9922 if (todo == 0)
9923 break;
9924 offset += address_size;
9925 }
9926 while (1);
9927 }
9928 else if (! bfd_set_section_contents (output_bfd,
9929 o->output_section,
9930 contents,
9931 offset, todo))
9932 return FALSE;
9933 }
c152c796
AM
9934 }
9935 break;
9936 }
9937 }
9938
9939 return TRUE;
9940}
9941
9942/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9943 requested by the linker, and does not come from any input file. This
c152c796
AM
9944 is used to build constructor and destructor tables when linking
9945 with -Ur. */
9946
9947static bfd_boolean
9948elf_reloc_link_order (bfd *output_bfd,
9949 struct bfd_link_info *info,
9950 asection *output_section,
9951 struct bfd_link_order *link_order)
9952{
9953 reloc_howto_type *howto;
9954 long indx;
9955 bfd_vma offset;
9956 bfd_vma addend;
d4730f92 9957 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
9958 struct elf_link_hash_entry **rel_hash_ptr;
9959 Elf_Internal_Shdr *rel_hdr;
9960 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9961 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9962 bfd_byte *erel;
9963 unsigned int i;
d4730f92 9964 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
9965
9966 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9967 if (howto == NULL)
9968 {
9969 bfd_set_error (bfd_error_bad_value);
9970 return FALSE;
9971 }
9972
9973 addend = link_order->u.reloc.p->addend;
9974
d4730f92
BS
9975 if (esdo->rel.hdr)
9976 reldata = &esdo->rel;
9977 else if (esdo->rela.hdr)
9978 reldata = &esdo->rela;
9979 else
9980 {
9981 reldata = NULL;
9982 BFD_ASSERT (0);
9983 }
9984
c152c796 9985 /* Figure out the symbol index. */
d4730f92 9986 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
9987 if (link_order->type == bfd_section_reloc_link_order)
9988 {
9989 indx = link_order->u.reloc.p->u.section->target_index;
9990 BFD_ASSERT (indx != 0);
9991 *rel_hash_ptr = NULL;
9992 }
9993 else
9994 {
9995 struct elf_link_hash_entry *h;
9996
9997 /* Treat a reloc against a defined symbol as though it were
9998 actually against the section. */
9999 h = ((struct elf_link_hash_entry *)
10000 bfd_wrapped_link_hash_lookup (output_bfd, info,
10001 link_order->u.reloc.p->u.name,
10002 FALSE, FALSE, TRUE));
10003 if (h != NULL
10004 && (h->root.type == bfd_link_hash_defined
10005 || h->root.type == bfd_link_hash_defweak))
10006 {
10007 asection *section;
10008
10009 section = h->root.u.def.section;
10010 indx = section->output_section->target_index;
10011 *rel_hash_ptr = NULL;
10012 /* It seems that we ought to add the symbol value to the
10013 addend here, but in practice it has already been added
10014 because it was passed to constructor_callback. */
10015 addend += section->output_section->vma + section->output_offset;
10016 }
10017 else if (h != NULL)
10018 {
10019 /* Setting the index to -2 tells elf_link_output_extsym that
10020 this symbol is used by a reloc. */
10021 h->indx = -2;
10022 *rel_hash_ptr = h;
10023 indx = 0;
10024 }
10025 else
10026 {
10027 if (! ((*info->callbacks->unattached_reloc)
10028 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10029 return FALSE;
10030 indx = 0;
10031 }
10032 }
10033
10034 /* If this is an inplace reloc, we must write the addend into the
10035 object file. */
10036 if (howto->partial_inplace && addend != 0)
10037 {
10038 bfd_size_type size;
10039 bfd_reloc_status_type rstat;
10040 bfd_byte *buf;
10041 bfd_boolean ok;
10042 const char *sym_name;
10043
a50b1753
NC
10044 size = (bfd_size_type) bfd_get_reloc_size (howto);
10045 buf = (bfd_byte *) bfd_zmalloc (size);
c152c796
AM
10046 if (buf == NULL)
10047 return FALSE;
10048 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10049 switch (rstat)
10050 {
10051 case bfd_reloc_ok:
10052 break;
10053
10054 default:
10055 case bfd_reloc_outofrange:
10056 abort ();
10057
10058 case bfd_reloc_overflow:
10059 if (link_order->type == bfd_section_reloc_link_order)
10060 sym_name = bfd_section_name (output_bfd,
10061 link_order->u.reloc.p->u.section);
10062 else
10063 sym_name = link_order->u.reloc.p->u.name;
10064 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10065 (info, NULL, sym_name, howto->name, addend, NULL,
10066 NULL, (bfd_vma) 0)))
c152c796
AM
10067 {
10068 free (buf);
10069 return FALSE;
10070 }
10071 break;
10072 }
10073 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10074 link_order->offset, size);
10075 free (buf);
10076 if (! ok)
10077 return FALSE;
10078 }
10079
10080 /* The address of a reloc is relative to the section in a
10081 relocatable file, and is a virtual address in an executable
10082 file. */
10083 offset = link_order->offset;
10084 if (! info->relocatable)
10085 offset += output_section->vma;
10086
10087 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10088 {
10089 irel[i].r_offset = offset;
10090 irel[i].r_info = 0;
10091 irel[i].r_addend = 0;
10092 }
10093 if (bed->s->arch_size == 32)
10094 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10095 else
10096 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10097
d4730f92 10098 rel_hdr = reldata->hdr;
c152c796
AM
10099 erel = rel_hdr->contents;
10100 if (rel_hdr->sh_type == SHT_REL)
10101 {
d4730f92 10102 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10103 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10104 }
10105 else
10106 {
10107 irel[0].r_addend = addend;
d4730f92 10108 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10109 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10110 }
10111
d4730f92 10112 ++reldata->count;
c152c796
AM
10113
10114 return TRUE;
10115}
10116
0b52efa6
PB
10117
10118/* Get the output vma of the section pointed to by the sh_link field. */
10119
10120static bfd_vma
10121elf_get_linked_section_vma (struct bfd_link_order *p)
10122{
10123 Elf_Internal_Shdr **elf_shdrp;
10124 asection *s;
10125 int elfsec;
10126
10127 s = p->u.indirect.section;
10128 elf_shdrp = elf_elfsections (s->owner);
10129 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10130 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10131 /* PR 290:
10132 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10133 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10134 sh_info fields. Hence we could get the situation
10135 where elfsec is 0. */
10136 if (elfsec == 0)
10137 {
10138 const struct elf_backend_data *bed
10139 = get_elf_backend_data (s->owner);
10140 if (bed->link_order_error_handler)
d003868e
AM
10141 bed->link_order_error_handler
10142 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10143 return 0;
10144 }
10145 else
10146 {
10147 s = elf_shdrp[elfsec]->bfd_section;
10148 return s->output_section->vma + s->output_offset;
10149 }
0b52efa6
PB
10150}
10151
10152
10153/* Compare two sections based on the locations of the sections they are
10154 linked to. Used by elf_fixup_link_order. */
10155
10156static int
10157compare_link_order (const void * a, const void * b)
10158{
10159 bfd_vma apos;
10160 bfd_vma bpos;
10161
10162 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10163 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10164 if (apos < bpos)
10165 return -1;
10166 return apos > bpos;
10167}
10168
10169
10170/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10171 order as their linked sections. Returns false if this could not be done
10172 because an output section includes both ordered and unordered
10173 sections. Ideally we'd do this in the linker proper. */
10174
10175static bfd_boolean
10176elf_fixup_link_order (bfd *abfd, asection *o)
10177{
10178 int seen_linkorder;
10179 int seen_other;
10180 int n;
10181 struct bfd_link_order *p;
10182 bfd *sub;
10183 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10184 unsigned elfsec;
0b52efa6 10185 struct bfd_link_order **sections;
d33cdfe3 10186 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10187 bfd_vma offset;
3b36f7e6 10188
d33cdfe3
L
10189 other_sec = NULL;
10190 linkorder_sec = NULL;
0b52efa6
PB
10191 seen_other = 0;
10192 seen_linkorder = 0;
8423293d 10193 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10194 {
d33cdfe3 10195 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10196 {
10197 s = p->u.indirect.section;
d33cdfe3
L
10198 sub = s->owner;
10199 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10200 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10201 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10202 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10203 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10204 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10205 {
10206 seen_linkorder++;
10207 linkorder_sec = s;
10208 }
0b52efa6 10209 else
d33cdfe3
L
10210 {
10211 seen_other++;
10212 other_sec = s;
10213 }
0b52efa6
PB
10214 }
10215 else
10216 seen_other++;
d33cdfe3
L
10217
10218 if (seen_other && seen_linkorder)
10219 {
10220 if (other_sec && linkorder_sec)
10221 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10222 o, linkorder_sec,
10223 linkorder_sec->owner, other_sec,
10224 other_sec->owner);
10225 else
10226 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10227 o);
10228 bfd_set_error (bfd_error_bad_value);
10229 return FALSE;
10230 }
0b52efa6
PB
10231 }
10232
10233 if (!seen_linkorder)
10234 return TRUE;
10235
0b52efa6 10236 sections = (struct bfd_link_order **)
14b1c01e
AM
10237 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10238 if (sections == NULL)
10239 return FALSE;
0b52efa6 10240 seen_linkorder = 0;
3b36f7e6 10241
8423293d 10242 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10243 {
10244 sections[seen_linkorder++] = p;
10245 }
10246 /* Sort the input sections in the order of their linked section. */
10247 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10248 compare_link_order);
10249
10250 /* Change the offsets of the sections. */
10251 offset = 0;
10252 for (n = 0; n < seen_linkorder; n++)
10253 {
10254 s = sections[n]->u.indirect.section;
461686a3 10255 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10256 s->output_offset = offset;
10257 sections[n]->offset = offset;
5dabe785 10258 /* FIXME: octets_per_byte. */
0b52efa6
PB
10259 offset += sections[n]->size;
10260 }
10261
4dd07732 10262 free (sections);
0b52efa6
PB
10263 return TRUE;
10264}
10265
10266
c152c796
AM
10267/* Do the final step of an ELF link. */
10268
10269bfd_boolean
10270bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10271{
10272 bfd_boolean dynamic;
10273 bfd_boolean emit_relocs;
10274 bfd *dynobj;
10275 struct elf_final_link_info finfo;
91d6fa6a
NC
10276 asection *o;
10277 struct bfd_link_order *p;
10278 bfd *sub;
c152c796
AM
10279 bfd_size_type max_contents_size;
10280 bfd_size_type max_external_reloc_size;
10281 bfd_size_type max_internal_reloc_count;
10282 bfd_size_type max_sym_count;
10283 bfd_size_type max_sym_shndx_count;
10284 file_ptr off;
10285 Elf_Internal_Sym elfsym;
10286 unsigned int i;
10287 Elf_Internal_Shdr *symtab_hdr;
10288 Elf_Internal_Shdr *symtab_shndx_hdr;
10289 Elf_Internal_Shdr *symstrtab_hdr;
10290 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10291 struct elf_outext_info eoinfo;
10292 bfd_boolean merged;
10293 size_t relativecount = 0;
10294 asection *reldyn = 0;
10295 bfd_size_type amt;
104d59d1
JM
10296 asection *attr_section = NULL;
10297 bfd_vma attr_size = 0;
10298 const char *std_attrs_section;
c152c796
AM
10299
10300 if (! is_elf_hash_table (info->hash))
10301 return FALSE;
10302
10303 if (info->shared)
10304 abfd->flags |= DYNAMIC;
10305
10306 dynamic = elf_hash_table (info)->dynamic_sections_created;
10307 dynobj = elf_hash_table (info)->dynobj;
10308
10309 emit_relocs = (info->relocatable
a4676736 10310 || info->emitrelocations);
c152c796
AM
10311
10312 finfo.info = info;
10313 finfo.output_bfd = abfd;
10314 finfo.symstrtab = _bfd_elf_stringtab_init ();
10315 if (finfo.symstrtab == NULL)
10316 return FALSE;
10317
10318 if (! dynamic)
10319 {
10320 finfo.dynsym_sec = NULL;
10321 finfo.hash_sec = NULL;
10322 finfo.symver_sec = NULL;
10323 }
10324 else
10325 {
10326 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10327 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
fdc90cb4 10328 BFD_ASSERT (finfo.dynsym_sec != NULL);
c152c796
AM
10329 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10330 /* Note that it is OK if symver_sec is NULL. */
10331 }
10332
10333 finfo.contents = NULL;
10334 finfo.external_relocs = NULL;
10335 finfo.internal_relocs = NULL;
10336 finfo.external_syms = NULL;
10337 finfo.locsym_shndx = NULL;
10338 finfo.internal_syms = NULL;
10339 finfo.indices = NULL;
10340 finfo.sections = NULL;
10341 finfo.symbuf = NULL;
10342 finfo.symshndxbuf = NULL;
10343 finfo.symbuf_count = 0;
10344 finfo.shndxbuf_size = 0;
10345
104d59d1
JM
10346 /* The object attributes have been merged. Remove the input
10347 sections from the link, and set the contents of the output
10348 secton. */
10349 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10350 for (o = abfd->sections; o != NULL; o = o->next)
10351 {
10352 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10353 || strcmp (o->name, ".gnu.attributes") == 0)
10354 {
10355 for (p = o->map_head.link_order; p != NULL; p = p->next)
10356 {
10357 asection *input_section;
10358
10359 if (p->type != bfd_indirect_link_order)
10360 continue;
10361 input_section = p->u.indirect.section;
10362 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10363 elf_link_input_bfd ignores this section. */
10364 input_section->flags &= ~SEC_HAS_CONTENTS;
10365 }
a0c8462f 10366
104d59d1
JM
10367 attr_size = bfd_elf_obj_attr_size (abfd);
10368 if (attr_size)
10369 {
10370 bfd_set_section_size (abfd, o, attr_size);
10371 attr_section = o;
10372 /* Skip this section later on. */
10373 o->map_head.link_order = NULL;
10374 }
10375 else
10376 o->flags |= SEC_EXCLUDE;
10377 }
10378 }
10379
c152c796
AM
10380 /* Count up the number of relocations we will output for each output
10381 section, so that we know the sizes of the reloc sections. We
10382 also figure out some maximum sizes. */
10383 max_contents_size = 0;
10384 max_external_reloc_size = 0;
10385 max_internal_reloc_count = 0;
10386 max_sym_count = 0;
10387 max_sym_shndx_count = 0;
10388 merged = FALSE;
10389 for (o = abfd->sections; o != NULL; o = o->next)
10390 {
10391 struct bfd_elf_section_data *esdo = elf_section_data (o);
10392 o->reloc_count = 0;
10393
8423293d 10394 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10395 {
10396 unsigned int reloc_count = 0;
10397 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10398
10399 if (p->type == bfd_section_reloc_link_order
10400 || p->type == bfd_symbol_reloc_link_order)
10401 reloc_count = 1;
10402 else if (p->type == bfd_indirect_link_order)
10403 {
10404 asection *sec;
10405
10406 sec = p->u.indirect.section;
10407 esdi = elf_section_data (sec);
10408
10409 /* Mark all sections which are to be included in the
10410 link. This will normally be every section. We need
10411 to do this so that we can identify any sections which
10412 the linker has decided to not include. */
10413 sec->linker_mark = TRUE;
10414
10415 if (sec->flags & SEC_MERGE)
10416 merged = TRUE;
10417
10418 if (info->relocatable || info->emitrelocations)
10419 reloc_count = sec->reloc_count;
10420 else if (bed->elf_backend_count_relocs)
58217f29 10421 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 10422
eea6121a
AM
10423 if (sec->rawsize > max_contents_size)
10424 max_contents_size = sec->rawsize;
10425 if (sec->size > max_contents_size)
10426 max_contents_size = sec->size;
c152c796
AM
10427
10428 /* We are interested in just local symbols, not all
10429 symbols. */
10430 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10431 && (sec->owner->flags & DYNAMIC) == 0)
10432 {
10433 size_t sym_count;
10434
10435 if (elf_bad_symtab (sec->owner))
10436 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10437 / bed->s->sizeof_sym);
10438 else
10439 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10440
10441 if (sym_count > max_sym_count)
10442 max_sym_count = sym_count;
10443
10444 if (sym_count > max_sym_shndx_count
10445 && elf_symtab_shndx (sec->owner) != 0)
10446 max_sym_shndx_count = sym_count;
10447
10448 if ((sec->flags & SEC_RELOC) != 0)
10449 {
d4730f92 10450 size_t ext_size = 0;
c152c796 10451
d4730f92
BS
10452 if (esdi->rel.hdr != NULL)
10453 ext_size = esdi->rel.hdr->sh_size;
10454 if (esdi->rela.hdr != NULL)
10455 ext_size += esdi->rela.hdr->sh_size;
7326c758 10456
c152c796
AM
10457 if (ext_size > max_external_reloc_size)
10458 max_external_reloc_size = ext_size;
10459 if (sec->reloc_count > max_internal_reloc_count)
10460 max_internal_reloc_count = sec->reloc_count;
10461 }
10462 }
10463 }
10464
10465 if (reloc_count == 0)
10466 continue;
10467
10468 o->reloc_count += reloc_count;
10469
d4730f92
BS
10470 if (p->type == bfd_indirect_link_order
10471 && (info->relocatable || info->emitrelocations))
c152c796 10472 {
d4730f92
BS
10473 if (esdi->rel.hdr)
10474 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10475 if (esdi->rela.hdr)
10476 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10477 }
10478 else
10479 {
10480 if (o->use_rela_p)
10481 esdo->rela.count += reloc_count;
2c2b4ed4 10482 else
d4730f92 10483 esdo->rel.count += reloc_count;
c152c796 10484 }
c152c796
AM
10485 }
10486
10487 if (o->reloc_count > 0)
10488 o->flags |= SEC_RELOC;
10489 else
10490 {
10491 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10492 set it (this is probably a bug) and if it is set
10493 assign_section_numbers will create a reloc section. */
10494 o->flags &=~ SEC_RELOC;
10495 }
10496
10497 /* If the SEC_ALLOC flag is not set, force the section VMA to
10498 zero. This is done in elf_fake_sections as well, but forcing
10499 the VMA to 0 here will ensure that relocs against these
10500 sections are handled correctly. */
10501 if ((o->flags & SEC_ALLOC) == 0
10502 && ! o->user_set_vma)
10503 o->vma = 0;
10504 }
10505
10506 if (! info->relocatable && merged)
10507 elf_link_hash_traverse (elf_hash_table (info),
10508 _bfd_elf_link_sec_merge_syms, abfd);
10509
10510 /* Figure out the file positions for everything but the symbol table
10511 and the relocs. We set symcount to force assign_section_numbers
10512 to create a symbol table. */
10513 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10514 BFD_ASSERT (! abfd->output_has_begun);
10515 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10516 goto error_return;
10517
ee75fd95 10518 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10519 for (o = abfd->sections; o != NULL; o = o->next)
10520 {
d4730f92 10521 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
10522 if ((o->flags & SEC_RELOC) != 0)
10523 {
d4730f92
BS
10524 if (esdo->rel.hdr
10525 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
10526 goto error_return;
10527
d4730f92
BS
10528 if (esdo->rela.hdr
10529 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
10530 goto error_return;
10531 }
10532
10533 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10534 to count upwards while actually outputting the relocations. */
d4730f92
BS
10535 esdo->rel.count = 0;
10536 esdo->rela.count = 0;
c152c796
AM
10537 }
10538
10539 _bfd_elf_assign_file_positions_for_relocs (abfd);
10540
10541 /* We have now assigned file positions for all the sections except
10542 .symtab and .strtab. We start the .symtab section at the current
10543 file position, and write directly to it. We build the .strtab
10544 section in memory. */
10545 bfd_get_symcount (abfd) = 0;
10546 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10547 /* sh_name is set in prep_headers. */
10548 symtab_hdr->sh_type = SHT_SYMTAB;
10549 /* sh_flags, sh_addr and sh_size all start off zero. */
10550 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10551 /* sh_link is set in assign_section_numbers. */
10552 /* sh_info is set below. */
10553 /* sh_offset is set just below. */
72de5009 10554 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796
AM
10555
10556 off = elf_tdata (abfd)->next_file_pos;
10557 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10558
10559 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10560 incorrect. We do not yet know the size of the .symtab section.
10561 We correct next_file_pos below, after we do know the size. */
10562
10563 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10564 continuously seeking to the right position in the file. */
10565 if (! info->keep_memory || max_sym_count < 20)
10566 finfo.symbuf_size = 20;
10567 else
10568 finfo.symbuf_size = max_sym_count;
10569 amt = finfo.symbuf_size;
10570 amt *= bed->s->sizeof_sym;
a50b1753 10571 finfo.symbuf = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10572 if (finfo.symbuf == NULL)
10573 goto error_return;
4fbb74a6 10574 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10575 {
10576 /* Wild guess at number of output symbols. realloc'd as needed. */
10577 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10578 finfo.shndxbuf_size = amt;
10579 amt *= sizeof (Elf_External_Sym_Shndx);
a50b1753 10580 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
c152c796
AM
10581 if (finfo.symshndxbuf == NULL)
10582 goto error_return;
10583 }
10584
10585 /* Start writing out the symbol table. The first symbol is always a
10586 dummy symbol. */
10587 if (info->strip != strip_all
10588 || emit_relocs)
10589 {
10590 elfsym.st_value = 0;
10591 elfsym.st_size = 0;
10592 elfsym.st_info = 0;
10593 elfsym.st_other = 0;
10594 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 10595 elfsym.st_target_internal = 0;
6e0b88f1
AM
10596 if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10597 NULL) != 1)
c152c796
AM
10598 goto error_return;
10599 }
10600
c152c796
AM
10601 /* Output a symbol for each section. We output these even if we are
10602 discarding local symbols, since they are used for relocs. These
10603 symbols have no names. We store the index of each one in the
10604 index field of the section, so that we can find it again when
10605 outputting relocs. */
10606 if (info->strip != strip_all
10607 || emit_relocs)
10608 {
10609 elfsym.st_size = 0;
10610 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10611 elfsym.st_other = 0;
f0b5bb34 10612 elfsym.st_value = 0;
35fc36a8 10613 elfsym.st_target_internal = 0;
c152c796
AM
10614 for (i = 1; i < elf_numsections (abfd); i++)
10615 {
10616 o = bfd_section_from_elf_index (abfd, i);
10617 if (o != NULL)
f0b5bb34
AM
10618 {
10619 o->target_index = bfd_get_symcount (abfd);
10620 elfsym.st_shndx = i;
10621 if (!info->relocatable)
10622 elfsym.st_value = o->vma;
6e0b88f1 10623 if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1)
f0b5bb34
AM
10624 goto error_return;
10625 }
c152c796
AM
10626 }
10627 }
10628
10629 /* Allocate some memory to hold information read in from the input
10630 files. */
10631 if (max_contents_size != 0)
10632 {
a50b1753 10633 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
c152c796
AM
10634 if (finfo.contents == NULL)
10635 goto error_return;
10636 }
10637
10638 if (max_external_reloc_size != 0)
10639 {
10640 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10641 if (finfo.external_relocs == NULL)
10642 goto error_return;
10643 }
10644
10645 if (max_internal_reloc_count != 0)
10646 {
10647 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10648 amt *= sizeof (Elf_Internal_Rela);
a50b1753 10649 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
c152c796
AM
10650 if (finfo.internal_relocs == NULL)
10651 goto error_return;
10652 }
10653
10654 if (max_sym_count != 0)
10655 {
10656 amt = max_sym_count * bed->s->sizeof_sym;
a50b1753 10657 finfo.external_syms = (bfd_byte *) bfd_malloc (amt);
c152c796
AM
10658 if (finfo.external_syms == NULL)
10659 goto error_return;
10660
10661 amt = max_sym_count * sizeof (Elf_Internal_Sym);
a50b1753 10662 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
c152c796
AM
10663 if (finfo.internal_syms == NULL)
10664 goto error_return;
10665
10666 amt = max_sym_count * sizeof (long);
a50b1753 10667 finfo.indices = (long int *) bfd_malloc (amt);
c152c796
AM
10668 if (finfo.indices == NULL)
10669 goto error_return;
10670
10671 amt = max_sym_count * sizeof (asection *);
a50b1753 10672 finfo.sections = (asection **) bfd_malloc (amt);
c152c796
AM
10673 if (finfo.sections == NULL)
10674 goto error_return;
10675 }
10676
10677 if (max_sym_shndx_count != 0)
10678 {
10679 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
a50b1753 10680 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
c152c796
AM
10681 if (finfo.locsym_shndx == NULL)
10682 goto error_return;
10683 }
10684
10685 if (elf_hash_table (info)->tls_sec)
10686 {
10687 bfd_vma base, end = 0;
10688 asection *sec;
10689
10690 for (sec = elf_hash_table (info)->tls_sec;
10691 sec && (sec->flags & SEC_THREAD_LOCAL);
10692 sec = sec->next)
10693 {
3a800eb9 10694 bfd_size_type size = sec->size;
c152c796 10695
3a800eb9
AM
10696 if (size == 0
10697 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10698 {
91d6fa6a
NC
10699 struct bfd_link_order *ord = sec->map_tail.link_order;
10700
10701 if (ord != NULL)
10702 size = ord->offset + ord->size;
c152c796
AM
10703 }
10704 end = sec->vma + size;
10705 }
10706 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
10707 /* Only align end of TLS section if static TLS doesn't have special
10708 alignment requirements. */
10709 if (bed->static_tls_alignment == 1)
10710 end = align_power (end,
10711 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
10712 elf_hash_table (info)->tls_size = end - base;
10713 }
10714
0b52efa6
PB
10715 /* Reorder SHF_LINK_ORDER sections. */
10716 for (o = abfd->sections; o != NULL; o = o->next)
10717 {
10718 if (!elf_fixup_link_order (abfd, o))
10719 return FALSE;
10720 }
10721
c152c796
AM
10722 /* Since ELF permits relocations to be against local symbols, we
10723 must have the local symbols available when we do the relocations.
10724 Since we would rather only read the local symbols once, and we
10725 would rather not keep them in memory, we handle all the
10726 relocations for a single input file at the same time.
10727
10728 Unfortunately, there is no way to know the total number of local
10729 symbols until we have seen all of them, and the local symbol
10730 indices precede the global symbol indices. This means that when
10731 we are generating relocatable output, and we see a reloc against
10732 a global symbol, we can not know the symbol index until we have
10733 finished examining all the local symbols to see which ones we are
10734 going to output. To deal with this, we keep the relocations in
10735 memory, and don't output them until the end of the link. This is
10736 an unfortunate waste of memory, but I don't see a good way around
10737 it. Fortunately, it only happens when performing a relocatable
10738 link, which is not the common case. FIXME: If keep_memory is set
10739 we could write the relocs out and then read them again; I don't
10740 know how bad the memory loss will be. */
10741
10742 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10743 sub->output_has_begun = FALSE;
10744 for (o = abfd->sections; o != NULL; o = o->next)
10745 {
8423293d 10746 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10747 {
10748 if (p->type == bfd_indirect_link_order
10749 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10750 == bfd_target_elf_flavour)
10751 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10752 {
10753 if (! sub->output_has_begun)
10754 {
10755 if (! elf_link_input_bfd (&finfo, sub))
10756 goto error_return;
10757 sub->output_has_begun = TRUE;
10758 }
10759 }
10760 else if (p->type == bfd_section_reloc_link_order
10761 || p->type == bfd_symbol_reloc_link_order)
10762 {
10763 if (! elf_reloc_link_order (abfd, info, o, p))
10764 goto error_return;
10765 }
10766 else
10767 {
10768 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
10769 {
10770 if (p->type == bfd_indirect_link_order
10771 && (bfd_get_flavour (sub)
10772 == bfd_target_elf_flavour)
10773 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10774 != bed->s->elfclass))
10775 {
10776 const char *iclass, *oclass;
10777
10778 if (bed->s->elfclass == ELFCLASS64)
10779 {
10780 iclass = "ELFCLASS32";
10781 oclass = "ELFCLASS64";
10782 }
10783 else
10784 {
10785 iclass = "ELFCLASS64";
10786 oclass = "ELFCLASS32";
10787 }
10788
10789 bfd_set_error (bfd_error_wrong_format);
10790 (*_bfd_error_handler)
10791 (_("%B: file class %s incompatible with %s"),
10792 sub, iclass, oclass);
10793 }
10794
10795 goto error_return;
10796 }
c152c796
AM
10797 }
10798 }
10799 }
10800
c0f00686
L
10801 /* Free symbol buffer if needed. */
10802 if (!info->reduce_memory_overheads)
10803 {
10804 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10805 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10806 && elf_tdata (sub)->symbuf)
c0f00686
L
10807 {
10808 free (elf_tdata (sub)->symbuf);
10809 elf_tdata (sub)->symbuf = NULL;
10810 }
10811 }
10812
c152c796
AM
10813 /* Output any global symbols that got converted to local in a
10814 version script or due to symbol visibility. We do this in a
10815 separate step since ELF requires all local symbols to appear
10816 prior to any global symbols. FIXME: We should only do this if
10817 some global symbols were, in fact, converted to become local.
10818 FIXME: Will this work correctly with the Irix 5 linker? */
10819 eoinfo.failed = FALSE;
10820 eoinfo.finfo = &finfo;
10821 eoinfo.localsyms = TRUE;
10822 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10823 &eoinfo);
10824 if (eoinfo.failed)
10825 return FALSE;
10826
4e617b1e
PB
10827 /* If backend needs to output some local symbols not present in the hash
10828 table, do it now. */
10829 if (bed->elf_backend_output_arch_local_syms)
10830 {
6e0b88f1 10831 typedef int (*out_sym_func)
4e617b1e
PB
10832 (void *, const char *, Elf_Internal_Sym *, asection *,
10833 struct elf_link_hash_entry *);
10834
10835 if (! ((*bed->elf_backend_output_arch_local_syms)
10836 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10837 return FALSE;
10838 }
10839
c152c796
AM
10840 /* That wrote out all the local symbols. Finish up the symbol table
10841 with the global symbols. Even if we want to strip everything we
10842 can, we still need to deal with those global symbols that got
10843 converted to local in a version script. */
10844
10845 /* The sh_info field records the index of the first non local symbol. */
10846 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10847
10848 if (dynamic
10849 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10850 {
10851 Elf_Internal_Sym sym;
10852 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10853 long last_local = 0;
10854
10855 /* Write out the section symbols for the output sections. */
67687978 10856 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10857 {
10858 asection *s;
10859
10860 sym.st_size = 0;
10861 sym.st_name = 0;
10862 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10863 sym.st_other = 0;
35fc36a8 10864 sym.st_target_internal = 0;
c152c796
AM
10865
10866 for (s = abfd->sections; s != NULL; s = s->next)
10867 {
10868 int indx;
10869 bfd_byte *dest;
10870 long dynindx;
10871
c152c796 10872 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10873 if (dynindx <= 0)
10874 continue;
10875 indx = elf_section_data (s)->this_idx;
c152c796
AM
10876 BFD_ASSERT (indx > 0);
10877 sym.st_shndx = indx;
c0d5a53d
L
10878 if (! check_dynsym (abfd, &sym))
10879 return FALSE;
c152c796
AM
10880 sym.st_value = s->vma;
10881 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10882 if (last_local < dynindx)
10883 last_local = dynindx;
c152c796
AM
10884 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10885 }
c152c796
AM
10886 }
10887
10888 /* Write out the local dynsyms. */
10889 if (elf_hash_table (info)->dynlocal)
10890 {
10891 struct elf_link_local_dynamic_entry *e;
10892 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10893 {
10894 asection *s;
10895 bfd_byte *dest;
10896
935bd1e0 10897 /* Copy the internal symbol and turn off visibility.
c152c796
AM
10898 Note that we saved a word of storage and overwrote
10899 the original st_name with the dynstr_index. */
10900 sym = e->isym;
935bd1e0 10901 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 10902
cb33740c
AM
10903 s = bfd_section_from_elf_index (e->input_bfd,
10904 e->isym.st_shndx);
10905 if (s != NULL)
c152c796 10906 {
c152c796
AM
10907 sym.st_shndx =
10908 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10909 if (! check_dynsym (abfd, &sym))
10910 return FALSE;
c152c796
AM
10911 sym.st_value = (s->output_section->vma
10912 + s->output_offset
10913 + e->isym.st_value);
10914 }
10915
10916 if (last_local < e->dynindx)
10917 last_local = e->dynindx;
10918
10919 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10920 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10921 }
10922 }
10923
10924 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10925 last_local + 1;
10926 }
10927
10928 /* We get the global symbols from the hash table. */
10929 eoinfo.failed = FALSE;
10930 eoinfo.localsyms = FALSE;
10931 eoinfo.finfo = &finfo;
10932 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10933 &eoinfo);
10934 if (eoinfo.failed)
10935 return FALSE;
10936
10937 /* If backend needs to output some symbols not present in the hash
10938 table, do it now. */
10939 if (bed->elf_backend_output_arch_syms)
10940 {
6e0b88f1 10941 typedef int (*out_sym_func)
c152c796
AM
10942 (void *, const char *, Elf_Internal_Sym *, asection *,
10943 struct elf_link_hash_entry *);
10944
10945 if (! ((*bed->elf_backend_output_arch_syms)
10946 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10947 return FALSE;
10948 }
10949
10950 /* Flush all symbols to the file. */
10951 if (! elf_link_flush_output_syms (&finfo, bed))
10952 return FALSE;
10953
10954 /* Now we know the size of the symtab section. */
10955 off += symtab_hdr->sh_size;
10956
10957 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10958 if (symtab_shndx_hdr->sh_name != 0)
10959 {
10960 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10961 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10962 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10963 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10964 symtab_shndx_hdr->sh_size = amt;
10965
10966 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10967 off, TRUE);
10968
10969 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10970 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10971 return FALSE;
10972 }
10973
10974
10975 /* Finish up and write out the symbol string table (.strtab)
10976 section. */
10977 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10978 /* sh_name was set in prep_headers. */
10979 symstrtab_hdr->sh_type = SHT_STRTAB;
10980 symstrtab_hdr->sh_flags = 0;
10981 symstrtab_hdr->sh_addr = 0;
10982 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10983 symstrtab_hdr->sh_entsize = 0;
10984 symstrtab_hdr->sh_link = 0;
10985 symstrtab_hdr->sh_info = 0;
10986 /* sh_offset is set just below. */
10987 symstrtab_hdr->sh_addralign = 1;
10988
10989 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10990 elf_tdata (abfd)->next_file_pos = off;
10991
10992 if (bfd_get_symcount (abfd) > 0)
10993 {
10994 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10995 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10996 return FALSE;
10997 }
10998
10999 /* Adjust the relocs to have the correct symbol indices. */
11000 for (o = abfd->sections; o != NULL; o = o->next)
11001 {
d4730f92 11002 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11003 if ((o->flags & SEC_RELOC) == 0)
11004 continue;
11005
d4730f92
BS
11006 if (esdo->rel.hdr != NULL)
11007 elf_link_adjust_relocs (abfd, &esdo->rel);
11008 if (esdo->rela.hdr != NULL)
11009 elf_link_adjust_relocs (abfd, &esdo->rela);
c152c796
AM
11010
11011 /* Set the reloc_count field to 0 to prevent write_relocs from
11012 trying to swap the relocs out itself. */
11013 o->reloc_count = 0;
11014 }
11015
11016 if (dynamic && info->combreloc && dynobj != NULL)
11017 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11018
11019 /* If we are linking against a dynamic object, or generating a
11020 shared library, finish up the dynamic linking information. */
11021 if (dynamic)
11022 {
11023 bfd_byte *dyncon, *dynconend;
11024
11025 /* Fix up .dynamic entries. */
11026 o = bfd_get_section_by_name (dynobj, ".dynamic");
11027 BFD_ASSERT (o != NULL);
11028
11029 dyncon = o->contents;
eea6121a 11030 dynconend = o->contents + o->size;
c152c796
AM
11031 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11032 {
11033 Elf_Internal_Dyn dyn;
11034 const char *name;
11035 unsigned int type;
11036
11037 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11038
11039 switch (dyn.d_tag)
11040 {
11041 default:
11042 continue;
11043 case DT_NULL:
11044 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11045 {
11046 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11047 {
11048 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11049 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11050 default: continue;
11051 }
11052 dyn.d_un.d_val = relativecount;
11053 relativecount = 0;
11054 break;
11055 }
11056 continue;
11057
11058 case DT_INIT:
11059 name = info->init_function;
11060 goto get_sym;
11061 case DT_FINI:
11062 name = info->fini_function;
11063 get_sym:
11064 {
11065 struct elf_link_hash_entry *h;
11066
11067 h = elf_link_hash_lookup (elf_hash_table (info), name,
11068 FALSE, FALSE, TRUE);
11069 if (h != NULL
11070 && (h->root.type == bfd_link_hash_defined
11071 || h->root.type == bfd_link_hash_defweak))
11072 {
bef26483 11073 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11074 o = h->root.u.def.section;
11075 if (o->output_section != NULL)
bef26483 11076 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11077 + o->output_offset);
11078 else
11079 {
11080 /* The symbol is imported from another shared
11081 library and does not apply to this one. */
bef26483 11082 dyn.d_un.d_ptr = 0;
c152c796
AM
11083 }
11084 break;
11085 }
11086 }
11087 continue;
11088
11089 case DT_PREINIT_ARRAYSZ:
11090 name = ".preinit_array";
11091 goto get_size;
11092 case DT_INIT_ARRAYSZ:
11093 name = ".init_array";
11094 goto get_size;
11095 case DT_FINI_ARRAYSZ:
11096 name = ".fini_array";
11097 get_size:
11098 o = bfd_get_section_by_name (abfd, name);
11099 if (o == NULL)
11100 {
11101 (*_bfd_error_handler)
d003868e 11102 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11103 goto error_return;
11104 }
eea6121a 11105 if (o->size == 0)
c152c796
AM
11106 (*_bfd_error_handler)
11107 (_("warning: %s section has zero size"), name);
eea6121a 11108 dyn.d_un.d_val = o->size;
c152c796
AM
11109 break;
11110
11111 case DT_PREINIT_ARRAY:
11112 name = ".preinit_array";
11113 goto get_vma;
11114 case DT_INIT_ARRAY:
11115 name = ".init_array";
11116 goto get_vma;
11117 case DT_FINI_ARRAY:
11118 name = ".fini_array";
11119 goto get_vma;
11120
11121 case DT_HASH:
11122 name = ".hash";
11123 goto get_vma;
fdc90cb4
JJ
11124 case DT_GNU_HASH:
11125 name = ".gnu.hash";
11126 goto get_vma;
c152c796
AM
11127 case DT_STRTAB:
11128 name = ".dynstr";
11129 goto get_vma;
11130 case DT_SYMTAB:
11131 name = ".dynsym";
11132 goto get_vma;
11133 case DT_VERDEF:
11134 name = ".gnu.version_d";
11135 goto get_vma;
11136 case DT_VERNEED:
11137 name = ".gnu.version_r";
11138 goto get_vma;
11139 case DT_VERSYM:
11140 name = ".gnu.version";
11141 get_vma:
11142 o = bfd_get_section_by_name (abfd, name);
11143 if (o == NULL)
11144 {
11145 (*_bfd_error_handler)
d003868e 11146 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11147 goto error_return;
11148 }
11149 dyn.d_un.d_ptr = o->vma;
11150 break;
11151
11152 case DT_REL:
11153 case DT_RELA:
11154 case DT_RELSZ:
11155 case DT_RELASZ:
11156 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11157 type = SHT_REL;
11158 else
11159 type = SHT_RELA;
11160 dyn.d_un.d_val = 0;
bef26483 11161 dyn.d_un.d_ptr = 0;
c152c796
AM
11162 for (i = 1; i < elf_numsections (abfd); i++)
11163 {
11164 Elf_Internal_Shdr *hdr;
11165
11166 hdr = elf_elfsections (abfd)[i];
11167 if (hdr->sh_type == type
11168 && (hdr->sh_flags & SHF_ALLOC) != 0)
11169 {
11170 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11171 dyn.d_un.d_val += hdr->sh_size;
11172 else
11173 {
bef26483
AM
11174 if (dyn.d_un.d_ptr == 0
11175 || hdr->sh_addr < dyn.d_un.d_ptr)
11176 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11177 }
11178 }
11179 }
11180 break;
11181 }
11182 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11183 }
11184 }
11185
11186 /* If we have created any dynamic sections, then output them. */
11187 if (dynobj != NULL)
11188 {
11189 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11190 goto error_return;
11191
943284cc
DJ
11192 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11193 if (info->warn_shared_textrel && info->shared)
11194 {
11195 bfd_byte *dyncon, *dynconend;
11196
11197 /* Fix up .dynamic entries. */
11198 o = bfd_get_section_by_name (dynobj, ".dynamic");
11199 BFD_ASSERT (o != NULL);
11200
11201 dyncon = o->contents;
11202 dynconend = o->contents + o->size;
11203 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11204 {
11205 Elf_Internal_Dyn dyn;
11206
11207 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11208
11209 if (dyn.d_tag == DT_TEXTREL)
11210 {
a0c8462f 11211 info->callbacks->einfo
9267588c 11212 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11213 break;
11214 }
11215 }
11216 }
11217
c152c796
AM
11218 for (o = dynobj->sections; o != NULL; o = o->next)
11219 {
11220 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11221 || o->size == 0
c152c796
AM
11222 || o->output_section == bfd_abs_section_ptr)
11223 continue;
11224 if ((o->flags & SEC_LINKER_CREATED) == 0)
11225 {
11226 /* At this point, we are only interested in sections
11227 created by _bfd_elf_link_create_dynamic_sections. */
11228 continue;
11229 }
3722b82f
AM
11230 if (elf_hash_table (info)->stab_info.stabstr == o)
11231 continue;
eea6121a
AM
11232 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11233 continue;
c152c796
AM
11234 if ((elf_section_data (o->output_section)->this_hdr.sh_type
11235 != SHT_STRTAB)
11236 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
11237 {
5dabe785 11238 /* FIXME: octets_per_byte. */
c152c796
AM
11239 if (! bfd_set_section_contents (abfd, o->output_section,
11240 o->contents,
11241 (file_ptr) o->output_offset,
eea6121a 11242 o->size))
c152c796
AM
11243 goto error_return;
11244 }
11245 else
11246 {
11247 /* The contents of the .dynstr section are actually in a
11248 stringtab. */
11249 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11250 if (bfd_seek (abfd, off, SEEK_SET) != 0
11251 || ! _bfd_elf_strtab_emit (abfd,
11252 elf_hash_table (info)->dynstr))
11253 goto error_return;
11254 }
11255 }
11256 }
11257
11258 if (info->relocatable)
11259 {
11260 bfd_boolean failed = FALSE;
11261
11262 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11263 if (failed)
11264 goto error_return;
11265 }
11266
11267 /* If we have optimized stabs strings, output them. */
3722b82f 11268 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11269 {
11270 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11271 goto error_return;
11272 }
11273
11274 if (info->eh_frame_hdr)
11275 {
11276 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11277 goto error_return;
11278 }
11279
11280 if (finfo.symstrtab != NULL)
11281 _bfd_stringtab_free (finfo.symstrtab);
11282 if (finfo.contents != NULL)
11283 free (finfo.contents);
11284 if (finfo.external_relocs != NULL)
11285 free (finfo.external_relocs);
11286 if (finfo.internal_relocs != NULL)
11287 free (finfo.internal_relocs);
11288 if (finfo.external_syms != NULL)
11289 free (finfo.external_syms);
11290 if (finfo.locsym_shndx != NULL)
11291 free (finfo.locsym_shndx);
11292 if (finfo.internal_syms != NULL)
11293 free (finfo.internal_syms);
11294 if (finfo.indices != NULL)
11295 free (finfo.indices);
11296 if (finfo.sections != NULL)
11297 free (finfo.sections);
11298 if (finfo.symbuf != NULL)
11299 free (finfo.symbuf);
11300 if (finfo.symshndxbuf != NULL)
11301 free (finfo.symshndxbuf);
11302 for (o = abfd->sections; o != NULL; o = o->next)
11303 {
d4730f92
BS
11304 struct bfd_elf_section_data *esdo = elf_section_data (o);
11305 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11306 free (esdo->rel.hashes);
11307 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11308 free (esdo->rela.hashes);
c152c796
AM
11309 }
11310
11311 elf_tdata (abfd)->linker = TRUE;
11312
104d59d1
JM
11313 if (attr_section)
11314 {
a50b1753 11315 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11316 if (contents == NULL)
d0f16d5e 11317 return FALSE; /* Bail out and fail. */
104d59d1
JM
11318 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11319 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11320 free (contents);
11321 }
11322
c152c796
AM
11323 return TRUE;
11324
11325 error_return:
11326 if (finfo.symstrtab != NULL)
11327 _bfd_stringtab_free (finfo.symstrtab);
11328 if (finfo.contents != NULL)
11329 free (finfo.contents);
11330 if (finfo.external_relocs != NULL)
11331 free (finfo.external_relocs);
11332 if (finfo.internal_relocs != NULL)
11333 free (finfo.internal_relocs);
11334 if (finfo.external_syms != NULL)
11335 free (finfo.external_syms);
11336 if (finfo.locsym_shndx != NULL)
11337 free (finfo.locsym_shndx);
11338 if (finfo.internal_syms != NULL)
11339 free (finfo.internal_syms);
11340 if (finfo.indices != NULL)
11341 free (finfo.indices);
11342 if (finfo.sections != NULL)
11343 free (finfo.sections);
11344 if (finfo.symbuf != NULL)
11345 free (finfo.symbuf);
11346 if (finfo.symshndxbuf != NULL)
11347 free (finfo.symshndxbuf);
11348 for (o = abfd->sections; o != NULL; o = o->next)
11349 {
d4730f92
BS
11350 struct bfd_elf_section_data *esdo = elf_section_data (o);
11351 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11352 free (esdo->rel.hashes);
11353 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11354 free (esdo->rela.hashes);
c152c796
AM
11355 }
11356
11357 return FALSE;
11358}
11359\f
5241d853
RS
11360/* Initialize COOKIE for input bfd ABFD. */
11361
11362static bfd_boolean
11363init_reloc_cookie (struct elf_reloc_cookie *cookie,
11364 struct bfd_link_info *info, bfd *abfd)
11365{
11366 Elf_Internal_Shdr *symtab_hdr;
11367 const struct elf_backend_data *bed;
11368
11369 bed = get_elf_backend_data (abfd);
11370 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11371
11372 cookie->abfd = abfd;
11373 cookie->sym_hashes = elf_sym_hashes (abfd);
11374 cookie->bad_symtab = elf_bad_symtab (abfd);
11375 if (cookie->bad_symtab)
11376 {
11377 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11378 cookie->extsymoff = 0;
11379 }
11380 else
11381 {
11382 cookie->locsymcount = symtab_hdr->sh_info;
11383 cookie->extsymoff = symtab_hdr->sh_info;
11384 }
11385
11386 if (bed->s->arch_size == 32)
11387 cookie->r_sym_shift = 8;
11388 else
11389 cookie->r_sym_shift = 32;
11390
11391 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11392 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11393 {
11394 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11395 cookie->locsymcount, 0,
11396 NULL, NULL, NULL);
11397 if (cookie->locsyms == NULL)
11398 {
11399 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11400 return FALSE;
11401 }
11402 if (info->keep_memory)
11403 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11404 }
11405 return TRUE;
11406}
11407
11408/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11409
11410static void
11411fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11412{
11413 Elf_Internal_Shdr *symtab_hdr;
11414
11415 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11416 if (cookie->locsyms != NULL
11417 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11418 free (cookie->locsyms);
11419}
11420
11421/* Initialize the relocation information in COOKIE for input section SEC
11422 of input bfd ABFD. */
11423
11424static bfd_boolean
11425init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11426 struct bfd_link_info *info, bfd *abfd,
11427 asection *sec)
11428{
11429 const struct elf_backend_data *bed;
11430
11431 if (sec->reloc_count == 0)
11432 {
11433 cookie->rels = NULL;
11434 cookie->relend = NULL;
11435 }
11436 else
11437 {
11438 bed = get_elf_backend_data (abfd);
11439
11440 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11441 info->keep_memory);
11442 if (cookie->rels == NULL)
11443 return FALSE;
11444 cookie->rel = cookie->rels;
11445 cookie->relend = (cookie->rels
11446 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11447 }
11448 cookie->rel = cookie->rels;
11449 return TRUE;
11450}
11451
11452/* Free the memory allocated by init_reloc_cookie_rels,
11453 if appropriate. */
11454
11455static void
11456fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11457 asection *sec)
11458{
11459 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11460 free (cookie->rels);
11461}
11462
11463/* Initialize the whole of COOKIE for input section SEC. */
11464
11465static bfd_boolean
11466init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11467 struct bfd_link_info *info,
11468 asection *sec)
11469{
11470 if (!init_reloc_cookie (cookie, info, sec->owner))
11471 goto error1;
11472 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11473 goto error2;
11474 return TRUE;
11475
11476 error2:
11477 fini_reloc_cookie (cookie, sec->owner);
11478 error1:
11479 return FALSE;
11480}
11481
11482/* Free the memory allocated by init_reloc_cookie_for_section,
11483 if appropriate. */
11484
11485static void
11486fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11487 asection *sec)
11488{
11489 fini_reloc_cookie_rels (cookie, sec);
11490 fini_reloc_cookie (cookie, sec->owner);
11491}
11492\f
c152c796
AM
11493/* Garbage collect unused sections. */
11494
07adf181
AM
11495/* Default gc_mark_hook. */
11496
11497asection *
11498_bfd_elf_gc_mark_hook (asection *sec,
11499 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11500 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11501 struct elf_link_hash_entry *h,
11502 Elf_Internal_Sym *sym)
11503{
bde6f3eb
L
11504 const char *sec_name;
11505
07adf181
AM
11506 if (h != NULL)
11507 {
11508 switch (h->root.type)
11509 {
11510 case bfd_link_hash_defined:
11511 case bfd_link_hash_defweak:
11512 return h->root.u.def.section;
11513
11514 case bfd_link_hash_common:
11515 return h->root.u.c.p->section;
11516
bde6f3eb
L
11517 case bfd_link_hash_undefined:
11518 case bfd_link_hash_undefweak:
11519 /* To work around a glibc bug, keep all XXX input sections
11520 when there is an as yet undefined reference to __start_XXX
11521 or __stop_XXX symbols. The linker will later define such
11522 symbols for orphan input sections that have a name
11523 representable as a C identifier. */
11524 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11525 sec_name = h->root.root.string + 8;
11526 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11527 sec_name = h->root.root.string + 7;
11528 else
11529 sec_name = NULL;
11530
11531 if (sec_name && *sec_name != '\0')
11532 {
11533 bfd *i;
11534
11535 for (i = info->input_bfds; i; i = i->link_next)
11536 {
11537 sec = bfd_get_section_by_name (i, sec_name);
11538 if (sec)
11539 sec->flags |= SEC_KEEP;
11540 }
11541 }
11542 break;
11543
07adf181
AM
11544 default:
11545 break;
11546 }
11547 }
11548 else
11549 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11550
11551 return NULL;
11552}
11553
5241d853
RS
11554/* COOKIE->rel describes a relocation against section SEC, which is
11555 a section we've decided to keep. Return the section that contains
11556 the relocation symbol, or NULL if no section contains it. */
11557
11558asection *
11559_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11560 elf_gc_mark_hook_fn gc_mark_hook,
11561 struct elf_reloc_cookie *cookie)
11562{
11563 unsigned long r_symndx;
11564 struct elf_link_hash_entry *h;
11565
11566 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 11567 if (r_symndx == STN_UNDEF)
5241d853
RS
11568 return NULL;
11569
11570 if (r_symndx >= cookie->locsymcount
11571 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11572 {
11573 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11574 while (h->root.type == bfd_link_hash_indirect
11575 || h->root.type == bfd_link_hash_warning)
11576 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11577 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11578 }
11579
11580 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11581 &cookie->locsyms[r_symndx]);
11582}
11583
11584/* COOKIE->rel describes a relocation against section SEC, which is
11585 a section we've decided to keep. Mark the section that contains
9d0a14d3 11586 the relocation symbol. */
5241d853
RS
11587
11588bfd_boolean
11589_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11590 asection *sec,
11591 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11592 struct elf_reloc_cookie *cookie)
5241d853
RS
11593{
11594 asection *rsec;
11595
11596 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11597 if (rsec && !rsec->gc_mark)
11598 {
11599 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11600 rsec->gc_mark = 1;
5241d853
RS
11601 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11602 return FALSE;
11603 }
11604 return TRUE;
11605}
11606
07adf181
AM
11607/* The mark phase of garbage collection. For a given section, mark
11608 it and any sections in this section's group, and all the sections
11609 which define symbols to which it refers. */
11610
ccfa59ea
AM
11611bfd_boolean
11612_bfd_elf_gc_mark (struct bfd_link_info *info,
11613 asection *sec,
6a5bb875 11614 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11615{
11616 bfd_boolean ret;
9d0a14d3 11617 asection *group_sec, *eh_frame;
c152c796
AM
11618
11619 sec->gc_mark = 1;
11620
11621 /* Mark all the sections in the group. */
11622 group_sec = elf_section_data (sec)->next_in_group;
11623 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11624 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11625 return FALSE;
11626
11627 /* Look through the section relocs. */
11628 ret = TRUE;
9d0a14d3
RS
11629 eh_frame = elf_eh_frame_section (sec->owner);
11630 if ((sec->flags & SEC_RELOC) != 0
11631 && sec->reloc_count > 0
11632 && sec != eh_frame)
c152c796 11633 {
5241d853 11634 struct elf_reloc_cookie cookie;
c152c796 11635
5241d853
RS
11636 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11637 ret = FALSE;
c152c796 11638 else
c152c796 11639 {
5241d853 11640 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11641 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11642 {
11643 ret = FALSE;
11644 break;
11645 }
11646 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11647 }
11648 }
9d0a14d3
RS
11649
11650 if (ret && eh_frame && elf_fde_list (sec))
11651 {
11652 struct elf_reloc_cookie cookie;
11653
11654 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11655 ret = FALSE;
11656 else
11657 {
11658 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11659 gc_mark_hook, &cookie))
11660 ret = FALSE;
11661 fini_reloc_cookie_for_section (&cookie, eh_frame);
11662 }
11663 }
11664
c152c796
AM
11665 return ret;
11666}
11667
11668/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11669
c17d87de
NC
11670struct elf_gc_sweep_symbol_info
11671{
ccabcbe5
AM
11672 struct bfd_link_info *info;
11673 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11674 bfd_boolean);
11675};
11676
c152c796 11677static bfd_boolean
ccabcbe5 11678elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11679{
c152c796
AM
11680 if (h->root.type == bfd_link_hash_warning)
11681 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11682
ccabcbe5
AM
11683 if ((h->root.type == bfd_link_hash_defined
11684 || h->root.type == bfd_link_hash_defweak)
11685 && !h->root.u.def.section->gc_mark
11686 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11687 {
a50b1753
NC
11688 struct elf_gc_sweep_symbol_info *inf =
11689 (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5
AM
11690 (*inf->hide_symbol) (inf->info, h, TRUE);
11691 }
c152c796
AM
11692
11693 return TRUE;
11694}
11695
11696/* The sweep phase of garbage collection. Remove all garbage sections. */
11697
11698typedef bfd_boolean (*gc_sweep_hook_fn)
11699 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11700
11701static bfd_boolean
ccabcbe5 11702elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11703{
11704 bfd *sub;
ccabcbe5
AM
11705 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11706 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11707 unsigned long section_sym_count;
11708 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11709
11710 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11711 {
11712 asection *o;
11713
11714 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11715 continue;
11716
11717 for (o = sub->sections; o != NULL; o = o->next)
11718 {
a33dafc3
L
11719 /* When any section in a section group is kept, we keep all
11720 sections in the section group. If the first member of
11721 the section group is excluded, we will also exclude the
11722 group section. */
11723 if (o->flags & SEC_GROUP)
11724 {
11725 asection *first = elf_next_in_group (o);
11726 o->gc_mark = first->gc_mark;
11727 }
11728 else if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
16583161
L
11729 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0
11730 || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE)
a33dafc3 11731 {
16583161 11732 /* Keep debug, special and SHT_NOTE sections. */
a33dafc3
L
11733 o->gc_mark = 1;
11734 }
c152c796
AM
11735
11736 if (o->gc_mark)
11737 continue;
11738
11739 /* Skip sweeping sections already excluded. */
11740 if (o->flags & SEC_EXCLUDE)
11741 continue;
11742
11743 /* Since this is early in the link process, it is simple
11744 to remove a section from the output. */
11745 o->flags |= SEC_EXCLUDE;
11746
c55fe096 11747 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11748 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11749
c152c796
AM
11750 /* But we also have to update some of the relocation
11751 info we collected before. */
11752 if (gc_sweep_hook
e8aaee2a
AM
11753 && (o->flags & SEC_RELOC) != 0
11754 && o->reloc_count > 0
11755 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11756 {
11757 Elf_Internal_Rela *internal_relocs;
11758 bfd_boolean r;
11759
11760 internal_relocs
11761 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11762 info->keep_memory);
11763 if (internal_relocs == NULL)
11764 return FALSE;
11765
11766 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11767
11768 if (elf_section_data (o)->relocs != internal_relocs)
11769 free (internal_relocs);
11770
11771 if (!r)
11772 return FALSE;
11773 }
11774 }
11775 }
11776
11777 /* Remove the symbols that were in the swept sections from the dynamic
11778 symbol table. GCFIXME: Anyone know how to get them out of the
11779 static symbol table as well? */
ccabcbe5
AM
11780 sweep_info.info = info;
11781 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11782 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11783 &sweep_info);
c152c796 11784
ccabcbe5 11785 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11786 return TRUE;
11787}
11788
11789/* Propagate collected vtable information. This is called through
11790 elf_link_hash_traverse. */
11791
11792static bfd_boolean
11793elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11794{
11795 if (h->root.type == bfd_link_hash_warning)
11796 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11797
11798 /* Those that are not vtables. */
f6e332e6 11799 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11800 return TRUE;
11801
11802 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11803 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11804 return TRUE;
11805
11806 /* If we've already been done, exit. */
f6e332e6 11807 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11808 return TRUE;
11809
11810 /* Make sure the parent's table is up to date. */
f6e332e6 11811 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11812
f6e332e6 11813 if (h->vtable->used == NULL)
c152c796
AM
11814 {
11815 /* None of this table's entries were referenced. Re-use the
11816 parent's table. */
f6e332e6
AM
11817 h->vtable->used = h->vtable->parent->vtable->used;
11818 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11819 }
11820 else
11821 {
11822 size_t n;
11823 bfd_boolean *cu, *pu;
11824
11825 /* Or the parent's entries into ours. */
f6e332e6 11826 cu = h->vtable->used;
c152c796 11827 cu[-1] = TRUE;
f6e332e6 11828 pu = h->vtable->parent->vtable->used;
c152c796
AM
11829 if (pu != NULL)
11830 {
11831 const struct elf_backend_data *bed;
11832 unsigned int log_file_align;
11833
11834 bed = get_elf_backend_data (h->root.u.def.section->owner);
11835 log_file_align = bed->s->log_file_align;
f6e332e6 11836 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11837 while (n--)
11838 {
11839 if (*pu)
11840 *cu = TRUE;
11841 pu++;
11842 cu++;
11843 }
11844 }
11845 }
11846
11847 return TRUE;
11848}
11849
11850static bfd_boolean
11851elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11852{
11853 asection *sec;
11854 bfd_vma hstart, hend;
11855 Elf_Internal_Rela *relstart, *relend, *rel;
11856 const struct elf_backend_data *bed;
11857 unsigned int log_file_align;
11858
11859 if (h->root.type == bfd_link_hash_warning)
11860 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11861
11862 /* Take care of both those symbols that do not describe vtables as
11863 well as those that are not loaded. */
f6e332e6 11864 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11865 return TRUE;
11866
11867 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11868 || h->root.type == bfd_link_hash_defweak);
11869
11870 sec = h->root.u.def.section;
11871 hstart = h->root.u.def.value;
11872 hend = hstart + h->size;
11873
11874 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11875 if (!relstart)
11876 return *(bfd_boolean *) okp = FALSE;
11877 bed = get_elf_backend_data (sec->owner);
11878 log_file_align = bed->s->log_file_align;
11879
11880 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11881
11882 for (rel = relstart; rel < relend; ++rel)
11883 if (rel->r_offset >= hstart && rel->r_offset < hend)
11884 {
11885 /* If the entry is in use, do nothing. */
f6e332e6
AM
11886 if (h->vtable->used
11887 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11888 {
11889 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11890 if (h->vtable->used[entry])
c152c796
AM
11891 continue;
11892 }
11893 /* Otherwise, kill it. */
11894 rel->r_offset = rel->r_info = rel->r_addend = 0;
11895 }
11896
11897 return TRUE;
11898}
11899
87538722
AM
11900/* Mark sections containing dynamically referenced symbols. When
11901 building shared libraries, we must assume that any visible symbol is
11902 referenced. */
715df9b8 11903
64d03ab5
AM
11904bfd_boolean
11905bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 11906{
87538722
AM
11907 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11908
715df9b8
EB
11909 if (h->root.type == bfd_link_hash_warning)
11910 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11911
11912 if ((h->root.type == bfd_link_hash_defined
11913 || h->root.type == bfd_link_hash_defweak)
87538722 11914 && (h->ref_dynamic
5adcfd8b 11915 || (!info->executable
87538722
AM
11916 && h->def_regular
11917 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11918 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
715df9b8
EB
11919 h->root.u.def.section->flags |= SEC_KEEP;
11920
11921 return TRUE;
11922}
3b36f7e6 11923
74f0fb50
AM
11924/* Keep all sections containing symbols undefined on the command-line,
11925 and the section containing the entry symbol. */
11926
11927void
11928_bfd_elf_gc_keep (struct bfd_link_info *info)
11929{
11930 struct bfd_sym_chain *sym;
11931
11932 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11933 {
11934 struct elf_link_hash_entry *h;
11935
11936 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11937 FALSE, FALSE, FALSE);
11938
11939 if (h != NULL
11940 && (h->root.type == bfd_link_hash_defined
11941 || h->root.type == bfd_link_hash_defweak)
11942 && !bfd_is_abs_section (h->root.u.def.section))
11943 h->root.u.def.section->flags |= SEC_KEEP;
11944 }
11945}
11946
c152c796
AM
11947/* Do mark and sweep of unused sections. */
11948
11949bfd_boolean
11950bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11951{
11952 bfd_boolean ok = TRUE;
11953 bfd *sub;
6a5bb875 11954 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 11955 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 11956
64d03ab5 11957 if (!bed->can_gc_sections
715df9b8 11958 || !is_elf_hash_table (info->hash))
c152c796
AM
11959 {
11960 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11961 return TRUE;
11962 }
11963
74f0fb50
AM
11964 bed->gc_keep (info);
11965
9d0a14d3
RS
11966 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11967 at the .eh_frame section if we can mark the FDEs individually. */
11968 _bfd_elf_begin_eh_frame_parsing (info);
11969 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11970 {
11971 asection *sec;
11972 struct elf_reloc_cookie cookie;
11973
11974 sec = bfd_get_section_by_name (sub, ".eh_frame");
11975 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11976 {
11977 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11978 if (elf_section_data (sec)->sec_info)
11979 elf_eh_frame_section (sub) = sec;
11980 fini_reloc_cookie_for_section (&cookie, sec);
11981 }
11982 }
11983 _bfd_elf_end_eh_frame_parsing (info);
11984
c152c796
AM
11985 /* Apply transitive closure to the vtable entry usage info. */
11986 elf_link_hash_traverse (elf_hash_table (info),
11987 elf_gc_propagate_vtable_entries_used,
11988 &ok);
11989 if (!ok)
11990 return FALSE;
11991
11992 /* Kill the vtable relocations that were not used. */
11993 elf_link_hash_traverse (elf_hash_table (info),
11994 elf_gc_smash_unused_vtentry_relocs,
11995 &ok);
11996 if (!ok)
11997 return FALSE;
11998
715df9b8
EB
11999 /* Mark dynamically referenced symbols. */
12000 if (elf_hash_table (info)->dynamic_sections_created)
12001 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 12002 bed->gc_mark_dynamic_ref,
87538722 12003 info);
c152c796 12004
715df9b8 12005 /* Grovel through relocs to find out who stays ... */
64d03ab5 12006 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
12007 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12008 {
12009 asection *o;
12010
12011 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12012 continue;
12013
12014 for (o = sub->sections; o != NULL; o = o->next)
a14a5de3 12015 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
39c2f51b
AM
12016 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12017 return FALSE;
c152c796
AM
12018 }
12019
6a5bb875
PB
12020 /* Allow the backend to mark additional target specific sections. */
12021 if (bed->gc_mark_extra_sections)
74f0fb50 12022 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12023
c152c796 12024 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12025 return elf_gc_sweep (abfd, info);
c152c796
AM
12026}
12027\f
12028/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12029
12030bfd_boolean
12031bfd_elf_gc_record_vtinherit (bfd *abfd,
12032 asection *sec,
12033 struct elf_link_hash_entry *h,
12034 bfd_vma offset)
12035{
12036 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12037 struct elf_link_hash_entry **search, *child;
12038 bfd_size_type extsymcount;
12039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12040
12041 /* The sh_info field of the symtab header tells us where the
12042 external symbols start. We don't care about the local symbols at
12043 this point. */
12044 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12045 if (!elf_bad_symtab (abfd))
12046 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12047
12048 sym_hashes = elf_sym_hashes (abfd);
12049 sym_hashes_end = sym_hashes + extsymcount;
12050
12051 /* Hunt down the child symbol, which is in this section at the same
12052 offset as the relocation. */
12053 for (search = sym_hashes; search != sym_hashes_end; ++search)
12054 {
12055 if ((child = *search) != NULL
12056 && (child->root.type == bfd_link_hash_defined
12057 || child->root.type == bfd_link_hash_defweak)
12058 && child->root.u.def.section == sec
12059 && child->root.u.def.value == offset)
12060 goto win;
12061 }
12062
d003868e
AM
12063 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12064 abfd, sec, (unsigned long) offset);
c152c796
AM
12065 bfd_set_error (bfd_error_invalid_operation);
12066 return FALSE;
12067
12068 win:
f6e332e6
AM
12069 if (!child->vtable)
12070 {
a50b1753
NC
12071 child->vtable = (struct elf_link_virtual_table_entry *)
12072 bfd_zalloc (abfd, sizeof (*child->vtable));
f6e332e6
AM
12073 if (!child->vtable)
12074 return FALSE;
12075 }
c152c796
AM
12076 if (!h)
12077 {
12078 /* This *should* only be the absolute section. It could potentially
12079 be that someone has defined a non-global vtable though, which
12080 would be bad. It isn't worth paging in the local symbols to be
12081 sure though; that case should simply be handled by the assembler. */
12082
f6e332e6 12083 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12084 }
12085 else
f6e332e6 12086 child->vtable->parent = h;
c152c796
AM
12087
12088 return TRUE;
12089}
12090
12091/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12092
12093bfd_boolean
12094bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12095 asection *sec ATTRIBUTE_UNUSED,
12096 struct elf_link_hash_entry *h,
12097 bfd_vma addend)
12098{
12099 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12100 unsigned int log_file_align = bed->s->log_file_align;
12101
f6e332e6
AM
12102 if (!h->vtable)
12103 {
a50b1753
NC
12104 h->vtable = (struct elf_link_virtual_table_entry *)
12105 bfd_zalloc (abfd, sizeof (*h->vtable));
f6e332e6
AM
12106 if (!h->vtable)
12107 return FALSE;
12108 }
12109
12110 if (addend >= h->vtable->size)
c152c796
AM
12111 {
12112 size_t size, bytes, file_align;
f6e332e6 12113 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12114
12115 /* While the symbol is undefined, we have to be prepared to handle
12116 a zero size. */
12117 file_align = 1 << log_file_align;
12118 if (h->root.type == bfd_link_hash_undefined)
12119 size = addend + file_align;
12120 else
12121 {
12122 size = h->size;
12123 if (addend >= size)
12124 {
12125 /* Oops! We've got a reference past the defined end of
12126 the table. This is probably a bug -- shall we warn? */
12127 size = addend + file_align;
12128 }
12129 }
12130 size = (size + file_align - 1) & -file_align;
12131
12132 /* Allocate one extra entry for use as a "done" flag for the
12133 consolidation pass. */
12134 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12135
12136 if (ptr)
12137 {
a50b1753 12138 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12139
12140 if (ptr != NULL)
12141 {
12142 size_t oldbytes;
12143
f6e332e6 12144 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12145 * sizeof (bfd_boolean));
12146 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12147 }
12148 }
12149 else
a50b1753 12150 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12151
12152 if (ptr == NULL)
12153 return FALSE;
12154
12155 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12156 h->vtable->used = ptr + 1;
12157 h->vtable->size = size;
c152c796
AM
12158 }
12159
f6e332e6 12160 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12161
12162 return TRUE;
12163}
12164
12165struct alloc_got_off_arg {
12166 bfd_vma gotoff;
10455f89 12167 struct bfd_link_info *info;
c152c796
AM
12168};
12169
12170/* We need a special top-level link routine to convert got reference counts
12171 to real got offsets. */
12172
12173static bfd_boolean
12174elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12175{
a50b1753 12176 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
12177 bfd *obfd = gofarg->info->output_bfd;
12178 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796
AM
12179
12180 if (h->root.type == bfd_link_hash_warning)
12181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12182
12183 if (h->got.refcount > 0)
12184 {
12185 h->got.offset = gofarg->gotoff;
10455f89 12186 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
12187 }
12188 else
12189 h->got.offset = (bfd_vma) -1;
12190
12191 return TRUE;
12192}
12193
12194/* And an accompanying bit to work out final got entry offsets once
12195 we're done. Should be called from final_link. */
12196
12197bfd_boolean
12198bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12199 struct bfd_link_info *info)
12200{
12201 bfd *i;
12202 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12203 bfd_vma gotoff;
c152c796
AM
12204 struct alloc_got_off_arg gofarg;
12205
10455f89
HPN
12206 BFD_ASSERT (abfd == info->output_bfd);
12207
c152c796
AM
12208 if (! is_elf_hash_table (info->hash))
12209 return FALSE;
12210
12211 /* The GOT offset is relative to the .got section, but the GOT header is
12212 put into the .got.plt section, if the backend uses it. */
12213 if (bed->want_got_plt)
12214 gotoff = 0;
12215 else
12216 gotoff = bed->got_header_size;
12217
12218 /* Do the local .got entries first. */
12219 for (i = info->input_bfds; i; i = i->link_next)
12220 {
12221 bfd_signed_vma *local_got;
12222 bfd_size_type j, locsymcount;
12223 Elf_Internal_Shdr *symtab_hdr;
12224
12225 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12226 continue;
12227
12228 local_got = elf_local_got_refcounts (i);
12229 if (!local_got)
12230 continue;
12231
12232 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12233 if (elf_bad_symtab (i))
12234 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12235 else
12236 locsymcount = symtab_hdr->sh_info;
12237
12238 for (j = 0; j < locsymcount; ++j)
12239 {
12240 if (local_got[j] > 0)
12241 {
12242 local_got[j] = gotoff;
10455f89 12243 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
12244 }
12245 else
12246 local_got[j] = (bfd_vma) -1;
12247 }
12248 }
12249
12250 /* Then the global .got entries. .plt refcounts are handled by
12251 adjust_dynamic_symbol */
12252 gofarg.gotoff = gotoff;
10455f89 12253 gofarg.info = info;
c152c796
AM
12254 elf_link_hash_traverse (elf_hash_table (info),
12255 elf_gc_allocate_got_offsets,
12256 &gofarg);
12257 return TRUE;
12258}
12259
12260/* Many folk need no more in the way of final link than this, once
12261 got entry reference counting is enabled. */
12262
12263bfd_boolean
12264bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12265{
12266 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12267 return FALSE;
12268
12269 /* Invoke the regular ELF backend linker to do all the work. */
12270 return bfd_elf_final_link (abfd, info);
12271}
12272
12273bfd_boolean
12274bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12275{
a50b1753 12276 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
12277
12278 if (rcookie->bad_symtab)
12279 rcookie->rel = rcookie->rels;
12280
12281 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12282 {
12283 unsigned long r_symndx;
12284
12285 if (! rcookie->bad_symtab)
12286 if (rcookie->rel->r_offset > offset)
12287 return FALSE;
12288 if (rcookie->rel->r_offset != offset)
12289 continue;
12290
12291 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 12292 if (r_symndx == STN_UNDEF)
c152c796
AM
12293 return TRUE;
12294
12295 if (r_symndx >= rcookie->locsymcount
12296 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12297 {
12298 struct elf_link_hash_entry *h;
12299
12300 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12301
12302 while (h->root.type == bfd_link_hash_indirect
12303 || h->root.type == bfd_link_hash_warning)
12304 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12305
12306 if ((h->root.type == bfd_link_hash_defined
12307 || h->root.type == bfd_link_hash_defweak)
12308 && elf_discarded_section (h->root.u.def.section))
12309 return TRUE;
12310 else
12311 return FALSE;
12312 }
12313 else
12314 {
12315 /* It's not a relocation against a global symbol,
12316 but it could be a relocation against a local
12317 symbol for a discarded section. */
12318 asection *isec;
12319 Elf_Internal_Sym *isym;
12320
12321 /* Need to: get the symbol; get the section. */
12322 isym = &rcookie->locsyms[r_symndx];
cb33740c
AM
12323 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12324 if (isec != NULL && elf_discarded_section (isec))
12325 return TRUE;
c152c796
AM
12326 }
12327 return FALSE;
12328 }
12329 return FALSE;
12330}
12331
12332/* Discard unneeded references to discarded sections.
12333 Returns TRUE if any section's size was changed. */
12334/* This function assumes that the relocations are in sorted order,
12335 which is true for all known assemblers. */
12336
12337bfd_boolean
12338bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12339{
12340 struct elf_reloc_cookie cookie;
12341 asection *stab, *eh;
c152c796
AM
12342 const struct elf_backend_data *bed;
12343 bfd *abfd;
c152c796
AM
12344 bfd_boolean ret = FALSE;
12345
12346 if (info->traditional_format
12347 || !is_elf_hash_table (info->hash))
12348 return FALSE;
12349
ca92cecb 12350 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
12351 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12352 {
12353 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12354 continue;
12355
12356 bed = get_elf_backend_data (abfd);
12357
12358 if ((abfd->flags & DYNAMIC) != 0)
12359 continue;
12360
8da3dbc5
AM
12361 eh = NULL;
12362 if (!info->relocatable)
12363 {
12364 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12365 if (eh != NULL
eea6121a 12366 && (eh->size == 0
8da3dbc5
AM
12367 || bfd_is_abs_section (eh->output_section)))
12368 eh = NULL;
12369 }
c152c796
AM
12370
12371 stab = bfd_get_section_by_name (abfd, ".stab");
12372 if (stab != NULL
eea6121a 12373 && (stab->size == 0
c152c796
AM
12374 || bfd_is_abs_section (stab->output_section)
12375 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12376 stab = NULL;
12377
12378 if (stab == NULL
12379 && eh == NULL
12380 && bed->elf_backend_discard_info == NULL)
12381 continue;
12382
5241d853
RS
12383 if (!init_reloc_cookie (&cookie, info, abfd))
12384 return FALSE;
c152c796 12385
5241d853
RS
12386 if (stab != NULL
12387 && stab->reloc_count > 0
12388 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 12389 {
5241d853
RS
12390 if (_bfd_discard_section_stabs (abfd, stab,
12391 elf_section_data (stab)->sec_info,
12392 bfd_elf_reloc_symbol_deleted_p,
12393 &cookie))
12394 ret = TRUE;
12395 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
12396 }
12397
5241d853
RS
12398 if (eh != NULL
12399 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 12400 {
ca92cecb 12401 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
12402 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12403 bfd_elf_reloc_symbol_deleted_p,
12404 &cookie))
12405 ret = TRUE;
5241d853 12406 fini_reloc_cookie_rels (&cookie, eh);
c152c796
AM
12407 }
12408
12409 if (bed->elf_backend_discard_info != NULL
12410 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12411 ret = TRUE;
12412
5241d853 12413 fini_reloc_cookie (&cookie, abfd);
c152c796 12414 }
ca92cecb 12415 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
12416
12417 if (info->eh_frame_hdr
12418 && !info->relocatable
12419 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12420 ret = TRUE;
12421
12422 return ret;
12423}
082b7297 12424
9659de1c
AM
12425/* For a SHT_GROUP section, return the group signature. For other
12426 sections, return the normal section name. */
12427
12428static const char *
12429section_signature (asection *sec)
12430{
12431 if ((sec->flags & SEC_GROUP) != 0
12432 && elf_next_in_group (sec) != NULL
12433 && elf_group_name (elf_next_in_group (sec)) != NULL)
12434 return elf_group_name (elf_next_in_group (sec));
12435 return sec->name;
12436}
12437
082b7297 12438void
9659de1c 12439_bfd_elf_section_already_linked (bfd *abfd, asection *sec,
c0f00686 12440 struct bfd_link_info *info)
082b7297
L
12441{
12442 flagword flags;
6d2cd210 12443 const char *name, *p;
082b7297
L
12444 struct bfd_section_already_linked *l;
12445 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666 12446
3d7f7666
L
12447 if (sec->output_section == bfd_abs_section_ptr)
12448 return;
082b7297
L
12449
12450 flags = sec->flags;
3d7f7666 12451
c2370991
AM
12452 /* Return if it isn't a linkonce section. A comdat group section
12453 also has SEC_LINK_ONCE set. */
12454 if ((flags & SEC_LINK_ONCE) == 0)
082b7297
L
12455 return;
12456
c2370991
AM
12457 /* Don't put group member sections on our list of already linked
12458 sections. They are handled as a group via their group section. */
12459 if (elf_sec_group (sec) != NULL)
12460 return;
3d7f7666 12461
082b7297
L
12462 /* FIXME: When doing a relocatable link, we may have trouble
12463 copying relocations in other sections that refer to local symbols
12464 in the section being discarded. Those relocations will have to
12465 be converted somehow; as of this writing I'm not sure that any of
12466 the backends handle that correctly.
12467
12468 It is tempting to instead not discard link once sections when
12469 doing a relocatable link (technically, they should be discarded
12470 whenever we are building constructors). However, that fails,
12471 because the linker winds up combining all the link once sections
12472 into a single large link once section, which defeats the purpose
12473 of having link once sections in the first place.
12474
12475 Also, not merging link once sections in a relocatable link
12476 causes trouble for MIPS ELF, which relies on link once semantics
12477 to handle the .reginfo section correctly. */
12478
9659de1c 12479 name = section_signature (sec);
082b7297 12480
0112cd26 12481 if (CONST_STRNEQ (name, ".gnu.linkonce.")
6d2cd210
JJ
12482 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12483 p++;
12484 else
12485 p = name;
12486
12487 already_linked_list = bfd_section_already_linked_table_lookup (p);
082b7297
L
12488
12489 for (l = already_linked_list->entry; l != NULL; l = l->next)
12490 {
c2370991
AM
12491 /* We may have 2 different types of sections on the list: group
12492 sections and linkonce sections. Match like sections. */
3d7f7666 12493 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9659de1c 12494 && strcmp (name, section_signature (l->sec)) == 0
082b7297
L
12495 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12496 {
12497 /* The section has already been linked. See if we should
6d2cd210 12498 issue a warning. */
082b7297
L
12499 switch (flags & SEC_LINK_DUPLICATES)
12500 {
12501 default:
12502 abort ();
12503
12504 case SEC_LINK_DUPLICATES_DISCARD:
12505 break;
12506
12507 case SEC_LINK_DUPLICATES_ONE_ONLY:
12508 (*_bfd_error_handler)
c93625e2 12509 (_("%B: ignoring duplicate section `%A'"),
d003868e 12510 abfd, sec);
082b7297
L
12511 break;
12512
12513 case SEC_LINK_DUPLICATES_SAME_SIZE:
12514 if (sec->size != l->sec->size)
12515 (*_bfd_error_handler)
c93625e2 12516 (_("%B: duplicate section `%A' has different size"),
d003868e 12517 abfd, sec);
082b7297 12518 break;
ea5158d8
DJ
12519
12520 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12521 if (sec->size != l->sec->size)
12522 (*_bfd_error_handler)
c93625e2 12523 (_("%B: duplicate section `%A' has different size"),
ea5158d8
DJ
12524 abfd, sec);
12525 else if (sec->size != 0)
12526 {
12527 bfd_byte *sec_contents, *l_sec_contents;
12528
12529 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12530 (*_bfd_error_handler)
c93625e2 12531 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12532 abfd, sec);
12533 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12534 &l_sec_contents))
12535 (*_bfd_error_handler)
c93625e2 12536 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12537 l->sec->owner, l->sec);
12538 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12539 (*_bfd_error_handler)
c93625e2 12540 (_("%B: warning: duplicate section `%A' has different contents"),
ea5158d8
DJ
12541 abfd, sec);
12542
12543 if (sec_contents)
12544 free (sec_contents);
12545 if (l_sec_contents)
12546 free (l_sec_contents);
12547 }
12548 break;
082b7297
L
12549 }
12550
12551 /* Set the output_section field so that lang_add_section
12552 does not create a lang_input_section structure for this
12553 section. Since there might be a symbol in the section
12554 being discarded, we must retain a pointer to the section
12555 which we are really going to use. */
12556 sec->output_section = bfd_abs_section_ptr;
12557 sec->kept_section = l->sec;
3b36f7e6 12558
082b7297 12559 if (flags & SEC_GROUP)
3d7f7666
L
12560 {
12561 asection *first = elf_next_in_group (sec);
12562 asection *s = first;
12563
12564 while (s != NULL)
12565 {
12566 s->output_section = bfd_abs_section_ptr;
12567 /* Record which group discards it. */
12568 s->kept_section = l->sec;
12569 s = elf_next_in_group (s);
12570 /* These lists are circular. */
12571 if (s == first)
12572 break;
12573 }
12574 }
082b7297
L
12575
12576 return;
12577 }
12578 }
12579
c2370991
AM
12580 /* A single member comdat group section may be discarded by a
12581 linkonce section and vice versa. */
12582
12583 if ((flags & SEC_GROUP) != 0)
3d7f7666 12584 {
c2370991
AM
12585 asection *first = elf_next_in_group (sec);
12586
12587 if (first != NULL && elf_next_in_group (first) == first)
12588 /* Check this single member group against linkonce sections. */
12589 for (l = already_linked_list->entry; l != NULL; l = l->next)
12590 if ((l->sec->flags & SEC_GROUP) == 0
12591 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12592 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12593 {
12594 first->output_section = bfd_abs_section_ptr;
12595 first->kept_section = l->sec;
12596 sec->output_section = bfd_abs_section_ptr;
12597 break;
12598 }
3d7f7666
L
12599 }
12600 else
c2370991 12601 /* Check this linkonce section against single member groups. */
6d2cd210
JJ
12602 for (l = already_linked_list->entry; l != NULL; l = l->next)
12603 if (l->sec->flags & SEC_GROUP)
12604 {
12605 asection *first = elf_next_in_group (l->sec);
12606
12607 if (first != NULL
12608 && elf_next_in_group (first) == first
c0f00686 12609 && bfd_elf_match_symbols_in_sections (first, sec, info))
6d2cd210
JJ
12610 {
12611 sec->output_section = bfd_abs_section_ptr;
c2370991 12612 sec->kept_section = first;
6d2cd210
JJ
12613 break;
12614 }
12615 }
12616
80c29487
JK
12617 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12618 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12619 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12620 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12621 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12622 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12623 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12624 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12625 The reverse order cannot happen as there is never a bfd with only the
12626 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12627 matter as here were are looking only for cross-bfd sections. */
12628
12629 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12630 for (l = already_linked_list->entry; l != NULL; l = l->next)
12631 if ((l->sec->flags & SEC_GROUP) == 0
12632 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12633 {
12634 if (abfd != l->sec->owner)
12635 sec->output_section = bfd_abs_section_ptr;
12636 break;
12637 }
12638
082b7297 12639 /* This is the first section with this name. Record it. */
a6626e8c 12640 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 12641 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
082b7297 12642}
81e1b023 12643
a4d8e49b
L
12644bfd_boolean
12645_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12646{
12647 return sym->st_shndx == SHN_COMMON;
12648}
12649
12650unsigned int
12651_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12652{
12653 return SHN_COMMON;
12654}
12655
12656asection *
12657_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12658{
12659 return bfd_com_section_ptr;
12660}
10455f89
HPN
12661
12662bfd_vma
12663_bfd_elf_default_got_elt_size (bfd *abfd,
12664 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12665 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12666 bfd *ibfd ATTRIBUTE_UNUSED,
12667 unsigned long symndx ATTRIBUTE_UNUSED)
12668{
12669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12670 return bed->s->arch_size / 8;
12671}
83bac4b0
NC
12672
12673/* Routines to support the creation of dynamic relocs. */
12674
83bac4b0
NC
12675/* Returns the name of the dynamic reloc section associated with SEC. */
12676
12677static const char *
12678get_dynamic_reloc_section_name (bfd * abfd,
12679 asection * sec,
12680 bfd_boolean is_rela)
12681{
ddcf1fcf
BS
12682 char *name;
12683 const char *old_name = bfd_get_section_name (NULL, sec);
12684 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 12685
ddcf1fcf 12686 if (old_name == NULL)
83bac4b0
NC
12687 return NULL;
12688
ddcf1fcf
BS
12689 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12690 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
12691
12692 return name;
12693}
12694
12695/* Returns the dynamic reloc section associated with SEC.
12696 If necessary compute the name of the dynamic reloc section based
12697 on SEC's name (looked up in ABFD's string table) and the setting
12698 of IS_RELA. */
12699
12700asection *
12701_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12702 asection * sec,
12703 bfd_boolean is_rela)
12704{
12705 asection * reloc_sec = elf_section_data (sec)->sreloc;
12706
12707 if (reloc_sec == NULL)
12708 {
12709 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12710
12711 if (name != NULL)
12712 {
12713 reloc_sec = bfd_get_section_by_name (abfd, name);
12714
12715 if (reloc_sec != NULL)
12716 elf_section_data (sec)->sreloc = reloc_sec;
12717 }
12718 }
12719
12720 return reloc_sec;
12721}
12722
12723/* Returns the dynamic reloc section associated with SEC. If the
12724 section does not exist it is created and attached to the DYNOBJ
12725 bfd and stored in the SRELOC field of SEC's elf_section_data
12726 structure.
f8076f98 12727
83bac4b0
NC
12728 ALIGNMENT is the alignment for the newly created section and
12729 IS_RELA defines whether the name should be .rela.<SEC's name>
12730 or .rel.<SEC's name>. The section name is looked up in the
12731 string table associated with ABFD. */
12732
12733asection *
12734_bfd_elf_make_dynamic_reloc_section (asection * sec,
12735 bfd * dynobj,
12736 unsigned int alignment,
12737 bfd * abfd,
12738 bfd_boolean is_rela)
12739{
12740 asection * reloc_sec = elf_section_data (sec)->sreloc;
12741
12742 if (reloc_sec == NULL)
12743 {
12744 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12745
12746 if (name == NULL)
12747 return NULL;
12748
12749 reloc_sec = bfd_get_section_by_name (dynobj, name);
12750
12751 if (reloc_sec == NULL)
12752 {
12753 flagword flags;
12754
12755 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12756 if ((sec->flags & SEC_ALLOC) != 0)
12757 flags |= SEC_ALLOC | SEC_LOAD;
12758
12759 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12760 if (reloc_sec != NULL)
12761 {
12762 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12763 reloc_sec = NULL;
12764 }
12765 }
12766
12767 elf_section_data (sec)->sreloc = reloc_sec;
12768 }
12769
12770 return reloc_sec;
12771}
1338dd10
PB
12772
12773/* Copy the ELF symbol type associated with a linker hash entry. */
12774void
12775_bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12776 struct bfd_link_hash_entry * hdest,
12777 struct bfd_link_hash_entry * hsrc)
12778{
12779 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12780 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12781
12782 ehdest->type = ehsrc->type;
35fc36a8 12783 ehdest->target_internal = ehsrc->target_internal;
1338dd10 12784}
351f65ca
L
12785
12786/* Append a RELA relocation REL to section S in BFD. */
12787
12788void
12789elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12790{
12791 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12792 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12793 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12794 bed->s->swap_reloca_out (abfd, rel, loc);
12795}
12796
12797/* Append a REL relocation REL to section S in BFD. */
12798
12799void
12800elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12801{
12802 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12803 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12804 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12805 bed->s->swap_reloca_out (abfd, rel, loc);
12806}